Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / net / bonding / bond_sysfs.c
blobced9ed8f995a45e00f657d085f8f2fce30a16c98
2 /*
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
13 * for more details.
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/sched.h>
26 #include <linux/device.h>
27 #include <linux/sysdev.h>
28 #include <linux/fs.h>
29 #include <linux/types.h>
30 #include <linux/string.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
33 #include <linux/in.h>
34 #include <linux/sysfs.h>
35 #include <linux/string.h>
36 #include <linux/ctype.h>
37 #include <linux/inet.h>
38 #include <linux/rtnetlink.h>
40 /* #define BONDING_DEBUG 1 */
41 #include "bonding.h"
42 #define to_class_dev(obj) container_of(obj,struct class_device,kobj)
43 #define to_net_dev(class) container_of(class, struct net_device, class_dev)
44 #define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
46 /*---------------------------- Declarations -------------------------------*/
49 extern struct list_head bond_dev_list;
50 extern struct bond_params bonding_defaults;
51 extern struct bond_parm_tbl bond_mode_tbl[];
52 extern struct bond_parm_tbl bond_lacp_tbl[];
53 extern struct bond_parm_tbl xmit_hashtype_tbl[];
54 extern struct bond_parm_tbl arp_validate_tbl[];
56 static int expected_refcount = -1;
57 static struct class *netdev_class;
58 /*--------------------------- Data Structures -----------------------------*/
60 /* Bonding sysfs lock. Why can't we just use the subsytem lock?
61 * Because kobject_register tries to acquire the subsystem lock. If
62 * we already hold the lock (which we would if the user was creating
63 * a new bond through the sysfs interface), we deadlock.
64 * This lock is only needed when deleting a bond - we need to make sure
65 * that we don't collide with an ongoing ioctl.
68 struct rw_semaphore bonding_rwsem;
73 /*------------------------------ Functions --------------------------------*/
76 * "show" function for the bond_masters attribute.
77 * The class parameter is ignored.
79 static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
81 int res = 0;
82 struct bonding *bond;
84 down_read(&(bonding_rwsem));
86 list_for_each_entry(bond, &bond_dev_list, bond_list) {
87 if (res > (PAGE_SIZE - IFNAMSIZ)) {
88 /* not enough space for another interface name */
89 if ((PAGE_SIZE - res) > 10)
90 res = PAGE_SIZE - 10;
91 res += sprintf(buffer + res, "++more++");
92 break;
94 res += sprintf(buffer + res, "%s ",
95 bond->dev->name);
97 res += sprintf(buffer + res, "\n");
98 res++;
99 up_read(&(bonding_rwsem));
100 return res;
104 * "store" function for the bond_masters attribute. This is what
105 * creates and deletes entire bonds.
107 * The class parameter is ignored.
111 static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
113 char command[IFNAMSIZ + 1] = {0, };
114 char *ifname;
115 int res = count;
116 struct bonding *bond;
117 struct bonding *nxt;
119 down_write(&(bonding_rwsem));
120 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
121 ifname = command + 1;
122 if ((strlen(command) <= 1) ||
123 !dev_valid_name(ifname))
124 goto err_no_cmd;
126 if (command[0] == '+') {
128 /* Check to see if the bond already exists. */
129 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
130 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
131 printk(KERN_ERR DRV_NAME
132 ": cannot add bond %s; it already exists\n",
133 ifname);
134 res = -EPERM;
135 goto out;
138 printk(KERN_INFO DRV_NAME
139 ": %s is being created...\n", ifname);
140 if (bond_create(ifname, &bonding_defaults, &bond)) {
141 printk(KERN_INFO DRV_NAME
142 ": %s interface already exists. Bond creation failed.\n",
143 ifname);
144 res = -EPERM;
146 goto out;
149 if (command[0] == '-') {
150 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
151 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
152 rtnl_lock();
153 /* check the ref count on the bond's kobject.
154 * If it's > expected, then there's a file open,
155 * and we have to fail.
157 if (atomic_read(&bond->dev->class_dev.kobj.kref.refcount)
158 > expected_refcount){
159 rtnl_unlock();
160 printk(KERN_INFO DRV_NAME
161 ": Unable remove bond %s due to open references.\n",
162 ifname);
163 res = -EPERM;
164 goto out;
166 printk(KERN_INFO DRV_NAME
167 ": %s is being deleted...\n",
168 bond->dev->name);
169 unregister_netdevice(bond->dev);
170 bond_deinit(bond->dev);
171 bond_destroy_sysfs_entry(bond);
172 rtnl_unlock();
173 goto out;
176 printk(KERN_ERR DRV_NAME
177 ": unable to delete non-existent bond %s\n", ifname);
178 res = -ENODEV;
179 goto out;
182 err_no_cmd:
183 printk(KERN_ERR DRV_NAME
184 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
185 res = -EPERM;
187 /* Always return either count or an error. If you return 0, you'll
188 * get called forever, which is bad.
190 out:
191 up_write(&(bonding_rwsem));
192 return res;
194 /* class attribute for bond_masters file. This ends up in /sys/class/net */
195 static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
196 bonding_show_bonds, bonding_store_bonds);
198 int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
200 char linkname[IFNAMSIZ+7];
201 int ret = 0;
203 /* first, create a link from the slave back to the master */
204 ret = sysfs_create_link(&(slave->class_dev.kobj), &(master->class_dev.kobj),
205 "master");
206 if (ret)
207 return ret;
208 /* next, create a link from the master to the slave */
209 sprintf(linkname,"slave_%s",slave->name);
210 ret = sysfs_create_link(&(master->class_dev.kobj), &(slave->class_dev.kobj),
211 linkname);
212 return ret;
216 void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
218 char linkname[IFNAMSIZ+7];
220 sysfs_remove_link(&(slave->class_dev.kobj), "master");
221 sprintf(linkname,"slave_%s",slave->name);
222 sysfs_remove_link(&(master->class_dev.kobj), linkname);
227 * Show the slaves in the current bond.
229 static ssize_t bonding_show_slaves(struct class_device *cd, char *buf)
231 struct slave *slave;
232 int i, res = 0;
233 struct bonding *bond = to_bond(cd);
235 read_lock_bh(&bond->lock);
236 bond_for_each_slave(bond, slave, i) {
237 if (res > (PAGE_SIZE - IFNAMSIZ)) {
238 /* not enough space for another interface name */
239 if ((PAGE_SIZE - res) > 10)
240 res = PAGE_SIZE - 10;
241 res += sprintf(buf + res, "++more++");
242 break;
244 res += sprintf(buf + res, "%s ", slave->dev->name);
246 read_unlock_bh(&bond->lock);
247 res += sprintf(buf + res, "\n");
248 res++;
249 return res;
253 * Set the slaves in the current bond. The bond interface must be
254 * up for this to succeed.
255 * This function is largely the same flow as bonding_update_bonds().
257 static ssize_t bonding_store_slaves(struct class_device *cd, const char *buffer, size_t count)
259 char command[IFNAMSIZ + 1] = { 0, };
260 char *ifname;
261 int i, res, found, ret = count;
262 struct slave *slave;
263 struct net_device *dev = NULL;
264 struct bonding *bond = to_bond(cd);
266 /* Quick sanity check -- is the bond interface up? */
267 if (!(bond->dev->flags & IFF_UP)) {
268 printk(KERN_ERR DRV_NAME
269 ": %s: Unable to update slaves because interface is down.\n",
270 bond->dev->name);
271 ret = -EPERM;
272 goto out;
275 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
277 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
278 ifname = command + 1;
279 if ((strlen(command) <= 1) ||
280 !dev_valid_name(ifname))
281 goto err_no_cmd;
283 if (command[0] == '+') {
285 /* Got a slave name in ifname. Is it already in the list? */
286 found = 0;
287 read_lock_bh(&bond->lock);
288 bond_for_each_slave(bond, slave, i)
289 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
290 printk(KERN_ERR DRV_NAME
291 ": %s: Interface %s is already enslaved!\n",
292 bond->dev->name, ifname);
293 ret = -EPERM;
294 read_unlock_bh(&bond->lock);
295 goto out;
298 read_unlock_bh(&bond->lock);
299 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
300 bond->dev->name, ifname);
301 dev = dev_get_by_name(ifname);
302 if (!dev) {
303 printk(KERN_INFO DRV_NAME
304 ": %s: Interface %s does not exist!\n",
305 bond->dev->name, ifname);
306 ret = -EPERM;
307 goto out;
309 else
310 dev_put(dev);
312 if (dev->flags & IFF_UP) {
313 printk(KERN_ERR DRV_NAME
314 ": %s: Error: Unable to enslave %s "
315 "because it is already up.\n",
316 bond->dev->name, dev->name);
317 ret = -EPERM;
318 goto out;
320 /* If this is the first slave, then we need to set
321 the master's hardware address to be the same as the
322 slave's. */
323 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
324 memcpy(bond->dev->dev_addr, dev->dev_addr,
325 dev->addr_len);
328 /* Set the slave's MTU to match the bond */
329 if (dev->mtu != bond->dev->mtu) {
330 if (dev->change_mtu) {
331 res = dev->change_mtu(dev,
332 bond->dev->mtu);
333 if (res) {
334 ret = res;
335 goto out;
337 } else {
338 dev->mtu = bond->dev->mtu;
341 rtnl_lock();
342 res = bond_enslave(bond->dev, dev);
343 rtnl_unlock();
344 if (res) {
345 ret = res;
347 goto out;
350 if (command[0] == '-') {
351 dev = NULL;
352 bond_for_each_slave(bond, slave, i)
353 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
354 dev = slave->dev;
355 break;
357 if (dev) {
358 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
359 bond->dev->name, dev->name);
360 rtnl_lock();
361 res = bond_release(bond->dev, dev);
362 rtnl_unlock();
363 if (res) {
364 ret = res;
365 goto out;
367 /* set the slave MTU to the default */
368 if (dev->change_mtu) {
369 dev->change_mtu(dev, 1500);
370 } else {
371 dev->mtu = 1500;
374 else {
375 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
376 ifname, bond->dev->name);
377 ret = -ENODEV;
379 goto out;
382 err_no_cmd:
383 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
384 ret = -EPERM;
386 out:
387 return ret;
390 static CLASS_DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
393 * Show and set the bonding mode. The bond interface must be down to
394 * change the mode.
396 static ssize_t bonding_show_mode(struct class_device *cd, char *buf)
398 struct bonding *bond = to_bond(cd);
400 return sprintf(buf, "%s %d\n",
401 bond_mode_tbl[bond->params.mode].modename,
402 bond->params.mode) + 1;
405 static ssize_t bonding_store_mode(struct class_device *cd, const char *buf, size_t count)
407 int new_value, ret = count;
408 struct bonding *bond = to_bond(cd);
410 if (bond->dev->flags & IFF_UP) {
411 printk(KERN_ERR DRV_NAME
412 ": unable to update mode of %s because interface is up.\n",
413 bond->dev->name);
414 ret = -EPERM;
415 goto out;
418 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
419 if (new_value < 0) {
420 printk(KERN_ERR DRV_NAME
421 ": %s: Ignoring invalid mode value %.*s.\n",
422 bond->dev->name,
423 (int)strlen(buf) - 1, buf);
424 ret = -EINVAL;
425 goto out;
426 } else {
427 if (bond->params.mode == BOND_MODE_8023AD)
428 bond_unset_master_3ad_flags(bond);
430 if (bond->params.mode == BOND_MODE_ALB)
431 bond_unset_master_alb_flags(bond);
433 bond->params.mode = new_value;
434 bond_set_mode_ops(bond, bond->params.mode);
435 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
436 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
438 out:
439 return ret;
441 static CLASS_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
444 * Show and set the bonding transmit hash method. The bond interface must be down to
445 * change the xmit hash policy.
447 static ssize_t bonding_show_xmit_hash(struct class_device *cd, char *buf)
449 int count;
450 struct bonding *bond = to_bond(cd);
452 if ((bond->params.mode != BOND_MODE_XOR) &&
453 (bond->params.mode != BOND_MODE_8023AD)) {
454 // Not Applicable
455 count = sprintf(buf, "NA\n") + 1;
456 } else {
457 count = sprintf(buf, "%s %d\n",
458 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
459 bond->params.xmit_policy) + 1;
462 return count;
465 static ssize_t bonding_store_xmit_hash(struct class_device *cd, const char *buf, size_t count)
467 int new_value, ret = count;
468 struct bonding *bond = to_bond(cd);
470 if (bond->dev->flags & IFF_UP) {
471 printk(KERN_ERR DRV_NAME
472 "%s: Interface is up. Unable to update xmit policy.\n",
473 bond->dev->name);
474 ret = -EPERM;
475 goto out;
478 if ((bond->params.mode != BOND_MODE_XOR) &&
479 (bond->params.mode != BOND_MODE_8023AD)) {
480 printk(KERN_ERR DRV_NAME
481 "%s: Transmit hash policy is irrelevant in this mode.\n",
482 bond->dev->name);
483 ret = -EPERM;
484 goto out;
487 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
488 if (new_value < 0) {
489 printk(KERN_ERR DRV_NAME
490 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
491 bond->dev->name,
492 (int)strlen(buf) - 1, buf);
493 ret = -EINVAL;
494 goto out;
495 } else {
496 bond->params.xmit_policy = new_value;
497 bond_set_mode_ops(bond, bond->params.mode);
498 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
499 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
501 out:
502 return ret;
504 static CLASS_DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
507 * Show and set arp_validate.
509 static ssize_t bonding_show_arp_validate(struct class_device *cd, char *buf)
511 struct bonding *bond = to_bond(cd);
513 return sprintf(buf, "%s %d\n",
514 arp_validate_tbl[bond->params.arp_validate].modename,
515 bond->params.arp_validate) + 1;
518 static ssize_t bonding_store_arp_validate(struct class_device *cd, const char *buf, size_t count)
520 int new_value;
521 struct bonding *bond = to_bond(cd);
523 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
524 if (new_value < 0) {
525 printk(KERN_ERR DRV_NAME
526 ": %s: Ignoring invalid arp_validate value %s\n",
527 bond->dev->name, buf);
528 return -EINVAL;
530 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
531 printk(KERN_ERR DRV_NAME
532 ": %s: arp_validate only supported in active-backup mode.\n",
533 bond->dev->name);
534 return -EINVAL;
536 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
537 bond->dev->name, arp_validate_tbl[new_value].modename,
538 new_value);
540 if (!bond->params.arp_validate && new_value) {
541 bond_register_arp(bond);
542 } else if (bond->params.arp_validate && !new_value) {
543 bond_unregister_arp(bond);
546 bond->params.arp_validate = new_value;
548 return count;
551 static CLASS_DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
554 * Show and set the arp timer interval. There are two tricky bits
555 * here. First, if ARP monitoring is activated, then we must disable
556 * MII monitoring. Second, if the ARP timer isn't running, we must
557 * start it.
559 static ssize_t bonding_show_arp_interval(struct class_device *cd, char *buf)
561 struct bonding *bond = to_bond(cd);
563 return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
566 static ssize_t bonding_store_arp_interval(struct class_device *cd, const char *buf, size_t count)
568 int new_value, ret = count;
569 struct bonding *bond = to_bond(cd);
571 if (sscanf(buf, "%d", &new_value) != 1) {
572 printk(KERN_ERR DRV_NAME
573 ": %s: no arp_interval value specified.\n",
574 bond->dev->name);
575 ret = -EINVAL;
576 goto out;
578 if (new_value < 0) {
579 printk(KERN_ERR DRV_NAME
580 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
581 bond->dev->name, new_value, INT_MAX);
582 ret = -EINVAL;
583 goto out;
586 printk(KERN_INFO DRV_NAME
587 ": %s: Setting ARP monitoring interval to %d.\n",
588 bond->dev->name, new_value);
589 bond->params.arp_interval = new_value;
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 /* Kill MII timer, else it brings bond's link down */
597 if (bond->arp_timer.function) {
598 printk(KERN_INFO DRV_NAME
599 ": %s: Kill MII timer, else it brings bond's link down...\n",
600 bond->dev->name);
601 del_timer_sync(&bond->mii_timer);
604 if (!bond->params.arp_targets[0]) {
605 printk(KERN_INFO DRV_NAME
606 ": %s: ARP monitoring has been set up, "
607 "but no ARP targets have been specified.\n",
608 bond->dev->name);
610 if (bond->dev->flags & IFF_UP) {
611 /* If the interface is up, we may need to fire off
612 * the ARP timer. If the interface is down, the
613 * timer will get fired off when the open function
614 * is called.
616 if (bond->arp_timer.function) {
617 /* The timer's already set up, so fire it off */
618 mod_timer(&bond->arp_timer, jiffies + 1);
619 } else {
620 /* Set up the timer. */
621 init_timer(&bond->arp_timer);
622 bond->arp_timer.expires = jiffies + 1;
623 bond->arp_timer.data =
624 (unsigned long) bond->dev;
625 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
626 bond->arp_timer.function =
627 (void *)
628 &bond_activebackup_arp_mon;
629 } else {
630 bond->arp_timer.function =
631 (void *)
632 &bond_loadbalance_arp_mon;
634 add_timer(&bond->arp_timer);
638 out:
639 return ret;
641 static CLASS_DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
644 * Show and set the arp targets.
646 static ssize_t bonding_show_arp_targets(struct class_device *cd, char *buf)
648 int i, res = 0;
649 struct bonding *bond = to_bond(cd);
651 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
652 if (bond->params.arp_targets[i])
653 res += sprintf(buf + res, "%u.%u.%u.%u ",
654 NIPQUAD(bond->params.arp_targets[i]));
656 if (res)
657 res--; /* eat the leftover space */
658 res += sprintf(buf + res, "\n");
659 res++;
660 return res;
663 static ssize_t bonding_store_arp_targets(struct class_device *cd, const char *buf, size_t count)
665 u32 newtarget;
666 int i = 0, done = 0, ret = count;
667 struct bonding *bond = to_bond(cd);
668 u32 *targets;
670 targets = bond->params.arp_targets;
671 newtarget = in_aton(buf + 1);
672 /* look for adds */
673 if (buf[0] == '+') {
674 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
675 printk(KERN_ERR DRV_NAME
676 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
677 bond->dev->name, NIPQUAD(newtarget));
678 ret = -EINVAL;
679 goto out;
681 /* look for an empty slot to put the target in, and check for dupes */
682 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
683 if (targets[i] == newtarget) { /* duplicate */
684 printk(KERN_ERR DRV_NAME
685 ": %s: ARP target %u.%u.%u.%u is already present\n",
686 bond->dev->name, NIPQUAD(newtarget));
687 if (done)
688 targets[i] = 0;
689 ret = -EINVAL;
690 goto out;
692 if (targets[i] == 0 && !done) {
693 printk(KERN_INFO DRV_NAME
694 ": %s: adding ARP target %d.%d.%d.%d.\n",
695 bond->dev->name, NIPQUAD(newtarget));
696 done = 1;
697 targets[i] = newtarget;
700 if (!done) {
701 printk(KERN_ERR DRV_NAME
702 ": %s: ARP target table is full!\n",
703 bond->dev->name);
704 ret = -EINVAL;
705 goto out;
709 else if (buf[0] == '-') {
710 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
711 printk(KERN_ERR DRV_NAME
712 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
713 bond->dev->name, NIPQUAD(newtarget));
714 ret = -EINVAL;
715 goto out;
718 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
719 if (targets[i] == newtarget) {
720 printk(KERN_INFO DRV_NAME
721 ": %s: removing ARP target %d.%d.%d.%d.\n",
722 bond->dev->name, NIPQUAD(newtarget));
723 targets[i] = 0;
724 done = 1;
727 if (!done) {
728 printk(KERN_INFO DRV_NAME
729 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
730 bond->dev->name, NIPQUAD(newtarget));
731 ret = -EINVAL;
732 goto out;
735 else {
736 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
737 bond->dev->name);
738 ret = -EPERM;
739 goto out;
742 out:
743 return ret;
745 static CLASS_DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
748 * Show and set the up and down delays. These must be multiples of the
749 * MII monitoring value, and are stored internally as the multiplier.
750 * Thus, we must translate to MS for the real world.
752 static ssize_t bonding_show_downdelay(struct class_device *cd, char *buf)
754 struct bonding *bond = to_bond(cd);
756 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
759 static ssize_t bonding_store_downdelay(struct class_device *cd, const char *buf, size_t count)
761 int new_value, ret = count;
762 struct bonding *bond = to_bond(cd);
764 if (!(bond->params.miimon)) {
765 printk(KERN_ERR DRV_NAME
766 ": %s: Unable to set down delay as MII monitoring is disabled\n",
767 bond->dev->name);
768 ret = -EPERM;
769 goto out;
772 if (sscanf(buf, "%d", &new_value) != 1) {
773 printk(KERN_ERR DRV_NAME
774 ": %s: no down delay value specified.\n",
775 bond->dev->name);
776 ret = -EINVAL;
777 goto out;
779 if (new_value < 0) {
780 printk(KERN_ERR DRV_NAME
781 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
782 bond->dev->name, new_value, 1, INT_MAX);
783 ret = -EINVAL;
784 goto out;
785 } else {
786 if ((new_value % bond->params.miimon) != 0) {
787 printk(KERN_WARNING DRV_NAME
788 ": %s: Warning: down delay (%d) is not a multiple "
789 "of miimon (%d), delay rounded to %d ms\n",
790 bond->dev->name, new_value, bond->params.miimon,
791 (new_value / bond->params.miimon) *
792 bond->params.miimon);
794 bond->params.downdelay = new_value / bond->params.miimon;
795 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
796 bond->dev->name, bond->params.downdelay * bond->params.miimon);
800 out:
801 return ret;
803 static CLASS_DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
805 static ssize_t bonding_show_updelay(struct class_device *cd, char *buf)
807 struct bonding *bond = to_bond(cd);
809 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
813 static ssize_t bonding_store_updelay(struct class_device *cd, const char *buf, size_t count)
815 int new_value, ret = count;
816 struct bonding *bond = to_bond(cd);
818 if (!(bond->params.miimon)) {
819 printk(KERN_ERR DRV_NAME
820 ": %s: Unable to set up delay as MII monitoring is disabled\n",
821 bond->dev->name);
822 ret = -EPERM;
823 goto out;
826 if (sscanf(buf, "%d", &new_value) != 1) {
827 printk(KERN_ERR DRV_NAME
828 ": %s: no up delay value specified.\n",
829 bond->dev->name);
830 ret = -EINVAL;
831 goto out;
833 if (new_value < 0) {
834 printk(KERN_ERR DRV_NAME
835 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
836 bond->dev->name, new_value, 1, INT_MAX);
837 ret = -EINVAL;
838 goto out;
839 } else {
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 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
850 bond->dev->name, bond->params.updelay * bond->params.miimon);
854 out:
855 return ret;
857 static CLASS_DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
860 * Show and set the LACP interval. Interface must be down, and the mode
861 * must be set to 802.3ad mode.
863 static ssize_t bonding_show_lacp(struct class_device *cd, char *buf)
865 struct bonding *bond = to_bond(cd);
867 return sprintf(buf, "%s %d\n",
868 bond_lacp_tbl[bond->params.lacp_fast].modename,
869 bond->params.lacp_fast) + 1;
872 static ssize_t bonding_store_lacp(struct class_device *cd, const char *buf, size_t count)
874 int new_value, ret = count;
875 struct bonding *bond = to_bond(cd);
877 if (bond->dev->flags & IFF_UP) {
878 printk(KERN_ERR DRV_NAME
879 ": %s: Unable to update LACP rate because interface is up.\n",
880 bond->dev->name);
881 ret = -EPERM;
882 goto out;
885 if (bond->params.mode != BOND_MODE_8023AD) {
886 printk(KERN_ERR DRV_NAME
887 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
888 bond->dev->name);
889 ret = -EPERM;
890 goto out;
893 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
895 if ((new_value == 1) || (new_value == 0)) {
896 bond->params.lacp_fast = new_value;
897 printk(KERN_INFO DRV_NAME
898 ": %s: Setting LACP rate to %s (%d).\n",
899 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
900 } else {
901 printk(KERN_ERR DRV_NAME
902 ": %s: Ignoring invalid LACP rate value %.*s.\n",
903 bond->dev->name, (int)strlen(buf) - 1, buf);
904 ret = -EINVAL;
906 out:
907 return ret;
909 static CLASS_DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
912 * Show and set the MII monitor interval. There are two tricky bits
913 * here. First, if MII monitoring is activated, then we must disable
914 * ARP monitoring. Second, if the timer isn't running, we must
915 * start it.
917 static ssize_t bonding_show_miimon(struct class_device *cd, char *buf)
919 struct bonding *bond = to_bond(cd);
921 return sprintf(buf, "%d\n", bond->params.miimon) + 1;
924 static ssize_t bonding_store_miimon(struct class_device *cd, const char *buf, size_t count)
926 int new_value, ret = count;
927 struct bonding *bond = to_bond(cd);
929 if (sscanf(buf, "%d", &new_value) != 1) {
930 printk(KERN_ERR DRV_NAME
931 ": %s: no miimon value specified.\n",
932 bond->dev->name);
933 ret = -EINVAL;
934 goto out;
936 if (new_value < 0) {
937 printk(KERN_ERR DRV_NAME
938 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
939 bond->dev->name, new_value, 1, INT_MAX);
940 ret = -EINVAL;
941 goto out;
942 } else {
943 printk(KERN_INFO DRV_NAME
944 ": %s: Setting MII monitoring interval to %d.\n",
945 bond->dev->name, new_value);
946 bond->params.miimon = new_value;
947 if(bond->params.updelay)
948 printk(KERN_INFO DRV_NAME
949 ": %s: Note: Updating updelay (to %d) "
950 "since it is a multiple of the miimon value.\n",
951 bond->dev->name,
952 bond->params.updelay * bond->params.miimon);
953 if(bond->params.downdelay)
954 printk(KERN_INFO DRV_NAME
955 ": %s: Note: Updating downdelay (to %d) "
956 "since it is a multiple of the miimon value.\n",
957 bond->dev->name,
958 bond->params.downdelay * bond->params.miimon);
959 if (bond->params.arp_interval) {
960 printk(KERN_INFO DRV_NAME
961 ": %s: MII monitoring cannot be used with "
962 "ARP monitoring. Disabling ARP monitoring...\n",
963 bond->dev->name);
964 bond->params.arp_interval = 0;
965 if (bond->params.arp_validate) {
966 bond_unregister_arp(bond);
967 bond->params.arp_validate =
968 BOND_ARP_VALIDATE_NONE;
970 /* Kill ARP timer, else it brings bond's link down */
971 if (bond->mii_timer.function) {
972 printk(KERN_INFO DRV_NAME
973 ": %s: Kill ARP timer, else it brings bond's link down...\n",
974 bond->dev->name);
975 del_timer_sync(&bond->arp_timer);
979 if (bond->dev->flags & IFF_UP) {
980 /* If the interface is up, we may need to fire off
981 * the MII timer. If the interface is down, the
982 * timer will get fired off when the open function
983 * is called.
985 if (bond->mii_timer.function) {
986 /* The timer's already set up, so fire it off */
987 mod_timer(&bond->mii_timer, jiffies + 1);
988 } else {
989 /* Set up the timer. */
990 init_timer(&bond->mii_timer);
991 bond->mii_timer.expires = jiffies + 1;
992 bond->mii_timer.data =
993 (unsigned long) bond->dev;
994 bond->mii_timer.function =
995 (void *) &bond_mii_monitor;
996 add_timer(&bond->mii_timer);
1000 out:
1001 return ret;
1003 static CLASS_DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
1006 * Show and set the primary slave. The store function is much
1007 * simpler than bonding_store_slaves function because it only needs to
1008 * handle one interface name.
1009 * The bond must be a mode that supports a primary for this be
1010 * set.
1012 static ssize_t bonding_show_primary(struct class_device *cd, char *buf)
1014 int count = 0;
1015 struct bonding *bond = to_bond(cd);
1017 if (bond->primary_slave)
1018 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
1019 else
1020 count = sprintf(buf, "\n") + 1;
1022 return count;
1025 static ssize_t bonding_store_primary(struct class_device *cd, const char *buf, size_t count)
1027 int i;
1028 struct slave *slave;
1029 struct bonding *bond = to_bond(cd);
1031 write_lock_bh(&bond->lock);
1032 if (!USES_PRIMARY(bond->params.mode)) {
1033 printk(KERN_INFO DRV_NAME
1034 ": %s: Unable to set primary slave; %s is in mode %d\n",
1035 bond->dev->name, bond->dev->name, bond->params.mode);
1036 } else {
1037 bond_for_each_slave(bond, slave, i) {
1038 if (strnicmp
1039 (slave->dev->name, buf,
1040 strlen(slave->dev->name)) == 0) {
1041 printk(KERN_INFO DRV_NAME
1042 ": %s: Setting %s as primary slave.\n",
1043 bond->dev->name, slave->dev->name);
1044 bond->primary_slave = slave;
1045 bond_select_active_slave(bond);
1046 goto out;
1050 /* if we got here, then we didn't match the name of any slave */
1052 if (strlen(buf) == 0 || buf[0] == '\n') {
1053 printk(KERN_INFO DRV_NAME
1054 ": %s: Setting primary slave to None.\n",
1055 bond->dev->name);
1056 bond->primary_slave = NULL;
1057 bond_select_active_slave(bond);
1058 } else {
1059 printk(KERN_INFO DRV_NAME
1060 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1061 bond->dev->name, (int)strlen(buf) - 1, buf);
1064 out:
1065 write_unlock_bh(&bond->lock);
1066 return count;
1068 static CLASS_DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
1071 * Show and set the use_carrier flag.
1073 static ssize_t bonding_show_carrier(struct class_device *cd, char *buf)
1075 struct bonding *bond = to_bond(cd);
1077 return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
1080 static ssize_t bonding_store_carrier(struct class_device *cd, const char *buf, size_t count)
1082 int new_value, ret = count;
1083 struct bonding *bond = to_bond(cd);
1086 if (sscanf(buf, "%d", &new_value) != 1) {
1087 printk(KERN_ERR DRV_NAME
1088 ": %s: no use_carrier value specified.\n",
1089 bond->dev->name);
1090 ret = -EINVAL;
1091 goto out;
1093 if ((new_value == 0) || (new_value == 1)) {
1094 bond->params.use_carrier = new_value;
1095 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1096 bond->dev->name, new_value);
1097 } else {
1098 printk(KERN_INFO DRV_NAME
1099 ": %s: Ignoring invalid use_carrier value %d.\n",
1100 bond->dev->name, new_value);
1102 out:
1103 return count;
1105 static CLASS_DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
1109 * Show and set currently active_slave.
1111 static ssize_t bonding_show_active_slave(struct class_device *cd, char *buf)
1113 struct slave *curr;
1114 struct bonding *bond = to_bond(cd);
1115 int count;
1118 read_lock(&bond->curr_slave_lock);
1119 curr = bond->curr_active_slave;
1120 read_unlock(&bond->curr_slave_lock);
1122 if (USES_PRIMARY(bond->params.mode) && curr)
1123 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
1124 else
1125 count = sprintf(buf, "\n") + 1;
1126 return count;
1129 static ssize_t bonding_store_active_slave(struct class_device *cd, const char *buf, size_t count)
1131 int i;
1132 struct slave *slave;
1133 struct slave *old_active = NULL;
1134 struct slave *new_active = NULL;
1135 struct bonding *bond = to_bond(cd);
1137 write_lock_bh(&bond->lock);
1138 if (!USES_PRIMARY(bond->params.mode)) {
1139 printk(KERN_INFO DRV_NAME
1140 ": %s: Unable to change active slave; %s is in mode %d\n",
1141 bond->dev->name, bond->dev->name, bond->params.mode);
1142 } else {
1143 bond_for_each_slave(bond, slave, i) {
1144 if (strnicmp
1145 (slave->dev->name, buf,
1146 strlen(slave->dev->name)) == 0) {
1147 old_active = bond->curr_active_slave;
1148 new_active = slave;
1149 if (new_active == old_active) {
1150 /* do nothing */
1151 printk(KERN_INFO DRV_NAME
1152 ": %s: %s is already the current active slave.\n",
1153 bond->dev->name, slave->dev->name);
1154 goto out;
1156 else {
1157 if ((new_active) &&
1158 (old_active) &&
1159 (new_active->link == BOND_LINK_UP) &&
1160 IS_UP(new_active->dev)) {
1161 printk(KERN_INFO DRV_NAME
1162 ": %s: Setting %s as active slave.\n",
1163 bond->dev->name, slave->dev->name);
1164 bond_change_active_slave(bond, new_active);
1166 else {
1167 printk(KERN_INFO DRV_NAME
1168 ": %s: Could not set %s as active slave; "
1169 "either %s is down or the link is down.\n",
1170 bond->dev->name, slave->dev->name,
1171 slave->dev->name);
1173 goto out;
1178 /* if we got here, then we didn't match the name of any slave */
1180 if (strlen(buf) == 0 || buf[0] == '\n') {
1181 printk(KERN_INFO DRV_NAME
1182 ": %s: Setting active slave to None.\n",
1183 bond->dev->name);
1184 bond->primary_slave = NULL;
1185 bond_select_active_slave(bond);
1186 } else {
1187 printk(KERN_INFO DRV_NAME
1188 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1189 bond->dev->name, (int)strlen(buf) - 1, buf);
1192 out:
1193 write_unlock_bh(&bond->lock);
1194 return count;
1197 static CLASS_DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
1201 * Show link status of the bond interface.
1203 static ssize_t bonding_show_mii_status(struct class_device *cd, char *buf)
1205 struct slave *curr;
1206 struct bonding *bond = to_bond(cd);
1208 read_lock(&bond->curr_slave_lock);
1209 curr = bond->curr_active_slave;
1210 read_unlock(&bond->curr_slave_lock);
1212 return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
1214 static CLASS_DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1218 * Show current 802.3ad aggregator ID.
1220 static ssize_t bonding_show_ad_aggregator(struct class_device *cd, char *buf)
1222 int count = 0;
1223 struct bonding *bond = to_bond(cd);
1225 if (bond->params.mode == BOND_MODE_8023AD) {
1226 struct ad_info ad_info;
1227 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
1229 else
1230 count = sprintf(buf, "\n") + 1;
1232 return count;
1234 static CLASS_DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1238 * Show number of active 802.3ad ports.
1240 static ssize_t bonding_show_ad_num_ports(struct class_device *cd, char *buf)
1242 int count = 0;
1243 struct bonding *bond = to_bond(cd);
1245 if (bond->params.mode == BOND_MODE_8023AD) {
1246 struct ad_info ad_info;
1247 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
1249 else
1250 count = sprintf(buf, "\n") + 1;
1252 return count;
1254 static CLASS_DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1258 * Show current 802.3ad actor key.
1260 static ssize_t bonding_show_ad_actor_key(struct class_device *cd, char *buf)
1262 int count = 0;
1263 struct bonding *bond = to_bond(cd);
1265 if (bond->params.mode == BOND_MODE_8023AD) {
1266 struct ad_info ad_info;
1267 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
1269 else
1270 count = sprintf(buf, "\n") + 1;
1272 return count;
1274 static CLASS_DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1278 * Show current 802.3ad partner key.
1280 static ssize_t bonding_show_ad_partner_key(struct class_device *cd, char *buf)
1282 int count = 0;
1283 struct bonding *bond = to_bond(cd);
1285 if (bond->params.mode == BOND_MODE_8023AD) {
1286 struct ad_info ad_info;
1287 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
1289 else
1290 count = sprintf(buf, "\n") + 1;
1292 return count;
1294 static CLASS_DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1298 * Show current 802.3ad partner mac.
1300 static ssize_t bonding_show_ad_partner_mac(struct class_device *cd, char *buf)
1302 int count = 0;
1303 struct bonding *bond = to_bond(cd);
1305 if (bond->params.mode == BOND_MODE_8023AD) {
1306 struct ad_info ad_info;
1307 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1308 count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
1309 ad_info.partner_system[0],
1310 ad_info.partner_system[1],
1311 ad_info.partner_system[2],
1312 ad_info.partner_system[3],
1313 ad_info.partner_system[4],
1314 ad_info.partner_system[5]) + 1;
1317 else
1318 count = sprintf(buf, "\n") + 1;
1320 return count;
1322 static CLASS_DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1326 static struct attribute *per_bond_attrs[] = {
1327 &class_device_attr_slaves.attr,
1328 &class_device_attr_mode.attr,
1329 &class_device_attr_arp_validate.attr,
1330 &class_device_attr_arp_interval.attr,
1331 &class_device_attr_arp_ip_target.attr,
1332 &class_device_attr_downdelay.attr,
1333 &class_device_attr_updelay.attr,
1334 &class_device_attr_lacp_rate.attr,
1335 &class_device_attr_xmit_hash_policy.attr,
1336 &class_device_attr_miimon.attr,
1337 &class_device_attr_primary.attr,
1338 &class_device_attr_use_carrier.attr,
1339 &class_device_attr_active_slave.attr,
1340 &class_device_attr_mii_status.attr,
1341 &class_device_attr_ad_aggregator.attr,
1342 &class_device_attr_ad_num_ports.attr,
1343 &class_device_attr_ad_actor_key.attr,
1344 &class_device_attr_ad_partner_key.attr,
1345 &class_device_attr_ad_partner_mac.attr,
1346 NULL,
1349 static struct attribute_group bonding_group = {
1350 .name = "bonding",
1351 .attrs = per_bond_attrs,
1355 * Initialize sysfs. This sets up the bonding_masters file in
1356 * /sys/class/net.
1358 int bond_create_sysfs(void)
1360 int ret = 0;
1361 struct bonding *firstbond;
1363 init_rwsem(&bonding_rwsem);
1365 /* get the netdev class pointer */
1366 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1367 if (!firstbond)
1368 return -ENODEV;
1370 netdev_class = firstbond->dev->class_dev.class;
1371 if (!netdev_class)
1372 return -ENODEV;
1374 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
1376 return ret;
1381 * Remove /sys/class/net/bonding_masters.
1383 void bond_destroy_sysfs(void)
1385 if (netdev_class)
1386 class_remove_file(netdev_class, &class_attr_bonding_masters);
1390 * Initialize sysfs for each bond. This sets up and registers
1391 * the 'bondctl' directory for each individual bond under /sys/class/net.
1393 int bond_create_sysfs_entry(struct bonding *bond)
1395 struct net_device *dev = bond->dev;
1396 int err;
1398 err = sysfs_create_group(&(dev->class_dev.kobj), &bonding_group);
1399 if (err) {
1400 printk(KERN_EMERG "eek! didn't create group!\n");
1403 if (expected_refcount < 1)
1404 expected_refcount = atomic_read(&bond->dev->class_dev.kobj.kref.refcount);
1406 return err;
1409 * Remove sysfs entries for each bond.
1411 void bond_destroy_sysfs_entry(struct bonding *bond)
1413 struct net_device *dev = bond->dev;
1415 sysfs_remove_group(&(dev->class_dev.kobj), &bonding_group);