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)
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);
363 new_value = bond_parse_parm(buf, arp_validate_tbl);
365 pr_err("%s: Ignoring invalid arp_validate value %s\n",
366 bond->dev->name, buf);
370 return restart_syscall();
372 ret = bond_option_arp_validate_set(bond, new_value);
381 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
382 bonding_store_arp_validate);
384 * Show and set arp_all_targets.
386 static ssize_t bonding_show_arp_all_targets(struct device *d,
387 struct device_attribute *attr,
390 struct bonding *bond = to_bond(d);
391 int value = bond->params.arp_all_targets;
393 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
397 static ssize_t bonding_store_arp_all_targets(struct device *d,
398 struct device_attribute *attr,
399 const char *buf, size_t count)
401 struct bonding *bond = to_bond(d);
404 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
406 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
407 bond->dev->name, buf);
412 return restart_syscall();
414 ret = bond_option_arp_all_targets_set(bond, new_value);
423 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
424 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
427 * Show and store fail_over_mac. User only allowed to change the
428 * value when there are no slaves.
430 static ssize_t bonding_show_fail_over_mac(struct device *d,
431 struct device_attribute *attr,
434 struct bonding *bond = to_bond(d);
436 return sprintf(buf, "%s %d\n",
437 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
438 bond->params.fail_over_mac);
441 static ssize_t bonding_store_fail_over_mac(struct device *d,
442 struct device_attribute *attr,
443 const char *buf, size_t count)
445 int new_value, ret = count;
446 struct bonding *bond = to_bond(d);
449 return restart_syscall();
451 if (bond_has_slaves(bond)) {
452 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
458 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
460 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
461 bond->dev->name, buf);
466 bond->params.fail_over_mac = new_value;
467 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
468 bond->dev->name, fail_over_mac_tbl[new_value].modename,
476 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
477 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
480 * Show and set the arp timer interval. There are two tricky bits
481 * here. First, if ARP monitoring is activated, then we must disable
482 * MII monitoring. Second, if the ARP timer isn't running, we must
485 static ssize_t bonding_show_arp_interval(struct device *d,
486 struct device_attribute *attr,
489 struct bonding *bond = to_bond(d);
491 return sprintf(buf, "%d\n", bond->params.arp_interval);
494 static ssize_t bonding_store_arp_interval(struct device *d,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
498 struct bonding *bond = to_bond(d);
501 if (sscanf(buf, "%d", &new_value) != 1) {
502 pr_err("%s: no arp_interval value specified.\n",
508 return restart_syscall();
510 ret = bond_option_arp_interval_set(bond, new_value);
517 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
518 bonding_show_arp_interval, bonding_store_arp_interval);
521 * Show and set the arp targets.
523 static ssize_t bonding_show_arp_targets(struct device *d,
524 struct device_attribute *attr,
528 struct bonding *bond = to_bond(d);
530 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
531 if (bond->params.arp_targets[i])
532 res += sprintf(buf + res, "%pI4 ",
533 &bond->params.arp_targets[i]);
536 buf[res-1] = '\n'; /* eat the leftover space */
540 static ssize_t bonding_store_arp_targets(struct device *d,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
544 struct bonding *bond = to_bond(d);
548 if (!in4_pton(buf + 1, -1, (u8 *)&target, -1, NULL)) {
549 pr_err("%s: invalid ARP target %pI4 specified\n",
550 bond->dev->name, &target);
555 return restart_syscall();
558 ret = bond_option_arp_ip_target_add(bond, target);
559 else if (buf[0] == '-')
560 ret = bond_option_arp_ip_target_rem(bond, target);
562 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
571 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
574 * Show and set the up and down delays. These must be multiples of the
575 * MII monitoring value, and are stored internally as the multiplier.
576 * Thus, we must translate to MS for the real world.
578 static ssize_t bonding_show_downdelay(struct device *d,
579 struct device_attribute *attr,
582 struct bonding *bond = to_bond(d);
584 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
587 static ssize_t bonding_store_downdelay(struct device *d,
588 struct device_attribute *attr,
589 const char *buf, size_t count)
592 struct bonding *bond = to_bond(d);
594 if (sscanf(buf, "%d", &new_value) != 1) {
595 pr_err("%s: no down delay value specified.\n", bond->dev->name);
600 return restart_syscall();
602 ret = bond_option_downdelay_set(bond, new_value);
609 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
610 bonding_show_downdelay, bonding_store_downdelay);
612 static ssize_t bonding_show_updelay(struct device *d,
613 struct device_attribute *attr,
616 struct bonding *bond = to_bond(d);
618 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
622 static ssize_t bonding_store_updelay(struct device *d,
623 struct device_attribute *attr,
624 const char *buf, size_t count)
627 struct bonding *bond = to_bond(d);
629 if (sscanf(buf, "%d", &new_value) != 1) {
630 pr_err("%s: no up delay value specified.\n",
636 return restart_syscall();
638 ret = bond_option_updelay_set(bond, new_value);
645 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
646 bonding_show_updelay, bonding_store_updelay);
649 * Show and set the LACP interval. Interface must be down, and the mode
650 * must be set to 802.3ad mode.
652 static ssize_t bonding_show_lacp(struct device *d,
653 struct device_attribute *attr,
656 struct bonding *bond = to_bond(d);
658 return sprintf(buf, "%s %d\n",
659 bond_lacp_tbl[bond->params.lacp_fast].modename,
660 bond->params.lacp_fast);
663 static ssize_t bonding_store_lacp(struct device *d,
664 struct device_attribute *attr,
665 const char *buf, size_t count)
667 struct bonding *bond = to_bond(d);
668 int new_value, ret = count;
671 return restart_syscall();
673 if (bond->dev->flags & IFF_UP) {
674 pr_err("%s: Unable to update LACP rate because interface is up.\n",
680 if (bond->params.mode != BOND_MODE_8023AD) {
681 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
687 new_value = bond_parse_parm(buf, bond_lacp_tbl);
689 if ((new_value == 1) || (new_value == 0)) {
690 bond->params.lacp_fast = new_value;
691 bond_3ad_update_lacp_rate(bond);
692 pr_info("%s: Setting LACP rate to %s (%d).\n",
693 bond->dev->name, bond_lacp_tbl[new_value].modename,
696 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
697 bond->dev->name, (int)strlen(buf) - 1, buf);
705 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
706 bonding_show_lacp, bonding_store_lacp);
708 static ssize_t bonding_show_min_links(struct device *d,
709 struct device_attribute *attr,
712 struct bonding *bond = to_bond(d);
714 return sprintf(buf, "%d\n", bond->params.min_links);
717 static ssize_t bonding_store_min_links(struct device *d,
718 struct device_attribute *attr,
719 const char *buf, size_t count)
721 struct bonding *bond = to_bond(d);
723 unsigned int new_value;
725 ret = kstrtouint(buf, 0, &new_value);
727 pr_err("%s: Ignoring invalid min links value %s.\n",
728 bond->dev->name, buf);
732 pr_info("%s: Setting min links value to %u\n",
733 bond->dev->name, new_value);
734 bond->params.min_links = new_value;
737 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
738 bonding_show_min_links, bonding_store_min_links);
740 static ssize_t bonding_show_ad_select(struct device *d,
741 struct device_attribute *attr,
744 struct bonding *bond = to_bond(d);
746 return sprintf(buf, "%s %d\n",
747 ad_select_tbl[bond->params.ad_select].modename,
748 bond->params.ad_select);
752 static ssize_t bonding_store_ad_select(struct device *d,
753 struct device_attribute *attr,
754 const char *buf, size_t count)
756 int new_value, ret = count;
757 struct bonding *bond = to_bond(d);
759 if (bond->dev->flags & IFF_UP) {
760 pr_err("%s: Unable to update ad_select because interface is up.\n",
766 new_value = bond_parse_parm(buf, ad_select_tbl);
768 if (new_value != -1) {
769 bond->params.ad_select = new_value;
770 pr_info("%s: Setting ad_select to %s (%d).\n",
771 bond->dev->name, ad_select_tbl[new_value].modename,
774 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
775 bond->dev->name, (int)strlen(buf) - 1, buf);
781 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
782 bonding_show_ad_select, bonding_store_ad_select);
785 * Show and set the number of peer notifications to send after a failover event.
787 static ssize_t bonding_show_num_peer_notif(struct device *d,
788 struct device_attribute *attr,
791 struct bonding *bond = to_bond(d);
792 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
795 static ssize_t bonding_store_num_peer_notif(struct device *d,
796 struct device_attribute *attr,
797 const char *buf, size_t count)
799 struct bonding *bond = to_bond(d);
800 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
801 return err ? err : count;
803 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
804 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
805 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
806 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
809 * Show and set the MII monitor interval. There are two tricky bits
810 * here. First, if MII monitoring is activated, then we must disable
811 * ARP monitoring. Second, if the timer isn't running, we must
814 static ssize_t bonding_show_miimon(struct device *d,
815 struct device_attribute *attr,
818 struct bonding *bond = to_bond(d);
820 return sprintf(buf, "%d\n", bond->params.miimon);
823 static ssize_t bonding_store_miimon(struct device *d,
824 struct device_attribute *attr,
825 const char *buf, size_t count)
828 struct bonding *bond = to_bond(d);
830 if (sscanf(buf, "%d", &new_value) != 1) {
831 pr_err("%s: no miimon value specified.\n",
837 return restart_syscall();
839 ret = bond_option_miimon_set(bond, new_value);
846 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
847 bonding_show_miimon, bonding_store_miimon);
850 * Show and set the primary slave. The store function is much
851 * simpler than bonding_store_slaves function because it only needs to
852 * handle one interface name.
853 * The bond must be a mode that supports a primary for this be
856 static ssize_t bonding_show_primary(struct device *d,
857 struct device_attribute *attr,
861 struct bonding *bond = to_bond(d);
863 if (bond->primary_slave)
864 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
869 static ssize_t bonding_store_primary(struct device *d,
870 struct device_attribute *attr,
871 const char *buf, size_t count)
873 struct bonding *bond = to_bond(d);
874 struct list_head *iter;
875 char ifname[IFNAMSIZ];
879 return restart_syscall();
881 read_lock(&bond->lock);
882 write_lock_bh(&bond->curr_slave_lock);
884 if (!USES_PRIMARY(bond->params.mode)) {
885 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
886 bond->dev->name, bond->dev->name, bond->params.mode);
890 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
892 /* check to see if we are clearing primary */
893 if (!strlen(ifname) || buf[0] == '\n') {
894 pr_info("%s: Setting primary slave to None.\n",
896 bond->primary_slave = NULL;
897 memset(bond->params.primary, 0, sizeof(bond->params.primary));
898 bond_select_active_slave(bond);
902 bond_for_each_slave(bond, slave, iter) {
903 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
904 pr_info("%s: Setting %s as primary slave.\n",
905 bond->dev->name, slave->dev->name);
906 bond->primary_slave = slave;
907 strcpy(bond->params.primary, slave->dev->name);
908 bond_select_active_slave(bond);
913 strncpy(bond->params.primary, ifname, IFNAMSIZ);
914 bond->params.primary[IFNAMSIZ - 1] = 0;
916 pr_info("%s: Recording %s as primary, "
917 "but it has not been enslaved to %s yet.\n",
918 bond->dev->name, ifname, bond->dev->name);
920 write_unlock_bh(&bond->curr_slave_lock);
921 read_unlock(&bond->lock);
922 unblock_netpoll_tx();
927 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
928 bonding_show_primary, bonding_store_primary);
931 * Show and set the primary_reselect flag.
933 static ssize_t bonding_show_primary_reselect(struct device *d,
934 struct device_attribute *attr,
937 struct bonding *bond = to_bond(d);
939 return sprintf(buf, "%s %d\n",
940 pri_reselect_tbl[bond->params.primary_reselect].modename,
941 bond->params.primary_reselect);
944 static ssize_t bonding_store_primary_reselect(struct device *d,
945 struct device_attribute *attr,
946 const char *buf, size_t count)
948 int new_value, ret = count;
949 struct bonding *bond = to_bond(d);
952 return restart_syscall();
954 new_value = bond_parse_parm(buf, pri_reselect_tbl);
956 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
958 (int) strlen(buf) - 1, buf);
963 bond->params.primary_reselect = new_value;
964 pr_info("%s: setting primary_reselect to %s (%d).\n",
965 bond->dev->name, pri_reselect_tbl[new_value].modename,
969 read_lock(&bond->lock);
970 write_lock_bh(&bond->curr_slave_lock);
971 bond_select_active_slave(bond);
972 write_unlock_bh(&bond->curr_slave_lock);
973 read_unlock(&bond->lock);
974 unblock_netpoll_tx();
979 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
980 bonding_show_primary_reselect,
981 bonding_store_primary_reselect);
984 * Show and set the use_carrier flag.
986 static ssize_t bonding_show_carrier(struct device *d,
987 struct device_attribute *attr,
990 struct bonding *bond = to_bond(d);
992 return sprintf(buf, "%d\n", bond->params.use_carrier);
995 static ssize_t bonding_store_carrier(struct device *d,
996 struct device_attribute *attr,
997 const char *buf, size_t count)
1000 struct bonding *bond = to_bond(d);
1002 if (sscanf(buf, "%d", &new_value) != 1) {
1003 pr_err("%s: no use_carrier value specified.\n",
1008 if (!rtnl_trylock())
1009 return restart_syscall();
1011 ret = bond_option_use_carrier_set(bond, new_value);
1018 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1019 bonding_show_carrier, bonding_store_carrier);
1023 * Show and set currently active_slave.
1025 static ssize_t bonding_show_active_slave(struct device *d,
1026 struct device_attribute *attr,
1029 struct bonding *bond = to_bond(d);
1030 struct net_device *slave_dev;
1034 slave_dev = bond_option_active_slave_get_rcu(bond);
1036 count = sprintf(buf, "%s\n", slave_dev->name);
1042 static ssize_t bonding_store_active_slave(struct device *d,
1043 struct device_attribute *attr,
1044 const char *buf, size_t count)
1047 struct bonding *bond = to_bond(d);
1048 char ifname[IFNAMSIZ];
1049 struct net_device *dev;
1051 if (!rtnl_trylock())
1052 return restart_syscall();
1054 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1055 if (!strlen(ifname) || buf[0] == '\n') {
1058 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1065 ret = bond_option_active_slave_set(bond, dev);
1075 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1076 bonding_show_active_slave, bonding_store_active_slave);
1080 * Show link status of the bond interface.
1082 static ssize_t bonding_show_mii_status(struct device *d,
1083 struct device_attribute *attr,
1086 struct bonding *bond = to_bond(d);
1088 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1090 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1093 * Show current 802.3ad aggregator ID.
1095 static ssize_t bonding_show_ad_aggregator(struct device *d,
1096 struct device_attribute *attr,
1100 struct bonding *bond = to_bond(d);
1102 if (bond->params.mode == BOND_MODE_8023AD) {
1103 struct ad_info ad_info;
1104 count = sprintf(buf, "%d\n",
1105 bond_3ad_get_active_agg_info(bond, &ad_info)
1106 ? 0 : ad_info.aggregator_id);
1111 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1115 * Show number of active 802.3ad ports.
1117 static ssize_t bonding_show_ad_num_ports(struct device *d,
1118 struct device_attribute *attr,
1122 struct bonding *bond = to_bond(d);
1124 if (bond->params.mode == BOND_MODE_8023AD) {
1125 struct ad_info ad_info;
1126 count = sprintf(buf, "%d\n",
1127 bond_3ad_get_active_agg_info(bond, &ad_info)
1128 ? 0 : ad_info.ports);
1133 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1137 * Show current 802.3ad actor key.
1139 static ssize_t bonding_show_ad_actor_key(struct device *d,
1140 struct device_attribute *attr,
1144 struct bonding *bond = to_bond(d);
1146 if (bond->params.mode == BOND_MODE_8023AD) {
1147 struct ad_info ad_info;
1148 count = sprintf(buf, "%d\n",
1149 bond_3ad_get_active_agg_info(bond, &ad_info)
1150 ? 0 : ad_info.actor_key);
1155 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1159 * Show current 802.3ad partner key.
1161 static ssize_t bonding_show_ad_partner_key(struct device *d,
1162 struct device_attribute *attr,
1166 struct bonding *bond = to_bond(d);
1168 if (bond->params.mode == BOND_MODE_8023AD) {
1169 struct ad_info ad_info;
1170 count = sprintf(buf, "%d\n",
1171 bond_3ad_get_active_agg_info(bond, &ad_info)
1172 ? 0 : ad_info.partner_key);
1177 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1181 * Show current 802.3ad partner mac.
1183 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1184 struct device_attribute *attr,
1188 struct bonding *bond = to_bond(d);
1190 if (bond->params.mode == BOND_MODE_8023AD) {
1191 struct ad_info ad_info;
1192 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1193 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1198 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1201 * Show the queue_ids of the slaves in the current bond.
1203 static ssize_t bonding_show_queue_id(struct device *d,
1204 struct device_attribute *attr,
1207 struct bonding *bond = to_bond(d);
1208 struct list_head *iter;
1209 struct slave *slave;
1212 if (!rtnl_trylock())
1213 return restart_syscall();
1215 bond_for_each_slave(bond, slave, iter) {
1216 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1217 /* not enough space for another interface_name:queue_id pair */
1218 if ((PAGE_SIZE - res) > 10)
1219 res = PAGE_SIZE - 10;
1220 res += sprintf(buf + res, "++more++ ");
1223 res += sprintf(buf + res, "%s:%d ",
1224 slave->dev->name, slave->queue_id);
1227 buf[res-1] = '\n'; /* eat the leftover space */
1235 * Set the queue_ids of the slaves in the current bond. The bond
1236 * interface must be enslaved for this to work.
1238 static ssize_t bonding_store_queue_id(struct device *d,
1239 struct device_attribute *attr,
1240 const char *buffer, size_t count)
1242 struct slave *slave, *update_slave;
1243 struct bonding *bond = to_bond(d);
1244 struct list_head *iter;
1248 struct net_device *sdev = NULL;
1250 if (!rtnl_trylock())
1251 return restart_syscall();
1253 /* delim will point to queue id if successful */
1254 delim = strchr(buffer, ':');
1259 * Terminate string that points to device name and bump it
1260 * up one, so we can read the queue id there.
1263 if (sscanf(++delim, "%hd\n", &qid) != 1)
1266 /* Check buffer length, valid ifname and queue id */
1267 if (strlen(buffer) > IFNAMSIZ ||
1268 !dev_valid_name(buffer) ||
1269 qid > bond->dev->real_num_tx_queues)
1272 /* Get the pointer to that interface if it exists */
1273 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1277 /* Search for thes slave and check for duplicate qids */
1278 update_slave = NULL;
1279 bond_for_each_slave(bond, slave, iter) {
1280 if (sdev == slave->dev)
1282 * We don't need to check the matching
1283 * slave for dups, since we're overwriting it
1285 update_slave = slave;
1286 else if (qid && qid == slave->queue_id) {
1294 /* Actually set the qids for the slave */
1295 update_slave->queue_id = qid;
1302 pr_info("invalid input for queue_id set for %s.\n",
1308 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1309 bonding_store_queue_id);
1313 * Show and set the all_slaves_active flag.
1315 static ssize_t bonding_show_slaves_active(struct device *d,
1316 struct device_attribute *attr,
1319 struct bonding *bond = to_bond(d);
1321 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1324 static ssize_t bonding_store_slaves_active(struct device *d,
1325 struct device_attribute *attr,
1326 const char *buf, size_t count)
1328 struct bonding *bond = to_bond(d);
1329 int new_value, ret = count;
1330 struct list_head *iter;
1331 struct slave *slave;
1333 if (!rtnl_trylock())
1334 return restart_syscall();
1336 if (sscanf(buf, "%d", &new_value) != 1) {
1337 pr_err("%s: no all_slaves_active value specified.\n",
1343 if (new_value == bond->params.all_slaves_active)
1346 if ((new_value == 0) || (new_value == 1)) {
1347 bond->params.all_slaves_active = new_value;
1349 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1350 bond->dev->name, new_value);
1355 bond_for_each_slave(bond, slave, iter) {
1356 if (!bond_is_active_slave(slave)) {
1358 slave->inactive = 0;
1360 slave->inactive = 1;
1367 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1368 bonding_show_slaves_active, bonding_store_slaves_active);
1371 * Show and set the number of IGMP membership reports to send on link failure
1373 static ssize_t bonding_show_resend_igmp(struct device *d,
1374 struct device_attribute *attr,
1377 struct bonding *bond = to_bond(d);
1379 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1382 static ssize_t bonding_store_resend_igmp(struct device *d,
1383 struct device_attribute *attr,
1384 const char *buf, size_t count)
1386 int new_value, ret = count;
1387 struct bonding *bond = to_bond(d);
1389 if (sscanf(buf, "%d", &new_value) != 1) {
1390 pr_err("%s: no resend_igmp value specified.\n",
1396 if (new_value < 0 || new_value > 255) {
1397 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1398 bond->dev->name, new_value);
1403 pr_info("%s: Setting resend_igmp to %d.\n",
1404 bond->dev->name, new_value);
1405 bond->params.resend_igmp = new_value;
1410 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1411 bonding_show_resend_igmp, bonding_store_resend_igmp);
1414 static ssize_t bonding_show_lp_interval(struct device *d,
1415 struct device_attribute *attr,
1418 struct bonding *bond = to_bond(d);
1419 return sprintf(buf, "%d\n", bond->params.lp_interval);
1422 static ssize_t bonding_store_lp_interval(struct device *d,
1423 struct device_attribute *attr,
1424 const char *buf, size_t count)
1426 struct bonding *bond = to_bond(d);
1427 int new_value, ret = count;
1429 if (sscanf(buf, "%d", &new_value) != 1) {
1430 pr_err("%s: no lp interval value specified.\n",
1436 if (new_value <= 0) {
1437 pr_err ("%s: lp_interval must be between 1 and %d\n",
1438 bond->dev->name, INT_MAX);
1443 bond->params.lp_interval = new_value;
1448 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1449 bonding_show_lp_interval, bonding_store_lp_interval);
1451 static ssize_t bonding_show_packets_per_slave(struct device *d,
1452 struct device_attribute *attr,
1455 struct bonding *bond = to_bond(d);
1456 unsigned int packets_per_slave = bond->params.packets_per_slave;
1458 if (packets_per_slave > 1)
1459 packets_per_slave = reciprocal_value(packets_per_slave);
1461 return sprintf(buf, "%u\n", packets_per_slave);
1464 static ssize_t bonding_store_packets_per_slave(struct device *d,
1465 struct device_attribute *attr,
1466 const char *buf, size_t count)
1468 struct bonding *bond = to_bond(d);
1469 int new_value, ret = count;
1471 if (sscanf(buf, "%d", &new_value) != 1) {
1472 pr_err("%s: no packets_per_slave value specified.\n",
1477 if (new_value < 0 || new_value > USHRT_MAX) {
1478 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1479 bond->dev->name, USHRT_MAX);
1483 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1484 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1487 bond->params.packets_per_slave = reciprocal_value(new_value);
1489 bond->params.packets_per_slave = new_value;
1494 static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1495 bonding_show_packets_per_slave,
1496 bonding_store_packets_per_slave);
1498 static struct attribute *per_bond_attrs[] = {
1499 &dev_attr_slaves.attr,
1500 &dev_attr_mode.attr,
1501 &dev_attr_fail_over_mac.attr,
1502 &dev_attr_arp_validate.attr,
1503 &dev_attr_arp_all_targets.attr,
1504 &dev_attr_arp_interval.attr,
1505 &dev_attr_arp_ip_target.attr,
1506 &dev_attr_downdelay.attr,
1507 &dev_attr_updelay.attr,
1508 &dev_attr_lacp_rate.attr,
1509 &dev_attr_ad_select.attr,
1510 &dev_attr_xmit_hash_policy.attr,
1511 &dev_attr_num_grat_arp.attr,
1512 &dev_attr_num_unsol_na.attr,
1513 &dev_attr_miimon.attr,
1514 &dev_attr_primary.attr,
1515 &dev_attr_primary_reselect.attr,
1516 &dev_attr_use_carrier.attr,
1517 &dev_attr_active_slave.attr,
1518 &dev_attr_mii_status.attr,
1519 &dev_attr_ad_aggregator.attr,
1520 &dev_attr_ad_num_ports.attr,
1521 &dev_attr_ad_actor_key.attr,
1522 &dev_attr_ad_partner_key.attr,
1523 &dev_attr_ad_partner_mac.attr,
1524 &dev_attr_queue_id.attr,
1525 &dev_attr_all_slaves_active.attr,
1526 &dev_attr_resend_igmp.attr,
1527 &dev_attr_min_links.attr,
1528 &dev_attr_lp_interval.attr,
1529 &dev_attr_packets_per_slave.attr,
1533 static struct attribute_group bonding_group = {
1535 .attrs = per_bond_attrs,
1539 * Initialize sysfs. This sets up the bonding_masters file in
1542 int bond_create_sysfs(struct bond_net *bn)
1546 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1547 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1549 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1552 * Permit multiple loads of the module by ignoring failures to
1553 * create the bonding_masters sysfs file. Bonding devices
1554 * created by second or subsequent loads of the module will
1555 * not be listed in, or controllable by, bonding_masters, but
1556 * will have the usual "bonding" sysfs directory.
1558 * This is done to preserve backwards compatibility for
1559 * initscripts/sysconfig, which load bonding multiple times to
1560 * configure multiple bonding devices.
1562 if (ret == -EEXIST) {
1563 /* Is someone being kinky and naming a device bonding_master? */
1564 if (__dev_get_by_name(bn->net,
1565 class_attr_bonding_masters.attr.name))
1566 pr_err("network device named %s already exists in sysfs",
1567 class_attr_bonding_masters.attr.name);
1576 * Remove /sys/class/net/bonding_masters.
1578 void bond_destroy_sysfs(struct bond_net *bn)
1580 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
1584 * Initialize sysfs for each bond. This sets up and registers
1585 * the 'bondctl' directory for each individual bond under /sys/class/net.
1587 void bond_prepare_sysfs_group(struct bonding *bond)
1589 bond->dev->sysfs_groups[0] = &bonding_group;