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))))
44 * "show" function for the bond_masters attribute.
45 * The class parameter is ignored.
47 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
54 list_for_each_entry(bond, &bond_dev_list, bond_list) {
55 if (res > (PAGE_SIZE - IFNAMSIZ)) {
56 /* not enough space for another interface name */
57 if ((PAGE_SIZE - res) > 10)
59 res += sprintf(buf + res, "++more++ ");
62 res += sprintf(buf + res, "%s ", bond->dev->name);
65 buf[res-1] = '\n'; /* eat the leftover space */
72 * "store" function for the bond_masters attribute. This is what
73 * creates and deletes entire bonds.
75 * The class parameter is ignored.
79 static ssize_t bonding_store_bonds(struct class *cls,
80 const char *buffer, size_t count)
82 char command[IFNAMSIZ + 1] = {0, };
87 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
89 if ((strlen(command) <= 1) ||
90 !dev_valid_name(ifname))
93 if (command[0] == '+') {
95 ": %s is being created...\n", ifname);
96 rv = bond_create(ifname);
98 pr_info(DRV_NAME ": Bond creation failed.\n");
104 if (command[0] == '-') {
107 list_for_each_entry(bond, &bond_dev_list, bond_list)
108 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
110 ": %s is being deleted...\n",
112 unregister_netdevice(bond->dev);
117 ": unable to delete non-existent bond %s\n", ifname);
123 pr_err(DRV_NAME ": no command found in bonding_masters."
124 " Use +ifname or -ifname.\n");
130 /* Always return either count or an error. If you return 0, you'll
131 * get called forever, which is bad.
136 /* class attribute for bond_masters file. This ends up in /sys/class/net */
137 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
138 bonding_show_bonds, bonding_store_bonds);
140 int bond_create_slave_symlinks(struct net_device *master,
141 struct net_device *slave)
143 char linkname[IFNAMSIZ+7];
146 /* first, create a link from the slave back to the master */
147 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
151 /* next, create a link from the master to the slave */
152 sprintf(linkname, "slave_%s", slave->name);
153 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
159 void bond_destroy_slave_symlinks(struct net_device *master,
160 struct net_device *slave)
162 char linkname[IFNAMSIZ+7];
164 sysfs_remove_link(&(slave->dev.kobj), "master");
165 sprintf(linkname, "slave_%s", slave->name);
166 sysfs_remove_link(&(master->dev.kobj), linkname);
171 * Show the slaves in the current bond.
173 static ssize_t bonding_show_slaves(struct device *d,
174 struct device_attribute *attr, char *buf)
178 struct bonding *bond = to_bond(d);
180 read_lock(&bond->lock);
181 bond_for_each_slave(bond, slave, i) {
182 if (res > (PAGE_SIZE - IFNAMSIZ)) {
183 /* not enough space for another interface name */
184 if ((PAGE_SIZE - res) > 10)
185 res = PAGE_SIZE - 10;
186 res += sprintf(buf + res, "++more++ ");
189 res += sprintf(buf + res, "%s ", slave->dev->name);
191 read_unlock(&bond->lock);
193 buf[res-1] = '\n'; /* eat the leftover space */
198 * Set the slaves in the current bond. The bond interface must be
199 * up for this to succeed.
200 * This function is largely the same flow as bonding_update_bonds().
202 static ssize_t bonding_store_slaves(struct device *d,
203 struct device_attribute *attr,
204 const char *buffer, size_t count)
206 char command[IFNAMSIZ + 1] = { 0, };
208 int i, res, found, ret = count;
211 struct net_device *dev = NULL;
212 struct bonding *bond = to_bond(d);
214 /* Quick sanity check -- is the bond interface up? */
215 if (!(bond->dev->flags & IFF_UP)) {
216 printk(KERN_WARNING DRV_NAME
217 ": %s: doing slave updates when interface is down.\n",
221 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
224 return restart_syscall();
226 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
227 ifname = command + 1;
228 if ((strlen(command) <= 1) ||
229 !dev_valid_name(ifname))
232 if (command[0] == '+') {
234 /* Got a slave name in ifname. Is it already in the list? */
236 read_lock(&bond->lock);
237 bond_for_each_slave(bond, slave, i)
238 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
240 ": %s: Interface %s is already enslaved!\n",
241 bond->dev->name, ifname);
243 read_unlock(&bond->lock);
247 read_unlock(&bond->lock);
248 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
249 bond->dev->name, ifname);
250 dev = dev_get_by_name(&init_net, ifname);
253 ": %s: Interface %s does not exist!\n",
254 bond->dev->name, ifname);
260 if (dev->flags & IFF_UP) {
262 ": %s: Error: Unable to enslave %s "
263 "because it is already up.\n",
264 bond->dev->name, dev->name);
268 /* If this is the first slave, then we need to set
269 the master's hardware address to be the same as the
271 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
272 memcpy(bond->dev->dev_addr, dev->dev_addr,
276 /* Set the slave's MTU to match the bond */
277 original_mtu = dev->mtu;
278 res = dev_set_mtu(dev, bond->dev->mtu);
284 res = bond_enslave(bond->dev, dev);
285 bond_for_each_slave(bond, slave, i)
286 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
287 slave->original_mtu = original_mtu;
294 if (command[0] == '-') {
297 bond_for_each_slave(bond, slave, i)
298 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
300 original_mtu = slave->original_mtu;
304 pr_info(DRV_NAME ": %s: Removing slave %s\n",
305 bond->dev->name, dev->name);
306 res = bond_release(bond->dev, dev);
311 /* set the slave MTU to the default */
312 dev_set_mtu(dev, original_mtu);
314 pr_err(DRV_NAME ": unable to remove non-existent"
315 " slave %s for bond %s.\n",
316 ifname, bond->dev->name);
323 pr_err(DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
331 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
332 bonding_store_slaves);
335 * Show and set the bonding mode. The bond interface must be down to
338 static ssize_t bonding_show_mode(struct device *d,
339 struct device_attribute *attr, char *buf)
341 struct bonding *bond = to_bond(d);
343 return sprintf(buf, "%s %d\n",
344 bond_mode_tbl[bond->params.mode].modename,
348 static ssize_t bonding_store_mode(struct device *d,
349 struct device_attribute *attr,
350 const char *buf, size_t count)
352 int new_value, ret = count;
353 struct bonding *bond = to_bond(d);
355 if (bond->dev->flags & IFF_UP) {
356 pr_err(DRV_NAME ": unable to update mode of %s"
357 " because interface is up.\n", bond->dev->name);
362 new_value = bond_parse_parm(buf, bond_mode_tbl);
365 ": %s: Ignoring invalid mode value %.*s.\n",
367 (int)strlen(buf) - 1, buf);
371 if (bond->params.mode == BOND_MODE_8023AD)
372 bond_unset_master_3ad_flags(bond);
374 if (bond->params.mode == BOND_MODE_ALB)
375 bond_unset_master_alb_flags(bond);
377 bond->params.mode = new_value;
378 bond_set_mode_ops(bond, bond->params.mode);
379 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
380 bond->dev->name, bond_mode_tbl[new_value].modename,
386 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
387 bonding_show_mode, bonding_store_mode);
390 * Show and set the bonding transmit hash method.
391 * The bond interface must be down to change the xmit hash policy.
393 static ssize_t bonding_show_xmit_hash(struct device *d,
394 struct device_attribute *attr,
397 struct bonding *bond = to_bond(d);
399 return sprintf(buf, "%s %d\n",
400 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
401 bond->params.xmit_policy);
404 static ssize_t bonding_store_xmit_hash(struct device *d,
405 struct device_attribute *attr,
406 const char *buf, size_t count)
408 int new_value, ret = count;
409 struct bonding *bond = to_bond(d);
411 if (bond->dev->flags & IFF_UP) {
413 "%s: Interface is up. Unable to update xmit policy.\n",
419 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
422 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
424 (int)strlen(buf) - 1, buf);
428 bond->params.xmit_policy = new_value;
429 bond_set_mode_ops(bond, bond->params.mode);
430 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
432 xmit_hashtype_tbl[new_value].modename, new_value);
437 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
438 bonding_show_xmit_hash, bonding_store_xmit_hash);
441 * Show and set arp_validate.
443 static ssize_t bonding_show_arp_validate(struct device *d,
444 struct device_attribute *attr,
447 struct bonding *bond = to_bond(d);
449 return sprintf(buf, "%s %d\n",
450 arp_validate_tbl[bond->params.arp_validate].modename,
451 bond->params.arp_validate);
454 static ssize_t bonding_store_arp_validate(struct device *d,
455 struct device_attribute *attr,
456 const char *buf, size_t count)
459 struct bonding *bond = to_bond(d);
461 new_value = bond_parse_parm(buf, arp_validate_tbl);
464 ": %s: Ignoring invalid arp_validate value %s\n",
465 bond->dev->name, buf);
468 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
470 ": %s: arp_validate only supported in active-backup mode.\n",
474 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
475 bond->dev->name, arp_validate_tbl[new_value].modename,
478 if (!bond->params.arp_validate && new_value)
479 bond_register_arp(bond);
480 else if (bond->params.arp_validate && !new_value)
481 bond_unregister_arp(bond);
483 bond->params.arp_validate = new_value;
488 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
489 bonding_store_arp_validate);
492 * Show and store fail_over_mac. User only allowed to change the
493 * value when there are no slaves.
495 static ssize_t bonding_show_fail_over_mac(struct device *d,
496 struct device_attribute *attr,
499 struct bonding *bond = to_bond(d);
501 return sprintf(buf, "%s %d\n",
502 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
503 bond->params.fail_over_mac);
506 static ssize_t bonding_store_fail_over_mac(struct device *d,
507 struct device_attribute *attr,
508 const char *buf, size_t count)
511 struct bonding *bond = to_bond(d);
513 if (bond->slave_cnt != 0) {
515 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
520 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
523 ": %s: Ignoring invalid fail_over_mac value %s.\n",
524 bond->dev->name, buf);
528 bond->params.fail_over_mac = new_value;
529 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
530 bond->dev->name, fail_over_mac_tbl[new_value].modename,
536 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
537 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
540 * Show and set the arp timer interval. There are two tricky bits
541 * here. First, if ARP monitoring is activated, then we must disable
542 * MII monitoring. Second, if the ARP timer isn't running, we must
545 static ssize_t bonding_show_arp_interval(struct device *d,
546 struct device_attribute *attr,
549 struct bonding *bond = to_bond(d);
551 return sprintf(buf, "%d\n", bond->params.arp_interval);
554 static ssize_t bonding_store_arp_interval(struct device *d,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
558 int new_value, ret = count;
559 struct bonding *bond = to_bond(d);
561 if (sscanf(buf, "%d", &new_value) != 1) {
563 ": %s: no arp_interval value specified.\n",
570 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
571 bond->dev->name, new_value, INT_MAX);
577 ": %s: Setting ARP monitoring interval to %d.\n",
578 bond->dev->name, new_value);
579 bond->params.arp_interval = new_value;
580 if (bond->params.arp_interval)
581 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
582 if (bond->params.miimon) {
584 ": %s: ARP monitoring cannot be used with MII monitoring. "
585 "%s Disabling MII monitoring.\n",
586 bond->dev->name, bond->dev->name);
587 bond->params.miimon = 0;
588 if (delayed_work_pending(&bond->mii_work)) {
589 cancel_delayed_work(&bond->mii_work);
590 flush_workqueue(bond->wq);
593 if (!bond->params.arp_targets[0]) {
595 ": %s: ARP monitoring has been set up, "
596 "but no ARP targets have been specified.\n",
599 if (bond->dev->flags & IFF_UP) {
600 /* If the interface is up, we may need to fire off
601 * the ARP timer. If the interface is down, the
602 * timer will get fired off when the open function
605 if (!delayed_work_pending(&bond->arp_work)) {
606 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
607 INIT_DELAYED_WORK(&bond->arp_work,
608 bond_activebackup_arp_mon);
610 INIT_DELAYED_WORK(&bond->arp_work,
611 bond_loadbalance_arp_mon);
613 queue_delayed_work(bond->wq, &bond->arp_work, 0);
620 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
621 bonding_show_arp_interval, bonding_store_arp_interval);
624 * Show and set the arp targets.
626 static ssize_t bonding_show_arp_targets(struct device *d,
627 struct device_attribute *attr,
631 struct bonding *bond = to_bond(d);
633 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
634 if (bond->params.arp_targets[i])
635 res += sprintf(buf + res, "%pI4 ",
636 &bond->params.arp_targets[i]);
639 buf[res-1] = '\n'; /* eat the leftover space */
643 static ssize_t bonding_store_arp_targets(struct device *d,
644 struct device_attribute *attr,
645 const char *buf, size_t count)
648 int i = 0, done = 0, ret = count;
649 struct bonding *bond = to_bond(d);
652 targets = bond->params.arp_targets;
653 newtarget = in_aton(buf + 1);
656 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
658 ": %s: invalid ARP target %pI4 specified for addition\n",
659 bond->dev->name, &newtarget);
663 /* look for an empty slot to put the target in, and check for dupes */
664 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
665 if (targets[i] == newtarget) { /* duplicate */
667 ": %s: ARP target %pI4 is already present\n",
668 bond->dev->name, &newtarget);
672 if (targets[i] == 0) {
674 ": %s: adding ARP target %pI4.\n",
675 bond->dev->name, &newtarget);
677 targets[i] = newtarget;
682 ": %s: ARP target table is full!\n",
688 } else if (buf[0] == '-') {
689 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
691 ": %s: invalid ARP target %pI4 specified for removal\n",
692 bond->dev->name, &newtarget);
697 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
698 if (targets[i] == newtarget) {
701 ": %s: removing ARP target %pI4.\n",
702 bond->dev->name, &newtarget);
703 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
704 targets[j] = targets[j+1];
712 ": %s: unable to remove nonexistent ARP target %pI4.\n",
713 bond->dev->name, &newtarget);
718 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
719 " for bond %s. Use +<addr> or -<addr>.\n",
728 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
731 * Show and set the up and down delays. These must be multiples of the
732 * MII monitoring value, and are stored internally as the multiplier.
733 * Thus, we must translate to MS for the real world.
735 static ssize_t bonding_show_downdelay(struct device *d,
736 struct device_attribute *attr,
739 struct bonding *bond = to_bond(d);
741 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
744 static ssize_t bonding_store_downdelay(struct device *d,
745 struct device_attribute *attr,
746 const char *buf, size_t count)
748 int new_value, ret = count;
749 struct bonding *bond = to_bond(d);
751 if (!(bond->params.miimon)) {
753 ": %s: Unable to set down delay as MII monitoring is disabled\n",
759 if (sscanf(buf, "%d", &new_value) != 1) {
761 ": %s: no down delay value specified.\n",
768 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
769 bond->dev->name, new_value, 1, INT_MAX);
773 if ((new_value % bond->params.miimon) != 0) {
774 printk(KERN_WARNING DRV_NAME
775 ": %s: Warning: down delay (%d) is not a multiple "
776 "of miimon (%d), delay rounded to %d ms\n",
777 bond->dev->name, new_value, bond->params.miimon,
778 (new_value / bond->params.miimon) *
779 bond->params.miimon);
781 bond->params.downdelay = new_value / bond->params.miimon;
782 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
784 bond->params.downdelay * bond->params.miimon);
791 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
792 bonding_show_downdelay, bonding_store_downdelay);
794 static ssize_t bonding_show_updelay(struct device *d,
795 struct device_attribute *attr,
798 struct bonding *bond = to_bond(d);
800 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
804 static ssize_t bonding_store_updelay(struct device *d,
805 struct device_attribute *attr,
806 const char *buf, size_t count)
808 int new_value, ret = count;
809 struct bonding *bond = to_bond(d);
811 if (!(bond->params.miimon)) {
813 ": %s: Unable to set up delay as MII monitoring is disabled\n",
819 if (sscanf(buf, "%d", &new_value) != 1) {
821 ": %s: no up delay value specified.\n",
828 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
829 bond->dev->name, new_value, 1, INT_MAX);
833 if ((new_value % bond->params.miimon) != 0) {
834 printk(KERN_WARNING DRV_NAME
835 ": %s: Warning: up delay (%d) is not a multiple "
836 "of miimon (%d), updelay rounded to %d ms\n",
837 bond->dev->name, new_value, bond->params.miimon,
838 (new_value / bond->params.miimon) *
839 bond->params.miimon);
841 bond->params.updelay = new_value / bond->params.miimon;
842 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
843 bond->dev->name, bond->params.updelay * bond->params.miimon);
850 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
851 bonding_show_updelay, bonding_store_updelay);
854 * Show and set the LACP interval. Interface must be down, and the mode
855 * must be set to 802.3ad mode.
857 static ssize_t bonding_show_lacp(struct device *d,
858 struct device_attribute *attr,
861 struct bonding *bond = to_bond(d);
863 return sprintf(buf, "%s %d\n",
864 bond_lacp_tbl[bond->params.lacp_fast].modename,
865 bond->params.lacp_fast);
868 static ssize_t bonding_store_lacp(struct device *d,
869 struct device_attribute *attr,
870 const char *buf, size_t count)
872 int new_value, ret = count;
873 struct bonding *bond = to_bond(d);
875 if (bond->dev->flags & IFF_UP) {
877 ": %s: Unable to update LACP rate because interface is up.\n",
883 if (bond->params.mode != BOND_MODE_8023AD) {
885 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
891 new_value = bond_parse_parm(buf, bond_lacp_tbl);
893 if ((new_value == 1) || (new_value == 0)) {
894 bond->params.lacp_fast = new_value;
895 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
896 bond->dev->name, bond_lacp_tbl[new_value].modename,
900 ": %s: Ignoring invalid LACP rate value %.*s.\n",
901 bond->dev->name, (int)strlen(buf) - 1, buf);
907 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
908 bonding_show_lacp, bonding_store_lacp);
910 static ssize_t bonding_show_ad_select(struct device *d,
911 struct device_attribute *attr,
914 struct bonding *bond = to_bond(d);
916 return sprintf(buf, "%s %d\n",
917 ad_select_tbl[bond->params.ad_select].modename,
918 bond->params.ad_select);
922 static ssize_t bonding_store_ad_select(struct device *d,
923 struct device_attribute *attr,
924 const char *buf, size_t count)
926 int new_value, ret = count;
927 struct bonding *bond = to_bond(d);
929 if (bond->dev->flags & IFF_UP) {
931 ": %s: Unable to update ad_select because interface "
932 "is up.\n", bond->dev->name);
937 new_value = bond_parse_parm(buf, ad_select_tbl);
939 if (new_value != -1) {
940 bond->params.ad_select = new_value;
942 ": %s: Setting ad_select to %s (%d).\n",
943 bond->dev->name, ad_select_tbl[new_value].modename,
947 ": %s: Ignoring invalid ad_select value %.*s.\n",
948 bond->dev->name, (int)strlen(buf) - 1, buf);
954 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
955 bonding_show_ad_select, bonding_store_ad_select);
958 * Show and set the number of grat ARP to send after a failover event.
960 static ssize_t bonding_show_n_grat_arp(struct device *d,
961 struct device_attribute *attr,
964 struct bonding *bond = to_bond(d);
966 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
969 static ssize_t bonding_store_n_grat_arp(struct device *d,
970 struct device_attribute *attr,
971 const char *buf, size_t count)
973 int new_value, ret = count;
974 struct bonding *bond = to_bond(d);
976 if (sscanf(buf, "%d", &new_value) != 1) {
978 ": %s: no num_grat_arp value specified.\n",
983 if (new_value < 0 || new_value > 255) {
985 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
986 bond->dev->name, new_value);
990 bond->params.num_grat_arp = new_value;
995 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
996 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
999 * Show and set the number of unsolicited NA's to send after a failover event.
1001 static ssize_t bonding_show_n_unsol_na(struct device *d,
1002 struct device_attribute *attr,
1005 struct bonding *bond = to_bond(d);
1007 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1010 static ssize_t bonding_store_n_unsol_na(struct device *d,
1011 struct device_attribute *attr,
1012 const char *buf, size_t count)
1014 int new_value, ret = count;
1015 struct bonding *bond = to_bond(d);
1017 if (sscanf(buf, "%d", &new_value) != 1) {
1019 ": %s: no num_unsol_na value specified.\n",
1025 if (new_value < 0 || new_value > 255) {
1027 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1028 bond->dev->name, new_value);
1032 bond->params.num_unsol_na = new_value;
1036 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1037 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1040 * Show and set the MII monitor interval. There are two tricky bits
1041 * here. First, if MII monitoring is activated, then we must disable
1042 * ARP monitoring. Second, if the timer isn't running, we must
1045 static ssize_t bonding_show_miimon(struct device *d,
1046 struct device_attribute *attr,
1049 struct bonding *bond = to_bond(d);
1051 return sprintf(buf, "%d\n", bond->params.miimon);
1054 static ssize_t bonding_store_miimon(struct device *d,
1055 struct device_attribute *attr,
1056 const char *buf, size_t count)
1058 int new_value, ret = count;
1059 struct bonding *bond = to_bond(d);
1061 if (sscanf(buf, "%d", &new_value) != 1) {
1063 ": %s: no miimon value specified.\n",
1068 if (new_value < 0) {
1070 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1071 bond->dev->name, new_value, 1, INT_MAX);
1076 ": %s: Setting MII monitoring interval to %d.\n",
1077 bond->dev->name, new_value);
1078 bond->params.miimon = new_value;
1079 if (bond->params.updelay)
1081 ": %s: Note: Updating updelay (to %d) "
1082 "since it is a multiple of the miimon value.\n",
1084 bond->params.updelay * bond->params.miimon);
1085 if (bond->params.downdelay)
1087 ": %s: Note: Updating downdelay (to %d) "
1088 "since it is a multiple of the miimon value.\n",
1090 bond->params.downdelay * bond->params.miimon);
1091 if (bond->params.arp_interval) {
1093 ": %s: MII monitoring cannot be used with "
1094 "ARP monitoring. Disabling ARP monitoring...\n",
1096 bond->params.arp_interval = 0;
1097 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1098 if (bond->params.arp_validate) {
1099 bond_unregister_arp(bond);
1100 bond->params.arp_validate =
1101 BOND_ARP_VALIDATE_NONE;
1103 if (delayed_work_pending(&bond->arp_work)) {
1104 cancel_delayed_work(&bond->arp_work);
1105 flush_workqueue(bond->wq);
1109 if (bond->dev->flags & IFF_UP) {
1110 /* If the interface is up, we may need to fire off
1111 * the MII timer. If the interface is down, the
1112 * timer will get fired off when the open function
1115 if (!delayed_work_pending(&bond->mii_work)) {
1116 INIT_DELAYED_WORK(&bond->mii_work,
1118 queue_delayed_work(bond->wq,
1119 &bond->mii_work, 0);
1126 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1127 bonding_show_miimon, bonding_store_miimon);
1130 * Show and set the primary slave. The store function is much
1131 * simpler than bonding_store_slaves function because it only needs to
1132 * handle one interface name.
1133 * The bond must be a mode that supports a primary for this be
1136 static ssize_t bonding_show_primary(struct device *d,
1137 struct device_attribute *attr,
1141 struct bonding *bond = to_bond(d);
1143 if (bond->primary_slave)
1144 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1149 static ssize_t bonding_store_primary(struct device *d,
1150 struct device_attribute *attr,
1151 const char *buf, size_t count)
1154 struct slave *slave;
1155 struct bonding *bond = to_bond(d);
1157 if (!rtnl_trylock())
1158 return restart_syscall();
1159 read_lock(&bond->lock);
1160 write_lock_bh(&bond->curr_slave_lock);
1162 if (!USES_PRIMARY(bond->params.mode)) {
1164 ": %s: Unable to set primary slave; %s is in mode %d\n",
1165 bond->dev->name, bond->dev->name, bond->params.mode);
1167 bond_for_each_slave(bond, slave, i) {
1169 (slave->dev->name, buf,
1170 strlen(slave->dev->name)) == 0) {
1172 ": %s: Setting %s as primary slave.\n",
1173 bond->dev->name, slave->dev->name);
1174 bond->primary_slave = slave;
1175 bond_select_active_slave(bond);
1180 /* if we got here, then we didn't match the name of any slave */
1182 if (strlen(buf) == 0 || buf[0] == '\n') {
1184 ": %s: Setting primary slave to None.\n",
1186 bond->primary_slave = NULL;
1187 bond_select_active_slave(bond);
1190 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1191 bond->dev->name, (int)strlen(buf) - 1, buf);
1195 write_unlock_bh(&bond->curr_slave_lock);
1196 read_unlock(&bond->lock);
1201 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1202 bonding_show_primary, bonding_store_primary);
1205 * Show and set the use_carrier flag.
1207 static ssize_t bonding_show_carrier(struct device *d,
1208 struct device_attribute *attr,
1211 struct bonding *bond = to_bond(d);
1213 return sprintf(buf, "%d\n", bond->params.use_carrier);
1216 static ssize_t bonding_store_carrier(struct device *d,
1217 struct device_attribute *attr,
1218 const char *buf, size_t count)
1220 int new_value, ret = count;
1221 struct bonding *bond = to_bond(d);
1224 if (sscanf(buf, "%d", &new_value) != 1) {
1226 ": %s: no use_carrier value specified.\n",
1231 if ((new_value == 0) || (new_value == 1)) {
1232 bond->params.use_carrier = new_value;
1233 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
1234 bond->dev->name, new_value);
1237 ": %s: Ignoring invalid use_carrier value %d.\n",
1238 bond->dev->name, new_value);
1243 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1244 bonding_show_carrier, bonding_store_carrier);
1248 * Show and set currently active_slave.
1250 static ssize_t bonding_show_active_slave(struct device *d,
1251 struct device_attribute *attr,
1255 struct bonding *bond = to_bond(d);
1258 read_lock(&bond->curr_slave_lock);
1259 curr = bond->curr_active_slave;
1260 read_unlock(&bond->curr_slave_lock);
1262 if (USES_PRIMARY(bond->params.mode) && curr)
1263 count = sprintf(buf, "%s\n", curr->dev->name);
1267 static ssize_t bonding_store_active_slave(struct device *d,
1268 struct device_attribute *attr,
1269 const char *buf, size_t count)
1272 struct slave *slave;
1273 struct slave *old_active = NULL;
1274 struct slave *new_active = NULL;
1275 struct bonding *bond = to_bond(d);
1277 if (!rtnl_trylock())
1278 return restart_syscall();
1279 read_lock(&bond->lock);
1280 write_lock_bh(&bond->curr_slave_lock);
1282 if (!USES_PRIMARY(bond->params.mode))
1283 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1284 " %s is in mode %d\n",
1285 bond->dev->name, bond->dev->name, bond->params.mode);
1287 bond_for_each_slave(bond, slave, i) {
1289 (slave->dev->name, buf,
1290 strlen(slave->dev->name)) == 0) {
1291 old_active = bond->curr_active_slave;
1293 if (new_active == old_active) {
1295 printk(KERN_INFO DRV_NAME
1296 ": %s: %s is already the current active slave.\n",
1297 bond->dev->name, slave->dev->name);
1303 (new_active->link == BOND_LINK_UP) &&
1304 IS_UP(new_active->dev)) {
1305 printk(KERN_INFO DRV_NAME
1306 ": %s: Setting %s as active slave.\n",
1307 bond->dev->name, slave->dev->name);
1308 bond_change_active_slave(bond, new_active);
1311 printk(KERN_INFO DRV_NAME
1312 ": %s: Could not set %s as active slave; "
1313 "either %s is down or the link is down.\n",
1314 bond->dev->name, slave->dev->name,
1322 /* if we got here, then we didn't match the name of any slave */
1324 if (strlen(buf) == 0 || buf[0] == '\n') {
1326 ": %s: Setting active slave to None.\n",
1328 bond->primary_slave = NULL;
1329 bond_select_active_slave(bond);
1331 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1332 " as active slave as it is not a slave.\n",
1333 bond->dev->name, (int)strlen(buf) - 1, buf);
1337 write_unlock_bh(&bond->curr_slave_lock);
1338 read_unlock(&bond->lock);
1344 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1345 bonding_show_active_slave, bonding_store_active_slave);
1349 * Show link status of the bond interface.
1351 static ssize_t bonding_show_mii_status(struct device *d,
1352 struct device_attribute *attr,
1356 struct bonding *bond = to_bond(d);
1358 read_lock(&bond->curr_slave_lock);
1359 curr = bond->curr_active_slave;
1360 read_unlock(&bond->curr_slave_lock);
1362 return sprintf(buf, "%s\n", curr ? "up" : "down");
1364 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1368 * Show current 802.3ad aggregator ID.
1370 static ssize_t bonding_show_ad_aggregator(struct device *d,
1371 struct device_attribute *attr,
1375 struct bonding *bond = to_bond(d);
1377 if (bond->params.mode == BOND_MODE_8023AD) {
1378 struct ad_info ad_info;
1379 count = sprintf(buf, "%d\n",
1380 (bond_3ad_get_active_agg_info(bond, &ad_info))
1381 ? 0 : ad_info.aggregator_id);
1386 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1390 * Show number of active 802.3ad ports.
1392 static ssize_t bonding_show_ad_num_ports(struct device *d,
1393 struct device_attribute *attr,
1397 struct bonding *bond = to_bond(d);
1399 if (bond->params.mode == BOND_MODE_8023AD) {
1400 struct ad_info ad_info;
1401 count = sprintf(buf, "%d\n",
1402 (bond_3ad_get_active_agg_info(bond, &ad_info))
1403 ? 0 : ad_info.ports);
1408 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1412 * Show current 802.3ad actor key.
1414 static ssize_t bonding_show_ad_actor_key(struct device *d,
1415 struct device_attribute *attr,
1419 struct bonding *bond = to_bond(d);
1421 if (bond->params.mode == BOND_MODE_8023AD) {
1422 struct ad_info ad_info;
1423 count = sprintf(buf, "%d\n",
1424 (bond_3ad_get_active_agg_info(bond, &ad_info))
1425 ? 0 : ad_info.actor_key);
1430 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1434 * Show current 802.3ad partner key.
1436 static ssize_t bonding_show_ad_partner_key(struct device *d,
1437 struct device_attribute *attr,
1441 struct bonding *bond = to_bond(d);
1443 if (bond->params.mode == BOND_MODE_8023AD) {
1444 struct ad_info ad_info;
1445 count = sprintf(buf, "%d\n",
1446 (bond_3ad_get_active_agg_info(bond, &ad_info))
1447 ? 0 : ad_info.partner_key);
1452 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1456 * Show current 802.3ad partner mac.
1458 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1459 struct device_attribute *attr,
1463 struct bonding *bond = to_bond(d);
1465 if (bond->params.mode == BOND_MODE_8023AD) {
1466 struct ad_info ad_info;
1467 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1468 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1473 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1477 static struct attribute *per_bond_attrs[] = {
1478 &dev_attr_slaves.attr,
1479 &dev_attr_mode.attr,
1480 &dev_attr_fail_over_mac.attr,
1481 &dev_attr_arp_validate.attr,
1482 &dev_attr_arp_interval.attr,
1483 &dev_attr_arp_ip_target.attr,
1484 &dev_attr_downdelay.attr,
1485 &dev_attr_updelay.attr,
1486 &dev_attr_lacp_rate.attr,
1487 &dev_attr_ad_select.attr,
1488 &dev_attr_xmit_hash_policy.attr,
1489 &dev_attr_num_grat_arp.attr,
1490 &dev_attr_num_unsol_na.attr,
1491 &dev_attr_miimon.attr,
1492 &dev_attr_primary.attr,
1493 &dev_attr_use_carrier.attr,
1494 &dev_attr_active_slave.attr,
1495 &dev_attr_mii_status.attr,
1496 &dev_attr_ad_aggregator.attr,
1497 &dev_attr_ad_num_ports.attr,
1498 &dev_attr_ad_actor_key.attr,
1499 &dev_attr_ad_partner_key.attr,
1500 &dev_attr_ad_partner_mac.attr,
1504 static struct attribute_group bonding_group = {
1506 .attrs = per_bond_attrs,
1510 * Initialize sysfs. This sets up the bonding_masters file in
1513 int bond_create_sysfs(void)
1517 ret = netdev_class_create_file(&class_attr_bonding_masters);
1519 * Permit multiple loads of the module by ignoring failures to
1520 * create the bonding_masters sysfs file. Bonding devices
1521 * created by second or subsequent loads of the module will
1522 * not be listed in, or controllable by, bonding_masters, but
1523 * will have the usual "bonding" sysfs directory.
1525 * This is done to preserve backwards compatibility for
1526 * initscripts/sysconfig, which load bonding multiple times to
1527 * configure multiple bonding devices.
1529 if (ret == -EEXIST) {
1530 /* Is someone being kinky and naming a device bonding_master? */
1531 if (__dev_get_by_name(&init_net,
1532 class_attr_bonding_masters.attr.name))
1534 "network device named %s already exists in sysfs",
1535 class_attr_bonding_masters.attr.name);
1544 * Remove /sys/class/net/bonding_masters.
1546 void bond_destroy_sysfs(void)
1548 netdev_class_remove_file(&class_attr_bonding_masters);
1552 * Initialize sysfs for each bond. This sets up and registers
1553 * the 'bondctl' directory for each individual bond under /sys/class/net.
1555 int bond_create_sysfs_entry(struct bonding *bond)
1557 struct net_device *dev = bond->dev;
1560 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
1562 printk(KERN_EMERG "eek! didn't create group!\n");
1567 * Remove sysfs entries for each bond.
1569 void bond_destroy_sysfs_entry(struct bonding *bond)
1571 struct net_device *dev = bond->dev;
1573 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);