[go: nahoru, domu]

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