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 <linux/etherdevice.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))))
45 * "show" function for the bond_masters attribute.
46 * The class parameter is ignored.
48 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
55 list_for_each_entry(bond, &bond_dev_list, bond_list) {
56 if (res > (PAGE_SIZE - IFNAMSIZ)) {
57 /* not enough space for another interface name */
58 if ((PAGE_SIZE - res) > 10)
60 res += sprintf(buf + res, "++more++ ");
63 res += sprintf(buf + res, "%s ", bond->dev->name);
66 buf[res-1] = '\n'; /* eat the leftover space */
72 static struct net_device *bond_get_by_name(const char *ifname)
76 list_for_each_entry(bond, &bond_dev_list, bond_list) {
77 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
84 * "store" function for the bond_masters attribute. This is what
85 * creates and deletes entire bonds.
87 * The class parameter is ignored.
91 static ssize_t bonding_store_bonds(struct class *cls,
92 const char *buffer, size_t count)
94 char command[IFNAMSIZ + 1] = {0, };
98 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
100 if ((strlen(command) <= 1) ||
101 !dev_valid_name(ifname))
104 if (command[0] == '+') {
106 ": %s is being created...\n", ifname);
107 rv = bond_create(ifname);
109 pr_info(DRV_NAME ": Bond creation failed.\n");
112 } else if (command[0] == '-') {
113 struct net_device *bond_dev;
116 bond_dev = bond_get_by_name(ifname);
118 pr_info(DRV_NAME ": %s is being deleted...\n",
120 unregister_netdevice(bond_dev);
122 pr_err(DRV_NAME ": unable to delete non-existent %s\n",
130 /* Always return either count or an error. If you return 0, you'll
131 * get called forever, which is bad.
136 pr_err(DRV_NAME ": no command found in bonding_masters."
137 " Use +ifname or -ifname.\n");
141 /* class attribute for bond_masters file. This ends up in /sys/class/net */
142 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
143 bonding_show_bonds, bonding_store_bonds);
145 int bond_create_slave_symlinks(struct net_device *master,
146 struct net_device *slave)
148 char linkname[IFNAMSIZ+7];
151 /* first, create a link from the slave back to the master */
152 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
156 /* next, create a link from the master to the slave */
157 sprintf(linkname, "slave_%s", slave->name);
158 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
164 void bond_destroy_slave_symlinks(struct net_device *master,
165 struct net_device *slave)
167 char linkname[IFNAMSIZ+7];
169 sysfs_remove_link(&(slave->dev.kobj), "master");
170 sprintf(linkname, "slave_%s", slave->name);
171 sysfs_remove_link(&(master->dev.kobj), linkname);
176 * Show the slaves in the current bond.
178 static ssize_t bonding_show_slaves(struct device *d,
179 struct device_attribute *attr, char *buf)
183 struct bonding *bond = to_bond(d);
185 read_lock(&bond->lock);
186 bond_for_each_slave(bond, slave, i) {
187 if (res > (PAGE_SIZE - IFNAMSIZ)) {
188 /* not enough space for another interface name */
189 if ((PAGE_SIZE - res) > 10)
190 res = PAGE_SIZE - 10;
191 res += sprintf(buf + res, "++more++ ");
194 res += sprintf(buf + res, "%s ", slave->dev->name);
196 read_unlock(&bond->lock);
198 buf[res-1] = '\n'; /* eat the leftover space */
203 * Set the slaves in the current bond. The bond interface must be
204 * up for this to succeed.
205 * This function is largely the same flow as bonding_update_bonds().
207 static ssize_t bonding_store_slaves(struct device *d,
208 struct device_attribute *attr,
209 const char *buffer, size_t count)
211 char command[IFNAMSIZ + 1] = { 0, };
213 int i, res, found, ret = count;
216 struct net_device *dev = NULL;
217 struct bonding *bond = to_bond(d);
219 /* Quick sanity check -- is the bond interface up? */
220 if (!(bond->dev->flags & IFF_UP)) {
221 printk(KERN_WARNING DRV_NAME
222 ": %s: doing slave updates when interface is down.\n",
226 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
229 return restart_syscall();
231 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
232 ifname = command + 1;
233 if ((strlen(command) <= 1) ||
234 !dev_valid_name(ifname))
237 if (command[0] == '+') {
239 /* Got a slave name in ifname. Is it already in the list? */
242 /* FIXME: get netns from sysfs object */
243 dev = __dev_get_by_name(&init_net, ifname);
246 ": %s: Interface %s does not exist!\n",
247 bond->dev->name, ifname);
252 if (dev->flags & IFF_UP) {
254 ": %s: Error: Unable to enslave %s "
255 "because it is already up.\n",
256 bond->dev->name, dev->name);
261 read_lock(&bond->lock);
262 bond_for_each_slave(bond, slave, i)
263 if (slave->dev == dev) {
265 ": %s: Interface %s is already enslaved!\n",
266 bond->dev->name, ifname);
268 read_unlock(&bond->lock);
271 read_unlock(&bond->lock);
273 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
274 bond->dev->name, ifname);
276 /* If this is the first slave, then we need to set
277 the master's hardware address to be the same as the
279 if (is_zero_ether_addr(bond->dev->dev_addr))
280 memcpy(bond->dev->dev_addr, dev->dev_addr,
283 /* Set the slave's MTU to match the bond */
284 original_mtu = dev->mtu;
285 res = dev_set_mtu(dev, bond->dev->mtu);
291 res = bond_enslave(bond->dev, dev);
292 bond_for_each_slave(bond, slave, i)
293 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
294 slave->original_mtu = original_mtu;
301 if (command[0] == '-') {
304 bond_for_each_slave(bond, slave, i)
305 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
307 original_mtu = slave->original_mtu;
311 pr_info(DRV_NAME ": %s: Removing slave %s\n",
312 bond->dev->name, dev->name);
313 res = bond_release(bond->dev, dev);
318 /* set the slave MTU to the default */
319 dev_set_mtu(dev, original_mtu);
321 pr_err(DRV_NAME ": unable to remove non-existent"
322 " slave %s for bond %s.\n",
323 ifname, bond->dev->name);
330 pr_err(DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
338 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
339 bonding_store_slaves);
342 * Show and set the bonding mode. The bond interface must be down to
345 static ssize_t bonding_show_mode(struct device *d,
346 struct device_attribute *attr, char *buf)
348 struct bonding *bond = to_bond(d);
350 return sprintf(buf, "%s %d\n",
351 bond_mode_tbl[bond->params.mode].modename,
355 static ssize_t bonding_store_mode(struct device *d,
356 struct device_attribute *attr,
357 const char *buf, size_t count)
359 int new_value, ret = count;
360 struct bonding *bond = to_bond(d);
362 if (bond->dev->flags & IFF_UP) {
363 pr_err(DRV_NAME ": unable to update mode of %s"
364 " because interface is up.\n", bond->dev->name);
369 new_value = bond_parse_parm(buf, bond_mode_tbl);
372 ": %s: Ignoring invalid mode value %.*s.\n",
374 (int)strlen(buf) - 1, buf);
378 if (bond->params.mode == BOND_MODE_8023AD)
379 bond_unset_master_3ad_flags(bond);
381 if (bond->params.mode == BOND_MODE_ALB)
382 bond_unset_master_alb_flags(bond);
384 bond->params.mode = new_value;
385 bond_set_mode_ops(bond, bond->params.mode);
386 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
387 bond->dev->name, bond_mode_tbl[new_value].modename,
393 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
394 bonding_show_mode, bonding_store_mode);
397 * Show and set the bonding transmit hash method.
398 * The bond interface must be down to change the xmit hash policy.
400 static ssize_t bonding_show_xmit_hash(struct device *d,
401 struct device_attribute *attr,
404 struct bonding *bond = to_bond(d);
406 return sprintf(buf, "%s %d\n",
407 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
408 bond->params.xmit_policy);
411 static ssize_t bonding_store_xmit_hash(struct device *d,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
415 int new_value, ret = count;
416 struct bonding *bond = to_bond(d);
418 if (bond->dev->flags & IFF_UP) {
420 "%s: Interface is up. Unable to update xmit policy.\n",
426 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
429 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
431 (int)strlen(buf) - 1, buf);
435 bond->params.xmit_policy = new_value;
436 bond_set_mode_ops(bond, bond->params.mode);
437 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
439 xmit_hashtype_tbl[new_value].modename, new_value);
444 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
445 bonding_show_xmit_hash, bonding_store_xmit_hash);
448 * Show and set arp_validate.
450 static ssize_t bonding_show_arp_validate(struct device *d,
451 struct device_attribute *attr,
454 struct bonding *bond = to_bond(d);
456 return sprintf(buf, "%s %d\n",
457 arp_validate_tbl[bond->params.arp_validate].modename,
458 bond->params.arp_validate);
461 static ssize_t bonding_store_arp_validate(struct device *d,
462 struct device_attribute *attr,
463 const char *buf, size_t count)
466 struct bonding *bond = to_bond(d);
468 new_value = bond_parse_parm(buf, arp_validate_tbl);
471 ": %s: Ignoring invalid arp_validate value %s\n",
472 bond->dev->name, buf);
475 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
477 ": %s: arp_validate only supported in active-backup mode.\n",
481 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
482 bond->dev->name, arp_validate_tbl[new_value].modename,
485 if (!bond->params.arp_validate && new_value)
486 bond_register_arp(bond);
487 else if (bond->params.arp_validate && !new_value)
488 bond_unregister_arp(bond);
490 bond->params.arp_validate = new_value;
495 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
496 bonding_store_arp_validate);
499 * Show and store fail_over_mac. User only allowed to change the
500 * value when there are no slaves.
502 static ssize_t bonding_show_fail_over_mac(struct device *d,
503 struct device_attribute *attr,
506 struct bonding *bond = to_bond(d);
508 return sprintf(buf, "%s %d\n",
509 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
510 bond->params.fail_over_mac);
513 static ssize_t bonding_store_fail_over_mac(struct device *d,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
518 struct bonding *bond = to_bond(d);
520 if (bond->slave_cnt != 0) {
522 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
527 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
530 ": %s: Ignoring invalid fail_over_mac value %s.\n",
531 bond->dev->name, buf);
535 bond->params.fail_over_mac = new_value;
536 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
537 bond->dev->name, fail_over_mac_tbl[new_value].modename,
543 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
544 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
547 * Show and set the arp timer interval. There are two tricky bits
548 * here. First, if ARP monitoring is activated, then we must disable
549 * MII monitoring. Second, if the ARP timer isn't running, we must
552 static ssize_t bonding_show_arp_interval(struct device *d,
553 struct device_attribute *attr,
556 struct bonding *bond = to_bond(d);
558 return sprintf(buf, "%d\n", bond->params.arp_interval);
561 static ssize_t bonding_store_arp_interval(struct device *d,
562 struct device_attribute *attr,
563 const char *buf, size_t count)
565 int new_value, ret = count;
566 struct bonding *bond = to_bond(d);
568 if (sscanf(buf, "%d", &new_value) != 1) {
570 ": %s: no arp_interval value specified.\n",
577 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
578 bond->dev->name, new_value, INT_MAX);
584 ": %s: Setting ARP monitoring interval to %d.\n",
585 bond->dev->name, new_value);
586 bond->params.arp_interval = new_value;
587 if (bond->params.arp_interval)
588 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
589 if (bond->params.miimon) {
591 ": %s: ARP monitoring cannot be used with MII monitoring. "
592 "%s Disabling MII monitoring.\n",
593 bond->dev->name, bond->dev->name);
594 bond->params.miimon = 0;
595 if (delayed_work_pending(&bond->mii_work)) {
596 cancel_delayed_work(&bond->mii_work);
597 flush_workqueue(bond->wq);
600 if (!bond->params.arp_targets[0]) {
602 ": %s: ARP monitoring has been set up, "
603 "but no ARP targets have been specified.\n",
606 if (bond->dev->flags & IFF_UP) {
607 /* If the interface is up, we may need to fire off
608 * the ARP timer. If the interface is down, the
609 * timer will get fired off when the open function
612 if (!delayed_work_pending(&bond->arp_work)) {
613 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
614 INIT_DELAYED_WORK(&bond->arp_work,
615 bond_activebackup_arp_mon);
617 INIT_DELAYED_WORK(&bond->arp_work,
618 bond_loadbalance_arp_mon);
620 queue_delayed_work(bond->wq, &bond->arp_work, 0);
627 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
628 bonding_show_arp_interval, bonding_store_arp_interval);
631 * Show and set the arp targets.
633 static ssize_t bonding_show_arp_targets(struct device *d,
634 struct device_attribute *attr,
638 struct bonding *bond = to_bond(d);
640 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
641 if (bond->params.arp_targets[i])
642 res += sprintf(buf + res, "%pI4 ",
643 &bond->params.arp_targets[i]);
646 buf[res-1] = '\n'; /* eat the leftover space */
650 static ssize_t bonding_store_arp_targets(struct device *d,
651 struct device_attribute *attr,
652 const char *buf, size_t count)
655 int i = 0, done = 0, ret = count;
656 struct bonding *bond = to_bond(d);
659 targets = bond->params.arp_targets;
660 newtarget = in_aton(buf + 1);
663 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
665 ": %s: invalid ARP target %pI4 specified for addition\n",
666 bond->dev->name, &newtarget);
670 /* look for an empty slot to put the target in, and check for dupes */
671 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
672 if (targets[i] == newtarget) { /* duplicate */
674 ": %s: ARP target %pI4 is already present\n",
675 bond->dev->name, &newtarget);
679 if (targets[i] == 0) {
681 ": %s: adding ARP target %pI4.\n",
682 bond->dev->name, &newtarget);
684 targets[i] = newtarget;
689 ": %s: ARP target table is full!\n",
695 } else if (buf[0] == '-') {
696 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
698 ": %s: invalid ARP target %pI4 specified for removal\n",
699 bond->dev->name, &newtarget);
704 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
705 if (targets[i] == newtarget) {
708 ": %s: removing ARP target %pI4.\n",
709 bond->dev->name, &newtarget);
710 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
711 targets[j] = targets[j+1];
719 ": %s: unable to remove nonexistent ARP target %pI4.\n",
720 bond->dev->name, &newtarget);
725 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
726 " for bond %s. Use +<addr> or -<addr>.\n",
735 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
738 * Show and set the up and down delays. These must be multiples of the
739 * MII monitoring value, and are stored internally as the multiplier.
740 * Thus, we must translate to MS for the real world.
742 static ssize_t bonding_show_downdelay(struct device *d,
743 struct device_attribute *attr,
746 struct bonding *bond = to_bond(d);
748 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
751 static ssize_t bonding_store_downdelay(struct device *d,
752 struct device_attribute *attr,
753 const char *buf, size_t count)
755 int new_value, ret = count;
756 struct bonding *bond = to_bond(d);
758 if (!(bond->params.miimon)) {
760 ": %s: Unable to set down delay as MII monitoring is disabled\n",
766 if (sscanf(buf, "%d", &new_value) != 1) {
768 ": %s: no down delay value specified.\n",
775 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
776 bond->dev->name, new_value, 1, INT_MAX);
780 if ((new_value % bond->params.miimon) != 0) {
781 printk(KERN_WARNING DRV_NAME
782 ": %s: Warning: down delay (%d) is not a multiple "
783 "of miimon (%d), delay rounded to %d ms\n",
784 bond->dev->name, new_value, bond->params.miimon,
785 (new_value / bond->params.miimon) *
786 bond->params.miimon);
788 bond->params.downdelay = new_value / bond->params.miimon;
789 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
791 bond->params.downdelay * bond->params.miimon);
798 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
799 bonding_show_downdelay, bonding_store_downdelay);
801 static ssize_t bonding_show_updelay(struct device *d,
802 struct device_attribute *attr,
805 struct bonding *bond = to_bond(d);
807 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
811 static ssize_t bonding_store_updelay(struct device *d,
812 struct device_attribute *attr,
813 const char *buf, size_t count)
815 int new_value, ret = count;
816 struct bonding *bond = to_bond(d);
818 if (!(bond->params.miimon)) {
820 ": %s: Unable to set up delay as MII monitoring is disabled\n",
826 if (sscanf(buf, "%d", &new_value) != 1) {
828 ": %s: no up delay value specified.\n",
835 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
836 bond->dev->name, new_value, 1, INT_MAX);
840 if ((new_value % bond->params.miimon) != 0) {
841 printk(KERN_WARNING DRV_NAME
842 ": %s: Warning: up delay (%d) is not a multiple "
843 "of miimon (%d), updelay rounded to %d ms\n",
844 bond->dev->name, new_value, bond->params.miimon,
845 (new_value / bond->params.miimon) *
846 bond->params.miimon);
848 bond->params.updelay = new_value / bond->params.miimon;
849 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
850 bond->dev->name, bond->params.updelay * bond->params.miimon);
857 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
858 bonding_show_updelay, bonding_store_updelay);
861 * Show and set the LACP interval. Interface must be down, and the mode
862 * must be set to 802.3ad mode.
864 static ssize_t bonding_show_lacp(struct device *d,
865 struct device_attribute *attr,
868 struct bonding *bond = to_bond(d);
870 return sprintf(buf, "%s %d\n",
871 bond_lacp_tbl[bond->params.lacp_fast].modename,
872 bond->params.lacp_fast);
875 static ssize_t bonding_store_lacp(struct device *d,
876 struct device_attribute *attr,
877 const char *buf, size_t count)
879 int new_value, ret = count;
880 struct bonding *bond = to_bond(d);
882 if (bond->dev->flags & IFF_UP) {
884 ": %s: Unable to update LACP rate because interface is up.\n",
890 if (bond->params.mode != BOND_MODE_8023AD) {
892 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
898 new_value = bond_parse_parm(buf, bond_lacp_tbl);
900 if ((new_value == 1) || (new_value == 0)) {
901 bond->params.lacp_fast = new_value;
902 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
903 bond->dev->name, bond_lacp_tbl[new_value].modename,
907 ": %s: Ignoring invalid LACP rate value %.*s.\n",
908 bond->dev->name, (int)strlen(buf) - 1, buf);
914 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
915 bonding_show_lacp, bonding_store_lacp);
917 static ssize_t bonding_show_ad_select(struct device *d,
918 struct device_attribute *attr,
921 struct bonding *bond = to_bond(d);
923 return sprintf(buf, "%s %d\n",
924 ad_select_tbl[bond->params.ad_select].modename,
925 bond->params.ad_select);
929 static ssize_t bonding_store_ad_select(struct device *d,
930 struct device_attribute *attr,
931 const char *buf, size_t count)
933 int new_value, ret = count;
934 struct bonding *bond = to_bond(d);
936 if (bond->dev->flags & IFF_UP) {
938 ": %s: Unable to update ad_select because interface "
939 "is up.\n", bond->dev->name);
944 new_value = bond_parse_parm(buf, ad_select_tbl);
946 if (new_value != -1) {
947 bond->params.ad_select = new_value;
949 ": %s: Setting ad_select to %s (%d).\n",
950 bond->dev->name, ad_select_tbl[new_value].modename,
954 ": %s: Ignoring invalid ad_select value %.*s.\n",
955 bond->dev->name, (int)strlen(buf) - 1, buf);
961 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
962 bonding_show_ad_select, bonding_store_ad_select);
965 * Show and set the number of grat ARP to send after a failover event.
967 static ssize_t bonding_show_n_grat_arp(struct device *d,
968 struct device_attribute *attr,
971 struct bonding *bond = to_bond(d);
973 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
976 static ssize_t bonding_store_n_grat_arp(struct device *d,
977 struct device_attribute *attr,
978 const char *buf, size_t count)
980 int new_value, ret = count;
981 struct bonding *bond = to_bond(d);
983 if (sscanf(buf, "%d", &new_value) != 1) {
985 ": %s: no num_grat_arp value specified.\n",
990 if (new_value < 0 || new_value > 255) {
992 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
993 bond->dev->name, new_value);
997 bond->params.num_grat_arp = new_value;
1002 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1003 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
1006 * Show and set the number of unsolicited NA's to send after a failover event.
1008 static ssize_t bonding_show_n_unsol_na(struct device *d,
1009 struct device_attribute *attr,
1012 struct bonding *bond = to_bond(d);
1014 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1017 static ssize_t bonding_store_n_unsol_na(struct device *d,
1018 struct device_attribute *attr,
1019 const char *buf, size_t count)
1021 int new_value, ret = count;
1022 struct bonding *bond = to_bond(d);
1024 if (sscanf(buf, "%d", &new_value) != 1) {
1026 ": %s: no num_unsol_na value specified.\n",
1032 if (new_value < 0 || new_value > 255) {
1034 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1035 bond->dev->name, new_value);
1039 bond->params.num_unsol_na = new_value;
1043 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1044 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1047 * Show and set the MII monitor interval. There are two tricky bits
1048 * here. First, if MII monitoring is activated, then we must disable
1049 * ARP monitoring. Second, if the timer isn't running, we must
1052 static ssize_t bonding_show_miimon(struct device *d,
1053 struct device_attribute *attr,
1056 struct bonding *bond = to_bond(d);
1058 return sprintf(buf, "%d\n", bond->params.miimon);
1061 static ssize_t bonding_store_miimon(struct device *d,
1062 struct device_attribute *attr,
1063 const char *buf, size_t count)
1065 int new_value, ret = count;
1066 struct bonding *bond = to_bond(d);
1068 if (sscanf(buf, "%d", &new_value) != 1) {
1070 ": %s: no miimon value specified.\n",
1075 if (new_value < 0) {
1077 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1078 bond->dev->name, new_value, 1, INT_MAX);
1083 ": %s: Setting MII monitoring interval to %d.\n",
1084 bond->dev->name, new_value);
1085 bond->params.miimon = new_value;
1086 if (bond->params.updelay)
1088 ": %s: Note: Updating updelay (to %d) "
1089 "since it is a multiple of the miimon value.\n",
1091 bond->params.updelay * bond->params.miimon);
1092 if (bond->params.downdelay)
1094 ": %s: Note: Updating downdelay (to %d) "
1095 "since it is a multiple of the miimon value.\n",
1097 bond->params.downdelay * bond->params.miimon);
1098 if (bond->params.arp_interval) {
1100 ": %s: MII monitoring cannot be used with "
1101 "ARP monitoring. Disabling ARP monitoring...\n",
1103 bond->params.arp_interval = 0;
1104 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1105 if (bond->params.arp_validate) {
1106 bond_unregister_arp(bond);
1107 bond->params.arp_validate =
1108 BOND_ARP_VALIDATE_NONE;
1110 if (delayed_work_pending(&bond->arp_work)) {
1111 cancel_delayed_work(&bond->arp_work);
1112 flush_workqueue(bond->wq);
1116 if (bond->dev->flags & IFF_UP) {
1117 /* If the interface is up, we may need to fire off
1118 * the MII timer. If the interface is down, the
1119 * timer will get fired off when the open function
1122 if (!delayed_work_pending(&bond->mii_work)) {
1123 INIT_DELAYED_WORK(&bond->mii_work,
1125 queue_delayed_work(bond->wq,
1126 &bond->mii_work, 0);
1133 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1134 bonding_show_miimon, bonding_store_miimon);
1137 * Show and set the primary slave. The store function is much
1138 * simpler than bonding_store_slaves function because it only needs to
1139 * handle one interface name.
1140 * The bond must be a mode that supports a primary for this be
1143 static ssize_t bonding_show_primary(struct device *d,
1144 struct device_attribute *attr,
1148 struct bonding *bond = to_bond(d);
1150 if (bond->primary_slave)
1151 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1156 static ssize_t bonding_store_primary(struct device *d,
1157 struct device_attribute *attr,
1158 const char *buf, size_t count)
1161 struct slave *slave;
1162 struct bonding *bond = to_bond(d);
1164 if (!rtnl_trylock())
1165 return restart_syscall();
1166 read_lock(&bond->lock);
1167 write_lock_bh(&bond->curr_slave_lock);
1169 if (!USES_PRIMARY(bond->params.mode)) {
1171 ": %s: Unable to set primary slave; %s is in mode %d\n",
1172 bond->dev->name, bond->dev->name, bond->params.mode);
1174 bond_for_each_slave(bond, slave, i) {
1176 (slave->dev->name, buf,
1177 strlen(slave->dev->name)) == 0) {
1179 ": %s: Setting %s as primary slave.\n",
1180 bond->dev->name, slave->dev->name);
1181 bond->primary_slave = slave;
1182 bond_select_active_slave(bond);
1187 /* if we got here, then we didn't match the name of any slave */
1189 if (strlen(buf) == 0 || buf[0] == '\n') {
1191 ": %s: Setting primary slave to None.\n",
1193 bond->primary_slave = NULL;
1194 bond_select_active_slave(bond);
1197 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1198 bond->dev->name, (int)strlen(buf) - 1, buf);
1202 write_unlock_bh(&bond->curr_slave_lock);
1203 read_unlock(&bond->lock);
1208 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1209 bonding_show_primary, bonding_store_primary);
1212 * Show and set the use_carrier flag.
1214 static ssize_t bonding_show_carrier(struct device *d,
1215 struct device_attribute *attr,
1218 struct bonding *bond = to_bond(d);
1220 return sprintf(buf, "%d\n", bond->params.use_carrier);
1223 static ssize_t bonding_store_carrier(struct device *d,
1224 struct device_attribute *attr,
1225 const char *buf, size_t count)
1227 int new_value, ret = count;
1228 struct bonding *bond = to_bond(d);
1231 if (sscanf(buf, "%d", &new_value) != 1) {
1233 ": %s: no use_carrier value specified.\n",
1238 if ((new_value == 0) || (new_value == 1)) {
1239 bond->params.use_carrier = new_value;
1240 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
1241 bond->dev->name, new_value);
1244 ": %s: Ignoring invalid use_carrier value %d.\n",
1245 bond->dev->name, new_value);
1250 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1251 bonding_show_carrier, bonding_store_carrier);
1255 * Show and set currently active_slave.
1257 static ssize_t bonding_show_active_slave(struct device *d,
1258 struct device_attribute *attr,
1262 struct bonding *bond = to_bond(d);
1265 read_lock(&bond->curr_slave_lock);
1266 curr = bond->curr_active_slave;
1267 read_unlock(&bond->curr_slave_lock);
1269 if (USES_PRIMARY(bond->params.mode) && curr)
1270 count = sprintf(buf, "%s\n", curr->dev->name);
1274 static ssize_t bonding_store_active_slave(struct device *d,
1275 struct device_attribute *attr,
1276 const char *buf, size_t count)
1279 struct slave *slave;
1280 struct slave *old_active = NULL;
1281 struct slave *new_active = NULL;
1282 struct bonding *bond = to_bond(d);
1284 if (!rtnl_trylock())
1285 return restart_syscall();
1286 read_lock(&bond->lock);
1287 write_lock_bh(&bond->curr_slave_lock);
1289 if (!USES_PRIMARY(bond->params.mode))
1290 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1291 " %s is in mode %d\n",
1292 bond->dev->name, bond->dev->name, bond->params.mode);
1294 bond_for_each_slave(bond, slave, i) {
1296 (slave->dev->name, buf,
1297 strlen(slave->dev->name)) == 0) {
1298 old_active = bond->curr_active_slave;
1300 if (new_active == old_active) {
1302 printk(KERN_INFO DRV_NAME
1303 ": %s: %s is already the current active slave.\n",
1304 bond->dev->name, slave->dev->name);
1310 (new_active->link == BOND_LINK_UP) &&
1311 IS_UP(new_active->dev)) {
1312 printk(KERN_INFO DRV_NAME
1313 ": %s: Setting %s as active slave.\n",
1314 bond->dev->name, slave->dev->name);
1315 bond_change_active_slave(bond, new_active);
1318 printk(KERN_INFO DRV_NAME
1319 ": %s: Could not set %s as active slave; "
1320 "either %s is down or the link is down.\n",
1321 bond->dev->name, slave->dev->name,
1329 /* if we got here, then we didn't match the name of any slave */
1331 if (strlen(buf) == 0 || buf[0] == '\n') {
1333 ": %s: Setting active slave to None.\n",
1335 bond->primary_slave = NULL;
1336 bond_select_active_slave(bond);
1338 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1339 " as active slave as it is not a slave.\n",
1340 bond->dev->name, (int)strlen(buf) - 1, buf);
1344 write_unlock_bh(&bond->curr_slave_lock);
1345 read_unlock(&bond->lock);
1351 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1352 bonding_show_active_slave, bonding_store_active_slave);
1356 * Show link status of the bond interface.
1358 static ssize_t bonding_show_mii_status(struct device *d,
1359 struct device_attribute *attr,
1363 struct bonding *bond = to_bond(d);
1365 read_lock(&bond->curr_slave_lock);
1366 curr = bond->curr_active_slave;
1367 read_unlock(&bond->curr_slave_lock);
1369 return sprintf(buf, "%s\n", curr ? "up" : "down");
1371 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1375 * Show current 802.3ad aggregator ID.
1377 static ssize_t bonding_show_ad_aggregator(struct device *d,
1378 struct device_attribute *attr,
1382 struct bonding *bond = to_bond(d);
1384 if (bond->params.mode == BOND_MODE_8023AD) {
1385 struct ad_info ad_info;
1386 count = sprintf(buf, "%d\n",
1387 (bond_3ad_get_active_agg_info(bond, &ad_info))
1388 ? 0 : ad_info.aggregator_id);
1393 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1397 * Show number of active 802.3ad ports.
1399 static ssize_t bonding_show_ad_num_ports(struct device *d,
1400 struct device_attribute *attr,
1404 struct bonding *bond = to_bond(d);
1406 if (bond->params.mode == BOND_MODE_8023AD) {
1407 struct ad_info ad_info;
1408 count = sprintf(buf, "%d\n",
1409 (bond_3ad_get_active_agg_info(bond, &ad_info))
1410 ? 0 : ad_info.ports);
1415 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1419 * Show current 802.3ad actor key.
1421 static ssize_t bonding_show_ad_actor_key(struct device *d,
1422 struct device_attribute *attr,
1426 struct bonding *bond = to_bond(d);
1428 if (bond->params.mode == BOND_MODE_8023AD) {
1429 struct ad_info ad_info;
1430 count = sprintf(buf, "%d\n",
1431 (bond_3ad_get_active_agg_info(bond, &ad_info))
1432 ? 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",
1453 (bond_3ad_get_active_agg_info(bond, &ad_info))
1454 ? 0 : ad_info.partner_key);
1459 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1463 * Show current 802.3ad partner mac.
1465 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1466 struct device_attribute *attr,
1470 struct bonding *bond = to_bond(d);
1472 if (bond->params.mode == BOND_MODE_8023AD) {
1473 struct ad_info ad_info;
1474 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1475 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1480 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1484 static struct attribute *per_bond_attrs[] = {
1485 &dev_attr_slaves.attr,
1486 &dev_attr_mode.attr,
1487 &dev_attr_fail_over_mac.attr,
1488 &dev_attr_arp_validate.attr,
1489 &dev_attr_arp_interval.attr,
1490 &dev_attr_arp_ip_target.attr,
1491 &dev_attr_downdelay.attr,
1492 &dev_attr_updelay.attr,
1493 &dev_attr_lacp_rate.attr,
1494 &dev_attr_ad_select.attr,
1495 &dev_attr_xmit_hash_policy.attr,
1496 &dev_attr_num_grat_arp.attr,
1497 &dev_attr_num_unsol_na.attr,
1498 &dev_attr_miimon.attr,
1499 &dev_attr_primary.attr,
1500 &dev_attr_use_carrier.attr,
1501 &dev_attr_active_slave.attr,
1502 &dev_attr_mii_status.attr,
1503 &dev_attr_ad_aggregator.attr,
1504 &dev_attr_ad_num_ports.attr,
1505 &dev_attr_ad_actor_key.attr,
1506 &dev_attr_ad_partner_key.attr,
1507 &dev_attr_ad_partner_mac.attr,
1511 static struct attribute_group bonding_group = {
1513 .attrs = per_bond_attrs,
1517 * Initialize sysfs. This sets up the bonding_masters file in
1520 int bond_create_sysfs(void)
1524 ret = netdev_class_create_file(&class_attr_bonding_masters);
1526 * Permit multiple loads of the module by ignoring failures to
1527 * create the bonding_masters sysfs file. Bonding devices
1528 * created by second or subsequent loads of the module will
1529 * not be listed in, or controllable by, bonding_masters, but
1530 * will have the usual "bonding" sysfs directory.
1532 * This is done to preserve backwards compatibility for
1533 * initscripts/sysconfig, which load bonding multiple times to
1534 * configure multiple bonding devices.
1536 if (ret == -EEXIST) {
1537 /* Is someone being kinky and naming a device bonding_master? */
1538 if (__dev_get_by_name(&init_net,
1539 class_attr_bonding_masters.attr.name))
1541 "network device named %s already exists in sysfs",
1542 class_attr_bonding_masters.attr.name);
1551 * Remove /sys/class/net/bonding_masters.
1553 void bond_destroy_sysfs(void)
1555 netdev_class_remove_file(&class_attr_bonding_masters);
1559 * Initialize sysfs for each bond. This sets up and registers
1560 * the 'bondctl' directory for each individual bond under /sys/class/net.
1562 int bond_create_sysfs_entry(struct bonding *bond)
1564 struct net_device *dev = bond->dev;
1567 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
1569 printk(KERN_EMERG "eek! didn't create group!\n");
1574 * Remove sysfs entries for each bond.
1576 void bond_destroy_sysfs_entry(struct bonding *bond)
1578 struct net_device *dev = bond->dev;
1580 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);