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