bond_sysfs.c revision 4d1ae5fb752b2504cf2c3d79abdfb410a09ad928
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 *
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/sched.h>
29#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/string.h>
32#include <linux/netdevice.h>
33#include <linux/inetdevice.h>
34#include <linux/in.h>
35#include <linux/sysfs.h>
36#include <linux/ctype.h>
37#include <linux/inet.h>
38#include <linux/rtnetlink.h>
39#include <linux/etherdevice.h>
40#include <net/net_namespace.h>
41#include <net/netns/generic.h>
42#include <linux/nsproxy.h>
43
44#include "bonding.h"
45
46#define to_dev(obj)	container_of(obj, struct device, kobj)
47#define to_bond(cd)	((struct bonding *)(netdev_priv(to_net_dev(cd))))
48
49/*
50 * "show" function for the bond_masters attribute.
51 * The class parameter is ignored.
52 */
53static ssize_t bonding_show_bonds(struct class *cls,
54				  struct class_attribute *attr,
55				  char *buf)
56{
57	struct bond_net *bn =
58		container_of(attr, struct bond_net, class_attr_bonding_masters);
59	int res = 0;
60	struct bonding *bond;
61
62	rtnl_lock();
63
64	list_for_each_entry(bond, &bn->dev_list, bond_list) {
65		if (res > (PAGE_SIZE - IFNAMSIZ)) {
66			/* not enough space for another interface name */
67			if ((PAGE_SIZE - res) > 10)
68				res = PAGE_SIZE - 10;
69			res += sprintf(buf + res, "++more++ ");
70			break;
71		}
72		res += sprintf(buf + res, "%s ", bond->dev->name);
73	}
74	if (res)
75		buf[res-1] = '\n'; /* eat the leftover space */
76
77	rtnl_unlock();
78	return res;
79}
80
81static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
82{
83	struct bonding *bond;
84
85	list_for_each_entry(bond, &bn->dev_list, bond_list) {
86		if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
87			return bond->dev;
88	}
89	return NULL;
90}
91
92/*
93 * "store" function for the bond_masters attribute.  This is what
94 * creates and deletes entire bonds.
95 *
96 * The class parameter is ignored.
97 *
98 */
99
100static ssize_t bonding_store_bonds(struct class *cls,
101				   struct class_attribute *attr,
102				   const char *buffer, size_t count)
103{
104	struct bond_net *bn =
105		container_of(attr, struct bond_net, class_attr_bonding_masters);
106	char command[IFNAMSIZ + 1] = {0, };
107	char *ifname;
108	int rv, res = count;
109
110	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
111	ifname = command + 1;
112	if ((strlen(command) <= 1) ||
113	    !dev_valid_name(ifname))
114		goto err_no_cmd;
115
116	if (command[0] == '+') {
117		pr_info("%s is being created...\n", ifname);
118		rv = bond_create(bn->net, ifname);
119		if (rv) {
120			if (rv == -EEXIST)
121				pr_info("%s already exists.\n", ifname);
122			else
123				pr_info("%s creation failed.\n", ifname);
124			res = rv;
125		}
126	} else if (command[0] == '-') {
127		struct net_device *bond_dev;
128
129		rtnl_lock();
130		bond_dev = bond_get_by_name(bn, ifname);
131		if (bond_dev) {
132			pr_info("%s is being deleted...\n", ifname);
133			unregister_netdevice(bond_dev);
134		} else {
135			pr_err("unable to delete non-existent %s\n", ifname);
136			res = -ENODEV;
137		}
138		rtnl_unlock();
139	} else
140		goto err_no_cmd;
141
142	/* Always return either count or an error.  If you return 0, you'll
143	 * get called forever, which is bad.
144	 */
145	return res;
146
147err_no_cmd:
148	pr_err("no command found in bonding_masters. Use +ifname or -ifname.\n");
149	return -EPERM;
150}
151
152static const void *bonding_namespace(struct class *cls,
153				     const struct class_attribute *attr)
154{
155	const struct bond_net *bn =
156		container_of(attr, struct bond_net, class_attr_bonding_masters);
157	return bn->net;
158}
159
160/* class attribute for bond_masters file.  This ends up in /sys/class/net */
161static const struct class_attribute class_attr_bonding_masters = {
162	.attr = {
163		.name = "bonding_masters",
164		.mode = S_IWUSR | S_IRUGO,
165	},
166	.show = bonding_show_bonds,
167	.store = bonding_store_bonds,
168	.namespace = bonding_namespace,
169};
170
171/*
172 * Show the slaves in the current bond.
173 */
174static ssize_t bonding_show_slaves(struct device *d,
175				   struct device_attribute *attr, char *buf)
176{
177	struct bonding *bond = to_bond(d);
178	struct list_head *iter;
179	struct slave *slave;
180	int res = 0;
181
182	if (!rtnl_trylock())
183		return restart_syscall();
184
185	bond_for_each_slave(bond, slave, iter) {
186		if (res > (PAGE_SIZE - IFNAMSIZ)) {
187			/* not enough space for another interface name */
188			if ((PAGE_SIZE - res) > 10)
189				res = PAGE_SIZE - 10;
190			res += sprintf(buf + res, "++more++ ");
191			break;
192		}
193		res += sprintf(buf + res, "%s ", slave->dev->name);
194	}
195
196	rtnl_unlock();
197
198	if (res)
199		buf[res-1] = '\n'; /* eat the leftover space */
200
201	return res;
202}
203
204/*
205 * Set the slaves in the current bond.
206 * This is supposed to be only thin wrapper for bond_enslave and bond_release.
207 * All hard work should be done there.
208 */
209static ssize_t bonding_store_slaves(struct device *d,
210				    struct device_attribute *attr,
211				    const char *buffer, size_t count)
212{
213	char command[IFNAMSIZ + 1] = { 0, };
214	char *ifname;
215	int res, ret = count;
216	struct net_device *dev;
217	struct bonding *bond = to_bond(d);
218
219	if (!rtnl_trylock())
220		return restart_syscall();
221
222	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
223	ifname = command + 1;
224	if ((strlen(command) <= 1) ||
225	    !dev_valid_name(ifname))
226		goto err_no_cmd;
227
228	dev = __dev_get_by_name(dev_net(bond->dev), ifname);
229	if (!dev) {
230		pr_info("%s: Interface %s does not exist!\n",
231			bond->dev->name, ifname);
232		ret = -ENODEV;
233		goto out;
234	}
235
236	switch (command[0]) {
237	case '+':
238		pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
239		res = bond_enslave(bond->dev, dev);
240		break;
241
242	case '-':
243		pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
244		res = bond_release(bond->dev, dev);
245		break;
246
247	default:
248		goto err_no_cmd;
249	}
250
251	if (res)
252		ret = res;
253	goto out;
254
255err_no_cmd:
256	pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
257	       bond->dev->name);
258	ret = -EPERM;
259
260out:
261	rtnl_unlock();
262	return ret;
263}
264
265static DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves,
266		   bonding_store_slaves);
267
268/*
269 * Show and set the bonding mode.  The bond interface must be down to
270 * change the mode.
271 */
272static ssize_t bonding_show_mode(struct device *d,
273				 struct device_attribute *attr, char *buf)
274{
275	struct bonding *bond = to_bond(d);
276
277	return sprintf(buf, "%s %d\n",
278			bond_mode_tbl[bond->params.mode].modename,
279			bond->params.mode);
280}
281
282static ssize_t bonding_store_mode(struct device *d,
283				  struct device_attribute *attr,
284				  const char *buf, size_t count)
285{
286	int new_value, ret = count;
287	struct bonding *bond = to_bond(d);
288
289	if (!rtnl_trylock())
290		return restart_syscall();
291
292	if (bond->dev->flags & IFF_UP) {
293		pr_err("unable to update mode of %s because interface is up.\n",
294		       bond->dev->name);
295		ret = -EPERM;
296		goto out;
297	}
298
299	if (bond_has_slaves(bond)) {
300		pr_err("unable to update mode of %s because it has slaves.\n",
301			bond->dev->name);
302		ret = -EPERM;
303		goto out;
304	}
305
306	new_value = bond_parse_parm(buf, bond_mode_tbl);
307	if (new_value < 0)  {
308		pr_err("%s: Ignoring invalid mode value %.*s.\n",
309		       bond->dev->name, (int)strlen(buf) - 1, buf);
310		ret = -EINVAL;
311		goto out;
312	}
313	if ((new_value == BOND_MODE_ALB ||
314	     new_value == BOND_MODE_TLB) &&
315	    bond->params.arp_interval) {
316		pr_err("%s: %s mode is incompatible with arp monitoring.\n",
317		       bond->dev->name, bond_mode_tbl[new_value].modename);
318		ret = -EINVAL;
319		goto out;
320	}
321
322	/* don't cache arp_validate between modes */
323	bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
324	bond->params.mode = new_value;
325	pr_info("%s: setting mode to %s (%d).\n",
326		bond->dev->name, bond_mode_tbl[new_value].modename,
327		new_value);
328out:
329	rtnl_unlock();
330	return ret;
331}
332static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
333		   bonding_show_mode, bonding_store_mode);
334
335/*
336 * Show and set the bonding transmit hash method.
337 */
338static ssize_t bonding_show_xmit_hash(struct device *d,
339				      struct device_attribute *attr,
340				      char *buf)
341{
342	struct bonding *bond = to_bond(d);
343
344	return sprintf(buf, "%s %d\n",
345		       xmit_hashtype_tbl[bond->params.xmit_policy].modename,
346		       bond->params.xmit_policy);
347}
348
349static ssize_t bonding_store_xmit_hash(struct device *d,
350				       struct device_attribute *attr,
351				       const char *buf, size_t count)
352{
353	int new_value, ret = count;
354	struct bonding *bond = to_bond(d);
355
356	new_value = bond_parse_parm(buf, xmit_hashtype_tbl);
357	if (new_value < 0)  {
358		pr_err("%s: Ignoring invalid xmit hash policy value %.*s.\n",
359		       bond->dev->name,
360		       (int)strlen(buf) - 1, buf);
361		ret = -EINVAL;
362	} else {
363		bond->params.xmit_policy = new_value;
364		pr_info("%s: setting xmit hash policy to %s (%d).\n",
365			bond->dev->name,
366			xmit_hashtype_tbl[new_value].modename, new_value);
367	}
368
369	return ret;
370}
371static DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR,
372		   bonding_show_xmit_hash, bonding_store_xmit_hash);
373
374/*
375 * Show and set arp_validate.
376 */
377static ssize_t bonding_show_arp_validate(struct device *d,
378					 struct device_attribute *attr,
379					 char *buf)
380{
381	struct bonding *bond = to_bond(d);
382
383	return sprintf(buf, "%s %d\n",
384		       arp_validate_tbl[bond->params.arp_validate].modename,
385		       bond->params.arp_validate);
386}
387
388static ssize_t bonding_store_arp_validate(struct device *d,
389					  struct device_attribute *attr,
390					  const char *buf, size_t count)
391{
392	struct bonding *bond = to_bond(d);
393	int new_value, ret = count;
394
395	if (!rtnl_trylock())
396		return restart_syscall();
397	new_value = bond_parse_parm(buf, arp_validate_tbl);
398	if (new_value < 0) {
399		pr_err("%s: Ignoring invalid arp_validate value %s\n",
400		       bond->dev->name, buf);
401		ret = -EINVAL;
402		goto out;
403	}
404	if (bond->params.mode != BOND_MODE_ACTIVEBACKUP) {
405		pr_err("%s: arp_validate only supported in active-backup mode.\n",
406		       bond->dev->name);
407		ret = -EINVAL;
408		goto out;
409	}
410	pr_info("%s: setting arp_validate to %s (%d).\n",
411		bond->dev->name, arp_validate_tbl[new_value].modename,
412		new_value);
413
414	if (bond->dev->flags & IFF_UP) {
415		if (!new_value)
416			bond->recv_probe = NULL;
417		else if (bond->params.arp_interval)
418			bond->recv_probe = bond_arp_rcv;
419	}
420	bond->params.arp_validate = new_value;
421out:
422	rtnl_unlock();
423
424	return ret;
425}
426
427static DEVICE_ATTR(arp_validate, S_IRUGO | S_IWUSR, bonding_show_arp_validate,
428		   bonding_store_arp_validate);
429/*
430 * Show and set arp_all_targets.
431 */
432static ssize_t bonding_show_arp_all_targets(struct device *d,
433					 struct device_attribute *attr,
434					 char *buf)
435{
436	struct bonding *bond = to_bond(d);
437	int value = bond->params.arp_all_targets;
438
439	return sprintf(buf, "%s %d\n", arp_all_targets_tbl[value].modename,
440		       value);
441}
442
443static ssize_t bonding_store_arp_all_targets(struct device *d,
444					  struct device_attribute *attr,
445					  const char *buf, size_t count)
446{
447	struct bonding *bond = to_bond(d);
448	int new_value;
449
450	new_value = bond_parse_parm(buf, arp_all_targets_tbl);
451	if (new_value < 0) {
452		pr_err("%s: Ignoring invalid arp_all_targets value %s\n",
453		       bond->dev->name, buf);
454		return -EINVAL;
455	}
456	pr_info("%s: setting arp_all_targets to %s (%d).\n",
457		bond->dev->name, arp_all_targets_tbl[new_value].modename,
458		new_value);
459
460	bond->params.arp_all_targets = new_value;
461
462	return count;
463}
464
465static DEVICE_ATTR(arp_all_targets, S_IRUGO | S_IWUSR,
466		   bonding_show_arp_all_targets, bonding_store_arp_all_targets);
467
468/*
469 * Show and store fail_over_mac.  User only allowed to change the
470 * value when there are no slaves.
471 */
472static ssize_t bonding_show_fail_over_mac(struct device *d,
473					  struct device_attribute *attr,
474					  char *buf)
475{
476	struct bonding *bond = to_bond(d);
477
478	return sprintf(buf, "%s %d\n",
479		       fail_over_mac_tbl[bond->params.fail_over_mac].modename,
480		       bond->params.fail_over_mac);
481}
482
483static ssize_t bonding_store_fail_over_mac(struct device *d,
484					   struct device_attribute *attr,
485					   const char *buf, size_t count)
486{
487	int new_value, ret = count;
488	struct bonding *bond = to_bond(d);
489
490	if (!rtnl_trylock())
491		return restart_syscall();
492
493	if (bond_has_slaves(bond)) {
494		pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
495		       bond->dev->name);
496		ret = -EPERM;
497		goto out;
498	}
499
500	new_value = bond_parse_parm(buf, fail_over_mac_tbl);
501	if (new_value < 0) {
502		pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
503		       bond->dev->name, buf);
504		ret = -EINVAL;
505		goto out;
506	}
507
508	bond->params.fail_over_mac = new_value;
509	pr_info("%s: Setting fail_over_mac to %s (%d).\n",
510		bond->dev->name, fail_over_mac_tbl[new_value].modename,
511		new_value);
512
513out:
514	rtnl_unlock();
515	return ret;
516}
517
518static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
519		   bonding_show_fail_over_mac, bonding_store_fail_over_mac);
520
521/*
522 * Show and set the arp timer interval.  There are two tricky bits
523 * here.  First, if ARP monitoring is activated, then we must disable
524 * MII monitoring.  Second, if the ARP timer isn't running, we must
525 * start it.
526 */
527static ssize_t bonding_show_arp_interval(struct device *d,
528					 struct device_attribute *attr,
529					 char *buf)
530{
531	struct bonding *bond = to_bond(d);
532
533	return sprintf(buf, "%d\n", bond->params.arp_interval);
534}
535
536static ssize_t bonding_store_arp_interval(struct device *d,
537					  struct device_attribute *attr,
538					  const char *buf, size_t count)
539{
540	struct bonding *bond = to_bond(d);
541	int new_value, ret = count;
542
543	if (!rtnl_trylock())
544		return restart_syscall();
545	if (sscanf(buf, "%d", &new_value) != 1) {
546		pr_err("%s: no arp_interval value specified.\n",
547		       bond->dev->name);
548		ret = -EINVAL;
549		goto out;
550	}
551	if (new_value < 0) {
552		pr_err("%s: Invalid arp_interval value %d not in range 0-%d; rejected.\n",
553		       bond->dev->name, new_value, INT_MAX);
554		ret = -EINVAL;
555		goto out;
556	}
557	if (bond->params.mode == BOND_MODE_ALB ||
558	    bond->params.mode == BOND_MODE_TLB) {
559		pr_info("%s: ARP monitoring cannot be used with ALB/TLB. Only MII monitoring is supported on %s.\n",
560			bond->dev->name, bond->dev->name);
561		ret = -EINVAL;
562		goto out;
563	}
564	pr_info("%s: Setting ARP monitoring interval to %d.\n",
565		bond->dev->name, new_value);
566	bond->params.arp_interval = new_value;
567	if (new_value) {
568		if (bond->params.miimon) {
569			pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
570				bond->dev->name, bond->dev->name);
571			bond->params.miimon = 0;
572		}
573		if (!bond->params.arp_targets[0])
574			pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
575				bond->dev->name);
576	}
577	if (bond->dev->flags & IFF_UP) {
578		/* If the interface is up, we may need to fire off
579		 * the ARP timer.  If the interface is down, the
580		 * timer will get fired off when the open function
581		 * is called.
582		 */
583		if (!new_value) {
584			if (bond->params.arp_validate)
585				bond->recv_probe = NULL;
586			cancel_delayed_work_sync(&bond->arp_work);
587		} else {
588			/* arp_validate can be set only in active-backup mode */
589			if (bond->params.arp_validate)
590				bond->recv_probe = bond_arp_rcv;
591			cancel_delayed_work_sync(&bond->mii_work);
592			queue_delayed_work(bond->wq, &bond->arp_work, 0);
593		}
594	}
595out:
596	rtnl_unlock();
597	return ret;
598}
599static DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR,
600		   bonding_show_arp_interval, bonding_store_arp_interval);
601
602/*
603 * Show and set the arp targets.
604 */
605static ssize_t bonding_show_arp_targets(struct device *d,
606					struct device_attribute *attr,
607					char *buf)
608{
609	int i, res = 0;
610	struct bonding *bond = to_bond(d);
611
612	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
613		if (bond->params.arp_targets[i])
614			res += sprintf(buf + res, "%pI4 ",
615				       &bond->params.arp_targets[i]);
616	}
617	if (res)
618		buf[res-1] = '\n'; /* eat the leftover space */
619	return res;
620}
621
622static ssize_t bonding_store_arp_targets(struct device *d,
623					 struct device_attribute *attr,
624					 const char *buf, size_t count)
625{
626	struct bonding *bond = to_bond(d);
627	struct list_head *iter;
628	struct slave *slave;
629	__be32 newtarget, *targets;
630	unsigned long *targets_rx;
631	int ind, i, j, ret = -EINVAL;
632
633	if (!rtnl_trylock())
634		return restart_syscall();
635
636	targets = bond->params.arp_targets;
637	newtarget = in_aton(buf + 1);
638	/* look for adds */
639	if (buf[0] == '+') {
640		if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
641			pr_err("%s: invalid ARP target %pI4 specified for addition\n",
642			       bond->dev->name, &newtarget);
643			goto out;
644		}
645
646		if (bond_get_targets_ip(targets, newtarget) != -1) { /* dup */
647			pr_err("%s: ARP target %pI4 is already present\n",
648			       bond->dev->name, &newtarget);
649			goto out;
650		}
651
652		ind = bond_get_targets_ip(targets, 0); /* first free slot */
653		if (ind == -1) {
654			pr_err("%s: ARP target table is full!\n",
655			       bond->dev->name);
656			goto out;
657		}
658
659		pr_info("%s: adding ARP target %pI4.\n", bond->dev->name,
660			 &newtarget);
661		/* not to race with bond_arp_rcv */
662		write_lock_bh(&bond->lock);
663		bond_for_each_slave(bond, slave, iter)
664			slave->target_last_arp_rx[ind] = jiffies;
665		targets[ind] = newtarget;
666		write_unlock_bh(&bond->lock);
667	} else if (buf[0] == '-')	{
668		if ((newtarget == 0) || (newtarget == htonl(INADDR_BROADCAST))) {
669			pr_err("%s: invalid ARP target %pI4 specified for removal\n",
670			       bond->dev->name, &newtarget);
671			goto out;
672		}
673
674		ind = bond_get_targets_ip(targets, newtarget);
675		if (ind == -1) {
676			pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
677				bond->dev->name, &newtarget);
678			goto out;
679		}
680
681		if (ind == 0 && !targets[1] && bond->params.arp_interval)
682			pr_warn("%s: removing last arp target with arp_interval on\n",
683				bond->dev->name);
684
685		pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
686			&newtarget);
687
688		write_lock_bh(&bond->lock);
689		bond_for_each_slave(bond, slave, iter) {
690			targets_rx = slave->target_last_arp_rx;
691			j = ind;
692			for (; (j < BOND_MAX_ARP_TARGETS-1) && targets[j+1]; j++)
693				targets_rx[j] = targets_rx[j+1];
694			targets_rx[j] = 0;
695		}
696		for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
697			targets[i] = targets[i+1];
698		targets[i] = 0;
699		write_unlock_bh(&bond->lock);
700	} else {
701		pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
702		       bond->dev->name);
703		ret = -EPERM;
704		goto out;
705	}
706
707	ret = count;
708out:
709	rtnl_unlock();
710	return ret;
711}
712static DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);
713
714/*
715 * Show and set the up and down delays.  These must be multiples of the
716 * MII monitoring value, and are stored internally as the multiplier.
717 * Thus, we must translate to MS for the real world.
718 */
719static ssize_t bonding_show_downdelay(struct device *d,
720				      struct device_attribute *attr,
721				      char *buf)
722{
723	struct bonding *bond = to_bond(d);
724
725	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
726}
727
728static ssize_t bonding_store_downdelay(struct device *d,
729				       struct device_attribute *attr,
730				       const char *buf, size_t count)
731{
732	int new_value, ret = count;
733	struct bonding *bond = to_bond(d);
734
735	if (!(bond->params.miimon)) {
736		pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
737		       bond->dev->name);
738		ret = -EPERM;
739		goto out;
740	}
741
742	if (sscanf(buf, "%d", &new_value) != 1) {
743		pr_err("%s: no down delay value specified.\n", bond->dev->name);
744		ret = -EINVAL;
745		goto out;
746	}
747	if (new_value < 0) {
748		pr_err("%s: Invalid down delay value %d not in range %d-%d; rejected.\n",
749		       bond->dev->name, new_value, 0, INT_MAX);
750		ret = -EINVAL;
751		goto out;
752	} else {
753		if ((new_value % bond->params.miimon) != 0) {
754			pr_warning("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
755				   bond->dev->name, new_value,
756				   bond->params.miimon,
757				   (new_value / bond->params.miimon) *
758				   bond->params.miimon);
759		}
760		bond->params.downdelay = new_value / bond->params.miimon;
761		pr_info("%s: Setting down delay to %d.\n",
762			bond->dev->name,
763			bond->params.downdelay * bond->params.miimon);
764
765	}
766
767out:
768	return ret;
769}
770static DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR,
771		   bonding_show_downdelay, bonding_store_downdelay);
772
773static ssize_t bonding_show_updelay(struct device *d,
774				    struct device_attribute *attr,
775				    char *buf)
776{
777	struct bonding *bond = to_bond(d);
778
779	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
780
781}
782
783static ssize_t bonding_store_updelay(struct device *d,
784				     struct device_attribute *attr,
785				     const char *buf, size_t count)
786{
787	int new_value, ret = count;
788	struct bonding *bond = to_bond(d);
789
790	if (!(bond->params.miimon)) {
791		pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
792		       bond->dev->name);
793		ret = -EPERM;
794		goto out;
795	}
796
797	if (sscanf(buf, "%d", &new_value) != 1) {
798		pr_err("%s: no up delay value specified.\n",
799		       bond->dev->name);
800		ret = -EINVAL;
801		goto out;
802	}
803	if (new_value < 0) {
804		pr_err("%s: Invalid up delay value %d not in range %d-%d; rejected.\n",
805		       bond->dev->name, new_value, 0, INT_MAX);
806		ret = -EINVAL;
807		goto out;
808	} else {
809		if ((new_value % bond->params.miimon) != 0) {
810			pr_warning("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
811				   bond->dev->name, new_value,
812				   bond->params.miimon,
813				   (new_value / bond->params.miimon) *
814				   bond->params.miimon);
815		}
816		bond->params.updelay = new_value / bond->params.miimon;
817		pr_info("%s: Setting up delay to %d.\n",
818			bond->dev->name,
819			bond->params.updelay * bond->params.miimon);
820	}
821
822out:
823	return ret;
824}
825static DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR,
826		   bonding_show_updelay, bonding_store_updelay);
827
828/*
829 * Show and set the LACP interval.  Interface must be down, and the mode
830 * must be set to 802.3ad mode.
831 */
832static ssize_t bonding_show_lacp(struct device *d,
833				 struct device_attribute *attr,
834				 char *buf)
835{
836	struct bonding *bond = to_bond(d);
837
838	return sprintf(buf, "%s %d\n",
839		bond_lacp_tbl[bond->params.lacp_fast].modename,
840		bond->params.lacp_fast);
841}
842
843static ssize_t bonding_store_lacp(struct device *d,
844				  struct device_attribute *attr,
845				  const char *buf, size_t count)
846{
847	struct bonding *bond = to_bond(d);
848	int new_value, ret = count;
849
850	if (!rtnl_trylock())
851		return restart_syscall();
852
853	if (bond->dev->flags & IFF_UP) {
854		pr_err("%s: Unable to update LACP rate because interface is up.\n",
855		       bond->dev->name);
856		ret = -EPERM;
857		goto out;
858	}
859
860	if (bond->params.mode != BOND_MODE_8023AD) {
861		pr_err("%s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
862		       bond->dev->name);
863		ret = -EPERM;
864		goto out;
865	}
866
867	new_value = bond_parse_parm(buf, bond_lacp_tbl);
868
869	if ((new_value == 1) || (new_value == 0)) {
870		bond->params.lacp_fast = new_value;
871		bond_3ad_update_lacp_rate(bond);
872		pr_info("%s: Setting LACP rate to %s (%d).\n",
873			bond->dev->name, bond_lacp_tbl[new_value].modename,
874			new_value);
875	} else {
876		pr_err("%s: Ignoring invalid LACP rate value %.*s.\n",
877		       bond->dev->name, (int)strlen(buf) - 1, buf);
878		ret = -EINVAL;
879	}
880out:
881	rtnl_unlock();
882
883	return ret;
884}
885static DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR,
886		   bonding_show_lacp, bonding_store_lacp);
887
888static ssize_t bonding_show_min_links(struct device *d,
889				      struct device_attribute *attr,
890				      char *buf)
891{
892	struct bonding *bond = to_bond(d);
893
894	return sprintf(buf, "%d\n", bond->params.min_links);
895}
896
897static ssize_t bonding_store_min_links(struct device *d,
898				       struct device_attribute *attr,
899				       const char *buf, size_t count)
900{
901	struct bonding *bond = to_bond(d);
902	int ret;
903	unsigned int new_value;
904
905	ret = kstrtouint(buf, 0, &new_value);
906	if (ret < 0) {
907		pr_err("%s: Ignoring invalid min links value %s.\n",
908		       bond->dev->name, buf);
909		return ret;
910	}
911
912	pr_info("%s: Setting min links value to %u\n",
913		bond->dev->name, new_value);
914	bond->params.min_links = new_value;
915	return count;
916}
917static DEVICE_ATTR(min_links, S_IRUGO | S_IWUSR,
918		   bonding_show_min_links, bonding_store_min_links);
919
920static ssize_t bonding_show_ad_select(struct device *d,
921				      struct device_attribute *attr,
922				      char *buf)
923{
924	struct bonding *bond = to_bond(d);
925
926	return sprintf(buf, "%s %d\n",
927		ad_select_tbl[bond->params.ad_select].modename,
928		bond->params.ad_select);
929}
930
931
932static ssize_t bonding_store_ad_select(struct device *d,
933				       struct device_attribute *attr,
934				       const char *buf, size_t count)
935{
936	int new_value, ret = count;
937	struct bonding *bond = to_bond(d);
938
939	if (bond->dev->flags & IFF_UP) {
940		pr_err("%s: Unable to update ad_select because interface is up.\n",
941		       bond->dev->name);
942		ret = -EPERM;
943		goto out;
944	}
945
946	new_value = bond_parse_parm(buf, ad_select_tbl);
947
948	if (new_value != -1) {
949		bond->params.ad_select = new_value;
950		pr_info("%s: Setting ad_select to %s (%d).\n",
951			bond->dev->name, ad_select_tbl[new_value].modename,
952			new_value);
953	} else {
954		pr_err("%s: Ignoring invalid ad_select value %.*s.\n",
955		       bond->dev->name, (int)strlen(buf) - 1, buf);
956		ret = -EINVAL;
957	}
958out:
959	return ret;
960}
961static DEVICE_ATTR(ad_select, S_IRUGO | S_IWUSR,
962		   bonding_show_ad_select, bonding_store_ad_select);
963
964/*
965 * Show and set the number of peer notifications to send after a failover event.
966 */
967static ssize_t bonding_show_num_peer_notif(struct device *d,
968					   struct device_attribute *attr,
969					   char *buf)
970{
971	struct bonding *bond = to_bond(d);
972	return sprintf(buf, "%d\n", bond->params.num_peer_notif);
973}
974
975static ssize_t bonding_store_num_peer_notif(struct device *d,
976					    struct device_attribute *attr,
977					    const char *buf, size_t count)
978{
979	struct bonding *bond = to_bond(d);
980	int err = kstrtou8(buf, 10, &bond->params.num_peer_notif);
981	return err ? err : count;
982}
983static DEVICE_ATTR(num_grat_arp, S_IRUGO | S_IWUSR,
984		   bonding_show_num_peer_notif, bonding_store_num_peer_notif);
985static DEVICE_ATTR(num_unsol_na, S_IRUGO | S_IWUSR,
986		   bonding_show_num_peer_notif, bonding_store_num_peer_notif);
987
988/*
989 * Show and set the MII monitor interval.  There are two tricky bits
990 * here.  First, if MII monitoring is activated, then we must disable
991 * ARP monitoring.  Second, if the timer isn't running, we must
992 * start it.
993 */
994static ssize_t bonding_show_miimon(struct device *d,
995				   struct device_attribute *attr,
996				   char *buf)
997{
998	struct bonding *bond = to_bond(d);
999
1000	return sprintf(buf, "%d\n", bond->params.miimon);
1001}
1002
1003static ssize_t bonding_store_miimon(struct device *d,
1004				    struct device_attribute *attr,
1005				    const char *buf, size_t count)
1006{
1007	int new_value, ret = count;
1008	struct bonding *bond = to_bond(d);
1009
1010	if (!rtnl_trylock())
1011		return restart_syscall();
1012	if (sscanf(buf, "%d", &new_value) != 1) {
1013		pr_err("%s: no miimon value specified.\n",
1014		       bond->dev->name);
1015		ret = -EINVAL;
1016		goto out;
1017	}
1018	if (new_value < 0) {
1019		pr_err("%s: Invalid miimon value %d not in range %d-%d; rejected.\n",
1020		       bond->dev->name, new_value, 0, INT_MAX);
1021		ret = -EINVAL;
1022		goto out;
1023	}
1024	pr_info("%s: Setting MII monitoring interval to %d.\n",
1025		bond->dev->name, new_value);
1026	bond->params.miimon = new_value;
1027	if (bond->params.updelay)
1028		pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
1029			bond->dev->name,
1030			bond->params.updelay * bond->params.miimon);
1031	if (bond->params.downdelay)
1032		pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
1033			bond->dev->name,
1034			bond->params.downdelay * bond->params.miimon);
1035	if (new_value && bond->params.arp_interval) {
1036		pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
1037			bond->dev->name);
1038		bond->params.arp_interval = 0;
1039		if (bond->params.arp_validate)
1040			bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
1041	}
1042	if (bond->dev->flags & IFF_UP) {
1043		/* If the interface is up, we may need to fire off
1044		 * the MII timer. If the interface is down, the
1045		 * timer will get fired off when the open function
1046		 * is called.
1047		 */
1048		if (!new_value) {
1049			cancel_delayed_work_sync(&bond->mii_work);
1050		} else {
1051			cancel_delayed_work_sync(&bond->arp_work);
1052			queue_delayed_work(bond->wq, &bond->mii_work, 0);
1053		}
1054	}
1055out:
1056	rtnl_unlock();
1057	return ret;
1058}
1059static DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR,
1060		   bonding_show_miimon, bonding_store_miimon);
1061
1062/*
1063 * Show and set the primary slave.  The store function is much
1064 * simpler than bonding_store_slaves function because it only needs to
1065 * handle one interface name.
1066 * The bond must be a mode that supports a primary for this be
1067 * set.
1068 */
1069static ssize_t bonding_show_primary(struct device *d,
1070				    struct device_attribute *attr,
1071				    char *buf)
1072{
1073	int count = 0;
1074	struct bonding *bond = to_bond(d);
1075
1076	if (bond->primary_slave)
1077		count = sprintf(buf, "%s\n", bond->primary_slave->dev->name);
1078
1079	return count;
1080}
1081
1082static ssize_t bonding_store_primary(struct device *d,
1083				     struct device_attribute *attr,
1084				     const char *buf, size_t count)
1085{
1086	struct bonding *bond = to_bond(d);
1087	struct list_head *iter;
1088	char ifname[IFNAMSIZ];
1089	struct slave *slave;
1090
1091	if (!rtnl_trylock())
1092		return restart_syscall();
1093	block_netpoll_tx();
1094	read_lock(&bond->lock);
1095	write_lock_bh(&bond->curr_slave_lock);
1096
1097	if (!USES_PRIMARY(bond->params.mode)) {
1098		pr_info("%s: Unable to set primary slave; %s is in mode %d\n",
1099			bond->dev->name, bond->dev->name, bond->params.mode);
1100		goto out;
1101	}
1102
1103	sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1104
1105	/* check to see if we are clearing primary */
1106	if (!strlen(ifname) || buf[0] == '\n') {
1107		pr_info("%s: Setting primary slave to None.\n",
1108			bond->dev->name);
1109		bond->primary_slave = NULL;
1110		memset(bond->params.primary, 0, sizeof(bond->params.primary));
1111		bond_select_active_slave(bond);
1112		goto out;
1113	}
1114
1115	bond_for_each_slave(bond, slave, iter) {
1116		if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1117			pr_info("%s: Setting %s as primary slave.\n",
1118				bond->dev->name, slave->dev->name);
1119			bond->primary_slave = slave;
1120			strcpy(bond->params.primary, slave->dev->name);
1121			bond_select_active_slave(bond);
1122			goto out;
1123		}
1124	}
1125
1126	strncpy(bond->params.primary, ifname, IFNAMSIZ);
1127	bond->params.primary[IFNAMSIZ - 1] = 0;
1128
1129	pr_info("%s: Recording %s as primary, "
1130		"but it has not been enslaved to %s yet.\n",
1131		bond->dev->name, ifname, bond->dev->name);
1132out:
1133	write_unlock_bh(&bond->curr_slave_lock);
1134	read_unlock(&bond->lock);
1135	unblock_netpoll_tx();
1136	rtnl_unlock();
1137
1138	return count;
1139}
1140static DEVICE_ATTR(primary, S_IRUGO | S_IWUSR,
1141		   bonding_show_primary, bonding_store_primary);
1142
1143/*
1144 * Show and set the primary_reselect flag.
1145 */
1146static ssize_t bonding_show_primary_reselect(struct device *d,
1147					     struct device_attribute *attr,
1148					     char *buf)
1149{
1150	struct bonding *bond = to_bond(d);
1151
1152	return sprintf(buf, "%s %d\n",
1153		       pri_reselect_tbl[bond->params.primary_reselect].modename,
1154		       bond->params.primary_reselect);
1155}
1156
1157static ssize_t bonding_store_primary_reselect(struct device *d,
1158					      struct device_attribute *attr,
1159					      const char *buf, size_t count)
1160{
1161	int new_value, ret = count;
1162	struct bonding *bond = to_bond(d);
1163
1164	if (!rtnl_trylock())
1165		return restart_syscall();
1166
1167	new_value = bond_parse_parm(buf, pri_reselect_tbl);
1168	if (new_value < 0)  {
1169		pr_err("%s: Ignoring invalid primary_reselect value %.*s.\n",
1170		       bond->dev->name,
1171		       (int) strlen(buf) - 1, buf);
1172		ret = -EINVAL;
1173		goto out;
1174	}
1175
1176	bond->params.primary_reselect = new_value;
1177	pr_info("%s: setting primary_reselect to %s (%d).\n",
1178		bond->dev->name, pri_reselect_tbl[new_value].modename,
1179		new_value);
1180
1181	block_netpoll_tx();
1182	read_lock(&bond->lock);
1183	write_lock_bh(&bond->curr_slave_lock);
1184	bond_select_active_slave(bond);
1185	write_unlock_bh(&bond->curr_slave_lock);
1186	read_unlock(&bond->lock);
1187	unblock_netpoll_tx();
1188out:
1189	rtnl_unlock();
1190	return ret;
1191}
1192static DEVICE_ATTR(primary_reselect, S_IRUGO | S_IWUSR,
1193		   bonding_show_primary_reselect,
1194		   bonding_store_primary_reselect);
1195
1196/*
1197 * Show and set the use_carrier flag.
1198 */
1199static ssize_t bonding_show_carrier(struct device *d,
1200				    struct device_attribute *attr,
1201				    char *buf)
1202{
1203	struct bonding *bond = to_bond(d);
1204
1205	return sprintf(buf, "%d\n", bond->params.use_carrier);
1206}
1207
1208static ssize_t bonding_store_carrier(struct device *d,
1209				     struct device_attribute *attr,
1210				     const char *buf, size_t count)
1211{
1212	int new_value, ret = count;
1213	struct bonding *bond = to_bond(d);
1214
1215
1216	if (sscanf(buf, "%d", &new_value) != 1) {
1217		pr_err("%s: no use_carrier value specified.\n",
1218		       bond->dev->name);
1219		ret = -EINVAL;
1220		goto out;
1221	}
1222	if ((new_value == 0) || (new_value == 1)) {
1223		bond->params.use_carrier = new_value;
1224		pr_info("%s: Setting use_carrier to %d.\n",
1225			bond->dev->name, new_value);
1226	} else {
1227		pr_info("%s: Ignoring invalid use_carrier value %d.\n",
1228			bond->dev->name, new_value);
1229	}
1230out:
1231	return ret;
1232}
1233static DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR,
1234		   bonding_show_carrier, bonding_store_carrier);
1235
1236
1237/*
1238 * Show and set currently active_slave.
1239 */
1240static ssize_t bonding_show_active_slave(struct device *d,
1241					 struct device_attribute *attr,
1242					 char *buf)
1243{
1244	struct bonding *bond = to_bond(d);
1245	struct slave *curr;
1246	int count = 0;
1247
1248	rcu_read_lock();
1249	curr = rcu_dereference(bond->curr_active_slave);
1250	if (USES_PRIMARY(bond->params.mode) && curr)
1251		count = sprintf(buf, "%s\n", curr->dev->name);
1252	rcu_read_unlock();
1253
1254	return count;
1255}
1256
1257static ssize_t bonding_store_active_slave(struct device *d,
1258					  struct device_attribute *attr,
1259					  const char *buf, size_t count)
1260{
1261	struct slave *slave, *old_active, *new_active;
1262	struct bonding *bond = to_bond(d);
1263	struct list_head *iter;
1264	char ifname[IFNAMSIZ];
1265
1266	if (!rtnl_trylock())
1267		return restart_syscall();
1268
1269	old_active = new_active = NULL;
1270	block_netpoll_tx();
1271	read_lock(&bond->lock);
1272	write_lock_bh(&bond->curr_slave_lock);
1273
1274	if (!USES_PRIMARY(bond->params.mode)) {
1275		pr_info("%s: Unable to change active slave; %s is in mode %d\n",
1276			bond->dev->name, bond->dev->name, bond->params.mode);
1277		goto out;
1278	}
1279
1280	sscanf(buf, "%15s", ifname); /* IFNAMSIZ */
1281
1282	/* check to see if we are clearing active */
1283	if (!strlen(ifname) || buf[0] == '\n') {
1284		pr_info("%s: Clearing current active slave.\n",
1285			bond->dev->name);
1286		rcu_assign_pointer(bond->curr_active_slave, NULL);
1287		bond_select_active_slave(bond);
1288		goto out;
1289	}
1290
1291	bond_for_each_slave(bond, slave, iter) {
1292		if (strncmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
1293			old_active = bond->curr_active_slave;
1294			new_active = slave;
1295			if (new_active == old_active) {
1296				/* do nothing */
1297				pr_info("%s: %s is already the current"
1298					" active slave.\n",
1299					bond->dev->name,
1300					slave->dev->name);
1301				goto out;
1302			} else {
1303				if ((new_active) &&
1304				    (old_active) &&
1305				    (new_active->link == BOND_LINK_UP) &&
1306				    IS_UP(new_active->dev)) {
1307					pr_info("%s: Setting %s as active"
1308						" slave.\n",
1309						bond->dev->name,
1310						slave->dev->name);
1311					bond_change_active_slave(bond,
1312								 new_active);
1313				} else {
1314					pr_info("%s: Could not set %s as"
1315						" active slave; either %s is"
1316						" down or the link is down.\n",
1317						bond->dev->name,
1318						slave->dev->name,
1319						slave->dev->name);
1320				}
1321				goto out;
1322			}
1323		}
1324	}
1325
1326	pr_info("%s: Unable to set %.*s as active slave.\n",
1327		bond->dev->name, (int)strlen(buf) - 1, buf);
1328 out:
1329	write_unlock_bh(&bond->curr_slave_lock);
1330	read_unlock(&bond->lock);
1331	unblock_netpoll_tx();
1332
1333	rtnl_unlock();
1334
1335	return count;
1336
1337}
1338static DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR,
1339		   bonding_show_active_slave, bonding_store_active_slave);
1340
1341
1342/*
1343 * Show link status of the bond interface.
1344 */
1345static ssize_t bonding_show_mii_status(struct device *d,
1346				       struct device_attribute *attr,
1347				       char *buf)
1348{
1349	struct bonding *bond = to_bond(d);
1350
1351	return sprintf(buf, "%s\n", bond->curr_active_slave ? "up" : "down");
1352}
1353static DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);
1354
1355/*
1356 * Show current 802.3ad aggregator ID.
1357 */
1358static ssize_t bonding_show_ad_aggregator(struct device *d,
1359					  struct device_attribute *attr,
1360					  char *buf)
1361{
1362	int count = 0;
1363	struct bonding *bond = to_bond(d);
1364
1365	if (bond->params.mode == BOND_MODE_8023AD) {
1366		struct ad_info ad_info;
1367		count = sprintf(buf, "%d\n",
1368				bond_3ad_get_active_agg_info(bond, &ad_info)
1369				?  0 : ad_info.aggregator_id);
1370	}
1371
1372	return count;
1373}
1374static DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);
1375
1376
1377/*
1378 * Show number of active 802.3ad ports.
1379 */
1380static ssize_t bonding_show_ad_num_ports(struct device *d,
1381					 struct device_attribute *attr,
1382					 char *buf)
1383{
1384	int count = 0;
1385	struct bonding *bond = to_bond(d);
1386
1387	if (bond->params.mode == BOND_MODE_8023AD) {
1388		struct ad_info ad_info;
1389		count = sprintf(buf, "%d\n",
1390				bond_3ad_get_active_agg_info(bond, &ad_info)
1391				?  0 : ad_info.ports);
1392	}
1393
1394	return count;
1395}
1396static DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);
1397
1398
1399/*
1400 * Show current 802.3ad actor key.
1401 */
1402static ssize_t bonding_show_ad_actor_key(struct device *d,
1403					 struct device_attribute *attr,
1404					 char *buf)
1405{
1406	int count = 0;
1407	struct bonding *bond = to_bond(d);
1408
1409	if (bond->params.mode == BOND_MODE_8023AD) {
1410		struct ad_info ad_info;
1411		count = sprintf(buf, "%d\n",
1412				bond_3ad_get_active_agg_info(bond, &ad_info)
1413				?  0 : ad_info.actor_key);
1414	}
1415
1416	return count;
1417}
1418static DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);
1419
1420
1421/*
1422 * Show current 802.3ad partner key.
1423 */
1424static ssize_t bonding_show_ad_partner_key(struct device *d,
1425					   struct device_attribute *attr,
1426					   char *buf)
1427{
1428	int count = 0;
1429	struct bonding *bond = to_bond(d);
1430
1431	if (bond->params.mode == BOND_MODE_8023AD) {
1432		struct ad_info ad_info;
1433		count = sprintf(buf, "%d\n",
1434				bond_3ad_get_active_agg_info(bond, &ad_info)
1435				?  0 : ad_info.partner_key);
1436	}
1437
1438	return count;
1439}
1440static DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);
1441
1442
1443/*
1444 * Show current 802.3ad partner mac.
1445 */
1446static ssize_t bonding_show_ad_partner_mac(struct device *d,
1447					   struct device_attribute *attr,
1448					   char *buf)
1449{
1450	int count = 0;
1451	struct bonding *bond = to_bond(d);
1452
1453	if (bond->params.mode == BOND_MODE_8023AD) {
1454		struct ad_info ad_info;
1455		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
1456			count = sprintf(buf, "%pM\n", ad_info.partner_system);
1457	}
1458
1459	return count;
1460}
1461static DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);
1462
1463/*
1464 * Show the queue_ids of the slaves in the current bond.
1465 */
1466static ssize_t bonding_show_queue_id(struct device *d,
1467				     struct device_attribute *attr,
1468				     char *buf)
1469{
1470	struct bonding *bond = to_bond(d);
1471	struct list_head *iter;
1472	struct slave *slave;
1473	int res = 0;
1474
1475	if (!rtnl_trylock())
1476		return restart_syscall();
1477
1478	bond_for_each_slave(bond, slave, iter) {
1479		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
1480			/* not enough space for another interface_name:queue_id pair */
1481			if ((PAGE_SIZE - res) > 10)
1482				res = PAGE_SIZE - 10;
1483			res += sprintf(buf + res, "++more++ ");
1484			break;
1485		}
1486		res += sprintf(buf + res, "%s:%d ",
1487			       slave->dev->name, slave->queue_id);
1488	}
1489	if (res)
1490		buf[res-1] = '\n'; /* eat the leftover space */
1491
1492	rtnl_unlock();
1493
1494	return res;
1495}
1496
1497/*
1498 * Set the queue_ids of the  slaves in the current bond.  The bond
1499 * interface must be enslaved for this to work.
1500 */
1501static ssize_t bonding_store_queue_id(struct device *d,
1502				      struct device_attribute *attr,
1503				      const char *buffer, size_t count)
1504{
1505	struct slave *slave, *update_slave;
1506	struct bonding *bond = to_bond(d);
1507	struct list_head *iter;
1508	u16 qid;
1509	int ret = count;
1510	char *delim;
1511	struct net_device *sdev = NULL;
1512
1513	if (!rtnl_trylock())
1514		return restart_syscall();
1515
1516	/* delim will point to queue id if successful */
1517	delim = strchr(buffer, ':');
1518	if (!delim)
1519		goto err_no_cmd;
1520
1521	/*
1522	 * Terminate string that points to device name and bump it
1523	 * up one, so we can read the queue id there.
1524	 */
1525	*delim = '\0';
1526	if (sscanf(++delim, "%hd\n", &qid) != 1)
1527		goto err_no_cmd;
1528
1529	/* Check buffer length, valid ifname and queue id */
1530	if (strlen(buffer) > IFNAMSIZ ||
1531	    !dev_valid_name(buffer) ||
1532	    qid > bond->dev->real_num_tx_queues)
1533		goto err_no_cmd;
1534
1535	/* Get the pointer to that interface if it exists */
1536	sdev = __dev_get_by_name(dev_net(bond->dev), buffer);
1537	if (!sdev)
1538		goto err_no_cmd;
1539
1540	/* Search for thes slave and check for duplicate qids */
1541	update_slave = NULL;
1542	bond_for_each_slave(bond, slave, iter) {
1543		if (sdev == slave->dev)
1544			/*
1545			 * We don't need to check the matching
1546			 * slave for dups, since we're overwriting it
1547			 */
1548			update_slave = slave;
1549		else if (qid && qid == slave->queue_id) {
1550			goto err_no_cmd;
1551		}
1552	}
1553
1554	if (!update_slave)
1555		goto err_no_cmd;
1556
1557	/* Actually set the qids for the slave */
1558	update_slave->queue_id = qid;
1559
1560out:
1561	rtnl_unlock();
1562	return ret;
1563
1564err_no_cmd:
1565	pr_info("invalid input for queue_id set for %s.\n",
1566		bond->dev->name);
1567	ret = -EPERM;
1568	goto out;
1569}
1570
1571static DEVICE_ATTR(queue_id, S_IRUGO | S_IWUSR, bonding_show_queue_id,
1572		   bonding_store_queue_id);
1573
1574
1575/*
1576 * Show and set the all_slaves_active flag.
1577 */
1578static ssize_t bonding_show_slaves_active(struct device *d,
1579					  struct device_attribute *attr,
1580					  char *buf)
1581{
1582	struct bonding *bond = to_bond(d);
1583
1584	return sprintf(buf, "%d\n", bond->params.all_slaves_active);
1585}
1586
1587static ssize_t bonding_store_slaves_active(struct device *d,
1588					   struct device_attribute *attr,
1589					   const char *buf, size_t count)
1590{
1591	struct bonding *bond = to_bond(d);
1592	int new_value, ret = count;
1593	struct list_head *iter;
1594	struct slave *slave;
1595
1596	if (!rtnl_trylock())
1597		return restart_syscall();
1598
1599	if (sscanf(buf, "%d", &new_value) != 1) {
1600		pr_err("%s: no all_slaves_active value specified.\n",
1601		       bond->dev->name);
1602		ret = -EINVAL;
1603		goto out;
1604	}
1605
1606	if (new_value == bond->params.all_slaves_active)
1607		goto out;
1608
1609	if ((new_value == 0) || (new_value == 1)) {
1610		bond->params.all_slaves_active = new_value;
1611	} else {
1612		pr_info("%s: Ignoring invalid all_slaves_active value %d.\n",
1613			bond->dev->name, new_value);
1614		ret = -EINVAL;
1615		goto out;
1616	}
1617
1618	bond_for_each_slave(bond, slave, iter) {
1619		if (!bond_is_active_slave(slave)) {
1620			if (new_value)
1621				slave->inactive = 0;
1622			else
1623				slave->inactive = 1;
1624		}
1625	}
1626out:
1627	rtnl_unlock();
1628	return ret;
1629}
1630static DEVICE_ATTR(all_slaves_active, S_IRUGO | S_IWUSR,
1631		   bonding_show_slaves_active, bonding_store_slaves_active);
1632
1633/*
1634 * Show and set the number of IGMP membership reports to send on link failure
1635 */
1636static ssize_t bonding_show_resend_igmp(struct device *d,
1637					struct device_attribute *attr,
1638					char *buf)
1639{
1640	struct bonding *bond = to_bond(d);
1641
1642	return sprintf(buf, "%d\n", bond->params.resend_igmp);
1643}
1644
1645static ssize_t bonding_store_resend_igmp(struct device *d,
1646					 struct device_attribute *attr,
1647					 const char *buf, size_t count)
1648{
1649	int new_value, ret = count;
1650	struct bonding *bond = to_bond(d);
1651
1652	if (sscanf(buf, "%d", &new_value) != 1) {
1653		pr_err("%s: no resend_igmp value specified.\n",
1654		       bond->dev->name);
1655		ret = -EINVAL;
1656		goto out;
1657	}
1658
1659	if (new_value < 0 || new_value > 255) {
1660		pr_err("%s: Invalid resend_igmp value %d not in range 0-255; rejected.\n",
1661		       bond->dev->name, new_value);
1662		ret = -EINVAL;
1663		goto out;
1664	}
1665
1666	pr_info("%s: Setting resend_igmp to %d.\n",
1667		bond->dev->name, new_value);
1668	bond->params.resend_igmp = new_value;
1669out:
1670	return ret;
1671}
1672
1673static DEVICE_ATTR(resend_igmp, S_IRUGO | S_IWUSR,
1674		   bonding_show_resend_igmp, bonding_store_resend_igmp);
1675
1676
1677static ssize_t bonding_show_lp_interval(struct device *d,
1678					struct device_attribute *attr,
1679					char *buf)
1680{
1681	struct bonding *bond = to_bond(d);
1682	return sprintf(buf, "%d\n", bond->params.lp_interval);
1683}
1684
1685static ssize_t bonding_store_lp_interval(struct device *d,
1686					 struct device_attribute *attr,
1687					 const char *buf, size_t count)
1688{
1689	struct bonding *bond = to_bond(d);
1690	int new_value, ret = count;
1691
1692	if (sscanf(buf, "%d", &new_value) != 1) {
1693		pr_err("%s: no lp interval value specified.\n",
1694			bond->dev->name);
1695		ret = -EINVAL;
1696		goto out;
1697	}
1698
1699	if (new_value <= 0) {
1700		pr_err ("%s: lp_interval must be between 1 and %d\n",
1701			bond->dev->name, INT_MAX);
1702		ret = -EINVAL;
1703		goto out;
1704	}
1705
1706	bond->params.lp_interval = new_value;
1707out:
1708	return ret;
1709}
1710
1711static DEVICE_ATTR(lp_interval, S_IRUGO | S_IWUSR,
1712		   bonding_show_lp_interval, bonding_store_lp_interval);
1713
1714static struct attribute *per_bond_attrs[] = {
1715	&dev_attr_slaves.attr,
1716	&dev_attr_mode.attr,
1717	&dev_attr_fail_over_mac.attr,
1718	&dev_attr_arp_validate.attr,
1719	&dev_attr_arp_all_targets.attr,
1720	&dev_attr_arp_interval.attr,
1721	&dev_attr_arp_ip_target.attr,
1722	&dev_attr_downdelay.attr,
1723	&dev_attr_updelay.attr,
1724	&dev_attr_lacp_rate.attr,
1725	&dev_attr_ad_select.attr,
1726	&dev_attr_xmit_hash_policy.attr,
1727	&dev_attr_num_grat_arp.attr,
1728	&dev_attr_num_unsol_na.attr,
1729	&dev_attr_miimon.attr,
1730	&dev_attr_primary.attr,
1731	&dev_attr_primary_reselect.attr,
1732	&dev_attr_use_carrier.attr,
1733	&dev_attr_active_slave.attr,
1734	&dev_attr_mii_status.attr,
1735	&dev_attr_ad_aggregator.attr,
1736	&dev_attr_ad_num_ports.attr,
1737	&dev_attr_ad_actor_key.attr,
1738	&dev_attr_ad_partner_key.attr,
1739	&dev_attr_ad_partner_mac.attr,
1740	&dev_attr_queue_id.attr,
1741	&dev_attr_all_slaves_active.attr,
1742	&dev_attr_resend_igmp.attr,
1743	&dev_attr_min_links.attr,
1744	&dev_attr_lp_interval.attr,
1745	NULL,
1746};
1747
1748static struct attribute_group bonding_group = {
1749	.name = "bonding",
1750	.attrs = per_bond_attrs,
1751};
1752
1753/*
1754 * Initialize sysfs.  This sets up the bonding_masters file in
1755 * /sys/class/net.
1756 */
1757int bond_create_sysfs(struct bond_net *bn)
1758{
1759	int ret;
1760
1761	bn->class_attr_bonding_masters = class_attr_bonding_masters;
1762	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
1763
1764	ret = netdev_class_create_file(&bn->class_attr_bonding_masters);
1765	/*
1766	 * Permit multiple loads of the module by ignoring failures to
1767	 * create the bonding_masters sysfs file.  Bonding devices
1768	 * created by second or subsequent loads of the module will
1769	 * not be listed in, or controllable by, bonding_masters, but
1770	 * will have the usual "bonding" sysfs directory.
1771	 *
1772	 * This is done to preserve backwards compatibility for
1773	 * initscripts/sysconfig, which load bonding multiple times to
1774	 * configure multiple bonding devices.
1775	 */
1776	if (ret == -EEXIST) {
1777		/* Is someone being kinky and naming a device bonding_master? */
1778		if (__dev_get_by_name(bn->net,
1779				      class_attr_bonding_masters.attr.name))
1780			pr_err("network device named %s already exists in sysfs",
1781			       class_attr_bonding_masters.attr.name);
1782		ret = 0;
1783	}
1784
1785	return ret;
1786
1787}
1788
1789/*
1790 * Remove /sys/class/net/bonding_masters.
1791 */
1792void bond_destroy_sysfs(struct bond_net *bn)
1793{
1794	netdev_class_remove_file(&bn->class_attr_bonding_masters);
1795}
1796
1797/*
1798 * Initialize sysfs for each bond.  This sets up and registers
1799 * the 'bondctl' directory for each individual bond under /sys/class/net.
1800 */
1801void bond_prepare_sysfs_group(struct bonding *bond)
1802{
1803	bond->dev->sysfs_groups[0] = &bonding_group;
1804}
1805
1806