blob: 60cccf2aa9594e5c98eefd4e93622fdf5257bec9 [file] [log] [blame]
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001
2/*
3 * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4 *
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.
9 *
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.
14 *
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.
18 *
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
21 *
Mitch Williamsb76cdba2005-11-09 10:36:41 -080022 */
Mitch Williamsb76cdba2005-11-09 10:36:41 -080023#include <linux/kernel.h>
24#include <linux/module.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080025#include <linux/device.h>
26#include <linux/sysdev.h>
27#include <linux/fs.h>
28#include <linux/types.h>
29#include <linux/string.h>
30#include <linux/netdevice.h>
31#include <linux/inetdevice.h>
32#include <linux/in.h>
33#include <linux/sysfs.h>
34#include <linux/string.h>
35#include <linux/ctype.h>
36#include <linux/inet.h>
37#include <linux/rtnetlink.h>
38
39/* #define BONDING_DEBUG 1 */
40#include "bonding.h"
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -070041#define to_dev(obj) container_of(obj,struct device,kobj)
Mitch Williamsb76cdba2005-11-09 10:36:41 -080042#define to_bond(cd) ((struct bonding *)(to_net_dev(cd)->priv))
43
44/*---------------------------- Declarations -------------------------------*/
45
46
47extern struct list_head bond_dev_list;
48extern struct bond_params bonding_defaults;
49extern struct bond_parm_tbl bond_mode_tbl[];
50extern struct bond_parm_tbl bond_lacp_tbl[];
51extern struct bond_parm_tbl xmit_hashtype_tbl[];
Jay Vosburghf5b2b962006-09-22 21:54:53 -070052extern struct bond_parm_tbl arp_validate_tbl[];
Mitch Williamsb76cdba2005-11-09 10:36:41 -080053
54static int expected_refcount = -1;
55static struct class *netdev_class;
56/*--------------------------- Data Structures -----------------------------*/
57
58/* Bonding sysfs lock. Why can't we just use the subsytem lock?
59 * Because kobject_register tries to acquire the subsystem lock. If
60 * we already hold the lock (which we would if the user was creating
61 * a new bond through the sysfs interface), we deadlock.
62 * This lock is only needed when deleting a bond - we need to make sure
63 * that we don't collide with an ongoing ioctl.
64 */
65
66struct rw_semaphore bonding_rwsem;
67
68
69
70
71/*------------------------------ Functions --------------------------------*/
72
73/*
74 * "show" function for the bond_masters attribute.
75 * The class parameter is ignored.
76 */
77static ssize_t bonding_show_bonds(struct class *cls, char *buffer)
78{
79 int res = 0;
80 struct bonding *bond;
81
82 down_read(&(bonding_rwsem));
83
84 list_for_each_entry(bond, &bond_dev_list, bond_list) {
85 if (res > (PAGE_SIZE - IFNAMSIZ)) {
86 /* not enough space for another interface name */
87 if ((PAGE_SIZE - res) > 10)
88 res = PAGE_SIZE - 10;
89 res += sprintf(buffer + res, "++more++");
90 break;
91 }
92 res += sprintf(buffer + res, "%s ",
93 bond->dev->name);
94 }
95 res += sprintf(buffer + res, "\n");
96 res++;
97 up_read(&(bonding_rwsem));
98 return res;
99}
100
101/*
102 * "store" function for the bond_masters attribute. This is what
103 * creates and deletes entire bonds.
104 *
105 * The class parameter is ignored.
106 *
107 */
108
109static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
110{
111 char command[IFNAMSIZ + 1] = {0, };
112 char *ifname;
113 int res = count;
114 struct bonding *bond;
115 struct bonding *nxt;
116
117 down_write(&(bonding_rwsem));
118 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
119 ifname = command + 1;
120 if ((strlen(command) <= 1) ||
121 !dev_valid_name(ifname))
122 goto err_no_cmd;
123
124 if (command[0] == '+') {
125
126 /* Check to see if the bond already exists. */
127 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
128 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
129 printk(KERN_ERR DRV_NAME
130 ": cannot add bond %s; it already exists\n",
131 ifname);
132 res = -EPERM;
133 goto out;
134 }
135
136 printk(KERN_INFO DRV_NAME
137 ": %s is being created...\n", ifname);
138 if (bond_create(ifname, &bonding_defaults, &bond)) {
139 printk(KERN_INFO DRV_NAME
140 ": %s interface already exists. Bond creation failed.\n",
141 ifname);
142 res = -EPERM;
143 }
144 goto out;
145 }
146
147 if (command[0] == '-') {
148 list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
149 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
150 rtnl_lock();
151 /* check the ref count on the bond's kobject.
152 * If it's > expected, then there's a file open,
153 * and we have to fail.
154 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700155 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800156 > expected_refcount){
157 rtnl_unlock();
158 printk(KERN_INFO DRV_NAME
159 ": Unable remove bond %s due to open references.\n",
160 ifname);
161 res = -EPERM;
162 goto out;
163 }
164 printk(KERN_INFO DRV_NAME
165 ": %s is being deleted...\n",
166 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800167 bond_deinit(bond->dev);
168 bond_destroy_sysfs_entry(bond);
Jay Vosburgh3201e652007-06-19 11:12:12 -0700169 unregister_netdevice(bond->dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800170 rtnl_unlock();
171 goto out;
172 }
173
174 printk(KERN_ERR DRV_NAME
175 ": unable to delete non-existent bond %s\n", ifname);
176 res = -ENODEV;
177 goto out;
178 }
179
180err_no_cmd:
181 printk(KERN_ERR DRV_NAME
182 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
183 res = -EPERM;
184
185 /* Always return either count or an error. If you return 0, you'll
186 * get called forever, which is bad.
187 */
188out:
189 up_write(&(bonding_rwsem));
190 return res;
191}
192/* class attribute for bond_masters file. This ends up in /sys/class/net */
193static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
194 bonding_show_bonds, bonding_store_bonds);
195
196int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
197{
198 char linkname[IFNAMSIZ+7];
199 int ret = 0;
200
201 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700202 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800203 "master");
204 if (ret)
205 return ret;
206 /* next, create a link from the master to the slave */
207 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700208 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800209 linkname);
210 return ret;
211
212}
213
214void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
215{
216 char linkname[IFNAMSIZ+7];
217
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700218 sysfs_remove_link(&(slave->dev.kobj), "master");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700220 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800221}
222
223
224/*
225 * Show the slaves in the current bond.
226 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700227static ssize_t bonding_show_slaves(struct device *d,
228 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800229{
230 struct slave *slave;
231 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700232 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800233
234 read_lock_bh(&bond->lock);
235 bond_for_each_slave(bond, slave, i) {
236 if (res > (PAGE_SIZE - IFNAMSIZ)) {
237 /* not enough space for another interface name */
238 if ((PAGE_SIZE - res) > 10)
239 res = PAGE_SIZE - 10;
240 res += sprintf(buf + res, "++more++");
241 break;
242 }
243 res += sprintf(buf + res, "%s ", slave->dev->name);
244 }
245 read_unlock_bh(&bond->lock);
246 res += sprintf(buf + res, "\n");
247 res++;
248 return res;
249}
250
251/*
252 * Set the slaves in the current bond. The bond interface must be
253 * up for this to succeed.
254 * This function is largely the same flow as bonding_update_bonds().
255 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700256static ssize_t bonding_store_slaves(struct device *d,
257 struct device_attribute *attr,
258 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800259{
260 char command[IFNAMSIZ + 1] = { 0, };
261 char *ifname;
262 int i, res, found, ret = count;
263 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800264 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700265 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800266
267 /* Quick sanity check -- is the bond interface up? */
268 if (!(bond->dev->flags & IFF_UP)) {
269 printk(KERN_ERR DRV_NAME
270 ": %s: Unable to update slaves because interface is down.\n",
271 bond->dev->name);
272 ret = -EPERM;
273 goto out;
274 }
275
276 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
277
278 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
279 ifname = command + 1;
280 if ((strlen(command) <= 1) ||
281 !dev_valid_name(ifname))
282 goto err_no_cmd;
283
284 if (command[0] == '+') {
285
286 /* Got a slave name in ifname. Is it already in the list? */
287 found = 0;
288 read_lock_bh(&bond->lock);
289 bond_for_each_slave(bond, slave, i)
290 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
291 printk(KERN_ERR DRV_NAME
292 ": %s: Interface %s is already enslaved!\n",
293 bond->dev->name, ifname);
294 ret = -EPERM;
295 read_unlock_bh(&bond->lock);
296 goto out;
297 }
298
299 read_unlock_bh(&bond->lock);
300 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
301 bond->dev->name, ifname);
302 dev = dev_get_by_name(ifname);
303 if (!dev) {
304 printk(KERN_INFO DRV_NAME
305 ": %s: Interface %s does not exist!\n",
306 bond->dev->name, ifname);
307 ret = -EPERM;
308 goto out;
309 }
310 else
311 dev_put(dev);
312
313 if (dev->flags & IFF_UP) {
314 printk(KERN_ERR DRV_NAME
315 ": %s: Error: Unable to enslave %s "
316 "because it is already up.\n",
317 bond->dev->name, dev->name);
318 ret = -EPERM;
319 goto out;
320 }
321 /* If this is the first slave, then we need to set
322 the master's hardware address to be the same as the
323 slave's. */
324 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
325 memcpy(bond->dev->dev_addr, dev->dev_addr,
326 dev->addr_len);
327 }
328
329 /* Set the slave's MTU to match the bond */
330 if (dev->mtu != bond->dev->mtu) {
331 if (dev->change_mtu) {
332 res = dev->change_mtu(dev,
333 bond->dev->mtu);
334 if (res) {
335 ret = res;
336 goto out;
337 }
338 } else {
339 dev->mtu = bond->dev->mtu;
340 }
341 }
342 rtnl_lock();
343 res = bond_enslave(bond->dev, dev);
344 rtnl_unlock();
345 if (res) {
346 ret = res;
347 }
348 goto out;
349 }
350
351 if (command[0] == '-') {
352 dev = NULL;
353 bond_for_each_slave(bond, slave, i)
354 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
355 dev = slave->dev;
356 break;
357 }
358 if (dev) {
359 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
360 bond->dev->name, dev->name);
361 rtnl_lock();
362 res = bond_release(bond->dev, dev);
363 rtnl_unlock();
364 if (res) {
365 ret = res;
366 goto out;
367 }
368 /* set the slave MTU to the default */
369 if (dev->change_mtu) {
370 dev->change_mtu(dev, 1500);
371 } else {
372 dev->mtu = 1500;
373 }
374 }
375 else {
376 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
377 ifname, bond->dev->name);
378 ret = -ENODEV;
379 }
380 goto out;
381 }
382
383err_no_cmd:
384 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
385 ret = -EPERM;
386
387out:
388 return ret;
389}
390
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700391static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800392
393/*
394 * Show and set the bonding mode. The bond interface must be down to
395 * change the mode.
396 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700397static ssize_t bonding_show_mode(struct device *d,
398 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800399{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700400 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800401
402 return sprintf(buf, "%s %d\n",
403 bond_mode_tbl[bond->params.mode].modename,
404 bond->params.mode) + 1;
405}
406
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700407static ssize_t bonding_store_mode(struct device *d,
408 struct device_attribute *attr,
409 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800410{
411 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700412 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800413
414 if (bond->dev->flags & IFF_UP) {
415 printk(KERN_ERR DRV_NAME
416 ": unable to update mode of %s because interface is up.\n",
417 bond->dev->name);
418 ret = -EPERM;
419 goto out;
420 }
421
422 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
423 if (new_value < 0) {
424 printk(KERN_ERR DRV_NAME
425 ": %s: Ignoring invalid mode value %.*s.\n",
426 bond->dev->name,
427 (int)strlen(buf) - 1, buf);
428 ret = -EINVAL;
429 goto out;
430 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800431 if (bond->params.mode == BOND_MODE_8023AD)
432 bond_unset_master_3ad_flags(bond);
433
434 if (bond->params.mode == BOND_MODE_ALB)
435 bond_unset_master_alb_flags(bond);
436
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800437 bond->params.mode = new_value;
438 bond_set_mode_ops(bond, bond->params.mode);
439 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
440 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
441 }
442out:
443 return ret;
444}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700445static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800446
447/*
448 * Show and set the bonding transmit hash method. The bond interface must be down to
449 * change the xmit hash policy.
450 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700451static ssize_t bonding_show_xmit_hash(struct device *d,
452 struct device_attribute *attr,
453 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800454{
455 int count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700456 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800457
458 if ((bond->params.mode != BOND_MODE_XOR) &&
459 (bond->params.mode != BOND_MODE_8023AD)) {
460 // Not Applicable
461 count = sprintf(buf, "NA\n") + 1;
462 } else {
463 count = sprintf(buf, "%s %d\n",
464 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
465 bond->params.xmit_policy) + 1;
466 }
467
468 return count;
469}
470
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700471static ssize_t bonding_store_xmit_hash(struct device *d,
472 struct device_attribute *attr,
473 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800474{
475 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700476 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800477
478 if (bond->dev->flags & IFF_UP) {
479 printk(KERN_ERR DRV_NAME
480 "%s: Interface is up. Unable to update xmit policy.\n",
481 bond->dev->name);
482 ret = -EPERM;
483 goto out;
484 }
485
486 if ((bond->params.mode != BOND_MODE_XOR) &&
487 (bond->params.mode != BOND_MODE_8023AD)) {
488 printk(KERN_ERR DRV_NAME
489 "%s: Transmit hash policy is irrelevant in this mode.\n",
490 bond->dev->name);
491 ret = -EPERM;
492 goto out;
493 }
494
495 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
496 if (new_value < 0) {
497 printk(KERN_ERR DRV_NAME
498 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
499 bond->dev->name,
500 (int)strlen(buf) - 1, buf);
501 ret = -EINVAL;
502 goto out;
503 } else {
504 bond->params.xmit_policy = new_value;
505 bond_set_mode_ops(bond, bond->params.mode);
506 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
507 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
508 }
509out:
510 return ret;
511}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700512static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800513
514/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700515 * Show and set arp_validate.
516 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700517static ssize_t bonding_show_arp_validate(struct device *d,
518 struct device_attribute *attr,
519 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700520{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700521 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700522
523 return sprintf(buf, "%s %d\n",
524 arp_validate_tbl[bond->params.arp_validate].modename,
525 bond->params.arp_validate) + 1;
526}
527
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700528static ssize_t bonding_store_arp_validate(struct device *d,
529 struct device_attribute *attr,
530 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700531{
532 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700533 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700534
535 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
536 if (new_value < 0) {
537 printk(KERN_ERR DRV_NAME
538 ": %s: Ignoring invalid arp_validate value %s\n",
539 bond->dev->name, buf);
540 return -EINVAL;
541 }
542 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
543 printk(KERN_ERR DRV_NAME
544 ": %s: arp_validate only supported in active-backup mode.\n",
545 bond->dev->name);
546 return -EINVAL;
547 }
548 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
549 bond->dev->name, arp_validate_tbl[new_value].modename,
550 new_value);
551
552 if (!bond->params.arp_validate && new_value) {
553 bond_register_arp(bond);
554 } else if (bond->params.arp_validate && !new_value) {
555 bond_unregister_arp(bond);
556 }
557
558 bond->params.arp_validate = new_value;
559
560 return count;
561}
562
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700563static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700564
565/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800566 * Show and set the arp timer interval. There are two tricky bits
567 * here. First, if ARP monitoring is activated, then we must disable
568 * MII monitoring. Second, if the ARP timer isn't running, we must
569 * start it.
570 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700571static ssize_t bonding_show_arp_interval(struct device *d,
572 struct device_attribute *attr,
573 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800574{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700575 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800576
577 return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
578}
579
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700580static ssize_t bonding_store_arp_interval(struct device *d,
581 struct device_attribute *attr,
582 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800583{
584 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700585 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800586
587 if (sscanf(buf, "%d", &new_value) != 1) {
588 printk(KERN_ERR DRV_NAME
589 ": %s: no arp_interval value specified.\n",
590 bond->dev->name);
591 ret = -EINVAL;
592 goto out;
593 }
594 if (new_value < 0) {
595 printk(KERN_ERR DRV_NAME
596 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
597 bond->dev->name, new_value, INT_MAX);
598 ret = -EINVAL;
599 goto out;
600 }
601
602 printk(KERN_INFO DRV_NAME
603 ": %s: Setting ARP monitoring interval to %d.\n",
604 bond->dev->name, new_value);
605 bond->params.arp_interval = new_value;
606 if (bond->params.miimon) {
607 printk(KERN_INFO DRV_NAME
608 ": %s: ARP monitoring cannot be used with MII monitoring. "
609 "%s Disabling MII monitoring.\n",
610 bond->dev->name, bond->dev->name);
611 bond->params.miimon = 0;
612 /* Kill MII timer, else it brings bond's link down */
613 if (bond->arp_timer.function) {
614 printk(KERN_INFO DRV_NAME
615 ": %s: Kill MII timer, else it brings bond's link down...\n",
616 bond->dev->name);
617 del_timer_sync(&bond->mii_timer);
618 }
619 }
620 if (!bond->params.arp_targets[0]) {
621 printk(KERN_INFO DRV_NAME
622 ": %s: ARP monitoring has been set up, "
623 "but no ARP targets have been specified.\n",
624 bond->dev->name);
625 }
626 if (bond->dev->flags & IFF_UP) {
627 /* If the interface is up, we may need to fire off
628 * the ARP timer. If the interface is down, the
629 * timer will get fired off when the open function
630 * is called.
631 */
632 if (bond->arp_timer.function) {
633 /* The timer's already set up, so fire it off */
634 mod_timer(&bond->arp_timer, jiffies + 1);
635 } else {
636 /* Set up the timer. */
637 init_timer(&bond->arp_timer);
638 bond->arp_timer.expires = jiffies + 1;
639 bond->arp_timer.data =
640 (unsigned long) bond->dev;
641 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
642 bond->arp_timer.function =
643 (void *)
644 &bond_activebackup_arp_mon;
645 } else {
646 bond->arp_timer.function =
647 (void *)
648 &bond_loadbalance_arp_mon;
649 }
650 add_timer(&bond->arp_timer);
651 }
652 }
653
654out:
655 return ret;
656}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700657static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800658
659/*
660 * Show and set the arp targets.
661 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700662static ssize_t bonding_show_arp_targets(struct device *d,
663 struct device_attribute *attr,
664 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800665{
666 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700667 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800668
669 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
670 if (bond->params.arp_targets[i])
671 res += sprintf(buf + res, "%u.%u.%u.%u ",
672 NIPQUAD(bond->params.arp_targets[i]));
673 }
674 if (res)
675 res--; /* eat the leftover space */
676 res += sprintf(buf + res, "\n");
677 res++;
678 return res;
679}
680
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700681static ssize_t bonding_store_arp_targets(struct device *d,
682 struct device_attribute *attr,
683 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800684{
685 u32 newtarget;
686 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700687 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800688 u32 *targets;
689
690 targets = bond->params.arp_targets;
691 newtarget = in_aton(buf + 1);
692 /* look for adds */
693 if (buf[0] == '+') {
694 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
695 printk(KERN_ERR DRV_NAME
696 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
697 bond->dev->name, NIPQUAD(newtarget));
698 ret = -EINVAL;
699 goto out;
700 }
701 /* look for an empty slot to put the target in, and check for dupes */
702 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
703 if (targets[i] == newtarget) { /* duplicate */
704 printk(KERN_ERR DRV_NAME
705 ": %s: ARP target %u.%u.%u.%u is already present\n",
706 bond->dev->name, NIPQUAD(newtarget));
707 if (done)
708 targets[i] = 0;
709 ret = -EINVAL;
710 goto out;
711 }
712 if (targets[i] == 0 && !done) {
713 printk(KERN_INFO DRV_NAME
714 ": %s: adding ARP target %d.%d.%d.%d.\n",
715 bond->dev->name, NIPQUAD(newtarget));
716 done = 1;
717 targets[i] = newtarget;
718 }
719 }
720 if (!done) {
721 printk(KERN_ERR DRV_NAME
722 ": %s: ARP target table is full!\n",
723 bond->dev->name);
724 ret = -EINVAL;
725 goto out;
726 }
727
728 }
729 else if (buf[0] == '-') {
730 if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
731 printk(KERN_ERR DRV_NAME
732 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
733 bond->dev->name, NIPQUAD(newtarget));
734 ret = -EINVAL;
735 goto out;
736 }
737
738 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
739 if (targets[i] == newtarget) {
740 printk(KERN_INFO DRV_NAME
741 ": %s: removing ARP target %d.%d.%d.%d.\n",
742 bond->dev->name, NIPQUAD(newtarget));
743 targets[i] = 0;
744 done = 1;
745 }
746 }
747 if (!done) {
748 printk(KERN_INFO DRV_NAME
749 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
750 bond->dev->name, NIPQUAD(newtarget));
751 ret = -EINVAL;
752 goto out;
753 }
754 }
755 else {
756 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
757 bond->dev->name);
758 ret = -EPERM;
759 goto out;
760 }
761
762out:
763 return ret;
764}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700765static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800766
767/*
768 * Show and set the up and down delays. These must be multiples of the
769 * MII monitoring value, and are stored internally as the multiplier.
770 * Thus, we must translate to MS for the real world.
771 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700772static ssize_t bonding_show_downdelay(struct device *d,
773 struct device_attribute *attr,
774 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800775{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700776 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800777
778 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
779}
780
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700781static ssize_t bonding_store_downdelay(struct device *d,
782 struct device_attribute *attr,
783 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800784{
785 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700786 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800787
788 if (!(bond->params.miimon)) {
789 printk(KERN_ERR DRV_NAME
790 ": %s: Unable to set down delay as MII monitoring is disabled\n",
791 bond->dev->name);
792 ret = -EPERM;
793 goto out;
794 }
795
796 if (sscanf(buf, "%d", &new_value) != 1) {
797 printk(KERN_ERR DRV_NAME
798 ": %s: no down delay value specified.\n",
799 bond->dev->name);
800 ret = -EINVAL;
801 goto out;
802 }
803 if (new_value < 0) {
804 printk(KERN_ERR DRV_NAME
805 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
806 bond->dev->name, new_value, 1, INT_MAX);
807 ret = -EINVAL;
808 goto out;
809 } else {
810 if ((new_value % bond->params.miimon) != 0) {
811 printk(KERN_WARNING DRV_NAME
812 ": %s: Warning: down delay (%d) is not a multiple "
813 "of miimon (%d), delay rounded to %d ms\n",
814 bond->dev->name, new_value, bond->params.miimon,
815 (new_value / bond->params.miimon) *
816 bond->params.miimon);
817 }
818 bond->params.downdelay = new_value / bond->params.miimon;
819 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
820 bond->dev->name, bond->params.downdelay * bond->params.miimon);
821
822 }
823
824out:
825 return ret;
826}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700827static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800828
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700829static ssize_t bonding_show_updelay(struct device *d,
830 struct device_attribute *attr,
831 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800832{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700833 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800834
835 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
836
837}
838
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700839static ssize_t bonding_store_updelay(struct device *d,
840 struct device_attribute *attr,
841 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800842{
843 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700844 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800845
846 if (!(bond->params.miimon)) {
847 printk(KERN_ERR DRV_NAME
848 ": %s: Unable to set up delay as MII monitoring is disabled\n",
849 bond->dev->name);
850 ret = -EPERM;
851 goto out;
852 }
853
854 if (sscanf(buf, "%d", &new_value) != 1) {
855 printk(KERN_ERR DRV_NAME
856 ": %s: no up delay value specified.\n",
857 bond->dev->name);
858 ret = -EINVAL;
859 goto out;
860 }
861 if (new_value < 0) {
862 printk(KERN_ERR DRV_NAME
863 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
864 bond->dev->name, new_value, 1, INT_MAX);
865 ret = -EINVAL;
866 goto out;
867 } else {
868 if ((new_value % bond->params.miimon) != 0) {
869 printk(KERN_WARNING DRV_NAME
870 ": %s: Warning: up delay (%d) is not a multiple "
871 "of miimon (%d), updelay rounded to %d ms\n",
872 bond->dev->name, new_value, bond->params.miimon,
873 (new_value / bond->params.miimon) *
874 bond->params.miimon);
875 }
876 bond->params.updelay = new_value / bond->params.miimon;
877 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
878 bond->dev->name, bond->params.updelay * bond->params.miimon);
879
880 }
881
882out:
883 return ret;
884}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700885static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800886
887/*
888 * Show and set the LACP interval. Interface must be down, and the mode
889 * must be set to 802.3ad mode.
890 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700891static ssize_t bonding_show_lacp(struct device *d,
892 struct device_attribute *attr,
893 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800894{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700895 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800896
897 return sprintf(buf, "%s %d\n",
898 bond_lacp_tbl[bond->params.lacp_fast].modename,
899 bond->params.lacp_fast) + 1;
900}
901
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700902static ssize_t bonding_store_lacp(struct device *d,
903 struct device_attribute *attr,
904 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800905{
906 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700907 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800908
909 if (bond->dev->flags & IFF_UP) {
910 printk(KERN_ERR DRV_NAME
911 ": %s: Unable to update LACP rate because interface is up.\n",
912 bond->dev->name);
913 ret = -EPERM;
914 goto out;
915 }
916
917 if (bond->params.mode != BOND_MODE_8023AD) {
918 printk(KERN_ERR DRV_NAME
919 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
920 bond->dev->name);
921 ret = -EPERM;
922 goto out;
923 }
924
925 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
926
927 if ((new_value == 1) || (new_value == 0)) {
928 bond->params.lacp_fast = new_value;
929 printk(KERN_INFO DRV_NAME
930 ": %s: Setting LACP rate to %s (%d).\n",
931 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
932 } else {
933 printk(KERN_ERR DRV_NAME
934 ": %s: Ignoring invalid LACP rate value %.*s.\n",
935 bond->dev->name, (int)strlen(buf) - 1, buf);
936 ret = -EINVAL;
937 }
938out:
939 return ret;
940}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700941static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800942
943/*
944 * Show and set the MII monitor interval. There are two tricky bits
945 * here. First, if MII monitoring is activated, then we must disable
946 * ARP monitoring. Second, if the timer isn't running, we must
947 * start it.
948 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700949static ssize_t bonding_show_miimon(struct device *d,
950 struct device_attribute *attr,
951 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800952{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700953 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800954
955 return sprintf(buf, "%d\n", bond->params.miimon) + 1;
956}
957
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700958static ssize_t bonding_store_miimon(struct device *d,
959 struct device_attribute *attr,
960 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800961{
962 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700963 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800964
965 if (sscanf(buf, "%d", &new_value) != 1) {
966 printk(KERN_ERR DRV_NAME
967 ": %s: no miimon value specified.\n",
968 bond->dev->name);
969 ret = -EINVAL;
970 goto out;
971 }
972 if (new_value < 0) {
973 printk(KERN_ERR DRV_NAME
974 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
975 bond->dev->name, new_value, 1, INT_MAX);
976 ret = -EINVAL;
977 goto out;
978 } else {
979 printk(KERN_INFO DRV_NAME
980 ": %s: Setting MII monitoring interval to %d.\n",
981 bond->dev->name, new_value);
982 bond->params.miimon = new_value;
983 if(bond->params.updelay)
984 printk(KERN_INFO DRV_NAME
985 ": %s: Note: Updating updelay (to %d) "
986 "since it is a multiple of the miimon value.\n",
987 bond->dev->name,
988 bond->params.updelay * bond->params.miimon);
989 if(bond->params.downdelay)
990 printk(KERN_INFO DRV_NAME
991 ": %s: Note: Updating downdelay (to %d) "
992 "since it is a multiple of the miimon value.\n",
993 bond->dev->name,
994 bond->params.downdelay * bond->params.miimon);
995 if (bond->params.arp_interval) {
996 printk(KERN_INFO DRV_NAME
997 ": %s: MII monitoring cannot be used with "
998 "ARP monitoring. Disabling ARP monitoring...\n",
999 bond->dev->name);
1000 bond->params.arp_interval = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001001 if (bond->params.arp_validate) {
1002 bond_unregister_arp(bond);
1003 bond->params.arp_validate =
1004 BOND_ARP_VALIDATE_NONE;
1005 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001006 /* Kill ARP timer, else it brings bond's link down */
1007 if (bond->mii_timer.function) {
1008 printk(KERN_INFO DRV_NAME
1009 ": %s: Kill ARP timer, else it brings bond's link down...\n",
1010 bond->dev->name);
1011 del_timer_sync(&bond->arp_timer);
1012 }
1013 }
1014
1015 if (bond->dev->flags & IFF_UP) {
1016 /* If the interface is up, we may need to fire off
1017 * the MII timer. If the interface is down, the
1018 * timer will get fired off when the open function
1019 * is called.
1020 */
1021 if (bond->mii_timer.function) {
1022 /* The timer's already set up, so fire it off */
1023 mod_timer(&bond->mii_timer, jiffies + 1);
1024 } else {
1025 /* Set up the timer. */
1026 init_timer(&bond->mii_timer);
1027 bond->mii_timer.expires = jiffies + 1;
1028 bond->mii_timer.data =
1029 (unsigned long) bond->dev;
1030 bond->mii_timer.function =
1031 (void *) &bond_mii_monitor;
1032 add_timer(&bond->mii_timer);
1033 }
1034 }
1035 }
1036out:
1037 return ret;
1038}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001039static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001040
1041/*
1042 * Show and set the primary slave. The store function is much
1043 * simpler than bonding_store_slaves function because it only needs to
1044 * handle one interface name.
1045 * The bond must be a mode that supports a primary for this be
1046 * set.
1047 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001048static ssize_t bonding_show_primary(struct device *d,
1049 struct device_attribute *attr,
1050 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001051{
1052 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001053 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001054
1055 if (bond->primary_slave)
1056 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
1057 else
1058 count = sprintf(buf, "\n") + 1;
1059
1060 return count;
1061}
1062
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001063static ssize_t bonding_store_primary(struct device *d,
1064 struct device_attribute *attr,
1065 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001066{
1067 int i;
1068 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001069 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001070
1071 write_lock_bh(&bond->lock);
1072 if (!USES_PRIMARY(bond->params.mode)) {
1073 printk(KERN_INFO DRV_NAME
1074 ": %s: Unable to set primary slave; %s is in mode %d\n",
1075 bond->dev->name, bond->dev->name, bond->params.mode);
1076 } else {
1077 bond_for_each_slave(bond, slave, i) {
1078 if (strnicmp
1079 (slave->dev->name, buf,
1080 strlen(slave->dev->name)) == 0) {
1081 printk(KERN_INFO DRV_NAME
1082 ": %s: Setting %s as primary slave.\n",
1083 bond->dev->name, slave->dev->name);
1084 bond->primary_slave = slave;
1085 bond_select_active_slave(bond);
1086 goto out;
1087 }
1088 }
1089
1090 /* if we got here, then we didn't match the name of any slave */
1091
1092 if (strlen(buf) == 0 || buf[0] == '\n') {
1093 printk(KERN_INFO DRV_NAME
1094 ": %s: Setting primary slave to None.\n",
1095 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001096 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001097 bond_select_active_slave(bond);
1098 } else {
1099 printk(KERN_INFO DRV_NAME
1100 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1101 bond->dev->name, (int)strlen(buf) - 1, buf);
1102 }
1103 }
1104out:
1105 write_unlock_bh(&bond->lock);
1106 return count;
1107}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001108static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001109
1110/*
1111 * Show and set the use_carrier flag.
1112 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001113static ssize_t bonding_show_carrier(struct device *d,
1114 struct device_attribute *attr,
1115 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001116{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001117 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001118
1119 return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
1120}
1121
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001122static ssize_t bonding_store_carrier(struct device *d,
1123 struct device_attribute *attr,
1124 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001125{
1126 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001127 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001128
1129
1130 if (sscanf(buf, "%d", &new_value) != 1) {
1131 printk(KERN_ERR DRV_NAME
1132 ": %s: no use_carrier value specified.\n",
1133 bond->dev->name);
1134 ret = -EINVAL;
1135 goto out;
1136 }
1137 if ((new_value == 0) || (new_value == 1)) {
1138 bond->params.use_carrier = new_value;
1139 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1140 bond->dev->name, new_value);
1141 } else {
1142 printk(KERN_INFO DRV_NAME
1143 ": %s: Ignoring invalid use_carrier value %d.\n",
1144 bond->dev->name, new_value);
1145 }
1146out:
1147 return count;
1148}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001149static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001150
1151
1152/*
1153 * Show and set currently active_slave.
1154 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001155static ssize_t bonding_show_active_slave(struct device *d,
1156 struct device_attribute *attr,
1157 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001158{
1159 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001160 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001161 int count;
1162
1163
1164 read_lock(&bond->curr_slave_lock);
1165 curr = bond->curr_active_slave;
1166 read_unlock(&bond->curr_slave_lock);
1167
1168 if (USES_PRIMARY(bond->params.mode) && curr)
1169 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
1170 else
1171 count = sprintf(buf, "\n") + 1;
1172 return count;
1173}
1174
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001175static ssize_t bonding_store_active_slave(struct device *d,
1176 struct device_attribute *attr,
1177 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001178{
1179 int i;
1180 struct slave *slave;
1181 struct slave *old_active = NULL;
1182 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001183 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001184
1185 write_lock_bh(&bond->lock);
1186 if (!USES_PRIMARY(bond->params.mode)) {
1187 printk(KERN_INFO DRV_NAME
1188 ": %s: Unable to change active slave; %s is in mode %d\n",
1189 bond->dev->name, bond->dev->name, bond->params.mode);
1190 } else {
1191 bond_for_each_slave(bond, slave, i) {
1192 if (strnicmp
1193 (slave->dev->name, buf,
1194 strlen(slave->dev->name)) == 0) {
1195 old_active = bond->curr_active_slave;
1196 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001197 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001198 /* do nothing */
1199 printk(KERN_INFO DRV_NAME
1200 ": %s: %s is already the current active slave.\n",
1201 bond->dev->name, slave->dev->name);
1202 goto out;
1203 }
1204 else {
1205 if ((new_active) &&
1206 (old_active) &&
1207 (new_active->link == BOND_LINK_UP) &&
1208 IS_UP(new_active->dev)) {
1209 printk(KERN_INFO DRV_NAME
1210 ": %s: Setting %s as active slave.\n",
1211 bond->dev->name, slave->dev->name);
1212 bond_change_active_slave(bond, new_active);
1213 }
1214 else {
1215 printk(KERN_INFO DRV_NAME
1216 ": %s: Could not set %s as active slave; "
1217 "either %s is down or the link is down.\n",
1218 bond->dev->name, slave->dev->name,
1219 slave->dev->name);
1220 }
1221 goto out;
1222 }
1223 }
1224 }
1225
1226 /* if we got here, then we didn't match the name of any slave */
1227
1228 if (strlen(buf) == 0 || buf[0] == '\n') {
1229 printk(KERN_INFO DRV_NAME
1230 ": %s: Setting active slave to None.\n",
1231 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001232 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001233 bond_select_active_slave(bond);
1234 } else {
1235 printk(KERN_INFO DRV_NAME
1236 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1237 bond->dev->name, (int)strlen(buf) - 1, buf);
1238 }
1239 }
1240out:
1241 write_unlock_bh(&bond->lock);
1242 return count;
1243
1244}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001245static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001246
1247
1248/*
1249 * Show link status of the bond interface.
1250 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001251static ssize_t bonding_show_mii_status(struct device *d,
1252 struct device_attribute *attr,
1253 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001254{
1255 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001256 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001257
1258 read_lock(&bond->curr_slave_lock);
1259 curr = bond->curr_active_slave;
1260 read_unlock(&bond->curr_slave_lock);
1261
1262 return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
1263}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001264static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001265
1266
1267/*
1268 * Show current 802.3ad aggregator ID.
1269 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001270static ssize_t bonding_show_ad_aggregator(struct device *d,
1271 struct device_attribute *attr,
1272 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001273{
1274 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001275 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001276
1277 if (bond->params.mode == BOND_MODE_8023AD) {
1278 struct ad_info ad_info;
1279 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
1280 }
1281 else
1282 count = sprintf(buf, "\n") + 1;
1283
1284 return count;
1285}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001286static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001287
1288
1289/*
1290 * Show number of active 802.3ad ports.
1291 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001292static ssize_t bonding_show_ad_num_ports(struct device *d,
1293 struct device_attribute *attr,
1294 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001295{
1296 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001297 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001298
1299 if (bond->params.mode == BOND_MODE_8023AD) {
1300 struct ad_info ad_info;
1301 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
1302 }
1303 else
1304 count = sprintf(buf, "\n") + 1;
1305
1306 return count;
1307}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001308static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001309
1310
1311/*
1312 * Show current 802.3ad actor key.
1313 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001314static ssize_t bonding_show_ad_actor_key(struct device *d,
1315 struct device_attribute *attr,
1316 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001317{
1318 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001319 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001320
1321 if (bond->params.mode == BOND_MODE_8023AD) {
1322 struct ad_info ad_info;
1323 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
1324 }
1325 else
1326 count = sprintf(buf, "\n") + 1;
1327
1328 return count;
1329}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001330static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001331
1332
1333/*
1334 * Show current 802.3ad partner key.
1335 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001336static ssize_t bonding_show_ad_partner_key(struct device *d,
1337 struct device_attribute *attr,
1338 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001339{
1340 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001341 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001342
1343 if (bond->params.mode == BOND_MODE_8023AD) {
1344 struct ad_info ad_info;
1345 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
1346 }
1347 else
1348 count = sprintf(buf, "\n") + 1;
1349
1350 return count;
1351}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001352static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001353
1354
1355/*
1356 * Show current 802.3ad partner mac.
1357 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001358static ssize_t bonding_show_ad_partner_mac(struct device *d,
1359 struct device_attribute *attr,
1360 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001361{
1362 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001363 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001364
1365 if (bond->params.mode == BOND_MODE_8023AD) {
1366 struct ad_info ad_info;
1367 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
1368 count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
1369 ad_info.partner_system[0],
1370 ad_info.partner_system[1],
1371 ad_info.partner_system[2],
1372 ad_info.partner_system[3],
1373 ad_info.partner_system[4],
1374 ad_info.partner_system[5]) + 1;
1375 }
1376 }
1377 else
1378 count = sprintf(buf, "\n") + 1;
1379
1380 return count;
1381}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001382static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001383
1384
1385
1386static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001387 &dev_attr_slaves.attr,
1388 &dev_attr_mode.attr,
1389 &dev_attr_arp_validate.attr,
1390 &dev_attr_arp_interval.attr,
1391 &dev_attr_arp_ip_target.attr,
1392 &dev_attr_downdelay.attr,
1393 &dev_attr_updelay.attr,
1394 &dev_attr_lacp_rate.attr,
1395 &dev_attr_xmit_hash_policy.attr,
1396 &dev_attr_miimon.attr,
1397 &dev_attr_primary.attr,
1398 &dev_attr_use_carrier.attr,
1399 &dev_attr_active_slave.attr,
1400 &dev_attr_mii_status.attr,
1401 &dev_attr_ad_aggregator.attr,
1402 &dev_attr_ad_num_ports.attr,
1403 &dev_attr_ad_actor_key.attr,
1404 &dev_attr_ad_partner_key.attr,
1405 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001406 NULL,
1407};
1408
1409static struct attribute_group bonding_group = {
1410 .name = "bonding",
1411 .attrs = per_bond_attrs,
1412};
1413
1414/*
1415 * Initialize sysfs. This sets up the bonding_masters file in
1416 * /sys/class/net.
1417 */
1418int bond_create_sysfs(void)
1419{
1420 int ret = 0;
1421 struct bonding *firstbond;
1422
1423 init_rwsem(&bonding_rwsem);
1424
1425 /* get the netdev class pointer */
1426 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1427 if (!firstbond)
1428 return -ENODEV;
1429
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001430 netdev_class = firstbond->dev->dev.class;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001431 if (!netdev_class)
1432 return -ENODEV;
1433
1434 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001435 /*
1436 * Permit multiple loads of the module by ignoring failures to
1437 * create the bonding_masters sysfs file. Bonding devices
1438 * created by second or subsequent loads of the module will
1439 * not be listed in, or controllable by, bonding_masters, but
1440 * will have the usual "bonding" sysfs directory.
1441 *
1442 * This is done to preserve backwards compatibility for
1443 * initscripts/sysconfig, which load bonding multiple times to
1444 * configure multiple bonding devices.
1445 */
1446 if (ret == -EEXIST) {
1447 netdev_class = NULL;
1448 return 0;
1449 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001450
1451 return ret;
1452
1453}
1454
1455/*
1456 * Remove /sys/class/net/bonding_masters.
1457 */
1458void bond_destroy_sysfs(void)
1459{
1460 if (netdev_class)
1461 class_remove_file(netdev_class, &class_attr_bonding_masters);
1462}
1463
1464/*
1465 * Initialize sysfs for each bond. This sets up and registers
1466 * the 'bondctl' directory for each individual bond under /sys/class/net.
1467 */
1468int bond_create_sysfs_entry(struct bonding *bond)
1469{
1470 struct net_device *dev = bond->dev;
1471 int err;
1472
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001473 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001474 if (err) {
1475 printk(KERN_EMERG "eek! didn't create group!\n");
1476 }
1477
1478 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001479 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001480
1481 return err;
1482}
1483/*
1484 * Remove sysfs entries for each bond.
1485 */
1486void bond_destroy_sysfs_entry(struct bonding *bond)
1487{
1488 struct net_device *dev = bond->dev;
1489
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001490 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001491}
1492