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