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