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);
361 int new_value, ret = count;
364 return restart_syscall();
365 new_value = bond_parse_parm(buf, arp_validate_tbl);
367 pr_err("%s: Ignoring invalid arp_validate value %s\n",
368 bond->dev->name, buf);
372 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
373 pr_err("%s: arp_validate only supported in active-backup mode.\n",
378 pr_info("%s: setting arp_validate to %s (%d).\n",
379 bond->dev->name, arp_validate_tbl[new_value].modename,
382 if (bond->dev->flags & IFF_UP) {
384 bond->recv_probe = NULL;
385 else if (bond->params.arp_interval)
386 bond->recv_probe = bond_arp_rcv;
388 bond->params.arp_validate = new_value;
395 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
396 bonding_store_arp_validate);
398 * Show and set arp_all_targets.
400 static ssize_t bonding_show_arp_all_targets(struct device *d,
401 struct device_attribute *attr,
404 struct bonding *bond = to_bond(d);
405 int value = bond->params.arp_all_targets;
407 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
411 static ssize_t bonding_store_arp_all_targets(struct device *d,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
415 struct bonding *bond = to_bond(d);
418 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
420 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
421 bond->dev->name, buf);
424 pr_info("%s: setting arp_all_targets to %s (%d).\n",
425 bond->dev->name, arp_all_targets_tbl[new_value].modename,
428 bond->params.arp_all_targets = new_value;
433 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
434 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
437 * Show and store fail_over_mac. User only allowed to change the
438 * value when there are no slaves.
440 static ssize_t bonding_show_fail_over_mac(struct device *d,
441 struct device_attribute *attr,
444 struct bonding *bond = to_bond(d);
446 return sprintf(buf, "%s %d\n",
447 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
448 bond->params.fail_over_mac);
451 static ssize_t bonding_store_fail_over_mac(struct device *d,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
455 int new_value, ret = count;
456 struct bonding *bond = to_bond(d);
459 return restart_syscall();
461 if (bond_has_slaves(bond)) {
462 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
468 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
470 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
471 bond->dev->name, buf);
476 bond->params.fail_over_mac = new_value;
477 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
478 bond->dev->name, fail_over_mac_tbl[new_value].modename,
486 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
487 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
490 * Show and set the arp timer interval. There are two tricky bits
491 * here. First, if ARP monitoring is activated, then we must disable
492 * MII monitoring. Second, if the ARP timer isn't running, we must
495 static ssize_t bonding_show_arp_interval(struct device *d,
496 struct device_attribute *attr,
499 struct bonding *bond = to_bond(d);
501 return sprintf(buf, "%d\n", bond->params.arp_interval);
504 static ssize_t bonding_store_arp_interval(struct device *d,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct bonding *bond = to_bond(d);
509 int new_value, ret = count;
512 return restart_syscall();
513 if (sscanf(buf, "%d", &new_value) != 1) {
514 pr_err("%s: no arp_interval value specified.\n",
520 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
521 bond->dev->name, new_value, INT_MAX);
525 if (BOND_NO_USES_ARP(bond->params.mode)) {
526 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
527 bond->dev->name, bond->dev->name);
531 pr_info("%s: Setting ARP monitoring interval to %d.\n",
532 bond->dev->name, new_value);
533 bond->params.arp_interval = new_value;
535 if (bond->params.miimon) {
536 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
537 bond->dev->name, bond->dev->name);
538 bond->params.miimon = 0;
540 if (!bond->params.arp_targets[0])
541 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
544 if (bond->dev->flags & IFF_UP) {
545 /* If the interface is up, we may need to fire off
546 * the ARP timer. If the interface is down, the
547 * timer will get fired off when the open function
551 if (bond->params.arp_validate)
552 bond->recv_probe = NULL;
553 cancel_delayed_work_sync(&bond->arp_work);
555 /* arp_validate can be set only in active-backup mode */
556 if (bond->params.arp_validate)
557 bond->recv_probe = bond_arp_rcv;
558 cancel_delayed_work_sync(&bond->mii_work);
559 queue_delayed_work(bond->wq, &bond->arp_work, 0);
566 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
567 bonding_show_arp_interval, bonding_store_arp_interval);
570 * Show and set the arp targets.
572 static ssize_t bonding_show_arp_targets(struct device *d,
573 struct device_attribute *attr,
577 struct bonding *bond = to_bond(d);
579 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
580 if (bond->params.arp_targets[i])
581 res += sprintf(buf + res, "%pI4 ",
582 &bond->params.arp_targets[i]);
585 buf[res-1] = '\n'; /* eat the leftover space */
589 static ssize_t bonding_store_arp_targets(struct device *d,
590 struct device_attribute *attr,
591 const char *buf, size_t count)
593 struct bonding *bond = to_bond(d);
594 struct list_head *iter;
596 __be32 newtarget, *targets;
597 unsigned long *targets_rx;
598 int ind, i, j, ret = -EINVAL;
601 return restart_syscall();
603 targets = bond->params.arp_targets;
604 if (!in4_pton(buf + 1, -1, (u8 *)&newtarget, -1, NULL) ||
605 IS_IP_TARGET_UNUSABLE_ADDRESS(newtarget)) {
606 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
607 bond->dev->name, &newtarget);
612 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
613 pr_err("%s: ARP target %pI4 is already present\n",
614 bond->dev->name, &newtarget);
618 ind = bond_get_targets_ip(targets, 0); /* first free slot */
620 pr_err("%s: ARP target table is full!\n",
625 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
627 /* not to race with bond_arp_rcv */
628 write_lock_bh(&bond->lock);
629 bond_for_each_slave(bond, slave, iter)
630 slave->target_last_arp_rx[ind] = jiffies;
631 targets[ind] = newtarget;
632 write_unlock_bh(&bond->lock);
633 } else if (buf[0] == '-') {
634 ind = bond_get_targets_ip(targets, newtarget);
636 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
637 bond->dev->name, &newtarget);
641 if (ind == 0 && !targets[1] && bond->params.arp_interval)
642 pr_warn("%s: removing last arp target with arp_interval on\n",
645 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
648 write_lock_bh(&bond->lock);
649 bond_for_each_slave(bond, slave, iter) {
650 targets_rx = slave->target_last_arp_rx;
652 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
653 targets_rx[j] = targets_rx[j+1];
656 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
657 targets[i] = targets[i+1];
659 write_unlock_bh(&bond->lock);
661 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
672 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
675 * Show and set the up and down delays. These must be multiples of the
676 * MII monitoring value, and are stored internally as the multiplier.
677 * Thus, we must translate to MS for the real world.
679 static ssize_t bonding_show_downdelay(struct device *d,
680 struct device_attribute *attr,
683 struct bonding *bond = to_bond(d);
685 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
688 static ssize_t bonding_store_downdelay(struct device *d,
689 struct device_attribute *attr,
690 const char *buf, size_t count)
692 int new_value, ret = count;
693 struct bonding *bond = to_bond(d);
696 return restart_syscall();
697 if (!(bond->params.miimon)) {
698 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
704 if (sscanf(buf, "%d", &new_value) != 1) {
705 pr_err("%s: no down delay value specified.\n", bond->dev->name);
710 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
711 bond->dev->name, new_value, 0, INT_MAX);
715 if ((new_value % bond->params.miimon) != 0) {
716 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
717 bond->dev->name, new_value,
719 (new_value / bond->params.miimon) *
720 bond->params.miimon);
722 bond->params.downdelay = new_value / bond->params.miimon;
723 pr_info("%s: Setting down delay to %d.\n",
725 bond->params.downdelay * bond->params.miimon);
733 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
734 bonding_show_downdelay, bonding_store_downdelay);
736 static ssize_t bonding_show_updelay(struct device *d,
737 struct device_attribute *attr,
740 struct bonding *bond = to_bond(d);
742 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
746 static ssize_t bonding_store_updelay(struct device *d,
747 struct device_attribute *attr,
748 const char *buf, size_t count)
750 int new_value, ret = count;
751 struct bonding *bond = to_bond(d);
754 return restart_syscall();
755 if (!(bond->params.miimon)) {
756 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
762 if (sscanf(buf, "%d", &new_value) != 1) {
763 pr_err("%s: no up delay value specified.\n",
769 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
770 bond->dev->name, new_value, 0, INT_MAX);
774 if ((new_value % bond->params.miimon) != 0) {
775 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
776 bond->dev->name, new_value,
778 (new_value / bond->params.miimon) *
779 bond->params.miimon);
781 bond->params.updelay = new_value / bond->params.miimon;
782 pr_info("%s: Setting up delay to %d.\n",
784 bond->params.updelay * bond->params.miimon);
791 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
792 bonding_show_updelay, bonding_store_updelay);
795 * Show and set the LACP interval. Interface must be down, and the mode
796 * must be set to 802.3ad mode.
798 static ssize_t bonding_show_lacp(struct device *d,
799 struct device_attribute *attr,
802 struct bonding *bond = to_bond(d);
804 return sprintf(buf, "%s %d\n",
805 bond_lacp_tbl[bond->params.lacp_fast].modename,
806 bond->params.lacp_fast);
809 static ssize_t bonding_store_lacp(struct device *d,
810 struct device_attribute *attr,
811 const char *buf, size_t count)
813 struct bonding *bond = to_bond(d);
814 int new_value, ret = count;
817 return restart_syscall();
819 if (bond->dev->flags & IFF_UP) {
820 pr_err("%s: Unable to update LACP rate because interface is up.\n",
826 if (bond->params.mode != BOND_MODE_8023AD) {
827 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
833 new_value = bond_parse_parm(buf, bond_lacp_tbl);
835 if ((new_value == 1) || (new_value == 0)) {
836 bond->params.lacp_fast = new_value;
837 bond_3ad_update_lacp_rate(bond);
838 pr_info("%s: Setting LACP rate to %s (%d).\n",
839 bond->dev->name, bond_lacp_tbl[new_value].modename,
842 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
843 bond->dev->name, (int)strlen(buf) - 1, buf);
851 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
852 bonding_show_lacp, bonding_store_lacp);
854 static ssize_t bonding_show_min_links(struct device *d,
855 struct device_attribute *attr,
858 struct bonding *bond = to_bond(d);
860 return sprintf(buf, "%d\n", bond->params.min_links);
863 static ssize_t bonding_store_min_links(struct device *d,
864 struct device_attribute *attr,
865 const char *buf, size_t count)
867 struct bonding *bond = to_bond(d);
869 unsigned int new_value;
871 ret = kstrtouint(buf, 0, &new_value);
873 pr_err("%s: Ignoring invalid min links value %s.\n",
874 bond->dev->name, buf);
878 pr_info("%s: Setting min links value to %u\n",
879 bond->dev->name, new_value);
880 bond->params.min_links = new_value;
883 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
884 bonding_show_min_links, bonding_store_min_links);
886 static ssize_t bonding_show_ad_select(struct device *d,
887 struct device_attribute *attr,
890 struct bonding *bond = to_bond(d);
892 return sprintf(buf, "%s %d\n",
893 ad_select_tbl[bond->params.ad_select].modename,
894 bond->params.ad_select);
898 static ssize_t bonding_store_ad_select(struct device *d,
899 struct device_attribute *attr,
900 const char *buf, size_t count)
902 int new_value, ret = count;
903 struct bonding *bond = to_bond(d);
905 if (bond->dev->flags & IFF_UP) {
906 pr_err("%s: Unable to update ad_select because interface is up.\n",
912 new_value = bond_parse_parm(buf, ad_select_tbl);
914 if (new_value != -1) {
915 bond->params.ad_select = new_value;
916 pr_info("%s: Setting ad_select to %s (%d).\n",
917 bond->dev->name, ad_select_tbl[new_value].modename,
920 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
921 bond->dev->name, (int)strlen(buf) - 1, buf);
927 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
928 bonding_show_ad_select, bonding_store_ad_select);
931 * Show and set the number of peer notifications to send after a failover event.
933 static ssize_t bonding_show_num_peer_notif(struct device *d,
934 struct device_attribute *attr,
937 struct bonding *bond = to_bond(d);
938 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
941 static ssize_t bonding_store_num_peer_notif(struct device *d,
942 struct device_attribute *attr,
943 const char *buf, size_t count)
945 struct bonding *bond = to_bond(d);
946 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
947 return err ? err : count;
949 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
950 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
951 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
952 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
955 * Show and set the MII monitor interval. There are two tricky bits
956 * here. First, if MII monitoring is activated, then we must disable
957 * ARP monitoring. Second, if the timer isn't running, we must
960 static ssize_t bonding_show_miimon(struct device *d,
961 struct device_attribute *attr,
964 struct bonding *bond = to_bond(d);
966 return sprintf(buf, "%d\n", bond->params.miimon);
969 static ssize_t bonding_store_miimon(struct device *d,
970 struct device_attribute *attr,
971 const char *buf, size_t count)
973 int new_value, ret = count;
974 struct bonding *bond = to_bond(d);
977 return restart_syscall();
978 if (sscanf(buf, "%d", &new_value) != 1) {
979 pr_err("%s: no miimon value specified.\n",
985 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
986 bond->dev->name, new_value, 0, INT_MAX);
990 pr_info("%s: Setting MII monitoring interval to %d.\n",
991 bond->dev->name, new_value);
992 bond->params.miimon = new_value;
993 if (bond->params.updelay)
994 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
996 bond->params.updelay * bond->params.miimon);
997 if (bond->params.downdelay)
998 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1000 bond->params.downdelay * bond->params.miimon);
1001 if (new_value && bond->params.arp_interval) {
1002 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1004 bond->params.arp_interval = 0;
1005 if (bond->params.arp_validate)
1006 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1008 if (bond->dev->flags & IFF_UP) {
1009 /* If the interface is up, we may need to fire off
1010 * the MII timer. If the interface is down, the
1011 * timer will get fired off when the open function
1015 cancel_delayed_work_sync(&bond->mii_work);
1017 cancel_delayed_work_sync(&bond->arp_work);
1018 queue_delayed_work(bond->wq, &bond->mii_work, 0);
1025 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1026 bonding_show_miimon, bonding_store_miimon);
1029 * Show and set the primary slave. The store function is much
1030 * simpler than bonding_store_slaves function because it only needs to
1031 * handle one interface name.
1032 * The bond must be a mode that supports a primary for this be
1035 static ssize_t bonding_show_primary(struct device *d,
1036 struct device_attribute *attr,
1040 struct bonding *bond = to_bond(d);
1042 if (bond->primary_slave)
1043 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1048 static ssize_t bonding_store_primary(struct device *d,
1049 struct device_attribute *attr,
1050 const char *buf, size_t count)
1052 struct bonding *bond = to_bond(d);
1053 struct list_head *iter;
1054 char ifname[IFNAMSIZ];
1055 struct slave *slave;
1057 if (!rtnl_trylock())
1058 return restart_syscall();
1060 read_lock(&bond->lock);
1061 write_lock_bh(&bond->curr_slave_lock);
1063 if (!USES_PRIMARY(bond->params.mode)) {
1064 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1065 bond->dev->name, bond->dev->name, bond->params.mode);
1069 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1071 /* check to see if we are clearing primary */
1072 if (!strlen(ifname) || buf[0] == '\n') {
1073 pr_info("%s: Setting primary slave to None.\n",
1075 bond->primary_slave = NULL;
1076 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1077 bond_select_active_slave(bond);
1081 bond_for_each_slave(bond, slave, iter) {
1082 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1083 pr_info("%s: Setting %s as primary slave.\n",
1084 bond->dev->name, slave->dev->name);
1085 bond->primary_slave = slave;
1086 strcpy(bond->params.primary, slave->dev->name);
1087 bond_select_active_slave(bond);
1092 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1093 bond->params.primary[IFNAMSIZ - 1] = 0;
1095 pr_info("%s: Recording %s as primary, "
1096 "but it has not been enslaved to %s yet.\n",
1097 bond->dev->name, ifname, bond->dev->name);
1099 write_unlock_bh(&bond->curr_slave_lock);
1100 read_unlock(&bond->lock);
1101 unblock_netpoll_tx();
1106 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1107 bonding_show_primary, bonding_store_primary);
1110 * Show and set the primary_reselect flag.
1112 static ssize_t bonding_show_primary_reselect(struct device *d,
1113 struct device_attribute *attr,
1116 struct bonding *bond = to_bond(d);
1118 return sprintf(buf, "%s %d\n",
1119 pri_reselect_tbl[bond->params.primary_reselect].modename,
1120 bond->params.primary_reselect);
1123 static ssize_t bonding_store_primary_reselect(struct device *d,
1124 struct device_attribute *attr,
1125 const char *buf, size_t count)
1127 int new_value, ret = count;
1128 struct bonding *bond = to_bond(d);
1130 if (!rtnl_trylock())
1131 return restart_syscall();
1133 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1134 if (new_value < 0) {
1135 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1137 (int) strlen(buf) - 1, buf);
1142 bond->params.primary_reselect = new_value;
1143 pr_info("%s: setting primary_reselect to %s (%d).\n",
1144 bond->dev->name, pri_reselect_tbl[new_value].modename,
1148 read_lock(&bond->lock);
1149 write_lock_bh(&bond->curr_slave_lock);
1150 bond_select_active_slave(bond);
1151 write_unlock_bh(&bond->curr_slave_lock);
1152 read_unlock(&bond->lock);
1153 unblock_netpoll_tx();
1158 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1159 bonding_show_primary_reselect,
1160 bonding_store_primary_reselect);
1163 * Show and set the use_carrier flag.
1165 static ssize_t bonding_show_carrier(struct device *d,
1166 struct device_attribute *attr,
1169 struct bonding *bond = to_bond(d);
1171 return sprintf(buf, "%d\n", bond->params.use_carrier);
1174 static ssize_t bonding_store_carrier(struct device *d,
1175 struct device_attribute *attr,
1176 const char *buf, size_t count)
1178 int new_value, ret = count;
1179 struct bonding *bond = to_bond(d);
1182 if (sscanf(buf, "%d", &new_value) != 1) {
1183 pr_err("%s: no use_carrier value specified.\n",
1188 if ((new_value == 0) || (new_value == 1)) {
1189 bond->params.use_carrier = new_value;
1190 pr_info("%s: Setting use_carrier to %d.\n",
1191 bond->dev->name, new_value);
1193 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1194 bond->dev->name, new_value);
1199 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1200 bonding_show_carrier, bonding_store_carrier);
1204 * Show and set currently active_slave.
1206 static ssize_t bonding_show_active_slave(struct device *d,
1207 struct device_attribute *attr,
1210 struct bonding *bond = to_bond(d);
1211 struct net_device *slave_dev;
1215 slave_dev = bond_option_active_slave_get_rcu(bond);
1217 count = sprintf(buf, "%s\n", slave_dev->name);
1223 static ssize_t bonding_store_active_slave(struct device *d,
1224 struct device_attribute *attr,
1225 const char *buf, size_t count)
1228 struct bonding *bond = to_bond(d);
1229 char ifname[IFNAMSIZ];
1230 struct net_device *dev;
1232 if (!rtnl_trylock())
1233 return restart_syscall();
1235 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1236 if (!strlen(ifname) || buf[0] == '\n') {
1239 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1246 ret = bond_option_active_slave_set(bond, dev);
1256 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1257 bonding_show_active_slave, bonding_store_active_slave);
1261 * Show link status of the bond interface.
1263 static ssize_t bonding_show_mii_status(struct device *d,
1264 struct device_attribute *attr,
1267 struct bonding *bond = to_bond(d);
1269 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1271 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1274 * Show current 802.3ad aggregator ID.
1276 static ssize_t bonding_show_ad_aggregator(struct device *d,
1277 struct device_attribute *attr,
1281 struct bonding *bond = to_bond(d);
1283 if (bond->params.mode == BOND_MODE_8023AD) {
1284 struct ad_info ad_info;
1285 count = sprintf(buf, "%d\n",
1286 bond_3ad_get_active_agg_info(bond, &ad_info)
1287 ? 0 : ad_info.aggregator_id);
1292 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1296 * Show number of active 802.3ad ports.
1298 static ssize_t bonding_show_ad_num_ports(struct device *d,
1299 struct device_attribute *attr,
1303 struct bonding *bond = to_bond(d);
1305 if (bond->params.mode == BOND_MODE_8023AD) {
1306 struct ad_info ad_info;
1307 count = sprintf(buf, "%d\n",
1308 bond_3ad_get_active_agg_info(bond, &ad_info)
1309 ? 0 : ad_info.ports);
1314 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1318 * Show current 802.3ad actor key.
1320 static ssize_t bonding_show_ad_actor_key(struct device *d,
1321 struct device_attribute *attr,
1325 struct bonding *bond = to_bond(d);
1327 if (bond->params.mode == BOND_MODE_8023AD) {
1328 struct ad_info ad_info;
1329 count = sprintf(buf, "%d\n",
1330 bond_3ad_get_active_agg_info(bond, &ad_info)
1331 ? 0 : ad_info.actor_key);
1336 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1340 * Show current 802.3ad partner key.
1342 static ssize_t bonding_show_ad_partner_key(struct device *d,
1343 struct device_attribute *attr,
1347 struct bonding *bond = to_bond(d);
1349 if (bond->params.mode == BOND_MODE_8023AD) {
1350 struct ad_info ad_info;
1351 count = sprintf(buf, "%d\n",
1352 bond_3ad_get_active_agg_info(bond, &ad_info)
1353 ? 0 : ad_info.partner_key);
1358 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1362 * Show current 802.3ad partner mac.
1364 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1365 struct device_attribute *attr,
1369 struct bonding *bond = to_bond(d);
1371 if (bond->params.mode == BOND_MODE_8023AD) {
1372 struct ad_info ad_info;
1373 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1374 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1379 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1382 * Show the queue_ids of the slaves in the current bond.
1384 static ssize_t bonding_show_queue_id(struct device *d,
1385 struct device_attribute *attr,
1388 struct bonding *bond = to_bond(d);
1389 struct list_head *iter;
1390 struct slave *slave;
1393 if (!rtnl_trylock())
1394 return restart_syscall();
1396 bond_for_each_slave(bond, slave, iter) {
1397 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1398 /* not enough space for another interface_name:queue_id pair */
1399 if ((PAGE_SIZE - res) > 10)
1400 res = PAGE_SIZE - 10;
1401 res += sprintf(buf + res, "++more++ ");
1404 res += sprintf(buf + res, "%s:%d ",
1405 slave->dev->name, slave->queue_id);
1408 buf[res-1] = '\n'; /* eat the leftover space */
1416 * Set the queue_ids of the slaves in the current bond. The bond
1417 * interface must be enslaved for this to work.
1419 static ssize_t bonding_store_queue_id(struct device *d,
1420 struct device_attribute *attr,
1421 const char *buffer, size_t count)
1423 struct slave *slave, *update_slave;
1424 struct bonding *bond = to_bond(d);
1425 struct list_head *iter;
1429 struct net_device *sdev = NULL;
1431 if (!rtnl_trylock())
1432 return restart_syscall();
1434 /* delim will point to queue id if successful */
1435 delim = strchr(buffer, ':');
1440 * Terminate string that points to device name and bump it
1441 * up one, so we can read the queue id there.
1444 if (sscanf(++delim, "%hd\n", &qid) != 1)
1447 /* Check buffer length, valid ifname and queue id */
1448 if (strlen(buffer) > IFNAMSIZ ||
1449 !dev_valid_name(buffer) ||
1450 qid > bond->dev->real_num_tx_queues)
1453 /* Get the pointer to that interface if it exists */
1454 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1458 /* Search for thes slave and check for duplicate qids */
1459 update_slave = NULL;
1460 bond_for_each_slave(bond, slave, iter) {
1461 if (sdev == slave->dev)
1463 * We don't need to check the matching
1464 * slave for dups, since we're overwriting it
1466 update_slave = slave;
1467 else if (qid && qid == slave->queue_id) {
1475 /* Actually set the qids for the slave */
1476 update_slave->queue_id = qid;
1483 pr_info("invalid input for queue_id set for %s.\n",
1489 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1490 bonding_store_queue_id);
1494 * Show and set the all_slaves_active flag.
1496 static ssize_t bonding_show_slaves_active(struct device *d,
1497 struct device_attribute *attr,
1500 struct bonding *bond = to_bond(d);
1502 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1505 static ssize_t bonding_store_slaves_active(struct device *d,
1506 struct device_attribute *attr,
1507 const char *buf, size_t count)
1509 struct bonding *bond = to_bond(d);
1510 int new_value, ret = count;
1511 struct list_head *iter;
1512 struct slave *slave;
1514 if (!rtnl_trylock())
1515 return restart_syscall();
1517 if (sscanf(buf, "%d", &new_value) != 1) {
1518 pr_err("%s: no all_slaves_active value specified.\n",
1524 if (new_value == bond->params.all_slaves_active)
1527 if ((new_value == 0) || (new_value == 1)) {
1528 bond->params.all_slaves_active = new_value;
1530 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1531 bond->dev->name, new_value);
1536 bond_for_each_slave(bond, slave, iter) {
1537 if (!bond_is_active_slave(slave)) {
1539 slave->inactive = 0;
1541 slave->inactive = 1;
1548 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1549 bonding_show_slaves_active, bonding_store_slaves_active);
1552 * Show and set the number of IGMP membership reports to send on link failure
1554 static ssize_t bonding_show_resend_igmp(struct device *d,
1555 struct device_attribute *attr,
1558 struct bonding *bond = to_bond(d);
1560 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1563 static ssize_t bonding_store_resend_igmp(struct device *d,
1564 struct device_attribute *attr,
1565 const char *buf, size_t count)
1567 int new_value, ret = count;
1568 struct bonding *bond = to_bond(d);
1570 if (sscanf(buf, "%d", &new_value) != 1) {
1571 pr_err("%s: no resend_igmp value specified.\n",
1577 if (new_value < 0 || new_value > 255) {
1578 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1579 bond->dev->name, new_value);
1584 pr_info("%s: Setting resend_igmp to %d.\n",
1585 bond->dev->name, new_value);
1586 bond->params.resend_igmp = new_value;
1591 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1592 bonding_show_resend_igmp, bonding_store_resend_igmp);
1595 static ssize_t bonding_show_lp_interval(struct device *d,
1596 struct device_attribute *attr,
1599 struct bonding *bond = to_bond(d);
1600 return sprintf(buf, "%d\n", bond->params.lp_interval);
1603 static ssize_t bonding_store_lp_interval(struct device *d,
1604 struct device_attribute *attr,
1605 const char *buf, size_t count)
1607 struct bonding *bond = to_bond(d);
1608 int new_value, ret = count;
1610 if (sscanf(buf, "%d", &new_value) != 1) {
1611 pr_err("%s: no lp interval value specified.\n",
1617 if (new_value <= 0) {
1618 pr_err ("%s: lp_interval must be between 1 and %d\n",
1619 bond->dev->name, INT_MAX);
1624 bond->params.lp_interval = new_value;
1629 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1630 bonding_show_lp_interval, bonding_store_lp_interval);
1632 static ssize_t bonding_show_packets_per_slave(struct device *d,
1633 struct device_attribute *attr,
1636 struct bonding *bond = to_bond(d);
1637 unsigned int packets_per_slave = bond->params.packets_per_slave;
1639 if (packets_per_slave > 1)
1640 packets_per_slave = reciprocal_value(packets_per_slave);
1642 return sprintf(buf, "%u\n", packets_per_slave);
1645 static ssize_t bonding_store_packets_per_slave(struct device *d,
1646 struct device_attribute *attr,
1647 const char *buf, size_t count)
1649 struct bonding *bond = to_bond(d);
1650 int new_value, ret = count;
1652 if (sscanf(buf, "%d", &new_value) != 1) {
1653 pr_err("%s: no packets_per_slave value specified.\n",
1658 if (new_value < 0 || new_value > USHRT_MAX) {
1659 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1660 bond->dev->name, USHRT_MAX);
1664 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1665 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1668 bond->params.packets_per_slave = reciprocal_value(new_value);
1670 bond->params.packets_per_slave = new_value;
1675 static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1676 bonding_show_packets_per_slave,
1677 bonding_store_packets_per_slave);
1679 static struct attribute *per_bond_attrs[] = {
1680 &dev_attr_slaves.attr,
1681 &dev_attr_mode.attr,
1682 &dev_attr_fail_over_mac.attr,
1683 &dev_attr_arp_validate.attr,
1684 &dev_attr_arp_all_targets.attr,
1685 &dev_attr_arp_interval.attr,
1686 &dev_attr_arp_ip_target.attr,
1687 &dev_attr_downdelay.attr,
1688 &dev_attr_updelay.attr,
1689 &dev_attr_lacp_rate.attr,
1690 &dev_attr_ad_select.attr,
1691 &dev_attr_xmit_hash_policy.attr,
1692 &dev_attr_num_grat_arp.attr,
1693 &dev_attr_num_unsol_na.attr,
1694 &dev_attr_miimon.attr,
1695 &dev_attr_primary.attr,
1696 &dev_attr_primary_reselect.attr,
1697 &dev_attr_use_carrier.attr,
1698 &dev_attr_active_slave.attr,
1699 &dev_attr_mii_status.attr,
1700 &dev_attr_ad_aggregator.attr,
1701 &dev_attr_ad_num_ports.attr,
1702 &dev_attr_ad_actor_key.attr,
1703 &dev_attr_ad_partner_key.attr,
1704 &dev_attr_ad_partner_mac.attr,
1705 &dev_attr_queue_id.attr,
1706 &dev_attr_all_slaves_active.attr,
1707 &dev_attr_resend_igmp.attr,
1708 &dev_attr_min_links.attr,
1709 &dev_attr_lp_interval.attr,
1710 &dev_attr_packets_per_slave.attr,
1714 static struct attribute_group bonding_group = {
1716 .attrs = per_bond_attrs,
1720 * Initialize sysfs. This sets up the bonding_masters file in
1723 int bond_create_sysfs(struct bond_net *bn)
1727 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1728 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1730 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1733 * Permit multiple loads of the module by ignoring failures to
1734 * create the bonding_masters sysfs file. Bonding devices
1735 * created by second or subsequent loads of the module will
1736 * not be listed in, or controllable by, bonding_masters, but
1737 * will have the usual "bonding" sysfs directory.
1739 * This is done to preserve backwards compatibility for
1740 * initscripts/sysconfig, which load bonding multiple times to
1741 * configure multiple bonding devices.
1743 if (ret == -EEXIST) {
1744 /* Is someone being kinky and naming a device bonding_master? */
1745 if (__dev_get_by_name(bn->net,
1746 class_attr_bonding_masters.attr.name))
1747 pr_err("network device named %s already exists in sysfs",
1748 class_attr_bonding_masters.attr.name);
1757 * Remove /sys/class/net/bonding_masters.
1759 void bond_destroy_sysfs(struct bond_net *bn)
1761 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
1765 * Initialize sysfs for each bond. This sets up and registers
1766 * the 'bondctl' directory for each individual bond under /sys/class/net.
1768 void bond_prepare_sysfs_group(struct bonding *bond)
1770 bond->dev->sysfs_groups[0] = &bonding_group;