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