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