mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / bonding / bond_sysfs.c
blobb60f95b2196e3c77dec12ce3f9c089c0487ad69a
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>
44 #include "bonding.h"
46 #define to_dev(obj) container_of(obj, struct device, kobj)
47 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
53 static ssize_t bonding_show_bonds(struct class *cls,
54 struct class_attribute *attr,
55 char *buf)
57 struct bond_net *bn =
58 container_of(attr, struct bond_net, class_attr_bonding_masters);
59 int res = 0;
60 struct bonding *bond;
62 rtnl_lock();
64 list_for_each_entry(bond, &bn->dev_list, bond_list) {
65 if (res > (PAGE_SIZE - IFNAMSIZ)) {
66 /* not enough space for another interface name */
67 if ((PAGE_SIZE - res) > 10)
68 res = PAGE_SIZE - 10;
69 res += sprintf(buf + res, "++more++ ");
70 break;
72 res += sprintf(buf + res, "%s ", bond->dev->name);
74 if (res)
75 buf[res-1] = '\n'; /* eat the leftover space */
77 rtnl_unlock();
78 return res;
81 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
83 struct bonding *bond;
85 list_for_each_entry(bond, &bn->dev_list, bond_list) {
86 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
87 return bond->dev;
89 return NULL;
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
96 * The class parameter is ignored.
100 static ssize_t bonding_store_bonds(struct class *cls,
101 struct class_attribute *attr,
102 const char *buffer, size_t count)
104 struct bond_net *bn =
105 container_of(attr, struct bond_net, class_attr_bonding_masters);
106 char command[IFNAMSIZ + 1] = {0, };
107 char *ifname;
108 int rv, res = count;
110 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
111 ifname = command + 1;
112 if ((strlen(command) <= 1) ||
113 !dev_valid_name(ifname))
114 goto err_no_cmd;
116 if (command[0] == '+') {
117 pr_info("%s is being created...\n", ifname);
118 rv = bond_create(bn->net, ifname);
119 if (rv) {
120 if (rv == -EEXIST)
121 pr_info("%s already exists.\n", ifname);
122 else
123 pr_info("%s creation failed.\n", ifname);
124 res = rv;
126 } else if (command[0] == '-') {
127 struct net_device *bond_dev;
129 rtnl_lock();
130 bond_dev = bond_get_by_name(bn, ifname);
131 if (bond_dev) {
132 pr_info("%s is being deleted...\n", ifname);
133 unregister_netdevice(bond_dev);
134 } else {
135 pr_err("unable to delete non-existent %s\n", ifname);
136 res = -ENODEV;
138 rtnl_unlock();
139 } else
140 goto err_no_cmd;
142 /* Always return either count or an error. If you return 0, you'll
143 * get called forever, which is bad.
145 return res;
147 err_no_cmd:
148 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
149 return -EPERM;
152 static const void *bonding_namespace(struct class *cls,
153 const struct class_attribute *attr)
155 const struct bond_net *bn =
156 container_of(attr, struct bond_net, class_attr_bonding_masters);
157 return bn->net;
160 /* class attribute for bond_masters file. This ends up in /sys/class/net */
161 static const struct class_attribute class_attr_bonding_masters = {
162 .attr = {
163 .name = "bonding_masters",
164 .mode = S_IWUSR | S_IRUGO,
166 .show = bonding_show_bonds,
167 .store = bonding_store_bonds,
168 .namespace = bonding_namespace,
171 int bond_create_slave_symlinks(struct net_device *master,
172 struct net_device *slave)
174 char linkname[IFNAMSIZ+7];
175 int ret = 0;
177 /* first, create a link from the slave back to the master */
178 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
179 "master");
180 if (ret)
181 return ret;
182 /* next, create a link from the master to the slave */
183 sprintf(linkname, "slave_%s", slave->name);
184 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
185 linkname);
187 /* free the master link created earlier in case of error */
188 if (ret)
189 sysfs_remove_link(&(slave->dev.kobj), "master");
191 return ret;
195 void bond_destroy_slave_symlinks(struct net_device *master,
196 struct net_device *slave)
198 char linkname[IFNAMSIZ+7];
200 sysfs_remove_link(&(slave->dev.kobj), "master");
201 sprintf(linkname, "slave_%s", slave->name);
202 sysfs_remove_link(&(master->dev.kobj), linkname);
207 * Show the slaves in the current bond.
209 static ssize_t bonding_show_slaves(struct device *d,
210 struct device_attribute *attr, char *buf)
212 struct bonding *bond = to_bond(d);
213 struct slave *slave;
214 int res = 0;
216 read_lock(&bond->lock);
217 bond_for_each_slave(bond, slave) {
218 if (res > (PAGE_SIZE - IFNAMSIZ)) {
219 /* not enough space for another interface name */
220 if ((PAGE_SIZE - res) > 10)
221 res = PAGE_SIZE - 10;
222 res += sprintf(buf + res, "++more++ ");
223 break;
225 res += sprintf(buf + res, "%s ", slave->dev->name);
227 read_unlock(&bond->lock);
228 if (res)
229 buf[res-1] = '\n'; /* eat the leftover space */
231 return res;
235 * Set the slaves in the current bond.
236 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
237 * All hard work should be done there.
239 static ssize_t bonding_store_slaves(struct device *d,
240 struct device_attribute *attr,
241 const char *buffer, size_t count)
243 char command[IFNAMSIZ + 1] = { 0, };
244 char *ifname;
245 int res, ret = count;
246 struct net_device *dev;
247 struct bonding *bond = to_bond(d);
249 if (!rtnl_trylock())
250 return restart_syscall();
252 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
253 ifname = command + 1;
254 if ((strlen(command) <= 1) ||
255 !dev_valid_name(ifname))
256 goto err_no_cmd;
258 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
259 if (!dev) {
260 pr_info("%s: Interface %s does not exist!\n",
261 bond->dev->name, ifname);
262 ret = -ENODEV;
263 goto out;
266 switch (command[0]) {
267 case '+':
268 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
269 res = bond_enslave(bond->dev, dev);
270 break;
272 case '-':
273 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
274 res = bond_release(bond->dev, dev);
275 break;
277 default:
278 goto err_no_cmd;
281 if (res)
282 ret = res;
283 goto out;
285 err_no_cmd:
286 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
287 bond->dev->name);
288 ret = -EPERM;
290 out:
291 rtnl_unlock();
292 return ret;
295 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
296 bonding_store_slaves);
299 * Show and set the bonding mode. The bond interface must be down to
300 * change the mode.
302 static ssize_t bonding_show_mode(struct device *d,
303 struct device_attribute *attr, char *buf)
305 struct bonding *bond = to_bond(d);
307 return sprintf(buf, "%s %d\n",
308 bond_mode_tbl[bond->params.mode].modename,
309 bond->params.mode);
312 static ssize_t bonding_store_mode(struct device *d,
313 struct device_attribute *attr,
314 const char *buf, size_t count)
316 int new_value, ret = count;
317 struct bonding *bond = to_bond(d);
319 if (!rtnl_trylock())
320 return restart_syscall();
322 if (bond->dev->flags & IFF_UP) {
323 pr_err("unable to update mode of %s because interface is up.\n",
324 bond->dev->name);
325 ret = -EPERM;
326 goto out;
329 if (!list_empty(&bond->slave_list)) {
330 pr_err("unable to update mode of %s because it has slaves.\n",
331 bond->dev->name);
332 ret = -EPERM;
333 goto out;
336 new_value = bond_parse_parm(buf, bond_mode_tbl);
337 if (new_value < 0) {
338 pr_err("%s: Ignoring invalid mode value %.*s.\n",
339 bond->dev->name, (int)strlen(buf) - 1, buf);
340 ret = -EINVAL;
341 goto out;
343 if ((new_value == BOND_MODE_ALB ||
344 new_value == BOND_MODE_TLB) &&
345 bond->params.arp_interval) {
346 pr_err("%s: %s mode is incompatible with arp monitoring.\n",
347 bond->dev->name, bond_mode_tbl[new_value].modename);
348 ret = -EINVAL;
349 goto out;
352 /* don't cache arp_validate between modes */
353 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
354 bond->params.mode = new_value;
355 bond_set_mode_ops(bond, bond->params.mode);
356 pr_info("%s: setting mode to %s (%d).\n",
357 bond->dev->name, bond_mode_tbl[new_value].modename,
358 new_value);
359 out:
360 rtnl_unlock();
361 return ret;
363 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
364 bonding_show_mode, bonding_store_mode);
367 * Show and set the bonding transmit hash method.
369 static ssize_t bonding_show_xmit_hash(struct device *d,
370 struct device_attribute *attr,
371 char *buf)
373 struct bonding *bond = to_bond(d);
375 return sprintf(buf, "%s %d\n",
376 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
377 bond->params.xmit_policy);
380 static ssize_t bonding_store_xmit_hash(struct device *d,
381 struct device_attribute *attr,
382 const char *buf, size_t count)
384 int new_value, ret = count;
385 struct bonding *bond = to_bond(d);
387 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
388 if (new_value < 0) {
389 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
390 bond->dev->name,
391 (int)strlen(buf) - 1, buf);
392 ret = -EINVAL;
393 } else {
394 bond->params.xmit_policy = new_value;
395 bond_set_mode_ops(bond, bond->params.mode);
396 pr_info("%s: setting xmit hash policy to %s (%d).\n",
397 bond->dev->name,
398 xmit_hashtype_tbl[new_value].modename, new_value);
401 return ret;
403 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
404 bonding_show_xmit_hash, bonding_store_xmit_hash);
407 * Show and set arp_validate.
409 static ssize_t bonding_show_arp_validate(struct device *d,
410 struct device_attribute *attr,
411 char *buf)
413 struct bonding *bond = to_bond(d);
415 return sprintf(buf, "%s %d\n",
416 arp_validate_tbl[bond->params.arp_validate].modename,
417 bond->params.arp_validate);
420 static ssize_t bonding_store_arp_validate(struct device *d,
421 struct device_attribute *attr,
422 const char *buf, size_t count)
424 struct bonding *bond = to_bond(d);
425 int new_value, ret = count;
427 if (!rtnl_trylock())
428 return restart_syscall();
429 new_value = bond_parse_parm(buf, arp_validate_tbl);
430 if (new_value < 0) {
431 pr_err("%s: Ignoring invalid arp_validate value %s\n",
432 bond->dev->name, buf);
433 ret = -EINVAL;
434 goto out;
436 if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
437 pr_err("%s: arp_validate only supported in active-backup mode.\n",
438 bond->dev->name);
439 ret = -EINVAL;
440 goto out;
442 pr_info("%s: setting arp_validate to %s (%d).\n",
443 bond->dev->name, arp_validate_tbl[new_value].modename,
444 new_value);
446 if (bond->dev->flags & IFF_UP) {
447 if (!new_value)
448 bond->recv_probe = NULL;
449 else if (bond->params.arp_interval)
450 bond->recv_probe = bond_arp_rcv;
452 bond->params.arp_validate = new_value;
453 out:
454 rtnl_unlock();
456 return ret;
459 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
460 bonding_store_arp_validate);
462 * Show and set arp_all_targets.
464 static ssize_t bonding_show_arp_all_targets(struct device *d,
465 struct device_attribute *attr,
466 char *buf)
468 struct bonding *bond = to_bond(d);
469 int value = bond->params.arp_all_targets;
471 return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
472 value);
475 static ssize_t bonding_store_arp_all_targets(struct device *d,
476 struct device_attribute *attr,
477 const char *buf, size_t count)
479 struct bonding *bond = to_bond(d);
480 int new_value;
482 new_value = bond_parse_parm(buf, arp_all_targets_tbl);
483 if (new_value < 0) {
484 pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
485 bond->dev->name, buf);
486 return -EINVAL;
488 pr_info("%s: setting arp_all_targets to %s (%d).\n",
489 bond->dev->name, arp_all_targets_tbl[new_value].modename,
490 new_value);
492 bond->params.arp_all_targets = new_value;
494 return count;
497 static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
498 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
501 * Show and store fail_over_mac. User only allowed to change the
502 * value when there are no slaves.
504 static ssize_t bonding_show_fail_over_mac(struct device *d,
505 struct device_attribute *attr,
506 char *buf)
508 struct bonding *bond = to_bond(d);
510 return sprintf(buf, "%s %d\n",
511 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
512 bond->params.fail_over_mac);
515 static ssize_t bonding_store_fail_over_mac(struct device *d,
516 struct device_attribute *attr,
517 const char *buf, size_t count)
519 int new_value, ret = count;
520 struct bonding *bond = to_bond(d);
522 if (!rtnl_trylock())
523 return restart_syscall();
525 if (!list_empty(&bond->slave_list)) {
526 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
527 bond->dev->name);
528 ret = -EPERM;
529 goto out;
532 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
533 if (new_value < 0) {
534 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
535 bond->dev->name, buf);
536 ret = -EINVAL;
537 goto out;
540 bond->params.fail_over_mac = new_value;
541 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
542 bond->dev->name, fail_over_mac_tbl[new_value].modename,
543 new_value);
545 out:
546 rtnl_unlock();
547 return ret;
550 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
551 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
554 * Show and set the arp timer interval. There are two tricky bits
555 * here. First, if ARP monitoring is activated, then we must disable
556 * MII monitoring. Second, if the ARP timer isn't running, we must
557 * start it.
559 static ssize_t bonding_show_arp_interval(struct device *d,
560 struct device_attribute *attr,
561 char *buf)
563 struct bonding *bond = to_bond(d);
565 return sprintf(buf, "%d\n", bond->params.arp_interval);
568 static ssize_t bonding_store_arp_interval(struct device *d,
569 struct device_attribute *attr,
570 const char *buf, size_t count)
572 struct bonding *bond = to_bond(d);
573 int new_value, ret = count;
575 if (!rtnl_trylock())
576 return restart_syscall();
577 if (sscanf(buf, "%d", &new_value) != 1) {
578 pr_err("%s: no arp_interval value specified.\n",
579 bond->dev->name);
580 ret = -EINVAL;
581 goto out;
583 if (new_value < 0) {
584 pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
585 bond->dev->name, new_value, INT_MAX);
586 ret = -EINVAL;
587 goto out;
589 if (bond->params.mode == BOND_MODE_ALB ||
590 bond->params.mode == BOND_MODE_TLB ||
591 bond->params.mode == BOND_MODE_8023AD) {
592 pr_info("%s: ARP monitoring cannot be used with ALB/TLB/802.3ad. Only MII monitoring is supported on %s.\n",
593 bond->dev->name, bond->dev->name);
594 ret = -EINVAL;
595 goto out;
597 pr_info("%s: Setting ARP monitoring interval to %d.\n",
598 bond->dev->name, new_value);
599 bond->params.arp_interval = new_value;
600 if (new_value) {
601 if (bond->params.miimon) {
602 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
603 bond->dev->name, bond->dev->name);
604 bond->params.miimon = 0;
606 if (!bond->params.arp_targets[0])
607 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
608 bond->dev->name);
610 if (bond->dev->flags & IFF_UP) {
611 /* If the interface is up, we may need to fire off
612 * the ARP timer. If the interface is down, the
613 * timer will get fired off when the open function
614 * is called.
616 if (!new_value) {
617 if (bond->params.arp_validate)
618 bond->recv_probe = NULL;
619 cancel_delayed_work_sync(&bond->arp_work);
620 } else {
621 /* arp_validate can be set only in active-backup mode */
622 if (bond->params.arp_validate)
623 bond->recv_probe = bond_arp_rcv;
624 cancel_delayed_work_sync(&bond->mii_work);
625 queue_delayed_work(bond->wq, &bond->arp_work, 0);
628 out:
629 rtnl_unlock();
630 return ret;
632 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
633 bonding_show_arp_interval, bonding_store_arp_interval);
636 * Show and set the arp targets.
638 static ssize_t bonding_show_arp_targets(struct device *d,
639 struct device_attribute *attr,
640 char *buf)
642 int i, res = 0;
643 struct bonding *bond = to_bond(d);
645 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
646 if (bond->params.arp_targets[i])
647 res += sprintf(buf + res, "%pI4 ",
648 &bond->params.arp_targets[i]);
650 if (res)
651 buf[res-1] = '\n'; /* eat the leftover space */
652 return res;
655 static ssize_t bonding_store_arp_targets(struct device *d,
656 struct device_attribute *attr,
657 const char *buf, size_t count)
659 struct bonding *bond = to_bond(d);
660 struct slave *slave;
661 __be32 newtarget, *targets;
662 unsigned long *targets_rx;
663 int ind, i, j, ret = -EINVAL;
665 targets = bond->params.arp_targets;
666 newtarget = in_aton(buf + 1);
667 /* look for adds */
668 if (buf[0] == '+') {
669 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
670 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
671 bond->dev->name, &newtarget);
672 goto out;
675 if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
676 pr_err("%s: ARP target %pI4 is already present\n",
677 bond->dev->name, &newtarget);
678 goto out;
681 ind = bond_get_targets_ip(targets, 0); /* first free slot */
682 if (ind == -1) {
683 pr_err("%s: ARP target table is full!\n",
684 bond->dev->name);
685 goto out;
688 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
689 &newtarget);
690 /* not to race with bond_arp_rcv */
691 write_lock_bh(&bond->lock);
692 bond_for_each_slave(bond, slave)
693 slave->target_last_arp_rx[ind] = jiffies;
694 targets[ind] = newtarget;
695 write_unlock_bh(&bond->lock);
696 } else if (buf[0] == '-') {
697 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
698 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
699 bond->dev->name, &newtarget);
700 goto out;
703 ind = bond_get_targets_ip(targets, newtarget);
704 if (ind == -1) {
705 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
706 bond->dev->name, &newtarget);
707 goto out;
710 if (ind == 0 && !targets[1] && bond->params.arp_interval)
711 pr_warn("%s: removing last arp target with arp_interval on\n",
712 bond->dev->name);
714 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
715 &newtarget);
717 write_lock_bh(&bond->lock);
718 bond_for_each_slave(bond, slave) {
719 targets_rx = slave->target_last_arp_rx;
720 j = ind;
721 for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
722 targets_rx[j] = targets_rx[j+1];
723 targets_rx[j] = 0;
725 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
726 targets[i] = targets[i+1];
727 targets[i] = 0;
728 write_unlock_bh(&bond->lock);
729 } else {
730 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
731 bond->dev->name);
732 ret = -EPERM;
733 goto out;
736 ret = count;
737 out:
738 return ret;
740 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
743 * Show and set the up and down delays. These must be multiples of the
744 * MII monitoring value, and are stored internally as the multiplier.
745 * Thus, we must translate to MS for the real world.
747 static ssize_t bonding_show_downdelay(struct device *d,
748 struct device_attribute *attr,
749 char *buf)
751 struct bonding *bond = to_bond(d);
753 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
756 static ssize_t bonding_store_downdelay(struct device *d,
757 struct device_attribute *attr,
758 const char *buf, size_t count)
760 int new_value, ret = count;
761 struct bonding *bond = to_bond(d);
763 if (!rtnl_trylock())
764 return restart_syscall();
765 if (!(bond->params.miimon)) {
766 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
767 bond->dev->name);
768 ret = -EPERM;
769 goto out;
772 if (sscanf(buf, "%d", &new_value) != 1) {
773 pr_err("%s: no down delay value specified.\n", bond->dev->name);
774 ret = -EINVAL;
775 goto out;
777 if (new_value < 0) {
778 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
779 bond->dev->name, new_value, 0, INT_MAX);
780 ret = -EINVAL;
781 goto out;
782 } else {
783 if ((new_value % bond->params.miimon) != 0) {
784 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
785 bond->dev->name, new_value,
786 bond->params.miimon,
787 (new_value / bond->params.miimon) *
788 bond->params.miimon);
790 bond->params.downdelay = new_value / bond->params.miimon;
791 pr_info("%s: Setting down delay to %d.\n",
792 bond->dev->name,
793 bond->params.downdelay * bond->params.miimon);
797 out:
798 rtnl_unlock();
799 return ret;
801 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
802 bonding_show_downdelay, bonding_store_downdelay);
804 static ssize_t bonding_show_updelay(struct device *d,
805 struct device_attribute *attr,
806 char *buf)
808 struct bonding *bond = to_bond(d);
810 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
814 static ssize_t bonding_store_updelay(struct device *d,
815 struct device_attribute *attr,
816 const char *buf, size_t count)
818 int new_value, ret = count;
819 struct bonding *bond = to_bond(d);
821 if (!rtnl_trylock())
822 return restart_syscall();
823 if (!(bond->params.miimon)) {
824 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
825 bond->dev->name);
826 ret = -EPERM;
827 goto out;
830 if (sscanf(buf, "%d", &new_value) != 1) {
831 pr_err("%s: no up delay value specified.\n",
832 bond->dev->name);
833 ret = -EINVAL;
834 goto out;
836 if (new_value < 0) {
837 pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
838 bond->dev->name, new_value, 0, INT_MAX);
839 ret = -EINVAL;
840 goto out;
841 } else {
842 if ((new_value % bond->params.miimon) != 0) {
843 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
844 bond->dev->name, new_value,
845 bond->params.miimon,
846 (new_value / bond->params.miimon) *
847 bond->params.miimon);
849 bond->params.updelay = new_value / bond->params.miimon;
850 pr_info("%s: Setting up delay to %d.\n",
851 bond->dev->name,
852 bond->params.updelay * bond->params.miimon);
855 out:
856 rtnl_unlock();
857 return ret;
859 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
860 bonding_show_updelay, bonding_store_updelay);
863 * Show and set the LACP interval. Interface must be down, and the mode
864 * must be set to 802.3ad mode.
866 static ssize_t bonding_show_lacp(struct device *d,
867 struct device_attribute *attr,
868 char *buf)
870 struct bonding *bond = to_bond(d);
872 return sprintf(buf, "%s %d\n",
873 bond_lacp_tbl[bond->params.lacp_fast].modename,
874 bond->params.lacp_fast);
877 static ssize_t bonding_store_lacp(struct device *d,
878 struct device_attribute *attr,
879 const char *buf, size_t count)
881 struct bonding *bond = to_bond(d);
882 int new_value, ret = count;
884 if (!rtnl_trylock())
885 return restart_syscall();
887 if (bond->dev->flags & IFF_UP) {
888 pr_err("%s: Unable to update LACP rate because interface is up.\n",
889 bond->dev->name);
890 ret = -EPERM;
891 goto out;
894 if (bond->params.mode != BOND_MODE_8023AD) {
895 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
896 bond->dev->name);
897 ret = -EPERM;
898 goto out;
901 new_value = bond_parse_parm(buf, bond_lacp_tbl);
903 if ((new_value == 1) || (new_value == 0)) {
904 bond->params.lacp_fast = new_value;
905 bond_3ad_update_lacp_rate(bond);
906 pr_info("%s: Setting LACP rate to %s (%d).\n",
907 bond->dev->name, bond_lacp_tbl[new_value].modename,
908 new_value);
909 } else {
910 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
911 bond->dev->name, (int)strlen(buf) - 1, buf);
912 ret = -EINVAL;
914 out:
915 rtnl_unlock();
917 return ret;
919 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
920 bonding_show_lacp, bonding_store_lacp);
922 static ssize_t bonding_show_min_links(struct device *d,
923 struct device_attribute *attr,
924 char *buf)
926 struct bonding *bond = to_bond(d);
928 return sprintf(buf, "%d\n", bond->params.min_links);
931 static ssize_t bonding_store_min_links(struct device *d,
932 struct device_attribute *attr,
933 const char *buf, size_t count)
935 struct bonding *bond = to_bond(d);
936 int ret;
937 unsigned int new_value;
939 ret = kstrtouint(buf, 0, &new_value);
940 if (ret < 0) {
941 pr_err("%s: Ignoring invalid min links value %s.\n",
942 bond->dev->name, buf);
943 return ret;
946 pr_info("%s: Setting min links value to %u\n",
947 bond->dev->name, new_value);
948 bond->params.min_links = new_value;
949 return count;
951 static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
952 bonding_show_min_links, bonding_store_min_links);
954 static ssize_t bonding_show_ad_select(struct device *d,
955 struct device_attribute *attr,
956 char *buf)
958 struct bonding *bond = to_bond(d);
960 return sprintf(buf, "%s %d\n",
961 ad_select_tbl[bond->params.ad_select].modename,
962 bond->params.ad_select);
966 static ssize_t bonding_store_ad_select(struct device *d,
967 struct device_attribute *attr,
968 const char *buf, size_t count)
970 int new_value, ret = count;
971 struct bonding *bond = to_bond(d);
973 if (bond->dev->flags & IFF_UP) {
974 pr_err("%s: Unable to update ad_select because interface is up.\n",
975 bond->dev->name);
976 ret = -EPERM;
977 goto out;
980 new_value = bond_parse_parm(buf, ad_select_tbl);
982 if (new_value != -1) {
983 bond->params.ad_select = new_value;
984 pr_info("%s: Setting ad_select to %s (%d).\n",
985 bond->dev->name, ad_select_tbl[new_value].modename,
986 new_value);
987 } else {
988 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
989 bond->dev->name, (int)strlen(buf) - 1, buf);
990 ret = -EINVAL;
992 out:
993 return ret;
995 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
996 bonding_show_ad_select, bonding_store_ad_select);
999 * Show and set the number of peer notifications to send after a failover event.
1001 static ssize_t bonding_show_num_peer_notif(struct device *d,
1002 struct device_attribute *attr,
1003 char *buf)
1005 struct bonding *bond = to_bond(d);
1006 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
1009 static ssize_t bonding_store_num_peer_notif(struct device *d,
1010 struct device_attribute *attr,
1011 const char *buf, size_t count)
1013 struct bonding *bond = to_bond(d);
1014 int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
1015 return err ? err : count;
1017 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1018 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
1019 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1020 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
1023 * Show and set the MII monitor interval. There are two tricky bits
1024 * here. First, if MII monitoring is activated, then we must disable
1025 * ARP monitoring. Second, if the timer isn't running, we must
1026 * start it.
1028 static ssize_t bonding_show_miimon(struct device *d,
1029 struct device_attribute *attr,
1030 char *buf)
1032 struct bonding *bond = to_bond(d);
1034 return sprintf(buf, "%d\n", bond->params.miimon);
1037 static ssize_t bonding_store_miimon(struct device *d,
1038 struct device_attribute *attr,
1039 const char *buf, size_t count)
1041 int new_value, ret = count;
1042 struct bonding *bond = to_bond(d);
1044 if (!rtnl_trylock())
1045 return restart_syscall();
1046 if (sscanf(buf, "%d", &new_value) != 1) {
1047 pr_err("%s: no miimon value specified.\n",
1048 bond->dev->name);
1049 ret = -EINVAL;
1050 goto out;
1052 if (new_value < 0) {
1053 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1054 bond->dev->name, new_value, 0, INT_MAX);
1055 ret = -EINVAL;
1056 goto out;
1058 pr_info("%s: Setting MII monitoring interval to %d.\n",
1059 bond->dev->name, new_value);
1060 bond->params.miimon = new_value;
1061 if (bond->params.updelay)
1062 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1063 bond->dev->name,
1064 bond->params.updelay * bond->params.miimon);
1065 if (bond->params.downdelay)
1066 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1067 bond->dev->name,
1068 bond->params.downdelay * bond->params.miimon);
1069 if (new_value && bond->params.arp_interval) {
1070 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1071 bond->dev->name);
1072 bond->params.arp_interval = 0;
1073 if (bond->params.arp_validate)
1074 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1076 if (bond->dev->flags & IFF_UP) {
1077 /* If the interface is up, we may need to fire off
1078 * the MII timer. If the interface is down, the
1079 * timer will get fired off when the open function
1080 * is called.
1082 if (!new_value) {
1083 cancel_delayed_work_sync(&bond->mii_work);
1084 } else {
1085 cancel_delayed_work_sync(&bond->arp_work);
1086 queue_delayed_work(bond->wq, &bond->mii_work, 0);
1089 out:
1090 rtnl_unlock();
1091 return ret;
1093 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1094 bonding_show_miimon, bonding_store_miimon);
1097 * Show and set the primary slave. The store function is much
1098 * simpler than bonding_store_slaves function because it only needs to
1099 * handle one interface name.
1100 * The bond must be a mode that supports a primary for this be
1101 * set.
1103 static ssize_t bonding_show_primary(struct device *d,
1104 struct device_attribute *attr,
1105 char *buf)
1107 int count = 0;
1108 struct bonding *bond = to_bond(d);
1110 if (bond->primary_slave)
1111 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1113 return count;
1116 static ssize_t bonding_store_primary(struct device *d,
1117 struct device_attribute *attr,
1118 const char *buf, size_t count)
1120 struct bonding *bond = to_bond(d);
1121 char ifname[IFNAMSIZ];
1122 struct slave *slave;
1124 if (!rtnl_trylock())
1125 return restart_syscall();
1126 block_netpoll_tx();
1127 read_lock(&bond->lock);
1128 write_lock_bh(&bond->curr_slave_lock);
1130 if (!USES_PRIMARY(bond->params.mode)) {
1131 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1132 bond->dev->name, bond->dev->name, bond->params.mode);
1133 goto out;
1136 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1138 /* check to see if we are clearing primary */
1139 if (!strlen(ifname) || buf[0] == '\n') {
1140 pr_info("%s: Setting primary slave to None.\n",
1141 bond->dev->name);
1142 bond->primary_slave = NULL;
1143 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1144 bond_select_active_slave(bond);
1145 goto out;
1148 bond_for_each_slave(bond, slave) {
1149 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1150 pr_info("%s: Setting %s as primary slave.\n",
1151 bond->dev->name, slave->dev->name);
1152 bond->primary_slave = slave;
1153 strcpy(bond->params.primary, slave->dev->name);
1154 bond_select_active_slave(bond);
1155 goto out;
1159 strncpy(bond->params.primary, ifname, IFNAMSIZ);
1160 bond->params.primary[IFNAMSIZ - 1] = 0;
1162 pr_info("%s: Recording %s as primary, "
1163 "but it has not been enslaved to %s yet.\n",
1164 bond->dev->name, ifname, bond->dev->name);
1165 out:
1166 write_unlock_bh(&bond->curr_slave_lock);
1167 read_unlock(&bond->lock);
1168 unblock_netpoll_tx();
1169 rtnl_unlock();
1171 return count;
1173 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1174 bonding_show_primary, bonding_store_primary);
1177 * Show and set the primary_reselect flag.
1179 static ssize_t bonding_show_primary_reselect(struct device *d,
1180 struct device_attribute *attr,
1181 char *buf)
1183 struct bonding *bond = to_bond(d);
1185 return sprintf(buf, "%s %d\n",
1186 pri_reselect_tbl[bond->params.primary_reselect].modename,
1187 bond->params.primary_reselect);
1190 static ssize_t bonding_store_primary_reselect(struct device *d,
1191 struct device_attribute *attr,
1192 const char *buf, size_t count)
1194 int new_value, ret = count;
1195 struct bonding *bond = to_bond(d);
1197 if (!rtnl_trylock())
1198 return restart_syscall();
1200 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1201 if (new_value < 0) {
1202 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1203 bond->dev->name,
1204 (int) strlen(buf) - 1, buf);
1205 ret = -EINVAL;
1206 goto out;
1209 bond->params.primary_reselect = new_value;
1210 pr_info("%s: setting primary_reselect to %s (%d).\n",
1211 bond->dev->name, pri_reselect_tbl[new_value].modename,
1212 new_value);
1214 block_netpoll_tx();
1215 read_lock(&bond->lock);
1216 write_lock_bh(&bond->curr_slave_lock);
1217 bond_select_active_slave(bond);
1218 write_unlock_bh(&bond->curr_slave_lock);
1219 read_unlock(&bond->lock);
1220 unblock_netpoll_tx();
1221 out:
1222 rtnl_unlock();
1223 return ret;
1225 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1226 bonding_show_primary_reselect,
1227 bonding_store_primary_reselect);
1230 * Show and set the use_carrier flag.
1232 static ssize_t bonding_show_carrier(struct device *d,
1233 struct device_attribute *attr,
1234 char *buf)
1236 struct bonding *bond = to_bond(d);
1238 return sprintf(buf, "%d\n", bond->params.use_carrier);
1241 static ssize_t bonding_store_carrier(struct device *d,
1242 struct device_attribute *attr,
1243 const char *buf, size_t count)
1245 int new_value, ret = count;
1246 struct bonding *bond = to_bond(d);
1249 if (sscanf(buf, "%d", &new_value) != 1) {
1250 pr_err("%s: no use_carrier value specified.\n",
1251 bond->dev->name);
1252 ret = -EINVAL;
1253 goto out;
1255 if ((new_value == 0) || (new_value == 1)) {
1256 bond->params.use_carrier = new_value;
1257 pr_info("%s: Setting use_carrier to %d.\n",
1258 bond->dev->name, new_value);
1259 } else {
1260 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1261 bond->dev->name, new_value);
1263 out:
1264 return ret;
1266 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1267 bonding_show_carrier, bonding_store_carrier);
1271 * Show and set currently active_slave.
1273 static ssize_t bonding_show_active_slave(struct device *d,
1274 struct device_attribute *attr,
1275 char *buf)
1277 struct bonding *bond = to_bond(d);
1278 struct slave *curr;
1279 int count = 0;
1281 rcu_read_lock();
1282 curr = rcu_dereference(bond->curr_active_slave);
1283 if (USES_PRIMARY(bond->params.mode) && curr)
1284 count = sprintf(buf, "%s\n", curr->dev->name);
1285 rcu_read_unlock();
1287 return count;
1290 static ssize_t bonding_store_active_slave(struct device *d,
1291 struct device_attribute *attr,
1292 const char *buf, size_t count)
1294 struct slave *slave, *old_active, *new_active;
1295 struct bonding *bond = to_bond(d);
1296 char ifname[IFNAMSIZ];
1298 if (!rtnl_trylock())
1299 return restart_syscall();
1301 old_active = new_active = NULL;
1302 block_netpoll_tx();
1303 read_lock(&bond->lock);
1304 write_lock_bh(&bond->curr_slave_lock);
1306 if (!USES_PRIMARY(bond->params.mode)) {
1307 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1308 bond->dev->name, bond->dev->name, bond->params.mode);
1309 goto out;
1312 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1314 /* check to see if we are clearing active */
1315 if (!strlen(ifname) || buf[0] == '\n') {
1316 pr_info("%s: Clearing current active slave.\n",
1317 bond->dev->name);
1318 rcu_assign_pointer(bond->curr_active_slave, NULL);
1319 bond_select_active_slave(bond);
1320 goto out;
1323 bond_for_each_slave(bond, slave) {
1324 if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1325 old_active = bond->curr_active_slave;
1326 new_active = slave;
1327 if (new_active == old_active) {
1328 /* do nothing */
1329 pr_info("%s: %s is already the current"
1330 " active slave.\n",
1331 bond->dev->name,
1332 slave->dev->name);
1333 goto out;
1334 } else {
1335 if ((new_active) &&
1336 (old_active) &&
1337 (new_active->link == BOND_LINK_UP) &&
1338 IS_UP(new_active->dev)) {
1339 pr_info("%s: Setting %s as active"
1340 " slave.\n",
1341 bond->dev->name,
1342 slave->dev->name);
1343 bond_change_active_slave(bond,
1344 new_active);
1345 } else {
1346 pr_info("%s: Could not set %s as"
1347 " active slave; either %s is"
1348 " down or the link is down.\n",
1349 bond->dev->name,
1350 slave->dev->name,
1351 slave->dev->name);
1353 goto out;
1358 pr_info("%s: Unable to set %.*s as active slave.\n",
1359 bond->dev->name, (int)strlen(buf) - 1, buf);
1360 out:
1361 write_unlock_bh(&bond->curr_slave_lock);
1362 read_unlock(&bond->lock);
1363 unblock_netpoll_tx();
1365 rtnl_unlock();
1367 return count;
1370 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1371 bonding_show_active_slave, bonding_store_active_slave);
1375 * Show link status of the bond interface.
1377 static ssize_t bonding_show_mii_status(struct device *d,
1378 struct device_attribute *attr,
1379 char *buf)
1381 struct bonding *bond = to_bond(d);
1383 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1385 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1388 * Show current 802.3ad aggregator ID.
1390 static ssize_t bonding_show_ad_aggregator(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.aggregator_id);
1404 return count;
1406 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1410 * Show number of active 802.3ad ports.
1412 static ssize_t bonding_show_ad_num_ports(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 count = sprintf(buf, "%d\n",
1422 bond_3ad_get_active_agg_info(bond, &ad_info)
1423 ? 0 : ad_info.ports);
1426 return count;
1428 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1432 * Show current 802.3ad actor key.
1434 static ssize_t bonding_show_ad_actor_key(struct device *d,
1435 struct device_attribute *attr,
1436 char *buf)
1438 int count = 0;
1439 struct bonding *bond = to_bond(d);
1441 if (bond->params.mode == BOND_MODE_8023AD) {
1442 struct ad_info ad_info;
1443 count = sprintf(buf, "%d\n",
1444 bond_3ad_get_active_agg_info(bond, &ad_info)
1445 ? 0 : ad_info.actor_key);
1448 return count;
1450 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1454 * Show current 802.3ad partner key.
1456 static ssize_t bonding_show_ad_partner_key(struct device *d,
1457 struct device_attribute *attr,
1458 char *buf)
1460 int count = 0;
1461 struct bonding *bond = to_bond(d);
1463 if (bond->params.mode == BOND_MODE_8023AD) {
1464 struct ad_info ad_info;
1465 count = sprintf(buf, "%d\n",
1466 bond_3ad_get_active_agg_info(bond, &ad_info)
1467 ? 0 : ad_info.partner_key);
1470 return count;
1472 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1476 * Show current 802.3ad partner mac.
1478 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1479 struct device_attribute *attr,
1480 char *buf)
1482 int count = 0;
1483 struct bonding *bond = to_bond(d);
1485 if (bond->params.mode == BOND_MODE_8023AD) {
1486 struct ad_info ad_info;
1487 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1488 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1491 return count;
1493 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1496 * Show the queue_ids of the slaves in the current bond.
1498 static ssize_t bonding_show_queue_id(struct device *d,
1499 struct device_attribute *attr,
1500 char *buf)
1502 struct bonding *bond = to_bond(d);
1503 struct slave *slave;
1504 int res = 0;
1506 if (!rtnl_trylock())
1507 return restart_syscall();
1509 read_lock(&bond->lock);
1510 bond_for_each_slave(bond, slave) {
1511 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1512 /* not enough space for another interface_name:queue_id pair */
1513 if ((PAGE_SIZE - res) > 10)
1514 res = PAGE_SIZE - 10;
1515 res += sprintf(buf + res, "++more++ ");
1516 break;
1518 res += sprintf(buf + res, "%s:%d ",
1519 slave->dev->name, slave->queue_id);
1521 read_unlock(&bond->lock);
1522 if (res)
1523 buf[res-1] = '\n'; /* eat the leftover space */
1524 rtnl_unlock();
1526 return res;
1530 * Set the queue_ids of the slaves in the current bond. The bond
1531 * interface must be enslaved for this to work.
1533 static ssize_t bonding_store_queue_id(struct device *d,
1534 struct device_attribute *attr,
1535 const char *buffer, size_t count)
1537 struct slave *slave, *update_slave;
1538 struct bonding *bond = to_bond(d);
1539 u16 qid;
1540 int ret = count;
1541 char *delim;
1542 struct net_device *sdev = NULL;
1544 if (!rtnl_trylock())
1545 return restart_syscall();
1547 /* delim will point to queue id if successful */
1548 delim = strchr(buffer, ':');
1549 if (!delim)
1550 goto err_no_cmd;
1553 * Terminate string that points to device name and bump it
1554 * up one, so we can read the queue id there.
1556 *delim = '\0';
1557 if (sscanf(++delim, "%hd\n", &qid) != 1)
1558 goto err_no_cmd;
1560 /* Check buffer length, valid ifname and queue id */
1561 if (strlen(buffer) > IFNAMSIZ ||
1562 !dev_valid_name(buffer) ||
1563 qid > bond->dev->real_num_tx_queues)
1564 goto err_no_cmd;
1566 /* Get the pointer to that interface if it exists */
1567 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1568 if (!sdev)
1569 goto err_no_cmd;
1571 read_lock(&bond->lock);
1573 /* Search for thes slave and check for duplicate qids */
1574 update_slave = NULL;
1575 bond_for_each_slave(bond, slave) {
1576 if (sdev == slave->dev)
1578 * We don't need to check the matching
1579 * slave for dups, since we're overwriting it
1581 update_slave = slave;
1582 else if (qid && qid == slave->queue_id) {
1583 goto err_no_cmd_unlock;
1587 if (!update_slave)
1588 goto err_no_cmd_unlock;
1590 /* Actually set the qids for the slave */
1591 update_slave->queue_id = qid;
1593 read_unlock(&bond->lock);
1594 out:
1595 rtnl_unlock();
1596 return ret;
1598 err_no_cmd_unlock:
1599 read_unlock(&bond->lock);
1600 err_no_cmd:
1601 pr_info("invalid input for queue_id set for %s.\n",
1602 bond->dev->name);
1603 ret = -EPERM;
1604 goto out;
1607 static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1608 bonding_store_queue_id);
1612 * Show and set the all_slaves_active flag.
1614 static ssize_t bonding_show_slaves_active(struct device *d,
1615 struct device_attribute *attr,
1616 char *buf)
1618 struct bonding *bond = to_bond(d);
1620 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1623 static ssize_t bonding_store_slaves_active(struct device *d,
1624 struct device_attribute *attr,
1625 const char *buf, size_t count)
1627 struct bonding *bond = to_bond(d);
1628 int new_value, ret = count;
1629 struct slave *slave;
1631 if (sscanf(buf, "%d", &new_value) != 1) {
1632 pr_err("%s: no all_slaves_active value specified.\n",
1633 bond->dev->name);
1634 ret = -EINVAL;
1635 goto out;
1638 if (new_value == bond->params.all_slaves_active)
1639 goto out;
1641 if ((new_value == 0) || (new_value == 1)) {
1642 bond->params.all_slaves_active = new_value;
1643 } else {
1644 pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1645 bond->dev->name, new_value);
1646 ret = -EINVAL;
1647 goto out;
1650 read_lock(&bond->lock);
1651 bond_for_each_slave(bond, slave) {
1652 if (!bond_is_active_slave(slave)) {
1653 if (new_value)
1654 slave->inactive = 0;
1655 else
1656 slave->inactive = 1;
1659 read_unlock(&bond->lock);
1660 out:
1661 return ret;
1663 static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1664 bonding_show_slaves_active, bonding_store_slaves_active);
1667 * Show and set the number of IGMP membership reports to send on link failure
1669 static ssize_t bonding_show_resend_igmp(struct device *d,
1670 struct device_attribute *attr,
1671 char *buf)
1673 struct bonding *bond = to_bond(d);
1675 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1678 static ssize_t bonding_store_resend_igmp(struct device *d,
1679 struct device_attribute *attr,
1680 const char *buf, size_t count)
1682 int new_value, ret = count;
1683 struct bonding *bond = to_bond(d);
1685 if (sscanf(buf, "%d", &new_value) != 1) {
1686 pr_err("%s: no resend_igmp value specified.\n",
1687 bond->dev->name);
1688 ret = -EINVAL;
1689 goto out;
1692 if (new_value < 0 || new_value > 255) {
1693 pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1694 bond->dev->name, new_value);
1695 ret = -EINVAL;
1696 goto out;
1699 pr_info("%s: Setting resend_igmp to %d.\n",
1700 bond->dev->name, new_value);
1701 bond->params.resend_igmp = new_value;
1702 out:
1703 return ret;
1706 static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1707 bonding_show_resend_igmp, bonding_store_resend_igmp);
1710 static ssize_t bonding_show_lp_interval(struct device *d,
1711 struct device_attribute *attr,
1712 char *buf)
1714 struct bonding *bond = to_bond(d);
1715 return sprintf(buf, "%d\n", bond->params.lp_interval);
1718 static ssize_t bonding_store_lp_interval(struct device *d,
1719 struct device_attribute *attr,
1720 const char *buf, size_t count)
1722 struct bonding *bond = to_bond(d);
1723 int new_value, ret = count;
1725 if (sscanf(buf, "%d", &new_value) != 1) {
1726 pr_err("%s: no lp interval value specified.\n",
1727 bond->dev->name);
1728 ret = -EINVAL;
1729 goto out;
1732 if (new_value <= 0) {
1733 pr_err ("%s: lp_interval must be between 1 and %d\n",
1734 bond->dev->name, INT_MAX);
1735 ret = -EINVAL;
1736 goto out;
1739 bond->params.lp_interval = new_value;
1740 out:
1741 return ret;
1744 static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1745 bonding_show_lp_interval, bonding_store_lp_interval);
1747 static struct attribute *per_bond_attrs[] = {
1748 &dev_attr_slaves.attr,
1749 &dev_attr_mode.attr,
1750 &dev_attr_fail_over_mac.attr,
1751 &dev_attr_arp_validate.attr,
1752 &dev_attr_arp_all_targets.attr,
1753 &dev_attr_arp_interval.attr,
1754 &dev_attr_arp_ip_target.attr,
1755 &dev_attr_downdelay.attr,
1756 &dev_attr_updelay.attr,
1757 &dev_attr_lacp_rate.attr,
1758 &dev_attr_ad_select.attr,
1759 &dev_attr_xmit_hash_policy.attr,
1760 &dev_attr_num_grat_arp.attr,
1761 &dev_attr_num_unsol_na.attr,
1762 &dev_attr_miimon.attr,
1763 &dev_attr_primary.attr,
1764 &dev_attr_primary_reselect.attr,
1765 &dev_attr_use_carrier.attr,
1766 &dev_attr_active_slave.attr,
1767 &dev_attr_mii_status.attr,
1768 &dev_attr_ad_aggregator.attr,
1769 &dev_attr_ad_num_ports.attr,
1770 &dev_attr_ad_actor_key.attr,
1771 &dev_attr_ad_partner_key.attr,
1772 &dev_attr_ad_partner_mac.attr,
1773 &dev_attr_queue_id.attr,
1774 &dev_attr_all_slaves_active.attr,
1775 &dev_attr_resend_igmp.attr,
1776 &dev_attr_min_links.attr,
1777 &dev_attr_lp_interval.attr,
1778 NULL,
1781 static struct attribute_group bonding_group = {
1782 .name = "bonding",
1783 .attrs = per_bond_attrs,
1787 * Initialize sysfs. This sets up the bonding_masters file in
1788 * /sys/class/net.
1790 int bond_create_sysfs(struct bond_net *bn)
1792 int ret;
1794 bn->class_attr_bonding_masters = class_attr_bonding_masters;
1795 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1797 ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
1799 * Permit multiple loads of the module by ignoring failures to
1800 * create the bonding_masters sysfs file. Bonding devices
1801 * created by second or subsequent loads of the module will
1802 * not be listed in, or controllable by, bonding_masters, but
1803 * will have the usual "bonding" sysfs directory.
1805 * This is done to preserve backwards compatibility for
1806 * initscripts/sysconfig, which load bonding multiple times to
1807 * configure multiple bonding devices.
1809 if (ret == -EEXIST) {
1810 /* Is someone being kinky and naming a device bonding_master? */
1811 if (__dev_get_by_name(bn->net,
1812 class_attr_bonding_masters.attr.name))
1813 pr_err("network device named %s already exists in sysfs",
1814 class_attr_bonding_masters.attr.name);
1815 ret = 0;
1818 return ret;
1823 * Remove /sys/class/net/bonding_masters.
1825 void bond_destroy_sysfs(struct bond_net *bn)
1827 netdev_class_remove_file(&bn->class_attr_bonding_masters);
1831 * Initialize sysfs for each bond. This sets up and registers
1832 * the 'bondctl' directory for each individual bond under /sys/class/net.
1834 void bond_prepare_sysfs_group(struct bonding *bond)
1836 bond->dev->sysfs_groups[0] = &bonding_group;