xref: /openbmc/linux/drivers/net/bonding/bond_main.c (revision 16f6ccde74a6f8538c62f127f17207c75f4dba7a)
1613a0141SBagas Sanjaya // SPDX-License-Identifier: GPL-1.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * originally based on the dummy device.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright 1999, Thomas Davis, tadavis@lbl.gov.
6613a0141SBagas Sanjaya  * Based on dummy.c, and eql.c devices.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * bonding.c: an Ethernet Bonding driver
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * This is useful to talk to a Cisco EtherChannel compatible equipment:
111da177e4SLinus Torvalds  *	Cisco 5500
121da177e4SLinus Torvalds  *	Sun Trunking (Solaris)
131da177e4SLinus Torvalds  *	Alteon AceDirector Trunks
141da177e4SLinus Torvalds  *	Linux Bonding
151da177e4SLinus Torvalds  *	and probably many L2 switches ...
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * How it works:
181da177e4SLinus Torvalds  *    ifconfig bond0 ipaddress netmask up
191da177e4SLinus Torvalds  *      will setup a network device, with an ip address.  No mac address
201da177e4SLinus Torvalds  *	will be assigned at this time.  The hw mac address will come from
211da177e4SLinus Torvalds  *	the first slave bonded to the channel.  All slaves will then use
221da177e4SLinus Torvalds  *	this hw mac address.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *    ifconfig bond0 down
251da177e4SLinus Torvalds  *         will release all slaves, marking them as down.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *    ifenslave bond0 eth0
281da177e4SLinus Torvalds  *	will attach eth0 to bond0 as a slave.  eth0 hw mac address will either
291da177e4SLinus Torvalds  *	a: be used as initial mac address
301da177e4SLinus Torvalds  *	b: if a hw mac address already is there, eth0's hw mac address
311da177e4SLinus Torvalds  *	   will then be set from bond0.
321da177e4SLinus Torvalds  *
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <linux/kernel.h>
361da177e4SLinus Torvalds #include <linux/module.h>
371da177e4SLinus Torvalds #include <linux/types.h>
381da177e4SLinus Torvalds #include <linux/fcntl.h>
39b6459415SJakub Kicinski #include <linux/filter.h>
401da177e4SLinus Torvalds #include <linux/interrupt.h>
411da177e4SLinus Torvalds #include <linux/ptrace.h>
421da177e4SLinus Torvalds #include <linux/ioport.h>
431da177e4SLinus Torvalds #include <linux/in.h>
44169a3e66SJay Vosburgh #include <net/ip.h>
451da177e4SLinus Torvalds #include <linux/ip.h>
46df98be06SMatteo Croce #include <linux/icmp.h>
47df98be06SMatteo Croce #include <linux/icmpv6.h>
48169a3e66SJay Vosburgh #include <linux/tcp.h>
49169a3e66SJay Vosburgh #include <linux/udp.h>
501da177e4SLinus Torvalds #include <linux/slab.h>
511da177e4SLinus Torvalds #include <linux/string.h>
521da177e4SLinus Torvalds #include <linux/init.h>
531da177e4SLinus Torvalds #include <linux/timer.h>
541da177e4SLinus Torvalds #include <linux/socket.h>
551da177e4SLinus Torvalds #include <linux/ctype.h>
561da177e4SLinus Torvalds #include <linux/inet.h>
571da177e4SLinus Torvalds #include <linux/bitops.h>
583d632c3fSStephen Hemminger #include <linux/io.h>
591da177e4SLinus Torvalds #include <asm/dma.h>
603d632c3fSStephen Hemminger #include <linux/uaccess.h>
611da177e4SLinus Torvalds #include <linux/errno.h>
621da177e4SLinus Torvalds #include <linux/netdevice.h>
631da177e4SLinus Torvalds #include <linux/inetdevice.h>
64a816c7c7SJay Vosburgh #include <linux/igmp.h>
651da177e4SLinus Torvalds #include <linux/etherdevice.h>
661da177e4SLinus Torvalds #include <linux/skbuff.h>
671da177e4SLinus Torvalds #include <net/sock.h>
681da177e4SLinus Torvalds #include <linux/rtnetlink.h>
691da177e4SLinus Torvalds #include <linux/smp.h>
701da177e4SLinus Torvalds #include <linux/if_ether.h>
711da177e4SLinus Torvalds #include <net/arp.h>
721da177e4SLinus Torvalds #include <linux/mii.h>
731da177e4SLinus Torvalds #include <linux/ethtool.h>
741da177e4SLinus Torvalds #include <linux/if_vlan.h>
751da177e4SLinus Torvalds #include <linux/if_bonding.h>
7694dd016aSHangbin Liu #include <linux/phy.h>
77b63bb739SDavid Sterba #include <linux/jiffies.h>
78e843fa50SNeil Horman #include <linux/preempt.h>
79c3ade5caSJay Vosburgh #include <net/route.h>
80457c4cbcSEric W. Biederman #include <net/net_namespace.h>
81ec87fd3bSEric W. Biederman #include <net/netns/generic.h>
825ee31c68SEric Dumazet #include <net/pkt_sched.h>
83278b2083Snikolay@redhat.com #include <linux/rculist.h>
841bd758ebSJiri Pirko #include <net/flow_dissector.h>
8518cb261aSJarod Wilson #include <net/xfrm.h>
861ef8019bSDavid S. Miller #include <net/bonding.h>
871ef8019bSDavid S. Miller #include <net/bond_3ad.h>
881ef8019bSDavid S. Miller #include <net/bond_alb.h>
8989df6a81STariq Toukan #if IS_ENABLED(CONFIG_TLS_DEVICE)
9089df6a81STariq Toukan #include <net/tls.h>
9189df6a81STariq Toukan #endif
924e24be01SHangbin Liu #include <net/ip6_route.h>
9392272ec4SJakub Kicinski #include <net/xdp.h>
941da177e4SLinus Torvalds 
9573b5a6f2SMatan Barak #include "bonding_priv.h"
9673b5a6f2SMatan Barak 
971da177e4SLinus Torvalds /*---------------------------- Module parameters ----------------------------*/
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* monitor all links that often (in milliseconds). <=0 disables monitoring */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds static int max_bonds	= BOND_DEFAULT_MAX_BONDS;
102bb1d9123SAndy Gospodarek static int tx_queues	= BOND_DEFAULT_TX_QUEUES;
103ad246c99SBen Hutchings static int num_peer_notif = 1;
104b98d9c66SNikolay Aleksandrov static int miimon;
1053d632c3fSStephen Hemminger static int updelay;
1063d632c3fSStephen Hemminger static int downdelay;
1071da177e4SLinus Torvalds static int use_carrier	= 1;
1083d632c3fSStephen Hemminger static char *mode;
1093d632c3fSStephen Hemminger static char *primary;
110a549952aSJiri Pirko static char *primary_reselect;
1113d632c3fSStephen Hemminger static char *lacp_rate;
112655f8919Sstephen hemminger static int min_links;
1133d632c3fSStephen Hemminger static char *ad_select;
1143d632c3fSStephen Hemminger static char *xmit_hash_policy;
1157bdb04edSNikolay Aleksandrov static int arp_interval;
1163d632c3fSStephen Hemminger static char *arp_ip_target[BOND_MAX_ARP_TARGETS];
1173d632c3fSStephen Hemminger static char *arp_validate;
1188599b52eSVeaceslav Falico static char *arp_all_targets;
1193d632c3fSStephen Hemminger static char *fail_over_mac;
120b07ea07bSdingtianhong static int all_slaves_active;
121d2991f75SStephen Hemminger static struct bond_params bonding_defaults;
122c2952c31SFlavio Leitner static int resend_igmp = BOND_DEFAULT_RESEND_IGMP;
12373958329SNikolay Aleksandrov static int packets_per_slave = 1;
1243a7129e5Sdingtianhong static int lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds module_param(max_bonds, int, 0);
1271da177e4SLinus Torvalds MODULE_PARM_DESC(max_bonds, "Max number of bonded devices");
128bb1d9123SAndy Gospodarek module_param(tx_queues, int, 0);
129bb1d9123SAndy Gospodarek MODULE_PARM_DESC(tx_queues, "Max number of transmit queues (default = 16)");
130ad246c99SBen Hutchings module_param_named(num_grat_arp, num_peer_notif, int, 0644);
13190e62474SAndy Gospodarek MODULE_PARM_DESC(num_grat_arp, "Number of peer notifications to send on "
13290e62474SAndy Gospodarek 			       "failover event (alias of num_unsol_na)");
133ad246c99SBen Hutchings module_param_named(num_unsol_na, num_peer_notif, int, 0644);
13490e62474SAndy Gospodarek MODULE_PARM_DESC(num_unsol_na, "Number of peer notifications to send on "
13590e62474SAndy Gospodarek 			       "failover event (alias of num_grat_arp)");
1361da177e4SLinus Torvalds module_param(miimon, int, 0);
1371da177e4SLinus Torvalds MODULE_PARM_DESC(miimon, "Link check interval in milliseconds");
1381da177e4SLinus Torvalds module_param(updelay, int, 0);
1391da177e4SLinus Torvalds MODULE_PARM_DESC(updelay, "Delay before considering link up, in milliseconds");
1401da177e4SLinus Torvalds module_param(downdelay, int, 0);
1412ac47660SMitch Williams MODULE_PARM_DESC(downdelay, "Delay before considering link down, "
1422ac47660SMitch Williams 			    "in milliseconds");
1431da177e4SLinus Torvalds module_param(use_carrier, int, 0);
1442ac47660SMitch Williams MODULE_PARM_DESC(use_carrier, "Use netif_carrier_ok (vs MII ioctls) in miimon; "
145b3c898e2SDebabrata Banerjee 			      "0 for off, 1 for on (default)");
1461da177e4SLinus Torvalds module_param(mode, charp, 0);
14790e62474SAndy Gospodarek MODULE_PARM_DESC(mode, "Mode of operation; 0 for balance-rr, "
1482ac47660SMitch Williams 		       "1 for active-backup, 2 for balance-xor, "
1492ac47660SMitch Williams 		       "3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, "
1502ac47660SMitch Williams 		       "6 for balance-alb");
1511da177e4SLinus Torvalds module_param(primary, charp, 0);
1521da177e4SLinus Torvalds MODULE_PARM_DESC(primary, "Primary network device to use");
153a549952aSJiri Pirko module_param(primary_reselect, charp, 0);
154a549952aSJiri Pirko MODULE_PARM_DESC(primary_reselect, "Reselect primary slave "
155a549952aSJiri Pirko 				   "once it comes up; "
156a549952aSJiri Pirko 				   "0 for always (default), "
157a549952aSJiri Pirko 				   "1 for only if speed of primary is "
158a549952aSJiri Pirko 				   "better, "
159a549952aSJiri Pirko 				   "2 for only on active slave "
160a549952aSJiri Pirko 				   "failure");
1611da177e4SLinus Torvalds module_param(lacp_rate, charp, 0);
16290e62474SAndy Gospodarek MODULE_PARM_DESC(lacp_rate, "LACPDU tx rate to request from 802.3ad partner; "
16390e62474SAndy Gospodarek 			    "0 for slow, 1 for fast");
164fd989c83SJay Vosburgh module_param(ad_select, charp, 0);
1650d039f33SZhu Yanjun MODULE_PARM_DESC(ad_select, "802.3ad aggregation selection logic; "
16690e62474SAndy Gospodarek 			    "0 for stable (default), 1 for bandwidth, "
16790e62474SAndy Gospodarek 			    "2 for count");
168655f8919Sstephen hemminger module_param(min_links, int, 0);
169655f8919Sstephen hemminger MODULE_PARM_DESC(min_links, "Minimum number of available links before turning on carrier");
170655f8919Sstephen hemminger 
171169a3e66SJay Vosburgh module_param(xmit_hash_policy, charp, 0);
172e79c1055SDebabrata Banerjee MODULE_PARM_DESC(xmit_hash_policy, "balance-alb, balance-tlb, balance-xor, 802.3ad hashing method; "
17390e62474SAndy Gospodarek 				   "0 for layer 2 (default), 1 for layer 3+4, "
17432819dc1SNikolay Aleksandrov 				   "2 for layer 2+3, 3 for encap layer 2+3, "
1757b8fc010SJarod Wilson 				   "4 for encap layer 3+4, 5 for vlan+srcmac");
1761da177e4SLinus Torvalds module_param(arp_interval, int, 0);
1771da177e4SLinus Torvalds MODULE_PARM_DESC(arp_interval, "arp interval in milliseconds");
1781da177e4SLinus Torvalds module_param_array(arp_ip_target, charp, NULL, 0);
1791da177e4SLinus Torvalds MODULE_PARM_DESC(arp_ip_target, "arp targets in n.n.n.n form");
180f5b2b966SJay Vosburgh module_param(arp_validate, charp, 0);
18190e62474SAndy Gospodarek MODULE_PARM_DESC(arp_validate, "validate src/dst of ARP probes; "
18290e62474SAndy Gospodarek 			       "0 for none (default), 1 for active, "
18390e62474SAndy Gospodarek 			       "2 for backup, 3 for all");
1848599b52eSVeaceslav Falico module_param(arp_all_targets, charp, 0);
1858599b52eSVeaceslav Falico MODULE_PARM_DESC(arp_all_targets, "fail on any/all arp targets timeout; 0 for any (default), 1 for all");
1863915c1e8SJay Vosburgh module_param(fail_over_mac, charp, 0);
18790e62474SAndy Gospodarek MODULE_PARM_DESC(fail_over_mac, "For active-backup, do not set all slaves to "
18890e62474SAndy Gospodarek 				"the same MAC; 0 for none (default), "
18990e62474SAndy Gospodarek 				"1 for active, 2 for follow");
190ebd8e497SAndy Gospodarek module_param(all_slaves_active, int, 0);
191ebd8e497SAndy Gospodarek MODULE_PARM_DESC(all_slaves_active, "Keep all frames received on an interface "
19290e62474SAndy Gospodarek 				     "by setting active flag for all slaves; "
193ebd8e497SAndy Gospodarek 				     "0 for never (default), 1 for always.");
194c2952c31SFlavio Leitner module_param(resend_igmp, int, 0);
19590e62474SAndy Gospodarek MODULE_PARM_DESC(resend_igmp, "Number of IGMP membership reports to send on "
19690e62474SAndy Gospodarek 			      "link failure");
19773958329SNikolay Aleksandrov module_param(packets_per_slave, int, 0);
19873958329SNikolay Aleksandrov MODULE_PARM_DESC(packets_per_slave, "Packets to send per slave in balance-rr "
19973958329SNikolay Aleksandrov 				    "mode; 0 for a random slave, 1 packet per "
20073958329SNikolay Aleksandrov 				    "slave (default), >1 packets per slave.");
2013a7129e5Sdingtianhong module_param(lp_interval, uint, 0);
2023a7129e5Sdingtianhong MODULE_PARM_DESC(lp_interval, "The number of seconds between instances where "
2033a7129e5Sdingtianhong 			      "the bonding driver sends learning packets to "
2043a7129e5Sdingtianhong 			      "each slaves peer switch. The default is 1.");
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds /*----------------------------- Global variables ----------------------------*/
2071da177e4SLinus Torvalds 
208e843fa50SNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
209fb4fa76aSNeil Horman atomic_t netpoll_block_tx = ATOMIC_INIT(0);
210e843fa50SNeil Horman #endif
211e843fa50SNeil Horman 
212c7d03a00SAlexey Dobriyan unsigned int bond_net_id __read_mostly;
2131da177e4SLinus Torvalds 
21458deb77cSMatteo Croce static const struct flow_dissector_key flow_keys_bonding_keys[] = {
21558deb77cSMatteo Croce 	{
21658deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_CONTROL,
21758deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, control),
21858deb77cSMatteo Croce 	},
21958deb77cSMatteo Croce 	{
22058deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_BASIC,
22158deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, basic),
22258deb77cSMatteo Croce 	},
22358deb77cSMatteo Croce 	{
22458deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
22558deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, addrs.v4addrs),
22658deb77cSMatteo Croce 	},
22758deb77cSMatteo Croce 	{
22858deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
22958deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, addrs.v6addrs),
23058deb77cSMatteo Croce 	},
23158deb77cSMatteo Croce 	{
23258deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_TIPC,
23358deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, addrs.tipckey),
23458deb77cSMatteo Croce 	},
23558deb77cSMatteo Croce 	{
23658deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_PORTS,
23758deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, ports),
23858deb77cSMatteo Croce 	},
23958deb77cSMatteo Croce 	{
24058deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_ICMP,
24158deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, icmp),
24258deb77cSMatteo Croce 	},
24358deb77cSMatteo Croce 	{
24458deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_VLAN,
24558deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, vlan),
24658deb77cSMatteo Croce 	},
24758deb77cSMatteo Croce 	{
24858deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
24958deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, tags),
25058deb77cSMatteo Croce 	},
25158deb77cSMatteo Croce 	{
25258deb77cSMatteo Croce 		.key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
25358deb77cSMatteo Croce 		.offset = offsetof(struct flow_keys, keyid),
25458deb77cSMatteo Croce 	},
25558deb77cSMatteo Croce };
25658deb77cSMatteo Croce 
25758deb77cSMatteo Croce static struct flow_dissector flow_keys_bonding __read_mostly;
25858deb77cSMatteo Croce 
2591da177e4SLinus Torvalds /*-------------------------- Forward declarations ---------------------------*/
2601da177e4SLinus Torvalds 
261181470fcSStephen Hemminger static int bond_init(struct net_device *bond_dev);
262c67dfb29SEric W. Biederman static void bond_uninit(struct net_device *bond_dev);
263bc1f4470Sstephen hemminger static void bond_get_stats(struct net_device *bond_dev,
2645f0c5f73SAndy Gospodarek 			   struct rtnl_link_stats64 *stats);
265ee637714SMahesh Bandewar static void bond_slave_arr_handler(struct work_struct *work);
26621a75f09SJay Vosburgh static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
26721a75f09SJay Vosburgh 				  int mod);
268d4859d74SMahesh Bandewar static void bond_netdev_notify_work(struct work_struct *work);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /*---------------------------- General routines -----------------------------*/
2711da177e4SLinus Torvalds 
bond_mode_name(int mode)272bd33acc3SAmerigo Wang const char *bond_mode_name(int mode)
2731da177e4SLinus Torvalds {
27477afc92bSHolger Eitzenberger 	static const char *names[] = {
27577afc92bSHolger Eitzenberger 		[BOND_MODE_ROUNDROBIN] = "load balancing (round-robin)",
27677afc92bSHolger Eitzenberger 		[BOND_MODE_ACTIVEBACKUP] = "fault-tolerance (active-backup)",
27777afc92bSHolger Eitzenberger 		[BOND_MODE_XOR] = "load balancing (xor)",
27877afc92bSHolger Eitzenberger 		[BOND_MODE_BROADCAST] = "fault-tolerance (broadcast)",
27977afc92bSHolger Eitzenberger 		[BOND_MODE_8023AD] = "IEEE 802.3ad Dynamic link aggregation",
28077afc92bSHolger Eitzenberger 		[BOND_MODE_TLB] = "transmit load balancing",
28177afc92bSHolger Eitzenberger 		[BOND_MODE_ALB] = "adaptive load balancing",
28277afc92bSHolger Eitzenberger 	};
28377afc92bSHolger Eitzenberger 
284f5280948SWang Sheng-Hui 	if (mode < BOND_MODE_ROUNDROBIN || mode > BOND_MODE_ALB)
2851da177e4SLinus Torvalds 		return "unknown";
28677afc92bSHolger Eitzenberger 
28777afc92bSHolger Eitzenberger 	return names[mode];
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds /**
2911da177e4SLinus Torvalds  * bond_dev_queue_xmit - Prepare skb for xmit.
2921da177e4SLinus Torvalds  *
2931da177e4SLinus Torvalds  * @bond: bond device that got this skb for tx.
2941da177e4SLinus Torvalds  * @skb: hw accel VLAN tagged skb to transmit
2951da177e4SLinus Torvalds  * @slave_dev: slave that is supposed to xmit this skbuff
2961da177e4SLinus Torvalds  */
bond_dev_queue_xmit(struct bonding * bond,struct sk_buff * skb,struct net_device * slave_dev)297ae46f184SEric Dumazet netdev_tx_t bond_dev_queue_xmit(struct bonding *bond, struct sk_buff *skb,
2983d632c3fSStephen Hemminger 			struct net_device *slave_dev)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	skb->dev = slave_dev;
301374eeb5aSNeil Horman 
3025ee31c68SEric Dumazet 	BUILD_BUG_ON(sizeof(skb->queue_mapping) !=
303df4ab5b3SJiri Pirko 		     sizeof(qdisc_skb_cb(skb)->slave_dev_queue_mapping));
304ae35c6f7STonghao Zhang 	skb_set_queue_mapping(skb, qdisc_skb_cb(skb)->slave_dev_queue_mapping);
305374eeb5aSNeil Horman 
306e15c3c22SAmerigo Wang 	if (unlikely(netpoll_tx_running(bond->dev)))
307ae46f184SEric Dumazet 		return bond_netpoll_send_skb(bond_get_slave_by_dev(bond, slave_dev), skb);
308ae46f184SEric Dumazet 
309ae46f184SEric Dumazet 	return dev_queue_xmit(skb);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
bond_sk_check(struct bonding * bond)31228581b9cSTariq Toukan static bool bond_sk_check(struct bonding *bond)
313007feb87STariq Toukan {
314007feb87STariq Toukan 	switch (BOND_MODE(bond)) {
315007feb87STariq Toukan 	case BOND_MODE_8023AD:
316007feb87STariq Toukan 	case BOND_MODE_XOR:
317007feb87STariq Toukan 		if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34)
318007feb87STariq Toukan 			return true;
319007feb87STariq Toukan 		fallthrough;
320007feb87STariq Toukan 	default:
321007feb87STariq Toukan 		return false;
322007feb87STariq Toukan 	}
323007feb87STariq Toukan }
324007feb87STariq Toukan 
bond_xdp_check(struct bonding * bond)3259e2ee5c7SJussi Maki static bool bond_xdp_check(struct bonding *bond)
3269e2ee5c7SJussi Maki {
3279e2ee5c7SJussi Maki 	switch (BOND_MODE(bond)) {
3289e2ee5c7SJussi Maki 	case BOND_MODE_ROUNDROBIN:
3299e2ee5c7SJussi Maki 	case BOND_MODE_ACTIVEBACKUP:
33039a0876dSJussi Maki 		return true;
3319e2ee5c7SJussi Maki 	case BOND_MODE_8023AD:
3329e2ee5c7SJussi Maki 	case BOND_MODE_XOR:
33339a0876dSJussi Maki 		/* vlan+srcmac is not supported with XDP as in most cases the 802.1q
33439a0876dSJussi Maki 		 * payload is not in the packet due to hardware offload.
33539a0876dSJussi Maki 		 */
33639a0876dSJussi Maki 		if (bond->params.xmit_policy != BOND_XMIT_POLICY_VLAN_SRCMAC)
3379e2ee5c7SJussi Maki 			return true;
33839a0876dSJussi Maki 		fallthrough;
3399e2ee5c7SJussi Maki 	default:
3409e2ee5c7SJussi Maki 		return false;
3419e2ee5c7SJussi Maki 	}
3429e2ee5c7SJussi Maki }
3439e2ee5c7SJussi Maki 
34418cb261aSJarod Wilson /*---------------------------------- VLAN -----------------------------------*/
34518cb261aSJarod Wilson 
346547942caSNikolay Aleksandrov /* In the following 2 functions, bond_vlan_rx_add_vid and bond_vlan_rx_kill_vid,
347cc0e4070SJiri Pirko  * We don't protect the slave list iteration with a lock because:
3481da177e4SLinus Torvalds  * a. This operation is performed in IOCTL context,
3491da177e4SLinus Torvalds  * b. The operation is protected by the RTNL semaphore in the 8021q code,
3501da177e4SLinus Torvalds  * c. Holding a lock with BH disabled while directly calling a base driver
3511da177e4SLinus Torvalds  *    entry point is generally a BAD idea.
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  * The design of synchronization/protection for this operation in the 8021q
3541da177e4SLinus Torvalds  * module is good for one or more VLAN devices over a single physical device
3551da177e4SLinus Torvalds  * and cannot be extended for a teaming solution like bonding, so there is a
3561da177e4SLinus Torvalds  * potential race condition here where a net device from the vlan group might
3571da177e4SLinus Torvalds  * be referenced (either by a base driver or the 8021q code) while it is being
3581da177e4SLinus Torvalds  * removed from the system. However, it turns out we're not making matters
3591da177e4SLinus Torvalds  * worse, and if it works for regular VLAN usage it will work here too.
3601da177e4SLinus Torvalds */
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /**
3631da177e4SLinus Torvalds  * bond_vlan_rx_add_vid - Propagates adding an id to slaves
3641da177e4SLinus Torvalds  * @bond_dev: bonding net device that got called
36545a1553bSLee Jones  * @proto: network protocol ID
3661da177e4SLinus Torvalds  * @vid: vlan id being added
3671da177e4SLinus Torvalds  */
bond_vlan_rx_add_vid(struct net_device * bond_dev,__be16 proto,u16 vid)36880d5c368SPatrick McHardy static int bond_vlan_rx_add_vid(struct net_device *bond_dev,
36980d5c368SPatrick McHardy 				__be16 proto, u16 vid)
3701da177e4SLinus Torvalds {
371454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
37281f23b13SVeaceslav Falico 	struct slave *slave, *rollback_slave;
3739caff1e7SVeaceslav Falico 	struct list_head *iter;
374dec1e90eSnikolay@redhat.com 	int res;
3751da177e4SLinus Torvalds 
3769caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
37780d5c368SPatrick McHardy 		res = vlan_vid_add(slave->dev, proto, vid);
37887002b03SJiri Pirko 		if (res)
37987002b03SJiri Pirko 			goto unwind;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
3828e586137SJiri Pirko 	return 0;
38387002b03SJiri Pirko 
38487002b03SJiri Pirko unwind:
38581f23b13SVeaceslav Falico 	/* unwind to the slave that failed */
3869caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, rollback_slave, iter) {
38781f23b13SVeaceslav Falico 		if (rollback_slave == slave)
38881f23b13SVeaceslav Falico 			break;
38981f23b13SVeaceslav Falico 
39081f23b13SVeaceslav Falico 		vlan_vid_del(rollback_slave->dev, proto, vid);
39181f23b13SVeaceslav Falico 	}
39287002b03SJiri Pirko 
39387002b03SJiri Pirko 	return res;
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds /**
3971da177e4SLinus Torvalds  * bond_vlan_rx_kill_vid - Propagates deleting an id to slaves
3981da177e4SLinus Torvalds  * @bond_dev: bonding net device that got called
39945a1553bSLee Jones  * @proto: network protocol ID
4001da177e4SLinus Torvalds  * @vid: vlan id being removed
4011da177e4SLinus Torvalds  */
bond_vlan_rx_kill_vid(struct net_device * bond_dev,__be16 proto,u16 vid)40280d5c368SPatrick McHardy static int bond_vlan_rx_kill_vid(struct net_device *bond_dev,
40380d5c368SPatrick McHardy 				 __be16 proto, u16 vid)
4041da177e4SLinus Torvalds {
405454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
4069caff1e7SVeaceslav Falico 	struct list_head *iter;
4071da177e4SLinus Torvalds 	struct slave *slave;
4081da177e4SLinus Torvalds 
4099caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter)
41080d5c368SPatrick McHardy 		vlan_vid_del(slave->dev, proto, vid);
4111da177e4SLinus Torvalds 
412e868b0c9SVeaceslav Falico 	if (bond_is_lb(bond))
413e868b0c9SVeaceslav Falico 		bond_alb_clear_vlan(bond, vid);
4148e586137SJiri Pirko 
4158e586137SJiri Pirko 	return 0;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
41818cb261aSJarod Wilson /*---------------------------------- XFRM -----------------------------------*/
41918cb261aSJarod Wilson 
42018cb261aSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
42118cb261aSJarod Wilson /**
42218cb261aSJarod Wilson  * bond_ipsec_add_sa - program device with a security association
42318cb261aSJarod Wilson  * @xs: pointer to transformer state struct
4247681a4f5SLeon Romanovsky  * @extack: extack point to fill failure reason
42518cb261aSJarod Wilson  **/
bond_ipsec_add_sa(struct xfrm_state * xs,struct netlink_ext_ack * extack)4267681a4f5SLeon Romanovsky static int bond_ipsec_add_sa(struct xfrm_state *xs,
4277681a4f5SLeon Romanovsky 			     struct netlink_ext_ack *extack)
42818cb261aSJarod Wilson {
42918cb261aSJarod Wilson 	struct net_device *bond_dev = xs->xso.dev;
43019792758SJianbo Liu 	struct net_device *real_dev;
4316b598069SJianbo Liu 	netdevice_tracker tracker;
4329a560550STaehee Yoo 	struct bond_ipsec *ipsec;
4335cd24cbeSJarod Wilson 	struct bonding *bond;
4345cd24cbeSJarod Wilson 	struct slave *slave;
435b648eba4STaehee Yoo 	int err;
43618cb261aSJarod Wilson 
4375cd24cbeSJarod Wilson 	if (!bond_dev)
4385cd24cbeSJarod Wilson 		return -EINVAL;
4395cd24cbeSJarod Wilson 
440b648eba4STaehee Yoo 	rcu_read_lock();
4415cd24cbeSJarod Wilson 	bond = netdev_priv(bond_dev);
442f548a476SJarod Wilson 	slave = rcu_dereference(bond->curr_active_slave);
4436b598069SJianbo Liu 	real_dev = slave ? slave->dev : NULL;
4446b598069SJianbo Liu 	netdev_hold(real_dev, &tracker, GFP_ATOMIC);
445105cd17aSTaehee Yoo 	rcu_read_unlock();
4466b598069SJianbo Liu 	if (!real_dev) {
4476b598069SJianbo Liu 		err = -ENODEV;
4486b598069SJianbo Liu 		goto out;
449105cd17aSTaehee Yoo 	}
450105cd17aSTaehee Yoo 
45119792758SJianbo Liu 	if (!real_dev->xfrmdev_ops ||
45219792758SJianbo Liu 	    !real_dev->xfrmdev_ops->xdo_dev_state_add ||
45319792758SJianbo Liu 	    netif_is_bond_master(real_dev)) {
4543fe57986SLeon Romanovsky 		NL_SET_ERR_MSG_MOD(extack, "Slave does not support ipsec offload");
4556b598069SJianbo Liu 		err = -EINVAL;
4566b598069SJianbo Liu 		goto out;
45718cb261aSJarod Wilson 	}
45818cb261aSJarod Wilson 
4596b598069SJianbo Liu 	ipsec = kmalloc(sizeof(*ipsec), GFP_KERNEL);
4609a560550STaehee Yoo 	if (!ipsec) {
4616b598069SJianbo Liu 		err = -ENOMEM;
4626b598069SJianbo Liu 		goto out;
4639a560550STaehee Yoo 	}
4649a560550STaehee Yoo 
46519792758SJianbo Liu 	xs->xso.real_dev = real_dev;
46619792758SJianbo Liu 	err = real_dev->xfrmdev_ops->xdo_dev_state_add(xs, extack);
4679a560550STaehee Yoo 	if (!err) {
4689a560550STaehee Yoo 		ipsec->xs = xs;
4699a560550STaehee Yoo 		INIT_LIST_HEAD(&ipsec->list);
4706b598069SJianbo Liu 		mutex_lock(&bond->ipsec_lock);
4719a560550STaehee Yoo 		list_add(&ipsec->list, &bond->ipsec_list);
4726b598069SJianbo Liu 		mutex_unlock(&bond->ipsec_lock);
4739a560550STaehee Yoo 	} else {
4749a560550STaehee Yoo 		kfree(ipsec);
4759a560550STaehee Yoo 	}
4766b598069SJianbo Liu out:
4776b598069SJianbo Liu 	netdev_put(real_dev, &tracker);
478b648eba4STaehee Yoo 	return err;
47918cb261aSJarod Wilson }
48018cb261aSJarod Wilson 
bond_ipsec_add_sa_all(struct bonding * bond)4819a560550STaehee Yoo static void bond_ipsec_add_sa_all(struct bonding *bond)
4829a560550STaehee Yoo {
4839a560550STaehee Yoo 	struct net_device *bond_dev = bond->dev;
48419792758SJianbo Liu 	struct net_device *real_dev;
4859a560550STaehee Yoo 	struct bond_ipsec *ipsec;
4869a560550STaehee Yoo 	struct slave *slave;
4879a560550STaehee Yoo 
4886b598069SJianbo Liu 	slave = rtnl_dereference(bond->curr_active_slave);
4896b598069SJianbo Liu 	real_dev = slave ? slave->dev : NULL;
4906b598069SJianbo Liu 	if (!real_dev)
4916b598069SJianbo Liu 		return;
4929a560550STaehee Yoo 
4936b598069SJianbo Liu 	mutex_lock(&bond->ipsec_lock);
49419792758SJianbo Liu 	if (!real_dev->xfrmdev_ops ||
49519792758SJianbo Liu 	    !real_dev->xfrmdev_ops->xdo_dev_state_add ||
49619792758SJianbo Liu 	    netif_is_bond_master(real_dev)) {
4979a560550STaehee Yoo 		if (!list_empty(&bond->ipsec_list))
49819792758SJianbo Liu 			slave_warn(bond_dev, real_dev,
4999a560550STaehee Yoo 				   "%s: no slave xdo_dev_state_add\n",
5009a560550STaehee Yoo 				   __func__);
5019a560550STaehee Yoo 		goto out;
5029a560550STaehee Yoo 	}
5039a560550STaehee Yoo 
5049a560550STaehee Yoo 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
5056b598069SJianbo Liu 		/* If new state is added before ipsec_lock acquired */
5066b598069SJianbo Liu 		if (ipsec->xs->xso.real_dev == real_dev)
5076b598069SJianbo Liu 			continue;
5086b598069SJianbo Liu 
50919792758SJianbo Liu 		ipsec->xs->xso.real_dev = real_dev;
51019792758SJianbo Liu 		if (real_dev->xfrmdev_ops->xdo_dev_state_add(ipsec->xs, NULL)) {
51119792758SJianbo Liu 			slave_warn(bond_dev, real_dev, "%s: failed to add SA\n", __func__);
5129a560550STaehee Yoo 			ipsec->xs->xso.real_dev = NULL;
5139a560550STaehee Yoo 		}
5149a560550STaehee Yoo 	}
5159a560550STaehee Yoo out:
5166b598069SJianbo Liu 	mutex_unlock(&bond->ipsec_lock);
5179a560550STaehee Yoo }
5189a560550STaehee Yoo 
51918cb261aSJarod Wilson /**
52018cb261aSJarod Wilson  * bond_ipsec_del_sa - clear out this specific SA
52118cb261aSJarod Wilson  * @xs: pointer to transformer state struct
52218cb261aSJarod Wilson  **/
bond_ipsec_del_sa(struct xfrm_state * xs)52318cb261aSJarod Wilson static void bond_ipsec_del_sa(struct xfrm_state *xs)
52418cb261aSJarod Wilson {
52518cb261aSJarod Wilson 	struct net_device *bond_dev = xs->xso.dev;
52619792758SJianbo Liu 	struct net_device *real_dev;
5276b598069SJianbo Liu 	netdevice_tracker tracker;
5289a560550STaehee Yoo 	struct bond_ipsec *ipsec;
5295cd24cbeSJarod Wilson 	struct bonding *bond;
5305cd24cbeSJarod Wilson 	struct slave *slave;
5315cd24cbeSJarod Wilson 
5325cd24cbeSJarod Wilson 	if (!bond_dev)
5335cd24cbeSJarod Wilson 		return;
5345cd24cbeSJarod Wilson 
535a22c39b8STaehee Yoo 	rcu_read_lock();
5365cd24cbeSJarod Wilson 	bond = netdev_priv(bond_dev);
537f548a476SJarod Wilson 	slave = rcu_dereference(bond->curr_active_slave);
5386b598069SJianbo Liu 	real_dev = slave ? slave->dev : NULL;
5396b598069SJianbo Liu 	netdev_hold(real_dev, &tracker, GFP_ATOMIC);
5406b598069SJianbo Liu 	rcu_read_unlock();
54118cb261aSJarod Wilson 
54218cb261aSJarod Wilson 	if (!slave)
543a22c39b8STaehee Yoo 		goto out;
54418cb261aSJarod Wilson 
5459a560550STaehee Yoo 	if (!xs->xso.real_dev)
5469a560550STaehee Yoo 		goto out;
5479a560550STaehee Yoo 
54819792758SJianbo Liu 	WARN_ON(xs->xso.real_dev != real_dev);
54918cb261aSJarod Wilson 
55019792758SJianbo Liu 	if (!real_dev->xfrmdev_ops ||
55119792758SJianbo Liu 	    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
55219792758SJianbo Liu 	    netif_is_bond_master(real_dev)) {
55319792758SJianbo Liu 		slave_warn(bond_dev, real_dev, "%s: no slave xdo_dev_state_delete\n", __func__);
554a22c39b8STaehee Yoo 		goto out;
55518cb261aSJarod Wilson 	}
55618cb261aSJarod Wilson 
55719792758SJianbo Liu 	real_dev->xfrmdev_ops->xdo_dev_state_delete(xs);
558a22c39b8STaehee Yoo out:
5596b598069SJianbo Liu 	netdev_put(real_dev, &tracker);
5606b598069SJianbo Liu 	mutex_lock(&bond->ipsec_lock);
5619a560550STaehee Yoo 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
5629a560550STaehee Yoo 		if (ipsec->xs == xs) {
5639a560550STaehee Yoo 			list_del(&ipsec->list);
5649a560550STaehee Yoo 			kfree(ipsec);
5659a560550STaehee Yoo 			break;
5669a560550STaehee Yoo 		}
5679a560550STaehee Yoo 	}
5686b598069SJianbo Liu 	mutex_unlock(&bond->ipsec_lock);
5699a560550STaehee Yoo }
5709a560550STaehee Yoo 
bond_ipsec_del_sa_all(struct bonding * bond)5719a560550STaehee Yoo static void bond_ipsec_del_sa_all(struct bonding *bond)
5729a560550STaehee Yoo {
5739a560550STaehee Yoo 	struct net_device *bond_dev = bond->dev;
57419792758SJianbo Liu 	struct net_device *real_dev;
5759a560550STaehee Yoo 	struct bond_ipsec *ipsec;
5769a560550STaehee Yoo 	struct slave *slave;
5779a560550STaehee Yoo 
5786b598069SJianbo Liu 	slave = rtnl_dereference(bond->curr_active_slave);
5796b598069SJianbo Liu 	real_dev = slave ? slave->dev : NULL;
5806b598069SJianbo Liu 	if (!real_dev)
5819a560550STaehee Yoo 		return;
5829a560550STaehee Yoo 
5836b598069SJianbo Liu 	mutex_lock(&bond->ipsec_lock);
5849a560550STaehee Yoo 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
5859a560550STaehee Yoo 		if (!ipsec->xs->xso.real_dev)
5869a560550STaehee Yoo 			continue;
5879a560550STaehee Yoo 
58819792758SJianbo Liu 		if (!real_dev->xfrmdev_ops ||
58919792758SJianbo Liu 		    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
59019792758SJianbo Liu 		    netif_is_bond_master(real_dev)) {
59119792758SJianbo Liu 			slave_warn(bond_dev, real_dev,
5929a560550STaehee Yoo 				   "%s: no slave xdo_dev_state_delete\n",
5939a560550STaehee Yoo 				   __func__);
5949a560550STaehee Yoo 		} else {
59519792758SJianbo Liu 			real_dev->xfrmdev_ops->xdo_dev_state_delete(ipsec->xs);
59619792758SJianbo Liu 			if (real_dev->xfrmdev_ops->xdo_dev_state_free)
59719792758SJianbo Liu 				real_dev->xfrmdev_ops->xdo_dev_state_free(ipsec->xs);
5989a560550STaehee Yoo 		}
5999a560550STaehee Yoo 	}
6006b598069SJianbo Liu 	mutex_unlock(&bond->ipsec_lock);
60118cb261aSJarod Wilson }
60218cb261aSJarod Wilson 
bond_ipsec_free_sa(struct xfrm_state * xs)603124a688eSJianbo Liu static void bond_ipsec_free_sa(struct xfrm_state *xs)
604124a688eSJianbo Liu {
605124a688eSJianbo Liu 	struct net_device *bond_dev = xs->xso.dev;
606124a688eSJianbo Liu 	struct net_device *real_dev;
607124a688eSJianbo Liu 	netdevice_tracker tracker;
608124a688eSJianbo Liu 	struct bonding *bond;
609124a688eSJianbo Liu 	struct slave *slave;
610124a688eSJianbo Liu 
611124a688eSJianbo Liu 	if (!bond_dev)
612124a688eSJianbo Liu 		return;
613124a688eSJianbo Liu 
614124a688eSJianbo Liu 	rcu_read_lock();
615124a688eSJianbo Liu 	bond = netdev_priv(bond_dev);
616124a688eSJianbo Liu 	slave = rcu_dereference(bond->curr_active_slave);
617124a688eSJianbo Liu 	real_dev = slave ? slave->dev : NULL;
618124a688eSJianbo Liu 	netdev_hold(real_dev, &tracker, GFP_ATOMIC);
619124a688eSJianbo Liu 	rcu_read_unlock();
620124a688eSJianbo Liu 
621124a688eSJianbo Liu 	if (!slave)
622124a688eSJianbo Liu 		goto out;
623124a688eSJianbo Liu 
624124a688eSJianbo Liu 	if (!xs->xso.real_dev)
625124a688eSJianbo Liu 		goto out;
626124a688eSJianbo Liu 
627124a688eSJianbo Liu 	WARN_ON(xs->xso.real_dev != real_dev);
628124a688eSJianbo Liu 
629124a688eSJianbo Liu 	if (real_dev && real_dev->xfrmdev_ops &&
630124a688eSJianbo Liu 	    real_dev->xfrmdev_ops->xdo_dev_state_free)
631124a688eSJianbo Liu 		real_dev->xfrmdev_ops->xdo_dev_state_free(xs);
632124a688eSJianbo Liu out:
633124a688eSJianbo Liu 	netdev_put(real_dev, &tracker);
634124a688eSJianbo Liu }
635124a688eSJianbo Liu 
63618cb261aSJarod Wilson /**
63718cb261aSJarod Wilson  * bond_ipsec_offload_ok - can this packet use the xfrm hw offload
63818cb261aSJarod Wilson  * @skb: current data packet
63918cb261aSJarod Wilson  * @xs: pointer to transformer state struct
64018cb261aSJarod Wilson  **/
bond_ipsec_offload_ok(struct sk_buff * skb,struct xfrm_state * xs)64118cb261aSJarod Wilson static bool bond_ipsec_offload_ok(struct sk_buff *skb, struct xfrm_state *xs)
64218cb261aSJarod Wilson {
64318cb261aSJarod Wilson 	struct net_device *bond_dev = xs->xso.dev;
6449a560550STaehee Yoo 	struct net_device *real_dev;
6459a560550STaehee Yoo 	struct slave *curr_active;
6469a560550STaehee Yoo 	struct bonding *bond;
647f2b3d38dSNikolay Aleksandrov 	bool ok = false;
6489a560550STaehee Yoo 
6499a560550STaehee Yoo 	bond = netdev_priv(bond_dev);
650955b785eSTaehee Yoo 	rcu_read_lock();
6519a560550STaehee Yoo 	curr_active = rcu_dereference(bond->curr_active_slave);
6520707260aSNikolay Aleksandrov 	if (!curr_active)
6530707260aSNikolay Aleksandrov 		goto out;
6549a560550STaehee Yoo 	real_dev = curr_active->dev;
65518cb261aSJarod Wilson 
656f2b3d38dSNikolay Aleksandrov 	if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)
657955b785eSTaehee Yoo 		goto out;
658a3b658cfSJarod Wilson 
659f2b3d38dSNikolay Aleksandrov 	if (!xs->xso.real_dev)
660955b785eSTaehee Yoo 		goto out;
6619a560550STaehee Yoo 
6629a560550STaehee Yoo 	if (!real_dev->xfrmdev_ops ||
6639a560550STaehee Yoo 	    !real_dev->xfrmdev_ops->xdo_dev_offload_ok ||
664f2b3d38dSNikolay Aleksandrov 	    netif_is_bond_master(real_dev))
665955b785eSTaehee Yoo 		goto out;
66618cb261aSJarod Wilson 
667f2b3d38dSNikolay Aleksandrov 	ok = real_dev->xfrmdev_ops->xdo_dev_offload_ok(skb, xs);
668955b785eSTaehee Yoo out:
669955b785eSTaehee Yoo 	rcu_read_unlock();
670f2b3d38dSNikolay Aleksandrov 	return ok;
67118cb261aSJarod Wilson }
67218cb261aSJarod Wilson 
67318cb261aSJarod Wilson static const struct xfrmdev_ops bond_xfrmdev_ops = {
67418cb261aSJarod Wilson 	.xdo_dev_state_add = bond_ipsec_add_sa,
67518cb261aSJarod Wilson 	.xdo_dev_state_delete = bond_ipsec_del_sa,
676124a688eSJianbo Liu 	.xdo_dev_state_free = bond_ipsec_free_sa,
67718cb261aSJarod Wilson 	.xdo_dev_offload_ok = bond_ipsec_offload_ok,
67818cb261aSJarod Wilson };
67918cb261aSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
68018cb261aSJarod Wilson 
6811da177e4SLinus Torvalds /*------------------------------- Link status -------------------------------*/
6821da177e4SLinus Torvalds 
683547942caSNikolay Aleksandrov /* Set the carrier state for the master according to the state of its
684ff59c456SJay Vosburgh  * slaves.  If any slaves are up, the master is up.  In 802.3ad mode,
685ff59c456SJay Vosburgh  * do special 802.3ad magic.
686ff59c456SJay Vosburgh  *
687ff59c456SJay Vosburgh  * Returns zero if carrier state does not change, nonzero if it does.
688ff59c456SJay Vosburgh  */
bond_set_carrier(struct bonding * bond)6892477bc9aSJonathan Toppins int bond_set_carrier(struct bonding *bond)
690ff59c456SJay Vosburgh {
6919caff1e7SVeaceslav Falico 	struct list_head *iter;
692ff59c456SJay Vosburgh 	struct slave *slave;
693ff59c456SJay Vosburgh 
6940965a1f3SVeaceslav Falico 	if (!bond_has_slaves(bond))
695ff59c456SJay Vosburgh 		goto down;
696ff59c456SJay Vosburgh 
69701844098SVeaceslav Falico 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
698ff59c456SJay Vosburgh 		return bond_3ad_set_carrier(bond);
699ff59c456SJay Vosburgh 
7009caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
701ff59c456SJay Vosburgh 		if (slave->link == BOND_LINK_UP) {
702ff59c456SJay Vosburgh 			if (!netif_carrier_ok(bond->dev)) {
703ff59c456SJay Vosburgh 				netif_carrier_on(bond->dev);
704ff59c456SJay Vosburgh 				return 1;
705ff59c456SJay Vosburgh 			}
706ff59c456SJay Vosburgh 			return 0;
707ff59c456SJay Vosburgh 		}
708ff59c456SJay Vosburgh 	}
709ff59c456SJay Vosburgh 
710ff59c456SJay Vosburgh down:
711ff59c456SJay Vosburgh 	if (netif_carrier_ok(bond->dev)) {
712ff59c456SJay Vosburgh 		netif_carrier_off(bond->dev);
713ff59c456SJay Vosburgh 		return 1;
714ff59c456SJay Vosburgh 	}
715ff59c456SJay Vosburgh 	return 0;
716ff59c456SJay Vosburgh }
717ff59c456SJay Vosburgh 
718547942caSNikolay Aleksandrov /* Get link speed and duplex from the slave's base driver
7191da177e4SLinus Torvalds  * using ethtool. If for some reason the call fails or the
72098f41f69SWeiping Pan  * values are invalid, set speed and duplex to -1,
721c4adfc82SMahesh Bandewar  * and return. Return 1 if speed or duplex settings are
722c4adfc82SMahesh Bandewar  * UNKNOWN; 0 otherwise.
7231da177e4SLinus Torvalds  */
bond_update_speed_duplex(struct slave * slave)724c4adfc82SMahesh Bandewar static int bond_update_speed_duplex(struct slave *slave)
7251da177e4SLinus Torvalds {
7261da177e4SLinus Torvalds 	struct net_device *slave_dev = slave->dev;
7279856909cSDavid Decotigny 	struct ethtool_link_ksettings ecmd;
72861a44b9cSMatthew Wilcox 	int res;
7291da177e4SLinus Torvalds 
730589665f5SDan Carpenter 	slave->speed = SPEED_UNKNOWN;
731589665f5SDan Carpenter 	slave->duplex = DUPLEX_UNKNOWN;
7321da177e4SLinus Torvalds 
7339856909cSDavid Decotigny 	res = __ethtool_get_link_ksettings(slave_dev, &ecmd);
73461a44b9cSMatthew Wilcox 	if (res < 0)
735c4adfc82SMahesh Bandewar 		return 1;
7369856909cSDavid Decotigny 	if (ecmd.base.speed == 0 || ecmd.base.speed == ((__u32)-1))
737c4adfc82SMahesh Bandewar 		return 1;
7389856909cSDavid Decotigny 	switch (ecmd.base.duplex) {
7391da177e4SLinus Torvalds 	case DUPLEX_FULL:
7401da177e4SLinus Torvalds 	case DUPLEX_HALF:
7411da177e4SLinus Torvalds 		break;
7421da177e4SLinus Torvalds 	default:
743c4adfc82SMahesh Bandewar 		return 1;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
7469856909cSDavid Decotigny 	slave->speed = ecmd.base.speed;
7479856909cSDavid Decotigny 	slave->duplex = ecmd.base.duplex;
7481da177e4SLinus Torvalds 
749c4adfc82SMahesh Bandewar 	return 0;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
bond_slave_link_status(s8 link)75207699f9aSsfeldma@cumulusnetworks.com const char *bond_slave_link_status(s8 link)
75307699f9aSsfeldma@cumulusnetworks.com {
75407699f9aSsfeldma@cumulusnetworks.com 	switch (link) {
75507699f9aSsfeldma@cumulusnetworks.com 	case BOND_LINK_UP:
75607699f9aSsfeldma@cumulusnetworks.com 		return "up";
75707699f9aSsfeldma@cumulusnetworks.com 	case BOND_LINK_FAIL:
75807699f9aSsfeldma@cumulusnetworks.com 		return "going down";
75907699f9aSsfeldma@cumulusnetworks.com 	case BOND_LINK_DOWN:
76007699f9aSsfeldma@cumulusnetworks.com 		return "down";
76107699f9aSsfeldma@cumulusnetworks.com 	case BOND_LINK_BACK:
76207699f9aSsfeldma@cumulusnetworks.com 		return "going back";
76307699f9aSsfeldma@cumulusnetworks.com 	default:
76407699f9aSsfeldma@cumulusnetworks.com 		return "unknown";
76507699f9aSsfeldma@cumulusnetworks.com 	}
76607699f9aSsfeldma@cumulusnetworks.com }
76707699f9aSsfeldma@cumulusnetworks.com 
768547942caSNikolay Aleksandrov /* if <dev> supports MII link status reporting, check its link status.
7691da177e4SLinus Torvalds  *
7701da177e4SLinus Torvalds  * We either do MII/ETHTOOL ioctls, or check netif_carrier_ok(),
7713d632c3fSStephen Hemminger  * depending upon the setting of the use_carrier parameter.
7721da177e4SLinus Torvalds  *
7731da177e4SLinus Torvalds  * Return either BMSR_LSTATUS, meaning that the link is up (or we
7741da177e4SLinus Torvalds  * can't tell and just pretend it is), or 0, meaning that the link is
7751da177e4SLinus Torvalds  * down.
7761da177e4SLinus Torvalds  *
7771da177e4SLinus Torvalds  * If reporting is non-zero, instead of faking link up, return -1 if
7781da177e4SLinus Torvalds  * both ETHTOOL and MII ioctls fail (meaning the device does not
7791da177e4SLinus Torvalds  * support them).  If use_carrier is set, return whatever it says.
7801da177e4SLinus Torvalds  * It'd be nice if there was a good way to tell if a driver supports
7811da177e4SLinus Torvalds  * netif_carrier, but there really isn't.
7821da177e4SLinus Torvalds  */
bond_check_dev_link(struct bonding * bond,struct net_device * slave_dev,int reporting)7833d632c3fSStephen Hemminger static int bond_check_dev_link(struct bonding *bond,
7843d632c3fSStephen Hemminger 			       struct net_device *slave_dev, int reporting)
7851da177e4SLinus Torvalds {
786eb7cc59aSStephen Hemminger 	const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
787d9d52832SJiri Bohac 	int (*ioctl)(struct net_device *, struct ifreq *, int);
7881da177e4SLinus Torvalds 	struct ifreq ifr;
7891da177e4SLinus Torvalds 	struct mii_ioctl_data *mii;
7901da177e4SLinus Torvalds 
7916c988853SPetri Gynther 	if (!reporting && !netif_running(slave_dev))
7926c988853SPetri Gynther 		return 0;
7936c988853SPetri Gynther 
794eb7cc59aSStephen Hemminger 	if (bond->params.use_carrier)
795b3c898e2SDebabrata Banerjee 		return netif_carrier_ok(slave_dev) ? BMSR_LSTATUS : 0;
7961da177e4SLinus Torvalds 
79729112f4eSJiri Pirko 	/* Try to get link status using Ethtool first. */
798c772dde3SBen Hutchings 	if (slave_dev->ethtool_ops->get_link)
799c772dde3SBen Hutchings 		return slave_dev->ethtool_ops->get_link(slave_dev) ?
800c772dde3SBen Hutchings 			BMSR_LSTATUS : 0;
80129112f4eSJiri Pirko 
8023d632c3fSStephen Hemminger 	/* Ethtool can't be used, fallback to MII ioctls. */
803a7605370SArnd Bergmann 	ioctl = slave_ops->ndo_eth_ioctl;
8041da177e4SLinus Torvalds 	if (ioctl) {
805547942caSNikolay Aleksandrov 		/* TODO: set pointer to correct ioctl on a per team member
806547942caSNikolay Aleksandrov 		 *       bases to make this more efficient. that is, once
807547942caSNikolay Aleksandrov 		 *       we determine the correct ioctl, we will always
808547942caSNikolay Aleksandrov 		 *       call it and not the others for that team
809547942caSNikolay Aleksandrov 		 *       member.
810547942caSNikolay Aleksandrov 		 */
8111da177e4SLinus Torvalds 
812547942caSNikolay Aleksandrov 		/* We cannot assume that SIOCGMIIPHY will also read a
8131da177e4SLinus Torvalds 		 * register; not all network drivers (e.g., e100)
8141da177e4SLinus Torvalds 		 * support that.
8151da177e4SLinus Torvalds 		 */
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 		/* Yes, the mii is overlaid on the ifreq.ifr_ifru */
81843902070SKees Cook 		strscpy_pad(ifr.ifr_name, slave_dev->name, IFNAMSIZ);
8191da177e4SLinus Torvalds 		mii = if_mii(&ifr);
8204ad41c1eSAl Viro 		if (ioctl(slave_dev, &ifr, SIOCGMIIPHY) == 0) {
8211da177e4SLinus Torvalds 			mii->reg_num = MII_BMSR;
8224ad41c1eSAl Viro 			if (ioctl(slave_dev, &ifr, SIOCGMIIREG) == 0)
8233d632c3fSStephen Hemminger 				return mii->val_out & BMSR_LSTATUS;
8241da177e4SLinus Torvalds 		}
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 
827a7605370SArnd Bergmann 	/* If reporting, report that either there's no ndo_eth_ioctl,
82861a44b9cSMatthew Wilcox 	 * or both SIOCGMIIREG and get_link failed (meaning that we
8291da177e4SLinus Torvalds 	 * cannot report link status).  If not reporting, pretend
8301da177e4SLinus Torvalds 	 * we're ok.
8311da177e4SLinus Torvalds 	 */
8323d632c3fSStephen Hemminger 	return reporting ? -1 : BMSR_LSTATUS;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds /*----------------------------- Multicast list ------------------------------*/
8361da177e4SLinus Torvalds 
837547942caSNikolay Aleksandrov /* Push the promiscuity flag down to appropriate slaves */
bond_set_promiscuity(struct bonding * bond,int inc)8387e1a1ac1SWang Chen static int bond_set_promiscuity(struct bonding *bond, int inc)
8391da177e4SLinus Torvalds {
8409caff1e7SVeaceslav Falico 	struct list_head *iter;
8417e1a1ac1SWang Chen 	int err = 0;
8429caff1e7SVeaceslav Falico 
843ec0865a9SVeaceslav Falico 	if (bond_uses_primary(bond)) {
84414056e79SVeaceslav Falico 		struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
8454740d638SEric Dumazet 
8464740d638SEric Dumazet 		if (curr_active)
8474740d638SEric Dumazet 			err = dev_set_promiscuity(curr_active->dev, inc);
8481da177e4SLinus Torvalds 	} else {
8491da177e4SLinus Torvalds 		struct slave *slave;
850dec1e90eSnikolay@redhat.com 
8519caff1e7SVeaceslav Falico 		bond_for_each_slave(bond, slave, iter) {
8527e1a1ac1SWang Chen 			err = dev_set_promiscuity(slave->dev, inc);
8537e1a1ac1SWang Chen 			if (err)
8547e1a1ac1SWang Chen 				return err;
8551da177e4SLinus Torvalds 		}
8561da177e4SLinus Torvalds 	}
8577e1a1ac1SWang Chen 	return err;
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
860547942caSNikolay Aleksandrov /* Push the allmulti flag down to all slaves */
bond_set_allmulti(struct bonding * bond,int inc)8617e1a1ac1SWang Chen static int bond_set_allmulti(struct bonding *bond, int inc)
8621da177e4SLinus Torvalds {
8639caff1e7SVeaceslav Falico 	struct list_head *iter;
8647e1a1ac1SWang Chen 	int err = 0;
8659caff1e7SVeaceslav Falico 
866ec0865a9SVeaceslav Falico 	if (bond_uses_primary(bond)) {
86714056e79SVeaceslav Falico 		struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
8684740d638SEric Dumazet 
8694740d638SEric Dumazet 		if (curr_active)
8704740d638SEric Dumazet 			err = dev_set_allmulti(curr_active->dev, inc);
8711da177e4SLinus Torvalds 	} else {
8721da177e4SLinus Torvalds 		struct slave *slave;
873dec1e90eSnikolay@redhat.com 
8749caff1e7SVeaceslav Falico 		bond_for_each_slave(bond, slave, iter) {
8757e1a1ac1SWang Chen 			err = dev_set_allmulti(slave->dev, inc);
8767e1a1ac1SWang Chen 			if (err)
8777e1a1ac1SWang Chen 				return err;
8781da177e4SLinus Torvalds 		}
8791da177e4SLinus Torvalds 	}
8807e1a1ac1SWang Chen 	return err;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
883547942caSNikolay Aleksandrov /* Retrieve the list of registered multicast addresses for the bonding
884a816c7c7SJay Vosburgh  * device and retransmit an IGMP JOIN request to the current active
885a816c7c7SJay Vosburgh  * slave.
886a816c7c7SJay Vosburgh  */
bond_resend_igmp_join_requests_delayed(struct work_struct * work)887379b7383Sstephen hemminger static void bond_resend_igmp_join_requests_delayed(struct work_struct *work)
8885a37e8caSFlavio Leitner {
8895a37e8caSFlavio Leitner 	struct bonding *bond = container_of(work, struct bonding,
8905a37e8caSFlavio Leitner 					    mcast_work.work);
891ad999eeeSVeaceslav Falico 
892f2369109Sdingtianhong 	if (!rtnl_trylock()) {
893f2369109Sdingtianhong 		queue_delayed_work(bond->wq, &bond->mcast_work, 1);
894f2369109Sdingtianhong 		return;
895f2369109Sdingtianhong 	}
896f2369109Sdingtianhong 	call_netdevice_notifiers(NETDEV_RESEND_IGMP, bond->dev);
897f2369109Sdingtianhong 
898f2369109Sdingtianhong 	if (bond->igmp_retrans > 1) {
899f2369109Sdingtianhong 		bond->igmp_retrans--;
900f2369109Sdingtianhong 		queue_delayed_work(bond->wq, &bond->mcast_work, HZ/5);
901f2369109Sdingtianhong 	}
902f2369109Sdingtianhong 	rtnl_unlock();
903a816c7c7SJay Vosburgh }
904a816c7c7SJay Vosburgh 
905547942caSNikolay Aleksandrov /* Flush bond's hardware addresses from slave */
bond_hw_addr_flush(struct net_device * bond_dev,struct net_device * slave_dev)906303d1cbfSJay Vosburgh static void bond_hw_addr_flush(struct net_device *bond_dev,
9073d632c3fSStephen Hemminger 			       struct net_device *slave_dev)
9081da177e4SLinus Torvalds {
909454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
9101da177e4SLinus Torvalds 
911303d1cbfSJay Vosburgh 	dev_uc_unsync(slave_dev, bond_dev);
912303d1cbfSJay Vosburgh 	dev_mc_unsync(slave_dev, bond_dev);
9131da177e4SLinus Torvalds 
9141d9a143eSBenjamin Poirier 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
9151d9a143eSBenjamin Poirier 		dev_mc_del(slave_dev, lacpdu_mcast_addr);
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds /*--------------------------- Active slave change ---------------------------*/
9191da177e4SLinus Torvalds 
920303d1cbfSJay Vosburgh /* Update the hardware address list and promisc/allmulti for the new and
921ec0865a9SVeaceslav Falico  * old active slaves (if any).  Modes that are not using primary keep all
922ec0865a9SVeaceslav Falico  * slaves up date at all times; only the modes that use primary need to call
923303d1cbfSJay Vosburgh  * this function to swap these settings during a failover.
9241da177e4SLinus Torvalds  */
bond_hw_addr_swap(struct bonding * bond,struct slave * new_active,struct slave * old_active)925303d1cbfSJay Vosburgh static void bond_hw_addr_swap(struct bonding *bond, struct slave *new_active,
9263d632c3fSStephen Hemminger 			      struct slave *old_active)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	if (old_active) {
9293d632c3fSStephen Hemminger 		if (bond->dev->flags & IFF_PROMISC)
9301da177e4SLinus Torvalds 			dev_set_promiscuity(old_active->dev, -1);
9311da177e4SLinus Torvalds 
9323d632c3fSStephen Hemminger 		if (bond->dev->flags & IFF_ALLMULTI)
9331da177e4SLinus Torvalds 			dev_set_allmulti(old_active->dev, -1);
9341da177e4SLinus Torvalds 
93586247abaSBenjamin Poirier 		if (bond->dev->flags & IFF_UP)
936303d1cbfSJay Vosburgh 			bond_hw_addr_flush(bond->dev, old_active->dev);
9379ea6b70fSHangbin Liu 
9389ea6b70fSHangbin Liu 		bond_slave_ns_maddrs_add(bond, old_active);
9391da177e4SLinus Torvalds 	}
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	if (new_active) {
9427e1a1ac1SWang Chen 		/* FIXME: Signal errors upstream. */
9433d632c3fSStephen Hemminger 		if (bond->dev->flags & IFF_PROMISC)
9441da177e4SLinus Torvalds 			dev_set_promiscuity(new_active->dev, 1);
9451da177e4SLinus Torvalds 
9463d632c3fSStephen Hemminger 		if (bond->dev->flags & IFF_ALLMULTI)
9471da177e4SLinus Torvalds 			dev_set_allmulti(new_active->dev, 1);
9481da177e4SLinus Torvalds 
94986247abaSBenjamin Poirier 		if (bond->dev->flags & IFF_UP) {
950d632ce98Snikolay@redhat.com 			netif_addr_lock_bh(bond->dev);
951303d1cbfSJay Vosburgh 			dev_uc_sync(new_active->dev, bond->dev);
952303d1cbfSJay Vosburgh 			dev_mc_sync(new_active->dev, bond->dev);
953d632ce98Snikolay@redhat.com 			netif_addr_unlock_bh(bond->dev);
9541da177e4SLinus Torvalds 		}
9559ea6b70fSHangbin Liu 
9569ea6b70fSHangbin Liu 		bond_slave_ns_maddrs_del(bond, new_active);
9571da177e4SLinus Torvalds 	}
95886247abaSBenjamin Poirier }
9591da177e4SLinus Torvalds 
960ae0d6750Snikolay@redhat.com /**
961ae0d6750Snikolay@redhat.com  * bond_set_dev_addr - clone slave's address to bond
962ae0d6750Snikolay@redhat.com  * @bond_dev: bond net device
963ae0d6750Snikolay@redhat.com  * @slave_dev: slave net device
964ae0d6750Snikolay@redhat.com  *
965ae0d6750Snikolay@redhat.com  * Should be called with RTNL held.
966ae0d6750Snikolay@redhat.com  */
bond_set_dev_addr(struct net_device * bond_dev,struct net_device * slave_dev)967b9245914SPetr Machata static int bond_set_dev_addr(struct net_device *bond_dev,
968ae0d6750Snikolay@redhat.com 			     struct net_device *slave_dev)
969ae0d6750Snikolay@redhat.com {
9701caf40deSPetr Machata 	int err;
9711caf40deSPetr Machata 
972e2a7420dSJarod Wilson 	slave_dbg(bond_dev, slave_dev, "bond_dev=%p slave_dev=%p slave_dev->addr_len=%d\n",
973e2a7420dSJarod Wilson 		  bond_dev, slave_dev, slave_dev->addr_len);
9741caf40deSPetr Machata 	err = dev_pre_changeaddr_notify(bond_dev, slave_dev->dev_addr, NULL);
9751caf40deSPetr Machata 	if (err)
9761caf40deSPetr Machata 		return err;
9771caf40deSPetr Machata 
9786f238100SJakub Kicinski 	__dev_addr_set(bond_dev, slave_dev->dev_addr, slave_dev->addr_len);
979ae0d6750Snikolay@redhat.com 	bond_dev->addr_assign_type = NET_ADDR_STOLEN;
980ae0d6750Snikolay@redhat.com 	call_netdevice_notifiers(NETDEV_CHANGEADDR, bond_dev);
981b9245914SPetr Machata 	return 0;
982ae0d6750Snikolay@redhat.com }
983ae0d6750Snikolay@redhat.com 
bond_get_old_active(struct bonding * bond,struct slave * new_active)984a951bc1eSdingtianhong static struct slave *bond_get_old_active(struct bonding *bond,
985a951bc1eSdingtianhong 					 struct slave *new_active)
986a951bc1eSdingtianhong {
987a951bc1eSdingtianhong 	struct slave *slave;
988a951bc1eSdingtianhong 	struct list_head *iter;
989a951bc1eSdingtianhong 
990a951bc1eSdingtianhong 	bond_for_each_slave(bond, slave, iter) {
991a951bc1eSdingtianhong 		if (slave == new_active)
992a951bc1eSdingtianhong 			continue;
993a951bc1eSdingtianhong 
994a951bc1eSdingtianhong 		if (ether_addr_equal(bond->dev->dev_addr, slave->dev->dev_addr))
995a951bc1eSdingtianhong 			return slave;
996a951bc1eSdingtianhong 	}
997a951bc1eSdingtianhong 
998a951bc1eSdingtianhong 	return NULL;
999a951bc1eSdingtianhong }
1000a951bc1eSdingtianhong 
1001547942caSNikolay Aleksandrov /* bond_do_fail_over_mac
10023915c1e8SJay Vosburgh  *
10033915c1e8SJay Vosburgh  * Perform special MAC address swapping for fail_over_mac settings
10043915c1e8SJay Vosburgh  *
10051c72cfdcSNikolay Aleksandrov  * Called with RTNL
10063915c1e8SJay Vosburgh  */
bond_do_fail_over_mac(struct bonding * bond,struct slave * new_active,struct slave * old_active)10073915c1e8SJay Vosburgh static void bond_do_fail_over_mac(struct bonding *bond,
10083915c1e8SJay Vosburgh 				  struct slave *new_active,
10093915c1e8SJay Vosburgh 				  struct slave *old_active)
10103915c1e8SJay Vosburgh {
1011faeeb317SJarod Wilson 	u8 tmp_mac[MAX_ADDR_LEN];
1012faeeb317SJarod Wilson 	struct sockaddr_storage ss;
10133915c1e8SJay Vosburgh 	int rv;
10143915c1e8SJay Vosburgh 
10153915c1e8SJay Vosburgh 	switch (bond->params.fail_over_mac) {
10163915c1e8SJay Vosburgh 	case BOND_FOM_ACTIVE:
1017b9245914SPetr Machata 		if (new_active) {
1018b9245914SPetr Machata 			rv = bond_set_dev_addr(bond->dev, new_active->dev);
1019b9245914SPetr Machata 			if (rv)
1020e2a7420dSJarod Wilson 				slave_err(bond->dev, new_active->dev, "Error %d setting bond MAC from slave\n",
1021e2a7420dSJarod Wilson 					  -rv);
1022b9245914SPetr Machata 		}
10233915c1e8SJay Vosburgh 		break;
10243915c1e8SJay Vosburgh 	case BOND_FOM_FOLLOW:
1025547942caSNikolay Aleksandrov 		/* if new_active && old_active, swap them
10263915c1e8SJay Vosburgh 		 * if just old_active, do nothing (going to no active slave)
10273915c1e8SJay Vosburgh 		 * if just new_active, set new_active to bond's MAC
10283915c1e8SJay Vosburgh 		 */
10293915c1e8SJay Vosburgh 		if (!new_active)
10303915c1e8SJay Vosburgh 			return;
10313915c1e8SJay Vosburgh 
1032a951bc1eSdingtianhong 		if (!old_active)
1033a951bc1eSdingtianhong 			old_active = bond_get_old_active(bond, new_active);
1034a951bc1eSdingtianhong 
10353915c1e8SJay Vosburgh 		if (old_active) {
1036faeeb317SJarod Wilson 			bond_hw_addr_copy(tmp_mac, new_active->dev->dev_addr,
1037faeeb317SJarod Wilson 					  new_active->dev->addr_len);
1038faeeb317SJarod Wilson 			bond_hw_addr_copy(ss.__data,
1039faeeb317SJarod Wilson 					  old_active->dev->dev_addr,
1040faeeb317SJarod Wilson 					  old_active->dev->addr_len);
1041faeeb317SJarod Wilson 			ss.ss_family = new_active->dev->type;
10423915c1e8SJay Vosburgh 		} else {
1043faeeb317SJarod Wilson 			bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
1044faeeb317SJarod Wilson 					  bond->dev->addr_len);
1045faeeb317SJarod Wilson 			ss.ss_family = bond->dev->type;
10463915c1e8SJay Vosburgh 		}
10473915c1e8SJay Vosburgh 
1048faeeb317SJarod Wilson 		rv = dev_set_mac_address(new_active->dev,
10493a37a963SPetr Machata 					 (struct sockaddr *)&ss, NULL);
10503915c1e8SJay Vosburgh 		if (rv) {
1051e2a7420dSJarod Wilson 			slave_err(bond->dev, new_active->dev, "Error %d setting MAC of new active slave\n",
1052e2a7420dSJarod Wilson 				  -rv);
10533915c1e8SJay Vosburgh 			goto out;
10543915c1e8SJay Vosburgh 		}
10553915c1e8SJay Vosburgh 
10563915c1e8SJay Vosburgh 		if (!old_active)
10573915c1e8SJay Vosburgh 			goto out;
10583915c1e8SJay Vosburgh 
1059faeeb317SJarod Wilson 		bond_hw_addr_copy(ss.__data, tmp_mac,
1060faeeb317SJarod Wilson 				  new_active->dev->addr_len);
1061faeeb317SJarod Wilson 		ss.ss_family = old_active->dev->type;
10623915c1e8SJay Vosburgh 
1063faeeb317SJarod Wilson 		rv = dev_set_mac_address(old_active->dev,
10643a37a963SPetr Machata 					 (struct sockaddr *)&ss, NULL);
10653915c1e8SJay Vosburgh 		if (rv)
1066e2a7420dSJarod Wilson 			slave_err(bond->dev, old_active->dev, "Error %d setting MAC of old active slave\n",
1067e2a7420dSJarod Wilson 				  -rv);
10683915c1e8SJay Vosburgh out:
10693915c1e8SJay Vosburgh 		break;
10703915c1e8SJay Vosburgh 	default:
107176444f50SVeaceslav Falico 		netdev_err(bond->dev, "bond_do_fail_over_mac impossible: bad policy %d\n",
107276444f50SVeaceslav Falico 			   bond->params.fail_over_mac);
10733915c1e8SJay Vosburgh 		break;
10743915c1e8SJay Vosburgh 	}
10753915c1e8SJay Vosburgh 
10763915c1e8SJay Vosburgh }
10773915c1e8SJay Vosburgh 
10780a2ff7ccSHangbin Liu /**
10790a2ff7ccSHangbin Liu  * bond_choose_primary_or_current - select the primary or high priority slave
10800a2ff7ccSHangbin Liu  * @bond: our bonding struct
10810a2ff7ccSHangbin Liu  *
10820a2ff7ccSHangbin Liu  * - Check if there is a primary link. If the primary link was set and is up,
10830a2ff7ccSHangbin Liu  *   go on and do link reselection.
10840a2ff7ccSHangbin Liu  *
10850a2ff7ccSHangbin Liu  * - If primary link is not set or down, find the highest priority link.
10860a2ff7ccSHangbin Liu  *   If the highest priority link is not current slave, set it as primary
10870a2ff7ccSHangbin Liu  *   link and do link reselection.
10880a2ff7ccSHangbin Liu  */
bond_choose_primary_or_current(struct bonding * bond)1089b5a983f3SMazhar Rana static struct slave *bond_choose_primary_or_current(struct bonding *bond)
1090a549952aSJiri Pirko {
1091059b47e8SNikolay Aleksandrov 	struct slave *prim = rtnl_dereference(bond->primary_slave);
10921c72cfdcSNikolay Aleksandrov 	struct slave *curr = rtnl_dereference(bond->curr_active_slave);
10930a2ff7ccSHangbin Liu 	struct slave *slave, *hprio = NULL;
10940a2ff7ccSHangbin Liu 	struct list_head *iter;
1095a549952aSJiri Pirko 
1096b5a983f3SMazhar Rana 	if (!prim || prim->link != BOND_LINK_UP) {
10970a2ff7ccSHangbin Liu 		bond_for_each_slave(bond, slave, iter) {
10980a2ff7ccSHangbin Liu 			if (slave->link == BOND_LINK_UP) {
10990a2ff7ccSHangbin Liu 				hprio = hprio ?: slave;
11000a2ff7ccSHangbin Liu 				if (slave->prio > hprio->prio)
11010a2ff7ccSHangbin Liu 					hprio = slave;
11020a2ff7ccSHangbin Liu 			}
11030a2ff7ccSHangbin Liu 		}
11040a2ff7ccSHangbin Liu 
11050a2ff7ccSHangbin Liu 		if (hprio && hprio != curr) {
11060a2ff7ccSHangbin Liu 			prim = hprio;
11070a2ff7ccSHangbin Liu 			goto link_reselect;
11080a2ff7ccSHangbin Liu 		}
11090a2ff7ccSHangbin Liu 
1110b5a983f3SMazhar Rana 		if (!curr || curr->link != BOND_LINK_UP)
1111b5a983f3SMazhar Rana 			return NULL;
1112b5a983f3SMazhar Rana 		return curr;
1113b5a983f3SMazhar Rana 	}
1114b5a983f3SMazhar Rana 
1115a549952aSJiri Pirko 	if (bond->force_primary) {
1116a549952aSJiri Pirko 		bond->force_primary = false;
1117b5a983f3SMazhar Rana 		return prim;
1118a549952aSJiri Pirko 	}
1119b5a983f3SMazhar Rana 
11200a2ff7ccSHangbin Liu link_reselect:
1121b5a983f3SMazhar Rana 	if (!curr || curr->link != BOND_LINK_UP)
1122b5a983f3SMazhar Rana 		return prim;
1123b5a983f3SMazhar Rana 
1124b5a983f3SMazhar Rana 	/* At this point, prim and curr are both up */
1125b5a983f3SMazhar Rana 	switch (bond->params.primary_reselect) {
1126b5a983f3SMazhar Rana 	case BOND_PRI_RESELECT_ALWAYS:
1127b5a983f3SMazhar Rana 		return prim;
1128b5a983f3SMazhar Rana 	case BOND_PRI_RESELECT_BETTER:
1129b5a983f3SMazhar Rana 		if (prim->speed < curr->speed)
1130b5a983f3SMazhar Rana 			return curr;
1131b5a983f3SMazhar Rana 		if (prim->speed == curr->speed && prim->duplex <= curr->duplex)
1132b5a983f3SMazhar Rana 			return curr;
1133b5a983f3SMazhar Rana 		return prim;
1134b5a983f3SMazhar Rana 	case BOND_PRI_RESELECT_FAILURE:
1135b5a983f3SMazhar Rana 		return curr;
1136b5a983f3SMazhar Rana 	default:
1137b5a983f3SMazhar Rana 		netdev_err(bond->dev, "impossible primary_reselect %d\n",
1138b5a983f3SMazhar Rana 			   bond->params.primary_reselect);
1139b5a983f3SMazhar Rana 		return curr;
1140b5a983f3SMazhar Rana 	}
1141a549952aSJiri Pirko }
11423915c1e8SJay Vosburgh 
11431da177e4SLinus Torvalds /**
1144b5a983f3SMazhar Rana  * bond_find_best_slave - select the best available slave to be the active one
11451da177e4SLinus Torvalds  * @bond: our bonding struct
11461da177e4SLinus Torvalds  */
bond_find_best_slave(struct bonding * bond)11471da177e4SLinus Torvalds static struct slave *bond_find_best_slave(struct bonding *bond)
11481da177e4SLinus Torvalds {
1149b5a983f3SMazhar Rana 	struct slave *slave, *bestslave = NULL;
115077140d29SVeaceslav Falico 	struct list_head *iter;
11511da177e4SLinus Torvalds 	int mintime = bond->params.updelay;
11521da177e4SLinus Torvalds 
1153b5a983f3SMazhar Rana 	slave = bond_choose_primary_or_current(bond);
1154b5a983f3SMazhar Rana 	if (slave)
1155b5a983f3SMazhar Rana 		return slave;
11561da177e4SLinus Torvalds 
115777140d29SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
115877140d29SVeaceslav Falico 		if (slave->link == BOND_LINK_UP)
115977140d29SVeaceslav Falico 			return slave;
1160b6adc610SVeaceslav Falico 		if (slave->link == BOND_LINK_BACK && bond_slave_is_up(slave) &&
116177140d29SVeaceslav Falico 		    slave->delay < mintime) {
116277140d29SVeaceslav Falico 			mintime = slave->delay;
116377140d29SVeaceslav Falico 			bestslave = slave;
11641da177e4SLinus Torvalds 		}
11651da177e4SLinus Torvalds 	}
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	return bestslave;
11681da177e4SLinus Torvalds }
11691da177e4SLinus Torvalds 
1170d9f01090SJohannes Berg /* must be called in RCU critical section or with RTNL held */
bond_should_notify_peers(struct bonding * bond)1171ad246c99SBen Hutchings static bool bond_should_notify_peers(struct bonding *bond)
1172ad246c99SBen Hutchings {
1173d9f01090SJohannes Berg 	struct slave *slave = rcu_dereference_rtnl(bond->curr_active_slave);
1174ad246c99SBen Hutchings 
1175ad246c99SBen Hutchings 	if (!slave || !bond->send_peer_notif ||
117607a4ddecSVincent Bernat 	    bond->send_peer_notif %
117707a4ddecSVincent Bernat 	    max(1, bond->params.peer_notif_delay) != 0 ||
1178b02e3e94SVenkat Venkatsubra 	    !netif_carrier_ok(bond->dev) ||
1179ad246c99SBen Hutchings 	    test_bit(__LINK_STATE_LINKWATCH_PENDING, &slave->dev->state))
1180ad246c99SBen Hutchings 		return false;
1181ad246c99SBen Hutchings 
1182fee32de2SSuresh Kumar 	netdev_dbg(bond->dev, "bond_should_notify_peers: slave %s\n",
1183fee32de2SSuresh Kumar 		   slave ? slave->dev->name : "NULL");
1184fee32de2SSuresh Kumar 
1185ad246c99SBen Hutchings 	return true;
1186ad246c99SBen Hutchings }
1187ad246c99SBen Hutchings 
11881da177e4SLinus Torvalds /**
1189acf61b3dSYang Yingliang  * bond_change_active_slave - change the active slave into the specified one
11901da177e4SLinus Torvalds  * @bond: our bonding struct
119145a1553bSLee Jones  * @new_active: the new slave to make the active one
11921da177e4SLinus Torvalds  *
11931da177e4SLinus Torvalds  * Set the new slave to the bond's settings and unset them on the old
11941da177e4SLinus Torvalds  * curr_active_slave.
11951da177e4SLinus Torvalds  * Setting include flags, mc-list, promiscuity, allmulti, etc.
11961da177e4SLinus Torvalds  *
11971da177e4SLinus Torvalds  * If @new's link state is %BOND_LINK_BACK we'll set it to %BOND_LINK_UP,
11981da177e4SLinus Torvalds  * because it is apparently the best available slave we have, even though its
11991da177e4SLinus Torvalds  * updelay hasn't timed out yet.
12001da177e4SLinus Torvalds  *
12011c72cfdcSNikolay Aleksandrov  * Caller must hold RTNL.
12021da177e4SLinus Torvalds  */
bond_change_active_slave(struct bonding * bond,struct slave * new_active)1203a77b5325SMitch Williams void bond_change_active_slave(struct bonding *bond, struct slave *new_active)
12041da177e4SLinus Torvalds {
12054740d638SEric Dumazet 	struct slave *old_active;
12064740d638SEric Dumazet 
12071c72cfdcSNikolay Aleksandrov 	ASSERT_RTNL();
12081c72cfdcSNikolay Aleksandrov 
12091c72cfdcSNikolay Aleksandrov 	old_active = rtnl_dereference(bond->curr_active_slave);
12101da177e4SLinus Torvalds 
12113d632c3fSStephen Hemminger 	if (old_active == new_active)
12121da177e4SLinus Torvalds 		return;
12131da177e4SLinus Torvalds 
121418cb261aSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
12159a560550STaehee Yoo 	bond_ipsec_del_sa_all(bond);
121618cb261aSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
121718cb261aSJarod Wilson 
12185cd24cbeSJarod Wilson 	if (new_active) {
12198e603460SVeaceslav Falico 		new_active->last_link_up = jiffies;
1220b2220cadSJay Vosburgh 
12211da177e4SLinus Torvalds 		if (new_active->link == BOND_LINK_BACK) {
1222ec0865a9SVeaceslav Falico 			if (bond_uses_primary(bond)) {
1223e2a7420dSJarod Wilson 				slave_info(bond->dev, new_active->dev, "making interface the new active one %d ms earlier\n",
12241da177e4SLinus Torvalds 					   (bond->params.updelay - new_active->delay) * bond->params.miimon);
12251da177e4SLinus Torvalds 			}
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 			new_active->delay = 0;
12285d397061SJiri Pirko 			bond_set_slave_link_state(new_active, BOND_LINK_UP,
12295d397061SJiri Pirko 						  BOND_SLAVE_NOTIFY_NOW);
12301da177e4SLinus Torvalds 
123101844098SVeaceslav Falico 			if (BOND_MODE(bond) == BOND_MODE_8023AD)
12321da177e4SLinus Torvalds 				bond_3ad_handle_link_change(new_active, BOND_LINK_UP);
12331da177e4SLinus Torvalds 
123458402054SHolger Eitzenberger 			if (bond_is_lb(bond))
12351da177e4SLinus Torvalds 				bond_alb_handle_link_change(bond, new_active, BOND_LINK_UP);
12361da177e4SLinus Torvalds 		} else {
123752333512SYufeng Mo 			if (bond_uses_primary(bond))
1238e2a7420dSJarod Wilson 				slave_info(bond->dev, new_active->dev, "making interface the new active one\n");
12391da177e4SLinus Torvalds 		}
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 
1242ec0865a9SVeaceslav Falico 	if (bond_uses_primary(bond))
1243303d1cbfSJay Vosburgh 		bond_hw_addr_swap(bond, new_active, old_active);
12441da177e4SLinus Torvalds 
124558402054SHolger Eitzenberger 	if (bond_is_lb(bond)) {
12461da177e4SLinus Torvalds 		bond_alb_handle_active_change(bond, new_active);
12478f903c70SJay Vosburgh 		if (old_active)
12485e5b0665Sdingtianhong 			bond_set_slave_inactive_flags(old_active,
12495e5b0665Sdingtianhong 						      BOND_SLAVE_NOTIFY_NOW);
12508f903c70SJay Vosburgh 		if (new_active)
12515e5b0665Sdingtianhong 			bond_set_slave_active_flags(new_active,
12525e5b0665Sdingtianhong 						    BOND_SLAVE_NOTIFY_NOW);
12531da177e4SLinus Torvalds 	} else {
1254278b2083Snikolay@redhat.com 		rcu_assign_pointer(bond->curr_active_slave, new_active);
12551da177e4SLinus Torvalds 	}
1256c3ade5caSJay Vosburgh 
125701844098SVeaceslav Falico 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
12583d632c3fSStephen Hemminger 		if (old_active)
12595e5b0665Sdingtianhong 			bond_set_slave_inactive_flags(old_active,
12605e5b0665Sdingtianhong 						      BOND_SLAVE_NOTIFY_NOW);
1261c3ade5caSJay Vosburgh 
1262c3ade5caSJay Vosburgh 		if (new_active) {
1263ad246c99SBen Hutchings 			bool should_notify_peers = false;
1264ad246c99SBen Hutchings 
12655e5b0665Sdingtianhong 			bond_set_slave_active_flags(new_active,
12665e5b0665Sdingtianhong 						    BOND_SLAVE_NOTIFY_NOW);
12672ab82852SMoni Shoua 
1268709f8a45SOr Gerlitz 			if (bond->params.fail_over_mac)
1269709f8a45SOr Gerlitz 				bond_do_fail_over_mac(bond, new_active,
1270709f8a45SOr Gerlitz 						      old_active);
12713915c1e8SJay Vosburgh 
1272ad246c99SBen Hutchings 			if (netif_running(bond->dev)) {
1273ad246c99SBen Hutchings 				bond->send_peer_notif =
127407a4ddecSVincent Bernat 					bond->params.num_peer_notif *
127507a4ddecSVincent Bernat 					max(1, bond->params.peer_notif_delay);
1276ad246c99SBen Hutchings 				should_notify_peers =
1277ad246c99SBen Hutchings 					bond_should_notify_peers(bond);
1278ad246c99SBen Hutchings 			}
1279ad246c99SBen Hutchings 
1280b7bc2a5bSAmerigo Wang 			call_netdevice_notifiers(NETDEV_BONDING_FAILOVER, bond->dev);
128107a4ddecSVincent Bernat 			if (should_notify_peers) {
128207a4ddecSVincent Bernat 				bond->send_peer_notif--;
1283b7bc2a5bSAmerigo Wang 				call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
1284b7bc2a5bSAmerigo Wang 							 bond->dev);
12857893b249SMoni Shoua 			}
1286c3ade5caSJay Vosburgh 		}
128707a4ddecSVincent Bernat 	}
1288a2fd940fSAndy Gospodarek 
12895cd24cbeSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
12909a560550STaehee Yoo 	bond_ipsec_add_sa_all(bond);
12915cd24cbeSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
12925cd24cbeSJarod Wilson 
12935a37e8caSFlavio Leitner 	/* resend IGMP joins since active slave has changed or
129494265cf5SFlavio Leitner 	 * all were sent on curr_active_slave.
129594265cf5SFlavio Leitner 	 * resend only if bond is brought up with the affected
1296547942caSNikolay Aleksandrov 	 * bonding modes and the retransmission is enabled
1297547942caSNikolay Aleksandrov 	 */
129894265cf5SFlavio Leitner 	if (netif_running(bond->dev) && (bond->params.resend_igmp > 0) &&
1299ec0865a9SVeaceslav Falico 	    ((bond_uses_primary(bond) && new_active) ||
130001844098SVeaceslav Falico 	     BOND_MODE(bond) == BOND_MODE_ROUNDROBIN)) {
1301c2952c31SFlavio Leitner 		bond->igmp_retrans = bond->params.resend_igmp;
13024beac029SNikolay Aleksandrov 		queue_delayed_work(bond->wq, &bond->mcast_work, 1);
1303a2fd940fSAndy Gospodarek 	}
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds /**
13071da177e4SLinus Torvalds  * bond_select_active_slave - select a new active slave, if needed
13081da177e4SLinus Torvalds  * @bond: our bonding struct
13091da177e4SLinus Torvalds  *
13103d632c3fSStephen Hemminger  * This functions should be called when one of the following occurs:
13111da177e4SLinus Torvalds  * - The old curr_active_slave has been released or lost its link.
13121da177e4SLinus Torvalds  * - The primary_slave has got its link back.
13131da177e4SLinus Torvalds  * - A slave has got its link back and there's no old curr_active_slave.
13141da177e4SLinus Torvalds  *
13151c72cfdcSNikolay Aleksandrov  * Caller must hold RTNL.
13161da177e4SLinus Torvalds  */
bond_select_active_slave(struct bonding * bond)1317a77b5325SMitch Williams void bond_select_active_slave(struct bonding *bond)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	struct slave *best_slave;
1320ff59c456SJay Vosburgh 	int rv;
13211da177e4SLinus Torvalds 
1322e0974585SNikolay Aleksandrov 	ASSERT_RTNL();
1323e0974585SNikolay Aleksandrov 
13241da177e4SLinus Torvalds 	best_slave = bond_find_best_slave(bond);
13251c72cfdcSNikolay Aleksandrov 	if (best_slave != rtnl_dereference(bond->curr_active_slave)) {
13261da177e4SLinus Torvalds 		bond_change_active_slave(bond, best_slave);
1327ff59c456SJay Vosburgh 		rv = bond_set_carrier(bond);
1328ff59c456SJay Vosburgh 		if (!rv)
1329ff59c456SJay Vosburgh 			return;
1330ff59c456SJay Vosburgh 
1331d66bd905SZhang Shengju 		if (netif_carrier_ok(bond->dev))
1332b8bd72d3SEric Dumazet 			netdev_info(bond->dev, "active interface up!\n");
1333d66bd905SZhang Shengju 		else
133476444f50SVeaceslav Falico 			netdev_info(bond->dev, "now running without any active interface!\n");
1335ff59c456SJay Vosburgh 	}
13361da177e4SLinus Torvalds }
13371da177e4SLinus Torvalds 
1338f6dc31a8SWANG Cong #ifdef CONFIG_NET_POLL_CONTROLLER
slave_enable_netpoll(struct slave * slave)13398a8efa22SAmerigo Wang static inline int slave_enable_netpoll(struct slave *slave)
1340f6dc31a8SWANG Cong {
13418a8efa22SAmerigo Wang 	struct netpoll *np;
13428a8efa22SAmerigo Wang 	int err = 0;
1343f6dc31a8SWANG Cong 
1344a8779ec1SEric W. Biederman 	np = kzalloc(sizeof(*np), GFP_KERNEL);
13458a8efa22SAmerigo Wang 	err = -ENOMEM;
13468a8efa22SAmerigo Wang 	if (!np)
13478a8efa22SAmerigo Wang 		goto out;
13488a8efa22SAmerigo Wang 
1349a8779ec1SEric W. Biederman 	err = __netpoll_setup(np, slave->dev);
13508a8efa22SAmerigo Wang 	if (err) {
13518a8efa22SAmerigo Wang 		kfree(np);
13528a8efa22SAmerigo Wang 		goto out;
1353f6dc31a8SWANG Cong 	}
13548a8efa22SAmerigo Wang 	slave->np = np;
13558a8efa22SAmerigo Wang out:
13568a8efa22SAmerigo Wang 	return err;
13578a8efa22SAmerigo Wang }
slave_disable_netpoll(struct slave * slave)13588a8efa22SAmerigo Wang static inline void slave_disable_netpoll(struct slave *slave)
13598a8efa22SAmerigo Wang {
13608a8efa22SAmerigo Wang 	struct netpoll *np = slave->np;
13618a8efa22SAmerigo Wang 
13628a8efa22SAmerigo Wang 	if (!np)
13638a8efa22SAmerigo Wang 		return;
13648a8efa22SAmerigo Wang 
13658a8efa22SAmerigo Wang 	slave->np = NULL;
1366c9fbd71fSDebabrata Banerjee 
1367c9fbd71fSDebabrata Banerjee 	__netpoll_free(np);
13688a8efa22SAmerigo Wang }
1369f6dc31a8SWANG Cong 
bond_poll_controller(struct net_device * bond_dev)1370f6dc31a8SWANG Cong static void bond_poll_controller(struct net_device *bond_dev)
1371f6dc31a8SWANG Cong {
1372616f4541SMahesh Bandewar 	struct bonding *bond = netdev_priv(bond_dev);
1373616f4541SMahesh Bandewar 	struct slave *slave = NULL;
1374616f4541SMahesh Bandewar 	struct list_head *iter;
1375616f4541SMahesh Bandewar 	struct ad_info ad_info;
1376616f4541SMahesh Bandewar 
1377616f4541SMahesh Bandewar 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
1378616f4541SMahesh Bandewar 		if (bond_3ad_get_active_agg_info(bond, &ad_info))
1379616f4541SMahesh Bandewar 			return;
1380616f4541SMahesh Bandewar 
1381616f4541SMahesh Bandewar 	bond_for_each_slave_rcu(bond, slave, iter) {
138293f62ad5SEric Dumazet 		if (!bond_slave_is_up(slave))
1383616f4541SMahesh Bandewar 			continue;
1384616f4541SMahesh Bandewar 
1385616f4541SMahesh Bandewar 		if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1386616f4541SMahesh Bandewar 			struct aggregator *agg =
1387616f4541SMahesh Bandewar 			    SLAVE_AD_INFO(slave)->port.aggregator;
1388616f4541SMahesh Bandewar 
1389616f4541SMahesh Bandewar 			if (agg &&
1390616f4541SMahesh Bandewar 			    agg->aggregator_identifier != ad_info.aggregator_id)
1391616f4541SMahesh Bandewar 				continue;
1392616f4541SMahesh Bandewar 		}
1393616f4541SMahesh Bandewar 
139493f62ad5SEric Dumazet 		netpoll_poll_dev(slave->dev);
1395616f4541SMahesh Bandewar 	}
13968a8efa22SAmerigo Wang }
13978a8efa22SAmerigo Wang 
bond_netpoll_cleanup(struct net_device * bond_dev)1398c4cdef9bSdingtianhong static void bond_netpoll_cleanup(struct net_device *bond_dev)
13998a8efa22SAmerigo Wang {
1400c4cdef9bSdingtianhong 	struct bonding *bond = netdev_priv(bond_dev);
14019caff1e7SVeaceslav Falico 	struct list_head *iter;
1402c2355e1aSNeil Horman 	struct slave *slave;
1403c2355e1aSNeil Horman 
14049caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter)
1405b6adc610SVeaceslav Falico 		if (bond_slave_is_up(slave))
14068a8efa22SAmerigo Wang 			slave_disable_netpoll(slave);
1407c2355e1aSNeil Horman }
1408f6dc31a8SWANG Cong 
bond_netpoll_setup(struct net_device * dev,struct netpoll_info * ni)1409a8779ec1SEric W. Biederman static int bond_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
14108a8efa22SAmerigo Wang {
14118a8efa22SAmerigo Wang 	struct bonding *bond = netdev_priv(dev);
14129caff1e7SVeaceslav Falico 	struct list_head *iter;
14138a8efa22SAmerigo Wang 	struct slave *slave;
1414dec1e90eSnikolay@redhat.com 	int err = 0;
1415f6dc31a8SWANG Cong 
14169caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
14178a8efa22SAmerigo Wang 		err = slave_enable_netpoll(slave);
14188a8efa22SAmerigo Wang 		if (err) {
1419c4cdef9bSdingtianhong 			bond_netpoll_cleanup(dev);
14208a8efa22SAmerigo Wang 			break;
14218a8efa22SAmerigo Wang 		}
14228a8efa22SAmerigo Wang 	}
14238a8efa22SAmerigo Wang 	return err;
14248a8efa22SAmerigo Wang }
14258a8efa22SAmerigo Wang #else
slave_enable_netpoll(struct slave * slave)14268a8efa22SAmerigo Wang static inline int slave_enable_netpoll(struct slave *slave)
14278a8efa22SAmerigo Wang {
14288a8efa22SAmerigo Wang 	return 0;
14298a8efa22SAmerigo Wang }
slave_disable_netpoll(struct slave * slave)14308a8efa22SAmerigo Wang static inline void slave_disable_netpoll(struct slave *slave)
14318a8efa22SAmerigo Wang {
14328a8efa22SAmerigo Wang }
bond_netpoll_cleanup(struct net_device * bond_dev)1433f6dc31a8SWANG Cong static void bond_netpoll_cleanup(struct net_device *bond_dev)
1434f6dc31a8SWANG Cong {
1435f6dc31a8SWANG Cong }
1436f6dc31a8SWANG Cong #endif
1437f6dc31a8SWANG Cong 
14381da177e4SLinus Torvalds /*---------------------------------- IOCTL ----------------------------------*/
14391da177e4SLinus Torvalds 
bond_fix_features(struct net_device * dev,netdev_features_t features)1440c8f44affSMichał Mirosław static netdev_features_t bond_fix_features(struct net_device *dev,
1441c8f44affSMichał Mirosław 					   netdev_features_t features)
1442b2a103e6SMichał Mirosław {
1443b2a103e6SMichał Mirosław 	struct bonding *bond = netdev_priv(dev);
14449caff1e7SVeaceslav Falico 	struct list_head *iter;
1445c8f44affSMichał Mirosław 	netdev_features_t mask;
14469b7b165aSnikolay@redhat.com 	struct slave *slave;
1447b2a103e6SMichał Mirosław 
14487889cbeeSScott Feldman 	mask = features;
1449c158cba3SRoopa Prabhu 
1450b2a103e6SMichał Mirosław 	features &= ~NETIF_F_ONE_FOR_ALL;
1451b2a103e6SMichał Mirosław 	features |= NETIF_F_ALL_FOR_ALL;
1452b2a103e6SMichał Mirosław 
14539caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
1454b2a103e6SMichał Mirosław 		features = netdev_increment_features(features,
1455b2a103e6SMichał Mirosław 						     slave->dev->features,
1456b2a103e6SMichał Mirosław 						     mask);
1457b2a103e6SMichał Mirosław 	}
1458b0ce3508SEric Dumazet 	features = netdev_add_tso_features(features, mask);
1459b2a103e6SMichał Mirosław 
1460b2a103e6SMichał Mirosław 	return features;
1461b2a103e6SMichał Mirosław }
1462b2a103e6SMichał Mirosław 
1463a188222bSTom Herbert #define BOND_VLAN_FEATURES	(NETIF_F_HW_CSUM | NETIF_F_SG | \
1464ecb8fed4SAlexander Lobakin 				 NETIF_F_FRAGLIST | NETIF_F_GSO_SOFTWARE | \
1465*679b5884SDaniel Borkmann 				 NETIF_F_GSO_ENCAP_ALL | \
146662f2a3a4SMichał Mirosław 				 NETIF_F_HIGHDMA | NETIF_F_LRO)
1467b2a103e6SMichał Mirosław 
1468a188222bSTom Herbert #define BOND_ENC_FEATURES	(NETIF_F_HW_CSUM | NETIF_F_SG | \
1469ecb8fed4SAlexander Lobakin 				 NETIF_F_RXCSUM | NETIF_F_GSO_SOFTWARE)
14705a7baa78SOr Gerlitz 
14712e770b50SAriel Levkovich #define BOND_MPLS_FEATURES	(NETIF_F_HW_CSUM | NETIF_F_SG | \
1472ecb8fed4SAlexander Lobakin 				 NETIF_F_GSO_SOFTWARE)
14732e770b50SAriel Levkovich 
147418cb261aSJarod Wilson 
bond_compute_features(struct bonding * bond)1475b2a103e6SMichał Mirosław static void bond_compute_features(struct bonding *bond)
14768531c5ffSArthur Kepner {
147702875878SEric Dumazet 	unsigned int dst_release_flag = IFF_XMIT_DST_RELEASE |
147802875878SEric Dumazet 					IFF_XMIT_DST_RELEASE_PERM;
1479c8f44affSMichał Mirosław 	netdev_features_t vlan_features = BOND_VLAN_FEATURES;
14805a7baa78SOr Gerlitz 	netdev_features_t enc_features  = BOND_ENC_FEATURES;
148118cb261aSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
148218cb261aSJarod Wilson 	netdev_features_t xfrm_features  = BOND_XFRM_FEATURES;
148318cb261aSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
14842e770b50SAriel Levkovich 	netdev_features_t mpls_features  = BOND_MPLS_FEATURES;
14859caff1e7SVeaceslav Falico 	struct net_device *bond_dev = bond->dev;
14869caff1e7SVeaceslav Falico 	struct list_head *iter;
14879caff1e7SVeaceslav Falico 	struct slave *slave;
1488b2a103e6SMichał Mirosław 	unsigned short max_hard_header_len = ETH_HLEN;
1489ee8b7a11SJakub Kicinski 	unsigned int tso_max_size = TSO_MAX_SIZE;
1490ee8b7a11SJakub Kicinski 	u16 tso_max_segs = TSO_MAX_SEGS;
1491b63365a2SHerbert Xu 
14920965a1f3SVeaceslav Falico 	if (!bond_has_slaves(bond))
1493b63365a2SHerbert Xu 		goto done;
1494a9b3ace4SMichal Kubeček 	vlan_features &= NETIF_F_ALL_FOR_ALL;
14952e770b50SAriel Levkovich 	mpls_features &= NETIF_F_ALL_FOR_ALL;
1496b63365a2SHerbert Xu 
14979caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
1498278339a4SJay Vosburgh 		vlan_features = netdev_increment_features(vlan_features,
1499b2a103e6SMichał Mirosław 			slave->dev->vlan_features, BOND_VLAN_FEATURES);
1500b2a103e6SMichał Mirosław 
15015a7baa78SOr Gerlitz 		enc_features = netdev_increment_features(enc_features,
15025a7baa78SOr Gerlitz 							 slave->dev->hw_enc_features,
15035a7baa78SOr Gerlitz 							 BOND_ENC_FEATURES);
15042e770b50SAriel Levkovich 
150518cb261aSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
150618cb261aSJarod Wilson 		xfrm_features = netdev_increment_features(xfrm_features,
150718cb261aSJarod Wilson 							  slave->dev->hw_enc_features,
150818cb261aSJarod Wilson 							  BOND_XFRM_FEATURES);
150918cb261aSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
151018cb261aSJarod Wilson 
15112e770b50SAriel Levkovich 		mpls_features = netdev_increment_features(mpls_features,
15122e770b50SAriel Levkovich 							  slave->dev->mpls_features,
15132e770b50SAriel Levkovich 							  BOND_MPLS_FEATURES);
15142e770b50SAriel Levkovich 
1515b6fe83e9SEric Dumazet 		dst_release_flag &= slave->dev->priv_flags;
151654ef3137SJay Vosburgh 		if (slave->dev->hard_header_len > max_hard_header_len)
151754ef3137SJay Vosburgh 			max_hard_header_len = slave->dev->hard_header_len;
15180e376bd0SSarveshwar Bandi 
1519ee8b7a11SJakub Kicinski 		tso_max_size = min(tso_max_size, slave->dev->tso_max_size);
1520ee8b7a11SJakub Kicinski 		tso_max_segs = min(tso_max_segs, slave->dev->tso_max_segs);
152154ef3137SJay Vosburgh 	}
152219cdead3SPaolo Abeni 	bond_dev->hard_header_len = max_hard_header_len;
15238531c5ffSArthur Kepner 
1524b63365a2SHerbert Xu done:
1525b2a103e6SMichał Mirosław 	bond_dev->vlan_features = vlan_features;
15268eea1ca8SWillem de Bruijn 	bond_dev->hw_enc_features = enc_features | NETIF_F_GSO_ENCAP_ALL |
1527d595b03dSYueHaibing 				    NETIF_F_HW_VLAN_CTAG_TX |
1528ecb8fed4SAlexander Lobakin 				    NETIF_F_HW_VLAN_STAG_TX;
152918cb261aSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
153018cb261aSJarod Wilson 	bond_dev->hw_enc_features |= xfrm_features;
153118cb261aSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
15322e770b50SAriel Levkovich 	bond_dev->mpls_features = mpls_features;
1533ee8b7a11SJakub Kicinski 	netif_set_tso_max_segs(bond_dev, tso_max_segs);
1534ee8b7a11SJakub Kicinski 	netif_set_tso_max_size(bond_dev, tso_max_size);
15358531c5ffSArthur Kepner 
153602875878SEric Dumazet 	bond_dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
153702875878SEric Dumazet 	if ((bond_dev->priv_flags & IFF_XMIT_DST_RELEASE_PERM) &&
153802875878SEric Dumazet 	    dst_release_flag == (IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM))
153902875878SEric Dumazet 		bond_dev->priv_flags |= IFF_XMIT_DST_RELEASE;
1540b6fe83e9SEric Dumazet 
1541b2a103e6SMichał Mirosław 	netdev_change_features(bond_dev);
15428531c5ffSArthur Kepner }
15438531c5ffSArthur Kepner 
bond_setup_by_slave(struct net_device * bond_dev,struct net_device * slave_dev)1544872254ddSMoni Shoua static void bond_setup_by_slave(struct net_device *bond_dev,
1545872254ddSMoni Shoua 				struct net_device *slave_dev)
1546872254ddSMoni Shoua {
1547d98c9121SEric Dumazet 	bool was_up = !!(bond_dev->flags & IFF_UP);
1548d98c9121SEric Dumazet 
1549d98c9121SEric Dumazet 	dev_close(bond_dev);
1550d98c9121SEric Dumazet 
15511284cd3aSJay Vosburgh 	bond_dev->header_ops	    = slave_dev->header_ops;
1552872254ddSMoni Shoua 
1553872254ddSMoni Shoua 	bond_dev->type		    = slave_dev->type;
1554872254ddSMoni Shoua 	bond_dev->hard_header_len   = slave_dev->hard_header_len;
1555f32f1933SEric Dumazet 	bond_dev->needed_headroom   = slave_dev->needed_headroom;
1556872254ddSMoni Shoua 	bond_dev->addr_len	    = slave_dev->addr_len;
1557872254ddSMoni Shoua 
1558872254ddSMoni Shoua 	memcpy(bond_dev->broadcast, slave_dev->broadcast,
1559872254ddSMoni Shoua 		slave_dev->addr_len);
1560da19a2b9SHangbin Liu 
1561da19a2b9SHangbin Liu 	if (slave_dev->flags & IFF_POINTOPOINT) {
1562da19a2b9SHangbin Liu 		bond_dev->flags &= ~(IFF_BROADCAST | IFF_MULTICAST);
1563da19a2b9SHangbin Liu 		bond_dev->flags |= (IFF_POINTOPOINT | IFF_NOARP);
1564da19a2b9SHangbin Liu 	}
1565d98c9121SEric Dumazet 	if (was_up)
1566d98c9121SEric Dumazet 		dev_open(bond_dev, NULL);
1567872254ddSMoni Shoua }
1568872254ddSMoni Shoua 
15695b2c4dd2SJiri Pirko /* On bonding slaves other than the currently active slave, suppress
15703aba891dSJiri Pirko  * duplicates except for alb non-mcast/bcast.
15715b2c4dd2SJiri Pirko  */
bond_should_deliver_exact_match(struct sk_buff * skb,struct slave * slave,struct bonding * bond)15725b2c4dd2SJiri Pirko static bool bond_should_deliver_exact_match(struct sk_buff *skb,
15730bd80dadSJiri Pirko 					    struct slave *slave,
15740bd80dadSJiri Pirko 					    struct bonding *bond)
15755b2c4dd2SJiri Pirko {
15762d7011caSJiri Pirko 	if (bond_is_slave_inactive(slave)) {
157701844098SVeaceslav Falico 		if (BOND_MODE(bond) == BOND_MODE_ALB &&
15785b2c4dd2SJiri Pirko 		    skb->pkt_type != PACKET_BROADCAST &&
15795b2c4dd2SJiri Pirko 		    skb->pkt_type != PACKET_MULTICAST)
15805b2c4dd2SJiri Pirko 			return false;
15815b2c4dd2SJiri Pirko 		return true;
15825b2c4dd2SJiri Pirko 	}
15835b2c4dd2SJiri Pirko 	return false;
15845b2c4dd2SJiri Pirko }
15855b2c4dd2SJiri Pirko 
bond_handle_frame(struct sk_buff ** pskb)15868a4eb573SJiri Pirko static rx_handler_result_t bond_handle_frame(struct sk_buff **pskb)
15875b2c4dd2SJiri Pirko {
15888a4eb573SJiri Pirko 	struct sk_buff *skb = *pskb;
1589f1c1775aSJiri Pirko 	struct slave *slave;
15900bd80dadSJiri Pirko 	struct bonding *bond;
1591de063b70SEric Dumazet 	int (*recv_probe)(const struct sk_buff *, struct bonding *,
15924d97480bSMitsuo Hayasaka 			  struct slave *);
159313a8e0c8SJiri Bohac 	int ret = RX_HANDLER_ANOTHER;
15945b2c4dd2SJiri Pirko 
15958a4eb573SJiri Pirko 	skb = skb_share_check(skb, GFP_ATOMIC);
15968a4eb573SJiri Pirko 	if (unlikely(!skb))
15978a4eb573SJiri Pirko 		return RX_HANDLER_CONSUMED;
15988a4eb573SJiri Pirko 
15998a4eb573SJiri Pirko 	*pskb = skb;
16005b2c4dd2SJiri Pirko 
160135d48903SJiri Pirko 	slave = bond_slave_get_rcu(skb->dev);
160235d48903SJiri Pirko 	bond = slave->bond;
16030bd80dadSJiri Pirko 
16046aa7de05SMark Rutland 	recv_probe = READ_ONCE(bond->recv_probe);
16054d97480bSMitsuo Hayasaka 	if (recv_probe) {
1606de063b70SEric Dumazet 		ret = recv_probe(skb, bond, slave);
160713a8e0c8SJiri Bohac 		if (ret == RX_HANDLER_CONSUMED) {
160813a8e0c8SJiri Bohac 			consume_skb(skb);
160913a8e0c8SJiri Bohac 			return ret;
161013a8e0c8SJiri Bohac 		}
16113aba891dSJiri Pirko 	}
16123aba891dSJiri Pirko 
16133c963a33SMichal Soltys 	/*
16143c963a33SMichal Soltys 	 * For packets determined by bond_should_deliver_exact_match() call to
16153c963a33SMichal Soltys 	 * be suppressed we want to make an exception for link-local packets.
16163c963a33SMichal Soltys 	 * This is necessary for e.g. LLDP daemons to be able to monitor
16173c963a33SMichal Soltys 	 * inactive slave links without being forced to bind to them
16183c963a33SMichal Soltys 	 * explicitly.
16193c963a33SMichal Soltys 	 *
16203c963a33SMichal Soltys 	 * At the same time, packets that are passed to the bonding master
16213c963a33SMichal Soltys 	 * (including link-local ones) can have their originating interface
16223c963a33SMichal Soltys 	 * determined via PACKET_ORIGDEV socket option.
16236a9e461fSMahesh Bandewar 	 */
16243c963a33SMichal Soltys 	if (bond_should_deliver_exact_match(skb, slave, bond)) {
16253c963a33SMichal Soltys 		if (is_link_local_ether_addr(eth_hdr(skb)->h_dest))
1626b89f04c6SChonggang Li 			return RX_HANDLER_PASS;
16278a4eb573SJiri Pirko 		return RX_HANDLER_EXACT;
16283c963a33SMichal Soltys 	}
16295b2c4dd2SJiri Pirko 
163035d48903SJiri Pirko 	skb->dev = bond->dev;
16315b2c4dd2SJiri Pirko 
163201844098SVeaceslav Falico 	if (BOND_MODE(bond) == BOND_MODE_ALB &&
16332e92a2d0SJulian Wiedmann 	    netif_is_bridge_port(bond->dev) &&
16345b2c4dd2SJiri Pirko 	    skb->pkt_type == PACKET_HOST) {
16355b2c4dd2SJiri Pirko 
1636541ac7c9SChangli Gao 		if (unlikely(skb_cow_head(skb,
1637541ac7c9SChangli Gao 					  skb->data - skb_mac_header(skb)))) {
1638541ac7c9SChangli Gao 			kfree_skb(skb);
16398a4eb573SJiri Pirko 			return RX_HANDLER_CONSUMED;
1640541ac7c9SChangli Gao 		}
1641faeeb317SJarod Wilson 		bond_hw_addr_copy(eth_hdr(skb)->h_dest, bond->dev->dev_addr,
1642faeeb317SJarod Wilson 				  bond->dev->addr_len);
16435b2c4dd2SJiri Pirko 	}
16445b2c4dd2SJiri Pirko 
164513a8e0c8SJiri Bohac 	return ret;
16465b2c4dd2SJiri Pirko }
16475b2c4dd2SJiri Pirko 
bond_lag_tx_type(struct bonding * bond)164841f0b049SJiri Pirko static enum netdev_lag_tx_type bond_lag_tx_type(struct bonding *bond)
1649471cb5a3SJiri Pirko {
165041f0b049SJiri Pirko 	switch (BOND_MODE(bond)) {
165141f0b049SJiri Pirko 	case BOND_MODE_ROUNDROBIN:
165241f0b049SJiri Pirko 		return NETDEV_LAG_TX_TYPE_ROUNDROBIN;
165341f0b049SJiri Pirko 	case BOND_MODE_ACTIVEBACKUP:
165441f0b049SJiri Pirko 		return NETDEV_LAG_TX_TYPE_ACTIVEBACKUP;
165541f0b049SJiri Pirko 	case BOND_MODE_BROADCAST:
165641f0b049SJiri Pirko 		return NETDEV_LAG_TX_TYPE_BROADCAST;
165741f0b049SJiri Pirko 	case BOND_MODE_XOR:
165841f0b049SJiri Pirko 	case BOND_MODE_8023AD:
165941f0b049SJiri Pirko 		return NETDEV_LAG_TX_TYPE_HASH;
166041f0b049SJiri Pirko 	default:
166141f0b049SJiri Pirko 		return NETDEV_LAG_TX_TYPE_UNKNOWN;
166241f0b049SJiri Pirko 	}
166341f0b049SJiri Pirko }
166441f0b049SJiri Pirko 
bond_lag_hash_type(struct bonding * bond,enum netdev_lag_tx_type type)1665f44aa9efSJohn Hurley static enum netdev_lag_hash bond_lag_hash_type(struct bonding *bond,
1666f44aa9efSJohn Hurley 					       enum netdev_lag_tx_type type)
1667f44aa9efSJohn Hurley {
1668f44aa9efSJohn Hurley 	if (type != NETDEV_LAG_TX_TYPE_HASH)
1669f44aa9efSJohn Hurley 		return NETDEV_LAG_HASH_NONE;
1670f44aa9efSJohn Hurley 
1671f44aa9efSJohn Hurley 	switch (bond->params.xmit_policy) {
1672f44aa9efSJohn Hurley 	case BOND_XMIT_POLICY_LAYER2:
1673f44aa9efSJohn Hurley 		return NETDEV_LAG_HASH_L2;
1674f44aa9efSJohn Hurley 	case BOND_XMIT_POLICY_LAYER34:
1675f44aa9efSJohn Hurley 		return NETDEV_LAG_HASH_L34;
1676f44aa9efSJohn Hurley 	case BOND_XMIT_POLICY_LAYER23:
1677f44aa9efSJohn Hurley 		return NETDEV_LAG_HASH_L23;
1678f44aa9efSJohn Hurley 	case BOND_XMIT_POLICY_ENCAP23:
1679f44aa9efSJohn Hurley 		return NETDEV_LAG_HASH_E23;
1680f44aa9efSJohn Hurley 	case BOND_XMIT_POLICY_ENCAP34:
1681f44aa9efSJohn Hurley 		return NETDEV_LAG_HASH_E34;
16827b8fc010SJarod Wilson 	case BOND_XMIT_POLICY_VLAN_SRCMAC:
16837b8fc010SJarod Wilson 		return NETDEV_LAG_HASH_VLAN_SRCMAC;
1684f44aa9efSJohn Hurley 	default:
1685f44aa9efSJohn Hurley 		return NETDEV_LAG_HASH_UNKNOWN;
1686f44aa9efSJohn Hurley 	}
1687f44aa9efSJohn Hurley }
1688f44aa9efSJohn Hurley 
bond_master_upper_dev_link(struct bonding * bond,struct slave * slave,struct netlink_ext_ack * extack)168942ab19eeSDavid Ahern static int bond_master_upper_dev_link(struct bonding *bond, struct slave *slave,
169042ab19eeSDavid Ahern 				      struct netlink_ext_ack *extack)
169141f0b049SJiri Pirko {
169241f0b049SJiri Pirko 	struct netdev_lag_upper_info lag_upper_info;
1693f44aa9efSJohn Hurley 	enum netdev_lag_tx_type type;
16948a321cf7SXin Long 	int err;
1695471cb5a3SJiri Pirko 
1696f44aa9efSJohn Hurley 	type = bond_lag_tx_type(bond);
1697f44aa9efSJohn Hurley 	lag_upper_info.tx_type = type;
1698f44aa9efSJohn Hurley 	lag_upper_info.hash_type = bond_lag_hash_type(bond, type);
16994597efe3SXin Long 
17008a321cf7SXin Long 	err = netdev_master_upper_dev_link(slave->dev, bond->dev, slave,
170142ab19eeSDavid Ahern 					   &lag_upper_info, extack);
17028a321cf7SXin Long 	if (err)
17038a321cf7SXin Long 		return err;
17048a321cf7SXin Long 
17058a321cf7SXin Long 	slave->dev->flags |= IFF_SLAVE;
17068a321cf7SXin Long 	return 0;
1707471cb5a3SJiri Pirko }
1708471cb5a3SJiri Pirko 
bond_upper_dev_unlink(struct bonding * bond,struct slave * slave)170941f0b049SJiri Pirko static void bond_upper_dev_unlink(struct bonding *bond, struct slave *slave)
1710471cb5a3SJiri Pirko {
171141f0b049SJiri Pirko 	netdev_upper_dev_unlink(slave->dev, bond->dev);
171241f0b049SJiri Pirko 	slave->dev->flags &= ~IFF_SLAVE;
1713471cb5a3SJiri Pirko }
1714471cb5a3SJiri Pirko 
slave_kobj_release(struct kobject * kobj)1715b9ad3e9fSJamie Iles static void slave_kobj_release(struct kobject *kobj)
17163fdddd85Sdingtianhong {
1717b9ad3e9fSJamie Iles 	struct slave *slave = to_slave(kobj);
17183fdddd85Sdingtianhong 	struct bonding *bond = bond_get_bond_by_slave(slave);
17193fdddd85Sdingtianhong 
1720d4859d74SMahesh Bandewar 	cancel_delayed_work_sync(&slave->notify_work);
172101844098SVeaceslav Falico 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
17223fdddd85Sdingtianhong 		kfree(SLAVE_AD_INFO(slave));
17233fdddd85Sdingtianhong 
17243fdddd85Sdingtianhong 	kfree(slave);
17253fdddd85Sdingtianhong }
17263fdddd85Sdingtianhong 
1727b9ad3e9fSJamie Iles static struct kobj_type slave_ktype = {
1728b9ad3e9fSJamie Iles 	.release = slave_kobj_release,
1729b9ad3e9fSJamie Iles #ifdef CONFIG_SYSFS
1730b9ad3e9fSJamie Iles 	.sysfs_ops = &slave_sysfs_ops,
1731b9ad3e9fSJamie Iles #endif
1732b9ad3e9fSJamie Iles };
1733b9ad3e9fSJamie Iles 
bond_kobj_init(struct slave * slave)1734b9ad3e9fSJamie Iles static int bond_kobj_init(struct slave *slave)
1735b9ad3e9fSJamie Iles {
1736b9ad3e9fSJamie Iles 	int err;
1737b9ad3e9fSJamie Iles 
1738b9ad3e9fSJamie Iles 	err = kobject_init_and_add(&slave->kobj, &slave_ktype,
1739b9ad3e9fSJamie Iles 				   &(slave->dev->dev.kobj), "bonding_slave");
1740b9ad3e9fSJamie Iles 	if (err)
1741b9ad3e9fSJamie Iles 		kobject_put(&slave->kobj);
1742b9ad3e9fSJamie Iles 
1743b9ad3e9fSJamie Iles 	return err;
1744b9ad3e9fSJamie Iles }
1745b9ad3e9fSJamie Iles 
bond_alloc_slave(struct bonding * bond,struct net_device * slave_dev)1746b9ad3e9fSJamie Iles static struct slave *bond_alloc_slave(struct bonding *bond,
1747b9ad3e9fSJamie Iles 				      struct net_device *slave_dev)
1748b9ad3e9fSJamie Iles {
1749b9ad3e9fSJamie Iles 	struct slave *slave = NULL;
1750b9ad3e9fSJamie Iles 
1751b9ad3e9fSJamie Iles 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
1752b9ad3e9fSJamie Iles 	if (!slave)
1753b9ad3e9fSJamie Iles 		return NULL;
1754b9ad3e9fSJamie Iles 
1755b9ad3e9fSJamie Iles 	slave->bond = bond;
1756b9ad3e9fSJamie Iles 	slave->dev = slave_dev;
175735d96e63SJohannes Berg 	INIT_DELAYED_WORK(&slave->notify_work, bond_netdev_notify_work);
1758b9ad3e9fSJamie Iles 
1759b9ad3e9fSJamie Iles 	if (bond_kobj_init(slave))
1760b9ad3e9fSJamie Iles 		return NULL;
1761b9ad3e9fSJamie Iles 
1762b9ad3e9fSJamie Iles 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
1763b9ad3e9fSJamie Iles 		SLAVE_AD_INFO(slave) = kzalloc(sizeof(struct ad_slave_info),
1764b9ad3e9fSJamie Iles 					       GFP_KERNEL);
1765b9ad3e9fSJamie Iles 		if (!SLAVE_AD_INFO(slave)) {
1766b9ad3e9fSJamie Iles 			kobject_put(&slave->kobj);
1767b9ad3e9fSJamie Iles 			return NULL;
1768b9ad3e9fSJamie Iles 		}
1769b9ad3e9fSJamie Iles 	}
1770b9ad3e9fSJamie Iles 
1771b9ad3e9fSJamie Iles 	return slave;
1772b9ad3e9fSJamie Iles }
1773b9ad3e9fSJamie Iles 
bond_fill_ifbond(struct bonding * bond,struct ifbond * info)177469a2338eSMoni Shoua static void bond_fill_ifbond(struct bonding *bond, struct ifbond *info)
177569a2338eSMoni Shoua {
177669a2338eSMoni Shoua 	info->bond_mode = BOND_MODE(bond);
177769a2338eSMoni Shoua 	info->miimon = bond->params.miimon;
177869a2338eSMoni Shoua 	info->num_slaves = bond->slave_cnt;
177969a2338eSMoni Shoua }
178069a2338eSMoni Shoua 
bond_fill_ifslave(struct slave * slave,struct ifslave * info)178169a2338eSMoni Shoua static void bond_fill_ifslave(struct slave *slave, struct ifslave *info)
178269a2338eSMoni Shoua {
178369a2338eSMoni Shoua 	strcpy(info->slave_name, slave->dev->name);
178469a2338eSMoni Shoua 	info->link = slave->link;
178569a2338eSMoni Shoua 	info->state = bond_slave_state(slave);
178669a2338eSMoni Shoua 	info->link_failure_count = slave->link_failure_count;
178769a2338eSMoni Shoua }
178869a2338eSMoni Shoua 
bond_netdev_notify_work(struct work_struct * _work)178969e61133SMoni Shoua static void bond_netdev_notify_work(struct work_struct *_work)
179069e61133SMoni Shoua {
1791d4859d74SMahesh Bandewar 	struct slave *slave = container_of(_work, struct slave,
1792d4859d74SMahesh Bandewar 					   notify_work.work);
179369e61133SMoni Shoua 
1794d4859d74SMahesh Bandewar 	if (rtnl_trylock()) {
1795d4859d74SMahesh Bandewar 		struct netdev_bonding_info binfo;
1796d4859d74SMahesh Bandewar 
1797d4859d74SMahesh Bandewar 		bond_fill_ifslave(slave, &binfo.slave);
1798d4859d74SMahesh Bandewar 		bond_fill_ifbond(slave->bond, &binfo.master);
1799d4859d74SMahesh Bandewar 		netdev_bonding_info_change(slave->dev, &binfo);
1800d4859d74SMahesh Bandewar 		rtnl_unlock();
1801d4859d74SMahesh Bandewar 	} else {
1802d4859d74SMahesh Bandewar 		queue_delayed_work(slave->bond->wq, &slave->notify_work, 1);
1803d4859d74SMahesh Bandewar 	}
180469e61133SMoni Shoua }
180569e61133SMoni Shoua 
bond_queue_slave_event(struct slave * slave)180669e61133SMoni Shoua void bond_queue_slave_event(struct slave *slave)
180769e61133SMoni Shoua {
1808d4859d74SMahesh Bandewar 	queue_delayed_work(slave->bond->wq, &slave->notify_work, 0);
180969e61133SMoni Shoua }
181069e61133SMoni Shoua 
bond_lower_state_changed(struct slave * slave)1811f7c7eb7fSJiri Pirko void bond_lower_state_changed(struct slave *slave)
1812f7c7eb7fSJiri Pirko {
1813f7c7eb7fSJiri Pirko 	struct netdev_lag_lower_state_info info;
1814f7c7eb7fSJiri Pirko 
1815f7c7eb7fSJiri Pirko 	info.link_up = slave->link == BOND_LINK_UP ||
1816f7c7eb7fSJiri Pirko 		       slave->link == BOND_LINK_FAIL;
1817f7c7eb7fSJiri Pirko 	info.tx_enabled = bond_is_active_slave(slave);
1818f7c7eb7fSJiri Pirko 	netdev_lower_state_changed(slave->dev, &info);
1819f7c7eb7fSJiri Pirko }
1820f7c7eb7fSJiri Pirko 
18216569fa2dSJonathan Toppins #define BOND_NL_ERR(bond_dev, extack, errmsg) do {		\
18226569fa2dSJonathan Toppins 	if (extack)						\
18236569fa2dSJonathan Toppins 		NL_SET_ERR_MSG(extack, errmsg);			\
18246569fa2dSJonathan Toppins 	else							\
18256569fa2dSJonathan Toppins 		netdev_err(bond_dev, "Error: %s\n", errmsg);	\
18266569fa2dSJonathan Toppins } while (0)
18276569fa2dSJonathan Toppins 
18286569fa2dSJonathan Toppins #define SLAVE_NL_ERR(bond_dev, slave_dev, extack, errmsg) do {		\
18296569fa2dSJonathan Toppins 	if (extack)							\
18306569fa2dSJonathan Toppins 		NL_SET_ERR_MSG(extack, errmsg);				\
18316569fa2dSJonathan Toppins 	else								\
18326569fa2dSJonathan Toppins 		slave_err(bond_dev, slave_dev, "Error: %s\n", errmsg);	\
18336569fa2dSJonathan Toppins } while (0)
18346569fa2dSJonathan Toppins 
18359ec7eb60SNikolay Aleksandrov /* The bonding driver uses ether_setup() to convert a master bond device
18369ec7eb60SNikolay Aleksandrov  * to ARPHRD_ETHER, that resets the target netdevice's flags so we always
1837c484fcc0SIdo Schimmel  * have to restore the IFF_MASTER flag, and only restore IFF_SLAVE and IFF_UP
1838c484fcc0SIdo Schimmel  * if they were set
18399ec7eb60SNikolay Aleksandrov  */
bond_ether_setup(struct net_device * bond_dev)18409ec7eb60SNikolay Aleksandrov static void bond_ether_setup(struct net_device *bond_dev)
18419ec7eb60SNikolay Aleksandrov {
1842c484fcc0SIdo Schimmel 	unsigned int flags = bond_dev->flags & (IFF_SLAVE | IFF_UP);
18439ec7eb60SNikolay Aleksandrov 
18449ec7eb60SNikolay Aleksandrov 	ether_setup(bond_dev);
1845c484fcc0SIdo Schimmel 	bond_dev->flags |= IFF_MASTER | flags;
18469ec7eb60SNikolay Aleksandrov 	bond_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
18479ec7eb60SNikolay Aleksandrov }
18489ec7eb60SNikolay Aleksandrov 
bond_xdp_set_features(struct net_device * bond_dev)1849cb9e6e58SLorenzo Bianconi void bond_xdp_set_features(struct net_device *bond_dev)
1850cb9e6e58SLorenzo Bianconi {
1851cb9e6e58SLorenzo Bianconi 	struct bonding *bond = netdev_priv(bond_dev);
1852cb9e6e58SLorenzo Bianconi 	xdp_features_t val = NETDEV_XDP_ACT_MASK;
1853cb9e6e58SLorenzo Bianconi 	struct list_head *iter;
1854cb9e6e58SLorenzo Bianconi 	struct slave *slave;
1855cb9e6e58SLorenzo Bianconi 
1856cb9e6e58SLorenzo Bianconi 	ASSERT_RTNL();
1857cb9e6e58SLorenzo Bianconi 
1858586a2f41SDaniel Borkmann 	if (!bond_xdp_check(bond) || !bond_has_slaves(bond)) {
1859cb9e6e58SLorenzo Bianconi 		xdp_clear_features_flag(bond_dev);
1860cb9e6e58SLorenzo Bianconi 		return;
1861cb9e6e58SLorenzo Bianconi 	}
1862cb9e6e58SLorenzo Bianconi 
1863cb9e6e58SLorenzo Bianconi 	bond_for_each_slave(bond, slave, iter)
1864cb9e6e58SLorenzo Bianconi 		val &= slave->dev->xdp_features;
1865cb9e6e58SLorenzo Bianconi 
186659d440aeSMagnus Karlsson 	val &= ~NETDEV_XDP_ACT_XSK_ZEROCOPY;
186759d440aeSMagnus Karlsson 
1868cb9e6e58SLorenzo Bianconi 	xdp_set_features_flag(bond_dev, val);
1869cb9e6e58SLorenzo Bianconi }
1870cb9e6e58SLorenzo Bianconi 
18711da177e4SLinus Torvalds /* enslave device <slave> to bond device <master> */
bond_enslave(struct net_device * bond_dev,struct net_device * slave_dev,struct netlink_ext_ack * extack)187233eaf2a6SDavid Ahern int bond_enslave(struct net_device *bond_dev, struct net_device *slave_dev,
187333eaf2a6SDavid Ahern 		 struct netlink_ext_ack *extack)
18741da177e4SLinus Torvalds {
1875454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
1876eb7cc59aSStephen Hemminger 	const struct net_device_ops *slave_ops = slave_dev->netdev_ops;
1877c8c23903SVeaceslav Falico 	struct slave *new_slave = NULL, *prev_slave;
1878faeeb317SJarod Wilson 	struct sockaddr_storage ss;
18791da177e4SLinus Torvalds 	int link_reporting;
18808599b52eSVeaceslav Falico 	int res = 0, i;
18811da177e4SLinus Torvalds 
18824d293fe1SDi Zhu 	if (slave_dev->flags & IFF_MASTER &&
18834d293fe1SDi Zhu 	    !netif_is_bond_master(slave_dev)) {
18846569fa2dSJonathan Toppins 		BOND_NL_ERR(bond_dev, extack,
18851b3f78dfSAntoine Tenart 			    "Device type (master device) cannot be enslaved");
18863c9ef511SDi Zhu 		return -EPERM;
18873c9ef511SDi Zhu 	}
18883c9ef511SDi Zhu 
1889c772dde3SBen Hutchings 	if (!bond->params.use_carrier &&
1890c772dde3SBen Hutchings 	    slave_dev->ethtool_ops->get_link == NULL &&
1891a7605370SArnd Bergmann 	    slave_ops->ndo_eth_ioctl == NULL) {
1892e2a7420dSJarod Wilson 		slave_warn(bond_dev, slave_dev, "no link monitoring support\n");
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 
189524b27fc4SMahesh Bandewar 	/* already in-use? */
189624b27fc4SMahesh Bandewar 	if (netdev_is_rx_handler_busy(slave_dev)) {
18976569fa2dSJonathan Toppins 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
18986569fa2dSJonathan Toppins 			     "Device is in use and cannot be enslaved");
18991da177e4SLinus Torvalds 		return -EBUSY;
19001da177e4SLinus Torvalds 	}
19011da177e4SLinus Torvalds 
190209a89c21SJiri Bohac 	if (bond_dev == slave_dev) {
19036569fa2dSJonathan Toppins 		BOND_NL_ERR(bond_dev, extack, "Cannot enslave bond to itself.");
190409a89c21SJiri Bohac 		return -EPERM;
190509a89c21SJiri Bohac 	}
190609a89c21SJiri Bohac 
19071da177e4SLinus Torvalds 	/* vlan challenged mutual exclusion */
19081da177e4SLinus Torvalds 	/* no need to lock since we're protected by rtnl_lock */
19091da177e4SLinus Torvalds 	if (slave_dev->features & NETIF_F_VLAN_CHALLENGED) {
1910e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "is NETIF_F_VLAN_CHALLENGED\n");
191155462cf3SJiri Pirko 		if (vlan_uses_dev(bond_dev)) {
19126569fa2dSJonathan Toppins 			SLAVE_NL_ERR(bond_dev, slave_dev, extack,
19136569fa2dSJonathan Toppins 				     "Can not enslave VLAN challenged device to VLAN enabled bond");
19141da177e4SLinus Torvalds 			return -EPERM;
19151da177e4SLinus Torvalds 		} else {
1916e2a7420dSJarod Wilson 			slave_warn(bond_dev, slave_dev, "enslaved VLAN challenged slave. Adding VLANs will be blocked as long as it is part of bond.\n");
19171da177e4SLinus Torvalds 		}
19181da177e4SLinus Torvalds 	} else {
1919e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "is !NETIF_F_VLAN_CHALLENGED\n");
19201da177e4SLinus Torvalds 	}
19211da177e4SLinus Torvalds 
192218cb261aSJarod Wilson 	if (slave_dev->features & NETIF_F_HW_ESP)
192318cb261aSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "is esp-hw-offload capable\n");
192418cb261aSJarod Wilson 
1925547942caSNikolay Aleksandrov 	/* Old ifenslave binaries are no longer supported.  These can
19263d632c3fSStephen Hemminger 	 * be identified with moderate accuracy by the state of the slave:
1927217df670SJay Vosburgh 	 * the current ifenslave will set the interface down prior to
1928217df670SJay Vosburgh 	 * enslaving it; the old ifenslave will not.
19291da177e4SLinus Torvalds 	 */
1930ce3ea1c7Syzhu1 	if (slave_dev->flags & IFF_UP) {
19316569fa2dSJonathan Toppins 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
19326569fa2dSJonathan Toppins 			     "Device can not be enslaved while up");
19331e2a8868SZhang Shengju 		return -EPERM;
19341da177e4SLinus Torvalds 	}
19351da177e4SLinus Torvalds 
1936872254ddSMoni Shoua 	/* set bonding device ether type by slave - bonding netdevices are
1937872254ddSMoni Shoua 	 * created with ether_setup, so when the slave type is not ARPHRD_ETHER
1938872254ddSMoni Shoua 	 * there is a need to override some of the type dependent attribs/funcs.
1939872254ddSMoni Shoua 	 *
1940872254ddSMoni Shoua 	 * bond ether type mutual exclusion - don't allow slaves of dissimilar
1941872254ddSMoni Shoua 	 * ether type (eg ARPHRD_ETHER and ARPHRD_INFINIBAND) share the same bond
1942872254ddSMoni Shoua 	 */
19430965a1f3SVeaceslav Falico 	if (!bond_has_slaves(bond)) {
1944e36b9d16SMoni Shoua 		if (bond_dev->type != slave_dev->type) {
1945e2a7420dSJarod Wilson 			slave_dbg(bond_dev, slave_dev, "change device type from %d to %d\n",
1946a4aee5c8SJoe Perches 				  bond_dev->type, slave_dev->type);
194775c78500SMoni Shoua 
1948b7bc2a5bSAmerigo Wang 			res = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
1949b7bc2a5bSAmerigo Wang 						       bond_dev);
19503ca5b404SJiri Pirko 			res = notifier_to_errno(res);
19513ca5b404SJiri Pirko 			if (res) {
1952e2a7420dSJarod Wilson 				slave_err(bond_dev, slave_dev, "refused to change device type\n");
19531e2a8868SZhang Shengju 				return -EBUSY;
19543ca5b404SJiri Pirko 			}
195575c78500SMoni Shoua 
195632a806c1SJiri Pirko 			/* Flush unicast and multicast addresses */
1957a748ee24SJiri Pirko 			dev_uc_flush(bond_dev);
195822bedad3SJiri Pirko 			dev_mc_flush(bond_dev);
195932a806c1SJiri Pirko 
1960872254ddSMoni Shoua 			if (slave_dev->type != ARPHRD_ETHER)
1961872254ddSMoni Shoua 				bond_setup_by_slave(bond_dev, slave_dev);
19629ec7eb60SNikolay Aleksandrov 			else
19639ec7eb60SNikolay Aleksandrov 				bond_ether_setup(bond_dev);
196475c78500SMoni Shoua 
1965b7bc2a5bSAmerigo Wang 			call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
1966b7bc2a5bSAmerigo Wang 						 bond_dev);
1967e36b9d16SMoni Shoua 		}
1968872254ddSMoni Shoua 	} else if (bond_dev->type != slave_dev->type) {
19696569fa2dSJonathan Toppins 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
19706569fa2dSJonathan Toppins 			     "Device type is different from other slaves");
19711e2a8868SZhang Shengju 		return -EINVAL;
1972872254ddSMoni Shoua 	}
1973872254ddSMoni Shoua 
19741533e773SMark Bloch 	if (slave_dev->type == ARPHRD_INFINIBAND &&
19751533e773SMark Bloch 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
19766569fa2dSJonathan Toppins 		SLAVE_NL_ERR(bond_dev, slave_dev, extack,
19776569fa2dSJonathan Toppins 			     "Only active-backup mode is supported for infiniband slaves");
19781533e773SMark Bloch 		res = -EOPNOTSUPP;
19791533e773SMark Bloch 		goto err_undo_flags;
19801533e773SMark Bloch 	}
19811533e773SMark Bloch 
19821533e773SMark Bloch 	if (!slave_ops->ndo_set_mac_address ||
19831533e773SMark Bloch 	    slave_dev->type == ARPHRD_INFINIBAND) {
1984e2a7420dSJarod Wilson 		slave_warn(bond_dev, slave_dev, "The slave device specified does not support setting the MAC address\n");
1985f5442441SVeaceslav Falico 		if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP &&
1986f5442441SVeaceslav Falico 		    bond->params.fail_over_mac != BOND_FOM_ACTIVE) {
1987f5442441SVeaceslav Falico 			if (!bond_has_slaves(bond)) {
19883915c1e8SJay Vosburgh 				bond->params.fail_over_mac = BOND_FOM_ACTIVE;
1989e2a7420dSJarod Wilson 				slave_warn(bond_dev, slave_dev, "Setting fail_over_mac to active for active-backup mode\n");
1990f5442441SVeaceslav Falico 			} else {
19916569fa2dSJonathan Toppins 				SLAVE_NL_ERR(bond_dev, slave_dev, extack,
19926569fa2dSJonathan Toppins 					     "Slave device does not support setting the MAC address, but fail_over_mac is not set to active");
19931da177e4SLinus Torvalds 				res = -EOPNOTSUPP;
19941da177e4SLinus Torvalds 				goto err_undo_flags;
19951da177e4SLinus Torvalds 			}
19962ab82852SMoni Shoua 		}
1997f5442441SVeaceslav Falico 	}
19981da177e4SLinus Torvalds 
19998d8fc29dSAmerigo Wang 	call_netdevice_notifiers(NETDEV_JOIN, slave_dev);
20008d8fc29dSAmerigo Wang 
2001c20811a7SJiri Pirko 	/* If this is the first slave, then we need to set the master's hardware
2002547942caSNikolay Aleksandrov 	 * address to be the same as the slave's.
2003547942caSNikolay Aleksandrov 	 */
20040965a1f3SVeaceslav Falico 	if (!bond_has_slaves(bond) &&
2005b9245914SPetr Machata 	    bond->dev->addr_assign_type == NET_ADDR_RANDOM) {
2006b9245914SPetr Machata 		res = bond_set_dev_addr(bond->dev, slave_dev);
2007b9245914SPetr Machata 		if (res)
2008b9245914SPetr Machata 			goto err_undo_flags;
2009b9245914SPetr Machata 	}
2010c20811a7SJiri Pirko 
2011b9ad3e9fSJamie Iles 	new_slave = bond_alloc_slave(bond, slave_dev);
20121da177e4SLinus Torvalds 	if (!new_slave) {
20131da177e4SLinus Torvalds 		res = -ENOMEM;
20141da177e4SLinus Torvalds 		goto err_undo_flags;
20151da177e4SLinus Torvalds 	}
20163fdddd85Sdingtianhong 
2017547942caSNikolay Aleksandrov 	/* Set the new_slave's queue_id to be zero.  Queue ID mapping
2018bb1d9123SAndy Gospodarek 	 * is set via sysfs or module option if desired.
2019bb1d9123SAndy Gospodarek 	 */
2020bb1d9123SAndy Gospodarek 	new_slave->queue_id = 0;
2021bb1d9123SAndy Gospodarek 
2022b15ba0fbSJiri Pirko 	/* Save slave's original mtu and then set it to match the bond */
2023b15ba0fbSJiri Pirko 	new_slave->original_mtu = slave_dev->mtu;
2024b15ba0fbSJiri Pirko 	res = dev_set_mtu(slave_dev, bond->dev->mtu);
2025b15ba0fbSJiri Pirko 	if (res) {
2026e2a7420dSJarod Wilson 		slave_err(bond_dev, slave_dev, "Error %d calling dev_set_mtu\n", res);
2027b15ba0fbSJiri Pirko 		goto err_free;
2028b15ba0fbSJiri Pirko 	}
2029b15ba0fbSJiri Pirko 
2030547942caSNikolay Aleksandrov 	/* Save slave's original ("permanent") mac address for modes
2031217df670SJay Vosburgh 	 * that need it, and for restoring it upon release, and then
2032217df670SJay Vosburgh 	 * set it to the master's address
20331da177e4SLinus Torvalds 	 */
2034faeeb317SJarod Wilson 	bond_hw_addr_copy(new_slave->perm_hwaddr, slave_dev->dev_addr,
2035faeeb317SJarod Wilson 			  slave_dev->addr_len);
20361da177e4SLinus Torvalds 
203700503b6fSdingtianhong 	if (!bond->params.fail_over_mac ||
203801844098SVeaceslav Falico 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
2039547942caSNikolay Aleksandrov 		/* Set slave to master's mac address.  The application already
2040217df670SJay Vosburgh 		 * set the master's mac address to that of the first slave
20411da177e4SLinus Torvalds 		 */
2042faeeb317SJarod Wilson 		memcpy(ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
2043faeeb317SJarod Wilson 		ss.ss_family = slave_dev->type;
20443a37a963SPetr Machata 		res = dev_set_mac_address(slave_dev, (struct sockaddr *)&ss,
20453a37a963SPetr Machata 					  extack);
20461da177e4SLinus Torvalds 		if (res) {
2047e2a7420dSJarod Wilson 			slave_err(bond_dev, slave_dev, "Error %d calling set_mac_address\n", res);
2048b15ba0fbSJiri Pirko 			goto err_restore_mtu;
20491da177e4SLinus Torvalds 		}
20502ab82852SMoni Shoua 	}
20511da177e4SLinus Torvalds 
20528a321cf7SXin Long 	/* set no_addrconf flag before open to prevent IPv6 addrconf */
20538a321cf7SXin Long 	slave_dev->priv_flags |= IFF_NO_ADDRCONF;
205403d84a5fSKarl Heiss 
20551da177e4SLinus Torvalds 	/* open the slave since the application closed it */
205600f54e68SPetr Machata 	res = dev_open(slave_dev, extack);
20571da177e4SLinus Torvalds 	if (res) {
2058e2a7420dSJarod Wilson 		slave_err(bond_dev, slave_dev, "Opening slave failed\n");
20591f718f0fSVeaceslav Falico 		goto err_restore_mac;
20601da177e4SLinus Torvalds 	}
20611da177e4SLinus Torvalds 
20620b680e75SJay Vosburgh 	slave_dev->priv_flags |= IFF_BONDING;
20635f0c5f73SAndy Gospodarek 	/* initialize slave stats */
20645f0c5f73SAndy Gospodarek 	dev_get_stats(new_slave->dev, &new_slave->slave_stats);
20651da177e4SLinus Torvalds 
206658402054SHolger Eitzenberger 	if (bond_is_lb(bond)) {
20671da177e4SLinus Torvalds 		/* bond_alb_init_slave() must be called before all other stages since
20681da177e4SLinus Torvalds 		 * it might fail and we do not want to have to undo everything
20691da177e4SLinus Torvalds 		 */
20701da177e4SLinus Torvalds 		res = bond_alb_init_slave(bond, new_slave);
20713d632c3fSStephen Hemminger 		if (res)
2072569f0c4dSJay Vosburgh 			goto err_close;
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 
2075b8e2fde4SWei Yongjun 	res = vlan_vids_add_by_dev(slave_dev, bond_dev);
2076b8e2fde4SWei Yongjun 	if (res) {
2077e2a7420dSJarod Wilson 		slave_err(bond_dev, slave_dev, "Couldn't add bond vlan ids\n");
2078ae42cc62SXin Long 		goto err_close;
20791ff412adSnikolay@redhat.com 	}
20801da177e4SLinus Torvalds 
2081c8c23903SVeaceslav Falico 	prev_slave = bond_last_slave(bond);
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	new_slave->delay = 0;
20841da177e4SLinus Torvalds 	new_slave->link_failure_count = 0;
20851da177e4SLinus Torvalds 
2086ad729bc9SAndreas Born 	if (bond_update_speed_duplex(new_slave) &&
2087ad729bc9SAndreas Born 	    bond_needs_speed_duplex(bond))
20883f3c278cSMahesh Bandewar 		new_slave->link = BOND_LINK_DOWN;
2089876254aeSVeaceslav Falico 
209049f17de7SVeaceslav Falico 	new_slave->last_rx = jiffies -
2091f31c7937SMichal Kubeček 		(msecs_to_jiffies(bond->params.arp_interval) + 1);
20928599b52eSVeaceslav Falico 	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++)
209349f17de7SVeaceslav Falico 		new_slave->target_last_arp_rx[i] = new_slave->last_rx;
2094f5b2b966SJay Vosburgh 
209506799a90SVladimir Oltean 	new_slave->last_tx = new_slave->last_rx;
209606799a90SVladimir Oltean 
20971da177e4SLinus Torvalds 	if (bond->params.miimon && !bond->params.use_carrier) {
20981da177e4SLinus Torvalds 		link_reporting = bond_check_dev_link(bond, slave_dev, 1);
20991da177e4SLinus Torvalds 
21001da177e4SLinus Torvalds 		if ((link_reporting == -1) && !bond->params.arp_interval) {
2101547942caSNikolay Aleksandrov 			/* miimon is set but a bonded network driver
21021da177e4SLinus Torvalds 			 * does not support ETHTOOL/MII and
21031da177e4SLinus Torvalds 			 * arp_interval is not set.  Note: if
21041da177e4SLinus Torvalds 			 * use_carrier is enabled, we will never go
21051da177e4SLinus Torvalds 			 * here (because netif_carrier is always
21061da177e4SLinus Torvalds 			 * supported); thus, we don't need to change
21071da177e4SLinus Torvalds 			 * the messages for netif_carrier.
21081da177e4SLinus Torvalds 			 */
2109e2a7420dSJarod Wilson 			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");
21101da177e4SLinus Torvalds 		} else if (link_reporting == -1) {
21111da177e4SLinus Torvalds 			/* unable get link status using mii/ethtool */
2112e2a7420dSJarod Wilson 			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");
21131da177e4SLinus Torvalds 		}
21141da177e4SLinus Torvalds 	}
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	/* check for initial state */
2117a30b0168SAviv Heller 	new_slave->link = BOND_LINK_NOCHANGE;
2118f31c7937SMichal Kubeček 	if (bond->params.miimon) {
2119f31c7937SMichal Kubeček 		if (bond_check_dev_link(bond, slave_dev, 0) == BMSR_LSTATUS) {
21201da177e4SLinus Torvalds 			if (bond->params.updelay) {
212169a2338eSMoni Shoua 				bond_set_slave_link_state(new_slave,
21225d397061SJiri Pirko 							  BOND_LINK_BACK,
21235d397061SJiri Pirko 							  BOND_SLAVE_NOTIFY_NOW);
21241da177e4SLinus Torvalds 				new_slave->delay = bond->params.updelay;
21251da177e4SLinus Torvalds 			} else {
212669a2338eSMoni Shoua 				bond_set_slave_link_state(new_slave,
21275d397061SJiri Pirko 							  BOND_LINK_UP,
21285d397061SJiri Pirko 							  BOND_SLAVE_NOTIFY_NOW);
21291da177e4SLinus Torvalds 			}
21301da177e4SLinus Torvalds 		} else {
21315d397061SJiri Pirko 			bond_set_slave_link_state(new_slave, BOND_LINK_DOWN,
21325d397061SJiri Pirko 						  BOND_SLAVE_NOTIFY_NOW);
21331da177e4SLinus Torvalds 		}
2134f31c7937SMichal Kubeček 	} else if (bond->params.arp_interval) {
213569a2338eSMoni Shoua 		bond_set_slave_link_state(new_slave,
213669a2338eSMoni Shoua 					  (netif_carrier_ok(slave_dev) ?
21375d397061SJiri Pirko 					  BOND_LINK_UP : BOND_LINK_DOWN),
21385d397061SJiri Pirko 					  BOND_SLAVE_NOTIFY_NOW);
2139f31c7937SMichal Kubeček 	} else {
21405d397061SJiri Pirko 		bond_set_slave_link_state(new_slave, BOND_LINK_UP,
21415d397061SJiri Pirko 					  BOND_SLAVE_NOTIFY_NOW);
2142f31c7937SMichal Kubeček 	}
2143f31c7937SMichal Kubeček 
2144f31c7937SMichal Kubeček 	if (new_slave->link != BOND_LINK_DOWN)
21458e603460SVeaceslav Falico 		new_slave->last_link_up = jiffies;
2146e2a7420dSJarod Wilson 	slave_dbg(bond_dev, slave_dev, "Initial state of slave is BOND_LINK_%s\n",
2147f31c7937SMichal Kubeček 		  new_slave->link == BOND_LINK_DOWN ? "DOWN" :
2148f31c7937SMichal Kubeček 		  (new_slave->link == BOND_LINK_UP ? "UP" : "BACK"));
21491da177e4SLinus Torvalds 
2150ec0865a9SVeaceslav Falico 	if (bond_uses_primary(bond) && bond->params.primary[0]) {
21511da177e4SLinus Torvalds 		/* if there is a primary slave, remember it */
2152a549952aSJiri Pirko 		if (strcmp(bond->params.primary, new_slave->dev->name) == 0) {
2153059b47e8SNikolay Aleksandrov 			rcu_assign_pointer(bond->primary_slave, new_slave);
2154a549952aSJiri Pirko 			bond->force_primary = true;
2155a549952aSJiri Pirko 		}
21561da177e4SLinus Torvalds 	}
21571da177e4SLinus Torvalds 
215801844098SVeaceslav Falico 	switch (BOND_MODE(bond)) {
21591da177e4SLinus Torvalds 	case BOND_MODE_ACTIVEBACKUP:
21605e5b0665Sdingtianhong 		bond_set_slave_inactive_flags(new_slave,
21615e5b0665Sdingtianhong 					      BOND_SLAVE_NOTIFY_NOW);
21621da177e4SLinus Torvalds 		break;
21631da177e4SLinus Torvalds 	case BOND_MODE_8023AD:
21641da177e4SLinus Torvalds 		/* in 802.3ad mode, the internal mechanism
21651da177e4SLinus Torvalds 		 * will activate the slaves in the selected
21661da177e4SLinus Torvalds 		 * aggregator
21671da177e4SLinus Torvalds 		 */
21685e5b0665Sdingtianhong 		bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
21691da177e4SLinus Torvalds 		/* if this is the first slave */
217023c147e0SVeaceslav Falico 		if (!prev_slave) {
21713fdddd85Sdingtianhong 			SLAVE_AD_INFO(new_slave)->id = 1;
21721da177e4SLinus Torvalds 			/* Initialize AD with the number of times that the AD timer is called in 1 second
21731da177e4SLinus Torvalds 			 * can be called only after the mac address of the bond is set
21741da177e4SLinus Torvalds 			 */
2175f2e44dffSJonathan Toppins 			bond_3ad_initialize(bond);
21761da177e4SLinus Torvalds 		} else {
21773fdddd85Sdingtianhong 			SLAVE_AD_INFO(new_slave)->id =
21783fdddd85Sdingtianhong 				SLAVE_AD_INFO(prev_slave)->id + 1;
21791da177e4SLinus Torvalds 		}
21801da177e4SLinus Torvalds 
21811da177e4SLinus Torvalds 		bond_3ad_bind_slave(new_slave);
21821da177e4SLinus Torvalds 		break;
21831da177e4SLinus Torvalds 	case BOND_MODE_TLB:
21841da177e4SLinus Torvalds 	case BOND_MODE_ALB:
2185e30bc066SJiri Pirko 		bond_set_active_slave(new_slave);
21865e5b0665Sdingtianhong 		bond_set_slave_inactive_flags(new_slave, BOND_SLAVE_NOTIFY_NOW);
21871da177e4SLinus Torvalds 		break;
21881da177e4SLinus Torvalds 	default:
2189e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "This slave is always active in trunk mode\n");
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds 		/* always active in trunk mode */
2192e30bc066SJiri Pirko 		bond_set_active_slave(new_slave);
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 		/* In trunking mode there is little meaning to curr_active_slave
21951da177e4SLinus Torvalds 		 * anyway (it holds no special properties of the bond device),
21961da177e4SLinus Torvalds 		 * so we can change it without calling change_active_interface()
21971da177e4SLinus Torvalds 		 */
21984740d638SEric Dumazet 		if (!rcu_access_pointer(bond->curr_active_slave) &&
21994740d638SEric Dumazet 		    new_slave->link == BOND_LINK_UP)
2200278b2083Snikolay@redhat.com 			rcu_assign_pointer(bond->curr_active_slave, new_slave);
22013d632c3fSStephen Hemminger 
22021da177e4SLinus Torvalds 		break;
22031da177e4SLinus Torvalds 	} /* switch(bond_mode) */
22041da177e4SLinus Torvalds 
2205f6dc31a8SWANG Cong #ifdef CONFIG_NET_POLL_CONTROLLER
2206ddea788cSXin Long 	if (bond->dev->npinfo) {
22078a8efa22SAmerigo Wang 		if (slave_enable_netpoll(new_slave)) {
2208e2a7420dSJarod Wilson 			slave_info(bond_dev, slave_dev, "master_dev is using netpoll, but new slave device does not support netpoll\n");
22098a8efa22SAmerigo Wang 			res = -EBUSY;
2210f7d9821aSstephen hemminger 			goto err_detach;
22118a8efa22SAmerigo Wang 		}
2212f6dc31a8SWANG Cong 	}
2213f6dc31a8SWANG Cong #endif
22148a8efa22SAmerigo Wang 
2215fbe168baSMichal Kubeček 	if (!(bond_dev->features & NETIF_F_LRO))
2216fbe168baSMichal Kubeček 		dev_disable_lro(slave_dev);
2217fbe168baSMichal Kubeček 
221835d48903SJiri Pirko 	res = netdev_rx_handler_register(slave_dev, bond_handle_frame,
221935d48903SJiri Pirko 					 new_slave);
222035d48903SJiri Pirko 	if (res) {
2221e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "Error %d calling netdev_rx_handler_register\n", res);
22225831d66eSVeaceslav Falico 		goto err_detach;
222335d48903SJiri Pirko 	}
222435d48903SJiri Pirko 
222542ab19eeSDavid Ahern 	res = bond_master_upper_dev_link(bond, new_slave, extack);
22261f718f0fSVeaceslav Falico 	if (res) {
2227e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "Error %d calling bond_master_upper_dev_link\n", res);
22281f718f0fSVeaceslav Falico 		goto err_unregister;
22291f718f0fSVeaceslav Falico 	}
22301f718f0fSVeaceslav Falico 
223132d4c564STobias Waldekranz 	bond_lower_state_changed(new_slave);
223232d4c564STobias Waldekranz 
223307699f9aSsfeldma@cumulusnetworks.com 	res = bond_sysfs_slave_add(new_slave);
223407699f9aSsfeldma@cumulusnetworks.com 	if (res) {
2235e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "Error %d calling bond_sysfs_slave_add\n", res);
223607699f9aSsfeldma@cumulusnetworks.com 		goto err_upper_unlink;
223707699f9aSsfeldma@cumulusnetworks.com 	}
223807699f9aSsfeldma@cumulusnetworks.com 
2239ae42cc62SXin Long 	/* If the mode uses primary, then the following is handled by
2240ae42cc62SXin Long 	 * bond_change_active_slave().
2241ae42cc62SXin Long 	 */
2242ae42cc62SXin Long 	if (!bond_uses_primary(bond)) {
2243ae42cc62SXin Long 		/* set promiscuity level to new slave */
2244ae42cc62SXin Long 		if (bond_dev->flags & IFF_PROMISC) {
2245ae42cc62SXin Long 			res = dev_set_promiscuity(slave_dev, 1);
2246ae42cc62SXin Long 			if (res)
2247ae42cc62SXin Long 				goto err_sysfs_del;
2248ae42cc62SXin Long 		}
2249ae42cc62SXin Long 
2250ae42cc62SXin Long 		/* set allmulti level to new slave */
2251ae42cc62SXin Long 		if (bond_dev->flags & IFF_ALLMULTI) {
2252ae42cc62SXin Long 			res = dev_set_allmulti(slave_dev, 1);
22539f5a90c1SXin Long 			if (res) {
22549f5a90c1SXin Long 				if (bond_dev->flags & IFF_PROMISC)
22559f5a90c1SXin Long 					dev_set_promiscuity(slave_dev, -1);
2256ae42cc62SXin Long 				goto err_sysfs_del;
2257ae42cc62SXin Long 			}
22589f5a90c1SXin Long 		}
2259ae42cc62SXin Long 
226086247abaSBenjamin Poirier 		if (bond_dev->flags & IFF_UP) {
2261ae42cc62SXin Long 			netif_addr_lock_bh(bond_dev);
2262ae42cc62SXin Long 			dev_mc_sync_multiple(slave_dev, bond_dev);
2263ae42cc62SXin Long 			dev_uc_sync_multiple(slave_dev, bond_dev);
2264ae42cc62SXin Long 			netif_addr_unlock_bh(bond_dev);
2265ae42cc62SXin Long 
22661d9a143eSBenjamin Poirier 			if (BOND_MODE(bond) == BOND_MODE_8023AD)
22671d9a143eSBenjamin Poirier 				dev_mc_add(slave_dev, lacpdu_mcast_addr);
2268ae42cc62SXin Long 		}
2269ae42cc62SXin Long 	}
2270ae42cc62SXin Long 
22715378c2e6SVeaceslav Falico 	bond->slave_cnt++;
22725378c2e6SVeaceslav Falico 	bond_compute_features(bond);
22735378c2e6SVeaceslav Falico 	bond_set_carrier(bond);
22745378c2e6SVeaceslav Falico 
22759ea6b70fSHangbin Liu 	/* Needs to be called before bond_select_active_slave(), which will
22769ea6b70fSHangbin Liu 	 * remove the maddrs if the slave is selected as active slave.
22779ea6b70fSHangbin Liu 	 */
22789ea6b70fSHangbin Liu 	bond_slave_ns_maddrs_add(bond, new_slave);
22799ea6b70fSHangbin Liu 
2280ec0865a9SVeaceslav Falico 	if (bond_uses_primary(bond)) {
2281f80889a5Sdingtianhong 		block_netpoll_tx();
22825378c2e6SVeaceslav Falico 		bond_select_active_slave(bond);
2283f80889a5Sdingtianhong 		unblock_netpoll_tx();
22845378c2e6SVeaceslav Falico 	}
22851f718f0fSVeaceslav Falico 
2286e79c1055SDebabrata Banerjee 	if (bond_mode_can_use_xmit_hash(bond))
2287ee637714SMahesh Bandewar 		bond_update_slave_arr(bond, NULL);
2288ee637714SMahesh Bandewar 
22899e2ee5c7SJussi Maki 	if (!slave_dev->netdev_ops->ndo_bpf ||
22909e2ee5c7SJussi Maki 	    !slave_dev->netdev_ops->ndo_xdp_xmit) {
22919e2ee5c7SJussi Maki 		if (bond->xdp_prog) {
22926569fa2dSJonathan Toppins 			SLAVE_NL_ERR(bond_dev, slave_dev, extack,
22936569fa2dSJonathan Toppins 				     "Slave does not support XDP");
22949e2ee5c7SJussi Maki 			res = -EOPNOTSUPP;
22959e2ee5c7SJussi Maki 			goto err_sysfs_del;
22969e2ee5c7SJussi Maki 		}
22976d5f1ef8SJussi Maki 	} else if (bond->xdp_prog) {
22989e2ee5c7SJussi Maki 		struct netdev_bpf xdp = {
22999e2ee5c7SJussi Maki 			.command = XDP_SETUP_PROG,
23009e2ee5c7SJussi Maki 			.flags   = 0,
23019e2ee5c7SJussi Maki 			.prog    = bond->xdp_prog,
23029e2ee5c7SJussi Maki 			.extack  = extack,
23039e2ee5c7SJussi Maki 		};
23049e2ee5c7SJussi Maki 
23059e2ee5c7SJussi Maki 		if (dev_xdp_prog_count(slave_dev) > 0) {
23066569fa2dSJonathan Toppins 			SLAVE_NL_ERR(bond_dev, slave_dev, extack,
23079e2ee5c7SJussi Maki 				     "Slave has XDP program loaded, please unload before enslaving");
23089e2ee5c7SJussi Maki 			res = -EOPNOTSUPP;
23099e2ee5c7SJussi Maki 			goto err_sysfs_del;
23109e2ee5c7SJussi Maki 		}
23119e2ee5c7SJussi Maki 
23129e2ee5c7SJussi Maki 		res = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
23139e2ee5c7SJussi Maki 		if (res < 0) {
23149e2ee5c7SJussi Maki 			/* ndo_bpf() sets extack error message */
23159e2ee5c7SJussi Maki 			slave_dbg(bond_dev, slave_dev, "Error %d calling ndo_bpf\n", res);
23169e2ee5c7SJussi Maki 			goto err_sysfs_del;
23179e2ee5c7SJussi Maki 		}
23189e2ee5c7SJussi Maki 		if (bond->xdp_prog)
23199e2ee5c7SJussi Maki 			bpf_prog_inc(bond->xdp_prog);
23209e2ee5c7SJussi Maki 	}
23219e2ee5c7SJussi Maki 
2322cb9e6e58SLorenzo Bianconi 	bond_xdp_set_features(bond_dev);
2323cb9e6e58SLorenzo Bianconi 
2324e2a7420dSJarod Wilson 	slave_info(bond_dev, slave_dev, "Enslaving as %s interface with %s link\n",
232590194264SJoe Perches 		   bond_is_active_slave(new_slave) ? "an active" : "a backup",
232690194264SJoe Perches 		   new_slave->link != BOND_LINK_DOWN ? "an up" : "a down");
23271da177e4SLinus Torvalds 
23281da177e4SLinus Torvalds 	/* enslave is successful */
232969e61133SMoni Shoua 	bond_queue_slave_event(new_slave);
23301da177e4SLinus Torvalds 	return 0;
23311da177e4SLinus Torvalds 
23321da177e4SLinus Torvalds /* Undo stages on error */
2333ae42cc62SXin Long err_sysfs_del:
2334ae42cc62SXin Long 	bond_sysfs_slave_del(new_slave);
2335ae42cc62SXin Long 
233607699f9aSsfeldma@cumulusnetworks.com err_upper_unlink:
233741f0b049SJiri Pirko 	bond_upper_dev_unlink(bond, new_slave);
233807699f9aSsfeldma@cumulusnetworks.com 
23391f718f0fSVeaceslav Falico err_unregister:
23401f718f0fSVeaceslav Falico 	netdev_rx_handler_unregister(slave_dev);
23411f718f0fSVeaceslav Falico 
2342f7d9821aSstephen hemminger err_detach:
23431ff412adSnikolay@redhat.com 	vlan_vids_del_by_dev(slave_dev, bond_dev);
2344059b47e8SNikolay Aleksandrov 	if (rcu_access_pointer(bond->primary_slave) == new_slave)
2345059b47e8SNikolay Aleksandrov 		RCU_INIT_POINTER(bond->primary_slave, NULL);
23464740d638SEric Dumazet 	if (rcu_access_pointer(bond->curr_active_slave) == new_slave) {
2347f80889a5Sdingtianhong 		block_netpoll_tx();
2348c8517035Sdingtianhong 		bond_change_active_slave(bond, NULL);
23493c5913b5Snikolay@redhat.com 		bond_select_active_slave(bond);
2350f80889a5Sdingtianhong 		unblock_netpoll_tx();
23513c5913b5Snikolay@redhat.com 	}
2352059b47e8SNikolay Aleksandrov 	/* either primary_slave or curr_active_slave might've changed */
2353059b47e8SNikolay Aleksandrov 	synchronize_rcu();
2354fc7a72acSnikolay@redhat.com 	slave_disable_netpoll(new_slave);
2355f7d9821aSstephen hemminger 
23561da177e4SLinus Torvalds err_close:
235765de65d9STaehee Yoo 	if (!netif_is_bond_master(slave_dev))
2358b6a5a7b9Snikolay@redhat.com 		slave_dev->priv_flags &= ~IFF_BONDING;
23591da177e4SLinus Torvalds 	dev_close(slave_dev);
23601da177e4SLinus Torvalds 
23611da177e4SLinus Torvalds err_restore_mac:
23628a321cf7SXin Long 	slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
236300503b6fSdingtianhong 	if (!bond->params.fail_over_mac ||
236401844098SVeaceslav Falico 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
23653915c1e8SJay Vosburgh 		/* XXX TODO - fom follow mode needs to change master's
23663915c1e8SJay Vosburgh 		 * MAC if this slave's MAC is in use by the bond, or at
23673915c1e8SJay Vosburgh 		 * least print a warning.
23683915c1e8SJay Vosburgh 		 */
2369faeeb317SJarod Wilson 		bond_hw_addr_copy(ss.__data, new_slave->perm_hwaddr,
2370faeeb317SJarod Wilson 				  new_slave->dev->addr_len);
2371faeeb317SJarod Wilson 		ss.ss_family = slave_dev->type;
23723a37a963SPetr Machata 		dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
23732ab82852SMoni Shoua 	}
23741da177e4SLinus Torvalds 
2375b15ba0fbSJiri Pirko err_restore_mtu:
2376b15ba0fbSJiri Pirko 	dev_set_mtu(slave_dev, new_slave->original_mtu);
2377b15ba0fbSJiri Pirko 
23781da177e4SLinus Torvalds err_free:
2379b9ad3e9fSJamie Iles 	kobject_put(&new_slave->kobj);
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds err_undo_flags:
2382b8fad459SNikolay Aleksandrov 	/* Enslave of first slave has failed and we need to fix master's mac */
23837d5cd2ceSNikolay Aleksandrov 	if (!bond_has_slaves(bond)) {
23847d5cd2ceSNikolay Aleksandrov 		if (ether_addr_equal_64bits(bond_dev->dev_addr,
23857d5cd2ceSNikolay Aleksandrov 					    slave_dev->dev_addr))
2386b8fad459SNikolay Aleksandrov 			eth_hw_addr_random(bond_dev);
23877d5cd2ceSNikolay Aleksandrov 		if (bond_dev->type != ARPHRD_ETHER) {
238840baec22SJay Vosburgh 			dev_close(bond_dev);
2389e667d469SNikolay Aleksandrov 			bond_ether_setup(bond_dev);
23907d5cd2ceSNikolay Aleksandrov 		}
23917d5cd2ceSNikolay Aleksandrov 	}
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds 	return res;
23941da177e4SLinus Torvalds }
23951da177e4SLinus Torvalds 
2396547942caSNikolay Aleksandrov /* Try to release the slave device <slave> from the bond device <master>
23971da177e4SLinus Torvalds  * It is legal to access curr_active_slave without a lock because all the function
23988c0bc550SNikolay Aleksandrov  * is RTNL-locked. If "all" is true it means that the function is being called
23990896341aSnikolay@redhat.com  * while destroying a bond interface and all slaves are being released.
24001da177e4SLinus Torvalds  *
24011da177e4SLinus Torvalds  * The rules for slave state should be:
24021da177e4SLinus Torvalds  *   for Active/Backup:
24031da177e4SLinus Torvalds  *     Active stays on all backups go down
24041da177e4SLinus Torvalds  *   for Bonded connections:
24051da177e4SLinus Torvalds  *     The first up interface should be left on and all others downed.
24061da177e4SLinus Torvalds  */
__bond_release_one(struct net_device * bond_dev,struct net_device * slave_dev,bool all,bool unregister)24070896341aSnikolay@redhat.com static int __bond_release_one(struct net_device *bond_dev,
24080896341aSnikolay@redhat.com 			      struct net_device *slave_dev,
2409f51048c3SWANG Cong 			      bool all, bool unregister)
24101da177e4SLinus Torvalds {
2411454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
24121da177e4SLinus Torvalds 	struct slave *slave, *oldcurrent;
2413faeeb317SJarod Wilson 	struct sockaddr_storage ss;
24145a0068deSNeil Horman 	int old_flags = bond_dev->flags;
2415c8f44affSMichał Mirosław 	netdev_features_t old_features = bond_dev->features;
24161da177e4SLinus Torvalds 
24171da177e4SLinus Torvalds 	/* slave is not a slave or master is not master of this slave */
24181da177e4SLinus Torvalds 	if (!(slave_dev->flags & IFF_SLAVE) ||
2419471cb5a3SJiri Pirko 	    !netdev_has_upper_dev(slave_dev, bond_dev)) {
2420e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "cannot release slave\n");
24211da177e4SLinus Torvalds 		return -EINVAL;
24221da177e4SLinus Torvalds 	}
24231da177e4SLinus Torvalds 
2424e843fa50SNeil Horman 	block_netpoll_tx();
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	slave = bond_get_slave_by_dev(bond, slave_dev);
24271da177e4SLinus Torvalds 	if (!slave) {
24281da177e4SLinus Torvalds 		/* not a slave of this bond */
2429e2a7420dSJarod Wilson 		slave_info(bond_dev, slave_dev, "interface not enslaved\n");
2430e843fa50SNeil Horman 		unblock_netpoll_tx();
24311da177e4SLinus Torvalds 		return -EINVAL;
24321da177e4SLinus Torvalds 	}
24331da177e4SLinus Torvalds 
243457beaca8SJiri Pirko 	bond_set_slave_inactive_flags(slave, BOND_SLAVE_NOTIFY_NOW);
243557beaca8SJiri Pirko 
243607699f9aSsfeldma@cumulusnetworks.com 	bond_sysfs_slave_del(slave);
243707699f9aSsfeldma@cumulusnetworks.com 
24385f0c5f73SAndy Gospodarek 	/* recompute stats just before removing the slave */
24395f0c5f73SAndy Gospodarek 	bond_get_stats(bond->dev, &bond->bond_stats);
24405f0c5f73SAndy Gospodarek 
24419e2ee5c7SJussi Maki 	if (bond->xdp_prog) {
24429e2ee5c7SJussi Maki 		struct netdev_bpf xdp = {
24439e2ee5c7SJussi Maki 			.command = XDP_SETUP_PROG,
24449e2ee5c7SJussi Maki 			.flags   = 0,
24459e2ee5c7SJussi Maki 			.prog	 = NULL,
24469e2ee5c7SJussi Maki 			.extack  = NULL,
24479e2ee5c7SJussi Maki 		};
24489e2ee5c7SJussi Maki 		if (slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp))
24499e2ee5c7SJussi Maki 			slave_warn(bond_dev, slave_dev, "failed to unload XDP program\n");
24509e2ee5c7SJussi Maki 	}
24519e2ee5c7SJussi Maki 
245235d48903SJiri Pirko 	/* unregister rx_handler early so bond_handle_frame wouldn't be called
245335d48903SJiri Pirko 	 * for this slave anymore.
245435d48903SJiri Pirko 	 */
245535d48903SJiri Pirko 	netdev_rx_handler_unregister(slave_dev);
245635d48903SJiri Pirko 
2457e470259fSNikolay Aleksandrov 	if (BOND_MODE(bond) == BOND_MODE_8023AD)
24581da177e4SLinus Torvalds 		bond_3ad_unbind_slave(slave);
24591da177e4SLinus Torvalds 
2460220ade77SYufeng Mo 	bond_upper_dev_unlink(bond, slave);
2461220ade77SYufeng Mo 
2462e79c1055SDebabrata Banerjee 	if (bond_mode_can_use_xmit_hash(bond))
2463ee637714SMahesh Bandewar 		bond_update_slave_arr(bond, slave);
2464ee637714SMahesh Bandewar 
2465e2a7420dSJarod Wilson 	slave_info(bond_dev, slave_dev, "Releasing %s interface\n",
2466e2a7420dSJarod Wilson 		    bond_is_active_slave(slave) ? "active" : "backup");
24671da177e4SLinus Torvalds 
24684740d638SEric Dumazet 	oldcurrent = rcu_access_pointer(bond->curr_active_slave);
24691da177e4SLinus Torvalds 
247085741718SEric Dumazet 	RCU_INIT_POINTER(bond->current_arp_slave, NULL);
24711da177e4SLinus Torvalds 
247200503b6fSdingtianhong 	if (!all && (!bond->params.fail_over_mac ||
247301844098SVeaceslav Falico 		     BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP)) {
2474844223abSdingtianhong 		if (ether_addr_equal_64bits(bond_dev->dev_addr, slave->perm_hwaddr) &&
24750965a1f3SVeaceslav Falico 		    bond_has_slaves(bond))
2476e2a7420dSJarod Wilson 			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",
2477e2a7420dSJarod Wilson 				   slave->perm_hwaddr);
2478dec1e90eSnikolay@redhat.com 	}
2479dec1e90eSnikolay@redhat.com 
2480059b47e8SNikolay Aleksandrov 	if (rtnl_dereference(bond->primary_slave) == slave)
2481059b47e8SNikolay Aleksandrov 		RCU_INIT_POINTER(bond->primary_slave, NULL);
24821da177e4SLinus Torvalds 
24831c72cfdcSNikolay Aleksandrov 	if (oldcurrent == slave)
24841da177e4SLinus Torvalds 		bond_change_active_slave(bond, NULL);
24851da177e4SLinus Torvalds 
24869ea6b70fSHangbin Liu 	/* Must be called after bond_change_active_slave () as the slave
24879ea6b70fSHangbin Liu 	 * might change from an active slave to a backup slave. Then it is
24889ea6b70fSHangbin Liu 	 * necessary to clear the maddrs on the backup slave.
24899ea6b70fSHangbin Liu 	 */
24909ea6b70fSHangbin Liu 	bond_slave_ns_maddrs_del(bond, slave);
24919ea6b70fSHangbin Liu 
249258402054SHolger Eitzenberger 	if (bond_is_lb(bond)) {
24931da177e4SLinus Torvalds 		/* Must be called only after the slave has been
24941da177e4SLinus Torvalds 		 * detached from the list and the curr_active_slave
24951da177e4SLinus Torvalds 		 * has been cleared (if our_slave == old_current),
24961da177e4SLinus Torvalds 		 * but before a new active slave is selected.
24971da177e4SLinus Torvalds 		 */
24981da177e4SLinus Torvalds 		bond_alb_deinit_slave(bond, slave);
24991da177e4SLinus Torvalds 	}
25001da177e4SLinus Torvalds 
25010896341aSnikolay@redhat.com 	if (all) {
250236708b89SPaul E. McKenney 		RCU_INIT_POINTER(bond->curr_active_slave, NULL);
25030896341aSnikolay@redhat.com 	} else if (oldcurrent == slave) {
2504547942caSNikolay Aleksandrov 		/* Note that we hold RTNL over this sequence, so there
2505059fe7a5SJay Vosburgh 		 * is no concern that another slave add/remove event
2506059fe7a5SJay Vosburgh 		 * will interfere.
2507059fe7a5SJay Vosburgh 		 */
25081da177e4SLinus Torvalds 		bond_select_active_slave(bond);
2509059fe7a5SJay Vosburgh 	}
2510059fe7a5SJay Vosburgh 
2511ff59c456SJay Vosburgh 	bond_set_carrier(bond);
2512a6ab75ceSZhang Changzhong 	if (!bond_has_slaves(bond))
2513409cc1f8SJiri Pirko 		eth_hw_addr_random(bond_dev);
25141da177e4SLinus Torvalds 
2515e843fa50SNeil Horman 	unblock_netpoll_tx();
2516278b2083Snikolay@redhat.com 	synchronize_rcu();
2517ee6154e1SNikolay Aleksandrov 	bond->slave_cnt--;
25181da177e4SLinus Torvalds 
25190965a1f3SVeaceslav Falico 	if (!bond_has_slaves(bond)) {
25202af73d4bSShlomo Pongratz 		call_netdevice_notifiers(NETDEV_CHANGEADDR, bond->dev);
252180028ea1SVeaceslav Falico 		call_netdevice_notifiers(NETDEV_RELEASE, bond->dev);
252280028ea1SVeaceslav Falico 	}
25232af73d4bSShlomo Pongratz 
2524b2a103e6SMichał Mirosław 	bond_compute_features(bond);
2525b2a103e6SMichał Mirosław 	if (!(bond_dev->features & NETIF_F_VLAN_CHALLENGED) &&
2526b2a103e6SMichał Mirosław 	    (old_features & NETIF_F_VLAN_CHALLENGED))
2527e2a7420dSJarod Wilson 		slave_info(bond_dev, slave_dev, "last VLAN challenged slave left bond - VLAN blocking is removed\n");
2528b2a103e6SMichał Mirosław 
25291ff412adSnikolay@redhat.com 	vlan_vids_del_by_dev(slave_dev, bond_dev);
25301da177e4SLinus Torvalds 
2531547942caSNikolay Aleksandrov 	/* If the mode uses primary, then this case was handled above by
2532303d1cbfSJay Vosburgh 	 * bond_change_active_slave(..., NULL)
25331da177e4SLinus Torvalds 	 */
2534ec0865a9SVeaceslav Falico 	if (!bond_uses_primary(bond)) {
25355a0068deSNeil Horman 		/* unset promiscuity level from slave
25365a0068deSNeil Horman 		 * NOTE: The NETDEV_CHANGEADDR call above may change the value
25375a0068deSNeil Horman 		 * of the IFF_PROMISC flag in the bond_dev, but we need the
25385a0068deSNeil Horman 		 * value of that flag before that change, as that was the value
25395a0068deSNeil Horman 		 * when this slave was attached, so we cache at the start of the
25405a0068deSNeil Horman 		 * function and use it here. Same goes for ALLMULTI below
25415a0068deSNeil Horman 		 */
25425a0068deSNeil Horman 		if (old_flags & IFF_PROMISC)
25431da177e4SLinus Torvalds 			dev_set_promiscuity(slave_dev, -1);
25441da177e4SLinus Torvalds 
25451da177e4SLinus Torvalds 		/* unset allmulti level from slave */
25465a0068deSNeil Horman 		if (old_flags & IFF_ALLMULTI)
25471da177e4SLinus Torvalds 			dev_set_allmulti(slave_dev, -1);
25481da177e4SLinus Torvalds 
254986247abaSBenjamin Poirier 		if (old_flags & IFF_UP)
2550303d1cbfSJay Vosburgh 			bond_hw_addr_flush(bond_dev, slave_dev);
25511da177e4SLinus Torvalds 	}
25521da177e4SLinus Torvalds 
25538a8efa22SAmerigo Wang 	slave_disable_netpoll(slave);
2554f6dc31a8SWANG Cong 
25551da177e4SLinus Torvalds 	/* close slave before restoring its mac address */
25561da177e4SLinus Torvalds 	dev_close(slave_dev);
25571da177e4SLinus Torvalds 
25588a321cf7SXin Long 	slave_dev->priv_flags &= ~IFF_NO_ADDRCONF;
25598a321cf7SXin Long 
256000503b6fSdingtianhong 	if (bond->params.fail_over_mac != BOND_FOM_ACTIVE ||
256101844098SVeaceslav Falico 	    BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
25621da177e4SLinus Torvalds 		/* restore original ("permanent") mac address */
2563faeeb317SJarod Wilson 		bond_hw_addr_copy(ss.__data, slave->perm_hwaddr,
2564faeeb317SJarod Wilson 				  slave->dev->addr_len);
2565faeeb317SJarod Wilson 		ss.ss_family = slave_dev->type;
25663a37a963SPetr Machata 		dev_set_mac_address(slave_dev, (struct sockaddr *)&ss, NULL);
25672ab82852SMoni Shoua 	}
25681da177e4SLinus Torvalds 
2569f51048c3SWANG Cong 	if (unregister)
2570f51048c3SWANG Cong 		__dev_set_mtu(slave_dev, slave->original_mtu);
2571f51048c3SWANG Cong 	else
2572b15ba0fbSJiri Pirko 		dev_set_mtu(slave_dev, slave->original_mtu);
2573b15ba0fbSJiri Pirko 
257465de65d9STaehee Yoo 	if (!netif_is_bond_master(slave_dev))
25752d7011caSJiri Pirko 		slave_dev->priv_flags &= ~IFF_BONDING;
25761da177e4SLinus Torvalds 
2577cb9e6e58SLorenzo Bianconi 	bond_xdp_set_features(bond_dev);
2578b9ad3e9fSJamie Iles 	kobject_put(&slave->kobj);
25791da177e4SLinus Torvalds 
2580547942caSNikolay Aleksandrov 	return 0;
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
25830896341aSnikolay@redhat.com /* A wrapper used because of ndo_del_link */
bond_release(struct net_device * bond_dev,struct net_device * slave_dev)25840896341aSnikolay@redhat.com int bond_release(struct net_device *bond_dev, struct net_device *slave_dev)
25850896341aSnikolay@redhat.com {
2586f51048c3SWANG Cong 	return __bond_release_one(bond_dev, slave_dev, false, false);
25870896341aSnikolay@redhat.com }
25880896341aSnikolay@redhat.com 
2589547942caSNikolay Aleksandrov /* First release a slave and then destroy the bond if no more slaves are left.
2590d90a162aSMoni Shoua  * Must be under rtnl_lock when this function is called.
2591d90a162aSMoni Shoua  */
bond_release_and_destroy(struct net_device * bond_dev,struct net_device * slave_dev)259226d8ee75Sstephen hemminger static int bond_release_and_destroy(struct net_device *bond_dev,
25933d632c3fSStephen Hemminger 				    struct net_device *slave_dev)
2594d90a162aSMoni Shoua {
2595454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
2596d90a162aSMoni Shoua 	int ret;
2597d90a162aSMoni Shoua 
2598f51048c3SWANG Cong 	ret = __bond_release_one(bond_dev, slave_dev, false, true);
259983270702SCong Wang 	if (ret == 0 && !bond_has_slaves(bond) &&
260083270702SCong Wang 	    bond_dev->reg_state != NETREG_UNREGISTERING) {
26018a8efa22SAmerigo Wang 		bond_dev->priv_flags |= IFF_DISABLE_NETPOLL;
2602e2a7420dSJarod Wilson 		netdev_info(bond_dev, "Destroying bond\n");
260306f6d109SNikolay Aleksandrov 		bond_remove_proc_entry(bond);
26049e71626cSStephen Hemminger 		unregister_netdevice(bond_dev);
2605d90a162aSMoni Shoua 	}
2606d90a162aSMoni Shoua 	return ret;
2607d90a162aSMoni Shoua }
2608d90a162aSMoni Shoua 
bond_info_query(struct net_device * bond_dev,struct ifbond * info)26093d67576dSZhu Yanjun static void bond_info_query(struct net_device *bond_dev, struct ifbond *info)
26101da177e4SLinus Torvalds {
2611454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
261286a5ad0aSYufeng Mo 
261369a2338eSMoni Shoua 	bond_fill_ifbond(bond, info);
26141da177e4SLinus Torvalds }
26151da177e4SLinus Torvalds 
bond_slave_info_query(struct net_device * bond_dev,struct ifslave * info)26161da177e4SLinus Torvalds static int bond_slave_info_query(struct net_device *bond_dev, struct ifslave *info)
26171da177e4SLinus Torvalds {
2618454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
26199caff1e7SVeaceslav Falico 	struct list_head *iter;
2620dec1e90eSnikolay@redhat.com 	int i = 0, res = -ENODEV;
26211da177e4SLinus Torvalds 	struct slave *slave;
26221da177e4SLinus Torvalds 
26239caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
2624dec1e90eSnikolay@redhat.com 		if (i++ == (int)info->slave_id) {
2625689c96ccSEric Dumazet 			res = 0;
262669a2338eSMoni Shoua 			bond_fill_ifslave(slave, info);
26271da177e4SLinus Torvalds 			break;
26281da177e4SLinus Torvalds 		}
26291da177e4SLinus Torvalds 	}
26301da177e4SLinus Torvalds 
2631689c96ccSEric Dumazet 	return res;
26321da177e4SLinus Torvalds }
26331da177e4SLinus Torvalds 
26341da177e4SLinus Torvalds /*-------------------------------- Monitoring -------------------------------*/
26351da177e4SLinus Torvalds 
26364740d638SEric Dumazet /* called with rcu_read_lock() */
bond_miimon_inspect(struct bonding * bond)2637f0c76d61SJay Vosburgh static int bond_miimon_inspect(struct bonding *bond)
26381da177e4SLinus Torvalds {
2639e5214f36SDan Carpenter 	bool ignore_updelay = false;
2640dec1e90eSnikolay@redhat.com 	int link_state, commit = 0;
26419caff1e7SVeaceslav Falico 	struct list_head *iter;
2642f0c76d61SJay Vosburgh 	struct slave *slave;
264341f89100SJiri Pirko 
2644f8a65ab2SJonathan Toppins 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP) {
26454740d638SEric Dumazet 		ignore_updelay = !rcu_dereference(bond->curr_active_slave);
2646f8a65ab2SJonathan Toppins 	} else {
2647f8a65ab2SJonathan Toppins 		struct bond_up_slave *usable_slaves;
2648f8a65ab2SJonathan Toppins 
2649f8a65ab2SJonathan Toppins 		usable_slaves = rcu_dereference(bond->usable_slaves);
2650f8a65ab2SJonathan Toppins 
2651f8a65ab2SJonathan Toppins 		if (usable_slaves && usable_slaves->count == 0)
2652f8a65ab2SJonathan Toppins 			ignore_updelay = true;
2653f8a65ab2SJonathan Toppins 	}
26541da177e4SLinus Torvalds 
26554cb4f97bSdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
26561899bb32SJay Vosburgh 		bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
26571da177e4SLinus Torvalds 
2658f0c76d61SJay Vosburgh 		link_state = bond_check_dev_link(bond, slave->dev, 0);
26591da177e4SLinus Torvalds 
26601da177e4SLinus Torvalds 		switch (slave->link) {
2661f0c76d61SJay Vosburgh 		case BOND_LINK_UP:
2662f0c76d61SJay Vosburgh 			if (link_state)
2663f0c76d61SJay Vosburgh 				continue;
2664f0c76d61SJay Vosburgh 
2665de77ecd4SMahesh Bandewar 			bond_propose_link_state(slave, BOND_LINK_FAIL);
2666d94708a5SWANG Cong 			commit++;
26671da177e4SLinus Torvalds 			slave->delay = bond->params.downdelay;
2668f0c76d61SJay Vosburgh 			if (slave->delay) {
2669e2a7420dSJarod Wilson 				slave_info(bond->dev, slave->dev, "link status down for %sinterface, disabling it in %d ms\n",
267001844098SVeaceslav Falico 					   (BOND_MODE(bond) ==
2671f0c76d61SJay Vosburgh 					    BOND_MODE_ACTIVEBACKUP) ?
2672e30bc066SJiri Pirko 					    (bond_is_active_slave(slave) ?
2673f0c76d61SJay Vosburgh 					     "active " : "backup ") : "",
26741da177e4SLinus Torvalds 					   bond->params.downdelay * bond->params.miimon);
26751da177e4SLinus Torvalds 			}
2676df561f66SGustavo A. R. Silva 			fallthrough;
2677f0c76d61SJay Vosburgh 		case BOND_LINK_FAIL:
2678f0c76d61SJay Vosburgh 			if (link_state) {
2679547942caSNikolay Aleksandrov 				/* recovered before downdelay expired */
2680de77ecd4SMahesh Bandewar 				bond_propose_link_state(slave, BOND_LINK_UP);
26818e603460SVeaceslav Falico 				slave->last_link_up = jiffies;
2682e2a7420dSJarod Wilson 				slave_info(bond->dev, slave->dev, "link status up again after %d ms\n",
2683f0c76d61SJay Vosburgh 					   (bond->params.downdelay - slave->delay) *
2684e2a7420dSJarod Wilson 					   bond->params.miimon);
2685fb9eb899SMahesh Bandewar 				commit++;
2686f0c76d61SJay Vosburgh 				continue;
26871da177e4SLinus Torvalds 			}
2688f0c76d61SJay Vosburgh 
2689f0c76d61SJay Vosburgh 			if (slave->delay <= 0) {
26901899bb32SJay Vosburgh 				bond_propose_link_state(slave, BOND_LINK_DOWN);
2691f0c76d61SJay Vosburgh 				commit++;
2692f0c76d61SJay Vosburgh 				continue;
2693f0c76d61SJay Vosburgh 			}
2694f0c76d61SJay Vosburgh 
2695f0c76d61SJay Vosburgh 			slave->delay--;
26961da177e4SLinus Torvalds 			break;
2697f0c76d61SJay Vosburgh 
2698f0c76d61SJay Vosburgh 		case BOND_LINK_DOWN:
2699f0c76d61SJay Vosburgh 			if (!link_state)
2700f0c76d61SJay Vosburgh 				continue;
2701f0c76d61SJay Vosburgh 
2702de77ecd4SMahesh Bandewar 			bond_propose_link_state(slave, BOND_LINK_BACK);
2703d94708a5SWANG Cong 			commit++;
27041da177e4SLinus Torvalds 			slave->delay = bond->params.updelay;
27051da177e4SLinus Torvalds 
2706f0c76d61SJay Vosburgh 			if (slave->delay) {
2707e2a7420dSJarod Wilson 				slave_info(bond->dev, slave->dev, "link status up, enabling it in %d ms\n",
270841f89100SJiri Pirko 					   ignore_updelay ? 0 :
2709f0c76d61SJay Vosburgh 					   bond->params.updelay *
2710f0c76d61SJay Vosburgh 					   bond->params.miimon);
27111da177e4SLinus Torvalds 			}
2712df561f66SGustavo A. R. Silva 			fallthrough;
2713f0c76d61SJay Vosburgh 		case BOND_LINK_BACK:
2714f0c76d61SJay Vosburgh 			if (!link_state) {
2715de77ecd4SMahesh Bandewar 				bond_propose_link_state(slave, BOND_LINK_DOWN);
2716e2a7420dSJarod Wilson 				slave_info(bond->dev, slave->dev, "link status down again after %d ms\n",
2717f0c76d61SJay Vosburgh 					   (bond->params.updelay - slave->delay) *
2718e2a7420dSJarod Wilson 					   bond->params.miimon);
2719fb9eb899SMahesh Bandewar 				commit++;
2720f0c76d61SJay Vosburgh 				continue;
2721f0c76d61SJay Vosburgh 			}
2722f0c76d61SJay Vosburgh 
272341f89100SJiri Pirko 			if (ignore_updelay)
272441f89100SJiri Pirko 				slave->delay = 0;
272541f89100SJiri Pirko 
2726f0c76d61SJay Vosburgh 			if (slave->delay <= 0) {
27271899bb32SJay Vosburgh 				bond_propose_link_state(slave, BOND_LINK_UP);
2728f0c76d61SJay Vosburgh 				commit++;
272941f89100SJiri Pirko 				ignore_updelay = false;
2730f0c76d61SJay Vosburgh 				continue;
2731f0c76d61SJay Vosburgh 			}
2732f0c76d61SJay Vosburgh 
2733f0c76d61SJay Vosburgh 			slave->delay--;
2734f0c76d61SJay Vosburgh 			break;
2735f0c76d61SJay Vosburgh 		}
2736f0c76d61SJay Vosburgh 	}
2737f0c76d61SJay Vosburgh 
2738f0c76d61SJay Vosburgh 	return commit;
2739f0c76d61SJay Vosburgh }
2740f0c76d61SJay Vosburgh 
bond_miimon_link_change(struct bonding * bond,struct slave * slave,char link)27417e878b60STonghao Zhang static void bond_miimon_link_change(struct bonding *bond,
27427e878b60STonghao Zhang 				    struct slave *slave,
27437e878b60STonghao Zhang 				    char link)
27447e878b60STonghao Zhang {
27457e878b60STonghao Zhang 	switch (BOND_MODE(bond)) {
27467e878b60STonghao Zhang 	case BOND_MODE_8023AD:
27477e878b60STonghao Zhang 		bond_3ad_handle_link_change(slave, link);
27487e878b60STonghao Zhang 		break;
27497e878b60STonghao Zhang 	case BOND_MODE_TLB:
27507e878b60STonghao Zhang 	case BOND_MODE_ALB:
27517e878b60STonghao Zhang 		bond_alb_handle_link_change(bond, slave, link);
27527e878b60STonghao Zhang 		break;
27537e878b60STonghao Zhang 	case BOND_MODE_XOR:
27547e878b60STonghao Zhang 		bond_update_slave_arr(bond, NULL);
27557e878b60STonghao Zhang 		break;
27567e878b60STonghao Zhang 	}
27577e878b60STonghao Zhang }
27587e878b60STonghao Zhang 
bond_miimon_commit(struct bonding * bond)2759f0c76d61SJay Vosburgh static void bond_miimon_commit(struct bonding *bond)
2760f0c76d61SJay Vosburgh {
276142c7ded0SEric Dumazet 	struct slave *slave, *primary, *active;
2762e95cc447SHangbin Liu 	bool do_failover = false;
2763e95cc447SHangbin Liu 	struct list_head *iter;
2764f0c76d61SJay Vosburgh 
276542c7ded0SEric Dumazet 	ASSERT_RTNL();
276642c7ded0SEric Dumazet 
27679caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
27681899bb32SJay Vosburgh 		switch (slave->link_new_state) {
2769f0c76d61SJay Vosburgh 		case BOND_LINK_NOCHANGE:
277012185dfeSThomas Falcon 			/* For 802.3ad mode, check current slave speed and
277112185dfeSThomas Falcon 			 * duplex again in case its port was disabled after
277212185dfeSThomas Falcon 			 * invalid speed/duplex reporting but recovered before
277312185dfeSThomas Falcon 			 * link monitoring could make a decision on the actual
277412185dfeSThomas Falcon 			 * link status
277512185dfeSThomas Falcon 			 */
277612185dfeSThomas Falcon 			if (BOND_MODE(bond) == BOND_MODE_8023AD &&
277712185dfeSThomas Falcon 			    slave->link == BOND_LINK_UP)
277812185dfeSThomas Falcon 				bond_3ad_adapter_speed_duplex_changed(slave);
2779f0c76d61SJay Vosburgh 			continue;
2780f0c76d61SJay Vosburgh 
2781f0c76d61SJay Vosburgh 		case BOND_LINK_UP:
2782ad729bc9SAndreas Born 			if (bond_update_speed_duplex(slave) &&
2783ad729bc9SAndreas Born 			    bond_needs_speed_duplex(bond)) {
27843f3c278cSMahesh Bandewar 				slave->link = BOND_LINK_DOWN;
278511e9d782SAndreas Born 				if (net_ratelimit())
2786e2a7420dSJarod Wilson 					slave_warn(bond->dev, slave->dev,
2787e2a7420dSJarod Wilson 						   "failed to get link speed/duplex\n");
2788b5bf0f5bSMahesh Bandewar 				continue;
2789b5bf0f5bSMahesh Bandewar 			}
27905d397061SJiri Pirko 			bond_set_slave_link_state(slave, BOND_LINK_UP,
27915d397061SJiri Pirko 						  BOND_SLAVE_NOTIFY_NOW);
27928e603460SVeaceslav Falico 			slave->last_link_up = jiffies;
27931da177e4SLinus Torvalds 
2794059b47e8SNikolay Aleksandrov 			primary = rtnl_dereference(bond->primary_slave);
279501844098SVeaceslav Falico 			if (BOND_MODE(bond) == BOND_MODE_8023AD) {
27961da177e4SLinus Torvalds 				/* prevent it from being the active one */
2797e30bc066SJiri Pirko 				bond_set_backup_slave(slave);
279801844098SVeaceslav Falico 			} else if (BOND_MODE(bond) != BOND_MODE_ACTIVEBACKUP) {
27991da177e4SLinus Torvalds 				/* make it immediately active */
2800e30bc066SJiri Pirko 				bond_set_active_slave(slave);
28011da177e4SLinus Torvalds 			}
28021da177e4SLinus Torvalds 
2803e2a7420dSJarod Wilson 			slave_info(bond->dev, slave->dev, "link status definitely up, %u Mbps %s duplex\n",
2804db4e9b2bSNikolay Aleksandrov 				   slave->speed == SPEED_UNKNOWN ? 0 : slave->speed,
2805db4e9b2bSNikolay Aleksandrov 				   slave->duplex ? "full" : "half");
28061da177e4SLinus Torvalds 
28077e878b60STonghao Zhang 			bond_miimon_link_change(bond, slave, BOND_LINK_UP);
2808ee637714SMahesh Bandewar 
280942c7ded0SEric Dumazet 			active = rtnl_dereference(bond->curr_active_slave);
281042c7ded0SEric Dumazet 			if (!active || slave == primary || slave->prio > active->prio)
2811e95cc447SHangbin Liu 				do_failover = true;
2812f0c76d61SJay Vosburgh 
2813f0c76d61SJay Vosburgh 			continue;
2814f0c76d61SJay Vosburgh 
2815f0c76d61SJay Vosburgh 		case BOND_LINK_DOWN:
2816fba4acdaSJay Vosburgh 			if (slave->link_failure_count < UINT_MAX)
2817fba4acdaSJay Vosburgh 				slave->link_failure_count++;
2818fba4acdaSJay Vosburgh 
28195d397061SJiri Pirko 			bond_set_slave_link_state(slave, BOND_LINK_DOWN,
28205d397061SJiri Pirko 						  BOND_SLAVE_NOTIFY_NOW);
2821f0c76d61SJay Vosburgh 
282201844098SVeaceslav Falico 			if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP ||
282301844098SVeaceslav Falico 			    BOND_MODE(bond) == BOND_MODE_8023AD)
28245e5b0665Sdingtianhong 				bond_set_slave_inactive_flags(slave,
28255e5b0665Sdingtianhong 							      BOND_SLAVE_NOTIFY_NOW);
2826f0c76d61SJay Vosburgh 
2827e2a7420dSJarod Wilson 			slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
2828f0c76d61SJay Vosburgh 
28297e878b60STonghao Zhang 			bond_miimon_link_change(bond, slave, BOND_LINK_DOWN);
2830ee637714SMahesh Bandewar 
28314740d638SEric Dumazet 			if (slave == rcu_access_pointer(bond->curr_active_slave))
2832e95cc447SHangbin Liu 				do_failover = true;
2833f0c76d61SJay Vosburgh 
2834f0c76d61SJay Vosburgh 			continue;
2835f0c76d61SJay Vosburgh 
28361da177e4SLinus Torvalds 		default:
2837e2a7420dSJarod Wilson 			slave_err(bond->dev, slave->dev, "invalid new link %d on slave\n",
28381899bb32SJay Vosburgh 				  slave->link_new_state);
28391899bb32SJay Vosburgh 			bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
28401da177e4SLinus Torvalds 
2841f0c76d61SJay Vosburgh 			continue;
28421da177e4SLinus Torvalds 		}
2843e95cc447SHangbin Liu 	}
28441da177e4SLinus Torvalds 
2845e95cc447SHangbin Liu 	if (do_failover) {
2846e843fa50SNeil Horman 		block_netpoll_tx();
28471da177e4SLinus Torvalds 		bond_select_active_slave(bond);
2848e843fa50SNeil Horman 		unblock_netpoll_tx();
2849f0c76d61SJay Vosburgh 	}
2850059fe7a5SJay Vosburgh 
2851ff59c456SJay Vosburgh 	bond_set_carrier(bond);
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
2854547942caSNikolay Aleksandrov /* bond_mii_monitor
28550b0eef66SJay Vosburgh  *
28560b0eef66SJay Vosburgh  * Really a wrapper that splits the mii monitor into two phases: an
2857f0c76d61SJay Vosburgh  * inspection, then (if inspection indicates something needs to be done)
2858f0c76d61SJay Vosburgh  * an acquisition of appropriate locks followed by a commit phase to
2859f0c76d61SJay Vosburgh  * implement whatever link state changes are indicated.
28600b0eef66SJay Vosburgh  */
bond_mii_monitor(struct work_struct * work)28616da67d26Sstephen hemminger static void bond_mii_monitor(struct work_struct *work)
28620b0eef66SJay Vosburgh {
28630b0eef66SJay Vosburgh 	struct bonding *bond = container_of(work, struct bonding,
28640b0eef66SJay Vosburgh 					    mii_work.work);
2865ad246c99SBen Hutchings 	bool should_notify_peers = false;
286607a4ddecSVincent Bernat 	bool commit;
28671f2cd845SDavid S. Miller 	unsigned long delay;
2868de77ecd4SMahesh Bandewar 	struct slave *slave;
2869de77ecd4SMahesh Bandewar 	struct list_head *iter;
28700b0eef66SJay Vosburgh 
28711f2cd845SDavid S. Miller 	delay = msecs_to_jiffies(bond->params.miimon);
28721f2cd845SDavid S. Miller 
28731f2cd845SDavid S. Miller 	if (!bond_has_slaves(bond))
28746b6c5261Sdingtianhong 		goto re_arm;
28756b6c5261Sdingtianhong 
28764cb4f97bSdingtianhong 	rcu_read_lock();
2877ad246c99SBen Hutchings 	should_notify_peers = bond_should_notify_peers(bond);
287807a4ddecSVincent Bernat 	commit = !!bond_miimon_inspect(bond);
287907a4ddecSVincent Bernat 	if (bond->send_peer_notif) {
28804cb4f97bSdingtianhong 		rcu_read_unlock();
288107a4ddecSVincent Bernat 		if (rtnl_trylock()) {
288207a4ddecSVincent Bernat 			bond->send_peer_notif--;
288307a4ddecSVincent Bernat 			rtnl_unlock();
288407a4ddecSVincent Bernat 		}
288507a4ddecSVincent Bernat 	} else {
288607a4ddecSVincent Bernat 		rcu_read_unlock();
288707a4ddecSVincent Bernat 	}
28881f2cd845SDavid S. Miller 
288907a4ddecSVincent Bernat 	if (commit) {
28901f2cd845SDavid S. Miller 		/* Race avoidance with bond_close cancel of workqueue */
28911f2cd845SDavid S. Miller 		if (!rtnl_trylock()) {
28921f2cd845SDavid S. Miller 			delay = 1;
28931f2cd845SDavid S. Miller 			should_notify_peers = false;
28941f2cd845SDavid S. Miller 			goto re_arm;
28951f2cd845SDavid S. Miller 		}
28961f2cd845SDavid S. Miller 
2897de77ecd4SMahesh Bandewar 		bond_for_each_slave(bond, slave, iter) {
2898de77ecd4SMahesh Bandewar 			bond_commit_link_state(slave, BOND_SLAVE_NOTIFY_LATER);
2899de77ecd4SMahesh Bandewar 		}
2900f0c76d61SJay Vosburgh 		bond_miimon_commit(bond);
2901f0c76d61SJay Vosburgh 
29021f2cd845SDavid S. Miller 		rtnl_unlock();	/* might sleep, hold no other locks */
290307a4ddecSVincent Bernat 	}
29040b0eef66SJay Vosburgh 
2905f0c76d61SJay Vosburgh re_arm:
2906e6d265e8SJay Vosburgh 	if (bond->params.miimon)
29071f2cd845SDavid S. Miller 		queue_delayed_work(bond->wq, &bond->mii_work, delay);
29081f2cd845SDavid S. Miller 
29091f2cd845SDavid S. Miller 	if (should_notify_peers) {
29101f2cd845SDavid S. Miller 		if (!rtnl_trylock())
29111f2cd845SDavid S. Miller 			return;
29121f2cd845SDavid S. Miller 		call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, bond->dev);
29131f2cd845SDavid S. Miller 		rtnl_unlock();
29141f2cd845SDavid S. Miller 	}
29150b0eef66SJay Vosburgh }
2916c3ade5caSJay Vosburgh 
bond_upper_dev_walk(struct net_device * upper,struct netdev_nested_priv * priv)2917eff74233STaehee Yoo static int bond_upper_dev_walk(struct net_device *upper,
2918eff74233STaehee Yoo 			       struct netdev_nested_priv *priv)
2919b3208b20SDavid Ahern {
2920eff74233STaehee Yoo 	__be32 ip = *(__be32 *)priv->data;
2921b3208b20SDavid Ahern 
2922b3208b20SDavid Ahern 	return ip == bond_confirm_addr(upper, 0, ip);
2923b3208b20SDavid Ahern }
2924b3208b20SDavid Ahern 
bond_has_this_ip(struct bonding * bond,__be32 ip)292550223ce4SVeaceslav Falico static bool bond_has_this_ip(struct bonding *bond, __be32 ip)
2926f5b2b966SJay Vosburgh {
2927eff74233STaehee Yoo 	struct netdev_nested_priv priv = {
2928eff74233STaehee Yoo 		.data = (void *)&ip,
2929eff74233STaehee Yoo 	};
293050223ce4SVeaceslav Falico 	bool ret = false;
2931f5b2b966SJay Vosburgh 
2932eaddcd76SAndy Gospodarek 	if (ip == bond_confirm_addr(bond->dev, 0, ip))
293350223ce4SVeaceslav Falico 		return true;
2934f5b2b966SJay Vosburgh 
2935eaddcd76SAndy Gospodarek 	rcu_read_lock();
2936eff74233STaehee Yoo 	if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_upper_dev_walk, &priv))
293750223ce4SVeaceslav Falico 		ret = true;
293850223ce4SVeaceslav Falico 	rcu_read_unlock();
2939f5b2b966SJay Vosburgh 
294050223ce4SVeaceslav Falico 	return ret;
2941f5b2b966SJay Vosburgh }
2942f5b2b966SJay Vosburgh 
2943c1bc7d73SSimon Horman #define BOND_VLAN_PROTO_NONE cpu_to_be16(0xffff)
2944c1bc7d73SSimon Horman 
bond_handle_vlan(struct slave * slave,struct bond_vlan_tag * tags,struct sk_buff * skb)29451fcd5d44SHangbin Liu static bool bond_handle_vlan(struct slave *slave, struct bond_vlan_tag *tags,
29461fcd5d44SHangbin Liu 			     struct sk_buff *skb)
2947c3ade5caSJay Vosburgh {
2948e2a7420dSJarod Wilson 	struct net_device *bond_dev = slave->bond->dev;
29491fcd5d44SHangbin Liu 	struct net_device *slave_dev = slave->dev;
29501fcd5d44SHangbin Liu 	struct bond_vlan_tag *outer_tag = tags;
295144a40855SVlad Yasevich 
2952c1bc7d73SSimon Horman 	if (!tags || tags->vlan_proto == BOND_VLAN_PROTO_NONE)
29531fcd5d44SHangbin Liu 		return true;
29543e403a77SVeaceslav Falico 
29553e403a77SVeaceslav Falico 	tags++;
29563e403a77SVeaceslav Falico 
295744a40855SVlad Yasevich 	/* Go through all the tags backwards and add them to the packet */
2958c1bc7d73SSimon Horman 	while (tags->vlan_proto != BOND_VLAN_PROTO_NONE) {
29593e403a77SVeaceslav Falico 		if (!tags->vlan_id) {
29603e403a77SVeaceslav Falico 			tags++;
296144a40855SVlad Yasevich 			continue;
29623e403a77SVeaceslav Falico 		}
296344a40855SVlad Yasevich 
2964e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "inner tag: proto %X vid %X\n",
29653e403a77SVeaceslav Falico 			  ntohs(outer_tag->vlan_proto), tags->vlan_id);
296662749e2cSJiri Pirko 		skb = vlan_insert_tag_set_proto(skb, tags->vlan_proto,
29673e403a77SVeaceslav Falico 						tags->vlan_id);
2968c3ade5caSJay Vosburgh 		if (!skb) {
29694873ac3cSdingtianhong 			net_err_ratelimited("failed to insert inner VLAN tag\n");
29701fcd5d44SHangbin Liu 			return false;
2971fbd929f2Sdingtianhong 		}
29723e403a77SVeaceslav Falico 
29733e403a77SVeaceslav Falico 		tags++;
2974fbd929f2Sdingtianhong 	}
297544a40855SVlad Yasevich 	/* Set the outer tag */
29763e403a77SVeaceslav Falico 	if (outer_tag->vlan_id) {
2977e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave_dev, "outer tag: proto %X vid %X\n",
29783e403a77SVeaceslav Falico 			  ntohs(outer_tag->vlan_proto), outer_tag->vlan_id);
2979b4bef1b5SJiri Pirko 		__vlan_hwaccel_put_tag(skb, outer_tag->vlan_proto,
29803e403a77SVeaceslav Falico 				       outer_tag->vlan_id);
2981c3ade5caSJay Vosburgh 	}
29823e403a77SVeaceslav Falico 
29831fcd5d44SHangbin Liu 	return true;
29841fcd5d44SHangbin Liu }
29851fcd5d44SHangbin Liu 
29861fcd5d44SHangbin Liu /* We go to the (large) trouble of VLAN tagging ARP frames because
29871fcd5d44SHangbin Liu  * switches in VLAN mode (especially if ports are configured as
29881fcd5d44SHangbin Liu  * "native" to a VLAN) might not pass non-tagged frames.
29891fcd5d44SHangbin Liu  */
bond_arp_send(struct slave * slave,int arp_op,__be32 dest_ip,__be32 src_ip,struct bond_vlan_tag * tags)29901fcd5d44SHangbin Liu static void bond_arp_send(struct slave *slave, int arp_op, __be32 dest_ip,
29911fcd5d44SHangbin Liu 			  __be32 src_ip, struct bond_vlan_tag *tags)
29921fcd5d44SHangbin Liu {
29931fcd5d44SHangbin Liu 	struct net_device *bond_dev = slave->bond->dev;
29941fcd5d44SHangbin Liu 	struct net_device *slave_dev = slave->dev;
29951fcd5d44SHangbin Liu 	struct sk_buff *skb;
29961fcd5d44SHangbin Liu 
29971fcd5d44SHangbin Liu 	slave_dbg(bond_dev, slave_dev, "arp %d on slave: dst %pI4 src %pI4\n",
29981fcd5d44SHangbin Liu 		  arp_op, &dest_ip, &src_ip);
29991fcd5d44SHangbin Liu 
30001fcd5d44SHangbin Liu 	skb = arp_create(arp_op, ETH_P_ARP, dest_ip, slave_dev, src_ip,
30011fcd5d44SHangbin Liu 			 NULL, slave_dev->dev_addr, NULL);
30021fcd5d44SHangbin Liu 
30031fcd5d44SHangbin Liu 	if (!skb) {
30041fcd5d44SHangbin Liu 		net_err_ratelimited("ARP packet allocation failed\n");
30051fcd5d44SHangbin Liu 		return;
30061fcd5d44SHangbin Liu 	}
30071fcd5d44SHangbin Liu 
300806799a90SVladimir Oltean 	if (bond_handle_vlan(slave, tags, skb)) {
300906799a90SVladimir Oltean 		slave_update_last_tx(slave);
3010c3ade5caSJay Vosburgh 		arp_xmit(skb);
301106799a90SVladimir Oltean 	}
301206799a90SVladimir Oltean 
30131fcd5d44SHangbin Liu 	return;
3014c3ade5caSJay Vosburgh }
3015c3ade5caSJay Vosburgh 
301644a40855SVlad Yasevich /* Validate the device path between the @start_dev and the @end_dev.
301744a40855SVlad Yasevich  * The path is valid if the @end_dev is reachable through device
301844a40855SVlad Yasevich  * stacking.
301944a40855SVlad Yasevich  * When the path is validated, collect any vlan information in the
302044a40855SVlad Yasevich  * path.
302144a40855SVlad Yasevich  */
bond_verify_device_path(struct net_device * start_dev,struct net_device * end_dev,int level)30223e403a77SVeaceslav Falico struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,
302344a40855SVlad Yasevich 					      struct net_device *end_dev,
30243e403a77SVeaceslav Falico 					      int level)
302544a40855SVlad Yasevich {
30263e403a77SVeaceslav Falico 	struct bond_vlan_tag *tags;
302744a40855SVlad Yasevich 	struct net_device *upper;
302844a40855SVlad Yasevich 	struct list_head  *iter;
302944a40855SVlad Yasevich 
30303e403a77SVeaceslav Falico 	if (start_dev == end_dev) {
30316396bb22SKees Cook 		tags = kcalloc(level + 1, sizeof(*tags), GFP_ATOMIC);
3032c9ac9476SZhengchao Shao 		if (!tags)
30333e403a77SVeaceslav Falico 			return ERR_PTR(-ENOMEM);
3034c1bc7d73SSimon Horman 		tags[level].vlan_proto = BOND_VLAN_PROTO_NONE;
30353e403a77SVeaceslav Falico 		return tags;
30363e403a77SVeaceslav Falico 	}
303744a40855SVlad Yasevich 
303844a40855SVlad Yasevich 	netdev_for_each_upper_dev_rcu(start_dev, upper, iter) {
30393e403a77SVeaceslav Falico 		tags = bond_verify_device_path(upper, end_dev, level + 1);
30403e403a77SVeaceslav Falico 		if (IS_ERR_OR_NULL(tags)) {
30413e403a77SVeaceslav Falico 			if (IS_ERR(tags))
30423e403a77SVeaceslav Falico 				return tags;
30433e403a77SVeaceslav Falico 			continue;
30443e403a77SVeaceslav Falico 		}
304544a40855SVlad Yasevich 		if (is_vlan_dev(upper)) {
30463e403a77SVeaceslav Falico 			tags[level].vlan_proto = vlan_dev_vlan_proto(upper);
30473e403a77SVeaceslav Falico 			tags[level].vlan_id = vlan_dev_vlan_id(upper);
304844a40855SVlad Yasevich 		}
304944a40855SVlad Yasevich 
30503e403a77SVeaceslav Falico 		return tags;
30513e403a77SVeaceslav Falico 	}
30523e403a77SVeaceslav Falico 
30533e403a77SVeaceslav Falico 	return NULL;
305444a40855SVlad Yasevich }
3055c3ade5caSJay Vosburgh 
bond_arp_send_all(struct bonding * bond,struct slave * slave)30561da177e4SLinus Torvalds static void bond_arp_send_all(struct bonding *bond, struct slave *slave)
30571da177e4SLinus Torvalds {
3058c3ade5caSJay Vosburgh 	struct rtable *rt;
30593e403a77SVeaceslav Falico 	struct bond_vlan_tag *tags;
306027bc11e6SVeaceslav Falico 	__be32 *targets = bond->params.arp_targets, addr;
3061fbd929f2Sdingtianhong 	int i;
30621da177e4SLinus Torvalds 
306327bc11e6SVeaceslav Falico 	for (i = 0; i < BOND_MAX_ARP_TARGETS && targets[i]; i++) {
3064e2a7420dSJarod Wilson 		slave_dbg(bond->dev, slave->dev, "%s: target %pI4\n",
3065e2a7420dSJarod Wilson 			  __func__, &targets[i]);
30663e403a77SVeaceslav Falico 		tags = NULL;
3067c3ade5caSJay Vosburgh 
306827bc11e6SVeaceslav Falico 		/* Find out through which dev should the packet go */
306978fbfd8aSDavid S. Miller 		rt = ip_route_output(dev_net(bond->dev), targets[i], 0,
307078fbfd8aSDavid S. Miller 				     RTO_ONLINK, 0);
3071b23dd4feSDavid S. Miller 		if (IS_ERR(rt)) {
307228572760SVeaceslav Falico 			/* there's no route to target - try to send arp
307328572760SVeaceslav Falico 			 * probe to generate any traffic (arp_validate=0)
307428572760SVeaceslav Falico 			 */
30754873ac3cSdingtianhong 			if (bond->params.arp_validate)
30760a4fd8dfSDavid Decotigny 				pr_warn_once("%s: no route to arp_ip_target %pI4 and arp_validate is set\n",
30774873ac3cSdingtianhong 					     bond->dev->name,
30784873ac3cSdingtianhong 					     &targets[i]);
3079e2a7420dSJarod Wilson 			bond_arp_send(slave, ARPOP_REQUEST, targets[i],
308044a40855SVlad Yasevich 				      0, tags);
3081c3ade5caSJay Vosburgh 			continue;
3082c3ade5caSJay Vosburgh 		}
3083c3ade5caSJay Vosburgh 
308427bc11e6SVeaceslav Falico 		/* bond device itself */
308527bc11e6SVeaceslav Falico 		if (rt->dst.dev == bond->dev)
308627bc11e6SVeaceslav Falico 			goto found;
308727bc11e6SVeaceslav Falico 
3088cc0e4070SJiri Pirko 		rcu_read_lock();
30893e403a77SVeaceslav Falico 		tags = bond_verify_device_path(bond->dev, rt->dst.dev, 0);
309044a40855SVlad Yasevich 		rcu_read_unlock();
3091fbd929f2Sdingtianhong 
30923e403a77SVeaceslav Falico 		if (!IS_ERR_OR_NULL(tags))
3093fbd929f2Sdingtianhong 			goto found;
309427bc11e6SVeaceslav Falico 
309527bc11e6SVeaceslav Falico 		/* Not our device - skip */
3096e2a7420dSJarod Wilson 		slave_dbg(bond->dev, slave->dev, "no path to arp_ip_target %pI4 via rt.dev %s\n",
309776444f50SVeaceslav Falico 			   &targets[i], rt->dst.dev ? rt->dst.dev->name : "NULL");
30983e32582fSVeaceslav Falico 
3099ed4b9f80SJay Vosburgh 		ip_rt_put(rt);
310027bc11e6SVeaceslav Falico 		continue;
310127bc11e6SVeaceslav Falico 
310227bc11e6SVeaceslav Falico found:
310327bc11e6SVeaceslav Falico 		addr = bond_confirm_addr(rt->dst.dev, targets[i], 0);
310427bc11e6SVeaceslav Falico 		ip_rt_put(rt);
3105e2a7420dSJarod Wilson 		bond_arp_send(slave, ARPOP_REQUEST, targets[i], addr, tags);
31063e403a77SVeaceslav Falico 		kfree(tags);
3107c3ade5caSJay Vosburgh 	}
3108c3ade5caSJay Vosburgh }
3109c3ade5caSJay Vosburgh 
bond_validate_arp(struct bonding * bond,struct slave * slave,__be32 sip,__be32 tip)3110d3bb52b0SAl Viro static void bond_validate_arp(struct bonding *bond, struct slave *slave, __be32 sip, __be32 tip)
3111f5b2b966SJay Vosburgh {
31128599b52eSVeaceslav Falico 	int i;
31138599b52eSVeaceslav Falico 
311487a7b84bSVeaceslav Falico 	if (!sip || !bond_has_this_ip(bond, tip)) {
3115e2a7420dSJarod Wilson 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 tip %pI4 not found\n",
3116e2a7420dSJarod Wilson 			   __func__, &sip, &tip);
3117f5b2b966SJay Vosburgh 		return;
3118f5b2b966SJay Vosburgh 	}
311987a7b84bSVeaceslav Falico 
31208599b52eSVeaceslav Falico 	i = bond_get_targets_ip(bond->params.arp_targets, sip);
31218599b52eSVeaceslav Falico 	if (i == -1) {
3122e2a7420dSJarod Wilson 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI4 not found in targets\n",
3123e2a7420dSJarod Wilson 			   __func__, &sip);
312487a7b84bSVeaceslav Falico 		return;
3125f5b2b966SJay Vosburgh 	}
312649f17de7SVeaceslav Falico 	slave->last_rx = jiffies;
31278599b52eSVeaceslav Falico 	slave->target_last_arp_rx[i] = jiffies;
3128f5b2b966SJay Vosburgh }
3129f5b2b966SJay Vosburgh 
bond_arp_rcv(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)31304e24be01SHangbin Liu static int bond_arp_rcv(const struct sk_buff *skb, struct bonding *bond,
31313aba891dSJiri Pirko 			struct slave *slave)
3132f5b2b966SJay Vosburgh {
3133de063b70SEric Dumazet 	struct arphdr *arp = (struct arphdr *)skb->data;
313421a75f09SJay Vosburgh 	struct slave *curr_active_slave, *curr_arp_slave;
3135f5b2b966SJay Vosburgh 	unsigned char *arp_ptr;
3136d3bb52b0SAl Viro 	__be32 sip, tip;
31376ade97daSAlexey Dobriyan 	unsigned int alen;
3138f5b2b966SJay Vosburgh 
3139de063b70SEric Dumazet 	alen = arp_hdr_len(bond->dev);
3140f5b2b966SJay Vosburgh 
3141de063b70SEric Dumazet 	if (alen > skb_headlen(skb)) {
3142de063b70SEric Dumazet 		arp = kmalloc(alen, GFP_ATOMIC);
3143de063b70SEric Dumazet 		if (!arp)
3144f5b2b966SJay Vosburgh 			goto out_unlock;
3145de063b70SEric Dumazet 		if (skb_copy_bits(skb, 0, arp, alen) < 0)
3146de063b70SEric Dumazet 			goto out_unlock;
3147de063b70SEric Dumazet 	}
3148f5b2b966SJay Vosburgh 
31493aba891dSJiri Pirko 	if (arp->ar_hln != bond->dev->addr_len ||
3150f5b2b966SJay Vosburgh 	    skb->pkt_type == PACKET_OTHERHOST ||
3151f5b2b966SJay Vosburgh 	    skb->pkt_type == PACKET_LOOPBACK ||
3152f5b2b966SJay Vosburgh 	    arp->ar_hrd != htons(ARPHRD_ETHER) ||
3153f5b2b966SJay Vosburgh 	    arp->ar_pro != htons(ETH_P_IP) ||
3154f5b2b966SJay Vosburgh 	    arp->ar_pln != 4)
3155f5b2b966SJay Vosburgh 		goto out_unlock;
3156f5b2b966SJay Vosburgh 
3157f5b2b966SJay Vosburgh 	arp_ptr = (unsigned char *)(arp + 1);
31583aba891dSJiri Pirko 	arp_ptr += bond->dev->addr_len;
3159f5b2b966SJay Vosburgh 	memcpy(&sip, arp_ptr, 4);
31603aba891dSJiri Pirko 	arp_ptr += 4 + bond->dev->addr_len;
3161f5b2b966SJay Vosburgh 	memcpy(&tip, arp_ptr, 4);
3162f5b2b966SJay Vosburgh 
3163e2a7420dSJarod Wilson 	slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI4 tip %pI4\n",
3164e2a7420dSJarod Wilson 		  __func__, slave->dev->name, bond_slave_state(slave),
316563779436SHarvey Harrison 		  bond->params.arp_validate, slave_do_arp_validate(bond, slave),
316663779436SHarvey Harrison 		  &sip, &tip);
3167f5b2b966SJay Vosburgh 
3168010d3c39SVeaceslav Falico 	curr_active_slave = rcu_dereference(bond->curr_active_slave);
316921a75f09SJay Vosburgh 	curr_arp_slave = rcu_dereference(bond->current_arp_slave);
3170010d3c39SVeaceslav Falico 
317121a75f09SJay Vosburgh 	/* We 'trust' the received ARP enough to validate it if:
3172aeea64acSVeaceslav Falico 	 *
317321a75f09SJay Vosburgh 	 * (a) the slave receiving the ARP is active (which includes the
317421a75f09SJay Vosburgh 	 * current ARP slave, if any), or
317521a75f09SJay Vosburgh 	 *
317621a75f09SJay Vosburgh 	 * (b) the receiving slave isn't active, but there is a currently
317721a75f09SJay Vosburgh 	 * active slave and it received valid arp reply(s) after it became
317821a75f09SJay Vosburgh 	 * the currently active slave, or
317921a75f09SJay Vosburgh 	 *
318021a75f09SJay Vosburgh 	 * (c) there is an ARP slave that sent an ARP during the prior ARP
318121a75f09SJay Vosburgh 	 * interval, and we receive an ARP reply on any slave.  We accept
318221a75f09SJay Vosburgh 	 * these because switch FDB update delays may deliver the ARP
318321a75f09SJay Vosburgh 	 * reply to a slave other than the sender of the ARP request.
318421a75f09SJay Vosburgh 	 *
318521a75f09SJay Vosburgh 	 * Note: for (b), backup slaves are receiving the broadcast ARP
318621a75f09SJay Vosburgh 	 * request, not a reply.  This request passes from the sending
318721a75f09SJay Vosburgh 	 * slave through the L2 switch(es) to the receiving slave.  Since
318821a75f09SJay Vosburgh 	 * this is checking the request, sip/tip are swapped for
318921a75f09SJay Vosburgh 	 * validation.
319021a75f09SJay Vosburgh 	 *
319121a75f09SJay Vosburgh 	 * This is done to avoid endless looping when we can't reach the
3192aeea64acSVeaceslav Falico 	 * arp_ip_target and fool ourselves with our own arp requests.
3193f5b2b966SJay Vosburgh 	 */
3194e30bc066SJiri Pirko 	if (bond_is_active_slave(slave))
3195f5b2b966SJay Vosburgh 		bond_validate_arp(bond, slave, sip, tip);
3196010d3c39SVeaceslav Falico 	else if (curr_active_slave &&
3197010d3c39SVeaceslav Falico 		 time_after(slave_last_rx(bond, curr_active_slave),
3198010d3c39SVeaceslav Falico 			    curr_active_slave->last_link_up))
3199f5b2b966SJay Vosburgh 		bond_validate_arp(bond, slave, tip, sip);
320021a75f09SJay Vosburgh 	else if (curr_arp_slave && (arp->ar_op == htons(ARPOP_REPLY)) &&
320106799a90SVladimir Oltean 		 bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
320221a75f09SJay Vosburgh 		bond_validate_arp(bond, slave, sip, tip);
3203f5b2b966SJay Vosburgh 
3204f5b2b966SJay Vosburgh out_unlock:
3205de063b70SEric Dumazet 	if (arp != (struct arphdr *)skb->data)
3206de063b70SEric Dumazet 		kfree(arp);
320713a8e0c8SJiri Bohac 	return RX_HANDLER_ANOTHER;
3208f5b2b966SJay Vosburgh }
3209f5b2b966SJay Vosburgh 
32104e24be01SHangbin Liu #if IS_ENABLED(CONFIG_IPV6)
bond_ns_send(struct slave * slave,const struct in6_addr * daddr,const struct in6_addr * saddr,struct bond_vlan_tag * tags)32114e24be01SHangbin Liu static void bond_ns_send(struct slave *slave, const struct in6_addr *daddr,
32124e24be01SHangbin Liu 			 const struct in6_addr *saddr, struct bond_vlan_tag *tags)
32134e24be01SHangbin Liu {
32144e24be01SHangbin Liu 	struct net_device *bond_dev = slave->bond->dev;
32154e24be01SHangbin Liu 	struct net_device *slave_dev = slave->dev;
32164e24be01SHangbin Liu 	struct in6_addr mcaddr;
32174e24be01SHangbin Liu 	struct sk_buff *skb;
32184e24be01SHangbin Liu 
32194e24be01SHangbin Liu 	slave_dbg(bond_dev, slave_dev, "NS on slave: dst %pI6c src %pI6c\n",
32204e24be01SHangbin Liu 		  daddr, saddr);
32214e24be01SHangbin Liu 
32224e24be01SHangbin Liu 	skb = ndisc_ns_create(slave_dev, daddr, saddr, 0);
32234e24be01SHangbin Liu 	if (!skb) {
32244e24be01SHangbin Liu 		net_err_ratelimited("NS packet allocation failed\n");
32254e24be01SHangbin Liu 		return;
32264e24be01SHangbin Liu 	}
32274e24be01SHangbin Liu 
32284e24be01SHangbin Liu 	addrconf_addr_solict_mult(daddr, &mcaddr);
322906799a90SVladimir Oltean 	if (bond_handle_vlan(slave, tags, skb)) {
323006799a90SVladimir Oltean 		slave_update_last_tx(slave);
32314e24be01SHangbin Liu 		ndisc_send_skb(skb, &mcaddr, saddr);
32324e24be01SHangbin Liu 	}
323306799a90SVladimir Oltean }
32344e24be01SHangbin Liu 
bond_ns_send_all(struct bonding * bond,struct slave * slave)32354e24be01SHangbin Liu static void bond_ns_send_all(struct bonding *bond, struct slave *slave)
32364e24be01SHangbin Liu {
32374e24be01SHangbin Liu 	struct in6_addr *targets = bond->params.ns_targets;
32384e24be01SHangbin Liu 	struct bond_vlan_tag *tags;
32394e24be01SHangbin Liu 	struct dst_entry *dst;
32404e24be01SHangbin Liu 	struct in6_addr saddr;
32414e24be01SHangbin Liu 	struct flowi6 fl6;
32424e24be01SHangbin Liu 	int i;
32434e24be01SHangbin Liu 
32444e24be01SHangbin Liu 	for (i = 0; i < BOND_MAX_NS_TARGETS && !ipv6_addr_any(&targets[i]); i++) {
32454e24be01SHangbin Liu 		slave_dbg(bond->dev, slave->dev, "%s: target %pI6c\n",
32464e24be01SHangbin Liu 			  __func__, &targets[i]);
32474e24be01SHangbin Liu 		tags = NULL;
32484e24be01SHangbin Liu 
32494e24be01SHangbin Liu 		/* Find out through which dev should the packet go */
32504e24be01SHangbin Liu 		memset(&fl6, 0, sizeof(struct flowi6));
32514e24be01SHangbin Liu 		fl6.daddr = targets[i];
32524e24be01SHangbin Liu 		fl6.flowi6_oif = bond->dev->ifindex;
32534e24be01SHangbin Liu 
32544e24be01SHangbin Liu 		dst = ip6_route_output(dev_net(bond->dev), NULL, &fl6);
32554e24be01SHangbin Liu 		if (dst->error) {
32564e24be01SHangbin Liu 			dst_release(dst);
32574e24be01SHangbin Liu 			/* there's no route to target - try to send arp
32584e24be01SHangbin Liu 			 * probe to generate any traffic (arp_validate=0)
32594e24be01SHangbin Liu 			 */
32604e24be01SHangbin Liu 			if (bond->params.arp_validate)
32614e24be01SHangbin Liu 				pr_warn_once("%s: no route to ns_ip6_target %pI6c and arp_validate is set\n",
32624e24be01SHangbin Liu 					     bond->dev->name,
32634e24be01SHangbin Liu 					     &targets[i]);
32644e24be01SHangbin Liu 			bond_ns_send(slave, &targets[i], &in6addr_any, tags);
32654e24be01SHangbin Liu 			continue;
32664e24be01SHangbin Liu 		}
32674e24be01SHangbin Liu 
32684e24be01SHangbin Liu 		/* bond device itself */
32694e24be01SHangbin Liu 		if (dst->dev == bond->dev)
32704e24be01SHangbin Liu 			goto found;
32714e24be01SHangbin Liu 
32724e24be01SHangbin Liu 		rcu_read_lock();
32734e24be01SHangbin Liu 		tags = bond_verify_device_path(bond->dev, dst->dev, 0);
32744e24be01SHangbin Liu 		rcu_read_unlock();
32754e24be01SHangbin Liu 
32764e24be01SHangbin Liu 		if (!IS_ERR_OR_NULL(tags))
32774e24be01SHangbin Liu 			goto found;
32784e24be01SHangbin Liu 
32794e24be01SHangbin Liu 		/* Not our device - skip */
32804e24be01SHangbin Liu 		slave_dbg(bond->dev, slave->dev, "no path to ns_ip6_target %pI6c via dst->dev %s\n",
32814e24be01SHangbin Liu 			  &targets[i], dst->dev ? dst->dev->name : "NULL");
32824e24be01SHangbin Liu 
32834e24be01SHangbin Liu 		dst_release(dst);
32844e24be01SHangbin Liu 		continue;
32854e24be01SHangbin Liu 
32864e24be01SHangbin Liu found:
32874e24be01SHangbin Liu 		if (!ipv6_dev_get_saddr(dev_net(dst->dev), dst->dev, &targets[i], 0, &saddr))
32884e24be01SHangbin Liu 			bond_ns_send(slave, &targets[i], &saddr, tags);
3289b7f14132SHangbin Liu 		else
3290b7f14132SHangbin Liu 			bond_ns_send(slave, &targets[i], &in6addr_any, tags);
3291b7f14132SHangbin Liu 
32924e24be01SHangbin Liu 		dst_release(dst);
32934e24be01SHangbin Liu 		kfree(tags);
32944e24be01SHangbin Liu 	}
32954e24be01SHangbin Liu }
32964e24be01SHangbin Liu 
bond_confirm_addr6(struct net_device * dev,struct netdev_nested_priv * priv)32974e24be01SHangbin Liu static int bond_confirm_addr6(struct net_device *dev,
32984e24be01SHangbin Liu 			      struct netdev_nested_priv *priv)
32994e24be01SHangbin Liu {
33004e24be01SHangbin Liu 	struct in6_addr *addr = (struct in6_addr *)priv->data;
33014e24be01SHangbin Liu 
33024e24be01SHangbin Liu 	return ipv6_chk_addr(dev_net(dev), addr, dev, 0);
33034e24be01SHangbin Liu }
33044e24be01SHangbin Liu 
bond_has_this_ip6(struct bonding * bond,struct in6_addr * addr)33054e24be01SHangbin Liu static bool bond_has_this_ip6(struct bonding *bond, struct in6_addr *addr)
33064e24be01SHangbin Liu {
33074e24be01SHangbin Liu 	struct netdev_nested_priv priv = {
33084e24be01SHangbin Liu 		.data = addr,
33094e24be01SHangbin Liu 	};
33104e24be01SHangbin Liu 	int ret = false;
33114e24be01SHangbin Liu 
33124e24be01SHangbin Liu 	if (bond_confirm_addr6(bond->dev, &priv))
33134e24be01SHangbin Liu 		return true;
33144e24be01SHangbin Liu 
33154e24be01SHangbin Liu 	rcu_read_lock();
33164e24be01SHangbin Liu 	if (netdev_walk_all_upper_dev_rcu(bond->dev, bond_confirm_addr6, &priv))
33174e24be01SHangbin Liu 		ret = true;
33184e24be01SHangbin Liu 	rcu_read_unlock();
33194e24be01SHangbin Liu 
33204e24be01SHangbin Liu 	return ret;
33214e24be01SHangbin Liu }
33224e24be01SHangbin Liu 
bond_validate_na(struct bonding * bond,struct slave * slave,struct in6_addr * saddr,struct in6_addr * daddr)3323592335a4SHangbin Liu static void bond_validate_na(struct bonding *bond, struct slave *slave,
33244e24be01SHangbin Liu 			     struct in6_addr *saddr, struct in6_addr *daddr)
33254e24be01SHangbin Liu {
33264e24be01SHangbin Liu 	int i;
33274e24be01SHangbin Liu 
3328592335a4SHangbin Liu 	/* Ignore NAs that:
3329592335a4SHangbin Liu 	 * 1. Source address is unspecified address.
3330592335a4SHangbin Liu 	 * 2. Dest address is neither all-nodes multicast address nor
3331592335a4SHangbin Liu 	 *    exist on bond interface.
3332592335a4SHangbin Liu 	 */
3333592335a4SHangbin Liu 	if (ipv6_addr_any(saddr) ||
3334592335a4SHangbin Liu 	    (!ipv6_addr_equal(daddr, &in6addr_linklocal_allnodes) &&
3335592335a4SHangbin Liu 	     !bond_has_this_ip6(bond, daddr))) {
33364e24be01SHangbin Liu 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c tip %pI6c not found\n",
33374e24be01SHangbin Liu 			  __func__, saddr, daddr);
33384e24be01SHangbin Liu 		return;
33394e24be01SHangbin Liu 	}
33404e24be01SHangbin Liu 
33414e24be01SHangbin Liu 	i = bond_get_targets_ip6(bond->params.ns_targets, saddr);
33424e24be01SHangbin Liu 	if (i == -1) {
33434e24be01SHangbin Liu 		slave_dbg(bond->dev, slave->dev, "%s: sip %pI6c not found in targets\n",
33444e24be01SHangbin Liu 			  __func__, saddr);
33454e24be01SHangbin Liu 		return;
33464e24be01SHangbin Liu 	}
33474e24be01SHangbin Liu 	slave->last_rx = jiffies;
33484e24be01SHangbin Liu 	slave->target_last_arp_rx[i] = jiffies;
33494e24be01SHangbin Liu }
33504e24be01SHangbin Liu 
bond_na_rcv(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)33514e24be01SHangbin Liu static int bond_na_rcv(const struct sk_buff *skb, struct bonding *bond,
33524e24be01SHangbin Liu 		       struct slave *slave)
33534e24be01SHangbin Liu {
33544e24be01SHangbin Liu 	struct slave *curr_active_slave, *curr_arp_slave;
33554e24be01SHangbin Liu 	struct in6_addr *saddr, *daddr;
33564d633d1bSHangbin Liu 	struct {
33574d633d1bSHangbin Liu 		struct ipv6hdr ip6;
33584d633d1bSHangbin Liu 		struct icmp6hdr icmp6;
33594d633d1bSHangbin Liu 	} *combined, _combined;
33604e24be01SHangbin Liu 
33614e24be01SHangbin Liu 	if (skb->pkt_type == PACKET_OTHERHOST ||
33624d633d1bSHangbin Liu 	    skb->pkt_type == PACKET_LOOPBACK)
33634e24be01SHangbin Liu 		goto out;
33644e24be01SHangbin Liu 
33654d633d1bSHangbin Liu 	combined = skb_header_pointer(skb, 0, sizeof(_combined), &_combined);
33664d633d1bSHangbin Liu 	if (!combined || combined->ip6.nexthdr != NEXTHDR_ICMP ||
33674598380fSHangbin Liu 	    (combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION &&
33684598380fSHangbin Liu 	     combined->icmp6.icmp6_type != NDISC_NEIGHBOUR_ADVERTISEMENT))
33694d633d1bSHangbin Liu 		goto out;
33704d633d1bSHangbin Liu 
33714d633d1bSHangbin Liu 	saddr = &combined->ip6.saddr;
33721f154f3bSHangbin Liu 	daddr = &combined->ip6.daddr;
33734e24be01SHangbin Liu 
33744e24be01SHangbin Liu 	slave_dbg(bond->dev, slave->dev, "%s: %s/%d av %d sv %d sip %pI6c tip %pI6c\n",
33754e24be01SHangbin Liu 		  __func__, slave->dev->name, bond_slave_state(slave),
33764e24be01SHangbin Liu 		  bond->params.arp_validate, slave_do_arp_validate(bond, slave),
33774e24be01SHangbin Liu 		  saddr, daddr);
33784e24be01SHangbin Liu 
33794e24be01SHangbin Liu 	curr_active_slave = rcu_dereference(bond->curr_active_slave);
33804e24be01SHangbin Liu 	curr_arp_slave = rcu_dereference(bond->current_arp_slave);
33814e24be01SHangbin Liu 
33824e24be01SHangbin Liu 	/* We 'trust' the received ARP enough to validate it if:
33834e24be01SHangbin Liu 	 * see bond_arp_rcv().
33844e24be01SHangbin Liu 	 */
33854e24be01SHangbin Liu 	if (bond_is_active_slave(slave))
3386592335a4SHangbin Liu 		bond_validate_na(bond, slave, saddr, daddr);
33874e24be01SHangbin Liu 	else if (curr_active_slave &&
33884e24be01SHangbin Liu 		 time_after(slave_last_rx(bond, curr_active_slave),
33894e24be01SHangbin Liu 			    curr_active_slave->last_link_up))
33904598380fSHangbin Liu 		bond_validate_na(bond, slave, daddr, saddr);
33914e24be01SHangbin Liu 	else if (curr_arp_slave &&
339206799a90SVladimir Oltean 		 bond_time_in_interval(bond, slave_last_tx(curr_arp_slave), 1))
3393592335a4SHangbin Liu 		bond_validate_na(bond, slave, saddr, daddr);
33944e24be01SHangbin Liu 
33954e24be01SHangbin Liu out:
33964e24be01SHangbin Liu 	return RX_HANDLER_ANOTHER;
33974e24be01SHangbin Liu }
33984e24be01SHangbin Liu #endif
33994e24be01SHangbin Liu 
bond_rcv_validate(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)34004e24be01SHangbin Liu int bond_rcv_validate(const struct sk_buff *skb, struct bonding *bond,
34014e24be01SHangbin Liu 		      struct slave *slave)
34024e24be01SHangbin Liu {
34034e24be01SHangbin Liu #if IS_ENABLED(CONFIG_IPV6)
34044e24be01SHangbin Liu 	bool is_ipv6 = skb->protocol == __cpu_to_be16(ETH_P_IPV6);
34054e24be01SHangbin Liu #endif
34064e24be01SHangbin Liu 	bool is_arp = skb->protocol == __cpu_to_be16(ETH_P_ARP);
34074e24be01SHangbin Liu 
34084e24be01SHangbin Liu 	slave_dbg(bond->dev, slave->dev, "%s: skb->dev %s\n",
34094e24be01SHangbin Liu 		  __func__, skb->dev->name);
34104e24be01SHangbin Liu 
34114e24be01SHangbin Liu 	/* Use arp validate logic for both ARP and NS */
34124e24be01SHangbin Liu 	if (!slave_do_arp_validate(bond, slave)) {
34134e24be01SHangbin Liu 		if ((slave_do_arp_validate_only(bond) && is_arp) ||
34144e24be01SHangbin Liu #if IS_ENABLED(CONFIG_IPV6)
34154e24be01SHangbin Liu 		    (slave_do_arp_validate_only(bond) && is_ipv6) ||
34164e24be01SHangbin Liu #endif
34174e24be01SHangbin Liu 		    !slave_do_arp_validate_only(bond))
34184e24be01SHangbin Liu 			slave->last_rx = jiffies;
34194e24be01SHangbin Liu 		return RX_HANDLER_ANOTHER;
34204e24be01SHangbin Liu 	} else if (is_arp) {
34214e24be01SHangbin Liu 		return bond_arp_rcv(skb, bond, slave);
34224e24be01SHangbin Liu #if IS_ENABLED(CONFIG_IPV6)
34234e24be01SHangbin Liu 	} else if (is_ipv6) {
34244e24be01SHangbin Liu 		return bond_na_rcv(skb, bond, slave);
34254e24be01SHangbin Liu #endif
34264e24be01SHangbin Liu 	} else {
34274e24be01SHangbin Liu 		return RX_HANDLER_ANOTHER;
34284e24be01SHangbin Liu 	}
34294e24be01SHangbin Liu }
34304e24be01SHangbin Liu 
bond_send_validate(struct bonding * bond,struct slave * slave)34314e24be01SHangbin Liu static void bond_send_validate(struct bonding *bond, struct slave *slave)
34324e24be01SHangbin Liu {
34334e24be01SHangbin Liu 	bond_arp_send_all(bond, slave);
34344e24be01SHangbin Liu #if IS_ENABLED(CONFIG_IPV6)
34354e24be01SHangbin Liu 	bond_ns_send_all(bond, slave);
34364e24be01SHangbin Liu #endif
34374e24be01SHangbin Liu }
34384e24be01SHangbin Liu 
3439e7f63f1dSVeaceslav Falico /* function to verify if we're in the arp_interval timeslice, returns true if
3440e7f63f1dSVeaceslav Falico  * (last_act - arp_interval) <= jiffies <= (last_act + mod * arp_interval +
3441e7f63f1dSVeaceslav Falico  * arp_interval/2) . the arp_interval/2 is needed for really fast networks.
3442e7f63f1dSVeaceslav Falico  */
bond_time_in_interval(struct bonding * bond,unsigned long last_act,int mod)3443e7f63f1dSVeaceslav Falico static bool bond_time_in_interval(struct bonding *bond, unsigned long last_act,
3444e7f63f1dSVeaceslav Falico 				  int mod)
3445e7f63f1dSVeaceslav Falico {
3446e7f63f1dSVeaceslav Falico 	int delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
3447e7f63f1dSVeaceslav Falico 
3448e7f63f1dSVeaceslav Falico 	return time_in_range(jiffies,
3449e7f63f1dSVeaceslav Falico 			     last_act - delta_in_ticks,
3450e7f63f1dSVeaceslav Falico 			     last_act + mod * delta_in_ticks + delta_in_ticks/2);
3451e7f63f1dSVeaceslav Falico }
3452e7f63f1dSVeaceslav Falico 
3453547942caSNikolay Aleksandrov /* This function is called regularly to monitor each slave's link
34541da177e4SLinus Torvalds  * ensuring that traffic is being sent and received when arp monitoring
34551da177e4SLinus Torvalds  * is used in load-balancing mode. if the adapter has been dormant, then an
34561da177e4SLinus Torvalds  * arp is transmitted to generate traffic. see activebackup_arp_monitor for
34571da177e4SLinus Torvalds  * arp monitoring in active backup mode.
34581da177e4SLinus Torvalds  */
bond_loadbalance_arp_mon(struct bonding * bond)3459d5e73f7bSMahesh Bandewar static void bond_loadbalance_arp_mon(struct bonding *bond)
34601da177e4SLinus Torvalds {
34611da177e4SLinus Torvalds 	struct slave *slave, *oldcurrent;
34629caff1e7SVeaceslav Falico 	struct list_head *iter;
34636fde8f03SDing Tianhong 	int do_failover = 0, slave_state_changed = 0;
34641da177e4SLinus Torvalds 
34651f2cd845SDavid S. Miller 	if (!bond_has_slaves(bond))
34667f1bb571Sdingtianhong 		goto re_arm;
34677f1bb571Sdingtianhong 
34682e52f4feSdingtianhong 	rcu_read_lock();
34692e52f4feSdingtianhong 
34704740d638SEric Dumazet 	oldcurrent = rcu_dereference(bond->curr_active_slave);
34711da177e4SLinus Torvalds 	/* see if any of the previous devices are up now (i.e. they have
34721da177e4SLinus Torvalds 	 * xmt and rcv traffic). the curr_active_slave does not come into
34738e603460SVeaceslav Falico 	 * the picture unless it is null. also, slave->last_link_up is not
34748e603460SVeaceslav Falico 	 * needed here because we send an arp on each slave and give a slave
34758e603460SVeaceslav Falico 	 * as long as it needs to get the tx/rx within the delta.
34761da177e4SLinus Torvalds 	 * TODO: what about up/down delay in arp mode? it wasn't here before
34771da177e4SLinus Torvalds 	 *       so it can wait
34781da177e4SLinus Torvalds 	 */
34792e52f4feSdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
348006799a90SVladimir Oltean 		unsigned long last_tx = slave_last_tx(slave);
3481cb32f2a0SJiri Bohac 
34821899bb32SJay Vosburgh 		bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3483797a9364SNithin Sujir 
34841da177e4SLinus Torvalds 		if (slave->link != BOND_LINK_UP) {
348506799a90SVladimir Oltean 			if (bond_time_in_interval(bond, last_tx, 1) &&
348649f17de7SVeaceslav Falico 			    bond_time_in_interval(bond, slave->last_rx, 1)) {
34871da177e4SLinus Torvalds 
34881899bb32SJay Vosburgh 				bond_propose_link_state(slave, BOND_LINK_UP);
34896fde8f03SDing Tianhong 				slave_state_changed = 1;
34901da177e4SLinus Torvalds 
34911da177e4SLinus Torvalds 				/* primary_slave has no meaning in round-robin
34921da177e4SLinus Torvalds 				 * mode. the window of a slave being up and
34931da177e4SLinus Torvalds 				 * curr_active_slave being null after enslaving
34941da177e4SLinus Torvalds 				 * is closed.
34951da177e4SLinus Torvalds 				 */
34961da177e4SLinus Torvalds 				if (!oldcurrent) {
3497e2a7420dSJarod Wilson 					slave_info(bond->dev, slave->dev, "link status definitely up\n");
34981da177e4SLinus Torvalds 					do_failover = 1;
34991da177e4SLinus Torvalds 				} else {
3500e2a7420dSJarod Wilson 					slave_info(bond->dev, slave->dev, "interface is now up\n");
35011da177e4SLinus Torvalds 				}
35021da177e4SLinus Torvalds 			}
35031da177e4SLinus Torvalds 		} else {
35041da177e4SLinus Torvalds 			/* slave->link == BOND_LINK_UP */
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds 			/* not all switches will respond to an arp request
35071da177e4SLinus Torvalds 			 * when the source ip is 0, so don't take the link down
35081da177e4SLinus Torvalds 			 * if we don't know our ip yet
35091da177e4SLinus Torvalds 			 */
351006799a90SVladimir Oltean 			if (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
35115944b5abSHangbin Liu 			    !bond_time_in_interval(bond, slave->last_rx, bond->params.missed_max)) {
35121da177e4SLinus Torvalds 
35131899bb32SJay Vosburgh 				bond_propose_link_state(slave, BOND_LINK_DOWN);
35146fde8f03SDing Tianhong 				slave_state_changed = 1;
35151da177e4SLinus Torvalds 
35163d632c3fSStephen Hemminger 				if (slave->link_failure_count < UINT_MAX)
35171da177e4SLinus Torvalds 					slave->link_failure_count++;
35181da177e4SLinus Torvalds 
3519e2a7420dSJarod Wilson 				slave_info(bond->dev, slave->dev, "interface is now down\n");
35201da177e4SLinus Torvalds 
35213d632c3fSStephen Hemminger 				if (slave == oldcurrent)
35221da177e4SLinus Torvalds 					do_failover = 1;
35231da177e4SLinus Torvalds 			}
35241da177e4SLinus Torvalds 		}
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds 		/* note: if switch is in round-robin mode, all links
35271da177e4SLinus Torvalds 		 * must tx arp to ensure all links rx an arp - otherwise
35281da177e4SLinus Torvalds 		 * links may oscillate or not come up at all; if switch is
35291da177e4SLinus Torvalds 		 * in something like xor mode, there is nothing we can
35301da177e4SLinus Torvalds 		 * do - all replies will be rx'ed on same link causing slaves
35311da177e4SLinus Torvalds 		 * to be unstable during low/no traffic periods
35321da177e4SLinus Torvalds 		 */
3533b6adc610SVeaceslav Falico 		if (bond_slave_is_up(slave))
35344e24be01SHangbin Liu 			bond_send_validate(bond, slave);
35351da177e4SLinus Torvalds 	}
35361da177e4SLinus Torvalds 
35372e52f4feSdingtianhong 	rcu_read_unlock();
35382e52f4feSdingtianhong 
35396fde8f03SDing Tianhong 	if (do_failover || slave_state_changed) {
35406fde8f03SDing Tianhong 		if (!rtnl_trylock())
35416fde8f03SDing Tianhong 			goto re_arm;
35426fde8f03SDing Tianhong 
3543797a9364SNithin Sujir 		bond_for_each_slave(bond, slave, iter) {
35441899bb32SJay Vosburgh 			if (slave->link_new_state != BOND_LINK_NOCHANGE)
35451899bb32SJay Vosburgh 				slave->link = slave->link_new_state;
3546797a9364SNithin Sujir 		}
3547797a9364SNithin Sujir 
35486fde8f03SDing Tianhong 		if (slave_state_changed) {
35496fde8f03SDing Tianhong 			bond_slave_state_change(bond);
3550ee637714SMahesh Bandewar 			if (BOND_MODE(bond) == BOND_MODE_XOR)
3551ee637714SMahesh Bandewar 				bond_update_slave_arr(bond, NULL);
3552b8e4500fSNikolay Aleksandrov 		}
3553b8e4500fSNikolay Aleksandrov 		if (do_failover) {
3554e843fa50SNeil Horman 			block_netpoll_tx();
35551da177e4SLinus Torvalds 			bond_select_active_slave(bond);
3556e843fa50SNeil Horman 			unblock_netpoll_tx();
35576fde8f03SDing Tianhong 		}
35582e52f4feSdingtianhong 		rtnl_unlock();
35591da177e4SLinus Torvalds 	}
35601da177e4SLinus Torvalds 
35611da177e4SLinus Torvalds re_arm:
3562e6d265e8SJay Vosburgh 	if (bond->params.arp_interval)
3563e7f63f1dSVeaceslav Falico 		queue_delayed_work(bond->wq, &bond->arp_work,
3564e7f63f1dSVeaceslav Falico 				   msecs_to_jiffies(bond->params.arp_interval));
35651da177e4SLinus Torvalds }
35661da177e4SLinus Torvalds 
3567547942caSNikolay Aleksandrov /* Called to inspect slaves for active-backup mode ARP monitor link state
35681899bb32SJay Vosburgh  * changes.  Sets proposed link state in slaves to specify what action
35691899bb32SJay Vosburgh  * should take place for the slave.  Returns 0 if no changes are found, >0
35701899bb32SJay Vosburgh  * if changes to link states must be committed.
3571b2220cadSJay Vosburgh  *
35728c0bc550SNikolay Aleksandrov  * Called with rcu_read_lock held.
35731da177e4SLinus Torvalds  */
bond_ab_arp_inspect(struct bonding * bond)3574e7f63f1dSVeaceslav Falico static int bond_ab_arp_inspect(struct bonding *bond)
35751da177e4SLinus Torvalds {
357606799a90SVladimir Oltean 	unsigned long last_tx, last_rx;
35779caff1e7SVeaceslav Falico 	struct list_head *iter;
3578dec1e90eSnikolay@redhat.com 	struct slave *slave;
3579dec1e90eSnikolay@redhat.com 	int commit = 0;
3580da210f55SJiri Bohac 
3581eb9fa4b0Sdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
35821899bb32SJay Vosburgh 		bond_propose_link_state(slave, BOND_LINK_NOCHANGE);
3583def4460cSVeaceslav Falico 		last_rx = slave_last_rx(bond, slave);
3584b2220cadSJay Vosburgh 
3585b2220cadSJay Vosburgh 		if (slave->link != BOND_LINK_UP) {
3586e7f63f1dSVeaceslav Falico 			if (bond_time_in_interval(bond, last_rx, 1)) {
35871899bb32SJay Vosburgh 				bond_propose_link_state(slave, BOND_LINK_UP);
3588b2220cadSJay Vosburgh 				commit++;
35890410d071SJiri Wiesner 			} else if (slave->link == BOND_LINK_BACK) {
35900410d071SJiri Wiesner 				bond_propose_link_state(slave, BOND_LINK_FAIL);
35910410d071SJiri Wiesner 				commit++;
3592b2220cadSJay Vosburgh 			}
3593b2220cadSJay Vosburgh 			continue;
3594b2220cadSJay Vosburgh 		}
3595b2220cadSJay Vosburgh 
3596547942caSNikolay Aleksandrov 		/* Give slaves 2*delta after being enslaved or made
3597b2220cadSJay Vosburgh 		 * active.  This avoids bouncing, as the last receive
3598b2220cadSJay Vosburgh 		 * times need a full ARP monitor cycle to be updated.
3599b2220cadSJay Vosburgh 		 */
36008e603460SVeaceslav Falico 		if (bond_time_in_interval(bond, slave->last_link_up, 2))
3601b2220cadSJay Vosburgh 			continue;
3602b2220cadSJay Vosburgh 
3603547942caSNikolay Aleksandrov 		/* Backup slave is down if:
3604b2220cadSJay Vosburgh 		 * - No current_arp_slave AND
36055944b5abSHangbin Liu 		 * - more than (missed_max+1)*delta since last receive AND
3606b2220cadSJay Vosburgh 		 * - the bond has an IP address
3607b2220cadSJay Vosburgh 		 *
3608b2220cadSJay Vosburgh 		 * Note: a non-null current_arp_slave indicates
3609b2220cadSJay Vosburgh 		 * the curr_active_slave went down and we are
3610b2220cadSJay Vosburgh 		 * searching for a new one; under this condition
3611b2220cadSJay Vosburgh 		 * we only take the curr_active_slave down - this
3612b2220cadSJay Vosburgh 		 * gives each slave a chance to tx/rx traffic
3613b2220cadSJay Vosburgh 		 * before being taken out
3614b2220cadSJay Vosburgh 		 */
3615e30bc066SJiri Pirko 		if (!bond_is_active_slave(slave) &&
361685741718SEric Dumazet 		    !rcu_access_pointer(bond->current_arp_slave) &&
36175944b5abSHangbin Liu 		    !bond_time_in_interval(bond, last_rx, bond->params.missed_max + 1)) {
36181899bb32SJay Vosburgh 			bond_propose_link_state(slave, BOND_LINK_DOWN);
3619b2220cadSJay Vosburgh 			commit++;
3620b2220cadSJay Vosburgh 		}
3621b2220cadSJay Vosburgh 
3622547942caSNikolay Aleksandrov 		/* Active slave is down if:
36235944b5abSHangbin Liu 		 * - more than missed_max*delta since transmitting OR
36245944b5abSHangbin Liu 		 * - (more than missed_max*delta since receive AND
3625b2220cadSJay Vosburgh 		 *    the bond has an IP address)
3626b2220cadSJay Vosburgh 		 */
362706799a90SVladimir Oltean 		last_tx = slave_last_tx(slave);
3628e30bc066SJiri Pirko 		if (bond_is_active_slave(slave) &&
362906799a90SVladimir Oltean 		    (!bond_time_in_interval(bond, last_tx, bond->params.missed_max) ||
36305944b5abSHangbin Liu 		     !bond_time_in_interval(bond, last_rx, bond->params.missed_max))) {
36311899bb32SJay Vosburgh 			bond_propose_link_state(slave, BOND_LINK_DOWN);
3632b2220cadSJay Vosburgh 			commit++;
3633b2220cadSJay Vosburgh 		}
3634b2220cadSJay Vosburgh 	}
3635b2220cadSJay Vosburgh 
3636b2220cadSJay Vosburgh 	return commit;
3637b2220cadSJay Vosburgh }
3638b2220cadSJay Vosburgh 
3639547942caSNikolay Aleksandrov /* Called to commit link state changes noted by inspection step of
3640b2220cadSJay Vosburgh  * active-backup mode ARP monitor.
3641b2220cadSJay Vosburgh  *
3642eb9fa4b0Sdingtianhong  * Called with RTNL hold.
3643b2220cadSJay Vosburgh  */
bond_ab_arp_commit(struct bonding * bond)3644e7f63f1dSVeaceslav Falico static void bond_ab_arp_commit(struct bonding *bond)
3645b2220cadSJay Vosburgh {
3646e95cc447SHangbin Liu 	bool do_failover = false;
36479caff1e7SVeaceslav Falico 	struct list_head *iter;
364806799a90SVladimir Oltean 	unsigned long last_tx;
3649dec1e90eSnikolay@redhat.com 	struct slave *slave;
36501da177e4SLinus Torvalds 
36519caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
36521899bb32SJay Vosburgh 		switch (slave->link_new_state) {
3653b2220cadSJay Vosburgh 		case BOND_LINK_NOCHANGE:
3654b2220cadSJay Vosburgh 			continue;
36551da177e4SLinus Torvalds 
3656b2220cadSJay Vosburgh 		case BOND_LINK_UP:
365706799a90SVladimir Oltean 			last_tx = slave_last_tx(slave);
36584740d638SEric Dumazet 			if (rtnl_dereference(bond->curr_active_slave) != slave ||
36594740d638SEric Dumazet 			    (!rtnl_dereference(bond->curr_active_slave) &&
366006799a90SVladimir Oltean 			     bond_time_in_interval(bond, last_tx, 1))) {
366185741718SEric Dumazet 				struct slave *current_arp_slave;
366285741718SEric Dumazet 
366385741718SEric Dumazet 				current_arp_slave = rtnl_dereference(bond->current_arp_slave);
36645d397061SJiri Pirko 				bond_set_slave_link_state(slave, BOND_LINK_UP,
36655d397061SJiri Pirko 							  BOND_SLAVE_NOTIFY_NOW);
366685741718SEric Dumazet 				if (current_arp_slave) {
36675a430974SVeaceslav Falico 					bond_set_slave_inactive_flags(
366885741718SEric Dumazet 						current_arp_slave,
36695e5b0665Sdingtianhong 						BOND_SLAVE_NOTIFY_NOW);
367085741718SEric Dumazet 					RCU_INIT_POINTER(bond->current_arp_slave, NULL);
36715a430974SVeaceslav Falico 				}
36721da177e4SLinus Torvalds 
3673e2a7420dSJarod Wilson 				slave_info(bond->dev, slave->dev, "link status definitely up\n");
3674b2220cadSJay Vosburgh 
36754740d638SEric Dumazet 				if (!rtnl_dereference(bond->curr_active_slave) ||
3676e95cc447SHangbin Liu 				    slave == rtnl_dereference(bond->primary_slave) ||
3677e95cc447SHangbin Liu 				    slave->prio > rtnl_dereference(bond->curr_active_slave)->prio)
3678e95cc447SHangbin Liu 					do_failover = true;
3679b2220cadSJay Vosburgh 
36801da177e4SLinus Torvalds 			}
36811da177e4SLinus Torvalds 
3682b9f60253SJiri Pirko 			continue;
36831da177e4SLinus Torvalds 
3684b2220cadSJay Vosburgh 		case BOND_LINK_DOWN:
3685b2220cadSJay Vosburgh 			if (slave->link_failure_count < UINT_MAX)
3686b2220cadSJay Vosburgh 				slave->link_failure_count++;
36871da177e4SLinus Torvalds 
36885d397061SJiri Pirko 			bond_set_slave_link_state(slave, BOND_LINK_DOWN,
36895d397061SJiri Pirko 						  BOND_SLAVE_NOTIFY_NOW);
36905e5b0665Sdingtianhong 			bond_set_slave_inactive_flags(slave,
36915e5b0665Sdingtianhong 						      BOND_SLAVE_NOTIFY_NOW);
36921da177e4SLinus Torvalds 
3693e2a7420dSJarod Wilson 			slave_info(bond->dev, slave->dev, "link status definitely down, disabling slave\n");
36941da177e4SLinus Torvalds 
36954740d638SEric Dumazet 			if (slave == rtnl_dereference(bond->curr_active_slave)) {
369685741718SEric Dumazet 				RCU_INIT_POINTER(bond->current_arp_slave, NULL);
3697e95cc447SHangbin Liu 				do_failover = true;
3698b2220cadSJay Vosburgh 			}
3699b9f60253SJiri Pirko 
3700b9f60253SJiri Pirko 			continue;
3701b2220cadSJay Vosburgh 
37020410d071SJiri Wiesner 		case BOND_LINK_FAIL:
37030410d071SJiri Wiesner 			bond_set_slave_link_state(slave, BOND_LINK_FAIL,
37040410d071SJiri Wiesner 						  BOND_SLAVE_NOTIFY_NOW);
37050410d071SJiri Wiesner 			bond_set_slave_inactive_flags(slave,
37060410d071SJiri Wiesner 						      BOND_SLAVE_NOTIFY_NOW);
37070410d071SJiri Wiesner 
37080410d071SJiri Wiesner 			/* A slave has just been enslaved and has become
37090410d071SJiri Wiesner 			 * the current active slave.
37100410d071SJiri Wiesner 			 */
37110410d071SJiri Wiesner 			if (rtnl_dereference(bond->curr_active_slave))
37120410d071SJiri Wiesner 				RCU_INIT_POINTER(bond->current_arp_slave, NULL);
37130410d071SJiri Wiesner 			continue;
37140410d071SJiri Wiesner 
3715b2220cadSJay Vosburgh 		default:
37161899bb32SJay Vosburgh 			slave_err(bond->dev, slave->dev,
37171899bb32SJay Vosburgh 				  "impossible: link_new_state %d on slave\n",
37181899bb32SJay Vosburgh 				  slave->link_new_state);
3719b9f60253SJiri Pirko 			continue;
3720b2220cadSJay Vosburgh 		}
3721e95cc447SHangbin Liu 	}
3722b2220cadSJay Vosburgh 
3723e95cc447SHangbin Liu 	if (do_failover) {
3724e843fa50SNeil Horman 		block_netpoll_tx();
3725b9f60253SJiri Pirko 		bond_select_active_slave(bond);
3726e843fa50SNeil Horman 		unblock_netpoll_tx();
37271da177e4SLinus Torvalds 	}
37281da177e4SLinus Torvalds 
3729b2220cadSJay Vosburgh 	bond_set_carrier(bond);
3730b2220cadSJay Vosburgh }
3731b2220cadSJay Vosburgh 
3732547942caSNikolay Aleksandrov /* Send ARP probes for active-backup mode ARP monitor.
3733b0929915Sdingtianhong  *
37348c0bc550SNikolay Aleksandrov  * Called with rcu_read_lock held.
37351da177e4SLinus Torvalds  */
bond_ab_arp_probe(struct bonding * bond)3736f2ebd477SVeaceslav Falico static bool bond_ab_arp_probe(struct bonding *bond)
3737b2220cadSJay Vosburgh {
3738eb9fa4b0Sdingtianhong 	struct slave *slave, *before = NULL, *new_slave = NULL,
3739b0929915Sdingtianhong 		     *curr_arp_slave = rcu_dereference(bond->current_arp_slave),
3740b0929915Sdingtianhong 		     *curr_active_slave = rcu_dereference(bond->curr_active_slave);
37414087df87SVeaceslav Falico 	struct list_head *iter;
37424087df87SVeaceslav Falico 	bool found = false;
3743b0929915Sdingtianhong 	bool should_notify_rtnl = BOND_SLAVE_NOTIFY_LATER;
3744f2ebd477SVeaceslav Falico 
374598b90f26SVeaceslav Falico 	if (curr_arp_slave && curr_active_slave)
374676444f50SVeaceslav Falico 		netdev_info(bond->dev, "PROBE: c_arp %s && cas %s BAD\n",
3747eb9fa4b0Sdingtianhong 			    curr_arp_slave->dev->name,
374898b90f26SVeaceslav Falico 			    curr_active_slave->dev->name);
3749b2220cadSJay Vosburgh 
375098b90f26SVeaceslav Falico 	if (curr_active_slave) {
37514e24be01SHangbin Liu 		bond_send_validate(bond, curr_active_slave);
3752b0929915Sdingtianhong 		return should_notify_rtnl;
37531da177e4SLinus Torvalds 	}
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds 	/* if we don't have a curr_active_slave, search for the next available
37561da177e4SLinus Torvalds 	 * backup slave from the current_arp_slave and make it the candidate
37571da177e4SLinus Torvalds 	 * for becoming the curr_active_slave
37581da177e4SLinus Torvalds 	 */
3759b2220cadSJay Vosburgh 
3760eb9fa4b0Sdingtianhong 	if (!curr_arp_slave) {
3761b0929915Sdingtianhong 		curr_arp_slave = bond_first_slave_rcu(bond);
3762b0929915Sdingtianhong 		if (!curr_arp_slave)
3763b0929915Sdingtianhong 			return should_notify_rtnl;
37641da177e4SLinus Torvalds 	}
37651da177e4SLinus Torvalds 
3766b0929915Sdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
3767b6adc610SVeaceslav Falico 		if (!found && !before && bond_slave_is_up(slave))
37684087df87SVeaceslav Falico 			before = slave;
37691da177e4SLinus Torvalds 
3770b6adc610SVeaceslav Falico 		if (found && !new_slave && bond_slave_is_up(slave))
37714087df87SVeaceslav Falico 			new_slave = slave;
37721da177e4SLinus Torvalds 		/* if the link state is up at this point, we
37731da177e4SLinus Torvalds 		 * mark it down - this can happen if we have
37741da177e4SLinus Torvalds 		 * simultaneous link failures and
37751da177e4SLinus Torvalds 		 * reselect_active_interface doesn't make this
37761da177e4SLinus Torvalds 		 * one the current slave so it is still marked
37771da177e4SLinus Torvalds 		 * up when it is actually down
37781da177e4SLinus Torvalds 		 */
3779b6adc610SVeaceslav Falico 		if (!bond_slave_is_up(slave) && slave->link == BOND_LINK_UP) {
37805d397061SJiri Pirko 			bond_set_slave_link_state(slave, BOND_LINK_DOWN,
37815d397061SJiri Pirko 						  BOND_SLAVE_NOTIFY_LATER);
3782b2220cadSJay Vosburgh 			if (slave->link_failure_count < UINT_MAX)
37831da177e4SLinus Torvalds 				slave->link_failure_count++;
37841da177e4SLinus Torvalds 
37855e5b0665Sdingtianhong 			bond_set_slave_inactive_flags(slave,
3786b0929915Sdingtianhong 						      BOND_SLAVE_NOTIFY_LATER);
37871da177e4SLinus Torvalds 
3788e2a7420dSJarod Wilson 			slave_info(bond->dev, slave->dev, "backup interface is now down\n");
37891da177e4SLinus Torvalds 		}
3790eb9fa4b0Sdingtianhong 		if (slave == curr_arp_slave)
37914087df87SVeaceslav Falico 			found = true;
37921da177e4SLinus Torvalds 	}
37934087df87SVeaceslav Falico 
37944087df87SVeaceslav Falico 	if (!new_slave && before)
37954087df87SVeaceslav Falico 		new_slave = before;
37964087df87SVeaceslav Falico 
3797b0929915Sdingtianhong 	if (!new_slave)
3798b0929915Sdingtianhong 		goto check_state;
37994087df87SVeaceslav Falico 
38005d397061SJiri Pirko 	bond_set_slave_link_state(new_slave, BOND_LINK_BACK,
38015d397061SJiri Pirko 				  BOND_SLAVE_NOTIFY_LATER);
3802b0929915Sdingtianhong 	bond_set_slave_active_flags(new_slave, BOND_SLAVE_NOTIFY_LATER);
38034e24be01SHangbin Liu 	bond_send_validate(bond, new_slave);
38048e603460SVeaceslav Falico 	new_slave->last_link_up = jiffies;
3805eb9fa4b0Sdingtianhong 	rcu_assign_pointer(bond->current_arp_slave, new_slave);
3806f2ebd477SVeaceslav Falico 
3807b0929915Sdingtianhong check_state:
3808b0929915Sdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
38095d397061SJiri Pirko 		if (slave->should_notify || slave->should_notify_link) {
3810b0929915Sdingtianhong 			should_notify_rtnl = BOND_SLAVE_NOTIFY_NOW;
3811b0929915Sdingtianhong 			break;
3812b0929915Sdingtianhong 		}
3813b0929915Sdingtianhong 	}
3814b0929915Sdingtianhong 	return should_notify_rtnl;
38151da177e4SLinus Torvalds }
3816b2220cadSJay Vosburgh 
bond_activebackup_arp_mon(struct bonding * bond)3817d5e73f7bSMahesh Bandewar static void bond_activebackup_arp_mon(struct bonding *bond)
3818b2220cadSJay Vosburgh {
3819b0929915Sdingtianhong 	bool should_notify_peers = false;
3820b0929915Sdingtianhong 	bool should_notify_rtnl = false;
38211f2cd845SDavid S. Miller 	int delta_in_ticks;
3822b2220cadSJay Vosburgh 
38231f2cd845SDavid S. Miller 	delta_in_ticks = msecs_to_jiffies(bond->params.arp_interval);
38241f2cd845SDavid S. Miller 
38251f2cd845SDavid S. Miller 	if (!bond_has_slaves(bond))
382680b9d236Sdingtianhong 		goto re_arm;
382780b9d236Sdingtianhong 
3828eb9fa4b0Sdingtianhong 	rcu_read_lock();
3829b0929915Sdingtianhong 
3830ad246c99SBen Hutchings 	should_notify_peers = bond_should_notify_peers(bond);
3831b0929915Sdingtianhong 
3832b0929915Sdingtianhong 	if (bond_ab_arp_inspect(bond)) {
3833eb9fa4b0Sdingtianhong 		rcu_read_unlock();
38341f2cd845SDavid S. Miller 
38351f2cd845SDavid S. Miller 		/* Race avoidance with bond_close flush of workqueue */
38361f2cd845SDavid S. Miller 		if (!rtnl_trylock()) {
38371f2cd845SDavid S. Miller 			delta_in_ticks = 1;
38381f2cd845SDavid S. Miller 			should_notify_peers = false;
38391f2cd845SDavid S. Miller 			goto re_arm;
38401f2cd845SDavid S. Miller 		}
38411f2cd845SDavid S. Miller 
3842e7f63f1dSVeaceslav Falico 		bond_ab_arp_commit(bond);
3843b0929915Sdingtianhong 
38441f2cd845SDavid S. Miller 		rtnl_unlock();
3845b0929915Sdingtianhong 		rcu_read_lock();
38461f2cd845SDavid S. Miller 	}
38471f2cd845SDavid S. Miller 
3848b0929915Sdingtianhong 	should_notify_rtnl = bond_ab_arp_probe(bond);
3849b0929915Sdingtianhong 	rcu_read_unlock();
3850b2220cadSJay Vosburgh 
38511da177e4SLinus Torvalds re_arm:
3852e6d265e8SJay Vosburgh 	if (bond->params.arp_interval)
38531f2cd845SDavid S. Miller 		queue_delayed_work(bond->wq, &bond->arp_work, delta_in_ticks);
38541f2cd845SDavid S. Miller 
3855b0929915Sdingtianhong 	if (should_notify_peers || should_notify_rtnl) {
38561f2cd845SDavid S. Miller 		if (!rtnl_trylock())
38571f2cd845SDavid S. Miller 			return;
3858b0929915Sdingtianhong 
38597a9214f3SJay Vosburgh 		if (should_notify_peers) {
38607a9214f3SJay Vosburgh 			bond->send_peer_notif--;
3861b0929915Sdingtianhong 			call_netdevice_notifiers(NETDEV_NOTIFY_PEERS,
3862b0929915Sdingtianhong 						 bond->dev);
38637a9214f3SJay Vosburgh 		}
38645d397061SJiri Pirko 		if (should_notify_rtnl) {
3865b0929915Sdingtianhong 			bond_slave_state_notify(bond);
38665d397061SJiri Pirko 			bond_slave_link_notify(bond);
38675d397061SJiri Pirko 		}
3868b0929915Sdingtianhong 
38691f2cd845SDavid S. Miller 		rtnl_unlock();
38701f2cd845SDavid S. Miller 	}
38711da177e4SLinus Torvalds }
38721da177e4SLinus Torvalds 
bond_arp_monitor(struct work_struct * work)3873d5e73f7bSMahesh Bandewar static void bond_arp_monitor(struct work_struct *work)
3874d5e73f7bSMahesh Bandewar {
3875d5e73f7bSMahesh Bandewar 	struct bonding *bond = container_of(work, struct bonding,
3876d5e73f7bSMahesh Bandewar 					    arp_work.work);
3877d5e73f7bSMahesh Bandewar 
3878d5e73f7bSMahesh Bandewar 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
3879d5e73f7bSMahesh Bandewar 		bond_activebackup_arp_mon(bond);
3880d5e73f7bSMahesh Bandewar 	else
3881d5e73f7bSMahesh Bandewar 		bond_loadbalance_arp_mon(bond);
3882d5e73f7bSMahesh Bandewar }
3883d5e73f7bSMahesh Bandewar 
38841da177e4SLinus Torvalds /*-------------------------- netdev event handling --------------------------*/
38851da177e4SLinus Torvalds 
3886547942caSNikolay Aleksandrov /* Change device name */
bond_event_changename(struct bonding * bond)38871da177e4SLinus Torvalds static int bond_event_changename(struct bonding *bond)
38881da177e4SLinus Torvalds {
38891da177e4SLinus Torvalds 	bond_remove_proc_entry(bond);
38901da177e4SLinus Torvalds 	bond_create_proc_entry(bond);
38917e083840SStephen Hemminger 
3892f073c7caSTaku Izumi 	bond_debug_reregister(bond);
3893f073c7caSTaku Izumi 
38941da177e4SLinus Torvalds 	return NOTIFY_DONE;
38951da177e4SLinus Torvalds }
38961da177e4SLinus Torvalds 
bond_master_netdev_event(unsigned long event,struct net_device * bond_dev)38973d632c3fSStephen Hemminger static int bond_master_netdev_event(unsigned long event,
38983d632c3fSStephen Hemminger 				    struct net_device *bond_dev)
38991da177e4SLinus Torvalds {
3900454d7c9bSWang Chen 	struct bonding *event_bond = netdev_priv(bond_dev);
39011da177e4SLinus Torvalds 
3902e2a7420dSJarod Wilson 	netdev_dbg(bond_dev, "%s called\n", __func__);
3903e2a7420dSJarod Wilson 
39041da177e4SLinus Torvalds 	switch (event) {
39051da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
39061da177e4SLinus Torvalds 		return bond_event_changename(event_bond);
3907a64d49c3SEric W. Biederman 	case NETDEV_UNREGISTER:
3908a64d49c3SEric W. Biederman 		bond_remove_proc_entry(event_bond);
39095b69874fSMahesh Bandewar #ifdef CONFIG_XFRM_OFFLOAD
39109a560550STaehee Yoo 		xfrm_dev_state_flush(dev_net(bond_dev), bond_dev, true);
39115b69874fSMahesh Bandewar #endif /* CONFIG_XFRM_OFFLOAD */
3912a64d49c3SEric W. Biederman 		break;
3913a64d49c3SEric W. Biederman 	case NETDEV_REGISTER:
3914a64d49c3SEric W. Biederman 		bond_create_proc_entry(event_bond);
3915a64d49c3SEric W. Biederman 		break;
39161da177e4SLinus Torvalds 	default:
39171da177e4SLinus Torvalds 		break;
39181da177e4SLinus Torvalds 	}
39191da177e4SLinus Torvalds 
39201da177e4SLinus Torvalds 	return NOTIFY_DONE;
39211da177e4SLinus Torvalds }
39221da177e4SLinus Torvalds 
bond_slave_netdev_event(unsigned long event,struct net_device * slave_dev)39233d632c3fSStephen Hemminger static int bond_slave_netdev_event(unsigned long event,
39243d632c3fSStephen Hemminger 				   struct net_device *slave_dev)
39251da177e4SLinus Torvalds {
3926059b47e8SNikolay Aleksandrov 	struct slave *slave = bond_slave_get_rtnl(slave_dev), *primary;
39276101391dSnikolay@redhat.com 	struct bonding *bond;
39286101391dSnikolay@redhat.com 	struct net_device *bond_dev;
39291da177e4SLinus Torvalds 
39306101391dSnikolay@redhat.com 	/* A netdev event can be generated while enslaving a device
39316101391dSnikolay@redhat.com 	 * before netdev_rx_handler_register is called in which case
39326101391dSnikolay@redhat.com 	 * slave will be NULL
39336101391dSnikolay@redhat.com 	 */
3934e2a7420dSJarod Wilson 	if (!slave) {
3935e2a7420dSJarod Wilson 		netdev_dbg(slave_dev, "%s called on NULL slave\n", __func__);
39366101391dSnikolay@redhat.com 		return NOTIFY_DONE;
3937e2a7420dSJarod Wilson 	}
3938e2a7420dSJarod Wilson 
39396101391dSnikolay@redhat.com 	bond_dev = slave->bond->dev;
39406101391dSnikolay@redhat.com 	bond = slave->bond;
3941059b47e8SNikolay Aleksandrov 	primary = rtnl_dereference(bond->primary_slave);
39426101391dSnikolay@redhat.com 
3943e2a7420dSJarod Wilson 	slave_dbg(bond_dev, slave_dev, "%s called\n", __func__);
3944e2a7420dSJarod Wilson 
39451da177e4SLinus Torvalds 	switch (event) {
39461da177e4SLinus Torvalds 	case NETDEV_UNREGISTER:
39478d2ada77Snikolay@redhat.com 		if (bond_dev->type != ARPHRD_ETHER)
39481284cd3aSJay Vosburgh 			bond_release_and_destroy(bond_dev, slave_dev);
39491284cd3aSJay Vosburgh 		else
3950f51048c3SWANG Cong 			__bond_release_one(bond_dev, slave_dev, false, true);
39511da177e4SLinus Torvalds 		break;
395298f41f69SWeiping Pan 	case NETDEV_UP:
39531da177e4SLinus Torvalds 	case NETDEV_CHANGE:
39544d2c0cdaSMahesh Bandewar 		/* For 802.3ad mode only:
39554d2c0cdaSMahesh Bandewar 		 * Getting invalid Speed/Duplex values here will put slave
395633403121SJarod Wilson 		 * in weird state. Mark it as link-fail if the link was
395733403121SJarod Wilson 		 * previously up or link-down if it hasn't yet come up, and
395833403121SJarod Wilson 		 * let link-monitoring (miimon) set it right when correct
395933403121SJarod Wilson 		 * speeds/duplex are available.
39604d2c0cdaSMahesh Bandewar 		 */
39614d2c0cdaSMahesh Bandewar 		if (bond_update_speed_duplex(slave) &&
396233403121SJarod Wilson 		    BOND_MODE(bond) == BOND_MODE_8023AD) {
396333403121SJarod Wilson 			if (slave->last_link_up)
3964ea53abfaSJarod Wilson 				slave->link = BOND_LINK_FAIL;
396533403121SJarod Wilson 			else
396633403121SJarod Wilson 				slave->link = BOND_LINK_DOWN;
396733403121SJarod Wilson 		}
39684d2c0cdaSMahesh Bandewar 
396952bc6716SMahesh Bandewar 		if (BOND_MODE(bond) == BOND_MODE_8023AD)
397052bc6716SMahesh Bandewar 			bond_3ad_adapter_speed_duplex_changed(slave);
3971df561f66SGustavo A. R. Silva 		fallthrough;
3972950ddcb1SMahesh Bandewar 	case NETDEV_DOWN:
3973ee637714SMahesh Bandewar 		/* Refresh slave-array if applicable!
3974ee637714SMahesh Bandewar 		 * If the setup does not use miimon or arpmon (mode-specific!),
3975ee637714SMahesh Bandewar 		 * then these events will not cause the slave-array to be
3976ee637714SMahesh Bandewar 		 * refreshed. This will cause xmit to use a slave that is not
3977ee637714SMahesh Bandewar 		 * usable. Avoid such situation by refeshing the array at these
3978ee637714SMahesh Bandewar 		 * events. If these (miimon/arpmon) parameters are configured
3979ee637714SMahesh Bandewar 		 * then array gets refreshed twice and that should be fine!
3980ee637714SMahesh Bandewar 		 */
3981e79c1055SDebabrata Banerjee 		if (bond_mode_can_use_xmit_hash(bond))
3982ee637714SMahesh Bandewar 			bond_update_slave_arr(bond, NULL);
39831da177e4SLinus Torvalds 		break;
39841da177e4SLinus Torvalds 	case NETDEV_CHANGEMTU:
3985547942caSNikolay Aleksandrov 		/* TODO: Should slaves be allowed to
39861da177e4SLinus Torvalds 		 * independently alter their MTU?  For
39871da177e4SLinus Torvalds 		 * an active-backup bond, slaves need
39881da177e4SLinus Torvalds 		 * not be the same type of device, so
39891da177e4SLinus Torvalds 		 * MTUs may vary.  For other modes,
39901da177e4SLinus Torvalds 		 * slaves arguably should have the
39911da177e4SLinus Torvalds 		 * same MTUs. To do this, we'd need to
39921da177e4SLinus Torvalds 		 * take over the slave's change_mtu
39931da177e4SLinus Torvalds 		 * function for the duration of their
39941da177e4SLinus Torvalds 		 * servitude.
39951da177e4SLinus Torvalds 		 */
39961da177e4SLinus Torvalds 		break;
39971da177e4SLinus Torvalds 	case NETDEV_CHANGENAME:
39983ec775b9SVeaceslav Falico 		/* we don't care if we don't have primary set */
3999ec0865a9SVeaceslav Falico 		if (!bond_uses_primary(bond) ||
40003ec775b9SVeaceslav Falico 		    !bond->params.primary[0])
40013ec775b9SVeaceslav Falico 			break;
40023ec775b9SVeaceslav Falico 
4003059b47e8SNikolay Aleksandrov 		if (slave == primary) {
40043ec775b9SVeaceslav Falico 			/* slave's name changed - he's no longer primary */
4005059b47e8SNikolay Aleksandrov 			RCU_INIT_POINTER(bond->primary_slave, NULL);
40063ec775b9SVeaceslav Falico 		} else if (!strcmp(slave_dev->name, bond->params.primary)) {
40073ec775b9SVeaceslav Falico 			/* we have a new primary slave */
4008059b47e8SNikolay Aleksandrov 			rcu_assign_pointer(bond->primary_slave, slave);
40093ec775b9SVeaceslav Falico 		} else { /* we didn't change primary - exit */
40103ec775b9SVeaceslav Falico 			break;
40113ec775b9SVeaceslav Falico 		}
40123ec775b9SVeaceslav Falico 
401376444f50SVeaceslav Falico 		netdev_info(bond->dev, "Primary slave changed to %s, reselecting active slave\n",
4014059b47e8SNikolay Aleksandrov 			    primary ? slave_dev->name : "none");
4015f80889a5Sdingtianhong 
4016f80889a5Sdingtianhong 		block_netpoll_tx();
40173ec775b9SVeaceslav Falico 		bond_select_active_slave(bond);
4018f80889a5Sdingtianhong 		unblock_netpoll_tx();
40191da177e4SLinus Torvalds 		break;
40208531c5ffSArthur Kepner 	case NETDEV_FEAT_CHANGE:
4021ae9b15fbSTaehee Yoo 		if (!bond->notifier_ctx) {
4022ae9b15fbSTaehee Yoo 			bond->notifier_ctx = true;
40238531c5ffSArthur Kepner 			bond_compute_features(bond);
4024ae9b15fbSTaehee Yoo 			bond->notifier_ctx = false;
4025ae9b15fbSTaehee Yoo 		}
40268531c5ffSArthur Kepner 		break;
40274aa5dee4SJiri Pirko 	case NETDEV_RESEND_IGMP:
40284aa5dee4SJiri Pirko 		/* Propagate to master device */
40294aa5dee4SJiri Pirko 		call_netdevice_notifiers(event, slave->bond->dev);
40304aa5dee4SJiri Pirko 		break;
4031cb9e6e58SLorenzo Bianconi 	case NETDEV_XDP_FEAT_CHANGE:
4032cb9e6e58SLorenzo Bianconi 		bond_xdp_set_features(bond_dev);
4033cb9e6e58SLorenzo Bianconi 		break;
40341da177e4SLinus Torvalds 	default:
40351da177e4SLinus Torvalds 		break;
40361da177e4SLinus Torvalds 	}
40371da177e4SLinus Torvalds 
40381da177e4SLinus Torvalds 	return NOTIFY_DONE;
40391da177e4SLinus Torvalds }
40401da177e4SLinus Torvalds 
4041547942caSNikolay Aleksandrov /* bond_netdev_event: handle netdev notifier chain events.
40421da177e4SLinus Torvalds  *
40431da177e4SLinus Torvalds  * This function receives events for the netdev chain.  The caller (an
4044e041c683SAlan Stern  * ioctl handler calling blocking_notifier_call_chain) holds the necessary
40451da177e4SLinus Torvalds  * locks for us to safely manipulate the slave devices (RTNL lock,
40461da177e4SLinus Torvalds  * dev_probe_lock).
40471da177e4SLinus Torvalds  */
bond_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)40483d632c3fSStephen Hemminger static int bond_netdev_event(struct notifier_block *this,
40493d632c3fSStephen Hemminger 			     unsigned long event, void *ptr)
40501da177e4SLinus Torvalds {
4051351638e7SJiri Pirko 	struct net_device *event_dev = netdev_notifier_info_to_dev(ptr);
40521da177e4SLinus Torvalds 
405375466dceSJarod Wilson 	netdev_dbg(event_dev, "%s received %s\n",
405475466dceSJarod Wilson 		   __func__, netdev_cmd_to_name(event));
40551da177e4SLinus Torvalds 
40560b680e75SJay Vosburgh 	if (!(event_dev->priv_flags & IFF_BONDING))
40570b680e75SJay Vosburgh 		return NOTIFY_DONE;
40580b680e75SJay Vosburgh 
40591da177e4SLinus Torvalds 	if (event_dev->flags & IFF_MASTER) {
406092480b39SSabrina Dubroca 		int ret;
406192480b39SSabrina Dubroca 
406292480b39SSabrina Dubroca 		ret = bond_master_netdev_event(event, event_dev);
406392480b39SSabrina Dubroca 		if (ret != NOTIFY_DONE)
406492480b39SSabrina Dubroca 			return ret;
40651da177e4SLinus Torvalds 	}
40661da177e4SLinus Torvalds 
4067e2a7420dSJarod Wilson 	if (event_dev->flags & IFF_SLAVE)
40681da177e4SLinus Torvalds 		return bond_slave_netdev_event(event, event_dev);
40691da177e4SLinus Torvalds 
40701da177e4SLinus Torvalds 	return NOTIFY_DONE;
40711da177e4SLinus Torvalds }
40721da177e4SLinus Torvalds 
40731da177e4SLinus Torvalds static struct notifier_block bond_netdev_notifier = {
40741da177e4SLinus Torvalds 	.notifier_call = bond_netdev_event,
40751da177e4SLinus Torvalds };
40761da177e4SLinus Torvalds 
4077169a3e66SJay Vosburgh /*---------------------------- Hashing Policies -----------------------------*/
4078169a3e66SJay Vosburgh 
4079a815bde5SJussi Maki /* Helper to access data in a packet, with or without a backing skb.
4080a815bde5SJussi Maki  * If skb is given the data is linearized if necessary via pskb_may_pull.
4081a815bde5SJussi Maki  */
bond_pull_data(struct sk_buff * skb,const void * data,int hlen,int n)4082a815bde5SJussi Maki static inline const void *bond_pull_data(struct sk_buff *skb,
4083a815bde5SJussi Maki 					 const void *data, int hlen, int n)
4084169a3e66SJay Vosburgh {
4085a815bde5SJussi Maki 	if (likely(n <= hlen))
4086a815bde5SJussi Maki 		return data;
4087a815bde5SJussi Maki 	else if (skb && likely(pskb_may_pull(skb, n)))
4088d93f3f99SJiri Wiesner 		return skb->data;
4089169a3e66SJay Vosburgh 
4090a815bde5SJussi Maki 	return NULL;
40916b923cb7SJohn Eaglesham }
40926b923cb7SJohn Eaglesham 
4093a815bde5SJussi Maki /* L2 hash helper */
bond_eth_hash(struct sk_buff * skb,const void * data,int mhoff,int hlen)4094a815bde5SJussi Maki static inline u32 bond_eth_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
4095a815bde5SJussi Maki {
4096a815bde5SJussi Maki 	struct ethhdr *ep;
4097a815bde5SJussi Maki 
4098a815bde5SJussi Maki 	data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
4099a815bde5SJussi Maki 	if (!data)
4100a815bde5SJussi Maki 		return 0;
4101a815bde5SJussi Maki 
4102a815bde5SJussi Maki 	ep = (struct ethhdr *)(data + mhoff);
41039e2ee5c7SJussi Maki 	return ep->h_dest[5] ^ ep->h_source[5] ^ be16_to_cpu(ep->h_proto);
4104a815bde5SJussi Maki }
4105a815bde5SJussi Maki 
bond_flow_ip(struct sk_buff * skb,struct flow_keys * fk,const void * data,int hlen,__be16 l2_proto,int * nhoff,int * ip_proto,bool l34)4106a815bde5SJussi Maki static bool bond_flow_ip(struct sk_buff *skb, struct flow_keys *fk, const void *data,
4107a815bde5SJussi Maki 			 int hlen, __be16 l2_proto, int *nhoff, int *ip_proto, bool l34)
4108df98be06SMatteo Croce {
4109df98be06SMatteo Croce 	const struct ipv6hdr *iph6;
4110df98be06SMatteo Croce 	const struct iphdr *iph;
4111df98be06SMatteo Croce 
4112a815bde5SJussi Maki 	if (l2_proto == htons(ETH_P_IP)) {
4113a815bde5SJussi Maki 		data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph));
4114a815bde5SJussi Maki 		if (!data)
4115df98be06SMatteo Croce 			return false;
4116a815bde5SJussi Maki 
4117a815bde5SJussi Maki 		iph = (const struct iphdr *)(data + *nhoff);
4118df98be06SMatteo Croce 		iph_to_flow_copy_v4addrs(fk, iph);
4119a815bde5SJussi Maki 		*nhoff += iph->ihl << 2;
4120df98be06SMatteo Croce 		if (!ip_is_fragment(iph))
4121a815bde5SJussi Maki 			*ip_proto = iph->protocol;
4122a815bde5SJussi Maki 	} else if (l2_proto == htons(ETH_P_IPV6)) {
4123a815bde5SJussi Maki 		data = bond_pull_data(skb, data, hlen, *nhoff + sizeof(*iph6));
4124a815bde5SJussi Maki 		if (!data)
4125df98be06SMatteo Croce 			return false;
4126a815bde5SJussi Maki 
4127a815bde5SJussi Maki 		iph6 = (const struct ipv6hdr *)(data + *nhoff);
4128df98be06SMatteo Croce 		iph_to_flow_copy_v6addrs(fk, iph6);
4129a815bde5SJussi Maki 		*nhoff += sizeof(*iph6);
4130a815bde5SJussi Maki 		*ip_proto = iph6->nexthdr;
4131df98be06SMatteo Croce 	} else {
4132df98be06SMatteo Croce 		return false;
4133df98be06SMatteo Croce 	}
4134df98be06SMatteo Croce 
4135a815bde5SJussi Maki 	if (l34 && *ip_proto >= 0)
4136a815bde5SJussi Maki 		fk->ports.ports = __skb_flow_get_ports(skb, *nhoff, *ip_proto, data, hlen);
4137df98be06SMatteo Croce 
4138df98be06SMatteo Croce 	return true;
4139df98be06SMatteo Croce }
4140df98be06SMatteo Croce 
bond_vlan_srcmac_hash(struct sk_buff * skb,const void * data,int mhoff,int hlen)4141a815bde5SJussi Maki static u32 bond_vlan_srcmac_hash(struct sk_buff *skb, const void *data, int mhoff, int hlen)
41427b8fc010SJarod Wilson {
41437b8fc010SJarod Wilson 	u32 srcmac_vendor = 0, srcmac_dev = 0;
414439a0876dSJussi Maki 	struct ethhdr *mac_hdr;
414539a0876dSJussi Maki 	u16 vlan = 0;
41467b8fc010SJarod Wilson 	int i;
41477b8fc010SJarod Wilson 
4148a815bde5SJussi Maki 	data = bond_pull_data(skb, data, hlen, mhoff + sizeof(struct ethhdr));
4149a815bde5SJussi Maki 	if (!data)
4150a815bde5SJussi Maki 		return 0;
4151a815bde5SJussi Maki 	mac_hdr = (struct ethhdr *)(data + mhoff);
4152a815bde5SJussi Maki 
41537b8fc010SJarod Wilson 	for (i = 0; i < 3; i++)
41547b8fc010SJarod Wilson 		srcmac_vendor = (srcmac_vendor << 8) | mac_hdr->h_source[i];
41557b8fc010SJarod Wilson 
41567b8fc010SJarod Wilson 	for (i = 3; i < ETH_ALEN; i++)
41577b8fc010SJarod Wilson 		srcmac_dev = (srcmac_dev << 8) | mac_hdr->h_source[i];
41587b8fc010SJarod Wilson 
415939a0876dSJussi Maki 	if (skb && skb_vlan_tag_present(skb))
41607b8fc010SJarod Wilson 		vlan = skb_vlan_tag_get(skb);
41617b8fc010SJarod Wilson 
41627b8fc010SJarod Wilson 	return vlan ^ srcmac_vendor ^ srcmac_dev;
41637b8fc010SJarod Wilson }
41647b8fc010SJarod Wilson 
416532819dc1SNikolay Aleksandrov /* Extract the appropriate headers based on bond's xmit policy */
bond_flow_dissect(struct bonding * bond,struct sk_buff * skb,const void * data,__be16 l2_proto,int nhoff,int hlen,struct flow_keys * fk)4166a815bde5SJussi Maki static bool bond_flow_dissect(struct bonding *bond, struct sk_buff *skb, const void *data,
4167a815bde5SJussi Maki 			      __be16 l2_proto, int nhoff, int hlen, struct flow_keys *fk)
41686b923cb7SJohn Eaglesham {
4169df98be06SMatteo Croce 	bool l34 = bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER34;
4170a815bde5SJussi Maki 	int ip_proto = -1;
41716b923cb7SJohn Eaglesham 
41727b8fc010SJarod Wilson 	switch (bond->params.xmit_policy) {
41737b8fc010SJarod Wilson 	case BOND_XMIT_POLICY_ENCAP23:
41747b8fc010SJarod Wilson 	case BOND_XMIT_POLICY_ENCAP34:
417558deb77cSMatteo Croce 		memset(fk, 0, sizeof(*fk));
417658deb77cSMatteo Croce 		return __skb_flow_dissect(NULL, skb, &flow_keys_bonding,
4177a815bde5SJussi Maki 					  fk, data, l2_proto, nhoff, hlen, 0);
41787b8fc010SJarod Wilson 	default:
41797b8fc010SJarod Wilson 		break;
418058deb77cSMatteo Croce 	}
418132819dc1SNikolay Aleksandrov 
418206635a35SJiri Pirko 	fk->ports.ports = 0;
418358deb77cSMatteo Croce 	memset(&fk->icmp, 0, sizeof(fk->icmp));
4184a815bde5SJussi Maki 	if (!bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34))
418532819dc1SNikolay Aleksandrov 		return false;
4186df98be06SMatteo Croce 
4187df98be06SMatteo Croce 	/* ICMP error packets contains at least 8 bytes of the header
4188df98be06SMatteo Croce 	 * of the packet which generated the error. Use this information
4189df98be06SMatteo Croce 	 * to correlate ICMP error packets within the same flow which
4190df98be06SMatteo Croce 	 * generated the error.
4191df98be06SMatteo Croce 	 */
4192a815bde5SJussi Maki 	if (ip_proto == IPPROTO_ICMP || ip_proto == IPPROTO_ICMPV6) {
4193a815bde5SJussi Maki 		skb_flow_get_icmp_tci(skb, &fk->icmp, data, nhoff, hlen);
4194a815bde5SJussi Maki 		if (ip_proto == IPPROTO_ICMP) {
4195df98be06SMatteo Croce 			if (!icmp_is_err(fk->icmp.type))
4196df98be06SMatteo Croce 				return true;
4197df98be06SMatteo Croce 
4198a815bde5SJussi Maki 			nhoff += sizeof(struct icmphdr);
4199a815bde5SJussi Maki 		} else if (ip_proto == IPPROTO_ICMPV6) {
4200df98be06SMatteo Croce 			if (!icmpv6_is_err(fk->icmp.type))
4201df98be06SMatteo Croce 				return true;
4202df98be06SMatteo Croce 
4203a815bde5SJussi Maki 			nhoff += sizeof(struct icmp6hdr);
4204df98be06SMatteo Croce 		}
4205a815bde5SJussi Maki 		return bond_flow_ip(skb, fk, data, hlen, l2_proto, &nhoff, &ip_proto, l34);
420658deb77cSMatteo Croce 	}
420732819dc1SNikolay Aleksandrov 
420832819dc1SNikolay Aleksandrov 	return true;
42096b923cb7SJohn Eaglesham }
42106b923cb7SJohn Eaglesham 
bond_ip_hash(u32 hash,struct flow_keys * flow,int xmit_policy)421149aefd13Ssuresh kumar static u32 bond_ip_hash(u32 hash, struct flow_keys *flow, int xmit_policy)
42125b998545STariq Toukan {
42135b998545STariq Toukan 	hash ^= (__force u32)flow_get_u32_dst(flow) ^
42145b998545STariq Toukan 		(__force u32)flow_get_u32_src(flow);
42155b998545STariq Toukan 	hash ^= (hash >> 16);
42165b998545STariq Toukan 	hash ^= (hash >> 8);
421749aefd13Ssuresh kumar 
42185b998545STariq Toukan 	/* discard lowest hash bit to deal with the common even ports pattern */
421949aefd13Ssuresh kumar 	if (xmit_policy == BOND_XMIT_POLICY_LAYER34 ||
422049aefd13Ssuresh kumar 		xmit_policy == BOND_XMIT_POLICY_ENCAP34)
42215b998545STariq Toukan 		return hash >> 1;
422249aefd13Ssuresh kumar 
422349aefd13Ssuresh kumar 	return hash;
42245b998545STariq Toukan }
42255b998545STariq Toukan 
4226a815bde5SJussi Maki /* Generate hash based on xmit policy. If @skb is given it is used to linearize
4227a815bde5SJussi Maki  * the data as required, but this function can be used without it if the data is
4228a815bde5SJussi Maki  * known to be linear (e.g. with xdp_buff).
4229a815bde5SJussi Maki  */
__bond_xmit_hash(struct bonding * bond,struct sk_buff * skb,const void * data,__be16 l2_proto,int mhoff,int nhoff,int hlen)4230a815bde5SJussi Maki static u32 __bond_xmit_hash(struct bonding *bond, struct sk_buff *skb, const void *data,
4231a815bde5SJussi Maki 			    __be16 l2_proto, int mhoff, int nhoff, int hlen)
4232a815bde5SJussi Maki {
4233a815bde5SJussi Maki 	struct flow_keys flow;
4234a815bde5SJussi Maki 	u32 hash;
4235a815bde5SJussi Maki 
4236a815bde5SJussi Maki 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_VLAN_SRCMAC)
4237a815bde5SJussi Maki 		return bond_vlan_srcmac_hash(skb, data, mhoff, hlen);
4238a815bde5SJussi Maki 
4239a815bde5SJussi Maki 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER2 ||
4240a815bde5SJussi Maki 	    !bond_flow_dissect(bond, skb, data, l2_proto, nhoff, hlen, &flow))
4241a815bde5SJussi Maki 		return bond_eth_hash(skb, data, mhoff, hlen);
4242a815bde5SJussi Maki 
4243a815bde5SJussi Maki 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_LAYER23 ||
4244a815bde5SJussi Maki 	    bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP23) {
4245a815bde5SJussi Maki 		hash = bond_eth_hash(skb, data, mhoff, hlen);
4246a815bde5SJussi Maki 	} else {
4247a815bde5SJussi Maki 		if (flow.icmp.id)
4248a815bde5SJussi Maki 			memcpy(&hash, &flow.icmp, sizeof(hash));
4249a815bde5SJussi Maki 		else
4250a815bde5SJussi Maki 			memcpy(&hash, &flow.ports.ports, sizeof(hash));
4251a815bde5SJussi Maki 	}
4252a815bde5SJussi Maki 
425349aefd13Ssuresh kumar 	return bond_ip_hash(hash, &flow, bond->params.xmit_policy);
4254a815bde5SJussi Maki }
4255a815bde5SJussi Maki 
425632819dc1SNikolay Aleksandrov /**
425732819dc1SNikolay Aleksandrov  * bond_xmit_hash - generate a hash value based on the xmit policy
425832819dc1SNikolay Aleksandrov  * @bond: bonding device
425932819dc1SNikolay Aleksandrov  * @skb: buffer to use for headers
426032819dc1SNikolay Aleksandrov  *
426132819dc1SNikolay Aleksandrov  * This function will extract the necessary headers from the skb buffer and use
426232819dc1SNikolay Aleksandrov  * them to generate a hash based on the xmit_policy set in the bonding device
42636b923cb7SJohn Eaglesham  */
bond_xmit_hash(struct bonding * bond,struct sk_buff * skb)4264ee62e868SMahesh Bandewar u32 bond_xmit_hash(struct bonding *bond, struct sk_buff *skb)
42656b923cb7SJohn Eaglesham {
42664b1b865eSEric Dumazet 	if (bond->params.xmit_policy == BOND_XMIT_POLICY_ENCAP34 &&
42674b1b865eSEric Dumazet 	    skb->l4_hash)
42684b1b865eSEric Dumazet 		return skb->hash;
42694b1b865eSEric Dumazet 
4270429e3d12SMoshe Tal 	return __bond_xmit_hash(bond, skb, skb->data, skb->protocol,
42716a940abdSEric Dumazet 				0, skb_network_offset(skb),
4272a815bde5SJussi Maki 				skb_headlen(skb));
427358deb77cSMatteo Croce }
42746b923cb7SJohn Eaglesham 
42759e2ee5c7SJussi Maki /**
42769e2ee5c7SJussi Maki  * bond_xmit_hash_xdp - generate a hash value based on the xmit policy
42779e2ee5c7SJussi Maki  * @bond: bonding device
42789e2ee5c7SJussi Maki  * @xdp: buffer to use for headers
42799e2ee5c7SJussi Maki  *
42809e2ee5c7SJussi Maki  * The XDP variant of bond_xmit_hash.
42819e2ee5c7SJussi Maki  */
bond_xmit_hash_xdp(struct bonding * bond,struct xdp_buff * xdp)42829e2ee5c7SJussi Maki static u32 bond_xmit_hash_xdp(struct bonding *bond, struct xdp_buff *xdp)
42839e2ee5c7SJussi Maki {
42849e2ee5c7SJussi Maki 	struct ethhdr *eth;
42859e2ee5c7SJussi Maki 
42869e2ee5c7SJussi Maki 	if (xdp->data + sizeof(struct ethhdr) > xdp->data_end)
42879e2ee5c7SJussi Maki 		return 0;
42889e2ee5c7SJussi Maki 
42899e2ee5c7SJussi Maki 	eth = (struct ethhdr *)xdp->data;
42909e2ee5c7SJussi Maki 
42919e2ee5c7SJussi Maki 	return __bond_xmit_hash(bond, NULL, xdp->data, eth->h_proto, 0,
42929e2ee5c7SJussi Maki 				sizeof(struct ethhdr), xdp->data_end - xdp->data);
4293169a3e66SJay Vosburgh }
4294169a3e66SJay Vosburgh 
42951da177e4SLinus Torvalds /*-------------------------- Device entry points ----------------------------*/
42961da177e4SLinus Torvalds 
bond_work_init_all(struct bonding * bond)4297ea8ffc08SMahesh Bandewar void bond_work_init_all(struct bonding *bond)
4298fbb0c41bSnikolay@redhat.com {
4299fbb0c41bSnikolay@redhat.com 	INIT_DELAYED_WORK(&bond->mcast_work,
4300fbb0c41bSnikolay@redhat.com 			  bond_resend_igmp_join_requests_delayed);
4301fbb0c41bSnikolay@redhat.com 	INIT_DELAYED_WORK(&bond->alb_work, bond_alb_monitor);
4302fbb0c41bSnikolay@redhat.com 	INIT_DELAYED_WORK(&bond->mii_work, bond_mii_monitor);
4303d5e73f7bSMahesh Bandewar 	INIT_DELAYED_WORK(&bond->arp_work, bond_arp_monitor);
4304fbb0c41bSnikolay@redhat.com 	INIT_DELAYED_WORK(&bond->ad_work, bond_3ad_state_machine_handler);
4305ee637714SMahesh Bandewar 	INIT_DELAYED_WORK(&bond->slave_arr_work, bond_slave_arr_handler);
4306fbb0c41bSnikolay@redhat.com }
4307fbb0c41bSnikolay@redhat.com 
bond_work_cancel_all(struct bonding * bond)4308fbb0c41bSnikolay@redhat.com static void bond_work_cancel_all(struct bonding *bond)
4309fbb0c41bSnikolay@redhat.com {
4310fbb0c41bSnikolay@redhat.com 	cancel_delayed_work_sync(&bond->mii_work);
4311fbb0c41bSnikolay@redhat.com 	cancel_delayed_work_sync(&bond->arp_work);
4312fbb0c41bSnikolay@redhat.com 	cancel_delayed_work_sync(&bond->alb_work);
4313fbb0c41bSnikolay@redhat.com 	cancel_delayed_work_sync(&bond->ad_work);
4314fbb0c41bSnikolay@redhat.com 	cancel_delayed_work_sync(&bond->mcast_work);
4315ee637714SMahesh Bandewar 	cancel_delayed_work_sync(&bond->slave_arr_work);
4316fbb0c41bSnikolay@redhat.com }
4317fbb0c41bSnikolay@redhat.com 
bond_open(struct net_device * bond_dev)43181da177e4SLinus Torvalds static int bond_open(struct net_device *bond_dev)
43191da177e4SLinus Torvalds {
4320454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
43219caff1e7SVeaceslav Falico 	struct list_head *iter;
4322ba3211ccSPeter Pan(潘卫平) 	struct slave *slave;
43231da177e4SLinus Torvalds 
43240e400d60SJonathan Toppins 	if (BOND_MODE(bond) == BOND_MODE_ROUNDROBIN && !bond->rr_tx_counter) {
43250e400d60SJonathan Toppins 		bond->rr_tx_counter = alloc_percpu(u32);
43260e400d60SJonathan Toppins 		if (!bond->rr_tx_counter)
43270e400d60SJonathan Toppins 			return -ENOMEM;
43280e400d60SJonathan Toppins 	}
43290e400d60SJonathan Toppins 
4330ba3211ccSPeter Pan(潘卫平) 	/* reset slave->backup and slave->inactive */
43310965a1f3SVeaceslav Falico 	if (bond_has_slaves(bond)) {
43329caff1e7SVeaceslav Falico 		bond_for_each_slave(bond, slave, iter) {
43334740d638SEric Dumazet 			if (bond_uses_primary(bond) &&
43344740d638SEric Dumazet 			    slave != rcu_access_pointer(bond->curr_active_slave)) {
43355e5b0665Sdingtianhong 				bond_set_slave_inactive_flags(slave,
43365e5b0665Sdingtianhong 							      BOND_SLAVE_NOTIFY_NOW);
43378bbe71a5SWilson Kok 			} else if (BOND_MODE(bond) != BOND_MODE_8023AD) {
43385e5b0665Sdingtianhong 				bond_set_slave_active_flags(slave,
43395e5b0665Sdingtianhong 							    BOND_SLAVE_NOTIFY_NOW);
4340ba3211ccSPeter Pan(潘卫平) 			}
4341ba3211ccSPeter Pan(潘卫平) 		}
4342ba3211ccSPeter Pan(潘卫平) 	}
4343ba3211ccSPeter Pan(潘卫平) 
434458402054SHolger Eitzenberger 	if (bond_is_lb(bond)) {
43451da177e4SLinus Torvalds 		/* bond_alb_initialize must be called before the timer
43461da177e4SLinus Torvalds 		 * is started.
43471da177e4SLinus Torvalds 		 */
434801844098SVeaceslav Falico 		if (bond_alb_initialize(bond, (BOND_MODE(bond) == BOND_MODE_ALB)))
4349b473946aSstephen hemminger 			return -ENOMEM;
4350e79c1055SDebabrata Banerjee 		if (bond->params.tlb_dynamic_lb || BOND_MODE(bond) == BOND_MODE_ALB)
43511b76b316SJay Vosburgh 			queue_delayed_work(bond->wq, &bond->alb_work, 0);
43521da177e4SLinus Torvalds 	}
43531da177e4SLinus Torvalds 
4354fbb0c41bSnikolay@redhat.com 	if (bond->params.miimon)  /* link check interval, in milliseconds. */
43551b76b316SJay Vosburgh 		queue_delayed_work(bond->wq, &bond->mii_work, 0);
43561da177e4SLinus Torvalds 
43571da177e4SLinus Torvalds 	if (bond->params.arp_interval) {  /* arp interval, in milliseconds. */
43581b76b316SJay Vosburgh 		queue_delayed_work(bond->wq, &bond->arp_work, 0);
43594e24be01SHangbin Liu 		bond->recv_probe = bond_rcv_validate;
43601da177e4SLinus Torvalds 	}
43611da177e4SLinus Torvalds 
436201844098SVeaceslav Falico 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
43631b76b316SJay Vosburgh 		queue_delayed_work(bond->wq, &bond->ad_work, 0);
43641da177e4SLinus Torvalds 		/* register to receive LACPDUs */
43653aba891dSJiri Pirko 		bond->recv_probe = bond_3ad_lacpdu_recv;
4366fd989c83SJay Vosburgh 		bond_3ad_initiate_agg_selection(bond, 1);
436786247abaSBenjamin Poirier 
436886247abaSBenjamin Poirier 		bond_for_each_slave(bond, slave, iter)
436986247abaSBenjamin Poirier 			dev_mc_add(slave->dev, lacpdu_mcast_addr);
43701da177e4SLinus Torvalds 	}
43711da177e4SLinus Torvalds 
4372e79c1055SDebabrata Banerjee 	if (bond_mode_can_use_xmit_hash(bond))
4373ee637714SMahesh Bandewar 		bond_update_slave_arr(bond, NULL);
4374ee637714SMahesh Bandewar 
43751da177e4SLinus Torvalds 	return 0;
43761da177e4SLinus Torvalds }
43771da177e4SLinus Torvalds 
bond_close(struct net_device * bond_dev)43781da177e4SLinus Torvalds static int bond_close(struct net_device *bond_dev)
43791da177e4SLinus Torvalds {
4380454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
438186247abaSBenjamin Poirier 	struct slave *slave;
43821da177e4SLinus Torvalds 
4383fbb0c41bSnikolay@redhat.com 	bond_work_cancel_all(bond);
43846c8d23f7Snikolay@redhat.com 	bond->send_peer_notif = 0;
4385ee8487c0Snikolay@redhat.com 	if (bond_is_lb(bond))
43861da177e4SLinus Torvalds 		bond_alb_deinitialize(bond);
43873aba891dSJiri Pirko 	bond->recv_probe = NULL;
43881da177e4SLinus Torvalds 
438986247abaSBenjamin Poirier 	if (bond_uses_primary(bond)) {
439086247abaSBenjamin Poirier 		rcu_read_lock();
439186247abaSBenjamin Poirier 		slave = rcu_dereference(bond->curr_active_slave);
439286247abaSBenjamin Poirier 		if (slave)
439386247abaSBenjamin Poirier 			bond_hw_addr_flush(bond_dev, slave->dev);
439486247abaSBenjamin Poirier 		rcu_read_unlock();
439586247abaSBenjamin Poirier 	} else {
439686247abaSBenjamin Poirier 		struct list_head *iter;
439786247abaSBenjamin Poirier 
439886247abaSBenjamin Poirier 		bond_for_each_slave(bond, slave, iter)
439986247abaSBenjamin Poirier 			bond_hw_addr_flush(bond_dev, slave->dev);
440086247abaSBenjamin Poirier 	}
440186247abaSBenjamin Poirier 
44021da177e4SLinus Torvalds 	return 0;
44031da177e4SLinus Torvalds }
44041da177e4SLinus Torvalds 
4405fe30937bSEric Dumazet /* fold stats, assuming all rtnl_link_stats64 fields are u64, but
4406fe30937bSEric Dumazet  * that some drivers can provide 32bit values only.
4407fe30937bSEric Dumazet  */
bond_fold_stats(struct rtnl_link_stats64 * _res,const struct rtnl_link_stats64 * _new,const struct rtnl_link_stats64 * _old)4408fe30937bSEric Dumazet static void bond_fold_stats(struct rtnl_link_stats64 *_res,
4409fe30937bSEric Dumazet 			    const struct rtnl_link_stats64 *_new,
4410fe30937bSEric Dumazet 			    const struct rtnl_link_stats64 *_old)
4411fe30937bSEric Dumazet {
4412fe30937bSEric Dumazet 	const u64 *new = (const u64 *)_new;
4413fe30937bSEric Dumazet 	const u64 *old = (const u64 *)_old;
4414fe30937bSEric Dumazet 	u64 *res = (u64 *)_res;
4415fe30937bSEric Dumazet 	int i;
4416fe30937bSEric Dumazet 
4417fe30937bSEric Dumazet 	for (i = 0; i < sizeof(*_res) / sizeof(u64); i++) {
4418fe30937bSEric Dumazet 		u64 nv = new[i];
4419fe30937bSEric Dumazet 		u64 ov = old[i];
4420142c6594SEric Dumazet 		s64 delta = nv - ov;
4421fe30937bSEric Dumazet 
4422fe30937bSEric Dumazet 		/* detects if this particular field is 32bit only */
4423fe30937bSEric Dumazet 		if (((nv | ov) >> 32) == 0)
4424142c6594SEric Dumazet 			delta = (s64)(s32)((u32)nv - (u32)ov);
4425142c6594SEric Dumazet 
4426142c6594SEric Dumazet 		/* filter anomalies, some drivers reset their stats
4427142c6594SEric Dumazet 		 * at down/up events.
4428142c6594SEric Dumazet 		 */
4429142c6594SEric Dumazet 		if (delta > 0)
4430142c6594SEric Dumazet 			res[i] += delta;
4431fe30937bSEric Dumazet 	}
4432fe30937bSEric Dumazet }
4433fe30937bSEric Dumazet 
4434b3e80d44STaehee Yoo #ifdef CONFIG_LOCKDEP
bond_get_lowest_level_rcu(struct net_device * dev)4435b3e80d44STaehee Yoo static int bond_get_lowest_level_rcu(struct net_device *dev)
4436b3e80d44STaehee Yoo {
4437b3e80d44STaehee Yoo 	struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
4438b3e80d44STaehee Yoo 	struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
4439b3e80d44STaehee Yoo 	int cur = 0, max = 0;
4440b3e80d44STaehee Yoo 
4441b3e80d44STaehee Yoo 	now = dev;
4442b3e80d44STaehee Yoo 	iter = &dev->adj_list.lower;
4443b3e80d44STaehee Yoo 
4444b3e80d44STaehee Yoo 	while (1) {
4445b3e80d44STaehee Yoo 		next = NULL;
4446b3e80d44STaehee Yoo 		while (1) {
4447b3e80d44STaehee Yoo 			ldev = netdev_next_lower_dev_rcu(now, &iter);
4448b3e80d44STaehee Yoo 			if (!ldev)
4449b3e80d44STaehee Yoo 				break;
4450b3e80d44STaehee Yoo 
4451b3e80d44STaehee Yoo 			next = ldev;
4452b3e80d44STaehee Yoo 			niter = &ldev->adj_list.lower;
4453b3e80d44STaehee Yoo 			dev_stack[cur] = now;
4454b3e80d44STaehee Yoo 			iter_stack[cur++] = iter;
4455b3e80d44STaehee Yoo 			if (max <= cur)
4456b3e80d44STaehee Yoo 				max = cur;
4457b3e80d44STaehee Yoo 			break;
4458b3e80d44STaehee Yoo 		}
4459b3e80d44STaehee Yoo 
4460b3e80d44STaehee Yoo 		if (!next) {
4461b3e80d44STaehee Yoo 			if (!cur)
4462b3e80d44STaehee Yoo 				return max;
4463b3e80d44STaehee Yoo 			next = dev_stack[--cur];
4464b3e80d44STaehee Yoo 			niter = iter_stack[cur];
4465b3e80d44STaehee Yoo 		}
4466b3e80d44STaehee Yoo 
4467b3e80d44STaehee Yoo 		now = next;
4468b3e80d44STaehee Yoo 		iter = niter;
4469b3e80d44STaehee Yoo 	}
4470b3e80d44STaehee Yoo 
4471b3e80d44STaehee Yoo 	return max;
4472b3e80d44STaehee Yoo }
4473b3e80d44STaehee Yoo #endif
4474b3e80d44STaehee Yoo 
bond_get_stats(struct net_device * bond_dev,struct rtnl_link_stats64 * stats)4475bc1f4470Sstephen hemminger static void bond_get_stats(struct net_device *bond_dev,
447628172739SEric Dumazet 			   struct rtnl_link_stats64 *stats)
44771da177e4SLinus Torvalds {
4478454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
447928172739SEric Dumazet 	struct rtnl_link_stats64 temp;
44809caff1e7SVeaceslav Falico 	struct list_head *iter;
44811da177e4SLinus Torvalds 	struct slave *slave;
4482b3e80d44STaehee Yoo 	int nest_level = 0;
44831da177e4SLinus Torvalds 
44841da177e4SLinus Torvalds 
4485fe30937bSEric Dumazet 	rcu_read_lock();
4486b3e80d44STaehee Yoo #ifdef CONFIG_LOCKDEP
4487b3e80d44STaehee Yoo 	nest_level = bond_get_lowest_level_rcu(bond_dev);
4488b3e80d44STaehee Yoo #endif
4489b3e80d44STaehee Yoo 
4490b3e80d44STaehee Yoo 	spin_lock_nested(&bond->stats_lock, nest_level);
4491b3e80d44STaehee Yoo 	memcpy(stats, &bond->bond_stats, sizeof(*stats));
4492b3e80d44STaehee Yoo 
4493fe30937bSEric Dumazet 	bond_for_each_slave_rcu(bond, slave, iter) {
4494fe30937bSEric Dumazet 		const struct rtnl_link_stats64 *new =
449528172739SEric Dumazet 			dev_get_stats(slave->dev, &temp);
4496eeda3fd6SStephen Hemminger 
4497fe30937bSEric Dumazet 		bond_fold_stats(stats, new, &slave->slave_stats);
44985f0c5f73SAndy Gospodarek 
44995f0c5f73SAndy Gospodarek 		/* save off the slave stats for the next run */
4500fe30937bSEric Dumazet 		memcpy(&slave->slave_stats, new, sizeof(*new));
45011da177e4SLinus Torvalds 	}
4502fe30937bSEric Dumazet 
45035f0c5f73SAndy Gospodarek 	memcpy(&bond->bond_stats, stats, sizeof(*stats));
4504fe30937bSEric Dumazet 	spin_unlock(&bond->stats_lock);
4505b3e80d44STaehee Yoo 	rcu_read_unlock();
45061da177e4SLinus Torvalds }
45071da177e4SLinus Torvalds 
bond_eth_ioctl(struct net_device * bond_dev,struct ifreq * ifr,int cmd)4508a7605370SArnd Bergmann static int bond_eth_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
45091da177e4SLinus Torvalds {
4510080a06e1SJiri Pirko 	struct bonding *bond = netdev_priv(bond_dev);
45111da177e4SLinus Torvalds 	struct mii_ioctl_data *mii = NULL;
45121da177e4SLinus Torvalds 
4513a7605370SArnd Bergmann 	netdev_dbg(bond_dev, "bond_eth_ioctl: cmd=%d\n", cmd);
45141da177e4SLinus Torvalds 
45151da177e4SLinus Torvalds 	switch (cmd) {
45161da177e4SLinus Torvalds 	case SIOCGMIIPHY:
45171da177e4SLinus Torvalds 		mii = if_mii(ifr);
45183d632c3fSStephen Hemminger 		if (!mii)
45191da177e4SLinus Torvalds 			return -EINVAL;
45203d632c3fSStephen Hemminger 
45211da177e4SLinus Torvalds 		mii->phy_id = 0;
4522df561f66SGustavo A. R. Silva 		fallthrough;
45231da177e4SLinus Torvalds 	case SIOCGMIIREG:
4524547942caSNikolay Aleksandrov 		/* We do this again just in case we were called by SIOCGMIIREG
45251da177e4SLinus Torvalds 		 * instead of SIOCGMIIPHY.
45261da177e4SLinus Torvalds 		 */
45271da177e4SLinus Torvalds 		mii = if_mii(ifr);
45283d632c3fSStephen Hemminger 		if (!mii)
45291da177e4SLinus Torvalds 			return -EINVAL;
45303d632c3fSStephen Hemminger 
45311da177e4SLinus Torvalds 		if (mii->reg_num == 1) {
45321da177e4SLinus Torvalds 			mii->val_out = 0;
45333d632c3fSStephen Hemminger 			if (netif_carrier_ok(bond->dev))
45341da177e4SLinus Torvalds 				mii->val_out = BMSR_LSTATUS;
45351da177e4SLinus Torvalds 		}
45361da177e4SLinus Torvalds 
453794dd016aSHangbin Liu 		break;
4538a7605370SArnd Bergmann 	default:
4539c0dabeb4SMaxim Georgiev 		return -EOPNOTSUPP;
4540a7605370SArnd Bergmann 	}
4541a7605370SArnd Bergmann 
4542c0dabeb4SMaxim Georgiev 	return 0;
4543a7605370SArnd Bergmann }
4544a7605370SArnd Bergmann 
bond_do_ioctl(struct net_device * bond_dev,struct ifreq * ifr,int cmd)4545a7605370SArnd Bergmann static int bond_do_ioctl(struct net_device *bond_dev, struct ifreq *ifr, int cmd)
4546a7605370SArnd Bergmann {
4547a7605370SArnd Bergmann 	struct bonding *bond = netdev_priv(bond_dev);
4548a7605370SArnd Bergmann 	struct net_device *slave_dev = NULL;
4549a7605370SArnd Bergmann 	struct ifbond k_binfo;
4550a7605370SArnd Bergmann 	struct ifbond __user *u_binfo = NULL;
4551a7605370SArnd Bergmann 	struct ifslave k_sinfo;
4552a7605370SArnd Bergmann 	struct ifslave __user *u_sinfo = NULL;
4553a7605370SArnd Bergmann 	struct bond_opt_value newval;
4554a7605370SArnd Bergmann 	struct net *net;
4555a7605370SArnd Bergmann 	int res = 0;
4556a7605370SArnd Bergmann 
4557a7605370SArnd Bergmann 	netdev_dbg(bond_dev, "bond_ioctl: cmd=%d\n", cmd);
4558a7605370SArnd Bergmann 
4559a7605370SArnd Bergmann 	switch (cmd) {
45601da177e4SLinus Torvalds 	case SIOCBONDINFOQUERY:
45611da177e4SLinus Torvalds 		u_binfo = (struct ifbond __user *)ifr->ifr_data;
45621da177e4SLinus Torvalds 
45633d632c3fSStephen Hemminger 		if (copy_from_user(&k_binfo, u_binfo, sizeof(ifbond)))
45641da177e4SLinus Torvalds 			return -EFAULT;
45651da177e4SLinus Torvalds 
45663d67576dSZhu Yanjun 		bond_info_query(bond_dev, &k_binfo);
45673d67576dSZhu Yanjun 		if (copy_to_user(u_binfo, &k_binfo, sizeof(ifbond)))
45681da177e4SLinus Torvalds 			return -EFAULT;
45691da177e4SLinus Torvalds 
45703d67576dSZhu Yanjun 		return 0;
45711da177e4SLinus Torvalds 	case SIOCBONDSLAVEINFOQUERY:
45721da177e4SLinus Torvalds 		u_sinfo = (struct ifslave __user *)ifr->ifr_data;
45731da177e4SLinus Torvalds 
45743d632c3fSStephen Hemminger 		if (copy_from_user(&k_sinfo, u_sinfo, sizeof(ifslave)))
45751da177e4SLinus Torvalds 			return -EFAULT;
45761da177e4SLinus Torvalds 
45771da177e4SLinus Torvalds 		res = bond_slave_info_query(bond_dev, &k_sinfo);
45783d632c3fSStephen Hemminger 		if (res == 0 &&
45793d632c3fSStephen Hemminger 		    copy_to_user(u_sinfo, &k_sinfo, sizeof(ifslave)))
45801da177e4SLinus Torvalds 			return -EFAULT;
45811da177e4SLinus Torvalds 
45821da177e4SLinus Torvalds 		return res;
45831da177e4SLinus Torvalds 	default:
45841da177e4SLinus Torvalds 		break;
45851da177e4SLinus Torvalds 	}
45861da177e4SLinus Torvalds 
4587387ff911SGao feng 	net = dev_net(bond_dev);
4588387ff911SGao feng 
4589387ff911SGao feng 	if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
45901da177e4SLinus Torvalds 		return -EPERM;
45911da177e4SLinus Torvalds 
45920917b933SYing Xue 	slave_dev = __dev_get_by_name(net, ifr->ifr_slave);
45931da177e4SLinus Torvalds 
4594e2a7420dSJarod Wilson 	slave_dbg(bond_dev, slave_dev, "slave_dev=%p:\n", slave_dev);
45951da177e4SLinus Torvalds 
45963d632c3fSStephen Hemminger 	if (!slave_dev)
45970917b933SYing Xue 		return -ENODEV;
45980917b933SYing Xue 
45991da177e4SLinus Torvalds 	switch (cmd) {
46001da177e4SLinus Torvalds 	case SIOCBONDENSLAVE:
460133eaf2a6SDavid Ahern 		res = bond_enslave(bond_dev, slave_dev, NULL);
46021da177e4SLinus Torvalds 		break;
46031da177e4SLinus Torvalds 	case SIOCBONDRELEASE:
46041da177e4SLinus Torvalds 		res = bond_release(bond_dev, slave_dev);
46051da177e4SLinus Torvalds 		break;
46061da177e4SLinus Torvalds 	case SIOCBONDSETHWADDR:
4607b9245914SPetr Machata 		res = bond_set_dev_addr(bond_dev, slave_dev);
46081da177e4SLinus Torvalds 		break;
46091da177e4SLinus Torvalds 	case SIOCBONDCHANGEACTIVE:
4610d1fbd3edSNikolay Aleksandrov 		bond_opt_initstr(&newval, slave_dev->name);
46117a7e96e0SVlad Yasevich 		res = __bond_opt_set_notify(bond, BOND_OPT_ACTIVE_SLAVE,
46127a7e96e0SVlad Yasevich 					    &newval);
46131da177e4SLinus Torvalds 		break;
46141da177e4SLinus Torvalds 	default:
46151da177e4SLinus Torvalds 		res = -EOPNOTSUPP;
46161da177e4SLinus Torvalds 	}
46171da177e4SLinus Torvalds 
46181da177e4SLinus Torvalds 	return res;
46191da177e4SLinus Torvalds }
46201da177e4SLinus Torvalds 
bond_siocdevprivate(struct net_device * bond_dev,struct ifreq * ifr,void __user * data,int cmd)4621232ec98eSArnd Bergmann static int bond_siocdevprivate(struct net_device *bond_dev, struct ifreq *ifr,
4622232ec98eSArnd Bergmann 			       void __user *data, int cmd)
4623232ec98eSArnd Bergmann {
4624232ec98eSArnd Bergmann 	struct ifreq ifrdata = { .ifr_data = data };
4625232ec98eSArnd Bergmann 
4626232ec98eSArnd Bergmann 	switch (cmd) {
4627232ec98eSArnd Bergmann 	case BOND_INFO_QUERY_OLD:
4628232ec98eSArnd Bergmann 		return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDINFOQUERY);
4629232ec98eSArnd Bergmann 	case BOND_SLAVE_INFO_QUERY_OLD:
4630232ec98eSArnd Bergmann 		return bond_do_ioctl(bond_dev, &ifrdata, SIOCBONDSLAVEINFOQUERY);
4631232ec98eSArnd Bergmann 	case BOND_ENSLAVE_OLD:
4632232ec98eSArnd Bergmann 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDENSLAVE);
4633232ec98eSArnd Bergmann 	case BOND_RELEASE_OLD:
4634232ec98eSArnd Bergmann 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDRELEASE);
4635232ec98eSArnd Bergmann 	case BOND_SETHWADDR_OLD:
4636232ec98eSArnd Bergmann 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDSETHWADDR);
4637232ec98eSArnd Bergmann 	case BOND_CHANGE_ACTIVE_OLD:
4638232ec98eSArnd Bergmann 		return bond_do_ioctl(bond_dev, ifr, SIOCBONDCHANGEACTIVE);
4639232ec98eSArnd Bergmann 	}
4640232ec98eSArnd Bergmann 
4641232ec98eSArnd Bergmann 	return -EOPNOTSUPP;
4642232ec98eSArnd Bergmann }
4643232ec98eSArnd Bergmann 
bond_change_rx_flags(struct net_device * bond_dev,int change)4644d03462b9SJiri Pirko static void bond_change_rx_flags(struct net_device *bond_dev, int change)
4645d03462b9SJiri Pirko {
4646d03462b9SJiri Pirko 	struct bonding *bond = netdev_priv(bond_dev);
4647d03462b9SJiri Pirko 
4648d03462b9SJiri Pirko 	if (change & IFF_PROMISC)
4649d03462b9SJiri Pirko 		bond_set_promiscuity(bond,
4650d03462b9SJiri Pirko 				     bond_dev->flags & IFF_PROMISC ? 1 : -1);
4651d03462b9SJiri Pirko 
4652d03462b9SJiri Pirko 	if (change & IFF_ALLMULTI)
4653d03462b9SJiri Pirko 		bond_set_allmulti(bond,
4654d03462b9SJiri Pirko 				  bond_dev->flags & IFF_ALLMULTI ? 1 : -1);
4655d03462b9SJiri Pirko }
4656d03462b9SJiri Pirko 
bond_set_rx_mode(struct net_device * bond_dev)4657303d1cbfSJay Vosburgh static void bond_set_rx_mode(struct net_device *bond_dev)
46581da177e4SLinus Torvalds {
4659454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
46609caff1e7SVeaceslav Falico 	struct list_head *iter;
4661303d1cbfSJay Vosburgh 	struct slave *slave;
46621da177e4SLinus Torvalds 
4663b3241870SVeaceslav Falico 	rcu_read_lock();
4664ec0865a9SVeaceslav Falico 	if (bond_uses_primary(bond)) {
4665b3241870SVeaceslav Falico 		slave = rcu_dereference(bond->curr_active_slave);
4666303d1cbfSJay Vosburgh 		if (slave) {
4667303d1cbfSJay Vosburgh 			dev_uc_sync(slave->dev, bond_dev);
4668303d1cbfSJay Vosburgh 			dev_mc_sync(slave->dev, bond_dev);
46691da177e4SLinus Torvalds 		}
4670303d1cbfSJay Vosburgh 	} else {
4671b3241870SVeaceslav Falico 		bond_for_each_slave_rcu(bond, slave, iter) {
4672303d1cbfSJay Vosburgh 			dev_uc_sync_multiple(slave->dev, bond_dev);
4673303d1cbfSJay Vosburgh 			dev_mc_sync_multiple(slave->dev, bond_dev);
46741da177e4SLinus Torvalds 		}
4675303d1cbfSJay Vosburgh 	}
4676b3241870SVeaceslav Falico 	rcu_read_unlock();
46771da177e4SLinus Torvalds }
46781da177e4SLinus Torvalds 
bond_neigh_init(struct neighbour * n)4679234bcf8aSShlomo Pongratz static int bond_neigh_init(struct neighbour *n)
468000829823SStephen Hemminger {
4681234bcf8aSShlomo Pongratz 	struct bonding *bond = netdev_priv(n->dev);
4682234bcf8aSShlomo Pongratz 	const struct net_device_ops *slave_ops;
4683234bcf8aSShlomo Pongratz 	struct neigh_parms parms;
4684dec1e90eSnikolay@redhat.com 	struct slave *slave;
46859e99bfefSEric Dumazet 	int ret = 0;
468600829823SStephen Hemminger 
46879e99bfefSEric Dumazet 	rcu_read_lock();
46889e99bfefSEric Dumazet 	slave = bond_first_slave_rcu(bond);
4689080bfa1eSDavid S. Miller 	if (!slave)
46909e99bfefSEric Dumazet 		goto out;
4691234bcf8aSShlomo Pongratz 	slave_ops = slave->dev->netdev_ops;
4692080bfa1eSDavid S. Miller 	if (!slave_ops->ndo_neigh_setup)
46939e99bfefSEric Dumazet 		goto out;
4694234bcf8aSShlomo Pongratz 
46959e99bfefSEric Dumazet 	/* TODO: find another way [1] to implement this.
46969e99bfefSEric Dumazet 	 * Passing a zeroed structure is fragile,
46979e99bfefSEric Dumazet 	 * but at least we do not pass garbage.
46989e99bfefSEric Dumazet 	 *
46999e99bfefSEric Dumazet 	 * [1] One way would be that ndo_neigh_setup() never touch
47009e99bfefSEric Dumazet 	 *     struct neigh_parms, but propagate the new neigh_setup()
47019e99bfefSEric Dumazet 	 *     back to ___neigh_create() / neigh_parms_alloc()
47029e99bfefSEric Dumazet 	 */
47039e99bfefSEric Dumazet 	memset(&parms, 0, sizeof(parms));
4704234bcf8aSShlomo Pongratz 	ret = slave_ops->ndo_neigh_setup(slave->dev, &parms);
47059e99bfefSEric Dumazet 
4706234bcf8aSShlomo Pongratz 	if (ret)
47079e99bfefSEric Dumazet 		goto out;
47089e99bfefSEric Dumazet 
47099e99bfefSEric Dumazet 	if (parms.neigh_setup)
47109e99bfefSEric Dumazet 		ret = parms.neigh_setup(n);
47119e99bfefSEric Dumazet out:
47129e99bfefSEric Dumazet 	rcu_read_unlock();
4713234bcf8aSShlomo Pongratz 	return ret;
471400829823SStephen Hemminger }
4715234bcf8aSShlomo Pongratz 
4716547942caSNikolay Aleksandrov /* The bonding ndo_neigh_setup is called at init time beofre any
4717234bcf8aSShlomo Pongratz  * slave exists. So we must declare proxy setup function which will
4718234bcf8aSShlomo Pongratz  * be used at run time to resolve the actual slave neigh param setup.
47199918d5bfSVeaceslav Falico  *
47209918d5bfSVeaceslav Falico  * It's also called by master devices (such as vlans) to setup their
47219918d5bfSVeaceslav Falico  * underlying devices. In that case - do nothing, we're already set up from
47229918d5bfSVeaceslav Falico  * our init.
4723234bcf8aSShlomo Pongratz  */
bond_neigh_setup(struct net_device * dev,struct neigh_parms * parms)4724234bcf8aSShlomo Pongratz static int bond_neigh_setup(struct net_device *dev,
4725234bcf8aSShlomo Pongratz 			    struct neigh_parms *parms)
4726234bcf8aSShlomo Pongratz {
47279918d5bfSVeaceslav Falico 	/* modify only our neigh_parms */
47289918d5bfSVeaceslav Falico 	if (parms->dev == dev)
4729234bcf8aSShlomo Pongratz 		parms->neigh_setup = bond_neigh_init;
4730234bcf8aSShlomo Pongratz 
473100829823SStephen Hemminger 	return 0;
473200829823SStephen Hemminger }
473300829823SStephen Hemminger 
4734547942caSNikolay Aleksandrov /* Change the MTU of all of a master's slaves to match the master */
bond_change_mtu(struct net_device * bond_dev,int new_mtu)47351da177e4SLinus Torvalds static int bond_change_mtu(struct net_device *bond_dev, int new_mtu)
47361da177e4SLinus Torvalds {
4737454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
473881f23b13SVeaceslav Falico 	struct slave *slave, *rollback_slave;
47399caff1e7SVeaceslav Falico 	struct list_head *iter;
47401da177e4SLinus Torvalds 	int res = 0;
47411da177e4SLinus Torvalds 
474276444f50SVeaceslav Falico 	netdev_dbg(bond_dev, "bond=%p, new_mtu=%d\n", bond, new_mtu);
47431da177e4SLinus Torvalds 
47449caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
4745e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave->dev, "s %p c_m %p\n",
474690194264SJoe Perches 			   slave, slave->dev->netdev_ops->ndo_change_mtu);
4747e944ef79SMitch Williams 
47481da177e4SLinus Torvalds 		res = dev_set_mtu(slave->dev, new_mtu);
47491da177e4SLinus Torvalds 
47501da177e4SLinus Torvalds 		if (res) {
47511da177e4SLinus Torvalds 			/* If we failed to set the slave's mtu to the new value
47521da177e4SLinus Torvalds 			 * we must abort the operation even in ACTIVE_BACKUP
47531da177e4SLinus Torvalds 			 * mode, because if we allow the backup slaves to have
47541da177e4SLinus Torvalds 			 * different mtu values than the active slave we'll
47551da177e4SLinus Torvalds 			 * need to change their mtu when doing a failover. That
47561da177e4SLinus Torvalds 			 * means changing their mtu from timer context, which
47571da177e4SLinus Torvalds 			 * is probably not a good idea.
47581da177e4SLinus Torvalds 			 */
4759e2a7420dSJarod Wilson 			slave_dbg(bond_dev, slave->dev, "err %d setting mtu to %d\n",
4760e2a7420dSJarod Wilson 				  res, new_mtu);
47611da177e4SLinus Torvalds 			goto unwind;
47621da177e4SLinus Torvalds 		}
47631da177e4SLinus Torvalds 	}
47641da177e4SLinus Torvalds 
47651da177e4SLinus Torvalds 	bond_dev->mtu = new_mtu;
47661da177e4SLinus Torvalds 
47671da177e4SLinus Torvalds 	return 0;
47681da177e4SLinus Torvalds 
47691da177e4SLinus Torvalds unwind:
47701da177e4SLinus Torvalds 	/* unwind from head to the slave that failed */
47719caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, rollback_slave, iter) {
47721da177e4SLinus Torvalds 		int tmp_res;
47731da177e4SLinus Torvalds 
477481f23b13SVeaceslav Falico 		if (rollback_slave == slave)
477581f23b13SVeaceslav Falico 			break;
477681f23b13SVeaceslav Falico 
477781f23b13SVeaceslav Falico 		tmp_res = dev_set_mtu(rollback_slave->dev, bond_dev->mtu);
4778e2a7420dSJarod Wilson 		if (tmp_res)
4779e2a7420dSJarod Wilson 			slave_dbg(bond_dev, rollback_slave->dev, "unwind err %d\n",
4780e2a7420dSJarod Wilson 				  tmp_res);
47811da177e4SLinus Torvalds 	}
47821da177e4SLinus Torvalds 
47831da177e4SLinus Torvalds 	return res;
47841da177e4SLinus Torvalds }
47851da177e4SLinus Torvalds 
4786547942caSNikolay Aleksandrov /* Change HW address
47871da177e4SLinus Torvalds  *
47881da177e4SLinus Torvalds  * Note that many devices must be down to change the HW address, and
47891da177e4SLinus Torvalds  * downing the master releases all slaves.  We can make bonds full of
47901da177e4SLinus Torvalds  * bonding devices to test this, however.
47911da177e4SLinus Torvalds  */
bond_set_mac_address(struct net_device * bond_dev,void * addr)47921da177e4SLinus Torvalds static int bond_set_mac_address(struct net_device *bond_dev, void *addr)
47931da177e4SLinus Torvalds {
4794454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
479581f23b13SVeaceslav Falico 	struct slave *slave, *rollback_slave;
4796faeeb317SJarod Wilson 	struct sockaddr_storage *ss = addr, tmp_ss;
47979caff1e7SVeaceslav Falico 	struct list_head *iter;
47981da177e4SLinus Torvalds 	int res = 0;
47991da177e4SLinus Torvalds 
480001844098SVeaceslav Falico 	if (BOND_MODE(bond) == BOND_MODE_ALB)
4801eb7cc59aSStephen Hemminger 		return bond_alb_set_mac_address(bond_dev, addr);
4802eb7cc59aSStephen Hemminger 
4803eb7cc59aSStephen Hemminger 
4804e2a7420dSJarod Wilson 	netdev_dbg(bond_dev, "%s: bond=%p\n", __func__, bond);
48051da177e4SLinus Torvalds 
48061b5acd29SJay Vosburgh 	/* If fail_over_mac is enabled, do nothing and return success.
48071b5acd29SJay Vosburgh 	 * Returning an error causes ifenslave to fail.
4808dd957c57SJay Vosburgh 	 */
4809cc689aaaSdingtianhong 	if (bond->params.fail_over_mac &&
481001844098SVeaceslav Falico 	    BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
4811dd957c57SJay Vosburgh 		return 0;
48122ab82852SMoni Shoua 
4813faeeb317SJarod Wilson 	if (!is_valid_ether_addr(ss->__data))
48141da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
48151da177e4SLinus Torvalds 
48169caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
4817e2a7420dSJarod Wilson 		slave_dbg(bond_dev, slave->dev, "%s: slave=%p\n",
4818e2a7420dSJarod Wilson 			  __func__, slave);
48193a37a963SPetr Machata 		res = dev_set_mac_address(slave->dev, addr, NULL);
48201da177e4SLinus Torvalds 		if (res) {
48211da177e4SLinus Torvalds 			/* TODO: consider downing the slave
48221da177e4SLinus Torvalds 			 * and retry ?
48231da177e4SLinus Torvalds 			 * User should expect communications
48241da177e4SLinus Torvalds 			 * breakage anyway until ARP finish
48251da177e4SLinus Torvalds 			 * updating, so...
48261da177e4SLinus Torvalds 			 */
4827e2a7420dSJarod Wilson 			slave_dbg(bond_dev, slave->dev, "%s: err %d\n",
4828e2a7420dSJarod Wilson 				  __func__, res);
48291da177e4SLinus Torvalds 			goto unwind;
48301da177e4SLinus Torvalds 		}
48311da177e4SLinus Torvalds 	}
48321da177e4SLinus Torvalds 
48331da177e4SLinus Torvalds 	/* success */
4834ea52a0b5SJakub Kicinski 	dev_addr_set(bond_dev, ss->__data);
48351da177e4SLinus Torvalds 	return 0;
48361da177e4SLinus Torvalds 
48371da177e4SLinus Torvalds unwind:
4838faeeb317SJarod Wilson 	memcpy(tmp_ss.__data, bond_dev->dev_addr, bond_dev->addr_len);
4839faeeb317SJarod Wilson 	tmp_ss.ss_family = bond_dev->type;
48401da177e4SLinus Torvalds 
48411da177e4SLinus Torvalds 	/* unwind from head to the slave that failed */
48429caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, rollback_slave, iter) {
48431da177e4SLinus Torvalds 		int tmp_res;
48441da177e4SLinus Torvalds 
484581f23b13SVeaceslav Falico 		if (rollback_slave == slave)
484681f23b13SVeaceslav Falico 			break;
484781f23b13SVeaceslav Falico 
4848faeeb317SJarod Wilson 		tmp_res = dev_set_mac_address(rollback_slave->dev,
48493a37a963SPetr Machata 					      (struct sockaddr *)&tmp_ss, NULL);
48501da177e4SLinus Torvalds 		if (tmp_res) {
4851e2a7420dSJarod Wilson 			slave_dbg(bond_dev, rollback_slave->dev, "%s: unwind err %d\n",
4852e2a7420dSJarod Wilson 				   __func__, tmp_res);
48531da177e4SLinus Torvalds 		}
48541da177e4SLinus Torvalds 	}
48551da177e4SLinus Torvalds 
48561da177e4SLinus Torvalds 	return res;
48571da177e4SLinus Torvalds }
48581da177e4SLinus Torvalds 
485915077228SNikolay Aleksandrov /**
486029d5bbccSMaor Gottlieb  * bond_get_slave_by_id - get xmit slave with slave_id
486115077228SNikolay Aleksandrov  * @bond: bonding device that is transmitting
486215077228SNikolay Aleksandrov  * @slave_id: slave id up to slave_cnt-1 through which to transmit
486315077228SNikolay Aleksandrov  *
486429d5bbccSMaor Gottlieb  * This function tries to get slave with slave_id but in case
486515077228SNikolay Aleksandrov  * it fails, it tries to find the first available slave for transmission.
486615077228SNikolay Aleksandrov  */
bond_get_slave_by_id(struct bonding * bond,int slave_id)486729d5bbccSMaor Gottlieb static struct slave *bond_get_slave_by_id(struct bonding *bond,
486829d5bbccSMaor Gottlieb 					  int slave_id)
486915077228SNikolay Aleksandrov {
48709caff1e7SVeaceslav Falico 	struct list_head *iter;
487115077228SNikolay Aleksandrov 	struct slave *slave;
487215077228SNikolay Aleksandrov 	int i = slave_id;
487315077228SNikolay Aleksandrov 
487415077228SNikolay Aleksandrov 	/* Here we start from the slave with slave_id */
48759caff1e7SVeaceslav Falico 	bond_for_each_slave_rcu(bond, slave, iter) {
487615077228SNikolay Aleksandrov 		if (--i < 0) {
4877ae46f184SEric Dumazet 			if (bond_slave_can_tx(slave))
487829d5bbccSMaor Gottlieb 				return slave;
487915077228SNikolay Aleksandrov 		}
488015077228SNikolay Aleksandrov 	}
488115077228SNikolay Aleksandrov 
488215077228SNikolay Aleksandrov 	/* Here we start from the first slave up to slave_id */
488315077228SNikolay Aleksandrov 	i = slave_id;
48849caff1e7SVeaceslav Falico 	bond_for_each_slave_rcu(bond, slave, iter) {
488515077228SNikolay Aleksandrov 		if (--i < 0)
488615077228SNikolay Aleksandrov 			break;
4887ae46f184SEric Dumazet 		if (bond_slave_can_tx(slave))
488829d5bbccSMaor Gottlieb 			return slave;
488915077228SNikolay Aleksandrov 	}
489015077228SNikolay Aleksandrov 	/* no slave that can tx has been found */
489129d5bbccSMaor Gottlieb 	return NULL;
489215077228SNikolay Aleksandrov }
489315077228SNikolay Aleksandrov 
489473958329SNikolay Aleksandrov /**
489573958329SNikolay Aleksandrov  * bond_rr_gen_slave_id - generate slave id based on packets_per_slave
489673958329SNikolay Aleksandrov  * @bond: bonding device to use
489773958329SNikolay Aleksandrov  *
489873958329SNikolay Aleksandrov  * Based on the value of the bonding device's packets_per_slave parameter
489973958329SNikolay Aleksandrov  * this function generates a slave id, which is usually used as the next
490073958329SNikolay Aleksandrov  * slave to transmit through.
490173958329SNikolay Aleksandrov  */
bond_rr_gen_slave_id(struct bonding * bond)490273958329SNikolay Aleksandrov static u32 bond_rr_gen_slave_id(struct bonding *bond)
490373958329SNikolay Aleksandrov {
490473958329SNikolay Aleksandrov 	u32 slave_id;
4905809fa972SHannes Frederic Sowa 	struct reciprocal_value reciprocal_packets_per_slave;
4906809fa972SHannes Frederic Sowa 	int packets_per_slave = bond->params.packets_per_slave;
490773958329SNikolay Aleksandrov 
490873958329SNikolay Aleksandrov 	switch (packets_per_slave) {
490973958329SNikolay Aleksandrov 	case 0:
4910a251c17aSJason A. Donenfeld 		slave_id = get_random_u32();
491173958329SNikolay Aleksandrov 		break;
491273958329SNikolay Aleksandrov 	case 1:
4913848ca918SJussi Maki 		slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
491473958329SNikolay Aleksandrov 		break;
491573958329SNikolay Aleksandrov 	default:
4916809fa972SHannes Frederic Sowa 		reciprocal_packets_per_slave =
4917809fa972SHannes Frederic Sowa 			bond->params.reciprocal_packets_per_slave;
4918848ca918SJussi Maki 		slave_id = this_cpu_inc_return(*bond->rr_tx_counter);
4919848ca918SJussi Maki 		slave_id = reciprocal_divide(slave_id,
4920809fa972SHannes Frederic Sowa 					     reciprocal_packets_per_slave);
492173958329SNikolay Aleksandrov 		break;
492273958329SNikolay Aleksandrov 	}
492373958329SNikolay Aleksandrov 
492473958329SNikolay Aleksandrov 	return slave_id;
492573958329SNikolay Aleksandrov }
492673958329SNikolay Aleksandrov 
bond_xmit_roundrobin_slave_get(struct bonding * bond,struct sk_buff * skb)492729d5bbccSMaor Gottlieb static struct slave *bond_xmit_roundrobin_slave_get(struct bonding *bond,
492829d5bbccSMaor Gottlieb 						    struct sk_buff *skb)
49291da177e4SLinus Torvalds {
493015077228SNikolay Aleksandrov 	struct slave *slave;
49319d1bc24bSCong Wang 	int slave_cnt;
493273958329SNikolay Aleksandrov 	u32 slave_id;
49331da177e4SLinus Torvalds 
493473958329SNikolay Aleksandrov 	/* Start with the curr_active_slave that joined the bond as the
4935a2fd940fSAndy Gospodarek 	 * default for sending IGMP traffic.  For failover purposes one
4936a2fd940fSAndy Gospodarek 	 * needs to maintain some consistency for the interface that will
4937a2fd940fSAndy Gospodarek 	 * send the join/membership reports.  The curr_active_slave found
4938a2fd940fSAndy Gospodarek 	 * will send all of this type of traffic.
4939a2fd940fSAndy Gospodarek 	 */
49409d1bc24bSCong Wang 	if (skb->protocol == htons(ETH_P_IP)) {
49419d1bc24bSCong Wang 		int noff = skb_network_offset(skb);
49429d1bc24bSCong Wang 		struct iphdr *iph;
49439d1bc24bSCong Wang 
49449d1bc24bSCong Wang 		if (unlikely(!pskb_may_pull(skb, noff + sizeof(*iph))))
49459d1bc24bSCong Wang 			goto non_igmp;
49469d1bc24bSCong Wang 
49479d1bc24bSCong Wang 		iph = ip_hdr(skb);
49489d1bc24bSCong Wang 		if (iph->protocol == IPPROTO_IGMP) {
4949278b2083Snikolay@redhat.com 			slave = rcu_dereference(bond->curr_active_slave);
4950a64d044eSdingtianhong 			if (slave)
495129d5bbccSMaor Gottlieb 				return slave;
495229d5bbccSMaor Gottlieb 			return bond_get_slave_by_id(bond, 0);
49539d1bc24bSCong Wang 		}
49549d1bc24bSCong Wang 	}
49559a72c2daSNikolay Aleksandrov 
49569d1bc24bSCong Wang non_igmp:
49579d1bc24bSCong Wang 	slave_cnt = READ_ONCE(bond->slave_cnt);
49589a72c2daSNikolay Aleksandrov 	if (likely(slave_cnt)) {
495929d5bbccSMaor Gottlieb 		slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
496029d5bbccSMaor Gottlieb 		return bond_get_slave_by_id(bond, slave_id);
49619a72c2daSNikolay Aleksandrov 	}
496229d5bbccSMaor Gottlieb 	return NULL;
496329d5bbccSMaor Gottlieb }
496429d5bbccSMaor Gottlieb 
bond_xdp_xmit_roundrobin_slave_get(struct bonding * bond,struct xdp_buff * xdp)49659e2ee5c7SJussi Maki static struct slave *bond_xdp_xmit_roundrobin_slave_get(struct bonding *bond,
49669e2ee5c7SJussi Maki 							struct xdp_buff *xdp)
49679e2ee5c7SJussi Maki {
49689e2ee5c7SJussi Maki 	struct slave *slave;
49699e2ee5c7SJussi Maki 	int slave_cnt;
49709e2ee5c7SJussi Maki 	u32 slave_id;
49719e2ee5c7SJussi Maki 	const struct ethhdr *eth;
49729e2ee5c7SJussi Maki 	void *data = xdp->data;
49739e2ee5c7SJussi Maki 
49749e2ee5c7SJussi Maki 	if (data + sizeof(struct ethhdr) > xdp->data_end)
49759e2ee5c7SJussi Maki 		goto non_igmp;
49769e2ee5c7SJussi Maki 
49779e2ee5c7SJussi Maki 	eth = (struct ethhdr *)data;
49789e2ee5c7SJussi Maki 	data += sizeof(struct ethhdr);
49799e2ee5c7SJussi Maki 
49809e2ee5c7SJussi Maki 	/* See comment on IGMP in bond_xmit_roundrobin_slave_get() */
49819e2ee5c7SJussi Maki 	if (eth->h_proto == htons(ETH_P_IP)) {
49829e2ee5c7SJussi Maki 		const struct iphdr *iph;
49839e2ee5c7SJussi Maki 
49849e2ee5c7SJussi Maki 		if (data + sizeof(struct iphdr) > xdp->data_end)
49859e2ee5c7SJussi Maki 			goto non_igmp;
49869e2ee5c7SJussi Maki 
49879e2ee5c7SJussi Maki 		iph = (struct iphdr *)data;
49889e2ee5c7SJussi Maki 
49899e2ee5c7SJussi Maki 		if (iph->protocol == IPPROTO_IGMP) {
49909e2ee5c7SJussi Maki 			slave = rcu_dereference(bond->curr_active_slave);
49919e2ee5c7SJussi Maki 			if (slave)
49929e2ee5c7SJussi Maki 				return slave;
49939e2ee5c7SJussi Maki 			return bond_get_slave_by_id(bond, 0);
49949e2ee5c7SJussi Maki 		}
49959e2ee5c7SJussi Maki 	}
49969e2ee5c7SJussi Maki 
49979e2ee5c7SJussi Maki non_igmp:
49989e2ee5c7SJussi Maki 	slave_cnt = READ_ONCE(bond->slave_cnt);
49999e2ee5c7SJussi Maki 	if (likely(slave_cnt)) {
50009e2ee5c7SJussi Maki 		slave_id = bond_rr_gen_slave_id(bond) % slave_cnt;
50019e2ee5c7SJussi Maki 		return bond_get_slave_by_id(bond, slave_id);
50029e2ee5c7SJussi Maki 	}
50039e2ee5c7SJussi Maki 	return NULL;
50049e2ee5c7SJussi Maki }
50059e2ee5c7SJussi Maki 
bond_xmit_roundrobin(struct sk_buff * skb,struct net_device * bond_dev)500629d5bbccSMaor Gottlieb static netdev_tx_t bond_xmit_roundrobin(struct sk_buff *skb,
500729d5bbccSMaor Gottlieb 					struct net_device *bond_dev)
500829d5bbccSMaor Gottlieb {
500929d5bbccSMaor Gottlieb 	struct bonding *bond = netdev_priv(bond_dev);
501029d5bbccSMaor Gottlieb 	struct slave *slave;
501129d5bbccSMaor Gottlieb 
501229d5bbccSMaor Gottlieb 	slave = bond_xmit_roundrobin_slave_get(bond, skb);
501376cd622fSSaeed Mahameed 	if (likely(slave))
501476cd622fSSaeed Mahameed 		return bond_dev_queue_xmit(bond, skb, slave->dev);
501576cd622fSSaeed Mahameed 
5016ae46f184SEric Dumazet 	return bond_tx_drop(bond_dev, skb);
50171da177e4SLinus Torvalds }
50181da177e4SLinus Torvalds 
bond_xmit_activebackup_slave_get(struct bonding * bond)5019a815bde5SJussi Maki static struct slave *bond_xmit_activebackup_slave_get(struct bonding *bond)
50205a19f1c1SMaor Gottlieb {
50215a19f1c1SMaor Gottlieb 	return rcu_dereference(bond->curr_active_slave);
50225a19f1c1SMaor Gottlieb }
50235a19f1c1SMaor Gottlieb 
5024547942caSNikolay Aleksandrov /* In active-backup mode, we know that bond->curr_active_slave is always valid if
50251da177e4SLinus Torvalds  * the bond has a usable interface.
50261da177e4SLinus Torvalds  */
bond_xmit_activebackup(struct sk_buff * skb,struct net_device * bond_dev)5027dbdc8a21STonghao Zhang static netdev_tx_t bond_xmit_activebackup(struct sk_buff *skb,
5028dbdc8a21STonghao Zhang 					  struct net_device *bond_dev)
50291da177e4SLinus Torvalds {
5030454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
503171bc3b2dSnikolay@redhat.com 	struct slave *slave;
50321da177e4SLinus Torvalds 
5033a815bde5SJussi Maki 	slave = bond_xmit_activebackup_slave_get(bond);
503471bc3b2dSnikolay@redhat.com 	if (slave)
5035ae46f184SEric Dumazet 		return bond_dev_queue_xmit(bond, skb, slave->dev);
50360693e88eSMichał Mirosław 
5037ae46f184SEric Dumazet 	return bond_tx_drop(bond_dev, skb);
50381da177e4SLinus Torvalds }
50391da177e4SLinus Torvalds 
5040ee637714SMahesh Bandewar /* Use this to update slave_array when (a) it's not appropriate to update
5041ee637714SMahesh Bandewar  * slave_array right away (note that update_slave_array() may sleep)
5042ee637714SMahesh Bandewar  * and / or (b) RTNL is not held.
50431da177e4SLinus Torvalds  */
bond_slave_arr_work_rearm(struct bonding * bond,unsigned long delay)5044ee637714SMahesh Bandewar void bond_slave_arr_work_rearm(struct bonding *bond, unsigned long delay)
50451da177e4SLinus Torvalds {
5046ee637714SMahesh Bandewar 	queue_delayed_work(bond->wq, &bond->slave_arr_work, delay);
5047ee637714SMahesh Bandewar }
50481da177e4SLinus Torvalds 
5049ee637714SMahesh Bandewar /* Slave array work handler. Holds only RTNL */
bond_slave_arr_handler(struct work_struct * work)5050ee637714SMahesh Bandewar static void bond_slave_arr_handler(struct work_struct *work)
5051ee637714SMahesh Bandewar {
5052ee637714SMahesh Bandewar 	struct bonding *bond = container_of(work, struct bonding,
5053ee637714SMahesh Bandewar 					    slave_arr_work.work);
5054ee637714SMahesh Bandewar 	int ret;
5055ee637714SMahesh Bandewar 
5056ee637714SMahesh Bandewar 	if (!rtnl_trylock())
5057ee637714SMahesh Bandewar 		goto err;
5058ee637714SMahesh Bandewar 
5059ee637714SMahesh Bandewar 	ret = bond_update_slave_arr(bond, NULL);
5060ee637714SMahesh Bandewar 	rtnl_unlock();
5061ee637714SMahesh Bandewar 	if (ret) {
5062ee637714SMahesh Bandewar 		pr_warn_ratelimited("Failed to update slave array from WT\n");
5063ee637714SMahesh Bandewar 		goto err;
5064ee637714SMahesh Bandewar 	}
5065ee637714SMahesh Bandewar 	return;
5066ee637714SMahesh Bandewar 
5067ee637714SMahesh Bandewar err:
5068ee637714SMahesh Bandewar 	bond_slave_arr_work_rearm(bond, 1);
5069ee637714SMahesh Bandewar }
5070ee637714SMahesh Bandewar 
bond_skip_slave(struct bond_up_slave * slaves,struct slave * skipslave)5071119d48fdSMaor Gottlieb static void bond_skip_slave(struct bond_up_slave *slaves,
5072119d48fdSMaor Gottlieb 			    struct slave *skipslave)
5073ee637714SMahesh Bandewar {
5074ee637714SMahesh Bandewar 	int idx;
5075ee637714SMahesh Bandewar 
5076ee637714SMahesh Bandewar 	/* Rare situation where caller has asked to skip a specific
5077ee637714SMahesh Bandewar 	 * slave but allocation failed (most likely!). BTW this is
5078ee637714SMahesh Bandewar 	 * only possible when the call is initiated from
5079ee637714SMahesh Bandewar 	 * __bond_release_one(). In this situation; overwrite the
5080ee637714SMahesh Bandewar 	 * skipslave entry in the array with the last entry from the
5081ee637714SMahesh Bandewar 	 * array to avoid a situation where the xmit path may choose
5082ee637714SMahesh Bandewar 	 * this to-be-skipped slave to send a packet out.
5083ee637714SMahesh Bandewar 	 */
5084119d48fdSMaor Gottlieb 	for (idx = 0; slaves && idx < slaves->count; idx++) {
5085119d48fdSMaor Gottlieb 		if (skipslave == slaves->arr[idx]) {
5086119d48fdSMaor Gottlieb 			slaves->arr[idx] =
5087119d48fdSMaor Gottlieb 				slaves->arr[slaves->count - 1];
5088119d48fdSMaor Gottlieb 			slaves->count--;
5089ee637714SMahesh Bandewar 			break;
5090ee637714SMahesh Bandewar 		}
5091ee637714SMahesh Bandewar 	}
5092ee637714SMahesh Bandewar }
5093119d48fdSMaor Gottlieb 
bond_set_slave_arr(struct bonding * bond,struct bond_up_slave * usable_slaves,struct bond_up_slave * all_slaves)50946b447e76SMaor Gottlieb static void bond_set_slave_arr(struct bonding *bond,
50956b447e76SMaor Gottlieb 			       struct bond_up_slave *usable_slaves,
50966b447e76SMaor Gottlieb 			       struct bond_up_slave *all_slaves)
50976b447e76SMaor Gottlieb {
50986b447e76SMaor Gottlieb 	struct bond_up_slave *usable, *all;
50996b447e76SMaor Gottlieb 
51006b447e76SMaor Gottlieb 	usable = rtnl_dereference(bond->usable_slaves);
51016b447e76SMaor Gottlieb 	rcu_assign_pointer(bond->usable_slaves, usable_slaves);
51026b447e76SMaor Gottlieb 	kfree_rcu(usable, rcu);
51036b447e76SMaor Gottlieb 
51046b447e76SMaor Gottlieb 	all = rtnl_dereference(bond->all_slaves);
51056b447e76SMaor Gottlieb 	rcu_assign_pointer(bond->all_slaves, all_slaves);
51066b447e76SMaor Gottlieb 	kfree_rcu(all, rcu);
51076b447e76SMaor Gottlieb }
51086b447e76SMaor Gottlieb 
bond_reset_slave_arr(struct bonding * bond)51096b447e76SMaor Gottlieb static void bond_reset_slave_arr(struct bonding *bond)
51106b447e76SMaor Gottlieb {
5111a8f3f4b4SZhengchao Shao 	bond_set_slave_arr(bond, NULL, NULL);
51126b447e76SMaor Gottlieb }
51136b447e76SMaor Gottlieb 
51141da177e4SLinus Torvalds /* Build the usable slaves array in control path for modes that use xmit-hash
51151da177e4SLinus Torvalds  * to determine the slave interface -
51161da177e4SLinus Torvalds  * (a) BOND_MODE_8023AD
51171da177e4SLinus Torvalds  * (b) BOND_MODE_XOR
51181da177e4SLinus Torvalds  * (c) (BOND_MODE_TLB || BOND_MODE_ALB) && tlb_dynamic_lb == 0
51191da177e4SLinus Torvalds  *
51201da177e4SLinus Torvalds  * The caller is expected to hold RTNL only and NO other lock!
51211da177e4SLinus Torvalds  */
bond_update_slave_arr(struct bonding * bond,struct slave * skipslave)51221da177e4SLinus Torvalds int bond_update_slave_arr(struct bonding *bond, struct slave *skipslave)
51231da177e4SLinus Torvalds {
51246b447e76SMaor Gottlieb 	struct bond_up_slave *usable_slaves = NULL, *all_slaves = NULL;
5125ee637714SMahesh Bandewar 	struct slave *slave;
5126ee637714SMahesh Bandewar 	struct list_head *iter;
5127ee637714SMahesh Bandewar 	int agg_id = 0;
5128ee637714SMahesh Bandewar 	int ret = 0;
5129ee637714SMahesh Bandewar 
513083d686a6Sjinyiting 	might_sleep();
5131ee637714SMahesh Bandewar 
5132ed7d4f02SMaor Gottlieb 	usable_slaves = kzalloc(struct_size(usable_slaves, arr,
5133ed7d4f02SMaor Gottlieb 					    bond->slave_cnt), GFP_KERNEL);
51346b447e76SMaor Gottlieb 	all_slaves = kzalloc(struct_size(all_slaves, arr,
51356b447e76SMaor Gottlieb 					 bond->slave_cnt), GFP_KERNEL);
51366b447e76SMaor Gottlieb 	if (!usable_slaves || !all_slaves) {
5137ee637714SMahesh Bandewar 		ret = -ENOMEM;
5138ee637714SMahesh Bandewar 		goto out;
5139ee637714SMahesh Bandewar 	}
5140ee637714SMahesh Bandewar 	if (BOND_MODE(bond) == BOND_MODE_8023AD) {
5141ee637714SMahesh Bandewar 		struct ad_info ad_info;
5142ee637714SMahesh Bandewar 
514383d686a6Sjinyiting 		spin_lock_bh(&bond->mode_lock);
5144ee637714SMahesh Bandewar 		if (bond_3ad_get_active_agg_info(bond, &ad_info)) {
514583d686a6Sjinyiting 			spin_unlock_bh(&bond->mode_lock);
5146ee637714SMahesh Bandewar 			pr_debug("bond_3ad_get_active_agg_info failed\n");
5147ee637714SMahesh Bandewar 			/* No active aggragator means it's not safe to use
5148ee637714SMahesh Bandewar 			 * the previous array.
5149ee637714SMahesh Bandewar 			 */
51506b447e76SMaor Gottlieb 			bond_reset_slave_arr(bond);
5151ee637714SMahesh Bandewar 			goto out;
5152ee637714SMahesh Bandewar 		}
515383d686a6Sjinyiting 		spin_unlock_bh(&bond->mode_lock);
5154ee637714SMahesh Bandewar 		agg_id = ad_info.aggregator_id;
5155ee637714SMahesh Bandewar 	}
5156ee637714SMahesh Bandewar 	bond_for_each_slave(bond, slave, iter) {
51576b447e76SMaor Gottlieb 		if (skipslave == slave)
51586b447e76SMaor Gottlieb 			continue;
51596b447e76SMaor Gottlieb 
51606b447e76SMaor Gottlieb 		all_slaves->arr[all_slaves->count++] = slave;
5161ee637714SMahesh Bandewar 		if (BOND_MODE(bond) == BOND_MODE_8023AD) {
5162ee637714SMahesh Bandewar 			struct aggregator *agg;
5163ee637714SMahesh Bandewar 
5164ee637714SMahesh Bandewar 			agg = SLAVE_AD_INFO(slave)->port.aggregator;
5165ee637714SMahesh Bandewar 			if (!agg || agg->aggregator_identifier != agg_id)
5166ee637714SMahesh Bandewar 				continue;
5167ee637714SMahesh Bandewar 		}
5168ee637714SMahesh Bandewar 		if (!bond_slave_can_tx(slave))
5169ee637714SMahesh Bandewar 			continue;
5170ee637714SMahesh Bandewar 
5171ee637714SMahesh Bandewar 		slave_dbg(bond->dev, slave->dev, "Adding slave to tx hash array[%d]\n",
5172ed7d4f02SMaor Gottlieb 			  usable_slaves->count);
5173ee637714SMahesh Bandewar 
5174ed7d4f02SMaor Gottlieb 		usable_slaves->arr[usable_slaves->count++] = slave;
5175ee637714SMahesh Bandewar 	}
5176ee637714SMahesh Bandewar 
51776b447e76SMaor Gottlieb 	bond_set_slave_arr(bond, usable_slaves, all_slaves);
5178ee637714SMahesh Bandewar 	return ret;
5179ee637714SMahesh Bandewar out:
51806b447e76SMaor Gottlieb 	if (ret != 0 && skipslave) {
51816b447e76SMaor Gottlieb 		bond_skip_slave(rtnl_dereference(bond->all_slaves),
51826b447e76SMaor Gottlieb 				skipslave);
5183ed7d4f02SMaor Gottlieb 		bond_skip_slave(rtnl_dereference(bond->usable_slaves),
5184ed7d4f02SMaor Gottlieb 				skipslave);
51856b447e76SMaor Gottlieb 	}
51866b447e76SMaor Gottlieb 	kfree_rcu(all_slaves, rcu);
51876b447e76SMaor Gottlieb 	kfree_rcu(usable_slaves, rcu);
5188ee637714SMahesh Bandewar 
5189ee637714SMahesh Bandewar 	return ret;
5190ee637714SMahesh Bandewar }
5191ee637714SMahesh Bandewar 
bond_xmit_3ad_xor_slave_get(struct bonding * bond,struct sk_buff * skb,struct bond_up_slave * slaves)5192c071d91dSMaor Gottlieb static struct slave *bond_xmit_3ad_xor_slave_get(struct bonding *bond,
5193c071d91dSMaor Gottlieb 						 struct sk_buff *skb,
5194c071d91dSMaor Gottlieb 						 struct bond_up_slave *slaves)
5195c071d91dSMaor Gottlieb {
5196c071d91dSMaor Gottlieb 	struct slave *slave;
5197c071d91dSMaor Gottlieb 	unsigned int count;
5198c071d91dSMaor Gottlieb 	u32 hash;
5199c071d91dSMaor Gottlieb 
5200c071d91dSMaor Gottlieb 	hash = bond_xmit_hash(bond, skb);
5201c071d91dSMaor Gottlieb 	count = slaves ? READ_ONCE(slaves->count) : 0;
5202c071d91dSMaor Gottlieb 	if (unlikely(!count))
5203c071d91dSMaor Gottlieb 		return NULL;
5204c071d91dSMaor Gottlieb 
5205c071d91dSMaor Gottlieb 	slave = slaves->arr[hash % count];
5206c071d91dSMaor Gottlieb 	return slave;
5207ee637714SMahesh Bandewar }
5208ee637714SMahesh Bandewar 
bond_xdp_xmit_3ad_xor_slave_get(struct bonding * bond,struct xdp_buff * xdp)52099e2ee5c7SJussi Maki static struct slave *bond_xdp_xmit_3ad_xor_slave_get(struct bonding *bond,
52109e2ee5c7SJussi Maki 						     struct xdp_buff *xdp)
52119e2ee5c7SJussi Maki {
52129e2ee5c7SJussi Maki 	struct bond_up_slave *slaves;
52139e2ee5c7SJussi Maki 	unsigned int count;
52149e2ee5c7SJussi Maki 	u32 hash;
52159e2ee5c7SJussi Maki 
52169e2ee5c7SJussi Maki 	hash = bond_xmit_hash_xdp(bond, xdp);
52179e2ee5c7SJussi Maki 	slaves = rcu_dereference(bond->usable_slaves);
52189e2ee5c7SJussi Maki 	count = slaves ? READ_ONCE(slaves->count) : 0;
52199e2ee5c7SJussi Maki 	if (unlikely(!count))
52209e2ee5c7SJussi Maki 		return NULL;
52219e2ee5c7SJussi Maki 
52229e2ee5c7SJussi Maki 	return slaves->arr[hash % count];
52239e2ee5c7SJussi Maki }
52249e2ee5c7SJussi Maki 
5225ee637714SMahesh Bandewar /* Use this Xmit function for 3AD as well as XOR modes. The current
5226ee637714SMahesh Bandewar  * usable slave array is formed in the control path. The xmit function
5227ee637714SMahesh Bandewar  * just calculates hash and sends the packet out.
5228ee637714SMahesh Bandewar  */
bond_3ad_xor_xmit(struct sk_buff * skb,struct net_device * dev)5229dbdc8a21STonghao Zhang static netdev_tx_t bond_3ad_xor_xmit(struct sk_buff *skb,
5230dbdc8a21STonghao Zhang 				     struct net_device *dev)
5231ee637714SMahesh Bandewar {
5232ee637714SMahesh Bandewar 	struct bonding *bond = netdev_priv(dev);
5233ee637714SMahesh Bandewar 	struct bond_up_slave *slaves;
5234c071d91dSMaor Gottlieb 	struct slave *slave;
5235ee637714SMahesh Bandewar 
5236ed7d4f02SMaor Gottlieb 	slaves = rcu_dereference(bond->usable_slaves);
5237c071d91dSMaor Gottlieb 	slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
5238c071d91dSMaor Gottlieb 	if (likely(slave))
5239ae46f184SEric Dumazet 		return bond_dev_queue_xmit(bond, skb, slave->dev);
52400693e88eSMichał Mirosław 
5241ae46f184SEric Dumazet 	return bond_tx_drop(dev, skb);
52421da177e4SLinus Torvalds }
52431da177e4SLinus Torvalds 
524478a646ceSNikolay Aleksandrov /* in broadcast mode, we send everything to all usable interfaces. */
bond_xmit_broadcast(struct sk_buff * skb,struct net_device * bond_dev)5245dbdc8a21STonghao Zhang static netdev_tx_t bond_xmit_broadcast(struct sk_buff *skb,
5246dbdc8a21STonghao Zhang 				       struct net_device *bond_dev)
52471da177e4SLinus Torvalds {
5248454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
524978a646ceSNikolay Aleksandrov 	struct slave *slave = NULL;
52509caff1e7SVeaceslav Falico 	struct list_head *iter;
52514e5bd03aSJie Wang 	bool xmit_suc = false;
52524e5bd03aSJie Wang 	bool skb_used = false;
52531da177e4SLinus Torvalds 
52549caff1e7SVeaceslav Falico 	bond_for_each_slave_rcu(bond, slave, iter) {
52554e5bd03aSJie Wang 		struct sk_buff *skb2;
525678a646ceSNikolay Aleksandrov 
52574e5bd03aSJie Wang 		if (!(bond_slave_is_up(slave) && slave->link == BOND_LINK_UP))
52584e5bd03aSJie Wang 			continue;
52594e5bd03aSJie Wang 
52604e5bd03aSJie Wang 		if (bond_is_last_slave(bond, slave)) {
52614e5bd03aSJie Wang 			skb2 = skb;
52624e5bd03aSJie Wang 			skb_used = true;
52634e5bd03aSJie Wang 		} else {
52644e5bd03aSJie Wang 			skb2 = skb_clone(skb, GFP_ATOMIC);
52651da177e4SLinus Torvalds 			if (!skb2) {
52669152e26dSdingtianhong 				net_err_ratelimited("%s: Error: %s: skb_clone() failed\n",
52679152e26dSdingtianhong 						    bond_dev->name, __func__);
52681da177e4SLinus Torvalds 				continue;
52691da177e4SLinus Torvalds 			}
52701da177e4SLinus Torvalds 		}
52713d632c3fSStephen Hemminger 
52724e5bd03aSJie Wang 		if (bond_dev_queue_xmit(bond, skb2, slave->dev) == NETDEV_TX_OK)
52734e5bd03aSJie Wang 			xmit_suc = true;
52744e5bd03aSJie Wang 	}
52754e5bd03aSJie Wang 
52764e5bd03aSJie Wang 	if (!skb_used)
52774e5bd03aSJie Wang 		dev_kfree_skb_any(skb);
52784e5bd03aSJie Wang 
52794e5bd03aSJie Wang 	if (xmit_suc)
52804e5bd03aSJie Wang 		return NETDEV_TX_OK;
52814e5bd03aSJie Wang 
5282625788b5SEric Dumazet 	dev_core_stats_tx_dropped_inc(bond_dev);
52834e5bd03aSJie Wang 	return NET_XMIT_DROP;
52841da177e4SLinus Torvalds }
52851da177e4SLinus Torvalds 
52861da177e4SLinus Torvalds /*------------------------- Device initialization ---------------------------*/
52871da177e4SLinus Torvalds 
5288547942caSNikolay Aleksandrov /* Lookup the slave that corresponds to a qid */
bond_slave_override(struct bonding * bond,struct sk_buff * skb)5289bb1d9123SAndy Gospodarek static inline int bond_slave_override(struct bonding *bond,
5290bb1d9123SAndy Gospodarek 				      struct sk_buff *skb)
5291bb1d9123SAndy Gospodarek {
5292bb1d9123SAndy Gospodarek 	struct slave *slave = NULL;
52939caff1e7SVeaceslav Falico 	struct list_head *iter;
5294bb1d9123SAndy Gospodarek 
5295ae35c6f7STonghao Zhang 	if (!skb_rx_queue_recorded(skb))
52960693e88eSMichał Mirosław 		return 1;
5297bb1d9123SAndy Gospodarek 
5298bb1d9123SAndy Gospodarek 	/* Find out if any slaves have the same mapping as this skb. */
52993900f290Sdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
5300ae35c6f7STonghao Zhang 		if (slave->queue_id == skb_get_queue_mapping(skb)) {
5301f5e2dc5dSAnton Nayshtut 			if (bond_slave_is_up(slave) &&
5302f5e2dc5dSAnton Nayshtut 			    slave->link == BOND_LINK_UP) {
53033900f290Sdingtianhong 				bond_dev_queue_xmit(bond, skb, slave->dev);
53043900f290Sdingtianhong 				return 0;
53053900f290Sdingtianhong 			}
53063900f290Sdingtianhong 			/* If the slave isn't UP, use default transmit policy. */
5307bb1d9123SAndy Gospodarek 			break;
5308bb1d9123SAndy Gospodarek 		}
5309bb1d9123SAndy Gospodarek 	}
5310bb1d9123SAndy Gospodarek 
53113900f290Sdingtianhong 	return 1;
5312bb1d9123SAndy Gospodarek }
5313bb1d9123SAndy Gospodarek 
5314374eeb5aSNeil Horman 
bond_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)5315f663dd9aSJason Wang static u16 bond_select_queue(struct net_device *dev, struct sk_buff *skb,
5316a350ecceSPaolo Abeni 			     struct net_device *sb_dev)
5317bb1d9123SAndy Gospodarek {
5318547942caSNikolay Aleksandrov 	/* This helper function exists to help dev_pick_tx get the correct
5319fd0e435bSPhil Oester 	 * destination queue.  Using a helper function skips a call to
5320bb1d9123SAndy Gospodarek 	 * skb_tx_hash and will put the skbs in the queue we expect on their
5321bb1d9123SAndy Gospodarek 	 * way down to the bonding driver.
5322bb1d9123SAndy Gospodarek 	 */
5323fd0e435bSPhil Oester 	u16 txq = skb_rx_queue_recorded(skb) ? skb_get_rx_queue(skb) : 0;
5324fd0e435bSPhil Oester 
5325547942caSNikolay Aleksandrov 	/* Save the original txq to restore before passing to the driver */
5326ae35c6f7STonghao Zhang 	qdisc_skb_cb(skb)->slave_dev_queue_mapping = skb_get_queue_mapping(skb);
5327374eeb5aSNeil Horman 
5328fd0e435bSPhil Oester 	if (unlikely(txq >= dev->real_num_tx_queues)) {
5329d30ee670SDavid Decotigny 		do {
5330fd0e435bSPhil Oester 			txq -= dev->real_num_tx_queues;
5331d30ee670SDavid Decotigny 		} while (txq >= dev->real_num_tx_queues);
5332fd0e435bSPhil Oester 	}
5333fd0e435bSPhil Oester 	return txq;
5334bb1d9123SAndy Gospodarek }
5335bb1d9123SAndy Gospodarek 
bond_xmit_get_slave(struct net_device * master_dev,struct sk_buff * skb,bool all_slaves)533633720aafSMaor Gottlieb static struct net_device *bond_xmit_get_slave(struct net_device *master_dev,
533733720aafSMaor Gottlieb 					      struct sk_buff *skb,
533833720aafSMaor Gottlieb 					      bool all_slaves)
533933720aafSMaor Gottlieb {
534033720aafSMaor Gottlieb 	struct bonding *bond = netdev_priv(master_dev);
534133720aafSMaor Gottlieb 	struct bond_up_slave *slaves;
534233720aafSMaor Gottlieb 	struct slave *slave = NULL;
534333720aafSMaor Gottlieb 
534433720aafSMaor Gottlieb 	switch (BOND_MODE(bond)) {
534533720aafSMaor Gottlieb 	case BOND_MODE_ROUNDROBIN:
534633720aafSMaor Gottlieb 		slave = bond_xmit_roundrobin_slave_get(bond, skb);
534733720aafSMaor Gottlieb 		break;
534833720aafSMaor Gottlieb 	case BOND_MODE_ACTIVEBACKUP:
5349a815bde5SJussi Maki 		slave = bond_xmit_activebackup_slave_get(bond);
535033720aafSMaor Gottlieb 		break;
535133720aafSMaor Gottlieb 	case BOND_MODE_8023AD:
535233720aafSMaor Gottlieb 	case BOND_MODE_XOR:
535333720aafSMaor Gottlieb 		if (all_slaves)
535433720aafSMaor Gottlieb 			slaves = rcu_dereference(bond->all_slaves);
535533720aafSMaor Gottlieb 		else
535633720aafSMaor Gottlieb 			slaves = rcu_dereference(bond->usable_slaves);
535733720aafSMaor Gottlieb 		slave = bond_xmit_3ad_xor_slave_get(bond, skb, slaves);
535833720aafSMaor Gottlieb 		break;
535933720aafSMaor Gottlieb 	case BOND_MODE_BROADCAST:
536033720aafSMaor Gottlieb 		break;
536133720aafSMaor Gottlieb 	case BOND_MODE_ALB:
536233720aafSMaor Gottlieb 		slave = bond_xmit_alb_slave_get(bond, skb);
536333720aafSMaor Gottlieb 		break;
536433720aafSMaor Gottlieb 	case BOND_MODE_TLB:
536533720aafSMaor Gottlieb 		slave = bond_xmit_tlb_slave_get(bond, skb);
536633720aafSMaor Gottlieb 		break;
536733720aafSMaor Gottlieb 	default:
536833720aafSMaor Gottlieb 		/* Should never happen, mode already checked */
536933720aafSMaor Gottlieb 		WARN_ONCE(true, "Unknown bonding mode");
537033720aafSMaor Gottlieb 		break;
537133720aafSMaor Gottlieb 	}
537233720aafSMaor Gottlieb 
537333720aafSMaor Gottlieb 	if (slave)
537433720aafSMaor Gottlieb 		return slave->dev;
537533720aafSMaor Gottlieb 	return NULL;
537633720aafSMaor Gottlieb }
537733720aafSMaor Gottlieb 
bond_sk_to_flow(struct sock * sk,struct flow_keys * flow)5378007feb87STariq Toukan static void bond_sk_to_flow(struct sock *sk, struct flow_keys *flow)
5379007feb87STariq Toukan {
5380007feb87STariq Toukan 	switch (sk->sk_family) {
5381007feb87STariq Toukan #if IS_ENABLED(CONFIG_IPV6)
5382007feb87STariq Toukan 	case AF_INET6:
538381ee0eb6SKuniyuki Iwashima 		if (ipv6_only_sock(sk) ||
5384007feb87STariq Toukan 		    ipv6_addr_type(&sk->sk_v6_daddr) != IPV6_ADDR_MAPPED) {
5385007feb87STariq Toukan 			flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
5386007feb87STariq Toukan 			flow->addrs.v6addrs.src = inet6_sk(sk)->saddr;
5387007feb87STariq Toukan 			flow->addrs.v6addrs.dst = sk->sk_v6_daddr;
5388007feb87STariq Toukan 			break;
5389007feb87STariq Toukan 		}
5390007feb87STariq Toukan 		fallthrough;
5391007feb87STariq Toukan #endif
5392007feb87STariq Toukan 	default: /* AF_INET */
5393007feb87STariq Toukan 		flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
5394007feb87STariq Toukan 		flow->addrs.v4addrs.src = inet_sk(sk)->inet_rcv_saddr;
5395007feb87STariq Toukan 		flow->addrs.v4addrs.dst = inet_sk(sk)->inet_daddr;
5396007feb87STariq Toukan 		break;
5397007feb87STariq Toukan 	}
5398007feb87STariq Toukan 
5399007feb87STariq Toukan 	flow->ports.src = inet_sk(sk)->inet_sport;
5400007feb87STariq Toukan 	flow->ports.dst = inet_sk(sk)->inet_dport;
5401007feb87STariq Toukan }
5402007feb87STariq Toukan 
5403007feb87STariq Toukan /**
5404007feb87STariq Toukan  * bond_sk_hash_l34 - generate a hash value based on the socket's L3 and L4 fields
5405007feb87STariq Toukan  * @sk: socket to use for headers
5406007feb87STariq Toukan  *
5407007feb87STariq Toukan  * This function will extract the necessary field from the socket and use
5408007feb87STariq Toukan  * them to generate a hash based on the LAYER34 xmit_policy.
5409007feb87STariq Toukan  * Assumes that sk is a TCP or UDP socket.
5410007feb87STariq Toukan  */
bond_sk_hash_l34(struct sock * sk)5411007feb87STariq Toukan static u32 bond_sk_hash_l34(struct sock *sk)
5412007feb87STariq Toukan {
5413007feb87STariq Toukan 	struct flow_keys flow;
5414007feb87STariq Toukan 	u32 hash;
5415007feb87STariq Toukan 
5416007feb87STariq Toukan 	bond_sk_to_flow(sk, &flow);
5417007feb87STariq Toukan 
5418007feb87STariq Toukan 	/* L4 */
5419007feb87STariq Toukan 	memcpy(&hash, &flow.ports.ports, sizeof(hash));
5420007feb87STariq Toukan 	/* L3 */
542149aefd13Ssuresh kumar 	return bond_ip_hash(hash, &flow, BOND_XMIT_POLICY_LAYER34);
5422007feb87STariq Toukan }
5423007feb87STariq Toukan 
__bond_sk_get_lower_dev(struct bonding * bond,struct sock * sk)5424007feb87STariq Toukan static struct net_device *__bond_sk_get_lower_dev(struct bonding *bond,
5425007feb87STariq Toukan 						  struct sock *sk)
5426007feb87STariq Toukan {
5427007feb87STariq Toukan 	struct bond_up_slave *slaves;
5428007feb87STariq Toukan 	struct slave *slave;
5429007feb87STariq Toukan 	unsigned int count;
5430007feb87STariq Toukan 	u32 hash;
5431007feb87STariq Toukan 
5432007feb87STariq Toukan 	slaves = rcu_dereference(bond->usable_slaves);
5433007feb87STariq Toukan 	count = slaves ? READ_ONCE(slaves->count) : 0;
5434007feb87STariq Toukan 	if (unlikely(!count))
5435007feb87STariq Toukan 		return NULL;
5436007feb87STariq Toukan 
5437007feb87STariq Toukan 	hash = bond_sk_hash_l34(sk);
5438007feb87STariq Toukan 	slave = slaves->arr[hash % count];
5439007feb87STariq Toukan 
5440007feb87STariq Toukan 	return slave->dev;
5441007feb87STariq Toukan }
5442007feb87STariq Toukan 
bond_sk_get_lower_dev(struct net_device * dev,struct sock * sk)5443007feb87STariq Toukan static struct net_device *bond_sk_get_lower_dev(struct net_device *dev,
5444007feb87STariq Toukan 						struct sock *sk)
5445007feb87STariq Toukan {
5446007feb87STariq Toukan 	struct bonding *bond = netdev_priv(dev);
5447007feb87STariq Toukan 	struct net_device *lower = NULL;
5448007feb87STariq Toukan 
5449007feb87STariq Toukan 	rcu_read_lock();
5450007feb87STariq Toukan 	if (bond_sk_check(bond))
5451007feb87STariq Toukan 		lower = __bond_sk_get_lower_dev(bond, sk);
5452007feb87STariq Toukan 	rcu_read_unlock();
5453007feb87STariq Toukan 
5454007feb87STariq Toukan 	return lower;
5455007feb87STariq Toukan }
5456007feb87STariq Toukan 
545789df6a81STariq Toukan #if IS_ENABLED(CONFIG_TLS_DEVICE)
bond_tls_device_xmit(struct bonding * bond,struct sk_buff * skb,struct net_device * dev)545889df6a81STariq Toukan static netdev_tx_t bond_tls_device_xmit(struct bonding *bond, struct sk_buff *skb,
545989df6a81STariq Toukan 					struct net_device *dev)
546089df6a81STariq Toukan {
546194ce3b64SMaxim Mikityanskiy 	struct net_device *tls_netdev = rcu_dereference(tls_get_ctx(skb->sk)->netdev);
546294ce3b64SMaxim Mikityanskiy 
5463ed3c9a2fSJakub Kicinski 	/* tls_netdev might become NULL, even if tls_is_skb_tx_device_offloaded
546494ce3b64SMaxim Mikityanskiy 	 * was true, if tls_device_down is running in parallel, but it's OK,
546594ce3b64SMaxim Mikityanskiy 	 * because bond_get_slave_by_dev has a NULL check.
546694ce3b64SMaxim Mikityanskiy 	 */
546794ce3b64SMaxim Mikityanskiy 	if (likely(bond_get_slave_by_dev(bond, tls_netdev)))
546894ce3b64SMaxim Mikityanskiy 		return bond_dev_queue_xmit(bond, skb, tls_netdev);
546989df6a81STariq Toukan 	return bond_tx_drop(dev, skb);
547089df6a81STariq Toukan }
547189df6a81STariq Toukan #endif
547289df6a81STariq Toukan 
__bond_start_xmit(struct sk_buff * skb,struct net_device * dev)54730693e88eSMichał Mirosław static netdev_tx_t __bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
547400829823SStephen Hemminger {
5475bb1d9123SAndy Gospodarek 	struct bonding *bond = netdev_priv(dev);
5476bb1d9123SAndy Gospodarek 
5477d1e2e5cdSVeaceslav Falico 	if (bond_should_override_tx_queue(bond) &&
5478d1e2e5cdSVeaceslav Falico 	    !bond_slave_override(bond, skb))
5479bb1d9123SAndy Gospodarek 		return NETDEV_TX_OK;
548000829823SStephen Hemminger 
548189df6a81STariq Toukan #if IS_ENABLED(CONFIG_TLS_DEVICE)
5482ed3c9a2fSJakub Kicinski 	if (tls_is_skb_tx_device_offloaded(skb))
548389df6a81STariq Toukan 		return bond_tls_device_xmit(bond, skb, dev);
548489df6a81STariq Toukan #endif
548589df6a81STariq Toukan 
548601844098SVeaceslav Falico 	switch (BOND_MODE(bond)) {
548700829823SStephen Hemminger 	case BOND_MODE_ROUNDROBIN:
548800829823SStephen Hemminger 		return bond_xmit_roundrobin(skb, dev);
548900829823SStephen Hemminger 	case BOND_MODE_ACTIVEBACKUP:
549000829823SStephen Hemminger 		return bond_xmit_activebackup(skb, dev);
5491ee637714SMahesh Bandewar 	case BOND_MODE_8023AD:
549200829823SStephen Hemminger 	case BOND_MODE_XOR:
5493ee637714SMahesh Bandewar 		return bond_3ad_xor_xmit(skb, dev);
549400829823SStephen Hemminger 	case BOND_MODE_BROADCAST:
549500829823SStephen Hemminger 		return bond_xmit_broadcast(skb, dev);
549600829823SStephen Hemminger 	case BOND_MODE_ALB:
549700829823SStephen Hemminger 		return bond_alb_xmit(skb, dev);
5498f05b42eaSMahesh Bandewar 	case BOND_MODE_TLB:
5499f05b42eaSMahesh Bandewar 		return bond_tlb_xmit(skb, dev);
550000829823SStephen Hemminger 	default:
550100829823SStephen Hemminger 		/* Should never happen, mode already checked */
550276444f50SVeaceslav Falico 		netdev_err(dev, "Unknown bonding mode %d\n", BOND_MODE(bond));
550300829823SStephen Hemminger 		WARN_ON_ONCE(1);
5504ae46f184SEric Dumazet 		return bond_tx_drop(dev, skb);
550500829823SStephen Hemminger 	}
550600829823SStephen Hemminger }
550700829823SStephen Hemminger 
bond_start_xmit(struct sk_buff * skb,struct net_device * dev)55080693e88eSMichał Mirosław static netdev_tx_t bond_start_xmit(struct sk_buff *skb, struct net_device *dev)
55090693e88eSMichał Mirosław {
55100693e88eSMichał Mirosław 	struct bonding *bond = netdev_priv(dev);
55110693e88eSMichał Mirosław 	netdev_tx_t ret = NETDEV_TX_OK;
55120693e88eSMichał Mirosław 
5513547942caSNikolay Aleksandrov 	/* If we risk deadlock from transmitting this in the
55140693e88eSMichał Mirosław 	 * netpoll path, tell netpoll to queue the frame for later tx
55150693e88eSMichał Mirosław 	 */
5516054bb880Sdingtianhong 	if (unlikely(is_netpoll_tx_blocked(dev)))
55170693e88eSMichał Mirosław 		return NETDEV_TX_BUSY;
55180693e88eSMichał Mirosław 
5519278b2083Snikolay@redhat.com 	rcu_read_lock();
55200965a1f3SVeaceslav Falico 	if (bond_has_slaves(bond))
55210693e88eSMichał Mirosław 		ret = __bond_start_xmit(skb, dev);
55220693e88eSMichał Mirosław 	else
5523ae46f184SEric Dumazet 		ret = bond_tx_drop(dev, skb);
5524278b2083Snikolay@redhat.com 	rcu_read_unlock();
55250693e88eSMichał Mirosław 
55260693e88eSMichał Mirosław 	return ret;
55270693e88eSMichał Mirosław }
552800829823SStephen Hemminger 
55299e2ee5c7SJussi Maki static struct net_device *
bond_xdp_get_xmit_slave(struct net_device * bond_dev,struct xdp_buff * xdp)55309e2ee5c7SJussi Maki bond_xdp_get_xmit_slave(struct net_device *bond_dev, struct xdp_buff *xdp)
55319e2ee5c7SJussi Maki {
55329e2ee5c7SJussi Maki 	struct bonding *bond = netdev_priv(bond_dev);
55339e2ee5c7SJussi Maki 	struct slave *slave;
55349e2ee5c7SJussi Maki 
55359e2ee5c7SJussi Maki 	/* Caller needs to hold rcu_read_lock() */
55369e2ee5c7SJussi Maki 
55379e2ee5c7SJussi Maki 	switch (BOND_MODE(bond)) {
55389e2ee5c7SJussi Maki 	case BOND_MODE_ROUNDROBIN:
55399e2ee5c7SJussi Maki 		slave = bond_xdp_xmit_roundrobin_slave_get(bond, xdp);
55409e2ee5c7SJussi Maki 		break;
55419e2ee5c7SJussi Maki 
55429e2ee5c7SJussi Maki 	case BOND_MODE_ACTIVEBACKUP:
55439e2ee5c7SJussi Maki 		slave = bond_xmit_activebackup_slave_get(bond);
55449e2ee5c7SJussi Maki 		break;
55459e2ee5c7SJussi Maki 
55469e2ee5c7SJussi Maki 	case BOND_MODE_8023AD:
55479e2ee5c7SJussi Maki 	case BOND_MODE_XOR:
55489e2ee5c7SJussi Maki 		slave = bond_xdp_xmit_3ad_xor_slave_get(bond, xdp);
55499e2ee5c7SJussi Maki 		break;
55509e2ee5c7SJussi Maki 
55519e2ee5c7SJussi Maki 	default:
5552ccd3e6ffSJiwon Kim 		if (net_ratelimit())
5553ccd3e6ffSJiwon Kim 			netdev_err(bond_dev, "Unknown bonding mode %d for xdp xmit\n",
5554ccd3e6ffSJiwon Kim 				   BOND_MODE(bond));
55559e2ee5c7SJussi Maki 		return NULL;
55569e2ee5c7SJussi Maki 	}
55579e2ee5c7SJussi Maki 
55589e2ee5c7SJussi Maki 	if (slave)
55599e2ee5c7SJussi Maki 		return slave->dev;
55609e2ee5c7SJussi Maki 
55619e2ee5c7SJussi Maki 	return NULL;
55629e2ee5c7SJussi Maki }
55639e2ee5c7SJussi Maki 
bond_xdp_xmit(struct net_device * bond_dev,int n,struct xdp_frame ** frames,u32 flags)55649e2ee5c7SJussi Maki static int bond_xdp_xmit(struct net_device *bond_dev,
55659e2ee5c7SJussi Maki 			 int n, struct xdp_frame **frames, u32 flags)
55669e2ee5c7SJussi Maki {
55679e2ee5c7SJussi Maki 	int nxmit, err = -ENXIO;
55689e2ee5c7SJussi Maki 
55699e2ee5c7SJussi Maki 	rcu_read_lock();
55709e2ee5c7SJussi Maki 
55719e2ee5c7SJussi Maki 	for (nxmit = 0; nxmit < n; nxmit++) {
55729e2ee5c7SJussi Maki 		struct xdp_frame *frame = frames[nxmit];
55739e2ee5c7SJussi Maki 		struct xdp_frame *frames1[] = {frame};
55749e2ee5c7SJussi Maki 		struct net_device *slave_dev;
55759e2ee5c7SJussi Maki 		struct xdp_buff xdp;
55769e2ee5c7SJussi Maki 
55779e2ee5c7SJussi Maki 		xdp_convert_frame_to_buff(frame, &xdp);
55789e2ee5c7SJussi Maki 
55799e2ee5c7SJussi Maki 		slave_dev = bond_xdp_get_xmit_slave(bond_dev, &xdp);
55809e2ee5c7SJussi Maki 		if (!slave_dev) {
55819e2ee5c7SJussi Maki 			err = -ENXIO;
55829e2ee5c7SJussi Maki 			break;
55839e2ee5c7SJussi Maki 		}
55849e2ee5c7SJussi Maki 
55859e2ee5c7SJussi Maki 		err = slave_dev->netdev_ops->ndo_xdp_xmit(slave_dev, 1, frames1, flags);
55869e2ee5c7SJussi Maki 		if (err < 1)
55879e2ee5c7SJussi Maki 			break;
55889e2ee5c7SJussi Maki 	}
55899e2ee5c7SJussi Maki 
55909e2ee5c7SJussi Maki 	rcu_read_unlock();
55919e2ee5c7SJussi Maki 
55929e2ee5c7SJussi Maki 	/* If error happened on the first frame then we can pass the error up, otherwise
55939e2ee5c7SJussi Maki 	 * report the number of frames that were xmitted.
55949e2ee5c7SJussi Maki 	 */
55959e2ee5c7SJussi Maki 	if (err < 0)
55969e2ee5c7SJussi Maki 		return (nxmit == 0 ? err : nxmit);
55979e2ee5c7SJussi Maki 
55989e2ee5c7SJussi Maki 	return nxmit;
55999e2ee5c7SJussi Maki }
56009e2ee5c7SJussi Maki 
bond_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)56019e2ee5c7SJussi Maki static int bond_xdp_set(struct net_device *dev, struct bpf_prog *prog,
56029e2ee5c7SJussi Maki 			struct netlink_ext_ack *extack)
56039e2ee5c7SJussi Maki {
56049e2ee5c7SJussi Maki 	struct bonding *bond = netdev_priv(dev);
56059e2ee5c7SJussi Maki 	struct list_head *iter;
56069e2ee5c7SJussi Maki 	struct slave *slave, *rollback_slave;
56079e2ee5c7SJussi Maki 	struct bpf_prog *old_prog;
56089e2ee5c7SJussi Maki 	struct netdev_bpf xdp = {
56099e2ee5c7SJussi Maki 		.command = XDP_SETUP_PROG,
56109e2ee5c7SJussi Maki 		.flags   = 0,
56119e2ee5c7SJussi Maki 		.prog    = prog,
56129e2ee5c7SJussi Maki 		.extack  = extack,
56139e2ee5c7SJussi Maki 	};
56149e2ee5c7SJussi Maki 	int err;
56159e2ee5c7SJussi Maki 
56169e2ee5c7SJussi Maki 	ASSERT_RTNL();
56179e2ee5c7SJussi Maki 
56189e2ee5c7SJussi Maki 	if (!bond_xdp_check(bond))
56199e2ee5c7SJussi Maki 		return -EOPNOTSUPP;
56209e2ee5c7SJussi Maki 
56219e2ee5c7SJussi Maki 	old_prog = bond->xdp_prog;
56229e2ee5c7SJussi Maki 	bond->xdp_prog = prog;
56239e2ee5c7SJussi Maki 
56249e2ee5c7SJussi Maki 	bond_for_each_slave(bond, slave, iter) {
56259e2ee5c7SJussi Maki 		struct net_device *slave_dev = slave->dev;
56269e2ee5c7SJussi Maki 
56279e2ee5c7SJussi Maki 		if (!slave_dev->netdev_ops->ndo_bpf ||
56289e2ee5c7SJussi Maki 		    !slave_dev->netdev_ops->ndo_xdp_xmit) {
56296569fa2dSJonathan Toppins 			SLAVE_NL_ERR(dev, slave_dev, extack,
56306569fa2dSJonathan Toppins 				     "Slave device does not support XDP");
56319e2ee5c7SJussi Maki 			err = -EOPNOTSUPP;
56329e2ee5c7SJussi Maki 			goto err;
56339e2ee5c7SJussi Maki 		}
56349e2ee5c7SJussi Maki 
56359e2ee5c7SJussi Maki 		if (dev_xdp_prog_count(slave_dev) > 0) {
56366569fa2dSJonathan Toppins 			SLAVE_NL_ERR(dev, slave_dev, extack,
56379e2ee5c7SJussi Maki 				     "Slave has XDP program loaded, please unload before enslaving");
56389e2ee5c7SJussi Maki 			err = -EOPNOTSUPP;
56399e2ee5c7SJussi Maki 			goto err;
56409e2ee5c7SJussi Maki 		}
56419e2ee5c7SJussi Maki 
56429e2ee5c7SJussi Maki 		err = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
56439e2ee5c7SJussi Maki 		if (err < 0) {
56449e2ee5c7SJussi Maki 			/* ndo_bpf() sets extack error message */
56459e2ee5c7SJussi Maki 			slave_err(dev, slave_dev, "Error %d calling ndo_bpf\n", err);
56469e2ee5c7SJussi Maki 			goto err;
56479e2ee5c7SJussi Maki 		}
56489e2ee5c7SJussi Maki 		if (prog)
56499e2ee5c7SJussi Maki 			bpf_prog_inc(prog);
56509e2ee5c7SJussi Maki 	}
56519e2ee5c7SJussi Maki 
56526d5f1ef8SJussi Maki 	if (prog) {
56539e2ee5c7SJussi Maki 		static_branch_inc(&bpf_master_redirect_enabled_key);
56546d5f1ef8SJussi Maki 	} else if (old_prog) {
56556d5f1ef8SJussi Maki 		bpf_prog_put(old_prog);
56569e2ee5c7SJussi Maki 		static_branch_dec(&bpf_master_redirect_enabled_key);
56576d5f1ef8SJussi Maki 	}
56589e2ee5c7SJussi Maki 
56599e2ee5c7SJussi Maki 	return 0;
56609e2ee5c7SJussi Maki 
56619e2ee5c7SJussi Maki err:
56629e2ee5c7SJussi Maki 	/* unwind the program changes */
56639e2ee5c7SJussi Maki 	bond->xdp_prog = old_prog;
56649e2ee5c7SJussi Maki 	xdp.prog = old_prog;
56659e2ee5c7SJussi Maki 	xdp.extack = NULL; /* do not overwrite original error */
56669e2ee5c7SJussi Maki 
56679e2ee5c7SJussi Maki 	bond_for_each_slave(bond, rollback_slave, iter) {
56689e2ee5c7SJussi Maki 		struct net_device *slave_dev = rollback_slave->dev;
56699e2ee5c7SJussi Maki 		int err_unwind;
56709e2ee5c7SJussi Maki 
56719e2ee5c7SJussi Maki 		if (slave == rollback_slave)
56729e2ee5c7SJussi Maki 			break;
56739e2ee5c7SJussi Maki 
56749e2ee5c7SJussi Maki 		err_unwind = slave_dev->netdev_ops->ndo_bpf(slave_dev, &xdp);
56759e2ee5c7SJussi Maki 		if (err_unwind < 0)
56769e2ee5c7SJussi Maki 			slave_err(dev, slave_dev,
56779e2ee5c7SJussi Maki 				  "Error %d when unwinding XDP program change\n", err_unwind);
56789e2ee5c7SJussi Maki 		else if (xdp.prog)
56799e2ee5c7SJussi Maki 			bpf_prog_inc(xdp.prog);
56809e2ee5c7SJussi Maki 	}
56819e2ee5c7SJussi Maki 	return err;
56829e2ee5c7SJussi Maki }
56839e2ee5c7SJussi Maki 
bond_xdp(struct net_device * dev,struct netdev_bpf * xdp)56849e2ee5c7SJussi Maki static int bond_xdp(struct net_device *dev, struct netdev_bpf *xdp)
56859e2ee5c7SJussi Maki {
56869e2ee5c7SJussi Maki 	switch (xdp->command) {
56879e2ee5c7SJussi Maki 	case XDP_SETUP_PROG:
56889e2ee5c7SJussi Maki 		return bond_xdp_set(dev, xdp->prog, xdp->extack);
56899e2ee5c7SJussi Maki 	default:
56909e2ee5c7SJussi Maki 		return -EINVAL;
56919e2ee5c7SJussi Maki 	}
56929e2ee5c7SJussi Maki }
56939e2ee5c7SJussi Maki 
bond_mode_bcast_speed(struct slave * slave,u32 speed)56944ca0d9acSJarod Wilson static u32 bond_mode_bcast_speed(struct slave *slave, u32 speed)
56954ca0d9acSJarod Wilson {
56964ca0d9acSJarod Wilson 	if (speed == 0 || speed == SPEED_UNKNOWN)
56974ca0d9acSJarod Wilson 		speed = slave->speed;
56984ca0d9acSJarod Wilson 	else
56994ca0d9acSJarod Wilson 		speed = min(speed, slave->speed);
57004ca0d9acSJarod Wilson 
57014ca0d9acSJarod Wilson 	return speed;
57024ca0d9acSJarod Wilson }
57034ca0d9acSJarod Wilson 
5704c0dabeb4SMaxim Georgiev /* Set the BOND_PHC_INDEX flag to notify user space */
bond_set_phc_index_flag(struct kernel_hwtstamp_config * kernel_cfg)5705c0dabeb4SMaxim Georgiev static int bond_set_phc_index_flag(struct kernel_hwtstamp_config *kernel_cfg)
5706c0dabeb4SMaxim Georgiev {
5707c0dabeb4SMaxim Georgiev 	struct ifreq *ifr = kernel_cfg->ifr;
5708c0dabeb4SMaxim Georgiev 	struct hwtstamp_config cfg;
5709c0dabeb4SMaxim Georgiev 
5710c0dabeb4SMaxim Georgiev 	if (kernel_cfg->copied_to_user) {
5711c0dabeb4SMaxim Georgiev 		/* Lower device has a legacy implementation */
5712c0dabeb4SMaxim Georgiev 		if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
5713c0dabeb4SMaxim Georgiev 			return -EFAULT;
5714c0dabeb4SMaxim Georgiev 
5715c0dabeb4SMaxim Georgiev 		cfg.flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
5716c0dabeb4SMaxim Georgiev 		if (copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)))
5717c0dabeb4SMaxim Georgiev 			return -EFAULT;
5718c0dabeb4SMaxim Georgiev 	} else {
5719c0dabeb4SMaxim Georgiev 		kernel_cfg->flags |= HWTSTAMP_FLAG_BONDED_PHC_INDEX;
5720c0dabeb4SMaxim Georgiev 	}
5721c0dabeb4SMaxim Georgiev 
5722c0dabeb4SMaxim Georgiev 	return 0;
5723c0dabeb4SMaxim Georgiev }
5724c0dabeb4SMaxim Georgiev 
bond_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * cfg)5725c0dabeb4SMaxim Georgiev static int bond_hwtstamp_get(struct net_device *dev,
5726c0dabeb4SMaxim Georgiev 			     struct kernel_hwtstamp_config *cfg)
5727c0dabeb4SMaxim Georgiev {
5728c0dabeb4SMaxim Georgiev 	struct bonding *bond = netdev_priv(dev);
5729c0dabeb4SMaxim Georgiev 	struct net_device *real_dev;
5730c0dabeb4SMaxim Georgiev 	int err;
5731c0dabeb4SMaxim Georgiev 
5732c0dabeb4SMaxim Georgiev 	real_dev = bond_option_active_slave_get_rcu(bond);
5733c0dabeb4SMaxim Georgiev 	if (!real_dev)
5734c0dabeb4SMaxim Georgiev 		return -EOPNOTSUPP;
5735c0dabeb4SMaxim Georgiev 
5736c0dabeb4SMaxim Georgiev 	err = generic_hwtstamp_get_lower(real_dev, cfg);
5737c0dabeb4SMaxim Georgiev 	if (err)
5738c0dabeb4SMaxim Georgiev 		return err;
5739c0dabeb4SMaxim Georgiev 
5740c0dabeb4SMaxim Georgiev 	return bond_set_phc_index_flag(cfg);
5741c0dabeb4SMaxim Georgiev }
5742c0dabeb4SMaxim Georgiev 
bond_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * cfg,struct netlink_ext_ack * extack)5743c0dabeb4SMaxim Georgiev static int bond_hwtstamp_set(struct net_device *dev,
5744c0dabeb4SMaxim Georgiev 			     struct kernel_hwtstamp_config *cfg,
5745c0dabeb4SMaxim Georgiev 			     struct netlink_ext_ack *extack)
5746c0dabeb4SMaxim Georgiev {
5747c0dabeb4SMaxim Georgiev 	struct bonding *bond = netdev_priv(dev);
5748c0dabeb4SMaxim Georgiev 	struct net_device *real_dev;
5749c0dabeb4SMaxim Georgiev 	int err;
5750c0dabeb4SMaxim Georgiev 
5751c0dabeb4SMaxim Georgiev 	if (!(cfg->flags & HWTSTAMP_FLAG_BONDED_PHC_INDEX))
5752c0dabeb4SMaxim Georgiev 		return -EOPNOTSUPP;
5753c0dabeb4SMaxim Georgiev 
5754c0dabeb4SMaxim Georgiev 	real_dev = bond_option_active_slave_get_rcu(bond);
5755c0dabeb4SMaxim Georgiev 	if (!real_dev)
5756c0dabeb4SMaxim Georgiev 		return -EOPNOTSUPP;
5757c0dabeb4SMaxim Georgiev 
5758c0dabeb4SMaxim Georgiev 	err = generic_hwtstamp_set_lower(real_dev, cfg, extack);
5759c0dabeb4SMaxim Georgiev 	if (err)
5760c0dabeb4SMaxim Georgiev 		return err;
5761c0dabeb4SMaxim Georgiev 
5762c0dabeb4SMaxim Georgiev 	return bond_set_phc_index_flag(cfg);
5763c0dabeb4SMaxim Georgiev }
5764c0dabeb4SMaxim Georgiev 
bond_ethtool_get_link_ksettings(struct net_device * bond_dev,struct ethtool_link_ksettings * cmd)5765d46b6349SPhilippe Reynes static int bond_ethtool_get_link_ksettings(struct net_device *bond_dev,
5766d46b6349SPhilippe Reynes 					   struct ethtool_link_ksettings *cmd)
5767bb5b052fSAndy Gospodarek {
5768bb5b052fSAndy Gospodarek 	struct bonding *bond = netdev_priv(bond_dev);
57699caff1e7SVeaceslav Falico 	struct list_head *iter;
5770dec1e90eSnikolay@redhat.com 	struct slave *slave;
57714ca0d9acSJarod Wilson 	u32 speed = 0;
5772bb5b052fSAndy Gospodarek 
5773d46b6349SPhilippe Reynes 	cmd->base.duplex = DUPLEX_UNKNOWN;
5774d46b6349SPhilippe Reynes 	cmd->base.port = PORT_OTHER;
5775bb5b052fSAndy Gospodarek 
57768557cd74SVeaceslav Falico 	/* Since bond_slave_can_tx returns false for all inactive or down slaves, we
5777bb5b052fSAndy Gospodarek 	 * do not need to check mode.  Though link speed might not represent
5778bb5b052fSAndy Gospodarek 	 * the true receive or transmit bandwidth (not all modes are symmetric)
5779bb5b052fSAndy Gospodarek 	 * this is an accurate maximum.
5780bb5b052fSAndy Gospodarek 	 */
57819caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
57828557cd74SVeaceslav Falico 		if (bond_slave_can_tx(slave)) {
5783691b2bf1SHangbin Liu 			bond_update_speed_duplex(slave);
57844ca0d9acSJarod Wilson 			if (slave->speed != SPEED_UNKNOWN) {
57854ca0d9acSJarod Wilson 				if (BOND_MODE(bond) == BOND_MODE_BROADCAST)
57864ca0d9acSJarod Wilson 					speed = bond_mode_bcast_speed(slave,
57874ca0d9acSJarod Wilson 								      speed);
57884ca0d9acSJarod Wilson 				else
5789bb5b052fSAndy Gospodarek 					speed += slave->speed;
57904ca0d9acSJarod Wilson 			}
5791d46b6349SPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_UNKNOWN &&
5792bb5b052fSAndy Gospodarek 			    slave->duplex != DUPLEX_UNKNOWN)
5793d46b6349SPhilippe Reynes 				cmd->base.duplex = slave->duplex;
5794bb5b052fSAndy Gospodarek 		}
5795bb5b052fSAndy Gospodarek 	}
5796d46b6349SPhilippe Reynes 	cmd->base.speed = speed ? : SPEED_UNKNOWN;
5797dec1e90eSnikolay@redhat.com 
5798bb5b052fSAndy Gospodarek 	return 0;
5799bb5b052fSAndy Gospodarek }
5800bb5b052fSAndy Gospodarek 
bond_ethtool_get_drvinfo(struct net_device * bond_dev,struct ethtool_drvinfo * drvinfo)5801217df670SJay Vosburgh static void bond_ethtool_get_drvinfo(struct net_device *bond_dev,
5802217df670SJay Vosburgh 				     struct ethtool_drvinfo *drvinfo)
5803217df670SJay Vosburgh {
5804fb3ceec1SWolfram Sang 	strscpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
58057826d43fSJiri Pirko 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), "%d",
58067826d43fSJiri Pirko 		 BOND_ABI_VERSION);
5807217df670SJay Vosburgh }
5808217df670SJay Vosburgh 
bond_ethtool_get_ts_info(struct net_device * bond_dev,struct ethtool_ts_info * info)580994dd016aSHangbin Liu static int bond_ethtool_get_ts_info(struct net_device *bond_dev,
581094dd016aSHangbin Liu 				    struct ethtool_ts_info *info)
581194dd016aSHangbin Liu {
581294dd016aSHangbin Liu 	struct bonding *bond = netdev_priv(bond_dev);
5813980f0799SHangbin Liu 	struct ethtool_ts_info ts_info;
581494dd016aSHangbin Liu 	const struct ethtool_ops *ops;
581594dd016aSHangbin Liu 	struct net_device *real_dev;
5816980f0799SHangbin Liu 	bool sw_tx_support = false;
581794dd016aSHangbin Liu 	struct phy_device *phydev;
5818980f0799SHangbin Liu 	struct list_head *iter;
5819980f0799SHangbin Liu 	struct slave *slave;
58209b80ccdaSHangbin Liu 	int ret = 0;
582194dd016aSHangbin Liu 
58229b80ccdaSHangbin Liu 	rcu_read_lock();
582394dd016aSHangbin Liu 	real_dev = bond_option_active_slave_get_rcu(bond);
58249b80ccdaSHangbin Liu 	dev_hold(real_dev);
58259b80ccdaSHangbin Liu 	rcu_read_unlock();
58269b80ccdaSHangbin Liu 
582794dd016aSHangbin Liu 	if (real_dev) {
582894dd016aSHangbin Liu 		ops = real_dev->ethtool_ops;
582994dd016aSHangbin Liu 		phydev = real_dev->phydev;
583094dd016aSHangbin Liu 
583194dd016aSHangbin Liu 		if (phy_has_tsinfo(phydev)) {
58329b80ccdaSHangbin Liu 			ret = phy_ts_info(phydev, info);
58339b80ccdaSHangbin Liu 			goto out;
583494dd016aSHangbin Liu 		} else if (ops->get_ts_info) {
58359b80ccdaSHangbin Liu 			ret = ops->get_ts_info(real_dev, info);
58369b80ccdaSHangbin Liu 			goto out;
583794dd016aSHangbin Liu 		}
5838980f0799SHangbin Liu 	} else {
5839980f0799SHangbin Liu 		/* Check if all slaves support software tx timestamping */
5840980f0799SHangbin Liu 		rcu_read_lock();
5841980f0799SHangbin Liu 		bond_for_each_slave_rcu(bond, slave, iter) {
5842980f0799SHangbin Liu 			ret = -1;
5843980f0799SHangbin Liu 			ops = slave->dev->ethtool_ops;
5844980f0799SHangbin Liu 			phydev = slave->dev->phydev;
5845980f0799SHangbin Liu 
5846980f0799SHangbin Liu 			if (phy_has_tsinfo(phydev))
5847980f0799SHangbin Liu 				ret = phy_ts_info(phydev, &ts_info);
5848980f0799SHangbin Liu 			else if (ops->get_ts_info)
5849980f0799SHangbin Liu 				ret = ops->get_ts_info(slave->dev, &ts_info);
5850980f0799SHangbin Liu 
5851980f0799SHangbin Liu 			if (!ret && (ts_info.so_timestamping & SOF_TIMESTAMPING_TX_SOFTWARE)) {
5852980f0799SHangbin Liu 				sw_tx_support = true;
5853980f0799SHangbin Liu 				continue;
585494dd016aSHangbin Liu 			}
585594dd016aSHangbin Liu 
5856980f0799SHangbin Liu 			sw_tx_support = false;
5857980f0799SHangbin Liu 			break;
5858980f0799SHangbin Liu 		}
5859980f0799SHangbin Liu 		rcu_read_unlock();
5860980f0799SHangbin Liu 	}
5861980f0799SHangbin Liu 
5862980f0799SHangbin Liu 	ret = 0;
586394dd016aSHangbin Liu 	info->so_timestamping = SOF_TIMESTAMPING_RX_SOFTWARE |
586494dd016aSHangbin Liu 				SOF_TIMESTAMPING_SOFTWARE;
5865980f0799SHangbin Liu 	if (sw_tx_support)
5866980f0799SHangbin Liu 		info->so_timestamping |= SOF_TIMESTAMPING_TX_SOFTWARE;
5867980f0799SHangbin Liu 
586894dd016aSHangbin Liu 	info->phc_index = -1;
586994dd016aSHangbin Liu 
58709b80ccdaSHangbin Liu out:
58719b80ccdaSHangbin Liu 	dev_put(real_dev);
58729b80ccdaSHangbin Liu 	return ret;
587394dd016aSHangbin Liu }
587494dd016aSHangbin Liu 
58757282d491SJeff Garzik static const struct ethtool_ops bond_ethtool_ops = {
5876217df670SJay Vosburgh 	.get_drvinfo		= bond_ethtool_get_drvinfo,
5877fa53ebacSStephen Hemminger 	.get_link		= ethtool_op_get_link,
5878d46b6349SPhilippe Reynes 	.get_link_ksettings	= bond_ethtool_get_link_ksettings,
587994dd016aSHangbin Liu 	.get_ts_info		= bond_ethtool_get_ts_info,
58808531c5ffSArthur Kepner };
58818531c5ffSArthur Kepner 
5882eb7cc59aSStephen Hemminger static const struct net_device_ops bond_netdev_ops = {
5883181470fcSStephen Hemminger 	.ndo_init		= bond_init,
58849e71626cSStephen Hemminger 	.ndo_uninit		= bond_uninit,
5885eb7cc59aSStephen Hemminger 	.ndo_open		= bond_open,
5886eb7cc59aSStephen Hemminger 	.ndo_stop		= bond_close,
588700829823SStephen Hemminger 	.ndo_start_xmit		= bond_start_xmit,
5888bb1d9123SAndy Gospodarek 	.ndo_select_queue	= bond_select_queue,
5889be1f3c2cSBen Hutchings 	.ndo_get_stats64	= bond_get_stats,
5890a7605370SArnd Bergmann 	.ndo_eth_ioctl		= bond_eth_ioctl,
58913d9d00bdSArnd Bergmann 	.ndo_siocbond		= bond_do_ioctl,
5892232ec98eSArnd Bergmann 	.ndo_siocdevprivate	= bond_siocdevprivate,
5893d03462b9SJiri Pirko 	.ndo_change_rx_flags	= bond_change_rx_flags,
5894303d1cbfSJay Vosburgh 	.ndo_set_rx_mode	= bond_set_rx_mode,
5895eb7cc59aSStephen Hemminger 	.ndo_change_mtu		= bond_change_mtu,
5896eb7cc59aSStephen Hemminger 	.ndo_set_mac_address	= bond_set_mac_address,
589700829823SStephen Hemminger 	.ndo_neigh_setup	= bond_neigh_setup,
5898eb7cc59aSStephen Hemminger 	.ndo_vlan_rx_add_vid	= bond_vlan_rx_add_vid,
5899eb7cc59aSStephen Hemminger 	.ndo_vlan_rx_kill_vid	= bond_vlan_rx_kill_vid,
5900f6dc31a8SWANG Cong #ifdef CONFIG_NET_POLL_CONTROLLER
59018a8efa22SAmerigo Wang 	.ndo_netpoll_setup	= bond_netpoll_setup,
5902f6dc31a8SWANG Cong 	.ndo_netpoll_cleanup	= bond_netpoll_cleanup,
5903f6dc31a8SWANG Cong 	.ndo_poll_controller	= bond_poll_controller,
5904f6dc31a8SWANG Cong #endif
59059232eccaSJiri Pirko 	.ndo_add_slave		= bond_enslave,
59069232eccaSJiri Pirko 	.ndo_del_slave		= bond_release,
5907b2a103e6SMichał Mirosław 	.ndo_fix_features	= bond_fix_features,
59084847f049SToshiaki Makita 	.ndo_features_check	= passthru_features_check,
590933720aafSMaor Gottlieb 	.ndo_get_xmit_slave	= bond_xmit_get_slave,
5910007feb87STariq Toukan 	.ndo_sk_get_lower_dev	= bond_sk_get_lower_dev,
59119e2ee5c7SJussi Maki 	.ndo_bpf		= bond_xdp,
59129e2ee5c7SJussi Maki 	.ndo_xdp_xmit           = bond_xdp_xmit,
59139e2ee5c7SJussi Maki 	.ndo_xdp_get_xmit_slave = bond_xdp_get_xmit_slave,
5914c0dabeb4SMaxim Georgiev 	.ndo_hwtstamp_get	= bond_hwtstamp_get,
5915c0dabeb4SMaxim Georgiev 	.ndo_hwtstamp_set	= bond_hwtstamp_set,
5916eb7cc59aSStephen Hemminger };
5917eb7cc59aSStephen Hemminger 
5918b3f92b63SDoug Goldstein static const struct device_type bond_type = {
5919b3f92b63SDoug Goldstein 	.name = "bond",
5920b3f92b63SDoug Goldstein };
5921b3f92b63SDoug Goldstein 
bond_destructor(struct net_device * bond_dev)59229e2e61fbSAmerigo Wang static void bond_destructor(struct net_device *bond_dev)
59239e2e61fbSAmerigo Wang {
59249e2e61fbSAmerigo Wang 	struct bonding *bond = netdev_priv(bond_dev);
592586a5ad0aSYufeng Mo 
59269e2e61fbSAmerigo Wang 	if (bond->wq)
59279e2e61fbSAmerigo Wang 		destroy_workqueue(bond->wq);
5928848ca918SJussi Maki 
5929848ca918SJussi Maki 	free_percpu(bond->rr_tx_counter);
59309e2e61fbSAmerigo Wang }
59319e2e61fbSAmerigo Wang 
bond_setup(struct net_device * bond_dev)59320a2a78c4SJiri Pirko void bond_setup(struct net_device *bond_dev)
59331da177e4SLinus Torvalds {
5934454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
59351da177e4SLinus Torvalds 
5936b7435628SNikolay Aleksandrov 	spin_lock_init(&bond->mode_lock);
5937d2991f75SStephen Hemminger 	bond->params = bonding_defaults;
59381da177e4SLinus Torvalds 
59391da177e4SLinus Torvalds 	/* Initialize pointers */
59401da177e4SLinus Torvalds 	bond->dev = bond_dev;
59411da177e4SLinus Torvalds 
59421da177e4SLinus Torvalds 	/* Initialize the device entry points */
5943181470fcSStephen Hemminger 	ether_setup(bond_dev);
594431c05415SWANG Cong 	bond_dev->max_mtu = ETH_MAX_MTU;
5945eb7cc59aSStephen Hemminger 	bond_dev->netdev_ops = &bond_netdev_ops;
59468531c5ffSArthur Kepner 	bond_dev->ethtool_ops = &bond_ethtool_ops;
59471da177e4SLinus Torvalds 
5948cf124db5SDavid S. Miller 	bond_dev->needs_free_netdev = true;
5949cf124db5SDavid S. Miller 	bond_dev->priv_destructor = bond_destructor;
59501da177e4SLinus Torvalds 
5951b3f92b63SDoug Goldstein 	SET_NETDEV_DEVTYPE(bond_dev, &bond_type);
5952b3f92b63SDoug Goldstein 
59531da177e4SLinus Torvalds 	/* Initialize the device options */
59541098cee6SZhang Shengju 	bond_dev->flags |= IFF_MASTER;
59551e6f20caSPhil Sutter 	bond_dev->priv_flags |= IFF_BONDING | IFF_UNICAST_FLT | IFF_NO_QUEUE;
5956550fd08cSNeil Horman 	bond_dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
5957181470fcSStephen Hemminger 
595818cb261aSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
595918cb261aSJarod Wilson 	/* set up xfrm device ops (only supported in active-backup right now) */
596018cb261aSJarod Wilson 	bond_dev->xfrmdev_ops = &bond_xfrmdev_ops;
59619a560550STaehee Yoo 	INIT_LIST_HEAD(&bond->ipsec_list);
59626b598069SJianbo Liu 	mutex_init(&bond->ipsec_lock);
596318cb261aSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
596418cb261aSJarod Wilson 
5965547942caSNikolay Aleksandrov 	/* don't acquire bond device's netif_tx_lock when transmitting */
59661da177e4SLinus Torvalds 	bond_dev->features |= NETIF_F_LLTX;
59671da177e4SLinus Torvalds 
59681da177e4SLinus Torvalds 	/* By default, we declare the bond to be fully
59691da177e4SLinus Torvalds 	 * VLAN hardware accelerated capable. Special
59701da177e4SLinus Torvalds 	 * care is taken in the various xmit functions
59711da177e4SLinus Torvalds 	 * when there are slaves that are not hw accel
59721da177e4SLinus Torvalds 	 * capable
59731da177e4SLinus Torvalds 	 */
59741da177e4SLinus Torvalds 
5975f9399814SWeilong Chen 	/* Don't allow bond devices to change network namespaces. */
5976f9399814SWeilong Chen 	bond_dev->features |= NETIF_F_NETNS_LOCAL;
5977f9399814SWeilong Chen 
5978b2a103e6SMichał Mirosław 	bond_dev->hw_features = BOND_VLAN_FEATURES |
5979f646968fSPatrick McHardy 				NETIF_F_HW_VLAN_CTAG_RX |
598001f4fd27SZiyang Xuan 				NETIF_F_HW_VLAN_CTAG_FILTER |
598101f4fd27SZiyang Xuan 				NETIF_F_HW_VLAN_STAG_RX |
598201f4fd27SZiyang Xuan 				NETIF_F_HW_VLAN_STAG_FILTER;
5983b2a103e6SMichał Mirosław 
5984ecb8fed4SAlexander Lobakin 	bond_dev->hw_features |= NETIF_F_GSO_ENCAP_ALL;
5985b2a103e6SMichał Mirosław 	bond_dev->features |= bond_dev->hw_features;
598630d8177eSYueHaibing 	bond_dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
5987a3b658cfSJarod Wilson #ifdef CONFIG_XFRM_OFFLOAD
5988d241b382SJarod Wilson 	bond_dev->hw_features |= BOND_XFRM_FEATURES;
5989d241b382SJarod Wilson 	/* Only enable XFRM features if this is an active-backup config */
5990d241b382SJarod Wilson 	if (BOND_MODE(bond) == BOND_MODE_ACTIVEBACKUP)
5991d241b382SJarod Wilson 		bond_dev->features |= BOND_XFRM_FEATURES;
5992a3b658cfSJarod Wilson #endif /* CONFIG_XFRM_OFFLOAD */
59931da177e4SLinus Torvalds }
59941da177e4SLinus Torvalds 
5995547942caSNikolay Aleksandrov /* Destroy a bonding device.
5996c67dfb29SEric W. Biederman  * Must be under rtnl_lock when this function is called.
5997a434e43fSJay Vosburgh  */
bond_uninit(struct net_device * bond_dev)5998c67dfb29SEric W. Biederman static void bond_uninit(struct net_device *bond_dev)
5999a434e43fSJay Vosburgh {
6000454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
6001544a028eSVeaceslav Falico 	struct list_head *iter;
6002544a028eSVeaceslav Falico 	struct slave *slave;
6003a434e43fSJay Vosburgh 
6004f6dc31a8SWANG Cong 	bond_netpoll_cleanup(bond_dev);
6005f6dc31a8SWANG Cong 
6006c67dfb29SEric W. Biederman 	/* Release the bonded slaves */
6007544a028eSVeaceslav Falico 	bond_for_each_slave(bond, slave, iter)
6008f51048c3SWANG Cong 		__bond_release_one(bond_dev, slave->dev, true, true);
600976444f50SVeaceslav Falico 	netdev_info(bond_dev, "Released all slaves\n");
6010c67dfb29SEric W. Biederman 
60116b598069SJianbo Liu #ifdef CONFIG_XFRM_OFFLOAD
60126b598069SJianbo Liu 	mutex_destroy(&bond->ipsec_lock);
60136b598069SJianbo Liu #endif /* CONFIG_XFRM_OFFLOAD */
60146b598069SJianbo Liu 
6015a8f3f4b4SZhengchao Shao 	bond_set_slave_arr(bond, NULL, NULL);
6016ee637714SMahesh Bandewar 
6017a434e43fSJay Vosburgh 	list_del(&bond->bond_list);
6018a434e43fSJay Vosburgh 
6019f073c7caSTaku Izumi 	bond_debug_unregister(bond);
6020a434e43fSJay Vosburgh }
6021a434e43fSJay Vosburgh 
60221da177e4SLinus Torvalds /*------------------------- Module initialization ---------------------------*/
60231da177e4SLinus Torvalds 
bond_check_params(struct bond_params * params)6024e08190efSZhengchao Shao static int __init bond_check_params(struct bond_params *params)
60251da177e4SLinus Torvalds {
60265a5c5fd4Snikolay@redhat.com 	int arp_validate_value, fail_over_mac_value, primary_reselect_value, i;
6027f3253339Sstephen hemminger 	struct bond_opt_value newval;
6028f3253339Sstephen hemminger 	const struct bond_opt_value *valptr;
602972ccc471SJarod Wilson 	int arp_all_targets_value = 0;
60306791e466SMahesh Bandewar 	u16 ad_actor_sys_prio = 0;
6031d22a5fc0SMahesh Bandewar 	u16 ad_user_port_key = 0;
603272ccc471SJarod Wilson 	__be32 arp_target[BOND_MAX_ARP_TARGETS] = { 0 };
6033dc9c4d0fSMahesh Bandewar 	int arp_ip_count;
6034dc9c4d0fSMahesh Bandewar 	int bond_mode	= BOND_MODE_ROUNDROBIN;
6035dc9c4d0fSMahesh Bandewar 	int xmit_hashtype = BOND_XMIT_POLICY_LAYER2;
6036dc9c4d0fSMahesh Bandewar 	int lacp_fast = 0;
6037f13ad104SNikolay Aleksandrov 	int tlb_dynamic_lb;
6038f5b2b966SJay Vosburgh 
6039547942caSNikolay Aleksandrov 	/* Convert string parameters. */
60401da177e4SLinus Torvalds 	if (mode) {
60412b3798d5SNikolay Aleksandrov 		bond_opt_initstr(&newval, mode);
60422b3798d5SNikolay Aleksandrov 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_MODE), &newval);
60432b3798d5SNikolay Aleksandrov 		if (!valptr) {
60442b3798d5SNikolay Aleksandrov 			pr_err("Error: Invalid bonding mode \"%s\"\n", mode);
60451da177e4SLinus Torvalds 			return -EINVAL;
60461da177e4SLinus Torvalds 		}
60472b3798d5SNikolay Aleksandrov 		bond_mode = valptr->value;
60481da177e4SLinus Torvalds 	}
60491da177e4SLinus Torvalds 
6050169a3e66SJay Vosburgh 	if (xmit_hash_policy) {
6051e79c1055SDebabrata Banerjee 		if (bond_mode == BOND_MODE_ROUNDROBIN ||
6052e79c1055SDebabrata Banerjee 		    bond_mode == BOND_MODE_ACTIVEBACKUP ||
6053e79c1055SDebabrata Banerjee 		    bond_mode == BOND_MODE_BROADCAST) {
6054a4aee5c8SJoe Perches 			pr_info("xmit_hash_policy param is irrelevant in mode %s\n",
6055169a3e66SJay Vosburgh 				bond_mode_name(bond_mode));
6056169a3e66SJay Vosburgh 		} else {
6057a4b32ce7SNikolay Aleksandrov 			bond_opt_initstr(&newval, xmit_hash_policy);
6058a4b32ce7SNikolay Aleksandrov 			valptr = bond_opt_parse(bond_opt_get(BOND_OPT_XMIT_HASH),
6059a4b32ce7SNikolay Aleksandrov 						&newval);
6060a4b32ce7SNikolay Aleksandrov 			if (!valptr) {
6061a4aee5c8SJoe Perches 				pr_err("Error: Invalid xmit_hash_policy \"%s\"\n",
6062169a3e66SJay Vosburgh 				       xmit_hash_policy);
6063169a3e66SJay Vosburgh 				return -EINVAL;
6064169a3e66SJay Vosburgh 			}
6065a4b32ce7SNikolay Aleksandrov 			xmit_hashtype = valptr->value;
6066169a3e66SJay Vosburgh 		}
6067169a3e66SJay Vosburgh 	}
6068169a3e66SJay Vosburgh 
60691da177e4SLinus Torvalds 	if (lacp_rate) {
60701da177e4SLinus Torvalds 		if (bond_mode != BOND_MODE_8023AD) {
6071a4aee5c8SJoe Perches 			pr_info("lacp_rate param is irrelevant in mode %s\n",
60721da177e4SLinus Torvalds 				bond_mode_name(bond_mode));
60731da177e4SLinus Torvalds 		} else {
6074d3131de7SNikolay Aleksandrov 			bond_opt_initstr(&newval, lacp_rate);
6075d3131de7SNikolay Aleksandrov 			valptr = bond_opt_parse(bond_opt_get(BOND_OPT_LACP_RATE),
6076d3131de7SNikolay Aleksandrov 						&newval);
6077d3131de7SNikolay Aleksandrov 			if (!valptr) {
6078a4aee5c8SJoe Perches 				pr_err("Error: Invalid lacp rate \"%s\"\n",
6079d3131de7SNikolay Aleksandrov 				       lacp_rate);
60801da177e4SLinus Torvalds 				return -EINVAL;
60811da177e4SLinus Torvalds 			}
6082d3131de7SNikolay Aleksandrov 			lacp_fast = valptr->value;
60831da177e4SLinus Torvalds 		}
60841da177e4SLinus Torvalds 	}
60851da177e4SLinus Torvalds 
6086fd989c83SJay Vosburgh 	if (ad_select) {
6087548d28bdSNikolay Aleksandrov 		bond_opt_initstr(&newval, ad_select);
60889e5f5eebSNikolay Aleksandrov 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_SELECT),
60899e5f5eebSNikolay Aleksandrov 					&newval);
60909e5f5eebSNikolay Aleksandrov 		if (!valptr) {
60919e5f5eebSNikolay Aleksandrov 			pr_err("Error: Invalid ad_select \"%s\"\n", ad_select);
6092fd989c83SJay Vosburgh 			return -EINVAL;
6093fd989c83SJay Vosburgh 		}
60949e5f5eebSNikolay Aleksandrov 		params->ad_select = valptr->value;
60959e5f5eebSNikolay Aleksandrov 		if (bond_mode != BOND_MODE_8023AD)
609691565ebbSJoe Perches 			pr_warn("ad_select param only affects 802.3ad mode\n");
6097fd989c83SJay Vosburgh 	} else {
6098fd989c83SJay Vosburgh 		params->ad_select = BOND_AD_STABLE;
6099fd989c83SJay Vosburgh 	}
6100fd989c83SJay Vosburgh 
6101f5841306SNicolas de Pesloüan 	if (max_bonds < 0) {
610291565ebbSJoe Perches 		pr_warn("Warning: max_bonds (%d) not in range %d-%d, so it was reset to BOND_DEFAULT_MAX_BONDS (%d)\n",
6103b8a9787eSJay Vosburgh 			max_bonds, 0, INT_MAX, BOND_DEFAULT_MAX_BONDS);
61041da177e4SLinus Torvalds 		max_bonds = BOND_DEFAULT_MAX_BONDS;
61051da177e4SLinus Torvalds 	}
61061da177e4SLinus Torvalds 
61071da177e4SLinus Torvalds 	if (miimon < 0) {
610891565ebbSJoe Perches 		pr_warn("Warning: miimon module parameter (%d), not in range 0-%d, so it was reset to 0\n",
6109b98d9c66SNikolay Aleksandrov 			miimon, INT_MAX);
6110b98d9c66SNikolay Aleksandrov 		miimon = 0;
61111da177e4SLinus Torvalds 	}
61121da177e4SLinus Torvalds 
61131da177e4SLinus Torvalds 	if (updelay < 0) {
611491565ebbSJoe Perches 		pr_warn("Warning: updelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
61151da177e4SLinus Torvalds 			updelay, INT_MAX);
61161da177e4SLinus Torvalds 		updelay = 0;
61171da177e4SLinus Torvalds 	}
61181da177e4SLinus Torvalds 
61191da177e4SLinus Torvalds 	if (downdelay < 0) {
612091565ebbSJoe Perches 		pr_warn("Warning: downdelay module parameter (%d), not in range 0-%d, so it was reset to 0\n",
61211da177e4SLinus Torvalds 			downdelay, INT_MAX);
61221da177e4SLinus Torvalds 		downdelay = 0;
61231da177e4SLinus Torvalds 	}
61241da177e4SLinus Torvalds 
6125b3c898e2SDebabrata Banerjee 	if ((use_carrier != 0) && (use_carrier != 1)) {
6126b3c898e2SDebabrata Banerjee 		pr_warn("Warning: use_carrier module parameter (%d), not of valid value (0/1), so it was set to 1\n",
61271da177e4SLinus Torvalds 			use_carrier);
61281da177e4SLinus Torvalds 		use_carrier = 1;
61291da177e4SLinus Torvalds 	}
61301da177e4SLinus Torvalds 
6131ad246c99SBen Hutchings 	if (num_peer_notif < 0 || num_peer_notif > 255) {
613291565ebbSJoe Perches 		pr_warn("Warning: num_grat_arp/num_unsol_na (%d) not in range 0-255 so it was reset to 1\n",
6133ad246c99SBen Hutchings 			num_peer_notif);
6134ad246c99SBen Hutchings 		num_peer_notif = 1;
6135ad246c99SBen Hutchings 	}
6136ad246c99SBen Hutchings 
6137834db4bcSdingtianhong 	/* reset values for 802.3ad/TLB/ALB */
6138267bed77SVeaceslav Falico 	if (!bond_mode_uses_arp(bond_mode)) {
61391da177e4SLinus Torvalds 		if (!miimon) {
614091565ebbSJoe Perches 			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");
614191565ebbSJoe Perches 			pr_warn("Forcing miimon to 100msec\n");
6142fe9d04afSdingtianhong 			miimon = BOND_DEFAULT_MIIMON;
61431da177e4SLinus Torvalds 		}
61441da177e4SLinus Torvalds 	}
61451da177e4SLinus Torvalds 
6146bb1d9123SAndy Gospodarek 	if (tx_queues < 1 || tx_queues > 255) {
614791565ebbSJoe Perches 		pr_warn("Warning: tx_queues (%d) should be between 1 and 255, resetting to %d\n",
6148bb1d9123SAndy Gospodarek 			tx_queues, BOND_DEFAULT_TX_QUEUES);
6149bb1d9123SAndy Gospodarek 		tx_queues = BOND_DEFAULT_TX_QUEUES;
6150bb1d9123SAndy Gospodarek 	}
6151bb1d9123SAndy Gospodarek 
6152ebd8e497SAndy Gospodarek 	if ((all_slaves_active != 0) && (all_slaves_active != 1)) {
615391565ebbSJoe Perches 		pr_warn("Warning: all_slaves_active module parameter (%d), not of valid value (0/1), so it was set to 0\n",
615491565ebbSJoe Perches 			all_slaves_active);
6155ebd8e497SAndy Gospodarek 		all_slaves_active = 0;
6156ebd8e497SAndy Gospodarek 	}
6157ebd8e497SAndy Gospodarek 
6158c2952c31SFlavio Leitner 	if (resend_igmp < 0 || resend_igmp > 255) {
615991565ebbSJoe Perches 		pr_warn("Warning: resend_igmp (%d) should be between 0 and 255, resetting to %d\n",
6160c2952c31SFlavio Leitner 			resend_igmp, BOND_DEFAULT_RESEND_IGMP);
6161c2952c31SFlavio Leitner 		resend_igmp = BOND_DEFAULT_RESEND_IGMP;
6162c2952c31SFlavio Leitner 	}
6163c2952c31SFlavio Leitner 
6164aa59d851SNikolay Aleksandrov 	bond_opt_initval(&newval, packets_per_slave);
6165aa59d851SNikolay Aleksandrov 	if (!bond_opt_parse(bond_opt_get(BOND_OPT_PACKETS_PER_SLAVE), &newval)) {
616673958329SNikolay Aleksandrov 		pr_warn("Warning: packets_per_slave (%d) should be between 0 and %u resetting to 1\n",
616773958329SNikolay Aleksandrov 			packets_per_slave, USHRT_MAX);
616873958329SNikolay Aleksandrov 		packets_per_slave = 1;
616973958329SNikolay Aleksandrov 	}
617073958329SNikolay Aleksandrov 
61711da177e4SLinus Torvalds 	if (bond_mode == BOND_MODE_ALB) {
6172a4aee5c8SJoe Perches 		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",
61731da177e4SLinus Torvalds 			  updelay);
61741da177e4SLinus Torvalds 	}
61751da177e4SLinus Torvalds 
61761da177e4SLinus Torvalds 	if (!miimon) {
61771da177e4SLinus Torvalds 		if (updelay || downdelay) {
61781da177e4SLinus Torvalds 			/* just warn the user the up/down delay will have
61791da177e4SLinus Torvalds 			 * no effect since miimon is zero...
61801da177e4SLinus Torvalds 			 */
618191565ebbSJoe Perches 			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",
61821da177e4SLinus Torvalds 				updelay, downdelay);
61831da177e4SLinus Torvalds 		}
61841da177e4SLinus Torvalds 	} else {
61851da177e4SLinus Torvalds 		/* don't allow arp monitoring */
61861da177e4SLinus Torvalds 		if (arp_interval) {
618791565ebbSJoe Perches 			pr_warn("Warning: miimon (%d) and arp_interval (%d) can't be used simultaneously, disabling ARP monitoring\n",
61881da177e4SLinus Torvalds 				miimon, arp_interval);
61891da177e4SLinus Torvalds 			arp_interval = 0;
61901da177e4SLinus Torvalds 		}
61911da177e4SLinus Torvalds 
61921da177e4SLinus Torvalds 		if ((updelay % miimon) != 0) {
619391565ebbSJoe Perches 			pr_warn("Warning: updelay (%d) is not a multiple of miimon (%d), updelay rounded to %d ms\n",
619491565ebbSJoe Perches 				updelay, miimon, (updelay / miimon) * miimon);
61951da177e4SLinus Torvalds 		}
61961da177e4SLinus Torvalds 
61971da177e4SLinus Torvalds 		updelay /= miimon;
61981da177e4SLinus Torvalds 
61991da177e4SLinus Torvalds 		if ((downdelay % miimon) != 0) {
620091565ebbSJoe Perches 			pr_warn("Warning: downdelay (%d) is not a multiple of miimon (%d), downdelay rounded to %d ms\n",
62011da177e4SLinus Torvalds 				downdelay, miimon,
62021da177e4SLinus Torvalds 				(downdelay / miimon) * miimon);
62031da177e4SLinus Torvalds 		}
62041da177e4SLinus Torvalds 
62051da177e4SLinus Torvalds 		downdelay /= miimon;
62061da177e4SLinus Torvalds 	}
62071da177e4SLinus Torvalds 
62081da177e4SLinus Torvalds 	if (arp_interval < 0) {
620991565ebbSJoe Perches 		pr_warn("Warning: arp_interval module parameter (%d), not in range 0-%d, so it was reset to 0\n",
62107bdb04edSNikolay Aleksandrov 			arp_interval, INT_MAX);
62117bdb04edSNikolay Aleksandrov 		arp_interval = 0;
62121da177e4SLinus Torvalds 	}
62131da177e4SLinus Torvalds 
62145a5c5fd4Snikolay@redhat.com 	for (arp_ip_count = 0, i = 0;
62155a5c5fd4Snikolay@redhat.com 	     (arp_ip_count < BOND_MAX_ARP_TARGETS) && arp_ip_target[i]; i++) {
621689015c18Sdingtianhong 		__be32 ip;
6217547942caSNikolay Aleksandrov 
6218547942caSNikolay Aleksandrov 		/* not a complete check, but good enough to catch mistakes */
621989015c18Sdingtianhong 		if (!in4_pton(arp_ip_target[i], -1, (u8 *)&ip, -1, NULL) ||
62202807a9feSVeaceslav Falico 		    !bond_is_ip_target_ok(ip)) {
622191565ebbSJoe Perches 			pr_warn("Warning: bad arp_ip_target module parameter (%s), ARP monitoring will not be performed\n",
62225a5c5fd4Snikolay@redhat.com 				arp_ip_target[i]);
62231da177e4SLinus Torvalds 			arp_interval = 0;
62241da177e4SLinus Torvalds 		} else {
62250afee4e8SVeaceslav Falico 			if (bond_get_targets_ip(arp_target, ip) == -1)
62265a5c5fd4Snikolay@redhat.com 				arp_target[arp_ip_count++] = ip;
62270afee4e8SVeaceslav Falico 			else
622891565ebbSJoe Perches 				pr_warn("Warning: duplicate address %pI4 in arp_ip_target, skipping\n",
62290afee4e8SVeaceslav Falico 					&ip);
62301da177e4SLinus Torvalds 		}
62311da177e4SLinus Torvalds 	}
62321da177e4SLinus Torvalds 
62331da177e4SLinus Torvalds 	if (arp_interval && !arp_ip_count) {
62341da177e4SLinus Torvalds 		/* don't allow arping if no arp_ip_target given... */
623591565ebbSJoe Perches 		pr_warn("Warning: arp_interval module parameter (%d) specified without providing an arp_ip_target parameter, arp_interval was reset to 0\n",
62361da177e4SLinus Torvalds 			arp_interval);
62371da177e4SLinus Torvalds 		arp_interval = 0;
62381da177e4SLinus Torvalds 	}
62391da177e4SLinus Torvalds 
6240f5b2b966SJay Vosburgh 	if (arp_validate) {
6241f5b2b966SJay Vosburgh 		if (!arp_interval) {
6242a4aee5c8SJoe Perches 			pr_err("arp_validate requires arp_interval\n");
6243f5b2b966SJay Vosburgh 			return -EINVAL;
6244f5b2b966SJay Vosburgh 		}
6245f5b2b966SJay Vosburgh 
624616228881SNikolay Aleksandrov 		bond_opt_initstr(&newval, arp_validate);
624716228881SNikolay Aleksandrov 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_VALIDATE),
624816228881SNikolay Aleksandrov 					&newval);
624916228881SNikolay Aleksandrov 		if (!valptr) {
6250a4aee5c8SJoe Perches 			pr_err("Error: invalid arp_validate \"%s\"\n",
625116228881SNikolay Aleksandrov 			       arp_validate);
6252f5b2b966SJay Vosburgh 			return -EINVAL;
6253f5b2b966SJay Vosburgh 		}
625416228881SNikolay Aleksandrov 		arp_validate_value = valptr->value;
625516228881SNikolay Aleksandrov 	} else {
6256f5b2b966SJay Vosburgh 		arp_validate_value = 0;
625716228881SNikolay Aleksandrov 	}
6258f5b2b966SJay Vosburgh 
62598599b52eSVeaceslav Falico 	if (arp_all_targets) {
6260edf36b24SNikolay Aleksandrov 		bond_opt_initstr(&newval, arp_all_targets);
6261edf36b24SNikolay Aleksandrov 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_ARP_ALL_TARGETS),
6262edf36b24SNikolay Aleksandrov 					&newval);
6263edf36b24SNikolay Aleksandrov 		if (!valptr) {
62648599b52eSVeaceslav Falico 			pr_err("Error: invalid arp_all_targets_value \"%s\"\n",
62658599b52eSVeaceslav Falico 			       arp_all_targets);
62668599b52eSVeaceslav Falico 			arp_all_targets_value = 0;
6267edf36b24SNikolay Aleksandrov 		} else {
6268edf36b24SNikolay Aleksandrov 			arp_all_targets_value = valptr->value;
62698599b52eSVeaceslav Falico 		}
62708599b52eSVeaceslav Falico 	}
62718599b52eSVeaceslav Falico 
62721da177e4SLinus Torvalds 	if (miimon) {
6273a4aee5c8SJoe Perches 		pr_info("MII link monitoring set to %d ms\n", miimon);
62741da177e4SLinus Torvalds 	} else if (arp_interval) {
627516228881SNikolay Aleksandrov 		valptr = bond_opt_get_val(BOND_OPT_ARP_VALIDATE,
627616228881SNikolay Aleksandrov 					  arp_validate_value);
6277a4aee5c8SJoe Perches 		pr_info("ARP monitoring set to %d ms, validate %s, with %d target(s):",
627816228881SNikolay Aleksandrov 			arp_interval, valptr->string, arp_ip_count);
62791da177e4SLinus Torvalds 
62801da177e4SLinus Torvalds 		for (i = 0; i < arp_ip_count; i++)
628190194264SJoe Perches 			pr_cont(" %s", arp_ip_target[i]);
62821da177e4SLinus Torvalds 
628390194264SJoe Perches 		pr_cont("\n");
62841da177e4SLinus Torvalds 
6285b8a9787eSJay Vosburgh 	} else if (max_bonds) {
62861da177e4SLinus Torvalds 		/* miimon and arp_interval not set, we need one so things
62871da177e4SLinus Torvalds 		 * work as expected, see bonding.txt for details
62881da177e4SLinus Torvalds 		 */
628990194264SJoe Perches 		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");
62901da177e4SLinus Torvalds 	}
62911da177e4SLinus Torvalds 
6292ec0865a9SVeaceslav Falico 	if (primary && !bond_mode_uses_primary(bond_mode)) {
62931da177e4SLinus Torvalds 		/* currently, using a primary only makes sense
62941da177e4SLinus Torvalds 		 * in active backup, TLB or ALB modes
62951da177e4SLinus Torvalds 		 */
629691565ebbSJoe Perches 		pr_warn("Warning: %s primary device specified but has no effect in %s mode\n",
62971da177e4SLinus Torvalds 			primary, bond_mode_name(bond_mode));
62981da177e4SLinus Torvalds 		primary = NULL;
62991da177e4SLinus Torvalds 	}
63001da177e4SLinus Torvalds 
6301a549952aSJiri Pirko 	if (primary && primary_reselect) {
6302388d3a6dSNikolay Aleksandrov 		bond_opt_initstr(&newval, primary_reselect);
6303388d3a6dSNikolay Aleksandrov 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_PRIMARY_RESELECT),
6304388d3a6dSNikolay Aleksandrov 					&newval);
6305388d3a6dSNikolay Aleksandrov 		if (!valptr) {
6306a4aee5c8SJoe Perches 			pr_err("Error: Invalid primary_reselect \"%s\"\n",
6307388d3a6dSNikolay Aleksandrov 			       primary_reselect);
6308a549952aSJiri Pirko 			return -EINVAL;
6309a549952aSJiri Pirko 		}
6310388d3a6dSNikolay Aleksandrov 		primary_reselect_value = valptr->value;
6311a549952aSJiri Pirko 	} else {
6312a549952aSJiri Pirko 		primary_reselect_value = BOND_PRI_RESELECT_ALWAYS;
6313a549952aSJiri Pirko 	}
6314a549952aSJiri Pirko 
63153915c1e8SJay Vosburgh 	if (fail_over_mac) {
63161df6b6aaSNikolay Aleksandrov 		bond_opt_initstr(&newval, fail_over_mac);
63171df6b6aaSNikolay Aleksandrov 		valptr = bond_opt_parse(bond_opt_get(BOND_OPT_FAIL_OVER_MAC),
63181df6b6aaSNikolay Aleksandrov 					&newval);
63191df6b6aaSNikolay Aleksandrov 		if (!valptr) {
6320a4aee5c8SJoe Perches 			pr_err("Error: invalid fail_over_mac \"%s\"\n",
63211df6b6aaSNikolay Aleksandrov 			       fail_over_mac);
63223915c1e8SJay Vosburgh 			return -EINVAL;
63233915c1e8SJay Vosburgh 		}
63241df6b6aaSNikolay Aleksandrov 		fail_over_mac_value = valptr->value;
63253915c1e8SJay Vosburgh 		if (bond_mode != BOND_MODE_ACTIVEBACKUP)
632691565ebbSJoe Perches 			pr_warn("Warning: fail_over_mac only affects active-backup mode\n");
63273915c1e8SJay Vosburgh 	} else {
63283915c1e8SJay Vosburgh 		fail_over_mac_value = BOND_FOM_NONE;
63293915c1e8SJay Vosburgh 	}
6330dd957c57SJay Vosburgh 
63316791e466SMahesh Bandewar 	bond_opt_initstr(&newval, "default");
63326791e466SMahesh Bandewar 	valptr = bond_opt_parse(
63336791e466SMahesh Bandewar 			bond_opt_get(BOND_OPT_AD_ACTOR_SYS_PRIO),
63346791e466SMahesh Bandewar 				     &newval);
63356791e466SMahesh Bandewar 	if (!valptr) {
63366791e466SMahesh Bandewar 		pr_err("Error: No ad_actor_sys_prio default value");
63376791e466SMahesh Bandewar 		return -EINVAL;
63386791e466SMahesh Bandewar 	}
63396791e466SMahesh Bandewar 	ad_actor_sys_prio = valptr->value;
63406791e466SMahesh Bandewar 
6341d22a5fc0SMahesh Bandewar 	valptr = bond_opt_parse(bond_opt_get(BOND_OPT_AD_USER_PORT_KEY),
6342d22a5fc0SMahesh Bandewar 				&newval);
6343d22a5fc0SMahesh Bandewar 	if (!valptr) {
6344d22a5fc0SMahesh Bandewar 		pr_err("Error: No ad_user_port_key default value");
6345d22a5fc0SMahesh Bandewar 		return -EINVAL;
6346d22a5fc0SMahesh Bandewar 	}
6347d22a5fc0SMahesh Bandewar 	ad_user_port_key = valptr->value;
6348d22a5fc0SMahesh Bandewar 
63498b426dc5SMahesh Bandewar 	bond_opt_initstr(&newval, "default");
6350f13ad104SNikolay Aleksandrov 	valptr = bond_opt_parse(bond_opt_get(BOND_OPT_TLB_DYNAMIC_LB), &newval);
63518b426dc5SMahesh Bandewar 	if (!valptr) {
63528b426dc5SMahesh Bandewar 		pr_err("Error: No tlb_dynamic_lb default value");
63538b426dc5SMahesh Bandewar 		return -EINVAL;
63548b426dc5SMahesh Bandewar 	}
63558b426dc5SMahesh Bandewar 	tlb_dynamic_lb = valptr->value;
63568b426dc5SMahesh Bandewar 
63573a7129e5Sdingtianhong 	if (lp_interval == 0) {
635891565ebbSJoe Perches 		pr_warn("Warning: ip_interval must be between 1 and %d, so it was reset to %d\n",
63593a7129e5Sdingtianhong 			INT_MAX, BOND_ALB_DEFAULT_LP_INTERVAL);
63603a7129e5Sdingtianhong 		lp_interval = BOND_ALB_DEFAULT_LP_INTERVAL;
63613a7129e5Sdingtianhong 	}
63623a7129e5Sdingtianhong 
63631da177e4SLinus Torvalds 	/* fill params struct with the proper values */
63641da177e4SLinus Torvalds 	params->mode = bond_mode;
6365169a3e66SJay Vosburgh 	params->xmit_policy = xmit_hashtype;
63661da177e4SLinus Torvalds 	params->miimon = miimon;
6367ad246c99SBen Hutchings 	params->num_peer_notif = num_peer_notif;
63681da177e4SLinus Torvalds 	params->arp_interval = arp_interval;
6369f5b2b966SJay Vosburgh 	params->arp_validate = arp_validate_value;
63708599b52eSVeaceslav Falico 	params->arp_all_targets = arp_all_targets_value;
63715944b5abSHangbin Liu 	params->missed_max = 2;
63721da177e4SLinus Torvalds 	params->updelay = updelay;
63731da177e4SLinus Torvalds 	params->downdelay = downdelay;
637407a4ddecSVincent Bernat 	params->peer_notif_delay = 0;
63751da177e4SLinus Torvalds 	params->use_carrier = use_carrier;
63763a755cd8SHangbin Liu 	params->lacp_active = 1;
63771da177e4SLinus Torvalds 	params->lacp_fast = lacp_fast;
63781da177e4SLinus Torvalds 	params->primary[0] = 0;
6379a549952aSJiri Pirko 	params->primary_reselect = primary_reselect_value;
63803915c1e8SJay Vosburgh 	params->fail_over_mac = fail_over_mac_value;
6381bb1d9123SAndy Gospodarek 	params->tx_queues = tx_queues;
6382ebd8e497SAndy Gospodarek 	params->all_slaves_active = all_slaves_active;
6383c2952c31SFlavio Leitner 	params->resend_igmp = resend_igmp;
6384655f8919Sstephen hemminger 	params->min_links = min_links;
63853a7129e5Sdingtianhong 	params->lp_interval = lp_interval;
638673958329SNikolay Aleksandrov 	params->packets_per_slave = packets_per_slave;
63878b426dc5SMahesh Bandewar 	params->tlb_dynamic_lb = tlb_dynamic_lb;
63886791e466SMahesh Bandewar 	params->ad_actor_sys_prio = ad_actor_sys_prio;
638974514957SMahesh Bandewar 	eth_zero_addr(params->ad_actor_system);
6390d22a5fc0SMahesh Bandewar 	params->ad_user_port_key = ad_user_port_key;
6391809fa972SHannes Frederic Sowa 	if (packets_per_slave > 0) {
6392809fa972SHannes Frederic Sowa 		params->reciprocal_packets_per_slave =
6393809fa972SHannes Frederic Sowa 			reciprocal_value(packets_per_slave);
6394809fa972SHannes Frederic Sowa 	} else {
6395809fa972SHannes Frederic Sowa 		/* reciprocal_packets_per_slave is unused if
6396809fa972SHannes Frederic Sowa 		 * packets_per_slave is 0 or 1, just initialize it
6397809fa972SHannes Frederic Sowa 		 */
6398809fa972SHannes Frederic Sowa 		params->reciprocal_packets_per_slave =
6399809fa972SHannes Frederic Sowa 			(struct reciprocal_value) { 0 };
6400809fa972SHannes Frederic Sowa 	}
6401809fa972SHannes Frederic Sowa 
640243902070SKees Cook 	if (primary)
640343902070SKees Cook 		strscpy_pad(params->primary, primary, sizeof(params->primary));
64041da177e4SLinus Torvalds 
64051da177e4SLinus Torvalds 	memcpy(params->arp_targets, arp_target, sizeof(arp_target));
6406c4caa500SHangbin Liu #if IS_ENABLED(CONFIG_IPV6)
64074e24be01SHangbin Liu 	memset(params->ns_targets, 0, sizeof(struct in6_addr) * BOND_MAX_NS_TARGETS);
6408c4caa500SHangbin Liu #endif
64091da177e4SLinus Torvalds 
64101da177e4SLinus Torvalds 	return 0;
64111da177e4SLinus Torvalds }
64121da177e4SLinus Torvalds 
6413547942caSNikolay Aleksandrov /* Called from registration process */
bond_init(struct net_device * bond_dev)6414181470fcSStephen Hemminger static int bond_init(struct net_device *bond_dev)
6415181470fcSStephen Hemminger {
6416181470fcSStephen Hemminger 	struct bonding *bond = netdev_priv(bond_dev);
6417ec87fd3bSEric W. Biederman 	struct bond_net *bn = net_generic(dev_net(bond_dev), bond_net_id);
6418181470fcSStephen Hemminger 
641976444f50SVeaceslav Falico 	netdev_dbg(bond_dev, "Begin bond_init\n");
6420181470fcSStephen Hemminger 
6421f9f225ebSBhaktipriya Shridhar 	bond->wq = alloc_ordered_workqueue(bond_dev->name, WQ_MEM_RECLAIM);
6422181470fcSStephen Hemminger 	if (!bond->wq)
6423181470fcSStephen Hemminger 		return -ENOMEM;
6424181470fcSStephen Hemminger 
6425ae9b15fbSTaehee Yoo 	bond->notifier_ctx = false;
6426ae9b15fbSTaehee Yoo 
6427089bca2cSTaehee Yoo 	spin_lock_init(&bond->stats_lock);
64281a33e10eSCong Wang 	netdev_lockdep_set_classes(bond_dev);
6429181470fcSStephen Hemminger 
6430ec87fd3bSEric W. Biederman 	list_add_tail(&bond->bond_list, &bn->dev_list);
6431181470fcSStephen Hemminger 
64326151b3d4SEric W. Biederman 	bond_prepare_sysfs_group(bond);
643322bedad3SJiri Pirko 
6434f073c7caSTaku Izumi 	bond_debug_register(bond);
6435f073c7caSTaku Izumi 
6436409cc1f8SJiri Pirko 	/* Ensure valid dev_addr */
6437409cc1f8SJiri Pirko 	if (is_zero_ether_addr(bond_dev->dev_addr) &&
643897a1e639Snikolay@redhat.com 	    bond_dev->addr_assign_type == NET_ADDR_PERM)
6439409cc1f8SJiri Pirko 		eth_hw_addr_random(bond_dev);
6440409cc1f8SJiri Pirko 
6441181470fcSStephen Hemminger 	return 0;
6442181470fcSStephen Hemminger }
6443181470fcSStephen Hemminger 
bond_get_num_tx_queues(void)64440a2a78c4SJiri Pirko unsigned int bond_get_num_tx_queues(void)
6445d5da4510SJiri Pirko {
6446efacb309Sstephen hemminger 	return tx_queues;
6447d5da4510SJiri Pirko }
6448d5da4510SJiri Pirko 
6449dfe60397SMitch Williams /* Create a new bond based on the specified name and bonding parameters.
6450e4b91c48SJay Vosburgh  * If name is NULL, obtain a suitable "bond%d" name for us.
6451dfe60397SMitch Williams  * Caller must NOT hold rtnl_lock; we need to release it here before we
6452dfe60397SMitch Williams  * set up our sysfs entries.
6453dfe60397SMitch Williams  */
bond_create(struct net * net,const char * name)6454ec87fd3bSEric W. Biederman int bond_create(struct net *net, const char *name)
64551da177e4SLinus Torvalds {
6456dfe60397SMitch Williams 	struct net_device *bond_dev;
6457e913fb27SPai 	struct bonding *bond;
64582fa3ee93SJonathan Toppins 	int res = -ENOMEM;
64591da177e4SLinus Torvalds 
64601da177e4SLinus Torvalds 	rtnl_lock();
6461027ea041SJay Vosburgh 
64621c5cae81SJiri Pirko 	bond_dev = alloc_netdev_mq(sizeof(struct bonding),
6463c835a677STom Gundersen 				   name ? name : "bond%d", NET_NAME_UNKNOWN,
6464bb1d9123SAndy Gospodarek 				   bond_setup, tx_queues);
64652fa3ee93SJonathan Toppins 	if (!bond_dev)
64662fa3ee93SJonathan Toppins 		goto out;
64671da177e4SLinus Torvalds 
6468e913fb27SPai 	bond = netdev_priv(bond_dev);
6469ec87fd3bSEric W. Biederman 	dev_net_set(bond_dev, net);
647088ead977SEric W. Biederman 	bond_dev->rtnl_link_ops = &bond_link_ops;
647188ead977SEric W. Biederman 
64721da177e4SLinus Torvalds 	res = register_netdevice(bond_dev);
6473544f287bSTaehee Yoo 	if (res < 0) {
6474544f287bSTaehee Yoo 		free_netdev(bond_dev);
64752fa3ee93SJonathan Toppins 		goto out;
6476544f287bSTaehee Yoo 	}
64770daa2303SPeter Zijlstra 
6478e826eafaSPhil Oester 	netif_carrier_off(bond_dev);
6479e826eafaSPhil Oester 
64804493b81bSMahesh Bandewar 	bond_work_init_all(bond);
64814493b81bSMahesh Bandewar 
64822fa3ee93SJonathan Toppins out:
6483dfe60397SMitch Williams 	rtnl_unlock();
64842fa3ee93SJonathan Toppins 	return res;
64851da177e4SLinus Torvalds }
64861da177e4SLinus Torvalds 
bond_net_init(struct net * net)64872c8c1e72SAlexey Dobriyan static int __net_init bond_net_init(struct net *net)
6488ec87fd3bSEric W. Biederman {
648915449745SEric W. Biederman 	struct bond_net *bn = net_generic(net, bond_net_id);
6490ec87fd3bSEric W. Biederman 
6491ec87fd3bSEric W. Biederman 	bn->net = net;
6492ec87fd3bSEric W. Biederman 	INIT_LIST_HEAD(&bn->dev_list);
6493ec87fd3bSEric W. Biederman 
6494ec87fd3bSEric W. Biederman 	bond_create_proc_dir(bn);
64954c22400aSEric W. Biederman 	bond_create_sysfs(bn);
649615449745SEric W. Biederman 
649715449745SEric W. Biederman 	return 0;
6498ec87fd3bSEric W. Biederman }
6499ec87fd3bSEric W. Biederman 
bond_net_exit_batch(struct list_head * net_list)650016a41634SEric Dumazet static void __net_exit bond_net_exit_batch(struct list_head *net_list)
6501ec87fd3bSEric W. Biederman {
650216a41634SEric Dumazet 	struct bond_net *bn;
650316a41634SEric Dumazet 	struct net *net;
650469b0216aSnikolay@redhat.com 	LIST_HEAD(list);
6505ec87fd3bSEric W. Biederman 
650616a41634SEric Dumazet 	list_for_each_entry(net, net_list, exit_list) {
650716a41634SEric Dumazet 		bn = net_generic(net, bond_net_id);
65084c22400aSEric W. Biederman 		bond_destroy_sysfs(bn);
650916a41634SEric Dumazet 	}
651069b0216aSnikolay@redhat.com 
651169b0216aSnikolay@redhat.com 	/* Kill off any bonds created after unregistering bond rtnl ops */
651269b0216aSnikolay@redhat.com 	rtnl_lock();
651316a41634SEric Dumazet 	list_for_each_entry(net, net_list, exit_list) {
651416a41634SEric Dumazet 		struct bonding *bond, *tmp_bond;
651516a41634SEric Dumazet 
651616a41634SEric Dumazet 		bn = net_generic(net, bond_net_id);
651769b0216aSnikolay@redhat.com 		list_for_each_entry_safe(bond, tmp_bond, &bn->dev_list, bond_list)
651869b0216aSnikolay@redhat.com 			unregister_netdevice_queue(bond->dev, &list);
651916a41634SEric Dumazet 	}
652069b0216aSnikolay@redhat.com 	unregister_netdevice_many(&list);
652169b0216aSnikolay@redhat.com 	rtnl_unlock();
652223fa5c2cSVeaceslav Falico 
652316a41634SEric Dumazet 	list_for_each_entry(net, net_list, exit_list) {
652416a41634SEric Dumazet 		bn = net_generic(net, bond_net_id);
652523fa5c2cSVeaceslav Falico 		bond_destroy_proc_dir(bn);
6526ec87fd3bSEric W. Biederman 	}
652716a41634SEric Dumazet }
6528ec87fd3bSEric W. Biederman 
6529ec87fd3bSEric W. Biederman static struct pernet_operations bond_net_ops = {
6530ec87fd3bSEric W. Biederman 	.init = bond_net_init,
653116a41634SEric Dumazet 	.exit_batch = bond_net_exit_batch,
653215449745SEric W. Biederman 	.id   = &bond_net_id,
653315449745SEric W. Biederman 	.size = sizeof(struct bond_net),
6534ec87fd3bSEric W. Biederman };
6535ec87fd3bSEric W. Biederman 
bonding_init(void)6536dfe60397SMitch Williams static int __init bonding_init(void)
6537dfe60397SMitch Williams {
6538dfe60397SMitch Williams 	int i;
6539dfe60397SMitch Williams 	int res;
6540dfe60397SMitch Williams 
6541dfe60397SMitch Williams 	res = bond_check_params(&bonding_defaults);
65423d632c3fSStephen Hemminger 	if (res)
6543dfe60397SMitch Williams 		goto out;
6544dfe60397SMitch Williams 
6545f07224c1STony Battersby 	bond_create_debugfs();
6546f07224c1STony Battersby 
654715449745SEric W. Biederman 	res = register_pernet_subsys(&bond_net_ops);
6548ec87fd3bSEric W. Biederman 	if (res)
6549f07224c1STony Battersby 		goto err_net_ops;
6550027ea041SJay Vosburgh 
65510a2a78c4SJiri Pirko 	res = bond_netlink_init();
655288ead977SEric W. Biederman 	if (res)
65536639104bSEric W. Biederman 		goto err_link;
655488ead977SEric W. Biederman 
6555dfe60397SMitch Williams 	for (i = 0; i < max_bonds; i++) {
6556ec87fd3bSEric W. Biederman 		res = bond_create(&init_net, NULL);
6557dfe60397SMitch Williams 		if (res)
6558dfe60397SMitch Williams 			goto err;
6559dfe60397SMitch Williams 	}
6560dfe60397SMitch Williams 
656158deb77cSMatteo Croce 	skb_flow_dissector_init(&flow_keys_bonding,
656258deb77cSMatteo Croce 				flow_keys_bonding_keys,
656358deb77cSMatteo Croce 				ARRAY_SIZE(flow_keys_bonding_keys));
656458deb77cSMatteo Croce 
65651da177e4SLinus Torvalds 	register_netdevice_notifier(&bond_netdev_notifier);
6566dfe60397SMitch Williams out:
65671da177e4SLinus Torvalds 	return res;
656888ead977SEric W. Biederman err:
65690a2a78c4SJiri Pirko 	bond_netlink_fini();
65706639104bSEric W. Biederman err_link:
657115449745SEric W. Biederman 	unregister_pernet_subsys(&bond_net_ops);
6572f07224c1STony Battersby err_net_ops:
6573f07224c1STony Battersby 	bond_destroy_debugfs();
657488ead977SEric W. Biederman 	goto out;
6575dfe60397SMitch Williams 
65761da177e4SLinus Torvalds }
65771da177e4SLinus Torvalds 
bonding_exit(void)65781da177e4SLinus Torvalds static void __exit bonding_exit(void)
65791da177e4SLinus Torvalds {
65801da177e4SLinus Torvalds 	unregister_netdevice_notifier(&bond_netdev_notifier);
65811da177e4SLinus Torvalds 
65820a2a78c4SJiri Pirko 	bond_netlink_fini();
6583ffcdedb6Snikolay@redhat.com 	unregister_pernet_subsys(&bond_net_ops);
6584e843fa50SNeil Horman 
6585f07224c1STony Battersby 	bond_destroy_debugfs();
6586f07224c1STony Battersby 
6587e843fa50SNeil Horman #ifdef CONFIG_NET_POLL_CONTROLLER
6588547942caSNikolay Aleksandrov 	/* Make sure we don't have an imbalance on our netpoll blocking */
6589fb4fa76aSNeil Horman 	WARN_ON(atomic_read(&netpoll_block_tx));
6590e843fa50SNeil Horman #endif
65911da177e4SLinus Torvalds }
65921da177e4SLinus Torvalds 
65931da177e4SLinus Torvalds module_init(bonding_init);
65941da177e4SLinus Torvalds module_exit(bonding_exit);
65951da177e4SLinus Torvalds MODULE_LICENSE("GPL");
65962b526b56SLeon Romanovsky MODULE_DESCRIPTION(DRV_DESCRIPTION);
65971da177e4SLinus Torvalds MODULE_AUTHOR("Thomas Davis, tadavis@lbl.gov and many others");
6598