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