2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/sched.h>
30 #include <linux/types.h>
31 #include <linux/string.h>
32 #include <linux/netdevice.h>
33 #include <linux/inetdevice.h>
35 #include <linux/sysfs.h>
36 #include <linux/ctype.h>
37 #include <linux/inet.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/etherdevice.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <linux/nsproxy.h>
43 #include <linux/reciprocal_div.h>
47 #define to_dev(obj) container_of(obj, struct device, kobj)
48 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
54 static ssize_t bonding_show_bonds(struct class *cls,
55 struct class_attribute *attr,
59 container_of(attr, struct bond_net, class_attr_bonding_masters);
65 list_for_each_entry(bond, &bn->dev_list, bond_list) {
66 if (res > (PAGE_SIZE - IFNAMSIZ)) {
67 /* not enough space for another interface name */
68 if ((PAGE_SIZE - res) > 10)
70 res += sprintf(buf + res, "++more++ ");
73 res += sprintf(buf + res, "%s ", bond->dev->name);
76 buf[res-1] = '\n'; /* eat the leftover space */
82 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
86 list_for_each_entry(bond, &bn->dev_list, bond_list) {
87 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
94 * "store" function for the bond_masters attribute. This is what
95 * creates and deletes entire bonds.
97 * The class parameter is ignored.
101 static ssize_t bonding_store_bonds(struct class *cls,
102 struct class_attribute *attr,
103 const char *buffer, size_t count)
105 struct bond_net *bn =
106 container_of(attr, struct bond_net, class_attr_bonding_masters);
107 char command[IFNAMSIZ + 1] = {0, };
111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
117 if (command[0] == '+') {
118 pr_info("%s is being created...\n", ifname);
119 rv = bond_create(bn->net, ifname);
122 pr_info("%s already exists.\n", ifname);
124 pr_info("%s creation failed.\n", ifname);
127 } else if (command[0] == '-') {
128 struct net_device *bond_dev;
131 bond_dev = bond_get_by_name(bn, ifname);
133 pr_info("%s is being deleted...\n", ifname);
134 unregister_netdevice(bond_dev);
136 pr_err("unable to delete non-existent %s\n", ifname);
143 /* Always return either count or an error. If you return 0, you'll
144 * get called forever, which is bad.
149 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
153 static const void *bonding_namespace(struct class *cls,
154 const struct class_attribute *attr)
156 const struct bond_net *bn =
157 container_of(attr, struct bond_net, class_attr_bonding_masters);
161 /* class attribute for bond_masters file. This ends up in /sys/class/net */
162 static const struct class_attribute class_attr_bonding_masters = {
164 .name = "bonding_masters",
165 .mode = S_IWUSR | S_IRUGO,
167 .show = bonding_show_bonds,
168 .store = bonding_store_bonds,
169 .namespace = bonding_namespace,
173 * Show the slaves in the current bond.
175 static ssize_t bonding_show_slaves(struct device *d,
176 struct device_attribute *attr, char *buf)
178 struct bonding *bond = to_bond(d);
179 struct list_head *iter;
184 return restart_syscall();
186 bond_for_each_slave(bond, slave, iter) {
187 if (res > (PAGE_SIZE - IFNAMSIZ)) {
188 /* not enough space for another interface name */
189 if ((PAGE_SIZE - res) > 10)
190 res = PAGE_SIZE - 10;
191 res += sprintf(buf + res, "++more++ ");
194 res += sprintf(buf + res, "%s ", slave->dev->name);
200 buf[res-1] = '\n'; /* eat the leftover space */
206 * Set the slaves in the current bond.
207 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
208 * All hard work should be done there.
210 static ssize_t bonding_store_slaves(struct device *d,
211 struct device_attribute *attr,
212 const char *buffer, size_t count)
214 char command[IFNAMSIZ + 1] = { 0, };
216 int res, ret = count;
217 struct net_device *dev;
218 struct bonding *bond = to_bond(d);
221 return restart_syscall();
223 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
224 ifname = command + 1;
225 if ((strlen(command) <= 1) ||
226 !dev_valid_name(ifname))
229 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
231 pr_info("%s: Interface %s does not exist!\n",
232 bond->dev->name, ifname);
237 switch (command[0]) {
239 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
240 res = bond_enslave(bond->dev, dev);
244 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
245 res = bond_release(bond->dev, dev);
257 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
266 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
267 bonding_store_slaves);
270 * Show and set the bonding mode. The bond interface must be down to
273 static ssize_t bonding_show_mode(struct device *d,
274 struct device_attribute *attr, char *buf)
276 struct bonding *bond = to_bond(d);
278 return sprintf(buf, "%s %d\n",
279 bond_mode_tbl[bond->params.mode].modename,
283 static ssize_t bonding_store_mode(struct device *d,
284 struct device_attribute *attr,
285 const char *buf, size_t count)
288 struct bonding *bond = to_bond(d);
290 new_value = bond_parse_parm(buf, bond_mode_tbl);
292 pr_err("%s: Ignoring invalid mode value %.*s.\n",
293 bond->dev->name, (int)strlen(buf) - 1, buf);
297 return restart_syscall();
299 ret = bond_option_mode_set(bond, new_value);
301 pr_info("%s: setting mode to %s (%d).\n",
302 bond->dev->name, bond_mode_tbl[new_value].modename,
310 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
311 bonding_show_mode, bonding_store_mode);
314 * Show and set the bonding transmit hash method.
316 static ssize_t bonding_show_xmit_hash(struct device *d,
317 struct device_attribute *attr,
320 struct bonding *bond = to_bond(d);
322 return sprintf(buf, "%s %d\n",
323 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
324 bond->params.xmit_policy);
327 static ssize_t bonding_store_xmit_hash(struct device *d,
328 struct device_attribute *attr,
329 const char *buf, size_t count)
331 int new_value, ret = count;
332 struct bonding *bond = to_bond(d);
334 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
336 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
338 (int)strlen(buf) - 1, buf);
341 bond->params.xmit_policy = new_value;
342 pr_info("%s: setting xmit hash policy to %s (%d).\n",
344 xmit_hashtype_tbl[new_value].modename, new_value);
349 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
350 bonding_show_xmit_hash, bonding_store_xmit_hash);
353 * Show and set arp_validate.
355 static ssize_t bonding_show_arp_validate(struct device *d,
356 struct device_attribute *attr,
359 struct bonding *bond = to_bond(d);
361 return sprintf(buf, "%s %d\n",
362 arp_validate_tbl[bond->params.arp_validate].modename,
363 bond->params.arp_validate);
366 static ssize_t bonding_store_arp_validate(struct device *d,
367 struct device_attribute *attr,
368 const char *buf, size_t count)
370 struct bonding *bond = to_bond(d);
371 int new_value, ret = count;
374 return restart_syscall();
375 new_value = bond_parse_parm(buf, arp_validate_tbl);
377 pr_err("%s: Ignoring invalid arp_validate value %s\n",
378 bond->dev->name, buf);
382 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
383 pr_err("%s: arp_validate only supported in active-backup mode.\n",
388 pr_info("%s: setting arp_validate to %s (%d).\n",
389 bond->dev->name, arp_validate_tbl[new_value].modename,
392 if (bond->dev->flags & IFF_UP) {
394 bond->recv_probe = NULL;
395 else if (bond->params.arp_interval)
396 bond->recv_probe = bond_arp_rcv;
398 bond->params.arp_validate = new_value;
405 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
406 bonding_store_arp_validate);
408 * Show and set arp_all_targets.
410 static ssize_t bonding_show_arp_all_targets(struct device *d,
411 struct device_attribute *attr,
414 struct bonding *bond = to_bond(d);
415 int value = bond->params.arp_all_targets;
417 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
421 static ssize_t bonding_store_arp_all_targets(struct device *d,
422 struct device_attribute *attr,
423 const char *buf, size_t count)
425 struct bonding *bond = to_bond(d);
428 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
430 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
431 bond->dev->name, buf);
434 pr_info("%s: setting arp_all_targets to %s (%d).\n",
435 bond->dev->name, arp_all_targets_tbl[new_value].modename,
438 bond->params.arp_all_targets = new_value;
443 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
444 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
447 * Show and store fail_over_mac. User only allowed to change the
448 * value when there are no slaves.
450 static ssize_t bonding_show_fail_over_mac(struct device *d,
451 struct device_attribute *attr,
454 struct bonding *bond = to_bond(d);
456 return sprintf(buf, "%s %d\n",
457 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
458 bond->params.fail_over_mac);
461 static ssize_t bonding_store_fail_over_mac(struct device *d,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
465 int new_value, ret = count;
466 struct bonding *bond = to_bond(d);
469 return restart_syscall();
471 if (bond_has_slaves(bond)) {
472 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
478 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
480 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
481 bond->dev->name, buf);
486 bond->params.fail_over_mac = new_value;
487 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
488 bond->dev->name, fail_over_mac_tbl[new_value].modename,
496 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
497 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
500 * Show and set the arp timer interval. There are two tricky bits
501 * here. First, if ARP monitoring is activated, then we must disable
502 * MII monitoring. Second, if the ARP timer isn't running, we must
505 static ssize_t bonding_show_arp_interval(struct device *d,
506 struct device_attribute *attr,
509 struct bonding *bond = to_bond(d);
511 return sprintf(buf, "%d\n", bond->params.arp_interval);
514 static ssize_t bonding_store_arp_interval(struct device *d,
515 struct device_attribute *attr,
516 const char *buf, size_t count)
518 struct bonding *bond = to_bond(d);
519 int new_value, ret = count;
522 return restart_syscall();
523 if (sscanf(buf, "%d", &new_value) != 1) {
524 pr_err("%s: no arp_interval value specified.\n",
530 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
531 bond->dev->name, new_value, INT_MAX);
535 if (bond->params.mode == BOND_MODE_ALB ||
536 bond->params.mode == BOND_MODE_TLB) {
537 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
538 bond->dev->name, bond->dev->name);
542 pr_info("%s: Setting ARP monitoring interval to %d.\n",
543 bond->dev->name, new_value);
544 bond->params.arp_interval = new_value;
546 if (bond->params.miimon) {
547 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
548 bond->dev->name, bond->dev->name);
549 bond->params.miimon = 0;
551 if (!bond->params.arp_targets[0])
552 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
555 if (bond->dev->flags & IFF_UP) {
556 /* If the interface is up, we may need to fire off
557 * the ARP timer. If the interface is down, the
558 * timer will get fired off when the open function
562 if (bond->params.arp_validate)
563 bond->recv_probe = NULL;
564 cancel_delayed_work_sync(&bond->arp_work);
566 /* arp_validate can be set only in active-backup mode */
567 if (bond->params.arp_validate)
568 bond->recv_probe = bond_arp_rcv;
569 cancel_delayed_work_sync(&bond->mii_work);
570 queue_delayed_work(bond->wq, &bond->arp_work, 0);
577 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
578 bonding_show_arp_interval, bonding_store_arp_interval);
581 * Show and set the arp targets.
583 static ssize_t bonding_show_arp_targets(struct device *d,
584 struct device_attribute *attr,
588 struct bonding *bond = to_bond(d);
590 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
591 if (bond->params.arp_targets[i])
592 res += sprintf(buf + res, "%pI4 ",
593 &bond->params.arp_targets[i]);
596 buf[res-1] = '\n'; /* eat the leftover space */
600 static ssize_t bonding_store_arp_targets(struct device *d,
601 struct device_attribute *attr,
602 const char *buf, size_t count)
604 struct bonding *bond = to_bond(d);
605 struct list_head *iter;
607 __be32 newtarget, *targets;
608 unsigned long *targets_rx;
609 int ind, i, j, ret = -EINVAL;
612 return restart_syscall();
614 targets = bond->params.arp_targets;
615 newtarget = in_aton(buf + 1);
618 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
619 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
620 bond->dev->name, &newtarget);
624 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
625 pr_err("%s: ARP target %pI4 is already present\n",
626 bond->dev->name, &newtarget);
630 ind = bond_get_targets_ip(targets, 0); /* first free slot */
632 pr_err("%s: ARP target table is full!\n",
637 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
639 /* not to race with bond_arp_rcv */
640 write_lock_bh(&bond->lock);
641 bond_for_each_slave(bond, slave, iter)
642 slave->target_last_arp_rx[ind] = jiffies;
643 targets[ind] = newtarget;
644 write_unlock_bh(&bond->lock);
645 } else if (buf[0] == '-') {
646 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
647 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
648 bond->dev->name, &newtarget);
652 ind = bond_get_targets_ip(targets, newtarget);
654 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
655 bond->dev->name, &newtarget);
659 if (ind == 0 && !targets[1] && bond->params.arp_interval)
660 pr_warn("%s: removing last arp target with arp_interval on\n",
663 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
666 write_lock_bh(&bond->lock);
667 bond_for_each_slave(bond, slave, iter) {
668 targets_rx = slave->target_last_arp_rx;
670 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
671 targets_rx[j] = targets_rx[j+1];
674 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
675 targets[i] = targets[i+1];
677 write_unlock_bh(&bond->lock);
679 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
690 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
693 * Show and set the up and down delays. These must be multiples of the
694 * MII monitoring value, and are stored internally as the multiplier.
695 * Thus, we must translate to MS for the real world.
697 static ssize_t bonding_show_downdelay(struct device *d,
698 struct device_attribute *attr,
701 struct bonding *bond = to_bond(d);
703 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
706 static ssize_t bonding_store_downdelay(struct device *d,
707 struct device_attribute *attr,
708 const char *buf, size_t count)
710 int new_value, ret = count;
711 struct bonding *bond = to_bond(d);
713 if (!(bond->params.miimon)) {
714 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
720 if (sscanf(buf, "%d", &new_value) != 1) {
721 pr_err("%s: no down delay value specified.\n", bond->dev->name);
726 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
727 bond->dev->name, new_value, 0, INT_MAX);
731 if ((new_value % bond->params.miimon) != 0) {
732 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
733 bond->dev->name, new_value,
735 (new_value / bond->params.miimon) *
736 bond->params.miimon);
738 bond->params.downdelay = new_value / bond->params.miimon;
739 pr_info("%s: Setting down delay to %d.\n",
741 bond->params.downdelay * bond->params.miimon);
748 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
749 bonding_show_downdelay, bonding_store_downdelay);
751 static ssize_t bonding_show_updelay(struct device *d,
752 struct device_attribute *attr,
755 struct bonding *bond = to_bond(d);
757 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
761 static ssize_t bonding_store_updelay(struct device *d,
762 struct device_attribute *attr,
763 const char *buf, size_t count)
765 int new_value, ret = count;
766 struct bonding *bond = to_bond(d);
768 if (!(bond->params.miimon)) {
769 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
775 if (sscanf(buf, "%d", &new_value) != 1) {
776 pr_err("%s: no up delay value specified.\n",
782 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
783 bond->dev->name, new_value, 0, INT_MAX);
787 if ((new_value % bond->params.miimon) != 0) {
788 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
789 bond->dev->name, new_value,
791 (new_value / bond->params.miimon) *
792 bond->params.miimon);
794 bond->params.updelay = new_value / bond->params.miimon;
795 pr_info("%s: Setting up delay to %d.\n",
797 bond->params.updelay * bond->params.miimon);
803 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
804 bonding_show_updelay, bonding_store_updelay);
807 * Show and set the LACP interval. Interface must be down, and the mode
808 * must be set to 802.3ad mode.
810 static ssize_t bonding_show_lacp(struct device *d,
811 struct device_attribute *attr,
814 struct bonding *bond = to_bond(d);
816 return sprintf(buf, "%s %d\n",
817 bond_lacp_tbl[bond->params.lacp_fast].modename,
818 bond->params.lacp_fast);
821 static ssize_t bonding_store_lacp(struct device *d,
822 struct device_attribute *attr,
823 const char *buf, size_t count)
825 struct bonding *bond = to_bond(d);
826 int new_value, ret = count;
829 return restart_syscall();
831 if (bond->dev->flags & IFF_UP) {
832 pr_err("%s: Unable to update LACP rate because interface is up.\n",
838 if (bond->params.mode != BOND_MODE_8023AD) {
839 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
845 new_value = bond_parse_parm(buf, bond_lacp_tbl);
847 if ((new_value == 1) || (new_value == 0)) {
848 bond->params.lacp_fast = new_value;
849 bond_3ad_update_lacp_rate(bond);
850 pr_info("%s: Setting LACP rate to %s (%d).\n",
851 bond->dev->name, bond_lacp_tbl[new_value].modename,
854 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
855 bond->dev->name, (int)strlen(buf) - 1, buf);
863 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
864 bonding_show_lacp, bonding_store_lacp);
866 static ssize_t bonding_show_min_links(struct device *d,
867 struct device_attribute *attr,
870 struct bonding *bond = to_bond(d);
872 return sprintf(buf, "%d\n", bond->params.min_links);
875 static ssize_t bonding_store_min_links(struct device *d,
876 struct device_attribute *attr,
877 const char *buf, size_t count)
879 struct bonding *bond = to_bond(d);
881 unsigned int new_value;
883 ret = kstrtouint(buf, 0, &new_value);
885 pr_err("%s: Ignoring invalid min links value %s.\n",
886 bond->dev->name, buf);
890 pr_info("%s: Setting min links value to %u\n",
891 bond->dev->name, new_value);
892 bond->params.min_links = new_value;
895 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
896 bonding_show_min_links, bonding_store_min_links);
898 static ssize_t bonding_show_ad_select(struct device *d,
899 struct device_attribute *attr,
902 struct bonding *bond = to_bond(d);
904 return sprintf(buf, "%s %d\n",
905 ad_select_tbl[bond->params.ad_select].modename,
906 bond->params.ad_select);
910 static ssize_t bonding_store_ad_select(struct device *d,
911 struct device_attribute *attr,
912 const char *buf, size_t count)
914 int new_value, ret = count;
915 struct bonding *bond = to_bond(d);
917 if (bond->dev->flags & IFF_UP) {
918 pr_err("%s: Unable to update ad_select because interface is up.\n",
924 new_value = bond_parse_parm(buf, ad_select_tbl);
926 if (new_value != -1) {
927 bond->params.ad_select = new_value;
928 pr_info("%s: Setting ad_select to %s (%d).\n",
929 bond->dev->name, ad_select_tbl[new_value].modename,
932 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
933 bond->dev->name, (int)strlen(buf) - 1, buf);
939 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
940 bonding_show_ad_select, bonding_store_ad_select);
943 * Show and set the number of peer notifications to send after a failover event.
945 static ssize_t bonding_show_num_peer_notif(struct device *d,
946 struct device_attribute *attr,
949 struct bonding *bond = to_bond(d);
950 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
953 static ssize_t bonding_store_num_peer_notif(struct device *d,
954 struct device_attribute *attr,
955 const char *buf, size_t count)
957 struct bonding *bond = to_bond(d);
958 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
959 return err ? err : count;
961 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
962 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
963 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
964 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
967 * Show and set the MII monitor interval. There are two tricky bits
968 * here. First, if MII monitoring is activated, then we must disable
969 * ARP monitoring. Second, if the timer isn't running, we must
972 static ssize_t bonding_show_miimon(struct device *d,
973 struct device_attribute *attr,
976 struct bonding *bond = to_bond(d);
978 return sprintf(buf, "%d\n", bond->params.miimon);
981 static ssize_t bonding_store_miimon(struct device *d,
982 struct device_attribute *attr,
983 const char *buf, size_t count)
985 int new_value, ret = count;
986 struct bonding *bond = to_bond(d);
989 return restart_syscall();
990 if (sscanf(buf, "%d", &new_value) != 1) {
991 pr_err("%s: no miimon value specified.\n",
997 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
998 bond->dev->name, new_value, 0, INT_MAX);
1002 pr_info("%s: Setting MII monitoring interval to %d.\n",
1003 bond->dev->name, new_value);
1004 bond->params.miimon = new_value;
1005 if (bond->params.updelay)
1006 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1008 bond->params.updelay * bond->params.miimon);
1009 if (bond->params.downdelay)
1010 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1012 bond->params.downdelay * bond->params.miimon);
1013 if (new_value && bond->params.arp_interval) {
1014 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1016 bond->params.arp_interval = 0;
1017 if (bond->params.arp_validate)
1018 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1020 if (bond->dev->flags & IFF_UP) {
1021 /* If the interface is up, we may need to fire off
1022 * the MII timer. If the interface is down, the
1023 * timer will get fired off when the open function
1027 cancel_delayed_work_sync(&bond->mii_work);
1029 cancel_delayed_work_sync(&bond->arp_work);
1030 queue_delayed_work(bond->wq, &bond->mii_work, 0);
1037 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1038 bonding_show_miimon, bonding_store_miimon);
1041 * Show and set the primary slave. The store function is much
1042 * simpler than bonding_store_slaves function because it only needs to
1043 * handle one interface name.
1044 * The bond must be a mode that supports a primary for this be
1047 static ssize_t bonding_show_primary(struct device *d,
1048 struct device_attribute *attr,
1052 struct bonding *bond = to_bond(d);
1054 if (bond->primary_slave)
1055 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1060 static ssize_t bonding_store_primary(struct device *d,
1061 struct device_attribute *attr,
1062 const char *buf, size_t count)
1064 struct bonding *bond = to_bond(d);
1065 struct list_head *iter;
1066 char ifname[IFNAMSIZ];
1067 struct slave *slave;
1069 if (!rtnl_trylock())
1070 return restart_syscall();
1072 read_lock(&bond->lock);
1073 write_lock_bh(&bond->curr_slave_lock);
1075 if (!USES_PRIMARY(bond->params.mode)) {
1076 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1077 bond->dev->name, bond->dev->name, bond->params.mode);
1081 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1083 /* check to see if we are clearing primary */
1084 if (!strlen(ifname) || buf[0] == '\n') {
1085 pr_info("%s: Setting primary slave to None.\n",
1087 bond->primary_slave = NULL;
1088 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1089 bond_select_active_slave(bond);
1093 bond_for_each_slave(bond, slave, iter) {
1094 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1095 pr_info("%s: Setting %s as primary slave.\n",
1096 bond->dev->name, slave->dev->name);
1097 bond->primary_slave = slave;
1098 strcpy(bond->params.primary, slave->dev->name);
1099 bond_select_active_slave(bond);
1104 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1105 bond->params.primary[IFNAMSIZ - 1] = 0;
1107 pr_info("%s: Recording %s as primary, "
1108 "but it has not been enslaved to %s yet.\n",
1109 bond->dev->name, ifname, bond->dev->name);
1111 write_unlock_bh(&bond->curr_slave_lock);
1112 read_unlock(&bond->lock);
1113 unblock_netpoll_tx();
1118 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1119 bonding_show_primary, bonding_store_primary);
1122 * Show and set the primary_reselect flag.
1124 static ssize_t bonding_show_primary_reselect(struct device *d,
1125 struct device_attribute *attr,
1128 struct bonding *bond = to_bond(d);
1130 return sprintf(buf, "%s %d\n",
1131 pri_reselect_tbl[bond->params.primary_reselect].modename,
1132 bond->params.primary_reselect);
1135 static ssize_t bonding_store_primary_reselect(struct device *d,
1136 struct device_attribute *attr,
1137 const char *buf, size_t count)
1139 int new_value, ret = count;
1140 struct bonding *bond = to_bond(d);
1142 if (!rtnl_trylock())
1143 return restart_syscall();
1145 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1146 if (new_value < 0) {
1147 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1149 (int) strlen(buf) - 1, buf);
1154 bond->params.primary_reselect = new_value;
1155 pr_info("%s: setting primary_reselect to %s (%d).\n",
1156 bond->dev->name, pri_reselect_tbl[new_value].modename,
1160 read_lock(&bond->lock);
1161 write_lock_bh(&bond->curr_slave_lock);
1162 bond_select_active_slave(bond);
1163 write_unlock_bh(&bond->curr_slave_lock);
1164 read_unlock(&bond->lock);
1165 unblock_netpoll_tx();
1170 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1171 bonding_show_primary_reselect,
1172 bonding_store_primary_reselect);
1175 * Show and set the use_carrier flag.
1177 static ssize_t bonding_show_carrier(struct device *d,
1178 struct device_attribute *attr,
1181 struct bonding *bond = to_bond(d);
1183 return sprintf(buf, "%d\n", bond->params.use_carrier);
1186 static ssize_t bonding_store_carrier(struct device *d,
1187 struct device_attribute *attr,
1188 const char *buf, size_t count)
1190 int new_value, ret = count;
1191 struct bonding *bond = to_bond(d);
1194 if (sscanf(buf, "%d", &new_value) != 1) {
1195 pr_err("%s: no use_carrier value specified.\n",
1200 if ((new_value == 0) || (new_value == 1)) {
1201 bond->params.use_carrier = new_value;
1202 pr_info("%s: Setting use_carrier to %d.\n",
1203 bond->dev->name, new_value);
1205 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1206 bond->dev->name, new_value);
1211 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1212 bonding_show_carrier, bonding_store_carrier);
1216 * Show and set currently active_slave.
1218 static ssize_t bonding_show_active_slave(struct device *d,
1219 struct device_attribute *attr,
1222 struct bonding *bond = to_bond(d);
1223 struct net_device *slave_dev;
1227 slave_dev = bond_option_active_slave_get_rcu(bond);
1229 count = sprintf(buf, "%s\n", slave_dev->name);
1235 static ssize_t bonding_store_active_slave(struct device *d,
1236 struct device_attribute *attr,
1237 const char *buf, size_t count)
1240 struct bonding *bond = to_bond(d);
1241 char ifname[IFNAMSIZ];
1242 struct net_device *dev;
1244 if (!rtnl_trylock())
1245 return restart_syscall();
1247 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1248 if (!strlen(ifname) || buf[0] == '\n') {
1251 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1258 ret = bond_option_active_slave_set(bond, dev);
1268 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1269 bonding_show_active_slave, bonding_store_active_slave);
1273 * Show link status of the bond interface.
1275 static ssize_t bonding_show_mii_status(struct device *d,
1276 struct device_attribute *attr,
1279 struct bonding *bond = to_bond(d);
1281 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1283 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1286 * Show current 802.3ad aggregator ID.
1288 static ssize_t bonding_show_ad_aggregator(struct device *d,
1289 struct device_attribute *attr,
1293 struct bonding *bond = to_bond(d);
1295 if (bond->params.mode == BOND_MODE_8023AD) {
1296 struct ad_info ad_info;
1297 count = sprintf(buf, "%d\n",
1298 bond_3ad_get_active_agg_info(bond, &ad_info)
1299 ? 0 : ad_info.aggregator_id);
1304 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1308 * Show number of active 802.3ad ports.
1310 static ssize_t bonding_show_ad_num_ports(struct device *d,
1311 struct device_attribute *attr,
1315 struct bonding *bond = to_bond(d);
1317 if (bond->params.mode == BOND_MODE_8023AD) {
1318 struct ad_info ad_info;
1319 count = sprintf(buf, "%d\n",
1320 bond_3ad_get_active_agg_info(bond, &ad_info)
1321 ? 0 : ad_info.ports);
1326 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1330 * Show current 802.3ad actor key.
1332 static ssize_t bonding_show_ad_actor_key(struct device *d,
1333 struct device_attribute *attr,
1337 struct bonding *bond = to_bond(d);
1339 if (bond->params.mode == BOND_MODE_8023AD) {
1340 struct ad_info ad_info;
1341 count = sprintf(buf, "%d\n",
1342 bond_3ad_get_active_agg_info(bond, &ad_info)
1343 ? 0 : ad_info.actor_key);
1348 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1352 * Show current 802.3ad partner key.
1354 static ssize_t bonding_show_ad_partner_key(struct device *d,
1355 struct device_attribute *attr,
1359 struct bonding *bond = to_bond(d);
1361 if (bond->params.mode == BOND_MODE_8023AD) {
1362 struct ad_info ad_info;
1363 count = sprintf(buf, "%d\n",
1364 bond_3ad_get_active_agg_info(bond, &ad_info)
1365 ? 0 : ad_info.partner_key);
1370 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1374 * Show current 802.3ad partner mac.
1376 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1377 struct device_attribute *attr,
1381 struct bonding *bond = to_bond(d);
1383 if (bond->params.mode == BOND_MODE_8023AD) {
1384 struct ad_info ad_info;
1385 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1386 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1391 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1394 * Show the queue_ids of the slaves in the current bond.
1396 static ssize_t bonding_show_queue_id(struct device *d,
1397 struct device_attribute *attr,
1400 struct bonding *bond = to_bond(d);
1401 struct list_head *iter;
1402 struct slave *slave;
1405 if (!rtnl_trylock())
1406 return restart_syscall();
1408 bond_for_each_slave(bond, slave, iter) {
1409 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1410 /* not enough space for another interface_name:queue_id pair */
1411 if ((PAGE_SIZE - res) > 10)
1412 res = PAGE_SIZE - 10;
1413 res += sprintf(buf + res, "++more++ ");
1416 res += sprintf(buf + res, "%s:%d ",
1417 slave->dev->name, slave->queue_id);
1420 buf[res-1] = '\n'; /* eat the leftover space */
1428 * Set the queue_ids of the slaves in the current bond. The bond
1429 * interface must be enslaved for this to work.
1431 static ssize_t bonding_store_queue_id(struct device *d,
1432 struct device_attribute *attr,
1433 const char *buffer, size_t count)
1435 struct slave *slave, *update_slave;
1436 struct bonding *bond = to_bond(d);
1437 struct list_head *iter;
1441 struct net_device *sdev = NULL;
1443 if (!rtnl_trylock())
1444 return restart_syscall();
1446 /* delim will point to queue id if successful */
1447 delim = strchr(buffer, ':');
1452 * Terminate string that points to device name and bump it
1453 * up one, so we can read the queue id there.
1456 if (sscanf(++delim, "%hd\n", &qid) != 1)
1459 /* Check buffer length, valid ifname and queue id */
1460 if (strlen(buffer) > IFNAMSIZ ||
1461 !dev_valid_name(buffer) ||
1462 qid > bond->dev->real_num_tx_queues)
1465 /* Get the pointer to that interface if it exists */
1466 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1470 /* Search for thes slave and check for duplicate qids */
1471 update_slave = NULL;
1472 bond_for_each_slave(bond, slave, iter) {
1473 if (sdev == slave->dev)
1475 * We don't need to check the matching
1476 * slave for dups, since we're overwriting it
1478 update_slave = slave;
1479 else if (qid && qid == slave->queue_id) {
1487 /* Actually set the qids for the slave */
1488 update_slave->queue_id = qid;
1495 pr_info("invalid input for queue_id set for %s.\n",
1501 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1502 bonding_store_queue_id);
1506 * Show and set the all_slaves_active flag.
1508 static ssize_t bonding_show_slaves_active(struct device *d,
1509 struct device_attribute *attr,
1512 struct bonding *bond = to_bond(d);
1514 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1517 static ssize_t bonding_store_slaves_active(struct device *d,
1518 struct device_attribute *attr,
1519 const char *buf, size_t count)
1521 struct bonding *bond = to_bond(d);
1522 int new_value, ret = count;
1523 struct list_head *iter;
1524 struct slave *slave;
1526 if (!rtnl_trylock())
1527 return restart_syscall();
1529 if (sscanf(buf, "%d", &new_value) != 1) {
1530 pr_err("%s: no all_slaves_active value specified.\n",
1536 if (new_value == bond->params.all_slaves_active)
1539 if ((new_value == 0) || (new_value == 1)) {
1540 bond->params.all_slaves_active = new_value;
1542 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1543 bond->dev->name, new_value);
1548 bond_for_each_slave(bond, slave, iter) {
1549 if (!bond_is_active_slave(slave)) {
1551 slave->inactive = 0;
1553 slave->inactive = 1;
1560 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1561 bonding_show_slaves_active, bonding_store_slaves_active);
1564 * Show and set the number of IGMP membership reports to send on link failure
1566 static ssize_t bonding_show_resend_igmp(struct device *d,
1567 struct device_attribute *attr,
1570 struct bonding *bond = to_bond(d);
1572 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1575 static ssize_t bonding_store_resend_igmp(struct device *d,
1576 struct device_attribute *attr,
1577 const char *buf, size_t count)
1579 int new_value, ret = count;
1580 struct bonding *bond = to_bond(d);
1582 if (sscanf(buf, "%d", &new_value) != 1) {
1583 pr_err("%s: no resend_igmp value specified.\n",
1589 if (new_value < 0 || new_value > 255) {
1590 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1591 bond->dev->name, new_value);
1596 pr_info("%s: Setting resend_igmp to %d.\n",
1597 bond->dev->name, new_value);
1598 bond->params.resend_igmp = new_value;
1603 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1604 bonding_show_resend_igmp, bonding_store_resend_igmp);
1607 static ssize_t bonding_show_lp_interval(struct device *d,
1608 struct device_attribute *attr,
1611 struct bonding *bond = to_bond(d);
1612 return sprintf(buf, "%d\n", bond->params.lp_interval);
1615 static ssize_t bonding_store_lp_interval(struct device *d,
1616 struct device_attribute *attr,
1617 const char *buf, size_t count)
1619 struct bonding *bond = to_bond(d);
1620 int new_value, ret = count;
1622 if (sscanf(buf, "%d", &new_value) != 1) {
1623 pr_err("%s: no lp interval value specified.\n",
1629 if (new_value <= 0) {
1630 pr_err ("%s: lp_interval must be between 1 and %d\n",
1631 bond->dev->name, INT_MAX);
1636 bond->params.lp_interval = new_value;
1641 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1642 bonding_show_lp_interval, bonding_store_lp_interval);
1644 static ssize_t bonding_show_packets_per_slave(struct device *d,
1645 struct device_attribute *attr,
1648 struct bonding *bond = to_bond(d);
1649 int packets_per_slave = bond->params.packets_per_slave;
1651 if (packets_per_slave > 1)
1652 packets_per_slave = reciprocal_value(packets_per_slave);
1654 return sprintf(buf, "%d\n", packets_per_slave);
1657 static ssize_t bonding_store_packets_per_slave(struct device *d,
1658 struct device_attribute *attr,
1659 const char *buf, size_t count)
1661 struct bonding *bond = to_bond(d);
1662 int new_value, ret = count;
1664 if (sscanf(buf, "%d", &new_value) != 1) {
1665 pr_err("%s: no packets_per_slave value specified.\n",
1670 if (new_value < 0 || new_value > USHRT_MAX) {
1671 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1672 bond->dev->name, USHRT_MAX);
1676 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1677 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1680 bond->params.packets_per_slave = reciprocal_value(new_value);
1682 bond->params.packets_per_slave = new_value;
1687 static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1688 bonding_show_packets_per_slave,
1689 bonding_store_packets_per_slave);
1691 static struct attribute *per_bond_attrs[] = {
1692 &dev_attr_slaves.attr,
1693 &dev_attr_mode.attr,
1694 &dev_attr_fail_over_mac.attr,
1695 &dev_attr_arp_validate.attr,
1696 &dev_attr_arp_all_targets.attr,
1697 &dev_attr_arp_interval.attr,
1698 &dev_attr_arp_ip_target.attr,
1699 &dev_attr_downdelay.attr,
1700 &dev_attr_updelay.attr,
1701 &dev_attr_lacp_rate.attr,
1702 &dev_attr_ad_select.attr,
1703 &dev_attr_xmit_hash_policy.attr,
1704 &dev_attr_num_grat_arp.attr,
1705 &dev_attr_num_unsol_na.attr,
1706 &dev_attr_miimon.attr,
1707 &dev_attr_primary.attr,
1708 &dev_attr_primary_reselect.attr,
1709 &dev_attr_use_carrier.attr,
1710 &dev_attr_active_slave.attr,
1711 &dev_attr_mii_status.attr,
1712 &dev_attr_ad_aggregator.attr,
1713 &dev_attr_ad_num_ports.attr,
1714 &dev_attr_ad_actor_key.attr,
1715 &dev_attr_ad_partner_key.attr,
1716 &dev_attr_ad_partner_mac.attr,
1717 &dev_attr_queue_id.attr,
1718 &dev_attr_all_slaves_active.attr,
1719 &dev_attr_resend_igmp.attr,
1720 &dev_attr_min_links.attr,
1721 &dev_attr_lp_interval.attr,
1722 &dev_attr_packets_per_slave.attr,
1726 static struct attribute_group bonding_group = {
1728 .attrs = per_bond_attrs,
1732 * Initialize sysfs. This sets up the bonding_masters file in
1735 int bond_create_sysfs(struct bond_net *bn)
1739 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1740 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1742 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
1744 * Permit multiple loads of the module by ignoring failures to
1745 * create the bonding_masters sysfs file. Bonding devices
1746 * created by second or subsequent loads of the module will
1747 * not be listed in, or controllable by, bonding_masters, but
1748 * will have the usual "bonding" sysfs directory.
1750 * This is done to preserve backwards compatibility for
1751 * initscripts/sysconfig, which load bonding multiple times to
1752 * configure multiple bonding devices.
1754 if (ret == -EEXIST) {
1755 /* Is someone being kinky and naming a device bonding_master? */
1756 if (__dev_get_by_name(bn->net,
1757 class_attr_bonding_masters.attr.name))
1758 pr_err("network device named %s already exists in sysfs",
1759 class_attr_bonding_masters.attr.name);
1768 * Remove /sys/class/net/bonding_masters.
1770 void bond_destroy_sysfs(struct bond_net *bn)
1772 netdev_class_remove_file(&bn->class_attr_bonding_masters);
1776 * Initialize sysfs for each bond. This sets up and registers
1777 * the 'bondctl' directory for each individual bond under /sys/class/net.
1779 void bond_prepare_sysfs_group(struct bonding *bond)
1781 bond->dev->sysfs_groups[0] = &bonding_group;