blob: 7f0ad97926dee8b45af4b294ba92a655163d942f [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Pirko72be35f2013-10-18 17:43:34 +02002/*
3 * drivers/net/bond/bond_options.c - bonding options
4 * Copyright (c) 2013 Jiri Pirko <jiri@resnulli.us>
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -08005 * Copyright (c) 2013 Scott Feldman <sfeldma@cumulusnetworks.com>
Jiri Pirko72be35f2013-10-18 17:43:34 +02006 */
7
Jiri Pirko72be35f2013-10-18 17:43:34 +02008#include <linux/errno.h>
9#include <linux/if.h>
Jiri Pirkod9e32b22013-10-18 17:43:35 +020010#include <linux/netdevice.h>
Mike Galbraitheb2d4c62014-02-14 08:21:04 +010011#include <linux/spinlock.h>
Jiri Pirkod9e32b22013-10-18 17:43:35 +020012#include <linux/rcupdate.h>
Nikolay Aleksandrov09117362014-01-22 14:53:16 +010013#include <linux/ctype.h>
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +010014#include <linux/inet.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010015#include <linux/sched/signal.h>
16
David S. Miller1ef80192014-11-10 13:27:49 -050017#include <net/bonding.h>
Jiri Pirko72be35f2013-10-18 17:43:34 +020018
stephen hemmingerf3253332014-03-04 16:36:44 -080019static int bond_option_active_slave_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080020 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080021static int bond_option_miimon_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080022 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080023static int bond_option_updelay_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080024 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080025static int bond_option_downdelay_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080026 const struct bond_opt_value *newval);
Vincent Bernat07a4dde2019-07-02 19:43:54 +020027static int bond_option_peer_notif_delay_set(struct bonding *bond,
28 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080029static int bond_option_use_carrier_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080030 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080031static int bond_option_arp_interval_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080032 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080033static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target);
34static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target);
35static int bond_option_arp_ip_targets_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080036 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080037static int bond_option_arp_validate_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080038 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080039static int bond_option_arp_all_targets_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080040 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080041static int bond_option_primary_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080042 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080043static int bond_option_primary_reselect_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080044 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080045static int bond_option_fail_over_mac_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080046 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080047static int bond_option_xmit_hash_policy_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080048 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080049static int bond_option_resend_igmp_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080050 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080051static int bond_option_num_peer_notif_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080052 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080053static int bond_option_all_slaves_active_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080054 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080055static int bond_option_min_links_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080056 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080057static int bond_option_lp_interval_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080058 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080059static int bond_option_pps_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080060 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080061static int bond_option_lacp_rate_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080062 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080063static int bond_option_ad_select_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080064 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080065static int bond_option_queue_id_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080066 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080067static int bond_option_mode_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080068 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080069static int bond_option_slaves_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -080070 const struct bond_opt_value *newval);
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -070071static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
72 const struct bond_opt_value *newval);
Mahesh Bandewar6791e462015-05-09 00:01:55 -070073static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
74 const struct bond_opt_value *newval);
Mahesh Bandewar74514952015-05-09 00:01:56 -070075static int bond_option_ad_actor_system_set(struct bonding *bond,
76 const struct bond_opt_value *newval);
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -070077static int bond_option_ad_user_port_key_set(struct bonding *bond,
78 const struct bond_opt_value *newval);
stephen hemmingerf3253332014-03-04 16:36:44 -080079
80
81static const struct bond_opt_value bond_mode_tbl[] = {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +010082 { "balance-rr", BOND_MODE_ROUNDROBIN, BOND_VALFLAG_DEFAULT},
83 { "active-backup", BOND_MODE_ACTIVEBACKUP, 0},
84 { "balance-xor", BOND_MODE_XOR, 0},
85 { "broadcast", BOND_MODE_BROADCAST, 0},
86 { "802.3ad", BOND_MODE_8023AD, 0},
87 { "balance-tlb", BOND_MODE_TLB, 0},
88 { "balance-alb", BOND_MODE_ALB, 0},
89 { NULL, -1, 0},
90};
91
stephen hemmingerf3253332014-03-04 16:36:44 -080092static const struct bond_opt_value bond_pps_tbl[] = {
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +010093 { "default", 1, BOND_VALFLAG_DEFAULT},
94 { "maxval", USHRT_MAX, BOND_VALFLAG_MAX},
95 { NULL, -1, 0},
96};
97
stephen hemmingerf3253332014-03-04 16:36:44 -080098static const struct bond_opt_value bond_xmit_hashtype_tbl[] = {
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +010099 { "layer2", BOND_XMIT_POLICY_LAYER2, BOND_VALFLAG_DEFAULT},
100 { "layer3+4", BOND_XMIT_POLICY_LAYER34, 0},
101 { "layer2+3", BOND_XMIT_POLICY_LAYER23, 0},
102 { "encap2+3", BOND_XMIT_POLICY_ENCAP23, 0},
103 { "encap3+4", BOND_XMIT_POLICY_ENCAP34, 0},
104 { NULL, -1, 0},
105};
106
stephen hemmingerf3253332014-03-04 16:36:44 -0800107static const struct bond_opt_value bond_arp_validate_tbl[] = {
Veaceslav Falico896149f2014-02-18 07:48:40 +0100108 { "none", BOND_ARP_VALIDATE_NONE, BOND_VALFLAG_DEFAULT},
109 { "active", BOND_ARP_VALIDATE_ACTIVE, 0},
110 { "backup", BOND_ARP_VALIDATE_BACKUP, 0},
111 { "all", BOND_ARP_VALIDATE_ALL, 0},
112 { "filter", BOND_ARP_FILTER, 0},
113 { "filter_active", BOND_ARP_FILTER_ACTIVE, 0},
114 { "filter_backup", BOND_ARP_FILTER_BACKUP, 0},
115 { NULL, -1, 0},
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100116};
117
stephen hemmingerf3253332014-03-04 16:36:44 -0800118static const struct bond_opt_value bond_arp_all_targets_tbl[] = {
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100119 { "any", BOND_ARP_TARGETS_ANY, BOND_VALFLAG_DEFAULT},
120 { "all", BOND_ARP_TARGETS_ALL, 0},
121 { NULL, -1, 0},
122};
123
stephen hemmingerf3253332014-03-04 16:36:44 -0800124static const struct bond_opt_value bond_fail_over_mac_tbl[] = {
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +0100125 { "none", BOND_FOM_NONE, BOND_VALFLAG_DEFAULT},
126 { "active", BOND_FOM_ACTIVE, 0},
127 { "follow", BOND_FOM_FOLLOW, 0},
128 { NULL, -1, 0},
129};
130
stephen hemmingerf3253332014-03-04 16:36:44 -0800131static const struct bond_opt_value bond_intmax_tbl[] = {
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100132 { "off", 0, BOND_VALFLAG_DEFAULT},
133 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
Nikolay Aleksandrov81c70802014-05-15 13:35:23 +0200134 { NULL, -1, 0}
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100135};
136
stephen hemmingerf3253332014-03-04 16:36:44 -0800137static const struct bond_opt_value bond_lacp_rate_tbl[] = {
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +0100138 { "slow", AD_LACP_SLOW, 0},
139 { "fast", AD_LACP_FAST, 0},
140 { NULL, -1, 0},
141};
142
stephen hemmingerf3253332014-03-04 16:36:44 -0800143static const struct bond_opt_value bond_ad_select_tbl[] = {
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +0100144 { "stable", BOND_AD_STABLE, BOND_VALFLAG_DEFAULT},
145 { "bandwidth", BOND_AD_BANDWIDTH, 0},
146 { "count", BOND_AD_COUNT, 0},
147 { NULL, -1, 0},
148};
149
stephen hemmingerf3253332014-03-04 16:36:44 -0800150static const struct bond_opt_value bond_num_peer_notif_tbl[] = {
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +0100151 { "off", 0, 0},
152 { "maxval", 255, BOND_VALFLAG_MAX},
153 { "default", 1, BOND_VALFLAG_DEFAULT},
154 { NULL, -1, 0}
155};
156
stephen hemmingerf3253332014-03-04 16:36:44 -0800157static const struct bond_opt_value bond_primary_reselect_tbl[] = {
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +0100158 { "always", BOND_PRI_RESELECT_ALWAYS, BOND_VALFLAG_DEFAULT},
159 { "better", BOND_PRI_RESELECT_BETTER, 0},
160 { "failure", BOND_PRI_RESELECT_FAILURE, 0},
161 { NULL, -1},
162};
163
stephen hemmingerf3253332014-03-04 16:36:44 -0800164static const struct bond_opt_value bond_use_carrier_tbl[] = {
Debabrata Banerjeeb3c898e2018-05-16 14:02:13 -0400165 { "off", 0, 0},
166 { "on", 1, BOND_VALFLAG_DEFAULT},
167 { NULL, -1, 0}
Nikolay Aleksandrov0fff06082014-01-22 14:53:34 +0100168};
169
stephen hemmingerf3253332014-03-04 16:36:44 -0800170static const struct bond_opt_value bond_all_slaves_active_tbl[] = {
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +0100171 { "off", 0, BOND_VALFLAG_DEFAULT},
172 { "on", 1, 0},
173 { NULL, -1, 0}
174};
175
stephen hemmingerf3253332014-03-04 16:36:44 -0800176static const struct bond_opt_value bond_resend_igmp_tbl[] = {
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +0100177 { "off", 0, 0},
178 { "maxval", 255, BOND_VALFLAG_MAX},
179 { "default", 1, BOND_VALFLAG_DEFAULT},
180 { NULL, -1, 0}
181};
182
stephen hemmingerf3253332014-03-04 16:36:44 -0800183static const struct bond_opt_value bond_lp_interval_tbl[] = {
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100184 { "minval", 1, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
185 { "maxval", INT_MAX, BOND_VALFLAG_MAX},
Sasha Levin5bd4e4c2014-03-06 16:53:11 -0500186 { NULL, -1, 0},
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100187};
188
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -0700189static const struct bond_opt_value bond_tlb_dynamic_lb_tbl[] = {
190 { "off", 0, 0},
191 { "on", 1, BOND_VALFLAG_DEFAULT},
192 { NULL, -1, 0}
193};
194
Mahesh Bandewar6791e462015-05-09 00:01:55 -0700195static const struct bond_opt_value bond_ad_actor_sys_prio_tbl[] = {
196 { "minval", 1, BOND_VALFLAG_MIN},
197 { "maxval", 65535, BOND_VALFLAG_MAX | BOND_VALFLAG_DEFAULT},
198 { NULL, -1, 0},
199};
200
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -0700201static const struct bond_opt_value bond_ad_user_port_key_tbl[] = {
202 { "minval", 0, BOND_VALFLAG_MIN | BOND_VALFLAG_DEFAULT},
203 { "maxval", 1023, BOND_VALFLAG_MAX},
204 { NULL, -1, 0},
205};
206
Jonathan Toppins7bfa0142015-01-09 13:31:08 -0500207static const struct bond_option bond_opts[BOND_OPT_LAST] = {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100208 [BOND_OPT_MODE] = {
209 .id = BOND_OPT_MODE,
210 .name = "mode",
211 .desc = "bond device mode",
212 .flags = BOND_OPTFLAG_NOSLAVES | BOND_OPTFLAG_IFDOWN,
213 .values = bond_mode_tbl,
214 .set = bond_option_mode_set
215 },
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +0100216 [BOND_OPT_PACKETS_PER_SLAVE] = {
217 .id = BOND_OPT_PACKETS_PER_SLAVE,
218 .name = "packets_per_slave",
219 .desc = "Packets to send per slave in RR mode",
220 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ROUNDROBIN)),
221 .values = bond_pps_tbl,
222 .set = bond_option_pps_set
223 },
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +0100224 [BOND_OPT_XMIT_HASH] = {
225 .id = BOND_OPT_XMIT_HASH,
226 .name = "xmit_hash_policy",
Mahesh Bandewarf05b42e2014-04-22 16:30:20 -0700227 .desc = "balance-xor, 802.3ad, and tlb hashing method",
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +0100228 .values = bond_xmit_hashtype_tbl,
229 .set = bond_option_xmit_hash_policy_set
230 },
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100231 [BOND_OPT_ARP_VALIDATE] = {
232 .id = BOND_OPT_ARP_VALIDATE,
233 .name = "arp_validate",
234 .desc = "validate src/dst of ARP probes",
Veaceslav Falico13ac34a2014-02-18 07:48:37 +0100235 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
236 BIT(BOND_MODE_ALB),
Nikolay Aleksandrov16228882014-01-22 14:53:20 +0100237 .values = bond_arp_validate_tbl,
238 .set = bond_option_arp_validate_set
239 },
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +0100240 [BOND_OPT_ARP_ALL_TARGETS] = {
241 .id = BOND_OPT_ARP_ALL_TARGETS,
242 .name = "arp_all_targets",
243 .desc = "fail on any/all arp targets timeout",
244 .values = bond_arp_all_targets_tbl,
245 .set = bond_option_arp_all_targets_set
246 },
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +0100247 [BOND_OPT_FAIL_OVER_MAC] = {
248 .id = BOND_OPT_FAIL_OVER_MAC,
249 .name = "fail_over_mac",
250 .desc = "For active-backup, do not set all slaves to the same MAC",
251 .flags = BOND_OPTFLAG_NOSLAVES,
252 .values = bond_fail_over_mac_tbl,
253 .set = bond_option_fail_over_mac_set
254 },
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100255 [BOND_OPT_ARP_INTERVAL] = {
256 .id = BOND_OPT_ARP_INTERVAL,
257 .name = "arp_interval",
258 .desc = "arp interval in milliseconds",
259 .unsuppmodes = BIT(BOND_MODE_8023AD) | BIT(BOND_MODE_TLB) |
260 BIT(BOND_MODE_ALB),
261 .values = bond_intmax_tbl,
262 .set = bond_option_arp_interval_set
263 },
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +0100264 [BOND_OPT_ARP_TARGETS] = {
265 .id = BOND_OPT_ARP_TARGETS,
266 .name = "arp_ip_target",
267 .desc = "arp targets in n.n.n.n form",
268 .flags = BOND_OPTFLAG_RAWVAL,
269 .set = bond_option_arp_ip_targets_set
270 },
Nikolay Aleksandrov25a9b542014-01-22 14:53:25 +0100271 [BOND_OPT_DOWNDELAY] = {
272 .id = BOND_OPT_DOWNDELAY,
273 .name = "downdelay",
274 .desc = "Delay before considering link down, in milliseconds",
275 .values = bond_intmax_tbl,
276 .set = bond_option_downdelay_set
277 },
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100278 [BOND_OPT_UPDELAY] = {
279 .id = BOND_OPT_UPDELAY,
280 .name = "updelay",
281 .desc = "Delay before considering link up, in milliseconds",
282 .values = bond_intmax_tbl,
283 .set = bond_option_updelay_set
284 },
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +0100285 [BOND_OPT_LACP_RATE] = {
286 .id = BOND_OPT_LACP_RATE,
287 .name = "lacp_rate",
288 .desc = "LACPDU tx rate to request from 802.3ad partner",
289 .flags = BOND_OPTFLAG_IFDOWN,
290 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
291 .values = bond_lacp_rate_tbl,
292 .set = bond_option_lacp_rate_set
293 },
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +0100294 [BOND_OPT_MINLINKS] = {
295 .id = BOND_OPT_MINLINKS,
296 .name = "min_links",
297 .desc = "Minimum number of available links before turning on carrier",
298 .values = bond_intmax_tbl,
299 .set = bond_option_min_links_set
300 },
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +0100301 [BOND_OPT_AD_SELECT] = {
302 .id = BOND_OPT_AD_SELECT,
303 .name = "ad_select",
304 .desc = "803.ad aggregation selection logic",
305 .flags = BOND_OPTFLAG_IFDOWN,
306 .values = bond_ad_select_tbl,
307 .set = bond_option_ad_select_set
308 },
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +0100309 [BOND_OPT_NUM_PEER_NOTIF] = {
310 .id = BOND_OPT_NUM_PEER_NOTIF,
311 .name = "num_unsol_na",
312 .desc = "Number of peer notifications to send on failover event",
313 .values = bond_num_peer_notif_tbl,
314 .set = bond_option_num_peer_notif_set
315 },
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100316 [BOND_OPT_MIIMON] = {
317 .id = BOND_OPT_MIIMON,
318 .name = "miimon",
319 .desc = "Link check interval in milliseconds",
320 .values = bond_intmax_tbl,
321 .set = bond_option_miimon_set
322 },
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +0100323 [BOND_OPT_PRIMARY] = {
324 .id = BOND_OPT_PRIMARY,
325 .name = "primary",
326 .desc = "Primary network device to use",
327 .flags = BOND_OPTFLAG_RAWVAL,
328 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
329 BIT(BOND_MODE_TLB) |
330 BIT(BOND_MODE_ALB)),
331 .set = bond_option_primary_set
332 },
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +0100333 [BOND_OPT_PRIMARY_RESELECT] = {
334 .id = BOND_OPT_PRIMARY_RESELECT,
335 .name = "primary_reselect",
336 .desc = "Reselect primary slave once it comes up",
337 .values = bond_primary_reselect_tbl,
338 .set = bond_option_primary_reselect_set
339 },
Nikolay Aleksandrov0fff06082014-01-22 14:53:34 +0100340 [BOND_OPT_USE_CARRIER] = {
341 .id = BOND_OPT_USE_CARRIER,
342 .name = "use_carrier",
343 .desc = "Use netif_carrier_ok (vs MII ioctls) in miimon",
344 .values = bond_use_carrier_tbl,
345 .set = bond_option_use_carrier_set
346 },
Nikolay Aleksandrovd1fbd3ed2014-01-22 14:53:35 +0100347 [BOND_OPT_ACTIVE_SLAVE] = {
348 .id = BOND_OPT_ACTIVE_SLAVE,
349 .name = "active_slave",
350 .desc = "Currently active slave",
351 .flags = BOND_OPTFLAG_RAWVAL,
352 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_ACTIVEBACKUP) |
353 BIT(BOND_MODE_TLB) |
354 BIT(BOND_MODE_ALB)),
355 .set = bond_option_active_slave_set
356 },
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +0100357 [BOND_OPT_QUEUE_ID] = {
358 .id = BOND_OPT_QUEUE_ID,
359 .name = "queue_id",
360 .desc = "Set queue id of a slave",
361 .flags = BOND_OPTFLAG_RAWVAL,
362 .set = bond_option_queue_id_set
363 },
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +0100364 [BOND_OPT_ALL_SLAVES_ACTIVE] = {
365 .id = BOND_OPT_ALL_SLAVES_ACTIVE,
366 .name = "all_slaves_active",
367 .desc = "Keep all frames received on an interface by setting active flag for all slaves",
368 .values = bond_all_slaves_active_tbl,
369 .set = bond_option_all_slaves_active_set
370 },
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +0100371 [BOND_OPT_RESEND_IGMP] = {
372 .id = BOND_OPT_RESEND_IGMP,
373 .name = "resend_igmp",
374 .desc = "Number of IGMP membership reports to send on link failure",
375 .values = bond_resend_igmp_tbl,
376 .set = bond_option_resend_igmp_set
377 },
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +0100378 [BOND_OPT_LP_INTERVAL] = {
379 .id = BOND_OPT_LP_INTERVAL,
380 .name = "lp_interval",
381 .desc = "The number of seconds between instances where the bonding driver sends learning packets to each slave's peer switch",
382 .values = bond_lp_interval_tbl,
383 .set = bond_option_lp_interval_set
384 },
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +0100385 [BOND_OPT_SLAVES] = {
386 .id = BOND_OPT_SLAVES,
387 .name = "slaves",
388 .desc = "Slave membership management",
389 .flags = BOND_OPTFLAG_RAWVAL,
390 .set = bond_option_slaves_set
391 },
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -0700392 [BOND_OPT_TLB_DYNAMIC_LB] = {
393 .id = BOND_OPT_TLB_DYNAMIC_LB,
Nikolay Aleksandrovdc3e5d12014-05-08 14:23:54 +0200394 .name = "tlb_dynamic_lb",
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -0700395 .desc = "Enable dynamic flow shuffling",
Debabrata Banerjeee79c1052018-05-14 14:48:09 -0400396 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_TLB) | BIT(BOND_MODE_ALB)),
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -0700397 .values = bond_tlb_dynamic_lb_tbl,
398 .flags = BOND_OPTFLAG_IFDOWN,
399 .set = bond_option_tlb_dynamic_lb_set,
Mahesh Bandewar6791e462015-05-09 00:01:55 -0700400 },
401 [BOND_OPT_AD_ACTOR_SYS_PRIO] = {
402 .id = BOND_OPT_AD_ACTOR_SYS_PRIO,
403 .name = "ad_actor_sys_prio",
404 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
Mahesh Bandewar6791e462015-05-09 00:01:55 -0700405 .values = bond_ad_actor_sys_prio_tbl,
406 .set = bond_option_ad_actor_sys_prio_set,
407 },
Mahesh Bandewar74514952015-05-09 00:01:56 -0700408 [BOND_OPT_AD_ACTOR_SYSTEM] = {
409 .id = BOND_OPT_AD_ACTOR_SYSTEM,
410 .name = "ad_actor_system",
411 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
Nikolay Aleksandrov7f20cd22016-02-04 17:42:28 +0100412 .flags = BOND_OPTFLAG_RAWVAL,
Mahesh Bandewar74514952015-05-09 00:01:56 -0700413 .set = bond_option_ad_actor_system_set,
414 },
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -0700415 [BOND_OPT_AD_USER_PORT_KEY] = {
416 .id = BOND_OPT_AD_USER_PORT_KEY,
417 .name = "ad_user_port_key",
418 .unsuppmodes = BOND_MODE_ALL_EX(BIT(BOND_MODE_8023AD)),
419 .flags = BOND_OPTFLAG_IFDOWN,
420 .values = bond_ad_user_port_key_tbl,
421 .set = bond_option_ad_user_port_key_set,
Nikolay Aleksandrov205845a2015-07-24 15:50:31 +0200422 },
423 [BOND_OPT_NUM_PEER_NOTIF_ALIAS] = {
424 .id = BOND_OPT_NUM_PEER_NOTIF_ALIAS,
425 .name = "num_grat_arp",
426 .desc = "Number of peer notifications to send on failover event",
427 .values = bond_num_peer_notif_tbl,
428 .set = bond_option_num_peer_notif_set
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200429 },
430 [BOND_OPT_PEER_NOTIF_DELAY] = {
431 .id = BOND_OPT_PEER_NOTIF_DELAY,
432 .name = "peer_notif_delay",
433 .desc = "Delay between each peer notification on failover event, in milliseconds",
434 .values = bond_intmax_tbl,
435 .set = bond_option_peer_notif_delay_set
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -0700436 }
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100437};
438
Nikolay Aleksandrovdc3e5d12014-05-08 14:23:54 +0200439/* Searches for an option by name */
440const struct bond_option *bond_opt_get_by_name(const char *name)
441{
442 const struct bond_option *opt;
443 int option;
444
445 for (option = 0; option < BOND_OPT_LAST; option++) {
446 opt = bond_opt_get(option);
447 if (opt && !strcmp(opt->name, name))
448 return opt;
449 }
450
451 return NULL;
452}
453
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100454/* Searches for a value in opt's values[] table */
stephen hemmingerf3253332014-03-04 16:36:44 -0800455const struct bond_opt_value *bond_opt_get_val(unsigned int option, u64 val)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100456{
stephen hemmingerf3253332014-03-04 16:36:44 -0800457 const struct bond_option *opt;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100458 int i;
459
460 opt = bond_opt_get(option);
461 if (WARN_ON(!opt))
462 return NULL;
463 for (i = 0; opt->values && opt->values[i].string; i++)
464 if (opt->values[i].value == val)
465 return &opt->values[i];
466
467 return NULL;
468}
469
470/* Searches for a value in opt's values[] table which matches the flagmask */
stephen hemmingerf3253332014-03-04 16:36:44 -0800471static const struct bond_opt_value *bond_opt_get_flags(const struct bond_option *opt,
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100472 u32 flagmask)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100473{
474 int i;
475
476 for (i = 0; opt->values && opt->values[i].string; i++)
477 if (opt->values[i].flags & flagmask)
478 return &opt->values[i];
479
480 return NULL;
481}
482
483/* If maxval is missing then there's no range to check. In case minval is
484 * missing then it's considered to be 0.
485 */
486static bool bond_opt_check_range(const struct bond_option *opt, u64 val)
487{
stephen hemmingerf3253332014-03-04 16:36:44 -0800488 const struct bond_opt_value *minval, *maxval;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100489
490 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
491 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
492 if (!maxval || (minval && val < minval->value) || val > maxval->value)
493 return false;
494
495 return true;
496}
497
498/**
499 * bond_opt_parse - parse option value
500 * @opt: the option to parse against
501 * @val: value to parse
502 *
503 * This function tries to extract the value from @val and check if it's
504 * a possible match for the option and returns NULL if a match isn't found,
505 * or the struct_opt_value that matched. It also strips the new line from
506 * @val->string if it's present.
507 */
stephen hemmingerf3253332014-03-04 16:36:44 -0800508const struct bond_opt_value *bond_opt_parse(const struct bond_option *opt,
509 struct bond_opt_value *val)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100510{
511 char *p, valstr[BOND_OPT_MAX_NAMELEN + 1] = { 0, };
stephen hemmingerf3253332014-03-04 16:36:44 -0800512 const struct bond_opt_value *tbl;
513 const struct bond_opt_value *ret = NULL;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100514 bool checkval;
515 int i, rv;
516
517 /* No parsing if the option wants a raw val */
518 if (opt->flags & BOND_OPTFLAG_RAWVAL)
519 return val;
520
521 tbl = opt->values;
522 if (!tbl)
523 goto out;
524
525 /* ULLONG_MAX is used to bypass string processing */
526 checkval = val->value != ULLONG_MAX;
527 if (!checkval) {
528 if (!val->string)
529 goto out;
530 p = strchr(val->string, '\n');
531 if (p)
532 *p = '\0';
533 for (p = val->string; *p; p++)
534 if (!(isdigit(*p) || isspace(*p)))
535 break;
536 /* The following code extracts the string to match or the value
537 * and sets checkval appropriately
538 */
539 if (*p) {
540 rv = sscanf(val->string, "%32s", valstr);
541 } else {
542 rv = sscanf(val->string, "%llu", &val->value);
543 checkval = true;
544 }
545 if (!rv)
546 goto out;
547 }
548
549 for (i = 0; tbl[i].string; i++) {
550 /* Check for exact match */
551 if (checkval) {
552 if (val->value == tbl[i].value)
553 ret = &tbl[i];
554 } else {
555 if (!strcmp(valstr, "default") &&
556 (tbl[i].flags & BOND_VALFLAG_DEFAULT))
557 ret = &tbl[i];
558
559 if (!strcmp(valstr, tbl[i].string))
560 ret = &tbl[i];
561 }
562 /* Found an exact match */
563 if (ret)
564 goto out;
565 }
566 /* Possible range match */
567 if (checkval && bond_opt_check_range(opt, val->value))
568 ret = val;
569out:
570 return ret;
571}
572
573/* Check opt's dependencies against bond mode and currently set options */
574static int bond_opt_check_deps(struct bonding *bond,
575 const struct bond_option *opt)
576{
577 struct bond_params *params = &bond->params;
578
579 if (test_bit(params->mode, &opt->unsuppmodes))
580 return -EACCES;
581 if ((opt->flags & BOND_OPTFLAG_NOSLAVES) && bond_has_slaves(bond))
582 return -ENOTEMPTY;
583 if ((opt->flags & BOND_OPTFLAG_IFDOWN) && (bond->dev->flags & IFF_UP))
584 return -EBUSY;
585
586 return 0;
587}
588
589static void bond_opt_dep_print(struct bonding *bond,
590 const struct bond_option *opt)
591{
stephen hemminger28f084c2014-03-06 14:20:17 -0800592 const struct bond_opt_value *modeval;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100593 struct bond_params *params;
594
595 params = &bond->params;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100596 modeval = bond_opt_get_val(BOND_OPT_MODE, params->mode);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100597 if (test_bit(params->mode, &opt->unsuppmodes))
Veaceslav Falico2de390b2014-07-15 19:36:08 +0200598 netdev_err(bond->dev, "option %s: mode dependency failed, not supported in mode %s(%llu)\n",
599 opt->name, modeval->string, modeval->value);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100600}
601
602static void bond_opt_error_interpret(struct bonding *bond,
603 const struct bond_option *opt,
stephen hemminger28f084c2014-03-06 14:20:17 -0800604 int error, const struct bond_opt_value *val)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100605{
stephen hemminger28f084c2014-03-06 14:20:17 -0800606 const struct bond_opt_value *minval, *maxval;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100607 char *p;
608
609 switch (error) {
610 case -EINVAL:
611 if (val) {
612 if (val->string) {
613 /* sometimes RAWVAL opts may have new lines */
614 p = strchr(val->string, '\n');
615 if (p)
616 *p = '\0';
Veaceslav Falico2de390b2014-07-15 19:36:08 +0200617 netdev_err(bond->dev, "option %s: invalid value (%s)\n",
618 opt->name, val->string);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100619 } else {
Veaceslav Falico2de390b2014-07-15 19:36:08 +0200620 netdev_err(bond->dev, "option %s: invalid value (%llu)\n",
621 opt->name, val->value);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100622 }
623 }
624 minval = bond_opt_get_flags(opt, BOND_VALFLAG_MIN);
625 maxval = bond_opt_get_flags(opt, BOND_VALFLAG_MAX);
626 if (!maxval)
627 break;
Veaceslav Falico2de390b2014-07-15 19:36:08 +0200628 netdev_err(bond->dev, "option %s: allowed values %llu - %llu\n",
629 opt->name, minval ? minval->value : 0, maxval->value);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100630 break;
631 case -EACCES:
632 bond_opt_dep_print(bond, opt);
633 break;
634 case -ENOTEMPTY:
Veaceslav Falico2de390b2014-07-15 19:36:08 +0200635 netdev_err(bond->dev, "option %s: unable to set because the bond device has slaves\n",
636 opt->name);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100637 break;
638 case -EBUSY:
Veaceslav Falico2de390b2014-07-15 19:36:08 +0200639 netdev_err(bond->dev, "option %s: unable to set because the bond device is up\n",
640 opt->name);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100641 break;
642 default:
643 break;
644 }
645}
646
647/**
648 * __bond_opt_set - set a bonding option
649 * @bond: target bond device
650 * @option: option to set
651 * @val: value to set it to
652 *
653 * This function is used to change the bond's option value, it can be
654 * used for both enabling/changing an option and for disabling it. RTNL lock
655 * must be obtained before calling this function.
656 */
657int __bond_opt_set(struct bonding *bond,
658 unsigned int option, struct bond_opt_value *val)
659{
stephen hemminger28f084c2014-03-06 14:20:17 -0800660 const struct bond_opt_value *retval = NULL;
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100661 const struct bond_option *opt;
662 int ret = -ENOENT;
663
664 ASSERT_RTNL();
665
666 opt = bond_opt_get(option);
667 if (WARN_ON(!val) || WARN_ON(!opt))
668 goto out;
669 ret = bond_opt_check_deps(bond, opt);
670 if (ret)
671 goto out;
672 retval = bond_opt_parse(opt, val);
673 if (!retval) {
674 ret = -EINVAL;
675 goto out;
676 }
677 ret = opt->set(bond, retval);
678out:
679 if (ret)
680 bond_opt_error_interpret(bond, opt, ret, val);
Vlad Yasevich7a7e96e2017-05-27 10:14:35 -0400681
682 return ret;
683}
684/**
685 * __bond_opt_set_notify - set a bonding option
686 * @bond: target bond device
687 * @option: option to set
688 * @val: value to set it to
689 *
690 * This function is used to change the bond's option value and trigger
691 * a notification to user sapce. It can be used for both enabling/changing
692 * an option and for disabling it. RTNL lock must be obtained before calling
693 * this function.
694 */
695int __bond_opt_set_notify(struct bonding *bond,
696 unsigned int option, struct bond_opt_value *val)
697{
698 int ret = -ENOENT;
699
700 ASSERT_RTNL();
701
702 ret = __bond_opt_set(bond, option, val);
703
704 if (!ret && (bond->dev->reg_state == NETREG_REGISTERED))
Jiri Pirkod4261e52014-08-19 16:02:12 +0200705 call_netdevice_notifiers(NETDEV_CHANGEINFODATA, bond->dev);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100706
707 return ret;
708}
709
710/**
711 * bond_opt_tryset_rtnl - try to acquire rtnl and call __bond_opt_set
712 * @bond: target bond device
713 * @option: option to set
714 * @buf: value to set it to
715 *
716 * This function tries to acquire RTNL without blocking and if successful
717 * calls __bond_opt_set. It is mainly used for sysfs option manipulation.
718 */
719int bond_opt_tryset_rtnl(struct bonding *bond, unsigned int option, char *buf)
720{
721 struct bond_opt_value optval;
722 int ret;
723
724 if (!rtnl_trylock())
725 return restart_syscall();
726 bond_opt_initstr(&optval, buf);
Vlad Yasevich7a7e96e2017-05-27 10:14:35 -0400727 ret = __bond_opt_set_notify(bond, option, &optval);
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100728 rtnl_unlock();
729
730 return ret;
731}
732
733/**
734 * bond_opt_get - get a pointer to an option
735 * @option: option for which to return a pointer
736 *
737 * This function checks if option is valid and if so returns a pointer
738 * to its entry in the bond_opts[] option array.
739 */
stephen hemmingerf3253332014-03-04 16:36:44 -0800740const struct bond_option *bond_opt_get(unsigned int option)
Nikolay Aleksandrov09117362014-01-22 14:53:16 +0100741{
742 if (!BOND_OPT_VALID(option))
743 return NULL;
744
745 return &bond_opts[option];
746}
747
Tariq Toukanf45583d2021-01-17 16:59:45 +0200748static bool bond_set_xfrm_features(struct bonding *bond)
Jarod Wilson007ab532020-12-05 12:22:29 -0500749{
750 if (!IS_ENABLED(CONFIG_XFRM_OFFLOAD))
Tariq Toukanf45583d2021-01-17 16:59:45 +0200751 return false;
Jarod Wilson007ab532020-12-05 12:22:29 -0500752
Tariq Toukanf45583d2021-01-17 16:59:45 +0200753 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
754 bond->dev->wanted_features |= BOND_XFRM_FEATURES;
Jarod Wilson007ab532020-12-05 12:22:29 -0500755 else
Tariq Toukanf45583d2021-01-17 16:59:45 +0200756 bond->dev->wanted_features &= ~BOND_XFRM_FEATURES;
Jarod Wilson007ab532020-12-05 12:22:29 -0500757
Tariq Toukanf45583d2021-01-17 16:59:45 +0200758 return true;
Jarod Wilson007ab532020-12-05 12:22:29 -0500759}
760
Nikolay Aleksandrovff11d8b2014-07-15 16:08:57 +0200761static int bond_option_mode_set(struct bonding *bond,
762 const struct bond_opt_value *newval)
Jiri Pirko72be35f2013-10-18 17:43:34 +0200763{
Jarod Wilsonc1f897c2018-07-18 14:49:36 -0400764 if (!bond_mode_uses_arp(newval->value)) {
765 if (bond->params.arp_interval) {
766 netdev_dbg(bond->dev, "%s mode is incompatible with arp monitoring, start mii monitoring\n",
767 newval->string);
768 /* disable arp monitoring */
769 bond->params.arp_interval = 0;
770 }
771
772 if (!bond->params.miimon) {
773 /* set miimon to default value */
774 bond->params.miimon = BOND_DEFAULT_MIIMON;
775 netdev_dbg(bond->dev, "Setting MII monitoring interval to %d\n",
776 bond->params.miimon);
777 }
Jiri Pirko72be35f2013-10-18 17:43:34 +0200778 }
779
Kosuke Tatsukawac6644d02017-09-06 22:47:59 +0000780 if (newval->value == BOND_MODE_ALB)
781 bond->params.tlb_dynamic_lb = 1;
782
Jiri Pirko72be35f2013-10-18 17:43:34 +0200783 /* don't cache arp_validate between modes */
784 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +0100785 bond->params.mode = newval->value;
786
Tariq Toukanf45583d2021-01-17 16:59:45 +0200787 if (bond->dev->reg_state == NETREG_REGISTERED)
788 if (bond_set_xfrm_features(bond))
789 netdev_update_features(bond->dev);
790
Jiri Pirko72be35f2013-10-18 17:43:34 +0200791 return 0;
792}
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200793
stephen hemmingerf3253332014-03-04 16:36:44 -0800794static int bond_option_active_slave_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -0800795 const struct bond_opt_value *newval)
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200796{
Nikolay Aleksandrovd1fbd3ed2014-01-22 14:53:35 +0100797 char ifname[IFNAMSIZ] = { 0, };
798 struct net_device *slave_dev;
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200799 int ret = 0;
800
Nikolay Aleksandrovd1fbd3ed2014-01-22 14:53:35 +0100801 sscanf(newval->string, "%15s", ifname); /* IFNAMSIZ */
802 if (!strlen(ifname) || newval->string[0] == '\n') {
803 slave_dev = NULL;
804 } else {
805 slave_dev = __dev_get_by_name(dev_net(bond->dev), ifname);
806 if (!slave_dev)
807 return -ENODEV;
808 }
809
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200810 if (slave_dev) {
811 if (!netif_is_bond_slave(slave_dev)) {
Jarod Wilsonf887e542019-06-07 10:59:32 -0400812 slave_err(bond->dev, slave_dev, "Device is not bonding slave\n");
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200813 return -EINVAL;
814 }
815
816 if (bond->dev != netdev_master_upper_dev_get(slave_dev)) {
Jarod Wilsonf887e542019-06-07 10:59:32 -0400817 slave_err(bond->dev, slave_dev, "Device is not our slave\n");
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200818 return -EINVAL;
819 }
820 }
821
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200822 block_netpoll_tx();
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200823 /* check to see if we are clearing active */
824 if (!slave_dev) {
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100825 netdev_dbg(bond->dev, "Clearing current active slave\n");
Monam Agarwal8800a242014-03-24 00:00:17 +0530826 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200827 bond_select_active_slave(bond);
828 } else {
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200829 struct slave *old_active = rtnl_dereference(bond->curr_active_slave);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200830 struct slave *new_active = bond_slave_get_rtnl(slave_dev);
831
832 BUG_ON(!new_active);
833
834 if (new_active == old_active) {
835 /* do nothing */
Jarod Wilsonf887e542019-06-07 10:59:32 -0400836 slave_dbg(bond->dev, new_active->dev, "is already the current active slave\n");
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200837 } else {
838 if (old_active && (new_active->link == BOND_LINK_UP) &&
Veaceslav Falicob6adc612014-05-15 21:39:57 +0200839 bond_slave_is_up(new_active)) {
Jarod Wilsonf887e542019-06-07 10:59:32 -0400840 slave_dbg(bond->dev, new_active->dev, "Setting as active slave\n");
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200841 bond_change_active_slave(bond, new_active);
842 } else {
Jarod Wilsonf887e542019-06-07 10:59:32 -0400843 slave_err(bond->dev, new_active->dev, "Could not set as active slave; either %s is down or the link is down\n",
844 new_active->dev->name);
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200845 ret = -EINVAL;
846 }
847 }
848 }
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200849 unblock_netpoll_tx();
Nikolay Aleksandrovd1fbd3ed2014-01-22 14:53:35 +0100850
Jiri Pirkod9e32b22013-10-18 17:43:35 +0200851 return ret;
852}
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800853
Nikolay Aleksandrovdc3e5d12014-05-08 14:23:54 +0200854/* There are two tricky bits here. First, if MII monitoring is activated, then
855 * we must disable ARP monitoring. Second, if the timer isn't running, we must
856 * start it.
857 */
stephen hemmingerf3253332014-03-04 16:36:44 -0800858static int bond_option_miimon_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -0800859 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800860{
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100861 netdev_dbg(bond->dev, "Setting MII monitoring interval to %llu\n",
862 newval->value);
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100863 bond->params.miimon = newval->value;
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800864 if (bond->params.updelay)
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100865 netdev_dbg(bond->dev, "Note: Updating updelay (to %d) since it is a multiple of the miimon value\n",
866 bond->params.updelay * bond->params.miimon);
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800867 if (bond->params.downdelay)
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100868 netdev_dbg(bond->dev, "Note: Updating downdelay (to %d) since it is a multiple of the miimon value\n",
869 bond->params.downdelay * bond->params.miimon);
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200870 if (bond->params.peer_notif_delay)
871 netdev_dbg(bond->dev, "Note: Updating peer_notif_delay (to %d) since it is a multiple of the miimon value\n",
872 bond->params.peer_notif_delay * bond->params.miimon);
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100873 if (newval->value && bond->params.arp_interval) {
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100874 netdev_dbg(bond->dev, "MII monitoring cannot be used with ARP monitoring - disabling ARP monitoring...\n");
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800875 bond->params.arp_interval = 0;
876 if (bond->params.arp_validate)
877 bond->params.arp_validate = BOND_ARP_VALIDATE_NONE;
878 }
879 if (bond->dev->flags & IFF_UP) {
880 /* If the interface is up, we may need to fire off
881 * the MII timer. If the interface is down, the
882 * timer will get fired off when the open function
883 * is called.
884 */
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100885 if (!newval->value) {
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800886 cancel_delayed_work_sync(&bond->mii_work);
887 } else {
888 cancel_delayed_work_sync(&bond->arp_work);
889 queue_delayed_work(bond->wq, &bond->mii_work, 0);
890 }
891 }
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100892
sfeldma@cumulusnetworks.comeecdaa62013-12-12 14:09:55 -0800893 return 0;
894}
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800895
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200896/* Set up, down and peer notification delays. These must be multiples
897 * of the MII monitoring value, and are stored internally as the
898 * multiplier. Thus, we must translate to MS for the real world.
Nikolay Aleksandrovdc3e5d12014-05-08 14:23:54 +0200899 */
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200900static int _bond_option_delay_set(struct bonding *bond,
901 const struct bond_opt_value *newval,
902 const char *name,
903 int *target)
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800904{
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100905 int value = newval->value;
906
Nikolay Aleksandrove4994612014-01-22 14:53:26 +0100907 if (!bond->params.miimon) {
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200908 netdev_err(bond->dev, "Unable to set %s as MII monitoring is disabled\n",
909 name);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800910 return -EPERM;
911 }
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100912 if ((value % bond->params.miimon) != 0) {
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200913 netdev_warn(bond->dev,
914 "%s (%d) is not a multiple of miimon (%d), value rounded to %d ms\n",
915 name,
Veaceslav Falico2de390b2014-07-15 19:36:08 +0200916 value, bond->params.miimon,
917 (value / bond->params.miimon) *
918 bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800919 }
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200920 *target = value / bond->params.miimon;
921 netdev_dbg(bond->dev, "Setting %s to %d\n",
922 name,
923 *target * bond->params.miimon);
sfeldma@cumulusnetworks.com25852e22013-12-12 14:10:02 -0800924
925 return 0;
926}
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800927
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200928static int bond_option_updelay_set(struct bonding *bond,
929 const struct bond_opt_value *newval)
930{
931 return _bond_option_delay_set(bond, newval, "up delay",
932 &bond->params.updelay);
933}
934
stephen hemmingerf3253332014-03-04 16:36:44 -0800935static int bond_option_downdelay_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -0800936 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800937{
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200938 return _bond_option_delay_set(bond, newval, "down delay",
939 &bond->params.downdelay);
940}
Nikolay Aleksandrov0681a282014-01-23 23:43:43 +0100941
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200942static int bond_option_peer_notif_delay_set(struct bonding *bond,
943 const struct bond_opt_value *newval)
944{
945 int ret = _bond_option_delay_set(bond, newval,
946 "peer notification delay",
947 &bond->params.peer_notif_delay);
948 return ret;
sfeldma@cumulusnetworks.comc7461f92013-12-12 14:10:09 -0800949}
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800950
stephen hemmingerf3253332014-03-04 16:36:44 -0800951static int bond_option_use_carrier_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -0800952 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800953{
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100954 netdev_dbg(bond->dev, "Setting use_carrier to %llu\n",
955 newval->value);
Nikolay Aleksandrov0fff06082014-01-22 14:53:34 +0100956 bond->params.use_carrier = newval->value;
sfeldma@cumulusnetworks.com9f53e142013-12-12 14:10:16 -0800957
958 return 0;
959}
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800960
Nikolay Aleksandrovdc3e5d12014-05-08 14:23:54 +0200961/* There are two tricky bits here. First, if ARP monitoring is activated, then
962 * we must disable MII monitoring. Second, if the ARP timer isn't running,
963 * we must start it.
964 */
stephen hemmingerf3253332014-03-04 16:36:44 -0800965static int bond_option_arp_interval_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -0800966 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800967{
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100968 netdev_dbg(bond->dev, "Setting ARP monitoring interval to %llu\n",
969 newval->value);
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100970 bond->params.arp_interval = newval->value;
971 if (newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800972 if (bond->params.miimon) {
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100973 netdev_dbg(bond->dev, "ARP monitoring cannot be used with MII monitoring. Disabling MII monitoring\n");
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800974 bond->params.miimon = 0;
975 }
976 if (!bond->params.arp_targets[0])
Michael Dilmoreeac306b2017-06-26 16:49:46 +0100977 netdev_dbg(bond->dev, "ARP monitoring has been set up, but no ARP targets have been specified\n");
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800978 }
979 if (bond->dev->flags & IFF_UP) {
980 /* If the interface is up, we may need to fire off
981 * the ARP timer. If the interface is down, the
982 * timer will get fired off when the open function
983 * is called.
984 */
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100985 if (!newval->value) {
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800986 if (bond->params.arp_validate)
987 bond->recv_probe = NULL;
988 cancel_delayed_work_sync(&bond->arp_work);
989 } else {
990 /* arp_validate can be set only in active-backup mode */
Veaceslav Falico3fe68df2014-02-18 07:48:39 +0100991 bond->recv_probe = bond_arp_rcv;
sfeldma@cumulusnetworks.com06151db2013-12-12 14:10:24 -0800992 cancel_delayed_work_sync(&bond->mii_work);
993 queue_delayed_work(bond->wq, &bond->arp_work, 0);
994 }
995 }
996
997 return 0;
998}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -0800999
1000static void _bond_options_arp_ip_target_set(struct bonding *bond, int slot,
1001 __be32 target,
1002 unsigned long last_rx)
1003{
1004 __be32 *targets = bond->params.arp_targets;
1005 struct list_head *iter;
1006 struct slave *slave;
1007
1008 if (slot >= 0 && slot < BOND_MAX_ARP_TARGETS) {
1009 bond_for_each_slave(bond, slave, iter)
1010 slave->target_last_arp_rx[slot] = last_rx;
1011 targets[slot] = target;
1012 }
1013}
1014
1015static int _bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
1016{
1017 __be32 *targets = bond->params.arp_targets;
1018 int ind;
1019
Veaceslav Falico2807a9f2014-05-15 21:39:56 +02001020 if (!bond_is_ip_target_ok(target)) {
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001021 netdev_err(bond->dev, "invalid ARP target %pI4 specified for addition\n",
1022 &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001023 return -EINVAL;
1024 }
1025
1026 if (bond_get_targets_ip(targets, target) != -1) { /* dup */
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001027 netdev_err(bond->dev, "ARP target %pI4 is already present\n",
1028 &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001029 return -EINVAL;
1030 }
1031
1032 ind = bond_get_targets_ip(targets, 0); /* first free slot */
1033 if (ind == -1) {
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001034 netdev_err(bond->dev, "ARP target table is full!\n");
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001035 return -EINVAL;
1036 }
1037
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001038 netdev_dbg(bond->dev, "Adding ARP target %pI4\n", &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001039
1040 _bond_options_arp_ip_target_set(bond, ind, target, jiffies);
1041
1042 return 0;
1043}
1044
stephen hemmingerf3253332014-03-04 16:36:44 -08001045static int bond_option_arp_ip_target_add(struct bonding *bond, __be32 target)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001046{
Nikolay Aleksandrov246df7b2014-09-09 23:17:02 +02001047 return _bond_option_arp_ip_target_add(bond, target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001048}
1049
stephen hemmingerf3253332014-03-04 16:36:44 -08001050static int bond_option_arp_ip_target_rem(struct bonding *bond, __be32 target)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001051{
1052 __be32 *targets = bond->params.arp_targets;
1053 struct list_head *iter;
1054 struct slave *slave;
1055 unsigned long *targets_rx;
1056 int ind, i;
1057
Veaceslav Falico2807a9f2014-05-15 21:39:56 +02001058 if (!bond_is_ip_target_ok(target)) {
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001059 netdev_err(bond->dev, "invalid ARP target %pI4 specified for removal\n",
1060 &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001061 return -EINVAL;
1062 }
1063
1064 ind = bond_get_targets_ip(targets, target);
1065 if (ind == -1) {
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001066 netdev_err(bond->dev, "unable to remove nonexistent ARP target %pI4\n",
1067 &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001068 return -EINVAL;
1069 }
1070
1071 if (ind == 0 && !targets[1] && bond->params.arp_interval)
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001072 netdev_warn(bond->dev, "Removing last arp target with arp_interval on\n");
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001073
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001074 netdev_dbg(bond->dev, "Removing ARP target %pI4\n", &target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001075
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001076 bond_for_each_slave(bond, slave, iter) {
1077 targets_rx = slave->target_last_arp_rx;
1078 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1079 targets_rx[i] = targets_rx[i+1];
1080 targets_rx[i] = 0;
1081 }
1082 for (i = ind; (i < BOND_MAX_ARP_TARGETS-1) && targets[i+1]; i++)
1083 targets[i] = targets[i+1];
1084 targets[i] = 0;
1085
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001086 return 0;
1087}
1088
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +01001089void bond_option_arp_ip_targets_clear(struct bonding *bond)
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001090{
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +01001091 int i;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001092
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001093 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
1094 _bond_options_arp_ip_target_set(bond, i, 0, 0);
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +01001095}
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001096
stephen hemmingerf3253332014-03-04 16:36:44 -08001097static int bond_option_arp_ip_targets_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001098 const struct bond_opt_value *newval)
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +01001099{
1100 int ret = -EPERM;
1101 __be32 target;
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001102
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +01001103 if (newval->string) {
1104 if (!in4_pton(newval->string+1, -1, (u8 *)&target, -1, NULL)) {
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001105 netdev_err(bond->dev, "invalid ARP target %pI4 specified\n",
1106 &target);
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +01001107 return ret;
1108 }
1109 if (newval->string[0] == '+')
1110 ret = bond_option_arp_ip_target_add(bond, target);
1111 else if (newval->string[0] == '-')
1112 ret = bond_option_arp_ip_target_rem(bond, target);
1113 else
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001114 netdev_err(bond->dev, "no command found in arp_ip_targets file - use +<addr> or -<addr>\n");
Nikolay Aleksandrov4fb0ef582014-01-22 14:53:24 +01001115 } else {
1116 target = newval->value;
1117 ret = bond_option_arp_ip_target_add(bond, target);
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001118 }
1119
sfeldma@cumulusnetworks.com7f28fa12013-12-12 14:10:31 -08001120 return ret;
1121}
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001122
stephen hemmingerf3253332014-03-04 16:36:44 -08001123static int bond_option_arp_validate_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001124 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001125{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001126 netdev_dbg(bond->dev, "Setting arp_validate to %s (%llu)\n",
1127 newval->string, newval->value);
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01001128 bond->params.arp_validate = newval->value;
sfeldma@cumulusnetworks.com29c49482013-12-12 14:10:38 -08001129
1130 return 0;
1131}
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -08001132
stephen hemmingerf3253332014-03-04 16:36:44 -08001133static int bond_option_arp_all_targets_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001134 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -08001135{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001136 netdev_dbg(bond->dev, "Setting arp_all_targets to %s (%llu)\n",
1137 newval->string, newval->value);
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +01001138 bond->params.arp_all_targets = newval->value;
sfeldma@cumulusnetworks.comd5c84252013-12-12 14:10:45 -08001139
1140 return 0;
1141}
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001142
stephen hemmingerf3253332014-03-04 16:36:44 -08001143static int bond_option_primary_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001144 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001145{
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001146 char *p, *primary = newval->string;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001147 struct list_head *iter;
1148 struct slave *slave;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001149
1150 block_netpoll_tx();
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001151
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001152 p = strchr(primary, '\n');
1153 if (p)
1154 *p = '\0';
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001155 /* check to see if we are clearing primary */
1156 if (!strlen(primary)) {
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001157 netdev_dbg(bond->dev, "Setting primary slave to None\n");
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001158 RCU_INIT_POINTER(bond->primary_slave, NULL);
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001159 memset(bond->params.primary, 0, sizeof(bond->params.primary));
1160 bond_select_active_slave(bond);
1161 goto out;
1162 }
1163
1164 bond_for_each_slave(bond, slave, iter) {
1165 if (strncmp(slave->dev->name, primary, IFNAMSIZ) == 0) {
Jarod Wilsonf887e542019-06-07 10:59:32 -04001166 slave_dbg(bond->dev, slave->dev, "Setting as primary slave\n");
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001167 rcu_assign_pointer(bond->primary_slave, slave);
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001168 strcpy(bond->params.primary, slave->dev->name);
Xiangning Yueb55bbf2018-06-07 13:39:59 +08001169 bond->force_primary = true;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001170 bond_select_active_slave(bond);
1171 goto out;
1172 }
1173 }
1174
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001175 if (rtnl_dereference(bond->primary_slave)) {
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001176 netdev_dbg(bond->dev, "Setting primary slave to None\n");
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001177 RCU_INIT_POINTER(bond->primary_slave, NULL);
dingtianhongc59ab672014-01-18 16:28:52 +08001178 bond_select_active_slave(bond);
1179 }
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001180 strncpy(bond->params.primary, primary, IFNAMSIZ);
1181 bond->params.primary[IFNAMSIZ - 1] = 0;
1182
Jarod Wilsonf887e542019-06-07 10:59:32 -04001183 netdev_dbg(bond->dev, "Recording %s as primary, but it has not been enslaved yet\n",
1184 primary);
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001185
1186out:
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001187 unblock_netpoll_tx();
1188
Nikolay Aleksandrov180222f2014-01-22 14:53:32 +01001189 return 0;
sfeldma@cumulusnetworks.com0a98a0d2013-12-15 16:41:51 -08001190}
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001191
stephen hemmingerf3253332014-03-04 16:36:44 -08001192static int bond_option_primary_reselect_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001193 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001194{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001195 netdev_dbg(bond->dev, "Setting primary_reselect to %s (%llu)\n",
1196 newval->string, newval->value);
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01001197 bond->params.primary_reselect = newval->value;
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001198
1199 block_netpoll_tx();
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001200 bond_select_active_slave(bond);
sfeldma@cumulusnetworks.com8a41ae42013-12-15 16:41:58 -08001201 unblock_netpoll_tx();
1202
1203 return 0;
1204}
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001205
stephen hemmingerf3253332014-03-04 16:36:44 -08001206static int bond_option_fail_over_mac_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001207 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001208{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001209 netdev_dbg(bond->dev, "Setting fail_over_mac to %s (%llu)\n",
1210 newval->string, newval->value);
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01001211 bond->params.fail_over_mac = newval->value;
sfeldma@cumulusnetworks.com89901972013-12-15 16:42:05 -08001212
1213 return 0;
1214}
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001215
stephen hemmingerf3253332014-03-04 16:36:44 -08001216static int bond_option_xmit_hash_policy_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001217 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001218{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001219 netdev_dbg(bond->dev, "Setting xmit hash policy to %s (%llu)\n",
1220 newval->string, newval->value);
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01001221 bond->params.xmit_policy = newval->value;
sfeldma@cumulusnetworks.comf70161c2013-12-15 16:42:12 -08001222
1223 return 0;
1224}
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001225
stephen hemmingerf3253332014-03-04 16:36:44 -08001226static int bond_option_resend_igmp_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001227 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001228{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001229 netdev_dbg(bond->dev, "Setting resend_igmp to %llu\n",
1230 newval->value);
Nikolay Aleksandrov105c8fb2014-01-22 14:53:38 +01001231 bond->params.resend_igmp = newval->value;
sfeldma@cumulusnetworks.comd8838de72013-12-15 16:42:19 -08001232
1233 return 0;
1234}
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001235
stephen hemmingerf3253332014-03-04 16:36:44 -08001236static int bond_option_num_peer_notif_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001237 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001238{
Nikolay Aleksandrovef56bec2014-01-22 14:53:30 +01001239 bond->params.num_peer_notif = newval->value;
1240
sfeldma@cumulusnetworks.com2c9839c2013-12-17 21:30:09 -08001241 return 0;
1242}
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001243
stephen hemmingerf3253332014-03-04 16:36:44 -08001244static int bond_option_all_slaves_active_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001245 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001246{
1247 struct list_head *iter;
1248 struct slave *slave;
1249
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001250 if (newval->value == bond->params.all_slaves_active)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001251 return 0;
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001252 bond->params.all_slaves_active = newval->value;
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001253 bond_for_each_slave(bond, slave, iter) {
1254 if (!bond_is_active_slave(slave)) {
Nikolay Aleksandrov3df01162014-01-22 14:53:37 +01001255 if (newval->value)
sfeldma@cumulusnetworks.com1cc0b1e2013-12-17 21:30:16 -08001256 slave->inactive = 0;
1257 else
1258 slave->inactive = 1;
1259 }
1260 }
1261
1262 return 0;
1263}
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001264
stephen hemmingerf3253332014-03-04 16:36:44 -08001265static int bond_option_min_links_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001266 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001267{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001268 netdev_dbg(bond->dev, "Setting min links value to %llu\n",
1269 newval->value);
Nikolay Aleksandrov633ddc92014-01-22 14:53:28 +01001270 bond->params.min_links = newval->value;
Jonathan Toppins2477bc92015-01-26 01:16:57 -05001271 bond_set_carrier(bond);
sfeldma@cumulusnetworks.com7d101002013-12-17 21:30:23 -08001272
1273 return 0;
1274}
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001275
stephen hemmingerf3253332014-03-04 16:36:44 -08001276static int bond_option_lp_interval_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001277 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001278{
Nikolay Aleksandrov4325b372014-01-22 14:53:39 +01001279 bond->params.lp_interval = newval->value;
sfeldma@cumulusnetworks.com8d836d092013-12-17 21:30:30 -08001280
1281 return 0;
1282}
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001283
stephen hemmingerf3253332014-03-04 16:36:44 -08001284static int bond_option_pps_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001285 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001286{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001287 netdev_dbg(bond->dev, "Setting packets per slave to %llu\n",
1288 newval->value);
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001289 bond->params.packets_per_slave = newval->value;
1290 if (newval->value > 0) {
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001291 bond->params.reciprocal_packets_per_slave =
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01001292 reciprocal_value(newval->value);
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01001293 } else {
1294 /* reciprocal_packets_per_slave is unused if
1295 * packets_per_slave is 0 or 1, just initialize it
1296 */
1297 bond->params.reciprocal_packets_per_slave =
1298 (struct reciprocal_value) { 0 };
1299 }
sfeldma@cumulusnetworks.comc13ab3f2013-12-17 21:30:37 -08001300
1301 return 0;
1302}
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001303
stephen hemmingerf3253332014-03-04 16:36:44 -08001304static int bond_option_lacp_rate_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001305 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001306{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001307 netdev_dbg(bond->dev, "Setting LACP rate to %s (%llu)\n",
1308 newval->string, newval->value);
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01001309 bond->params.lacp_fast = newval->value;
sfeldma@cumulusnetworks.com3243c472014-01-03 14:28:18 -08001310 bond_3ad_update_lacp_rate(bond);
sfeldma@cumulusnetworks.com998e40bb2014-01-03 14:18:41 -08001311
1312 return 0;
1313}
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001314
stephen hemmingerf3253332014-03-04 16:36:44 -08001315static int bond_option_ad_select_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001316 const struct bond_opt_value *newval)
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001317{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001318 netdev_dbg(bond->dev, "Setting ad_select to %s (%llu)\n",
1319 newval->string, newval->value);
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01001320 bond->params.ad_select = newval->value;
sfeldma@cumulusnetworks.comec029fa2014-01-03 14:18:49 -08001321
1322 return 0;
1323}
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001324
stephen hemmingerf3253332014-03-04 16:36:44 -08001325static int bond_option_queue_id_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001326 const struct bond_opt_value *newval)
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001327{
1328 struct slave *slave, *update_slave;
1329 struct net_device *sdev;
1330 struct list_head *iter;
1331 char *delim;
1332 int ret = 0;
1333 u16 qid;
1334
1335 /* delim will point to queue id if successful */
1336 delim = strchr(newval->string, ':');
1337 if (!delim)
1338 goto err_no_cmd;
1339
1340 /* Terminate string that points to device name and bump it
1341 * up one, so we can read the queue id there.
1342 */
1343 *delim = '\0';
1344 if (sscanf(++delim, "%hd\n", &qid) != 1)
1345 goto err_no_cmd;
1346
1347 /* Check buffer length, valid ifname and queue id */
dingtianhongc313c892014-02-12 14:58:50 +08001348 if (!dev_valid_name(newval->string) ||
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001349 qid > bond->dev->real_num_tx_queues)
1350 goto err_no_cmd;
1351
1352 /* Get the pointer to that interface if it exists */
1353 sdev = __dev_get_by_name(dev_net(bond->dev), newval->string);
1354 if (!sdev)
1355 goto err_no_cmd;
1356
1357 /* Search for thes slave and check for duplicate qids */
1358 update_slave = NULL;
1359 bond_for_each_slave(bond, slave, iter) {
1360 if (sdev == slave->dev)
1361 /* We don't need to check the matching
1362 * slave for dups, since we're overwriting it
1363 */
1364 update_slave = slave;
1365 else if (qid && qid == slave->queue_id) {
1366 goto err_no_cmd;
1367 }
1368 }
1369
1370 if (!update_slave)
1371 goto err_no_cmd;
1372
1373 /* Actually set the qids for the slave */
1374 update_slave->queue_id = qid;
1375
1376out:
1377 return ret;
1378
1379err_no_cmd:
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001380 netdev_dbg(bond->dev, "invalid input for queue_id set\n");
Nikolay Aleksandrov24089ba2014-01-22 14:53:36 +01001381 ret = -EPERM;
1382 goto out;
1383
1384}
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001385
stephen hemmingerf3253332014-03-04 16:36:44 -08001386static int bond_option_slaves_set(struct bonding *bond,
stephen hemminger28f084c2014-03-06 14:20:17 -08001387 const struct bond_opt_value *newval)
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001388{
1389 char command[IFNAMSIZ + 1] = { 0, };
1390 struct net_device *dev;
1391 char *ifname;
1392 int ret;
1393
1394 sscanf(newval->string, "%16s", command); /* IFNAMSIZ*/
1395 ifname = command + 1;
1396 if ((strlen(command) <= 1) ||
Tonghao Zhang415d3942019-02-11 10:49:48 -08001397 (command[0] != '+' && command[0] != '-') ||
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001398 !dev_valid_name(ifname))
1399 goto err_no_cmd;
1400
1401 dev = __dev_get_by_name(dev_net(bond->dev), ifname);
1402 if (!dev) {
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001403 netdev_dbg(bond->dev, "interface %s does not exist!\n",
1404 ifname);
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001405 ret = -ENODEV;
1406 goto out;
1407 }
1408
1409 switch (command[0]) {
1410 case '+':
Jarod Wilsonf887e542019-06-07 10:59:32 -04001411 slave_dbg(bond->dev, dev, "Enslaving interface\n");
David Ahern33eaf2a2017-10-04 17:48:46 -07001412 ret = bond_enslave(bond->dev, dev, NULL);
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001413 break;
1414
1415 case '-':
Jarod Wilsonf887e542019-06-07 10:59:32 -04001416 slave_dbg(bond->dev, dev, "Releasing interface\n");
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001417 ret = bond_release(bond->dev, dev);
1418 break;
1419
1420 default:
Tonghao Zhang415d3942019-02-11 10:49:48 -08001421 /* should not run here. */
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001422 goto err_no_cmd;
1423 }
1424
1425out:
1426 return ret;
1427
1428err_no_cmd:
Veaceslav Falico2de390b2014-07-15 19:36:08 +02001429 netdev_err(bond->dev, "no command found in slaves file - use +ifname or -ifname\n");
Nikolay Aleksandrov0e2e5b62014-01-22 14:53:40 +01001430 ret = -EPERM;
1431 goto out;
1432}
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07001433
1434static int bond_option_tlb_dynamic_lb_set(struct bonding *bond,
1435 const struct bond_opt_value *newval)
1436{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001437 netdev_dbg(bond->dev, "Setting dynamic-lb to %s (%llu)\n",
1438 newval->string, newval->value);
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07001439 bond->params.tlb_dynamic_lb = newval->value;
1440
1441 return 0;
1442}
Mahesh Bandewar6791e462015-05-09 00:01:55 -07001443
1444static int bond_option_ad_actor_sys_prio_set(struct bonding *bond,
1445 const struct bond_opt_value *newval)
1446{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001447 netdev_dbg(bond->dev, "Setting ad_actor_sys_prio to %llu\n",
1448 newval->value);
Mahesh Bandewar6791e462015-05-09 00:01:55 -07001449
1450 bond->params.ad_actor_sys_prio = newval->value;
Nikolay Aleksandrov5ee14e62016-02-03 13:17:01 +01001451 bond_3ad_update_ad_actor_settings(bond);
1452
Mahesh Bandewar6791e462015-05-09 00:01:55 -07001453 return 0;
1454}
Mahesh Bandewar74514952015-05-09 00:01:56 -07001455
1456static int bond_option_ad_actor_system_set(struct bonding *bond,
1457 const struct bond_opt_value *newval)
1458{
1459 u8 macaddr[ETH_ALEN];
Andy Gospodarek171a42c2015-05-09 00:01:58 -07001460 u8 *mac;
Mahesh Bandewar74514952015-05-09 00:01:56 -07001461
Andy Gospodarek171a42c2015-05-09 00:01:58 -07001462 if (newval->string) {
Andy Shevchenkoce5c1442017-12-19 20:20:44 +02001463 if (!mac_pton(newval->string, macaddr))
Andy Gospodarek171a42c2015-05-09 00:01:58 -07001464 goto err;
1465 mac = macaddr;
1466 } else {
1467 mac = (u8 *)&newval->value;
Mahesh Bandewar74514952015-05-09 00:01:56 -07001468 }
1469
Andy Gospodarek171a42c2015-05-09 00:01:58 -07001470 if (!is_valid_ether_addr(mac))
1471 goto err;
1472
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001473 netdev_dbg(bond->dev, "Setting ad_actor_system to %pM\n", mac);
Andy Gospodarek171a42c2015-05-09 00:01:58 -07001474 ether_addr_copy(bond->params.ad_actor_system, mac);
Nikolay Aleksandrov5ee14e62016-02-03 13:17:01 +01001475 bond_3ad_update_ad_actor_settings(bond);
1476
Mahesh Bandewar74514952015-05-09 00:01:56 -07001477 return 0;
Andy Gospodarek171a42c2015-05-09 00:01:58 -07001478
1479err:
Jarod Wilsonf887e542019-06-07 10:59:32 -04001480 netdev_err(bond->dev, "Invalid ad_actor_system MAC address.\n");
Andy Gospodarek171a42c2015-05-09 00:01:58 -07001481 return -EINVAL;
Mahesh Bandewar74514952015-05-09 00:01:56 -07001482}
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07001483
1484static int bond_option_ad_user_port_key_set(struct bonding *bond,
1485 const struct bond_opt_value *newval)
1486{
Michael Dilmoreeac306b2017-06-26 16:49:46 +01001487 netdev_dbg(bond->dev, "Setting ad_user_port_key to %llu\n",
1488 newval->value);
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07001489
1490 bond->params.ad_user_port_key = newval->value;
1491 return 0;
1492}