bonding: refactor ARP active-backup monitor
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / bonding / bond_sysfs.c
CommitLineData
b76cdba9
MW
1
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 *
b76cdba9 22 */
b76cdba9
MW
23#include <linux/kernel.h>
24#include <linux/module.h>
b76cdba9
MW
25#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>
b76cdba9
MW
34#include <linux/ctype.h>
35#include <linux/inet.h>
36#include <linux/rtnetlink.h>
881d966b 37#include <net/net_namespace.h>
b76cdba9
MW
38
39/* #define BONDING_DEBUG 1 */
40#include "bonding.h"
43cb76d9 41#define to_dev(obj) container_of(obj,struct device,kobj)
b76cdba9
MW
42#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[];
f5b2b966 52extern struct bond_parm_tbl arp_validate_tbl[];
b76cdba9
MW
53
54static int expected_refcount = -1;
55static struct class *netdev_class;
56/*--------------------------- Data Structures -----------------------------*/
57
3a4fa0a2 58/* Bonding sysfs lock. Why can't we just use the subsystem lock?
b76cdba9
MW
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 */
b8843665 77static ssize_t bonding_show_bonds(struct class *cls, char *buf)
b76cdba9
MW
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;
b8843665 89 res += sprintf(buf + res, "++more++ ");
b76cdba9
MW
90 break;
91 }
b8843665 92 res += sprintf(buf + res, "%s ", bond->dev->name);
b76cdba9 93 }
1dcdcd69
WF
94 if (res)
95 buf[res-1] = '\n'; /* eat the leftover space */
b76cdba9
MW
96 up_read(&(bonding_rwsem));
97 return res;
98}
99
100/*
101 * "store" function for the bond_masters attribute. This is what
102 * creates and deletes entire bonds.
103 *
104 * The class parameter is ignored.
105 *
106 */
107
108static ssize_t bonding_store_bonds(struct class *cls, const char *buffer, size_t count)
109{
110 char command[IFNAMSIZ + 1] = {0, };
111 char *ifname;
027ea041 112 int rv, res = count;
b76cdba9 113 struct bonding *bond;
b76cdba9 114
b76cdba9
MW
115 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
116 ifname = command + 1;
117 if ((strlen(command) <= 1) ||
118 !dev_valid_name(ifname))
119 goto err_no_cmd;
120
121 if (command[0] == '+') {
b76cdba9
MW
122 printk(KERN_INFO DRV_NAME
123 ": %s is being created...\n", ifname);
0dd646fe 124 rv = bond_create(ifname, &bonding_defaults);
027ea041
JV
125 if (rv) {
126 printk(KERN_INFO DRV_NAME ": Bond creation failed.\n");
127 res = rv;
b76cdba9
MW
128 }
129 goto out;
130 }
131
132 if (command[0] == '-') {
027ea041
JV
133 rtnl_lock();
134 down_write(&bonding_rwsem);
135
0883beca 136 list_for_each_entry(bond, &bond_dev_list, bond_list)
b76cdba9 137 if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
b76cdba9
MW
138 /* check the ref count on the bond's kobject.
139 * If it's > expected, then there's a file open,
140 * and we have to fail.
141 */
43cb76d9 142 if (atomic_read(&bond->dev->dev.kobj.kref.refcount)
b76cdba9 143 > expected_refcount){
b76cdba9
MW
144 printk(KERN_INFO DRV_NAME
145 ": Unable remove bond %s due to open references.\n",
146 ifname);
147 res = -EPERM;
c4ebc66a 148 goto out_unlock;
b76cdba9
MW
149 }
150 printk(KERN_INFO DRV_NAME
151 ": %s is being deleted...\n",
152 bond->dev->name);
d90a162a 153 bond_destroy(bond);
c4ebc66a 154 goto out_unlock;
b76cdba9
MW
155 }
156
157 printk(KERN_ERR DRV_NAME
158 ": unable to delete non-existent bond %s\n", ifname);
159 res = -ENODEV;
c4ebc66a 160 goto out_unlock;
b76cdba9
MW
161 }
162
163err_no_cmd:
164 printk(KERN_ERR DRV_NAME
165 ": no command found in bonding_masters. Use +ifname or -ifname.\n");
c4ebc66a
JV
166 return -EPERM;
167
168out_unlock:
169 up_write(&bonding_rwsem);
170 rtnl_unlock();
b76cdba9
MW
171
172 /* Always return either count or an error. If you return 0, you'll
173 * get called forever, which is bad.
174 */
175out:
b76cdba9
MW
176 return res;
177}
178/* class attribute for bond_masters file. This ends up in /sys/class/net */
179static CLASS_ATTR(bonding_masters, S_IWUSR | S_IRUGO,
180 bonding_show_bonds, bonding_store_bonds);
181
182int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
183{
184 char linkname[IFNAMSIZ+7];
185 int ret = 0;
186
187 /* first, create a link from the slave back to the master */
43cb76d9 188 ret = sysfs_create_link(&(slave->dev.kobj), &(master->dev.kobj),
b76cdba9
MW
189 "master");
190 if (ret)
191 return ret;
192 /* next, create a link from the master to the slave */
193 sprintf(linkname,"slave_%s",slave->name);
43cb76d9 194 ret = sysfs_create_link(&(master->dev.kobj), &(slave->dev.kobj),
b76cdba9
MW
195 linkname);
196 return ret;
197
198}
199
200void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
201{
202 char linkname[IFNAMSIZ+7];
203
43cb76d9 204 sysfs_remove_link(&(slave->dev.kobj), "master");
b76cdba9 205 sprintf(linkname,"slave_%s",slave->name);
43cb76d9 206 sysfs_remove_link(&(master->dev.kobj), linkname);
b76cdba9
MW
207}
208
209
210/*
211 * Show the slaves in the current bond.
212 */
43cb76d9
GKH
213static ssize_t bonding_show_slaves(struct device *d,
214 struct device_attribute *attr, char *buf)
b76cdba9
MW
215{
216 struct slave *slave;
217 int i, res = 0;
43cb76d9 218 struct bonding *bond = to_bond(d);
b76cdba9 219
6603a6f2 220 read_lock(&bond->lock);
b76cdba9
MW
221 bond_for_each_slave(bond, slave, i) {
222 if (res > (PAGE_SIZE - IFNAMSIZ)) {
223 /* not enough space for another interface name */
224 if ((PAGE_SIZE - res) > 10)
225 res = PAGE_SIZE - 10;
7bd46508 226 res += sprintf(buf + res, "++more++ ");
b76cdba9
MW
227 break;
228 }
229 res += sprintf(buf + res, "%s ", slave->dev->name);
230 }
6603a6f2 231 read_unlock(&bond->lock);
1dcdcd69
WF
232 if (res)
233 buf[res-1] = '\n'; /* eat the leftover space */
b76cdba9
MW
234 return res;
235}
236
237/*
238 * Set the slaves in the current bond. The bond interface must be
239 * up for this to succeed.
240 * This function is largely the same flow as bonding_update_bonds().
241 */
43cb76d9
GKH
242static ssize_t bonding_store_slaves(struct device *d,
243 struct device_attribute *attr,
244 const char *buffer, size_t count)
b76cdba9
MW
245{
246 char command[IFNAMSIZ + 1] = { 0, };
247 char *ifname;
248 int i, res, found, ret = count;
3158bf7d 249 u32 original_mtu;
b76cdba9 250 struct slave *slave;
3418db7c 251 struct net_device *dev = NULL;
43cb76d9 252 struct bonding *bond = to_bond(d);
b76cdba9
MW
253
254 /* Quick sanity check -- is the bond interface up? */
255 if (!(bond->dev->flags & IFF_UP)) {
6b1bf096
MS
256 printk(KERN_WARNING DRV_NAME
257 ": %s: doing slave updates when interface is down.\n",
b76cdba9 258 bond->dev->name);
b76cdba9
MW
259 }
260
261 /* Note: We can't hold bond->lock here, as bond_create grabs it. */
262
027ea041
JV
263 rtnl_lock();
264 down_write(&(bonding_rwsem));
265
b76cdba9
MW
266 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
267 ifname = command + 1;
268 if ((strlen(command) <= 1) ||
269 !dev_valid_name(ifname))
270 goto err_no_cmd;
271
272 if (command[0] == '+') {
273
274 /* Got a slave name in ifname. Is it already in the list? */
275 found = 0;
6603a6f2 276 read_lock(&bond->lock);
b76cdba9
MW
277 bond_for_each_slave(bond, slave, i)
278 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
279 printk(KERN_ERR DRV_NAME
280 ": %s: Interface %s is already enslaved!\n",
281 bond->dev->name, ifname);
282 ret = -EPERM;
6603a6f2 283 read_unlock(&bond->lock);
b76cdba9
MW
284 goto out;
285 }
286
6603a6f2 287 read_unlock(&bond->lock);
b76cdba9
MW
288 printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
289 bond->dev->name, ifname);
881d966b 290 dev = dev_get_by_name(&init_net, ifname);
b76cdba9
MW
291 if (!dev) {
292 printk(KERN_INFO DRV_NAME
293 ": %s: Interface %s does not exist!\n",
294 bond->dev->name, ifname);
295 ret = -EPERM;
296 goto out;
297 }
298 else
299 dev_put(dev);
300
301 if (dev->flags & IFF_UP) {
302 printk(KERN_ERR DRV_NAME
303 ": %s: Error: Unable to enslave %s "
304 "because it is already up.\n",
305 bond->dev->name, dev->name);
306 ret = -EPERM;
307 goto out;
308 }
309 /* If this is the first slave, then we need to set
310 the master's hardware address to be the same as the
311 slave's. */
312 if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
313 memcpy(bond->dev->dev_addr, dev->dev_addr,
314 dev->addr_len);
315 }
316
317 /* Set the slave's MTU to match the bond */
3158bf7d 318 original_mtu = dev->mtu;
b76cdba9
MW
319 if (dev->mtu != bond->dev->mtu) {
320 if (dev->change_mtu) {
321 res = dev->change_mtu(dev,
322 bond->dev->mtu);
323 if (res) {
324 ret = res;
325 goto out;
326 }
327 } else {
328 dev->mtu = bond->dev->mtu;
329 }
330 }
b76cdba9 331 res = bond_enslave(bond->dev, dev);
3158bf7d
MS
332 bond_for_each_slave(bond, slave, i)
333 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0)
334 slave->original_mtu = original_mtu;
b76cdba9
MW
335 if (res) {
336 ret = res;
337 }
338 goto out;
339 }
340
341 if (command[0] == '-') {
342 dev = NULL;
6952d892 343 original_mtu = 0;
b76cdba9
MW
344 bond_for_each_slave(bond, slave, i)
345 if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
346 dev = slave->dev;
3158bf7d 347 original_mtu = slave->original_mtu;
b76cdba9
MW
348 break;
349 }
350 if (dev) {
351 printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
352 bond->dev->name, dev->name);
d90a162a
MS
353 if (bond->setup_by_slave)
354 res = bond_release_and_destroy(bond->dev, dev);
355 else
356 res = bond_release(bond->dev, dev);
b76cdba9
MW
357 if (res) {
358 ret = res;
359 goto out;
360 }
361 /* set the slave MTU to the default */
362 if (dev->change_mtu) {
3158bf7d 363 dev->change_mtu(dev, original_mtu);
b76cdba9 364 } else {
3158bf7d 365 dev->mtu = original_mtu;
b76cdba9
MW
366 }
367 }
368 else {
369 printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
370 ifname, bond->dev->name);
371 ret = -ENODEV;
372 }
373 goto out;
374 }
375
376err_no_cmd:
377 printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
378 ret = -EPERM;
379
380out:
027ea041
JV
381 up_write(&(bonding_rwsem));
382 rtnl_unlock();
b76cdba9
MW
383 return ret;
384}
385
43cb76d9 386static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);
b76cdba9
MW
387
388/*
389 * Show and set the bonding mode. The bond interface must be down to
390 * change the mode.
391 */
43cb76d9
GKH
392static ssize_t bonding_show_mode(struct device *d,
393 struct device_attribute *attr, char *buf)
b76cdba9 394{
43cb76d9 395 struct bonding *bond = to_bond(d);
b76cdba9
MW
396
397 return sprintf(buf, "%s %d\n",
398 bond_mode_tbl[bond->params.mode].modename,
7bd46508 399 bond->params.mode);
b76cdba9
MW
400}
401
43cb76d9
GKH
402static ssize_t bonding_store_mode(struct device *d,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
b76cdba9
MW
405{
406 int new_value, ret = count;
43cb76d9 407 struct bonding *bond = to_bond(d);
b76cdba9
MW
408
409 if (bond->dev->flags & IFF_UP) {
410 printk(KERN_ERR DRV_NAME
411 ": unable to update mode of %s because interface is up.\n",
412 bond->dev->name);
413 ret = -EPERM;
414 goto out;
415 }
416
ece95f7f 417 new_value = bond_parse_parm(buf, bond_mode_tbl);
b76cdba9
MW
418 if (new_value < 0) {
419 printk(KERN_ERR DRV_NAME
420 ": %s: Ignoring invalid mode value %.*s.\n",
421 bond->dev->name,
422 (int)strlen(buf) - 1, buf);
423 ret = -EINVAL;
424 goto out;
425 } else {
8f903c70
JV
426 if (bond->params.mode == BOND_MODE_8023AD)
427 bond_unset_master_3ad_flags(bond);
428
429 if (bond->params.mode == BOND_MODE_ALB)
430 bond_unset_master_alb_flags(bond);
431
b76cdba9
MW
432 bond->params.mode = new_value;
433 bond_set_mode_ops(bond, bond->params.mode);
434 printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
435 bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
436 }
437out:
438 return ret;
439}
43cb76d9 440static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);
b76cdba9
MW
441
442/*
443 * Show and set the bonding transmit hash method. The bond interface must be down to
444 * change the xmit hash policy.
445 */
43cb76d9
GKH
446static ssize_t bonding_show_xmit_hash(struct device *d,
447 struct device_attribute *attr,
448 char *buf)
b76cdba9 449{
43cb76d9 450 struct bonding *bond = to_bond(d);
b76cdba9 451
8e4b9329
WF
452 return sprintf(buf, "%s %d\n",
453 xmit_hashtype_tbl[bond->params.xmit_policy].modename,
454 bond->params.xmit_policy);
b76cdba9
MW
455}
456
43cb76d9
GKH
457static ssize_t bonding_store_xmit_hash(struct device *d,
458 struct device_attribute *attr,
459 const char *buf, size_t count)
b76cdba9
MW
460{
461 int new_value, ret = count;
43cb76d9 462 struct bonding *bond = to_bond(d);
b76cdba9
MW
463
464 if (bond->dev->flags & IFF_UP) {
465 printk(KERN_ERR DRV_NAME
466 "%s: Interface is up. Unable to update xmit policy.\n",
467 bond->dev->name);
468 ret = -EPERM;
469 goto out;
470 }
471
ece95f7f 472 new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
b76cdba9
MW
473 if (new_value < 0) {
474 printk(KERN_ERR DRV_NAME
475 ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
476 bond->dev->name,
477 (int)strlen(buf) - 1, buf);
478 ret = -EINVAL;
479 goto out;
480 } else {
481 bond->params.xmit_policy = new_value;
482 bond_set_mode_ops(bond, bond->params.mode);
483 printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
484 bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
485 }
486out:
487 return ret;
488}
43cb76d9 489static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);
b76cdba9 490
f5b2b966
JV
491/*
492 * Show and set arp_validate.
493 */
43cb76d9
GKH
494static ssize_t bonding_show_arp_validate(struct device *d,
495 struct device_attribute *attr,
496 char *buf)
f5b2b966 497{
43cb76d9 498 struct bonding *bond = to_bond(d);
f5b2b966
JV
499
500 return sprintf(buf, "%s %d\n",
501 arp_validate_tbl[bond->params.arp_validate].modename,
7bd46508 502 bond->params.arp_validate);
f5b2b966
JV
503}
504
43cb76d9
GKH
505static ssize_t bonding_store_arp_validate(struct device *d,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
f5b2b966
JV
508{
509 int new_value;
43cb76d9 510 struct bonding *bond = to_bond(d);
f5b2b966 511
ece95f7f 512 new_value = bond_parse_parm(buf, arp_validate_tbl);
f5b2b966
JV
513 if (new_value < 0) {
514 printk(KERN_ERR DRV_NAME
515 ": %s: Ignoring invalid arp_validate value %s\n",
516 bond->dev->name, buf);
517 return -EINVAL;
518 }
519 if (new_value && (bond->params.mode != BOND_MODE_ACTIVEBACKUP)) {
520 printk(KERN_ERR DRV_NAME
521 ": %s: arp_validate only supported in active-backup mode.\n",
522 bond->dev->name);
523 return -EINVAL;
524 }
525 printk(KERN_INFO DRV_NAME ": %s: setting arp_validate to %s (%d).\n",
526 bond->dev->name, arp_validate_tbl[new_value].modename,
527 new_value);
528
529 if (!bond->params.arp_validate && new_value) {
530 bond_register_arp(bond);
531 } else if (bond->params.arp_validate && !new_value) {
532 bond_unregister_arp(bond);
533 }
534
535 bond->params.arp_validate = new_value;
536
537 return count;
538}
539
43cb76d9 540static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate, bonding_store_arp_validate);
f5b2b966 541
dd957c57
JV
542/*
543 * Show and store fail_over_mac. User only allowed to change the
544 * value when there are no slaves.
545 */
546static ssize_t bonding_show_fail_over_mac(struct device *d, struct device_attribute *attr, char *buf)
547{
548 struct bonding *bond = to_bond(d);
549
550 return sprintf(buf, "%d\n", bond->params.fail_over_mac) + 1;
551}
552
553static ssize_t bonding_store_fail_over_mac(struct device *d, struct device_attribute *attr, const char *buf, size_t count)
554{
555 int new_value;
556 int ret = count;
557 struct bonding *bond = to_bond(d);
558
559 if (bond->slave_cnt != 0) {
560 printk(KERN_ERR DRV_NAME
561 ": %s: Can't alter fail_over_mac with slaves in bond.\n",
562 bond->dev->name);
563 ret = -EPERM;
564 goto out;
565 }
566
567 if (sscanf(buf, "%d", &new_value) != 1) {
568 printk(KERN_ERR DRV_NAME
569 ": %s: no fail_over_mac value specified.\n",
570 bond->dev->name);
571 ret = -EINVAL;
572 goto out;
573 }
574
575 if ((new_value == 0) || (new_value == 1)) {
576 bond->params.fail_over_mac = new_value;
577 printk(KERN_INFO DRV_NAME ": %s: Setting fail_over_mac to %d.\n",
578 bond->dev->name, new_value);
579 } else {
580 printk(KERN_INFO DRV_NAME
581 ": %s: Ignoring invalid fail_over_mac value %d.\n",
582 bond->dev->name, new_value);
583 }
584out:
585 return ret;
586}
587
588static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR, bonding_show_fail_over_mac, bonding_store_fail_over_mac);
589
b76cdba9
MW
590/*
591 * Show and set the arp timer interval. There are two tricky bits
592 * here. First, if ARP monitoring is activated, then we must disable
593 * MII monitoring. Second, if the ARP timer isn't running, we must
594 * start it.
595 */
43cb76d9
GKH
596static ssize_t bonding_show_arp_interval(struct device *d,
597 struct device_attribute *attr,
598 char *buf)
b76cdba9 599{
43cb76d9 600 struct bonding *bond = to_bond(d);
b76cdba9 601
7bd46508 602 return sprintf(buf, "%d\n", bond->params.arp_interval);
b76cdba9
MW
603}
604
43cb76d9
GKH
605static ssize_t bonding_store_arp_interval(struct device *d,
606 struct device_attribute *attr,
607 const char *buf, size_t count)
b76cdba9
MW
608{
609 int new_value, ret = count;
43cb76d9 610 struct bonding *bond = to_bond(d);
b76cdba9
MW
611
612 if (sscanf(buf, "%d", &new_value) != 1) {
613 printk(KERN_ERR DRV_NAME
614 ": %s: no arp_interval value specified.\n",
615 bond->dev->name);
616 ret = -EINVAL;
617 goto out;
618 }
619 if (new_value < 0) {
620 printk(KERN_ERR DRV_NAME
621 ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
622 bond->dev->name, new_value, INT_MAX);
623 ret = -EINVAL;
624 goto out;
625 }
626
627 printk(KERN_INFO DRV_NAME
628 ": %s: Setting ARP monitoring interval to %d.\n",
629 bond->dev->name, new_value);
630 bond->params.arp_interval = new_value;
631 if (bond->params.miimon) {
632 printk(KERN_INFO DRV_NAME
633 ": %s: ARP monitoring cannot be used with MII monitoring. "
634 "%s Disabling MII monitoring.\n",
635 bond->dev->name, bond->dev->name);
636 bond->params.miimon = 0;
1b76b316
JV
637 if (delayed_work_pending(&bond->mii_work)) {
638 cancel_delayed_work(&bond->mii_work);
639 flush_workqueue(bond->wq);
b76cdba9
MW
640 }
641 }
642 if (!bond->params.arp_targets[0]) {
643 printk(KERN_INFO DRV_NAME
644 ": %s: ARP monitoring has been set up, "
645 "but no ARP targets have been specified.\n",
646 bond->dev->name);
647 }
648 if (bond->dev->flags & IFF_UP) {
649 /* If the interface is up, we may need to fire off
650 * the ARP timer. If the interface is down, the
651 * timer will get fired off when the open function
652 * is called.
653 */
1b76b316
JV
654 if (!delayed_work_pending(&bond->arp_work)) {
655 if (bond->params.mode == BOND_MODE_ACTIVEBACKUP)
656 INIT_DELAYED_WORK(&bond->arp_work,
657 bond_activebackup_arp_mon);
658 else
659 INIT_DELAYED_WORK(&bond->arp_work,
660 bond_loadbalance_arp_mon);
661
662 queue_delayed_work(bond->wq, &bond->arp_work, 0);
b76cdba9
MW
663 }
664 }
665
666out:
667 return ret;
668}
43cb76d9 669static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);
b76cdba9
MW
670
671/*
672 * Show and set the arp targets.
673 */
43cb76d9
GKH
674static ssize_t bonding_show_arp_targets(struct device *d,
675 struct device_attribute *attr,
676 char *buf)
b76cdba9
MW
677{
678 int i, res = 0;
43cb76d9 679 struct bonding *bond = to_bond(d);
b76cdba9
MW
680
681 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
682 if (bond->params.arp_targets[i])
683 res += sprintf(buf + res, "%u.%u.%u.%u ",
684 NIPQUAD(bond->params.arp_targets[i]));
685 }
1dcdcd69
WF
686 if (res)
687 buf[res-1] = '\n'; /* eat the leftover space */
b76cdba9
MW
688 return res;
689}
690
43cb76d9
GKH
691static ssize_t bonding_store_arp_targets(struct device *d,
692 struct device_attribute *attr,
693 const char *buf, size_t count)
b76cdba9 694{
d3bb52b0 695 __be32 newtarget;
b76cdba9 696 int i = 0, done = 0, ret = count;
43cb76d9 697 struct bonding *bond = to_bond(d);
d3bb52b0 698 __be32 *targets;
b76cdba9
MW
699
700 targets = bond->params.arp_targets;
701 newtarget = in_aton(buf + 1);
702 /* look for adds */
703 if (buf[0] == '+') {
d3bb52b0 704 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
b76cdba9
MW
705 printk(KERN_ERR DRV_NAME
706 ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
707 bond->dev->name, NIPQUAD(newtarget));
708 ret = -EINVAL;
709 goto out;
710 }
711 /* look for an empty slot to put the target in, and check for dupes */
712 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
713 if (targets[i] == newtarget) { /* duplicate */
714 printk(KERN_ERR DRV_NAME
715 ": %s: ARP target %u.%u.%u.%u is already present\n",
716 bond->dev->name, NIPQUAD(newtarget));
717 if (done)
718 targets[i] = 0;
719 ret = -EINVAL;
720 goto out;
721 }
722 if (targets[i] == 0 && !done) {
723 printk(KERN_INFO DRV_NAME
724 ": %s: adding ARP target %d.%d.%d.%d.\n",
725 bond->dev->name, NIPQUAD(newtarget));
726 done = 1;
727 targets[i] = newtarget;
728 }
729 }
730 if (!done) {
731 printk(KERN_ERR DRV_NAME
732 ": %s: ARP target table is full!\n",
733 bond->dev->name);
734 ret = -EINVAL;
735 goto out;
736 }
737
738 }
739 else if (buf[0] == '-') {
d3bb52b0 740 if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
b76cdba9
MW
741 printk(KERN_ERR DRV_NAME
742 ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
743 bond->dev->name, NIPQUAD(newtarget));
744 ret = -EINVAL;
745 goto out;
746 }
747
748 for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
749 if (targets[i] == newtarget) {
750 printk(KERN_INFO DRV_NAME
751 ": %s: removing ARP target %d.%d.%d.%d.\n",
752 bond->dev->name, NIPQUAD(newtarget));
753 targets[i] = 0;
754 done = 1;
755 }
756 }
757 if (!done) {
758 printk(KERN_INFO DRV_NAME
759 ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
760 bond->dev->name, NIPQUAD(newtarget));
761 ret = -EINVAL;
762 goto out;
763 }
764 }
765 else {
766 printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
767 bond->dev->name);
768 ret = -EPERM;
769 goto out;
770 }
771
772out:
773 return ret;
774}
43cb76d9 775static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
b76cdba9
MW
776
777/*
778 * Show and set the up and down delays. These must be multiples of the
779 * MII monitoring value, and are stored internally as the multiplier.
780 * Thus, we must translate to MS for the real world.
781 */
43cb76d9
GKH
782static ssize_t bonding_show_downdelay(struct device *d,
783 struct device_attribute *attr,
784 char *buf)
b76cdba9 785{
43cb76d9 786 struct bonding *bond = to_bond(d);
b76cdba9 787
7bd46508 788 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
b76cdba9
MW
789}
790
43cb76d9
GKH
791static ssize_t bonding_store_downdelay(struct device *d,
792 struct device_attribute *attr,
793 const char *buf, size_t count)
b76cdba9
MW
794{
795 int new_value, ret = count;
43cb76d9 796 struct bonding *bond = to_bond(d);
b76cdba9
MW
797
798 if (!(bond->params.miimon)) {
799 printk(KERN_ERR DRV_NAME
800 ": %s: Unable to set down delay as MII monitoring is disabled\n",
801 bond->dev->name);
802 ret = -EPERM;
803 goto out;
804 }
805
806 if (sscanf(buf, "%d", &new_value) != 1) {
807 printk(KERN_ERR DRV_NAME
808 ": %s: no down delay value specified.\n",
809 bond->dev->name);
810 ret = -EINVAL;
811 goto out;
812 }
813 if (new_value < 0) {
814 printk(KERN_ERR DRV_NAME
815 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
816 bond->dev->name, new_value, 1, INT_MAX);
817 ret = -EINVAL;
818 goto out;
819 } else {
820 if ((new_value % bond->params.miimon) != 0) {
821 printk(KERN_WARNING DRV_NAME
822 ": %s: Warning: down delay (%d) is not a multiple "
823 "of miimon (%d), delay rounded to %d ms\n",
824 bond->dev->name, new_value, bond->params.miimon,
825 (new_value / bond->params.miimon) *
826 bond->params.miimon);
827 }
828 bond->params.downdelay = new_value / bond->params.miimon;
829 printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
830 bond->dev->name, bond->params.downdelay * bond->params.miimon);
831
832 }
833
834out:
835 return ret;
836}
43cb76d9 837static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);
b76cdba9 838
43cb76d9
GKH
839static ssize_t bonding_show_updelay(struct device *d,
840 struct device_attribute *attr,
841 char *buf)
b76cdba9 842{
43cb76d9 843 struct bonding *bond = to_bond(d);
b76cdba9 844
7bd46508 845 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
b76cdba9
MW
846
847}
848
43cb76d9
GKH
849static ssize_t bonding_store_updelay(struct device *d,
850 struct device_attribute *attr,
851 const char *buf, size_t count)
b76cdba9
MW
852{
853 int new_value, ret = count;
43cb76d9 854 struct bonding *bond = to_bond(d);
b76cdba9
MW
855
856 if (!(bond->params.miimon)) {
857 printk(KERN_ERR DRV_NAME
858 ": %s: Unable to set up delay as MII monitoring is disabled\n",
859 bond->dev->name);
860 ret = -EPERM;
861 goto out;
862 }
863
864 if (sscanf(buf, "%d", &new_value) != 1) {
865 printk(KERN_ERR DRV_NAME
866 ": %s: no up delay value specified.\n",
867 bond->dev->name);
868 ret = -EINVAL;
869 goto out;
870 }
871 if (new_value < 0) {
872 printk(KERN_ERR DRV_NAME
873 ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
874 bond->dev->name, new_value, 1, INT_MAX);
875 ret = -EINVAL;
876 goto out;
877 } else {
878 if ((new_value % bond->params.miimon) != 0) {
879 printk(KERN_WARNING DRV_NAME
880 ": %s: Warning: up delay (%d) is not a multiple "
881 "of miimon (%d), updelay rounded to %d ms\n",
882 bond->dev->name, new_value, bond->params.miimon,
883 (new_value / bond->params.miimon) *
884 bond->params.miimon);
885 }
886 bond->params.updelay = new_value / bond->params.miimon;
887 printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
888 bond->dev->name, bond->params.updelay * bond->params.miimon);
889
890 }
891
892out:
893 return ret;
894}
43cb76d9 895static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);
b76cdba9
MW
896
897/*
898 * Show and set the LACP interval. Interface must be down, and the mode
899 * must be set to 802.3ad mode.
900 */
43cb76d9
GKH
901static ssize_t bonding_show_lacp(struct device *d,
902 struct device_attribute *attr,
903 char *buf)
b76cdba9 904{
43cb76d9 905 struct bonding *bond = to_bond(d);
b76cdba9
MW
906
907 return sprintf(buf, "%s %d\n",
908 bond_lacp_tbl[bond->params.lacp_fast].modename,
7bd46508 909 bond->params.lacp_fast);
b76cdba9
MW
910}
911
43cb76d9
GKH
912static ssize_t bonding_store_lacp(struct device *d,
913 struct device_attribute *attr,
914 const char *buf, size_t count)
b76cdba9
MW
915{
916 int new_value, ret = count;
43cb76d9 917 struct bonding *bond = to_bond(d);
b76cdba9
MW
918
919 if (bond->dev->flags & IFF_UP) {
920 printk(KERN_ERR DRV_NAME
921 ": %s: Unable to update LACP rate because interface is up.\n",
922 bond->dev->name);
923 ret = -EPERM;
924 goto out;
925 }
926
927 if (bond->params.mode != BOND_MODE_8023AD) {
928 printk(KERN_ERR DRV_NAME
929 ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
930 bond->dev->name);
931 ret = -EPERM;
932 goto out;
933 }
934
ece95f7f 935 new_value = bond_parse_parm(buf, bond_lacp_tbl);
b76cdba9
MW
936
937 if ((new_value == 1) || (new_value == 0)) {
938 bond->params.lacp_fast = new_value;
939 printk(KERN_INFO DRV_NAME
940 ": %s: Setting LACP rate to %s (%d).\n",
941 bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
942 } else {
943 printk(KERN_ERR DRV_NAME
944 ": %s: Ignoring invalid LACP rate value %.*s.\n",
945 bond->dev->name, (int)strlen(buf) - 1, buf);
946 ret = -EINVAL;
947 }
948out:
949 return ret;
950}
43cb76d9 951static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);
b76cdba9 952
7893b249
MS
953/*
954 * Show and set the number of grat ARP to send after a failover event.
955 */
956static ssize_t bonding_show_n_grat_arp(struct device *d,
957 struct device_attribute *attr,
958 char *buf)
959{
960 struct bonding *bond = to_bond(d);
961
962 return sprintf(buf, "%d\n", bond->params.num_grat_arp);
963}
964
965static ssize_t bonding_store_n_grat_arp(struct device *d,
966 struct device_attribute *attr,
967 const char *buf, size_t count)
968{
969 int new_value, ret = count;
970 struct bonding *bond = to_bond(d);
971
972 if (sscanf(buf, "%d", &new_value) != 1) {
973 printk(KERN_ERR DRV_NAME
974 ": %s: no num_grat_arp value specified.\n",
975 bond->dev->name);
976 ret = -EINVAL;
977 goto out;
978 }
979 if (new_value < 0 || new_value > 255) {
980 printk(KERN_ERR DRV_NAME
981 ": %s: Invalid num_grat_arp value %d not in range 0-255; rejected.\n",
982 bond->dev->name, new_value);
983 ret = -EINVAL;
984 goto out;
985 } else {
986 bond->params.num_grat_arp = new_value;
987 }
988out:
989 return ret;
990}
991static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR, bonding_show_n_grat_arp, bonding_store_n_grat_arp);
b76cdba9
MW
992/*
993 * Show and set the MII monitor interval. There are two tricky bits
994 * here. First, if MII monitoring is activated, then we must disable
995 * ARP monitoring. Second, if the timer isn't running, we must
996 * start it.
997 */
43cb76d9
GKH
998static ssize_t bonding_show_miimon(struct device *d,
999 struct device_attribute *attr,
1000 char *buf)
b76cdba9 1001{
43cb76d9 1002 struct bonding *bond = to_bond(d);
b76cdba9 1003
7bd46508 1004 return sprintf(buf, "%d\n", bond->params.miimon);
b76cdba9
MW
1005}
1006
43cb76d9
GKH
1007static ssize_t bonding_store_miimon(struct device *d,
1008 struct device_attribute *attr,
1009 const char *buf, size_t count)
b76cdba9
MW
1010{
1011 int new_value, ret = count;
43cb76d9 1012 struct bonding *bond = to_bond(d);
b76cdba9
MW
1013
1014 if (sscanf(buf, "%d", &new_value) != 1) {
1015 printk(KERN_ERR DRV_NAME
1016 ": %s: no miimon value specified.\n",
1017 bond->dev->name);
1018 ret = -EINVAL;
1019 goto out;
1020 }
1021 if (new_value < 0) {
1022 printk(KERN_ERR DRV_NAME
1023 ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1024 bond->dev->name, new_value, 1, INT_MAX);
1025 ret = -EINVAL;
1026 goto out;
1027 } else {
1028 printk(KERN_INFO DRV_NAME
1029 ": %s: Setting MII monitoring interval to %d.\n",
1030 bond->dev->name, new_value);
1031 bond->params.miimon = new_value;
1032 if(bond->params.updelay)
1033 printk(KERN_INFO DRV_NAME
1034 ": %s: Note: Updating updelay (to %d) "
1035 "since it is a multiple of the miimon value.\n",
1036 bond->dev->name,
1037 bond->params.updelay * bond->params.miimon);
1038 if(bond->params.downdelay)
1039 printk(KERN_INFO DRV_NAME
1040 ": %s: Note: Updating downdelay (to %d) "
1041 "since it is a multiple of the miimon value.\n",
1042 bond->dev->name,
1043 bond->params.downdelay * bond->params.miimon);
1044 if (bond->params.arp_interval) {
1045 printk(KERN_INFO DRV_NAME
1046 ": %s: MII monitoring cannot be used with "
1047 "ARP monitoring. Disabling ARP monitoring...\n",
1048 bond->dev->name);
1049 bond->params.arp_interval = 0;
f5b2b966
JV
1050 if (bond->params.arp_validate) {
1051 bond_unregister_arp(bond);
1052 bond->params.arp_validate =
1053 BOND_ARP_VALIDATE_NONE;
1054 }
1b76b316
JV
1055 if (delayed_work_pending(&bond->arp_work)) {
1056 cancel_delayed_work(&bond->arp_work);
1057 flush_workqueue(bond->wq);
b76cdba9
MW
1058 }
1059 }
1060
1061 if (bond->dev->flags & IFF_UP) {
1062 /* If the interface is up, we may need to fire off
1063 * the MII timer. If the interface is down, the
1064 * timer will get fired off when the open function
1065 * is called.
1066 */
1b76b316
JV
1067 if (!delayed_work_pending(&bond->mii_work)) {
1068 INIT_DELAYED_WORK(&bond->mii_work,
1069 bond_mii_monitor);
1070 queue_delayed_work(bond->wq,
1071 &bond->mii_work, 0);
b76cdba9
MW
1072 }
1073 }
1074 }
1075out:
1076 return ret;
1077}
43cb76d9 1078static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);
b76cdba9
MW
1079
1080/*
1081 * Show and set the primary slave. The store function is much
1082 * simpler than bonding_store_slaves function because it only needs to
1083 * handle one interface name.
1084 * The bond must be a mode that supports a primary for this be
1085 * set.
1086 */
43cb76d9
GKH
1087static ssize_t bonding_show_primary(struct device *d,
1088 struct device_attribute *attr,
1089 char *buf)
b76cdba9
MW
1090{
1091 int count = 0;
43cb76d9 1092 struct bonding *bond = to_bond(d);
b76cdba9
MW
1093
1094 if (bond->primary_slave)
7bd46508 1095 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
b76cdba9
MW
1096
1097 return count;
1098}
1099
43cb76d9
GKH
1100static ssize_t bonding_store_primary(struct device *d,
1101 struct device_attribute *attr,
1102 const char *buf, size_t count)
b76cdba9
MW
1103{
1104 int i;
1105 struct slave *slave;
43cb76d9 1106 struct bonding *bond = to_bond(d);
b76cdba9 1107
e934dd78
JV
1108 rtnl_lock();
1109 read_lock(&bond->lock);
1110 write_lock_bh(&bond->curr_slave_lock);
1111
b76cdba9
MW
1112 if (!USES_PRIMARY(bond->params.mode)) {
1113 printk(KERN_INFO DRV_NAME
1114 ": %s: Unable to set primary slave; %s is in mode %d\n",
1115 bond->dev->name, bond->dev->name, bond->params.mode);
1116 } else {
1117 bond_for_each_slave(bond, slave, i) {
1118 if (strnicmp
1119 (slave->dev->name, buf,
1120 strlen(slave->dev->name)) == 0) {
1121 printk(KERN_INFO DRV_NAME
1122 ": %s: Setting %s as primary slave.\n",
1123 bond->dev->name, slave->dev->name);
1124 bond->primary_slave = slave;
1125 bond_select_active_slave(bond);
1126 goto out;
1127 }
1128 }
1129
1130 /* if we got here, then we didn't match the name of any slave */
1131
1132 if (strlen(buf) == 0 || buf[0] == '\n') {
1133 printk(KERN_INFO DRV_NAME
1134 ": %s: Setting primary slave to None.\n",
1135 bond->dev->name);
3418db7c 1136 bond->primary_slave = NULL;
b76cdba9
MW
1137 bond_select_active_slave(bond);
1138 } else {
1139 printk(KERN_INFO DRV_NAME
1140 ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
1141 bond->dev->name, (int)strlen(buf) - 1, buf);
1142 }
1143 }
1144out:
e934dd78
JV
1145 write_unlock_bh(&bond->curr_slave_lock);
1146 read_unlock(&bond->lock);
6603a6f2
JV
1147 rtnl_unlock();
1148
b76cdba9
MW
1149 return count;
1150}
43cb76d9 1151static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);
b76cdba9
MW
1152
1153/*
1154 * Show and set the use_carrier flag.
1155 */
43cb76d9
GKH
1156static ssize_t bonding_show_carrier(struct device *d,
1157 struct device_attribute *attr,
1158 char *buf)
b76cdba9 1159{
43cb76d9 1160 struct bonding *bond = to_bond(d);
b76cdba9 1161
7bd46508 1162 return sprintf(buf, "%d\n", bond->params.use_carrier);
b76cdba9
MW
1163}
1164
43cb76d9
GKH
1165static ssize_t bonding_store_carrier(struct device *d,
1166 struct device_attribute *attr,
1167 const char *buf, size_t count)
b76cdba9
MW
1168{
1169 int new_value, ret = count;
43cb76d9 1170 struct bonding *bond = to_bond(d);
b76cdba9
MW
1171
1172
1173 if (sscanf(buf, "%d", &new_value) != 1) {
1174 printk(KERN_ERR DRV_NAME
1175 ": %s: no use_carrier value specified.\n",
1176 bond->dev->name);
1177 ret = -EINVAL;
1178 goto out;
1179 }
1180 if ((new_value == 0) || (new_value == 1)) {
1181 bond->params.use_carrier = new_value;
1182 printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
1183 bond->dev->name, new_value);
1184 } else {
1185 printk(KERN_INFO DRV_NAME
1186 ": %s: Ignoring invalid use_carrier value %d.\n",
1187 bond->dev->name, new_value);
1188 }
1189out:
1190 return count;
1191}
43cb76d9 1192static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);
b76cdba9
MW
1193
1194
1195/*
1196 * Show and set currently active_slave.
1197 */
43cb76d9
GKH
1198static ssize_t bonding_show_active_slave(struct device *d,
1199 struct device_attribute *attr,
1200 char *buf)
b76cdba9
MW
1201{
1202 struct slave *curr;
43cb76d9 1203 struct bonding *bond = to_bond(d);
16cd0160 1204 int count = 0;
b76cdba9 1205
b76cdba9
MW
1206 read_lock(&bond->curr_slave_lock);
1207 curr = bond->curr_active_slave;
1208 read_unlock(&bond->curr_slave_lock);
1209
1210 if (USES_PRIMARY(bond->params.mode) && curr)
7bd46508 1211 count = sprintf(buf, "%s\n", curr->dev->name);
b76cdba9
MW
1212 return count;
1213}
1214
43cb76d9
GKH
1215static ssize_t bonding_store_active_slave(struct device *d,
1216 struct device_attribute *attr,
1217 const char *buf, size_t count)
b76cdba9
MW
1218{
1219 int i;
1220 struct slave *slave;
1221 struct slave *old_active = NULL;
1222 struct slave *new_active = NULL;
43cb76d9 1223 struct bonding *bond = to_bond(d);
b76cdba9 1224
1466a219 1225 rtnl_lock();
e934dd78
JV
1226 read_lock(&bond->lock);
1227 write_lock_bh(&bond->curr_slave_lock);
1466a219 1228
b76cdba9
MW
1229 if (!USES_PRIMARY(bond->params.mode)) {
1230 printk(KERN_INFO DRV_NAME
1231 ": %s: Unable to change active slave; %s is in mode %d\n",
1232 bond->dev->name, bond->dev->name, bond->params.mode);
1233 } else {
1234 bond_for_each_slave(bond, slave, i) {
1235 if (strnicmp
1236 (slave->dev->name, buf,
1237 strlen(slave->dev->name)) == 0) {
1238 old_active = bond->curr_active_slave;
1239 new_active = slave;
a50d8de2 1240 if (new_active == old_active) {
b76cdba9
MW
1241 /* do nothing */
1242 printk(KERN_INFO DRV_NAME
1243 ": %s: %s is already the current active slave.\n",
1244 bond->dev->name, slave->dev->name);
1245 goto out;
1246 }
1247 else {
1248 if ((new_active) &&
1249 (old_active) &&
1250 (new_active->link == BOND_LINK_UP) &&
1251 IS_UP(new_active->dev)) {
1252 printk(KERN_INFO DRV_NAME
1253 ": %s: Setting %s as active slave.\n",
1254 bond->dev->name, slave->dev->name);
1255 bond_change_active_slave(bond, new_active);
1256 }
1257 else {
1258 printk(KERN_INFO DRV_NAME
1259 ": %s: Could not set %s as active slave; "
1260 "either %s is down or the link is down.\n",
1261 bond->dev->name, slave->dev->name,
1262 slave->dev->name);
1263 }
1264 goto out;
1265 }
1266 }
1267 }
1268
1269 /* if we got here, then we didn't match the name of any slave */
1270
1271 if (strlen(buf) == 0 || buf[0] == '\n') {
1272 printk(KERN_INFO DRV_NAME
1273 ": %s: Setting active slave to None.\n",
1274 bond->dev->name);
3418db7c 1275 bond->primary_slave = NULL;
b76cdba9
MW
1276 bond_select_active_slave(bond);
1277 } else {
1278 printk(KERN_INFO DRV_NAME
1279 ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
1280 bond->dev->name, (int)strlen(buf) - 1, buf);
1281 }
1282 }
1283out:
e934dd78
JV
1284 write_unlock_bh(&bond->curr_slave_lock);
1285 read_unlock(&bond->lock);
6603a6f2
JV
1286 rtnl_unlock();
1287
b76cdba9
MW
1288 return count;
1289
1290}
43cb76d9 1291static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);
b76cdba9
MW
1292
1293
1294/*
1295 * Show link status of the bond interface.
1296 */
43cb76d9
GKH
1297static ssize_t bonding_show_mii_status(struct device *d,
1298 struct device_attribute *attr,
1299 char *buf)
b76cdba9
MW
1300{
1301 struct slave *curr;
43cb76d9 1302 struct bonding *bond = to_bond(d);
b76cdba9
MW
1303
1304 read_lock(&bond->curr_slave_lock);
1305 curr = bond->curr_active_slave;
1306 read_unlock(&bond->curr_slave_lock);
1307
7bd46508 1308 return sprintf(buf, "%s\n", (curr) ? "up" : "down");
b76cdba9 1309}
43cb76d9 1310static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
b76cdba9
MW
1311
1312
1313/*
1314 * Show current 802.3ad aggregator ID.
1315 */
43cb76d9
GKH
1316static ssize_t bonding_show_ad_aggregator(struct device *d,
1317 struct device_attribute *attr,
1318 char *buf)
b76cdba9
MW
1319{
1320 int count = 0;
43cb76d9 1321 struct bonding *bond = to_bond(d);
b76cdba9
MW
1322
1323 if (bond->params.mode == BOND_MODE_8023AD) {
1324 struct ad_info ad_info;
7bd46508 1325 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.aggregator_id);
b76cdba9 1326 }
b76cdba9
MW
1327
1328 return count;
1329}
43cb76d9 1330static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
b76cdba9
MW
1331
1332
1333/*
1334 * Show number of active 802.3ad ports.
1335 */
43cb76d9
GKH
1336static ssize_t bonding_show_ad_num_ports(struct device *d,
1337 struct device_attribute *attr,
1338 char *buf)
b76cdba9
MW
1339{
1340 int count = 0;
43cb76d9 1341 struct bonding *bond = to_bond(d);
b76cdba9
MW
1342
1343 if (bond->params.mode == BOND_MODE_8023AD) {
1344 struct ad_info ad_info;
7bd46508 1345 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0: ad_info.ports);
b76cdba9 1346 }
b76cdba9
MW
1347
1348 return count;
1349}
43cb76d9 1350static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
b76cdba9
MW
1351
1352
1353/*
1354 * Show current 802.3ad actor key.
1355 */
43cb76d9
GKH
1356static ssize_t bonding_show_ad_actor_key(struct device *d,
1357 struct device_attribute *attr,
1358 char *buf)
b76cdba9
MW
1359{
1360 int count = 0;
43cb76d9 1361 struct bonding *bond = to_bond(d);
b76cdba9
MW
1362
1363 if (bond->params.mode == BOND_MODE_8023AD) {
1364 struct ad_info ad_info;
7bd46508 1365 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.actor_key);
b76cdba9 1366 }
b76cdba9
MW
1367
1368 return count;
1369}
43cb76d9 1370static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
b76cdba9
MW
1371
1372
1373/*
1374 * Show current 802.3ad partner key.
1375 */
43cb76d9
GKH
1376static ssize_t bonding_show_ad_partner_key(struct device *d,
1377 struct device_attribute *attr,
1378 char *buf)
b76cdba9
MW
1379{
1380 int count = 0;
43cb76d9 1381 struct bonding *bond = to_bond(d);
b76cdba9
MW
1382
1383 if (bond->params.mode == BOND_MODE_8023AD) {
1384 struct ad_info ad_info;
7bd46508 1385 count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ? 0 : ad_info.partner_key);
b76cdba9 1386 }
b76cdba9
MW
1387
1388 return count;
1389}
43cb76d9 1390static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
b76cdba9
MW
1391
1392
1393/*
1394 * Show current 802.3ad partner mac.
1395 */
43cb76d9
GKH
1396static ssize_t bonding_show_ad_partner_mac(struct device *d,
1397 struct device_attribute *attr,
1398 char *buf)
b76cdba9
MW
1399{
1400 int count = 0;
43cb76d9 1401 struct bonding *bond = to_bond(d);
0795af57 1402 DECLARE_MAC_BUF(mac);
b76cdba9
MW
1403
1404 if (bond->params.mode == BOND_MODE_8023AD) {
1405 struct ad_info ad_info;
1406 if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
0795af57 1407 count = sprintf(buf,"%s\n",
7bd46508 1408 print_mac(mac, ad_info.partner_system));
b76cdba9
MW
1409 }
1410 }
b76cdba9
MW
1411
1412 return count;
1413}
43cb76d9 1414static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
b76cdba9
MW
1415
1416
1417
1418static struct attribute *per_bond_attrs[] = {
43cb76d9
GKH
1419 &dev_attr_slaves.attr,
1420 &dev_attr_mode.attr,
dd957c57 1421 &dev_attr_fail_over_mac.attr,
43cb76d9
GKH
1422 &dev_attr_arp_validate.attr,
1423 &dev_attr_arp_interval.attr,
1424 &dev_attr_arp_ip_target.attr,
1425 &dev_attr_downdelay.attr,
1426 &dev_attr_updelay.attr,
1427 &dev_attr_lacp_rate.attr,
1428 &dev_attr_xmit_hash_policy.attr,
7893b249 1429 &dev_attr_num_grat_arp.attr,
43cb76d9
GKH
1430 &dev_attr_miimon.attr,
1431 &dev_attr_primary.attr,
1432 &dev_attr_use_carrier.attr,
1433 &dev_attr_active_slave.attr,
1434 &dev_attr_mii_status.attr,
1435 &dev_attr_ad_aggregator.attr,
1436 &dev_attr_ad_num_ports.attr,
1437 &dev_attr_ad_actor_key.attr,
1438 &dev_attr_ad_partner_key.attr,
1439 &dev_attr_ad_partner_mac.attr,
b76cdba9
MW
1440 NULL,
1441};
1442
1443static struct attribute_group bonding_group = {
1444 .name = "bonding",
1445 .attrs = per_bond_attrs,
1446};
1447
1448/*
1449 * Initialize sysfs. This sets up the bonding_masters file in
1450 * /sys/class/net.
1451 */
1452int bond_create_sysfs(void)
1453{
1454 int ret = 0;
1455 struct bonding *firstbond;
1456
b76cdba9
MW
1457 /* get the netdev class pointer */
1458 firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
1459 if (!firstbond)
1460 return -ENODEV;
1461
43cb76d9 1462 netdev_class = firstbond->dev->dev.class;
b76cdba9
MW
1463 if (!netdev_class)
1464 return -ENODEV;
1465
1466 ret = class_create_file(netdev_class, &class_attr_bonding_masters);
877cbd36
JV
1467 /*
1468 * Permit multiple loads of the module by ignoring failures to
1469 * create the bonding_masters sysfs file. Bonding devices
1470 * created by second or subsequent loads of the module will
1471 * not be listed in, or controllable by, bonding_masters, but
1472 * will have the usual "bonding" sysfs directory.
1473 *
1474 * This is done to preserve backwards compatibility for
1475 * initscripts/sysconfig, which load bonding multiple times to
1476 * configure multiple bonding devices.
1477 */
1478 if (ret == -EEXIST) {
38d2f38b
SH
1479 /* Is someone being kinky and naming a device bonding_master? */
1480 if (__dev_get_by_name(&init_net,
1481 class_attr_bonding_masters.attr.name))
1482 printk(KERN_ERR
1483 "network device named %s already exists in sysfs",
1484 class_attr_bonding_masters.attr.name);
1485 else {
1486 netdev_class = NULL;
1487 return 0;
1488 }
877cbd36 1489 }
b76cdba9
MW
1490
1491 return ret;
1492
1493}
1494
1495/*
1496 * Remove /sys/class/net/bonding_masters.
1497 */
1498void bond_destroy_sysfs(void)
1499{
1500 if (netdev_class)
1501 class_remove_file(netdev_class, &class_attr_bonding_masters);
1502}
1503
1504/*
1505 * Initialize sysfs for each bond. This sets up and registers
1506 * the 'bondctl' directory for each individual bond under /sys/class/net.
1507 */
1508int bond_create_sysfs_entry(struct bonding *bond)
1509{
1510 struct net_device *dev = bond->dev;
1511 int err;
1512
43cb76d9 1513 err = sysfs_create_group(&(dev->dev.kobj), &bonding_group);
b76cdba9
MW
1514 if (err) {
1515 printk(KERN_EMERG "eek! didn't create group!\n");
1516 }
1517
1518 if (expected_refcount < 1)
43cb76d9 1519 expected_refcount = atomic_read(&bond->dev->dev.kobj.kref.refcount);
b76cdba9
MW
1520
1521 return err;
1522}
1523/*
1524 * Remove sysfs entries for each bond.
1525 */
1526void bond_destroy_sysfs_entry(struct bonding *bond)
1527{
1528 struct net_device *dev = bond->dev;
1529
43cb76d9 1530 sysfs_remove_group(&(dev->dev.kobj), &bonding_group);
b76cdba9
MW
1531}
1532