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