2 * drivers/net/bond/bond_options.c - bonding options
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
4 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/errno.h>
14 #include <linux/netdevice.h>
15 #include <linux/spinlock.h>
16 #include <linux/rcupdate.h>
17 #include <linux/ctype.h>
18 #include <linux/inet.h>
19 #include <net/bonding.h>
21 static int bond_option_active_slave_set(struct bonding *bond,
22 const struct bond_opt_value *newval);
23 static int bond_option_miimon_set(struct bonding *bond,
24 const struct bond_opt_value *newval);
25 static int bond_option_updelay_set(struct bonding *bond,
26 const struct bond_opt_value *newval);
27 static int bond_option_downdelay_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_rate_set(struct bonding *bond,
62 const struct bond_opt_value *newval);
63 static int bond_option_ad_select_set(struct bonding *bond,
64 const struct bond_opt_value *newval);
65 static int bond_option_queue_id_set(struct bonding *bond,
66 const struct bond_opt_value *newval);
67 static int bond_option_mode_set(struct bonding *bond,
68 const struct bond_opt_value *newval);
69 static int bond_option_slaves_set(struct bonding *bond,
70 const struct bond_opt_value *newval);
71 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
72 const struct bond_opt_value *newval);
73 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
74 const struct bond_opt_value *newval);
77 static const struct bond_opt_value bond_mode_tbl[] = {
78 { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT},
79 { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
80 { "balance-xor", BOND_MODE_XOR, 0},
81 { "broadcast", BOND_MODE_BROADCAST, 0},
82 { "802.3ad", BOND_MODE_8023AD, 0},
83 { "balance-tlb", BOND_MODE_TLB, 0},
84 { "balance-alb", BOND_MODE_ALB, 0},
88 static const struct bond_opt_value bond_pps_tbl[] = {
89 { "default", 1, BOND_VALFLAG_DEFAULT},
90 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
94 static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
95 { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
96 { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
97 { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
98 { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
99 { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
103 static const struct bond_opt_value bond_arp_validate_tbl[] = {
104 { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT},
105 { "active", BOND_ARP_VALIDATE_ACTIVE, 0},
106 { "backup", BOND_ARP_VALIDATE_BACKUP, 0},
107 { "all", BOND_ARP_VALIDATE_ALL, 0},
108 { "filter", BOND_ARP_FILTER, 0},
109 { "filter_active", BOND_ARP_FILTER_ACTIVE, 0},
110 { "filter_backup", BOND_ARP_FILTER_BACKUP, 0},
114 static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
115 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
116 { "all", BOND_ARP_TARGETS_ALL, 0},
120 static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
121 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
122 { "active", BOND_FOM_ACTIVE, 0},
123 { "follow", BOND_FOM_FOLLOW, 0},
127 static const struct bond_opt_value bond_intmax_tbl[] = {
128 { "off", 0, BOND_VALFLAG_DEFAULT},
129 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
133 static const struct bond_opt_value bond_lacp_rate_tbl[] = {
134 { "slow", AD_LACP_SLOW, 0},
135 { "fast", AD_LACP_FAST, 0},
139 static const struct bond_opt_value bond_ad_select_tbl[] = {
140 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
141 { "bandwidth", BOND_AD_BANDWIDTH, 0},
142 { "count", BOND_AD_COUNT, 0},
146 static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
148 { "maxval", 255, BOND_VALFLAG_MAX},
149 { "default", 1, BOND_VALFLAG_DEFAULT},
153 static const struct bond_opt_value bond_primary_reselect_tbl[] = {
154 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
155 { "better", BOND_PRI_RESELECT_BETTER, 0},
156 { "failure", BOND_PRI_RESELECT_FAILURE, 0},
160 static const struct bond_opt_value bond_use_carrier_tbl[] = {
162 { "on", 1, BOND_VALFLAG_DEFAULT},
166 static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
167 { "off", 0, BOND_VALFLAG_DEFAULT},
172 static const struct bond_opt_value bond_resend_igmp_tbl[] = {
174 { "maxval", 255, BOND_VALFLAG_MAX},
175 { "default", 1, BOND_VALFLAG_DEFAULT},
179 static const struct bond_opt_value bond_lp_interval_tbl[] = {
180 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
181 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
185 static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
187 { "on", 1, BOND_VALFLAG_DEFAULT},
191 static const struct bond_opt_value bond_ad_actor_sys_prio_tbl[] = {
192 { "minval", 1, BOND_VALFLAG_MIN},
193 { "maxval", 65535, BOND_VALFLAG_MAX | BOND_VALFLAG_DEFAULT},
197 static const struct bond_option bond_opts[BOND_OPT_LAST] = {
201 .desc = "bond device mode",
202 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
203 .values = bond_mode_tbl,
204 .set = bond_option_mode_set
206 [BOND_OPT_PACKETS_PER_SLAVE] = {
207 .id = BOND_OPT_PACKETS_PER_SLAVE,
208 .name = "packets_per_slave",
209 .desc = "Packets to send per slave in RR mode",
210 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
211 .values = bond_pps_tbl,
212 .set = bond_option_pps_set
214 [BOND_OPT_XMIT_HASH] = {
215 .id = BOND_OPT_XMIT_HASH,
216 .name = "xmit_hash_policy",
217 .desc = "balance-xor, 802.3ad, and tlb hashing method",
218 .values = bond_xmit_hashtype_tbl,
219 .set = bond_option_xmit_hash_policy_set
221 [BOND_OPT_ARP_VALIDATE] = {
222 .id = BOND_OPT_ARP_VALIDATE,
223 .name = "arp_validate",
224 .desc = "validate src/dst of ARP probes",
225 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
227 .values = bond_arp_validate_tbl,
228 .set = bond_option_arp_validate_set
230 [BOND_OPT_ARP_ALL_TARGETS] = {
231 .id = BOND_OPT_ARP_ALL_TARGETS,
232 .name = "arp_all_targets",
233 .desc = "fail on any/all arp targets timeout",
234 .values = bond_arp_all_targets_tbl,
235 .set = bond_option_arp_all_targets_set
237 [BOND_OPT_FAIL_OVER_MAC] = {
238 .id = BOND_OPT_FAIL_OVER_MAC,
239 .name = "fail_over_mac",
240 .desc = "For active-backup, do not set all slaves to the same MAC",
241 .flags = BOND_OPTFLAG_NOSLAVES,
242 .values = bond_fail_over_mac_tbl,
243 .set = bond_option_fail_over_mac_set
245 [BOND_OPT_ARP_INTERVAL] = {
246 .id = BOND_OPT_ARP_INTERVAL,
247 .name = "arp_interval",
248 .desc = "arp interval in milliseconds",
249 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
251 .values = bond_intmax_tbl,
252 .set = bond_option_arp_interval_set
254 [BOND_OPT_ARP_TARGETS] = {
255 .id = BOND_OPT_ARP_TARGETS,
256 .name = "arp_ip_target",
257 .desc = "arp targets in n.n.n.n form",
258 .flags = BOND_OPTFLAG_RAWVAL,
259 .set = bond_option_arp_ip_targets_set
261 [BOND_OPT_DOWNDELAY] = {
262 .id = BOND_OPT_DOWNDELAY,
264 .desc = "Delay before considering link down, in milliseconds",
265 .values = bond_intmax_tbl,
266 .set = bond_option_downdelay_set
268 [BOND_OPT_UPDELAY] = {
269 .id = BOND_OPT_UPDELAY,
271 .desc = "Delay before considering link up, in milliseconds",
272 .values = bond_intmax_tbl,
273 .set = bond_option_updelay_set
275 [BOND_OPT_LACP_RATE] = {
276 .id = BOND_OPT_LACP_RATE,
278 .desc = "LACPDU tx rate to request from 802.3ad partner",
279 .flags = BOND_OPTFLAG_IFDOWN,
280 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
281 .values = bond_lacp_rate_tbl,
282 .set = bond_option_lacp_rate_set
284 [BOND_OPT_MINLINKS] = {
285 .id = BOND_OPT_MINLINKS,
287 .desc = "Minimum number of available links before turning on carrier",
288 .values = bond_intmax_tbl,
289 .set = bond_option_min_links_set
291 [BOND_OPT_AD_SELECT] = {
292 .id = BOND_OPT_AD_SELECT,
294 .desc = "803.ad aggregation selection logic",
295 .flags = BOND_OPTFLAG_IFDOWN,
296 .values = bond_ad_select_tbl,
297 .set = bond_option_ad_select_set
299 [BOND_OPT_NUM_PEER_NOTIF] = {
300 .id = BOND_OPT_NUM_PEER_NOTIF,
301 .name = "num_unsol_na",
302 .desc = "Number of peer notifications to send on failover event",
303 .values = bond_num_peer_notif_tbl,
304 .set = bond_option_num_peer_notif_set
306 [BOND_OPT_MIIMON] = {
307 .id = BOND_OPT_MIIMON,
309 .desc = "Link check interval in milliseconds",
310 .values = bond_intmax_tbl,
311 .set = bond_option_miimon_set
313 [BOND_OPT_PRIMARY] = {
314 .id = BOND_OPT_PRIMARY,
316 .desc = "Primary network device to use",
317 .flags = BOND_OPTFLAG_RAWVAL,
318 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
321 .set = bond_option_primary_set
323 [BOND_OPT_PRIMARY_RESELECT] = {
324 .id = BOND_OPT_PRIMARY_RESELECT,
325 .name = "primary_reselect",
326 .desc = "Reselect primary slave once it comes up",
327 .values = bond_primary_reselect_tbl,
328 .set = bond_option_primary_reselect_set
330 [BOND_OPT_USE_CARRIER] = {
331 .id = BOND_OPT_USE_CARRIER,
332 .name = "use_carrier",
333 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
334 .values = bond_use_carrier_tbl,
335 .set = bond_option_use_carrier_set
337 [BOND_OPT_ACTIVE_SLAVE] = {
338 .id = BOND_OPT_ACTIVE_SLAVE,
339 .name = "active_slave",
340 .desc = "Currently active slave",
341 .flags = BOND_OPTFLAG_RAWVAL,
342 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
345 .set = bond_option_active_slave_set
347 [BOND_OPT_QUEUE_ID] = {
348 .id = BOND_OPT_QUEUE_ID,
350 .desc = "Set queue id of a slave",
351 .flags = BOND_OPTFLAG_RAWVAL,
352 .set = bond_option_queue_id_set
354 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
355 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
356 .name = "all_slaves_active",
357 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
358 .values = bond_all_slaves_active_tbl,
359 .set = bond_option_all_slaves_active_set
361 [BOND_OPT_RESEND_IGMP] = {
362 .id = BOND_OPT_RESEND_IGMP,
363 .name = "resend_igmp",
364 .desc = "Number of IGMP membership reports to send on link failure",
365 .values = bond_resend_igmp_tbl,
366 .set = bond_option_resend_igmp_set
368 [BOND_OPT_LP_INTERVAL] = {
369 .id = BOND_OPT_LP_INTERVAL,
370 .name = "lp_interval",
371 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
372 .values = bond_lp_interval_tbl,
373 .set = bond_option_lp_interval_set
375 [BOND_OPT_SLAVES] = {
376 .id = BOND_OPT_SLAVES,
378 .desc = "Slave membership management",
379 .flags = BOND_OPTFLAG_RAWVAL,
380 .set = bond_option_slaves_set
382 [BOND_OPT_TLB_DYNAMIC_LB] = {
383 .id = BOND_OPT_TLB_DYNAMIC_LB,
384 .name = "tlb_dynamic_lb",
385 .desc = "Enable dynamic flow shuffling",
386 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB)),
387 .values = bond_tlb_dynamic_lb_tbl,
388 .flags = BOND_OPTFLAG_IFDOWN,
389 .set = bond_option_tlb_dynamic_lb_set,
391 [BOND_OPT_AD_ACTOR_SYS_PRIO] = {
392 .id = BOND_OPT_AD_ACTOR_SYS_PRIO,
393 .name = "ad_actor_sys_prio",
394 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
395 .flags = BOND_OPTFLAG_IFDOWN,
396 .values = bond_ad_actor_sys_prio_tbl,
397 .set = bond_option_ad_actor_sys_prio_set,
401 /* Searches for an option by name */
402 const struct bond_option *bond_opt_get_by_name(const char *name)
404 const struct bond_option *opt;
407 for (option = 0; option < BOND_OPT_LAST; option++) {
408 opt = bond_opt_get(option);
409 if (opt && !strcmp(opt->name, name))
416 /* Searches for a value in opt's values[] table */
417 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
419 const struct bond_option *opt;
422 opt = bond_opt_get(option);
425 for (i = 0; opt->values && opt->values[i].string; i++)
426 if (opt->values[i].value == val)
427 return &opt->values[i];
432 /* Searches for a value in opt's values[] table which matches the flagmask */
433 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
438 for (i = 0; opt->values && opt->values[i].string; i++)
439 if (opt->values[i].flags & flagmask)
440 return &opt->values[i];
445 /* If maxval is missing then there's no range to check. In case minval is
446 * missing then it's considered to be 0.
448 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
450 const struct bond_opt_value *minval, *maxval;
452 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
453 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
454 if (!maxval || (minval && val < minval->value) || val > maxval->value)
461 * bond_opt_parse - parse option value
462 * @opt: the option to parse against
463 * @val: value to parse
465 * This function tries to extract the value from @val and check if it's
466 * a possible match for the option and returns NULL if a match isn't found,
467 * or the struct_opt_value that matched. It also strips the new line from
468 * @val->string if it's present.
470 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
471 struct bond_opt_value *val)
473 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
474 const struct bond_opt_value *tbl;
475 const struct bond_opt_value *ret = NULL;
479 /* No parsing if the option wants a raw val */
480 if (opt->flags & BOND_OPTFLAG_RAWVAL)
487 /* ULLONG_MAX is used to bypass string processing */
488 checkval = val->value != ULLONG_MAX;
492 p = strchr(val->string, '\n');
495 for (p = val->string; *p; p++)
496 if (!(isdigit(*p) || isspace(*p)))
498 /* The following code extracts the string to match or the value
499 * and sets checkval appropriately
502 rv = sscanf(val->string, "%32s", valstr);
504 rv = sscanf(val->string, "%llu", &val->value);
511 for (i = 0; tbl[i].string; i++) {
512 /* Check for exact match */
514 if (val->value == tbl[i].value)
517 if (!strcmp(valstr, "default") &&
518 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
521 if (!strcmp(valstr, tbl[i].string))
524 /* Found an exact match */
528 /* Possible range match */
529 if (checkval && bond_opt_check_range(opt, val->value))
535 /* Check opt's dependencies against bond mode and currently set options */
536 static int bond_opt_check_deps(struct bonding *bond,
537 const struct bond_option *opt)
539 struct bond_params *params = &bond->params;
541 if (test_bit(params->mode, &opt->unsuppmodes))
543 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
545 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
551 static void bond_opt_dep_print(struct bonding *bond,
552 const struct bond_option *opt)
554 const struct bond_opt_value *modeval;
555 struct bond_params *params;
557 params = &bond->params;
558 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
559 if (test_bit(params->mode, &opt->unsuppmodes))
560 netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
561 opt->name, modeval->string, modeval->value);
564 static void bond_opt_error_interpret(struct bonding *bond,
565 const struct bond_option *opt,
566 int error, const struct bond_opt_value *val)
568 const struct bond_opt_value *minval, *maxval;
575 /* sometimes RAWVAL opts may have new lines */
576 p = strchr(val->string, '\n');
579 netdev_err(bond->dev, "option %s: invalid value (%s)\n",
580 opt->name, val->string);
582 netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
583 opt->name, val->value);
586 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
587 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
590 netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
591 opt->name, minval ? minval->value : 0, maxval->value);
594 bond_opt_dep_print(bond, opt);
597 netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
601 netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
610 * __bond_opt_set - set a bonding option
611 * @bond: target bond device
612 * @option: option to set
613 * @val: value to set it to
615 * This function is used to change the bond's option value, it can be
616 * used for both enabling/changing an option and for disabling it. RTNL lock
617 * must be obtained before calling this function.
619 int __bond_opt_set(struct bonding *bond,
620 unsigned int option, struct bond_opt_value *val)
622 const struct bond_opt_value *retval = NULL;
623 const struct bond_option *opt;
628 opt = bond_opt_get(option);
629 if (WARN_ON(!val) || WARN_ON(!opt))
631 ret = bond_opt_check_deps(bond, opt);
634 retval = bond_opt_parse(opt, val);
639 ret = opt->set(bond, retval);
642 bond_opt_error_interpret(bond, opt, ret, val);
644 call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
650 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
651 * @bond: target bond device
652 * @option: option to set
653 * @buf: value to set it to
655 * This function tries to acquire RTNL without blocking and if successful
656 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
658 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
660 struct bond_opt_value optval;
664 return restart_syscall();
665 bond_opt_initstr(&optval, buf);
666 ret = __bond_opt_set(bond, option, &optval);
673 * bond_opt_get - get a pointer to an option
674 * @option: option for which to return a pointer
676 * This function checks if option is valid and if so returns a pointer
677 * to its entry in the bond_opts[] option array.
679 const struct bond_option *bond_opt_get(unsigned int option)
681 if (!BOND_OPT_VALID(option))
684 return &bond_opts[option];
687 static int bond_option_mode_set(struct bonding *bond,
688 const struct bond_opt_value *newval)
690 if (!bond_mode_uses_arp(newval->value) && bond->params.arp_interval) {
691 netdev_info(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
693 /* disable arp monitoring */
694 bond->params.arp_interval = 0;
695 /* set miimon to default value */
696 bond->params.miimon = BOND_DEFAULT_MIIMON;
697 netdev_info(bond->dev, "Setting MII monitoring interval to %d\n",
698 bond->params.miimon);
701 /* don't cache arp_validate between modes */
702 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
703 bond->params.mode = newval->value;
708 static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
711 return bond_uses_primary(bond) && slave ? slave->dev : NULL;
714 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
716 struct slave *slave = rcu_dereference(bond->curr_active_slave);
718 return __bond_option_active_slave_get(bond, slave);
721 static int bond_option_active_slave_set(struct bonding *bond,
722 const struct bond_opt_value *newval)
724 char ifname[IFNAMSIZ] = { 0, };
725 struct net_device *slave_dev;
728 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
729 if (!strlen(ifname) || newval->string[0] == '\n') {
732 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
738 if (!netif_is_bond_slave(slave_dev)) {
739 netdev_err(bond->dev, "Device %s is not bonding slave\n",
744 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
745 netdev_err(bond->dev, "Device %s is not our slave\n",
752 /* check to see if we are clearing active */
754 netdev_info(bond->dev, "Clearing current active slave\n");
755 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
756 bond_select_active_slave(bond);
758 struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
759 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
763 if (new_active == old_active) {
765 netdev_info(bond->dev, "%s is already the current active slave\n",
766 new_active->dev->name);
768 if (old_active && (new_active->link == BOND_LINK_UP) &&
769 bond_slave_is_up(new_active)) {
770 netdev_info(bond->dev, "Setting %s as active slave\n",
771 new_active->dev->name);
772 bond_change_active_slave(bond, new_active);
774 netdev_err(bond->dev, "Could not set %s as active slave; either %s is down or the link is down\n",
775 new_active->dev->name,
776 new_active->dev->name);
781 unblock_netpoll_tx();
786 /* There are two tricky bits here. First, if MII monitoring is activated, then
787 * we must disable ARP monitoring. Second, if the timer isn't running, we must
790 static int bond_option_miimon_set(struct bonding *bond,
791 const struct bond_opt_value *newval)
793 netdev_info(bond->dev, "Setting MII monitoring interval to %llu\n",
795 bond->params.miimon = newval->value;
796 if (bond->params.updelay)
797 netdev_info(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
798 bond->params.updelay * bond->params.miimon);
799 if (bond->params.downdelay)
800 netdev_info(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
801 bond->params.downdelay * bond->params.miimon);
802 if (newval->value && bond->params.arp_interval) {
803 netdev_info(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
804 bond->params.arp_interval = 0;
805 if (bond->params.arp_validate)
806 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
808 if (bond->dev->flags & IFF_UP) {
809 /* If the interface is up, we may need to fire off
810 * the MII timer. If the interface is down, the
811 * timer will get fired off when the open function
814 if (!newval->value) {
815 cancel_delayed_work_sync(&bond->mii_work);
817 cancel_delayed_work_sync(&bond->arp_work);
818 queue_delayed_work(bond->wq, &bond->mii_work, 0);
825 /* Set up and down delays. These must be multiples of the
826 * MII monitoring value, and are stored internally as the multiplier.
827 * Thus, we must translate to MS for the real world.
829 static int bond_option_updelay_set(struct bonding *bond,
830 const struct bond_opt_value *newval)
832 int value = newval->value;
834 if (!bond->params.miimon) {
835 netdev_err(bond->dev, "Unable to set up delay as MII monitoring is disabled\n");
838 if ((value % bond->params.miimon) != 0) {
839 netdev_warn(bond->dev, "up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
840 value, bond->params.miimon,
841 (value / bond->params.miimon) *
842 bond->params.miimon);
844 bond->params.updelay = value / bond->params.miimon;
845 netdev_info(bond->dev, "Setting up delay to %d\n",
846 bond->params.updelay * bond->params.miimon);
851 static int bond_option_downdelay_set(struct bonding *bond,
852 const struct bond_opt_value *newval)
854 int value = newval->value;
856 if (!bond->params.miimon) {
857 netdev_err(bond->dev, "Unable to set down delay as MII monitoring is disabled\n");
860 if ((value % bond->params.miimon) != 0) {
861 netdev_warn(bond->dev, "down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
862 value, bond->params.miimon,
863 (value / bond->params.miimon) *
864 bond->params.miimon);
866 bond->params.downdelay = value / bond->params.miimon;
867 netdev_info(bond->dev, "Setting down delay to %d\n",
868 bond->params.downdelay * bond->params.miimon);
873 static int bond_option_use_carrier_set(struct bonding *bond,
874 const struct bond_opt_value *newval)
876 netdev_info(bond->dev, "Setting use_carrier to %llu\n",
878 bond->params.use_carrier = newval->value;
883 /* There are two tricky bits here. First, if ARP monitoring is activated, then
884 * we must disable MII monitoring. Second, if the ARP timer isn't running,
887 static int bond_option_arp_interval_set(struct bonding *bond,
888 const struct bond_opt_value *newval)
890 netdev_info(bond->dev, "Setting ARP monitoring interval to %llu\n",
892 bond->params.arp_interval = newval->value;
894 if (bond->params.miimon) {
895 netdev_info(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
896 bond->params.miimon = 0;
898 if (!bond->params.arp_targets[0])
899 netdev_info(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
901 if (bond->dev->flags & IFF_UP) {
902 /* If the interface is up, we may need to fire off
903 * the ARP timer. If the interface is down, the
904 * timer will get fired off when the open function
907 if (!newval->value) {
908 if (bond->params.arp_validate)
909 bond->recv_probe = NULL;
910 cancel_delayed_work_sync(&bond->arp_work);
912 /* arp_validate can be set only in active-backup mode */
913 bond->recv_probe = bond_arp_rcv;
914 cancel_delayed_work_sync(&bond->mii_work);
915 queue_delayed_work(bond->wq, &bond->arp_work, 0);
922 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
924 unsigned long last_rx)
926 __be32 *targets = bond->params.arp_targets;
927 struct list_head *iter;
930 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
931 bond_for_each_slave(bond, slave, iter)
932 slave->target_last_arp_rx[slot] = last_rx;
933 targets[slot] = target;
937 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
939 __be32 *targets = bond->params.arp_targets;
942 if (!bond_is_ip_target_ok(target)) {
943 netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
948 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
949 netdev_err(bond->dev, "ARP target %pI4 is already present\n",
954 ind = bond_get_targets_ip(targets, 0); /* first free slot */
956 netdev_err(bond->dev, "ARP target table is full!\n");
960 netdev_info(bond->dev, "Adding ARP target %pI4\n", &target);
962 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
967 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
969 return _bond_option_arp_ip_target_add(bond, target);
972 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
974 __be32 *targets = bond->params.arp_targets;
975 struct list_head *iter;
977 unsigned long *targets_rx;
980 if (!bond_is_ip_target_ok(target)) {
981 netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
986 ind = bond_get_targets_ip(targets, target);
988 netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
993 if (ind == 0 && !targets[1] && bond->params.arp_interval)
994 netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
996 netdev_info(bond->dev, "Removing ARP target %pI4\n", &target);
998 bond_for_each_slave(bond, slave, iter) {
999 targets_rx = slave->target_last_arp_rx;
1000 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1001 targets_rx[i] = targets_rx[i+1];
1004 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1005 targets[i] = targets[i+1];
1011 void bond_option_arp_ip_targets_clear(struct bonding *bond)
1015 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1016 _bond_options_arp_ip_target_set(bond, i, 0, 0);
1019 static int bond_option_arp_ip_targets_set(struct bonding *bond,
1020 const struct bond_opt_value *newval)
1025 if (newval->string) {
1026 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1027 netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
1031 if (newval->string[0] == '+')
1032 ret = bond_option_arp_ip_target_add(bond, target);
1033 else if (newval->string[0] == '-')
1034 ret = bond_option_arp_ip_target_rem(bond, target);
1036 netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
1038 target = newval->value;
1039 ret = bond_option_arp_ip_target_add(bond, target);
1045 static int bond_option_arp_validate_set(struct bonding *bond,
1046 const struct bond_opt_value *newval)
1048 netdev_info(bond->dev, "Setting arp_validate to %s (%llu)\n",
1049 newval->string, newval->value);
1051 if (bond->dev->flags & IFF_UP) {
1053 bond->recv_probe = NULL;
1054 else if (bond->params.arp_interval)
1055 bond->recv_probe = bond_arp_rcv;
1057 bond->params.arp_validate = newval->value;
1062 static int bond_option_arp_all_targets_set(struct bonding *bond,
1063 const struct bond_opt_value *newval)
1065 netdev_info(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
1066 newval->string, newval->value);
1067 bond->params.arp_all_targets = newval->value;
1072 static int bond_option_primary_set(struct bonding *bond,
1073 const struct bond_opt_value *newval)
1075 char *p, *primary = newval->string;
1076 struct list_head *iter;
1077 struct slave *slave;
1081 p = strchr(primary, '\n');
1084 /* check to see if we are clearing primary */
1085 if (!strlen(primary)) {
1086 netdev_info(bond->dev, "Setting primary slave to None\n");
1087 RCU_INIT_POINTER(bond->primary_slave, NULL);
1088 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1089 bond_select_active_slave(bond);
1093 bond_for_each_slave(bond, slave, iter) {
1094 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1095 netdev_info(bond->dev, "Setting %s as primary slave\n",
1097 rcu_assign_pointer(bond->primary_slave, slave);
1098 strcpy(bond->params.primary, slave->dev->name);
1099 bond_select_active_slave(bond);
1104 if (rtnl_dereference(bond->primary_slave)) {
1105 netdev_info(bond->dev, "Setting primary slave to None\n");
1106 RCU_INIT_POINTER(bond->primary_slave, NULL);
1107 bond_select_active_slave(bond);
1109 strncpy(bond->params.primary, primary, IFNAMSIZ);
1110 bond->params.primary[IFNAMSIZ - 1] = 0;
1112 netdev_info(bond->dev, "Recording %s as primary, but it has not been enslaved to %s yet\n",
1113 primary, bond->dev->name);
1116 unblock_netpoll_tx();
1121 static int bond_option_primary_reselect_set(struct bonding *bond,
1122 const struct bond_opt_value *newval)
1124 netdev_info(bond->dev, "Setting primary_reselect to %s (%llu)\n",
1125 newval->string, newval->value);
1126 bond->params.primary_reselect = newval->value;
1129 bond_select_active_slave(bond);
1130 unblock_netpoll_tx();
1135 static int bond_option_fail_over_mac_set(struct bonding *bond,
1136 const struct bond_opt_value *newval)
1138 netdev_info(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
1139 newval->string, newval->value);
1140 bond->params.fail_over_mac = newval->value;
1145 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1146 const struct bond_opt_value *newval)
1148 netdev_info(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
1149 newval->string, newval->value);
1150 bond->params.xmit_policy = newval->value;
1155 static int bond_option_resend_igmp_set(struct bonding *bond,
1156 const struct bond_opt_value *newval)
1158 netdev_info(bond->dev, "Setting resend_igmp to %llu\n",
1160 bond->params.resend_igmp = newval->value;
1165 static int bond_option_num_peer_notif_set(struct bonding *bond,
1166 const struct bond_opt_value *newval)
1168 bond->params.num_peer_notif = newval->value;
1173 static int bond_option_all_slaves_active_set(struct bonding *bond,
1174 const struct bond_opt_value *newval)
1176 struct list_head *iter;
1177 struct slave *slave;
1179 if (newval->value == bond->params.all_slaves_active)
1181 bond->params.all_slaves_active = newval->value;
1182 bond_for_each_slave(bond, slave, iter) {
1183 if (!bond_is_active_slave(slave)) {
1185 slave->inactive = 0;
1187 slave->inactive = 1;
1194 static int bond_option_min_links_set(struct bonding *bond,
1195 const struct bond_opt_value *newval)
1197 netdev_info(bond->dev, "Setting min links value to %llu\n",
1199 bond->params.min_links = newval->value;
1200 bond_set_carrier(bond);
1205 static int bond_option_lp_interval_set(struct bonding *bond,
1206 const struct bond_opt_value *newval)
1208 bond->params.lp_interval = newval->value;
1213 static int bond_option_pps_set(struct bonding *bond,
1214 const struct bond_opt_value *newval)
1216 bond->params.packets_per_slave = newval->value;
1217 if (newval->value > 0) {
1218 bond->params.reciprocal_packets_per_slave =
1219 reciprocal_value(newval->value);
1221 /* reciprocal_packets_per_slave is unused if
1222 * packets_per_slave is 0 or 1, just initialize it
1224 bond->params.reciprocal_packets_per_slave =
1225 (struct reciprocal_value) { 0 };
1231 static int bond_option_lacp_rate_set(struct bonding *bond,
1232 const struct bond_opt_value *newval)
1234 netdev_info(bond->dev, "Setting LACP rate to %s (%llu)\n",
1235 newval->string, newval->value);
1236 bond->params.lacp_fast = newval->value;
1237 bond_3ad_update_lacp_rate(bond);
1242 static int bond_option_ad_select_set(struct bonding *bond,
1243 const struct bond_opt_value *newval)
1245 netdev_info(bond->dev, "Setting ad_select to %s (%llu)\n",
1246 newval->string, newval->value);
1247 bond->params.ad_select = newval->value;
1252 static int bond_option_queue_id_set(struct bonding *bond,
1253 const struct bond_opt_value *newval)
1255 struct slave *slave, *update_slave;
1256 struct net_device *sdev;
1257 struct list_head *iter;
1262 /* delim will point to queue id if successful */
1263 delim = strchr(newval->string, ':');
1267 /* Terminate string that points to device name and bump it
1268 * up one, so we can read the queue id there.
1271 if (sscanf(++delim, "%hd\n", &qid) != 1)
1274 /* Check buffer length, valid ifname and queue id */
1275 if (!dev_valid_name(newval->string) ||
1276 qid > bond->dev->real_num_tx_queues)
1279 /* Get the pointer to that interface if it exists */
1280 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1284 /* Search for thes slave and check for duplicate qids */
1285 update_slave = NULL;
1286 bond_for_each_slave(bond, slave, iter) {
1287 if (sdev == slave->dev)
1288 /* We don't need to check the matching
1289 * slave for dups, since we're overwriting it
1291 update_slave = slave;
1292 else if (qid && qid == slave->queue_id) {
1300 /* Actually set the qids for the slave */
1301 update_slave->queue_id = qid;
1307 netdev_info(bond->dev, "invalid input for queue_id set\n");
1313 static int bond_option_slaves_set(struct bonding *bond,
1314 const struct bond_opt_value *newval)
1316 char command[IFNAMSIZ + 1] = { 0, };
1317 struct net_device *dev;
1321 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1322 ifname = command + 1;
1323 if ((strlen(command) <= 1) ||
1324 !dev_valid_name(ifname))
1327 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1329 netdev_info(bond->dev, "interface %s does not exist!\n",
1335 switch (command[0]) {
1337 netdev_info(bond->dev, "Adding slave %s\n", dev->name);
1338 ret = bond_enslave(bond->dev, dev);
1342 netdev_info(bond->dev, "Removing slave %s\n", dev->name);
1343 ret = bond_release(bond->dev, dev);
1354 netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
1359 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1360 const struct bond_opt_value *newval)
1362 netdev_info(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
1363 newval->string, newval->value);
1364 bond->params.tlb_dynamic_lb = newval->value;
1369 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
1370 const struct bond_opt_value *newval)
1372 netdev_info(bond->dev, "Setting ad_actor_sys_prio to (%llu)\n",
1375 bond->params.ad_actor_sys_prio = newval->value;