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