blob: 415a37e44caeb772983bf3cf790bf973031c59b1 [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>
38#include <linux/interrupt.h>
39#include <linux/ptrace.h>
40#include <linux/ioport.h>
41#include <linux/in.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040042#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/ip.h>
Matteo Crocedf98be02019-11-15 12:10:37 +010044#include <linux/icmp.h>
45#include <linux/icmpv6.h>
Jay Vosburgh169a3e62005-06-26 17:54:11 -040046#include <linux/tcp.h>
47#include <linux/udp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/slab.h>
49#include <linux/string.h>
50#include <linux/init.h>
51#include <linux/timer.h>
52#include <linux/socket.h>
53#include <linux/ctype.h>
54#include <linux/inet.h>
55#include <linux/bitops.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000056#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/dma.h>
Stephen Hemminger3d632c32009-06-12 19:02:48 +000058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/errno.h>
60#include <linux/netdevice.h>
61#include <linux/inetdevice.h>
Jay Vosburgha816c7c2007-02-28 17:03:37 -080062#include <linux/igmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/etherdevice.h>
64#include <linux/skbuff.h>
65#include <net/sock.h>
66#include <linux/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/smp.h>
68#include <linux/if_ether.h>
69#include <net/arp.h>
70#include <linux/mii.h>
71#include <linux/ethtool.h>
72#include <linux/if_vlan.h>
73#include <linux/if_bonding.h>
David Sterbab63bb732007-12-06 23:40:33 -080074#include <linux/jiffies.h>
Neil Hormane843fa52010-10-13 16:01:50 +000075#include <linux/preempt.h>
Jay Vosburghc3ade5c2005-06-26 17:52:20 -040076#include <net/route.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020077#include <net/net_namespace.h>
Eric W. Biedermanec87fd32009-10-29 14:18:26 +000078#include <net/netns/generic.h>
Eric Dumazet5ee31c682012-06-12 06:03:51 +000079#include <net/pkt_sched.h>
nikolay@redhat.com278b2082013-08-01 16:54:51 +020080#include <linux/rculist.h>
Jiri Pirko1bd758e2015-05-12 14:56:07 +020081#include <net/flow_dissector.h>
Jarod Wilson18cb2612020-06-19 10:31:55 -040082#include <net/xfrm.h>
David S. Miller1ef80192014-11-10 13:27:49 -050083#include <net/bonding.h>
84#include <net/bond_3ad.h>
85#include <net/bond_alb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Matan Barak73b5a6f22015-04-26 15:55:57 +030087#include "bonding_priv.h"
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*---------------------------- Module parameters ----------------------------*/
90
91/* monitor all links that often (in milliseconds). <=0 disables monitoring */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93static int max_bonds = BOND_DEFAULT_MAX_BONDS;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +000094static int tx_queues = BOND_DEFAULT_TX_QUEUES;
Ben Hutchingsad246c92011-04-26 15:25:52 +000095static int num_peer_notif = 1;
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +010096static int miimon;
Stephen Hemminger3d632c32009-06-12 19:02:48 +000097static int updelay;
98static int downdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int use_carrier = 1;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000100static char *mode;
101static char *primary;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000102static char *primary_reselect;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000103static char *lacp_rate;
stephen hemminger655f8912011-06-22 09:54:39 +0000104static int min_links;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000105static char *ad_select;
106static char *xmit_hash_policy;
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +0100107static int arp_interval;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000108static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
109static char *arp_validate;
Veaceslav Falico8599b522013-06-24 11:49:34 +0200110static char *arp_all_targets;
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000111static char *fail_over_mac;
dingtianhongb07ea072013-07-23 15:25:47 +0800112static int all_slaves_active;
Stephen Hemmingerd2991f72009-06-12 19:02:44 +0000113static struct bond_params bonding_defaults;
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000114static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +0100115static int packets_per_slave = 1;
dingtianhong3a7129e2013-12-21 14:40:12 +0800116static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118module_param(max_bonds, int, 0);
119MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
Andy Gospodarekbb1d9122010-06-02 08:40:18 +0000120module_param(tx_queues, int, 0);
121MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000122module_param_named(num_grat_arp, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000123MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
124 "failover event (alias of num_unsol_na)");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000125module_param_named(num_unsol_na, num_peer_notif, int, 0644);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000126MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
127 "failover event (alias of num_grat_arp)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128module_param(miimon, int, 0);
129MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
130module_param(updelay, int, 0);
131MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
132module_param(downdelay, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800133MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
134 "in milliseconds");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135module_param(use_carrier, int, 0);
Mitch Williams2ac47662005-11-09 10:35:03 -0800136MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
Debabrata Banerjeeb3c898e2018-05-16 14:02:13 -0400137 "0 for off, 1 for on (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138module_param(mode, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000139MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
Mitch Williams2ac47662005-11-09 10:35:03 -0800140 "1 for active-backup, 2 for balance-xor, "
141 "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
142 "6 for balance-alb");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143module_param(primary, charp, 0);
144MODULE_PARM_DESC(primary, "Primary network device to use");
Jiri Pirkoa5499522009-09-25 03:28:09 +0000145module_param(primary_reselect, charp, 0);
146MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
147 "once it comes up; "
148 "0 for always (default), "
149 "1 for only if speed of primary is "
150 "better, "
151 "2 for only on active slave "
152 "failure");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153module_param(lacp_rate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000154MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
155 "0 for slow, 1 for fast");
Jay Vosburghfd989c82008-11-04 17:51:16 -0800156module_param(ad_select, charp, 0);
Zhu Yanjun0d039f32016-08-09 21:36:04 +0800157MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
Andy Gospodarek90e62472011-05-25 04:41:59 +0000158 "0 for stable (default), 1 for bandwidth, "
159 "2 for count");
stephen hemminger655f8912011-06-22 09:54:39 +0000160module_param(min_links, int, 0);
161MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
162
Jay Vosburgh169a3e62005-06-26 17:54:11 -0400163module_param(xmit_hash_policy, charp, 0);
Debabrata Banerjeee79c1052018-05-14 14:48:09 -0400164MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
Andy Gospodarek90e62472011-05-25 04:41:59 +0000165 "0 for layer 2 (default), 1 for layer 3+4, "
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +0200166 "2 for layer 2+3, 3 for encap layer 2+3, "
167 "4 for encap layer 3+4");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168module_param(arp_interval, int, 0);
169MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
170module_param_array(arp_ip_target, charp, NULL, 0);
171MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
Jay Vosburghf5b2b962006-09-22 21:54:53 -0700172module_param(arp_validate, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000173MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
174 "0 for none (default), 1 for active, "
175 "2 for backup, 3 for all");
Veaceslav Falico8599b522013-06-24 11:49:34 +0200176module_param(arp_all_targets, charp, 0);
177MODULE_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 -0700178module_param(fail_over_mac, charp, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000179MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
180 "the same MAC; 0 for none (default), "
181 "1 for active, 2 for follow");
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000182module_param(all_slaves_active, int, 0);
Masanari Iida37b70212014-09-09 18:07:55 +0900183MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
Andy Gospodarek90e62472011-05-25 04:41:59 +0000184 "by setting active flag for all slaves; "
Andy Gospodarekebd8e492010-06-02 08:39:21 +0000185 "0 for never (default), 1 for always.");
Flavio Leitnerc2952c32010-10-05 14:23:59 +0000186module_param(resend_igmp, int, 0);
Andy Gospodarek90e62472011-05-25 04:41:59 +0000187MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
188 "link failure");
Nikolay Aleksandrov73958322013-11-05 13:51:41 +0100189module_param(packets_per_slave, int, 0);
190MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
191 "mode; 0 for a random slave, 1 packet per "
192 "slave (default), >1 packets per slave.");
dingtianhong3a7129e2013-12-21 14:40:12 +0800193module_param(lp_interval, uint, 0);
194MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
195 "the bonding driver sends learning packets to "
196 "each slaves peer switch. The default is 1.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/*----------------------------- Global variables ----------------------------*/
199
Neil Hormane843fa52010-10-13 16:01:50 +0000200#ifdef CONFIG_NET_POLL_CONTROLLER
Neil Hormanfb4fa762010-12-06 09:05:50 +0000201atomic_t netpoll_block_tx = ATOMIC_INIT(0);
Neil Hormane843fa52010-10-13 16:01:50 +0000202#endif
203
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300204unsigned int bond_net_id __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Matteo Croce58deb772019-10-29 14:50:53 +0100206static const struct flow_dissector_key flow_keys_bonding_keys[] = {
207 {
208 .key_id = FLOW_DISSECTOR_KEY_CONTROL,
209 .offset = offsetof(struct flow_keys, control),
210 },
211 {
212 .key_id = FLOW_DISSECTOR_KEY_BASIC,
213 .offset = offsetof(struct flow_keys, basic),
214 },
215 {
216 .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
217 .offset = offsetof(struct flow_keys, addrs.v4addrs),
218 },
219 {
220 .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
221 .offset = offsetof(struct flow_keys, addrs.v6addrs),
222 },
223 {
224 .key_id = FLOW_DISSECTOR_KEY_TIPC,
225 .offset = offsetof(struct flow_keys, addrs.tipckey),
226 },
227 {
228 .key_id = FLOW_DISSECTOR_KEY_PORTS,
229 .offset = offsetof(struct flow_keys, ports),
230 },
231 {
232 .key_id = FLOW_DISSECTOR_KEY_ICMP,
233 .offset = offsetof(struct flow_keys, icmp),
234 },
235 {
236 .key_id = FLOW_DISSECTOR_KEY_VLAN,
237 .offset = offsetof(struct flow_keys, vlan),
238 },
239 {
240 .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
241 .offset = offsetof(struct flow_keys, tags),
242 },
243 {
244 .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
245 .offset = offsetof(struct flow_keys, keyid),
246 },
247};
248
249static struct flow_dissector flow_keys_bonding __read_mostly;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*-------------------------- Forward declarations ---------------------------*/
252
Stephen Hemminger181470f2009-06-12 19:02:52 +0000253static int bond_init(struct net_device *bond_dev);
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +0000254static void bond_uninit(struct net_device *bond_dev);
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800255static void bond_get_stats(struct net_device *bond_dev,
256 struct rtnl_link_stats64 *stats);
Mahesh Bandewaree637712014-10-04 17:45:01 -0700257static void bond_slave_arr_handler(struct work_struct *work);
Jay Vosburgh21a75f02016-02-02 13:35:56 -0800258static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
259 int mod);
Mahesh Bandeward4859d72018-09-24 14:40:11 -0700260static void bond_netdev_notify_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262/*---------------------------- General routines -----------------------------*/
263
Amerigo Wangbd33acc2011-03-06 21:58:46 +0000264const char *bond_mode_name(int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800266 static const char *names[] = {
267 [BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
268 [BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
269 [BOND_MODE_XOR] = "load balancing (xor)",
270 [BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000271 [BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800272 [BOND_MODE_TLB] = "transmit load balancing",
273 [BOND_MODE_ALB] = "adaptive load balancing",
274 };
275
Wang Sheng-Huif5280942013-07-24 14:53:26 +0800276 if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return "unknown";
Holger Eitzenberger77afc922008-12-09 23:08:09 -0800278
279 return names[mode];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * bond_dev_queue_xmit - Prepare skb for xmit.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * @bond: bond device that got this skb for tx.
286 * @skb: hw accel VLAN tagged skb to transmit
287 * @slave_dev: slave that is supposed to xmit this skbuff
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Eric Dumazetae46f1842020-05-07 09:32:22 -0700289netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000290 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Ben Hutchings83874512010-12-13 08:19:28 +0000292 skb->dev = slave_dev;
Neil Horman374eeb52011-06-03 10:35:52 +0000293
Eric Dumazet5ee31c682012-06-12 06:03:51 +0000294 BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
Jiri Pirkodf4ab5b2012-07-20 02:28:49 +0000295 sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
Tonghao Zhangae35c6f2018-05-11 02:53:11 -0700296 skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
Neil Horman374eeb52011-06-03 10:35:52 +0000297
Amerigo Wange15c3c222012-08-10 01:24:45 +0000298 if (unlikely(netpoll_tx_running(bond->dev)))
Eric Dumazetae46f1842020-05-07 09:32:22 -0700299 return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
300
301 return dev_queue_xmit(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Jarod Wilson18cb2612020-06-19 10:31:55 -0400304/*---------------------------------- VLAN -----------------------------------*/
305
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200306/* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000307 * We don't protect the slave list iteration with a lock because:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * a. This operation is performed in IOCTL context,
309 * b. The operation is protected by the RTNL semaphore in the 8021q code,
310 * c. Holding a lock with BH disabled while directly calling a base driver
311 * entry point is generally a BAD idea.
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * The design of synchronization/protection for this operation in the 8021q
314 * module is good for one or more VLAN devices over a single physical device
315 * and cannot be extended for a teaming solution like bonding, so there is a
316 * potential race condition here where a net device from the vlan group might
317 * be referenced (either by a base driver or the 8021q code) while it is being
318 * removed from the system. However, it turns out we're not making matters
319 * worse, and if it works for regular VLAN usage it will work here too.
320*/
321
322/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * bond_vlan_rx_add_vid - Propagates adding an id to slaves
324 * @bond_dev: bonding net device that got called
Lee Jones45a15532020-08-14 12:39:05 +0100325 * @proto: network protocol ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * @vid: vlan id being added
327 */
Patrick McHardy80d5c362013-04-19 02:04:28 +0000328static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
329 __be16 proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Wang Chen454d7c92008-11-12 23:37:49 -0800331 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico81f23b12013-09-25 09:20:13 +0200332 struct slave *slave, *rollback_slave;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200333 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200334 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200336 bond_for_each_slave(bond, slave, iter) {
Patrick McHardy80d5c362013-04-19 02:04:28 +0000337 res = vlan_vid_add(slave->dev, proto, vid);
Jiri Pirko87002b02011-12-08 04:11:17 +0000338 if (res)
339 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
Jiri Pirko8e586132011-12-08 19:52:37 -0500342 return 0;
Jiri Pirko87002b02011-12-08 04:11:17 +0000343
344unwind:
Veaceslav Falico81f23b12013-09-25 09:20:13 +0200345 /* unwind to the slave that failed */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200346 bond_for_each_slave(bond, rollback_slave, iter) {
Veaceslav Falico81f23b12013-09-25 09:20:13 +0200347 if (rollback_slave == slave)
348 break;
349
350 vlan_vid_del(rollback_slave->dev, proto, vid);
351 }
Jiri Pirko87002b02011-12-08 04:11:17 +0000352
353 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/**
357 * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
358 * @bond_dev: bonding net device that got called
Lee Jones45a15532020-08-14 12:39:05 +0100359 * @proto: network protocol ID
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * @vid: vlan id being removed
361 */
Patrick McHardy80d5c362013-04-19 02:04:28 +0000362static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
363 __be16 proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Wang Chen454d7c92008-11-12 23:37:49 -0800365 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200366 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200369 bond_for_each_slave(bond, slave, iter)
Patrick McHardy80d5c362013-04-19 02:04:28 +0000370 vlan_vid_del(slave->dev, proto, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Veaceslav Falicoe868b0c2013-08-28 23:25:15 +0200372 if (bond_is_lb(bond))
373 bond_alb_clear_vlan(bond, vid);
Jiri Pirko8e586132011-12-08 19:52:37 -0500374
375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Jarod Wilson18cb2612020-06-19 10:31:55 -0400378/*---------------------------------- XFRM -----------------------------------*/
379
380#ifdef CONFIG_XFRM_OFFLOAD
381/**
382 * bond_ipsec_add_sa - program device with a security association
383 * @xs: pointer to transformer state struct
384 **/
385static int bond_ipsec_add_sa(struct xfrm_state *xs)
386{
387 struct net_device *bond_dev = xs->xso.dev;
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400388 struct bonding *bond;
389 struct slave *slave;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400390
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400391 if (!bond_dev)
392 return -EINVAL;
393
394 bond = netdev_priv(bond_dev);
Jarod Wilsonf548a472020-07-08 18:58:49 -0400395 slave = rcu_dereference(bond->curr_active_slave);
Jarod Wilsonbdfd2d12020-06-23 16:40:01 -0400396 xs->xso.real_dev = slave->dev;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400397 bond->xs = xs;
398
399 if (!(slave->dev->xfrmdev_ops
400 && slave->dev->xfrmdev_ops->xdo_dev_state_add)) {
401 slave_warn(bond_dev, slave->dev, "Slave does not support ipsec offload\n");
402 return -EINVAL;
403 }
404
405 return slave->dev->xfrmdev_ops->xdo_dev_state_add(xs);
406}
407
408/**
409 * bond_ipsec_del_sa - clear out this specific SA
410 * @xs: pointer to transformer state struct
411 **/
412static void bond_ipsec_del_sa(struct xfrm_state *xs)
413{
414 struct net_device *bond_dev = xs->xso.dev;
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400415 struct bonding *bond;
416 struct slave *slave;
417
418 if (!bond_dev)
419 return;
420
421 bond = netdev_priv(bond_dev);
Jarod Wilsonf548a472020-07-08 18:58:49 -0400422 slave = rcu_dereference(bond->curr_active_slave);
Jarod Wilson18cb2612020-06-19 10:31:55 -0400423
424 if (!slave)
425 return;
426
Jarod Wilsonbdfd2d12020-06-23 16:40:01 -0400427 xs->xso.real_dev = slave->dev;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400428
429 if (!(slave->dev->xfrmdev_ops
430 && slave->dev->xfrmdev_ops->xdo_dev_state_delete)) {
431 slave_warn(bond_dev, slave->dev, "%s: no slave xdo_dev_state_delete\n", __func__);
432 return;
433 }
434
435 slave->dev->xfrmdev_ops->xdo_dev_state_delete(xs);
436}
437
438/**
439 * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
440 * @skb: current data packet
441 * @xs: pointer to transformer state struct
442 **/
443static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
444{
445 struct net_device *bond_dev = xs->xso.dev;
446 struct bonding *bond = netdev_priv(bond_dev);
Jarod Wilsonf548a472020-07-08 18:58:49 -0400447 struct slave *curr_active = rcu_dereference(bond->curr_active_slave);
Jarod Wilson18cb2612020-06-19 10:31:55 -0400448 struct net_device *slave_dev = curr_active->dev;
449
Jarod Wilsona3b658c2020-06-30 14:49:41 -0400450 if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
451 return true;
452
Jarod Wilson18cb2612020-06-19 10:31:55 -0400453 if (!(slave_dev->xfrmdev_ops
454 && slave_dev->xfrmdev_ops->xdo_dev_offload_ok)) {
455 slave_warn(bond_dev, slave_dev, "%s: no slave xdo_dev_offload_ok\n", __func__);
456 return false;
457 }
458
Jarod Wilsonbdfd2d12020-06-23 16:40:01 -0400459 xs->xso.real_dev = slave_dev;
Jarod Wilson18cb2612020-06-19 10:31:55 -0400460 return slave_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
461}
462
463static const struct xfrmdev_ops bond_xfrmdev_ops = {
464 .xdo_dev_state_add = bond_ipsec_add_sa,
465 .xdo_dev_state_delete = bond_ipsec_del_sa,
466 .xdo_dev_offload_ok = bond_ipsec_offload_ok,
467};
468#endif /* CONFIG_XFRM_OFFLOAD */
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*------------------------------- Link status -------------------------------*/
471
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200472/* Set the carrier state for the master according to the state of its
Jay Vosburghff59c452006-03-27 13:27:43 -0800473 * slaves. If any slaves are up, the master is up. In 802.3ad mode,
474 * do special 802.3ad magic.
475 *
476 * Returns zero if carrier state does not change, nonzero if it does.
477 */
Jonathan Toppins2477bc92015-01-26 01:16:57 -0500478int bond_set_carrier(struct bonding *bond)
Jay Vosburghff59c452006-03-27 13:27:43 -0800479{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200480 struct list_head *iter;
Jay Vosburghff59c452006-03-27 13:27:43 -0800481 struct slave *slave;
Jay Vosburghff59c452006-03-27 13:27:43 -0800482
Veaceslav Falico0965a1f2013-09-25 09:20:21 +0200483 if (!bond_has_slaves(bond))
Jay Vosburghff59c452006-03-27 13:27:43 -0800484 goto down;
485
Veaceslav Falico01844092014-05-15 21:39:55 +0200486 if (BOND_MODE(bond) == BOND_MODE_8023AD)
Jay Vosburghff59c452006-03-27 13:27:43 -0800487 return bond_3ad_set_carrier(bond);
488
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200489 bond_for_each_slave(bond, slave, iter) {
Jay Vosburghff59c452006-03-27 13:27:43 -0800490 if (slave->link == BOND_LINK_UP) {
491 if (!netif_carrier_ok(bond->dev)) {
492 netif_carrier_on(bond->dev);
493 return 1;
494 }
495 return 0;
496 }
497 }
498
499down:
500 if (netif_carrier_ok(bond->dev)) {
501 netif_carrier_off(bond->dev);
502 return 1;
503 }
504 return 0;
505}
506
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200507/* Get link speed and duplex from the slave's base driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * using ethtool. If for some reason the call fails or the
Weiping Pan98f41f62011-10-31 17:20:48 +0000509 * values are invalid, set speed and duplex to -1,
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700510 * and return. Return 1 if speed or duplex settings are
511 * UNKNOWN; 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700513static int bond_update_speed_duplex(struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct net_device *slave_dev = slave->dev;
David Decotigny98569092016-02-24 10:58:02 -0800516 struct ethtool_link_ksettings ecmd;
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700517 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Dan Carpenter589665f2011-11-04 08:21:38 +0000519 slave->speed = SPEED_UNKNOWN;
520 slave->duplex = DUPLEX_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
David Decotigny98569092016-02-24 10:58:02 -0800522 res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700523 if (res < 0)
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700524 return 1;
David Decotigny98569092016-02-24 10:58:02 -0800525 if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700526 return 1;
David Decotigny98569092016-02-24 10:58:02 -0800527 switch (ecmd.base.duplex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 case DUPLEX_FULL:
529 case DUPLEX_HALF:
530 break;
531 default:
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700532 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
David Decotigny98569092016-02-24 10:58:02 -0800535 slave->speed = ecmd.base.speed;
536 slave->duplex = ecmd.base.duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Mahesh Bandewarc4adfc82017-03-27 11:37:35 -0700538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -0800541const char *bond_slave_link_status(s8 link)
542{
543 switch (link) {
544 case BOND_LINK_UP:
545 return "up";
546 case BOND_LINK_FAIL:
547 return "going down";
548 case BOND_LINK_DOWN:
549 return "down";
550 case BOND_LINK_BACK:
551 return "going back";
552 default:
553 return "unknown";
554 }
555}
556
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200557/* if <dev> supports MII link status reporting, check its link status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 *
559 * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000560 * depending upon the setting of the use_carrier parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 *
562 * Return either BMSR_LSTATUS, meaning that the link is up (or we
563 * can't tell and just pretend it is), or 0, meaning that the link is
564 * down.
565 *
566 * If reporting is non-zero, instead of faking link up, return -1 if
567 * both ETHTOOL and MII ioctls fail (meaning the device does not
568 * support them). If use_carrier is set, return whatever it says.
569 * It'd be nice if there was a good way to tell if a driver supports
570 * netif_carrier, but there really isn't.
571 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000572static int bond_check_dev_link(struct bonding *bond,
573 struct net_device *slave_dev, int reporting)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800575 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Jiri Bohacd9d52832009-10-28 22:23:54 -0700576 int (*ioctl)(struct net_device *, struct ifreq *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct ifreq ifr;
578 struct mii_ioctl_data *mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Petri Gynther6c988852009-08-28 12:05:15 +0000580 if (!reporting && !netif_running(slave_dev))
581 return 0;
582
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800583 if (bond->params.use_carrier)
Debabrata Banerjeeb3c898e2018-05-16 14:02:13 -0400584 return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Jiri Pirko29112f42009-04-24 01:58:23 +0000586 /* Try to get link status using Ethtool first. */
Ben Hutchingsc772dde2012-12-07 06:15:32 +0000587 if (slave_dev->ethtool_ops->get_link)
588 return slave_dev->ethtool_ops->get_link(slave_dev) ?
589 BMSR_LSTATUS : 0;
Jiri Pirko29112f42009-04-24 01:58:23 +0000590
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000591 /* Ethtool can't be used, fallback to MII ioctls. */
Stephen Hemmingereb7cc592008-11-19 21:56:05 -0800592 ioctl = slave_ops->ndo_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (ioctl) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200594 /* TODO: set pointer to correct ioctl on a per team member
595 * bases to make this more efficient. that is, once
596 * we determine the correct ioctl, we will always
597 * call it and not the others for that team
598 * member.
599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200601 /* We cannot assume that SIOCGMIIPHY will also read a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * register; not all network drivers (e.g., e100)
603 * support that.
604 */
605
606 /* Yes, the mii is overlaid on the ifreq.ifr_ifru */
607 strncpy(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
608 mii = if_mii(&ifr);
Al Viro4ad41c12016-09-03 19:37:25 -0400609 if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 mii->reg_num = MII_BMSR;
Al Viro4ad41c12016-09-03 19:37:25 -0400611 if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0)
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000612 return mii->val_out & BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 }
615
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200616 /* If reporting, report that either there's no dev->do_ioctl,
Matthew Wilcox61a44b92007-07-31 14:00:02 -0700617 * or both SIOCGMIIREG and get_link failed (meaning that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * cannot report link status). If not reporting, pretend
619 * we're ok.
620 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000621 return reporting ? -1 : BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
624/*----------------------------- Multicast list ------------------------------*/
625
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200626/* Push the promiscuity flag down to appropriate slaves */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700627static int bond_set_promiscuity(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200629 struct list_head *iter;
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700630 int err = 0;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200631
Veaceslav Falicoec0865a2014-05-15 21:39:54 +0200632 if (bond_uses_primary(bond)) {
Veaceslav Falico14056e72014-07-16 18:32:01 +0200633 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
Eric Dumazet4740d632014-07-15 06:56:55 -0700634
Eric Dumazet4740d632014-07-15 06:56:55 -0700635 if (curr_active)
636 err = dev_set_promiscuity(curr_active->dev, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 } else {
638 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200639
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200640 bond_for_each_slave(bond, slave, iter) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700641 err = dev_set_promiscuity(slave->dev, inc);
642 if (err)
643 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200649/* Push the allmulti flag down to all slaves */
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700650static int bond_set_allmulti(struct bonding *bond, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200652 struct list_head *iter;
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700653 int err = 0;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200654
Veaceslav Falicoec0865a2014-05-15 21:39:54 +0200655 if (bond_uses_primary(bond)) {
Veaceslav Falico14056e72014-07-16 18:32:01 +0200656 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
Eric Dumazet4740d632014-07-15 06:56:55 -0700657
Eric Dumazet4740d632014-07-15 06:56:55 -0700658 if (curr_active)
659 err = dev_set_allmulti(curr_active->dev, inc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } else {
661 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200662
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200663 bond_for_each_slave(bond, slave, iter) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700664 err = dev_set_allmulti(slave->dev, inc);
665 if (err)
666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 }
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700669 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200672/* Retrieve the list of registered multicast addresses for the bonding
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800673 * device and retransmit an IGMP JOIN request to the current active
674 * slave.
675 */
stephen hemminger379b7382010-10-15 11:02:56 +0000676static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
Flavio Leitner5a37e8c2010-10-05 14:23:57 +0000677{
678 struct bonding *bond = container_of(work, struct bonding,
Flavio Leitner94265cf2011-05-25 08:38:58 +0000679 mcast_work.work);
Veaceslav Falicoad999ee2013-03-26 04:10:02 +0000680
dingtianhongf2369102013-12-13 10:20:26 +0800681 if (!rtnl_trylock()) {
682 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
683 return;
684 }
685 call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
686
687 if (bond->igmp_retrans > 1) {
688 bond->igmp_retrans--;
689 queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
690 }
691 rtnl_unlock();
Jay Vosburgha816c7c2007-02-28 17:03:37 -0800692}
693
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200694/* Flush bond's hardware addresses from slave */
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000695static void bond_hw_addr_flush(struct net_device *bond_dev,
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000696 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Wang Chen454d7c92008-11-12 23:37:49 -0800698 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000700 dev_uc_unsync(slave_dev, bond_dev);
701 dev_mc_unsync(slave_dev, bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Veaceslav Falico01844092014-05-15 21:39:55 +0200703 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /* del lacpdu mc addr from mc list */
705 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
706
Jiri Pirko22bedad32010-04-01 21:22:57 +0000707 dev_mc_del(slave_dev, lacpdu_multicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709}
710
711/*--------------------------- Active slave change ---------------------------*/
712
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000713/* Update the hardware address list and promisc/allmulti for the new and
Veaceslav Falicoec0865a2014-05-15 21:39:54 +0200714 * old active slaves (if any). Modes that are not using primary keep all
715 * slaves up date at all times; only the modes that use primary need to call
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000716 * this function to swap these settings during a failover.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000718static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
719 struct slave *old_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (old_active) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000722 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 dev_set_promiscuity(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000725 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 dev_set_allmulti(old_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000728 bond_hw_addr_flush(bond->dev, old_active->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
731 if (new_active) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700732 /* FIXME: Signal errors upstream. */
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000733 if (bond->dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dev_set_promiscuity(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000736 if (bond->dev->flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 dev_set_allmulti(new_active->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
nikolay@redhat.comd632ce92013-04-18 07:33:38 +0000739 netif_addr_lock_bh(bond->dev);
Jay Vosburgh303d1cb2013-05-31 11:57:30 +0000740 dev_uc_sync(new_active->dev, bond->dev);
741 dev_mc_sync(new_active->dev, bond->dev);
nikolay@redhat.comd632ce92013-04-18 07:33:38 +0000742 netif_addr_unlock_bh(bond->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744}
745
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200746/**
747 * bond_set_dev_addr - clone slave's address to bond
748 * @bond_dev: bond net device
749 * @slave_dev: slave net device
750 *
751 * Should be called with RTNL held.
752 */
Petr Machatab9245912018-12-13 11:54:44 +0000753static int bond_set_dev_addr(struct net_device *bond_dev,
754 struct net_device *slave_dev)
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200755{
Petr Machata1caf40d2018-12-13 11:54:46 +0000756 int err;
757
Jarod Wilsone2a74202019-06-07 10:59:29 -0400758 slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
759 bond_dev, slave_dev, slave_dev->addr_len);
Petr Machata1caf40d2018-12-13 11:54:46 +0000760 err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
761 if (err)
762 return err;
763
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200764 memcpy(bond_dev->dev_addr, slave_dev->dev_addr, slave_dev->addr_len);
765 bond_dev->addr_assign_type = NET_ADDR_STOLEN;
766 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
Petr Machatab9245912018-12-13 11:54:44 +0000767 return 0;
nikolay@redhat.comae0d6752013-06-26 17:13:39 +0200768}
769
dingtianhonga951bc12015-07-16 16:30:02 +0800770static struct slave *bond_get_old_active(struct bonding *bond,
771 struct slave *new_active)
772{
773 struct slave *slave;
774 struct list_head *iter;
775
776 bond_for_each_slave(bond, slave, iter) {
777 if (slave == new_active)
778 continue;
779
780 if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
781 return slave;
782 }
783
784 return NULL;
785}
786
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200787/* bond_do_fail_over_mac
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700788 *
789 * Perform special MAC address swapping for fail_over_mac settings
790 *
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200791 * Called with RTNL
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700792 */
793static void bond_do_fail_over_mac(struct bonding *bond,
794 struct slave *new_active,
795 struct slave *old_active)
796{
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400797 u8 tmp_mac[MAX_ADDR_LEN];
798 struct sockaddr_storage ss;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700799 int rv;
800
801 switch (bond->params.fail_over_mac) {
802 case BOND_FOM_ACTIVE:
Petr Machatab9245912018-12-13 11:54:44 +0000803 if (new_active) {
804 rv = bond_set_dev_addr(bond->dev, new_active->dev);
805 if (rv)
Jarod Wilsone2a74202019-06-07 10:59:29 -0400806 slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
807 -rv);
Petr Machatab9245912018-12-13 11:54:44 +0000808 }
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700809 break;
810 case BOND_FOM_FOLLOW:
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200811 /* if new_active && old_active, swap them
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700812 * if just old_active, do nothing (going to no active slave)
813 * if just new_active, set new_active to bond's MAC
814 */
815 if (!new_active)
816 return;
817
dingtianhonga951bc12015-07-16 16:30:02 +0800818 if (!old_active)
819 old_active = bond_get_old_active(bond, new_active);
820
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700821 if (old_active) {
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400822 bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
823 new_active->dev->addr_len);
824 bond_hw_addr_copy(ss.__data,
825 old_active->dev->dev_addr,
826 old_active->dev->addr_len);
827 ss.ss_family = new_active->dev->type;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700828 } else {
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400829 bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
830 bond->dev->addr_len);
831 ss.ss_family = bond->dev->type;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700832 }
833
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400834 rv = dev_set_mac_address(new_active->dev,
Petr Machata3a37a962018-12-13 11:54:30 +0000835 (struct sockaddr *)&ss, NULL);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700836 if (rv) {
Jarod Wilsone2a74202019-06-07 10:59:29 -0400837 slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
838 -rv);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700839 goto out;
840 }
841
842 if (!old_active)
843 goto out;
844
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400845 bond_hw_addr_copy(ss.__data, tmp_mac,
846 new_active->dev->addr_len);
847 ss.ss_family = old_active->dev->type;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700848
Jarod Wilsonfaeeb312017-04-04 17:32:42 -0400849 rv = dev_set_mac_address(old_active->dev,
Petr Machata3a37a962018-12-13 11:54:30 +0000850 (struct sockaddr *)&ss, NULL);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700851 if (rv)
Jarod Wilsone2a74202019-06-07 10:59:29 -0400852 slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
853 -rv);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700854out:
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700855 break;
856 default:
Veaceslav Falico76444f52014-07-15 19:35:58 +0200857 netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
858 bond->params.fail_over_mac);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700859 break;
860 }
861
862}
863
Mazhar Ranab5a983f2015-07-07 15:04:50 +0530864static struct slave *bond_choose_primary_or_current(struct bonding *bond)
Jiri Pirkoa5499522009-09-25 03:28:09 +0000865{
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +0200866 struct slave *prim = rtnl_dereference(bond->primary_slave);
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200867 struct slave *curr = rtnl_dereference(bond->curr_active_slave);
Jiri Pirkoa5499522009-09-25 03:28:09 +0000868
Mazhar Ranab5a983f2015-07-07 15:04:50 +0530869 if (!prim || prim->link != BOND_LINK_UP) {
870 if (!curr || curr->link != BOND_LINK_UP)
871 return NULL;
872 return curr;
873 }
874
Jiri Pirkoa5499522009-09-25 03:28:09 +0000875 if (bond->force_primary) {
876 bond->force_primary = false;
Mazhar Ranab5a983f2015-07-07 15:04:50 +0530877 return prim;
Jiri Pirkoa5499522009-09-25 03:28:09 +0000878 }
Mazhar Ranab5a983f2015-07-07 15:04:50 +0530879
880 if (!curr || curr->link != BOND_LINK_UP)
881 return prim;
882
883 /* At this point, prim and curr are both up */
884 switch (bond->params.primary_reselect) {
885 case BOND_PRI_RESELECT_ALWAYS:
886 return prim;
887 case BOND_PRI_RESELECT_BETTER:
888 if (prim->speed < curr->speed)
889 return curr;
890 if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
891 return curr;
892 return prim;
893 case BOND_PRI_RESELECT_FAILURE:
894 return curr;
895 default:
896 netdev_err(bond->dev, "impossible primary_reselect %d\n",
897 bond->params.primary_reselect);
898 return curr;
899 }
Jiri Pirkoa5499522009-09-25 03:28:09 +0000900}
Jay Vosburgh3915c1e82008-05-17 21:10:14 -0700901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/**
Mazhar Ranab5a983f2015-07-07 15:04:50 +0530903 * bond_find_best_slave - select the best available slave to be the active one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * @bond: our bonding struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 */
906static struct slave *bond_find_best_slave(struct bonding *bond)
907{
Mazhar Ranab5a983f2015-07-07 15:04:50 +0530908 struct slave *slave, *bestslave = NULL;
Veaceslav Falico77140d22013-09-25 09:20:18 +0200909 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 int mintime = bond->params.updelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Mazhar Ranab5a983f2015-07-07 15:04:50 +0530912 slave = bond_choose_primary_or_current(bond);
913 if (slave)
914 return slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Veaceslav Falico77140d22013-09-25 09:20:18 +0200916 bond_for_each_slave(bond, slave, iter) {
917 if (slave->link == BOND_LINK_UP)
918 return slave;
Veaceslav Falicob6adc612014-05-15 21:39:57 +0200919 if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
Veaceslav Falico77140d22013-09-25 09:20:18 +0200920 slave->delay < mintime) {
921 mintime = slave->delay;
922 bestslave = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 }
925
926 return bestslave;
927}
928
Ben Hutchingsad246c92011-04-26 15:25:52 +0000929static bool bond_should_notify_peers(struct bonding *bond)
930{
dingtianhong4cb4f972013-12-13 10:19:39 +0800931 struct slave *slave;
932
933 rcu_read_lock();
934 slave = rcu_dereference(bond->curr_active_slave);
935 rcu_read_unlock();
Ben Hutchingsad246c92011-04-26 15:25:52 +0000936
Veaceslav Falico76444f52014-07-15 19:35:58 +0200937 netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
938 slave ? slave->dev->name : "NULL");
Ben Hutchingsad246c92011-04-26 15:25:52 +0000939
940 if (!slave || !bond->send_peer_notif ||
Vincent Bernat07a4dde2019-07-02 19:43:54 +0200941 bond->send_peer_notif %
942 max(1, bond->params.peer_notif_delay) != 0 ||
Venkat Venkatsubrab02e3e92015-08-11 07:57:23 -0700943 !netif_carrier_ok(bond->dev) ||
Ben Hutchingsad246c92011-04-26 15:25:52 +0000944 test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
945 return false;
946
Ben Hutchingsad246c92011-04-26 15:25:52 +0000947 return true;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/**
951 * change_active_interface - change the active slave into the specified one
952 * @bond: our bonding struct
Lee Jones45a15532020-08-14 12:39:05 +0100953 * @new_active: the new slave to make the active one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 *
955 * Set the new slave to the bond's settings and unset them on the old
956 * curr_active_slave.
957 * Setting include flags, mc-list, promiscuity, allmulti, etc.
958 *
959 * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
960 * because it is apparently the best available slave we have, even though its
961 * updelay hasn't timed out yet.
962 *
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200963 * Caller must hold RTNL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
Mitch Williamsa77b5322005-11-09 10:35:51 -0800965void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Eric Dumazet4740d632014-07-15 06:56:55 -0700967 struct slave *old_active;
968
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200969 ASSERT_RTNL();
970
971 old_active = rtnl_dereference(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Stephen Hemminger3d632c32009-06-12 19:02:48 +0000973 if (old_active == new_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Jarod Wilson18cb2612020-06-19 10:31:55 -0400976#ifdef CONFIG_XFRM_OFFLOAD
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400977 if (old_active && bond->xs)
978 bond_ipsec_del_sa(bond->xs);
Jarod Wilson18cb2612020-06-19 10:31:55 -0400979#endif /* CONFIG_XFRM_OFFLOAD */
980
Jarod Wilson5cd24cb2020-07-08 13:46:31 -0400981 if (new_active) {
Veaceslav Falico8e603462014-02-18 07:48:46 +0100982 new_active->last_link_up = jiffies;
Jay Vosburghb2220ca2008-05-17 21:10:13 -0700983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (new_active->link == BOND_LINK_BACK) {
Veaceslav Falicoec0865a2014-05-15 21:39:54 +0200985 if (bond_uses_primary(bond)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -0400986 slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
987 (bond->params.updelay - new_active->delay) * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
989
990 new_active->delay = 0;
Jiri Pirko5d397062015-12-03 12:12:19 +0100991 bond_set_slave_link_state(new_active, BOND_LINK_UP,
992 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Veaceslav Falico01844092014-05-15 21:39:55 +0200994 if (BOND_MODE(bond) == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Holger Eitzenberger58402052008-12-09 23:07:13 -0800997 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 } else {
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001000 if (bond_uses_primary(bond)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001001 slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 }
1004 }
1005
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001006 if (bond_uses_primary(bond))
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00001007 bond_hw_addr_swap(bond, new_active, old_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Holger Eitzenberger58402052008-12-09 23:07:13 -08001009 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 bond_alb_handle_active_change(bond, new_active);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001011 if (old_active)
dingtianhong5e5b0662014-02-26 11:05:22 +08001012 bond_set_slave_inactive_flags(old_active,
1013 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburgh8f903c72006-02-21 16:36:44 -08001014 if (new_active)
dingtianhong5e5b0662014-02-26 11:05:22 +08001015 bond_set_slave_active_flags(new_active,
1016 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 } else {
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001018 rcu_assign_pointer(bond->curr_active_slave, new_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001020
Veaceslav Falico01844092014-05-15 21:39:55 +02001021 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001022 if (old_active)
dingtianhong5e5b0662014-02-26 11:05:22 +08001023 bond_set_slave_inactive_flags(old_active,
1024 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001025
1026 if (new_active) {
Ben Hutchingsad246c92011-04-26 15:25:52 +00001027 bool should_notify_peers = false;
1028
dingtianhong5e5b0662014-02-26 11:05:22 +08001029 bond_set_slave_active_flags(new_active,
1030 BOND_SLAVE_NOTIFY_NOW);
Moni Shoua2ab82852007-10-09 19:43:39 -07001031
Or Gerlitz709f8a42008-06-13 18:12:01 -07001032 if (bond->params.fail_over_mac)
1033 bond_do_fail_over_mac(bond, new_active,
1034 old_active);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001035
Ben Hutchingsad246c92011-04-26 15:25:52 +00001036 if (netif_running(bond->dev)) {
1037 bond->send_peer_notif =
Vincent Bernat07a4dde2019-07-02 19:43:54 +02001038 bond->params.num_peer_notif *
1039 max(1, bond->params.peer_notif_delay);
Ben Hutchingsad246c92011-04-26 15:25:52 +00001040 should_notify_peers =
1041 bond_should_notify_peers(bond);
1042 }
1043
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001044 call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
Vincent Bernat07a4dde2019-07-02 19:43:54 +02001045 if (should_notify_peers) {
1046 bond->send_peer_notif--;
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001047 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
1048 bond->dev);
Vincent Bernat07a4dde2019-07-02 19:43:54 +02001049 }
Moni Shoua7893b242008-05-17 21:10:12 -07001050 }
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04001051 }
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001052
Jarod Wilson5cd24cb2020-07-08 13:46:31 -04001053#ifdef CONFIG_XFRM_OFFLOAD
1054 if (new_active && bond->xs) {
1055 xfrm_dev_state_flush(dev_net(bond->dev), bond->dev, true);
1056 bond_ipsec_add_sa(bond->xs);
1057 }
1058#endif /* CONFIG_XFRM_OFFLOAD */
1059
Flavio Leitner5a37e8c2010-10-05 14:23:57 +00001060 /* resend IGMP joins since active slave has changed or
Flavio Leitner94265cf2011-05-25 08:38:58 +00001061 * all were sent on curr_active_slave.
1062 * resend only if bond is brought up with the affected
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001063 * bonding modes and the retransmission is enabled
1064 */
Flavio Leitner94265cf2011-05-25 08:38:58 +00001065 if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001066 ((bond_uses_primary(bond) && new_active) ||
Veaceslav Falico01844092014-05-15 21:39:55 +02001067 BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
Flavio Leitnerc2952c32010-10-05 14:23:59 +00001068 bond->igmp_retrans = bond->params.resend_igmp;
Nikolay Aleksandrov4beac022013-08-01 11:51:42 +02001069 queue_delayed_work(bond->wq, &bond->mcast_work, 1);
Andy Gospodareka2fd9402010-03-25 14:49:05 +00001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073/**
1074 * bond_select_active_slave - select a new active slave, if needed
1075 * @bond: our bonding struct
1076 *
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001077 * This functions should be called when one of the following occurs:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * - The old curr_active_slave has been released or lost its link.
1079 * - The primary_slave has got its link back.
1080 * - A slave has got its link back and there's no old curr_active_slave.
1081 *
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02001082 * Caller must hold RTNL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Mitch Williamsa77b5322005-11-09 10:35:51 -08001084void bond_select_active_slave(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 struct slave *best_slave;
Jay Vosburghff59c452006-03-27 13:27:43 -08001087 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Nikolay Aleksandrove0974582014-09-15 17:19:35 +02001089 ASSERT_RTNL();
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 best_slave = bond_find_best_slave(bond);
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02001092 if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 bond_change_active_slave(bond, best_slave);
Jay Vosburghff59c452006-03-27 13:27:43 -08001094 rv = bond_set_carrier(bond);
1095 if (!rv)
1096 return;
1097
Zhang Shengjud66bd902016-02-03 02:02:32 +00001098 if (netif_carrier_ok(bond->dev))
Eric Dumazetb8bd72d2019-07-01 10:48:51 -07001099 netdev_info(bond->dev, "active interface up!\n");
Zhang Shengjud66bd902016-02-03 02:02:32 +00001100 else
Veaceslav Falico76444f52014-07-15 19:35:58 +02001101 netdev_info(bond->dev, "now running without any active interface!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103}
1104
WANG Congf6dc31a2010-05-06 00:48:51 -07001105#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001106static inline int slave_enable_netpoll(struct slave *slave)
WANG Congf6dc31a2010-05-06 00:48:51 -07001107{
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001108 struct netpoll *np;
1109 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001110
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001111 np = kzalloc(sizeof(*np), GFP_KERNEL);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001112 err = -ENOMEM;
1113 if (!np)
1114 goto out;
1115
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001116 err = __netpoll_setup(np, slave->dev);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001117 if (err) {
1118 kfree(np);
1119 goto out;
WANG Congf6dc31a2010-05-06 00:48:51 -07001120 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001121 slave->np = np;
1122out:
1123 return err;
1124}
1125static inline void slave_disable_netpoll(struct slave *slave)
1126{
1127 struct netpoll *np = slave->np;
1128
1129 if (!np)
1130 return;
1131
1132 slave->np = NULL;
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -04001133
1134 __netpoll_free(np);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001135}
WANG Congf6dc31a2010-05-06 00:48:51 -07001136
1137static void bond_poll_controller(struct net_device *bond_dev)
1138{
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001139 struct bonding *bond = netdev_priv(bond_dev);
1140 struct slave *slave = NULL;
1141 struct list_head *iter;
1142 struct ad_info ad_info;
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001143
1144 if (BOND_MODE(bond) == BOND_MODE_8023AD)
1145 if (bond_3ad_get_active_agg_info(bond, &ad_info))
1146 return;
1147
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001148 bond_for_each_slave_rcu(bond, slave, iter) {
Eric Dumazet93f62ad2018-09-21 15:27:39 -07001149 if (!bond_slave_is_up(slave))
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001150 continue;
1151
1152 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1153 struct aggregator *agg =
1154 SLAVE_AD_INFO(slave)->port.aggregator;
1155
1156 if (agg &&
1157 agg->aggregator_identifier != ad_info.aggregator_id)
1158 continue;
1159 }
1160
Eric Dumazet93f62ad2018-09-21 15:27:39 -07001161 netpoll_poll_dev(slave->dev);
Mahesh Bandewar616f4542015-03-04 21:57:52 -08001162 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001163}
1164
dingtianhongc4cdef92013-07-23 15:25:27 +08001165static void bond_netpoll_cleanup(struct net_device *bond_dev)
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001166{
dingtianhongc4cdef92013-07-23 15:25:27 +08001167 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001168 struct list_head *iter;
Neil Hormanc2355e12010-10-13 16:01:49 +00001169 struct slave *slave;
Neil Hormanc2355e12010-10-13 16:01:49 +00001170
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001171 bond_for_each_slave(bond, slave, iter)
Veaceslav Falicob6adc612014-05-15 21:39:57 +02001172 if (bond_slave_is_up(slave))
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001173 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07001174}
WANG Congf6dc31a2010-05-06 00:48:51 -07001175
Eric W. Biedermana8779ec2014-03-27 15:36:38 -07001176static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001177{
1178 struct bonding *bond = netdev_priv(dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001179 struct list_head *iter;
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001180 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001181 int err = 0;
WANG Congf6dc31a2010-05-06 00:48:51 -07001182
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001183 bond_for_each_slave(bond, slave, iter) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001184 err = slave_enable_netpoll(slave);
1185 if (err) {
dingtianhongc4cdef92013-07-23 15:25:27 +08001186 bond_netpoll_cleanup(dev);
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001187 break;
1188 }
1189 }
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001190 return err;
1191}
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001192#else
1193static inline int slave_enable_netpoll(struct slave *slave)
1194{
1195 return 0;
1196}
1197static inline void slave_disable_netpoll(struct slave *slave)
1198{
1199}
WANG Congf6dc31a2010-05-06 00:48:51 -07001200static void bond_netpoll_cleanup(struct net_device *bond_dev)
1201{
1202}
WANG Congf6dc31a2010-05-06 00:48:51 -07001203#endif
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/*---------------------------------- IOCTL ----------------------------------*/
1206
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001207static netdev_features_t bond_fix_features(struct net_device *dev,
nikolay@redhat.com9b7b1652013-09-02 13:51:41 +02001208 netdev_features_t features)
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001209{
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001210 struct bonding *bond = netdev_priv(dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001211 struct list_head *iter;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001212 netdev_features_t mask;
nikolay@redhat.com9b7b1652013-09-02 13:51:41 +02001213 struct slave *slave;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001214
Scott Feldman7889cbe2015-05-10 09:48:07 -07001215 mask = features;
Roopa Prabhuc158cba2015-01-29 22:40:16 -08001216
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001217 features &= ~NETIF_F_ONE_FOR_ALL;
1218 features |= NETIF_F_ALL_FOR_ALL;
1219
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001220 bond_for_each_slave(bond, slave, iter) {
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001221 features = netdev_increment_features(features,
1222 slave->dev->features,
1223 mask);
1224 }
Eric Dumazetb0ce3502013-05-16 07:34:53 +00001225 features = netdev_add_tso_features(features, mask);
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001226
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001227 return features;
1228}
1229
Tom Herberta1882222015-12-14 11:19:43 -08001230#define BOND_VLAN_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
Michał Mirosław62f2a3a2011-07-13 14:10:29 +00001231 NETIF_F_FRAGLIST | NETIF_F_ALL_TSO | \
1232 NETIF_F_HIGHDMA | NETIF_F_LRO)
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001233
Tom Herberta1882222015-12-14 11:19:43 -08001234#define BOND_ENC_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1235 NETIF_F_RXCSUM | NETIF_F_ALL_TSO)
Or Gerlitz5a7baa72014-06-17 16:11:09 +03001236
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001237#define BOND_MPLS_FEATURES (NETIF_F_HW_CSUM | NETIF_F_SG | \
1238 NETIF_F_ALL_TSO)
1239
Jarod Wilson18cb2612020-06-19 10:31:55 -04001240
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001241static void bond_compute_features(struct bonding *bond)
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001242{
Eric Dumazet02875872014-10-05 18:38:35 -07001243 unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
1244 IFF_XMIT_DST_RELEASE_PERM;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001245 netdev_features_t vlan_features = BOND_VLAN_FEATURES;
Or Gerlitz5a7baa72014-06-17 16:11:09 +03001246 netdev_features_t enc_features = BOND_ENC_FEATURES;
Jarod Wilson18cb2612020-06-19 10:31:55 -04001247#ifdef CONFIG_XFRM_OFFLOAD
1248 netdev_features_t xfrm_features = BOND_XFRM_FEATURES;
1249#endif /* CONFIG_XFRM_OFFLOAD */
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001250 netdev_features_t mpls_features = BOND_MPLS_FEATURES;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001251 struct net_device *bond_dev = bond->dev;
1252 struct list_head *iter;
1253 struct slave *slave;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001254 unsigned short max_hard_header_len = ETH_HLEN;
Sarveshwar Bandi0e376bd2012-11-21 04:35:03 +00001255 unsigned int gso_max_size = GSO_MAX_SIZE;
1256 u16 gso_max_segs = GSO_MAX_SEGS;
Herbert Xub63365a2008-10-23 01:11:29 -07001257
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001258 if (!bond_has_slaves(bond))
Herbert Xub63365a2008-10-23 01:11:29 -07001259 goto done;
Michal Kubečeka9b3ace2014-05-20 08:29:35 +02001260 vlan_features &= NETIF_F_ALL_FOR_ALL;
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001261 mpls_features &= NETIF_F_ALL_FOR_ALL;
Herbert Xub63365a2008-10-23 01:11:29 -07001262
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001263 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh278339a2009-08-28 12:05:12 +00001264 vlan_features = netdev_increment_features(vlan_features,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001265 slave->dev->vlan_features, BOND_VLAN_FEATURES);
1266
Or Gerlitz5a7baa72014-06-17 16:11:09 +03001267 enc_features = netdev_increment_features(enc_features,
1268 slave->dev->hw_enc_features,
1269 BOND_ENC_FEATURES);
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001270
Jarod Wilson18cb2612020-06-19 10:31:55 -04001271#ifdef CONFIG_XFRM_OFFLOAD
1272 xfrm_features = netdev_increment_features(xfrm_features,
1273 slave->dev->hw_enc_features,
1274 BOND_XFRM_FEATURES);
1275#endif /* CONFIG_XFRM_OFFLOAD */
1276
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001277 mpls_features = netdev_increment_features(mpls_features,
1278 slave->dev->mpls_features,
1279 BOND_MPLS_FEATURES);
1280
Eric Dumazetb6fe83e2012-07-17 12:19:48 +00001281 dst_release_flag &= slave->dev->priv_flags;
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001282 if (slave->dev->hard_header_len > max_hard_header_len)
1283 max_hard_header_len = slave->dev->hard_header_len;
Sarveshwar Bandi0e376bd2012-11-21 04:35:03 +00001284
1285 gso_max_size = min(gso_max_size, slave->dev->gso_max_size);
1286 gso_max_segs = min(gso_max_segs, slave->dev->gso_max_segs);
Jay Vosburgh54ef3132006-09-22 21:53:39 -07001287 }
Paolo Abeni19cdead2017-04-27 19:29:34 +02001288 bond_dev->hard_header_len = max_hard_header_len;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001289
Herbert Xub63365a2008-10-23 01:11:29 -07001290done:
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001291 bond_dev->vlan_features = vlan_features;
Willem de Bruijn8eea1ca2018-05-22 11:34:40 -04001292 bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
YueHaibingd595b032019-08-07 10:19:59 +08001293 NETIF_F_HW_VLAN_CTAG_TX |
1294 NETIF_F_HW_VLAN_STAG_TX |
Willem de Bruijn8eea1ca2018-05-22 11:34:40 -04001295 NETIF_F_GSO_UDP_L4;
Jarod Wilson18cb2612020-06-19 10:31:55 -04001296#ifdef CONFIG_XFRM_OFFLOAD
1297 bond_dev->hw_enc_features |= xfrm_features;
1298#endif /* CONFIG_XFRM_OFFLOAD */
Ariel Levkovich2e770b52019-06-03 22:36:46 +00001299 bond_dev->mpls_features = mpls_features;
Sarveshwar Bandi0e376bd2012-11-21 04:35:03 +00001300 bond_dev->gso_max_segs = gso_max_segs;
1301 netif_set_gso_max_size(bond_dev, gso_max_size);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001302
Eric Dumazet02875872014-10-05 18:38:35 -07001303 bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1304 if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
1305 dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
1306 bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE;
Eric Dumazetb6fe83e2012-07-17 12:19:48 +00001307
Michał Mirosławb2a103e2011-05-07 03:22:17 +00001308 netdev_change_features(bond_dev);
Arthur Kepner8531c5f2005-08-23 01:34:53 -04001309}
1310
Moni Shoua872254d2007-10-09 19:43:38 -07001311static void bond_setup_by_slave(struct net_device *bond_dev,
1312 struct net_device *slave_dev)
1313{
Stephen Hemminger00829822008-11-20 20:14:53 -08001314 bond_dev->header_ops = slave_dev->header_ops;
Moni Shoua872254d2007-10-09 19:43:38 -07001315
1316 bond_dev->type = slave_dev->type;
1317 bond_dev->hard_header_len = slave_dev->hard_header_len;
1318 bond_dev->addr_len = slave_dev->addr_len;
1319
1320 memcpy(bond_dev->broadcast, slave_dev->broadcast,
1321 slave_dev->addr_len);
1322}
1323
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001324/* On bonding slaves other than the currently active slave, suppress
Jiri Pirko3aba8912011-04-19 03:48:16 +00001325 * duplicates except for alb non-mcast/bcast.
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001326 */
1327static bool bond_should_deliver_exact_match(struct sk_buff *skb,
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001328 struct slave *slave,
1329 struct bonding *bond)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001330{
Jiri Pirko2d7011c2011-03-16 08:46:43 +00001331 if (bond_is_slave_inactive(slave)) {
Veaceslav Falico01844092014-05-15 21:39:55 +02001332 if (BOND_MODE(bond) == BOND_MODE_ALB &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001333 skb->pkt_type != PACKET_BROADCAST &&
1334 skb->pkt_type != PACKET_MULTICAST)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001335 return false;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001336 return true;
1337 }
1338 return false;
1339}
1340
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001341static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001342{
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001343 struct sk_buff *skb = *pskb;
Jiri Pirkof1c17752011-03-12 03:14:35 +00001344 struct slave *slave;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001345 struct bonding *bond;
Eric Dumazetde063b72012-06-11 19:23:07 +00001346 int (*recv_probe)(const struct sk_buff *, struct bonding *,
1347 struct slave *);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00001348 int ret = RX_HANDLER_ANOTHER;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001349
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001350 skb = skb_share_check(skb, GFP_ATOMIC);
1351 if (unlikely(!skb))
1352 return RX_HANDLER_CONSUMED;
1353
1354 *pskb = skb;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001355
Jiri Pirko35d48902011-03-22 02:38:12 +00001356 slave = bond_slave_get_rcu(skb->dev);
1357 bond = slave->bond;
Jiri Pirko0bd80da2011-03-16 08:45:23 +00001358
Mark Rutland6aa7de02017-10-23 14:07:29 -07001359 recv_probe = READ_ONCE(bond->recv_probe);
Mitsuo Hayasaka4d974802011-10-12 16:04:29 +00001360 if (recv_probe) {
Eric Dumazetde063b72012-06-11 19:23:07 +00001361 ret = recv_probe(skb, bond, slave);
1362 if (ret == RX_HANDLER_CONSUMED) {
1363 consume_skb(skb);
1364 return ret;
Jiri Pirko3aba8912011-04-19 03:48:16 +00001365 }
1366 }
1367
Michal Soltys3c963a32019-02-18 17:55:28 +01001368 /*
1369 * For packets determined by bond_should_deliver_exact_match() call to
1370 * be suppressed we want to make an exception for link-local packets.
1371 * This is necessary for e.g. LLDP daemons to be able to monitor
1372 * inactive slave links without being forced to bind to them
1373 * explicitly.
1374 *
1375 * At the same time, packets that are passed to the bonding master
1376 * (including link-local ones) can have their originating interface
1377 * determined via PACKET_ORIGDEV socket option.
Mahesh Bandewar6a9e4612018-09-24 14:39:42 -07001378 */
Michal Soltys3c963a32019-02-18 17:55:28 +01001379 if (bond_should_deliver_exact_match(skb, slave, bond)) {
1380 if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
1381 return RX_HANDLER_PASS;
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001382 return RX_HANDLER_EXACT;
Michal Soltys3c963a32019-02-18 17:55:28 +01001383 }
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001384
Jiri Pirko35d48902011-03-22 02:38:12 +00001385 skb->dev = bond->dev;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001386
Veaceslav Falico01844092014-05-15 21:39:55 +02001387 if (BOND_MODE(bond) == BOND_MODE_ALB &&
Julian Wiedmann2e92a2d2020-02-20 09:00:07 +01001388 netif_is_bridge_port(bond->dev) &&
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001389 skb->pkt_type == PACKET_HOST) {
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001390
Changli Gao541ac7c2011-03-02 21:07:14 +00001391 if (unlikely(skb_cow_head(skb,
1392 skb->data - skb_mac_header(skb)))) {
1393 kfree_skb(skb);
Jiri Pirko8a4eb572011-03-12 03:14:39 +00001394 return RX_HANDLER_CONSUMED;
Changli Gao541ac7c2011-03-02 21:07:14 +00001395 }
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001396 bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
1397 bond->dev->addr_len);
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001398 }
1399
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00001400 return ret;
Jiri Pirko5b2c4dd2011-02-23 09:05:42 +00001401}
1402
Jiri Pirko41f0b042015-12-03 12:12:14 +01001403static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001404{
Jiri Pirko41f0b042015-12-03 12:12:14 +01001405 switch (BOND_MODE(bond)) {
1406 case BOND_MODE_ROUNDROBIN:
1407 return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
1408 case BOND_MODE_ACTIVEBACKUP:
1409 return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
1410 case BOND_MODE_BROADCAST:
1411 return NETDEV_LAG_TX_TYPE_BROADCAST;
1412 case BOND_MODE_XOR:
1413 case BOND_MODE_8023AD:
1414 return NETDEV_LAG_TX_TYPE_HASH;
1415 default:
1416 return NETDEV_LAG_TX_TYPE_UNKNOWN;
1417 }
1418}
1419
John Hurleyf44aa9e2018-05-23 19:22:52 -07001420static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
1421 enum netdev_lag_tx_type type)
1422{
1423 if (type != NETDEV_LAG_TX_TYPE_HASH)
1424 return NETDEV_LAG_HASH_NONE;
1425
1426 switch (bond->params.xmit_policy) {
1427 case BOND_XMIT_POLICY_LAYER2:
1428 return NETDEV_LAG_HASH_L2;
1429 case BOND_XMIT_POLICY_LAYER34:
1430 return NETDEV_LAG_HASH_L34;
1431 case BOND_XMIT_POLICY_LAYER23:
1432 return NETDEV_LAG_HASH_L23;
1433 case BOND_XMIT_POLICY_ENCAP23:
1434 return NETDEV_LAG_HASH_E23;
1435 case BOND_XMIT_POLICY_ENCAP34:
1436 return NETDEV_LAG_HASH_E34;
1437 default:
1438 return NETDEV_LAG_HASH_UNKNOWN;
1439 }
1440}
1441
David Ahern42ab19e2017-10-04 17:48:47 -07001442static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
1443 struct netlink_ext_ack *extack)
Jiri Pirko41f0b042015-12-03 12:12:14 +01001444{
1445 struct netdev_lag_upper_info lag_upper_info;
John Hurleyf44aa9e2018-05-23 19:22:52 -07001446 enum netdev_lag_tx_type type;
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001447
John Hurleyf44aa9e2018-05-23 19:22:52 -07001448 type = bond_lag_tx_type(bond);
1449 lag_upper_info.tx_type = type;
1450 lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
Xin Long4597efe2017-10-24 13:54:18 +08001451
1452 return netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
1453 &lag_upper_info, extack);
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001454}
1455
Jiri Pirko41f0b042015-12-03 12:12:14 +01001456static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001457{
Jiri Pirko41f0b042015-12-03 12:12:14 +01001458 netdev_upper_dev_unlink(slave->dev, bond->dev);
1459 slave->dev->flags &= ~IFF_SLAVE;
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001460}
1461
dingtianhong3fdddd82014-05-12 15:08:43 +08001462static struct slave *bond_alloc_slave(struct bonding *bond)
1463{
1464 struct slave *slave = NULL;
1465
Zhang Shengjud66bd902016-02-03 02:02:32 +00001466 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
dingtianhong3fdddd82014-05-12 15:08:43 +08001467 if (!slave)
1468 return NULL;
1469
Veaceslav Falico01844092014-05-15 21:39:55 +02001470 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001471 SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info),
1472 GFP_KERNEL);
1473 if (!SLAVE_AD_INFO(slave)) {
1474 kfree(slave);
1475 return NULL;
1476 }
1477 }
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001478 INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
1479
dingtianhong3fdddd82014-05-12 15:08:43 +08001480 return slave;
1481}
1482
1483static void bond_free_slave(struct slave *slave)
1484{
1485 struct bonding *bond = bond_get_bond_by_slave(slave);
1486
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001487 cancel_delayed_work_sync(&slave->notify_work);
Veaceslav Falico01844092014-05-15 21:39:55 +02001488 if (BOND_MODE(bond) == BOND_MODE_8023AD)
dingtianhong3fdddd82014-05-12 15:08:43 +08001489 kfree(SLAVE_AD_INFO(slave));
1490
1491 kfree(slave);
1492}
1493
Moni Shoua69a23382015-02-03 16:48:30 +02001494static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
1495{
1496 info->bond_mode = BOND_MODE(bond);
1497 info->miimon = bond->params.miimon;
1498 info->num_slaves = bond->slave_cnt;
1499}
1500
1501static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
1502{
1503 strcpy(info->slave_name, slave->dev->name);
1504 info->link = slave->link;
1505 info->state = bond_slave_state(slave);
1506 info->link_failure_count = slave->link_failure_count;
1507}
1508
Moni Shoua69e61132015-02-03 16:48:31 +02001509static void bond_netdev_notify_work(struct work_struct *_work)
1510{
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001511 struct slave *slave = container_of(_work, struct slave,
1512 notify_work.work);
Moni Shoua69e61132015-02-03 16:48:31 +02001513
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001514 if (rtnl_trylock()) {
1515 struct netdev_bonding_info binfo;
1516
1517 bond_fill_ifslave(slave, &binfo.slave);
1518 bond_fill_ifbond(slave->bond, &binfo.master);
1519 netdev_bonding_info_change(slave->dev, &binfo);
1520 rtnl_unlock();
1521 } else {
1522 queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
1523 }
Moni Shoua69e61132015-02-03 16:48:31 +02001524}
1525
1526void bond_queue_slave_event(struct slave *slave)
1527{
Mahesh Bandeward4859d72018-09-24 14:40:11 -07001528 queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
Moni Shoua69e61132015-02-03 16:48:31 +02001529}
1530
Jiri Pirkof7c7eb72015-12-03 12:12:20 +01001531void bond_lower_state_changed(struct slave *slave)
1532{
1533 struct netdev_lag_lower_state_info info;
1534
1535 info.link_up = slave->link == BOND_LINK_UP ||
1536 slave->link == BOND_LINK_FAIL;
1537 info.tx_enabled = bond_is_active_slave(slave);
1538 netdev_lower_state_changed(slave->dev, &info);
1539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/* enslave device <slave> to bond device <master> */
David Ahern33eaf2a2017-10-04 17:48:46 -07001542int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
1543 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Wang Chen454d7c92008-11-12 23:37:49 -08001545 struct bonding *bond = netdev_priv(bond_dev);
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001546 const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
Veaceslav Falicoc8c23902013-09-25 09:20:25 +02001547 struct slave *new_slave = NULL, *prev_slave;
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001548 struct sockaddr_storage ss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int link_reporting;
Veaceslav Falico8599b522013-06-24 11:49:34 +02001550 int res = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Ben Hutchingsc772dde2012-12-07 06:15:32 +00001552 if (!bond->params.use_carrier &&
1553 slave_dev->ethtool_ops->get_link == NULL &&
1554 slave_ops->ndo_do_ioctl == NULL) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001555 slave_warn(bond_dev, slave_dev, "no link monitoring support\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557
Mahesh Bandewar24b27fc2016-09-01 22:18:34 -07001558 /* already in-use? */
1559 if (netdev_is_rx_handler_busy(slave_dev)) {
David Ahern759088b2017-10-04 17:48:49 -07001560 NL_SET_ERR_MSG(extack, "Device is in use and cannot be enslaved");
Jarod Wilsone2a74202019-06-07 10:59:29 -04001561 slave_err(bond_dev, slave_dev,
1562 "Error: Device is in use and cannot be enslaved\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 return -EBUSY;
1564 }
1565
Jiri Bohac09a89c22014-02-26 18:20:13 +01001566 if (bond_dev == slave_dev) {
David Ahern759088b2017-10-04 17:48:49 -07001567 NL_SET_ERR_MSG(extack, "Cannot enslave bond to itself.");
Veaceslav Falico76444f52014-07-15 19:35:58 +02001568 netdev_err(bond_dev, "cannot enslave bond to itself.\n");
Jiri Bohac09a89c22014-02-26 18:20:13 +01001569 return -EPERM;
1570 }
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* vlan challenged mutual exclusion */
1573 /* no need to lock since we're protected by rtnl_lock */
1574 if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001575 slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
Jiri Pirko55462cf2012-10-14 04:30:56 +00001576 if (vlan_uses_dev(bond_dev)) {
David Ahern759088b2017-10-04 17:48:49 -07001577 NL_SET_ERR_MSG(extack, "Can not enslave VLAN challenged device to VLAN enabled bond");
Jarod Wilsone2a74202019-06-07 10:59:29 -04001578 slave_err(bond_dev, slave_dev, "Error: cannot enslave VLAN challenged slave on VLAN enabled bond\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return -EPERM;
1580 } else {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001581 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 -07001582 }
1583 } else {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001584 slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Jarod Wilson18cb2612020-06-19 10:31:55 -04001587 if (slave_dev->features & NETIF_F_HW_ESP)
1588 slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
1589
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001590 /* Old ifenslave binaries are no longer supported. These can
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001591 * be identified with moderate accuracy by the state of the slave:
Jay Vosburgh217df672005-09-26 16:11:50 -07001592 * the current ifenslave will set the interface down prior to
1593 * enslaving it; the old ifenslave will not.
1594 */
yzhu1ce3ea1c2015-12-03 18:00:55 +08001595 if (slave_dev->flags & IFF_UP) {
David Ahern759088b2017-10-04 17:48:49 -07001596 NL_SET_ERR_MSG(extack, "Device can not be enslaved while up");
Jarod Wilsone2a74202019-06-07 10:59:29 -04001597 slave_err(bond_dev, slave_dev, "slave is up - this may be due to an out of date ifenslave\n");
Zhang Shengju1e2a8862016-02-09 10:37:46 +00001598 return -EPERM;
Jay Vosburgh217df672005-09-26 16:11:50 -07001599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Moni Shoua872254d2007-10-09 19:43:38 -07001601 /* set bonding device ether type by slave - bonding netdevices are
1602 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1603 * there is a need to override some of the type dependent attribs/funcs.
1604 *
1605 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1606 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1607 */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001608 if (!bond_has_slaves(bond)) {
Moni Shouae36b9d12009-07-15 04:56:31 +00001609 if (bond_dev->type != slave_dev->type) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001610 slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
1611 bond_dev->type, slave_dev->type);
Moni Shoua75c78502009-09-15 02:37:40 -07001612
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001613 res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
1614 bond_dev);
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001615 res = notifier_to_errno(res);
1616 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001617 slave_err(bond_dev, slave_dev, "refused to change device type\n");
Zhang Shengju1e2a8862016-02-09 10:37:46 +00001618 return -EBUSY;
Jiri Pirko3ca5b402010-03-10 10:29:35 +00001619 }
Moni Shoua75c78502009-09-15 02:37:40 -07001620
Jiri Pirko32a806c2010-03-19 04:00:23 +00001621 /* Flush unicast and multicast addresses */
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001622 dev_uc_flush(bond_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001623 dev_mc_flush(bond_dev);
Jiri Pirko32a806c2010-03-19 04:00:23 +00001624
Moni Shouae36b9d12009-07-15 04:56:31 +00001625 if (slave_dev->type != ARPHRD_ETHER)
1626 bond_setup_by_slave(bond_dev, slave_dev);
Neil Horman550fd082011-07-26 06:05:38 +00001627 else {
Moni Shouae36b9d12009-07-15 04:56:31 +00001628 ether_setup(bond_dev);
Neil Horman550fd082011-07-26 06:05:38 +00001629 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
1630 }
Moni Shoua75c78502009-09-15 02:37:40 -07001631
Amerigo Wangb7bc2a52012-08-09 22:14:57 +00001632 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
1633 bond_dev);
Moni Shouae36b9d12009-07-15 04:56:31 +00001634 }
Moni Shoua872254d2007-10-09 19:43:38 -07001635 } else if (bond_dev->type != slave_dev->type) {
David Ahern759088b2017-10-04 17:48:49 -07001636 NL_SET_ERR_MSG(extack, "Device type is different from other slaves");
Jarod Wilsone2a74202019-06-07 10:59:29 -04001637 slave_err(bond_dev, slave_dev, "ether type (%d) is different from other slaves (%d), can not enslave it\n",
1638 slave_dev->type, bond_dev->type);
Zhang Shengju1e2a8862016-02-09 10:37:46 +00001639 return -EINVAL;
Moni Shoua872254d2007-10-09 19:43:38 -07001640 }
1641
Mark Bloch1533e772016-07-21 11:52:55 +03001642 if (slave_dev->type == ARPHRD_INFINIBAND &&
1643 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
David Ahern759088b2017-10-04 17:48:49 -07001644 NL_SET_ERR_MSG(extack, "Only active-backup mode is supported for infiniband slaves");
Jarod Wilsone2a74202019-06-07 10:59:29 -04001645 slave_warn(bond_dev, slave_dev, "Type (%d) supports only active-backup mode\n",
1646 slave_dev->type);
Mark Bloch1533e772016-07-21 11:52:55 +03001647 res = -EOPNOTSUPP;
1648 goto err_undo_flags;
1649 }
1650
1651 if (!slave_ops->ndo_set_mac_address ||
1652 slave_dev->type == ARPHRD_INFINIBAND) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001653 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 +02001654 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
1655 bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1656 if (!bond_has_slaves(bond)) {
dingtianhong00503b62014-01-25 13:00:29 +08001657 bond->params.fail_over_mac = BOND_FOM_ACTIVE;
Jarod Wilsone2a74202019-06-07 10:59:29 -04001658 slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
Veaceslav Falicof5442442014-07-15 13:26:01 +02001659 } else {
David Ahern759088b2017-10-04 17:48:49 -07001660 NL_SET_ERR_MSG(extack, "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
Jarod Wilsone2a74202019-06-07 10:59:29 -04001661 slave_err(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address, but fail_over_mac is not set to active\n");
Veaceslav Falicof5442442014-07-15 13:26:01 +02001662 res = -EOPNOTSUPP;
1663 goto err_undo_flags;
dingtianhong00503b62014-01-25 13:00:29 +08001664 }
Moni Shoua2ab82852007-10-09 19:43:39 -07001665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
Amerigo Wang8d8fc292011-05-19 21:39:10 +00001668 call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
1669
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001670 /* If this is the first slave, then we need to set the master's hardware
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001671 * address to be the same as the slave's.
1672 */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001673 if (!bond_has_slaves(bond) &&
Petr Machatab9245912018-12-13 11:54:44 +00001674 bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
1675 res = bond_set_dev_addr(bond->dev, slave_dev);
1676 if (res)
1677 goto err_undo_flags;
1678 }
Jiri Pirkoc20811a2010-05-19 01:14:29 +00001679
dingtianhong3fdddd82014-05-12 15:08:43 +08001680 new_slave = bond_alloc_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (!new_slave) {
1682 res = -ENOMEM;
1683 goto err_undo_flags;
1684 }
dingtianhong3fdddd82014-05-12 15:08:43 +08001685
Veaceslav Falicodc73c412014-05-21 17:42:00 +02001686 new_slave->bond = bond;
1687 new_slave->dev = slave_dev;
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001688 /* Set the new_slave's queue_id to be zero. Queue ID mapping
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00001689 * is set via sysfs or module option if desired.
1690 */
1691 new_slave->queue_id = 0;
1692
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001693 /* Save slave's original mtu and then set it to match the bond */
1694 new_slave->original_mtu = slave_dev->mtu;
1695 res = dev_set_mtu(slave_dev, bond->dev->mtu);
1696 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001697 slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001698 goto err_free;
1699 }
1700
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001701 /* Save slave's original ("permanent") mac address for modes
Jay Vosburgh217df672005-09-26 16:11:50 -07001702 * that need it, and for restoring it upon release, and then
1703 * set it to the master's address
1704 */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001705 bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
1706 slave_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
dingtianhong00503b62014-01-25 13:00:29 +08001708 if (!bond->params.fail_over_mac ||
Veaceslav Falico01844092014-05-15 21:39:55 +02001709 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001710 /* Set slave to master's mac address. The application already
Moni Shoua2ab82852007-10-09 19:43:39 -07001711 * set the master's mac address to that of the first slave
1712 */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001713 memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
1714 ss.ss_family = slave_dev->type;
Petr Machata3a37a962018-12-13 11:54:30 +00001715 res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss,
1716 extack);
Moni Shoua2ab82852007-10-09 19:43:39 -07001717 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001718 slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00001719 goto err_restore_mtu;
Moni Shoua2ab82852007-10-09 19:43:39 -07001720 }
Jay Vosburgh217df672005-09-26 16:11:50 -07001721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Karl Heiss03d84a52016-01-11 08:28:43 -05001723 /* set slave flag before open to prevent IPv6 addrconf */
1724 slave_dev->flags |= IFF_SLAVE;
1725
Jay Vosburgh217df672005-09-26 16:11:50 -07001726 /* open the slave since the application closed it */
Petr Machata00f54e62018-12-06 17:05:36 +00001727 res = dev_open(slave_dev, extack);
Jay Vosburgh217df672005-09-26 16:11:50 -07001728 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001729 slave_err(bond_dev, slave_dev, "Opening slave failed\n");
Veaceslav Falico1f718f02013-09-25 09:20:10 +02001730 goto err_restore_mac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
Jay Vosburgh0b680e72006-09-22 21:54:10 -07001733 slave_dev->priv_flags |= IFF_BONDING;
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04001734 /* initialize slave stats */
1735 dev_get_stats(new_slave->dev, &new_slave->slave_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Holger Eitzenberger58402052008-12-09 23:07:13 -08001737 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /* bond_alb_init_slave() must be called before all other stages since
1739 * it might fail and we do not want to have to undo everything
1740 */
1741 res = bond_alb_init_slave(bond, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001742 if (res)
Jay Vosburgh569f0c42008-05-02 18:06:02 -07001743 goto err_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745
Wei Yongjunb8e2fde2013-08-23 10:45:07 +08001746 res = vlan_vids_add_by_dev(slave_dev, bond_dev);
1747 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001748 slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
Xin Longae42cc62018-03-26 01:16:46 +08001749 goto err_close;
nikolay@redhat.com1ff412a2013-08-06 12:40:15 +02001750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Veaceslav Falicoc8c23902013-09-25 09:20:25 +02001752 prev_slave = bond_last_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 new_slave->delay = 0;
1755 new_slave->link_failure_count = 0;
1756
Andreas Bornad729bc92017-08-10 06:41:44 +02001757 if (bond_update_speed_duplex(new_slave) &&
1758 bond_needs_speed_duplex(bond))
Mahesh Bandewar3f3c2782017-04-03 18:38:39 -07001759 new_slave->link = BOND_LINK_DOWN;
Veaceslav Falico876254a2013-03-12 06:31:32 +00001760
Veaceslav Falico49f17de2014-02-18 07:48:47 +01001761 new_slave->last_rx = jiffies -
Michal Kubečekf31c7932012-04-17 02:02:06 +00001762 (msecs_to_jiffies(bond->params.arp_interval) + 1);
Veaceslav Falico8599b522013-06-24 11:49:34 +02001763 for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
Veaceslav Falico49f17de2014-02-18 07:48:47 +01001764 new_slave->target_last_arp_rx[i] = new_slave->last_rx;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (bond->params.miimon && !bond->params.use_carrier) {
1767 link_reporting = bond_check_dev_link(bond, slave_dev, 1);
1768
1769 if ((link_reporting == -1) && !bond->params.arp_interval) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001770 /* miimon is set but a bonded network driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * does not support ETHTOOL/MII and
1772 * arp_interval is not set. Note: if
1773 * use_carrier is enabled, we will never go
1774 * here (because netif_carrier is always
1775 * supported); thus, we don't need to change
1776 * the messages for netif_carrier.
1777 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04001778 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 -07001779 } else if (link_reporting == -1) {
1780 /* unable get link status using mii/ethtool */
Jarod Wilsone2a74202019-06-07 10:59:29 -04001781 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 -07001782 }
1783 }
1784
1785 /* check for initial state */
Aviv Hellera30b0162016-07-05 12:09:47 +03001786 new_slave->link = BOND_LINK_NOCHANGE;
Michal Kubečekf31c7932012-04-17 02:02:06 +00001787 if (bond->params.miimon) {
1788 if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) {
1789 if (bond->params.updelay) {
Moni Shoua69a23382015-02-03 16:48:30 +02001790 bond_set_slave_link_state(new_slave,
Jiri Pirko5d397062015-12-03 12:12:19 +01001791 BOND_LINK_BACK,
1792 BOND_SLAVE_NOTIFY_NOW);
Michal Kubečekf31c7932012-04-17 02:02:06 +00001793 new_slave->delay = bond->params.updelay;
1794 } else {
Moni Shoua69a23382015-02-03 16:48:30 +02001795 bond_set_slave_link_state(new_slave,
Jiri Pirko5d397062015-12-03 12:12:19 +01001796 BOND_LINK_UP,
1797 BOND_SLAVE_NOTIFY_NOW);
Michal Kubečekf31c7932012-04-17 02:02:06 +00001798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 } else {
Jiri Pirko5d397062015-12-03 12:12:19 +01001800 bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
1801 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Michal Kubečekf31c7932012-04-17 02:02:06 +00001803 } else if (bond->params.arp_interval) {
Moni Shoua69a23382015-02-03 16:48:30 +02001804 bond_set_slave_link_state(new_slave,
1805 (netif_carrier_ok(slave_dev) ?
Jiri Pirko5d397062015-12-03 12:12:19 +01001806 BOND_LINK_UP : BOND_LINK_DOWN),
1807 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 } else {
Jiri Pirko5d397062015-12-03 12:12:19 +01001809 bond_set_slave_link_state(new_slave, BOND_LINK_UP,
1810 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Michal Kubečekf31c7932012-04-17 02:02:06 +00001813 if (new_slave->link != BOND_LINK_DOWN)
Veaceslav Falico8e603462014-02-18 07:48:46 +01001814 new_slave->last_link_up = jiffies;
Jarod Wilsone2a74202019-06-07 10:59:29 -04001815 slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
1816 new_slave->link == BOND_LINK_DOWN ? "DOWN" :
1817 (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
Michal Kubečekf31c7932012-04-17 02:02:06 +00001818
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001819 if (bond_uses_primary(bond) && bond->params.primary[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /* if there is a primary slave, remember it */
Jiri Pirkoa5499522009-09-25 03:28:09 +00001821 if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001822 rcu_assign_pointer(bond->primary_slave, new_slave);
Jiri Pirkoa5499522009-09-25 03:28:09 +00001823 bond->force_primary = true;
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
Veaceslav Falico01844092014-05-15 21:39:55 +02001827 switch (BOND_MODE(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 case BOND_MODE_ACTIVEBACKUP:
dingtianhong5e5b0662014-02-26 11:05:22 +08001829 bond_set_slave_inactive_flags(new_slave,
1830 BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 break;
1832 case BOND_MODE_8023AD:
1833 /* in 802.3ad mode, the internal mechanism
1834 * will activate the slaves in the selected
1835 * aggregator
1836 */
dingtianhong5e5b0662014-02-26 11:05:22 +08001837 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /* if this is the first slave */
Veaceslav Falico23c147e2013-09-27 15:10:57 +02001839 if (!prev_slave) {
dingtianhong3fdddd82014-05-12 15:08:43 +08001840 SLAVE_AD_INFO(new_slave)->id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /* Initialize AD with the number of times that the AD timer is called in 1 second
1842 * can be called only after the mac address of the bond is set
1843 */
Peter Pan(潘卫平)56d00c672011-06-08 21:19:02 +00001844 bond_3ad_initialize(bond, 1000/AD_TIMER_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 } else {
dingtianhong3fdddd82014-05-12 15:08:43 +08001846 SLAVE_AD_INFO(new_slave)->id =
1847 SLAVE_AD_INFO(prev_slave)->id + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849
1850 bond_3ad_bind_slave(new_slave);
1851 break;
1852 case BOND_MODE_TLB:
1853 case BOND_MODE_ALB:
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001854 bond_set_active_slave(new_slave);
dingtianhong5e5b0662014-02-26 11:05:22 +08001855 bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
1857 default:
Jarod Wilsone2a74202019-06-07 10:59:29 -04001858 slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 /* always active in trunk mode */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00001861 bond_set_active_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* In trunking mode there is little meaning to curr_active_slave
1864 * anyway (it holds no special properties of the bond device),
1865 * so we can change it without calling change_active_interface()
1866 */
Eric Dumazet4740d632014-07-15 06:56:55 -07001867 if (!rcu_access_pointer(bond->curr_active_slave) &&
1868 new_slave->link == BOND_LINK_UP)
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001869 rcu_assign_pointer(bond->curr_active_slave, new_slave);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 break;
1872 } /* switch(bond_mode) */
1873
WANG Congf6dc31a2010-05-06 00:48:51 -07001874#ifdef CONFIG_NET_POLL_CONTROLLER
Xin Longddea7882018-04-22 19:11:50 +08001875 if (bond->dev->npinfo) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001876 if (slave_enable_netpoll(new_slave)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001877 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 +00001878 res = -EBUSY;
stephen hemmingerf7d98212011-12-31 13:26:46 +00001879 goto err_detach;
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001880 }
WANG Congf6dc31a2010-05-06 00:48:51 -07001881 }
1882#endif
Amerigo Wang8a8efa22011-02-17 23:43:32 +00001883
Michal Kubečekfbe168b2014-11-13 07:54:50 +01001884 if (!(bond_dev->features & NETIF_F_LRO))
1885 dev_disable_lro(slave_dev);
1886
Jiri Pirko35d48902011-03-22 02:38:12 +00001887 res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
1888 new_slave);
1889 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001890 slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
Veaceslav Falico5831d662013-09-25 09:20:32 +02001891 goto err_detach;
Jiri Pirko35d48902011-03-22 02:38:12 +00001892 }
1893
David Ahern42ab19e2017-10-04 17:48:47 -07001894 res = bond_master_upper_dev_link(bond, new_slave, extack);
Veaceslav Falico1f718f02013-09-25 09:20:10 +02001895 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001896 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
Veaceslav Falico1f718f02013-09-25 09:20:10 +02001897 goto err_unregister;
1898 }
1899
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08001900 res = bond_sysfs_slave_add(new_slave);
1901 if (res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04001902 slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08001903 goto err_upper_unlink;
1904 }
1905
Xin Longae42cc62018-03-26 01:16:46 +08001906 /* If the mode uses primary, then the following is handled by
1907 * bond_change_active_slave().
1908 */
1909 if (!bond_uses_primary(bond)) {
1910 /* set promiscuity level to new slave */
1911 if (bond_dev->flags & IFF_PROMISC) {
1912 res = dev_set_promiscuity(slave_dev, 1);
1913 if (res)
1914 goto err_sysfs_del;
1915 }
1916
1917 /* set allmulti level to new slave */
1918 if (bond_dev->flags & IFF_ALLMULTI) {
1919 res = dev_set_allmulti(slave_dev, 1);
Xin Long9f5a90c2018-03-26 01:16:47 +08001920 if (res) {
1921 if (bond_dev->flags & IFF_PROMISC)
1922 dev_set_promiscuity(slave_dev, -1);
Xin Longae42cc62018-03-26 01:16:46 +08001923 goto err_sysfs_del;
Xin Long9f5a90c2018-03-26 01:16:47 +08001924 }
Xin Longae42cc62018-03-26 01:16:46 +08001925 }
1926
1927 netif_addr_lock_bh(bond_dev);
1928 dev_mc_sync_multiple(slave_dev, bond_dev);
1929 dev_uc_sync_multiple(slave_dev, bond_dev);
1930 netif_addr_unlock_bh(bond_dev);
1931
1932 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1933 /* add lacpdu mc addr to mc list */
1934 u8 lacpdu_multicast[ETH_ALEN] = MULTICAST_LACPDU_ADDR;
1935
1936 dev_mc_add(slave_dev, lacpdu_multicast);
1937 }
1938 }
1939
Veaceslav Falico5378c2e2013-10-21 11:48:30 +02001940 bond->slave_cnt++;
1941 bond_compute_features(bond);
1942 bond_set_carrier(bond);
1943
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02001944 if (bond_uses_primary(bond)) {
dingtianhongf80889a2014-02-12 12:06:40 +08001945 block_netpoll_tx();
Veaceslav Falico5378c2e2013-10-21 11:48:30 +02001946 bond_select_active_slave(bond);
dingtianhongf80889a2014-02-12 12:06:40 +08001947 unblock_netpoll_tx();
Veaceslav Falico5378c2e2013-10-21 11:48:30 +02001948 }
Veaceslav Falico1f718f02013-09-25 09:20:10 +02001949
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04001950 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07001951 bond_update_slave_arr(bond, NULL);
1952
Debabrata Banerjee21706ee2018-05-09 19:32:11 -04001953
Jarod Wilsone2a74202019-06-07 10:59:29 -04001954 slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
1955 bond_is_active_slave(new_slave) ? "an active" : "a backup",
1956 new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* enslave is successful */
Moni Shoua69e61132015-02-03 16:48:31 +02001959 bond_queue_slave_event(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return 0;
1961
1962/* Undo stages on error */
Xin Longae42cc62018-03-26 01:16:46 +08001963err_sysfs_del:
1964 bond_sysfs_slave_del(new_slave);
1965
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08001966err_upper_unlink:
Jiri Pirko41f0b042015-12-03 12:12:14 +01001967 bond_upper_dev_unlink(bond, new_slave);
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08001968
Veaceslav Falico1f718f02013-09-25 09:20:10 +02001969err_unregister:
1970 netdev_rx_handler_unregister(slave_dev);
1971
stephen hemmingerf7d98212011-12-31 13:26:46 +00001972err_detach:
nikolay@redhat.com1ff412a2013-08-06 12:40:15 +02001973 vlan_vids_del_by_dev(slave_dev, bond_dev);
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001974 if (rcu_access_pointer(bond->primary_slave) == new_slave)
1975 RCU_INIT_POINTER(bond->primary_slave, NULL);
Eric Dumazet4740d632014-07-15 06:56:55 -07001976 if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
dingtianhongf80889a2014-02-12 12:06:40 +08001977 block_netpoll_tx();
dingtianhongc8517032013-12-13 10:20:07 +08001978 bond_change_active_slave(bond, NULL);
nikolay@redhat.com3c5913b532013-04-18 07:33:36 +00001979 bond_select_active_slave(bond);
dingtianhongf80889a2014-02-12 12:06:40 +08001980 unblock_netpoll_tx();
nikolay@redhat.com3c5913b532013-04-18 07:33:36 +00001981 }
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02001982 /* either primary_slave or curr_active_slave might've changed */
1983 synchronize_rcu();
nikolay@redhat.comfc7a72a2013-04-18 07:33:37 +00001984 slave_disable_netpoll(new_slave);
stephen hemmingerf7d98212011-12-31 13:26:46 +00001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986err_close:
Taehee Yoo65de65d2019-10-21 18:47:52 +00001987 if (!netif_is_bond_master(slave_dev))
1988 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 dev_close(slave_dev);
1990
1991err_restore_mac:
Karl Heiss03d84a52016-01-11 08:28:43 -05001992 slave_dev->flags &= ~IFF_SLAVE;
dingtianhong00503b62014-01-25 13:00:29 +08001993 if (!bond->params.fail_over_mac ||
Veaceslav Falico01844092014-05-15 21:39:55 +02001994 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07001995 /* XXX TODO - fom follow mode needs to change master's
1996 * MAC if this slave's MAC is in use by the bond, or at
1997 * least print a warning.
1998 */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04001999 bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
2000 new_slave->dev->addr_len);
2001 ss.ss_family = slave_dev->type;
Petr Machata3a37a962018-12-13 11:54:30 +00002002 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
Moni Shoua2ab82852007-10-09 19:43:39 -07002003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002005err_restore_mtu:
2006 dev_set_mtu(slave_dev, new_slave->original_mtu);
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008err_free:
dingtianhong3fdddd82014-05-12 15:08:43 +08002009 bond_free_slave(new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011err_undo_flags:
Nikolay Aleksandrovb8fad452013-06-12 00:07:01 +02002012 /* Enslave of first slave has failed and we need to fix master's mac */
Nikolay Aleksandrov7d5cd2c2015-07-15 22:57:01 +02002013 if (!bond_has_slaves(bond)) {
2014 if (ether_addr_equal_64bits(bond_dev->dev_addr,
2015 slave_dev->dev_addr))
2016 eth_hw_addr_random(bond_dev);
2017 if (bond_dev->type != ARPHRD_ETHER) {
Jay Vosburgh40baec22015-11-06 17:23:23 -08002018 dev_close(bond_dev);
Nikolay Aleksandrov7d5cd2c2015-07-15 22:57:01 +02002019 ether_setup(bond_dev);
2020 bond_dev->flags |= IFF_MASTER;
2021 bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2022 }
2023 }
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return res;
2026}
2027
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002028/* Try to release the slave device <slave> from the bond device <master>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 * It is legal to access curr_active_slave without a lock because all the function
Nikolay Aleksandrov8c0bc552014-09-11 22:49:28 +02002030 * is RTNL-locked. If "all" is true it means that the function is being called
nikolay@redhat.com08963412013-02-18 14:09:42 +00002031 * while destroying a bond interface and all slaves are being released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 *
2033 * The rules for slave state should be:
2034 * for Active/Backup:
2035 * Active stays on all backups go down
2036 * for Bonded connections:
2037 * The first up interface should be left on and all others downed.
2038 */
nikolay@redhat.com08963412013-02-18 14:09:42 +00002039static int __bond_release_one(struct net_device *bond_dev,
2040 struct net_device *slave_dev,
WANG Congf51048c2017-07-06 15:01:57 -07002041 bool all, bool unregister)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Wang Chen454d7c92008-11-12 23:37:49 -08002043 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 struct slave *slave, *oldcurrent;
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04002045 struct sockaddr_storage ss;
Neil Horman5a0068d2013-09-27 12:22:15 -04002046 int old_flags = bond_dev->flags;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002047 netdev_features_t old_features = bond_dev->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 /* slave is not a slave or master is not master of this slave */
2050 if (!(slave_dev->flags & IFF_SLAVE) ||
Jiri Pirko471cb5a2013-01-03 22:49:01 +00002051 !netdev_has_upper_dev(slave_dev, bond_dev)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002052 slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return -EINVAL;
2054 }
2055
Neil Hormane843fa52010-10-13 16:01:50 +00002056 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 slave = bond_get_slave_by_dev(bond, slave_dev);
2059 if (!slave) {
2060 /* not a slave of this bond */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002061 slave_info(bond_dev, slave_dev, "interface not enslaved\n");
Neil Hormane843fa52010-10-13 16:01:50 +00002062 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EINVAL;
2064 }
2065
Jiri Pirko57beaca2015-12-03 12:12:21 +01002066 bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
2067
sfeldma@cumulusnetworks.com07699f92014-01-16 22:57:49 -08002068 bond_sysfs_slave_del(slave);
2069
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04002070 /* recompute stats just before removing the slave */
2071 bond_get_stats(bond->dev, &bond->bond_stats);
2072
Jiri Pirko41f0b042015-12-03 12:12:14 +01002073 bond_upper_dev_unlink(bond, slave);
Jiri Pirko35d48902011-03-22 02:38:12 +00002074 /* unregister rx_handler early so bond_handle_frame wouldn't be called
2075 * for this slave anymore.
2076 */
2077 netdev_rx_handler_unregister(slave_dev);
Jiri Pirko35d48902011-03-22 02:38:12 +00002078
Nikolay Aleksandrove4702592014-09-11 22:49:27 +02002079 if (BOND_MODE(bond) == BOND_MODE_8023AD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 bond_3ad_unbind_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04002082 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07002083 bond_update_slave_arr(bond, slave);
2084
Jarod Wilsone2a74202019-06-07 10:59:29 -04002085 slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
2086 bond_is_active_slave(slave) ? "active" : "backup");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Eric Dumazet4740d632014-07-15 06:56:55 -07002088 oldcurrent = rcu_access_pointer(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Eric Dumazet85741712014-07-15 06:56:56 -07002090 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
dingtianhong00503b62014-01-25 13:00:29 +08002092 if (!all && (!bond->params.fail_over_mac ||
Veaceslav Falico01844092014-05-15 21:39:55 +02002093 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
dingtianhong844223a2014-01-02 09:13:16 +08002094 if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002095 bond_has_slaves(bond))
Jarod Wilsone2a74202019-06-07 10:59:29 -04002096 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",
2097 slave->perm_hwaddr);
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002098 }
2099
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002100 if (rtnl_dereference(bond->primary_slave) == slave)
2101 RCU_INIT_POINTER(bond->primary_slave, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +02002103 if (oldcurrent == slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 bond_change_active_slave(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Holger Eitzenberger58402052008-12-09 23:07:13 -08002106 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /* Must be called only after the slave has been
2108 * detached from the list and the curr_active_slave
2109 * has been cleared (if our_slave == old_current),
2110 * but before a new active slave is selected.
2111 */
2112 bond_alb_deinit_slave(bond, slave);
2113 }
2114
nikolay@redhat.com08963412013-02-18 14:09:42 +00002115 if (all) {
Paul E. McKenney36708b82013-12-09 15:19:53 -08002116 RCU_INIT_POINTER(bond->curr_active_slave, NULL);
nikolay@redhat.com08963412013-02-18 14:09:42 +00002117 } else if (oldcurrent == slave) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002118 /* Note that we hold RTNL over this sequence, so there
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002119 * is no concern that another slave add/remove event
2120 * will interfere.
2121 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 bond_select_active_slave(bond);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002123 }
2124
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002125 if (!bond_has_slaves(bond)) {
Jay Vosburghff59c452006-03-27 13:27:43 -08002126 bond_set_carrier(bond);
Jiri Pirko409cc1f2013-01-30 11:08:11 +01002127 eth_hw_addr_random(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
2129
Neil Hormane843fa52010-10-13 16:01:50 +00002130 unblock_netpoll_tx();
nikolay@redhat.com278b2082013-08-01 16:54:51 +02002131 synchronize_rcu();
Nikolay Aleksandrovee6154e2014-02-26 14:20:30 +01002132 bond->slave_cnt--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02002134 if (!bond_has_slaves(bond)) {
Shlomo Pongratz2af73d42012-04-03 22:56:19 +00002135 call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
Veaceslav Falico80028ea2013-03-06 07:10:32 +00002136 call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
2137 }
Shlomo Pongratz2af73d42012-04-03 22:56:19 +00002138
Michał Mirosławb2a103e2011-05-07 03:22:17 +00002139 bond_compute_features(bond);
2140 if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2141 (old_features & NETIF_F_VLAN_CHALLENGED))
Jarod Wilsone2a74202019-06-07 10:59:29 -04002142 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 +00002143
nikolay@redhat.com1ff412a2013-08-06 12:40:15 +02002144 vlan_vids_del_by_dev(slave_dev, bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002146 /* If the mode uses primary, then this case was handled above by
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00002147 * bond_change_active_slave(..., NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02002149 if (!bond_uses_primary(bond)) {
Neil Horman5a0068d2013-09-27 12:22:15 -04002150 /* unset promiscuity level from slave
2151 * NOTE: The NETDEV_CHANGEADDR call above may change the value
2152 * of the IFF_PROMISC flag in the bond_dev, but we need the
2153 * value of that flag before that change, as that was the value
2154 * when this slave was attached, so we cache at the start of the
2155 * function and use it here. Same goes for ALLMULTI below
2156 */
2157 if (old_flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 dev_set_promiscuity(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /* unset allmulti level from slave */
Neil Horman5a0068d2013-09-27 12:22:15 -04002161 if (old_flags & IFF_ALLMULTI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 dev_set_allmulti(slave_dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00002164 bond_hw_addr_flush(bond_dev, slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002167 slave_disable_netpoll(slave);
WANG Congf6dc31a2010-05-06 00:48:51 -07002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /* close slave before restoring its mac address */
2170 dev_close(slave_dev);
2171
dingtianhong00503b62014-01-25 13:00:29 +08002172 if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
Veaceslav Falico01844092014-05-15 21:39:55 +02002173 BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Moni Shoua2ab82852007-10-09 19:43:39 -07002174 /* restore original ("permanent") mac address */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04002175 bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
2176 slave->dev->addr_len);
2177 ss.ss_family = slave_dev->type;
Petr Machata3a37a962018-12-13 11:54:30 +00002178 dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
Moni Shoua2ab82852007-10-09 19:43:39 -07002179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
WANG Congf51048c2017-07-06 15:01:57 -07002181 if (unregister)
2182 __dev_set_mtu(slave_dev, slave->original_mtu);
2183 else
2184 dev_set_mtu(slave_dev, slave->original_mtu);
Jiri Pirkob15ba0f2010-05-18 05:42:40 +00002185
Taehee Yoo65de65d2019-10-21 18:47:52 +00002186 if (!netif_is_bond_master(slave_dev))
2187 slave_dev->priv_flags &= ~IFF_BONDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
dingtianhong3fdddd82014-05-12 15:08:43 +08002189 bond_free_slave(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
nikolay@redhat.com08963412013-02-18 14:09:42 +00002194/* A wrapper used because of ndo_del_link */
2195int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
2196{
WANG Congf51048c2017-07-06 15:01:57 -07002197 return __bond_release_one(bond_dev, slave_dev, false, false);
nikolay@redhat.com08963412013-02-18 14:09:42 +00002198}
2199
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002200/* First release a slave and then destroy the bond if no more slaves are left.
2201 * Must be under rtnl_lock when this function is called.
2202 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002203static int bond_release_and_destroy(struct net_device *bond_dev,
2204 struct net_device *slave_dev)
Moni Shouad90a1622007-10-09 19:43:43 -07002205{
Wang Chen454d7c92008-11-12 23:37:49 -08002206 struct bonding *bond = netdev_priv(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002207 int ret;
2208
WANG Congf51048c2017-07-06 15:01:57 -07002209 ret = __bond_release_one(bond_dev, slave_dev, false, true);
Cong Wang83270702020-08-14 20:05:58 -07002210 if (ret == 0 && !bond_has_slaves(bond) &&
2211 bond_dev->reg_state != NETREG_UNREGISTERING) {
Amerigo Wang8a8efa22011-02-17 23:43:32 +00002212 bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
Jarod Wilsone2a74202019-06-07 10:59:29 -04002213 netdev_info(bond_dev, "Destroying bond\n");
Nikolay Aleksandrov06f6d102015-07-15 21:52:51 +02002214 bond_remove_proc_entry(bond);
Stephen Hemminger9e716262009-06-12 19:02:47 +00002215 unregister_netdevice(bond_dev);
Moni Shouad90a1622007-10-09 19:43:43 -07002216 }
2217 return ret;
2218}
2219
Zhu Yanjun3d675762017-02-02 23:46:21 -05002220static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Wang Chen454d7c92008-11-12 23:37:49 -08002222 struct bonding *bond = netdev_priv(bond_dev);
Moni Shoua69a23382015-02-03 16:48:30 +02002223 bond_fill_ifbond(bond, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
2226static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
2227{
Wang Chen454d7c92008-11-12 23:37:49 -08002228 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002229 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002230 int i = 0, res = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002233 bond_for_each_slave(bond, slave, iter) {
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002234 if (i++ == (int)info->slave_id) {
Eric Dumazet689c96c2009-04-23 03:39:04 +00002235 res = 0;
Moni Shoua69a23382015-02-03 16:48:30 +02002236 bond_fill_ifslave(slave, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 break;
2238 }
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Eric Dumazet689c96c2009-04-23 03:39:04 +00002241 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
2244/*-------------------------------- Monitoring -------------------------------*/
2245
Eric Dumazet4740d632014-07-15 06:56:55 -07002246/* called with rcu_read_lock() */
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002247static int bond_miimon_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002249 int link_state, commit = 0;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002250 struct list_head *iter;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002251 struct slave *slave;
Jiri Pirko41f89102009-04-24 03:57:29 +00002252 bool ignore_updelay;
2253
Eric Dumazet4740d632014-07-15 06:56:55 -07002254 ignore_updelay = !rcu_dereference(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
dingtianhong4cb4f972013-12-13 10:19:39 +08002256 bond_for_each_slave_rcu(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002257 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002259 link_state = bond_check_dev_link(bond, slave->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 switch (slave->link) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002262 case BOND_LINK_UP:
2263 if (link_state)
2264 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002266 bond_propose_link_state(slave, BOND_LINK_FAIL);
WANG Congd94708a2017-07-25 09:44:25 -07002267 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002268 slave->delay = bond->params.downdelay;
2269 if (slave->delay) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002270 slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
2271 (BOND_MODE(bond) ==
2272 BOND_MODE_ACTIVEBACKUP) ?
2273 (bond_is_active_slave(slave) ?
2274 "active " : "backup ") : "",
2275 bond->params.downdelay * bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002277 /*FALLTHRU*/
2278 case BOND_LINK_FAIL:
2279 if (link_state) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002280 /* recovered before downdelay expired */
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002281 bond_propose_link_state(slave, BOND_LINK_UP);
Veaceslav Falico8e603462014-02-18 07:48:46 +01002282 slave->last_link_up = jiffies;
Jarod Wilsone2a74202019-06-07 10:59:29 -04002283 slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
2284 (bond->params.downdelay - slave->delay) *
2285 bond->params.miimon);
Mahesh Bandewarfb9eb892017-04-11 22:36:00 -07002286 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002287 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002289
2290 if (slave->delay <= 0) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002291 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002292 commit++;
2293 continue;
2294 }
2295
2296 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002299 case BOND_LINK_DOWN:
2300 if (!link_state)
2301 continue;
2302
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002303 bond_propose_link_state(slave, BOND_LINK_BACK);
WANG Congd94708a2017-07-25 09:44:25 -07002304 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002305 slave->delay = bond->params.updelay;
2306
2307 if (slave->delay) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002308 slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
2309 ignore_updelay ? 0 :
2310 bond->params.updelay *
2311 bond->params.miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002313 /*FALLTHRU*/
2314 case BOND_LINK_BACK:
2315 if (!link_state) {
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002316 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jarod Wilsone2a74202019-06-07 10:59:29 -04002317 slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
2318 (bond->params.updelay - slave->delay) *
2319 bond->params.miimon);
Mahesh Bandewarfb9eb892017-04-11 22:36:00 -07002320 commit++;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002321 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002323
Jiri Pirko41f89102009-04-24 03:57:29 +00002324 if (ignore_updelay)
2325 slave->delay = 0;
2326
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002327 if (slave->delay <= 0) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002328 bond_propose_link_state(slave, BOND_LINK_UP);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002329 commit++;
Jiri Pirko41f89102009-04-24 03:57:29 +00002330 ignore_updelay = false;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002331 continue;
2332 }
2333
2334 slave->delay--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 break;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002336 }
2337 }
2338
2339 return commit;
2340}
2341
Tonghao Zhang7e878b62018-05-16 19:09:23 -07002342static void bond_miimon_link_change(struct bonding *bond,
2343 struct slave *slave,
2344 char link)
2345{
2346 switch (BOND_MODE(bond)) {
2347 case BOND_MODE_8023AD:
2348 bond_3ad_handle_link_change(slave, link);
2349 break;
2350 case BOND_MODE_TLB:
2351 case BOND_MODE_ALB:
2352 bond_alb_handle_link_change(bond, slave, link);
2353 break;
2354 case BOND_MODE_XOR:
2355 bond_update_slave_arr(bond, NULL);
2356 break;
2357 }
2358}
2359
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002360static void bond_miimon_commit(struct bonding *bond)
2361{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002362 struct list_head *iter;
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002363 struct slave *slave, *primary;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002364
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002365 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002366 switch (slave->link_new_state) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002367 case BOND_LINK_NOCHANGE:
Thomas Falcon12185df2019-07-16 17:25:10 -05002368 /* For 802.3ad mode, check current slave speed and
2369 * duplex again in case its port was disabled after
2370 * invalid speed/duplex reporting but recovered before
2371 * link monitoring could make a decision on the actual
2372 * link status
2373 */
2374 if (BOND_MODE(bond) == BOND_MODE_8023AD &&
2375 slave->link == BOND_LINK_UP)
2376 bond_3ad_adapter_speed_duplex_changed(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002377 continue;
2378
2379 case BOND_LINK_UP:
Andreas Bornad729bc92017-08-10 06:41:44 +02002380 if (bond_update_speed_duplex(slave) &&
2381 bond_needs_speed_duplex(bond)) {
Mahesh Bandewar3f3c2782017-04-03 18:38:39 -07002382 slave->link = BOND_LINK_DOWN;
Andreas Born11e9d782017-08-12 00:36:55 +02002383 if (net_ratelimit())
Jarod Wilsone2a74202019-06-07 10:59:29 -04002384 slave_warn(bond->dev, slave->dev,
2385 "failed to get link speed/duplex\n");
Mahesh Bandewarb5bf0f52017-03-27 11:37:37 -07002386 continue;
2387 }
Jiri Pirko5d397062015-12-03 12:12:19 +01002388 bond_set_slave_link_state(slave, BOND_LINK_UP,
2389 BOND_SLAVE_NOTIFY_NOW);
Veaceslav Falico8e603462014-02-18 07:48:46 +01002390 slave->last_link_up = jiffies;
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002391
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002392 primary = rtnl_dereference(bond->primary_slave);
Veaceslav Falico01844092014-05-15 21:39:55 +02002393 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002394 /* prevent it from being the active one */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002395 bond_set_backup_slave(slave);
Veaceslav Falico01844092014-05-15 21:39:55 +02002396 } else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002397 /* make it immediately active */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002398 bond_set_active_slave(slave);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002399 }
2400
Jarod Wilsone2a74202019-06-07 10:59:29 -04002401 slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
2402 slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2403 slave->duplex ? "full" : "half");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002404
Tonghao Zhang7e878b62018-05-16 19:09:23 -07002405 bond_miimon_link_change(bond, slave, BOND_LINK_UP);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002406
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02002407 if (!bond->curr_active_slave || slave == primary)
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002408 goto do_failover;
2409
2410 continue;
2411
2412 case BOND_LINK_DOWN:
Jay Vosburghfba4acd2008-10-30 17:41:14 -07002413 if (slave->link_failure_count < UINT_MAX)
2414 slave->link_failure_count++;
2415
Jiri Pirko5d397062015-12-03 12:12:19 +01002416 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
2417 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002418
Veaceslav Falico01844092014-05-15 21:39:55 +02002419 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
2420 BOND_MODE(bond) == BOND_MODE_8023AD)
dingtianhong5e5b0662014-02-26 11:05:22 +08002421 bond_set_slave_inactive_flags(slave,
2422 BOND_SLAVE_NOTIFY_NOW);
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002423
Jarod Wilsone2a74202019-06-07 10:59:29 -04002424 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002425
Tonghao Zhang7e878b62018-05-16 19:09:23 -07002426 bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002427
Eric Dumazet4740d632014-07-15 06:56:55 -07002428 if (slave == rcu_access_pointer(bond->curr_active_slave))
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002429 goto do_failover;
2430
2431 continue;
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 default:
Jarod Wilsone2a74202019-06-07 10:59:29 -04002434 slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n",
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002435 slave->link_new_state);
2436 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002438 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002441do_failover:
Neil Hormane843fa52010-10-13 16:01:50 +00002442 block_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 bond_select_active_slave(bond);
Neil Hormane843fa52010-10-13 16:01:50 +00002444 unblock_netpoll_tx();
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002445 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07002446
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002447 bond_set_carrier(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002450/* bond_mii_monitor
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002451 *
2452 * Really a wrapper that splits the mii monitor into two phases: an
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002453 * inspection, then (if inspection indicates something needs to be done)
2454 * an acquisition of appropriate locks followed by a commit phase to
2455 * implement whatever link state changes are indicated.
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002456 */
stephen hemminger6da67d22013-12-30 10:43:41 -08002457static void bond_mii_monitor(struct work_struct *work)
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002458{
2459 struct bonding *bond = container_of(work, struct bonding,
2460 mii_work.work);
Ben Hutchingsad246c92011-04-26 15:25:52 +00002461 bool should_notify_peers = false;
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002462 bool commit;
David S. Miller1f2cd842013-10-28 00:11:22 -04002463 unsigned long delay;
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002464 struct slave *slave;
2465 struct list_head *iter;
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002466
David S. Miller1f2cd842013-10-28 00:11:22 -04002467 delay = msecs_to_jiffies(bond->params.miimon);
2468
2469 if (!bond_has_slaves(bond))
dingtianhong6b6c5262013-10-24 11:09:03 +08002470 goto re_arm;
dingtianhong6b6c5262013-10-24 11:09:03 +08002471
dingtianhong4cb4f972013-12-13 10:19:39 +08002472 rcu_read_lock();
Ben Hutchingsad246c92011-04-26 15:25:52 +00002473 should_notify_peers = bond_should_notify_peers(bond);
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002474 commit = !!bond_miimon_inspect(bond);
2475 if (bond->send_peer_notif) {
dingtianhong4cb4f972013-12-13 10:19:39 +08002476 rcu_read_unlock();
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002477 if (rtnl_trylock()) {
2478 bond->send_peer_notif--;
2479 rtnl_unlock();
2480 }
2481 } else {
2482 rcu_read_unlock();
2483 }
David S. Miller1f2cd842013-10-28 00:11:22 -04002484
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002485 if (commit) {
David S. Miller1f2cd842013-10-28 00:11:22 -04002486 /* Race avoidance with bond_close cancel of workqueue */
2487 if (!rtnl_trylock()) {
David S. Miller1f2cd842013-10-28 00:11:22 -04002488 delay = 1;
2489 should_notify_peers = false;
2490 goto re_arm;
2491 }
2492
Mahesh Bandewarde77ecd2017-03-27 11:37:33 -07002493 bond_for_each_slave(bond, slave, iter) {
2494 bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER);
2495 }
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002496 bond_miimon_commit(bond);
2497
David S. Miller1f2cd842013-10-28 00:11:22 -04002498 rtnl_unlock(); /* might sleep, hold no other locks */
Vincent Bernat07a4dde2019-07-02 19:43:54 +02002499 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002500
Jay Vosburghf0c76d62008-07-02 18:21:58 -07002501re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002502 if (bond->params.miimon)
David S. Miller1f2cd842013-10-28 00:11:22 -04002503 queue_delayed_work(bond->wq, &bond->mii_work, delay);
2504
David S. Miller1f2cd842013-10-28 00:11:22 -04002505 if (should_notify_peers) {
2506 if (!rtnl_trylock())
2507 return;
2508 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
2509 rtnl_unlock();
2510 }
Jay Vosburgh0b0eef62007-10-17 17:37:48 -07002511}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002512
David Ahernb3208b22016-10-17 19:15:45 -07002513static int bond_upper_dev_walk(struct net_device *upper, void *data)
2514{
2515 __be32 ip = *((__be32 *)data);
2516
2517 return ip == bond_confirm_addr(upper, 0, ip);
2518}
2519
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002520static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002521{
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002522 bool ret = false;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002523
Andy Gospodarekeaddcd72012-03-22 16:14:29 +00002524 if (ip == bond_confirm_addr(bond->dev, 0, ip))
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002525 return true;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002526
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002527 rcu_read_lock();
David Ahernb3208b22016-10-17 19:15:45 -07002528 if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &ip))
2529 ret = true;
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002530 rcu_read_unlock();
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002531
Veaceslav Falico50223ce2013-08-28 23:25:11 +02002532 return ret;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002533}
2534
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002535/* We go to the (large) trouble of VLAN tagging ARP frames because
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002536 * switches in VLAN mode (especially if ports are configured as
2537 * "native" to a VLAN) might not pass non-tagged frames.
2538 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002539static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
2540 __be32 src_ip, struct bond_vlan_tag *tags)
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002541{
2542 struct sk_buff *skb;
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002543 struct bond_vlan_tag *outer_tag = tags;
Jarod Wilsone2a74202019-06-07 10:59:29 -04002544 struct net_device *slave_dev = slave->dev;
2545 struct net_device *bond_dev = slave->bond->dev;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002546
Jarod Wilsone2a74202019-06-07 10:59:29 -04002547 slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
2548 arp_op, &dest_ip, &src_ip);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002549
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002550 skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
2551 NULL, slave_dev->dev_addr, NULL);
2552
2553 if (!skb) {
dingtianhong4873ac32014-03-25 17:44:44 +08002554 net_err_ratelimited("ARP packet allocation failed\n");
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002555 return;
2556 }
dingtianhongfbd929f2014-03-25 17:44:43 +08002557
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002558 if (!tags || tags->vlan_proto == VLAN_N_VID)
2559 goto xmit;
2560
2561 tags++;
2562
Vlad Yasevich44a40852014-05-16 17:20:38 -04002563 /* Go through all the tags backwards and add them to the packet */
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002564 while (tags->vlan_proto != VLAN_N_VID) {
2565 if (!tags->vlan_id) {
2566 tags++;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002567 continue;
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002568 }
Vlad Yasevich44a40852014-05-16 17:20:38 -04002569
Jarod Wilsone2a74202019-06-07 10:59:29 -04002570 slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
2571 ntohs(outer_tag->vlan_proto), tags->vlan_id);
Jiri Pirko62749e22014-11-19 14:04:58 +01002572 skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
2573 tags->vlan_id);
Vlad Yasevich44a40852014-05-16 17:20:38 -04002574 if (!skb) {
2575 net_err_ratelimited("failed to insert inner VLAN tag\n");
2576 return;
2577 }
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002578
2579 tags++;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002580 }
2581 /* Set the outer tag */
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002582 if (outer_tag->vlan_id) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002583 slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
2584 ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
Jiri Pirkob4bef1b2014-11-19 14:04:57 +01002585 __vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
2586 outer_tag->vlan_id);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002587 }
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002588
2589xmit:
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002590 arp_xmit(skb);
2591}
2592
Vlad Yasevich44a40852014-05-16 17:20:38 -04002593/* Validate the device path between the @start_dev and the @end_dev.
2594 * The path is valid if the @end_dev is reachable through device
2595 * stacking.
2596 * When the path is validated, collect any vlan information in the
2597 * path.
2598 */
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002599struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
2600 struct net_device *end_dev,
2601 int level)
Vlad Yasevich44a40852014-05-16 17:20:38 -04002602{
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002603 struct bond_vlan_tag *tags;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002604 struct net_device *upper;
2605 struct list_head *iter;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002606
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002607 if (start_dev == end_dev) {
Kees Cook6396bb22018-06-12 14:03:40 -07002608 tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC);
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002609 if (!tags)
2610 return ERR_PTR(-ENOMEM);
2611 tags[level].vlan_proto = VLAN_N_VID;
2612 return tags;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002613 }
2614
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002615 netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
2616 tags = bond_verify_device_path(upper, end_dev, level + 1);
2617 if (IS_ERR_OR_NULL(tags)) {
2618 if (IS_ERR(tags))
2619 return tags;
2620 continue;
2621 }
2622 if (is_vlan_dev(upper)) {
2623 tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
2624 tags[level].vlan_id = vlan_dev_vlan_id(upper);
2625 }
2626
2627 return tags;
2628 }
2629
2630 return NULL;
Vlad Yasevich44a40852014-05-16 17:20:38 -04002631}
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
2634{
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002635 struct rtable *rt;
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002636 struct bond_vlan_tag *tags;
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002637 __be32 *targets = bond->params.arp_targets, addr;
dingtianhongfbd929f2014-03-25 17:44:43 +08002638 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002640 for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002641 slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
2642 __func__, &targets[i]);
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002643 tags = NULL;
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002644
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002645 /* Find out through which dev should the packet go */
David S. Miller78fbfd82011-03-12 00:00:52 -05002646 rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
2647 RTO_ONLINK, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08002648 if (IS_ERR(rt)) {
Veaceslav Falico28572762014-02-28 12:39:19 +01002649 /* there's no route to target - try to send arp
2650 * probe to generate any traffic (arp_validate=0)
2651 */
dingtianhong4873ac32014-03-25 17:44:44 +08002652 if (bond->params.arp_validate)
2653 net_warn_ratelimited("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
2654 bond->dev->name,
2655 &targets[i]);
Jarod Wilsone2a74202019-06-07 10:59:29 -04002656 bond_arp_send(slave, ARPOP_REQUEST, targets[i],
Vlad Yasevich44a40852014-05-16 17:20:38 -04002657 0, tags);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002658 continue;
2659 }
2660
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002661 /* bond device itself */
2662 if (rt->dst.dev == bond->dev)
2663 goto found;
2664
2665 rcu_read_lock();
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002666 tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002667 rcu_read_unlock();
2668
Veaceslav Falico3e403a72014-07-17 17:02:23 +02002669 if (!IS_ERR_OR_NULL(tags))
Vlad Yasevich44a40852014-05-16 17:20:38 -04002670 goto found;
2671
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002672 /* Not our device - skip */
Jarod Wilsone2a74202019-06-07 10:59:29 -04002673 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 +02002674 &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
Veaceslav Falico3e325822013-08-28 23:25:16 +02002675
Jay Vosburghed4b9f82005-09-14 14:52:09 -07002676 ip_rt_put(rt);
Veaceslav Falico27bc11e2013-08-28 23:25:10 +02002677 continue;
2678
2679found:
2680 addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
2681 ip_rt_put(rt);
Jarod Wilsone2a74202019-06-07 10:59:29 -04002682 bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
Dan Carpentera67eed52014-07-25 15:21:21 +03002683 kfree(tags);
Jay Vosburghc3ade5c2005-06-26 17:52:20 -04002684 }
2685}
2686
Al Virod3bb52b2007-08-22 20:06:58 -04002687static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002688{
Veaceslav Falico8599b522013-06-24 11:49:34 +02002689 int i;
2690
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002691 if (!sip || !bond_has_this_ip(bond, tip)) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002692 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
2693 __func__, &sip, &tip);
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002694 return;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002695 }
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002696
Veaceslav Falico8599b522013-06-24 11:49:34 +02002697 i = bond_get_targets_ip(bond->params.arp_targets, sip);
2698 if (i == -1) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002699 slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
2700 __func__, &sip);
Veaceslav Falico87a7b842013-06-24 11:49:29 +02002701 return;
2702 }
Veaceslav Falico49f17de2014-02-18 07:48:47 +01002703 slave->last_rx = jiffies;
Veaceslav Falico8599b522013-06-24 11:49:34 +02002704 slave->target_last_arp_rx[i] = jiffies;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002705}
2706
nikolay@redhat.com5bb9e0b2013-09-07 00:00:26 +02002707int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
2708 struct slave *slave)
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002709{
Eric Dumazetde063b72012-06-11 19:23:07 +00002710 struct arphdr *arp = (struct arphdr *)skb->data;
Jay Vosburgh21a75f02016-02-02 13:35:56 -08002711 struct slave *curr_active_slave, *curr_arp_slave;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002712 unsigned char *arp_ptr;
Al Virod3bb52b2007-08-22 20:06:58 -04002713 __be32 sip, tip;
Alexey Dobriyan6ade97d2017-09-26 23:12:28 +03002714 int is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
2715 unsigned int alen;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002716
Veaceslav Falicof2cb6912014-02-18 07:48:42 +01002717 if (!slave_do_arp_validate(bond, slave)) {
dingtianhongbedabf92014-05-07 22:10:20 +08002718 if ((slave_do_arp_validate_only(bond) && is_arp) ||
2719 !slave_do_arp_validate_only(bond))
Veaceslav Falico49f17de2014-02-18 07:48:47 +01002720 slave->last_rx = jiffies;
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002721 return RX_HANDLER_ANOTHER;
Veaceslav Falicof2cb6912014-02-18 07:48:42 +01002722 } else if (!is_arp) {
2723 return RX_HANDLER_ANOTHER;
2724 }
Veaceslav Falico2c146102013-06-24 11:49:31 +02002725
Eric Dumazetde063b72012-06-11 19:23:07 +00002726 alen = arp_hdr_len(bond->dev);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002727
Jarod Wilsone2a74202019-06-07 10:59:29 -04002728 slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
2729 __func__, skb->dev->name);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002730
Eric Dumazetde063b72012-06-11 19:23:07 +00002731 if (alen > skb_headlen(skb)) {
2732 arp = kmalloc(alen, GFP_ATOMIC);
2733 if (!arp)
2734 goto out_unlock;
2735 if (skb_copy_bits(skb, 0, arp, alen) < 0)
2736 goto out_unlock;
2737 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002738
Jiri Pirko3aba8912011-04-19 03:48:16 +00002739 if (arp->ar_hln != bond->dev->addr_len ||
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002740 skb->pkt_type == PACKET_OTHERHOST ||
2741 skb->pkt_type == PACKET_LOOPBACK ||
2742 arp->ar_hrd != htons(ARPHRD_ETHER) ||
2743 arp->ar_pro != htons(ETH_P_IP) ||
2744 arp->ar_pln != 4)
2745 goto out_unlock;
2746
2747 arp_ptr = (unsigned char *)(arp + 1);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002748 arp_ptr += bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002749 memcpy(&sip, arp_ptr, 4);
Jiri Pirko3aba8912011-04-19 03:48:16 +00002750 arp_ptr += 4 + bond->dev->addr_len;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002751 memcpy(&tip, arp_ptr, 4);
2752
Jarod Wilsone2a74202019-06-07 10:59:29 -04002753 slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
2754 __func__, slave->dev->name, bond_slave_state(slave),
2755 bond->params.arp_validate, slave_do_arp_validate(bond, slave),
2756 &sip, &tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002757
Veaceslav Falico010d3c32014-02-20 12:07:57 +01002758 curr_active_slave = rcu_dereference(bond->curr_active_slave);
Jay Vosburgh21a75f02016-02-02 13:35:56 -08002759 curr_arp_slave = rcu_dereference(bond->current_arp_slave);
Veaceslav Falico010d3c32014-02-20 12:07:57 +01002760
Jay Vosburgh21a75f02016-02-02 13:35:56 -08002761 /* We 'trust' the received ARP enough to validate it if:
Veaceslav Falicoaeea64a2013-06-24 11:49:32 +02002762 *
Jay Vosburgh21a75f02016-02-02 13:35:56 -08002763 * (a) the slave receiving the ARP is active (which includes the
2764 * current ARP slave, if any), or
2765 *
2766 * (b) the receiving slave isn't active, but there is a currently
2767 * active slave and it received valid arp reply(s) after it became
2768 * the currently active slave, or
2769 *
2770 * (c) there is an ARP slave that sent an ARP during the prior ARP
2771 * interval, and we receive an ARP reply on any slave. We accept
2772 * these because switch FDB update delays may deliver the ARP
2773 * reply to a slave other than the sender of the ARP request.
2774 *
2775 * Note: for (b), backup slaves are receiving the broadcast ARP
2776 * request, not a reply. This request passes from the sending
2777 * slave through the L2 switch(es) to the receiving slave. Since
2778 * this is checking the request, sip/tip are swapped for
2779 * validation.
2780 *
2781 * This is done to avoid endless looping when we can't reach the
Veaceslav Falicoaeea64a2013-06-24 11:49:32 +02002782 * arp_ip_target and fool ourselves with our own arp requests.
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002783 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002784 if (bond_is_active_slave(slave))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002785 bond_validate_arp(bond, slave, sip, tip);
Veaceslav Falico010d3c32014-02-20 12:07:57 +01002786 else if (curr_active_slave &&
2787 time_after(slave_last_rx(bond, curr_active_slave),
2788 curr_active_slave->last_link_up))
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002789 bond_validate_arp(bond, slave, tip, sip);
Jay Vosburgh21a75f02016-02-02 13:35:56 -08002790 else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
2791 bond_time_in_interval(bond,
2792 dev_trans_start(curr_arp_slave->dev), 1))
2793 bond_validate_arp(bond, slave, sip, tip);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002794
2795out_unlock:
Eric Dumazetde063b72012-06-11 19:23:07 +00002796 if (arp != (struct arphdr *)skb->data)
2797 kfree(arp);
Jiri Bohac13a8e0c2012-05-09 01:01:40 +00002798 return RX_HANDLER_ANOTHER;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07002799}
2800
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002801/* function to verify if we're in the arp_interval timeslice, returns true if
2802 * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
2803 * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
2804 */
2805static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
2806 int mod)
2807{
2808 int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
2809
2810 return time_in_range(jiffies,
2811 last_act - delta_in_ticks,
2812 last_act + mod * delta_in_ticks + delta_in_ticks/2);
2813}
2814
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002815/* This function is called regularly to monitor each slave's link
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * ensuring that traffic is being sent and received when arp monitoring
2817 * is used in load-balancing mode. if the adapter has been dormant, then an
2818 * arp is transmitted to generate traffic. see activebackup_arp_monitor for
2819 * arp monitoring in active backup mode.
2820 */
Mahesh Bandeward5e73f72017-03-08 10:55:51 -08002821static void bond_loadbalance_arp_mon(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct slave *slave, *oldcurrent;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002824 struct list_head *iter;
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002825 int do_failover = 0, slave_state_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
David S. Miller1f2cd842013-10-28 00:11:22 -04002827 if (!bond_has_slaves(bond))
dingtianhong7f1bb572013-10-24 11:09:17 +08002828 goto re_arm;
dingtianhong7f1bb572013-10-24 11:09:17 +08002829
dingtianhong2e52f4f2013-12-13 10:19:50 +08002830 rcu_read_lock();
2831
Eric Dumazet4740d632014-07-15 06:56:55 -07002832 oldcurrent = rcu_dereference(bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 /* see if any of the previous devices are up now (i.e. they have
2834 * xmt and rcv traffic). the curr_active_slave does not come into
Veaceslav Falico8e603462014-02-18 07:48:46 +01002835 * the picture unless it is null. also, slave->last_link_up is not
2836 * needed here because we send an arp on each slave and give a slave
2837 * as long as it needs to get the tx/rx within the delta.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 * TODO: what about up/down delay in arp mode? it wasn't here before
2839 * so it can wait
2840 */
dingtianhong2e52f4f2013-12-13 10:19:50 +08002841 bond_for_each_slave_rcu(bond, slave, iter) {
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002842 unsigned long trans_start = dev_trans_start(slave->dev);
2843
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002844 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Nithin Sujir797a9362017-05-24 19:45:17 -07002845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (slave->link != BOND_LINK_UP) {
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002847 if (bond_time_in_interval(bond, trans_start, 1) &&
Veaceslav Falico49f17de2014-02-18 07:48:47 +01002848 bond_time_in_interval(bond, slave->last_rx, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002850 bond_propose_link_state(slave, BOND_LINK_UP);
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002851 slave_state_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853 /* primary_slave has no meaning in round-robin
2854 * mode. the window of a slave being up and
2855 * curr_active_slave being null after enslaving
2856 * is closed.
2857 */
2858 if (!oldcurrent) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002859 slave_info(bond->dev, slave->dev, "link status definitely up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 do_failover = 1;
2861 } else {
Jarod Wilsone2a74202019-06-07 10:59:29 -04002862 slave_info(bond->dev, slave->dev, "interface is now up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864 }
2865 } else {
2866 /* slave->link == BOND_LINK_UP */
2867
2868 /* not all switches will respond to an arp request
2869 * when the source ip is 0, so don't take the link down
2870 * if we don't know our ip yet
2871 */
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002872 if (!bond_time_in_interval(bond, trans_start, 2) ||
Veaceslav Falico49f17de2014-02-18 07:48:47 +01002873 !bond_time_in_interval(bond, slave->last_rx, 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002875 bond_propose_link_state(slave, BOND_LINK_DOWN);
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002876 slave_state_changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002878 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Jarod Wilsone2a74202019-06-07 10:59:29 -04002881 slave_info(bond->dev, slave->dev, "interface is now down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Stephen Hemminger3d632c32009-06-12 19:02:48 +00002883 if (slave == oldcurrent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 do_failover = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 }
2886 }
2887
2888 /* note: if switch is in round-robin mode, all links
2889 * must tx arp to ensure all links rx an arp - otherwise
2890 * links may oscillate or not come up at all; if switch is
2891 * in something like xor mode, there is nothing we can
2892 * do - all replies will be rx'ed on same link causing slaves
2893 * to be unstable during low/no traffic periods
2894 */
Veaceslav Falicob6adc612014-05-15 21:39:57 +02002895 if (bond_slave_is_up(slave))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 bond_arp_send_all(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898
dingtianhong2e52f4f2013-12-13 10:19:50 +08002899 rcu_read_unlock();
2900
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002901 if (do_failover || slave_state_changed) {
dingtianhong2e52f4f2013-12-13 10:19:50 +08002902 if (!rtnl_trylock())
2903 goto re_arm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Nithin Sujir797a9362017-05-24 19:45:17 -07002905 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002906 if (slave->link_new_state != BOND_LINK_NOCHANGE)
2907 slave->link = slave->link_new_state;
Nithin Sujir797a9362017-05-24 19:45:17 -07002908 }
2909
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002910 if (slave_state_changed) {
2911 bond_slave_state_change(bond);
Mahesh Bandewaree637712014-10-04 17:45:01 -07002912 if (BOND_MODE(bond) == BOND_MODE_XOR)
2913 bond_update_slave_arr(bond, NULL);
Nikolay Aleksandrovb8e45002014-11-18 15:14:44 +01002914 }
2915 if (do_failover) {
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002916 block_netpoll_tx();
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002917 bond_select_active_slave(bond);
Ding Tianhong6fde8f02014-01-28 11:48:53 +08002918 unblock_netpoll_tx();
2919 }
dingtianhong2e52f4f2013-12-13 10:19:50 +08002920 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
2922
2923re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00002924 if (bond->params.arp_interval)
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002925 queue_delayed_work(bond->wq, &bond->arp_work,
2926 msecs_to_jiffies(bond->params.arp_interval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002929/* Called to inspect slaves for active-backup mode ARP monitor link state
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002930 * changes. Sets proposed link state in slaves to specify what action
2931 * should take place for the slave. Returns 0 if no changes are found, >0
2932 * if changes to link states must be committed.
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002933 *
Nikolay Aleksandrov8c0bc552014-09-11 22:49:28 +02002934 * Called with rcu_read_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 */
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002936static int bond_ab_arp_inspect(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Veaceslav Falicodef44602013-08-03 03:50:35 +02002938 unsigned long trans_start, last_rx;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02002939 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002940 struct slave *slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02002941 int commit = 0;
Jiri Bohacda210f52012-08-30 12:02:47 +00002942
dingtianhongeb9fa4b2013-12-13 10:20:02 +08002943 bond_for_each_slave_rcu(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002944 bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
Veaceslav Falicodef44602013-08-03 03:50:35 +02002945 last_rx = slave_last_rx(bond, slave);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (slave->link != BOND_LINK_UP) {
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002948 if (bond_time_in_interval(bond, last_rx, 1)) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002949 bond_propose_link_state(slave, BOND_LINK_UP);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002950 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002952 continue;
2953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002955 /* Give slaves 2*delta after being enslaved or made
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002956 * active. This avoids bouncing, as the last receive
2957 * times need a full ARP monitor cycle to be updated.
2958 */
Veaceslav Falico8e603462014-02-18 07:48:46 +01002959 if (bond_time_in_interval(bond, slave->last_link_up, 2))
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002960 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002962 /* Backup slave is down if:
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002963 * - No current_arp_slave AND
2964 * - more than 3*delta since last receive AND
2965 * - the bond has an IP address
2966 *
2967 * Note: a non-null current_arp_slave indicates
2968 * the curr_active_slave went down and we are
2969 * searching for a new one; under this condition
2970 * we only take the curr_active_slave down - this
2971 * gives each slave a chance to tx/rx traffic
2972 * before being taken out
2973 */
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002974 if (!bond_is_active_slave(slave) &&
Eric Dumazet85741712014-07-15 06:56:56 -07002975 !rcu_access_pointer(bond->current_arp_slave) &&
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002976 !bond_time_in_interval(bond, last_rx, 3)) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002977 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002978 commit++;
2979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002981 /* Active slave is down if:
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002982 * - more than 2*delta since transmitting OR
2983 * - (more than 2*delta since receive AND
2984 * the bond has an IP address)
2985 */
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00002986 trans_start = dev_trans_start(slave->dev);
Jiri Pirkoe30bc062011-03-12 03:14:37 +00002987 if (bond_is_active_slave(slave) &&
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02002988 (!bond_time_in_interval(bond, trans_start, 2) ||
2989 !bond_time_in_interval(bond, last_rx, 2))) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07002990 bond_propose_link_state(slave, BOND_LINK_DOWN);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002991 commit++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
2993 }
2994
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002995 return commit;
2996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02002998/* Called to commit link state changes noted by inspection step of
Jay Vosburghb2220ca2008-05-17 21:10:13 -07002999 * active-backup mode ARP monitor.
3000 *
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003001 * Called with RTNL hold.
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003002 */
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003003static void bond_ab_arp_commit(struct bonding *bond)
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003004{
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003005 unsigned long trans_start;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003006 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02003007 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003009 bond_for_each_slave(bond, slave, iter) {
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003010 switch (slave->link_new_state) {
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003011 case BOND_LINK_NOCHANGE:
3012 continue;
3013
3014 case BOND_LINK_UP:
Jiri Bohaccb32f2a2010-09-02 05:45:54 +00003015 trans_start = dev_trans_start(slave->dev);
Eric Dumazet4740d632014-07-15 06:56:55 -07003016 if (rtnl_dereference(bond->curr_active_slave) != slave ||
3017 (!rtnl_dereference(bond->curr_active_slave) &&
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003018 bond_time_in_interval(bond, trans_start, 1))) {
Eric Dumazet85741712014-07-15 06:56:56 -07003019 struct slave *current_arp_slave;
3020
3021 current_arp_slave = rtnl_dereference(bond->current_arp_slave);
Jiri Pirko5d397062015-12-03 12:12:19 +01003022 bond_set_slave_link_state(slave, BOND_LINK_UP,
3023 BOND_SLAVE_NOTIFY_NOW);
Eric Dumazet85741712014-07-15 06:56:56 -07003024 if (current_arp_slave) {
Veaceslav Falico5a4309742012-04-05 03:47:43 +00003025 bond_set_slave_inactive_flags(
Eric Dumazet85741712014-07-15 06:56:56 -07003026 current_arp_slave,
dingtianhong5e5b0662014-02-26 11:05:22 +08003027 BOND_SLAVE_NOTIFY_NOW);
Eric Dumazet85741712014-07-15 06:56:56 -07003028 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
Veaceslav Falico5a4309742012-04-05 03:47:43 +00003029 }
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003030
Jarod Wilsone2a74202019-06-07 10:59:29 -04003031 slave_info(bond->dev, slave->dev, "link status definitely up\n");
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003032
Eric Dumazet4740d632014-07-15 06:56:55 -07003033 if (!rtnl_dereference(bond->curr_active_slave) ||
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003034 slave == rtnl_dereference(bond->primary_slave))
Jiri Pirkob9f60252009-08-31 11:09:38 +00003035 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003036
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003037 }
3038
Jiri Pirkob9f60252009-08-31 11:09:38 +00003039 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003040
3041 case BOND_LINK_DOWN:
3042 if (slave->link_failure_count < UINT_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Jiri Pirko5d397062015-12-03 12:12:19 +01003045 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3046 BOND_SLAVE_NOTIFY_NOW);
dingtianhong5e5b0662014-02-26 11:05:22 +08003047 bond_set_slave_inactive_flags(slave,
3048 BOND_SLAVE_NOTIFY_NOW);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003049
Jarod Wilsone2a74202019-06-07 10:59:29 -04003050 slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003051
Eric Dumazet4740d632014-07-15 06:56:55 -07003052 if (slave == rtnl_dereference(bond->curr_active_slave)) {
Eric Dumazet85741712014-07-15 06:56:56 -07003053 RCU_INIT_POINTER(bond->current_arp_slave, NULL);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003054 goto do_failover;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003055 }
Jiri Pirkob9f60252009-08-31 11:09:38 +00003056
3057 continue;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003058
3059 default:
Jay Vosburgh1899bb32019-11-01 21:56:42 -07003060 slave_err(bond->dev, slave->dev,
3061 "impossible: link_new_state %d on slave\n",
3062 slave->link_new_state);
Jiri Pirkob9f60252009-08-31 11:09:38 +00003063 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Jiri Pirkob9f60252009-08-31 11:09:38 +00003066do_failover:
Neil Hormane843fa52010-10-13 16:01:50 +00003067 block_netpoll_tx();
Jiri Pirkob9f60252009-08-31 11:09:38 +00003068 bond_select_active_slave(bond);
Neil Hormane843fa52010-10-13 16:01:50 +00003069 unblock_netpoll_tx();
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003070 }
3071
3072 bond_set_carrier(bond);
3073}
3074
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003075/* Send ARP probes for active-backup mode ARP monitor.
dingtianhongb0929912014-02-26 11:05:23 +08003076 *
Nikolay Aleksandrov8c0bc552014-09-11 22:49:28 +02003077 * Called with rcu_read_lock held.
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003078 */
Veaceslav Falicof2ebd472014-01-27 14:37:32 +01003079static bool bond_ab_arp_probe(struct bonding *bond)
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003080{
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003081 struct slave *slave, *before = NULL, *new_slave = NULL,
dingtianhongb0929912014-02-26 11:05:23 +08003082 *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
3083 *curr_active_slave = rcu_dereference(bond->curr_active_slave);
Veaceslav Falico4087df872013-09-25 09:20:19 +02003084 struct list_head *iter;
3085 bool found = false;
dingtianhongb0929912014-02-26 11:05:23 +08003086 bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
Veaceslav Falicof2ebd472014-01-27 14:37:32 +01003087
Veaceslav Falico98b90f22014-01-27 14:37:31 +01003088 if (curr_arp_slave && curr_active_slave)
Veaceslav Falico76444f52014-07-15 19:35:58 +02003089 netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
3090 curr_arp_slave->dev->name,
3091 curr_active_slave->dev->name);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003092
Veaceslav Falico98b90f22014-01-27 14:37:31 +01003093 if (curr_active_slave) {
3094 bond_arp_send_all(bond, curr_active_slave);
dingtianhongb0929912014-02-26 11:05:23 +08003095 return should_notify_rtnl;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003096 }
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 /* if we don't have a curr_active_slave, search for the next available
3099 * backup slave from the current_arp_slave and make it the candidate
3100 * for becoming the curr_active_slave
3101 */
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003102
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003103 if (!curr_arp_slave) {
dingtianhongb0929912014-02-26 11:05:23 +08003104 curr_arp_slave = bond_first_slave_rcu(bond);
3105 if (!curr_arp_slave)
3106 return should_notify_rtnl;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003107 }
3108
dingtianhongb0929912014-02-26 11:05:23 +08003109 bond_set_slave_inactive_flags(curr_arp_slave, BOND_SLAVE_NOTIFY_LATER);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003110
dingtianhongb0929912014-02-26 11:05:23 +08003111 bond_for_each_slave_rcu(bond, slave, iter) {
Veaceslav Falicob6adc612014-05-15 21:39:57 +02003112 if (!found && !before && bond_slave_is_up(slave))
Veaceslav Falico4087df872013-09-25 09:20:19 +02003113 before = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Veaceslav Falicob6adc612014-05-15 21:39:57 +02003115 if (found && !new_slave && bond_slave_is_up(slave))
Veaceslav Falico4087df872013-09-25 09:20:19 +02003116 new_slave = slave;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003117 /* if the link state is up at this point, we
3118 * mark it down - this can happen if we have
3119 * simultaneous link failures and
3120 * reselect_active_interface doesn't make this
3121 * one the current slave so it is still marked
3122 * up when it is actually down
3123 */
Veaceslav Falicob6adc612014-05-15 21:39:57 +02003124 if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
Jiri Pirko5d397062015-12-03 12:12:19 +01003125 bond_set_slave_link_state(slave, BOND_LINK_DOWN,
3126 BOND_SLAVE_NOTIFY_LATER);
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003127 if (slave->link_failure_count < UINT_MAX)
3128 slave->link_failure_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
dingtianhong5e5b0662014-02-26 11:05:22 +08003130 bond_set_slave_inactive_flags(slave,
dingtianhongb0929912014-02-26 11:05:23 +08003131 BOND_SLAVE_NOTIFY_LATER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Jarod Wilsone2a74202019-06-07 10:59:29 -04003133 slave_info(bond->dev, slave->dev, "backup interface is now down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003135 if (slave == curr_arp_slave)
Veaceslav Falico4087df872013-09-25 09:20:19 +02003136 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Veaceslav Falico4087df872013-09-25 09:20:19 +02003138
3139 if (!new_slave && before)
3140 new_slave = before;
3141
dingtianhongb0929912014-02-26 11:05:23 +08003142 if (!new_slave)
3143 goto check_state;
Veaceslav Falico4087df872013-09-25 09:20:19 +02003144
Jiri Pirko5d397062015-12-03 12:12:19 +01003145 bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
3146 BOND_SLAVE_NOTIFY_LATER);
dingtianhongb0929912014-02-26 11:05:23 +08003147 bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
Veaceslav Falico4087df872013-09-25 09:20:19 +02003148 bond_arp_send_all(bond, new_slave);
Veaceslav Falico8e603462014-02-18 07:48:46 +01003149 new_slave->last_link_up = jiffies;
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003150 rcu_assign_pointer(bond->current_arp_slave, new_slave);
Veaceslav Falicof2ebd472014-01-27 14:37:32 +01003151
dingtianhongb0929912014-02-26 11:05:23 +08003152check_state:
3153 bond_for_each_slave_rcu(bond, slave, iter) {
Jiri Pirko5d397062015-12-03 12:12:19 +01003154 if (slave->should_notify || slave->should_notify_link) {
dingtianhongb0929912014-02-26 11:05:23 +08003155 should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
3156 break;
3157 }
3158 }
3159 return should_notify_rtnl;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003160}
3161
Mahesh Bandeward5e73f72017-03-08 10:55:51 -08003162static void bond_activebackup_arp_mon(struct bonding *bond)
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003163{
dingtianhongb0929912014-02-26 11:05:23 +08003164 bool should_notify_peers = false;
3165 bool should_notify_rtnl = false;
David S. Miller1f2cd842013-10-28 00:11:22 -04003166 int delta_in_ticks;
Jay Vosburghb2220ca2008-05-17 21:10:13 -07003167
David S. Miller1f2cd842013-10-28 00:11:22 -04003168 delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3169
3170 if (!bond_has_slaves(bond))
dingtianhong80b9d232013-10-24 11:09:25 +08003171 goto re_arm;
dingtianhong80b9d232013-10-24 11:09:25 +08003172
dingtianhongeb9fa4b2013-12-13 10:20:02 +08003173 rcu_read_lock();
Ben Hutchingsad246c92011-04-26 15:25:52 +00003174
dingtianhongb0929912014-02-26 11:05:23 +08003175 should_notify_peers = bond_should_notify_peers(bond);
3176
3177 if (bond_ab_arp_inspect(bond)) {
3178 rcu_read_unlock();
3179
David S. Miller1f2cd842013-10-28 00:11:22 -04003180 /* Race avoidance with bond_close flush of workqueue */
3181 if (!rtnl_trylock()) {
David S. Miller1f2cd842013-10-28 00:11:22 -04003182 delta_in_ticks = 1;
3183 should_notify_peers = false;
3184 goto re_arm;
3185 }
3186
Veaceslav Falicoe7f63f12013-08-03 03:50:36 +02003187 bond_ab_arp_commit(bond);
dingtianhongb0929912014-02-26 11:05:23 +08003188
David S. Miller1f2cd842013-10-28 00:11:22 -04003189 rtnl_unlock();
dingtianhongb0929912014-02-26 11:05:23 +08003190 rcu_read_lock();
David S. Miller1f2cd842013-10-28 00:11:22 -04003191 }
3192
dingtianhongb0929912014-02-26 11:05:23 +08003193 should_notify_rtnl = bond_ab_arp_probe(bond);
3194 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00003197 if (bond->params.arp_interval)
David S. Miller1f2cd842013-10-28 00:11:22 -04003198 queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
3199
dingtianhongb0929912014-02-26 11:05:23 +08003200 if (should_notify_peers || should_notify_rtnl) {
David S. Miller1f2cd842013-10-28 00:11:22 -04003201 if (!rtnl_trylock())
3202 return;
dingtianhongb0929912014-02-26 11:05:23 +08003203
3204 if (should_notify_peers)
3205 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
3206 bond->dev);
Jiri Pirko5d397062015-12-03 12:12:19 +01003207 if (should_notify_rtnl) {
dingtianhongb0929912014-02-26 11:05:23 +08003208 bond_slave_state_notify(bond);
Jiri Pirko5d397062015-12-03 12:12:19 +01003209 bond_slave_link_notify(bond);
3210 }
dingtianhongb0929912014-02-26 11:05:23 +08003211
David S. Miller1f2cd842013-10-28 00:11:22 -04003212 rtnl_unlock();
3213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
Mahesh Bandeward5e73f72017-03-08 10:55:51 -08003216static void bond_arp_monitor(struct work_struct *work)
3217{
3218 struct bonding *bond = container_of(work, struct bonding,
3219 arp_work.work);
3220
3221 if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
3222 bond_activebackup_arp_mon(bond);
3223 else
3224 bond_loadbalance_arp_mon(bond);
3225}
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227/*-------------------------- netdev event handling --------------------------*/
3228
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003229/* Change device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230static int bond_event_changename(struct bonding *bond)
3231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 bond_remove_proc_entry(bond);
3233 bond_create_proc_entry(bond);
Stephen Hemminger7e083842009-06-12 19:02:46 +00003234
Taku Izumif073c7c2010-12-09 15:17:13 +00003235 bond_debug_reregister(bond);
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return NOTIFY_DONE;
3238}
3239
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003240static int bond_master_netdev_event(unsigned long event,
3241 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Wang Chen454d7c92008-11-12 23:37:49 -08003243 struct bonding *event_bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Jarod Wilsone2a74202019-06-07 10:59:29 -04003245 netdev_dbg(bond_dev, "%s called\n", __func__);
3246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 switch (event) {
3248 case NETDEV_CHANGENAME:
3249 return bond_event_changename(event_bond);
Eric W. Biedermana64d49c2012-07-09 10:51:45 +00003250 case NETDEV_UNREGISTER:
3251 bond_remove_proc_entry(event_bond);
3252 break;
3253 case NETDEV_REGISTER:
3254 bond_create_proc_entry(event_bond);
3255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 default:
3257 break;
3258 }
3259
3260 return NOTIFY_DONE;
3261}
3262
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003263static int bond_slave_netdev_event(unsigned long event,
3264 struct net_device *slave_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003266 struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
nikolay@redhat.com61013912013-04-11 09:18:55 +00003267 struct bonding *bond;
3268 struct net_device *bond_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
nikolay@redhat.com61013912013-04-11 09:18:55 +00003270 /* A netdev event can be generated while enslaving a device
3271 * before netdev_rx_handler_register is called in which case
3272 * slave will be NULL
3273 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04003274 if (!slave) {
3275 netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
nikolay@redhat.com61013912013-04-11 09:18:55 +00003276 return NOTIFY_DONE;
Jarod Wilsone2a74202019-06-07 10:59:29 -04003277 }
3278
nikolay@redhat.com61013912013-04-11 09:18:55 +00003279 bond_dev = slave->bond->dev;
3280 bond = slave->bond;
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003281 primary = rtnl_dereference(bond->primary_slave);
nikolay@redhat.com61013912013-04-11 09:18:55 +00003282
Jarod Wilsone2a74202019-06-07 10:59:29 -04003283 slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
3284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 switch (event) {
3286 case NETDEV_UNREGISTER:
nikolay@redhat.com8d2ada72013-06-26 17:13:37 +02003287 if (bond_dev->type != ARPHRD_ETHER)
Jiri Pirko471cb5a2013-01-03 22:49:01 +00003288 bond_release_and_destroy(bond_dev, slave_dev);
3289 else
WANG Congf51048c2017-07-06 15:01:57 -07003290 __bond_release_one(bond_dev, slave_dev, false, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 break;
Weiping Pan98f41f62011-10-31 17:20:48 +00003292 case NETDEV_UP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 case NETDEV_CHANGE:
Mahesh Bandewar4d2c0cd2017-09-27 18:03:49 -07003294 /* For 802.3ad mode only:
3295 * Getting invalid Speed/Duplex values here will put slave
Jarod Wilson33403122019-05-24 09:49:28 -04003296 * in weird state. Mark it as link-fail if the link was
3297 * previously up or link-down if it hasn't yet come up, and
3298 * let link-monitoring (miimon) set it right when correct
3299 * speeds/duplex are available.
Mahesh Bandewar4d2c0cd2017-09-27 18:03:49 -07003300 */
3301 if (bond_update_speed_duplex(slave) &&
Jarod Wilson33403122019-05-24 09:49:28 -04003302 BOND_MODE(bond) == BOND_MODE_8023AD) {
3303 if (slave->last_link_up)
3304 slave->link = BOND_LINK_FAIL;
3305 else
3306 slave->link = BOND_LINK_DOWN;
3307 }
Mahesh Bandewar4d2c0cd2017-09-27 18:03:49 -07003308
Mahesh Bandewar52bc6712015-10-31 12:45:11 -07003309 if (BOND_MODE(bond) == BOND_MODE_8023AD)
3310 bond_3ad_adapter_speed_duplex_changed(slave);
Mahesh Bandewar950ddcb2015-02-19 10:13:25 -08003311 /* Fallthrough */
3312 case NETDEV_DOWN:
Mahesh Bandewaree637712014-10-04 17:45:01 -07003313 /* Refresh slave-array if applicable!
3314 * If the setup does not use miimon or arpmon (mode-specific!),
3315 * then these events will not cause the slave-array to be
3316 * refreshed. This will cause xmit to use a slave that is not
3317 * usable. Avoid such situation by refeshing the array at these
3318 * events. If these (miimon/arpmon) parameters are configured
3319 * then array gets refreshed twice and that should be fine!
3320 */
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04003321 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07003322 bond_update_slave_arr(bond, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 case NETDEV_CHANGEMTU:
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003325 /* TODO: Should slaves be allowed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 * independently alter their MTU? For
3327 * an active-backup bond, slaves need
3328 * not be the same type of device, so
3329 * MTUs may vary. For other modes,
3330 * slaves arguably should have the
3331 * same MTUs. To do this, we'd need to
3332 * take over the slave's change_mtu
3333 * function for the duration of their
3334 * servitude.
3335 */
3336 break;
3337 case NETDEV_CHANGENAME:
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003338 /* we don't care if we don't have primary set */
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02003339 if (!bond_uses_primary(bond) ||
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003340 !bond->params.primary[0])
3341 break;
3342
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003343 if (slave == primary) {
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003344 /* slave's name changed - he's no longer primary */
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003345 RCU_INIT_POINTER(bond->primary_slave, NULL);
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003346 } else if (!strcmp(slave_dev->name, bond->params.primary)) {
3347 /* we have a new primary slave */
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003348 rcu_assign_pointer(bond->primary_slave, slave);
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003349 } else { /* we didn't change primary - exit */
3350 break;
3351 }
3352
Veaceslav Falico76444f52014-07-15 19:35:58 +02003353 netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
Nikolay Aleksandrov059b47e2014-09-09 23:17:00 +02003354 primary ? slave_dev->name : "none");
dingtianhongf80889a2014-02-12 12:06:40 +08003355
3356 block_netpoll_tx();
Veaceslav Falico3ec775b2014-01-16 02:04:29 +01003357 bond_select_active_slave(bond);
dingtianhongf80889a2014-02-12 12:06:40 +08003358 unblock_netpoll_tx();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 break;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04003360 case NETDEV_FEAT_CHANGE:
3361 bond_compute_features(bond);
3362 break;
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003363 case NETDEV_RESEND_IGMP:
3364 /* Propagate to master device */
3365 call_netdevice_notifiers(event, slave->bond->dev);
3366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 default:
3368 break;
3369 }
3370
3371 return NOTIFY_DONE;
3372}
3373
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003374/* bond_netdev_event: handle netdev notifier chain events.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 *
3376 * This function receives events for the netdev chain. The caller (an
Alan Sterne041c682006-03-27 01:16:30 -08003377 * ioctl handler calling blocking_notifier_call_chain) holds the necessary
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 * locks for us to safely manipulate the slave devices (RTNL lock,
3379 * dev_probe_lock).
3380 */
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003381static int bond_netdev_event(struct notifier_block *this,
3382 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Jiri Pirko351638e2013-05-28 01:30:21 +00003384 struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Jarod Wilson75466dc2019-06-07 10:59:26 -04003386 netdev_dbg(event_dev, "%s received %s\n",
3387 __func__, netdev_cmd_to_name(event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Jay Vosburgh0b680e72006-09-22 21:54:10 -07003389 if (!(event_dev->priv_flags & IFF_BONDING))
3390 return NOTIFY_DONE;
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (event_dev->flags & IFF_MASTER) {
Sabrina Dubroca92480b32019-04-12 15:04:10 +02003393 int ret;
3394
Sabrina Dubroca92480b32019-04-12 15:04:10 +02003395 ret = bond_master_netdev_event(event, event_dev);
3396 if (ret != NOTIFY_DONE)
3397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
3399
Jarod Wilsone2a74202019-06-07 10:59:29 -04003400 if (event_dev->flags & IFF_SLAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 return bond_slave_netdev_event(event, event_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
3403 return NOTIFY_DONE;
3404}
3405
3406static struct notifier_block bond_netdev_notifier = {
3407 .notifier_call = bond_netdev_event,
3408};
3409
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003410/*---------------------------- Hashing Policies -----------------------------*/
3411
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003412/* L2 hash helper */
3413static inline u32 bond_eth_hash(struct sk_buff *skb)
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003414{
Jianhua Xiece04d632014-07-17 14:16:25 +08003415 struct ethhdr *ep, hdr_tmp;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003416
Jianhua Xiece04d632014-07-17 14:16:25 +08003417 ep = skb_header_pointer(skb, 0, sizeof(hdr_tmp), &hdr_tmp);
3418 if (ep)
3419 return ep->h_dest[5] ^ ep->h_source[5] ^ ep->h_proto;
John Eaglesham6b923cb2012-08-21 20:43:35 +00003420 return 0;
3421}
3422
Matteo Crocedf98be02019-11-15 12:10:37 +01003423static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk,
3424 int *noff, int *proto, bool l34)
3425{
3426 const struct ipv6hdr *iph6;
3427 const struct iphdr *iph;
3428
3429 if (skb->protocol == htons(ETH_P_IP)) {
3430 if (unlikely(!pskb_may_pull(skb, *noff + sizeof(*iph))))
3431 return false;
3432 iph = (const struct iphdr *)(skb->data + *noff);
3433 iph_to_flow_copy_v4addrs(fk, iph);
3434 *noff += iph->ihl << 2;
3435 if (!ip_is_fragment(iph))
3436 *proto = iph->protocol;
3437 } else if (skb->protocol == htons(ETH_P_IPV6)) {
3438 if (unlikely(!pskb_may_pull(skb, *noff + sizeof(*iph6))))
3439 return false;
3440 iph6 = (const struct ipv6hdr *)(skb->data + *noff);
3441 iph_to_flow_copy_v6addrs(fk, iph6);
3442 *noff += sizeof(*iph6);
3443 *proto = iph6->nexthdr;
3444 } else {
3445 return false;
3446 }
3447
3448 if (l34 && *proto >= 0)
3449 fk->ports.ports = skb_flow_get_ports(skb, *noff, *proto);
3450
3451 return true;
3452}
3453
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003454/* Extract the appropriate headers based on bond's xmit policy */
3455static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb,
3456 struct flow_keys *fk)
John Eaglesham6b923cb2012-08-21 20:43:35 +00003457{
Matteo Crocedf98be02019-11-15 12:10:37 +01003458 bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003459 int noff, proto = -1;
John Eaglesham6b923cb2012-08-21 20:43:35 +00003460
Matteo Croce58deb772019-10-29 14:50:53 +01003461 if (bond->params.xmit_policy > BOND_XMIT_POLICY_LAYER23) {
3462 memset(fk, 0, sizeof(*fk));
3463 return __skb_flow_dissect(NULL, skb, &flow_keys_bonding,
3464 fk, NULL, 0, 0, 0, 0);
3465 }
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003466
Jiri Pirko06635a32015-05-12 14:56:16 +02003467 fk->ports.ports = 0;
Matteo Croce58deb772019-10-29 14:50:53 +01003468 memset(&fk->icmp, 0, sizeof(fk->icmp));
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003469 noff = skb_network_offset(skb);
Matteo Crocedf98be02019-11-15 12:10:37 +01003470 if (!bond_flow_ip(skb, fk, &noff, &proto, l34))
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003471 return false;
Matteo Crocedf98be02019-11-15 12:10:37 +01003472
3473 /* ICMP error packets contains at least 8 bytes of the header
3474 * of the packet which generated the error. Use this information
3475 * to correlate ICMP error packets within the same flow which
3476 * generated the error.
3477 */
3478 if (proto == IPPROTO_ICMP || proto == IPPROTO_ICMPV6) {
3479 skb_flow_get_icmp_tci(skb, &fk->icmp, skb->data,
3480 skb_transport_offset(skb),
3481 skb_headlen(skb));
3482 if (proto == IPPROTO_ICMP) {
3483 if (!icmp_is_err(fk->icmp.type))
3484 return true;
3485
3486 noff += sizeof(struct icmphdr);
3487 } else if (proto == IPPROTO_ICMPV6) {
3488 if (!icmpv6_is_err(fk->icmp.type))
3489 return true;
3490
3491 noff += sizeof(struct icmp6hdr);
3492 }
3493 return bond_flow_ip(skb, fk, &noff, &proto, l34);
Matteo Croce58deb772019-10-29 14:50:53 +01003494 }
John Eaglesham6b923cb2012-08-21 20:43:35 +00003495
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003496 return true;
John Eaglesham6b923cb2012-08-21 20:43:35 +00003497}
3498
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003499/**
3500 * bond_xmit_hash - generate a hash value based on the xmit policy
3501 * @bond: bonding device
3502 * @skb: buffer to use for headers
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003503 *
3504 * This function will extract the necessary headers from the skb buffer and use
3505 * them to generate a hash based on the xmit_policy set in the bonding device
John Eaglesham6b923cb2012-08-21 20:43:35 +00003506 */
Mahesh Bandewaree62e862014-04-22 16:30:15 -07003507u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
John Eaglesham6b923cb2012-08-21 20:43:35 +00003508{
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003509 struct flow_keys flow;
3510 u32 hash;
John Eaglesham6b923cb2012-08-21 20:43:35 +00003511
Eric Dumazet4b1b8652015-09-15 15:24:28 -07003512 if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
3513 skb->l4_hash)
3514 return skb->hash;
3515
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003516 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
3517 !bond_flow_dissect(bond, skb, &flow))
Mahesh Bandewaree62e862014-04-22 16:30:15 -07003518 return bond_eth_hash(skb);
Eric Dumazet43945422013-04-15 17:03:24 +00003519
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003520 if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
Matteo Croce58deb772019-10-29 14:50:53 +01003521 bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003522 hash = bond_eth_hash(skb);
Matteo Croce58deb772019-10-29 14:50:53 +01003523 } else {
3524 if (flow.icmp.id)
3525 memcpy(&hash, &flow.icmp, sizeof(hash));
3526 else
3527 memcpy(&hash, &flow.ports.ports, sizeof(hash));
3528 }
Tom Herbertc3f83242015-06-04 09:16:40 -07003529 hash ^= (__force u32)flow_get_u32_dst(&flow) ^
3530 (__force u32)flow_get_u32_src(&flow);
Nikolay Aleksandrov32819dc2013-10-02 13:39:25 +02003531 hash ^= (hash >> 16);
3532 hash ^= (hash >> 8);
John Eaglesham6b923cb2012-08-21 20:43:35 +00003533
Hangbin Liub5f86212017-11-06 09:01:57 +08003534 return hash >> 1;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04003535}
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537/*-------------------------- Device entry points ----------------------------*/
3538
Mahesh Bandewarea8ffc02017-04-20 12:49:24 -07003539void bond_work_init_all(struct bonding *bond)
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003540{
3541 INIT_DELAYED_WORK(&bond->mcast_work,
3542 bond_resend_igmp_join_requests_delayed);
3543 INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
3544 INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
Mahesh Bandeward5e73f72017-03-08 10:55:51 -08003545 INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003546 INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
Mahesh Bandewaree637712014-10-04 17:45:01 -07003547 INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003548}
3549
3550static void bond_work_cancel_all(struct bonding *bond)
3551{
3552 cancel_delayed_work_sync(&bond->mii_work);
3553 cancel_delayed_work_sync(&bond->arp_work);
3554 cancel_delayed_work_sync(&bond->alb_work);
3555 cancel_delayed_work_sync(&bond->ad_work);
3556 cancel_delayed_work_sync(&bond->mcast_work);
Mahesh Bandewaree637712014-10-04 17:45:01 -07003557 cancel_delayed_work_sync(&bond->slave_arr_work);
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003558}
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560static int bond_open(struct net_device *bond_dev)
3561{
Wang Chen454d7c92008-11-12 23:37:49 -08003562 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003563 struct list_head *iter;
Peter Pan(潘卫平)ba3211c2011-08-15 15:57:35 +00003564 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Peter Pan(潘卫平)ba3211c2011-08-15 15:57:35 +00003566 /* reset slave->backup and slave->inactive */
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02003567 if (bond_has_slaves(bond)) {
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003568 bond_for_each_slave(bond, slave, iter) {
Eric Dumazet4740d632014-07-15 06:56:55 -07003569 if (bond_uses_primary(bond) &&
3570 slave != rcu_access_pointer(bond->curr_active_slave)) {
dingtianhong5e5b0662014-02-26 11:05:22 +08003571 bond_set_slave_inactive_flags(slave,
3572 BOND_SLAVE_NOTIFY_NOW);
Wilson Kok8bbe71a2015-01-26 01:16:58 -05003573 } else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
dingtianhong5e5b0662014-02-26 11:05:22 +08003574 bond_set_slave_active_flags(slave,
3575 BOND_SLAVE_NOTIFY_NOW);
Peter Pan(潘卫平)ba3211c2011-08-15 15:57:35 +00003576 }
3577 }
Peter Pan(潘卫平)ba3211c2011-08-15 15:57:35 +00003578 }
Peter Pan(潘卫平)ba3211c2011-08-15 15:57:35 +00003579
Holger Eitzenberger58402052008-12-09 23:07:13 -08003580 if (bond_is_lb(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 /* bond_alb_initialize must be called before the timer
3582 * is started.
3583 */
Veaceslav Falico01844092014-05-15 21:39:55 +02003584 if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
stephen hemmingerb4739462010-01-25 23:34:15 +00003585 return -ENOMEM;
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04003586 if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07003587 queue_delayed_work(bond->wq, &bond->alb_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 }
3589
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003590 if (bond->params.miimon) /* link check interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003591 queue_delayed_work(bond->wq, &bond->mii_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
3593 if (bond->params.arp_interval) { /* arp interval, in milliseconds. */
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003594 queue_delayed_work(bond->wq, &bond->arp_work, 0);
Veaceslav Falico3fe68df2014-02-18 07:48:39 +01003595 bond->recv_probe = bond_arp_rcv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 }
3597
Veaceslav Falico01844092014-05-15 21:39:55 +02003598 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
Jay Vosburgh1b76b312007-10-17 17:37:45 -07003599 queue_delayed_work(bond->wq, &bond->ad_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 /* register to receive LACPDUs */
Jiri Pirko3aba8912011-04-19 03:48:16 +00003601 bond->recv_probe = bond_3ad_lacpdu_recv;
Jay Vosburghfd989c82008-11-04 17:51:16 -08003602 bond_3ad_initiate_agg_selection(bond, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
3604
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04003605 if (bond_mode_can_use_xmit_hash(bond))
Mahesh Bandewaree637712014-10-04 17:45:01 -07003606 bond_update_slave_arr(bond, NULL);
3607
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 return 0;
3609}
3610
3611static int bond_close(struct net_device *bond_dev)
3612{
Wang Chen454d7c92008-11-12 23:37:49 -08003613 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
nikolay@redhat.comfbb0c412012-11-29 01:31:31 +00003615 bond_work_cancel_all(bond);
nikolay@redhat.com6c8d23f2013-09-02 13:51:38 +02003616 bond->send_peer_notif = 0;
nikolay@redhat.comee8487c2013-09-02 13:51:39 +02003617 if (bond_is_lb(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 bond_alb_deinitialize(bond);
Jiri Pirko3aba8912011-04-19 03:48:16 +00003619 bond->recv_probe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
3621 return 0;
3622}
3623
Eric Dumazetfe309372016-03-17 17:23:36 -07003624/* fold stats, assuming all rtnl_link_stats64 fields are u64, but
3625 * that some drivers can provide 32bit values only.
3626 */
3627static void bond_fold_stats(struct rtnl_link_stats64 *_res,
3628 const struct rtnl_link_stats64 *_new,
3629 const struct rtnl_link_stats64 *_old)
3630{
3631 const u64 *new = (const u64 *)_new;
3632 const u64 *old = (const u64 *)_old;
3633 u64 *res = (u64 *)_res;
3634 int i;
3635
3636 for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
3637 u64 nv = new[i];
3638 u64 ov = old[i];
Eric Dumazet142c6592017-03-29 10:45:44 -07003639 s64 delta = nv - ov;
Eric Dumazetfe309372016-03-17 17:23:36 -07003640
3641 /* detects if this particular field is 32bit only */
3642 if (((nv | ov) >> 32) == 0)
Eric Dumazet142c6592017-03-29 10:45:44 -07003643 delta = (s64)(s32)((u32)nv - (u32)ov);
3644
3645 /* filter anomalies, some drivers reset their stats
3646 * at down/up events.
3647 */
3648 if (delta > 0)
3649 res[i] += delta;
Eric Dumazetfe309372016-03-17 17:23:36 -07003650 }
3651}
3652
Taehee Yoob3e80d42020-02-15 10:50:40 +00003653#ifdef CONFIG_LOCKDEP
3654static int bond_get_lowest_level_rcu(struct net_device *dev)
3655{
3656 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
3657 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
3658 int cur = 0, max = 0;
3659
3660 now = dev;
3661 iter = &dev->adj_list.lower;
3662
3663 while (1) {
3664 next = NULL;
3665 while (1) {
3666 ldev = netdev_next_lower_dev_rcu(now, &iter);
3667 if (!ldev)
3668 break;
3669
3670 next = ldev;
3671 niter = &ldev->adj_list.lower;
3672 dev_stack[cur] = now;
3673 iter_stack[cur++] = iter;
3674 if (max <= cur)
3675 max = cur;
3676 break;
3677 }
3678
3679 if (!next) {
3680 if (!cur)
3681 return max;
3682 next = dev_stack[--cur];
3683 niter = iter_stack[cur];
3684 }
3685
3686 now = next;
3687 iter = niter;
3688 }
3689
3690 return max;
3691}
3692#endif
3693
stephen hemmingerbc1f4472017-01-06 19:12:52 -08003694static void bond_get_stats(struct net_device *bond_dev,
3695 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Wang Chen454d7c92008-11-12 23:37:49 -08003697 struct bonding *bond = netdev_priv(bond_dev);
Eric Dumazet28172732010-07-07 14:58:56 -07003698 struct rtnl_link_stats64 temp;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003699 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 struct slave *slave;
Taehee Yoob3e80d42020-02-15 10:50:40 +00003701 int nest_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Eric Dumazetfe309372016-03-17 17:23:36 -07003704 rcu_read_lock();
Taehee Yoob3e80d42020-02-15 10:50:40 +00003705#ifdef CONFIG_LOCKDEP
3706 nest_level = bond_get_lowest_level_rcu(bond_dev);
3707#endif
3708
3709 spin_lock_nested(&bond->stats_lock, nest_level);
3710 memcpy(stats, &bond->bond_stats, sizeof(*stats));
3711
Eric Dumazetfe309372016-03-17 17:23:36 -07003712 bond_for_each_slave_rcu(bond, slave, iter) {
3713 const struct rtnl_link_stats64 *new =
Eric Dumazet28172732010-07-07 14:58:56 -07003714 dev_get_stats(slave->dev, &temp);
Stephen Hemmingereeda3fd2008-11-19 21:40:23 -08003715
Eric Dumazetfe309372016-03-17 17:23:36 -07003716 bond_fold_stats(stats, new, &slave->slave_stats);
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04003717
3718 /* save off the slave stats for the next run */
Eric Dumazetfe309372016-03-17 17:23:36 -07003719 memcpy(&slave->slave_stats, new, sizeof(*new));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
Eric Dumazetfe309372016-03-17 17:23:36 -07003721
Andy Gospodarek5f0c5f72014-09-28 22:34:37 -04003722 memcpy(&bond->bond_stats, stats, sizeof(*stats));
Eric Dumazetfe309372016-03-17 17:23:36 -07003723 spin_unlock(&bond->stats_lock);
Taehee Yoob3e80d42020-02-15 10:50:40 +00003724 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
3727static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
3728{
Jiri Pirko080a06e2013-10-18 17:43:36 +02003729 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 struct net_device *slave_dev = NULL;
3731 struct ifbond k_binfo;
3732 struct ifbond __user *u_binfo = NULL;
3733 struct ifslave k_sinfo;
3734 struct ifslave __user *u_sinfo = NULL;
3735 struct mii_ioctl_data *mii = NULL;
Nikolay Aleksandrovd1fbd3ed2014-01-22 14:53:35 +01003736 struct bond_opt_value newval;
Gao feng387ff9112013-01-31 16:31:00 +00003737 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 int res = 0;
3739
Veaceslav Falico76444f52014-07-15 19:35:58 +02003740 netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
3742 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 case SIOCGMIIPHY:
3744 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003745 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 mii->phy_id = 0;
3749 /* Fall Through */
3750 case SIOCGMIIREG:
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003751 /* We do this again just in case we were called by SIOCGMIIREG
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 * instead of SIOCGMIIPHY.
3753 */
3754 mii = if_mii(ifr);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003755 if (!mii)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 return -EINVAL;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003757
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 if (mii->reg_num == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 mii->val_out = 0;
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003760 if (netif_carrier_ok(bond->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 mii->val_out = BMSR_LSTATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 }
3763
3764 return 0;
3765 case BOND_INFO_QUERY_OLD:
3766 case SIOCBONDINFOQUERY:
3767 u_binfo = (struct ifbond __user *)ifr->ifr_data;
3768
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003769 if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Zhu Yanjun3d675762017-02-02 23:46:21 -05003772 bond_info_query(bond_dev, &k_binfo);
3773 if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003774 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Zhu Yanjun3d675762017-02-02 23:46:21 -05003776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 case BOND_SLAVE_INFO_QUERY_OLD:
3778 case SIOCBONDSLAVEINFOQUERY:
3779 u_sinfo = (struct ifslave __user *)ifr->ifr_data;
3780
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003781 if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
3784 res = bond_slave_info_query(bond_dev, &k_sinfo);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003785 if (res == 0 &&
3786 copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
3787 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 return res;
3790 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 break;
3792 }
3793
Gao feng387ff9112013-01-31 16:31:00 +00003794 net = dev_net(bond_dev);
3795
3796 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Ying Xue0917b932014-01-15 10:23:37 +08003799 slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Jarod Wilsone2a74202019-06-07 10:59:29 -04003801 slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Stephen Hemminger3d632c32009-06-12 19:02:48 +00003803 if (!slave_dev)
Ying Xue0917b932014-01-15 10:23:37 +08003804 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Ying Xue0917b932014-01-15 10:23:37 +08003806 switch (cmd) {
3807 case BOND_ENSLAVE_OLD:
3808 case SIOCBONDENSLAVE:
David Ahern33eaf2a2017-10-04 17:48:46 -07003809 res = bond_enslave(bond_dev, slave_dev, NULL);
Ying Xue0917b932014-01-15 10:23:37 +08003810 break;
3811 case BOND_RELEASE_OLD:
3812 case SIOCBONDRELEASE:
3813 res = bond_release(bond_dev, slave_dev);
3814 break;
3815 case BOND_SETHWADDR_OLD:
3816 case SIOCBONDSETHWADDR:
Petr Machatab9245912018-12-13 11:54:44 +00003817 res = bond_set_dev_addr(bond_dev, slave_dev);
Ying Xue0917b932014-01-15 10:23:37 +08003818 break;
3819 case BOND_CHANGE_ACTIVE_OLD:
3820 case SIOCBONDCHANGEACTIVE:
Nikolay Aleksandrovd1fbd3ed2014-01-22 14:53:35 +01003821 bond_opt_initstr(&newval, slave_dev->name);
Vlad Yasevich7a7e96e2017-05-27 10:14:35 -04003822 res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
3823 &newval);
Ying Xue0917b932014-01-15 10:23:37 +08003824 break;
3825 default:
3826 res = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 return res;
3830}
3831
Jiri Pirkod03462b2011-08-16 03:15:04 +00003832static void bond_change_rx_flags(struct net_device *bond_dev, int change)
3833{
3834 struct bonding *bond = netdev_priv(bond_dev);
3835
3836 if (change & IFF_PROMISC)
3837 bond_set_promiscuity(bond,
3838 bond_dev->flags & IFF_PROMISC ? 1 : -1);
3839
3840 if (change & IFF_ALLMULTI)
3841 bond_set_allmulti(bond,
3842 bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
3843}
3844
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00003845static void bond_set_rx_mode(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846{
Wang Chen454d7c92008-11-12 23:37:49 -08003847 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003848 struct list_head *iter;
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00003849 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Veaceslav Falicob3241872013-09-28 21:18:56 +02003851 rcu_read_lock();
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02003852 if (bond_uses_primary(bond)) {
Veaceslav Falicob3241872013-09-28 21:18:56 +02003853 slave = rcu_dereference(bond->curr_active_slave);
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00003854 if (slave) {
3855 dev_uc_sync(slave->dev, bond_dev);
3856 dev_mc_sync(slave->dev, bond_dev);
3857 }
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00003858 } else {
Veaceslav Falicob3241872013-09-28 21:18:56 +02003859 bond_for_each_slave_rcu(bond, slave, iter) {
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00003860 dev_uc_sync_multiple(slave->dev, bond_dev);
3861 dev_mc_sync_multiple(slave->dev, bond_dev);
3862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 }
Veaceslav Falicob3241872013-09-28 21:18:56 +02003864 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865}
3866
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003867static int bond_neigh_init(struct neighbour *n)
Stephen Hemminger00829822008-11-20 20:14:53 -08003868{
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003869 struct bonding *bond = netdev_priv(n->dev);
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003870 const struct net_device_ops *slave_ops;
3871 struct neigh_parms parms;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02003872 struct slave *slave;
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003873 int ret = 0;
Stephen Hemminger00829822008-11-20 20:14:53 -08003874
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003875 rcu_read_lock();
3876 slave = bond_first_slave_rcu(bond);
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003877 if (!slave)
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003878 goto out;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003879 slave_ops = slave->dev->netdev_ops;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003880 if (!slave_ops->ndo_neigh_setup)
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003881 goto out;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003882
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003883 /* TODO: find another way [1] to implement this.
3884 * Passing a zeroed structure is fragile,
3885 * but at least we do not pass garbage.
3886 *
3887 * [1] One way would be that ndo_neigh_setup() never touch
3888 * struct neigh_parms, but propagate the new neigh_setup()
3889 * back to ___neigh_create() / neigh_parms_alloc()
3890 */
3891 memset(&parms, 0, sizeof(parms));
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003892 ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003893
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003894 if (ret)
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003895 goto out;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003896
Eric Dumazet9e99bfe2019-12-07 14:10:34 -08003897 if (parms.neigh_setup)
3898 ret = parms.neigh_setup(n);
3899out:
3900 rcu_read_unlock();
3901 return ret;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003902}
3903
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003904/* The bonding ndo_neigh_setup is called at init time beofre any
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003905 * slave exists. So we must declare proxy setup function which will
3906 * be used at run time to resolve the actual slave neigh param setup.
Veaceslav Falico9918d5b2013-08-02 19:07:39 +02003907 *
3908 * It's also called by master devices (such as vlans) to setup their
3909 * underlying devices. In that case - do nothing, we're already set up from
3910 * our init.
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003911 */
3912static int bond_neigh_setup(struct net_device *dev,
3913 struct neigh_parms *parms)
3914{
Veaceslav Falico9918d5b2013-08-02 19:07:39 +02003915 /* modify only our neigh_parms */
3916 if (parms->dev == dev)
3917 parms->neigh_setup = bond_neigh_init;
Shlomo Pongratz234bcf82012-04-03 22:56:20 +00003918
Stephen Hemminger00829822008-11-20 20:14:53 -08003919 return 0;
3920}
3921
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003922/* Change the MTU of all of a master's slaves to match the master */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
3924{
Wang Chen454d7c92008-11-12 23:37:49 -08003925 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico81f23b12013-09-25 09:20:13 +02003926 struct slave *slave, *rollback_slave;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003927 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Veaceslav Falico76444f52014-07-15 19:35:58 +02003930 netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003932 bond_for_each_slave(bond, slave, iter) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04003933 slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
Veaceslav Falico76444f52014-07-15 19:35:58 +02003934 slave, slave->dev->netdev_ops->ndo_change_mtu);
Mitch Williamse944ef72005-11-09 10:36:50 -08003935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 res = dev_set_mtu(slave->dev, new_mtu);
3937
3938 if (res) {
3939 /* If we failed to set the slave's mtu to the new value
3940 * we must abort the operation even in ACTIVE_BACKUP
3941 * mode, because if we allow the backup slaves to have
3942 * different mtu values than the active slave we'll
3943 * need to change their mtu when doing a failover. That
3944 * means changing their mtu from timer context, which
3945 * is probably not a good idea.
3946 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04003947 slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
3948 res, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 goto unwind;
3950 }
3951 }
3952
3953 bond_dev->mtu = new_mtu;
3954
3955 return 0;
3956
3957unwind:
3958 /* unwind from head to the slave that failed */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003959 bond_for_each_slave(bond, rollback_slave, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 int tmp_res;
3961
Veaceslav Falico81f23b12013-09-25 09:20:13 +02003962 if (rollback_slave == slave)
3963 break;
3964
3965 tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
Jarod Wilsone2a74202019-06-07 10:59:29 -04003966 if (tmp_res)
3967 slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
3968 tmp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 }
3970
3971 return res;
3972}
3973
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02003974/* Change HW address
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 *
3976 * Note that many devices must be down to change the HW address, and
3977 * downing the master releases all slaves. We can make bonds full of
3978 * bonding devices to test this, however.
3979 */
3980static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
3981{
Wang Chen454d7c92008-11-12 23:37:49 -08003982 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico81f23b12013-09-25 09:20:13 +02003983 struct slave *slave, *rollback_slave;
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04003984 struct sockaddr_storage *ss = addr, tmp_ss;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02003985 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Veaceslav Falico01844092014-05-15 21:39:55 +02003988 if (BOND_MODE(bond) == BOND_MODE_ALB)
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08003989 return bond_alb_set_mac_address(bond_dev, addr);
3990
3991
Jarod Wilsone2a74202019-06-07 10:59:29 -04003992 netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Jay Vosburgh1b5acd22013-05-31 11:57:31 +00003994 /* If fail_over_mac is enabled, do nothing and return success.
3995 * Returning an error causes ifenslave to fail.
Jay Vosburghdd957c52007-10-09 19:57:24 -07003996 */
dingtianhongcc689aa2014-01-25 13:00:57 +08003997 if (bond->params.fail_over_mac &&
Veaceslav Falico01844092014-05-15 21:39:55 +02003998 BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
Jay Vosburghdd957c52007-10-09 19:57:24 -07003999 return 0;
Moni Shoua2ab82852007-10-09 19:43:39 -07004000
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004001 if (!is_valid_ether_addr(ss->__data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004004 bond_for_each_slave(bond, slave, iter) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04004005 slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
4006 __func__, slave);
Petr Machata3a37a962018-12-13 11:54:30 +00004007 res = dev_set_mac_address(slave->dev, addr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 if (res) {
4009 /* TODO: consider downing the slave
4010 * and retry ?
4011 * User should expect communications
4012 * breakage anyway until ARP finish
4013 * updating, so...
4014 */
Jarod Wilsone2a74202019-06-07 10:59:29 -04004015 slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
4016 __func__, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 goto unwind;
4018 }
4019 }
4020
4021 /* success */
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004022 memcpy(bond_dev->dev_addr, ss->__data, bond_dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 return 0;
4024
4025unwind:
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004026 memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
4027 tmp_ss.ss_family = bond_dev->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 /* unwind from head to the slave that failed */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004030 bond_for_each_slave(bond, rollback_slave, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 int tmp_res;
4032
Veaceslav Falico81f23b12013-09-25 09:20:13 +02004033 if (rollback_slave == slave)
4034 break;
4035
Jarod Wilsonfaeeb312017-04-04 17:32:42 -04004036 tmp_res = dev_set_mac_address(rollback_slave->dev,
Petr Machata3a37a962018-12-13 11:54:30 +00004037 (struct sockaddr *)&tmp_ss, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 if (tmp_res) {
Jarod Wilsone2a74202019-06-07 10:59:29 -04004039 slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
4040 __func__, tmp_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
4042 }
4043
4044 return res;
4045}
4046
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004047/**
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004048 * bond_get_slave_by_id - get xmit slave with slave_id
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004049 * @bond: bonding device that is transmitting
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004050 * @slave_id: slave id up to slave_cnt-1 through which to transmit
4051 *
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004052 * This function tries to get slave with slave_id but in case
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004053 * it fails, it tries to find the first available slave for transmission.
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004054 */
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004055static struct slave *bond_get_slave_by_id(struct bonding *bond,
4056 int slave_id)
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004057{
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004058 struct list_head *iter;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004059 struct slave *slave;
4060 int i = slave_id;
4061
4062 /* Here we start from the slave with slave_id */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004063 bond_for_each_slave_rcu(bond, slave, iter) {
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004064 if (--i < 0) {
Eric Dumazetae46f1842020-05-07 09:32:22 -07004065 if (bond_slave_can_tx(slave))
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004066 return slave;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004067 }
4068 }
4069
4070 /* Here we start from the first slave up to slave_id */
4071 i = slave_id;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004072 bond_for_each_slave_rcu(bond, slave, iter) {
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004073 if (--i < 0)
4074 break;
Eric Dumazetae46f1842020-05-07 09:32:22 -07004075 if (bond_slave_can_tx(slave))
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004076 return slave;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004077 }
4078 /* no slave that can tx has been found */
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004079 return NULL;
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004080}
4081
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004082/**
4083 * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
4084 * @bond: bonding device to use
4085 *
4086 * Based on the value of the bonding device's packets_per_slave parameter
4087 * this function generates a slave id, which is usually used as the next
4088 * slave to transmit through.
4089 */
4090static u32 bond_rr_gen_slave_id(struct bonding *bond)
4091{
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004092 u32 slave_id;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01004093 struct reciprocal_value reciprocal_packets_per_slave;
4094 int packets_per_slave = bond->params.packets_per_slave;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004095
4096 switch (packets_per_slave) {
4097 case 0:
4098 slave_id = prandom_u32();
4099 break;
4100 case 1:
4101 slave_id = bond->rr_tx_counter;
4102 break;
4103 default:
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01004104 reciprocal_packets_per_slave =
4105 bond->params.reciprocal_packets_per_slave;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004106 slave_id = reciprocal_divide(bond->rr_tx_counter,
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01004107 reciprocal_packets_per_slave);
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004108 break;
4109 }
4110 bond->rr_tx_counter++;
4111
4112 return slave_id;
4113}
4114
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004115static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
4116 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
Nikolay Aleksandrov15077222013-08-01 16:54:50 +02004118 struct slave *slave;
Cong Wang9d1bc242019-07-01 20:40:24 -07004119 int slave_cnt;
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004120 u32 slave_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004122 /* Start with the curr_active_slave that joined the bond as the
Andy Gospodareka2fd9402010-03-25 14:49:05 +00004123 * default for sending IGMP traffic. For failover purposes one
4124 * needs to maintain some consistency for the interface that will
4125 * send the join/membership reports. The curr_active_slave found
4126 * will send all of this type of traffic.
Jay Vosburghcf5f9042007-10-17 17:37:47 -07004127 */
Cong Wang9d1bc242019-07-01 20:40:24 -07004128 if (skb->protocol == htons(ETH_P_IP)) {
4129 int noff = skb_network_offset(skb);
4130 struct iphdr *iph;
Nikolay Aleksandrov9a72c2d2014-09-12 17:38:18 +02004131
Cong Wang9d1bc242019-07-01 20:40:24 -07004132 if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
4133 goto non_igmp;
4134
4135 iph = ip_hdr(skb);
4136 if (iph->protocol == IPPROTO_IGMP) {
4137 slave = rcu_dereference(bond->curr_active_slave);
4138 if (slave)
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004139 return slave;
4140 return bond_get_slave_by_id(bond, 0);
Nikolay Aleksandrov9a72c2d2014-09-12 17:38:18 +02004141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 }
Michał Mirosław0693e882011-05-07 01:48:02 +00004143
Cong Wang9d1bc242019-07-01 20:40:24 -07004144non_igmp:
4145 slave_cnt = READ_ONCE(bond->slave_cnt);
4146 if (likely(slave_cnt)) {
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004147 slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
4148 return bond_get_slave_by_id(bond, slave_id);
Cong Wang9d1bc242019-07-01 20:40:24 -07004149 }
Maor Gottlieb29d5bbc2020-04-30 22:21:36 +03004150 return NULL;
4151}
4152
4153static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
4154 struct net_device *bond_dev)
4155{
4156 struct bonding *bond = netdev_priv(bond_dev);
4157 struct slave *slave;
4158
4159 slave = bond_xmit_roundrobin_slave_get(bond, skb);
Saeed Mahameed76cd6222020-05-09 00:06:35 -07004160 if (likely(slave))
4161 return bond_dev_queue_xmit(bond, skb, slave->dev);
4162
Eric Dumazetae46f1842020-05-07 09:32:22 -07004163 return bond_tx_drop(bond_dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
Maor Gottlieb5a19f1c2020-04-30 22:21:37 +03004166static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond,
4167 struct sk_buff *skb)
4168{
4169 return rcu_dereference(bond->curr_active_slave);
4170}
4171
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004172/* In active-backup mode, we know that bond->curr_active_slave is always valid if
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 * the bond has a usable interface.
4174 */
Tonghao Zhangdbdc8a22018-05-11 02:53:10 -07004175static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
4176 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
Wang Chen454d7c92008-11-12 23:37:49 -08004178 struct bonding *bond = netdev_priv(bond_dev);
nikolay@redhat.com71bc3b22013-08-01 16:54:48 +02004179 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Maor Gottlieb5a19f1c2020-04-30 22:21:37 +03004181 slave = bond_xmit_activebackup_slave_get(bond, skb);
nikolay@redhat.com71bc3b22013-08-01 16:54:48 +02004182 if (slave)
Eric Dumazetae46f1842020-05-07 09:32:22 -07004183 return bond_dev_queue_xmit(bond, skb, slave->dev);
Michał Mirosław0693e882011-05-07 01:48:02 +00004184
Eric Dumazetae46f1842020-05-07 09:32:22 -07004185 return bond_tx_drop(bond_dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
4187
Mahesh Bandewaree637712014-10-04 17:45:01 -07004188/* Use this to update slave_array when (a) it's not appropriate to update
4189 * slave_array right away (note that update_slave_array() may sleep)
4190 * and / or (b) RTNL is not held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 */
Mahesh Bandewaree637712014-10-04 17:45:01 -07004192void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Mahesh Bandewaree637712014-10-04 17:45:01 -07004194 queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
4195}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Mahesh Bandewaree637712014-10-04 17:45:01 -07004197/* Slave array work handler. Holds only RTNL */
4198static void bond_slave_arr_handler(struct work_struct *work)
4199{
4200 struct bonding *bond = container_of(work, struct bonding,
4201 slave_arr_work.work);
4202 int ret;
4203
4204 if (!rtnl_trylock())
4205 goto err;
4206
4207 ret = bond_update_slave_arr(bond, NULL);
4208 rtnl_unlock();
4209 if (ret) {
4210 pr_warn_ratelimited("Failed to update slave array from WT\n");
4211 goto err;
4212 }
4213 return;
4214
4215err:
4216 bond_slave_arr_work_rearm(bond, 1);
4217}
4218
Maor Gottlieb119d48f2020-04-30 22:21:32 +03004219static void bond_skip_slave(struct bond_up_slave *slaves,
4220 struct slave *skipslave)
4221{
4222 int idx;
4223
4224 /* Rare situation where caller has asked to skip a specific
4225 * slave but allocation failed (most likely!). BTW this is
4226 * only possible when the call is initiated from
4227 * __bond_release_one(). In this situation; overwrite the
4228 * skipslave entry in the array with the last entry from the
4229 * array to avoid a situation where the xmit path may choose
4230 * this to-be-skipped slave to send a packet out.
4231 */
4232 for (idx = 0; slaves && idx < slaves->count; idx++) {
4233 if (skipslave == slaves->arr[idx]) {
4234 slaves->arr[idx] =
4235 slaves->arr[slaves->count - 1];
4236 slaves->count--;
4237 break;
4238 }
4239 }
4240}
4241
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004242static void bond_set_slave_arr(struct bonding *bond,
4243 struct bond_up_slave *usable_slaves,
4244 struct bond_up_slave *all_slaves)
4245{
4246 struct bond_up_slave *usable, *all;
4247
4248 usable = rtnl_dereference(bond->usable_slaves);
4249 rcu_assign_pointer(bond->usable_slaves, usable_slaves);
4250 kfree_rcu(usable, rcu);
4251
4252 all = rtnl_dereference(bond->all_slaves);
4253 rcu_assign_pointer(bond->all_slaves, all_slaves);
4254 kfree_rcu(all, rcu);
4255}
4256
4257static void bond_reset_slave_arr(struct bonding *bond)
4258{
4259 struct bond_up_slave *usable, *all;
4260
4261 usable = rtnl_dereference(bond->usable_slaves);
4262 if (usable) {
4263 RCU_INIT_POINTER(bond->usable_slaves, NULL);
4264 kfree_rcu(usable, rcu);
4265 }
4266
4267 all = rtnl_dereference(bond->all_slaves);
4268 if (all) {
4269 RCU_INIT_POINTER(bond->all_slaves, NULL);
4270 kfree_rcu(all, rcu);
4271 }
4272}
4273
Mahesh Bandewaree637712014-10-04 17:45:01 -07004274/* Build the usable slaves array in control path for modes that use xmit-hash
4275 * to determine the slave interface -
4276 * (a) BOND_MODE_8023AD
4277 * (b) BOND_MODE_XOR
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04004278 * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
Mahesh Bandewaree637712014-10-04 17:45:01 -07004279 *
4280 * The caller is expected to hold RTNL only and NO other lock!
4281 */
4282int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
4283{
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004284 struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004285 struct slave *slave;
4286 struct list_head *iter;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004287 int agg_id = 0;
4288 int ret = 0;
4289
4290#ifdef CONFIG_LOCKDEP
4291 WARN_ON(lockdep_is_held(&bond->mode_lock));
4292#endif
4293
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004294 usable_slaves = kzalloc(struct_size(usable_slaves, arr,
4295 bond->slave_cnt), GFP_KERNEL);
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004296 all_slaves = kzalloc(struct_size(all_slaves, arr,
4297 bond->slave_cnt), GFP_KERNEL);
4298 if (!usable_slaves || !all_slaves) {
Mahesh Bandewaree637712014-10-04 17:45:01 -07004299 ret = -ENOMEM;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004300 goto out;
4301 }
4302 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4303 struct ad_info ad_info;
4304
4305 if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
4306 pr_debug("bond_3ad_get_active_agg_info failed\n");
Mahesh Bandewaree637712014-10-04 17:45:01 -07004307 /* No active aggragator means it's not safe to use
4308 * the previous array.
4309 */
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004310 bond_reset_slave_arr(bond);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004311 goto out;
4312 }
Mahesh Bandewaree637712014-10-04 17:45:01 -07004313 agg_id = ad_info.aggregator_id;
4314 }
4315 bond_for_each_slave(bond, slave, iter) {
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004316 if (skipslave == slave)
4317 continue;
4318
4319 all_slaves->arr[all_slaves->count++] = slave;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004320 if (BOND_MODE(bond) == BOND_MODE_8023AD) {
4321 struct aggregator *agg;
4322
4323 agg = SLAVE_AD_INFO(slave)->port.aggregator;
4324 if (!agg || agg->aggregator_identifier != agg_id)
4325 continue;
4326 }
4327 if (!bond_slave_can_tx(slave))
4328 continue;
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04004329
Jarod Wilsone2a74202019-06-07 10:59:29 -04004330 slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004331 usable_slaves->count);
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04004332
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004333 usable_slaves->arr[usable_slaves->count++] = slave;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004334 }
4335
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004336 bond_set_slave_arr(bond, usable_slaves, all_slaves);
4337 return ret;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004338out:
4339 if (ret != 0 && skipslave) {
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004340 bond_skip_slave(rtnl_dereference(bond->all_slaves),
4341 skipslave);
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004342 bond_skip_slave(rtnl_dereference(bond->usable_slaves),
4343 skipslave);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004344 }
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004345 kfree_rcu(all_slaves, rcu);
4346 kfree_rcu(usable_slaves, rcu);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004347
Mahesh Bandewaree637712014-10-04 17:45:01 -07004348 return ret;
4349}
4350
Maor Gottliebc071d912020-04-30 22:21:35 +03004351static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
4352 struct sk_buff *skb,
4353 struct bond_up_slave *slaves)
4354{
4355 struct slave *slave;
4356 unsigned int count;
4357 u32 hash;
4358
4359 hash = bond_xmit_hash(bond, skb);
4360 count = slaves ? READ_ONCE(slaves->count) : 0;
4361 if (unlikely(!count))
4362 return NULL;
4363
4364 slave = slaves->arr[hash % count];
4365 return slave;
4366}
4367
Mahesh Bandewaree637712014-10-04 17:45:01 -07004368/* Use this Xmit function for 3AD as well as XOR modes. The current
4369 * usable slave array is formed in the control path. The xmit function
4370 * just calculates hash and sends the packet out.
4371 */
Tonghao Zhangdbdc8a22018-05-11 02:53:10 -07004372static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
4373 struct net_device *dev)
Mahesh Bandewaree637712014-10-04 17:45:01 -07004374{
4375 struct bonding *bond = netdev_priv(dev);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004376 struct bond_up_slave *slaves;
Maor Gottliebc071d912020-04-30 22:21:35 +03004377 struct slave *slave;
Mahesh Bandewaree637712014-10-04 17:45:01 -07004378
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004379 slaves = rcu_dereference(bond->usable_slaves);
Maor Gottliebc071d912020-04-30 22:21:35 +03004380 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4381 if (likely(slave))
Eric Dumazetae46f1842020-05-07 09:32:22 -07004382 return bond_dev_queue_xmit(bond, skb, slave->dev);
Michał Mirosław0693e882011-05-07 01:48:02 +00004383
Eric Dumazetae46f1842020-05-07 09:32:22 -07004384 return bond_tx_drop(dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385}
4386
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004387/* in broadcast mode, we send everything to all usable interfaces. */
Tonghao Zhangdbdc8a22018-05-11 02:53:10 -07004388static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
4389 struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
Wang Chen454d7c92008-11-12 23:37:49 -08004391 struct bonding *bond = netdev_priv(bond_dev);
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004392 struct slave *slave = NULL;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004393 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004395 bond_for_each_slave_rcu(bond, slave, iter) {
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004396 if (bond_is_last_slave(bond, slave))
4397 break;
Veaceslav Falicob6adc612014-05-15 21:39:57 +02004398 if (bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004399 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004401 if (!skb2) {
dingtianhong9152e262014-03-25 17:00:10 +08004402 net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
4403 bond_dev->name, __func__);
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004404 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 }
Nikolay Aleksandrov78a646c2013-08-01 16:54:49 +02004406 bond_dev_queue_xmit(bond, skb2, slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
4408 }
Veaceslav Falicob6adc612014-05-15 21:39:57 +02004409 if (slave && bond_slave_is_up(slave) && slave->link == BOND_LINK_UP)
Eric Dumazetae46f1842020-05-07 09:32:22 -07004410 return bond_dev_queue_xmit(bond, skb, slave->dev);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00004411
Eric Dumazetae46f1842020-05-07 09:32:22 -07004412 return bond_tx_drop(bond_dev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413}
4414
4415/*------------------------- Device initialization ---------------------------*/
4416
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004417/* Lookup the slave that corresponds to a qid */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004418static inline int bond_slave_override(struct bonding *bond,
4419 struct sk_buff *skb)
4420{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004421 struct slave *slave = NULL;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004422 struct list_head *iter;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004423
Tonghao Zhangae35c6f2018-05-11 02:53:11 -07004424 if (!skb_rx_queue_recorded(skb))
Michał Mirosław0693e882011-05-07 01:48:02 +00004425 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004426
4427 /* Find out if any slaves have the same mapping as this skb. */
dingtianhong3900f292014-01-02 09:13:06 +08004428 bond_for_each_slave_rcu(bond, slave, iter) {
Tonghao Zhangae35c6f2018-05-11 02:53:11 -07004429 if (slave->queue_id == skb_get_queue_mapping(skb)) {
Anton Nayshtutf5e2dc52015-03-29 14:20:25 +03004430 if (bond_slave_is_up(slave) &&
4431 slave->link == BOND_LINK_UP) {
dingtianhong3900f292014-01-02 09:13:06 +08004432 bond_dev_queue_xmit(bond, skb, slave->dev);
4433 return 0;
4434 }
4435 /* If the slave isn't UP, use default transmit policy. */
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004436 break;
4437 }
4438 }
4439
dingtianhong3900f292014-01-02 09:13:06 +08004440 return 1;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004441}
4442
Neil Horman374eeb52011-06-03 10:35:52 +00004443
Jason Wangf663dd92014-01-10 16:18:26 +08004444static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
Paolo Abenia350ecc2019-03-20 11:02:06 +01004445 struct net_device *sb_dev)
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004446{
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004447 /* This helper function exists to help dev_pick_tx get the correct
Phil Oesterfd0e4352011-03-14 06:22:04 +00004448 * destination queue. Using a helper function skips a call to
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004449 * skb_tx_hash and will put the skbs in the queue we expect on their
4450 * way down to the bonding driver.
4451 */
Phil Oesterfd0e4352011-03-14 06:22:04 +00004452 u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
4453
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004454 /* Save the original txq to restore before passing to the driver */
Tonghao Zhangae35c6f2018-05-11 02:53:11 -07004455 qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
Neil Horman374eeb52011-06-03 10:35:52 +00004456
Phil Oesterfd0e4352011-03-14 06:22:04 +00004457 if (unlikely(txq >= dev->real_num_tx_queues)) {
David Decotignyd30ee672011-04-13 15:22:29 +00004458 do {
Phil Oesterfd0e4352011-03-14 06:22:04 +00004459 txq -= dev->real_num_tx_queues;
David Decotignyd30ee672011-04-13 15:22:29 +00004460 } while (txq >= dev->real_num_tx_queues);
Phil Oesterfd0e4352011-03-14 06:22:04 +00004461 }
4462 return txq;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004463}
4464
Maor Gottlieb33720aa2020-04-30 22:21:39 +03004465static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
4466 struct sk_buff *skb,
4467 bool all_slaves)
4468{
4469 struct bonding *bond = netdev_priv(master_dev);
4470 struct bond_up_slave *slaves;
4471 struct slave *slave = NULL;
4472
4473 switch (BOND_MODE(bond)) {
4474 case BOND_MODE_ROUNDROBIN:
4475 slave = bond_xmit_roundrobin_slave_get(bond, skb);
4476 break;
4477 case BOND_MODE_ACTIVEBACKUP:
4478 slave = bond_xmit_activebackup_slave_get(bond, skb);
4479 break;
4480 case BOND_MODE_8023AD:
4481 case BOND_MODE_XOR:
4482 if (all_slaves)
4483 slaves = rcu_dereference(bond->all_slaves);
4484 else
4485 slaves = rcu_dereference(bond->usable_slaves);
4486 slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
4487 break;
4488 case BOND_MODE_BROADCAST:
4489 break;
4490 case BOND_MODE_ALB:
4491 slave = bond_xmit_alb_slave_get(bond, skb);
4492 break;
4493 case BOND_MODE_TLB:
4494 slave = bond_xmit_tlb_slave_get(bond, skb);
4495 break;
4496 default:
4497 /* Should never happen, mode already checked */
4498 WARN_ONCE(true, "Unknown bonding mode");
4499 break;
4500 }
4501
4502 if (slave)
4503 return slave->dev;
4504 return NULL;
4505}
4506
Michał Mirosław0693e882011-05-07 01:48:02 +00004507static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
Stephen Hemminger00829822008-11-20 20:14:53 -08004508{
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004509 struct bonding *bond = netdev_priv(dev);
4510
Veaceslav Falicod1e2e5c2014-05-15 21:39:52 +02004511 if (bond_should_override_tx_queue(bond) &&
4512 !bond_slave_override(bond, skb))
4513 return NETDEV_TX_OK;
Stephen Hemminger00829822008-11-20 20:14:53 -08004514
Veaceslav Falico01844092014-05-15 21:39:55 +02004515 switch (BOND_MODE(bond)) {
Stephen Hemminger00829822008-11-20 20:14:53 -08004516 case BOND_MODE_ROUNDROBIN:
4517 return bond_xmit_roundrobin(skb, dev);
4518 case BOND_MODE_ACTIVEBACKUP:
4519 return bond_xmit_activebackup(skb, dev);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004520 case BOND_MODE_8023AD:
Stephen Hemminger00829822008-11-20 20:14:53 -08004521 case BOND_MODE_XOR:
Mahesh Bandewaree637712014-10-04 17:45:01 -07004522 return bond_3ad_xor_xmit(skb, dev);
Stephen Hemminger00829822008-11-20 20:14:53 -08004523 case BOND_MODE_BROADCAST:
4524 return bond_xmit_broadcast(skb, dev);
Stephen Hemminger00829822008-11-20 20:14:53 -08004525 case BOND_MODE_ALB:
Stephen Hemminger00829822008-11-20 20:14:53 -08004526 return bond_alb_xmit(skb, dev);
Mahesh Bandewarf05b42e2014-04-22 16:30:20 -07004527 case BOND_MODE_TLB:
4528 return bond_tlb_xmit(skb, dev);
Stephen Hemminger00829822008-11-20 20:14:53 -08004529 default:
4530 /* Should never happen, mode already checked */
Veaceslav Falico76444f52014-07-15 19:35:58 +02004531 netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
Stephen Hemminger00829822008-11-20 20:14:53 -08004532 WARN_ON_ONCE(1);
Eric Dumazetae46f1842020-05-07 09:32:22 -07004533 return bond_tx_drop(dev, skb);
Stephen Hemminger00829822008-11-20 20:14:53 -08004534 }
4535}
4536
Michał Mirosław0693e882011-05-07 01:48:02 +00004537static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
4538{
4539 struct bonding *bond = netdev_priv(dev);
4540 netdev_tx_t ret = NETDEV_TX_OK;
4541
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004542 /* If we risk deadlock from transmitting this in the
Michał Mirosław0693e882011-05-07 01:48:02 +00004543 * netpoll path, tell netpoll to queue the frame for later tx
4544 */
dingtianhong054bb882014-03-25 17:00:09 +08004545 if (unlikely(is_netpoll_tx_blocked(dev)))
Michał Mirosław0693e882011-05-07 01:48:02 +00004546 return NETDEV_TX_BUSY;
4547
nikolay@redhat.com278b2082013-08-01 16:54:51 +02004548 rcu_read_lock();
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02004549 if (bond_has_slaves(bond))
Michał Mirosław0693e882011-05-07 01:48:02 +00004550 ret = __bond_start_xmit(skb, dev);
4551 else
Eric Dumazetae46f1842020-05-07 09:32:22 -07004552 ret = bond_tx_drop(dev, skb);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02004553 rcu_read_unlock();
Michał Mirosław0693e882011-05-07 01:48:02 +00004554
4555 return ret;
4556}
Stephen Hemminger00829822008-11-20 20:14:53 -08004557
Jarod Wilson4ca0d9a2020-08-13 10:09:00 -04004558static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
4559{
4560 if (speed == 0 || speed == SPEED_UNKNOWN)
4561 speed = slave->speed;
4562 else
4563 speed = min(speed, slave->speed);
4564
4565 return speed;
4566}
4567
Philippe Reynesd46b6342016-10-25 18:41:31 +02004568static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
4569 struct ethtool_link_ksettings *cmd)
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00004570{
4571 struct bonding *bond = netdev_priv(bond_dev);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004572 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02004573 struct slave *slave;
Jarod Wilson4ca0d9a2020-08-13 10:09:00 -04004574 u32 speed = 0;
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00004575
Philippe Reynesd46b6342016-10-25 18:41:31 +02004576 cmd->base.duplex = DUPLEX_UNKNOWN;
4577 cmd->base.port = PORT_OTHER;
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00004578
Veaceslav Falico8557cd72014-05-15 21:39:59 +02004579 /* Since bond_slave_can_tx returns false for all inactive or down slaves, we
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00004580 * do not need to check mode. Though link speed might not represent
4581 * the true receive or transmit bandwidth (not all modes are symmetric)
4582 * this is an accurate maximum.
4583 */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02004584 bond_for_each_slave(bond, slave, iter) {
Veaceslav Falico8557cd72014-05-15 21:39:59 +02004585 if (bond_slave_can_tx(slave)) {
Jarod Wilson4ca0d9a2020-08-13 10:09:00 -04004586 if (slave->speed != SPEED_UNKNOWN) {
4587 if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
4588 speed = bond_mode_bcast_speed(slave,
4589 speed);
4590 else
4591 speed += slave->speed;
4592 }
Philippe Reynesd46b6342016-10-25 18:41:31 +02004593 if (cmd->base.duplex == DUPLEX_UNKNOWN &&
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00004594 slave->duplex != DUPLEX_UNKNOWN)
Philippe Reynesd46b6342016-10-25 18:41:31 +02004595 cmd->base.duplex = slave->duplex;
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00004596 }
4597 }
Philippe Reynesd46b6342016-10-25 18:41:31 +02004598 cmd->base.speed = speed ? : SPEED_UNKNOWN;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02004599
Andy Gospodarekbb5b0522013-04-16 14:46:00 +00004600 return 0;
4601}
4602
Jay Vosburgh217df672005-09-26 16:11:50 -07004603static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
Jiri Pirko7826d432013-01-06 00:44:26 +00004604 struct ethtool_drvinfo *drvinfo)
Jay Vosburgh217df672005-09-26 16:11:50 -07004605{
Jiri Pirko7826d432013-01-06 00:44:26 +00004606 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +00004607 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
4608 BOND_ABI_VERSION);
Jay Vosburgh217df672005-09-26 16:11:50 -07004609}
4610
Jeff Garzik7282d492006-09-13 14:30:00 -04004611static const struct ethtool_ops bond_ethtool_ops = {
Jay Vosburgh217df672005-09-26 16:11:50 -07004612 .get_drvinfo = bond_ethtool_get_drvinfo,
Stephen Hemmingerfa53eba2008-09-13 21:17:09 -04004613 .get_link = ethtool_op_get_link,
Philippe Reynesd46b6342016-10-25 18:41:31 +02004614 .get_link_ksettings = bond_ethtool_get_link_ksettings,
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004615};
4616
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004617static const struct net_device_ops bond_netdev_ops = {
Stephen Hemminger181470f2009-06-12 19:02:52 +00004618 .ndo_init = bond_init,
Stephen Hemminger9e716262009-06-12 19:02:47 +00004619 .ndo_uninit = bond_uninit,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004620 .ndo_open = bond_open,
4621 .ndo_stop = bond_close,
Stephen Hemminger00829822008-11-20 20:14:53 -08004622 .ndo_start_xmit = bond_start_xmit,
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004623 .ndo_select_queue = bond_select_queue,
Ben Hutchingsbe1f3c22010-06-08 07:19:54 +00004624 .ndo_get_stats64 = bond_get_stats,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004625 .ndo_do_ioctl = bond_do_ioctl,
Jiri Pirkod03462b2011-08-16 03:15:04 +00004626 .ndo_change_rx_flags = bond_change_rx_flags,
Jay Vosburgh303d1cb2013-05-31 11:57:30 +00004627 .ndo_set_rx_mode = bond_set_rx_mode,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004628 .ndo_change_mtu = bond_change_mtu,
Jiri Pirkocc0e4072011-07-20 04:54:46 +00004629 .ndo_set_mac_address = bond_set_mac_address,
Stephen Hemminger00829822008-11-20 20:14:53 -08004630 .ndo_neigh_setup = bond_neigh_setup,
Jiri Pirkocc0e4072011-07-20 04:54:46 +00004631 .ndo_vlan_rx_add_vid = bond_vlan_rx_add_vid,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004632 .ndo_vlan_rx_kill_vid = bond_vlan_rx_kill_vid,
WANG Congf6dc31a2010-05-06 00:48:51 -07004633#ifdef CONFIG_NET_POLL_CONTROLLER
Amerigo Wang8a8efa22011-02-17 23:43:32 +00004634 .ndo_netpoll_setup = bond_netpoll_setup,
WANG Congf6dc31a2010-05-06 00:48:51 -07004635 .ndo_netpoll_cleanup = bond_netpoll_cleanup,
4636 .ndo_poll_controller = bond_poll_controller,
4637#endif
Jiri Pirko9232ecc2011-02-13 09:33:01 +00004638 .ndo_add_slave = bond_enslave,
4639 .ndo_del_slave = bond_release,
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004640 .ndo_fix_features = bond_fix_features,
Toshiaki Makita4847f042015-03-27 14:31:14 +09004641 .ndo_features_check = passthru_features_check,
Maor Gottlieb33720aa2020-04-30 22:21:39 +03004642 .ndo_get_xmit_slave = bond_xmit_get_slave,
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004643};
4644
Doug Goldsteinb3f92b62013-02-18 14:59:23 +00004645static const struct device_type bond_type = {
4646 .name = "bond",
4647};
4648
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004649static void bond_destructor(struct net_device *bond_dev)
4650{
4651 struct bonding *bond = netdev_priv(bond_dev);
4652 if (bond->wq)
4653 destroy_workqueue(bond->wq);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00004654}
4655
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02004656void bond_setup(struct net_device *bond_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Wang Chen454d7c92008-11-12 23:37:49 -08004658 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Nikolay Aleksandrovb7435622014-09-11 22:49:25 +02004660 spin_lock_init(&bond->mode_lock);
Stephen Hemmingerd2991f72009-06-12 19:02:44 +00004661 bond->params = bonding_defaults;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
4663 /* Initialize pointers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 bond->dev = bond_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 /* Initialize the device entry points */
Stephen Hemminger181470f2009-06-12 19:02:52 +00004667 ether_setup(bond_dev);
WANG Cong31c05412017-03-02 12:24:36 -08004668 bond_dev->max_mtu = ETH_MAX_MTU;
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08004669 bond_dev->netdev_ops = &bond_netdev_ops;
Arthur Kepner8531c5f2005-08-23 01:34:53 -04004670 bond_dev->ethtool_ops = &bond_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
David S. Millercf124db2017-05-08 12:52:56 -04004672 bond_dev->needs_free_netdev = true;
4673 bond_dev->priv_destructor = bond_destructor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Doug Goldsteinb3f92b62013-02-18 14:59:23 +00004675 SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
4676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 /* Initialize the device options */
Zhang Shengju1098cee2016-03-16 09:59:15 +00004678 bond_dev->flags |= IFF_MASTER;
Phil Sutter1e6f20c2015-08-18 10:30:39 +02004679 bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
Neil Horman550fd082011-07-26 06:05:38 +00004680 bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
Stephen Hemminger181470f2009-06-12 19:02:52 +00004681
Jarod Wilson18cb2612020-06-19 10:31:55 -04004682#ifdef CONFIG_XFRM_OFFLOAD
4683 /* set up xfrm device ops (only supported in active-backup right now) */
Jarod Wilsona3b658c2020-06-30 14:49:41 -04004684 bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
Jarod Wilson18cb2612020-06-19 10:31:55 -04004685 bond->xs = NULL;
4686#endif /* CONFIG_XFRM_OFFLOAD */
4687
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004688 /* don't acquire bond device's netif_tx_lock when transmitting */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 bond_dev->features |= NETIF_F_LLTX;
4690
4691 /* By default, we declare the bond to be fully
4692 * VLAN hardware accelerated capable. Special
4693 * care is taken in the various xmit functions
4694 * when there are slaves that are not hw accel
4695 * capable
4696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Weilong Chenf9399812014-01-22 17:16:30 +08004698 /* Don't allow bond devices to change network namespaces. */
4699 bond_dev->features |= NETIF_F_NETNS_LOCAL;
4700
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004701 bond_dev->hw_features = BOND_VLAN_FEATURES |
Patrick McHardyf6469682013-04-19 02:04:27 +00004702 NETIF_F_HW_VLAN_CTAG_RX |
4703 NETIF_F_HW_VLAN_CTAG_FILTER;
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004704
Willem de Bruijn8eea1ca2018-05-22 11:34:40 -04004705 bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL | NETIF_F_GSO_UDP_L4;
Jarod Wilson18cb2612020-06-19 10:31:55 -04004706#ifdef CONFIG_XFRM_OFFLOAD
Jarod Wilsona3b658c2020-06-30 14:49:41 -04004707 bond_dev->hw_features |= BOND_XFRM_FEATURES;
Jarod Wilson18cb2612020-06-19 10:31:55 -04004708#endif /* CONFIG_XFRM_OFFLOAD */
Michał Mirosławb2a103e2011-05-07 03:22:17 +00004709 bond_dev->features |= bond_dev->hw_features;
YueHaibing30d81772019-06-26 16:08:44 +08004710 bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Jarod Wilsona3b658c2020-06-30 14:49:41 -04004711#ifdef CONFIG_XFRM_OFFLOAD
4712 /* Disable XFRM features if this isn't an active-backup config */
4713 if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
4714 bond_dev->features &= ~BOND_XFRM_FEATURES;
4715#endif /* CONFIG_XFRM_OFFLOAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716}
4717
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004718/* Destroy a bonding device.
4719 * Must be under rtnl_lock when this function is called.
4720 */
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004721static void bond_uninit(struct net_device *bond_dev)
Jay Vosburgha434e432008-10-30 17:41:15 -07004722{
Wang Chen454d7c92008-11-12 23:37:49 -08004723 struct bonding *bond = netdev_priv(bond_dev);
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004724 struct bond_up_slave *usable, *all;
Veaceslav Falico544a0282013-09-25 09:20:15 +02004725 struct list_head *iter;
4726 struct slave *slave;
Jay Vosburgha434e432008-10-30 17:41:15 -07004727
WANG Congf6dc31a2010-05-06 00:48:51 -07004728 bond_netpoll_cleanup(bond_dev);
4729
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004730 /* Release the bonded slaves */
Veaceslav Falico544a0282013-09-25 09:20:15 +02004731 bond_for_each_slave(bond, slave, iter)
WANG Congf51048c2017-07-06 15:01:57 -07004732 __bond_release_one(bond_dev, slave->dev, true, true);
Veaceslav Falico76444f52014-07-15 19:35:58 +02004733 netdev_info(bond_dev, "Released all slaves\n");
Eric W. Biedermanc67dfb22009-10-29 14:18:24 +00004734
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004735 usable = rtnl_dereference(bond->usable_slaves);
4736 if (usable) {
Maor Gottliebed7d4f02020-04-30 22:21:33 +03004737 RCU_INIT_POINTER(bond->usable_slaves, NULL);
Maor Gottlieb6b447e72020-04-30 22:21:38 +03004738 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);
Mahesh Bandewaree637712014-10-04 17:45:01 -07004745 }
4746
Jay Vosburgha434e432008-10-30 17:41:15 -07004747 list_del(&bond->bond_list);
4748
Taku Izumif073c7c2010-12-09 15:17:13 +00004749 bond_debug_unregister(bond);
Jay Vosburgha434e432008-10-30 17:41:15 -07004750}
4751
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752/*------------------------- Module initialization ---------------------------*/
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754static int bond_check_params(struct bond_params *params)
4755{
nikolay@redhat.com5a5c5fd2013-05-18 01:18:30 +00004756 int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
stephen hemmingerf3253332014-03-04 16:36:44 -08004757 struct bond_opt_value newval;
4758 const struct bond_opt_value *valptr;
Jarod Wilson72ccc472017-05-19 14:46:46 -04004759 int arp_all_targets_value = 0;
Mahesh Bandewar6791e462015-05-09 00:01:55 -07004760 u16 ad_actor_sys_prio = 0;
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07004761 u16 ad_user_port_key = 0;
Jarod Wilson72ccc472017-05-19 14:46:46 -04004762 __be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
Mahesh Bandewardc9c4d02017-03-08 10:56:02 -08004763 int arp_ip_count;
4764 int bond_mode = BOND_MODE_ROUNDROBIN;
4765 int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
4766 int lacp_fast = 0;
Nikolay Aleksandrovf13ad102017-09-12 15:10:05 +03004767 int tlb_dynamic_lb;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004768
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004769 /* Convert string parameters. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 if (mode) {
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +01004771 bond_opt_initstr(&newval, mode);
4772 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
4773 if (!valptr) {
4774 pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return -EINVAL;
4776 }
Nikolay Aleksandrov2b3798d2014-01-22 14:53:17 +01004777 bond_mode = valptr->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004780 if (xmit_hash_policy) {
Debabrata Banerjeee79c1052018-05-14 14:48:09 -04004781 if (bond_mode == BOND_MODE_ROUNDROBIN ||
4782 bond_mode == BOND_MODE_ACTIVEBACKUP ||
4783 bond_mode == BOND_MODE_BROADCAST) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004784 pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
Joe Perches90194262014-02-15 16:01:45 -08004785 bond_mode_name(bond_mode));
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004786 } else {
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01004787 bond_opt_initstr(&newval, xmit_hash_policy);
4788 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
4789 &newval);
4790 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004791 pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004792 xmit_hash_policy);
4793 return -EINVAL;
4794 }
Nikolay Aleksandrova4b32ce2014-01-22 14:53:19 +01004795 xmit_hashtype = valptr->value;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04004796 }
4797 }
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (lacp_rate) {
4800 if (bond_mode != BOND_MODE_8023AD) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004801 pr_info("lacp_rate param is irrelevant in mode %s\n",
4802 bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 } else {
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01004804 bond_opt_initstr(&newval, lacp_rate);
4805 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
4806 &newval);
4807 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004808 pr_err("Error: Invalid lacp rate \"%s\"\n",
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01004809 lacp_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 return -EINVAL;
4811 }
Nikolay Aleksandrovd3131de2014-01-22 14:53:27 +01004812 lacp_fast = valptr->value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 }
4814 }
4815
Jay Vosburghfd989c82008-11-04 17:51:16 -08004816 if (ad_select) {
Nikolay Aleksandrov548d28b2014-07-13 09:47:47 +02004817 bond_opt_initstr(&newval, ad_select);
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01004818 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
4819 &newval);
4820 if (!valptr) {
4821 pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
Jay Vosburghfd989c82008-11-04 17:51:16 -08004822 return -EINVAL;
4823 }
Nikolay Aleksandrov9e5f5ee2014-01-22 14:53:29 +01004824 params->ad_select = valptr->value;
4825 if (bond_mode != BOND_MODE_8023AD)
Joe Perches91565eb2014-02-15 15:57:04 -08004826 pr_warn("ad_select param only affects 802.3ad mode\n");
Jay Vosburghfd989c82008-11-04 17:51:16 -08004827 } else {
4828 params->ad_select = BOND_AD_STABLE;
4829 }
4830
Nicolas de Pesloüanf5841302009-08-28 13:18:34 +00004831 if (max_bonds < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08004832 pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
4833 max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 max_bonds = BOND_DEFAULT_MAX_BONDS;
4835 }
4836
4837 if (miimon < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08004838 pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4839 miimon, INT_MAX);
Nikolay Aleksandrovb98d9c62014-01-22 14:53:31 +01004840 miimon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 }
4842
4843 if (updelay < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08004844 pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4845 updelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 updelay = 0;
4847 }
4848
4849 if (downdelay < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08004850 pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4851 downdelay, INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 downdelay = 0;
4853 }
4854
Debabrata Banerjeeb3c898e2018-05-16 14:02:13 -04004855 if ((use_carrier != 0) && (use_carrier != 1)) {
4856 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 -08004857 use_carrier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 use_carrier = 1;
4859 }
4860
Ben Hutchingsad246c92011-04-26 15:25:52 +00004861 if (num_peer_notif < 0 || num_peer_notif > 255) {
Joe Perches91565eb2014-02-15 15:57:04 -08004862 pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
4863 num_peer_notif);
Ben Hutchingsad246c92011-04-26 15:25:52 +00004864 num_peer_notif = 1;
4865 }
4866
dingtianhong834db4b2013-12-21 14:40:17 +08004867 /* reset values for 802.3ad/TLB/ALB */
Veaceslav Falico267bed72014-05-15 21:39:53 +02004868 if (!bond_mode_uses_arp(bond_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 if (!miimon) {
Joe Perches91565eb2014-02-15 15:57:04 -08004870 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");
4871 pr_warn("Forcing miimon to 100msec\n");
dingtianhongfe9d04a2013-11-22 22:28:43 +08004872 miimon = BOND_DEFAULT_MIIMON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
4874 }
4875
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004876 if (tx_queues < 1 || tx_queues > 255) {
Joe Perches91565eb2014-02-15 15:57:04 -08004877 pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
4878 tx_queues, BOND_DEFAULT_TX_QUEUES);
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00004879 tx_queues = BOND_DEFAULT_TX_QUEUES;
4880 }
4881
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004882 if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
Joe Perches91565eb2014-02-15 15:57:04 -08004883 pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
4884 all_slaves_active);
Andy Gospodarekebd8e492010-06-02 08:39:21 +00004885 all_slaves_active = 0;
4886 }
4887
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004888 if (resend_igmp < 0 || resend_igmp > 255) {
Joe Perches91565eb2014-02-15 15:57:04 -08004889 pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
4890 resend_igmp, BOND_DEFAULT_RESEND_IGMP);
Flavio Leitnerc2952c32010-10-05 14:23:59 +00004891 resend_igmp = BOND_DEFAULT_RESEND_IGMP;
4892 }
4893
Nikolay Aleksandrovaa59d852014-01-22 14:53:18 +01004894 bond_opt_initval(&newval, packets_per_slave);
4895 if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
Nikolay Aleksandrov73958322013-11-05 13:51:41 +01004896 pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
4897 packets_per_slave, USHRT_MAX);
4898 packets_per_slave = 1;
4899 }
4900
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 if (bond_mode == BOND_MODE_ALB) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004902 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",
4903 updelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 }
4905
4906 if (!miimon) {
4907 if (updelay || downdelay) {
4908 /* just warn the user the up/down delay will have
4909 * no effect since miimon is zero...
4910 */
Joe Perches91565eb2014-02-15 15:57:04 -08004911 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",
4912 updelay, downdelay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 }
4914 } else {
4915 /* don't allow arp monitoring */
4916 if (arp_interval) {
Joe Perches91565eb2014-02-15 15:57:04 -08004917 pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
4918 miimon, arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 arp_interval = 0;
4920 }
4921
4922 if ((updelay % miimon) != 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08004923 pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
4924 updelay, miimon, (updelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 }
4926
4927 updelay /= miimon;
4928
4929 if ((downdelay % miimon) != 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08004930 pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
4931 downdelay, miimon,
4932 (downdelay / miimon) * miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
4934
4935 downdelay /= miimon;
4936 }
4937
4938 if (arp_interval < 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08004939 pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
4940 arp_interval, INT_MAX);
Nikolay Aleksandrov7bdb04e2014-01-22 14:53:23 +01004941 arp_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943
nikolay@redhat.com5a5c5fd2013-05-18 01:18:30 +00004944 for (arp_ip_count = 0, i = 0;
4945 (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
dingtianhong89015c12013-12-04 18:59:31 +08004946 __be32 ip;
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02004947
4948 /* not a complete check, but good enough to catch mistakes */
dingtianhong89015c12013-12-04 18:59:31 +08004949 if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
Veaceslav Falico2807a9f2014-05-15 21:39:56 +02004950 !bond_is_ip_target_ok(ip)) {
Joe Perches91565eb2014-02-15 15:57:04 -08004951 pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
4952 arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 arp_interval = 0;
4954 } else {
Veaceslav Falico0afee4e2013-06-24 11:49:30 +02004955 if (bond_get_targets_ip(arp_target, ip) == -1)
4956 arp_target[arp_ip_count++] = ip;
4957 else
Joe Perches91565eb2014-02-15 15:57:04 -08004958 pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
4959 &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 }
4961 }
4962
4963 if (arp_interval && !arp_ip_count) {
4964 /* don't allow arping if no arp_ip_target given... */
Joe Perches91565eb2014-02-15 15:57:04 -08004965 pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
4966 arp_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 arp_interval = 0;
4968 }
4969
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004970 if (arp_validate) {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004971 if (!arp_interval) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004972 pr_err("arp_validate requires arp_interval\n");
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004973 return -EINVAL;
4974 }
4975
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01004976 bond_opt_initstr(&newval, arp_validate);
4977 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
4978 &newval);
4979 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08004980 pr_err("Error: invalid arp_validate \"%s\"\n",
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01004981 arp_validate);
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004982 return -EINVAL;
4983 }
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01004984 arp_validate_value = valptr->value;
4985 } else {
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004986 arp_validate_value = 0;
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01004987 }
Jay Vosburghf5b2b962006-09-22 21:54:53 -07004988
Veaceslav Falico8599b522013-06-24 11:49:34 +02004989 if (arp_all_targets) {
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +01004990 bond_opt_initstr(&newval, arp_all_targets);
4991 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
4992 &newval);
4993 if (!valptr) {
Veaceslav Falico8599b522013-06-24 11:49:34 +02004994 pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
4995 arp_all_targets);
4996 arp_all_targets_value = 0;
Nikolay Aleksandrovedf36b22014-01-22 14:53:21 +01004997 } else {
4998 arp_all_targets_value = valptr->value;
Veaceslav Falico8599b522013-06-24 11:49:34 +02004999 }
5000 }
5001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 if (miimon) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005003 pr_info("MII link monitoring set to %d ms\n", miimon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 } else if (arp_interval) {
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005005 valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
5006 arp_validate_value);
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005007 pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
Nikolay Aleksandrov16228882014-01-22 14:53:20 +01005008 arp_interval, valptr->string, arp_ip_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
5010 for (i = 0; i < arp_ip_count; i++)
Joe Perches90194262014-02-15 16:01:45 -08005011 pr_cont(" %s", arp_ip_target[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
Joe Perches90194262014-02-15 16:01:45 -08005013 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Jay Vosburghb8a97872008-06-13 18:12:04 -07005015 } else if (max_bonds) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 /* miimon and arp_interval not set, we need one so things
5017 * work as expected, see bonding.txt for details
5018 */
Joe Perches90194262014-02-15 16:01:45 -08005019 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 -07005020 }
5021
Veaceslav Falicoec0865a2014-05-15 21:39:54 +02005022 if (primary && !bond_mode_uses_primary(bond_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 /* currently, using a primary only makes sense
5024 * in active backup, TLB or ALB modes
5025 */
Joe Perches91565eb2014-02-15 15:57:04 -08005026 pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
5027 primary, bond_mode_name(bond_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 primary = NULL;
5029 }
5030
Jiri Pirkoa5499522009-09-25 03:28:09 +00005031 if (primary && primary_reselect) {
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01005032 bond_opt_initstr(&newval, primary_reselect);
5033 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
5034 &newval);
5035 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005036 pr_err("Error: Invalid primary_reselect \"%s\"\n",
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01005037 primary_reselect);
Jiri Pirkoa5499522009-09-25 03:28:09 +00005038 return -EINVAL;
5039 }
Nikolay Aleksandrov388d3a62014-01-22 14:53:33 +01005040 primary_reselect_value = valptr->value;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005041 } else {
5042 primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
5043 }
5044
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005045 if (fail_over_mac) {
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01005046 bond_opt_initstr(&newval, fail_over_mac);
5047 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
5048 &newval);
5049 if (!valptr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005050 pr_err("Error: invalid fail_over_mac \"%s\"\n",
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01005051 fail_over_mac);
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005052 return -EINVAL;
5053 }
Nikolay Aleksandrov1df6b6a2014-01-22 14:53:22 +01005054 fail_over_mac_value = valptr->value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005055 if (bond_mode != BOND_MODE_ACTIVEBACKUP)
Joe Perches91565eb2014-02-15 15:57:04 -08005056 pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005057 } else {
5058 fail_over_mac_value = BOND_FOM_NONE;
5059 }
Jay Vosburghdd957c52007-10-09 19:57:24 -07005060
Mahesh Bandewar6791e462015-05-09 00:01:55 -07005061 bond_opt_initstr(&newval, "default");
5062 valptr = bond_opt_parse(
5063 bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
5064 &newval);
5065 if (!valptr) {
5066 pr_err("Error: No ad_actor_sys_prio default value");
5067 return -EINVAL;
5068 }
5069 ad_actor_sys_prio = valptr->value;
5070
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07005071 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
5072 &newval);
5073 if (!valptr) {
5074 pr_err("Error: No ad_user_port_key default value");
5075 return -EINVAL;
5076 }
5077 ad_user_port_key = valptr->value;
5078
Nikolay Aleksandrovf13ad102017-09-12 15:10:05 +03005079 bond_opt_initstr(&newval, "default");
5080 valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
5081 if (!valptr) {
5082 pr_err("Error: No tlb_dynamic_lb default value");
5083 return -EINVAL;
Mahesh Bandewar8b426dc2017-03-08 10:55:56 -08005084 }
Nikolay Aleksandrovf13ad102017-09-12 15:10:05 +03005085 tlb_dynamic_lb = valptr->value;
Mahesh Bandewar8b426dc2017-03-08 10:55:56 -08005086
dingtianhong3a7129e2013-12-21 14:40:12 +08005087 if (lp_interval == 0) {
Joe Perches91565eb2014-02-15 15:57:04 -08005088 pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
5089 INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
dingtianhong3a7129e2013-12-21 14:40:12 +08005090 lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
5091 }
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 /* fill params struct with the proper values */
5094 params->mode = bond_mode;
Jay Vosburgh169a3e62005-06-26 17:54:11 -04005095 params->xmit_policy = xmit_hashtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 params->miimon = miimon;
Ben Hutchingsad246c92011-04-26 15:25:52 +00005097 params->num_peer_notif = num_peer_notif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 params->arp_interval = arp_interval;
Jay Vosburghf5b2b962006-09-22 21:54:53 -07005099 params->arp_validate = arp_validate_value;
Veaceslav Falico8599b522013-06-24 11:49:34 +02005100 params->arp_all_targets = arp_all_targets_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 params->updelay = updelay;
5102 params->downdelay = downdelay;
Vincent Bernat07a4dde2019-07-02 19:43:54 +02005103 params->peer_notif_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 params->use_carrier = use_carrier;
5105 params->lacp_fast = lacp_fast;
5106 params->primary[0] = 0;
Jiri Pirkoa5499522009-09-25 03:28:09 +00005107 params->primary_reselect = primary_reselect_value;
Jay Vosburgh3915c1e82008-05-17 21:10:14 -07005108 params->fail_over_mac = fail_over_mac_value;
Andy Gospodarekbb1d9122010-06-02 08:40:18 +00005109 params->tx_queues = tx_queues;
Andy Gospodarekebd8e492010-06-02 08:39:21 +00005110 params->all_slaves_active = all_slaves_active;
Flavio Leitnerc2952c32010-10-05 14:23:59 +00005111 params->resend_igmp = resend_igmp;
stephen hemminger655f8912011-06-22 09:54:39 +00005112 params->min_links = min_links;
dingtianhong3a7129e2013-12-21 14:40:12 +08005113 params->lp_interval = lp_interval;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01005114 params->packets_per_slave = packets_per_slave;
Mahesh Bandewar8b426dc2017-03-08 10:55:56 -08005115 params->tlb_dynamic_lb = tlb_dynamic_lb;
Mahesh Bandewar6791e462015-05-09 00:01:55 -07005116 params->ad_actor_sys_prio = ad_actor_sys_prio;
Mahesh Bandewar74514952015-05-09 00:01:56 -07005117 eth_zero_addr(params->ad_actor_system);
Mahesh Bandeward22a5fc2015-05-09 00:01:57 -07005118 params->ad_user_port_key = ad_user_port_key;
Hannes Frederic Sowa809fa972014-01-22 02:29:41 +01005119 if (packets_per_slave > 0) {
5120 params->reciprocal_packets_per_slave =
5121 reciprocal_value(packets_per_slave);
5122 } else {
5123 /* reciprocal_packets_per_slave is unused if
5124 * packets_per_slave is 0 or 1, just initialize it
5125 */
5126 params->reciprocal_packets_per_slave =
5127 (struct reciprocal_value) { 0 };
5128 }
5129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 if (primary) {
5131 strncpy(params->primary, primary, IFNAMSIZ);
5132 params->primary[IFNAMSIZ - 1] = 0;
5133 }
5134
5135 memcpy(params->arp_targets, arp_target, sizeof(arp_target));
5136
5137 return 0;
5138}
5139
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005140/* Called from registration process */
Stephen Hemminger181470f2009-06-12 19:02:52 +00005141static int bond_init(struct net_device *bond_dev)
5142{
5143 struct bonding *bond = netdev_priv(bond_dev);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005144 struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005145
Veaceslav Falico76444f52014-07-15 19:35:58 +02005146 netdev_dbg(bond_dev, "Begin bond_init\n");
Stephen Hemminger181470f2009-06-12 19:02:52 +00005147
Bhaktipriya Shridharf9f225e2016-08-30 22:02:01 +05305148 bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005149 if (!bond->wq)
5150 return -ENOMEM;
5151
Taehee Yoo089bca22019-10-21 18:47:53 +00005152 spin_lock_init(&bond->stats_lock);
Cong Wang1a33e102020-05-02 22:22:19 -07005153 netdev_lockdep_set_classes(bond_dev);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005154
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005155 list_add_tail(&bond->bond_list, &bn->dev_list);
Stephen Hemminger181470f2009-06-12 19:02:52 +00005156
Eric W. Biederman6151b3d2009-10-29 14:18:22 +00005157 bond_prepare_sysfs_group(bond);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005158
Taku Izumif073c7c2010-12-09 15:17:13 +00005159 bond_debug_register(bond);
5160
Jiri Pirko409cc1f2013-01-30 11:08:11 +01005161 /* Ensure valid dev_addr */
5162 if (is_zero_ether_addr(bond_dev->dev_addr) &&
nikolay@redhat.com97a1e632013-06-26 17:13:38 +02005163 bond_dev->addr_assign_type == NET_ADDR_PERM)
Jiri Pirko409cc1f2013-01-30 11:08:11 +01005164 eth_hw_addr_random(bond_dev);
Jiri Pirko409cc1f2013-01-30 11:08:11 +01005165
Stephen Hemminger181470f2009-06-12 19:02:52 +00005166 return 0;
5167}
5168
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005169unsigned int bond_get_num_tx_queues(void)
Jiri Pirkod5da4512011-08-10 06:09:44 +00005170{
stephen hemmingerefacb302012-04-10 18:34:43 +00005171 return tx_queues;
Jiri Pirkod5da4512011-08-10 06:09:44 +00005172}
5173
Mitch Williamsdfe60392005-11-09 10:36:04 -08005174/* Create a new bond based on the specified name and bonding parameters.
Jay Vosburghe4b91c42007-01-19 18:15:31 -08005175 * If name is NULL, obtain a suitable "bond%d" name for us.
Mitch Williamsdfe60392005-11-09 10:36:04 -08005176 * Caller must NOT hold rtnl_lock; we need to release it here before we
5177 * set up our sysfs entries.
5178 */
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005179int bond_create(struct net *net, const char *name)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005180{
5181 struct net_device *bond_dev;
Paie913fb22015-04-29 14:24:23 -04005182 struct bonding *bond;
5183 struct alb_bond_info *bond_info;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005184 int res;
5185
5186 rtnl_lock();
Jay Vosburgh027ea042008-01-17 16:25:02 -08005187
Jiri Pirko1c5cae82011-04-30 01:21:32 +00005188 bond_dev = alloc_netdev_mq(sizeof(struct bonding),
Tom Gundersenc835a672014-07-14 16:37:24 +02005189 name ? name : "bond%d", NET_NAME_UNKNOWN,
Jiri Pirko1c5cae82011-04-30 01:21:32 +00005190 bond_setup, tx_queues);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005191 if (!bond_dev) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08005192 pr_err("%s: eek! can't alloc netdev!\n", name);
Amerigo Wang9e2e61f2010-03-31 21:30:52 +00005193 rtnl_unlock();
5194 return -ENOMEM;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005195 }
5196
Paie913fb22015-04-29 14:24:23 -04005197 /*
5198 * Initialize rx_hashtbl_used_head to RLB_NULL_INDEX.
5199 * It is set to 0 by default which is wrong.
5200 */
5201 bond = netdev_priv(bond_dev);
5202 bond_info = &(BOND_ALB_INFO(bond));
5203 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
5204
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005205 dev_net_set(bond_dev, net);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005206 bond_dev->rtnl_link_ops = &bond_link_ops;
5207
Mitch Williamsdfe60392005-11-09 10:36:04 -08005208 res = register_netdevice(bond_dev);
Taehee Yoo544f2872020-07-19 12:11:24 +00005209 if (res < 0) {
5210 free_netdev(bond_dev);
5211 rtnl_unlock();
5212
5213 return res;
5214 }
Peter Zijlstra0daa23032006-11-08 19:51:01 -08005215
Phil Oestere826eaf2011-03-14 06:22:05 +00005216 netif_carrier_off(bond_dev);
5217
Mahesh Bandewar4493b812017-03-08 10:55:54 -08005218 bond_work_init_all(bond);
5219
Mitch Williamsdfe60392005-11-09 10:36:04 -08005220 rtnl_unlock();
Taehee Yoo544f2872020-07-19 12:11:24 +00005221 return 0;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005222}
5223
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005224static int __net_init bond_net_init(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005225{
Eric W. Biederman15449742009-11-29 15:46:04 +00005226 struct bond_net *bn = net_generic(net, bond_net_id);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005227
5228 bn->net = net;
5229 INIT_LIST_HEAD(&bn->dev_list);
5230
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005231 bond_create_proc_dir(bn);
Eric W. Biederman4c224002011-10-12 21:56:25 +00005232 bond_create_sysfs(bn);
Veaceslav Falico87a7b842013-06-24 11:49:29 +02005233
Eric W. Biederman15449742009-11-29 15:46:04 +00005234 return 0;
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005235}
5236
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005237static void __net_exit bond_net_exit(struct net *net)
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005238{
Eric W. Biederman15449742009-11-29 15:46:04 +00005239 struct bond_net *bn = net_generic(net, bond_net_id);
nikolay@redhat.com69b02162013-04-06 00:54:38 +00005240 struct bonding *bond, *tmp_bond;
5241 LIST_HEAD(list);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005242
Eric W. Biederman4c224002011-10-12 21:56:25 +00005243 bond_destroy_sysfs(bn);
nikolay@redhat.com69b02162013-04-06 00:54:38 +00005244
5245 /* Kill off any bonds created after unregistering bond rtnl ops */
5246 rtnl_lock();
5247 list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
5248 unregister_netdevice_queue(bond->dev, &list);
5249 unregister_netdevice_many(&list);
5250 rtnl_unlock();
Veaceslav Falico23fa5c22014-07-17 12:04:08 +02005251
5252 bond_destroy_proc_dir(bn);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005253}
5254
5255static struct pernet_operations bond_net_ops = {
5256 .init = bond_net_init,
5257 .exit = bond_net_exit,
Eric W. Biederman15449742009-11-29 15:46:04 +00005258 .id = &bond_net_id,
5259 .size = sizeof(struct bond_net),
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005260};
5261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262static int __init bonding_init(void)
5263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 int i;
5265 int res;
5266
Mitch Williamsdfe60392005-11-09 10:36:04 -08005267 res = bond_check_params(&bonding_defaults);
Stephen Hemminger3d632c32009-06-12 19:02:48 +00005268 if (res)
Mitch Williamsdfe60392005-11-09 10:36:04 -08005269 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
Eric W. Biederman15449742009-11-29 15:46:04 +00005271 res = register_pernet_subsys(&bond_net_ops);
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005272 if (res)
5273 goto out;
Jay Vosburgh027ea042008-01-17 16:25:02 -08005274
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005275 res = bond_netlink_init();
Eric W. Biederman88ead972009-10-29 14:18:25 +00005276 if (res)
Eric W. Biederman66391042009-10-29 23:58:54 +00005277 goto err_link;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005278
Taku Izumif073c7c2010-12-09 15:17:13 +00005279 bond_create_debugfs();
5280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 for (i = 0; i < max_bonds; i++) {
Eric W. Biedermanec87fd32009-10-29 14:18:26 +00005282 res = bond_create(&init_net, NULL);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005283 if (res)
5284 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 }
5286
Matteo Croce58deb772019-10-29 14:50:53 +01005287 skb_flow_dissector_init(&flow_keys_bonding,
5288 flow_keys_bonding_keys,
5289 ARRAY_SIZE(flow_keys_bonding_keys));
5290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 register_netdevice_notifier(&bond_netdev_notifier);
Mitch Williamsdfe60392005-11-09 10:36:04 -08005292out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 return res;
Eric W. Biederman88ead972009-10-29 14:18:25 +00005294err:
Thomas Richterdb298682014-04-09 12:52:59 +02005295 bond_destroy_debugfs();
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005296 bond_netlink_fini();
Eric W. Biederman66391042009-10-29 23:58:54 +00005297err_link:
Eric W. Biederman15449742009-11-29 15:46:04 +00005298 unregister_pernet_subsys(&bond_net_ops);
Eric W. Biederman88ead972009-10-29 14:18:25 +00005299 goto out;
Mitch Williamsdfe60392005-11-09 10:36:04 -08005300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301}
5302
5303static void __exit bonding_exit(void)
5304{
5305 unregister_netdevice_notifier(&bond_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
Taku Izumif073c7c2010-12-09 15:17:13 +00005307 bond_destroy_debugfs();
Pavel Emelyanovae68c392008-05-02 17:49:39 -07005308
Jiri Pirko0a2a78c2013-10-18 17:43:33 +02005309 bond_netlink_fini();
nikolay@redhat.comffcdedb2013-04-06 00:54:37 +00005310 unregister_pernet_subsys(&bond_net_ops);
Neil Hormane843fa52010-10-13 16:01:50 +00005311
5312#ifdef CONFIG_NET_POLL_CONTROLLER
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02005313 /* Make sure we don't have an imbalance on our netpoll blocking */
Neil Hormanfb4fa762010-12-06 09:05:50 +00005314 WARN_ON(atomic_read(&netpoll_block_tx));
Neil Hormane843fa52010-10-13 16:01:50 +00005315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316}
5317
5318module_init(bonding_init);
5319module_exit(bonding_exit);
5320MODULE_LICENSE("GPL");
Leon Romanovsky2b526b52020-02-24 10:52:54 +02005321MODULE_DESCRIPTION(DRV_DESCRIPTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");