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/string.h>
35 #include <linux/ctype.h>
36 #include <linux/inet.h>
37 #include <linux/rtnetlink.h>
38 #include <net/net_namespace.h>
40 /* #define BONDING_DEBUG 1 */
42 #define to_dev(obj) container_of(obj,struct device,kobj)
43 #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
45 /*---------------------------- Declarations -------------------------------*/
48 extern struct list_head bond_dev_list;
49 extern struct bond_params bonding_defaults;
50 extern struct bond_parm_tbl bond_mode_tbl[];
51 extern struct bond_parm_tbl bond_lacp_tbl[];
52 extern struct bond_parm_tbl xmit_hashtype_tbl[];
53 extern struct bond_parm_tbl arp_validate_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->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 bond_deinit(bond->dev);
169 bond_destroy_sysfs_entry(bond);
170 unregister_netdevice(bond->dev);
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->dev.kobj), &(master->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->dev.kobj), &(slave->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->dev.kobj), "master");
220 sprintf(linkname,"slave_%s",slave->name);
221 sysfs_remove_link(&(master->dev.kobj), linkname);
226 * Show the slaves in the current bond.
228 static ssize_t bonding_show_slaves(struct device *d,
229 struct device_attribute *attr, char *buf)
233 struct bonding *bond = to_bond(d);
235 read_lock_bh(&bond->lock);
236 bond_for_each_slave(bond, slave, i) {
237 if (res > (PAGE_SIZE - IFNAMSIZ)) {
238 /* not enough space for another interface name */
239 if ((PAGE_SIZE - res) > 10)
240 res = PAGE_SIZE - 10;
241 res += sprintf(buf + res, "++more++");
244 res += sprintf(buf + res, "%s ", slave->dev->name);
246 read_unlock_bh(&bond->lock);
247 res += sprintf(buf + res, "\n");
253 * Set the slaves in the current bond. The bond interface must be
254 * up for this to succeed.
255 * This function is largely the same flow as bonding_update_bonds().
257 static ssize_t bonding_store_slaves(struct device *d,
258 struct device_attribute *attr,
259 const char *buffer, size_t count)
261 char command[IFNAMSIZ + 1] = { 0, };
263 int i, res, found, ret = count;
265 struct net_device *dev = NULL;
266 struct bonding *bond = to_bond(d);
268 /* Quick sanity check -- is the bond interface up? */
269 if (!(bond->dev->flags & IFF_UP)) {
270 printk(KERN_ERR DRV_NAME
271 ": %s: Unable to update slaves because interface is down.\n",
277 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
279 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
280 ifname = command + 1;
281 if ((strlen(command) <= 1) ||
282 !dev_valid_name(ifname))
285 if (command[0] == '+') {
287 /* Got a slave name in ifname. Is it already in the list? */
289 read_lock_bh(&bond->lock);
290 bond_for_each_slave(bond, slave, i)
291 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
292 printk(KERN_ERR DRV_NAME
293 ": %s: Interface %s is already enslaved!\n",
294 bond->dev->name, ifname);
296 read_unlock_bh(&bond->lock);
300 read_unlock_bh(&bond->lock);
301 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
302 bond->dev->name, ifname);
303 dev = dev_get_by_name(&init_net, ifname);
305 printk(KERN_INFO DRV_NAME
306 ": %s: Interface %s does not exist!\n",
307 bond->dev->name, ifname);
314 if (dev->flags & IFF_UP) {
315 printk(KERN_ERR DRV_NAME
316 ": %s: Error: Unable to enslave %s "
317 "because it is already up.\n",
318 bond->dev->name, dev->name);
322 /* If this is the first slave, then we need to set
323 the master's hardware address to be the same as the
325 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
326 memcpy(bond->dev->dev_addr, dev->dev_addr,
330 /* Set the slave's MTU to match the bond */
331 if (dev->mtu != bond->dev->mtu) {
332 if (dev->change_mtu) {
333 res = dev->change_mtu(dev,
340 dev->mtu = bond->dev->mtu;
344 res = bond_enslave(bond->dev, dev);
352 if (command[0] == '-') {
354 bond_for_each_slave(bond, slave, i)
355 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
360 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
361 bond->dev->name, dev->name);
363 res = bond_release(bond->dev, dev);
369 /* set the slave MTU to the default */
370 if (dev->change_mtu) {
371 dev->change_mtu(dev, 1500);
377 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
378 ifname, bond->dev->name);
385 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
392 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
395 * Show and set the bonding mode. The bond interface must be down to
398 static ssize_t bonding_show_mode(struct device *d,
399 struct device_attribute *attr, char *buf)
401 struct bonding *bond = to_bond(d);
403 return sprintf(buf, "%s %d\n",
404 bond_mode_tbl[bond->params.mode].modename,
405 bond->params.mode) + 1;
408 static ssize_t bonding_store_mode(struct device *d,
409 struct device_attribute *attr,
410 const char *buf, size_t count)
412 int new_value, ret = count;
413 struct bonding *bond = to_bond(d);
415 if (bond->dev->flags & IFF_UP) {
416 printk(KERN_ERR DRV_NAME
417 ": unable to update mode of %s because interface is up.\n",
423 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
425 printk(KERN_ERR DRV_NAME
426 ": %s: Ignoring invalid mode value %.*s.\n",
428 (int)strlen(buf) - 1, buf);
432 if (bond->params.mode == BOND_MODE_8023AD)
433 bond_unset_master_3ad_flags(bond);
435 if (bond->params.mode == BOND_MODE_ALB)
436 bond_unset_master_alb_flags(bond);
438 bond->params.mode = new_value;
439 bond_set_mode_ops(bond, bond->params.mode);
440 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
441 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
446 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
449 * Show and set the bonding transmit hash method. The bond interface must be down to
450 * change the xmit hash policy.
452 static ssize_t bonding_show_xmit_hash(struct device *d,
453 struct device_attribute *attr,
457 struct bonding *bond = to_bond(d);
459 if ((bond->params.mode != BOND_MODE_XOR) &&
460 (bond->params.mode != BOND_MODE_8023AD)) {
462 count = sprintf(buf, "NA\n") + 1;
464 count = sprintf(buf, "%s %d\n",
465 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
466 bond->params.xmit_policy) + 1;
472 static ssize_t bonding_store_xmit_hash(struct device *d,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
476 int new_value, ret = count;
477 struct bonding *bond = to_bond(d);
479 if (bond->dev->flags & IFF_UP) {
480 printk(KERN_ERR DRV_NAME
481 "%s: Interface is up. Unable to update xmit policy.\n",
487 if ((bond->params.mode != BOND_MODE_XOR) &&
488 (bond->params.mode != BOND_MODE_8023AD)) {
489 printk(KERN_ERR DRV_NAME
490 "%s: Transmit hash policy is irrelevant in this mode.\n",
496 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
498 printk(KERN_ERR DRV_NAME
499 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
501 (int)strlen(buf) - 1, buf);
505 bond->params.xmit_policy = new_value;
506 bond_set_mode_ops(bond, bond->params.mode);
507 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
508 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
513 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
516 * Show and set arp_validate.
518 static ssize_t bonding_show_arp_validate(struct device *d,
519 struct device_attribute *attr,
522 struct bonding *bond = to_bond(d);
524 return sprintf(buf, "%s %d\n",
525 arp_validate_tbl[bond->params.arp_validate].modename,
526 bond->params.arp_validate) + 1;
529 static ssize_t bonding_store_arp_validate(struct device *d,
530 struct device_attribute *attr,
531 const char *buf, size_t count)
534 struct bonding *bond = to_bond(d);
536 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
538 printk(KERN_ERR DRV_NAME
539 ": %s: Ignoring invalid arp_validate value %s\n",
540 bond->dev->name, buf);
543 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
544 printk(KERN_ERR DRV_NAME
545 ": %s: arp_validate only supported in active-backup mode.\n",
549 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
550 bond->dev->name, arp_validate_tbl[new_value].modename,
553 if (!bond->params.arp_validate && new_value) {
554 bond_register_arp(bond);
555 } else if (bond->params.arp_validate && !new_value) {
556 bond_unregister_arp(bond);
559 bond->params.arp_validate = new_value;
564 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
567 * Show and set the arp timer interval. There are two tricky bits
568 * here. First, if ARP monitoring is activated, then we must disable
569 * MII monitoring. Second, if the ARP timer isn't running, we must
572 static ssize_t bonding_show_arp_interval(struct device *d,
573 struct device_attribute *attr,
576 struct bonding *bond = to_bond(d);
578 return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
581 static ssize_t bonding_store_arp_interval(struct device *d,
582 struct device_attribute *attr,
583 const char *buf, size_t count)
585 int new_value, ret = count;
586 struct bonding *bond = to_bond(d);
588 if (sscanf(buf, "%d", &new_value) != 1) {
589 printk(KERN_ERR DRV_NAME
590 ": %s: no arp_interval value specified.\n",
596 printk(KERN_ERR DRV_NAME
597 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
598 bond->dev->name, new_value, INT_MAX);
603 printk(KERN_INFO DRV_NAME
604 ": %s: Setting ARP monitoring interval to %d.\n",
605 bond->dev->name, new_value);
606 bond->params.arp_interval = new_value;
607 if (bond->params.miimon) {
608 printk(KERN_INFO DRV_NAME
609 ": %s: ARP monitoring cannot be used with MII monitoring. "
610 "%s Disabling MII monitoring.\n",
611 bond->dev->name, bond->dev->name);
612 bond->params.miimon = 0;
613 /* Kill MII timer, else it brings bond's link down */
614 if (bond->arp_timer.function) {
615 printk(KERN_INFO DRV_NAME
616 ": %s: Kill MII timer, else it brings bond's link down...\n",
618 del_timer_sync(&bond->mii_timer);
621 if (!bond->params.arp_targets[0]) {
622 printk(KERN_INFO DRV_NAME
623 ": %s: ARP monitoring has been set up, "
624 "but no ARP targets have been specified.\n",
627 if (bond->dev->flags & IFF_UP) {
628 /* If the interface is up, we may need to fire off
629 * the ARP timer. If the interface is down, the
630 * timer will get fired off when the open function
633 if (bond->arp_timer.function) {
634 /* The timer's already set up, so fire it off */
635 mod_timer(&bond->arp_timer, jiffies + 1);
637 /* Set up the timer. */
638 init_timer(&bond->arp_timer);
639 bond->arp_timer.expires = jiffies + 1;
640 bond->arp_timer.data =
641 (unsigned long) bond->dev;
642 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
643 bond->arp_timer.function =
645 &bond_activebackup_arp_mon;
647 bond->arp_timer.function =
649 &bond_loadbalance_arp_mon;
651 add_timer(&bond->arp_timer);
658 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
661 * Show and set the arp targets.
663 static ssize_t bonding_show_arp_targets(struct device *d,
664 struct device_attribute *attr,
668 struct bonding *bond = to_bond(d);
670 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
671 if (bond->params.arp_targets[i])
672 res += sprintf(buf + res, "%u.%u.%u.%u ",
673 NIPQUAD(bond->params.arp_targets[i]));
676 res--; /* eat the leftover space */
677 res += sprintf(buf + res, "\n");
682 static ssize_t bonding_store_arp_targets(struct device *d,
683 struct device_attribute *attr,
684 const char *buf, size_t count)
687 int i = 0, done = 0, ret = count;
688 struct bonding *bond = to_bond(d);
691 targets = bond->params.arp_targets;
692 newtarget = in_aton(buf + 1);
695 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
696 printk(KERN_ERR DRV_NAME
697 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
698 bond->dev->name, NIPQUAD(newtarget));
702 /* look for an empty slot to put the target in, and check for dupes */
703 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
704 if (targets[i] == newtarget) { /* duplicate */
705 printk(KERN_ERR DRV_NAME
706 ": %s: ARP target %u.%u.%u.%u is already present\n",
707 bond->dev->name, NIPQUAD(newtarget));
713 if (targets[i] == 0 && !done) {
714 printk(KERN_INFO DRV_NAME
715 ": %s: adding ARP target %d.%d.%d.%d.\n",
716 bond->dev->name, NIPQUAD(newtarget));
718 targets[i] = newtarget;
722 printk(KERN_ERR DRV_NAME
723 ": %s: ARP target table is full!\n",
730 else if (buf[0] == '-') {
731 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
732 printk(KERN_ERR DRV_NAME
733 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
734 bond->dev->name, NIPQUAD(newtarget));
739 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
740 if (targets[i] == newtarget) {
741 printk(KERN_INFO DRV_NAME
742 ": %s: removing ARP target %d.%d.%d.%d.\n",
743 bond->dev->name, NIPQUAD(newtarget));
749 printk(KERN_INFO DRV_NAME
750 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
751 bond->dev->name, NIPQUAD(newtarget));
757 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
766 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
769 * Show and set the up and down delays. These must be multiples of the
770 * MII monitoring value, and are stored internally as the multiplier.
771 * Thus, we must translate to MS for the real world.
773 static ssize_t bonding_show_downdelay(struct device *d,
774 struct device_attribute *attr,
777 struct bonding *bond = to_bond(d);
779 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
782 static ssize_t bonding_store_downdelay(struct device *d,
783 struct device_attribute *attr,
784 const char *buf, size_t count)
786 int new_value, ret = count;
787 struct bonding *bond = to_bond(d);
789 if (!(bond->params.miimon)) {
790 printk(KERN_ERR DRV_NAME
791 ": %s: Unable to set down delay as MII monitoring is disabled\n",
797 if (sscanf(buf, "%d", &new_value) != 1) {
798 printk(KERN_ERR DRV_NAME
799 ": %s: no down delay value specified.\n",
805 printk(KERN_ERR DRV_NAME
806 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
807 bond->dev->name, new_value, 1, INT_MAX);
811 if ((new_value % bond->params.miimon) != 0) {
812 printk(KERN_WARNING DRV_NAME
813 ": %s: Warning: down delay (%d) is not a multiple "
814 "of miimon (%d), delay rounded to %d ms\n",
815 bond->dev->name, new_value, bond->params.miimon,
816 (new_value / bond->params.miimon) *
817 bond->params.miimon);
819 bond->params.downdelay = new_value / bond->params.miimon;
820 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
821 bond->dev->name, bond->params.downdelay * bond->params.miimon);
828 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
830 static ssize_t bonding_show_updelay(struct device *d,
831 struct device_attribute *attr,
834 struct bonding *bond = to_bond(d);
836 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
840 static ssize_t bonding_store_updelay(struct device *d,
841 struct device_attribute *attr,
842 const char *buf, size_t count)
844 int new_value, ret = count;
845 struct bonding *bond = to_bond(d);
847 if (!(bond->params.miimon)) {
848 printk(KERN_ERR DRV_NAME
849 ": %s: Unable to set up delay as MII monitoring is disabled\n",
855 if (sscanf(buf, "%d", &new_value) != 1) {
856 printk(KERN_ERR DRV_NAME
857 ": %s: no up delay value specified.\n",
863 printk(KERN_ERR DRV_NAME
864 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
865 bond->dev->name, new_value, 1, INT_MAX);
869 if ((new_value % bond->params.miimon) != 0) {
870 printk(KERN_WARNING DRV_NAME
871 ": %s: Warning: up delay (%d) is not a multiple "
872 "of miimon (%d), updelay rounded to %d ms\n",
873 bond->dev->name, new_value, bond->params.miimon,
874 (new_value / bond->params.miimon) *
875 bond->params.miimon);
877 bond->params.updelay = new_value / bond->params.miimon;
878 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
879 bond->dev->name, bond->params.updelay * bond->params.miimon);
886 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
889 * Show and set the LACP interval. Interface must be down, and the mode
890 * must be set to 802.3ad mode.
892 static ssize_t bonding_show_lacp(struct device *d,
893 struct device_attribute *attr,
896 struct bonding *bond = to_bond(d);
898 return sprintf(buf, "%s %d\n",
899 bond_lacp_tbl[bond->params.lacp_fast].modename,
900 bond->params.lacp_fast) + 1;
903 static ssize_t bonding_store_lacp(struct device *d,
904 struct device_attribute *attr,
905 const char *buf, size_t count)
907 int new_value, ret = count;
908 struct bonding *bond = to_bond(d);
910 if (bond->dev->flags & IFF_UP) {
911 printk(KERN_ERR DRV_NAME
912 ": %s: Unable to update LACP rate because interface is up.\n",
918 if (bond->params.mode != BOND_MODE_8023AD) {
919 printk(KERN_ERR DRV_NAME
920 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
926 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
928 if ((new_value == 1) || (new_value == 0)) {
929 bond->params.lacp_fast = new_value;
930 printk(KERN_INFO DRV_NAME
931 ": %s: Setting LACP rate to %s (%d).\n",
932 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
934 printk(KERN_ERR DRV_NAME
935 ": %s: Ignoring invalid LACP rate value %.*s.\n",
936 bond->dev->name, (int)strlen(buf) - 1, buf);
942 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
945 * Show and set the MII monitor interval. There are two tricky bits
946 * here. First, if MII monitoring is activated, then we must disable
947 * ARP monitoring. Second, if the timer isn't running, we must
950 static ssize_t bonding_show_miimon(struct device *d,
951 struct device_attribute *attr,
954 struct bonding *bond = to_bond(d);
956 return sprintf(buf, "%d\n", bond->params.miimon) + 1;
959 static ssize_t bonding_store_miimon(struct device *d,
960 struct device_attribute *attr,
961 const char *buf, size_t count)
963 int new_value, ret = count;
964 struct bonding *bond = to_bond(d);
966 if (sscanf(buf, "%d", &new_value) != 1) {
967 printk(KERN_ERR DRV_NAME
968 ": %s: no miimon value specified.\n",
974 printk(KERN_ERR DRV_NAME
975 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
976 bond->dev->name, new_value, 1, INT_MAX);
980 printk(KERN_INFO DRV_NAME
981 ": %s: Setting MII monitoring interval to %d.\n",
982 bond->dev->name, new_value);
983 bond->params.miimon = new_value;
984 if(bond->params.updelay)
985 printk(KERN_INFO DRV_NAME
986 ": %s: Note: Updating updelay (to %d) "
987 "since it is a multiple of the miimon value.\n",
989 bond->params.updelay * bond->params.miimon);
990 if(bond->params.downdelay)
991 printk(KERN_INFO DRV_NAME
992 ": %s: Note: Updating downdelay (to %d) "
993 "since it is a multiple of the miimon value.\n",
995 bond->params.downdelay * bond->params.miimon);
996 if (bond->params.arp_interval) {
997 printk(KERN_INFO DRV_NAME
998 ": %s: MII monitoring cannot be used with "
999 "ARP monitoring. Disabling ARP monitoring...\n",
1001 bond->params.arp_interval = 0;
1002 if (bond->params.arp_validate) {
1003 bond_unregister_arp(bond);
1004 bond->params.arp_validate =
1005 BOND_ARP_VALIDATE_NONE;
1007 /* Kill ARP timer, else it brings bond's link down */
1008 if (bond->mii_timer.function) {
1009 printk(KERN_INFO DRV_NAME
1010 ": %s: Kill ARP timer, else it brings bond's link down...\n",
1012 del_timer_sync(&bond->arp_timer);
1016 if (bond->dev->flags & IFF_UP) {
1017 /* If the interface is up, we may need to fire off
1018 * the MII timer. If the interface is down, the
1019 * timer will get fired off when the open function
1022 if (bond->mii_timer.function) {
1023 /* The timer's already set up, so fire it off */
1024 mod_timer(&bond->mii_timer, jiffies + 1);
1026 /* Set up the timer. */
1027 init_timer(&bond->mii_timer);
1028 bond->mii_timer.expires = jiffies + 1;
1029 bond->mii_timer.data =
1030 (unsigned long) bond->dev;
1031 bond->mii_timer.function =
1032 (void *) &bond_mii_monitor;
1033 add_timer(&bond->mii_timer);
1040 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
1043 * Show and set the primary slave. The store function is much
1044 * simpler than bonding_store_slaves function because it only needs to
1045 * handle one interface name.
1046 * The bond must be a mode that supports a primary for this be
1049 static ssize_t bonding_show_primary(struct device *d,
1050 struct device_attribute *attr,
1054 struct bonding *bond = to_bond(d);
1056 if (bond->primary_slave)
1057 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
1059 count = sprintf(buf, "\n") + 1;
1064 static ssize_t bonding_store_primary(struct device *d,
1065 struct device_attribute *attr,
1066 const char *buf, size_t count)
1069 struct slave *slave;
1070 struct bonding *bond = to_bond(d);
1072 write_lock_bh(&bond->lock);
1073 if (!USES_PRIMARY(bond->params.mode)) {
1074 printk(KERN_INFO DRV_NAME
1075 ": %s: Unable to set primary slave; %s is in mode %d\n",
1076 bond->dev->name, bond->dev->name, bond->params.mode);
1078 bond_for_each_slave(bond, slave, i) {
1080 (slave->dev->name, buf,
1081 strlen(slave->dev->name)) == 0) {
1082 printk(KERN_INFO DRV_NAME
1083 ": %s: Setting %s as primary slave.\n",
1084 bond->dev->name, slave->dev->name);
1085 bond->primary_slave = slave;
1086 bond_select_active_slave(bond);
1091 /* if we got here, then we didn't match the name of any slave */
1093 if (strlen(buf) == 0 || buf[0] == '\n') {
1094 printk(KERN_INFO DRV_NAME
1095 ": %s: Setting primary slave to None.\n",
1097 bond->primary_slave = NULL;
1098 bond_select_active_slave(bond);
1100 printk(KERN_INFO DRV_NAME
1101 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1102 bond->dev->name, (int)strlen(buf) - 1, buf);
1106 write_unlock_bh(&bond->lock);
1109 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
1112 * Show and set the use_carrier flag.
1114 static ssize_t bonding_show_carrier(struct device *d,
1115 struct device_attribute *attr,
1118 struct bonding *bond = to_bond(d);
1120 return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
1123 static ssize_t bonding_store_carrier(struct device *d,
1124 struct device_attribute *attr,
1125 const char *buf, size_t count)
1127 int new_value, ret = count;
1128 struct bonding *bond = to_bond(d);
1131 if (sscanf(buf, "%d", &new_value) != 1) {
1132 printk(KERN_ERR DRV_NAME
1133 ": %s: no use_carrier value specified.\n",
1138 if ((new_value == 0) || (new_value == 1)) {
1139 bond->params.use_carrier = new_value;
1140 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1141 bond->dev->name, new_value);
1143 printk(KERN_INFO DRV_NAME
1144 ": %s: Ignoring invalid use_carrier value %d.\n",
1145 bond->dev->name, new_value);
1150 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
1154 * Show and set currently active_slave.
1156 static ssize_t bonding_show_active_slave(struct device *d,
1157 struct device_attribute *attr,
1161 struct bonding *bond = to_bond(d);
1165 read_lock(&bond->curr_slave_lock);
1166 curr = bond->curr_active_slave;
1167 read_unlock(&bond->curr_slave_lock);
1169 if (USES_PRIMARY(bond->params.mode) && curr)
1170 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
1172 count = sprintf(buf, "\n") + 1;
1176 static ssize_t bonding_store_active_slave(struct device *d,
1177 struct device_attribute *attr,
1178 const char *buf, size_t count)
1181 struct slave *slave;
1182 struct slave *old_active = NULL;
1183 struct slave *new_active = NULL;
1184 struct bonding *bond = to_bond(d);
1186 write_lock_bh(&bond->lock);
1187 if (!USES_PRIMARY(bond->params.mode)) {
1188 printk(KERN_INFO DRV_NAME
1189 ": %s: Unable to change active slave; %s is in mode %d\n",
1190 bond->dev->name, bond->dev->name, bond->params.mode);
1192 bond_for_each_slave(bond, slave, i) {
1194 (slave->dev->name, buf,
1195 strlen(slave->dev->name)) == 0) {
1196 old_active = bond->curr_active_slave;
1198 if (new_active == old_active) {
1200 printk(KERN_INFO DRV_NAME
1201 ": %s: %s is already the current active slave.\n",
1202 bond->dev->name, slave->dev->name);
1208 (new_active->link == BOND_LINK_UP) &&
1209 IS_UP(new_active->dev)) {
1210 printk(KERN_INFO DRV_NAME
1211 ": %s: Setting %s as active slave.\n",
1212 bond->dev->name, slave->dev->name);
1213 bond_change_active_slave(bond, new_active);
1216 printk(KERN_INFO DRV_NAME
1217 ": %s: Could not set %s as active slave; "
1218 "either %s is down or the link is down.\n",
1219 bond->dev->name, slave->dev->name,
1227 /* if we got here, then we didn't match the name of any slave */
1229 if (strlen(buf) == 0 || buf[0] == '\n') {
1230 printk(KERN_INFO DRV_NAME
1231 ": %s: Setting active slave to None.\n",
1233 bond->primary_slave = NULL;
1234 bond_select_active_slave(bond);
1236 printk(KERN_INFO DRV_NAME
1237 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1238 bond->dev->name, (int)strlen(buf) - 1, buf);
1242 write_unlock_bh(&bond->lock);
1246 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
1250 * Show link status of the bond interface.
1252 static ssize_t bonding_show_mii_status(struct device *d,
1253 struct device_attribute *attr,
1257 struct bonding *bond = to_bond(d);
1259 read_lock(&bond->curr_slave_lock);
1260 curr = bond->curr_active_slave;
1261 read_unlock(&bond->curr_slave_lock);
1263 return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
1265 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1269 * Show current 802.3ad aggregator ID.
1271 static ssize_t bonding_show_ad_aggregator(struct device *d,
1272 struct device_attribute *attr,
1276 struct bonding *bond = to_bond(d);
1278 if (bond->params.mode == BOND_MODE_8023AD) {
1279 struct ad_info ad_info;
1280 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
1283 count = sprintf(buf, "\n") + 1;
1287 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1291 * Show number of active 802.3ad ports.
1293 static ssize_t bonding_show_ad_num_ports(struct device *d,
1294 struct device_attribute *attr,
1298 struct bonding *bond = to_bond(d);
1300 if (bond->params.mode == BOND_MODE_8023AD) {
1301 struct ad_info ad_info;
1302 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
1305 count = sprintf(buf, "\n") + 1;
1309 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1313 * Show current 802.3ad actor key.
1315 static ssize_t bonding_show_ad_actor_key(struct device *d,
1316 struct device_attribute *attr,
1320 struct bonding *bond = to_bond(d);
1322 if (bond->params.mode == BOND_MODE_8023AD) {
1323 struct ad_info ad_info;
1324 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
1327 count = sprintf(buf, "\n") + 1;
1331 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1335 * Show current 802.3ad partner key.
1337 static ssize_t bonding_show_ad_partner_key(struct device *d,
1338 struct device_attribute *attr,
1342 struct bonding *bond = to_bond(d);
1344 if (bond->params.mode == BOND_MODE_8023AD) {
1345 struct ad_info ad_info;
1346 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
1349 count = sprintf(buf, "\n") + 1;
1353 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1357 * Show current 802.3ad partner mac.
1359 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1360 struct device_attribute *attr,
1364 struct bonding *bond = to_bond(d);
1366 if (bond->params.mode == BOND_MODE_8023AD) {
1367 struct ad_info ad_info;
1368 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1369 count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
1370 ad_info.partner_system[0],
1371 ad_info.partner_system[1],
1372 ad_info.partner_system[2],
1373 ad_info.partner_system[3],
1374 ad_info.partner_system[4],
1375 ad_info.partner_system[5]) + 1;
1379 count = sprintf(buf, "\n") + 1;
1383 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1387 static struct attribute *per_bond_attrs[] = {
1388 &dev_attr_slaves.attr,
1389 &dev_attr_mode.attr,
1390 &dev_attr_arp_validate.attr,
1391 &dev_attr_arp_interval.attr,
1392 &dev_attr_arp_ip_target.attr,
1393 &dev_attr_downdelay.attr,
1394 &dev_attr_updelay.attr,
1395 &dev_attr_lacp_rate.attr,
1396 &dev_attr_xmit_hash_policy.attr,
1397 &dev_attr_miimon.attr,
1398 &dev_attr_primary.attr,
1399 &dev_attr_use_carrier.attr,
1400 &dev_attr_active_slave.attr,
1401 &dev_attr_mii_status.attr,
1402 &dev_attr_ad_aggregator.attr,
1403 &dev_attr_ad_num_ports.attr,
1404 &dev_attr_ad_actor_key.attr,
1405 &dev_attr_ad_partner_key.attr,
1406 &dev_attr_ad_partner_mac.attr,
1410 static struct attribute_group bonding_group = {
1412 .attrs = per_bond_attrs,
1416 * Initialize sysfs. This sets up the bonding_masters file in
1419 int bond_create_sysfs(void)
1422 struct bonding *firstbond;
1424 init_rwsem(&bonding_rwsem);
1426 /* get the netdev class pointer */
1427 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1431 netdev_class = firstbond->dev->dev.class;
1435 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
1437 * Permit multiple loads of the module by ignoring failures to
1438 * create the bonding_masters sysfs file. Bonding devices
1439 * created by second or subsequent loads of the module will
1440 * not be listed in, or controllable by, bonding_masters, but
1441 * will have the usual "bonding" sysfs directory.
1443 * This is done to preserve backwards compatibility for
1444 * initscripts/sysconfig, which load bonding multiple times to
1445 * configure multiple bonding devices.
1447 if (ret == -EEXIST) {
1448 netdev_class = NULL;
1457 * Remove /sys/class/net/bonding_masters.
1459 void bond_destroy_sysfs(void)
1462 class_remove_file(netdev_class, &class_attr_bonding_masters);
1466 * Initialize sysfs for each bond. This sets up and registers
1467 * the 'bondctl' directory for each individual bond under /sys/class/net.
1469 int bond_create_sysfs_entry(struct bonding *bond)
1471 struct net_device *dev = bond->dev;
1474 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
1476 printk(KERN_EMERG "eek! didn't create group!\n");
1479 if (expected_refcount < 1)
1480 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
1485 * Remove sysfs entries for each bond.
1487 void bond_destroy_sysfs_entry(struct bonding *bond)
1489 struct net_device *dev = bond->dev;
1491 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);