xref: /openbmc/linux/drivers/net/bonding/bond_sysfs.c (revision c2cd9d04)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright(c) 2004-2005 Intel Corporation. All rights reserved.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/device.h>
11 #include <linux/sched/signal.h>
12 #include <linux/fs.h>
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/netdevice.h>
16 #include <linux/inetdevice.h>
17 #include <linux/in.h>
18 #include <linux/sysfs.h>
19 #include <linux/ctype.h>
20 #include <linux/inet.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/etherdevice.h>
23 #include <net/net_namespace.h>
24 #include <net/netns/generic.h>
25 #include <linux/nsproxy.h>
26 
27 #include <net/bonding.h>
28 
29 #define to_bond(cd)	((struct bonding *)(netdev_priv(to_net_dev(cd))))
30 
31 /* "show" function for the bond_masters attribute.
32  * The class parameter is ignored.
33  */
34 static ssize_t bonding_show_bonds(struct class *cls,
35 				  struct class_attribute *attr,
36 				  char *buf)
37 {
38 	struct bond_net *bn =
39 		container_of(attr, struct bond_net, class_attr_bonding_masters);
40 	int res = 0;
41 	struct bonding *bond;
42 
43 	rtnl_lock();
44 
45 	list_for_each_entry(bond, &bn->dev_list, bond_list) {
46 		if (res > (PAGE_SIZE - IFNAMSIZ)) {
47 			/* not enough space for another interface name */
48 			if ((PAGE_SIZE - res) > 10)
49 				res = PAGE_SIZE - 10;
50 			res += sprintf(buf + res, "++more++ ");
51 			break;
52 		}
53 		res += sprintf(buf + res, "%s ", bond->dev->name);
54 	}
55 	if (res)
56 		buf[res-1] = '\n'; /* eat the leftover space */
57 
58 	rtnl_unlock();
59 	return res;
60 }
61 
62 static struct net_device *bond_get_by_name(struct bond_net *bn, const char *ifname)
63 {
64 	struct bonding *bond;
65 
66 	list_for_each_entry(bond, &bn->dev_list, bond_list) {
67 		if (strncmp(bond->dev->name, ifname, IFNAMSIZ) == 0)
68 			return bond->dev;
69 	}
70 	return NULL;
71 }
72 
73 /* "store" function for the bond_masters attribute.  This is what
74  * creates and deletes entire bonds.
75  *
76  * The class parameter is ignored.
77  */
78 static ssize_t bonding_store_bonds(struct class *cls,
79 				   struct class_attribute *attr,
80 				   const char *buffer, size_t count)
81 {
82 	struct bond_net *bn =
83 		container_of(attr, struct bond_net, class_attr_bonding_masters);
84 	char command[IFNAMSIZ + 1] = {0, };
85 	char *ifname;
86 	int rv, res = count;
87 
88 	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
89 	ifname = command + 1;
90 	if ((strlen(command) <= 1) ||
91 	    !dev_valid_name(ifname))
92 		goto err_no_cmd;
93 
94 	if (command[0] == '+') {
95 		pr_info("%s is being created...\n", ifname);
96 		rv = bond_create(bn->net, ifname);
97 		if (rv) {
98 			if (rv == -EEXIST)
99 				pr_info("%s already exists\n", ifname);
100 			else
101 				pr_info("%s creation failed\n", ifname);
102 			res = rv;
103 		}
104 	} else if (command[0] == '-') {
105 		struct net_device *bond_dev;
106 
107 		rtnl_lock();
108 		bond_dev = bond_get_by_name(bn, ifname);
109 		if (bond_dev) {
110 			pr_info("%s is being deleted...\n", ifname);
111 			unregister_netdevice(bond_dev);
112 		} else {
113 			pr_err("unable to delete non-existent %s\n", ifname);
114 			res = -ENODEV;
115 		}
116 		rtnl_unlock();
117 	} else
118 		goto err_no_cmd;
119 
120 	/* Always return either count or an error.  If you return 0, you'll
121 	 * get called forever, which is bad.
122 	 */
123 	return res;
124 
125 err_no_cmd:
126 	pr_err("no command found in bonding_masters - use +ifname or -ifname\n");
127 	return -EPERM;
128 }
129 
130 /* class attribute for bond_masters file.  This ends up in /sys/class/net */
131 static const struct class_attribute class_attr_bonding_masters = {
132 	.attr = {
133 		.name = "bonding_masters",
134 		.mode = 0644,
135 	},
136 	.show = bonding_show_bonds,
137 	.store = bonding_store_bonds,
138 };
139 
140 /* Generic "store" method for bonding sysfs option setting */
141 static ssize_t bonding_sysfs_store_option(struct device *d,
142 					  struct device_attribute *attr,
143 					  const char *buffer, size_t count)
144 {
145 	struct bonding *bond = to_bond(d);
146 	const struct bond_option *opt;
147 	char *buffer_clone;
148 	int ret;
149 
150 	opt = bond_opt_get_by_name(attr->attr.name);
151 	if (WARN_ON(!opt))
152 		return -ENOENT;
153 	buffer_clone = kstrndup(buffer, count, GFP_KERNEL);
154 	if (!buffer_clone)
155 		return -ENOMEM;
156 	ret = bond_opt_tryset_rtnl(bond, opt->id, buffer_clone);
157 	if (!ret)
158 		ret = count;
159 	kfree(buffer_clone);
160 
161 	return ret;
162 }
163 
164 /* Show the slaves in the current bond. */
165 static ssize_t bonding_show_slaves(struct device *d,
166 				   struct device_attribute *attr, char *buf)
167 {
168 	struct bonding *bond = to_bond(d);
169 	struct list_head *iter;
170 	struct slave *slave;
171 	int res = 0;
172 
173 	if (!rtnl_trylock())
174 		return restart_syscall();
175 
176 	bond_for_each_slave(bond, slave, iter) {
177 		if (res > (PAGE_SIZE - IFNAMSIZ)) {
178 			/* not enough space for another interface name */
179 			if ((PAGE_SIZE - res) > 10)
180 				res = PAGE_SIZE - 10;
181 			res += sprintf(buf + res, "++more++ ");
182 			break;
183 		}
184 		res += sprintf(buf + res, "%s ", slave->dev->name);
185 	}
186 
187 	rtnl_unlock();
188 
189 	if (res)
190 		buf[res-1] = '\n'; /* eat the leftover space */
191 
192 	return res;
193 }
194 static DEVICE_ATTR(slaves, 0644, bonding_show_slaves,
195 		   bonding_sysfs_store_option);
196 
197 /* Show the bonding mode. */
198 static ssize_t bonding_show_mode(struct device *d,
199 				 struct device_attribute *attr, char *buf)
200 {
201 	struct bonding *bond = to_bond(d);
202 	const struct bond_opt_value *val;
203 
204 	val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
205 
206 	return sprintf(buf, "%s %d\n", val->string, BOND_MODE(bond));
207 }
208 static DEVICE_ATTR(mode, 0644, bonding_show_mode, bonding_sysfs_store_option);
209 
210 /* Show the bonding transmit hash method. */
211 static ssize_t bonding_show_xmit_hash(struct device *d,
212 				      struct device_attribute *attr,
213 				      char *buf)
214 {
215 	struct bonding *bond = to_bond(d);
216 	const struct bond_opt_value *val;
217 
218 	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, bond->params.xmit_policy);
219 
220 	return sprintf(buf, "%s %d\n", val->string, bond->params.xmit_policy);
221 }
222 static DEVICE_ATTR(xmit_hash_policy, 0644,
223 		   bonding_show_xmit_hash, bonding_sysfs_store_option);
224 
225 /* Show arp_validate. */
226 static ssize_t bonding_show_arp_validate(struct device *d,
227 					 struct device_attribute *attr,
228 					 char *buf)
229 {
230 	struct bonding *bond = to_bond(d);
231 	const struct bond_opt_value *val;
232 
233 	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
234 			       bond->params.arp_validate);
235 
236 	return sprintf(buf, "%s %d\n", val->string, bond->params.arp_validate);
237 }
238 static DEVICE_ATTR(arp_validate, 0644, bonding_show_arp_validate,
239 		   bonding_sysfs_store_option);
240 
241 /* Show arp_all_targets. */
242 static ssize_t bonding_show_arp_all_targets(struct device *d,
243 					 struct device_attribute *attr,
244 					 char *buf)
245 {
246 	struct bonding *bond = to_bond(d);
247 	const struct bond_opt_value *val;
248 
249 	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS,
250 			       bond->params.arp_all_targets);
251 	return sprintf(buf, "%s %d\n",
252 		       val->string, bond->params.arp_all_targets);
253 }
254 static DEVICE_ATTR(arp_all_targets, 0644,
255 		   bonding_show_arp_all_targets, bonding_sysfs_store_option);
256 
257 /* Show fail_over_mac. */
258 static ssize_t bonding_show_fail_over_mac(struct device *d,
259 					  struct device_attribute *attr,
260 					  char *buf)
261 {
262 	struct bonding *bond = to_bond(d);
263 	const struct bond_opt_value *val;
264 
265 	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC,
266 			       bond->params.fail_over_mac);
267 
268 	return sprintf(buf, "%s %d\n", val->string, bond->params.fail_over_mac);
269 }
270 static DEVICE_ATTR(fail_over_mac, 0644,
271 		   bonding_show_fail_over_mac, bonding_sysfs_store_option);
272 
273 /* Show the arp timer interval. */
274 static ssize_t bonding_show_arp_interval(struct device *d,
275 					 struct device_attribute *attr,
276 					 char *buf)
277 {
278 	struct bonding *bond = to_bond(d);
279 
280 	return sprintf(buf, "%d\n", bond->params.arp_interval);
281 }
282 static DEVICE_ATTR(arp_interval, 0644,
283 		   bonding_show_arp_interval, bonding_sysfs_store_option);
284 
285 /* Show the arp targets. */
286 static ssize_t bonding_show_arp_targets(struct device *d,
287 					struct device_attribute *attr,
288 					char *buf)
289 {
290 	struct bonding *bond = to_bond(d);
291 	int i, res = 0;
292 
293 	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
294 		if (bond->params.arp_targets[i])
295 			res += sprintf(buf + res, "%pI4 ",
296 				       &bond->params.arp_targets[i]);
297 	}
298 	if (res)
299 		buf[res-1] = '\n'; /* eat the leftover space */
300 
301 	return res;
302 }
303 static DEVICE_ATTR(arp_ip_target, 0644,
304 		   bonding_show_arp_targets, bonding_sysfs_store_option);
305 
306 /* Show the up and down delays. */
307 static ssize_t bonding_show_downdelay(struct device *d,
308 				      struct device_attribute *attr,
309 				      char *buf)
310 {
311 	struct bonding *bond = to_bond(d);
312 
313 	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon);
314 }
315 static DEVICE_ATTR(downdelay, 0644,
316 		   bonding_show_downdelay, bonding_sysfs_store_option);
317 
318 static ssize_t bonding_show_updelay(struct device *d,
319 				    struct device_attribute *attr,
320 				    char *buf)
321 {
322 	struct bonding *bond = to_bond(d);
323 
324 	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon);
325 
326 }
327 static DEVICE_ATTR(updelay, 0644,
328 		   bonding_show_updelay, bonding_sysfs_store_option);
329 
330 /* Show the LACP interval. */
331 static ssize_t bonding_show_lacp(struct device *d,
332 				 struct device_attribute *attr,
333 				 char *buf)
334 {
335 	struct bonding *bond = to_bond(d);
336 	const struct bond_opt_value *val;
337 
338 	val = bond_opt_get_val(BOND_OPT_LACP_RATE, bond->params.lacp_fast);
339 
340 	return sprintf(buf, "%s %d\n", val->string, bond->params.lacp_fast);
341 }
342 static DEVICE_ATTR(lacp_rate, 0644,
343 		   bonding_show_lacp, bonding_sysfs_store_option);
344 
345 static ssize_t bonding_show_min_links(struct device *d,
346 				      struct device_attribute *attr,
347 				      char *buf)
348 {
349 	struct bonding *bond = to_bond(d);
350 
351 	return sprintf(buf, "%u\n", bond->params.min_links);
352 }
353 static DEVICE_ATTR(min_links, 0644,
354 		   bonding_show_min_links, bonding_sysfs_store_option);
355 
356 static ssize_t bonding_show_ad_select(struct device *d,
357 				      struct device_attribute *attr,
358 				      char *buf)
359 {
360 	struct bonding *bond = to_bond(d);
361 	const struct bond_opt_value *val;
362 
363 	val = bond_opt_get_val(BOND_OPT_AD_SELECT, bond->params.ad_select);
364 
365 	return sprintf(buf, "%s %d\n", val->string, bond->params.ad_select);
366 }
367 static DEVICE_ATTR(ad_select, 0644,
368 		   bonding_show_ad_select, bonding_sysfs_store_option);
369 
370 /* Show the number of peer notifications to send after a failover event. */
371 static ssize_t bonding_show_num_peer_notif(struct device *d,
372 					   struct device_attribute *attr,
373 					   char *buf)
374 {
375 	struct bonding *bond = to_bond(d);
376 	return sprintf(buf, "%d\n", bond->params.num_peer_notif);
377 }
378 static DEVICE_ATTR(num_grat_arp, 0644,
379 		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
380 static DEVICE_ATTR(num_unsol_na, 0644,
381 		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
382 
383 /* Show the MII monitor interval. */
384 static ssize_t bonding_show_miimon(struct device *d,
385 				   struct device_attribute *attr,
386 				   char *buf)
387 {
388 	struct bonding *bond = to_bond(d);
389 
390 	return sprintf(buf, "%d\n", bond->params.miimon);
391 }
392 static DEVICE_ATTR(miimon, 0644,
393 		   bonding_show_miimon, bonding_sysfs_store_option);
394 
395 /* Show the primary slave. */
396 static ssize_t bonding_show_primary(struct device *d,
397 				    struct device_attribute *attr,
398 				    char *buf)
399 {
400 	struct bonding *bond = to_bond(d);
401 	struct slave *primary;
402 	int count = 0;
403 
404 	rcu_read_lock();
405 	primary = rcu_dereference(bond->primary_slave);
406 	if (primary)
407 		count = sprintf(buf, "%s\n", primary->dev->name);
408 	rcu_read_unlock();
409 
410 	return count;
411 }
412 static DEVICE_ATTR(primary, 0644,
413 		   bonding_show_primary, bonding_sysfs_store_option);
414 
415 /* Show the primary_reselect flag. */
416 static ssize_t bonding_show_primary_reselect(struct device *d,
417 					     struct device_attribute *attr,
418 					     char *buf)
419 {
420 	struct bonding *bond = to_bond(d);
421 	const struct bond_opt_value *val;
422 
423 	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT,
424 			       bond->params.primary_reselect);
425 
426 	return sprintf(buf, "%s %d\n",
427 		       val->string, bond->params.primary_reselect);
428 }
429 static DEVICE_ATTR(primary_reselect, 0644,
430 		   bonding_show_primary_reselect, bonding_sysfs_store_option);
431 
432 /* Show the use_carrier flag. */
433 static ssize_t bonding_show_carrier(struct device *d,
434 				    struct device_attribute *attr,
435 				    char *buf)
436 {
437 	struct bonding *bond = to_bond(d);
438 
439 	return sprintf(buf, "%d\n", bond->params.use_carrier);
440 }
441 static DEVICE_ATTR(use_carrier, 0644,
442 		   bonding_show_carrier, bonding_sysfs_store_option);
443 
444 
445 /* Show currently active_slave. */
446 static ssize_t bonding_show_active_slave(struct device *d,
447 					 struct device_attribute *attr,
448 					 char *buf)
449 {
450 	struct bonding *bond = to_bond(d);
451 	struct net_device *slave_dev;
452 	int count = 0;
453 
454 	rcu_read_lock();
455 	slave_dev = bond_option_active_slave_get_rcu(bond);
456 	if (slave_dev)
457 		count = sprintf(buf, "%s\n", slave_dev->name);
458 	rcu_read_unlock();
459 
460 	return count;
461 }
462 static DEVICE_ATTR(active_slave, 0644,
463 		   bonding_show_active_slave, bonding_sysfs_store_option);
464 
465 /* Show link status of the bond interface. */
466 static ssize_t bonding_show_mii_status(struct device *d,
467 				       struct device_attribute *attr,
468 				       char *buf)
469 {
470 	struct bonding *bond = to_bond(d);
471 	bool active = netif_carrier_ok(bond->dev);
472 
473 	return sprintf(buf, "%s\n", active ? "up" : "down");
474 }
475 static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
476 
477 /* Show current 802.3ad aggregator ID. */
478 static ssize_t bonding_show_ad_aggregator(struct device *d,
479 					  struct device_attribute *attr,
480 					  char *buf)
481 {
482 	int count = 0;
483 	struct bonding *bond = to_bond(d);
484 
485 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
486 		struct ad_info ad_info;
487 		count = sprintf(buf, "%d\n",
488 				bond_3ad_get_active_agg_info(bond, &ad_info)
489 				?  0 : ad_info.aggregator_id);
490 	}
491 
492 	return count;
493 }
494 static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
495 
496 
497 /* Show number of active 802.3ad ports. */
498 static ssize_t bonding_show_ad_num_ports(struct device *d,
499 					 struct device_attribute *attr,
500 					 char *buf)
501 {
502 	int count = 0;
503 	struct bonding *bond = to_bond(d);
504 
505 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
506 		struct ad_info ad_info;
507 		count = sprintf(buf, "%d\n",
508 				bond_3ad_get_active_agg_info(bond, &ad_info)
509 				?  0 : ad_info.ports);
510 	}
511 
512 	return count;
513 }
514 static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
515 
516 
517 /* Show current 802.3ad actor key. */
518 static ssize_t bonding_show_ad_actor_key(struct device *d,
519 					 struct device_attribute *attr,
520 					 char *buf)
521 {
522 	int count = 0;
523 	struct bonding *bond = to_bond(d);
524 
525 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
526 		struct ad_info ad_info;
527 		count = sprintf(buf, "%d\n",
528 				bond_3ad_get_active_agg_info(bond, &ad_info)
529 				?  0 : ad_info.actor_key);
530 	}
531 
532 	return count;
533 }
534 static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
535 
536 
537 /* Show current 802.3ad partner key. */
538 static ssize_t bonding_show_ad_partner_key(struct device *d,
539 					   struct device_attribute *attr,
540 					   char *buf)
541 {
542 	int count = 0;
543 	struct bonding *bond = to_bond(d);
544 
545 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
546 		struct ad_info ad_info;
547 		count = sprintf(buf, "%d\n",
548 				bond_3ad_get_active_agg_info(bond, &ad_info)
549 				?  0 : ad_info.partner_key);
550 	}
551 
552 	return count;
553 }
554 static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
555 
556 
557 /* Show current 802.3ad partner mac. */
558 static ssize_t bonding_show_ad_partner_mac(struct device *d,
559 					   struct device_attribute *attr,
560 					   char *buf)
561 {
562 	int count = 0;
563 	struct bonding *bond = to_bond(d);
564 
565 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
566 		struct ad_info ad_info;
567 		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
568 			count = sprintf(buf, "%pM\n", ad_info.partner_system);
569 	}
570 
571 	return count;
572 }
573 static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
574 
575 /* Show the queue_ids of the slaves in the current bond. */
576 static ssize_t bonding_show_queue_id(struct device *d,
577 				     struct device_attribute *attr,
578 				     char *buf)
579 {
580 	struct bonding *bond = to_bond(d);
581 	struct list_head *iter;
582 	struct slave *slave;
583 	int res = 0;
584 
585 	if (!rtnl_trylock())
586 		return restart_syscall();
587 
588 	bond_for_each_slave(bond, slave, iter) {
589 		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
590 			/* not enough space for another interface_name:queue_id pair */
591 			if ((PAGE_SIZE - res) > 10)
592 				res = PAGE_SIZE - 10;
593 			res += sprintf(buf + res, "++more++ ");
594 			break;
595 		}
596 		res += sprintf(buf + res, "%s:%d ",
597 			       slave->dev->name, slave->queue_id);
598 	}
599 	if (res)
600 		buf[res-1] = '\n'; /* eat the leftover space */
601 
602 	rtnl_unlock();
603 
604 	return res;
605 }
606 static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
607 		   bonding_sysfs_store_option);
608 
609 
610 /* Show the all_slaves_active flag. */
611 static ssize_t bonding_show_slaves_active(struct device *d,
612 					  struct device_attribute *attr,
613 					  char *buf)
614 {
615 	struct bonding *bond = to_bond(d);
616 
617 	return sprintf(buf, "%d\n", bond->params.all_slaves_active);
618 }
619 static DEVICE_ATTR(all_slaves_active, 0644,
620 		   bonding_show_slaves_active, bonding_sysfs_store_option);
621 
622 /* Show the number of IGMP membership reports to send on link failure */
623 static ssize_t bonding_show_resend_igmp(struct device *d,
624 					struct device_attribute *attr,
625 					char *buf)
626 {
627 	struct bonding *bond = to_bond(d);
628 
629 	return sprintf(buf, "%d\n", bond->params.resend_igmp);
630 }
631 static DEVICE_ATTR(resend_igmp, 0644,
632 		   bonding_show_resend_igmp, bonding_sysfs_store_option);
633 
634 
635 static ssize_t bonding_show_lp_interval(struct device *d,
636 					struct device_attribute *attr,
637 					char *buf)
638 {
639 	struct bonding *bond = to_bond(d);
640 
641 	return sprintf(buf, "%d\n", bond->params.lp_interval);
642 }
643 static DEVICE_ATTR(lp_interval, 0644,
644 		   bonding_show_lp_interval, bonding_sysfs_store_option);
645 
646 static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
647 					   struct device_attribute *attr,
648 					   char *buf)
649 {
650 	struct bonding *bond = to_bond(d);
651 	return sprintf(buf, "%d\n", bond->params.tlb_dynamic_lb);
652 }
653 static DEVICE_ATTR(tlb_dynamic_lb, 0644,
654 		   bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
655 
656 static ssize_t bonding_show_packets_per_slave(struct device *d,
657 					      struct device_attribute *attr,
658 					      char *buf)
659 {
660 	struct bonding *bond = to_bond(d);
661 	unsigned int packets_per_slave = bond->params.packets_per_slave;
662 
663 	return sprintf(buf, "%u\n", packets_per_slave);
664 }
665 static DEVICE_ATTR(packets_per_slave, 0644,
666 		   bonding_show_packets_per_slave, bonding_sysfs_store_option);
667 
668 static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
669 					      struct device_attribute *attr,
670 					      char *buf)
671 {
672 	struct bonding *bond = to_bond(d);
673 
674 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
675 		return sprintf(buf, "%hu\n", bond->params.ad_actor_sys_prio);
676 
677 	return 0;
678 }
679 static DEVICE_ATTR(ad_actor_sys_prio, 0644,
680 		   bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
681 
682 static ssize_t bonding_show_ad_actor_system(struct device *d,
683 					    struct device_attribute *attr,
684 					    char *buf)
685 {
686 	struct bonding *bond = to_bond(d);
687 
688 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
689 		return sprintf(buf, "%pM\n", bond->params.ad_actor_system);
690 
691 	return 0;
692 }
693 
694 static DEVICE_ATTR(ad_actor_system, 0644,
695 		   bonding_show_ad_actor_system, bonding_sysfs_store_option);
696 
697 static ssize_t bonding_show_ad_user_port_key(struct device *d,
698 					     struct device_attribute *attr,
699 					     char *buf)
700 {
701 	struct bonding *bond = to_bond(d);
702 
703 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
704 		return sprintf(buf, "%hu\n", bond->params.ad_user_port_key);
705 
706 	return 0;
707 }
708 static DEVICE_ATTR(ad_user_port_key, 0644,
709 		   bonding_show_ad_user_port_key, bonding_sysfs_store_option);
710 
711 static struct attribute *per_bond_attrs[] = {
712 	&dev_attr_slaves.attr,
713 	&dev_attr_mode.attr,
714 	&dev_attr_fail_over_mac.attr,
715 	&dev_attr_arp_validate.attr,
716 	&dev_attr_arp_all_targets.attr,
717 	&dev_attr_arp_interval.attr,
718 	&dev_attr_arp_ip_target.attr,
719 	&dev_attr_downdelay.attr,
720 	&dev_attr_updelay.attr,
721 	&dev_attr_lacp_rate.attr,
722 	&dev_attr_ad_select.attr,
723 	&dev_attr_xmit_hash_policy.attr,
724 	&dev_attr_num_grat_arp.attr,
725 	&dev_attr_num_unsol_na.attr,
726 	&dev_attr_miimon.attr,
727 	&dev_attr_primary.attr,
728 	&dev_attr_primary_reselect.attr,
729 	&dev_attr_use_carrier.attr,
730 	&dev_attr_active_slave.attr,
731 	&dev_attr_mii_status.attr,
732 	&dev_attr_ad_aggregator.attr,
733 	&dev_attr_ad_num_ports.attr,
734 	&dev_attr_ad_actor_key.attr,
735 	&dev_attr_ad_partner_key.attr,
736 	&dev_attr_ad_partner_mac.attr,
737 	&dev_attr_queue_id.attr,
738 	&dev_attr_all_slaves_active.attr,
739 	&dev_attr_resend_igmp.attr,
740 	&dev_attr_min_links.attr,
741 	&dev_attr_lp_interval.attr,
742 	&dev_attr_packets_per_slave.attr,
743 	&dev_attr_tlb_dynamic_lb.attr,
744 	&dev_attr_ad_actor_sys_prio.attr,
745 	&dev_attr_ad_actor_system.attr,
746 	&dev_attr_ad_user_port_key.attr,
747 	NULL,
748 };
749 
750 static const struct attribute_group bonding_group = {
751 	.name = "bonding",
752 	.attrs = per_bond_attrs,
753 };
754 
755 /* Initialize sysfs.  This sets up the bonding_masters file in
756  * /sys/class/net.
757  */
758 int bond_create_sysfs(struct bond_net *bn)
759 {
760 	int ret;
761 
762 	bn->class_attr_bonding_masters = class_attr_bonding_masters;
763 	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
764 
765 	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
766 					  bn->net);
767 	/* Permit multiple loads of the module by ignoring failures to
768 	 * create the bonding_masters sysfs file.  Bonding devices
769 	 * created by second or subsequent loads of the module will
770 	 * not be listed in, or controllable by, bonding_masters, but
771 	 * will have the usual "bonding" sysfs directory.
772 	 *
773 	 * This is done to preserve backwards compatibility for
774 	 * initscripts/sysconfig, which load bonding multiple times to
775 	 * configure multiple bonding devices.
776 	 */
777 	if (ret == -EEXIST) {
778 		/* Is someone being kinky and naming a device bonding_master? */
779 		if (__dev_get_by_name(bn->net,
780 				      class_attr_bonding_masters.attr.name))
781 			pr_err("network device named %s already exists in sysfs\n",
782 			       class_attr_bonding_masters.attr.name);
783 		ret = 0;
784 	}
785 
786 	return ret;
787 
788 }
789 
790 /* Remove /sys/class/net/bonding_masters. */
791 void bond_destroy_sysfs(struct bond_net *bn)
792 {
793 	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, bn->net);
794 }
795 
796 /* Initialize sysfs for each bond.  This sets up and registers
797  * the 'bondctl' directory for each individual bond under /sys/class/net.
798  */
799 void bond_prepare_sysfs_group(struct bonding *bond)
800 {
801 	bond->dev->sysfs_groups[0] = &bonding_group;
802 }
803 
804