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