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