Merge branch 'for-linus' of git://git.infradead.org/users/vkoul/slave-dma
[sfrench/cifs-2.6.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 #include <linux/errno.h>
13 #include <linux/if.h>
14 #include <linux/netdevice.h>
15 #include <linux/spinlock.h>
16 #include <linux/rcupdate.h>
17 #include <linux/ctype.h>
18 #include <linux/inet.h>
19 #include "bonding.h"
20
21 static int bond_option_active_slave_set(struct bonding *bond,
22                                         const struct bond_opt_value *newval);
23 static int bond_option_miimon_set(struct bonding *bond,
24                                   const struct bond_opt_value *newval);
25 static int bond_option_updelay_set(struct bonding *bond,
26                                    const struct bond_opt_value *newval);
27 static int bond_option_downdelay_set(struct bonding *bond,
28                                      const struct bond_opt_value *newval);
29 static int bond_option_use_carrier_set(struct bonding *bond,
30                                        const struct bond_opt_value *newval);
31 static int bond_option_arp_interval_set(struct bonding *bond,
32                                         const struct bond_opt_value *newval);
33 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
34 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
35 static int bond_option_arp_ip_targets_set(struct bonding *bond,
36                                           const struct bond_opt_value *newval);
37 static int bond_option_arp_validate_set(struct bonding *bond,
38                                         const struct bond_opt_value *newval);
39 static int bond_option_arp_all_targets_set(struct bonding *bond,
40                                            const struct bond_opt_value *newval);
41 static int bond_option_primary_set(struct bonding *bond,
42                                    const struct bond_opt_value *newval);
43 static int bond_option_primary_reselect_set(struct bonding *bond,
44                                             const struct bond_opt_value *newval);
45 static int bond_option_fail_over_mac_set(struct bonding *bond,
46                                          const struct bond_opt_value *newval);
47 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
48                                             const struct bond_opt_value *newval);
49 static int bond_option_resend_igmp_set(struct bonding *bond,
50                                        const struct bond_opt_value *newval);
51 static int bond_option_num_peer_notif_set(struct bonding *bond,
52                                           const struct bond_opt_value *newval);
53 static int bond_option_all_slaves_active_set(struct bonding *bond,
54                                              const struct bond_opt_value *newval);
55 static int bond_option_min_links_set(struct bonding *bond,
56                                      const struct bond_opt_value *newval);
57 static int bond_option_lp_interval_set(struct bonding *bond,
58                                        const struct bond_opt_value *newval);
59 static int bond_option_pps_set(struct bonding *bond,
60                                const struct bond_opt_value *newval);
61 static int bond_option_lacp_rate_set(struct bonding *bond,
62                                      const struct bond_opt_value *newval);
63 static int bond_option_ad_select_set(struct bonding *bond,
64                                      const struct bond_opt_value *newval);
65 static int bond_option_queue_id_set(struct bonding *bond,
66                                     const struct bond_opt_value *newval);
67 static int bond_option_mode_set(struct bonding *bond,
68                                 const struct bond_opt_value *newval);
69 static int bond_option_slaves_set(struct bonding *bond,
70                                   const struct bond_opt_value *newval);
71 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
72                                   const struct bond_opt_value *newval);
73
74
75 static const struct bond_opt_value bond_mode_tbl[] = {
76         { "balance-rr",    BOND_MODE_ROUNDROBIN,   BOND_VALFLAG_DEFAULT},
77         { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
78         { "balance-xor",   BOND_MODE_XOR,          0},
79         { "broadcast",     BOND_MODE_BROADCAST,    0},
80         { "802.3ad",       BOND_MODE_8023AD,       0},
81         { "balance-tlb",   BOND_MODE_TLB,          0},
82         { "balance-alb",   BOND_MODE_ALB,          0},
83         { NULL,            -1,                     0},
84 };
85
86 static const struct bond_opt_value bond_pps_tbl[] = {
87         { "default", 1,         BOND_VALFLAG_DEFAULT},
88         { "maxval",  USHRT_MAX, BOND_VALFLAG_MAX},
89         { NULL,      -1,        0},
90 };
91
92 static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
93         { "layer2",   BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
94         { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
95         { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
96         { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
97         { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
98         { NULL,       -1,                       0},
99 };
100
101 static const struct bond_opt_value bond_arp_validate_tbl[] = {
102         { "none",               BOND_ARP_VALIDATE_NONE,         BOND_VALFLAG_DEFAULT},
103         { "active",             BOND_ARP_VALIDATE_ACTIVE,       0},
104         { "backup",             BOND_ARP_VALIDATE_BACKUP,       0},
105         { "all",                BOND_ARP_VALIDATE_ALL,          0},
106         { "filter",             BOND_ARP_FILTER,                0},
107         { "filter_active",      BOND_ARP_FILTER_ACTIVE,         0},
108         { "filter_backup",      BOND_ARP_FILTER_BACKUP,         0},
109         { NULL,                 -1,                             0},
110 };
111
112 static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
113         { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
114         { "all", BOND_ARP_TARGETS_ALL, 0},
115         { NULL,  -1,                   0},
116 };
117
118 static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
119         { "none",   BOND_FOM_NONE,   BOND_VALFLAG_DEFAULT},
120         { "active", BOND_FOM_ACTIVE, 0},
121         { "follow", BOND_FOM_FOLLOW, 0},
122         { NULL,     -1,              0},
123 };
124
125 static const struct bond_opt_value bond_intmax_tbl[] = {
126         { "off",     0,       BOND_VALFLAG_DEFAULT},
127         { "maxval",  INT_MAX, BOND_VALFLAG_MAX},
128         { NULL,      -1,      0}
129 };
130
131 static const struct bond_opt_value bond_lacp_rate_tbl[] = {
132         { "slow", AD_LACP_SLOW, 0},
133         { "fast", AD_LACP_FAST, 0},
134         { NULL,   -1,           0},
135 };
136
137 static const struct bond_opt_value bond_ad_select_tbl[] = {
138         { "stable",    BOND_AD_STABLE,    BOND_VALFLAG_DEFAULT},
139         { "bandwidth", BOND_AD_BANDWIDTH, 0},
140         { "count",     BOND_AD_COUNT,     0},
141         { NULL,        -1,                0},
142 };
143
144 static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
145         { "off",     0,   0},
146         { "maxval",  255, BOND_VALFLAG_MAX},
147         { "default", 1,   BOND_VALFLAG_DEFAULT},
148         { NULL,      -1,  0}
149 };
150
151 static const struct bond_opt_value bond_primary_reselect_tbl[] = {
152         { "always",  BOND_PRI_RESELECT_ALWAYS,  BOND_VALFLAG_DEFAULT},
153         { "better",  BOND_PRI_RESELECT_BETTER,  0},
154         { "failure", BOND_PRI_RESELECT_FAILURE, 0},
155         { NULL,      -1},
156 };
157
158 static const struct bond_opt_value bond_use_carrier_tbl[] = {
159         { "off", 0,  0},
160         { "on",  1,  BOND_VALFLAG_DEFAULT},
161         { NULL,  -1, 0}
162 };
163
164 static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
165         { "off", 0,  BOND_VALFLAG_DEFAULT},
166         { "on",  1,  0},
167         { NULL,  -1, 0}
168 };
169
170 static const struct bond_opt_value bond_resend_igmp_tbl[] = {
171         { "off",     0,   0},
172         { "maxval",  255, BOND_VALFLAG_MAX},
173         { "default", 1,   BOND_VALFLAG_DEFAULT},
174         { NULL,      -1,  0}
175 };
176
177 static const struct bond_opt_value bond_lp_interval_tbl[] = {
178         { "minval",  1,       BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
179         { "maxval",  INT_MAX, BOND_VALFLAG_MAX},
180         { NULL,      -1,      0},
181 };
182
183 static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
184         { "off", 0,  0},
185         { "on",  1,  BOND_VALFLAG_DEFAULT},
186         { NULL,  -1, 0}
187 };
188
189 static const struct bond_option bond_opts[] = {
190         [BOND_OPT_MODE] = {
191                 .id = BOND_OPT_MODE,
192                 .name = "mode",
193                 .desc = "bond device mode",
194                 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
195                 .values = bond_mode_tbl,
196                 .set = bond_option_mode_set
197         },
198         [BOND_OPT_PACKETS_PER_SLAVE] = {
199                 .id = BOND_OPT_PACKETS_PER_SLAVE,
200                 .name = "packets_per_slave",
201                 .desc = "Packets to send per slave in RR mode",
202                 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
203                 .values = bond_pps_tbl,
204                 .set = bond_option_pps_set
205         },
206         [BOND_OPT_XMIT_HASH] = {
207                 .id = BOND_OPT_XMIT_HASH,
208                 .name = "xmit_hash_policy",
209                 .desc = "balance-xor, 802.3ad, and tlb hashing method",
210                 .values = bond_xmit_hashtype_tbl,
211                 .set = bond_option_xmit_hash_policy_set
212         },
213         [BOND_OPT_ARP_VALIDATE] = {
214                 .id = BOND_OPT_ARP_VALIDATE,
215                 .name = "arp_validate",
216                 .desc = "validate src/dst of ARP probes",
217                 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
218                                BIT(BOND_MODE_ALB),
219                 .values = bond_arp_validate_tbl,
220                 .set = bond_option_arp_validate_set
221         },
222         [BOND_OPT_ARP_ALL_TARGETS] = {
223                 .id = BOND_OPT_ARP_ALL_TARGETS,
224                 .name = "arp_all_targets",
225                 .desc = "fail on any/all arp targets timeout",
226                 .values = bond_arp_all_targets_tbl,
227                 .set = bond_option_arp_all_targets_set
228         },
229         [BOND_OPT_FAIL_OVER_MAC] = {
230                 .id = BOND_OPT_FAIL_OVER_MAC,
231                 .name = "fail_over_mac",
232                 .desc = "For active-backup, do not set all slaves to the same MAC",
233                 .flags = BOND_OPTFLAG_NOSLAVES,
234                 .values = bond_fail_over_mac_tbl,
235                 .set = bond_option_fail_over_mac_set
236         },
237         [BOND_OPT_ARP_INTERVAL] = {
238                 .id = BOND_OPT_ARP_INTERVAL,
239                 .name = "arp_interval",
240                 .desc = "arp interval in milliseconds",
241                 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
242                                BIT(BOND_MODE_ALB),
243                 .values = bond_intmax_tbl,
244                 .set = bond_option_arp_interval_set
245         },
246         [BOND_OPT_ARP_TARGETS] = {
247                 .id = BOND_OPT_ARP_TARGETS,
248                 .name = "arp_ip_target",
249                 .desc = "arp targets in n.n.n.n form",
250                 .flags = BOND_OPTFLAG_RAWVAL,
251                 .set = bond_option_arp_ip_targets_set
252         },
253         [BOND_OPT_DOWNDELAY] = {
254                 .id = BOND_OPT_DOWNDELAY,
255                 .name = "downdelay",
256                 .desc = "Delay before considering link down, in milliseconds",
257                 .values = bond_intmax_tbl,
258                 .set = bond_option_downdelay_set
259         },
260         [BOND_OPT_UPDELAY] = {
261                 .id = BOND_OPT_UPDELAY,
262                 .name = "updelay",
263                 .desc = "Delay before considering link up, in milliseconds",
264                 .values = bond_intmax_tbl,
265                 .set = bond_option_updelay_set
266         },
267         [BOND_OPT_LACP_RATE] = {
268                 .id = BOND_OPT_LACP_RATE,
269                 .name = "lacp_rate",
270                 .desc = "LACPDU tx rate to request from 802.3ad partner",
271                 .flags = BOND_OPTFLAG_IFDOWN,
272                 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
273                 .values = bond_lacp_rate_tbl,
274                 .set = bond_option_lacp_rate_set
275         },
276         [BOND_OPT_MINLINKS] = {
277                 .id = BOND_OPT_MINLINKS,
278                 .name = "min_links",
279                 .desc = "Minimum number of available links before turning on carrier",
280                 .values = bond_intmax_tbl,
281                 .set = bond_option_min_links_set
282         },
283         [BOND_OPT_AD_SELECT] = {
284                 .id = BOND_OPT_AD_SELECT,
285                 .name = "ad_select",
286                 .desc = "803.ad aggregation selection logic",
287                 .flags = BOND_OPTFLAG_IFDOWN,
288                 .values = bond_ad_select_tbl,
289                 .set = bond_option_ad_select_set
290         },
291         [BOND_OPT_NUM_PEER_NOTIF] = {
292                 .id = BOND_OPT_NUM_PEER_NOTIF,
293                 .name = "num_unsol_na",
294                 .desc = "Number of peer notifications to send on failover event",
295                 .values = bond_num_peer_notif_tbl,
296                 .set = bond_option_num_peer_notif_set
297         },
298         [BOND_OPT_MIIMON] = {
299                 .id = BOND_OPT_MIIMON,
300                 .name = "miimon",
301                 .desc = "Link check interval in milliseconds",
302                 .values = bond_intmax_tbl,
303                 .set = bond_option_miimon_set
304         },
305         [BOND_OPT_PRIMARY] = {
306                 .id = BOND_OPT_PRIMARY,
307                 .name = "primary",
308                 .desc = "Primary network device to use",
309                 .flags = BOND_OPTFLAG_RAWVAL,
310                 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
311                                                 BIT(BOND_MODE_TLB) |
312                                                 BIT(BOND_MODE_ALB)),
313                 .set = bond_option_primary_set
314         },
315         [BOND_OPT_PRIMARY_RESELECT] = {
316                 .id = BOND_OPT_PRIMARY_RESELECT,
317                 .name = "primary_reselect",
318                 .desc = "Reselect primary slave once it comes up",
319                 .values = bond_primary_reselect_tbl,
320                 .set = bond_option_primary_reselect_set
321         },
322         [BOND_OPT_USE_CARRIER] = {
323                 .id = BOND_OPT_USE_CARRIER,
324                 .name = "use_carrier",
325                 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
326                 .values = bond_use_carrier_tbl,
327                 .set = bond_option_use_carrier_set
328         },
329         [BOND_OPT_ACTIVE_SLAVE] = {
330                 .id = BOND_OPT_ACTIVE_SLAVE,
331                 .name = "active_slave",
332                 .desc = "Currently active slave",
333                 .flags = BOND_OPTFLAG_RAWVAL,
334                 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
335                                                 BIT(BOND_MODE_TLB) |
336                                                 BIT(BOND_MODE_ALB)),
337                 .set = bond_option_active_slave_set
338         },
339         [BOND_OPT_QUEUE_ID] = {
340                 .id = BOND_OPT_QUEUE_ID,
341                 .name = "queue_id",
342                 .desc = "Set queue id of a slave",
343                 .flags = BOND_OPTFLAG_RAWVAL,
344                 .set = bond_option_queue_id_set
345         },
346         [BOND_OPT_ALL_SLAVES_ACTIVE] = {
347                 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
348                 .name = "all_slaves_active",
349                 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
350                 .values = bond_all_slaves_active_tbl,
351                 .set = bond_option_all_slaves_active_set
352         },
353         [BOND_OPT_RESEND_IGMP] = {
354                 .id = BOND_OPT_RESEND_IGMP,
355                 .name = "resend_igmp",
356                 .desc = "Number of IGMP membership reports to send on link failure",
357                 .values = bond_resend_igmp_tbl,
358                 .set = bond_option_resend_igmp_set
359         },
360         [BOND_OPT_LP_INTERVAL] = {
361                 .id = BOND_OPT_LP_INTERVAL,
362                 .name = "lp_interval",
363                 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
364                 .values = bond_lp_interval_tbl,
365                 .set = bond_option_lp_interval_set
366         },
367         [BOND_OPT_SLAVES] = {
368                 .id = BOND_OPT_SLAVES,
369                 .name = "slaves",
370                 .desc = "Slave membership management",
371                 .flags = BOND_OPTFLAG_RAWVAL,
372                 .set = bond_option_slaves_set
373         },
374         [BOND_OPT_TLB_DYNAMIC_LB] = {
375                 .id = BOND_OPT_TLB_DYNAMIC_LB,
376                 .name = "tlb_dynamic_lb",
377                 .desc = "Enable dynamic flow shuffling",
378                 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB)),
379                 .values = bond_tlb_dynamic_lb_tbl,
380                 .flags = BOND_OPTFLAG_IFDOWN,
381                 .set = bond_option_tlb_dynamic_lb_set,
382         },
383         { }
384 };
385
386 /* Searches for an option by name */
387 const struct bond_option *bond_opt_get_by_name(const char *name)
388 {
389         const struct bond_option *opt;
390         int option;
391
392         for (option = 0; option < BOND_OPT_LAST; option++) {
393                 opt = bond_opt_get(option);
394                 if (opt && !strcmp(opt->name, name))
395                         return opt;
396         }
397
398         return NULL;
399 }
400
401 /* Searches for a value in opt's values[] table */
402 const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
403 {
404         const struct bond_option *opt;
405         int i;
406
407         opt = bond_opt_get(option);
408         if (WARN_ON(!opt))
409                 return NULL;
410         for (i = 0; opt->values && opt->values[i].string; i++)
411                 if (opt->values[i].value == val)
412                         return &opt->values[i];
413
414         return NULL;
415 }
416
417 /* Searches for a value in opt's values[] table which matches the flagmask */
418 static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
419                                                  u32 flagmask)
420 {
421         int i;
422
423         for (i = 0; opt->values && opt->values[i].string; i++)
424                 if (opt->values[i].flags & flagmask)
425                         return &opt->values[i];
426
427         return NULL;
428 }
429
430 /* If maxval is missing then there's no range to check. In case minval is
431  * missing then it's considered to be 0.
432  */
433 static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
434 {
435         const struct bond_opt_value *minval, *maxval;
436
437         minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
438         maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
439         if (!maxval || (minval && val < minval->value) || val > maxval->value)
440                 return false;
441
442         return true;
443 }
444
445 /**
446  * bond_opt_parse - parse option value
447  * @opt: the option to parse against
448  * @val: value to parse
449  *
450  * This function tries to extract the value from @val and check if it's
451  * a possible match for the option and returns NULL if a match isn't found,
452  * or the struct_opt_value that matched. It also strips the new line from
453  * @val->string if it's present.
454  */
455 const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
456                                             struct bond_opt_value *val)
457 {
458         char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
459         const struct bond_opt_value *tbl;
460         const struct bond_opt_value *ret = NULL;
461         bool checkval;
462         int i, rv;
463
464         /* No parsing if the option wants a raw val */
465         if (opt->flags & BOND_OPTFLAG_RAWVAL)
466                 return val;
467
468         tbl = opt->values;
469         if (!tbl)
470                 goto out;
471
472         /* ULLONG_MAX is used to bypass string processing */
473         checkval = val->value != ULLONG_MAX;
474         if (!checkval) {
475                 if (!val->string)
476                         goto out;
477                 p = strchr(val->string, '\n');
478                 if (p)
479                         *p = '\0';
480                 for (p = val->string; *p; p++)
481                         if (!(isdigit(*p) || isspace(*p)))
482                                 break;
483                 /* The following code extracts the string to match or the value
484                  * and sets checkval appropriately
485                  */
486                 if (*p) {
487                         rv = sscanf(val->string, "%32s", valstr);
488                 } else {
489                         rv = sscanf(val->string, "%llu", &val->value);
490                         checkval = true;
491                 }
492                 if (!rv)
493                         goto out;
494         }
495
496         for (i = 0; tbl[i].string; i++) {
497                 /* Check for exact match */
498                 if (checkval) {
499                         if (val->value == tbl[i].value)
500                                 ret = &tbl[i];
501                 } else {
502                         if (!strcmp(valstr, "default") &&
503                             (tbl[i].flags & BOND_VALFLAG_DEFAULT))
504                                 ret = &tbl[i];
505
506                         if (!strcmp(valstr, tbl[i].string))
507                                 ret = &tbl[i];
508                 }
509                 /* Found an exact match */
510                 if (ret)
511                         goto out;
512         }
513         /* Possible range match */
514         if (checkval && bond_opt_check_range(opt, val->value))
515                 ret = val;
516 out:
517         return ret;
518 }
519
520 /* Check opt's dependencies against bond mode and currently set options */
521 static int bond_opt_check_deps(struct bonding *bond,
522                                const struct bond_option *opt)
523 {
524         struct bond_params *params = &bond->params;
525
526         if (test_bit(params->mode, &opt->unsuppmodes))
527                 return -EACCES;
528         if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
529                 return -ENOTEMPTY;
530         if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
531                 return -EBUSY;
532
533         return 0;
534 }
535
536 static void bond_opt_dep_print(struct bonding *bond,
537                                const struct bond_option *opt)
538 {
539         const struct bond_opt_value *modeval;
540         struct bond_params *params;
541
542         params = &bond->params;
543         modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
544         if (test_bit(params->mode, &opt->unsuppmodes))
545                 netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
546                            opt->name, modeval->string, modeval->value);
547 }
548
549 static void bond_opt_error_interpret(struct bonding *bond,
550                                      const struct bond_option *opt,
551                                      int error, const struct bond_opt_value *val)
552 {
553         const struct bond_opt_value *minval, *maxval;
554         char *p;
555
556         switch (error) {
557         case -EINVAL:
558                 if (val) {
559                         if (val->string) {
560                                 /* sometimes RAWVAL opts may have new lines */
561                                 p = strchr(val->string, '\n');
562                                 if (p)
563                                         *p = '\0';
564                                 netdev_err(bond->dev, "option %s: invalid value (%s)\n",
565                                            opt->name, val->string);
566                         } else {
567                                 netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
568                                            opt->name, val->value);
569                         }
570                 }
571                 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
572                 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
573                 if (!maxval)
574                         break;
575                 netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
576                            opt->name, minval ? minval->value : 0, maxval->value);
577                 break;
578         case -EACCES:
579                 bond_opt_dep_print(bond, opt);
580                 break;
581         case -ENOTEMPTY:
582                 netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
583                            opt->name);
584                 break;
585         case -EBUSY:
586                 netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
587                            opt->name);
588                 break;
589         default:
590                 break;
591         }
592 }
593
594 /**
595  * __bond_opt_set - set a bonding option
596  * @bond: target bond device
597  * @option: option to set
598  * @val: value to set it to
599  *
600  * This function is used to change the bond's option value, it can be
601  * used for both enabling/changing an option and for disabling it. RTNL lock
602  * must be obtained before calling this function.
603  */
604 int __bond_opt_set(struct bonding *bond,
605                    unsigned int option, struct bond_opt_value *val)
606 {
607         const struct bond_opt_value *retval = NULL;
608         const struct bond_option *opt;
609         int ret = -ENOENT;
610
611         ASSERT_RTNL();
612
613         opt = bond_opt_get(option);
614         if (WARN_ON(!val) || WARN_ON(!opt))
615                 goto out;
616         ret = bond_opt_check_deps(bond, opt);
617         if (ret)
618                 goto out;
619         retval = bond_opt_parse(opt, val);
620         if (!retval) {
621                 ret = -EINVAL;
622                 goto out;
623         }
624         ret = opt->set(bond, retval);
625 out:
626         if (ret)
627                 bond_opt_error_interpret(bond, opt, ret, val);
628
629         return ret;
630 }
631
632 /**
633  * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
634  * @bond: target bond device
635  * @option: option to set
636  * @buf: value to set it to
637  *
638  * This function tries to acquire RTNL without blocking and if successful
639  * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
640  */
641 int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
642 {
643         struct bond_opt_value optval;
644         int ret;
645
646         if (!rtnl_trylock())
647                 return restart_syscall();
648         bond_opt_initstr(&optval, buf);
649         ret = __bond_opt_set(bond, option, &optval);
650         rtnl_unlock();
651
652         return ret;
653 }
654
655 /**
656  * bond_opt_get - get a pointer to an option
657  * @option: option for which to return a pointer
658  *
659  * This function checks if option is valid and if so returns a pointer
660  * to its entry in the bond_opts[] option array.
661  */
662 const struct bond_option *bond_opt_get(unsigned int option)
663 {
664         if (!BOND_OPT_VALID(option))
665                 return NULL;
666
667         return &bond_opts[option];
668 }
669
670 static int bond_option_mode_set(struct bonding *bond,
671                                 const struct bond_opt_value *newval)
672 {
673         if (!bond_mode_uses_arp(newval->value) && bond->params.arp_interval) {
674                 netdev_info(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
675                             newval->string);
676                 /* disable arp monitoring */
677                 bond->params.arp_interval = 0;
678                 /* set miimon to default value */
679                 bond->params.miimon = BOND_DEFAULT_MIIMON;
680                 netdev_info(bond->dev, "Setting MII monitoring interval to %d\n",
681                             bond->params.miimon);
682         }
683
684         /* don't cache arp_validate between modes */
685         bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
686         bond->params.mode = newval->value;
687
688         return 0;
689 }
690
691 static struct net_device *__bond_option_active_slave_get(struct bonding *bond,
692                                                          struct slave *slave)
693 {
694         return bond_uses_primary(bond) && slave ? slave->dev : NULL;
695 }
696
697 struct net_device *bond_option_active_slave_get_rcu(struct bonding *bond)
698 {
699         struct slave *slave = rcu_dereference(bond->curr_active_slave);
700
701         return __bond_option_active_slave_get(bond, slave);
702 }
703
704 static int bond_option_active_slave_set(struct bonding *bond,
705                                         const struct bond_opt_value *newval)
706 {
707         char ifname[IFNAMSIZ] = { 0, };
708         struct net_device *slave_dev;
709         int ret = 0;
710
711         sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
712         if (!strlen(ifname) || newval->string[0] == '\n') {
713                 slave_dev = NULL;
714         } else {
715                 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
716                 if (!slave_dev)
717                         return -ENODEV;
718         }
719
720         if (slave_dev) {
721                 if (!netif_is_bond_slave(slave_dev)) {
722                         netdev_err(bond->dev, "Device %s is not bonding slave\n",
723                                    slave_dev->name);
724                         return -EINVAL;
725                 }
726
727                 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
728                         netdev_err(bond->dev, "Device %s is not our slave\n",
729                                    slave_dev->name);
730                         return -EINVAL;
731                 }
732         }
733
734         block_netpoll_tx();
735         write_lock_bh(&bond->curr_slave_lock);
736
737         /* check to see if we are clearing active */
738         if (!slave_dev) {
739                 netdev_info(bond->dev, "Clearing current active slave\n");
740                 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
741                 bond_select_active_slave(bond);
742         } else {
743                 struct slave *old_active = bond_deref_active_protected(bond);
744                 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
745
746                 BUG_ON(!new_active);
747
748                 if (new_active == old_active) {
749                         /* do nothing */
750                         netdev_info(bond->dev, "%s is already the current active slave\n",
751                                     new_active->dev->name);
752                 } else {
753                         if (old_active && (new_active->link == BOND_LINK_UP) &&
754                             bond_slave_is_up(new_active)) {
755                                 netdev_info(bond->dev, "Setting %s as active slave\n",
756                                             new_active->dev->name);
757                                 bond_change_active_slave(bond, new_active);
758                         } else {
759                                 netdev_err(bond->dev, "Could not set %s as active slave; either %s is down or the link is down\n",
760                                            new_active->dev->name,
761                                            new_active->dev->name);
762                                 ret = -EINVAL;
763                         }
764                 }
765         }
766
767         write_unlock_bh(&bond->curr_slave_lock);
768         unblock_netpoll_tx();
769
770         return ret;
771 }
772
773 /* There are two tricky bits here.  First, if MII monitoring is activated, then
774  * we must disable ARP monitoring.  Second, if the timer isn't running, we must
775  * start it.
776  */
777 static int bond_option_miimon_set(struct bonding *bond,
778                                   const struct bond_opt_value *newval)
779 {
780         netdev_info(bond->dev, "Setting MII monitoring interval to %llu\n",
781                     newval->value);
782         bond->params.miimon = newval->value;
783         if (bond->params.updelay)
784                 netdev_info(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
785                         bond->params.updelay * bond->params.miimon);
786         if (bond->params.downdelay)
787                 netdev_info(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
788                             bond->params.downdelay * bond->params.miimon);
789         if (newval->value && bond->params.arp_interval) {
790                 netdev_info(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
791                 bond->params.arp_interval = 0;
792                 if (bond->params.arp_validate)
793                         bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
794         }
795         if (bond->dev->flags & IFF_UP) {
796                 /* If the interface is up, we may need to fire off
797                  * the MII timer. If the interface is down, the
798                  * timer will get fired off when the open function
799                  * is called.
800                  */
801                 if (!newval->value) {
802                         cancel_delayed_work_sync(&bond->mii_work);
803                 } else {
804                         cancel_delayed_work_sync(&bond->arp_work);
805                         queue_delayed_work(bond->wq, &bond->mii_work, 0);
806                 }
807         }
808
809         return 0;
810 }
811
812 /* Set up and down delays. These must be multiples of the
813  * MII monitoring value, and are stored internally as the multiplier.
814  * Thus, we must translate to MS for the real world.
815  */
816 static int bond_option_updelay_set(struct bonding *bond,
817                                    const struct bond_opt_value *newval)
818 {
819         int value = newval->value;
820
821         if (!bond->params.miimon) {
822                 netdev_err(bond->dev, "Unable to set up delay as MII monitoring is disabled\n");
823                 return -EPERM;
824         }
825         if ((value % bond->params.miimon) != 0) {
826                 netdev_warn(bond->dev, "up delay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
827                             value, bond->params.miimon,
828                             (value / bond->params.miimon) *
829                             bond->params.miimon);
830         }
831         bond->params.updelay = value / bond->params.miimon;
832         netdev_info(bond->dev, "Setting up delay to %d\n",
833                     bond->params.updelay * bond->params.miimon);
834
835         return 0;
836 }
837
838 static int bond_option_downdelay_set(struct bonding *bond,
839                                      const struct bond_opt_value *newval)
840 {
841         int value = newval->value;
842
843         if (!bond->params.miimon) {
844                 netdev_err(bond->dev, "Unable to set down delay as MII monitoring is disabled\n");
845                 return -EPERM;
846         }
847         if ((value % bond->params.miimon) != 0) {
848                 netdev_warn(bond->dev, "down delay (%d) is not a multiple of miimon (%d), delay rounded to %d ms\n",
849                             value, bond->params.miimon,
850                             (value / bond->params.miimon) *
851                             bond->params.miimon);
852         }
853         bond->params.downdelay = value / bond->params.miimon;
854         netdev_info(bond->dev, "Setting down delay to %d\n",
855                     bond->params.downdelay * bond->params.miimon);
856
857         return 0;
858 }
859
860 static int bond_option_use_carrier_set(struct bonding *bond,
861                                        const struct bond_opt_value *newval)
862 {
863         netdev_info(bond->dev, "Setting use_carrier to %llu\n",
864                     newval->value);
865         bond->params.use_carrier = newval->value;
866
867         return 0;
868 }
869
870 /* There are two tricky bits here.  First, if ARP monitoring is activated, then
871  * we must disable MII monitoring.  Second, if the ARP timer isn't running,
872  * we must start it.
873  */
874 static int bond_option_arp_interval_set(struct bonding *bond,
875                                         const struct bond_opt_value *newval)
876 {
877         netdev_info(bond->dev, "Setting ARP monitoring interval to %llu\n",
878                     newval->value);
879         bond->params.arp_interval = newval->value;
880         if (newval->value) {
881                 if (bond->params.miimon) {
882                         netdev_info(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
883                         bond->params.miimon = 0;
884                 }
885                 if (!bond->params.arp_targets[0])
886                         netdev_info(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
887         }
888         if (bond->dev->flags & IFF_UP) {
889                 /* If the interface is up, we may need to fire off
890                  * the ARP timer.  If the interface is down, the
891                  * timer will get fired off when the open function
892                  * is called.
893                  */
894                 if (!newval->value) {
895                         if (bond->params.arp_validate)
896                                 bond->recv_probe = NULL;
897                         cancel_delayed_work_sync(&bond->arp_work);
898                 } else {
899                         /* arp_validate can be set only in active-backup mode */
900                         bond->recv_probe = bond_arp_rcv;
901                         cancel_delayed_work_sync(&bond->mii_work);
902                         queue_delayed_work(bond->wq, &bond->arp_work, 0);
903                 }
904         }
905
906         return 0;
907 }
908
909 static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
910                                             __be32 target,
911                                             unsigned long last_rx)
912 {
913         __be32 *targets = bond->params.arp_targets;
914         struct list_head *iter;
915         struct slave *slave;
916
917         if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
918                 bond_for_each_slave(bond, slave, iter)
919                         slave->target_last_arp_rx[slot] = last_rx;
920                 targets[slot] = target;
921         }
922 }
923
924 static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
925 {
926         __be32 *targets = bond->params.arp_targets;
927         int ind;
928
929         if (!bond_is_ip_target_ok(target)) {
930                 netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
931                            &target);
932                 return -EINVAL;
933         }
934
935         if (bond_get_targets_ip(targets, target) != -1) { /* dup */
936                 netdev_err(bond->dev, "ARP target %pI4 is already present\n",
937                            &target);
938                 return -EINVAL;
939         }
940
941         ind = bond_get_targets_ip(targets, 0); /* first free slot */
942         if (ind == -1) {
943                 netdev_err(bond->dev, "ARP target table is full!\n");
944                 return -EINVAL;
945         }
946
947         netdev_info(bond->dev, "Adding ARP target %pI4\n", &target);
948
949         _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
950
951         return 0;
952 }
953
954 static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
955 {
956         int ret;
957
958         /* not to race with bond_arp_rcv */
959         write_lock_bh(&bond->lock);
960         ret = _bond_option_arp_ip_target_add(bond, target);
961         write_unlock_bh(&bond->lock);
962
963         return ret;
964 }
965
966 static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
967 {
968         __be32 *targets = bond->params.arp_targets;
969         struct list_head *iter;
970         struct slave *slave;
971         unsigned long *targets_rx;
972         int ind, i;
973
974         if (!bond_is_ip_target_ok(target)) {
975                 netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
976                            &target);
977                 return -EINVAL;
978         }
979
980         ind = bond_get_targets_ip(targets, target);
981         if (ind == -1) {
982                 netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
983                            &target);
984                 return -EINVAL;
985         }
986
987         if (ind == 0 && !targets[1] && bond->params.arp_interval)
988                 netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
989
990         netdev_info(bond->dev, "Removing ARP target %pI4\n", &target);
991
992         /* not to race with bond_arp_rcv */
993         write_lock_bh(&bond->lock);
994
995         bond_for_each_slave(bond, slave, iter) {
996                 targets_rx = slave->target_last_arp_rx;
997                 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
998                         targets_rx[i] = targets_rx[i+1];
999                 targets_rx[i] = 0;
1000         }
1001         for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1002                 targets[i] = targets[i+1];
1003         targets[i] = 0;
1004
1005         write_unlock_bh(&bond->lock);
1006
1007         return 0;
1008 }
1009
1010 void bond_option_arp_ip_targets_clear(struct bonding *bond)
1011 {
1012         int i;
1013
1014         /* not to race with bond_arp_rcv */
1015         write_lock_bh(&bond->lock);
1016         for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1017                 _bond_options_arp_ip_target_set(bond, i, 0, 0);
1018         write_unlock_bh(&bond->lock);
1019 }
1020
1021 static int bond_option_arp_ip_targets_set(struct bonding *bond,
1022                                           const struct bond_opt_value *newval)
1023 {
1024         int ret = -EPERM;
1025         __be32 target;
1026
1027         if (newval->string) {
1028                 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
1029                         netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
1030                                    &target);
1031                         return ret;
1032                 }
1033                 if (newval->string[0] == '+')
1034                         ret = bond_option_arp_ip_target_add(bond, target);
1035                 else if (newval->string[0] == '-')
1036                         ret = bond_option_arp_ip_target_rem(bond, target);
1037                 else
1038                         netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
1039         } else {
1040                 target = newval->value;
1041                 ret = bond_option_arp_ip_target_add(bond, target);
1042         }
1043
1044         return ret;
1045 }
1046
1047 static int bond_option_arp_validate_set(struct bonding *bond,
1048                                         const struct bond_opt_value *newval)
1049 {
1050         netdev_info(bond->dev, "Setting arp_validate to %s (%llu)\n",
1051                     newval->string, newval->value);
1052
1053         if (bond->dev->flags & IFF_UP) {
1054                 if (!newval->value)
1055                         bond->recv_probe = NULL;
1056                 else if (bond->params.arp_interval)
1057                         bond->recv_probe = bond_arp_rcv;
1058         }
1059         bond->params.arp_validate = newval->value;
1060
1061         return 0;
1062 }
1063
1064 static int bond_option_arp_all_targets_set(struct bonding *bond,
1065                                            const struct bond_opt_value *newval)
1066 {
1067         netdev_info(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
1068                     newval->string, newval->value);
1069         bond->params.arp_all_targets = newval->value;
1070
1071         return 0;
1072 }
1073
1074 static int bond_option_primary_set(struct bonding *bond,
1075                                    const struct bond_opt_value *newval)
1076 {
1077         char *p, *primary = newval->string;
1078         struct list_head *iter;
1079         struct slave *slave;
1080
1081         block_netpoll_tx();
1082         read_lock(&bond->lock);
1083         write_lock_bh(&bond->curr_slave_lock);
1084
1085         p = strchr(primary, '\n');
1086         if (p)
1087                 *p = '\0';
1088         /* check to see if we are clearing primary */
1089         if (!strlen(primary)) {
1090                 netdev_info(bond->dev, "Setting primary slave to None\n");
1091                 bond->primary_slave = NULL;
1092                 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1093                 bond_select_active_slave(bond);
1094                 goto out;
1095         }
1096
1097         bond_for_each_slave(bond, slave, iter) {
1098                 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
1099                         netdev_info(bond->dev, "Setting %s as primary slave\n",
1100                                     slave->dev->name);
1101                         bond->primary_slave = slave;
1102                         strcpy(bond->params.primary, slave->dev->name);
1103                         bond_select_active_slave(bond);
1104                         goto out;
1105                 }
1106         }
1107
1108         if (bond->primary_slave) {
1109                 netdev_info(bond->dev, "Setting primary slave to None\n");
1110                 bond->primary_slave = NULL;
1111                 bond_select_active_slave(bond);
1112         }
1113         strncpy(bond->params.primary, primary, IFNAMSIZ);
1114         bond->params.primary[IFNAMSIZ - 1] = 0;
1115
1116         netdev_info(bond->dev, "Recording %s as primary, but it has not been enslaved to %s yet\n",
1117                     primary, bond->dev->name);
1118
1119 out:
1120         write_unlock_bh(&bond->curr_slave_lock);
1121         read_unlock(&bond->lock);
1122         unblock_netpoll_tx();
1123
1124         return 0;
1125 }
1126
1127 static int bond_option_primary_reselect_set(struct bonding *bond,
1128                                             const struct bond_opt_value *newval)
1129 {
1130         netdev_info(bond->dev, "Setting primary_reselect to %s (%llu)\n",
1131                     newval->string, newval->value);
1132         bond->params.primary_reselect = newval->value;
1133
1134         block_netpoll_tx();
1135         write_lock_bh(&bond->curr_slave_lock);
1136         bond_select_active_slave(bond);
1137         write_unlock_bh(&bond->curr_slave_lock);
1138         unblock_netpoll_tx();
1139
1140         return 0;
1141 }
1142
1143 static int bond_option_fail_over_mac_set(struct bonding *bond,
1144                                          const struct bond_opt_value *newval)
1145 {
1146         netdev_info(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
1147                     newval->string, newval->value);
1148         bond->params.fail_over_mac = newval->value;
1149
1150         return 0;
1151 }
1152
1153 static int bond_option_xmit_hash_policy_set(struct bonding *bond,
1154                                             const struct bond_opt_value *newval)
1155 {
1156         netdev_info(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
1157                     newval->string, newval->value);
1158         bond->params.xmit_policy = newval->value;
1159
1160         return 0;
1161 }
1162
1163 static int bond_option_resend_igmp_set(struct bonding *bond,
1164                                        const struct bond_opt_value *newval)
1165 {
1166         netdev_info(bond->dev, "Setting resend_igmp to %llu\n",
1167                     newval->value);
1168         bond->params.resend_igmp = newval->value;
1169
1170         return 0;
1171 }
1172
1173 static int bond_option_num_peer_notif_set(struct bonding *bond,
1174                                    const struct bond_opt_value *newval)
1175 {
1176         bond->params.num_peer_notif = newval->value;
1177
1178         return 0;
1179 }
1180
1181 static int bond_option_all_slaves_active_set(struct bonding *bond,
1182                                              const struct bond_opt_value *newval)
1183 {
1184         struct list_head *iter;
1185         struct slave *slave;
1186
1187         if (newval->value == bond->params.all_slaves_active)
1188                 return 0;
1189         bond->params.all_slaves_active = newval->value;
1190         bond_for_each_slave(bond, slave, iter) {
1191                 if (!bond_is_active_slave(slave)) {
1192                         if (newval->value)
1193                                 slave->inactive = 0;
1194                         else
1195                                 slave->inactive = 1;
1196                 }
1197         }
1198
1199         return 0;
1200 }
1201
1202 static int bond_option_min_links_set(struct bonding *bond,
1203                                      const struct bond_opt_value *newval)
1204 {
1205         netdev_info(bond->dev, "Setting min links value to %llu\n",
1206                     newval->value);
1207         bond->params.min_links = newval->value;
1208
1209         return 0;
1210 }
1211
1212 static int bond_option_lp_interval_set(struct bonding *bond,
1213                                        const struct bond_opt_value *newval)
1214 {
1215         bond->params.lp_interval = newval->value;
1216
1217         return 0;
1218 }
1219
1220 static int bond_option_pps_set(struct bonding *bond,
1221                                const struct bond_opt_value *newval)
1222 {
1223         bond->params.packets_per_slave = newval->value;
1224         if (newval->value > 0) {
1225                 bond->params.reciprocal_packets_per_slave =
1226                         reciprocal_value(newval->value);
1227         } else {
1228                 /* reciprocal_packets_per_slave is unused if
1229                  * packets_per_slave is 0 or 1, just initialize it
1230                  */
1231                 bond->params.reciprocal_packets_per_slave =
1232                         (struct reciprocal_value) { 0 };
1233         }
1234
1235         return 0;
1236 }
1237
1238 static int bond_option_lacp_rate_set(struct bonding *bond,
1239                                      const struct bond_opt_value *newval)
1240 {
1241         netdev_info(bond->dev, "Setting LACP rate to %s (%llu)\n",
1242                     newval->string, newval->value);
1243         bond->params.lacp_fast = newval->value;
1244         bond_3ad_update_lacp_rate(bond);
1245
1246         return 0;
1247 }
1248
1249 static int bond_option_ad_select_set(struct bonding *bond,
1250                                      const struct bond_opt_value *newval)
1251 {
1252         netdev_info(bond->dev, "Setting ad_select to %s (%llu)\n",
1253                     newval->string, newval->value);
1254         bond->params.ad_select = newval->value;
1255
1256         return 0;
1257 }
1258
1259 static int bond_option_queue_id_set(struct bonding *bond,
1260                                     const struct bond_opt_value *newval)
1261 {
1262         struct slave *slave, *update_slave;
1263         struct net_device *sdev;
1264         struct list_head *iter;
1265         char *delim;
1266         int ret = 0;
1267         u16 qid;
1268
1269         /* delim will point to queue id if successful */
1270         delim = strchr(newval->string, ':');
1271         if (!delim)
1272                 goto err_no_cmd;
1273
1274         /* Terminate string that points to device name and bump it
1275          * up one, so we can read the queue id there.
1276          */
1277         *delim = '\0';
1278         if (sscanf(++delim, "%hd\n", &qid) != 1)
1279                 goto err_no_cmd;
1280
1281         /* Check buffer length, valid ifname and queue id */
1282         if (!dev_valid_name(newval->string) ||
1283             qid > bond->dev->real_num_tx_queues)
1284                 goto err_no_cmd;
1285
1286         /* Get the pointer to that interface if it exists */
1287         sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1288         if (!sdev)
1289                 goto err_no_cmd;
1290
1291         /* Search for thes slave and check for duplicate qids */
1292         update_slave = NULL;
1293         bond_for_each_slave(bond, slave, iter) {
1294                 if (sdev == slave->dev)
1295                         /* We don't need to check the matching
1296                          * slave for dups, since we're overwriting it
1297                          */
1298                         update_slave = slave;
1299                 else if (qid && qid == slave->queue_id) {
1300                         goto err_no_cmd;
1301                 }
1302         }
1303
1304         if (!update_slave)
1305                 goto err_no_cmd;
1306
1307         /* Actually set the qids for the slave */
1308         update_slave->queue_id = qid;
1309
1310 out:
1311         return ret;
1312
1313 err_no_cmd:
1314         netdev_info(bond->dev, "invalid input for queue_id set\n");
1315         ret = -EPERM;
1316         goto out;
1317
1318 }
1319
1320 static int bond_option_slaves_set(struct bonding *bond,
1321                                   const struct bond_opt_value *newval)
1322 {
1323         char command[IFNAMSIZ + 1] = { 0, };
1324         struct net_device *dev;
1325         char *ifname;
1326         int ret;
1327
1328         sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1329         ifname = command + 1;
1330         if ((strlen(command) <= 1) ||
1331             !dev_valid_name(ifname))
1332                 goto err_no_cmd;
1333
1334         dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1335         if (!dev) {
1336                 netdev_info(bond->dev, "interface %s does not exist!\n",
1337                             ifname);
1338                 ret = -ENODEV;
1339                 goto out;
1340         }
1341
1342         switch (command[0]) {
1343         case '+':
1344                 netdev_info(bond->dev, "Adding slave %s\n", dev->name);
1345                 ret = bond_enslave(bond->dev, dev);
1346                 break;
1347
1348         case '-':
1349                 netdev_info(bond->dev, "Removing slave %s\n", dev->name);
1350                 ret = bond_release(bond->dev, dev);
1351                 break;
1352
1353         default:
1354                 goto err_no_cmd;
1355         }
1356
1357 out:
1358         return ret;
1359
1360 err_no_cmd:
1361         netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
1362         ret = -EPERM;
1363         goto out;
1364 }
1365
1366 static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1367                                           const struct bond_opt_value *newval)
1368 {
1369         netdev_info(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
1370                     newval->string, newval->value);
1371         bond->params.tlb_dynamic_lb = newval->value;
1372
1373         return 0;
1374 }