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>
29 #include <linux/sysdev.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/netdevice.h>
34 #include <linux/inetdevice.h>
36 #include <linux/sysfs.h>
37 #include <linux/ctype.h>
38 #include <linux/inet.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/etherdevice.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <linux/nsproxy.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,
58 struct net *net = current->nsproxy->net_ns;
59 struct bond_net *bn = net_generic(net, bond_net_id);
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 net *net, const char *ifname)
84 struct bond_net *bn = net_generic(net, bond_net_id);
87 list_for_each_entry(bond, &bn->dev_list, bond_list) {
88 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
95 * "store" function for the bond_masters attribute. This is what
96 * creates and deletes entire bonds.
98 * The class parameter is ignored.
102 static ssize_t bonding_store_bonds(struct class *cls,
103 struct class_attribute *attr,
104 const char *buffer, size_t count)
106 struct net *net = current->nsproxy->net_ns;
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(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(net, 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 /* class attribute for bond_masters file. This ends up in /sys/class/net */
154 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
155 bonding_show_bonds, bonding_store_bonds);
157 int bond_create_slave_symlinks(struct net_device *master,
158 struct net_device *slave)
160 char linkname[IFNAMSIZ+7];
163 /* first, create a link from the slave back to the master */
164 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
168 /* next, create a link from the master to the slave */
169 sprintf(linkname, "slave_%s", slave->name);
170 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
176 void bond_destroy_slave_symlinks(struct net_device *master,
177 struct net_device *slave)
179 char linkname[IFNAMSIZ+7];
181 sysfs_remove_link(&(slave->dev.kobj), "master");
182 sprintf(linkname, "slave_%s", slave->name);
183 sysfs_remove_link(&(master->dev.kobj), linkname);
188 * Show the slaves in the current bond.
190 static ssize_t bonding_show_slaves(struct device *d,
191 struct device_attribute *attr, char *buf)
195 struct bonding *bond = to_bond(d);
197 read_lock(&bond->lock);
198 bond_for_each_slave(bond, slave, i) {
199 if (res > (PAGE_SIZE - IFNAMSIZ)) {
200 /* not enough space for another interface name */
201 if ((PAGE_SIZE - res) > 10)
202 res = PAGE_SIZE - 10;
203 res += sprintf(buf + res, "++more++ ");
206 res += sprintf(buf + res, "%s ", slave->dev->name);
208 read_unlock(&bond->lock);
210 buf[res-1] = '\n'; /* eat the leftover space */
215 * Set the slaves in the current bond. The bond interface must be
216 * up for this to succeed.
217 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
218 * All hard work should be done there.
220 static ssize_t bonding_store_slaves(struct device *d,
221 struct device_attribute *attr,
222 const char *buffer, size_t count)
224 char command[IFNAMSIZ + 1] = { 0, };
226 int res, ret = count;
227 struct net_device *dev;
228 struct bonding *bond = to_bond(d);
230 /* Quick sanity check -- is the bond interface up? */
231 if (!(bond->dev->flags & IFF_UP)) {
232 pr_warning("%s: doing slave updates when interface is down.\n",
237 return restart_syscall();
239 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
240 ifname = command + 1;
241 if ((strlen(command) <= 1) ||
242 !dev_valid_name(ifname))
245 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
247 pr_info("%s: Interface %s does not exist!\n",
248 bond->dev->name, ifname);
253 switch (command[0]) {
255 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
256 res = bond_enslave(bond->dev, dev);
260 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
261 res = bond_release(bond->dev, dev);
273 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
282 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
283 bonding_store_slaves);
286 * Show and set the bonding mode. The bond interface must be down to
289 static ssize_t bonding_show_mode(struct device *d,
290 struct device_attribute *attr, char *buf)
292 struct bonding *bond = to_bond(d);
294 return sprintf(buf, "%s %d\n",
295 bond_mode_tbl[bond->params.mode].modename,
299 static ssize_t bonding_store_mode(struct device *d,
300 struct device_attribute *attr,
301 const char *buf, size_t count)
303 int new_value, ret = count;
304 struct bonding *bond = to_bond(d);
306 if (bond->dev->flags & IFF_UP) {
307 pr_err("unable to update mode of %s because interface is up.\n",
313 new_value = bond_parse_parm(buf, bond_mode_tbl);
315 pr_err("%s: Ignoring invalid mode value %.*s.\n",
316 bond->dev->name, (int)strlen(buf) - 1, buf);
320 if ((new_value == BOND_MODE_ALB ||
321 new_value == BOND_MODE_TLB) &&
322 bond->params.arp_interval) {
323 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
324 bond->dev->name, bond_mode_tbl[new_value].modename);
329 bond->params.mode = new_value;
330 bond_set_mode_ops(bond, bond->params.mode);
331 pr_info("%s: setting mode to %s (%d).\n",
332 bond->dev->name, bond_mode_tbl[new_value].modename,
337 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
338 bonding_show_mode, bonding_store_mode);
341 * Show and set the bonding transmit hash method.
342 * The bond interface must be down to change the xmit hash policy.
344 static ssize_t bonding_show_xmit_hash(struct device *d,
345 struct device_attribute *attr,
348 struct bonding *bond = to_bond(d);
350 return sprintf(buf, "%s %d\n",
351 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
352 bond->params.xmit_policy);
355 static ssize_t bonding_store_xmit_hash(struct device *d,
356 struct device_attribute *attr,
357 const char *buf, size_t count)
359 int new_value, ret = count;
360 struct bonding *bond = to_bond(d);
362 if (bond->dev->flags & IFF_UP) {
363 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
369 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
371 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
373 (int)strlen(buf) - 1, buf);
377 bond->params.xmit_policy = new_value;
378 bond_set_mode_ops(bond, bond->params.mode);
379 pr_info("%s: setting xmit hash policy to %s (%d).\n",
381 xmit_hashtype_tbl[new_value].modename, new_value);
386 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
387 bonding_show_xmit_hash, bonding_store_xmit_hash);
390 * Show and set arp_validate.
392 static ssize_t bonding_show_arp_validate(struct device *d,
393 struct device_attribute *attr,
396 struct bonding *bond = to_bond(d);
398 return sprintf(buf, "%s %d\n",
399 arp_validate_tbl[bond->params.arp_validate].modename,
400 bond->params.arp_validate);
403 static ssize_t bonding_store_arp_validate(struct device *d,
404 struct device_attribute *attr,
405 const char *buf, size_t count)
408 struct bonding *bond = to_bond(d);
410 new_value = bond_parse_parm(buf, arp_validate_tbl);
412 pr_err("%s: Ignoring invalid arp_validate value %s\n",
413 bond->dev->name, buf);
416 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
417 pr_err("%s: arp_validate only supported in active-backup mode.\n",
421 pr_info("%s: setting arp_validate to %s (%d).\n",
422 bond->dev->name, arp_validate_tbl[new_value].modename,
425 bond->params.arp_validate = new_value;
430 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
431 bonding_store_arp_validate);
434 * Show and store fail_over_mac. User only allowed to change the
435 * value when there are no slaves.
437 static ssize_t bonding_show_fail_over_mac(struct device *d,
438 struct device_attribute *attr,
441 struct bonding *bond = to_bond(d);
443 return sprintf(buf, "%s %d\n",
444 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
445 bond->params.fail_over_mac);
448 static ssize_t bonding_store_fail_over_mac(struct device *d,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
453 struct bonding *bond = to_bond(d);
455 if (bond->slave_cnt != 0) {
456 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
461 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
463 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
464 bond->dev->name, buf);
468 bond->params.fail_over_mac = new_value;
469 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
470 bond->dev->name, fail_over_mac_tbl[new_value].modename,
476 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
477 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
480 * Show and set the arp timer interval. There are two tricky bits
481 * here. First, if ARP monitoring is activated, then we must disable
482 * MII monitoring. Second, if the ARP timer isn't running, we must
485 static ssize_t bonding_show_arp_interval(struct device *d,
486 struct device_attribute *attr,
489 struct bonding *bond = to_bond(d);
491 return sprintf(buf, "%d\n", bond->params.arp_interval);
494 static ssize_t bonding_store_arp_interval(struct device *d,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
498 int new_value, ret = count;
499 struct bonding *bond = to_bond(d);
501 if (sscanf(buf, "%d", &new_value) != 1) {
502 pr_err("%s: no arp_interval value specified.\n",
508 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
509 bond->dev->name, new_value, INT_MAX);
513 if (bond->params.mode == BOND_MODE_ALB ||
514 bond->params.mode == BOND_MODE_TLB) {
515 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
516 bond->dev->name, bond->dev->name);
520 pr_info("%s: Setting ARP monitoring interval to %d.\n",
521 bond->dev->name, new_value);
522 bond->params.arp_interval = new_value;
523 if (bond->params.miimon) {
524 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
525 bond->dev->name, bond->dev->name);
526 bond->params.miimon = 0;
527 if (delayed_work_pending(&bond->mii_work)) {
528 cancel_delayed_work(&bond->mii_work);
529 flush_workqueue(bond->wq);
532 if (!bond->params.arp_targets[0]) {
533 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
536 if (bond->dev->flags & IFF_UP) {
537 /* If the interface is up, we may need to fire off
538 * the ARP timer. If the interface is down, the
539 * timer will get fired off when the open function
542 if (!delayed_work_pending(&bond->arp_work)) {
543 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
544 INIT_DELAYED_WORK(&bond->arp_work,
545 bond_activebackup_arp_mon);
547 INIT_DELAYED_WORK(&bond->arp_work,
548 bond_loadbalance_arp_mon);
550 queue_delayed_work(bond->wq, &bond->arp_work, 0);
557 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
558 bonding_show_arp_interval, bonding_store_arp_interval);
561 * Show and set the arp targets.
563 static ssize_t bonding_show_arp_targets(struct device *d,
564 struct device_attribute *attr,
568 struct bonding *bond = to_bond(d);
570 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
571 if (bond->params.arp_targets[i])
572 res += sprintf(buf + res, "%pI4 ",
573 &bond->params.arp_targets[i]);
576 buf[res-1] = '\n'; /* eat the leftover space */
580 static ssize_t bonding_store_arp_targets(struct device *d,
581 struct device_attribute *attr,
582 const char *buf, size_t count)
585 int i = 0, done = 0, ret = count;
586 struct bonding *bond = to_bond(d);
589 targets = bond->params.arp_targets;
590 newtarget = in_aton(buf + 1);
593 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
594 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
595 bond->dev->name, &newtarget);
599 /* look for an empty slot to put the target in, and check for dupes */
600 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
601 if (targets[i] == newtarget) { /* duplicate */
602 pr_err("%s: ARP target %pI4 is already present\n",
603 bond->dev->name, &newtarget);
607 if (targets[i] == 0) {
608 pr_info("%s: adding ARP target %pI4.\n",
609 bond->dev->name, &newtarget);
611 targets[i] = newtarget;
615 pr_err("%s: ARP target table is full!\n",
621 } else if (buf[0] == '-') {
622 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
623 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
624 bond->dev->name, &newtarget);
629 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
630 if (targets[i] == newtarget) {
632 pr_info("%s: removing ARP target %pI4.\n",
633 bond->dev->name, &newtarget);
634 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
635 targets[j] = targets[j+1];
642 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
643 bond->dev->name, &newtarget);
648 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
657 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
660 * Show and set the up and down delays. These must be multiples of the
661 * MII monitoring value, and are stored internally as the multiplier.
662 * Thus, we must translate to MS for the real world.
664 static ssize_t bonding_show_downdelay(struct device *d,
665 struct device_attribute *attr,
668 struct bonding *bond = to_bond(d);
670 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
673 static ssize_t bonding_store_downdelay(struct device *d,
674 struct device_attribute *attr,
675 const char *buf, size_t count)
677 int new_value, ret = count;
678 struct bonding *bond = to_bond(d);
680 if (!(bond->params.miimon)) {
681 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
687 if (sscanf(buf, "%d", &new_value) != 1) {
688 pr_err("%s: no down delay value specified.\n", bond->dev->name);
693 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
694 bond->dev->name, new_value, 1, INT_MAX);
698 if ((new_value % bond->params.miimon) != 0) {
699 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
700 bond->dev->name, new_value,
702 (new_value / bond->params.miimon) *
703 bond->params.miimon);
705 bond->params.downdelay = new_value / bond->params.miimon;
706 pr_info("%s: Setting down delay to %d.\n",
708 bond->params.downdelay * bond->params.miimon);
715 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
716 bonding_show_downdelay, bonding_store_downdelay);
718 static ssize_t bonding_show_updelay(struct device *d,
719 struct device_attribute *attr,
722 struct bonding *bond = to_bond(d);
724 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
728 static ssize_t bonding_store_updelay(struct device *d,
729 struct device_attribute *attr,
730 const char *buf, size_t count)
732 int new_value, ret = count;
733 struct bonding *bond = to_bond(d);
735 if (!(bond->params.miimon)) {
736 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
742 if (sscanf(buf, "%d", &new_value) != 1) {
743 pr_err("%s: no up delay value specified.\n",
749 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
750 bond->dev->name, new_value, 1, INT_MAX);
754 if ((new_value % bond->params.miimon) != 0) {
755 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
756 bond->dev->name, new_value,
758 (new_value / bond->params.miimon) *
759 bond->params.miimon);
761 bond->params.updelay = new_value / bond->params.miimon;
762 pr_info("%s: Setting up delay to %d.\n",
764 bond->params.updelay * bond->params.miimon);
770 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
771 bonding_show_updelay, bonding_store_updelay);
774 * Show and set the LACP interval. Interface must be down, and the mode
775 * must be set to 802.3ad mode.
777 static ssize_t bonding_show_lacp(struct device *d,
778 struct device_attribute *attr,
781 struct bonding *bond = to_bond(d);
783 return sprintf(buf, "%s %d\n",
784 bond_lacp_tbl[bond->params.lacp_fast].modename,
785 bond->params.lacp_fast);
788 static ssize_t bonding_store_lacp(struct device *d,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
792 int new_value, ret = count;
793 struct bonding *bond = to_bond(d);
795 if (bond->dev->flags & IFF_UP) {
796 pr_err("%s: Unable to update LACP rate because interface is up.\n",
802 if (bond->params.mode != BOND_MODE_8023AD) {
803 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
809 new_value = bond_parse_parm(buf, bond_lacp_tbl);
811 if ((new_value == 1) || (new_value == 0)) {
812 bond->params.lacp_fast = new_value;
813 pr_info("%s: Setting LACP rate to %s (%d).\n",
814 bond->dev->name, bond_lacp_tbl[new_value].modename,
817 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
818 bond->dev->name, (int)strlen(buf) - 1, buf);
824 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
825 bonding_show_lacp, bonding_store_lacp);
827 static ssize_t bonding_show_ad_select(struct device *d,
828 struct device_attribute *attr,
831 struct bonding *bond = to_bond(d);
833 return sprintf(buf, "%s %d\n",
834 ad_select_tbl[bond->params.ad_select].modename,
835 bond->params.ad_select);
839 static ssize_t bonding_store_ad_select(struct device *d,
840 struct device_attribute *attr,
841 const char *buf, size_t count)
843 int new_value, ret = count;
844 struct bonding *bond = to_bond(d);
846 if (bond->dev->flags & IFF_UP) {
847 pr_err("%s: Unable to update ad_select because interface is up.\n",
853 new_value = bond_parse_parm(buf, ad_select_tbl);
855 if (new_value != -1) {
856 bond->params.ad_select = new_value;
857 pr_info("%s: Setting ad_select to %s (%d).\n",
858 bond->dev->name, ad_select_tbl[new_value].modename,
861 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
862 bond->dev->name, (int)strlen(buf) - 1, buf);
868 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
869 bonding_show_ad_select, bonding_store_ad_select);
872 * Show and set the MII monitor interval. There are two tricky bits
873 * here. First, if MII monitoring is activated, then we must disable
874 * ARP monitoring. Second, if the timer isn't running, we must
877 static ssize_t bonding_show_miimon(struct device *d,
878 struct device_attribute *attr,
881 struct bonding *bond = to_bond(d);
883 return sprintf(buf, "%d\n", bond->params.miimon);
886 static ssize_t bonding_store_miimon(struct device *d,
887 struct device_attribute *attr,
888 const char *buf, size_t count)
890 int new_value, ret = count;
891 struct bonding *bond = to_bond(d);
893 if (sscanf(buf, "%d", &new_value) != 1) {
894 pr_err("%s: no miimon value specified.\n",
900 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
901 bond->dev->name, new_value, 1, INT_MAX);
905 pr_info("%s: Setting MII monitoring interval to %d.\n",
906 bond->dev->name, new_value);
907 bond->params.miimon = new_value;
908 if (bond->params.updelay)
909 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
911 bond->params.updelay * bond->params.miimon);
912 if (bond->params.downdelay)
913 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
915 bond->params.downdelay * bond->params.miimon);
916 if (bond->params.arp_interval) {
917 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
919 bond->params.arp_interval = 0;
920 if (bond->params.arp_validate) {
921 bond->params.arp_validate =
922 BOND_ARP_VALIDATE_NONE;
924 if (delayed_work_pending(&bond->arp_work)) {
925 cancel_delayed_work(&bond->arp_work);
926 flush_workqueue(bond->wq);
930 if (bond->dev->flags & IFF_UP) {
931 /* If the interface is up, we may need to fire off
932 * the MII timer. If the interface is down, the
933 * timer will get fired off when the open function
936 if (!delayed_work_pending(&bond->mii_work)) {
937 INIT_DELAYED_WORK(&bond->mii_work,
939 queue_delayed_work(bond->wq,
947 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
948 bonding_show_miimon, bonding_store_miimon);
951 * Show and set the primary slave. The store function is much
952 * simpler than bonding_store_slaves function because it only needs to
953 * handle one interface name.
954 * The bond must be a mode that supports a primary for this be
957 static ssize_t bonding_show_primary(struct device *d,
958 struct device_attribute *attr,
962 struct bonding *bond = to_bond(d);
964 if (bond->primary_slave)
965 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
970 static ssize_t bonding_store_primary(struct device *d,
971 struct device_attribute *attr,
972 const char *buf, size_t count)
976 struct bonding *bond = to_bond(d);
979 return restart_syscall();
981 read_lock(&bond->lock);
982 write_lock_bh(&bond->curr_slave_lock);
984 if (!USES_PRIMARY(bond->params.mode)) {
985 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
986 bond->dev->name, bond->dev->name, bond->params.mode);
988 bond_for_each_slave(bond, slave, i) {
990 (slave->dev->name, buf,
991 strlen(slave->dev->name)) == 0) {
992 pr_info("%s: Setting %s as primary slave.\n",
993 bond->dev->name, slave->dev->name);
994 bond->primary_slave = slave;
995 strcpy(bond->params.primary, slave->dev->name);
996 bond_select_active_slave(bond);
1001 /* if we got here, then we didn't match the name of any slave */
1003 if (strlen(buf) == 0 || buf[0] == '\n') {
1004 pr_info("%s: Setting primary slave to None.\n",
1006 bond->primary_slave = NULL;
1007 bond_select_active_slave(bond);
1009 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1010 bond->dev->name, (int)strlen(buf) - 1, buf);
1014 write_unlock_bh(&bond->curr_slave_lock);
1015 read_unlock(&bond->lock);
1016 unblock_netpoll_tx();
1021 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1022 bonding_show_primary, bonding_store_primary);
1025 * Show and set the primary_reselect flag.
1027 static ssize_t bonding_show_primary_reselect(struct device *d,
1028 struct device_attribute *attr,
1031 struct bonding *bond = to_bond(d);
1033 return sprintf(buf, "%s %d\n",
1034 pri_reselect_tbl[bond->params.primary_reselect].modename,
1035 bond->params.primary_reselect);
1038 static ssize_t bonding_store_primary_reselect(struct device *d,
1039 struct device_attribute *attr,
1040 const char *buf, size_t count)
1042 int new_value, ret = count;
1043 struct bonding *bond = to_bond(d);
1045 if (!rtnl_trylock())
1046 return restart_syscall();
1048 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1049 if (new_value < 0) {
1050 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1052 (int) strlen(buf) - 1, buf);
1057 bond->params.primary_reselect = new_value;
1058 pr_info("%s: setting primary_reselect to %s (%d).\n",
1059 bond->dev->name, pri_reselect_tbl[new_value].modename,
1063 read_lock(&bond->lock);
1064 write_lock_bh(&bond->curr_slave_lock);
1065 bond_select_active_slave(bond);
1066 write_unlock_bh(&bond->curr_slave_lock);
1067 read_unlock(&bond->lock);
1068 unblock_netpoll_tx();
1073 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1074 bonding_show_primary_reselect,
1075 bonding_store_primary_reselect);
1078 * Show and set the use_carrier flag.
1080 static ssize_t bonding_show_carrier(struct device *d,
1081 struct device_attribute *attr,
1084 struct bonding *bond = to_bond(d);
1086 return sprintf(buf, "%d\n", bond->params.use_carrier);
1089 static ssize_t bonding_store_carrier(struct device *d,
1090 struct device_attribute *attr,
1091 const char *buf, size_t count)
1093 int new_value, ret = count;
1094 struct bonding *bond = to_bond(d);
1097 if (sscanf(buf, "%d", &new_value) != 1) {
1098 pr_err("%s: no use_carrier value specified.\n",
1103 if ((new_value == 0) || (new_value == 1)) {
1104 bond->params.use_carrier = new_value;
1105 pr_info("%s: Setting use_carrier to %d.\n",
1106 bond->dev->name, new_value);
1108 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1109 bond->dev->name, new_value);
1114 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1115 bonding_show_carrier, bonding_store_carrier);
1119 * Show and set currently active_slave.
1121 static ssize_t bonding_show_active_slave(struct device *d,
1122 struct device_attribute *attr,
1126 struct bonding *bond = to_bond(d);
1129 read_lock(&bond->curr_slave_lock);
1130 curr = bond->curr_active_slave;
1131 read_unlock(&bond->curr_slave_lock);
1133 if (USES_PRIMARY(bond->params.mode) && curr)
1134 count = sprintf(buf, "%s\n", curr->dev->name);
1138 static ssize_t bonding_store_active_slave(struct device *d,
1139 struct device_attribute *attr,
1140 const char *buf, size_t count)
1143 struct slave *slave;
1144 struct slave *old_active = NULL;
1145 struct slave *new_active = NULL;
1146 struct bonding *bond = to_bond(d);
1148 if (!rtnl_trylock())
1149 return restart_syscall();
1152 read_lock(&bond->lock);
1153 write_lock_bh(&bond->curr_slave_lock);
1155 if (!USES_PRIMARY(bond->params.mode))
1156 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1157 bond->dev->name, bond->dev->name, bond->params.mode);
1159 bond_for_each_slave(bond, slave, i) {
1161 (slave->dev->name, buf,
1162 strlen(slave->dev->name)) == 0) {
1163 old_active = bond->curr_active_slave;
1165 if (new_active == old_active) {
1167 pr_info("%s: %s is already the current active slave.\n",
1175 (new_active->link == BOND_LINK_UP) &&
1176 IS_UP(new_active->dev)) {
1177 pr_info("%s: Setting %s as active slave.\n",
1180 bond_change_active_slave(bond, new_active);
1183 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1193 /* if we got here, then we didn't match the name of any slave */
1195 if (strlen(buf) == 0 || buf[0] == '\n') {
1196 pr_info("%s: Setting active slave to None.\n",
1198 bond->primary_slave = NULL;
1199 bond_select_active_slave(bond);
1201 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
1202 bond->dev->name, (int)strlen(buf) - 1, buf);
1206 write_unlock_bh(&bond->curr_slave_lock);
1207 read_unlock(&bond->lock);
1208 unblock_netpoll_tx();
1215 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1216 bonding_show_active_slave, bonding_store_active_slave);
1220 * Show link status of the bond interface.
1222 static ssize_t bonding_show_mii_status(struct device *d,
1223 struct device_attribute *attr,
1227 struct bonding *bond = to_bond(d);
1229 read_lock(&bond->curr_slave_lock);
1230 curr = bond->curr_active_slave;
1231 read_unlock(&bond->curr_slave_lock);
1233 return sprintf(buf, "%s\n", curr ? "up" : "down");
1235 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1239 * Show current 802.3ad aggregator ID.
1241 static ssize_t bonding_show_ad_aggregator(struct device *d,
1242 struct device_attribute *attr,
1246 struct bonding *bond = to_bond(d);
1248 if (bond->params.mode == BOND_MODE_8023AD) {
1249 struct ad_info ad_info;
1250 count = sprintf(buf, "%d\n",
1251 (bond_3ad_get_active_agg_info(bond, &ad_info))
1252 ? 0 : ad_info.aggregator_id);
1257 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1261 * Show number of active 802.3ad ports.
1263 static ssize_t bonding_show_ad_num_ports(struct device *d,
1264 struct device_attribute *attr,
1268 struct bonding *bond = to_bond(d);
1270 if (bond->params.mode == BOND_MODE_8023AD) {
1271 struct ad_info ad_info;
1272 count = sprintf(buf, "%d\n",
1273 (bond_3ad_get_active_agg_info(bond, &ad_info))
1274 ? 0 : ad_info.ports);
1279 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1283 * Show current 802.3ad actor key.
1285 static ssize_t bonding_show_ad_actor_key(struct device *d,
1286 struct device_attribute *attr,
1290 struct bonding *bond = to_bond(d);
1292 if (bond->params.mode == BOND_MODE_8023AD) {
1293 struct ad_info ad_info;
1294 count = sprintf(buf, "%d\n",
1295 (bond_3ad_get_active_agg_info(bond, &ad_info))
1296 ? 0 : ad_info.actor_key);
1301 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1305 * Show current 802.3ad partner key.
1307 static ssize_t bonding_show_ad_partner_key(struct device *d,
1308 struct device_attribute *attr,
1312 struct bonding *bond = to_bond(d);
1314 if (bond->params.mode == BOND_MODE_8023AD) {
1315 struct ad_info ad_info;
1316 count = sprintf(buf, "%d\n",
1317 (bond_3ad_get_active_agg_info(bond, &ad_info))
1318 ? 0 : ad_info.partner_key);
1323 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1327 * Show current 802.3ad partner mac.
1329 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1330 struct device_attribute *attr,
1334 struct bonding *bond = to_bond(d);
1336 if (bond->params.mode == BOND_MODE_8023AD) {
1337 struct ad_info ad_info;
1338 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1339 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1344 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1347 * Show the queue_ids of the slaves in the current bond.
1349 static ssize_t bonding_show_queue_id(struct device *d,
1350 struct device_attribute *attr,
1353 struct slave *slave;
1355 struct bonding *bond = to_bond(d);
1357 if (!rtnl_trylock())
1358 return restart_syscall();
1360 read_lock(&bond->lock);
1361 bond_for_each_slave(bond, slave, i) {
1362 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1363 /* not enough space for another interface_name:queue_id pair */
1364 if ((PAGE_SIZE - res) > 10)
1365 res = PAGE_SIZE - 10;
1366 res += sprintf(buf + res, "++more++ ");
1369 res += sprintf(buf + res, "%s:%d ",
1370 slave->dev->name, slave->queue_id);
1372 read_unlock(&bond->lock);
1374 buf[res-1] = '\n'; /* eat the leftover space */
1380 * Set the queue_ids of the slaves in the current bond. The bond
1381 * interface must be enslaved for this to work.
1383 static ssize_t bonding_store_queue_id(struct device *d,
1384 struct device_attribute *attr,
1385 const char *buffer, size_t count)
1387 struct slave *slave, *update_slave;
1388 struct bonding *bond = to_bond(d);
1392 struct net_device *sdev = NULL;
1394 if (!rtnl_trylock())
1395 return restart_syscall();
1397 /* delim will point to queue id if successful */
1398 delim = strchr(buffer, ':');
1403 * Terminate string that points to device name and bump it
1404 * up one, so we can read the queue id there.
1407 if (sscanf(++delim, "%hd\n", &qid) != 1)
1410 /* Check buffer length, valid ifname and queue id */
1411 if (strlen(buffer) > IFNAMSIZ ||
1412 !dev_valid_name(buffer) ||
1413 qid > bond->params.tx_queues)
1416 /* Get the pointer to that interface if it exists */
1417 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1421 read_lock(&bond->lock);
1423 /* Search for thes slave and check for duplicate qids */
1424 update_slave = NULL;
1425 bond_for_each_slave(bond, slave, i) {
1426 if (sdev == slave->dev)
1428 * We don't need to check the matching
1429 * slave for dups, since we're overwriting it
1431 update_slave = slave;
1432 else if (qid && qid == slave->queue_id) {
1433 goto err_no_cmd_unlock;
1438 goto err_no_cmd_unlock;
1440 /* Actually set the qids for the slave */
1441 update_slave->queue_id = qid;
1443 read_unlock(&bond->lock);
1449 read_unlock(&bond->lock);
1451 pr_info("invalid input for queue_id set for %s.\n",
1457 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1458 bonding_store_queue_id);
1462 * Show and set the all_slaves_active flag.
1464 static ssize_t bonding_show_slaves_active(struct device *d,
1465 struct device_attribute *attr,
1468 struct bonding *bond = to_bond(d);
1470 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1473 static ssize_t bonding_store_slaves_active(struct device *d,
1474 struct device_attribute *attr,
1475 const char *buf, size_t count)
1477 int i, new_value, ret = count;
1478 struct bonding *bond = to_bond(d);
1479 struct slave *slave;
1481 if (sscanf(buf, "%d", &new_value) != 1) {
1482 pr_err("%s: no all_slaves_active value specified.\n",
1488 if (new_value == bond->params.all_slaves_active)
1491 if ((new_value == 0) || (new_value == 1)) {
1492 bond->params.all_slaves_active = new_value;
1494 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1495 bond->dev->name, new_value);
1500 bond_for_each_slave(bond, slave, i) {
1501 if (!bond_is_active_slave(slave)) {
1503 slave->inactive = 0;
1505 slave->inactive = 1;
1511 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1512 bonding_show_slaves_active, bonding_store_slaves_active);
1515 * Show and set the number of IGMP membership reports to send on link failure
1517 static ssize_t bonding_show_resend_igmp(struct device *d,
1518 struct device_attribute *attr,
1521 struct bonding *bond = to_bond(d);
1523 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1526 static ssize_t bonding_store_resend_igmp(struct device *d,
1527 struct device_attribute *attr,
1528 const char *buf, size_t count)
1530 int new_value, ret = count;
1531 struct bonding *bond = to_bond(d);
1533 if (sscanf(buf, "%d", &new_value) != 1) {
1534 pr_err("%s: no resend_igmp value specified.\n",
1540 if (new_value < 0) {
1541 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1542 bond->dev->name, new_value);
1547 pr_info("%s: Setting resend_igmp to %d.\n",
1548 bond->dev->name, new_value);
1549 bond->params.resend_igmp = new_value;
1554 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1555 bonding_show_resend_igmp, bonding_store_resend_igmp);
1557 static struct attribute *per_bond_attrs[] = {
1558 &dev_attr_slaves.attr,
1559 &dev_attr_mode.attr,
1560 &dev_attr_fail_over_mac.attr,
1561 &dev_attr_arp_validate.attr,
1562 &dev_attr_arp_interval.attr,
1563 &dev_attr_arp_ip_target.attr,
1564 &dev_attr_downdelay.attr,
1565 &dev_attr_updelay.attr,
1566 &dev_attr_lacp_rate.attr,
1567 &dev_attr_ad_select.attr,
1568 &dev_attr_xmit_hash_policy.attr,
1569 &dev_attr_miimon.attr,
1570 &dev_attr_primary.attr,
1571 &dev_attr_primary_reselect.attr,
1572 &dev_attr_use_carrier.attr,
1573 &dev_attr_active_slave.attr,
1574 &dev_attr_mii_status.attr,
1575 &dev_attr_ad_aggregator.attr,
1576 &dev_attr_ad_num_ports.attr,
1577 &dev_attr_ad_actor_key.attr,
1578 &dev_attr_ad_partner_key.attr,
1579 &dev_attr_ad_partner_mac.attr,
1580 &dev_attr_queue_id.attr,
1581 &dev_attr_all_slaves_active.attr,
1582 &dev_attr_resend_igmp.attr,
1586 static struct attribute_group bonding_group = {
1588 .attrs = per_bond_attrs,
1592 * Initialize sysfs. This sets up the bonding_masters file in
1595 int bond_create_sysfs(void)
1599 ret = netdev_class_create_file(&class_attr_bonding_masters);
1601 * Permit multiple loads of the module by ignoring failures to
1602 * create the bonding_masters sysfs file. Bonding devices
1603 * created by second or subsequent loads of the module will
1604 * not be listed in, or controllable by, bonding_masters, but
1605 * will have the usual "bonding" sysfs directory.
1607 * This is done to preserve backwards compatibility for
1608 * initscripts/sysconfig, which load bonding multiple times to
1609 * configure multiple bonding devices.
1611 if (ret == -EEXIST) {
1612 /* Is someone being kinky and naming a device bonding_master? */
1613 if (__dev_get_by_name(&init_net,
1614 class_attr_bonding_masters.attr.name))
1615 pr_err("network device named %s already exists in sysfs",
1616 class_attr_bonding_masters.attr.name);
1625 * Remove /sys/class/net/bonding_masters.
1627 void bond_destroy_sysfs(void)
1629 netdev_class_remove_file(&class_attr_bonding_masters);
1633 * Initialize sysfs for each bond. This sets up and registers
1634 * the 'bondctl' directory for each individual bond under /sys/class/net.
1636 void bond_prepare_sysfs_group(struct bonding *bond)
1638 bond->dev->sysfs_groups[0] = &bonding_group;