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>
41 #define to_dev(obj) container_of(obj,struct device,kobj)
42 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
44 /*---------------------------- Declarations -------------------------------*/
46 extern struct bond_params bonding_defaults;
47 extern struct bond_parm_tbl ad_select_tbl[];
49 static int expected_refcount = -1;
50 /*--------------------------- Data Structures -----------------------------*/
52 /* Bonding sysfs lock. Why can't we just use the subsystem lock?
53 * Because kobject_register tries to acquire the subsystem lock. If
54 * we already hold the lock (which we would if the user was creating
55 * a new bond through the sysfs interface), we deadlock.
56 * This lock is only needed when deleting a bond - we need to make sure
57 * that we don't collide with an ongoing ioctl.
60 struct rw_semaphore bonding_rwsem;
65 /*------------------------------ Functions --------------------------------*/
68 * "show" function for the bond_masters attribute.
69 * The class parameter is ignored.
71 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
76 down_read(&(bonding_rwsem));
78 list_for_each_entry(bond, &bond_dev_list, bond_list) {
79 if (res > (PAGE_SIZE - IFNAMSIZ)) {
80 /* not enough space for another interface name */
81 if ((PAGE_SIZE - res) > 10)
83 res += sprintf(buf + res, "++more++ ");
86 res += sprintf(buf + res, "%s ", bond->dev->name);
89 buf[res-1] = '\n'; /* eat the leftover space */
90 up_read(&(bonding_rwsem));
95 * "store" function for the bond_masters attribute. This is what
96 * creates and deletes entire bonds.
98 * The class parameter is ignored.
102 static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
104 char command[IFNAMSIZ + 1] = {0, };
107 struct bonding *bond;
109 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
110 ifname = command + 1;
111 if ((strlen(command) <= 1) ||
112 !dev_valid_name(ifname))
115 if (command[0] == '+') {
116 printk(KERN_INFO DRV_NAME
117 ": %s is being created...\n", ifname);
118 rv = bond_create(ifname, &bonding_defaults);
120 printk(KERN_INFO DRV_NAME ": Bond creation failed.\n");
126 if (command[0] == '-') {
128 down_write(&bonding_rwsem);
130 list_for_each_entry(bond, &bond_dev_list, bond_list)
131 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
132 /* check the ref count on the bond's kobject.
133 * If it's > expected, then there's a file open,
134 * and we have to fail.
136 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
137 > expected_refcount){
138 printk(KERN_INFO DRV_NAME
139 ": Unable remove bond %s due to open references.\n",
144 printk(KERN_INFO DRV_NAME
145 ": %s is being deleted...\n",
151 printk(KERN_ERR DRV_NAME
152 ": unable to delete non-existent bond %s\n", ifname);
158 printk(KERN_ERR DRV_NAME
159 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
163 up_write(&bonding_rwsem);
166 /* Always return either count or an error. If you return 0, you'll
167 * get called forever, which is bad.
172 /* class attribute for bond_masters file. This ends up in /sys/class/net */
173 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
174 bonding_show_bonds, bonding_store_bonds);
176 int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
178 char linkname[IFNAMSIZ+7];
181 /* first, create a link from the slave back to the master */
182 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
186 /* next, create a link from the master to the slave */
187 sprintf(linkname,"slave_%s",slave->name);
188 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
194 void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
196 char linkname[IFNAMSIZ+7];
198 sysfs_remove_link(&(slave->dev.kobj), "master");
199 sprintf(linkname,"slave_%s",slave->name);
200 sysfs_remove_link(&(master->dev.kobj), linkname);
205 * Show the slaves in the current bond.
207 static ssize_t bonding_show_slaves(struct device *d,
208 struct device_attribute *attr, char *buf)
212 struct bonding *bond = to_bond(d);
214 read_lock(&bond->lock);
215 bond_for_each_slave(bond, slave, i) {
216 if (res > (PAGE_SIZE - IFNAMSIZ)) {
217 /* not enough space for another interface name */
218 if ((PAGE_SIZE - res) > 10)
219 res = PAGE_SIZE - 10;
220 res += sprintf(buf + res, "++more++ ");
223 res += sprintf(buf + res, "%s ", slave->dev->name);
225 read_unlock(&bond->lock);
227 buf[res-1] = '\n'; /* eat the leftover space */
232 * Set the slaves in the current bond. The bond interface must be
233 * up for this to succeed.
234 * This function is largely the same flow as bonding_update_bonds().
236 static ssize_t bonding_store_slaves(struct device *d,
237 struct device_attribute *attr,
238 const char *buffer, size_t count)
240 char command[IFNAMSIZ + 1] = { 0, };
242 int i, res, found, ret = count;
245 struct net_device *dev = NULL;
246 struct bonding *bond = to_bond(d);
248 /* Quick sanity check -- is the bond interface up? */
249 if (!(bond->dev->flags & IFF_UP)) {
250 printk(KERN_WARNING DRV_NAME
251 ": %s: doing slave updates when interface is down.\n",
255 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
258 down_write(&(bonding_rwsem));
260 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
261 ifname = command + 1;
262 if ((strlen(command) <= 1) ||
263 !dev_valid_name(ifname))
266 if (command[0] == '+') {
268 /* Got a slave name in ifname. Is it already in the list? */
270 read_lock(&bond->lock);
271 bond_for_each_slave(bond, slave, i)
272 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
273 printk(KERN_ERR DRV_NAME
274 ": %s: Interface %s is already enslaved!\n",
275 bond->dev->name, ifname);
277 read_unlock(&bond->lock);
281 read_unlock(&bond->lock);
282 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
283 bond->dev->name, ifname);
284 dev = dev_get_by_name(&init_net, ifname);
286 printk(KERN_INFO DRV_NAME
287 ": %s: Interface %s does not exist!\n",
288 bond->dev->name, ifname);
295 if (dev->flags & IFF_UP) {
296 printk(KERN_ERR DRV_NAME
297 ": %s: Error: Unable to enslave %s "
298 "because it is already up.\n",
299 bond->dev->name, dev->name);
303 /* If this is the first slave, then we need to set
304 the master's hardware address to be the same as the
306 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
307 memcpy(bond->dev->dev_addr, dev->dev_addr,
311 /* Set the slave's MTU to match the bond */
312 original_mtu = dev->mtu;
313 res = dev_set_mtu(dev, bond->dev->mtu);
319 res = bond_enslave(bond->dev, dev);
320 bond_for_each_slave(bond, slave, i)
321 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
322 slave->original_mtu = original_mtu;
329 if (command[0] == '-') {
332 bond_for_each_slave(bond, slave, i)
333 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
335 original_mtu = slave->original_mtu;
339 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
340 bond->dev->name, dev->name);
341 res = bond_release(bond->dev, dev);
346 /* set the slave MTU to the default */
347 dev_set_mtu(dev, original_mtu);
350 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
351 ifname, bond->dev->name);
358 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
362 up_write(&(bonding_rwsem));
367 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
370 * Show and set the bonding mode. The bond interface must be down to
373 static ssize_t bonding_show_mode(struct device *d,
374 struct device_attribute *attr, char *buf)
376 struct bonding *bond = to_bond(d);
378 return sprintf(buf, "%s %d\n",
379 bond_mode_tbl[bond->params.mode].modename,
383 static ssize_t bonding_store_mode(struct device *d,
384 struct device_attribute *attr,
385 const char *buf, size_t count)
387 int new_value, ret = count;
388 struct bonding *bond = to_bond(d);
390 if (bond->dev->flags & IFF_UP) {
391 printk(KERN_ERR DRV_NAME
392 ": unable to update mode of %s because interface is up.\n",
398 new_value = bond_parse_parm(buf, bond_mode_tbl);
400 printk(KERN_ERR DRV_NAME
401 ": %s: Ignoring invalid mode value %.*s.\n",
403 (int)strlen(buf) - 1, buf);
407 if (bond->params.mode == BOND_MODE_8023AD)
408 bond_unset_master_3ad_flags(bond);
410 if (bond->params.mode == BOND_MODE_ALB)
411 bond_unset_master_alb_flags(bond);
413 bond->params.mode = new_value;
414 bond_set_mode_ops(bond, bond->params.mode);
415 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
416 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
421 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
424 * Show and set the bonding transmit hash method. The bond interface must be down to
425 * change the xmit hash policy.
427 static ssize_t bonding_show_xmit_hash(struct device *d,
428 struct device_attribute *attr,
431 struct bonding *bond = to_bond(d);
433 return sprintf(buf, "%s %d\n",
434 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
435 bond->params.xmit_policy);
438 static ssize_t bonding_store_xmit_hash(struct device *d,
439 struct device_attribute *attr,
440 const char *buf, size_t count)
442 int new_value, ret = count;
443 struct bonding *bond = to_bond(d);
445 if (bond->dev->flags & IFF_UP) {
446 printk(KERN_ERR DRV_NAME
447 "%s: Interface is up. Unable to update xmit policy.\n",
453 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
455 printk(KERN_ERR DRV_NAME
456 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
458 (int)strlen(buf) - 1, buf);
462 bond->params.xmit_policy = new_value;
463 bond_set_mode_ops(bond, bond->params.mode);
464 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
465 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
470 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
473 * Show and set arp_validate.
475 static ssize_t bonding_show_arp_validate(struct device *d,
476 struct device_attribute *attr,
479 struct bonding *bond = to_bond(d);
481 return sprintf(buf, "%s %d\n",
482 arp_validate_tbl[bond->params.arp_validate].modename,
483 bond->params.arp_validate);
486 static ssize_t bonding_store_arp_validate(struct device *d,
487 struct device_attribute *attr,
488 const char *buf, size_t count)
491 struct bonding *bond = to_bond(d);
493 new_value = bond_parse_parm(buf, arp_validate_tbl);
495 printk(KERN_ERR DRV_NAME
496 ": %s: Ignoring invalid arp_validate value %s\n",
497 bond->dev->name, buf);
500 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
501 printk(KERN_ERR DRV_NAME
502 ": %s: arp_validate only supported in active-backup mode.\n",
506 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
507 bond->dev->name, arp_validate_tbl[new_value].modename,
510 if (!bond->params.arp_validate && new_value) {
511 bond_register_arp(bond);
512 } else if (bond->params.arp_validate && !new_value) {
513 bond_unregister_arp(bond);
516 bond->params.arp_validate = new_value;
521 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
524 * Show and store fail_over_mac. User only allowed to change the
525 * value when there are no slaves.
527 static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
529 struct bonding *bond = to_bond(d);
531 return sprintf(buf, "%s %d\n",
532 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
533 bond->params.fail_over_mac);
536 static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
539 struct bonding *bond = to_bond(d);
541 if (bond->slave_cnt != 0) {
542 printk(KERN_ERR DRV_NAME
543 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
548 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
550 printk(KERN_ERR DRV_NAME
551 ": %s: Ignoring invalid fail_over_mac value %s.\n",
552 bond->dev->name, buf);
556 bond->params.fail_over_mac = new_value;
557 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
558 bond->dev->name, fail_over_mac_tbl[new_value].modename,
564 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
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);
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.arp_interval)
608 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
609 if (bond->params.miimon) {
610 printk(KERN_INFO DRV_NAME
611 ": %s: ARP monitoring cannot be used with MII monitoring. "
612 "%s Disabling MII monitoring.\n",
613 bond->dev->name, bond->dev->name);
614 bond->params.miimon = 0;
615 if (delayed_work_pending(&bond->mii_work)) {
616 cancel_delayed_work(&bond->mii_work);
617 flush_workqueue(bond->wq);
620 if (!bond->params.arp_targets[0]) {
621 printk(KERN_INFO DRV_NAME
622 ": %s: ARP monitoring has been set up, "
623 "but no ARP targets have been specified.\n",
626 if (bond->dev->flags & IFF_UP) {
627 /* If the interface is up, we may need to fire off
628 * the ARP timer. If the interface is down, the
629 * timer will get fired off when the open function
632 if (!delayed_work_pending(&bond->arp_work)) {
633 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
634 INIT_DELAYED_WORK(&bond->arp_work,
635 bond_activebackup_arp_mon);
637 INIT_DELAYED_WORK(&bond->arp_work,
638 bond_loadbalance_arp_mon);
640 queue_delayed_work(bond->wq, &bond->arp_work, 0);
647 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
650 * Show and set the arp targets.
652 static ssize_t bonding_show_arp_targets(struct device *d,
653 struct device_attribute *attr,
657 struct bonding *bond = to_bond(d);
659 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
660 if (bond->params.arp_targets[i])
661 res += sprintf(buf + res, "%pI4 ",
662 &bond->params.arp_targets[i]);
665 buf[res-1] = '\n'; /* eat the leftover space */
669 static ssize_t bonding_store_arp_targets(struct device *d,
670 struct device_attribute *attr,
671 const char *buf, size_t count)
674 int i = 0, done = 0, ret = count;
675 struct bonding *bond = to_bond(d);
678 targets = bond->params.arp_targets;
679 newtarget = in_aton(buf + 1);
682 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
683 printk(KERN_ERR DRV_NAME
684 ": %s: invalid ARP target %pI4 specified for addition\n",
685 bond->dev->name, &newtarget);
689 /* look for an empty slot to put the target in, and check for dupes */
690 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
691 if (targets[i] == newtarget) { /* duplicate */
692 printk(KERN_ERR DRV_NAME
693 ": %s: ARP target %pI4 is already present\n",
694 bond->dev->name, &newtarget);
700 if (targets[i] == 0 && !done) {
701 printk(KERN_INFO DRV_NAME
702 ": %s: adding ARP target %pI4.\n",
703 bond->dev->name, &newtarget);
705 targets[i] = newtarget;
709 printk(KERN_ERR DRV_NAME
710 ": %s: ARP target table is full!\n",
717 else if (buf[0] == '-') {
718 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
719 printk(KERN_ERR DRV_NAME
720 ": %s: invalid ARP target %pI4 specified for removal\n",
721 bond->dev->name, &newtarget);
726 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
727 if (targets[i] == newtarget) {
728 printk(KERN_INFO DRV_NAME
729 ": %s: removing ARP target %pI4.\n",
730 bond->dev->name, &newtarget);
736 printk(KERN_INFO DRV_NAME
737 ": %s: unable to remove nonexistent ARP target %pI4.\n",
738 bond->dev->name, &newtarget);
744 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
753 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
756 * Show and set the up and down delays. These must be multiples of the
757 * MII monitoring value, and are stored internally as the multiplier.
758 * Thus, we must translate to MS for the real world.
760 static ssize_t bonding_show_downdelay(struct device *d,
761 struct device_attribute *attr,
764 struct bonding *bond = to_bond(d);
766 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
769 static ssize_t bonding_store_downdelay(struct device *d,
770 struct device_attribute *attr,
771 const char *buf, size_t count)
773 int new_value, ret = count;
774 struct bonding *bond = to_bond(d);
776 if (!(bond->params.miimon)) {
777 printk(KERN_ERR DRV_NAME
778 ": %s: Unable to set down delay as MII monitoring is disabled\n",
784 if (sscanf(buf, "%d", &new_value) != 1) {
785 printk(KERN_ERR DRV_NAME
786 ": %s: no down delay value specified.\n",
792 printk(KERN_ERR DRV_NAME
793 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
794 bond->dev->name, new_value, 1, INT_MAX);
798 if ((new_value % bond->params.miimon) != 0) {
799 printk(KERN_WARNING DRV_NAME
800 ": %s: Warning: down delay (%d) is not a multiple "
801 "of miimon (%d), delay rounded to %d ms\n",
802 bond->dev->name, new_value, bond->params.miimon,
803 (new_value / bond->params.miimon) *
804 bond->params.miimon);
806 bond->params.downdelay = new_value / bond->params.miimon;
807 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
808 bond->dev->name, bond->params.downdelay * bond->params.miimon);
815 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
817 static ssize_t bonding_show_updelay(struct device *d,
818 struct device_attribute *attr,
821 struct bonding *bond = to_bond(d);
823 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
827 static ssize_t bonding_store_updelay(struct device *d,
828 struct device_attribute *attr,
829 const char *buf, size_t count)
831 int new_value, ret = count;
832 struct bonding *bond = to_bond(d);
834 if (!(bond->params.miimon)) {
835 printk(KERN_ERR DRV_NAME
836 ": %s: Unable to set up delay as MII monitoring is disabled\n",
842 if (sscanf(buf, "%d", &new_value) != 1) {
843 printk(KERN_ERR DRV_NAME
844 ": %s: no up delay value specified.\n",
850 printk(KERN_ERR DRV_NAME
851 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
852 bond->dev->name, new_value, 1, INT_MAX);
856 if ((new_value % bond->params.miimon) != 0) {
857 printk(KERN_WARNING DRV_NAME
858 ": %s: Warning: up delay (%d) is not a multiple "
859 "of miimon (%d), updelay rounded to %d ms\n",
860 bond->dev->name, new_value, bond->params.miimon,
861 (new_value / bond->params.miimon) *
862 bond->params.miimon);
864 bond->params.updelay = new_value / bond->params.miimon;
865 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
866 bond->dev->name, bond->params.updelay * bond->params.miimon);
873 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
876 * Show and set the LACP interval. Interface must be down, and the mode
877 * must be set to 802.3ad mode.
879 static ssize_t bonding_show_lacp(struct device *d,
880 struct device_attribute *attr,
883 struct bonding *bond = to_bond(d);
885 return sprintf(buf, "%s %d\n",
886 bond_lacp_tbl[bond->params.lacp_fast].modename,
887 bond->params.lacp_fast);
890 static ssize_t bonding_store_lacp(struct device *d,
891 struct device_attribute *attr,
892 const char *buf, size_t count)
894 int new_value, ret = count;
895 struct bonding *bond = to_bond(d);
897 if (bond->dev->flags & IFF_UP) {
898 printk(KERN_ERR DRV_NAME
899 ": %s: Unable to update LACP rate because interface is up.\n",
905 if (bond->params.mode != BOND_MODE_8023AD) {
906 printk(KERN_ERR DRV_NAME
907 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
913 new_value = bond_parse_parm(buf, bond_lacp_tbl);
915 if ((new_value == 1) || (new_value == 0)) {
916 bond->params.lacp_fast = new_value;
917 printk(KERN_INFO DRV_NAME
918 ": %s: Setting LACP rate to %s (%d).\n",
919 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
921 printk(KERN_ERR DRV_NAME
922 ": %s: Ignoring invalid LACP rate value %.*s.\n",
923 bond->dev->name, (int)strlen(buf) - 1, buf);
929 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
931 static ssize_t bonding_show_ad_select(struct device *d,
932 struct device_attribute *attr,
935 struct bonding *bond = to_bond(d);
937 return sprintf(buf, "%s %d\n",
938 ad_select_tbl[bond->params.ad_select].modename,
939 bond->params.ad_select);
943 static ssize_t bonding_store_ad_select(struct device *d,
944 struct device_attribute *attr,
945 const char *buf, size_t count)
947 int new_value, ret = count;
948 struct bonding *bond = to_bond(d);
950 if (bond->dev->flags & IFF_UP) {
951 printk(KERN_ERR DRV_NAME
952 ": %s: Unable to update ad_select because interface "
953 "is up.\n", bond->dev->name);
958 new_value = bond_parse_parm(buf, ad_select_tbl);
960 if (new_value != -1) {
961 bond->params.ad_select = new_value;
962 printk(KERN_INFO DRV_NAME
963 ": %s: Setting ad_select to %s (%d).\n",
964 bond->dev->name, ad_select_tbl[new_value].modename,
967 printk(KERN_ERR DRV_NAME
968 ": %s: Ignoring invalid ad_select value %.*s.\n",
969 bond->dev->name, (int)strlen(buf) - 1, buf);
976 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR, bonding_show_ad_select, bonding_store_ad_select);
979 * Show and set the number of grat ARP to send after a failover event.
981 static ssize_t bonding_show_n_grat_arp(struct device *d,
982 struct device_attribute *attr,
985 struct bonding *bond = to_bond(d);
987 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
990 static ssize_t bonding_store_n_grat_arp(struct device *d,
991 struct device_attribute *attr,
992 const char *buf, size_t count)
994 int new_value, ret = count;
995 struct bonding *bond = to_bond(d);
997 if (sscanf(buf, "%d", &new_value) != 1) {
998 printk(KERN_ERR DRV_NAME
999 ": %s: no num_grat_arp value specified.\n",
1004 if (new_value < 0 || new_value > 255) {
1005 printk(KERN_ERR DRV_NAME
1006 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1007 bond->dev->name, new_value);
1011 bond->params.num_grat_arp = new_value;
1016 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR, bonding_show_n_grat_arp, bonding_store_n_grat_arp);
1019 * Show and set the number of unsolicted NA's to send after a failover event.
1021 static ssize_t bonding_show_n_unsol_na(struct device *d,
1022 struct device_attribute *attr,
1025 struct bonding *bond = to_bond(d);
1027 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1030 static ssize_t bonding_store_n_unsol_na(struct device *d,
1031 struct device_attribute *attr,
1032 const char *buf, size_t count)
1034 int new_value, ret = count;
1035 struct bonding *bond = to_bond(d);
1037 if (sscanf(buf, "%d", &new_value) != 1) {
1038 printk(KERN_ERR DRV_NAME
1039 ": %s: no num_unsol_na value specified.\n",
1044 if (new_value < 0 || new_value > 255) {
1045 printk(KERN_ERR DRV_NAME
1046 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1047 bond->dev->name, new_value);
1051 bond->params.num_unsol_na = new_value;
1056 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR, bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1059 * Show and set the MII monitor interval. There are two tricky bits
1060 * here. First, if MII monitoring is activated, then we must disable
1061 * ARP monitoring. Second, if the timer isn't running, we must
1064 static ssize_t bonding_show_miimon(struct device *d,
1065 struct device_attribute *attr,
1068 struct bonding *bond = to_bond(d);
1070 return sprintf(buf, "%d\n", bond->params.miimon);
1073 static ssize_t bonding_store_miimon(struct device *d,
1074 struct device_attribute *attr,
1075 const char *buf, size_t count)
1077 int new_value, ret = count;
1078 struct bonding *bond = to_bond(d);
1080 if (sscanf(buf, "%d", &new_value) != 1) {
1081 printk(KERN_ERR DRV_NAME
1082 ": %s: no miimon value specified.\n",
1087 if (new_value < 0) {
1088 printk(KERN_ERR DRV_NAME
1089 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1090 bond->dev->name, new_value, 1, INT_MAX);
1094 printk(KERN_INFO DRV_NAME
1095 ": %s: Setting MII monitoring interval to %d.\n",
1096 bond->dev->name, new_value);
1097 bond->params.miimon = new_value;
1098 if(bond->params.updelay)
1099 printk(KERN_INFO DRV_NAME
1100 ": %s: Note: Updating updelay (to %d) "
1101 "since it is a multiple of the miimon value.\n",
1103 bond->params.updelay * bond->params.miimon);
1104 if(bond->params.downdelay)
1105 printk(KERN_INFO DRV_NAME
1106 ": %s: Note: Updating downdelay (to %d) "
1107 "since it is a multiple of the miimon value.\n",
1109 bond->params.downdelay * bond->params.miimon);
1110 if (bond->params.arp_interval) {
1111 printk(KERN_INFO DRV_NAME
1112 ": %s: MII monitoring cannot be used with "
1113 "ARP monitoring. Disabling ARP monitoring...\n",
1115 bond->params.arp_interval = 0;
1116 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1117 if (bond->params.arp_validate) {
1118 bond_unregister_arp(bond);
1119 bond->params.arp_validate =
1120 BOND_ARP_VALIDATE_NONE;
1122 if (delayed_work_pending(&bond->arp_work)) {
1123 cancel_delayed_work(&bond->arp_work);
1124 flush_workqueue(bond->wq);
1128 if (bond->dev->flags & IFF_UP) {
1129 /* If the interface is up, we may need to fire off
1130 * the MII timer. If the interface is down, the
1131 * timer will get fired off when the open function
1134 if (!delayed_work_pending(&bond->mii_work)) {
1135 INIT_DELAYED_WORK(&bond->mii_work,
1137 queue_delayed_work(bond->wq,
1138 &bond->mii_work, 0);
1145 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
1148 * Show and set the primary slave. The store function is much
1149 * simpler than bonding_store_slaves function because it only needs to
1150 * handle one interface name.
1151 * The bond must be a mode that supports a primary for this be
1154 static ssize_t bonding_show_primary(struct device *d,
1155 struct device_attribute *attr,
1159 struct bonding *bond = to_bond(d);
1161 if (bond->primary_slave)
1162 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1167 static ssize_t bonding_store_primary(struct device *d,
1168 struct device_attribute *attr,
1169 const char *buf, size_t count)
1172 struct slave *slave;
1173 struct bonding *bond = to_bond(d);
1176 read_lock(&bond->lock);
1177 write_lock_bh(&bond->curr_slave_lock);
1179 if (!USES_PRIMARY(bond->params.mode)) {
1180 printk(KERN_INFO DRV_NAME
1181 ": %s: Unable to set primary slave; %s is in mode %d\n",
1182 bond->dev->name, bond->dev->name, bond->params.mode);
1184 bond_for_each_slave(bond, slave, i) {
1186 (slave->dev->name, buf,
1187 strlen(slave->dev->name)) == 0) {
1188 printk(KERN_INFO DRV_NAME
1189 ": %s: Setting %s as primary slave.\n",
1190 bond->dev->name, slave->dev->name);
1191 bond->primary_slave = slave;
1192 bond_select_active_slave(bond);
1197 /* if we got here, then we didn't match the name of any slave */
1199 if (strlen(buf) == 0 || buf[0] == '\n') {
1200 printk(KERN_INFO DRV_NAME
1201 ": %s: Setting primary slave to None.\n",
1203 bond->primary_slave = NULL;
1204 bond_select_active_slave(bond);
1206 printk(KERN_INFO DRV_NAME
1207 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1208 bond->dev->name, (int)strlen(buf) - 1, buf);
1212 write_unlock_bh(&bond->curr_slave_lock);
1213 read_unlock(&bond->lock);
1218 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
1221 * Show and set the use_carrier flag.
1223 static ssize_t bonding_show_carrier(struct device *d,
1224 struct device_attribute *attr,
1227 struct bonding *bond = to_bond(d);
1229 return sprintf(buf, "%d\n", bond->params.use_carrier);
1232 static ssize_t bonding_store_carrier(struct device *d,
1233 struct device_attribute *attr,
1234 const char *buf, size_t count)
1236 int new_value, ret = count;
1237 struct bonding *bond = to_bond(d);
1240 if (sscanf(buf, "%d", &new_value) != 1) {
1241 printk(KERN_ERR DRV_NAME
1242 ": %s: no use_carrier value specified.\n",
1247 if ((new_value == 0) || (new_value == 1)) {
1248 bond->params.use_carrier = new_value;
1249 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1250 bond->dev->name, new_value);
1252 printk(KERN_INFO DRV_NAME
1253 ": %s: Ignoring invalid use_carrier value %d.\n",
1254 bond->dev->name, new_value);
1259 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
1263 * Show and set currently active_slave.
1265 static ssize_t bonding_show_active_slave(struct device *d,
1266 struct device_attribute *attr,
1270 struct bonding *bond = to_bond(d);
1273 read_lock(&bond->curr_slave_lock);
1274 curr = bond->curr_active_slave;
1275 read_unlock(&bond->curr_slave_lock);
1277 if (USES_PRIMARY(bond->params.mode) && curr)
1278 count = sprintf(buf, "%s\n", curr->dev->name);
1282 static ssize_t bonding_store_active_slave(struct device *d,
1283 struct device_attribute *attr,
1284 const char *buf, size_t count)
1287 struct slave *slave;
1288 struct slave *old_active = NULL;
1289 struct slave *new_active = NULL;
1290 struct bonding *bond = to_bond(d);
1293 read_lock(&bond->lock);
1294 write_lock_bh(&bond->curr_slave_lock);
1296 if (!USES_PRIMARY(bond->params.mode)) {
1297 printk(KERN_INFO DRV_NAME
1298 ": %s: Unable to change active slave; %s is in mode %d\n",
1299 bond->dev->name, bond->dev->name, bond->params.mode);
1301 bond_for_each_slave(bond, slave, i) {
1303 (slave->dev->name, buf,
1304 strlen(slave->dev->name)) == 0) {
1305 old_active = bond->curr_active_slave;
1307 if (new_active == old_active) {
1309 printk(KERN_INFO DRV_NAME
1310 ": %s: %s is already the current active slave.\n",
1311 bond->dev->name, slave->dev->name);
1317 (new_active->link == BOND_LINK_UP) &&
1318 IS_UP(new_active->dev)) {
1319 printk(KERN_INFO DRV_NAME
1320 ": %s: Setting %s as active slave.\n",
1321 bond->dev->name, slave->dev->name);
1322 bond_change_active_slave(bond, new_active);
1325 printk(KERN_INFO DRV_NAME
1326 ": %s: Could not set %s as active slave; "
1327 "either %s is down or the link is down.\n",
1328 bond->dev->name, slave->dev->name,
1336 /* if we got here, then we didn't match the name of any slave */
1338 if (strlen(buf) == 0 || buf[0] == '\n') {
1339 printk(KERN_INFO DRV_NAME
1340 ": %s: Setting active slave to None.\n",
1342 bond->primary_slave = NULL;
1343 bond_select_active_slave(bond);
1345 printk(KERN_INFO DRV_NAME
1346 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1347 bond->dev->name, (int)strlen(buf) - 1, buf);
1351 write_unlock_bh(&bond->curr_slave_lock);
1352 read_unlock(&bond->lock);
1358 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
1362 * Show link status of the bond interface.
1364 static ssize_t bonding_show_mii_status(struct device *d,
1365 struct device_attribute *attr,
1369 struct bonding *bond = to_bond(d);
1371 read_lock(&bond->curr_slave_lock);
1372 curr = bond->curr_active_slave;
1373 read_unlock(&bond->curr_slave_lock);
1375 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
1377 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1381 * Show current 802.3ad aggregator ID.
1383 static ssize_t bonding_show_ad_aggregator(struct device *d,
1384 struct device_attribute *attr,
1388 struct bonding *bond = to_bond(d);
1390 if (bond->params.mode == BOND_MODE_8023AD) {
1391 struct ad_info ad_info;
1392 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id);
1397 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1401 * Show number of active 802.3ad ports.
1403 static ssize_t bonding_show_ad_num_ports(struct device *d,
1404 struct device_attribute *attr,
1408 struct bonding *bond = to_bond(d);
1410 if (bond->params.mode == BOND_MODE_8023AD) {
1411 struct ad_info ad_info;
1412 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports);
1417 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1421 * Show current 802.3ad actor key.
1423 static ssize_t bonding_show_ad_actor_key(struct device *d,
1424 struct device_attribute *attr,
1428 struct bonding *bond = to_bond(d);
1430 if (bond->params.mode == BOND_MODE_8023AD) {
1431 struct ad_info ad_info;
1432 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key);
1437 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1441 * Show current 802.3ad partner key.
1443 static ssize_t bonding_show_ad_partner_key(struct device *d,
1444 struct device_attribute *attr,
1448 struct bonding *bond = to_bond(d);
1450 if (bond->params.mode == BOND_MODE_8023AD) {
1451 struct ad_info ad_info;
1452 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key);
1457 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1461 * Show current 802.3ad partner mac.
1463 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1464 struct device_attribute *attr,
1468 struct bonding *bond = to_bond(d);
1470 if (bond->params.mode == BOND_MODE_8023AD) {
1471 struct ad_info ad_info;
1472 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1473 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1479 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1483 static struct attribute *per_bond_attrs[] = {
1484 &dev_attr_slaves.attr,
1485 &dev_attr_mode.attr,
1486 &dev_attr_fail_over_mac.attr,
1487 &dev_attr_arp_validate.attr,
1488 &dev_attr_arp_interval.attr,
1489 &dev_attr_arp_ip_target.attr,
1490 &dev_attr_downdelay.attr,
1491 &dev_attr_updelay.attr,
1492 &dev_attr_lacp_rate.attr,
1493 &dev_attr_ad_select.attr,
1494 &dev_attr_xmit_hash_policy.attr,
1495 &dev_attr_num_grat_arp.attr,
1496 &dev_attr_num_unsol_na.attr,
1497 &dev_attr_miimon.attr,
1498 &dev_attr_primary.attr,
1499 &dev_attr_use_carrier.attr,
1500 &dev_attr_active_slave.attr,
1501 &dev_attr_mii_status.attr,
1502 &dev_attr_ad_aggregator.attr,
1503 &dev_attr_ad_num_ports.attr,
1504 &dev_attr_ad_actor_key.attr,
1505 &dev_attr_ad_partner_key.attr,
1506 &dev_attr_ad_partner_mac.attr,
1510 static struct attribute_group bonding_group = {
1512 .attrs = per_bond_attrs,
1516 * Initialize sysfs. This sets up the bonding_masters file in
1519 int bond_create_sysfs(void)
1523 ret = netdev_class_create_file(&class_attr_bonding_masters);
1525 * Permit multiple loads of the module by ignoring failures to
1526 * create the bonding_masters sysfs file. Bonding devices
1527 * created by second or subsequent loads of the module will
1528 * not be listed in, or controllable by, bonding_masters, but
1529 * will have the usual "bonding" sysfs directory.
1531 * This is done to preserve backwards compatibility for
1532 * initscripts/sysconfig, which load bonding multiple times to
1533 * configure multiple bonding devices.
1535 if (ret == -EEXIST) {
1536 /* Is someone being kinky and naming a device bonding_master? */
1537 if (__dev_get_by_name(&init_net,
1538 class_attr_bonding_masters.attr.name))
1540 "network device named %s already exists in sysfs",
1541 class_attr_bonding_masters.attr.name);
1549 * Remove /sys/class/net/bonding_masters.
1551 void bond_destroy_sysfs(void)
1553 netdev_class_remove_file(&class_attr_bonding_masters);
1557 * Initialize sysfs for each bond. This sets up and registers
1558 * the 'bondctl' directory for each individual bond under /sys/class/net.
1560 int bond_create_sysfs_entry(struct bonding *bond)
1562 struct net_device *dev = bond->dev;
1565 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
1567 printk(KERN_EMERG "eek! didn't create group!\n");
1570 if (expected_refcount < 1)
1571 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
1576 * Remove sysfs entries for each bond.
1578 void bond_destroy_sysfs_entry(struct bonding *bond)
1580 struct net_device *dev = bond->dev;
1582 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);