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