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 struct bond_opt_value *newval);
25 static int bond_option_miimon_set(struct bonding *bond,
26 struct bond_opt_value *newval);
27 static int bond_option_updelay_set(struct bonding *bond,
28 struct bond_opt_value *newval);
29 static int bond_option_downdelay_set(struct bonding *bond,
30 struct bond_opt_value *newval);
31 static int bond_option_use_carrier_set(struct bonding *bond,
32 struct bond_opt_value *newval);
33 static int bond_option_arp_interval_set(struct bonding *bond,
34 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 struct bond_opt_value *newval);
39 static int bond_option_arp_validate_set(struct bonding *bond,
40 struct bond_opt_value *newval);
41 static int bond_option_arp_all_targets_set(struct bonding *bond,
42 struct bond_opt_value *newval);
43 static int bond_option_primary_set(struct bonding *bond,
44 struct bond_opt_value *newval);
45 static int bond_option_primary_reselect_set(struct bonding *bond,
46 struct bond_opt_value *newval);
47 static int bond_option_fail_over_mac_set(struct bonding *bond,
48 struct bond_opt_value *newval);
49 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
50 struct bond_opt_value *newval);
51 static int bond_option_resend_igmp_set(struct bonding *bond,
52 struct bond_opt_value *newval);
53 static int bond_option_num_peer_notif_set(struct bonding *bond,
54 struct bond_opt_value *newval);
55 static int bond_option_all_slaves_active_set(struct bonding *bond,
56 struct bond_opt_value *newval);
57 static int bond_option_min_links_set(struct bonding *bond,
58 struct bond_opt_value *newval);
59 static int bond_option_lp_interval_set(struct bonding *bond,
60 struct bond_opt_value *newval);
61 static int bond_option_pps_set(struct bonding *bond,
62 struct bond_opt_value *newval);
63 static int bond_option_lacp_rate_set(struct bonding *bond,
64 struct bond_opt_value *newval);
65 static int bond_option_ad_select_set(struct bonding *bond,
66 struct bond_opt_value *newval);
67 static int bond_option_queue_id_set(struct bonding *bond,
68 struct bond_opt_value *newval);
69 static int bond_option_mode_set(struct bonding *bond,
70 struct bond_opt_value *newval);
71 static int bond_option_slaves_set(struct bonding *bond,
72 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},
181 static const struct bond_option bond_opts[] = {
185 .desc = "bond device mode",
186 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
187 .values = bond_mode_tbl,
188 .set = bond_option_mode_set
190 [BOND_OPT_PACKETS_PER_SLAVE] = {
191 .id = BOND_OPT_PACKETS_PER_SLAVE,
192 .name = "packets_per_slave",
193 .desc = "Packets to send per slave in RR mode",
194 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
195 .values = bond_pps_tbl,
196 .set = bond_option_pps_set
198 [BOND_OPT_XMIT_HASH] = {
199 .id = BOND_OPT_XMIT_HASH,
200 .name = "xmit_hash_policy",
201 .desc = "balance-xor and 802.3ad hashing method",
202 .values = bond_xmit_hashtype_tbl,
203 .set = bond_option_xmit_hash_policy_set
205 [BOND_OPT_ARP_VALIDATE] = {
206 .id = BOND_OPT_ARP_VALIDATE,
207 .name = "arp_validate",
208 .desc = "validate src/dst of ARP probes",
209 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
211 .values = bond_arp_validate_tbl,
212 .set = bond_option_arp_validate_set
214 [BOND_OPT_ARP_ALL_TARGETS] = {
215 .id = BOND_OPT_ARP_ALL_TARGETS,
216 .name = "arp_all_targets",
217 .desc = "fail on any/all arp targets timeout",
218 .values = bond_arp_all_targets_tbl,
219 .set = bond_option_arp_all_targets_set
221 [BOND_OPT_FAIL_OVER_MAC] = {
222 .id = BOND_OPT_FAIL_OVER_MAC,
223 .name = "fail_over_mac",
224 .desc = "For active-backup, do not set all slaves to the same MAC",
225 .flags = BOND_OPTFLAG_NOSLAVES,
226 .values = bond_fail_over_mac_tbl,
227 .set = bond_option_fail_over_mac_set
229 [BOND_OPT_ARP_INTERVAL] = {
230 .id = BOND_OPT_ARP_INTERVAL,
231 .name = "arp_interval",
232 .desc = "arp interval in milliseconds",
233 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
235 .values = bond_intmax_tbl,
236 .set = bond_option_arp_interval_set
238 [BOND_OPT_ARP_TARGETS] = {
239 .id = BOND_OPT_ARP_TARGETS,
240 .name = "arp_ip_target",
241 .desc = "arp targets in n.n.n.n form",
242 .flags = BOND_OPTFLAG_RAWVAL,
243 .set = bond_option_arp_ip_targets_set
245 [BOND_OPT_DOWNDELAY] = {
246 .id = BOND_OPT_DOWNDELAY,
248 .desc = "Delay before considering link down, in milliseconds",
249 .values = bond_intmax_tbl,
250 .set = bond_option_downdelay_set
252 [BOND_OPT_UPDELAY] = {
253 .id = BOND_OPT_UPDELAY,
255 .desc = "Delay before considering link up, in milliseconds",
256 .values = bond_intmax_tbl,
257 .set = bond_option_updelay_set
259 [BOND_OPT_LACP_RATE] = {
260 .id = BOND_OPT_LACP_RATE,
262 .desc = "LACPDU tx rate to request from 802.3ad partner",
263 .flags = BOND_OPTFLAG_IFDOWN,
264 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
265 .values = bond_lacp_rate_tbl,
266 .set = bond_option_lacp_rate_set
268 [BOND_OPT_MINLINKS] = {
269 .id = BOND_OPT_MINLINKS,
271 .desc = "Minimum number of available links before turning on carrier",
272 .values = bond_intmax_tbl,
273 .set = bond_option_min_links_set
275 [BOND_OPT_AD_SELECT] = {
276 .id = BOND_OPT_AD_SELECT,
278 .desc = "803.ad aggregation selection logic",
279 .flags = BOND_OPTFLAG_IFDOWN,
280 .values = bond_ad_select_tbl,
281 .set = bond_option_ad_select_set
283 [BOND_OPT_NUM_PEER_NOTIF] = {
284 .id = BOND_OPT_NUM_PEER_NOTIF,
285 .name = "num_unsol_na",
286 .desc = "Number of peer notifications to send on failover event",
287 .values = bond_num_peer_notif_tbl,
288 .set = bond_option_num_peer_notif_set
290 [BOND_OPT_MIIMON] = {
291 .id = BOND_OPT_MIIMON,
293 .desc = "Link check interval in milliseconds",
294 .values = bond_intmax_tbl,
295 .set = bond_option_miimon_set
297 [BOND_OPT_PRIMARY] = {
298 .id = BOND_OPT_PRIMARY,
300 .desc = "Primary network device to use",
301 .flags = BOND_OPTFLAG_RAWVAL,
302 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
305 .set = bond_option_primary_set
307 [BOND_OPT_PRIMARY_RESELECT] = {
308 .id = BOND_OPT_PRIMARY_RESELECT,
309 .name = "primary_reselect",
310 .desc = "Reselect primary slave once it comes up",
311 .values = bond_primary_reselect_tbl,
312 .set = bond_option_primary_reselect_set
314 [BOND_OPT_USE_CARRIER] = {
315 .id = BOND_OPT_USE_CARRIER,
316 .name = "use_carrier",
317 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
318 .values = bond_use_carrier_tbl,
319 .set = bond_option_use_carrier_set
321 [BOND_OPT_ACTIVE_SLAVE] = {
322 .id = BOND_OPT_ACTIVE_SLAVE,
323 .name = "active_slave",
324 .desc = "Currently active slave",
325 .flags = BOND_OPTFLAG_RAWVAL,
326 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
329 .set = bond_option_active_slave_set
331 [BOND_OPT_QUEUE_ID] = {
332 .id = BOND_OPT_QUEUE_ID,
334 .desc = "Set queue id of a slave",
335 .flags = BOND_OPTFLAG_RAWVAL,
336 .set = bond_option_queue_id_set
338 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
339 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
340 .name = "all_slaves_active",
341 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
342 .values = bond_all_slaves_active_tbl,
343 .set = bond_option_all_slaves_active_set
345 [BOND_OPT_RESEND_IGMP] = {
346 .id = BOND_OPT_RESEND_IGMP,
347 .name = "resend_igmp",
348 .desc = "Number of IGMP membership reports to send on link failure",
349 .values = bond_resend_igmp_tbl,
350 .set = bond_option_resend_igmp_set
352 [BOND_OPT_LP_INTERVAL] = {
353 .id = BOND_OPT_LP_INTERVAL,
354 .name = "lp_interval",
355 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
356 .values = bond_lp_interval_tbl,
357 .set = bond_option_lp_interval_set
359 [BOND_OPT_SLAVES] = {
360 .id = BOND_OPT_SLAVES,
362 .desc = "Slave membership management",
363 .flags = BOND_OPTFLAG_RAWVAL,
364 .set = bond_option_slaves_set
369 /* Searches for a value in opt's values[] table */
370 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
372 const struct bond_option *opt;
375 opt = bond_opt_get(option);
378 for (i = 0; opt->values && opt->values[i].string; i++)
379 if (opt->values[i].value == val)
380 return &opt->values[i];
385 /* Searches for a value in opt's values[] table which matches the flagmask */
386 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
391 for (i = 0; opt->values && opt->values[i].string; i++)
392 if (opt->values[i].flags & flagmask)
393 return &opt->values[i];
398 /* If maxval is missing then there's no range to check. In case minval is
399 * missing then it's considered to be 0.
401 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
403 const struct bond_opt_value *minval, *maxval;
405 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
406 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
407 if (!maxval || (minval && val < minval->value) || val > maxval->value)
414 * bond_opt_parse - parse option value
415 * @opt: the option to parse against
416 * @val: value to parse
418 * This function tries to extract the value from @val and check if it's
419 * a possible match for the option and returns NULL if a match isn't found,
420 * or the struct_opt_value that matched. It also strips the new line from
421 * @val->string if it's present.
423 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
424 struct bond_opt_value *val)
426 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
427 const struct bond_opt_value *tbl;
428 const struct bond_opt_value *ret = NULL;
432 /* No parsing if the option wants a raw val */
433 if (opt->flags & BOND_OPTFLAG_RAWVAL)
440 /* ULLONG_MAX is used to bypass string processing */
441 checkval = val->value != ULLONG_MAX;
445 p = strchr(val->string, '\n');
448 for (p = val->string; *p; p++)
449 if (!(isdigit(*p) || isspace(*p)))
451 /* The following code extracts the string to match or the value
452 * and sets checkval appropriately
455 rv = sscanf(val->string, "%32s", valstr);
457 rv = sscanf(val->string, "%llu", &val->value);
464 for (i = 0; tbl[i].string; i++) {
465 /* Check for exact match */
467 if (val->value == tbl[i].value)
470 if (!strcmp(valstr, "default") &&
471 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
474 if (!strcmp(valstr, tbl[i].string))
477 /* Found an exact match */
481 /* Possible range match */
482 if (checkval && bond_opt_check_range(opt, val->value))
488 /* Check opt's dependencies against bond mode and currently set options */
489 static int bond_opt_check_deps(struct bonding *bond,
490 const struct bond_option *opt)
492 struct bond_params *params = &bond->params;
494 if (test_bit(params->mode, &opt->unsuppmodes))
496 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
498 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
504 static void bond_opt_dep_print(struct bonding *bond,
505 const struct bond_option *opt)
507 struct bond_opt_value *modeval;
508 struct bond_params *params;
510 params = &bond->params;
511 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
512 if (test_bit(params->mode, &opt->unsuppmodes))
513 pr_err("%s: option %s: mode dependency failed, not supported in mode %s(%llu)\n",
514 bond->dev->name, opt->name,
515 modeval->string, modeval->value);
518 static void bond_opt_error_interpret(struct bonding *bond,
519 const struct bond_option *opt,
520 int error, struct bond_opt_value *val)
522 struct bond_opt_value *minval, *maxval;
529 /* sometimes RAWVAL opts may have new lines */
530 p = strchr(val->string, '\n');
533 pr_err("%s: option %s: invalid value (%s)\n",
534 bond->dev->name, opt->name, val->string);
536 pr_err("%s: option %s: invalid value (%llu)\n",
537 bond->dev->name, opt->name, val->value);
540 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
541 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
544 pr_err("%s: option %s: allowed values %llu - %llu\n",
545 bond->dev->name, opt->name, minval ? minval->value : 0,
549 bond_opt_dep_print(bond, opt);
552 pr_err("%s: option %s: unable to set because the bond device has slaves\n",
553 bond->dev->name, opt->name);
556 pr_err("%s: option %s: unable to set because the bond device is up\n",
557 bond->dev->name, opt->name);
565 * __bond_opt_set - set a bonding option
566 * @bond: target bond device
567 * @option: option to set
568 * @val: value to set it to
570 * This function is used to change the bond's option value, it can be
571 * used for both enabling/changing an option and for disabling it. RTNL lock
572 * must be obtained before calling this function.
574 int __bond_opt_set(struct bonding *bond,
575 unsigned int option, struct bond_opt_value *val)
577 struct bond_opt_value *retval = NULL;
578 const struct bond_option *opt;
583 opt = bond_opt_get(option);
584 if (WARN_ON(!val) || WARN_ON(!opt))
586 ret = bond_opt_check_deps(bond, opt);
589 retval = bond_opt_parse(opt, val);
594 ret = opt->set(bond, retval);
597 bond_opt_error_interpret(bond, opt, ret, val);
603 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
604 * @bond: target bond device
605 * @option: option to set
606 * @buf: value to set it to
608 * This function tries to acquire RTNL without blocking and if successful
609 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
611 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
613 struct bond_opt_value optval;
617 return restart_syscall();
618 bond_opt_initstr(&optval, buf);
619 ret = __bond_opt_set(bond, option, &optval);
626 * bond_opt_get - get a pointer to an option
627 * @option: option for which to return a pointer
629 * This function checks if option is valid and if so returns a pointer
630 * to its entry in the bond_opts[] option array.
632 const struct bond_option *bond_opt_get(unsigned int option)
634 if (!BOND_OPT_VALID(option))
637 return &bond_opts[option];
640 int bond_option_mode_set(struct bonding *bond, struct bond_opt_value *newval)
642 if (BOND_NO_USES_ARP(newval->value) && bond->params.arp_interval) {
643 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
644 bond->dev->name, newval->string);
645 /* disable arp monitoring */
646 bond->params.arp_interval = 0;
647 /* set miimon to default value */
648 bond->params.miimon = BOND_DEFAULT_MIIMON;
649 pr_info("%s: Setting MII monitoring interval to %d\n",
650 bond->dev->name, bond->params.miimon);
653 /* don't cache arp_validate between modes */
654 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
655 bond->params.mode = newval->value;
660 static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
663 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
666 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
668 struct slave *slave = rcu_dereference(bond->curr_active_slave);
670 return __bond_option_active_slave_get(bond, slave);
673 struct net_device *bond_option_active_slave_get(struct bonding *bond)
675 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
678 static int bond_option_active_slave_set(struct bonding *bond,
679 struct bond_opt_value *newval)
681 char ifname[IFNAMSIZ] = { 0, };
682 struct net_device *slave_dev;
685 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
686 if (!strlen(ifname) || newval->string[0] == '\n') {
689 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
695 if (!netif_is_bond_slave(slave_dev)) {
696 pr_err("Device %s is not bonding slave\n",
701 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
702 pr_err("%s: Device %s is not our slave\n",
703 bond->dev->name, slave_dev->name);
709 write_lock_bh(&bond->curr_slave_lock);
711 /* check to see if we are clearing active */
713 pr_info("%s: Clearing current active slave\n", bond->dev->name);
714 rcu_assign_pointer(bond->curr_active_slave, NULL);
715 bond_select_active_slave(bond);
717 struct slave *old_active = bond->curr_active_slave;
718 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
722 if (new_active == old_active) {
724 pr_info("%s: %s is already the current active slave\n",
725 bond->dev->name, new_active->dev->name);
727 if (old_active && (new_active->link == BOND_LINK_UP) &&
728 IS_UP(new_active->dev)) {
729 pr_info("%s: Setting %s as active slave\n",
730 bond->dev->name, new_active->dev->name);
731 bond_change_active_slave(bond, new_active);
733 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down\n",
734 bond->dev->name, new_active->dev->name,
735 new_active->dev->name);
741 write_unlock_bh(&bond->curr_slave_lock);
742 unblock_netpoll_tx();
747 static int bond_option_miimon_set(struct bonding *bond,
748 struct bond_opt_value *newval)
750 pr_info("%s: Setting MII monitoring interval to %llu\n",
751 bond->dev->name, newval->value);
752 bond->params.miimon = newval->value;
753 if (bond->params.updelay)
754 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
756 bond->params.updelay * bond->params.miimon);
757 if (bond->params.downdelay)
758 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
760 bond->params.downdelay * bond->params.miimon);
761 if (newval->value && bond->params.arp_interval) {
762 pr_info("%s: MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n",
764 bond->params.arp_interval = 0;
765 if (bond->params.arp_validate)
766 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
768 if (bond->dev->flags & IFF_UP) {
769 /* If the interface is up, we may need to fire off
770 * the MII timer. If the interface is down, the
771 * timer will get fired off when the open function
774 if (!newval->value) {
775 cancel_delayed_work_sync(&bond->mii_work);
777 cancel_delayed_work_sync(&bond->arp_work);
778 queue_delayed_work(bond->wq, &bond->mii_work, 0);
785 static int bond_option_updelay_set(struct bonding *bond,
786 struct bond_opt_value *newval)
788 int value = newval->value;
790 if (!bond->params.miimon) {
791 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
795 if ((value % bond->params.miimon) != 0) {
796 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
797 bond->dev->name, value,
799 (value / bond->params.miimon) *
800 bond->params.miimon);
802 bond->params.updelay = value / bond->params.miimon;
803 pr_info("%s: Setting up delay to %d\n",
804 bond->dev->name, bond->params.updelay * bond->params.miimon);
809 static int bond_option_downdelay_set(struct bonding *bond,
810 struct bond_opt_value *newval)
812 int value = newval->value;
814 if (!bond->params.miimon) {
815 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
819 if ((value % bond->params.miimon) != 0) {
820 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
821 bond->dev->name, value,
823 (value / bond->params.miimon) *
824 bond->params.miimon);
826 bond->params.downdelay = value / bond->params.miimon;
827 pr_info("%s: Setting down delay to %d\n",
828 bond->dev->name, bond->params.downdelay * bond->params.miimon);
833 static int bond_option_use_carrier_set(struct bonding *bond,
834 struct bond_opt_value *newval)
836 pr_info("%s: Setting use_carrier to %llu\n",
837 bond->dev->name, newval->value);
838 bond->params.use_carrier = newval->value;
843 static int bond_option_arp_interval_set(struct bonding *bond,
844 struct bond_opt_value *newval)
846 pr_info("%s: Setting ARP monitoring interval to %llu\n",
847 bond->dev->name, newval->value);
848 bond->params.arp_interval = newval->value;
850 if (bond->params.miimon) {
851 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring\n",
852 bond->dev->name, bond->dev->name);
853 bond->params.miimon = 0;
855 if (!bond->params.arp_targets[0])
856 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified\n",
859 if (bond->dev->flags & IFF_UP) {
860 /* If the interface is up, we may need to fire off
861 * the ARP timer. If the interface is down, the
862 * timer will get fired off when the open function
865 if (!newval->value) {
866 if (bond->params.arp_validate)
867 bond->recv_probe = NULL;
868 cancel_delayed_work_sync(&bond->arp_work);
870 /* arp_validate can be set only in active-backup mode */
871 bond->recv_probe = bond_arp_rcv;
872 cancel_delayed_work_sync(&bond->mii_work);
873 queue_delayed_work(bond->wq, &bond->arp_work, 0);
880 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
882 unsigned long last_rx)
884 __be32 *targets = bond->params.arp_targets;
885 struct list_head *iter;
888 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
889 bond_for_each_slave(bond, slave, iter)
890 slave->target_last_arp_rx[slot] = last_rx;
891 targets[slot] = target;
895 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
897 __be32 *targets = bond->params.arp_targets;
900 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
901 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
902 bond->dev->name, &target);
906 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
907 pr_err("%s: ARP target %pI4 is already present\n",
908 bond->dev->name, &target);
912 ind = bond_get_targets_ip(targets, 0); /* first free slot */
914 pr_err("%s: ARP target table is full!\n", bond->dev->name);
918 pr_info("%s: Adding ARP target %pI4\n", bond->dev->name, &target);
920 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
925 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
929 /* not to race with bond_arp_rcv */
930 write_lock_bh(&bond->lock);
931 ret = _bond_option_arp_ip_target_add(bond, target);
932 write_unlock_bh(&bond->lock);
937 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
939 __be32 *targets = bond->params.arp_targets;
940 struct list_head *iter;
942 unsigned long *targets_rx;
945 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
946 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
947 bond->dev->name, &target);
951 ind = bond_get_targets_ip(targets, target);
953 pr_err("%s: unable to remove nonexistent ARP target %pI4\n",
954 bond->dev->name, &target);
958 if (ind == 0 && !targets[1] && bond->params.arp_interval)
959 pr_warn("%s: Removing last arp target with arp_interval on\n",
962 pr_info("%s: Removing ARP target %pI4\n", bond->dev->name, &target);
964 /* not to race with bond_arp_rcv */
965 write_lock_bh(&bond->lock);
967 bond_for_each_slave(bond, slave, iter) {
968 targets_rx = slave->target_last_arp_rx;
969 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
970 targets_rx[i] = targets_rx[i+1];
973 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
974 targets[i] = targets[i+1];
977 write_unlock_bh(&bond->lock);
982 void bond_option_arp_ip_targets_clear(struct bonding *bond)
986 /* not to race with bond_arp_rcv */
987 write_lock_bh(&bond->lock);
988 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
989 _bond_options_arp_ip_target_set(bond, i, 0, 0);
990 write_unlock_bh(&bond->lock);
993 static int bond_option_arp_ip_targets_set(struct bonding *bond,
994 struct bond_opt_value *newval)
999 if (newval->string) {
1000 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1001 pr_err("%s: invalid ARP target %pI4 specified\n",
1002 bond->dev->name, &target);
1005 if (newval->string[0] == '+')
1006 ret = bond_option_arp_ip_target_add(bond, target);
1007 else if (newval->string[0] == '-')
1008 ret = bond_option_arp_ip_target_rem(bond, target);
1010 pr_err("no command found in arp_ip_targets file for bond %s - use +<addr> or -<addr>\n",
1013 target = newval->value;
1014 ret = bond_option_arp_ip_target_add(bond, target);
1020 static int bond_option_arp_validate_set(struct bonding *bond,
1021 struct bond_opt_value *newval)
1023 pr_info("%s: Setting arp_validate to %s (%llu)\n",
1024 bond->dev->name, newval->string, newval->value);
1026 if (bond->dev->flags & IFF_UP) {
1028 bond->recv_probe = NULL;
1029 else if (bond->params.arp_interval)
1030 bond->recv_probe = bond_arp_rcv;
1032 bond->params.arp_validate = newval->value;
1037 static int bond_option_arp_all_targets_set(struct bonding *bond,
1038 struct bond_opt_value *newval)
1040 pr_info("%s: Setting arp_all_targets to %s (%llu)\n",
1041 bond->dev->name, newval->string, newval->value);
1042 bond->params.arp_all_targets = newval->value;
1047 static int bond_option_primary_set(struct bonding *bond,
1048 struct bond_opt_value *newval)
1050 char *p, *primary = newval->string;
1051 struct list_head *iter;
1052 struct slave *slave;
1055 read_lock(&bond->lock);
1056 write_lock_bh(&bond->curr_slave_lock);
1058 p = strchr(primary, '\n');
1061 /* check to see if we are clearing primary */
1062 if (!strlen(primary)) {
1063 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
1064 bond->primary_slave = NULL;
1065 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1066 bond_select_active_slave(bond);
1070 bond_for_each_slave(bond, slave, iter) {
1071 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1072 pr_info("%s: Setting %s as primary slave\n",
1073 bond->dev->name, slave->dev->name);
1074 bond->primary_slave = slave;
1075 strcpy(bond->params.primary, slave->dev->name);
1076 bond_select_active_slave(bond);
1081 if (bond->primary_slave) {
1082 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
1083 bond->primary_slave = NULL;
1084 bond_select_active_slave(bond);
1086 strncpy(bond->params.primary, primary, IFNAMSIZ);
1087 bond->params.primary[IFNAMSIZ - 1] = 0;
1089 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet\n",
1090 bond->dev->name, primary, bond->dev->name);
1093 write_unlock_bh(&bond->curr_slave_lock);
1094 read_unlock(&bond->lock);
1095 unblock_netpoll_tx();
1100 static int bond_option_primary_reselect_set(struct bonding *bond,
1101 struct bond_opt_value *newval)
1103 pr_info("%s: Setting primary_reselect to %s (%llu)\n",
1104 bond->dev->name, newval->string, newval->value);
1105 bond->params.primary_reselect = newval->value;
1108 write_lock_bh(&bond->curr_slave_lock);
1109 bond_select_active_slave(bond);
1110 write_unlock_bh(&bond->curr_slave_lock);
1111 unblock_netpoll_tx();
1116 static int bond_option_fail_over_mac_set(struct bonding *bond,
1117 struct bond_opt_value *newval)
1119 pr_info("%s: Setting fail_over_mac to %s (%llu)\n",
1120 bond->dev->name, newval->string, newval->value);
1121 bond->params.fail_over_mac = newval->value;
1126 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1127 struct bond_opt_value *newval)
1129 pr_info("%s: Setting xmit hash policy to %s (%llu)\n",
1130 bond->dev->name, newval->string, newval->value);
1131 bond->params.xmit_policy = newval->value;
1136 static int bond_option_resend_igmp_set(struct bonding *bond,
1137 struct bond_opt_value *newval)
1139 pr_info("%s: Setting resend_igmp to %llu\n",
1140 bond->dev->name, newval->value);
1141 bond->params.resend_igmp = newval->value;
1146 static int bond_option_num_peer_notif_set(struct bonding *bond,
1147 struct bond_opt_value *newval)
1149 bond->params.num_peer_notif = newval->value;
1154 static int bond_option_all_slaves_active_set(struct bonding *bond,
1155 struct bond_opt_value *newval)
1157 struct list_head *iter;
1158 struct slave *slave;
1160 if (newval->value == bond->params.all_slaves_active)
1162 bond->params.all_slaves_active = newval->value;
1163 bond_for_each_slave(bond, slave, iter) {
1164 if (!bond_is_active_slave(slave)) {
1166 slave->inactive = 0;
1168 slave->inactive = 1;
1175 static int bond_option_min_links_set(struct bonding *bond,
1176 struct bond_opt_value *newval)
1178 pr_info("%s: Setting min links value to %llu\n",
1179 bond->dev->name, newval->value);
1180 bond->params.min_links = newval->value;
1185 static int bond_option_lp_interval_set(struct bonding *bond,
1186 struct bond_opt_value *newval)
1188 bond->params.lp_interval = newval->value;
1193 static int bond_option_pps_set(struct bonding *bond,
1194 struct bond_opt_value *newval)
1196 bond->params.packets_per_slave = newval->value;
1197 if (newval->value > 0) {
1198 bond->params.reciprocal_packets_per_slave =
1199 reciprocal_value(newval->value);
1201 /* reciprocal_packets_per_slave is unused if
1202 * packets_per_slave is 0 or 1, just initialize it
1204 bond->params.reciprocal_packets_per_slave =
1205 (struct reciprocal_value) { 0 };
1211 static int bond_option_lacp_rate_set(struct bonding *bond,
1212 struct bond_opt_value *newval)
1214 pr_info("%s: Setting LACP rate to %s (%llu)\n",
1215 bond->dev->name, newval->string, newval->value);
1216 bond->params.lacp_fast = newval->value;
1217 bond_3ad_update_lacp_rate(bond);
1222 static int bond_option_ad_select_set(struct bonding *bond,
1223 struct bond_opt_value *newval)
1225 pr_info("%s: Setting ad_select to %s (%llu)\n",
1226 bond->dev->name, newval->string, newval->value);
1227 bond->params.ad_select = newval->value;
1232 static int bond_option_queue_id_set(struct bonding *bond,
1233 struct bond_opt_value *newval)
1235 struct slave *slave, *update_slave;
1236 struct net_device *sdev;
1237 struct list_head *iter;
1242 /* delim will point to queue id if successful */
1243 delim = strchr(newval->string, ':');
1247 /* Terminate string that points to device name and bump it
1248 * up one, so we can read the queue id there.
1251 if (sscanf(++delim, "%hd\n", &qid) != 1)
1254 /* Check buffer length, valid ifname and queue id */
1255 if (!dev_valid_name(newval->string) ||
1256 qid > bond->dev->real_num_tx_queues)
1259 /* Get the pointer to that interface if it exists */
1260 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1264 /* Search for thes slave and check for duplicate qids */
1265 update_slave = NULL;
1266 bond_for_each_slave(bond, slave, iter) {
1267 if (sdev == slave->dev)
1268 /* We don't need to check the matching
1269 * slave for dups, since we're overwriting it
1271 update_slave = slave;
1272 else if (qid && qid == slave->queue_id) {
1280 /* Actually set the qids for the slave */
1281 update_slave->queue_id = qid;
1287 pr_info("invalid input for queue_id set for %s\n", bond->dev->name);
1293 static int bond_option_slaves_set(struct bonding *bond,
1294 struct bond_opt_value *newval)
1296 char command[IFNAMSIZ + 1] = { 0, };
1297 struct net_device *dev;
1301 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1302 ifname = command + 1;
1303 if ((strlen(command) <= 1) ||
1304 !dev_valid_name(ifname))
1307 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1309 pr_info("%s: interface %s does not exist!\n",
1310 bond->dev->name, ifname);
1315 switch (command[0]) {
1317 pr_info("%s: Adding slave %s\n", bond->dev->name, dev->name);
1318 ret = bond_enslave(bond->dev, dev);
1322 pr_info("%s: Removing slave %s\n", bond->dev->name, dev->name);
1323 ret = bond_release(bond->dev, dev);
1334 pr_err("no command found in slaves file for bond %s - use +ifname or -ifname\n",