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