2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/sched.h>
30 #include <linux/types.h>
31 #include <linux/string.h>
32 #include <linux/netdevice.h>
33 #include <linux/inetdevice.h>
35 #include <linux/sysfs.h>
36 #include <linux/ctype.h>
37 #include <linux/inet.h>
38 #include <linux/rtnetlink.h>
39 #include <linux/etherdevice.h>
40 #include <net/net_namespace.h>
41 #include <net/netns/generic.h>
42 #include <linux/nsproxy.h>
43 #include <linux/reciprocal_div.h>
47 #define to_dev(obj) container_of(obj, struct device, kobj)
48 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
54 static ssize_t bonding_show_bonds(struct class *cls,
55 struct class_attribute *attr,
59 container_of(attr, struct bond_net, class_attr_bonding_masters);
65 list_for_each_entry(bond, &bn->dev_list, bond_list) {
66 if (res > (PAGE_SIZE - IFNAMSIZ)) {
67 /* not enough space for another interface name */
68 if ((PAGE_SIZE - res) > 10)
70 res += sprintf(buf + res, "++more++ ");
73 res += sprintf(buf + res, "%s ", bond->dev->name);
76 buf[res-1] = '\n'; /* eat the leftover space */
82 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
86 list_for_each_entry(bond, &bn->dev_list, bond_list) {
87 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
94 * "store" function for the bond_masters attribute. This is what
95 * creates and deletes entire bonds.
97 * The class parameter is ignored.
101 static ssize_t bonding_store_bonds(struct class *cls,
102 struct class_attribute *attr,
103 const char *buffer, size_t count)
105 struct bond_net *bn =
106 container_of(attr, struct bond_net, class_attr_bonding_masters);
107 char command[IFNAMSIZ + 1] = {0, };
111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
117 if (command[0] == '+') {
118 pr_info("%s is being created...\n", ifname);
119 rv = bond_create(bn->net, ifname);
122 pr_info("%s already exists.\n", ifname);
124 pr_info("%s creation failed.\n", ifname);
127 } else if (command[0] == '-') {
128 struct net_device *bond_dev;
131 bond_dev = bond_get_by_name(bn, ifname);
133 pr_info("%s is being deleted...\n", ifname);
134 unregister_netdevice(bond_dev);
136 pr_err("unable to delete non-existent %s\n", ifname);
143 /* Always return either count or an error. If you return 0, you'll
144 * get called forever, which is bad.
149 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
153 /* class attribute for bond_masters file. This ends up in /sys/class/net */
154 static const struct class_attribute class_attr_bonding_masters = {
156 .name = "bonding_masters",
157 .mode = S_IWUSR | S_IRUGO,
159 .show = bonding_show_bonds,
160 .store = bonding_store_bonds,
164 * Show the slaves in the current bond.
166 static ssize_t bonding_show_slaves(struct device *d,
167 struct device_attribute *attr, char *buf)
169 struct bonding *bond = to_bond(d);
170 struct list_head *iter;
175 return restart_syscall();
177 bond_for_each_slave(bond, slave, iter) {
178 if (res > (PAGE_SIZE - IFNAMSIZ)) {
179 /* not enough space for another interface name */
180 if ((PAGE_SIZE - res) > 10)
181 res = PAGE_SIZE - 10;
182 res += sprintf(buf + res, "++more++ ");
185 res += sprintf(buf + res, "%s ", slave->dev->name);
191 buf[res-1] = '\n'; /* eat the leftover space */
197 * Set the slaves in the current bond.
198 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
199 * All hard work should be done there.
201 static ssize_t bonding_store_slaves(struct device *d,
202 struct device_attribute *attr,
203 const char *buffer, size_t count)
205 char command[IFNAMSIZ + 1] = { 0, };
207 int res, ret = count;
208 struct net_device *dev;
209 struct bonding *bond = to_bond(d);
212 return restart_syscall();
214 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
215 ifname = command + 1;
216 if ((strlen(command) <= 1) ||
217 !dev_valid_name(ifname))
220 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
222 pr_info("%s: Interface %s does not exist!\n",
223 bond->dev->name, ifname);
228 switch (command[0]) {
230 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
231 res = bond_enslave(bond->dev, dev);
235 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
236 res = bond_release(bond->dev, dev);
248 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
257 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
258 bonding_store_slaves);
261 * Show and set the bonding mode. The bond interface must be down to
264 static ssize_t bonding_show_mode(struct device *d,
265 struct device_attribute *attr, char *buf)
267 struct bonding *bond = to_bond(d);
269 return sprintf(buf, "%s %d\n",
270 bond_mode_tbl[bond->params.mode].modename,
274 static ssize_t bonding_store_mode(struct device *d,
275 struct device_attribute *attr,
276 const char *buf, size_t count)
279 struct bonding *bond = to_bond(d);
281 new_value = bond_parse_parm(buf, bond_mode_tbl);
283 pr_err("%s: Ignoring invalid mode value %.*s.\n",
284 bond->dev->name, (int)strlen(buf) - 1, buf);
288 return restart_syscall();
290 ret = bond_option_mode_set(bond, new_value);
292 pr_info("%s: setting mode to %s (%d).\n",
293 bond->dev->name, bond_mode_tbl[new_value].modename,
301 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
302 bonding_show_mode, bonding_store_mode);
305 * Show and set the bonding transmit hash method.
307 static ssize_t bonding_show_xmit_hash(struct device *d,
308 struct device_attribute *attr,
311 struct bonding *bond = to_bond(d);
313 return sprintf(buf, "%s %d\n",
314 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
315 bond->params.xmit_policy);
318 static ssize_t bonding_store_xmit_hash(struct device *d,
319 struct device_attribute *attr,
320 const char *buf, size_t count)
322 int new_value, ret = count;
323 struct bonding *bond = to_bond(d);
325 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
327 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
329 (int)strlen(buf) - 1, buf);
332 bond->params.xmit_policy = new_value;
333 pr_info("%s: setting xmit hash policy to %s (%d).\n",
335 xmit_hashtype_tbl[new_value].modename, new_value);
340 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
341 bonding_show_xmit_hash, bonding_store_xmit_hash);
344 * Show and set arp_validate.
346 static ssize_t bonding_show_arp_validate(struct device *d,
347 struct device_attribute *attr,
350 struct bonding *bond = to_bond(d);
352 return sprintf(buf, "%s %d\n",
353 arp_validate_tbl[bond->params.arp_validate].modename,
354 bond->params.arp_validate);
357 static ssize_t bonding_store_arp_validate(struct device *d,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
361 struct bonding *bond = to_bond(d);
362 int new_value, ret = count;
365 return restart_syscall();
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 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
374 pr_err("%s: arp_validate only supported in active-backup mode.\n",
379 pr_info("%s: setting arp_validate to %s (%d).\n",
380 bond->dev->name, arp_validate_tbl[new_value].modename,
383 if (bond->dev->flags & IFF_UP) {
385 bond->recv_probe = NULL;
386 else if (bond->params.arp_interval)
387 bond->recv_probe = bond_arp_rcv;
389 bond->params.arp_validate = new_value;
396 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
397 bonding_store_arp_validate);
399 * Show and set arp_all_targets.
401 static ssize_t bonding_show_arp_all_targets(struct device *d,
402 struct device_attribute *attr,
405 struct bonding *bond = to_bond(d);
406 int value = bond->params.arp_all_targets;
408 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
412 static ssize_t bonding_store_arp_all_targets(struct device *d,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
416 struct bonding *bond = to_bond(d);
419 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
421 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
422 bond->dev->name, buf);
425 pr_info("%s: setting arp_all_targets to %s (%d).\n",
426 bond->dev->name, arp_all_targets_tbl[new_value].modename,
429 bond->params.arp_all_targets = new_value;
434 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
435 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
438 * Show and store fail_over_mac. User only allowed to change the
439 * value when there are no slaves.
441 static ssize_t bonding_show_fail_over_mac(struct device *d,
442 struct device_attribute *attr,
445 struct bonding *bond = to_bond(d);
447 return sprintf(buf, "%s %d\n",
448 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
449 bond->params.fail_over_mac);
452 static ssize_t bonding_store_fail_over_mac(struct device *d,
453 struct device_attribute *attr,
454 const char *buf, size_t count)
456 int new_value, ret = count;
457 struct bonding *bond = to_bond(d);
460 return restart_syscall();
462 if (bond_has_slaves(bond)) {
463 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
469 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
471 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
472 bond->dev->name, buf);
477 bond->params.fail_over_mac = new_value;
478 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
479 bond->dev->name, fail_over_mac_tbl[new_value].modename,
487 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
488 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
491 * Show and set the arp timer interval. There are two tricky bits
492 * here. First, if ARP monitoring is activated, then we must disable
493 * MII monitoring. Second, if the ARP timer isn't running, we must
496 static ssize_t bonding_show_arp_interval(struct device *d,
497 struct device_attribute *attr,
500 struct bonding *bond = to_bond(d);
502 return sprintf(buf, "%d\n", bond->params.arp_interval);
505 static ssize_t bonding_store_arp_interval(struct device *d,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
509 struct bonding *bond = to_bond(d);
510 int new_value, ret = count;
513 return restart_syscall();
514 if (sscanf(buf, "%d", &new_value) != 1) {
515 pr_err("%s: no arp_interval value specified.\n",
521 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
522 bond->dev->name, new_value, INT_MAX);
526 if (BOND_NO_USES_ARP(bond->params.mode)) {
527 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
528 bond->dev->name, bond->dev->name);
532 pr_info("%s: Setting ARP monitoring interval to %d.\n",
533 bond->dev->name, new_value);
534 bond->params.arp_interval = new_value;
536 if (bond->params.miimon) {
537 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
538 bond->dev->name, bond->dev->name);
539 bond->params.miimon = 0;
541 if (!bond->params.arp_targets[0])
542 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
545 if (bond->dev->flags & IFF_UP) {
546 /* If the interface is up, we may need to fire off
547 * the ARP timer. If the interface is down, the
548 * timer will get fired off when the open function
552 if (bond->params.arp_validate)
553 bond->recv_probe = NULL;
554 cancel_delayed_work_sync(&bond->arp_work);
556 /* arp_validate can be set only in active-backup mode */
557 if (bond->params.arp_validate)
558 bond->recv_probe = bond_arp_rcv;
559 cancel_delayed_work_sync(&bond->mii_work);
560 queue_delayed_work(bond->wq, &bond->arp_work, 0);
567 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
568 bonding_show_arp_interval, bonding_store_arp_interval);
571 * Show and set the arp targets.
573 static ssize_t bonding_show_arp_targets(struct device *d,
574 struct device_attribute *attr,
578 struct bonding *bond = to_bond(d);
580 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
581 if (bond->params.arp_targets[i])
582 res += sprintf(buf + res, "%pI4 ",
583 &bond->params.arp_targets[i]);
586 buf[res-1] = '\n'; /* eat the leftover space */
590 static ssize_t bonding_store_arp_targets(struct device *d,
591 struct device_attribute *attr,
592 const char *buf, size_t count)
594 struct bonding *bond = to_bond(d);
595 struct list_head *iter;
597 __be32 newtarget, *targets;
598 unsigned long *targets_rx;
599 int ind, i, j, ret = -EINVAL;
602 return restart_syscall();
604 targets = bond->params.arp_targets;
605 if (!in4_pton(buf + 1, -1, (u8 *)&newtarget, -1, NULL) ||
606 IS_IP_TARGET_UNUSABLE_ADDRESS(newtarget)) {
607 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
608 bond->dev->name, &newtarget);
613 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
614 pr_err("%s: ARP target %pI4 is already present\n",
615 bond->dev->name, &newtarget);
619 ind = bond_get_targets_ip(targets, 0); /* first free slot */
621 pr_err("%s: ARP target table is full!\n",
626 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
628 /* not to race with bond_arp_rcv */
629 write_lock_bh(&bond->lock);
630 bond_for_each_slave(bond, slave, iter)
631 slave->target_last_arp_rx[ind] = jiffies;
632 targets[ind] = newtarget;
633 write_unlock_bh(&bond->lock);
634 } else if (buf[0] == '-') {
635 ind = bond_get_targets_ip(targets, newtarget);
637 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
638 bond->dev->name, &newtarget);
642 if (ind == 0 && !targets[1] && bond->params.arp_interval)
643 pr_warn("%s: removing last arp target with arp_interval on\n",
646 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
649 write_lock_bh(&bond->lock);
650 bond_for_each_slave(bond, slave, iter) {
651 targets_rx = slave->target_last_arp_rx;
653 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
654 targets_rx[j] = targets_rx[j+1];
657 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
658 targets[i] = targets[i+1];
660 write_unlock_bh(&bond->lock);
662 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
673 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
676 * Show and set the up and down delays. These must be multiples of the
677 * MII monitoring value, and are stored internally as the multiplier.
678 * Thus, we must translate to MS for the real world.
680 static ssize_t bonding_show_downdelay(struct device *d,
681 struct device_attribute *attr,
684 struct bonding *bond = to_bond(d);
686 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
689 static ssize_t bonding_store_downdelay(struct device *d,
690 struct device_attribute *attr,
691 const char *buf, size_t count)
693 int new_value, ret = count;
694 struct bonding *bond = to_bond(d);
697 return restart_syscall();
698 if (!(bond->params.miimon)) {
699 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
705 if (sscanf(buf, "%d", &new_value) != 1) {
706 pr_err("%s: no down delay value specified.\n", bond->dev->name);
711 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
712 bond->dev->name, new_value, 0, INT_MAX);
716 if ((new_value % bond->params.miimon) != 0) {
717 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
718 bond->dev->name, new_value,
720 (new_value / bond->params.miimon) *
721 bond->params.miimon);
723 bond->params.downdelay = new_value / bond->params.miimon;
724 pr_info("%s: Setting down delay to %d.\n",
726 bond->params.downdelay * bond->params.miimon);
734 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
735 bonding_show_downdelay, bonding_store_downdelay);
737 static ssize_t bonding_show_updelay(struct device *d,
738 struct device_attribute *attr,
741 struct bonding *bond = to_bond(d);
743 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
747 static ssize_t bonding_store_updelay(struct device *d,
748 struct device_attribute *attr,
749 const char *buf, size_t count)
751 int new_value, ret = count;
752 struct bonding *bond = to_bond(d);
755 return restart_syscall();
756 if (!(bond->params.miimon)) {
757 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
763 if (sscanf(buf, "%d", &new_value) != 1) {
764 pr_err("%s: no up delay value specified.\n",
770 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
771 bond->dev->name, new_value, 0, INT_MAX);
775 if ((new_value % bond->params.miimon) != 0) {
776 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
777 bond->dev->name, new_value,
779 (new_value / bond->params.miimon) *
780 bond->params.miimon);
782 bond->params.updelay = new_value / bond->params.miimon;
783 pr_info("%s: Setting up delay to %d.\n",
785 bond->params.updelay * bond->params.miimon);
792 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
793 bonding_show_updelay, bonding_store_updelay);
796 * Show and set the LACP interval. Interface must be down, and the mode
797 * must be set to 802.3ad mode.
799 static ssize_t bonding_show_lacp(struct device *d,
800 struct device_attribute *attr,
803 struct bonding *bond = to_bond(d);
805 return sprintf(buf, "%s %d\n",
806 bond_lacp_tbl[bond->params.lacp_fast].modename,
807 bond->params.lacp_fast);
810 static ssize_t bonding_store_lacp(struct device *d,
811 struct device_attribute *attr,
812 const char *buf, size_t count)
814 struct bonding *bond = to_bond(d);
815 int new_value, ret = count;
818 return restart_syscall();
820 if (bond->dev->flags & IFF_UP) {
821 pr_err("%s: Unable to update LACP rate because interface is up.\n",
827 if (bond->params.mode != BOND_MODE_8023AD) {
828 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
834 new_value = bond_parse_parm(buf, bond_lacp_tbl);
836 if ((new_value == 1) || (new_value == 0)) {
837 bond->params.lacp_fast = new_value;
838 bond_3ad_update_lacp_rate(bond);
839 pr_info("%s: Setting LACP rate to %s (%d).\n",
840 bond->dev->name, bond_lacp_tbl[new_value].modename,
843 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
844 bond->dev->name, (int)strlen(buf) - 1, buf);
852 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
853 bonding_show_lacp, bonding_store_lacp);
855 static ssize_t bonding_show_min_links(struct device *d,
856 struct device_attribute *attr,
859 struct bonding *bond = to_bond(d);
861 return sprintf(buf, "%d\n", bond->params.min_links);
864 static ssize_t bonding_store_min_links(struct device *d,
865 struct device_attribute *attr,
866 const char *buf, size_t count)
868 struct bonding *bond = to_bond(d);
870 unsigned int new_value;
872 ret = kstrtouint(buf, 0, &new_value);
874 pr_err("%s: Ignoring invalid min links value %s.\n",
875 bond->dev->name, buf);
879 pr_info("%s: Setting min links value to %u\n",
880 bond->dev->name, new_value);
881 bond->params.min_links = new_value;
884 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
885 bonding_show_min_links, bonding_store_min_links);
887 static ssize_t bonding_show_ad_select(struct device *d,
888 struct device_attribute *attr,
891 struct bonding *bond = to_bond(d);
893 return sprintf(buf, "%s %d\n",
894 ad_select_tbl[bond->params.ad_select].modename,
895 bond->params.ad_select);
899 static ssize_t bonding_store_ad_select(struct device *d,
900 struct device_attribute *attr,
901 const char *buf, size_t count)
903 int new_value, ret = count;
904 struct bonding *bond = to_bond(d);
906 if (bond->dev->flags & IFF_UP) {
907 pr_err("%s: Unable to update ad_select because interface is up.\n",
913 new_value = bond_parse_parm(buf, ad_select_tbl);
915 if (new_value != -1) {
916 bond->params.ad_select = new_value;
917 pr_info("%s: Setting ad_select to %s (%d).\n",
918 bond->dev->name, ad_select_tbl[new_value].modename,
921 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
922 bond->dev->name, (int)strlen(buf) - 1, buf);
928 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
929 bonding_show_ad_select, bonding_store_ad_select);
932 * Show and set the number of peer notifications to send after a failover event.
934 static ssize_t bonding_show_num_peer_notif(struct device *d,
935 struct device_attribute *attr,
938 struct bonding *bond = to_bond(d);
939 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
942 static ssize_t bonding_store_num_peer_notif(struct device *d,
943 struct device_attribute *attr,
944 const char *buf, size_t count)
946 struct bonding *bond = to_bond(d);
947 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
948 return err ? err : count;
950 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
951 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
952 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
953 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
956 * Show and set the MII monitor interval. There are two tricky bits
957 * here. First, if MII monitoring is activated, then we must disable
958 * ARP monitoring. Second, if the timer isn't running, we must
961 static ssize_t bonding_show_miimon(struct device *d,
962 struct device_attribute *attr,
965 struct bonding *bond = to_bond(d);
967 return sprintf(buf, "%d\n", bond->params.miimon);
970 static ssize_t bonding_store_miimon(struct device *d,
971 struct device_attribute *attr,
972 const char *buf, size_t count)
974 int new_value, ret = count;
975 struct bonding *bond = to_bond(d);
978 return restart_syscall();
979 if (sscanf(buf, "%d", &new_value) != 1) {
980 pr_err("%s: no miimon value specified.\n",
986 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
987 bond->dev->name, new_value, 0, INT_MAX);
991 pr_info("%s: Setting MII monitoring interval to %d.\n",
992 bond->dev->name, new_value);
993 bond->params.miimon = new_value;
994 if (bond->params.updelay)
995 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
997 bond->params.updelay * bond->params.miimon);
998 if (bond->params.downdelay)
999 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1001 bond->params.downdelay * bond->params.miimon);
1002 if (new_value && bond->params.arp_interval) {
1003 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1005 bond->params.arp_interval = 0;
1006 if (bond->params.arp_validate)
1007 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1009 if (bond->dev->flags & IFF_UP) {
1010 /* If the interface is up, we may need to fire off
1011 * the MII timer. If the interface is down, the
1012 * timer will get fired off when the open function
1016 cancel_delayed_work_sync(&bond->mii_work);
1018 cancel_delayed_work_sync(&bond->arp_work);
1019 queue_delayed_work(bond->wq, &bond->mii_work, 0);
1026 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1027 bonding_show_miimon, bonding_store_miimon);
1030 * Show and set the primary slave. The store function is much
1031 * simpler than bonding_store_slaves function because it only needs to
1032 * handle one interface name.
1033 * The bond must be a mode that supports a primary for this be
1036 static ssize_t bonding_show_primary(struct device *d,
1037 struct device_attribute *attr,
1041 struct bonding *bond = to_bond(d);
1043 if (bond->primary_slave)
1044 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1049 static ssize_t bonding_store_primary(struct device *d,
1050 struct device_attribute *attr,
1051 const char *buf, size_t count)
1053 struct bonding *bond = to_bond(d);
1054 struct list_head *iter;
1055 char ifname[IFNAMSIZ];
1056 struct slave *slave;
1058 if (!rtnl_trylock())
1059 return restart_syscall();
1061 read_lock(&bond->lock);
1062 write_lock_bh(&bond->curr_slave_lock);
1064 if (!USES_PRIMARY(bond->params.mode)) {
1065 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1066 bond->dev->name, bond->dev->name, bond->params.mode);
1070 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1072 /* check to see if we are clearing primary */
1073 if (!strlen(ifname) || buf[0] == '\n') {
1074 pr_info("%s: Setting primary slave to None.\n",
1076 bond->primary_slave = NULL;
1077 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1078 bond_select_active_slave(bond);
1082 bond_for_each_slave(bond, slave, iter) {
1083 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1084 pr_info("%s: Setting %s as primary slave.\n",
1085 bond->dev->name, slave->dev->name);
1086 bond->primary_slave = slave;
1087 strcpy(bond->params.primary, slave->dev->name);
1088 bond_select_active_slave(bond);
1093 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1094 bond->params.primary[IFNAMSIZ - 1] = 0;
1096 pr_info("%s: Recording %s as primary, "
1097 "but it has not been enslaved to %s yet.\n",
1098 bond->dev->name, ifname, bond->dev->name);
1100 write_unlock_bh(&bond->curr_slave_lock);
1101 read_unlock(&bond->lock);
1102 unblock_netpoll_tx();
1107 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1108 bonding_show_primary, bonding_store_primary);
1111 * Show and set the primary_reselect flag.
1113 static ssize_t bonding_show_primary_reselect(struct device *d,
1114 struct device_attribute *attr,
1117 struct bonding *bond = to_bond(d);
1119 return sprintf(buf, "%s %d\n",
1120 pri_reselect_tbl[bond->params.primary_reselect].modename,
1121 bond->params.primary_reselect);
1124 static ssize_t bonding_store_primary_reselect(struct device *d,
1125 struct device_attribute *attr,
1126 const char *buf, size_t count)
1128 int new_value, ret = count;
1129 struct bonding *bond = to_bond(d);
1131 if (!rtnl_trylock())
1132 return restart_syscall();
1134 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1135 if (new_value < 0) {
1136 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1138 (int) strlen(buf) - 1, buf);
1143 bond->params.primary_reselect = new_value;
1144 pr_info("%s: setting primary_reselect to %s (%d).\n",
1145 bond->dev->name, pri_reselect_tbl[new_value].modename,
1149 read_lock(&bond->lock);
1150 write_lock_bh(&bond->curr_slave_lock);
1151 bond_select_active_slave(bond);
1152 write_unlock_bh(&bond->curr_slave_lock);
1153 read_unlock(&bond->lock);
1154 unblock_netpoll_tx();
1159 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1160 bonding_show_primary_reselect,
1161 bonding_store_primary_reselect);
1164 * Show and set the use_carrier flag.
1166 static ssize_t bonding_show_carrier(struct device *d,
1167 struct device_attribute *attr,
1170 struct bonding *bond = to_bond(d);
1172 return sprintf(buf, "%d\n", bond->params.use_carrier);
1175 static ssize_t bonding_store_carrier(struct device *d,
1176 struct device_attribute *attr,
1177 const char *buf, size_t count)
1179 int new_value, ret = count;
1180 struct bonding *bond = to_bond(d);
1183 if (sscanf(buf, "%d", &new_value) != 1) {
1184 pr_err("%s: no use_carrier value specified.\n",
1189 if ((new_value == 0) || (new_value == 1)) {
1190 bond->params.use_carrier = new_value;
1191 pr_info("%s: Setting use_carrier to %d.\n",
1192 bond->dev->name, new_value);
1194 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1195 bond->dev->name, new_value);
1200 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1201 bonding_show_carrier, bonding_store_carrier);
1205 * Show and set currently active_slave.
1207 static ssize_t bonding_show_active_slave(struct device *d,
1208 struct device_attribute *attr,
1211 struct bonding *bond = to_bond(d);
1212 struct net_device *slave_dev;
1216 slave_dev = bond_option_active_slave_get_rcu(bond);
1218 count = sprintf(buf, "%s\n", slave_dev->name);
1224 static ssize_t bonding_store_active_slave(struct device *d,
1225 struct device_attribute *attr,
1226 const char *buf, size_t count)
1229 struct bonding *bond = to_bond(d);
1230 char ifname[IFNAMSIZ];
1231 struct net_device *dev;
1233 if (!rtnl_trylock())
1234 return restart_syscall();
1236 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1237 if (!strlen(ifname) || buf[0] == '\n') {
1240 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1247 ret = bond_option_active_slave_set(bond, dev);
1257 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1258 bonding_show_active_slave, bonding_store_active_slave);
1262 * Show link status of the bond interface.
1264 static ssize_t bonding_show_mii_status(struct device *d,
1265 struct device_attribute *attr,
1268 struct bonding *bond = to_bond(d);
1270 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1272 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1275 * Show current 802.3ad aggregator ID.
1277 static ssize_t bonding_show_ad_aggregator(struct device *d,
1278 struct device_attribute *attr,
1282 struct bonding *bond = to_bond(d);
1284 if (bond->params.mode == BOND_MODE_8023AD) {
1285 struct ad_info ad_info;
1286 count = sprintf(buf, "%d\n",
1287 bond_3ad_get_active_agg_info(bond, &ad_info)
1288 ? 0 : ad_info.aggregator_id);
1293 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1297 * Show number of active 802.3ad ports.
1299 static ssize_t bonding_show_ad_num_ports(struct device *d,
1300 struct device_attribute *attr,
1304 struct bonding *bond = to_bond(d);
1306 if (bond->params.mode == BOND_MODE_8023AD) {
1307 struct ad_info ad_info;
1308 count = sprintf(buf, "%d\n",
1309 bond_3ad_get_active_agg_info(bond, &ad_info)
1310 ? 0 : ad_info.ports);
1315 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1319 * Show current 802.3ad actor key.
1321 static ssize_t bonding_show_ad_actor_key(struct device *d,
1322 struct device_attribute *attr,
1326 struct bonding *bond = to_bond(d);
1328 if (bond->params.mode == BOND_MODE_8023AD) {
1329 struct ad_info ad_info;
1330 count = sprintf(buf, "%d\n",
1331 bond_3ad_get_active_agg_info(bond, &ad_info)
1332 ? 0 : ad_info.actor_key);
1337 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1341 * Show current 802.3ad partner key.
1343 static ssize_t bonding_show_ad_partner_key(struct device *d,
1344 struct device_attribute *attr,
1348 struct bonding *bond = to_bond(d);
1350 if (bond->params.mode == BOND_MODE_8023AD) {
1351 struct ad_info ad_info;
1352 count = sprintf(buf, "%d\n",
1353 bond_3ad_get_active_agg_info(bond, &ad_info)
1354 ? 0 : ad_info.partner_key);
1359 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1363 * Show current 802.3ad partner mac.
1365 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1366 struct device_attribute *attr,
1370 struct bonding *bond = to_bond(d);
1372 if (bond->params.mode == BOND_MODE_8023AD) {
1373 struct ad_info ad_info;
1374 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1375 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1380 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1383 * Show the queue_ids of the slaves in the current bond.
1385 static ssize_t bonding_show_queue_id(struct device *d,
1386 struct device_attribute *attr,
1389 struct bonding *bond = to_bond(d);
1390 struct list_head *iter;
1391 struct slave *slave;
1394 if (!rtnl_trylock())
1395 return restart_syscall();
1397 bond_for_each_slave(bond, slave, iter) {
1398 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1399 /* not enough space for another interface_name:queue_id pair */
1400 if ((PAGE_SIZE - res) > 10)
1401 res = PAGE_SIZE - 10;
1402 res += sprintf(buf + res, "++more++ ");
1405 res += sprintf(buf + res, "%s:%d ",
1406 slave->dev->name, slave->queue_id);
1409 buf[res-1] = '\n'; /* eat the leftover space */
1417 * Set the queue_ids of the slaves in the current bond. The bond
1418 * interface must be enslaved for this to work.
1420 static ssize_t bonding_store_queue_id(struct device *d,
1421 struct device_attribute *attr,
1422 const char *buffer, size_t count)
1424 struct slave *slave, *update_slave;
1425 struct bonding *bond = to_bond(d);
1426 struct list_head *iter;
1430 struct net_device *sdev = NULL;
1432 if (!rtnl_trylock())
1433 return restart_syscall();
1435 /* delim will point to queue id if successful */
1436 delim = strchr(buffer, ':');
1441 * Terminate string that points to device name and bump it
1442 * up one, so we can read the queue id there.
1445 if (sscanf(++delim, "%hd\n", &qid) != 1)
1448 /* Check buffer length, valid ifname and queue id */
1449 if (strlen(buffer) > IFNAMSIZ ||
1450 !dev_valid_name(buffer) ||
1451 qid > bond->dev->real_num_tx_queues)
1454 /* Get the pointer to that interface if it exists */
1455 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1459 /* Search for thes slave and check for duplicate qids */
1460 update_slave = NULL;
1461 bond_for_each_slave(bond, slave, iter) {
1462 if (sdev == slave->dev)
1464 * We don't need to check the matching
1465 * slave for dups, since we're overwriting it
1467 update_slave = slave;
1468 else if (qid && qid == slave->queue_id) {
1476 /* Actually set the qids for the slave */
1477 update_slave->queue_id = qid;
1484 pr_info("invalid input for queue_id set for %s.\n",
1490 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1491 bonding_store_queue_id);
1495 * Show and set the all_slaves_active flag.
1497 static ssize_t bonding_show_slaves_active(struct device *d,
1498 struct device_attribute *attr,
1501 struct bonding *bond = to_bond(d);
1503 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1506 static ssize_t bonding_store_slaves_active(struct device *d,
1507 struct device_attribute *attr,
1508 const char *buf, size_t count)
1510 struct bonding *bond = to_bond(d);
1511 int new_value, ret = count;
1512 struct list_head *iter;
1513 struct slave *slave;
1515 if (!rtnl_trylock())
1516 return restart_syscall();
1518 if (sscanf(buf, "%d", &new_value) != 1) {
1519 pr_err("%s: no all_slaves_active value specified.\n",
1525 if (new_value == bond->params.all_slaves_active)
1528 if ((new_value == 0) || (new_value == 1)) {
1529 bond->params.all_slaves_active = new_value;
1531 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1532 bond->dev->name, new_value);
1537 bond_for_each_slave(bond, slave, iter) {
1538 if (!bond_is_active_slave(slave)) {
1540 slave->inactive = 0;
1542 slave->inactive = 1;
1549 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1550 bonding_show_slaves_active, bonding_store_slaves_active);
1553 * Show and set the number of IGMP membership reports to send on link failure
1555 static ssize_t bonding_show_resend_igmp(struct device *d,
1556 struct device_attribute *attr,
1559 struct bonding *bond = to_bond(d);
1561 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1564 static ssize_t bonding_store_resend_igmp(struct device *d,
1565 struct device_attribute *attr,
1566 const char *buf, size_t count)
1568 int new_value, ret = count;
1569 struct bonding *bond = to_bond(d);
1571 if (sscanf(buf, "%d", &new_value) != 1) {
1572 pr_err("%s: no resend_igmp value specified.\n",
1578 if (new_value < 0 || new_value > 255) {
1579 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1580 bond->dev->name, new_value);
1585 pr_info("%s: Setting resend_igmp to %d.\n",
1586 bond->dev->name, new_value);
1587 bond->params.resend_igmp = new_value;
1592 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1593 bonding_show_resend_igmp, bonding_store_resend_igmp);
1596 static ssize_t bonding_show_lp_interval(struct device *d,
1597 struct device_attribute *attr,
1600 struct bonding *bond = to_bond(d);
1601 return sprintf(buf, "%d\n", bond->params.lp_interval);
1604 static ssize_t bonding_store_lp_interval(struct device *d,
1605 struct device_attribute *attr,
1606 const char *buf, size_t count)
1608 struct bonding *bond = to_bond(d);
1609 int new_value, ret = count;
1611 if (sscanf(buf, "%d", &new_value) != 1) {
1612 pr_err("%s: no lp interval value specified.\n",
1618 if (new_value <= 0) {
1619 pr_err ("%s: lp_interval must be between 1 and %d\n",
1620 bond->dev->name, INT_MAX);
1625 bond->params.lp_interval = new_value;
1630 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1631 bonding_show_lp_interval, bonding_store_lp_interval);
1633 static ssize_t bonding_show_packets_per_slave(struct device *d,
1634 struct device_attribute *attr,
1637 struct bonding *bond = to_bond(d);
1638 int packets_per_slave = bond->params.packets_per_slave;
1640 if (packets_per_slave > 1)
1641 packets_per_slave = reciprocal_value(packets_per_slave);
1643 return sprintf(buf, "%d\n", packets_per_slave);
1646 static ssize_t bonding_store_packets_per_slave(struct device *d,
1647 struct device_attribute *attr,
1648 const char *buf, size_t count)
1650 struct bonding *bond = to_bond(d);
1651 int new_value, ret = count;
1653 if (sscanf(buf, "%d", &new_value) != 1) {
1654 pr_err("%s: no packets_per_slave value specified.\n",
1659 if (new_value < 0 || new_value > USHRT_MAX) {
1660 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1661 bond->dev->name, USHRT_MAX);
1665 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1666 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1669 bond->params.packets_per_slave = reciprocal_value(new_value);
1671 bond->params.packets_per_slave = new_value;
1676 static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1677 bonding_show_packets_per_slave,
1678 bonding_store_packets_per_slave);
1680 static struct attribute *per_bond_attrs[] = {
1681 &dev_attr_slaves.attr,
1682 &dev_attr_mode.attr,
1683 &dev_attr_fail_over_mac.attr,
1684 &dev_attr_arp_validate.attr,
1685 &dev_attr_arp_all_targets.attr,
1686 &dev_attr_arp_interval.attr,
1687 &dev_attr_arp_ip_target.attr,
1688 &dev_attr_downdelay.attr,
1689 &dev_attr_updelay.attr,
1690 &dev_attr_lacp_rate.attr,
1691 &dev_attr_ad_select.attr,
1692 &dev_attr_xmit_hash_policy.attr,
1693 &dev_attr_num_grat_arp.attr,
1694 &dev_attr_num_unsol_na.attr,
1695 &dev_attr_miimon.attr,
1696 &dev_attr_primary.attr,
1697 &dev_attr_primary_reselect.attr,
1698 &dev_attr_use_carrier.attr,
1699 &dev_attr_active_slave.attr,
1700 &dev_attr_mii_status.attr,
1701 &dev_attr_ad_aggregator.attr,
1702 &dev_attr_ad_num_ports.attr,
1703 &dev_attr_ad_actor_key.attr,
1704 &dev_attr_ad_partner_key.attr,
1705 &dev_attr_ad_partner_mac.attr,
1706 &dev_attr_queue_id.attr,
1707 &dev_attr_all_slaves_active.attr,
1708 &dev_attr_resend_igmp.attr,
1709 &dev_attr_min_links.attr,
1710 &dev_attr_lp_interval.attr,
1711 &dev_attr_packets_per_slave.attr,
1715 static struct attribute_group bonding_group = {
1717 .attrs = per_bond_attrs,
1721 * Initialize sysfs. This sets up the bonding_masters file in
1724 int bond_create_sysfs(struct bond_net *bn)
1728 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1729 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1731 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1734 * Permit multiple loads of the module by ignoring failures to
1735 * create the bonding_masters sysfs file. Bonding devices
1736 * created by second or subsequent loads of the module will
1737 * not be listed in, or controllable by, bonding_masters, but
1738 * will have the usual "bonding" sysfs directory.
1740 * This is done to preserve backwards compatibility for
1741 * initscripts/sysconfig, which load bonding multiple times to
1742 * configure multiple bonding devices.
1744 if (ret == -EEXIST) {
1745 /* Is someone being kinky and naming a device bonding_master? */
1746 if (__dev_get_by_name(bn->net,
1747 class_attr_bonding_masters.attr.name))
1748 pr_err("network device named %s already exists in sysfs",
1749 class_attr_bonding_masters.attr.name);
1758 * Remove /sys/class/net/bonding_masters.
1760 void bond_destroy_sysfs(struct bond_net *bn)
1762 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
1766 * Initialize sysfs for each bond. This sets up and registers
1767 * the 'bondctl' directory for each individual bond under /sys/class/net.
1769 void bond_prepare_sysfs_group(struct bonding *bond)
1771 bond->dev->sysfs_groups[0] = &bonding_group;