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