3 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/sysdev.h>
28 #include <linux/types.h>
29 #include <linux/string.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
33 #include <linux/sysfs.h>
34 #include <linux/ctype.h>
35 #include <linux/inet.h>
36 #include <linux/rtnetlink.h>
37 #include <net/net_namespace.h>
41 #define to_dev(obj) container_of(obj,struct device,kobj)
42 #define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
44 /*---------------------------- Declarations -------------------------------*/
46 static int expected_refcount = -1;
48 /*------------------------------ Functions --------------------------------*/
51 * "show" function for the bond_masters attribute.
52 * The class parameter is ignored.
54 static ssize_t bonding_show_bonds(struct class *cls, char *buf)
61 list_for_each_entry(bond, &bond_dev_list, bond_list) {
62 if (res > (PAGE_SIZE - IFNAMSIZ)) {
63 /* not enough space for another interface name */
64 if ((PAGE_SIZE - res) > 10)
66 res += sprintf(buf + res, "++more++ ");
69 res += sprintf(buf + res, "%s ", bond->dev->name);
72 buf[res-1] = '\n'; /* eat the leftover space */
79 * "store" function for the bond_masters attribute. This is what
80 * creates and deletes entire bonds.
82 * The class parameter is ignored.
86 static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
88 char command[IFNAMSIZ + 1] = {0, };
93 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
95 if ((strlen(command) <= 1) ||
96 !dev_valid_name(ifname))
99 if (command[0] == '+') {
100 printk(KERN_INFO DRV_NAME
101 ": %s is being created...\n", ifname);
102 rv = bond_create(ifname);
104 printk(KERN_INFO DRV_NAME ": Bond creation failed.\n");
110 if (command[0] == '-') {
113 list_for_each_entry(bond, &bond_dev_list, bond_list)
114 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
115 /* check the ref count on the bond's kobject.
116 * If it's > expected, then there's a file open,
117 * and we have to fail.
119 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
120 > expected_refcount){
121 printk(KERN_INFO DRV_NAME
122 ": Unable remove bond %s due to open references.\n",
127 printk(KERN_INFO DRV_NAME
128 ": %s is being deleted...\n",
134 printk(KERN_ERR DRV_NAME
135 ": unable to delete non-existent bond %s\n", ifname);
141 printk(KERN_ERR DRV_NAME
142 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
148 /* Always return either count or an error. If you return 0, you'll
149 * get called forever, which is bad.
154 /* class attribute for bond_masters file. This ends up in /sys/class/net */
155 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
156 bonding_show_bonds, bonding_store_bonds);
158 int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
160 char linkname[IFNAMSIZ+7];
163 /* first, create a link from the slave back to the master */
164 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
168 /* next, create a link from the master to the slave */
169 sprintf(linkname,"slave_%s",slave->name);
170 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
176 void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
178 char linkname[IFNAMSIZ+7];
180 sysfs_remove_link(&(slave->dev.kobj), "master");
181 sprintf(linkname,"slave_%s",slave->name);
182 sysfs_remove_link(&(master->dev.kobj), linkname);
187 * Show the slaves in the current bond.
189 static ssize_t bonding_show_slaves(struct device *d,
190 struct device_attribute *attr, char *buf)
194 struct bonding *bond = to_bond(d);
196 read_lock(&bond->lock);
197 bond_for_each_slave(bond, slave, i) {
198 if (res > (PAGE_SIZE - IFNAMSIZ)) {
199 /* not enough space for another interface name */
200 if ((PAGE_SIZE - res) > 10)
201 res = PAGE_SIZE - 10;
202 res += sprintf(buf + res, "++more++ ");
205 res += sprintf(buf + res, "%s ", slave->dev->name);
207 read_unlock(&bond->lock);
209 buf[res-1] = '\n'; /* eat the leftover space */
214 * Set the slaves in the current bond. The bond interface must be
215 * up for this to succeed.
216 * This function is largely the same flow as bonding_update_bonds().
218 static ssize_t bonding_store_slaves(struct device *d,
219 struct device_attribute *attr,
220 const char *buffer, size_t count)
222 char command[IFNAMSIZ + 1] = { 0, };
224 int i, res, found, ret = count;
227 struct net_device *dev = NULL;
228 struct bonding *bond = to_bond(d);
230 /* Quick sanity check -- is the bond interface up? */
231 if (!(bond->dev->flags & IFF_UP)) {
232 printk(KERN_WARNING DRV_NAME
233 ": %s: doing slave updates when interface is down.\n",
237 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
240 return restart_syscall();
242 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
243 ifname = command + 1;
244 if ((strlen(command) <= 1) ||
245 !dev_valid_name(ifname))
248 if (command[0] == '+') {
250 /* Got a slave name in ifname. Is it already in the list? */
252 read_lock(&bond->lock);
253 bond_for_each_slave(bond, slave, i)
254 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
255 printk(KERN_ERR DRV_NAME
256 ": %s: Interface %s is already enslaved!\n",
257 bond->dev->name, ifname);
259 read_unlock(&bond->lock);
263 read_unlock(&bond->lock);
264 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
265 bond->dev->name, ifname);
266 dev = dev_get_by_name(&init_net, ifname);
268 printk(KERN_INFO DRV_NAME
269 ": %s: Interface %s does not exist!\n",
270 bond->dev->name, ifname);
277 if (dev->flags & IFF_UP) {
278 printk(KERN_ERR DRV_NAME
279 ": %s: Error: Unable to enslave %s "
280 "because it is already up.\n",
281 bond->dev->name, dev->name);
285 /* If this is the first slave, then we need to set
286 the master's hardware address to be the same as the
288 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
289 memcpy(bond->dev->dev_addr, dev->dev_addr,
293 /* Set the slave's MTU to match the bond */
294 original_mtu = dev->mtu;
295 res = dev_set_mtu(dev, bond->dev->mtu);
301 res = bond_enslave(bond->dev, dev);
302 bond_for_each_slave(bond, slave, i)
303 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
304 slave->original_mtu = original_mtu;
311 if (command[0] == '-') {
314 bond_for_each_slave(bond, slave, i)
315 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
317 original_mtu = slave->original_mtu;
321 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
322 bond->dev->name, dev->name);
323 res = bond_release(bond->dev, dev);
328 /* set the slave MTU to the default */
329 dev_set_mtu(dev, original_mtu);
332 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
333 ifname, bond->dev->name);
340 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
348 static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
351 * Show and set the bonding mode. The bond interface must be down to
354 static ssize_t bonding_show_mode(struct device *d,
355 struct device_attribute *attr, char *buf)
357 struct bonding *bond = to_bond(d);
359 return sprintf(buf, "%s %d\n",
360 bond_mode_tbl[bond->params.mode].modename,
364 static ssize_t bonding_store_mode(struct device *d,
365 struct device_attribute *attr,
366 const char *buf, size_t count)
368 int new_value, ret = count;
369 struct bonding *bond = to_bond(d);
371 if (bond->dev->flags & IFF_UP) {
372 printk(KERN_ERR DRV_NAME
373 ": unable to update mode of %s because interface is up.\n",
379 new_value = bond_parse_parm(buf, bond_mode_tbl);
381 printk(KERN_ERR DRV_NAME
382 ": %s: Ignoring invalid mode value %.*s.\n",
384 (int)strlen(buf) - 1, buf);
388 if (bond->params.mode == BOND_MODE_8023AD)
389 bond_unset_master_3ad_flags(bond);
391 if (bond->params.mode == BOND_MODE_ALB)
392 bond_unset_master_alb_flags(bond);
394 bond->params.mode = new_value;
395 bond_set_mode_ops(bond, bond->params.mode);
396 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
397 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
402 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
405 * Show and set the bonding transmit hash method. The bond interface must be down to
406 * change the xmit hash policy.
408 static ssize_t bonding_show_xmit_hash(struct device *d,
409 struct device_attribute *attr,
412 struct bonding *bond = to_bond(d);
414 return sprintf(buf, "%s %d\n",
415 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
416 bond->params.xmit_policy);
419 static ssize_t bonding_store_xmit_hash(struct device *d,
420 struct device_attribute *attr,
421 const char *buf, size_t count)
423 int new_value, ret = count;
424 struct bonding *bond = to_bond(d);
426 if (bond->dev->flags & IFF_UP) {
427 printk(KERN_ERR DRV_NAME
428 "%s: Interface is up. Unable to update xmit policy.\n",
434 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
436 printk(KERN_ERR DRV_NAME
437 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
439 (int)strlen(buf) - 1, buf);
443 bond->params.xmit_policy = new_value;
444 bond_set_mode_ops(bond, bond->params.mode);
445 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
446 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
451 static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
454 * Show and set arp_validate.
456 static ssize_t bonding_show_arp_validate(struct device *d,
457 struct device_attribute *attr,
460 struct bonding *bond = to_bond(d);
462 return sprintf(buf, "%s %d\n",
463 arp_validate_tbl[bond->params.arp_validate].modename,
464 bond->params.arp_validate);
467 static ssize_t bonding_store_arp_validate(struct device *d,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
472 struct bonding *bond = to_bond(d);
474 new_value = bond_parse_parm(buf, arp_validate_tbl);
476 printk(KERN_ERR DRV_NAME
477 ": %s: Ignoring invalid arp_validate value %s\n",
478 bond->dev->name, buf);
481 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
482 printk(KERN_ERR DRV_NAME
483 ": %s: arp_validate only supported in active-backup mode.\n",
487 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
488 bond->dev->name, arp_validate_tbl[new_value].modename,
491 if (!bond->params.arp_validate && new_value) {
492 bond_register_arp(bond);
493 } else if (bond->params.arp_validate && !new_value) {
494 bond_unregister_arp(bond);
497 bond->params.arp_validate = new_value;
502 static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
505 * Show and store fail_over_mac. User only allowed to change the
506 * value when there are no slaves.
508 static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
510 struct bonding *bond = to_bond(d);
512 return sprintf(buf, "%s %d\n",
513 fail_over_mac_tbl[bond->params.fail_over_mac].modename,
514 bond->params.fail_over_mac);
517 static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
520 struct bonding *bond = to_bond(d);
522 if (bond->slave_cnt != 0) {
523 printk(KERN_ERR DRV_NAME
524 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
529 new_value = bond_parse_parm(buf, fail_over_mac_tbl);
531 printk(KERN_ERR DRV_NAME
532 ": %s: Ignoring invalid fail_over_mac value %s.\n",
533 bond->dev->name, buf);
537 bond->params.fail_over_mac = new_value;
538 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %s (%d).\n",
539 bond->dev->name, fail_over_mac_tbl[new_value].modename,
545 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
548 * Show and set the arp timer interval. There are two tricky bits
549 * here. First, if ARP monitoring is activated, then we must disable
550 * MII monitoring. Second, if the ARP timer isn't running, we must
553 static ssize_t bonding_show_arp_interval(struct device *d,
554 struct device_attribute *attr,
557 struct bonding *bond = to_bond(d);
559 return sprintf(buf, "%d\n", bond->params.arp_interval);
562 static ssize_t bonding_store_arp_interval(struct device *d,
563 struct device_attribute *attr,
564 const char *buf, size_t count)
566 int new_value, ret = count;
567 struct bonding *bond = to_bond(d);
569 if (sscanf(buf, "%d", &new_value) != 1) {
570 printk(KERN_ERR DRV_NAME
571 ": %s: no arp_interval value specified.\n",
577 printk(KERN_ERR DRV_NAME
578 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
579 bond->dev->name, new_value, INT_MAX);
584 printk(KERN_INFO DRV_NAME
585 ": %s: Setting ARP monitoring interval to %d.\n",
586 bond->dev->name, new_value);
587 bond->params.arp_interval = new_value;
588 if (bond->params.arp_interval)
589 bond->dev->priv_flags |= IFF_MASTER_ARPMON;
590 if (bond->params.miimon) {
591 printk(KERN_INFO DRV_NAME
592 ": %s: ARP monitoring cannot be used with MII monitoring. "
593 "%s Disabling MII monitoring.\n",
594 bond->dev->name, bond->dev->name);
595 bond->params.miimon = 0;
596 if (delayed_work_pending(&bond->mii_work)) {
597 cancel_delayed_work(&bond->mii_work);
598 flush_workqueue(bond->wq);
601 if (!bond->params.arp_targets[0]) {
602 printk(KERN_INFO DRV_NAME
603 ": %s: ARP monitoring has been set up, "
604 "but no ARP targets have been specified.\n",
607 if (bond->dev->flags & IFF_UP) {
608 /* If the interface is up, we may need to fire off
609 * the ARP timer. If the interface is down, the
610 * timer will get fired off when the open function
613 if (!delayed_work_pending(&bond->arp_work)) {
614 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
615 INIT_DELAYED_WORK(&bond->arp_work,
616 bond_activebackup_arp_mon);
618 INIT_DELAYED_WORK(&bond->arp_work,
619 bond_loadbalance_arp_mon);
621 queue_delayed_work(bond->wq, &bond->arp_work, 0);
628 static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , 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))) {
664 printk(KERN_ERR DRV_NAME
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 */
673 printk(KERN_ERR DRV_NAME
674 ": %s: ARP target %pI4 is already present\n",
675 bond->dev->name, &newtarget);
679 if (targets[i] == 0) {
680 printk(KERN_INFO DRV_NAME
681 ": %s: adding ARP target %pI4.\n",
682 bond->dev->name, &newtarget);
684 targets[i] = newtarget;
688 printk(KERN_ERR DRV_NAME
689 ": %s: ARP target table is full!\n",
696 else if (buf[0] == '-') {
697 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
698 printk(KERN_ERR DRV_NAME
699 ": %s: invalid ARP target %pI4 specified for removal\n",
700 bond->dev->name, &newtarget);
705 for (i = 0; (i < BOND_MAX_ARP_TARGETS) && !done; i++) {
706 if (targets[i] == newtarget) {
708 printk(KERN_INFO DRV_NAME
709 ": %s: removing ARP target %pI4.\n",
710 bond->dev->name, &newtarget);
711 for (j = i; (j < (BOND_MAX_ARP_TARGETS-1)) && targets[j+1]; j++)
712 targets[j] = targets[j+1];
719 printk(KERN_INFO DRV_NAME
720 ": %s: unable to remove nonexistent ARP target %pI4.\n",
721 bond->dev->name, &newtarget);
727 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
736 static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
739 * Show and set the up and down delays. These must be multiples of the
740 * MII monitoring value, and are stored internally as the multiplier.
741 * Thus, we must translate to MS for the real world.
743 static ssize_t bonding_show_downdelay(struct device *d,
744 struct device_attribute *attr,
747 struct bonding *bond = to_bond(d);
749 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
752 static ssize_t bonding_store_downdelay(struct device *d,
753 struct device_attribute *attr,
754 const char *buf, size_t count)
756 int new_value, ret = count;
757 struct bonding *bond = to_bond(d);
759 if (!(bond->params.miimon)) {
760 printk(KERN_ERR DRV_NAME
761 ": %s: Unable to set down delay as MII monitoring is disabled\n",
767 if (sscanf(buf, "%d", &new_value) != 1) {
768 printk(KERN_ERR DRV_NAME
769 ": %s: no down delay value specified.\n",
775 printk(KERN_ERR DRV_NAME
776 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
777 bond->dev->name, new_value, 1, INT_MAX);
781 if ((new_value % bond->params.miimon) != 0) {
782 printk(KERN_WARNING DRV_NAME
783 ": %s: Warning: down delay (%d) is not a multiple "
784 "of miimon (%d), delay rounded to %d ms\n",
785 bond->dev->name, new_value, bond->params.miimon,
786 (new_value / bond->params.miimon) *
787 bond->params.miimon);
789 bond->params.downdelay = new_value / bond->params.miimon;
790 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
791 bond->dev->name, bond->params.downdelay * bond->params.miimon);
798 static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
800 static ssize_t bonding_show_updelay(struct device *d,
801 struct device_attribute *attr,
804 struct bonding *bond = to_bond(d);
806 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
810 static ssize_t bonding_store_updelay(struct device *d,
811 struct device_attribute *attr,
812 const char *buf, size_t count)
814 int new_value, ret = count;
815 struct bonding *bond = to_bond(d);
817 if (!(bond->params.miimon)) {
818 printk(KERN_ERR DRV_NAME
819 ": %s: Unable to set up delay as MII monitoring is disabled\n",
825 if (sscanf(buf, "%d", &new_value) != 1) {
826 printk(KERN_ERR DRV_NAME
827 ": %s: no up delay value specified.\n",
833 printk(KERN_ERR DRV_NAME
834 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
835 bond->dev->name, new_value, 1, INT_MAX);
839 if ((new_value % bond->params.miimon) != 0) {
840 printk(KERN_WARNING DRV_NAME
841 ": %s: Warning: up delay (%d) is not a multiple "
842 "of miimon (%d), updelay rounded to %d ms\n",
843 bond->dev->name, new_value, bond->params.miimon,
844 (new_value / bond->params.miimon) *
845 bond->params.miimon);
847 bond->params.updelay = new_value / bond->params.miimon;
848 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
849 bond->dev->name, bond->params.updelay * bond->params.miimon);
856 static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
859 * Show and set the LACP interval. Interface must be down, and the mode
860 * must be set to 802.3ad mode.
862 static ssize_t bonding_show_lacp(struct device *d,
863 struct device_attribute *attr,
866 struct bonding *bond = to_bond(d);
868 return sprintf(buf, "%s %d\n",
869 bond_lacp_tbl[bond->params.lacp_fast].modename,
870 bond->params.lacp_fast);
873 static ssize_t bonding_store_lacp(struct device *d,
874 struct device_attribute *attr,
875 const char *buf, size_t count)
877 int new_value, ret = count;
878 struct bonding *bond = to_bond(d);
880 if (bond->dev->flags & IFF_UP) {
881 printk(KERN_ERR DRV_NAME
882 ": %s: Unable to update LACP rate because interface is up.\n",
888 if (bond->params.mode != BOND_MODE_8023AD) {
889 printk(KERN_ERR DRV_NAME
890 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
896 new_value = bond_parse_parm(buf, bond_lacp_tbl);
898 if ((new_value == 1) || (new_value == 0)) {
899 bond->params.lacp_fast = new_value;
900 printk(KERN_INFO DRV_NAME
901 ": %s: Setting LACP rate to %s (%d).\n",
902 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
904 printk(KERN_ERR DRV_NAME
905 ": %s: Ignoring invalid LACP rate value %.*s.\n",
906 bond->dev->name, (int)strlen(buf) - 1, buf);
912 static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
914 static ssize_t bonding_show_ad_select(struct device *d,
915 struct device_attribute *attr,
918 struct bonding *bond = to_bond(d);
920 return sprintf(buf, "%s %d\n",
921 ad_select_tbl[bond->params.ad_select].modename,
922 bond->params.ad_select);
926 static ssize_t bonding_store_ad_select(struct device *d,
927 struct device_attribute *attr,
928 const char *buf, size_t count)
930 int new_value, ret = count;
931 struct bonding *bond = to_bond(d);
933 if (bond->dev->flags & IFF_UP) {
934 printk(KERN_ERR DRV_NAME
935 ": %s: Unable to update ad_select because interface "
936 "is up.\n", bond->dev->name);
941 new_value = bond_parse_parm(buf, ad_select_tbl);
943 if (new_value != -1) {
944 bond->params.ad_select = new_value;
945 printk(KERN_INFO DRV_NAME
946 ": %s: Setting ad_select to %s (%d).\n",
947 bond->dev->name, ad_select_tbl[new_value].modename,
950 printk(KERN_ERR DRV_NAME
951 ": %s: Ignoring invalid ad_select value %.*s.\n",
952 bond->dev->name, (int)strlen(buf) - 1, buf);
959 static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR, bonding_show_ad_select, bonding_store_ad_select);
962 * Show and set the number of grat ARP to send after a failover event.
964 static ssize_t bonding_show_n_grat_arp(struct device *d,
965 struct device_attribute *attr,
968 struct bonding *bond = to_bond(d);
970 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
973 static ssize_t bonding_store_n_grat_arp(struct device *d,
974 struct device_attribute *attr,
975 const char *buf, size_t count)
977 int new_value, ret = count;
978 struct bonding *bond = to_bond(d);
980 if (sscanf(buf, "%d", &new_value) != 1) {
981 printk(KERN_ERR DRV_NAME
982 ": %s: no num_grat_arp value specified.\n",
987 if (new_value < 0 || new_value > 255) {
988 printk(KERN_ERR DRV_NAME
989 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
990 bond->dev->name, new_value);
994 bond->params.num_grat_arp = new_value;
999 static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR, bonding_show_n_grat_arp, bonding_store_n_grat_arp);
1002 * Show and set the number of unsolicted NA's to send after a failover event.
1004 static ssize_t bonding_show_n_unsol_na(struct device *d,
1005 struct device_attribute *attr,
1008 struct bonding *bond = to_bond(d);
1010 return sprintf(buf, "%d\n", bond->params.num_unsol_na);
1013 static ssize_t bonding_store_n_unsol_na(struct device *d,
1014 struct device_attribute *attr,
1015 const char *buf, size_t count)
1017 int new_value, ret = count;
1018 struct bonding *bond = to_bond(d);
1020 if (sscanf(buf, "%d", &new_value) != 1) {
1021 printk(KERN_ERR DRV_NAME
1022 ": %s: no num_unsol_na value specified.\n",
1027 if (new_value < 0 || new_value > 255) {
1028 printk(KERN_ERR DRV_NAME
1029 ": %s: Invalid num_unsol_na value %d not in range 0-255; rejected.\n",
1030 bond->dev->name, new_value);
1034 bond->params.num_unsol_na = new_value;
1039 static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR, bonding_show_n_unsol_na, bonding_store_n_unsol_na);
1042 * Show and set the MII monitor interval. There are two tricky bits
1043 * here. First, if MII monitoring is activated, then we must disable
1044 * ARP monitoring. Second, if the timer isn't running, we must
1047 static ssize_t bonding_show_miimon(struct device *d,
1048 struct device_attribute *attr,
1051 struct bonding *bond = to_bond(d);
1053 return sprintf(buf, "%d\n", bond->params.miimon);
1056 static ssize_t bonding_store_miimon(struct device *d,
1057 struct device_attribute *attr,
1058 const char *buf, size_t count)
1060 int new_value, ret = count;
1061 struct bonding *bond = to_bond(d);
1063 if (sscanf(buf, "%d", &new_value) != 1) {
1064 printk(KERN_ERR DRV_NAME
1065 ": %s: no miimon value specified.\n",
1070 if (new_value < 0) {
1071 printk(KERN_ERR DRV_NAME
1072 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1073 bond->dev->name, new_value, 1, INT_MAX);
1077 printk(KERN_INFO DRV_NAME
1078 ": %s: Setting MII monitoring interval to %d.\n",
1079 bond->dev->name, new_value);
1080 bond->params.miimon = new_value;
1081 if(bond->params.updelay)
1082 printk(KERN_INFO DRV_NAME
1083 ": %s: Note: Updating updelay (to %d) "
1084 "since it is a multiple of the miimon value.\n",
1086 bond->params.updelay * bond->params.miimon);
1087 if(bond->params.downdelay)
1088 printk(KERN_INFO DRV_NAME
1089 ": %s: Note: Updating downdelay (to %d) "
1090 "since it is a multiple of the miimon value.\n",
1092 bond->params.downdelay * bond->params.miimon);
1093 if (bond->params.arp_interval) {
1094 printk(KERN_INFO DRV_NAME
1095 ": %s: MII monitoring cannot be used with "
1096 "ARP monitoring. Disabling ARP monitoring...\n",
1098 bond->params.arp_interval = 0;
1099 bond->dev->priv_flags &= ~IFF_MASTER_ARPMON;
1100 if (bond->params.arp_validate) {
1101 bond_unregister_arp(bond);
1102 bond->params.arp_validate =
1103 BOND_ARP_VALIDATE_NONE;
1105 if (delayed_work_pending(&bond->arp_work)) {
1106 cancel_delayed_work(&bond->arp_work);
1107 flush_workqueue(bond->wq);
1111 if (bond->dev->flags & IFF_UP) {
1112 /* If the interface is up, we may need to fire off
1113 * the MII timer. If the interface is down, the
1114 * timer will get fired off when the open function
1117 if (!delayed_work_pending(&bond->mii_work)) {
1118 INIT_DELAYED_WORK(&bond->mii_work,
1120 queue_delayed_work(bond->wq,
1121 &bond->mii_work, 0);
1128 static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
1131 * Show and set the primary slave. The store function is much
1132 * simpler than bonding_store_slaves function because it only needs to
1133 * handle one interface name.
1134 * The bond must be a mode that supports a primary for this be
1137 static ssize_t bonding_show_primary(struct device *d,
1138 struct device_attribute *attr,
1142 struct bonding *bond = to_bond(d);
1144 if (bond->primary_slave)
1145 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1150 static ssize_t bonding_store_primary(struct device *d,
1151 struct device_attribute *attr,
1152 const char *buf, size_t count)
1155 struct slave *slave;
1156 struct bonding *bond = to_bond(d);
1158 if (!rtnl_trylock())
1159 return restart_syscall();
1160 read_lock(&bond->lock);
1161 write_lock_bh(&bond->curr_slave_lock);
1163 if (!USES_PRIMARY(bond->params.mode)) {
1164 printk(KERN_INFO DRV_NAME
1165 ": %s: Unable to set primary slave; %s is in mode %d\n",
1166 bond->dev->name, bond->dev->name, bond->params.mode);
1168 bond_for_each_slave(bond, slave, i) {
1170 (slave->dev->name, buf,
1171 strlen(slave->dev->name)) == 0) {
1172 printk(KERN_INFO DRV_NAME
1173 ": %s: Setting %s as primary slave.\n",
1174 bond->dev->name, slave->dev->name);
1175 bond->primary_slave = slave;
1176 bond_select_active_slave(bond);
1181 /* if we got here, then we didn't match the name of any slave */
1183 if (strlen(buf) == 0 || buf[0] == '\n') {
1184 printk(KERN_INFO DRV_NAME
1185 ": %s: Setting primary slave to None.\n",
1187 bond->primary_slave = NULL;
1188 bond_select_active_slave(bond);
1190 printk(KERN_INFO DRV_NAME
1191 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1192 bond->dev->name, (int)strlen(buf) - 1, buf);
1196 write_unlock_bh(&bond->curr_slave_lock);
1197 read_unlock(&bond->lock);
1202 static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, 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) {
1225 printk(KERN_ERR DRV_NAME
1226 ": %s: no use_carrier value specified.\n",
1231 if ((new_value == 0) || (new_value == 1)) {
1232 bond->params.use_carrier = new_value;
1233 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1234 bond->dev->name, new_value);
1236 printk(KERN_INFO DRV_NAME
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, bonding_show_carrier, bonding_store_carrier);
1247 * Show and set currently active_slave.
1249 static ssize_t bonding_show_active_slave(struct device *d,
1250 struct device_attribute *attr,
1254 struct bonding *bond = to_bond(d);
1257 read_lock(&bond->curr_slave_lock);
1258 curr = bond->curr_active_slave;
1259 read_unlock(&bond->curr_slave_lock);
1261 if (USES_PRIMARY(bond->params.mode) && curr)
1262 count = sprintf(buf, "%s\n", curr->dev->name);
1266 static ssize_t bonding_store_active_slave(struct device *d,
1267 struct device_attribute *attr,
1268 const char *buf, size_t count)
1271 struct slave *slave;
1272 struct slave *old_active = NULL;
1273 struct slave *new_active = NULL;
1274 struct bonding *bond = to_bond(d);
1276 if (!rtnl_trylock())
1277 return restart_syscall();
1278 read_lock(&bond->lock);
1279 write_lock_bh(&bond->curr_slave_lock);
1281 if (!USES_PRIMARY(bond->params.mode)) {
1282 printk(KERN_INFO DRV_NAME
1283 ": %s: Unable to change active slave; %s is in mode %d\n",
1284 bond->dev->name, bond->dev->name, bond->params.mode);
1286 bond_for_each_slave(bond, slave, i) {
1288 (slave->dev->name, buf,
1289 strlen(slave->dev->name)) == 0) {
1290 old_active = bond->curr_active_slave;
1292 if (new_active == old_active) {
1294 printk(KERN_INFO DRV_NAME
1295 ": %s: %s is already the current active slave.\n",
1296 bond->dev->name, slave->dev->name);
1302 (new_active->link == BOND_LINK_UP) &&
1303 IS_UP(new_active->dev)) {
1304 printk(KERN_INFO DRV_NAME
1305 ": %s: Setting %s as active slave.\n",
1306 bond->dev->name, slave->dev->name);
1307 bond_change_active_slave(bond, new_active);
1310 printk(KERN_INFO DRV_NAME
1311 ": %s: Could not set %s as active slave; "
1312 "either %s is down or the link is down.\n",
1313 bond->dev->name, slave->dev->name,
1321 /* if we got here, then we didn't match the name of any slave */
1323 if (strlen(buf) == 0 || buf[0] == '\n') {
1324 printk(KERN_INFO DRV_NAME
1325 ": %s: Setting active slave to None.\n",
1327 bond->primary_slave = NULL;
1328 bond_select_active_slave(bond);
1330 printk(KERN_INFO DRV_NAME
1331 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1332 bond->dev->name, (int)strlen(buf) - 1, buf);
1336 write_unlock_bh(&bond->curr_slave_lock);
1337 read_unlock(&bond->lock);
1343 static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
1347 * Show link status of the bond interface.
1349 static ssize_t bonding_show_mii_status(struct device *d,
1350 struct device_attribute *attr,
1354 struct bonding *bond = to_bond(d);
1356 read_lock(&bond->curr_slave_lock);
1357 curr = bond->curr_active_slave;
1358 read_unlock(&bond->curr_slave_lock);
1360 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
1362 static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1366 * Show current 802.3ad aggregator ID.
1368 static ssize_t bonding_show_ad_aggregator(struct device *d,
1369 struct device_attribute *attr,
1373 struct bonding *bond = to_bond(d);
1375 if (bond->params.mode == BOND_MODE_8023AD) {
1376 struct ad_info ad_info;
1377 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id);
1382 static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1386 * Show number of active 802.3ad ports.
1388 static ssize_t bonding_show_ad_num_ports(struct device *d,
1389 struct device_attribute *attr,
1393 struct bonding *bond = to_bond(d);
1395 if (bond->params.mode == BOND_MODE_8023AD) {
1396 struct ad_info ad_info;
1397 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports);
1402 static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1406 * Show current 802.3ad actor key.
1408 static ssize_t bonding_show_ad_actor_key(struct device *d,
1409 struct device_attribute *attr,
1413 struct bonding *bond = to_bond(d);
1415 if (bond->params.mode == BOND_MODE_8023AD) {
1416 struct ad_info ad_info;
1417 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key);
1422 static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1426 * Show current 802.3ad partner key.
1428 static ssize_t bonding_show_ad_partner_key(struct device *d,
1429 struct device_attribute *attr,
1433 struct bonding *bond = to_bond(d);
1435 if (bond->params.mode == BOND_MODE_8023AD) {
1436 struct ad_info ad_info;
1437 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key);
1442 static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1446 * Show current 802.3ad partner mac.
1448 static ssize_t bonding_show_ad_partner_mac(struct device *d,
1449 struct device_attribute *attr,
1453 struct bonding *bond = to_bond(d);
1455 if (bond->params.mode == BOND_MODE_8023AD) {
1456 struct ad_info ad_info;
1457 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1458 count = sprintf(buf, "%pM\n", ad_info.partner_system);
1464 static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1468 static struct attribute *per_bond_attrs[] = {
1469 &dev_attr_slaves.attr,
1470 &dev_attr_mode.attr,
1471 &dev_attr_fail_over_mac.attr,
1472 &dev_attr_arp_validate.attr,
1473 &dev_attr_arp_interval.attr,
1474 &dev_attr_arp_ip_target.attr,
1475 &dev_attr_downdelay.attr,
1476 &dev_attr_updelay.attr,
1477 &dev_attr_lacp_rate.attr,
1478 &dev_attr_ad_select.attr,
1479 &dev_attr_xmit_hash_policy.attr,
1480 &dev_attr_num_grat_arp.attr,
1481 &dev_attr_num_unsol_na.attr,
1482 &dev_attr_miimon.attr,
1483 &dev_attr_primary.attr,
1484 &dev_attr_use_carrier.attr,
1485 &dev_attr_active_slave.attr,
1486 &dev_attr_mii_status.attr,
1487 &dev_attr_ad_aggregator.attr,
1488 &dev_attr_ad_num_ports.attr,
1489 &dev_attr_ad_actor_key.attr,
1490 &dev_attr_ad_partner_key.attr,
1491 &dev_attr_ad_partner_mac.attr,
1495 static struct attribute_group bonding_group = {
1497 .attrs = per_bond_attrs,
1501 * Initialize sysfs. This sets up the bonding_masters file in
1504 int bond_create_sysfs(void)
1508 ret = netdev_class_create_file(&class_attr_bonding_masters);
1510 * Permit multiple loads of the module by ignoring failures to
1511 * create the bonding_masters sysfs file. Bonding devices
1512 * created by second or subsequent loads of the module will
1513 * not be listed in, or controllable by, bonding_masters, but
1514 * will have the usual "bonding" sysfs directory.
1516 * This is done to preserve backwards compatibility for
1517 * initscripts/sysconfig, which load bonding multiple times to
1518 * configure multiple bonding devices.
1520 if (ret == -EEXIST) {
1521 /* Is someone being kinky and naming a device bonding_master? */
1522 if (__dev_get_by_name(&init_net,
1523 class_attr_bonding_masters.attr.name))
1525 "network device named %s already exists in sysfs",
1526 class_attr_bonding_masters.attr.name);
1535 * Remove /sys/class/net/bonding_masters.
1537 void bond_destroy_sysfs(void)
1539 netdev_class_remove_file(&class_attr_bonding_masters);
1543 * Initialize sysfs for each bond. This sets up and registers
1544 * the 'bondctl' directory for each individual bond under /sys/class/net.
1546 int bond_create_sysfs_entry(struct bonding *bond)
1548 struct net_device *dev = bond->dev;
1551 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
1553 printk(KERN_EMERG "eek! didn't create group!\n");
1556 if (expected_refcount < 1)
1557 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
1562 * Remove sysfs entries for each bond.
1564 void bond_destroy_sysfs_entry(struct bonding *bond)
1566 struct net_device *dev = bond->dev;
1568 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);