bonding: prevent deadlock on slave store with alb mode (v3)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / bonding / bond_sysfs.c
blobddc316500fa8a0394beb70ba741f28ab79e56000
1 /*
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
12 * for more details.
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>
30 #include <linux/fs.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/netdevice.h>
34 #include <linux/inetdevice.h>
35 #include <linux/in.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>
45 #include "bonding.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,
56 char *buf)
58 struct net *net = current->nsproxy->net_ns;
59 struct bond_net *bn = net_generic(net, bond_net_id);
60 int res = 0;
61 struct bonding *bond;
63 rtnl_lock();
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)
69 res = PAGE_SIZE - 10;
70 res += sprintf(buf + res, "++more++ ");
71 break;
73 res += sprintf(buf + res, "%s ", bond->dev->name);
75 if (res)
76 buf[res-1] = '\n'; /* eat the leftover space */
78 rtnl_unlock();
79 return res;
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);
85 struct bonding *bond;
87 list_for_each_entry(bond, &bn->dev_list, bond_list) {
88 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
89 return bond->dev;
91 return NULL;
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, };
108 char *ifname;
109 int rv, res = count;
111 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
112 ifname = command + 1;
113 if ((strlen(command) <= 1) ||
114 !dev_valid_name(ifname))
115 goto err_no_cmd;
117 if (command[0] == '+') {
118 pr_info("%s is being created...\n", ifname);
119 rv = bond_create(net, ifname);
120 if (rv) {
121 pr_info("Bond creation failed.\n");
122 res = rv;
124 } else if (command[0] == '-') {
125 struct net_device *bond_dev;
127 rtnl_lock();
128 bond_dev = bond_get_by_name(net, ifname);
129 if (bond_dev) {
130 pr_info("%s is being deleted...\n", ifname);
131 unregister_netdevice(bond_dev);
132 } else {
133 pr_err("unable to delete non-existent %s\n", ifname);
134 res = -ENODEV;
136 rtnl_unlock();
137 } else
138 goto err_no_cmd;
140 /* Always return either count or an error. If you return 0, you'll
141 * get called forever, which is bad.
143 return res;
145 err_no_cmd:
146 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
147 return -EPERM;
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];
158 int ret = 0;
160 /* first, create a link from the slave back to the master */
161 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
162 "master");
163 if (ret)
164 return ret;
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),
168 linkname);
169 return ret;
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)
190 struct slave *slave;
191 int i, res = 0;
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++ ");
201 break;
203 res += sprintf(buf + res, "%s ", slave->dev->name);
205 read_unlock(&bond->lock);
206 if (res)
207 buf[res-1] = '\n'; /* eat the leftover space */
208 return res;
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, };
222 char *ifname;
223 int res, ret = count;
224 struct net_device *dev;
225 struct bonding *bond = to_bond(d);
227 if (!rtnl_trylock())
228 return restart_syscall();
230 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
231 ifname = command + 1;
232 if ((strlen(command) <= 1) ||
233 !dev_valid_name(ifname))
234 goto err_no_cmd;
236 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
237 if (!dev) {
238 pr_info("%s: Interface %s does not exist!\n",
239 bond->dev->name, ifname);
240 ret = -ENODEV;
241 goto out;
244 switch (command[0]) {
245 case '+':
246 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
247 res = bond_enslave(bond->dev, dev);
248 break;
250 case '-':
251 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
252 res = bond_release(bond->dev, dev);
253 break;
255 default:
256 goto err_no_cmd;
259 if (res)
260 ret = res;
261 goto out;
263 err_no_cmd:
264 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
265 bond->dev->name);
266 ret = -EPERM;
268 out:
269 rtnl_unlock();
270 return ret;
273 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
274 bonding_store_slaves);
277 * Show and set the bonding mode. The bond interface must be down to
278 * change the mode.
280 static ssize_t bonding_show_mode(struct device *d,
281 struct device_attribute *attr, char *buf)
283 struct bonding *bond = to_bond(d);
285 return sprintf(buf, "%s %d\n",
286 bond_mode_tbl[bond->params.mode].modename,
287 bond->params.mode);
290 static ssize_t bonding_store_mode(struct device *d,
291 struct device_attribute *attr,
292 const char *buf, size_t count)
294 int new_value, ret = count;
295 struct bonding *bond = to_bond(d);
297 if (bond->dev->flags & IFF_UP) {
298 pr_err("unable to update mode of %s because interface is up.\n",
299 bond->dev->name);
300 ret = -EPERM;
301 goto out;
304 new_value = bond_parse_parm(buf, bond_mode_tbl);
305 if (new_value < 0) {
306 pr_err("%s: Ignoring invalid mode value %.*s.\n",
307 bond->dev->name, (int)strlen(buf) - 1, buf);
308 ret = -EINVAL;
309 goto out;
311 if ((new_value == BOND_MODE_ALB ||
312 new_value == BOND_MODE_TLB) &&
313 bond->params.arp_interval) {
314 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
315 bond->dev->name, bond_mode_tbl[new_value].modename);
316 ret = -EINVAL;
317 goto out;
319 if (bond->params.mode == BOND_MODE_8023AD)
320 bond_unset_master_3ad_flags(bond);
322 if (bond->params.mode == BOND_MODE_ALB)
323 bond_unset_master_alb_flags(bond);
325 bond->params.mode = new_value;
326 bond_set_mode_ops(bond, bond->params.mode);
327 pr_info("%s: setting mode to %s (%d).\n",
328 bond->dev->name, bond_mode_tbl[new_value].modename,
329 new_value);
330 out:
331 return ret;
333 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
334 bonding_show_mode, bonding_store_mode);
337 * Show and set the bonding transmit hash method.
338 * The bond interface must be down to change the xmit hash policy.
340 static ssize_t bonding_show_xmit_hash(struct device *d,
341 struct device_attribute *attr,
342 char *buf)
344 struct bonding *bond = to_bond(d);
346 return sprintf(buf, "%s %d\n",
347 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
348 bond->params.xmit_policy);
351 static ssize_t bonding_store_xmit_hash(struct device *d,
352 struct device_attribute *attr,
353 const char *buf, size_t count)
355 int new_value, ret = count;
356 struct bonding *bond = to_bond(d);
358 if (bond->dev->flags & IFF_UP) {
359 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
360 bond->dev->name);
361 ret = -EPERM;
362 goto out;
365 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
366 if (new_value < 0) {
367 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
368 bond->dev->name,
369 (int)strlen(buf) - 1, buf);
370 ret = -EINVAL;
371 goto out;
372 } else {
373 bond->params.xmit_policy = new_value;
374 bond_set_mode_ops(bond, bond->params.mode);
375 pr_info("%s: setting xmit hash policy to %s (%d).\n",
376 bond->dev->name,
377 xmit_hashtype_tbl[new_value].modename, new_value);
379 out:
380 return ret;
382 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
383 bonding_show_xmit_hash, bonding_store_xmit_hash);
386 * Show and set arp_validate.
388 static ssize_t bonding_show_arp_validate(struct device *d,
389 struct device_attribute *attr,
390 char *buf)
392 struct bonding *bond = to_bond(d);
394 return sprintf(buf, "%s %d\n",
395 arp_validate_tbl[bond->params.arp_validate].modename,
396 bond->params.arp_validate);
399 static ssize_t bonding_store_arp_validate(struct device *d,
400 struct device_attribute *attr,
401 const char *buf, size_t count)
403 int new_value;
404 struct bonding *bond = to_bond(d);
406 new_value = bond_parse_parm(buf, arp_validate_tbl);
407 if (new_value < 0) {
408 pr_err("%s: Ignoring invalid arp_validate value %s\n",
409 bond->dev->name, buf);
410 return -EINVAL;
412 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
413 pr_err("%s: arp_validate only supported in active-backup mode.\n",
414 bond->dev->name);
415 return -EINVAL;
417 pr_info("%s: setting arp_validate to %s (%d).\n",
418 bond->dev->name, arp_validate_tbl[new_value].modename,
419 new_value);
421 if (!bond->params.arp_validate && new_value)
422 bond_register_arp(bond);
423 else if (bond->params.arp_validate && !new_value)
424 bond_unregister_arp(bond);
426 bond->params.arp_validate = new_value;
428 return count;
431 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
432 bonding_store_arp_validate);
435 * Show and store fail_over_mac. User only allowed to change the
436 * value when there are no slaves.
438 static ssize_t bonding_show_fail_over_mac(struct device *d,
439 struct device_attribute *attr,
440 char *buf)
442 struct bonding *bond = to_bond(d);
444 return sprintf(buf, "%s %d\n",
445 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
446 bond->params.fail_over_mac);
449 static ssize_t bonding_store_fail_over_mac(struct device *d,
450 struct device_attribute *attr,
451 const char *buf, size_t count)
453 int new_value;
454 struct bonding *bond = to_bond(d);
456 if (bond->slave_cnt != 0) {
457 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
458 bond->dev->name);
459 return -EPERM;
462 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
463 if (new_value < 0) {
464 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
465 bond->dev->name, buf);
466 return -EINVAL;
469 bond->params.fail_over_mac = new_value;
470 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
471 bond->dev->name, fail_over_mac_tbl[new_value].modename,
472 new_value);
474 return count;
477 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
478 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
481 * Show and set the arp timer interval. There are two tricky bits
482 * here. First, if ARP monitoring is activated, then we must disable
483 * MII monitoring. Second, if the ARP timer isn't running, we must
484 * start it.
486 static ssize_t bonding_show_arp_interval(struct device *d,
487 struct device_attribute *attr,
488 char *buf)
490 struct bonding *bond = to_bond(d);
492 return sprintf(buf, "%d\n", bond->params.arp_interval);
495 static ssize_t bonding_store_arp_interval(struct device *d,
496 struct device_attribute *attr,
497 const char *buf, size_t count)
499 int new_value, ret = count;
500 struct bonding *bond = to_bond(d);
502 if (sscanf(buf, "%d", &new_value) != 1) {
503 pr_err("%s: no arp_interval value specified.\n",
504 bond->dev->name);
505 ret = -EINVAL;
506 goto out;
508 if (new_value < 0) {
509 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
510 bond->dev->name, new_value, INT_MAX);
511 ret = -EINVAL;
512 goto out;
514 if (bond->params.mode == BOND_MODE_ALB ||
515 bond->params.mode == BOND_MODE_TLB) {
516 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
517 bond->dev->name, bond->dev->name);
518 ret = -EINVAL;
519 goto out;
521 pr_info("%s: Setting ARP monitoring interval to %d.\n",
522 bond->dev->name, new_value);
523 bond->params.arp_interval = new_value;
524 if (bond->params.arp_interval)
525 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
526 if (bond->params.miimon) {
527 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
528 bond->dev->name, bond->dev->name);
529 bond->params.miimon = 0;
530 if (delayed_work_pending(&bond->mii_work)) {
531 cancel_delayed_work(&bond->mii_work);
532 flush_workqueue(bond->wq);
535 if (!bond->params.arp_targets[0]) {
536 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
537 bond->dev->name);
539 if (bond->dev->flags & IFF_UP) {
540 /* If the interface is up, we may need to fire off
541 * the ARP timer. If the interface is down, the
542 * timer will get fired off when the open function
543 * is called.
545 if (!delayed_work_pending(&bond->arp_work)) {
546 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
547 INIT_DELAYED_WORK(&bond->arp_work,
548 bond_activebackup_arp_mon);
549 else
550 INIT_DELAYED_WORK(&bond->arp_work,
551 bond_loadbalance_arp_mon);
553 queue_delayed_work(bond->wq, &bond->arp_work, 0);
557 out:
558 return ret;
560 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
561 bonding_show_arp_interval, bonding_store_arp_interval);
564 * Show and set the arp targets.
566 static ssize_t bonding_show_arp_targets(struct device *d,
567 struct device_attribute *attr,
568 char *buf)
570 int i, res = 0;
571 struct bonding *bond = to_bond(d);
573 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
574 if (bond->params.arp_targets[i])
575 res += sprintf(buf + res, "%pI4 ",
576 &bond->params.arp_targets[i]);
578 if (res)
579 buf[res-1] = '\n'; /* eat the leftover space */
580 return res;
583 static ssize_t bonding_store_arp_targets(struct device *d,
584 struct device_attribute *attr,
585 const char *buf, size_t count)
587 __be32 newtarget;
588 int i = 0, done = 0, ret = count;
589 struct bonding *bond = to_bond(d);
590 __be32 *targets;
592 targets = bond->params.arp_targets;
593 newtarget = in_aton(buf + 1);
594 /* look for adds */
595 if (buf[0] == '+') {
596 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
597 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
598 bond->dev->name, &newtarget);
599 ret = -EINVAL;
600 goto out;
602 /* look for an empty slot to put the target in, and check for dupes */
603 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
604 if (targets[i] == newtarget) { /* duplicate */
605 pr_err("%s: ARP target %pI4 is already present\n",
606 bond->dev->name, &newtarget);
607 ret = -EINVAL;
608 goto out;
610 if (targets[i] == 0) {
611 pr_info("%s: adding ARP target %pI4.\n",
612 bond->dev->name, &newtarget);
613 done = 1;
614 targets[i] = newtarget;
617 if (!done) {
618 pr_err("%s: ARP target table is full!\n",
619 bond->dev->name);
620 ret = -EINVAL;
621 goto out;
624 } else if (buf[0] == '-') {
625 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
626 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
627 bond->dev->name, &newtarget);
628 ret = -EINVAL;
629 goto out;
632 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
633 if (targets[i] == newtarget) {
634 int j;
635 pr_info("%s: removing ARP target %pI4.\n",
636 bond->dev->name, &newtarget);
637 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
638 targets[j] = targets[j+1];
640 targets[j] = 0;
641 done = 1;
644 if (!done) {
645 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
646 bond->dev->name, &newtarget);
647 ret = -EINVAL;
648 goto out;
650 } else {
651 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
652 bond->dev->name);
653 ret = -EPERM;
654 goto out;
657 out:
658 return ret;
660 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
663 * Show and set the up and down delays. These must be multiples of the
664 * MII monitoring value, and are stored internally as the multiplier.
665 * Thus, we must translate to MS for the real world.
667 static ssize_t bonding_show_downdelay(struct device *d,
668 struct device_attribute *attr,
669 char *buf)
671 struct bonding *bond = to_bond(d);
673 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
676 static ssize_t bonding_store_downdelay(struct device *d,
677 struct device_attribute *attr,
678 const char *buf, size_t count)
680 int new_value, ret = count;
681 struct bonding *bond = to_bond(d);
683 if (!(bond->params.miimon)) {
684 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
685 bond->dev->name);
686 ret = -EPERM;
687 goto out;
690 if (sscanf(buf, "%d", &new_value) != 1) {
691 pr_err("%s: no down delay value specified.\n", bond->dev->name);
692 ret = -EINVAL;
693 goto out;
695 if (new_value < 0) {
696 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
697 bond->dev->name, new_value, 1, INT_MAX);
698 ret = -EINVAL;
699 goto out;
700 } else {
701 if ((new_value % bond->params.miimon) != 0) {
702 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
703 bond->dev->name, new_value,
704 bond->params.miimon,
705 (new_value / bond->params.miimon) *
706 bond->params.miimon);
708 bond->params.downdelay = new_value / bond->params.miimon;
709 pr_info("%s: Setting down delay to %d.\n",
710 bond->dev->name,
711 bond->params.downdelay * bond->params.miimon);
715 out:
716 return ret;
718 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
719 bonding_show_downdelay, bonding_store_downdelay);
721 static ssize_t bonding_show_updelay(struct device *d,
722 struct device_attribute *attr,
723 char *buf)
725 struct bonding *bond = to_bond(d);
727 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
731 static ssize_t bonding_store_updelay(struct device *d,
732 struct device_attribute *attr,
733 const char *buf, size_t count)
735 int new_value, ret = count;
736 struct bonding *bond = to_bond(d);
738 if (!(bond->params.miimon)) {
739 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
740 bond->dev->name);
741 ret = -EPERM;
742 goto out;
745 if (sscanf(buf, "%d", &new_value) != 1) {
746 pr_err("%s: no up delay value specified.\n",
747 bond->dev->name);
748 ret = -EINVAL;
749 goto out;
751 if (new_value < 0) {
752 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
753 bond->dev->name, new_value, 1, INT_MAX);
754 ret = -EINVAL;
755 goto out;
756 } else {
757 if ((new_value % bond->params.miimon) != 0) {
758 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
759 bond->dev->name, new_value,
760 bond->params.miimon,
761 (new_value / bond->params.miimon) *
762 bond->params.miimon);
764 bond->params.updelay = new_value / bond->params.miimon;
765 pr_info("%s: Setting up delay to %d.\n",
766 bond->dev->name,
767 bond->params.updelay * bond->params.miimon);
770 out:
771 return ret;
773 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
774 bonding_show_updelay, bonding_store_updelay);
777 * Show and set the LACP interval. Interface must be down, and the mode
778 * must be set to 802.3ad mode.
780 static ssize_t bonding_show_lacp(struct device *d,
781 struct device_attribute *attr,
782 char *buf)
784 struct bonding *bond = to_bond(d);
786 return sprintf(buf, "%s %d\n",
787 bond_lacp_tbl[bond->params.lacp_fast].modename,
788 bond->params.lacp_fast);
791 static ssize_t bonding_store_lacp(struct device *d,
792 struct device_attribute *attr,
793 const char *buf, size_t count)
795 int new_value, ret = count;
796 struct bonding *bond = to_bond(d);
798 if (bond->dev->flags & IFF_UP) {
799 pr_err("%s: Unable to update LACP rate because interface is up.\n",
800 bond->dev->name);
801 ret = -EPERM;
802 goto out;
805 if (bond->params.mode != BOND_MODE_8023AD) {
806 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
807 bond->dev->name);
808 ret = -EPERM;
809 goto out;
812 new_value = bond_parse_parm(buf, bond_lacp_tbl);
814 if ((new_value == 1) || (new_value == 0)) {
815 bond->params.lacp_fast = new_value;
816 pr_info("%s: Setting LACP rate to %s (%d).\n",
817 bond->dev->name, bond_lacp_tbl[new_value].modename,
818 new_value);
819 } else {
820 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
821 bond->dev->name, (int)strlen(buf) - 1, buf);
822 ret = -EINVAL;
824 out:
825 return ret;
827 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
828 bonding_show_lacp, bonding_store_lacp);
830 static ssize_t bonding_show_ad_select(struct device *d,
831 struct device_attribute *attr,
832 char *buf)
834 struct bonding *bond = to_bond(d);
836 return sprintf(buf, "%s %d\n",
837 ad_select_tbl[bond->params.ad_select].modename,
838 bond->params.ad_select);
842 static ssize_t bonding_store_ad_select(struct device *d,
843 struct device_attribute *attr,
844 const char *buf, size_t count)
846 int new_value, ret = count;
847 struct bonding *bond = to_bond(d);
849 if (bond->dev->flags & IFF_UP) {
850 pr_err("%s: Unable to update ad_select because interface is up.\n",
851 bond->dev->name);
852 ret = -EPERM;
853 goto out;
856 new_value = bond_parse_parm(buf, ad_select_tbl);
858 if (new_value != -1) {
859 bond->params.ad_select = new_value;
860 pr_info("%s: Setting ad_select to %s (%d).\n",
861 bond->dev->name, ad_select_tbl[new_value].modename,
862 new_value);
863 } else {
864 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
865 bond->dev->name, (int)strlen(buf) - 1, buf);
866 ret = -EINVAL;
868 out:
869 return ret;
871 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
872 bonding_show_ad_select, bonding_store_ad_select);
875 * Show and set the number of grat ARP to send after a failover event.
877 static ssize_t bonding_show_n_grat_arp(struct device *d,
878 struct device_attribute *attr,
879 char *buf)
881 struct bonding *bond = to_bond(d);
883 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
886 static ssize_t bonding_store_n_grat_arp(struct device *d,
887 struct device_attribute *attr,
888 const char *buf, size_t count)
890 int new_value, ret = count;
891 struct bonding *bond = to_bond(d);
893 if (sscanf(buf, "%d", &new_value) != 1) {
894 pr_err("%s: no num_grat_arp value specified.\n",
895 bond->dev->name);
896 ret = -EINVAL;
897 goto out;
899 if (new_value < 0 || new_value > 255) {
900 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
901 bond->dev->name, new_value);
902 ret = -EINVAL;
903 goto out;
904 } else {
905 bond->params.num_grat_arp = new_value;
907 out:
908 return ret;
910 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
911 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
914 * Show and set the number of unsolicited NA's to send after a failover event.
916 static ssize_t bonding_show_n_unsol_na(struct device *d,
917 struct device_attribute *attr,
918 char *buf)
920 struct bonding *bond = to_bond(d);
922 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
925 static ssize_t bonding_store_n_unsol_na(struct device *d,
926 struct device_attribute *attr,
927 const char *buf, size_t count)
929 int new_value, ret = count;
930 struct bonding *bond = to_bond(d);
932 if (sscanf(buf, "%d", &new_value) != 1) {
933 pr_err("%s: no num_unsol_na value specified.\n",
934 bond->dev->name);
935 ret = -EINVAL;
936 goto out;
939 if (new_value < 0 || new_value > 255) {
940 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
941 bond->dev->name, new_value);
942 ret = -EINVAL;
943 goto out;
944 } else
945 bond->params.num_unsol_na = new_value;
946 out:
947 return ret;
949 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
950 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
953 * Show and set the MII monitor interval. There are two tricky bits
954 * here. First, if MII monitoring is activated, then we must disable
955 * ARP monitoring. Second, if the timer isn't running, we must
956 * start it.
958 static ssize_t bonding_show_miimon(struct device *d,
959 struct device_attribute *attr,
960 char *buf)
962 struct bonding *bond = to_bond(d);
964 return sprintf(buf, "%d\n", bond->params.miimon);
967 static ssize_t bonding_store_miimon(struct device *d,
968 struct device_attribute *attr,
969 const char *buf, size_t count)
971 int new_value, ret = count;
972 struct bonding *bond = to_bond(d);
974 if (sscanf(buf, "%d", &new_value) != 1) {
975 pr_err("%s: no miimon value specified.\n",
976 bond->dev->name);
977 ret = -EINVAL;
978 goto out;
980 if (new_value < 0) {
981 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
982 bond->dev->name, new_value, 1, INT_MAX);
983 ret = -EINVAL;
984 goto out;
985 } else {
986 pr_info("%s: Setting MII monitoring interval to %d.\n",
987 bond->dev->name, new_value);
988 bond->params.miimon = new_value;
989 if (bond->params.updelay)
990 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
991 bond->dev->name,
992 bond->params.updelay * bond->params.miimon);
993 if (bond->params.downdelay)
994 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
995 bond->dev->name,
996 bond->params.downdelay * bond->params.miimon);
997 if (bond->params.arp_interval) {
998 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
999 bond->dev->name);
1000 bond->params.arp_interval = 0;
1001 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1002 if (bond->params.arp_validate) {
1003 bond_unregister_arp(bond);
1004 bond->params.arp_validate =
1005 BOND_ARP_VALIDATE_NONE;
1007 if (delayed_work_pending(&bond->arp_work)) {
1008 cancel_delayed_work(&bond->arp_work);
1009 flush_workqueue(bond->wq);
1013 if (bond->dev->flags & IFF_UP) {
1014 /* If the interface is up, we may need to fire off
1015 * the MII timer. If the interface is down, the
1016 * timer will get fired off when the open function
1017 * is called.
1019 if (!delayed_work_pending(&bond->mii_work)) {
1020 INIT_DELAYED_WORK(&bond->mii_work,
1021 bond_mii_monitor);
1022 queue_delayed_work(bond->wq,
1023 &bond->mii_work, 0);
1027 out:
1028 return ret;
1030 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1031 bonding_show_miimon, bonding_store_miimon);
1034 * Show and set the primary slave. The store function is much
1035 * simpler than bonding_store_slaves function because it only needs to
1036 * handle one interface name.
1037 * The bond must be a mode that supports a primary for this be
1038 * set.
1040 static ssize_t bonding_show_primary(struct device *d,
1041 struct device_attribute *attr,
1042 char *buf)
1044 int count = 0;
1045 struct bonding *bond = to_bond(d);
1047 if (bond->primary_slave)
1048 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1050 return count;
1053 static ssize_t bonding_store_primary(struct device *d,
1054 struct device_attribute *attr,
1055 const char *buf, size_t count)
1057 int i;
1058 struct slave *slave;
1059 struct bonding *bond = to_bond(d);
1061 if (!rtnl_trylock())
1062 return restart_syscall();
1063 block_netpoll_tx();
1064 read_lock(&bond->lock);
1065 write_lock_bh(&bond->curr_slave_lock);
1067 if (!USES_PRIMARY(bond->params.mode)) {
1068 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1069 bond->dev->name, bond->dev->name, bond->params.mode);
1070 } else {
1071 bond_for_each_slave(bond, slave, i) {
1072 if (strnicmp
1073 (slave->dev->name, buf,
1074 strlen(slave->dev->name)) == 0) {
1075 pr_info("%s: Setting %s as primary slave.\n",
1076 bond->dev->name, slave->dev->name);
1077 bond->primary_slave = slave;
1078 strcpy(bond->params.primary, slave->dev->name);
1079 bond_select_active_slave(bond);
1080 goto out;
1084 /* if we got here, then we didn't match the name of any slave */
1086 if (strlen(buf) == 0 || buf[0] == '\n') {
1087 pr_info("%s: Setting primary slave to None.\n",
1088 bond->dev->name);
1089 bond->primary_slave = NULL;
1090 bond_select_active_slave(bond);
1091 } else {
1092 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1093 bond->dev->name, (int)strlen(buf) - 1, buf);
1096 out:
1097 write_unlock_bh(&bond->curr_slave_lock);
1098 read_unlock(&bond->lock);
1099 unblock_netpoll_tx();
1100 rtnl_unlock();
1102 return count;
1104 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1105 bonding_show_primary, bonding_store_primary);
1108 * Show and set the primary_reselect flag.
1110 static ssize_t bonding_show_primary_reselect(struct device *d,
1111 struct device_attribute *attr,
1112 char *buf)
1114 struct bonding *bond = to_bond(d);
1116 return sprintf(buf, "%s %d\n",
1117 pri_reselect_tbl[bond->params.primary_reselect].modename,
1118 bond->params.primary_reselect);
1121 static ssize_t bonding_store_primary_reselect(struct device *d,
1122 struct device_attribute *attr,
1123 const char *buf, size_t count)
1125 int new_value, ret = count;
1126 struct bonding *bond = to_bond(d);
1128 if (!rtnl_trylock())
1129 return restart_syscall();
1131 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1132 if (new_value < 0) {
1133 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1134 bond->dev->name,
1135 (int) strlen(buf) - 1, buf);
1136 ret = -EINVAL;
1137 goto out;
1140 bond->params.primary_reselect = new_value;
1141 pr_info("%s: setting primary_reselect to %s (%d).\n",
1142 bond->dev->name, pri_reselect_tbl[new_value].modename,
1143 new_value);
1145 block_netpoll_tx();
1146 read_lock(&bond->lock);
1147 write_lock_bh(&bond->curr_slave_lock);
1148 bond_select_active_slave(bond);
1149 write_unlock_bh(&bond->curr_slave_lock);
1150 read_unlock(&bond->lock);
1151 unblock_netpoll_tx();
1152 out:
1153 rtnl_unlock();
1154 return ret;
1156 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1157 bonding_show_primary_reselect,
1158 bonding_store_primary_reselect);
1161 * Show and set the use_carrier flag.
1163 static ssize_t bonding_show_carrier(struct device *d,
1164 struct device_attribute *attr,
1165 char *buf)
1167 struct bonding *bond = to_bond(d);
1169 return sprintf(buf, "%d\n", bond->params.use_carrier);
1172 static ssize_t bonding_store_carrier(struct device *d,
1173 struct device_attribute *attr,
1174 const char *buf, size_t count)
1176 int new_value, ret = count;
1177 struct bonding *bond = to_bond(d);
1180 if (sscanf(buf, "%d", &new_value) != 1) {
1181 pr_err("%s: no use_carrier value specified.\n",
1182 bond->dev->name);
1183 ret = -EINVAL;
1184 goto out;
1186 if ((new_value == 0) || (new_value == 1)) {
1187 bond->params.use_carrier = new_value;
1188 pr_info("%s: Setting use_carrier to %d.\n",
1189 bond->dev->name, new_value);
1190 } else {
1191 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1192 bond->dev->name, new_value);
1194 out:
1195 return count;
1197 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1198 bonding_show_carrier, bonding_store_carrier);
1202 * Show and set currently active_slave.
1204 static ssize_t bonding_show_active_slave(struct device *d,
1205 struct device_attribute *attr,
1206 char *buf)
1208 struct slave *curr;
1209 struct bonding *bond = to_bond(d);
1210 int count = 0;
1212 read_lock(&bond->curr_slave_lock);
1213 curr = bond->curr_active_slave;
1214 read_unlock(&bond->curr_slave_lock);
1216 if (USES_PRIMARY(bond->params.mode) && curr)
1217 count = sprintf(buf, "%s\n", curr->dev->name);
1218 return count;
1221 static ssize_t bonding_store_active_slave(struct device *d,
1222 struct device_attribute *attr,
1223 const char *buf, size_t count)
1225 int i;
1226 struct slave *slave;
1227 struct slave *old_active = NULL;
1228 struct slave *new_active = NULL;
1229 struct bonding *bond = to_bond(d);
1231 if (!rtnl_trylock())
1232 return restart_syscall();
1234 block_netpoll_tx();
1235 read_lock(&bond->lock);
1236 write_lock_bh(&bond->curr_slave_lock);
1238 if (!USES_PRIMARY(bond->params.mode))
1239 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1240 bond->dev->name, bond->dev->name, bond->params.mode);
1241 else {
1242 bond_for_each_slave(bond, slave, i) {
1243 if (strnicmp
1244 (slave->dev->name, buf,
1245 strlen(slave->dev->name)) == 0) {
1246 old_active = bond->curr_active_slave;
1247 new_active = slave;
1248 if (new_active == old_active) {
1249 /* do nothing */
1250 pr_info("%s: %s is already the current active slave.\n",
1251 bond->dev->name,
1252 slave->dev->name);
1253 goto out;
1255 else {
1256 if ((new_active) &&
1257 (old_active) &&
1258 (new_active->link == BOND_LINK_UP) &&
1259 IS_UP(new_active->dev)) {
1260 pr_info("%s: Setting %s as active slave.\n",
1261 bond->dev->name,
1262 slave->dev->name);
1263 bond_change_active_slave(bond, new_active);
1265 else {
1266 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1267 bond->dev->name,
1268 slave->dev->name,
1269 slave->dev->name);
1271 goto out;
1276 /* if we got here, then we didn't match the name of any slave */
1278 if (strlen(buf) == 0 || buf[0] == '\n') {
1279 pr_info("%s: Setting active slave to None.\n",
1280 bond->dev->name);
1281 bond->primary_slave = NULL;
1282 bond_select_active_slave(bond);
1283 } else {
1284 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
1285 bond->dev->name, (int)strlen(buf) - 1, buf);
1288 out:
1289 write_unlock_bh(&bond->curr_slave_lock);
1290 read_unlock(&bond->lock);
1291 unblock_netpoll_tx();
1293 rtnl_unlock();
1295 return count;
1298 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1299 bonding_show_active_slave, bonding_store_active_slave);
1303 * Show link status of the bond interface.
1305 static ssize_t bonding_show_mii_status(struct device *d,
1306 struct device_attribute *attr,
1307 char *buf)
1309 struct slave *curr;
1310 struct bonding *bond = to_bond(d);
1312 read_lock(&bond->curr_slave_lock);
1313 curr = bond->curr_active_slave;
1314 read_unlock(&bond->curr_slave_lock);
1316 return sprintf(buf, "%s\n", curr ? "up" : "down");
1318 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1322 * Show current 802.3ad aggregator ID.
1324 static ssize_t bonding_show_ad_aggregator(struct device *d,
1325 struct device_attribute *attr,
1326 char *buf)
1328 int count = 0;
1329 struct bonding *bond = to_bond(d);
1331 if (bond->params.mode == BOND_MODE_8023AD) {
1332 struct ad_info ad_info;
1333 count = sprintf(buf, "%d\n",
1334 (bond_3ad_get_active_agg_info(bond, &ad_info))
1335 ? 0 : ad_info.aggregator_id);
1338 return count;
1340 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1344 * Show number of active 802.3ad ports.
1346 static ssize_t bonding_show_ad_num_ports(struct device *d,
1347 struct device_attribute *attr,
1348 char *buf)
1350 int count = 0;
1351 struct bonding *bond = to_bond(d);
1353 if (bond->params.mode == BOND_MODE_8023AD) {
1354 struct ad_info ad_info;
1355 count = sprintf(buf, "%d\n",
1356 (bond_3ad_get_active_agg_info(bond, &ad_info))
1357 ? 0 : ad_info.ports);
1360 return count;
1362 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1366 * Show current 802.3ad actor key.
1368 static ssize_t bonding_show_ad_actor_key(struct device *d,
1369 struct device_attribute *attr,
1370 char *buf)
1372 int count = 0;
1373 struct bonding *bond = to_bond(d);
1375 if (bond->params.mode == BOND_MODE_8023AD) {
1376 struct ad_info ad_info;
1377 count = sprintf(buf, "%d\n",
1378 (bond_3ad_get_active_agg_info(bond, &ad_info))
1379 ? 0 : ad_info.actor_key);
1382 return count;
1384 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1388 * Show current 802.3ad partner key.
1390 static ssize_t bonding_show_ad_partner_key(struct device *d,
1391 struct device_attribute *attr,
1392 char *buf)
1394 int count = 0;
1395 struct bonding *bond = to_bond(d);
1397 if (bond->params.mode == BOND_MODE_8023AD) {
1398 struct ad_info ad_info;
1399 count = sprintf(buf, "%d\n",
1400 (bond_3ad_get_active_agg_info(bond, &ad_info))
1401 ? 0 : ad_info.partner_key);
1404 return count;
1406 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1410 * Show current 802.3ad partner mac.
1412 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1413 struct device_attribute *attr,
1414 char *buf)
1416 int count = 0;
1417 struct bonding *bond = to_bond(d);
1419 if (bond->params.mode == BOND_MODE_8023AD) {
1420 struct ad_info ad_info;
1421 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1422 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1425 return count;
1427 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1430 * Show the queue_ids of the slaves in the current bond.
1432 static ssize_t bonding_show_queue_id(struct device *d,
1433 struct device_attribute *attr,
1434 char *buf)
1436 struct slave *slave;
1437 int i, res = 0;
1438 struct bonding *bond = to_bond(d);
1440 if (!rtnl_trylock())
1441 return restart_syscall();
1443 read_lock(&bond->lock);
1444 bond_for_each_slave(bond, slave, i) {
1445 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1446 /* not enough space for another interface_name:queue_id pair */
1447 if ((PAGE_SIZE - res) > 10)
1448 res = PAGE_SIZE - 10;
1449 res += sprintf(buf + res, "++more++ ");
1450 break;
1452 res += sprintf(buf + res, "%s:%d ",
1453 slave->dev->name, slave->queue_id);
1455 read_unlock(&bond->lock);
1456 if (res)
1457 buf[res-1] = '\n'; /* eat the leftover space */
1458 rtnl_unlock();
1459 return res;
1463 * Set the queue_ids of the slaves in the current bond. The bond
1464 * interface must be enslaved for this to work.
1466 static ssize_t bonding_store_queue_id(struct device *d,
1467 struct device_attribute *attr,
1468 const char *buffer, size_t count)
1470 struct slave *slave, *update_slave;
1471 struct bonding *bond = to_bond(d);
1472 u16 qid;
1473 int i, ret = count;
1474 char *delim;
1475 struct net_device *sdev = NULL;
1477 if (!rtnl_trylock())
1478 return restart_syscall();
1480 /* delim will point to queue id if successful */
1481 delim = strchr(buffer, ':');
1482 if (!delim)
1483 goto err_no_cmd;
1486 * Terminate string that points to device name and bump it
1487 * up one, so we can read the queue id there.
1489 *delim = '\0';
1490 if (sscanf(++delim, "%hd\n", &qid) != 1)
1491 goto err_no_cmd;
1493 /* Check buffer length, valid ifname and queue id */
1494 if (strlen(buffer) > IFNAMSIZ ||
1495 !dev_valid_name(buffer) ||
1496 qid > bond->params.tx_queues)
1497 goto err_no_cmd;
1499 /* Get the pointer to that interface if it exists */
1500 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1501 if (!sdev)
1502 goto err_no_cmd;
1504 read_lock(&bond->lock);
1506 /* Search for thes slave and check for duplicate qids */
1507 update_slave = NULL;
1508 bond_for_each_slave(bond, slave, i) {
1509 if (sdev == slave->dev)
1511 * We don't need to check the matching
1512 * slave for dups, since we're overwriting it
1514 update_slave = slave;
1515 else if (qid && qid == slave->queue_id) {
1516 goto err_no_cmd_unlock;
1520 if (!update_slave)
1521 goto err_no_cmd_unlock;
1523 /* Actually set the qids for the slave */
1524 update_slave->queue_id = qid;
1526 read_unlock(&bond->lock);
1527 out:
1528 rtnl_unlock();
1529 return ret;
1531 err_no_cmd_unlock:
1532 read_unlock(&bond->lock);
1533 err_no_cmd:
1534 pr_info("invalid input for queue_id set for %s.\n",
1535 bond->dev->name);
1536 ret = -EPERM;
1537 goto out;
1540 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1541 bonding_store_queue_id);
1545 * Show and set the all_slaves_active flag.
1547 static ssize_t bonding_show_slaves_active(struct device *d,
1548 struct device_attribute *attr,
1549 char *buf)
1551 struct bonding *bond = to_bond(d);
1553 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1556 static ssize_t bonding_store_slaves_active(struct device *d,
1557 struct device_attribute *attr,
1558 const char *buf, size_t count)
1560 int i, new_value, ret = count;
1561 struct bonding *bond = to_bond(d);
1562 struct slave *slave;
1564 if (sscanf(buf, "%d", &new_value) != 1) {
1565 pr_err("%s: no all_slaves_active value specified.\n",
1566 bond->dev->name);
1567 ret = -EINVAL;
1568 goto out;
1571 if (new_value == bond->params.all_slaves_active)
1572 goto out;
1574 if ((new_value == 0) || (new_value == 1)) {
1575 bond->params.all_slaves_active = new_value;
1576 } else {
1577 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1578 bond->dev->name, new_value);
1579 ret = -EINVAL;
1580 goto out;
1583 bond_for_each_slave(bond, slave, i) {
1584 if (slave->state == BOND_STATE_BACKUP) {
1585 if (new_value)
1586 slave->dev->priv_flags &= ~IFF_SLAVE_INACTIVE;
1587 else
1588 slave->dev->priv_flags |= IFF_SLAVE_INACTIVE;
1591 out:
1592 return count;
1594 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1595 bonding_show_slaves_active, bonding_store_slaves_active);
1598 * Show and set the number of IGMP membership reports to send on link failure
1600 static ssize_t bonding_show_resend_igmp(struct device *d,
1601 struct device_attribute *attr,
1602 char *buf)
1604 struct bonding *bond = to_bond(d);
1606 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1609 static ssize_t bonding_store_resend_igmp(struct device *d,
1610 struct device_attribute *attr,
1611 const char *buf, size_t count)
1613 int new_value, ret = count;
1614 struct bonding *bond = to_bond(d);
1616 if (sscanf(buf, "%d", &new_value) != 1) {
1617 pr_err("%s: no resend_igmp value specified.\n",
1618 bond->dev->name);
1619 ret = -EINVAL;
1620 goto out;
1623 if (new_value < 0) {
1624 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1625 bond->dev->name, new_value);
1626 ret = -EINVAL;
1627 goto out;
1630 pr_info("%s: Setting resend_igmp to %d.\n",
1631 bond->dev->name, new_value);
1632 bond->params.resend_igmp = new_value;
1633 out:
1634 return ret;
1637 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1638 bonding_show_resend_igmp, bonding_store_resend_igmp);
1640 static struct attribute *per_bond_attrs[] = {
1641 &dev_attr_slaves.attr,
1642 &dev_attr_mode.attr,
1643 &dev_attr_fail_over_mac.attr,
1644 &dev_attr_arp_validate.attr,
1645 &dev_attr_arp_interval.attr,
1646 &dev_attr_arp_ip_target.attr,
1647 &dev_attr_downdelay.attr,
1648 &dev_attr_updelay.attr,
1649 &dev_attr_lacp_rate.attr,
1650 &dev_attr_ad_select.attr,
1651 &dev_attr_xmit_hash_policy.attr,
1652 &dev_attr_num_grat_arp.attr,
1653 &dev_attr_num_unsol_na.attr,
1654 &dev_attr_miimon.attr,
1655 &dev_attr_primary.attr,
1656 &dev_attr_primary_reselect.attr,
1657 &dev_attr_use_carrier.attr,
1658 &dev_attr_active_slave.attr,
1659 &dev_attr_mii_status.attr,
1660 &dev_attr_ad_aggregator.attr,
1661 &dev_attr_ad_num_ports.attr,
1662 &dev_attr_ad_actor_key.attr,
1663 &dev_attr_ad_partner_key.attr,
1664 &dev_attr_ad_partner_mac.attr,
1665 &dev_attr_queue_id.attr,
1666 &dev_attr_all_slaves_active.attr,
1667 &dev_attr_resend_igmp.attr,
1668 NULL,
1671 static struct attribute_group bonding_group = {
1672 .name = "bonding",
1673 .attrs = per_bond_attrs,
1677 * Initialize sysfs. This sets up the bonding_masters file in
1678 * /sys/class/net.
1680 int bond_create_sysfs(void)
1682 int ret;
1684 ret = netdev_class_create_file(&class_attr_bonding_masters);
1686 * Permit multiple loads of the module by ignoring failures to
1687 * create the bonding_masters sysfs file. Bonding devices
1688 * created by second or subsequent loads of the module will
1689 * not be listed in, or controllable by, bonding_masters, but
1690 * will have the usual "bonding" sysfs directory.
1692 * This is done to preserve backwards compatibility for
1693 * initscripts/sysconfig, which load bonding multiple times to
1694 * configure multiple bonding devices.
1696 if (ret == -EEXIST) {
1697 /* Is someone being kinky and naming a device bonding_master? */
1698 if (__dev_get_by_name(&init_net,
1699 class_attr_bonding_masters.attr.name))
1700 pr_err("network device named %s already exists in sysfs",
1701 class_attr_bonding_masters.attr.name);
1702 ret = 0;
1705 return ret;
1710 * Remove /sys/class/net/bonding_masters.
1712 void bond_destroy_sysfs(void)
1714 netdev_class_remove_file(&class_attr_bonding_masters);
1718 * Initialize sysfs for each bond. This sets up and registers
1719 * the 'bondctl' directory for each individual bond under /sys/class/net.
1721 void bond_prepare_sysfs_group(struct bonding *bond)
1723 bond->dev->sysfs_groups[0] = &bonding_group;