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