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>
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 static const void *bonding_namespace(struct class *cls,
153 const struct class_attribute *attr)
155 const struct bond_net *bn =
156 container_of(attr, struct bond_net, class_attr_bonding_masters);
160 /* class attribute for bond_masters file. This ends up in /sys/class/net */
161 static const struct class_attribute class_attr_bonding_masters = {
163 .name = "bonding_masters",
164 .mode = S_IWUSR | S_IRUGO,
166 .show = bonding_show_bonds,
167 .store = bonding_store_bonds,
168 .namespace = bonding_namespace,
172 * Show the slaves in the current bond.
174 static ssize_t bonding_show_slaves(struct device *d,
175 struct device_attribute *attr, char *buf)
177 struct bonding *bond = to_bond(d);
178 struct list_head *iter;
183 return restart_syscall();
185 bond_for_each_slave(bond, slave, iter) {
186 if (res > (PAGE_SIZE - IFNAMSIZ)) {
187 /* not enough space for another interface name */
188 if ((PAGE_SIZE - res) > 10)
189 res = PAGE_SIZE - 10;
190 res += sprintf(buf + res, "++more++ ");
193 res += sprintf(buf + res, "%s ", slave->dev->name);
199 buf[res-1] = '\n'; /* eat the leftover space */
205 * Set the slaves in the current bond.
206 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
207 * All hard work should be done there.
209 static ssize_t bonding_store_slaves(struct device *d,
210 struct device_attribute *attr,
211 const char *buffer, size_t count)
213 char command[IFNAMSIZ + 1] = { 0, };
215 int res, ret = count;
216 struct net_device *dev;
217 struct bonding *bond = to_bond(d);
220 return restart_syscall();
222 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
223 ifname = command + 1;
224 if ((strlen(command) <= 1) ||
225 !dev_valid_name(ifname))
228 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
230 pr_info("%s: Interface %s does not exist!\n",
231 bond->dev->name, ifname);
236 switch (command[0]) {
238 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
239 res = bond_enslave(bond->dev, dev);
243 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
244 res = bond_release(bond->dev, dev);
256 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
265 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
266 bonding_store_slaves);
269 * Show and set the bonding mode. The bond interface must be down to
272 static ssize_t bonding_show_mode(struct device *d,
273 struct device_attribute *attr, char *buf)
275 struct bonding *bond = to_bond(d);
277 return sprintf(buf, "%s %d\n",
278 bond_mode_tbl[bond->params.mode].modename,
282 static ssize_t bonding_store_mode(struct device *d,
283 struct device_attribute *attr,
284 const char *buf, size_t count)
287 struct bonding *bond = to_bond(d);
289 new_value = bond_parse_parm(buf, bond_mode_tbl);
291 pr_err("%s: Ignoring invalid mode value %.*s.\n",
292 bond->dev->name, (int)strlen(buf) - 1, buf);
296 return restart_syscall();
298 ret = bond_option_mode_set(bond, new_value);
300 pr_info("%s: setting mode to %s (%d).\n",
301 bond->dev->name, bond_mode_tbl[new_value].modename,
309 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
310 bonding_show_mode, bonding_store_mode);
313 * Show and set the bonding transmit hash method.
315 static ssize_t bonding_show_xmit_hash(struct device *d,
316 struct device_attribute *attr,
319 struct bonding *bond = to_bond(d);
321 return sprintf(buf, "%s %d\n",
322 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
323 bond->params.xmit_policy);
326 static ssize_t bonding_store_xmit_hash(struct device *d,
327 struct device_attribute *attr,
328 const char *buf, size_t count)
330 int new_value, ret = count;
331 struct bonding *bond = to_bond(d);
333 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
335 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
337 (int)strlen(buf) - 1, buf);
340 bond->params.xmit_policy = new_value;
341 pr_info("%s: setting xmit hash policy to %s (%d).\n",
343 xmit_hashtype_tbl[new_value].modename, new_value);
348 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
349 bonding_show_xmit_hash, bonding_store_xmit_hash);
352 * Show and set arp_validate.
354 static ssize_t bonding_show_arp_validate(struct device *d,
355 struct device_attribute *attr,
358 struct bonding *bond = to_bond(d);
360 return sprintf(buf, "%s %d\n",
361 arp_validate_tbl[bond->params.arp_validate].modename,
362 bond->params.arp_validate);
365 static ssize_t bonding_store_arp_validate(struct device *d,
366 struct device_attribute *attr,
367 const char *buf, size_t count)
369 struct bonding *bond = to_bond(d);
370 int new_value, ret = count;
373 return restart_syscall();
374 new_value = bond_parse_parm(buf, arp_validate_tbl);
376 pr_err("%s: Ignoring invalid arp_validate value %s\n",
377 bond->dev->name, buf);
381 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
382 pr_err("%s: arp_validate only supported in active-backup mode.\n",
387 pr_info("%s: setting arp_validate to %s (%d).\n",
388 bond->dev->name, arp_validate_tbl[new_value].modename,
391 if (bond->dev->flags & IFF_UP) {
393 bond->recv_probe = NULL;
394 else if (bond->params.arp_interval)
395 bond->recv_probe = bond_arp_rcv;
397 bond->params.arp_validate = new_value;
404 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
405 bonding_store_arp_validate);
407 * Show and set arp_all_targets.
409 static ssize_t bonding_show_arp_all_targets(struct device *d,
410 struct device_attribute *attr,
413 struct bonding *bond = to_bond(d);
414 int value = bond->params.arp_all_targets;
416 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
420 static ssize_t bonding_store_arp_all_targets(struct device *d,
421 struct device_attribute *attr,
422 const char *buf, size_t count)
424 struct bonding *bond = to_bond(d);
427 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
429 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
430 bond->dev->name, buf);
433 pr_info("%s: setting arp_all_targets to %s (%d).\n",
434 bond->dev->name, arp_all_targets_tbl[new_value].modename,
437 bond->params.arp_all_targets = new_value;
442 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
443 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
446 * Show and store fail_over_mac. User only allowed to change the
447 * value when there are no slaves.
449 static ssize_t bonding_show_fail_over_mac(struct device *d,
450 struct device_attribute *attr,
453 struct bonding *bond = to_bond(d);
455 return sprintf(buf, "%s %d\n",
456 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
457 bond->params.fail_over_mac);
460 static ssize_t bonding_store_fail_over_mac(struct device *d,
461 struct device_attribute *attr,
462 const char *buf, size_t count)
464 int new_value, ret = count;
465 struct bonding *bond = to_bond(d);
468 return restart_syscall();
470 if (bond_has_slaves(bond)) {
471 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
477 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
479 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
480 bond->dev->name, buf);
485 bond->params.fail_over_mac = new_value;
486 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
487 bond->dev->name, fail_over_mac_tbl[new_value].modename,
495 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
496 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
499 * Show and set the arp timer interval. There are two tricky bits
500 * here. First, if ARP monitoring is activated, then we must disable
501 * MII monitoring. Second, if the ARP timer isn't running, we must
504 static ssize_t bonding_show_arp_interval(struct device *d,
505 struct device_attribute *attr,
508 struct bonding *bond = to_bond(d);
510 return sprintf(buf, "%d\n", bond->params.arp_interval);
513 static ssize_t bonding_store_arp_interval(struct device *d,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
517 struct bonding *bond = to_bond(d);
518 int new_value, ret = count;
521 return restart_syscall();
522 if (sscanf(buf, "%d", &new_value) != 1) {
523 pr_err("%s: no arp_interval value specified.\n",
529 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
530 bond->dev->name, new_value, INT_MAX);
534 if (bond->params.mode == BOND_MODE_ALB ||
535 bond->params.mode == BOND_MODE_TLB) {
536 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
537 bond->dev->name, bond->dev->name);
541 pr_info("%s: Setting ARP monitoring interval to %d.\n",
542 bond->dev->name, new_value);
543 bond->params.arp_interval = new_value;
545 if (bond->params.miimon) {
546 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
547 bond->dev->name, bond->dev->name);
548 bond->params.miimon = 0;
550 if (!bond->params.arp_targets[0])
551 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
554 if (bond->dev->flags & IFF_UP) {
555 /* If the interface is up, we may need to fire off
556 * the ARP timer. If the interface is down, the
557 * timer will get fired off when the open function
561 if (bond->params.arp_validate)
562 bond->recv_probe = NULL;
563 cancel_delayed_work_sync(&bond->arp_work);
565 /* arp_validate can be set only in active-backup mode */
566 if (bond->params.arp_validate)
567 bond->recv_probe = bond_arp_rcv;
568 cancel_delayed_work_sync(&bond->mii_work);
569 queue_delayed_work(bond->wq, &bond->arp_work, 0);
576 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
577 bonding_show_arp_interval, bonding_store_arp_interval);
580 * Show and set the arp targets.
582 static ssize_t bonding_show_arp_targets(struct device *d,
583 struct device_attribute *attr,
587 struct bonding *bond = to_bond(d);
589 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
590 if (bond->params.arp_targets[i])
591 res += sprintf(buf + res, "%pI4 ",
592 &bond->params.arp_targets[i]);
595 buf[res-1] = '\n'; /* eat the leftover space */
599 static ssize_t bonding_store_arp_targets(struct device *d,
600 struct device_attribute *attr,
601 const char *buf, size_t count)
603 struct bonding *bond = to_bond(d);
604 struct list_head *iter;
606 __be32 newtarget, *targets;
607 unsigned long *targets_rx;
608 int ind, i, j, ret = -EINVAL;
611 return restart_syscall();
613 targets = bond->params.arp_targets;
614 newtarget = in_aton(buf + 1);
617 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
618 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
619 bond->dev->name, &newtarget);
623 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
624 pr_err("%s: ARP target %pI4 is already present\n",
625 bond->dev->name, &newtarget);
629 ind = bond_get_targets_ip(targets, 0); /* first free slot */
631 pr_err("%s: ARP target table is full!\n",
636 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
638 /* not to race with bond_arp_rcv */
639 write_lock_bh(&bond->lock);
640 bond_for_each_slave(bond, slave, iter)
641 slave->target_last_arp_rx[ind] = jiffies;
642 targets[ind] = newtarget;
643 write_unlock_bh(&bond->lock);
644 } else if (buf[0] == '-') {
645 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
646 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
647 bond->dev->name, &newtarget);
651 ind = bond_get_targets_ip(targets, newtarget);
653 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
654 bond->dev->name, &newtarget);
658 if (ind == 0 && !targets[1] && bond->params.arp_interval)
659 pr_warn("%s: removing last arp target with arp_interval on\n",
662 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
665 write_lock_bh(&bond->lock);
666 bond_for_each_slave(bond, slave, iter) {
667 targets_rx = slave->target_last_arp_rx;
669 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
670 targets_rx[j] = targets_rx[j+1];
673 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
674 targets[i] = targets[i+1];
676 write_unlock_bh(&bond->lock);
678 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
689 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
692 * Show and set the up and down delays. These must be multiples of the
693 * MII monitoring value, and are stored internally as the multiplier.
694 * Thus, we must translate to MS for the real world.
696 static ssize_t bonding_show_downdelay(struct device *d,
697 struct device_attribute *attr,
700 struct bonding *bond = to_bond(d);
702 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
705 static ssize_t bonding_store_downdelay(struct device *d,
706 struct device_attribute *attr,
707 const char *buf, size_t count)
709 int new_value, ret = count;
710 struct bonding *bond = to_bond(d);
712 if (!(bond->params.miimon)) {
713 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
719 if (sscanf(buf, "%d", &new_value) != 1) {
720 pr_err("%s: no down delay value specified.\n", bond->dev->name);
725 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
726 bond->dev->name, new_value, 0, INT_MAX);
730 if ((new_value % bond->params.miimon) != 0) {
731 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
732 bond->dev->name, new_value,
734 (new_value / bond->params.miimon) *
735 bond->params.miimon);
737 bond->params.downdelay = new_value / bond->params.miimon;
738 pr_info("%s: Setting down delay to %d.\n",
740 bond->params.downdelay * bond->params.miimon);
747 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
748 bonding_show_downdelay, bonding_store_downdelay);
750 static ssize_t bonding_show_updelay(struct device *d,
751 struct device_attribute *attr,
754 struct bonding *bond = to_bond(d);
756 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
760 static ssize_t bonding_store_updelay(struct device *d,
761 struct device_attribute *attr,
762 const char *buf, size_t count)
764 int new_value, ret = count;
765 struct bonding *bond = to_bond(d);
767 if (!(bond->params.miimon)) {
768 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
774 if (sscanf(buf, "%d", &new_value) != 1) {
775 pr_err("%s: no up delay value specified.\n",
781 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
782 bond->dev->name, new_value, 0, INT_MAX);
786 if ((new_value % bond->params.miimon) != 0) {
787 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
788 bond->dev->name, new_value,
790 (new_value / bond->params.miimon) *
791 bond->params.miimon);
793 bond->params.updelay = new_value / bond->params.miimon;
794 pr_info("%s: Setting up delay to %d.\n",
796 bond->params.updelay * bond->params.miimon);
802 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
803 bonding_show_updelay, bonding_store_updelay);
806 * Show and set the LACP interval. Interface must be down, and the mode
807 * must be set to 802.3ad mode.
809 static ssize_t bonding_show_lacp(struct device *d,
810 struct device_attribute *attr,
813 struct bonding *bond = to_bond(d);
815 return sprintf(buf, "%s %d\n",
816 bond_lacp_tbl[bond->params.lacp_fast].modename,
817 bond->params.lacp_fast);
820 static ssize_t bonding_store_lacp(struct device *d,
821 struct device_attribute *attr,
822 const char *buf, size_t count)
824 struct bonding *bond = to_bond(d);
825 int new_value, ret = count;
828 return restart_syscall();
830 if (bond->dev->flags & IFF_UP) {
831 pr_err("%s: Unable to update LACP rate because interface is up.\n",
837 if (bond->params.mode != BOND_MODE_8023AD) {
838 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
844 new_value = bond_parse_parm(buf, bond_lacp_tbl);
846 if ((new_value == 1) || (new_value == 0)) {
847 bond->params.lacp_fast = new_value;
848 bond_3ad_update_lacp_rate(bond);
849 pr_info("%s: Setting LACP rate to %s (%d).\n",
850 bond->dev->name, bond_lacp_tbl[new_value].modename,
853 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
854 bond->dev->name, (int)strlen(buf) - 1, buf);
862 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
863 bonding_show_lacp, bonding_store_lacp);
865 static ssize_t bonding_show_min_links(struct device *d,
866 struct device_attribute *attr,
869 struct bonding *bond = to_bond(d);
871 return sprintf(buf, "%d\n", bond->params.min_links);
874 static ssize_t bonding_store_min_links(struct device *d,
875 struct device_attribute *attr,
876 const char *buf, size_t count)
878 struct bonding *bond = to_bond(d);
880 unsigned int new_value;
882 ret = kstrtouint(buf, 0, &new_value);
884 pr_err("%s: Ignoring invalid min links value %s.\n",
885 bond->dev->name, buf);
889 pr_info("%s: Setting min links value to %u\n",
890 bond->dev->name, new_value);
891 bond->params.min_links = new_value;
894 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
895 bonding_show_min_links, bonding_store_min_links);
897 static ssize_t bonding_show_ad_select(struct device *d,
898 struct device_attribute *attr,
901 struct bonding *bond = to_bond(d);
903 return sprintf(buf, "%s %d\n",
904 ad_select_tbl[bond->params.ad_select].modename,
905 bond->params.ad_select);
909 static ssize_t bonding_store_ad_select(struct device *d,
910 struct device_attribute *attr,
911 const char *buf, size_t count)
913 int new_value, ret = count;
914 struct bonding *bond = to_bond(d);
916 if (bond->dev->flags & IFF_UP) {
917 pr_err("%s: Unable to update ad_select because interface is up.\n",
923 new_value = bond_parse_parm(buf, ad_select_tbl);
925 if (new_value != -1) {
926 bond->params.ad_select = new_value;
927 pr_info("%s: Setting ad_select to %s (%d).\n",
928 bond->dev->name, ad_select_tbl[new_value].modename,
931 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
932 bond->dev->name, (int)strlen(buf) - 1, buf);
938 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
939 bonding_show_ad_select, bonding_store_ad_select);
942 * Show and set the number of peer notifications to send after a failover event.
944 static ssize_t bonding_show_num_peer_notif(struct device *d,
945 struct device_attribute *attr,
948 struct bonding *bond = to_bond(d);
949 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
952 static ssize_t bonding_store_num_peer_notif(struct device *d,
953 struct device_attribute *attr,
954 const char *buf, size_t count)
956 struct bonding *bond = to_bond(d);
957 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
958 return err ? err : count;
960 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
961 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
962 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
963 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
966 * Show and set the MII monitor interval. There are two tricky bits
967 * here. First, if MII monitoring is activated, then we must disable
968 * ARP monitoring. Second, if the timer isn't running, we must
971 static ssize_t bonding_show_miimon(struct device *d,
972 struct device_attribute *attr,
975 struct bonding *bond = to_bond(d);
977 return sprintf(buf, "%d\n", bond->params.miimon);
980 static ssize_t bonding_store_miimon(struct device *d,
981 struct device_attribute *attr,
982 const char *buf, size_t count)
984 int new_value, ret = count;
985 struct bonding *bond = to_bond(d);
988 return restart_syscall();
989 if (sscanf(buf, "%d", &new_value) != 1) {
990 pr_err("%s: no miimon value specified.\n",
996 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
997 bond->dev->name, new_value, 0, INT_MAX);
1001 pr_info("%s: Setting MII monitoring interval to %d.\n",
1002 bond->dev->name, new_value);
1003 bond->params.miimon = new_value;
1004 if (bond->params.updelay)
1005 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1007 bond->params.updelay * bond->params.miimon);
1008 if (bond->params.downdelay)
1009 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1011 bond->params.downdelay * bond->params.miimon);
1012 if (new_value && bond->params.arp_interval) {
1013 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1015 bond->params.arp_interval = 0;
1016 if (bond->params.arp_validate)
1017 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1019 if (bond->dev->flags & IFF_UP) {
1020 /* If the interface is up, we may need to fire off
1021 * the MII timer. If the interface is down, the
1022 * timer will get fired off when the open function
1026 cancel_delayed_work_sync(&bond->mii_work);
1028 cancel_delayed_work_sync(&bond->arp_work);
1029 queue_delayed_work(bond->wq, &bond->mii_work, 0);
1036 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1037 bonding_show_miimon, bonding_store_miimon);
1040 * Show and set the primary slave. The store function is much
1041 * simpler than bonding_store_slaves function because it only needs to
1042 * handle one interface name.
1043 * The bond must be a mode that supports a primary for this be
1046 static ssize_t bonding_show_primary(struct device *d,
1047 struct device_attribute *attr,
1051 struct bonding *bond = to_bond(d);
1053 if (bond->primary_slave)
1054 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1059 static ssize_t bonding_store_primary(struct device *d,
1060 struct device_attribute *attr,
1061 const char *buf, size_t count)
1063 struct bonding *bond = to_bond(d);
1064 struct list_head *iter;
1065 char ifname[IFNAMSIZ];
1066 struct slave *slave;
1068 if (!rtnl_trylock())
1069 return restart_syscall();
1071 read_lock(&bond->lock);
1072 write_lock_bh(&bond->curr_slave_lock);
1074 if (!USES_PRIMARY(bond->params.mode)) {
1075 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1076 bond->dev->name, bond->dev->name, bond->params.mode);
1080 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1082 /* check to see if we are clearing primary */
1083 if (!strlen(ifname) || buf[0] == '\n') {
1084 pr_info("%s: Setting primary slave to None.\n",
1086 bond->primary_slave = NULL;
1087 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1088 bond_select_active_slave(bond);
1092 bond_for_each_slave(bond, slave, iter) {
1093 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1094 pr_info("%s: Setting %s as primary slave.\n",
1095 bond->dev->name, slave->dev->name);
1096 bond->primary_slave = slave;
1097 strcpy(bond->params.primary, slave->dev->name);
1098 bond_select_active_slave(bond);
1103 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1104 bond->params.primary[IFNAMSIZ - 1] = 0;
1106 pr_info("%s: Recording %s as primary, "
1107 "but it has not been enslaved to %s yet.\n",
1108 bond->dev->name, ifname, bond->dev->name);
1110 write_unlock_bh(&bond->curr_slave_lock);
1111 read_unlock(&bond->lock);
1112 unblock_netpoll_tx();
1117 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1118 bonding_show_primary, bonding_store_primary);
1121 * Show and set the primary_reselect flag.
1123 static ssize_t bonding_show_primary_reselect(struct device *d,
1124 struct device_attribute *attr,
1127 struct bonding *bond = to_bond(d);
1129 return sprintf(buf, "%s %d\n",
1130 pri_reselect_tbl[bond->params.primary_reselect].modename,
1131 bond->params.primary_reselect);
1134 static ssize_t bonding_store_primary_reselect(struct device *d,
1135 struct device_attribute *attr,
1136 const char *buf, size_t count)
1138 int new_value, ret = count;
1139 struct bonding *bond = to_bond(d);
1141 if (!rtnl_trylock())
1142 return restart_syscall();
1144 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1145 if (new_value < 0) {
1146 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1148 (int) strlen(buf) - 1, buf);
1153 bond->params.primary_reselect = new_value;
1154 pr_info("%s: setting primary_reselect to %s (%d).\n",
1155 bond->dev->name, pri_reselect_tbl[new_value].modename,
1159 read_lock(&bond->lock);
1160 write_lock_bh(&bond->curr_slave_lock);
1161 bond_select_active_slave(bond);
1162 write_unlock_bh(&bond->curr_slave_lock);
1163 read_unlock(&bond->lock);
1164 unblock_netpoll_tx();
1169 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1170 bonding_show_primary_reselect,
1171 bonding_store_primary_reselect);
1174 * Show and set the use_carrier flag.
1176 static ssize_t bonding_show_carrier(struct device *d,
1177 struct device_attribute *attr,
1180 struct bonding *bond = to_bond(d);
1182 return sprintf(buf, "%d\n", bond->params.use_carrier);
1185 static ssize_t bonding_store_carrier(struct device *d,
1186 struct device_attribute *attr,
1187 const char *buf, size_t count)
1189 int new_value, ret = count;
1190 struct bonding *bond = to_bond(d);
1193 if (sscanf(buf, "%d", &new_value) != 1) {
1194 pr_err("%s: no use_carrier value specified.\n",
1199 if ((new_value == 0) || (new_value == 1)) {
1200 bond->params.use_carrier = new_value;
1201 pr_info("%s: Setting use_carrier to %d.\n",
1202 bond->dev->name, new_value);
1204 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1205 bond->dev->name, new_value);
1210 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1211 bonding_show_carrier, bonding_store_carrier);
1215 * Show and set currently active_slave.
1217 static ssize_t bonding_show_active_slave(struct device *d,
1218 struct device_attribute *attr,
1221 struct bonding *bond = to_bond(d);
1222 struct net_device *slave_dev;
1226 slave_dev = bond_option_active_slave_get_rcu(bond);
1228 count = sprintf(buf, "%s\n", slave_dev->name);
1234 static ssize_t bonding_store_active_slave(struct device *d,
1235 struct device_attribute *attr,
1236 const char *buf, size_t count)
1239 struct bonding *bond = to_bond(d);
1240 char ifname[IFNAMSIZ];
1241 struct net_device *dev;
1243 if (!rtnl_trylock())
1244 return restart_syscall();
1246 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1247 if (!strlen(ifname) || buf[0] == '\n') {
1250 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1257 ret = bond_option_active_slave_set(bond, dev);
1267 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1268 bonding_show_active_slave, bonding_store_active_slave);
1272 * Show link status of the bond interface.
1274 static ssize_t bonding_show_mii_status(struct device *d,
1275 struct device_attribute *attr,
1278 struct bonding *bond = to_bond(d);
1280 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1282 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1285 * Show current 802.3ad aggregator ID.
1287 static ssize_t bonding_show_ad_aggregator(struct device *d,
1288 struct device_attribute *attr,
1292 struct bonding *bond = to_bond(d);
1294 if (bond->params.mode == BOND_MODE_8023AD) {
1295 struct ad_info ad_info;
1296 count = sprintf(buf, "%d\n",
1297 bond_3ad_get_active_agg_info(bond, &ad_info)
1298 ? 0 : ad_info.aggregator_id);
1303 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1307 * Show number of active 802.3ad ports.
1309 static ssize_t bonding_show_ad_num_ports(struct device *d,
1310 struct device_attribute *attr,
1314 struct bonding *bond = to_bond(d);
1316 if (bond->params.mode == BOND_MODE_8023AD) {
1317 struct ad_info ad_info;
1318 count = sprintf(buf, "%d\n",
1319 bond_3ad_get_active_agg_info(bond, &ad_info)
1320 ? 0 : ad_info.ports);
1325 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1329 * Show current 802.3ad actor key.
1331 static ssize_t bonding_show_ad_actor_key(struct device *d,
1332 struct device_attribute *attr,
1336 struct bonding *bond = to_bond(d);
1338 if (bond->params.mode == BOND_MODE_8023AD) {
1339 struct ad_info ad_info;
1340 count = sprintf(buf, "%d\n",
1341 bond_3ad_get_active_agg_info(bond, &ad_info)
1342 ? 0 : ad_info.actor_key);
1347 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1351 * Show current 802.3ad partner key.
1353 static ssize_t bonding_show_ad_partner_key(struct device *d,
1354 struct device_attribute *attr,
1358 struct bonding *bond = to_bond(d);
1360 if (bond->params.mode == BOND_MODE_8023AD) {
1361 struct ad_info ad_info;
1362 count = sprintf(buf, "%d\n",
1363 bond_3ad_get_active_agg_info(bond, &ad_info)
1364 ? 0 : ad_info.partner_key);
1369 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1373 * Show current 802.3ad partner mac.
1375 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1376 struct device_attribute *attr,
1380 struct bonding *bond = to_bond(d);
1382 if (bond->params.mode == BOND_MODE_8023AD) {
1383 struct ad_info ad_info;
1384 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1385 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1390 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1393 * Show the queue_ids of the slaves in the current bond.
1395 static ssize_t bonding_show_queue_id(struct device *d,
1396 struct device_attribute *attr,
1399 struct bonding *bond = to_bond(d);
1400 struct list_head *iter;
1401 struct slave *slave;
1404 if (!rtnl_trylock())
1405 return restart_syscall();
1407 bond_for_each_slave(bond, slave, iter) {
1408 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1409 /* not enough space for another interface_name:queue_id pair */
1410 if ((PAGE_SIZE - res) > 10)
1411 res = PAGE_SIZE - 10;
1412 res += sprintf(buf + res, "++more++ ");
1415 res += sprintf(buf + res, "%s:%d ",
1416 slave->dev->name, slave->queue_id);
1419 buf[res-1] = '\n'; /* eat the leftover space */
1427 * Set the queue_ids of the slaves in the current bond. The bond
1428 * interface must be enslaved for this to work.
1430 static ssize_t bonding_store_queue_id(struct device *d,
1431 struct device_attribute *attr,
1432 const char *buffer, size_t count)
1434 struct slave *slave, *update_slave;
1435 struct bonding *bond = to_bond(d);
1436 struct list_head *iter;
1440 struct net_device *sdev = NULL;
1442 if (!rtnl_trylock())
1443 return restart_syscall();
1445 /* delim will point to queue id if successful */
1446 delim = strchr(buffer, ':');
1451 * Terminate string that points to device name and bump it
1452 * up one, so we can read the queue id there.
1455 if (sscanf(++delim, "%hd\n", &qid) != 1)
1458 /* Check buffer length, valid ifname and queue id */
1459 if (strlen(buffer) > IFNAMSIZ ||
1460 !dev_valid_name(buffer) ||
1461 qid > bond->dev->real_num_tx_queues)
1464 /* Get the pointer to that interface if it exists */
1465 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1469 /* Search for thes slave and check for duplicate qids */
1470 update_slave = NULL;
1471 bond_for_each_slave(bond, slave, iter) {
1472 if (sdev == slave->dev)
1474 * We don't need to check the matching
1475 * slave for dups, since we're overwriting it
1477 update_slave = slave;
1478 else if (qid && qid == slave->queue_id) {
1486 /* Actually set the qids for the slave */
1487 update_slave->queue_id = qid;
1494 pr_info("invalid input for queue_id set for %s.\n",
1500 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1501 bonding_store_queue_id);
1505 * Show and set the all_slaves_active flag.
1507 static ssize_t bonding_show_slaves_active(struct device *d,
1508 struct device_attribute *attr,
1511 struct bonding *bond = to_bond(d);
1513 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1516 static ssize_t bonding_store_slaves_active(struct device *d,
1517 struct device_attribute *attr,
1518 const char *buf, size_t count)
1520 struct bonding *bond = to_bond(d);
1521 int new_value, ret = count;
1522 struct list_head *iter;
1523 struct slave *slave;
1525 if (!rtnl_trylock())
1526 return restart_syscall();
1528 if (sscanf(buf, "%d", &new_value) != 1) {
1529 pr_err("%s: no all_slaves_active value specified.\n",
1535 if (new_value == bond->params.all_slaves_active)
1538 if ((new_value == 0) || (new_value == 1)) {
1539 bond->params.all_slaves_active = new_value;
1541 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1542 bond->dev->name, new_value);
1547 bond_for_each_slave(bond, slave, iter) {
1548 if (!bond_is_active_slave(slave)) {
1550 slave->inactive = 0;
1552 slave->inactive = 1;
1559 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1560 bonding_show_slaves_active, bonding_store_slaves_active);
1563 * Show and set the number of IGMP membership reports to send on link failure
1565 static ssize_t bonding_show_resend_igmp(struct device *d,
1566 struct device_attribute *attr,
1569 struct bonding *bond = to_bond(d);
1571 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1574 static ssize_t bonding_store_resend_igmp(struct device *d,
1575 struct device_attribute *attr,
1576 const char *buf, size_t count)
1578 int new_value, ret = count;
1579 struct bonding *bond = to_bond(d);
1581 if (sscanf(buf, "%d", &new_value) != 1) {
1582 pr_err("%s: no resend_igmp value specified.\n",
1588 if (new_value < 0 || new_value > 255) {
1589 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1590 bond->dev->name, new_value);
1595 pr_info("%s: Setting resend_igmp to %d.\n",
1596 bond->dev->name, new_value);
1597 bond->params.resend_igmp = new_value;
1602 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1603 bonding_show_resend_igmp, bonding_store_resend_igmp);
1606 static ssize_t bonding_show_lp_interval(struct device *d,
1607 struct device_attribute *attr,
1610 struct bonding *bond = to_bond(d);
1611 return sprintf(buf, "%d\n", bond->params.lp_interval);
1614 static ssize_t bonding_store_lp_interval(struct device *d,
1615 struct device_attribute *attr,
1616 const char *buf, size_t count)
1618 struct bonding *bond = to_bond(d);
1619 int new_value, ret = count;
1621 if (sscanf(buf, "%d", &new_value) != 1) {
1622 pr_err("%s: no lp interval value specified.\n",
1628 if (new_value <= 0) {
1629 pr_err ("%s: lp_interval must be between 1 and %d\n",
1630 bond->dev->name, INT_MAX);
1635 bond->params.lp_interval = new_value;
1640 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1641 bonding_show_lp_interval, bonding_store_lp_interval);
1643 static struct attribute *per_bond_attrs[] = {
1644 &dev_attr_slaves.attr,
1645 &dev_attr_mode.attr,
1646 &dev_attr_fail_over_mac.attr,
1647 &dev_attr_arp_validate.attr,
1648 &dev_attr_arp_all_targets.attr,
1649 &dev_attr_arp_interval.attr,
1650 &dev_attr_arp_ip_target.attr,
1651 &dev_attr_downdelay.attr,
1652 &dev_attr_updelay.attr,
1653 &dev_attr_lacp_rate.attr,
1654 &dev_attr_ad_select.attr,
1655 &dev_attr_xmit_hash_policy.attr,
1656 &dev_attr_num_grat_arp.attr,
1657 &dev_attr_num_unsol_na.attr,
1658 &dev_attr_miimon.attr,
1659 &dev_attr_primary.attr,
1660 &dev_attr_primary_reselect.attr,
1661 &dev_attr_use_carrier.attr,
1662 &dev_attr_active_slave.attr,
1663 &dev_attr_mii_status.attr,
1664 &dev_attr_ad_aggregator.attr,
1665 &dev_attr_ad_num_ports.attr,
1666 &dev_attr_ad_actor_key.attr,
1667 &dev_attr_ad_partner_key.attr,
1668 &dev_attr_ad_partner_mac.attr,
1669 &dev_attr_queue_id.attr,
1670 &dev_attr_all_slaves_active.attr,
1671 &dev_attr_resend_igmp.attr,
1672 &dev_attr_min_links.attr,
1673 &dev_attr_lp_interval.attr,
1677 static struct attribute_group bonding_group = {
1679 .attrs = per_bond_attrs,
1683 * Initialize sysfs. This sets up the bonding_masters file in
1686 int bond_create_sysfs(struct bond_net *bn)
1690 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1691 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1693 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
1695 * Permit multiple loads of the module by ignoring failures to
1696 * create the bonding_masters sysfs file. Bonding devices
1697 * created by second or subsequent loads of the module will
1698 * not be listed in, or controllable by, bonding_masters, but
1699 * will have the usual "bonding" sysfs directory.
1701 * This is done to preserve backwards compatibility for
1702 * initscripts/sysconfig, which load bonding multiple times to
1703 * configure multiple bonding devices.
1705 if (ret == -EEXIST) {
1706 /* Is someone being kinky and naming a device bonding_master? */
1707 if (__dev_get_by_name(bn->net,
1708 class_attr_bonding_masters.attr.name))
1709 pr_err("network device named %s already exists in sysfs",
1710 class_attr_bonding_masters.attr.name);
1719 * Remove /sys/class/net/bonding_masters.
1721 void bond_destroy_sysfs(struct bond_net *bn)
1723 netdev_class_remove_file(&bn->class_attr_bonding_masters);
1727 * Initialize sysfs for each bond. This sets up and registers
1728 * the 'bondctl' directory for each individual bond under /sys/class/net.
1730 void bond_prepare_sysfs_group(struct bonding *bond)
1732 bond->dev->sysfs_groups[0] = &bonding_group;