3 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/sysdev.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
33 #include <linux/sysfs.h>
34 #include <linux/ctype.h>
35 #include <linux/inet.h>
36 #include <linux/rtnetlink.h>
37 #include <net/net_namespace.h>
39 /* #define BONDING_DEBUG 1 */
41 #define to_dev(obj) container_of(obj,struct device,kobj)
42 #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
44 /*---------------------------- Declarations -------------------------------*/
47 extern struct list_head bond_dev_list;
48 extern struct bond_params bonding_defaults;
49 extern struct bond_parm_tbl bond_mode_tbl[];
50 extern struct bond_parm_tbl bond_lacp_tbl[];
51 extern struct bond_parm_tbl xmit_hashtype_tbl[];
52 extern struct bond_parm_tbl arp_validate_tbl[];
54 static int expected_refcount = -1;
55 static struct class *netdev_class;
56 /*--------------------------- Data Structures -----------------------------*/
58 /* Bonding sysfs lock. Why can't we just use the subsystem lock?
59 * Because kobject_register tries to acquire the subsystem lock. If
60 * we already hold the lock (which we would if the user was creating
61 * a new bond through the sysfs interface), we deadlock.
62 * This lock is only needed when deleting a bond - we need to make sure
63 * that we don't collide with an ongoing ioctl.
66 struct rw_semaphore bonding_rwsem;
71 /*------------------------------ Functions --------------------------------*/
74 * "show" function for the bond_masters attribute.
75 * The class parameter is ignored.
77 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
82 down_read(&(bonding_rwsem));
84 list_for_each_entry(bond, &bond_dev_list, bond_list) {
85 if (res > (PAGE_SIZE - IFNAMSIZ)) {
86 /* not enough space for another interface name */
87 if ((PAGE_SIZE - res) > 10)
89 res += sprintf(buf + res, "++more++ ");
92 res += sprintf(buf + res, "%s ", bond->dev->name);
94 if (res) buf[res-1] = '\n'; /* eat the leftover space */
95 up_read(&(bonding_rwsem));
100 * "store" function for the bond_masters attribute. This is what
101 * creates and deletes entire bonds.
103 * The class parameter is ignored.
107 static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
109 char command[IFNAMSIZ + 1] = {0, };
112 struct bonding *bond;
115 down_write(&(bonding_rwsem));
116 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
117 ifname = command + 1;
118 if ((strlen(command) <= 1) ||
119 !dev_valid_name(ifname))
122 if (command[0] == '+') {
124 /* Check to see if the bond already exists. */
125 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
126 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
127 printk(KERN_ERR DRV_NAME
128 ": cannot add bond %s; it already exists\n",
134 printk(KERN_INFO DRV_NAME
135 ": %s is being created...\n", ifname);
136 if (bond_create(ifname, &bonding_defaults, &bond)) {
137 printk(KERN_INFO DRV_NAME
138 ": %s interface already exists. Bond creation failed.\n",
145 if (command[0] == '-') {
146 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
147 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
149 /* check the ref count on the bond's kobject.
150 * If it's > expected, then there's a file open,
151 * and we have to fail.
153 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
154 > expected_refcount){
156 printk(KERN_INFO DRV_NAME
157 ": Unable remove bond %s due to open references.\n",
162 printk(KERN_INFO DRV_NAME
163 ": %s is being deleted...\n",
170 printk(KERN_ERR DRV_NAME
171 ": unable to delete non-existent bond %s\n", ifname);
177 printk(KERN_ERR DRV_NAME
178 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
181 /* Always return either count or an error. If you return 0, you'll
182 * get called forever, which is bad.
185 up_write(&(bonding_rwsem));
188 /* class attribute for bond_masters file. This ends up in /sys/class/net */
189 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
190 bonding_show_bonds, bonding_store_bonds);
192 int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
194 char linkname[IFNAMSIZ+7];
197 /* first, create a link from the slave back to the master */
198 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
202 /* next, create a link from the master to the slave */
203 sprintf(linkname,"slave_%s",slave->name);
204 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
210 void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
212 char linkname[IFNAMSIZ+7];
214 sysfs_remove_link(&(slave->dev.kobj), "master");
215 sprintf(linkname,"slave_%s",slave->name);
216 sysfs_remove_link(&(master->dev.kobj), linkname);
221 * Show the slaves in the current bond.
223 static ssize_t bonding_show_slaves(struct device *d,
224 struct device_attribute *attr, char *buf)
228 struct bonding *bond = to_bond(d);
230 read_lock(&bond->lock);
231 bond_for_each_slave(bond, slave, i) {
232 if (res > (PAGE_SIZE - IFNAMSIZ)) {
233 /* not enough space for another interface name */
234 if ((PAGE_SIZE - res) > 10)
235 res = PAGE_SIZE - 10;
236 res += sprintf(buf + res, "++more++ ");
239 res += sprintf(buf + res, "%s ", slave->dev->name);
241 read_unlock(&bond->lock);
242 if (res) buf[res-1] = '\n'; /* eat the leftover space */
247 * Set the slaves in the current bond. The bond interface must be
248 * up for this to succeed.
249 * This function is largely the same flow as bonding_update_bonds().
251 static ssize_t bonding_store_slaves(struct device *d,
252 struct device_attribute *attr,
253 const char *buffer, size_t count)
255 char command[IFNAMSIZ + 1] = { 0, };
257 int i, res, found, ret = count;
260 struct net_device *dev = NULL;
261 struct bonding *bond = to_bond(d);
263 /* Quick sanity check -- is the bond interface up? */
264 if (!(bond->dev->flags & IFF_UP)) {
265 printk(KERN_WARNING DRV_NAME
266 ": %s: doing slave updates when interface is down.\n",
270 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
272 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
273 ifname = command + 1;
274 if ((strlen(command) <= 1) ||
275 !dev_valid_name(ifname))
278 if (command[0] == '+') {
280 /* Got a slave name in ifname. Is it already in the list? */
282 read_lock(&bond->lock);
283 bond_for_each_slave(bond, slave, i)
284 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
285 printk(KERN_ERR DRV_NAME
286 ": %s: Interface %s is already enslaved!\n",
287 bond->dev->name, ifname);
289 read_unlock(&bond->lock);
293 read_unlock(&bond->lock);
294 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
295 bond->dev->name, ifname);
296 dev = dev_get_by_name(&init_net, ifname);
298 printk(KERN_INFO DRV_NAME
299 ": %s: Interface %s does not exist!\n",
300 bond->dev->name, ifname);
307 if (dev->flags & IFF_UP) {
308 printk(KERN_ERR DRV_NAME
309 ": %s: Error: Unable to enslave %s "
310 "because it is already up.\n",
311 bond->dev->name, dev->name);
315 /* If this is the first slave, then we need to set
316 the master's hardware address to be the same as the
318 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
319 memcpy(bond->dev->dev_addr, dev->dev_addr,
323 /* Set the slave's MTU to match the bond */
324 original_mtu = dev->mtu;
325 if (dev->mtu != bond->dev->mtu) {
326 if (dev->change_mtu) {
327 res = dev->change_mtu(dev,
334 dev->mtu = bond->dev->mtu;
338 res = bond_enslave(bond->dev, dev);
339 bond_for_each_slave(bond, slave, i)
340 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
341 slave->original_mtu = original_mtu;
349 if (command[0] == '-') {
351 bond_for_each_slave(bond, slave, i)
352 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
354 original_mtu = slave->original_mtu;
358 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
359 bond->dev->name, dev->name);
361 if (bond->setup_by_slave)
362 res = bond_release_and_destroy(bond->dev, dev);
364 res = bond_release(bond->dev, dev);
370 /* set the slave MTU to the default */
371 if (dev->change_mtu) {
372 dev->change_mtu(dev, original_mtu);
374 dev->mtu = original_mtu;
378 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
379 ifname, bond->dev->name);
386 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
393 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
396 * Show and set the bonding mode. The bond interface must be down to
399 static ssize_t bonding_show_mode(struct device *d,
400 struct device_attribute *attr, char *buf)
402 struct bonding *bond = to_bond(d);
404 return sprintf(buf, "%s %d\n",
405 bond_mode_tbl[bond->params.mode].modename,
409 static ssize_t bonding_store_mode(struct device *d,
410 struct device_attribute *attr,
411 const char *buf, size_t count)
413 int new_value, ret = count;
414 struct bonding *bond = to_bond(d);
416 if (bond->dev->flags & IFF_UP) {
417 printk(KERN_ERR DRV_NAME
418 ": unable to update mode of %s because interface is up.\n",
424 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
426 printk(KERN_ERR DRV_NAME
427 ": %s: Ignoring invalid mode value %.*s.\n",
429 (int)strlen(buf) - 1, buf);
433 if (bond->params.mode == BOND_MODE_8023AD)
434 bond_unset_master_3ad_flags(bond);
436 if (bond->params.mode == BOND_MODE_ALB)
437 bond_unset_master_alb_flags(bond);
439 bond->params.mode = new_value;
440 bond_set_mode_ops(bond, bond->params.mode);
441 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
442 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
447 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
450 * Show and set the bonding transmit hash method. The bond interface must be down to
451 * change the xmit hash policy.
453 static ssize_t bonding_show_xmit_hash(struct device *d,
454 struct device_attribute *attr,
458 struct bonding *bond = to_bond(d);
460 if ((bond->params.mode == BOND_MODE_XOR) ||
461 (bond->params.mode == BOND_MODE_8023AD)) {
462 count = sprintf(buf, "%s %d\n",
463 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
464 bond->params.xmit_policy);
470 static ssize_t bonding_store_xmit_hash(struct device *d,
471 struct device_attribute *attr,
472 const char *buf, size_t count)
474 int new_value, ret = count;
475 struct bonding *bond = to_bond(d);
477 if (bond->dev->flags & IFF_UP) {
478 printk(KERN_ERR DRV_NAME
479 "%s: Interface is up. Unable to update xmit policy.\n",
485 if ((bond->params.mode != BOND_MODE_XOR) &&
486 (bond->params.mode != BOND_MODE_8023AD)) {
487 printk(KERN_ERR DRV_NAME
488 "%s: Transmit hash policy is irrelevant in this mode.\n",
494 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
496 printk(KERN_ERR DRV_NAME
497 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
499 (int)strlen(buf) - 1, buf);
503 bond->params.xmit_policy = new_value;
504 bond_set_mode_ops(bond, bond->params.mode);
505 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
506 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
511 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
514 * Show and set arp_validate.
516 static ssize_t bonding_show_arp_validate(struct device *d,
517 struct device_attribute *attr,
520 struct bonding *bond = to_bond(d);
522 return sprintf(buf, "%s %d\n",
523 arp_validate_tbl[bond->params.arp_validate].modename,
524 bond->params.arp_validate);
527 static ssize_t bonding_store_arp_validate(struct device *d,
528 struct device_attribute *attr,
529 const char *buf, size_t count)
532 struct bonding *bond = to_bond(d);
534 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
536 printk(KERN_ERR DRV_NAME
537 ": %s: Ignoring invalid arp_validate value %s\n",
538 bond->dev->name, buf);
541 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
542 printk(KERN_ERR DRV_NAME
543 ": %s: arp_validate only supported in active-backup mode.\n",
547 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
548 bond->dev->name, arp_validate_tbl[new_value].modename,
551 if (!bond->params.arp_validate && new_value) {
552 bond_register_arp(bond);
553 } else if (bond->params.arp_validate && !new_value) {
554 bond_unregister_arp(bond);
557 bond->params.arp_validate = new_value;
562 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
565 * Show and store fail_over_mac. User only allowed to change the
566 * value when there are no slaves.
568 static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
570 struct bonding *bond = to_bond(d);
572 return sprintf(buf, "%d\n", bond->params.fail_over_mac) + 1;
575 static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
579 struct bonding *bond = to_bond(d);
581 if (bond->slave_cnt != 0) {
582 printk(KERN_ERR DRV_NAME
583 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
589 if (sscanf(buf, "%d", &new_value) != 1) {
590 printk(KERN_ERR DRV_NAME
591 ": %s: no fail_over_mac value specified.\n",
597 if ((new_value == 0) || (new_value == 1)) {
598 bond->params.fail_over_mac = new_value;
599 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %d.\n",
600 bond->dev->name, new_value);
602 printk(KERN_INFO DRV_NAME
603 ": %s: Ignoring invalid fail_over_mac value %d.\n",
604 bond->dev->name, new_value);
610 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
613 * Show and set the arp timer interval. There are two tricky bits
614 * here. First, if ARP monitoring is activated, then we must disable
615 * MII monitoring. Second, if the ARP timer isn't running, we must
618 static ssize_t bonding_show_arp_interval(struct device *d,
619 struct device_attribute *attr,
622 struct bonding *bond = to_bond(d);
624 return sprintf(buf, "%d\n", bond->params.arp_interval);
627 static ssize_t bonding_store_arp_interval(struct device *d,
628 struct device_attribute *attr,
629 const char *buf, size_t count)
631 int new_value, ret = count;
632 struct bonding *bond = to_bond(d);
634 if (sscanf(buf, "%d", &new_value) != 1) {
635 printk(KERN_ERR DRV_NAME
636 ": %s: no arp_interval value specified.\n",
642 printk(KERN_ERR DRV_NAME
643 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
644 bond->dev->name, new_value, INT_MAX);
649 printk(KERN_INFO DRV_NAME
650 ": %s: Setting ARP monitoring interval to %d.\n",
651 bond->dev->name, new_value);
652 bond->params.arp_interval = new_value;
653 if (bond->params.miimon) {
654 printk(KERN_INFO DRV_NAME
655 ": %s: ARP monitoring cannot be used with MII monitoring. "
656 "%s Disabling MII monitoring.\n",
657 bond->dev->name, bond->dev->name);
658 bond->params.miimon = 0;
659 if (delayed_work_pending(&bond->mii_work)) {
660 cancel_delayed_work(&bond->mii_work);
661 flush_workqueue(bond->wq);
664 if (!bond->params.arp_targets[0]) {
665 printk(KERN_INFO DRV_NAME
666 ": %s: ARP monitoring has been set up, "
667 "but no ARP targets have been specified.\n",
670 if (bond->dev->flags & IFF_UP) {
671 /* If the interface is up, we may need to fire off
672 * the ARP timer. If the interface is down, the
673 * timer will get fired off when the open function
676 if (!delayed_work_pending(&bond->arp_work)) {
677 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
678 INIT_DELAYED_WORK(&bond->arp_work,
679 bond_activebackup_arp_mon);
681 INIT_DELAYED_WORK(&bond->arp_work,
682 bond_loadbalance_arp_mon);
684 queue_delayed_work(bond->wq, &bond->arp_work, 0);
691 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
694 * Show and set the arp targets.
696 static ssize_t bonding_show_arp_targets(struct device *d,
697 struct device_attribute *attr,
701 struct bonding *bond = to_bond(d);
703 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
704 if (bond->params.arp_targets[i])
705 res += sprintf(buf + res, "%u.%u.%u.%u ",
706 NIPQUAD(bond->params.arp_targets[i]));
708 if (res) buf[res-1] = '\n'; /* eat the leftover space */
712 static ssize_t bonding_store_arp_targets(struct device *d,
713 struct device_attribute *attr,
714 const char *buf, size_t count)
717 int i = 0, done = 0, ret = count;
718 struct bonding *bond = to_bond(d);
721 targets = bond->params.arp_targets;
722 newtarget = in_aton(buf + 1);
725 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
726 printk(KERN_ERR DRV_NAME
727 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
728 bond->dev->name, NIPQUAD(newtarget));
732 /* look for an empty slot to put the target in, and check for dupes */
733 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
734 if (targets[i] == newtarget) { /* duplicate */
735 printk(KERN_ERR DRV_NAME
736 ": %s: ARP target %u.%u.%u.%u is already present\n",
737 bond->dev->name, NIPQUAD(newtarget));
743 if (targets[i] == 0 && !done) {
744 printk(KERN_INFO DRV_NAME
745 ": %s: adding ARP target %d.%d.%d.%d.\n",
746 bond->dev->name, NIPQUAD(newtarget));
748 targets[i] = newtarget;
752 printk(KERN_ERR DRV_NAME
753 ": %s: ARP target table is full!\n",
760 else if (buf[0] == '-') {
761 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
762 printk(KERN_ERR DRV_NAME
763 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
764 bond->dev->name, NIPQUAD(newtarget));
769 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
770 if (targets[i] == newtarget) {
771 printk(KERN_INFO DRV_NAME
772 ": %s: removing ARP target %d.%d.%d.%d.\n",
773 bond->dev->name, NIPQUAD(newtarget));
779 printk(KERN_INFO DRV_NAME
780 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
781 bond->dev->name, NIPQUAD(newtarget));
787 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
796 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
799 * Show and set the up and down delays. These must be multiples of the
800 * MII monitoring value, and are stored internally as the multiplier.
801 * Thus, we must translate to MS for the real world.
803 static ssize_t bonding_show_downdelay(struct device *d,
804 struct device_attribute *attr,
807 struct bonding *bond = to_bond(d);
809 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
812 static ssize_t bonding_store_downdelay(struct device *d,
813 struct device_attribute *attr,
814 const char *buf, size_t count)
816 int new_value, ret = count;
817 struct bonding *bond = to_bond(d);
819 if (!(bond->params.miimon)) {
820 printk(KERN_ERR DRV_NAME
821 ": %s: Unable to set down delay as MII monitoring is disabled\n",
827 if (sscanf(buf, "%d", &new_value) != 1) {
828 printk(KERN_ERR DRV_NAME
829 ": %s: no down delay value specified.\n",
835 printk(KERN_ERR DRV_NAME
836 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
837 bond->dev->name, new_value, 1, INT_MAX);
841 if ((new_value % bond->params.miimon) != 0) {
842 printk(KERN_WARNING DRV_NAME
843 ": %s: Warning: down delay (%d) is not a multiple "
844 "of miimon (%d), delay rounded to %d ms\n",
845 bond->dev->name, new_value, bond->params.miimon,
846 (new_value / bond->params.miimon) *
847 bond->params.miimon);
849 bond->params.downdelay = new_value / bond->params.miimon;
850 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
851 bond->dev->name, bond->params.downdelay * bond->params.miimon);
858 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
860 static ssize_t bonding_show_updelay(struct device *d,
861 struct device_attribute *attr,
864 struct bonding *bond = to_bond(d);
866 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
870 static ssize_t bonding_store_updelay(struct device *d,
871 struct device_attribute *attr,
872 const char *buf, size_t count)
874 int new_value, ret = count;
875 struct bonding *bond = to_bond(d);
877 if (!(bond->params.miimon)) {
878 printk(KERN_ERR DRV_NAME
879 ": %s: Unable to set up delay as MII monitoring is disabled\n",
885 if (sscanf(buf, "%d", &new_value) != 1) {
886 printk(KERN_ERR DRV_NAME
887 ": %s: no up delay value specified.\n",
893 printk(KERN_ERR DRV_NAME
894 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
895 bond->dev->name, new_value, 1, INT_MAX);
899 if ((new_value % bond->params.miimon) != 0) {
900 printk(KERN_WARNING DRV_NAME
901 ": %s: Warning: up delay (%d) is not a multiple "
902 "of miimon (%d), updelay rounded to %d ms\n",
903 bond->dev->name, new_value, bond->params.miimon,
904 (new_value / bond->params.miimon) *
905 bond->params.miimon);
907 bond->params.updelay = new_value / bond->params.miimon;
908 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
909 bond->dev->name, bond->params.updelay * bond->params.miimon);
916 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
919 * Show and set the LACP interval. Interface must be down, and the mode
920 * must be set to 802.3ad mode.
922 static ssize_t bonding_show_lacp(struct device *d,
923 struct device_attribute *attr,
926 struct bonding *bond = to_bond(d);
928 return sprintf(buf, "%s %d\n",
929 bond_lacp_tbl[bond->params.lacp_fast].modename,
930 bond->params.lacp_fast);
933 static ssize_t bonding_store_lacp(struct device *d,
934 struct device_attribute *attr,
935 const char *buf, size_t count)
937 int new_value, ret = count;
938 struct bonding *bond = to_bond(d);
940 if (bond->dev->flags & IFF_UP) {
941 printk(KERN_ERR DRV_NAME
942 ": %s: Unable to update LACP rate because interface is up.\n",
948 if (bond->params.mode != BOND_MODE_8023AD) {
949 printk(KERN_ERR DRV_NAME
950 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
956 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
958 if ((new_value == 1) || (new_value == 0)) {
959 bond->params.lacp_fast = new_value;
960 printk(KERN_INFO DRV_NAME
961 ": %s: Setting LACP rate to %s (%d).\n",
962 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
964 printk(KERN_ERR DRV_NAME
965 ": %s: Ignoring invalid LACP rate value %.*s.\n",
966 bond->dev->name, (int)strlen(buf) - 1, buf);
972 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
975 * Show and set the MII monitor interval. There are two tricky bits
976 * here. First, if MII monitoring is activated, then we must disable
977 * ARP monitoring. Second, if the timer isn't running, we must
980 static ssize_t bonding_show_miimon(struct device *d,
981 struct device_attribute *attr,
984 struct bonding *bond = to_bond(d);
986 return sprintf(buf, "%d\n", bond->params.miimon);
989 static ssize_t bonding_store_miimon(struct device *d,
990 struct device_attribute *attr,
991 const char *buf, size_t count)
993 int new_value, ret = count;
994 struct bonding *bond = to_bond(d);
996 if (sscanf(buf, "%d", &new_value) != 1) {
997 printk(KERN_ERR DRV_NAME
998 ": %s: no miimon value specified.\n",
1003 if (new_value < 0) {
1004 printk(KERN_ERR DRV_NAME
1005 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1006 bond->dev->name, new_value, 1, INT_MAX);
1010 printk(KERN_INFO DRV_NAME
1011 ": %s: Setting MII monitoring interval to %d.\n",
1012 bond->dev->name, new_value);
1013 bond->params.miimon = new_value;
1014 if(bond->params.updelay)
1015 printk(KERN_INFO DRV_NAME
1016 ": %s: Note: Updating updelay (to %d) "
1017 "since it is a multiple of the miimon value.\n",
1019 bond->params.updelay * bond->params.miimon);
1020 if(bond->params.downdelay)
1021 printk(KERN_INFO DRV_NAME
1022 ": %s: Note: Updating downdelay (to %d) "
1023 "since it is a multiple of the miimon value.\n",
1025 bond->params.downdelay * bond->params.miimon);
1026 if (bond->params.arp_interval) {
1027 printk(KERN_INFO DRV_NAME
1028 ": %s: MII monitoring cannot be used with "
1029 "ARP monitoring. Disabling ARP monitoring...\n",
1031 bond->params.arp_interval = 0;
1032 if (bond->params.arp_validate) {
1033 bond_unregister_arp(bond);
1034 bond->params.arp_validate =
1035 BOND_ARP_VALIDATE_NONE;
1037 if (delayed_work_pending(&bond->arp_work)) {
1038 cancel_delayed_work(&bond->arp_work);
1039 flush_workqueue(bond->wq);
1043 if (bond->dev->flags & IFF_UP) {
1044 /* If the interface is up, we may need to fire off
1045 * the MII timer. If the interface is down, the
1046 * timer will get fired off when the open function
1049 if (!delayed_work_pending(&bond->mii_work)) {
1050 INIT_DELAYED_WORK(&bond->mii_work,
1052 queue_delayed_work(bond->wq,
1053 &bond->mii_work, 0);
1060 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
1063 * Show and set the primary slave. The store function is much
1064 * simpler than bonding_store_slaves function because it only needs to
1065 * handle one interface name.
1066 * The bond must be a mode that supports a primary for this be
1069 static ssize_t bonding_show_primary(struct device *d,
1070 struct device_attribute *attr,
1074 struct bonding *bond = to_bond(d);
1076 if (bond->primary_slave)
1077 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1082 static ssize_t bonding_store_primary(struct device *d,
1083 struct device_attribute *attr,
1084 const char *buf, size_t count)
1087 struct slave *slave;
1088 struct bonding *bond = to_bond(d);
1090 write_lock_bh(&bond->lock);
1091 if (!USES_PRIMARY(bond->params.mode)) {
1092 printk(KERN_INFO DRV_NAME
1093 ": %s: Unable to set primary slave; %s is in mode %d\n",
1094 bond->dev->name, bond->dev->name, bond->params.mode);
1096 bond_for_each_slave(bond, slave, i) {
1098 (slave->dev->name, buf,
1099 strlen(slave->dev->name)) == 0) {
1100 printk(KERN_INFO DRV_NAME
1101 ": %s: Setting %s as primary slave.\n",
1102 bond->dev->name, slave->dev->name);
1103 bond->primary_slave = slave;
1104 bond_select_active_slave(bond);
1109 /* if we got here, then we didn't match the name of any slave */
1111 if (strlen(buf) == 0 || buf[0] == '\n') {
1112 printk(KERN_INFO DRV_NAME
1113 ": %s: Setting primary slave to None.\n",
1115 bond->primary_slave = NULL;
1116 bond_select_active_slave(bond);
1118 printk(KERN_INFO DRV_NAME
1119 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1120 bond->dev->name, (int)strlen(buf) - 1, buf);
1124 write_unlock_bh(&bond->lock);
1130 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
1133 * Show and set the use_carrier flag.
1135 static ssize_t bonding_show_carrier(struct device *d,
1136 struct device_attribute *attr,
1139 struct bonding *bond = to_bond(d);
1141 return sprintf(buf, "%d\n", bond->params.use_carrier);
1144 static ssize_t bonding_store_carrier(struct device *d,
1145 struct device_attribute *attr,
1146 const char *buf, size_t count)
1148 int new_value, ret = count;
1149 struct bonding *bond = to_bond(d);
1152 if (sscanf(buf, "%d", &new_value) != 1) {
1153 printk(KERN_ERR DRV_NAME
1154 ": %s: no use_carrier value specified.\n",
1159 if ((new_value == 0) || (new_value == 1)) {
1160 bond->params.use_carrier = new_value;
1161 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1162 bond->dev->name, new_value);
1164 printk(KERN_INFO DRV_NAME
1165 ": %s: Ignoring invalid use_carrier value %d.\n",
1166 bond->dev->name, new_value);
1171 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
1175 * Show and set currently active_slave.
1177 static ssize_t bonding_show_active_slave(struct device *d,
1178 struct device_attribute *attr,
1182 struct bonding *bond = to_bond(d);
1185 read_lock(&bond->curr_slave_lock);
1186 curr = bond->curr_active_slave;
1187 read_unlock(&bond->curr_slave_lock);
1189 if (USES_PRIMARY(bond->params.mode) && curr)
1190 count = sprintf(buf, "%s\n", curr->dev->name);
1194 static ssize_t bonding_store_active_slave(struct device *d,
1195 struct device_attribute *attr,
1196 const char *buf, size_t count)
1199 struct slave *slave;
1200 struct slave *old_active = NULL;
1201 struct slave *new_active = NULL;
1202 struct bonding *bond = to_bond(d);
1205 write_lock_bh(&bond->lock);
1207 if (!USES_PRIMARY(bond->params.mode)) {
1208 printk(KERN_INFO DRV_NAME
1209 ": %s: Unable to change active slave; %s is in mode %d\n",
1210 bond->dev->name, bond->dev->name, bond->params.mode);
1212 bond_for_each_slave(bond, slave, i) {
1214 (slave->dev->name, buf,
1215 strlen(slave->dev->name)) == 0) {
1216 old_active = bond->curr_active_slave;
1218 if (new_active == old_active) {
1220 printk(KERN_INFO DRV_NAME
1221 ": %s: %s is already the current active slave.\n",
1222 bond->dev->name, slave->dev->name);
1228 (new_active->link == BOND_LINK_UP) &&
1229 IS_UP(new_active->dev)) {
1230 printk(KERN_INFO DRV_NAME
1231 ": %s: Setting %s as active slave.\n",
1232 bond->dev->name, slave->dev->name);
1233 bond_change_active_slave(bond, new_active);
1236 printk(KERN_INFO DRV_NAME
1237 ": %s: Could not set %s as active slave; "
1238 "either %s is down or the link is down.\n",
1239 bond->dev->name, slave->dev->name,
1247 /* if we got here, then we didn't match the name of any slave */
1249 if (strlen(buf) == 0 || buf[0] == '\n') {
1250 printk(KERN_INFO DRV_NAME
1251 ": %s: Setting active slave to None.\n",
1253 bond->primary_slave = NULL;
1254 bond_select_active_slave(bond);
1256 printk(KERN_INFO DRV_NAME
1257 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1258 bond->dev->name, (int)strlen(buf) - 1, buf);
1262 write_unlock_bh(&bond->lock);
1268 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
1272 * Show link status of the bond interface.
1274 static ssize_t bonding_show_mii_status(struct device *d,
1275 struct device_attribute *attr,
1279 struct bonding *bond = to_bond(d);
1281 read_lock(&bond->curr_slave_lock);
1282 curr = bond->curr_active_slave;
1283 read_unlock(&bond->curr_slave_lock);
1285 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
1287 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1291 * Show current 802.3ad aggregator ID.
1293 static ssize_t bonding_show_ad_aggregator(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.aggregator_id);
1307 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1311 * Show number of active 802.3ad ports.
1313 static ssize_t bonding_show_ad_num_ports(struct device *d,
1314 struct device_attribute *attr,
1318 struct bonding *bond = to_bond(d);
1320 if (bond->params.mode == BOND_MODE_8023AD) {
1321 struct ad_info ad_info;
1322 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports);
1327 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1331 * Show current 802.3ad actor key.
1333 static ssize_t bonding_show_ad_actor_key(struct device *d,
1334 struct device_attribute *attr,
1338 struct bonding *bond = to_bond(d);
1340 if (bond->params.mode == BOND_MODE_8023AD) {
1341 struct ad_info ad_info;
1342 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key);
1347 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1351 * Show current 802.3ad partner key.
1353 static ssize_t bonding_show_ad_partner_key(struct device *d,
1354 struct device_attribute *attr,
1358 struct bonding *bond = to_bond(d);
1360 if (bond->params.mode == BOND_MODE_8023AD) {
1361 struct ad_info ad_info;
1362 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key);
1367 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1371 * Show current 802.3ad partner mac.
1373 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1374 struct device_attribute *attr,
1378 struct bonding *bond = to_bond(d);
1379 DECLARE_MAC_BUF(mac);
1381 if (bond->params.mode == BOND_MODE_8023AD) {
1382 struct ad_info ad_info;
1383 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1384 count = sprintf(buf,"%s\n",
1385 print_mac(mac, ad_info.partner_system));
1391 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1395 static struct attribute *per_bond_attrs[] = {
1396 &dev_attr_slaves.attr,
1397 &dev_attr_mode.attr,
1398 &dev_attr_fail_over_mac.attr,
1399 &dev_attr_arp_validate.attr,
1400 &dev_attr_arp_interval.attr,
1401 &dev_attr_arp_ip_target.attr,
1402 &dev_attr_downdelay.attr,
1403 &dev_attr_updelay.attr,
1404 &dev_attr_lacp_rate.attr,
1405 &dev_attr_xmit_hash_policy.attr,
1406 &dev_attr_miimon.attr,
1407 &dev_attr_primary.attr,
1408 &dev_attr_use_carrier.attr,
1409 &dev_attr_active_slave.attr,
1410 &dev_attr_mii_status.attr,
1411 &dev_attr_ad_aggregator.attr,
1412 &dev_attr_ad_num_ports.attr,
1413 &dev_attr_ad_actor_key.attr,
1414 &dev_attr_ad_partner_key.attr,
1415 &dev_attr_ad_partner_mac.attr,
1419 static struct attribute_group bonding_group = {
1421 .attrs = per_bond_attrs,
1425 * Initialize sysfs. This sets up the bonding_masters file in
1428 int bond_create_sysfs(void)
1431 struct bonding *firstbond;
1433 init_rwsem(&bonding_rwsem);
1435 /* get the netdev class pointer */
1436 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1440 netdev_class = firstbond->dev->dev.class;
1444 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
1446 * Permit multiple loads of the module by ignoring failures to
1447 * create the bonding_masters sysfs file. Bonding devices
1448 * created by second or subsequent loads of the module will
1449 * not be listed in, or controllable by, bonding_masters, but
1450 * will have the usual "bonding" sysfs directory.
1452 * This is done to preserve backwards compatibility for
1453 * initscripts/sysconfig, which load bonding multiple times to
1454 * configure multiple bonding devices.
1456 if (ret == -EEXIST) {
1457 netdev_class = NULL;
1466 * Remove /sys/class/net/bonding_masters.
1468 void bond_destroy_sysfs(void)
1471 class_remove_file(netdev_class, &class_attr_bonding_masters);
1475 * Initialize sysfs for each bond. This sets up and registers
1476 * the 'bondctl' directory for each individual bond under /sys/class/net.
1478 int bond_create_sysfs_entry(struct bonding *bond)
1480 struct net_device *dev = bond->dev;
1483 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
1485 printk(KERN_EMERG "eek! didn't create group!\n");
1488 if (expected_refcount < 1)
1489 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
1494 * Remove sysfs entries for each bond.
1496 void bond_destroy_sysfs_entry(struct bonding *bond)
1498 struct net_device *dev = bond->dev;
1500 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);