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