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