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