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