blob: 238b56d77c369d9595d55bc681c2191c49dd2905 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * originally based on the dummy device.
3 *
4 * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
5 * Licensed under the GPL. Based on dummy.c, and eql.c devices.
6 *
7 * bonding.c: an Ethernet Bonding driver
8 *
9 * This is useful to talk to a Cisco EtherChannel compatible equipment:
10 * Cisco 5500
11 * Sun Trunking (Solaris)
12 * Alteon AceDirector Trunks
13 * Linux Bonding
14 * and probably many L2 switches ...
15 *
16 * How it works:
17 * ifconfig bond0 ipaddress netmask up
18 * will setup a network device, with an ip address. No mac address
19 * will be assigned at this time. The hw mac address will come from
20 * the first slave bonded to the channel. All slaves will then use
21 * this hw mac address.
22 *
23 * ifconfig bond0 down
24 * will release all slaves, marking them as down.
25 *
26 * ifenslave bond0 eth0
27 * will attach eth0 to bond0 as a slave. eth0 hw mac address will either
28 * a: be used as initial mac address
29 * b: if a hw mac address already is there, eth0's hw mac address
30 * will then be set from bond0.
31 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/kernel.h>
35#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/types.h>
37#include <linux/fcntl.h>
Jakub Kicinskib6459412021-12-28 16:49:13 -080038#include <linux/filter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/interrupt.h>
40#include <linux/ptrace.h>
41#include <linux/ioport.h>
42#include <linux/in.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040043#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/ip.h>
Matteo Crocedf98be02019-11-15 12:10:37 +010045#include <linux/icmp.h>
46#include <linux/icmpv6.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040047#include <linux/tcp.h>
48#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/slab.h>
50#include <linux/string.h>
51#include <linux/init.h>
52#include <linux/timer.h>
53#include <linux/socket.h>
54#include <linux/ctype.h>
55#include <linux/inet.h>
56#include <linux/bitops.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000057#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/dma.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/errno.h>
61#include <linux/netdevice.h>
62#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080063#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/etherdevice.h>
65#include <linux/skbuff.h>
66#include <net/sock.h>
67#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/smp.h>
69#include <linux/if_ether.h>
70#include <net/arp.h>
71#include <linux/mii.h>
72#include <linux/ethtool.h>
73#include <linux/if_vlan.h>
74#include <linux/if_bonding.h>
Hangbin Liu94dd0162021-11-30 15:09:32 +080075#include <linux/phy.h>
David Sterbab63bb732007-12-06 23:40:33 -080076#include <linux/jiffies.h>
Neil Hormane843fa52010-10-13 16:01:50 +000077#include <linux/preempt.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040078#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020079#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000080#include <net/netns/generic.h>
Eric Dumazet5ee31c682012-06-12 06:03:51 +000081#include <net/pkt_sched.h>
nikolay@redhat.com278b2082013-08-01 16:54:51 +020082#include <linux/rculist.h>
Jiri Pirko1bd758e2015-05-12 14:56:07 +020083#include <net/flow_dissector.h>
Jarod Wilson18cb2612020-06-19 10:31:55 -040084#include <net/xfrm.h>
David S. Miller1ef80192014-11-10 13:27:49 -050085#include <net/bonding.h>
86#include <net/bond_3ad.h>
87#include <net/bond_alb.h>
Tariq Toukan89df6a82021-01-17 16:59:46 +020088#if IS_ENABLED(CONFIG_TLS_DEVICE)
89#include <net/tls.h>
90#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Matan Barak73b5a6f22015-04-26 15:55:57 +030092#include "bonding_priv.h"
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*---------------------------- Module parameters ----------------------------*/
95
96/* monitor all links that often (in milliseconds). <=0 disables monitoring */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +000099static int tx_queues = BOND_DEFAULT_TX_QUEUES;
Ben Hutchingsad246c92011-04-26 15:25:52 +0000100static int num_peer_notif = 1;
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +0100101static int miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000102static int updelay;
103static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000105static char *mode;
106static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000107static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000108static char *lacp_rate;
stephen hemminger655f8912011-06-22 09:54:39 +0000109static int min_links;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000110static char *ad_select;
111static char *xmit_hash_policy;
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100112static int arp_interval;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000113static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
114static char *arp_validate;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200115static char *arp_all_targets;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000116static char *fail_over_mac;
dingtianhongb07ea072013-07-23 15:25:47 +0800117static int all_slaves_active;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000118static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000119static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +0100120static int packets_per_slave = 1;
dingtianhong3a7129e2013-12-21 14:40:12 +0800121static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123module_param(max_bonds, int, 0);
124MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000125module_param(tx_queues, int, 0);
126MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000127module_param_named(num_grat_arp, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000128MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
129 "failover event (alias of num_unsol_na)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000130module_param_named(num_unsol_na, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000131MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
132 "failover event (alias of num_grat_arp)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133module_param(miimon, int, 0);
134MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
135module_param(updelay, int, 0);
136MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
137module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800138MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
139 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800141MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
Debabrata Banerjeeb3c898e2018-05-16 14:02:13 -0400142 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143module_param(mode, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000144MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
Mitch Williams2ac47662005-11-09 10:35:03 -0800145 "1 for active-backup, 2 for balance-xor, "
146 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
147 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148module_param(primary, charp, 0);
149MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000150module_param(primary_reselect, charp, 0);
151MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
152 "once it comes up; "
153 "0 for always (default), "
154 "1 for only if speed of primary is "
155 "better, "
156 "2 for only on active slave "
157 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158module_param(lacp_rate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000159MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
160 "0 for slow, 1 for fast");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800161module_param(ad_select, charp, 0);
Zhu Yanjun0d039f32016-08-09 21:36:04 +0800162MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
Andy Gospodarek90e62472011-05-25 04:41:59 +0000163 "0 for stable (default), 1 for bandwidth, "
164 "2 for count");
stephen hemminger655f8912011-06-22 09:54:39 +0000165module_param(min_links, int, 0);
166MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
167
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400168module_param(xmit_hash_policy, charp, 0);
Debabrata Banerjeee79c1052018-05-14 14:48:09 -0400169MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
Andy Gospodarek90e62472011-05-25 04:41:59 +0000170 "0 for layer 2 (default), 1 for layer 3+4, "
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +0200171 "2 for layer 2+3, 3 for encap layer 2+3, "
Jarod Wilson7b8fc012021-01-18 20:09:27 -0500172 "4 for encap layer 3+4, 5 for vlan+srcmac");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173module_param(arp_interval, int, 0);
174MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
175module_param_array(arp_ip_target, charp, NULL, 0);
176MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700177module_param(arp_validate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000178MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
179 "0 for none (default), 1 for active, "
180 "2 for backup, 3 for all");
Veaceslav Falico8599b522013-06-24 11:49:34 +0200181module_param(arp_all_targets, charp, 0);
182MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700183module_param(fail_over_mac, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000184MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
185 "the same MAC; 0 for none (default), "
186 "1 for active, 2 for follow");
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000187module_param(all_slaves_active, int, 0);
Masanari Iida37b70212014-09-09 18:07:55 +0900188MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
Andy Gospodarek90e62472011-05-25 04:41:59 +0000189 "by setting active flag for all slaves; "
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000190 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000191module_param(resend_igmp, int, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000192MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
193 "link failure");
Nikolay Aleksandrov73958322013-11-05 13:51:41 +0100194module_param(packets_per_slave, int, 0);
195MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
196 "mode; 0 for a random slave, 1 packet per "
197 "slave (default), >1 packets per slave.");
dingtianhong3a7129e2013-12-21 14:40:12 +0800198module_param(lp_interval, uint, 0);
199MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
200 "the bonding driver sends learning packets to "
201 "each slaves peer switch. The default is 1.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/*----------------------------- Global variables ----------------------------*/
204
Neil Hormane843fa52010-10-13 16:01:50 +0000205#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000206atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000207#endif
208
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300209unsigned int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Matteo Croce58deb772019-10-29 14:50:53 +0100211static const struct flow_dissector_key flow_keys_bonding_keys[] = {
212 {
213 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
214 .offset = offsetof(struct flow_keys, control),
215 },
216 {
217 .key_id = FLOW_DISSECTOR_KEY_BASIC,
218 .offset = offsetof(struct flow_keys, basic),
219 },
220 {
221 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
222 .offset = offsetof(struct flow_keys, addrs.v4addrs),
223 },
224 {
225 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
226 .offset = offsetof(struct flow_keys, addrs.v6addrs),
227 },
228 {
229 .key_id = FLOW_DISSECTOR_KEY_TIPC,
230 .offset = offsetof(struct flow_keys, addrs.tipckey),
231 },
232 {
233 .key_id = FLOW_DISSECTOR_KEY_PORTS,
234 .offset = offsetof(struct flow_keys, ports),
235 },
236 {
237 .key_id = FLOW_DISSECTOR_KEY_ICMP,
238 .offset = offsetof(struct flow_keys, icmp),
239 },
240 {
241 .key_id = FLOW_DISSECTOR_KEY_VLAN,
242 .offset = offsetof(struct flow_keys, vlan),
243 },
244 {
245 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
246 .offset = offsetof(struct flow_keys, tags),
247 },
248 {
249 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
250 .offset = offsetof(struct flow_keys, keyid),
251 },
252};
253
254static struct flow_dissector flow_keys_bonding __read_mostly;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*-------------------------- Forward declarations ---------------------------*/
257
Stephen Hemminger181470f2009-06-12 19:02:52 +0000258static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000259static void bond_uninit(struct net_device *bond_dev);
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800260static void bond_get_stats(struct net_device *bond_dev,
261 struct rtnl_link_stats64 *stats);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700262static void bond_slave_arr_handler(struct work_struct *work);
Jay Vosburgh21a75f02016-02-02 13:35:56 -0800263static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
264 int mod);
Mahesh Bandeward4859d72018-09-24 14:40:11 -0700265static void bond_netdev_notify_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267/*---------------------------- General routines -----------------------------*/
268
Amerigo Wangbd33acc2011-03-06 21:58:46 +0000269const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800271 static const char *names[] = {
272 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
273 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
274 [BOND_MODE_XOR] = "load balancing (xor)",
275 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000276 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800277 [BOND_MODE_TLB] = "transmit load balancing",
278 [BOND_MODE_ALB] = "adaptive load balancing",
279 };
280
Wang Sheng-Huif5280942013-07-24 14:53:26 +0800281 if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800283
284 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000289 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * @bond: bond device that got this skb for tx.
291 * @skb: hw accel VLAN tagged skb to transmit
292 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
Eric Dumazetae46f1842020-05-07 09:32:22 -0700294netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000295 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Ben Hutchings83874512010-12-13 08:19:28 +0000297 skb->dev = slave_dev;
Neil Horman374eeb52011-06-03 10:35:52 +0000298
Eric Dumazet5ee31c682012-06-12 06:03:51 +0000299 BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
Jiri Pirkodf4ab5b2012-07-20 02:28:49 +0000300 sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
Tonghao Zhangae35c6f2018-05-11 02:53:11 -0700301 skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
Neil Horman374eeb52011-06-03 10:35:52 +0000302
Amerigo Wange15c3c222012-08-10 01:24:45 +0000303 if (unlikely(netpoll_tx_running(bond->dev)))
Eric Dumazetae46f1842020-05-07 09:32:22 -0700304 return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
305
306 return dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Tariq Toukan007feb82021-01-17 16:59:44 +0200309bool bond_sk_check(struct bonding *bond)
310{
311 switch (BOND_MODE(bond)) {
312 case BOND_MODE_8023AD:
313 case BOND_MODE_XOR:
314 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
315 return true;
316 fallthrough;
317 default:
318 return false;
319 }
320}
321
Jussi Maki9e2ee5c2021-07-31 05:57:34 +0000322static bool bond_xdp_check(struct bonding *bond)
323{
324 switch (BOND_MODE(bond)) {
325 case BOND_MODE_ROUNDROBIN:
326 case BOND_MODE_ACTIVEBACKUP:
Jussi Maki39a08762021-08-12 14:52:41 +0000327 return true;
Jussi Maki9e2ee5c2021-07-31 05:57:34 +0000328 case BOND_MODE_8023AD:
329 case BOND_MODE_XOR:
Jussi Maki39a08762021-08-12 14:52:41 +0000330 /* vlan+srcmac is not supported with XDP as in most cases the 802.1q
331 * payload is not in the packet due to hardware offload.
332 */
333 if (bond->params.xmit_policy != BOND_XMIT_POLICY_VLAN_SRCMAC)
334 return true;
335 fallthrough;
Jussi Maki9e2ee5c2021-07-31 05:57:34 +0000336 default:
337 return false;
338 }
339}
340
Jarod Wilson18cb2612020-06-19 10:31:55 -0400341/*---------------------------------- VLAN -----------------------------------*/
342
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200343/* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000344 * We don't protect the slave list iteration with a lock because:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * a. This operation is performed in IOCTL context,
346 * b. The operation is protected by the RTNL semaphore in the 8021q code,
347 * c. Holding a lock with BH disabled while directly calling a base driver
348 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000349 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * The design of synchronization/protection for this operation in the 8021q
351 * module is good for one or more VLAN devices over a single physical device
352 * and cannot be extended for a teaming solution like bonding, so there is a
353 * potential race condition here where a net device from the vlan group might
354 * be referenced (either by a base driver or the 8021q code) while it is being
355 * removed from the system. However, it turns out we're not making matters
356 * worse, and if it works for regular VLAN usage it will work here too.
357*/
358
359/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
361 * @bond_dev: bonding net device that got called
Lee Jones45a15532020-08-14 12:39:05 +0100362 * @proto: network protocol ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * @vid: vlan id being added
364 */
Patrick McHardy80d5c362013-04-19 02:04:28 +0000365static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
366 __be16 proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Wang Chen454d7c92008-11-12 23:37:49 -0800368 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico81f23b12013-09-25 09:20:13 +0200369 struct slave *slave, *rollback_slave;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200370 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200371 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200373 bond_for_each_slave(bond, slave, iter) {
Patrick McHardy80d5c362013-04-19 02:04:28 +0000374 res = vlan_vid_add(slave->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +0000375 if (res)
376 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
Jiri Pirko8e586132011-12-08 19:52:37 -0500379 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +0000380
381unwind:
Veaceslav Falico81f23b12013-09-25 09:20:13 +0200382 /* unwind to the slave that failed */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200383 bond_for_each_slave(bond, rollback_slave, iter) {
Veaceslav Falico81f23b12013-09-25 09:20:13 +0200384 if (rollback_slave == slave)
385 break;
386
387 vlan_vid_del(rollback_slave->dev, proto, vid);
388 }
Jiri Pirko87002b02011-12-08 04:11:17 +0000389
390 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393/**
394 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
395 * @bond_dev: bonding net device that got called
Lee Jones45a15532020-08-14 12:39:05 +0100396 * @proto: network protocol ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * @vid: vlan id being removed
398 */
Patrick McHardy80d5c362013-04-19 02:04:28 +0000399static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
400 __be16 proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Wang Chen454d7c92008-11-12 23:37:49 -0800402 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200403 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200406 bond_for_each_slave(bond, slave, iter)
Patrick McHardy80d5c362013-04-19 02:04:28 +0000407 vlan_vid_del(slave->dev, proto, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Veaceslav Falicoe868b0c2013-08-28 23:25:15 +0200409 if (bond_is_lb(bond))
410 bond_alb_clear_vlan(bond, vid);
Jiri Pirko8e586132011-12-08 19:52:37 -0500411
412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Jarod Wilson18cb2612020-06-19 10:31:55 -0400415/*---------------------------------- XFRM -----------------------------------*/
416
417#ifdef CONFIG_XFRM_OFFLOAD
418/**
419 * bond_ipsec_add_sa - program device with a security association
420 * @xs: pointer to transformer state struct
421 **/
422static int bond_ipsec_add_sa(struct xfrm_state *xs)
423{
424 struct net_device *bond_dev = xs->xso.dev;
Taehee Yoo9a560552021-07-05 15:38:12 +0000425 struct bond_ipsec *ipsec;
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400426 struct bonding *bond;
427 struct slave *slave;
Taehee Yoob648eba2021-07-05 15:38:06 +0000428 int err;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400429
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400430 if (!bond_dev)
431 return -EINVAL;
432
Taehee Yoob648eba2021-07-05 15:38:06 +0000433 rcu_read_lock();
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400434 bond = netdev_priv(bond_dev);
Jarod Wilsonf548a472020-07-08 18:58:49 -0400435 slave = rcu_dereference(bond->curr_active_slave);
Taehee Yoo105cd172021-07-05 15:38:07 +0000436 if (!slave) {
437 rcu_read_unlock();
438 return -ENODEV;
439 }
440
Taehee Yoob1216932021-07-05 15:38:11 +0000441 if (!slave->dev->xfrmdev_ops ||
442 !slave->dev->xfrmdev_ops->xdo_dev_state_add ||
443 netif_is_bond_master(slave->dev)) {
Jarod Wilson18cb2612020-06-19 10:31:55 -0400444 slave_warn(bond_dev, slave->dev, "Slave does not support ipsec offload\n");
Taehee Yoob648eba2021-07-05 15:38:06 +0000445 rcu_read_unlock();
Jarod Wilson18cb2612020-06-19 10:31:55 -0400446 return -EINVAL;
447 }
448
Taehee Yoo9a560552021-07-05 15:38:12 +0000449 ipsec = kmalloc(sizeof(*ipsec), GFP_ATOMIC);
450 if (!ipsec) {
451 rcu_read_unlock();
452 return -ENOMEM;
453 }
454 xs->xso.real_dev = slave->dev;
455
Taehee Yoob648eba2021-07-05 15:38:06 +0000456 err = slave->dev->xfrmdev_ops->xdo_dev_state_add(xs);
Taehee Yoo9a560552021-07-05 15:38:12 +0000457 if (!err) {
458 ipsec->xs = xs;
459 INIT_LIST_HEAD(&ipsec->list);
460 spin_lock_bh(&bond->ipsec_lock);
461 list_add(&ipsec->list, &bond->ipsec_list);
462 spin_unlock_bh(&bond->ipsec_lock);
463 } else {
464 kfree(ipsec);
465 }
Taehee Yoob648eba2021-07-05 15:38:06 +0000466 rcu_read_unlock();
467 return err;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400468}
469
Taehee Yoo9a560552021-07-05 15:38:12 +0000470static void bond_ipsec_add_sa_all(struct bonding *bond)
471{
472 struct net_device *bond_dev = bond->dev;
473 struct bond_ipsec *ipsec;
474 struct slave *slave;
475
476 rcu_read_lock();
477 slave = rcu_dereference(bond->curr_active_slave);
478 if (!slave)
479 goto out;
480
481 if (!slave->dev->xfrmdev_ops ||
482 !slave->dev->xfrmdev_ops->xdo_dev_state_add ||
483 netif_is_bond_master(slave->dev)) {
484 spin_lock_bh(&bond->ipsec_lock);
485 if (!list_empty(&bond->ipsec_list))
486 slave_warn(bond_dev, slave->dev,
487 "%s: no slave xdo_dev_state_add\n",
488 __func__);
489 spin_unlock_bh(&bond->ipsec_lock);
490 goto out;
491 }
492
493 spin_lock_bh(&bond->ipsec_lock);
494 list_for_each_entry(ipsec, &bond->ipsec_list, list) {
495 ipsec->xs->xso.real_dev = slave->dev;
496 if (slave->dev->xfrmdev_ops->xdo_dev_state_add(ipsec->xs)) {
497 slave_warn(bond_dev, slave->dev, "%s: failed to add SA\n", __func__);
498 ipsec->xs->xso.real_dev = NULL;
499 }
500 }
501 spin_unlock_bh(&bond->ipsec_lock);
502out:
503 rcu_read_unlock();
504}
505
Jarod Wilson18cb2612020-06-19 10:31:55 -0400506/**
507 * bond_ipsec_del_sa - clear out this specific SA
508 * @xs: pointer to transformer state struct
509 **/
510static void bond_ipsec_del_sa(struct xfrm_state *xs)
511{
512 struct net_device *bond_dev = xs->xso.dev;
Taehee Yoo9a560552021-07-05 15:38:12 +0000513 struct bond_ipsec *ipsec;
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400514 struct bonding *bond;
515 struct slave *slave;
516
517 if (!bond_dev)
518 return;
519
Taehee Yooa22c39b2021-07-05 15:38:10 +0000520 rcu_read_lock();
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400521 bond = netdev_priv(bond_dev);
Jarod Wilsonf548a472020-07-08 18:58:49 -0400522 slave = rcu_dereference(bond->curr_active_slave);
Jarod Wilson18cb2612020-06-19 10:31:55 -0400523
524 if (!slave)
Taehee Yooa22c39b2021-07-05 15:38:10 +0000525 goto out;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400526
Taehee Yoo9a560552021-07-05 15:38:12 +0000527 if (!xs->xso.real_dev)
528 goto out;
529
530 WARN_ON(xs->xso.real_dev != slave->dev);
Jarod Wilson18cb2612020-06-19 10:31:55 -0400531
Taehee Yoob1216932021-07-05 15:38:11 +0000532 if (!slave->dev->xfrmdev_ops ||
533 !slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
534 netif_is_bond_master(slave->dev)) {
Jarod Wilson18cb2612020-06-19 10:31:55 -0400535 slave_warn(bond_dev, slave->dev, "%s: no slave xdo_dev_state_delete\n", __func__);
Taehee Yooa22c39b2021-07-05 15:38:10 +0000536 goto out;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400537 }
538
539 slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs);
Taehee Yooa22c39b2021-07-05 15:38:10 +0000540out:
Taehee Yoo9a560552021-07-05 15:38:12 +0000541 spin_lock_bh(&bond->ipsec_lock);
542 list_for_each_entry(ipsec, &bond->ipsec_list, list) {
543 if (ipsec->xs == xs) {
544 list_del(&ipsec->list);
545 kfree(ipsec);
546 break;
547 }
548 }
549 spin_unlock_bh(&bond->ipsec_lock);
550 rcu_read_unlock();
551}
552
553static void bond_ipsec_del_sa_all(struct bonding *bond)
554{
555 struct net_device *bond_dev = bond->dev;
556 struct bond_ipsec *ipsec;
557 struct slave *slave;
558
559 rcu_read_lock();
560 slave = rcu_dereference(bond->curr_active_slave);
561 if (!slave) {
562 rcu_read_unlock();
563 return;
564 }
565
566 spin_lock_bh(&bond->ipsec_lock);
567 list_for_each_entry(ipsec, &bond->ipsec_list, list) {
568 if (!ipsec->xs->xso.real_dev)
569 continue;
570
571 if (!slave->dev->xfrmdev_ops ||
572 !slave->dev->xfrmdev_ops->xdo_dev_state_delete ||
573 netif_is_bond_master(slave->dev)) {
574 slave_warn(bond_dev, slave->dev,
575 "%s: no slave xdo_dev_state_delete\n",
576 __func__);
577 } else {
578 slave->dev->xfrmdev_ops->xdo_dev_state_delete(ipsec->xs);
579 }
580 ipsec->xs->xso.real_dev = NULL;
581 }
582 spin_unlock_bh(&bond->ipsec_lock);
Taehee Yooa22c39b2021-07-05 15:38:10 +0000583 rcu_read_unlock();
Jarod Wilson18cb2612020-06-19 10:31:55 -0400584}
585
586/**
587 * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
588 * @skb: current data packet
589 * @xs: pointer to transformer state struct
590 **/
591static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
592{
593 struct net_device *bond_dev = xs->xso.dev;
Taehee Yoo9a560552021-07-05 15:38:12 +0000594 struct net_device *real_dev;
595 struct slave *curr_active;
596 struct bonding *bond;
Taehee Yoo955b7852021-07-05 15:38:13 +0000597 int err;
Taehee Yoo9a560552021-07-05 15:38:12 +0000598
599 bond = netdev_priv(bond_dev);
Taehee Yoo955b7852021-07-05 15:38:13 +0000600 rcu_read_lock();
Taehee Yoo9a560552021-07-05 15:38:12 +0000601 curr_active = rcu_dereference(bond->curr_active_slave);
602 real_dev = curr_active->dev;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400603
Taehee Yoo955b7852021-07-05 15:38:13 +0000604 if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Taehee Yoo168e6962021-07-05 15:38:14 +0000605 err = false;
Taehee Yoo955b7852021-07-05 15:38:13 +0000606 goto out;
607 }
Jarod Wilsona3b658c2020-06-30 14:49:41 -0400608
Taehee Yoo955b7852021-07-05 15:38:13 +0000609 if (!xs->xso.real_dev) {
610 err = false;
611 goto out;
612 }
Taehee Yoo9a560552021-07-05 15:38:12 +0000613
614 if (!real_dev->xfrmdev_ops ||
615 !real_dev->xfrmdev_ops->xdo_dev_offload_ok ||
616 netif_is_bond_master(real_dev)) {
Taehee Yoo955b7852021-07-05 15:38:13 +0000617 err = false;
618 goto out;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400619 }
620
Taehee Yoo955b7852021-07-05 15:38:13 +0000621 err = real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
622out:
623 rcu_read_unlock();
624 return err;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400625}
626
627static const struct xfrmdev_ops bond_xfrmdev_ops = {
628 .xdo_dev_state_add = bond_ipsec_add_sa,
629 .xdo_dev_state_delete = bond_ipsec_del_sa,
630 .xdo_dev_offload_ok = bond_ipsec_offload_ok,
631};
632#endif /* CONFIG_XFRM_OFFLOAD */
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*------------------------------- Link status -------------------------------*/
635
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200636/* Set the carrier state for the master according to the state of its
Jay Vosburghff59c452006-03-27 13:27:43 -0800637 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
638 * do special 802.3ad magic.
639 *
640 * Returns zero if carrier state does not change, nonzero if it does.
641 */
Jonathan Toppins2477bc92015-01-26 01:16:57 -0500642int bond_set_carrier(struct bonding *bond)
Jay Vosburghff59c452006-03-27 13:27:43 -0800643{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200644 struct list_head *iter;
Jay Vosburghff59c452006-03-27 13:27:43 -0800645 struct slave *slave;
Jay Vosburghff59c452006-03-27 13:27:43 -0800646
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200647 if (!bond_has_slaves(bond))
Jay Vosburghff59c452006-03-27 13:27:43 -0800648 goto down;
649
Veaceslav Falico01844092014-05-15 21:39:55 +0200650 if (BOND_MODE(bond) == BOND_MODE_8023AD)
Jay Vosburghff59c452006-03-27 13:27:43 -0800651 return bond_3ad_set_carrier(bond);
652
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200653 bond_for_each_slave(bond, slave, iter) {
Jay Vosburghff59c452006-03-27 13:27:43 -0800654 if (slave->link == BOND_LINK_UP) {
655 if (!netif_carrier_ok(bond->dev)) {
656 netif_carrier_on(bond->dev);
657 return 1;
658 }
659 return 0;
660 }
661 }
662
663down:
664 if (netif_carrier_ok(bond->dev)) {
665 netif_carrier_off(bond->dev);
666 return 1;
667 }
668 return 0;
669}
670
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200671/* Get link speed and duplex from the slave's base driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * using ethtool. If for some reason the call fails or the
Weiping Pan98f41f62011-10-31 17:20:48 +0000673 * values are invalid, set speed and duplex to -1,
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700674 * and return. Return 1 if speed or duplex settings are
675 * UNKNOWN; 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700677static int bond_update_speed_duplex(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct net_device *slave_dev = slave->dev;
David Decotigny98569092016-02-24 10:58:02 -0800680 struct ethtool_link_ksettings ecmd;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700681 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dan Carpenter589665f2011-11-04 08:21:38 +0000683 slave->speed = SPEED_UNKNOWN;
684 slave->duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
David Decotigny98569092016-02-24 10:58:02 -0800686 res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700687 if (res < 0)
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700688 return 1;
David Decotigny98569092016-02-24 10:58:02 -0800689 if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700690 return 1;
David Decotigny98569092016-02-24 10:58:02 -0800691 switch (ecmd.base.duplex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 case DUPLEX_FULL:
693 case DUPLEX_HALF:
694 break;
695 default:
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700696 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
David Decotigny98569092016-02-24 10:58:02 -0800699 slave->speed = ecmd.base.speed;
700 slave->duplex = ecmd.base.duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -0800705const char *bond_slave_link_status(s8 link)
706{
707 switch (link) {
708 case BOND_LINK_UP:
709 return "up";
710 case BOND_LINK_FAIL:
711 return "going down";
712 case BOND_LINK_DOWN:
713 return "down";
714 case BOND_LINK_BACK:
715 return "going back";
716 default:
717 return "unknown";
718 }
719}
720
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200721/* if <dev> supports MII link status reporting, check its link status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 *
723 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000724 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 *
726 * Return either BMSR_LSTATUS, meaning that the link is up (or we
727 * can't tell and just pretend it is), or 0, meaning that the link is
728 * down.
729 *
730 * If reporting is non-zero, instead of faking link up, return -1 if
731 * both ETHTOOL and MII ioctls fail (meaning the device does not
732 * support them). If use_carrier is set, return whatever it says.
733 * It'd be nice if there was a good way to tell if a driver supports
734 * netif_carrier, but there really isn't.
735 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000736static int bond_check_dev_link(struct bonding *bond,
737 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800739 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700740 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct ifreq ifr;
742 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Petri Gynther6c988852009-08-28 12:05:15 +0000744 if (!reporting && !netif_running(slave_dev))
745 return 0;
746
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800747 if (bond->params.use_carrier)
Debabrata Banerjeeb3c898e2018-05-16 14:02:13 -0400748 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Jiri Pirko29112f42009-04-24 01:58:23 +0000750 /* Try to get link status using Ethtool first. */
Ben Hutchingsc772dde2012-12-07 06:15:32 +0000751 if (slave_dev->ethtool_ops->get_link)
752 return slave_dev->ethtool_ops->get_link(slave_dev) ?
753 BMSR_LSTATUS : 0;
Jiri Pirko29112f42009-04-24 01:58:23 +0000754
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000755 /* Ethtool can't be used, fallback to MII ioctls. */
Arnd Bergmanna7605372021-07-27 15:45:13 +0200756 ioctl = slave_ops->ndo_eth_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (ioctl) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200758 /* TODO: set pointer to correct ioctl on a per team member
759 * bases to make this more efficient. that is, once
760 * we determine the correct ioctl, we will always
761 * call it and not the others for that team
762 * member.
763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200765 /* We cannot assume that SIOCGMIIPHY will also read a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 * register; not all network drivers (e.g., e100)
767 * support that.
768 */
769
770 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
Kees Cook43902072021-06-02 13:58:20 -0700771 strscpy_pad(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 mii = if_mii(&ifr);
Al Viro4ad41c12016-09-03 19:37:25 -0400773 if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 mii->reg_num = MII_BMSR;
Al Viro4ad41c12016-09-03 19:37:25 -0400775 if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0)
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000776 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778 }
779
Arnd Bergmanna7605372021-07-27 15:45:13 +0200780 /* If reporting, report that either there's no ndo_eth_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700781 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * cannot report link status). If not reporting, pretend
783 * we're ok.
784 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000785 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788/*----------------------------- Multicast list ------------------------------*/
789
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200790/* Push the promiscuity flag down to appropriate slaves */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700791static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200793 struct list_head *iter;
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700794 int err = 0;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200795
Veaceslav Falicoec0865a2014-05-15 21:39:54 +0200796 if (bond_uses_primary(bond)) {
Veaceslav Falico14056e72014-07-16 18:32:01 +0200797 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
Eric Dumazet4740d632014-07-15 06:56:55 -0700798
Eric Dumazet4740d632014-07-15 06:56:55 -0700799 if (curr_active)
800 err = dev_set_promiscuity(curr_active->dev, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 } else {
802 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200803
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200804 bond_for_each_slave(bond, slave, iter) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700805 err = dev_set_promiscuity(slave->dev, inc);
806 if (err)
807 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200813/* Push the allmulti flag down to all slaves */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700814static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200816 struct list_head *iter;
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700817 int err = 0;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200818
Veaceslav Falicoec0865a2014-05-15 21:39:54 +0200819 if (bond_uses_primary(bond)) {
Veaceslav Falico14056e72014-07-16 18:32:01 +0200820 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
Eric Dumazet4740d632014-07-15 06:56:55 -0700821
Eric Dumazet4740d632014-07-15 06:56:55 -0700822 if (curr_active)
823 err = dev_set_allmulti(curr_active->dev, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 } else {
825 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200826
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200827 bond_for_each_slave(bond, slave, iter) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700828 err = dev_set_allmulti(slave->dev, inc);
829 if (err)
830 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700833 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200836/* Retrieve the list of registered multicast addresses for the bonding
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800837 * device and retransmit an IGMP JOIN request to the current active
838 * slave.
839 */
stephen hemminger379b7382010-10-15 11:02:56 +0000840static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000841{
842 struct bonding *bond = container_of(work, struct bonding,
Flavio Leitner94265cf2011-05-25 08:38:58 +0000843 mcast_work.work);
Veaceslav Falicoad999ee2013-03-26 04:10:02 +0000844
dingtianhongf2369102013-12-13 10:20:26 +0800845 if (!rtnl_trylock()) {
846 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
847 return;
848 }
849 call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
850
851 if (bond->igmp_retrans > 1) {
852 bond->igmp_retrans--;
853 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
854 }
855 rtnl_unlock();
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800856}
857
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200858/* Flush bond's hardware addresses from slave */
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000859static void bond_hw_addr_flush(struct net_device *bond_dev,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000860 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Wang Chen454d7c92008-11-12 23:37:49 -0800862 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000864 dev_uc_unsync(slave_dev, bond_dev);
865 dev_mc_unsync(slave_dev, bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Veaceslav Falico01844092014-05-15 21:39:55 +0200867 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* del lacpdu mc addr from mc list */
869 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
870
Jiri Pirko22bedad32010-04-01 21:22:57 +0000871 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873}
874
875/*--------------------------- Active slave change ---------------------------*/
876
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000877/* Update the hardware address list and promisc/allmulti for the new and
Veaceslav Falicoec0865a2014-05-15 21:39:54 +0200878 * old active slaves (if any). Modes that are not using primary keep all
879 * slaves up date at all times; only the modes that use primary need to call
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000880 * this function to swap these settings during a failover.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000882static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
883 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000886 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000889 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000892 bond_hw_addr_flush(bond->dev, old_active->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
895 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700896 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000897 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000900 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
nikolay@redhat.comd632ce92013-04-18 07:33:38 +0000903 netif_addr_lock_bh(bond->dev);
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000904 dev_uc_sync(new_active->dev, bond->dev);
905 dev_mc_sync(new_active->dev, bond->dev);
nikolay@redhat.comd632ce92013-04-18 07:33:38 +0000906 netif_addr_unlock_bh(bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908}
909
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200910/**
911 * bond_set_dev_addr - clone slave's address to bond
912 * @bond_dev: bond net device
913 * @slave_dev: slave net device
914 *
915 * Should be called with RTNL held.
916 */
Petr Machatab9245912018-12-13 11:54:44 +0000917static int bond_set_dev_addr(struct net_device *bond_dev,
918 struct net_device *slave_dev)
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200919{
Petr Machata1caf40d2018-12-13 11:54:46 +0000920 int err;
921
Jarod Wilsone2a74202019-06-07 10:59:29 -0400922 slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
923 bond_dev, slave_dev, slave_dev->addr_len);
Petr Machata1caf40d2018-12-13 11:54:46 +0000924 err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
925 if (err)
926 return err;
927
Jakub Kicinski6f238102021-10-22 16:20:59 -0700928 __dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len);
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200929 bond_dev->addr_assign_type = NET_ADDR_STOLEN;
930 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
Petr Machatab9245912018-12-13 11:54:44 +0000931 return 0;
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200932}
933
dingtianhonga951bc12015-07-16 16:30:02 +0800934static struct slave *bond_get_old_active(struct bonding *bond,
935 struct slave *new_active)
936{
937 struct slave *slave;
938 struct list_head *iter;
939
940 bond_for_each_slave(bond, slave, iter) {
941 if (slave == new_active)
942 continue;
943
944 if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
945 return slave;
946 }
947
948 return NULL;
949}
950
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200951/* bond_do_fail_over_mac
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700952 *
953 * Perform special MAC address swapping for fail_over_mac settings
954 *
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200955 * Called with RTNL
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700956 */
957static void bond_do_fail_over_mac(struct bonding *bond,
958 struct slave *new_active,
959 struct slave *old_active)
960{
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400961 u8 tmp_mac[MAX_ADDR_LEN];
962 struct sockaddr_storage ss;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700963 int rv;
964
965 switch (bond->params.fail_over_mac) {
966 case BOND_FOM_ACTIVE:
Petr Machatab9245912018-12-13 11:54:44 +0000967 if (new_active) {
968 rv = bond_set_dev_addr(bond->dev, new_active->dev);
969 if (rv)
Jarod Wilsone2a74202019-06-07 10:59:29 -0400970 slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
971 -rv);
Petr Machatab9245912018-12-13 11:54:44 +0000972 }
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700973 break;
974 case BOND_FOM_FOLLOW:
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200975 /* if new_active && old_active, swap them
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700976 * if just old_active, do nothing (going to no active slave)
977 * if just new_active, set new_active to bond's MAC
978 */
979 if (!new_active)
980 return;
981
dingtianhonga951bc12015-07-16 16:30:02 +0800982 if (!old_active)
983 old_active = bond_get_old_active(bond, new_active);
984
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700985 if (old_active) {
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400986 bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
987 new_active->dev->addr_len);
988 bond_hw_addr_copy(ss.__data,
989 old_active->dev->dev_addr,
990 old_active->dev->addr_len);
991 ss.ss_family = new_active->dev->type;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700992 } else {
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400993 bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
994 bond->dev->addr_len);
995 ss.ss_family = bond->dev->type;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700996 }
997
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400998 rv = dev_set_mac_address(new_active->dev,
Petr Machata3a37a962018-12-13 11:54:30 +0000999 (struct sockaddr *)&ss, NULL);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001000 if (rv) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001001 slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
1002 -rv);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001003 goto out;
1004 }
1005
1006 if (!old_active)
1007 goto out;
1008
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001009 bond_hw_addr_copy(ss.__data, tmp_mac,
1010 new_active->dev->addr_len);
1011 ss.ss_family = old_active->dev->type;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001012
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001013 rv = dev_set_mac_address(old_active->dev,
Petr Machata3a37a962018-12-13 11:54:30 +00001014 (struct sockaddr *)&ss, NULL);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001015 if (rv)
Jarod Wilsone2a74202019-06-07 10:59:29 -04001016 slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
1017 -rv);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001018out:
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001019 break;
1020 default:
Veaceslav Falico76444f52014-07-15 19:35:58 +02001021 netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
1022 bond->params.fail_over_mac);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001023 break;
1024 }
1025
1026}
1027
Mazhar Ranab5a983f2015-07-07 15:04:50 +05301028static struct slave *bond_choose_primary_or_current(struct bonding *bond)
Jiri Pirkoa5499522009-09-25 03:28:09 +00001029{
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001030 struct slave *prim = rtnl_dereference(bond->primary_slave);
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02001031 struct slave *curr = rtnl_dereference(bond->curr_active_slave);
Jiri Pirkoa5499522009-09-25 03:28:09 +00001032
Mazhar Ranab5a983f2015-07-07 15:04:50 +05301033 if (!prim || prim->link != BOND_LINK_UP) {
1034 if (!curr || curr->link != BOND_LINK_UP)
1035 return NULL;
1036 return curr;
1037 }
1038
Jiri Pirkoa5499522009-09-25 03:28:09 +00001039 if (bond->force_primary) {
1040 bond->force_primary = false;
Mazhar Ranab5a983f2015-07-07 15:04:50 +05301041 return prim;
Jiri Pirkoa5499522009-09-25 03:28:09 +00001042 }
Mazhar Ranab5a983f2015-07-07 15:04:50 +05301043
1044 if (!curr || curr->link != BOND_LINK_UP)
1045 return prim;
1046
1047 /* At this point, prim and curr are both up */
1048 switch (bond->params.primary_reselect) {
1049 case BOND_PRI_RESELECT_ALWAYS:
1050 return prim;
1051 case BOND_PRI_RESELECT_BETTER:
1052 if (prim->speed < curr->speed)
1053 return curr;
1054 if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
1055 return curr;
1056 return prim;
1057 case BOND_PRI_RESELECT_FAILURE:
1058 return curr;
1059 default:
1060 netdev_err(bond->dev, "impossible primary_reselect %d\n",
1061 bond->params.primary_reselect);
1062 return curr;
1063 }
Jiri Pirkoa5499522009-09-25 03:28:09 +00001064}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/**
Mazhar Ranab5a983f2015-07-07 15:04:50 +05301067 * bond_find_best_slave - select the best available slave to be the active one
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * @bond: our bonding struct
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
1070static struct slave *bond_find_best_slave(struct bonding *bond)
1071{
Mazhar Ranab5a983f2015-07-07 15:04:50 +05301072 struct slave *slave, *bestslave = NULL;
Veaceslav Falico77140d22013-09-25 09:20:18 +02001073 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 int mintime = bond->params.updelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Mazhar Ranab5a983f2015-07-07 15:04:50 +05301076 slave = bond_choose_primary_or_current(bond);
1077 if (slave)
1078 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Veaceslav Falico77140d22013-09-25 09:20:18 +02001080 bond_for_each_slave(bond, slave, iter) {
1081 if (slave->link == BOND_LINK_UP)
1082 return slave;
Veaceslav Falicob6adc612014-05-15 21:39:57 +02001083 if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
Veaceslav Falico77140d22013-09-25 09:20:18 +02001084 slave->delay < mintime) {
1085 mintime = slave->delay;
1086 bestslave = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088 }
1089
1090 return bestslave;
1091}
1092
Ben Hutchingsad246c92011-04-26 15:25:52 +00001093static bool bond_should_notify_peers(struct bonding *bond)
1094{
dingtianhong4cb4f972013-12-13 10:19:39 +08001095 struct slave *slave;
1096
1097 rcu_read_lock();
1098 slave = rcu_dereference(bond->curr_active_slave);
1099 rcu_read_unlock();
Ben Hutchingsad246c92011-04-26 15:25:52 +00001100
Ben Hutchingsad246c92011-04-26 15:25:52 +00001101 if (!slave || !bond->send_peer_notif ||
Vincent Bernat07a4dde2019-07-02 19:43:54 +02001102 bond->send_peer_notif %
1103 max(1, bond->params.peer_notif_delay) != 0 ||
Venkat Venkatsubrab02e3e92015-08-11 07:57:23 -07001104 !netif_carrier_ok(bond->dev) ||
Ben Hutchingsad246c92011-04-26 15:25:52 +00001105 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
1106 return false;
1107
Suresh Kumarfee32de2021-12-13 11:17:09 +05301108 netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
1109 slave ? slave->dev->name : "NULL");
1110
Ben Hutchingsad246c92011-04-26 15:25:52 +00001111 return true;
1112}
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/**
Yang Yingliangacf61b32021-03-29 20:42:57 +08001115 * bond_change_active_slave - change the active slave into the specified one
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * @bond: our bonding struct
Lee Jones45a15532020-08-14 12:39:05 +01001117 * @new_active: the new slave to make the active one
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 *
1119 * Set the new slave to the bond's settings and unset them on the old
1120 * curr_active_slave.
1121 * Setting include flags, mc-list, promiscuity, allmulti, etc.
1122 *
1123 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
1124 * because it is apparently the best available slave we have, even though its
1125 * updelay hasn't timed out yet.
1126 *
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02001127 * Caller must hold RTNL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001129void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Eric Dumazet4740d632014-07-15 06:56:55 -07001131 struct slave *old_active;
1132
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02001133 ASSERT_RTNL();
1134
1135 old_active = rtnl_dereference(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001137 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Jarod Wilson18cb2612020-06-19 10:31:55 -04001140#ifdef CONFIG_XFRM_OFFLOAD
Taehee Yoo9a560552021-07-05 15:38:12 +00001141 bond_ipsec_del_sa_all(bond);
Jarod Wilson18cb2612020-06-19 10:31:55 -04001142#endif /* CONFIG_XFRM_OFFLOAD */
1143
Jarod Wilson5cd24cb2020-07-08 13:46:31 -04001144 if (new_active) {
Veaceslav Falico8e603462014-02-18 07:48:46 +01001145 new_active->last_link_up = jiffies;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (new_active->link == BOND_LINK_BACK) {
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001148 if (bond_uses_primary(bond)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001149 slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
1150 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
1153 new_active->delay = 0;
Jiri Pirko5d397062015-12-03 12:12:19 +01001154 bond_set_slave_link_state(new_active, BOND_LINK_UP,
1155 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Veaceslav Falico01844092014-05-15 21:39:55 +02001157 if (BOND_MODE(bond) == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Holger Eitzenberger58402052008-12-09 23:07:13 -08001160 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } else {
Yufeng Mo52333512021-05-20 14:18:34 +08001163 if (bond_uses_primary(bond))
Jarod Wilsone2a74202019-06-07 10:59:29 -04001164 slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 }
1167
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001168 if (bond_uses_primary(bond))
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00001169 bond_hw_addr_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Holger Eitzenberger58402052008-12-09 23:07:13 -08001171 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001173 if (old_active)
dingtianhong5e5b0662014-02-26 11:05:22 +08001174 bond_set_slave_inactive_flags(old_active,
1175 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001176 if (new_active)
dingtianhong5e5b0662014-02-26 11:05:22 +08001177 bond_set_slave_active_flags(new_active,
1178 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 } else {
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001180 rcu_assign_pointer(bond->curr_active_slave, new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001182
Veaceslav Falico01844092014-05-15 21:39:55 +02001183 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001184 if (old_active)
dingtianhong5e5b0662014-02-26 11:05:22 +08001185 bond_set_slave_inactive_flags(old_active,
1186 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001187
1188 if (new_active) {
Ben Hutchingsad246c92011-04-26 15:25:52 +00001189 bool should_notify_peers = false;
1190
dingtianhong5e5b0662014-02-26 11:05:22 +08001191 bond_set_slave_active_flags(new_active,
1192 BOND_SLAVE_NOTIFY_NOW);
Moni Shoua2ab82852007-10-09 19:43:39 -07001193
Or Gerlitz709f8a42008-06-13 18:12:01 -07001194 if (bond->params.fail_over_mac)
1195 bond_do_fail_over_mac(bond, new_active,
1196 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001197
Ben Hutchingsad246c92011-04-26 15:25:52 +00001198 if (netif_running(bond->dev)) {
1199 bond->send_peer_notif =
Vincent Bernat07a4dde2019-07-02 19:43:54 +02001200 bond->params.num_peer_notif *
1201 max(1, bond->params.peer_notif_delay);
Ben Hutchingsad246c92011-04-26 15:25:52 +00001202 should_notify_peers =
1203 bond_should_notify_peers(bond);
1204 }
1205
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001206 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
Vincent Bernat07a4dde2019-07-02 19:43:54 +02001207 if (should_notify_peers) {
1208 bond->send_peer_notif--;
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001209 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
1210 bond->dev);
Vincent Bernat07a4dde2019-07-02 19:43:54 +02001211 }
Moni Shoua7893b242008-05-17 21:10:12 -07001212 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001213 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001214
Jarod Wilson5cd24cb2020-07-08 13:46:31 -04001215#ifdef CONFIG_XFRM_OFFLOAD
Taehee Yoo9a560552021-07-05 15:38:12 +00001216 bond_ipsec_add_sa_all(bond);
Jarod Wilson5cd24cb2020-07-08 13:46:31 -04001217#endif /* CONFIG_XFRM_OFFLOAD */
1218
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001219 /* resend IGMP joins since active slave has changed or
Flavio Leitner94265cf2011-05-25 08:38:58 +00001220 * all were sent on curr_active_slave.
1221 * resend only if bond is brought up with the affected
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001222 * bonding modes and the retransmission is enabled
1223 */
Flavio Leitner94265cf2011-05-25 08:38:58 +00001224 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001225 ((bond_uses_primary(bond) && new_active) ||
Veaceslav Falico01844092014-05-15 21:39:55 +02001226 BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001227 bond->igmp_retrans = bond->params.resend_igmp;
Nikolay Aleksandrov4beac022013-08-01 11:51:42 +02001228 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/**
1233 * bond_select_active_slave - select a new active slave, if needed
1234 * @bond: our bonding struct
1235 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001236 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * - The old curr_active_slave has been released or lost its link.
1238 * - The primary_slave has got its link back.
1239 * - A slave has got its link back and there's no old curr_active_slave.
1240 *
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02001241 * Caller must hold RTNL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001243void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
1245 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001246 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Nikolay Aleksandrove0974582014-09-15 17:19:35 +02001248 ASSERT_RTNL();
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 best_slave = bond_find_best_slave(bond);
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02001251 if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001253 rv = bond_set_carrier(bond);
1254 if (!rv)
1255 return;
1256
Zhang Shengjud66bd902016-02-03 02:02:32 +00001257 if (netif_carrier_ok(bond->dev))
Eric Dumazetb8bd72d2019-07-01 10:48:51 -07001258 netdev_info(bond->dev, "active interface up!\n");
Zhang Shengjud66bd902016-02-03 02:02:32 +00001259 else
Veaceslav Falico76444f52014-07-15 19:35:58 +02001260 netdev_info(bond->dev, "now running without any active interface!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
1262}
1263
WANG Congf6dc31a2010-05-06 00:48:51 -07001264#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001265static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001266{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001267 struct netpoll *np;
1268 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001269
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001270 np = kzalloc(sizeof(*np), GFP_KERNEL);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001271 err = -ENOMEM;
1272 if (!np)
1273 goto out;
1274
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001275 err = __netpoll_setup(np, slave->dev);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001276 if (err) {
1277 kfree(np);
1278 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001279 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001280 slave->np = np;
1281out:
1282 return err;
1283}
1284static inline void slave_disable_netpoll(struct slave *slave)
1285{
1286 struct netpoll *np = slave->np;
1287
1288 if (!np)
1289 return;
1290
1291 slave->np = NULL;
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -04001292
1293 __netpoll_free(np);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001294}
WANG Congf6dc31a2010-05-06 00:48:51 -07001295
1296static void bond_poll_controller(struct net_device *bond_dev)
1297{
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001298 struct bonding *bond = netdev_priv(bond_dev);
1299 struct slave *slave = NULL;
1300 struct list_head *iter;
1301 struct ad_info ad_info;
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001302
1303 if (BOND_MODE(bond) == BOND_MODE_8023AD)
1304 if (bond_3ad_get_active_agg_info(bond, &ad_info))
1305 return;
1306
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001307 bond_for_each_slave_rcu(bond, slave, iter) {
Eric Dumazet93f62ad2018-09-21 15:27:39 -07001308 if (!bond_slave_is_up(slave))
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001309 continue;
1310
1311 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1312 struct aggregator *agg =
1313 SLAVE_AD_INFO(slave)->port.aggregator;
1314
1315 if (agg &&
1316 agg->aggregator_identifier != ad_info.aggregator_id)
1317 continue;
1318 }
1319
Eric Dumazet93f62ad2018-09-21 15:27:39 -07001320 netpoll_poll_dev(slave->dev);
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001321 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001322}
1323
dingtianhongc4cdef92013-07-23 15:25:27 +08001324static void bond_netpoll_cleanup(struct net_device *bond_dev)
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001325{
dingtianhongc4cdef92013-07-23 15:25:27 +08001326 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001327 struct list_head *iter;
Neil Hormanc2355e12010-10-13 16:01:49 +00001328 struct slave *slave;
Neil Hormanc2355e12010-10-13 16:01:49 +00001329
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001330 bond_for_each_slave(bond, slave, iter)
Veaceslav Falicob6adc612014-05-15 21:39:57 +02001331 if (bond_slave_is_up(slave))
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001332 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001333}
WANG Congf6dc31a2010-05-06 00:48:51 -07001334
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001335static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001336{
1337 struct bonding *bond = netdev_priv(dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001338 struct list_head *iter;
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001339 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001340 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001341
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001342 bond_for_each_slave(bond, slave, iter) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001343 err = slave_enable_netpoll(slave);
1344 if (err) {
dingtianhongc4cdef92013-07-23 15:25:27 +08001345 bond_netpoll_cleanup(dev);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001346 break;
1347 }
1348 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001349 return err;
1350}
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001351#else
1352static inline int slave_enable_netpoll(struct slave *slave)
1353{
1354 return 0;
1355}
1356static inline void slave_disable_netpoll(struct slave *slave)
1357{
1358}
WANG Congf6dc31a2010-05-06 00:48:51 -07001359static void bond_netpoll_cleanup(struct net_device *bond_dev)
1360{
1361}
WANG Congf6dc31a2010-05-06 00:48:51 -07001362#endif
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/*---------------------------------- IOCTL ----------------------------------*/
1365
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001366static netdev_features_t bond_fix_features(struct net_device *dev,
nikolay@redhat.com9b7b1652013-09-02 13:51:41 +02001367 netdev_features_t features)
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001368{
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001369 struct bonding *bond = netdev_priv(dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001370 struct list_head *iter;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001371 netdev_features_t mask;
nikolay@redhat.com9b7b1652013-09-02 13:51:41 +02001372 struct slave *slave;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001373
Tariq Toukan89df6a82021-01-17 16:59:46 +02001374#if IS_ENABLED(CONFIG_TLS_DEVICE)
1375 if (bond_sk_check(bond))
1376 features |= BOND_TLS_FEATURES;
1377 else
1378 features &= ~BOND_TLS_FEATURES;
1379#endif
1380
Scott Feldman7889cbe2015-05-10 09:48:07 -07001381 mask = features;
Roopa Prabhuc158cba2015-01-29 22:40:16 -08001382
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001383 features &= ~NETIF_F_ONE_FOR_ALL;
1384 features |= NETIF_F_ALL_FOR_ALL;
1385
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001386 bond_for_each_slave(bond, slave, iter) {
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001387 features = netdev_increment_features(features,
1388 slave->dev->features,
1389 mask);
1390 }
Eric Dumazetb0ce3502013-05-16 07:34:53 +00001391 features = netdev_add_tso_features(features, mask);
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001392
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001393 return features;
1394}
1395
Tom Herberta1882222015-12-14 11:19:43 -08001396#define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
Alexander Lobakinecb8fed2020-11-01 13:17:17 +00001397 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | \
Michał Mirosław62f2a3a2011-07-13 14:10:29 +00001398 NETIF_F_HIGHDMA | NETIF_F_LRO)
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001399
Tom Herberta1882222015-12-14 11:19:43 -08001400#define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
Alexander Lobakinecb8fed2020-11-01 13:17:17 +00001401 NETIF_F_RXCSUM | NETIF_F_GSO_SOFTWARE)
Or Gerlitz5a7baa72014-06-17 16:11:09 +03001402
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001403#define BOND_MPLS_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
Alexander Lobakinecb8fed2020-11-01 13:17:17 +00001404 NETIF_F_GSO_SOFTWARE)
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001405
Jarod Wilson18cb2612020-06-19 10:31:55 -04001406
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001407static void bond_compute_features(struct bonding *bond)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001408{
Eric Dumazet02875872014-10-05 18:38:35 -07001409 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
1410 IFF_XMIT_DST_RELEASE_PERM;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001411 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
Or Gerlitz5a7baa72014-06-17 16:11:09 +03001412 netdev_features_t enc_features = BOND_ENC_FEATURES;
Jarod Wilson18cb2612020-06-19 10:31:55 -04001413#ifdef CONFIG_XFRM_OFFLOAD
1414 netdev_features_t xfrm_features = BOND_XFRM_FEATURES;
1415#endif /* CONFIG_XFRM_OFFLOAD */
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001416 netdev_features_t mpls_features = BOND_MPLS_FEATURES;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001417 struct net_device *bond_dev = bond->dev;
1418 struct list_head *iter;
1419 struct slave *slave;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001420 unsigned short max_hard_header_len = ETH_HLEN;
Sarveshwar Bandi0e376bd2012-11-21 04:35:03 +00001421 unsigned int gso_max_size = GSO_MAX_SIZE;
1422 u16 gso_max_segs = GSO_MAX_SEGS;
Herbert Xub63365a2008-10-23 01:11:29 -07001423
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001424 if (!bond_has_slaves(bond))
Herbert Xub63365a2008-10-23 01:11:29 -07001425 goto done;
Michal Kubečeka9b3ace2014-05-20 08:29:35 +02001426 vlan_features &= NETIF_F_ALL_FOR_ALL;
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001427 mpls_features &= NETIF_F_ALL_FOR_ALL;
Herbert Xub63365a2008-10-23 01:11:29 -07001428
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001429 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh278339a2009-08-28 12:05:12 +00001430 vlan_features = netdev_increment_features(vlan_features,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001431 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1432
Or Gerlitz5a7baa72014-06-17 16:11:09 +03001433 enc_features = netdev_increment_features(enc_features,
1434 slave->dev->hw_enc_features,
1435 BOND_ENC_FEATURES);
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001436
Jarod Wilson18cb2612020-06-19 10:31:55 -04001437#ifdef CONFIG_XFRM_OFFLOAD
1438 xfrm_features = netdev_increment_features(xfrm_features,
1439 slave->dev->hw_enc_features,
1440 BOND_XFRM_FEATURES);
1441#endif /* CONFIG_XFRM_OFFLOAD */
1442
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001443 mpls_features = netdev_increment_features(mpls_features,
1444 slave->dev->mpls_features,
1445 BOND_MPLS_FEATURES);
1446
Eric Dumazetb6fe83e2012-07-17 12:19:48 +00001447 dst_release_flag &= slave->dev->priv_flags;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001448 if (slave->dev->hard_header_len > max_hard_header_len)
1449 max_hard_header_len = slave->dev->hard_header_len;
Sarveshwar Bandi0e376bd2012-11-21 04:35:03 +00001450
1451 gso_max_size = min(gso_max_size, slave->dev->gso_max_size);
1452 gso_max_segs = min(gso_max_segs, slave->dev->gso_max_segs);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001453 }
Paolo Abeni19cdead2017-04-27 19:29:34 +02001454 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001455
Herbert Xub63365a2008-10-23 01:11:29 -07001456done:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001457 bond_dev->vlan_features = vlan_features;
Willem de Bruijn8eea1ca2018-05-22 11:34:40 -04001458 bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
YueHaibingd595b032019-08-07 10:19:59 +08001459 NETIF_F_HW_VLAN_CTAG_TX |
Alexander Lobakinecb8fed2020-11-01 13:17:17 +00001460 NETIF_F_HW_VLAN_STAG_TX;
Jarod Wilson18cb2612020-06-19 10:31:55 -04001461#ifdef CONFIG_XFRM_OFFLOAD
1462 bond_dev->hw_enc_features |= xfrm_features;
1463#endif /* CONFIG_XFRM_OFFLOAD */
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001464 bond_dev->mpls_features = mpls_features;
Eric Dumazet6d872df2021-11-19 07:43:32 -08001465 netif_set_gso_max_segs(bond_dev, gso_max_segs);
Sarveshwar Bandi0e376bd2012-11-21 04:35:03 +00001466 netif_set_gso_max_size(bond_dev, gso_max_size);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001467
Eric Dumazet02875872014-10-05 18:38:35 -07001468 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1469 if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
1470 dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1471 bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE;
Eric Dumazetb6fe83e2012-07-17 12:19:48 +00001472
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001473 netdev_change_features(bond_dev);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001474}
1475
Moni Shoua872254d2007-10-09 19:43:38 -07001476static void bond_setup_by_slave(struct net_device *bond_dev,
1477 struct net_device *slave_dev)
1478{
Stephen Hemminger00829822008-11-20 20:14:53 -08001479 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001480
1481 bond_dev->type = slave_dev->type;
1482 bond_dev->hard_header_len = slave_dev->hard_header_len;
Eric Dumazetf32f1932020-09-25 06:38:07 -07001483 bond_dev->needed_headroom = slave_dev->needed_headroom;
Moni Shoua872254d2007-10-09 19:43:38 -07001484 bond_dev->addr_len = slave_dev->addr_len;
1485
1486 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1487 slave_dev->addr_len);
1488}
1489
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001490/* On bonding slaves other than the currently active slave, suppress
Jiri Pirko3aba8912011-04-19 03:48:16 +00001491 * duplicates except for alb non-mcast/bcast.
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001492 */
1493static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001494 struct slave *slave,
1495 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001496{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001497 if (bond_is_slave_inactive(slave)) {
Veaceslav Falico01844092014-05-15 21:39:55 +02001498 if (BOND_MODE(bond) == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001499 skb->pkt_type != PACKET_BROADCAST &&
1500 skb->pkt_type != PACKET_MULTICAST)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001501 return false;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001502 return true;
1503 }
1504 return false;
1505}
1506
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001507static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001508{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001509 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001510 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001511 struct bonding *bond;
Eric Dumazetde063b72012-06-11 19:23:07 +00001512 int (*recv_probe)(const struct sk_buff *, struct bonding *,
1513 struct slave *);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00001514 int ret = RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001515
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001516 skb = skb_share_check(skb, GFP_ATOMIC);
1517 if (unlikely(!skb))
1518 return RX_HANDLER_CONSUMED;
1519
1520 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001521
Jiri Pirko35d48902011-03-22 02:38:12 +00001522 slave = bond_slave_get_rcu(skb->dev);
1523 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001524
Mark Rutland6aa7de02017-10-23 14:07:29 -07001525 recv_probe = READ_ONCE(bond->recv_probe);
Mitsuo Hayasaka4d974802011-10-12 16:04:29 +00001526 if (recv_probe) {
Eric Dumazetde063b72012-06-11 19:23:07 +00001527 ret = recv_probe(skb, bond, slave);
1528 if (ret == RX_HANDLER_CONSUMED) {
1529 consume_skb(skb);
1530 return ret;
Jiri Pirko3aba8912011-04-19 03:48:16 +00001531 }
1532 }
1533
Michal Soltys3c963a32019-02-18 17:55:28 +01001534 /*
1535 * For packets determined by bond_should_deliver_exact_match() call to
1536 * be suppressed we want to make an exception for link-local packets.
1537 * This is necessary for e.g. LLDP daemons to be able to monitor
1538 * inactive slave links without being forced to bind to them
1539 * explicitly.
1540 *
1541 * At the same time, packets that are passed to the bonding master
1542 * (including link-local ones) can have their originating interface
1543 * determined via PACKET_ORIGDEV socket option.
Mahesh Bandewar6a9e4612018-09-24 14:39:42 -07001544 */
Michal Soltys3c963a32019-02-18 17:55:28 +01001545 if (bond_should_deliver_exact_match(skb, slave, bond)) {
1546 if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
1547 return RX_HANDLER_PASS;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001548 return RX_HANDLER_EXACT;
Michal Soltys3c963a32019-02-18 17:55:28 +01001549 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001550
Jiri Pirko35d48902011-03-22 02:38:12 +00001551 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001552
Veaceslav Falico01844092014-05-15 21:39:55 +02001553 if (BOND_MODE(bond) == BOND_MODE_ALB &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01001554 netif_is_bridge_port(bond->dev) &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001555 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001556
Changli Gao541ac7c2011-03-02 21:07:14 +00001557 if (unlikely(skb_cow_head(skb,
1558 skb->data - skb_mac_header(skb)))) {
1559 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001560 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001561 }
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001562 bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
1563 bond->dev->addr_len);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001564 }
1565
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00001566 return ret;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001567}
1568
Jiri Pirko41f0b042015-12-03 12:12:14 +01001569static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001570{
Jiri Pirko41f0b042015-12-03 12:12:14 +01001571 switch (BOND_MODE(bond)) {
1572 case BOND_MODE_ROUNDROBIN:
1573 return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
1574 case BOND_MODE_ACTIVEBACKUP:
1575 return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
1576 case BOND_MODE_BROADCAST:
1577 return NETDEV_LAG_TX_TYPE_BROADCAST;
1578 case BOND_MODE_XOR:
1579 case BOND_MODE_8023AD:
1580 return NETDEV_LAG_TX_TYPE_HASH;
1581 default:
1582 return NETDEV_LAG_TX_TYPE_UNKNOWN;
1583 }
1584}
1585
John Hurleyf44aa9e2018-05-23 19:22:52 -07001586static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
1587 enum netdev_lag_tx_type type)
1588{
1589 if (type != NETDEV_LAG_TX_TYPE_HASH)
1590 return NETDEV_LAG_HASH_NONE;
1591
1592 switch (bond->params.xmit_policy) {
1593 case BOND_XMIT_POLICY_LAYER2:
1594 return NETDEV_LAG_HASH_L2;
1595 case BOND_XMIT_POLICY_LAYER34:
1596 return NETDEV_LAG_HASH_L34;
1597 case BOND_XMIT_POLICY_LAYER23:
1598 return NETDEV_LAG_HASH_L23;
1599 case BOND_XMIT_POLICY_ENCAP23:
1600 return NETDEV_LAG_HASH_E23;
1601 case BOND_XMIT_POLICY_ENCAP34:
1602 return NETDEV_LAG_HASH_E34;
Jarod Wilson7b8fc012021-01-18 20:09:27 -05001603 case BOND_XMIT_POLICY_VLAN_SRCMAC:
1604 return NETDEV_LAG_HASH_VLAN_SRCMAC;
John Hurleyf44aa9e2018-05-23 19:22:52 -07001605 default:
1606 return NETDEV_LAG_HASH_UNKNOWN;
1607 }
1608}
1609
David Ahern42ab19e2017-10-04 17:48:47 -07001610static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
1611 struct netlink_ext_ack *extack)
Jiri Pirko41f0b042015-12-03 12:12:14 +01001612{
1613 struct netdev_lag_upper_info lag_upper_info;
John Hurleyf44aa9e2018-05-23 19:22:52 -07001614 enum netdev_lag_tx_type type;
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001615
John Hurleyf44aa9e2018-05-23 19:22:52 -07001616 type = bond_lag_tx_type(bond);
1617 lag_upper_info.tx_type = type;
1618 lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
Xin Long4597efe2017-10-24 13:54:18 +08001619
1620 return netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
1621 &lag_upper_info, extack);
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001622}
1623
Jiri Pirko41f0b042015-12-03 12:12:14 +01001624static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001625{
Jiri Pirko41f0b042015-12-03 12:12:14 +01001626 netdev_upper_dev_unlink(slave->dev, bond->dev);
1627 slave->dev->flags &= ~IFF_SLAVE;
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001628}
1629
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00001630static void slave_kobj_release(struct kobject *kobj)
dingtianhong3fdddd82014-05-12 15:08:43 +08001631{
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00001632 struct slave *slave = to_slave(kobj);
dingtianhong3fdddd82014-05-12 15:08:43 +08001633 struct bonding *bond = bond_get_bond_by_slave(slave);
1634
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001635 cancel_delayed_work_sync(&slave->notify_work);
Veaceslav Falico01844092014-05-15 21:39:55 +02001636 if (BOND_MODE(bond) == BOND_MODE_8023AD)
dingtianhong3fdddd82014-05-12 15:08:43 +08001637 kfree(SLAVE_AD_INFO(slave));
1638
1639 kfree(slave);
1640}
1641
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00001642static struct kobj_type slave_ktype = {
1643 .release = slave_kobj_release,
1644#ifdef CONFIG_SYSFS
1645 .sysfs_ops = &slave_sysfs_ops,
1646#endif
1647};
1648
1649static int bond_kobj_init(struct slave *slave)
1650{
1651 int err;
1652
1653 err = kobject_init_and_add(&slave->kobj, &slave_ktype,
1654 &(slave->dev->dev.kobj), "bonding_slave");
1655 if (err)
1656 kobject_put(&slave->kobj);
1657
1658 return err;
1659}
1660
1661static struct slave *bond_alloc_slave(struct bonding *bond,
1662 struct net_device *slave_dev)
1663{
1664 struct slave *slave = NULL;
1665
1666 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1667 if (!slave)
1668 return NULL;
1669
1670 slave->bond = bond;
1671 slave->dev = slave_dev;
Johannes Berg35d96e62021-05-17 16:13:35 +02001672 INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00001673
1674 if (bond_kobj_init(slave))
1675 return NULL;
1676
1677 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1678 SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info),
1679 GFP_KERNEL);
1680 if (!SLAVE_AD_INFO(slave)) {
1681 kobject_put(&slave->kobj);
1682 return NULL;
1683 }
1684 }
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00001685
1686 return slave;
1687}
1688
Moni Shoua69a23382015-02-03 16:48:30 +02001689static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
1690{
1691 info->bond_mode = BOND_MODE(bond);
1692 info->miimon = bond->params.miimon;
1693 info->num_slaves = bond->slave_cnt;
1694}
1695
1696static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
1697{
1698 strcpy(info->slave_name, slave->dev->name);
1699 info->link = slave->link;
1700 info->state = bond_slave_state(slave);
1701 info->link_failure_count = slave->link_failure_count;
1702}
1703
Moni Shoua69e61132015-02-03 16:48:31 +02001704static void bond_netdev_notify_work(struct work_struct *_work)
1705{
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001706 struct slave *slave = container_of(_work, struct slave,
1707 notify_work.work);
Moni Shoua69e61132015-02-03 16:48:31 +02001708
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001709 if (rtnl_trylock()) {
1710 struct netdev_bonding_info binfo;
1711
1712 bond_fill_ifslave(slave, &binfo.slave);
1713 bond_fill_ifbond(slave->bond, &binfo.master);
1714 netdev_bonding_info_change(slave->dev, &binfo);
1715 rtnl_unlock();
1716 } else {
1717 queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
1718 }
Moni Shoua69e61132015-02-03 16:48:31 +02001719}
1720
1721void bond_queue_slave_event(struct slave *slave)
1722{
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001723 queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
Moni Shoua69e61132015-02-03 16:48:31 +02001724}
1725
Jiri Pirkof7c7eb72015-12-03 12:12:20 +01001726void bond_lower_state_changed(struct slave *slave)
1727{
1728 struct netdev_lag_lower_state_info info;
1729
1730 info.link_up = slave->link == BOND_LINK_UP ||
1731 slave->link == BOND_LINK_FAIL;
1732 info.tx_enabled = bond_is_active_slave(slave);
1733 netdev_lower_state_changed(slave->dev, &info);
1734}
1735
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001736#define BOND_NL_ERR(bond_dev, extack, errmsg) do { \
1737 if (extack) \
1738 NL_SET_ERR_MSG(extack, errmsg); \
1739 else \
1740 netdev_err(bond_dev, "Error: %s\n", errmsg); \
1741} while (0)
1742
1743#define SLAVE_NL_ERR(bond_dev, slave_dev, extack, errmsg) do { \
1744 if (extack) \
1745 NL_SET_ERR_MSG(extack, errmsg); \
1746 else \
1747 slave_err(bond_dev, slave_dev, "Error: %s\n", errmsg); \
1748} while (0)
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750/* enslave device <slave> to bond device <master> */
David Ahern33eaf2a2017-10-04 17:48:46 -07001751int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
1752 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Wang Chen454d7c92008-11-12 23:37:49 -08001754 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001755 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Veaceslav Falicoc8c23902013-09-25 09:20:25 +02001756 struct slave *new_slave = NULL, *prev_slave;
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001757 struct sockaddr_storage ss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int link_reporting;
Veaceslav Falico8599b522013-06-24 11:49:34 +02001759 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Di Zhu4d293fe2021-06-23 11:21:08 +08001761 if (slave_dev->flags & IFF_MASTER &&
1762 !netif_is_bond_master(slave_dev)) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001763 BOND_NL_ERR(bond_dev, extack,
Antoine Tenart1b3f78d2021-08-16 12:08:28 +02001764 "Device type (master device) cannot be enslaved");
Di Zhu3c9ef512021-06-22 11:09:29 +08001765 return -EPERM;
1766 }
1767
Ben Hutchingsc772dde2012-12-07 06:15:32 +00001768 if (!bond->params.use_carrier &&
1769 slave_dev->ethtool_ops->get_link == NULL &&
Arnd Bergmanna7605372021-07-27 15:45:13 +02001770 slave_ops->ndo_eth_ioctl == NULL) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001771 slave_warn(bond_dev, slave_dev, "no link monitoring support\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07001774 /* already in-use? */
1775 if (netdev_is_rx_handler_busy(slave_dev)) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001776 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1777 "Device is in use and cannot be enslaved");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return -EBUSY;
1779 }
1780
Jiri Bohac09a89c22014-02-26 18:20:13 +01001781 if (bond_dev == slave_dev) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001782 BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself.");
Jiri Bohac09a89c22014-02-26 18:20:13 +01001783 return -EPERM;
1784 }
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /* vlan challenged mutual exclusion */
1787 /* no need to lock since we're protected by rtnl_lock */
1788 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001789 slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
Jiri Pirko55462cf2012-10-14 04:30:56 +00001790 if (vlan_uses_dev(bond_dev)) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001791 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1792 "Can not enslave VLAN challenged device to VLAN enabled bond");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return -EPERM;
1794 } else {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001795 slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797 } else {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001798 slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
Jarod Wilson18cb2612020-06-19 10:31:55 -04001801 if (slave_dev->features & NETIF_F_HW_ESP)
1802 slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
1803
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001804 /* Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001805 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001806 * the current ifenslave will set the interface down prior to
1807 * enslaving it; the old ifenslave will not.
1808 */
yzhu1ce3ea1c2015-12-03 18:00:55 +08001809 if (slave_dev->flags & IFF_UP) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001810 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1811 "Device can not be enslaved while up");
Zhang Shengju1e2a8862016-02-09 10:37:46 +00001812 return -EPERM;
Jay Vosburgh217df672005-09-26 16:11:50 -07001813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Moni Shoua872254d2007-10-09 19:43:38 -07001815 /* set bonding device ether type by slave - bonding netdevices are
1816 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1817 * there is a need to override some of the type dependent attribs/funcs.
1818 *
1819 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1820 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1821 */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001822 if (!bond_has_slaves(bond)) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001823 if (bond_dev->type != slave_dev->type) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001824 slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
1825 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001826
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001827 res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
1828 bond_dev);
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001829 res = notifier_to_errno(res);
1830 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001831 slave_err(bond_dev, slave_dev, "refused to change device type\n");
Zhang Shengju1e2a8862016-02-09 10:37:46 +00001832 return -EBUSY;
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001833 }
Moni Shoua75c78502009-09-15 02:37:40 -07001834
Jiri Pirko32a806c2010-03-19 04:00:23 +00001835 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001836 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001837 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001838
Moni Shouae36b9d12009-07-15 04:56:31 +00001839 if (slave_dev->type != ARPHRD_ETHER)
1840 bond_setup_by_slave(bond_dev, slave_dev);
Neil Horman550fd082011-07-26 06:05:38 +00001841 else {
Moni Shouae36b9d12009-07-15 04:56:31 +00001842 ether_setup(bond_dev);
Neil Horman550fd082011-07-26 06:05:38 +00001843 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1844 }
Moni Shoua75c78502009-09-15 02:37:40 -07001845
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001846 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
1847 bond_dev);
Moni Shouae36b9d12009-07-15 04:56:31 +00001848 }
Moni Shoua872254d2007-10-09 19:43:38 -07001849 } else if (bond_dev->type != slave_dev->type) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001850 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1851 "Device type is different from other slaves");
Zhang Shengju1e2a8862016-02-09 10:37:46 +00001852 return -EINVAL;
Moni Shoua872254d2007-10-09 19:43:38 -07001853 }
1854
Mark Bloch1533e772016-07-21 11:52:55 +03001855 if (slave_dev->type == ARPHRD_INFINIBAND &&
1856 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001857 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1858 "Only active-backup mode is supported for infiniband slaves");
Mark Bloch1533e772016-07-21 11:52:55 +03001859 res = -EOPNOTSUPP;
1860 goto err_undo_flags;
1861 }
1862
1863 if (!slave_ops->ndo_set_mac_address ||
1864 slave_dev->type == ARPHRD_INFINIBAND) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001865 slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n");
Veaceslav Falicof5442442014-07-15 13:26:01 +02001866 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
1867 bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1868 if (!bond_has_slaves(bond)) {
dingtianhong00503b62014-01-25 13:00:29 +08001869 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
Jarod Wilsone2a74202019-06-07 10:59:29 -04001870 slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
Veaceslav Falicof5442442014-07-15 13:26:01 +02001871 } else {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04001872 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
1873 "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
Veaceslav Falicof5442442014-07-15 13:26:01 +02001874 res = -EOPNOTSUPP;
1875 goto err_undo_flags;
dingtianhong00503b62014-01-25 13:00:29 +08001876 }
Moni Shoua2ab82852007-10-09 19:43:39 -07001877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
1879
Amerigo Wang8d8fc292011-05-19 21:39:10 +00001880 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1881
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001882 /* If this is the first slave, then we need to set the master's hardware
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001883 * address to be the same as the slave's.
1884 */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001885 if (!bond_has_slaves(bond) &&
Petr Machatab9245912018-12-13 11:54:44 +00001886 bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
1887 res = bond_set_dev_addr(bond->dev, slave_dev);
1888 if (res)
1889 goto err_undo_flags;
1890 }
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001891
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00001892 new_slave = bond_alloc_slave(bond, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (!new_slave) {
1894 res = -ENOMEM;
1895 goto err_undo_flags;
1896 }
dingtianhong3fdddd82014-05-12 15:08:43 +08001897
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001898 /* Set the new_slave's queue_id to be zero. Queue ID mapping
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001899 * is set via sysfs or module option if desired.
1900 */
1901 new_slave->queue_id = 0;
1902
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001903 /* Save slave's original mtu and then set it to match the bond */
1904 new_slave->original_mtu = slave_dev->mtu;
1905 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1906 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001907 slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001908 goto err_free;
1909 }
1910
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001911 /* Save slave's original ("permanent") mac address for modes
Jay Vosburgh217df672005-09-26 16:11:50 -07001912 * that need it, and for restoring it upon release, and then
1913 * set it to the master's address
1914 */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001915 bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
1916 slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
dingtianhong00503b62014-01-25 13:00:29 +08001918 if (!bond->params.fail_over_mac ||
Veaceslav Falico01844092014-05-15 21:39:55 +02001919 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001920 /* Set slave to master's mac address. The application already
Moni Shoua2ab82852007-10-09 19:43:39 -07001921 * set the master's mac address to that of the first slave
1922 */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001923 memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
1924 ss.ss_family = slave_dev->type;
Petr Machata3a37a962018-12-13 11:54:30 +00001925 res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss,
1926 extack);
Moni Shoua2ab82852007-10-09 19:43:39 -07001927 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001928 slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001929 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001930 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Karl Heiss03d84a52016-01-11 08:28:43 -05001933 /* set slave flag before open to prevent IPv6 addrconf */
1934 slave_dev->flags |= IFF_SLAVE;
1935
Jay Vosburgh217df672005-09-26 16:11:50 -07001936 /* open the slave since the application closed it */
Petr Machata00f54e62018-12-06 17:05:36 +00001937 res = dev_open(slave_dev, extack);
Jay Vosburgh217df672005-09-26 16:11:50 -07001938 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001939 slave_err(bond_dev, slave_dev, "Opening slave failed\n");
Veaceslav Falico1f718f02013-09-25 09:20:10 +02001940 goto err_restore_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001943 slave_dev->priv_flags |= IFF_BONDING;
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04001944 /* initialize slave stats */
1945 dev_get_stats(new_slave->dev, &new_slave->slave_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Holger Eitzenberger58402052008-12-09 23:07:13 -08001947 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /* bond_alb_init_slave() must be called before all other stages since
1949 * it might fail and we do not want to have to undo everything
1950 */
1951 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001952 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001953 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
Wei Yongjunb8e2fde2013-08-23 10:45:07 +08001956 res = vlan_vids_add_by_dev(slave_dev, bond_dev);
1957 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001958 slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
Xin Longae42cc62018-03-26 01:16:46 +08001959 goto err_close;
nikolay@redhat.com1ff412a2013-08-06 12:40:15 +02001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Veaceslav Falicoc8c23902013-09-25 09:20:25 +02001962 prev_slave = bond_last_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 new_slave->delay = 0;
1965 new_slave->link_failure_count = 0;
1966
Andreas Bornad729bc92017-08-10 06:41:44 +02001967 if (bond_update_speed_duplex(new_slave) &&
1968 bond_needs_speed_duplex(bond))
Mahesh Bandewar3f3c2782017-04-03 18:38:39 -07001969 new_slave->link = BOND_LINK_DOWN;
Veaceslav Falico876254a2013-03-12 06:31:32 +00001970
Veaceslav Falico49f17de2014-02-18 07:48:47 +01001971 new_slave->last_rx = jiffies -
Michal Kubečekf31c7932012-04-17 02:02:06 +00001972 (msecs_to_jiffies(bond->params.arp_interval) + 1);
Veaceslav Falico8599b522013-06-24 11:49:34 +02001973 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
Veaceslav Falico49f17de2014-02-18 07:48:47 +01001974 new_slave->target_last_arp_rx[i] = new_slave->last_rx;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (bond->params.miimon && !bond->params.use_carrier) {
1977 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1978
1979 if ((link_reporting == -1) && !bond->params.arp_interval) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001980 /* miimon is set but a bonded network driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 * does not support ETHTOOL/MII and
1982 * arp_interval is not set. Note: if
1983 * use_carrier is enabled, we will never go
1984 * here (because netif_carrier is always
1985 * supported); thus, we don't need to change
1986 * the messages for netif_carrier.
1987 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04001988 slave_warn(bond_dev, slave_dev, "MII and ETHTOOL support not available for slave, and arp_interval/arp_ip_target module parameters not specified, thus bonding will not detect link failures! see bonding.txt for details\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 } else if (link_reporting == -1) {
1990 /* unable get link status using mii/ethtool */
Jarod Wilsone2a74202019-06-07 10:59:29 -04001991 slave_warn(bond_dev, slave_dev, "can't get link status from slave; the network driver associated with this interface does not support MII or ETHTOOL link status reporting, thus miimon has no effect on this interface\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993 }
1994
1995 /* check for initial state */
Aviv Hellera30b0162016-07-05 12:09:47 +03001996 new_slave->link = BOND_LINK_NOCHANGE;
Michal Kubečekf31c7932012-04-17 02:02:06 +00001997 if (bond->params.miimon) {
1998 if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) {
1999 if (bond->params.updelay) {
Moni Shoua69a23382015-02-03 16:48:30 +02002000 bond_set_slave_link_state(new_slave,
Jiri Pirko5d397062015-12-03 12:12:19 +01002001 BOND_LINK_BACK,
2002 BOND_SLAVE_NOTIFY_NOW);
Michal Kubečekf31c7932012-04-17 02:02:06 +00002003 new_slave->delay = bond->params.updelay;
2004 } else {
Moni Shoua69a23382015-02-03 16:48:30 +02002005 bond_set_slave_link_state(new_slave,
Jiri Pirko5d397062015-12-03 12:12:19 +01002006 BOND_LINK_UP,
2007 BOND_SLAVE_NOTIFY_NOW);
Michal Kubečekf31c7932012-04-17 02:02:06 +00002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } else {
Jiri Pirko5d397062015-12-03 12:12:19 +01002010 bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
2011 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
Michal Kubečekf31c7932012-04-17 02:02:06 +00002013 } else if (bond->params.arp_interval) {
Moni Shoua69a23382015-02-03 16:48:30 +02002014 bond_set_slave_link_state(new_slave,
2015 (netif_carrier_ok(slave_dev) ?
Jiri Pirko5d397062015-12-03 12:12:19 +01002016 BOND_LINK_UP : BOND_LINK_DOWN),
2017 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 } else {
Jiri Pirko5d397062015-12-03 12:12:19 +01002019 bond_set_slave_link_state(new_slave, BOND_LINK_UP,
2020 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022
Michal Kubečekf31c7932012-04-17 02:02:06 +00002023 if (new_slave->link != BOND_LINK_DOWN)
Veaceslav Falico8e603462014-02-18 07:48:46 +01002024 new_slave->last_link_up = jiffies;
Jarod Wilsone2a74202019-06-07 10:59:29 -04002025 slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
2026 new_slave->link == BOND_LINK_DOWN ? "DOWN" :
2027 (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
Michal Kubečekf31c7932012-04-17 02:02:06 +00002028
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02002029 if (bond_uses_primary(bond) && bond->params.primary[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00002031 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002032 rcu_assign_pointer(bond->primary_slave, new_slave);
Jiri Pirkoa5499522009-09-25 03:28:09 +00002033 bond->force_primary = true;
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
Veaceslav Falico01844092014-05-15 21:39:55 +02002037 switch (BOND_MODE(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 case BOND_MODE_ACTIVEBACKUP:
dingtianhong5e5b0662014-02-26 11:05:22 +08002039 bond_set_slave_inactive_flags(new_slave,
2040 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 break;
2042 case BOND_MODE_8023AD:
2043 /* in 802.3ad mode, the internal mechanism
2044 * will activate the slaves in the selected
2045 * aggregator
2046 */
dingtianhong5e5b0662014-02-26 11:05:22 +08002047 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /* if this is the first slave */
Veaceslav Falico23c147e2013-09-27 15:10:57 +02002049 if (!prev_slave) {
dingtianhong3fdddd82014-05-12 15:08:43 +08002050 SLAVE_AD_INFO(new_slave)->id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* Initialize AD with the number of times that the AD timer is called in 1 second
2052 * can be called only after the mac address of the bond is set
2053 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00002054 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 } else {
dingtianhong3fdddd82014-05-12 15:08:43 +08002056 SLAVE_AD_INFO(new_slave)->id =
2057 SLAVE_AD_INFO(prev_slave)->id + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059
2060 bond_3ad_bind_slave(new_slave);
2061 break;
2062 case BOND_MODE_TLB:
2063 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002064 bond_set_active_slave(new_slave);
dingtianhong5e5b0662014-02-26 11:05:22 +08002065 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 break;
2067 default:
Jarod Wilsone2a74202019-06-07 10:59:29 -04002068 slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002071 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 /* In trunking mode there is little meaning to curr_active_slave
2074 * anyway (it holds no special properties of the bond device),
2075 * so we can change it without calling change_active_interface()
2076 */
Eric Dumazet4740d632014-07-15 06:56:55 -07002077 if (!rcu_access_pointer(bond->curr_active_slave) &&
2078 new_slave->link == BOND_LINK_UP)
nikolay@redhat.com278b2082013-08-01 16:54:51 +02002079 rcu_assign_pointer(bond->curr_active_slave, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 break;
2082 } /* switch(bond_mode) */
2083
WANG Congf6dc31a2010-05-06 00:48:51 -07002084#ifdef CONFIG_NET_POLL_CONTROLLER
Xin Longddea7882018-04-22 19:11:50 +08002085 if (bond->dev->npinfo) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002086 if (slave_enable_netpoll(new_slave)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002087 slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n");
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002088 res = -EBUSY;
stephen hemmingerf7d98212011-12-31 13:26:46 +00002089 goto err_detach;
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002090 }
WANG Congf6dc31a2010-05-06 00:48:51 -07002091 }
2092#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002093
Michal Kubečekfbe168b2014-11-13 07:54:50 +01002094 if (!(bond_dev->features & NETIF_F_LRO))
2095 dev_disable_lro(slave_dev);
2096
Jiri Pirko35d48902011-03-22 02:38:12 +00002097 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
2098 new_slave);
2099 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002100 slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
Veaceslav Falico5831d662013-09-25 09:20:32 +02002101 goto err_detach;
Jiri Pirko35d48902011-03-22 02:38:12 +00002102 }
2103
David Ahern42ab19e2017-10-04 17:48:47 -07002104 res = bond_master_upper_dev_link(bond, new_slave, extack);
Veaceslav Falico1f718f02013-09-25 09:20:10 +02002105 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002106 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
Veaceslav Falico1f718f02013-09-25 09:20:10 +02002107 goto err_unregister;
2108 }
2109
Tobias Waldekranz32d4c562021-01-13 09:42:51 +01002110 bond_lower_state_changed(new_slave);
2111
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08002112 res = bond_sysfs_slave_add(new_slave);
2113 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002114 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08002115 goto err_upper_unlink;
2116 }
2117
Xin Longae42cc62018-03-26 01:16:46 +08002118 /* If the mode uses primary, then the following is handled by
2119 * bond_change_active_slave().
2120 */
2121 if (!bond_uses_primary(bond)) {
2122 /* set promiscuity level to new slave */
2123 if (bond_dev->flags & IFF_PROMISC) {
2124 res = dev_set_promiscuity(slave_dev, 1);
2125 if (res)
2126 goto err_sysfs_del;
2127 }
2128
2129 /* set allmulti level to new slave */
2130 if (bond_dev->flags & IFF_ALLMULTI) {
2131 res = dev_set_allmulti(slave_dev, 1);
Xin Long9f5a90c2018-03-26 01:16:47 +08002132 if (res) {
2133 if (bond_dev->flags & IFF_PROMISC)
2134 dev_set_promiscuity(slave_dev, -1);
Xin Longae42cc62018-03-26 01:16:46 +08002135 goto err_sysfs_del;
Xin Long9f5a90c2018-03-26 01:16:47 +08002136 }
Xin Longae42cc62018-03-26 01:16:46 +08002137 }
2138
2139 netif_addr_lock_bh(bond_dev);
2140 dev_mc_sync_multiple(slave_dev, bond_dev);
2141 dev_uc_sync_multiple(slave_dev, bond_dev);
2142 netif_addr_unlock_bh(bond_dev);
2143
2144 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
2145 /* add lacpdu mc addr to mc list */
2146 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
2147
2148 dev_mc_add(slave_dev, lacpdu_multicast);
2149 }
2150 }
2151
Veaceslav Falico5378c2e2013-10-21 11:48:30 +02002152 bond->slave_cnt++;
2153 bond_compute_features(bond);
2154 bond_set_carrier(bond);
2155
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02002156 if (bond_uses_primary(bond)) {
dingtianhongf80889a2014-02-12 12:06:40 +08002157 block_netpoll_tx();
Veaceslav Falico5378c2e2013-10-21 11:48:30 +02002158 bond_select_active_slave(bond);
dingtianhongf80889a2014-02-12 12:06:40 +08002159 unblock_netpoll_tx();
Veaceslav Falico5378c2e2013-10-21 11:48:30 +02002160 }
Veaceslav Falico1f718f02013-09-25 09:20:10 +02002161
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04002162 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07002163 bond_update_slave_arr(bond, NULL);
2164
Debabrata Banerjee21706ee2018-05-09 19:32:11 -04002165
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00002166 if (!slave_dev->netdev_ops->ndo_bpf ||
2167 !slave_dev->netdev_ops->ndo_xdp_xmit) {
2168 if (bond->xdp_prog) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04002169 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
2170 "Slave does not support XDP");
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00002171 res = -EOPNOTSUPP;
2172 goto err_sysfs_del;
2173 }
Jussi Maki6d5f1ef2021-09-06 10:56:37 +02002174 } else if (bond->xdp_prog) {
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00002175 struct netdev_bpf xdp = {
2176 .command = XDP_SETUP_PROG,
2177 .flags = 0,
2178 .prog = bond->xdp_prog,
2179 .extack = extack,
2180 };
2181
2182 if (dev_xdp_prog_count(slave_dev) > 0) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04002183 SLAVE_NL_ERR(bond_dev, slave_dev, extack,
2184 "Slave has XDP program loaded, please unload before enslaving");
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00002185 res = -EOPNOTSUPP;
2186 goto err_sysfs_del;
2187 }
2188
2189 res = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
2190 if (res < 0) {
2191 /* ndo_bpf() sets extack error message */
2192 slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res);
2193 goto err_sysfs_del;
2194 }
2195 if (bond->xdp_prog)
2196 bpf_prog_inc(bond->xdp_prog);
2197 }
2198
Jarod Wilsone2a74202019-06-07 10:59:29 -04002199 slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
2200 bond_is_active_slave(new_slave) ? "an active" : "a backup",
2201 new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* enslave is successful */
Moni Shoua69e61132015-02-03 16:48:31 +02002204 bond_queue_slave_event(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return 0;
2206
2207/* Undo stages on error */
Xin Longae42cc62018-03-26 01:16:46 +08002208err_sysfs_del:
2209 bond_sysfs_slave_del(new_slave);
2210
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08002211err_upper_unlink:
Jiri Pirko41f0b042015-12-03 12:12:14 +01002212 bond_upper_dev_unlink(bond, new_slave);
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08002213
Veaceslav Falico1f718f02013-09-25 09:20:10 +02002214err_unregister:
2215 netdev_rx_handler_unregister(slave_dev);
2216
stephen hemmingerf7d98212011-12-31 13:26:46 +00002217err_detach:
nikolay@redhat.com1ff412a2013-08-06 12:40:15 +02002218 vlan_vids_del_by_dev(slave_dev, bond_dev);
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002219 if (rcu_access_pointer(bond->primary_slave) == new_slave)
2220 RCU_INIT_POINTER(bond->primary_slave, NULL);
Eric Dumazet4740d632014-07-15 06:56:55 -07002221 if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
dingtianhongf80889a2014-02-12 12:06:40 +08002222 block_netpoll_tx();
dingtianhongc8517032013-12-13 10:20:07 +08002223 bond_change_active_slave(bond, NULL);
nikolay@redhat.com3c5913b532013-04-18 07:33:36 +00002224 bond_select_active_slave(bond);
dingtianhongf80889a2014-02-12 12:06:40 +08002225 unblock_netpoll_tx();
nikolay@redhat.com3c5913b532013-04-18 07:33:36 +00002226 }
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002227 /* either primary_slave or curr_active_slave might've changed */
2228 synchronize_rcu();
nikolay@redhat.comfc7a72a2013-04-18 07:33:37 +00002229 slave_disable_netpoll(new_slave);
stephen hemmingerf7d98212011-12-31 13:26:46 +00002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231err_close:
Taehee Yoo65de65d2019-10-21 18:47:52 +00002232 if (!netif_is_bond_master(slave_dev))
2233 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 dev_close(slave_dev);
2235
2236err_restore_mac:
Karl Heiss03d84a52016-01-11 08:28:43 -05002237 slave_dev->flags &= ~IFF_SLAVE;
dingtianhong00503b62014-01-25 13:00:29 +08002238 if (!bond->params.fail_over_mac ||
Veaceslav Falico01844092014-05-15 21:39:55 +02002239 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07002240 /* XXX TODO - fom follow mode needs to change master's
2241 * MAC if this slave's MAC is in use by the bond, or at
2242 * least print a warning.
2243 */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04002244 bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
2245 new_slave->dev->addr_len);
2246 ss.ss_family = slave_dev->type;
Petr Machata3a37a962018-12-13 11:54:30 +00002247 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
Moni Shoua2ab82852007-10-09 19:43:39 -07002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002250err_restore_mtu:
2251 dev_set_mtu(slave_dev, new_slave->original_mtu);
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253err_free:
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00002254 kobject_put(&new_slave->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256err_undo_flags:
Nikolay Aleksandrovb8fad452013-06-12 00:07:01 +02002257 /* Enslave of first slave has failed and we need to fix master's mac */
Nikolay Aleksandrov7d5cd2c2015-07-15 22:57:01 +02002258 if (!bond_has_slaves(bond)) {
2259 if (ether_addr_equal_64bits(bond_dev->dev_addr,
2260 slave_dev->dev_addr))
2261 eth_hw_addr_random(bond_dev);
2262 if (bond_dev->type != ARPHRD_ETHER) {
Jay Vosburgh40baec22015-11-06 17:23:23 -08002263 dev_close(bond_dev);
Nikolay Aleksandrov7d5cd2c2015-07-15 22:57:01 +02002264 ether_setup(bond_dev);
2265 bond_dev->flags |= IFF_MASTER;
2266 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2267 }
2268 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return res;
2271}
2272
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002273/* Try to release the slave device <slave> from the bond device <master>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 * It is legal to access curr_active_slave without a lock because all the function
Nikolay Aleksandrov8c0bc552014-09-11 22:49:28 +02002275 * is RTNL-locked. If "all" is true it means that the function is being called
nikolay@redhat.com08963412013-02-18 14:09:42 +00002276 * while destroying a bond interface and all slaves are being released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 *
2278 * The rules for slave state should be:
2279 * for Active/Backup:
2280 * Active stays on all backups go down
2281 * for Bonded connections:
2282 * The first up interface should be left on and all others downed.
2283 */
nikolay@redhat.com08963412013-02-18 14:09:42 +00002284static int __bond_release_one(struct net_device *bond_dev,
2285 struct net_device *slave_dev,
WANG Congf51048c2017-07-06 15:01:57 -07002286 bool all, bool unregister)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Wang Chen454d7c92008-11-12 23:37:49 -08002288 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 struct slave *slave, *oldcurrent;
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04002290 struct sockaddr_storage ss;
Neil Horman5a0068d2013-09-27 12:22:15 -04002291 int old_flags = bond_dev->flags;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002292 netdev_features_t old_features = bond_dev->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
2294 /* slave is not a slave or master is not master of this slave */
2295 if (!(slave_dev->flags & IFF_SLAVE) ||
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002296 !netdev_has_upper_dev(slave_dev, bond_dev)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002297 slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return -EINVAL;
2299 }
2300
Neil Hormane843fa52010-10-13 16:01:50 +00002301 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 slave = bond_get_slave_by_dev(bond, slave_dev);
2304 if (!slave) {
2305 /* not a slave of this bond */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002306 slave_info(bond_dev, slave_dev, "interface not enslaved\n");
Neil Hormane843fa52010-10-13 16:01:50 +00002307 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return -EINVAL;
2309 }
2310
Jiri Pirko57beaca2015-12-03 12:12:21 +01002311 bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
2312
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08002313 bond_sysfs_slave_del(slave);
2314
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04002315 /* recompute stats just before removing the slave */
2316 bond_get_stats(bond->dev, &bond->bond_stats);
2317
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00002318 if (bond->xdp_prog) {
2319 struct netdev_bpf xdp = {
2320 .command = XDP_SETUP_PROG,
2321 .flags = 0,
2322 .prog = NULL,
2323 .extack = NULL,
2324 };
2325 if (slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp))
2326 slave_warn(bond_dev, slave_dev, "failed to unload XDP program\n");
2327 }
2328
Jiri Pirko35d48902011-03-22 02:38:12 +00002329 /* unregister rx_handler early so bond_handle_frame wouldn't be called
2330 * for this slave anymore.
2331 */
2332 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko35d48902011-03-22 02:38:12 +00002333
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002334 if (BOND_MODE(bond) == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Yufeng Mo220ade72021-07-30 10:19:11 +08002337 bond_upper_dev_unlink(bond, slave);
2338
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04002339 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07002340 bond_update_slave_arr(bond, slave);
2341
Jarod Wilsone2a74202019-06-07 10:59:29 -04002342 slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
2343 bond_is_active_slave(slave) ? "active" : "backup");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Eric Dumazet4740d632014-07-15 06:56:55 -07002345 oldcurrent = rcu_access_pointer(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Eric Dumazet85741712014-07-15 06:56:56 -07002347 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
dingtianhong00503b62014-01-25 13:00:29 +08002349 if (!all && (!bond->params.fail_over_mac ||
Veaceslav Falico01844092014-05-15 21:39:55 +02002350 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
dingtianhong844223a2014-01-02 09:13:16 +08002351 if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002352 bond_has_slaves(bond))
Jarod Wilsone2a74202019-06-07 10:59:29 -04002353 slave_warn(bond_dev, slave_dev, "the permanent HWaddr of slave - %pM - is still in use by bond - set the HWaddr of slave to a different address to avoid conflicts\n",
2354 slave->perm_hwaddr);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002355 }
2356
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002357 if (rtnl_dereference(bond->primary_slave) == slave)
2358 RCU_INIT_POINTER(bond->primary_slave, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02002360 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Holger Eitzenberger58402052008-12-09 23:07:13 -08002363 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /* Must be called only after the slave has been
2365 * detached from the list and the curr_active_slave
2366 * has been cleared (if our_slave == old_current),
2367 * but before a new active slave is selected.
2368 */
2369 bond_alb_deinit_slave(bond, slave);
2370 }
2371
nikolay@redhat.com08963412013-02-18 14:09:42 +00002372 if (all) {
Paul E. McKenney36708b82013-12-09 15:19:53 -08002373 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
nikolay@redhat.com08963412013-02-18 14:09:42 +00002374 } else if (oldcurrent == slave) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002375 /* Note that we hold RTNL over this sequence, so there
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002376 * is no concern that another slave add/remove event
2377 * will interfere.
2378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002380 }
2381
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002382 if (!bond_has_slaves(bond)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002383 bond_set_carrier(bond);
Jiri Pirko409cc1f2013-01-30 11:08:11 +01002384 eth_hw_addr_random(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386
Neil Hormane843fa52010-10-13 16:01:50 +00002387 unblock_netpoll_tx();
nikolay@redhat.com278b2082013-08-01 16:54:51 +02002388 synchronize_rcu();
Nikolay Aleksandrovee6154e2014-02-26 14:20:30 +01002389 bond->slave_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002391 if (!bond_has_slaves(bond)) {
Shlomo Pongratz2af73d42012-04-03 22:56:19 +00002392 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
Veaceslav Falico80028ea2013-03-06 07:10:32 +00002393 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
2394 }
Shlomo Pongratz2af73d42012-04-03 22:56:19 +00002395
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002396 bond_compute_features(bond);
2397 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2398 (old_features & NETIF_F_VLAN_CHALLENGED))
Jarod Wilsone2a74202019-06-07 10:59:29 -04002399 slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002400
nikolay@redhat.com1ff412a2013-08-06 12:40:15 +02002401 vlan_vids_del_by_dev(slave_dev, bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002403 /* If the mode uses primary, then this case was handled above by
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00002404 * bond_change_active_slave(..., NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 */
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02002406 if (!bond_uses_primary(bond)) {
Neil Horman5a0068d2013-09-27 12:22:15 -04002407 /* unset promiscuity level from slave
2408 * NOTE: The NETDEV_CHANGEADDR call above may change the value
2409 * of the IFF_PROMISC flag in the bond_dev, but we need the
2410 * value of that flag before that change, as that was the value
2411 * when this slave was attached, so we cache at the start of the
2412 * function and use it here. Same goes for ALLMULTI below
2413 */
2414 if (old_flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 /* unset allmulti level from slave */
Neil Horman5a0068d2013-09-27 12:22:15 -04002418 if (old_flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00002421 bond_hw_addr_flush(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 }
2423
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002424 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* close slave before restoring its mac address */
2427 dev_close(slave_dev);
2428
dingtianhong00503b62014-01-25 13:00:29 +08002429 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
Veaceslav Falico01844092014-05-15 21:39:55 +02002430 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002431 /* restore original ("permanent") mac address */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04002432 bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
2433 slave->dev->addr_len);
2434 ss.ss_family = slave_dev->type;
Petr Machata3a37a962018-12-13 11:54:30 +00002435 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
Moni Shoua2ab82852007-10-09 19:43:39 -07002436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
WANG Congf51048c2017-07-06 15:01:57 -07002438 if (unregister)
2439 __dev_set_mtu(slave_dev, slave->original_mtu);
2440 else
2441 dev_set_mtu(slave_dev, slave->original_mtu);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002442
Taehee Yoo65de65d2019-10-21 18:47:52 +00002443 if (!netif_is_bond_master(slave_dev))
2444 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Jamie Ilesb9ad3e92020-11-20 14:28:27 +00002446 kobject_put(&slave->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
nikolay@redhat.com08963412013-02-18 14:09:42 +00002451/* A wrapper used because of ndo_del_link */
2452int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2453{
WANG Congf51048c2017-07-06 15:01:57 -07002454 return __bond_release_one(bond_dev, slave_dev, false, false);
nikolay@redhat.com08963412013-02-18 14:09:42 +00002455}
2456
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002457/* First release a slave and then destroy the bond if no more slaves are left.
2458 * Must be under rtnl_lock when this function is called.
2459 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002460static int bond_release_and_destroy(struct net_device *bond_dev,
2461 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002462{
Wang Chen454d7c92008-11-12 23:37:49 -08002463 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002464 int ret;
2465
WANG Congf51048c2017-07-06 15:01:57 -07002466 ret = __bond_release_one(bond_dev, slave_dev, false, true);
Cong Wang83270702020-08-14 20:05:58 -07002467 if (ret == 0 && !bond_has_slaves(bond) &&
2468 bond_dev->reg_state != NETREG_UNREGISTERING) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002469 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Jarod Wilsone2a74202019-06-07 10:59:29 -04002470 netdev_info(bond_dev, "Destroying bond\n");
Nikolay Aleksandrov06f6d102015-07-15 21:52:51 +02002471 bond_remove_proc_entry(bond);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002472 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002473 }
2474 return ret;
2475}
2476
Zhu Yanjun3d675762017-02-02 23:46:21 -05002477static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Wang Chen454d7c92008-11-12 23:37:49 -08002479 struct bonding *bond = netdev_priv(bond_dev);
Yufeng Mo86a5ad02021-05-20 14:18:32 +08002480
Moni Shoua69a23382015-02-03 16:48:30 +02002481 bond_fill_ifbond(bond, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2485{
Wang Chen454d7c92008-11-12 23:37:49 -08002486 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002487 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002488 int i = 0, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002491 bond_for_each_slave(bond, slave, iter) {
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002492 if (i++ == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002493 res = 0;
Moni Shoua69a23382015-02-03 16:48:30 +02002494 bond_fill_ifslave(slave, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 break;
2496 }
2497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Eric Dumazet689c96c2009-04-23 03:39:04 +00002499 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
2502/*-------------------------------- Monitoring -------------------------------*/
2503
Eric Dumazet4740d632014-07-15 06:56:55 -07002504/* called with rcu_read_lock() */
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002505static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002507 int link_state, commit = 0;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002508 struct list_head *iter;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002509 struct slave *slave;
Jiri Pirko41f89102009-04-24 03:57:29 +00002510 bool ignore_updelay;
2511
Eric Dumazet4740d632014-07-15 06:56:55 -07002512 ignore_updelay = !rcu_dereference(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
dingtianhong4cb4f972013-12-13 10:19:39 +08002514 bond_for_each_slave_rcu(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002515 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002517 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002520 case BOND_LINK_UP:
2521 if (link_state)
2522 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002524 bond_propose_link_state(slave, BOND_LINK_FAIL);
WANG Congd94708a2017-07-25 09:44:25 -07002525 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002526 slave->delay = bond->params.downdelay;
2527 if (slave->delay) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002528 slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
2529 (BOND_MODE(bond) ==
2530 BOND_MODE_ACTIVEBACKUP) ?
2531 (bond_is_active_slave(slave) ?
2532 "active " : "backup ") : "",
2533 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002535 fallthrough;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002536 case BOND_LINK_FAIL:
2537 if (link_state) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002538 /* recovered before downdelay expired */
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002539 bond_propose_link_state(slave, BOND_LINK_UP);
Veaceslav Falico8e603462014-02-18 07:48:46 +01002540 slave->last_link_up = jiffies;
Jarod Wilsone2a74202019-06-07 10:59:29 -04002541 slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
2542 (bond->params.downdelay - slave->delay) *
2543 bond->params.miimon);
Mahesh Bandewarfb9eb892017-04-11 22:36:00 -07002544 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002545 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002547
2548 if (slave->delay <= 0) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002549 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002550 commit++;
2551 continue;
2552 }
2553
2554 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002557 case BOND_LINK_DOWN:
2558 if (!link_state)
2559 continue;
2560
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002561 bond_propose_link_state(slave, BOND_LINK_BACK);
WANG Congd94708a2017-07-25 09:44:25 -07002562 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002563 slave->delay = bond->params.updelay;
2564
2565 if (slave->delay) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002566 slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
2567 ignore_updelay ? 0 :
2568 bond->params.updelay *
2569 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002571 fallthrough;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002572 case BOND_LINK_BACK:
2573 if (!link_state) {
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002574 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jarod Wilsone2a74202019-06-07 10:59:29 -04002575 slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
2576 (bond->params.updelay - slave->delay) *
2577 bond->params.miimon);
Mahesh Bandewarfb9eb892017-04-11 22:36:00 -07002578 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002579 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002581
Jiri Pirko41f89102009-04-24 03:57:29 +00002582 if (ignore_updelay)
2583 slave->delay = 0;
2584
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002585 if (slave->delay <= 0) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002586 bond_propose_link_state(slave, BOND_LINK_UP);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002587 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002588 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002589 continue;
2590 }
2591
2592 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002594 }
2595 }
2596
2597 return commit;
2598}
2599
Tonghao Zhang7e878b62018-05-16 19:09:23 -07002600static void bond_miimon_link_change(struct bonding *bond,
2601 struct slave *slave,
2602 char link)
2603{
2604 switch (BOND_MODE(bond)) {
2605 case BOND_MODE_8023AD:
2606 bond_3ad_handle_link_change(slave, link);
2607 break;
2608 case BOND_MODE_TLB:
2609 case BOND_MODE_ALB:
2610 bond_alb_handle_link_change(bond, slave, link);
2611 break;
2612 case BOND_MODE_XOR:
2613 bond_update_slave_arr(bond, NULL);
2614 break;
2615 }
2616}
2617
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002618static void bond_miimon_commit(struct bonding *bond)
2619{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002620 struct list_head *iter;
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002621 struct slave *slave, *primary;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002622
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002623 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002624 switch (slave->link_new_state) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002625 case BOND_LINK_NOCHANGE:
Thomas Falcon12185df2019-07-16 17:25:10 -05002626 /* For 802.3ad mode, check current slave speed and
2627 * duplex again in case its port was disabled after
2628 * invalid speed/duplex reporting but recovered before
2629 * link monitoring could make a decision on the actual
2630 * link status
2631 */
2632 if (BOND_MODE(bond) == BOND_MODE_8023AD &&
2633 slave->link == BOND_LINK_UP)
2634 bond_3ad_adapter_speed_duplex_changed(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002635 continue;
2636
2637 case BOND_LINK_UP:
Andreas Bornad729bc92017-08-10 06:41:44 +02002638 if (bond_update_speed_duplex(slave) &&
2639 bond_needs_speed_duplex(bond)) {
Mahesh Bandewar3f3c2782017-04-03 18:38:39 -07002640 slave->link = BOND_LINK_DOWN;
Andreas Born11e9d782017-08-12 00:36:55 +02002641 if (net_ratelimit())
Jarod Wilsone2a74202019-06-07 10:59:29 -04002642 slave_warn(bond->dev, slave->dev,
2643 "failed to get link speed/duplex\n");
Mahesh Bandewarb5bf0f52017-03-27 11:37:37 -07002644 continue;
2645 }
Jiri Pirko5d397062015-12-03 12:12:19 +01002646 bond_set_slave_link_state(slave, BOND_LINK_UP,
2647 BOND_SLAVE_NOTIFY_NOW);
Veaceslav Falico8e603462014-02-18 07:48:46 +01002648 slave->last_link_up = jiffies;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002649
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002650 primary = rtnl_dereference(bond->primary_slave);
Veaceslav Falico01844092014-05-15 21:39:55 +02002651 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002652 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002653 bond_set_backup_slave(slave);
Veaceslav Falico01844092014-05-15 21:39:55 +02002654 } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002655 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002656 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002657 }
2658
Jarod Wilsone2a74202019-06-07 10:59:29 -04002659 slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
2660 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2661 slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002662
Tonghao Zhang7e878b62018-05-16 19:09:23 -07002663 bond_miimon_link_change(bond, slave, BOND_LINK_UP);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002664
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002665 if (!bond->curr_active_slave || slave == primary)
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002666 goto do_failover;
2667
2668 continue;
2669
2670 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002671 if (slave->link_failure_count < UINT_MAX)
2672 slave->link_failure_count++;
2673
Jiri Pirko5d397062015-12-03 12:12:19 +01002674 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
2675 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002676
Veaceslav Falico01844092014-05-15 21:39:55 +02002677 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
2678 BOND_MODE(bond) == BOND_MODE_8023AD)
dingtianhong5e5b0662014-02-26 11:05:22 +08002679 bond_set_slave_inactive_flags(slave,
2680 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002681
Jarod Wilsone2a74202019-06-07 10:59:29 -04002682 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002683
Tonghao Zhang7e878b62018-05-16 19:09:23 -07002684 bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002685
Eric Dumazet4740d632014-07-15 06:56:55 -07002686 if (slave == rcu_access_pointer(bond->curr_active_slave))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002687 goto do_failover;
2688
2689 continue;
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 default:
Jarod Wilsone2a74202019-06-07 10:59:29 -04002692 slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n",
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002693 slave->link_new_state);
2694 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002696 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002699do_failover:
Neil Hormane843fa52010-10-13 16:01:50 +00002700 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 bond_select_active_slave(bond);
Neil Hormane843fa52010-10-13 16:01:50 +00002702 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002703 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002704
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002705 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002708/* bond_mii_monitor
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002709 *
2710 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002711 * inspection, then (if inspection indicates something needs to be done)
2712 * an acquisition of appropriate locks followed by a commit phase to
2713 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002714 */
stephen hemminger6da67d22013-12-30 10:43:41 -08002715static void bond_mii_monitor(struct work_struct *work)
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002716{
2717 struct bonding *bond = container_of(work, struct bonding,
2718 mii_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002719 bool should_notify_peers = false;
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002720 bool commit;
David S. Miller1f2cd842013-10-28 00:11:22 -04002721 unsigned long delay;
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002722 struct slave *slave;
2723 struct list_head *iter;
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002724
David S. Miller1f2cd842013-10-28 00:11:22 -04002725 delay = msecs_to_jiffies(bond->params.miimon);
2726
2727 if (!bond_has_slaves(bond))
dingtianhong6b6c5262013-10-24 11:09:03 +08002728 goto re_arm;
dingtianhong6b6c5262013-10-24 11:09:03 +08002729
dingtianhong4cb4f972013-12-13 10:19:39 +08002730 rcu_read_lock();
Ben Hutchingsad246c92011-04-26 15:25:52 +00002731 should_notify_peers = bond_should_notify_peers(bond);
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002732 commit = !!bond_miimon_inspect(bond);
2733 if (bond->send_peer_notif) {
dingtianhong4cb4f972013-12-13 10:19:39 +08002734 rcu_read_unlock();
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002735 if (rtnl_trylock()) {
2736 bond->send_peer_notif--;
2737 rtnl_unlock();
2738 }
2739 } else {
2740 rcu_read_unlock();
2741 }
David S. Miller1f2cd842013-10-28 00:11:22 -04002742
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002743 if (commit) {
David S. Miller1f2cd842013-10-28 00:11:22 -04002744 /* Race avoidance with bond_close cancel of workqueue */
2745 if (!rtnl_trylock()) {
David S. Miller1f2cd842013-10-28 00:11:22 -04002746 delay = 1;
2747 should_notify_peers = false;
2748 goto re_arm;
2749 }
2750
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002751 bond_for_each_slave(bond, slave, iter) {
2752 bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER);
2753 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002754 bond_miimon_commit(bond);
2755
David S. Miller1f2cd842013-10-28 00:11:22 -04002756 rtnl_unlock(); /* might sleep, hold no other locks */
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002757 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002758
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002759re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002760 if (bond->params.miimon)
David S. Miller1f2cd842013-10-28 00:11:22 -04002761 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2762
David S. Miller1f2cd842013-10-28 00:11:22 -04002763 if (should_notify_peers) {
2764 if (!rtnl_trylock())
2765 return;
2766 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
2767 rtnl_unlock();
2768 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002769}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002770
Taehee Yooeff74232020-09-25 18:13:12 +00002771static int bond_upper_dev_walk(struct net_device *upper,
2772 struct netdev_nested_priv *priv)
David Ahernb3208b22016-10-17 19:15:45 -07002773{
Taehee Yooeff74232020-09-25 18:13:12 +00002774 __be32 ip = *(__be32 *)priv->data;
David Ahernb3208b22016-10-17 19:15:45 -07002775
2776 return ip == bond_confirm_addr(upper, 0, ip);
2777}
2778
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002779static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002780{
Taehee Yooeff74232020-09-25 18:13:12 +00002781 struct netdev_nested_priv priv = {
2782 .data = (void *)&ip,
2783 };
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002784 bool ret = false;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002785
Andy Gospodarekeaddcd72012-03-22 16:14:29 +00002786 if (ip == bond_confirm_addr(bond->dev, 0, ip))
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002787 return true;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002788
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002789 rcu_read_lock();
Taehee Yooeff74232020-09-25 18:13:12 +00002790 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv))
David Ahernb3208b22016-10-17 19:15:45 -07002791 ret = true;
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002792 rcu_read_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002793
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002794 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002795}
2796
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002797/* We go to the (large) trouble of VLAN tagging ARP frames because
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002798 * switches in VLAN mode (especially if ports are configured as
2799 * "native" to a VLAN) might not pass non-tagged frames.
2800 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002801static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
2802 __be32 src_ip, struct bond_vlan_tag *tags)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002803{
2804 struct sk_buff *skb;
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002805 struct bond_vlan_tag *outer_tag = tags;
Jarod Wilsone2a74202019-06-07 10:59:29 -04002806 struct net_device *slave_dev = slave->dev;
2807 struct net_device *bond_dev = slave->bond->dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002808
Jarod Wilsone2a74202019-06-07 10:59:29 -04002809 slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
2810 arp_op, &dest_ip, &src_ip);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002811
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002812 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2813 NULL, slave_dev->dev_addr, NULL);
2814
2815 if (!skb) {
dingtianhong4873ac32014-03-25 17:44:44 +08002816 net_err_ratelimited("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002817 return;
2818 }
dingtianhongfbd929f2014-03-25 17:44:43 +08002819
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002820 if (!tags || tags->vlan_proto == VLAN_N_VID)
2821 goto xmit;
2822
2823 tags++;
2824
Vlad Yasevich44a40852014-05-16 17:20:38 -04002825 /* Go through all the tags backwards and add them to the packet */
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002826 while (tags->vlan_proto != VLAN_N_VID) {
2827 if (!tags->vlan_id) {
2828 tags++;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002829 continue;
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002830 }
Vlad Yasevich44a40852014-05-16 17:20:38 -04002831
Jarod Wilsone2a74202019-06-07 10:59:29 -04002832 slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
2833 ntohs(outer_tag->vlan_proto), tags->vlan_id);
Jiri Pirko62749e22014-11-19 14:04:58 +01002834 skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
2835 tags->vlan_id);
Vlad Yasevich44a40852014-05-16 17:20:38 -04002836 if (!skb) {
2837 net_err_ratelimited("failed to insert inner VLAN tag\n");
2838 return;
2839 }
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002840
2841 tags++;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002842 }
2843 /* Set the outer tag */
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002844 if (outer_tag->vlan_id) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002845 slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
2846 ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
Jiri Pirkob4bef1b2014-11-19 14:04:57 +01002847 __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
2848 outer_tag->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002849 }
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002850
2851xmit:
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002852 arp_xmit(skb);
2853}
2854
Vlad Yasevich44a40852014-05-16 17:20:38 -04002855/* Validate the device path between the @start_dev and the @end_dev.
2856 * The path is valid if the @end_dev is reachable through device
2857 * stacking.
2858 * When the path is validated, collect any vlan information in the
2859 * path.
2860 */
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002861struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
2862 struct net_device *end_dev,
2863 int level)
Vlad Yasevich44a40852014-05-16 17:20:38 -04002864{
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002865 struct bond_vlan_tag *tags;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002866 struct net_device *upper;
2867 struct list_head *iter;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002868
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002869 if (start_dev == end_dev) {
Kees Cook6396bb22018-06-12 14:03:40 -07002870 tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC);
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002871 if (!tags)
2872 return ERR_PTR(-ENOMEM);
2873 tags[level].vlan_proto = VLAN_N_VID;
2874 return tags;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002875 }
2876
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002877 netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
2878 tags = bond_verify_device_path(upper, end_dev, level + 1);
2879 if (IS_ERR_OR_NULL(tags)) {
2880 if (IS_ERR(tags))
2881 return tags;
2882 continue;
2883 }
2884 if (is_vlan_dev(upper)) {
2885 tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
2886 tags[level].vlan_id = vlan_dev_vlan_id(upper);
2887 }
2888
2889 return tags;
2890 }
2891
2892 return NULL;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002893}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2896{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002897 struct rtable *rt;
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002898 struct bond_vlan_tag *tags;
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002899 __be32 *targets = bond->params.arp_targets, addr;
dingtianhongfbd929f2014-03-25 17:44:43 +08002900 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002902 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002903 slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
2904 __func__, &targets[i]);
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002905 tags = NULL;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002906
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002907 /* Find out through which dev should the packet go */
David S. Miller78fbfd82011-03-12 00:00:52 -05002908 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2909 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002910 if (IS_ERR(rt)) {
Veaceslav Falico28572762014-02-28 12:39:19 +01002911 /* there's no route to target - try to send arp
2912 * probe to generate any traffic (arp_validate=0)
2913 */
dingtianhong4873ac32014-03-25 17:44:44 +08002914 if (bond->params.arp_validate)
David Decotigny0a4fd8d2021-09-03 23:31:29 -07002915 pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
2916 bond->dev->name,
2917 &targets[i]);
Jarod Wilsone2a74202019-06-07 10:59:29 -04002918 bond_arp_send(slave, ARPOP_REQUEST, targets[i],
Vlad Yasevich44a40852014-05-16 17:20:38 -04002919 0, tags);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002920 continue;
2921 }
2922
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002923 /* bond device itself */
2924 if (rt->dst.dev == bond->dev)
2925 goto found;
2926
2927 rcu_read_lock();
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002928 tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002929 rcu_read_unlock();
2930
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002931 if (!IS_ERR_OR_NULL(tags))
Vlad Yasevich44a40852014-05-16 17:20:38 -04002932 goto found;
2933
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002934 /* Not our device - skip */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002935 slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
Veaceslav Falico76444f52014-07-15 19:35:58 +02002936 &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
Veaceslav Falico3e325822013-08-28 23:25:16 +02002937
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002938 ip_rt_put(rt);
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002939 continue;
2940
2941found:
2942 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
2943 ip_rt_put(rt);
Jarod Wilsone2a74202019-06-07 10:59:29 -04002944 bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
Dan Carpentera67eed52014-07-25 15:21:21 +03002945 kfree(tags);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002946 }
2947}
2948
Al Virod3bb52b2007-08-22 20:06:58 -04002949static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002950{
Veaceslav Falico8599b522013-06-24 11:49:34 +02002951 int i;
2952
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002953 if (!sip || !bond_has_this_ip(bond, tip)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002954 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
2955 __func__, &sip, &tip);
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002956 return;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002957 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002958
Veaceslav Falico8599b522013-06-24 11:49:34 +02002959 i = bond_get_targets_ip(bond->params.arp_targets, sip);
2960 if (i == -1) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002961 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
2962 __func__, &sip);
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002963 return;
2964 }
Veaceslav Falico49f17de2014-02-18 07:48:47 +01002965 slave->last_rx = jiffies;
Veaceslav Falico8599b522013-06-24 11:49:34 +02002966 slave->target_last_arp_rx[i] = jiffies;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002967}
2968
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +02002969int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
2970 struct slave *slave)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002971{
Eric Dumazetde063b72012-06-11 19:23:07 +00002972 struct arphdr *arp = (struct arphdr *)skb->data;
Jay Vosburgh21a75f02016-02-02 13:35:56 -08002973 struct slave *curr_active_slave, *curr_arp_slave;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002974 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002975 __be32 sip, tip;
Alexey Dobriyan6ade97d2017-09-26 23:12:28 +03002976 int is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
2977 unsigned int alen;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002978
Veaceslav Falicof2cb6912014-02-18 07:48:42 +01002979 if (!slave_do_arp_validate(bond, slave)) {
dingtianhongbedabf92014-05-07 22:10:20 +08002980 if ((slave_do_arp_validate_only(bond) && is_arp) ||
2981 !slave_do_arp_validate_only(bond))
Veaceslav Falico49f17de2014-02-18 07:48:47 +01002982 slave->last_rx = jiffies;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002983 return RX_HANDLER_ANOTHER;
Veaceslav Falicof2cb6912014-02-18 07:48:42 +01002984 } else if (!is_arp) {
2985 return RX_HANDLER_ANOTHER;
2986 }
Veaceslav Falico2c146102013-06-24 11:49:31 +02002987
Eric Dumazetde063b72012-06-11 19:23:07 +00002988 alen = arp_hdr_len(bond->dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002989
Jarod Wilsone2a74202019-06-07 10:59:29 -04002990 slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
2991 __func__, skb->dev->name);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002992
Eric Dumazetde063b72012-06-11 19:23:07 +00002993 if (alen > skb_headlen(skb)) {
2994 arp = kmalloc(alen, GFP_ATOMIC);
2995 if (!arp)
2996 goto out_unlock;
2997 if (skb_copy_bits(skb, 0, arp, alen) < 0)
2998 goto out_unlock;
2999 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003000
Jiri Pirko3aba8912011-04-19 03:48:16 +00003001 if (arp->ar_hln != bond->dev->addr_len ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003002 skb->pkt_type == PACKET_OTHERHOST ||
3003 skb->pkt_type == PACKET_LOOPBACK ||
3004 arp->ar_hrd != htons(ARPHRD_ETHER) ||
3005 arp->ar_pro != htons(ETH_P_IP) ||
3006 arp->ar_pln != 4)
3007 goto out_unlock;
3008
3009 arp_ptr = (unsigned char *)(arp + 1);
Jiri Pirko3aba8912011-04-19 03:48:16 +00003010 arp_ptr += bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003011 memcpy(&sip, arp_ptr, 4);
Jiri Pirko3aba8912011-04-19 03:48:16 +00003012 arp_ptr += 4 + bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003013 memcpy(&tip, arp_ptr, 4);
3014
Jarod Wilsone2a74202019-06-07 10:59:29 -04003015 slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
3016 __func__, slave->dev->name, bond_slave_state(slave),
3017 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
3018 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003019
Veaceslav Falico010d3c32014-02-20 12:07:57 +01003020 curr_active_slave = rcu_dereference(bond->curr_active_slave);
Jay Vosburgh21a75f02016-02-02 13:35:56 -08003021 curr_arp_slave = rcu_dereference(bond->current_arp_slave);
Veaceslav Falico010d3c32014-02-20 12:07:57 +01003022
Jay Vosburgh21a75f02016-02-02 13:35:56 -08003023 /* We 'trust' the received ARP enough to validate it if:
Veaceslav Falicoaeea64a2013-06-24 11:49:32 +02003024 *
Jay Vosburgh21a75f02016-02-02 13:35:56 -08003025 * (a) the slave receiving the ARP is active (which includes the
3026 * current ARP slave, if any), or
3027 *
3028 * (b) the receiving slave isn't active, but there is a currently
3029 * active slave and it received valid arp reply(s) after it became
3030 * the currently active slave, or
3031 *
3032 * (c) there is an ARP slave that sent an ARP during the prior ARP
3033 * interval, and we receive an ARP reply on any slave. We accept
3034 * these because switch FDB update delays may deliver the ARP
3035 * reply to a slave other than the sender of the ARP request.
3036 *
3037 * Note: for (b), backup slaves are receiving the broadcast ARP
3038 * request, not a reply. This request passes from the sending
3039 * slave through the L2 switch(es) to the receiving slave. Since
3040 * this is checking the request, sip/tip are swapped for
3041 * validation.
3042 *
3043 * This is done to avoid endless looping when we can't reach the
Veaceslav Falicoaeea64a2013-06-24 11:49:32 +02003044 * arp_ip_target and fool ourselves with our own arp requests.
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003045 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003046 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003047 bond_validate_arp(bond, slave, sip, tip);
Veaceslav Falico010d3c32014-02-20 12:07:57 +01003048 else if (curr_active_slave &&
3049 time_after(slave_last_rx(bond, curr_active_slave),
3050 curr_active_slave->last_link_up))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003051 bond_validate_arp(bond, slave, tip, sip);
Jay Vosburgh21a75f02016-02-02 13:35:56 -08003052 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
3053 bond_time_in_interval(bond,
3054 dev_trans_start(curr_arp_slave->dev), 1))
3055 bond_validate_arp(bond, slave, sip, tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003056
3057out_unlock:
Eric Dumazetde063b72012-06-11 19:23:07 +00003058 if (arp != (struct arphdr *)skb->data)
3059 kfree(arp);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00003060 return RX_HANDLER_ANOTHER;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07003061}
3062
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003063/* function to verify if we're in the arp_interval timeslice, returns true if
3064 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
3065 * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
3066 */
3067static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
3068 int mod)
3069{
3070 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3071
3072 return time_in_range(jiffies,
3073 last_act - delta_in_ticks,
3074 last_act + mod * delta_in_ticks + delta_in_ticks/2);
3075}
3076
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003077/* This function is called regularly to monitor each slave's link
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 * ensuring that traffic is being sent and received when arp monitoring
3079 * is used in load-balancing mode. if the adapter has been dormant, then an
3080 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
3081 * arp monitoring in active backup mode.
3082 */
Mahesh Bandeward5e73f72017-03-08 10:55:51 -08003083static void bond_loadbalance_arp_mon(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct slave *slave, *oldcurrent;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003086 struct list_head *iter;
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003087 int do_failover = 0, slave_state_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
David S. Miller1f2cd842013-10-28 00:11:22 -04003089 if (!bond_has_slaves(bond))
dingtianhong7f1bb572013-10-24 11:09:17 +08003090 goto re_arm;
dingtianhong7f1bb572013-10-24 11:09:17 +08003091
dingtianhong2e52f4f2013-12-13 10:19:50 +08003092 rcu_read_lock();
3093
Eric Dumazet4740d632014-07-15 06:56:55 -07003094 oldcurrent = rcu_dereference(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 /* see if any of the previous devices are up now (i.e. they have
3096 * xmt and rcv traffic). the curr_active_slave does not come into
Veaceslav Falico8e603462014-02-18 07:48:46 +01003097 * the picture unless it is null. also, slave->last_link_up is not
3098 * needed here because we send an arp on each slave and give a slave
3099 * as long as it needs to get the tx/rx within the delta.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 * TODO: what about up/down delay in arp mode? it wasn't here before
3101 * so it can wait
3102 */
dingtianhong2e52f4f2013-12-13 10:19:50 +08003103 bond_for_each_slave_rcu(bond, slave, iter) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003104 unsigned long trans_start = dev_trans_start(slave->dev);
3105
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003106 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Nithin Sujir797a9362017-05-24 19:45:17 -07003107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if (slave->link != BOND_LINK_UP) {
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003109 if (bond_time_in_interval(bond, trans_start, 1) &&
Veaceslav Falico49f17de2014-02-18 07:48:47 +01003110 bond_time_in_interval(bond, slave->last_rx, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003112 bond_propose_link_state(slave, BOND_LINK_UP);
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003113 slave_state_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* primary_slave has no meaning in round-robin
3116 * mode. the window of a slave being up and
3117 * curr_active_slave being null after enslaving
3118 * is closed.
3119 */
3120 if (!oldcurrent) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04003121 slave_info(bond->dev, slave->dev, "link status definitely up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 do_failover = 1;
3123 } else {
Jarod Wilsone2a74202019-06-07 10:59:29 -04003124 slave_info(bond->dev, slave->dev, "interface is now up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 }
3127 } else {
3128 /* slave->link == BOND_LINK_UP */
3129
3130 /* not all switches will respond to an arp request
3131 * when the source ip is 0, so don't take the link down
3132 * if we don't know our ip yet
3133 */
Hangbin Liu5944b5a2021-11-30 12:29:47 +08003134 if (!bond_time_in_interval(bond, trans_start, bond->params.missed_max) ||
3135 !bond_time_in_interval(bond, slave->last_rx, bond->params.missed_max)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003137 bond_propose_link_state(slave, BOND_LINK_DOWN);
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003138 slave_state_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003140 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Jarod Wilsone2a74202019-06-07 10:59:29 -04003143 slave_info(bond->dev, slave->dev, "interface is now down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003145 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
3148 }
3149
3150 /* note: if switch is in round-robin mode, all links
3151 * must tx arp to ensure all links rx an arp - otherwise
3152 * links may oscillate or not come up at all; if switch is
3153 * in something like xor mode, there is nothing we can
3154 * do - all replies will be rx'ed on same link causing slaves
3155 * to be unstable during low/no traffic periods
3156 */
Veaceslav Falicob6adc612014-05-15 21:39:57 +02003157 if (bond_slave_is_up(slave))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160
dingtianhong2e52f4f2013-12-13 10:19:50 +08003161 rcu_read_unlock();
3162
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003163 if (do_failover || slave_state_changed) {
dingtianhong2e52f4f2013-12-13 10:19:50 +08003164 if (!rtnl_trylock())
3165 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Nithin Sujir797a9362017-05-24 19:45:17 -07003167 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003168 if (slave->link_new_state != BOND_LINK_NOCHANGE)
3169 slave->link = slave->link_new_state;
Nithin Sujir797a9362017-05-24 19:45:17 -07003170 }
3171
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003172 if (slave_state_changed) {
3173 bond_slave_state_change(bond);
Mahesh Bandewaree637712014-10-04 17:45:01 -07003174 if (BOND_MODE(bond) == BOND_MODE_XOR)
3175 bond_update_slave_arr(bond, NULL);
Nikolay Aleksandrovb8e45002014-11-18 15:14:44 +01003176 }
3177 if (do_failover) {
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003178 block_netpoll_tx();
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003179 bond_select_active_slave(bond);
Ding Tianhong6fde8f02014-01-28 11:48:53 +08003180 unblock_netpoll_tx();
3181 }
dingtianhong2e52f4f2013-12-13 10:19:50 +08003182 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184
3185re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003186 if (bond->params.arp_interval)
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003187 queue_delayed_work(bond->wq, &bond->arp_work,
3188 msecs_to_jiffies(bond->params.arp_interval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003191/* Called to inspect slaves for active-backup mode ARP monitor link state
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003192 * changes. Sets proposed link state in slaves to specify what action
3193 * should take place for the slave. Returns 0 if no changes are found, >0
3194 * if changes to link states must be committed.
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003195 *
Nikolay Aleksandrov8c0bc552014-09-11 22:49:28 +02003196 * Called with rcu_read_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 */
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003198static int bond_ab_arp_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Veaceslav Falicodef44602013-08-03 03:50:35 +02003200 unsigned long trans_start, last_rx;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003201 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02003202 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02003203 int commit = 0;
Jiri Bohacda210f52012-08-30 12:02:47 +00003204
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003205 bond_for_each_slave_rcu(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003206 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Veaceslav Falicodef44602013-08-03 03:50:35 +02003207 last_rx = slave_last_rx(bond, slave);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 if (slave->link != BOND_LINK_UP) {
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003210 if (bond_time_in_interval(bond, last_rx, 1)) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003211 bond_propose_link_state(slave, BOND_LINK_UP);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003212 commit++;
Jiri Wiesner0410d072020-08-16 20:52:44 +02003213 } else if (slave->link == BOND_LINK_BACK) {
3214 bond_propose_link_state(slave, BOND_LINK_FAIL);
3215 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003217 continue;
3218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003220 /* Give slaves 2*delta after being enslaved or made
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003221 * active. This avoids bouncing, as the last receive
3222 * times need a full ARP monitor cycle to be updated.
3223 */
Veaceslav Falico8e603462014-02-18 07:48:46 +01003224 if (bond_time_in_interval(bond, slave->last_link_up, 2))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003225 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003227 /* Backup slave is down if:
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003228 * - No current_arp_slave AND
Hangbin Liu5944b5a2021-11-30 12:29:47 +08003229 * - more than (missed_max+1)*delta since last receive AND
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003230 * - the bond has an IP address
3231 *
3232 * Note: a non-null current_arp_slave indicates
3233 * the curr_active_slave went down and we are
3234 * searching for a new one; under this condition
3235 * we only take the curr_active_slave down - this
3236 * gives each slave a chance to tx/rx traffic
3237 * before being taken out
3238 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003239 if (!bond_is_active_slave(slave) &&
Eric Dumazet85741712014-07-15 06:56:56 -07003240 !rcu_access_pointer(bond->current_arp_slave) &&
Hangbin Liu5944b5a2021-11-30 12:29:47 +08003241 !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003242 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003243 commit++;
3244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003246 /* Active slave is down if:
Hangbin Liu5944b5a2021-11-30 12:29:47 +08003247 * - more than missed_max*delta since transmitting OR
3248 * - (more than missed_max*delta since receive AND
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003249 * the bond has an IP address)
3250 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003251 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00003252 if (bond_is_active_slave(slave) &&
Hangbin Liu5944b5a2021-11-30 12:29:47 +08003253 (!bond_time_in_interval(bond, trans_start, bond->params.missed_max) ||
3254 !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003255 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003256 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 }
3259
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003260 return commit;
3261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003263/* Called to commit link state changes noted by inspection step of
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003264 * active-backup mode ARP monitor.
3265 *
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003266 * Called with RTNL hold.
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003267 */
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003268static void bond_ab_arp_commit(struct bonding *bond)
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003269{
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003270 unsigned long trans_start;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003271 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02003272 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003274 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003275 switch (slave->link_new_state) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003276 case BOND_LINK_NOCHANGE:
3277 continue;
3278
3279 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003280 trans_start = dev_trans_start(slave->dev);
Eric Dumazet4740d632014-07-15 06:56:55 -07003281 if (rtnl_dereference(bond->curr_active_slave) != slave ||
3282 (!rtnl_dereference(bond->curr_active_slave) &&
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003283 bond_time_in_interval(bond, trans_start, 1))) {
Eric Dumazet85741712014-07-15 06:56:56 -07003284 struct slave *current_arp_slave;
3285
3286 current_arp_slave = rtnl_dereference(bond->current_arp_slave);
Jiri Pirko5d397062015-12-03 12:12:19 +01003287 bond_set_slave_link_state(slave, BOND_LINK_UP,
3288 BOND_SLAVE_NOTIFY_NOW);
Eric Dumazet85741712014-07-15 06:56:56 -07003289 if (current_arp_slave) {
Veaceslav Falico5a4309742012-04-05 03:47:43 +00003290 bond_set_slave_inactive_flags(
Eric Dumazet85741712014-07-15 06:56:56 -07003291 current_arp_slave,
dingtianhong5e5b0662014-02-26 11:05:22 +08003292 BOND_SLAVE_NOTIFY_NOW);
Eric Dumazet85741712014-07-15 06:56:56 -07003293 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
Veaceslav Falico5a4309742012-04-05 03:47:43 +00003294 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003295
Jarod Wilsone2a74202019-06-07 10:59:29 -04003296 slave_info(bond->dev, slave->dev, "link status definitely up\n");
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003297
Eric Dumazet4740d632014-07-15 06:56:55 -07003298 if (!rtnl_dereference(bond->curr_active_slave) ||
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003299 slave == rtnl_dereference(bond->primary_slave))
Jiri Pirkob9f60252009-08-31 11:09:38 +00003300 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003301
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003302 }
3303
Jiri Pirkob9f60252009-08-31 11:09:38 +00003304 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003305
3306 case BOND_LINK_DOWN:
3307 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Jiri Pirko5d397062015-12-03 12:12:19 +01003310 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3311 BOND_SLAVE_NOTIFY_NOW);
dingtianhong5e5b0662014-02-26 11:05:22 +08003312 bond_set_slave_inactive_flags(slave,
3313 BOND_SLAVE_NOTIFY_NOW);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003314
Jarod Wilsone2a74202019-06-07 10:59:29 -04003315 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003316
Eric Dumazet4740d632014-07-15 06:56:55 -07003317 if (slave == rtnl_dereference(bond->curr_active_slave)) {
Eric Dumazet85741712014-07-15 06:56:56 -07003318 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003319 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003320 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003321
3322 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003323
Jiri Wiesner0410d072020-08-16 20:52:44 +02003324 case BOND_LINK_FAIL:
3325 bond_set_slave_link_state(slave, BOND_LINK_FAIL,
3326 BOND_SLAVE_NOTIFY_NOW);
3327 bond_set_slave_inactive_flags(slave,
3328 BOND_SLAVE_NOTIFY_NOW);
3329
3330 /* A slave has just been enslaved and has become
3331 * the current active slave.
3332 */
3333 if (rtnl_dereference(bond->curr_active_slave))
3334 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3335 continue;
3336
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003337 default:
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003338 slave_err(bond->dev, slave->dev,
3339 "impossible: link_new_state %d on slave\n",
3340 slave->link_new_state);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003341 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Jiri Pirkob9f60252009-08-31 11:09:38 +00003344do_failover:
Neil Hormane843fa52010-10-13 16:01:50 +00003345 block_netpoll_tx();
Jiri Pirkob9f60252009-08-31 11:09:38 +00003346 bond_select_active_slave(bond);
Neil Hormane843fa52010-10-13 16:01:50 +00003347 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003348 }
3349
3350 bond_set_carrier(bond);
3351}
3352
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003353/* Send ARP probes for active-backup mode ARP monitor.
dingtianhongb0929912014-02-26 11:05:23 +08003354 *
Nikolay Aleksandrov8c0bc552014-09-11 22:49:28 +02003355 * Called with rcu_read_lock held.
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003356 */
Veaceslav Falicof2ebd472014-01-27 14:37:32 +01003357static bool bond_ab_arp_probe(struct bonding *bond)
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003358{
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003359 struct slave *slave, *before = NULL, *new_slave = NULL,
dingtianhongb0929912014-02-26 11:05:23 +08003360 *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
3361 *curr_active_slave = rcu_dereference(bond->curr_active_slave);
Veaceslav Falico4087df872013-09-25 09:20:19 +02003362 struct list_head *iter;
3363 bool found = false;
dingtianhongb0929912014-02-26 11:05:23 +08003364 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
Veaceslav Falicof2ebd472014-01-27 14:37:32 +01003365
Veaceslav Falico98b90f22014-01-27 14:37:31 +01003366 if (curr_arp_slave && curr_active_slave)
Veaceslav Falico76444f52014-07-15 19:35:58 +02003367 netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
3368 curr_arp_slave->dev->name,
3369 curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003370
Veaceslav Falico98b90f22014-01-27 14:37:31 +01003371 if (curr_active_slave) {
3372 bond_arp_send_all(bond, curr_active_slave);
dingtianhongb0929912014-02-26 11:05:23 +08003373 return should_notify_rtnl;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003374 }
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 /* if we don't have a curr_active_slave, search for the next available
3377 * backup slave from the current_arp_slave and make it the candidate
3378 * for becoming the curr_active_slave
3379 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003380
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003381 if (!curr_arp_slave) {
dingtianhongb0929912014-02-26 11:05:23 +08003382 curr_arp_slave = bond_first_slave_rcu(bond);
3383 if (!curr_arp_slave)
3384 return should_notify_rtnl;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003385 }
3386
dingtianhongb0929912014-02-26 11:05:23 +08003387 bond_for_each_slave_rcu(bond, slave, iter) {
Veaceslav Falicob6adc612014-05-15 21:39:57 +02003388 if (!found && !before && bond_slave_is_up(slave))
Veaceslav Falico4087df872013-09-25 09:20:19 +02003389 before = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Veaceslav Falicob6adc612014-05-15 21:39:57 +02003391 if (found && !new_slave && bond_slave_is_up(slave))
Veaceslav Falico4087df872013-09-25 09:20:19 +02003392 new_slave = slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003393 /* if the link state is up at this point, we
3394 * mark it down - this can happen if we have
3395 * simultaneous link failures and
3396 * reselect_active_interface doesn't make this
3397 * one the current slave so it is still marked
3398 * up when it is actually down
3399 */
Veaceslav Falicob6adc612014-05-15 21:39:57 +02003400 if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
Jiri Pirko5d397062015-12-03 12:12:19 +01003401 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3402 BOND_SLAVE_NOTIFY_LATER);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003403 if (slave->link_failure_count < UINT_MAX)
3404 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
dingtianhong5e5b0662014-02-26 11:05:22 +08003406 bond_set_slave_inactive_flags(slave,
dingtianhongb0929912014-02-26 11:05:23 +08003407 BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Jarod Wilsone2a74202019-06-07 10:59:29 -04003409 slave_info(bond->dev, slave->dev, "backup interface is now down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 }
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003411 if (slave == curr_arp_slave)
Veaceslav Falico4087df872013-09-25 09:20:19 +02003412 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
Veaceslav Falico4087df872013-09-25 09:20:19 +02003414
3415 if (!new_slave && before)
3416 new_slave = before;
3417
dingtianhongb0929912014-02-26 11:05:23 +08003418 if (!new_slave)
3419 goto check_state;
Veaceslav Falico4087df872013-09-25 09:20:19 +02003420
Jiri Pirko5d397062015-12-03 12:12:19 +01003421 bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
3422 BOND_SLAVE_NOTIFY_LATER);
dingtianhongb0929912014-02-26 11:05:23 +08003423 bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
Veaceslav Falico4087df872013-09-25 09:20:19 +02003424 bond_arp_send_all(bond, new_slave);
Veaceslav Falico8e603462014-02-18 07:48:46 +01003425 new_slave->last_link_up = jiffies;
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003426 rcu_assign_pointer(bond->current_arp_slave, new_slave);
Veaceslav Falicof2ebd472014-01-27 14:37:32 +01003427
dingtianhongb0929912014-02-26 11:05:23 +08003428check_state:
3429 bond_for_each_slave_rcu(bond, slave, iter) {
Jiri Pirko5d397062015-12-03 12:12:19 +01003430 if (slave->should_notify || slave->should_notify_link) {
dingtianhongb0929912014-02-26 11:05:23 +08003431 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
3432 break;
3433 }
3434 }
3435 return should_notify_rtnl;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003436}
3437
Mahesh Bandeward5e73f72017-03-08 10:55:51 -08003438static void bond_activebackup_arp_mon(struct bonding *bond)
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003439{
dingtianhongb0929912014-02-26 11:05:23 +08003440 bool should_notify_peers = false;
3441 bool should_notify_rtnl = false;
David S. Miller1f2cd842013-10-28 00:11:22 -04003442 int delta_in_ticks;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003443
David S. Miller1f2cd842013-10-28 00:11:22 -04003444 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3445
3446 if (!bond_has_slaves(bond))
dingtianhong80b9d232013-10-24 11:09:25 +08003447 goto re_arm;
dingtianhong80b9d232013-10-24 11:09:25 +08003448
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003449 rcu_read_lock();
Ben Hutchingsad246c92011-04-26 15:25:52 +00003450
dingtianhongb0929912014-02-26 11:05:23 +08003451 should_notify_peers = bond_should_notify_peers(bond);
3452
3453 if (bond_ab_arp_inspect(bond)) {
3454 rcu_read_unlock();
3455
David S. Miller1f2cd842013-10-28 00:11:22 -04003456 /* Race avoidance with bond_close flush of workqueue */
3457 if (!rtnl_trylock()) {
David S. Miller1f2cd842013-10-28 00:11:22 -04003458 delta_in_ticks = 1;
3459 should_notify_peers = false;
3460 goto re_arm;
3461 }
3462
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003463 bond_ab_arp_commit(bond);
dingtianhongb0929912014-02-26 11:05:23 +08003464
David S. Miller1f2cd842013-10-28 00:11:22 -04003465 rtnl_unlock();
dingtianhongb0929912014-02-26 11:05:23 +08003466 rcu_read_lock();
David S. Miller1f2cd842013-10-28 00:11:22 -04003467 }
3468
dingtianhongb0929912014-02-26 11:05:23 +08003469 should_notify_rtnl = bond_ab_arp_probe(bond);
3470 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
3472re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003473 if (bond->params.arp_interval)
David S. Miller1f2cd842013-10-28 00:11:22 -04003474 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3475
dingtianhongb0929912014-02-26 11:05:23 +08003476 if (should_notify_peers || should_notify_rtnl) {
David S. Miller1f2cd842013-10-28 00:11:22 -04003477 if (!rtnl_trylock())
3478 return;
dingtianhongb0929912014-02-26 11:05:23 +08003479
3480 if (should_notify_peers)
3481 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
3482 bond->dev);
Jiri Pirko5d397062015-12-03 12:12:19 +01003483 if (should_notify_rtnl) {
dingtianhongb0929912014-02-26 11:05:23 +08003484 bond_slave_state_notify(bond);
Jiri Pirko5d397062015-12-03 12:12:19 +01003485 bond_slave_link_notify(bond);
3486 }
dingtianhongb0929912014-02-26 11:05:23 +08003487
David S. Miller1f2cd842013-10-28 00:11:22 -04003488 rtnl_unlock();
3489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490}
3491
Mahesh Bandeward5e73f72017-03-08 10:55:51 -08003492static void bond_arp_monitor(struct work_struct *work)
3493{
3494 struct bonding *bond = container_of(work, struct bonding,
3495 arp_work.work);
3496
3497 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
3498 bond_activebackup_arp_mon(bond);
3499 else
3500 bond_loadbalance_arp_mon(bond);
3501}
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503/*-------------------------- netdev event handling --------------------------*/
3504
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003505/* Change device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506static int bond_event_changename(struct bonding *bond)
3507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 bond_remove_proc_entry(bond);
3509 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003510
Taku Izumif073c7c2010-12-09 15:17:13 +00003511 bond_debug_reregister(bond);
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return NOTIFY_DONE;
3514}
3515
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003516static int bond_master_netdev_event(unsigned long event,
3517 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
Wang Chen454d7c92008-11-12 23:37:49 -08003519 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Jarod Wilsone2a74202019-06-07 10:59:29 -04003521 netdev_dbg(bond_dev, "%s called\n", __func__);
3522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 switch (event) {
3524 case NETDEV_CHANGENAME:
3525 return bond_event_changename(event_bond);
Eric W. Biedermana64d49c2012-07-09 10:51:45 +00003526 case NETDEV_UNREGISTER:
3527 bond_remove_proc_entry(event_bond);
Mahesh Bandewar5b698742021-07-16 16:09:41 -07003528#ifdef CONFIG_XFRM_OFFLOAD
Taehee Yoo9a560552021-07-05 15:38:12 +00003529 xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
Mahesh Bandewar5b698742021-07-16 16:09:41 -07003530#endif /* CONFIG_XFRM_OFFLOAD */
Eric W. Biedermana64d49c2012-07-09 10:51:45 +00003531 break;
3532 case NETDEV_REGISTER:
3533 bond_create_proc_entry(event_bond);
3534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 default:
3536 break;
3537 }
3538
3539 return NOTIFY_DONE;
3540}
3541
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003542static int bond_slave_netdev_event(unsigned long event,
3543 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003545 struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
nikolay@redhat.com61013912013-04-11 09:18:55 +00003546 struct bonding *bond;
3547 struct net_device *bond_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
nikolay@redhat.com61013912013-04-11 09:18:55 +00003549 /* A netdev event can be generated while enslaving a device
3550 * before netdev_rx_handler_register is called in which case
3551 * slave will be NULL
3552 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04003553 if (!slave) {
3554 netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
nikolay@redhat.com61013912013-04-11 09:18:55 +00003555 return NOTIFY_DONE;
Jarod Wilsone2a74202019-06-07 10:59:29 -04003556 }
3557
nikolay@redhat.com61013912013-04-11 09:18:55 +00003558 bond_dev = slave->bond->dev;
3559 bond = slave->bond;
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003560 primary = rtnl_dereference(bond->primary_slave);
nikolay@redhat.com61013912013-04-11 09:18:55 +00003561
Jarod Wilsone2a74202019-06-07 10:59:29 -04003562 slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
3563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 switch (event) {
3565 case NETDEV_UNREGISTER:
nikolay@redhat.com8d2ada72013-06-26 17:13:37 +02003566 if (bond_dev->type != ARPHRD_ETHER)
Jiri Pirko471cb5a2013-01-03 22:49:01 +00003567 bond_release_and_destroy(bond_dev, slave_dev);
3568 else
WANG Congf51048c2017-07-06 15:01:57 -07003569 __bond_release_one(bond_dev, slave_dev, false, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 break;
Weiping Pan98f41f62011-10-31 17:20:48 +00003571 case NETDEV_UP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 case NETDEV_CHANGE:
Mahesh Bandewar4d2c0cd2017-09-27 18:03:49 -07003573 /* For 802.3ad mode only:
3574 * Getting invalid Speed/Duplex values here will put slave
Jarod Wilson33403122019-05-24 09:49:28 -04003575 * in weird state. Mark it as link-fail if the link was
3576 * previously up or link-down if it hasn't yet come up, and
3577 * let link-monitoring (miimon) set it right when correct
3578 * speeds/duplex are available.
Mahesh Bandewar4d2c0cd2017-09-27 18:03:49 -07003579 */
3580 if (bond_update_speed_duplex(slave) &&
Jarod Wilson33403122019-05-24 09:49:28 -04003581 BOND_MODE(bond) == BOND_MODE_8023AD) {
3582 if (slave->last_link_up)
3583 slave->link = BOND_LINK_FAIL;
3584 else
3585 slave->link = BOND_LINK_DOWN;
3586 }
Mahesh Bandewar4d2c0cd2017-09-27 18:03:49 -07003587
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07003588 if (BOND_MODE(bond) == BOND_MODE_8023AD)
3589 bond_3ad_adapter_speed_duplex_changed(slave);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003590 fallthrough;
Mahesh Bandewar950ddcb2015-02-19 10:13:25 -08003591 case NETDEV_DOWN:
Mahesh Bandewaree637712014-10-04 17:45:01 -07003592 /* Refresh slave-array if applicable!
3593 * If the setup does not use miimon or arpmon (mode-specific!),
3594 * then these events will not cause the slave-array to be
3595 * refreshed. This will cause xmit to use a slave that is not
3596 * usable. Avoid such situation by refeshing the array at these
3597 * events. If these (miimon/arpmon) parameters are configured
3598 * then array gets refreshed twice and that should be fine!
3599 */
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04003600 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07003601 bond_update_slave_arr(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 case NETDEV_CHANGEMTU:
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003604 /* TODO: Should slaves be allowed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 * independently alter their MTU? For
3606 * an active-backup bond, slaves need
3607 * not be the same type of device, so
3608 * MTUs may vary. For other modes,
3609 * slaves arguably should have the
3610 * same MTUs. To do this, we'd need to
3611 * take over the slave's change_mtu
3612 * function for the duration of their
3613 * servitude.
3614 */
3615 break;
3616 case NETDEV_CHANGENAME:
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003617 /* we don't care if we don't have primary set */
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02003618 if (!bond_uses_primary(bond) ||
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003619 !bond->params.primary[0])
3620 break;
3621
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003622 if (slave == primary) {
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003623 /* slave's name changed - he's no longer primary */
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003624 RCU_INIT_POINTER(bond->primary_slave, NULL);
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003625 } else if (!strcmp(slave_dev->name, bond->params.primary)) {
3626 /* we have a new primary slave */
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003627 rcu_assign_pointer(bond->primary_slave, slave);
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003628 } else { /* we didn't change primary - exit */
3629 break;
3630 }
3631
Veaceslav Falico76444f52014-07-15 19:35:58 +02003632 netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003633 primary ? slave_dev->name : "none");
dingtianhongf80889a2014-02-12 12:06:40 +08003634
3635 block_netpoll_tx();
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003636 bond_select_active_slave(bond);
dingtianhongf80889a2014-02-12 12:06:40 +08003637 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003639 case NETDEV_FEAT_CHANGE:
3640 bond_compute_features(bond);
3641 break;
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003642 case NETDEV_RESEND_IGMP:
3643 /* Propagate to master device */
3644 call_netdevice_notifiers(event, slave->bond->dev);
3645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 default:
3647 break;
3648 }
3649
3650 return NOTIFY_DONE;
3651}
3652
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003653/* bond_netdev_event: handle netdev notifier chain events.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 *
3655 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003656 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 * locks for us to safely manipulate the slave devices (RTNL lock,
3658 * dev_probe_lock).
3659 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003660static int bond_netdev_event(struct notifier_block *this,
3661 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Jiri Pirko351638e2013-05-28 01:30:21 +00003663 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Jarod Wilson75466dc2019-06-07 10:59:26 -04003665 netdev_dbg(event_dev, "%s received %s\n",
3666 __func__, netdev_cmd_to_name(event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003668 if (!(event_dev->priv_flags & IFF_BONDING))
3669 return NOTIFY_DONE;
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 if (event_dev->flags & IFF_MASTER) {
Sabrina Dubroca92480b32019-04-12 15:04:10 +02003672 int ret;
3673
Sabrina Dubroca92480b32019-04-12 15:04:10 +02003674 ret = bond_master_netdev_event(event, event_dev);
3675 if (ret != NOTIFY_DONE)
3676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 }
3678
Jarod Wilsone2a74202019-06-07 10:59:29 -04003679 if (event_dev->flags & IFF_SLAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 return bond_slave_netdev_event(event, event_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 return NOTIFY_DONE;
3683}
3684
3685static struct notifier_block bond_netdev_notifier = {
3686 .notifier_call = bond_netdev_event,
3687};
3688
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003689/*---------------------------- Hashing Policies -----------------------------*/
3690
Jussi Makia815bde2021-07-31 05:57:32 +00003691/* Helper to access data in a packet, with or without a backing skb.
3692 * If skb is given the data is linearized if necessary via pskb_may_pull.
3693 */
3694static inline const void *bond_pull_data(struct sk_buff *skb,
3695 const void *data, int hlen, int n)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003696{
Jussi Makia815bde2021-07-31 05:57:32 +00003697 if (likely(n <= hlen))
3698 return data;
3699 else if (skb && likely(pskb_may_pull(skb, n)))
3700 return skb->head;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003701
Jussi Makia815bde2021-07-31 05:57:32 +00003702 return NULL;
John Eaglesham6b923cb2012-08-21 20:43:35 +00003703}
3704
Jussi Makia815bde2021-07-31 05:57:32 +00003705/* L2 hash helper */
3706static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
3707{
3708 struct ethhdr *ep;
3709
3710 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
3711 if (!data)
3712 return 0;
3713
3714 ep = (struct ethhdr *)(data + mhoff);
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00003715 return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto);
Jussi Makia815bde2021-07-31 05:57:32 +00003716}
3717
3718static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data,
3719 int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34)
Matteo Crocedf98be02019-11-15 12:10:37 +01003720{
3721 const struct ipv6hdr *iph6;
3722 const struct iphdr *iph;
3723
Jussi Makia815bde2021-07-31 05:57:32 +00003724 if (l2_proto == htons(ETH_P_IP)) {
3725 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph));
3726 if (!data)
Matteo Crocedf98be02019-11-15 12:10:37 +01003727 return false;
Jussi Makia815bde2021-07-31 05:57:32 +00003728
3729 iph = (const struct iphdr *)(data + *nhoff);
Matteo Crocedf98be02019-11-15 12:10:37 +01003730 iph_to_flow_copy_v4addrs(fk, iph);
Jussi Makia815bde2021-07-31 05:57:32 +00003731 *nhoff += iph->ihl << 2;
Matteo Crocedf98be02019-11-15 12:10:37 +01003732 if (!ip_is_fragment(iph))
Jussi Makia815bde2021-07-31 05:57:32 +00003733 *ip_proto = iph->protocol;
3734 } else if (l2_proto == htons(ETH_P_IPV6)) {
3735 data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6));
3736 if (!data)
Matteo Crocedf98be02019-11-15 12:10:37 +01003737 return false;
Jussi Makia815bde2021-07-31 05:57:32 +00003738
3739 iph6 = (const struct ipv6hdr *)(data + *nhoff);
Matteo Crocedf98be02019-11-15 12:10:37 +01003740 iph_to_flow_copy_v6addrs(fk, iph6);
Jussi Makia815bde2021-07-31 05:57:32 +00003741 *nhoff += sizeof(*iph6);
3742 *ip_proto = iph6->nexthdr;
Matteo Crocedf98be02019-11-15 12:10:37 +01003743 } else {
3744 return false;
3745 }
3746
Jussi Makia815bde2021-07-31 05:57:32 +00003747 if (l34 && *ip_proto >= 0)
3748 fk->ports.ports = __skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen);
Matteo Crocedf98be02019-11-15 12:10:37 +01003749
3750 return true;
3751}
3752
Jussi Makia815bde2021-07-31 05:57:32 +00003753static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
Jarod Wilson7b8fc012021-01-18 20:09:27 -05003754{
Jarod Wilson7b8fc012021-01-18 20:09:27 -05003755 u32 srcmac_vendor = 0, srcmac_dev = 0;
Jussi Maki39a08762021-08-12 14:52:41 +00003756 struct ethhdr *mac_hdr;
3757 u16 vlan = 0;
Jarod Wilson7b8fc012021-01-18 20:09:27 -05003758 int i;
3759
Jussi Makia815bde2021-07-31 05:57:32 +00003760 data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
3761 if (!data)
3762 return 0;
3763 mac_hdr = (struct ethhdr *)(data + mhoff);
3764
Jarod Wilson7b8fc012021-01-18 20:09:27 -05003765 for (i = 0; i < 3; i++)
3766 srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i];
3767
3768 for (i = 3; i < ETH_ALEN; i++)
3769 srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];
3770
Jussi Maki39a08762021-08-12 14:52:41 +00003771 if (skb && skb_vlan_tag_present(skb))
3772 vlan = skb_vlan_tag_get(skb);
Jarod Wilson7b8fc012021-01-18 20:09:27 -05003773
3774 return vlan ^ srcmac_vendor ^ srcmac_dev;
3775}
3776
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003777/* Extract the appropriate headers based on bond's xmit policy */
Jussi Makia815bde2021-07-31 05:57:32 +00003778static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data,
3779 __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk)
John Eaglesham6b923cb2012-08-21 20:43:35 +00003780{
Matteo Crocedf98be02019-11-15 12:10:37 +01003781 bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
Jussi Makia815bde2021-07-31 05:57:32 +00003782 int ip_proto = -1;
John Eaglesham6b923cb2012-08-21 20:43:35 +00003783
Jarod Wilson7b8fc012021-01-18 20:09:27 -05003784 switch (bond->params.xmit_policy) {
3785 case BOND_XMIT_POLICY_ENCAP23:
3786 case BOND_XMIT_POLICY_ENCAP34:
Matteo Croce58deb772019-10-29 14:50:53 +01003787 memset(fk, 0, sizeof(*fk));
3788 return __skb_flow_dissect(NULL, skb, &flow_keys_bonding,
Jussi Makia815bde2021-07-31 05:57:32 +00003789 fk, data, l2_proto, nhoff, hlen, 0);
Jarod Wilson7b8fc012021-01-18 20:09:27 -05003790 default:
3791 break;
Matteo Croce58deb772019-10-29 14:50:53 +01003792 }
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003793
Jiri Pirko06635a32015-05-12 14:56:16 +02003794 fk->ports.ports = 0;
Matteo Croce58deb772019-10-29 14:50:53 +01003795 memset(&fk->icmp, 0, sizeof(fk->icmp));
Jussi Makia815bde2021-07-31 05:57:32 +00003796 if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34))
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003797 return false;
Matteo Crocedf98be02019-11-15 12:10:37 +01003798
3799 /* ICMP error packets contains at least 8 bytes of the header
3800 * of the packet which generated the error. Use this information
3801 * to correlate ICMP error packets within the same flow which
3802 * generated the error.
3803 */
Jussi Makia815bde2021-07-31 05:57:32 +00003804 if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) {
3805 skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen);
3806 if (ip_proto == IPPROTO_ICMP) {
Matteo Crocedf98be02019-11-15 12:10:37 +01003807 if (!icmp_is_err(fk->icmp.type))
3808 return true;
3809
Jussi Makia815bde2021-07-31 05:57:32 +00003810 nhoff += sizeof(struct icmphdr);
3811 } else if (ip_proto == IPPROTO_ICMPV6) {
Matteo Crocedf98be02019-11-15 12:10:37 +01003812 if (!icmpv6_is_err(fk->icmp.type))
3813 return true;
3814
Jussi Makia815bde2021-07-31 05:57:32 +00003815 nhoff += sizeof(struct icmp6hdr);
Matteo Crocedf98be02019-11-15 12:10:37 +01003816 }
Jussi Makia815bde2021-07-31 05:57:32 +00003817 return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34);
Matteo Croce58deb772019-10-29 14:50:53 +01003818 }
John Eaglesham6b923cb2012-08-21 20:43:35 +00003819
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003820 return true;
John Eaglesham6b923cb2012-08-21 20:43:35 +00003821}
3822
Tariq Toukan5b998542021-01-17 16:59:43 +02003823static u32 bond_ip_hash(u32 hash, struct flow_keys *flow)
3824{
3825 hash ^= (__force u32)flow_get_u32_dst(flow) ^
3826 (__force u32)flow_get_u32_src(flow);
3827 hash ^= (hash >> 16);
3828 hash ^= (hash >> 8);
3829 /* discard lowest hash bit to deal with the common even ports pattern */
3830 return hash >> 1;
3831}
3832
Jussi Makia815bde2021-07-31 05:57:32 +00003833/* Generate hash based on xmit policy. If @skb is given it is used to linearize
3834 * the data as required, but this function can be used without it if the data is
3835 * known to be linear (e.g. with xdp_buff).
3836 */
3837static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data,
3838 __be16 l2_proto, int mhoff, int nhoff, int hlen)
3839{
3840 struct flow_keys flow;
3841 u32 hash;
3842
3843 if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
3844 return bond_vlan_srcmac_hash(skb, data, mhoff, hlen);
3845
3846 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
3847 !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow))
3848 return bond_eth_hash(skb, data, mhoff, hlen);
3849
3850 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
3851 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
3852 hash = bond_eth_hash(skb, data, mhoff, hlen);
3853 } else {
3854 if (flow.icmp.id)
3855 memcpy(&hash, &flow.icmp, sizeof(hash));
3856 else
3857 memcpy(&hash, &flow.ports.ports, sizeof(hash));
3858 }
3859
3860 return bond_ip_hash(hash, &flow);
3861}
3862
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003863/**
3864 * bond_xmit_hash - generate a hash value based on the xmit policy
3865 * @bond: bonding device
3866 * @skb: buffer to use for headers
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003867 *
3868 * This function will extract the necessary headers from the skb buffer and use
3869 * them to generate a hash based on the xmit_policy set in the bonding device
John Eaglesham6b923cb2012-08-21 20:43:35 +00003870 */
Mahesh Bandewaree62e862014-04-22 16:30:15 -07003871u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
John Eaglesham6b923cb2012-08-21 20:43:35 +00003872{
Eric Dumazet4b1b8652015-09-15 15:24:28 -07003873 if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
3874 skb->l4_hash)
3875 return skb->hash;
3876
Moshe Tal429e3d12022-01-16 19:39:29 +02003877 return __bond_xmit_hash(bond, skb, skb->data, skb->protocol,
3878 skb_mac_offset(skb), skb_network_offset(skb),
Jussi Makia815bde2021-07-31 05:57:32 +00003879 skb_headlen(skb));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003880}
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003881
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00003882/**
3883 * bond_xmit_hash_xdp - generate a hash value based on the xmit policy
3884 * @bond: bonding device
3885 * @xdp: buffer to use for headers
3886 *
3887 * The XDP variant of bond_xmit_hash.
3888 */
3889static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp)
3890{
3891 struct ethhdr *eth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00003893 if (xdp->data + sizeof(struct ethhdr) > xdp->data_end)
3894 return 0;
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003895
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00003896 eth = (struct ethhdr *)xdp->data;
3897
3898 return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0,
3899 sizeof(struct ethhdr), xdp->data_end - xdp->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900}
3901
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003902/*-------------------------- Device entry points ----------------------------*/
3903
3904void bond_work_init_all(struct bonding *bond)
3905{
3906 INIT_DELAYED_WORK(&bond->mcast_work,
3907 bond_resend_igmp_join_requests_delayed);
3908 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3909 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
3910 INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
3911 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
3912 INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
3913}
3914
3915static void bond_work_cancel_all(struct bonding *bond)
3916{
3917 cancel_delayed_work_sync(&bond->mii_work);
3918 cancel_delayed_work_sync(&bond->arp_work);
3919 cancel_delayed_work_sync(&bond->alb_work);
3920 cancel_delayed_work_sync(&bond->ad_work);
3921 cancel_delayed_work_sync(&bond->mcast_work);
Mahesh Bandewaree637712014-10-04 17:45:01 -07003922 cancel_delayed_work_sync(&bond->slave_arr_work);
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003923}
3924
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925static int bond_open(struct net_device *bond_dev)
3926{
3927 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003928 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
3931 /* reset slave->backup and slave->inactive */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02003932 if (bond_has_slaves(bond)) {
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003933 bond_for_each_slave(bond, slave, iter) {
Eric Dumazet4740d632014-07-15 06:56:55 -07003934 if (bond_uses_primary(bond) &&
3935 slave != rcu_access_pointer(bond->curr_active_slave)) {
dingtianhong5e5b0662014-02-26 11:05:22 +08003936 bond_set_slave_inactive_flags(slave,
3937 BOND_SLAVE_NOTIFY_NOW);
Wilson Kok8bbe71a2015-01-26 01:16:58 -05003938 } else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
dingtianhong5e5b0662014-02-26 11:05:22 +08003939 bond_set_slave_active_flags(slave,
3940 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 if (bond_is_lb(bond)) {
3946 /* bond_alb_initialize must be called before the timer
3947 * is started.
3948 */
Veaceslav Falico01844092014-05-15 21:39:55 +02003949 if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return -ENOMEM;
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04003951 if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07003952 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 }
3954
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003955 if (bond->params.miimon) /* link check interval, in milliseconds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003959 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Veaceslav Falico3fe68df2014-02-18 07:48:39 +01003960 bond->recv_probe = bond_arp_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 }
3962
Veaceslav Falico01844092014-05-15 21:39:55 +02003963 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003964 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 /* register to receive LACPDUs */
Jiri Pirko3aba8912011-04-19 03:48:16 +00003966 bond->recv_probe = bond_3ad_lacpdu_recv;
Jay Vosburghfd989c82008-11-04 17:51:16 -08003967 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
3969
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04003970 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07003971 bond_update_slave_arr(bond, NULL);
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 return 0;
3974}
3975
3976static int bond_close(struct net_device *bond_dev)
3977{
Wang Chen454d7c92008-11-12 23:37:49 -08003978 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003980 bond_work_cancel_all(bond);
nikolay@redhat.com6c8d23f2013-09-02 13:51:38 +02003981 bond->send_peer_notif = 0;
nikolay@redhat.comee8487c2013-09-02 13:51:39 +02003982 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 bond_alb_deinitialize(bond);
Jiri Pirko3aba8912011-04-19 03:48:16 +00003984 bond->recv_probe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 return 0;
3987}
3988
Eric Dumazetfe309372016-03-17 17:23:36 -07003989/* fold stats, assuming all rtnl_link_stats64 fields are u64, but
3990 * that some drivers can provide 32bit values only.
3991 */
3992static void bond_fold_stats(struct rtnl_link_stats64 *_res,
3993 const struct rtnl_link_stats64 *_new,
3994 const struct rtnl_link_stats64 *_old)
3995{
3996 const u64 *new = (const u64 *)_new;
3997 const u64 *old = (const u64 *)_old;
3998 u64 *res = (u64 *)_res;
3999 int i;
4000
4001 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
4002 u64 nv = new[i];
4003 u64 ov = old[i];
Eric Dumazet142c6592017-03-29 10:45:44 -07004004 s64 delta = nv - ov;
Eric Dumazetfe309372016-03-17 17:23:36 -07004005
4006 /* detects if this particular field is 32bit only */
4007 if (((nv | ov) >> 32) == 0)
Eric Dumazet142c6592017-03-29 10:45:44 -07004008 delta = (s64)(s32)((u32)nv - (u32)ov);
4009
4010 /* filter anomalies, some drivers reset their stats
4011 * at down/up events.
4012 */
4013 if (delta > 0)
4014 res[i] += delta;
Eric Dumazetfe309372016-03-17 17:23:36 -07004015 }
4016}
4017
Taehee Yoob3e80d42020-02-15 10:50:40 +00004018#ifdef CONFIG_LOCKDEP
4019static int bond_get_lowest_level_rcu(struct net_device *dev)
4020{
4021 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
4022 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
4023 int cur = 0, max = 0;
4024
4025 now = dev;
4026 iter = &dev->adj_list.lower;
4027
4028 while (1) {
4029 next = NULL;
4030 while (1) {
4031 ldev = netdev_next_lower_dev_rcu(now, &iter);
4032 if (!ldev)
4033 break;
4034
4035 next = ldev;
4036 niter = &ldev->adj_list.lower;
4037 dev_stack[cur] = now;
4038 iter_stack[cur++] = iter;
4039 if (max <= cur)
4040 max = cur;
4041 break;
4042 }
4043
4044 if (!next) {
4045 if (!cur)
4046 return max;
4047 next = dev_stack[--cur];
4048 niter = iter_stack[cur];
4049 }
4050
4051 now = next;
4052 iter = niter;
4053 }
4054
4055 return max;
4056}
4057#endif
4058
stephen hemmingerbc1f4472017-01-06 19:12:52 -08004059static void bond_get_stats(struct net_device *bond_dev,
4060 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Wang Chen454d7c92008-11-12 23:37:49 -08004062 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07004063 struct rtnl_link_stats64 temp;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004064 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 struct slave *slave;
Taehee Yoob3e80d42020-02-15 10:50:40 +00004066 int nest_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Eric Dumazetfe309372016-03-17 17:23:36 -07004069 rcu_read_lock();
Taehee Yoob3e80d42020-02-15 10:50:40 +00004070#ifdef CONFIG_LOCKDEP
4071 nest_level = bond_get_lowest_level_rcu(bond_dev);
4072#endif
4073
4074 spin_lock_nested(&bond->stats_lock, nest_level);
4075 memcpy(stats, &bond->bond_stats, sizeof(*stats));
4076
Eric Dumazetfe309372016-03-17 17:23:36 -07004077 bond_for_each_slave_rcu(bond, slave, iter) {
4078 const struct rtnl_link_stats64 *new =
Eric Dumazet28172732010-07-07 14:58:56 -07004079 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08004080
Eric Dumazetfe309372016-03-17 17:23:36 -07004081 bond_fold_stats(stats, new, &slave->slave_stats);
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04004082
4083 /* save off the slave stats for the next run */
Eric Dumazetfe309372016-03-17 17:23:36 -07004084 memcpy(&slave->slave_stats, new, sizeof(*new));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 }
Eric Dumazetfe309372016-03-17 17:23:36 -07004086
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04004087 memcpy(&bond->bond_stats, stats, sizeof(*stats));
Eric Dumazetfe309372016-03-17 17:23:36 -07004088 spin_unlock(&bond->stats_lock);
Taehee Yoob3e80d42020-02-15 10:50:40 +00004089 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
Arnd Bergmanna7605372021-07-27 15:45:13 +02004092static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093{
Jiri Pirko080a06e2013-10-18 17:43:36 +02004094 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 struct mii_ioctl_data *mii = NULL;
Hangbin Liu94dd0162021-11-30 15:09:32 +08004096 const struct net_device_ops *ops;
4097 struct net_device *real_dev;
Hangbin Liu085d61002021-12-10 16:59:59 +08004098 struct hwtstamp_config cfg;
Hangbin Liu94dd0162021-11-30 15:09:32 +08004099 struct ifreq ifrr;
4100 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Arnd Bergmanna7605372021-07-27 15:45:13 +02004102 netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
4104 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 case SIOCGMIIPHY:
4106 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004107 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004109
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 mii->phy_id = 0;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004111 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 case SIOCGMIIREG:
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004113 /* We do this again just in case we were called by SIOCGMIIREG
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 * instead of SIOCGMIIPHY.
4115 */
4116 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004117 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (mii->reg_num == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 mii->val_out = 0;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004122 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 mii->val_out = BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125
Hangbin Liu94dd0162021-11-30 15:09:32 +08004126 break;
4127 case SIOCSHWTSTAMP:
Hangbin Liu085d61002021-12-10 16:59:59 +08004128 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
4129 return -EFAULT;
Hangbin Liu94dd0162021-11-30 15:09:32 +08004130
Hangbin Liucfe355c2021-12-29 16:09:38 +08004131 if (!(cfg.flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX))
4132 return -EOPNOTSUPP;
Hangbin Liu94dd0162021-11-30 15:09:32 +08004133
Hangbin Liucfe355c2021-12-29 16:09:38 +08004134 fallthrough;
4135 case SIOCGHWTSTAMP:
Hangbin Liucfe355c2021-12-29 16:09:38 +08004136 real_dev = bond_option_active_slave_get_rcu(bond);
Hangbin Liucfe355c2021-12-29 16:09:38 +08004137 if (!real_dev)
4138 return -EOPNOTSUPP;
Hangbin Liu085d61002021-12-10 16:59:59 +08004139
Hangbin Liucfe355c2021-12-29 16:09:38 +08004140 strscpy_pad(ifrr.ifr_name, real_dev->name, IFNAMSIZ);
4141 ifrr.ifr_ifru = ifr->ifr_ifru;
Hangbin Liu085d61002021-12-10 16:59:59 +08004142
Hangbin Liucfe355c2021-12-29 16:09:38 +08004143 ops = real_dev->netdev_ops;
4144 if (netif_device_present(real_dev) && ops->ndo_eth_ioctl) {
4145 res = ops->ndo_eth_ioctl(real_dev, &ifrr, cmd);
4146 if (res)
4147 return res;
4148
4149 ifr->ifr_ifru = ifrr.ifr_ifru;
4150 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
4151 return -EFAULT;
4152
4153 /* Set the BOND_PHC_INDEX flag to notify user space */
4154 cfg.flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
4155
4156 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ?
4157 -EFAULT : 0;
Hangbin Liu94dd0162021-11-30 15:09:32 +08004158 }
Hangbin Liu085d61002021-12-10 16:59:59 +08004159 fallthrough;
Arnd Bergmanna7605372021-07-27 15:45:13 +02004160 default:
4161 res = -EOPNOTSUPP;
4162 }
4163
4164 return res;
4165}
4166
4167static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4168{
4169 struct bonding *bond = netdev_priv(bond_dev);
4170 struct net_device *slave_dev = NULL;
4171 struct ifbond k_binfo;
4172 struct ifbond __user *u_binfo = NULL;
4173 struct ifslave k_sinfo;
4174 struct ifslave __user *u_sinfo = NULL;
4175 struct bond_opt_value newval;
4176 struct net *net;
4177 int res = 0;
4178
4179 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
4180
4181 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 case SIOCBONDINFOQUERY:
4183 u_binfo = (struct ifbond __user *)ifr->ifr_data;
4184
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004185 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
Zhu Yanjun3d675762017-02-02 23:46:21 -05004188 bond_info_query(bond_dev, &k_binfo);
4189 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004190 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
Zhu Yanjun3d675762017-02-02 23:46:21 -05004192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 case SIOCBONDSLAVEINFOQUERY:
4194 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
4195
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004196 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004200 if (res == 0 &&
4201 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
4202 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
4204 return res;
4205 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 break;
4207 }
4208
Gao feng387ff9112013-01-31 16:31:00 +00004209 net = dev_net(bond_dev);
4210
4211 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Ying Xue0917b932014-01-15 10:23:37 +08004214 slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Jarod Wilsone2a74202019-06-07 10:59:29 -04004216 slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004218 if (!slave_dev)
Ying Xue0917b932014-01-15 10:23:37 +08004219 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Ying Xue0917b932014-01-15 10:23:37 +08004221 switch (cmd) {
Ying Xue0917b932014-01-15 10:23:37 +08004222 case SIOCBONDENSLAVE:
David Ahern33eaf2a2017-10-04 17:48:46 -07004223 res = bond_enslave(bond_dev, slave_dev, NULL);
Ying Xue0917b932014-01-15 10:23:37 +08004224 break;
Ying Xue0917b932014-01-15 10:23:37 +08004225 case SIOCBONDRELEASE:
4226 res = bond_release(bond_dev, slave_dev);
4227 break;
Ying Xue0917b932014-01-15 10:23:37 +08004228 case SIOCBONDSETHWADDR:
Petr Machatab9245912018-12-13 11:54:44 +00004229 res = bond_set_dev_addr(bond_dev, slave_dev);
Ying Xue0917b932014-01-15 10:23:37 +08004230 break;
Ying Xue0917b932014-01-15 10:23:37 +08004231 case SIOCBONDCHANGEACTIVE:
Nikolay Aleksandrovd1fbd3ed2014-01-22 14:53:35 +01004232 bond_opt_initstr(&newval, slave_dev->name);
Vlad Yasevich7a7e96e2017-05-27 10:14:35 -04004233 res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
4234 &newval);
Ying Xue0917b932014-01-15 10:23:37 +08004235 break;
4236 default:
4237 res = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return res;
4241}
4242
Arnd Bergmann232ec982021-07-27 15:44:54 +02004243static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr,
4244 void __user *data, int cmd)
4245{
4246 struct ifreq ifrdata = { .ifr_data = data };
4247
4248 switch (cmd) {
4249 case BOND_INFO_QUERY_OLD:
4250 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY);
4251 case BOND_SLAVE_INFO_QUERY_OLD:
4252 return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY);
4253 case BOND_ENSLAVE_OLD:
4254 return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE);
4255 case BOND_RELEASE_OLD:
4256 return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE);
4257 case BOND_SETHWADDR_OLD:
4258 return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR);
4259 case BOND_CHANGE_ACTIVE_OLD:
4260 return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE);
4261 }
4262
4263 return -EOPNOTSUPP;
4264}
4265
Jiri Pirkod03462b2011-08-16 03:15:04 +00004266static void bond_change_rx_flags(struct net_device *bond_dev, int change)
4267{
4268 struct bonding *bond = netdev_priv(bond_dev);
4269
4270 if (change & IFF_PROMISC)
4271 bond_set_promiscuity(bond,
4272 bond_dev->flags & IFF_PROMISC ? 1 : -1);
4273
4274 if (change & IFF_ALLMULTI)
4275 bond_set_allmulti(bond,
4276 bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
4277}
4278
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00004279static void bond_set_rx_mode(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Wang Chen454d7c92008-11-12 23:37:49 -08004281 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004282 struct list_head *iter;
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00004283 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Veaceslav Falicob3241872013-09-28 21:18:56 +02004285 rcu_read_lock();
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02004286 if (bond_uses_primary(bond)) {
Veaceslav Falicob3241872013-09-28 21:18:56 +02004287 slave = rcu_dereference(bond->curr_active_slave);
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00004288 if (slave) {
4289 dev_uc_sync(slave->dev, bond_dev);
4290 dev_mc_sync(slave->dev, bond_dev);
4291 }
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00004292 } else {
Veaceslav Falicob3241872013-09-28 21:18:56 +02004293 bond_for_each_slave_rcu(bond, slave, iter) {
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00004294 dev_uc_sync_multiple(slave->dev, bond_dev);
4295 dev_mc_sync_multiple(slave->dev, bond_dev);
4296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 }
Veaceslav Falicob3241872013-09-28 21:18:56 +02004298 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004301static int bond_neigh_init(struct neighbour *n)
Stephen Hemminger00829822008-11-20 20:14:53 -08004302{
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004303 struct bonding *bond = netdev_priv(n->dev);
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004304 const struct net_device_ops *slave_ops;
4305 struct neigh_parms parms;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02004306 struct slave *slave;
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004307 int ret = 0;
Stephen Hemminger00829822008-11-20 20:14:53 -08004308
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004309 rcu_read_lock();
4310 slave = bond_first_slave_rcu(bond);
David S. Miller080bfa12021-03-12 12:15:03 -08004311 if (!slave)
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004312 goto out;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004313 slave_ops = slave->dev->netdev_ops;
David S. Miller080bfa12021-03-12 12:15:03 -08004314 if (!slave_ops->ndo_neigh_setup)
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004315 goto out;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004316
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004317 /* TODO: find another way [1] to implement this.
4318 * Passing a zeroed structure is fragile,
4319 * but at least we do not pass garbage.
4320 *
4321 * [1] One way would be that ndo_neigh_setup() never touch
4322 * struct neigh_parms, but propagate the new neigh_setup()
4323 * back to ___neigh_create() / neigh_parms_alloc()
4324 */
4325 memset(&parms, 0, sizeof(parms));
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004326 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004327
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004328 if (ret)
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004329 goto out;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004330
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08004331 if (parms.neigh_setup)
4332 ret = parms.neigh_setup(n);
4333out:
4334 rcu_read_unlock();
4335 return ret;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004336}
4337
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004338/* The bonding ndo_neigh_setup is called at init time beofre any
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004339 * slave exists. So we must declare proxy setup function which will
4340 * be used at run time to resolve the actual slave neigh param setup.
Veaceslav Falico9918d5b2013-08-02 19:07:39 +02004341 *
4342 * It's also called by master devices (such as vlans) to setup their
4343 * underlying devices. In that case - do nothing, we're already set up from
4344 * our init.
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004345 */
4346static int bond_neigh_setup(struct net_device *dev,
4347 struct neigh_parms *parms)
4348{
Veaceslav Falico9918d5b2013-08-02 19:07:39 +02004349 /* modify only our neigh_parms */
4350 if (parms->dev == dev)
4351 parms->neigh_setup = bond_neigh_init;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00004352
Stephen Hemminger00829822008-11-20 20:14:53 -08004353 return 0;
4354}
4355
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004356/* Change the MTU of all of a master's slaves to match the master */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
4358{
Wang Chen454d7c92008-11-12 23:37:49 -08004359 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico81f23b12013-09-25 09:20:13 +02004360 struct slave *slave, *rollback_slave;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004361 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Veaceslav Falico76444f52014-07-15 19:35:58 +02004364 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004366 bond_for_each_slave(bond, slave, iter) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04004367 slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
Veaceslav Falico76444f52014-07-15 19:35:58 +02004368 slave, slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08004369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 res = dev_set_mtu(slave->dev, new_mtu);
4371
4372 if (res) {
4373 /* If we failed to set the slave's mtu to the new value
4374 * we must abort the operation even in ACTIVE_BACKUP
4375 * mode, because if we allow the backup slaves to have
4376 * different mtu values than the active slave we'll
4377 * need to change their mtu when doing a failover. That
4378 * means changing their mtu from timer context, which
4379 * is probably not a good idea.
4380 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04004381 slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
4382 res, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 goto unwind;
4384 }
4385 }
4386
4387 bond_dev->mtu = new_mtu;
4388
4389 return 0;
4390
4391unwind:
4392 /* unwind from head to the slave that failed */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004393 bond_for_each_slave(bond, rollback_slave, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 int tmp_res;
4395
Veaceslav Falico81f23b12013-09-25 09:20:13 +02004396 if (rollback_slave == slave)
4397 break;
4398
4399 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
Jarod Wilsone2a74202019-06-07 10:59:29 -04004400 if (tmp_res)
4401 slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
4402 tmp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 }
4404
4405 return res;
4406}
4407
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004408/* Change HW address
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 *
4410 * Note that many devices must be down to change the HW address, and
4411 * downing the master releases all slaves. We can make bonds full of
4412 * bonding devices to test this, however.
4413 */
4414static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
4415{
Wang Chen454d7c92008-11-12 23:37:49 -08004416 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico81f23b12013-09-25 09:20:13 +02004417 struct slave *slave, *rollback_slave;
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004418 struct sockaddr_storage *ss = addr, tmp_ss;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004419 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Veaceslav Falico01844092014-05-15 21:39:55 +02004422 if (BOND_MODE(bond) == BOND_MODE_ALB)
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004423 return bond_alb_set_mac_address(bond_dev, addr);
4424
4425
Jarod Wilsone2a74202019-06-07 10:59:29 -04004426 netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Jay Vosburgh1b5acd22013-05-31 11:57:31 +00004428 /* If fail_over_mac is enabled, do nothing and return success.
4429 * Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07004430 */
dingtianhongcc689aa2014-01-25 13:00:57 +08004431 if (bond->params.fail_over_mac &&
Veaceslav Falico01844092014-05-15 21:39:55 +02004432 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
Jay Vosburghdd957c52007-10-09 19:57:24 -07004433 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004434
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004435 if (!is_valid_ether_addr(ss->__data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004438 bond_for_each_slave(bond, slave, iter) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04004439 slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
4440 __func__, slave);
Petr Machata3a37a962018-12-13 11:54:30 +00004441 res = dev_set_mac_address(slave->dev, addr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 if (res) {
4443 /* TODO: consider downing the slave
4444 * and retry ?
4445 * User should expect communications
4446 * breakage anyway until ARP finish
4447 * updating, so...
4448 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04004449 slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
4450 __func__, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 goto unwind;
4452 }
4453 }
4454
4455 /* success */
Jakub Kicinskiea52a0b2021-10-08 10:53:39 -07004456 dev_addr_set(bond_dev, ss->__data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 return 0;
4458
4459unwind:
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004460 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
4461 tmp_ss.ss_family = bond_dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 /* unwind from head to the slave that failed */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004464 bond_for_each_slave(bond, rollback_slave, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 int tmp_res;
4466
Veaceslav Falico81f23b12013-09-25 09:20:13 +02004467 if (rollback_slave == slave)
4468 break;
4469
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004470 tmp_res = dev_set_mac_address(rollback_slave->dev,
Petr Machata3a37a962018-12-13 11:54:30 +00004471 (struct sockaddr *)&tmp_ss, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 if (tmp_res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04004473 slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
4474 __func__, tmp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 }
4476 }
4477
4478 return res;
4479}
4480
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004481/**
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004482 * bond_get_slave_by_id - get xmit slave with slave_id
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004483 * @bond: bonding device that is transmitting
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004484 * @slave_id: slave id up to slave_cnt-1 through which to transmit
4485 *
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004486 * This function tries to get slave with slave_id but in case
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004487 * it fails, it tries to find the first available slave for transmission.
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004488 */
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004489static struct slave *bond_get_slave_by_id(struct bonding *bond,
4490 int slave_id)
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004491{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004492 struct list_head *iter;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004493 struct slave *slave;
4494 int i = slave_id;
4495
4496 /* Here we start from the slave with slave_id */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004497 bond_for_each_slave_rcu(bond, slave, iter) {
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004498 if (--i < 0) {
Eric Dumazetae46f1842020-05-07 09:32:22 -07004499 if (bond_slave_can_tx(slave))
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004500 return slave;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004501 }
4502 }
4503
4504 /* Here we start from the first slave up to slave_id */
4505 i = slave_id;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004506 bond_for_each_slave_rcu(bond, slave, iter) {
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004507 if (--i < 0)
4508 break;
Eric Dumazetae46f1842020-05-07 09:32:22 -07004509 if (bond_slave_can_tx(slave))
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004510 return slave;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004511 }
4512 /* no slave that can tx has been found */
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004513 return NULL;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004514}
4515
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004516/**
4517 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
4518 * @bond: bonding device to use
4519 *
4520 * Based on the value of the bonding device's packets_per_slave parameter
4521 * this function generates a slave id, which is usually used as the next
4522 * slave to transmit through.
4523 */
4524static u32 bond_rr_gen_slave_id(struct bonding *bond)
4525{
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004526 u32 slave_id;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01004527 struct reciprocal_value reciprocal_packets_per_slave;
4528 int packets_per_slave = bond->params.packets_per_slave;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004529
4530 switch (packets_per_slave) {
4531 case 0:
4532 slave_id = prandom_u32();
4533 break;
4534 case 1:
Jussi Maki848ca912021-06-15 08:54:15 +00004535 slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004536 break;
4537 default:
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01004538 reciprocal_packets_per_slave =
4539 bond->params.reciprocal_packets_per_slave;
Jussi Maki848ca912021-06-15 08:54:15 +00004540 slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4541 slave_id = reciprocal_divide(slave_id,
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01004542 reciprocal_packets_per_slave);
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004543 break;
4544 }
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004545
4546 return slave_id;
4547}
4548
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004549static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
4550 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004552 struct slave *slave;
Cong Wang9d1bc242019-07-01 20:40:24 -07004553 int slave_cnt;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004554 u32 slave_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004556 /* Start with the curr_active_slave that joined the bond as the
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004557 * default for sending IGMP traffic. For failover purposes one
4558 * needs to maintain some consistency for the interface that will
4559 * send the join/membership reports. The curr_active_slave found
4560 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004561 */
Cong Wang9d1bc242019-07-01 20:40:24 -07004562 if (skb->protocol == htons(ETH_P_IP)) {
4563 int noff = skb_network_offset(skb);
4564 struct iphdr *iph;
Nikolay Aleksandrov9a72c2d2014-09-12 17:38:18 +02004565
Cong Wang9d1bc242019-07-01 20:40:24 -07004566 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
4567 goto non_igmp;
4568
4569 iph = ip_hdr(skb);
4570 if (iph->protocol == IPPROTO_IGMP) {
4571 slave = rcu_dereference(bond->curr_active_slave);
4572 if (slave)
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004573 return slave;
4574 return bond_get_slave_by_id(bond, 0);
Nikolay Aleksandrov9a72c2d2014-09-12 17:38:18 +02004575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004577
Cong Wang9d1bc242019-07-01 20:40:24 -07004578non_igmp:
4579 slave_cnt = READ_ONCE(bond->slave_cnt);
4580 if (likely(slave_cnt)) {
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004581 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4582 return bond_get_slave_by_id(bond, slave_id);
Cong Wang9d1bc242019-07-01 20:40:24 -07004583 }
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004584 return NULL;
4585}
4586
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00004587static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond,
4588 struct xdp_buff *xdp)
4589{
4590 struct slave *slave;
4591 int slave_cnt;
4592 u32 slave_id;
4593 const struct ethhdr *eth;
4594 void *data = xdp->data;
4595
4596 if (data + sizeof(struct ethhdr) > xdp->data_end)
4597 goto non_igmp;
4598
4599 eth = (struct ethhdr *)data;
4600 data += sizeof(struct ethhdr);
4601
4602 /* See comment on IGMP in bond_xmit_roundrobin_slave_get() */
4603 if (eth->h_proto == htons(ETH_P_IP)) {
4604 const struct iphdr *iph;
4605
4606 if (data + sizeof(struct iphdr) > xdp->data_end)
4607 goto non_igmp;
4608
4609 iph = (struct iphdr *)data;
4610
4611 if (iph->protocol == IPPROTO_IGMP) {
4612 slave = rcu_dereference(bond->curr_active_slave);
4613 if (slave)
4614 return slave;
4615 return bond_get_slave_by_id(bond, 0);
4616 }
4617 }
4618
4619non_igmp:
4620 slave_cnt = READ_ONCE(bond->slave_cnt);
4621 if (likely(slave_cnt)) {
4622 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4623 return bond_get_slave_by_id(bond, slave_id);
4624 }
4625 return NULL;
4626}
4627
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004628static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
4629 struct net_device *bond_dev)
4630{
4631 struct bonding *bond = netdev_priv(bond_dev);
4632 struct slave *slave;
4633
4634 slave = bond_xmit_roundrobin_slave_get(bond, skb);
Saeed Mahameed76cd6222020-05-09 00:06:35 -07004635 if (likely(slave))
4636 return bond_dev_queue_xmit(bond, skb, slave->dev);
4637
Eric Dumazetae46f1842020-05-07 09:32:22 -07004638 return bond_tx_drop(bond_dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
Jussi Makia815bde2021-07-31 05:57:32 +00004641static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond)
Maor Gottlieb5a19f1c2020-04-30 22:21:37 +03004642{
4643 return rcu_dereference(bond->curr_active_slave);
4644}
4645
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004646/* In active-backup mode, we know that bond->curr_active_slave is always valid if
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 * the bond has a usable interface.
4648 */
Tonghao Zhangdbdc8a22018-05-11 02:53:10 -07004649static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
4650 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Wang Chen454d7c92008-11-12 23:37:49 -08004652 struct bonding *bond = netdev_priv(bond_dev);
nikolay@redhat.com71bc3b22013-08-01 16:54:48 +02004653 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Jussi Makia815bde2021-07-31 05:57:32 +00004655 slave = bond_xmit_activebackup_slave_get(bond);
nikolay@redhat.com71bc3b22013-08-01 16:54:48 +02004656 if (slave)
Eric Dumazetae46f1842020-05-07 09:32:22 -07004657 return bond_dev_queue_xmit(bond, skb, slave->dev);
Michał Mirosław0693e882011-05-07 01:48:02 +00004658
Eric Dumazetae46f1842020-05-07 09:32:22 -07004659 return bond_tx_drop(bond_dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
Mahesh Bandewaree637712014-10-04 17:45:01 -07004662/* Use this to update slave_array when (a) it's not appropriate to update
4663 * slave_array right away (note that update_slave_array() may sleep)
4664 * and / or (b) RTNL is not held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07004666void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
Mahesh Bandewaree637712014-10-04 17:45:01 -07004668 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
4669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Mahesh Bandewaree637712014-10-04 17:45:01 -07004671/* Slave array work handler. Holds only RTNL */
4672static void bond_slave_arr_handler(struct work_struct *work)
4673{
4674 struct bonding *bond = container_of(work, struct bonding,
4675 slave_arr_work.work);
4676 int ret;
4677
4678 if (!rtnl_trylock())
4679 goto err;
4680
4681 ret = bond_update_slave_arr(bond, NULL);
4682 rtnl_unlock();
4683 if (ret) {
4684 pr_warn_ratelimited("Failed to update slave array from WT\n");
4685 goto err;
4686 }
4687 return;
4688
4689err:
4690 bond_slave_arr_work_rearm(bond, 1);
4691}
4692
Maor Gottlieb119d48f2020-04-30 22:21:32 +03004693static void bond_skip_slave(struct bond_up_slave *slaves,
4694 struct slave *skipslave)
4695{
4696 int idx;
4697
4698 /* Rare situation where caller has asked to skip a specific
4699 * slave but allocation failed (most likely!). BTW this is
4700 * only possible when the call is initiated from
4701 * __bond_release_one(). In this situation; overwrite the
4702 * skipslave entry in the array with the last entry from the
4703 * array to avoid a situation where the xmit path may choose
4704 * this to-be-skipped slave to send a packet out.
4705 */
4706 for (idx = 0; slaves && idx < slaves->count; idx++) {
4707 if (skipslave == slaves->arr[idx]) {
4708 slaves->arr[idx] =
4709 slaves->arr[slaves->count - 1];
4710 slaves->count--;
4711 break;
4712 }
4713 }
4714}
4715
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004716static void bond_set_slave_arr(struct bonding *bond,
4717 struct bond_up_slave *usable_slaves,
4718 struct bond_up_slave *all_slaves)
4719{
4720 struct bond_up_slave *usable, *all;
4721
4722 usable = rtnl_dereference(bond->usable_slaves);
4723 rcu_assign_pointer(bond->usable_slaves, usable_slaves);
4724 kfree_rcu(usable, rcu);
4725
4726 all = rtnl_dereference(bond->all_slaves);
4727 rcu_assign_pointer(bond->all_slaves, all_slaves);
4728 kfree_rcu(all, rcu);
4729}
4730
4731static void bond_reset_slave_arr(struct bonding *bond)
4732{
4733 struct bond_up_slave *usable, *all;
4734
4735 usable = rtnl_dereference(bond->usable_slaves);
4736 if (usable) {
4737 RCU_INIT_POINTER(bond->usable_slaves, NULL);
4738 kfree_rcu(usable, rcu);
4739 }
4740
4741 all = rtnl_dereference(bond->all_slaves);
4742 if (all) {
4743 RCU_INIT_POINTER(bond->all_slaves, NULL);
4744 kfree_rcu(all, rcu);
4745 }
4746}
4747
Mahesh Bandewaree637712014-10-04 17:45:01 -07004748/* Build the usable slaves array in control path for modes that use xmit-hash
4749 * to determine the slave interface -
4750 * (a) BOND_MODE_8023AD
4751 * (b) BOND_MODE_XOR
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04004752 * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
Mahesh Bandewaree637712014-10-04 17:45:01 -07004753 *
4754 * The caller is expected to hold RTNL only and NO other lock!
4755 */
4756int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
4757{
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004758 struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004759 struct slave *slave;
4760 struct list_head *iter;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004761 int agg_id = 0;
4762 int ret = 0;
4763
jinyiting83d686a2021-04-21 16:38:21 +08004764 might_sleep();
Mahesh Bandewaree637712014-10-04 17:45:01 -07004765
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004766 usable_slaves = kzalloc(struct_size(usable_slaves, arr,
4767 bond->slave_cnt), GFP_KERNEL);
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004768 all_slaves = kzalloc(struct_size(all_slaves, arr,
4769 bond->slave_cnt), GFP_KERNEL);
4770 if (!usable_slaves || !all_slaves) {
Mahesh Bandewaree637712014-10-04 17:45:01 -07004771 ret = -ENOMEM;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004772 goto out;
4773 }
4774 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4775 struct ad_info ad_info;
4776
jinyiting83d686a2021-04-21 16:38:21 +08004777 spin_lock_bh(&bond->mode_lock);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004778 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
jinyiting83d686a2021-04-21 16:38:21 +08004779 spin_unlock_bh(&bond->mode_lock);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004780 pr_debug("bond_3ad_get_active_agg_info failed\n");
Mahesh Bandewaree637712014-10-04 17:45:01 -07004781 /* No active aggragator means it's not safe to use
4782 * the previous array.
4783 */
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004784 bond_reset_slave_arr(bond);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004785 goto out;
4786 }
jinyiting83d686a2021-04-21 16:38:21 +08004787 spin_unlock_bh(&bond->mode_lock);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004788 agg_id = ad_info.aggregator_id;
4789 }
4790 bond_for_each_slave(bond, slave, iter) {
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004791 if (skipslave == slave)
4792 continue;
4793
4794 all_slaves->arr[all_slaves->count++] = slave;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004795 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4796 struct aggregator *agg;
4797
4798 agg = SLAVE_AD_INFO(slave)->port.aggregator;
4799 if (!agg || agg->aggregator_identifier != agg_id)
4800 continue;
4801 }
4802 if (!bond_slave_can_tx(slave))
4803 continue;
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04004804
Jarod Wilsone2a74202019-06-07 10:59:29 -04004805 slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004806 usable_slaves->count);
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04004807
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004808 usable_slaves->arr[usable_slaves->count++] = slave;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004809 }
4810
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004811 bond_set_slave_arr(bond, usable_slaves, all_slaves);
4812 return ret;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004813out:
4814 if (ret != 0 && skipslave) {
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004815 bond_skip_slave(rtnl_dereference(bond->all_slaves),
4816 skipslave);
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004817 bond_skip_slave(rtnl_dereference(bond->usable_slaves),
4818 skipslave);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004819 }
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004820 kfree_rcu(all_slaves, rcu);
4821 kfree_rcu(usable_slaves, rcu);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004822
Mahesh Bandewaree637712014-10-04 17:45:01 -07004823 return ret;
4824}
4825
Maor Gottliebc071d912020-04-30 22:21:35 +03004826static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
4827 struct sk_buff *skb,
4828 struct bond_up_slave *slaves)
4829{
4830 struct slave *slave;
4831 unsigned int count;
4832 u32 hash;
4833
4834 hash = bond_xmit_hash(bond, skb);
4835 count = slaves ? READ_ONCE(slaves->count) : 0;
4836 if (unlikely(!count))
4837 return NULL;
4838
4839 slave = slaves->arr[hash % count];
4840 return slave;
4841}
4842
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00004843static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond,
4844 struct xdp_buff *xdp)
4845{
4846 struct bond_up_slave *slaves;
4847 unsigned int count;
4848 u32 hash;
4849
4850 hash = bond_xmit_hash_xdp(bond, xdp);
4851 slaves = rcu_dereference(bond->usable_slaves);
4852 count = slaves ? READ_ONCE(slaves->count) : 0;
4853 if (unlikely(!count))
4854 return NULL;
4855
4856 return slaves->arr[hash % count];
4857}
4858
Mahesh Bandewaree637712014-10-04 17:45:01 -07004859/* Use this Xmit function for 3AD as well as XOR modes. The current
4860 * usable slave array is formed in the control path. The xmit function
4861 * just calculates hash and sends the packet out.
4862 */
Tonghao Zhangdbdc8a22018-05-11 02:53:10 -07004863static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
4864 struct net_device *dev)
Mahesh Bandewaree637712014-10-04 17:45:01 -07004865{
4866 struct bonding *bond = netdev_priv(dev);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004867 struct bond_up_slave *slaves;
Maor Gottliebc071d912020-04-30 22:21:35 +03004868 struct slave *slave;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004869
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004870 slaves = rcu_dereference(bond->usable_slaves);
Maor Gottliebc071d912020-04-30 22:21:35 +03004871 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4872 if (likely(slave))
Eric Dumazetae46f1842020-05-07 09:32:22 -07004873 return bond_dev_queue_xmit(bond, skb, slave->dev);
Michał Mirosław0693e882011-05-07 01:48:02 +00004874
Eric Dumazetae46f1842020-05-07 09:32:22 -07004875 return bond_tx_drop(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004878/* in broadcast mode, we send everything to all usable interfaces. */
Tonghao Zhangdbdc8a22018-05-11 02:53:10 -07004879static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
4880 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881{
Wang Chen454d7c92008-11-12 23:37:49 -08004882 struct bonding *bond = netdev_priv(bond_dev);
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004883 struct slave *slave = NULL;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004884 struct list_head *iter;
Jie Wang4e5bd032022-01-12 20:54:18 +08004885 bool xmit_suc = false;
4886 bool skb_used = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004888 bond_for_each_slave_rcu(bond, slave, iter) {
Jie Wang4e5bd032022-01-12 20:54:18 +08004889 struct sk_buff *skb2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Jie Wang4e5bd032022-01-12 20:54:18 +08004891 if (!(bond_slave_is_up(slave) && slave->link == BOND_LINK_UP))
4892 continue;
4893
4894 if (bond_is_last_slave(bond, slave)) {
4895 skb2 = skb;
4896 skb_used = true;
4897 } else {
4898 skb2 = skb_clone(skb, GFP_ATOMIC);
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004899 if (!skb2) {
dingtianhong9152e262014-03-25 17:00:10 +08004900 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
4901 bond_dev->name, __func__);
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004902 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004905
Jie Wang4e5bd032022-01-12 20:54:18 +08004906 if (bond_dev_queue_xmit(bond, skb2, slave->dev) == NETDEV_TX_OK)
4907 xmit_suc = true;
4908 }
4909
4910 if (!skb_used)
4911 dev_kfree_skb_any(skb);
4912
4913 if (xmit_suc)
4914 return NETDEV_TX_OK;
4915
4916 atomic_long_inc(&bond_dev->tx_dropped);
4917 return NET_XMIT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918}
4919
4920/*------------------------- Device initialization ---------------------------*/
4921
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004922/* Lookup the slave that corresponds to a qid */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004923static inline int bond_slave_override(struct bonding *bond,
4924 struct sk_buff *skb)
4925{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004926 struct slave *slave = NULL;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004927 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004928
Tonghao Zhangae35c6f2018-05-11 02:53:11 -07004929 if (!skb_rx_queue_recorded(skb))
Michał Mirosław0693e882011-05-07 01:48:02 +00004930 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004931
4932 /* Find out if any slaves have the same mapping as this skb. */
dingtianhong3900f292014-01-02 09:13:06 +08004933 bond_for_each_slave_rcu(bond, slave, iter) {
Tonghao Zhangae35c6f2018-05-11 02:53:11 -07004934 if (slave->queue_id == skb_get_queue_mapping(skb)) {
Anton Nayshtutf5e2dc52015-03-29 14:20:25 +03004935 if (bond_slave_is_up(slave) &&
4936 slave->link == BOND_LINK_UP) {
dingtianhong3900f292014-01-02 09:13:06 +08004937 bond_dev_queue_xmit(bond, skb, slave->dev);
4938 return 0;
4939 }
4940 /* If the slave isn't UP, use default transmit policy. */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004941 break;
4942 }
4943 }
4944
dingtianhong3900f292014-01-02 09:13:06 +08004945 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004946}
4947
Neil Horman374eeb52011-06-03 10:35:52 +00004948
Jason Wangf663dd92014-01-10 16:18:26 +08004949static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01004950 struct net_device *sb_dev)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004951{
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004952 /* This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004953 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004954 * skb_tx_hash and will put the skbs in the queue we expect on their
4955 * way down to the bonding driver.
4956 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004957 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4958
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004959 /* Save the original txq to restore before passing to the driver */
Tonghao Zhangae35c6f2018-05-11 02:53:11 -07004960 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
Neil Horman374eeb52011-06-03 10:35:52 +00004961
Phil Oesterfd0e4352011-03-14 06:22:04 +00004962 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004963 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004964 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004965 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004966 }
4967 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004968}
4969
Maor Gottlieb33720aa2020-04-30 22:21:39 +03004970static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
4971 struct sk_buff *skb,
4972 bool all_slaves)
4973{
4974 struct bonding *bond = netdev_priv(master_dev);
4975 struct bond_up_slave *slaves;
4976 struct slave *slave = NULL;
4977
4978 switch (BOND_MODE(bond)) {
4979 case BOND_MODE_ROUNDROBIN:
4980 slave = bond_xmit_roundrobin_slave_get(bond, skb);
4981 break;
4982 case BOND_MODE_ACTIVEBACKUP:
Jussi Makia815bde2021-07-31 05:57:32 +00004983 slave = bond_xmit_activebackup_slave_get(bond);
Maor Gottlieb33720aa2020-04-30 22:21:39 +03004984 break;
4985 case BOND_MODE_8023AD:
4986 case BOND_MODE_XOR:
4987 if (all_slaves)
4988 slaves = rcu_dereference(bond->all_slaves);
4989 else
4990 slaves = rcu_dereference(bond->usable_slaves);
4991 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4992 break;
4993 case BOND_MODE_BROADCAST:
4994 break;
4995 case BOND_MODE_ALB:
4996 slave = bond_xmit_alb_slave_get(bond, skb);
4997 break;
4998 case BOND_MODE_TLB:
4999 slave = bond_xmit_tlb_slave_get(bond, skb);
5000 break;
5001 default:
5002 /* Should never happen, mode already checked */
5003 WARN_ONCE(true, "Unknown bonding mode");
5004 break;
5005 }
5006
5007 if (slave)
5008 return slave->dev;
5009 return NULL;
5010}
5011
Tariq Toukan007feb82021-01-17 16:59:44 +02005012static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow)
5013{
5014 switch (sk->sk_family) {
5015#if IS_ENABLED(CONFIG_IPV6)
5016 case AF_INET6:
5017 if (sk->sk_ipv6only ||
5018 ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) {
5019 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
5020 flow->addrs.v6addrs.src = inet6_sk(sk)->saddr;
5021 flow->addrs.v6addrs.dst = sk->sk_v6_daddr;
5022 break;
5023 }
5024 fallthrough;
5025#endif
5026 default: /* AF_INET */
5027 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
5028 flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr;
5029 flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr;
5030 break;
5031 }
5032
5033 flow->ports.src = inet_sk(sk)->inet_sport;
5034 flow->ports.dst = inet_sk(sk)->inet_dport;
5035}
5036
5037/**
5038 * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields
5039 * @sk: socket to use for headers
5040 *
5041 * This function will extract the necessary field from the socket and use
5042 * them to generate a hash based on the LAYER34 xmit_policy.
5043 * Assumes that sk is a TCP or UDP socket.
5044 */
5045static u32 bond_sk_hash_l34(struct sock *sk)
5046{
5047 struct flow_keys flow;
5048 u32 hash;
5049
5050 bond_sk_to_flow(sk, &flow);
5051
5052 /* L4 */
5053 memcpy(&hash, &flow.ports.ports, sizeof(hash));
5054 /* L3 */
5055 return bond_ip_hash(hash, &flow);
5056}
5057
5058static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond,
5059 struct sock *sk)
5060{
5061 struct bond_up_slave *slaves;
5062 struct slave *slave;
5063 unsigned int count;
5064 u32 hash;
5065
5066 slaves = rcu_dereference(bond->usable_slaves);
5067 count = slaves ? READ_ONCE(slaves->count) : 0;
5068 if (unlikely(!count))
5069 return NULL;
5070
5071 hash = bond_sk_hash_l34(sk);
5072 slave = slaves->arr[hash % count];
5073
5074 return slave->dev;
5075}
5076
5077static struct net_device *bond_sk_get_lower_dev(struct net_device *dev,
5078 struct sock *sk)
5079{
5080 struct bonding *bond = netdev_priv(dev);
5081 struct net_device *lower = NULL;
5082
5083 rcu_read_lock();
5084 if (bond_sk_check(bond))
5085 lower = __bond_sk_get_lower_dev(bond, sk);
5086 rcu_read_unlock();
5087
5088 return lower;
5089}
5090
Tariq Toukan89df6a82021-01-17 16:59:46 +02005091#if IS_ENABLED(CONFIG_TLS_DEVICE)
5092static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb,
5093 struct net_device *dev)
5094{
5095 if (likely(bond_get_slave_by_dev(bond, tls_get_ctx(skb->sk)->netdev)))
5096 return bond_dev_queue_xmit(bond, skb, tls_get_ctx(skb->sk)->netdev);
5097 return bond_tx_drop(dev, skb);
5098}
5099#endif
5100
Michał Mirosław0693e882011-05-07 01:48:02 +00005101static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08005102{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005103 struct bonding *bond = netdev_priv(dev);
5104
Veaceslav Falicod1e2e5c2014-05-15 21:39:52 +02005105 if (bond_should_override_tx_queue(bond) &&
5106 !bond_slave_override(bond, skb))
5107 return NETDEV_TX_OK;
Stephen Hemminger00829822008-11-20 20:14:53 -08005108
Tariq Toukan89df6a82021-01-17 16:59:46 +02005109#if IS_ENABLED(CONFIG_TLS_DEVICE)
5110 if (skb->sk && tls_is_sk_tx_device_offloaded(skb->sk))
5111 return bond_tls_device_xmit(bond, skb, dev);
5112#endif
5113
Veaceslav Falico01844092014-05-15 21:39:55 +02005114 switch (BOND_MODE(bond)) {
Stephen Hemminger00829822008-11-20 20:14:53 -08005115 case BOND_MODE_ROUNDROBIN:
5116 return bond_xmit_roundrobin(skb, dev);
5117 case BOND_MODE_ACTIVEBACKUP:
5118 return bond_xmit_activebackup(skb, dev);
Mahesh Bandewaree637712014-10-04 17:45:01 -07005119 case BOND_MODE_8023AD:
Stephen Hemminger00829822008-11-20 20:14:53 -08005120 case BOND_MODE_XOR:
Mahesh Bandewaree637712014-10-04 17:45:01 -07005121 return bond_3ad_xor_xmit(skb, dev);
Stephen Hemminger00829822008-11-20 20:14:53 -08005122 case BOND_MODE_BROADCAST:
5123 return bond_xmit_broadcast(skb, dev);
Stephen Hemminger00829822008-11-20 20:14:53 -08005124 case BOND_MODE_ALB:
Stephen Hemminger00829822008-11-20 20:14:53 -08005125 return bond_alb_xmit(skb, dev);
Mahesh Bandewarf05b42e2014-04-22 16:30:20 -07005126 case BOND_MODE_TLB:
5127 return bond_tlb_xmit(skb, dev);
Stephen Hemminger00829822008-11-20 20:14:53 -08005128 default:
5129 /* Should never happen, mode already checked */
Veaceslav Falico76444f52014-07-15 19:35:58 +02005130 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
Stephen Hemminger00829822008-11-20 20:14:53 -08005131 WARN_ON_ONCE(1);
Eric Dumazetae46f1842020-05-07 09:32:22 -07005132 return bond_tx_drop(dev, skb);
Stephen Hemminger00829822008-11-20 20:14:53 -08005133 }
5134}
5135
Michał Mirosław0693e882011-05-07 01:48:02 +00005136static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
5137{
5138 struct bonding *bond = netdev_priv(dev);
5139 netdev_tx_t ret = NETDEV_TX_OK;
5140
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005141 /* If we risk deadlock from transmitting this in the
Michał Mirosław0693e882011-05-07 01:48:02 +00005142 * netpoll path, tell netpoll to queue the frame for later tx
5143 */
dingtianhong054bb882014-03-25 17:00:09 +08005144 if (unlikely(is_netpoll_tx_blocked(dev)))
Michał Mirosław0693e882011-05-07 01:48:02 +00005145 return NETDEV_TX_BUSY;
5146
nikolay@redhat.com278b2082013-08-01 16:54:51 +02005147 rcu_read_lock();
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02005148 if (bond_has_slaves(bond))
Michał Mirosław0693e882011-05-07 01:48:02 +00005149 ret = __bond_start_xmit(skb, dev);
5150 else
Eric Dumazetae46f1842020-05-07 09:32:22 -07005151 ret = bond_tx_drop(dev, skb);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02005152 rcu_read_unlock();
Michał Mirosław0693e882011-05-07 01:48:02 +00005153
5154 return ret;
5155}
Stephen Hemminger00829822008-11-20 20:14:53 -08005156
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00005157static struct net_device *
5158bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp)
5159{
5160 struct bonding *bond = netdev_priv(bond_dev);
5161 struct slave *slave;
5162
5163 /* Caller needs to hold rcu_read_lock() */
5164
5165 switch (BOND_MODE(bond)) {
5166 case BOND_MODE_ROUNDROBIN:
5167 slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp);
5168 break;
5169
5170 case BOND_MODE_ACTIVEBACKUP:
5171 slave = bond_xmit_activebackup_slave_get(bond);
5172 break;
5173
5174 case BOND_MODE_8023AD:
5175 case BOND_MODE_XOR:
5176 slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp);
5177 break;
5178
5179 default:
5180 /* Should never happen. Mode guarded by bond_xdp_check() */
5181 netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n", BOND_MODE(bond));
5182 WARN_ON_ONCE(1);
5183 return NULL;
5184 }
5185
5186 if (slave)
5187 return slave->dev;
5188
5189 return NULL;
5190}
5191
5192static int bond_xdp_xmit(struct net_device *bond_dev,
5193 int n, struct xdp_frame **frames, u32 flags)
5194{
5195 int nxmit, err = -ENXIO;
5196
5197 rcu_read_lock();
5198
5199 for (nxmit = 0; nxmit < n; nxmit++) {
5200 struct xdp_frame *frame = frames[nxmit];
5201 struct xdp_frame *frames1[] = {frame};
5202 struct net_device *slave_dev;
5203 struct xdp_buff xdp;
5204
5205 xdp_convert_frame_to_buff(frame, &xdp);
5206
5207 slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp);
5208 if (!slave_dev) {
5209 err = -ENXIO;
5210 break;
5211 }
5212
5213 err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags);
5214 if (err < 1)
5215 break;
5216 }
5217
5218 rcu_read_unlock();
5219
5220 /* If error happened on the first frame then we can pass the error up, otherwise
5221 * report the number of frames that were xmitted.
5222 */
5223 if (err < 0)
5224 return (nxmit == 0 ? err : nxmit);
5225
5226 return nxmit;
5227}
5228
5229static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
5230 struct netlink_ext_ack *extack)
5231{
5232 struct bonding *bond = netdev_priv(dev);
5233 struct list_head *iter;
5234 struct slave *slave, *rollback_slave;
5235 struct bpf_prog *old_prog;
5236 struct netdev_bpf xdp = {
5237 .command = XDP_SETUP_PROG,
5238 .flags = 0,
5239 .prog = prog,
5240 .extack = extack,
5241 };
5242 int err;
5243
5244 ASSERT_RTNL();
5245
5246 if (!bond_xdp_check(bond))
5247 return -EOPNOTSUPP;
5248
5249 old_prog = bond->xdp_prog;
5250 bond->xdp_prog = prog;
5251
5252 bond_for_each_slave(bond, slave, iter) {
5253 struct net_device *slave_dev = slave->dev;
5254
5255 if (!slave_dev->netdev_ops->ndo_bpf ||
5256 !slave_dev->netdev_ops->ndo_xdp_xmit) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04005257 SLAVE_NL_ERR(dev, slave_dev, extack,
5258 "Slave device does not support XDP");
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00005259 err = -EOPNOTSUPP;
5260 goto err;
5261 }
5262
5263 if (dev_xdp_prog_count(slave_dev) > 0) {
Jonathan Toppins6569fa22021-08-10 22:53:31 -04005264 SLAVE_NL_ERR(dev, slave_dev, extack,
5265 "Slave has XDP program loaded, please unload before enslaving");
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00005266 err = -EOPNOTSUPP;
5267 goto err;
5268 }
5269
5270 err = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
5271 if (err < 0) {
5272 /* ndo_bpf() sets extack error message */
5273 slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err);
5274 goto err;
5275 }
5276 if (prog)
5277 bpf_prog_inc(prog);
5278 }
5279
Jussi Maki6d5f1ef2021-09-06 10:56:37 +02005280 if (prog) {
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00005281 static_branch_inc(&bpf_master_redirect_enabled_key);
Jussi Maki6d5f1ef2021-09-06 10:56:37 +02005282 } else if (old_prog) {
5283 bpf_prog_put(old_prog);
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00005284 static_branch_dec(&bpf_master_redirect_enabled_key);
Jussi Maki6d5f1ef2021-09-06 10:56:37 +02005285 }
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00005286
5287 return 0;
5288
5289err:
5290 /* unwind the program changes */
5291 bond->xdp_prog = old_prog;
5292 xdp.prog = old_prog;
5293 xdp.extack = NULL; /* do not overwrite original error */
5294
5295 bond_for_each_slave(bond, rollback_slave, iter) {
5296 struct net_device *slave_dev = rollback_slave->dev;
5297 int err_unwind;
5298
5299 if (slave == rollback_slave)
5300 break;
5301
5302 err_unwind = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
5303 if (err_unwind < 0)
5304 slave_err(dev, slave_dev,
5305 "Error %d when unwinding XDP program change\n", err_unwind);
5306 else if (xdp.prog)
5307 bpf_prog_inc(xdp.prog);
5308 }
5309 return err;
5310}
5311
5312static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp)
5313{
5314 switch (xdp->command) {
5315 case XDP_SETUP_PROG:
5316 return bond_xdp_set(dev, xdp->prog, xdp->extack);
5317 default:
5318 return -EINVAL;
5319 }
5320}
5321
Jarod Wilson4ca0d9a2020-08-13 10:09:00 -04005322static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
5323{
5324 if (speed == 0 || speed == SPEED_UNKNOWN)
5325 speed = slave->speed;
5326 else
5327 speed = min(speed, slave->speed);
5328
5329 return speed;
5330}
5331
Philippe Reynesd46b6342016-10-25 18:41:31 +02005332static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
5333 struct ethtool_link_ksettings *cmd)
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00005334{
5335 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02005336 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02005337 struct slave *slave;
Jarod Wilson4ca0d9a2020-08-13 10:09:00 -04005338 u32 speed = 0;
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00005339
Philippe Reynesd46b6342016-10-25 18:41:31 +02005340 cmd->base.duplex = DUPLEX_UNKNOWN;
5341 cmd->base.port = PORT_OTHER;
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00005342
Veaceslav Falico8557cd72014-05-15 21:39:59 +02005343 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00005344 * do not need to check mode. Though link speed might not represent
5345 * the true receive or transmit bandwidth (not all modes are symmetric)
5346 * this is an accurate maximum.
5347 */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02005348 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8557cd72014-05-15 21:39:59 +02005349 if (bond_slave_can_tx(slave)) {
Jarod Wilson4ca0d9a2020-08-13 10:09:00 -04005350 if (slave->speed != SPEED_UNKNOWN) {
5351 if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
5352 speed = bond_mode_bcast_speed(slave,
5353 speed);
5354 else
5355 speed += slave->speed;
5356 }
Philippe Reynesd46b6342016-10-25 18:41:31 +02005357 if (cmd->base.duplex == DUPLEX_UNKNOWN &&
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00005358 slave->duplex != DUPLEX_UNKNOWN)
Philippe Reynesd46b6342016-10-25 18:41:31 +02005359 cmd->base.duplex = slave->duplex;
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00005360 }
5361 }
Philippe Reynesd46b6342016-10-25 18:41:31 +02005362 cmd->base.speed = speed ? : SPEED_UNKNOWN;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02005363
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00005364 return 0;
5365}
5366
Jay Vosburgh217df672005-09-26 16:11:50 -07005367static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
Jiri Pirko7826d432013-01-06 00:44:26 +00005368 struct ethtool_drvinfo *drvinfo)
Jay Vosburgh217df672005-09-26 16:11:50 -07005369{
Jiri Pirko7826d432013-01-06 00:44:26 +00005370 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00005371 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
5372 BOND_ABI_VERSION);
Jay Vosburgh217df672005-09-26 16:11:50 -07005373}
5374
Hangbin Liu94dd0162021-11-30 15:09:32 +08005375static int bond_ethtool_get_ts_info(struct net_device *bond_dev,
5376 struct ethtool_ts_info *info)
5377{
5378 struct bonding *bond = netdev_priv(bond_dev);
5379 const struct ethtool_ops *ops;
5380 struct net_device *real_dev;
5381 struct phy_device *phydev;
5382
Hangbin Liu94dd0162021-11-30 15:09:32 +08005383 real_dev = bond_option_active_slave_get_rcu(bond);
Hangbin Liu94dd0162021-11-30 15:09:32 +08005384 if (real_dev) {
5385 ops = real_dev->ethtool_ops;
5386 phydev = real_dev->phydev;
5387
5388 if (phy_has_tsinfo(phydev)) {
5389 return phy_ts_info(phydev, info);
5390 } else if (ops->get_ts_info) {
5391 return ops->get_ts_info(real_dev, info);
5392 }
5393 }
5394
5395 info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
5396 SOF_TIMESTAMPING_SOFTWARE;
5397 info->phc_index = -1;
5398
5399 return 0;
5400}
5401
Jeff Garzik7282d492006-09-13 14:30:00 -04005402static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07005403 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04005404 .get_link = ethtool_op_get_link,
Philippe Reynesd46b6342016-10-25 18:41:31 +02005405 .get_link_ksettings = bond_ethtool_get_link_ksettings,
Hangbin Liu94dd0162021-11-30 15:09:32 +08005406 .get_ts_info = bond_ethtool_get_ts_info,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04005407};
5408
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08005409static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00005410 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00005411 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08005412 .ndo_open = bond_open,
5413 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08005414 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005415 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00005416 .ndo_get_stats64 = bond_get_stats,
Arnd Bergmanna7605372021-07-27 15:45:13 +02005417 .ndo_eth_ioctl = bond_eth_ioctl,
Arnd Bergmann3d9d00bd2021-07-27 15:45:17 +02005418 .ndo_siocbond = bond_do_ioctl,
Arnd Bergmann232ec982021-07-27 15:44:54 +02005419 .ndo_siocdevprivate = bond_siocdevprivate,
Jiri Pirkod03462b2011-08-16 03:15:04 +00005420 .ndo_change_rx_flags = bond_change_rx_flags,
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00005421 .ndo_set_rx_mode = bond_set_rx_mode,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08005422 .ndo_change_mtu = bond_change_mtu,
Jiri Pirkocc0e4072011-07-20 04:54:46 +00005423 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08005424 .ndo_neigh_setup = bond_neigh_setup,
Jiri Pirkocc0e4072011-07-20 04:54:46 +00005425 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08005426 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07005427#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00005428 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07005429 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
5430 .ndo_poll_controller = bond_poll_controller,
5431#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00005432 .ndo_add_slave = bond_enslave,
5433 .ndo_del_slave = bond_release,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00005434 .ndo_fix_features = bond_fix_features,
Toshiaki Makita4847f042015-03-27 14:31:14 +09005435 .ndo_features_check = passthru_features_check,
Maor Gottlieb33720aa2020-04-30 22:21:39 +03005436 .ndo_get_xmit_slave = bond_xmit_get_slave,
Tariq Toukan007feb82021-01-17 16:59:44 +02005437 .ndo_sk_get_lower_dev = bond_sk_get_lower_dev,
Jussi Maki9e2ee5c2021-07-31 05:57:34 +00005438 .ndo_bpf = bond_xdp,
5439 .ndo_xdp_xmit = bond_xdp_xmit,
5440 .ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08005441};
5442
Doug Goldsteinb3f92b62013-02-18 14:59:23 +00005443static const struct device_type bond_type = {
5444 .name = "bond",
5445};
5446
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005447static void bond_destructor(struct net_device *bond_dev)
5448{
5449 struct bonding *bond = netdev_priv(bond_dev);
Yufeng Mo86a5ad02021-05-20 14:18:32 +08005450
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005451 if (bond->wq)
5452 destroy_workqueue(bond->wq);
Jussi Maki848ca912021-06-15 08:54:15 +00005453
5454 if (bond->rr_tx_counter)
5455 free_percpu(bond->rr_tx_counter);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005456}
5457
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005458void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
Wang Chen454d7c92008-11-12 23:37:49 -08005460 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02005462 spin_lock_init(&bond->mode_lock);
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00005463 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
5465 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 bond->dev = bond_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
5468 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00005469 ether_setup(bond_dev);
WANG Cong31c05412017-03-02 12:24:36 -08005470 bond_dev->max_mtu = ETH_MAX_MTU;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08005471 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04005472 bond_dev->ethtool_ops = &bond_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
David S. Millercf124db2017-05-08 12:52:56 -04005474 bond_dev->needs_free_netdev = true;
5475 bond_dev->priv_destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
Doug Goldsteinb3f92b62013-02-18 14:59:23 +00005477 SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
5478
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 /* Initialize the device options */
Zhang Shengju1098cee2016-03-16 09:59:15 +00005480 bond_dev->flags |= IFF_MASTER;
Phil Sutter1e6f20c2015-08-18 10:30:39 +02005481 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
Neil Horman550fd082011-07-26 06:05:38 +00005482 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005483
Jarod Wilson18cb2612020-06-19 10:31:55 -04005484#ifdef CONFIG_XFRM_OFFLOAD
5485 /* set up xfrm device ops (only supported in active-backup right now) */
Jarod Wilsona3b658c2020-06-30 14:49:41 -04005486 bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
Taehee Yoo9a560552021-07-05 15:38:12 +00005487 INIT_LIST_HEAD(&bond->ipsec_list);
5488 spin_lock_init(&bond->ipsec_lock);
Jarod Wilson18cb2612020-06-19 10:31:55 -04005489#endif /* CONFIG_XFRM_OFFLOAD */
5490
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005491 /* don't acquire bond device's netif_tx_lock when transmitting */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 bond_dev->features |= NETIF_F_LLTX;
5493
5494 /* By default, we declare the bond to be fully
5495 * VLAN hardware accelerated capable. Special
5496 * care is taken in the various xmit functions
5497 * when there are slaves that are not hw accel
5498 * capable
5499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Weilong Chenf9399812014-01-22 17:16:30 +08005501 /* Don't allow bond devices to change network namespaces. */
5502 bond_dev->features |= NETIF_F_NETNS_LOCAL;
5503
Michał Mirosławb2a103e2011-05-07 03:22:17 +00005504 bond_dev->hw_features = BOND_VLAN_FEATURES |
Patrick McHardyf6469682013-04-19 02:04:27 +00005505 NETIF_F_HW_VLAN_CTAG_RX |
5506 NETIF_F_HW_VLAN_CTAG_FILTER;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00005507
Alexander Lobakinecb8fed2020-11-01 13:17:17 +00005508 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00005509 bond_dev->features |= bond_dev->hw_features;
YueHaibing30d81772019-06-26 16:08:44 +08005510 bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Jarod Wilsona3b658c2020-06-30 14:49:41 -04005511#ifdef CONFIG_XFRM_OFFLOAD
Jarod Wilsond241b382020-12-05 12:40:03 -05005512 bond_dev->hw_features |= BOND_XFRM_FEATURES;
5513 /* Only enable XFRM features if this is an active-backup config */
5514 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
5515 bond_dev->features |= BOND_XFRM_FEATURES;
Jarod Wilsona3b658c2020-06-30 14:49:41 -04005516#endif /* CONFIG_XFRM_OFFLOAD */
Tariq Toukan89df6a82021-01-17 16:59:46 +02005517#if IS_ENABLED(CONFIG_TLS_DEVICE)
5518 if (bond_sk_check(bond))
5519 bond_dev->features |= BOND_TLS_FEATURES;
5520#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005523/* Destroy a bonding device.
5524 * Must be under rtnl_lock when this function is called.
5525 */
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00005526static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07005527{
Wang Chen454d7c92008-11-12 23:37:49 -08005528 struct bonding *bond = netdev_priv(bond_dev);
Maor Gottlieb6b447e72020-04-30 22:21:38 +03005529 struct bond_up_slave *usable, *all;
Veaceslav Falico544a0282013-09-25 09:20:15 +02005530 struct list_head *iter;
5531 struct slave *slave;
Jay Vosburgha434e432008-10-30 17:41:15 -07005532
WANG Congf6dc31a2010-05-06 00:48:51 -07005533 bond_netpoll_cleanup(bond_dev);
5534
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00005535 /* Release the bonded slaves */
Veaceslav Falico544a0282013-09-25 09:20:15 +02005536 bond_for_each_slave(bond, slave, iter)
WANG Congf51048c2017-07-06 15:01:57 -07005537 __bond_release_one(bond_dev, slave->dev, true, true);
Veaceslav Falico76444f52014-07-15 19:35:58 +02005538 netdev_info(bond_dev, "Released all slaves\n");
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00005539
Maor Gottlieb6b447e72020-04-30 22:21:38 +03005540 usable = rtnl_dereference(bond->usable_slaves);
5541 if (usable) {
Maor Gottliebed7d4f02020-04-30 22:21:33 +03005542 RCU_INIT_POINTER(bond->usable_slaves, NULL);
Maor Gottlieb6b447e72020-04-30 22:21:38 +03005543 kfree_rcu(usable, rcu);
5544 }
5545
5546 all = rtnl_dereference(bond->all_slaves);
5547 if (all) {
5548 RCU_INIT_POINTER(bond->all_slaves, NULL);
5549 kfree_rcu(all, rcu);
Mahesh Bandewaree637712014-10-04 17:45:01 -07005550 }
5551
Jay Vosburgha434e432008-10-30 17:41:15 -07005552 list_del(&bond->bond_list);
5553
Taku Izumif073c7c2010-12-09 15:17:13 +00005554 bond_debug_unregister(bond);
Jay Vosburgha434e432008-10-30 17:41:15 -07005555}
5556
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557/*------------------------- Module initialization ---------------------------*/
5558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559static int bond_check_params(struct bond_params *params)
5560{
nikolay@redhat.com5a5c5fd2013-05-18 01:18:30 +00005561 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
stephen hemmingerf3253332014-03-04 16:36:44 -08005562 struct bond_opt_value newval;
5563 const struct bond_opt_value *valptr;
Jarod Wilson72ccc472017-05-19 14:46:46 -04005564 int arp_all_targets_value = 0;
Mahesh Bandewar6791e462015-05-09 00:01:55 -07005565 u16 ad_actor_sys_prio = 0;
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07005566 u16 ad_user_port_key = 0;
Jarod Wilson72ccc472017-05-19 14:46:46 -04005567 __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
Mahesh Bandewardc9c4d02017-03-08 10:56:02 -08005568 int arp_ip_count;
5569 int bond_mode = BOND_MODE_ROUNDROBIN;
5570 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
5571 int lacp_fast = 0;
Nikolay Aleksandrovf13ad102017-09-12 15:10:05 +03005572 int tlb_dynamic_lb;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005573
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005574 /* Convert string parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 if (mode) {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +01005576 bond_opt_initstr(&newval, mode);
5577 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
5578 if (!valptr) {
5579 pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 return -EINVAL;
5581 }
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +01005582 bond_mode = valptr->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 }
5584
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005585 if (xmit_hash_policy) {
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04005586 if (bond_mode == BOND_MODE_ROUNDROBIN ||
5587 bond_mode == BOND_MODE_ACTIVEBACKUP ||
5588 bond_mode == BOND_MODE_BROADCAST) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005589 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Joe Perches90194262014-02-15 16:01:45 -08005590 bond_mode_name(bond_mode));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005591 } else {
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01005592 bond_opt_initstr(&newval, xmit_hash_policy);
5593 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
5594 &newval);
5595 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005596 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005597 xmit_hash_policy);
5598 return -EINVAL;
5599 }
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01005600 xmit_hashtype = valptr->value;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005601 }
5602 }
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 if (lacp_rate) {
5605 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005606 pr_info("lacp_rate param is irrelevant in mode %s\n",
5607 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 } else {
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01005609 bond_opt_initstr(&newval, lacp_rate);
5610 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
5611 &newval);
5612 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005613 pr_err("Error: Invalid lacp rate \"%s\"\n",
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01005614 lacp_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 return -EINVAL;
5616 }
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01005617 lacp_fast = valptr->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 }
5619 }
5620
Jay Vosburghfd989c82008-11-04 17:51:16 -08005621 if (ad_select) {
Nikolay Aleksandrov548d28b2014-07-13 09:47:47 +02005622 bond_opt_initstr(&newval, ad_select);
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01005623 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
5624 &newval);
5625 if (!valptr) {
5626 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -08005627 return -EINVAL;
5628 }
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01005629 params->ad_select = valptr->value;
5630 if (bond_mode != BOND_MODE_8023AD)
Joe Perches91565eb2014-02-15 15:57:04 -08005631 pr_warn("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08005632 } else {
5633 params->ad_select = BOND_AD_STABLE;
5634 }
5635
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00005636 if (max_bonds < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005637 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
5638 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 max_bonds = BOND_DEFAULT_MAX_BONDS;
5640 }
5641
5642 if (miimon < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005643 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5644 miimon, INT_MAX);
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +01005645 miimon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 }
5647
5648 if (updelay < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005649 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5650 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 updelay = 0;
5652 }
5653
5654 if (downdelay < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005655 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5656 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 downdelay = 0;
5658 }
5659
Debabrata Banerjeeb3c898e2018-05-16 14:02:13 -04005660 if ((use_carrier != 0) && (use_carrier != 1)) {
5661 pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
Joe Perches91565eb2014-02-15 15:57:04 -08005662 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 use_carrier = 1;
5664 }
5665
Ben Hutchingsad246c92011-04-26 15:25:52 +00005666 if (num_peer_notif < 0 || num_peer_notif > 255) {
Joe Perches91565eb2014-02-15 15:57:04 -08005667 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
5668 num_peer_notif);
Ben Hutchingsad246c92011-04-26 15:25:52 +00005669 num_peer_notif = 1;
5670 }
5671
dingtianhong834db4b2013-12-21 14:40:17 +08005672 /* reset values for 802.3ad/TLB/ALB */
Veaceslav Falico267bed72014-05-15 21:39:53 +02005673 if (!bond_mode_uses_arp(bond_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 if (!miimon) {
Joe Perches91565eb2014-02-15 15:57:04 -08005675 pr_warn("Warning: miimon must be specified, otherwise bonding will not detect link failure, speed and duplex which are essential for 802.3ad operation\n");
5676 pr_warn("Forcing miimon to 100msec\n");
dingtianhongfe9d04a2013-11-22 22:28:43 +08005677 miimon = BOND_DEFAULT_MIIMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 }
5679 }
5680
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005681 if (tx_queues < 1 || tx_queues > 255) {
Joe Perches91565eb2014-02-15 15:57:04 -08005682 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
5683 tx_queues, BOND_DEFAULT_TX_QUEUES);
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005684 tx_queues = BOND_DEFAULT_TX_QUEUES;
5685 }
5686
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005687 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
Joe Perches91565eb2014-02-15 15:57:04 -08005688 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
5689 all_slaves_active);
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005690 all_slaves_active = 0;
5691 }
5692
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005693 if (resend_igmp < 0 || resend_igmp > 255) {
Joe Perches91565eb2014-02-15 15:57:04 -08005694 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
5695 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005696 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
5697 }
5698
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01005699 bond_opt_initval(&newval, packets_per_slave);
5700 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01005701 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
5702 packets_per_slave, USHRT_MAX);
5703 packets_per_slave = 1;
5704 }
5705
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005707 pr_notice("In ALB mode you might experience client disconnections upon reconnection of a link if the bonding module updelay parameter (%d msec) is incompatible with the forwarding delay time of the switch\n",
5708 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 }
5710
5711 if (!miimon) {
5712 if (updelay || downdelay) {
5713 /* just warn the user the up/down delay will have
5714 * no effect since miimon is zero...
5715 */
Joe Perches91565eb2014-02-15 15:57:04 -08005716 pr_warn("Warning: miimon module parameter not set and updelay (%d) or downdelay (%d) module parameter is set; updelay and downdelay have no effect unless miimon is set\n",
5717 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 }
5719 } else {
5720 /* don't allow arp monitoring */
5721 if (arp_interval) {
Joe Perches91565eb2014-02-15 15:57:04 -08005722 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
5723 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 arp_interval = 0;
5725 }
5726
5727 if ((updelay % miimon) != 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005728 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
5729 updelay, miimon, (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 }
5731
5732 updelay /= miimon;
5733
5734 if ((downdelay % miimon) != 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005735 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
5736 downdelay, miimon,
5737 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 }
5739
5740 downdelay /= miimon;
5741 }
5742
5743 if (arp_interval < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005744 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
5745 arp_interval, INT_MAX);
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +01005746 arp_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 }
5748
nikolay@redhat.com5a5c5fd2013-05-18 01:18:30 +00005749 for (arp_ip_count = 0, i = 0;
5750 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
dingtianhong89015c12013-12-04 18:59:31 +08005751 __be32 ip;
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005752
5753 /* not a complete check, but good enough to catch mistakes */
dingtianhong89015c12013-12-04 18:59:31 +08005754 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
Veaceslav Falico2807a9f2014-05-15 21:39:56 +02005755 !bond_is_ip_target_ok(ip)) {
Joe Perches91565eb2014-02-15 15:57:04 -08005756 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
5757 arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 arp_interval = 0;
5759 } else {
Veaceslav Falico0afee4e2013-06-24 11:49:30 +02005760 if (bond_get_targets_ip(arp_target, ip) == -1)
5761 arp_target[arp_ip_count++] = ip;
5762 else
Joe Perches91565eb2014-02-15 15:57:04 -08005763 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
5764 &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 }
5766 }
5767
5768 if (arp_interval && !arp_ip_count) {
5769 /* don't allow arping if no arp_ip_target given... */
Joe Perches91565eb2014-02-15 15:57:04 -08005770 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
5771 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 arp_interval = 0;
5773 }
5774
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005775 if (arp_validate) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005776 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005777 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005778 return -EINVAL;
5779 }
5780
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005781 bond_opt_initstr(&newval, arp_validate);
5782 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
5783 &newval);
5784 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005785 pr_err("Error: invalid arp_validate \"%s\"\n",
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005786 arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005787 return -EINVAL;
5788 }
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005789 arp_validate_value = valptr->value;
5790 } else {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005791 arp_validate_value = 0;
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005792 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005793
Veaceslav Falico8599b522013-06-24 11:49:34 +02005794 if (arp_all_targets) {
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +01005795 bond_opt_initstr(&newval, arp_all_targets);
5796 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
5797 &newval);
5798 if (!valptr) {
Veaceslav Falico8599b522013-06-24 11:49:34 +02005799 pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
5800 arp_all_targets);
5801 arp_all_targets_value = 0;
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +01005802 } else {
5803 arp_all_targets_value = valptr->value;
Veaceslav Falico8599b522013-06-24 11:49:34 +02005804 }
5805 }
5806
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005808 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 } else if (arp_interval) {
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005810 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
5811 arp_validate_value);
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005812 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005813 arp_interval, valptr->string, arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
5815 for (i = 0; i < arp_ip_count; i++)
Joe Perches90194262014-02-15 16:01:45 -08005816 pr_cont(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Joe Perches90194262014-02-15 16:01:45 -08005818 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819
Jay Vosburghb8a97872008-06-13 18:12:04 -07005820 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 /* miimon and arp_interval not set, we need one so things
5822 * work as expected, see bonding.txt for details
5823 */
Joe Perches90194262014-02-15 16:01:45 -08005824 pr_debug("Warning: either miimon or arp_interval and arp_ip_target module parameters must be specified, otherwise bonding will not detect link failures! see bonding.txt for details\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 }
5826
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02005827 if (primary && !bond_mode_uses_primary(bond_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 /* currently, using a primary only makes sense
5829 * in active backup, TLB or ALB modes
5830 */
Joe Perches91565eb2014-02-15 15:57:04 -08005831 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
5832 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 primary = NULL;
5834 }
5835
Jiri Pirkoa5499522009-09-25 03:28:09 +00005836 if (primary && primary_reselect) {
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01005837 bond_opt_initstr(&newval, primary_reselect);
5838 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
5839 &newval);
5840 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005841 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01005842 primary_reselect);
Jiri Pirkoa5499522009-09-25 03:28:09 +00005843 return -EINVAL;
5844 }
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01005845 primary_reselect_value = valptr->value;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005846 } else {
5847 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5848 }
5849
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005850 if (fail_over_mac) {
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01005851 bond_opt_initstr(&newval, fail_over_mac);
5852 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
5853 &newval);
5854 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005855 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01005856 fail_over_mac);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005857 return -EINVAL;
5858 }
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01005859 fail_over_mac_value = valptr->value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005860 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perches91565eb2014-02-15 15:57:04 -08005861 pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005862 } else {
5863 fail_over_mac_value = BOND_FOM_NONE;
5864 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005865
Mahesh Bandewar6791e462015-05-09 00:01:55 -07005866 bond_opt_initstr(&newval, "default");
5867 valptr = bond_opt_parse(
5868 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
5869 &newval);
5870 if (!valptr) {
5871 pr_err("Error: No ad_actor_sys_prio default value");
5872 return -EINVAL;
5873 }
5874 ad_actor_sys_prio = valptr->value;
5875
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07005876 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
5877 &newval);
5878 if (!valptr) {
5879 pr_err("Error: No ad_user_port_key default value");
5880 return -EINVAL;
5881 }
5882 ad_user_port_key = valptr->value;
5883
Nikolay Aleksandrovf13ad102017-09-12 15:10:05 +03005884 bond_opt_initstr(&newval, "default");
5885 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
5886 if (!valptr) {
5887 pr_err("Error: No tlb_dynamic_lb default value");
5888 return -EINVAL;
Mahesh Bandewar8b426dc2017-03-08 10:55:56 -08005889 }
Nikolay Aleksandrovf13ad102017-09-12 15:10:05 +03005890 tlb_dynamic_lb = valptr->value;
Mahesh Bandewar8b426dc2017-03-08 10:55:56 -08005891
dingtianhong3a7129e2013-12-21 14:40:12 +08005892 if (lp_interval == 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005893 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
5894 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
dingtianhong3a7129e2013-12-21 14:40:12 +08005895 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
5896 }
5897
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 /* fill params struct with the proper values */
5899 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005900 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 params->miimon = miimon;
Ben Hutchingsad246c92011-04-26 15:25:52 +00005902 params->num_peer_notif = num_peer_notif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005904 params->arp_validate = arp_validate_value;
Veaceslav Falico8599b522013-06-24 11:49:34 +02005905 params->arp_all_targets = arp_all_targets_value;
Hangbin Liu5944b5a2021-11-30 12:29:47 +08005906 params->missed_max = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 params->updelay = updelay;
5908 params->downdelay = downdelay;
Vincent Bernat07a4dde2019-07-02 19:43:54 +02005909 params->peer_notif_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 params->use_carrier = use_carrier;
Hangbin Liu3a755cd2021-08-02 11:02:19 +08005911 params->lacp_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 params->lacp_fast = lacp_fast;
5913 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005914 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005915 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005916 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005917 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005918 params->resend_igmp = resend_igmp;
stephen hemminger655f8912011-06-22 09:54:39 +00005919 params->min_links = min_links;
dingtianhong3a7129e2013-12-21 14:40:12 +08005920 params->lp_interval = lp_interval;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01005921 params->packets_per_slave = packets_per_slave;
Mahesh Bandewar8b426dc2017-03-08 10:55:56 -08005922 params->tlb_dynamic_lb = tlb_dynamic_lb;
Mahesh Bandewar6791e462015-05-09 00:01:55 -07005923 params->ad_actor_sys_prio = ad_actor_sys_prio;
Mahesh Bandewar74514952015-05-09 00:01:56 -07005924 eth_zero_addr(params->ad_actor_system);
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07005925 params->ad_user_port_key = ad_user_port_key;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01005926 if (packets_per_slave > 0) {
5927 params->reciprocal_packets_per_slave =
5928 reciprocal_value(packets_per_slave);
5929 } else {
5930 /* reciprocal_packets_per_slave is unused if
5931 * packets_per_slave is 0 or 1, just initialize it
5932 */
5933 params->reciprocal_packets_per_slave =
5934 (struct reciprocal_value) { 0 };
5935 }
5936
Kees Cook43902072021-06-02 13:58:20 -07005937 if (primary)
5938 strscpy_pad(params->primary, primary, sizeof(params->primary));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5941
5942 return 0;
5943}
5944
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005945/* Called from registration process */
Stephen Hemminger181470f2009-06-12 19:02:52 +00005946static int bond_init(struct net_device *bond_dev)
5947{
5948 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005949 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005950
Veaceslav Falico76444f52014-07-15 19:35:58 +02005951 netdev_dbg(bond_dev, "Begin bond_init\n");
Stephen Hemminger181470f2009-06-12 19:02:52 +00005952
Bhaktipriya Shridharf9f225e2016-08-30 22:02:01 +05305953 bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005954 if (!bond->wq)
5955 return -ENOMEM;
5956
Jussi Maki848ca912021-06-15 08:54:15 +00005957 if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN) {
5958 bond->rr_tx_counter = alloc_percpu(u32);
5959 if (!bond->rr_tx_counter) {
5960 destroy_workqueue(bond->wq);
5961 bond->wq = NULL;
5962 return -ENOMEM;
5963 }
5964 }
5965
Taehee Yoo089bca22019-10-21 18:47:53 +00005966 spin_lock_init(&bond->stats_lock);
Cong Wang1a33e102020-05-02 22:22:19 -07005967 netdev_lockdep_set_classes(bond_dev);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005968
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005969 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005970
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005971 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005972
Taku Izumif073c7c2010-12-09 15:17:13 +00005973 bond_debug_register(bond);
5974
Jiri Pirko409cc1f2013-01-30 11:08:11 +01005975 /* Ensure valid dev_addr */
5976 if (is_zero_ether_addr(bond_dev->dev_addr) &&
nikolay@redhat.com97a1e632013-06-26 17:13:38 +02005977 bond_dev->addr_assign_type == NET_ADDR_PERM)
Jiri Pirko409cc1f2013-01-30 11:08:11 +01005978 eth_hw_addr_random(bond_dev);
Jiri Pirko409cc1f2013-01-30 11:08:11 +01005979
Stephen Hemminger181470f2009-06-12 19:02:52 +00005980 return 0;
5981}
5982
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005983unsigned int bond_get_num_tx_queues(void)
Jiri Pirkod5da4512011-08-10 06:09:44 +00005984{
stephen hemmingerefacb302012-04-10 18:34:43 +00005985 return tx_queues;
Jiri Pirkod5da4512011-08-10 06:09:44 +00005986}
5987
Mitch Williamsdfe60392005-11-09 10:36:04 -08005988/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005989 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005990 * Caller must NOT hold rtnl_lock; we need to release it here before we
5991 * set up our sysfs entries.
5992 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005993int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005994{
5995 struct net_device *bond_dev;
Paie913fb22015-04-29 14:24:23 -04005996 struct bonding *bond;
5997 struct alb_bond_info *bond_info;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005998 int res;
5999
6000 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08006001
Jiri Pirko1c5cae82011-04-30 01:21:32 +00006002 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
Tom Gundersenc835a672014-07-14 16:37:24 +02006003 name ? name : "bond%d", NET_NAME_UNKNOWN,
Jiri Pirko1c5cae82011-04-30 01:21:32 +00006004 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08006005 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08006006 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00006007 rtnl_unlock();
6008 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08006009 }
6010
Paie913fb22015-04-29 14:24:23 -04006011 /*
6012 * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX.
6013 * It is set to 0 by default which is wrong.
6014 */
6015 bond = netdev_priv(bond_dev);
6016 bond_info = &(BOND_ALB_INFO(bond));
6017 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
6018
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006019 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00006020 bond_dev->rtnl_link_ops = &bond_link_ops;
6021
Mitch Williamsdfe60392005-11-09 10:36:04 -08006022 res = register_netdevice(bond_dev);
Taehee Yoo544f2872020-07-19 12:11:24 +00006023 if (res < 0) {
6024 free_netdev(bond_dev);
6025 rtnl_unlock();
6026
6027 return res;
6028 }
Peter Zijlstra0daa23032006-11-08 19:51:01 -08006029
Phil Oestere826eaf2011-03-14 06:22:05 +00006030 netif_carrier_off(bond_dev);
6031
Mahesh Bandewar4493b812017-03-08 10:55:54 -08006032 bond_work_init_all(bond);
6033
Mitch Williamsdfe60392005-11-09 10:36:04 -08006034 rtnl_unlock();
Taehee Yoo544f2872020-07-19 12:11:24 +00006035 return 0;
Mitch Williamsdfe60392005-11-09 10:36:04 -08006036}
6037
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006038static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006039{
Eric W. Biederman15449742009-11-29 15:46:04 +00006040 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006041
6042 bn->net = net;
6043 INIT_LIST_HEAD(&bn->dev_list);
6044
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006045 bond_create_proc_dir(bn);
Eric W. Biederman4c224002011-10-12 21:56:25 +00006046 bond_create_sysfs(bn);
Veaceslav Falico87a7b842013-06-24 11:49:29 +02006047
Eric W. Biederman15449742009-11-29 15:46:04 +00006048 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006049}
6050
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006051static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006052{
Eric W. Biederman15449742009-11-29 15:46:04 +00006053 struct bond_net *bn = net_generic(net, bond_net_id);
nikolay@redhat.com69b02162013-04-06 00:54:38 +00006054 struct bonding *bond, *tmp_bond;
6055 LIST_HEAD(list);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006056
Eric W. Biederman4c224002011-10-12 21:56:25 +00006057 bond_destroy_sysfs(bn);
nikolay@redhat.com69b02162013-04-06 00:54:38 +00006058
6059 /* Kill off any bonds created after unregistering bond rtnl ops */
6060 rtnl_lock();
6061 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
6062 unregister_netdevice_queue(bond->dev, &list);
6063 unregister_netdevice_many(&list);
6064 rtnl_unlock();
Veaceslav Falico23fa5c22014-07-17 12:04:08 +02006065
6066 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006067}
6068
6069static struct pernet_operations bond_net_ops = {
6070 .init = bond_net_init,
6071 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00006072 .id = &bond_net_id,
6073 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006074};
6075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076static int __init bonding_init(void)
6077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 int i;
6079 int res;
6080
Mitch Williamsdfe60392005-11-09 10:36:04 -08006081 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00006082 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08006083 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Eric W. Biederman15449742009-11-29 15:46:04 +00006085 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006086 if (res)
6087 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08006088
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02006089 res = bond_netlink_init();
Eric W. Biederman88ead972009-10-29 14:18:25 +00006090 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00006091 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00006092
Taku Izumif073c7c2010-12-09 15:17:13 +00006093 bond_create_debugfs();
6094
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00006096 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08006097 if (res)
6098 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 }
6100
Matteo Croce58deb772019-10-29 14:50:53 +01006101 skb_flow_dissector_init(&flow_keys_bonding,
6102 flow_keys_bonding_keys,
6103 ARRAY_SIZE(flow_keys_bonding_keys));
6104
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 register_netdevice_notifier(&bond_netdev_notifier);
Mitch Williamsdfe60392005-11-09 10:36:04 -08006106out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00006108err:
Thomas Richterdb298682014-04-09 12:52:59 +02006109 bond_destroy_debugfs();
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02006110 bond_netlink_fini();
Eric W. Biederman66391042009-10-29 23:58:54 +00006111err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00006112 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00006113 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08006114
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115}
6116
6117static void __exit bonding_exit(void)
6118{
6119 unregister_netdevice_notifier(&bond_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Taku Izumif073c7c2010-12-09 15:17:13 +00006121 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07006122
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02006123 bond_netlink_fini();
nikolay@redhat.comffcdedb2013-04-06 00:54:37 +00006124 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00006125
6126#ifdef CONFIG_NET_POLL_CONTROLLER
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02006127 /* Make sure we don't have an imbalance on our netpoll blocking */
Neil Hormanfb4fa762010-12-06 09:05:50 +00006128 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00006129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130}
6131
6132module_init(bonding_init);
6133module_exit(bonding_exit);
6134MODULE_LICENSE("GPL");
Leon Romanovsky2b526b52020-02-24 10:52:54 +02006135MODULE_DESCRIPTION(DRV_DESCRIPTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");