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