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/sched.h>
26 #include <linux/sysdev.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
33 #include <linux/sysfs.h>
34 #include <linux/ctype.h>
35 #include <linux/inet.h>
36 #include <linux/rtnetlink.h>
37 #include <linux/etherdevice.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
40 #include <linux/nsproxy.h>
44 #define to_dev(obj) container_of(obj, struct device, kobj)
45 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
48 * "show" function for the bond_masters attribute.
49 * The class parameter is ignored.
51 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
53 struct net *net = current->nsproxy->net_ns;
54 struct bond_net *bn = net_generic(net, bond_net_id);
60 list_for_each_entry(bond, &bn->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 */
77 static struct net_device *bond_get_by_name(struct net *net, const char *ifname)
79 struct bond_net *bn = net_generic(net, bond_net_id);
82 list_for_each_entry(bond, &bn->dev_list, bond_list) {
83 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
90 * "store" function for the bond_masters attribute. This is what
91 * creates and deletes entire bonds.
93 * The class parameter is ignored.
97 static ssize_t bonding_store_bonds(struct class *cls,
98 const char *buffer, size_t count)
100 struct net *net = current->nsproxy->net_ns;
101 char command[IFNAMSIZ + 1] = {0, };
105 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
106 ifname = command + 1;
107 if ((strlen(command) <= 1) ||
108 !dev_valid_name(ifname))
111 if (command[0] == '+') {
113 ": %s is being created...\n", ifname);
114 rv = bond_create(net, ifname);
116 pr_info(DRV_NAME ": Bond creation failed.\n");
119 } else if (command[0] == '-') {
120 struct net_device *bond_dev;
123 bond_dev = bond_get_by_name(net, ifname);
125 pr_info(DRV_NAME ": %s is being deleted...\n",
127 unregister_netdevice(bond_dev);
129 pr_err(DRV_NAME ": unable to delete non-existent %s\n",
137 /* Always return either count or an error. If you return 0, you'll
138 * get called forever, which is bad.
143 pr_err(DRV_NAME ": no command found in bonding_masters."
144 " Use +ifname or -ifname.\n");
148 /* class attribute for bond_masters file. This ends up in /sys/class/net */
149 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
150 bonding_show_bonds, bonding_store_bonds);
152 int bond_create_slave_symlinks(struct net_device *master,
153 struct net_device *slave)
155 char linkname[IFNAMSIZ+7];
158 /* first, create a link from the slave back to the master */
159 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
163 /* next, create a link from the master to the slave */
164 sprintf(linkname, "slave_%s", slave->name);
165 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
171 void bond_destroy_slave_symlinks(struct net_device *master,
172 struct net_device *slave)
174 char linkname[IFNAMSIZ+7];
176 sysfs_remove_link(&(slave->dev.kobj), "master");
177 sprintf(linkname, "slave_%s", slave->name);
178 sysfs_remove_link(&(master->dev.kobj), linkname);
183 * Show the slaves in the current bond.
185 static ssize_t bonding_show_slaves(struct device *d,
186 struct device_attribute *attr, char *buf)
190 struct bonding *bond = to_bond(d);
192 read_lock(&bond->lock);
193 bond_for_each_slave(bond, slave, i) {
194 if (res > (PAGE_SIZE - IFNAMSIZ)) {
195 /* not enough space for another interface name */
196 if ((PAGE_SIZE - res) > 10)
197 res = PAGE_SIZE - 10;
198 res += sprintf(buf + res, "++more++ ");
201 res += sprintf(buf + res, "%s ", slave->dev->name);
203 read_unlock(&bond->lock);
205 buf[res-1] = '\n'; /* eat the leftover space */
210 * Set the slaves in the current bond. The bond interface must be
211 * up for this to succeed.
212 * This function is largely the same flow as bonding_update_bonds().
214 static ssize_t bonding_store_slaves(struct device *d,
215 struct device_attribute *attr,
216 const char *buffer, size_t count)
218 char command[IFNAMSIZ + 1] = { 0, };
220 int i, res, found, ret = count;
223 struct net_device *dev = NULL;
224 struct bonding *bond = to_bond(d);
226 /* Quick sanity check -- is the bond interface up? */
227 if (!(bond->dev->flags & IFF_UP)) {
228 pr_warning(DRV_NAME ": %s: doing slave updates when "
229 "interface is down.\n", bond->dev->name);
232 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
235 return restart_syscall();
237 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
238 ifname = command + 1;
239 if ((strlen(command) <= 1) ||
240 !dev_valid_name(ifname))
243 if (command[0] == '+') {
245 /* Got a slave name in ifname. Is it already in the list? */
248 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
251 ": %s: Interface %s does not exist!\n",
252 bond->dev->name, ifname);
257 if (dev->flags & IFF_UP) {
259 ": %s: Error: Unable to enslave %s "
260 "because it is already up.\n",
261 bond->dev->name, dev->name);
266 read_lock(&bond->lock);
267 bond_for_each_slave(bond, slave, i)
268 if (slave->dev == dev) {
270 ": %s: Interface %s is already enslaved!\n",
271 bond->dev->name, ifname);
273 read_unlock(&bond->lock);
276 read_unlock(&bond->lock);
278 pr_info(DRV_NAME ": %s: Adding slave %s.\n",
279 bond->dev->name, ifname);
281 /* If this is the first slave, then we need to set
282 the master's hardware address to be the same as the
284 if (is_zero_ether_addr(bond->dev->dev_addr))
285 memcpy(bond->dev->dev_addr, dev->dev_addr,
288 /* Set the slave's MTU to match the bond */
289 original_mtu = dev->mtu;
290 res = dev_set_mtu(dev, bond->dev->mtu);
296 res = bond_enslave(bond->dev, dev);
297 bond_for_each_slave(bond, slave, i)
298 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
299 slave->original_mtu = original_mtu;
306 if (command[0] == '-') {
309 bond_for_each_slave(bond, slave, i)
310 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
312 original_mtu = slave->original_mtu;
316 pr_info(DRV_NAME ": %s: Removing slave %s\n",
317 bond->dev->name, dev->name);
318 res = bond_release(bond->dev, dev);
323 /* set the slave MTU to the default */
324 dev_set_mtu(dev, original_mtu);
326 pr_err(DRV_NAME ": unable to remove non-existent"
327 " slave %s for bond %s.\n",
328 ifname, bond->dev->name);
335 pr_err(DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
343 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
344 bonding_store_slaves);
347 * Show and set the bonding mode. The bond interface must be down to
350 static ssize_t bonding_show_mode(struct device *d,
351 struct device_attribute *attr, char *buf)
353 struct bonding *bond = to_bond(d);
355 return sprintf(buf, "%s %d\n",
356 bond_mode_tbl[bond->params.mode].modename,
360 static ssize_t bonding_store_mode(struct device *d,
361 struct device_attribute *attr,
362 const char *buf, size_t count)
364 int new_value, ret = count;
365 struct bonding *bond = to_bond(d);
367 if (bond->dev->flags & IFF_UP) {
368 pr_err(DRV_NAME ": unable to update mode of %s"
369 " because interface is up.\n", bond->dev->name);
374 new_value = bond_parse_parm(buf, bond_mode_tbl);
377 ": %s: Ignoring invalid mode value %.*s.\n",
379 (int)strlen(buf) - 1, buf);
383 if (bond->params.mode == BOND_MODE_8023AD)
384 bond_unset_master_3ad_flags(bond);
386 if (bond->params.mode == BOND_MODE_ALB)
387 bond_unset_master_alb_flags(bond);
389 bond->params.mode = new_value;
390 bond_set_mode_ops(bond, bond->params.mode);
391 pr_info(DRV_NAME ": %s: setting mode to %s (%d).\n",
392 bond->dev->name, bond_mode_tbl[new_value].modename,
398 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
399 bonding_show_mode, bonding_store_mode);
402 * Show and set the bonding transmit hash method.
403 * The bond interface must be down to change the xmit hash policy.
405 static ssize_t bonding_show_xmit_hash(struct device *d,
406 struct device_attribute *attr,
409 struct bonding *bond = to_bond(d);
411 return sprintf(buf, "%s %d\n",
412 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
413 bond->params.xmit_policy);
416 static ssize_t bonding_store_xmit_hash(struct device *d,
417 struct device_attribute *attr,
418 const char *buf, size_t count)
420 int new_value, ret = count;
421 struct bonding *bond = to_bond(d);
423 if (bond->dev->flags & IFF_UP) {
425 "%s: Interface is up. Unable to update xmit policy.\n",
431 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
434 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
436 (int)strlen(buf) - 1, buf);
440 bond->params.xmit_policy = new_value;
441 bond_set_mode_ops(bond, bond->params.mode);
442 pr_info(DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
444 xmit_hashtype_tbl[new_value].modename, new_value);
449 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
450 bonding_show_xmit_hash, bonding_store_xmit_hash);
453 * Show and set arp_validate.
455 static ssize_t bonding_show_arp_validate(struct device *d,
456 struct device_attribute *attr,
459 struct bonding *bond = to_bond(d);
461 return sprintf(buf, "%s %d\n",
462 arp_validate_tbl[bond->params.arp_validate].modename,
463 bond->params.arp_validate);
466 static ssize_t bonding_store_arp_validate(struct device *d,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
471 struct bonding *bond = to_bond(d);
473 new_value = bond_parse_parm(buf, arp_validate_tbl);
476 ": %s: Ignoring invalid arp_validate value %s\n",
477 bond->dev->name, buf);
480 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
482 ": %s: arp_validate only supported in active-backup mode.\n",
486 pr_info(DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
487 bond->dev->name, arp_validate_tbl[new_value].modename,
490 if (!bond->params.arp_validate && new_value)
491 bond_register_arp(bond);
492 else if (bond->params.arp_validate && !new_value)
493 bond_unregister_arp(bond);
495 bond->params.arp_validate = new_value;
500 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
501 bonding_store_arp_validate);
504 * Show and store fail_over_mac. User only allowed to change the
505 * value when there are no slaves.
507 static ssize_t bonding_show_fail_over_mac(struct device *d,
508 struct device_attribute *attr,
511 struct bonding *bond = to_bond(d);
513 return sprintf(buf, "%s %d\n",
514 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
515 bond->params.fail_over_mac);
518 static ssize_t bonding_store_fail_over_mac(struct device *d,
519 struct device_attribute *attr,
520 const char *buf, size_t count)
523 struct bonding *bond = to_bond(d);
525 if (bond->slave_cnt != 0) {
527 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
532 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
535 ": %s: Ignoring invalid fail_over_mac value %s.\n",
536 bond->dev->name, buf);
540 bond->params.fail_over_mac = new_value;
541 pr_info(DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
542 bond->dev->name, fail_over_mac_tbl[new_value].modename,
548 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
549 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
552 * Show and set the arp timer interval. There are two tricky bits
553 * here. First, if ARP monitoring is activated, then we must disable
554 * MII monitoring. Second, if the ARP timer isn't running, we must
557 static ssize_t bonding_show_arp_interval(struct device *d,
558 struct device_attribute *attr,
561 struct bonding *bond = to_bond(d);
563 return sprintf(buf, "%d\n", bond->params.arp_interval);
566 static ssize_t bonding_store_arp_interval(struct device *d,
567 struct device_attribute *attr,
568 const char *buf, size_t count)
570 int new_value, ret = count;
571 struct bonding *bond = to_bond(d);
573 if (sscanf(buf, "%d", &new_value) != 1) {
575 ": %s: no arp_interval value specified.\n",
582 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
583 bond->dev->name, new_value, INT_MAX);
589 ": %s: Setting ARP monitoring interval to %d.\n",
590 bond->dev->name, new_value);
591 bond->params.arp_interval = new_value;
592 if (bond->params.arp_interval)
593 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
594 if (bond->params.miimon) {
596 ": %s: ARP monitoring cannot be used with MII monitoring. "
597 "%s Disabling MII monitoring.\n",
598 bond->dev->name, bond->dev->name);
599 bond->params.miimon = 0;
600 if (delayed_work_pending(&bond->mii_work)) {
601 cancel_delayed_work(&bond->mii_work);
602 flush_workqueue(bond->wq);
605 if (!bond->params.arp_targets[0]) {
607 ": %s: ARP monitoring has been set up, "
608 "but no ARP targets have been specified.\n",
611 if (bond->dev->flags & IFF_UP) {
612 /* If the interface is up, we may need to fire off
613 * the ARP timer. If the interface is down, the
614 * timer will get fired off when the open function
617 if (!delayed_work_pending(&bond->arp_work)) {
618 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
619 INIT_DELAYED_WORK(&bond->arp_work,
620 bond_activebackup_arp_mon);
622 INIT_DELAYED_WORK(&bond->arp_work,
623 bond_loadbalance_arp_mon);
625 queue_delayed_work(bond->wq, &bond->arp_work, 0);
632 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
633 bonding_show_arp_interval, bonding_store_arp_interval);
636 * Show and set the arp targets.
638 static ssize_t bonding_show_arp_targets(struct device *d,
639 struct device_attribute *attr,
643 struct bonding *bond = to_bond(d);
645 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
646 if (bond->params.arp_targets[i])
647 res += sprintf(buf + res, "%pI4 ",
648 &bond->params.arp_targets[i]);
651 buf[res-1] = '\n'; /* eat the leftover space */
655 static ssize_t bonding_store_arp_targets(struct device *d,
656 struct device_attribute *attr,
657 const char *buf, size_t count)
660 int i = 0, done = 0, ret = count;
661 struct bonding *bond = to_bond(d);
664 targets = bond->params.arp_targets;
665 newtarget = in_aton(buf + 1);
668 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
670 ": %s: invalid ARP target %pI4 specified for addition\n",
671 bond->dev->name, &newtarget);
675 /* look for an empty slot to put the target in, and check for dupes */
676 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
677 if (targets[i] == newtarget) { /* duplicate */
679 ": %s: ARP target %pI4 is already present\n",
680 bond->dev->name, &newtarget);
684 if (targets[i] == 0) {
686 ": %s: adding ARP target %pI4.\n",
687 bond->dev->name, &newtarget);
689 targets[i] = newtarget;
694 ": %s: ARP target table is full!\n",
700 } else if (buf[0] == '-') {
701 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
703 ": %s: invalid ARP target %pI4 specified for removal\n",
704 bond->dev->name, &newtarget);
709 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
710 if (targets[i] == newtarget) {
713 ": %s: removing ARP target %pI4.\n",
714 bond->dev->name, &newtarget);
715 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
716 targets[j] = targets[j+1];
724 ": %s: unable to remove nonexistent ARP target %pI4.\n",
725 bond->dev->name, &newtarget);
730 pr_err(DRV_NAME ": no command found in arp_ip_targets file"
731 " for bond %s. Use +<addr> or -<addr>.\n",
740 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
743 * Show and set the up and down delays. These must be multiples of the
744 * MII monitoring value, and are stored internally as the multiplier.
745 * Thus, we must translate to MS for the real world.
747 static ssize_t bonding_show_downdelay(struct device *d,
748 struct device_attribute *attr,
751 struct bonding *bond = to_bond(d);
753 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
756 static ssize_t bonding_store_downdelay(struct device *d,
757 struct device_attribute *attr,
758 const char *buf, size_t count)
760 int new_value, ret = count;
761 struct bonding *bond = to_bond(d);
763 if (!(bond->params.miimon)) {
765 ": %s: Unable to set down delay as MII monitoring is disabled\n",
771 if (sscanf(buf, "%d", &new_value) != 1) {
773 ": %s: no down delay value specified.\n",
780 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
781 bond->dev->name, new_value, 1, INT_MAX);
785 if ((new_value % bond->params.miimon) != 0) {
787 ": %s: Warning: down delay (%d) is not a "
788 "multiple of miimon (%d), delay rounded "
790 bond->dev->name, new_value,
792 (new_value / bond->params.miimon) *
793 bond->params.miimon);
795 bond->params.downdelay = new_value / bond->params.miimon;
796 pr_info(DRV_NAME ": %s: Setting down delay to %d.\n",
798 bond->params.downdelay * bond->params.miimon);
805 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
806 bonding_show_downdelay, bonding_store_downdelay);
808 static ssize_t bonding_show_updelay(struct device *d,
809 struct device_attribute *attr,
812 struct bonding *bond = to_bond(d);
814 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
818 static ssize_t bonding_store_updelay(struct device *d,
819 struct device_attribute *attr,
820 const char *buf, size_t count)
822 int new_value, ret = count;
823 struct bonding *bond = to_bond(d);
825 if (!(bond->params.miimon)) {
827 ": %s: Unable to set up delay as MII monitoring is disabled\n",
833 if (sscanf(buf, "%d", &new_value) != 1) {
835 ": %s: no up delay value specified.\n",
842 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
843 bond->dev->name, new_value, 1, INT_MAX);
847 if ((new_value % bond->params.miimon) != 0) {
849 ": %s: Warning: up delay (%d) is not a "
850 "multiple of miimon (%d), updelay rounded "
852 bond->dev->name, new_value,
854 (new_value / bond->params.miimon) *
855 bond->params.miimon);
857 bond->params.updelay = new_value / bond->params.miimon;
858 pr_info(DRV_NAME ": %s: Setting up delay to %d.\n",
859 bond->dev->name, bond->params.updelay * bond->params.miimon);
866 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
867 bonding_show_updelay, bonding_store_updelay);
870 * Show and set the LACP interval. Interface must be down, and the mode
871 * must be set to 802.3ad mode.
873 static ssize_t bonding_show_lacp(struct device *d,
874 struct device_attribute *attr,
877 struct bonding *bond = to_bond(d);
879 return sprintf(buf, "%s %d\n",
880 bond_lacp_tbl[bond->params.lacp_fast].modename,
881 bond->params.lacp_fast);
884 static ssize_t bonding_store_lacp(struct device *d,
885 struct device_attribute *attr,
886 const char *buf, size_t count)
888 int new_value, ret = count;
889 struct bonding *bond = to_bond(d);
891 if (bond->dev->flags & IFF_UP) {
893 ": %s: Unable to update LACP rate because interface is up.\n",
899 if (bond->params.mode != BOND_MODE_8023AD) {
901 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
907 new_value = bond_parse_parm(buf, bond_lacp_tbl);
909 if ((new_value == 1) || (new_value == 0)) {
910 bond->params.lacp_fast = new_value;
911 pr_info(DRV_NAME ": %s: Setting LACP rate to %s (%d).\n",
912 bond->dev->name, bond_lacp_tbl[new_value].modename,
916 ": %s: Ignoring invalid LACP rate value %.*s.\n",
917 bond->dev->name, (int)strlen(buf) - 1, buf);
923 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
924 bonding_show_lacp, bonding_store_lacp);
926 static ssize_t bonding_show_ad_select(struct device *d,
927 struct device_attribute *attr,
930 struct bonding *bond = to_bond(d);
932 return sprintf(buf, "%s %d\n",
933 ad_select_tbl[bond->params.ad_select].modename,
934 bond->params.ad_select);
938 static ssize_t bonding_store_ad_select(struct device *d,
939 struct device_attribute *attr,
940 const char *buf, size_t count)
942 int new_value, ret = count;
943 struct bonding *bond = to_bond(d);
945 if (bond->dev->flags & IFF_UP) {
947 ": %s: Unable to update ad_select because interface "
948 "is up.\n", bond->dev->name);
953 new_value = bond_parse_parm(buf, ad_select_tbl);
955 if (new_value != -1) {
956 bond->params.ad_select = new_value;
958 ": %s: Setting ad_select to %s (%d).\n",
959 bond->dev->name, ad_select_tbl[new_value].modename,
963 ": %s: Ignoring invalid ad_select value %.*s.\n",
964 bond->dev->name, (int)strlen(buf) - 1, buf);
970 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
971 bonding_show_ad_select, bonding_store_ad_select);
974 * Show and set the number of grat ARP to send after a failover event.
976 static ssize_t bonding_show_n_grat_arp(struct device *d,
977 struct device_attribute *attr,
980 struct bonding *bond = to_bond(d);
982 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
985 static ssize_t bonding_store_n_grat_arp(struct device *d,
986 struct device_attribute *attr,
987 const char *buf, size_t count)
989 int new_value, ret = count;
990 struct bonding *bond = to_bond(d);
992 if (sscanf(buf, "%d", &new_value) != 1) {
994 ": %s: no num_grat_arp value specified.\n",
999 if (new_value < 0 || new_value > 255) {
1001 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
1002 bond->dev->name, new_value);
1006 bond->params.num_grat_arp = new_value;
1011 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
1012 bonding_show_n_grat_arp, bonding_store_n_grat_arp);
1015 * Show and set the number of unsolicited NA's to send after a failover event.
1017 static ssize_t bonding_show_n_unsol_na(struct device *d,
1018 struct device_attribute *attr,
1021 struct bonding *bond = to_bond(d);
1023 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1026 static ssize_t bonding_store_n_unsol_na(struct device *d,
1027 struct device_attribute *attr,
1028 const char *buf, size_t count)
1030 int new_value, ret = count;
1031 struct bonding *bond = to_bond(d);
1033 if (sscanf(buf, "%d", &new_value) != 1) {
1035 ": %s: no num_unsol_na value specified.\n",
1041 if (new_value < 0 || new_value > 255) {
1043 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1044 bond->dev->name, new_value);
1048 bond->params.num_unsol_na = new_value;
1052 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
1053 bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1056 * Show and set the MII monitor interval. There are two tricky bits
1057 * here. First, if MII monitoring is activated, then we must disable
1058 * ARP monitoring. Second, if the timer isn't running, we must
1061 static ssize_t bonding_show_miimon(struct device *d,
1062 struct device_attribute *attr,
1065 struct bonding *bond = to_bond(d);
1067 return sprintf(buf, "%d\n", bond->params.miimon);
1070 static ssize_t bonding_store_miimon(struct device *d,
1071 struct device_attribute *attr,
1072 const char *buf, size_t count)
1074 int new_value, ret = count;
1075 struct bonding *bond = to_bond(d);
1077 if (sscanf(buf, "%d", &new_value) != 1) {
1079 ": %s: no miimon value specified.\n",
1084 if (new_value < 0) {
1086 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1087 bond->dev->name, new_value, 1, INT_MAX);
1092 ": %s: Setting MII monitoring interval to %d.\n",
1093 bond->dev->name, new_value);
1094 bond->params.miimon = new_value;
1095 if (bond->params.updelay)
1097 ": %s: Note: Updating updelay (to %d) "
1098 "since it is a multiple of the miimon value.\n",
1100 bond->params.updelay * bond->params.miimon);
1101 if (bond->params.downdelay)
1103 ": %s: Note: Updating downdelay (to %d) "
1104 "since it is a multiple of the miimon value.\n",
1106 bond->params.downdelay * bond->params.miimon);
1107 if (bond->params.arp_interval) {
1109 ": %s: MII monitoring cannot be used with "
1110 "ARP monitoring. Disabling ARP monitoring...\n",
1112 bond->params.arp_interval = 0;
1113 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1114 if (bond->params.arp_validate) {
1115 bond_unregister_arp(bond);
1116 bond->params.arp_validate =
1117 BOND_ARP_VALIDATE_NONE;
1119 if (delayed_work_pending(&bond->arp_work)) {
1120 cancel_delayed_work(&bond->arp_work);
1121 flush_workqueue(bond->wq);
1125 if (bond->dev->flags & IFF_UP) {
1126 /* If the interface is up, we may need to fire off
1127 * the MII timer. If the interface is down, the
1128 * timer will get fired off when the open function
1131 if (!delayed_work_pending(&bond->mii_work)) {
1132 INIT_DELAYED_WORK(&bond->mii_work,
1134 queue_delayed_work(bond->wq,
1135 &bond->mii_work, 0);
1142 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1143 bonding_show_miimon, bonding_store_miimon);
1146 * Show and set the primary slave. The store function is much
1147 * simpler than bonding_store_slaves function because it only needs to
1148 * handle one interface name.
1149 * The bond must be a mode that supports a primary for this be
1152 static ssize_t bonding_show_primary(struct device *d,
1153 struct device_attribute *attr,
1157 struct bonding *bond = to_bond(d);
1159 if (bond->primary_slave)
1160 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1165 static ssize_t bonding_store_primary(struct device *d,
1166 struct device_attribute *attr,
1167 const char *buf, size_t count)
1170 struct slave *slave;
1171 struct bonding *bond = to_bond(d);
1173 if (!rtnl_trylock())
1174 return restart_syscall();
1175 read_lock(&bond->lock);
1176 write_lock_bh(&bond->curr_slave_lock);
1178 if (!USES_PRIMARY(bond->params.mode)) {
1180 ": %s: Unable to set primary slave; %s is in mode %d\n",
1181 bond->dev->name, bond->dev->name, bond->params.mode);
1183 bond_for_each_slave(bond, slave, i) {
1185 (slave->dev->name, buf,
1186 strlen(slave->dev->name)) == 0) {
1188 ": %s: Setting %s as primary slave.\n",
1189 bond->dev->name, slave->dev->name);
1190 bond->primary_slave = slave;
1191 strcpy(bond->params.primary, slave->dev->name);
1192 bond_select_active_slave(bond);
1197 /* if we got here, then we didn't match the name of any slave */
1199 if (strlen(buf) == 0 || buf[0] == '\n') {
1201 ": %s: Setting primary slave to None.\n",
1203 bond->primary_slave = NULL;
1204 bond_select_active_slave(bond);
1207 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1208 bond->dev->name, (int)strlen(buf) - 1, buf);
1212 write_unlock_bh(&bond->curr_slave_lock);
1213 read_unlock(&bond->lock);
1218 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1219 bonding_show_primary, bonding_store_primary);
1222 * Show and set the primary_reselect flag.
1224 static ssize_t bonding_show_primary_reselect(struct device *d,
1225 struct device_attribute *attr,
1228 struct bonding *bond = to_bond(d);
1230 return sprintf(buf, "%s %d\n",
1231 pri_reselect_tbl[bond->params.primary_reselect].modename,
1232 bond->params.primary_reselect);
1235 static ssize_t bonding_store_primary_reselect(struct device *d,
1236 struct device_attribute *attr,
1237 const char *buf, size_t count)
1239 int new_value, ret = count;
1240 struct bonding *bond = to_bond(d);
1242 if (!rtnl_trylock())
1243 return restart_syscall();
1245 new_value = bond_parse_parm(buf, pri_reselect_tbl);
1246 if (new_value < 0) {
1248 ": %s: Ignoring invalid primary_reselect value %.*s.\n",
1250 (int) strlen(buf) - 1, buf);
1255 bond->params.primary_reselect = new_value;
1256 pr_info(DRV_NAME ": %s: setting primary_reselect to %s (%d).\n",
1257 bond->dev->name, pri_reselect_tbl[new_value].modename,
1260 read_lock(&bond->lock);
1261 write_lock_bh(&bond->curr_slave_lock);
1262 bond_select_active_slave(bond);
1263 write_unlock_bh(&bond->curr_slave_lock);
1264 read_unlock(&bond->lock);
1269 static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1270 bonding_show_primary_reselect,
1271 bonding_store_primary_reselect);
1274 * Show and set the use_carrier flag.
1276 static ssize_t bonding_show_carrier(struct device *d,
1277 struct device_attribute *attr,
1280 struct bonding *bond = to_bond(d);
1282 return sprintf(buf, "%d\n", bond->params.use_carrier);
1285 static ssize_t bonding_store_carrier(struct device *d,
1286 struct device_attribute *attr,
1287 const char *buf, size_t count)
1289 int new_value, ret = count;
1290 struct bonding *bond = to_bond(d);
1293 if (sscanf(buf, "%d", &new_value) != 1) {
1295 ": %s: no use_carrier value specified.\n",
1300 if ((new_value == 0) || (new_value == 1)) {
1301 bond->params.use_carrier = new_value;
1302 pr_info(DRV_NAME ": %s: Setting use_carrier to %d.\n",
1303 bond->dev->name, new_value);
1306 ": %s: Ignoring invalid use_carrier value %d.\n",
1307 bond->dev->name, new_value);
1312 static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1313 bonding_show_carrier, bonding_store_carrier);
1317 * Show and set currently active_slave.
1319 static ssize_t bonding_show_active_slave(struct device *d,
1320 struct device_attribute *attr,
1324 struct bonding *bond = to_bond(d);
1327 read_lock(&bond->curr_slave_lock);
1328 curr = bond->curr_active_slave;
1329 read_unlock(&bond->curr_slave_lock);
1331 if (USES_PRIMARY(bond->params.mode) && curr)
1332 count = sprintf(buf, "%s\n", curr->dev->name);
1336 static ssize_t bonding_store_active_slave(struct device *d,
1337 struct device_attribute *attr,
1338 const char *buf, size_t count)
1341 struct slave *slave;
1342 struct slave *old_active = NULL;
1343 struct slave *new_active = NULL;
1344 struct bonding *bond = to_bond(d);
1346 if (!rtnl_trylock())
1347 return restart_syscall();
1348 read_lock(&bond->lock);
1349 write_lock_bh(&bond->curr_slave_lock);
1351 if (!USES_PRIMARY(bond->params.mode))
1352 pr_info(DRV_NAME ": %s: Unable to change active slave;"
1353 " %s is in mode %d\n",
1354 bond->dev->name, bond->dev->name, bond->params.mode);
1356 bond_for_each_slave(bond, slave, i) {
1358 (slave->dev->name, buf,
1359 strlen(slave->dev->name)) == 0) {
1360 old_active = bond->curr_active_slave;
1362 if (new_active == old_active) {
1365 ": %s: %s is already the current active slave.\n",
1366 bond->dev->name, slave->dev->name);
1372 (new_active->link == BOND_LINK_UP) &&
1373 IS_UP(new_active->dev)) {
1375 ": %s: Setting %s as active slave.\n",
1376 bond->dev->name, slave->dev->name);
1377 bond_change_active_slave(bond, new_active);
1381 ": %s: Could not set %s as active slave; "
1382 "either %s is down or the link is down.\n",
1383 bond->dev->name, slave->dev->name,
1391 /* if we got here, then we didn't match the name of any slave */
1393 if (strlen(buf) == 0 || buf[0] == '\n') {
1395 ": %s: Setting active slave to None.\n",
1397 bond->primary_slave = NULL;
1398 bond_select_active_slave(bond);
1400 pr_info(DRV_NAME ": %s: Unable to set %.*s"
1401 " as active slave as it is not a slave.\n",
1402 bond->dev->name, (int)strlen(buf) - 1, buf);
1406 write_unlock_bh(&bond->curr_slave_lock);
1407 read_unlock(&bond->lock);
1413 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1414 bonding_show_active_slave, bonding_store_active_slave);
1418 * Show link status of the bond interface.
1420 static ssize_t bonding_show_mii_status(struct device *d,
1421 struct device_attribute *attr,
1425 struct bonding *bond = to_bond(d);
1427 read_lock(&bond->curr_slave_lock);
1428 curr = bond->curr_active_slave;
1429 read_unlock(&bond->curr_slave_lock);
1431 return sprintf(buf, "%s\n", curr ? "up" : "down");
1433 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1437 * Show current 802.3ad aggregator ID.
1439 static ssize_t bonding_show_ad_aggregator(struct device *d,
1440 struct device_attribute *attr,
1444 struct bonding *bond = to_bond(d);
1446 if (bond->params.mode == BOND_MODE_8023AD) {
1447 struct ad_info ad_info;
1448 count = sprintf(buf, "%d\n",
1449 (bond_3ad_get_active_agg_info(bond, &ad_info))
1450 ? 0 : ad_info.aggregator_id);
1455 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1459 * Show number of active 802.3ad ports.
1461 static ssize_t bonding_show_ad_num_ports(struct device *d,
1462 struct device_attribute *attr,
1466 struct bonding *bond = to_bond(d);
1468 if (bond->params.mode == BOND_MODE_8023AD) {
1469 struct ad_info ad_info;
1470 count = sprintf(buf, "%d\n",
1471 (bond_3ad_get_active_agg_info(bond, &ad_info))
1472 ? 0 : ad_info.ports);
1477 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1481 * Show current 802.3ad actor key.
1483 static ssize_t bonding_show_ad_actor_key(struct device *d,
1484 struct device_attribute *attr,
1488 struct bonding *bond = to_bond(d);
1490 if (bond->params.mode == BOND_MODE_8023AD) {
1491 struct ad_info ad_info;
1492 count = sprintf(buf, "%d\n",
1493 (bond_3ad_get_active_agg_info(bond, &ad_info))
1494 ? 0 : ad_info.actor_key);
1499 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1503 * Show current 802.3ad partner key.
1505 static ssize_t bonding_show_ad_partner_key(struct device *d,
1506 struct device_attribute *attr,
1510 struct bonding *bond = to_bond(d);
1512 if (bond->params.mode == BOND_MODE_8023AD) {
1513 struct ad_info ad_info;
1514 count = sprintf(buf, "%d\n",
1515 (bond_3ad_get_active_agg_info(bond, &ad_info))
1516 ? 0 : ad_info.partner_key);
1521 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1525 * Show current 802.3ad partner mac.
1527 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1528 struct device_attribute *attr,
1532 struct bonding *bond = to_bond(d);
1534 if (bond->params.mode == BOND_MODE_8023AD) {
1535 struct ad_info ad_info;
1536 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1537 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1542 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1546 static struct attribute *per_bond_attrs[] = {
1547 &dev_attr_slaves.attr,
1548 &dev_attr_mode.attr,
1549 &dev_attr_fail_over_mac.attr,
1550 &dev_attr_arp_validate.attr,
1551 &dev_attr_arp_interval.attr,
1552 &dev_attr_arp_ip_target.attr,
1553 &dev_attr_downdelay.attr,
1554 &dev_attr_updelay.attr,
1555 &dev_attr_lacp_rate.attr,
1556 &dev_attr_ad_select.attr,
1557 &dev_attr_xmit_hash_policy.attr,
1558 &dev_attr_num_grat_arp.attr,
1559 &dev_attr_num_unsol_na.attr,
1560 &dev_attr_miimon.attr,
1561 &dev_attr_primary.attr,
1562 &dev_attr_primary_reselect.attr,
1563 &dev_attr_use_carrier.attr,
1564 &dev_attr_active_slave.attr,
1565 &dev_attr_mii_status.attr,
1566 &dev_attr_ad_aggregator.attr,
1567 &dev_attr_ad_num_ports.attr,
1568 &dev_attr_ad_actor_key.attr,
1569 &dev_attr_ad_partner_key.attr,
1570 &dev_attr_ad_partner_mac.attr,
1574 static struct attribute_group bonding_group = {
1576 .attrs = per_bond_attrs,
1580 * Initialize sysfs. This sets up the bonding_masters file in
1583 int bond_create_sysfs(void)
1587 ret = netdev_class_create_file(&class_attr_bonding_masters);
1589 * Permit multiple loads of the module by ignoring failures to
1590 * create the bonding_masters sysfs file. Bonding devices
1591 * created by second or subsequent loads of the module will
1592 * not be listed in, or controllable by, bonding_masters, but
1593 * will have the usual "bonding" sysfs directory.
1595 * This is done to preserve backwards compatibility for
1596 * initscripts/sysconfig, which load bonding multiple times to
1597 * configure multiple bonding devices.
1599 if (ret == -EEXIST) {
1600 /* Is someone being kinky and naming a device bonding_master? */
1601 if (__dev_get_by_name(&init_net,
1602 class_attr_bonding_masters.attr.name))
1603 pr_err("network device named %s already "
1605 class_attr_bonding_masters.attr.name);
1614 * Remove /sys/class/net/bonding_masters.
1616 void bond_destroy_sysfs(void)
1618 netdev_class_remove_file(&class_attr_bonding_masters);
1622 * Initialize sysfs for each bond. This sets up and registers
1623 * the 'bondctl' directory for each individual bond under /sys/class/net.
1625 void bond_prepare_sysfs_group(struct bonding *bond)
1627 bond->dev->sysfs_groups[0] = &bonding_group;