xref: /linux/drivers/net/bonding/bond_sysfs.c (revision 6443f4f20bdae726fe01cf5946fba9742a0ffda6)
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(const struct class *cls,
35 				  const struct class_attribute *attr,
36 				  char *buf)
37 {
38 	const struct bond_net *bn =
39 		container_of_const(attr, struct bond_net, class_attr_bonding_masters);
40 	struct bonding *bond;
41 	int res = 0;
42 
43 	rcu_read_lock();
44 
45 	list_for_each_entry_rcu(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 += sysfs_emit_at(buf, res, "++more++ ");
51 			break;
52 		}
53 		res += sysfs_emit_at(buf, res, "%s ", bond->dev->name);
54 	}
55 	if (res)
56 		buf[res-1] = '\n'; /* eat the leftover space */
57 
58 	rcu_read_unlock();
59 	return res;
60 }
61 
62 static struct net_device *bond_get_by_name(const 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(const struct class *cls,
79 				   const struct class_attribute *attr,
80 				   const char *buffer, size_t count)
81 {
82 	const struct bond_net *bn =
83 		container_of_const(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 	rcu_read_lock();
174 
175 	bond_for_each_slave_rcu(bond, slave, iter) {
176 		if (res > (PAGE_SIZE - IFNAMSIZ)) {
177 			/* not enough space for another interface name */
178 			if ((PAGE_SIZE - res) > 10)
179 				res = PAGE_SIZE - 10;
180 			res += sysfs_emit_at(buf, res, "++more++ ");
181 			break;
182 		}
183 		res += sysfs_emit_at(buf, res, "%s ", slave->dev->name);
184 	}
185 
186 	rcu_read_unlock();
187 
188 	if (res)
189 		buf[res-1] = '\n'; /* eat the leftover space */
190 
191 	return res;
192 }
193 static DEVICE_ATTR(slaves, 0644, bonding_show_slaves,
194 		   bonding_sysfs_store_option);
195 
196 /* Show the bonding mode. */
197 static ssize_t bonding_show_mode(struct device *d,
198 				 struct device_attribute *attr, char *buf)
199 {
200 	struct bonding *bond = to_bond(d);
201 	const struct bond_opt_value *val;
202 
203 	val = bond_opt_get_val(BOND_OPT_MODE, BOND_MODE(bond));
204 
205 	return sysfs_emit(buf, "%s %d\n", val->string, BOND_MODE(bond));
206 }
207 static DEVICE_ATTR(mode, 0644, bonding_show_mode, bonding_sysfs_store_option);
208 
209 /* Show the bonding transmit hash method. */
210 static ssize_t bonding_show_xmit_hash(struct device *d,
211 				      struct device_attribute *attr,
212 				      char *buf)
213 {
214 	struct bonding *bond = to_bond(d);
215 	const struct bond_opt_value *val;
216 	int xmit_policy;
217 
218 	xmit_policy = READ_ONCE(bond->params.xmit_policy);
219 	val = bond_opt_get_val(BOND_OPT_XMIT_HASH, xmit_policy);
220 
221 	return sysfs_emit(buf, "%s %d\n", val->string, xmit_policy);
222 }
223 static DEVICE_ATTR(xmit_hash_policy, 0644,
224 		   bonding_show_xmit_hash, bonding_sysfs_store_option);
225 
226 /* Show arp_validate. */
227 static ssize_t bonding_show_arp_validate(struct device *d,
228 					 struct device_attribute *attr,
229 					 char *buf)
230 {
231 	struct bonding *bond = to_bond(d);
232 	const struct bond_opt_value *val;
233 	int arp_validate;
234 
235 	arp_validate = READ_ONCE(bond->params.arp_validate);
236 	val = bond_opt_get_val(BOND_OPT_ARP_VALIDATE, arp_validate);
237 
238 	return sysfs_emit(buf, "%s %d\n", val->string, arp_validate);
239 }
240 static DEVICE_ATTR(arp_validate, 0644, bonding_show_arp_validate,
241 		   bonding_sysfs_store_option);
242 
243 /* Show arp_all_targets. */
244 static ssize_t bonding_show_arp_all_targets(struct device *d,
245 					 struct device_attribute *attr,
246 					 char *buf)
247 {
248 	struct bonding *bond = to_bond(d);
249 	const struct bond_opt_value *val;
250 	int arp_all_targets;
251 
252 	arp_all_targets = READ_ONCE(bond->params.arp_all_targets);
253 	val = bond_opt_get_val(BOND_OPT_ARP_ALL_TARGETS, arp_all_targets);
254 	return sysfs_emit(buf, "%s %d\n", val->string, arp_all_targets);
255 }
256 static DEVICE_ATTR(arp_all_targets, 0644,
257 		   bonding_show_arp_all_targets, bonding_sysfs_store_option);
258 
259 /* Show fail_over_mac. */
260 static ssize_t bonding_show_fail_over_mac(struct device *d,
261 					  struct device_attribute *attr,
262 					  char *buf)
263 {
264 	struct bonding *bond = to_bond(d);
265 	const struct bond_opt_value *val;
266 	int fail_over_mac;
267 
268 	fail_over_mac = READ_ONCE(bond->params.fail_over_mac);
269 	val = bond_opt_get_val(BOND_OPT_FAIL_OVER_MAC, fail_over_mac);
270 
271 	return sysfs_emit(buf, "%s %d\n", val->string, fail_over_mac);
272 }
273 static DEVICE_ATTR(fail_over_mac, 0644,
274 		   bonding_show_fail_over_mac, bonding_sysfs_store_option);
275 
276 /* Show the arp timer interval. */
277 static ssize_t bonding_show_arp_interval(struct device *d,
278 					 struct device_attribute *attr,
279 					 char *buf)
280 {
281 	struct bonding *bond = to_bond(d);
282 
283 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.arp_interval));
284 }
285 static DEVICE_ATTR(arp_interval, 0644,
286 		   bonding_show_arp_interval, bonding_sysfs_store_option);
287 
288 /* Show the arp targets. */
289 static ssize_t bonding_show_arp_targets(struct device *d,
290 					struct device_attribute *attr,
291 					char *buf)
292 {
293 	struct bonding *bond = to_bond(d);
294 	int i, res = 0;
295 
296 	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
297 		__be32 t = READ_ONCE(bond->params.arp_targets[i]);
298 
299 		if (t)
300 			res += sysfs_emit_at(buf, res, "%pI4 ", &t);
301 	}
302 	if (res)
303 		buf[res-1] = '\n'; /* eat the leftover space */
304 
305 	return res;
306 }
307 static DEVICE_ATTR(arp_ip_target, 0644,
308 		   bonding_show_arp_targets, bonding_sysfs_store_option);
309 
310 /* Show the arp missed max. */
311 static ssize_t bonding_show_missed_max(struct device *d,
312 				       struct device_attribute *attr,
313 				       char *buf)
314 {
315 	struct bonding *bond = to_bond(d);
316 
317 	return sysfs_emit(buf, "%u\n", READ_ONCE(bond->params.missed_max));
318 }
319 static DEVICE_ATTR(arp_missed_max, 0644,
320 		   bonding_show_missed_max, bonding_sysfs_store_option);
321 
322 /* Show the up and down delays. */
323 static ssize_t bonding_show_downdelay(struct device *d,
324 				      struct device_attribute *attr,
325 				      char *buf)
326 {
327 	struct bonding *bond = to_bond(d);
328 
329 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.downdelay) *
330 				       READ_ONCE(bond->params.miimon));
331 }
332 static DEVICE_ATTR(downdelay, 0644,
333 		   bonding_show_downdelay, bonding_sysfs_store_option);
334 
335 static ssize_t bonding_show_updelay(struct device *d,
336 				    struct device_attribute *attr,
337 				    char *buf)
338 {
339 	struct bonding *bond = to_bond(d);
340 
341 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.updelay) *
342 				       READ_ONCE(bond->params.miimon));
343 
344 }
345 static DEVICE_ATTR(updelay, 0644,
346 		   bonding_show_updelay, bonding_sysfs_store_option);
347 
348 static ssize_t bonding_show_peer_notif_delay(struct device *d,
349 					     struct device_attribute *attr,
350 					     char *buf)
351 {
352 	struct bonding *bond = to_bond(d);
353 
354 	return sysfs_emit(buf, "%d\n",
355 			  READ_ONCE(bond->params.peer_notif_delay) *
356 			  READ_ONCE(bond->params.miimon));
357 }
358 static DEVICE_ATTR(peer_notif_delay, 0644,
359 		   bonding_show_peer_notif_delay, bonding_sysfs_store_option);
360 
361 /* Show the LACP activity and interval. */
362 static ssize_t bonding_show_lacp_active(struct device *d,
363 					struct device_attribute *attr,
364 					char *buf)
365 {
366 	struct bonding *bond = to_bond(d);
367 	const struct bond_opt_value *val;
368 	int lacp_active;
369 
370 	lacp_active = READ_ONCE(bond->params.lacp_active);
371 	val = bond_opt_get_val(BOND_OPT_LACP_ACTIVE, lacp_active);
372 
373 	return sysfs_emit(buf, "%s %d\n", val->string, lacp_active);
374 }
375 static DEVICE_ATTR(lacp_active, 0644,
376 		   bonding_show_lacp_active, bonding_sysfs_store_option);
377 
378 static ssize_t bonding_show_lacp_rate(struct device *d,
379 				      struct device_attribute *attr,
380 				      char *buf)
381 {
382 	struct bonding *bond = to_bond(d);
383 	const struct bond_opt_value *val;
384 	int lacp_fast;
385 
386 	lacp_fast = READ_ONCE(bond->params.lacp_fast);
387 	val = bond_opt_get_val(BOND_OPT_LACP_RATE, lacp_fast);
388 
389 	return sysfs_emit(buf, "%s %d\n", val->string, lacp_fast);
390 }
391 static DEVICE_ATTR(lacp_rate, 0644,
392 		   bonding_show_lacp_rate, bonding_sysfs_store_option);
393 
394 static ssize_t bonding_show_min_links(struct device *d,
395 				      struct device_attribute *attr,
396 				      char *buf)
397 {
398 	struct bonding *bond = to_bond(d);
399 
400 	return sysfs_emit(buf, "%u\n", READ_ONCE(bond->params.min_links));
401 }
402 static DEVICE_ATTR(min_links, 0644,
403 		   bonding_show_min_links, bonding_sysfs_store_option);
404 
405 static ssize_t bonding_show_ad_select(struct device *d,
406 				      struct device_attribute *attr,
407 				      char *buf)
408 {
409 	struct bonding *bond = to_bond(d);
410 	const struct bond_opt_value *val;
411 	int ad_select;
412 
413 	ad_select = READ_ONCE(bond->params.ad_select);
414 	val = bond_opt_get_val(BOND_OPT_AD_SELECT, ad_select);
415 
416 	return sysfs_emit(buf, "%s %d\n", val->string, ad_select);
417 }
418 static DEVICE_ATTR(ad_select, 0644,
419 		   bonding_show_ad_select, bonding_sysfs_store_option);
420 
421 /* Show the number of peer notifications to send after a failover event. */
422 static ssize_t bonding_show_num_peer_notif(struct device *d,
423 					   struct device_attribute *attr,
424 					   char *buf)
425 {
426 	struct bonding *bond = to_bond(d);
427 
428 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.num_peer_notif));
429 }
430 static DEVICE_ATTR(num_grat_arp, 0644,
431 		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
432 static DEVICE_ATTR(num_unsol_na, 0644,
433 		   bonding_show_num_peer_notif, bonding_sysfs_store_option);
434 
435 /* Show the MII monitor interval. */
436 static ssize_t bonding_show_miimon(struct device *d,
437 				   struct device_attribute *attr,
438 				   char *buf)
439 {
440 	struct bonding *bond = to_bond(d);
441 
442 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.miimon));
443 }
444 static DEVICE_ATTR(miimon, 0644,
445 		   bonding_show_miimon, bonding_sysfs_store_option);
446 
447 /* Show the primary slave. */
448 static ssize_t bonding_show_primary(struct device *d,
449 				    struct device_attribute *attr,
450 				    char *buf)
451 {
452 	struct bonding *bond = to_bond(d);
453 	struct slave *primary;
454 	int count = 0;
455 
456 	rcu_read_lock();
457 	primary = rcu_dereference(bond->primary_slave);
458 	if (primary)
459 		count = sysfs_emit(buf, "%s\n", primary->dev->name);
460 	rcu_read_unlock();
461 
462 	return count;
463 }
464 static DEVICE_ATTR(primary, 0644,
465 		   bonding_show_primary, bonding_sysfs_store_option);
466 
467 /* Show the primary_reselect flag. */
468 static ssize_t bonding_show_primary_reselect(struct device *d,
469 					     struct device_attribute *attr,
470 					     char *buf)
471 {
472 	const struct bonding *bond = to_bond(d);
473 	const struct bond_opt_value *val;
474 	int primary_reselect;
475 
476 	primary_reselect = READ_ONCE(bond->params.primary_reselect);
477 
478 	val = bond_opt_get_val(BOND_OPT_PRIMARY_RESELECT, primary_reselect);
479 
480 	return sysfs_emit(buf, "%s %d\n", val->string, primary_reselect);
481 }
482 static DEVICE_ATTR(primary_reselect, 0644,
483 		   bonding_show_primary_reselect, bonding_sysfs_store_option);
484 
485 /* use_carrier is obsolete, but print value for compatibility */
486 static ssize_t bonding_show_carrier(struct device *d,
487 				    struct device_attribute *attr,
488 				    char *buf)
489 {
490 	return sysfs_emit(buf, "1\n");
491 }
492 static DEVICE_ATTR(use_carrier, 0644,
493 		   bonding_show_carrier, bonding_sysfs_store_option);
494 
495 
496 /* Show currently active_slave. */
497 static ssize_t bonding_show_active_slave(struct device *d,
498 					 struct device_attribute *attr,
499 					 char *buf)
500 {
501 	struct bonding *bond = to_bond(d);
502 	struct net_device *slave_dev;
503 	int count = 0;
504 
505 	rcu_read_lock();
506 	slave_dev = bond_option_active_slave_get_rcu(bond);
507 	if (slave_dev)
508 		count = sysfs_emit(buf, "%s\n", slave_dev->name);
509 	rcu_read_unlock();
510 
511 	return count;
512 }
513 static DEVICE_ATTR(active_slave, 0644,
514 		   bonding_show_active_slave, bonding_sysfs_store_option);
515 
516 /* Show link status of the bond interface. */
517 static ssize_t bonding_show_mii_status(struct device *d,
518 				       struct device_attribute *attr,
519 				       char *buf)
520 {
521 	struct bonding *bond = to_bond(d);
522 	bool active = netif_carrier_ok(bond->dev);
523 
524 	return sysfs_emit(buf, "%s\n", active ? "up" : "down");
525 }
526 static DEVICE_ATTR(mii_status, 0444, bonding_show_mii_status, NULL);
527 
528 /* Show current 802.3ad aggregator ID. */
529 static ssize_t bonding_show_ad_aggregator(struct device *d,
530 					  struct device_attribute *attr,
531 					  char *buf)
532 {
533 	int count = 0;
534 	struct bonding *bond = to_bond(d);
535 
536 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
537 		struct ad_info ad_info;
538 
539 		count = sysfs_emit(buf, "%d\n",
540 				   bond_3ad_get_active_agg_info(bond, &ad_info)
541 				   ?  0 : ad_info.aggregator_id);
542 	}
543 
544 	return count;
545 }
546 static DEVICE_ATTR(ad_aggregator, 0444, bonding_show_ad_aggregator, NULL);
547 
548 
549 /* Show number of active 802.3ad ports. */
550 static ssize_t bonding_show_ad_num_ports(struct device *d,
551 					 struct device_attribute *attr,
552 					 char *buf)
553 {
554 	int count = 0;
555 	struct bonding *bond = to_bond(d);
556 
557 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
558 		struct ad_info ad_info;
559 
560 		count = sysfs_emit(buf, "%d\n",
561 				   bond_3ad_get_active_agg_info(bond, &ad_info)
562 				   ?  0 : ad_info.ports);
563 	}
564 
565 	return count;
566 }
567 static DEVICE_ATTR(ad_num_ports, 0444, bonding_show_ad_num_ports, NULL);
568 
569 
570 /* Show current 802.3ad actor key. */
571 static ssize_t bonding_show_ad_actor_key(struct device *d,
572 					 struct device_attribute *attr,
573 					 char *buf)
574 {
575 	int count = 0;
576 	struct bonding *bond = to_bond(d);
577 
578 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
579 		struct ad_info ad_info;
580 
581 		count = sysfs_emit(buf, "%d\n",
582 				   bond_3ad_get_active_agg_info(bond, &ad_info)
583 				   ?  0 : ad_info.actor_key);
584 	}
585 
586 	return count;
587 }
588 static DEVICE_ATTR(ad_actor_key, 0444, bonding_show_ad_actor_key, NULL);
589 
590 
591 /* Show current 802.3ad partner key. */
592 static ssize_t bonding_show_ad_partner_key(struct device *d,
593 					   struct device_attribute *attr,
594 					   char *buf)
595 {
596 	int count = 0;
597 	struct bonding *bond = to_bond(d);
598 
599 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
600 		struct ad_info ad_info;
601 
602 		count = sysfs_emit(buf, "%d\n",
603 				   bond_3ad_get_active_agg_info(bond, &ad_info)
604 				   ?  0 : ad_info.partner_key);
605 	}
606 
607 	return count;
608 }
609 static DEVICE_ATTR(ad_partner_key, 0444, bonding_show_ad_partner_key, NULL);
610 
611 
612 /* Show current 802.3ad partner mac. */
613 static ssize_t bonding_show_ad_partner_mac(struct device *d,
614 					   struct device_attribute *attr,
615 					   char *buf)
616 {
617 	int count = 0;
618 	struct bonding *bond = to_bond(d);
619 
620 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN)) {
621 		struct ad_info ad_info;
622 
623 		if (!bond_3ad_get_active_agg_info(bond, &ad_info))
624 			count = sysfs_emit(buf, "%pM\n", ad_info.partner_system);
625 	}
626 
627 	return count;
628 }
629 static DEVICE_ATTR(ad_partner_mac, 0444, bonding_show_ad_partner_mac, NULL);
630 
631 /* Show the queue_ids of the slaves in the current bond. */
632 static ssize_t bonding_show_queue_id(struct device *d,
633 				     struct device_attribute *attr,
634 				     char *buf)
635 {
636 	struct bonding *bond = to_bond(d);
637 	struct list_head *iter;
638 	struct slave *slave;
639 	int res = 0;
640 
641 	rcu_read_lock();
642 
643 	bond_for_each_slave_rcu(bond, slave, iter) {
644 		if (res > (PAGE_SIZE - IFNAMSIZ - 6)) {
645 			/* not enough space for another interface_name:queue_id pair */
646 			if ((PAGE_SIZE - res) > 10)
647 				res = PAGE_SIZE - 10;
648 			res += sysfs_emit_at(buf, res, "++more++ ");
649 			break;
650 		}
651 		res += sysfs_emit_at(buf, res, "%s:%d ",
652 				     slave->dev->name,
653 				     READ_ONCE(slave->queue_id));
654 	}
655 	if (res)
656 		buf[res-1] = '\n'; /* eat the leftover space */
657 
658 	rcu_read_unlock();
659 
660 	return res;
661 }
662 static DEVICE_ATTR(queue_id, 0644, bonding_show_queue_id,
663 		   bonding_sysfs_store_option);
664 
665 
666 /* Show the all_slaves_active flag. */
667 static ssize_t bonding_show_slaves_active(struct device *d,
668 					  struct device_attribute *attr,
669 					  char *buf)
670 {
671 	struct bonding *bond = to_bond(d);
672 
673 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.all_slaves_active));
674 }
675 static DEVICE_ATTR(all_slaves_active, 0644,
676 		   bonding_show_slaves_active, bonding_sysfs_store_option);
677 
678 /* Show the number of IGMP membership reports to send on link failure */
679 static ssize_t bonding_show_resend_igmp(struct device *d,
680 					struct device_attribute *attr,
681 					char *buf)
682 {
683 	struct bonding *bond = to_bond(d);
684 
685 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.resend_igmp));
686 }
687 static DEVICE_ATTR(resend_igmp, 0644,
688 		   bonding_show_resend_igmp, bonding_sysfs_store_option);
689 
690 
691 static ssize_t bonding_show_lp_interval(struct device *d,
692 					struct device_attribute *attr,
693 					char *buf)
694 {
695 	struct bonding *bond = to_bond(d);
696 
697 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.lp_interval));
698 }
699 static DEVICE_ATTR(lp_interval, 0644,
700 		   bonding_show_lp_interval, bonding_sysfs_store_option);
701 
702 static ssize_t bonding_show_tlb_dynamic_lb(struct device *d,
703 					   struct device_attribute *attr,
704 					   char *buf)
705 {
706 	struct bonding *bond = to_bond(d);
707 
708 	return sysfs_emit(buf, "%d\n", READ_ONCE(bond->params.tlb_dynamic_lb));
709 }
710 static DEVICE_ATTR(tlb_dynamic_lb, 0644,
711 		   bonding_show_tlb_dynamic_lb, bonding_sysfs_store_option);
712 
713 static ssize_t bonding_show_packets_per_slave(struct device *d,
714 					      struct device_attribute *attr,
715 					      char *buf)
716 {
717 	struct bonding *bond = to_bond(d);
718 	unsigned int packets_per_slave = READ_ONCE(bond->params.packets_per_slave);
719 
720 	return sysfs_emit(buf, "%u\n", packets_per_slave);
721 }
722 static DEVICE_ATTR(packets_per_slave, 0644,
723 		   bonding_show_packets_per_slave, bonding_sysfs_store_option);
724 
725 static ssize_t bonding_show_ad_actor_sys_prio(struct device *d,
726 					      struct device_attribute *attr,
727 					      char *buf)
728 {
729 	struct bonding *bond = to_bond(d);
730 
731 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
732 		return sysfs_emit(buf, "%hu\n", READ_ONCE(bond->params.ad_actor_sys_prio));
733 
734 	return 0;
735 }
736 static DEVICE_ATTR(ad_actor_sys_prio, 0644,
737 		   bonding_show_ad_actor_sys_prio, bonding_sysfs_store_option);
738 
739 static ssize_t bonding_show_ad_actor_system(struct device *d,
740 					    struct device_attribute *attr,
741 					    char *buf)
742 {
743 	struct bonding *bond = to_bond(d);
744 
745 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
746 		return sysfs_emit(buf, "%pM\n", bond->params.ad_actor_system);
747 
748 	return 0;
749 }
750 
751 static DEVICE_ATTR(ad_actor_system, 0644,
752 		   bonding_show_ad_actor_system, bonding_sysfs_store_option);
753 
754 static ssize_t bonding_show_ad_user_port_key(struct device *d,
755 					     struct device_attribute *attr,
756 					     char *buf)
757 {
758 	struct bonding *bond = to_bond(d);
759 
760 	if (BOND_MODE(bond) == BOND_MODE_8023AD && capable(CAP_NET_ADMIN))
761 		return sysfs_emit(buf, "%hu\n",
762 				  READ_ONCE(bond->params.ad_user_port_key));
763 
764 	return 0;
765 }
766 static DEVICE_ATTR(ad_user_port_key, 0644,
767 		   bonding_show_ad_user_port_key, bonding_sysfs_store_option);
768 
769 static struct attribute *per_bond_attrs[] = {
770 	&dev_attr_slaves.attr,
771 	&dev_attr_mode.attr,
772 	&dev_attr_fail_over_mac.attr,
773 	&dev_attr_arp_validate.attr,
774 	&dev_attr_arp_all_targets.attr,
775 	&dev_attr_arp_interval.attr,
776 	&dev_attr_arp_ip_target.attr,
777 	&dev_attr_downdelay.attr,
778 	&dev_attr_updelay.attr,
779 	&dev_attr_peer_notif_delay.attr,
780 	&dev_attr_lacp_active.attr,
781 	&dev_attr_lacp_rate.attr,
782 	&dev_attr_ad_select.attr,
783 	&dev_attr_xmit_hash_policy.attr,
784 	&dev_attr_num_grat_arp.attr,
785 	&dev_attr_num_unsol_na.attr,
786 	&dev_attr_miimon.attr,
787 	&dev_attr_primary.attr,
788 	&dev_attr_primary_reselect.attr,
789 	&dev_attr_use_carrier.attr,
790 	&dev_attr_active_slave.attr,
791 	&dev_attr_mii_status.attr,
792 	&dev_attr_ad_aggregator.attr,
793 	&dev_attr_ad_num_ports.attr,
794 	&dev_attr_ad_actor_key.attr,
795 	&dev_attr_ad_partner_key.attr,
796 	&dev_attr_ad_partner_mac.attr,
797 	&dev_attr_queue_id.attr,
798 	&dev_attr_all_slaves_active.attr,
799 	&dev_attr_resend_igmp.attr,
800 	&dev_attr_min_links.attr,
801 	&dev_attr_lp_interval.attr,
802 	&dev_attr_packets_per_slave.attr,
803 	&dev_attr_tlb_dynamic_lb.attr,
804 	&dev_attr_ad_actor_sys_prio.attr,
805 	&dev_attr_ad_actor_system.attr,
806 	&dev_attr_ad_user_port_key.attr,
807 	&dev_attr_arp_missed_max.attr,
808 	NULL,
809 };
810 
811 static const struct attribute_group bonding_group = {
812 	.name = "bonding",
813 	.attrs = per_bond_attrs,
814 };
815 
816 /* Initialize sysfs.  This sets up the bonding_masters file in
817  * /sys/class/net.
818  */
819 int __net_init bond_create_sysfs(struct bond_net *bn)
820 {
821 	int ret;
822 
823 	bn->class_attr_bonding_masters = class_attr_bonding_masters;
824 	sysfs_attr_init(&bn->class_attr_bonding_masters.attr);
825 
826 	ret = netdev_class_create_file_ns(&bn->class_attr_bonding_masters,
827 					  to_ns_common(bn->net));
828 	/* Permit multiple loads of the module by ignoring failures to
829 	 * create the bonding_masters sysfs file.  Bonding devices
830 	 * created by second or subsequent loads of the module will
831 	 * not be listed in, or controllable by, bonding_masters, but
832 	 * will have the usual "bonding" sysfs directory.
833 	 *
834 	 * This is done to preserve backwards compatibility for
835 	 * initscripts/sysconfig, which load bonding multiple times to
836 	 * configure multiple bonding devices.
837 	 */
838 	if (ret == -EEXIST) {
839 		/* Is someone being kinky and naming a device bonding_master? */
840 		if (netdev_name_in_use(bn->net,
841 				       class_attr_bonding_masters.attr.name))
842 			pr_err("network device named %s already exists in sysfs\n",
843 			       class_attr_bonding_masters.attr.name);
844 		ret = 0;
845 	}
846 
847 	return ret;
848 
849 }
850 
851 /* Remove /sys/class/net/bonding_masters. */
852 void __net_exit bond_destroy_sysfs(struct bond_net *bn)
853 {
854 	netdev_class_remove_file_ns(&bn->class_attr_bonding_masters, to_ns_common(bn->net));
855 }
856 
857 /* Initialize sysfs for each bond.  This sets up and registers
858  * the 'bondctl' directory for each individual bond under /sys/class/net.
859  */
860 void bond_prepare_sysfs_group(struct bonding *bond)
861 {
862 	bond->dev->sysfs_groups[0] = &bonding_group;
863 }
864 
865