2 * drivers/net/bond/bond_options.c - bonding options
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);
75 static const struct bond_opt_value bond_mode_tbl[] = {
76 { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT},
77 { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
78 { "balance-xor", BOND_MODE_XOR, 0},
79 { "broadcast", BOND_MODE_BROADCAST, 0},
80 { "802.3ad", BOND_MODE_8023AD, 0},
81 { "balance-tlb", BOND_MODE_TLB, 0},
82 { "balance-alb", BOND_MODE_ALB, 0},
86 static const struct bond_opt_value bond_pps_tbl[] = {
87 { "default", 1, BOND_VALFLAG_DEFAULT},
88 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
92 static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
93 { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
94 { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
95 { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
96 { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
97 { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
101 static const struct bond_opt_value bond_arp_validate_tbl[] = {
102 { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT},
103 { "active", BOND_ARP_VALIDATE_ACTIVE, 0},
104 { "backup", BOND_ARP_VALIDATE_BACKUP, 0},
105 { "all", BOND_ARP_VALIDATE_ALL, 0},
106 { "filter", BOND_ARP_FILTER, 0},
107 { "filter_active", BOND_ARP_FILTER_ACTIVE, 0},
108 { "filter_backup", BOND_ARP_FILTER_BACKUP, 0},
112 static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
113 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
114 { "all", BOND_ARP_TARGETS_ALL, 0},
118 static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
119 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
120 { "active", BOND_FOM_ACTIVE, 0},
121 { "follow", BOND_FOM_FOLLOW, 0},
125 static const struct bond_opt_value bond_intmax_tbl[] = {
126 { "off", 0, BOND_VALFLAG_DEFAULT},
127 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
130 static const struct bond_opt_value bond_lacp_rate_tbl[] = {
131 { "slow", AD_LACP_SLOW, 0},
132 { "fast", AD_LACP_FAST, 0},
136 static const struct bond_opt_value bond_ad_select_tbl[] = {
137 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
138 { "bandwidth", BOND_AD_BANDWIDTH, 0},
139 { "count", BOND_AD_COUNT, 0},
143 static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
145 { "maxval", 255, BOND_VALFLAG_MAX},
146 { "default", 1, BOND_VALFLAG_DEFAULT},
150 static const struct bond_opt_value bond_primary_reselect_tbl[] = {
151 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
152 { "better", BOND_PRI_RESELECT_BETTER, 0},
153 { "failure", BOND_PRI_RESELECT_FAILURE, 0},
157 static const struct bond_opt_value bond_use_carrier_tbl[] = {
159 { "on", 1, BOND_VALFLAG_DEFAULT},
163 static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
164 { "off", 0, BOND_VALFLAG_DEFAULT},
169 static const struct bond_opt_value bond_resend_igmp_tbl[] = {
171 { "maxval", 255, BOND_VALFLAG_MAX},
172 { "default", 1, BOND_VALFLAG_DEFAULT},
176 static const struct bond_opt_value bond_lp_interval_tbl[] = {
177 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
178 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
182 static const struct bond_option bond_opts[] = {
186 .desc = "bond device mode",
187 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
188 .values = bond_mode_tbl,
189 .set = bond_option_mode_set
191 [BOND_OPT_PACKETS_PER_SLAVE] = {
192 .id = BOND_OPT_PACKETS_PER_SLAVE,
193 .name = "packets_per_slave",
194 .desc = "Packets to send per slave in RR mode",
195 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
196 .values = bond_pps_tbl,
197 .set = bond_option_pps_set
199 [BOND_OPT_XMIT_HASH] = {
200 .id = BOND_OPT_XMIT_HASH,
201 .name = "xmit_hash_policy",
202 .desc = "balance-xor and 802.3ad hashing method",
203 .values = bond_xmit_hashtype_tbl,
204 .set = bond_option_xmit_hash_policy_set
206 [BOND_OPT_ARP_VALIDATE] = {
207 .id = BOND_OPT_ARP_VALIDATE,
208 .name = "arp_validate",
209 .desc = "validate src/dst of ARP probes",
210 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
212 .values = bond_arp_validate_tbl,
213 .set = bond_option_arp_validate_set
215 [BOND_OPT_ARP_ALL_TARGETS] = {
216 .id = BOND_OPT_ARP_ALL_TARGETS,
217 .name = "arp_all_targets",
218 .desc = "fail on any/all arp targets timeout",
219 .values = bond_arp_all_targets_tbl,
220 .set = bond_option_arp_all_targets_set
222 [BOND_OPT_FAIL_OVER_MAC] = {
223 .id = BOND_OPT_FAIL_OVER_MAC,
224 .name = "fail_over_mac",
225 .desc = "For active-backup, do not set all slaves to the same MAC",
226 .flags = BOND_OPTFLAG_NOSLAVES,
227 .values = bond_fail_over_mac_tbl,
228 .set = bond_option_fail_over_mac_set
230 [BOND_OPT_ARP_INTERVAL] = {
231 .id = BOND_OPT_ARP_INTERVAL,
232 .name = "arp_interval",
233 .desc = "arp interval in milliseconds",
234 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
236 .values = bond_intmax_tbl,
237 .set = bond_option_arp_interval_set
239 [BOND_OPT_ARP_TARGETS] = {
240 .id = BOND_OPT_ARP_TARGETS,
241 .name = "arp_ip_target",
242 .desc = "arp targets in n.n.n.n form",
243 .flags = BOND_OPTFLAG_RAWVAL,
244 .set = bond_option_arp_ip_targets_set
246 [BOND_OPT_DOWNDELAY] = {
247 .id = BOND_OPT_DOWNDELAY,
249 .desc = "Delay before considering link down, in milliseconds",
250 .values = bond_intmax_tbl,
251 .set = bond_option_downdelay_set
253 [BOND_OPT_UPDELAY] = {
254 .id = BOND_OPT_UPDELAY,
256 .desc = "Delay before considering link up, in milliseconds",
257 .values = bond_intmax_tbl,
258 .set = bond_option_updelay_set
260 [BOND_OPT_LACP_RATE] = {
261 .id = BOND_OPT_LACP_RATE,
263 .desc = "LACPDU tx rate to request from 802.3ad partner",
264 .flags = BOND_OPTFLAG_IFDOWN,
265 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
266 .values = bond_lacp_rate_tbl,
267 .set = bond_option_lacp_rate_set
269 [BOND_OPT_MINLINKS] = {
270 .id = BOND_OPT_MINLINKS,
272 .desc = "Minimum number of available links before turning on carrier",
273 .values = bond_intmax_tbl,
274 .set = bond_option_min_links_set
276 [BOND_OPT_AD_SELECT] = {
277 .id = BOND_OPT_AD_SELECT,
279 .desc = "803.ad aggregation selection logic",
280 .flags = BOND_OPTFLAG_IFDOWN,
281 .values = bond_ad_select_tbl,
282 .set = bond_option_ad_select_set
284 [BOND_OPT_NUM_PEER_NOTIF] = {
285 .id = BOND_OPT_NUM_PEER_NOTIF,
286 .name = "num_unsol_na",
287 .desc = "Number of peer notifications to send on failover event",
288 .values = bond_num_peer_notif_tbl,
289 .set = bond_option_num_peer_notif_set
291 [BOND_OPT_MIIMON] = {
292 .id = BOND_OPT_MIIMON,
294 .desc = "Link check interval in milliseconds",
295 .values = bond_intmax_tbl,
296 .set = bond_option_miimon_set
298 [BOND_OPT_PRIMARY] = {
299 .id = BOND_OPT_PRIMARY,
301 .desc = "Primary network device to use",
302 .flags = BOND_OPTFLAG_RAWVAL,
303 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
306 .set = bond_option_primary_set
308 [BOND_OPT_PRIMARY_RESELECT] = {
309 .id = BOND_OPT_PRIMARY_RESELECT,
310 .name = "primary_reselect",
311 .desc = "Reselect primary slave once it comes up",
312 .values = bond_primary_reselect_tbl,
313 .set = bond_option_primary_reselect_set
315 [BOND_OPT_USE_CARRIER] = {
316 .id = BOND_OPT_USE_CARRIER,
317 .name = "use_carrier",
318 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
319 .values = bond_use_carrier_tbl,
320 .set = bond_option_use_carrier_set
322 [BOND_OPT_ACTIVE_SLAVE] = {
323 .id = BOND_OPT_ACTIVE_SLAVE,
324 .name = "active_slave",
325 .desc = "Currently active slave",
326 .flags = BOND_OPTFLAG_RAWVAL,
327 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
330 .set = bond_option_active_slave_set
332 [BOND_OPT_QUEUE_ID] = {
333 .id = BOND_OPT_QUEUE_ID,
335 .desc = "Set queue id of a slave",
336 .flags = BOND_OPTFLAG_RAWVAL,
337 .set = bond_option_queue_id_set
339 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
340 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
341 .name = "all_slaves_active",
342 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
343 .values = bond_all_slaves_active_tbl,
344 .set = bond_option_all_slaves_active_set
346 [BOND_OPT_RESEND_IGMP] = {
347 .id = BOND_OPT_RESEND_IGMP,
348 .name = "resend_igmp",
349 .desc = "Number of IGMP membership reports to send on link failure",
350 .values = bond_resend_igmp_tbl,
351 .set = bond_option_resend_igmp_set
353 [BOND_OPT_LP_INTERVAL] = {
354 .id = BOND_OPT_LP_INTERVAL,
355 .name = "lp_interval",
356 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
357 .values = bond_lp_interval_tbl,
358 .set = bond_option_lp_interval_set
360 [BOND_OPT_SLAVES] = {
361 .id = BOND_OPT_SLAVES,
363 .desc = "Slave membership management",
364 .flags = BOND_OPTFLAG_RAWVAL,
365 .set = bond_option_slaves_set
370 /* Searches for a value in opt's values[] table */
371 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
373 const struct bond_option *opt;
376 opt = bond_opt_get(option);
379 for (i = 0; opt->values && opt->values[i].string; i++)
380 if (opt->values[i].value == val)
381 return &opt->values[i];
386 /* Searches for a value in opt's values[] table which matches the flagmask */
387 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
392 for (i = 0; opt->values && opt->values[i].string; i++)
393 if (opt->values[i].flags & flagmask)
394 return &opt->values[i];
399 /* If maxval is missing then there's no range to check. In case minval is
400 * missing then it's considered to be 0.
402 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
404 const struct bond_opt_value *minval, *maxval;
406 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
407 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
408 if (!maxval || (minval && val < minval->value) || val > maxval->value)
415 * bond_opt_parse - parse option value
416 * @opt: the option to parse against
417 * @val: value to parse
419 * This function tries to extract the value from @val and check if it's
420 * a possible match for the option and returns NULL if a match isn't found,
421 * or the struct_opt_value that matched. It also strips the new line from
422 * @val->string if it's present.
424 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
425 struct bond_opt_value *val)
427 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
428 const struct bond_opt_value *tbl;
429 const struct bond_opt_value *ret = NULL;
433 /* No parsing if the option wants a raw val */
434 if (opt->flags & BOND_OPTFLAG_RAWVAL)
441 /* ULLONG_MAX is used to bypass string processing */
442 checkval = val->value != ULLONG_MAX;
446 p = strchr(val->string, '\n');
449 for (p = val->string; *p; p++)
450 if (!(isdigit(*p) || isspace(*p)))
452 /* The following code extracts the string to match or the value
453 * and sets checkval appropriately
456 rv = sscanf(val->string, "%32s", valstr);
458 rv = sscanf(val->string, "%llu", &val->value);
465 for (i = 0; tbl[i].string; i++) {
466 /* Check for exact match */
468 if (val->value == tbl[i].value)
471 if (!strcmp(valstr, "default") &&
472 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
475 if (!strcmp(valstr, tbl[i].string))
478 /* Found an exact match */
482 /* Possible range match */
483 if (checkval && bond_opt_check_range(opt, val->value))
489 /* Check opt's dependencies against bond mode and currently set options */
490 static int bond_opt_check_deps(struct bonding *bond,
491 const struct bond_option *opt)
493 struct bond_params *params = &bond->params;
495 if (test_bit(params->mode, &opt->unsuppmodes))
497 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
499 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
505 static void bond_opt_dep_print(struct bonding *bond,
506 const struct bond_option *opt)
508 const struct bond_opt_value *modeval;
509 struct bond_params *params;
511 params = &bond->params;
512 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
513 if (test_bit(params->mode, &opt->unsuppmodes))
514 pr_err("%s: option %s: mode dependency failed, not supported in mode %s(%llu)\n",
515 bond->dev->name, opt->name,
516 modeval->string, modeval->value);
519 static void bond_opt_error_interpret(struct bonding *bond,
520 const struct bond_option *opt,
521 int error, const struct bond_opt_value *val)
523 const struct bond_opt_value *minval, *maxval;
530 /* sometimes RAWVAL opts may have new lines */
531 p = strchr(val->string, '\n');
534 pr_err("%s: option %s: invalid value (%s)\n",
535 bond->dev->name, opt->name, val->string);
537 pr_err("%s: option %s: invalid value (%llu)\n",
538 bond->dev->name, opt->name, val->value);
541 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
542 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
545 pr_err("%s: option %s: allowed values %llu - %llu\n",
546 bond->dev->name, opt->name, minval ? minval->value : 0,
550 bond_opt_dep_print(bond, opt);
553 pr_err("%s: option %s: unable to set because the bond device has slaves\n",
554 bond->dev->name, opt->name);
557 pr_err("%s: option %s: unable to set because the bond device is up\n",
558 bond->dev->name, opt->name);
566 * __bond_opt_set - set a bonding option
567 * @bond: target bond device
568 * @option: option to set
569 * @val: value to set it to
571 * This function is used to change the bond's option value, it can be
572 * used for both enabling/changing an option and for disabling it. RTNL lock
573 * must be obtained before calling this function.
575 int __bond_opt_set(struct bonding *bond,
576 unsigned int option, struct bond_opt_value *val)
578 const struct bond_opt_value *retval = NULL;
579 const struct bond_option *opt;
584 opt = bond_opt_get(option);
585 if (WARN_ON(!val) || WARN_ON(!opt))
587 ret = bond_opt_check_deps(bond, opt);
590 retval = bond_opt_parse(opt, val);
595 ret = opt->set(bond, retval);
598 bond_opt_error_interpret(bond, opt, ret, val);
604 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
605 * @bond: target bond device
606 * @option: option to set
607 * @buf: value to set it to
609 * This function tries to acquire RTNL without blocking and if successful
610 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
612 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
614 struct bond_opt_value optval;
618 return restart_syscall();
619 bond_opt_initstr(&optval, buf);
620 ret = __bond_opt_set(bond, option, &optval);
627 * bond_opt_get - get a pointer to an option
628 * @option: option for which to return a pointer
630 * This function checks if option is valid and if so returns a pointer
631 * to its entry in the bond_opts[] option array.
633 const struct bond_option *bond_opt_get(unsigned int option)
635 if (!BOND_OPT_VALID(option))
638 return &bond_opts[option];
641 int bond_option_mode_set(struct bonding *bond, const struct bond_opt_value *newval)
643 if (BOND_NO_USES_ARP(newval->value) && bond->params.arp_interval) {
644 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
645 bond->dev->name, newval->string);
646 /* disable arp monitoring */
647 bond->params.arp_interval = 0;
648 /* set miimon to default value */
649 bond->params.miimon = BOND_DEFAULT_MIIMON;
650 pr_info("%s: Setting MII monitoring interval to %d\n",
651 bond->dev->name, bond->params.miimon);
654 /* don't cache arp_validate between modes */
655 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
656 bond->params.mode = newval->value;
661 static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
664 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
667 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
669 struct slave *slave = rcu_dereference(bond->curr_active_slave);
671 return __bond_option_active_slave_get(bond, slave);
674 struct net_device *bond_option_active_slave_get(struct bonding *bond)
676 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
679 static int bond_option_active_slave_set(struct bonding *bond,
680 const struct bond_opt_value *newval)
682 char ifname[IFNAMSIZ] = { 0, };
683 struct net_device *slave_dev;
686 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
687 if (!strlen(ifname) || newval->string[0] == '\n') {
690 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
696 if (!netif_is_bond_slave(slave_dev)) {
697 pr_err("Device %s is not bonding slave\n",
702 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
703 pr_err("%s: Device %s is not our slave\n",
704 bond->dev->name, slave_dev->name);
710 write_lock_bh(&bond->curr_slave_lock);
712 /* check to see if we are clearing active */
714 pr_info("%s: Clearing current active slave\n", bond->dev->name);
715 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
716 bond_select_active_slave(bond);
718 struct slave *old_active = bond->curr_active_slave;
719 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
723 if (new_active == old_active) {
725 pr_info("%s: %s is already the current active slave\n",
726 bond->dev->name, new_active->dev->name);
728 if (old_active && (new_active->link == BOND_LINK_UP) &&
729 IS_UP(new_active->dev)) {
730 pr_info("%s: Setting %s as active slave\n",
731 bond->dev->name, new_active->dev->name);
732 bond_change_active_slave(bond, new_active);
734 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down\n",
735 bond->dev->name, new_active->dev->name,
736 new_active->dev->name);
742 write_unlock_bh(&bond->curr_slave_lock);
743 unblock_netpoll_tx();
748 static int bond_option_miimon_set(struct bonding *bond,
749 const struct bond_opt_value *newval)
751 pr_info("%s: Setting MII monitoring interval to %llu\n",
752 bond->dev->name, newval->value);
753 bond->params.miimon = newval->value;
754 if (bond->params.updelay)
755 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
757 bond->params.updelay * bond->params.miimon);
758 if (bond->params.downdelay)
759 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
761 bond->params.downdelay * bond->params.miimon);
762 if (newval->value && bond->params.arp_interval) {
763 pr_info("%s: MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n",
765 bond->params.arp_interval = 0;
766 if (bond->params.arp_validate)
767 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
769 if (bond->dev->flags & IFF_UP) {
770 /* If the interface is up, we may need to fire off
771 * the MII timer. If the interface is down, the
772 * timer will get fired off when the open function
775 if (!newval->value) {
776 cancel_delayed_work_sync(&bond->mii_work);
778 cancel_delayed_work_sync(&bond->arp_work);
779 queue_delayed_work(bond->wq, &bond->mii_work, 0);
786 static int bond_option_updelay_set(struct bonding *bond,
787 const struct bond_opt_value *newval)
789 int value = newval->value;
791 if (!bond->params.miimon) {
792 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
796 if ((value % bond->params.miimon) != 0) {
797 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
798 bond->dev->name, value,
800 (value / bond->params.miimon) *
801 bond->params.miimon);
803 bond->params.updelay = value / bond->params.miimon;
804 pr_info("%s: Setting up delay to %d\n",
805 bond->dev->name, bond->params.updelay * bond->params.miimon);
810 static int bond_option_downdelay_set(struct bonding *bond,
811 const struct bond_opt_value *newval)
813 int value = newval->value;
815 if (!bond->params.miimon) {
816 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
820 if ((value % bond->params.miimon) != 0) {
821 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
822 bond->dev->name, value,
824 (value / bond->params.miimon) *
825 bond->params.miimon);
827 bond->params.downdelay = value / bond->params.miimon;
828 pr_info("%s: Setting down delay to %d\n",
829 bond->dev->name, bond->params.downdelay * bond->params.miimon);
834 static int bond_option_use_carrier_set(struct bonding *bond,
835 const struct bond_opt_value *newval)
837 pr_info("%s: Setting use_carrier to %llu\n",
838 bond->dev->name, newval->value);
839 bond->params.use_carrier = newval->value;
844 static int bond_option_arp_interval_set(struct bonding *bond,
845 const struct bond_opt_value *newval)
847 pr_info("%s: Setting ARP monitoring interval to %llu\n",
848 bond->dev->name, newval->value);
849 bond->params.arp_interval = newval->value;
851 if (bond->params.miimon) {
852 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring\n",
853 bond->dev->name, bond->dev->name);
854 bond->params.miimon = 0;
856 if (!bond->params.arp_targets[0])
857 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified\n",
860 if (bond->dev->flags & IFF_UP) {
861 /* If the interface is up, we may need to fire off
862 * the ARP timer. If the interface is down, the
863 * timer will get fired off when the open function
866 if (!newval->value) {
867 if (bond->params.arp_validate)
868 bond->recv_probe = NULL;
869 cancel_delayed_work_sync(&bond->arp_work);
871 /* arp_validate can be set only in active-backup mode */
872 bond->recv_probe = bond_arp_rcv;
873 cancel_delayed_work_sync(&bond->mii_work);
874 queue_delayed_work(bond->wq, &bond->arp_work, 0);
881 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
883 unsigned long last_rx)
885 __be32 *targets = bond->params.arp_targets;
886 struct list_head *iter;
889 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
890 bond_for_each_slave(bond, slave, iter)
891 slave->target_last_arp_rx[slot] = last_rx;
892 targets[slot] = target;
896 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
898 __be32 *targets = bond->params.arp_targets;
901 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
902 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
903 bond->dev->name, &target);
907 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
908 pr_err("%s: ARP target %pI4 is already present\n",
909 bond->dev->name, &target);
913 ind = bond_get_targets_ip(targets, 0); /* first free slot */
915 pr_err("%s: ARP target table is full!\n", bond->dev->name);
919 pr_info("%s: Adding ARP target %pI4\n", bond->dev->name, &target);
921 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
926 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
930 /* not to race with bond_arp_rcv */
931 write_lock_bh(&bond->lock);
932 ret = _bond_option_arp_ip_target_add(bond, target);
933 write_unlock_bh(&bond->lock);
938 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
940 __be32 *targets = bond->params.arp_targets;
941 struct list_head *iter;
943 unsigned long *targets_rx;
946 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
947 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
948 bond->dev->name, &target);
952 ind = bond_get_targets_ip(targets, target);
954 pr_err("%s: unable to remove nonexistent ARP target %pI4\n",
955 bond->dev->name, &target);
959 if (ind == 0 && !targets[1] && bond->params.arp_interval)
960 pr_warn("%s: Removing last arp target with arp_interval on\n",
963 pr_info("%s: Removing ARP target %pI4\n", bond->dev->name, &target);
965 /* not to race with bond_arp_rcv */
966 write_lock_bh(&bond->lock);
968 bond_for_each_slave(bond, slave, iter) {
969 targets_rx = slave->target_last_arp_rx;
970 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
971 targets_rx[i] = targets_rx[i+1];
974 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
975 targets[i] = targets[i+1];
978 write_unlock_bh(&bond->lock);
983 void bond_option_arp_ip_targets_clear(struct bonding *bond)
987 /* not to race with bond_arp_rcv */
988 write_lock_bh(&bond->lock);
989 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
990 _bond_options_arp_ip_target_set(bond, i, 0, 0);
991 write_unlock_bh(&bond->lock);
994 static int bond_option_arp_ip_targets_set(struct bonding *bond,
995 const struct bond_opt_value *newval)
1000 if (newval->string) {
1001 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1002 pr_err("%s: invalid ARP target %pI4 specified\n",
1003 bond->dev->name, &target);
1006 if (newval->string[0] == '+')
1007 ret = bond_option_arp_ip_target_add(bond, target);
1008 else if (newval->string[0] == '-')
1009 ret = bond_option_arp_ip_target_rem(bond, target);
1011 pr_err("no command found in arp_ip_targets file for bond %s - use +<addr> or -<addr>\n",
1014 target = newval->value;
1015 ret = bond_option_arp_ip_target_add(bond, target);
1021 static int bond_option_arp_validate_set(struct bonding *bond,
1022 const struct bond_opt_value *newval)
1024 pr_info("%s: Setting arp_validate to %s (%llu)\n",
1025 bond->dev->name, newval->string, newval->value);
1027 if (bond->dev->flags & IFF_UP) {
1029 bond->recv_probe = NULL;
1030 else if (bond->params.arp_interval)
1031 bond->recv_probe = bond_arp_rcv;
1033 bond->params.arp_validate = newval->value;
1038 static int bond_option_arp_all_targets_set(struct bonding *bond,
1039 const struct bond_opt_value *newval)
1041 pr_info("%s: Setting arp_all_targets to %s (%llu)\n",
1042 bond->dev->name, newval->string, newval->value);
1043 bond->params.arp_all_targets = newval->value;
1048 static int bond_option_primary_set(struct bonding *bond,
1049 const struct bond_opt_value *newval)
1051 char *p, *primary = newval->string;
1052 struct list_head *iter;
1053 struct slave *slave;
1056 read_lock(&bond->lock);
1057 write_lock_bh(&bond->curr_slave_lock);
1059 p = strchr(primary, '\n');
1062 /* check to see if we are clearing primary */
1063 if (!strlen(primary)) {
1064 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
1065 bond->primary_slave = NULL;
1066 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1067 bond_select_active_slave(bond);
1071 bond_for_each_slave(bond, slave, iter) {
1072 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1073 pr_info("%s: Setting %s as primary slave\n",
1074 bond->dev->name, slave->dev->name);
1075 bond->primary_slave = slave;
1076 strcpy(bond->params.primary, slave->dev->name);
1077 bond_select_active_slave(bond);
1082 if (bond->primary_slave) {
1083 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
1084 bond->primary_slave = NULL;
1085 bond_select_active_slave(bond);
1087 strncpy(bond->params.primary, primary, IFNAMSIZ);
1088 bond->params.primary[IFNAMSIZ - 1] = 0;
1090 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet\n",
1091 bond->dev->name, primary, bond->dev->name);
1094 write_unlock_bh(&bond->curr_slave_lock);
1095 read_unlock(&bond->lock);
1096 unblock_netpoll_tx();
1101 static int bond_option_primary_reselect_set(struct bonding *bond,
1102 const struct bond_opt_value *newval)
1104 pr_info("%s: Setting primary_reselect to %s (%llu)\n",
1105 bond->dev->name, newval->string, newval->value);
1106 bond->params.primary_reselect = newval->value;
1109 write_lock_bh(&bond->curr_slave_lock);
1110 bond_select_active_slave(bond);
1111 write_unlock_bh(&bond->curr_slave_lock);
1112 unblock_netpoll_tx();
1117 static int bond_option_fail_over_mac_set(struct bonding *bond,
1118 const struct bond_opt_value *newval)
1120 pr_info("%s: Setting fail_over_mac to %s (%llu)\n",
1121 bond->dev->name, newval->string, newval->value);
1122 bond->params.fail_over_mac = newval->value;
1127 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1128 const struct bond_opt_value *newval)
1130 pr_info("%s: Setting xmit hash policy to %s (%llu)\n",
1131 bond->dev->name, newval->string, newval->value);
1132 bond->params.xmit_policy = newval->value;
1137 static int bond_option_resend_igmp_set(struct bonding *bond,
1138 const struct bond_opt_value *newval)
1140 pr_info("%s: Setting resend_igmp to %llu\n",
1141 bond->dev->name, newval->value);
1142 bond->params.resend_igmp = newval->value;
1147 static int bond_option_num_peer_notif_set(struct bonding *bond,
1148 const struct bond_opt_value *newval)
1150 bond->params.num_peer_notif = newval->value;
1155 static int bond_option_all_slaves_active_set(struct bonding *bond,
1156 const struct bond_opt_value *newval)
1158 struct list_head *iter;
1159 struct slave *slave;
1161 if (newval->value == bond->params.all_slaves_active)
1163 bond->params.all_slaves_active = newval->value;
1164 bond_for_each_slave(bond, slave, iter) {
1165 if (!bond_is_active_slave(slave)) {
1167 slave->inactive = 0;
1169 slave->inactive = 1;
1176 static int bond_option_min_links_set(struct bonding *bond,
1177 const struct bond_opt_value *newval)
1179 pr_info("%s: Setting min links value to %llu\n",
1180 bond->dev->name, newval->value);
1181 bond->params.min_links = newval->value;
1186 static int bond_option_lp_interval_set(struct bonding *bond,
1187 const struct bond_opt_value *newval)
1189 bond->params.lp_interval = newval->value;
1194 static int bond_option_pps_set(struct bonding *bond,
1195 const struct bond_opt_value *newval)
1197 bond->params.packets_per_slave = newval->value;
1198 if (newval->value > 0) {
1199 bond->params.reciprocal_packets_per_slave =
1200 reciprocal_value(newval->value);
1202 /* reciprocal_packets_per_slave is unused if
1203 * packets_per_slave is 0 or 1, just initialize it
1205 bond->params.reciprocal_packets_per_slave =
1206 (struct reciprocal_value) { 0 };
1212 static int bond_option_lacp_rate_set(struct bonding *bond,
1213 const struct bond_opt_value *newval)
1215 pr_info("%s: Setting LACP rate to %s (%llu)\n",
1216 bond->dev->name, newval->string, newval->value);
1217 bond->params.lacp_fast = newval->value;
1218 bond_3ad_update_lacp_rate(bond);
1223 static int bond_option_ad_select_set(struct bonding *bond,
1224 const struct bond_opt_value *newval)
1226 pr_info("%s: Setting ad_select to %s (%llu)\n",
1227 bond->dev->name, newval->string, newval->value);
1228 bond->params.ad_select = newval->value;
1233 static int bond_option_queue_id_set(struct bonding *bond,
1234 const struct bond_opt_value *newval)
1236 struct slave *slave, *update_slave;
1237 struct net_device *sdev;
1238 struct list_head *iter;
1243 /* delim will point to queue id if successful */
1244 delim = strchr(newval->string, ':');
1248 /* Terminate string that points to device name and bump it
1249 * up one, so we can read the queue id there.
1252 if (sscanf(++delim, "%hd\n", &qid) != 1)
1255 /* Check buffer length, valid ifname and queue id */
1256 if (!dev_valid_name(newval->string) ||
1257 qid > bond->dev->real_num_tx_queues)
1260 /* Get the pointer to that interface if it exists */
1261 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1265 /* Search for thes slave and check for duplicate qids */
1266 update_slave = NULL;
1267 bond_for_each_slave(bond, slave, iter) {
1268 if (sdev == slave->dev)
1269 /* We don't need to check the matching
1270 * slave for dups, since we're overwriting it
1272 update_slave = slave;
1273 else if (qid && qid == slave->queue_id) {
1281 /* Actually set the qids for the slave */
1282 update_slave->queue_id = qid;
1288 pr_info("invalid input for queue_id set for %s\n", bond->dev->name);
1294 static int bond_option_slaves_set(struct bonding *bond,
1295 const struct bond_opt_value *newval)
1297 char command[IFNAMSIZ + 1] = { 0, };
1298 struct net_device *dev;
1302 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1303 ifname = command + 1;
1304 if ((strlen(command) <= 1) ||
1305 !dev_valid_name(ifname))
1308 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1310 pr_info("%s: interface %s does not exist!\n",
1311 bond->dev->name, ifname);
1316 switch (command[0]) {
1318 pr_info("%s: Adding slave %s\n", bond->dev->name, dev->name);
1319 ret = bond_enslave(bond->dev, dev);
1323 pr_info("%s: Removing slave %s\n", bond->dev->name, dev->name);
1324 ret = bond_release(bond->dev, dev);
1335 pr_err("no command found in slaves file for bond %s - use +ifname or -ifname\n",