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, see <http://www.gnu.org/licenses/>.
17 * The full GNU General Public License is included in this distribution in the
18 * file called LICENSE.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/sched.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
34 #include <linux/sysfs.h>
35 #include <linux/ctype.h>
36 #include <linux/inet.h>
37 #include <linux/rtnetlink.h>
38 #include <linux/etherdevice.h>
39 #include <net/net_namespace.h>
40 #include <net/netns/generic.h>
41 #include <linux/nsproxy.h>
42 #include <linux/reciprocal_div.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)
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);
333 return restart_syscall();
335 ret = bond_option_xmit_hash_policy_set(bond, new_value);
342 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
343 bonding_show_xmit_hash, bonding_store_xmit_hash);
346 * Show and set arp_validate.
348 static ssize_t bonding_show_arp_validate(struct device *d,
349 struct device_attribute *attr,
352 struct bonding *bond = to_bond(d);
354 return sprintf(buf, "%s %d\n",
355 arp_validate_tbl[bond->params.arp_validate].modename,
356 bond->params.arp_validate);
359 static ssize_t bonding_store_arp_validate(struct device *d,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
363 struct bonding *bond = to_bond(d);
366 new_value = bond_parse_parm(buf, arp_validate_tbl);
368 pr_err("%s: Ignoring invalid arp_validate value %s\n",
369 bond->dev->name, buf);
373 return restart_syscall();
375 ret = bond_option_arp_validate_set(bond, new_value);
384 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
385 bonding_store_arp_validate);
387 * Show and set arp_all_targets.
389 static ssize_t bonding_show_arp_all_targets(struct device *d,
390 struct device_attribute *attr,
393 struct bonding *bond = to_bond(d);
394 int value = bond->params.arp_all_targets;
396 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
400 static ssize_t bonding_store_arp_all_targets(struct device *d,
401 struct device_attribute *attr,
402 const char *buf, size_t count)
404 struct bonding *bond = to_bond(d);
407 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
409 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
410 bond->dev->name, buf);
415 return restart_syscall();
417 ret = bond_option_arp_all_targets_set(bond, new_value);
426 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
427 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
430 * Show and store fail_over_mac. User only allowed to change the
431 * value when there are no slaves.
433 static ssize_t bonding_show_fail_over_mac(struct device *d,
434 struct device_attribute *attr,
437 struct bonding *bond = to_bond(d);
439 return sprintf(buf, "%s %d\n",
440 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
441 bond->params.fail_over_mac);
444 static ssize_t bonding_store_fail_over_mac(struct device *d,
445 struct device_attribute *attr,
446 const char *buf, size_t count)
449 struct bonding *bond = to_bond(d);
451 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
453 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
454 bond->dev->name, buf);
459 return restart_syscall();
461 ret = bond_option_fail_over_mac_set(bond, new_value);
469 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
470 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
473 * Show and set the arp timer interval. There are two tricky bits
474 * here. First, if ARP monitoring is activated, then we must disable
475 * MII monitoring. Second, if the ARP timer isn't running, we must
478 static ssize_t bonding_show_arp_interval(struct device *d,
479 struct device_attribute *attr,
482 struct bonding *bond = to_bond(d);
484 return sprintf(buf, "%d\n", bond->params.arp_interval);
487 static ssize_t bonding_store_arp_interval(struct device *d,
488 struct device_attribute *attr,
489 const char *buf, size_t count)
491 struct bonding *bond = to_bond(d);
494 if (sscanf(buf, "%d", &new_value) != 1) {
495 pr_err("%s: no arp_interval value specified.\n",
501 return restart_syscall();
503 ret = bond_option_arp_interval_set(bond, new_value);
510 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
511 bonding_show_arp_interval, bonding_store_arp_interval);
514 * Show and set the arp targets.
516 static ssize_t bonding_show_arp_targets(struct device *d,
517 struct device_attribute *attr,
521 struct bonding *bond = to_bond(d);
523 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
524 if (bond->params.arp_targets[i])
525 res += sprintf(buf + res, "%pI4 ",
526 &bond->params.arp_targets[i]);
529 buf[res-1] = '\n'; /* eat the leftover space */
533 static ssize_t bonding_store_arp_targets(struct device *d,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
537 struct bonding *bond = to_bond(d);
541 if (!in4_pton(buf + 1, -1, (u8 *)&target, -1, NULL)) {
542 pr_err("%s: invalid ARP target %pI4 specified\n",
543 bond->dev->name, &target);
548 return restart_syscall();
551 ret = bond_option_arp_ip_target_add(bond, target);
552 else if (buf[0] == '-')
553 ret = bond_option_arp_ip_target_rem(bond, target);
555 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
564 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
567 * Show and set the up and down delays. These must be multiples of the
568 * MII monitoring value, and are stored internally as the multiplier.
569 * Thus, we must translate to MS for the real world.
571 static ssize_t bonding_show_downdelay(struct device *d,
572 struct device_attribute *attr,
575 struct bonding *bond = to_bond(d);
577 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
580 static ssize_t bonding_store_downdelay(struct device *d,
581 struct device_attribute *attr,
582 const char *buf, size_t count)
585 struct bonding *bond = to_bond(d);
587 if (sscanf(buf, "%d", &new_value) != 1) {
588 pr_err("%s: no down delay value specified.\n", bond->dev->name);
593 return restart_syscall();
595 ret = bond_option_downdelay_set(bond, new_value);
602 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
603 bonding_show_downdelay, bonding_store_downdelay);
605 static ssize_t bonding_show_updelay(struct device *d,
606 struct device_attribute *attr,
609 struct bonding *bond = to_bond(d);
611 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
615 static ssize_t bonding_store_updelay(struct device *d,
616 struct device_attribute *attr,
617 const char *buf, size_t count)
620 struct bonding *bond = to_bond(d);
622 if (sscanf(buf, "%d", &new_value) != 1) {
623 pr_err("%s: no up delay value specified.\n",
629 return restart_syscall();
631 ret = bond_option_updelay_set(bond, new_value);
638 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
639 bonding_show_updelay, bonding_store_updelay);
642 * Show and set the LACP interval. Interface must be down, and the mode
643 * must be set to 802.3ad mode.
645 static ssize_t bonding_show_lacp(struct device *d,
646 struct device_attribute *attr,
649 struct bonding *bond = to_bond(d);
651 return sprintf(buf, "%s %d\n",
652 bond_lacp_tbl[bond->params.lacp_fast].modename,
653 bond->params.lacp_fast);
656 static ssize_t bonding_store_lacp(struct device *d,
657 struct device_attribute *attr,
658 const char *buf, size_t count)
660 struct bonding *bond = to_bond(d);
661 int new_value, ret = count;
664 return restart_syscall();
666 if (bond->dev->flags & IFF_UP) {
667 pr_err("%s: Unable to update LACP rate because interface is up.\n",
673 if (bond->params.mode != BOND_MODE_8023AD) {
674 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
680 new_value = bond_parse_parm(buf, bond_lacp_tbl);
682 if ((new_value == 1) || (new_value == 0)) {
683 bond->params.lacp_fast = new_value;
684 bond_3ad_update_lacp_rate(bond);
685 pr_info("%s: Setting LACP rate to %s (%d).\n",
686 bond->dev->name, bond_lacp_tbl[new_value].modename,
689 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
690 bond->dev->name, (int)strlen(buf) - 1, buf);
698 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
699 bonding_show_lacp, bonding_store_lacp);
701 static ssize_t bonding_show_min_links(struct device *d,
702 struct device_attribute *attr,
705 struct bonding *bond = to_bond(d);
707 return sprintf(buf, "%d\n", bond->params.min_links);
710 static ssize_t bonding_store_min_links(struct device *d,
711 struct device_attribute *attr,
712 const char *buf, size_t count)
714 struct bonding *bond = to_bond(d);
716 unsigned int new_value;
718 ret = kstrtouint(buf, 0, &new_value);
720 pr_err("%s: Ignoring invalid min links value %s.\n",
721 bond->dev->name, buf);
725 pr_info("%s: Setting min links value to %u\n",
726 bond->dev->name, new_value);
727 bond->params.min_links = new_value;
730 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
731 bonding_show_min_links, bonding_store_min_links);
733 static ssize_t bonding_show_ad_select(struct device *d,
734 struct device_attribute *attr,
737 struct bonding *bond = to_bond(d);
739 return sprintf(buf, "%s %d\n",
740 ad_select_tbl[bond->params.ad_select].modename,
741 bond->params.ad_select);
745 static ssize_t bonding_store_ad_select(struct device *d,
746 struct device_attribute *attr,
747 const char *buf, size_t count)
749 int new_value, ret = count;
750 struct bonding *bond = to_bond(d);
752 if (bond->dev->flags & IFF_UP) {
753 pr_err("%s: Unable to update ad_select because interface is up.\n",
759 new_value = bond_parse_parm(buf, ad_select_tbl);
761 if (new_value != -1) {
762 bond->params.ad_select = new_value;
763 pr_info("%s: Setting ad_select to %s (%d).\n",
764 bond->dev->name, ad_select_tbl[new_value].modename,
767 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
768 bond->dev->name, (int)strlen(buf) - 1, buf);
774 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
775 bonding_show_ad_select, bonding_store_ad_select);
778 * Show and set the number of peer notifications to send after a failover event.
780 static ssize_t bonding_show_num_peer_notif(struct device *d,
781 struct device_attribute *attr,
784 struct bonding *bond = to_bond(d);
785 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
788 static ssize_t bonding_store_num_peer_notif(struct device *d,
789 struct device_attribute *attr,
790 const char *buf, size_t count)
792 struct bonding *bond = to_bond(d);
796 ret = kstrtou8(buf, 10, &new_value);
798 pr_err("%s: invalid value %s specified.\n",
799 bond->dev->name, buf);
804 return restart_syscall();
806 ret = bond_option_num_peer_notif_set(bond, new_value);
813 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
814 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
815 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
816 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
819 * Show and set the MII monitor interval. There are two tricky bits
820 * here. First, if MII monitoring is activated, then we must disable
821 * ARP monitoring. Second, if the timer isn't running, we must
824 static ssize_t bonding_show_miimon(struct device *d,
825 struct device_attribute *attr,
828 struct bonding *bond = to_bond(d);
830 return sprintf(buf, "%d\n", bond->params.miimon);
833 static ssize_t bonding_store_miimon(struct device *d,
834 struct device_attribute *attr,
835 const char *buf, size_t count)
838 struct bonding *bond = to_bond(d);
840 if (sscanf(buf, "%d", &new_value) != 1) {
841 pr_err("%s: no miimon value specified.\n",
847 return restart_syscall();
849 ret = bond_option_miimon_set(bond, new_value);
856 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
857 bonding_show_miimon, bonding_store_miimon);
860 * Show and set the primary slave. The store function is much
861 * simpler than bonding_store_slaves function because it only needs to
862 * handle one interface name.
863 * The bond must be a mode that supports a primary for this be
866 static ssize_t bonding_show_primary(struct device *d,
867 struct device_attribute *attr,
871 struct bonding *bond = to_bond(d);
873 if (bond->primary_slave)
874 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
879 static ssize_t bonding_store_primary(struct device *d,
880 struct device_attribute *attr,
881 const char *buf, size_t count)
883 struct bonding *bond = to_bond(d);
884 char ifname[IFNAMSIZ];
887 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
888 if (ifname[0] == '\n')
892 return restart_syscall();
894 ret = bond_option_primary_set(bond, ifname);
901 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
902 bonding_show_primary, bonding_store_primary);
905 * Show and set the primary_reselect flag.
907 static ssize_t bonding_show_primary_reselect(struct device *d,
908 struct device_attribute *attr,
911 struct bonding *bond = to_bond(d);
913 return sprintf(buf, "%s %d\n",
914 pri_reselect_tbl[bond->params.primary_reselect].modename,
915 bond->params.primary_reselect);
918 static ssize_t bonding_store_primary_reselect(struct device *d,
919 struct device_attribute *attr,
920 const char *buf, size_t count)
923 struct bonding *bond = to_bond(d);
925 new_value = bond_parse_parm(buf, pri_reselect_tbl);
927 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
929 (int) strlen(buf) - 1, buf);
934 return restart_syscall();
936 ret = bond_option_primary_reselect_set(bond, new_value);
943 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
944 bonding_show_primary_reselect,
945 bonding_store_primary_reselect);
948 * Show and set the use_carrier flag.
950 static ssize_t bonding_show_carrier(struct device *d,
951 struct device_attribute *attr,
954 struct bonding *bond = to_bond(d);
956 return sprintf(buf, "%d\n", bond->params.use_carrier);
959 static ssize_t bonding_store_carrier(struct device *d,
960 struct device_attribute *attr,
961 const char *buf, size_t count)
964 struct bonding *bond = to_bond(d);
966 if (sscanf(buf, "%d", &new_value) != 1) {
967 pr_err("%s: no use_carrier value specified.\n",
973 return restart_syscall();
975 ret = bond_option_use_carrier_set(bond, new_value);
982 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
983 bonding_show_carrier, bonding_store_carrier);
987 * Show and set currently active_slave.
989 static ssize_t bonding_show_active_slave(struct device *d,
990 struct device_attribute *attr,
993 struct bonding *bond = to_bond(d);
994 struct net_device *slave_dev;
998 slave_dev = bond_option_active_slave_get_rcu(bond);
1000 count = sprintf(buf, "%s\n", slave_dev->name);
1006 static ssize_t bonding_store_active_slave(struct device *d,
1007 struct device_attribute *attr,
1008 const char *buf, size_t count)
1011 struct bonding *bond = to_bond(d);
1012 char ifname[IFNAMSIZ];
1013 struct net_device *dev;
1015 if (!rtnl_trylock())
1016 return restart_syscall();
1018 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1019 if (!strlen(ifname) || buf[0] == '\n') {
1022 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1029 ret = bond_option_active_slave_set(bond, dev);
1039 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1040 bonding_show_active_slave, bonding_store_active_slave);
1044 * Show link status of the bond interface.
1046 static ssize_t bonding_show_mii_status(struct device *d,
1047 struct device_attribute *attr,
1050 struct bonding *bond = to_bond(d);
1052 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1054 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1057 * Show current 802.3ad aggregator ID.
1059 static ssize_t bonding_show_ad_aggregator(struct device *d,
1060 struct device_attribute *attr,
1064 struct bonding *bond = to_bond(d);
1066 if (bond->params.mode == BOND_MODE_8023AD) {
1067 struct ad_info ad_info;
1068 count = sprintf(buf, "%d\n",
1069 bond_3ad_get_active_agg_info(bond, &ad_info)
1070 ? 0 : ad_info.aggregator_id);
1075 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1079 * Show number of active 802.3ad ports.
1081 static ssize_t bonding_show_ad_num_ports(struct device *d,
1082 struct device_attribute *attr,
1086 struct bonding *bond = to_bond(d);
1088 if (bond->params.mode == BOND_MODE_8023AD) {
1089 struct ad_info ad_info;
1090 count = sprintf(buf, "%d\n",
1091 bond_3ad_get_active_agg_info(bond, &ad_info)
1092 ? 0 : ad_info.ports);
1097 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1101 * Show current 802.3ad actor key.
1103 static ssize_t bonding_show_ad_actor_key(struct device *d,
1104 struct device_attribute *attr,
1108 struct bonding *bond = to_bond(d);
1110 if (bond->params.mode == BOND_MODE_8023AD) {
1111 struct ad_info ad_info;
1112 count = sprintf(buf, "%d\n",
1113 bond_3ad_get_active_agg_info(bond, &ad_info)
1114 ? 0 : ad_info.actor_key);
1119 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1123 * Show current 802.3ad partner key.
1125 static ssize_t bonding_show_ad_partner_key(struct device *d,
1126 struct device_attribute *attr,
1130 struct bonding *bond = to_bond(d);
1132 if (bond->params.mode == BOND_MODE_8023AD) {
1133 struct ad_info ad_info;
1134 count = sprintf(buf, "%d\n",
1135 bond_3ad_get_active_agg_info(bond, &ad_info)
1136 ? 0 : ad_info.partner_key);
1141 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1145 * Show current 802.3ad partner mac.
1147 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1148 struct device_attribute *attr,
1152 struct bonding *bond = to_bond(d);
1154 if (bond->params.mode == BOND_MODE_8023AD) {
1155 struct ad_info ad_info;
1156 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1157 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1162 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1165 * Show the queue_ids of the slaves in the current bond.
1167 static ssize_t bonding_show_queue_id(struct device *d,
1168 struct device_attribute *attr,
1171 struct bonding *bond = to_bond(d);
1172 struct list_head *iter;
1173 struct slave *slave;
1176 if (!rtnl_trylock())
1177 return restart_syscall();
1179 bond_for_each_slave(bond, slave, iter) {
1180 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1181 /* not enough space for another interface_name:queue_id pair */
1182 if ((PAGE_SIZE - res) > 10)
1183 res = PAGE_SIZE - 10;
1184 res += sprintf(buf + res, "++more++ ");
1187 res += sprintf(buf + res, "%s:%d ",
1188 slave->dev->name, slave->queue_id);
1191 buf[res-1] = '\n'; /* eat the leftover space */
1199 * Set the queue_ids of the slaves in the current bond. The bond
1200 * interface must be enslaved for this to work.
1202 static ssize_t bonding_store_queue_id(struct device *d,
1203 struct device_attribute *attr,
1204 const char *buffer, size_t count)
1206 struct slave *slave, *update_slave;
1207 struct bonding *bond = to_bond(d);
1208 struct list_head *iter;
1212 struct net_device *sdev = NULL;
1214 if (!rtnl_trylock())
1215 return restart_syscall();
1217 /* delim will point to queue id if successful */
1218 delim = strchr(buffer, ':');
1223 * Terminate string that points to device name and bump it
1224 * up one, so we can read the queue id there.
1227 if (sscanf(++delim, "%hd\n", &qid) != 1)
1230 /* Check buffer length, valid ifname and queue id */
1231 if (strlen(buffer) > IFNAMSIZ ||
1232 !dev_valid_name(buffer) ||
1233 qid > bond->dev->real_num_tx_queues)
1236 /* Get the pointer to that interface if it exists */
1237 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1241 /* Search for thes slave and check for duplicate qids */
1242 update_slave = NULL;
1243 bond_for_each_slave(bond, slave, iter) {
1244 if (sdev == slave->dev)
1246 * We don't need to check the matching
1247 * slave for dups, since we're overwriting it
1249 update_slave = slave;
1250 else if (qid && qid == slave->queue_id) {
1258 /* Actually set the qids for the slave */
1259 update_slave->queue_id = qid;
1266 pr_info("invalid input for queue_id set for %s.\n",
1272 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1273 bonding_store_queue_id);
1277 * Show and set the all_slaves_active flag.
1279 static ssize_t bonding_show_slaves_active(struct device *d,
1280 struct device_attribute *attr,
1283 struct bonding *bond = to_bond(d);
1285 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1288 static ssize_t bonding_store_slaves_active(struct device *d,
1289 struct device_attribute *attr,
1290 const char *buf, size_t count)
1292 struct bonding *bond = to_bond(d);
1293 int new_value, ret = count;
1294 struct list_head *iter;
1295 struct slave *slave;
1297 if (!rtnl_trylock())
1298 return restart_syscall();
1300 if (sscanf(buf, "%d", &new_value) != 1) {
1301 pr_err("%s: no all_slaves_active value specified.\n",
1307 if (new_value == bond->params.all_slaves_active)
1310 if ((new_value == 0) || (new_value == 1)) {
1311 bond->params.all_slaves_active = new_value;
1313 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1314 bond->dev->name, new_value);
1319 bond_for_each_slave(bond, slave, iter) {
1320 if (!bond_is_active_slave(slave)) {
1322 slave->inactive = 0;
1324 slave->inactive = 1;
1331 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1332 bonding_show_slaves_active, bonding_store_slaves_active);
1335 * Show and set the number of IGMP membership reports to send on link failure
1337 static ssize_t bonding_show_resend_igmp(struct device *d,
1338 struct device_attribute *attr,
1341 struct bonding *bond = to_bond(d);
1343 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1346 static ssize_t bonding_store_resend_igmp(struct device *d,
1347 struct device_attribute *attr,
1348 const char *buf, size_t count)
1350 int new_value, ret = count;
1351 struct bonding *bond = to_bond(d);
1353 if (sscanf(buf, "%d", &new_value) != 1) {
1354 pr_err("%s: no resend_igmp value specified.\n",
1359 if (!rtnl_trylock())
1360 return restart_syscall();
1362 ret = bond_option_resend_igmp_set(bond, new_value);
1370 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1371 bonding_show_resend_igmp, bonding_store_resend_igmp);
1374 static ssize_t bonding_show_lp_interval(struct device *d,
1375 struct device_attribute *attr,
1378 struct bonding *bond = to_bond(d);
1379 return sprintf(buf, "%d\n", bond->params.lp_interval);
1382 static ssize_t bonding_store_lp_interval(struct device *d,
1383 struct device_attribute *attr,
1384 const char *buf, size_t count)
1386 struct bonding *bond = to_bond(d);
1387 int new_value, ret = count;
1389 if (sscanf(buf, "%d", &new_value) != 1) {
1390 pr_err("%s: no lp interval value specified.\n",
1396 if (new_value <= 0) {
1397 pr_err ("%s: lp_interval must be between 1 and %d\n",
1398 bond->dev->name, INT_MAX);
1403 bond->params.lp_interval = new_value;
1408 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1409 bonding_show_lp_interval, bonding_store_lp_interval);
1411 static ssize_t bonding_show_packets_per_slave(struct device *d,
1412 struct device_attribute *attr,
1415 struct bonding *bond = to_bond(d);
1416 unsigned int packets_per_slave = bond->params.packets_per_slave;
1418 if (packets_per_slave > 1)
1419 packets_per_slave = reciprocal_value(packets_per_slave);
1421 return sprintf(buf, "%u\n", packets_per_slave);
1424 static ssize_t bonding_store_packets_per_slave(struct device *d,
1425 struct device_attribute *attr,
1426 const char *buf, size_t count)
1428 struct bonding *bond = to_bond(d);
1429 int new_value, ret = count;
1431 if (sscanf(buf, "%d", &new_value) != 1) {
1432 pr_err("%s: no packets_per_slave value specified.\n",
1437 if (new_value < 0 || new_value > USHRT_MAX) {
1438 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1439 bond->dev->name, USHRT_MAX);
1443 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1444 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1447 bond->params.packets_per_slave = reciprocal_value(new_value);
1449 bond->params.packets_per_slave = new_value;
1454 static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1455 bonding_show_packets_per_slave,
1456 bonding_store_packets_per_slave);
1458 static struct attribute *per_bond_attrs[] = {
1459 &dev_attr_slaves.attr,
1460 &dev_attr_mode.attr,
1461 &dev_attr_fail_over_mac.attr,
1462 &dev_attr_arp_validate.attr,
1463 &dev_attr_arp_all_targets.attr,
1464 &dev_attr_arp_interval.attr,
1465 &dev_attr_arp_ip_target.attr,
1466 &dev_attr_downdelay.attr,
1467 &dev_attr_updelay.attr,
1468 &dev_attr_lacp_rate.attr,
1469 &dev_attr_ad_select.attr,
1470 &dev_attr_xmit_hash_policy.attr,
1471 &dev_attr_num_grat_arp.attr,
1472 &dev_attr_num_unsol_na.attr,
1473 &dev_attr_miimon.attr,
1474 &dev_attr_primary.attr,
1475 &dev_attr_primary_reselect.attr,
1476 &dev_attr_use_carrier.attr,
1477 &dev_attr_active_slave.attr,
1478 &dev_attr_mii_status.attr,
1479 &dev_attr_ad_aggregator.attr,
1480 &dev_attr_ad_num_ports.attr,
1481 &dev_attr_ad_actor_key.attr,
1482 &dev_attr_ad_partner_key.attr,
1483 &dev_attr_ad_partner_mac.attr,
1484 &dev_attr_queue_id.attr,
1485 &dev_attr_all_slaves_active.attr,
1486 &dev_attr_resend_igmp.attr,
1487 &dev_attr_min_links.attr,
1488 &dev_attr_lp_interval.attr,
1489 &dev_attr_packets_per_slave.attr,
1493 static struct attribute_group bonding_group = {
1495 .attrs = per_bond_attrs,
1499 * Initialize sysfs. This sets up the bonding_masters file in
1502 int bond_create_sysfs(struct bond_net *bn)
1506 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1507 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1509 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1512 * Permit multiple loads of the module by ignoring failures to
1513 * create the bonding_masters sysfs file. Bonding devices
1514 * created by second or subsequent loads of the module will
1515 * not be listed in, or controllable by, bonding_masters, but
1516 * will have the usual "bonding" sysfs directory.
1518 * This is done to preserve backwards compatibility for
1519 * initscripts/sysconfig, which load bonding multiple times to
1520 * configure multiple bonding devices.
1522 if (ret == -EEXIST) {
1523 /* Is someone being kinky and naming a device bonding_master? */
1524 if (__dev_get_by_name(bn->net,
1525 class_attr_bonding_masters.attr.name))
1526 pr_err("network device named %s already exists in sysfs",
1527 class_attr_bonding_masters.attr.name);
1536 * Remove /sys/class/net/bonding_masters.
1538 void bond_destroy_sysfs(struct bond_net *bn)
1540 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
1544 * Initialize sysfs for each bond. This sets up and registers
1545 * the 'bondctl' directory for each individual bond under /sys/class/net.
1547 void bond_prepare_sysfs_group(struct bonding *bond)
1549 bond->dev->sysfs_groups[0] = &bonding_group;