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>
45 #define to_dev(obj) container_of(obj, struct device, kobj)
46 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
49 * "show" function for the bond_masters attribute.
50 * The class parameter is ignored.
52 static ssize_t bonding_show_bonds(struct class *cls,
53 struct class_attribute *attr,
57 container_of(attr, struct bond_net, class_attr_bonding_masters);
63 list_for_each_entry(bond, &bn->dev_list, bond_list) {
64 if (res > (PAGE_SIZE - IFNAMSIZ)) {
65 /* not enough space for another interface name */
66 if ((PAGE_SIZE - res) > 10)
68 res += sprintf(buf + res, "++more++ ");
71 res += sprintf(buf + res, "%s ", bond->dev->name);
74 buf[res-1] = '\n'; /* eat the leftover space */
80 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
84 list_for_each_entry(bond, &bn->dev_list, bond_list) {
85 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
92 * "store" function for the bond_masters attribute. This is what
93 * creates and deletes entire bonds.
95 * The class parameter is ignored.
99 static ssize_t bonding_store_bonds(struct class *cls,
100 struct class_attribute *attr,
101 const char *buffer, size_t count)
103 struct bond_net *bn =
104 container_of(attr, struct bond_net, class_attr_bonding_masters);
105 char command[IFNAMSIZ + 1] = {0, };
109 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
110 ifname = command + 1;
111 if ((strlen(command) <= 1) ||
112 !dev_valid_name(ifname))
115 if (command[0] == '+') {
116 pr_info("%s is being created...\n", ifname);
117 rv = bond_create(bn->net, ifname);
120 pr_info("%s already exists.\n", ifname);
122 pr_info("%s creation failed.\n", ifname);
125 } else if (command[0] == '-') {
126 struct net_device *bond_dev;
129 bond_dev = bond_get_by_name(bn, ifname);
131 pr_info("%s is being deleted...\n", ifname);
132 unregister_netdevice(bond_dev);
134 pr_err("unable to delete non-existent %s\n", ifname);
141 /* Always return either count or an error. If you return 0, you'll
142 * get called forever, which is bad.
147 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
151 /* class attribute for bond_masters file. This ends up in /sys/class/net */
152 static const struct class_attribute class_attr_bonding_masters = {
154 .name = "bonding_masters",
155 .mode = S_IWUSR | S_IRUGO,
157 .show = bonding_show_bonds,
158 .store = bonding_store_bonds,
162 * Show the slaves in the current bond.
164 static ssize_t bonding_show_slaves(struct device *d,
165 struct device_attribute *attr, char *buf)
167 struct bonding *bond = to_bond(d);
168 struct list_head *iter;
173 return restart_syscall();
175 bond_for_each_slave(bond, slave, iter) {
176 if (res > (PAGE_SIZE - IFNAMSIZ)) {
177 /* not enough space for another interface name */
178 if ((PAGE_SIZE - res) > 10)
179 res = PAGE_SIZE - 10;
180 res += sprintf(buf + res, "++more++ ");
183 res += sprintf(buf + res, "%s ", slave->dev->name);
189 buf[res-1] = '\n'; /* eat the leftover space */
195 * Set the slaves in the current bond.
196 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
197 * All hard work should be done there.
199 static ssize_t bonding_store_slaves(struct device *d,
200 struct device_attribute *attr,
201 const char *buffer, size_t count)
203 char command[IFNAMSIZ + 1] = { 0, };
205 int res, ret = count;
206 struct net_device *dev;
207 struct bonding *bond = to_bond(d);
210 return restart_syscall();
212 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
213 ifname = command + 1;
214 if ((strlen(command) <= 1) ||
215 !dev_valid_name(ifname))
218 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
220 pr_info("%s: Interface %s does not exist!\n",
221 bond->dev->name, ifname);
226 switch (command[0]) {
228 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
229 res = bond_enslave(bond->dev, dev);
233 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
234 res = bond_release(bond->dev, dev);
246 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
255 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
256 bonding_store_slaves);
259 * Show and set the bonding mode. The bond interface must be down to
262 static ssize_t bonding_show_mode(struct device *d,
263 struct device_attribute *attr, char *buf)
265 struct bonding *bond = to_bond(d);
266 struct bond_opt_value *val;
268 val = bond_opt_get_val(BOND_OPT_MODE, bond->params.mode);
270 return sprintf(buf, "%s %d\n", val->string, bond->params.mode);
273 static ssize_t bonding_store_mode(struct device *d,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
277 struct bonding *bond = to_bond(d);
280 ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MODE, (char *)buf);
286 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
287 bonding_show_mode, bonding_store_mode);
290 * Show and set the bonding transmit hash method.
292 static ssize_t bonding_show_xmit_hash(struct device *d,
293 struct device_attribute *attr,
296 struct bonding *bond = to_bond(d);
298 return sprintf(buf, "%s %d\n",
299 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
300 bond->params.xmit_policy);
303 static ssize_t bonding_store_xmit_hash(struct device *d,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
308 struct bonding *bond = to_bond(d);
310 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
312 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
314 (int)strlen(buf) - 1, buf);
319 return restart_syscall();
321 ret = bond_option_xmit_hash_policy_set(bond, new_value);
328 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
329 bonding_show_xmit_hash, bonding_store_xmit_hash);
332 * Show and set arp_validate.
334 static ssize_t bonding_show_arp_validate(struct device *d,
335 struct device_attribute *attr,
338 struct bonding *bond = to_bond(d);
340 return sprintf(buf, "%s %d\n",
341 arp_validate_tbl[bond->params.arp_validate].modename,
342 bond->params.arp_validate);
345 static ssize_t bonding_store_arp_validate(struct device *d,
346 struct device_attribute *attr,
347 const char *buf, size_t count)
349 struct bonding *bond = to_bond(d);
352 new_value = bond_parse_parm(buf, arp_validate_tbl);
354 pr_err("%s: Ignoring invalid arp_validate value %s\n",
355 bond->dev->name, buf);
359 return restart_syscall();
361 ret = bond_option_arp_validate_set(bond, new_value);
370 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
371 bonding_store_arp_validate);
373 * Show and set arp_all_targets.
375 static ssize_t bonding_show_arp_all_targets(struct device *d,
376 struct device_attribute *attr,
379 struct bonding *bond = to_bond(d);
380 int value = bond->params.arp_all_targets;
382 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
386 static ssize_t bonding_store_arp_all_targets(struct device *d,
387 struct device_attribute *attr,
388 const char *buf, size_t count)
390 struct bonding *bond = to_bond(d);
393 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
395 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
396 bond->dev->name, buf);
401 return restart_syscall();
403 ret = bond_option_arp_all_targets_set(bond, new_value);
412 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
413 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
416 * Show and store fail_over_mac. User only allowed to change the
417 * value when there are no slaves.
419 static ssize_t bonding_show_fail_over_mac(struct device *d,
420 struct device_attribute *attr,
423 struct bonding *bond = to_bond(d);
425 return sprintf(buf, "%s %d\n",
426 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
427 bond->params.fail_over_mac);
430 static ssize_t bonding_store_fail_over_mac(struct device *d,
431 struct device_attribute *attr,
432 const char *buf, size_t count)
435 struct bonding *bond = to_bond(d);
437 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
439 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
440 bond->dev->name, buf);
445 return restart_syscall();
447 ret = bond_option_fail_over_mac_set(bond, new_value);
455 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
456 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
459 * Show and set the arp timer interval. There are two tricky bits
460 * here. First, if ARP monitoring is activated, then we must disable
461 * MII monitoring. Second, if the ARP timer isn't running, we must
464 static ssize_t bonding_show_arp_interval(struct device *d,
465 struct device_attribute *attr,
468 struct bonding *bond = to_bond(d);
470 return sprintf(buf, "%d\n", bond->params.arp_interval);
473 static ssize_t bonding_store_arp_interval(struct device *d,
474 struct device_attribute *attr,
475 const char *buf, size_t count)
477 struct bonding *bond = to_bond(d);
480 if (sscanf(buf, "%d", &new_value) != 1) {
481 pr_err("%s: no arp_interval value specified.\n",
487 return restart_syscall();
489 ret = bond_option_arp_interval_set(bond, new_value);
496 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
497 bonding_show_arp_interval, bonding_store_arp_interval);
500 * Show and set the arp targets.
502 static ssize_t bonding_show_arp_targets(struct device *d,
503 struct device_attribute *attr,
507 struct bonding *bond = to_bond(d);
509 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
510 if (bond->params.arp_targets[i])
511 res += sprintf(buf + res, "%pI4 ",
512 &bond->params.arp_targets[i]);
515 buf[res-1] = '\n'; /* eat the leftover space */
519 static ssize_t bonding_store_arp_targets(struct device *d,
520 struct device_attribute *attr,
521 const char *buf, size_t count)
523 struct bonding *bond = to_bond(d);
527 if (!in4_pton(buf + 1, -1, (u8 *)&target, -1, NULL)) {
528 pr_err("%s: invalid ARP target %pI4 specified\n",
529 bond->dev->name, &target);
534 return restart_syscall();
537 ret = bond_option_arp_ip_target_add(bond, target);
538 else if (buf[0] == '-')
539 ret = bond_option_arp_ip_target_rem(bond, target);
541 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
550 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
553 * Show and set the up and down delays. These must be multiples of the
554 * MII monitoring value, and are stored internally as the multiplier.
555 * Thus, we must translate to MS for the real world.
557 static ssize_t bonding_show_downdelay(struct device *d,
558 struct device_attribute *attr,
561 struct bonding *bond = to_bond(d);
563 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
566 static ssize_t bonding_store_downdelay(struct device *d,
567 struct device_attribute *attr,
568 const char *buf, size_t count)
571 struct bonding *bond = to_bond(d);
573 if (sscanf(buf, "%d", &new_value) != 1) {
574 pr_err("%s: no down delay value specified.\n", bond->dev->name);
579 return restart_syscall();
581 ret = bond_option_downdelay_set(bond, new_value);
588 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
589 bonding_show_downdelay, bonding_store_downdelay);
591 static ssize_t bonding_show_updelay(struct device *d,
592 struct device_attribute *attr,
595 struct bonding *bond = to_bond(d);
597 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
601 static ssize_t bonding_store_updelay(struct device *d,
602 struct device_attribute *attr,
603 const char *buf, size_t count)
606 struct bonding *bond = to_bond(d);
608 if (sscanf(buf, "%d", &new_value) != 1) {
609 pr_err("%s: no up delay value specified.\n",
615 return restart_syscall();
617 ret = bond_option_updelay_set(bond, new_value);
624 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
625 bonding_show_updelay, bonding_store_updelay);
628 * Show and set the LACP interval. Interface must be down, and the mode
629 * must be set to 802.3ad mode.
631 static ssize_t bonding_show_lacp(struct device *d,
632 struct device_attribute *attr,
635 struct bonding *bond = to_bond(d);
637 return sprintf(buf, "%s %d\n",
638 bond_lacp_tbl[bond->params.lacp_fast].modename,
639 bond->params.lacp_fast);
642 static ssize_t bonding_store_lacp(struct device *d,
643 struct device_attribute *attr,
644 const char *buf, size_t count)
646 struct bonding *bond = to_bond(d);
649 new_value = bond_parse_parm(buf, bond_lacp_tbl);
651 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
652 bond->dev->name, (int)strlen(buf) - 1, buf);
657 return restart_syscall();
659 ret = bond_option_lacp_rate_set(bond, new_value);
666 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
667 bonding_show_lacp, bonding_store_lacp);
669 static ssize_t bonding_show_min_links(struct device *d,
670 struct device_attribute *attr,
673 struct bonding *bond = to_bond(d);
675 return sprintf(buf, "%d\n", bond->params.min_links);
678 static ssize_t bonding_store_min_links(struct device *d,
679 struct device_attribute *attr,
680 const char *buf, size_t count)
682 struct bonding *bond = to_bond(d);
684 unsigned int new_value;
686 ret = kstrtouint(buf, 0, &new_value);
688 pr_err("%s: Ignoring invalid min links value %s.\n",
689 bond->dev->name, buf);
694 return restart_syscall();
696 ret = bond_option_min_links_set(bond, new_value);
703 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
704 bonding_show_min_links, bonding_store_min_links);
706 static ssize_t bonding_show_ad_select(struct device *d,
707 struct device_attribute *attr,
710 struct bonding *bond = to_bond(d);
712 return sprintf(buf, "%s %d\n",
713 ad_select_tbl[bond->params.ad_select].modename,
714 bond->params.ad_select);
718 static ssize_t bonding_store_ad_select(struct device *d,
719 struct device_attribute *attr,
720 const char *buf, size_t count)
723 struct bonding *bond = to_bond(d);
725 new_value = bond_parse_parm(buf, ad_select_tbl);
727 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
728 bond->dev->name, (int)strlen(buf) - 1, buf);
733 return restart_syscall();
735 ret = bond_option_ad_select_set(bond, new_value);
742 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
743 bonding_show_ad_select, bonding_store_ad_select);
746 * Show and set the number of peer notifications to send after a failover event.
748 static ssize_t bonding_show_num_peer_notif(struct device *d,
749 struct device_attribute *attr,
752 struct bonding *bond = to_bond(d);
753 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
756 static ssize_t bonding_store_num_peer_notif(struct device *d,
757 struct device_attribute *attr,
758 const char *buf, size_t count)
760 struct bonding *bond = to_bond(d);
764 ret = kstrtou8(buf, 10, &new_value);
766 pr_err("%s: invalid value %s specified.\n",
767 bond->dev->name, buf);
772 return restart_syscall();
774 ret = bond_option_num_peer_notif_set(bond, new_value);
781 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
782 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
783 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
784 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
787 * Show and set the MII monitor interval. There are two tricky bits
788 * here. First, if MII monitoring is activated, then we must disable
789 * ARP monitoring. Second, if the timer isn't running, we must
792 static ssize_t bonding_show_miimon(struct device *d,
793 struct device_attribute *attr,
796 struct bonding *bond = to_bond(d);
798 return sprintf(buf, "%d\n", bond->params.miimon);
801 static ssize_t bonding_store_miimon(struct device *d,
802 struct device_attribute *attr,
803 const char *buf, size_t count)
806 struct bonding *bond = to_bond(d);
808 if (sscanf(buf, "%d", &new_value) != 1) {
809 pr_err("%s: no miimon value specified.\n",
815 return restart_syscall();
817 ret = bond_option_miimon_set(bond, new_value);
824 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
825 bonding_show_miimon, bonding_store_miimon);
828 * Show and set the primary slave. The store function is much
829 * simpler than bonding_store_slaves function because it only needs to
830 * handle one interface name.
831 * The bond must be a mode that supports a primary for this be
834 static ssize_t bonding_show_primary(struct device *d,
835 struct device_attribute *attr,
839 struct bonding *bond = to_bond(d);
841 if (bond->primary_slave)
842 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
847 static ssize_t bonding_store_primary(struct device *d,
848 struct device_attribute *attr,
849 const char *buf, size_t count)
851 struct bonding *bond = to_bond(d);
852 char ifname[IFNAMSIZ];
855 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
856 if (ifname[0] == '\n')
860 return restart_syscall();
862 ret = bond_option_primary_set(bond, ifname);
869 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
870 bonding_show_primary, bonding_store_primary);
873 * Show and set the primary_reselect flag.
875 static ssize_t bonding_show_primary_reselect(struct device *d,
876 struct device_attribute *attr,
879 struct bonding *bond = to_bond(d);
881 return sprintf(buf, "%s %d\n",
882 pri_reselect_tbl[bond->params.primary_reselect].modename,
883 bond->params.primary_reselect);
886 static ssize_t bonding_store_primary_reselect(struct device *d,
887 struct device_attribute *attr,
888 const char *buf, size_t count)
891 struct bonding *bond = to_bond(d);
893 new_value = bond_parse_parm(buf, pri_reselect_tbl);
895 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
897 (int) strlen(buf) - 1, buf);
902 return restart_syscall();
904 ret = bond_option_primary_reselect_set(bond, new_value);
911 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
912 bonding_show_primary_reselect,
913 bonding_store_primary_reselect);
916 * Show and set the use_carrier flag.
918 static ssize_t bonding_show_carrier(struct device *d,
919 struct device_attribute *attr,
922 struct bonding *bond = to_bond(d);
924 return sprintf(buf, "%d\n", bond->params.use_carrier);
927 static ssize_t bonding_store_carrier(struct device *d,
928 struct device_attribute *attr,
929 const char *buf, size_t count)
932 struct bonding *bond = to_bond(d);
934 if (sscanf(buf, "%d", &new_value) != 1) {
935 pr_err("%s: no use_carrier value specified.\n",
941 return restart_syscall();
943 ret = bond_option_use_carrier_set(bond, new_value);
950 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
951 bonding_show_carrier, bonding_store_carrier);
955 * Show and set currently active_slave.
957 static ssize_t bonding_show_active_slave(struct device *d,
958 struct device_attribute *attr,
961 struct bonding *bond = to_bond(d);
962 struct net_device *slave_dev;
966 slave_dev = bond_option_active_slave_get_rcu(bond);
968 count = sprintf(buf, "%s\n", slave_dev->name);
974 static ssize_t bonding_store_active_slave(struct device *d,
975 struct device_attribute *attr,
976 const char *buf, size_t count)
979 struct bonding *bond = to_bond(d);
980 char ifname[IFNAMSIZ];
981 struct net_device *dev;
984 return restart_syscall();
986 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
987 if (!strlen(ifname) || buf[0] == '\n') {
990 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
997 ret = bond_option_active_slave_set(bond, dev);
1007 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1008 bonding_show_active_slave, bonding_store_active_slave);
1012 * Show link status of the bond interface.
1014 static ssize_t bonding_show_mii_status(struct device *d,
1015 struct device_attribute *attr,
1018 struct bonding *bond = to_bond(d);
1020 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1022 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1025 * Show current 802.3ad aggregator ID.
1027 static ssize_t bonding_show_ad_aggregator(struct device *d,
1028 struct device_attribute *attr,
1032 struct bonding *bond = to_bond(d);
1034 if (bond->params.mode == BOND_MODE_8023AD) {
1035 struct ad_info ad_info;
1036 count = sprintf(buf, "%d\n",
1037 bond_3ad_get_active_agg_info(bond, &ad_info)
1038 ? 0 : ad_info.aggregator_id);
1043 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1047 * Show number of active 802.3ad ports.
1049 static ssize_t bonding_show_ad_num_ports(struct device *d,
1050 struct device_attribute *attr,
1054 struct bonding *bond = to_bond(d);
1056 if (bond->params.mode == BOND_MODE_8023AD) {
1057 struct ad_info ad_info;
1058 count = sprintf(buf, "%d\n",
1059 bond_3ad_get_active_agg_info(bond, &ad_info)
1060 ? 0 : ad_info.ports);
1065 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1069 * Show current 802.3ad actor key.
1071 static ssize_t bonding_show_ad_actor_key(struct device *d,
1072 struct device_attribute *attr,
1076 struct bonding *bond = to_bond(d);
1078 if (bond->params.mode == BOND_MODE_8023AD) {
1079 struct ad_info ad_info;
1080 count = sprintf(buf, "%d\n",
1081 bond_3ad_get_active_agg_info(bond, &ad_info)
1082 ? 0 : ad_info.actor_key);
1087 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1091 * Show current 802.3ad partner key.
1093 static ssize_t bonding_show_ad_partner_key(struct device *d,
1094 struct device_attribute *attr,
1098 struct bonding *bond = to_bond(d);
1100 if (bond->params.mode == BOND_MODE_8023AD) {
1101 struct ad_info ad_info;
1102 count = sprintf(buf, "%d\n",
1103 bond_3ad_get_active_agg_info(bond, &ad_info)
1104 ? 0 : ad_info.partner_key);
1109 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1113 * Show current 802.3ad partner mac.
1115 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1116 struct device_attribute *attr,
1120 struct bonding *bond = to_bond(d);
1122 if (bond->params.mode == BOND_MODE_8023AD) {
1123 struct ad_info ad_info;
1124 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1125 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1130 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1133 * Show the queue_ids of the slaves in the current bond.
1135 static ssize_t bonding_show_queue_id(struct device *d,
1136 struct device_attribute *attr,
1139 struct bonding *bond = to_bond(d);
1140 struct list_head *iter;
1141 struct slave *slave;
1144 if (!rtnl_trylock())
1145 return restart_syscall();
1147 bond_for_each_slave(bond, slave, iter) {
1148 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1149 /* not enough space for another interface_name:queue_id pair */
1150 if ((PAGE_SIZE - res) > 10)
1151 res = PAGE_SIZE - 10;
1152 res += sprintf(buf + res, "++more++ ");
1155 res += sprintf(buf + res, "%s:%d ",
1156 slave->dev->name, slave->queue_id);
1159 buf[res-1] = '\n'; /* eat the leftover space */
1167 * Set the queue_ids of the slaves in the current bond. The bond
1168 * interface must be enslaved for this to work.
1170 static ssize_t bonding_store_queue_id(struct device *d,
1171 struct device_attribute *attr,
1172 const char *buffer, size_t count)
1174 struct slave *slave, *update_slave;
1175 struct bonding *bond = to_bond(d);
1176 struct list_head *iter;
1180 struct net_device *sdev = NULL;
1182 if (!rtnl_trylock())
1183 return restart_syscall();
1185 /* delim will point to queue id if successful */
1186 delim = strchr(buffer, ':');
1191 * Terminate string that points to device name and bump it
1192 * up one, so we can read the queue id there.
1195 if (sscanf(++delim, "%hd\n", &qid) != 1)
1198 /* Check buffer length, valid ifname and queue id */
1199 if (strlen(buffer) > IFNAMSIZ ||
1200 !dev_valid_name(buffer) ||
1201 qid > bond->dev->real_num_tx_queues)
1204 /* Get the pointer to that interface if it exists */
1205 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1209 /* Search for thes slave and check for duplicate qids */
1210 update_slave = NULL;
1211 bond_for_each_slave(bond, slave, iter) {
1212 if (sdev == slave->dev)
1214 * We don't need to check the matching
1215 * slave for dups, since we're overwriting it
1217 update_slave = slave;
1218 else if (qid && qid == slave->queue_id) {
1226 /* Actually set the qids for the slave */
1227 update_slave->queue_id = qid;
1234 pr_info("invalid input for queue_id set for %s.\n",
1240 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1241 bonding_store_queue_id);
1245 * Show and set the all_slaves_active flag.
1247 static ssize_t bonding_show_slaves_active(struct device *d,
1248 struct device_attribute *attr,
1251 struct bonding *bond = to_bond(d);
1253 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1256 static ssize_t bonding_store_slaves_active(struct device *d,
1257 struct device_attribute *attr,
1258 const char *buf, size_t count)
1260 struct bonding *bond = to_bond(d);
1263 if (sscanf(buf, "%d", &new_value) != 1) {
1264 pr_err("%s: no all_slaves_active value specified.\n",
1269 if (!rtnl_trylock())
1270 return restart_syscall();
1272 ret = bond_option_all_slaves_active_set(bond, new_value);
1279 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1280 bonding_show_slaves_active, bonding_store_slaves_active);
1283 * Show and set the number of IGMP membership reports to send on link failure
1285 static ssize_t bonding_show_resend_igmp(struct device *d,
1286 struct device_attribute *attr,
1289 struct bonding *bond = to_bond(d);
1291 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1294 static ssize_t bonding_store_resend_igmp(struct device *d,
1295 struct device_attribute *attr,
1296 const char *buf, size_t count)
1298 int new_value, ret = count;
1299 struct bonding *bond = to_bond(d);
1301 if (sscanf(buf, "%d", &new_value) != 1) {
1302 pr_err("%s: no resend_igmp value specified.\n",
1307 if (!rtnl_trylock())
1308 return restart_syscall();
1310 ret = bond_option_resend_igmp_set(bond, new_value);
1318 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1319 bonding_show_resend_igmp, bonding_store_resend_igmp);
1322 static ssize_t bonding_show_lp_interval(struct device *d,
1323 struct device_attribute *attr,
1326 struct bonding *bond = to_bond(d);
1327 return sprintf(buf, "%d\n", bond->params.lp_interval);
1330 static ssize_t bonding_store_lp_interval(struct device *d,
1331 struct device_attribute *attr,
1332 const char *buf, size_t count)
1334 struct bonding *bond = to_bond(d);
1337 if (sscanf(buf, "%d", &new_value) != 1) {
1338 pr_err("%s: no lp interval value specified.\n",
1343 if (!rtnl_trylock())
1344 return restart_syscall();
1346 ret = bond_option_lp_interval_set(bond, new_value);
1354 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1355 bonding_show_lp_interval, bonding_store_lp_interval);
1357 static ssize_t bonding_show_packets_per_slave(struct device *d,
1358 struct device_attribute *attr,
1361 struct bonding *bond = to_bond(d);
1362 unsigned int packets_per_slave = bond->params.packets_per_slave;
1363 return sprintf(buf, "%u\n", packets_per_slave);
1366 static ssize_t bonding_store_packets_per_slave(struct device *d,
1367 struct device_attribute *attr,
1368 const char *buf, size_t count)
1370 struct bonding *bond = to_bond(d);
1373 if (sscanf(buf, "%d", &new_value) != 1) {
1374 pr_err("%s: no packets_per_slave value specified.\n",
1379 if (!rtnl_trylock())
1380 return restart_syscall();
1382 ret = bond_option_packets_per_slave_set(bond, new_value);
1390 static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1391 bonding_show_packets_per_slave,
1392 bonding_store_packets_per_slave);
1394 static struct attribute *per_bond_attrs[] = {
1395 &dev_attr_slaves.attr,
1396 &dev_attr_mode.attr,
1397 &dev_attr_fail_over_mac.attr,
1398 &dev_attr_arp_validate.attr,
1399 &dev_attr_arp_all_targets.attr,
1400 &dev_attr_arp_interval.attr,
1401 &dev_attr_arp_ip_target.attr,
1402 &dev_attr_downdelay.attr,
1403 &dev_attr_updelay.attr,
1404 &dev_attr_lacp_rate.attr,
1405 &dev_attr_ad_select.attr,
1406 &dev_attr_xmit_hash_policy.attr,
1407 &dev_attr_num_grat_arp.attr,
1408 &dev_attr_num_unsol_na.attr,
1409 &dev_attr_miimon.attr,
1410 &dev_attr_primary.attr,
1411 &dev_attr_primary_reselect.attr,
1412 &dev_attr_use_carrier.attr,
1413 &dev_attr_active_slave.attr,
1414 &dev_attr_mii_status.attr,
1415 &dev_attr_ad_aggregator.attr,
1416 &dev_attr_ad_num_ports.attr,
1417 &dev_attr_ad_actor_key.attr,
1418 &dev_attr_ad_partner_key.attr,
1419 &dev_attr_ad_partner_mac.attr,
1420 &dev_attr_queue_id.attr,
1421 &dev_attr_all_slaves_active.attr,
1422 &dev_attr_resend_igmp.attr,
1423 &dev_attr_min_links.attr,
1424 &dev_attr_lp_interval.attr,
1425 &dev_attr_packets_per_slave.attr,
1429 static struct attribute_group bonding_group = {
1431 .attrs = per_bond_attrs,
1435 * Initialize sysfs. This sets up the bonding_masters file in
1438 int bond_create_sysfs(struct bond_net *bn)
1442 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1443 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1445 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1448 * Permit multiple loads of the module by ignoring failures to
1449 * create the bonding_masters sysfs file. Bonding devices
1450 * created by second or subsequent loads of the module will
1451 * not be listed in, or controllable by, bonding_masters, but
1452 * will have the usual "bonding" sysfs directory.
1454 * This is done to preserve backwards compatibility for
1455 * initscripts/sysconfig, which load bonding multiple times to
1456 * configure multiple bonding devices.
1458 if (ret == -EEXIST) {
1459 /* Is someone being kinky and naming a device bonding_master? */
1460 if (__dev_get_by_name(bn->net,
1461 class_attr_bonding_masters.attr.name))
1462 pr_err("network device named %s already exists in sysfs",
1463 class_attr_bonding_masters.attr.name);
1472 * Remove /sys/class/net/bonding_masters.
1474 void bond_destroy_sysfs(struct bond_net *bn)
1476 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
1480 * Initialize sysfs for each bond. This sets up and registers
1481 * the 'bondctl' directory for each individual bond under /sys/class/net.
1483 void bond_prepare_sysfs_group(struct bonding *bond)
1485 bond->dev->sysfs_groups[0] = &bonding_group;