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/sched.h>
26 #include <linux/device.h>
27 #include <linux/sysdev.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
34 #include <linux/sysfs.h>
35 #include <linux/string.h>
36 #include <linux/ctype.h>
37 #include <linux/inet.h>
38 #include <linux/rtnetlink.h>
40 /* #define BONDING_DEBUG 1 */
42 #define to_class_dev(obj) container_of(obj,struct class_device,kobj)
43 #define to_net_dev(class) container_of(class, struct net_device, class_dev)
44 #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
46 /*---------------------------- Declarations -------------------------------*/
49 extern struct list_head bond_dev_list;
50 extern struct bond_params bonding_defaults;
51 extern struct bond_parm_tbl bond_mode_tbl[];
52 extern struct bond_parm_tbl bond_lacp_tbl[];
53 extern struct bond_parm_tbl xmit_hashtype_tbl[];
55 static int expected_refcount = -1;
56 static struct class *netdev_class;
57 /*--------------------------- Data Structures -----------------------------*/
59 /* Bonding sysfs lock. Why can't we just use the subsytem lock?
60 * Because kobject_register tries to acquire the subsystem lock. If
61 * we already hold the lock (which we would if the user was creating
62 * a new bond through the sysfs interface), we deadlock.
63 * This lock is only needed when deleting a bond - we need to make sure
64 * that we don't collide with an ongoing ioctl.
67 struct rw_semaphore bonding_rwsem;
72 /*------------------------------ Functions --------------------------------*/
75 * "show" function for the bond_masters attribute.
76 * The class parameter is ignored.
78 static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
83 down_read(&(bonding_rwsem));
85 list_for_each_entry(bond, &bond_dev_list, bond_list) {
86 if (res > (PAGE_SIZE - IFNAMSIZ)) {
87 /* not enough space for another interface name */
88 if ((PAGE_SIZE - res) > 10)
90 res += sprintf(buffer + res, "++more++");
93 res += sprintf(buffer + res, "%s ",
96 res += sprintf(buffer + res, "\n");
98 up_read(&(bonding_rwsem));
103 * "store" function for the bond_masters attribute. This is what
104 * creates and deletes entire bonds.
106 * The class parameter is ignored.
110 static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
112 char command[IFNAMSIZ + 1] = {0, };
115 struct bonding *bond;
118 down_write(&(bonding_rwsem));
119 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
120 ifname = command + 1;
121 if ((strlen(command) <= 1) ||
122 !dev_valid_name(ifname))
125 if (command[0] == '+') {
127 /* Check to see if the bond already exists. */
128 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
129 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
130 printk(KERN_ERR DRV_NAME
131 ": cannot add bond %s; it already exists\n",
137 printk(KERN_INFO DRV_NAME
138 ": %s is being created...\n", ifname);
139 if (bond_create(ifname, &bonding_defaults, &bond)) {
140 printk(KERN_INFO DRV_NAME
141 ": %s interface already exists. Bond creation failed.\n",
148 if (command[0] == '-') {
149 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
150 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
152 /* check the ref count on the bond's kobject.
153 * If it's > expected, then there's a file open,
154 * and we have to fail.
156 if (atomic_read(&bond->dev->class_dev.kobj.kref.refcount)
157 > expected_refcount){
159 printk(KERN_INFO DRV_NAME
160 ": Unable remove bond %s due to open references.\n",
165 printk(KERN_INFO DRV_NAME
166 ": %s is being deleted...\n",
168 unregister_netdevice(bond->dev);
169 bond_deinit(bond->dev);
170 bond_destroy_sysfs_entry(bond);
175 printk(KERN_ERR DRV_NAME
176 ": unable to delete non-existent bond %s\n", ifname);
182 printk(KERN_ERR DRV_NAME
183 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
186 /* Always return either count or an error. If you return 0, you'll
187 * get called forever, which is bad.
190 up_write(&(bonding_rwsem));
193 /* class attribute for bond_masters file. This ends up in /sys/class/net */
194 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
195 bonding_show_bonds, bonding_store_bonds);
197 int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
199 char linkname[IFNAMSIZ+7];
202 /* first, create a link from the slave back to the master */
203 ret = sysfs_create_link(&(slave->class_dev.kobj), &(master->class_dev.kobj),
207 /* next, create a link from the master to the slave */
208 sprintf(linkname,"slave_%s",slave->name);
209 ret = sysfs_create_link(&(master->class_dev.kobj), &(slave->class_dev.kobj),
215 void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
217 char linkname[IFNAMSIZ+7];
219 sysfs_remove_link(&(slave->class_dev.kobj), "master");
220 sprintf(linkname,"slave_%s",slave->name);
221 sysfs_remove_link(&(master->class_dev.kobj), linkname);
226 * Show the slaves in the current bond.
228 static ssize_t bonding_show_slaves(struct class_device *cd, char *buf)
232 struct bonding *bond = to_bond(cd);
234 read_lock_bh(&bond->lock);
235 bond_for_each_slave(bond, slave, i) {
236 if (res > (PAGE_SIZE - IFNAMSIZ)) {
237 /* not enough space for another interface name */
238 if ((PAGE_SIZE - res) > 10)
239 res = PAGE_SIZE - 10;
240 res += sprintf(buf + res, "++more++");
243 res += sprintf(buf + res, "%s ", slave->dev->name);
245 read_unlock_bh(&bond->lock);
246 res += sprintf(buf + res, "\n");
252 * Set the slaves in the current bond. The bond interface must be
253 * up for this to succeed.
254 * This function is largely the same flow as bonding_update_bonds().
256 static ssize_t bonding_store_slaves(struct class_device *cd, const char *buffer, size_t count)
258 char command[IFNAMSIZ + 1] = { 0, };
260 int i, res, found, ret = count;
262 struct net_device *dev = NULL;
263 struct bonding *bond = to_bond(cd);
265 /* Quick sanity check -- is the bond interface up? */
266 if (!(bond->dev->flags & IFF_UP)) {
267 printk(KERN_ERR DRV_NAME
268 ": %s: Unable to update slaves because interface is down.\n",
274 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
276 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
277 ifname = command + 1;
278 if ((strlen(command) <= 1) ||
279 !dev_valid_name(ifname))
282 if (command[0] == '+') {
284 /* Got a slave name in ifname. Is it already in the list? */
286 read_lock_bh(&bond->lock);
287 bond_for_each_slave(bond, slave, i)
288 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
289 printk(KERN_ERR DRV_NAME
290 ": %s: Interface %s is already enslaved!\n",
291 bond->dev->name, ifname);
293 read_unlock_bh(&bond->lock);
297 read_unlock_bh(&bond->lock);
298 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
299 bond->dev->name, ifname);
300 dev = dev_get_by_name(ifname);
302 printk(KERN_INFO DRV_NAME
303 ": %s: Interface %s does not exist!\n",
304 bond->dev->name, ifname);
311 if (dev->flags & IFF_UP) {
312 printk(KERN_ERR DRV_NAME
313 ": %s: Error: Unable to enslave %s "
314 "because it is already up.\n",
315 bond->dev->name, dev->name);
319 /* If this is the first slave, then we need to set
320 the master's hardware address to be the same as the
322 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
323 memcpy(bond->dev->dev_addr, dev->dev_addr,
327 /* Set the slave's MTU to match the bond */
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);
349 if (command[0] == '-') {
351 bond_for_each_slave(bond, slave, i)
352 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
357 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
358 bond->dev->name, dev->name);
360 res = bond_release(bond->dev, dev);
366 /* set the slave MTU to the default */
367 if (dev->change_mtu) {
368 dev->change_mtu(dev, 1500);
374 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
375 ifname, bond->dev->name);
382 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
389 static CLASS_DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
392 * Show and set the bonding mode. The bond interface must be down to
395 static ssize_t bonding_show_mode(struct class_device *cd, char *buf)
397 struct bonding *bond = to_bond(cd);
399 return sprintf(buf, "%s %d\n",
400 bond_mode_tbl[bond->params.mode].modename,
401 bond->params.mode) + 1;
404 static ssize_t bonding_store_mode(struct class_device *cd, const char *buf, size_t count)
406 int new_value, ret = count;
407 struct bonding *bond = to_bond(cd);
409 if (bond->dev->flags & IFF_UP) {
410 printk(KERN_ERR DRV_NAME
411 ": unable to update mode of %s because interface is up.\n",
417 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
419 printk(KERN_ERR DRV_NAME
420 ": %s: Ignoring invalid mode value %.*s.\n",
422 (int)strlen(buf) - 1, buf);
426 if (bond->params.mode == BOND_MODE_8023AD)
427 bond_unset_master_3ad_flags(bond);
429 if (bond->params.mode == BOND_MODE_ALB)
430 bond_unset_master_alb_flags(bond);
432 bond->params.mode = new_value;
433 bond_set_mode_ops(bond, bond->params.mode);
434 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
435 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
440 static CLASS_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
443 * Show and set the bonding transmit hash method. The bond interface must be down to
444 * change the xmit hash policy.
446 static ssize_t bonding_show_xmit_hash(struct class_device *cd, char *buf)
449 struct bonding *bond = to_bond(cd);
451 if ((bond->params.mode != BOND_MODE_XOR) &&
452 (bond->params.mode != BOND_MODE_8023AD)) {
454 count = sprintf(buf, "NA\n") + 1;
456 count = sprintf(buf, "%s %d\n",
457 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
458 bond->params.xmit_policy) + 1;
464 static ssize_t bonding_store_xmit_hash(struct class_device *cd, const char *buf, size_t count)
466 int new_value, ret = count;
467 struct bonding *bond = to_bond(cd);
469 if (bond->dev->flags & IFF_UP) {
470 printk(KERN_ERR DRV_NAME
471 "%s: Interface is up. Unable to update xmit policy.\n",
477 if ((bond->params.mode != BOND_MODE_XOR) &&
478 (bond->params.mode != BOND_MODE_8023AD)) {
479 printk(KERN_ERR DRV_NAME
480 "%s: Transmit hash policy is irrelevant in this mode.\n",
486 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
488 printk(KERN_ERR DRV_NAME
489 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
491 (int)strlen(buf) - 1, buf);
495 bond->params.xmit_policy = new_value;
496 bond_set_mode_ops(bond, bond->params.mode);
497 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
498 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
503 static CLASS_DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
506 * Show and set the arp timer interval. There are two tricky bits
507 * here. First, if ARP monitoring is activated, then we must disable
508 * MII monitoring. Second, if the ARP timer isn't running, we must
511 static ssize_t bonding_show_arp_interval(struct class_device *cd, char *buf)
513 struct bonding *bond = to_bond(cd);
515 return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
518 static ssize_t bonding_store_arp_interval(struct class_device *cd, const char *buf, size_t count)
520 int new_value, ret = count;
521 struct bonding *bond = to_bond(cd);
523 if (sscanf(buf, "%d", &new_value) != 1) {
524 printk(KERN_ERR DRV_NAME
525 ": %s: no arp_interval value specified.\n",
531 printk(KERN_ERR DRV_NAME
532 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
533 bond->dev->name, new_value, INT_MAX);
538 printk(KERN_INFO DRV_NAME
539 ": %s: Setting ARP monitoring interval to %d.\n",
540 bond->dev->name, new_value);
541 bond->params.arp_interval = new_value;
542 if (bond->params.miimon) {
543 printk(KERN_INFO DRV_NAME
544 ": %s: ARP monitoring cannot be used with MII monitoring. "
545 "%s Disabling MII monitoring.\n",
546 bond->dev->name, bond->dev->name);
547 bond->params.miimon = 0;
548 /* Kill MII timer, else it brings bond's link down */
549 if (bond->arp_timer.function) {
550 printk(KERN_INFO DRV_NAME
551 ": %s: Kill MII timer, else it brings bond's link down...\n",
553 del_timer_sync(&bond->mii_timer);
556 if (!bond->params.arp_targets[0]) {
557 printk(KERN_INFO DRV_NAME
558 ": %s: ARP monitoring has been set up, "
559 "but no ARP targets have been specified.\n",
562 if (bond->dev->flags & IFF_UP) {
563 /* If the interface is up, we may need to fire off
564 * the ARP timer. If the interface is down, the
565 * timer will get fired off when the open function
568 if (bond->arp_timer.function) {
569 /* The timer's already set up, so fire it off */
570 mod_timer(&bond->arp_timer, jiffies + 1);
572 /* Set up the timer. */
573 init_timer(&bond->arp_timer);
574 bond->arp_timer.expires = jiffies + 1;
575 bond->arp_timer.data =
576 (unsigned long) bond->dev;
577 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
578 bond->arp_timer.function =
580 &bond_activebackup_arp_mon;
582 bond->arp_timer.function =
584 &bond_loadbalance_arp_mon;
586 add_timer(&bond->arp_timer);
593 static CLASS_DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
596 * Show and set the arp targets.
598 static ssize_t bonding_show_arp_targets(struct class_device *cd, char *buf)
601 struct bonding *bond = to_bond(cd);
603 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
604 if (bond->params.arp_targets[i])
605 res += sprintf(buf + res, "%u.%u.%u.%u ",
606 NIPQUAD(bond->params.arp_targets[i]));
609 res--; /* eat the leftover space */
610 res += sprintf(buf + res, "\n");
615 static ssize_t bonding_store_arp_targets(struct class_device *cd, const char *buf, size_t count)
618 int i = 0, done = 0, ret = count;
619 struct bonding *bond = to_bond(cd);
622 targets = bond->params.arp_targets;
623 newtarget = in_aton(buf + 1);
626 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
627 printk(KERN_ERR DRV_NAME
628 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
629 bond->dev->name, NIPQUAD(newtarget));
633 /* look for an empty slot to put the target in, and check for dupes */
634 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
635 if (targets[i] == newtarget) { /* duplicate */
636 printk(KERN_ERR DRV_NAME
637 ": %s: ARP target %u.%u.%u.%u is already present\n",
638 bond->dev->name, NIPQUAD(newtarget));
644 if (targets[i] == 0 && !done) {
645 printk(KERN_INFO DRV_NAME
646 ": %s: adding ARP target %d.%d.%d.%d.\n",
647 bond->dev->name, NIPQUAD(newtarget));
649 targets[i] = newtarget;
653 printk(KERN_ERR DRV_NAME
654 ": %s: ARP target table is full!\n",
661 else if (buf[0] == '-') {
662 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
663 printk(KERN_ERR DRV_NAME
664 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
665 bond->dev->name, NIPQUAD(newtarget));
670 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
671 if (targets[i] == newtarget) {
672 printk(KERN_INFO DRV_NAME
673 ": %s: removing ARP target %d.%d.%d.%d.\n",
674 bond->dev->name, NIPQUAD(newtarget));
680 printk(KERN_INFO DRV_NAME
681 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
682 bond->dev->name, NIPQUAD(newtarget));
688 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
697 static CLASS_DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
700 * Show and set the up and down delays. These must be multiples of the
701 * MII monitoring value, and are stored internally as the multiplier.
702 * Thus, we must translate to MS for the real world.
704 static ssize_t bonding_show_downdelay(struct class_device *cd, char *buf)
706 struct bonding *bond = to_bond(cd);
708 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
711 static ssize_t bonding_store_downdelay(struct class_device *cd, const char *buf, size_t count)
713 int new_value, ret = count;
714 struct bonding *bond = to_bond(cd);
716 if (!(bond->params.miimon)) {
717 printk(KERN_ERR DRV_NAME
718 ": %s: Unable to set down delay as MII monitoring is disabled\n",
724 if (sscanf(buf, "%d", &new_value) != 1) {
725 printk(KERN_ERR DRV_NAME
726 ": %s: no down delay value specified.\n",
732 printk(KERN_ERR DRV_NAME
733 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
734 bond->dev->name, new_value, 1, INT_MAX);
738 if ((new_value % bond->params.miimon) != 0) {
739 printk(KERN_WARNING DRV_NAME
740 ": %s: Warning: down delay (%d) is not a multiple "
741 "of miimon (%d), delay rounded to %d ms\n",
742 bond->dev->name, new_value, bond->params.miimon,
743 (new_value / bond->params.miimon) *
744 bond->params.miimon);
746 bond->params.downdelay = new_value / bond->params.miimon;
747 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
748 bond->dev->name, bond->params.downdelay * bond->params.miimon);
755 static CLASS_DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
757 static ssize_t bonding_show_updelay(struct class_device *cd, char *buf)
759 struct bonding *bond = to_bond(cd);
761 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
765 static ssize_t bonding_store_updelay(struct class_device *cd, const char *buf, size_t count)
767 int new_value, ret = count;
768 struct bonding *bond = to_bond(cd);
770 if (!(bond->params.miimon)) {
771 printk(KERN_ERR DRV_NAME
772 ": %s: Unable to set up delay as MII monitoring is disabled\n",
778 if (sscanf(buf, "%d", &new_value) != 1) {
779 printk(KERN_ERR DRV_NAME
780 ": %s: no up delay value specified.\n",
786 printk(KERN_ERR DRV_NAME
787 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
788 bond->dev->name, new_value, 1, INT_MAX);
792 if ((new_value % bond->params.miimon) != 0) {
793 printk(KERN_WARNING DRV_NAME
794 ": %s: Warning: up delay (%d) is not a multiple "
795 "of miimon (%d), updelay rounded to %d ms\n",
796 bond->dev->name, new_value, bond->params.miimon,
797 (new_value / bond->params.miimon) *
798 bond->params.miimon);
800 bond->params.updelay = new_value / bond->params.miimon;
801 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
802 bond->dev->name, bond->params.updelay * bond->params.miimon);
809 static CLASS_DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
812 * Show and set the LACP interval. Interface must be down, and the mode
813 * must be set to 802.3ad mode.
815 static ssize_t bonding_show_lacp(struct class_device *cd, char *buf)
817 struct bonding *bond = to_bond(cd);
819 return sprintf(buf, "%s %d\n",
820 bond_lacp_tbl[bond->params.lacp_fast].modename,
821 bond->params.lacp_fast) + 1;
824 static ssize_t bonding_store_lacp(struct class_device *cd, const char *buf, size_t count)
826 int new_value, ret = count;
827 struct bonding *bond = to_bond(cd);
829 if (bond->dev->flags & IFF_UP) {
830 printk(KERN_ERR DRV_NAME
831 ": %s: Unable to update LACP rate because interface is up.\n",
837 if (bond->params.mode != BOND_MODE_8023AD) {
838 printk(KERN_ERR DRV_NAME
839 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
845 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
847 if ((new_value == 1) || (new_value == 0)) {
848 bond->params.lacp_fast = new_value;
849 printk(KERN_INFO DRV_NAME
850 ": %s: Setting LACP rate to %s (%d).\n",
851 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
853 printk(KERN_ERR DRV_NAME
854 ": %s: Ignoring invalid LACP rate value %.*s.\n",
855 bond->dev->name, (int)strlen(buf) - 1, buf);
861 static CLASS_DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
864 * Show and set the MII monitor interval. There are two tricky bits
865 * here. First, if MII monitoring is activated, then we must disable
866 * ARP monitoring. Second, if the timer isn't running, we must
869 static ssize_t bonding_show_miimon(struct class_device *cd, char *buf)
871 struct bonding *bond = to_bond(cd);
873 return sprintf(buf, "%d\n", bond->params.miimon) + 1;
876 static ssize_t bonding_store_miimon(struct class_device *cd, const char *buf, size_t count)
878 int new_value, ret = count;
879 struct bonding *bond = to_bond(cd);
881 if (sscanf(buf, "%d", &new_value) != 1) {
882 printk(KERN_ERR DRV_NAME
883 ": %s: no miimon value specified.\n",
889 printk(KERN_ERR DRV_NAME
890 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
891 bond->dev->name, new_value, 1, INT_MAX);
895 printk(KERN_INFO DRV_NAME
896 ": %s: Setting MII monitoring interval to %d.\n",
897 bond->dev->name, new_value);
898 bond->params.miimon = new_value;
899 if(bond->params.updelay)
900 printk(KERN_INFO DRV_NAME
901 ": %s: Note: Updating updelay (to %d) "
902 "since it is a multiple of the miimon value.\n",
904 bond->params.updelay * bond->params.miimon);
905 if(bond->params.downdelay)
906 printk(KERN_INFO DRV_NAME
907 ": %s: Note: Updating downdelay (to %d) "
908 "since it is a multiple of the miimon value.\n",
910 bond->params.downdelay * bond->params.miimon);
911 if (bond->params.arp_interval) {
912 printk(KERN_INFO DRV_NAME
913 ": %s: MII monitoring cannot be used with "
914 "ARP monitoring. Disabling ARP monitoring...\n",
916 bond->params.arp_interval = 0;
917 /* Kill ARP timer, else it brings bond's link down */
918 if (bond->mii_timer.function) {
919 printk(KERN_INFO DRV_NAME
920 ": %s: Kill ARP timer, else it brings bond's link down...\n",
922 del_timer_sync(&bond->arp_timer);
926 if (bond->dev->flags & IFF_UP) {
927 /* If the interface is up, we may need to fire off
928 * the MII timer. If the interface is down, the
929 * timer will get fired off when the open function
932 if (bond->mii_timer.function) {
933 /* The timer's already set up, so fire it off */
934 mod_timer(&bond->mii_timer, jiffies + 1);
936 /* Set up the timer. */
937 init_timer(&bond->mii_timer);
938 bond->mii_timer.expires = jiffies + 1;
939 bond->mii_timer.data =
940 (unsigned long) bond->dev;
941 bond->mii_timer.function =
942 (void *) &bond_mii_monitor;
943 add_timer(&bond->mii_timer);
950 static CLASS_DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
953 * Show and set the primary slave. The store function is much
954 * simpler than bonding_store_slaves function because it only needs to
955 * handle one interface name.
956 * The bond must be a mode that supports a primary for this be
959 static ssize_t bonding_show_primary(struct class_device *cd, char *buf)
962 struct bonding *bond = to_bond(cd);
964 if (bond->primary_slave)
965 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
967 count = sprintf(buf, "\n") + 1;
972 static ssize_t bonding_store_primary(struct class_device *cd, const char *buf, size_t count)
976 struct bonding *bond = to_bond(cd);
978 write_lock_bh(&bond->lock);
979 if (!USES_PRIMARY(bond->params.mode)) {
980 printk(KERN_INFO DRV_NAME
981 ": %s: Unable to set primary slave; %s is in mode %d\n",
982 bond->dev->name, bond->dev->name, bond->params.mode);
984 bond_for_each_slave(bond, slave, i) {
986 (slave->dev->name, buf,
987 strlen(slave->dev->name)) == 0) {
988 printk(KERN_INFO DRV_NAME
989 ": %s: Setting %s as primary slave.\n",
990 bond->dev->name, slave->dev->name);
991 bond->primary_slave = slave;
992 bond_select_active_slave(bond);
997 /* if we got here, then we didn't match the name of any slave */
999 if (strlen(buf) == 0 || buf[0] == '\n') {
1000 printk(KERN_INFO DRV_NAME
1001 ": %s: Setting primary slave to None.\n",
1003 bond->primary_slave = NULL;
1004 bond_select_active_slave(bond);
1006 printk(KERN_INFO DRV_NAME
1007 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1008 bond->dev->name, (int)strlen(buf) - 1, buf);
1012 write_unlock_bh(&bond->lock);
1015 static CLASS_DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
1018 * Show and set the use_carrier flag.
1020 static ssize_t bonding_show_carrier(struct class_device *cd, char *buf)
1022 struct bonding *bond = to_bond(cd);
1024 return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
1027 static ssize_t bonding_store_carrier(struct class_device *cd, const char *buf, size_t count)
1029 int new_value, ret = count;
1030 struct bonding *bond = to_bond(cd);
1033 if (sscanf(buf, "%d", &new_value) != 1) {
1034 printk(KERN_ERR DRV_NAME
1035 ": %s: no use_carrier value specified.\n",
1040 if ((new_value == 0) || (new_value == 1)) {
1041 bond->params.use_carrier = new_value;
1042 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1043 bond->dev->name, new_value);
1045 printk(KERN_INFO DRV_NAME
1046 ": %s: Ignoring invalid use_carrier value %d.\n",
1047 bond->dev->name, new_value);
1052 static CLASS_DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
1056 * Show and set currently active_slave.
1058 static ssize_t bonding_show_active_slave(struct class_device *cd, char *buf)
1061 struct bonding *bond = to_bond(cd);
1065 read_lock(&bond->curr_slave_lock);
1066 curr = bond->curr_active_slave;
1067 read_unlock(&bond->curr_slave_lock);
1069 if (USES_PRIMARY(bond->params.mode) && curr)
1070 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
1072 count = sprintf(buf, "\n") + 1;
1076 static ssize_t bonding_store_active_slave(struct class_device *cd, const char *buf, size_t count)
1079 struct slave *slave;
1080 struct slave *old_active = NULL;
1081 struct slave *new_active = NULL;
1082 struct bonding *bond = to_bond(cd);
1084 write_lock_bh(&bond->lock);
1085 if (!USES_PRIMARY(bond->params.mode)) {
1086 printk(KERN_INFO DRV_NAME
1087 ": %s: Unable to change active slave; %s is in mode %d\n",
1088 bond->dev->name, bond->dev->name, bond->params.mode);
1090 bond_for_each_slave(bond, slave, i) {
1092 (slave->dev->name, buf,
1093 strlen(slave->dev->name)) == 0) {
1094 old_active = bond->curr_active_slave;
1096 if (new_active == old_active) {
1098 printk(KERN_INFO DRV_NAME
1099 ": %s: %s is already the current active slave.\n",
1100 bond->dev->name, slave->dev->name);
1106 (new_active->link == BOND_LINK_UP) &&
1107 IS_UP(new_active->dev)) {
1108 printk(KERN_INFO DRV_NAME
1109 ": %s: Setting %s as active slave.\n",
1110 bond->dev->name, slave->dev->name);
1111 bond_change_active_slave(bond, new_active);
1114 printk(KERN_INFO DRV_NAME
1115 ": %s: Could not set %s as active slave; "
1116 "either %s is down or the link is down.\n",
1117 bond->dev->name, slave->dev->name,
1125 /* if we got here, then we didn't match the name of any slave */
1127 if (strlen(buf) == 0 || buf[0] == '\n') {
1128 printk(KERN_INFO DRV_NAME
1129 ": %s: Setting active slave to None.\n",
1131 bond->primary_slave = NULL;
1132 bond_select_active_slave(bond);
1134 printk(KERN_INFO DRV_NAME
1135 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1136 bond->dev->name, (int)strlen(buf) - 1, buf);
1140 write_unlock_bh(&bond->lock);
1144 static CLASS_DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
1148 * Show link status of the bond interface.
1150 static ssize_t bonding_show_mii_status(struct class_device *cd, char *buf)
1153 struct bonding *bond = to_bond(cd);
1155 read_lock(&bond->curr_slave_lock);
1156 curr = bond->curr_active_slave;
1157 read_unlock(&bond->curr_slave_lock);
1159 return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
1161 static CLASS_DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1165 * Show current 802.3ad aggregator ID.
1167 static ssize_t bonding_show_ad_aggregator(struct class_device *cd, char *buf)
1170 struct bonding *bond = to_bond(cd);
1172 if (bond->params.mode == BOND_MODE_8023AD) {
1173 struct ad_info ad_info;
1174 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
1177 count = sprintf(buf, "\n") + 1;
1181 static CLASS_DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1185 * Show number of active 802.3ad ports.
1187 static ssize_t bonding_show_ad_num_ports(struct class_device *cd, char *buf)
1190 struct bonding *bond = to_bond(cd);
1192 if (bond->params.mode == BOND_MODE_8023AD) {
1193 struct ad_info ad_info;
1194 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
1197 count = sprintf(buf, "\n") + 1;
1201 static CLASS_DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1205 * Show current 802.3ad actor key.
1207 static ssize_t bonding_show_ad_actor_key(struct class_device *cd, char *buf)
1210 struct bonding *bond = to_bond(cd);
1212 if (bond->params.mode == BOND_MODE_8023AD) {
1213 struct ad_info ad_info;
1214 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
1217 count = sprintf(buf, "\n") + 1;
1221 static CLASS_DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1225 * Show current 802.3ad partner key.
1227 static ssize_t bonding_show_ad_partner_key(struct class_device *cd, char *buf)
1230 struct bonding *bond = to_bond(cd);
1232 if (bond->params.mode == BOND_MODE_8023AD) {
1233 struct ad_info ad_info;
1234 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
1237 count = sprintf(buf, "\n") + 1;
1241 static CLASS_DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1245 * Show current 802.3ad partner mac.
1247 static ssize_t bonding_show_ad_partner_mac(struct class_device *cd, char *buf)
1250 struct bonding *bond = to_bond(cd);
1252 if (bond->params.mode == BOND_MODE_8023AD) {
1253 struct ad_info ad_info;
1254 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1255 count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
1256 ad_info.partner_system[0],
1257 ad_info.partner_system[1],
1258 ad_info.partner_system[2],
1259 ad_info.partner_system[3],
1260 ad_info.partner_system[4],
1261 ad_info.partner_system[5]) + 1;
1265 count = sprintf(buf, "\n") + 1;
1269 static CLASS_DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1273 static struct attribute *per_bond_attrs[] = {
1274 &class_device_attr_slaves.attr,
1275 &class_device_attr_mode.attr,
1276 &class_device_attr_arp_interval.attr,
1277 &class_device_attr_arp_ip_target.attr,
1278 &class_device_attr_downdelay.attr,
1279 &class_device_attr_updelay.attr,
1280 &class_device_attr_lacp_rate.attr,
1281 &class_device_attr_xmit_hash_policy.attr,
1282 &class_device_attr_miimon.attr,
1283 &class_device_attr_primary.attr,
1284 &class_device_attr_use_carrier.attr,
1285 &class_device_attr_active_slave.attr,
1286 &class_device_attr_mii_status.attr,
1287 &class_device_attr_ad_aggregator.attr,
1288 &class_device_attr_ad_num_ports.attr,
1289 &class_device_attr_ad_actor_key.attr,
1290 &class_device_attr_ad_partner_key.attr,
1291 &class_device_attr_ad_partner_mac.attr,
1295 static struct attribute_group bonding_group = {
1297 .attrs = per_bond_attrs,
1301 * Initialize sysfs. This sets up the bonding_masters file in
1304 int bond_create_sysfs(void)
1307 struct bonding *firstbond;
1309 init_rwsem(&bonding_rwsem);
1311 /* get the netdev class pointer */
1312 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1316 netdev_class = firstbond->dev->class_dev.class;
1320 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
1327 * Remove /sys/class/net/bonding_masters.
1329 void bond_destroy_sysfs(void)
1332 class_remove_file(netdev_class, &class_attr_bonding_masters);
1336 * Initialize sysfs for each bond. This sets up and registers
1337 * the 'bondctl' directory for each individual bond under /sys/class/net.
1339 int bond_create_sysfs_entry(struct bonding *bond)
1341 struct net_device *dev = bond->dev;
1344 err = sysfs_create_group(&(dev->class_dev.kobj), &bonding_group);
1346 printk(KERN_EMERG "eek! didn't create group!\n");
1349 if (expected_refcount < 1)
1350 expected_refcount = atomic_read(&bond->dev->class_dev.kobj.kref.refcount);
1355 * Remove sysfs entries for each bond.
1357 void bond_destroy_sysfs_entry(struct bonding *bond)
1359 struct net_device *dev = bond->dev;
1361 sysfs_remove_group(&(dev->class_dev.kobj), &bonding_group);