3 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/sysdev.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
33 #include <linux/sysfs.h>
34 #include <linux/ctype.h>
35 #include <linux/inet.h>
36 #include <linux/rtnetlink.h>
37 #include <net/net_namespace.h>
39 /* #define BONDING_DEBUG 1 */
41 #define to_dev(obj) container_of(obj,struct device,kobj)
42 #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
44 /*---------------------------- Declarations -------------------------------*/
47 extern struct list_head bond_dev_list
;
48 extern struct bond_params bonding_defaults
;
49 extern struct bond_parm_tbl bond_mode_tbl
[];
50 extern struct bond_parm_tbl bond_lacp_tbl
[];
51 extern struct bond_parm_tbl xmit_hashtype_tbl
[];
52 extern struct bond_parm_tbl arp_validate_tbl
[];
54 static int expected_refcount
= -1;
55 static struct class *netdev_class
;
56 /*--------------------------- Data Structures -----------------------------*/
58 /* Bonding sysfs lock. Why can't we just use the subsytem lock?
59 * Because kobject_register tries to acquire the subsystem lock. If
60 * we already hold the lock (which we would if the user was creating
61 * a new bond through the sysfs interface), we deadlock.
62 * This lock is only needed when deleting a bond - we need to make sure
63 * that we don't collide with an ongoing ioctl.
66 struct rw_semaphore bonding_rwsem
;
71 /*------------------------------ Functions --------------------------------*/
74 * "show" function for the bond_masters attribute.
75 * The class parameter is ignored.
77 static ssize_t
bonding_show_bonds(struct class *cls
, char *buffer
)
82 down_read(&(bonding_rwsem
));
84 list_for_each_entry(bond
, &bond_dev_list
, bond_list
) {
85 if (res
> (PAGE_SIZE
- IFNAMSIZ
)) {
86 /* not enough space for another interface name */
87 if ((PAGE_SIZE
- res
) > 10)
89 res
+= sprintf(buffer
+ res
, "++more++");
92 res
+= sprintf(buffer
+ res
, "%s ",
95 res
+= sprintf(buffer
+ res
, "\n");
97 up_read(&(bonding_rwsem
));
102 * "store" function for the bond_masters attribute. This is what
103 * creates and deletes entire bonds.
105 * The class parameter is ignored.
109 static ssize_t
bonding_store_bonds(struct class *cls
, const char *buffer
, size_t count
)
111 char command
[IFNAMSIZ
+ 1] = {0, };
114 struct bonding
*bond
;
117 down_write(&(bonding_rwsem
));
118 sscanf(buffer
, "%16s", command
); /* IFNAMSIZ*/
119 ifname
= command
+ 1;
120 if ((strlen(command
) <= 1) ||
121 !dev_valid_name(ifname
))
124 if (command
[0] == '+') {
126 /* Check to see if the bond already exists. */
127 list_for_each_entry_safe(bond
, nxt
, &bond_dev_list
, bond_list
)
128 if (strnicmp(bond
->dev
->name
, ifname
, IFNAMSIZ
) == 0) {
129 printk(KERN_ERR DRV_NAME
130 ": cannot add bond %s; it already exists\n",
136 printk(KERN_INFO DRV_NAME
137 ": %s is being created...\n", ifname
);
138 if (bond_create(ifname
, &bonding_defaults
, &bond
)) {
139 printk(KERN_INFO DRV_NAME
140 ": %s interface already exists. Bond creation failed.\n",
147 if (command
[0] == '-') {
148 list_for_each_entry_safe(bond
, nxt
, &bond_dev_list
, bond_list
)
149 if (strnicmp(bond
->dev
->name
, ifname
, IFNAMSIZ
) == 0) {
151 /* check the ref count on the bond's kobject.
152 * If it's > expected, then there's a file open,
153 * and we have to fail.
155 if (atomic_read(&bond
->dev
->dev
.kobj
.kref
.refcount
)
156 > expected_refcount
){
158 printk(KERN_INFO DRV_NAME
159 ": Unable remove bond %s due to open references.\n",
164 printk(KERN_INFO DRV_NAME
165 ": %s is being deleted...\n",
172 printk(KERN_ERR DRV_NAME
173 ": unable to delete non-existent bond %s\n", ifname
);
179 printk(KERN_ERR DRV_NAME
180 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
183 /* Always return either count or an error. If you return 0, you'll
184 * get called forever, which is bad.
187 up_write(&(bonding_rwsem
));
190 /* class attribute for bond_masters file. This ends up in /sys/class/net */
191 static CLASS_ATTR(bonding_masters
, S_IWUSR
| S_IRUGO
,
192 bonding_show_bonds
, bonding_store_bonds
);
194 int bond_create_slave_symlinks(struct net_device
*master
, struct net_device
*slave
)
196 char linkname
[IFNAMSIZ
+7];
199 /* first, create a link from the slave back to the master */
200 ret
= sysfs_create_link(&(slave
->dev
.kobj
), &(master
->dev
.kobj
),
204 /* next, create a link from the master to the slave */
205 sprintf(linkname
,"slave_%s",slave
->name
);
206 ret
= sysfs_create_link(&(master
->dev
.kobj
), &(slave
->dev
.kobj
),
212 void bond_destroy_slave_symlinks(struct net_device
*master
, struct net_device
*slave
)
214 char linkname
[IFNAMSIZ
+7];
216 sysfs_remove_link(&(slave
->dev
.kobj
), "master");
217 sprintf(linkname
,"slave_%s",slave
->name
);
218 sysfs_remove_link(&(master
->dev
.kobj
), linkname
);
223 * Show the slaves in the current bond.
225 static ssize_t
bonding_show_slaves(struct device
*d
,
226 struct device_attribute
*attr
, char *buf
)
230 struct bonding
*bond
= to_bond(d
);
232 read_lock_bh(&bond
->lock
);
233 bond_for_each_slave(bond
, slave
, i
) {
234 if (res
> (PAGE_SIZE
- IFNAMSIZ
)) {
235 /* not enough space for another interface name */
236 if ((PAGE_SIZE
- res
) > 10)
237 res
= PAGE_SIZE
- 10;
238 res
+= sprintf(buf
+ res
, "++more++");
241 res
+= sprintf(buf
+ res
, "%s ", slave
->dev
->name
);
243 read_unlock_bh(&bond
->lock
);
244 res
+= sprintf(buf
+ res
, "\n");
250 * Set the slaves in the current bond. The bond interface must be
251 * up for this to succeed.
252 * This function is largely the same flow as bonding_update_bonds().
254 static ssize_t
bonding_store_slaves(struct device
*d
,
255 struct device_attribute
*attr
,
256 const char *buffer
, size_t count
)
258 char command
[IFNAMSIZ
+ 1] = { 0, };
260 int i
, res
, found
, ret
= count
;
263 struct net_device
*dev
= NULL
;
264 struct bonding
*bond
= to_bond(d
);
266 /* Quick sanity check -- is the bond interface up? */
267 if (!(bond
->dev
->flags
& IFF_UP
)) {
268 printk(KERN_WARNING DRV_NAME
269 ": %s: doing slave updates when interface is down.\n",
273 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
275 sscanf(buffer
, "%16s", command
); /* IFNAMSIZ*/
276 ifname
= command
+ 1;
277 if ((strlen(command
) <= 1) ||
278 !dev_valid_name(ifname
))
281 if (command
[0] == '+') {
283 /* Got a slave name in ifname. Is it already in the list? */
285 read_lock_bh(&bond
->lock
);
286 bond_for_each_slave(bond
, slave
, i
)
287 if (strnicmp(slave
->dev
->name
, ifname
, IFNAMSIZ
) == 0) {
288 printk(KERN_ERR DRV_NAME
289 ": %s: Interface %s is already enslaved!\n",
290 bond
->dev
->name
, ifname
);
292 read_unlock_bh(&bond
->lock
);
296 read_unlock_bh(&bond
->lock
);
297 printk(KERN_INFO DRV_NAME
": %s: Adding slave %s.\n",
298 bond
->dev
->name
, ifname
);
299 dev
= dev_get_by_name(&init_net
, ifname
);
301 printk(KERN_INFO DRV_NAME
302 ": %s: Interface %s does not exist!\n",
303 bond
->dev
->name
, ifname
);
310 if (dev
->flags
& IFF_UP
) {
311 printk(KERN_ERR DRV_NAME
312 ": %s: Error: Unable to enslave %s "
313 "because it is already up.\n",
314 bond
->dev
->name
, dev
->name
);
318 /* If this is the first slave, then we need to set
319 the master's hardware address to be the same as the
321 if (!(*((u32
*) & (bond
->dev
->dev_addr
[0])))) {
322 memcpy(bond
->dev
->dev_addr
, dev
->dev_addr
,
326 /* Set the slave's MTU to match the bond */
327 original_mtu
= dev
->mtu
;
328 if (dev
->mtu
!= bond
->dev
->mtu
) {
329 if (dev
->change_mtu
) {
330 res
= dev
->change_mtu(dev
,
337 dev
->mtu
= bond
->dev
->mtu
;
341 res
= bond_enslave(bond
->dev
, dev
);
342 bond_for_each_slave(bond
, slave
, i
)
343 if (strnicmp(slave
->dev
->name
, ifname
, IFNAMSIZ
) == 0)
344 slave
->original_mtu
= original_mtu
;
352 if (command
[0] == '-') {
354 bond_for_each_slave(bond
, slave
, i
)
355 if (strnicmp(slave
->dev
->name
, ifname
, IFNAMSIZ
) == 0) {
357 original_mtu
= slave
->original_mtu
;
361 printk(KERN_INFO DRV_NAME
": %s: Removing slave %s\n",
362 bond
->dev
->name
, dev
->name
);
364 if (bond
->setup_by_slave
)
365 res
= bond_release_and_destroy(bond
->dev
, dev
);
367 res
= bond_release(bond
->dev
, dev
);
373 /* set the slave MTU to the default */
374 if (dev
->change_mtu
) {
375 dev
->change_mtu(dev
, original_mtu
);
377 dev
->mtu
= original_mtu
;
381 printk(KERN_ERR DRV_NAME
": unable to remove non-existent slave %s for bond %s.\n",
382 ifname
, bond
->dev
->name
);
389 printk(KERN_ERR DRV_NAME
": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond
->dev
->name
);
396 static DEVICE_ATTR(slaves
, S_IRUGO
| S_IWUSR
, bonding_show_slaves
, bonding_store_slaves
);
399 * Show and set the bonding mode. The bond interface must be down to
402 static ssize_t
bonding_show_mode(struct device
*d
,
403 struct device_attribute
*attr
, char *buf
)
405 struct bonding
*bond
= to_bond(d
);
407 return sprintf(buf
, "%s %d\n",
408 bond_mode_tbl
[bond
->params
.mode
].modename
,
409 bond
->params
.mode
) + 1;
412 static ssize_t
bonding_store_mode(struct device
*d
,
413 struct device_attribute
*attr
,
414 const char *buf
, size_t count
)
416 int new_value
, ret
= count
;
417 struct bonding
*bond
= to_bond(d
);
419 if (bond
->dev
->flags
& IFF_UP
) {
420 printk(KERN_ERR DRV_NAME
421 ": unable to update mode of %s because interface is up.\n",
427 new_value
= bond_parse_parm((char *)buf
, bond_mode_tbl
);
429 printk(KERN_ERR DRV_NAME
430 ": %s: Ignoring invalid mode value %.*s.\n",
432 (int)strlen(buf
) - 1, buf
);
436 if (bond
->params
.mode
== BOND_MODE_8023AD
)
437 bond_unset_master_3ad_flags(bond
);
439 if (bond
->params
.mode
== BOND_MODE_ALB
)
440 bond_unset_master_alb_flags(bond
);
442 bond
->params
.mode
= new_value
;
443 bond_set_mode_ops(bond
, bond
->params
.mode
);
444 printk(KERN_INFO DRV_NAME
": %s: setting mode to %s (%d).\n",
445 bond
->dev
->name
, bond_mode_tbl
[new_value
].modename
, new_value
);
450 static DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
, bonding_show_mode
, bonding_store_mode
);
453 * Show and set the bonding transmit hash method. The bond interface must be down to
454 * change the xmit hash policy.
456 static ssize_t
bonding_show_xmit_hash(struct device
*d
,
457 struct device_attribute
*attr
,
461 struct bonding
*bond
= to_bond(d
);
463 if ((bond
->params
.mode
!= BOND_MODE_XOR
) &&
464 (bond
->params
.mode
!= BOND_MODE_8023AD
)) {
466 count
= sprintf(buf
, "NA\n") + 1;
468 count
= sprintf(buf
, "%s %d\n",
469 xmit_hashtype_tbl
[bond
->params
.xmit_policy
].modename
,
470 bond
->params
.xmit_policy
) + 1;
476 static ssize_t
bonding_store_xmit_hash(struct device
*d
,
477 struct device_attribute
*attr
,
478 const char *buf
, size_t count
)
480 int new_value
, ret
= count
;
481 struct bonding
*bond
= to_bond(d
);
483 if (bond
->dev
->flags
& IFF_UP
) {
484 printk(KERN_ERR DRV_NAME
485 "%s: Interface is up. Unable to update xmit policy.\n",
491 if ((bond
->params
.mode
!= BOND_MODE_XOR
) &&
492 (bond
->params
.mode
!= BOND_MODE_8023AD
)) {
493 printk(KERN_ERR DRV_NAME
494 "%s: Transmit hash policy is irrelevant in this mode.\n",
500 new_value
= bond_parse_parm((char *)buf
, xmit_hashtype_tbl
);
502 printk(KERN_ERR DRV_NAME
503 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
505 (int)strlen(buf
) - 1, buf
);
509 bond
->params
.xmit_policy
= new_value
;
510 bond_set_mode_ops(bond
, bond
->params
.mode
);
511 printk(KERN_INFO DRV_NAME
": %s: setting xmit hash policy to %s (%d).\n",
512 bond
->dev
->name
, xmit_hashtype_tbl
[new_value
].modename
, new_value
);
517 static DEVICE_ATTR(xmit_hash_policy
, S_IRUGO
| S_IWUSR
, bonding_show_xmit_hash
, bonding_store_xmit_hash
);
520 * Show and set arp_validate.
522 static ssize_t
bonding_show_arp_validate(struct device
*d
,
523 struct device_attribute
*attr
,
526 struct bonding
*bond
= to_bond(d
);
528 return sprintf(buf
, "%s %d\n",
529 arp_validate_tbl
[bond
->params
.arp_validate
].modename
,
530 bond
->params
.arp_validate
) + 1;
533 static ssize_t
bonding_store_arp_validate(struct device
*d
,
534 struct device_attribute
*attr
,
535 const char *buf
, size_t count
)
538 struct bonding
*bond
= to_bond(d
);
540 new_value
= bond_parse_parm((char *)buf
, arp_validate_tbl
);
542 printk(KERN_ERR DRV_NAME
543 ": %s: Ignoring invalid arp_validate value %s\n",
544 bond
->dev
->name
, buf
);
547 if (new_value
&& (bond
->params
.mode
!= BOND_MODE_ACTIVEBACKUP
)) {
548 printk(KERN_ERR DRV_NAME
549 ": %s: arp_validate only supported in active-backup mode.\n",
553 printk(KERN_INFO DRV_NAME
": %s: setting arp_validate to %s (%d).\n",
554 bond
->dev
->name
, arp_validate_tbl
[new_value
].modename
,
557 if (!bond
->params
.arp_validate
&& new_value
) {
558 bond_register_arp(bond
);
559 } else if (bond
->params
.arp_validate
&& !new_value
) {
560 bond_unregister_arp(bond
);
563 bond
->params
.arp_validate
= new_value
;
568 static DEVICE_ATTR(arp_validate
, S_IRUGO
| S_IWUSR
, bonding_show_arp_validate
, bonding_store_arp_validate
);
571 * Show and store fail_over_mac. User only allowed to change the
572 * value when there are no slaves.
574 static ssize_t
bonding_show_fail_over_mac(struct device
*d
, struct device_attribute
*attr
, char *buf
)
576 struct bonding
*bond
= to_bond(d
);
578 return sprintf(buf
, "%d\n", bond
->params
.fail_over_mac
) + 1;
581 static ssize_t
bonding_store_fail_over_mac(struct device
*d
, struct device_attribute
*attr
, const char *buf
, size_t count
)
585 struct bonding
*bond
= to_bond(d
);
587 if (bond
->slave_cnt
!= 0) {
588 printk(KERN_ERR DRV_NAME
589 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
595 if (sscanf(buf
, "%d", &new_value
) != 1) {
596 printk(KERN_ERR DRV_NAME
597 ": %s: no fail_over_mac value specified.\n",
603 if ((new_value
== 0) || (new_value
== 1)) {
604 bond
->params
.fail_over_mac
= new_value
;
605 printk(KERN_INFO DRV_NAME
": %s: Setting fail_over_mac to %d.\n",
606 bond
->dev
->name
, new_value
);
608 printk(KERN_INFO DRV_NAME
609 ": %s: Ignoring invalid fail_over_mac value %d.\n",
610 bond
->dev
->name
, new_value
);
616 static DEVICE_ATTR(fail_over_mac
, S_IRUGO
| S_IWUSR
, bonding_show_fail_over_mac
, bonding_store_fail_over_mac
);
619 * Show and set the arp timer interval. There are two tricky bits
620 * here. First, if ARP monitoring is activated, then we must disable
621 * MII monitoring. Second, if the ARP timer isn't running, we must
624 static ssize_t
bonding_show_arp_interval(struct device
*d
,
625 struct device_attribute
*attr
,
628 struct bonding
*bond
= to_bond(d
);
630 return sprintf(buf
, "%d\n", bond
->params
.arp_interval
) + 1;
633 static ssize_t
bonding_store_arp_interval(struct device
*d
,
634 struct device_attribute
*attr
,
635 const char *buf
, size_t count
)
637 int new_value
, ret
= count
;
638 struct bonding
*bond
= to_bond(d
);
640 if (sscanf(buf
, "%d", &new_value
) != 1) {
641 printk(KERN_ERR DRV_NAME
642 ": %s: no arp_interval value specified.\n",
648 printk(KERN_ERR DRV_NAME
649 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
650 bond
->dev
->name
, new_value
, INT_MAX
);
655 printk(KERN_INFO DRV_NAME
656 ": %s: Setting ARP monitoring interval to %d.\n",
657 bond
->dev
->name
, new_value
);
658 bond
->params
.arp_interval
= new_value
;
659 if (bond
->params
.miimon
) {
660 printk(KERN_INFO DRV_NAME
661 ": %s: ARP monitoring cannot be used with MII monitoring. "
662 "%s Disabling MII monitoring.\n",
663 bond
->dev
->name
, bond
->dev
->name
);
664 bond
->params
.miimon
= 0;
665 /* Kill MII timer, else it brings bond's link down */
666 if (bond
->arp_timer
.function
) {
667 printk(KERN_INFO DRV_NAME
668 ": %s: Kill MII timer, else it brings bond's link down...\n",
670 del_timer_sync(&bond
->mii_timer
);
673 if (!bond
->params
.arp_targets
[0]) {
674 printk(KERN_INFO DRV_NAME
675 ": %s: ARP monitoring has been set up, "
676 "but no ARP targets have been specified.\n",
679 if (bond
->dev
->flags
& IFF_UP
) {
680 /* If the interface is up, we may need to fire off
681 * the ARP timer. If the interface is down, the
682 * timer will get fired off when the open function
685 if (bond
->arp_timer
.function
) {
686 /* The timer's already set up, so fire it off */
687 mod_timer(&bond
->arp_timer
, jiffies
+ 1);
689 /* Set up the timer. */
690 init_timer(&bond
->arp_timer
);
691 bond
->arp_timer
.expires
= jiffies
+ 1;
692 bond
->arp_timer
.data
=
693 (unsigned long) bond
->dev
;
694 if (bond
->params
.mode
== BOND_MODE_ACTIVEBACKUP
) {
695 bond
->arp_timer
.function
=
697 &bond_activebackup_arp_mon
;
699 bond
->arp_timer
.function
=
701 &bond_loadbalance_arp_mon
;
703 add_timer(&bond
->arp_timer
);
710 static DEVICE_ATTR(arp_interval
, S_IRUGO
| S_IWUSR
, bonding_show_arp_interval
, bonding_store_arp_interval
);
713 * Show and set the arp targets.
715 static ssize_t
bonding_show_arp_targets(struct device
*d
,
716 struct device_attribute
*attr
,
720 struct bonding
*bond
= to_bond(d
);
722 for (i
= 0; i
< BOND_MAX_ARP_TARGETS
; i
++) {
723 if (bond
->params
.arp_targets
[i
])
724 res
+= sprintf(buf
+ res
, "%u.%u.%u.%u ",
725 NIPQUAD(bond
->params
.arp_targets
[i
]));
728 res
--; /* eat the leftover space */
729 res
+= sprintf(buf
+ res
, "\n");
734 static ssize_t
bonding_store_arp_targets(struct device
*d
,
735 struct device_attribute
*attr
,
736 const char *buf
, size_t count
)
739 int i
= 0, done
= 0, ret
= count
;
740 struct bonding
*bond
= to_bond(d
);
743 targets
= bond
->params
.arp_targets
;
744 newtarget
= in_aton(buf
+ 1);
747 if ((newtarget
== 0) || (newtarget
== htonl(INADDR_BROADCAST
))) {
748 printk(KERN_ERR DRV_NAME
749 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
750 bond
->dev
->name
, NIPQUAD(newtarget
));
754 /* look for an empty slot to put the target in, and check for dupes */
755 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
); i
++) {
756 if (targets
[i
] == newtarget
) { /* duplicate */
757 printk(KERN_ERR DRV_NAME
758 ": %s: ARP target %u.%u.%u.%u is already present\n",
759 bond
->dev
->name
, NIPQUAD(newtarget
));
765 if (targets
[i
] == 0 && !done
) {
766 printk(KERN_INFO DRV_NAME
767 ": %s: adding ARP target %d.%d.%d.%d.\n",
768 bond
->dev
->name
, NIPQUAD(newtarget
));
770 targets
[i
] = newtarget
;
774 printk(KERN_ERR DRV_NAME
775 ": %s: ARP target table is full!\n",
782 else if (buf
[0] == '-') {
783 if ((newtarget
== 0) || (newtarget
== htonl(INADDR_BROADCAST
))) {
784 printk(KERN_ERR DRV_NAME
785 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
786 bond
->dev
->name
, NIPQUAD(newtarget
));
791 for (i
= 0; (i
< BOND_MAX_ARP_TARGETS
); i
++) {
792 if (targets
[i
] == newtarget
) {
793 printk(KERN_INFO DRV_NAME
794 ": %s: removing ARP target %d.%d.%d.%d.\n",
795 bond
->dev
->name
, NIPQUAD(newtarget
));
801 printk(KERN_INFO DRV_NAME
802 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
803 bond
->dev
->name
, NIPQUAD(newtarget
));
809 printk(KERN_ERR DRV_NAME
": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
818 static DEVICE_ATTR(arp_ip_target
, S_IRUGO
| S_IWUSR
, bonding_show_arp_targets
, bonding_store_arp_targets
);
821 * Show and set the up and down delays. These must be multiples of the
822 * MII monitoring value, and are stored internally as the multiplier.
823 * Thus, we must translate to MS for the real world.
825 static ssize_t
bonding_show_downdelay(struct device
*d
,
826 struct device_attribute
*attr
,
829 struct bonding
*bond
= to_bond(d
);
831 return sprintf(buf
, "%d\n", bond
->params
.downdelay
* bond
->params
.miimon
) + 1;
834 static ssize_t
bonding_store_downdelay(struct device
*d
,
835 struct device_attribute
*attr
,
836 const char *buf
, size_t count
)
838 int new_value
, ret
= count
;
839 struct bonding
*bond
= to_bond(d
);
841 if (!(bond
->params
.miimon
)) {
842 printk(KERN_ERR DRV_NAME
843 ": %s: Unable to set down delay as MII monitoring is disabled\n",
849 if (sscanf(buf
, "%d", &new_value
) != 1) {
850 printk(KERN_ERR DRV_NAME
851 ": %s: no down delay value specified.\n",
857 printk(KERN_ERR DRV_NAME
858 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
859 bond
->dev
->name
, new_value
, 1, INT_MAX
);
863 if ((new_value
% bond
->params
.miimon
) != 0) {
864 printk(KERN_WARNING DRV_NAME
865 ": %s: Warning: down delay (%d) is not a multiple "
866 "of miimon (%d), delay rounded to %d ms\n",
867 bond
->dev
->name
, new_value
, bond
->params
.miimon
,
868 (new_value
/ bond
->params
.miimon
) *
869 bond
->params
.miimon
);
871 bond
->params
.downdelay
= new_value
/ bond
->params
.miimon
;
872 printk(KERN_INFO DRV_NAME
": %s: Setting down delay to %d.\n",
873 bond
->dev
->name
, bond
->params
.downdelay
* bond
->params
.miimon
);
880 static DEVICE_ATTR(downdelay
, S_IRUGO
| S_IWUSR
, bonding_show_downdelay
, bonding_store_downdelay
);
882 static ssize_t
bonding_show_updelay(struct device
*d
,
883 struct device_attribute
*attr
,
886 struct bonding
*bond
= to_bond(d
);
888 return sprintf(buf
, "%d\n", bond
->params
.updelay
* bond
->params
.miimon
) + 1;
892 static ssize_t
bonding_store_updelay(struct device
*d
,
893 struct device_attribute
*attr
,
894 const char *buf
, size_t count
)
896 int new_value
, ret
= count
;
897 struct bonding
*bond
= to_bond(d
);
899 if (!(bond
->params
.miimon
)) {
900 printk(KERN_ERR DRV_NAME
901 ": %s: Unable to set up delay as MII monitoring is disabled\n",
907 if (sscanf(buf
, "%d", &new_value
) != 1) {
908 printk(KERN_ERR DRV_NAME
909 ": %s: no up delay value specified.\n",
915 printk(KERN_ERR DRV_NAME
916 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
917 bond
->dev
->name
, new_value
, 1, INT_MAX
);
921 if ((new_value
% bond
->params
.miimon
) != 0) {
922 printk(KERN_WARNING DRV_NAME
923 ": %s: Warning: up delay (%d) is not a multiple "
924 "of miimon (%d), updelay rounded to %d ms\n",
925 bond
->dev
->name
, new_value
, bond
->params
.miimon
,
926 (new_value
/ bond
->params
.miimon
) *
927 bond
->params
.miimon
);
929 bond
->params
.updelay
= new_value
/ bond
->params
.miimon
;
930 printk(KERN_INFO DRV_NAME
": %s: Setting up delay to %d.\n",
931 bond
->dev
->name
, bond
->params
.updelay
* bond
->params
.miimon
);
938 static DEVICE_ATTR(updelay
, S_IRUGO
| S_IWUSR
, bonding_show_updelay
, bonding_store_updelay
);
941 * Show and set the LACP interval. Interface must be down, and the mode
942 * must be set to 802.3ad mode.
944 static ssize_t
bonding_show_lacp(struct device
*d
,
945 struct device_attribute
*attr
,
948 struct bonding
*bond
= to_bond(d
);
950 return sprintf(buf
, "%s %d\n",
951 bond_lacp_tbl
[bond
->params
.lacp_fast
].modename
,
952 bond
->params
.lacp_fast
) + 1;
955 static ssize_t
bonding_store_lacp(struct device
*d
,
956 struct device_attribute
*attr
,
957 const char *buf
, size_t count
)
959 int new_value
, ret
= count
;
960 struct bonding
*bond
= to_bond(d
);
962 if (bond
->dev
->flags
& IFF_UP
) {
963 printk(KERN_ERR DRV_NAME
964 ": %s: Unable to update LACP rate because interface is up.\n",
970 if (bond
->params
.mode
!= BOND_MODE_8023AD
) {
971 printk(KERN_ERR DRV_NAME
972 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
978 new_value
= bond_parse_parm((char *)buf
, bond_lacp_tbl
);
980 if ((new_value
== 1) || (new_value
== 0)) {
981 bond
->params
.lacp_fast
= new_value
;
982 printk(KERN_INFO DRV_NAME
983 ": %s: Setting LACP rate to %s (%d).\n",
984 bond
->dev
->name
, bond_lacp_tbl
[new_value
].modename
, new_value
);
986 printk(KERN_ERR DRV_NAME
987 ": %s: Ignoring invalid LACP rate value %.*s.\n",
988 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
994 static DEVICE_ATTR(lacp_rate
, S_IRUGO
| S_IWUSR
, bonding_show_lacp
, bonding_store_lacp
);
997 * Show and set the MII monitor interval. There are two tricky bits
998 * here. First, if MII monitoring is activated, then we must disable
999 * ARP monitoring. Second, if the timer isn't running, we must
1002 static ssize_t
bonding_show_miimon(struct device
*d
,
1003 struct device_attribute
*attr
,
1006 struct bonding
*bond
= to_bond(d
);
1008 return sprintf(buf
, "%d\n", bond
->params
.miimon
) + 1;
1011 static ssize_t
bonding_store_miimon(struct device
*d
,
1012 struct device_attribute
*attr
,
1013 const char *buf
, size_t count
)
1015 int new_value
, ret
= count
;
1016 struct bonding
*bond
= to_bond(d
);
1018 if (sscanf(buf
, "%d", &new_value
) != 1) {
1019 printk(KERN_ERR DRV_NAME
1020 ": %s: no miimon value specified.\n",
1025 if (new_value
< 0) {
1026 printk(KERN_ERR DRV_NAME
1027 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1028 bond
->dev
->name
, new_value
, 1, INT_MAX
);
1032 printk(KERN_INFO DRV_NAME
1033 ": %s: Setting MII monitoring interval to %d.\n",
1034 bond
->dev
->name
, new_value
);
1035 bond
->params
.miimon
= new_value
;
1036 if(bond
->params
.updelay
)
1037 printk(KERN_INFO DRV_NAME
1038 ": %s: Note: Updating updelay (to %d) "
1039 "since it is a multiple of the miimon value.\n",
1041 bond
->params
.updelay
* bond
->params
.miimon
);
1042 if(bond
->params
.downdelay
)
1043 printk(KERN_INFO DRV_NAME
1044 ": %s: Note: Updating downdelay (to %d) "
1045 "since it is a multiple of the miimon value.\n",
1047 bond
->params
.downdelay
* bond
->params
.miimon
);
1048 if (bond
->params
.arp_interval
) {
1049 printk(KERN_INFO DRV_NAME
1050 ": %s: MII monitoring cannot be used with "
1051 "ARP monitoring. Disabling ARP monitoring...\n",
1053 bond
->params
.arp_interval
= 0;
1054 if (bond
->params
.arp_validate
) {
1055 bond_unregister_arp(bond
);
1056 bond
->params
.arp_validate
=
1057 BOND_ARP_VALIDATE_NONE
;
1059 /* Kill ARP timer, else it brings bond's link down */
1060 if (bond
->mii_timer
.function
) {
1061 printk(KERN_INFO DRV_NAME
1062 ": %s: Kill ARP timer, else it brings bond's link down...\n",
1064 del_timer_sync(&bond
->arp_timer
);
1068 if (bond
->dev
->flags
& IFF_UP
) {
1069 /* If the interface is up, we may need to fire off
1070 * the MII timer. If the interface is down, the
1071 * timer will get fired off when the open function
1074 if (bond
->mii_timer
.function
) {
1075 /* The timer's already set up, so fire it off */
1076 mod_timer(&bond
->mii_timer
, jiffies
+ 1);
1078 /* Set up the timer. */
1079 init_timer(&bond
->mii_timer
);
1080 bond
->mii_timer
.expires
= jiffies
+ 1;
1081 bond
->mii_timer
.data
=
1082 (unsigned long) bond
->dev
;
1083 bond
->mii_timer
.function
=
1084 (void *) &bond_mii_monitor
;
1085 add_timer(&bond
->mii_timer
);
1092 static DEVICE_ATTR(miimon
, S_IRUGO
| S_IWUSR
, bonding_show_miimon
, bonding_store_miimon
);
1095 * Show and set the primary slave. The store function is much
1096 * simpler than bonding_store_slaves function because it only needs to
1097 * handle one interface name.
1098 * The bond must be a mode that supports a primary for this be
1101 static ssize_t
bonding_show_primary(struct device
*d
,
1102 struct device_attribute
*attr
,
1106 struct bonding
*bond
= to_bond(d
);
1108 if (bond
->primary_slave
)
1109 count
= sprintf(buf
, "%s\n", bond
->primary_slave
->dev
->name
) + 1;
1111 count
= sprintf(buf
, "\n") + 1;
1116 static ssize_t
bonding_store_primary(struct device
*d
,
1117 struct device_attribute
*attr
,
1118 const char *buf
, size_t count
)
1121 struct slave
*slave
;
1122 struct bonding
*bond
= to_bond(d
);
1124 write_lock_bh(&bond
->lock
);
1125 if (!USES_PRIMARY(bond
->params
.mode
)) {
1126 printk(KERN_INFO DRV_NAME
1127 ": %s: Unable to set primary slave; %s is in mode %d\n",
1128 bond
->dev
->name
, bond
->dev
->name
, bond
->params
.mode
);
1130 bond_for_each_slave(bond
, slave
, i
) {
1132 (slave
->dev
->name
, buf
,
1133 strlen(slave
->dev
->name
)) == 0) {
1134 printk(KERN_INFO DRV_NAME
1135 ": %s: Setting %s as primary slave.\n",
1136 bond
->dev
->name
, slave
->dev
->name
);
1137 bond
->primary_slave
= slave
;
1138 bond_select_active_slave(bond
);
1143 /* if we got here, then we didn't match the name of any slave */
1145 if (strlen(buf
) == 0 || buf
[0] == '\n') {
1146 printk(KERN_INFO DRV_NAME
1147 ": %s: Setting primary slave to None.\n",
1149 bond
->primary_slave
= NULL
;
1150 bond_select_active_slave(bond
);
1152 printk(KERN_INFO DRV_NAME
1153 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1154 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
1158 write_unlock_bh(&bond
->lock
);
1161 static DEVICE_ATTR(primary
, S_IRUGO
| S_IWUSR
, bonding_show_primary
, bonding_store_primary
);
1164 * Show and set the use_carrier flag.
1166 static ssize_t
bonding_show_carrier(struct device
*d
,
1167 struct device_attribute
*attr
,
1170 struct bonding
*bond
= to_bond(d
);
1172 return sprintf(buf
, "%d\n", bond
->params
.use_carrier
) + 1;
1175 static ssize_t
bonding_store_carrier(struct device
*d
,
1176 struct device_attribute
*attr
,
1177 const char *buf
, size_t count
)
1179 int new_value
, ret
= count
;
1180 struct bonding
*bond
= to_bond(d
);
1183 if (sscanf(buf
, "%d", &new_value
) != 1) {
1184 printk(KERN_ERR DRV_NAME
1185 ": %s: no use_carrier value specified.\n",
1190 if ((new_value
== 0) || (new_value
== 1)) {
1191 bond
->params
.use_carrier
= new_value
;
1192 printk(KERN_INFO DRV_NAME
": %s: Setting use_carrier to %d.\n",
1193 bond
->dev
->name
, new_value
);
1195 printk(KERN_INFO DRV_NAME
1196 ": %s: Ignoring invalid use_carrier value %d.\n",
1197 bond
->dev
->name
, new_value
);
1202 static DEVICE_ATTR(use_carrier
, S_IRUGO
| S_IWUSR
, bonding_show_carrier
, bonding_store_carrier
);
1206 * Show and set currently active_slave.
1208 static ssize_t
bonding_show_active_slave(struct device
*d
,
1209 struct device_attribute
*attr
,
1213 struct bonding
*bond
= to_bond(d
);
1217 read_lock(&bond
->curr_slave_lock
);
1218 curr
= bond
->curr_active_slave
;
1219 read_unlock(&bond
->curr_slave_lock
);
1221 if (USES_PRIMARY(bond
->params
.mode
) && curr
)
1222 count
= sprintf(buf
, "%s\n", curr
->dev
->name
) + 1;
1224 count
= sprintf(buf
, "\n") + 1;
1228 static ssize_t
bonding_store_active_slave(struct device
*d
,
1229 struct device_attribute
*attr
,
1230 const char *buf
, size_t count
)
1233 struct slave
*slave
;
1234 struct slave
*old_active
= NULL
;
1235 struct slave
*new_active
= NULL
;
1236 struct bonding
*bond
= to_bond(d
);
1238 write_lock_bh(&bond
->lock
);
1239 if (!USES_PRIMARY(bond
->params
.mode
)) {
1240 printk(KERN_INFO DRV_NAME
1241 ": %s: Unable to change active slave; %s is in mode %d\n",
1242 bond
->dev
->name
, bond
->dev
->name
, bond
->params
.mode
);
1244 bond_for_each_slave(bond
, slave
, i
) {
1246 (slave
->dev
->name
, buf
,
1247 strlen(slave
->dev
->name
)) == 0) {
1248 old_active
= bond
->curr_active_slave
;
1250 if (new_active
== old_active
) {
1252 printk(KERN_INFO DRV_NAME
1253 ": %s: %s is already the current active slave.\n",
1254 bond
->dev
->name
, slave
->dev
->name
);
1260 (new_active
->link
== BOND_LINK_UP
) &&
1261 IS_UP(new_active
->dev
)) {
1262 printk(KERN_INFO DRV_NAME
1263 ": %s: Setting %s as active slave.\n",
1264 bond
->dev
->name
, slave
->dev
->name
);
1265 bond_change_active_slave(bond
, new_active
);
1268 printk(KERN_INFO DRV_NAME
1269 ": %s: Could not set %s as active slave; "
1270 "either %s is down or the link is down.\n",
1271 bond
->dev
->name
, slave
->dev
->name
,
1279 /* if we got here, then we didn't match the name of any slave */
1281 if (strlen(buf
) == 0 || buf
[0] == '\n') {
1282 printk(KERN_INFO DRV_NAME
1283 ": %s: Setting active slave to None.\n",
1285 bond
->primary_slave
= NULL
;
1286 bond_select_active_slave(bond
);
1288 printk(KERN_INFO DRV_NAME
1289 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1290 bond
->dev
->name
, (int)strlen(buf
) - 1, buf
);
1294 write_unlock_bh(&bond
->lock
);
1298 static DEVICE_ATTR(active_slave
, S_IRUGO
| S_IWUSR
, bonding_show_active_slave
, bonding_store_active_slave
);
1302 * Show link status of the bond interface.
1304 static ssize_t
bonding_show_mii_status(struct device
*d
,
1305 struct device_attribute
*attr
,
1309 struct bonding
*bond
= to_bond(d
);
1311 read_lock(&bond
->curr_slave_lock
);
1312 curr
= bond
->curr_active_slave
;
1313 read_unlock(&bond
->curr_slave_lock
);
1315 return sprintf(buf
, "%s\n", (curr
) ? "up" : "down") + 1;
1317 static DEVICE_ATTR(mii_status
, S_IRUGO
, bonding_show_mii_status
, NULL
);
1321 * Show current 802.3ad aggregator ID.
1323 static ssize_t
bonding_show_ad_aggregator(struct device
*d
,
1324 struct device_attribute
*attr
,
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", (bond_3ad_get_active_agg_info(bond
, &ad_info
)) ? 0 : ad_info
.aggregator_id
) + 1;
1335 count
= sprintf(buf
, "\n") + 1;
1339 static DEVICE_ATTR(ad_aggregator
, S_IRUGO
, bonding_show_ad_aggregator
, NULL
);
1343 * Show number of active 802.3ad ports.
1345 static ssize_t
bonding_show_ad_num_ports(struct device
*d
,
1346 struct device_attribute
*attr
,
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", (bond_3ad_get_active_agg_info(bond
, &ad_info
)) ? 0: ad_info
.ports
) + 1;
1357 count
= sprintf(buf
, "\n") + 1;
1361 static DEVICE_ATTR(ad_num_ports
, S_IRUGO
, bonding_show_ad_num_ports
, NULL
);
1365 * Show current 802.3ad actor key.
1367 static ssize_t
bonding_show_ad_actor_key(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", (bond_3ad_get_active_agg_info(bond
, &ad_info
)) ? 0 : ad_info
.actor_key
) + 1;
1379 count
= sprintf(buf
, "\n") + 1;
1383 static DEVICE_ATTR(ad_actor_key
, S_IRUGO
, bonding_show_ad_actor_key
, NULL
);
1387 * Show current 802.3ad partner key.
1389 static ssize_t
bonding_show_ad_partner_key(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", (bond_3ad_get_active_agg_info(bond
, &ad_info
)) ? 0 : ad_info
.partner_key
) + 1;
1401 count
= sprintf(buf
, "\n") + 1;
1405 static DEVICE_ATTR(ad_partner_key
, S_IRUGO
, bonding_show_ad_partner_key
, NULL
);
1409 * Show current 802.3ad partner mac.
1411 static ssize_t
bonding_show_ad_partner_mac(struct device
*d
,
1412 struct device_attribute
*attr
,
1416 struct bonding
*bond
= to_bond(d
);
1417 DECLARE_MAC_BUF(mac
);
1419 if (bond
->params
.mode
== BOND_MODE_8023AD
) {
1420 struct ad_info ad_info
;
1421 if (!bond_3ad_get_active_agg_info(bond
, &ad_info
)) {
1422 count
= sprintf(buf
,"%s\n",
1423 print_mac(mac
, ad_info
.partner_system
))
1428 count
= sprintf(buf
, "\n") + 1;
1432 static DEVICE_ATTR(ad_partner_mac
, S_IRUGO
, bonding_show_ad_partner_mac
, NULL
);
1436 static struct attribute
*per_bond_attrs
[] = {
1437 &dev_attr_slaves
.attr
,
1438 &dev_attr_mode
.attr
,
1439 &dev_attr_fail_over_mac
.attr
,
1440 &dev_attr_arp_validate
.attr
,
1441 &dev_attr_arp_interval
.attr
,
1442 &dev_attr_arp_ip_target
.attr
,
1443 &dev_attr_downdelay
.attr
,
1444 &dev_attr_updelay
.attr
,
1445 &dev_attr_lacp_rate
.attr
,
1446 &dev_attr_xmit_hash_policy
.attr
,
1447 &dev_attr_miimon
.attr
,
1448 &dev_attr_primary
.attr
,
1449 &dev_attr_use_carrier
.attr
,
1450 &dev_attr_active_slave
.attr
,
1451 &dev_attr_mii_status
.attr
,
1452 &dev_attr_ad_aggregator
.attr
,
1453 &dev_attr_ad_num_ports
.attr
,
1454 &dev_attr_ad_actor_key
.attr
,
1455 &dev_attr_ad_partner_key
.attr
,
1456 &dev_attr_ad_partner_mac
.attr
,
1460 static struct attribute_group bonding_group
= {
1462 .attrs
= per_bond_attrs
,
1466 * Initialize sysfs. This sets up the bonding_masters file in
1469 int bond_create_sysfs(void)
1472 struct bonding
*firstbond
;
1474 init_rwsem(&bonding_rwsem
);
1476 /* get the netdev class pointer */
1477 firstbond
= container_of(bond_dev_list
.next
, struct bonding
, bond_list
);
1481 netdev_class
= firstbond
->dev
->dev
.class;
1485 ret
= class_create_file(netdev_class
, &class_attr_bonding_masters
);
1487 * Permit multiple loads of the module by ignoring failures to
1488 * create the bonding_masters sysfs file. Bonding devices
1489 * created by second or subsequent loads of the module will
1490 * not be listed in, or controllable by, bonding_masters, but
1491 * will have the usual "bonding" sysfs directory.
1493 * This is done to preserve backwards compatibility for
1494 * initscripts/sysconfig, which load bonding multiple times to
1495 * configure multiple bonding devices.
1497 if (ret
== -EEXIST
) {
1498 netdev_class
= NULL
;
1507 * Remove /sys/class/net/bonding_masters.
1509 void bond_destroy_sysfs(void)
1512 class_remove_file(netdev_class
, &class_attr_bonding_masters
);
1516 * Initialize sysfs for each bond. This sets up and registers
1517 * the 'bondctl' directory for each individual bond under /sys/class/net.
1519 int bond_create_sysfs_entry(struct bonding
*bond
)
1521 struct net_device
*dev
= bond
->dev
;
1524 err
= sysfs_create_group(&(dev
->dev
.kobj
), &bonding_group
);
1526 printk(KERN_EMERG
"eek! didn't create group!\n");
1529 if (expected_refcount
< 1)
1530 expected_refcount
= atomic_read(&bond
->dev
->dev
.kobj
.kref
.refcount
);
1535 * Remove sysfs entries for each bond.
1537 void bond_destroy_sysfs_entry(struct bonding
*bond
)
1539 struct net_device
*dev
= bond
->dev
;
1541 sysfs_remove_group(&(dev
->dev
.kobj
), &bonding_group
);