blob: ca4e429f9ec2505a2ab4d97314357077f06dcec3 [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);
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)) {
Moni Shoua6b1bf092007-10-09 19:43:40 -0700269 printk(KERN_WARNING DRV_NAME
270 ": %s: doing slave updates when interface is down.\n",
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800271 bond->dev->name);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800272 }
273
274 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
275
276 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
277 ifname = command + 1;
278 if ((strlen(command) <= 1) ||
279 !dev_valid_name(ifname))
280 goto err_no_cmd;
281
282 if (command[0] == '+') {
283
284 /* Got a slave name in ifname. Is it already in the list? */
285 found = 0;
286 read_lock_bh(&bond->lock);
287 bond_for_each_slave(bond, slave, i)
288 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
289 printk(KERN_ERR DRV_NAME
290 ": %s: Interface %s is already enslaved!\n",
291 bond->dev->name, ifname);
292 ret = -EPERM;
293 read_unlock_bh(&bond->lock);
294 goto out;
295 }
296
297 read_unlock_bh(&bond->lock);
298 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
299 bond->dev->name, ifname);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700300 dev = dev_get_by_name(&init_net, ifname);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800301 if (!dev) {
302 printk(KERN_INFO DRV_NAME
303 ": %s: Interface %s does not exist!\n",
304 bond->dev->name, ifname);
305 ret = -EPERM;
306 goto out;
307 }
308 else
309 dev_put(dev);
310
311 if (dev->flags & IFF_UP) {
312 printk(KERN_ERR DRV_NAME
313 ": %s: Error: Unable to enslave %s "
314 "because it is already up.\n",
315 bond->dev->name, dev->name);
316 ret = -EPERM;
317 goto out;
318 }
319 /* If this is the first slave, then we need to set
320 the master's hardware address to be the same as the
321 slave's. */
322 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
323 memcpy(bond->dev->dev_addr, dev->dev_addr,
324 dev->addr_len);
325 }
326
327 /* Set the slave's MTU to match the bond */
328 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);
342 rtnl_unlock();
343 if (res) {
344 ret = res;
345 }
346 goto out;
347 }
348
349 if (command[0] == '-') {
350 dev = NULL;
351 bond_for_each_slave(bond, slave, i)
352 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
353 dev = slave->dev;
354 break;
355 }
356 if (dev) {
357 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
358 bond->dev->name, dev->name);
359 rtnl_lock();
360 res = bond_release(bond->dev, dev);
361 rtnl_unlock();
362 if (res) {
363 ret = res;
364 goto out;
365 }
366 /* set the slave MTU to the default */
367 if (dev->change_mtu) {
368 dev->change_mtu(dev, 1500);
369 } else {
370 dev->mtu = 1500;
371 }
372 }
373 else {
374 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
375 ifname, bond->dev->name);
376 ret = -ENODEV;
377 }
378 goto out;
379 }
380
381err_no_cmd:
382 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
383 ret = -EPERM;
384
385out:
386 return ret;
387}
388
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700389static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800390
391/*
392 * Show and set the bonding mode. The bond interface must be down to
393 * change the mode.
394 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700395static ssize_t bonding_show_mode(struct device *d,
396 struct device_attribute *attr, char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800397{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700398 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800399
400 return sprintf(buf, "%s %d\n",
401 bond_mode_tbl[bond->params.mode].modename,
402 bond->params.mode) + 1;
403}
404
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700405static ssize_t bonding_store_mode(struct device *d,
406 struct device_attribute *attr,
407 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800408{
409 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700410 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800411
412 if (bond->dev->flags & IFF_UP) {
413 printk(KERN_ERR DRV_NAME
414 ": unable to update mode of %s because interface is up.\n",
415 bond->dev->name);
416 ret = -EPERM;
417 goto out;
418 }
419
420 new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
421 if (new_value < 0) {
422 printk(KERN_ERR DRV_NAME
423 ": %s: Ignoring invalid mode value %.*s.\n",
424 bond->dev->name,
425 (int)strlen(buf) - 1, buf);
426 ret = -EINVAL;
427 goto out;
428 } else {
Jay Vosburgh8f903c72006-02-21 16:36:44 -0800429 if (bond->params.mode == BOND_MODE_8023AD)
430 bond_unset_master_3ad_flags(bond);
431
432 if (bond->params.mode == BOND_MODE_ALB)
433 bond_unset_master_alb_flags(bond);
434
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800435 bond->params.mode = new_value;
436 bond_set_mode_ops(bond, bond->params.mode);
437 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
438 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
439 }
440out:
441 return ret;
442}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700443static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800444
445/*
446 * Show and set the bonding transmit hash method. The bond interface must be down to
447 * change the xmit hash policy.
448 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700449static ssize_t bonding_show_xmit_hash(struct device *d,
450 struct device_attribute *attr,
451 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800452{
453 int count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700454 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800455
456 if ((bond->params.mode != BOND_MODE_XOR) &&
457 (bond->params.mode != BOND_MODE_8023AD)) {
458 // Not Applicable
459 count = sprintf(buf, "NA\n") + 1;
460 } else {
461 count = sprintf(buf, "%s %d\n",
462 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
463 bond->params.xmit_policy) + 1;
464 }
465
466 return count;
467}
468
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700469static ssize_t bonding_store_xmit_hash(struct device *d,
470 struct device_attribute *attr,
471 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800472{
473 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700474 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800475
476 if (bond->dev->flags & IFF_UP) {
477 printk(KERN_ERR DRV_NAME
478 "%s: Interface is up. Unable to update xmit policy.\n",
479 bond->dev->name);
480 ret = -EPERM;
481 goto out;
482 }
483
484 if ((bond->params.mode != BOND_MODE_XOR) &&
485 (bond->params.mode != BOND_MODE_8023AD)) {
486 printk(KERN_ERR DRV_NAME
487 "%s: Transmit hash policy is irrelevant in this mode.\n",
488 bond->dev->name);
489 ret = -EPERM;
490 goto out;
491 }
492
493 new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
494 if (new_value < 0) {
495 printk(KERN_ERR DRV_NAME
496 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
497 bond->dev->name,
498 (int)strlen(buf) - 1, buf);
499 ret = -EINVAL;
500 goto out;
501 } else {
502 bond->params.xmit_policy = new_value;
503 bond_set_mode_ops(bond, bond->params.mode);
504 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
505 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
506 }
507out:
508 return ret;
509}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700510static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800511
512/*
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700513 * Show and set arp_validate.
514 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700515static ssize_t bonding_show_arp_validate(struct device *d,
516 struct device_attribute *attr,
517 char *buf)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700518{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700519 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700520
521 return sprintf(buf, "%s %d\n",
522 arp_validate_tbl[bond->params.arp_validate].modename,
523 bond->params.arp_validate) + 1;
524}
525
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700526static ssize_t bonding_store_arp_validate(struct device *d,
527 struct device_attribute *attr,
528 const char *buf, size_t count)
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700529{
530 int new_value;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700531 struct bonding *bond = to_bond(d);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700532
533 new_value = bond_parse_parm((char *)buf, arp_validate_tbl);
534 if (new_value < 0) {
535 printk(KERN_ERR DRV_NAME
536 ": %s: Ignoring invalid arp_validate value %s\n",
537 bond->dev->name, buf);
538 return -EINVAL;
539 }
540 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
541 printk(KERN_ERR DRV_NAME
542 ": %s: arp_validate only supported in active-backup mode.\n",
543 bond->dev->name);
544 return -EINVAL;
545 }
546 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
547 bond->dev->name, arp_validate_tbl[new_value].modename,
548 new_value);
549
550 if (!bond->params.arp_validate && new_value) {
551 bond_register_arp(bond);
552 } else if (bond->params.arp_validate && !new_value) {
553 bond_unregister_arp(bond);
554 }
555
556 bond->params.arp_validate = new_value;
557
558 return count;
559}
560
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700561static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700562
563/*
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800564 * Show and set the arp timer interval. There are two tricky bits
565 * here. First, if ARP monitoring is activated, then we must disable
566 * MII monitoring. Second, if the ARP timer isn't running, we must
567 * start it.
568 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700569static ssize_t bonding_show_arp_interval(struct device *d,
570 struct device_attribute *attr,
571 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800572{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700573 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800574
575 return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
576}
577
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700578static ssize_t bonding_store_arp_interval(struct device *d,
579 struct device_attribute *attr,
580 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800581{
582 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700583 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800584
585 if (sscanf(buf, "%d", &new_value) != 1) {
586 printk(KERN_ERR DRV_NAME
587 ": %s: no arp_interval value specified.\n",
588 bond->dev->name);
589 ret = -EINVAL;
590 goto out;
591 }
592 if (new_value < 0) {
593 printk(KERN_ERR DRV_NAME
594 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
595 bond->dev->name, new_value, INT_MAX);
596 ret = -EINVAL;
597 goto out;
598 }
599
600 printk(KERN_INFO DRV_NAME
601 ": %s: Setting ARP monitoring interval to %d.\n",
602 bond->dev->name, new_value);
603 bond->params.arp_interval = new_value;
604 if (bond->params.miimon) {
605 printk(KERN_INFO DRV_NAME
606 ": %s: ARP monitoring cannot be used with MII monitoring. "
607 "%s Disabling MII monitoring.\n",
608 bond->dev->name, bond->dev->name);
609 bond->params.miimon = 0;
610 /* Kill MII timer, else it brings bond's link down */
611 if (bond->arp_timer.function) {
612 printk(KERN_INFO DRV_NAME
613 ": %s: Kill MII timer, else it brings bond's link down...\n",
614 bond->dev->name);
615 del_timer_sync(&bond->mii_timer);
616 }
617 }
618 if (!bond->params.arp_targets[0]) {
619 printk(KERN_INFO DRV_NAME
620 ": %s: ARP monitoring has been set up, "
621 "but no ARP targets have been specified.\n",
622 bond->dev->name);
623 }
624 if (bond->dev->flags & IFF_UP) {
625 /* If the interface is up, we may need to fire off
626 * the ARP timer. If the interface is down, the
627 * timer will get fired off when the open function
628 * is called.
629 */
630 if (bond->arp_timer.function) {
631 /* The timer's already set up, so fire it off */
632 mod_timer(&bond->arp_timer, jiffies + 1);
633 } else {
634 /* Set up the timer. */
635 init_timer(&bond->arp_timer);
636 bond->arp_timer.expires = jiffies + 1;
637 bond->arp_timer.data =
638 (unsigned long) bond->dev;
639 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
640 bond->arp_timer.function =
641 (void *)
642 &bond_activebackup_arp_mon;
643 } else {
644 bond->arp_timer.function =
645 (void *)
646 &bond_loadbalance_arp_mon;
647 }
648 add_timer(&bond->arp_timer);
649 }
650 }
651
652out:
653 return ret;
654}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700655static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800656
657/*
658 * Show and set the arp targets.
659 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700660static ssize_t bonding_show_arp_targets(struct device *d,
661 struct device_attribute *attr,
662 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800663{
664 int i, res = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700665 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800666
667 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
668 if (bond->params.arp_targets[i])
669 res += sprintf(buf + res, "%u.%u.%u.%u ",
670 NIPQUAD(bond->params.arp_targets[i]));
671 }
672 if (res)
673 res--; /* eat the leftover space */
674 res += sprintf(buf + res, "\n");
675 res++;
676 return res;
677}
678
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700679static ssize_t bonding_store_arp_targets(struct device *d,
680 struct device_attribute *attr,
681 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800682{
Al Virod3bb52b2007-08-22 20:06:58 -0400683 __be32 newtarget;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800684 int i = 0, done = 0, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700685 struct bonding *bond = to_bond(d);
Al Virod3bb52b2007-08-22 20:06:58 -0400686 __be32 *targets;
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800687
688 targets = bond->params.arp_targets;
689 newtarget = in_aton(buf + 1);
690 /* look for adds */
691 if (buf[0] == '+') {
Al Virod3bb52b2007-08-22 20:06:58 -0400692 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800693 printk(KERN_ERR DRV_NAME
694 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
695 bond->dev->name, NIPQUAD(newtarget));
696 ret = -EINVAL;
697 goto out;
698 }
699 /* look for an empty slot to put the target in, and check for dupes */
700 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
701 if (targets[i] == newtarget) { /* duplicate */
702 printk(KERN_ERR DRV_NAME
703 ": %s: ARP target %u.%u.%u.%u is already present\n",
704 bond->dev->name, NIPQUAD(newtarget));
705 if (done)
706 targets[i] = 0;
707 ret = -EINVAL;
708 goto out;
709 }
710 if (targets[i] == 0 && !done) {
711 printk(KERN_INFO DRV_NAME
712 ": %s: adding ARP target %d.%d.%d.%d.\n",
713 bond->dev->name, NIPQUAD(newtarget));
714 done = 1;
715 targets[i] = newtarget;
716 }
717 }
718 if (!done) {
719 printk(KERN_ERR DRV_NAME
720 ": %s: ARP target table is full!\n",
721 bond->dev->name);
722 ret = -EINVAL;
723 goto out;
724 }
725
726 }
727 else if (buf[0] == '-') {
Al Virod3bb52b2007-08-22 20:06:58 -0400728 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800729 printk(KERN_ERR DRV_NAME
730 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
731 bond->dev->name, NIPQUAD(newtarget));
732 ret = -EINVAL;
733 goto out;
734 }
735
736 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
737 if (targets[i] == newtarget) {
738 printk(KERN_INFO DRV_NAME
739 ": %s: removing ARP target %d.%d.%d.%d.\n",
740 bond->dev->name, NIPQUAD(newtarget));
741 targets[i] = 0;
742 done = 1;
743 }
744 }
745 if (!done) {
746 printk(KERN_INFO DRV_NAME
747 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
748 bond->dev->name, NIPQUAD(newtarget));
749 ret = -EINVAL;
750 goto out;
751 }
752 }
753 else {
754 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
755 bond->dev->name);
756 ret = -EPERM;
757 goto out;
758 }
759
760out:
761 return ret;
762}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700763static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800764
765/*
766 * Show and set the up and down delays. These must be multiples of the
767 * MII monitoring value, and are stored internally as the multiplier.
768 * Thus, we must translate to MS for the real world.
769 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700770static ssize_t bonding_show_downdelay(struct device *d,
771 struct device_attribute *attr,
772 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800773{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700774 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800775
776 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
777}
778
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700779static ssize_t bonding_store_downdelay(struct device *d,
780 struct device_attribute *attr,
781 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800782{
783 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700784 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800785
786 if (!(bond->params.miimon)) {
787 printk(KERN_ERR DRV_NAME
788 ": %s: Unable to set down delay as MII monitoring is disabled\n",
789 bond->dev->name);
790 ret = -EPERM;
791 goto out;
792 }
793
794 if (sscanf(buf, "%d", &new_value) != 1) {
795 printk(KERN_ERR DRV_NAME
796 ": %s: no down delay value specified.\n",
797 bond->dev->name);
798 ret = -EINVAL;
799 goto out;
800 }
801 if (new_value < 0) {
802 printk(KERN_ERR DRV_NAME
803 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
804 bond->dev->name, new_value, 1, INT_MAX);
805 ret = -EINVAL;
806 goto out;
807 } else {
808 if ((new_value % bond->params.miimon) != 0) {
809 printk(KERN_WARNING DRV_NAME
810 ": %s: Warning: down delay (%d) is not a multiple "
811 "of miimon (%d), delay rounded to %d ms\n",
812 bond->dev->name, new_value, bond->params.miimon,
813 (new_value / bond->params.miimon) *
814 bond->params.miimon);
815 }
816 bond->params.downdelay = new_value / bond->params.miimon;
817 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
818 bond->dev->name, bond->params.downdelay * bond->params.miimon);
819
820 }
821
822out:
823 return ret;
824}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700825static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800826
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700827static ssize_t bonding_show_updelay(struct device *d,
828 struct device_attribute *attr,
829 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800830{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700831 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800832
833 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;
834
835}
836
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700837static ssize_t bonding_store_updelay(struct device *d,
838 struct device_attribute *attr,
839 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800840{
841 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700842 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800843
844 if (!(bond->params.miimon)) {
845 printk(KERN_ERR DRV_NAME
846 ": %s: Unable to set up delay as MII monitoring is disabled\n",
847 bond->dev->name);
848 ret = -EPERM;
849 goto out;
850 }
851
852 if (sscanf(buf, "%d", &new_value) != 1) {
853 printk(KERN_ERR DRV_NAME
854 ": %s: no up delay value specified.\n",
855 bond->dev->name);
856 ret = -EINVAL;
857 goto out;
858 }
859 if (new_value < 0) {
860 printk(KERN_ERR DRV_NAME
861 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
862 bond->dev->name, new_value, 1, INT_MAX);
863 ret = -EINVAL;
864 goto out;
865 } else {
866 if ((new_value % bond->params.miimon) != 0) {
867 printk(KERN_WARNING DRV_NAME
868 ": %s: Warning: up delay (%d) is not a multiple "
869 "of miimon (%d), updelay rounded to %d ms\n",
870 bond->dev->name, new_value, bond->params.miimon,
871 (new_value / bond->params.miimon) *
872 bond->params.miimon);
873 }
874 bond->params.updelay = new_value / bond->params.miimon;
875 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
876 bond->dev->name, bond->params.updelay * bond->params.miimon);
877
878 }
879
880out:
881 return ret;
882}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700883static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800884
885/*
886 * Show and set the LACP interval. Interface must be down, and the mode
887 * must be set to 802.3ad mode.
888 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700889static ssize_t bonding_show_lacp(struct device *d,
890 struct device_attribute *attr,
891 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800892{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700893 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800894
895 return sprintf(buf, "%s %d\n",
896 bond_lacp_tbl[bond->params.lacp_fast].modename,
897 bond->params.lacp_fast) + 1;
898}
899
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700900static ssize_t bonding_store_lacp(struct device *d,
901 struct device_attribute *attr,
902 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800903{
904 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700905 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800906
907 if (bond->dev->flags & IFF_UP) {
908 printk(KERN_ERR DRV_NAME
909 ": %s: Unable to update LACP rate because interface is up.\n",
910 bond->dev->name);
911 ret = -EPERM;
912 goto out;
913 }
914
915 if (bond->params.mode != BOND_MODE_8023AD) {
916 printk(KERN_ERR DRV_NAME
917 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
918 bond->dev->name);
919 ret = -EPERM;
920 goto out;
921 }
922
923 new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);
924
925 if ((new_value == 1) || (new_value == 0)) {
926 bond->params.lacp_fast = new_value;
927 printk(KERN_INFO DRV_NAME
928 ": %s: Setting LACP rate to %s (%d).\n",
929 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
930 } else {
931 printk(KERN_ERR DRV_NAME
932 ": %s: Ignoring invalid LACP rate value %.*s.\n",
933 bond->dev->name, (int)strlen(buf) - 1, buf);
934 ret = -EINVAL;
935 }
936out:
937 return ret;
938}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700939static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800940
941/*
942 * Show and set the MII monitor interval. There are two tricky bits
943 * here. First, if MII monitoring is activated, then we must disable
944 * ARP monitoring. Second, if the timer isn't running, we must
945 * start it.
946 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700947static ssize_t bonding_show_miimon(struct device *d,
948 struct device_attribute *attr,
949 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800950{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700951 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800952
953 return sprintf(buf, "%d\n", bond->params.miimon) + 1;
954}
955
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700956static ssize_t bonding_store_miimon(struct device *d,
957 struct device_attribute *attr,
958 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800959{
960 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -0700961 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -0800962
963 if (sscanf(buf, "%d", &new_value) != 1) {
964 printk(KERN_ERR DRV_NAME
965 ": %s: no miimon value specified.\n",
966 bond->dev->name);
967 ret = -EINVAL;
968 goto out;
969 }
970 if (new_value < 0) {
971 printk(KERN_ERR DRV_NAME
972 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
973 bond->dev->name, new_value, 1, INT_MAX);
974 ret = -EINVAL;
975 goto out;
976 } else {
977 printk(KERN_INFO DRV_NAME
978 ": %s: Setting MII monitoring interval to %d.\n",
979 bond->dev->name, new_value);
980 bond->params.miimon = new_value;
981 if(bond->params.updelay)
982 printk(KERN_INFO DRV_NAME
983 ": %s: Note: Updating updelay (to %d) "
984 "since it is a multiple of the miimon value.\n",
985 bond->dev->name,
986 bond->params.updelay * bond->params.miimon);
987 if(bond->params.downdelay)
988 printk(KERN_INFO DRV_NAME
989 ": %s: Note: Updating downdelay (to %d) "
990 "since it is a multiple of the miimon value.\n",
991 bond->dev->name,
992 bond->params.downdelay * bond->params.miimon);
993 if (bond->params.arp_interval) {
994 printk(KERN_INFO DRV_NAME
995 ": %s: MII monitoring cannot be used with "
996 "ARP monitoring. Disabling ARP monitoring...\n",
997 bond->dev->name);
998 bond->params.arp_interval = 0;
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700999 if (bond->params.arp_validate) {
1000 bond_unregister_arp(bond);
1001 bond->params.arp_validate =
1002 BOND_ARP_VALIDATE_NONE;
1003 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001004 /* Kill ARP timer, else it brings bond's link down */
1005 if (bond->mii_timer.function) {
1006 printk(KERN_INFO DRV_NAME
1007 ": %s: Kill ARP timer, else it brings bond's link down...\n",
1008 bond->dev->name);
1009 del_timer_sync(&bond->arp_timer);
1010 }
1011 }
1012
1013 if (bond->dev->flags & IFF_UP) {
1014 /* If the interface is up, we may need to fire off
1015 * the MII timer. If the interface is down, the
1016 * timer will get fired off when the open function
1017 * is called.
1018 */
1019 if (bond->mii_timer.function) {
1020 /* The timer's already set up, so fire it off */
1021 mod_timer(&bond->mii_timer, jiffies + 1);
1022 } else {
1023 /* Set up the timer. */
1024 init_timer(&bond->mii_timer);
1025 bond->mii_timer.expires = jiffies + 1;
1026 bond->mii_timer.data =
1027 (unsigned long) bond->dev;
1028 bond->mii_timer.function =
1029 (void *) &bond_mii_monitor;
1030 add_timer(&bond->mii_timer);
1031 }
1032 }
1033 }
1034out:
1035 return ret;
1036}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001037static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001038
1039/*
1040 * Show and set the primary slave. The store function is much
1041 * simpler than bonding_store_slaves function because it only needs to
1042 * handle one interface name.
1043 * The bond must be a mode that supports a primary for this be
1044 * set.
1045 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001046static ssize_t bonding_show_primary(struct device *d,
1047 struct device_attribute *attr,
1048 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001049{
1050 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001051 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001052
1053 if (bond->primary_slave)
1054 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
1055 else
1056 count = sprintf(buf, "\n") + 1;
1057
1058 return count;
1059}
1060
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001061static ssize_t bonding_store_primary(struct device *d,
1062 struct device_attribute *attr,
1063 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001064{
1065 int i;
1066 struct slave *slave;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001067 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001068
1069 write_lock_bh(&bond->lock);
1070 if (!USES_PRIMARY(bond->params.mode)) {
1071 printk(KERN_INFO DRV_NAME
1072 ": %s: Unable to set primary slave; %s is in mode %d\n",
1073 bond->dev->name, bond->dev->name, bond->params.mode);
1074 } else {
1075 bond_for_each_slave(bond, slave, i) {
1076 if (strnicmp
1077 (slave->dev->name, buf,
1078 strlen(slave->dev->name)) == 0) {
1079 printk(KERN_INFO DRV_NAME
1080 ": %s: Setting %s as primary slave.\n",
1081 bond->dev->name, slave->dev->name);
1082 bond->primary_slave = slave;
1083 bond_select_active_slave(bond);
1084 goto out;
1085 }
1086 }
1087
1088 /* if we got here, then we didn't match the name of any slave */
1089
1090 if (strlen(buf) == 0 || buf[0] == '\n') {
1091 printk(KERN_INFO DRV_NAME
1092 ": %s: Setting primary slave to None.\n",
1093 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001094 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001095 bond_select_active_slave(bond);
1096 } else {
1097 printk(KERN_INFO DRV_NAME
1098 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1099 bond->dev->name, (int)strlen(buf) - 1, buf);
1100 }
1101 }
1102out:
1103 write_unlock_bh(&bond->lock);
1104 return count;
1105}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001106static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001107
1108/*
1109 * Show and set the use_carrier flag.
1110 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001111static ssize_t bonding_show_carrier(struct device *d,
1112 struct device_attribute *attr,
1113 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001114{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001115 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001116
1117 return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
1118}
1119
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001120static ssize_t bonding_store_carrier(struct device *d,
1121 struct device_attribute *attr,
1122 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001123{
1124 int new_value, ret = count;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001125 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001126
1127
1128 if (sscanf(buf, "%d", &new_value) != 1) {
1129 printk(KERN_ERR DRV_NAME
1130 ": %s: no use_carrier value specified.\n",
1131 bond->dev->name);
1132 ret = -EINVAL;
1133 goto out;
1134 }
1135 if ((new_value == 0) || (new_value == 1)) {
1136 bond->params.use_carrier = new_value;
1137 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1138 bond->dev->name, new_value);
1139 } else {
1140 printk(KERN_INFO DRV_NAME
1141 ": %s: Ignoring invalid use_carrier value %d.\n",
1142 bond->dev->name, new_value);
1143 }
1144out:
1145 return count;
1146}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001147static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001148
1149
1150/*
1151 * Show and set currently active_slave.
1152 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001153static ssize_t bonding_show_active_slave(struct device *d,
1154 struct device_attribute *attr,
1155 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001156{
1157 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001158 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001159 int count;
1160
1161
1162 read_lock(&bond->curr_slave_lock);
1163 curr = bond->curr_active_slave;
1164 read_unlock(&bond->curr_slave_lock);
1165
1166 if (USES_PRIMARY(bond->params.mode) && curr)
1167 count = sprintf(buf, "%s\n", curr->dev->name) + 1;
1168 else
1169 count = sprintf(buf, "\n") + 1;
1170 return count;
1171}
1172
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001173static ssize_t bonding_store_active_slave(struct device *d,
1174 struct device_attribute *attr,
1175 const char *buf, size_t count)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001176{
1177 int i;
1178 struct slave *slave;
1179 struct slave *old_active = NULL;
1180 struct slave *new_active = NULL;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001181 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001182
1183 write_lock_bh(&bond->lock);
1184 if (!USES_PRIMARY(bond->params.mode)) {
1185 printk(KERN_INFO DRV_NAME
1186 ": %s: Unable to change active slave; %s is in mode %d\n",
1187 bond->dev->name, bond->dev->name, bond->params.mode);
1188 } else {
1189 bond_for_each_slave(bond, slave, i) {
1190 if (strnicmp
1191 (slave->dev->name, buf,
1192 strlen(slave->dev->name)) == 0) {
1193 old_active = bond->curr_active_slave;
1194 new_active = slave;
Jay Vosburgha50d8de2006-09-22 21:53:25 -07001195 if (new_active == old_active) {
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001196 /* do nothing */
1197 printk(KERN_INFO DRV_NAME
1198 ": %s: %s is already the current active slave.\n",
1199 bond->dev->name, slave->dev->name);
1200 goto out;
1201 }
1202 else {
1203 if ((new_active) &&
1204 (old_active) &&
1205 (new_active->link == BOND_LINK_UP) &&
1206 IS_UP(new_active->dev)) {
1207 printk(KERN_INFO DRV_NAME
1208 ": %s: Setting %s as active slave.\n",
1209 bond->dev->name, slave->dev->name);
1210 bond_change_active_slave(bond, new_active);
1211 }
1212 else {
1213 printk(KERN_INFO DRV_NAME
1214 ": %s: Could not set %s as active slave; "
1215 "either %s is down or the link is down.\n",
1216 bond->dev->name, slave->dev->name,
1217 slave->dev->name);
1218 }
1219 goto out;
1220 }
1221 }
1222 }
1223
1224 /* if we got here, then we didn't match the name of any slave */
1225
1226 if (strlen(buf) == 0 || buf[0] == '\n') {
1227 printk(KERN_INFO DRV_NAME
1228 ": %s: Setting active slave to None.\n",
1229 bond->dev->name);
Luiz Fernando Capitulino3418db72006-02-01 00:54:34 -08001230 bond->primary_slave = NULL;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001231 bond_select_active_slave(bond);
1232 } else {
1233 printk(KERN_INFO DRV_NAME
1234 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1235 bond->dev->name, (int)strlen(buf) - 1, buf);
1236 }
1237 }
1238out:
1239 write_unlock_bh(&bond->lock);
1240 return count;
1241
1242}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001243static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001244
1245
1246/*
1247 * Show link status of the bond interface.
1248 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001249static ssize_t bonding_show_mii_status(struct device *d,
1250 struct device_attribute *attr,
1251 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001252{
1253 struct slave *curr;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001254 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001255
1256 read_lock(&bond->curr_slave_lock);
1257 curr = bond->curr_active_slave;
1258 read_unlock(&bond->curr_slave_lock);
1259
1260 return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
1261}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001262static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001263
1264
1265/*
1266 * Show current 802.3ad aggregator ID.
1267 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001268static ssize_t bonding_show_ad_aggregator(struct device *d,
1269 struct device_attribute *attr,
1270 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001271{
1272 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001273 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001274
1275 if (bond->params.mode == BOND_MODE_8023AD) {
1276 struct ad_info ad_info;
1277 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id) + 1;
1278 }
1279 else
1280 count = sprintf(buf, "\n") + 1;
1281
1282 return count;
1283}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001284static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001285
1286
1287/*
1288 * Show number of active 802.3ad ports.
1289 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001290static ssize_t bonding_show_ad_num_ports(struct device *d,
1291 struct device_attribute *attr,
1292 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001293{
1294 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001295 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001296
1297 if (bond->params.mode == BOND_MODE_8023AD) {
1298 struct ad_info ad_info;
1299 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports) + 1;
1300 }
1301 else
1302 count = sprintf(buf, "\n") + 1;
1303
1304 return count;
1305}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001306static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001307
1308
1309/*
1310 * Show current 802.3ad actor key.
1311 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001312static ssize_t bonding_show_ad_actor_key(struct device *d,
1313 struct device_attribute *attr,
1314 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001315{
1316 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001317 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001318
1319 if (bond->params.mode == BOND_MODE_8023AD) {
1320 struct ad_info ad_info;
1321 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key) + 1;
1322 }
1323 else
1324 count = sprintf(buf, "\n") + 1;
1325
1326 return count;
1327}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001328static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001329
1330
1331/*
1332 * Show current 802.3ad partner key.
1333 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001334static ssize_t bonding_show_ad_partner_key(struct device *d,
1335 struct device_attribute *attr,
1336 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001337{
1338 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001339 struct bonding *bond = to_bond(d);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001340
1341 if (bond->params.mode == BOND_MODE_8023AD) {
1342 struct ad_info ad_info;
1343 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key) + 1;
1344 }
1345 else
1346 count = sprintf(buf, "\n") + 1;
1347
1348 return count;
1349}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001350static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001351
1352
1353/*
1354 * Show current 802.3ad partner mac.
1355 */
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001356static ssize_t bonding_show_ad_partner_mac(struct device *d,
1357 struct device_attribute *attr,
1358 char *buf)
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001359{
1360 int count = 0;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001361 struct bonding *bond = to_bond(d);
Joe Perches0795af52007-10-03 17:59:30 -07001362 DECLARE_MAC_BUF(mac);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001363
1364 if (bond->params.mode == BOND_MODE_8023AD) {
1365 struct ad_info ad_info;
1366 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
Joe Perches0795af52007-10-03 17:59:30 -07001367 count = sprintf(buf,"%s\n",
1368 print_mac(mac, ad_info.partner_system))
1369 + 1;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001370 }
1371 }
1372 else
1373 count = sprintf(buf, "\n") + 1;
1374
1375 return count;
1376}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001377static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001378
1379
1380
1381static struct attribute *per_bond_attrs[] = {
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001382 &dev_attr_slaves.attr,
1383 &dev_attr_mode.attr,
1384 &dev_attr_arp_validate.attr,
1385 &dev_attr_arp_interval.attr,
1386 &dev_attr_arp_ip_target.attr,
1387 &dev_attr_downdelay.attr,
1388 &dev_attr_updelay.attr,
1389 &dev_attr_lacp_rate.attr,
1390 &dev_attr_xmit_hash_policy.attr,
1391 &dev_attr_miimon.attr,
1392 &dev_attr_primary.attr,
1393 &dev_attr_use_carrier.attr,
1394 &dev_attr_active_slave.attr,
1395 &dev_attr_mii_status.attr,
1396 &dev_attr_ad_aggregator.attr,
1397 &dev_attr_ad_num_ports.attr,
1398 &dev_attr_ad_actor_key.attr,
1399 &dev_attr_ad_partner_key.attr,
1400 &dev_attr_ad_partner_mac.attr,
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001401 NULL,
1402};
1403
1404static struct attribute_group bonding_group = {
1405 .name = "bonding",
1406 .attrs = per_bond_attrs,
1407};
1408
1409/*
1410 * Initialize sysfs. This sets up the bonding_masters file in
1411 * /sys/class/net.
1412 */
1413int bond_create_sysfs(void)
1414{
1415 int ret = 0;
1416 struct bonding *firstbond;
1417
1418 init_rwsem(&bonding_rwsem);
1419
1420 /* get the netdev class pointer */
1421 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1422 if (!firstbond)
1423 return -ENODEV;
1424
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001425 netdev_class = firstbond->dev->dev.class;
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001426 if (!netdev_class)
1427 return -ENODEV;
1428
1429 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
Jay Vosburgh877cbd32007-01-19 18:15:47 -08001430 /*
1431 * Permit multiple loads of the module by ignoring failures to
1432 * create the bonding_masters sysfs file. Bonding devices
1433 * created by second or subsequent loads of the module will
1434 * not be listed in, or controllable by, bonding_masters, but
1435 * will have the usual "bonding" sysfs directory.
1436 *
1437 * This is done to preserve backwards compatibility for
1438 * initscripts/sysconfig, which load bonding multiple times to
1439 * configure multiple bonding devices.
1440 */
1441 if (ret == -EEXIST) {
1442 netdev_class = NULL;
1443 return 0;
1444 }
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001445
1446 return ret;
1447
1448}
1449
1450/*
1451 * Remove /sys/class/net/bonding_masters.
1452 */
1453void bond_destroy_sysfs(void)
1454{
1455 if (netdev_class)
1456 class_remove_file(netdev_class, &class_attr_bonding_masters);
1457}
1458
1459/*
1460 * Initialize sysfs for each bond. This sets up and registers
1461 * the 'bondctl' directory for each individual bond under /sys/class/net.
1462 */
1463int bond_create_sysfs_entry(struct bonding *bond)
1464{
1465 struct net_device *dev = bond->dev;
1466 int err;
1467
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001468 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001469 if (err) {
1470 printk(KERN_EMERG "eek! didn't create group!\n");
1471 }
1472
1473 if (expected_refcount < 1)
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001474 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001475
1476 return err;
1477}
1478/*
1479 * Remove sysfs entries for each bond.
1480 */
1481void bond_destroy_sysfs_entry(struct bonding *bond)
1482{
1483 struct net_device *dev = bond->dev;
1484
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001485 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
Mitch Williamsb76cdba2005-11-09 10:36:41 -08001486}
1487