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