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