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