blob: fc6d25e7d053b021425aa3ac769e35d88d15e60b [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 Aleksandrov4fb0ef582014-01-22 14:53:24 +010020#include <linux/inet.h>
Jiri Pirko72be35f2013-10-18 17:43:34 +020021#include "bonding.h"
22
stephen hemmingerf3253332014-03-04 16:36:44 -080023static int bond_option_active_slave_set(struct bonding *bond,
24 struct bond_opt_value *newval);
25static int bond_option_miimon_set(struct bonding *bond,
26 struct bond_opt_value *newval);
27static int bond_option_updelay_set(struct bonding *bond,
28 struct bond_opt_value *newval);
29static int bond_option_downdelay_set(struct bonding *bond,
30 struct bond_opt_value *newval);
31static int bond_option_use_carrier_set(struct bonding *bond,
32 struct bond_opt_value *newval);
33static int bond_option_arp_interval_set(struct bonding *bond,
34 struct bond_opt_value *newval);
35static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
36static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
37static int bond_option_arp_ip_targets_set(struct bonding *bond,
38 struct bond_opt_value *newval);
39static int bond_option_arp_validate_set(struct bonding *bond,
40 struct bond_opt_value *newval);
41static int bond_option_arp_all_targets_set(struct bonding *bond,
42 struct bond_opt_value *newval);
43static int bond_option_primary_set(struct bonding *bond,
44 struct bond_opt_value *newval);
45static int bond_option_primary_reselect_set(struct bonding *bond,
46 struct bond_opt_value *newval);
47static int bond_option_fail_over_mac_set(struct bonding *bond,
48 struct bond_opt_value *newval);
49static int bond_option_xmit_hash_policy_set(struct bonding *bond,
50 struct bond_opt_value *newval);
51static int bond_option_resend_igmp_set(struct bonding *bond,
52 struct bond_opt_value *newval);
53static int bond_option_num_peer_notif_set(struct bonding *bond,
54 struct bond_opt_value *newval);
55static int bond_option_all_slaves_active_set(struct bonding *bond,
56 struct bond_opt_value *newval);
57static int bond_option_min_links_set(struct bonding *bond,
58 struct bond_opt_value *newval);
59static int bond_option_lp_interval_set(struct bonding *bond,
60 struct bond_opt_value *newval);
61static int bond_option_pps_set(struct bonding *bond,
62 struct bond_opt_value *newval);
63static int bond_option_lacp_rate_set(struct bonding *bond,
64 struct bond_opt_value *newval);
65static int bond_option_ad_select_set(struct bonding *bond,
66 struct bond_opt_value *newval);
67static int bond_option_queue_id_set(struct bonding *bond,
68 struct bond_opt_value *newval);
69static int bond_option_mode_set(struct bonding *bond,
70 struct bond_opt_value *newval);
71static int bond_option_slaves_set(struct bonding *bond,
72 struct bond_opt_value *newval);
73
74
75static const struct bond_opt_value bond_mode_tbl[] = {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +010076 { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT},
77 { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
78 { "balance-xor", BOND_MODE_XOR, 0},
79 { "broadcast", BOND_MODE_BROADCAST, 0},
80 { "802.3ad", BOND_MODE_8023AD, 0},
81 { "balance-tlb", BOND_MODE_TLB, 0},
82 { "balance-alb", BOND_MODE_ALB, 0},
83 { NULL, -1, 0},
84};
85
stephen hemmingerf3253332014-03-04 16:36:44 -080086static const struct bond_opt_value bond_pps_tbl[] = {
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +010087 { "default", 1, BOND_VALFLAG_DEFAULT},
88 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
89 { NULL, -1, 0},
90};
91
stephen hemmingerf3253332014-03-04 16:36:44 -080092static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +010093 { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
94 { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
95 { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
96 { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
97 { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
98 { NULL, -1, 0},
99};
100
stephen hemmingerf3253332014-03-04 16:36:44 -0800101static const struct bond_opt_value bond_arp_validate_tbl[] = {
Veaceslav Falico896149f2014-02-18 07:48:40 +0100102 { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT},
103 { "active", BOND_ARP_VALIDATE_ACTIVE, 0},
104 { "backup", BOND_ARP_VALIDATE_BACKUP, 0},
105 { "all", BOND_ARP_VALIDATE_ALL, 0},
106 { "filter", BOND_ARP_FILTER, 0},
107 { "filter_active", BOND_ARP_FILTER_ACTIVE, 0},
108 { "filter_backup", BOND_ARP_FILTER_BACKUP, 0},
109 { NULL, -1, 0},
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100110};
111
stephen hemmingerf3253332014-03-04 16:36:44 -0800112static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100113 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
114 { "all", BOND_ARP_TARGETS_ALL, 0},
115 { NULL, -1, 0},
116};
117
stephen hemmingerf3253332014-03-04 16:36:44 -0800118static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +0100119 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
120 { "active", BOND_FOM_ACTIVE, 0},
121 { "follow", BOND_FOM_FOLLOW, 0},
122 { NULL, -1, 0},
123};
124
stephen hemmingerf3253332014-03-04 16:36:44 -0800125static const struct bond_opt_value bond_intmax_tbl[] = {
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100126 { "off", 0, BOND_VALFLAG_DEFAULT},
127 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
128};
129
stephen hemmingerf3253332014-03-04 16:36:44 -0800130static const struct bond_opt_value bond_lacp_rate_tbl[] = {
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +0100131 { "slow", AD_LACP_SLOW, 0},
132 { "fast", AD_LACP_FAST, 0},
133 { NULL, -1, 0},
134};
135
stephen hemmingerf3253332014-03-04 16:36:44 -0800136static const struct bond_opt_value bond_ad_select_tbl[] = {
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +0100137 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
138 { "bandwidth", BOND_AD_BANDWIDTH, 0},
139 { "count", BOND_AD_COUNT, 0},
140 { NULL, -1, 0},
141};
142
stephen hemmingerf3253332014-03-04 16:36:44 -0800143static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +0100144 { "off", 0, 0},
145 { "maxval", 255, BOND_VALFLAG_MAX},
146 { "default", 1, BOND_VALFLAG_DEFAULT},
147 { NULL, -1, 0}
148};
149
stephen hemmingerf3253332014-03-04 16:36:44 -0800150static const struct bond_opt_value bond_primary_reselect_tbl[] = {
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +0100151 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
152 { "better", BOND_PRI_RESELECT_BETTER, 0},
153 { "failure", BOND_PRI_RESELECT_FAILURE, 0},
154 { NULL, -1},
155};
156
stephen hemmingerf3253332014-03-04 16:36:44 -0800157static const struct bond_opt_value bond_use_carrier_tbl[] = {
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100158 { "off", 0, 0},
159 { "on", 1, BOND_VALFLAG_DEFAULT},
160 { NULL, -1, 0}
161};
162
stephen hemmingerf3253332014-03-04 16:36:44 -0800163static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +0100164 { "off", 0, BOND_VALFLAG_DEFAULT},
165 { "on", 1, 0},
166 { NULL, -1, 0}
167};
168
stephen hemmingerf3253332014-03-04 16:36:44 -0800169static const struct bond_opt_value bond_resend_igmp_tbl[] = {
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +0100170 { "off", 0, 0},
171 { "maxval", 255, BOND_VALFLAG_MAX},
172 { "default", 1, BOND_VALFLAG_DEFAULT},
173 { NULL, -1, 0}
174};
175
stephen hemmingerf3253332014-03-04 16:36:44 -0800176static const struct bond_opt_value bond_lp_interval_tbl[] = {
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100177 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
178 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
179};
180
stephen hemmingerf3253332014-03-04 16:36:44 -0800181static const struct bond_option bond_opts[] = {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100182 [BOND_OPT_MODE] = {
183 .id = BOND_OPT_MODE,
184 .name = "mode",
185 .desc = "bond device mode",
186 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
187 .values = bond_mode_tbl,
188 .set = bond_option_mode_set
189 },
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +0100190 [BOND_OPT_PACKETS_PER_SLAVE] = {
191 .id = BOND_OPT_PACKETS_PER_SLAVE,
192 .name = "packets_per_slave",
193 .desc = "Packets to send per slave in RR mode",
194 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
195 .values = bond_pps_tbl,
196 .set = bond_option_pps_set
197 },
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +0100198 [BOND_OPT_XMIT_HASH] = {
199 .id = BOND_OPT_XMIT_HASH,
200 .name = "xmit_hash_policy",
201 .desc = "balance-xor and 802.3ad hashing method",
202 .values = bond_xmit_hashtype_tbl,
203 .set = bond_option_xmit_hash_policy_set
204 },
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100205 [BOND_OPT_ARP_VALIDATE] = {
206 .id = BOND_OPT_ARP_VALIDATE,
207 .name = "arp_validate",
208 .desc = "validate src/dst of ARP probes",
Veaceslav Falico13ac34a2014-02-18 07:48:37 +0100209 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
210 BIT(BOND_MODE_ALB),
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100211 .values = bond_arp_validate_tbl,
212 .set = bond_option_arp_validate_set
213 },
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100214 [BOND_OPT_ARP_ALL_TARGETS] = {
215 .id = BOND_OPT_ARP_ALL_TARGETS,
216 .name = "arp_all_targets",
217 .desc = "fail on any/all arp targets timeout",
218 .values = bond_arp_all_targets_tbl,
219 .set = bond_option_arp_all_targets_set
220 },
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +0100221 [BOND_OPT_FAIL_OVER_MAC] = {
222 .id = BOND_OPT_FAIL_OVER_MAC,
223 .name = "fail_over_mac",
224 .desc = "For active-backup, do not set all slaves to the same MAC",
225 .flags = BOND_OPTFLAG_NOSLAVES,
226 .values = bond_fail_over_mac_tbl,
227 .set = bond_option_fail_over_mac_set
228 },
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100229 [BOND_OPT_ARP_INTERVAL] = {
230 .id = BOND_OPT_ARP_INTERVAL,
231 .name = "arp_interval",
232 .desc = "arp interval in milliseconds",
233 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
234 BIT(BOND_MODE_ALB),
235 .values = bond_intmax_tbl,
236 .set = bond_option_arp_interval_set
237 },
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100238 [BOND_OPT_ARP_TARGETS] = {
239 .id = BOND_OPT_ARP_TARGETS,
240 .name = "arp_ip_target",
241 .desc = "arp targets in n.n.n.n form",
242 .flags = BOND_OPTFLAG_RAWVAL,
243 .set = bond_option_arp_ip_targets_set
244 },
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100245 [BOND_OPT_DOWNDELAY] = {
246 .id = BOND_OPT_DOWNDELAY,
247 .name = "downdelay",
248 .desc = "Delay before considering link down, in milliseconds",
249 .values = bond_intmax_tbl,
250 .set = bond_option_downdelay_set
251 },
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100252 [BOND_OPT_UPDELAY] = {
253 .id = BOND_OPT_UPDELAY,
254 .name = "updelay",
255 .desc = "Delay before considering link up, in milliseconds",
256 .values = bond_intmax_tbl,
257 .set = bond_option_updelay_set
258 },
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +0100259 [BOND_OPT_LACP_RATE] = {
260 .id = BOND_OPT_LACP_RATE,
261 .name = "lacp_rate",
262 .desc = "LACPDU tx rate to request from 802.3ad partner",
263 .flags = BOND_OPTFLAG_IFDOWN,
264 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
265 .values = bond_lacp_rate_tbl,
266 .set = bond_option_lacp_rate_set
267 },
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +0100268 [BOND_OPT_MINLINKS] = {
269 .id = BOND_OPT_MINLINKS,
270 .name = "min_links",
271 .desc = "Minimum number of available links before turning on carrier",
272 .values = bond_intmax_tbl,
273 .set = bond_option_min_links_set
274 },
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +0100275 [BOND_OPT_AD_SELECT] = {
276 .id = BOND_OPT_AD_SELECT,
277 .name = "ad_select",
278 .desc = "803.ad aggregation selection logic",
279 .flags = BOND_OPTFLAG_IFDOWN,
280 .values = bond_ad_select_tbl,
281 .set = bond_option_ad_select_set
282 },
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +0100283 [BOND_OPT_NUM_PEER_NOTIF] = {
284 .id = BOND_OPT_NUM_PEER_NOTIF,
285 .name = "num_unsol_na",
286 .desc = "Number of peer notifications to send on failover event",
287 .values = bond_num_peer_notif_tbl,
288 .set = bond_option_num_peer_notif_set
289 },
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100290 [BOND_OPT_MIIMON] = {
291 .id = BOND_OPT_MIIMON,
292 .name = "miimon",
293 .desc = "Link check interval in milliseconds",
294 .values = bond_intmax_tbl,
295 .set = bond_option_miimon_set
296 },
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100297 [BOND_OPT_PRIMARY] = {
298 .id = BOND_OPT_PRIMARY,
299 .name = "primary",
300 .desc = "Primary network device to use",
301 .flags = BOND_OPTFLAG_RAWVAL,
302 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
303 BIT(BOND_MODE_TLB) |
304 BIT(BOND_MODE_ALB)),
305 .set = bond_option_primary_set
306 },
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +0100307 [BOND_OPT_PRIMARY_RESELECT] = {
308 .id = BOND_OPT_PRIMARY_RESELECT,
309 .name = "primary_reselect",
310 .desc = "Reselect primary slave once it comes up",
311 .values = bond_primary_reselect_tbl,
312 .set = bond_option_primary_reselect_set
313 },
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100314 [BOND_OPT_USE_CARRIER] = {
315 .id = BOND_OPT_USE_CARRIER,
316 .name = "use_carrier",
317 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
318 .values = bond_use_carrier_tbl,
319 .set = bond_option_use_carrier_set
320 },
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100321 [BOND_OPT_ACTIVE_SLAVE] = {
322 .id = BOND_OPT_ACTIVE_SLAVE,
323 .name = "active_slave",
324 .desc = "Currently active slave",
325 .flags = BOND_OPTFLAG_RAWVAL,
326 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
327 BIT(BOND_MODE_TLB) |
328 BIT(BOND_MODE_ALB)),
329 .set = bond_option_active_slave_set
330 },
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +0100331 [BOND_OPT_QUEUE_ID] = {
332 .id = BOND_OPT_QUEUE_ID,
333 .name = "queue_id",
334 .desc = "Set queue id of a slave",
335 .flags = BOND_OPTFLAG_RAWVAL,
336 .set = bond_option_queue_id_set
337 },
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +0100338 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
339 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
340 .name = "all_slaves_active",
341 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
342 .values = bond_all_slaves_active_tbl,
343 .set = bond_option_all_slaves_active_set
344 },
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +0100345 [BOND_OPT_RESEND_IGMP] = {
346 .id = BOND_OPT_RESEND_IGMP,
347 .name = "resend_igmp",
348 .desc = "Number of IGMP membership reports to send on link failure",
349 .values = bond_resend_igmp_tbl,
350 .set = bond_option_resend_igmp_set
351 },
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100352 [BOND_OPT_LP_INTERVAL] = {
353 .id = BOND_OPT_LP_INTERVAL,
354 .name = "lp_interval",
355 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
356 .values = bond_lp_interval_tbl,
357 .set = bond_option_lp_interval_set
358 },
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +0100359 [BOND_OPT_SLAVES] = {
360 .id = BOND_OPT_SLAVES,
361 .name = "slaves",
362 .desc = "Slave membership management",
363 .flags = BOND_OPTFLAG_RAWVAL,
364 .set = bond_option_slaves_set
365 },
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100366 { }
367};
368
369/* Searches for a value in opt's values[] table */
stephen hemmingerf3253332014-03-04 16:36:44 -0800370const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100371{
stephen hemmingerf3253332014-03-04 16:36:44 -0800372 const struct bond_option *opt;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100373 int i;
374
375 opt = bond_opt_get(option);
376 if (WARN_ON(!opt))
377 return NULL;
378 for (i = 0; opt->values && opt->values[i].string; i++)
379 if (opt->values[i].value == val)
380 return &opt->values[i];
381
382 return NULL;
383}
384
385/* Searches for a value in opt's values[] table which matches the flagmask */
stephen hemmingerf3253332014-03-04 16:36:44 -0800386static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100387 u32 flagmask)
388{
389 int i;
390
391 for (i = 0; opt->values && opt->values[i].string; i++)
392 if (opt->values[i].flags & flagmask)
393 return &opt->values[i];
394
395 return NULL;
396}
397
398/* If maxval is missing then there's no range to check. In case minval is
399 * missing then it's considered to be 0.
400 */
401static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
402{
stephen hemmingerf3253332014-03-04 16:36:44 -0800403 const struct bond_opt_value *minval, *maxval;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100404
405 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
406 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
407 if (!maxval || (minval && val < minval->value) || val > maxval->value)
408 return false;
409
410 return true;
411}
412
413/**
414 * bond_opt_parse - parse option value
415 * @opt: the option to parse against
416 * @val: value to parse
417 *
418 * This function tries to extract the value from @val and check if it's
419 * a possible match for the option and returns NULL if a match isn't found,
420 * or the struct_opt_value that matched. It also strips the new line from
421 * @val->string if it's present.
422 */
stephen hemmingerf3253332014-03-04 16:36:44 -0800423const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
424 struct bond_opt_value *val)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100425{
426 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
stephen hemmingerf3253332014-03-04 16:36:44 -0800427 const struct bond_opt_value *tbl;
428 const struct bond_opt_value *ret = NULL;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100429 bool checkval;
430 int i, rv;
431
432 /* No parsing if the option wants a raw val */
433 if (opt->flags & BOND_OPTFLAG_RAWVAL)
434 return val;
435
436 tbl = opt->values;
437 if (!tbl)
438 goto out;
439
440 /* ULLONG_MAX is used to bypass string processing */
441 checkval = val->value != ULLONG_MAX;
442 if (!checkval) {
443 if (!val->string)
444 goto out;
445 p = strchr(val->string, '\n');
446 if (p)
447 *p = '\0';
448 for (p = val->string; *p; p++)
449 if (!(isdigit(*p) || isspace(*p)))
450 break;
451 /* The following code extracts the string to match or the value
452 * and sets checkval appropriately
453 */
454 if (*p) {
455 rv = sscanf(val->string, "%32s", valstr);
456 } else {
457 rv = sscanf(val->string, "%llu", &val->value);
458 checkval = true;
459 }
460 if (!rv)
461 goto out;
462 }
463
464 for (i = 0; tbl[i].string; i++) {
465 /* Check for exact match */
466 if (checkval) {
467 if (val->value == tbl[i].value)
468 ret = &tbl[i];
469 } else {
470 if (!strcmp(valstr, "default") &&
471 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
472 ret = &tbl[i];
473
474 if (!strcmp(valstr, tbl[i].string))
475 ret = &tbl[i];
476 }
477 /* Found an exact match */
478 if (ret)
479 goto out;
480 }
481 /* Possible range match */
482 if (checkval && bond_opt_check_range(opt, val->value))
483 ret = val;
484out:
485 return ret;
486}
487
488/* Check opt's dependencies against bond mode and currently set options */
489static int bond_opt_check_deps(struct bonding *bond,
490 const struct bond_option *opt)
491{
492 struct bond_params *params = &bond->params;
493
494 if (test_bit(params->mode, &opt->unsuppmodes))
495 return -EACCES;
496 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
497 return -ENOTEMPTY;
498 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
499 return -EBUSY;
500
501 return 0;
502}
503
504static void bond_opt_dep_print(struct bonding *bond,
505 const struct bond_option *opt)
506{
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100507 struct bond_opt_value *modeval;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100508 struct bond_params *params;
509
510 params = &bond->params;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100511 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100512 if (test_bit(params->mode, &opt->unsuppmodes))
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100513 pr_err("%s: option %s: mode dependency failed, not supported in mode %s(%llu)\n",
514 bond->dev->name, opt->name,
515 modeval->string, modeval->value);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100516}
517
518static void bond_opt_error_interpret(struct bonding *bond,
519 const struct bond_option *opt,
520 int error, struct bond_opt_value *val)
521{
522 struct bond_opt_value *minval, *maxval;
523 char *p;
524
525 switch (error) {
526 case -EINVAL:
527 if (val) {
528 if (val->string) {
529 /* sometimes RAWVAL opts may have new lines */
530 p = strchr(val->string, '\n');
531 if (p)
532 *p = '\0';
Joe Perches90194262014-02-15 16:01:45 -0800533 pr_err("%s: option %s: invalid value (%s)\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100534 bond->dev->name, opt->name, val->string);
535 } else {
Joe Perches90194262014-02-15 16:01:45 -0800536 pr_err("%s: option %s: invalid value (%llu)\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100537 bond->dev->name, opt->name, val->value);
538 }
539 }
540 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
541 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
542 if (!maxval)
543 break;
Joe Perches90194262014-02-15 16:01:45 -0800544 pr_err("%s: option %s: allowed values %llu - %llu\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100545 bond->dev->name, opt->name, minval ? minval->value : 0,
546 maxval->value);
547 break;
548 case -EACCES:
549 bond_opt_dep_print(bond, opt);
550 break;
551 case -ENOTEMPTY:
Joe Perches90194262014-02-15 16:01:45 -0800552 pr_err("%s: option %s: unable to set because the bond device has slaves\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100553 bond->dev->name, opt->name);
554 break;
555 case -EBUSY:
Joe Perches90194262014-02-15 16:01:45 -0800556 pr_err("%s: option %s: unable to set because the bond device is up\n",
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100557 bond->dev->name, opt->name);
558 break;
559 default:
560 break;
561 }
562}
563
564/**
565 * __bond_opt_set - set a bonding option
566 * @bond: target bond device
567 * @option: option to set
568 * @val: value to set it to
569 *
570 * This function is used to change the bond's option value, it can be
571 * used for both enabling/changing an option and for disabling it. RTNL lock
572 * must be obtained before calling this function.
573 */
574int __bond_opt_set(struct bonding *bond,
575 unsigned int option, struct bond_opt_value *val)
576{
577 struct bond_opt_value *retval = NULL;
578 const struct bond_option *opt;
579 int ret = -ENOENT;
580
581 ASSERT_RTNL();
582
583 opt = bond_opt_get(option);
584 if (WARN_ON(!val) || WARN_ON(!opt))
585 goto out;
586 ret = bond_opt_check_deps(bond, opt);
587 if (ret)
588 goto out;
589 retval = bond_opt_parse(opt, val);
590 if (!retval) {
591 ret = -EINVAL;
592 goto out;
593 }
594 ret = opt->set(bond, retval);
595out:
596 if (ret)
597 bond_opt_error_interpret(bond, opt, ret, val);
598
599 return ret;
600}
601
602/**
603 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
604 * @bond: target bond device
605 * @option: option to set
606 * @buf: value to set it to
607 *
608 * This function tries to acquire RTNL without blocking and if successful
609 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
610 */
611int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
612{
613 struct bond_opt_value optval;
614 int ret;
615
616 if (!rtnl_trylock())
617 return restart_syscall();
618 bond_opt_initstr(&optval, buf);
619 ret = __bond_opt_set(bond, option, &optval);
620 rtnl_unlock();
621
622 return ret;
623}
624
625/**
626 * bond_opt_get - get a pointer to an option
627 * @option: option for which to return a pointer
628 *
629 * This function checks if option is valid and if so returns a pointer
630 * to its entry in the bond_opts[] option array.
631 */
stephen hemmingerf3253332014-03-04 16:36:44 -0800632const struct bond_option *bond_opt_get(unsigned int option)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100633{
634 if (!BOND_OPT_VALID(option))
635 return NULL;
636
637 return &bond_opts[option];
638}
639
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100640int bond_option_mode_set(struct bonding *bond, struct bond_opt_value *newval)
Jiri Pirko72be35f2013-10-18 17:43:34 +0200641{
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100642 if (BOND_NO_USES_ARP(newval->value) && bond->params.arp_interval) {
dingtianhongfe9d04a2013-11-22 22:28:43 +0800643 pr_info("%s: %s mode is incompatible with arp monitoring, start mii monitoring\n",
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100644 bond->dev->name, newval->string);
dingtianhongfe9d04a2013-11-22 22:28:43 +0800645 /* disable arp monitoring */
646 bond->params.arp_interval = 0;
647 /* set miimon to default value */
648 bond->params.miimon = BOND_DEFAULT_MIIMON;
Joe Perches90194262014-02-15 16:01:45 -0800649 pr_info("%s: Setting MII monitoring interval to %d\n",
dingtianhongfe9d04a2013-11-22 22:28:43 +0800650 bond->dev->name, bond->params.miimon);
Jiri Pirko72be35f2013-10-18 17:43:34 +0200651 }
652
653 /* don't cache arp_validate between modes */
654 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100655 bond->params.mode = newval->value;
656
Jiri Pirko72be35f2013-10-18 17:43:34 +0200657 return 0;
658}
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200659
Jiri Pirko752d48b2013-10-18 17:43:37 +0200660static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
661 struct slave *slave)
662{
663 return USES_PRIMARY(bond->params.mode) && slave ? slave->dev : NULL;
664}
665
666struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
667{
668 struct slave *slave = rcu_dereference(bond->curr_active_slave);
669
670 return __bond_option_active_slave_get(bond, slave);
671}
672
673struct net_device *bond_option_active_slave_get(struct bonding *bond)
674{
675 return __bond_option_active_slave_get(bond, bond->curr_active_slave);
676}
677
stephen hemmingerf3253332014-03-04 16:36:44 -0800678static int bond_option_active_slave_set(struct bonding *bond,
679 struct bond_opt_value *newval)
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200680{
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100681 char ifname[IFNAMSIZ] = { 0, };
682 struct net_device *slave_dev;
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200683 int ret = 0;
684
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100685 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
686 if (!strlen(ifname) || newval->string[0] == '\n') {
687 slave_dev = NULL;
688 } else {
689 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
690 if (!slave_dev)
691 return -ENODEV;
692 }
693
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200694 if (slave_dev) {
695 if (!netif_is_bond_slave(slave_dev)) {
Joe Perches90194262014-02-15 16:01:45 -0800696 pr_err("Device %s is not bonding slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200697 slave_dev->name);
698 return -EINVAL;
699 }
700
701 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
Joe Perches90194262014-02-15 16:01:45 -0800702 pr_err("%s: Device %s is not our slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200703 bond->dev->name, slave_dev->name);
704 return -EINVAL;
705 }
706 }
707
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200708 block_netpoll_tx();
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200709 write_lock_bh(&bond->curr_slave_lock);
710
711 /* check to see if we are clearing active */
712 if (!slave_dev) {
Joe Perches90194262014-02-15 16:01:45 -0800713 pr_info("%s: Clearing current active slave\n", bond->dev->name);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200714 rcu_assign_pointer(bond->curr_active_slave, NULL);
715 bond_select_active_slave(bond);
716 } else {
717 struct slave *old_active = bond->curr_active_slave;
718 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
719
720 BUG_ON(!new_active);
721
722 if (new_active == old_active) {
723 /* do nothing */
Joe Perches90194262014-02-15 16:01:45 -0800724 pr_info("%s: %s is already the current active slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200725 bond->dev->name, new_active->dev->name);
726 } else {
727 if (old_active && (new_active->link == BOND_LINK_UP) &&
728 IS_UP(new_active->dev)) {
Joe Perches90194262014-02-15 16:01:45 -0800729 pr_info("%s: Setting %s as active slave\n",
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200730 bond->dev->name, new_active->dev->name);
731 bond_change_active_slave(bond, new_active);
732 } else {
Joe Perches90194262014-02-15 16:01:45 -0800733 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 +0200734 bond->dev->name, new_active->dev->name,
735 new_active->dev->name);
736 ret = -EINVAL;
737 }
738 }
739 }
740
741 write_unlock_bh(&bond->curr_slave_lock);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200742 unblock_netpoll_tx();
Nikolay Aleksandrovd1fbd3e2014-01-22 14:53:35 +0100743
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200744 return ret;
745}
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800746
stephen hemmingerf3253332014-03-04 16:36:44 -0800747static int bond_option_miimon_set(struct bonding *bond,
748 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800749{
Joe Perches90194262014-02-15 16:01:45 -0800750 pr_info("%s: Setting MII monitoring interval to %llu\n",
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100751 bond->dev->name, newval->value);
752 bond->params.miimon = newval->value;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800753 if (bond->params.updelay)
Joe Perches90194262014-02-15 16:01:45 -0800754 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 -0800755 bond->dev->name,
756 bond->params.updelay * bond->params.miimon);
757 if (bond->params.downdelay)
Joe Perches90194262014-02-15 16:01:45 -0800758 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 -0800759 bond->dev->name,
760 bond->params.downdelay * bond->params.miimon);
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100761 if (newval->value && bond->params.arp_interval) {
Joe Perches90194262014-02-15 16:01:45 -0800762 pr_info("%s: MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n",
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800763 bond->dev->name);
764 bond->params.arp_interval = 0;
765 if (bond->params.arp_validate)
766 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
767 }
768 if (bond->dev->flags & IFF_UP) {
769 /* If the interface is up, we may need to fire off
770 * the MII timer. If the interface is down, the
771 * timer will get fired off when the open function
772 * is called.
773 */
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100774 if (!newval->value) {
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800775 cancel_delayed_work_sync(&bond->mii_work);
776 } else {
777 cancel_delayed_work_sync(&bond->arp_work);
778 queue_delayed_work(bond->wq, &bond->mii_work, 0);
779 }
780 }
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100781
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800782 return 0;
783}
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800784
stephen hemmingerf3253332014-03-04 16:36:44 -0800785static int bond_option_updelay_set(struct bonding *bond,
786 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800787{
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100788 int value = newval->value;
789
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100790 if (!bond->params.miimon) {
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800791 pr_err("%s: Unable to set up delay as MII monitoring is disabled\n",
792 bond->dev->name);
793 return -EPERM;
794 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100795 if ((value % bond->params.miimon) != 0) {
796 pr_warn("%s: Warning: up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
797 bond->dev->name, value,
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100798 bond->params.miimon,
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100799 (value / bond->params.miimon) *
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100800 bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800801 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100802 bond->params.updelay = value / bond->params.miimon;
Joe Perches90194262014-02-15 16:01:45 -0800803 pr_info("%s: Setting up delay to %d\n",
804 bond->dev->name, bond->params.updelay * bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800805
806 return 0;
807}
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800808
stephen hemmingerf3253332014-03-04 16:36:44 -0800809static int bond_option_downdelay_set(struct bonding *bond,
810 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800811{
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100812 int value = newval->value;
813
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100814 if (!bond->params.miimon) {
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800815 pr_err("%s: Unable to set down delay as MII monitoring is disabled\n",
816 bond->dev->name);
817 return -EPERM;
818 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100819 if ((value % bond->params.miimon) != 0) {
820 pr_warn("%s: Warning: down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
821 bond->dev->name, value,
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100822 bond->params.miimon,
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100823 (value / bond->params.miimon) *
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100824 bond->params.miimon);
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800825 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100826 bond->params.downdelay = value / bond->params.miimon;
Joe Perches90194262014-02-15 16:01:45 -0800827 pr_info("%s: Setting down delay to %d\n",
828 bond->dev->name, bond->params.downdelay * bond->params.miimon);
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800829
830 return 0;
831}
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800832
stephen hemmingerf3253332014-03-04 16:36:44 -0800833static int bond_option_use_carrier_set(struct bonding *bond,
834 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800835{
Joe Perches90194262014-02-15 16:01:45 -0800836 pr_info("%s: Setting use_carrier to %llu\n",
Nikolay Aleksandrov0fff0602014-01-22 14:53:34 +0100837 bond->dev->name, newval->value);
838 bond->params.use_carrier = newval->value;
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800839
840 return 0;
841}
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800842
stephen hemmingerf3253332014-03-04 16:36:44 -0800843static int bond_option_arp_interval_set(struct bonding *bond,
844 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800845{
Joe Perches90194262014-02-15 16:01:45 -0800846 pr_info("%s: Setting ARP monitoring interval to %llu\n",
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100847 bond->dev->name, newval->value);
848 bond->params.arp_interval = newval->value;
849 if (newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800850 if (bond->params.miimon) {
Joe Perches90194262014-02-15 16:01:45 -0800851 pr_info("%s: ARP monitoring cannot be used with MII monitoring. %s Disabling MII monitoring\n",
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800852 bond->dev->name, bond->dev->name);
853 bond->params.miimon = 0;
854 }
855 if (!bond->params.arp_targets[0])
Joe Perches90194262014-02-15 16:01:45 -0800856 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 -0800857 bond->dev->name);
858 }
859 if (bond->dev->flags & IFF_UP) {
860 /* If the interface is up, we may need to fire off
861 * the ARP timer. If the interface is down, the
862 * timer will get fired off when the open function
863 * is called.
864 */
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100865 if (!newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800866 if (bond->params.arp_validate)
867 bond->recv_probe = NULL;
868 cancel_delayed_work_sync(&bond->arp_work);
869 } else {
870 /* arp_validate can be set only in active-backup mode */
Veaceslav Falico3fe68df2014-02-18 07:48:39 +0100871 bond->recv_probe = bond_arp_rcv;
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800872 cancel_delayed_work_sync(&bond->mii_work);
873 queue_delayed_work(bond->wq, &bond->arp_work, 0);
874 }
875 }
876
877 return 0;
878}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800879
880static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
881 __be32 target,
882 unsigned long last_rx)
883{
884 __be32 *targets = bond->params.arp_targets;
885 struct list_head *iter;
886 struct slave *slave;
887
888 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
889 bond_for_each_slave(bond, slave, iter)
890 slave->target_last_arp_rx[slot] = last_rx;
891 targets[slot] = target;
892 }
893}
894
895static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
896{
897 __be32 *targets = bond->params.arp_targets;
898 int ind;
899
900 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
901 pr_err("%s: invalid ARP target %pI4 specified for addition\n",
902 bond->dev->name, &target);
903 return -EINVAL;
904 }
905
906 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
907 pr_err("%s: ARP target %pI4 is already present\n",
908 bond->dev->name, &target);
909 return -EINVAL;
910 }
911
912 ind = bond_get_targets_ip(targets, 0); /* first free slot */
913 if (ind == -1) {
Joe Perches90194262014-02-15 16:01:45 -0800914 pr_err("%s: ARP target table is full!\n", bond->dev->name);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800915 return -EINVAL;
916 }
917
Joe Perches90194262014-02-15 16:01:45 -0800918 pr_info("%s: Adding ARP target %pI4\n", bond->dev->name, &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800919
920 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
921
922 return 0;
923}
924
stephen hemmingerf3253332014-03-04 16:36:44 -0800925static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800926{
927 int ret;
928
929 /* not to race with bond_arp_rcv */
930 write_lock_bh(&bond->lock);
931 ret = _bond_option_arp_ip_target_add(bond, target);
932 write_unlock_bh(&bond->lock);
933
934 return ret;
935}
936
stephen hemmingerf3253332014-03-04 16:36:44 -0800937static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800938{
939 __be32 *targets = bond->params.arp_targets;
940 struct list_head *iter;
941 struct slave *slave;
942 unsigned long *targets_rx;
943 int ind, i;
944
945 if (IS_IP_TARGET_UNUSABLE_ADDRESS(target)) {
946 pr_err("%s: invalid ARP target %pI4 specified for removal\n",
947 bond->dev->name, &target);
948 return -EINVAL;
949 }
950
951 ind = bond_get_targets_ip(targets, target);
952 if (ind == -1) {
Joe Perches90194262014-02-15 16:01:45 -0800953 pr_err("%s: unable to remove nonexistent ARP target %pI4\n",
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800954 bond->dev->name, &target);
955 return -EINVAL;
956 }
957
958 if (ind == 0 && !targets[1] && bond->params.arp_interval)
Joe Perches90194262014-02-15 16:01:45 -0800959 pr_warn("%s: Removing last arp target with arp_interval on\n",
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800960 bond->dev->name);
961
Joe Perches90194262014-02-15 16:01:45 -0800962 pr_info("%s: Removing ARP target %pI4\n", bond->dev->name, &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800963
964 /* not to race with bond_arp_rcv */
965 write_lock_bh(&bond->lock);
966
967 bond_for_each_slave(bond, slave, iter) {
968 targets_rx = slave->target_last_arp_rx;
969 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
970 targets_rx[i] = targets_rx[i+1];
971 targets_rx[i] = 0;
972 }
973 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
974 targets[i] = targets[i+1];
975 targets[i] = 0;
976
977 write_unlock_bh(&bond->lock);
978
979 return 0;
980}
981
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100982void bond_option_arp_ip_targets_clear(struct bonding *bond)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800983{
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100984 int i;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800985
986 /* not to race with bond_arp_rcv */
987 write_lock_bh(&bond->lock);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800988 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
989 _bond_options_arp_ip_target_set(bond, i, 0, 0);
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100990 write_unlock_bh(&bond->lock);
991}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800992
stephen hemmingerf3253332014-03-04 16:36:44 -0800993static int bond_option_arp_ip_targets_set(struct bonding *bond,
994 struct bond_opt_value *newval)
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100995{
996 int ret = -EPERM;
997 __be32 target;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800998
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100999 if (newval->string) {
1000 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1001 pr_err("%s: invalid ARP target %pI4 specified\n",
1002 bond->dev->name, &target);
1003 return ret;
1004 }
1005 if (newval->string[0] == '+')
1006 ret = bond_option_arp_ip_target_add(bond, target);
1007 else if (newval->string[0] == '-')
1008 ret = bond_option_arp_ip_target_rem(bond, target);
1009 else
Joe Perches90194262014-02-15 16:01:45 -08001010 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 +01001011 bond->dev->name);
1012 } else {
1013 target = newval->value;
1014 ret = bond_option_arp_ip_target_add(bond, target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001015 }
1016
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001017 return ret;
1018}
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001019
stephen hemmingerf3253332014-03-04 16:36:44 -08001020static int bond_option_arp_validate_set(struct bonding *bond,
1021 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001022{
Joe Perches90194262014-02-15 16:01:45 -08001023 pr_info("%s: Setting arp_validate to %s (%llu)\n",
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01001024 bond->dev->name, newval->string, newval->value);
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001025
1026 if (bond->dev->flags & IFF_UP) {
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01001027 if (!newval->value)
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001028 bond->recv_probe = NULL;
1029 else if (bond->params.arp_interval)
1030 bond->recv_probe = bond_arp_rcv;
1031 }
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01001032 bond->params.arp_validate = newval->value;
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001033
1034 return 0;
1035}
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -08001036
stephen hemmingerf3253332014-03-04 16:36:44 -08001037static int bond_option_arp_all_targets_set(struct bonding *bond,
1038 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -08001039{
Joe Perches90194262014-02-15 16:01:45 -08001040 pr_info("%s: Setting arp_all_targets to %s (%llu)\n",
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +01001041 bond->dev->name, newval->string, newval->value);
1042 bond->params.arp_all_targets = newval->value;
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -08001043
1044 return 0;
1045}
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001046
stephen hemmingerf3253332014-03-04 16:36:44 -08001047static int bond_option_primary_set(struct bonding *bond,
1048 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001049{
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001050 char *p, *primary = newval->string;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001051 struct list_head *iter;
1052 struct slave *slave;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001053
1054 block_netpoll_tx();
1055 read_lock(&bond->lock);
1056 write_lock_bh(&bond->curr_slave_lock);
1057
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001058 p = strchr(primary, '\n');
1059 if (p)
1060 *p = '\0';
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001061 /* check to see if we are clearing primary */
1062 if (!strlen(primary)) {
Joe Perches90194262014-02-15 16:01:45 -08001063 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001064 bond->primary_slave = NULL;
1065 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1066 bond_select_active_slave(bond);
1067 goto out;
1068 }
1069
1070 bond_for_each_slave(bond, slave, iter) {
1071 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
Joe Perches90194262014-02-15 16:01:45 -08001072 pr_info("%s: Setting %s as primary slave\n",
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001073 bond->dev->name, slave->dev->name);
1074 bond->primary_slave = slave;
1075 strcpy(bond->params.primary, slave->dev->name);
1076 bond_select_active_slave(bond);
1077 goto out;
1078 }
1079 }
1080
dingtianhongc59ab672014-01-18 16:28:52 +08001081 if (bond->primary_slave) {
Joe Perches90194262014-02-15 16:01:45 -08001082 pr_info("%s: Setting primary slave to None\n", bond->dev->name);
dingtianhongc59ab672014-01-18 16:28:52 +08001083 bond->primary_slave = NULL;
1084 bond_select_active_slave(bond);
1085 }
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001086 strncpy(bond->params.primary, primary, IFNAMSIZ);
1087 bond->params.primary[IFNAMSIZ - 1] = 0;
1088
Joe Perches90194262014-02-15 16:01:45 -08001089 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 -08001090 bond->dev->name, primary, bond->dev->name);
1091
1092out:
1093 write_unlock_bh(&bond->curr_slave_lock);
1094 read_unlock(&bond->lock);
1095 unblock_netpoll_tx();
1096
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001097 return 0;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001098}
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001099
stephen hemmingerf3253332014-03-04 16:36:44 -08001100static int bond_option_primary_reselect_set(struct bonding *bond,
1101 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001102{
Joe Perches90194262014-02-15 16:01:45 -08001103 pr_info("%s: Setting primary_reselect to %s (%llu)\n",
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01001104 bond->dev->name, newval->string, newval->value);
1105 bond->params.primary_reselect = newval->value;
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001106
1107 block_netpoll_tx();
1108 write_lock_bh(&bond->curr_slave_lock);
1109 bond_select_active_slave(bond);
1110 write_unlock_bh(&bond->curr_slave_lock);
1111 unblock_netpoll_tx();
1112
1113 return 0;
1114}
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001115
stephen hemmingerf3253332014-03-04 16:36:44 -08001116static int bond_option_fail_over_mac_set(struct bonding *bond,
1117 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001118{
Joe Perches90194262014-02-15 16:01:45 -08001119 pr_info("%s: Setting fail_over_mac to %s (%llu)\n",
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01001120 bond->dev->name, newval->string, newval->value);
1121 bond->params.fail_over_mac = newval->value;
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001122
1123 return 0;
1124}
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001125
stephen hemmingerf3253332014-03-04 16:36:44 -08001126static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1127 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001128{
Joe Perches90194262014-02-15 16:01:45 -08001129 pr_info("%s: Setting xmit hash policy to %s (%llu)\n",
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01001130 bond->dev->name, newval->string, newval->value);
1131 bond->params.xmit_policy = newval->value;
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001132
1133 return 0;
1134}
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001135
stephen hemmingerf3253332014-03-04 16:36:44 -08001136static int bond_option_resend_igmp_set(struct bonding *bond,
1137 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001138{
Joe Perches90194262014-02-15 16:01:45 -08001139 pr_info("%s: Setting resend_igmp to %llu\n",
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +01001140 bond->dev->name, newval->value);
1141 bond->params.resend_igmp = newval->value;
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001142
1143 return 0;
1144}
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001145
stephen hemmingerf3253332014-03-04 16:36:44 -08001146static int bond_option_num_peer_notif_set(struct bonding *bond,
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +01001147 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001148{
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +01001149 bond->params.num_peer_notif = newval->value;
1150
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001151 return 0;
1152}
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001153
stephen hemmingerf3253332014-03-04 16:36:44 -08001154static int bond_option_all_slaves_active_set(struct bonding *bond,
1155 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001156{
1157 struct list_head *iter;
1158 struct slave *slave;
1159
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001160 if (newval->value == bond->params.all_slaves_active)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001161 return 0;
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001162 bond->params.all_slaves_active = newval->value;
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001163 bond_for_each_slave(bond, slave, iter) {
1164 if (!bond_is_active_slave(slave)) {
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001165 if (newval->value)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001166 slave->inactive = 0;
1167 else
1168 slave->inactive = 1;
1169 }
1170 }
1171
1172 return 0;
1173}
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001174
stephen hemmingerf3253332014-03-04 16:36:44 -08001175static int bond_option_min_links_set(struct bonding *bond,
1176 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001177{
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +01001178 pr_info("%s: Setting min links value to %llu\n",
1179 bond->dev->name, newval->value);
1180 bond->params.min_links = newval->value;
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001181
1182 return 0;
1183}
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001184
stephen hemmingerf3253332014-03-04 16:36:44 -08001185static int bond_option_lp_interval_set(struct bonding *bond,
1186 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001187{
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +01001188 bond->params.lp_interval = newval->value;
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001189
1190 return 0;
1191}
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001192
stephen hemmingerf3253332014-03-04 16:36:44 -08001193static int bond_option_pps_set(struct bonding *bond,
1194 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001195{
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001196 bond->params.packets_per_slave = newval->value;
1197 if (newval->value > 0) {
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001198 bond->params.reciprocal_packets_per_slave =
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001199 reciprocal_value(newval->value);
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001200 } else {
1201 /* reciprocal_packets_per_slave is unused if
1202 * packets_per_slave is 0 or 1, just initialize it
1203 */
1204 bond->params.reciprocal_packets_per_slave =
1205 (struct reciprocal_value) { 0 };
1206 }
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001207
1208 return 0;
1209}
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001210
stephen hemmingerf3253332014-03-04 16:36:44 -08001211static int bond_option_lacp_rate_set(struct bonding *bond,
1212 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001213{
Joe Perches90194262014-02-15 16:01:45 -08001214 pr_info("%s: Setting LACP rate to %s (%llu)\n",
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01001215 bond->dev->name, newval->string, newval->value);
1216 bond->params.lacp_fast = newval->value;
sfeldma@cumulusnetworks.com3243c472014-01-03 14:28:18 -08001217 bond_3ad_update_lacp_rate(bond);
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001218
1219 return 0;
1220}
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001221
stephen hemmingerf3253332014-03-04 16:36:44 -08001222static int bond_option_ad_select_set(struct bonding *bond,
1223 struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001224{
Joe Perches90194262014-02-15 16:01:45 -08001225 pr_info("%s: Setting ad_select to %s (%llu)\n",
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01001226 bond->dev->name, newval->string, newval->value);
1227 bond->params.ad_select = newval->value;
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001228
1229 return 0;
1230}
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001231
stephen hemmingerf3253332014-03-04 16:36:44 -08001232static int bond_option_queue_id_set(struct bonding *bond,
1233 struct bond_opt_value *newval)
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001234{
1235 struct slave *slave, *update_slave;
1236 struct net_device *sdev;
1237 struct list_head *iter;
1238 char *delim;
1239 int ret = 0;
1240 u16 qid;
1241
1242 /* delim will point to queue id if successful */
1243 delim = strchr(newval->string, ':');
1244 if (!delim)
1245 goto err_no_cmd;
1246
1247 /* Terminate string that points to device name and bump it
1248 * up one, so we can read the queue id there.
1249 */
1250 *delim = '\0';
1251 if (sscanf(++delim, "%hd\n", &qid) != 1)
1252 goto err_no_cmd;
1253
1254 /* Check buffer length, valid ifname and queue id */
dingtianhongc313c892014-02-12 14:58:50 +08001255 if (!dev_valid_name(newval->string) ||
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001256 qid > bond->dev->real_num_tx_queues)
1257 goto err_no_cmd;
1258
1259 /* Get the pointer to that interface if it exists */
1260 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1261 if (!sdev)
1262 goto err_no_cmd;
1263
1264 /* Search for thes slave and check for duplicate qids */
1265 update_slave = NULL;
1266 bond_for_each_slave(bond, slave, iter) {
1267 if (sdev == slave->dev)
1268 /* We don't need to check the matching
1269 * slave for dups, since we're overwriting it
1270 */
1271 update_slave = slave;
1272 else if (qid && qid == slave->queue_id) {
1273 goto err_no_cmd;
1274 }
1275 }
1276
1277 if (!update_slave)
1278 goto err_no_cmd;
1279
1280 /* Actually set the qids for the slave */
1281 update_slave->queue_id = qid;
1282
1283out:
1284 return ret;
1285
1286err_no_cmd:
Joe Perches90194262014-02-15 16:01:45 -08001287 pr_info("invalid input for queue_id set for %s\n", bond->dev->name);
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001288 ret = -EPERM;
1289 goto out;
1290
1291}
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001292
stephen hemmingerf3253332014-03-04 16:36:44 -08001293static int bond_option_slaves_set(struct bonding *bond,
1294 struct bond_opt_value *newval)
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001295{
1296 char command[IFNAMSIZ + 1] = { 0, };
1297 struct net_device *dev;
1298 char *ifname;
1299 int ret;
1300
1301 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1302 ifname = command + 1;
1303 if ((strlen(command) <= 1) ||
1304 !dev_valid_name(ifname))
1305 goto err_no_cmd;
1306
1307 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1308 if (!dev) {
Joe Perches90194262014-02-15 16:01:45 -08001309 pr_info("%s: interface %s does not exist!\n",
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001310 bond->dev->name, ifname);
1311 ret = -ENODEV;
1312 goto out;
1313 }
1314
1315 switch (command[0]) {
1316 case '+':
Joe Perches90194262014-02-15 16:01:45 -08001317 pr_info("%s: Adding slave %s\n", bond->dev->name, dev->name);
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001318 ret = bond_enslave(bond->dev, dev);
1319 break;
1320
1321 case '-':
Joe Perches90194262014-02-15 16:01:45 -08001322 pr_info("%s: Removing slave %s\n", bond->dev->name, dev->name);
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001323 ret = bond_release(bond->dev, dev);
1324 break;
1325
1326 default:
1327 goto err_no_cmd;
1328 }
1329
1330out:
1331 return ret;
1332
1333err_no_cmd:
Joe Perches90194262014-02-15 16:01:45 -08001334 pr_err("no command found in slaves file for bond %s - use +ifname or -ifname\n",
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001335 bond->dev->name);
1336 ret = -EPERM;
1337 goto out;
1338}