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