2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/sched.h>
29 #include <linux/sysdev.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/netdevice.h>
34 #include <linux/inetdevice.h>
36 #include <linux/sysfs.h>
37 #include <linux/ctype.h>
38 #include <linux/inet.h>
39 #include <linux/rtnetlink.h>
40 #include <linux/etherdevice.h>
41 #include <net/net_namespace.h>
42 #include <net/netns/generic.h>
43 #include <linux/nsproxy.h>
47 #define to_dev(obj) container_of(obj, struct device, kobj)
48 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
54 static ssize_t
bonding_show_bonds(struct class *cls
, char *buf
)
56 struct net
*net
= current
->nsproxy
->net_ns
;
57 struct bond_net
*bn
= net_generic(net
, bond_net_id
);
63 list_for_each_entry(bond
, &bn
->dev_list
, bond_list
) {
64 if (res
> (PAGE_SIZE
- IFNAMSIZ
)) {
65 /* not enough space for another interface name */
66 if ((PAGE_SIZE
- res
) > 10)
68 res
+= sprintf(buf
+ res
, "++more++ ");
71 res
+= sprintf(buf
+ res
, "%s ", bond
->dev
->name
);
74 buf
[res
-1] = '\n'; /* eat the leftover space */
80 static struct net_device
*bond_get_by_name(struct net
*net
, const char *ifname
)
82 struct bond_net
*bn
= net_generic(net
, bond_net_id
);
85 list_for_each_entry(bond
, &bn
->dev_list
, bond_list
) {
86 if (strncmp(bond
->dev
->name
, ifname
, IFNAMSIZ
) == 0)
93 * "store" function for the bond_masters attribute. This is what
94 * creates and deletes entire bonds.
96 * The class parameter is ignored.
100 static ssize_t
bonding_store_bonds(struct class *cls
,
101 const char *buffer
, size_t count
)
103 struct net
*net
= current
->nsproxy
->net_ns
;
104 char command
[IFNAMSIZ
+ 1] = {0, };
108 sscanf(buffer
, "%16s", command
); /* IFNAMSIZ*/
109 ifname
= command
+ 1;
110 if ((strlen(command
) <= 1) ||
111 !dev_valid_name(ifname
))
114 if (command
[0] == '+') {
115 pr_info("%s is being created...\n", ifname
);
116 rv
= bond_create(net
, ifname
);
118 pr_info("Bond creation failed.\n");
121 } else if (command
[0] == '-') {
122 struct net_device
*bond_dev
;
125 bond_dev
= bond_get_by_name(net
, ifname
);
127 pr_info("%s is being deleted...\n", ifname
);
128 unregister_netdevice(bond_dev
);
130 pr_err("unable to delete non-existent %s\n", ifname
);
137 /* Always return either count or an error. If you return 0, you'll
138 * get called forever, which is bad.
143 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
147 /* class attribute for bond_masters file. This ends up in /sys/class/net */
148 static CLASS_ATTR(bonding_masters
, S_IWUSR
| S_IRUGO
,
149 bonding_show_bonds
, bonding_store_bonds
);
151 int bond_create_slave_symlinks(struct net_device
*master
,
152 struct net_device
*slave
)
154 char linkname
[IFNAMSIZ
+7];
157 /* first, create a link from the slave back to the master */
158 ret
= sysfs_create_link(&(slave
->dev
.kobj
), &(master
->dev
.kobj
),
162 /* next, create a link from the master to the slave */
163 sprintf(linkname
, "slave_%s", slave
->name
);
164 ret
= sysfs_create_link(&(master
->dev
.kobj
), &(slave
->dev
.kobj
),
170 void bond_destroy_slave_symlinks(struct net_device
*master
,
171 struct net_device
*slave
)
173 char linkname
[IFNAMSIZ
+7];
175 sysfs_remove_link(&(slave
->dev
.kobj
), "master");
176 sprintf(linkname
, "slave_%s", slave
->name
);
177 sysfs_remove_link(&(master
->dev
.kobj
), linkname
);
182 * Show the slaves in the current bond.
184 static ssize_t
bonding_show_slaves(struct device
*d
,
185 struct device_attribute
*attr
, char *buf
)
189 struct bonding
*bond
= to_bond(d
);
191 read_lock(&bond
->lock
);
192 bond_for_each_slave(bond
, slave
, i
) {
193 if (res
> (PAGE_SIZE
- IFNAMSIZ
)) {
194 /* not enough space for another interface name */
195 if ((PAGE_SIZE
- res
) > 10)
196 res
= PAGE_SIZE
- 10;
197 res
+= sprintf(buf
+ res
, "++more++ ");
200 res
+= sprintf(buf
+ res
, "%s ", slave
->dev
->name
);
202 read_unlock(&bond
->lock
);
204 buf
[res
-1] = '\n'; /* eat the leftover space */
209 * Set the slaves in the current bond. The bond interface must be
210 * up for this to succeed.
211 * This function is largely the same flow as bonding_update_bonds().
213 static ssize_t
bonding_store_slaves(struct device
*d
,
214 struct device_attribute
*attr
,
215 const char *buffer
, size_t count
)
217 char command
[IFNAMSIZ
+ 1] = { 0, };
219 int i
, res
, found
, ret
= count
;
222 struct net_device
*dev
= NULL
;
223 struct bonding
*bond
= to_bond(d
);
225 /* Quick sanity check -- is the bond interface up? */
226 if (!(bond
->dev
->flags
& IFF_UP
)) {
227 pr_warning("%s: doing slave updates when interface is down.\n",
231 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
234 return restart_syscall();
236 sscanf(buffer
, "%16s", command
); /* IFNAMSIZ*/
237 ifname
= command
+ 1;
238 if ((strlen(command
) <= 1) ||
239 !dev_valid_name(ifname
))
242 if (command
[0] == '+') {
244 /* Got a slave name in ifname. Is it already in the list? */
247 dev
= __dev_get_by_name(dev_net(bond
->dev
), ifname
);
249 pr_info("%s: Interface %s does not exist!\n",
250 bond
->dev
->name
, ifname
);
255 if (dev
->flags
& IFF_UP
) {
256 pr_err("%s: Error: Unable to enslave %s because it is already up.\n",
257 bond
->dev
->name
, dev
->name
);
262 read_lock(&bond
->lock
);
263 bond_for_each_slave(bond
, slave
, i
)
264 if (slave
->dev
== dev
) {
265 pr_err("%s: Interface %s is already enslaved!\n",
266 bond
->dev
->name
, ifname
);
268 read_unlock(&bond
->lock
);
271 read_unlock(&bond
->lock
);
273 pr_info("%s: Adding slave %s.\n", bond
->dev
->name
, ifname
);
275 /* If this is the first slave, then we need to set
276 the master's hardware address to be the same as the
278 if (is_zero_ether_addr(bond
->dev
->dev_addr
))
279 memcpy(bond
->dev
->dev_addr
, dev
->dev_addr
,
282 /* Set the slave's MTU to match the bond */
283 original_mtu
= dev
->mtu
;
284 res
= dev_set_mtu(dev
, bond
->dev
->mtu
);
290 res
= bond_enslave(bond
->dev
, dev
);
291 bond_for_each_slave(bond
, slave
, i
)
292 if (strnicmp(slave
->dev
->name
, ifname
, IFNAMSIZ
) == 0)
293 slave
->original_mtu
= original_mtu
;
300 if (command
[0] == '-') {
303 bond_for_each_slave(bond
, slave
, i
)
304 if (strnicmp(slave
->dev
->name
, ifname
, IFNAMSIZ
) == 0) {
306 original_mtu
= slave
->original_mtu
;
310 pr_info("%s: Removing slave %s\n",
311 bond
->dev
->name
, dev
->name
);
312 res
= bond_release(bond
->dev
, dev
);
317 /* set the slave MTU to the default */
318 dev_set_mtu(dev
, original_mtu
);
320 pr_err("unable to remove non-existent slave %s for bond %s.\n",
321 ifname
, bond
->dev
->name
);
328 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
337 static DEVICE_ATTR(slaves
, S_IRUGO
| S_IWUSR
, bonding_show_slaves
,
338 bonding_store_slaves
);
341 * Show and set the bonding mode. The bond interface must be down to
344 static ssize_t
bonding_show_mode(struct device
*d
,
345 struct device_attribute
*attr
, char *buf
)
347 struct bonding
*bond
= to_bond(d
);
349 return sprintf(buf
, "%s %d\n",
350 bond_mode_tbl
[bond
->params
.mode
].modename
,
354 static ssize_t
bonding_store_mode(struct device
*d
,
355 struct device_attribute
*attr
,
356 const char *buf
, size_t count
)
358 int new_value
, ret
= count
;
359 struct bonding
*bond
= to_bond(d
);
361 if (bond
->dev
->flags
& IFF_UP
) {
362 pr_err("unable to update mode of %s because interface is up.\n",
368 new_value
= bond_parse_parm(buf
, bond_mode_tbl
);
370 pr_err("%s: Ignoring invalid mode value %.*s.\n",
371 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
375 if (bond
->params
.mode
== BOND_MODE_8023AD
)
376 bond_unset_master_3ad_flags(bond
);
378 if (bond
->params
.mode
== BOND_MODE_ALB
)
379 bond_unset_master_alb_flags(bond
);
381 bond
->params
.mode
= new_value
;
382 bond_set_mode_ops(bond
, bond
->params
.mode
);
383 pr_info("%s: setting mode to %s (%d).\n",
384 bond
->dev
->name
, bond_mode_tbl
[new_value
].modename
,
390 static DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
391 bonding_show_mode
, bonding_store_mode
);
394 * Show and set the bonding transmit hash method.
395 * The bond interface must be down to change the xmit hash policy.
397 static ssize_t
bonding_show_xmit_hash(struct device
*d
,
398 struct device_attribute
*attr
,
401 struct bonding
*bond
= to_bond(d
);
403 return sprintf(buf
, "%s %d\n",
404 xmit_hashtype_tbl
[bond
->params
.xmit_policy
].modename
,
405 bond
->params
.xmit_policy
);
408 static ssize_t
bonding_store_xmit_hash(struct device
*d
,
409 struct device_attribute
*attr
,
410 const char *buf
, size_t count
)
412 int new_value
, ret
= count
;
413 struct bonding
*bond
= to_bond(d
);
415 if (bond
->dev
->flags
& IFF_UP
) {
416 pr_err("%s: Interface is up. Unable to update xmit policy.\n",
422 new_value
= bond_parse_parm(buf
, xmit_hashtype_tbl
);
424 pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
426 (int)strlen(buf
) - 1, buf
);
430 bond
->params
.xmit_policy
= new_value
;
431 bond_set_mode_ops(bond
, bond
->params
.mode
);
432 pr_info("%s: setting xmit hash policy to %s (%d).\n",
434 xmit_hashtype_tbl
[new_value
].modename
, new_value
);
439 static DEVICE_ATTR(xmit_hash_policy
, S_IRUGO
| S_IWUSR
,
440 bonding_show_xmit_hash
, bonding_store_xmit_hash
);
443 * Show and set arp_validate.
445 static ssize_t
bonding_show_arp_validate(struct device
*d
,
446 struct device_attribute
*attr
,
449 struct bonding
*bond
= to_bond(d
);
451 return sprintf(buf
, "%s %d\n",
452 arp_validate_tbl
[bond
->params
.arp_validate
].modename
,
453 bond
->params
.arp_validate
);
456 static ssize_t
bonding_store_arp_validate(struct device
*d
,
457 struct device_attribute
*attr
,
458 const char *buf
, size_t count
)
461 struct bonding
*bond
= to_bond(d
);
463 new_value
= bond_parse_parm(buf
, arp_validate_tbl
);
465 pr_err("%s: Ignoring invalid arp_validate value %s\n",
466 bond
->dev
->name
, buf
);
469 if (new_value
&& (bond
->params
.mode
!= BOND_MODE_ACTIVEBACKUP
)) {
470 pr_err("%s: arp_validate only supported in active-backup mode.\n",
474 pr_info("%s: setting arp_validate to %s (%d).\n",
475 bond
->dev
->name
, arp_validate_tbl
[new_value
].modename
,
478 if (!bond
->params
.arp_validate
&& new_value
)
479 bond_register_arp(bond
);
480 else if (bond
->params
.arp_validate
&& !new_value
)
481 bond_unregister_arp(bond
);
483 bond
->params
.arp_validate
= new_value
;
488 static DEVICE_ATTR(arp_validate
, S_IRUGO
| S_IWUSR
, bonding_show_arp_validate
,
489 bonding_store_arp_validate
);
492 * Show and store fail_over_mac. User only allowed to change the
493 * value when there are no slaves.
495 static ssize_t
bonding_show_fail_over_mac(struct device
*d
,
496 struct device_attribute
*attr
,
499 struct bonding
*bond
= to_bond(d
);
501 return sprintf(buf
, "%s %d\n",
502 fail_over_mac_tbl
[bond
->params
.fail_over_mac
].modename
,
503 bond
->params
.fail_over_mac
);
506 static ssize_t
bonding_store_fail_over_mac(struct device
*d
,
507 struct device_attribute
*attr
,
508 const char *buf
, size_t count
)
511 struct bonding
*bond
= to_bond(d
);
513 if (bond
->slave_cnt
!= 0) {
514 pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
519 new_value
= bond_parse_parm(buf
, fail_over_mac_tbl
);
521 pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
522 bond
->dev
->name
, buf
);
526 bond
->params
.fail_over_mac
= new_value
;
527 pr_info("%s: Setting fail_over_mac to %s (%d).\n",
528 bond
->dev
->name
, fail_over_mac_tbl
[new_value
].modename
,
534 static DEVICE_ATTR(fail_over_mac
, S_IRUGO
| S_IWUSR
,
535 bonding_show_fail_over_mac
, bonding_store_fail_over_mac
);
538 * Show and set the arp timer interval. There are two tricky bits
539 * here. First, if ARP monitoring is activated, then we must disable
540 * MII monitoring. Second, if the ARP timer isn't running, we must
543 static ssize_t
bonding_show_arp_interval(struct device
*d
,
544 struct device_attribute
*attr
,
547 struct bonding
*bond
= to_bond(d
);
549 return sprintf(buf
, "%d\n", bond
->params
.arp_interval
);
552 static ssize_t
bonding_store_arp_interval(struct device
*d
,
553 struct device_attribute
*attr
,
554 const char *buf
, size_t count
)
556 int new_value
, ret
= count
;
557 struct bonding
*bond
= to_bond(d
);
559 if (sscanf(buf
, "%d", &new_value
) != 1) {
560 pr_err("%s: no arp_interval value specified.\n",
566 pr_err("%s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
567 bond
->dev
->name
, new_value
, INT_MAX
);
572 pr_info("%s: Setting ARP monitoring interval to %d.\n",
573 bond
->dev
->name
, new_value
);
574 bond
->params
.arp_interval
= new_value
;
575 if (bond
->params
.arp_interval
)
576 bond
->dev
->priv_flags
|= IFF_MASTER_ARPMON
;
577 if (bond
->params
.miimon
) {
578 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
579 bond
->dev
->name
, bond
->dev
->name
);
580 bond
->params
.miimon
= 0;
581 if (delayed_work_pending(&bond
->mii_work
)) {
582 cancel_delayed_work(&bond
->mii_work
);
583 flush_workqueue(bond
->wq
);
586 if (!bond
->params
.arp_targets
[0]) {
587 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
590 if (bond
->dev
->flags
& IFF_UP
) {
591 /* If the interface is up, we may need to fire off
592 * the ARP timer. If the interface is down, the
593 * timer will get fired off when the open function
596 if (!delayed_work_pending(&bond
->arp_work
)) {
597 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
)
598 INIT_DELAYED_WORK(&bond
->arp_work
,
599 bond_activebackup_arp_mon
);
601 INIT_DELAYED_WORK(&bond
->arp_work
,
602 bond_loadbalance_arp_mon
);
604 queue_delayed_work(bond
->wq
, &bond
->arp_work
, 0);
611 static DEVICE_ATTR(arp_interval
, S_IRUGO
| S_IWUSR
,
612 bonding_show_arp_interval
, bonding_store_arp_interval
);
615 * Show and set the arp targets.
617 static ssize_t
bonding_show_arp_targets(struct device
*d
,
618 struct device_attribute
*attr
,
622 struct bonding
*bond
= to_bond(d
);
624 for (i
= 0; i
< BOND_MAX_ARP_TARGETS
; i
++) {
625 if (bond
->params
.arp_targets
[i
])
626 res
+= sprintf(buf
+ res
, "%pI4 ",
627 &bond
->params
.arp_targets
[i
]);
630 buf
[res
-1] = '\n'; /* eat the leftover space */
634 static ssize_t
bonding_store_arp_targets(struct device
*d
,
635 struct device_attribute
*attr
,
636 const char *buf
, size_t count
)
639 int i
= 0, done
= 0, ret
= count
;
640 struct bonding
*bond
= to_bond(d
);
643 targets
= bond
->params
.arp_targets
;
644 newtarget
= in_aton(buf
+ 1);
647 if ((newtarget
== 0) || (newtarget
== htonl(INADDR_BROADCAST
))) {
648 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
649 bond
->dev
->name
, &newtarget
);
653 /* look for an empty slot to put the target in, and check for dupes */
654 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
) && !done
; i
++) {
655 if (targets
[i
] == newtarget
) { /* duplicate */
656 pr_err("%s: ARP target %pI4 is already present\n",
657 bond
->dev
->name
, &newtarget
);
661 if (targets
[i
] == 0) {
662 pr_info("%s: adding ARP target %pI4.\n",
663 bond
->dev
->name
, &newtarget
);
665 targets
[i
] = newtarget
;
669 pr_err("%s: ARP target table is full!\n",
675 } else if (buf
[0] == '-') {
676 if ((newtarget
== 0) || (newtarget
== htonl(INADDR_BROADCAST
))) {
677 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
678 bond
->dev
->name
, &newtarget
);
683 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
) && !done
; i
++) {
684 if (targets
[i
] == newtarget
) {
686 pr_info("%s: removing ARP target %pI4.\n",
687 bond
->dev
->name
, &newtarget
);
688 for (j
= i
; (j
< (BOND_MAX_ARP_TARGETS
-1)) && targets
[j
+1]; j
++)
689 targets
[j
] = targets
[j
+1];
696 pr_info("%s: unable to remove nonexistent ARP target %pI4.\n",
697 bond
->dev
->name
, &newtarget
);
702 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
711 static DEVICE_ATTR(arp_ip_target
, S_IRUGO
| S_IWUSR
, bonding_show_arp_targets
, bonding_store_arp_targets
);
714 * Show and set the up and down delays. These must be multiples of the
715 * MII monitoring value, and are stored internally as the multiplier.
716 * Thus, we must translate to MS for the real world.
718 static ssize_t
bonding_show_downdelay(struct device
*d
,
719 struct device_attribute
*attr
,
722 struct bonding
*bond
= to_bond(d
);
724 return sprintf(buf
, "%d\n", bond
->params
.downdelay
* bond
->params
.miimon
);
727 static ssize_t
bonding_store_downdelay(struct device
*d
,
728 struct device_attribute
*attr
,
729 const char *buf
, size_t count
)
731 int new_value
, ret
= count
;
732 struct bonding
*bond
= to_bond(d
);
734 if (!(bond
->params
.miimon
)) {
735 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
741 if (sscanf(buf
, "%d", &new_value
) != 1) {
742 pr_err("%s: no down delay value specified.\n", bond
->dev
->name
);
747 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
748 bond
->dev
->name
, new_value
, 1, INT_MAX
);
752 if ((new_value
% bond
->params
.miimon
) != 0) {
753 pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
754 bond
->dev
->name
, new_value
,
756 (new_value
/ bond
->params
.miimon
) *
757 bond
->params
.miimon
);
759 bond
->params
.downdelay
= new_value
/ bond
->params
.miimon
;
760 pr_info("%s: Setting down delay to %d.\n",
762 bond
->params
.downdelay
* bond
->params
.miimon
);
769 static DEVICE_ATTR(downdelay
, S_IRUGO
| S_IWUSR
,
770 bonding_show_downdelay
, bonding_store_downdelay
);
772 static ssize_t
bonding_show_updelay(struct device
*d
,
773 struct device_attribute
*attr
,
776 struct bonding
*bond
= to_bond(d
);
778 return sprintf(buf
, "%d\n", bond
->params
.updelay
* bond
->params
.miimon
);
782 static ssize_t
bonding_store_updelay(struct device
*d
,
783 struct device_attribute
*attr
,
784 const char *buf
, size_t count
)
786 int new_value
, ret
= count
;
787 struct bonding
*bond
= to_bond(d
);
789 if (!(bond
->params
.miimon
)) {
790 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
796 if (sscanf(buf
, "%d", &new_value
) != 1) {
797 pr_err("%s: no up delay value specified.\n",
803 pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
804 bond
->dev
->name
, new_value
, 1, INT_MAX
);
808 if ((new_value
% bond
->params
.miimon
) != 0) {
809 pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
810 bond
->dev
->name
, new_value
,
812 (new_value
/ bond
->params
.miimon
) *
813 bond
->params
.miimon
);
815 bond
->params
.updelay
= new_value
/ bond
->params
.miimon
;
816 pr_info("%s: Setting up delay to %d.\n",
818 bond
->params
.updelay
* bond
->params
.miimon
);
824 static DEVICE_ATTR(updelay
, S_IRUGO
| S_IWUSR
,
825 bonding_show_updelay
, bonding_store_updelay
);
828 * Show and set the LACP interval. Interface must be down, and the mode
829 * must be set to 802.3ad mode.
831 static ssize_t
bonding_show_lacp(struct device
*d
,
832 struct device_attribute
*attr
,
835 struct bonding
*bond
= to_bond(d
);
837 return sprintf(buf
, "%s %d\n",
838 bond_lacp_tbl
[bond
->params
.lacp_fast
].modename
,
839 bond
->params
.lacp_fast
);
842 static ssize_t
bonding_store_lacp(struct device
*d
,
843 struct device_attribute
*attr
,
844 const char *buf
, size_t count
)
846 int new_value
, ret
= count
;
847 struct bonding
*bond
= to_bond(d
);
849 if (bond
->dev
->flags
& IFF_UP
) {
850 pr_err("%s: Unable to update LACP rate because interface is up.\n",
856 if (bond
->params
.mode
!= BOND_MODE_8023AD
) {
857 pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
863 new_value
= bond_parse_parm(buf
, bond_lacp_tbl
);
865 if ((new_value
== 1) || (new_value
== 0)) {
866 bond
->params
.lacp_fast
= new_value
;
867 pr_info("%s: Setting LACP rate to %s (%d).\n",
868 bond
->dev
->name
, bond_lacp_tbl
[new_value
].modename
,
871 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
872 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
878 static DEVICE_ATTR(lacp_rate
, S_IRUGO
| S_IWUSR
,
879 bonding_show_lacp
, bonding_store_lacp
);
881 static ssize_t
bonding_show_ad_select(struct device
*d
,
882 struct device_attribute
*attr
,
885 struct bonding
*bond
= to_bond(d
);
887 return sprintf(buf
, "%s %d\n",
888 ad_select_tbl
[bond
->params
.ad_select
].modename
,
889 bond
->params
.ad_select
);
893 static ssize_t
bonding_store_ad_select(struct device
*d
,
894 struct device_attribute
*attr
,
895 const char *buf
, size_t count
)
897 int new_value
, ret
= count
;
898 struct bonding
*bond
= to_bond(d
);
900 if (bond
->dev
->flags
& IFF_UP
) {
901 pr_err("%s: Unable to update ad_select because interface is up.\n",
907 new_value
= bond_parse_parm(buf
, ad_select_tbl
);
909 if (new_value
!= -1) {
910 bond
->params
.ad_select
= new_value
;
911 pr_info("%s: Setting ad_select to %s (%d).\n",
912 bond
->dev
->name
, ad_select_tbl
[new_value
].modename
,
915 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
916 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
922 static DEVICE_ATTR(ad_select
, S_IRUGO
| S_IWUSR
,
923 bonding_show_ad_select
, bonding_store_ad_select
);
926 * Show and set the number of grat ARP to send after a failover event.
928 static ssize_t
bonding_show_n_grat_arp(struct device
*d
,
929 struct device_attribute
*attr
,
932 struct bonding
*bond
= to_bond(d
);
934 return sprintf(buf
, "%d\n", bond
->params
.num_grat_arp
);
937 static ssize_t
bonding_store_n_grat_arp(struct device
*d
,
938 struct device_attribute
*attr
,
939 const char *buf
, size_t count
)
941 int new_value
, ret
= count
;
942 struct bonding
*bond
= to_bond(d
);
944 if (sscanf(buf
, "%d", &new_value
) != 1) {
945 pr_err("%s: no num_grat_arp value specified.\n",
950 if (new_value
< 0 || new_value
> 255) {
951 pr_err("%s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
952 bond
->dev
->name
, new_value
);
956 bond
->params
.num_grat_arp
= new_value
;
961 static DEVICE_ATTR(num_grat_arp
, S_IRUGO
| S_IWUSR
,
962 bonding_show_n_grat_arp
, bonding_store_n_grat_arp
);
965 * Show and set the number of unsolicited NA's to send after a failover event.
967 static ssize_t
bonding_show_n_unsol_na(struct device
*d
,
968 struct device_attribute
*attr
,
971 struct bonding
*bond
= to_bond(d
);
973 return sprintf(buf
, "%d\n", bond
->params
.num_unsol_na
);
976 static ssize_t
bonding_store_n_unsol_na(struct device
*d
,
977 struct device_attribute
*attr
,
978 const char *buf
, size_t count
)
980 int new_value
, ret
= count
;
981 struct bonding
*bond
= to_bond(d
);
983 if (sscanf(buf
, "%d", &new_value
) != 1) {
984 pr_err("%s: no num_unsol_na value specified.\n",
990 if (new_value
< 0 || new_value
> 255) {
991 pr_err("%s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
992 bond
->dev
->name
, new_value
);
996 bond
->params
.num_unsol_na
= new_value
;
1000 static DEVICE_ATTR(num_unsol_na
, S_IRUGO
| S_IWUSR
,
1001 bonding_show_n_unsol_na
, bonding_store_n_unsol_na
);
1004 * Show and set the MII monitor interval. There are two tricky bits
1005 * here. First, if MII monitoring is activated, then we must disable
1006 * ARP monitoring. Second, if the timer isn't running, we must
1009 static ssize_t
bonding_show_miimon(struct device
*d
,
1010 struct device_attribute
*attr
,
1013 struct bonding
*bond
= to_bond(d
);
1015 return sprintf(buf
, "%d\n", bond
->params
.miimon
);
1018 static ssize_t
bonding_store_miimon(struct device
*d
,
1019 struct device_attribute
*attr
,
1020 const char *buf
, size_t count
)
1022 int new_value
, ret
= count
;
1023 struct bonding
*bond
= to_bond(d
);
1025 if (sscanf(buf
, "%d", &new_value
) != 1) {
1026 pr_err("%s: no miimon value specified.\n",
1031 if (new_value
< 0) {
1032 pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1033 bond
->dev
->name
, new_value
, 1, INT_MAX
);
1037 pr_info("%s: Setting MII monitoring interval to %d.\n",
1038 bond
->dev
->name
, new_value
);
1039 bond
->params
.miimon
= new_value
;
1040 if (bond
->params
.updelay
)
1041 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1043 bond
->params
.updelay
* bond
->params
.miimon
);
1044 if (bond
->params
.downdelay
)
1045 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1047 bond
->params
.downdelay
* bond
->params
.miimon
);
1048 if (bond
->params
.arp_interval
) {
1049 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1051 bond
->params
.arp_interval
= 0;
1052 bond
->dev
->priv_flags
&= ~IFF_MASTER_ARPMON
;
1053 if (bond
->params
.arp_validate
) {
1054 bond_unregister_arp(bond
);
1055 bond
->params
.arp_validate
=
1056 BOND_ARP_VALIDATE_NONE
;
1058 if (delayed_work_pending(&bond
->arp_work
)) {
1059 cancel_delayed_work(&bond
->arp_work
);
1060 flush_workqueue(bond
->wq
);
1064 if (bond
->dev
->flags
& IFF_UP
) {
1065 /* If the interface is up, we may need to fire off
1066 * the MII timer. If the interface is down, the
1067 * timer will get fired off when the open function
1070 if (!delayed_work_pending(&bond
->mii_work
)) {
1071 INIT_DELAYED_WORK(&bond
->mii_work
,
1073 queue_delayed_work(bond
->wq
,
1074 &bond
->mii_work
, 0);
1081 static DEVICE_ATTR(miimon
, S_IRUGO
| S_IWUSR
,
1082 bonding_show_miimon
, bonding_store_miimon
);
1085 * Show and set the primary slave. The store function is much
1086 * simpler than bonding_store_slaves function because it only needs to
1087 * handle one interface name.
1088 * The bond must be a mode that supports a primary for this be
1091 static ssize_t
bonding_show_primary(struct device
*d
,
1092 struct device_attribute
*attr
,
1096 struct bonding
*bond
= to_bond(d
);
1098 if (bond
->primary_slave
)
1099 count
= sprintf(buf
, "%s\n", bond
->primary_slave
->dev
->name
);
1104 static ssize_t
bonding_store_primary(struct device
*d
,
1105 struct device_attribute
*attr
,
1106 const char *buf
, size_t count
)
1109 struct slave
*slave
;
1110 struct bonding
*bond
= to_bond(d
);
1112 if (!rtnl_trylock())
1113 return restart_syscall();
1114 read_lock(&bond
->lock
);
1115 write_lock_bh(&bond
->curr_slave_lock
);
1117 if (!USES_PRIMARY(bond
->params
.mode
)) {
1118 pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1119 bond
->dev
->name
, bond
->dev
->name
, bond
->params
.mode
);
1121 bond_for_each_slave(bond
, slave
, i
) {
1123 (slave
->dev
->name
, buf
,
1124 strlen(slave
->dev
->name
)) == 0) {
1125 pr_info("%s: Setting %s as primary slave.\n",
1126 bond
->dev
->name
, slave
->dev
->name
);
1127 bond
->primary_slave
= slave
;
1128 strcpy(bond
->params
.primary
, slave
->dev
->name
);
1129 bond_select_active_slave(bond
);
1134 /* if we got here, then we didn't match the name of any slave */
1136 if (strlen(buf
) == 0 || buf
[0] == '\n') {
1137 pr_info("%s: Setting primary slave to None.\n",
1139 bond
->primary_slave
= NULL
;
1140 bond_select_active_slave(bond
);
1142 pr_info("%s: Unable to set %.*s as primary slave as it is not a slave.\n",
1143 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
1147 write_unlock_bh(&bond
->curr_slave_lock
);
1148 read_unlock(&bond
->lock
);
1153 static DEVICE_ATTR(primary
, S_IRUGO
| S_IWUSR
,
1154 bonding_show_primary
, bonding_store_primary
);
1157 * Show and set the primary_reselect flag.
1159 static ssize_t
bonding_show_primary_reselect(struct device
*d
,
1160 struct device_attribute
*attr
,
1163 struct bonding
*bond
= to_bond(d
);
1165 return sprintf(buf
, "%s %d\n",
1166 pri_reselect_tbl
[bond
->params
.primary_reselect
].modename
,
1167 bond
->params
.primary_reselect
);
1170 static ssize_t
bonding_store_primary_reselect(struct device
*d
,
1171 struct device_attribute
*attr
,
1172 const char *buf
, size_t count
)
1174 int new_value
, ret
= count
;
1175 struct bonding
*bond
= to_bond(d
);
1177 if (!rtnl_trylock())
1178 return restart_syscall();
1180 new_value
= bond_parse_parm(buf
, pri_reselect_tbl
);
1181 if (new_value
< 0) {
1182 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1184 (int) strlen(buf
) - 1, buf
);
1189 bond
->params
.primary_reselect
= new_value
;
1190 pr_info("%s: setting primary_reselect to %s (%d).\n",
1191 bond
->dev
->name
, pri_reselect_tbl
[new_value
].modename
,
1194 read_lock(&bond
->lock
);
1195 write_lock_bh(&bond
->curr_slave_lock
);
1196 bond_select_active_slave(bond
);
1197 write_unlock_bh(&bond
->curr_slave_lock
);
1198 read_unlock(&bond
->lock
);
1203 static DEVICE_ATTR(primary_reselect
, S_IRUGO
| S_IWUSR
,
1204 bonding_show_primary_reselect
,
1205 bonding_store_primary_reselect
);
1208 * Show and set the use_carrier flag.
1210 static ssize_t
bonding_show_carrier(struct device
*d
,
1211 struct device_attribute
*attr
,
1214 struct bonding
*bond
= to_bond(d
);
1216 return sprintf(buf
, "%d\n", bond
->params
.use_carrier
);
1219 static ssize_t
bonding_store_carrier(struct device
*d
,
1220 struct device_attribute
*attr
,
1221 const char *buf
, size_t count
)
1223 int new_value
, ret
= count
;
1224 struct bonding
*bond
= to_bond(d
);
1227 if (sscanf(buf
, "%d", &new_value
) != 1) {
1228 pr_err("%s: no use_carrier value specified.\n",
1233 if ((new_value
== 0) || (new_value
== 1)) {
1234 bond
->params
.use_carrier
= new_value
;
1235 pr_info("%s: Setting use_carrier to %d.\n",
1236 bond
->dev
->name
, new_value
);
1238 pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1239 bond
->dev
->name
, new_value
);
1244 static DEVICE_ATTR(use_carrier
, S_IRUGO
| S_IWUSR
,
1245 bonding_show_carrier
, bonding_store_carrier
);
1249 * Show and set currently active_slave.
1251 static ssize_t
bonding_show_active_slave(struct device
*d
,
1252 struct device_attribute
*attr
,
1256 struct bonding
*bond
= to_bond(d
);
1259 read_lock(&bond
->curr_slave_lock
);
1260 curr
= bond
->curr_active_slave
;
1261 read_unlock(&bond
->curr_slave_lock
);
1263 if (USES_PRIMARY(bond
->params
.mode
) && curr
)
1264 count
= sprintf(buf
, "%s\n", curr
->dev
->name
);
1268 static ssize_t
bonding_store_active_slave(struct device
*d
,
1269 struct device_attribute
*attr
,
1270 const char *buf
, size_t count
)
1273 struct slave
*slave
;
1274 struct slave
*old_active
= NULL
;
1275 struct slave
*new_active
= NULL
;
1276 struct bonding
*bond
= to_bond(d
);
1278 if (!rtnl_trylock())
1279 return restart_syscall();
1280 read_lock(&bond
->lock
);
1281 write_lock_bh(&bond
->curr_slave_lock
);
1283 if (!USES_PRIMARY(bond
->params
.mode
))
1284 pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1285 bond
->dev
->name
, bond
->dev
->name
, bond
->params
.mode
);
1287 bond_for_each_slave(bond
, slave
, i
) {
1289 (slave
->dev
->name
, buf
,
1290 strlen(slave
->dev
->name
)) == 0) {
1291 old_active
= bond
->curr_active_slave
;
1293 if (new_active
== old_active
) {
1295 pr_info("%s: %s is already the current active slave.\n",
1303 (new_active
->link
== BOND_LINK_UP
) &&
1304 IS_UP(new_active
->dev
)) {
1305 pr_info("%s: Setting %s as active slave.\n",
1308 bond_change_active_slave(bond
, new_active
);
1311 pr_info("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
1321 /* if we got here, then we didn't match the name of any slave */
1323 if (strlen(buf
) == 0 || buf
[0] == '\n') {
1324 pr_info("%s: Setting active slave to None.\n",
1326 bond
->primary_slave
= NULL
;
1327 bond_select_active_slave(bond
);
1329 pr_info("%s: Unable to set %.*s as active slave as it is not a slave.\n",
1330 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
1334 write_unlock_bh(&bond
->curr_slave_lock
);
1335 read_unlock(&bond
->lock
);
1341 static DEVICE_ATTR(active_slave
, S_IRUGO
| S_IWUSR
,
1342 bonding_show_active_slave
, bonding_store_active_slave
);
1346 * Show link status of the bond interface.
1348 static ssize_t
bonding_show_mii_status(struct device
*d
,
1349 struct device_attribute
*attr
,
1353 struct bonding
*bond
= to_bond(d
);
1355 read_lock(&bond
->curr_slave_lock
);
1356 curr
= bond
->curr_active_slave
;
1357 read_unlock(&bond
->curr_slave_lock
);
1359 return sprintf(buf
, "%s\n", curr
? "up" : "down");
1361 static DEVICE_ATTR(mii_status
, S_IRUGO
, bonding_show_mii_status
, NULL
);
1365 * Show current 802.3ad aggregator ID.
1367 static ssize_t
bonding_show_ad_aggregator(struct device
*d
,
1368 struct device_attribute
*attr
,
1372 struct bonding
*bond
= to_bond(d
);
1374 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1375 struct ad_info ad_info
;
1376 count
= sprintf(buf
, "%d\n",
1377 (bond_3ad_get_active_agg_info(bond
, &ad_info
))
1378 ? 0 : ad_info
.aggregator_id
);
1383 static DEVICE_ATTR(ad_aggregator
, S_IRUGO
, bonding_show_ad_aggregator
, NULL
);
1387 * Show number of active 802.3ad ports.
1389 static ssize_t
bonding_show_ad_num_ports(struct device
*d
,
1390 struct device_attribute
*attr
,
1394 struct bonding
*bond
= to_bond(d
);
1396 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1397 struct ad_info ad_info
;
1398 count
= sprintf(buf
, "%d\n",
1399 (bond_3ad_get_active_agg_info(bond
, &ad_info
))
1400 ? 0 : ad_info
.ports
);
1405 static DEVICE_ATTR(ad_num_ports
, S_IRUGO
, bonding_show_ad_num_ports
, NULL
);
1409 * Show current 802.3ad actor key.
1411 static ssize_t
bonding_show_ad_actor_key(struct device
*d
,
1412 struct device_attribute
*attr
,
1416 struct bonding
*bond
= to_bond(d
);
1418 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1419 struct ad_info ad_info
;
1420 count
= sprintf(buf
, "%d\n",
1421 (bond_3ad_get_active_agg_info(bond
, &ad_info
))
1422 ? 0 : ad_info
.actor_key
);
1427 static DEVICE_ATTR(ad_actor_key
, S_IRUGO
, bonding_show_ad_actor_key
, NULL
);
1431 * Show current 802.3ad partner key.
1433 static ssize_t
bonding_show_ad_partner_key(struct device
*d
,
1434 struct device_attribute
*attr
,
1438 struct bonding
*bond
= to_bond(d
);
1440 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1441 struct ad_info ad_info
;
1442 count
= sprintf(buf
, "%d\n",
1443 (bond_3ad_get_active_agg_info(bond
, &ad_info
))
1444 ? 0 : ad_info
.partner_key
);
1449 static DEVICE_ATTR(ad_partner_key
, S_IRUGO
, bonding_show_ad_partner_key
, NULL
);
1453 * Show current 802.3ad partner mac.
1455 static ssize_t
bonding_show_ad_partner_mac(struct device
*d
,
1456 struct device_attribute
*attr
,
1460 struct bonding
*bond
= to_bond(d
);
1462 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1463 struct ad_info ad_info
;
1464 if (!bond_3ad_get_active_agg_info(bond
, &ad_info
))
1465 count
= sprintf(buf
, "%pM\n", ad_info
.partner_system
);
1470 static DEVICE_ATTR(ad_partner_mac
, S_IRUGO
, bonding_show_ad_partner_mac
, NULL
);
1474 static struct attribute
*per_bond_attrs
[] = {
1475 &dev_attr_slaves
.attr
,
1476 &dev_attr_mode
.attr
,
1477 &dev_attr_fail_over_mac
.attr
,
1478 &dev_attr_arp_validate
.attr
,
1479 &dev_attr_arp_interval
.attr
,
1480 &dev_attr_arp_ip_target
.attr
,
1481 &dev_attr_downdelay
.attr
,
1482 &dev_attr_updelay
.attr
,
1483 &dev_attr_lacp_rate
.attr
,
1484 &dev_attr_ad_select
.attr
,
1485 &dev_attr_xmit_hash_policy
.attr
,
1486 &dev_attr_num_grat_arp
.attr
,
1487 &dev_attr_num_unsol_na
.attr
,
1488 &dev_attr_miimon
.attr
,
1489 &dev_attr_primary
.attr
,
1490 &dev_attr_primary_reselect
.attr
,
1491 &dev_attr_use_carrier
.attr
,
1492 &dev_attr_active_slave
.attr
,
1493 &dev_attr_mii_status
.attr
,
1494 &dev_attr_ad_aggregator
.attr
,
1495 &dev_attr_ad_num_ports
.attr
,
1496 &dev_attr_ad_actor_key
.attr
,
1497 &dev_attr_ad_partner_key
.attr
,
1498 &dev_attr_ad_partner_mac
.attr
,
1502 static struct attribute_group bonding_group
= {
1504 .attrs
= per_bond_attrs
,
1508 * Initialize sysfs. This sets up the bonding_masters file in
1511 int bond_create_sysfs(void)
1515 ret
= netdev_class_create_file(&class_attr_bonding_masters
);
1517 * Permit multiple loads of the module by ignoring failures to
1518 * create the bonding_masters sysfs file. Bonding devices
1519 * created by second or subsequent loads of the module will
1520 * not be listed in, or controllable by, bonding_masters, but
1521 * will have the usual "bonding" sysfs directory.
1523 * This is done to preserve backwards compatibility for
1524 * initscripts/sysconfig, which load bonding multiple times to
1525 * configure multiple bonding devices.
1527 if (ret
== -EEXIST
) {
1528 /* Is someone being kinky and naming a device bonding_master? */
1529 if (__dev_get_by_name(&init_net
,
1530 class_attr_bonding_masters
.attr
.name
))
1531 pr_err("network device named %s already exists in sysfs",
1532 class_attr_bonding_masters
.attr
.name
);
1541 * Remove /sys/class/net/bonding_masters.
1543 void bond_destroy_sysfs(void)
1545 netdev_class_remove_file(&class_attr_bonding_masters
);
1549 * Initialize sysfs for each bond. This sets up and registers
1550 * the 'bondctl' directory for each individual bond under /sys/class/net.
1552 void bond_prepare_sysfs_group(struct bonding
*bond
)
1554 bond
->dev
->sysfs_groups
[0] = &bonding_group
;