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