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