2 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/sysdev.h>
27 #include <linux/types.h>
28 #include <linux/string.h>
29 #include <linux/netdevice.h>
30 #include <linux/inetdevice.h>
32 #include <linux/sysfs.h>
33 #include <linux/ctype.h>
34 #include <linux/inet.h>
35 #include <linux/rtnetlink.h>
36 #include <net/net_namespace.h>
40 #define to_dev(obj) container_of(obj, struct device, kobj)
41 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
43 /*---------------------------- Declarations -------------------------------*/
45 static int expected_refcount = -1;
47 /*------------------------------ Functions --------------------------------*/
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
53 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
60 list_for_each_entry(bond, &bond_dev_list, bond_list) {
61 if (res > (PAGE_SIZE - IFNAMSIZ)) {
62 /* not enough space for another interface name */
63 if ((PAGE_SIZE - res) > 10)
65 res += sprintf(buf + res, "++more++ ");
68 res += sprintf(buf + res, "%s ", bond->dev->name);
71 buf[res-1] = '\n'; /* eat the leftover space */
78 * "store" function for the bond_masters attribute. This is what
79 * creates and deletes entire bonds.
81 * The class parameter is ignored.
85 static ssize_t bonding_store_bonds(struct class *cls,
86 const char *buffer, size_t count)
88 char command[IFNAMSIZ + 1] = {0, };
93 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
95 if ((strlen(command) <= 1) ||
96 !dev_valid_name(ifname))
99 if (command[0] == '+') {
101 ": %s is being created...\n", ifname);
102 rv = bond_create(ifname);
104 pr_info(DRV_NAME ": Bond creation failed.\n");
110 if (command[0] == '-') {
113 list_for_each_entry(bond, &bond_dev_list, bond_list)
114 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
115 /* check the ref count on the bond's kobject.
116 * If it's > expected, then there's a file open,
117 * and we have to fail.
119 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
120 > expected_refcount){
122 ": Unable remove bond %s due to open references.\n",
128 ": %s is being deleted...\n",
130 unregister_netdevice(bond->dev);
135 ": unable to delete non-existent bond %s\n", ifname);
141 pr_err(DRV_NAME ": no command found in bonding_masters."
142 " Use +ifname or -ifname.\n");
148 /* Always return either count or an error. If you return 0, you'll
149 * get called forever, which is bad.
154 /* class attribute for bond_masters file. This ends up in /sys/class/net */
155 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
156 bonding_show_bonds, bonding_store_bonds);
158 int bond_create_slave_symlinks(struct net_device *master,
159 struct net_device *slave)
161 char linkname[IFNAMSIZ+7];
164 /* first, create a link from the slave back to the master */
165 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
169 /* next, create a link from the master to the slave */
170 sprintf(linkname, "slave_%s", slave->name);
171 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
177 void bond_destroy_slave_symlinks(struct net_device *master,
178 struct net_device *slave)
180 char linkname[IFNAMSIZ+7];
182 sysfs_remove_link(&(slave->dev.kobj), "master");
183 sprintf(linkname, "slave_%s", slave->name);
184 sysfs_remove_link(&(master->dev.kobj), linkname);
189 * Show the slaves in the current bond.
191 static ssize_t bonding_show_slaves(struct device *d,
192 struct device_attribute *attr, char *buf)
196 struct bonding *bond = to_bond(d);
198 read_lock(&bond->lock);
199 bond_for_each_slave(bond, slave, i) {
200 if (res > (PAGE_SIZE - IFNAMSIZ)) {
201 /* not enough space for another interface name */
202 if ((PAGE_SIZE - res) > 10)
203 res = PAGE_SIZE - 10;
204 res += sprintf(buf + res, "++more++ ");
207 res += sprintf(buf + res, "%s ", slave->dev->name);
209 read_unlock(&bond->lock);
211 buf[res-1] = '\n'; /* eat the leftover space */
216 * Set the slaves in the current bond. The bond interface must be
217 * up for this to succeed.
218 * This function is largely the same flow as bonding_update_bonds().
220 static ssize_t bonding_store_slaves(struct device *d,
221 struct device_attribute *attr,
222 const char *buffer, size_t count)
224 char command[IFNAMSIZ + 1] = { 0, };
226 int i, res, found, ret = count;
229 struct net_device *dev = NULL;
230 struct bonding *bond = to_bond(d);
232 /* Quick sanity check -- is the bond interface up? */
233 if (!(bond->dev->flags & IFF_UP)) {
234 printk(KERN_WARNING DRV_NAME
235 ": %s: doing slave updates when interface is down.\n",
239 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
242 return restart_syscall();
244 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
245 ifname = command + 1;
246 if ((strlen(command) <= 1) ||
247 !dev_valid_name(ifname))
250 if (command[0] == '+') {
252 /* Got a slave name in ifname. Is it already in the list? */
254 read_lock(&bond->lock);
255 bond_for_each_slave(bond, slave, i)
256 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
258 ": %s: Interface %s is already enslaved!\n",
259 bond->dev->name, ifname);
261 read_unlock(&bond->lock);
265 read_unlock(&bond->lock);
266 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
267 bond->dev->name, ifname);
268 dev = dev_get_by_name(&init_net, ifname);
271 ": %s: Interface %s does not exist!\n",
272 bond->dev->name, ifname);
278 if (dev->flags & IFF_UP) {
280 ": %s: Error: Unable to enslave %s "
281 "because it is already up.\n",
282 bond->dev->name, dev->name);
286 /* If this is the first slave, then we need to set
287 the master's hardware address to be the same as the
289 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
290 memcpy(bond->dev->dev_addr, dev->dev_addr,
294 /* Set the slave's MTU to match the bond */
295 original_mtu = dev->mtu;
296 res = dev_set_mtu(dev, bond->dev->mtu);
302 res = bond_enslave(bond->dev, dev);
303 bond_for_each_slave(bond, slave, i)
304 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
305 slave->original_mtu = original_mtu;
312 if (command[0] == '-') {
315 bond_for_each_slave(bond, slave, i)
316 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
318 original_mtu = slave->original_mtu;
322 pr_info(DRV_NAME ": %s: Removing slave %s\n",
323 bond->dev->name, dev->name);
324 res = bond_release(bond->dev, dev);
329 /* set the slave MTU to the default */
330 dev_set_mtu(dev, original_mtu);
332 pr_err(DRV_NAME ": unable to remove non-existent"
333 " slave %s for bond %s.\n",
334 ifname, bond->dev->name);
341 pr_err(DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
349 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
350 bonding_store_slaves);
353 * Show and set the bonding mode. The bond interface must be down to
356 static ssize_t bonding_show_mode(struct device *d,
357 struct device_attribute *attr, char *buf)
359 struct bonding *bond = to_bond(d);
361 return sprintf(buf, "%s %d\n",
362 bond_mode_tbl[bond->params.mode].modename,
366 static ssize_t bonding_store_mode(struct device *d,
367 struct device_attribute *attr,
368 const char *buf, size_t count)
370 int new_value, ret = count;
371 struct bonding *bond = to_bond(d);
373 if (bond->dev->flags & IFF_UP) {
374 pr_err(DRV_NAME ": unable to update mode of %s"
375 " because interface is up.\n", bond->dev->name);
380 new_value = bond_parse_parm(buf, bond_mode_tbl);
383 ": %s: Ignoring invalid mode value %.*s.\n",
385 (int)strlen(buf) - 1, buf);
389 if (bond->params.mode == BOND_MODE_8023AD)
390 bond_unset_master_3ad_flags(bond);
392 if (bond->params.mode == BOND_MODE_ALB)
393 bond_unset_master_alb_flags(bond);
395 bond->params.mode = new_value;
396 bond_set_mode_ops(bond, bond->params.mode);
397 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
398 bond->dev->name, bond_mode_tbl[new_value].modename,
404 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
405 bonding_show_mode, bonding_store_mode);
408 * Show and set the bonding transmit hash method.
409 * The bond interface must be down to change the xmit hash policy.
411 static ssize_t bonding_show_xmit_hash(struct device *d,
412 struct device_attribute *attr,
415 struct bonding *bond = to_bond(d);
417 return sprintf(buf, "%s %d\n",
418 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
419 bond->params.xmit_policy);
422 static ssize_t bonding_store_xmit_hash(struct device *d,
423 struct device_attribute *attr,
424 const char *buf, size_t count)
426 int new_value, ret = count;
427 struct bonding *bond = to_bond(d);
429 if (bond->dev->flags & IFF_UP) {
431 "%s: Interface is up. Unable to update xmit policy.\n",
437 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
440 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
442 (int)strlen(buf) - 1, buf);
446 bond->params.xmit_policy = new_value;
447 bond_set_mode_ops(bond, bond->params.mode);
448 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
450 xmit_hashtype_tbl[new_value].modename, new_value);
455 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
456 bonding_show_xmit_hash, bonding_store_xmit_hash);
459 * Show and set arp_validate.
461 static ssize_t bonding_show_arp_validate(struct device *d,
462 struct device_attribute *attr,
465 struct bonding *bond = to_bond(d);
467 return sprintf(buf, "%s %d\n",
468 arp_validate_tbl[bond->params.arp_validate].modename,
469 bond->params.arp_validate);
472 static ssize_t bonding_store_arp_validate(struct device *d,
473 struct device_attribute *attr,
474 const char *buf, size_t count)
477 struct bonding *bond = to_bond(d);
479 new_value = bond_parse_parm(buf, arp_validate_tbl);
482 ": %s: Ignoring invalid arp_validate value %s\n",
483 bond->dev->name, buf);
486 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
488 ": %s: arp_validate only supported in active-backup mode.\n",
492 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
493 bond->dev->name, arp_validate_tbl[new_value].modename,
496 if (!bond->params.arp_validate && new_value)
497 bond_register_arp(bond);
498 else if (bond->params.arp_validate && !new_value)
499 bond_unregister_arp(bond);
501 bond->params.arp_validate = new_value;
506 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
507 bonding_store_arp_validate);
510 * Show and store fail_over_mac. User only allowed to change the
511 * value when there are no slaves.
513 static ssize_t bonding_show_fail_over_mac(struct device *d,
514 struct device_attribute *attr,
517 struct bonding *bond = to_bond(d);
519 return sprintf(buf, "%s %d\n",
520 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
521 bond->params.fail_over_mac);
524 static ssize_t bonding_store_fail_over_mac(struct device *d,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
529 struct bonding *bond = to_bond(d);
531 if (bond->slave_cnt != 0) {
533 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
538 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
541 ": %s: Ignoring invalid fail_over_mac value %s.\n",
542 bond->dev->name, buf);
546 bond->params.fail_over_mac = new_value;
547 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
548 bond->dev->name, fail_over_mac_tbl[new_value].modename,
554 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
555 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
558 * Show and set the arp timer interval. There are two tricky bits
559 * here. First, if ARP monitoring is activated, then we must disable
560 * MII monitoring. Second, if the ARP timer isn't running, we must
563 static ssize_t bonding_show_arp_interval(struct device *d,
564 struct device_attribute *attr,
567 struct bonding *bond = to_bond(d);
569 return sprintf(buf, "%d\n", bond->params.arp_interval);
572 static ssize_t bonding_store_arp_interval(struct device *d,
573 struct device_attribute *attr,
574 const char *buf, size_t count)
576 int new_value, ret = count;
577 struct bonding *bond = to_bond(d);
579 if (sscanf(buf, "%d", &new_value) != 1) {
581 ": %s: no arp_interval value specified.\n",
588 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
589 bond->dev->name, new_value, INT_MAX);
595 ": %s: Setting ARP monitoring interval to %d.\n",
596 bond->dev->name, new_value);
597 bond->params.arp_interval = new_value;
598 if (bond->params.arp_interval)
599 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
600 if (bond->params.miimon) {
602 ": %s: ARP monitoring cannot be used with MII monitoring. "
603 "%s Disabling MII monitoring.\n",
604 bond->dev->name, bond->dev->name);
605 bond->params.miimon = 0;
606 if (delayed_work_pending(&bond->mii_work)) {
607 cancel_delayed_work(&bond->mii_work);
608 flush_workqueue(bond->wq);
611 if (!bond->params.arp_targets[0]) {
613 ": %s: ARP monitoring has been set up, "
614 "but no ARP targets have been specified.\n",
617 if (bond->dev->flags & IFF_UP) {
618 /* If the interface is up, we may need to fire off
619 * the ARP timer. If the interface is down, the
620 * timer will get fired off when the open function
623 if (!delayed_work_pending(&bond->arp_work)) {
624 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
625 INIT_DELAYED_WORK(&bond->arp_work,
626 bond_activebackup_arp_mon);
628 INIT_DELAYED_WORK(&bond->arp_work,
629 bond_loadbalance_arp_mon);
631 queue_delayed_work(bond->wq, &bond->arp_work, 0);
638 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
639 bonding_show_arp_interval, bonding_store_arp_interval);
642 * Show and set the arp targets.
644 static ssize_t bonding_show_arp_targets(struct device *d,
645 struct device_attribute *attr,
649 struct bonding *bond = to_bond(d);
651 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
652 if (bond->params.arp_targets[i])
653 res += sprintf(buf + res, "%pI4 ",
654 &bond->params.arp_targets[i]);
657 buf[res-1] = '\n'; /* eat the leftover space */
661 static ssize_t bonding_store_arp_targets(struct device *d,
662 struct device_attribute *attr,
663 const char *buf, size_t count)
666 int i = 0, done = 0, ret = count;
667 struct bonding *bond = to_bond(d);
670 targets = bond->params.arp_targets;
671 newtarget = in_aton(buf + 1);
674 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
676 ": %s: invalid ARP target %pI4 specified for addition\n",
677 bond->dev->name, &newtarget);
681 /* look for an empty slot to put the target in, and check for dupes */
682 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
683 if (targets[i] == newtarget) { /* duplicate */
685 ": %s: ARP target %pI4 is already present\n",
686 bond->dev->name, &newtarget);
690 if (targets[i] == 0) {
692 ": %s: adding ARP target %pI4.\n",
693 bond->dev->name, &newtarget);
695 targets[i] = newtarget;
700 ": %s: ARP target table is full!\n",
706 } else if (buf[0] == '-') {
707 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
709 ": %s: invalid ARP target %pI4 specified for removal\n",
710 bond->dev->name, &newtarget);
715 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
716 if (targets[i] == newtarget) {
719 ": %s: removing ARP target %pI4.\n",
720 bond->dev->name, &newtarget);
721 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
722 targets[j] = targets[j+1];
730 ": %s: unable to remove nonexistent ARP target %pI4.\n",
731 bond->dev->name, &newtarget);
736 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
737 " for bond %s. Use +<addr> or -<addr>.\n",
746 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
749 * Show and set the up and down delays. These must be multiples of the
750 * MII monitoring value, and are stored internally as the multiplier.
751 * Thus, we must translate to MS for the real world.
753 static ssize_t bonding_show_downdelay(struct device *d,
754 struct device_attribute *attr,
757 struct bonding *bond = to_bond(d);
759 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
762 static ssize_t bonding_store_downdelay(struct device *d,
763 struct device_attribute *attr,
764 const char *buf, size_t count)
766 int new_value, ret = count;
767 struct bonding *bond = to_bond(d);
769 if (!(bond->params.miimon)) {
771 ": %s: Unable to set down delay as MII monitoring is disabled\n",
777 if (sscanf(buf, "%d", &new_value) != 1) {
779 ": %s: no down delay value specified.\n",
786 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
787 bond->dev->name, new_value, 1, INT_MAX);
791 if ((new_value % bond->params.miimon) != 0) {
792 printk(KERN_WARNING DRV_NAME
793 ": %s: Warning: down delay (%d) is not a multiple "
794 "of miimon (%d), delay rounded to %d ms\n",
795 bond->dev->name, new_value, bond->params.miimon,
796 (new_value / bond->params.miimon) *
797 bond->params.miimon);
799 bond->params.downdelay = new_value / bond->params.miimon;
800 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
802 bond->params.downdelay * bond->params.miimon);
809 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
810 bonding_show_downdelay, bonding_store_downdelay);
812 static ssize_t bonding_show_updelay(struct device *d,
813 struct device_attribute *attr,
816 struct bonding *bond = to_bond(d);
818 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
822 static ssize_t bonding_store_updelay(struct device *d,
823 struct device_attribute *attr,
824 const char *buf, size_t count)
826 int new_value, ret = count;
827 struct bonding *bond = to_bond(d);
829 if (!(bond->params.miimon)) {
831 ": %s: Unable to set up delay as MII monitoring is disabled\n",
837 if (sscanf(buf, "%d", &new_value) != 1) {
839 ": %s: no up delay value specified.\n",
846 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
847 bond->dev->name, new_value, 1, INT_MAX);
851 if ((new_value % bond->params.miimon) != 0) {
852 printk(KERN_WARNING DRV_NAME
853 ": %s: Warning: up delay (%d) is not a multiple "
854 "of miimon (%d), updelay rounded to %d ms\n",
855 bond->dev->name, new_value, bond->params.miimon,
856 (new_value / bond->params.miimon) *
857 bond->params.miimon);
859 bond->params.updelay = new_value / bond->params.miimon;
860 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
861 bond->dev->name, bond->params.updelay * bond->params.miimon);
868 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
869 bonding_show_updelay, bonding_store_updelay);
872 * Show and set the LACP interval. Interface must be down, and the mode
873 * must be set to 802.3ad mode.
875 static ssize_t bonding_show_lacp(struct device *d,
876 struct device_attribute *attr,
879 struct bonding *bond = to_bond(d);
881 return sprintf(buf, "%s %d\n",
882 bond_lacp_tbl[bond->params.lacp_fast].modename,
883 bond->params.lacp_fast);
886 static ssize_t bonding_store_lacp(struct device *d,
887 struct device_attribute *attr,
888 const char *buf, size_t count)
890 int new_value, ret = count;
891 struct bonding *bond = to_bond(d);
893 if (bond->dev->flags & IFF_UP) {
895 ": %s: Unable to update LACP rate because interface is up.\n",
901 if (bond->params.mode != BOND_MODE_8023AD) {
903 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
909 new_value = bond_parse_parm(buf, bond_lacp_tbl);
911 if ((new_value == 1) || (new_value == 0)) {
912 bond->params.lacp_fast = new_value;
913 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
914 bond->dev->name, bond_lacp_tbl[new_value].modename,
918 ": %s: Ignoring invalid LACP rate value %.*s.\n",
919 bond->dev->name, (int)strlen(buf) - 1, buf);
925 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
926 bonding_show_lacp, bonding_store_lacp);
928 static ssize_t bonding_show_ad_select(struct device *d,
929 struct device_attribute *attr,
932 struct bonding *bond = to_bond(d);
934 return sprintf(buf, "%s %d\n",
935 ad_select_tbl[bond->params.ad_select].modename,
936 bond->params.ad_select);
940 static ssize_t bonding_store_ad_select(struct device *d,
941 struct device_attribute *attr,
942 const char *buf, size_t count)
944 int new_value, ret = count;
945 struct bonding *bond = to_bond(d);
947 if (bond->dev->flags & IFF_UP) {
949 ": %s: Unable to update ad_select because interface "
950 "is up.\n", bond->dev->name);
955 new_value = bond_parse_parm(buf, ad_select_tbl);
957 if (new_value != -1) {
958 bond->params.ad_select = new_value;
960 ": %s: Setting ad_select to %s (%d).\n",
961 bond->dev->name, ad_select_tbl[new_value].modename,
965 ": %s: Ignoring invalid ad_select value %.*s.\n",
966 bond->dev->name, (int)strlen(buf) - 1, buf);
972 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
973 bonding_show_ad_select, bonding_store_ad_select);
976 * Show and set the number of grat ARP to send after a failover event.
978 static ssize_t bonding_show_n_grat_arp(struct device *d,
979 struct device_attribute *attr,
982 struct bonding *bond = to_bond(d);
984 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
987 static ssize_t bonding_store_n_grat_arp(struct device *d,
988 struct device_attribute *attr,
989 const char *buf, size_t count)
991 int new_value, ret = count;
992 struct bonding *bond = to_bond(d);
994 if (sscanf(buf, "%d", &new_value) != 1) {
996 ": %s: no num_grat_arp value specified.\n",
1001 if (new_value < 0 || new_value > 255) {
1003 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1004 bond->dev->name, new_value);
1008 bond->params.num_grat_arp = new_value;
1013 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1014 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
1017 * Show and set the number of unsolicited NA's to send after a failover event.
1019 static ssize_t bonding_show_n_unsol_na(struct device *d,
1020 struct device_attribute *attr,
1023 struct bonding *bond = to_bond(d);
1025 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1028 static ssize_t bonding_store_n_unsol_na(struct device *d,
1029 struct device_attribute *attr,
1030 const char *buf, size_t count)
1032 int new_value, ret = count;
1033 struct bonding *bond = to_bond(d);
1035 if (sscanf(buf, "%d", &new_value) != 1) {
1037 ": %s: no num_unsol_na value specified.\n",
1043 if (new_value < 0 || new_value > 255) {
1045 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1046 bond->dev->name, new_value);
1050 bond->params.num_unsol_na = new_value;
1054 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1055 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1058 * Show and set the MII monitor interval. There are two tricky bits
1059 * here. First, if MII monitoring is activated, then we must disable
1060 * ARP monitoring. Second, if the timer isn't running, we must
1063 static ssize_t bonding_show_miimon(struct device *d,
1064 struct device_attribute *attr,
1067 struct bonding *bond = to_bond(d);
1069 return sprintf(buf, "%d\n", bond->params.miimon);
1072 static ssize_t bonding_store_miimon(struct device *d,
1073 struct device_attribute *attr,
1074 const char *buf, size_t count)
1076 int new_value, ret = count;
1077 struct bonding *bond = to_bond(d);
1079 if (sscanf(buf, "%d", &new_value) != 1) {
1081 ": %s: no miimon value specified.\n",
1086 if (new_value < 0) {
1088 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1089 bond->dev->name, new_value, 1, INT_MAX);
1094 ": %s: Setting MII monitoring interval to %d.\n",
1095 bond->dev->name, new_value);
1096 bond->params.miimon = new_value;
1097 if (bond->params.updelay)
1099 ": %s: Note: Updating updelay (to %d) "
1100 "since it is a multiple of the miimon value.\n",
1102 bond->params.updelay * bond->params.miimon);
1103 if (bond->params.downdelay)
1105 ": %s: Note: Updating downdelay (to %d) "
1106 "since it is a multiple of the miimon value.\n",
1108 bond->params.downdelay * bond->params.miimon);
1109 if (bond->params.arp_interval) {
1111 ": %s: MII monitoring cannot be used with "
1112 "ARP monitoring. Disabling ARP monitoring...\n",
1114 bond->params.arp_interval = 0;
1115 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1116 if (bond->params.arp_validate) {
1117 bond_unregister_arp(bond);
1118 bond->params.arp_validate =
1119 BOND_ARP_VALIDATE_NONE;
1121 if (delayed_work_pending(&bond->arp_work)) {
1122 cancel_delayed_work(&bond->arp_work);
1123 flush_workqueue(bond->wq);
1127 if (bond->dev->flags & IFF_UP) {
1128 /* If the interface is up, we may need to fire off
1129 * the MII timer. If the interface is down, the
1130 * timer will get fired off when the open function
1133 if (!delayed_work_pending(&bond->mii_work)) {
1134 INIT_DELAYED_WORK(&bond->mii_work,
1136 queue_delayed_work(bond->wq,
1137 &bond->mii_work, 0);
1144 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1145 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);
1175 if (!rtnl_trylock())
1176 return restart_syscall();
1177 read_lock(&bond->lock);
1178 write_lock_bh(&bond->curr_slave_lock);
1180 if (!USES_PRIMARY(bond->params.mode)) {
1182 ": %s: Unable to set primary slave; %s is in mode %d\n",
1183 bond->dev->name, bond->dev->name, bond->params.mode);
1185 bond_for_each_slave(bond, slave, i) {
1187 (slave->dev->name, buf,
1188 strlen(slave->dev->name)) == 0) {
1190 ": %s: Setting %s as primary slave.\n",
1191 bond->dev->name, slave->dev->name);
1192 bond->primary_slave = slave;
1193 bond_select_active_slave(bond);
1198 /* if we got here, then we didn't match the name of any slave */
1200 if (strlen(buf) == 0 || buf[0] == '\n') {
1202 ": %s: Setting primary slave to None.\n",
1204 bond->primary_slave = NULL;
1205 bond_select_active_slave(bond);
1208 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1209 bond->dev->name, (int)strlen(buf) - 1, buf);
1213 write_unlock_bh(&bond->curr_slave_lock);
1214 read_unlock(&bond->lock);
1219 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1220 bonding_show_primary, bonding_store_primary);
1223 * Show and set the use_carrier flag.
1225 static ssize_t bonding_show_carrier(struct device *d,
1226 struct device_attribute *attr,
1229 struct bonding *bond = to_bond(d);
1231 return sprintf(buf, "%d\n", bond->params.use_carrier);
1234 static ssize_t bonding_store_carrier(struct device *d,
1235 struct device_attribute *attr,
1236 const char *buf, size_t count)
1238 int new_value, ret = count;
1239 struct bonding *bond = to_bond(d);
1242 if (sscanf(buf, "%d", &new_value) != 1) {
1244 ": %s: no use_carrier value specified.\n",
1249 if ((new_value == 0) || (new_value == 1)) {
1250 bond->params.use_carrier = new_value;
1251 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
1252 bond->dev->name, new_value);
1255 ": %s: Ignoring invalid use_carrier value %d.\n",
1256 bond->dev->name, new_value);
1261 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1262 bonding_show_carrier, bonding_store_carrier);
1266 * Show and set currently active_slave.
1268 static ssize_t bonding_show_active_slave(struct device *d,
1269 struct device_attribute *attr,
1273 struct bonding *bond = to_bond(d);
1276 read_lock(&bond->curr_slave_lock);
1277 curr = bond->curr_active_slave;
1278 read_unlock(&bond->curr_slave_lock);
1280 if (USES_PRIMARY(bond->params.mode) && curr)
1281 count = sprintf(buf, "%s\n", curr->dev->name);
1285 static ssize_t bonding_store_active_slave(struct device *d,
1286 struct device_attribute *attr,
1287 const char *buf, size_t count)
1290 struct slave *slave;
1291 struct slave *old_active = NULL;
1292 struct slave *new_active = NULL;
1293 struct bonding *bond = to_bond(d);
1295 if (!rtnl_trylock())
1296 return restart_syscall();
1297 read_lock(&bond->lock);
1298 write_lock_bh(&bond->curr_slave_lock);
1300 if (!USES_PRIMARY(bond->params.mode))
1301 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1302 " %s is in mode %d\n",
1303 bond->dev->name, bond->dev->name, bond->params.mode);
1305 bond_for_each_slave(bond, slave, i) {
1307 (slave->dev->name, buf,
1308 strlen(slave->dev->name)) == 0) {
1309 old_active = bond->curr_active_slave;
1311 if (new_active == old_active) {
1313 printk(KERN_INFO DRV_NAME
1314 ": %s: %s is already the current active slave.\n",
1315 bond->dev->name, slave->dev->name);
1321 (new_active->link == BOND_LINK_UP) &&
1322 IS_UP(new_active->dev)) {
1323 printk(KERN_INFO DRV_NAME
1324 ": %s: Setting %s as active slave.\n",
1325 bond->dev->name, slave->dev->name);
1326 bond_change_active_slave(bond, new_active);
1329 printk(KERN_INFO DRV_NAME
1330 ": %s: Could not set %s as active slave; "
1331 "either %s is down or the link is down.\n",
1332 bond->dev->name, slave->dev->name,
1340 /* if we got here, then we didn't match the name of any slave */
1342 if (strlen(buf) == 0 || buf[0] == '\n') {
1344 ": %s: Setting active slave to None.\n",
1346 bond->primary_slave = NULL;
1347 bond_select_active_slave(bond);
1349 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1350 " as active slave as it is not a slave.\n",
1351 bond->dev->name, (int)strlen(buf) - 1, buf);
1355 write_unlock_bh(&bond->curr_slave_lock);
1356 read_unlock(&bond->lock);
1362 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1363 bonding_show_active_slave, bonding_store_active_slave);
1367 * Show link status of the bond interface.
1369 static ssize_t bonding_show_mii_status(struct device *d,
1370 struct device_attribute *attr,
1374 struct bonding *bond = to_bond(d);
1376 read_lock(&bond->curr_slave_lock);
1377 curr = bond->curr_active_slave;
1378 read_unlock(&bond->curr_slave_lock);
1380 return sprintf(buf, "%s\n", curr ? "up" : "down");
1382 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1386 * Show current 802.3ad aggregator ID.
1388 static ssize_t bonding_show_ad_aggregator(struct device *d,
1389 struct device_attribute *attr,
1393 struct bonding *bond = to_bond(d);
1395 if (bond->params.mode == BOND_MODE_8023AD) {
1396 struct ad_info ad_info;
1397 count = sprintf(buf, "%d\n",
1398 (bond_3ad_get_active_agg_info(bond, &ad_info))
1399 ? 0 : ad_info.aggregator_id);
1404 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1408 * Show number of active 802.3ad ports.
1410 static ssize_t bonding_show_ad_num_ports(struct device *d,
1411 struct device_attribute *attr,
1415 struct bonding *bond = to_bond(d);
1417 if (bond->params.mode == BOND_MODE_8023AD) {
1418 struct ad_info ad_info;
1419 count = sprintf(buf, "%d\n",
1420 (bond_3ad_get_active_agg_info(bond, &ad_info))
1421 ? 0 : ad_info.ports);
1426 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1430 * Show current 802.3ad actor key.
1432 static ssize_t bonding_show_ad_actor_key(struct device *d,
1433 struct device_attribute *attr,
1437 struct bonding *bond = to_bond(d);
1439 if (bond->params.mode == BOND_MODE_8023AD) {
1440 struct ad_info ad_info;
1441 count = sprintf(buf, "%d\n",
1442 (bond_3ad_get_active_agg_info(bond, &ad_info))
1443 ? 0 : ad_info.actor_key);
1448 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1452 * Show current 802.3ad partner key.
1454 static ssize_t bonding_show_ad_partner_key(struct device *d,
1455 struct device_attribute *attr,
1459 struct bonding *bond = to_bond(d);
1461 if (bond->params.mode == BOND_MODE_8023AD) {
1462 struct ad_info ad_info;
1463 count = sprintf(buf, "%d\n",
1464 (bond_3ad_get_active_agg_info(bond, &ad_info))
1465 ? 0 : ad_info.partner_key);
1470 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1474 * Show current 802.3ad partner mac.
1476 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1477 struct device_attribute *attr,
1481 struct bonding *bond = to_bond(d);
1483 if (bond->params.mode == BOND_MODE_8023AD) {
1484 struct ad_info ad_info;
1485 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1486 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1491 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1495 static struct attribute *per_bond_attrs[] = {
1496 &dev_attr_slaves.attr,
1497 &dev_attr_mode.attr,
1498 &dev_attr_fail_over_mac.attr,
1499 &dev_attr_arp_validate.attr,
1500 &dev_attr_arp_interval.attr,
1501 &dev_attr_arp_ip_target.attr,
1502 &dev_attr_downdelay.attr,
1503 &dev_attr_updelay.attr,
1504 &dev_attr_lacp_rate.attr,
1505 &dev_attr_ad_select.attr,
1506 &dev_attr_xmit_hash_policy.attr,
1507 &dev_attr_num_grat_arp.attr,
1508 &dev_attr_num_unsol_na.attr,
1509 &dev_attr_miimon.attr,
1510 &dev_attr_primary.attr,
1511 &dev_attr_use_carrier.attr,
1512 &dev_attr_active_slave.attr,
1513 &dev_attr_mii_status.attr,
1514 &dev_attr_ad_aggregator.attr,
1515 &dev_attr_ad_num_ports.attr,
1516 &dev_attr_ad_actor_key.attr,
1517 &dev_attr_ad_partner_key.attr,
1518 &dev_attr_ad_partner_mac.attr,
1522 static struct attribute_group bonding_group = {
1524 .attrs = per_bond_attrs,
1528 * Initialize sysfs. This sets up the bonding_masters file in
1531 int bond_create_sysfs(void)
1535 ret = netdev_class_create_file(&class_attr_bonding_masters);
1537 * Permit multiple loads of the module by ignoring failures to
1538 * create the bonding_masters sysfs file. Bonding devices
1539 * created by second or subsequent loads of the module will
1540 * not be listed in, or controllable by, bonding_masters, but
1541 * will have the usual "bonding" sysfs directory.
1543 * This is done to preserve backwards compatibility for
1544 * initscripts/sysconfig, which load bonding multiple times to
1545 * configure multiple bonding devices.
1547 if (ret == -EEXIST) {
1548 /* Is someone being kinky and naming a device bonding_master? */
1549 if (__dev_get_by_name(&init_net,
1550 class_attr_bonding_masters.attr.name))
1552 "network device named %s already exists in sysfs",
1553 class_attr_bonding_masters.attr.name);
1562 * Remove /sys/class/net/bonding_masters.
1564 void bond_destroy_sysfs(void)
1566 netdev_class_remove_file(&class_attr_bonding_masters);
1570 * Initialize sysfs for each bond. This sets up and registers
1571 * the 'bondctl' directory for each individual bond under /sys/class/net.
1573 int bond_create_sysfs_entry(struct bonding *bond)
1575 struct net_device *dev = bond->dev;
1578 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
1580 printk(KERN_EMERG "eek! didn't create group!\n");
1582 if (expected_refcount < 1)
1583 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
1588 * Remove sysfs entries for each bond.
1590 void bond_destroy_sysfs_entry(struct bonding *bond)
1592 struct net_device *dev = bond->dev;
1594 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);