bonding: convert fail_over_mac to use the new option API
[linux-2.6-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
adf8d3ff 15 * with this program; if not, see <http://www.gnu.org/licenses/>.
b76cdba9
MW
16 *
17 * The full GNU General Public License is included in this distribution in the
18 * file called LICENSE.
19 *
b76cdba9 20 */
a4aee5c8
JP
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
b76cdba9
MW
24#include <linux/kernel.h>
25#include <linux/module.h>
b76cdba9 26#include <linux/device.h>
d43c36dc 27#include <linux/sched.h>
b76cdba9
MW
28#include <linux/fs.h>
29#include <linux/types.h>
30#include <linux/string.h>
31#include <linux/netdevice.h>
32#include <linux/inetdevice.h>
33#include <linux/in.h>
34#include <linux/sysfs.h>
b76cdba9
MW
35#include <linux/ctype.h>
36#include <linux/inet.h>
37#include <linux/rtnetlink.h>
5c5129b5 38#include <linux/etherdevice.h>
881d966b 39#include <net/net_namespace.h>
ec87fd3b
EB
40#include <net/netns/generic.h>
41#include <linux/nsproxy.h>
b76cdba9 42
b76cdba9 43#include "bonding.h"
5a03cdb7 44
3d632c3f 45#define to_dev(obj) container_of(obj, struct device, kobj)
454d7c9b 46#define to_bond(cd) ((struct bonding *)(netdev_priv(to_net_dev(cd))))
b76cdba9 47
b76cdba9
MW
48/*
49 * "show" function for the bond_masters attribute.
50 * The class parameter is ignored.
51 */
28812fe1
AK
52static ssize_t bonding_show_bonds(struct class *cls,
53 struct class_attribute *attr,
54 char *buf)
b76cdba9 55{
4c22400a
EB
56 struct bond_net *bn =
57 container_of(attr, struct bond_net, class_attr_bonding_masters);
b76cdba9
MW
58 int res = 0;
59 struct bonding *bond;
60
7e083840 61 rtnl_lock();
b76cdba9 62
ec87fd3b 63 list_for_each_entry(bond, &bn->dev_list, bond_list) {
b76cdba9
MW
64 if (res > (PAGE_SIZE - IFNAMSIZ)) {
65 /* not enough space for another interface name */
66 if ((PAGE_SIZE - res) > 10)
67 res = PAGE_SIZE - 10;
b8843665 68 res += sprintf(buf + res, "++more++ ");
b76cdba9
MW
69 break;
70 }
b8843665 71 res += sprintf(buf + res, "%s ", bond->dev->name);
b76cdba9 72 }
1dcdcd69
WF
73 if (res)
74 buf[res-1] = '\n'; /* eat the leftover space */
7e083840
SH
75
76 rtnl_unlock();
b76cdba9
MW
77 return res;
78}
79
4c22400a 80static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
373500db
SH
81{
82 struct bonding *bond;
83
ec87fd3b 84 list_for_each_entry(bond, &bn->dev_list, bond_list) {
373500db
SH
85 if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
86 return bond->dev;
87 }
88 return NULL;
89}
90
b76cdba9
MW
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
3d632c3f 99static ssize_t bonding_store_bonds(struct class *cls,
28812fe1 100 struct class_attribute *attr,
3d632c3f 101 const char *buffer, size_t count)
b76cdba9 102{
4c22400a
EB
103 struct bond_net *bn =
104 container_of(attr, struct bond_net, class_attr_bonding_masters);
b76cdba9
MW
105 char command[IFNAMSIZ + 1] = {0, };
106 char *ifname;
027ea041 107 int rv, res = count;
b76cdba9 108
b76cdba9
MW
109 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
110 ifname = command + 1;
111 if ((strlen(command) <= 1) ||
112 !dev_valid_name(ifname))
113 goto err_no_cmd;
114
115 if (command[0] == '+') {
a4aee5c8 116 pr_info("%s is being created...\n", ifname);
4c22400a 117 rv = bond_create(bn->net, ifname);
027ea041 118 if (rv) {
5f86cad1
PO
119 if (rv == -EEXIST)
120 pr_info("%s already exists.\n", ifname);
121 else
122 pr_info("%s creation failed.\n", ifname);
027ea041 123 res = rv;
b76cdba9 124 }
373500db
SH
125 } else if (command[0] == '-') {
126 struct net_device *bond_dev;
b76cdba9 127
027ea041 128 rtnl_lock();
4c22400a 129 bond_dev = bond_get_by_name(bn, ifname);
373500db 130 if (bond_dev) {
a4aee5c8 131 pr_info("%s is being deleted...\n", ifname);
373500db
SH
132 unregister_netdevice(bond_dev);
133 } else {
a4aee5c8 134 pr_err("unable to delete non-existent %s\n", ifname);
373500db
SH
135 res = -ENODEV;
136 }
137 rtnl_unlock();
138 } else
139 goto err_no_cmd;
027ea041 140
373500db
SH
141 /* Always return either count or an error. If you return 0, you'll
142 * get called forever, which is bad.
143 */
144 return res;
b76cdba9
MW
145
146err_no_cmd:
a4aee5c8 147 pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
c4ebc66a 148 return -EPERM;
b76cdba9 149}
373500db 150
b76cdba9 151/* class attribute for bond_masters file. This ends up in /sys/class/net */
4c22400a
EB
152static const struct class_attribute class_attr_bonding_masters = {
153 .attr = {
154 .name = "bonding_masters",
155 .mode = S_IWUSR | S_IRUGO,
156 },
157 .show = bonding_show_bonds,
158 .store = bonding_store_bonds,
4c22400a 159};
b76cdba9 160
b76cdba9
MW
161/*
162 * Show the slaves in the current bond.
163 */
43cb76d9
GKH
164static ssize_t bonding_show_slaves(struct device *d,
165 struct device_attribute *attr, char *buf)
b76cdba9 166{
43cb76d9 167 struct bonding *bond = to_bond(d);
9caff1e7 168 struct list_head *iter;
dec1e90e 169 struct slave *slave;
170 int res = 0;
b76cdba9 171
4d1ae5fb 172 if (!rtnl_trylock())
173 return restart_syscall();
174
9caff1e7 175 bond_for_each_slave(bond, slave, iter) {
b76cdba9
MW
176 if (res > (PAGE_SIZE - IFNAMSIZ)) {
177 /* not enough space for another interface name */
178 if ((PAGE_SIZE - res) > 10)
179 res = PAGE_SIZE - 10;
7bd46508 180 res += sprintf(buf + res, "++more++ ");
b76cdba9
MW
181 break;
182 }
183 res += sprintf(buf + res, "%s ", slave->dev->name);
184 }
4d1ae5fb 185
186 rtnl_unlock();
187
1dcdcd69
WF
188 if (res)
189 buf[res-1] = '\n'; /* eat the leftover space */
dec1e90e 190
b76cdba9
MW
191 return res;
192}
193
194/*
d6641ccf 195 * Set the slaves in the current bond.
f9f3545e
JP
196 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
197 * All hard work should be done there.
b76cdba9 198 */
43cb76d9
GKH
199static ssize_t bonding_store_slaves(struct device *d,
200 struct device_attribute *attr,
201 const char *buffer, size_t count)
b76cdba9
MW
202{
203 char command[IFNAMSIZ + 1] = { 0, };
204 char *ifname;
f9f3545e
JP
205 int res, ret = count;
206 struct net_device *dev;
43cb76d9 207 struct bonding *bond = to_bond(d);
b76cdba9 208
496a60cd
EB
209 if (!rtnl_trylock())
210 return restart_syscall();
027ea041 211
b76cdba9
MW
212 sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
213 ifname = command + 1;
214 if ((strlen(command) <= 1) ||
215 !dev_valid_name(ifname))
216 goto err_no_cmd;
217
f9f3545e
JP
218 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
219 if (!dev) {
220 pr_info("%s: Interface %s does not exist!\n",
221 bond->dev->name, ifname);
222 ret = -ENODEV;
223 goto out;
224 }
b76cdba9 225
f9f3545e
JP
226 switch (command[0]) {
227 case '+':
228 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
b76cdba9 229 res = bond_enslave(bond->dev, dev);
f9f3545e 230 break;
3d632c3f 231
f9f3545e
JP
232 case '-':
233 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
234 res = bond_release(bond->dev, dev);
235 break;
b76cdba9 236
f9f3545e
JP
237 default:
238 goto err_no_cmd;
b76cdba9
MW
239 }
240
f9f3545e
JP
241 if (res)
242 ret = res;
243 goto out;
244
b76cdba9 245err_no_cmd:
a4aee5c8
JP
246 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
247 bond->dev->name);
b76cdba9
MW
248 ret = -EPERM;
249
250out:
027ea041 251 rtnl_unlock();
b76cdba9
MW
252 return ret;
253}
254
3d632c3f
SH
255static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
256 bonding_store_slaves);
b76cdba9
MW
257
258/*
259 * Show and set the bonding mode. The bond interface must be down to
260 * change the mode.
261 */
43cb76d9
GKH
262static ssize_t bonding_show_mode(struct device *d,
263 struct device_attribute *attr, char *buf)
b76cdba9 264{
43cb76d9 265 struct bonding *bond = to_bond(d);
2b3798d5 266 struct bond_opt_value *val;
b76cdba9 267
2b3798d5
NA
268 val = bond_opt_get_val(BOND_OPT_MODE, bond->params.mode);
269
270 return sprintf(buf, "%s %d\n", val->string, bond->params.mode);
b76cdba9
MW
271}
272
43cb76d9
GKH
273static ssize_t bonding_store_mode(struct device *d,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
b76cdba9 276{
43cb76d9 277 struct bonding *bond = to_bond(d);
2b3798d5 278 int ret;
b76cdba9 279
2b3798d5
NA
280 ret = bond_opt_tryset_rtnl(bond, BOND_OPT_MODE, (char *)buf);
281 if (!ret)
72be35fe 282 ret = count;
8f903c70 283
b76cdba9
MW
284 return ret;
285}
3d632c3f
SH
286static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
287 bonding_show_mode, bonding_store_mode);
b76cdba9
MW
288
289/*
3d632c3f 290 * Show and set the bonding transmit hash method.
b76cdba9 291 */
43cb76d9
GKH
292static ssize_t bonding_show_xmit_hash(struct device *d,
293 struct device_attribute *attr,
294 char *buf)
b76cdba9 295{
43cb76d9 296 struct bonding *bond = to_bond(d);
a4b32ce7
NA
297 struct bond_opt_value *val;
298
299 val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
b76cdba9 300
a4b32ce7 301 return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
b76cdba9
MW
302}
303
43cb76d9
GKH
304static ssize_t bonding_store_xmit_hash(struct device *d,
305 struct device_attribute *attr,
306 const char *buf, size_t count)
b76cdba9 307{
43cb76d9 308 struct bonding *bond = to_bond(d);
a4b32ce7 309 int ret;
b76cdba9 310
a4b32ce7 311 ret = bond_opt_tryset_rtnl(bond, BOND_OPT_XMIT_HASH, (char *)buf);
f70161c6 312 if (!ret)
313 ret = count;
314
b76cdba9
MW
315 return ret;
316}
3d632c3f
SH
317static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
318 bonding_show_xmit_hash, bonding_store_xmit_hash);
b76cdba9 319
f5b2b966
JV
320/*
321 * Show and set arp_validate.
322 */
43cb76d9
GKH
323static ssize_t bonding_show_arp_validate(struct device *d,
324 struct device_attribute *attr,
325 char *buf)
f5b2b966 326{
43cb76d9 327 struct bonding *bond = to_bond(d);
16228881
NA
328 struct bond_opt_value *val;
329
330 val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
331 bond->params.arp_validate);
f5b2b966 332
16228881 333 return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
f5b2b966
JV
334}
335
43cb76d9
GKH
336static ssize_t bonding_store_arp_validate(struct device *d,
337 struct device_attribute *attr,
338 const char *buf, size_t count)
f5b2b966 339{
43cb76d9 340 struct bonding *bond = to_bond(d);
16228881 341 int ret;
29c49482 342
16228881 343 ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_VALIDATE, (char *)buf);
29c49482 344 if (!ret)
345 ret = count;
346
5c5038dc 347 return ret;
f5b2b966
JV
348}
349
3d632c3f
SH
350static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
351 bonding_store_arp_validate);
8599b52e
VF
352/*
353 * Show and set arp_all_targets.
354 */
355static ssize_t bonding_show_arp_all_targets(struct device *d,
356 struct device_attribute *attr,
357 char *buf)
358{
359 struct bonding *bond = to_bond(d);
edf36b24 360 struct bond_opt_value *val;
8599b52e 361
edf36b24
NA
362 val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
363 bond->params.arp_all_targets);
364 return sprintf(buf, "%s %d\n",
365 val->string, bond->params.arp_all_targets);
8599b52e
VF
366}
367
368static ssize_t bonding_store_arp_all_targets(struct device *d,
369 struct device_attribute *attr,
370 const char *buf, size_t count)
371{
372 struct bonding *bond = to_bond(d);
edf36b24 373 int ret;
8599b52e 374
edf36b24 375 ret = bond_opt_tryset_rtnl(bond, BOND_OPT_ARP_ALL_TARGETS, (char *)buf);
d5c84254 376 if (!ret)
377 ret = count;
378
d5c84254 379 return ret;
8599b52e
VF
380}
381
382static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
383 bonding_show_arp_all_targets, bonding_store_arp_all_targets);
f5b2b966 384
dd957c57
JV
385/*
386 * Show and store fail_over_mac. User only allowed to change the
387 * value when there are no slaves.
388 */
3d632c3f
SH
389static ssize_t bonding_show_fail_over_mac(struct device *d,
390 struct device_attribute *attr,
391 char *buf)
dd957c57
JV
392{
393 struct bonding *bond = to_bond(d);
1df6b6aa 394 struct bond_opt_value *val;
dd957c57 395
1df6b6aa
NA
396 val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
397 bond->params.fail_over_mac);
398
399 return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
dd957c57
JV
400}
401
3d632c3f
SH
402static ssize_t bonding_store_fail_over_mac(struct device *d,
403 struct device_attribute *attr,
404 const char *buf, size_t count)
dd957c57 405{
dd957c57 406 struct bonding *bond = to_bond(d);
1df6b6aa 407 int ret;
dd957c57 408
1df6b6aa 409 ret = bond_opt_tryset_rtnl(bond, BOND_OPT_FAIL_OVER_MAC, (char *)buf);
89901972 410 if (!ret)
411 ret = count;
3915c1e8 412
9402b746 413 return ret;
dd957c57
JV
414}
415
3d632c3f
SH
416static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
417 bonding_show_fail_over_mac, bonding_store_fail_over_mac);
dd957c57 418
b76cdba9
MW
419/*
420 * Show and set the arp timer interval. There are two tricky bits
421 * here. First, if ARP monitoring is activated, then we must disable
422 * MII monitoring. Second, if the ARP timer isn't running, we must
423 * start it.
424 */
43cb76d9
GKH
425static ssize_t bonding_show_arp_interval(struct device *d,
426 struct device_attribute *attr,
427 char *buf)
b76cdba9 428{
43cb76d9 429 struct bonding *bond = to_bond(d);
b76cdba9 430
7bd46508 431 return sprintf(buf, "%d\n", bond->params.arp_interval);
b76cdba9
MW
432}
433
43cb76d9
GKH
434static ssize_t bonding_store_arp_interval(struct device *d,
435 struct device_attribute *attr,
436 const char *buf, size_t count)
b76cdba9 437{
43cb76d9 438 struct bonding *bond = to_bond(d);
06151dbc 439 int new_value, ret;
b76cdba9
MW
440
441 if (sscanf(buf, "%d", &new_value) != 1) {
a4aee5c8 442 pr_err("%s: no arp_interval value specified.\n",
06151dbc 443 bond->dev->name);
444 return -EINVAL;
b76cdba9 445 }
06151dbc 446
447 if (!rtnl_trylock())
448 return restart_syscall();
449
450 ret = bond_option_arp_interval_set(bond, new_value);
451 if (!ret)
452 ret = count;
453
fbb0c41b 454 rtnl_unlock();
b76cdba9
MW
455 return ret;
456}
3d632c3f
SH
457static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
458 bonding_show_arp_interval, bonding_store_arp_interval);
b76cdba9
MW
459
460/*
461 * Show and set the arp targets.
462 */
43cb76d9
GKH
463static ssize_t bonding_show_arp_targets(struct device *d,
464 struct device_attribute *attr,
465 char *buf)
b76cdba9
MW
466{
467 int i, res = 0;
43cb76d9 468 struct bonding *bond = to_bond(d);
b76cdba9
MW
469
470 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
471 if (bond->params.arp_targets[i])
63779436
HH
472 res += sprintf(buf + res, "%pI4 ",
473 &bond->params.arp_targets[i]);
b76cdba9 474 }
1dcdcd69
WF
475 if (res)
476 buf[res-1] = '\n'; /* eat the leftover space */
b76cdba9
MW
477 return res;
478}
479
43cb76d9
GKH
480static ssize_t bonding_store_arp_targets(struct device *d,
481 struct device_attribute *attr,
482 const char *buf, size_t count)
b76cdba9 483{
43cb76d9 484 struct bonding *bond = to_bond(d);
7f28fa10 485 __be32 target;
486 int ret = -EPERM;
4d1ae5fb 487
7f28fa10 488 if (!in4_pton(buf + 1, -1, (u8 *)&target, -1, NULL)) {
489 pr_err("%s: invalid ARP target %pI4 specified\n",
490 bond->dev->name, &target);
491 return -EPERM;
f9de11a1 492 }
8599b52e 493
7f28fa10 494 if (!rtnl_trylock())
495 return restart_syscall();
8599b52e 496
7f28fa10 497 if (buf[0] == '+')
498 ret = bond_option_arp_ip_target_add(bond, target);
499 else if (buf[0] == '-')
500 ret = bond_option_arp_ip_target_rem(bond, target);
501 else
a4aee5c8
JP
502 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
503 bond->dev->name);
b76cdba9 504
7f28fa10 505 if (!ret)
506 ret = count;
507
4d1ae5fb 508 rtnl_unlock();
b76cdba9
MW
509 return ret;
510}
43cb76d9 511static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
b76cdba9
MW
512
513/*
514 * Show and set the up and down delays. These must be multiples of the
515 * MII monitoring value, and are stored internally as the multiplier.
516 * Thus, we must translate to MS for the real world.
517 */
43cb76d9
GKH
518static ssize_t bonding_show_downdelay(struct device *d,
519 struct device_attribute *attr,
520 char *buf)
b76cdba9 521{
43cb76d9 522 struct bonding *bond = to_bond(d);
b76cdba9 523
7bd46508 524 return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
b76cdba9
MW
525}
526
43cb76d9
GKH
527static ssize_t bonding_store_downdelay(struct device *d,
528 struct device_attribute *attr,
529 const char *buf, size_t count)
b76cdba9 530{
c7461f9b 531 int new_value, ret;
43cb76d9 532 struct bonding *bond = to_bond(d);
b76cdba9 533
b76cdba9 534 if (sscanf(buf, "%d", &new_value) != 1) {
a4aee5c8 535 pr_err("%s: no down delay value specified.\n", bond->dev->name);
c7461f9b 536 return -EINVAL;
b76cdba9 537 }
b76cdba9 538
c7461f9b 539 if (!rtnl_trylock())
540 return restart_syscall();
541
542 ret = bond_option_downdelay_set(bond, new_value);
543 if (!ret)
544 ret = count;
b76cdba9 545
b869ccfa 546 rtnl_unlock();
b76cdba9
MW
547 return ret;
548}
3d632c3f
SH
549static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
550 bonding_show_downdelay, bonding_store_downdelay);
b76cdba9 551
43cb76d9
GKH
552static ssize_t bonding_show_updelay(struct device *d,
553 struct device_attribute *attr,
554 char *buf)
b76cdba9 555{
43cb76d9 556 struct bonding *bond = to_bond(d);
b76cdba9 557
7bd46508 558 return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
b76cdba9
MW
559
560}
561
43cb76d9
GKH
562static ssize_t bonding_store_updelay(struct device *d,
563 struct device_attribute *attr,
564 const char *buf, size_t count)
b76cdba9 565{
25852e29 566 int new_value, ret;
43cb76d9 567 struct bonding *bond = to_bond(d);
b76cdba9 568
b76cdba9 569 if (sscanf(buf, "%d", &new_value) != 1) {
a4aee5c8 570 pr_err("%s: no up delay value specified.\n",
25852e29 571 bond->dev->name);
572 return -EINVAL;
b76cdba9
MW
573 }
574
25852e29 575 if (!rtnl_trylock())
576 return restart_syscall();
577
578 ret = bond_option_updelay_set(bond, new_value);
579 if (!ret)
580 ret = count;
581
b869ccfa 582 rtnl_unlock();
b76cdba9
MW
583 return ret;
584}
3d632c3f
SH
585static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
586 bonding_show_updelay, bonding_store_updelay);
b76cdba9
MW
587
588/*
589 * Show and set the LACP interval. Interface must be down, and the mode
590 * must be set to 802.3ad mode.
591 */
43cb76d9
GKH
592static ssize_t bonding_show_lacp(struct device *d,
593 struct device_attribute *attr,
594 char *buf)
b76cdba9 595{
43cb76d9 596 struct bonding *bond = to_bond(d);
b76cdba9
MW
597
598 return sprintf(buf, "%s %d\n",
599 bond_lacp_tbl[bond->params.lacp_fast].modename,
7bd46508 600 bond->params.lacp_fast);
b76cdba9
MW
601}
602
43cb76d9
GKH
603static ssize_t bonding_store_lacp(struct device *d,
604 struct device_attribute *attr,
605 const char *buf, size_t count)
b76cdba9 606{
43cb76d9 607 struct bonding *bond = to_bond(d);
998e40bb 608 int new_value, ret;
b76cdba9 609
ece95f7f 610 new_value = bond_parse_parm(buf, bond_lacp_tbl);
998e40bb 611 if (new_value < 0) {
a4aee5c8 612 pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
3d632c3f 613 bond->dev->name, (int)strlen(buf) - 1, buf);
998e40bb 614 return -EINVAL;
b76cdba9 615 }
c509316b 616
998e40bb 617 if (!rtnl_trylock())
618 return restart_syscall();
619
620 ret = bond_option_lacp_rate_set(bond, new_value);
621 if (!ret)
622 ret = count;
623
624 rtnl_unlock();
b76cdba9
MW
625 return ret;
626}
3d632c3f
SH
627static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
628 bonding_show_lacp, bonding_store_lacp);
b76cdba9 629
655f8919 630static ssize_t bonding_show_min_links(struct device *d,
631 struct device_attribute *attr,
632 char *buf)
633{
634 struct bonding *bond = to_bond(d);
635
636 return sprintf(buf, "%d\n", bond->params.min_links);
637}
638
639static ssize_t bonding_store_min_links(struct device *d,
640 struct device_attribute *attr,
641 const char *buf, size_t count)
642{
643 struct bonding *bond = to_bond(d);
644 int ret;
645 unsigned int new_value;
646
647 ret = kstrtouint(buf, 0, &new_value);
648 if (ret < 0) {
649 pr_err("%s: Ignoring invalid min links value %s.\n",
650 bond->dev->name, buf);
651 return ret;
652 }
653
7d101008 654 if (!rtnl_trylock())
655 return restart_syscall();
656
657 ret = bond_option_min_links_set(bond, new_value);
658 if (!ret)
659 ret = count;
660
661 rtnl_unlock();
662 return ret;
655f8919 663}
664static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
665 bonding_show_min_links, bonding_store_min_links);
666
fd989c83
JV
667static ssize_t bonding_show_ad_select(struct device *d,
668 struct device_attribute *attr,
669 char *buf)
670{
671 struct bonding *bond = to_bond(d);
672
673 return sprintf(buf, "%s %d\n",
674 ad_select_tbl[bond->params.ad_select].modename,
675 bond->params.ad_select);
676}
677
678
679static ssize_t bonding_store_ad_select(struct device *d,
680 struct device_attribute *attr,
681 const char *buf, size_t count)
682{
ec029fac 683 int new_value, ret;
fd989c83
JV
684 struct bonding *bond = to_bond(d);
685
fd989c83 686 new_value = bond_parse_parm(buf, ad_select_tbl);
ec029fac 687 if (new_value < 0) {
a4aee5c8 688 pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
fd989c83 689 bond->dev->name, (int)strlen(buf) - 1, buf);
ec029fac 690 return -EINVAL;
fd989c83 691 }
ec029fac 692
693 if (!rtnl_trylock())
694 return restart_syscall();
695
696 ret = bond_option_ad_select_set(bond, new_value);
697 if (!ret)
698 ret = count;
699
700 rtnl_unlock();
fd989c83
JV
701 return ret;
702}
3d632c3f
SH
703static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
704 bonding_show_ad_select, bonding_store_ad_select);
fd989c83 705
ad246c99
BH
706/*
707 * Show and set the number of peer notifications to send after a failover event.
708 */
709static ssize_t bonding_show_num_peer_notif(struct device *d,
710 struct device_attribute *attr,
711 char *buf)
712{
713 struct bonding *bond = to_bond(d);
714 return sprintf(buf, "%d\n", bond->params.num_peer_notif);
715}
716
717static ssize_t bonding_store_num_peer_notif(struct device *d,
718 struct device_attribute *attr,
719 const char *buf, size_t count)
720{
721 struct bonding *bond = to_bond(d);
2c9839c1 722 u8 new_value;
723 int ret;
724
725 ret = kstrtou8(buf, 10, &new_value);
0b23810d 726 if (ret) {
2c9839c1 727 pr_err("%s: invalid value %s specified.\n",
728 bond->dev->name, buf);
729 return ret;
730 }
731
732 if (!rtnl_trylock())
733 return restart_syscall();
734
735 ret = bond_option_num_peer_notif_set(bond, new_value);
736 if (!ret)
737 ret = count;
738
739 rtnl_unlock();
740 return ret;
ad246c99
BH
741}
742static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
743 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
744static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
745 bonding_show_num_peer_notif, bonding_store_num_peer_notif);
746
b76cdba9
MW
747/*
748 * Show and set the MII monitor interval. There are two tricky bits
749 * here. First, if MII monitoring is activated, then we must disable
750 * ARP monitoring. Second, if the timer isn't running, we must
751 * start it.
752 */
43cb76d9
GKH
753static ssize_t bonding_show_miimon(struct device *d,
754 struct device_attribute *attr,
755 char *buf)
b76cdba9 756{
43cb76d9 757 struct bonding *bond = to_bond(d);
b76cdba9 758
7bd46508 759 return sprintf(buf, "%d\n", bond->params.miimon);
b76cdba9
MW
760}
761
43cb76d9
GKH
762static ssize_t bonding_store_miimon(struct device *d,
763 struct device_attribute *attr,
764 const char *buf, size_t count)
b76cdba9 765{
eecdaa6e 766 int new_value, ret;
43cb76d9 767 struct bonding *bond = to_bond(d);
b76cdba9
MW
768
769 if (sscanf(buf, "%d", &new_value) != 1) {
a4aee5c8 770 pr_err("%s: no miimon value specified.\n",
b76cdba9 771 bond->dev->name);
eecdaa6e 772 return -EINVAL;
b76cdba9 773 }
eecdaa6e 774
775 if (!rtnl_trylock())
776 return restart_syscall();
777
778 ret = bond_option_miimon_set(bond, new_value);
779 if (!ret)
780 ret = count;
781
fbb0c41b 782 rtnl_unlock();
b76cdba9
MW
783 return ret;
784}
3d632c3f
SH
785static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
786 bonding_show_miimon, bonding_store_miimon);
b76cdba9
MW
787
788/*
789 * Show and set the primary slave. The store function is much
790 * simpler than bonding_store_slaves function because it only needs to
791 * handle one interface name.
792 * The bond must be a mode that supports a primary for this be
793 * set.
794 */
43cb76d9
GKH
795static ssize_t bonding_show_primary(struct device *d,
796 struct device_attribute *attr,
797 char *buf)
b76cdba9
MW
798{
799 int count = 0;
43cb76d9 800 struct bonding *bond = to_bond(d);
b76cdba9
MW
801
802 if (bond->primary_slave)
7bd46508 803 count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
b76cdba9
MW
804
805 return count;
806}
807
43cb76d9
GKH
808static ssize_t bonding_store_primary(struct device *d,
809 struct device_attribute *attr,
810 const char *buf, size_t count)
b76cdba9 811{
43cb76d9 812 struct bonding *bond = to_bond(d);
f4bb2e9c 813 char ifname[IFNAMSIZ];
0a98a0d1 814 int ret;
b76cdba9 815
eb6e98a1 816 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
0a98a0d1 817 if (ifname[0] == '\n')
818 ifname[0] = '\0';
b76cdba9 819
0a98a0d1 820 if (!rtnl_trylock())
821 return restart_syscall();
f4bb2e9c 822
0a98a0d1 823 ret = bond_option_primary_set(bond, ifname);
824 if (!ret)
825 ret = count;
8a93664d 826
6603a6f2 827 rtnl_unlock();
0a98a0d1 828 return ret;
b76cdba9 829}
3d632c3f
SH
830static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
831 bonding_show_primary, bonding_store_primary);
b76cdba9 832
a549952a
JP
833/*
834 * Show and set the primary_reselect flag.
835 */
836static ssize_t bonding_show_primary_reselect(struct device *d,
837 struct device_attribute *attr,
838 char *buf)
839{
840 struct bonding *bond = to_bond(d);
841
842 return sprintf(buf, "%s %d\n",
843 pri_reselect_tbl[bond->params.primary_reselect].modename,
844 bond->params.primary_reselect);
845}
846
847static ssize_t bonding_store_primary_reselect(struct device *d,
848 struct device_attribute *attr,
849 const char *buf, size_t count)
850{
8a41ae44 851 int new_value, ret;
a549952a
JP
852 struct bonding *bond = to_bond(d);
853
a549952a
JP
854 new_value = bond_parse_parm(buf, pri_reselect_tbl);
855 if (new_value < 0) {
a4aee5c8 856 pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
a549952a
JP
857 bond->dev->name,
858 (int) strlen(buf) - 1, buf);
8a41ae44 859 return -EINVAL;
a549952a
JP
860 }
861
8a41ae44 862 if (!rtnl_trylock())
863 return restart_syscall();
864
865 ret = bond_option_primary_reselect_set(bond, new_value);
866 if (!ret)
867 ret = count;
a549952a 868
a549952a
JP
869 rtnl_unlock();
870 return ret;
871}
872static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
873 bonding_show_primary_reselect,
874 bonding_store_primary_reselect);
875
b76cdba9
MW
876/*
877 * Show and set the use_carrier flag.
878 */
43cb76d9
GKH
879static ssize_t bonding_show_carrier(struct device *d,
880 struct device_attribute *attr,
881 char *buf)
b76cdba9 882{
43cb76d9 883 struct bonding *bond = to_bond(d);
b76cdba9 884
7bd46508 885 return sprintf(buf, "%d\n", bond->params.use_carrier);
b76cdba9
MW
886}
887
43cb76d9
GKH
888static ssize_t bonding_store_carrier(struct device *d,
889 struct device_attribute *attr,
890 const char *buf, size_t count)
b76cdba9 891{
9f53e14e 892 int new_value, ret;
43cb76d9 893 struct bonding *bond = to_bond(d);
b76cdba9 894
b76cdba9 895 if (sscanf(buf, "%d", &new_value) != 1) {
a4aee5c8 896 pr_err("%s: no use_carrier value specified.\n",
b76cdba9 897 bond->dev->name);
9f53e14e 898 return -EINVAL;
b76cdba9 899 }
9f53e14e 900
901 if (!rtnl_trylock())
902 return restart_syscall();
903
904 ret = bond_option_use_carrier_set(bond, new_value);
905 if (!ret)
906 ret = count;
907
908 rtnl_unlock();
672bda33 909 return ret;
b76cdba9 910}
3d632c3f
SH
911static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
912 bonding_show_carrier, bonding_store_carrier);
b76cdba9
MW
913
914
915/*
916 * Show and set currently active_slave.
917 */
43cb76d9
GKH
918static ssize_t bonding_show_active_slave(struct device *d,
919 struct device_attribute *attr,
920 char *buf)
b76cdba9 921{
43cb76d9 922 struct bonding *bond = to_bond(d);
752d48b5 923 struct net_device *slave_dev;
16cd0160 924 int count = 0;
b76cdba9 925
278b2083 926 rcu_read_lock();
752d48b5
JP
927 slave_dev = bond_option_active_slave_get_rcu(bond);
928 if (slave_dev)
929 count = sprintf(buf, "%s\n", slave_dev->name);
278b2083 930 rcu_read_unlock();
931
b76cdba9
MW
932 return count;
933}
934
43cb76d9
GKH
935static ssize_t bonding_store_active_slave(struct device *d,
936 struct device_attribute *attr,
937 const char *buf, size_t count)
b76cdba9 938{
d9e32b21 939 int ret;
43cb76d9 940 struct bonding *bond = to_bond(d);
f4bb2e9c 941 char ifname[IFNAMSIZ];
d9e32b21 942 struct net_device *dev;
b76cdba9 943
496a60cd
EB
944 if (!rtnl_trylock())
945 return restart_syscall();
e843fa50 946
c84e1590 947 sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
f4bb2e9c 948 if (!strlen(ifname) || buf[0] == '\n') {
d9e32b21
JP
949 dev = NULL;
950 } else {
951 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
952 if (!dev) {
953 ret = -ENODEV;
954 goto out;
b76cdba9 955 }
b76cdba9 956 }
f4bb2e9c 957
d9e32b21
JP
958 ret = bond_option_active_slave_set(bond, dev);
959 if (!ret)
960 ret = count;
e843fa50 961
d9e32b21 962 out:
6603a6f2
JV
963 rtnl_unlock();
964
d9e32b21 965 return ret;
b76cdba9
MW
966
967}
3d632c3f
SH
968static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
969 bonding_show_active_slave, bonding_store_active_slave);
b76cdba9
MW
970
971
972/*
973 * Show link status of the bond interface.
974 */
43cb76d9
GKH
975static ssize_t bonding_show_mii_status(struct device *d,
976 struct device_attribute *attr,
977 char *buf)
b76cdba9 978{
43cb76d9 979 struct bonding *bond = to_bond(d);
b76cdba9 980
278b2083 981 return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
b76cdba9 982}
43cb76d9 983static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
b76cdba9 984
b76cdba9
MW
985/*
986 * Show current 802.3ad aggregator ID.
987 */
43cb76d9
GKH
988static ssize_t bonding_show_ad_aggregator(struct device *d,
989 struct device_attribute *attr,
990 char *buf)
b76cdba9
MW
991{
992 int count = 0;
43cb76d9 993 struct bonding *bond = to_bond(d);
b76cdba9
MW
994
995 if (bond->params.mode == BOND_MODE_8023AD) {
996 struct ad_info ad_info;
3d632c3f 997 count = sprintf(buf, "%d\n",
318debd8 998 bond_3ad_get_active_agg_info(bond, &ad_info)
3d632c3f 999 ? 0 : ad_info.aggregator_id);
b76cdba9 1000 }
b76cdba9
MW
1001
1002 return count;
1003}
43cb76d9 1004static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
b76cdba9
MW
1005
1006
1007/*
1008 * Show number of active 802.3ad ports.
1009 */
43cb76d9
GKH
1010static ssize_t bonding_show_ad_num_ports(struct device *d,
1011 struct device_attribute *attr,
1012 char *buf)
b76cdba9
MW
1013{
1014 int count = 0;
43cb76d9 1015 struct bonding *bond = to_bond(d);
b76cdba9
MW
1016
1017 if (bond->params.mode == BOND_MODE_8023AD) {
1018 struct ad_info ad_info;
3d632c3f 1019 count = sprintf(buf, "%d\n",
318debd8 1020 bond_3ad_get_active_agg_info(bond, &ad_info)
3d632c3f 1021 ? 0 : ad_info.ports);
b76cdba9 1022 }
b76cdba9
MW
1023
1024 return count;
1025}
43cb76d9 1026static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
b76cdba9
MW
1027
1028
1029/*
1030 * Show current 802.3ad actor key.
1031 */
43cb76d9
GKH
1032static ssize_t bonding_show_ad_actor_key(struct device *d,
1033 struct device_attribute *attr,
1034 char *buf)
b76cdba9
MW
1035{
1036 int count = 0;
43cb76d9 1037 struct bonding *bond = to_bond(d);
b76cdba9
MW
1038
1039 if (bond->params.mode == BOND_MODE_8023AD) {
1040 struct ad_info ad_info;
3d632c3f 1041 count = sprintf(buf, "%d\n",
318debd8 1042 bond_3ad_get_active_agg_info(bond, &ad_info)
3d632c3f 1043 ? 0 : ad_info.actor_key);
b76cdba9 1044 }
b76cdba9
MW
1045
1046 return count;
1047}
43cb76d9 1048static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
b76cdba9
MW
1049
1050
1051/*
1052 * Show current 802.3ad partner key.
1053 */
43cb76d9
GKH
1054static ssize_t bonding_show_ad_partner_key(struct device *d,
1055 struct device_attribute *attr,
1056 char *buf)
b76cdba9
MW
1057{
1058 int count = 0;
43cb76d9 1059 struct bonding *bond = to_bond(d);
b76cdba9
MW
1060
1061 if (bond->params.mode == BOND_MODE_8023AD) {
1062 struct ad_info ad_info;
3d632c3f 1063 count = sprintf(buf, "%d\n",
318debd8 1064 bond_3ad_get_active_agg_info(bond, &ad_info)
3d632c3f 1065 ? 0 : ad_info.partner_key);
b76cdba9 1066 }
b76cdba9
MW
1067
1068 return count;
1069}
43cb76d9 1070static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
b76cdba9
MW
1071
1072
1073/*
1074 * Show current 802.3ad partner mac.
1075 */
43cb76d9
GKH
1076static ssize_t bonding_show_ad_partner_mac(struct device *d,
1077 struct device_attribute *attr,
1078 char *buf)
b76cdba9
MW
1079{
1080 int count = 0;
43cb76d9 1081 struct bonding *bond = to_bond(d);
b76cdba9
MW
1082
1083 if (bond->params.mode == BOND_MODE_8023AD) {
1084 struct ad_info ad_info;
3d632c3f 1085 if (!bond_3ad_get_active_agg_info(bond, &ad_info))
e174961c 1086 count = sprintf(buf, "%pM\n", ad_info.partner_system);
b76cdba9 1087 }
b76cdba9
MW
1088
1089 return count;
1090}
43cb76d9 1091static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
b76cdba9 1092
bb1d9123
AG
1093/*
1094 * Show the queue_ids of the slaves in the current bond.
1095 */
1096static ssize_t bonding_show_queue_id(struct device *d,
1097 struct device_attribute *attr,
1098 char *buf)
1099{
bb1d9123 1100 struct bonding *bond = to_bond(d);
9caff1e7 1101 struct list_head *iter;
dec1e90e 1102 struct slave *slave;
1103 int res = 0;
bb1d9123
AG
1104
1105 if (!rtnl_trylock())
1106 return restart_syscall();
1107
9caff1e7 1108 bond_for_each_slave(bond, slave, iter) {
79236680
NP
1109 if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1110 /* not enough space for another interface_name:queue_id pair */
bb1d9123
AG
1111 if ((PAGE_SIZE - res) > 10)
1112 res = PAGE_SIZE - 10;
1113 res += sprintf(buf + res, "++more++ ");
1114 break;
1115 }
1116 res += sprintf(buf + res, "%s:%d ",
1117 slave->dev->name, slave->queue_id);
1118 }
bb1d9123
AG
1119 if (res)
1120 buf[res-1] = '\n'; /* eat the leftover space */
4d1ae5fb 1121
bb1d9123 1122 rtnl_unlock();
dec1e90e 1123
bb1d9123
AG
1124 return res;
1125}
1126
1127/*
1128 * Set the queue_ids of the slaves in the current bond. The bond
1129 * interface must be enslaved for this to work.
1130 */
1131static ssize_t bonding_store_queue_id(struct device *d,
1132 struct device_attribute *attr,
1133 const char *buffer, size_t count)
1134{
1135 struct slave *slave, *update_slave;
1136 struct bonding *bond = to_bond(d);
9caff1e7 1137 struct list_head *iter;
bb1d9123 1138 u16 qid;
dec1e90e 1139 int ret = count;
bb1d9123
AG
1140 char *delim;
1141 struct net_device *sdev = NULL;
1142
1143 if (!rtnl_trylock())
1144 return restart_syscall();
1145
1146 /* delim will point to queue id if successful */
1147 delim = strchr(buffer, ':');
1148 if (!delim)
1149 goto err_no_cmd;
1150
1151 /*
1152 * Terminate string that points to device name and bump it
1153 * up one, so we can read the queue id there.
1154 */
1155 *delim = '\0';
1156 if (sscanf(++delim, "%hd\n", &qid) != 1)
1157 goto err_no_cmd;
1158
1159 /* Check buffer length, valid ifname and queue id */
1160 if (strlen(buffer) > IFNAMSIZ ||
1161 !dev_valid_name(buffer) ||
8a540ff9 1162 qid > bond->dev->real_num_tx_queues)
bb1d9123
AG
1163 goto err_no_cmd;
1164
1165 /* Get the pointer to that interface if it exists */
1166 sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1167 if (!sdev)
1168 goto err_no_cmd;
1169
bb1d9123
AG
1170 /* Search for thes slave and check for duplicate qids */
1171 update_slave = NULL;
9caff1e7 1172 bond_for_each_slave(bond, slave, iter) {
bb1d9123
AG
1173 if (sdev == slave->dev)
1174 /*
1175 * We don't need to check the matching
1176 * slave for dups, since we're overwriting it
1177 */
1178 update_slave = slave;
1179 else if (qid && qid == slave->queue_id) {
4d1ae5fb 1180 goto err_no_cmd;
bb1d9123
AG
1181 }
1182 }
1183
1184 if (!update_slave)
4d1ae5fb 1185 goto err_no_cmd;
bb1d9123
AG
1186
1187 /* Actually set the qids for the slave */
1188 update_slave->queue_id = qid;
1189
bb1d9123
AG
1190out:
1191 rtnl_unlock();
1192 return ret;
1193
bb1d9123
AG
1194err_no_cmd:
1195 pr_info("invalid input for queue_id set for %s.\n",
1196 bond->dev->name);
1197 ret = -EPERM;
1198 goto out;
1199}
1200
1201static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1202 bonding_store_queue_id);
1203
1204
ebd8e497
AG
1205/*
1206 * Show and set the all_slaves_active flag.
1207 */
1208static ssize_t bonding_show_slaves_active(struct device *d,
1209 struct device_attribute *attr,
1210 char *buf)
1211{
1212 struct bonding *bond = to_bond(d);
1213
1214 return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1215}
1216
1217static ssize_t bonding_store_slaves_active(struct device *d,
1218 struct device_attribute *attr,
1219 const char *buf, size_t count)
1220{
ebd8e497 1221 struct bonding *bond = to_bond(d);
1cc0b1e3 1222 int new_value, ret;
4d1ae5fb 1223
ebd8e497
AG
1224 if (sscanf(buf, "%d", &new_value) != 1) {
1225 pr_err("%s: no all_slaves_active value specified.\n",
1226 bond->dev->name);
1cc0b1e3 1227 return -EINVAL;
ebd8e497
AG
1228 }
1229
1cc0b1e3 1230 if (!rtnl_trylock())
1231 return restart_syscall();
ebd8e497 1232
1cc0b1e3 1233 ret = bond_option_all_slaves_active_set(bond, new_value);
1234 if (!ret)
1235 ret = count;
b76cdba9 1236
4d1ae5fb 1237 rtnl_unlock();
672bda33 1238 return ret;
ebd8e497
AG
1239}
1240static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1241 bonding_show_slaves_active, bonding_store_slaves_active);
b76cdba9 1242
c2952c31
FL
1243/*
1244 * Show and set the number of IGMP membership reports to send on link failure
1245 */
1246static ssize_t bonding_show_resend_igmp(struct device *d,
94265cf5
FL
1247 struct device_attribute *attr,
1248 char *buf)
c2952c31
FL
1249{
1250 struct bonding *bond = to_bond(d);
1251
1252 return sprintf(buf, "%d\n", bond->params.resend_igmp);
1253}
1254
1255static ssize_t bonding_store_resend_igmp(struct device *d,
94265cf5
FL
1256 struct device_attribute *attr,
1257 const char *buf, size_t count)
c2952c31
FL
1258{
1259 int new_value, ret = count;
1260 struct bonding *bond = to_bond(d);
1261
1262 if (sscanf(buf, "%d", &new_value) != 1) {
1263 pr_err("%s: no resend_igmp value specified.\n",
1264 bond->dev->name);
d8838de7 1265 return -EINVAL;
c2952c31
FL
1266 }
1267
d8838de7 1268 if (!rtnl_trylock())
1269 return restart_syscall();
c2952c31 1270
d8838de7 1271 ret = bond_option_resend_igmp_set(bond, new_value);
1272 if (!ret)
1273 ret = count;
1274
1275 rtnl_unlock();
c2952c31
FL
1276 return ret;
1277}
1278
1279static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1280 bonding_show_resend_igmp, bonding_store_resend_igmp);
1281
7eacd038
NH
1282
1283static ssize_t bonding_show_lp_interval(struct device *d,
1284 struct device_attribute *attr,
1285 char *buf)
1286{
1287 struct bonding *bond = to_bond(d);
1288 return sprintf(buf, "%d\n", bond->params.lp_interval);
1289}
1290
1291static ssize_t bonding_store_lp_interval(struct device *d,
1292 struct device_attribute *attr,
1293 const char *buf, size_t count)
1294{
1295 struct bonding *bond = to_bond(d);
8d836d09 1296 int new_value, ret;
7eacd038
NH
1297
1298 if (sscanf(buf, "%d", &new_value) != 1) {
1299 pr_err("%s: no lp interval value specified.\n",
1300 bond->dev->name);
8d836d09 1301 return -EINVAL;
7eacd038
NH
1302 }
1303
8d836d09 1304 if (!rtnl_trylock())
1305 return restart_syscall();
7eacd038 1306
8d836d09 1307 ret = bond_option_lp_interval_set(bond, new_value);
1308 if (!ret)
1309 ret = count;
1310
1311 rtnl_unlock();
7eacd038
NH
1312 return ret;
1313}
1314
1315static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1316 bonding_show_lp_interval, bonding_store_lp_interval);
1317
73958329
NA
1318static ssize_t bonding_show_packets_per_slave(struct device *d,
1319 struct device_attribute *attr,
1320 char *buf)
1321{
1322 struct bonding *bond = to_bond(d);
a752a8b9 1323 unsigned int packets_per_slave = bond->params.packets_per_slave;
a752a8b9 1324 return sprintf(buf, "%u\n", packets_per_slave);
73958329
NA
1325}
1326
1327static ssize_t bonding_store_packets_per_slave(struct device *d,
1328 struct device_attribute *attr,
1329 const char *buf, size_t count)
1330{
1331 struct bonding *bond = to_bond(d);
aa59d851 1332 int ret;
c13ab3ff 1333
aa59d851
NA
1334 ret = bond_opt_tryset_rtnl(bond, BOND_OPT_PACKETS_PER_SLAVE,
1335 (char *)buf);
c13ab3ff 1336 if (!ret)
1337 ret = count;
1338
73958329
NA
1339 return ret;
1340}
1341
1342static DEVICE_ATTR(packets_per_slave, S_IRUGO | S_IWUSR,
1343 bonding_show_packets_per_slave,
1344 bonding_store_packets_per_slave);
1345
b76cdba9 1346static struct attribute *per_bond_attrs[] = {
43cb76d9
GKH
1347 &dev_attr_slaves.attr,
1348 &dev_attr_mode.attr,
dd957c57 1349 &dev_attr_fail_over_mac.attr,
43cb76d9 1350 &dev_attr_arp_validate.attr,
8599b52e 1351 &dev_attr_arp_all_targets.attr,
43cb76d9
GKH
1352 &dev_attr_arp_interval.attr,
1353 &dev_attr_arp_ip_target.attr,
1354 &dev_attr_downdelay.attr,
1355 &dev_attr_updelay.attr,
1356 &dev_attr_lacp_rate.attr,
fd989c83 1357 &dev_attr_ad_select.attr,
43cb76d9 1358 &dev_attr_xmit_hash_policy.attr,
ad246c99
BH
1359 &dev_attr_num_grat_arp.attr,
1360 &dev_attr_num_unsol_na.attr,
43cb76d9
GKH
1361 &dev_attr_miimon.attr,
1362 &dev_attr_primary.attr,
a549952a 1363 &dev_attr_primary_reselect.attr,
43cb76d9
GKH
1364 &dev_attr_use_carrier.attr,
1365 &dev_attr_active_slave.attr,
1366 &dev_attr_mii_status.attr,
1367 &dev_attr_ad_aggregator.attr,
1368 &dev_attr_ad_num_ports.attr,
1369 &dev_attr_ad_actor_key.attr,
1370 &dev_attr_ad_partner_key.attr,
1371 &dev_attr_ad_partner_mac.attr,
bb1d9123 1372 &dev_attr_queue_id.attr,
ebd8e497 1373 &dev_attr_all_slaves_active.attr,
c2952c31 1374 &dev_attr_resend_igmp.attr,
655f8919 1375 &dev_attr_min_links.attr,
7eacd038 1376 &dev_attr_lp_interval.attr,
73958329 1377 &dev_attr_packets_per_slave.attr,
b76cdba9
MW
1378 NULL,
1379};
1380
1381static struct attribute_group bonding_group = {
1382 .name = "bonding",
1383 .attrs = per_bond_attrs,
1384};
1385
1386/*
1387 * Initialize sysfs. This sets up the bonding_masters file in
1388 * /sys/class/net.
1389 */
4c22400a 1390int bond_create_sysfs(struct bond_net *bn)
b76cdba9 1391{
b8a9787e 1392 int ret;
b76cdba9 1393
4c22400a 1394 bn->class_attr_bonding_masters = class_attr_bonding_masters;
01718e36 1395 sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
4c22400a 1396
58292cbe
TH
1397 ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
1398 bn->net);
877cbd36
JV
1399 /*
1400 * Permit multiple loads of the module by ignoring failures to
1401 * create the bonding_masters sysfs file. Bonding devices
1402 * created by second or subsequent loads of the module will
1403 * not be listed in, or controllable by, bonding_masters, but
1404 * will have the usual "bonding" sysfs directory.
1405 *
1406 * This is done to preserve backwards compatibility for
1407 * initscripts/sysconfig, which load bonding multiple times to
1408 * configure multiple bonding devices.
1409 */
1410 if (ret == -EEXIST) {
38d2f38b 1411 /* Is someone being kinky and naming a device bonding_master? */
4c22400a 1412 if (__dev_get_by_name(bn->net,
38d2f38b 1413 class_attr_bonding_masters.attr.name))
a4aee5c8 1414 pr_err("network device named %s already exists in sysfs",
38d2f38b 1415 class_attr_bonding_masters.attr.name);
130aa61a 1416 ret = 0;
877cbd36 1417 }
b76cdba9
MW
1418
1419 return ret;
1420
1421}
1422
1423/*
1424 * Remove /sys/class/net/bonding_masters.
1425 */
4c22400a 1426void bond_destroy_sysfs(struct bond_net *bn)
b76cdba9 1427{
58292cbe 1428 netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
b76cdba9
MW
1429}
1430
1431/*
1432 * Initialize sysfs for each bond. This sets up and registers
1433 * the 'bondctl' directory for each individual bond under /sys/class/net.
1434 */
6151b3d4 1435void bond_prepare_sysfs_group(struct bonding *bond)
b76cdba9 1436{
6151b3d4 1437 bond->dev->sysfs_groups[0] = &bonding_group;
b76cdba9
MW
1438}
1439