xref: /linux/drivers/net/bonding/bond_options.c (revision 4201c9260a8d3c4ef238e51692a7e9b4e1e29efe)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/net/bond/bond_options.c - bonding options
4  * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
5  * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/if.h>
10 #include <linux/netdevice.h>
11 #include <linux/spinlock.h>
12 #include <linux/rcupdate.h>
13 #include <linux/ctype.h>
14 #include <linux/inet.h>
15 #include <linux/sched/signal.h>
16 
17 #include <net/bonding.h>
18 
19 static int bond_option_active_slave_set(struct bonding *bond,
20 					const struct bond_opt_value *newval);
21 static int bond_option_miimon_set(struct bonding *bond,
22 				  const struct bond_opt_value *newval);
23 static int bond_option_updelay_set(struct bonding *bond,
24 				   const struct bond_opt_value *newval);
25 static int bond_option_downdelay_set(struct bonding *bond,
26 				     const struct bond_opt_value *newval);
27 static int bond_option_use_carrier_set(struct bonding *bond,
28 				       const struct bond_opt_value *newval);
29 static int bond_option_arp_interval_set(struct bonding *bond,
30 					const struct bond_opt_value *newval);
31 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
32 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
33 static int bond_option_arp_ip_targets_set(struct bonding *bond,
34 					  const struct bond_opt_value *newval);
35 static int bond_option_arp_validate_set(struct bonding *bond,
36 					const struct bond_opt_value *newval);
37 static int bond_option_arp_all_targets_set(struct bonding *bond,
38 					   const struct bond_opt_value *newval);
39 static int bond_option_primary_set(struct bonding *bond,
40 				   const struct bond_opt_value *newval);
41 static int bond_option_primary_reselect_set(struct bonding *bond,
42 					    const struct bond_opt_value *newval);
43 static int bond_option_fail_over_mac_set(struct bonding *bond,
44 					 const struct bond_opt_value *newval);
45 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
46 					    const struct bond_opt_value *newval);
47 static int bond_option_resend_igmp_set(struct bonding *bond,
48 				       const struct bond_opt_value *newval);
49 static int bond_option_num_peer_notif_set(struct bonding *bond,
50 					  const struct bond_opt_value *newval);
51 static int bond_option_all_slaves_active_set(struct bonding *bond,
52 					     const struct bond_opt_value *newval);
53 static int bond_option_min_links_set(struct bonding *bond,
54 				     const struct bond_opt_value *newval);
55 static int bond_option_lp_interval_set(struct bonding *bond,
56 				       const struct bond_opt_value *newval);
57 static int bond_option_pps_set(struct bonding *bond,
58 			       const struct bond_opt_value *newval);
59 static int bond_option_lacp_rate_set(struct bonding *bond,
60 				     const struct bond_opt_value *newval);
61 static int bond_option_ad_select_set(struct bonding *bond,
62 				     const struct bond_opt_value *newval);
63 static int bond_option_queue_id_set(struct bonding *bond,
64 				    const struct bond_opt_value *newval);
65 static int bond_option_mode_set(struct bonding *bond,
66 				const struct bond_opt_value *newval);
67 static int bond_option_slaves_set(struct bonding *bond,
68 				  const struct bond_opt_value *newval);
69 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
70 				  const struct bond_opt_value *newval);
71 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
72 					     const struct bond_opt_value *newval);
73 static int bond_option_ad_actor_system_set(struct bonding *bond,
74 					   const struct bond_opt_value *newval);
75 static int bond_option_ad_user_port_key_set(struct bonding *bond,
76 					    const struct bond_opt_value *newval);
77 
78 
79 static const struct bond_opt_value bond_mode_tbl[] = {
80 	{ "balance-rr",    BOND_MODE_ROUNDROBIN,   BOND_VALFLAG_DEFAULT},
81 	{ "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
82 	{ "balance-xor",   BOND_MODE_XOR,          0},
83 	{ "broadcast",     BOND_MODE_BROADCAST,    0},
84 	{ "802.3ad",       BOND_MODE_8023AD,       0},
85 	{ "balance-tlb",   BOND_MODE_TLB,          0},
86 	{ "balance-alb",   BOND_MODE_ALB,          0},
87 	{ NULL,            -1,                     0},
88 };
89 
90 static const struct bond_opt_value bond_pps_tbl[] = {
91 	{ "default", 1,         BOND_VALFLAG_DEFAULT},
92 	{ "maxval",  USHRT_MAX, BOND_VALFLAG_MAX},
93 	{ NULL,      -1,        0},
94 };
95 
96 static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
97 	{ "layer2",   BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
98 	{ "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
99 	{ "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
100 	{ "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
101 	{ "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
102 	{ NULL,       -1,                       0},
103 };
104 
105 static const struct bond_opt_value bond_arp_validate_tbl[] = {
106 	{ "none",		BOND_ARP_VALIDATE_NONE,		BOND_VALFLAG_DEFAULT},
107 	{ "active",		BOND_ARP_VALIDATE_ACTIVE,	0},
108 	{ "backup",		BOND_ARP_VALIDATE_BACKUP,	0},
109 	{ "all",		BOND_ARP_VALIDATE_ALL,		0},
110 	{ "filter",		BOND_ARP_FILTER,		0},
111 	{ "filter_active",	BOND_ARP_FILTER_ACTIVE,		0},
112 	{ "filter_backup",	BOND_ARP_FILTER_BACKUP,		0},
113 	{ NULL,			-1,				0},
114 };
115 
116 static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
117 	{ "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
118 	{ "all", BOND_ARP_TARGETS_ALL, 0},
119 	{ NULL,  -1,                   0},
120 };
121 
122 static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
123 	{ "none",   BOND_FOM_NONE,   BOND_VALFLAG_DEFAULT},
124 	{ "active", BOND_FOM_ACTIVE, 0},
125 	{ "follow", BOND_FOM_FOLLOW, 0},
126 	{ NULL,     -1,              0},
127 };
128 
129 static const struct bond_opt_value bond_intmax_tbl[] = {
130 	{ "off",     0,       BOND_VALFLAG_DEFAULT},
131 	{ "maxval",  INT_MAX, BOND_VALFLAG_MAX},
132 	{ NULL,      -1,      0}
133 };
134 
135 static const struct bond_opt_value bond_lacp_rate_tbl[] = {
136 	{ "slow", AD_LACP_SLOW, 0},
137 	{ "fast", AD_LACP_FAST, 0},
138 	{ NULL,   -1,           0},
139 };
140 
141 static const struct bond_opt_value bond_ad_select_tbl[] = {
142 	{ "stable",    BOND_AD_STABLE,    BOND_VALFLAG_DEFAULT},
143 	{ "bandwidth", BOND_AD_BANDWIDTH, 0},
144 	{ "count",     BOND_AD_COUNT,     0},
145 	{ NULL,        -1,                0},
146 };
147 
148 static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
149 	{ "off",     0,   0},
150 	{ "maxval",  255, BOND_VALFLAG_MAX},
151 	{ "default", 1,   BOND_VALFLAG_DEFAULT},
152 	{ NULL,      -1,  0}
153 };
154 
155 static const struct bond_opt_value bond_primary_reselect_tbl[] = {
156 	{ "always",  BOND_PRI_RESELECT_ALWAYS,  BOND_VALFLAG_DEFAULT},
157 	{ "better",  BOND_PRI_RESELECT_BETTER,  0},
158 	{ "failure", BOND_PRI_RESELECT_FAILURE, 0},
159 	{ NULL,      -1},
160 };
161 
162 static const struct bond_opt_value bond_use_carrier_tbl[] = {
163 	{ "off", 0,  0},
164 	{ "on",  1,  BOND_VALFLAG_DEFAULT},
165 	{ NULL,  -1, 0}
166 };
167 
168 static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
169 	{ "off", 0,  BOND_VALFLAG_DEFAULT},
170 	{ "on",  1,  0},
171 	{ NULL,  -1, 0}
172 };
173 
174 static const struct bond_opt_value bond_resend_igmp_tbl[] = {
175 	{ "off",     0,   0},
176 	{ "maxval",  255, BOND_VALFLAG_MAX},
177 	{ "default", 1,   BOND_VALFLAG_DEFAULT},
178 	{ NULL,      -1,  0}
179 };
180 
181 static const struct bond_opt_value bond_lp_interval_tbl[] = {
182 	{ "minval",  1,       BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
183 	{ "maxval",  INT_MAX, BOND_VALFLAG_MAX},
184 	{ NULL,      -1,      0},
185 };
186 
187 static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
188 	{ "off", 0,  0},
189 	{ "on",  1,  BOND_VALFLAG_DEFAULT},
190 	{ NULL,  -1, 0}
191 };
192 
193 static const struct bond_opt_value bond_ad_actor_sys_prio_tbl[] = {
194 	{ "minval",  1,     BOND_VALFLAG_MIN},
195 	{ "maxval",  65535, BOND_VALFLAG_MAX | BOND_VALFLAG_DEFAULT},
196 	{ NULL,      -1,    0},
197 };
198 
199 static const struct bond_opt_value bond_ad_user_port_key_tbl[] = {
200 	{ "minval",  0,     BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
201 	{ "maxval",  1023,  BOND_VALFLAG_MAX},
202 	{ NULL,      -1,    0},
203 };
204 
205 static const struct bond_option bond_opts[BOND_OPT_LAST] = {
206 	[BOND_OPT_MODE] = {
207 		.id = BOND_OPT_MODE,
208 		.name = "mode",
209 		.desc = "bond device mode",
210 		.flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
211 		.values = bond_mode_tbl,
212 		.set = bond_option_mode_set
213 	},
214 	[BOND_OPT_PACKETS_PER_SLAVE] = {
215 		.id = BOND_OPT_PACKETS_PER_SLAVE,
216 		.name = "packets_per_slave",
217 		.desc = "Packets to send per slave in RR mode",
218 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
219 		.values = bond_pps_tbl,
220 		.set = bond_option_pps_set
221 	},
222 	[BOND_OPT_XMIT_HASH] = {
223 		.id = BOND_OPT_XMIT_HASH,
224 		.name = "xmit_hash_policy",
225 		.desc = "balance-xor, 802.3ad, and tlb hashing method",
226 		.values = bond_xmit_hashtype_tbl,
227 		.set = bond_option_xmit_hash_policy_set
228 	},
229 	[BOND_OPT_ARP_VALIDATE] = {
230 		.id = BOND_OPT_ARP_VALIDATE,
231 		.name = "arp_validate",
232 		.desc = "validate src/dst of ARP probes",
233 		.unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
234 			       BIT(BOND_MODE_ALB),
235 		.values = bond_arp_validate_tbl,
236 		.set = bond_option_arp_validate_set
237 	},
238 	[BOND_OPT_ARP_ALL_TARGETS] = {
239 		.id = BOND_OPT_ARP_ALL_TARGETS,
240 		.name = "arp_all_targets",
241 		.desc = "fail on any/all arp targets timeout",
242 		.values = bond_arp_all_targets_tbl,
243 		.set = bond_option_arp_all_targets_set
244 	},
245 	[BOND_OPT_FAIL_OVER_MAC] = {
246 		.id = BOND_OPT_FAIL_OVER_MAC,
247 		.name = "fail_over_mac",
248 		.desc = "For active-backup, do not set all slaves to the same MAC",
249 		.flags = BOND_OPTFLAG_NOSLAVES,
250 		.values = bond_fail_over_mac_tbl,
251 		.set = bond_option_fail_over_mac_set
252 	},
253 	[BOND_OPT_ARP_INTERVAL] = {
254 		.id = BOND_OPT_ARP_INTERVAL,
255 		.name = "arp_interval",
256 		.desc = "arp interval in milliseconds",
257 		.unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
258 			       BIT(BOND_MODE_ALB),
259 		.values = bond_intmax_tbl,
260 		.set = bond_option_arp_interval_set
261 	},
262 	[BOND_OPT_ARP_TARGETS] = {
263 		.id = BOND_OPT_ARP_TARGETS,
264 		.name = "arp_ip_target",
265 		.desc = "arp targets in n.n.n.n form",
266 		.flags = BOND_OPTFLAG_RAWVAL,
267 		.set = bond_option_arp_ip_targets_set
268 	},
269 	[BOND_OPT_DOWNDELAY] = {
270 		.id = BOND_OPT_DOWNDELAY,
271 		.name = "downdelay",
272 		.desc = "Delay before considering link down, in milliseconds",
273 		.values = bond_intmax_tbl,
274 		.set = bond_option_downdelay_set
275 	},
276 	[BOND_OPT_UPDELAY] = {
277 		.id = BOND_OPT_UPDELAY,
278 		.name = "updelay",
279 		.desc = "Delay before considering link up, in milliseconds",
280 		.values = bond_intmax_tbl,
281 		.set = bond_option_updelay_set
282 	},
283 	[BOND_OPT_LACP_RATE] = {
284 		.id = BOND_OPT_LACP_RATE,
285 		.name = "lacp_rate",
286 		.desc = "LACPDU tx rate to request from 802.3ad partner",
287 		.flags = BOND_OPTFLAG_IFDOWN,
288 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
289 		.values = bond_lacp_rate_tbl,
290 		.set = bond_option_lacp_rate_set
291 	},
292 	[BOND_OPT_MINLINKS] = {
293 		.id = BOND_OPT_MINLINKS,
294 		.name = "min_links",
295 		.desc = "Minimum number of available links before turning on carrier",
296 		.values = bond_intmax_tbl,
297 		.set = bond_option_min_links_set
298 	},
299 	[BOND_OPT_AD_SELECT] = {
300 		.id = BOND_OPT_AD_SELECT,
301 		.name = "ad_select",
302 		.desc = "803.ad aggregation selection logic",
303 		.flags = BOND_OPTFLAG_IFDOWN,
304 		.values = bond_ad_select_tbl,
305 		.set = bond_option_ad_select_set
306 	},
307 	[BOND_OPT_NUM_PEER_NOTIF] = {
308 		.id = BOND_OPT_NUM_PEER_NOTIF,
309 		.name = "num_unsol_na",
310 		.desc = "Number of peer notifications to send on failover event",
311 		.values = bond_num_peer_notif_tbl,
312 		.set = bond_option_num_peer_notif_set
313 	},
314 	[BOND_OPT_MIIMON] = {
315 		.id = BOND_OPT_MIIMON,
316 		.name = "miimon",
317 		.desc = "Link check interval in milliseconds",
318 		.values = bond_intmax_tbl,
319 		.set = bond_option_miimon_set
320 	},
321 	[BOND_OPT_PRIMARY] = {
322 		.id = BOND_OPT_PRIMARY,
323 		.name = "primary",
324 		.desc = "Primary network device to use",
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_primary_set
330 	},
331 	[BOND_OPT_PRIMARY_RESELECT] = {
332 		.id = BOND_OPT_PRIMARY_RESELECT,
333 		.name = "primary_reselect",
334 		.desc = "Reselect primary slave once it comes up",
335 		.values = bond_primary_reselect_tbl,
336 		.set = bond_option_primary_reselect_set
337 	},
338 	[BOND_OPT_USE_CARRIER] = {
339 		.id = BOND_OPT_USE_CARRIER,
340 		.name = "use_carrier",
341 		.desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
342 		.values = bond_use_carrier_tbl,
343 		.set = bond_option_use_carrier_set
344 	},
345 	[BOND_OPT_ACTIVE_SLAVE] = {
346 		.id = BOND_OPT_ACTIVE_SLAVE,
347 		.name = "active_slave",
348 		.desc = "Currently active slave",
349 		.flags = BOND_OPTFLAG_RAWVAL,
350 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
351 						BIT(BOND_MODE_TLB) |
352 						BIT(BOND_MODE_ALB)),
353 		.set = bond_option_active_slave_set
354 	},
355 	[BOND_OPT_QUEUE_ID] = {
356 		.id = BOND_OPT_QUEUE_ID,
357 		.name = "queue_id",
358 		.desc = "Set queue id of a slave",
359 		.flags = BOND_OPTFLAG_RAWVAL,
360 		.set = bond_option_queue_id_set
361 	},
362 	[BOND_OPT_ALL_SLAVES_ACTIVE] = {
363 		.id = BOND_OPT_ALL_SLAVES_ACTIVE,
364 		.name = "all_slaves_active",
365 		.desc = "Keep all frames received on an interface by setting active flag for all slaves",
366 		.values = bond_all_slaves_active_tbl,
367 		.set = bond_option_all_slaves_active_set
368 	},
369 	[BOND_OPT_RESEND_IGMP] = {
370 		.id = BOND_OPT_RESEND_IGMP,
371 		.name = "resend_igmp",
372 		.desc = "Number of IGMP membership reports to send on link failure",
373 		.values = bond_resend_igmp_tbl,
374 		.set = bond_option_resend_igmp_set
375 	},
376 	[BOND_OPT_LP_INTERVAL] = {
377 		.id = BOND_OPT_LP_INTERVAL,
378 		.name = "lp_interval",
379 		.desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
380 		.values = bond_lp_interval_tbl,
381 		.set = bond_option_lp_interval_set
382 	},
383 	[BOND_OPT_SLAVES] = {
384 		.id = BOND_OPT_SLAVES,
385 		.name = "slaves",
386 		.desc = "Slave membership management",
387 		.flags = BOND_OPTFLAG_RAWVAL,
388 		.set = bond_option_slaves_set
389 	},
390 	[BOND_OPT_TLB_DYNAMIC_LB] = {
391 		.id = BOND_OPT_TLB_DYNAMIC_LB,
392 		.name = "tlb_dynamic_lb",
393 		.desc = "Enable dynamic flow shuffling",
394 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB) | BIT(BOND_MODE_ALB)),
395 		.values = bond_tlb_dynamic_lb_tbl,
396 		.flags = BOND_OPTFLAG_IFDOWN,
397 		.set = bond_option_tlb_dynamic_lb_set,
398 	},
399 	[BOND_OPT_AD_ACTOR_SYS_PRIO] = {
400 		.id = BOND_OPT_AD_ACTOR_SYS_PRIO,
401 		.name = "ad_actor_sys_prio",
402 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
403 		.values = bond_ad_actor_sys_prio_tbl,
404 		.set = bond_option_ad_actor_sys_prio_set,
405 	},
406 	[BOND_OPT_AD_ACTOR_SYSTEM] = {
407 		.id = BOND_OPT_AD_ACTOR_SYSTEM,
408 		.name = "ad_actor_system",
409 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
410 		.flags = BOND_OPTFLAG_RAWVAL,
411 		.set = bond_option_ad_actor_system_set,
412 	},
413 	[BOND_OPT_AD_USER_PORT_KEY] = {
414 		.id = BOND_OPT_AD_USER_PORT_KEY,
415 		.name = "ad_user_port_key",
416 		.unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
417 		.flags = BOND_OPTFLAG_IFDOWN,
418 		.values = bond_ad_user_port_key_tbl,
419 		.set = bond_option_ad_user_port_key_set,
420 	},
421 	[BOND_OPT_NUM_PEER_NOTIF_ALIAS] = {
422 		.id = BOND_OPT_NUM_PEER_NOTIF_ALIAS,
423 		.name = "num_grat_arp",
424 		.desc = "Number of peer notifications to send on failover event",
425 		.values = bond_num_peer_notif_tbl,
426 		.set = bond_option_num_peer_notif_set
427 	}
428 };
429 
430 /* Searches for an option by name */
431 const struct bond_option *bond_opt_get_by_name(const char *name)
432 {
433 	const struct bond_option *opt;
434 	int option;
435 
436 	for (option = 0; option < BOND_OPT_LAST; option++) {
437 		opt = bond_opt_get(option);
438 		if (opt && !strcmp(opt->name, name))
439 			return opt;
440 	}
441 
442 	return NULL;
443 }
444 
445 /* Searches for a value in opt's values[] table */
446 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
447 {
448 	const struct bond_option *opt;
449 	int i;
450 
451 	opt = bond_opt_get(option);
452 	if (WARN_ON(!opt))
453 		return NULL;
454 	for (i = 0; opt->values && opt->values[i].string; i++)
455 		if (opt->values[i].value == val)
456 			return &opt->values[i];
457 
458 	return NULL;
459 }
460 
461 /* Searches for a value in opt's values[] table which matches the flagmask */
462 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
463 						       u32 flagmask)
464 {
465 	int i;
466 
467 	for (i = 0; opt->values && opt->values[i].string; i++)
468 		if (opt->values[i].flags & flagmask)
469 			return &opt->values[i];
470 
471 	return NULL;
472 }
473 
474 /* If maxval is missing then there's no range to check. In case minval is
475  * missing then it's considered to be 0.
476  */
477 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
478 {
479 	const struct bond_opt_value *minval, *maxval;
480 
481 	minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
482 	maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
483 	if (!maxval || (minval && val < minval->value) || val > maxval->value)
484 		return false;
485 
486 	return true;
487 }
488 
489 /**
490  * bond_opt_parse - parse option value
491  * @opt: the option to parse against
492  * @val: value to parse
493  *
494  * This function tries to extract the value from @val and check if it's
495  * a possible match for the option and returns NULL if a match isn't found,
496  * or the struct_opt_value that matched. It also strips the new line from
497  * @val->string if it's present.
498  */
499 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
500 					    struct bond_opt_value *val)
501 {
502 	char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
503 	const struct bond_opt_value *tbl;
504 	const struct bond_opt_value *ret = NULL;
505 	bool checkval;
506 	int i, rv;
507 
508 	/* No parsing if the option wants a raw val */
509 	if (opt->flags & BOND_OPTFLAG_RAWVAL)
510 		return val;
511 
512 	tbl = opt->values;
513 	if (!tbl)
514 		goto out;
515 
516 	/* ULLONG_MAX is used to bypass string processing */
517 	checkval = val->value != ULLONG_MAX;
518 	if (!checkval) {
519 		if (!val->string)
520 			goto out;
521 		p = strchr(val->string, '\n');
522 		if (p)
523 			*p = '\0';
524 		for (p = val->string; *p; p++)
525 			if (!(isdigit(*p) || isspace(*p)))
526 				break;
527 		/* The following code extracts the string to match or the value
528 		 * and sets checkval appropriately
529 		 */
530 		if (*p) {
531 			rv = sscanf(val->string, "%32s", valstr);
532 		} else {
533 			rv = sscanf(val->string, "%llu", &val->value);
534 			checkval = true;
535 		}
536 		if (!rv)
537 			goto out;
538 	}
539 
540 	for (i = 0; tbl[i].string; i++) {
541 		/* Check for exact match */
542 		if (checkval) {
543 			if (val->value == tbl[i].value)
544 				ret = &tbl[i];
545 		} else {
546 			if (!strcmp(valstr, "default") &&
547 			    (tbl[i].flags & BOND_VALFLAG_DEFAULT))
548 				ret = &tbl[i];
549 
550 			if (!strcmp(valstr, tbl[i].string))
551 				ret = &tbl[i];
552 		}
553 		/* Found an exact match */
554 		if (ret)
555 			goto out;
556 	}
557 	/* Possible range match */
558 	if (checkval && bond_opt_check_range(opt, val->value))
559 		ret = val;
560 out:
561 	return ret;
562 }
563 
564 /* Check opt's dependencies against bond mode and currently set options */
565 static int bond_opt_check_deps(struct bonding *bond,
566 			       const struct bond_option *opt)
567 {
568 	struct bond_params *params = &bond->params;
569 
570 	if (test_bit(params->mode, &opt->unsuppmodes))
571 		return -EACCES;
572 	if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
573 		return -ENOTEMPTY;
574 	if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
575 		return -EBUSY;
576 
577 	return 0;
578 }
579 
580 static void bond_opt_dep_print(struct bonding *bond,
581 			       const struct bond_option *opt)
582 {
583 	const struct bond_opt_value *modeval;
584 	struct bond_params *params;
585 
586 	params = &bond->params;
587 	modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
588 	if (test_bit(params->mode, &opt->unsuppmodes))
589 		netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
590 			   opt->name, modeval->string, modeval->value);
591 }
592 
593 static void bond_opt_error_interpret(struct bonding *bond,
594 				     const struct bond_option *opt,
595 				     int error, const struct bond_opt_value *val)
596 {
597 	const struct bond_opt_value *minval, *maxval;
598 	char *p;
599 
600 	switch (error) {
601 	case -EINVAL:
602 		if (val) {
603 			if (val->string) {
604 				/* sometimes RAWVAL opts may have new lines */
605 				p = strchr(val->string, '\n');
606 				if (p)
607 					*p = '\0';
608 				netdev_err(bond->dev, "option %s: invalid value (%s)\n",
609 					   opt->name, val->string);
610 			} else {
611 				netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
612 					   opt->name, val->value);
613 			}
614 		}
615 		minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
616 		maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
617 		if (!maxval)
618 			break;
619 		netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
620 			   opt->name, minval ? minval->value : 0, maxval->value);
621 		break;
622 	case -EACCES:
623 		bond_opt_dep_print(bond, opt);
624 		break;
625 	case -ENOTEMPTY:
626 		netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
627 			   opt->name);
628 		break;
629 	case -EBUSY:
630 		netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
631 			   opt->name);
632 		break;
633 	default:
634 		break;
635 	}
636 }
637 
638 /**
639  * __bond_opt_set - set a bonding option
640  * @bond: target bond device
641  * @option: option to set
642  * @val: value to set it to
643  *
644  * This function is used to change the bond's option value, it can be
645  * used for both enabling/changing an option and for disabling it. RTNL lock
646  * must be obtained before calling this function.
647  */
648 int __bond_opt_set(struct bonding *bond,
649 		   unsigned int option, struct bond_opt_value *val)
650 {
651 	const struct bond_opt_value *retval = NULL;
652 	const struct bond_option *opt;
653 	int ret = -ENOENT;
654 
655 	ASSERT_RTNL();
656 
657 	opt = bond_opt_get(option);
658 	if (WARN_ON(!val) || WARN_ON(!opt))
659 		goto out;
660 	ret = bond_opt_check_deps(bond, opt);
661 	if (ret)
662 		goto out;
663 	retval = bond_opt_parse(opt, val);
664 	if (!retval) {
665 		ret = -EINVAL;
666 		goto out;
667 	}
668 	ret = opt->set(bond, retval);
669 out:
670 	if (ret)
671 		bond_opt_error_interpret(bond, opt, ret, val);
672 
673 	return ret;
674 }
675 /**
676  * __bond_opt_set_notify - set a bonding option
677  * @bond: target bond device
678  * @option: option to set
679  * @val: value to set it to
680  *
681  * This function is used to change the bond's option value and trigger
682  * a notification to user sapce. It can be used for both enabling/changing
683  * an option and for disabling it. RTNL lock must be obtained before calling
684  * this function.
685  */
686 int __bond_opt_set_notify(struct bonding *bond,
687 			  unsigned int option, struct bond_opt_value *val)
688 {
689 	int ret = -ENOENT;
690 
691 	ASSERT_RTNL();
692 
693 	ret = __bond_opt_set(bond, option, val);
694 
695 	if (!ret && (bond->dev->reg_state == NETREG_REGISTERED))
696 		call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
697 
698 	return ret;
699 }
700 
701 /**
702  * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
703  * @bond: target bond device
704  * @option: option to set
705  * @buf: value to set it to
706  *
707  * This function tries to acquire RTNL without blocking and if successful
708  * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
709  */
710 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
711 {
712 	struct bond_opt_value optval;
713 	int ret;
714 
715 	if (!rtnl_trylock())
716 		return restart_syscall();
717 	bond_opt_initstr(&optval, buf);
718 	ret = __bond_opt_set_notify(bond, option, &optval);
719 	rtnl_unlock();
720 
721 	return ret;
722 }
723 
724 /**
725  * bond_opt_get - get a pointer to an option
726  * @option: option for which to return a pointer
727  *
728  * This function checks if option is valid and if so returns a pointer
729  * to its entry in the bond_opts[] option array.
730  */
731 const struct bond_option *bond_opt_get(unsigned int option)
732 {
733 	if (!BOND_OPT_VALID(option))
734 		return NULL;
735 
736 	return &bond_opts[option];
737 }
738 
739 static int bond_option_mode_set(struct bonding *bond,
740 				const struct bond_opt_value *newval)
741 {
742 	if (!bond_mode_uses_arp(newval->value)) {
743 		if (bond->params.arp_interval) {
744 			netdev_dbg(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
745 				   newval->string);
746 			/* disable arp monitoring */
747 			bond->params.arp_interval = 0;
748 		}
749 
750 		if (!bond->params.miimon) {
751 			/* set miimon to default value */
752 			bond->params.miimon = BOND_DEFAULT_MIIMON;
753 			netdev_dbg(bond->dev, "Setting MII monitoring interval to %d\n",
754 				   bond->params.miimon);
755 		}
756 	}
757 
758 	if (newval->value == BOND_MODE_ALB)
759 		bond->params.tlb_dynamic_lb = 1;
760 
761 	/* don't cache arp_validate between modes */
762 	bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
763 	bond->params.mode = newval->value;
764 
765 	return 0;
766 }
767 
768 static int bond_option_active_slave_set(struct bonding *bond,
769 					const struct bond_opt_value *newval)
770 {
771 	char ifname[IFNAMSIZ] = { 0, };
772 	struct net_device *slave_dev;
773 	int ret = 0;
774 
775 	sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
776 	if (!strlen(ifname) || newval->string[0] == '\n') {
777 		slave_dev = NULL;
778 	} else {
779 		slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
780 		if (!slave_dev)
781 			return -ENODEV;
782 	}
783 
784 	if (slave_dev) {
785 		if (!netif_is_bond_slave(slave_dev)) {
786 			slave_err(bond->dev, slave_dev, "Device is not bonding slave\n");
787 			return -EINVAL;
788 		}
789 
790 		if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
791 			slave_err(bond->dev, slave_dev, "Device is not our slave\n");
792 			return -EINVAL;
793 		}
794 	}
795 
796 	block_netpoll_tx();
797 	/* check to see if we are clearing active */
798 	if (!slave_dev) {
799 		netdev_dbg(bond->dev, "Clearing current active slave\n");
800 		RCU_INIT_POINTER(bond->curr_active_slave, NULL);
801 		bond_select_active_slave(bond);
802 	} else {
803 		struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
804 		struct slave *new_active = bond_slave_get_rtnl(slave_dev);
805 
806 		BUG_ON(!new_active);
807 
808 		if (new_active == old_active) {
809 			/* do nothing */
810 			slave_dbg(bond->dev, new_active->dev, "is already the current active slave\n");
811 		} else {
812 			if (old_active && (new_active->link == BOND_LINK_UP) &&
813 			    bond_slave_is_up(new_active)) {
814 				slave_dbg(bond->dev, new_active->dev, "Setting as active slave\n");
815 				bond_change_active_slave(bond, new_active);
816 			} else {
817 				slave_err(bond->dev, new_active->dev, "Could not set as active slave; either %s is down or the link is down\n",
818 					  new_active->dev->name);
819 				ret = -EINVAL;
820 			}
821 		}
822 	}
823 	unblock_netpoll_tx();
824 
825 	return ret;
826 }
827 
828 /* There are two tricky bits here.  First, if MII monitoring is activated, then
829  * we must disable ARP monitoring.  Second, if the timer isn't running, we must
830  * start it.
831  */
832 static int bond_option_miimon_set(struct bonding *bond,
833 				  const struct bond_opt_value *newval)
834 {
835 	netdev_dbg(bond->dev, "Setting MII monitoring interval to %llu\n",
836 		   newval->value);
837 	bond->params.miimon = newval->value;
838 	if (bond->params.updelay)
839 		netdev_dbg(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
840 			   bond->params.updelay * bond->params.miimon);
841 	if (bond->params.downdelay)
842 		netdev_dbg(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
843 			   bond->params.downdelay * bond->params.miimon);
844 	if (newval->value && bond->params.arp_interval) {
845 		netdev_dbg(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
846 		bond->params.arp_interval = 0;
847 		if (bond->params.arp_validate)
848 			bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
849 	}
850 	if (bond->dev->flags & IFF_UP) {
851 		/* If the interface is up, we may need to fire off
852 		 * the MII timer. If the interface is down, the
853 		 * timer will get fired off when the open function
854 		 * is called.
855 		 */
856 		if (!newval->value) {
857 			cancel_delayed_work_sync(&bond->mii_work);
858 		} else {
859 			cancel_delayed_work_sync(&bond->arp_work);
860 			queue_delayed_work(bond->wq, &bond->mii_work, 0);
861 		}
862 	}
863 
864 	return 0;
865 }
866 
867 /* Set up and down delays. These must be multiples of the
868  * MII monitoring value, and are stored internally as the multiplier.
869  * Thus, we must translate to MS for the real world.
870  */
871 static int bond_option_updelay_set(struct bonding *bond,
872 				   const struct bond_opt_value *newval)
873 {
874 	int value = newval->value;
875 
876 	if (!bond->params.miimon) {
877 		netdev_err(bond->dev, "Unable to set up delay as MII monitoring is disabled\n");
878 		return -EPERM;
879 	}
880 	if ((value % bond->params.miimon) != 0) {
881 		netdev_warn(bond->dev, "up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
882 			    value, bond->params.miimon,
883 			    (value / bond->params.miimon) *
884 			    bond->params.miimon);
885 	}
886 	bond->params.updelay = value / bond->params.miimon;
887 	netdev_dbg(bond->dev, "Setting up delay to %d\n",
888 		   bond->params.updelay * bond->params.miimon);
889 
890 	return 0;
891 }
892 
893 static int bond_option_downdelay_set(struct bonding *bond,
894 				     const struct bond_opt_value *newval)
895 {
896 	int value = newval->value;
897 
898 	if (!bond->params.miimon) {
899 		netdev_err(bond->dev, "Unable to set down delay as MII monitoring is disabled\n");
900 		return -EPERM;
901 	}
902 	if ((value % bond->params.miimon) != 0) {
903 		netdev_warn(bond->dev, "down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
904 			    value, bond->params.miimon,
905 			    (value / bond->params.miimon) *
906 			    bond->params.miimon);
907 	}
908 	bond->params.downdelay = value / bond->params.miimon;
909 	netdev_dbg(bond->dev, "Setting down delay to %d\n",
910 		   bond->params.downdelay * bond->params.miimon);
911 
912 	return 0;
913 }
914 
915 static int bond_option_use_carrier_set(struct bonding *bond,
916 				       const struct bond_opt_value *newval)
917 {
918 	netdev_dbg(bond->dev, "Setting use_carrier to %llu\n",
919 		   newval->value);
920 	bond->params.use_carrier = newval->value;
921 
922 	return 0;
923 }
924 
925 /* There are two tricky bits here.  First, if ARP monitoring is activated, then
926  * we must disable MII monitoring.  Second, if the ARP timer isn't running,
927  * we must start it.
928  */
929 static int bond_option_arp_interval_set(struct bonding *bond,
930 					const struct bond_opt_value *newval)
931 {
932 	netdev_dbg(bond->dev, "Setting ARP monitoring interval to %llu\n",
933 		   newval->value);
934 	bond->params.arp_interval = newval->value;
935 	if (newval->value) {
936 		if (bond->params.miimon) {
937 			netdev_dbg(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
938 			bond->params.miimon = 0;
939 		}
940 		if (!bond->params.arp_targets[0])
941 			netdev_dbg(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
942 	}
943 	if (bond->dev->flags & IFF_UP) {
944 		/* If the interface is up, we may need to fire off
945 		 * the ARP timer.  If the interface is down, the
946 		 * timer will get fired off when the open function
947 		 * is called.
948 		 */
949 		if (!newval->value) {
950 			if (bond->params.arp_validate)
951 				bond->recv_probe = NULL;
952 			cancel_delayed_work_sync(&bond->arp_work);
953 		} else {
954 			/* arp_validate can be set only in active-backup mode */
955 			bond->recv_probe = bond_arp_rcv;
956 			cancel_delayed_work_sync(&bond->mii_work);
957 			queue_delayed_work(bond->wq, &bond->arp_work, 0);
958 		}
959 	}
960 
961 	return 0;
962 }
963 
964 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
965 					    __be32 target,
966 					    unsigned long last_rx)
967 {
968 	__be32 *targets = bond->params.arp_targets;
969 	struct list_head *iter;
970 	struct slave *slave;
971 
972 	if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
973 		bond_for_each_slave(bond, slave, iter)
974 			slave->target_last_arp_rx[slot] = last_rx;
975 		targets[slot] = target;
976 	}
977 }
978 
979 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
980 {
981 	__be32 *targets = bond->params.arp_targets;
982 	int ind;
983 
984 	if (!bond_is_ip_target_ok(target)) {
985 		netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
986 			   &target);
987 		return -EINVAL;
988 	}
989 
990 	if (bond_get_targets_ip(targets, target) != -1) { /* dup */
991 		netdev_err(bond->dev, "ARP target %pI4 is already present\n",
992 			   &target);
993 		return -EINVAL;
994 	}
995 
996 	ind = bond_get_targets_ip(targets, 0); /* first free slot */
997 	if (ind == -1) {
998 		netdev_err(bond->dev, "ARP target table is full!\n");
999 		return -EINVAL;
1000 	}
1001 
1002 	netdev_dbg(bond->dev, "Adding ARP target %pI4\n", &target);
1003 
1004 	_bond_options_arp_ip_target_set(bond, ind, target, jiffies);
1005 
1006 	return 0;
1007 }
1008 
1009 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
1010 {
1011 	return _bond_option_arp_ip_target_add(bond, target);
1012 }
1013 
1014 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
1015 {
1016 	__be32 *targets = bond->params.arp_targets;
1017 	struct list_head *iter;
1018 	struct slave *slave;
1019 	unsigned long *targets_rx;
1020 	int ind, i;
1021 
1022 	if (!bond_is_ip_target_ok(target)) {
1023 		netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
1024 			   &target);
1025 		return -EINVAL;
1026 	}
1027 
1028 	ind = bond_get_targets_ip(targets, target);
1029 	if (ind == -1) {
1030 		netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
1031 			   &target);
1032 		return -EINVAL;
1033 	}
1034 
1035 	if (ind == 0 && !targets[1] && bond->params.arp_interval)
1036 		netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
1037 
1038 	netdev_dbg(bond->dev, "Removing ARP target %pI4\n", &target);
1039 
1040 	bond_for_each_slave(bond, slave, iter) {
1041 		targets_rx = slave->target_last_arp_rx;
1042 		for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1043 			targets_rx[i] = targets_rx[i+1];
1044 		targets_rx[i] = 0;
1045 	}
1046 	for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1047 		targets[i] = targets[i+1];
1048 	targets[i] = 0;
1049 
1050 	return 0;
1051 }
1052 
1053 void bond_option_arp_ip_targets_clear(struct bonding *bond)
1054 {
1055 	int i;
1056 
1057 	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1058 		_bond_options_arp_ip_target_set(bond, i, 0, 0);
1059 }
1060 
1061 static int bond_option_arp_ip_targets_set(struct bonding *bond,
1062 					  const struct bond_opt_value *newval)
1063 {
1064 	int ret = -EPERM;
1065 	__be32 target;
1066 
1067 	if (newval->string) {
1068 		if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1069 			netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
1070 				   &target);
1071 			return ret;
1072 		}
1073 		if (newval->string[0] == '+')
1074 			ret = bond_option_arp_ip_target_add(bond, target);
1075 		else if (newval->string[0] == '-')
1076 			ret = bond_option_arp_ip_target_rem(bond, target);
1077 		else
1078 			netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
1079 	} else {
1080 		target = newval->value;
1081 		ret = bond_option_arp_ip_target_add(bond, target);
1082 	}
1083 
1084 	return ret;
1085 }
1086 
1087 static int bond_option_arp_validate_set(struct bonding *bond,
1088 					const struct bond_opt_value *newval)
1089 {
1090 	netdev_dbg(bond->dev, "Setting arp_validate to %s (%llu)\n",
1091 		   newval->string, newval->value);
1092 	bond->params.arp_validate = newval->value;
1093 
1094 	return 0;
1095 }
1096 
1097 static int bond_option_arp_all_targets_set(struct bonding *bond,
1098 					   const struct bond_opt_value *newval)
1099 {
1100 	netdev_dbg(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
1101 		   newval->string, newval->value);
1102 	bond->params.arp_all_targets = newval->value;
1103 
1104 	return 0;
1105 }
1106 
1107 static int bond_option_primary_set(struct bonding *bond,
1108 				   const struct bond_opt_value *newval)
1109 {
1110 	char *p, *primary = newval->string;
1111 	struct list_head *iter;
1112 	struct slave *slave;
1113 
1114 	block_netpoll_tx();
1115 
1116 	p = strchr(primary, '\n');
1117 	if (p)
1118 		*p = '\0';
1119 	/* check to see if we are clearing primary */
1120 	if (!strlen(primary)) {
1121 		netdev_dbg(bond->dev, "Setting primary slave to None\n");
1122 		RCU_INIT_POINTER(bond->primary_slave, NULL);
1123 		memset(bond->params.primary, 0, sizeof(bond->params.primary));
1124 		bond_select_active_slave(bond);
1125 		goto out;
1126 	}
1127 
1128 	bond_for_each_slave(bond, slave, iter) {
1129 		if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1130 			slave_dbg(bond->dev, slave->dev, "Setting as primary slave\n");
1131 			rcu_assign_pointer(bond->primary_slave, slave);
1132 			strcpy(bond->params.primary, slave->dev->name);
1133 			bond->force_primary = true;
1134 			bond_select_active_slave(bond);
1135 			goto out;
1136 		}
1137 	}
1138 
1139 	if (rtnl_dereference(bond->primary_slave)) {
1140 		netdev_dbg(bond->dev, "Setting primary slave to None\n");
1141 		RCU_INIT_POINTER(bond->primary_slave, NULL);
1142 		bond_select_active_slave(bond);
1143 	}
1144 	strncpy(bond->params.primary, primary, IFNAMSIZ);
1145 	bond->params.primary[IFNAMSIZ - 1] = 0;
1146 
1147 	netdev_dbg(bond->dev, "Recording %s as primary, but it has not been enslaved yet\n",
1148 		   primary);
1149 
1150 out:
1151 	unblock_netpoll_tx();
1152 
1153 	return 0;
1154 }
1155 
1156 static int bond_option_primary_reselect_set(struct bonding *bond,
1157 					    const struct bond_opt_value *newval)
1158 {
1159 	netdev_dbg(bond->dev, "Setting primary_reselect to %s (%llu)\n",
1160 		   newval->string, newval->value);
1161 	bond->params.primary_reselect = newval->value;
1162 
1163 	block_netpoll_tx();
1164 	bond_select_active_slave(bond);
1165 	unblock_netpoll_tx();
1166 
1167 	return 0;
1168 }
1169 
1170 static int bond_option_fail_over_mac_set(struct bonding *bond,
1171 					 const struct bond_opt_value *newval)
1172 {
1173 	netdev_dbg(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
1174 		   newval->string, newval->value);
1175 	bond->params.fail_over_mac = newval->value;
1176 
1177 	return 0;
1178 }
1179 
1180 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1181 					    const struct bond_opt_value *newval)
1182 {
1183 	netdev_dbg(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
1184 		   newval->string, newval->value);
1185 	bond->params.xmit_policy = newval->value;
1186 
1187 	return 0;
1188 }
1189 
1190 static int bond_option_resend_igmp_set(struct bonding *bond,
1191 				       const struct bond_opt_value *newval)
1192 {
1193 	netdev_dbg(bond->dev, "Setting resend_igmp to %llu\n",
1194 		   newval->value);
1195 	bond->params.resend_igmp = newval->value;
1196 
1197 	return 0;
1198 }
1199 
1200 static int bond_option_num_peer_notif_set(struct bonding *bond,
1201 				   const struct bond_opt_value *newval)
1202 {
1203 	bond->params.num_peer_notif = newval->value;
1204 
1205 	return 0;
1206 }
1207 
1208 static int bond_option_all_slaves_active_set(struct bonding *bond,
1209 					     const struct bond_opt_value *newval)
1210 {
1211 	struct list_head *iter;
1212 	struct slave *slave;
1213 
1214 	if (newval->value == bond->params.all_slaves_active)
1215 		return 0;
1216 	bond->params.all_slaves_active = newval->value;
1217 	bond_for_each_slave(bond, slave, iter) {
1218 		if (!bond_is_active_slave(slave)) {
1219 			if (newval->value)
1220 				slave->inactive = 0;
1221 			else
1222 				slave->inactive = 1;
1223 		}
1224 	}
1225 
1226 	return 0;
1227 }
1228 
1229 static int bond_option_min_links_set(struct bonding *bond,
1230 				     const struct bond_opt_value *newval)
1231 {
1232 	netdev_dbg(bond->dev, "Setting min links value to %llu\n",
1233 		   newval->value);
1234 	bond->params.min_links = newval->value;
1235 	bond_set_carrier(bond);
1236 
1237 	return 0;
1238 }
1239 
1240 static int bond_option_lp_interval_set(struct bonding *bond,
1241 				       const struct bond_opt_value *newval)
1242 {
1243 	bond->params.lp_interval = newval->value;
1244 
1245 	return 0;
1246 }
1247 
1248 static int bond_option_pps_set(struct bonding *bond,
1249 			       const struct bond_opt_value *newval)
1250 {
1251 	netdev_dbg(bond->dev, "Setting packets per slave to %llu\n",
1252 		   newval->value);
1253 	bond->params.packets_per_slave = newval->value;
1254 	if (newval->value > 0) {
1255 		bond->params.reciprocal_packets_per_slave =
1256 			reciprocal_value(newval->value);
1257 	} else {
1258 		/* reciprocal_packets_per_slave is unused if
1259 		 * packets_per_slave is 0 or 1, just initialize it
1260 		 */
1261 		bond->params.reciprocal_packets_per_slave =
1262 			(struct reciprocal_value) { 0 };
1263 	}
1264 
1265 	return 0;
1266 }
1267 
1268 static int bond_option_lacp_rate_set(struct bonding *bond,
1269 				     const struct bond_opt_value *newval)
1270 {
1271 	netdev_dbg(bond->dev, "Setting LACP rate to %s (%llu)\n",
1272 		   newval->string, newval->value);
1273 	bond->params.lacp_fast = newval->value;
1274 	bond_3ad_update_lacp_rate(bond);
1275 
1276 	return 0;
1277 }
1278 
1279 static int bond_option_ad_select_set(struct bonding *bond,
1280 				     const struct bond_opt_value *newval)
1281 {
1282 	netdev_dbg(bond->dev, "Setting ad_select to %s (%llu)\n",
1283 		   newval->string, newval->value);
1284 	bond->params.ad_select = newval->value;
1285 
1286 	return 0;
1287 }
1288 
1289 static int bond_option_queue_id_set(struct bonding *bond,
1290 				    const struct bond_opt_value *newval)
1291 {
1292 	struct slave *slave, *update_slave;
1293 	struct net_device *sdev;
1294 	struct list_head *iter;
1295 	char *delim;
1296 	int ret = 0;
1297 	u16 qid;
1298 
1299 	/* delim will point to queue id if successful */
1300 	delim = strchr(newval->string, ':');
1301 	if (!delim)
1302 		goto err_no_cmd;
1303 
1304 	/* Terminate string that points to device name and bump it
1305 	 * up one, so we can read the queue id there.
1306 	 */
1307 	*delim = '\0';
1308 	if (sscanf(++delim, "%hd\n", &qid) != 1)
1309 		goto err_no_cmd;
1310 
1311 	/* Check buffer length, valid ifname and queue id */
1312 	if (!dev_valid_name(newval->string) ||
1313 	    qid > bond->dev->real_num_tx_queues)
1314 		goto err_no_cmd;
1315 
1316 	/* Get the pointer to that interface if it exists */
1317 	sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1318 	if (!sdev)
1319 		goto err_no_cmd;
1320 
1321 	/* Search for thes slave and check for duplicate qids */
1322 	update_slave = NULL;
1323 	bond_for_each_slave(bond, slave, iter) {
1324 		if (sdev == slave->dev)
1325 			/* We don't need to check the matching
1326 			 * slave for dups, since we're overwriting it
1327 			 */
1328 			update_slave = slave;
1329 		else if (qid && qid == slave->queue_id) {
1330 			goto err_no_cmd;
1331 		}
1332 	}
1333 
1334 	if (!update_slave)
1335 		goto err_no_cmd;
1336 
1337 	/* Actually set the qids for the slave */
1338 	update_slave->queue_id = qid;
1339 
1340 out:
1341 	return ret;
1342 
1343 err_no_cmd:
1344 	netdev_dbg(bond->dev, "invalid input for queue_id set\n");
1345 	ret = -EPERM;
1346 	goto out;
1347 
1348 }
1349 
1350 static int bond_option_slaves_set(struct bonding *bond,
1351 				  const struct bond_opt_value *newval)
1352 {
1353 	char command[IFNAMSIZ + 1] = { 0, };
1354 	struct net_device *dev;
1355 	char *ifname;
1356 	int ret;
1357 
1358 	sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1359 	ifname = command + 1;
1360 	if ((strlen(command) <= 1) ||
1361 	    (command[0] != '+' && command[0] != '-') ||
1362 	    !dev_valid_name(ifname))
1363 		goto err_no_cmd;
1364 
1365 	dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1366 	if (!dev) {
1367 		netdev_dbg(bond->dev, "interface %s does not exist!\n",
1368 			   ifname);
1369 		ret = -ENODEV;
1370 		goto out;
1371 	}
1372 
1373 	switch (command[0]) {
1374 	case '+':
1375 		slave_dbg(bond->dev, dev, "Enslaving interface\n");
1376 		ret = bond_enslave(bond->dev, dev, NULL);
1377 		break;
1378 
1379 	case '-':
1380 		slave_dbg(bond->dev, dev, "Releasing interface\n");
1381 		ret = bond_release(bond->dev, dev);
1382 		break;
1383 
1384 	default:
1385 		/* should not run here. */
1386 		goto err_no_cmd;
1387 	}
1388 
1389 out:
1390 	return ret;
1391 
1392 err_no_cmd:
1393 	netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
1394 	ret = -EPERM;
1395 	goto out;
1396 }
1397 
1398 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1399 					  const struct bond_opt_value *newval)
1400 {
1401 	netdev_dbg(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
1402 		   newval->string, newval->value);
1403 	bond->params.tlb_dynamic_lb = newval->value;
1404 
1405 	return 0;
1406 }
1407 
1408 static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
1409 					     const struct bond_opt_value *newval)
1410 {
1411 	netdev_dbg(bond->dev, "Setting ad_actor_sys_prio to %llu\n",
1412 		   newval->value);
1413 
1414 	bond->params.ad_actor_sys_prio = newval->value;
1415 	bond_3ad_update_ad_actor_settings(bond);
1416 
1417 	return 0;
1418 }
1419 
1420 static int bond_option_ad_actor_system_set(struct bonding *bond,
1421 					   const struct bond_opt_value *newval)
1422 {
1423 	u8 macaddr[ETH_ALEN];
1424 	u8 *mac;
1425 
1426 	if (newval->string) {
1427 		if (!mac_pton(newval->string, macaddr))
1428 			goto err;
1429 		mac = macaddr;
1430 	} else {
1431 		mac = (u8 *)&newval->value;
1432 	}
1433 
1434 	if (!is_valid_ether_addr(mac))
1435 		goto err;
1436 
1437 	netdev_dbg(bond->dev, "Setting ad_actor_system to %pM\n", mac);
1438 	ether_addr_copy(bond->params.ad_actor_system, mac);
1439 	bond_3ad_update_ad_actor_settings(bond);
1440 
1441 	return 0;
1442 
1443 err:
1444 	netdev_err(bond->dev, "Invalid ad_actor_system MAC address.\n");
1445 	return -EINVAL;
1446 }
1447 
1448 static int bond_option_ad_user_port_key_set(struct bonding *bond,
1449 					    const struct bond_opt_value *newval)
1450 {
1451 	netdev_dbg(bond->dev, "Setting ad_user_port_key to %llu\n",
1452 		   newval->value);
1453 
1454 	bond->params.ad_user_port_key = newval->value;
1455 	return 0;
1456 }
1457