blob: 298c26509095cdd6a6306aa954fe79b1ba46622b [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>
Mike Galbraitheb2d4c62014-02-14 08:21:04 +010017#include <linux/spinlock.h>
Jiri Pirkod9e32b22013-10-18 17:43:35 +020018#include <linux/rcupdate.h>
Nikolay Aleksandrov09117362014-01-22 14:53:16 +010019#include <linux/ctype.h>
Nikolay Aleksandrov4fb0ef52014-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},
Sasha Levin5bd4e4c2014-03-06 16:53:11 -0500124 { NULL, -1, 0},
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100125};
126
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100127static struct bond_option bond_opts[] = {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100128 [BOND_OPT_MODE] = {
129 .id = BOND_OPT_MODE,
130 .name = "mode",
131 .desc = "bond device mode",
132 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
133 .values = bond_mode_tbl,
134 .set = bond_option_mode_set
135 },
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +0100136 [BOND_OPT_PACKETS_PER_SLAVE] = {
137 .id = BOND_OPT_PACKETS_PER_SLAVE,
138 .name = "packets_per_slave",
139 .desc = "Packets to send per slave in RR mode",
140 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
141 .values = bond_pps_tbl,
142 .set = bond_option_pps_set
143 },
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +0100144 [BOND_OPT_XMIT_HASH] = {
145 .id = BOND_OPT_XMIT_HASH,
146 .name = "xmit_hash_policy",
147 .desc = "balance-xor and 802.3ad hashing method",
148 .values = bond_xmit_hashtype_tbl,
149 .set = bond_option_xmit_hash_policy_set
150 },
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100151 [BOND_OPT_ARP_VALIDATE] = {
152 .id = BOND_OPT_ARP_VALIDATE,
153 .name = "arp_validate",
154 .desc = "validate src/dst of ARP probes",
155 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP)),
156 .values = bond_arp_validate_tbl,
157 .set = bond_option_arp_validate_set
158 },
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100159 [BOND_OPT_ARP_ALL_TARGETS] = {
160 .id = BOND_OPT_ARP_ALL_TARGETS,
161 .name = "arp_all_targets",
162 .desc = "fail on any/all arp targets timeout",
163 .values = bond_arp_all_targets_tbl,
164 .set = bond_option_arp_all_targets_set
165 },
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +0100166 [BOND_OPT_FAIL_OVER_MAC] = {
167 .id = BOND_OPT_FAIL_OVER_MAC,
168 .name = "fail_over_mac",
169 .desc = "For active-backup, do not set all slaves to the same MAC",
170 .flags = BOND_OPTFLAG_NOSLAVES,
171 .values = bond_fail_over_mac_tbl,
172 .set = bond_option_fail_over_mac_set
173 },
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100174 [BOND_OPT_ARP_INTERVAL] = {
175 .id = BOND_OPT_ARP_INTERVAL,
176 .name = "arp_interval",
177 .desc = "arp interval in milliseconds",
178 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
179 BIT(BOND_MODE_ALB),
180 .values = bond_intmax_tbl,
181 .set = bond_option_arp_interval_set
182 },
Nikolay Aleksandrov4fb0ef52014-01-22 14:53:24 +0100183 [BOND_OPT_ARP_TARGETS] = {
184 .id = BOND_OPT_ARP_TARGETS,
185 .name = "arp_ip_target",
186 .desc = "arp targets in n.n.n.n form",
187 .flags = BOND_OPTFLAG_RAWVAL,
188 .set = bond_option_arp_ip_targets_set
189 },
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100190 [BOND_OPT_DOWNDELAY] = {
191 .id = BOND_OPT_DOWNDELAY,
192 .name = "downdelay",
193 .desc = "Delay before considering link down, in milliseconds",
194 .values = bond_intmax_tbl,
195 .set = bond_option_downdelay_set
196 },
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100197 [BOND_OPT_UPDELAY] = {
198 .id = BOND_OPT_UPDELAY,
199 .name = "updelay",
200 .desc = "Delay before considering link up, in milliseconds",
201 .values = bond_intmax_tbl,
202 .set = bond_option_updelay_set
203 },
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +0100204 [BOND_OPT_LACP_RATE] = {
205 .id = BOND_OPT_LACP_RATE,
206 .name = "lacp_rate",
207 .desc = "LACPDU tx rate to request from 802.3ad partner",
208 .flags = BOND_OPTFLAG_IFDOWN,
209 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
210 .values = bond_lacp_rate_tbl,
211 .set = bond_option_lacp_rate_set
212 },
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +0100213 [BOND_OPT_MINLINKS] = {
214 .id = BOND_OPT_MINLINKS,
215 .name = "min_links",
216 .desc = "Minimum number of available links before turning on carrier",
217 .values = bond_intmax_tbl,
218 .set = bond_option_min_links_set
219 },
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +0100220 [BOND_OPT_AD_SELECT] = {
221 .id = BOND_OPT_AD_SELECT,
222 .name = "ad_select",
223 .desc = "803.ad aggregation selection logic",
224 .flags = BOND_OPTFLAG_IFDOWN,
225 .values = bond_ad_select_tbl,
226 .set = bond_option_ad_select_set
227 },
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +0100228 [BOND_OPT_NUM_PEER_NOTIF] = {
229 .id = BOND_OPT_NUM_PEER_NOTIF,
230 .name = "num_unsol_na",
231 .desc = "Number of peer notifications to send on failover event",
232 .values = bond_num_peer_notif_tbl,
233 .set = bond_option_num_peer_notif_set
234 },
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100235 [BOND_OPT_MIIMON] = {
236 .id = BOND_OPT_MIIMON,
237 .name = "miimon",
238 .desc = "Link check interval in milliseconds",
239 .values = bond_intmax_tbl,
240 .set = bond_option_miimon_set
241 },
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100242 [BOND_OPT_PRIMARY] = {
243 .id = BOND_OPT_PRIMARY,
244 .name = "primary",
245 .desc = "Primary network device to use",
246 .flags = BOND_OPTFLAG_RAWVAL,
247 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
248 BIT(BOND_MODE_TLB) |
249 BIT(BOND_MODE_ALB)),
250 .set = bond_option_primary_set
251 },
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +0100252 [BOND_OPT_PRIMARY_RESELECT] = {
253 .id = BOND_OPT_PRIMARY_RESELECT,
254 .name = "primary_reselect",
255 .desc = "Reselect primary slave once it comes up",
256 .values = bond_primary_reselect_tbl,
257 .set = bond_option_primary_reselect_set
258 },
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100259 [BOND_OPT_USE_CARRIER] = {
260 .id = BOND_OPT_USE_CARRIER,
261 .name = "use_carrier",
262 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
263 .values = bond_use_carrier_tbl,
264 .set = bond_option_use_carrier_set
265 },
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100266 [BOND_OPT_ACTIVE_SLAVE] = {
267 .id = BOND_OPT_ACTIVE_SLAVE,
268 .name = "active_slave",
269 .desc = "Currently active slave",
270 .flags = BOND_OPTFLAG_RAWVAL,
271 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
272 BIT(BOND_MODE_TLB) |
273 BIT(BOND_MODE_ALB)),
274 .set = bond_option_active_slave_set
275 },
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +0100276 [BOND_OPT_QUEUE_ID] = {
277 .id = BOND_OPT_QUEUE_ID,
278 .name = "queue_id",
279 .desc = "Set queue id of a slave",
280 .flags = BOND_OPTFLAG_RAWVAL,
281 .set = bond_option_queue_id_set
282 },
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +0100283 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
284 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
285 .name = "all_slaves_active",
286 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
287 .values = bond_all_slaves_active_tbl,
288 .set = bond_option_all_slaves_active_set
289 },
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +0100290 [BOND_OPT_RESEND_IGMP] = {
291 .id = BOND_OPT_RESEND_IGMP,
292 .name = "resend_igmp",
293 .desc = "Number of IGMP membership reports to send on link failure",
294 .values = bond_resend_igmp_tbl,
295 .set = bond_option_resend_igmp_set
296 },
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100297 [BOND_OPT_LP_INTERVAL] = {
298 .id = BOND_OPT_LP_INTERVAL,
299 .name = "lp_interval",
300 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
301 .values = bond_lp_interval_tbl,
302 .set = bond_option_lp_interval_set
303 },
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +0100304 [BOND_OPT_SLAVES] = {
305 .id = BOND_OPT_SLAVES,
306 .name = "slaves",
307 .desc = "Slave membership management",
308 .flags = BOND_OPTFLAG_RAWVAL,
309 .set = bond_option_slaves_set
310 },
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100311 { }
312};
313
314/* Searches for a value in opt's values[] table */
315struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
316{
317 struct bond_option *opt;
318 int i;
319
320 opt = bond_opt_get(option);
321 if (WARN_ON(!opt))
322 return NULL;
323 for (i = 0; opt->values && opt->values[i].string; i++)
324 if (opt->values[i].value == val)
325 return &opt->values[i];
326
327 return NULL;
328}
329
330/* Searches for a value in opt's values[] table which matches the flagmask */
331static struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
332 u32 flagmask)
333{
334 int i;
335
336 for (i = 0; opt->values && opt->values[i].string; i++)
337 if (opt->values[i].flags & flagmask)
338 return &opt->values[i];
339
340 return NULL;
341}
342
343/* If maxval is missing then there's no range to check. In case minval is
344 * missing then it's considered to be 0.
345 */
346static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
347{
348 struct bond_opt_value *minval, *maxval;
349
350 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
351 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
352 if (!maxval || (minval && val < minval->value) || val > maxval->value)
353 return false;
354
355 return true;
356}
357
358/**
359 * bond_opt_parse - parse option value
360 * @opt: the option to parse against
361 * @val: value to parse
362 *
363 * This function tries to extract the value from @val and check if it's
364 * a possible match for the option and returns NULL if a match isn't found,
365 * or the struct_opt_value that matched. It also strips the new line from
366 * @val->string if it's present.
367 */
368struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
369 struct bond_opt_value *val)
370{
371 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
372 struct bond_opt_value *tbl, *ret = NULL;
373 bool checkval;
374 int i, rv;
375
376 /* No parsing if the option wants a raw val */
377 if (opt->flags & BOND_OPTFLAG_RAWVAL)
378 return val;
379
380 tbl = opt->values;
381 if (!tbl)
382 goto out;
383
384 /* ULLONG_MAX is used to bypass string processing */
385 checkval = val->value != ULLONG_MAX;
386 if (!checkval) {
387 if (!val->string)
388 goto out;
389 p = strchr(val->string, '\n');
390 if (p)
391 *p = '\0';
392 for (p = val->string; *p; p++)
393 if (!(isdigit(*p) || isspace(*p)))
394 break;
395 /* The following code extracts the string to match or the value
396 * and sets checkval appropriately
397 */
398 if (*p) {
399 rv = sscanf(val->string, "%32s", valstr);
400 } else {
401 rv = sscanf(val->string, "%llu", &val->value);
402 checkval = true;
403 }
404 if (!rv)
405 goto out;
406 }
407
408 for (i = 0; tbl[i].string; i++) {
409 /* Check for exact match */
410 if (checkval) {
411 if (val->value == tbl[i].value)
412 ret = &tbl[i];
413 } else {
414 if (!strcmp(valstr, "default") &&
415 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
416 ret = &tbl[i];
417
418 if (!strcmp(valstr, tbl[i].string))
419 ret = &tbl[i];
420 }
421 /* Found an exact match */
422 if (ret)
423 goto out;
424 }
425 /* Possible range match */
426 if (checkval && bond_opt_check_range(opt, val->value))
427 ret = val;
428out:
429 return ret;
430}
431
432/* Check opt's dependencies against bond mode and currently set options */
433static int bond_opt_check_deps(struct bonding *bond,
434 const struct bond_option *opt)
435{
436 struct bond_params *params = &bond->params;
437
438 if (test_bit(params->mode, &opt->unsuppmodes))
439 return -EACCES;
440 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
441 return -ENOTEMPTY;
442 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
443 return -EBUSY;
444
445 return 0;
446}
447
448static void bond_opt_dep_print(struct bonding *bond,
449 const struct bond_option *opt)
450{
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100451 struct bond_opt_value *modeval;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100452 struct bond_params *params;
453
454 params = &bond->params;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100455 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100456 if (test_bit(params->mode, &opt->unsuppmodes))
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100457 pr_err("%s: option %s: mode dependency failed, not supported in mode %s(%llu)\n",
458 bond->dev->name, opt->name,
459 modeval->string, modeval->value);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100460}
461
462static void bond_opt_error_interpret(struct bonding *bond,
463 const struct bond_option *opt,
464 int error, struct bond_opt_value *val)
465{
466 struct bond_opt_value *minval, *maxval;
467 char *p;
468
469 switch (error) {
470 case -EINVAL:
471 if (val) {
472 if (val->string) {
473 /* sometimes RAWVAL opts may have new lines */
474 p = strchr(val->string, '\n');
475 if (p)
476 *p = '\0';
477 pr_err("%s: option %s: invalid value (%s).\n",
478 bond->dev->name, opt->name, val->string);
479 } else {
480 pr_err("%s: option %s: invalid value (%llu).\n",
481 bond->dev->name, opt->name, val->value);
482 }
483 }
484 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
485 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
486 if (!maxval)
487 break;
488 pr_err("%s: option %s: allowed values %llu - %llu.\n",
489 bond->dev->name, opt->name, minval ? minval->value : 0,
490 maxval->value);
491 break;
492 case -EACCES:
493 bond_opt_dep_print(bond, opt);
494 break;
495 case -ENOTEMPTY:
496 pr_err("%s: option %s: unable to set because the bond device has slaves.\n",
497 bond->dev->name, opt->name);
498 break;
499 case -EBUSY:
500 pr_err("%s: option %s: unable to set because the bond device is up.\n",
501 bond->dev->name, opt->name);
502 break;
503 default:
504 break;
505 }
506}
507
508/**
509 * __bond_opt_set - set a bonding option
510 * @bond: target bond device
511 * @option: option to set
512 * @val: value to set it to
513 *
514 * This function is used to change the bond's option value, it can be
515 * used for both enabling/changing an option and for disabling it. RTNL lock
516 * must be obtained before calling this function.
517 */
518int __bond_opt_set(struct bonding *bond,
519 unsigned int option, struct bond_opt_value *val)
520{
521 struct bond_opt_value *retval = NULL;
522 const struct bond_option *opt;
523 int ret = -ENOENT;
524
525 ASSERT_RTNL();
526
527 opt = bond_opt_get(option);
528 if (WARN_ON(!val) || WARN_ON(!opt))
529 goto out;
530 ret = bond_opt_check_deps(bond, opt);
531 if (ret)
532 goto out;
533 retval = bond_opt_parse(opt, val);
534 if (!retval) {
535 ret = -EINVAL;
536 goto out;
537 }
538 ret = opt->set(bond, retval);
539out:
540 if (ret)
541 bond_opt_error_interpret(bond, opt, ret, val);
542
543 return ret;
544}
545
546/**
547 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
548 * @bond: target bond device
549 * @option: option to set
550 * @buf: value to set it to
551 *
552 * This function tries to acquire RTNL without blocking and if successful
553 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
554 */
555int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
556{
557 struct bond_opt_value optval;
558 int ret;
559
560 if (!rtnl_trylock())
561 return restart_syscall();
562 bond_opt_initstr(&optval, buf);
563 ret = __bond_opt_set(bond, option, &optval);
564 rtnl_unlock();
565
566 return ret;
567}
568
569/**
570 * bond_opt_get - get a pointer to an option
571 * @option: option for which to return a pointer
572 *
573 * This function checks if option is valid and if so returns a pointer
574 * to its entry in the bond_opts[] option array.
575 */
576struct bond_option *bond_opt_get(unsigned int option)
577{
578 if (!BOND_OPT_VALID(option))
579 return NULL;
580
581 return &bond_opts[option];
582}
583
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100584int bond_option_mode_set(struct bonding *bond, struct bond_opt_value *newval)
Jiri Pirko72be35f2013-10-18 17:43:34 +0200585{
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100586 if (BOND_NO_USES_ARP(newval->value) && bond->params.arp_interval) {
dingtianhongfe9d04a2013-11-22 22:28:43 +0800587 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100588 bond->dev->name, newval->string);
dingtianhongfe9d04a2013-11-22 22:28:43 +0800589 /* disable arp monitoring */
590 bond->params.arp_interval = 0;
591 /* set miimon to default value */
592 bond->params.miimon = BOND_DEFAULT_MIIMON;
593 pr_info("%s: Setting MII monitoring interval to %d.\n",
594 bond->dev->name, bond->params.miimon);
Jiri Pirko72be35f2013-10-18 17:43:34 +0200595 }
596
597 /* don't cache arp_validate between modes */
598 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100599 bond->params.mode = newval->value;
600
Jiri Pirko72be35f2013-10-18 17:43:34 +0200601 return 0;
602}
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200603
Jiri Pirko752d48b2013-10-18 17:43:37 +0200604static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
605 struct slave *slave)
606{
607 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
608}
609
610struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
611{
612 struct slave *slave = rcu_dereference(bond->curr_active_slave);
613
614 return __bond_option_active_slave_get(bond, slave);
615}
616
617struct net_device *bond_option_active_slave_get(struct bonding *bond)
618{
619 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
620}
621
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200622int bond_option_active_slave_set(struct bonding *bond,
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100623 struct bond_opt_value *newval)
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200624{
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100625 char ifname[IFNAMSIZ] = { 0, };
626 struct net_device *slave_dev;
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200627 int ret = 0;
628
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100629 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
630 if (!strlen(ifname) || newval->string[0] == '\n') {
631 slave_dev = NULL;
632 } else {
633 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
634 if (!slave_dev)
635 return -ENODEV;
636 }
637
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200638 if (slave_dev) {
639 if (!netif_is_bond_slave(slave_dev)) {
640 pr_err("Device %s is not bonding slave.\n",
641 slave_dev->name);
642 return -EINVAL;
643 }
644
645 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
646 pr_err("%s: Device %s is not our slave.\n",
647 bond->dev->name, slave_dev->name);
648 return -EINVAL;
649 }
650 }
651
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200652 block_netpoll_tx();
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200653 write_lock_bh(&bond->curr_slave_lock);
654
655 /* check to see if we are clearing active */
656 if (!slave_dev) {
657 pr_info("%s: Clearing current active slave.\n",
658 bond->dev->name);
659 rcu_assign_pointer(bond->curr_active_slave, NULL);
660 bond_select_active_slave(bond);
661 } else {
662 struct slave *old_active = bond->curr_active_slave;
663 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
664
665 BUG_ON(!new_active);
666
667 if (new_active == old_active) {
668 /* do nothing */
669 pr_info("%s: %s is already the current active slave.\n",
670 bond->dev->name, new_active->dev->name);
671 } else {
672 if (old_active && (new_active->link == BOND_LINK_UP) &&
673 IS_UP(new_active->dev)) {
674 pr_info("%s: Setting %s as active slave.\n",
675 bond->dev->name, new_active->dev->name);
676 bond_change_active_slave(bond, new_active);
677 } else {
678 pr_err("%s: Could not set %s as active slave; either %s is down or the link is down.\n",
679 bond->dev->name, new_active->dev->name,
680 new_active->dev->name);
681 ret = -EINVAL;
682 }
683 }
684 }
685
686 write_unlock_bh(&bond->curr_slave_lock);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200687 unblock_netpoll_tx();
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100688
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200689 return ret;
690}
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800691
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100692int bond_option_miimon_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800693{
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100694 pr_info("%s: Setting MII monitoring interval to %llu.\n",
695 bond->dev->name, newval->value);
696 bond->params.miimon = newval->value;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800697 if (bond->params.updelay)
698 pr_info("%s: Note: Updating updelay (to %d) since it is a multiple of the miimon value.\n",
699 bond->dev->name,
700 bond->params.updelay * bond->params.miimon);
701 if (bond->params.downdelay)
702 pr_info("%s: Note: Updating downdelay (to %d) since it is a multiple of the miimon value.\n",
703 bond->dev->name,
704 bond->params.downdelay * bond->params.miimon);
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100705 if (newval->value && bond->params.arp_interval) {
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800706 pr_info("%s: MII monitoring cannot be used with ARP monitoring. Disabling ARP monitoring...\n",
707 bond->dev->name);
708 bond->params.arp_interval = 0;
709 if (bond->params.arp_validate)
710 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
711 }
712 if (bond->dev->flags & IFF_UP) {
713 /* If the interface is up, we may need to fire off
714 * the MII timer. If the interface is down, the
715 * timer will get fired off when the open function
716 * is called.
717 */
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100718 if (!newval->value) {
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800719 cancel_delayed_work_sync(&bond->mii_work);
720 } else {
721 cancel_delayed_work_sync(&bond->arp_work);
722 queue_delayed_work(bond->wq, &bond->mii_work, 0);
723 }
724 }
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100725
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800726 return 0;
727}
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800728
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100729int bond_option_updelay_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800730{
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100731 int value = newval->value;
732
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100733 if (!bond->params.miimon) {
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800734 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
735 bond->dev->name);
736 return -EPERM;
737 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100738 if ((value % bond->params.miimon) != 0) {
739 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
740 bond->dev->name, value,
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100741 bond->params.miimon,
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100742 (value / bond->params.miimon) *
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100743 bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800744 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100745 bond->params.updelay = value / bond->params.miimon;
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100746 pr_info("%s: Setting up delay to %d.\n",
747 bond->dev->name,
748 bond->params.updelay * bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800749
750 return 0;
751}
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800752
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100753int bond_option_downdelay_set(struct bonding *bond,
754 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800755{
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100756 int value = newval->value;
757
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100758 if (!bond->params.miimon) {
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800759 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
760 bond->dev->name);
761 return -EPERM;
762 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100763 if ((value % bond->params.miimon) != 0) {
764 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
765 bond->dev->name, value,
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100766 bond->params.miimon,
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100767 (value / bond->params.miimon) *
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100768 bond->params.miimon);
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800769 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100770 bond->params.downdelay = value / bond->params.miimon;
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100771 pr_info("%s: Setting down delay to %d.\n",
772 bond->dev->name,
773 bond->params.downdelay * bond->params.miimon);
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800774
775 return 0;
776}
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800777
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100778int bond_option_use_carrier_set(struct bonding *bond,
779 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800780{
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100781 pr_info("%s: Setting use_carrier to %llu.\n",
782 bond->dev->name, newval->value);
783 bond->params.use_carrier = newval->value;
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800784
785 return 0;
786}
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800787
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100788int bond_option_arp_interval_set(struct bonding *bond,
789 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800790{
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100791 pr_info("%s: Setting ARP monitoring interval to %llu.\n",
792 bond->dev->name, newval->value);
793 bond->params.arp_interval = newval->value;
794 if (newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800795 if (bond->params.miimon) {
796 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring.\n",
797 bond->dev->name, bond->dev->name);
798 bond->params.miimon = 0;
799 }
800 if (!bond->params.arp_targets[0])
801 pr_info("%s: ARP monitoring has been set up, but no ARP targets have been specified.\n",
802 bond->dev->name);
803 }
804 if (bond->dev->flags & IFF_UP) {
805 /* If the interface is up, we may need to fire off
806 * the ARP timer. If the interface is down, the
807 * timer will get fired off when the open function
808 * is called.
809 */
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100810 if (!newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800811 if (bond->params.arp_validate)
812 bond->recv_probe = NULL;
813 cancel_delayed_work_sync(&bond->arp_work);
814 } else {
815 /* arp_validate can be set only in active-backup mode */
816 if (bond->params.arp_validate)
817 bond->recv_probe = bond_arp_rcv;
818 cancel_delayed_work_sync(&bond->mii_work);
819 queue_delayed_work(bond->wq, &bond->arp_work, 0);
820 }
821 }
822
823 return 0;
824}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800825
826static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
827 __be32 target,
828 unsigned long last_rx)
829{
830 __be32 *targets = bond->params.arp_targets;
831 struct list_head *iter;
832 struct slave *slave;
833
834 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
835 bond_for_each_slave(bond, slave, iter)
836 slave->target_last_arp_rx[slot] = last_rx;
837 targets[slot] = target;
838 }
839}
840
841static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
842{
843 __be32 *targets = bond->params.arp_targets;
844 int ind;
845
846 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
847 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
848 bond->dev->name, &target);
849 return -EINVAL;
850 }
851
852 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
853 pr_err("%s: ARP target %pI4 is already present\n",
854 bond->dev->name, &target);
855 return -EINVAL;
856 }
857
858 ind = bond_get_targets_ip(targets, 0); /* first free slot */
859 if (ind == -1) {
860 pr_err("%s: ARP target table is full!\n",
861 bond->dev->name);
862 return -EINVAL;
863 }
864
865 pr_info("%s: adding ARP target %pI4.\n", bond->dev->name, &target);
866
867 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
868
869 return 0;
870}
871
872int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
873{
874 int ret;
875
876 /* not to race with bond_arp_rcv */
877 write_lock_bh(&bond->lock);
878 ret = _bond_option_arp_ip_target_add(bond, target);
879 write_unlock_bh(&bond->lock);
880
881 return ret;
882}
883
884int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
885{
886 __be32 *targets = bond->params.arp_targets;
887 struct list_head *iter;
888 struct slave *slave;
889 unsigned long *targets_rx;
890 int ind, i;
891
892 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
893 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
894 bond->dev->name, &target);
895 return -EINVAL;
896 }
897
898 ind = bond_get_targets_ip(targets, target);
899 if (ind == -1) {
900 pr_err("%s: unable to remove nonexistent ARP target %pI4.\n",
901 bond->dev->name, &target);
902 return -EINVAL;
903 }
904
905 if (ind == 0 && !targets[1] && bond->params.arp_interval)
906 pr_warn("%s: removing last arp target with arp_interval on\n",
907 bond->dev->name);
908
909 pr_info("%s: removing ARP target %pI4.\n", bond->dev->name,
910 &target);
911
912 /* not to race with bond_arp_rcv */
913 write_lock_bh(&bond->lock);
914
915 bond_for_each_slave(bond, slave, iter) {
916 targets_rx = slave->target_last_arp_rx;
917 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
918 targets_rx[i] = targets_rx[i+1];
919 targets_rx[i] = 0;
920 }
921 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
922 targets[i] = targets[i+1];
923 targets[i] = 0;
924
925 write_unlock_bh(&bond->lock);
926
927 return 0;
928}
929
Nikolay Aleksandrov4fb0ef52014-01-22 14:53:24 +0100930void bond_option_arp_ip_targets_clear(struct bonding *bond)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800931{
Nikolay Aleksandrov4fb0ef52014-01-22 14:53:24 +0100932 int i;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800933
934 /* not to race with bond_arp_rcv */
935 write_lock_bh(&bond->lock);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800936 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
937 _bond_options_arp_ip_target_set(bond, i, 0, 0);
Nikolay Aleksandrov4fb0ef52014-01-22 14:53:24 +0100938 write_unlock_bh(&bond->lock);
939}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800940
Nikolay Aleksandrov4fb0ef52014-01-22 14:53:24 +0100941int bond_option_arp_ip_targets_set(struct bonding *bond,
942 struct bond_opt_value *newval)
943{
944 int ret = -EPERM;
945 __be32 target;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800946
Nikolay Aleksandrov4fb0ef52014-01-22 14:53:24 +0100947 if (newval->string) {
948 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
949 pr_err("%s: invalid ARP target %pI4 specified\n",
950 bond->dev->name, &target);
951 return ret;
952 }
953 if (newval->string[0] == '+')
954 ret = bond_option_arp_ip_target_add(bond, target);
955 else if (newval->string[0] == '-')
956 ret = bond_option_arp_ip_target_rem(bond, target);
957 else
958 pr_err("no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
959 bond->dev->name);
960 } else {
961 target = newval->value;
962 ret = bond_option_arp_ip_target_add(bond, target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800963 }
964
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800965 return ret;
966}
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800967
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100968int bond_option_arp_validate_set(struct bonding *bond,
969 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800970{
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100971 pr_info("%s: setting arp_validate to %s (%llu).\n",
972 bond->dev->name, newval->string, newval->value);
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800973
974 if (bond->dev->flags & IFF_UP) {
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100975 if (!newval->value)
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800976 bond->recv_probe = NULL;
977 else if (bond->params.arp_interval)
978 bond->recv_probe = bond_arp_rcv;
979 }
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100980 bond->params.arp_validate = newval->value;
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -0800981
982 return 0;
983}
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800984
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100985int bond_option_arp_all_targets_set(struct bonding *bond,
986 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800987{
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100988 pr_info("%s: setting arp_all_targets to %s (%llu).\n",
989 bond->dev->name, newval->string, newval->value);
990 bond->params.arp_all_targets = newval->value;
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -0800991
992 return 0;
993}
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800994
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100995int bond_option_primary_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800996{
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100997 char *p, *primary = newval->string;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -0800998 struct list_head *iter;
999 struct slave *slave;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001000
1001 block_netpoll_tx();
1002 read_lock(&bond->lock);
1003 write_lock_bh(&bond->curr_slave_lock);
1004
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001005 p = strchr(primary, '\n');
1006 if (p)
1007 *p = '\0';
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001008 /* check to see if we are clearing primary */
1009 if (!strlen(primary)) {
1010 pr_info("%s: Setting primary slave to None.\n",
1011 bond->dev->name);
1012 bond->primary_slave = NULL;
1013 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1014 bond_select_active_slave(bond);
1015 goto out;
1016 }
1017
1018 bond_for_each_slave(bond, slave, iter) {
1019 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1020 pr_info("%s: Setting %s as primary slave.\n",
1021 bond->dev->name, slave->dev->name);
1022 bond->primary_slave = slave;
1023 strcpy(bond->params.primary, slave->dev->name);
1024 bond_select_active_slave(bond);
1025 goto out;
1026 }
1027 }
1028
dingtianhongc59ab672014-01-18 16:28:52 +08001029 if (bond->primary_slave) {
1030 pr_info("%s: Setting primary slave to None.\n",
1031 bond->dev->name);
1032 bond->primary_slave = NULL;
1033 bond_select_active_slave(bond);
1034 }
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001035 strncpy(bond->params.primary, primary, IFNAMSIZ);
1036 bond->params.primary[IFNAMSIZ - 1] = 0;
1037
1038 pr_info("%s: Recording %s as primary, but it has not been enslaved to %s yet.\n",
1039 bond->dev->name, primary, bond->dev->name);
1040
1041out:
1042 write_unlock_bh(&bond->curr_slave_lock);
1043 read_unlock(&bond->lock);
1044 unblock_netpoll_tx();
1045
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001046 return 0;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001047}
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001048
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01001049int bond_option_primary_reselect_set(struct bonding *bond,
1050 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001051{
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01001052 pr_info("%s: setting primary_reselect to %s (%llu).\n",
1053 bond->dev->name, newval->string, newval->value);
1054 bond->params.primary_reselect = newval->value;
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001055
1056 block_netpoll_tx();
1057 write_lock_bh(&bond->curr_slave_lock);
1058 bond_select_active_slave(bond);
1059 write_unlock_bh(&bond->curr_slave_lock);
1060 unblock_netpoll_tx();
1061
1062 return 0;
1063}
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001064
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01001065int bond_option_fail_over_mac_set(struct bonding *bond,
1066 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001067{
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01001068 pr_info("%s: Setting fail_over_mac to %s (%llu).\n",
1069 bond->dev->name, newval->string, newval->value);
1070 bond->params.fail_over_mac = newval->value;
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001071
1072 return 0;
1073}
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001074
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01001075int bond_option_xmit_hash_policy_set(struct bonding *bond,
1076 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001077{
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01001078 pr_info("%s: setting xmit hash policy to %s (%llu).\n",
1079 bond->dev->name, newval->string, newval->value);
1080 bond->params.xmit_policy = newval->value;
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001081
1082 return 0;
1083}
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001084
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +01001085int bond_option_resend_igmp_set(struct bonding *bond,
1086 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001087{
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +01001088 pr_info("%s: Setting resend_igmp to %llu.\n",
1089 bond->dev->name, newval->value);
1090 bond->params.resend_igmp = newval->value;
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001091
1092 return 0;
1093}
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001094
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +01001095int bond_option_num_peer_notif_set(struct bonding *bond,
1096 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001097{
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +01001098 bond->params.num_peer_notif = newval->value;
1099
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001100 return 0;
1101}
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001102
1103int bond_option_all_slaves_active_set(struct bonding *bond,
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001104 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001105{
1106 struct list_head *iter;
1107 struct slave *slave;
1108
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001109 if (newval->value == bond->params.all_slaves_active)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001110 return 0;
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001111 bond->params.all_slaves_active = newval->value;
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001112 bond_for_each_slave(bond, slave, iter) {
1113 if (!bond_is_active_slave(slave)) {
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001114 if (newval->value)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001115 slave->inactive = 0;
1116 else
1117 slave->inactive = 1;
1118 }
1119 }
1120
1121 return 0;
1122}
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001123
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +01001124int bond_option_min_links_set(struct bonding *bond,
1125 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001126{
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +01001127 pr_info("%s: Setting min links value to %llu\n",
1128 bond->dev->name, newval->value);
1129 bond->params.min_links = newval->value;
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001130
1131 return 0;
1132}
sfeldma@cumulusnetworks.com8d836d02013-12-17 21:30:30 -08001133
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +01001134int bond_option_lp_interval_set(struct bonding *bond,
1135 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8d836d02013-12-17 21:30:30 -08001136{
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +01001137 bond->params.lp_interval = newval->value;
sfeldma@cumulusnetworks.com8d836d02013-12-17 21:30:30 -08001138
1139 return 0;
1140}
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001141
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001142int bond_option_pps_set(struct bonding *bond, struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001143{
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001144 bond->params.packets_per_slave = newval->value;
1145 if (newval->value > 0) {
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001146 bond->params.reciprocal_packets_per_slave =
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001147 reciprocal_value(newval->value);
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001148 } else {
1149 /* reciprocal_packets_per_slave is unused if
1150 * packets_per_slave is 0 or 1, just initialize it
1151 */
1152 bond->params.reciprocal_packets_per_slave =
1153 (struct reciprocal_value) { 0 };
1154 }
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001155
1156 return 0;
1157}
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001158
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01001159int bond_option_lacp_rate_set(struct bonding *bond,
1160 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001161{
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01001162 pr_info("%s: Setting LACP rate to %s (%llu).\n",
1163 bond->dev->name, newval->string, newval->value);
1164 bond->params.lacp_fast = newval->value;
sfeldma@cumulusnetworks.com3243c472014-01-03 14:28:18 -08001165 bond_3ad_update_lacp_rate(bond);
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001166
1167 return 0;
1168}
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001169
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01001170int bond_option_ad_select_set(struct bonding *bond,
1171 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001172{
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01001173 pr_info("%s: Setting ad_select to %s (%llu).\n",
1174 bond->dev->name, newval->string, newval->value);
1175 bond->params.ad_select = newval->value;
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001176
1177 return 0;
1178}
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001179
1180int bond_option_queue_id_set(struct bonding *bond,
1181 struct bond_opt_value *newval)
1182{
1183 struct slave *slave, *update_slave;
1184 struct net_device *sdev;
1185 struct list_head *iter;
1186 char *delim;
1187 int ret = 0;
1188 u16 qid;
1189
1190 /* delim will point to queue id if successful */
1191 delim = strchr(newval->string, ':');
1192 if (!delim)
1193 goto err_no_cmd;
1194
1195 /* Terminate string that points to device name and bump it
1196 * up one, so we can read the queue id there.
1197 */
1198 *delim = '\0';
1199 if (sscanf(++delim, "%hd\n", &qid) != 1)
1200 goto err_no_cmd;
1201
1202 /* Check buffer length, valid ifname and queue id */
1203 if (strlen(newval->string) > IFNAMSIZ ||
1204 !dev_valid_name(newval->string) ||
1205 qid > bond->dev->real_num_tx_queues)
1206 goto err_no_cmd;
1207
1208 /* Get the pointer to that interface if it exists */
1209 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1210 if (!sdev)
1211 goto err_no_cmd;
1212
1213 /* Search for thes slave and check for duplicate qids */
1214 update_slave = NULL;
1215 bond_for_each_slave(bond, slave, iter) {
1216 if (sdev == slave->dev)
1217 /* We don't need to check the matching
1218 * slave for dups, since we're overwriting it
1219 */
1220 update_slave = slave;
1221 else if (qid && qid == slave->queue_id) {
1222 goto err_no_cmd;
1223 }
1224 }
1225
1226 if (!update_slave)
1227 goto err_no_cmd;
1228
1229 /* Actually set the qids for the slave */
1230 update_slave->queue_id = qid;
1231
1232out:
1233 return ret;
1234
1235err_no_cmd:
1236 pr_info("invalid input for queue_id set for %s.\n",
1237 bond->dev->name);
1238 ret = -EPERM;
1239 goto out;
1240
1241}
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001242
1243int bond_option_slaves_set(struct bonding *bond, struct bond_opt_value *newval)
1244{
1245 char command[IFNAMSIZ + 1] = { 0, };
1246 struct net_device *dev;
1247 char *ifname;
1248 int ret;
1249
1250 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1251 ifname = command + 1;
1252 if ((strlen(command) <= 1) ||
1253 !dev_valid_name(ifname))
1254 goto err_no_cmd;
1255
1256 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1257 if (!dev) {
1258 pr_info("%s: Interface %s does not exist!\n",
1259 bond->dev->name, ifname);
1260 ret = -ENODEV;
1261 goto out;
1262 }
1263
1264 switch (command[0]) {
1265 case '+':
1266 pr_info("%s: Adding slave %s.\n", bond->dev->name, dev->name);
1267 ret = bond_enslave(bond->dev, dev);
1268 break;
1269
1270 case '-':
1271 pr_info("%s: Removing slave %s.\n", bond->dev->name, dev->name);
1272 ret = bond_release(bond->dev, dev);
1273 break;
1274
1275 default:
1276 goto err_no_cmd;
1277 }
1278
1279out:
1280 return ret;
1281
1282err_no_cmd:
1283 pr_err("no command found in slaves file for bond %s. Use +ifname or -ifname.\n",
1284 bond->dev->name);
1285 ret = -EPERM;
1286 goto out;
1287}