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