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 ((new_value == BOND_MODE_ALB ||
318 new_value == BOND_MODE_TLB) &&
319 bond->params.arp_interval) {
320 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
321 bond->dev->name, bond_mode_tbl[new_value].modename);
325 if (bond->params.mode == BOND_MODE_8023AD)
326 bond_unset_master_3ad_flags(bond);
328 if (bond->params.mode == BOND_MODE_ALB)
329 bond_unset_master_alb_flags(bond);
331 bond->params.mode = new_value;
332 bond_set_mode_ops(bond, bond->params.mode);
333 pr_info("%s: setting mode to %s (%d).\n",
334 bond->dev->name, bond_mode_tbl[new_value].modename,
339 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
340 bonding_show_mode, bonding_store_mode);
343 * Show and set the bonding transmit hash method.
344 * The bond interface must be down to change the xmit hash policy.
346 static ssize_t bonding_show_xmit_hash(struct device *d,
347 struct device_attribute *attr,
350 struct bonding *bond = to_bond(d);
352 return sprintf(buf, "%s %d\n",
353 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
354 bond->params.xmit_policy);
357 static ssize_t bonding_store_xmit_hash(struct device *d,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
361 int new_value, ret = count;
362 struct bonding *bond = to_bond(d);
364 if (bond->dev->flags & IFF_UP) {
365 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
371 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
373 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
375 (int)strlen(buf) - 1, buf);
379 bond->params.xmit_policy = new_value;
380 bond_set_mode_ops(bond, bond->params.mode);
381 pr_info("%s: setting xmit hash policy to %s (%d).\n",
383 xmit_hashtype_tbl[new_value].modename, new_value);
388 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
389 bonding_show_xmit_hash, bonding_store_xmit_hash);
392 * Show and set arp_validate.
394 static ssize_t bonding_show_arp_validate(struct device *d,
395 struct device_attribute *attr,
398 struct bonding *bond = to_bond(d);
400 return sprintf(buf, "%s %d\n",
401 arp_validate_tbl[bond->params.arp_validate].modename,
402 bond->params.arp_validate);
405 static ssize_t bonding_store_arp_validate(struct device *d,
406 struct device_attribute *attr,
407 const char *buf, size_t count)
410 struct bonding *bond = to_bond(d);
412 new_value = bond_parse_parm(buf, arp_validate_tbl);
414 pr_err("%s: Ignoring invalid arp_validate value %s\n",
415 bond->dev->name, buf);
418 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
419 pr_err("%s: arp_validate only supported in active-backup mode.\n",
423 pr_info("%s: setting arp_validate to %s (%d).\n",
424 bond->dev->name, arp_validate_tbl[new_value].modename,
427 if (!bond->params.arp_validate && new_value)
428 bond_register_arp(bond);
429 else if (bond->params.arp_validate && !new_value)
430 bond_unregister_arp(bond);
432 bond->params.arp_validate = new_value;
437 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
438 bonding_store_arp_validate);
441 * Show and store fail_over_mac. User only allowed to change the
442 * value when there are no slaves.
444 static ssize_t bonding_show_fail_over_mac(struct device *d,
445 struct device_attribute *attr,
448 struct bonding *bond = to_bond(d);
450 return sprintf(buf, "%s %d\n",
451 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
452 bond->params.fail_over_mac);
455 static ssize_t bonding_store_fail_over_mac(struct device *d,
456 struct device_attribute *attr,
457 const char *buf, size_t count)
460 struct bonding *bond = to_bond(d);
462 if (bond->slave_cnt != 0) {
463 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
468 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
470 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
471 bond->dev->name, buf);
475 bond->params.fail_over_mac = new_value;
476 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
477 bond->dev->name, fail_over_mac_tbl[new_value].modename,
483 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
484 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
487 * Show and set the arp timer interval. There are two tricky bits
488 * here. First, if ARP monitoring is activated, then we must disable
489 * MII monitoring. Second, if the ARP timer isn't running, we must
492 static ssize_t bonding_show_arp_interval(struct device *d,
493 struct device_attribute *attr,
496 struct bonding *bond = to_bond(d);
498 return sprintf(buf, "%d\n", bond->params.arp_interval);
501 static ssize_t bonding_store_arp_interval(struct device *d,
502 struct device_attribute *attr,
503 const char *buf, size_t count)
505 int new_value, ret = count;
506 struct bonding *bond = to_bond(d);
508 if (sscanf(buf, "%d", &new_value) != 1) {
509 pr_err("%s: no arp_interval value specified.\n",
515 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
516 bond->dev->name, new_value, INT_MAX);
520 if (bond->params.mode == BOND_MODE_ALB ||
521 bond->params.mode == BOND_MODE_TLB) {
522 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
523 bond->dev->name, bond->dev->name);
527 pr_info("%s: Setting ARP monitoring interval to %d.\n",
528 bond->dev->name, new_value);
529 bond->params.arp_interval = new_value;
530 if (bond->params.arp_interval)
531 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
532 if (bond->params.miimon) {
533 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
534 bond->dev->name, bond->dev->name);
535 bond->params.miimon = 0;
536 if (delayed_work_pending(&bond->mii_work)) {
537 cancel_delayed_work(&bond->mii_work);
538 flush_workqueue(bond->wq);
541 if (!bond->params.arp_targets[0]) {
542 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
545 if (bond->dev->flags & IFF_UP) {
546 /* If the interface is up, we may need to fire off
547 * the ARP timer. If the interface is down, the
548 * timer will get fired off when the open function
551 if (!delayed_work_pending(&bond->arp_work)) {
552 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
553 INIT_DELAYED_WORK(&bond->arp_work,
554 bond_activebackup_arp_mon);
556 INIT_DELAYED_WORK(&bond->arp_work,
557 bond_loadbalance_arp_mon);
559 queue_delayed_work(bond->wq, &bond->arp_work, 0);
566 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
567 bonding_show_arp_interval, bonding_store_arp_interval);
570 * Show and set the arp targets.
572 static ssize_t bonding_show_arp_targets(struct device *d,
573 struct device_attribute *attr,
577 struct bonding *bond = to_bond(d);
579 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
580 if (bond->params.arp_targets[i])
581 res += sprintf(buf + res, "%pI4 ",
582 &bond->params.arp_targets[i]);
585 buf[res-1] = '\n'; /* eat the leftover space */
589 static ssize_t bonding_store_arp_targets(struct device *d,
590 struct device_attribute *attr,
591 const char *buf, size_t count)
594 int i = 0, done = 0, ret = count;
595 struct bonding *bond = to_bond(d);
598 targets = bond->params.arp_targets;
599 newtarget = in_aton(buf + 1);
602 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
603 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
604 bond->dev->name, &newtarget);
608 /* look for an empty slot to put the target in, and check for dupes */
609 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
610 if (targets[i] == newtarget) { /* duplicate */
611 pr_err("%s: ARP target %pI4 is already present\n",
612 bond->dev->name, &newtarget);
616 if (targets[i] == 0) {
617 pr_info("%s: adding ARP target %pI4.\n",
618 bond->dev->name, &newtarget);
620 targets[i] = newtarget;
624 pr_err("%s: ARP target table is full!\n",
630 } else if (buf[0] == '-') {
631 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
632 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
633 bond->dev->name, &newtarget);
638 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
639 if (targets[i] == newtarget) {
641 pr_info("%s: removing ARP target %pI4.\n",
642 bond->dev->name, &newtarget);
643 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
644 targets[j] = targets[j+1];
651 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
652 bond->dev->name, &newtarget);
657 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
666 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
669 * Show and set the up and down delays. These must be multiples of the
670 * MII monitoring value, and are stored internally as the multiplier.
671 * Thus, we must translate to MS for the real world.
673 static ssize_t bonding_show_downdelay(struct device *d,
674 struct device_attribute *attr,
677 struct bonding *bond = to_bond(d);
679 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
682 static ssize_t bonding_store_downdelay(struct device *d,
683 struct device_attribute *attr,
684 const char *buf, size_t count)
686 int new_value, ret = count;
687 struct bonding *bond = to_bond(d);
689 if (!(bond->params.miimon)) {
690 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
696 if (sscanf(buf, "%d", &new_value) != 1) {
697 pr_err("%s: no down delay value specified.\n", bond->dev->name);
702 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
703 bond->dev->name, new_value, 1, INT_MAX);
707 if ((new_value % bond->params.miimon) != 0) {
708 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
709 bond->dev->name, new_value,
711 (new_value / bond->params.miimon) *
712 bond->params.miimon);
714 bond->params.downdelay = new_value / bond->params.miimon;
715 pr_info("%s: Setting down delay to %d.\n",
717 bond->params.downdelay * bond->params.miimon);
724 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
725 bonding_show_downdelay, bonding_store_downdelay);
727 static ssize_t bonding_show_updelay(struct device *d,
728 struct device_attribute *attr,
731 struct bonding *bond = to_bond(d);
733 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
737 static ssize_t bonding_store_updelay(struct device *d,
738 struct device_attribute *attr,
739 const char *buf, size_t count)
741 int new_value, ret = count;
742 struct bonding *bond = to_bond(d);
744 if (!(bond->params.miimon)) {
745 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
751 if (sscanf(buf, "%d", &new_value) != 1) {
752 pr_err("%s: no up delay value specified.\n",
758 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
759 bond->dev->name, new_value, 1, INT_MAX);
763 if ((new_value % bond->params.miimon) != 0) {
764 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
765 bond->dev->name, new_value,
767 (new_value / bond->params.miimon) *
768 bond->params.miimon);
770 bond->params.updelay = new_value / bond->params.miimon;
771 pr_info("%s: Setting up delay to %d.\n",
773 bond->params.updelay * bond->params.miimon);
779 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
780 bonding_show_updelay, bonding_store_updelay);
783 * Show and set the LACP interval. Interface must be down, and the mode
784 * must be set to 802.3ad mode.
786 static ssize_t bonding_show_lacp(struct device *d,
787 struct device_attribute *attr,
790 struct bonding *bond = to_bond(d);
792 return sprintf(buf, "%s %d\n",
793 bond_lacp_tbl[bond->params.lacp_fast].modename,
794 bond->params.lacp_fast);
797 static ssize_t bonding_store_lacp(struct device *d,
798 struct device_attribute *attr,
799 const char *buf, size_t count)
801 int new_value, ret = count;
802 struct bonding *bond = to_bond(d);
804 if (bond->dev->flags & IFF_UP) {
805 pr_err("%s: Unable to update LACP rate because interface is up.\n",
811 if (bond->params.mode != BOND_MODE_8023AD) {
812 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
818 new_value = bond_parse_parm(buf, bond_lacp_tbl);
820 if ((new_value == 1) || (new_value == 0)) {
821 bond->params.lacp_fast = new_value;
822 pr_info("%s: Setting LACP rate to %s (%d).\n",
823 bond->dev->name, bond_lacp_tbl[new_value].modename,
826 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
827 bond->dev->name, (int)strlen(buf) - 1, buf);
833 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
834 bonding_show_lacp, bonding_store_lacp);
836 static ssize_t bonding_show_ad_select(struct device *d,
837 struct device_attribute *attr,
840 struct bonding *bond = to_bond(d);
842 return sprintf(buf, "%s %d\n",
843 ad_select_tbl[bond->params.ad_select].modename,
844 bond->params.ad_select);
848 static ssize_t bonding_store_ad_select(struct device *d,
849 struct device_attribute *attr,
850 const char *buf, size_t count)
852 int new_value, ret = count;
853 struct bonding *bond = to_bond(d);
855 if (bond->dev->flags & IFF_UP) {
856 pr_err("%s: Unable to update ad_select because interface is up.\n",
862 new_value = bond_parse_parm(buf, ad_select_tbl);
864 if (new_value != -1) {
865 bond->params.ad_select = new_value;
866 pr_info("%s: Setting ad_select to %s (%d).\n",
867 bond->dev->name, ad_select_tbl[new_value].modename,
870 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
871 bond->dev->name, (int)strlen(buf) - 1, buf);
877 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
878 bonding_show_ad_select, bonding_store_ad_select);
881 * Show and set the number of grat ARP to send after a failover event.
883 static ssize_t bonding_show_n_grat_arp(struct device *d,
884 struct device_attribute *attr,
887 struct bonding *bond = to_bond(d);
889 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
892 static ssize_t bonding_store_n_grat_arp(struct device *d,
893 struct device_attribute *attr,
894 const char *buf, size_t count)
896 int new_value, ret = count;
897 struct bonding *bond = to_bond(d);
899 if (sscanf(buf, "%d", &new_value) != 1) {
900 pr_err("%s: no num_grat_arp value specified.\n",
905 if (new_value < 0 || new_value > 255) {
906 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
907 bond->dev->name, new_value);
911 bond->params.num_grat_arp = new_value;
916 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
917 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
920 * Show and set the number of unsolicited NA's to send after a failover event.
922 static ssize_t bonding_show_n_unsol_na(struct device *d,
923 struct device_attribute *attr,
926 struct bonding *bond = to_bond(d);
928 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
931 static ssize_t bonding_store_n_unsol_na(struct device *d,
932 struct device_attribute *attr,
933 const char *buf, size_t count)
935 int new_value, ret = count;
936 struct bonding *bond = to_bond(d);
938 if (sscanf(buf, "%d", &new_value) != 1) {
939 pr_err("%s: no num_unsol_na value specified.\n",
945 if (new_value < 0 || new_value > 255) {
946 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
947 bond->dev->name, new_value);
951 bond->params.num_unsol_na = new_value;
955 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
956 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
959 * Show and set the MII monitor interval. There are two tricky bits
960 * here. First, if MII monitoring is activated, then we must disable
961 * ARP monitoring. Second, if the timer isn't running, we must
964 static ssize_t bonding_show_miimon(struct device *d,
965 struct device_attribute *attr,
968 struct bonding *bond = to_bond(d);
970 return sprintf(buf, "%d\n", bond->params.miimon);
973 static ssize_t bonding_store_miimon(struct device *d,
974 struct device_attribute *attr,
975 const char *buf, size_t count)
977 int new_value, ret = count;
978 struct bonding *bond = to_bond(d);
980 if (sscanf(buf, "%d", &new_value) != 1) {
981 pr_err("%s: no miimon value specified.\n",
987 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
988 bond->dev->name, new_value, 1, INT_MAX);
992 pr_info("%s: Setting MII monitoring interval to %d.\n",
993 bond->dev->name, new_value);
994 bond->params.miimon = new_value;
995 if (bond->params.updelay)
996 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
998 bond->params.updelay * bond->params.miimon);
999 if (bond->params.downdelay)
1000 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1002 bond->params.downdelay * bond->params.miimon);
1003 if (bond->params.arp_interval) {
1004 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1006 bond->params.arp_interval = 0;
1007 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1008 if (bond->params.arp_validate) {
1009 bond_unregister_arp(bond);
1010 bond->params.arp_validate =
1011 BOND_ARP_VALIDATE_NONE;
1013 if (delayed_work_pending(&bond->arp_work)) {
1014 cancel_delayed_work(&bond->arp_work);
1015 flush_workqueue(bond->wq);
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
1025 if (!delayed_work_pending(&bond->mii_work)) {
1026 INIT_DELAYED_WORK(&bond->mii_work,
1028 queue_delayed_work(bond->wq,
1029 &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)
1064 struct slave *slave;
1065 struct bonding *bond = to_bond(d);
1067 if (!rtnl_trylock())
1068 return restart_syscall();
1070 read_lock(&bond->lock);
1071 write_lock_bh(&bond->curr_slave_lock);
1073 if (!USES_PRIMARY(bond->params.mode)) {
1074 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1075 bond->dev->name, bond->dev->name, bond->params.mode);
1077 bond_for_each_slave(bond, slave, i) {
1079 (slave->dev->name, buf,
1080 strlen(slave->dev->name)) == 0) {
1081 pr_info("%s: Setting %s as primary slave.\n",
1082 bond->dev->name, slave->dev->name);
1083 bond->primary_slave = slave;
1084 strcpy(bond->params.primary, slave->dev->name);
1085 bond_select_active_slave(bond);
1090 /* if we got here, then we didn't match the name of any slave */
1092 if (strlen(buf) == 0 || buf[0] == '\n') {
1093 pr_info("%s: Setting primary slave to None.\n",
1095 bond->primary_slave = NULL;
1096 bond_select_active_slave(bond);
1098 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1099 bond->dev->name, (int)strlen(buf) - 1, buf);
1103 write_unlock_bh(&bond->curr_slave_lock);
1104 read_unlock(&bond->lock);
1105 unblock_netpoll_tx();
1110 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1111 bonding_show_primary, bonding_store_primary);
1114 * Show and set the primary_reselect flag.
1116 static ssize_t bonding_show_primary_reselect(struct device *d,
1117 struct device_attribute *attr,
1120 struct bonding *bond = to_bond(d);
1122 return sprintf(buf, "%s %d\n",
1123 pri_reselect_tbl[bond->params.primary_reselect].modename,
1124 bond->params.primary_reselect);
1127 static ssize_t bonding_store_primary_reselect(struct device *d,
1128 struct device_attribute *attr,
1129 const char *buf, size_t count)
1131 int new_value, ret = count;
1132 struct bonding *bond = to_bond(d);
1134 if (!rtnl_trylock())
1135 return restart_syscall();
1137 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1138 if (new_value < 0) {
1139 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1141 (int) strlen(buf) - 1, buf);
1146 bond->params.primary_reselect = new_value;
1147 pr_info("%s: setting primary_reselect to %s (%d).\n",
1148 bond->dev->name, pri_reselect_tbl[new_value].modename,
1152 read_lock(&bond->lock);
1153 write_lock_bh(&bond->curr_slave_lock);
1154 bond_select_active_slave(bond);
1155 write_unlock_bh(&bond->curr_slave_lock);
1156 read_unlock(&bond->lock);
1157 unblock_netpoll_tx();
1162 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1163 bonding_show_primary_reselect,
1164 bonding_store_primary_reselect);
1167 * Show and set the use_carrier flag.
1169 static ssize_t bonding_show_carrier(struct device *d,
1170 struct device_attribute *attr,
1173 struct bonding *bond = to_bond(d);
1175 return sprintf(buf, "%d\n", bond->params.use_carrier);
1178 static ssize_t bonding_store_carrier(struct device *d,
1179 struct device_attribute *attr,
1180 const char *buf, size_t count)
1182 int new_value, ret = count;
1183 struct bonding *bond = to_bond(d);
1186 if (sscanf(buf, "%d", &new_value) != 1) {
1187 pr_err("%s: no use_carrier value specified.\n",
1192 if ((new_value == 0) || (new_value == 1)) {
1193 bond->params.use_carrier = new_value;
1194 pr_info("%s: Setting use_carrier to %d.\n",
1195 bond->dev->name, new_value);
1197 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1198 bond->dev->name, new_value);
1203 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1204 bonding_show_carrier, bonding_store_carrier);
1208 * Show and set currently active_slave.
1210 static ssize_t bonding_show_active_slave(struct device *d,
1211 struct device_attribute *attr,
1215 struct bonding *bond = to_bond(d);
1218 read_lock(&bond->curr_slave_lock);
1219 curr = bond->curr_active_slave;
1220 read_unlock(&bond->curr_slave_lock);
1222 if (USES_PRIMARY(bond->params.mode) && curr)
1223 count = sprintf(buf, "%s\n", curr->dev->name);
1227 static ssize_t bonding_store_active_slave(struct device *d,
1228 struct device_attribute *attr,
1229 const char *buf, size_t count)
1232 struct slave *slave;
1233 struct slave *old_active = NULL;
1234 struct slave *new_active = NULL;
1235 struct bonding *bond = to_bond(d);
1237 if (!rtnl_trylock())
1238 return restart_syscall();
1241 read_lock(&bond->lock);
1242 write_lock_bh(&bond->curr_slave_lock);
1244 if (!USES_PRIMARY(bond->params.mode))
1245 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1246 bond->dev->name, bond->dev->name, bond->params.mode);
1248 bond_for_each_slave(bond, slave, i) {
1250 (slave->dev->name, buf,
1251 strlen(slave->dev->name)) == 0) {
1252 old_active = bond->curr_active_slave;
1254 if (new_active == old_active) {
1256 pr_info("%s: %s is already the current active slave.\n",
1264 (new_active->link == BOND_LINK_UP) &&
1265 IS_UP(new_active->dev)) {
1266 pr_info("%s: Setting %s as active slave.\n",
1269 bond_change_active_slave(bond, new_active);
1272 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1282 /* if we got here, then we didn't match the name of any slave */
1284 if (strlen(buf) == 0 || buf[0] == '\n') {
1285 pr_info("%s: Setting active slave to None.\n",
1287 bond->primary_slave = NULL;
1288 bond_select_active_slave(bond);
1290 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
1291 bond->dev->name, (int)strlen(buf) - 1, buf);
1295 write_unlock_bh(&bond->curr_slave_lock);
1296 read_unlock(&bond->lock);
1297 unblock_netpoll_tx();
1304 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1305 bonding_show_active_slave, bonding_store_active_slave);
1309 * Show link status of the bond interface.
1311 static ssize_t bonding_show_mii_status(struct device *d,
1312 struct device_attribute *attr,
1316 struct bonding *bond = to_bond(d);
1318 read_lock(&bond->curr_slave_lock);
1319 curr = bond->curr_active_slave;
1320 read_unlock(&bond->curr_slave_lock);
1322 return sprintf(buf, "%s\n", curr ? "up" : "down");
1324 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1328 * Show current 802.3ad aggregator ID.
1330 static ssize_t bonding_show_ad_aggregator(struct device *d,
1331 struct device_attribute *attr,
1335 struct bonding *bond = to_bond(d);
1337 if (bond->params.mode == BOND_MODE_8023AD) {
1338 struct ad_info ad_info;
1339 count = sprintf(buf, "%d\n",
1340 (bond_3ad_get_active_agg_info(bond, &ad_info))
1341 ? 0 : ad_info.aggregator_id);
1346 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1350 * Show number of active 802.3ad ports.
1352 static ssize_t bonding_show_ad_num_ports(struct device *d,
1353 struct device_attribute *attr,
1357 struct bonding *bond = to_bond(d);
1359 if (bond->params.mode == BOND_MODE_8023AD) {
1360 struct ad_info ad_info;
1361 count = sprintf(buf, "%d\n",
1362 (bond_3ad_get_active_agg_info(bond, &ad_info))
1363 ? 0 : ad_info.ports);
1368 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1372 * Show current 802.3ad actor key.
1374 static ssize_t bonding_show_ad_actor_key(struct device *d,
1375 struct device_attribute *attr,
1379 struct bonding *bond = to_bond(d);
1381 if (bond->params.mode == BOND_MODE_8023AD) {
1382 struct ad_info ad_info;
1383 count = sprintf(buf, "%d\n",
1384 (bond_3ad_get_active_agg_info(bond, &ad_info))
1385 ? 0 : ad_info.actor_key);
1390 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1394 * Show current 802.3ad partner key.
1396 static ssize_t bonding_show_ad_partner_key(struct device *d,
1397 struct device_attribute *attr,
1401 struct bonding *bond = to_bond(d);
1403 if (bond->params.mode == BOND_MODE_8023AD) {
1404 struct ad_info ad_info;
1405 count = sprintf(buf, "%d\n",
1406 (bond_3ad_get_active_agg_info(bond, &ad_info))
1407 ? 0 : ad_info.partner_key);
1412 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1416 * Show current 802.3ad partner mac.
1418 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1419 struct device_attribute *attr,
1423 struct bonding *bond = to_bond(d);
1425 if (bond->params.mode == BOND_MODE_8023AD) {
1426 struct ad_info ad_info;
1427 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1428 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1433 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1436 * Show the queue_ids of the slaves in the current bond.
1438 static ssize_t bonding_show_queue_id(struct device *d,
1439 struct device_attribute *attr,
1442 struct slave *slave;
1444 struct bonding *bond = to_bond(d);
1446 if (!rtnl_trylock())
1447 return restart_syscall();
1449 read_lock(&bond->lock);
1450 bond_for_each_slave(bond, slave, i) {
1451 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1452 /* not enough space for another interface_name:queue_id pair */
1453 if ((PAGE_SIZE - res) > 10)
1454 res = PAGE_SIZE - 10;
1455 res += sprintf(buf + res, "++more++ ");
1458 res += sprintf(buf + res, "%s:%d ",
1459 slave->dev->name, slave->queue_id);
1461 read_unlock(&bond->lock);
1463 buf[res-1] = '\n'; /* eat the leftover space */
1469 * Set the queue_ids of the slaves in the current bond. The bond
1470 * interface must be enslaved for this to work.
1472 static ssize_t bonding_store_queue_id(struct device *d,
1473 struct device_attribute *attr,
1474 const char *buffer, size_t count)
1476 struct slave *slave, *update_slave;
1477 struct bonding *bond = to_bond(d);
1481 struct net_device *sdev = NULL;
1483 if (!rtnl_trylock())
1484 return restart_syscall();
1486 /* delim will point to queue id if successful */
1487 delim = strchr(buffer, ':');
1492 * Terminate string that points to device name and bump it
1493 * up one, so we can read the queue id there.
1496 if (sscanf(++delim, "%hd\n", &qid) != 1)
1499 /* Check buffer length, valid ifname and queue id */
1500 if (strlen(buffer) > IFNAMSIZ ||
1501 !dev_valid_name(buffer) ||
1502 qid > bond->params.tx_queues)
1505 /* Get the pointer to that interface if it exists */
1506 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1510 read_lock(&bond->lock);
1512 /* Search for thes slave and check for duplicate qids */
1513 update_slave = NULL;
1514 bond_for_each_slave(bond, slave, i) {
1515 if (sdev == slave->dev)
1517 * We don't need to check the matching
1518 * slave for dups, since we're overwriting it
1520 update_slave = slave;
1521 else if (qid && qid == slave->queue_id) {
1522 goto err_no_cmd_unlock;
1527 goto err_no_cmd_unlock;
1529 /* Actually set the qids for the slave */
1530 update_slave->queue_id = qid;
1532 read_unlock(&bond->lock);
1538 read_unlock(&bond->lock);
1540 pr_info("invalid input for queue_id set for %s.\n",
1546 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1547 bonding_store_queue_id);
1551 * Show and set the all_slaves_active flag.
1553 static ssize_t bonding_show_slaves_active(struct device *d,
1554 struct device_attribute *attr,
1557 struct bonding *bond = to_bond(d);
1559 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1562 static ssize_t bonding_store_slaves_active(struct device *d,
1563 struct device_attribute *attr,
1564 const char *buf, size_t count)
1566 int i, new_value, ret = count;
1567 struct bonding *bond = to_bond(d);
1568 struct slave *slave;
1570 if (sscanf(buf, "%d", &new_value) != 1) {
1571 pr_err("%s: no all_slaves_active value specified.\n",
1577 if (new_value == bond->params.all_slaves_active)
1580 if ((new_value == 0) || (new_value == 1)) {
1581 bond->params.all_slaves_active = new_value;
1583 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1584 bond->dev->name, new_value);
1589 bond_for_each_slave(bond, slave, i) {
1590 if (slave->state == BOND_STATE_BACKUP) {
1592 slave->dev->priv_flags &= ~IFF_SLAVE_INACTIVE;
1594 slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
1600 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1601 bonding_show_slaves_active, bonding_store_slaves_active);
1604 * Show and set the number of IGMP membership reports to send on link failure
1606 static ssize_t bonding_show_resend_igmp(struct device *d,
1607 struct device_attribute *attr,
1610 struct bonding *bond = to_bond(d);
1612 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1615 static ssize_t bonding_store_resend_igmp(struct device *d,
1616 struct device_attribute *attr,
1617 const char *buf, size_t count)
1619 int new_value, ret = count;
1620 struct bonding *bond = to_bond(d);
1622 if (sscanf(buf, "%d", &new_value) != 1) {
1623 pr_err("%s: no resend_igmp value specified.\n",
1629 if (new_value < 0) {
1630 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1631 bond->dev->name, new_value);
1636 pr_info("%s: Setting resend_igmp to %d.\n",
1637 bond->dev->name, new_value);
1638 bond->params.resend_igmp = new_value;
1643 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1644 bonding_show_resend_igmp, bonding_store_resend_igmp);
1646 static struct attribute *per_bond_attrs[] = {
1647 &dev_attr_slaves.attr,
1648 &dev_attr_mode.attr,
1649 &dev_attr_fail_over_mac.attr,
1650 &dev_attr_arp_validate.attr,
1651 &dev_attr_arp_interval.attr,
1652 &dev_attr_arp_ip_target.attr,
1653 &dev_attr_downdelay.attr,
1654 &dev_attr_updelay.attr,
1655 &dev_attr_lacp_rate.attr,
1656 &dev_attr_ad_select.attr,
1657 &dev_attr_xmit_hash_policy.attr,
1658 &dev_attr_num_grat_arp.attr,
1659 &dev_attr_num_unsol_na.attr,
1660 &dev_attr_miimon.attr,
1661 &dev_attr_primary.attr,
1662 &dev_attr_primary_reselect.attr,
1663 &dev_attr_use_carrier.attr,
1664 &dev_attr_active_slave.attr,
1665 &dev_attr_mii_status.attr,
1666 &dev_attr_ad_aggregator.attr,
1667 &dev_attr_ad_num_ports.attr,
1668 &dev_attr_ad_actor_key.attr,
1669 &dev_attr_ad_partner_key.attr,
1670 &dev_attr_ad_partner_mac.attr,
1671 &dev_attr_queue_id.attr,
1672 &dev_attr_all_slaves_active.attr,
1673 &dev_attr_resend_igmp.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(void)
1690 ret = netdev_class_create_file(&class_attr_bonding_masters);
1692 * Permit multiple loads of the module by ignoring failures to
1693 * create the bonding_masters sysfs file. Bonding devices
1694 * created by second or subsequent loads of the module will
1695 * not be listed in, or controllable by, bonding_masters, but
1696 * will have the usual "bonding" sysfs directory.
1698 * This is done to preserve backwards compatibility for
1699 * initscripts/sysconfig, which load bonding multiple times to
1700 * configure multiple bonding devices.
1702 if (ret == -EEXIST) {
1703 /* Is someone being kinky and naming a device bonding_master? */
1704 if (__dev_get_by_name(&init_net,
1705 class_attr_bonding_masters.attr.name))
1706 pr_err("network device named %s already exists in sysfs",
1707 class_attr_bonding_masters.attr.name);
1716 * Remove /sys/class/net/bonding_masters.
1718 void bond_destroy_sysfs(void)
1720 netdev_class_remove_file(&class_attr_bonding_masters);
1724 * Initialize sysfs for each bond. This sets up and registers
1725 * the 'bondctl' directory for each individual bond under /sys/class/net.
1727 void bond_prepare_sysfs_group(struct bonding *bond)
1729 bond->dev->sysfs_groups[0] = &bonding_group;