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 if (!bond->params.arp_validate && new_value)
426 bond_register_arp(bond);
427 else if (bond->params.arp_validate && !new_value)
428 bond_unregister_arp(bond);
430 bond->params.arp_validate = new_value;
435 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
436 bonding_store_arp_validate);
439 * Show and store fail_over_mac. User only allowed to change the
440 * value when there are no slaves.
442 static ssize_t bonding_show_fail_over_mac(struct device *d,
443 struct device_attribute *attr,
446 struct bonding *bond = to_bond(d);
448 return sprintf(buf, "%s %d\n",
449 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
450 bond->params.fail_over_mac);
453 static ssize_t bonding_store_fail_over_mac(struct device *d,
454 struct device_attribute *attr,
455 const char *buf, size_t count)
458 struct bonding *bond = to_bond(d);
460 if (bond->slave_cnt != 0) {
461 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
466 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
468 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
469 bond->dev->name, buf);
473 bond->params.fail_over_mac = new_value;
474 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
475 bond->dev->name, fail_over_mac_tbl[new_value].modename,
481 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
482 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
485 * Show and set the arp timer interval. There are two tricky bits
486 * here. First, if ARP monitoring is activated, then we must disable
487 * MII monitoring. Second, if the ARP timer isn't running, we must
490 static ssize_t bonding_show_arp_interval(struct device *d,
491 struct device_attribute *attr,
494 struct bonding *bond = to_bond(d);
496 return sprintf(buf, "%d\n", bond->params.arp_interval);
499 static ssize_t bonding_store_arp_interval(struct device *d,
500 struct device_attribute *attr,
501 const char *buf, size_t count)
503 int new_value, ret = count;
504 struct bonding *bond = to_bond(d);
506 if (sscanf(buf, "%d", &new_value) != 1) {
507 pr_err("%s: no arp_interval value specified.\n",
513 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
514 bond->dev->name, new_value, INT_MAX);
518 if (bond->params.mode == BOND_MODE_ALB ||
519 bond->params.mode == BOND_MODE_TLB) {
520 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
521 bond->dev->name, bond->dev->name);
525 pr_info("%s: Setting ARP monitoring interval to %d.\n",
526 bond->dev->name, new_value);
527 bond->params.arp_interval = new_value;
528 if (bond->params.miimon) {
529 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
530 bond->dev->name, bond->dev->name);
531 bond->params.miimon = 0;
532 if (delayed_work_pending(&bond->mii_work)) {
533 cancel_delayed_work(&bond->mii_work);
534 flush_workqueue(bond->wq);
537 if (!bond->params.arp_targets[0]) {
538 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
541 if (bond->dev->flags & IFF_UP) {
542 /* If the interface is up, we may need to fire off
543 * the ARP timer. If the interface is down, the
544 * timer will get fired off when the open function
547 if (!delayed_work_pending(&bond->arp_work)) {
548 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
549 INIT_DELAYED_WORK(&bond->arp_work,
550 bond_activebackup_arp_mon);
552 INIT_DELAYED_WORK(&bond->arp_work,
553 bond_loadbalance_arp_mon);
555 queue_delayed_work(bond->wq, &bond->arp_work, 0);
562 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
563 bonding_show_arp_interval, bonding_store_arp_interval);
566 * Show and set the arp targets.
568 static ssize_t bonding_show_arp_targets(struct device *d,
569 struct device_attribute *attr,
573 struct bonding *bond = to_bond(d);
575 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
576 if (bond->params.arp_targets[i])
577 res += sprintf(buf + res, "%pI4 ",
578 &bond->params.arp_targets[i]);
581 buf[res-1] = '\n'; /* eat the leftover space */
585 static ssize_t bonding_store_arp_targets(struct device *d,
586 struct device_attribute *attr,
587 const char *buf, size_t count)
590 int i = 0, done = 0, ret = count;
591 struct bonding *bond = to_bond(d);
594 targets = bond->params.arp_targets;
595 newtarget = in_aton(buf + 1);
598 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
599 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
600 bond->dev->name, &newtarget);
604 /* look for an empty slot to put the target in, and check for dupes */
605 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
606 if (targets[i] == newtarget) { /* duplicate */
607 pr_err("%s: ARP target %pI4 is already present\n",
608 bond->dev->name, &newtarget);
612 if (targets[i] == 0) {
613 pr_info("%s: adding ARP target %pI4.\n",
614 bond->dev->name, &newtarget);
616 targets[i] = newtarget;
620 pr_err("%s: ARP target table is full!\n",
626 } else if (buf[0] == '-') {
627 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
628 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
629 bond->dev->name, &newtarget);
634 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
635 if (targets[i] == newtarget) {
637 pr_info("%s: removing ARP target %pI4.\n",
638 bond->dev->name, &newtarget);
639 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
640 targets[j] = targets[j+1];
647 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
648 bond->dev->name, &newtarget);
653 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
662 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
665 * Show and set the up and down delays. These must be multiples of the
666 * MII monitoring value, and are stored internally as the multiplier.
667 * Thus, we must translate to MS for the real world.
669 static ssize_t bonding_show_downdelay(struct device *d,
670 struct device_attribute *attr,
673 struct bonding *bond = to_bond(d);
675 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
678 static ssize_t bonding_store_downdelay(struct device *d,
679 struct device_attribute *attr,
680 const char *buf, size_t count)
682 int new_value, ret = count;
683 struct bonding *bond = to_bond(d);
685 if (!(bond->params.miimon)) {
686 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
692 if (sscanf(buf, "%d", &new_value) != 1) {
693 pr_err("%s: no down delay value specified.\n", bond->dev->name);
698 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
699 bond->dev->name, new_value, 1, INT_MAX);
703 if ((new_value % bond->params.miimon) != 0) {
704 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
705 bond->dev->name, new_value,
707 (new_value / bond->params.miimon) *
708 bond->params.miimon);
710 bond->params.downdelay = new_value / bond->params.miimon;
711 pr_info("%s: Setting down delay to %d.\n",
713 bond->params.downdelay * bond->params.miimon);
720 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
721 bonding_show_downdelay, bonding_store_downdelay);
723 static ssize_t bonding_show_updelay(struct device *d,
724 struct device_attribute *attr,
727 struct bonding *bond = to_bond(d);
729 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
733 static ssize_t bonding_store_updelay(struct device *d,
734 struct device_attribute *attr,
735 const char *buf, size_t count)
737 int new_value, ret = count;
738 struct bonding *bond = to_bond(d);
740 if (!(bond->params.miimon)) {
741 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
747 if (sscanf(buf, "%d", &new_value) != 1) {
748 pr_err("%s: no up delay value specified.\n",
754 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
755 bond->dev->name, new_value, 1, INT_MAX);
759 if ((new_value % bond->params.miimon) != 0) {
760 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
761 bond->dev->name, new_value,
763 (new_value / bond->params.miimon) *
764 bond->params.miimon);
766 bond->params.updelay = new_value / bond->params.miimon;
767 pr_info("%s: Setting up delay to %d.\n",
769 bond->params.updelay * bond->params.miimon);
775 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
776 bonding_show_updelay, bonding_store_updelay);
779 * Show and set the LACP interval. Interface must be down, and the mode
780 * must be set to 802.3ad mode.
782 static ssize_t bonding_show_lacp(struct device *d,
783 struct device_attribute *attr,
786 struct bonding *bond = to_bond(d);
788 return sprintf(buf, "%s %d\n",
789 bond_lacp_tbl[bond->params.lacp_fast].modename,
790 bond->params.lacp_fast);
793 static ssize_t bonding_store_lacp(struct device *d,
794 struct device_attribute *attr,
795 const char *buf, size_t count)
797 int new_value, ret = count;
798 struct bonding *bond = to_bond(d);
800 if (bond->dev->flags & IFF_UP) {
801 pr_err("%s: Unable to update LACP rate because interface is up.\n",
807 if (bond->params.mode != BOND_MODE_8023AD) {
808 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
814 new_value = bond_parse_parm(buf, bond_lacp_tbl);
816 if ((new_value == 1) || (new_value == 0)) {
817 bond->params.lacp_fast = new_value;
818 pr_info("%s: Setting LACP rate to %s (%d).\n",
819 bond->dev->name, bond_lacp_tbl[new_value].modename,
822 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
823 bond->dev->name, (int)strlen(buf) - 1, buf);
829 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
830 bonding_show_lacp, bonding_store_lacp);
832 static ssize_t bonding_show_ad_select(struct device *d,
833 struct device_attribute *attr,
836 struct bonding *bond = to_bond(d);
838 return sprintf(buf, "%s %d\n",
839 ad_select_tbl[bond->params.ad_select].modename,
840 bond->params.ad_select);
844 static ssize_t bonding_store_ad_select(struct device *d,
845 struct device_attribute *attr,
846 const char *buf, size_t count)
848 int new_value, ret = count;
849 struct bonding *bond = to_bond(d);
851 if (bond->dev->flags & IFF_UP) {
852 pr_err("%s: Unable to update ad_select because interface is up.\n",
858 new_value = bond_parse_parm(buf, ad_select_tbl);
860 if (new_value != -1) {
861 bond->params.ad_select = new_value;
862 pr_info("%s: Setting ad_select to %s (%d).\n",
863 bond->dev->name, ad_select_tbl[new_value].modename,
866 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
867 bond->dev->name, (int)strlen(buf) - 1, buf);
873 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
874 bonding_show_ad_select, bonding_store_ad_select);
877 * Show and set the number of grat ARP to send after a failover event.
879 static ssize_t bonding_show_n_grat_arp(struct device *d,
880 struct device_attribute *attr,
883 struct bonding *bond = to_bond(d);
885 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
888 static ssize_t bonding_store_n_grat_arp(struct device *d,
889 struct device_attribute *attr,
890 const char *buf, size_t count)
892 int new_value, ret = count;
893 struct bonding *bond = to_bond(d);
895 if (sscanf(buf, "%d", &new_value) != 1) {
896 pr_err("%s: no num_grat_arp value specified.\n",
901 if (new_value < 0 || new_value > 255) {
902 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
903 bond->dev->name, new_value);
907 bond->params.num_grat_arp = new_value;
912 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
913 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
916 * Show and set the number of unsolicited NA's to send after a failover event.
918 static ssize_t bonding_show_n_unsol_na(struct device *d,
919 struct device_attribute *attr,
922 struct bonding *bond = to_bond(d);
924 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
927 static ssize_t bonding_store_n_unsol_na(struct device *d,
928 struct device_attribute *attr,
929 const char *buf, size_t count)
931 int new_value, ret = count;
932 struct bonding *bond = to_bond(d);
934 if (sscanf(buf, "%d", &new_value) != 1) {
935 pr_err("%s: no num_unsol_na value specified.\n",
941 if (new_value < 0 || new_value > 255) {
942 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
943 bond->dev->name, new_value);
947 bond->params.num_unsol_na = new_value;
951 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
952 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
955 * Show and set the MII monitor interval. There are two tricky bits
956 * here. First, if MII monitoring is activated, then we must disable
957 * ARP monitoring. Second, if the timer isn't running, we must
960 static ssize_t bonding_show_miimon(struct device *d,
961 struct device_attribute *attr,
964 struct bonding *bond = to_bond(d);
966 return sprintf(buf, "%d\n", bond->params.miimon);
969 static ssize_t bonding_store_miimon(struct device *d,
970 struct device_attribute *attr,
971 const char *buf, size_t count)
973 int new_value, ret = count;
974 struct bonding *bond = to_bond(d);
976 if (sscanf(buf, "%d", &new_value) != 1) {
977 pr_err("%s: no miimon value specified.\n",
983 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
984 bond->dev->name, new_value, 1, INT_MAX);
988 pr_info("%s: Setting MII monitoring interval to %d.\n",
989 bond->dev->name, new_value);
990 bond->params.miimon = new_value;
991 if (bond->params.updelay)
992 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
994 bond->params.updelay * bond->params.miimon);
995 if (bond->params.downdelay)
996 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
998 bond->params.downdelay * bond->params.miimon);
999 if (bond->params.arp_interval) {
1000 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1002 bond->params.arp_interval = 0;
1003 if (bond->params.arp_validate) {
1004 bond_unregister_arp(bond);
1005 bond->params.arp_validate =
1006 BOND_ARP_VALIDATE_NONE;
1008 if (delayed_work_pending(&bond->arp_work)) {
1009 cancel_delayed_work(&bond->arp_work);
1010 flush_workqueue(bond->wq);
1014 if (bond->dev->flags & IFF_UP) {
1015 /* If the interface is up, we may need to fire off
1016 * the MII timer. If the interface is down, the
1017 * timer will get fired off when the open function
1020 if (!delayed_work_pending(&bond->mii_work)) {
1021 INIT_DELAYED_WORK(&bond->mii_work,
1023 queue_delayed_work(bond->wq,
1024 &bond->mii_work, 0);
1031 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1032 bonding_show_miimon, bonding_store_miimon);
1035 * Show and set the primary slave. The store function is much
1036 * simpler than bonding_store_slaves function because it only needs to
1037 * handle one interface name.
1038 * The bond must be a mode that supports a primary for this be
1041 static ssize_t bonding_show_primary(struct device *d,
1042 struct device_attribute *attr,
1046 struct bonding *bond = to_bond(d);
1048 if (bond->primary_slave)
1049 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1054 static ssize_t bonding_store_primary(struct device *d,
1055 struct device_attribute *attr,
1056 const char *buf, size_t count)
1059 struct slave *slave;
1060 struct bonding *bond = to_bond(d);
1062 if (!rtnl_trylock())
1063 return restart_syscall();
1065 read_lock(&bond->lock);
1066 write_lock_bh(&bond->curr_slave_lock);
1068 if (!USES_PRIMARY(bond->params.mode)) {
1069 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1070 bond->dev->name, bond->dev->name, bond->params.mode);
1072 bond_for_each_slave(bond, slave, i) {
1074 (slave->dev->name, buf,
1075 strlen(slave->dev->name)) == 0) {
1076 pr_info("%s: Setting %s as primary slave.\n",
1077 bond->dev->name, slave->dev->name);
1078 bond->primary_slave = slave;
1079 strcpy(bond->params.primary, slave->dev->name);
1080 bond_select_active_slave(bond);
1085 /* if we got here, then we didn't match the name of any slave */
1087 if (strlen(buf) == 0 || buf[0] == '\n') {
1088 pr_info("%s: Setting primary slave to None.\n",
1090 bond->primary_slave = NULL;
1091 bond_select_active_slave(bond);
1093 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1094 bond->dev->name, (int)strlen(buf) - 1, buf);
1098 write_unlock_bh(&bond->curr_slave_lock);
1099 read_unlock(&bond->lock);
1100 unblock_netpoll_tx();
1105 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1106 bonding_show_primary, bonding_store_primary);
1109 * Show and set the primary_reselect flag.
1111 static ssize_t bonding_show_primary_reselect(struct device *d,
1112 struct device_attribute *attr,
1115 struct bonding *bond = to_bond(d);
1117 return sprintf(buf, "%s %d\n",
1118 pri_reselect_tbl[bond->params.primary_reselect].modename,
1119 bond->params.primary_reselect);
1122 static ssize_t bonding_store_primary_reselect(struct device *d,
1123 struct device_attribute *attr,
1124 const char *buf, size_t count)
1126 int new_value, ret = count;
1127 struct bonding *bond = to_bond(d);
1129 if (!rtnl_trylock())
1130 return restart_syscall();
1132 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1133 if (new_value < 0) {
1134 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1136 (int) strlen(buf) - 1, buf);
1141 bond->params.primary_reselect = new_value;
1142 pr_info("%s: setting primary_reselect to %s (%d).\n",
1143 bond->dev->name, pri_reselect_tbl[new_value].modename,
1147 read_lock(&bond->lock);
1148 write_lock_bh(&bond->curr_slave_lock);
1149 bond_select_active_slave(bond);
1150 write_unlock_bh(&bond->curr_slave_lock);
1151 read_unlock(&bond->lock);
1152 unblock_netpoll_tx();
1157 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1158 bonding_show_primary_reselect,
1159 bonding_store_primary_reselect);
1162 * Show and set the use_carrier flag.
1164 static ssize_t bonding_show_carrier(struct device *d,
1165 struct device_attribute *attr,
1168 struct bonding *bond = to_bond(d);
1170 return sprintf(buf, "%d\n", bond->params.use_carrier);
1173 static ssize_t bonding_store_carrier(struct device *d,
1174 struct device_attribute *attr,
1175 const char *buf, size_t count)
1177 int new_value, ret = count;
1178 struct bonding *bond = to_bond(d);
1181 if (sscanf(buf, "%d", &new_value) != 1) {
1182 pr_err("%s: no use_carrier value specified.\n",
1187 if ((new_value == 0) || (new_value == 1)) {
1188 bond->params.use_carrier = new_value;
1189 pr_info("%s: Setting use_carrier to %d.\n",
1190 bond->dev->name, new_value);
1192 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1193 bond->dev->name, new_value);
1198 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1199 bonding_show_carrier, bonding_store_carrier);
1203 * Show and set currently active_slave.
1205 static ssize_t bonding_show_active_slave(struct device *d,
1206 struct device_attribute *attr,
1210 struct bonding *bond = to_bond(d);
1213 read_lock(&bond->curr_slave_lock);
1214 curr = bond->curr_active_slave;
1215 read_unlock(&bond->curr_slave_lock);
1217 if (USES_PRIMARY(bond->params.mode) && curr)
1218 count = sprintf(buf, "%s\n", curr->dev->name);
1222 static ssize_t bonding_store_active_slave(struct device *d,
1223 struct device_attribute *attr,
1224 const char *buf, size_t count)
1227 struct slave *slave;
1228 struct slave *old_active = NULL;
1229 struct slave *new_active = NULL;
1230 struct bonding *bond = to_bond(d);
1232 if (!rtnl_trylock())
1233 return restart_syscall();
1236 read_lock(&bond->lock);
1237 write_lock_bh(&bond->curr_slave_lock);
1239 if (!USES_PRIMARY(bond->params.mode))
1240 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1241 bond->dev->name, bond->dev->name, bond->params.mode);
1243 bond_for_each_slave(bond, slave, i) {
1245 (slave->dev->name, buf,
1246 strlen(slave->dev->name)) == 0) {
1247 old_active = bond->curr_active_slave;
1249 if (new_active == old_active) {
1251 pr_info("%s: %s is already the current active slave.\n",
1259 (new_active->link == BOND_LINK_UP) &&
1260 IS_UP(new_active->dev)) {
1261 pr_info("%s: Setting %s as active slave.\n",
1264 bond_change_active_slave(bond, new_active);
1267 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1277 /* if we got here, then we didn't match the name of any slave */
1279 if (strlen(buf) == 0 || buf[0] == '\n') {
1280 pr_info("%s: Setting active slave to None.\n",
1282 bond->primary_slave = NULL;
1283 bond_select_active_slave(bond);
1285 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
1286 bond->dev->name, (int)strlen(buf) - 1, buf);
1290 write_unlock_bh(&bond->curr_slave_lock);
1291 read_unlock(&bond->lock);
1292 unblock_netpoll_tx();
1299 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1300 bonding_show_active_slave, bonding_store_active_slave);
1304 * Show link status of the bond interface.
1306 static ssize_t bonding_show_mii_status(struct device *d,
1307 struct device_attribute *attr,
1311 struct bonding *bond = to_bond(d);
1313 read_lock(&bond->curr_slave_lock);
1314 curr = bond->curr_active_slave;
1315 read_unlock(&bond->curr_slave_lock);
1317 return sprintf(buf, "%s\n", curr ? "up" : "down");
1319 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1323 * Show current 802.3ad aggregator ID.
1325 static ssize_t bonding_show_ad_aggregator(struct device *d,
1326 struct device_attribute *attr,
1330 struct bonding *bond = to_bond(d);
1332 if (bond->params.mode == BOND_MODE_8023AD) {
1333 struct ad_info ad_info;
1334 count = sprintf(buf, "%d\n",
1335 (bond_3ad_get_active_agg_info(bond, &ad_info))
1336 ? 0 : ad_info.aggregator_id);
1341 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1345 * Show number of active 802.3ad ports.
1347 static ssize_t bonding_show_ad_num_ports(struct device *d,
1348 struct device_attribute *attr,
1352 struct bonding *bond = to_bond(d);
1354 if (bond->params.mode == BOND_MODE_8023AD) {
1355 struct ad_info ad_info;
1356 count = sprintf(buf, "%d\n",
1357 (bond_3ad_get_active_agg_info(bond, &ad_info))
1358 ? 0 : ad_info.ports);
1363 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1367 * Show current 802.3ad actor key.
1369 static ssize_t bonding_show_ad_actor_key(struct device *d,
1370 struct device_attribute *attr,
1374 struct bonding *bond = to_bond(d);
1376 if (bond->params.mode == BOND_MODE_8023AD) {
1377 struct ad_info ad_info;
1378 count = sprintf(buf, "%d\n",
1379 (bond_3ad_get_active_agg_info(bond, &ad_info))
1380 ? 0 : ad_info.actor_key);
1385 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1389 * Show current 802.3ad partner key.
1391 static ssize_t bonding_show_ad_partner_key(struct device *d,
1392 struct device_attribute *attr,
1396 struct bonding *bond = to_bond(d);
1398 if (bond->params.mode == BOND_MODE_8023AD) {
1399 struct ad_info ad_info;
1400 count = sprintf(buf, "%d\n",
1401 (bond_3ad_get_active_agg_info(bond, &ad_info))
1402 ? 0 : ad_info.partner_key);
1407 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1411 * Show current 802.3ad partner mac.
1413 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1414 struct device_attribute *attr,
1418 struct bonding *bond = to_bond(d);
1420 if (bond->params.mode == BOND_MODE_8023AD) {
1421 struct ad_info ad_info;
1422 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1423 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1428 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1431 * Show the queue_ids of the slaves in the current bond.
1433 static ssize_t bonding_show_queue_id(struct device *d,
1434 struct device_attribute *attr,
1437 struct slave *slave;
1439 struct bonding *bond = to_bond(d);
1441 if (!rtnl_trylock())
1442 return restart_syscall();
1444 read_lock(&bond->lock);
1445 bond_for_each_slave(bond, slave, i) {
1446 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1447 /* not enough space for another interface_name:queue_id pair */
1448 if ((PAGE_SIZE - res) > 10)
1449 res = PAGE_SIZE - 10;
1450 res += sprintf(buf + res, "++more++ ");
1453 res += sprintf(buf + res, "%s:%d ",
1454 slave->dev->name, slave->queue_id);
1456 read_unlock(&bond->lock);
1458 buf[res-1] = '\n'; /* eat the leftover space */
1464 * Set the queue_ids of the slaves in the current bond. The bond
1465 * interface must be enslaved for this to work.
1467 static ssize_t bonding_store_queue_id(struct device *d,
1468 struct device_attribute *attr,
1469 const char *buffer, size_t count)
1471 struct slave *slave, *update_slave;
1472 struct bonding *bond = to_bond(d);
1476 struct net_device *sdev = NULL;
1478 if (!rtnl_trylock())
1479 return restart_syscall();
1481 /* delim will point to queue id if successful */
1482 delim = strchr(buffer, ':');
1487 * Terminate string that points to device name and bump it
1488 * up one, so we can read the queue id there.
1491 if (sscanf(++delim, "%hd\n", &qid) != 1)
1494 /* Check buffer length, valid ifname and queue id */
1495 if (strlen(buffer) > IFNAMSIZ ||
1496 !dev_valid_name(buffer) ||
1497 qid > bond->params.tx_queues)
1500 /* Get the pointer to that interface if it exists */
1501 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1505 read_lock(&bond->lock);
1507 /* Search for thes slave and check for duplicate qids */
1508 update_slave = NULL;
1509 bond_for_each_slave(bond, slave, i) {
1510 if (sdev == slave->dev)
1512 * We don't need to check the matching
1513 * slave for dups, since we're overwriting it
1515 update_slave = slave;
1516 else if (qid && qid == slave->queue_id) {
1517 goto err_no_cmd_unlock;
1522 goto err_no_cmd_unlock;
1524 /* Actually set the qids for the slave */
1525 update_slave->queue_id = qid;
1527 read_unlock(&bond->lock);
1533 read_unlock(&bond->lock);
1535 pr_info("invalid input for queue_id set for %s.\n",
1541 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1542 bonding_store_queue_id);
1546 * Show and set the all_slaves_active flag.
1548 static ssize_t bonding_show_slaves_active(struct device *d,
1549 struct device_attribute *attr,
1552 struct bonding *bond = to_bond(d);
1554 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1557 static ssize_t bonding_store_slaves_active(struct device *d,
1558 struct device_attribute *attr,
1559 const char *buf, size_t count)
1561 int i, new_value, ret = count;
1562 struct bonding *bond = to_bond(d);
1563 struct slave *slave;
1565 if (sscanf(buf, "%d", &new_value) != 1) {
1566 pr_err("%s: no all_slaves_active value specified.\n",
1572 if (new_value == bond->params.all_slaves_active)
1575 if ((new_value == 0) || (new_value == 1)) {
1576 bond->params.all_slaves_active = new_value;
1578 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1579 bond->dev->name, new_value);
1584 bond_for_each_slave(bond, slave, i) {
1585 if (!bond_is_active_slave(slave)) {
1587 slave->dev->priv_flags &= ~IFF_SLAVE_INACTIVE;
1589 slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
1595 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1596 bonding_show_slaves_active, bonding_store_slaves_active);
1599 * Show and set the number of IGMP membership reports to send on link failure
1601 static ssize_t bonding_show_resend_igmp(struct device *d,
1602 struct device_attribute *attr,
1605 struct bonding *bond = to_bond(d);
1607 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1610 static ssize_t bonding_store_resend_igmp(struct device *d,
1611 struct device_attribute *attr,
1612 const char *buf, size_t count)
1614 int new_value, ret = count;
1615 struct bonding *bond = to_bond(d);
1617 if (sscanf(buf, "%d", &new_value) != 1) {
1618 pr_err("%s: no resend_igmp value specified.\n",
1624 if (new_value < 0) {
1625 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1626 bond->dev->name, new_value);
1631 pr_info("%s: Setting resend_igmp to %d.\n",
1632 bond->dev->name, new_value);
1633 bond->params.resend_igmp = new_value;
1638 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1639 bonding_show_resend_igmp, bonding_store_resend_igmp);
1641 static struct attribute *per_bond_attrs[] = {
1642 &dev_attr_slaves.attr,
1643 &dev_attr_mode.attr,
1644 &dev_attr_fail_over_mac.attr,
1645 &dev_attr_arp_validate.attr,
1646 &dev_attr_arp_interval.attr,
1647 &dev_attr_arp_ip_target.attr,
1648 &dev_attr_downdelay.attr,
1649 &dev_attr_updelay.attr,
1650 &dev_attr_lacp_rate.attr,
1651 &dev_attr_ad_select.attr,
1652 &dev_attr_xmit_hash_policy.attr,
1653 &dev_attr_num_grat_arp.attr,
1654 &dev_attr_num_unsol_na.attr,
1655 &dev_attr_miimon.attr,
1656 &dev_attr_primary.attr,
1657 &dev_attr_primary_reselect.attr,
1658 &dev_attr_use_carrier.attr,
1659 &dev_attr_active_slave.attr,
1660 &dev_attr_mii_status.attr,
1661 &dev_attr_ad_aggregator.attr,
1662 &dev_attr_ad_num_ports.attr,
1663 &dev_attr_ad_actor_key.attr,
1664 &dev_attr_ad_partner_key.attr,
1665 &dev_attr_ad_partner_mac.attr,
1666 &dev_attr_queue_id.attr,
1667 &dev_attr_all_slaves_active.attr,
1668 &dev_attr_resend_igmp.attr,
1672 static struct attribute_group bonding_group = {
1674 .attrs = per_bond_attrs,
1678 * Initialize sysfs. This sets up the bonding_masters file in
1681 int bond_create_sysfs(void)
1685 ret = netdev_class_create_file(&class_attr_bonding_masters);
1687 * Permit multiple loads of the module by ignoring failures to
1688 * create the bonding_masters sysfs file. Bonding devices
1689 * created by second or subsequent loads of the module will
1690 * not be listed in, or controllable by, bonding_masters, but
1691 * will have the usual "bonding" sysfs directory.
1693 * This is done to preserve backwards compatibility for
1694 * initscripts/sysconfig, which load bonding multiple times to
1695 * configure multiple bonding devices.
1697 if (ret == -EEXIST) {
1698 /* Is someone being kinky and naming a device bonding_master? */
1699 if (__dev_get_by_name(&init_net,
1700 class_attr_bonding_masters.attr.name))
1701 pr_err("network device named %s already exists in sysfs",
1702 class_attr_bonding_masters.attr.name);
1711 * Remove /sys/class/net/bonding_masters.
1713 void bond_destroy_sysfs(void)
1715 netdev_class_remove_file(&class_attr_bonding_masters);
1719 * Initialize sysfs for each bond. This sets up and registers
1720 * the 'bondctl' directory for each individual bond under /sys/class/net.
1722 void bond_prepare_sysfs_group(struct bonding *bond)
1724 bond->dev->sysfs_groups[0] = &bonding_group;