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>
29 #include <linux/sysdev.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/netdevice.h>
34 #include <linux/inetdevice.h>
36 #include <linux/sysfs.h>
37 #include <linux/ctype.h>
38 #include <linux/inet.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/etherdevice.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <linux/nsproxy.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,
58 struct net *net = current->nsproxy->net_ns;
59 struct bond_net *bn = net_generic(net, bond_net_id);
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 net *net, const char *ifname)
84 struct bond_net *bn = net_generic(net, bond_net_id);
87 list_for_each_entry(bond, &bn->dev_list, bond_list) {
88 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
95 * "store" function for the bond_masters attribute. This is what
96 * creates and deletes entire bonds.
98 * The class parameter is ignored.
102 static ssize_t bonding_store_bonds(struct class *cls,
103 struct class_attribute *attr,
104 const char *buffer, size_t count)
106 struct net *net = current->nsproxy->net_ns;
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(net, ifname);
121 pr_info("Bond creation failed.\n");
124 } else if (command[0] == '-') {
125 struct net_device *bond_dev;
128 bond_dev = bond_get_by_name(net, ifname);
130 pr_info("%s is being deleted...\n", ifname);
131 unregister_netdevice(bond_dev);
133 pr_err("unable to delete non-existent %s\n", ifname);
140 /* Always return either count or an error. If you return 0, you'll
141 * get called forever, which is bad.
146 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
150 /* class attribute for bond_masters file. This ends up in /sys/class/net */
151 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
152 bonding_show_bonds, bonding_store_bonds);
154 int bond_create_slave_symlinks(struct net_device *master,
155 struct net_device *slave)
157 char linkname[IFNAMSIZ+7];
160 /* first, create a link from the slave back to the master */
161 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
165 /* next, create a link from the master to the slave */
166 sprintf(linkname, "slave_%s", slave->name);
167 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
173 void bond_destroy_slave_symlinks(struct net_device *master,
174 struct net_device *slave)
176 char linkname[IFNAMSIZ+7];
178 sysfs_remove_link(&(slave->dev.kobj), "master");
179 sprintf(linkname, "slave_%s", slave->name);
180 sysfs_remove_link(&(master->dev.kobj), linkname);
185 * Show the slaves in the current bond.
187 static ssize_t bonding_show_slaves(struct device *d,
188 struct device_attribute *attr, char *buf)
192 struct bonding *bond = to_bond(d);
194 read_lock(&bond->lock);
195 bond_for_each_slave(bond, slave, i) {
196 if (res > (PAGE_SIZE - IFNAMSIZ)) {
197 /* not enough space for another interface name */
198 if ((PAGE_SIZE - res) > 10)
199 res = PAGE_SIZE - 10;
200 res += sprintf(buf + res, "++more++ ");
203 res += sprintf(buf + res, "%s ", slave->dev->name);
205 read_unlock(&bond->lock);
207 buf[res-1] = '\n'; /* eat the leftover space */
212 * Set the slaves in the current bond. The bond interface must be
213 * up for this to succeed.
214 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
215 * All hard work should be done there.
217 static ssize_t bonding_store_slaves(struct device *d,
218 struct device_attribute *attr,
219 const char *buffer, size_t count)
221 char command[IFNAMSIZ + 1] = { 0, };
223 int res, ret = count;
224 struct net_device *dev;
225 struct bonding *bond = to_bond(d);
227 /* Quick sanity check -- is the bond interface up? */
228 if (!(bond->dev->flags & IFF_UP)) {
229 pr_warning("%s: doing slave updates when interface is down.\n",
234 return restart_syscall();
236 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
237 ifname = command + 1;
238 if ((strlen(command) <= 1) ||
239 !dev_valid_name(ifname))
242 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
244 pr_info("%s: Interface %s does not exist!\n",
245 bond->dev->name, ifname);
250 switch (command[0]) {
252 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
253 res = bond_enslave(bond->dev, dev);
257 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
258 res = bond_release(bond->dev, dev);
270 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
279 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
280 bonding_store_slaves);
283 * Show and set the bonding mode. The bond interface must be down to
286 static ssize_t bonding_show_mode(struct device *d,
287 struct device_attribute *attr, char *buf)
289 struct bonding *bond = to_bond(d);
291 return sprintf(buf, "%s %d\n",
292 bond_mode_tbl[bond->params.mode].modename,
296 static ssize_t bonding_store_mode(struct device *d,
297 struct device_attribute *attr,
298 const char *buf, size_t count)
300 int new_value, ret = count;
301 struct bonding *bond = to_bond(d);
303 if (bond->dev->flags & IFF_UP) {
304 pr_err("unable to update mode of %s because interface is up.\n",
310 new_value = bond_parse_parm(buf, bond_mode_tbl);
312 pr_err("%s: Ignoring invalid mode value %.*s.\n",
313 bond->dev->name, (int)strlen(buf) - 1, buf);
317 if (bond->params.mode == BOND_MODE_8023AD)
318 bond_unset_master_3ad_flags(bond);
320 if (bond->params.mode == BOND_MODE_ALB)
321 bond_unset_master_alb_flags(bond);
323 bond->params.mode = new_value;
324 bond_set_mode_ops(bond, bond->params.mode);
325 pr_info("%s: setting mode to %s (%d).\n",
326 bond->dev->name, bond_mode_tbl[new_value].modename,
332 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
333 bonding_show_mode, bonding_store_mode);
336 * Show and set the bonding transmit hash method.
337 * The bond interface must be down to change the xmit hash policy.
339 static ssize_t bonding_show_xmit_hash(struct device *d,
340 struct device_attribute *attr,
343 struct bonding *bond = to_bond(d);
345 return sprintf(buf, "%s %d\n",
346 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
347 bond->params.xmit_policy);
350 static ssize_t bonding_store_xmit_hash(struct device *d,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
354 int new_value, ret = count;
355 struct bonding *bond = to_bond(d);
357 if (bond->dev->flags & IFF_UP) {
358 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
364 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
366 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
368 (int)strlen(buf) - 1, buf);
372 bond->params.xmit_policy = new_value;
373 bond_set_mode_ops(bond, bond->params.mode);
374 pr_info("%s: setting xmit hash policy to %s (%d).\n",
376 xmit_hashtype_tbl[new_value].modename, new_value);
381 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
382 bonding_show_xmit_hash, bonding_store_xmit_hash);
385 * Show and set arp_validate.
387 static ssize_t bonding_show_arp_validate(struct device *d,
388 struct device_attribute *attr,
391 struct bonding *bond = to_bond(d);
393 return sprintf(buf, "%s %d\n",
394 arp_validate_tbl[bond->params.arp_validate].modename,
395 bond->params.arp_validate);
398 static ssize_t bonding_store_arp_validate(struct device *d,
399 struct device_attribute *attr,
400 const char *buf, size_t count)
403 struct bonding *bond = to_bond(d);
405 new_value = bond_parse_parm(buf, arp_validate_tbl);
407 pr_err("%s: Ignoring invalid arp_validate value %s\n",
408 bond->dev->name, buf);
411 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
412 pr_err("%s: arp_validate only supported in active-backup mode.\n",
416 pr_info("%s: setting arp_validate to %s (%d).\n",
417 bond->dev->name, arp_validate_tbl[new_value].modename,
420 if (!bond->params.arp_validate && new_value)
421 bond_register_arp(bond);
422 else if (bond->params.arp_validate && !new_value)
423 bond_unregister_arp(bond);
425 bond->params.arp_validate = new_value;
430 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
431 bonding_store_arp_validate);
434 * Show and store fail_over_mac. User only allowed to change the
435 * value when there are no slaves.
437 static ssize_t bonding_show_fail_over_mac(struct device *d,
438 struct device_attribute *attr,
441 struct bonding *bond = to_bond(d);
443 return sprintf(buf, "%s %d\n",
444 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
445 bond->params.fail_over_mac);
448 static ssize_t bonding_store_fail_over_mac(struct device *d,
449 struct device_attribute *attr,
450 const char *buf, size_t count)
453 struct bonding *bond = to_bond(d);
455 if (bond->slave_cnt != 0) {
456 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
461 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
463 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
464 bond->dev->name, buf);
468 bond->params.fail_over_mac = new_value;
469 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
470 bond->dev->name, fail_over_mac_tbl[new_value].modename,
476 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
477 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
480 * Show and set the arp timer interval. There are two tricky bits
481 * here. First, if ARP monitoring is activated, then we must disable
482 * MII monitoring. Second, if the ARP timer isn't running, we must
485 static ssize_t bonding_show_arp_interval(struct device *d,
486 struct device_attribute *attr,
489 struct bonding *bond = to_bond(d);
491 return sprintf(buf, "%d\n", bond->params.arp_interval);
494 static ssize_t bonding_store_arp_interval(struct device *d,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
498 int new_value, ret = count;
499 struct bonding *bond = to_bond(d);
501 if (sscanf(buf, "%d", &new_value) != 1) {
502 pr_err("%s: no arp_interval value specified.\n",
508 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
509 bond->dev->name, new_value, INT_MAX);
514 pr_info("%s: Setting ARP monitoring interval to %d.\n",
515 bond->dev->name, new_value);
516 bond->params.arp_interval = new_value;
517 if (bond->params.arp_interval)
518 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
519 if (bond->params.miimon) {
520 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
521 bond->dev->name, bond->dev->name);
522 bond->params.miimon = 0;
523 if (delayed_work_pending(&bond->mii_work)) {
524 cancel_delayed_work(&bond->mii_work);
525 flush_workqueue(bond->wq);
528 if (!bond->params.arp_targets[0]) {
529 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
532 if (bond->dev->flags & IFF_UP) {
533 /* If the interface is up, we may need to fire off
534 * the ARP timer. If the interface is down, the
535 * timer will get fired off when the open function
538 if (!delayed_work_pending(&bond->arp_work)) {
539 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
540 INIT_DELAYED_WORK(&bond->arp_work,
541 bond_activebackup_arp_mon);
543 INIT_DELAYED_WORK(&bond->arp_work,
544 bond_loadbalance_arp_mon);
546 queue_delayed_work(bond->wq, &bond->arp_work, 0);
553 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
554 bonding_show_arp_interval, bonding_store_arp_interval);
557 * Show and set the arp targets.
559 static ssize_t bonding_show_arp_targets(struct device *d,
560 struct device_attribute *attr,
564 struct bonding *bond = to_bond(d);
566 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
567 if (bond->params.arp_targets[i])
568 res += sprintf(buf + res, "%pI4 ",
569 &bond->params.arp_targets[i]);
572 buf[res-1] = '\n'; /* eat the leftover space */
576 static ssize_t bonding_store_arp_targets(struct device *d,
577 struct device_attribute *attr,
578 const char *buf, size_t count)
581 int i = 0, done = 0, ret = count;
582 struct bonding *bond = to_bond(d);
585 targets = bond->params.arp_targets;
586 newtarget = in_aton(buf + 1);
589 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
590 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
591 bond->dev->name, &newtarget);
595 /* look for an empty slot to put the target in, and check for dupes */
596 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
597 if (targets[i] == newtarget) { /* duplicate */
598 pr_err("%s: ARP target %pI4 is already present\n",
599 bond->dev->name, &newtarget);
603 if (targets[i] == 0) {
604 pr_info("%s: adding ARP target %pI4.\n",
605 bond->dev->name, &newtarget);
607 targets[i] = newtarget;
611 pr_err("%s: ARP target table is full!\n",
617 } else if (buf[0] == '-') {
618 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
619 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
620 bond->dev->name, &newtarget);
625 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
626 if (targets[i] == newtarget) {
628 pr_info("%s: removing ARP target %pI4.\n",
629 bond->dev->name, &newtarget);
630 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
631 targets[j] = targets[j+1];
638 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
639 bond->dev->name, &newtarget);
644 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
653 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
656 * Show and set the up and down delays. These must be multiples of the
657 * MII monitoring value, and are stored internally as the multiplier.
658 * Thus, we must translate to MS for the real world.
660 static ssize_t bonding_show_downdelay(struct device *d,
661 struct device_attribute *attr,
664 struct bonding *bond = to_bond(d);
666 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
669 static ssize_t bonding_store_downdelay(struct device *d,
670 struct device_attribute *attr,
671 const char *buf, size_t count)
673 int new_value, ret = count;
674 struct bonding *bond = to_bond(d);
676 if (!(bond->params.miimon)) {
677 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
683 if (sscanf(buf, "%d", &new_value) != 1) {
684 pr_err("%s: no down delay value specified.\n", bond->dev->name);
689 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
690 bond->dev->name, new_value, 1, INT_MAX);
694 if ((new_value % bond->params.miimon) != 0) {
695 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
696 bond->dev->name, new_value,
698 (new_value / bond->params.miimon) *
699 bond->params.miimon);
701 bond->params.downdelay = new_value / bond->params.miimon;
702 pr_info("%s: Setting down delay to %d.\n",
704 bond->params.downdelay * bond->params.miimon);
711 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
712 bonding_show_downdelay, bonding_store_downdelay);
714 static ssize_t bonding_show_updelay(struct device *d,
715 struct device_attribute *attr,
718 struct bonding *bond = to_bond(d);
720 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
724 static ssize_t bonding_store_updelay(struct device *d,
725 struct device_attribute *attr,
726 const char *buf, size_t count)
728 int new_value, ret = count;
729 struct bonding *bond = to_bond(d);
731 if (!(bond->params.miimon)) {
732 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
738 if (sscanf(buf, "%d", &new_value) != 1) {
739 pr_err("%s: no up delay value specified.\n",
745 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
746 bond->dev->name, new_value, 1, INT_MAX);
750 if ((new_value % bond->params.miimon) != 0) {
751 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
752 bond->dev->name, new_value,
754 (new_value / bond->params.miimon) *
755 bond->params.miimon);
757 bond->params.updelay = new_value / bond->params.miimon;
758 pr_info("%s: Setting up delay to %d.\n",
760 bond->params.updelay * bond->params.miimon);
766 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
767 bonding_show_updelay, bonding_store_updelay);
770 * Show and set the LACP interval. Interface must be down, and the mode
771 * must be set to 802.3ad mode.
773 static ssize_t bonding_show_lacp(struct device *d,
774 struct device_attribute *attr,
777 struct bonding *bond = to_bond(d);
779 return sprintf(buf, "%s %d\n",
780 bond_lacp_tbl[bond->params.lacp_fast].modename,
781 bond->params.lacp_fast);
784 static ssize_t bonding_store_lacp(struct device *d,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
788 int new_value, ret = count;
789 struct bonding *bond = to_bond(d);
791 if (bond->dev->flags & IFF_UP) {
792 pr_err("%s: Unable to update LACP rate because interface is up.\n",
798 if (bond->params.mode != BOND_MODE_8023AD) {
799 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
805 new_value = bond_parse_parm(buf, bond_lacp_tbl);
807 if ((new_value == 1) || (new_value == 0)) {
808 bond->params.lacp_fast = new_value;
809 pr_info("%s: Setting LACP rate to %s (%d).\n",
810 bond->dev->name, bond_lacp_tbl[new_value].modename,
813 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
814 bond->dev->name, (int)strlen(buf) - 1, buf);
820 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
821 bonding_show_lacp, bonding_store_lacp);
823 static ssize_t bonding_show_ad_select(struct device *d,
824 struct device_attribute *attr,
827 struct bonding *bond = to_bond(d);
829 return sprintf(buf, "%s %d\n",
830 ad_select_tbl[bond->params.ad_select].modename,
831 bond->params.ad_select);
835 static ssize_t bonding_store_ad_select(struct device *d,
836 struct device_attribute *attr,
837 const char *buf, size_t count)
839 int new_value, ret = count;
840 struct bonding *bond = to_bond(d);
842 if (bond->dev->flags & IFF_UP) {
843 pr_err("%s: Unable to update ad_select because interface is up.\n",
849 new_value = bond_parse_parm(buf, ad_select_tbl);
851 if (new_value != -1) {
852 bond->params.ad_select = new_value;
853 pr_info("%s: Setting ad_select to %s (%d).\n",
854 bond->dev->name, ad_select_tbl[new_value].modename,
857 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
858 bond->dev->name, (int)strlen(buf) - 1, buf);
864 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
865 bonding_show_ad_select, bonding_store_ad_select);
868 * Show and set the number of grat ARP to send after a failover event.
870 static ssize_t bonding_show_n_grat_arp(struct device *d,
871 struct device_attribute *attr,
874 struct bonding *bond = to_bond(d);
876 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
879 static ssize_t bonding_store_n_grat_arp(struct device *d,
880 struct device_attribute *attr,
881 const char *buf, size_t count)
883 int new_value, ret = count;
884 struct bonding *bond = to_bond(d);
886 if (sscanf(buf, "%d", &new_value) != 1) {
887 pr_err("%s: no num_grat_arp value specified.\n",
892 if (new_value < 0 || new_value > 255) {
893 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
894 bond->dev->name, new_value);
898 bond->params.num_grat_arp = new_value;
903 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
904 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
907 * Show and set the number of unsolicited NA's to send after a failover event.
909 static ssize_t bonding_show_n_unsol_na(struct device *d,
910 struct device_attribute *attr,
913 struct bonding *bond = to_bond(d);
915 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
918 static ssize_t bonding_store_n_unsol_na(struct device *d,
919 struct device_attribute *attr,
920 const char *buf, size_t count)
922 int new_value, ret = count;
923 struct bonding *bond = to_bond(d);
925 if (sscanf(buf, "%d", &new_value) != 1) {
926 pr_err("%s: no num_unsol_na value specified.\n",
932 if (new_value < 0 || new_value > 255) {
933 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
934 bond->dev->name, new_value);
938 bond->params.num_unsol_na = new_value;
942 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
943 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
946 * Show and set the MII monitor interval. There are two tricky bits
947 * here. First, if MII monitoring is activated, then we must disable
948 * ARP monitoring. Second, if the timer isn't running, we must
951 static ssize_t bonding_show_miimon(struct device *d,
952 struct device_attribute *attr,
955 struct bonding *bond = to_bond(d);
957 return sprintf(buf, "%d\n", bond->params.miimon);
960 static ssize_t bonding_store_miimon(struct device *d,
961 struct device_attribute *attr,
962 const char *buf, size_t count)
964 int new_value, ret = count;
965 struct bonding *bond = to_bond(d);
967 if (sscanf(buf, "%d", &new_value) != 1) {
968 pr_err("%s: no miimon value specified.\n",
974 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
975 bond->dev->name, new_value, 1, INT_MAX);
979 pr_info("%s: Setting MII monitoring interval to %d.\n",
980 bond->dev->name, new_value);
981 bond->params.miimon = new_value;
982 if (bond->params.updelay)
983 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
985 bond->params.updelay * bond->params.miimon);
986 if (bond->params.downdelay)
987 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
989 bond->params.downdelay * bond->params.miimon);
990 if (bond->params.arp_interval) {
991 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
993 bond->params.arp_interval = 0;
994 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
995 if (bond->params.arp_validate) {
996 bond_unregister_arp(bond);
997 bond->params.arp_validate =
998 BOND_ARP_VALIDATE_NONE;
1000 if (delayed_work_pending(&bond->arp_work)) {
1001 cancel_delayed_work(&bond->arp_work);
1002 flush_workqueue(bond->wq);
1006 if (bond->dev->flags & IFF_UP) {
1007 /* If the interface is up, we may need to fire off
1008 * the MII timer. If the interface is down, the
1009 * timer will get fired off when the open function
1012 if (!delayed_work_pending(&bond->mii_work)) {
1013 INIT_DELAYED_WORK(&bond->mii_work,
1015 queue_delayed_work(bond->wq,
1016 &bond->mii_work, 0);
1023 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1024 bonding_show_miimon, bonding_store_miimon);
1027 * Show and set the primary slave. The store function is much
1028 * simpler than bonding_store_slaves function because it only needs to
1029 * handle one interface name.
1030 * The bond must be a mode that supports a primary for this be
1033 static ssize_t bonding_show_primary(struct device *d,
1034 struct device_attribute *attr,
1038 struct bonding *bond = to_bond(d);
1040 if (bond->primary_slave)
1041 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1046 static ssize_t bonding_store_primary(struct device *d,
1047 struct device_attribute *attr,
1048 const char *buf, size_t count)
1051 struct slave *slave;
1052 struct bonding *bond = to_bond(d);
1054 if (!rtnl_trylock())
1055 return restart_syscall();
1056 read_lock(&bond->lock);
1057 write_lock_bh(&bond->curr_slave_lock);
1059 if (!USES_PRIMARY(bond->params.mode)) {
1060 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1061 bond->dev->name, bond->dev->name, bond->params.mode);
1063 bond_for_each_slave(bond, slave, i) {
1065 (slave->dev->name, buf,
1066 strlen(slave->dev->name)) == 0) {
1067 pr_info("%s: Setting %s as primary slave.\n",
1068 bond->dev->name, slave->dev->name);
1069 bond->primary_slave = slave;
1070 strcpy(bond->params.primary, slave->dev->name);
1071 bond_select_active_slave(bond);
1076 /* if we got here, then we didn't match the name of any slave */
1078 if (strlen(buf) == 0 || buf[0] == '\n') {
1079 pr_info("%s: Setting primary slave to None.\n",
1081 bond->primary_slave = NULL;
1082 bond_select_active_slave(bond);
1084 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1085 bond->dev->name, (int)strlen(buf) - 1, buf);
1089 write_unlock_bh(&bond->curr_slave_lock);
1090 read_unlock(&bond->lock);
1095 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1096 bonding_show_primary, bonding_store_primary);
1099 * Show and set the primary_reselect flag.
1101 static ssize_t bonding_show_primary_reselect(struct device *d,
1102 struct device_attribute *attr,
1105 struct bonding *bond = to_bond(d);
1107 return sprintf(buf, "%s %d\n",
1108 pri_reselect_tbl[bond->params.primary_reselect].modename,
1109 bond->params.primary_reselect);
1112 static ssize_t bonding_store_primary_reselect(struct device *d,
1113 struct device_attribute *attr,
1114 const char *buf, size_t count)
1116 int new_value, ret = count;
1117 struct bonding *bond = to_bond(d);
1119 if (!rtnl_trylock())
1120 return restart_syscall();
1122 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1123 if (new_value < 0) {
1124 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1126 (int) strlen(buf) - 1, buf);
1131 bond->params.primary_reselect = new_value;
1132 pr_info("%s: setting primary_reselect to %s (%d).\n",
1133 bond->dev->name, pri_reselect_tbl[new_value].modename,
1136 read_lock(&bond->lock);
1137 write_lock_bh(&bond->curr_slave_lock);
1138 bond_select_active_slave(bond);
1139 write_unlock_bh(&bond->curr_slave_lock);
1140 read_unlock(&bond->lock);
1145 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1146 bonding_show_primary_reselect,
1147 bonding_store_primary_reselect);
1150 * Show and set the use_carrier flag.
1152 static ssize_t bonding_show_carrier(struct device *d,
1153 struct device_attribute *attr,
1156 struct bonding *bond = to_bond(d);
1158 return sprintf(buf, "%d\n", bond->params.use_carrier);
1161 static ssize_t bonding_store_carrier(struct device *d,
1162 struct device_attribute *attr,
1163 const char *buf, size_t count)
1165 int new_value, ret = count;
1166 struct bonding *bond = to_bond(d);
1169 if (sscanf(buf, "%d", &new_value) != 1) {
1170 pr_err("%s: no use_carrier value specified.\n",
1175 if ((new_value == 0) || (new_value == 1)) {
1176 bond->params.use_carrier = new_value;
1177 pr_info("%s: Setting use_carrier to %d.\n",
1178 bond->dev->name, new_value);
1180 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1181 bond->dev->name, new_value);
1186 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1187 bonding_show_carrier, bonding_store_carrier);
1191 * Show and set currently active_slave.
1193 static ssize_t bonding_show_active_slave(struct device *d,
1194 struct device_attribute *attr,
1198 struct bonding *bond = to_bond(d);
1201 read_lock(&bond->curr_slave_lock);
1202 curr = bond->curr_active_slave;
1203 read_unlock(&bond->curr_slave_lock);
1205 if (USES_PRIMARY(bond->params.mode) && curr)
1206 count = sprintf(buf, "%s\n", curr->dev->name);
1210 static ssize_t bonding_store_active_slave(struct device *d,
1211 struct device_attribute *attr,
1212 const char *buf, size_t count)
1215 struct slave *slave;
1216 struct slave *old_active = NULL;
1217 struct slave *new_active = NULL;
1218 struct bonding *bond = to_bond(d);
1220 if (!rtnl_trylock())
1221 return restart_syscall();
1222 read_lock(&bond->lock);
1223 write_lock_bh(&bond->curr_slave_lock);
1225 if (!USES_PRIMARY(bond->params.mode))
1226 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1227 bond->dev->name, bond->dev->name, bond->params.mode);
1229 bond_for_each_slave(bond, slave, i) {
1231 (slave->dev->name, buf,
1232 strlen(slave->dev->name)) == 0) {
1233 old_active = bond->curr_active_slave;
1235 if (new_active == old_active) {
1237 pr_info("%s: %s is already the current active slave.\n",
1245 (new_active->link == BOND_LINK_UP) &&
1246 IS_UP(new_active->dev)) {
1247 pr_info("%s: Setting %s as active slave.\n",
1250 bond_change_active_slave(bond, new_active);
1253 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1263 /* if we got here, then we didn't match the name of any slave */
1265 if (strlen(buf) == 0 || buf[0] == '\n') {
1266 pr_info("%s: Setting active slave to None.\n",
1268 bond->primary_slave = NULL;
1269 bond_select_active_slave(bond);
1271 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
1272 bond->dev->name, (int)strlen(buf) - 1, buf);
1276 write_unlock_bh(&bond->curr_slave_lock);
1277 read_unlock(&bond->lock);
1283 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1284 bonding_show_active_slave, bonding_store_active_slave);
1288 * Show link status of the bond interface.
1290 static ssize_t bonding_show_mii_status(struct device *d,
1291 struct device_attribute *attr,
1295 struct bonding *bond = to_bond(d);
1297 read_lock(&bond->curr_slave_lock);
1298 curr = bond->curr_active_slave;
1299 read_unlock(&bond->curr_slave_lock);
1301 return sprintf(buf, "%s\n", curr ? "up" : "down");
1303 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1307 * Show current 802.3ad aggregator ID.
1309 static ssize_t bonding_show_ad_aggregator(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.aggregator_id);
1325 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1329 * Show number of active 802.3ad ports.
1331 static ssize_t bonding_show_ad_num_ports(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.ports);
1347 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1351 * Show current 802.3ad actor key.
1353 static ssize_t bonding_show_ad_actor_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.actor_key);
1369 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1373 * Show current 802.3ad partner key.
1375 static ssize_t bonding_show_ad_partner_key(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 count = sprintf(buf, "%d\n",
1385 (bond_3ad_get_active_agg_info(bond, &ad_info))
1386 ? 0 : ad_info.partner_key);
1391 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1395 * Show current 802.3ad partner mac.
1397 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1398 struct device_attribute *attr,
1402 struct bonding *bond = to_bond(d);
1404 if (bond->params.mode == BOND_MODE_8023AD) {
1405 struct ad_info ad_info;
1406 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1407 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1412 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1415 * Show the queue_ids of the slaves in the current bond.
1417 static ssize_t bonding_show_queue_id(struct device *d,
1418 struct device_attribute *attr,
1421 struct slave *slave;
1423 struct bonding *bond = to_bond(d);
1425 if (!rtnl_trylock())
1426 return restart_syscall();
1428 read_lock(&bond->lock);
1429 bond_for_each_slave(bond, slave, i) {
1430 if (res > (PAGE_SIZE - 6)) {
1431 /* not enough space for another interface name */
1432 if ((PAGE_SIZE - res) > 10)
1433 res = PAGE_SIZE - 10;
1434 res += sprintf(buf + res, "++more++ ");
1437 res += sprintf(buf + res, "%s:%d ",
1438 slave->dev->name, slave->queue_id);
1440 read_unlock(&bond->lock);
1442 buf[res-1] = '\n'; /* eat the leftover space */
1448 * Set the queue_ids of the slaves in the current bond. The bond
1449 * interface must be enslaved for this to work.
1451 static ssize_t bonding_store_queue_id(struct device *d,
1452 struct device_attribute *attr,
1453 const char *buffer, size_t count)
1455 struct slave *slave, *update_slave;
1456 struct bonding *bond = to_bond(d);
1460 struct net_device *sdev = NULL;
1462 if (!rtnl_trylock())
1463 return restart_syscall();
1465 /* delim will point to queue id if successful */
1466 delim = strchr(buffer, ':');
1471 * Terminate string that points to device name and bump it
1472 * up one, so we can read the queue id there.
1475 if (sscanf(++delim, "%hd\n", &qid) != 1)
1478 /* Check buffer length, valid ifname and queue id */
1479 if (strlen(buffer) > IFNAMSIZ ||
1480 !dev_valid_name(buffer) ||
1481 qid > bond->params.tx_queues)
1484 /* Get the pointer to that interface if it exists */
1485 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1489 read_lock(&bond->lock);
1491 /* Search for thes slave and check for duplicate qids */
1492 update_slave = NULL;
1493 bond_for_each_slave(bond, slave, i) {
1494 if (sdev == slave->dev)
1496 * We don't need to check the matching
1497 * slave for dups, since we're overwriting it
1499 update_slave = slave;
1500 else if (qid && qid == slave->queue_id) {
1501 goto err_no_cmd_unlock;
1506 goto err_no_cmd_unlock;
1508 /* Actually set the qids for the slave */
1509 update_slave->queue_id = qid;
1511 read_unlock(&bond->lock);
1517 read_unlock(&bond->lock);
1519 pr_info("invalid input for queue_id set for %s.\n",
1525 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1526 bonding_store_queue_id);
1530 * Show and set the all_slaves_active flag.
1532 static ssize_t bonding_show_slaves_active(struct device *d,
1533 struct device_attribute *attr,
1536 struct bonding *bond = to_bond(d);
1538 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1541 static ssize_t bonding_store_slaves_active(struct device *d,
1542 struct device_attribute *attr,
1543 const char *buf, size_t count)
1545 int i, new_value, ret = count;
1546 struct bonding *bond = to_bond(d);
1547 struct slave *slave;
1549 if (sscanf(buf, "%d", &new_value) != 1) {
1550 pr_err("%s: no all_slaves_active value specified.\n",
1556 if (new_value == bond->params.all_slaves_active)
1559 if ((new_value == 0) || (new_value == 1)) {
1560 bond->params.all_slaves_active = new_value;
1562 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1563 bond->dev->name, new_value);
1568 bond_for_each_slave(bond, slave, i) {
1569 if (slave->state == BOND_STATE_BACKUP) {
1571 slave->dev->priv_flags &= ~IFF_SLAVE_INACTIVE;
1573 slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
1579 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1580 bonding_show_slaves_active, bonding_store_slaves_active);
1582 static struct attribute *per_bond_attrs[] = {
1583 &dev_attr_slaves.attr,
1584 &dev_attr_mode.attr,
1585 &dev_attr_fail_over_mac.attr,
1586 &dev_attr_arp_validate.attr,
1587 &dev_attr_arp_interval.attr,
1588 &dev_attr_arp_ip_target.attr,
1589 &dev_attr_downdelay.attr,
1590 &dev_attr_updelay.attr,
1591 &dev_attr_lacp_rate.attr,
1592 &dev_attr_ad_select.attr,
1593 &dev_attr_xmit_hash_policy.attr,
1594 &dev_attr_num_grat_arp.attr,
1595 &dev_attr_num_unsol_na.attr,
1596 &dev_attr_miimon.attr,
1597 &dev_attr_primary.attr,
1598 &dev_attr_primary_reselect.attr,
1599 &dev_attr_use_carrier.attr,
1600 &dev_attr_active_slave.attr,
1601 &dev_attr_mii_status.attr,
1602 &dev_attr_ad_aggregator.attr,
1603 &dev_attr_ad_num_ports.attr,
1604 &dev_attr_ad_actor_key.attr,
1605 &dev_attr_ad_partner_key.attr,
1606 &dev_attr_ad_partner_mac.attr,
1607 &dev_attr_queue_id.attr,
1608 &dev_attr_all_slaves_active.attr,
1612 static struct attribute_group bonding_group = {
1614 .attrs = per_bond_attrs,
1618 * Initialize sysfs. This sets up the bonding_masters file in
1621 int bond_create_sysfs(void)
1625 ret = netdev_class_create_file(&class_attr_bonding_masters);
1627 * Permit multiple loads of the module by ignoring failures to
1628 * create the bonding_masters sysfs file. Bonding devices
1629 * created by second or subsequent loads of the module will
1630 * not be listed in, or controllable by, bonding_masters, but
1631 * will have the usual "bonding" sysfs directory.
1633 * This is done to preserve backwards compatibility for
1634 * initscripts/sysconfig, which load bonding multiple times to
1635 * configure multiple bonding devices.
1637 if (ret == -EEXIST) {
1638 /* Is someone being kinky and naming a device bonding_master? */
1639 if (__dev_get_by_name(&init_net,
1640 class_attr_bonding_masters.attr.name))
1641 pr_err("network device named %s already exists in sysfs",
1642 class_attr_bonding_masters.attr.name);
1651 * Remove /sys/class/net/bonding_masters.
1653 void bond_destroy_sysfs(void)
1655 netdev_class_remove_file(&class_attr_bonding_masters);
1659 * Initialize sysfs for each bond. This sets up and registers
1660 * the 'bondctl' directory for each individual bond under /sys/class/net.
1662 void bond_prepare_sysfs_group(struct bonding *bond)
1664 bond->dev->sysfs_groups[0] = &bonding_group;