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