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