bonding: permit enslaving interfaces without set_mac support
[linux-block.git] / drivers / net / bonding / bond_options.c
CommitLineData
72be35fe
JP
1/*
2 * drivers/net/bond/bond_options.c - bonding options
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
eecdaa6e 4 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
72be35fe
JP
5 *
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.
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/errno.h>
15#include <linux/if.h>
d9e32b21 16#include <linux/netdevice.h>
eb2d4c64 17#include <linux/spinlock.h>
d9e32b21 18#include <linux/rcupdate.h>
09117362 19#include <linux/ctype.h>
4fb0ef58 20#include <linux/inet.h>
72be35fe
JP
21#include "bonding.h"
22
f3253339 23static int bond_option_active_slave_set(struct bonding *bond,
28f084cc 24 const struct bond_opt_value *newval);
f3253339 25static int bond_option_miimon_set(struct bonding *bond,
28f084cc 26 const struct bond_opt_value *newval);
f3253339 27static int bond_option_updelay_set(struct bonding *bond,
28f084cc 28 const struct bond_opt_value *newval);
f3253339 29static int bond_option_downdelay_set(struct bonding *bond,
28f084cc 30 const struct bond_opt_value *newval);
f3253339 31static int bond_option_use_carrier_set(struct bonding *bond,
28f084cc 32 const struct bond_opt_value *newval);
f3253339 33static int bond_option_arp_interval_set(struct bonding *bond,
28f084cc 34 const struct bond_opt_value *newval);
f3253339 35static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
36static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
37static int bond_option_arp_ip_targets_set(struct bonding *bond,
28f084cc 38 const struct bond_opt_value *newval);
f3253339 39static int bond_option_arp_validate_set(struct bonding *bond,
28f084cc 40 const struct bond_opt_value *newval);
f3253339 41static int bond_option_arp_all_targets_set(struct bonding *bond,
28f084cc 42 const struct bond_opt_value *newval);
f3253339 43static int bond_option_primary_set(struct bonding *bond,
28f084cc 44 const struct bond_opt_value *newval);
f3253339 45static int bond_option_primary_reselect_set(struct bonding *bond,
28f084cc 46 const struct bond_opt_value *newval);
f3253339 47static int bond_option_fail_over_mac_set(struct bonding *bond,
28f084cc 48 const struct bond_opt_value *newval);
f3253339 49static int bond_option_xmit_hash_policy_set(struct bonding *bond,
28f084cc 50 const struct bond_opt_value *newval);
f3253339 51static int bond_option_resend_igmp_set(struct bonding *bond,
28f084cc 52 const struct bond_opt_value *newval);
f3253339 53static int bond_option_num_peer_notif_set(struct bonding *bond,
28f084cc 54 const struct bond_opt_value *newval);
f3253339 55static int bond_option_all_slaves_active_set(struct bonding *bond,
28f084cc 56 const struct bond_opt_value *newval);
f3253339 57static int bond_option_min_links_set(struct bonding *bond,
28f084cc 58 const struct bond_opt_value *newval);
f3253339 59static int bond_option_lp_interval_set(struct bonding *bond,
28f084cc 60 const struct bond_opt_value *newval);
f3253339 61static int bond_option_pps_set(struct bonding *bond,
28f084cc 62 const struct bond_opt_value *newval);
f3253339 63static int bond_option_lacp_rate_set(struct bonding *bond,
28f084cc 64 const struct bond_opt_value *newval);
f3253339 65static int bond_option_ad_select_set(struct bonding *bond,
28f084cc 66 const struct bond_opt_value *newval);
f3253339 67static int bond_option_queue_id_set(struct bonding *bond,
28f084cc 68 const struct bond_opt_value *newval);
f3253339 69static int bond_option_mode_set(struct bonding *bond,
28f084cc 70 const struct bond_opt_value *newval);
f3253339 71static int bond_option_slaves_set(struct bonding *bond,
28f084cc 72 const struct bond_opt_value *newval);
e9f0fb88
MB
73static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
74 const struct bond_opt_value *newval);
f3253339 75
76
77static const struct bond_opt_value bond_mode_tbl[] = {
2b3798d5
NA
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},
85 { NULL, -1, 0},
86};
87
f3253339 88static const struct bond_opt_value bond_pps_tbl[] = {
aa59d851
NA
89 { "default", 1, BOND_VALFLAG_DEFAULT},
90 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
91 { NULL, -1, 0},
92};
93
f3253339 94static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
a4b32ce7
NA
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},
100 { NULL, -1, 0},
101};
102
f3253339 103static const struct bond_opt_value bond_arp_validate_tbl[] = {
896149ff
VF
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},
111 { NULL, -1, 0},
16228881
NA
112};
113
f3253339 114static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
edf36b24
NA
115 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
116 { "all", BOND_ARP_TARGETS_ALL, 0},
117 { NULL, -1, 0},
118};
119
f3253339 120static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
1df6b6aa
NA
121 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
122 { "active", BOND_FOM_ACTIVE, 0},
123 { "follow", BOND_FOM_FOLLOW, 0},
124 { NULL, -1, 0},
125};
126
f3253339 127static const struct bond_opt_value bond_intmax_tbl[] = {
7bdb04ed
NA
128 { "off", 0, BOND_VALFLAG_DEFAULT},
129 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
81c70806 130 { NULL, -1, 0}
7bdb04ed
NA
131};
132
f3253339 133static const struct bond_opt_value bond_lacp_rate_tbl[] = {
d3131de7
NA
134 { "slow", AD_LACP_SLOW, 0},
135 { "fast", AD_LACP_FAST, 0},
136 { NULL, -1, 0},
137};
138
f3253339 139static const struct bond_opt_value bond_ad_select_tbl[] = {
9e5f5eeb
NA
140 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
141 { "bandwidth", BOND_AD_BANDWIDTH, 0},
142 { "count", BOND_AD_COUNT, 0},
143 { NULL, -1, 0},
144};
145
f3253339 146static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
ef56becb
NA
147 { "off", 0, 0},
148 { "maxval", 255, BOND_VALFLAG_MAX},
149 { "default", 1, BOND_VALFLAG_DEFAULT},
150 { NULL, -1, 0}
151};
152
f3253339 153static const struct bond_opt_value bond_primary_reselect_tbl[] = {
388d3a6d
NA
154 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
155 { "better", BOND_PRI_RESELECT_BETTER, 0},
156 { "failure", BOND_PRI_RESELECT_FAILURE, 0},
157 { NULL, -1},
158};
159
f3253339 160static const struct bond_opt_value bond_use_carrier_tbl[] = {
0fff0608
NA
161 { "off", 0, 0},
162 { "on", 1, BOND_VALFLAG_DEFAULT},
163 { NULL, -1, 0}
164};
165
f3253339 166static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
3df01162
NA
167 { "off", 0, BOND_VALFLAG_DEFAULT},
168 { "on", 1, 0},
169 { NULL, -1, 0}
170};
171
f3253339 172static const struct bond_opt_value bond_resend_igmp_tbl[] = {
105c8fb6
NA
173 { "off", 0, 0},
174 { "maxval", 255, BOND_VALFLAG_MAX},
175 { "default", 1, BOND_VALFLAG_DEFAULT},
176 { NULL, -1, 0}
177};
178
f3253339 179static const struct bond_opt_value bond_lp_interval_tbl[] = {
4325b374
NA
180 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
181 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
5bd4e4c1 182 { NULL, -1, 0},
4325b374
NA
183};
184
e9f0fb88
MB
185static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
186 { "off", 0, 0},
187 { "on", 1, BOND_VALFLAG_DEFAULT},
188 { NULL, -1, 0}
189};
190
f3253339 191static const struct bond_option bond_opts[] = {
2b3798d5
NA
192 [BOND_OPT_MODE] = {
193 .id = BOND_OPT_MODE,
194 .name = "mode",
195 .desc = "bond device mode",
196 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
197 .values = bond_mode_tbl,
198 .set = bond_option_mode_set
199 },
aa59d851
NA
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
207 },
a4b32ce7
NA
208 [BOND_OPT_XMIT_HASH] = {
209 .id = BOND_OPT_XMIT_HASH,
210 .name = "xmit_hash_policy",
f05b42ea 211 .desc = "balance-xor, 802.3ad, and tlb hashing method",
a4b32ce7
NA
212 .values = bond_xmit_hashtype_tbl,
213 .set = bond_option_xmit_hash_policy_set
214 },
16228881
NA
215 [BOND_OPT_ARP_VALIDATE] = {
216 .id = BOND_OPT_ARP_VALIDATE,
217 .name = "arp_validate",
218 .desc = "validate src/dst of ARP probes",
13ac34a8
VF
219 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
220 BIT(BOND_MODE_ALB),
16228881
NA
221 .values = bond_arp_validate_tbl,
222 .set = bond_option_arp_validate_set
223 },
edf36b24
NA
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
230 },
1df6b6aa
NA
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
238 },
7bdb04ed
NA
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) |
244 BIT(BOND_MODE_ALB),
245 .values = bond_intmax_tbl,
246 .set = bond_option_arp_interval_set
247 },
4fb0ef58
NA
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
254 },
25a9b54a
NA
255 [BOND_OPT_DOWNDELAY] = {
256 .id = BOND_OPT_DOWNDELAY,
257 .name = "downdelay",
258 .desc = "Delay before considering link down, in milliseconds",
259 .values = bond_intmax_tbl,
260 .set = bond_option_downdelay_set
261 },
e4994612
NA
262 [BOND_OPT_UPDELAY] = {
263 .id = BOND_OPT_UPDELAY,
264 .name = "updelay",
265 .desc = "Delay before considering link up, in milliseconds",
266 .values = bond_intmax_tbl,
267 .set = bond_option_updelay_set
268 },
d3131de7
NA
269 [BOND_OPT_LACP_RATE] = {
270 .id = BOND_OPT_LACP_RATE,
271 .name = "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
277 },
633ddc9e
NA
278 [BOND_OPT_MINLINKS] = {
279 .id = BOND_OPT_MINLINKS,
280 .name = "min_links",
281 .desc = "Minimum number of available links before turning on carrier",
282 .values = bond_intmax_tbl,
283 .set = bond_option_min_links_set
284 },
9e5f5eeb
NA
285 [BOND_OPT_AD_SELECT] = {
286 .id = BOND_OPT_AD_SELECT,
287 .name = "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
292 },
ef56becb
NA
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
299 },
b98d9c66
NA
300 [BOND_OPT_MIIMON] = {
301 .id = BOND_OPT_MIIMON,
302 .name = "miimon",
303 .desc = "Link check interval in milliseconds",
304 .values = bond_intmax_tbl,
305 .set = bond_option_miimon_set
306 },
180222f0
NA
307 [BOND_OPT_PRIMARY] = {
308 .id = BOND_OPT_PRIMARY,
309 .name = "primary",
310 .desc = "Primary network device to use",
311 .flags = BOND_OPTFLAG_RAWVAL,
312 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
313 BIT(BOND_MODE_TLB) |
314 BIT(BOND_MODE_ALB)),
315 .set = bond_option_primary_set
316 },
388d3a6d
NA
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
323 },
0fff0608
NA
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
330 },
d1fbd3ed
NA
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) |
337 BIT(BOND_MODE_TLB) |
338 BIT(BOND_MODE_ALB)),
339 .set = bond_option_active_slave_set
340 },
24089ba1
NA
341 [BOND_OPT_QUEUE_ID] = {
342 .id = BOND_OPT_QUEUE_ID,
343 .name = "queue_id",
344 .desc = "Set queue id of a slave",
345 .flags = BOND_OPTFLAG_RAWVAL,
346 .set = bond_option_queue_id_set
347 },
3df01162
NA
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
354 },
105c8fb6
NA
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
361 },
4325b374
NA
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
368 },
0e2e5b66
NA
369 [BOND_OPT_SLAVES] = {
370 .id = BOND_OPT_SLAVES,
371 .name = "slaves",
372 .desc = "Slave membership management",
373 .flags = BOND_OPTFLAG_RAWVAL,
374 .set = bond_option_slaves_set
375 },
e9f0fb88
MB
376 [BOND_OPT_TLB_DYNAMIC_LB] = {
377 .id = BOND_OPT_TLB_DYNAMIC_LB,
dc3e5d18 378 .name = "tlb_dynamic_lb",
e9f0fb88
MB
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,
384 },
09117362
NA
385 { }
386};
387
dc3e5d18
NA
388/* Searches for an option by name */
389const struct bond_option *bond_opt_get_by_name(const char *name)
390{
391 const struct bond_option *opt;
392 int option;
393
394 for (option = 0; option < BOND_OPT_LAST; option++) {
395 opt = bond_opt_get(option);
396 if (opt && !strcmp(opt->name, name))
397 return opt;
398 }
399
400 return NULL;
401}
402
09117362 403/* Searches for a value in opt's values[] table */
f3253339 404const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
09117362 405{
f3253339 406 const struct bond_option *opt;
09117362
NA
407 int i;
408
409 opt = bond_opt_get(option);
410 if (WARN_ON(!opt))
411 return NULL;
412 for (i = 0; opt->values && opt->values[i].string; i++)
413 if (opt->values[i].value == val)
414 return &opt->values[i];
415
416 return NULL;
417}
418
419/* Searches for a value in opt's values[] table which matches the flagmask */
f3253339 420static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
09117362
NA
421 u32 flagmask)
422{
423 int i;
424
425 for (i = 0; opt->values && opt->values[i].string; i++)
426 if (opt->values[i].flags & flagmask)
427 return &opt->values[i];
428
429 return NULL;
430}
431
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.
434 */
435static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
436{
f3253339 437 const struct bond_opt_value *minval, *maxval;
09117362
NA
438
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)
442 return false;
443
444 return true;
445}
446
447/**
448 * bond_opt_parse - parse option value
449 * @opt: the option to parse against
450 * @val: value to parse
451 *
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.
456 */
f3253339 457const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
458 struct bond_opt_value *val)
09117362
NA
459{
460 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
f3253339 461 const struct bond_opt_value *tbl;
462 const struct bond_opt_value *ret = NULL;
09117362
NA
463 bool checkval;
464 int i, rv;
465
466 /* No parsing if the option wants a raw val */
467 if (opt->flags & BOND_OPTFLAG_RAWVAL)
468 return val;
469
470 tbl = opt->values;
471 if (!tbl)
472 goto out;
473
474 /* ULLONG_MAX is used to bypass string processing */
475 checkval = val->value != ULLONG_MAX;
476 if (!checkval) {
477 if (!val->string)
478 goto out;
479 p = strchr(val->string, '\n');
480 if (p)
481 *p = '\0';
482 for (p = val->string; *p; p++)
483 if (!(isdigit(*p) || isspace(*p)))
484 break;
485 /* The following code extracts the string to match or the value
486 * and sets checkval appropriately
487 */
488 if (*p) {
489 rv = sscanf(val->string, "%32s", valstr);
490 } else {
491 rv = sscanf(val->string, "%llu", &val->value);
492 checkval = true;
493 }
494 if (!rv)
495 goto out;
496 }
497
498 for (i = 0; tbl[i].string; i++) {
499 /* Check for exact match */
500 if (checkval) {
501 if (val->value == tbl[i].value)
502 ret = &tbl[i];
503 } else {
504 if (!strcmp(valstr, "default") &&
505 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
506 ret = &tbl[i];
507
508 if (!strcmp(valstr, tbl[i].string))
509 ret = &tbl[i];
510 }
511 /* Found an exact match */
512 if (ret)
513 goto out;
514 }
515 /* Possible range match */
516 if (checkval && bond_opt_check_range(opt, val->value))
517 ret = val;
518out:
519 return ret;
520}
521
522/* Check opt's dependencies against bond mode and currently set options */
523static int bond_opt_check_deps(struct bonding *bond,
524 const struct bond_option *opt)
525{
526 struct bond_params *params = &bond->params;
527
528 if (test_bit(params->mode, &opt->unsuppmodes))
529 return -EACCES;
530 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
531 return -ENOTEMPTY;
532 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
533 return -EBUSY;
534
535 return 0;
536}
537
538static void bond_opt_dep_print(struct bonding *bond,
539 const struct bond_option *opt)
540{
28f084cc 541 const struct bond_opt_value *modeval;
09117362
NA
542 struct bond_params *params;
543
544 params = &bond->params;
2b3798d5 545 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
09117362 546 if (test_bit(params->mode, &opt->unsuppmodes))
2b3798d5
NA
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);
09117362
NA
550}
551
552static void bond_opt_error_interpret(struct bonding *bond,
553 const struct bond_option *opt,
28f084cc 554 int error, const struct bond_opt_value *val)
09117362 555{
28f084cc 556 const struct bond_opt_value *minval, *maxval;
09117362
NA
557 char *p;
558
559 switch (error) {
560 case -EINVAL:
561 if (val) {
562 if (val->string) {
563 /* sometimes RAWVAL opts may have new lines */
564 p = strchr(val->string, '\n');
565 if (p)
566 *p = '\0';
90194264 567 pr_err("%s: option %s: invalid value (%s)\n",
09117362
NA
568 bond->dev->name, opt->name, val->string);
569 } else {
90194264 570 pr_err("%s: option %s: invalid value (%llu)\n",
09117362
NA
571 bond->dev->name, opt->name, val->value);
572 }
573 }
574 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
575 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
576 if (!maxval)
577 break;
90194264 578 pr_err("%s: option %s: allowed values %llu - %llu\n",
09117362
NA
579 bond->dev->name, opt->name, minval ? minval->value : 0,
580 maxval->value);
581 break;
582 case -EACCES:
583 bond_opt_dep_print(bond, opt);
584 break;
585 case -ENOTEMPTY:
90194264 586 pr_err("%s: option %s: unable to set because the bond device has slaves\n",
09117362
NA
587 bond->dev->name, opt->name);
588 break;
589 case -EBUSY:
90194264 590 pr_err("%s: option %s: unable to set because the bond device is up\n",
09117362
NA
591 bond->dev->name, opt->name);
592 break;
593 default:
594 break;
595 }
596}
597
598/**
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
603 *
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.
607 */
608int __bond_opt_set(struct bonding *bond,
609 unsigned int option, struct bond_opt_value *val)
610{
28f084cc 611 const struct bond_opt_value *retval = NULL;
09117362
NA
612 const struct bond_option *opt;
613 int ret = -ENOENT;
614
615 ASSERT_RTNL();
616
617 opt = bond_opt_get(option);
618 if (WARN_ON(!val) || WARN_ON(!opt))
619 goto out;
620 ret = bond_opt_check_deps(bond, opt);
621 if (ret)
622 goto out;
623 retval = bond_opt_parse(opt, val);
624 if (!retval) {
625 ret = -EINVAL;
626 goto out;
627 }
628 ret = opt->set(bond, retval);
629out:
630 if (ret)
631 bond_opt_error_interpret(bond, opt, ret, val);
632
633 return ret;
634}
635
636/**
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
641 *
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.
644 */
645int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
646{
647 struct bond_opt_value optval;
648 int ret;
649
650 if (!rtnl_trylock())
651 return restart_syscall();
652 bond_opt_initstr(&optval, buf);
653 ret = __bond_opt_set(bond, option, &optval);
654 rtnl_unlock();
655
656 return ret;
657}
658
659/**
660 * bond_opt_get - get a pointer to an option
661 * @option: option for which to return a pointer
662 *
663 * This function checks if option is valid and if so returns a pointer
664 * to its entry in the bond_opts[] option array.
665 */
f3253339 666const struct bond_option *bond_opt_get(unsigned int option)
09117362
NA
667{
668 if (!BOND_OPT_VALID(option))
669 return NULL;
670
671 return &bond_opts[option];
672}
673
28f084cc 674int bond_option_mode_set(struct bonding *bond, const struct bond_opt_value *newval)
72be35fe 675{
267bed77 676 if (!bond_mode_uses_arp(newval->value) && bond->params.arp_interval) {
fe9d04af 677 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
2b3798d5 678 bond->dev->name, newval->string);
fe9d04af 679 /* disable arp monitoring */
680 bond->params.arp_interval = 0;
681 /* set miimon to default value */
682 bond->params.miimon = BOND_DEFAULT_MIIMON;
90194264 683 pr_info("%s: Setting MII monitoring interval to %d\n",
fe9d04af 684 bond->dev->name, bond->params.miimon);
72be35fe
JP
685 }
686
687 /* don't cache arp_validate between modes */
688 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
2b3798d5
NA
689 bond->params.mode = newval->value;
690
72be35fe
JP
691 return 0;
692}
d9e32b21 693
752d48b5
JP
694static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
695 struct slave *slave)
696{
ec0865a9 697 return bond_uses_primary(bond) && slave ? slave->dev : NULL;
752d48b5
JP
698}
699
700struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
701{
702 struct slave *slave = rcu_dereference(bond->curr_active_slave);
703
704 return __bond_option_active_slave_get(bond, slave);
705}
706
f3253339 707static int bond_option_active_slave_set(struct bonding *bond,
28f084cc 708 const struct bond_opt_value *newval)
d9e32b21 709{
d1fbd3ed
NA
710 char ifname[IFNAMSIZ] = { 0, };
711 struct net_device *slave_dev;
d9e32b21
JP
712 int ret = 0;
713
d1fbd3ed
NA
714 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
715 if (!strlen(ifname) || newval->string[0] == '\n') {
716 slave_dev = NULL;
717 } else {
718 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
719 if (!slave_dev)
720 return -ENODEV;
721 }
722
d9e32b21
JP
723 if (slave_dev) {
724 if (!netif_is_bond_slave(slave_dev)) {
90194264 725 pr_err("Device %s is not bonding slave\n",
d9e32b21
JP
726 slave_dev->name);
727 return -EINVAL;
728 }
729
730 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
90194264 731 pr_err("%s: Device %s is not our slave\n",
d9e32b21
JP
732 bond->dev->name, slave_dev->name);
733 return -EINVAL;
734 }
735 }
736
d9e32b21 737 block_netpoll_tx();
d9e32b21
JP
738 write_lock_bh(&bond->curr_slave_lock);
739
740 /* check to see if we are clearing active */
741 if (!slave_dev) {
90194264 742 pr_info("%s: Clearing current active slave\n", bond->dev->name);
8800a244 743 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
d9e32b21
JP
744 bond_select_active_slave(bond);
745 } else {
4740d638 746 struct slave *old_active = bond_deref_active_protected(bond);
d9e32b21
JP
747 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
748
749 BUG_ON(!new_active);
750
751 if (new_active == old_active) {
752 /* do nothing */
90194264 753 pr_info("%s: %s is already the current active slave\n",
d9e32b21
JP
754 bond->dev->name, new_active->dev->name);
755 } else {
756 if (old_active && (new_active->link == BOND_LINK_UP) &&
b6adc610 757 bond_slave_is_up(new_active)) {
90194264 758 pr_info("%s: Setting %s as active slave\n",
d9e32b21
JP
759 bond->dev->name, new_active->dev->name);
760 bond_change_active_slave(bond, new_active);
761 } else {
90194264 762 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down\n",
d9e32b21
JP
763 bond->dev->name, new_active->dev->name,
764 new_active->dev->name);
765 ret = -EINVAL;
766 }
767 }
768 }
769
770 write_unlock_bh(&bond->curr_slave_lock);
d9e32b21 771 unblock_netpoll_tx();
d1fbd3ed 772
d9e32b21
JP
773 return ret;
774}
eecdaa6e 775
dc3e5d18
NA
776/* There are two tricky bits here. First, if MII monitoring is activated, then
777 * we must disable ARP monitoring. Second, if the timer isn't running, we must
778 * start it.
779 */
f3253339 780static int bond_option_miimon_set(struct bonding *bond,
28f084cc 781 const struct bond_opt_value *newval)
eecdaa6e 782{
90194264 783 pr_info("%s: Setting MII monitoring interval to %llu\n",
b98d9c66
NA
784 bond->dev->name, newval->value);
785 bond->params.miimon = newval->value;
eecdaa6e 786 if (bond->params.updelay)
90194264 787 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
eecdaa6e 788 bond->dev->name,
789 bond->params.updelay * bond->params.miimon);
790 if (bond->params.downdelay)
90194264 791 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
eecdaa6e 792 bond->dev->name,
793 bond->params.downdelay * bond->params.miimon);
b98d9c66 794 if (newval->value && bond->params.arp_interval) {
90194264 795 pr_info("%s: MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n",
eecdaa6e 796 bond->dev->name);
797 bond->params.arp_interval = 0;
798 if (bond->params.arp_validate)
799 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
800 }
801 if (bond->dev->flags & IFF_UP) {
802 /* If the interface is up, we may need to fire off
803 * the MII timer. If the interface is down, the
804 * timer will get fired off when the open function
805 * is called.
806 */
b98d9c66 807 if (!newval->value) {
eecdaa6e 808 cancel_delayed_work_sync(&bond->mii_work);
809 } else {
810 cancel_delayed_work_sync(&bond->arp_work);
811 queue_delayed_work(bond->wq, &bond->mii_work, 0);
812 }
813 }
b98d9c66 814
eecdaa6e 815 return 0;
816}
25852e29 817
dc3e5d18
NA
818/* Set up and down delays. These must be multiples of the
819 * MII monitoring value, and are stored internally as the multiplier.
820 * Thus, we must translate to MS for the real world.
821 */
f3253339 822static int bond_option_updelay_set(struct bonding *bond,
28f084cc 823 const struct bond_opt_value *newval)
25852e29 824{
0681a282
NA
825 int value = newval->value;
826
e4994612 827 if (!bond->params.miimon) {
25852e29 828 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
829 bond->dev->name);
830 return -EPERM;
831 }
0681a282
NA
832 if ((value % bond->params.miimon) != 0) {
833 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
834 bond->dev->name, value,
e4994612 835 bond->params.miimon,
0681a282 836 (value / bond->params.miimon) *
e4994612 837 bond->params.miimon);
25852e29 838 }
0681a282 839 bond->params.updelay = value / bond->params.miimon;
90194264
JP
840 pr_info("%s: Setting up delay to %d\n",
841 bond->dev->name, bond->params.updelay * bond->params.miimon);
25852e29 842
843 return 0;
844}
c7461f9b 845
f3253339 846static int bond_option_downdelay_set(struct bonding *bond,
28f084cc 847 const struct bond_opt_value *newval)
c7461f9b 848{
0681a282
NA
849 int value = newval->value;
850
25a9b54a 851 if (!bond->params.miimon) {
c7461f9b 852 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
853 bond->dev->name);
854 return -EPERM;
855 }
0681a282
NA
856 if ((value % bond->params.miimon) != 0) {
857 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
858 bond->dev->name, value,
25a9b54a 859 bond->params.miimon,
0681a282 860 (value / bond->params.miimon) *
25a9b54a 861 bond->params.miimon);
c7461f9b 862 }
0681a282 863 bond->params.downdelay = value / bond->params.miimon;
90194264
JP
864 pr_info("%s: Setting down delay to %d\n",
865 bond->dev->name, bond->params.downdelay * bond->params.miimon);
c7461f9b 866
867 return 0;
868}
9f53e14e 869
f3253339 870static int bond_option_use_carrier_set(struct bonding *bond,
28f084cc 871 const struct bond_opt_value *newval)
9f53e14e 872{
90194264 873 pr_info("%s: Setting use_carrier to %llu\n",
0fff0608
NA
874 bond->dev->name, newval->value);
875 bond->params.use_carrier = newval->value;
9f53e14e 876
877 return 0;
878}
06151dbc 879
dc3e5d18
NA
880/* There are two tricky bits here. First, if ARP monitoring is activated, then
881 * we must disable MII monitoring. Second, if the ARP timer isn't running,
882 * we must start it.
883 */
f3253339 884static int bond_option_arp_interval_set(struct bonding *bond,
28f084cc 885 const struct bond_opt_value *newval)
06151dbc 886{
90194264 887 pr_info("%s: Setting ARP monitoring interval to %llu\n",
7bdb04ed
NA
888 bond->dev->name, newval->value);
889 bond->params.arp_interval = newval->value;
890 if (newval->value) {
06151dbc 891 if (bond->params.miimon) {
90194264 892 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring\n",
06151dbc 893 bond->dev->name, bond->dev->name);
894 bond->params.miimon = 0;
895 }
896 if (!bond->params.arp_targets[0])
90194264 897 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified\n",
06151dbc 898 bond->dev->name);
899 }
900 if (bond->dev->flags & IFF_UP) {
901 /* If the interface is up, we may need to fire off
902 * the ARP timer. If the interface is down, the
903 * timer will get fired off when the open function
904 * is called.
905 */
7bdb04ed 906 if (!newval->value) {
06151dbc 907 if (bond->params.arp_validate)
908 bond->recv_probe = NULL;
909 cancel_delayed_work_sync(&bond->arp_work);
910 } else {
911 /* arp_validate can be set only in active-backup mode */
3fe68df9 912 bond->recv_probe = bond_arp_rcv;
06151dbc 913 cancel_delayed_work_sync(&bond->mii_work);
914 queue_delayed_work(bond->wq, &bond->arp_work, 0);
915 }
916 }
917
918 return 0;
919}
7f28fa10 920
921static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
922 __be32 target,
923 unsigned long last_rx)
924{
925 __be32 *targets = bond->params.arp_targets;
926 struct list_head *iter;
927 struct slave *slave;
928
929 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
930 bond_for_each_slave(bond, slave, iter)
931 slave->target_last_arp_rx[slot] = last_rx;
932 targets[slot] = target;
933 }
934}
935
936static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
937{
938 __be32 *targets = bond->params.arp_targets;
939 int ind;
940
2807a9fe 941 if (!bond_is_ip_target_ok(target)) {
7f28fa10 942 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
943 bond->dev->name, &target);
944 return -EINVAL;
945 }
946
947 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
948 pr_err("%s: ARP target %pI4 is already present\n",
949 bond->dev->name, &target);
950 return -EINVAL;
951 }
952
953 ind = bond_get_targets_ip(targets, 0); /* first free slot */
954 if (ind == -1) {
90194264 955 pr_err("%s: ARP target table is full!\n", bond->dev->name);
7f28fa10 956 return -EINVAL;
957 }
958
90194264 959 pr_info("%s: Adding ARP target %pI4\n", bond->dev->name, &target);
7f28fa10 960
961 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
962
963 return 0;
964}
965
f3253339 966static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
7f28fa10 967{
968 int ret;
969
970 /* not to race with bond_arp_rcv */
971 write_lock_bh(&bond->lock);
972 ret = _bond_option_arp_ip_target_add(bond, target);
973 write_unlock_bh(&bond->lock);
974
975 return ret;
976}
977
f3253339 978static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
7f28fa10 979{
980 __be32 *targets = bond->params.arp_targets;
981 struct list_head *iter;
982 struct slave *slave;
983 unsigned long *targets_rx;
984 int ind, i;
985
2807a9fe 986 if (!bond_is_ip_target_ok(target)) {
7f28fa10 987 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
988 bond->dev->name, &target);
989 return -EINVAL;
990 }
991
992 ind = bond_get_targets_ip(targets, target);
993 if (ind == -1) {
90194264 994 pr_err("%s: unable to remove nonexistent ARP target %pI4\n",
7f28fa10 995 bond->dev->name, &target);
996 return -EINVAL;
997 }
998
999 if (ind == 0 && !targets[1] && bond->params.arp_interval)
90194264 1000 pr_warn("%s: Removing last arp target with arp_interval on\n",
7f28fa10 1001 bond->dev->name);
1002
90194264 1003 pr_info("%s: Removing ARP target %pI4\n", bond->dev->name, &target);
7f28fa10 1004
1005 /* not to race with bond_arp_rcv */
1006 write_lock_bh(&bond->lock);
1007
1008 bond_for_each_slave(bond, slave, iter) {
1009 targets_rx = slave->target_last_arp_rx;
1010 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1011 targets_rx[i] = targets_rx[i+1];
1012 targets_rx[i] = 0;
1013 }
1014 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1015 targets[i] = targets[i+1];
1016 targets[i] = 0;
1017
1018 write_unlock_bh(&bond->lock);
1019
1020 return 0;
1021}
1022
4fb0ef58 1023void bond_option_arp_ip_targets_clear(struct bonding *bond)
7f28fa10 1024{
4fb0ef58 1025 int i;
7f28fa10 1026
1027 /* not to race with bond_arp_rcv */
1028 write_lock_bh(&bond->lock);
7f28fa10 1029 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1030 _bond_options_arp_ip_target_set(bond, i, 0, 0);
4fb0ef58
NA
1031 write_unlock_bh(&bond->lock);
1032}
7f28fa10 1033
f3253339 1034static int bond_option_arp_ip_targets_set(struct bonding *bond,
28f084cc 1035 const struct bond_opt_value *newval)
4fb0ef58
NA
1036{
1037 int ret = -EPERM;
1038 __be32 target;
1039
1040 if (newval->string) {
1041 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1042 pr_err("%s: invalid ARP target %pI4 specified\n",
1043 bond->dev->name, &target);
1044 return ret;
1045 }
1046 if (newval->string[0] == '+')
1047 ret = bond_option_arp_ip_target_add(bond, target);
1048 else if (newval->string[0] == '-')
1049 ret = bond_option_arp_ip_target_rem(bond, target);
1050 else
90194264 1051 pr_err("no command found in arp_ip_targets file for bond %s - use +<addr> or -<addr>\n",
4fb0ef58
NA
1052 bond->dev->name);
1053 } else {
1054 target = newval->value;
1055 ret = bond_option_arp_ip_target_add(bond, target);
7f28fa10 1056 }
1057
7f28fa10 1058 return ret;
1059}
29c49482 1060
f3253339 1061static int bond_option_arp_validate_set(struct bonding *bond,
28f084cc 1062 const struct bond_opt_value *newval)
29c49482 1063{
90194264 1064 pr_info("%s: Setting arp_validate to %s (%llu)\n",
16228881 1065 bond->dev->name, newval->string, newval->value);
29c49482 1066
1067 if (bond->dev->flags & IFF_UP) {
16228881 1068 if (!newval->value)
29c49482 1069 bond->recv_probe = NULL;
1070 else if (bond->params.arp_interval)
1071 bond->recv_probe = bond_arp_rcv;
1072 }
16228881 1073 bond->params.arp_validate = newval->value;
29c49482 1074
1075 return 0;
1076}
d5c84254 1077
f3253339 1078static int bond_option_arp_all_targets_set(struct bonding *bond,
28f084cc 1079 const struct bond_opt_value *newval)
d5c84254 1080{
90194264 1081 pr_info("%s: Setting arp_all_targets to %s (%llu)\n",
edf36b24
NA
1082 bond->dev->name, newval->string, newval->value);
1083 bond->params.arp_all_targets = newval->value;
d5c84254 1084
1085 return 0;
1086}
0a98a0d1 1087
f3253339 1088static int bond_option_primary_set(struct bonding *bond,
28f084cc 1089 const struct bond_opt_value *newval)
0a98a0d1 1090{
180222f0 1091 char *p, *primary = newval->string;
0a98a0d1 1092 struct list_head *iter;
1093 struct slave *slave;
0a98a0d1 1094
1095 block_netpoll_tx();
1096 read_lock(&bond->lock);
1097 write_lock_bh(&bond->curr_slave_lock);
1098
180222f0
NA
1099 p = strchr(primary, '\n');
1100 if (p)
1101 *p = '\0';
0a98a0d1 1102 /* check to see if we are clearing primary */
1103 if (!strlen(primary)) {
90194264 1104 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
0a98a0d1 1105 bond->primary_slave = NULL;
1106 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1107 bond_select_active_slave(bond);
1108 goto out;
1109 }
1110
1111 bond_for_each_slave(bond, slave, iter) {
1112 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
90194264 1113 pr_info("%s: Setting %s as primary slave\n",
0a98a0d1 1114 bond->dev->name, slave->dev->name);
1115 bond->primary_slave = slave;
1116 strcpy(bond->params.primary, slave->dev->name);
1117 bond_select_active_slave(bond);
1118 goto out;
1119 }
1120 }
1121
c59ab673 1122 if (bond->primary_slave) {
90194264 1123 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
c59ab673 1124 bond->primary_slave = NULL;
1125 bond_select_active_slave(bond);
1126 }
0a98a0d1 1127 strncpy(bond->params.primary, primary, IFNAMSIZ);
1128 bond->params.primary[IFNAMSIZ - 1] = 0;
1129
90194264 1130 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet\n",
0a98a0d1 1131 bond->dev->name, primary, bond->dev->name);
1132
1133out:
1134 write_unlock_bh(&bond->curr_slave_lock);
1135 read_unlock(&bond->lock);
1136 unblock_netpoll_tx();
1137
180222f0 1138 return 0;
0a98a0d1 1139}
8a41ae44 1140
f3253339 1141static int bond_option_primary_reselect_set(struct bonding *bond,
28f084cc 1142 const struct bond_opt_value *newval)
8a41ae44 1143{
90194264 1144 pr_info("%s: Setting primary_reselect to %s (%llu)\n",
388d3a6d
NA
1145 bond->dev->name, newval->string, newval->value);
1146 bond->params.primary_reselect = newval->value;
8a41ae44 1147
1148 block_netpoll_tx();
1149 write_lock_bh(&bond->curr_slave_lock);
1150 bond_select_active_slave(bond);
1151 write_unlock_bh(&bond->curr_slave_lock);
1152 unblock_netpoll_tx();
1153
1154 return 0;
1155}
89901972 1156
f3253339 1157static int bond_option_fail_over_mac_set(struct bonding *bond,
28f084cc 1158 const struct bond_opt_value *newval)
89901972 1159{
90194264 1160 pr_info("%s: Setting fail_over_mac to %s (%llu)\n",
1df6b6aa
NA
1161 bond->dev->name, newval->string, newval->value);
1162 bond->params.fail_over_mac = newval->value;
89901972 1163
1164 return 0;
1165}
f70161c6 1166
f3253339 1167static int bond_option_xmit_hash_policy_set(struct bonding *bond,
28f084cc 1168 const struct bond_opt_value *newval)
f70161c6 1169{
90194264 1170 pr_info("%s: Setting xmit hash policy to %s (%llu)\n",
a4b32ce7
NA
1171 bond->dev->name, newval->string, newval->value);
1172 bond->params.xmit_policy = newval->value;
f70161c6 1173
1174 return 0;
1175}
d8838de7 1176
f3253339 1177static int bond_option_resend_igmp_set(struct bonding *bond,
28f084cc 1178 const struct bond_opt_value *newval)
d8838de7 1179{
90194264 1180 pr_info("%s: Setting resend_igmp to %llu\n",
105c8fb6
NA
1181 bond->dev->name, newval->value);
1182 bond->params.resend_igmp = newval->value;
d8838de7 1183
1184 return 0;
1185}
2c9839c1 1186
f3253339 1187static int bond_option_num_peer_notif_set(struct bonding *bond,
28f084cc 1188 const struct bond_opt_value *newval)
2c9839c1 1189{
ef56becb
NA
1190 bond->params.num_peer_notif = newval->value;
1191
2c9839c1 1192 return 0;
1193}
1cc0b1e3 1194
f3253339 1195static int bond_option_all_slaves_active_set(struct bonding *bond,
28f084cc 1196 const struct bond_opt_value *newval)
1cc0b1e3 1197{
1198 struct list_head *iter;
1199 struct slave *slave;
1200
3df01162 1201 if (newval->value == bond->params.all_slaves_active)
1cc0b1e3 1202 return 0;
3df01162 1203 bond->params.all_slaves_active = newval->value;
1cc0b1e3 1204 bond_for_each_slave(bond, slave, iter) {
1205 if (!bond_is_active_slave(slave)) {
3df01162 1206 if (newval->value)
1cc0b1e3 1207 slave->inactive = 0;
1208 else
1209 slave->inactive = 1;
1210 }
1211 }
1212
1213 return 0;
1214}
7d101008 1215
f3253339 1216static int bond_option_min_links_set(struct bonding *bond,
28f084cc 1217 const struct bond_opt_value *newval)
7d101008 1218{
633ddc9e
NA
1219 pr_info("%s: Setting min links value to %llu\n",
1220 bond->dev->name, newval->value);
1221 bond->params.min_links = newval->value;
7d101008 1222
1223 return 0;
1224}
8d836d09 1225
f3253339 1226static int bond_option_lp_interval_set(struct bonding *bond,
28f084cc 1227 const struct bond_opt_value *newval)
8d836d09 1228{
4325b374 1229 bond->params.lp_interval = newval->value;
8d836d09 1230
1231 return 0;
1232}
c13ab3ff 1233
f3253339 1234static int bond_option_pps_set(struct bonding *bond,
28f084cc 1235 const struct bond_opt_value *newval)
c13ab3ff 1236{
aa59d851
NA
1237 bond->params.packets_per_slave = newval->value;
1238 if (newval->value > 0) {
809fa972 1239 bond->params.reciprocal_packets_per_slave =
aa59d851 1240 reciprocal_value(newval->value);
809fa972
HFS
1241 } else {
1242 /* reciprocal_packets_per_slave is unused if
1243 * packets_per_slave is 0 or 1, just initialize it
1244 */
1245 bond->params.reciprocal_packets_per_slave =
1246 (struct reciprocal_value) { 0 };
1247 }
c13ab3ff 1248
1249 return 0;
1250}
998e40bb 1251
f3253339 1252static int bond_option_lacp_rate_set(struct bonding *bond,
28f084cc 1253 const struct bond_opt_value *newval)
998e40bb 1254{
90194264 1255 pr_info("%s: Setting LACP rate to %s (%llu)\n",
d3131de7
NA
1256 bond->dev->name, newval->string, newval->value);
1257 bond->params.lacp_fast = newval->value;
3243c47b 1258 bond_3ad_update_lacp_rate(bond);
998e40bb 1259
1260 return 0;
1261}
ec029fac 1262
f3253339 1263static int bond_option_ad_select_set(struct bonding *bond,
28f084cc 1264 const struct bond_opt_value *newval)
ec029fac 1265{
90194264 1266 pr_info("%s: Setting ad_select to %s (%llu)\n",
9e5f5eeb
NA
1267 bond->dev->name, newval->string, newval->value);
1268 bond->params.ad_select = newval->value;
ec029fac 1269
1270 return 0;
1271}
24089ba1 1272
f3253339 1273static int bond_option_queue_id_set(struct bonding *bond,
28f084cc 1274 const struct bond_opt_value *newval)
24089ba1
NA
1275{
1276 struct slave *slave, *update_slave;
1277 struct net_device *sdev;
1278 struct list_head *iter;
1279 char *delim;
1280 int ret = 0;
1281 u16 qid;
1282
1283 /* delim will point to queue id if successful */
1284 delim = strchr(newval->string, ':');
1285 if (!delim)
1286 goto err_no_cmd;
1287
1288 /* Terminate string that points to device name and bump it
1289 * up one, so we can read the queue id there.
1290 */
1291 *delim = '\0';
1292 if (sscanf(++delim, "%hd\n", &qid) != 1)
1293 goto err_no_cmd;
1294
1295 /* Check buffer length, valid ifname and queue id */
c313c89e 1296 if (!dev_valid_name(newval->string) ||
24089ba1
NA
1297 qid > bond->dev->real_num_tx_queues)
1298 goto err_no_cmd;
1299
1300 /* Get the pointer to that interface if it exists */
1301 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1302 if (!sdev)
1303 goto err_no_cmd;
1304
1305 /* Search for thes slave and check for duplicate qids */
1306 update_slave = NULL;
1307 bond_for_each_slave(bond, slave, iter) {
1308 if (sdev == slave->dev)
1309 /* We don't need to check the matching
1310 * slave for dups, since we're overwriting it
1311 */
1312 update_slave = slave;
1313 else if (qid && qid == slave->queue_id) {
1314 goto err_no_cmd;
1315 }
1316 }
1317
1318 if (!update_slave)
1319 goto err_no_cmd;
1320
1321 /* Actually set the qids for the slave */
1322 update_slave->queue_id = qid;
1323
1324out:
1325 return ret;
1326
1327err_no_cmd:
90194264 1328 pr_info("invalid input for queue_id set for %s\n", bond->dev->name);
24089ba1
NA
1329 ret = -EPERM;
1330 goto out;
1331
1332}
0e2e5b66 1333
f3253339 1334static int bond_option_slaves_set(struct bonding *bond,
28f084cc 1335 const struct bond_opt_value *newval)
0e2e5b66
NA
1336{
1337 char command[IFNAMSIZ + 1] = { 0, };
1338 struct net_device *dev;
1339 char *ifname;
1340 int ret;
1341
1342 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1343 ifname = command + 1;
1344 if ((strlen(command) <= 1) ||
1345 !dev_valid_name(ifname))
1346 goto err_no_cmd;
1347
1348 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1349 if (!dev) {
90194264 1350 pr_info("%s: interface %s does not exist!\n",
0e2e5b66
NA
1351 bond->dev->name, ifname);
1352 ret = -ENODEV;
1353 goto out;
1354 }
1355
1356 switch (command[0]) {
1357 case '+':
90194264 1358 pr_info("%s: Adding slave %s\n", bond->dev->name, dev->name);
0e2e5b66
NA
1359 ret = bond_enslave(bond->dev, dev);
1360 break;
1361
1362 case '-':
90194264 1363 pr_info("%s: Removing slave %s\n", bond->dev->name, dev->name);
0e2e5b66
NA
1364 ret = bond_release(bond->dev, dev);
1365 break;
1366
1367 default:
1368 goto err_no_cmd;
1369 }
1370
1371out:
1372 return ret;
1373
1374err_no_cmd:
90194264 1375 pr_err("no command found in slaves file for bond %s - use +ifname or -ifname\n",
0e2e5b66
NA
1376 bond->dev->name);
1377 ret = -EPERM;
1378 goto out;
1379}
e9f0fb88
MB
1380
1381static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1382 const struct bond_opt_value *newval)
1383{
1384 pr_info("%s: Setting dynamic-lb to %s (%llu)\n",
1385 bond->dev->name, newval->string, newval->value);
1386 bond->params.tlb_dynamic_lb = newval->value;
1387
1388 return 0;
1389}