blob: b5d2a13fe6277ac47f235a8e5a03e0864d438f0c [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>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080034#include <linux/ctype.h>
35#include <linux/inet.h>
36#include <linux/rtnetlink.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070037#include <net/net_namespace.h>
Mitch Williamsb76cdba2005-11-09 10:36:41 -080038
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);
Moni Shouad90a1622007-10-09 19:43:43 -0700167 bond_destroy(bond);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800168 rtnl_unlock();
169 goto out;
170 }
171
172 printk(KERN_ERR DRV_NAME
173 ": unable to delete non-existent bond %s\n", ifname);
174 res = -ENODEV;
175 goto out;
176 }
177
178err_no_cmd:
179 printk(KERN_ERR DRV_NAME
180 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
181 res = -EPERM;
182
183 /* Always return either count or an error. If you return 0, you'll
184 * get called forever, which is bad.
185 */
186out:
187 up_write(&(bonding_rwsem));
188 return res;
189}
190/* class attribute for bond_masters file. This ends up in /sys/class/net */
191static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
192 bonding_show_bonds, bonding_store_bonds);
193
194int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
195{
196 char linkname[IFNAMSIZ+7];
197 int ret = 0;
198
199 /* first, create a link from the slave back to the master */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700200 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800201 "master");
202 if (ret)
203 return ret;
204 /* next, create a link from the master to the slave */
205 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700206 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800207 linkname);
208 return ret;
209
210}
211
212void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
213{
214 char linkname[IFNAMSIZ+7];
215
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700216 sysfs_remove_link(&(slave->dev.kobj), "master");
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800217 sprintf(linkname,"slave_%s",slave->name);
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700218 sysfs_remove_link(&(master->dev.kobj), linkname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800219}
220
221
222/*
223 * Show the slaves in the current bond.
224 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700225static ssize_t bonding_show_slaves(struct device *d,
226 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800227{
228 struct slave *slave;
229 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700230 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800231
232 read_lock_bh(&bond->lock);
233 bond_for_each_slave(bond, slave, i) {
234 if (res > (PAGE_SIZE - IFNAMSIZ)) {
235 /* not enough space for another interface name */
236 if ((PAGE_SIZE - res) > 10)
237 res = PAGE_SIZE - 10;
238 res += sprintf(buf + res, "++more++");
239 break;
240 }
241 res += sprintf(buf + res, "%s ", slave->dev->name);
242 }
243 read_unlock_bh(&bond->lock);
244 res += sprintf(buf + res, "\n");
245 res++;
246 return res;
247}
248
249/*
250 * Set the slaves in the current bond. The bond interface must be
251 * up for this to succeed.
252 * This function is largely the same flow as bonding_update_bonds().
253 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700254static ssize_t bonding_store_slaves(struct device *d,
255 struct device_attribute *attr,
256 const char *buffer, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800257{
258 char command[IFNAMSIZ + 1] = { 0, };
259 char *ifname;
260 int i, res, found, ret = count;
Moni Shoua3158bf72007-10-09 19:43:41 -0700261 u32 original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800262 struct slave *slave;
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -0800263 struct net_device *dev = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700264 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800265
266 /* Quick sanity check -- is the bond interface up? */
267 if (!(bond->dev->flags & IFF_UP)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700268 printk(KERN_WARNING DRV_NAME
269 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800270 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800271 }
272
273 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
274
275 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
276 ifname = command + 1;
277 if ((strlen(command) <= 1) ||
278 !dev_valid_name(ifname))
279 goto err_no_cmd;
280
281 if (command[0] == '+') {
282
283 /* Got a slave name in ifname. Is it already in the list? */
284 found = 0;
285 read_lock_bh(&bond->lock);
286 bond_for_each_slave(bond, slave, i)
287 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
288 printk(KERN_ERR DRV_NAME
289 ": %s: Interface %s is already enslaved!\n",
290 bond->dev->name, ifname);
291 ret = -EPERM;
292 read_unlock_bh(&bond->lock);
293 goto out;
294 }
295
296 read_unlock_bh(&bond->lock);
297 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
298 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700299 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800300 if (!dev) {
301 printk(KERN_INFO DRV_NAME
302 ": %s: Interface %s does not exist!\n",
303 bond->dev->name, ifname);
304 ret = -EPERM;
305 goto out;
306 }
307 else
308 dev_put(dev);
309
310 if (dev->flags & IFF_UP) {
311 printk(KERN_ERR DRV_NAME
312 ": %s: Error: Unable to enslave %s "
313 "because it is already up.\n",
314 bond->dev->name, dev->name);
315 ret = -EPERM;
316 goto out;
317 }
318 /* If this is the first slave, then we need to set
319 the master's hardware address to be the same as the
320 slave's. */
321 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
322 memcpy(bond->dev->dev_addr, dev->dev_addr,
323 dev->addr_len);
324 }
325
326 /* Set the slave's MTU to match the bond */
Moni Shoua3158bf72007-10-09 19:43:41 -0700327 original_mtu = dev->mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800328 if (dev->mtu != bond->dev->mtu) {
329 if (dev->change_mtu) {
330 res = dev->change_mtu(dev,
331 bond->dev->mtu);
332 if (res) {
333 ret = res;
334 goto out;
335 }
336 } else {
337 dev->mtu = bond->dev->mtu;
338 }
339 }
340 rtnl_lock();
341 res = bond_enslave(bond->dev, dev);
Moni Shoua3158bf72007-10-09 19:43:41 -0700342 bond_for_each_slave(bond, slave, i)
343 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
344 slave->original_mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800345 rtnl_unlock();
346 if (res) {
347 ret = res;
348 }
349 goto out;
350 }
351
352 if (command[0] == '-') {
353 dev = NULL;
354 bond_for_each_slave(bond, slave, i)
355 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
356 dev = slave->dev;
Moni Shoua3158bf72007-10-09 19:43:41 -0700357 original_mtu = slave->original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800358 break;
359 }
360 if (dev) {
361 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
362 bond->dev->name, dev->name);
363 rtnl_lock();
Moni Shouad90a1622007-10-09 19:43:43 -0700364 if (bond->setup_by_slave)
365 res = bond_release_and_destroy(bond->dev, dev);
366 else
367 res = bond_release(bond->dev, dev);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800368 rtnl_unlock();
369 if (res) {
370 ret = res;
371 goto out;
372 }
373 /* set the slave MTU to the default */
374 if (dev->change_mtu) {
Moni Shoua3158bf72007-10-09 19:43:41 -0700375 dev->change_mtu(dev, original_mtu);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800376 } else {
Moni Shoua3158bf72007-10-09 19:43:41 -0700377 dev->mtu = original_mtu;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800378 }
379 }
380 else {
381 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
382 ifname, bond->dev->name);
383 ret = -ENODEV;
384 }
385 goto out;
386 }
387
388err_no_cmd:
389 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
390 ret = -EPERM;
391
392out:
393 return ret;
394}
395
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700396static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800397
398/*
399 * Show and set the bonding mode. The bond interface must be down to
400 * change the mode.
401 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700402static ssize_t bonding_show_mode(struct device *d,
403 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800404{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700405 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800406
407 return sprintf(buf, "%s %d\n",
408 bond_mode_tbl[bond->params.mode].modename,
409 bond->params.mode) + 1;
410}
411
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700412static ssize_t bonding_store_mode(struct device *d,
413 struct device_attribute *attr,
414 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800415{
416 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700417 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800418
419 if (bond->dev->flags & IFF_UP) {
420 printk(KERN_ERR DRV_NAME
421 ": unable to update mode of %s because interface is up.\n",
422 bond->dev->name);
423 ret = -EPERM;
424 goto out;
425 }
426
427 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
428 if (new_value < 0) {
429 printk(KERN_ERR DRV_NAME
430 ": %s: Ignoring invalid mode value %.*s.\n",
431 bond->dev->name,
432 (int)strlen(buf) - 1, buf);
433 ret = -EINVAL;
434 goto out;
435 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800436 if (bond->params.mode == BOND_MODE_8023AD)
437 bond_unset_master_3ad_flags(bond);
438
439 if (bond->params.mode == BOND_MODE_ALB)
440 bond_unset_master_alb_flags(bond);
441
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800442 bond->params.mode = new_value;
443 bond_set_mode_ops(bond, bond->params.mode);
444 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
445 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
446 }
447out:
448 return ret;
449}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700450static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800451
452/*
453 * Show and set the bonding transmit hash method. The bond interface must be down to
454 * change the xmit hash policy.
455 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700456static ssize_t bonding_show_xmit_hash(struct device *d,
457 struct device_attribute *attr,
458 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800459{
460 int count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700461 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800462
463 if ((bond->params.mode != BOND_MODE_XOR) &&
464 (bond->params.mode != BOND_MODE_8023AD)) {
465 // Not Applicable
466 count = sprintf(buf, "NA\n") + 1;
467 } else {
468 count = sprintf(buf, "%s %d\n",
469 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
470 bond->params.xmit_policy) + 1;
471 }
472
473 return count;
474}
475
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700476static ssize_t bonding_store_xmit_hash(struct device *d,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800479{
480 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700481 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800482
483 if (bond->dev->flags & IFF_UP) {
484 printk(KERN_ERR DRV_NAME
485 "%s: Interface is up. Unable to update xmit policy.\n",
486 bond->dev->name);
487 ret = -EPERM;
488 goto out;
489 }
490
491 if ((bond->params.mode != BOND_MODE_XOR) &&
492 (bond->params.mode != BOND_MODE_8023AD)) {
493 printk(KERN_ERR DRV_NAME
494 "%s: Transmit hash policy is irrelevant in this mode.\n",
495 bond->dev->name);
496 ret = -EPERM;
497 goto out;
498 }
499
500 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
501 if (new_value < 0) {
502 printk(KERN_ERR DRV_NAME
503 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
504 bond->dev->name,
505 (int)strlen(buf) - 1, buf);
506 ret = -EINVAL;
507 goto out;
508 } else {
509 bond->params.xmit_policy = new_value;
510 bond_set_mode_ops(bond, bond->params.mode);
511 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
512 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
513 }
514out:
515 return ret;
516}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700517static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800518
519/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700520 * Show and set arp_validate.
521 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700522static ssize_t bonding_show_arp_validate(struct device *d,
523 struct device_attribute *attr,
524 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700525{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700526 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700527
528 return sprintf(buf, "%s %d\n",
529 arp_validate_tbl[bond->params.arp_validate].modename,
530 bond->params.arp_validate) + 1;
531}
532
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700533static ssize_t bonding_store_arp_validate(struct device *d,
534 struct device_attribute *attr,
535 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700536{
537 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700538 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700539
540 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
541 if (new_value < 0) {
542 printk(KERN_ERR DRV_NAME
543 ": %s: Ignoring invalid arp_validate value %s\n",
544 bond->dev->name, buf);
545 return -EINVAL;
546 }
547 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
548 printk(KERN_ERR DRV_NAME
549 ": %s: arp_validate only supported in active-backup mode.\n",
550 bond->dev->name);
551 return -EINVAL;
552 }
553 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
554 bond->dev->name, arp_validate_tbl[new_value].modename,
555 new_value);
556
557 if (!bond->params.arp_validate && new_value) {
558 bond_register_arp(bond);
559 } else if (bond->params.arp_validate && !new_value) {
560 bond_unregister_arp(bond);
561 }
562
563 bond->params.arp_validate = new_value;
564
565 return count;
566}
567
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700568static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700569
570/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800571 * Show and set the arp timer interval. There are two tricky bits
572 * here. First, if ARP monitoring is activated, then we must disable
573 * MII monitoring. Second, if the ARP timer isn't running, we must
574 * start it.
575 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700576static ssize_t bonding_show_arp_interval(struct device *d,
577 struct device_attribute *attr,
578 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800579{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700580 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800581
582 return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
583}
584
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700585static ssize_t bonding_store_arp_interval(struct device *d,
586 struct device_attribute *attr,
587 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800588{
589 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700590 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800591
592 if (sscanf(buf, "%d", &new_value) != 1) {
593 printk(KERN_ERR DRV_NAME
594 ": %s: no arp_interval value specified.\n",
595 bond->dev->name);
596 ret = -EINVAL;
597 goto out;
598 }
599 if (new_value < 0) {
600 printk(KERN_ERR DRV_NAME
601 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
602 bond->dev->name, new_value, INT_MAX);
603 ret = -EINVAL;
604 goto out;
605 }
606
607 printk(KERN_INFO DRV_NAME
608 ": %s: Setting ARP monitoring interval to %d.\n",
609 bond->dev->name, new_value);
610 bond->params.arp_interval = new_value;
611 if (bond->params.miimon) {
612 printk(KERN_INFO DRV_NAME
613 ": %s: ARP monitoring cannot be used with MII monitoring. "
614 "%s Disabling MII monitoring.\n",
615 bond->dev->name, bond->dev->name);
616 bond->params.miimon = 0;
617 /* Kill MII timer, else it brings bond's link down */
618 if (bond->arp_timer.function) {
619 printk(KERN_INFO DRV_NAME
620 ": %s: Kill MII timer, else it brings bond's link down...\n",
621 bond->dev->name);
622 del_timer_sync(&bond->mii_timer);
623 }
624 }
625 if (!bond->params.arp_targets[0]) {
626 printk(KERN_INFO DRV_NAME
627 ": %s: ARP monitoring has been set up, "
628 "but no ARP targets have been specified.\n",
629 bond->dev->name);
630 }
631 if (bond->dev->flags & IFF_UP) {
632 /* If the interface is up, we may need to fire off
633 * the ARP timer. If the interface is down, the
634 * timer will get fired off when the open function
635 * is called.
636 */
637 if (bond->arp_timer.function) {
638 /* The timer's already set up, so fire it off */
639 mod_timer(&bond->arp_timer, jiffies + 1);
640 } else {
641 /* Set up the timer. */
642 init_timer(&bond->arp_timer);
643 bond->arp_timer.expires = jiffies + 1;
644 bond->arp_timer.data =
645 (unsigned long) bond->dev;
646 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
647 bond->arp_timer.function =
648 (void *)
649 &bond_activebackup_arp_mon;
650 } else {
651 bond->arp_timer.function =
652 (void *)
653 &bond_loadbalance_arp_mon;
654 }
655 add_timer(&bond->arp_timer);
656 }
657 }
658
659out:
660 return ret;
661}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700662static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800663
664/*
665 * Show and set the arp targets.
666 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700667static ssize_t bonding_show_arp_targets(struct device *d,
668 struct device_attribute *attr,
669 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800670{
671 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700672 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800673
674 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
675 if (bond->params.arp_targets[i])
676 res += sprintf(buf + res, "%u.%u.%u.%u ",
677 NIPQUAD(bond->params.arp_targets[i]));
678 }
679 if (res)
680 res--; /* eat the leftover space */
681 res += sprintf(buf + res, "\n");
682 res++;
683 return res;
684}
685
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700686static ssize_t bonding_store_arp_targets(struct device *d,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800689{
Al Virod3bb52b2007-08-22 20:06:58 -0400690 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800691 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700692 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400693 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800694
695 targets = bond->params.arp_targets;
696 newtarget = in_aton(buf + 1);
697 /* look for adds */
698 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400699 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800700 printk(KERN_ERR DRV_NAME
701 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
702 bond->dev->name, NIPQUAD(newtarget));
703 ret = -EINVAL;
704 goto out;
705 }
706 /* look for an empty slot to put the target in, and check for dupes */
707 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
708 if (targets[i] == newtarget) { /* duplicate */
709 printk(KERN_ERR DRV_NAME
710 ": %s: ARP target %u.%u.%u.%u is already present\n",
711 bond->dev->name, NIPQUAD(newtarget));
712 if (done)
713 targets[i] = 0;
714 ret = -EINVAL;
715 goto out;
716 }
717 if (targets[i] == 0 && !done) {
718 printk(KERN_INFO DRV_NAME
719 ": %s: adding ARP target %d.%d.%d.%d.\n",
720 bond->dev->name, NIPQUAD(newtarget));
721 done = 1;
722 targets[i] = newtarget;
723 }
724 }
725 if (!done) {
726 printk(KERN_ERR DRV_NAME
727 ": %s: ARP target table is full!\n",
728 bond->dev->name);
729 ret = -EINVAL;
730 goto out;
731 }
732
733 }
734 else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400735 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800736 printk(KERN_ERR DRV_NAME
737 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
738 bond->dev->name, NIPQUAD(newtarget));
739 ret = -EINVAL;
740 goto out;
741 }
742
743 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
744 if (targets[i] == newtarget) {
745 printk(KERN_INFO DRV_NAME
746 ": %s: removing ARP target %d.%d.%d.%d.\n",
747 bond->dev->name, NIPQUAD(newtarget));
748 targets[i] = 0;
749 done = 1;
750 }
751 }
752 if (!done) {
753 printk(KERN_INFO DRV_NAME
754 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
755 bond->dev->name, NIPQUAD(newtarget));
756 ret = -EINVAL;
757 goto out;
758 }
759 }
760 else {
761 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
762 bond->dev->name);
763 ret = -EPERM;
764 goto out;
765 }
766
767out:
768 return ret;
769}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700770static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800771
772/*
773 * Show and set the up and down delays. These must be multiples of the
774 * MII monitoring value, and are stored internally as the multiplier.
775 * Thus, we must translate to MS for the real world.
776 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700777static ssize_t bonding_show_downdelay(struct device *d,
778 struct device_attribute *attr,
779 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800780{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700781 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800782
783 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
784}
785
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700786static ssize_t bonding_store_downdelay(struct device *d,
787 struct device_attribute *attr,
788 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800789{
790 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700791 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800792
793 if (!(bond->params.miimon)) {
794 printk(KERN_ERR DRV_NAME
795 ": %s: Unable to set down delay as MII monitoring is disabled\n",
796 bond->dev->name);
797 ret = -EPERM;
798 goto out;
799 }
800
801 if (sscanf(buf, "%d", &new_value) != 1) {
802 printk(KERN_ERR DRV_NAME
803 ": %s: no down delay value specified.\n",
804 bond->dev->name);
805 ret = -EINVAL;
806 goto out;
807 }
808 if (new_value < 0) {
809 printk(KERN_ERR DRV_NAME
810 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
811 bond->dev->name, new_value, 1, INT_MAX);
812 ret = -EINVAL;
813 goto out;
814 } else {
815 if ((new_value % bond->params.miimon) != 0) {
816 printk(KERN_WARNING DRV_NAME
817 ": %s: Warning: down delay (%d) is not a multiple "
818 "of miimon (%d), delay rounded to %d ms\n",
819 bond->dev->name, new_value, bond->params.miimon,
820 (new_value / bond->params.miimon) *
821 bond->params.miimon);
822 }
823 bond->params.downdelay = new_value / bond->params.miimon;
824 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
825 bond->dev->name, bond->params.downdelay * bond->params.miimon);
826
827 }
828
829out:
830 return ret;
831}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700832static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800833
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700834static ssize_t bonding_show_updelay(struct device *d,
835 struct device_attribute *attr,
836 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800837{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700838 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800839
840 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
841
842}
843
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700844static ssize_t bonding_store_updelay(struct device *d,
845 struct device_attribute *attr,
846 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800847{
848 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700849 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800850
851 if (!(bond->params.miimon)) {
852 printk(KERN_ERR DRV_NAME
853 ": %s: Unable to set up delay as MII monitoring is disabled\n",
854 bond->dev->name);
855 ret = -EPERM;
856 goto out;
857 }
858
859 if (sscanf(buf, "%d", &new_value) != 1) {
860 printk(KERN_ERR DRV_NAME
861 ": %s: no up delay value specified.\n",
862 bond->dev->name);
863 ret = -EINVAL;
864 goto out;
865 }
866 if (new_value < 0) {
867 printk(KERN_ERR DRV_NAME
868 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
869 bond->dev->name, new_value, 1, INT_MAX);
870 ret = -EINVAL;
871 goto out;
872 } else {
873 if ((new_value % bond->params.miimon) != 0) {
874 printk(KERN_WARNING DRV_NAME
875 ": %s: Warning: up delay (%d) is not a multiple "
876 "of miimon (%d), updelay rounded to %d ms\n",
877 bond->dev->name, new_value, bond->params.miimon,
878 (new_value / bond->params.miimon) *
879 bond->params.miimon);
880 }
881 bond->params.updelay = new_value / bond->params.miimon;
882 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
883 bond->dev->name, bond->params.updelay * bond->params.miimon);
884
885 }
886
887out:
888 return ret;
889}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700890static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800891
892/*
893 * Show and set the LACP interval. Interface must be down, and the mode
894 * must be set to 802.3ad mode.
895 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700896static ssize_t bonding_show_lacp(struct device *d,
897 struct device_attribute *attr,
898 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800899{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700900 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800901
902 return sprintf(buf, "%s %d\n",
903 bond_lacp_tbl[bond->params.lacp_fast].modename,
904 bond->params.lacp_fast) + 1;
905}
906
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700907static ssize_t bonding_store_lacp(struct device *d,
908 struct device_attribute *attr,
909 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800910{
911 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700912 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800913
914 if (bond->dev->flags & IFF_UP) {
915 printk(KERN_ERR DRV_NAME
916 ": %s: Unable to update LACP rate because interface is up.\n",
917 bond->dev->name);
918 ret = -EPERM;
919 goto out;
920 }
921
922 if (bond->params.mode != BOND_MODE_8023AD) {
923 printk(KERN_ERR DRV_NAME
924 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
925 bond->dev->name);
926 ret = -EPERM;
927 goto out;
928 }
929
930 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
931
932 if ((new_value == 1) || (new_value == 0)) {
933 bond->params.lacp_fast = new_value;
934 printk(KERN_INFO DRV_NAME
935 ": %s: Setting LACP rate to %s (%d).\n",
936 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
937 } else {
938 printk(KERN_ERR DRV_NAME
939 ": %s: Ignoring invalid LACP rate value %.*s.\n",
940 bond->dev->name, (int)strlen(buf) - 1, buf);
941 ret = -EINVAL;
942 }
943out:
944 return ret;
945}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700946static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800947
948/*
949 * Show and set the MII monitor interval. There are two tricky bits
950 * here. First, if MII monitoring is activated, then we must disable
951 * ARP monitoring. Second, if the timer isn't running, we must
952 * start it.
953 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700954static ssize_t bonding_show_miimon(struct device *d,
955 struct device_attribute *attr,
956 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800957{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700958 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800959
960 return sprintf(buf, "%d\n", bond->params.miimon) + 1;
961}
962
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700963static ssize_t bonding_store_miimon(struct device *d,
964 struct device_attribute *attr,
965 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800966{
967 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700968 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800969
970 if (sscanf(buf, "%d", &new_value) != 1) {
971 printk(KERN_ERR DRV_NAME
972 ": %s: no miimon value specified.\n",
973 bond->dev->name);
974 ret = -EINVAL;
975 goto out;
976 }
977 if (new_value < 0) {
978 printk(KERN_ERR DRV_NAME
979 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
980 bond->dev->name, new_value, 1, INT_MAX);
981 ret = -EINVAL;
982 goto out;
983 } else {
984 printk(KERN_INFO DRV_NAME
985 ": %s: Setting MII monitoring interval to %d.\n",
986 bond->dev->name, new_value);
987 bond->params.miimon = new_value;
988 if(bond->params.updelay)
989 printk(KERN_INFO DRV_NAME
990 ": %s: Note: Updating updelay (to %d) "
991 "since it is a multiple of the miimon value.\n",
992 bond->dev->name,
993 bond->params.updelay * bond->params.miimon);
994 if(bond->params.downdelay)
995 printk(KERN_INFO DRV_NAME
996 ": %s: Note: Updating downdelay (to %d) "
997 "since it is a multiple of the miimon value.\n",
998 bond->dev->name,
999 bond->params.downdelay * bond->params.miimon);
1000 if (bond->params.arp_interval) {
1001 printk(KERN_INFO DRV_NAME
1002 ": %s: MII monitoring cannot be used with "
1003 "ARP monitoring. Disabling ARP monitoring...\n",
1004 bond->dev->name);
1005 bond->params.arp_interval = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001006 if (bond->params.arp_validate) {
1007 bond_unregister_arp(bond);
1008 bond->params.arp_validate =
1009 BOND_ARP_VALIDATE_NONE;
1010 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001011 /* Kill ARP timer, else it brings bond's link down */
1012 if (bond->mii_timer.function) {
1013 printk(KERN_INFO DRV_NAME
1014 ": %s: Kill ARP timer, else it brings bond's link down...\n",
1015 bond->dev->name);
1016 del_timer_sync(&bond->arp_timer);
1017 }
1018 }
1019
1020 if (bond->dev->flags & IFF_UP) {
1021 /* If the interface is up, we may need to fire off
1022 * the MII timer. If the interface is down, the
1023 * timer will get fired off when the open function
1024 * is called.
1025 */
1026 if (bond->mii_timer.function) {
1027 /* The timer's already set up, so fire it off */
1028 mod_timer(&bond->mii_timer, jiffies + 1);
1029 } else {
1030 /* Set up the timer. */
1031 init_timer(&bond->mii_timer);
1032 bond->mii_timer.expires = jiffies + 1;
1033 bond->mii_timer.data =
1034 (unsigned long) bond->dev;
1035 bond->mii_timer.function =
1036 (void *) &bond_mii_monitor;
1037 add_timer(&bond->mii_timer);
1038 }
1039 }
1040 }
1041out:
1042 return ret;
1043}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001044static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001045
1046/*
1047 * Show and set the primary slave. The store function is much
1048 * simpler than bonding_store_slaves function because it only needs to
1049 * handle one interface name.
1050 * The bond must be a mode that supports a primary for this be
1051 * set.
1052 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001053static ssize_t bonding_show_primary(struct device *d,
1054 struct device_attribute *attr,
1055 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001056{
1057 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001058 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001059
1060 if (bond->primary_slave)
1061 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
1062 else
1063 count = sprintf(buf, "\n") + 1;
1064
1065 return count;
1066}
1067
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001068static ssize_t bonding_store_primary(struct device *d,
1069 struct device_attribute *attr,
1070 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001071{
1072 int i;
1073 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001074 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001075
1076 write_lock_bh(&bond->lock);
1077 if (!USES_PRIMARY(bond->params.mode)) {
1078 printk(KERN_INFO DRV_NAME
1079 ": %s: Unable to set primary slave; %s is in mode %d\n",
1080 bond->dev->name, bond->dev->name, bond->params.mode);
1081 } else {
1082 bond_for_each_slave(bond, slave, i) {
1083 if (strnicmp
1084 (slave->dev->name, buf,
1085 strlen(slave->dev->name)) == 0) {
1086 printk(KERN_INFO DRV_NAME
1087 ": %s: Setting %s as primary slave.\n",
1088 bond->dev->name, slave->dev->name);
1089 bond->primary_slave = slave;
1090 bond_select_active_slave(bond);
1091 goto out;
1092 }
1093 }
1094
1095 /* if we got here, then we didn't match the name of any slave */
1096
1097 if (strlen(buf) == 0 || buf[0] == '\n') {
1098 printk(KERN_INFO DRV_NAME
1099 ": %s: Setting primary slave to None.\n",
1100 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001101 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001102 bond_select_active_slave(bond);
1103 } else {
1104 printk(KERN_INFO DRV_NAME
1105 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1106 bond->dev->name, (int)strlen(buf) - 1, buf);
1107 }
1108 }
1109out:
1110 write_unlock_bh(&bond->lock);
1111 return count;
1112}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001113static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001114
1115/*
1116 * Show and set the use_carrier flag.
1117 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001118static ssize_t bonding_show_carrier(struct device *d,
1119 struct device_attribute *attr,
1120 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001121{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001122 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001123
1124 return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
1125}
1126
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001127static ssize_t bonding_store_carrier(struct device *d,
1128 struct device_attribute *attr,
1129 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001130{
1131 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001132 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001133
1134
1135 if (sscanf(buf, "%d", &new_value) != 1) {
1136 printk(KERN_ERR DRV_NAME
1137 ": %s: no use_carrier value specified.\n",
1138 bond->dev->name);
1139 ret = -EINVAL;
1140 goto out;
1141 }
1142 if ((new_value == 0) || (new_value == 1)) {
1143 bond->params.use_carrier = new_value;
1144 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1145 bond->dev->name, new_value);
1146 } else {
1147 printk(KERN_INFO DRV_NAME
1148 ": %s: Ignoring invalid use_carrier value %d.\n",
1149 bond->dev->name, new_value);
1150 }
1151out:
1152 return count;
1153}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001154static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001155
1156
1157/*
1158 * Show and set currently active_slave.
1159 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001160static ssize_t bonding_show_active_slave(struct device *d,
1161 struct device_attribute *attr,
1162 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001163{
1164 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001165 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001166 int count;
1167
1168
1169 read_lock(&bond->curr_slave_lock);
1170 curr = bond->curr_active_slave;
1171 read_unlock(&bond->curr_slave_lock);
1172
1173 if (USES_PRIMARY(bond->params.mode) && curr)
1174 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
1175 else
1176 count = sprintf(buf, "\n") + 1;
1177 return count;
1178}
1179
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001180static ssize_t bonding_store_active_slave(struct device *d,
1181 struct device_attribute *attr,
1182 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001183{
1184 int i;
1185 struct slave *slave;
1186 struct slave *old_active = NULL;
1187 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001188 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001189
1190 write_lock_bh(&bond->lock);
1191 if (!USES_PRIMARY(bond->params.mode)) {
1192 printk(KERN_INFO DRV_NAME
1193 ": %s: Unable to change active slave; %s is in mode %d\n",
1194 bond->dev->name, bond->dev->name, bond->params.mode);
1195 } else {
1196 bond_for_each_slave(bond, slave, i) {
1197 if (strnicmp
1198 (slave->dev->name, buf,
1199 strlen(slave->dev->name)) == 0) {
1200 old_active = bond->curr_active_slave;
1201 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001202 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001203 /* do nothing */
1204 printk(KERN_INFO DRV_NAME
1205 ": %s: %s is already the current active slave.\n",
1206 bond->dev->name, slave->dev->name);
1207 goto out;
1208 }
1209 else {
1210 if ((new_active) &&
1211 (old_active) &&
1212 (new_active->link == BOND_LINK_UP) &&
1213 IS_UP(new_active->dev)) {
1214 printk(KERN_INFO DRV_NAME
1215 ": %s: Setting %s as active slave.\n",
1216 bond->dev->name, slave->dev->name);
1217 bond_change_active_slave(bond, new_active);
1218 }
1219 else {
1220 printk(KERN_INFO DRV_NAME
1221 ": %s: Could not set %s as active slave; "
1222 "either %s is down or the link is down.\n",
1223 bond->dev->name, slave->dev->name,
1224 slave->dev->name);
1225 }
1226 goto out;
1227 }
1228 }
1229 }
1230
1231 /* if we got here, then we didn't match the name of any slave */
1232
1233 if (strlen(buf) == 0 || buf[0] == '\n') {
1234 printk(KERN_INFO DRV_NAME
1235 ": %s: Setting active slave to None.\n",
1236 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001237 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001238 bond_select_active_slave(bond);
1239 } else {
1240 printk(KERN_INFO DRV_NAME
1241 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1242 bond->dev->name, (int)strlen(buf) - 1, buf);
1243 }
1244 }
1245out:
1246 write_unlock_bh(&bond->lock);
1247 return count;
1248
1249}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001250static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001251
1252
1253/*
1254 * Show link status of the bond interface.
1255 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001256static ssize_t bonding_show_mii_status(struct device *d,
1257 struct device_attribute *attr,
1258 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001259{
1260 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001261 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001262
1263 read_lock(&bond->curr_slave_lock);
1264 curr = bond->curr_active_slave;
1265 read_unlock(&bond->curr_slave_lock);
1266
1267 return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
1268}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001269static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001270
1271
1272/*
1273 * Show current 802.3ad aggregator ID.
1274 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001275static ssize_t bonding_show_ad_aggregator(struct device *d,
1276 struct device_attribute *attr,
1277 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001278{
1279 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001280 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001281
1282 if (bond->params.mode == BOND_MODE_8023AD) {
1283 struct ad_info ad_info;
1284 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
1285 }
1286 else
1287 count = sprintf(buf, "\n") + 1;
1288
1289 return count;
1290}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001291static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001292
1293
1294/*
1295 * Show number of active 802.3ad ports.
1296 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001297static ssize_t bonding_show_ad_num_ports(struct device *d,
1298 struct device_attribute *attr,
1299 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001300{
1301 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001302 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001303
1304 if (bond->params.mode == BOND_MODE_8023AD) {
1305 struct ad_info ad_info;
1306 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
1307 }
1308 else
1309 count = sprintf(buf, "\n") + 1;
1310
1311 return count;
1312}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001313static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001314
1315
1316/*
1317 * Show current 802.3ad actor key.
1318 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001319static ssize_t bonding_show_ad_actor_key(struct device *d,
1320 struct device_attribute *attr,
1321 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001322{
1323 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001324 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001325
1326 if (bond->params.mode == BOND_MODE_8023AD) {
1327 struct ad_info ad_info;
1328 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
1329 }
1330 else
1331 count = sprintf(buf, "\n") + 1;
1332
1333 return count;
1334}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001335static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001336
1337
1338/*
1339 * Show current 802.3ad partner key.
1340 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001341static ssize_t bonding_show_ad_partner_key(struct device *d,
1342 struct device_attribute *attr,
1343 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001344{
1345 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001346 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001347
1348 if (bond->params.mode == BOND_MODE_8023AD) {
1349 struct ad_info ad_info;
1350 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
1351 }
1352 else
1353 count = sprintf(buf, "\n") + 1;
1354
1355 return count;
1356}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001357static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001358
1359
1360/*
1361 * Show current 802.3ad partner mac.
1362 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001363static ssize_t bonding_show_ad_partner_mac(struct device *d,
1364 struct device_attribute *attr,
1365 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001366{
1367 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001368 struct bonding *bond = to_bond(d);
Joe Perches0795af52007-10-03 17:59:30 -07001369 DECLARE_MAC_BUF(mac);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370
1371 if (bond->params.mode == BOND_MODE_8023AD) {
1372 struct ad_info ad_info;
1373 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
Joe Perches0795af52007-10-03 17:59:30 -07001374 count = sprintf(buf,"%s\n",
1375 print_mac(mac, ad_info.partner_system))
1376 + 1;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001377 }
1378 }
1379 else
1380 count = sprintf(buf, "\n") + 1;
1381
1382 return count;
1383}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001384static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001385
1386
1387
1388static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001389 &dev_attr_slaves.attr,
1390 &dev_attr_mode.attr,
1391 &dev_attr_arp_validate.attr,
1392 &dev_attr_arp_interval.attr,
1393 &dev_attr_arp_ip_target.attr,
1394 &dev_attr_downdelay.attr,
1395 &dev_attr_updelay.attr,
1396 &dev_attr_lacp_rate.attr,
1397 &dev_attr_xmit_hash_policy.attr,
1398 &dev_attr_miimon.attr,
1399 &dev_attr_primary.attr,
1400 &dev_attr_use_carrier.attr,
1401 &dev_attr_active_slave.attr,
1402 &dev_attr_mii_status.attr,
1403 &dev_attr_ad_aggregator.attr,
1404 &dev_attr_ad_num_ports.attr,
1405 &dev_attr_ad_actor_key.attr,
1406 &dev_attr_ad_partner_key.attr,
1407 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001408 NULL,
1409};
1410
1411static struct attribute_group bonding_group = {
1412 .name = "bonding",
1413 .attrs = per_bond_attrs,
1414};
1415
1416/*
1417 * Initialize sysfs. This sets up the bonding_masters file in
1418 * /sys/class/net.
1419 */
1420int bond_create_sysfs(void)
1421{
1422 int ret = 0;
1423 struct bonding *firstbond;
1424
1425 init_rwsem(&bonding_rwsem);
1426
1427 /* get the netdev class pointer */
1428 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1429 if (!firstbond)
1430 return -ENODEV;
1431
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001432 netdev_class = firstbond->dev->dev.class;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001433 if (!netdev_class)
1434 return -ENODEV;
1435
1436 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001437 /*
1438 * Permit multiple loads of the module by ignoring failures to
1439 * create the bonding_masters sysfs file. Bonding devices
1440 * created by second or subsequent loads of the module will
1441 * not be listed in, or controllable by, bonding_masters, but
1442 * will have the usual "bonding" sysfs directory.
1443 *
1444 * This is done to preserve backwards compatibility for
1445 * initscripts/sysconfig, which load bonding multiple times to
1446 * configure multiple bonding devices.
1447 */
1448 if (ret == -EEXIST) {
1449 netdev_class = NULL;
1450 return 0;
1451 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001452
1453 return ret;
1454
1455}
1456
1457/*
1458 * Remove /sys/class/net/bonding_masters.
1459 */
1460void bond_destroy_sysfs(void)
1461{
1462 if (netdev_class)
1463 class_remove_file(netdev_class, &class_attr_bonding_masters);
1464}
1465
1466/*
1467 * Initialize sysfs for each bond. This sets up and registers
1468 * the 'bondctl' directory for each individual bond under /sys/class/net.
1469 */
1470int bond_create_sysfs_entry(struct bonding *bond)
1471{
1472 struct net_device *dev = bond->dev;
1473 int err;
1474
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001475 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001476 if (err) {
1477 printk(KERN_EMERG "eek! didn't create group!\n");
1478 }
1479
1480 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001481 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001482
1483 return err;
1484}
1485/*
1486 * Remove sysfs entries for each bond.
1487 */
1488void bond_destroy_sysfs_entry(struct bonding *bond)
1489{
1490 struct net_device *dev = bond->dev;
1491
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001492 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001493}
1494