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