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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/errno.h>
16 #include <linux/netdevice.h>
17 #include <linux/spinlock.h>
18 #include <linux/rcupdate.h>
19 #include <linux/ctype.h>
20 #include <linux/inet.h>
23 static int bond_option_active_slave_set(struct bonding *bond,
24 const struct bond_opt_value *newval);
25 static int bond_option_miimon_set(struct bonding *bond,
26 const struct bond_opt_value *newval);
27 static int bond_option_updelay_set(struct bonding *bond,
28 const struct bond_opt_value *newval);
29 static int bond_option_downdelay_set(struct bonding *bond,
30 const struct bond_opt_value *newval);
31 static int bond_option_use_carrier_set(struct bonding *bond,
32 const struct bond_opt_value *newval);
33 static int bond_option_arp_interval_set(struct bonding *bond,
34 const struct bond_opt_value *newval);
35 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
36 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
37 static int bond_option_arp_ip_targets_set(struct bonding *bond,
38 const struct bond_opt_value *newval);
39 static int bond_option_arp_validate_set(struct bonding *bond,
40 const struct bond_opt_value *newval);
41 static int bond_option_arp_all_targets_set(struct bonding *bond,
42 const struct bond_opt_value *newval);
43 static int bond_option_primary_set(struct bonding *bond,
44 const struct bond_opt_value *newval);
45 static int bond_option_primary_reselect_set(struct bonding *bond,
46 const struct bond_opt_value *newval);
47 static int bond_option_fail_over_mac_set(struct bonding *bond,
48 const struct bond_opt_value *newval);
49 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
50 const struct bond_opt_value *newval);
51 static int bond_option_resend_igmp_set(struct bonding *bond,
52 const struct bond_opt_value *newval);
53 static int bond_option_num_peer_notif_set(struct bonding *bond,
54 const struct bond_opt_value *newval);
55 static int bond_option_all_slaves_active_set(struct bonding *bond,
56 const struct bond_opt_value *newval);
57 static int bond_option_min_links_set(struct bonding *bond,
58 const struct bond_opt_value *newval);
59 static int bond_option_lp_interval_set(struct bonding *bond,
60 const struct bond_opt_value *newval);
61 static int bond_option_pps_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);
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_option bond_opts[] = {
195 .desc = "bond device mode",
196 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
197 .values = bond_mode_tbl,
198 .set = bond_option_mode_set
200 [BOND_OPT_PACKETS_PER_SLAVE] = {
201 .id = BOND_OPT_PACKETS_PER_SLAVE,
202 .name = "packets_per_slave",
203 .desc = "Packets to send per slave in RR mode",
204 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
205 .values = bond_pps_tbl,
206 .set = bond_option_pps_set
208 [BOND_OPT_XMIT_HASH] = {
209 .id = BOND_OPT_XMIT_HASH,
210 .name = "xmit_hash_policy",
211 .desc = "balance-xor, 802.3ad, and tlb hashing method",
212 .values = bond_xmit_hashtype_tbl,
213 .set = bond_option_xmit_hash_policy_set
215 [BOND_OPT_ARP_VALIDATE] = {
216 .id = BOND_OPT_ARP_VALIDATE,
217 .name = "arp_validate",
218 .desc = "validate src/dst of ARP probes",
219 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
221 .values = bond_arp_validate_tbl,
222 .set = bond_option_arp_validate_set
224 [BOND_OPT_ARP_ALL_TARGETS] = {
225 .id = BOND_OPT_ARP_ALL_TARGETS,
226 .name = "arp_all_targets",
227 .desc = "fail on any/all arp targets timeout",
228 .values = bond_arp_all_targets_tbl,
229 .set = bond_option_arp_all_targets_set
231 [BOND_OPT_FAIL_OVER_MAC] = {
232 .id = BOND_OPT_FAIL_OVER_MAC,
233 .name = "fail_over_mac",
234 .desc = "For active-backup, do not set all slaves to the same MAC",
235 .flags = BOND_OPTFLAG_NOSLAVES,
236 .values = bond_fail_over_mac_tbl,
237 .set = bond_option_fail_over_mac_set
239 [BOND_OPT_ARP_INTERVAL] = {
240 .id = BOND_OPT_ARP_INTERVAL,
241 .name = "arp_interval",
242 .desc = "arp interval in milliseconds",
243 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
245 .values = bond_intmax_tbl,
246 .set = bond_option_arp_interval_set
248 [BOND_OPT_ARP_TARGETS] = {
249 .id = BOND_OPT_ARP_TARGETS,
250 .name = "arp_ip_target",
251 .desc = "arp targets in n.n.n.n form",
252 .flags = BOND_OPTFLAG_RAWVAL,
253 .set = bond_option_arp_ip_targets_set
255 [BOND_OPT_DOWNDELAY] = {
256 .id = BOND_OPT_DOWNDELAY,
258 .desc = "Delay before considering link down, in milliseconds",
259 .values = bond_intmax_tbl,
260 .set = bond_option_downdelay_set
262 [BOND_OPT_UPDELAY] = {
263 .id = BOND_OPT_UPDELAY,
265 .desc = "Delay before considering link up, in milliseconds",
266 .values = bond_intmax_tbl,
267 .set = bond_option_updelay_set
269 [BOND_OPT_LACP_RATE] = {
270 .id = BOND_OPT_LACP_RATE,
272 .desc = "LACPDU tx rate to request from 802.3ad partner",
273 .flags = BOND_OPTFLAG_IFDOWN,
274 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
275 .values = bond_lacp_rate_tbl,
276 .set = bond_option_lacp_rate_set
278 [BOND_OPT_MINLINKS] = {
279 .id = BOND_OPT_MINLINKS,
281 .desc = "Minimum number of available links before turning on carrier",
282 .values = bond_intmax_tbl,
283 .set = bond_option_min_links_set
285 [BOND_OPT_AD_SELECT] = {
286 .id = BOND_OPT_AD_SELECT,
288 .desc = "803.ad aggregation selection logic",
289 .flags = BOND_OPTFLAG_IFDOWN,
290 .values = bond_ad_select_tbl,
291 .set = bond_option_ad_select_set
293 [BOND_OPT_NUM_PEER_NOTIF] = {
294 .id = BOND_OPT_NUM_PEER_NOTIF,
295 .name = "num_unsol_na",
296 .desc = "Number of peer notifications to send on failover event",
297 .values = bond_num_peer_notif_tbl,
298 .set = bond_option_num_peer_notif_set
300 [BOND_OPT_MIIMON] = {
301 .id = BOND_OPT_MIIMON,
303 .desc = "Link check interval in milliseconds",
304 .values = bond_intmax_tbl,
305 .set = bond_option_miimon_set
307 [BOND_OPT_PRIMARY] = {
308 .id = BOND_OPT_PRIMARY,
310 .desc = "Primary network device to use",
311 .flags = BOND_OPTFLAG_RAWVAL,
312 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
315 .set = bond_option_primary_set
317 [BOND_OPT_PRIMARY_RESELECT] = {
318 .id = BOND_OPT_PRIMARY_RESELECT,
319 .name = "primary_reselect",
320 .desc = "Reselect primary slave once it comes up",
321 .values = bond_primary_reselect_tbl,
322 .set = bond_option_primary_reselect_set
324 [BOND_OPT_USE_CARRIER] = {
325 .id = BOND_OPT_USE_CARRIER,
326 .name = "use_carrier",
327 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
328 .values = bond_use_carrier_tbl,
329 .set = bond_option_use_carrier_set
331 [BOND_OPT_ACTIVE_SLAVE] = {
332 .id = BOND_OPT_ACTIVE_SLAVE,
333 .name = "active_slave",
334 .desc = "Currently active slave",
335 .flags = BOND_OPTFLAG_RAWVAL,
336 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
339 .set = bond_option_active_slave_set
341 [BOND_OPT_QUEUE_ID] = {
342 .id = BOND_OPT_QUEUE_ID,
344 .desc = "Set queue id of a slave",
345 .flags = BOND_OPTFLAG_RAWVAL,
346 .set = bond_option_queue_id_set
348 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
349 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
350 .name = "all_slaves_active",
351 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
352 .values = bond_all_slaves_active_tbl,
353 .set = bond_option_all_slaves_active_set
355 [BOND_OPT_RESEND_IGMP] = {
356 .id = BOND_OPT_RESEND_IGMP,
357 .name = "resend_igmp",
358 .desc = "Number of IGMP membership reports to send on link failure",
359 .values = bond_resend_igmp_tbl,
360 .set = bond_option_resend_igmp_set
362 [BOND_OPT_LP_INTERVAL] = {
363 .id = BOND_OPT_LP_INTERVAL,
364 .name = "lp_interval",
365 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
366 .values = bond_lp_interval_tbl,
367 .set = bond_option_lp_interval_set
369 [BOND_OPT_SLAVES] = {
370 .id = BOND_OPT_SLAVES,
372 .desc = "Slave membership management",
373 .flags = BOND_OPTFLAG_RAWVAL,
374 .set = bond_option_slaves_set
376 [BOND_OPT_TLB_DYNAMIC_LB] = {
377 .id = BOND_OPT_TLB_DYNAMIC_LB,
378 .name = "tlb_dynamic_lb",
379 .desc = "Enable dynamic flow shuffling",
380 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB)),
381 .values = bond_tlb_dynamic_lb_tbl,
382 .flags = BOND_OPTFLAG_IFDOWN,
383 .set = bond_option_tlb_dynamic_lb_set,
388 /* Searches for an option by name */
389 const struct bond_option *bond_opt_get_by_name(const char *name)
391 const struct bond_option *opt;
394 for (option = 0; option < BOND_OPT_LAST; option++) {
395 opt = bond_opt_get(option);
396 if (opt && !strcmp(opt->name, name))
403 /* Searches for a value in opt's values[] table */
404 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
406 const struct bond_option *opt;
409 opt = bond_opt_get(option);
412 for (i = 0; opt->values && opt->values[i].string; i++)
413 if (opt->values[i].value == val)
414 return &opt->values[i];
419 /* Searches for a value in opt's values[] table which matches the flagmask */
420 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
425 for (i = 0; opt->values && opt->values[i].string; i++)
426 if (opt->values[i].flags & flagmask)
427 return &opt->values[i];
432 /* If maxval is missing then there's no range to check. In case minval is
433 * missing then it's considered to be 0.
435 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
437 const struct bond_opt_value *minval, *maxval;
439 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
440 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
441 if (!maxval || (minval && val < minval->value) || val > maxval->value)
448 * bond_opt_parse - parse option value
449 * @opt: the option to parse against
450 * @val: value to parse
452 * This function tries to extract the value from @val and check if it's
453 * a possible match for the option and returns NULL if a match isn't found,
454 * or the struct_opt_value that matched. It also strips the new line from
455 * @val->string if it's present.
457 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
458 struct bond_opt_value *val)
460 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
461 const struct bond_opt_value *tbl;
462 const struct bond_opt_value *ret = NULL;
466 /* No parsing if the option wants a raw val */
467 if (opt->flags & BOND_OPTFLAG_RAWVAL)
474 /* ULLONG_MAX is used to bypass string processing */
475 checkval = val->value != ULLONG_MAX;
479 p = strchr(val->string, '\n');
482 for (p = val->string; *p; p++)
483 if (!(isdigit(*p) || isspace(*p)))
485 /* The following code extracts the string to match or the value
486 * and sets checkval appropriately
489 rv = sscanf(val->string, "%32s", valstr);
491 rv = sscanf(val->string, "%llu", &val->value);
498 for (i = 0; tbl[i].string; i++) {
499 /* Check for exact match */
501 if (val->value == tbl[i].value)
504 if (!strcmp(valstr, "default") &&
505 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
508 if (!strcmp(valstr, tbl[i].string))
511 /* Found an exact match */
515 /* Possible range match */
516 if (checkval && bond_opt_check_range(opt, val->value))
522 /* Check opt's dependencies against bond mode and currently set options */
523 static int bond_opt_check_deps(struct bonding *bond,
524 const struct bond_option *opt)
526 struct bond_params *params = &bond->params;
528 if (test_bit(params->mode, &opt->unsuppmodes))
530 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
532 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
538 static void bond_opt_dep_print(struct bonding *bond,
539 const struct bond_option *opt)
541 const struct bond_opt_value *modeval;
542 struct bond_params *params;
544 params = &bond->params;
545 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
546 if (test_bit(params->mode, &opt->unsuppmodes))
547 pr_err("%s: option %s: mode dependency failed, not supported in mode %s(%llu)\n",
548 bond->dev->name, opt->name,
549 modeval->string, modeval->value);
552 static void bond_opt_error_interpret(struct bonding *bond,
553 const struct bond_option *opt,
554 int error, const struct bond_opt_value *val)
556 const struct bond_opt_value *minval, *maxval;
563 /* sometimes RAWVAL opts may have new lines */
564 p = strchr(val->string, '\n');
567 pr_err("%s: option %s: invalid value (%s)\n",
568 bond->dev->name, opt->name, val->string);
570 pr_err("%s: option %s: invalid value (%llu)\n",
571 bond->dev->name, opt->name, val->value);
574 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
575 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
578 pr_err("%s: option %s: allowed values %llu - %llu\n",
579 bond->dev->name, opt->name, minval ? minval->value : 0,
583 bond_opt_dep_print(bond, opt);
586 pr_err("%s: option %s: unable to set because the bond device has slaves\n",
587 bond->dev->name, opt->name);
590 pr_err("%s: option %s: unable to set because the bond device is up\n",
591 bond->dev->name, opt->name);
599 * __bond_opt_set - set a bonding option
600 * @bond: target bond device
601 * @option: option to set
602 * @val: value to set it to
604 * This function is used to change the bond's option value, it can be
605 * used for both enabling/changing an option and for disabling it. RTNL lock
606 * must be obtained before calling this function.
608 int __bond_opt_set(struct bonding *bond,
609 unsigned int option, struct bond_opt_value *val)
611 const struct bond_opt_value *retval = NULL;
612 const struct bond_option *opt;
617 opt = bond_opt_get(option);
618 if (WARN_ON(!val) || WARN_ON(!opt))
620 ret = bond_opt_check_deps(bond, opt);
623 retval = bond_opt_parse(opt, val);
628 ret = opt->set(bond, retval);
631 bond_opt_error_interpret(bond, opt, ret, val);
637 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
638 * @bond: target bond device
639 * @option: option to set
640 * @buf: value to set it to
642 * This function tries to acquire RTNL without blocking and if successful
643 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
645 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
647 struct bond_opt_value optval;
651 return restart_syscall();
652 bond_opt_initstr(&optval, buf);
653 ret = __bond_opt_set(bond, option, &optval);
660 * bond_opt_get - get a pointer to an option
661 * @option: option for which to return a pointer
663 * This function checks if option is valid and if so returns a pointer
664 * to its entry in the bond_opts[] option array.
666 const struct bond_option *bond_opt_get(unsigned int option)
668 if (!BOND_OPT_VALID(option))
671 return &bond_opts[option];
674 int bond_option_mode_set(struct bonding *bond, const struct bond_opt_value *newval)
676 if (!bond_mode_uses_arp(newval->value) && bond->params.arp_interval) {
677 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
678 bond->dev->name, newval->string);
679 /* disable arp monitoring */
680 bond->params.arp_interval = 0;
681 /* set miimon to default value */
682 bond->params.miimon = BOND_DEFAULT_MIIMON;
683 pr_info("%s: Setting MII monitoring interval to %d\n",
684 bond->dev->name, bond->params.miimon);
687 /* don't cache arp_validate between modes */
688 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
689 bond->params.mode = newval->value;
694 static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
697 return bond_uses_primary(bond) && slave ? slave->dev : NULL;
700 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
702 struct slave *slave = rcu_dereference(bond->curr_active_slave);
704 return __bond_option_active_slave_get(bond, slave);
707 struct net_device *bond_option_active_slave_get(struct bonding *bond)
709 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
712 static int bond_option_active_slave_set(struct bonding *bond,
713 const struct bond_opt_value *newval)
715 char ifname[IFNAMSIZ] = { 0, };
716 struct net_device *slave_dev;
719 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
720 if (!strlen(ifname) || newval->string[0] == '\n') {
723 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
729 if (!netif_is_bond_slave(slave_dev)) {
730 pr_err("Device %s is not bonding slave\n",
735 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
736 pr_err("%s: Device %s is not our slave\n",
737 bond->dev->name, slave_dev->name);
743 write_lock_bh(&bond->curr_slave_lock);
745 /* check to see if we are clearing active */
747 pr_info("%s: Clearing current active slave\n", bond->dev->name);
748 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
749 bond_select_active_slave(bond);
751 struct slave *old_active = bond->curr_active_slave;
752 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
756 if (new_active == old_active) {
758 pr_info("%s: %s is already the current active slave\n",
759 bond->dev->name, new_active->dev->name);
761 if (old_active && (new_active->link == BOND_LINK_UP) &&
762 bond_slave_is_up(new_active)) {
763 pr_info("%s: Setting %s as active slave\n",
764 bond->dev->name, new_active->dev->name);
765 bond_change_active_slave(bond, new_active);
767 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down\n",
768 bond->dev->name, new_active->dev->name,
769 new_active->dev->name);
775 write_unlock_bh(&bond->curr_slave_lock);
776 unblock_netpoll_tx();
781 /* There are two tricky bits here. First, if MII monitoring is activated, then
782 * we must disable ARP monitoring. Second, if the timer isn't running, we must
785 static int bond_option_miimon_set(struct bonding *bond,
786 const struct bond_opt_value *newval)
788 pr_info("%s: Setting MII monitoring interval to %llu\n",
789 bond->dev->name, newval->value);
790 bond->params.miimon = newval->value;
791 if (bond->params.updelay)
792 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
794 bond->params.updelay * bond->params.miimon);
795 if (bond->params.downdelay)
796 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
798 bond->params.downdelay * bond->params.miimon);
799 if (newval->value && bond->params.arp_interval) {
800 pr_info("%s: MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n",
802 bond->params.arp_interval = 0;
803 if (bond->params.arp_validate)
804 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
806 if (bond->dev->flags & IFF_UP) {
807 /* If the interface is up, we may need to fire off
808 * the MII timer. If the interface is down, the
809 * timer will get fired off when the open function
812 if (!newval->value) {
813 cancel_delayed_work_sync(&bond->mii_work);
815 cancel_delayed_work_sync(&bond->arp_work);
816 queue_delayed_work(bond->wq, &bond->mii_work, 0);
823 /* Set up and down delays. These must be multiples of the
824 * MII monitoring value, and are stored internally as the multiplier.
825 * Thus, we must translate to MS for the real world.
827 static int bond_option_updelay_set(struct bonding *bond,
828 const struct bond_opt_value *newval)
830 int value = newval->value;
832 if (!bond->params.miimon) {
833 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
837 if ((value % bond->params.miimon) != 0) {
838 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
839 bond->dev->name, value,
841 (value / bond->params.miimon) *
842 bond->params.miimon);
844 bond->params.updelay = value / bond->params.miimon;
845 pr_info("%s: Setting up delay to %d\n",
846 bond->dev->name, 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 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
861 if ((value % bond->params.miimon) != 0) {
862 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
863 bond->dev->name, value,
865 (value / bond->params.miimon) *
866 bond->params.miimon);
868 bond->params.downdelay = value / bond->params.miimon;
869 pr_info("%s: Setting down delay to %d\n",
870 bond->dev->name, bond->params.downdelay * bond->params.miimon);
875 static int bond_option_use_carrier_set(struct bonding *bond,
876 const struct bond_opt_value *newval)
878 pr_info("%s: Setting use_carrier to %llu\n",
879 bond->dev->name, newval->value);
880 bond->params.use_carrier = newval->value;
885 /* There are two tricky bits here. First, if ARP monitoring is activated, then
886 * we must disable MII monitoring. Second, if the ARP timer isn't running,
889 static int bond_option_arp_interval_set(struct bonding *bond,
890 const struct bond_opt_value *newval)
892 pr_info("%s: Setting ARP monitoring interval to %llu\n",
893 bond->dev->name, newval->value);
894 bond->params.arp_interval = newval->value;
896 if (bond->params.miimon) {
897 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring\n",
898 bond->dev->name, bond->dev->name);
899 bond->params.miimon = 0;
901 if (!bond->params.arp_targets[0])
902 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified\n",
905 if (bond->dev->flags & IFF_UP) {
906 /* If the interface is up, we may need to fire off
907 * the ARP timer. If the interface is down, the
908 * timer will get fired off when the open function
911 if (!newval->value) {
912 if (bond->params.arp_validate)
913 bond->recv_probe = NULL;
914 cancel_delayed_work_sync(&bond->arp_work);
916 /* arp_validate can be set only in active-backup mode */
917 bond->recv_probe = bond_arp_rcv;
918 cancel_delayed_work_sync(&bond->mii_work);
919 queue_delayed_work(bond->wq, &bond->arp_work, 0);
926 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
928 unsigned long last_rx)
930 __be32 *targets = bond->params.arp_targets;
931 struct list_head *iter;
934 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
935 bond_for_each_slave(bond, slave, iter)
936 slave->target_last_arp_rx[slot] = last_rx;
937 targets[slot] = target;
941 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
943 __be32 *targets = bond->params.arp_targets;
946 if (!bond_is_ip_target_ok(target)) {
947 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
948 bond->dev->name, &target);
952 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
953 pr_err("%s: ARP target %pI4 is already present\n",
954 bond->dev->name, &target);
958 ind = bond_get_targets_ip(targets, 0); /* first free slot */
960 pr_err("%s: ARP target table is full!\n", bond->dev->name);
964 pr_info("%s: Adding ARP target %pI4\n", bond->dev->name, &target);
966 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
971 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
975 /* not to race with bond_arp_rcv */
976 write_lock_bh(&bond->lock);
977 ret = _bond_option_arp_ip_target_add(bond, target);
978 write_unlock_bh(&bond->lock);
983 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
985 __be32 *targets = bond->params.arp_targets;
986 struct list_head *iter;
988 unsigned long *targets_rx;
991 if (!bond_is_ip_target_ok(target)) {
992 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
993 bond->dev->name, &target);
997 ind = bond_get_targets_ip(targets, target);
999 pr_err("%s: unable to remove nonexistent ARP target %pI4\n",
1000 bond->dev->name, &target);
1004 if (ind == 0 && !targets[1] && bond->params.arp_interval)
1005 pr_warn("%s: Removing last arp target with arp_interval on\n",
1008 pr_info("%s: Removing ARP target %pI4\n", bond->dev->name, &target);
1010 /* not to race with bond_arp_rcv */
1011 write_lock_bh(&bond->lock);
1013 bond_for_each_slave(bond, slave, iter) {
1014 targets_rx = slave->target_last_arp_rx;
1015 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1016 targets_rx[i] = targets_rx[i+1];
1019 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1020 targets[i] = targets[i+1];
1023 write_unlock_bh(&bond->lock);
1028 void bond_option_arp_ip_targets_clear(struct bonding *bond)
1032 /* not to race with bond_arp_rcv */
1033 write_lock_bh(&bond->lock);
1034 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1035 _bond_options_arp_ip_target_set(bond, i, 0, 0);
1036 write_unlock_bh(&bond->lock);
1039 static int bond_option_arp_ip_targets_set(struct bonding *bond,
1040 const struct bond_opt_value *newval)
1045 if (newval->string) {
1046 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1047 pr_err("%s: invalid ARP target %pI4 specified\n",
1048 bond->dev->name, &target);
1051 if (newval->string[0] == '+')
1052 ret = bond_option_arp_ip_target_add(bond, target);
1053 else if (newval->string[0] == '-')
1054 ret = bond_option_arp_ip_target_rem(bond, target);
1056 pr_err("no command found in arp_ip_targets file for bond %s - use +<addr> or -<addr>\n",
1059 target = newval->value;
1060 ret = bond_option_arp_ip_target_add(bond, target);
1066 static int bond_option_arp_validate_set(struct bonding *bond,
1067 const struct bond_opt_value *newval)
1069 pr_info("%s: Setting arp_validate to %s (%llu)\n",
1070 bond->dev->name, newval->string, newval->value);
1072 if (bond->dev->flags & IFF_UP) {
1074 bond->recv_probe = NULL;
1075 else if (bond->params.arp_interval)
1076 bond->recv_probe = bond_arp_rcv;
1078 bond->params.arp_validate = newval->value;
1083 static int bond_option_arp_all_targets_set(struct bonding *bond,
1084 const struct bond_opt_value *newval)
1086 pr_info("%s: Setting arp_all_targets to %s (%llu)\n",
1087 bond->dev->name, newval->string, newval->value);
1088 bond->params.arp_all_targets = newval->value;
1093 static int bond_option_primary_set(struct bonding *bond,
1094 const struct bond_opt_value *newval)
1096 char *p, *primary = newval->string;
1097 struct list_head *iter;
1098 struct slave *slave;
1101 read_lock(&bond->lock);
1102 write_lock_bh(&bond->curr_slave_lock);
1104 p = strchr(primary, '\n');
1107 /* check to see if we are clearing primary */
1108 if (!strlen(primary)) {
1109 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
1110 bond->primary_slave = NULL;
1111 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1112 bond_select_active_slave(bond);
1116 bond_for_each_slave(bond, slave, iter) {
1117 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1118 pr_info("%s: Setting %s as primary slave\n",
1119 bond->dev->name, slave->dev->name);
1120 bond->primary_slave = slave;
1121 strcpy(bond->params.primary, slave->dev->name);
1122 bond_select_active_slave(bond);
1127 if (bond->primary_slave) {
1128 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
1129 bond->primary_slave = NULL;
1130 bond_select_active_slave(bond);
1132 strncpy(bond->params.primary, primary, IFNAMSIZ);
1133 bond->params.primary[IFNAMSIZ - 1] = 0;
1135 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet\n",
1136 bond->dev->name, primary, bond->dev->name);
1139 write_unlock_bh(&bond->curr_slave_lock);
1140 read_unlock(&bond->lock);
1141 unblock_netpoll_tx();
1146 static int bond_option_primary_reselect_set(struct bonding *bond,
1147 const struct bond_opt_value *newval)
1149 pr_info("%s: Setting primary_reselect to %s (%llu)\n",
1150 bond->dev->name, newval->string, newval->value);
1151 bond->params.primary_reselect = newval->value;
1154 write_lock_bh(&bond->curr_slave_lock);
1155 bond_select_active_slave(bond);
1156 write_unlock_bh(&bond->curr_slave_lock);
1157 unblock_netpoll_tx();
1162 static int bond_option_fail_over_mac_set(struct bonding *bond,
1163 const struct bond_opt_value *newval)
1165 pr_info("%s: Setting fail_over_mac to %s (%llu)\n",
1166 bond->dev->name, newval->string, newval->value);
1167 bond->params.fail_over_mac = newval->value;
1172 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1173 const struct bond_opt_value *newval)
1175 pr_info("%s: Setting xmit hash policy to %s (%llu)\n",
1176 bond->dev->name, newval->string, newval->value);
1177 bond->params.xmit_policy = newval->value;
1182 static int bond_option_resend_igmp_set(struct bonding *bond,
1183 const struct bond_opt_value *newval)
1185 pr_info("%s: Setting resend_igmp to %llu\n",
1186 bond->dev->name, newval->value);
1187 bond->params.resend_igmp = newval->value;
1192 static int bond_option_num_peer_notif_set(struct bonding *bond,
1193 const struct bond_opt_value *newval)
1195 bond->params.num_peer_notif = newval->value;
1200 static int bond_option_all_slaves_active_set(struct bonding *bond,
1201 const struct bond_opt_value *newval)
1203 struct list_head *iter;
1204 struct slave *slave;
1206 if (newval->value == bond->params.all_slaves_active)
1208 bond->params.all_slaves_active = newval->value;
1209 bond_for_each_slave(bond, slave, iter) {
1210 if (!bond_is_active_slave(slave)) {
1212 slave->inactive = 0;
1214 slave->inactive = 1;
1221 static int bond_option_min_links_set(struct bonding *bond,
1222 const struct bond_opt_value *newval)
1224 pr_info("%s: Setting min links value to %llu\n",
1225 bond->dev->name, newval->value);
1226 bond->params.min_links = newval->value;
1231 static int bond_option_lp_interval_set(struct bonding *bond,
1232 const struct bond_opt_value *newval)
1234 bond->params.lp_interval = newval->value;
1239 static int bond_option_pps_set(struct bonding *bond,
1240 const struct bond_opt_value *newval)
1242 bond->params.packets_per_slave = newval->value;
1243 if (newval->value > 0) {
1244 bond->params.reciprocal_packets_per_slave =
1245 reciprocal_value(newval->value);
1247 /* reciprocal_packets_per_slave is unused if
1248 * packets_per_slave is 0 or 1, just initialize it
1250 bond->params.reciprocal_packets_per_slave =
1251 (struct reciprocal_value) { 0 };
1257 static int bond_option_lacp_rate_set(struct bonding *bond,
1258 const struct bond_opt_value *newval)
1260 pr_info("%s: Setting LACP rate to %s (%llu)\n",
1261 bond->dev->name, newval->string, newval->value);
1262 bond->params.lacp_fast = newval->value;
1263 bond_3ad_update_lacp_rate(bond);
1268 static int bond_option_ad_select_set(struct bonding *bond,
1269 const struct bond_opt_value *newval)
1271 pr_info("%s: Setting ad_select to %s (%llu)\n",
1272 bond->dev->name, newval->string, newval->value);
1273 bond->params.ad_select = newval->value;
1278 static int bond_option_queue_id_set(struct bonding *bond,
1279 const struct bond_opt_value *newval)
1281 struct slave *slave, *update_slave;
1282 struct net_device *sdev;
1283 struct list_head *iter;
1288 /* delim will point to queue id if successful */
1289 delim = strchr(newval->string, ':');
1293 /* Terminate string that points to device name and bump it
1294 * up one, so we can read the queue id there.
1297 if (sscanf(++delim, "%hd\n", &qid) != 1)
1300 /* Check buffer length, valid ifname and queue id */
1301 if (!dev_valid_name(newval->string) ||
1302 qid > bond->dev->real_num_tx_queues)
1305 /* Get the pointer to that interface if it exists */
1306 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1310 /* Search for thes slave and check for duplicate qids */
1311 update_slave = NULL;
1312 bond_for_each_slave(bond, slave, iter) {
1313 if (sdev == slave->dev)
1314 /* We don't need to check the matching
1315 * slave for dups, since we're overwriting it
1317 update_slave = slave;
1318 else if (qid && qid == slave->queue_id) {
1326 /* Actually set the qids for the slave */
1327 update_slave->queue_id = qid;
1333 pr_info("invalid input for queue_id set for %s\n", bond->dev->name);
1339 static int bond_option_slaves_set(struct bonding *bond,
1340 const struct bond_opt_value *newval)
1342 char command[IFNAMSIZ + 1] = { 0, };
1343 struct net_device *dev;
1347 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1348 ifname = command + 1;
1349 if ((strlen(command) <= 1) ||
1350 !dev_valid_name(ifname))
1353 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1355 pr_info("%s: interface %s does not exist!\n",
1356 bond->dev->name, ifname);
1361 switch (command[0]) {
1363 pr_info("%s: Adding slave %s\n", bond->dev->name, dev->name);
1364 ret = bond_enslave(bond->dev, dev);
1368 pr_info("%s: Removing slave %s\n", bond->dev->name, dev->name);
1369 ret = bond_release(bond->dev, dev);
1380 pr_err("no command found in slaves file for bond %s - use +ifname or -ifname\n",
1386 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1387 const struct bond_opt_value *newval)
1389 pr_info("%s: Setting dynamic-lb to %s (%llu)\n",
1390 bond->dev->name, newval->string, newval->value);
1391 bond->params.tlb_dynamic_lb = newval->value;