2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/sched.h>
30 #include <linux/types.h>
31 #include <linux/string.h>
32 #include <linux/netdevice.h>
33 #include <linux/inetdevice.h>
35 #include <linux/sysfs.h>
36 #include <linux/ctype.h>
37 #include <linux/inet.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/etherdevice.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <linux/nsproxy.h>
46 #define to_dev(obj) container_of(obj, struct device, kobj)
47 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
53 static ssize_t bonding_show_bonds(struct class *cls,
54 struct class_attribute *attr,
58 container_of(attr, struct bond_net, class_attr_bonding_masters);
64 list_for_each_entry(bond, &bn->dev_list, bond_list) {
65 if (res > (PAGE_SIZE - IFNAMSIZ)) {
66 /* not enough space for another interface name */
67 if ((PAGE_SIZE - res) > 10)
69 res += sprintf(buf + res, "++more++ ");
72 res += sprintf(buf + res, "%s ", bond->dev->name);
75 buf[res-1] = '\n'; /* eat the leftover space */
81 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
85 list_for_each_entry(bond, &bn->dev_list, bond_list) {
86 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
96 * The class parameter is ignored.
100 static ssize_t bonding_store_bonds(struct class *cls,
101 struct class_attribute *attr,
102 const char *buffer, size_t count)
104 struct bond_net *bn =
105 container_of(attr, struct bond_net, class_attr_bonding_masters);
106 char command[IFNAMSIZ + 1] = {0, };
110 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
111 ifname = command + 1;
112 if ((strlen(command) <= 1) ||
113 !dev_valid_name(ifname))
116 if (command[0] == '+') {
117 pr_info("%s is being created...\n", ifname);
118 rv = bond_create(bn->net, ifname);
121 pr_info("%s already exists.\n", ifname);
123 pr_info("%s creation failed.\n", ifname);
126 } else if (command[0] == '-') {
127 struct net_device *bond_dev;
130 bond_dev = bond_get_by_name(bn, ifname);
132 pr_info("%s is being deleted...\n", ifname);
133 unregister_netdevice(bond_dev);
135 pr_err("unable to delete non-existent %s\n", ifname);
142 /* Always return either count or an error. If you return 0, you'll
143 * get called forever, which is bad.
148 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
152 /* class attribute for bond_masters file. This ends up in /sys/class/net */
153 static const struct class_attribute class_attr_bonding_masters = {
155 .name = "bonding_masters",
156 .mode = S_IWUSR | S_IRUGO,
158 .show = bonding_show_bonds,
159 .store = bonding_store_bonds,
163 * Show the slaves in the current bond.
165 static ssize_t bonding_show_slaves(struct device *d,
166 struct device_attribute *attr, char *buf)
168 struct bonding *bond = to_bond(d);
169 struct list_head *iter;
174 return restart_syscall();
176 bond_for_each_slave(bond, slave, iter) {
177 if (res > (PAGE_SIZE - IFNAMSIZ)) {
178 /* not enough space for another interface name */
179 if ((PAGE_SIZE - res) > 10)
180 res = PAGE_SIZE - 10;
181 res += sprintf(buf + res, "++more++ ");
184 res += sprintf(buf + res, "%s ", slave->dev->name);
190 buf[res-1] = '\n'; /* eat the leftover space */
196 * Set the slaves in the current bond.
197 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
198 * All hard work should be done there.
200 static ssize_t bonding_store_slaves(struct device *d,
201 struct device_attribute *attr,
202 const char *buffer, size_t count)
204 char command[IFNAMSIZ + 1] = { 0, };
206 int res, ret = count;
207 struct net_device *dev;
208 struct bonding *bond = to_bond(d);
211 return restart_syscall();
213 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
214 ifname = command + 1;
215 if ((strlen(command) <= 1) ||
216 !dev_valid_name(ifname))
219 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
221 pr_info("%s: Interface %s does not exist!\n",
222 bond->dev->name, ifname);
227 switch (command[0]) {
229 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
230 res = bond_enslave(bond->dev, dev);
234 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
235 res = bond_release(bond->dev, dev);
247 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
256 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
257 bonding_store_slaves);
260 * Show and set the bonding mode. The bond interface must be down to
263 static ssize_t bonding_show_mode(struct device *d,
264 struct device_attribute *attr, char *buf)
266 struct bonding *bond = to_bond(d);
268 return sprintf(buf, "%s %d\n",
269 bond_mode_tbl[bond->params.mode].modename,
273 static ssize_t bonding_store_mode(struct device *d,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
278 struct bonding *bond = to_bond(d);
280 new_value = bond_parse_parm(buf, bond_mode_tbl);
282 pr_err("%s: Ignoring invalid mode value %.*s.\n",
283 bond->dev->name, (int)strlen(buf) - 1, buf);
287 return restart_syscall();
289 ret = bond_option_mode_set(bond, new_value);
291 pr_info("%s: setting mode to %s (%d).\n",
292 bond->dev->name, bond_mode_tbl[new_value].modename,
300 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
301 bonding_show_mode, bonding_store_mode);
304 * Show and set the bonding transmit hash method.
306 static ssize_t bonding_show_xmit_hash(struct device *d,
307 struct device_attribute *attr,
310 struct bonding *bond = to_bond(d);
312 return sprintf(buf, "%s %d\n",
313 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
314 bond->params.xmit_policy);
317 static ssize_t bonding_store_xmit_hash(struct device *d,
318 struct device_attribute *attr,
319 const char *buf, size_t count)
321 int new_value, ret = count;
322 struct bonding *bond = to_bond(d);
324 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
326 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
328 (int)strlen(buf) - 1, buf);
331 bond->params.xmit_policy = new_value;
332 pr_info("%s: setting xmit hash policy to %s (%d).\n",
334 xmit_hashtype_tbl[new_value].modename, new_value);
339 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
340 bonding_show_xmit_hash, bonding_store_xmit_hash);
343 * Show and set arp_validate.
345 static ssize_t bonding_show_arp_validate(struct device *d,
346 struct device_attribute *attr,
349 struct bonding *bond = to_bond(d);
351 return sprintf(buf, "%s %d\n",
352 arp_validate_tbl[bond->params.arp_validate].modename,
353 bond->params.arp_validate);
356 static ssize_t bonding_store_arp_validate(struct device *d,
357 struct device_attribute *attr,
358 const char *buf, size_t count)
360 struct bonding *bond = to_bond(d);
361 int new_value, ret = count;
364 return restart_syscall();
365 new_value = bond_parse_parm(buf, arp_validate_tbl);
367 pr_err("%s: Ignoring invalid arp_validate value %s\n",
368 bond->dev->name, buf);
372 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
373 pr_err("%s: arp_validate only supported in active-backup mode.\n",
378 pr_info("%s: setting arp_validate to %s (%d).\n",
379 bond->dev->name, arp_validate_tbl[new_value].modename,
382 if (bond->dev->flags & IFF_UP) {
384 bond->recv_probe = NULL;
385 else if (bond->params.arp_interval)
386 bond->recv_probe = bond_arp_rcv;
388 bond->params.arp_validate = new_value;
395 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
396 bonding_store_arp_validate);
398 * Show and set arp_all_targets.
400 static ssize_t bonding_show_arp_all_targets(struct device *d,
401 struct device_attribute *attr,
404 struct bonding *bond = to_bond(d);
405 int value = bond->params.arp_all_targets;
407 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
411 static ssize_t bonding_store_arp_all_targets(struct device *d,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
415 struct bonding *bond = to_bond(d);
418 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
420 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
421 bond->dev->name, buf);
424 pr_info("%s: setting arp_all_targets to %s (%d).\n",
425 bond->dev->name, arp_all_targets_tbl[new_value].modename,
428 bond->params.arp_all_targets = new_value;
433 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
434 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
437 * Show and store fail_over_mac. User only allowed to change the
438 * value when there are no slaves.
440 static ssize_t bonding_show_fail_over_mac(struct device *d,
441 struct device_attribute *attr,
444 struct bonding *bond = to_bond(d);
446 return sprintf(buf, "%s %d\n",
447 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
448 bond->params.fail_over_mac);
451 static ssize_t bonding_store_fail_over_mac(struct device *d,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
455 int new_value, ret = count;
456 struct bonding *bond = to_bond(d);
459 return restart_syscall();
461 if (bond_has_slaves(bond)) {
462 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
468 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
470 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
471 bond->dev->name, buf);
476 bond->params.fail_over_mac = new_value;
477 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
478 bond->dev->name, fail_over_mac_tbl[new_value].modename,
486 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
487 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
490 * Show and set the arp timer interval. There are two tricky bits
491 * here. First, if ARP monitoring is activated, then we must disable
492 * MII monitoring. Second, if the ARP timer isn't running, we must
495 static ssize_t bonding_show_arp_interval(struct device *d,
496 struct device_attribute *attr,
499 struct bonding *bond = to_bond(d);
501 return sprintf(buf, "%d\n", bond->params.arp_interval);
504 static ssize_t bonding_store_arp_interval(struct device *d,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct bonding *bond = to_bond(d);
509 int new_value, ret = count;
512 return restart_syscall();
513 if (sscanf(buf, "%d", &new_value) != 1) {
514 pr_err("%s: no arp_interval value specified.\n",
520 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
521 bond->dev->name, new_value, INT_MAX);
525 if (bond->params.mode == BOND_MODE_ALB ||
526 bond->params.mode == BOND_MODE_TLB) {
527 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
528 bond->dev->name, bond->dev->name);
532 pr_info("%s: Setting ARP monitoring interval to %d.\n",
533 bond->dev->name, new_value);
534 bond->params.arp_interval = new_value;
536 if (bond->params.miimon) {
537 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
538 bond->dev->name, bond->dev->name);
539 bond->params.miimon = 0;
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
552 if (bond->params.arp_validate)
553 bond->recv_probe = NULL;
554 cancel_delayed_work_sync(&bond->arp_work);
556 /* arp_validate can be set only in active-backup mode */
557 if (bond->params.arp_validate)
558 bond->recv_probe = bond_arp_rcv;
559 cancel_delayed_work_sync(&bond->mii_work);
560 queue_delayed_work(bond->wq, &bond->arp_work, 0);
567 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
568 bonding_show_arp_interval, bonding_store_arp_interval);
571 * Show and set the arp targets.
573 static ssize_t bonding_show_arp_targets(struct device *d,
574 struct device_attribute *attr,
578 struct bonding *bond = to_bond(d);
580 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
581 if (bond->params.arp_targets[i])
582 res += sprintf(buf + res, "%pI4 ",
583 &bond->params.arp_targets[i]);
586 buf[res-1] = '\n'; /* eat the leftover space */
590 static ssize_t bonding_store_arp_targets(struct device *d,
591 struct device_attribute *attr,
592 const char *buf, size_t count)
594 struct bonding *bond = to_bond(d);
595 struct list_head *iter;
597 __be32 newtarget, *targets;
598 unsigned long *targets_rx;
599 int ind, i, j, ret = -EINVAL;
602 return restart_syscall();
604 targets = bond->params.arp_targets;
605 newtarget = in_aton(buf + 1);
608 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
609 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
610 bond->dev->name, &newtarget);
614 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
615 pr_err("%s: ARP target %pI4 is already present\n",
616 bond->dev->name, &newtarget);
620 ind = bond_get_targets_ip(targets, 0); /* first free slot */
622 pr_err("%s: ARP target table is full!\n",
627 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
629 /* not to race with bond_arp_rcv */
630 write_lock_bh(&bond->lock);
631 bond_for_each_slave(bond, slave, iter)
632 slave->target_last_arp_rx[ind] = jiffies;
633 targets[ind] = newtarget;
634 write_unlock_bh(&bond->lock);
635 } else if (buf[0] == '-') {
636 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
637 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
638 bond->dev->name, &newtarget);
642 ind = bond_get_targets_ip(targets, newtarget);
644 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
645 bond->dev->name, &newtarget);
649 if (ind == 0 && !targets[1] && bond->params.arp_interval)
650 pr_warn("%s: removing last arp target with arp_interval on\n",
653 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
656 write_lock_bh(&bond->lock);
657 bond_for_each_slave(bond, slave, iter) {
658 targets_rx = slave->target_last_arp_rx;
660 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
661 targets_rx[j] = targets_rx[j+1];
664 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
665 targets[i] = targets[i+1];
667 write_unlock_bh(&bond->lock);
669 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
680 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
683 * Show and set the up and down delays. These must be multiples of the
684 * MII monitoring value, and are stored internally as the multiplier.
685 * Thus, we must translate to MS for the real world.
687 static ssize_t bonding_show_downdelay(struct device *d,
688 struct device_attribute *attr,
691 struct bonding *bond = to_bond(d);
693 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
696 static ssize_t bonding_store_downdelay(struct device *d,
697 struct device_attribute *attr,
698 const char *buf, size_t count)
700 int new_value, ret = count;
701 struct bonding *bond = to_bond(d);
703 if (!(bond->params.miimon)) {
704 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
710 if (sscanf(buf, "%d", &new_value) != 1) {
711 pr_err("%s: no down delay value specified.\n", bond->dev->name);
716 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
717 bond->dev->name, new_value, 0, INT_MAX);
721 if ((new_value % bond->params.miimon) != 0) {
722 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
723 bond->dev->name, new_value,
725 (new_value / bond->params.miimon) *
726 bond->params.miimon);
728 bond->params.downdelay = new_value / bond->params.miimon;
729 pr_info("%s: Setting down delay to %d.\n",
731 bond->params.downdelay * bond->params.miimon);
738 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
739 bonding_show_downdelay, bonding_store_downdelay);
741 static ssize_t bonding_show_updelay(struct device *d,
742 struct device_attribute *attr,
745 struct bonding *bond = to_bond(d);
747 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
751 static ssize_t bonding_store_updelay(struct device *d,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
755 int new_value, ret = count;
756 struct bonding *bond = to_bond(d);
758 if (!(bond->params.miimon)) {
759 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
765 if (sscanf(buf, "%d", &new_value) != 1) {
766 pr_err("%s: no up delay value specified.\n",
772 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
773 bond->dev->name, new_value, 0, INT_MAX);
777 if ((new_value % bond->params.miimon) != 0) {
778 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
779 bond->dev->name, new_value,
781 (new_value / bond->params.miimon) *
782 bond->params.miimon);
784 bond->params.updelay = new_value / bond->params.miimon;
785 pr_info("%s: Setting up delay to %d.\n",
787 bond->params.updelay * bond->params.miimon);
793 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
794 bonding_show_updelay, bonding_store_updelay);
797 * Show and set the LACP interval. Interface must be down, and the mode
798 * must be set to 802.3ad mode.
800 static ssize_t bonding_show_lacp(struct device *d,
801 struct device_attribute *attr,
804 struct bonding *bond = to_bond(d);
806 return sprintf(buf, "%s %d\n",
807 bond_lacp_tbl[bond->params.lacp_fast].modename,
808 bond->params.lacp_fast);
811 static ssize_t bonding_store_lacp(struct device *d,
812 struct device_attribute *attr,
813 const char *buf, size_t count)
815 struct bonding *bond = to_bond(d);
816 int new_value, ret = count;
819 return restart_syscall();
821 if (bond->dev->flags & IFF_UP) {
822 pr_err("%s: Unable to update LACP rate because interface is up.\n",
828 if (bond->params.mode != BOND_MODE_8023AD) {
829 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
835 new_value = bond_parse_parm(buf, bond_lacp_tbl);
837 if ((new_value == 1) || (new_value == 0)) {
838 bond->params.lacp_fast = new_value;
839 bond_3ad_update_lacp_rate(bond);
840 pr_info("%s: Setting LACP rate to %s (%d).\n",
841 bond->dev->name, bond_lacp_tbl[new_value].modename,
844 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
845 bond->dev->name, (int)strlen(buf) - 1, buf);
853 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
854 bonding_show_lacp, bonding_store_lacp);
856 static ssize_t bonding_show_min_links(struct device *d,
857 struct device_attribute *attr,
860 struct bonding *bond = to_bond(d);
862 return sprintf(buf, "%d\n", bond->params.min_links);
865 static ssize_t bonding_store_min_links(struct device *d,
866 struct device_attribute *attr,
867 const char *buf, size_t count)
869 struct bonding *bond = to_bond(d);
871 unsigned int new_value;
873 ret = kstrtouint(buf, 0, &new_value);
875 pr_err("%s: Ignoring invalid min links value %s.\n",
876 bond->dev->name, buf);
880 pr_info("%s: Setting min links value to %u\n",
881 bond->dev->name, new_value);
882 bond->params.min_links = new_value;
885 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
886 bonding_show_min_links, bonding_store_min_links);
888 static ssize_t bonding_show_ad_select(struct device *d,
889 struct device_attribute *attr,
892 struct bonding *bond = to_bond(d);
894 return sprintf(buf, "%s %d\n",
895 ad_select_tbl[bond->params.ad_select].modename,
896 bond->params.ad_select);
900 static ssize_t bonding_store_ad_select(struct device *d,
901 struct device_attribute *attr,
902 const char *buf, size_t count)
904 int new_value, ret = count;
905 struct bonding *bond = to_bond(d);
907 if (bond->dev->flags & IFF_UP) {
908 pr_err("%s: Unable to update ad_select because interface is up.\n",
914 new_value = bond_parse_parm(buf, ad_select_tbl);
916 if (new_value != -1) {
917 bond->params.ad_select = new_value;
918 pr_info("%s: Setting ad_select to %s (%d).\n",
919 bond->dev->name, ad_select_tbl[new_value].modename,
922 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
923 bond->dev->name, (int)strlen(buf) - 1, buf);
929 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
930 bonding_show_ad_select, bonding_store_ad_select);
933 * Show and set the number of peer notifications to send after a failover event.
935 static ssize_t bonding_show_num_peer_notif(struct device *d,
936 struct device_attribute *attr,
939 struct bonding *bond = to_bond(d);
940 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
943 static ssize_t bonding_store_num_peer_notif(struct device *d,
944 struct device_attribute *attr,
945 const char *buf, size_t count)
947 struct bonding *bond = to_bond(d);
948 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
949 return err ? err : count;
951 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
952 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
953 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
954 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
957 * Show and set the MII monitor interval. There are two tricky bits
958 * here. First, if MII monitoring is activated, then we must disable
959 * ARP monitoring. Second, if the timer isn't running, we must
962 static ssize_t bonding_show_miimon(struct device *d,
963 struct device_attribute *attr,
966 struct bonding *bond = to_bond(d);
968 return sprintf(buf, "%d\n", bond->params.miimon);
971 static ssize_t bonding_store_miimon(struct device *d,
972 struct device_attribute *attr,
973 const char *buf, size_t count)
975 int new_value, ret = count;
976 struct bonding *bond = to_bond(d);
979 return restart_syscall();
980 if (sscanf(buf, "%d", &new_value) != 1) {
981 pr_err("%s: no miimon value specified.\n",
987 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
988 bond->dev->name, new_value, 0, INT_MAX);
992 pr_info("%s: Setting MII monitoring interval to %d.\n",
993 bond->dev->name, new_value);
994 bond->params.miimon = new_value;
995 if (bond->params.updelay)
996 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
998 bond->params.updelay * bond->params.miimon);
999 if (bond->params.downdelay)
1000 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1002 bond->params.downdelay * bond->params.miimon);
1003 if (new_value && bond->params.arp_interval) {
1004 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1006 bond->params.arp_interval = 0;
1007 if (bond->params.arp_validate)
1008 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1010 if (bond->dev->flags & IFF_UP) {
1011 /* If the interface is up, we may need to fire off
1012 * the MII timer. If the interface is down, the
1013 * timer will get fired off when the open function
1017 cancel_delayed_work_sync(&bond->mii_work);
1019 cancel_delayed_work_sync(&bond->arp_work);
1020 queue_delayed_work(bond->wq, &bond->mii_work, 0);
1027 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1028 bonding_show_miimon, bonding_store_miimon);
1031 * Show and set the primary slave. The store function is much
1032 * simpler than bonding_store_slaves function because it only needs to
1033 * handle one interface name.
1034 * The bond must be a mode that supports a primary for this be
1037 static ssize_t bonding_show_primary(struct device *d,
1038 struct device_attribute *attr,
1042 struct bonding *bond = to_bond(d);
1044 if (bond->primary_slave)
1045 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1050 static ssize_t bonding_store_primary(struct device *d,
1051 struct device_attribute *attr,
1052 const char *buf, size_t count)
1054 struct bonding *bond = to_bond(d);
1055 struct list_head *iter;
1056 char ifname[IFNAMSIZ];
1057 struct slave *slave;
1059 if (!rtnl_trylock())
1060 return restart_syscall();
1062 read_lock(&bond->lock);
1063 write_lock_bh(&bond->curr_slave_lock);
1065 if (!USES_PRIMARY(bond->params.mode)) {
1066 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1067 bond->dev->name, bond->dev->name, bond->params.mode);
1071 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1073 /* check to see if we are clearing primary */
1074 if (!strlen(ifname) || buf[0] == '\n') {
1075 pr_info("%s: Setting primary slave to None.\n",
1077 bond->primary_slave = NULL;
1078 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1079 bond_select_active_slave(bond);
1083 bond_for_each_slave(bond, slave, iter) {
1084 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1085 pr_info("%s: Setting %s as primary slave.\n",
1086 bond->dev->name, slave->dev->name);
1087 bond->primary_slave = slave;
1088 strcpy(bond->params.primary, slave->dev->name);
1089 bond_select_active_slave(bond);
1094 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1095 bond->params.primary[IFNAMSIZ - 1] = 0;
1097 pr_info("%s: Recording %s as primary, "
1098 "but it has not been enslaved to %s yet.\n",
1099 bond->dev->name, ifname, bond->dev->name);
1101 write_unlock_bh(&bond->curr_slave_lock);
1102 read_unlock(&bond->lock);
1103 unblock_netpoll_tx();
1108 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1109 bonding_show_primary, bonding_store_primary);
1112 * Show and set the primary_reselect flag.
1114 static ssize_t bonding_show_primary_reselect(struct device *d,
1115 struct device_attribute *attr,
1118 struct bonding *bond = to_bond(d);
1120 return sprintf(buf, "%s %d\n",
1121 pri_reselect_tbl[bond->params.primary_reselect].modename,
1122 bond->params.primary_reselect);
1125 static ssize_t bonding_store_primary_reselect(struct device *d,
1126 struct device_attribute *attr,
1127 const char *buf, size_t count)
1129 int new_value, ret = count;
1130 struct bonding *bond = to_bond(d);
1132 if (!rtnl_trylock())
1133 return restart_syscall();
1135 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1136 if (new_value < 0) {
1137 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1139 (int) strlen(buf) - 1, buf);
1144 bond->params.primary_reselect = new_value;
1145 pr_info("%s: setting primary_reselect to %s (%d).\n",
1146 bond->dev->name, pri_reselect_tbl[new_value].modename,
1150 read_lock(&bond->lock);
1151 write_lock_bh(&bond->curr_slave_lock);
1152 bond_select_active_slave(bond);
1153 write_unlock_bh(&bond->curr_slave_lock);
1154 read_unlock(&bond->lock);
1155 unblock_netpoll_tx();
1160 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1161 bonding_show_primary_reselect,
1162 bonding_store_primary_reselect);
1165 * Show and set the use_carrier flag.
1167 static ssize_t bonding_show_carrier(struct device *d,
1168 struct device_attribute *attr,
1171 struct bonding *bond = to_bond(d);
1173 return sprintf(buf, "%d\n", bond->params.use_carrier);
1176 static ssize_t bonding_store_carrier(struct device *d,
1177 struct device_attribute *attr,
1178 const char *buf, size_t count)
1180 int new_value, ret = count;
1181 struct bonding *bond = to_bond(d);
1184 if (sscanf(buf, "%d", &new_value) != 1) {
1185 pr_err("%s: no use_carrier value specified.\n",
1190 if ((new_value == 0) || (new_value == 1)) {
1191 bond->params.use_carrier = new_value;
1192 pr_info("%s: Setting use_carrier to %d.\n",
1193 bond->dev->name, new_value);
1195 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1196 bond->dev->name, new_value);
1201 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1202 bonding_show_carrier, bonding_store_carrier);
1206 * Show and set currently active_slave.
1208 static ssize_t bonding_show_active_slave(struct device *d,
1209 struct device_attribute *attr,
1212 struct bonding *bond = to_bond(d);
1213 struct net_device *slave_dev;
1217 slave_dev = bond_option_active_slave_get_rcu(bond);
1219 count = sprintf(buf, "%s\n", slave_dev->name);
1225 static ssize_t bonding_store_active_slave(struct device *d,
1226 struct device_attribute *attr,
1227 const char *buf, size_t count)
1230 struct bonding *bond = to_bond(d);
1231 char ifname[IFNAMSIZ];
1232 struct net_device *dev;
1234 if (!rtnl_trylock())
1235 return restart_syscall();
1237 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1238 if (!strlen(ifname) || buf[0] == '\n') {
1241 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1248 ret = bond_option_active_slave_set(bond, dev);
1258 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1259 bonding_show_active_slave, bonding_store_active_slave);
1263 * Show link status of the bond interface.
1265 static ssize_t bonding_show_mii_status(struct device *d,
1266 struct device_attribute *attr,
1269 struct bonding *bond = to_bond(d);
1271 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1273 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1276 * Show current 802.3ad aggregator ID.
1278 static ssize_t bonding_show_ad_aggregator(struct device *d,
1279 struct device_attribute *attr,
1283 struct bonding *bond = to_bond(d);
1285 if (bond->params.mode == BOND_MODE_8023AD) {
1286 struct ad_info ad_info;
1287 count = sprintf(buf, "%d\n",
1288 bond_3ad_get_active_agg_info(bond, &ad_info)
1289 ? 0 : ad_info.aggregator_id);
1294 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1298 * Show number of active 802.3ad ports.
1300 static ssize_t bonding_show_ad_num_ports(struct device *d,
1301 struct device_attribute *attr,
1305 struct bonding *bond = to_bond(d);
1307 if (bond->params.mode == BOND_MODE_8023AD) {
1308 struct ad_info ad_info;
1309 count = sprintf(buf, "%d\n",
1310 bond_3ad_get_active_agg_info(bond, &ad_info)
1311 ? 0 : ad_info.ports);
1316 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1320 * Show current 802.3ad actor key.
1322 static ssize_t bonding_show_ad_actor_key(struct device *d,
1323 struct device_attribute *attr,
1327 struct bonding *bond = to_bond(d);
1329 if (bond->params.mode == BOND_MODE_8023AD) {
1330 struct ad_info ad_info;
1331 count = sprintf(buf, "%d\n",
1332 bond_3ad_get_active_agg_info(bond, &ad_info)
1333 ? 0 : ad_info.actor_key);
1338 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1342 * Show current 802.3ad partner key.
1344 static ssize_t bonding_show_ad_partner_key(struct device *d,
1345 struct device_attribute *attr,
1349 struct bonding *bond = to_bond(d);
1351 if (bond->params.mode == BOND_MODE_8023AD) {
1352 struct ad_info ad_info;
1353 count = sprintf(buf, "%d\n",
1354 bond_3ad_get_active_agg_info(bond, &ad_info)
1355 ? 0 : ad_info.partner_key);
1360 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1364 * Show current 802.3ad partner mac.
1366 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1367 struct device_attribute *attr,
1371 struct bonding *bond = to_bond(d);
1373 if (bond->params.mode == BOND_MODE_8023AD) {
1374 struct ad_info ad_info;
1375 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1376 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1381 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1384 * Show the queue_ids of the slaves in the current bond.
1386 static ssize_t bonding_show_queue_id(struct device *d,
1387 struct device_attribute *attr,
1390 struct bonding *bond = to_bond(d);
1391 struct list_head *iter;
1392 struct slave *slave;
1395 if (!rtnl_trylock())
1396 return restart_syscall();
1398 bond_for_each_slave(bond, slave, iter) {
1399 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1400 /* not enough space for another interface_name:queue_id pair */
1401 if ((PAGE_SIZE - res) > 10)
1402 res = PAGE_SIZE - 10;
1403 res += sprintf(buf + res, "++more++ ");
1406 res += sprintf(buf + res, "%s:%d ",
1407 slave->dev->name, slave->queue_id);
1410 buf[res-1] = '\n'; /* eat the leftover space */
1418 * Set the queue_ids of the slaves in the current bond. The bond
1419 * interface must be enslaved for this to work.
1421 static ssize_t bonding_store_queue_id(struct device *d,
1422 struct device_attribute *attr,
1423 const char *buffer, size_t count)
1425 struct slave *slave, *update_slave;
1426 struct bonding *bond = to_bond(d);
1427 struct list_head *iter;
1431 struct net_device *sdev = NULL;
1433 if (!rtnl_trylock())
1434 return restart_syscall();
1436 /* delim will point to queue id if successful */
1437 delim = strchr(buffer, ':');
1442 * Terminate string that points to device name and bump it
1443 * up one, so we can read the queue id there.
1446 if (sscanf(++delim, "%hd\n", &qid) != 1)
1449 /* Check buffer length, valid ifname and queue id */
1450 if (strlen(buffer) > IFNAMSIZ ||
1451 !dev_valid_name(buffer) ||
1452 qid > bond->dev->real_num_tx_queues)
1455 /* Get the pointer to that interface if it exists */
1456 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1460 /* Search for thes slave and check for duplicate qids */
1461 update_slave = NULL;
1462 bond_for_each_slave(bond, slave, iter) {
1463 if (sdev == slave->dev)
1465 * We don't need to check the matching
1466 * slave for dups, since we're overwriting it
1468 update_slave = slave;
1469 else if (qid && qid == slave->queue_id) {
1477 /* Actually set the qids for the slave */
1478 update_slave->queue_id = qid;
1485 pr_info("invalid input for queue_id set for %s.\n",
1491 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1492 bonding_store_queue_id);
1496 * Show and set the all_slaves_active flag.
1498 static ssize_t bonding_show_slaves_active(struct device *d,
1499 struct device_attribute *attr,
1502 struct bonding *bond = to_bond(d);
1504 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1507 static ssize_t bonding_store_slaves_active(struct device *d,
1508 struct device_attribute *attr,
1509 const char *buf, size_t count)
1511 struct bonding *bond = to_bond(d);
1512 int new_value, ret = count;
1513 struct list_head *iter;
1514 struct slave *slave;
1516 if (!rtnl_trylock())
1517 return restart_syscall();
1519 if (sscanf(buf, "%d", &new_value) != 1) {
1520 pr_err("%s: no all_slaves_active value specified.\n",
1526 if (new_value == bond->params.all_slaves_active)
1529 if ((new_value == 0) || (new_value == 1)) {
1530 bond->params.all_slaves_active = new_value;
1532 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1533 bond->dev->name, new_value);
1538 bond_for_each_slave(bond, slave, iter) {
1539 if (!bond_is_active_slave(slave)) {
1541 slave->inactive = 0;
1543 slave->inactive = 1;
1550 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1551 bonding_show_slaves_active, bonding_store_slaves_active);
1554 * Show and set the number of IGMP membership reports to send on link failure
1556 static ssize_t bonding_show_resend_igmp(struct device *d,
1557 struct device_attribute *attr,
1560 struct bonding *bond = to_bond(d);
1562 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1565 static ssize_t bonding_store_resend_igmp(struct device *d,
1566 struct device_attribute *attr,
1567 const char *buf, size_t count)
1569 int new_value, ret = count;
1570 struct bonding *bond = to_bond(d);
1572 if (sscanf(buf, "%d", &new_value) != 1) {
1573 pr_err("%s: no resend_igmp value specified.\n",
1579 if (new_value < 0 || new_value > 255) {
1580 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1581 bond->dev->name, new_value);
1586 pr_info("%s: Setting resend_igmp to %d.\n",
1587 bond->dev->name, new_value);
1588 bond->params.resend_igmp = new_value;
1593 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1594 bonding_show_resend_igmp, bonding_store_resend_igmp);
1597 static ssize_t bonding_show_lp_interval(struct device *d,
1598 struct device_attribute *attr,
1601 struct bonding *bond = to_bond(d);
1602 return sprintf(buf, "%d\n", bond->params.lp_interval);
1605 static ssize_t bonding_store_lp_interval(struct device *d,
1606 struct device_attribute *attr,
1607 const char *buf, size_t count)
1609 struct bonding *bond = to_bond(d);
1610 int new_value, ret = count;
1612 if (sscanf(buf, "%d", &new_value) != 1) {
1613 pr_err("%s: no lp interval value specified.\n",
1619 if (new_value <= 0) {
1620 pr_err ("%s: lp_interval must be between 1 and %d\n",
1621 bond->dev->name, INT_MAX);
1626 bond->params.lp_interval = new_value;
1631 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1632 bonding_show_lp_interval, bonding_store_lp_interval);
1634 static struct attribute *per_bond_attrs[] = {
1635 &dev_attr_slaves.attr,
1636 &dev_attr_mode.attr,
1637 &dev_attr_fail_over_mac.attr,
1638 &dev_attr_arp_validate.attr,
1639 &dev_attr_arp_all_targets.attr,
1640 &dev_attr_arp_interval.attr,
1641 &dev_attr_arp_ip_target.attr,
1642 &dev_attr_downdelay.attr,
1643 &dev_attr_updelay.attr,
1644 &dev_attr_lacp_rate.attr,
1645 &dev_attr_ad_select.attr,
1646 &dev_attr_xmit_hash_policy.attr,
1647 &dev_attr_num_grat_arp.attr,
1648 &dev_attr_num_unsol_na.attr,
1649 &dev_attr_miimon.attr,
1650 &dev_attr_primary.attr,
1651 &dev_attr_primary_reselect.attr,
1652 &dev_attr_use_carrier.attr,
1653 &dev_attr_active_slave.attr,
1654 &dev_attr_mii_status.attr,
1655 &dev_attr_ad_aggregator.attr,
1656 &dev_attr_ad_num_ports.attr,
1657 &dev_attr_ad_actor_key.attr,
1658 &dev_attr_ad_partner_key.attr,
1659 &dev_attr_ad_partner_mac.attr,
1660 &dev_attr_queue_id.attr,
1661 &dev_attr_all_slaves_active.attr,
1662 &dev_attr_resend_igmp.attr,
1663 &dev_attr_min_links.attr,
1664 &dev_attr_lp_interval.attr,
1668 static struct attribute_group bonding_group = {
1670 .attrs = per_bond_attrs,
1674 * Initialize sysfs. This sets up the bonding_masters file in
1677 int bond_create_sysfs(struct bond_net *bn)
1681 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1682 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1684 ret = netdev_class_create_file_ns(&bn->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(bn->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(struct bond_net *bn)
1715 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
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;