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,
59 container_of(attr, struct bond_net, class_attr_bonding_masters);
65 list_for_each_entry(bond, &bn->dev_list, bond_list) {
66 if (res > (PAGE_SIZE - IFNAMSIZ)) {
67 /* not enough space for another interface name */
68 if ((PAGE_SIZE - res) > 10)
70 res += sprintf(buf + res, "++more++ ");
73 res += sprintf(buf + res, "%s ", bond->dev->name);
76 buf[res-1] = '\n'; /* eat the leftover space */
82 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
86 list_for_each_entry(bond, &bn->dev_list, bond_list) {
87 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
94 * "store" function for the bond_masters attribute. This is what
95 * creates and deletes entire bonds.
97 * The class parameter is ignored.
101 static ssize_t bonding_store_bonds(struct class *cls,
102 struct class_attribute *attr,
103 const char *buffer, size_t count)
105 struct bond_net *bn =
106 container_of(attr, struct bond_net, class_attr_bonding_masters);
107 char command[IFNAMSIZ + 1] = {0, };
111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
117 if (command[0] == '+') {
118 pr_info("%s is being created...\n", ifname);
119 rv = bond_create(bn->net, ifname);
122 pr_info("%s already exists.\n", ifname);
124 pr_info("%s creation failed.\n", ifname);
127 } else if (command[0] == '-') {
128 struct net_device *bond_dev;
131 bond_dev = bond_get_by_name(bn, ifname);
133 pr_info("%s is being deleted...\n", ifname);
134 unregister_netdevice(bond_dev);
136 pr_err("unable to delete non-existent %s\n", ifname);
143 /* Always return either count or an error. If you return 0, you'll
144 * get called forever, which is bad.
149 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
153 static const void *bonding_namespace(struct class *cls,
154 const struct class_attribute *attr)
156 const struct bond_net *bn =
157 container_of(attr, struct bond_net, class_attr_bonding_masters);
161 /* class attribute for bond_masters file. This ends up in /sys/class/net */
162 static const struct class_attribute class_attr_bonding_masters = {
164 .name = "bonding_masters",
165 .mode = S_IWUSR | S_IRUGO,
167 .show = bonding_show_bonds,
168 .store = bonding_store_bonds,
169 .namespace = bonding_namespace,
172 int bond_create_slave_symlinks(struct net_device *master,
173 struct net_device *slave)
175 char linkname[IFNAMSIZ+7];
178 /* first, create a link from the slave back to the master */
179 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
183 /* next, create a link from the master to the slave */
184 sprintf(linkname, "slave_%s", slave->name);
185 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
191 void bond_destroy_slave_symlinks(struct net_device *master,
192 struct net_device *slave)
194 char linkname[IFNAMSIZ+7];
196 sysfs_remove_link(&(slave->dev.kobj), "master");
197 sprintf(linkname, "slave_%s", slave->name);
198 sysfs_remove_link(&(master->dev.kobj), linkname);
203 * Show the slaves in the current bond.
205 static ssize_t bonding_show_slaves(struct device *d,
206 struct device_attribute *attr, char *buf)
210 struct bonding *bond = to_bond(d);
212 read_lock(&bond->lock);
213 bond_for_each_slave(bond, slave, i) {
214 if (res > (PAGE_SIZE - IFNAMSIZ)) {
215 /* not enough space for another interface name */
216 if ((PAGE_SIZE - res) > 10)
217 res = PAGE_SIZE - 10;
218 res += sprintf(buf + res, "++more++ ");
221 res += sprintf(buf + res, "%s ", slave->dev->name);
223 read_unlock(&bond->lock);
225 buf[res-1] = '\n'; /* eat the leftover space */
230 * Set the slaves in the current bond. The bond interface must be
231 * up for this to succeed.
232 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
233 * All hard work should be done there.
235 static ssize_t bonding_store_slaves(struct device *d,
236 struct device_attribute *attr,
237 const char *buffer, size_t count)
239 char command[IFNAMSIZ + 1] = { 0, };
241 int res, ret = count;
242 struct net_device *dev;
243 struct bonding *bond = to_bond(d);
246 return restart_syscall();
248 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
249 ifname = command + 1;
250 if ((strlen(command) <= 1) ||
251 !dev_valid_name(ifname))
254 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
256 pr_info("%s: Interface %s does not exist!\n",
257 bond->dev->name, ifname);
262 switch (command[0]) {
264 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
265 res = bond_enslave(bond->dev, dev);
269 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
270 res = bond_release(bond->dev, dev);
282 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
291 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
292 bonding_store_slaves);
295 * Show and set the bonding mode. The bond interface must be down to
298 static ssize_t bonding_show_mode(struct device *d,
299 struct device_attribute *attr, char *buf)
301 struct bonding *bond = to_bond(d);
303 return sprintf(buf, "%s %d\n",
304 bond_mode_tbl[bond->params.mode].modename,
308 static ssize_t bonding_store_mode(struct device *d,
309 struct device_attribute *attr,
310 const char *buf, size_t count)
312 int new_value, ret = count;
313 struct bonding *bond = to_bond(d);
315 if (bond->dev->flags & IFF_UP) {
316 pr_err("unable to update mode of %s because interface is up.\n",
322 new_value = bond_parse_parm(buf, bond_mode_tbl);
324 pr_err("%s: Ignoring invalid mode value %.*s.\n",
325 bond->dev->name, (int)strlen(buf) - 1, buf);
329 if ((new_value == BOND_MODE_ALB ||
330 new_value == BOND_MODE_TLB) &&
331 bond->params.arp_interval) {
332 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
333 bond->dev->name, bond_mode_tbl[new_value].modename);
338 bond->params.mode = new_value;
339 bond_set_mode_ops(bond, bond->params.mode);
340 pr_info("%s: setting mode to %s (%d).\n",
341 bond->dev->name, bond_mode_tbl[new_value].modename,
346 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
347 bonding_show_mode, bonding_store_mode);
350 * Show and set the bonding transmit hash method.
351 * The bond interface must be down to change the xmit hash policy.
353 static ssize_t bonding_show_xmit_hash(struct device *d,
354 struct device_attribute *attr,
357 struct bonding *bond = to_bond(d);
359 return sprintf(buf, "%s %d\n",
360 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
361 bond->params.xmit_policy);
364 static ssize_t bonding_store_xmit_hash(struct device *d,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
368 int new_value, ret = count;
369 struct bonding *bond = to_bond(d);
371 if (bond->dev->flags & IFF_UP) {
372 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
378 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
380 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
382 (int)strlen(buf) - 1, buf);
386 bond->params.xmit_policy = new_value;
387 bond_set_mode_ops(bond, bond->params.mode);
388 pr_info("%s: setting xmit hash policy to %s (%d).\n",
390 xmit_hashtype_tbl[new_value].modename, new_value);
395 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
396 bonding_show_xmit_hash, bonding_store_xmit_hash);
399 * Show and set arp_validate.
401 static ssize_t bonding_show_arp_validate(struct device *d,
402 struct device_attribute *attr,
405 struct bonding *bond = to_bond(d);
407 return sprintf(buf, "%s %d\n",
408 arp_validate_tbl[bond->params.arp_validate].modename,
409 bond->params.arp_validate);
412 static ssize_t bonding_store_arp_validate(struct device *d,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
417 struct bonding *bond = to_bond(d);
419 new_value = bond_parse_parm(buf, arp_validate_tbl);
421 pr_err("%s: Ignoring invalid arp_validate value %s\n",
422 bond->dev->name, buf);
425 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
426 pr_err("%s: arp_validate only supported in active-backup mode.\n",
430 pr_info("%s: setting arp_validate to %s (%d).\n",
431 bond->dev->name, arp_validate_tbl[new_value].modename,
434 bond->params.arp_validate = new_value;
439 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
440 bonding_store_arp_validate);
443 * Show and store fail_over_mac. User only allowed to change the
444 * value when there are no slaves.
446 static ssize_t bonding_show_fail_over_mac(struct device *d,
447 struct device_attribute *attr,
450 struct bonding *bond = to_bond(d);
452 return sprintf(buf, "%s %d\n",
453 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
454 bond->params.fail_over_mac);
457 static ssize_t bonding_store_fail_over_mac(struct device *d,
458 struct device_attribute *attr,
459 const char *buf, size_t count)
462 struct bonding *bond = to_bond(d);
464 if (bond->slave_cnt != 0) {
465 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
470 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
472 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
473 bond->dev->name, buf);
477 bond->params.fail_over_mac = new_value;
478 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
479 bond->dev->name, fail_over_mac_tbl[new_value].modename,
485 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
486 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
489 * Show and set the arp timer interval. There are two tricky bits
490 * here. First, if ARP monitoring is activated, then we must disable
491 * MII monitoring. Second, if the ARP timer isn't running, we must
494 static ssize_t bonding_show_arp_interval(struct device *d,
495 struct device_attribute *attr,
498 struct bonding *bond = to_bond(d);
500 return sprintf(buf, "%d\n", bond->params.arp_interval);
503 static ssize_t bonding_store_arp_interval(struct device *d,
504 struct device_attribute *attr,
505 const char *buf, size_t count)
507 int new_value, ret = count;
508 struct bonding *bond = to_bond(d);
510 if (sscanf(buf, "%d", &new_value) != 1) {
511 pr_err("%s: no arp_interval value specified.\n",
517 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
518 bond->dev->name, new_value, INT_MAX);
522 if (bond->params.mode == BOND_MODE_ALB ||
523 bond->params.mode == BOND_MODE_TLB) {
524 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
525 bond->dev->name, bond->dev->name);
529 pr_info("%s: Setting ARP monitoring interval to %d.\n",
530 bond->dev->name, new_value);
531 bond->params.arp_interval = new_value;
532 if (bond->params.miimon) {
533 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
534 bond->dev->name, bond->dev->name);
535 bond->params.miimon = 0;
536 if (delayed_work_pending(&bond->mii_work)) {
537 cancel_delayed_work(&bond->mii_work);
538 flush_workqueue(bond->wq);
541 if (!bond->params.arp_targets[0]) {
542 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
545 if (bond->dev->flags & IFF_UP) {
546 /* If the interface is up, we may need to fire off
547 * the ARP timer. If the interface is down, the
548 * timer will get fired off when the open function
551 if (!delayed_work_pending(&bond->arp_work)) {
552 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
553 INIT_DELAYED_WORK(&bond->arp_work,
554 bond_activebackup_arp_mon);
556 INIT_DELAYED_WORK(&bond->arp_work,
557 bond_loadbalance_arp_mon);
559 queue_delayed_work(bond->wq, &bond->arp_work, 0);
566 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
567 bonding_show_arp_interval, bonding_store_arp_interval);
570 * Show and set the arp targets.
572 static ssize_t bonding_show_arp_targets(struct device *d,
573 struct device_attribute *attr,
577 struct bonding *bond = to_bond(d);
579 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
580 if (bond->params.arp_targets[i])
581 res += sprintf(buf + res, "%pI4 ",
582 &bond->params.arp_targets[i]);
585 buf[res-1] = '\n'; /* eat the leftover space */
589 static ssize_t bonding_store_arp_targets(struct device *d,
590 struct device_attribute *attr,
591 const char *buf, size_t count)
594 int i = 0, done = 0, ret = count;
595 struct bonding *bond = to_bond(d);
598 targets = bond->params.arp_targets;
599 newtarget = in_aton(buf + 1);
602 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
603 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
604 bond->dev->name, &newtarget);
608 /* look for an empty slot to put the target in, and check for dupes */
609 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
610 if (targets[i] == newtarget) { /* duplicate */
611 pr_err("%s: ARP target %pI4 is already present\n",
612 bond->dev->name, &newtarget);
616 if (targets[i] == 0) {
617 pr_info("%s: adding ARP target %pI4.\n",
618 bond->dev->name, &newtarget);
620 targets[i] = newtarget;
624 pr_err("%s: ARP target table is full!\n",
630 } else if (buf[0] == '-') {
631 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
632 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
633 bond->dev->name, &newtarget);
638 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
639 if (targets[i] == newtarget) {
641 pr_info("%s: removing ARP target %pI4.\n",
642 bond->dev->name, &newtarget);
643 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
644 targets[j] = targets[j+1];
651 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
652 bond->dev->name, &newtarget);
657 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
666 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
669 * Show and set the up and down delays. These must be multiples of the
670 * MII monitoring value, and are stored internally as the multiplier.
671 * Thus, we must translate to MS for the real world.
673 static ssize_t bonding_show_downdelay(struct device *d,
674 struct device_attribute *attr,
677 struct bonding *bond = to_bond(d);
679 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
682 static ssize_t bonding_store_downdelay(struct device *d,
683 struct device_attribute *attr,
684 const char *buf, size_t count)
686 int new_value, ret = count;
687 struct bonding *bond = to_bond(d);
689 if (!(bond->params.miimon)) {
690 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
696 if (sscanf(buf, "%d", &new_value) != 1) {
697 pr_err("%s: no down delay value specified.\n", bond->dev->name);
702 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
703 bond->dev->name, new_value, 1, INT_MAX);
707 if ((new_value % bond->params.miimon) != 0) {
708 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
709 bond->dev->name, new_value,
711 (new_value / bond->params.miimon) *
712 bond->params.miimon);
714 bond->params.downdelay = new_value / bond->params.miimon;
715 pr_info("%s: Setting down delay to %d.\n",
717 bond->params.downdelay * bond->params.miimon);
724 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
725 bonding_show_downdelay, bonding_store_downdelay);
727 static ssize_t bonding_show_updelay(struct device *d,
728 struct device_attribute *attr,
731 struct bonding *bond = to_bond(d);
733 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
737 static ssize_t bonding_store_updelay(struct device *d,
738 struct device_attribute *attr,
739 const char *buf, size_t count)
741 int new_value, ret = count;
742 struct bonding *bond = to_bond(d);
744 if (!(bond->params.miimon)) {
745 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
751 if (sscanf(buf, "%d", &new_value) != 1) {
752 pr_err("%s: no up delay value specified.\n",
758 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
759 bond->dev->name, new_value, 1, INT_MAX);
763 if ((new_value % bond->params.miimon) != 0) {
764 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
765 bond->dev->name, new_value,
767 (new_value / bond->params.miimon) *
768 bond->params.miimon);
770 bond->params.updelay = new_value / bond->params.miimon;
771 pr_info("%s: Setting up delay to %d.\n",
773 bond->params.updelay * bond->params.miimon);
779 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
780 bonding_show_updelay, bonding_store_updelay);
783 * Show and set the LACP interval. Interface must be down, and the mode
784 * must be set to 802.3ad mode.
786 static ssize_t bonding_show_lacp(struct device *d,
787 struct device_attribute *attr,
790 struct bonding *bond = to_bond(d);
792 return sprintf(buf, "%s %d\n",
793 bond_lacp_tbl[bond->params.lacp_fast].modename,
794 bond->params.lacp_fast);
797 static ssize_t bonding_store_lacp(struct device *d,
798 struct device_attribute *attr,
799 const char *buf, size_t count)
801 int new_value, ret = count;
802 struct bonding *bond = to_bond(d);
804 if (bond->dev->flags & IFF_UP) {
805 pr_err("%s: Unable to update LACP rate because interface is up.\n",
811 if (bond->params.mode != BOND_MODE_8023AD) {
812 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
818 new_value = bond_parse_parm(buf, bond_lacp_tbl);
820 if ((new_value == 1) || (new_value == 0)) {
821 bond->params.lacp_fast = new_value;
822 bond_3ad_update_lacp_rate(bond);
823 pr_info("%s: Setting LACP rate to %s (%d).\n",
824 bond->dev->name, bond_lacp_tbl[new_value].modename,
827 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
828 bond->dev->name, (int)strlen(buf) - 1, buf);
834 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
835 bonding_show_lacp, bonding_store_lacp);
837 static ssize_t bonding_show_min_links(struct device *d,
838 struct device_attribute *attr,
841 struct bonding *bond = to_bond(d);
843 return sprintf(buf, "%d\n", bond->params.min_links);
846 static ssize_t bonding_store_min_links(struct device *d,
847 struct device_attribute *attr,
848 const char *buf, size_t count)
850 struct bonding *bond = to_bond(d);
852 unsigned int new_value;
854 ret = kstrtouint(buf, 0, &new_value);
856 pr_err("%s: Ignoring invalid min links value %s.\n",
857 bond->dev->name, buf);
861 pr_info("%s: Setting min links value to %u\n",
862 bond->dev->name, new_value);
863 bond->params.min_links = new_value;
866 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
867 bonding_show_min_links, bonding_store_min_links);
869 static ssize_t bonding_show_ad_select(struct device *d,
870 struct device_attribute *attr,
873 struct bonding *bond = to_bond(d);
875 return sprintf(buf, "%s %d\n",
876 ad_select_tbl[bond->params.ad_select].modename,
877 bond->params.ad_select);
881 static ssize_t bonding_store_ad_select(struct device *d,
882 struct device_attribute *attr,
883 const char *buf, size_t count)
885 int new_value, ret = count;
886 struct bonding *bond = to_bond(d);
888 if (bond->dev->flags & IFF_UP) {
889 pr_err("%s: Unable to update ad_select because interface is up.\n",
895 new_value = bond_parse_parm(buf, ad_select_tbl);
897 if (new_value != -1) {
898 bond->params.ad_select = new_value;
899 pr_info("%s: Setting ad_select to %s (%d).\n",
900 bond->dev->name, ad_select_tbl[new_value].modename,
903 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
904 bond->dev->name, (int)strlen(buf) - 1, buf);
910 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
911 bonding_show_ad_select, bonding_store_ad_select);
914 * Show and set the number of peer notifications to send after a failover event.
916 static ssize_t bonding_show_num_peer_notif(struct device *d,
917 struct device_attribute *attr,
920 struct bonding *bond = to_bond(d);
921 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
924 static ssize_t bonding_store_num_peer_notif(struct device *d,
925 struct device_attribute *attr,
926 const char *buf, size_t count)
928 struct bonding *bond = to_bond(d);
929 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
930 return err ? err : count;
932 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
933 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
934 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
935 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
938 * Show and set the MII monitor interval. There are two tricky bits
939 * here. First, if MII monitoring is activated, then we must disable
940 * ARP monitoring. Second, if the timer isn't running, we must
943 static ssize_t bonding_show_miimon(struct device *d,
944 struct device_attribute *attr,
947 struct bonding *bond = to_bond(d);
949 return sprintf(buf, "%d\n", bond->params.miimon);
952 static ssize_t bonding_store_miimon(struct device *d,
953 struct device_attribute *attr,
954 const char *buf, size_t count)
956 int new_value, ret = count;
957 struct bonding *bond = to_bond(d);
959 if (sscanf(buf, "%d", &new_value) != 1) {
960 pr_err("%s: no miimon value specified.\n",
966 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
967 bond->dev->name, new_value, 1, INT_MAX);
971 pr_info("%s: Setting MII monitoring interval to %d.\n",
972 bond->dev->name, new_value);
973 bond->params.miimon = new_value;
974 if (bond->params.updelay)
975 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
977 bond->params.updelay * bond->params.miimon);
978 if (bond->params.downdelay)
979 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
981 bond->params.downdelay * bond->params.miimon);
982 if (bond->params.arp_interval) {
983 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
985 bond->params.arp_interval = 0;
986 if (bond->params.arp_validate) {
987 bond->params.arp_validate =
988 BOND_ARP_VALIDATE_NONE;
990 if (delayed_work_pending(&bond->arp_work)) {
991 cancel_delayed_work(&bond->arp_work);
992 flush_workqueue(bond->wq);
996 if (bond->dev->flags & IFF_UP) {
997 /* If the interface is up, we may need to fire off
998 * the MII timer. If the interface is down, the
999 * timer will get fired off when the open function
1002 if (!delayed_work_pending(&bond->mii_work)) {
1003 INIT_DELAYED_WORK(&bond->mii_work,
1005 queue_delayed_work(bond->wq,
1006 &bond->mii_work, 0);
1013 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1014 bonding_show_miimon, bonding_store_miimon);
1017 * Show and set the primary slave. The store function is much
1018 * simpler than bonding_store_slaves function because it only needs to
1019 * handle one interface name.
1020 * The bond must be a mode that supports a primary for this be
1023 static ssize_t bonding_show_primary(struct device *d,
1024 struct device_attribute *attr,
1028 struct bonding *bond = to_bond(d);
1030 if (bond->primary_slave)
1031 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1036 static ssize_t bonding_store_primary(struct device *d,
1037 struct device_attribute *attr,
1038 const char *buf, size_t count)
1041 struct slave *slave;
1042 struct bonding *bond = to_bond(d);
1043 char ifname[IFNAMSIZ];
1045 if (!rtnl_trylock())
1046 return restart_syscall();
1048 read_lock(&bond->lock);
1049 write_lock_bh(&bond->curr_slave_lock);
1051 if (!USES_PRIMARY(bond->params.mode)) {
1052 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1053 bond->dev->name, bond->dev->name, bond->params.mode);
1057 sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
1059 /* check to see if we are clearing primary */
1060 if (!strlen(ifname) || buf[0] == '\n') {
1061 pr_info("%s: Setting primary slave to None.\n",
1063 bond->primary_slave = NULL;
1064 bond_select_active_slave(bond);
1068 bond_for_each_slave(bond, slave, i) {
1069 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1070 pr_info("%s: Setting %s as primary slave.\n",
1071 bond->dev->name, slave->dev->name);
1072 bond->primary_slave = slave;
1073 strcpy(bond->params.primary, slave->dev->name);
1074 bond_select_active_slave(bond);
1079 pr_info("%s: Unable to set %.*s as primary slave.\n",
1080 bond->dev->name, (int)strlen(buf) - 1, buf);
1082 write_unlock_bh(&bond->curr_slave_lock);
1083 read_unlock(&bond->lock);
1084 unblock_netpoll_tx();
1089 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1090 bonding_show_primary, bonding_store_primary);
1093 * Show and set the primary_reselect flag.
1095 static ssize_t bonding_show_primary_reselect(struct device *d,
1096 struct device_attribute *attr,
1099 struct bonding *bond = to_bond(d);
1101 return sprintf(buf, "%s %d\n",
1102 pri_reselect_tbl[bond->params.primary_reselect].modename,
1103 bond->params.primary_reselect);
1106 static ssize_t bonding_store_primary_reselect(struct device *d,
1107 struct device_attribute *attr,
1108 const char *buf, size_t count)
1110 int new_value, ret = count;
1111 struct bonding *bond = to_bond(d);
1113 if (!rtnl_trylock())
1114 return restart_syscall();
1116 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1117 if (new_value < 0) {
1118 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1120 (int) strlen(buf) - 1, buf);
1125 bond->params.primary_reselect = new_value;
1126 pr_info("%s: setting primary_reselect to %s (%d).\n",
1127 bond->dev->name, pri_reselect_tbl[new_value].modename,
1131 read_lock(&bond->lock);
1132 write_lock_bh(&bond->curr_slave_lock);
1133 bond_select_active_slave(bond);
1134 write_unlock_bh(&bond->curr_slave_lock);
1135 read_unlock(&bond->lock);
1136 unblock_netpoll_tx();
1141 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1142 bonding_show_primary_reselect,
1143 bonding_store_primary_reselect);
1146 * Show and set the use_carrier flag.
1148 static ssize_t bonding_show_carrier(struct device *d,
1149 struct device_attribute *attr,
1152 struct bonding *bond = to_bond(d);
1154 return sprintf(buf, "%d\n", bond->params.use_carrier);
1157 static ssize_t bonding_store_carrier(struct device *d,
1158 struct device_attribute *attr,
1159 const char *buf, size_t count)
1161 int new_value, ret = count;
1162 struct bonding *bond = to_bond(d);
1165 if (sscanf(buf, "%d", &new_value) != 1) {
1166 pr_err("%s: no use_carrier value specified.\n",
1171 if ((new_value == 0) || (new_value == 1)) {
1172 bond->params.use_carrier = new_value;
1173 pr_info("%s: Setting use_carrier to %d.\n",
1174 bond->dev->name, new_value);
1176 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1177 bond->dev->name, new_value);
1182 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1183 bonding_show_carrier, bonding_store_carrier);
1187 * Show and set currently active_slave.
1189 static ssize_t bonding_show_active_slave(struct device *d,
1190 struct device_attribute *attr,
1194 struct bonding *bond = to_bond(d);
1197 read_lock(&bond->curr_slave_lock);
1198 curr = bond->curr_active_slave;
1199 read_unlock(&bond->curr_slave_lock);
1201 if (USES_PRIMARY(bond->params.mode) && curr)
1202 count = sprintf(buf, "%s\n", curr->dev->name);
1206 static ssize_t bonding_store_active_slave(struct device *d,
1207 struct device_attribute *attr,
1208 const char *buf, size_t count)
1211 struct slave *slave;
1212 struct slave *old_active = NULL;
1213 struct slave *new_active = NULL;
1214 struct bonding *bond = to_bond(d);
1215 char ifname[IFNAMSIZ];
1217 if (!rtnl_trylock())
1218 return restart_syscall();
1221 read_lock(&bond->lock);
1222 write_lock_bh(&bond->curr_slave_lock);
1224 if (!USES_PRIMARY(bond->params.mode)) {
1225 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1226 bond->dev->name, bond->dev->name, bond->params.mode);
1230 sscanf(buf, "%16s", ifname); /* IFNAMSIZ */
1232 /* check to see if we are clearing active */
1233 if (!strlen(ifname) || buf[0] == '\n') {
1234 pr_info("%s: Clearing current active slave.\n",
1236 bond->curr_active_slave = NULL;
1237 bond_select_active_slave(bond);
1241 bond_for_each_slave(bond, slave, i) {
1242 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1243 old_active = bond->curr_active_slave;
1245 if (new_active == old_active) {
1247 pr_info("%s: %s is already the current"
1256 (new_active->link == BOND_LINK_UP) &&
1257 IS_UP(new_active->dev)) {
1258 pr_info("%s: Setting %s as active"
1262 bond_change_active_slave(bond,
1266 pr_info("%s: Could not set %s as"
1267 " active slave; either %s is"
1268 " down or the link is down.\n",
1278 pr_info("%s: Unable to set %.*s as active slave.\n",
1279 bond->dev->name, (int)strlen(buf) - 1, buf);
1281 write_unlock_bh(&bond->curr_slave_lock);
1282 read_unlock(&bond->lock);
1283 unblock_netpoll_tx();
1290 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1291 bonding_show_active_slave, bonding_store_active_slave);
1295 * Show link status of the bond interface.
1297 static ssize_t bonding_show_mii_status(struct device *d,
1298 struct device_attribute *attr,
1302 struct bonding *bond = to_bond(d);
1304 read_lock(&bond->curr_slave_lock);
1305 curr = bond->curr_active_slave;
1306 read_unlock(&bond->curr_slave_lock);
1308 return sprintf(buf, "%s\n", curr ? "up" : "down");
1310 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1314 * Show current 802.3ad aggregator ID.
1316 static ssize_t bonding_show_ad_aggregator(struct device *d,
1317 struct device_attribute *attr,
1321 struct bonding *bond = to_bond(d);
1323 if (bond->params.mode == BOND_MODE_8023AD) {
1324 struct ad_info ad_info;
1325 count = sprintf(buf, "%d\n",
1326 (bond_3ad_get_active_agg_info(bond, &ad_info))
1327 ? 0 : ad_info.aggregator_id);
1332 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1336 * Show number of active 802.3ad ports.
1338 static ssize_t bonding_show_ad_num_ports(struct device *d,
1339 struct device_attribute *attr,
1343 struct bonding *bond = to_bond(d);
1345 if (bond->params.mode == BOND_MODE_8023AD) {
1346 struct ad_info ad_info;
1347 count = sprintf(buf, "%d\n",
1348 (bond_3ad_get_active_agg_info(bond, &ad_info))
1349 ? 0 : ad_info.ports);
1354 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1358 * Show current 802.3ad actor key.
1360 static ssize_t bonding_show_ad_actor_key(struct device *d,
1361 struct device_attribute *attr,
1365 struct bonding *bond = to_bond(d);
1367 if (bond->params.mode == BOND_MODE_8023AD) {
1368 struct ad_info ad_info;
1369 count = sprintf(buf, "%d\n",
1370 (bond_3ad_get_active_agg_info(bond, &ad_info))
1371 ? 0 : ad_info.actor_key);
1376 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1380 * Show current 802.3ad partner key.
1382 static ssize_t bonding_show_ad_partner_key(struct device *d,
1383 struct device_attribute *attr,
1387 struct bonding *bond = to_bond(d);
1389 if (bond->params.mode == BOND_MODE_8023AD) {
1390 struct ad_info ad_info;
1391 count = sprintf(buf, "%d\n",
1392 (bond_3ad_get_active_agg_info(bond, &ad_info))
1393 ? 0 : ad_info.partner_key);
1398 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1402 * Show current 802.3ad partner mac.
1404 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1405 struct device_attribute *attr,
1409 struct bonding *bond = to_bond(d);
1411 if (bond->params.mode == BOND_MODE_8023AD) {
1412 struct ad_info ad_info;
1413 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1414 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1419 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1422 * Show the queue_ids of the slaves in the current bond.
1424 static ssize_t bonding_show_queue_id(struct device *d,
1425 struct device_attribute *attr,
1428 struct slave *slave;
1430 struct bonding *bond = to_bond(d);
1432 if (!rtnl_trylock())
1433 return restart_syscall();
1435 read_lock(&bond->lock);
1436 bond_for_each_slave(bond, slave, i) {
1437 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1438 /* not enough space for another interface_name:queue_id pair */
1439 if ((PAGE_SIZE - res) > 10)
1440 res = PAGE_SIZE - 10;
1441 res += sprintf(buf + res, "++more++ ");
1444 res += sprintf(buf + res, "%s:%d ",
1445 slave->dev->name, slave->queue_id);
1447 read_unlock(&bond->lock);
1449 buf[res-1] = '\n'; /* eat the leftover space */
1455 * Set the queue_ids of the slaves in the current bond. The bond
1456 * interface must be enslaved for this to work.
1458 static ssize_t bonding_store_queue_id(struct device *d,
1459 struct device_attribute *attr,
1460 const char *buffer, size_t count)
1462 struct slave *slave, *update_slave;
1463 struct bonding *bond = to_bond(d);
1467 struct net_device *sdev = NULL;
1469 if (!rtnl_trylock())
1470 return restart_syscall();
1472 /* delim will point to queue id if successful */
1473 delim = strchr(buffer, ':');
1478 * Terminate string that points to device name and bump it
1479 * up one, so we can read the queue id there.
1482 if (sscanf(++delim, "%hd\n", &qid) != 1)
1485 /* Check buffer length, valid ifname and queue id */
1486 if (strlen(buffer) > IFNAMSIZ ||
1487 !dev_valid_name(buffer) ||
1488 qid > bond->params.tx_queues)
1491 /* Get the pointer to that interface if it exists */
1492 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1496 read_lock(&bond->lock);
1498 /* Search for thes slave and check for duplicate qids */
1499 update_slave = NULL;
1500 bond_for_each_slave(bond, slave, i) {
1501 if (sdev == slave->dev)
1503 * We don't need to check the matching
1504 * slave for dups, since we're overwriting it
1506 update_slave = slave;
1507 else if (qid && qid == slave->queue_id) {
1508 goto err_no_cmd_unlock;
1513 goto err_no_cmd_unlock;
1515 /* Actually set the qids for the slave */
1516 update_slave->queue_id = qid;
1518 read_unlock(&bond->lock);
1524 read_unlock(&bond->lock);
1526 pr_info("invalid input for queue_id set for %s.\n",
1532 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1533 bonding_store_queue_id);
1537 * Show and set the all_slaves_active flag.
1539 static ssize_t bonding_show_slaves_active(struct device *d,
1540 struct device_attribute *attr,
1543 struct bonding *bond = to_bond(d);
1545 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1548 static ssize_t bonding_store_slaves_active(struct device *d,
1549 struct device_attribute *attr,
1550 const char *buf, size_t count)
1552 int i, new_value, ret = count;
1553 struct bonding *bond = to_bond(d);
1554 struct slave *slave;
1556 if (sscanf(buf, "%d", &new_value) != 1) {
1557 pr_err("%s: no all_slaves_active value specified.\n",
1563 if (new_value == bond->params.all_slaves_active)
1566 if ((new_value == 0) || (new_value == 1)) {
1567 bond->params.all_slaves_active = new_value;
1569 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1570 bond->dev->name, new_value);
1575 bond_for_each_slave(bond, slave, i) {
1576 if (!bond_is_active_slave(slave)) {
1578 slave->inactive = 0;
1580 slave->inactive = 1;
1586 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1587 bonding_show_slaves_active, bonding_store_slaves_active);
1590 * Show and set the number of IGMP membership reports to send on link failure
1592 static ssize_t bonding_show_resend_igmp(struct device *d,
1593 struct device_attribute *attr,
1596 struct bonding *bond = to_bond(d);
1598 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1601 static ssize_t bonding_store_resend_igmp(struct device *d,
1602 struct device_attribute *attr,
1603 const char *buf, size_t count)
1605 int new_value, ret = count;
1606 struct bonding *bond = to_bond(d);
1608 if (sscanf(buf, "%d", &new_value) != 1) {
1609 pr_err("%s: no resend_igmp value specified.\n",
1615 if (new_value < 0 || new_value > 255) {
1616 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1617 bond->dev->name, new_value);
1622 pr_info("%s: Setting resend_igmp to %d.\n",
1623 bond->dev->name, new_value);
1624 bond->params.resend_igmp = new_value;
1629 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1630 bonding_show_resend_igmp, bonding_store_resend_igmp);
1632 static struct attribute *per_bond_attrs[] = {
1633 &dev_attr_slaves.attr,
1634 &dev_attr_mode.attr,
1635 &dev_attr_fail_over_mac.attr,
1636 &dev_attr_arp_validate.attr,
1637 &dev_attr_arp_interval.attr,
1638 &dev_attr_arp_ip_target.attr,
1639 &dev_attr_downdelay.attr,
1640 &dev_attr_updelay.attr,
1641 &dev_attr_lacp_rate.attr,
1642 &dev_attr_ad_select.attr,
1643 &dev_attr_xmit_hash_policy.attr,
1644 &dev_attr_num_grat_arp.attr,
1645 &dev_attr_num_unsol_na.attr,
1646 &dev_attr_miimon.attr,
1647 &dev_attr_primary.attr,
1648 &dev_attr_primary_reselect.attr,
1649 &dev_attr_use_carrier.attr,
1650 &dev_attr_active_slave.attr,
1651 &dev_attr_mii_status.attr,
1652 &dev_attr_ad_aggregator.attr,
1653 &dev_attr_ad_num_ports.attr,
1654 &dev_attr_ad_actor_key.attr,
1655 &dev_attr_ad_partner_key.attr,
1656 &dev_attr_ad_partner_mac.attr,
1657 &dev_attr_queue_id.attr,
1658 &dev_attr_all_slaves_active.attr,
1659 &dev_attr_resend_igmp.attr,
1660 &dev_attr_min_links.attr,
1664 static struct attribute_group bonding_group = {
1666 .attrs = per_bond_attrs,
1670 * Initialize sysfs. This sets up the bonding_masters file in
1673 int bond_create_sysfs(struct bond_net *bn)
1677 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1678 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1680 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
1682 * Permit multiple loads of the module by ignoring failures to
1683 * create the bonding_masters sysfs file. Bonding devices
1684 * created by second or subsequent loads of the module will
1685 * not be listed in, or controllable by, bonding_masters, but
1686 * will have the usual "bonding" sysfs directory.
1688 * This is done to preserve backwards compatibility for
1689 * initscripts/sysconfig, which load bonding multiple times to
1690 * configure multiple bonding devices.
1692 if (ret == -EEXIST) {
1693 /* Is someone being kinky and naming a device bonding_master? */
1694 if (__dev_get_by_name(bn->net,
1695 class_attr_bonding_masters.attr.name))
1696 pr_err("network device named %s already exists in sysfs",
1697 class_attr_bonding_masters.attr.name);
1706 * Remove /sys/class/net/bonding_masters.
1708 void bond_destroy_sysfs(struct bond_net *bn)
1710 netdev_class_remove_file(&bn->class_attr_bonding_masters);
1714 * Initialize sysfs for each bond. This sets up and registers
1715 * the 'bondctl' directory for each individual bond under /sys/class/net.
1717 void bond_prepare_sysfs_group(struct bonding *bond)
1719 bond->dev->sysfs_groups[0] = &bonding_group;