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