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);
231 return restart_syscall();
233 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
234 ifname = command + 1;
235 if ((strlen(command) <= 1) ||
236 !dev_valid_name(ifname))
239 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
241 pr_info("%s: Interface %s does not exist!\n",
242 bond->dev->name, ifname);
247 switch (command[0]) {
249 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
250 res = bond_enslave(bond->dev, dev);
254 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
255 res = bond_release(bond->dev, dev);
267 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
276 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
277 bonding_store_slaves);
280 * Show and set the bonding mode. The bond interface must be down to
283 static ssize_t bonding_show_mode(struct device *d,
284 struct device_attribute *attr, char *buf)
286 struct bonding *bond = to_bond(d);
288 return sprintf(buf, "%s %d\n",
289 bond_mode_tbl[bond->params.mode].modename,
293 static ssize_t bonding_store_mode(struct device *d,
294 struct device_attribute *attr,
295 const char *buf, size_t count)
297 int new_value, ret = count;
298 struct bonding *bond = to_bond(d);
300 if (bond->dev->flags & IFF_UP) {
301 pr_err("unable to update mode of %s because interface is up.\n",
307 new_value = bond_parse_parm(buf, bond_mode_tbl);
309 pr_err("%s: Ignoring invalid mode value %.*s.\n",
310 bond->dev->name, (int)strlen(buf) - 1, buf);
314 if ((new_value == BOND_MODE_ALB ||
315 new_value == BOND_MODE_TLB) &&
316 bond->params.arp_interval) {
317 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
318 bond->dev->name, bond_mode_tbl[new_value].modename);
323 bond->params.mode = new_value;
324 bond_set_mode_ops(bond, bond->params.mode);
325 pr_info("%s: setting mode to %s (%d).\n",
326 bond->dev->name, bond_mode_tbl[new_value].modename,
331 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
332 bonding_show_mode, bonding_store_mode);
335 * Show and set the bonding transmit hash method.
336 * The bond interface must be down to change the xmit hash policy.
338 static ssize_t bonding_show_xmit_hash(struct device *d,
339 struct device_attribute *attr,
342 struct bonding *bond = to_bond(d);
344 return sprintf(buf, "%s %d\n",
345 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
346 bond->params.xmit_policy);
349 static ssize_t bonding_store_xmit_hash(struct device *d,
350 struct device_attribute *attr,
351 const char *buf, size_t count)
353 int new_value, ret = count;
354 struct bonding *bond = to_bond(d);
356 if (bond->dev->flags & IFF_UP) {
357 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
363 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
365 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
367 (int)strlen(buf) - 1, buf);
371 bond->params.xmit_policy = new_value;
372 bond_set_mode_ops(bond, bond->params.mode);
373 pr_info("%s: setting xmit hash policy to %s (%d).\n",
375 xmit_hashtype_tbl[new_value].modename, new_value);
380 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
381 bonding_show_xmit_hash, bonding_store_xmit_hash);
384 * Show and set arp_validate.
386 static ssize_t bonding_show_arp_validate(struct device *d,
387 struct device_attribute *attr,
390 struct bonding *bond = to_bond(d);
392 return sprintf(buf, "%s %d\n",
393 arp_validate_tbl[bond->params.arp_validate].modename,
394 bond->params.arp_validate);
397 static ssize_t bonding_store_arp_validate(struct device *d,
398 struct device_attribute *attr,
399 const char *buf, size_t count)
402 struct bonding *bond = to_bond(d);
404 new_value = bond_parse_parm(buf, arp_validate_tbl);
406 pr_err("%s: Ignoring invalid arp_validate value %s\n",
407 bond->dev->name, buf);
410 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
411 pr_err("%s: arp_validate only supported in active-backup mode.\n",
415 pr_info("%s: setting arp_validate to %s (%d).\n",
416 bond->dev->name, arp_validate_tbl[new_value].modename,
419 bond->params.arp_validate = new_value;
424 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
425 bonding_store_arp_validate);
428 * Show and store fail_over_mac. User only allowed to change the
429 * value when there are no slaves.
431 static ssize_t bonding_show_fail_over_mac(struct device *d,
432 struct device_attribute *attr,
435 struct bonding *bond = to_bond(d);
437 return sprintf(buf, "%s %d\n",
438 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
439 bond->params.fail_over_mac);
442 static ssize_t bonding_store_fail_over_mac(struct device *d,
443 struct device_attribute *attr,
444 const char *buf, size_t count)
447 struct bonding *bond = to_bond(d);
449 if (bond->slave_cnt != 0) {
450 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
455 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
457 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
458 bond->dev->name, buf);
462 bond->params.fail_over_mac = new_value;
463 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
464 bond->dev->name, fail_over_mac_tbl[new_value].modename,
470 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
471 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
474 * Show and set the arp timer interval. There are two tricky bits
475 * here. First, if ARP monitoring is activated, then we must disable
476 * MII monitoring. Second, if the ARP timer isn't running, we must
479 static ssize_t bonding_show_arp_interval(struct device *d,
480 struct device_attribute *attr,
483 struct bonding *bond = to_bond(d);
485 return sprintf(buf, "%d\n", bond->params.arp_interval);
488 static ssize_t bonding_store_arp_interval(struct device *d,
489 struct device_attribute *attr,
490 const char *buf, size_t count)
492 int new_value, ret = count;
493 struct bonding *bond = to_bond(d);
495 if (sscanf(buf, "%d", &new_value) != 1) {
496 pr_err("%s: no arp_interval value specified.\n",
502 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
503 bond->dev->name, new_value, INT_MAX);
507 if (bond->params.mode == BOND_MODE_ALB ||
508 bond->params.mode == BOND_MODE_TLB) {
509 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
510 bond->dev->name, bond->dev->name);
514 pr_info("%s: Setting ARP monitoring interval to %d.\n",
515 bond->dev->name, new_value);
516 bond->params.arp_interval = new_value;
517 if (bond->params.miimon) {
518 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
519 bond->dev->name, bond->dev->name);
520 bond->params.miimon = 0;
521 if (delayed_work_pending(&bond->mii_work)) {
522 cancel_delayed_work(&bond->mii_work);
523 flush_workqueue(bond->wq);
526 if (!bond->params.arp_targets[0]) {
527 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
530 if (bond->dev->flags & IFF_UP) {
531 /* If the interface is up, we may need to fire off
532 * the ARP timer. If the interface is down, the
533 * timer will get fired off when the open function
536 if (!delayed_work_pending(&bond->arp_work)) {
537 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
538 INIT_DELAYED_WORK(&bond->arp_work,
539 bond_activebackup_arp_mon);
541 INIT_DELAYED_WORK(&bond->arp_work,
542 bond_loadbalance_arp_mon);
544 queue_delayed_work(bond->wq, &bond->arp_work, 0);
551 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
552 bonding_show_arp_interval, bonding_store_arp_interval);
555 * Show and set the arp targets.
557 static ssize_t bonding_show_arp_targets(struct device *d,
558 struct device_attribute *attr,
562 struct bonding *bond = to_bond(d);
564 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
565 if (bond->params.arp_targets[i])
566 res += sprintf(buf + res, "%pI4 ",
567 &bond->params.arp_targets[i]);
570 buf[res-1] = '\n'; /* eat the leftover space */
574 static ssize_t bonding_store_arp_targets(struct device *d,
575 struct device_attribute *attr,
576 const char *buf, size_t count)
579 int i = 0, done = 0, ret = count;
580 struct bonding *bond = to_bond(d);
583 targets = bond->params.arp_targets;
584 newtarget = in_aton(buf + 1);
587 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
588 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
589 bond->dev->name, &newtarget);
593 /* look for an empty slot to put the target in, and check for dupes */
594 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
595 if (targets[i] == newtarget) { /* duplicate */
596 pr_err("%s: ARP target %pI4 is already present\n",
597 bond->dev->name, &newtarget);
601 if (targets[i] == 0) {
602 pr_info("%s: adding ARP target %pI4.\n",
603 bond->dev->name, &newtarget);
605 targets[i] = newtarget;
609 pr_err("%s: ARP target table is full!\n",
615 } else if (buf[0] == '-') {
616 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
617 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
618 bond->dev->name, &newtarget);
623 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
624 if (targets[i] == newtarget) {
626 pr_info("%s: removing ARP target %pI4.\n",
627 bond->dev->name, &newtarget);
628 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
629 targets[j] = targets[j+1];
636 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
637 bond->dev->name, &newtarget);
642 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
651 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
654 * Show and set the up and down delays. These must be multiples of the
655 * MII monitoring value, and are stored internally as the multiplier.
656 * Thus, we must translate to MS for the real world.
658 static ssize_t bonding_show_downdelay(struct device *d,
659 struct device_attribute *attr,
662 struct bonding *bond = to_bond(d);
664 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
667 static ssize_t bonding_store_downdelay(struct device *d,
668 struct device_attribute *attr,
669 const char *buf, size_t count)
671 int new_value, ret = count;
672 struct bonding *bond = to_bond(d);
674 if (!(bond->params.miimon)) {
675 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
681 if (sscanf(buf, "%d", &new_value) != 1) {
682 pr_err("%s: no down delay value specified.\n", bond->dev->name);
687 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
688 bond->dev->name, new_value, 1, INT_MAX);
692 if ((new_value % bond->params.miimon) != 0) {
693 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
694 bond->dev->name, new_value,
696 (new_value / bond->params.miimon) *
697 bond->params.miimon);
699 bond->params.downdelay = new_value / bond->params.miimon;
700 pr_info("%s: Setting down delay to %d.\n",
702 bond->params.downdelay * bond->params.miimon);
709 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
710 bonding_show_downdelay, bonding_store_downdelay);
712 static ssize_t bonding_show_updelay(struct device *d,
713 struct device_attribute *attr,
716 struct bonding *bond = to_bond(d);
718 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
722 static ssize_t bonding_store_updelay(struct device *d,
723 struct device_attribute *attr,
724 const char *buf, size_t count)
726 int new_value, ret = count;
727 struct bonding *bond = to_bond(d);
729 if (!(bond->params.miimon)) {
730 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
736 if (sscanf(buf, "%d", &new_value) != 1) {
737 pr_err("%s: no up delay value specified.\n",
743 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
744 bond->dev->name, new_value, 1, INT_MAX);
748 if ((new_value % bond->params.miimon) != 0) {
749 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
750 bond->dev->name, new_value,
752 (new_value / bond->params.miimon) *
753 bond->params.miimon);
755 bond->params.updelay = new_value / bond->params.miimon;
756 pr_info("%s: Setting up delay to %d.\n",
758 bond->params.updelay * bond->params.miimon);
764 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
765 bonding_show_updelay, bonding_store_updelay);
768 * Show and set the LACP interval. Interface must be down, and the mode
769 * must be set to 802.3ad mode.
771 static ssize_t bonding_show_lacp(struct device *d,
772 struct device_attribute *attr,
775 struct bonding *bond = to_bond(d);
777 return sprintf(buf, "%s %d\n",
778 bond_lacp_tbl[bond->params.lacp_fast].modename,
779 bond->params.lacp_fast);
782 static ssize_t bonding_store_lacp(struct device *d,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
786 int new_value, ret = count;
787 struct bonding *bond = to_bond(d);
789 if (bond->dev->flags & IFF_UP) {
790 pr_err("%s: Unable to update LACP rate because interface is up.\n",
796 if (bond->params.mode != BOND_MODE_8023AD) {
797 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
803 new_value = bond_parse_parm(buf, bond_lacp_tbl);
805 if ((new_value == 1) || (new_value == 0)) {
806 bond->params.lacp_fast = new_value;
807 bond_3ad_update_lacp_rate(bond);
808 pr_info("%s: Setting LACP rate to %s (%d).\n",
809 bond->dev->name, bond_lacp_tbl[new_value].modename,
812 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
813 bond->dev->name, (int)strlen(buf) - 1, buf);
819 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
820 bonding_show_lacp, bonding_store_lacp);
822 static ssize_t bonding_show_min_links(struct device *d,
823 struct device_attribute *attr,
826 struct bonding *bond = to_bond(d);
828 return sprintf(buf, "%d\n", bond->params.min_links);
831 static ssize_t bonding_store_min_links(struct device *d,
832 struct device_attribute *attr,
833 const char *buf, size_t count)
835 struct bonding *bond = to_bond(d);
837 unsigned int new_value;
839 ret = kstrtouint(buf, 0, &new_value);
841 pr_err("%s: Ignoring invalid min links value %s.\n",
842 bond->dev->name, buf);
846 pr_info("%s: Setting min links value to %u\n",
847 bond->dev->name, new_value);
848 bond->params.min_links = new_value;
851 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
852 bonding_show_min_links, bonding_store_min_links);
854 static ssize_t bonding_show_ad_select(struct device *d,
855 struct device_attribute *attr,
858 struct bonding *bond = to_bond(d);
860 return sprintf(buf, "%s %d\n",
861 ad_select_tbl[bond->params.ad_select].modename,
862 bond->params.ad_select);
866 static ssize_t bonding_store_ad_select(struct device *d,
867 struct device_attribute *attr,
868 const char *buf, size_t count)
870 int new_value, ret = count;
871 struct bonding *bond = to_bond(d);
873 if (bond->dev->flags & IFF_UP) {
874 pr_err("%s: Unable to update ad_select because interface is up.\n",
880 new_value = bond_parse_parm(buf, ad_select_tbl);
882 if (new_value != -1) {
883 bond->params.ad_select = new_value;
884 pr_info("%s: Setting ad_select to %s (%d).\n",
885 bond->dev->name, ad_select_tbl[new_value].modename,
888 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
889 bond->dev->name, (int)strlen(buf) - 1, buf);
895 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
896 bonding_show_ad_select, bonding_store_ad_select);
899 * Show and set the number of peer notifications to send after a failover event.
901 static ssize_t bonding_show_num_peer_notif(struct device *d,
902 struct device_attribute *attr,
905 struct bonding *bond = to_bond(d);
906 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
909 static ssize_t bonding_store_num_peer_notif(struct device *d,
910 struct device_attribute *attr,
911 const char *buf, size_t count)
913 struct bonding *bond = to_bond(d);
914 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
915 return err ? err : count;
917 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
918 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
919 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
920 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
923 * Show and set the MII monitor interval. There are two tricky bits
924 * here. First, if MII monitoring is activated, then we must disable
925 * ARP monitoring. Second, if the timer isn't running, we must
928 static ssize_t bonding_show_miimon(struct device *d,
929 struct device_attribute *attr,
932 struct bonding *bond = to_bond(d);
934 return sprintf(buf, "%d\n", bond->params.miimon);
937 static ssize_t bonding_store_miimon(struct device *d,
938 struct device_attribute *attr,
939 const char *buf, size_t count)
941 int new_value, ret = count;
942 struct bonding *bond = to_bond(d);
944 if (sscanf(buf, "%d", &new_value) != 1) {
945 pr_err("%s: no miimon value specified.\n",
951 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
952 bond->dev->name, new_value, 1, INT_MAX);
956 pr_info("%s: Setting MII monitoring interval to %d.\n",
957 bond->dev->name, new_value);
958 bond->params.miimon = new_value;
959 if (bond->params.updelay)
960 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
962 bond->params.updelay * bond->params.miimon);
963 if (bond->params.downdelay)
964 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
966 bond->params.downdelay * bond->params.miimon);
967 if (bond->params.arp_interval) {
968 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
970 bond->params.arp_interval = 0;
971 if (bond->params.arp_validate) {
972 bond->params.arp_validate =
973 BOND_ARP_VALIDATE_NONE;
975 if (delayed_work_pending(&bond->arp_work)) {
976 cancel_delayed_work(&bond->arp_work);
977 flush_workqueue(bond->wq);
981 if (bond->dev->flags & IFF_UP) {
982 /* If the interface is up, we may need to fire off
983 * the MII timer. If the interface is down, the
984 * timer will get fired off when the open function
987 if (!delayed_work_pending(&bond->mii_work)) {
988 INIT_DELAYED_WORK(&bond->mii_work,
990 queue_delayed_work(bond->wq,
998 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
999 bonding_show_miimon, bonding_store_miimon);
1002 * Show and set the primary slave. The store function is much
1003 * simpler than bonding_store_slaves function because it only needs to
1004 * handle one interface name.
1005 * The bond must be a mode that supports a primary for this be
1008 static ssize_t bonding_show_primary(struct device *d,
1009 struct device_attribute *attr,
1013 struct bonding *bond = to_bond(d);
1015 if (bond->primary_slave)
1016 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1021 static ssize_t bonding_store_primary(struct device *d,
1022 struct device_attribute *attr,
1023 const char *buf, size_t count)
1026 struct slave *slave;
1027 struct bonding *bond = to_bond(d);
1029 if (!rtnl_trylock())
1030 return restart_syscall();
1032 read_lock(&bond->lock);
1033 write_lock_bh(&bond->curr_slave_lock);
1035 if (!USES_PRIMARY(bond->params.mode)) {
1036 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1037 bond->dev->name, bond->dev->name, bond->params.mode);
1039 bond_for_each_slave(bond, slave, i) {
1041 (slave->dev->name, buf,
1042 strlen(slave->dev->name)) == 0) {
1043 pr_info("%s: Setting %s as primary slave.\n",
1044 bond->dev->name, slave->dev->name);
1045 bond->primary_slave = slave;
1046 strcpy(bond->params.primary, slave->dev->name);
1047 bond_select_active_slave(bond);
1052 /* if we got here, then we didn't match the name of any slave */
1054 if (strlen(buf) == 0 || buf[0] == '\n') {
1055 pr_info("%s: Setting primary slave to None.\n",
1057 bond->primary_slave = NULL;
1058 bond_select_active_slave(bond);
1060 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1061 bond->dev->name, (int)strlen(buf) - 1, buf);
1065 write_unlock_bh(&bond->curr_slave_lock);
1066 read_unlock(&bond->lock);
1067 unblock_netpoll_tx();
1072 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1073 bonding_show_primary, bonding_store_primary);
1076 * Show and set the primary_reselect flag.
1078 static ssize_t bonding_show_primary_reselect(struct device *d,
1079 struct device_attribute *attr,
1082 struct bonding *bond = to_bond(d);
1084 return sprintf(buf, "%s %d\n",
1085 pri_reselect_tbl[bond->params.primary_reselect].modename,
1086 bond->params.primary_reselect);
1089 static ssize_t bonding_store_primary_reselect(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);
1096 if (!rtnl_trylock())
1097 return restart_syscall();
1099 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1100 if (new_value < 0) {
1101 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1103 (int) strlen(buf) - 1, buf);
1108 bond->params.primary_reselect = new_value;
1109 pr_info("%s: setting primary_reselect to %s (%d).\n",
1110 bond->dev->name, pri_reselect_tbl[new_value].modename,
1114 read_lock(&bond->lock);
1115 write_lock_bh(&bond->curr_slave_lock);
1116 bond_select_active_slave(bond);
1117 write_unlock_bh(&bond->curr_slave_lock);
1118 read_unlock(&bond->lock);
1119 unblock_netpoll_tx();
1124 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1125 bonding_show_primary_reselect,
1126 bonding_store_primary_reselect);
1129 * Show and set the use_carrier flag.
1131 static ssize_t bonding_show_carrier(struct device *d,
1132 struct device_attribute *attr,
1135 struct bonding *bond = to_bond(d);
1137 return sprintf(buf, "%d\n", bond->params.use_carrier);
1140 static ssize_t bonding_store_carrier(struct device *d,
1141 struct device_attribute *attr,
1142 const char *buf, size_t count)
1144 int new_value, ret = count;
1145 struct bonding *bond = to_bond(d);
1148 if (sscanf(buf, "%d", &new_value) != 1) {
1149 pr_err("%s: no use_carrier value specified.\n",
1154 if ((new_value == 0) || (new_value == 1)) {
1155 bond->params.use_carrier = new_value;
1156 pr_info("%s: Setting use_carrier to %d.\n",
1157 bond->dev->name, new_value);
1159 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1160 bond->dev->name, new_value);
1165 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1166 bonding_show_carrier, bonding_store_carrier);
1170 * Show and set currently active_slave.
1172 static ssize_t bonding_show_active_slave(struct device *d,
1173 struct device_attribute *attr,
1177 struct bonding *bond = to_bond(d);
1180 read_lock(&bond->curr_slave_lock);
1181 curr = bond->curr_active_slave;
1182 read_unlock(&bond->curr_slave_lock);
1184 if (USES_PRIMARY(bond->params.mode) && curr)
1185 count = sprintf(buf, "%s\n", curr->dev->name);
1189 static ssize_t bonding_store_active_slave(struct device *d,
1190 struct device_attribute *attr,
1191 const char *buf, size_t count)
1194 struct slave *slave;
1195 struct slave *old_active = NULL;
1196 struct slave *new_active = NULL;
1197 struct bonding *bond = to_bond(d);
1199 if (!rtnl_trylock())
1200 return restart_syscall();
1203 read_lock(&bond->lock);
1204 write_lock_bh(&bond->curr_slave_lock);
1206 if (!USES_PRIMARY(bond->params.mode))
1207 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1208 bond->dev->name, bond->dev->name, bond->params.mode);
1210 bond_for_each_slave(bond, slave, i) {
1212 (slave->dev->name, buf,
1213 strlen(slave->dev->name)) == 0) {
1214 old_active = bond->curr_active_slave;
1216 if (new_active == old_active) {
1218 pr_info("%s: %s is already the current active slave.\n",
1226 (new_active->link == BOND_LINK_UP) &&
1227 IS_UP(new_active->dev)) {
1228 pr_info("%s: Setting %s as active slave.\n",
1231 bond_change_active_slave(bond, new_active);
1234 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1244 /* if we got here, then we didn't match the name of any slave */
1246 if (strlen(buf) == 0 || buf[0] == '\n') {
1247 pr_info("%s: Setting active slave to None.\n",
1249 bond->primary_slave = NULL;
1250 bond_select_active_slave(bond);
1252 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
1253 bond->dev->name, (int)strlen(buf) - 1, buf);
1257 write_unlock_bh(&bond->curr_slave_lock);
1258 read_unlock(&bond->lock);
1259 unblock_netpoll_tx();
1266 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1267 bonding_show_active_slave, bonding_store_active_slave);
1271 * Show link status of the bond interface.
1273 static ssize_t bonding_show_mii_status(struct device *d,
1274 struct device_attribute *attr,
1278 struct bonding *bond = to_bond(d);
1280 read_lock(&bond->curr_slave_lock);
1281 curr = bond->curr_active_slave;
1282 read_unlock(&bond->curr_slave_lock);
1284 return sprintf(buf, "%s\n", curr ? "up" : "down");
1286 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1290 * Show current 802.3ad aggregator ID.
1292 static ssize_t bonding_show_ad_aggregator(struct device *d,
1293 struct device_attribute *attr,
1297 struct bonding *bond = to_bond(d);
1299 if (bond->params.mode == BOND_MODE_8023AD) {
1300 struct ad_info ad_info;
1301 count = sprintf(buf, "%d\n",
1302 (bond_3ad_get_active_agg_info(bond, &ad_info))
1303 ? 0 : ad_info.aggregator_id);
1308 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1312 * Show number of active 802.3ad ports.
1314 static ssize_t bonding_show_ad_num_ports(struct device *d,
1315 struct device_attribute *attr,
1319 struct bonding *bond = to_bond(d);
1321 if (bond->params.mode == BOND_MODE_8023AD) {
1322 struct ad_info ad_info;
1323 count = sprintf(buf, "%d\n",
1324 (bond_3ad_get_active_agg_info(bond, &ad_info))
1325 ? 0 : ad_info.ports);
1330 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1334 * Show current 802.3ad actor key.
1336 static ssize_t bonding_show_ad_actor_key(struct device *d,
1337 struct device_attribute *attr,
1341 struct bonding *bond = to_bond(d);
1343 if (bond->params.mode == BOND_MODE_8023AD) {
1344 struct ad_info ad_info;
1345 count = sprintf(buf, "%d\n",
1346 (bond_3ad_get_active_agg_info(bond, &ad_info))
1347 ? 0 : ad_info.actor_key);
1352 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1356 * Show current 802.3ad partner key.
1358 static ssize_t bonding_show_ad_partner_key(struct device *d,
1359 struct device_attribute *attr,
1363 struct bonding *bond = to_bond(d);
1365 if (bond->params.mode == BOND_MODE_8023AD) {
1366 struct ad_info ad_info;
1367 count = sprintf(buf, "%d\n",
1368 (bond_3ad_get_active_agg_info(bond, &ad_info))
1369 ? 0 : ad_info.partner_key);
1374 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1378 * Show current 802.3ad partner mac.
1380 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1381 struct device_attribute *attr,
1385 struct bonding *bond = to_bond(d);
1387 if (bond->params.mode == BOND_MODE_8023AD) {
1388 struct ad_info ad_info;
1389 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1390 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1395 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1398 * Show the queue_ids of the slaves in the current bond.
1400 static ssize_t bonding_show_queue_id(struct device *d,
1401 struct device_attribute *attr,
1404 struct slave *slave;
1406 struct bonding *bond = to_bond(d);
1408 if (!rtnl_trylock())
1409 return restart_syscall();
1411 read_lock(&bond->lock);
1412 bond_for_each_slave(bond, slave, i) {
1413 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1414 /* not enough space for another interface_name:queue_id pair */
1415 if ((PAGE_SIZE - res) > 10)
1416 res = PAGE_SIZE - 10;
1417 res += sprintf(buf + res, "++more++ ");
1420 res += sprintf(buf + res, "%s:%d ",
1421 slave->dev->name, slave->queue_id);
1423 read_unlock(&bond->lock);
1425 buf[res-1] = '\n'; /* eat the leftover space */
1431 * Set the queue_ids of the slaves in the current bond. The bond
1432 * interface must be enslaved for this to work.
1434 static ssize_t bonding_store_queue_id(struct device *d,
1435 struct device_attribute *attr,
1436 const char *buffer, size_t count)
1438 struct slave *slave, *update_slave;
1439 struct bonding *bond = to_bond(d);
1443 struct net_device *sdev = NULL;
1445 if (!rtnl_trylock())
1446 return restart_syscall();
1448 /* delim will point to queue id if successful */
1449 delim = strchr(buffer, ':');
1454 * Terminate string that points to device name and bump it
1455 * up one, so we can read the queue id there.
1458 if (sscanf(++delim, "%hd\n", &qid) != 1)
1461 /* Check buffer length, valid ifname and queue id */
1462 if (strlen(buffer) > IFNAMSIZ ||
1463 !dev_valid_name(buffer) ||
1464 qid > bond->params.tx_queues)
1467 /* Get the pointer to that interface if it exists */
1468 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1472 read_lock(&bond->lock);
1474 /* Search for thes slave and check for duplicate qids */
1475 update_slave = NULL;
1476 bond_for_each_slave(bond, slave, i) {
1477 if (sdev == slave->dev)
1479 * We don't need to check the matching
1480 * slave for dups, since we're overwriting it
1482 update_slave = slave;
1483 else if (qid && qid == slave->queue_id) {
1484 goto err_no_cmd_unlock;
1489 goto err_no_cmd_unlock;
1491 /* Actually set the qids for the slave */
1492 update_slave->queue_id = qid;
1494 read_unlock(&bond->lock);
1500 read_unlock(&bond->lock);
1502 pr_info("invalid input for queue_id set for %s.\n",
1508 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1509 bonding_store_queue_id);
1513 * Show and set the all_slaves_active flag.
1515 static ssize_t bonding_show_slaves_active(struct device *d,
1516 struct device_attribute *attr,
1519 struct bonding *bond = to_bond(d);
1521 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1524 static ssize_t bonding_store_slaves_active(struct device *d,
1525 struct device_attribute *attr,
1526 const char *buf, size_t count)
1528 int i, new_value, ret = count;
1529 struct bonding *bond = to_bond(d);
1530 struct slave *slave;
1532 if (sscanf(buf, "%d", &new_value) != 1) {
1533 pr_err("%s: no all_slaves_active value specified.\n",
1539 if (new_value == bond->params.all_slaves_active)
1542 if ((new_value == 0) || (new_value == 1)) {
1543 bond->params.all_slaves_active = new_value;
1545 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1546 bond->dev->name, new_value);
1551 bond_for_each_slave(bond, slave, i) {
1552 if (!bond_is_active_slave(slave)) {
1554 slave->inactive = 0;
1556 slave->inactive = 1;
1562 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1563 bonding_show_slaves_active, bonding_store_slaves_active);
1566 * Show and set the number of IGMP membership reports to send on link failure
1568 static ssize_t bonding_show_resend_igmp(struct device *d,
1569 struct device_attribute *attr,
1572 struct bonding *bond = to_bond(d);
1574 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1577 static ssize_t bonding_store_resend_igmp(struct device *d,
1578 struct device_attribute *attr,
1579 const char *buf, size_t count)
1581 int new_value, ret = count;
1582 struct bonding *bond = to_bond(d);
1584 if (sscanf(buf, "%d", &new_value) != 1) {
1585 pr_err("%s: no resend_igmp value specified.\n",
1591 if (new_value < 0 || new_value > 255) {
1592 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1593 bond->dev->name, new_value);
1598 pr_info("%s: Setting resend_igmp to %d.\n",
1599 bond->dev->name, new_value);
1600 bond->params.resend_igmp = new_value;
1605 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1606 bonding_show_resend_igmp, bonding_store_resend_igmp);
1608 static struct attribute *per_bond_attrs[] = {
1609 &dev_attr_slaves.attr,
1610 &dev_attr_mode.attr,
1611 &dev_attr_fail_over_mac.attr,
1612 &dev_attr_arp_validate.attr,
1613 &dev_attr_arp_interval.attr,
1614 &dev_attr_arp_ip_target.attr,
1615 &dev_attr_downdelay.attr,
1616 &dev_attr_updelay.attr,
1617 &dev_attr_lacp_rate.attr,
1618 &dev_attr_ad_select.attr,
1619 &dev_attr_xmit_hash_policy.attr,
1620 &dev_attr_num_grat_arp.attr,
1621 &dev_attr_num_unsol_na.attr,
1622 &dev_attr_miimon.attr,
1623 &dev_attr_primary.attr,
1624 &dev_attr_primary_reselect.attr,
1625 &dev_attr_use_carrier.attr,
1626 &dev_attr_active_slave.attr,
1627 &dev_attr_mii_status.attr,
1628 &dev_attr_ad_aggregator.attr,
1629 &dev_attr_ad_num_ports.attr,
1630 &dev_attr_ad_actor_key.attr,
1631 &dev_attr_ad_partner_key.attr,
1632 &dev_attr_ad_partner_mac.attr,
1633 &dev_attr_queue_id.attr,
1634 &dev_attr_all_slaves_active.attr,
1635 &dev_attr_resend_igmp.attr,
1636 &dev_attr_min_links.attr,
1640 static struct attribute_group bonding_group = {
1642 .attrs = per_bond_attrs,
1646 * Initialize sysfs. This sets up the bonding_masters file in
1649 int bond_create_sysfs(void)
1653 ret = netdev_class_create_file(&class_attr_bonding_masters);
1655 * Permit multiple loads of the module by ignoring failures to
1656 * create the bonding_masters sysfs file. Bonding devices
1657 * created by second or subsequent loads of the module will
1658 * not be listed in, or controllable by, bonding_masters, but
1659 * will have the usual "bonding" sysfs directory.
1661 * This is done to preserve backwards compatibility for
1662 * initscripts/sysconfig, which load bonding multiple times to
1663 * configure multiple bonding devices.
1665 if (ret == -EEXIST) {
1666 /* Is someone being kinky and naming a device bonding_master? */
1667 if (__dev_get_by_name(&init_net,
1668 class_attr_bonding_masters.attr.name))
1669 pr_err("network device named %s already exists in sysfs",
1670 class_attr_bonding_masters.attr.name);
1679 * Remove /sys/class/net/bonding_masters.
1681 void bond_destroy_sysfs(void)
1683 netdev_class_remove_file(&class_attr_bonding_masters);
1687 * Initialize sysfs for each bond. This sets up and registers
1688 * the 'bondctl' directory for each individual bond under /sys/class/net.
1690 void bond_prepare_sysfs_group(struct bonding *bond)
1692 bond->dev->sysfs_groups[0] = &bonding_group;