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.
25 * 2004/12/12 - Mitch Williams <mitch.a.williams at intel dot com>
26 * - Initial creation of sysfs interface.
28 * 2005/06/22 - Radheka Godse <radheka.godse at intel dot com>
29 * - Added ifenslave -c type functionality to sysfs
30 * - Added sysfs files for attributes such as MII Status and
31 * 802.3ad aggregator that are displayed in /proc
32 * - Added "name value" format to sysfs "mode" and
33 * "lacp_rate", for e.g., "active-backup 1" or "slow 0" for
34 * consistency and ease of script parsing
35 * - Fixed reversal of octets in arp_ip_targets via sysfs
36 * - sysfs support to handle bond interface re-naming
37 * - Moved all sysfs entries into /sys/class/net instead of
38 * of using a standalone subsystem.
39 * - Added sysfs symlinks between masters and slaves
40 * - Corrected bugs in sysfs unload path when creating bonds
41 * with existing interface names.
42 * - Removed redundant sysfs stat file since it duplicates slave info
44 * - Fixed errors in sysfs show/store arp targets.
45 * - For consistency with ifenslave, instead of exiting
46 * with an error, updated bonding sysfs to
47 * close and attempt to enslave an up adapter.
48 * - Fixed NULL dereference when adding a slave interface
49 * that does not exist.
50 * - Added checks in sysfs bonding to reject invalid ip addresses
51 * - Synch up with post linux-2.6.12 bonding changes
52 * - Created sysfs bond attrib for xmit_hash_policy
54 * 2005/09/19 - Mitch Williams <mitch.a.williams at intel dot com>
55 * - Changed semantics of multi-item files to be command-based
56 * instead of list-based.
57 * - Changed ARP target handler to use in_aton instead of sscanf
59 * 2005/09/27 - Mitch Williams <mitch.a.williams at intel dot com>
60 * - Made line endings consistent.
61 * - Removed "none" from primary output - just put blank instead
62 * - Fixed bug with long interface names
64 #include <linux/config.h>
65 #include <linux/kernel.h>
66 #include <linux/module.h>
67 #include <linux/sched.h>
68 #include <linux/device.h>
69 #include <linux/sysdev.h>
71 #include <linux/types.h>
72 #include <linux/string.h>
73 #include <linux/netdevice.h>
74 #include <linux/inetdevice.h>
76 #include <linux/sysfs.h>
77 #include <linux/string.h>
78 #include <linux/ctype.h>
79 #include <linux/inet.h>
80 #include <linux/rtnetlink.h>
82 /* #define BONDING_DEBUG 1 */
84 #define to_class_dev(obj) container_of(obj,struct class_device,kobj)
85 #define to_net_dev(class) container_of(class, struct net_device, class_dev)
86 #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
88 /*---------------------------- Declarations -------------------------------*/
91 extern struct list_head bond_dev_list;
92 extern struct bond_params bonding_defaults;
93 extern struct bond_parm_tbl bond_mode_tbl[];
94 extern struct bond_parm_tbl bond_lacp_tbl[];
95 extern struct bond_parm_tbl xmit_hashtype_tbl[];
97 static int expected_refcount = -1;
98 static struct class *netdev_class;
99 /*--------------------------- Data Structures -----------------------------*/
101 /* Bonding sysfs lock. Why can't we just use the subsytem lock?
102 * Because kobject_register tries to acquire the subsystem lock. If
103 * we already hold the lock (which we would if the user was creating
104 * a new bond through the sysfs interface), we deadlock.
105 * This lock is only needed when deleting a bond - we need to make sure
106 * that we don't collide with an ongoing ioctl.
109 struct rw_semaphore bonding_rwsem;
114 /*------------------------------ Functions --------------------------------*/
117 * "show" function for the bond_masters attribute.
118 * The class parameter is ignored.
120 static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
123 struct bonding *bond;
125 down_read(&(bonding_rwsem));
127 list_for_each_entry(bond, &bond_dev_list, bond_list) {
128 if (res > (PAGE_SIZE - IFNAMSIZ)) {
129 /* not enough space for another interface name */
130 if ((PAGE_SIZE - res) > 10)
131 res = PAGE_SIZE - 10;
132 res += sprintf(buffer + res, "++more++");
135 res += sprintf(buffer + res, "%s ",
138 res += sprintf(buffer + res, "\n");
140 up_read(&(bonding_rwsem));
145 * "store" function for the bond_masters attribute. This is what
146 * creates and deletes entire bonds.
148 * The class parameter is ignored.
152 static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
154 char command[IFNAMSIZ + 1] = {0, };
157 struct bonding *bond;
160 down_write(&(bonding_rwsem));
161 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
162 ifname = command + 1;
163 if ((strlen(command) <= 1) ||
164 !dev_valid_name(ifname))
167 if (command[0] == '+') {
169 /* Check to see if the bond already exists. */
170 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
171 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
172 printk(KERN_ERR DRV_NAME
173 ": cannot add bond %s; it already exists\n",
179 printk(KERN_INFO DRV_NAME
180 ": %s is being created...\n", ifname);
181 if (bond_create(ifname, &bonding_defaults, &bond)) {
182 printk(KERN_INFO DRV_NAME
183 ": %s interface already exists. Bond creation failed.\n",
190 if (command[0] == '-') {
191 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
192 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
194 /* check the ref count on the bond's kobject.
195 * If it's > expected, then there's a file open,
196 * and we have to fail.
198 if (atomic_read(&bond->dev->class_dev.kobj.kref.refcount)
199 > expected_refcount){
201 printk(KERN_INFO DRV_NAME
202 ": Unable remove bond %s due to open references.\n",
207 printk(KERN_INFO DRV_NAME
208 ": %s is being deleted...\n",
210 unregister_netdevice(bond->dev);
211 bond_deinit(bond->dev);
212 bond_destroy_sysfs_entry(bond);
217 printk(KERN_ERR DRV_NAME
218 ": unable to delete non-existent bond %s\n", ifname);
224 printk(KERN_ERR DRV_NAME
225 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
228 /* Always return either count or an error. If you return 0, you'll
229 * get called forever, which is bad.
232 up_write(&(bonding_rwsem));
235 /* class attribute for bond_masters file. This ends up in /sys/class/net */
236 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
237 bonding_show_bonds, bonding_store_bonds);
239 int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
241 char linkname[IFNAMSIZ+7];
244 /* first, create a link from the slave back to the master */
245 ret = sysfs_create_link(&(slave->class_dev.kobj), &(master->class_dev.kobj),
249 /* next, create a link from the master to the slave */
250 sprintf(linkname,"slave_%s",slave->name);
251 ret = sysfs_create_link(&(master->class_dev.kobj), &(slave->class_dev.kobj),
257 void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
259 char linkname[IFNAMSIZ+7];
261 sysfs_remove_link(&(slave->class_dev.kobj), "master");
262 sprintf(linkname,"slave_%s",slave->name);
263 sysfs_remove_link(&(master->class_dev.kobj), linkname);
268 * Show the slaves in the current bond.
270 static ssize_t bonding_show_slaves(struct class_device *cd, char *buf)
274 struct bonding *bond = to_bond(cd);
276 read_lock_bh(&bond->lock);
277 bond_for_each_slave(bond, slave, i) {
278 if (res > (PAGE_SIZE - IFNAMSIZ)) {
279 /* not enough space for another interface name */
280 if ((PAGE_SIZE - res) > 10)
281 res = PAGE_SIZE - 10;
282 res += sprintf(buf + res, "++more++");
285 res += sprintf(buf + res, "%s ", slave->dev->name);
287 read_unlock_bh(&bond->lock);
288 res += sprintf(buf + res, "\n");
294 * Set the slaves in the current bond. The bond interface must be
295 * up for this to succeed.
296 * This function is largely the same flow as bonding_update_bonds().
298 static ssize_t bonding_store_slaves(struct class_device *cd, const char *buffer, size_t count)
300 char command[IFNAMSIZ + 1] = { 0, };
302 int i, res, found, ret = count;
304 struct net_device *dev = 0;
305 struct bonding *bond = to_bond(cd);
307 /* Quick sanity check -- is the bond interface up? */
308 if (!(bond->dev->flags & IFF_UP)) {
309 printk(KERN_ERR DRV_NAME
310 ": %s: Unable to update slaves because interface is down.\n",
316 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
318 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
319 ifname = command + 1;
320 if ((strlen(command) <= 1) ||
321 !dev_valid_name(ifname))
324 if (command[0] == '+') {
326 /* Got a slave name in ifname. Is it already in the list? */
328 read_lock_bh(&bond->lock);
329 bond_for_each_slave(bond, slave, i)
330 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
331 printk(KERN_ERR DRV_NAME
332 ": %s: Interface %s is already enslaved!\n",
333 bond->dev->name, ifname);
335 read_unlock_bh(&bond->lock);
339 read_unlock_bh(&bond->lock);
340 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
341 bond->dev->name, ifname);
342 dev = dev_get_by_name(ifname);
344 printk(KERN_INFO DRV_NAME
345 ": %s: Interface %s does not exist!\n",
346 bond->dev->name, ifname);
353 if (dev->flags & IFF_UP) {
354 printk(KERN_ERR DRV_NAME
355 ": %s: Error: Unable to enslave %s "
356 "because it is already up.\n",
357 bond->dev->name, dev->name);
361 /* If this is the first slave, then we need to set
362 the master's hardware address to be the same as the
364 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
365 memcpy(bond->dev->dev_addr, dev->dev_addr,
369 /* Set the slave's MTU to match the bond */
370 if (dev->mtu != bond->dev->mtu) {
371 if (dev->change_mtu) {
372 res = dev->change_mtu(dev,
379 dev->mtu = bond->dev->mtu;
383 res = bond_enslave(bond->dev, dev);
391 if (command[0] == '-') {
393 bond_for_each_slave(bond, slave, i)
394 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
399 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
400 bond->dev->name, dev->name);
402 res = bond_release(bond->dev, dev);
408 /* set the slave MTU to the default */
409 if (dev->change_mtu) {
410 dev->change_mtu(dev, 1500);
416 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
417 ifname, bond->dev->name);
424 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
431 static CLASS_DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
434 * Show and set the bonding mode. The bond interface must be down to
437 static ssize_t bonding_show_mode(struct class_device *cd, char *buf)
439 struct bonding *bond = to_bond(cd);
441 return sprintf(buf, "%s %d\n",
442 bond_mode_tbl[bond->params.mode].modename,
443 bond->params.mode) + 1;
446 static ssize_t bonding_store_mode(struct class_device *cd, const char *buf, size_t count)
448 int new_value, ret = count;
449 struct bonding *bond = to_bond(cd);
451 if (bond->dev->flags & IFF_UP) {
452 printk(KERN_ERR DRV_NAME
453 ": unable to update mode of %s because interface is up.\n",
459 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
461 printk(KERN_ERR DRV_NAME
462 ": %s: Ignoring invalid mode value %.*s.\n",
464 (int)strlen(buf) - 1, buf);
468 bond->params.mode = new_value;
469 bond_set_mode_ops(bond, bond->params.mode);
470 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
471 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
476 static CLASS_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
479 * Show and set the bonding transmit hash method. The bond interface must be down to
480 * change the xmit hash policy.
482 static ssize_t bonding_show_xmit_hash(struct class_device *cd, char *buf)
485 struct bonding *bond = to_bond(cd);
487 if ((bond->params.mode != BOND_MODE_XOR) &&
488 (bond->params.mode != BOND_MODE_8023AD)) {
490 count = sprintf(buf, "NA\n") + 1;
492 count = sprintf(buf, "%s %d\n",
493 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
494 bond->params.xmit_policy) + 1;
500 static ssize_t bonding_store_xmit_hash(struct class_device *cd, const char *buf, size_t count)
502 int new_value, ret = count;
503 struct bonding *bond = to_bond(cd);
505 if (bond->dev->flags & IFF_UP) {
506 printk(KERN_ERR DRV_NAME
507 "%s: Interface is up. Unable to update xmit policy.\n",
513 if ((bond->params.mode != BOND_MODE_XOR) &&
514 (bond->params.mode != BOND_MODE_8023AD)) {
515 printk(KERN_ERR DRV_NAME
516 "%s: Transmit hash policy is irrelevant in this mode.\n",
522 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
524 printk(KERN_ERR DRV_NAME
525 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
527 (int)strlen(buf) - 1, buf);
531 bond->params.xmit_policy = new_value;
532 bond_set_mode_ops(bond, bond->params.mode);
533 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
534 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
539 static CLASS_DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
542 * Show and set the arp timer interval. There are two tricky bits
543 * here. First, if ARP monitoring is activated, then we must disable
544 * MII monitoring. Second, if the ARP timer isn't running, we must
547 static ssize_t bonding_show_arp_interval(struct class_device *cd, char *buf)
549 struct bonding *bond = to_bond(cd);
551 return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
554 static ssize_t bonding_store_arp_interval(struct class_device *cd, const char *buf, size_t count)
556 int new_value, ret = count;
557 struct bonding *bond = to_bond(cd);
559 if (sscanf(buf, "%d", &new_value) != 1) {
560 printk(KERN_ERR DRV_NAME
561 ": %s: no arp_interval value specified.\n",
567 printk(KERN_ERR DRV_NAME
568 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
569 bond->dev->name, new_value, INT_MAX);
574 printk(KERN_INFO DRV_NAME
575 ": %s: Setting ARP monitoring interval to %d.\n",
576 bond->dev->name, new_value);
577 bond->params.arp_interval = new_value;
578 if (bond->params.miimon) {
579 printk(KERN_INFO DRV_NAME
580 ": %s: ARP monitoring cannot be used with MII monitoring. "
581 "%s Disabling MII monitoring.\n",
582 bond->dev->name, bond->dev->name);
583 bond->params.miimon = 0;
584 /* Kill MII timer, else it brings bond's link down */
585 if (bond->arp_timer.function) {
586 printk(KERN_INFO DRV_NAME
587 ": %s: Kill MII timer, else it brings bond's link down...\n",
589 del_timer_sync(&bond->mii_timer);
592 if (!bond->params.arp_targets[0]) {
593 printk(KERN_INFO DRV_NAME
594 ": %s: ARP monitoring has been set up, "
595 "but no ARP targets have been specified.\n",
598 if (bond->dev->flags & IFF_UP) {
599 /* If the interface is up, we may need to fire off
600 * the ARP timer. If the interface is down, the
601 * timer will get fired off when the open function
604 if (bond->arp_timer.function) {
605 /* The timer's already set up, so fire it off */
606 mod_timer(&bond->arp_timer, jiffies + 1);
608 /* Set up the timer. */
609 init_timer(&bond->arp_timer);
610 bond->arp_timer.expires = jiffies + 1;
611 bond->arp_timer.data =
612 (unsigned long) bond->dev;
613 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
614 bond->arp_timer.function =
616 &bond_activebackup_arp_mon;
618 bond->arp_timer.function =
620 &bond_loadbalance_arp_mon;
622 add_timer(&bond->arp_timer);
629 static CLASS_DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
632 * Show and set the arp targets.
634 static ssize_t bonding_show_arp_targets(struct class_device *cd, char *buf)
637 struct bonding *bond = to_bond(cd);
639 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
640 if (bond->params.arp_targets[i])
641 res += sprintf(buf + res, "%u.%u.%u.%u ",
642 NIPQUAD(bond->params.arp_targets[i]));
645 res--; /* eat the leftover space */
646 res += sprintf(buf + res, "\n");
651 static ssize_t bonding_store_arp_targets(struct class_device *cd, const char *buf, size_t count)
654 int i = 0, done = 0, ret = count;
655 struct bonding *bond = to_bond(cd);
658 targets = bond->params.arp_targets;
659 newtarget = in_aton(buf + 1);
662 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
663 printk(KERN_ERR DRV_NAME
664 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
665 bond->dev->name, NIPQUAD(newtarget));
669 /* look for an empty slot to put the target in, and check for dupes */
670 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
671 if (targets[i] == newtarget) { /* duplicate */
672 printk(KERN_ERR DRV_NAME
673 ": %s: ARP target %u.%u.%u.%u is already present\n",
674 bond->dev->name, NIPQUAD(newtarget));
680 if (targets[i] == 0 && !done) {
681 printk(KERN_INFO DRV_NAME
682 ": %s: adding ARP target %d.%d.%d.%d.\n",
683 bond->dev->name, NIPQUAD(newtarget));
685 targets[i] = newtarget;
689 printk(KERN_ERR DRV_NAME
690 ": %s: ARP target table is full!\n",
697 else if (buf[0] == '-') {
698 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
699 printk(KERN_ERR DRV_NAME
700 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
701 bond->dev->name, NIPQUAD(newtarget));
706 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
707 if (targets[i] == newtarget) {
708 printk(KERN_INFO DRV_NAME
709 ": %s: removing ARP target %d.%d.%d.%d.\n",
710 bond->dev->name, NIPQUAD(newtarget));
716 printk(KERN_INFO DRV_NAME
717 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
718 bond->dev->name, NIPQUAD(newtarget));
724 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
733 static CLASS_DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
736 * Show and set the up and down delays. These must be multiples of the
737 * MII monitoring value, and are stored internally as the multiplier.
738 * Thus, we must translate to MS for the real world.
740 static ssize_t bonding_show_downdelay(struct class_device *cd, char *buf)
742 struct bonding *bond = to_bond(cd);
744 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
747 static ssize_t bonding_store_downdelay(struct class_device *cd, const char *buf, size_t count)
749 int new_value, ret = count;
750 struct bonding *bond = to_bond(cd);
752 if (!(bond->params.miimon)) {
753 printk(KERN_ERR DRV_NAME
754 ": %s: Unable to set down delay as MII monitoring is disabled\n",
760 if (sscanf(buf, "%d", &new_value) != 1) {
761 printk(KERN_ERR DRV_NAME
762 ": %s: no down delay value specified.\n",
768 printk(KERN_ERR DRV_NAME
769 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
770 bond->dev->name, new_value, 1, INT_MAX);
774 if ((new_value % bond->params.miimon) != 0) {
775 printk(KERN_WARNING DRV_NAME
776 ": %s: Warning: down delay (%d) is not a multiple "
777 "of miimon (%d), delay rounded to %d ms\n",
778 bond->dev->name, new_value, bond->params.miimon,
779 (new_value / bond->params.miimon) *
780 bond->params.miimon);
782 bond->params.downdelay = new_value / bond->params.miimon;
783 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
784 bond->dev->name, bond->params.downdelay * bond->params.miimon);
791 static CLASS_DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
793 static ssize_t bonding_show_updelay(struct class_device *cd, char *buf)
795 struct bonding *bond = to_bond(cd);
797 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
801 static ssize_t bonding_store_updelay(struct class_device *cd, const char *buf, size_t count)
803 int new_value, ret = count;
804 struct bonding *bond = to_bond(cd);
806 if (!(bond->params.miimon)) {
807 printk(KERN_ERR DRV_NAME
808 ": %s: Unable to set up delay as MII monitoring is disabled\n",
814 if (sscanf(buf, "%d", &new_value) != 1) {
815 printk(KERN_ERR DRV_NAME
816 ": %s: no up delay value specified.\n",
822 printk(KERN_ERR DRV_NAME
823 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
824 bond->dev->name, new_value, 1, INT_MAX);
828 if ((new_value % bond->params.miimon) != 0) {
829 printk(KERN_WARNING DRV_NAME
830 ": %s: Warning: up delay (%d) is not a multiple "
831 "of miimon (%d), updelay rounded to %d ms\n",
832 bond->dev->name, new_value, bond->params.miimon,
833 (new_value / bond->params.miimon) *
834 bond->params.miimon);
836 bond->params.updelay = new_value / bond->params.miimon;
837 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
838 bond->dev->name, bond->params.updelay * bond->params.miimon);
845 static CLASS_DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
848 * Show and set the LACP interval. Interface must be down, and the mode
849 * must be set to 802.3ad mode.
851 static ssize_t bonding_show_lacp(struct class_device *cd, char *buf)
853 struct bonding *bond = to_bond(cd);
855 return sprintf(buf, "%s %d\n",
856 bond_lacp_tbl[bond->params.lacp_fast].modename,
857 bond->params.lacp_fast) + 1;
860 static ssize_t bonding_store_lacp(struct class_device *cd, const char *buf, size_t count)
862 int new_value, ret = count;
863 struct bonding *bond = to_bond(cd);
865 if (bond->dev->flags & IFF_UP) {
866 printk(KERN_ERR DRV_NAME
867 ": %s: Unable to update LACP rate because interface is up.\n",
873 if (bond->params.mode != BOND_MODE_8023AD) {
874 printk(KERN_ERR DRV_NAME
875 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
881 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
883 if ((new_value == 1) || (new_value == 0)) {
884 bond->params.lacp_fast = new_value;
885 printk(KERN_INFO DRV_NAME
886 ": %s: Setting LACP rate to %s (%d).\n",
887 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
889 printk(KERN_ERR DRV_NAME
890 ": %s: Ignoring invalid LACP rate value %.*s.\n",
891 bond->dev->name, (int)strlen(buf) - 1, buf);
897 static CLASS_DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
900 * Show and set the MII monitor interval. There are two tricky bits
901 * here. First, if MII monitoring is activated, then we must disable
902 * ARP monitoring. Second, if the timer isn't running, we must
905 static ssize_t bonding_show_miimon(struct class_device *cd, char *buf)
907 struct bonding *bond = to_bond(cd);
909 return sprintf(buf, "%d\n", bond->params.miimon) + 1;
912 static ssize_t bonding_store_miimon(struct class_device *cd, const char *buf, size_t count)
914 int new_value, ret = count;
915 struct bonding *bond = to_bond(cd);
917 if (sscanf(buf, "%d", &new_value) != 1) {
918 printk(KERN_ERR DRV_NAME
919 ": %s: no miimon value specified.\n",
925 printk(KERN_ERR DRV_NAME
926 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
927 bond->dev->name, new_value, 1, INT_MAX);
931 printk(KERN_INFO DRV_NAME
932 ": %s: Setting MII monitoring interval to %d.\n",
933 bond->dev->name, new_value);
934 bond->params.miimon = new_value;
935 if(bond->params.updelay)
936 printk(KERN_INFO DRV_NAME
937 ": %s: Note: Updating updelay (to %d) "
938 "since it is a multiple of the miimon value.\n",
940 bond->params.updelay * bond->params.miimon);
941 if(bond->params.downdelay)
942 printk(KERN_INFO DRV_NAME
943 ": %s: Note: Updating downdelay (to %d) "
944 "since it is a multiple of the miimon value.\n",
946 bond->params.downdelay * bond->params.miimon);
947 if (bond->params.arp_interval) {
948 printk(KERN_INFO DRV_NAME
949 ": %s: MII monitoring cannot be used with "
950 "ARP monitoring. Disabling ARP monitoring...\n",
952 bond->params.arp_interval = 0;
953 /* Kill ARP timer, else it brings bond's link down */
954 if (bond->mii_timer.function) {
955 printk(KERN_INFO DRV_NAME
956 ": %s: Kill ARP timer, else it brings bond's link down...\n",
958 del_timer_sync(&bond->arp_timer);
962 if (bond->dev->flags & IFF_UP) {
963 /* If the interface is up, we may need to fire off
964 * the MII timer. If the interface is down, the
965 * timer will get fired off when the open function
968 if (bond->mii_timer.function) {
969 /* The timer's already set up, so fire it off */
970 mod_timer(&bond->mii_timer, jiffies + 1);
972 /* Set up the timer. */
973 init_timer(&bond->mii_timer);
974 bond->mii_timer.expires = jiffies + 1;
975 bond->mii_timer.data =
976 (unsigned long) bond->dev;
977 bond->mii_timer.function =
978 (void *) &bond_mii_monitor;
979 add_timer(&bond->mii_timer);
986 static CLASS_DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
989 * Show and set the primary slave. The store function is much
990 * simpler than bonding_store_slaves function because it only needs to
991 * handle one interface name.
992 * The bond must be a mode that supports a primary for this be
995 static ssize_t bonding_show_primary(struct class_device *cd, char *buf)
998 struct bonding *bond = to_bond(cd);
1000 if (bond->primary_slave)
1001 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
1003 count = sprintf(buf, "\n") + 1;
1008 static ssize_t bonding_store_primary(struct class_device *cd, const char *buf, size_t count)
1011 struct slave *slave;
1012 struct bonding *bond = to_bond(cd);
1014 write_lock_bh(&bond->lock);
1015 if (!USES_PRIMARY(bond->params.mode)) {
1016 printk(KERN_INFO DRV_NAME
1017 ": %s: Unable to set primary slave; %s is in mode %d\n",
1018 bond->dev->name, bond->dev->name, bond->params.mode);
1020 bond_for_each_slave(bond, slave, i) {
1022 (slave->dev->name, buf,
1023 strlen(slave->dev->name)) == 0) {
1024 printk(KERN_INFO DRV_NAME
1025 ": %s: Setting %s as primary slave.\n",
1026 bond->dev->name, slave->dev->name);
1027 bond->primary_slave = slave;
1028 bond_select_active_slave(bond);
1033 /* if we got here, then we didn't match the name of any slave */
1035 if (strlen(buf) == 0 || buf[0] == '\n') {
1036 printk(KERN_INFO DRV_NAME
1037 ": %s: Setting primary slave to None.\n",
1039 bond->primary_slave = 0;
1040 bond_select_active_slave(bond);
1042 printk(KERN_INFO DRV_NAME
1043 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1044 bond->dev->name, (int)strlen(buf) - 1, buf);
1048 write_unlock_bh(&bond->lock);
1051 static CLASS_DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
1054 * Show and set the use_carrier flag.
1056 static ssize_t bonding_show_carrier(struct class_device *cd, char *buf)
1058 struct bonding *bond = to_bond(cd);
1060 return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
1063 static ssize_t bonding_store_carrier(struct class_device *cd, const char *buf, size_t count)
1065 int new_value, ret = count;
1066 struct bonding *bond = to_bond(cd);
1069 if (sscanf(buf, "%d", &new_value) != 1) {
1070 printk(KERN_ERR DRV_NAME
1071 ": %s: no use_carrier value specified.\n",
1076 if ((new_value == 0) || (new_value == 1)) {
1077 bond->params.use_carrier = new_value;
1078 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1079 bond->dev->name, new_value);
1081 printk(KERN_INFO DRV_NAME
1082 ": %s: Ignoring invalid use_carrier value %d.\n",
1083 bond->dev->name, new_value);
1088 static CLASS_DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
1092 * Show and set currently active_slave.
1094 static ssize_t bonding_show_active_slave(struct class_device *cd, char *buf)
1097 struct bonding *bond = to_bond(cd);
1101 read_lock(&bond->curr_slave_lock);
1102 curr = bond->curr_active_slave;
1103 read_unlock(&bond->curr_slave_lock);
1105 if (USES_PRIMARY(bond->params.mode) && curr)
1106 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
1108 count = sprintf(buf, "\n") + 1;
1112 static ssize_t bonding_store_active_slave(struct class_device *cd, const char *buf, size_t count)
1115 struct slave *slave;
1116 struct slave *old_active = NULL;
1117 struct slave *new_active = NULL;
1118 struct bonding *bond = to_bond(cd);
1120 write_lock_bh(&bond->lock);
1121 if (!USES_PRIMARY(bond->params.mode)) {
1122 printk(KERN_INFO DRV_NAME
1123 ": %s: Unable to change active slave; %s is in mode %d\n",
1124 bond->dev->name, bond->dev->name, bond->params.mode);
1126 bond_for_each_slave(bond, slave, i) {
1128 (slave->dev->name, buf,
1129 strlen(slave->dev->name)) == 0) {
1130 old_active = bond->curr_active_slave;
1132 if (new_active && (new_active == old_active)) {
1134 printk(KERN_INFO DRV_NAME
1135 ": %s: %s is already the current active slave.\n",
1136 bond->dev->name, slave->dev->name);
1142 (new_active->link == BOND_LINK_UP) &&
1143 IS_UP(new_active->dev)) {
1144 printk(KERN_INFO DRV_NAME
1145 ": %s: Setting %s as active slave.\n",
1146 bond->dev->name, slave->dev->name);
1147 bond_change_active_slave(bond, new_active);
1150 printk(KERN_INFO DRV_NAME
1151 ": %s: Could not set %s as active slave; "
1152 "either %s is down or the link is down.\n",
1153 bond->dev->name, slave->dev->name,
1161 /* if we got here, then we didn't match the name of any slave */
1163 if (strlen(buf) == 0 || buf[0] == '\n') {
1164 printk(KERN_INFO DRV_NAME
1165 ": %s: Setting active slave to None.\n",
1167 bond->primary_slave = 0;
1168 bond_select_active_slave(bond);
1170 printk(KERN_INFO DRV_NAME
1171 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1172 bond->dev->name, (int)strlen(buf) - 1, buf);
1176 write_unlock_bh(&bond->lock);
1180 static CLASS_DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
1184 * Show link status of the bond interface.
1186 static ssize_t bonding_show_mii_status(struct class_device *cd, char *buf)
1189 struct bonding *bond = to_bond(cd);
1191 read_lock(&bond->curr_slave_lock);
1192 curr = bond->curr_active_slave;
1193 read_unlock(&bond->curr_slave_lock);
1195 return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
1197 static CLASS_DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1201 * Show current 802.3ad aggregator ID.
1203 static ssize_t bonding_show_ad_aggregator(struct class_device *cd, char *buf)
1206 struct bonding *bond = to_bond(cd);
1208 if (bond->params.mode == BOND_MODE_8023AD) {
1209 struct ad_info ad_info;
1210 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
1213 count = sprintf(buf, "\n") + 1;
1217 static CLASS_DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1221 * Show number of active 802.3ad ports.
1223 static ssize_t bonding_show_ad_num_ports(struct class_device *cd, char *buf)
1226 struct bonding *bond = to_bond(cd);
1228 if (bond->params.mode == BOND_MODE_8023AD) {
1229 struct ad_info ad_info;
1230 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
1233 count = sprintf(buf, "\n") + 1;
1237 static CLASS_DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1241 * Show current 802.3ad actor key.
1243 static ssize_t bonding_show_ad_actor_key(struct class_device *cd, char *buf)
1246 struct bonding *bond = to_bond(cd);
1248 if (bond->params.mode == BOND_MODE_8023AD) {
1249 struct ad_info ad_info;
1250 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
1253 count = sprintf(buf, "\n") + 1;
1257 static CLASS_DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1261 * Show current 802.3ad partner key.
1263 static ssize_t bonding_show_ad_partner_key(struct class_device *cd, char *buf)
1266 struct bonding *bond = to_bond(cd);
1268 if (bond->params.mode == BOND_MODE_8023AD) {
1269 struct ad_info ad_info;
1270 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
1273 count = sprintf(buf, "\n") + 1;
1277 static CLASS_DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1281 * Show current 802.3ad partner mac.
1283 static ssize_t bonding_show_ad_partner_mac(struct class_device *cd, char *buf)
1286 struct bonding *bond = to_bond(cd);
1288 if (bond->params.mode == BOND_MODE_8023AD) {
1289 struct ad_info ad_info;
1290 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1291 count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
1292 ad_info.partner_system[0],
1293 ad_info.partner_system[1],
1294 ad_info.partner_system[2],
1295 ad_info.partner_system[3],
1296 ad_info.partner_system[4],
1297 ad_info.partner_system[5]) + 1;
1301 count = sprintf(buf, "\n") + 1;
1305 static CLASS_DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1309 static struct attribute *per_bond_attrs[] = {
1310 &class_device_attr_slaves.attr,
1311 &class_device_attr_mode.attr,
1312 &class_device_attr_arp_interval.attr,
1313 &class_device_attr_arp_ip_target.attr,
1314 &class_device_attr_downdelay.attr,
1315 &class_device_attr_updelay.attr,
1316 &class_device_attr_lacp_rate.attr,
1317 &class_device_attr_xmit_hash_policy.attr,
1318 &class_device_attr_miimon.attr,
1319 &class_device_attr_primary.attr,
1320 &class_device_attr_use_carrier.attr,
1321 &class_device_attr_active_slave.attr,
1322 &class_device_attr_mii_status.attr,
1323 &class_device_attr_ad_aggregator.attr,
1324 &class_device_attr_ad_num_ports.attr,
1325 &class_device_attr_ad_actor_key.attr,
1326 &class_device_attr_ad_partner_key.attr,
1327 &class_device_attr_ad_partner_mac.attr,
1331 static struct attribute_group bonding_group = {
1333 .attrs = per_bond_attrs,
1337 * Initialize sysfs. This sets up the bonding_masters file in
1340 int bond_create_sysfs(void)
1343 struct bonding *firstbond;
1345 init_rwsem(&bonding_rwsem);
1347 /* get the netdev class pointer */
1348 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1352 netdev_class = firstbond->dev->class_dev.class;
1356 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
1363 * Remove /sys/class/net/bonding_masters.
1365 void bond_destroy_sysfs(void)
1368 class_remove_file(netdev_class, &class_attr_bonding_masters);
1372 * Initialize sysfs for each bond. This sets up and registers
1373 * the 'bondctl' directory for each individual bond under /sys/class/net.
1375 int bond_create_sysfs_entry(struct bonding *bond)
1377 struct net_device *dev = bond->dev;
1380 err = sysfs_create_group(&(dev->class_dev.kobj), &bonding_group);
1382 printk(KERN_EMERG "eek! didn't create group!\n");
1385 if (expected_refcount < 1)
1386 expected_refcount = atomic_read(&bond->dev->class_dev.kobj.kref.refcount);
1391 * Remove sysfs entries for each bond.
1393 void bond_destroy_sysfs_entry(struct bonding *bond)
1395 struct net_device *dev = bond->dev;
1397 sysfs_remove_group(&(dev->class_dev.kobj), &bonding_group);