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,
171 int bond_create_slave_symlinks(struct net_device *master,
172 struct net_device *slave)
174 char linkname[IFNAMSIZ+7];
177 /* first, create a link from the slave back to the master */
178 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
182 /* next, create a link from the master to the slave */
183 sprintf(linkname, "slave_%s", slave->name);
184 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
187 /* free the master link created earlier in case of error */
189 sysfs_remove_link(&(slave->dev.kobj), "master");
195 void bond_destroy_slave_symlinks(struct net_device *master,
196 struct net_device *slave)
198 char linkname[IFNAMSIZ+7];
200 sysfs_remove_link(&(slave->dev.kobj), "master");
201 sprintf(linkname, "slave_%s", slave->name);
202 sysfs_remove_link(&(master->dev.kobj), linkname);
207 * Show the slaves in the current bond.
209 static ssize_t bonding_show_slaves(struct device *d,
210 struct device_attribute *attr, char *buf)
214 struct bonding *bond = to_bond(d);
216 read_lock(&bond->lock);
217 bond_for_each_slave(bond, slave, i) {
218 if (res > (PAGE_SIZE - IFNAMSIZ)) {
219 /* not enough space for another interface name */
220 if ((PAGE_SIZE - res) > 10)
221 res = PAGE_SIZE - 10;
222 res += sprintf(buf + res, "++more++ ");
225 res += sprintf(buf + res, "%s ", slave->dev->name);
227 read_unlock(&bond->lock);
229 buf[res-1] = '\n'; /* eat the leftover space */
234 * Set the slaves in the current bond. The bond interface must be
235 * up for this to succeed.
236 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
237 * All hard work should be done there.
239 static ssize_t bonding_store_slaves(struct device *d,
240 struct device_attribute *attr,
241 const char *buffer, size_t count)
243 char command[IFNAMSIZ + 1] = { 0, };
245 int res, ret = count;
246 struct net_device *dev;
247 struct bonding *bond = to_bond(d);
250 return restart_syscall();
252 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
253 ifname = command + 1;
254 if ((strlen(command) <= 1) ||
255 !dev_valid_name(ifname))
258 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
260 pr_info("%s: Interface %s does not exist!\n",
261 bond->dev->name, ifname);
266 switch (command[0]) {
268 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
269 res = bond_enslave(bond->dev, dev);
273 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
274 res = bond_release(bond->dev, dev);
286 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
295 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
296 bonding_store_slaves);
299 * Show and set the bonding mode. The bond interface must be down to
302 static ssize_t bonding_show_mode(struct device *d,
303 struct device_attribute *attr, char *buf)
305 struct bonding *bond = to_bond(d);
307 return sprintf(buf, "%s %d\n",
308 bond_mode_tbl[bond->params.mode].modename,
312 static ssize_t bonding_store_mode(struct device *d,
313 struct device_attribute *attr,
314 const char *buf, size_t count)
316 int new_value, ret = count;
317 struct bonding *bond = to_bond(d);
320 return restart_syscall();
322 if (bond->dev->flags & IFF_UP) {
323 pr_err("unable to update mode of %s because interface is up.\n",
329 if (bond->slave_cnt > 0) {
330 pr_err("unable to update mode of %s because it has slaves.\n",
336 new_value = bond_parse_parm(buf, bond_mode_tbl);
338 pr_err("%s: Ignoring invalid mode value %.*s.\n",
339 bond->dev->name, (int)strlen(buf) - 1, buf);
343 if ((new_value == BOND_MODE_ALB ||
344 new_value == BOND_MODE_TLB) &&
345 bond->params.arp_interval) {
346 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
347 bond->dev->name, bond_mode_tbl[new_value].modename);
352 bond->params.mode = new_value;
353 bond_set_mode_ops(bond, bond->params.mode);
354 pr_info("%s: setting mode to %s (%d).\n",
355 bond->dev->name, bond_mode_tbl[new_value].modename,
361 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
362 bonding_show_mode, bonding_store_mode);
365 * Show and set the bonding transmit hash method.
366 * The bond interface must be down to change the xmit hash policy.
368 static ssize_t bonding_show_xmit_hash(struct device *d,
369 struct device_attribute *attr,
372 struct bonding *bond = to_bond(d);
374 return sprintf(buf, "%s %d\n",
375 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
376 bond->params.xmit_policy);
379 static ssize_t bonding_store_xmit_hash(struct device *d,
380 struct device_attribute *attr,
381 const char *buf, size_t count)
383 int new_value, ret = count;
384 struct bonding *bond = to_bond(d);
386 if (bond->dev->flags & IFF_UP) {
387 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
393 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
395 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
397 (int)strlen(buf) - 1, buf);
401 bond->params.xmit_policy = new_value;
402 bond_set_mode_ops(bond, bond->params.mode);
403 pr_info("%s: setting xmit hash policy to %s (%d).\n",
405 xmit_hashtype_tbl[new_value].modename, new_value);
410 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
411 bonding_show_xmit_hash, bonding_store_xmit_hash);
414 * Show and set arp_validate.
416 static ssize_t bonding_show_arp_validate(struct device *d,
417 struct device_attribute *attr,
420 struct bonding *bond = to_bond(d);
422 return sprintf(buf, "%s %d\n",
423 arp_validate_tbl[bond->params.arp_validate].modename,
424 bond->params.arp_validate);
427 static ssize_t bonding_store_arp_validate(struct device *d,
428 struct device_attribute *attr,
429 const char *buf, size_t count)
432 struct bonding *bond = to_bond(d);
434 new_value = bond_parse_parm(buf, arp_validate_tbl);
436 pr_err("%s: Ignoring invalid arp_validate value %s\n",
437 bond->dev->name, buf);
440 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
441 pr_err("%s: arp_validate only supported in active-backup mode.\n",
445 pr_info("%s: setting arp_validate to %s (%d).\n",
446 bond->dev->name, arp_validate_tbl[new_value].modename,
449 bond->params.arp_validate = new_value;
454 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
455 bonding_store_arp_validate);
458 * Show and store fail_over_mac. User only allowed to change the
459 * value when there are no slaves.
461 static ssize_t bonding_show_fail_over_mac(struct device *d,
462 struct device_attribute *attr,
465 struct bonding *bond = to_bond(d);
467 return sprintf(buf, "%s %d\n",
468 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
469 bond->params.fail_over_mac);
472 static ssize_t bonding_store_fail_over_mac(struct device *d,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
477 struct bonding *bond = to_bond(d);
479 if (bond->slave_cnt != 0) {
480 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
485 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
487 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
488 bond->dev->name, buf);
492 bond->params.fail_over_mac = new_value;
493 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
494 bond->dev->name, fail_over_mac_tbl[new_value].modename,
500 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
501 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
504 * Show and set the arp timer interval. There are two tricky bits
505 * here. First, if ARP monitoring is activated, then we must disable
506 * MII monitoring. Second, if the ARP timer isn't running, we must
509 static ssize_t bonding_show_arp_interval(struct device *d,
510 struct device_attribute *attr,
513 struct bonding *bond = to_bond(d);
515 return sprintf(buf, "%d\n", bond->params.arp_interval);
518 static ssize_t bonding_store_arp_interval(struct device *d,
519 struct device_attribute *attr,
520 const char *buf, size_t count)
522 int new_value, ret = count;
523 struct bonding *bond = to_bond(d);
526 return restart_syscall();
527 if (sscanf(buf, "%d", &new_value) != 1) {
528 pr_err("%s: no arp_interval value specified.\n",
534 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
535 bond->dev->name, new_value, INT_MAX);
539 if (bond->params.mode == BOND_MODE_ALB ||
540 bond->params.mode == BOND_MODE_TLB ||
541 bond->params.mode == BOND_MODE_8023AD) {
542 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
543 bond->dev->name, bond->dev->name);
547 pr_info("%s: Setting ARP monitoring interval to %d.\n",
548 bond->dev->name, new_value);
549 bond->params.arp_interval = new_value;
551 if (bond->params.miimon) {
552 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
553 bond->dev->name, bond->dev->name);
554 bond->params.miimon = 0;
556 if (!bond->params.arp_targets[0])
557 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
560 if (bond->dev->flags & IFF_UP) {
561 /* If the interface is up, we may need to fire off
562 * the ARP timer. If the interface is down, the
563 * timer will get fired off when the open function
567 cancel_delayed_work_sync(&bond->arp_work);
569 cancel_delayed_work_sync(&bond->mii_work);
570 queue_delayed_work(bond->wq, &bond->arp_work, 0);
577 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
578 bonding_show_arp_interval, bonding_store_arp_interval);
581 * Show and set the arp targets.
583 static ssize_t bonding_show_arp_targets(struct device *d,
584 struct device_attribute *attr,
588 struct bonding *bond = to_bond(d);
590 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
591 if (bond->params.arp_targets[i])
592 res += sprintf(buf + res, "%pI4 ",
593 &bond->params.arp_targets[i]);
596 buf[res-1] = '\n'; /* eat the leftover space */
600 static ssize_t bonding_store_arp_targets(struct device *d,
601 struct device_attribute *attr,
602 const char *buf, size_t count)
605 int i = 0, done = 0, ret = count;
606 struct bonding *bond = to_bond(d);
609 targets = bond->params.arp_targets;
610 newtarget = in_aton(buf + 1);
613 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
614 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
615 bond->dev->name, &newtarget);
619 /* look for an empty slot to put the target in, and check for dupes */
620 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
621 if (targets[i] == newtarget) { /* duplicate */
622 pr_err("%s: ARP target %pI4 is already present\n",
623 bond->dev->name, &newtarget);
627 if (targets[i] == 0) {
628 pr_info("%s: adding ARP target %pI4.\n",
629 bond->dev->name, &newtarget);
631 targets[i] = newtarget;
635 pr_err("%s: ARP target table is full!\n",
641 } else if (buf[0] == '-') {
642 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
643 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
644 bond->dev->name, &newtarget);
649 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
650 if (targets[i] == newtarget) {
652 pr_info("%s: removing ARP target %pI4.\n",
653 bond->dev->name, &newtarget);
654 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
655 targets[j] = targets[j+1];
662 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
663 bond->dev->name, &newtarget);
668 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
677 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
680 * Show and set the up and down delays. These must be multiples of the
681 * MII monitoring value, and are stored internally as the multiplier.
682 * Thus, we must translate to MS for the real world.
684 static ssize_t bonding_show_downdelay(struct device *d,
685 struct device_attribute *attr,
688 struct bonding *bond = to_bond(d);
690 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
693 static ssize_t bonding_store_downdelay(struct device *d,
694 struct device_attribute *attr,
695 const char *buf, size_t count)
697 int new_value, ret = count;
698 struct bonding *bond = to_bond(d);
701 return restart_syscall();
702 if (!(bond->params.miimon)) {
703 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
709 if (sscanf(buf, "%d", &new_value) != 1) {
710 pr_err("%s: no down delay value specified.\n", bond->dev->name);
715 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
716 bond->dev->name, new_value, 0, INT_MAX);
720 if ((new_value % bond->params.miimon) != 0) {
721 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
722 bond->dev->name, new_value,
724 (new_value / bond->params.miimon) *
725 bond->params.miimon);
727 bond->params.downdelay = new_value / bond->params.miimon;
728 pr_info("%s: Setting down delay to %d.\n",
730 bond->params.downdelay * bond->params.miimon);
738 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
739 bonding_show_downdelay, bonding_store_downdelay);
741 static ssize_t bonding_show_updelay(struct device *d,
742 struct device_attribute *attr,
745 struct bonding *bond = to_bond(d);
747 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
751 static ssize_t bonding_store_updelay(struct device *d,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
755 int new_value, ret = count;
756 struct bonding *bond = to_bond(d);
759 return restart_syscall();
760 if (!(bond->params.miimon)) {
761 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
767 if (sscanf(buf, "%d", &new_value) != 1) {
768 pr_err("%s: no up delay value specified.\n",
774 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
775 bond->dev->name, new_value, 0, INT_MAX);
779 if ((new_value % bond->params.miimon) != 0) {
780 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
781 bond->dev->name, new_value,
783 (new_value / bond->params.miimon) *
784 bond->params.miimon);
786 bond->params.updelay = new_value / bond->params.miimon;
787 pr_info("%s: Setting up delay to %d.\n",
789 bond->params.updelay * bond->params.miimon);
796 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
797 bonding_show_updelay, bonding_store_updelay);
800 * Show and set the LACP interval. Interface must be down, and the mode
801 * must be set to 802.3ad mode.
803 static ssize_t bonding_show_lacp(struct device *d,
804 struct device_attribute *attr,
807 struct bonding *bond = to_bond(d);
809 return sprintf(buf, "%s %d\n",
810 bond_lacp_tbl[bond->params.lacp_fast].modename,
811 bond->params.lacp_fast);
814 static ssize_t bonding_store_lacp(struct device *d,
815 struct device_attribute *attr,
816 const char *buf, size_t count)
818 int new_value, ret = count;
819 struct bonding *bond = to_bond(d);
821 if (bond->dev->flags & IFF_UP) {
822 pr_err("%s: Unable to update LACP rate because interface is up.\n",
828 if (bond->params.mode != BOND_MODE_8023AD) {
829 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
835 new_value = bond_parse_parm(buf, bond_lacp_tbl);
837 if ((new_value == 1) || (new_value == 0)) {
838 bond->params.lacp_fast = new_value;
839 bond_3ad_update_lacp_rate(bond);
840 pr_info("%s: Setting LACP rate to %s (%d).\n",
841 bond->dev->name, bond_lacp_tbl[new_value].modename,
844 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
845 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)
1053 struct slave *slave;
1054 struct bonding *bond = to_bond(d);
1055 char ifname[IFNAMSIZ];
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, i) {
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,
1211 struct bonding *bond = to_bond(d);
1214 read_lock(&bond->curr_slave_lock);
1215 curr = bond->curr_active_slave;
1216 read_unlock(&bond->curr_slave_lock);
1218 if (USES_PRIMARY(bond->params.mode) && curr)
1219 count = sprintf(buf, "%s\n", curr->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 slave *slave;
1229 struct slave *old_active = NULL;
1230 struct slave *new_active = NULL;
1231 struct bonding *bond = to_bond(d);
1232 char ifname[IFNAMSIZ];
1234 if (!rtnl_trylock())
1235 return restart_syscall();
1238 read_lock(&bond->lock);
1239 write_lock_bh(&bond->curr_slave_lock);
1241 if (!USES_PRIMARY(bond->params.mode)) {
1242 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1243 bond->dev->name, bond->dev->name, bond->params.mode);
1247 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1249 /* check to see if we are clearing active */
1250 if (!strlen(ifname) || buf[0] == '\n') {
1251 pr_info("%s: Clearing current active slave.\n",
1253 bond->curr_active_slave = NULL;
1254 bond_select_active_slave(bond);
1258 bond_for_each_slave(bond, slave, i) {
1259 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1260 old_active = bond->curr_active_slave;
1262 if (new_active == old_active) {
1264 pr_info("%s: %s is already the current"
1273 (new_active->link == BOND_LINK_UP) &&
1274 IS_UP(new_active->dev)) {
1275 pr_info("%s: Setting %s as active"
1279 bond_change_active_slave(bond,
1283 pr_info("%s: Could not set %s as"
1284 " active slave; either %s is"
1285 " down or the link is down.\n",
1295 pr_info("%s: Unable to set %.*s as active slave.\n",
1296 bond->dev->name, (int)strlen(buf) - 1, buf);
1298 write_unlock_bh(&bond->curr_slave_lock);
1299 read_unlock(&bond->lock);
1300 unblock_netpoll_tx();
1307 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1308 bonding_show_active_slave, bonding_store_active_slave);
1312 * Show link status of the bond interface.
1314 static ssize_t bonding_show_mii_status(struct device *d,
1315 struct device_attribute *attr,
1319 struct bonding *bond = to_bond(d);
1321 read_lock(&bond->curr_slave_lock);
1322 curr = bond->curr_active_slave;
1323 read_unlock(&bond->curr_slave_lock);
1325 return sprintf(buf, "%s\n", curr ? "up" : "down");
1327 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1330 * Show current 802.3ad aggregator ID.
1332 static ssize_t bonding_show_ad_aggregator(struct device *d,
1333 struct device_attribute *attr,
1337 struct bonding *bond = to_bond(d);
1339 if (bond->params.mode == BOND_MODE_8023AD) {
1340 struct ad_info ad_info;
1341 count = sprintf(buf, "%d\n",
1342 bond_3ad_get_active_agg_info(bond, &ad_info)
1343 ? 0 : ad_info.aggregator_id);
1348 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1352 * Show number of active 802.3ad ports.
1354 static ssize_t bonding_show_ad_num_ports(struct device *d,
1355 struct device_attribute *attr,
1359 struct bonding *bond = to_bond(d);
1361 if (bond->params.mode == BOND_MODE_8023AD) {
1362 struct ad_info ad_info;
1363 count = sprintf(buf, "%d\n",
1364 bond_3ad_get_active_agg_info(bond, &ad_info)
1365 ? 0 : ad_info.ports);
1370 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1374 * Show current 802.3ad actor key.
1376 static ssize_t bonding_show_ad_actor_key(struct device *d,
1377 struct device_attribute *attr,
1381 struct bonding *bond = to_bond(d);
1383 if (bond->params.mode == BOND_MODE_8023AD) {
1384 struct ad_info ad_info;
1385 count = sprintf(buf, "%d\n",
1386 bond_3ad_get_active_agg_info(bond, &ad_info)
1387 ? 0 : ad_info.actor_key);
1392 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1396 * Show current 802.3ad partner key.
1398 static ssize_t bonding_show_ad_partner_key(struct device *d,
1399 struct device_attribute *attr,
1403 struct bonding *bond = to_bond(d);
1405 if (bond->params.mode == BOND_MODE_8023AD) {
1406 struct ad_info ad_info;
1407 count = sprintf(buf, "%d\n",
1408 bond_3ad_get_active_agg_info(bond, &ad_info)
1409 ? 0 : ad_info.partner_key);
1414 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1418 * Show current 802.3ad partner mac.
1420 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1421 struct device_attribute *attr,
1425 struct bonding *bond = to_bond(d);
1427 if (bond->params.mode == BOND_MODE_8023AD) {
1428 struct ad_info ad_info;
1429 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1430 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1435 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1438 * Show the queue_ids of the slaves in the current bond.
1440 static ssize_t bonding_show_queue_id(struct device *d,
1441 struct device_attribute *attr,
1444 struct slave *slave;
1446 struct bonding *bond = to_bond(d);
1448 if (!rtnl_trylock())
1449 return restart_syscall();
1451 read_lock(&bond->lock);
1452 bond_for_each_slave(bond, slave, i) {
1453 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1454 /* not enough space for another interface_name:queue_id pair */
1455 if ((PAGE_SIZE - res) > 10)
1456 res = PAGE_SIZE - 10;
1457 res += sprintf(buf + res, "++more++ ");
1460 res += sprintf(buf + res, "%s:%d ",
1461 slave->dev->name, slave->queue_id);
1463 read_unlock(&bond->lock);
1465 buf[res-1] = '\n'; /* eat the leftover space */
1471 * Set the queue_ids of the slaves in the current bond. The bond
1472 * interface must be enslaved for this to work.
1474 static ssize_t bonding_store_queue_id(struct device *d,
1475 struct device_attribute *attr,
1476 const char *buffer, size_t count)
1478 struct slave *slave, *update_slave;
1479 struct bonding *bond = to_bond(d);
1483 struct net_device *sdev = NULL;
1485 if (!rtnl_trylock())
1486 return restart_syscall();
1488 /* delim will point to queue id if successful */
1489 delim = strchr(buffer, ':');
1494 * Terminate string that points to device name and bump it
1495 * up one, so we can read the queue id there.
1498 if (sscanf(++delim, "%hd\n", &qid) != 1)
1501 /* Check buffer length, valid ifname and queue id */
1502 if (strlen(buffer) > IFNAMSIZ ||
1503 !dev_valid_name(buffer) ||
1504 qid > bond->dev->real_num_tx_queues)
1507 /* Get the pointer to that interface if it exists */
1508 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1512 read_lock(&bond->lock);
1514 /* Search for thes slave and check for duplicate qids */
1515 update_slave = NULL;
1516 bond_for_each_slave(bond, slave, i) {
1517 if (sdev == slave->dev)
1519 * We don't need to check the matching
1520 * slave for dups, since we're overwriting it
1522 update_slave = slave;
1523 else if (qid && qid == slave->queue_id) {
1524 goto err_no_cmd_unlock;
1529 goto err_no_cmd_unlock;
1531 /* Actually set the qids for the slave */
1532 update_slave->queue_id = qid;
1534 read_unlock(&bond->lock);
1540 read_unlock(&bond->lock);
1542 pr_info("invalid input for queue_id set for %s.\n",
1548 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1549 bonding_store_queue_id);
1553 * Show and set the all_slaves_active flag.
1555 static ssize_t bonding_show_slaves_active(struct device *d,
1556 struct device_attribute *attr,
1559 struct bonding *bond = to_bond(d);
1561 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1564 static ssize_t bonding_store_slaves_active(struct device *d,
1565 struct device_attribute *attr,
1566 const char *buf, size_t count)
1568 int i, new_value, ret = count;
1569 struct bonding *bond = to_bond(d);
1570 struct slave *slave;
1572 if (sscanf(buf, "%d", &new_value) != 1) {
1573 pr_err("%s: no all_slaves_active value specified.\n",
1579 if (new_value == bond->params.all_slaves_active)
1582 if ((new_value == 0) || (new_value == 1)) {
1583 bond->params.all_slaves_active = new_value;
1585 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1586 bond->dev->name, new_value);
1591 read_lock(&bond->lock);
1592 bond_for_each_slave(bond, slave, i) {
1593 if (!bond_is_active_slave(slave)) {
1595 slave->inactive = 0;
1597 slave->inactive = 1;
1600 read_unlock(&bond->lock);
1604 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1605 bonding_show_slaves_active, bonding_store_slaves_active);
1608 * Show and set the number of IGMP membership reports to send on link failure
1610 static ssize_t bonding_show_resend_igmp(struct device *d,
1611 struct device_attribute *attr,
1614 struct bonding *bond = to_bond(d);
1616 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1619 static ssize_t bonding_store_resend_igmp(struct device *d,
1620 struct device_attribute *attr,
1621 const char *buf, size_t count)
1623 int new_value, ret = count;
1624 struct bonding *bond = to_bond(d);
1626 if (sscanf(buf, "%d", &new_value) != 1) {
1627 pr_err("%s: no resend_igmp value specified.\n",
1633 if (new_value < 0 || new_value > 255) {
1634 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1635 bond->dev->name, new_value);
1640 pr_info("%s: Setting resend_igmp to %d.\n",
1641 bond->dev->name, new_value);
1642 bond->params.resend_igmp = new_value;
1647 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1648 bonding_show_resend_igmp, bonding_store_resend_igmp);
1650 static struct attribute *per_bond_attrs[] = {
1651 &dev_attr_slaves.attr,
1652 &dev_attr_mode.attr,
1653 &dev_attr_fail_over_mac.attr,
1654 &dev_attr_arp_validate.attr,
1655 &dev_attr_arp_interval.attr,
1656 &dev_attr_arp_ip_target.attr,
1657 &dev_attr_downdelay.attr,
1658 &dev_attr_updelay.attr,
1659 &dev_attr_lacp_rate.attr,
1660 &dev_attr_ad_select.attr,
1661 &dev_attr_xmit_hash_policy.attr,
1662 &dev_attr_num_grat_arp.attr,
1663 &dev_attr_num_unsol_na.attr,
1664 &dev_attr_miimon.attr,
1665 &dev_attr_primary.attr,
1666 &dev_attr_primary_reselect.attr,
1667 &dev_attr_use_carrier.attr,
1668 &dev_attr_active_slave.attr,
1669 &dev_attr_mii_status.attr,
1670 &dev_attr_ad_aggregator.attr,
1671 &dev_attr_ad_num_ports.attr,
1672 &dev_attr_ad_actor_key.attr,
1673 &dev_attr_ad_partner_key.attr,
1674 &dev_attr_ad_partner_mac.attr,
1675 &dev_attr_queue_id.attr,
1676 &dev_attr_all_slaves_active.attr,
1677 &dev_attr_resend_igmp.attr,
1678 &dev_attr_min_links.attr,
1682 static struct attribute_group bonding_group = {
1684 .attrs = per_bond_attrs,
1688 * Initialize sysfs. This sets up the bonding_masters file in
1691 int bond_create_sysfs(struct bond_net *bn)
1695 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1696 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1698 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
1700 * Permit multiple loads of the module by ignoring failures to
1701 * create the bonding_masters sysfs file. Bonding devices
1702 * created by second or subsequent loads of the module will
1703 * not be listed in, or controllable by, bonding_masters, but
1704 * will have the usual "bonding" sysfs directory.
1706 * This is done to preserve backwards compatibility for
1707 * initscripts/sysconfig, which load bonding multiple times to
1708 * configure multiple bonding devices.
1710 if (ret == -EEXIST) {
1711 /* Is someone being kinky and naming a device bonding_master? */
1712 if (__dev_get_by_name(bn->net,
1713 class_attr_bonding_masters.attr.name))
1714 pr_err("network device named %s already exists in sysfs",
1715 class_attr_bonding_masters.attr.name);
1724 * Remove /sys/class/net/bonding_masters.
1726 void bond_destroy_sysfs(struct bond_net *bn)
1728 netdev_class_remove_file(&bn->class_attr_bonding_masters);
1732 * Initialize sysfs for each bond. This sets up and registers
1733 * the 'bondctl' directory for each individual bond under /sys/class/net.
1735 void bond_prepare_sysfs_group(struct bonding *bond)
1737 bond->dev->sysfs_groups[0] = &bonding_group;