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