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