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