Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next
[linux-2.6.git] / drivers / net / bonding / bond_sysfs.c
blobbc8fd362a5aa7624fc01a6c79722f4b57e824aef
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/fs.h>
30 #include <linux/types.h>
31 #include <linux/string.h>
32 #include <linux/netdevice.h>
33 #include <linux/inetdevice.h>
34 #include <linux/in.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>
43 #include <linux/reciprocal_div.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 bond_net *bn =
59 container_of(attr, struct bond_net, class_attr_bonding_masters);
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 bond_net *bn, const char *ifname)
84 struct bonding *bond;
86 list_for_each_entry(bond, &bn->dev_list, bond_list) {
87 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
88 return bond->dev;
90 return NULL;
94 * "store" function for the bond_masters attribute. This is what
95 * creates and deletes entire bonds.
97 * The class parameter is ignored.
101 static ssize_t bonding_store_bonds(struct class *cls,
102 struct class_attribute *attr,
103 const char *buffer, size_t count)
105 struct bond_net *bn =
106 container_of(attr, struct bond_net, class_attr_bonding_masters);
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(bn->net, ifname);
120 if (rv) {
121 if (rv == -EEXIST)
122 pr_info("%s already exists.\n", ifname);
123 else
124 pr_info("%s creation failed.\n", ifname);
125 res = rv;
127 } else if (command[0] == '-') {
128 struct net_device *bond_dev;
130 rtnl_lock();
131 bond_dev = bond_get_by_name(bn, ifname);
132 if (bond_dev) {
133 pr_info("%s is being deleted...\n", ifname);
134 unregister_netdevice(bond_dev);
135 } else {
136 pr_err("unable to delete non-existent %s\n", ifname);
137 res = -ENODEV;
139 rtnl_unlock();
140 } else
141 goto err_no_cmd;
143 /* Always return either count or an error. If you return 0, you'll
144 * get called forever, which is bad.
146 return res;
148 err_no_cmd:
149 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
150 return -EPERM;
153 /* class attribute for bond_masters file. This ends up in /sys/class/net */
154 static const struct class_attribute class_attr_bonding_masters = {
155 .attr = {
156 .name = "bonding_masters",
157 .mode = S_IWUSR | S_IRUGO,
159 .show = bonding_show_bonds,
160 .store = bonding_store_bonds,
164 * Show the slaves in the current bond.
166 static ssize_t bonding_show_slaves(struct device *d,
167 struct device_attribute *attr, char *buf)
169 struct bonding *bond = to_bond(d);
170 struct list_head *iter;
171 struct slave *slave;
172 int res = 0;
174 if (!rtnl_trylock())
175 return restart_syscall();
177 bond_for_each_slave(bond, slave, iter) {
178 if (res > (PAGE_SIZE - IFNAMSIZ)) {
179 /* not enough space for another interface name */
180 if ((PAGE_SIZE - res) > 10)
181 res = PAGE_SIZE - 10;
182 res += sprintf(buf + res, "++more++ ");
183 break;
185 res += sprintf(buf + res, "%s ", slave->dev->name);
188 rtnl_unlock();
190 if (res)
191 buf[res-1] = '\n'; /* eat the leftover space */
193 return res;
197 * Set the slaves in the current bond.
198 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
199 * All hard work should be done there.
201 static ssize_t bonding_store_slaves(struct device *d,
202 struct device_attribute *attr,
203 const char *buffer, size_t count)
205 char command[IFNAMSIZ + 1] = { 0, };
206 char *ifname;
207 int res, ret = count;
208 struct net_device *dev;
209 struct bonding *bond = to_bond(d);
211 if (!rtnl_trylock())
212 return restart_syscall();
214 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
215 ifname = command + 1;
216 if ((strlen(command) <= 1) ||
217 !dev_valid_name(ifname))
218 goto err_no_cmd;
220 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
221 if (!dev) {
222 pr_info("%s: Interface %s does not exist!\n",
223 bond->dev->name, ifname);
224 ret = -ENODEV;
225 goto out;
228 switch (command[0]) {
229 case '+':
230 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
231 res = bond_enslave(bond->dev, dev);
232 break;
234 case '-':
235 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
236 res = bond_release(bond->dev, dev);
237 break;
239 default:
240 goto err_no_cmd;
243 if (res)
244 ret = res;
245 goto out;
247 err_no_cmd:
248 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
249 bond->dev->name);
250 ret = -EPERM;
252 out:
253 rtnl_unlock();
254 return ret;
257 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
258 bonding_store_slaves);
261 * Show and set the bonding mode. The bond interface must be down to
262 * change the mode.
264 static ssize_t bonding_show_mode(struct device *d,
265 struct device_attribute *attr, char *buf)
267 struct bonding *bond = to_bond(d);
269 return sprintf(buf, "%s %d\n",
270 bond_mode_tbl[bond->params.mode].modename,
271 bond->params.mode);
274 static ssize_t bonding_store_mode(struct device *d,
275 struct device_attribute *attr,
276 const char *buf, size_t count)
278 int new_value, ret;
279 struct bonding *bond = to_bond(d);
281 new_value = bond_parse_parm(buf, bond_mode_tbl);
282 if (new_value < 0) {
283 pr_err("%s: Ignoring invalid mode value %.*s.\n",
284 bond->dev->name, (int)strlen(buf) - 1, buf);
285 return -EINVAL;
287 if (!rtnl_trylock())
288 return restart_syscall();
290 ret = bond_option_mode_set(bond, new_value);
291 if (!ret) {
292 pr_info("%s: setting mode to %s (%d).\n",
293 bond->dev->name, bond_mode_tbl[new_value].modename,
294 new_value);
295 ret = count;
298 rtnl_unlock();
299 return ret;
301 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
302 bonding_show_mode, bonding_store_mode);
305 * Show and set the bonding transmit hash method.
307 static ssize_t bonding_show_xmit_hash(struct device *d,
308 struct device_attribute *attr,
309 char *buf)
311 struct bonding *bond = to_bond(d);
313 return sprintf(buf, "%s %d\n",
314 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
315 bond->params.xmit_policy);
318 static ssize_t bonding_store_xmit_hash(struct device *d,
319 struct device_attribute *attr,
320 const char *buf, size_t count)
322 int new_value, ret = count;
323 struct bonding *bond = to_bond(d);
325 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
326 if (new_value < 0) {
327 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
328 bond->dev->name,
329 (int)strlen(buf) - 1, buf);
330 ret = -EINVAL;
331 } else {
332 bond->params.xmit_policy = new_value;
333 pr_info("%s: setting xmit hash policy to %s (%d).\n",
334 bond->dev->name,
335 xmit_hashtype_tbl[new_value].modename, new_value);
338 return ret;
340 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
341 bonding_show_xmit_hash, bonding_store_xmit_hash);
344 * Show and set arp_validate.
346 static ssize_t bonding_show_arp_validate(struct device *d,
347 struct device_attribute *attr,
348 char *buf)
350 struct bonding *bond = to_bond(d);
352 return sprintf(buf, "%s %d\n",
353 arp_validate_tbl[bond->params.arp_validate].modename,
354 bond->params.arp_validate);
357 static ssize_t bonding_store_arp_validate(struct device *d,
358 struct device_attribute *attr,
359 const char *buf, size_t count)
361 struct bonding *bond = to_bond(d);
362 int new_value, ret = count;
364 if (!rtnl_trylock())
365 return restart_syscall();
366 new_value = bond_parse_parm(buf, arp_validate_tbl);
367 if (new_value < 0) {
368 pr_err("%s: Ignoring invalid arp_validate value %s\n",
369 bond->dev->name, buf);
370 ret = -EINVAL;
371 goto out;
373 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
374 pr_err("%s: arp_validate only supported in active-backup mode.\n",
375 bond->dev->name);
376 ret = -EINVAL;
377 goto out;
379 pr_info("%s: setting arp_validate to %s (%d).\n",
380 bond->dev->name, arp_validate_tbl[new_value].modename,
381 new_value);
383 if (bond->dev->flags & IFF_UP) {
384 if (!new_value)
385 bond->recv_probe = NULL;
386 else if (bond->params.arp_interval)
387 bond->recv_probe = bond_arp_rcv;
389 bond->params.arp_validate = new_value;
390 out:
391 rtnl_unlock();
393 return ret;
396 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
397 bonding_store_arp_validate);
399 * Show and set arp_all_targets.
401 static ssize_t bonding_show_arp_all_targets(struct device *d,
402 struct device_attribute *attr,
403 char *buf)
405 struct bonding *bond = to_bond(d);
406 int value = bond->params.arp_all_targets;
408 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
409 value);
412 static ssize_t bonding_store_arp_all_targets(struct device *d,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
416 struct bonding *bond = to_bond(d);
417 int new_value;
419 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
420 if (new_value < 0) {
421 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
422 bond->dev->name, buf);
423 return -EINVAL;
425 pr_info("%s: setting arp_all_targets to %s (%d).\n",
426 bond->dev->name, arp_all_targets_tbl[new_value].modename,
427 new_value);
429 bond->params.arp_all_targets = new_value;
431 return count;
434 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
435 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
438 * Show and store fail_over_mac. User only allowed to change the
439 * value when there are no slaves.
441 static ssize_t bonding_show_fail_over_mac(struct device *d,
442 struct device_attribute *attr,
443 char *buf)
445 struct bonding *bond = to_bond(d);
447 return sprintf(buf, "%s %d\n",
448 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
449 bond->params.fail_over_mac);
452 static ssize_t bonding_store_fail_over_mac(struct device *d,
453 struct device_attribute *attr,
454 const char *buf, size_t count)
456 int new_value, ret = count;
457 struct bonding *bond = to_bond(d);
459 if (!rtnl_trylock())
460 return restart_syscall();
462 if (bond_has_slaves(bond)) {
463 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
464 bond->dev->name);
465 ret = -EPERM;
466 goto out;
469 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
470 if (new_value < 0) {
471 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
472 bond->dev->name, buf);
473 ret = -EINVAL;
474 goto out;
477 bond->params.fail_over_mac = new_value;
478 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
479 bond->dev->name, fail_over_mac_tbl[new_value].modename,
480 new_value);
482 out:
483 rtnl_unlock();
484 return ret;
487 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
488 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
491 * Show and set the arp timer interval. There are two tricky bits
492 * here. First, if ARP monitoring is activated, then we must disable
493 * MII monitoring. Second, if the ARP timer isn't running, we must
494 * start it.
496 static ssize_t bonding_show_arp_interval(struct device *d,
497 struct device_attribute *attr,
498 char *buf)
500 struct bonding *bond = to_bond(d);
502 return sprintf(buf, "%d\n", bond->params.arp_interval);
505 static ssize_t bonding_store_arp_interval(struct device *d,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
509 struct bonding *bond = to_bond(d);
510 int new_value, ret = count;
512 if (!rtnl_trylock())
513 return restart_syscall();
514 if (sscanf(buf, "%d", &new_value) != 1) {
515 pr_err("%s: no arp_interval value specified.\n",
516 bond->dev->name);
517 ret = -EINVAL;
518 goto out;
520 if (new_value < 0) {
521 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
522 bond->dev->name, new_value, INT_MAX);
523 ret = -EINVAL;
524 goto out;
526 if (bond->params.mode == BOND_MODE_ALB ||
527 bond->params.mode == BOND_MODE_TLB) {
528 pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
529 bond->dev->name, bond->dev->name);
530 ret = -EINVAL;
531 goto out;
533 pr_info("%s: Setting ARP monitoring interval to %d.\n",
534 bond->dev->name, new_value);
535 bond->params.arp_interval = new_value;
536 if (new_value) {
537 if (bond->params.miimon) {
538 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
539 bond->dev->name, bond->dev->name);
540 bond->params.miimon = 0;
542 if (!bond->params.arp_targets[0])
543 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
544 bond->dev->name);
546 if (bond->dev->flags & IFF_UP) {
547 /* If the interface is up, we may need to fire off
548 * the ARP timer. If the interface is down, the
549 * timer will get fired off when the open function
550 * is called.
552 if (!new_value) {
553 if (bond->params.arp_validate)
554 bond->recv_probe = NULL;
555 cancel_delayed_work_sync(&bond->arp_work);
556 } else {
557 /* arp_validate can be set only in active-backup mode */
558 if (bond->params.arp_validate)
559 bond->recv_probe = bond_arp_rcv;
560 cancel_delayed_work_sync(&bond->mii_work);
561 queue_delayed_work(bond->wq, &bond->arp_work, 0);
564 out:
565 rtnl_unlock();
566 return ret;
568 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
569 bonding_show_arp_interval, bonding_store_arp_interval);
572 * Show and set the arp targets.
574 static ssize_t bonding_show_arp_targets(struct device *d,
575 struct device_attribute *attr,
576 char *buf)
578 int i, res = 0;
579 struct bonding *bond = to_bond(d);
581 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
582 if (bond->params.arp_targets[i])
583 res += sprintf(buf + res, "%pI4 ",
584 &bond->params.arp_targets[i]);
586 if (res)
587 buf[res-1] = '\n'; /* eat the leftover space */
588 return res;
591 static ssize_t bonding_store_arp_targets(struct device *d,
592 struct device_attribute *attr,
593 const char *buf, size_t count)
595 struct bonding *bond = to_bond(d);
596 struct list_head *iter;
597 struct slave *slave;
598 __be32 newtarget, *targets;
599 unsigned long *targets_rx;
600 int ind, i, j, ret = -EINVAL;
602 if (!rtnl_trylock())
603 return restart_syscall();
605 targets = bond->params.arp_targets;
606 newtarget = in_aton(buf + 1);
607 /* look for adds */
608 if (buf[0] == '+') {
609 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
610 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
611 bond->dev->name, &newtarget);
612 goto out;
615 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
616 pr_err("%s: ARP target %pI4 is already present\n",
617 bond->dev->name, &newtarget);
618 goto out;
621 ind = bond_get_targets_ip(targets, 0); /* first free slot */
622 if (ind == -1) {
623 pr_err("%s: ARP target table is full!\n",
624 bond->dev->name);
625 goto out;
628 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
629 &newtarget);
630 /* not to race with bond_arp_rcv */
631 write_lock_bh(&bond->lock);
632 bond_for_each_slave(bond, slave, iter)
633 slave->target_last_arp_rx[ind] = jiffies;
634 targets[ind] = newtarget;
635 write_unlock_bh(&bond->lock);
636 } else if (buf[0] == '-') {
637 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
638 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
639 bond->dev->name, &newtarget);
640 goto out;
643 ind = bond_get_targets_ip(targets, newtarget);
644 if (ind == -1) {
645 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
646 bond->dev->name, &newtarget);
647 goto out;
650 if (ind == 0 && !targets[1] && bond->params.arp_interval)
651 pr_warn("%s: removing last arp target with arp_interval on\n",
652 bond->dev->name);
654 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
655 &newtarget);
657 write_lock_bh(&bond->lock);
658 bond_for_each_slave(bond, slave, iter) {
659 targets_rx = slave->target_last_arp_rx;
660 j = ind;
661 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
662 targets_rx[j] = targets_rx[j+1];
663 targets_rx[j] = 0;
665 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
666 targets[i] = targets[i+1];
667 targets[i] = 0;
668 write_unlock_bh(&bond->lock);
669 } else {
670 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
671 bond->dev->name);
672 ret = -EPERM;
673 goto out;
676 ret = count;
677 out:
678 rtnl_unlock();
679 return ret;
681 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
684 * Show and set the up and down delays. These must be multiples of the
685 * MII monitoring value, and are stored internally as the multiplier.
686 * Thus, we must translate to MS for the real world.
688 static ssize_t bonding_show_downdelay(struct device *d,
689 struct device_attribute *attr,
690 char *buf)
692 struct bonding *bond = to_bond(d);
694 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
697 static ssize_t bonding_store_downdelay(struct device *d,
698 struct device_attribute *attr,
699 const char *buf, size_t count)
701 int new_value, ret = count;
702 struct bonding *bond = to_bond(d);
704 if (!(bond->params.miimon)) {
705 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
706 bond->dev->name);
707 ret = -EPERM;
708 goto out;
711 if (sscanf(buf, "%d", &new_value) != 1) {
712 pr_err("%s: no down delay value specified.\n", bond->dev->name);
713 ret = -EINVAL;
714 goto out;
716 if (new_value < 0) {
717 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
718 bond->dev->name, new_value, 0, INT_MAX);
719 ret = -EINVAL;
720 goto out;
721 } else {
722 if ((new_value % bond->params.miimon) != 0) {
723 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
724 bond->dev->name, new_value,
725 bond->params.miimon,
726 (new_value / bond->params.miimon) *
727 bond->params.miimon);
729 bond->params.downdelay = new_value / bond->params.miimon;
730 pr_info("%s: Setting down delay to %d.\n",
731 bond->dev->name,
732 bond->params.downdelay * bond->params.miimon);
736 out:
737 return ret;
739 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
740 bonding_show_downdelay, bonding_store_downdelay);
742 static ssize_t bonding_show_updelay(struct device *d,
743 struct device_attribute *attr,
744 char *buf)
746 struct bonding *bond = to_bond(d);
748 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
752 static ssize_t bonding_store_updelay(struct device *d,
753 struct device_attribute *attr,
754 const char *buf, size_t count)
756 int new_value, ret = count;
757 struct bonding *bond = to_bond(d);
759 if (!(bond->params.miimon)) {
760 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
761 bond->dev->name);
762 ret = -EPERM;
763 goto out;
766 if (sscanf(buf, "%d", &new_value) != 1) {
767 pr_err("%s: no up delay value specified.\n",
768 bond->dev->name);
769 ret = -EINVAL;
770 goto out;
772 if (new_value < 0) {
773 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
774 bond->dev->name, new_value, 0, INT_MAX);
775 ret = -EINVAL;
776 goto out;
777 } else {
778 if ((new_value % bond->params.miimon) != 0) {
779 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
780 bond->dev->name, new_value,
781 bond->params.miimon,
782 (new_value / bond->params.miimon) *
783 bond->params.miimon);
785 bond->params.updelay = new_value / bond->params.miimon;
786 pr_info("%s: Setting up delay to %d.\n",
787 bond->dev->name,
788 bond->params.updelay * bond->params.miimon);
791 out:
792 return ret;
794 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
795 bonding_show_updelay, bonding_store_updelay);
798 * Show and set the LACP interval. Interface must be down, and the mode
799 * must be set to 802.3ad mode.
801 static ssize_t bonding_show_lacp(struct device *d,
802 struct device_attribute *attr,
803 char *buf)
805 struct bonding *bond = to_bond(d);
807 return sprintf(buf, "%s %d\n",
808 bond_lacp_tbl[bond->params.lacp_fast].modename,
809 bond->params.lacp_fast);
812 static ssize_t bonding_store_lacp(struct device *d,
813 struct device_attribute *attr,
814 const char *buf, size_t count)
816 struct bonding *bond = to_bond(d);
817 int new_value, ret = count;
819 if (!rtnl_trylock())
820 return restart_syscall();
822 if (bond->dev->flags & IFF_UP) {
823 pr_err("%s: Unable to update LACP rate because interface is up.\n",
824 bond->dev->name);
825 ret = -EPERM;
826 goto out;
829 if (bond->params.mode != BOND_MODE_8023AD) {
830 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
831 bond->dev->name);
832 ret = -EPERM;
833 goto out;
836 new_value = bond_parse_parm(buf, bond_lacp_tbl);
838 if ((new_value == 1) || (new_value == 0)) {
839 bond->params.lacp_fast = new_value;
840 bond_3ad_update_lacp_rate(bond);
841 pr_info("%s: Setting LACP rate to %s (%d).\n",
842 bond->dev->name, bond_lacp_tbl[new_value].modename,
843 new_value);
844 } else {
845 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
846 bond->dev->name, (int)strlen(buf) - 1, buf);
847 ret = -EINVAL;
849 out:
850 rtnl_unlock();
852 return ret;
854 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
855 bonding_show_lacp, bonding_store_lacp);
857 static ssize_t bonding_show_min_links(struct device *d,
858 struct device_attribute *attr,
859 char *buf)
861 struct bonding *bond = to_bond(d);
863 return sprintf(buf, "%d\n", bond->params.min_links);
866 static ssize_t bonding_store_min_links(struct device *d,
867 struct device_attribute *attr,
868 const char *buf, size_t count)
870 struct bonding *bond = to_bond(d);
871 int ret;
872 unsigned int new_value;
874 ret = kstrtouint(buf, 0, &new_value);
875 if (ret < 0) {
876 pr_err("%s: Ignoring invalid min links value %s.\n",
877 bond->dev->name, buf);
878 return ret;
881 pr_info("%s: Setting min links value to %u\n",
882 bond->dev->name, new_value);
883 bond->params.min_links = new_value;
884 return count;
886 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
887 bonding_show_min_links, bonding_store_min_links);
889 static ssize_t bonding_show_ad_select(struct device *d,
890 struct device_attribute *attr,
891 char *buf)
893 struct bonding *bond = to_bond(d);
895 return sprintf(buf, "%s %d\n",
896 ad_select_tbl[bond->params.ad_select].modename,
897 bond->params.ad_select);
901 static ssize_t bonding_store_ad_select(struct device *d,
902 struct device_attribute *attr,
903 const char *buf, size_t count)
905 int new_value, ret = count;
906 struct bonding *bond = to_bond(d);
908 if (bond->dev->flags & IFF_UP) {
909 pr_err("%s: Unable to update ad_select because interface is up.\n",
910 bond->dev->name);
911 ret = -EPERM;
912 goto out;
915 new_value = bond_parse_parm(buf, ad_select_tbl);
917 if (new_value != -1) {
918 bond->params.ad_select = new_value;
919 pr_info("%s: Setting ad_select to %s (%d).\n",
920 bond->dev->name, ad_select_tbl[new_value].modename,
921 new_value);
922 } else {
923 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
924 bond->dev->name, (int)strlen(buf) - 1, buf);
925 ret = -EINVAL;
927 out:
928 return ret;
930 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
931 bonding_show_ad_select, bonding_store_ad_select);
934 * Show and set the number of peer notifications to send after a failover event.
936 static ssize_t bonding_show_num_peer_notif(struct device *d,
937 struct device_attribute *attr,
938 char *buf)
940 struct bonding *bond = to_bond(d);
941 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
944 static ssize_t bonding_store_num_peer_notif(struct device *d,
945 struct device_attribute *attr,
946 const char *buf, size_t count)
948 struct bonding *bond = to_bond(d);
949 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
950 return err ? err : count;
952 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
953 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
954 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
955 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
958 * Show and set the MII monitor interval. There are two tricky bits
959 * here. First, if MII monitoring is activated, then we must disable
960 * ARP monitoring. Second, if the timer isn't running, we must
961 * start it.
963 static ssize_t bonding_show_miimon(struct device *d,
964 struct device_attribute *attr,
965 char *buf)
967 struct bonding *bond = to_bond(d);
969 return sprintf(buf, "%d\n", bond->params.miimon);
972 static ssize_t bonding_store_miimon(struct device *d,
973 struct device_attribute *attr,
974 const char *buf, size_t count)
976 int new_value, ret = count;
977 struct bonding *bond = to_bond(d);
979 if (!rtnl_trylock())
980 return restart_syscall();
981 if (sscanf(buf, "%d", &new_value) != 1) {
982 pr_err("%s: no miimon value specified.\n",
983 bond->dev->name);
984 ret = -EINVAL;
985 goto out;
987 if (new_value < 0) {
988 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
989 bond->dev->name, new_value, 0, INT_MAX);
990 ret = -EINVAL;
991 goto out;
993 pr_info("%s: Setting MII monitoring interval to %d.\n",
994 bond->dev->name, new_value);
995 bond->params.miimon = new_value;
996 if (bond->params.updelay)
997 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
998 bond->dev->name,
999 bond->params.updelay * bond->params.miimon);
1000 if (bond->params.downdelay)
1001 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1002 bond->dev->name,
1003 bond->params.downdelay * bond->params.miimon);
1004 if (new_value && bond->params.arp_interval) {
1005 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1006 bond->dev->name);
1007 bond->params.arp_interval = 0;
1008 if (bond->params.arp_validate)
1009 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1011 if (bond->dev->flags & IFF_UP) {
1012 /* If the interface is up, we may need to fire off
1013 * the MII timer. If the interface is down, the
1014 * timer will get fired off when the open function
1015 * is called.
1017 if (!new_value) {
1018 cancel_delayed_work_sync(&bond->mii_work);
1019 } else {
1020 cancel_delayed_work_sync(&bond->arp_work);
1021 queue_delayed_work(bond->wq, &bond->mii_work, 0);
1024 out:
1025 rtnl_unlock();
1026 return ret;
1028 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1029 bonding_show_miimon, bonding_store_miimon);
1032 * Show and set the primary slave. The store function is much
1033 * simpler than bonding_store_slaves function because it only needs to
1034 * handle one interface name.
1035 * The bond must be a mode that supports a primary for this be
1036 * set.
1038 static ssize_t bonding_show_primary(struct device *d,
1039 struct device_attribute *attr,
1040 char *buf)
1042 int count = 0;
1043 struct bonding *bond = to_bond(d);
1045 if (bond->primary_slave)
1046 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1048 return count;
1051 static ssize_t bonding_store_primary(struct device *d,
1052 struct device_attribute *attr,
1053 const char *buf, size_t count)
1055 struct bonding *bond = to_bond(d);
1056 struct list_head *iter;
1057 char ifname[IFNAMSIZ];
1058 struct slave *slave;
1060 if (!rtnl_trylock())
1061 return restart_syscall();
1062 block_netpoll_tx();
1063 read_lock(&bond->lock);
1064 write_lock_bh(&bond->curr_slave_lock);
1066 if (!USES_PRIMARY(bond->params.mode)) {
1067 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1068 bond->dev->name, bond->dev->name, bond->params.mode);
1069 goto out;
1072 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1074 /* check to see if we are clearing primary */
1075 if (!strlen(ifname) || buf[0] == '\n') {
1076 pr_info("%s: Setting primary slave to None.\n",
1077 bond->dev->name);
1078 bond->primary_slave = NULL;
1079 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1080 bond_select_active_slave(bond);
1081 goto out;
1084 bond_for_each_slave(bond, slave, iter) {
1085 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1086 pr_info("%s: Setting %s as primary slave.\n",
1087 bond->dev->name, slave->dev->name);
1088 bond->primary_slave = slave;
1089 strcpy(bond->params.primary, slave->dev->name);
1090 bond_select_active_slave(bond);
1091 goto out;
1095 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1096 bond->params.primary[IFNAMSIZ - 1] = 0;
1098 pr_info("%s: Recording %s as primary, "
1099 "but it has not been enslaved to %s yet.\n",
1100 bond->dev->name, ifname, bond->dev->name);
1101 out:
1102 write_unlock_bh(&bond->curr_slave_lock);
1103 read_unlock(&bond->lock);
1104 unblock_netpoll_tx();
1105 rtnl_unlock();
1107 return count;
1109 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1110 bonding_show_primary, bonding_store_primary);
1113 * Show and set the primary_reselect flag.
1115 static ssize_t bonding_show_primary_reselect(struct device *d,
1116 struct device_attribute *attr,
1117 char *buf)
1119 struct bonding *bond = to_bond(d);
1121 return sprintf(buf, "%s %d\n",
1122 pri_reselect_tbl[bond->params.primary_reselect].modename,
1123 bond->params.primary_reselect);
1126 static ssize_t bonding_store_primary_reselect(struct device *d,
1127 struct device_attribute *attr,
1128 const char *buf, size_t count)
1130 int new_value, ret = count;
1131 struct bonding *bond = to_bond(d);
1133 if (!rtnl_trylock())
1134 return restart_syscall();
1136 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1137 if (new_value < 0) {
1138 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1139 bond->dev->name,
1140 (int) strlen(buf) - 1, buf);
1141 ret = -EINVAL;
1142 goto out;
1145 bond->params.primary_reselect = new_value;
1146 pr_info("%s: setting primary_reselect to %s (%d).\n",
1147 bond->dev->name, pri_reselect_tbl[new_value].modename,
1148 new_value);
1150 block_netpoll_tx();
1151 read_lock(&bond->lock);
1152 write_lock_bh(&bond->curr_slave_lock);
1153 bond_select_active_slave(bond);
1154 write_unlock_bh(&bond->curr_slave_lock);
1155 read_unlock(&bond->lock);
1156 unblock_netpoll_tx();
1157 out:
1158 rtnl_unlock();
1159 return ret;
1161 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1162 bonding_show_primary_reselect,
1163 bonding_store_primary_reselect);
1166 * Show and set the use_carrier flag.
1168 static ssize_t bonding_show_carrier(struct device *d,
1169 struct device_attribute *attr,
1170 char *buf)
1172 struct bonding *bond = to_bond(d);
1174 return sprintf(buf, "%d\n", bond->params.use_carrier);
1177 static ssize_t bonding_store_carrier(struct device *d,
1178 struct device_attribute *attr,
1179 const char *buf, size_t count)
1181 int new_value, ret = count;
1182 struct bonding *bond = to_bond(d);
1185 if (sscanf(buf, "%d", &new_value) != 1) {
1186 pr_err("%s: no use_carrier value specified.\n",
1187 bond->dev->name);
1188 ret = -EINVAL;
1189 goto out;
1191 if ((new_value == 0) || (new_value == 1)) {
1192 bond->params.use_carrier = new_value;
1193 pr_info("%s: Setting use_carrier to %d.\n",
1194 bond->dev->name, new_value);
1195 } else {
1196 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1197 bond->dev->name, new_value);
1199 out:
1200 return ret;
1202 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1203 bonding_show_carrier, bonding_store_carrier);
1207 * Show and set currently active_slave.
1209 static ssize_t bonding_show_active_slave(struct device *d,
1210 struct device_attribute *attr,
1211 char *buf)
1213 struct bonding *bond = to_bond(d);
1214 struct net_device *slave_dev;
1215 int count = 0;
1217 rcu_read_lock();
1218 slave_dev = bond_option_active_slave_get_rcu(bond);
1219 if (slave_dev)
1220 count = sprintf(buf, "%s\n", slave_dev->name);
1221 rcu_read_unlock();
1223 return count;
1226 static ssize_t bonding_store_active_slave(struct device *d,
1227 struct device_attribute *attr,
1228 const char *buf, size_t count)
1230 int ret;
1231 struct bonding *bond = to_bond(d);
1232 char ifname[IFNAMSIZ];
1233 struct net_device *dev;
1235 if (!rtnl_trylock())
1236 return restart_syscall();
1238 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1239 if (!strlen(ifname) || buf[0] == '\n') {
1240 dev = NULL;
1241 } else {
1242 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1243 if (!dev) {
1244 ret = -ENODEV;
1245 goto out;
1249 ret = bond_option_active_slave_set(bond, dev);
1250 if (!ret)
1251 ret = count;
1253 out:
1254 rtnl_unlock();
1256 return ret;
1259 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1260 bonding_show_active_slave, bonding_store_active_slave);
1264 * Show link status of the bond interface.
1266 static ssize_t bonding_show_mii_status(struct device *d,
1267 struct device_attribute *attr,
1268 char *buf)
1270 struct bonding *bond = to_bond(d);
1272 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1274 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1277 * Show current 802.3ad aggregator ID.
1279 static ssize_t bonding_show_ad_aggregator(struct device *d,
1280 struct device_attribute *attr,
1281 char *buf)
1283 int count = 0;
1284 struct bonding *bond = to_bond(d);
1286 if (bond->params.mode == BOND_MODE_8023AD) {
1287 struct ad_info ad_info;
1288 count = sprintf(buf, "%d\n",
1289 bond_3ad_get_active_agg_info(bond, &ad_info)
1290 ? 0 : ad_info.aggregator_id);
1293 return count;
1295 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1299 * Show number of active 802.3ad ports.
1301 static ssize_t bonding_show_ad_num_ports(struct device *d,
1302 struct device_attribute *attr,
1303 char *buf)
1305 int count = 0;
1306 struct bonding *bond = to_bond(d);
1308 if (bond->params.mode == BOND_MODE_8023AD) {
1309 struct ad_info ad_info;
1310 count = sprintf(buf, "%d\n",
1311 bond_3ad_get_active_agg_info(bond, &ad_info)
1312 ? 0 : ad_info.ports);
1315 return count;
1317 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1321 * Show current 802.3ad actor key.
1323 static ssize_t bonding_show_ad_actor_key(struct device *d,
1324 struct device_attribute *attr,
1325 char *buf)
1327 int count = 0;
1328 struct bonding *bond = to_bond(d);
1330 if (bond->params.mode == BOND_MODE_8023AD) {
1331 struct ad_info ad_info;
1332 count = sprintf(buf, "%d\n",
1333 bond_3ad_get_active_agg_info(bond, &ad_info)
1334 ? 0 : ad_info.actor_key);
1337 return count;
1339 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1343 * Show current 802.3ad partner key.
1345 static ssize_t bonding_show_ad_partner_key(struct device *d,
1346 struct device_attribute *attr,
1347 char *buf)
1349 int count = 0;
1350 struct bonding *bond = to_bond(d);
1352 if (bond->params.mode == BOND_MODE_8023AD) {
1353 struct ad_info ad_info;
1354 count = sprintf(buf, "%d\n",
1355 bond_3ad_get_active_agg_info(bond, &ad_info)
1356 ? 0 : ad_info.partner_key);
1359 return count;
1361 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1365 * Show current 802.3ad partner mac.
1367 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1368 struct device_attribute *attr,
1369 char *buf)
1371 int count = 0;
1372 struct bonding *bond = to_bond(d);
1374 if (bond->params.mode == BOND_MODE_8023AD) {
1375 struct ad_info ad_info;
1376 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1377 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1380 return count;
1382 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1385 * Show the queue_ids of the slaves in the current bond.
1387 static ssize_t bonding_show_queue_id(struct device *d,
1388 struct device_attribute *attr,
1389 char *buf)
1391 struct bonding *bond = to_bond(d);
1392 struct list_head *iter;
1393 struct slave *slave;
1394 int res = 0;
1396 if (!rtnl_trylock())
1397 return restart_syscall();
1399 bond_for_each_slave(bond, slave, iter) {
1400 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1401 /* not enough space for another interface_name:queue_id pair */
1402 if ((PAGE_SIZE - res) > 10)
1403 res = PAGE_SIZE - 10;
1404 res += sprintf(buf + res, "++more++ ");
1405 break;
1407 res += sprintf(buf + res, "%s:%d ",
1408 slave->dev->name, slave->queue_id);
1410 if (res)
1411 buf[res-1] = '\n'; /* eat the leftover space */
1413 rtnl_unlock();
1415 return res;
1419 * Set the queue_ids of the slaves in the current bond. The bond
1420 * interface must be enslaved for this to work.
1422 static ssize_t bonding_store_queue_id(struct device *d,
1423 struct device_attribute *attr,
1424 const char *buffer, size_t count)
1426 struct slave *slave, *update_slave;
1427 struct bonding *bond = to_bond(d);
1428 struct list_head *iter;
1429 u16 qid;
1430 int ret = count;
1431 char *delim;
1432 struct net_device *sdev = NULL;
1434 if (!rtnl_trylock())
1435 return restart_syscall();
1437 /* delim will point to queue id if successful */
1438 delim = strchr(buffer, ':');
1439 if (!delim)
1440 goto err_no_cmd;
1443 * Terminate string that points to device name and bump it
1444 * up one, so we can read the queue id there.
1446 *delim = '\0';
1447 if (sscanf(++delim, "%hd\n", &qid) != 1)
1448 goto err_no_cmd;
1450 /* Check buffer length, valid ifname and queue id */
1451 if (strlen(buffer) > IFNAMSIZ ||
1452 !dev_valid_name(buffer) ||
1453 qid > bond->dev->real_num_tx_queues)
1454 goto err_no_cmd;
1456 /* Get the pointer to that interface if it exists */
1457 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1458 if (!sdev)
1459 goto err_no_cmd;
1461 /* Search for thes slave and check for duplicate qids */
1462 update_slave = NULL;
1463 bond_for_each_slave(bond, slave, iter) {
1464 if (sdev == slave->dev)
1466 * We don't need to check the matching
1467 * slave for dups, since we're overwriting it
1469 update_slave = slave;
1470 else if (qid && qid == slave->queue_id) {
1471 goto err_no_cmd;
1475 if (!update_slave)
1476 goto err_no_cmd;
1478 /* Actually set the qids for the slave */
1479 update_slave->queue_id = qid;
1481 out:
1482 rtnl_unlock();
1483 return ret;
1485 err_no_cmd:
1486 pr_info("invalid input for queue_id set for %s.\n",
1487 bond->dev->name);
1488 ret = -EPERM;
1489 goto out;
1492 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1493 bonding_store_queue_id);
1497 * Show and set the all_slaves_active flag.
1499 static ssize_t bonding_show_slaves_active(struct device *d,
1500 struct device_attribute *attr,
1501 char *buf)
1503 struct bonding *bond = to_bond(d);
1505 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1508 static ssize_t bonding_store_slaves_active(struct device *d,
1509 struct device_attribute *attr,
1510 const char *buf, size_t count)
1512 struct bonding *bond = to_bond(d);
1513 int new_value, ret = count;
1514 struct list_head *iter;
1515 struct slave *slave;
1517 if (!rtnl_trylock())
1518 return restart_syscall();
1520 if (sscanf(buf, "%d", &new_value) != 1) {
1521 pr_err("%s: no all_slaves_active value specified.\n",
1522 bond->dev->name);
1523 ret = -EINVAL;
1524 goto out;
1527 if (new_value == bond->params.all_slaves_active)
1528 goto out;
1530 if ((new_value == 0) || (new_value == 1)) {
1531 bond->params.all_slaves_active = new_value;
1532 } else {
1533 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1534 bond->dev->name, new_value);
1535 ret = -EINVAL;
1536 goto out;
1539 bond_for_each_slave(bond, slave, iter) {
1540 if (!bond_is_active_slave(slave)) {
1541 if (new_value)
1542 slave->inactive = 0;
1543 else
1544 slave->inactive = 1;
1547 out:
1548 rtnl_unlock();
1549 return ret;
1551 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1552 bonding_show_slaves_active, bonding_store_slaves_active);
1555 * Show and set the number of IGMP membership reports to send on link failure
1557 static ssize_t bonding_show_resend_igmp(struct device *d,
1558 struct device_attribute *attr,
1559 char *buf)
1561 struct bonding *bond = to_bond(d);
1563 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1566 static ssize_t bonding_store_resend_igmp(struct device *d,
1567 struct device_attribute *attr,
1568 const char *buf, size_t count)
1570 int new_value, ret = count;
1571 struct bonding *bond = to_bond(d);
1573 if (sscanf(buf, "%d", &new_value) != 1) {
1574 pr_err("%s: no resend_igmp value specified.\n",
1575 bond->dev->name);
1576 ret = -EINVAL;
1577 goto out;
1580 if (new_value < 0 || new_value > 255) {
1581 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1582 bond->dev->name, new_value);
1583 ret = -EINVAL;
1584 goto out;
1587 pr_info("%s: Setting resend_igmp to %d.\n",
1588 bond->dev->name, new_value);
1589 bond->params.resend_igmp = new_value;
1590 out:
1591 return ret;
1594 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1595 bonding_show_resend_igmp, bonding_store_resend_igmp);
1598 static ssize_t bonding_show_lp_interval(struct device *d,
1599 struct device_attribute *attr,
1600 char *buf)
1602 struct bonding *bond = to_bond(d);
1603 return sprintf(buf, "%d\n", bond->params.lp_interval);
1606 static ssize_t bonding_store_lp_interval(struct device *d,
1607 struct device_attribute *attr,
1608 const char *buf, size_t count)
1610 struct bonding *bond = to_bond(d);
1611 int new_value, ret = count;
1613 if (sscanf(buf, "%d", &new_value) != 1) {
1614 pr_err("%s: no lp interval value specified.\n",
1615 bond->dev->name);
1616 ret = -EINVAL;
1617 goto out;
1620 if (new_value <= 0) {
1621 pr_err ("%s: lp_interval must be between 1 and %d\n",
1622 bond->dev->name, INT_MAX);
1623 ret = -EINVAL;
1624 goto out;
1627 bond->params.lp_interval = new_value;
1628 out:
1629 return ret;
1632 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1633 bonding_show_lp_interval, bonding_store_lp_interval);
1635 static ssize_t bonding_show_packets_per_slave(struct device *d,
1636 struct device_attribute *attr,
1637 char *buf)
1639 struct bonding *bond = to_bond(d);
1640 int packets_per_slave = bond->params.packets_per_slave;
1642 if (packets_per_slave > 1)
1643 packets_per_slave = reciprocal_value(packets_per_slave);
1645 return sprintf(buf, "%d\n", packets_per_slave);
1648 static ssize_t bonding_store_packets_per_slave(struct device *d,
1649 struct device_attribute *attr,
1650 const char *buf, size_t count)
1652 struct bonding *bond = to_bond(d);
1653 int new_value, ret = count;
1655 if (sscanf(buf, "%d", &new_value) != 1) {
1656 pr_err("%s: no packets_per_slave value specified.\n",
1657 bond->dev->name);
1658 ret = -EINVAL;
1659 goto out;
1661 if (new_value < 0 || new_value > USHRT_MAX) {
1662 pr_err("%s: packets_per_slave must be between 0 and %u\n",
1663 bond->dev->name, USHRT_MAX);
1664 ret = -EINVAL;
1665 goto out;
1667 if (bond->params.mode != BOND_MODE_ROUNDROBIN)
1668 pr_warn("%s: Warning: packets_per_slave has effect only in balance-rr mode\n",
1669 bond->dev->name);
1670 if (new_value > 1)
1671 bond->params.packets_per_slave = reciprocal_value(new_value);
1672 else
1673 bond->params.packets_per_slave = new_value;
1674 out:
1675 return ret;
1678 static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1679 bonding_show_packets_per_slave,
1680 bonding_store_packets_per_slave);
1682 static struct attribute *per_bond_attrs[] = {
1683 &dev_attr_slaves.attr,
1684 &dev_attr_mode.attr,
1685 &dev_attr_fail_over_mac.attr,
1686 &dev_attr_arp_validate.attr,
1687 &dev_attr_arp_all_targets.attr,
1688 &dev_attr_arp_interval.attr,
1689 &dev_attr_arp_ip_target.attr,
1690 &dev_attr_downdelay.attr,
1691 &dev_attr_updelay.attr,
1692 &dev_attr_lacp_rate.attr,
1693 &dev_attr_ad_select.attr,
1694 &dev_attr_xmit_hash_policy.attr,
1695 &dev_attr_num_grat_arp.attr,
1696 &dev_attr_num_unsol_na.attr,
1697 &dev_attr_miimon.attr,
1698 &dev_attr_primary.attr,
1699 &dev_attr_primary_reselect.attr,
1700 &dev_attr_use_carrier.attr,
1701 &dev_attr_active_slave.attr,
1702 &dev_attr_mii_status.attr,
1703 &dev_attr_ad_aggregator.attr,
1704 &dev_attr_ad_num_ports.attr,
1705 &dev_attr_ad_actor_key.attr,
1706 &dev_attr_ad_partner_key.attr,
1707 &dev_attr_ad_partner_mac.attr,
1708 &dev_attr_queue_id.attr,
1709 &dev_attr_all_slaves_active.attr,
1710 &dev_attr_resend_igmp.attr,
1711 &dev_attr_min_links.attr,
1712 &dev_attr_lp_interval.attr,
1713 &dev_attr_packets_per_slave.attr,
1714 NULL,
1717 static struct attribute_group bonding_group = {
1718 .name = "bonding",
1719 .attrs = per_bond_attrs,
1723 * Initialize sysfs. This sets up the bonding_masters file in
1724 * /sys/class/net.
1726 int bond_create_sysfs(struct bond_net *bn)
1728 int ret;
1730 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1731 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1733 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1734 bn->net);
1736 * Permit multiple loads of the module by ignoring failures to
1737 * create the bonding_masters sysfs file. Bonding devices
1738 * created by second or subsequent loads of the module will
1739 * not be listed in, or controllable by, bonding_masters, but
1740 * will have the usual "bonding" sysfs directory.
1742 * This is done to preserve backwards compatibility for
1743 * initscripts/sysconfig, which load bonding multiple times to
1744 * configure multiple bonding devices.
1746 if (ret == -EEXIST) {
1747 /* Is someone being kinky and naming a device bonding_master? */
1748 if (__dev_get_by_name(bn->net,
1749 class_attr_bonding_masters.attr.name))
1750 pr_err("network device named %s already exists in sysfs",
1751 class_attr_bonding_masters.attr.name);
1752 ret = 0;
1755 return ret;
1760 * Remove /sys/class/net/bonding_masters.
1762 void bond_destroy_sysfs(struct bond_net *bn)
1764 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
1768 * Initialize sysfs for each bond. This sets up and registers
1769 * the 'bondctl' directory for each individual bond under /sys/class/net.
1771 void bond_prepare_sysfs_group(struct bonding *bond)
1773 bond->dev->sysfs_groups[0] = &bonding_group;