1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/net/bond/bond_options.c - bonding options
8 #include <linux/errno.h>
10 #include <linux/netdevice.h>
11 #include <linux/spinlock.h>
12 #include <linux/rcupdate.h>
13 #include <linux/ctype.h>
14 #include <linux/inet.h>
15 #include <linux/sched/signal.h>
17 #include <net/bonding.h>
19 static int bond_option_active_slave_set(struct bonding *bond,
20 const struct bond_opt_value *newval);
21 static int bond_option_miimon_set(struct bonding *bond,
22 const struct bond_opt_value *newval);
23 static int bond_option_updelay_set(struct bonding *bond,
24 const struct bond_opt_value *newval);
25 static int bond_option_downdelay_set(struct bonding *bond,
26 const struct bond_opt_value *newval);
27 static int bond_option_peer_notif_delay_set(struct bonding *bond,
28 const struct bond_opt_value *newval);
29 static int bond_option_use_carrier_set(struct bonding *bond,
30 const struct bond_opt_value *newval);
31 static int bond_option_arp_interval_set(struct bonding *bond,
32 const struct bond_opt_value *newval);
33 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
34 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
35 static int bond_option_arp_ip_targets_set(struct bonding *bond,
36 const struct bond_opt_value *newval);
37 static int bond_option_arp_validate_set(struct bonding *bond,
38 const struct bond_opt_value *newval);
39 static int bond_option_arp_all_targets_set(struct bonding *bond,
40 const struct bond_opt_value *newval);
41 static int bond_option_primary_set(struct bonding *bond,
42 const struct bond_opt_value *newval);
43 static int bond_option_primary_reselect_set(struct bonding *bond,
44 const struct bond_opt_value *newval);
45 static int bond_option_fail_over_mac_set(struct bonding *bond,
46 const struct bond_opt_value *newval);
47 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
48 const struct bond_opt_value *newval);
49 static int bond_option_resend_igmp_set(struct bonding *bond,
50 const struct bond_opt_value *newval);
51 static int bond_option_num_peer_notif_set(struct bonding *bond,
52 const struct bond_opt_value *newval);
53 static int bond_option_all_slaves_active_set(struct bonding *bond,
54 const struct bond_opt_value *newval);
55 static int bond_option_min_links_set(struct bonding *bond,
56 const struct bond_opt_value *newval);
57 static int bond_option_lp_interval_set(struct bonding *bond,
58 const struct bond_opt_value *newval);
59 static int bond_option_pps_set(struct bonding *bond,
60 const struct bond_opt_value *newval);
61 static int bond_option_lacp_active_set(struct bonding *bond,
62 const struct bond_opt_value *newval);
63 static int bond_option_lacp_rate_set(struct bonding *bond,
64 const struct bond_opt_value *newval);
65 static int bond_option_ad_select_set(struct bonding *bond,
66 const struct bond_opt_value *newval);
67 static int bond_option_queue_id_set(struct bonding *bond,
68 const struct bond_opt_value *newval);
69 static int bond_option_mode_set(struct bonding *bond,
70 const struct bond_opt_value *newval);
71 static int bond_option_slaves_set(struct bonding *bond,
72 const struct bond_opt_value *newval);
73 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
74 const struct bond_opt_value *newval);
75 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
76 const struct bond_opt_value *newval);
77 static int bond_option_ad_actor_system_set(struct bonding *bond,
78 const struct bond_opt_value *newval);
79 static int bond_option_ad_user_port_key_set(struct bonding *bond,
80 const struct bond_opt_value *newval);
83 static const struct bond_opt_value bond_mode_tbl[] = {
84 { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT},
85 { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
86 { "balance-xor", BOND_MODE_XOR, 0},
87 { "broadcast", BOND_MODE_BROADCAST, 0},
88 { "802.3ad", BOND_MODE_8023AD, 0},
89 { "balance-tlb", BOND_MODE_TLB, 0},
90 { "balance-alb", BOND_MODE_ALB, 0},
94 static const struct bond_opt_value bond_pps_tbl[] = {
95 { "default", 1, BOND_VALFLAG_DEFAULT},
96 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
100 static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
101 { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
102 { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
103 { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
104 { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
105 { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
106 { "vlan+srcmac", BOND_XMIT_POLICY_VLAN_SRCMAC, 0},
110 static const struct bond_opt_value bond_arp_validate_tbl[] = {
111 { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT},
112 { "active", BOND_ARP_VALIDATE_ACTIVE, 0},
113 { "backup", BOND_ARP_VALIDATE_BACKUP, 0},
114 { "all", BOND_ARP_VALIDATE_ALL, 0},
115 { "filter", BOND_ARP_FILTER, 0},
116 { "filter_active", BOND_ARP_FILTER_ACTIVE, 0},
117 { "filter_backup", BOND_ARP_FILTER_BACKUP, 0},
121 static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
122 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
123 { "all", BOND_ARP_TARGETS_ALL, 0},
127 static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
128 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
129 { "active", BOND_FOM_ACTIVE, 0},
130 { "follow", BOND_FOM_FOLLOW, 0},
134 static const struct bond_opt_value bond_intmax_tbl[] = {
135 { "off", 0, BOND_VALFLAG_DEFAULT},
136 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
140 static const struct bond_opt_value bond_lacp_active[] = {
142 { "on", 1, BOND_VALFLAG_DEFAULT},
146 static const struct bond_opt_value bond_lacp_rate_tbl[] = {
147 { "slow", AD_LACP_SLOW, 0},
148 { "fast", AD_LACP_FAST, 0},
152 static const struct bond_opt_value bond_ad_select_tbl[] = {
153 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
154 { "bandwidth", BOND_AD_BANDWIDTH, 0},
155 { "count", BOND_AD_COUNT, 0},
159 static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
161 { "maxval", 255, BOND_VALFLAG_MAX},
162 { "default", 1, BOND_VALFLAG_DEFAULT},
166 static const struct bond_opt_value bond_primary_reselect_tbl[] = {
167 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
168 { "better", BOND_PRI_RESELECT_BETTER, 0},
169 { "failure", BOND_PRI_RESELECT_FAILURE, 0},
173 static const struct bond_opt_value bond_use_carrier_tbl[] = {
175 { "on", 1, BOND_VALFLAG_DEFAULT},
179 static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
180 { "off", 0, BOND_VALFLAG_DEFAULT},
185 static const struct bond_opt_value bond_resend_igmp_tbl[] = {
187 { "maxval", 255, BOND_VALFLAG_MAX},
188 { "default", 1, BOND_VALFLAG_DEFAULT},
192 static const struct bond_opt_value bond_lp_interval_tbl[] = {
193 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
194 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
198 static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
200 { "on", 1, BOND_VALFLAG_DEFAULT},
204 static const struct bond_opt_value bond_ad_actor_sys_prio_tbl[] = {
205 { "minval", 1, BOND_VALFLAG_MIN},
206 { "maxval", 65535, BOND_VALFLAG_MAX | BOND_VALFLAG_DEFAULT},
210 static const struct bond_opt_value bond_ad_user_port_key_tbl[] = {
211 { "minval", 0, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
212 { "maxval", 1023, BOND_VALFLAG_MAX},
216 static const struct bond_option bond_opts[BOND_OPT_LAST] = {
220 .desc = "bond device mode",
221 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
222 .values = bond_mode_tbl,
223 .set = bond_option_mode_set
225 [BOND_OPT_PACKETS_PER_SLAVE] = {
226 .id = BOND_OPT_PACKETS_PER_SLAVE,
227 .name = "packets_per_slave",
228 .desc = "Packets to send per slave in RR mode",
229 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
230 .values = bond_pps_tbl,
231 .set = bond_option_pps_set
233 [BOND_OPT_XMIT_HASH] = {
234 .id = BOND_OPT_XMIT_HASH,
235 .name = "xmit_hash_policy",
236 .desc = "balance-xor, 802.3ad, and tlb hashing method",
237 .values = bond_xmit_hashtype_tbl,
238 .set = bond_option_xmit_hash_policy_set
240 [BOND_OPT_ARP_VALIDATE] = {
241 .id = BOND_OPT_ARP_VALIDATE,
242 .name = "arp_validate",
243 .desc = "validate src/dst of ARP probes",
244 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
246 .values = bond_arp_validate_tbl,
247 .set = bond_option_arp_validate_set
249 [BOND_OPT_ARP_ALL_TARGETS] = {
250 .id = BOND_OPT_ARP_ALL_TARGETS,
251 .name = "arp_all_targets",
252 .desc = "fail on any/all arp targets timeout",
253 .values = bond_arp_all_targets_tbl,
254 .set = bond_option_arp_all_targets_set
256 [BOND_OPT_FAIL_OVER_MAC] = {
257 .id = BOND_OPT_FAIL_OVER_MAC,
258 .name = "fail_over_mac",
259 .desc = "For active-backup, do not set all slaves to the same MAC",
260 .flags = BOND_OPTFLAG_NOSLAVES,
261 .values = bond_fail_over_mac_tbl,
262 .set = bond_option_fail_over_mac_set
264 [BOND_OPT_ARP_INTERVAL] = {
265 .id = BOND_OPT_ARP_INTERVAL,
266 .name = "arp_interval",
267 .desc = "arp interval in milliseconds",
268 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
270 .values = bond_intmax_tbl,
271 .set = bond_option_arp_interval_set
273 [BOND_OPT_ARP_TARGETS] = {
274 .id = BOND_OPT_ARP_TARGETS,
275 .name = "arp_ip_target",
276 .desc = "arp targets in n.n.n.n form",
277 .flags = BOND_OPTFLAG_RAWVAL,
278 .set = bond_option_arp_ip_targets_set
280 [BOND_OPT_DOWNDELAY] = {
281 .id = BOND_OPT_DOWNDELAY,
283 .desc = "Delay before considering link down, in milliseconds",
284 .values = bond_intmax_tbl,
285 .set = bond_option_downdelay_set
287 [BOND_OPT_UPDELAY] = {
288 .id = BOND_OPT_UPDELAY,
290 .desc = "Delay before considering link up, in milliseconds",
291 .values = bond_intmax_tbl,
292 .set = bond_option_updelay_set
294 [BOND_OPT_LACP_ACTIVE] = {
295 .id = BOND_OPT_LACP_ACTIVE,
296 .name = "lacp_active",
297 .desc = "Send LACPDU frames with configured lacp rate or acts as speak when spoken to",
298 .flags = BOND_OPTFLAG_IFDOWN,
299 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
300 .values = bond_lacp_active,
301 .set = bond_option_lacp_active_set
303 [BOND_OPT_LACP_RATE] = {
304 .id = BOND_OPT_LACP_RATE,
306 .desc = "LACPDU tx rate to request from 802.3ad partner",
307 .flags = BOND_OPTFLAG_IFDOWN,
308 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
309 .values = bond_lacp_rate_tbl,
310 .set = bond_option_lacp_rate_set
312 [BOND_OPT_MINLINKS] = {
313 .id = BOND_OPT_MINLINKS,
315 .desc = "Minimum number of available links before turning on carrier",
316 .values = bond_intmax_tbl,
317 .set = bond_option_min_links_set
319 [BOND_OPT_AD_SELECT] = {
320 .id = BOND_OPT_AD_SELECT,
322 .desc = "803.ad aggregation selection logic",
323 .flags = BOND_OPTFLAG_IFDOWN,
324 .values = bond_ad_select_tbl,
325 .set = bond_option_ad_select_set
327 [BOND_OPT_NUM_PEER_NOTIF] = {
328 .id = BOND_OPT_NUM_PEER_NOTIF,
329 .name = "num_unsol_na",
330 .desc = "Number of peer notifications to send on failover event",
331 .values = bond_num_peer_notif_tbl,
332 .set = bond_option_num_peer_notif_set
334 [BOND_OPT_MIIMON] = {
335 .id = BOND_OPT_MIIMON,
337 .desc = "Link check interval in milliseconds",
338 .values = bond_intmax_tbl,
339 .set = bond_option_miimon_set
341 [BOND_OPT_PRIMARY] = {
342 .id = BOND_OPT_PRIMARY,
344 .desc = "Primary network device to use",
345 .flags = BOND_OPTFLAG_RAWVAL,
346 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
349 .set = bond_option_primary_set
351 [BOND_OPT_PRIMARY_RESELECT] = {
352 .id = BOND_OPT_PRIMARY_RESELECT,
353 .name = "primary_reselect",
354 .desc = "Reselect primary slave once it comes up",
355 .values = bond_primary_reselect_tbl,
356 .set = bond_option_primary_reselect_set
358 [BOND_OPT_USE_CARRIER] = {
359 .id = BOND_OPT_USE_CARRIER,
360 .name = "use_carrier",
361 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
362 .values = bond_use_carrier_tbl,
363 .set = bond_option_use_carrier_set
365 [BOND_OPT_ACTIVE_SLAVE] = {
366 .id = BOND_OPT_ACTIVE_SLAVE,
367 .name = "active_slave",
368 .desc = "Currently active slave",
369 .flags = BOND_OPTFLAG_RAWVAL,
370 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
373 .set = bond_option_active_slave_set
375 [BOND_OPT_QUEUE_ID] = {
376 .id = BOND_OPT_QUEUE_ID,
378 .desc = "Set queue id of a slave",
379 .flags = BOND_OPTFLAG_RAWVAL,
380 .set = bond_option_queue_id_set
382 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
383 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
384 .name = "all_slaves_active",
385 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
386 .values = bond_all_slaves_active_tbl,
387 .set = bond_option_all_slaves_active_set
389 [BOND_OPT_RESEND_IGMP] = {
390 .id = BOND_OPT_RESEND_IGMP,
391 .name = "resend_igmp",
392 .desc = "Number of IGMP membership reports to send on link failure",
393 .values = bond_resend_igmp_tbl,
394 .set = bond_option_resend_igmp_set
396 [BOND_OPT_LP_INTERVAL] = {
397 .id = BOND_OPT_LP_INTERVAL,
398 .name = "lp_interval",
399 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
400 .values = bond_lp_interval_tbl,
401 .set = bond_option_lp_interval_set
403 [BOND_OPT_SLAVES] = {
404 .id = BOND_OPT_SLAVES,
406 .desc = "Slave membership management",
407 .flags = BOND_OPTFLAG_RAWVAL,
408 .set = bond_option_slaves_set
410 [BOND_OPT_TLB_DYNAMIC_LB] = {
411 .id = BOND_OPT_TLB_DYNAMIC_LB,
412 .name = "tlb_dynamic_lb",
413 .desc = "Enable dynamic flow shuffling",
414 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB) | BIT(BOND_MODE_ALB)),
415 .values = bond_tlb_dynamic_lb_tbl,
416 .flags = BOND_OPTFLAG_IFDOWN,
417 .set = bond_option_tlb_dynamic_lb_set,
419 [BOND_OPT_AD_ACTOR_SYS_PRIO] = {
420 .id = BOND_OPT_AD_ACTOR_SYS_PRIO,
421 .name = "ad_actor_sys_prio",
422 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
423 .values = bond_ad_actor_sys_prio_tbl,
424 .set = bond_option_ad_actor_sys_prio_set,
426 [BOND_OPT_AD_ACTOR_SYSTEM] = {
427 .id = BOND_OPT_AD_ACTOR_SYSTEM,
428 .name = "ad_actor_system",
429 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
430 .flags = BOND_OPTFLAG_RAWVAL,
431 .set = bond_option_ad_actor_system_set,
433 [BOND_OPT_AD_USER_PORT_KEY] = {
434 .id = BOND_OPT_AD_USER_PORT_KEY,
435 .name = "ad_user_port_key",
436 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
437 .flags = BOND_OPTFLAG_IFDOWN,
438 .values = bond_ad_user_port_key_tbl,
439 .set = bond_option_ad_user_port_key_set,
441 [BOND_OPT_NUM_PEER_NOTIF_ALIAS] = {
442 .id = BOND_OPT_NUM_PEER_NOTIF_ALIAS,
443 .name = "num_grat_arp",
444 .desc = "Number of peer notifications to send on failover event",
445 .values = bond_num_peer_notif_tbl,
446 .set = bond_option_num_peer_notif_set
448 [BOND_OPT_PEER_NOTIF_DELAY] = {
449 .id = BOND_OPT_PEER_NOTIF_DELAY,
450 .name = "peer_notif_delay",
451 .desc = "Delay between each peer notification on failover event, in milliseconds",
452 .values = bond_intmax_tbl,
453 .set = bond_option_peer_notif_delay_set
457 /* Searches for an option by name */
458 const struct bond_option *bond_opt_get_by_name(const char *name)
460 const struct bond_option *opt;
463 for (option = 0; option < BOND_OPT_LAST; option++) {
464 opt = bond_opt_get(option);
465 if (opt && !strcmp(opt->name, name))
472 /* Searches for a value in opt's values[] table */
473 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
475 const struct bond_option *opt;
478 opt = bond_opt_get(option);
481 for (i = 0; opt->values && opt->values[i].string; i++)
482 if (opt->values[i].value == val)
483 return &opt->values[i];
488 /* Searches for a value in opt's values[] table which matches the flagmask */
489 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
494 for (i = 0; opt->values && opt->values[i].string; i++)
495 if (opt->values[i].flags & flagmask)
496 return &opt->values[i];
501 /* If maxval is missing then there's no range to check. In case minval is
502 * missing then it's considered to be 0.
504 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
506 const struct bond_opt_value *minval, *maxval;
508 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
509 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
510 if (!maxval || (minval && val < minval->value) || val > maxval->value)
517 * bond_opt_parse - parse option value
518 * @opt: the option to parse against
519 * @val: value to parse
521 * This function tries to extract the value from @val and check if it's
522 * a possible match for the option and returns NULL if a match isn't found,
523 * or the struct_opt_value that matched. It also strips the new line from
524 * @val->string if it's present.
526 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
527 struct bond_opt_value *val)
529 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
530 const struct bond_opt_value *tbl;
531 const struct bond_opt_value *ret = NULL;
535 /* No parsing if the option wants a raw val */
536 if (opt->flags & BOND_OPTFLAG_RAWVAL)
543 /* ULLONG_MAX is used to bypass string processing */
544 checkval = val->value != ULLONG_MAX;
548 p = strchr(val->string, '\n');
551 for (p = val->string; *p; p++)
552 if (!(isdigit(*p) || isspace(*p)))
554 /* The following code extracts the string to match or the value
555 * and sets checkval appropriately
558 rv = sscanf(val->string, "%32s", valstr);
560 rv = sscanf(val->string, "%llu", &val->value);
567 for (i = 0; tbl[i].string; i++) {
568 /* Check for exact match */
570 if (val->value == tbl[i].value)
573 if (!strcmp(valstr, "default") &&
574 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
577 if (!strcmp(valstr, tbl[i].string))
580 /* Found an exact match */
584 /* Possible range match */
585 if (checkval && bond_opt_check_range(opt, val->value))
591 /* Check opt's dependencies against bond mode and currently set options */
592 static int bond_opt_check_deps(struct bonding *bond,
593 const struct bond_option *opt)
595 struct bond_params *params = &bond->params;
597 if (test_bit(params->mode, &opt->unsuppmodes))
599 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
601 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
607 static void bond_opt_dep_print(struct bonding *bond,
608 const struct bond_option *opt)
610 const struct bond_opt_value *modeval;
611 struct bond_params *params;
613 params = &bond->params;
614 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
615 if (test_bit(params->mode, &opt->unsuppmodes))
616 netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
617 opt->name, modeval->string, modeval->value);
620 static void bond_opt_error_interpret(struct bonding *bond,
621 const struct bond_option *opt,
622 int error, const struct bond_opt_value *val)
624 const struct bond_opt_value *minval, *maxval;
631 /* sometimes RAWVAL opts may have new lines */
632 p = strchr(val->string, '\n');
635 netdev_err(bond->dev, "option %s: invalid value (%s)\n",
636 opt->name, val->string);
638 netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
639 opt->name, val->value);
642 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
643 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
646 netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
647 opt->name, minval ? minval->value : 0, maxval->value);
650 bond_opt_dep_print(bond, opt);
653 netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
657 netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
661 if (val && val->string) {
662 p = strchr(val->string, '\n');
665 netdev_err(bond->dev, "option %s: interface %s does not exist!\n",
666 opt->name, val->string);
675 * __bond_opt_set - set a bonding option
676 * @bond: target bond device
677 * @option: option to set
678 * @val: value to set it to
680 * This function is used to change the bond's option value, it can be
681 * used for both enabling/changing an option and for disabling it. RTNL lock
682 * must be obtained before calling this function.
684 int __bond_opt_set(struct bonding *bond,
685 unsigned int option, struct bond_opt_value *val)
687 const struct bond_opt_value *retval = NULL;
688 const struct bond_option *opt;
693 opt = bond_opt_get(option);
694 if (WARN_ON(!val) || WARN_ON(!opt))
696 ret = bond_opt_check_deps(bond, opt);
699 retval = bond_opt_parse(opt, val);
704 ret = opt->set(bond, retval);
707 bond_opt_error_interpret(bond, opt, ret, val);
712 * __bond_opt_set_notify - set a bonding option
713 * @bond: target bond device
714 * @option: option to set
715 * @val: value to set it to
717 * This function is used to change the bond's option value and trigger
718 * a notification to user sapce. It can be used for both enabling/changing
719 * an option and for disabling it. RTNL lock must be obtained before calling
722 int __bond_opt_set_notify(struct bonding *bond,
723 unsigned int option, struct bond_opt_value *val)
729 ret = __bond_opt_set(bond, option, val);
731 if (!ret && (bond->dev->reg_state == NETREG_REGISTERED))
732 call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
738 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
739 * @bond: target bond device
740 * @option: option to set
741 * @buf: value to set it to
743 * This function tries to acquire RTNL without blocking and if successful
744 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
746 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
748 struct bond_opt_value optval;
752 return restart_syscall();
753 bond_opt_initstr(&optval, buf);
754 ret = __bond_opt_set_notify(bond, option, &optval);
761 * bond_opt_get - get a pointer to an option
762 * @option: option for which to return a pointer
764 * This function checks if option is valid and if so returns a pointer
765 * to its entry in the bond_opts[] option array.
767 const struct bond_option *bond_opt_get(unsigned int option)
769 if (!BOND_OPT_VALID(option))
772 return &bond_opts[option];
775 static bool bond_set_xfrm_features(struct bonding *bond)
777 if (!IS_ENABLED(CONFIG_XFRM_OFFLOAD))
780 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
781 bond->dev->wanted_features |= BOND_XFRM_FEATURES;
783 bond->dev->wanted_features &= ~BOND_XFRM_FEATURES;
788 static bool bond_set_tls_features(struct bonding *bond)
790 if (!IS_ENABLED(CONFIG_TLS_DEVICE))
793 if (bond_sk_check(bond))
794 bond->dev->wanted_features |= BOND_TLS_FEATURES;
796 bond->dev->wanted_features &= ~BOND_TLS_FEATURES;
801 static int bond_option_mode_set(struct bonding *bond,
802 const struct bond_opt_value *newval)
804 if (!bond_mode_uses_arp(newval->value)) {
805 if (bond->params.arp_interval) {
806 netdev_dbg(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
808 /* disable arp monitoring */
809 bond->params.arp_interval = 0;
812 if (!bond->params.miimon) {
813 /* set miimon to default value */
814 bond->params.miimon = BOND_DEFAULT_MIIMON;
815 netdev_dbg(bond->dev, "Setting MII monitoring interval to %d\n",
816 bond->params.miimon);
820 if (newval->value == BOND_MODE_ALB)
821 bond->params.tlb_dynamic_lb = 1;
823 /* don't cache arp_validate between modes */
824 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
825 bond->params.mode = newval->value;
827 if (bond->dev->reg_state == NETREG_REGISTERED) {
830 update |= bond_set_xfrm_features(bond);
831 update |= bond_set_tls_features(bond);
834 netdev_update_features(bond->dev);
840 static int bond_option_active_slave_set(struct bonding *bond,
841 const struct bond_opt_value *newval)
843 char ifname[IFNAMSIZ] = { 0, };
844 struct net_device *slave_dev;
847 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
848 if (!strlen(ifname) || newval->string[0] == '\n') {
851 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
857 if (!netif_is_bond_slave(slave_dev)) {
858 slave_err(bond->dev, slave_dev, "Device is not bonding slave\n");
862 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
863 slave_err(bond->dev, slave_dev, "Device is not our slave\n");
869 /* check to see if we are clearing active */
871 netdev_dbg(bond->dev, "Clearing current active slave\n");
872 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
873 bond_select_active_slave(bond);
875 struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
876 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
880 if (new_active == old_active) {
882 slave_dbg(bond->dev, new_active->dev, "is already the current active slave\n");
884 if (old_active && (new_active->link == BOND_LINK_UP) &&
885 bond_slave_is_up(new_active)) {
886 slave_dbg(bond->dev, new_active->dev, "Setting as active slave\n");
887 bond_change_active_slave(bond, new_active);
889 slave_err(bond->dev, new_active->dev, "Could not set as active slave; either %s is down or the link is down\n",
890 new_active->dev->name);
895 unblock_netpoll_tx();
900 /* There are two tricky bits here. First, if MII monitoring is activated, then
901 * we must disable ARP monitoring. Second, if the timer isn't running, we must
904 static int bond_option_miimon_set(struct bonding *bond,
905 const struct bond_opt_value *newval)
907 netdev_dbg(bond->dev, "Setting MII monitoring interval to %llu\n",
909 bond->params.miimon = newval->value;
910 if (bond->params.updelay)
911 netdev_dbg(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
912 bond->params.updelay * bond->params.miimon);
913 if (bond->params.downdelay)
914 netdev_dbg(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
915 bond->params.downdelay * bond->params.miimon);
916 if (bond->params.peer_notif_delay)
917 netdev_dbg(bond->dev, "Note: Updating peer_notif_delay (to %d) since it is a multiple of the miimon value\n",
918 bond->params.peer_notif_delay * bond->params.miimon);
919 if (newval->value && bond->params.arp_interval) {
920 netdev_dbg(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
921 bond->params.arp_interval = 0;
922 if (bond->params.arp_validate)
923 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
925 if (bond->dev->flags & IFF_UP) {
926 /* If the interface is up, we may need to fire off
927 * the MII timer. If the interface is down, the
928 * timer will get fired off when the open function
931 if (!newval->value) {
932 cancel_delayed_work_sync(&bond->mii_work);
934 cancel_delayed_work_sync(&bond->arp_work);
935 queue_delayed_work(bond->wq, &bond->mii_work, 0);
942 /* Set up, down and peer notification delays. These must be multiples
943 * of the MII monitoring value, and are stored internally as the
944 * multiplier. Thus, we must translate to MS for the real world.
946 static int _bond_option_delay_set(struct bonding *bond,
947 const struct bond_opt_value *newval,
951 int value = newval->value;
953 if (!bond->params.miimon) {
954 netdev_err(bond->dev, "Unable to set %s as MII monitoring is disabled\n",
958 if ((value % bond->params.miimon) != 0) {
959 netdev_warn(bond->dev,
960 "%s (%d) is not a multiple of miimon (%d), value rounded to %d ms\n",
962 value, bond->params.miimon,
963 (value / bond->params.miimon) *
964 bond->params.miimon);
966 *target = value / bond->params.miimon;
967 netdev_dbg(bond->dev, "Setting %s to %d\n",
969 *target * bond->params.miimon);
974 static int bond_option_updelay_set(struct bonding *bond,
975 const struct bond_opt_value *newval)
977 return _bond_option_delay_set(bond, newval, "up delay",
978 &bond->params.updelay);
981 static int bond_option_downdelay_set(struct bonding *bond,
982 const struct bond_opt_value *newval)
984 return _bond_option_delay_set(bond, newval, "down delay",
985 &bond->params.downdelay);
988 static int bond_option_peer_notif_delay_set(struct bonding *bond,
989 const struct bond_opt_value *newval)
991 int ret = _bond_option_delay_set(bond, newval,
992 "peer notification delay",
993 &bond->params.peer_notif_delay);
997 static int bond_option_use_carrier_set(struct bonding *bond,
998 const struct bond_opt_value *newval)
1000 netdev_dbg(bond->dev, "Setting use_carrier to %llu\n",
1002 bond->params.use_carrier = newval->value;
1007 /* There are two tricky bits here. First, if ARP monitoring is activated, then
1008 * we must disable MII monitoring. Second, if the ARP timer isn't running,
1011 static int bond_option_arp_interval_set(struct bonding *bond,
1012 const struct bond_opt_value *newval)
1014 netdev_dbg(bond->dev, "Setting ARP monitoring interval to %llu\n",
1016 bond->params.arp_interval = newval->value;
1017 if (newval->value) {
1018 if (bond->params.miimon) {
1019 netdev_dbg(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
1020 bond->params.miimon = 0;
1022 if (!bond->params.arp_targets[0])
1023 netdev_dbg(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
1025 if (bond->dev->flags & IFF_UP) {
1026 /* If the interface is up, we may need to fire off
1027 * the ARP timer. If the interface is down, the
1028 * timer will get fired off when the open function
1031 if (!newval->value) {
1032 if (bond->params.arp_validate)
1033 bond->recv_probe = NULL;
1034 cancel_delayed_work_sync(&bond->arp_work);
1036 /* arp_validate can be set only in active-backup mode */
1037 bond->recv_probe = bond_arp_rcv;
1038 cancel_delayed_work_sync(&bond->mii_work);
1039 queue_delayed_work(bond->wq, &bond->arp_work, 0);
1046 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
1048 unsigned long last_rx)
1050 __be32 *targets = bond->params.arp_targets;
1051 struct list_head *iter;
1052 struct slave *slave;
1054 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
1055 bond_for_each_slave(bond, slave, iter)
1056 slave->target_last_arp_rx[slot] = last_rx;
1057 targets[slot] = target;
1061 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
1063 __be32 *targets = bond->params.arp_targets;
1066 if (!bond_is_ip_target_ok(target)) {
1067 netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
1072 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
1073 netdev_err(bond->dev, "ARP target %pI4 is already present\n",
1078 ind = bond_get_targets_ip(targets, 0); /* first free slot */
1080 netdev_err(bond->dev, "ARP target table is full!\n");
1084 netdev_dbg(bond->dev, "Adding ARP target %pI4\n", &target);
1086 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
1091 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
1093 return _bond_option_arp_ip_target_add(bond, target);
1096 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
1098 __be32 *targets = bond->params.arp_targets;
1099 struct list_head *iter;
1100 struct slave *slave;
1101 unsigned long *targets_rx;
1104 if (!bond_is_ip_target_ok(target)) {
1105 netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
1110 ind = bond_get_targets_ip(targets, target);
1112 netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
1117 if (ind == 0 && !targets[1] && bond->params.arp_interval)
1118 netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
1120 netdev_dbg(bond->dev, "Removing ARP target %pI4\n", &target);
1122 bond_for_each_slave(bond, slave, iter) {
1123 targets_rx = slave->target_last_arp_rx;
1124 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1125 targets_rx[i] = targets_rx[i+1];
1128 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1129 targets[i] = targets[i+1];
1135 void bond_option_arp_ip_targets_clear(struct bonding *bond)
1139 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1140 _bond_options_arp_ip_target_set(bond, i, 0, 0);
1143 static int bond_option_arp_ip_targets_set(struct bonding *bond,
1144 const struct bond_opt_value *newval)
1149 if (newval->string) {
1150 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1151 netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
1155 if (newval->string[0] == '+')
1156 ret = bond_option_arp_ip_target_add(bond, target);
1157 else if (newval->string[0] == '-')
1158 ret = bond_option_arp_ip_target_rem(bond, target);
1160 netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
1162 target = newval->value;
1163 ret = bond_option_arp_ip_target_add(bond, target);
1169 static int bond_option_arp_validate_set(struct bonding *bond,
1170 const struct bond_opt_value *newval)
1172 netdev_dbg(bond->dev, "Setting arp_validate to %s (%llu)\n",
1173 newval->string, newval->value);
1174 bond->params.arp_validate = newval->value;
1179 static int bond_option_arp_all_targets_set(struct bonding *bond,
1180 const struct bond_opt_value *newval)
1182 netdev_dbg(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
1183 newval->string, newval->value);
1184 bond->params.arp_all_targets = newval->value;
1189 static int bond_option_primary_set(struct bonding *bond,
1190 const struct bond_opt_value *newval)
1192 char *p, *primary = newval->string;
1193 struct list_head *iter;
1194 struct slave *slave;
1198 p = strchr(primary, '\n');
1201 /* check to see if we are clearing primary */
1202 if (!strlen(primary)) {
1203 netdev_dbg(bond->dev, "Setting primary slave to None\n");
1204 RCU_INIT_POINTER(bond->primary_slave, NULL);
1205 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1206 bond_select_active_slave(bond);
1210 bond_for_each_slave(bond, slave, iter) {
1211 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1212 slave_dbg(bond->dev, slave->dev, "Setting as primary slave\n");
1213 rcu_assign_pointer(bond->primary_slave, slave);
1214 strcpy(bond->params.primary, slave->dev->name);
1215 bond->force_primary = true;
1216 bond_select_active_slave(bond);
1221 if (rtnl_dereference(bond->primary_slave)) {
1222 netdev_dbg(bond->dev, "Setting primary slave to None\n");
1223 RCU_INIT_POINTER(bond->primary_slave, NULL);
1224 bond_select_active_slave(bond);
1226 strscpy_pad(bond->params.primary, primary, IFNAMSIZ);
1228 netdev_dbg(bond->dev, "Recording %s as primary, but it has not been enslaved yet\n",
1232 unblock_netpoll_tx();
1237 static int bond_option_primary_reselect_set(struct bonding *bond,
1238 const struct bond_opt_value *newval)
1240 netdev_dbg(bond->dev, "Setting primary_reselect to %s (%llu)\n",
1241 newval->string, newval->value);
1242 bond->params.primary_reselect = newval->value;
1245 bond_select_active_slave(bond);
1246 unblock_netpoll_tx();
1251 static int bond_option_fail_over_mac_set(struct bonding *bond,
1252 const struct bond_opt_value *newval)
1254 netdev_dbg(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
1255 newval->string, newval->value);
1256 bond->params.fail_over_mac = newval->value;
1261 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1262 const struct bond_opt_value *newval)
1264 netdev_dbg(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
1265 newval->string, newval->value);
1266 bond->params.xmit_policy = newval->value;
1268 if (bond->dev->reg_state == NETREG_REGISTERED)
1269 if (bond_set_tls_features(bond))
1270 netdev_update_features(bond->dev);
1275 static int bond_option_resend_igmp_set(struct bonding *bond,
1276 const struct bond_opt_value *newval)
1278 netdev_dbg(bond->dev, "Setting resend_igmp to %llu\n",
1280 bond->params.resend_igmp = newval->value;
1285 static int bond_option_num_peer_notif_set(struct bonding *bond,
1286 const struct bond_opt_value *newval)
1288 bond->params.num_peer_notif = newval->value;
1293 static int bond_option_all_slaves_active_set(struct bonding *bond,
1294 const struct bond_opt_value *newval)
1296 struct list_head *iter;
1297 struct slave *slave;
1299 if (newval->value == bond->params.all_slaves_active)
1301 bond->params.all_slaves_active = newval->value;
1302 bond_for_each_slave(bond, slave, iter) {
1303 if (!bond_is_active_slave(slave)) {
1305 slave->inactive = 0;
1307 slave->inactive = 1;
1314 static int bond_option_min_links_set(struct bonding *bond,
1315 const struct bond_opt_value *newval)
1317 netdev_dbg(bond->dev, "Setting min links value to %llu\n",
1319 bond->params.min_links = newval->value;
1320 bond_set_carrier(bond);
1325 static int bond_option_lp_interval_set(struct bonding *bond,
1326 const struct bond_opt_value *newval)
1328 bond->params.lp_interval = newval->value;
1333 static int bond_option_pps_set(struct bonding *bond,
1334 const struct bond_opt_value *newval)
1336 netdev_dbg(bond->dev, "Setting packets per slave to %llu\n",
1338 bond->params.packets_per_slave = newval->value;
1339 if (newval->value > 0) {
1340 bond->params.reciprocal_packets_per_slave =
1341 reciprocal_value(newval->value);
1343 /* reciprocal_packets_per_slave is unused if
1344 * packets_per_slave is 0 or 1, just initialize it
1346 bond->params.reciprocal_packets_per_slave =
1347 (struct reciprocal_value) { 0 };
1353 static int bond_option_lacp_active_set(struct bonding *bond,
1354 const struct bond_opt_value *newval)
1356 netdev_dbg(bond->dev, "Setting LACP active to %s (%llu)\n",
1357 newval->string, newval->value);
1358 bond->params.lacp_active = newval->value;
1363 static int bond_option_lacp_rate_set(struct bonding *bond,
1364 const struct bond_opt_value *newval)
1366 netdev_dbg(bond->dev, "Setting LACP rate to %s (%llu)\n",
1367 newval->string, newval->value);
1368 bond->params.lacp_fast = newval->value;
1369 bond_3ad_update_lacp_rate(bond);
1374 static int bond_option_ad_select_set(struct bonding *bond,
1375 const struct bond_opt_value *newval)
1377 netdev_dbg(bond->dev, "Setting ad_select to %s (%llu)\n",
1378 newval->string, newval->value);
1379 bond->params.ad_select = newval->value;
1384 static int bond_option_queue_id_set(struct bonding *bond,
1385 const struct bond_opt_value *newval)
1387 struct slave *slave, *update_slave;
1388 struct net_device *sdev;
1389 struct list_head *iter;
1394 /* delim will point to queue id if successful */
1395 delim = strchr(newval->string, ':');
1399 /* Terminate string that points to device name and bump it
1400 * up one, so we can read the queue id there.
1403 if (sscanf(++delim, "%hd\n", &qid) != 1)
1406 /* Check buffer length, valid ifname and queue id */
1407 if (!dev_valid_name(newval->string) ||
1408 qid > bond->dev->real_num_tx_queues)
1411 /* Get the pointer to that interface if it exists */
1412 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1416 /* Search for thes slave and check for duplicate qids */
1417 update_slave = NULL;
1418 bond_for_each_slave(bond, slave, iter) {
1419 if (sdev == slave->dev)
1420 /* We don't need to check the matching
1421 * slave for dups, since we're overwriting it
1423 update_slave = slave;
1424 else if (qid && qid == slave->queue_id) {
1432 /* Actually set the qids for the slave */
1433 update_slave->queue_id = qid;
1439 netdev_dbg(bond->dev, "invalid input for queue_id set\n");
1445 static int bond_option_slaves_set(struct bonding *bond,
1446 const struct bond_opt_value *newval)
1448 char command[IFNAMSIZ + 1] = { 0, };
1449 struct net_device *dev;
1453 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1454 ifname = command + 1;
1455 if ((strlen(command) <= 1) ||
1456 (command[0] != '+' && command[0] != '-') ||
1457 !dev_valid_name(ifname))
1460 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1462 netdev_dbg(bond->dev, "interface %s does not exist!\n",
1468 switch (command[0]) {
1470 slave_dbg(bond->dev, dev, "Enslaving interface\n");
1471 ret = bond_enslave(bond->dev, dev, NULL);
1475 slave_dbg(bond->dev, dev, "Releasing interface\n");
1476 ret = bond_release(bond->dev, dev);
1480 /* should not run here. */
1488 netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
1493 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1494 const struct bond_opt_value *newval)
1496 netdev_dbg(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
1497 newval->string, newval->value);
1498 bond->params.tlb_dynamic_lb = newval->value;
1503 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
1504 const struct bond_opt_value *newval)
1506 netdev_dbg(bond->dev, "Setting ad_actor_sys_prio to %llu\n",
1509 bond->params.ad_actor_sys_prio = newval->value;
1510 bond_3ad_update_ad_actor_settings(bond);
1515 static int bond_option_ad_actor_system_set(struct bonding *bond,
1516 const struct bond_opt_value *newval)
1518 u8 macaddr[ETH_ALEN];
1521 if (newval->string) {
1522 if (!mac_pton(newval->string, macaddr))
1526 mac = (u8 *)&newval->value;
1529 if (!is_valid_ether_addr(mac))
1532 netdev_dbg(bond->dev, "Setting ad_actor_system to %pM\n", mac);
1533 ether_addr_copy(bond->params.ad_actor_system, mac);
1534 bond_3ad_update_ad_actor_settings(bond);
1539 netdev_err(bond->dev, "Invalid ad_actor_system MAC address.\n");
1543 static int bond_option_ad_user_port_key_set(struct bonding *bond,
1544 const struct bond_opt_value *newval)
1546 netdev_dbg(bond->dev, "Setting ad_user_port_key to %llu\n",
1549 bond->params.ad_user_port_key = newval->value;