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