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