blob: f3eb44d2e2316dd69c44bdcd9ccd1a6f1ea5db39 [file] [log] [blame]
Jiri Pirko72be35f2013-10-18 17:43:34 +02001/*
2 * drivers/net/bond/bond_options.c - bonding options
3 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -08004 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
Jiri Pirko72be35f2013-10-18 17:43:34 +02005 *
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>
Jiri Pirkod9e32b22013-10-18 17:43:35 +020016#include <linux/netdevice.h>
17#include <linux/rwlock.h>
18#include <linux/rcupdate.h>
Nikolay Aleksandrov09117362014-01-22 14:53:16 +010019#include <linux/ctype.h>
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +010020#include <linux/inet.h>
Jiri Pirko72be35f2013-10-18 17:43:34 +020021#include "bonding.h"
22
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +010023static 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
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +010034static 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
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +010040static 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
Nikolay Aleksandrov16228882014-01-22 14:53:20 +010049static 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
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +010057static 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
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +010063static 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
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +010070static struct bond_opt_value bond_intmax_tbl[] = {
71 { "off", 0, BOND_VALFLAG_DEFAULT},
72 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
73};
74
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +010075static struct bond_opt_value bond_lacp_rate_tbl[] = {
76 { "slow", AD_LACP_SLOW, 0},
77 { "fast", AD_LACP_FAST, 0},
78 { NULL, -1, 0},
79};
80
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +010081static struct bond_opt_value bond_ad_select_tbl[] = {
82 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
83 { "bandwidth", BOND_AD_BANDWIDTH, 0},
84 { "count", BOND_AD_COUNT, 0},
85 { NULL, -1, 0},
86};
87
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +010088static struct bond_opt_value bond_num_peer_notif_tbl[] = {
89 { "off", 0, 0},
90 { "maxval", 255, BOND_VALFLAG_MAX},
91 { "default", 1, BOND_VALFLAG_DEFAULT},
92 { NULL, -1, 0}
93};
94
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +010095static struct bond_opt_value bond_primary_reselect_tbl[] = {
96 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
97 { "better", BOND_PRI_RESELECT_BETTER, 0},
98 { "failure", BOND_PRI_RESELECT_FAILURE, 0},
99 { NULL, -1},
100};
101
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100102static struct bond_opt_value bond_use_carrier_tbl[] = {
103 { "off", 0, 0},
104 { "on", 1, BOND_VALFLAG_DEFAULT},
105 { NULL, -1, 0}
106};
107
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +0100108static struct bond_opt_value bond_all_slaves_active_tbl[] = {
109 { "off", 0, BOND_VALFLAG_DEFAULT},
110 { "on", 1, 0},
111 { NULL, -1, 0}
112};
113
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +0100114static struct bond_opt_value bond_resend_igmp_tbl[] = {
115 { "off", 0, 0},
116 { "maxval", 255, BOND_VALFLAG_MAX},
117 { "default", 1, BOND_VALFLAG_DEFAULT},
118 { NULL, -1, 0}
119};
120
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100121static struct bond_opt_value bond_lp_interval_tbl[] = {
122 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
123 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
124};
125
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100126static struct bond_option bond_opts[] = {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100127 [BOND_OPT_MODE] = {
128 .id = BOND_OPT_MODE,
129 .name = "mode",
130 .desc = "bond device mode",
131 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
132 .values = bond_mode_tbl,
133 .set = bond_option_mode_set
134 },
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +0100135 [BOND_OPT_PACKETS_PER_SLAVE] = {
136 .id = BOND_OPT_PACKETS_PER_SLAVE,
137 .name = "packets_per_slave",
138 .desc = "Packets to send per slave in RR mode",
139 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
140 .values = bond_pps_tbl,
141 .set = bond_option_pps_set
142 },
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +0100143 [BOND_OPT_XMIT_HASH] = {
144 .id = BOND_OPT_XMIT_HASH,
145 .name = "xmit_hash_policy",
146 .desc = "balance-xor and 802.3ad hashing method",
147 .values = bond_xmit_hashtype_tbl,
148 .set = bond_option_xmit_hash_policy_set
149 },
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100150 [BOND_OPT_ARP_VALIDATE] = {
151 .id = BOND_OPT_ARP_VALIDATE,
152 .name = "arp_validate",
153 .desc = "validate src/dst of ARP probes",
154 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP)),
155 .values = bond_arp_validate_tbl,
156 .set = bond_option_arp_validate_set
157 },
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100158 [BOND_OPT_ARP_ALL_TARGETS] = {
159 .id = BOND_OPT_ARP_ALL_TARGETS,
160 .name = "arp_all_targets",
161 .desc = "fail on any/all arp targets timeout",
162 .values = bond_arp_all_targets_tbl,
163 .set = bond_option_arp_all_targets_set
164 },
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +0100165 [BOND_OPT_FAIL_OVER_MAC] = {
166 .id = BOND_OPT_FAIL_OVER_MAC,
167 .name = "fail_over_mac",
168 .desc = "For active-backup, do not set all slaves to the same MAC",
169 .flags = BOND_OPTFLAG_NOSLAVES,
170 .values = bond_fail_over_mac_tbl,
171 .set = bond_option_fail_over_mac_set
172 },
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100173 [BOND_OPT_ARP_INTERVAL] = {
174 .id = BOND_OPT_ARP_INTERVAL,
175 .name = "arp_interval",
176 .desc = "arp interval in milliseconds",
177 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
178 BIT(BOND_MODE_ALB),
179 .values = bond_intmax_tbl,
180 .set = bond_option_arp_interval_set
181 },
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100182 [BOND_OPT_ARP_TARGETS] = {
183 .id = BOND_OPT_ARP_TARGETS,
184 .name = "arp_ip_target",
185 .desc = "arp targets in n.n.n.n form",
186 .flags = BOND_OPTFLAG_RAWVAL,
187 .set = bond_option_arp_ip_targets_set
188 },
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100189 [BOND_OPT_DOWNDELAY] = {
190 .id = BOND_OPT_DOWNDELAY,
191 .name = "downdelay",
192 .desc = "Delay before considering link down, in milliseconds",
193 .values = bond_intmax_tbl,
194 .set = bond_option_downdelay_set
195 },
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100196 [BOND_OPT_UPDELAY] = {
197 .id = BOND_OPT_UPDELAY,
198 .name = "updelay",
199 .desc = "Delay before considering link up, in milliseconds",
200 .values = bond_intmax_tbl,
201 .set = bond_option_updelay_set
202 },
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +0100203 [BOND_OPT_LACP_RATE] = {
204 .id = BOND_OPT_LACP_RATE,
205 .name = "lacp_rate",
206 .desc = "LACPDU tx rate to request from 802.3ad partner",
207 .flags = BOND_OPTFLAG_IFDOWN,
208 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
209 .values = bond_lacp_rate_tbl,
210 .set = bond_option_lacp_rate_set
211 },
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +0100212 [BOND_OPT_MINLINKS] = {
213 .id = BOND_OPT_MINLINKS,
214 .name = "min_links",
215 .desc = "Minimum number of available links before turning on carrier",
216 .values = bond_intmax_tbl,
217 .set = bond_option_min_links_set
218 },
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +0100219 [BOND_OPT_AD_SELECT] = {
220 .id = BOND_OPT_AD_SELECT,
221 .name = "ad_select",
222 .desc = "803.ad aggregation selection logic",
223 .flags = BOND_OPTFLAG_IFDOWN,
224 .values = bond_ad_select_tbl,
225 .set = bond_option_ad_select_set
226 },
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +0100227 [BOND_OPT_NUM_PEER_NOTIF] = {
228 .id = BOND_OPT_NUM_PEER_NOTIF,
229 .name = "num_unsol_na",
230 .desc = "Number of peer notifications to send on failover event",
231 .values = bond_num_peer_notif_tbl,
232 .set = bond_option_num_peer_notif_set
233 },
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100234 [BOND_OPT_MIIMON] = {
235 .id = BOND_OPT_MIIMON,
236 .name = "miimon",
237 .desc = "Link check interval in milliseconds",
238 .values = bond_intmax_tbl,
239 .set = bond_option_miimon_set
240 },
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100241 [BOND_OPT_PRIMARY] = {
242 .id = BOND_OPT_PRIMARY,
243 .name = "primary",
244 .desc = "Primary network device to use",
245 .flags = BOND_OPTFLAG_RAWVAL,
246 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
247 BIT(BOND_MODE_TLB) |
248 BIT(BOND_MODE_ALB)),
249 .set = bond_option_primary_set
250 },
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +0100251 [BOND_OPT_PRIMARY_RESELECT] = {
252 .id = BOND_OPT_PRIMARY_RESELECT,
253 .name = "primary_reselect",
254 .desc = "Reselect primary slave once it comes up",
255 .values = bond_primary_reselect_tbl,
256 .set = bond_option_primary_reselect_set
257 },
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100258 [BOND_OPT_USE_CARRIER] = {
259 .id = BOND_OPT_USE_CARRIER,
260 .name = "use_carrier",
261 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
262 .values = bond_use_carrier_tbl,
263 .set = bond_option_use_carrier_set
264 },
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100265 [BOND_OPT_ACTIVE_SLAVE] = {
266 .id = BOND_OPT_ACTIVE_SLAVE,
267 .name = "active_slave",
268 .desc = "Currently active slave",
269 .flags = BOND_OPTFLAG_RAWVAL,
270 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
271 BIT(BOND_MODE_TLB) |
272 BIT(BOND_MODE_ALB)),
273 .set = bond_option_active_slave_set
274 },
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +0100275 [BOND_OPT_QUEUE_ID] = {
276 .id = BOND_OPT_QUEUE_ID,
277 .name = "queue_id",
278 .desc = "Set queue id of a slave",
279 .flags = BOND_OPTFLAG_RAWVAL,
280 .set = bond_option_queue_id_set
281 },
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +0100282 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
283 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
284 .name = "all_slaves_active",
285 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
286 .values = bond_all_slaves_active_tbl,
287 .set = bond_option_all_slaves_active_set
288 },
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +0100289 [BOND_OPT_RESEND_IGMP] = {
290 .id = BOND_OPT_RESEND_IGMP,
291 .name = "resend_igmp",
292 .desc = "Number of IGMP membership reports to send on link failure",
293 .values = bond_resend_igmp_tbl,
294 .set = bond_option_resend_igmp_set
295 },
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100296 [BOND_OPT_LP_INTERVAL] = {
297 .id = BOND_OPT_LP_INTERVAL,
298 .name = "lp_interval",
299 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
300 .values = bond_lp_interval_tbl,
301 .set = bond_option_lp_interval_set
302 },
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +0100303 [BOND_OPT_SLAVES] = {
304 .id = BOND_OPT_SLAVES,
305 .name = "slaves",
306 .desc = "Slave membership management",
307 .flags = BOND_OPTFLAG_RAWVAL,
308 .set = bond_option_slaves_set
309 },
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100310 { }
311};
312
313/* Searches for a value in opt's values[] table */
314struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
315{
316 struct bond_option *opt;
317 int i;
318
319 opt = bond_opt_get(option);
320 if (WARN_ON(!opt))
321 return NULL;
322 for (i = 0; opt->values && opt->values[i].string; i++)
323 if (opt->values[i].value == val)
324 return &opt->values[i];
325
326 return NULL;
327}
328
329/* Searches for a value in opt's values[] table which matches the flagmask */
330static struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
331 u32 flagmask)
332{
333 int i;
334
335 for (i = 0; opt->values && opt->values[i].string; i++)
336 if (opt->values[i].flags & flagmask)
337 return &opt->values[i];
338
339 return NULL;
340}
341
342/* If maxval is missing then there's no range to check. In case minval is
343 * missing then it's considered to be 0.
344 */
345static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
346{
347 struct bond_opt_value *minval, *maxval;
348
349 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
350 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
351 if (!maxval || (minval && val < minval->value) || val > maxval->value)
352 return false;
353
354 return true;
355}
356
357/**
358 * bond_opt_parse - parse option value
359 * @opt: the option to parse against
360 * @val: value to parse
361 *
362 * This function tries to extract the value from @val and check if it's
363 * a possible match for the option and returns NULL if a match isn't found,
364 * or the struct_opt_value that matched. It also strips the new line from
365 * @val->string if it's present.
366 */
367struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
368 struct bond_opt_value *val)
369{
370 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
371 struct bond_opt_value *tbl, *ret = NULL;
372 bool checkval;
373 int i, rv;
374
375 /* No parsing if the option wants a raw val */
376 if (opt->flags & BOND_OPTFLAG_RAWVAL)
377 return val;
378
379 tbl = opt->values;
380 if (!tbl)
381 goto out;
382
383 /* ULLONG_MAX is used to bypass string processing */
384 checkval = val->value != ULLONG_MAX;
385 if (!checkval) {
386 if (!val->string)
387 goto out;
388 p = strchr(val->string, '\n');
389 if (p)
390 *p = '\0';
391 for (p = val->string; *p; p++)
392 if (!(isdigit(*p) || isspace(*p)))
393 break;
394 /* The following code extracts the string to match or the value
395 * and sets checkval appropriately
396 */
397 if (*p) {
398 rv = sscanf(val->string, "%32s", valstr);
399 } else {
400 rv = sscanf(val->string, "%llu", &val->value);
401 checkval = true;
402 }
403 if (!rv)
404 goto out;
405 }
406
407 for (i = 0; tbl[i].string; i++) {
408 /* Check for exact match */
409 if (checkval) {
410 if (val->value == tbl[i].value)
411 ret = &tbl[i];
412 } else {
413 if (!strcmp(valstr, "default") &&
414 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
415 ret = &tbl[i];
416
417 if (!strcmp(valstr, tbl[i].string))
418 ret = &tbl[i];
419 }
420 /* Found an exact match */
421 if (ret)
422 goto out;
423 }
424 /* Possible range match */
425 if (checkval && bond_opt_check_range(opt, val->value))
426 ret = val;
427out:
428 return ret;
429}
430
431/* Check opt's dependencies against bond mode and currently set options */
432static int bond_opt_check_deps(struct bonding *bond,
433 const struct bond_option *opt)
434{
435 struct bond_params *params = &bond->params;
436
437 if (test_bit(params->mode, &opt->unsuppmodes))
438 return -EACCES;
439 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
440 return -ENOTEMPTY;
441 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
442 return -EBUSY;
443
444 return 0;
445}
446
447static void bond_opt_dep_print(struct bonding *bond,
448 const struct bond_option *opt)
449{
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100450 struct bond_opt_value *modeval;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100451 struct bond_params *params;
452
453 params = &bond->params;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100454 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100455 if (test_bit(params->mode, &opt->unsuppmodes))
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100456 pr_err("%s: option %s: mode dependency failed, not supported in mode %s(%llu)\n",
457 bond->dev->name, opt->name,
458 modeval->string, modeval->value);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100459}
460
461static void bond_opt_error_interpret(struct bonding *bond,
462 const struct bond_option *opt,
463 int error, struct bond_opt_value *val)
464{
465 struct bond_opt_value *minval, *maxval;
466 char *p;
467
468 switch (error) {
469 case -EINVAL:
470 if (val) {
471 if (val->string) {
472 /* sometimes RAWVAL opts may have new lines */
473 p = strchr(val->string, '\n');
474 if (p)
475 *p = '\0';
Joe Perches90194262014-02-15 16:01:45 -0800476 pr_err("%s: option %s: invalid value (%s)\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100477 bond->dev->name, opt->name, val->string);
478 } else {
Joe Perches90194262014-02-15 16:01:45 -0800479 pr_err("%s: option %s: invalid value (%llu)\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100480 bond->dev->name, opt->name, val->value);
481 }
482 }
483 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
484 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
485 if (!maxval)
486 break;
Joe Perches90194262014-02-15 16:01:45 -0800487 pr_err("%s: option %s: allowed values %llu - %llu\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100488 bond->dev->name, opt->name, minval ? minval->value : 0,
489 maxval->value);
490 break;
491 case -EACCES:
492 bond_opt_dep_print(bond, opt);
493 break;
494 case -ENOTEMPTY:
Joe Perches90194262014-02-15 16:01:45 -0800495 pr_err("%s: option %s: unable to set because the bond device has slaves\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100496 bond->dev->name, opt->name);
497 break;
498 case -EBUSY:
Joe Perches90194262014-02-15 16:01:45 -0800499 pr_err("%s: option %s: unable to set because the bond device is up\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100500 bond->dev->name, opt->name);
501 break;
502 default:
503 break;
504 }
505}
506
507/**
508 * __bond_opt_set - set a bonding option
509 * @bond: target bond device
510 * @option: option to set
511 * @val: value to set it to
512 *
513 * This function is used to change the bond's option value, it can be
514 * used for both enabling/changing an option and for disabling it. RTNL lock
515 * must be obtained before calling this function.
516 */
517int __bond_opt_set(struct bonding *bond,
518 unsigned int option, struct bond_opt_value *val)
519{
520 struct bond_opt_value *retval = NULL;
521 const struct bond_option *opt;
522 int ret = -ENOENT;
523
524 ASSERT_RTNL();
525
526 opt = bond_opt_get(option);
527 if (WARN_ON(!val) || WARN_ON(!opt))
528 goto out;
529 ret = bond_opt_check_deps(bond, opt);
530 if (ret)
531 goto out;
532 retval = bond_opt_parse(opt, val);
533 if (!retval) {
534 ret = -EINVAL;
535 goto out;
536 }
537 ret = opt->set(bond, retval);
538out:
539 if (ret)
540 bond_opt_error_interpret(bond, opt, ret, val);
541
542 return ret;
543}
544
545/**
546 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
547 * @bond: target bond device
548 * @option: option to set
549 * @buf: value to set it to
550 *
551 * This function tries to acquire RTNL without blocking and if successful
552 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
553 */
554int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
555{
556 struct bond_opt_value optval;
557 int ret;
558
559 if (!rtnl_trylock())
560 return restart_syscall();
561 bond_opt_initstr(&optval, buf);
562 ret = __bond_opt_set(bond, option, &optval);
563 rtnl_unlock();
564
565 return ret;
566}
567
568/**
569 * bond_opt_get - get a pointer to an option
570 * @option: option for which to return a pointer
571 *
572 * This function checks if option is valid and if so returns a pointer
573 * to its entry in the bond_opts[] option array.
574 */
575struct bond_option *bond_opt_get(unsigned int option)
576{
577 if (!BOND_OPT_VALID(option))
578 return NULL;
579
580 return &bond_opts[option];
581}
582
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100583int bond_option_mode_set(struct bonding *bond, struct bond_opt_value *newval)
Jiri Pirko72be35f2013-10-18 17:43:34 +0200584{
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100585 if (BOND_NO_USES_ARP(newval->value) && bond->params.arp_interval) {
dingtianhongfe9d04a2013-11-22 22:28:43 +0800586 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100587 bond->dev->name, newval->string);
dingtianhongfe9d04a2013-11-22 22:28:43 +0800588 /* disable arp monitoring */
589 bond->params.arp_interval = 0;
590 /* set miimon to default value */
591 bond->params.miimon = BOND_DEFAULT_MIIMON;
Joe Perches90194262014-02-15 16:01:45 -0800592 pr_info("%s: Setting MII monitoring interval to %d\n",
dingtianhongfe9d04a2013-11-22 22:28:43 +0800593 bond->dev->name, bond->params.miimon);
Jiri Pirko72be35f2013-10-18 17:43:34 +0200594 }
595
596 /* don't cache arp_validate between modes */
597 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100598 bond->params.mode = newval->value;
599
Jiri Pirko72be35f2013-10-18 17:43:34 +0200600 return 0;
601}
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200602
Jiri Pirko752d48b2013-10-18 17:43:37 +0200603static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
604 struct slave *slave)
605{
606 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
607}
608
609struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
610{
611 struct slave *slave = rcu_dereference(bond->curr_active_slave);
612
613 return __bond_option_active_slave_get(bond, slave);
614}
615
616struct net_device *bond_option_active_slave_get(struct bonding *bond)
617{
618 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
619}
620
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200621int bond_option_active_slave_set(struct bonding *bond,
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100622 struct bond_opt_value *newval)
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200623{
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100624 char ifname[IFNAMSIZ] = { 0, };
625 struct net_device *slave_dev;
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200626 int ret = 0;
627
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100628 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
629 if (!strlen(ifname) || newval->string[0] == '\n') {
630 slave_dev = NULL;
631 } else {
632 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
633 if (!slave_dev)
634 return -ENODEV;
635 }
636
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200637 if (slave_dev) {
638 if (!netif_is_bond_slave(slave_dev)) {
Joe Perches90194262014-02-15 16:01:45 -0800639 pr_err("Device %s is not bonding slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200640 slave_dev->name);
641 return -EINVAL;
642 }
643
644 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
Joe Perches90194262014-02-15 16:01:45 -0800645 pr_err("%s: Device %s is not our slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200646 bond->dev->name, slave_dev->name);
647 return -EINVAL;
648 }
649 }
650
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200651 block_netpoll_tx();
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200652 write_lock_bh(&bond->curr_slave_lock);
653
654 /* check to see if we are clearing active */
655 if (!slave_dev) {
Joe Perches90194262014-02-15 16:01:45 -0800656 pr_info("%s: Clearing current active slave\n", bond->dev->name);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200657 rcu_assign_pointer(bond->curr_active_slave, NULL);
658 bond_select_active_slave(bond);
659 } else {
660 struct slave *old_active = bond->curr_active_slave;
661 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
662
663 BUG_ON(!new_active);
664
665 if (new_active == old_active) {
666 /* do nothing */
Joe Perches90194262014-02-15 16:01:45 -0800667 pr_info("%s: %s is already the current active slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200668 bond->dev->name, new_active->dev->name);
669 } else {
670 if (old_active && (new_active->link == BOND_LINK_UP) &&
671 IS_UP(new_active->dev)) {
Joe Perches90194262014-02-15 16:01:45 -0800672 pr_info("%s: Setting %s as active slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200673 bond->dev->name, new_active->dev->name);
674 bond_change_active_slave(bond, new_active);
675 } else {
Joe Perches90194262014-02-15 16:01:45 -0800676 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200677 bond->dev->name, new_active->dev->name,
678 new_active->dev->name);
679 ret = -EINVAL;
680 }
681 }
682 }
683
684 write_unlock_bh(&bond->curr_slave_lock);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200685 unblock_netpoll_tx();
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100686
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200687 return ret;
688}
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800689
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100690int bond_option_miimon_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800691{
Joe Perches90194262014-02-15 16:01:45 -0800692 pr_info("%s: Setting MII monitoring interval to %llu\n",
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100693 bond->dev->name, newval->value);
694 bond->params.miimon = newval->value;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800695 if (bond->params.updelay)
Joe Perches90194262014-02-15 16:01:45 -0800696 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800697 bond->dev->name,
698 bond->params.updelay * bond->params.miimon);
699 if (bond->params.downdelay)
Joe Perches90194262014-02-15 16:01:45 -0800700 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800701 bond->dev->name,
702 bond->params.downdelay * bond->params.miimon);
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100703 if (newval->value && bond->params.arp_interval) {
Joe Perches90194262014-02-15 16:01:45 -0800704 pr_info("%s: MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n",
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800705 bond->dev->name);
706 bond->params.arp_interval = 0;
707 if (bond->params.arp_validate)
708 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
709 }
710 if (bond->dev->flags & IFF_UP) {
711 /* If the interface is up, we may need to fire off
712 * the MII timer. If the interface is down, the
713 * timer will get fired off when the open function
714 * is called.
715 */
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100716 if (!newval->value) {
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800717 cancel_delayed_work_sync(&bond->mii_work);
718 } else {
719 cancel_delayed_work_sync(&bond->arp_work);
720 queue_delayed_work(bond->wq, &bond->mii_work, 0);
721 }
722 }
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100723
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800724 return 0;
725}
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800726
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100727int bond_option_updelay_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800728{
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100729 int value = newval->value;
730
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100731 if (!bond->params.miimon) {
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800732 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
733 bond->dev->name);
734 return -EPERM;
735 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100736 if ((value % bond->params.miimon) != 0) {
737 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
738 bond->dev->name, value,
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100739 bond->params.miimon,
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100740 (value / bond->params.miimon) *
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100741 bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800742 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100743 bond->params.updelay = value / bond->params.miimon;
Joe Perches90194262014-02-15 16:01:45 -0800744 pr_info("%s: Setting up delay to %d\n",
745 bond->dev->name, bond->params.updelay * bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800746
747 return 0;
748}
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800749
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100750int bond_option_downdelay_set(struct bonding *bond,
751 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800752{
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100753 int value = newval->value;
754
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100755 if (!bond->params.miimon) {
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800756 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
757 bond->dev->name);
758 return -EPERM;
759 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100760 if ((value % bond->params.miimon) != 0) {
761 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
762 bond->dev->name, value,
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100763 bond->params.miimon,
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100764 (value / bond->params.miimon) *
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100765 bond->params.miimon);
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800766 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100767 bond->params.downdelay = value / bond->params.miimon;
Joe Perches90194262014-02-15 16:01:45 -0800768 pr_info("%s: Setting down delay to %d\n",
769 bond->dev->name, bond->params.downdelay * bond->params.miimon);
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800770
771 return 0;
772}
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800773
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100774int bond_option_use_carrier_set(struct bonding *bond,
775 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800776{
Joe Perches90194262014-02-15 16:01:45 -0800777 pr_info("%s: Setting use_carrier to %llu\n",
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100778 bond->dev->name, newval->value);
779 bond->params.use_carrier = newval->value;
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800780
781 return 0;
782}
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800783
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100784int bond_option_arp_interval_set(struct bonding *bond,
785 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800786{
Joe Perches90194262014-02-15 16:01:45 -0800787 pr_info("%s: Setting ARP monitoring interval to %llu\n",
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100788 bond->dev->name, newval->value);
789 bond->params.arp_interval = newval->value;
790 if (newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800791 if (bond->params.miimon) {
Joe Perches90194262014-02-15 16:01:45 -0800792 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring\n",
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800793 bond->dev->name, bond->dev->name);
794 bond->params.miimon = 0;
795 }
796 if (!bond->params.arp_targets[0])
Joe Perches90194262014-02-15 16:01:45 -0800797 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified\n",
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800798 bond->dev->name);
799 }
800 if (bond->dev->flags & IFF_UP) {
801 /* If the interface is up, we may need to fire off
802 * the ARP timer. If the interface is down, the
803 * timer will get fired off when the open function
804 * is called.
805 */
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100806 if (!newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800807 if (bond->params.arp_validate)
808 bond->recv_probe = NULL;
809 cancel_delayed_work_sync(&bond->arp_work);
810 } else {
811 /* arp_validate can be set only in active-backup mode */
812 if (bond->params.arp_validate)
813 bond->recv_probe = bond_arp_rcv;
814 cancel_delayed_work_sync(&bond->mii_work);
815 queue_delayed_work(bond->wq, &bond->arp_work, 0);
816 }
817 }
818
819 return 0;
820}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800821
822static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
823 __be32 target,
824 unsigned long last_rx)
825{
826 __be32 *targets = bond->params.arp_targets;
827 struct list_head *iter;
828 struct slave *slave;
829
830 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
831 bond_for_each_slave(bond, slave, iter)
832 slave->target_last_arp_rx[slot] = last_rx;
833 targets[slot] = target;
834 }
835}
836
837static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
838{
839 __be32 *targets = bond->params.arp_targets;
840 int ind;
841
842 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
843 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
844 bond->dev->name, &target);
845 return -EINVAL;
846 }
847
848 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
849 pr_err("%s: ARP target %pI4 is already present\n",
850 bond->dev->name, &target);
851 return -EINVAL;
852 }
853
854 ind = bond_get_targets_ip(targets, 0); /* first free slot */
855 if (ind == -1) {
Joe Perches90194262014-02-15 16:01:45 -0800856 pr_err("%s: ARP target table is full!\n", bond->dev->name);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800857 return -EINVAL;
858 }
859
Joe Perches90194262014-02-15 16:01:45 -0800860 pr_info("%s: Adding ARP target %pI4\n", bond->dev->name, &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800861
862 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
863
864 return 0;
865}
866
867int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
868{
869 int ret;
870
871 /* not to race with bond_arp_rcv */
872 write_lock_bh(&bond->lock);
873 ret = _bond_option_arp_ip_target_add(bond, target);
874 write_unlock_bh(&bond->lock);
875
876 return ret;
877}
878
879int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
880{
881 __be32 *targets = bond->params.arp_targets;
882 struct list_head *iter;
883 struct slave *slave;
884 unsigned long *targets_rx;
885 int ind, i;
886
887 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
888 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
889 bond->dev->name, &target);
890 return -EINVAL;
891 }
892
893 ind = bond_get_targets_ip(targets, target);
894 if (ind == -1) {
Joe Perches90194262014-02-15 16:01:45 -0800895 pr_err("%s: unable to remove nonexistent ARP target %pI4\n",
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800896 bond->dev->name, &target);
897 return -EINVAL;
898 }
899
900 if (ind == 0 && !targets[1] && bond->params.arp_interval)
Joe Perches90194262014-02-15 16:01:45 -0800901 pr_warn("%s: Removing last arp target with arp_interval on\n",
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800902 bond->dev->name);
903
Joe Perches90194262014-02-15 16:01:45 -0800904 pr_info("%s: Removing ARP target %pI4\n", bond->dev->name, &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800905
906 /* not to race with bond_arp_rcv */
907 write_lock_bh(&bond->lock);
908
909 bond_for_each_slave(bond, slave, iter) {
910 targets_rx = slave->target_last_arp_rx;
911 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
912 targets_rx[i] = targets_rx[i+1];
913 targets_rx[i] = 0;
914 }
915 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
916 targets[i] = targets[i+1];
917 targets[i] = 0;
918
919 write_unlock_bh(&bond->lock);
920
921 return 0;
922}
923
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100924void bond_option_arp_ip_targets_clear(struct bonding *bond)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800925{
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100926 int i;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800927
928 /* not to race with bond_arp_rcv */
929 write_lock_bh(&bond->lock);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800930 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
931 _bond_options_arp_ip_target_set(bond, i, 0, 0);
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100932 write_unlock_bh(&bond->lock);
933}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800934
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100935int bond_option_arp_ip_targets_set(struct bonding *bond,
936 struct bond_opt_value *newval)
937{
938 int ret = -EPERM;
939 __be32 target;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800940
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100941 if (newval->string) {
942 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
943 pr_err("%s: invalid ARP target %pI4 specified\n",
944 bond->dev->name, &target);
945 return ret;
946 }
947 if (newval->string[0] == '+')
948 ret = bond_option_arp_ip_target_add(bond, target);
949 else if (newval->string[0] == '-')
950 ret = bond_option_arp_ip_target_rem(bond, target);
951 else
Joe Perches90194262014-02-15 16:01:45 -0800952 pr_err("no command found in arp_ip_targets file for bond %s - use +<addr> or -<addr>\n",
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100953 bond->dev->name);
954 } else {
955 target = newval->value;
956 ret = bond_option_arp_ip_target_add(bond, target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800957 }
958
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800959 return ret;
960}
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800961
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100962int bond_option_arp_validate_set(struct bonding *bond,
963 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800964{
Joe Perches90194262014-02-15 16:01:45 -0800965 pr_info("%s: Setting arp_validate to %s (%llu)\n",
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100966 bond->dev->name, newval->string, newval->value);
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800967
968 if (bond->dev->flags & IFF_UP) {
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100969 if (!newval->value)
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800970 bond->recv_probe = NULL;
971 else if (bond->params.arp_interval)
972 bond->recv_probe = bond_arp_rcv;
973 }
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100974 bond->params.arp_validate = newval->value;
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800975
976 return 0;
977}
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800978
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100979int bond_option_arp_all_targets_set(struct bonding *bond,
980 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800981{
Joe Perches90194262014-02-15 16:01:45 -0800982 pr_info("%s: Setting arp_all_targets to %s (%llu)\n",
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100983 bond->dev->name, newval->string, newval->value);
984 bond->params.arp_all_targets = newval->value;
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800985
986 return 0;
987}
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800988
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100989int bond_option_primary_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800990{
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100991 char *p, *primary = newval->string;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800992 struct list_head *iter;
993 struct slave *slave;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800994
995 block_netpoll_tx();
996 read_lock(&bond->lock);
997 write_lock_bh(&bond->curr_slave_lock);
998
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100999 p = strchr(primary, '\n');
1000 if (p)
1001 *p = '\0';
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001002 /* check to see if we are clearing primary */
1003 if (!strlen(primary)) {
Joe Perches90194262014-02-15 16:01:45 -08001004 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001005 bond->primary_slave = NULL;
1006 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1007 bond_select_active_slave(bond);
1008 goto out;
1009 }
1010
1011 bond_for_each_slave(bond, slave, iter) {
1012 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
Joe Perches90194262014-02-15 16:01:45 -08001013 pr_info("%s: Setting %s as primary slave\n",
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001014 bond->dev->name, slave->dev->name);
1015 bond->primary_slave = slave;
1016 strcpy(bond->params.primary, slave->dev->name);
1017 bond_select_active_slave(bond);
1018 goto out;
1019 }
1020 }
1021
dingtianhongc59ab672014-01-18 16:28:52 +08001022 if (bond->primary_slave) {
Joe Perches90194262014-02-15 16:01:45 -08001023 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
dingtianhongc59ab672014-01-18 16:28:52 +08001024 bond->primary_slave = NULL;
1025 bond_select_active_slave(bond);
1026 }
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001027 strncpy(bond->params.primary, primary, IFNAMSIZ);
1028 bond->params.primary[IFNAMSIZ - 1] = 0;
1029
Joe Perches90194262014-02-15 16:01:45 -08001030 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet\n",
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001031 bond->dev->name, primary, bond->dev->name);
1032
1033out:
1034 write_unlock_bh(&bond->curr_slave_lock);
1035 read_unlock(&bond->lock);
1036 unblock_netpoll_tx();
1037
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001038 return 0;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001039}
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001040
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01001041int bond_option_primary_reselect_set(struct bonding *bond,
1042 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001043{
Joe Perches90194262014-02-15 16:01:45 -08001044 pr_info("%s: Setting primary_reselect to %s (%llu)\n",
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01001045 bond->dev->name, newval->string, newval->value);
1046 bond->params.primary_reselect = newval->value;
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001047
1048 block_netpoll_tx();
1049 write_lock_bh(&bond->curr_slave_lock);
1050 bond_select_active_slave(bond);
1051 write_unlock_bh(&bond->curr_slave_lock);
1052 unblock_netpoll_tx();
1053
1054 return 0;
1055}
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001056
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01001057int bond_option_fail_over_mac_set(struct bonding *bond,
1058 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001059{
Joe Perches90194262014-02-15 16:01:45 -08001060 pr_info("%s: Setting fail_over_mac to %s (%llu)\n",
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01001061 bond->dev->name, newval->string, newval->value);
1062 bond->params.fail_over_mac = newval->value;
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001063
1064 return 0;
1065}
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001066
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01001067int bond_option_xmit_hash_policy_set(struct bonding *bond,
1068 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001069{
Joe Perches90194262014-02-15 16:01:45 -08001070 pr_info("%s: Setting xmit hash policy to %s (%llu)\n",
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01001071 bond->dev->name, newval->string, newval->value);
1072 bond->params.xmit_policy = newval->value;
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001073
1074 return 0;
1075}
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001076
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +01001077int bond_option_resend_igmp_set(struct bonding *bond,
1078 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001079{
Joe Perches90194262014-02-15 16:01:45 -08001080 pr_info("%s: Setting resend_igmp to %llu\n",
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +01001081 bond->dev->name, newval->value);
1082 bond->params.resend_igmp = newval->value;
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001083
1084 return 0;
1085}
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001086
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +01001087int bond_option_num_peer_notif_set(struct bonding *bond,
1088 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001089{
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +01001090 bond->params.num_peer_notif = newval->value;
1091
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001092 return 0;
1093}
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001094
1095int bond_option_all_slaves_active_set(struct bonding *bond,
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001096 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001097{
1098 struct list_head *iter;
1099 struct slave *slave;
1100
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001101 if (newval->value == bond->params.all_slaves_active)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001102 return 0;
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001103 bond->params.all_slaves_active = newval->value;
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001104 bond_for_each_slave(bond, slave, iter) {
1105 if (!bond_is_active_slave(slave)) {
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001106 if (newval->value)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001107 slave->inactive = 0;
1108 else
1109 slave->inactive = 1;
1110 }
1111 }
1112
1113 return 0;
1114}
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001115
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +01001116int bond_option_min_links_set(struct bonding *bond,
1117 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001118{
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +01001119 pr_info("%s: Setting min links value to %llu\n",
1120 bond->dev->name, newval->value);
1121 bond->params.min_links = newval->value;
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001122
1123 return 0;
1124}
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001125
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +01001126int bond_option_lp_interval_set(struct bonding *bond,
1127 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001128{
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +01001129 bond->params.lp_interval = newval->value;
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001130
1131 return 0;
1132}
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001133
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001134int bond_option_pps_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001135{
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001136 bond->params.packets_per_slave = newval->value;
1137 if (newval->value > 0) {
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001138 bond->params.reciprocal_packets_per_slave =
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001139 reciprocal_value(newval->value);
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001140 } else {
1141 /* reciprocal_packets_per_slave is unused if
1142 * packets_per_slave is 0 or 1, just initialize it
1143 */
1144 bond->params.reciprocal_packets_per_slave =
1145 (struct reciprocal_value) { 0 };
1146 }
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001147
1148 return 0;
1149}
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001150
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01001151int bond_option_lacp_rate_set(struct bonding *bond,
1152 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001153{
Joe Perches90194262014-02-15 16:01:45 -08001154 pr_info("%s: Setting LACP rate to %s (%llu)\n",
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01001155 bond->dev->name, newval->string, newval->value);
1156 bond->params.lacp_fast = newval->value;
sfeldma@cumulusnetworks.com3243c472014-01-03 14:28:18 -08001157 bond_3ad_update_lacp_rate(bond);
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001158
1159 return 0;
1160}
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001161
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01001162int bond_option_ad_select_set(struct bonding *bond,
1163 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001164{
Joe Perches90194262014-02-15 16:01:45 -08001165 pr_info("%s: Setting ad_select to %s (%llu)\n",
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01001166 bond->dev->name, newval->string, newval->value);
1167 bond->params.ad_select = newval->value;
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001168
1169 return 0;
1170}
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001171
1172int bond_option_queue_id_set(struct bonding *bond,
1173 struct bond_opt_value *newval)
1174{
1175 struct slave *slave, *update_slave;
1176 struct net_device *sdev;
1177 struct list_head *iter;
1178 char *delim;
1179 int ret = 0;
1180 u16 qid;
1181
1182 /* delim will point to queue id if successful */
1183 delim = strchr(newval->string, ':');
1184 if (!delim)
1185 goto err_no_cmd;
1186
1187 /* Terminate string that points to device name and bump it
1188 * up one, so we can read the queue id there.
1189 */
1190 *delim = '\0';
1191 if (sscanf(++delim, "%hd\n", &qid) != 1)
1192 goto err_no_cmd;
1193
1194 /* Check buffer length, valid ifname and queue id */
dingtianhongc313c892014-02-12 14:58:50 +08001195 if (!dev_valid_name(newval->string) ||
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001196 qid > bond->dev->real_num_tx_queues)
1197 goto err_no_cmd;
1198
1199 /* Get the pointer to that interface if it exists */
1200 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1201 if (!sdev)
1202 goto err_no_cmd;
1203
1204 /* Search for thes slave and check for duplicate qids */
1205 update_slave = NULL;
1206 bond_for_each_slave(bond, slave, iter) {
1207 if (sdev == slave->dev)
1208 /* We don't need to check the matching
1209 * slave for dups, since we're overwriting it
1210 */
1211 update_slave = slave;
1212 else if (qid && qid == slave->queue_id) {
1213 goto err_no_cmd;
1214 }
1215 }
1216
1217 if (!update_slave)
1218 goto err_no_cmd;
1219
1220 /* Actually set the qids for the slave */
1221 update_slave->queue_id = qid;
1222
1223out:
1224 return ret;
1225
1226err_no_cmd:
Joe Perches90194262014-02-15 16:01:45 -08001227 pr_info("invalid input for queue_id set for %s\n", bond->dev->name);
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001228 ret = -EPERM;
1229 goto out;
1230
1231}
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001232
1233int bond_option_slaves_set(struct bonding *bond, struct bond_opt_value *newval)
1234{
1235 char command[IFNAMSIZ + 1] = { 0, };
1236 struct net_device *dev;
1237 char *ifname;
1238 int ret;
1239
1240 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1241 ifname = command + 1;
1242 if ((strlen(command) <= 1) ||
1243 !dev_valid_name(ifname))
1244 goto err_no_cmd;
1245
1246 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1247 if (!dev) {
Joe Perches90194262014-02-15 16:01:45 -08001248 pr_info("%s: interface %s does not exist!\n",
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001249 bond->dev->name, ifname);
1250 ret = -ENODEV;
1251 goto out;
1252 }
1253
1254 switch (command[0]) {
1255 case '+':
Joe Perches90194262014-02-15 16:01:45 -08001256 pr_info("%s: Adding slave %s\n", bond->dev->name, dev->name);
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001257 ret = bond_enslave(bond->dev, dev);
1258 break;
1259
1260 case '-':
Joe Perches90194262014-02-15 16:01:45 -08001261 pr_info("%s: Removing slave %s\n", bond->dev->name, dev->name);
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001262 ret = bond_release(bond->dev, dev);
1263 break;
1264
1265 default:
1266 goto err_no_cmd;
1267 }
1268
1269out:
1270 return ret;
1271
1272err_no_cmd:
Joe Perches90194262014-02-15 16:01:45 -08001273 pr_err("no command found in slaves file for bond %s - use +ifname or -ifname\n",
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001274 bond->dev->name);
1275 ret = -EPERM;
1276 goto out;
1277}