xref: /openbmc/linux/drivers/net/bonding/bond_alb.c (revision 87832e937c808a7ebc41254b408362e3255c87c9)
10a65089eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/skbuff.h>
71da177e4SLinus Torvalds #include <linux/netdevice.h>
81da177e4SLinus Torvalds #include <linux/etherdevice.h>
91da177e4SLinus Torvalds #include <linux/pkt_sched.h>
101da177e4SLinus Torvalds #include <linux/spinlock.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/timer.h>
131da177e4SLinus Torvalds #include <linux/ip.h>
141da177e4SLinus Torvalds #include <linux/ipv6.h>
151da177e4SLinus Torvalds #include <linux/if_arp.h>
161da177e4SLinus Torvalds #include <linux/if_ether.h>
171da177e4SLinus Torvalds #include <linux/if_bonding.h>
181da177e4SLinus Torvalds #include <linux/if_vlan.h>
191da177e4SLinus Torvalds #include <linux/in.h>
201da177e4SLinus Torvalds #include <net/arp.h>
212d1ea19dSVlad Yasevich #include <net/ipv6.h>
22c78b8b20SJakub Kicinski #include <net/ndisc.h>
231da177e4SLinus Torvalds #include <asm/byteorder.h>
241ef8019bSDavid S. Miller #include <net/bonding.h>
251ef8019bSDavid S. Miller #include <net/bond_alb.h>
261da177e4SLinus Torvalds 
27f87fda00SEric Dumazet static const u8 mac_v6_allmcast[ETH_ALEN + 2] __long_aligned = {
28885a136cSEric Dumazet 	0x33, 0x33, 0x00, 0x00, 0x00, 0x01
29885a136cSEric Dumazet };
301da177e4SLinus Torvalds static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #pragma pack(1)
331da177e4SLinus Torvalds struct learning_pkt {
341da177e4SLinus Torvalds 	u8 mac_dst[ETH_ALEN];
351da177e4SLinus Torvalds 	u8 mac_src[ETH_ALEN];
36d3bb52b0SAl Viro 	__be16 type;
371da177e4SLinus Torvalds 	u8 padding[ETH_ZLEN - ETH_HLEN];
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds struct arp_pkt {
41d3bb52b0SAl Viro 	__be16  hw_addr_space;
42d3bb52b0SAl Viro 	__be16  prot_addr_space;
431da177e4SLinus Torvalds 	u8      hw_addr_len;
441da177e4SLinus Torvalds 	u8      prot_addr_len;
45d3bb52b0SAl Viro 	__be16  op_code;
461da177e4SLinus Torvalds 	u8      mac_src[ETH_ALEN];	/* sender hardware address */
47d3bb52b0SAl Viro 	__be32  ip_src;			/* sender IP address */
481da177e4SLinus Torvalds 	u8      mac_dst[ETH_ALEN];	/* target hardware address */
49d3bb52b0SAl Viro 	__be32  ip_dst;			/* target IP address */
501da177e4SLinus Torvalds };
511da177e4SLinus Torvalds #pragma pack()
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /* Forward declaration */
546f238100SJakub Kicinski static void alb_send_learning_packets(struct slave *slave, const u8 mac_addr[],
55d0c21d43SVlad Yasevich 				      bool strict_match);
56e53665c6SJiri Bohac static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp);
57e53665c6SJiri Bohac static void rlb_src_unlink(struct bonding *bond, u32 index);
58e53665c6SJiri Bohac static void rlb_src_link(struct bonding *bond, u32 ip_src_hash,
59e53665c6SJiri Bohac 			 u32 ip_dst_hash);
601da177e4SLinus Torvalds 
_simple_hash(const u8 * hash_start,int hash_size)61eddc9ec5SArnaldo Carvalho de Melo static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	int i;
641da177e4SLinus Torvalds 	u8 hash = 0;
651da177e4SLinus Torvalds 
66fdb89d75SWang Yufen 	for (i = 0; i < hash_size; i++)
671da177e4SLinus Torvalds 		hash ^= hash_start[i];
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	return hash;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*********************** tlb specific functions ***************************/
731da177e4SLinus Torvalds 
tlb_init_table_entry(struct tlb_client_info * entry,int save_load)741da177e4SLinus Torvalds static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	if (save_load) {
771da177e4SLinus Torvalds 		entry->load_history = 1 + entry->tx_bytes /
781da177e4SLinus Torvalds 				      BOND_TLB_REBALANCE_INTERVAL;
791da177e4SLinus Torvalds 		entry->tx_bytes = 0;
801da177e4SLinus Torvalds 	}
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	entry->tx_slave = NULL;
831da177e4SLinus Torvalds 	entry->next = TLB_NULL_INDEX;
841da177e4SLinus Torvalds 	entry->prev = TLB_NULL_INDEX;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
tlb_init_slave(struct slave * slave)871da177e4SLinus Torvalds static inline void tlb_init_slave(struct slave *slave)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	SLAVE_TLB_INFO(slave).load = 0;
901da177e4SLinus Torvalds 	SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
__tlb_clear_slave(struct bonding * bond,struct slave * slave,int save_load)93f515e6b7SMaxim Uvarov static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
94f515e6b7SMaxim Uvarov 			 int save_load)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	struct tlb_client_info *tx_hash_table;
971da177e4SLinus Torvalds 	u32 index;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	/* clear slave from tx_hashtbl */
1001da177e4SLinus Torvalds 	tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
1011da177e4SLinus Torvalds 
102ce39a800SAndy Gospodarek 	/* skip this if we've already freed the tx hash table */
103ce39a800SAndy Gospodarek 	if (tx_hash_table) {
1041da177e4SLinus Torvalds 		index = SLAVE_TLB_INFO(slave).head;
1051da177e4SLinus Torvalds 		while (index != TLB_NULL_INDEX) {
1061da177e4SLinus Torvalds 			u32 next_index = tx_hash_table[index].next;
10786a5ad0aSYufeng Mo 
1081da177e4SLinus Torvalds 			tlb_init_table_entry(&tx_hash_table[index], save_load);
1091da177e4SLinus Torvalds 			index = next_index;
1101da177e4SLinus Torvalds 		}
111ce39a800SAndy Gospodarek 	}
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	tlb_init_slave(slave);
114f515e6b7SMaxim Uvarov }
1155af47b2fSJay Vosburgh 
tlb_clear_slave(struct bonding * bond,struct slave * slave,int save_load)116f515e6b7SMaxim Uvarov static void tlb_clear_slave(struct bonding *bond, struct slave *slave,
117f515e6b7SMaxim Uvarov 			 int save_load)
118f515e6b7SMaxim Uvarov {
1194bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
120f515e6b7SMaxim Uvarov 	__tlb_clear_slave(bond, slave, save_load);
1214bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* Must be called before starting the monitor timer */
tlb_initialize(struct bonding * bond)1251da177e4SLinus Torvalds static int tlb_initialize(struct bonding *bond)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1281da177e4SLinus Torvalds 	int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
1290d206a3aSMitch Williams 	struct tlb_client_info *new_hashtbl;
1301da177e4SLinus Torvalds 	int i;
1311da177e4SLinus Torvalds 
132243cb4e5SJoe Jin 	new_hashtbl = kzalloc(size, GFP_KERNEL);
133e404decbSJoe Perches 	if (!new_hashtbl)
1346d9b6f42SAmitoj Kaur Chawla 		return -ENOMEM;
135e404decbSJoe Perches 
1364bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
1370d206a3aSMitch Williams 
1380d206a3aSMitch Williams 	bond_info->tx_hashtbl = new_hashtbl;
1391da177e4SLinus Torvalds 
140fdb89d75SWang Yufen 	for (i = 0; i < TLB_HASH_TABLE_SIZE; i++)
14138dbaf0aSPeter Pan(潘卫平) 		tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
1421da177e4SLinus Torvalds 
1434bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	return 0;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds /* Must be called only after all slaves have been released */
tlb_deinitialize(struct bonding * bond)1491da177e4SLinus Torvalds static void tlb_deinitialize(struct bonding *bond)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1521da177e4SLinus Torvalds 
1534bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	kfree(bond_info->tx_hashtbl);
1561da177e4SLinus Torvalds 	bond_info->tx_hashtbl = NULL;
1571da177e4SLinus Torvalds 
1584bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
compute_gap(struct slave * slave)161097811bbSJiri Pirko static long long compute_gap(struct slave *slave)
162097811bbSJiri Pirko {
163097811bbSJiri Pirko 	return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
164097811bbSJiri Pirko 	       (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
165097811bbSJiri Pirko }
166097811bbSJiri Pirko 
tlb_get_least_loaded_slave(struct bonding * bond)1671da177e4SLinus Torvalds static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct slave *slave, *least_loaded;
1709caff1e7SVeaceslav Falico 	struct list_head *iter;
171097811bbSJiri Pirko 	long long max_gap;
1721da177e4SLinus Torvalds 
173097811bbSJiri Pirko 	least_loaded = NULL;
174097811bbSJiri Pirko 	max_gap = LLONG_MIN;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/* Find the slave with the largest gap */
17728c71926Sdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
1788557cd74SVeaceslav Falico 		if (bond_slave_can_tx(slave)) {
179097811bbSJiri Pirko 			long long gap = compute_gap(slave);
180097811bbSJiri Pirko 
1811da177e4SLinus Torvalds 			if (max_gap < gap) {
1821da177e4SLinus Torvalds 				least_loaded = slave;
1831da177e4SLinus Torvalds 				max_gap = gap;
1841da177e4SLinus Torvalds 			}
1851da177e4SLinus Torvalds 		}
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	return least_loaded;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
__tlb_choose_channel(struct bonding * bond,u32 hash_index,u32 skb_len)191f515e6b7SMaxim Uvarov static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
192f515e6b7SMaxim Uvarov 						u32 skb_len)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1951da177e4SLinus Torvalds 	struct tlb_client_info *hash_table;
1961da177e4SLinus Torvalds 	struct slave *assigned_slave;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	hash_table = bond_info->tx_hashtbl;
1991da177e4SLinus Torvalds 	assigned_slave = hash_table[hash_index].tx_slave;
2001da177e4SLinus Torvalds 	if (!assigned_slave) {
2011da177e4SLinus Torvalds 		assigned_slave = tlb_get_least_loaded_slave(bond);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 		if (assigned_slave) {
2041da177e4SLinus Torvalds 			struct tlb_slave_info *slave_info =
2051da177e4SLinus Torvalds 				&(SLAVE_TLB_INFO(assigned_slave));
2061da177e4SLinus Torvalds 			u32 next_index = slave_info->head;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 			hash_table[hash_index].tx_slave = assigned_slave;
2091da177e4SLinus Torvalds 			hash_table[hash_index].next = next_index;
2101da177e4SLinus Torvalds 			hash_table[hash_index].prev = TLB_NULL_INDEX;
2111da177e4SLinus Torvalds 
212fdb89d75SWang Yufen 			if (next_index != TLB_NULL_INDEX)
2131da177e4SLinus Torvalds 				hash_table[next_index].prev = hash_index;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 			slave_info->head = hash_index;
2161da177e4SLinus Torvalds 			slave_info->load +=
2171da177e4SLinus Torvalds 				hash_table[hash_index].load_history;
2181da177e4SLinus Torvalds 		}
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
221fdb89d75SWang Yufen 	if (assigned_slave)
2221da177e4SLinus Torvalds 		hash_table[hash_index].tx_bytes += skb_len;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	return assigned_slave;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
tlb_choose_channel(struct bonding * bond,u32 hash_index,u32 skb_len)227f515e6b7SMaxim Uvarov static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
228f515e6b7SMaxim Uvarov 					u32 skb_len)
229f515e6b7SMaxim Uvarov {
230f515e6b7SMaxim Uvarov 	struct slave *tx_slave;
231547942caSNikolay Aleksandrov 
2324057c58dSWang Hai 	/* We don't need to disable softirq here, because
233f515e6b7SMaxim Uvarov 	 * tlb_choose_channel() is only called by bond_alb_xmit()
234f515e6b7SMaxim Uvarov 	 * which already has softirq disabled.
235f515e6b7SMaxim Uvarov 	 */
2364bab16d7SNikolay Aleksandrov 	spin_lock(&bond->mode_lock);
237f515e6b7SMaxim Uvarov 	tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
2384bab16d7SNikolay Aleksandrov 	spin_unlock(&bond->mode_lock);
239547942caSNikolay Aleksandrov 
240f515e6b7SMaxim Uvarov 	return tx_slave;
241f515e6b7SMaxim Uvarov }
242f515e6b7SMaxim Uvarov 
2431da177e4SLinus Torvalds /*********************** rlb specific functions ***************************/
244f515e6b7SMaxim Uvarov 
2451da177e4SLinus Torvalds /* when an ARP REPLY is received from a client update its info
2461da177e4SLinus Torvalds  * in the rx_hashtbl
2471da177e4SLinus Torvalds  */
rlb_update_entry_from_arp(struct bonding * bond,struct arp_pkt * arp)2481da177e4SLinus Torvalds static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
2511da177e4SLinus Torvalds 	struct rlb_client_info *client_info;
2521da177e4SLinus Torvalds 	u32 hash_index;
2531da177e4SLinus Torvalds 
2544bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	hash_index = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
2571da177e4SLinus Torvalds 	client_info = &(bond_info->rx_hashtbl[hash_index]);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	if ((client_info->assigned) &&
2601da177e4SLinus Torvalds 	    (client_info->ip_src == arp->ip_dst) &&
26142d782acSFlavio Leitner 	    (client_info->ip_dst == arp->ip_src) &&
262a6700db1SJoe Perches 	    (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) {
2631da177e4SLinus Torvalds 		/* update the clients MAC address */
264ada0f863SJoe Perches 		ether_addr_copy(client_info->mac_dst, arp->mac_src);
2651da177e4SLinus Torvalds 		client_info->ntt = 1;
2661da177e4SLinus Torvalds 		bond_info->rx_ntt = 1;
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2694bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
rlb_arp_recv(const struct sk_buff * skb,struct bonding * bond,struct slave * slave)272de063b70SEric Dumazet static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond,
2733aba891dSJiri Pirko 			struct slave *slave)
2741da177e4SLinus Torvalds {
275de063b70SEric Dumazet 	struct arp_pkt *arp, _arp;
2761da177e4SLinus Torvalds 
2773aba891dSJiri Pirko 	if (skb->protocol != cpu_to_be16(ETH_P_ARP))
278b99215cdSDavid S. Miller 		goto out;
2796146b1a4SJay Vosburgh 
280de063b70SEric Dumazet 	arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp);
281de063b70SEric Dumazet 	if (!arp)
282b99215cdSDavid S. Miller 		goto out;
2831da177e4SLinus Torvalds 
284e53665c6SJiri Bohac 	/* We received an ARP from arp->ip_src.
285e53665c6SJiri Bohac 	 * We might have used this IP address previously (on the bonding host
286e53665c6SJiri Bohac 	 * itself or on a system that is bridged together with the bond).
287e53665c6SJiri Bohac 	 * However, if arp->mac_src is different than what is stored in
288e53665c6SJiri Bohac 	 * rx_hashtbl, some other host is now using the IP and we must prevent
289e53665c6SJiri Bohac 	 * sending out client updates with this IP address and the old MAC
290e53665c6SJiri Bohac 	 * address.
291e53665c6SJiri Bohac 	 * Clean up all hash table entries that have this address as ip_src but
292e53665c6SJiri Bohac 	 * have a different mac_src.
293e53665c6SJiri Bohac 	 */
294e53665c6SJiri Bohac 	rlb_purge_src_ip(bond, arp);
295e53665c6SJiri Bohac 
2961da177e4SLinus Torvalds 	if (arp->op_code == htons(ARPOP_REPLY)) {
2971da177e4SLinus Torvalds 		/* update rx hash table for this ARP */
2981da177e4SLinus Torvalds 		rlb_update_entry_from_arp(bond, arp);
2997ea2e423SJarod Wilson 		slave_dbg(bond->dev, slave->dev, "Server received an ARP Reply from client\n");
3001da177e4SLinus Torvalds 	}
301b99215cdSDavid S. Miller out:
302b99215cdSDavid S. Miller 	return RX_HANDLER_ANOTHER;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3058c0bc550SNikolay Aleksandrov /* Caller must hold rcu_read_lock() */
__rlb_next_rx_slave(struct bonding * bond)30628c71926Sdingtianhong static struct slave *__rlb_next_rx_slave(struct bonding *bond)
30728c71926Sdingtianhong {
30828c71926Sdingtianhong 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
30928c71926Sdingtianhong 	struct slave *before = NULL, *rx_slave = NULL, *slave;
31028c71926Sdingtianhong 	struct list_head *iter;
31128c71926Sdingtianhong 	bool found = false;
31228c71926Sdingtianhong 
31328c71926Sdingtianhong 	bond_for_each_slave_rcu(bond, slave, iter) {
3148557cd74SVeaceslav Falico 		if (!bond_slave_can_tx(slave))
31528c71926Sdingtianhong 			continue;
31628c71926Sdingtianhong 		if (!found) {
31728c71926Sdingtianhong 			if (!before || before->speed < slave->speed)
31828c71926Sdingtianhong 				before = slave;
31928c71926Sdingtianhong 		} else {
32028c71926Sdingtianhong 			if (!rx_slave || rx_slave->speed < slave->speed)
32128c71926Sdingtianhong 				rx_slave = slave;
32228c71926Sdingtianhong 		}
32328c71926Sdingtianhong 		if (slave == bond_info->rx_slave)
32428c71926Sdingtianhong 			found = true;
32528c71926Sdingtianhong 	}
32628c71926Sdingtianhong 	/* we didn't find anything after the current or we have something
32728c71926Sdingtianhong 	 * better before and up to the current slave
32828c71926Sdingtianhong 	 */
32928c71926Sdingtianhong 	if (!rx_slave || (before && rx_slave->speed < before->speed))
33028c71926Sdingtianhong 		rx_slave = before;
33128c71926Sdingtianhong 
33228c71926Sdingtianhong 	if (rx_slave)
33328c71926Sdingtianhong 		bond_info->rx_slave = rx_slave;
33428c71926Sdingtianhong 
33528c71926Sdingtianhong 	return rx_slave;
33628c71926Sdingtianhong }
33728c71926Sdingtianhong 
33856924c38SNikolay Aleksandrov /* Caller must hold RTNL, rcu_read_lock is obtained only to silence checkers */
rlb_next_rx_slave(struct bonding * bond)33956924c38SNikolay Aleksandrov static struct slave *rlb_next_rx_slave(struct bonding *bond)
34056924c38SNikolay Aleksandrov {
34156924c38SNikolay Aleksandrov 	struct slave *rx_slave;
34256924c38SNikolay Aleksandrov 
34356924c38SNikolay Aleksandrov 	ASSERT_RTNL();
34456924c38SNikolay Aleksandrov 
34556924c38SNikolay Aleksandrov 	rcu_read_lock();
34656924c38SNikolay Aleksandrov 	rx_slave = __rlb_next_rx_slave(bond);
34756924c38SNikolay Aleksandrov 	rcu_read_unlock();
34856924c38SNikolay Aleksandrov 
34956924c38SNikolay Aleksandrov 	return rx_slave;
35056924c38SNikolay Aleksandrov }
35156924c38SNikolay Aleksandrov 
3521da177e4SLinus Torvalds /* teach the switch the mac of a disabled slave
3531da177e4SLinus Torvalds  * on the primary for fault tolerance
3541da177e4SLinus Torvalds  *
35562c5f518SNikolay Aleksandrov  * Caller must hold RTNL
3561da177e4SLinus Torvalds  */
rlb_teach_disabled_mac_on_primary(struct bonding * bond,const u8 addr[])3576f238100SJakub Kicinski static void rlb_teach_disabled_mac_on_primary(struct bonding *bond,
3586f238100SJakub Kicinski 					      const u8 addr[])
3591da177e4SLinus Torvalds {
3601c72cfdcSNikolay Aleksandrov 	struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
3614740d638SEric Dumazet 
3624740d638SEric Dumazet 	if (!curr_active)
3631da177e4SLinus Torvalds 		return;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	if (!bond->alb_info.primary_is_promisc) {
3664740d638SEric Dumazet 		if (!dev_set_promiscuity(curr_active->dev, 1))
3671da177e4SLinus Torvalds 			bond->alb_info.primary_is_promisc = 1;
3687e1a1ac1SWang Chen 		else
3697e1a1ac1SWang Chen 			bond->alb_info.primary_is_promisc = 0;
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	bond->alb_info.rlb_promisc_timeout_counter = 0;
3731da177e4SLinus Torvalds 
3744740d638SEric Dumazet 	alb_send_learning_packets(curr_active, addr, true);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /* slave being removed should not be active at this point
3781da177e4SLinus Torvalds  *
379b2e7acebSdingtianhong  * Caller must hold rtnl.
3801da177e4SLinus Torvalds  */
rlb_clear_slave(struct bonding * bond,struct slave * slave)3811da177e4SLinus Torvalds static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
3841da177e4SLinus Torvalds 	struct rlb_client_info *rx_hash_table;
3851da177e4SLinus Torvalds 	u32 index, next_index;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* clear slave from rx_hashtbl */
3884bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	rx_hash_table = bond_info->rx_hashtbl;
391e53665c6SJiri Bohac 	index = bond_info->rx_hashtbl_used_head;
3921da177e4SLinus Torvalds 	for (; index != RLB_NULL_INDEX; index = next_index) {
393e53665c6SJiri Bohac 		next_index = rx_hash_table[index].used_next;
3941da177e4SLinus Torvalds 		if (rx_hash_table[index].slave == slave) {
3951da177e4SLinus Torvalds 			struct slave *assigned_slave = rlb_next_rx_slave(bond);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 			if (assigned_slave) {
3981da177e4SLinus Torvalds 				rx_hash_table[index].slave = assigned_slave;
399cbeeea70SDebabrata Banerjee 				if (is_valid_ether_addr(rx_hash_table[index].mac_dst)) {
4001da177e4SLinus Torvalds 					bond_info->rx_hashtbl[index].ntt = 1;
4011da177e4SLinus Torvalds 					bond_info->rx_ntt = 1;
4021da177e4SLinus Torvalds 					/* A slave has been removed from the
4031da177e4SLinus Torvalds 					 * table because it is either disabled
4041da177e4SLinus Torvalds 					 * or being released. We must retry the
4051da177e4SLinus Torvalds 					 * update to avoid clients from not
4061da177e4SLinus Torvalds 					 * being updated & disconnecting when
4071da177e4SLinus Torvalds 					 * there is stress
4081da177e4SLinus Torvalds 					 */
4091da177e4SLinus Torvalds 					bond_info->rlb_update_retry_counter =
4101da177e4SLinus Torvalds 						RLB_UPDATE_RETRY;
4111da177e4SLinus Torvalds 				}
4121da177e4SLinus Torvalds 			} else {  /* there is no active slave */
4131da177e4SLinus Torvalds 				rx_hash_table[index].slave = NULL;
4141da177e4SLinus Torvalds 			}
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4184bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
4191da177e4SLinus Torvalds 
4201c72cfdcSNikolay Aleksandrov 	if (slave != rtnl_dereference(bond->curr_active_slave))
4211da177e4SLinus Torvalds 		rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
rlb_update_client(struct rlb_client_info * client_info)4241da177e4SLinus Torvalds static void rlb_update_client(struct rlb_client_info *client_info)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	int i;
4271da177e4SLinus Torvalds 
4284fa8667cSDebabrata Banerjee 	if (!client_info->slave || !is_valid_ether_addr(client_info->mac_dst))
4291da177e4SLinus Torvalds 		return;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
4321da177e4SLinus Torvalds 		struct sk_buff *skb;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 		skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
4351da177e4SLinus Torvalds 				 client_info->ip_dst,
4361da177e4SLinus Torvalds 				 client_info->slave->dev,
4371da177e4SLinus Torvalds 				 client_info->ip_src,
4381da177e4SLinus Torvalds 				 client_info->mac_dst,
4391da177e4SLinus Torvalds 				 client_info->slave->dev->dev_addr,
4401da177e4SLinus Torvalds 				 client_info->mac_dst);
4411da177e4SLinus Torvalds 		if (!skb) {
4427ea2e423SJarod Wilson 			slave_err(client_info->slave->bond->dev,
4437ea2e423SJarod Wilson 				  client_info->slave->dev,
4440a111a03SVeaceslav Falico 				  "failed to create an ARP packet\n");
4451da177e4SLinus Torvalds 			continue;
4461da177e4SLinus Torvalds 		}
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 		skb->dev = client_info->slave->dev;
4491da177e4SLinus Torvalds 
450d3ab3ffdSVeaceslav Falico 		if (client_info->vlan_id) {
451b4bef1b5SJiri Pirko 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
452b4bef1b5SJiri Pirko 					       client_info->vlan_id);
4531da177e4SLinus Torvalds 		}
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 		arp_xmit(skb);
4561da177e4SLinus Torvalds 	}
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /* sends ARP REPLIES that update the clients that need updating */
rlb_update_rx_clients(struct bonding * bond)4601da177e4SLinus Torvalds static void rlb_update_rx_clients(struct bonding *bond)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
4631da177e4SLinus Torvalds 	struct rlb_client_info *client_info;
4641da177e4SLinus Torvalds 	u32 hash_index;
4651da177e4SLinus Torvalds 
4664bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
4671da177e4SLinus Torvalds 
468e53665c6SJiri Bohac 	hash_index = bond_info->rx_hashtbl_used_head;
469e53665c6SJiri Bohac 	for (; hash_index != RLB_NULL_INDEX;
470e53665c6SJiri Bohac 	     hash_index = client_info->used_next) {
4711da177e4SLinus Torvalds 		client_info = &(bond_info->rx_hashtbl[hash_index]);
4721da177e4SLinus Torvalds 		if (client_info->ntt) {
4731da177e4SLinus Torvalds 			rlb_update_client(client_info);
47435d75ee4SWang Yufen 			if (bond_info->rlb_update_retry_counter == 0)
4751da177e4SLinus Torvalds 				client_info->ntt = 0;
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
47994e2bd68SThadeu Lima de Souza Cascardo 	/* do not update the entries again until this counter is zero so that
4801da177e4SLinus Torvalds 	 * not to confuse the clients.
4811da177e4SLinus Torvalds 	 */
4821da177e4SLinus Torvalds 	bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
4831da177e4SLinus Torvalds 
4844bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* The slave was assigned a new mac address - update the clients */
rlb_req_update_slave_clients(struct bonding * bond,struct slave * slave)4881da177e4SLinus Torvalds static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
4911da177e4SLinus Torvalds 	struct rlb_client_info *client_info;
4921da177e4SLinus Torvalds 	int ntt = 0;
4931da177e4SLinus Torvalds 	u32 hash_index;
4941da177e4SLinus Torvalds 
4954bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
4961da177e4SLinus Torvalds 
497e53665c6SJiri Bohac 	hash_index = bond_info->rx_hashtbl_used_head;
498e53665c6SJiri Bohac 	for (; hash_index != RLB_NULL_INDEX;
499e53665c6SJiri Bohac 	     hash_index = client_info->used_next) {
5001da177e4SLinus Torvalds 		client_info = &(bond_info->rx_hashtbl[hash_index]);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 		if ((client_info->slave == slave) &&
503cbeeea70SDebabrata Banerjee 		    is_valid_ether_addr(client_info->mac_dst)) {
5041da177e4SLinus Torvalds 			client_info->ntt = 1;
5051da177e4SLinus Torvalds 			ntt = 1;
5061da177e4SLinus Torvalds 		}
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
5094708a1b1SWang Yufen 	/* update the team's flag only after the whole iteration */
5101da177e4SLinus Torvalds 	if (ntt) {
5111da177e4SLinus Torvalds 		bond_info->rx_ntt = 1;
5124708a1b1SWang Yufen 		/* fasten the change */
5131da177e4SLinus Torvalds 		bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 
5164bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds /* mark all clients using src_ip to be updated */
rlb_req_update_subnet_clients(struct bonding * bond,__be32 src_ip)520d3bb52b0SAl Viro static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
5231da177e4SLinus Torvalds 	struct rlb_client_info *client_info;
5241da177e4SLinus Torvalds 	u32 hash_index;
5251da177e4SLinus Torvalds 
5264bab16d7SNikolay Aleksandrov 	spin_lock(&bond->mode_lock);
5271da177e4SLinus Torvalds 
528e53665c6SJiri Bohac 	hash_index = bond_info->rx_hashtbl_used_head;
529e53665c6SJiri Bohac 	for (; hash_index != RLB_NULL_INDEX;
530e53665c6SJiri Bohac 	     hash_index = client_info->used_next) {
5311da177e4SLinus Torvalds 		client_info = &(bond_info->rx_hashtbl[hash_index]);
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 		if (!client_info->slave) {
5340a111a03SVeaceslav Falico 			netdev_err(bond->dev, "found a client with no channel in the client's hash table\n");
5351da177e4SLinus Torvalds 			continue;
5361da177e4SLinus Torvalds 		}
5371da177e4SLinus Torvalds 		/* update all clients using this src_ip, that are not assigned
5381da177e4SLinus Torvalds 		 * to the team's address (curr_active_slave) and have a known
5391da177e4SLinus Torvalds 		 * unicast mac address.
5401da177e4SLinus Torvalds 		 */
5411da177e4SLinus Torvalds 		if ((client_info->ip_src == src_ip) &&
542a6700db1SJoe Perches 		    !ether_addr_equal_64bits(client_info->slave->dev->dev_addr,
543885a136cSEric Dumazet 					     bond->dev->dev_addr) &&
544cbeeea70SDebabrata Banerjee 		    is_valid_ether_addr(client_info->mac_dst)) {
5451da177e4SLinus Torvalds 			client_info->ntt = 1;
5461da177e4SLinus Torvalds 			bond_info->rx_ntt = 1;
5471da177e4SLinus Torvalds 		}
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5504bab16d7SNikolay Aleksandrov 	spin_unlock(&bond->mode_lock);
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
rlb_choose_channel(struct sk_buff * skb,struct bonding * bond,const struct arp_pkt * arp)553b7469e83SEric Dumazet static struct slave *rlb_choose_channel(struct sk_buff *skb,
554b7469e83SEric Dumazet 					struct bonding *bond,
555b7469e83SEric Dumazet 					const struct arp_pkt *arp)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
55828c71926Sdingtianhong 	struct slave *assigned_slave, *curr_active_slave;
5591da177e4SLinus Torvalds 	struct rlb_client_info *client_info;
5601da177e4SLinus Torvalds 	u32 hash_index = 0;
5611da177e4SLinus Torvalds 
5624bab16d7SNikolay Aleksandrov 	spin_lock(&bond->mode_lock);
5631da177e4SLinus Torvalds 
56428c71926Sdingtianhong 	curr_active_slave = rcu_dereference(bond->curr_active_slave);
56528c71926Sdingtianhong 
566e364a341SAmerigo Wang 	hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
5671da177e4SLinus Torvalds 	client_info = &(bond_info->rx_hashtbl[hash_index]);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	if (client_info->assigned) {
5701da177e4SLinus Torvalds 		if ((client_info->ip_src == arp->ip_src) &&
5711da177e4SLinus Torvalds 		    (client_info->ip_dst == arp->ip_dst)) {
5721da177e4SLinus Torvalds 			/* the entry is already assigned to this client */
573cbeeea70SDebabrata Banerjee 			if (!is_broadcast_ether_addr(arp->mac_dst)) {
5741da177e4SLinus Torvalds 				/* update mac address from arp */
575ada0f863SJoe Perches 				ether_addr_copy(client_info->mac_dst, arp->mac_dst);
5761da177e4SLinus Torvalds 			}
577ada0f863SJoe Perches 			ether_addr_copy(client_info->mac_src, arp->mac_src);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 			assigned_slave = client_info->slave;
5801da177e4SLinus Torvalds 			if (assigned_slave) {
5814bab16d7SNikolay Aleksandrov 				spin_unlock(&bond->mode_lock);
5821da177e4SLinus Torvalds 				return assigned_slave;
5831da177e4SLinus Torvalds 			}
5841da177e4SLinus Torvalds 		} else {
5851da177e4SLinus Torvalds 			/* the entry is already assigned to some other client,
5861da177e4SLinus Torvalds 			 * move the old client to primary (curr_active_slave) so
5871da177e4SLinus Torvalds 			 * that the new client can be assigned to this entry.
5881da177e4SLinus Torvalds 			 */
5894740d638SEric Dumazet 			if (curr_active_slave &&
59028c71926Sdingtianhong 			    client_info->slave != curr_active_slave) {
59128c71926Sdingtianhong 				client_info->slave = curr_active_slave;
5921da177e4SLinus Torvalds 				rlb_update_client(client_info);
5931da177e4SLinus Torvalds 			}
5941da177e4SLinus Torvalds 		}
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 	/* assign a new slave */
59728c71926Sdingtianhong 	assigned_slave = __rlb_next_rx_slave(bond);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	if (assigned_slave) {
600e53665c6SJiri Bohac 		if (!(client_info->assigned &&
601e53665c6SJiri Bohac 		      client_info->ip_src == arp->ip_src)) {
602e53665c6SJiri Bohac 			/* ip_src is going to be updated,
603e53665c6SJiri Bohac 			 * fix the src hash list
604e53665c6SJiri Bohac 			 */
605e53665c6SJiri Bohac 			u32 hash_src = _simple_hash((u8 *)&arp->ip_src,
606e53665c6SJiri Bohac 						    sizeof(arp->ip_src));
607e53665c6SJiri Bohac 			rlb_src_unlink(bond, hash_index);
608e53665c6SJiri Bohac 			rlb_src_link(bond, hash_src, hash_index);
609e53665c6SJiri Bohac 		}
610e53665c6SJiri Bohac 
6111da177e4SLinus Torvalds 		client_info->ip_src = arp->ip_src;
6121da177e4SLinus Torvalds 		client_info->ip_dst = arp->ip_dst;
6134057c58dSWang Hai 		/* arp->mac_dst is broadcast for arp requests.
6141da177e4SLinus Torvalds 		 * will be updated with clients actual unicast mac address
6151da177e4SLinus Torvalds 		 * upon receiving an arp reply.
6161da177e4SLinus Torvalds 		 */
617ada0f863SJoe Perches 		ether_addr_copy(client_info->mac_dst, arp->mac_dst);
618ada0f863SJoe Perches 		ether_addr_copy(client_info->mac_src, arp->mac_src);
6191da177e4SLinus Torvalds 		client_info->slave = assigned_slave;
6201da177e4SLinus Torvalds 
621cbeeea70SDebabrata Banerjee 		if (is_valid_ether_addr(client_info->mac_dst)) {
6221da177e4SLinus Torvalds 			client_info->ntt = 1;
6231da177e4SLinus Torvalds 			bond->alb_info.rx_ntt = 1;
6241da177e4SLinus Torvalds 		} else {
6251da177e4SLinus Torvalds 			client_info->ntt = 0;
6261da177e4SLinus Torvalds 		}
6271da177e4SLinus Torvalds 
628fb00bc2eSdingtianhong 		if (vlan_get_tag(skb, &client_info->vlan_id))
629d3ab3ffdSVeaceslav Falico 			client_info->vlan_id = 0;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 		if (!client_info->assigned) {
632e53665c6SJiri Bohac 			u32 prev_tbl_head = bond_info->rx_hashtbl_used_head;
63386a5ad0aSYufeng Mo 
634e53665c6SJiri Bohac 			bond_info->rx_hashtbl_used_head = hash_index;
635e53665c6SJiri Bohac 			client_info->used_next = prev_tbl_head;
6361da177e4SLinus Torvalds 			if (prev_tbl_head != RLB_NULL_INDEX) {
637e53665c6SJiri Bohac 				bond_info->rx_hashtbl[prev_tbl_head].used_prev =
6381da177e4SLinus Torvalds 					hash_index;
6391da177e4SLinus Torvalds 			}
6401da177e4SLinus Torvalds 			client_info->assigned = 1;
6411da177e4SLinus Torvalds 		}
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 
6444bab16d7SNikolay Aleksandrov 	spin_unlock(&bond->mode_lock);
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	return assigned_slave;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds /* chooses (and returns) transmit channel for arp reply
6501da177e4SLinus Torvalds  * does not choose channel for other arp types since they are
6511da177e4SLinus Torvalds  * sent on the curr_active_slave
6521da177e4SLinus Torvalds  */
rlb_arp_xmit(struct sk_buff * skb,struct bonding * bond)6531da177e4SLinus Torvalds static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
6541da177e4SLinus Torvalds {
6551da177e4SLinus Torvalds 	struct slave *tx_slave = NULL;
656d5410ac7SSun Shouxin 	struct net_device *dev;
657b7469e83SEric Dumazet 	struct arp_pkt *arp;
658b7469e83SEric Dumazet 
659b7469e83SEric Dumazet 	if (!pskb_network_may_pull(skb, sizeof(*arp)))
660b7469e83SEric Dumazet 		return NULL;
661b7469e83SEric Dumazet 	arp = (struct arp_pkt *)skb_network_header(skb);
6621da177e4SLinus Torvalds 
663e74216b8SHangbin Liu 	/* Don't modify or load balance ARPs that do not originate
664e74216b8SHangbin Liu 	 * from the bond itself or a VLAN directly above the bond.
665567b871eSzheng.li 	 */
666e74216b8SHangbin Liu 	if (!bond_slave_has_mac_rcu(bond, arp->mac_src))
667567b871eSzheng.li 		return NULL;
668567b871eSzheng.li 
669d5410ac7SSun Shouxin 	dev = ip_dev_find(dev_net(bond->dev), arp->ip_src);
670d5410ac7SSun Shouxin 	if (dev) {
671f11e5bd1SMateusz Kowalski 		if (netif_is_any_bridge_master(dev)) {
6724f5d33f4SJay Vosburgh 			dev_put(dev);
673d5410ac7SSun Shouxin 			return NULL;
674d5410ac7SSun Shouxin 		}
6754f5d33f4SJay Vosburgh 		dev_put(dev);
6764f5d33f4SJay Vosburgh 	}
677d5410ac7SSun Shouxin 
678f14c4e4eSBrian Haley 	if (arp->op_code == htons(ARPOP_REPLY)) {
679547942caSNikolay Aleksandrov 		/* the arp must be sent on the selected rx channel */
680b7469e83SEric Dumazet 		tx_slave = rlb_choose_channel(skb, bond, arp);
68135d75ee4SWang Yufen 		if (tx_slave)
682faeeb317SJarod Wilson 			bond_hw_addr_copy(arp->mac_src, tx_slave->dev->dev_addr,
683faeeb317SJarod Wilson 					  tx_slave->dev->addr_len);
6847ea2e423SJarod Wilson 		netdev_dbg(bond->dev, "(slave %s): Server sent ARP Reply packet\n",
6857ea2e423SJarod Wilson 			   tx_slave ? tx_slave->dev->name : "NULL");
686f14c4e4eSBrian Haley 	} else if (arp->op_code == htons(ARPOP_REQUEST)) {
6871da177e4SLinus Torvalds 		/* Create an entry in the rx_hashtbl for this client as a
6881da177e4SLinus Torvalds 		 * place holder.
6891da177e4SLinus Torvalds 		 * When the arp reply is received the entry will be updated
6901da177e4SLinus Torvalds 		 * with the correct unicast address of the client.
6911da177e4SLinus Torvalds 		 */
692b7469e83SEric Dumazet 		tx_slave = rlb_choose_channel(skb, bond, arp);
6931da177e4SLinus Torvalds 
69477c8e2c0SPeter Pan(潘卫平) 		/* The ARP reply packets must be delayed so that
6951da177e4SLinus Torvalds 		 * they can cancel out the influence of the ARP request.
6961da177e4SLinus Torvalds 		 */
6971da177e4SLinus Torvalds 		bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 		/* arp requests are broadcast and are sent on the primary
7001da177e4SLinus Torvalds 		 * the arp request will collapse all clients on the subnet to
7011da177e4SLinus Torvalds 		 * the primary slave. We must register these clients to be
7021da177e4SLinus Torvalds 		 * updated with their assigned mac.
7031da177e4SLinus Torvalds 		 */
7041da177e4SLinus Torvalds 		rlb_req_update_subnet_clients(bond, arp->ip_src);
7057ea2e423SJarod Wilson 		netdev_dbg(bond->dev, "(slave %s): Server sent ARP Request packet\n",
7067ea2e423SJarod Wilson 			   tx_slave ? tx_slave->dev->name : "NULL");
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	return tx_slave;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds 
rlb_rebalance(struct bonding * bond)7121da177e4SLinus Torvalds static void rlb_rebalance(struct bonding *bond)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
7151da177e4SLinus Torvalds 	struct slave *assigned_slave;
7161da177e4SLinus Torvalds 	struct rlb_client_info *client_info;
7171da177e4SLinus Torvalds 	int ntt;
7181da177e4SLinus Torvalds 	u32 hash_index;
7191da177e4SLinus Torvalds 
7204bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	ntt = 0;
723e53665c6SJiri Bohac 	hash_index = bond_info->rx_hashtbl_used_head;
724e53665c6SJiri Bohac 	for (; hash_index != RLB_NULL_INDEX;
725e53665c6SJiri Bohac 	     hash_index = client_info->used_next) {
7261da177e4SLinus Torvalds 		client_info = &(bond_info->rx_hashtbl[hash_index]);
727733ab639Sdingtianhong 		assigned_slave = __rlb_next_rx_slave(bond);
7281da177e4SLinus Torvalds 		if (assigned_slave && (client_info->slave != assigned_slave)) {
7291da177e4SLinus Torvalds 			client_info->slave = assigned_slave;
73025780410SDebabrata Banerjee 			if (!is_zero_ether_addr(client_info->mac_dst)) {
7311da177e4SLinus Torvalds 				client_info->ntt = 1;
7321da177e4SLinus Torvalds 				ntt = 1;
7331da177e4SLinus Torvalds 			}
7341da177e4SLinus Torvalds 		}
73525780410SDebabrata Banerjee 	}
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	/* update the team's flag only after the whole iteration */
73835d75ee4SWang Yufen 	if (ntt)
7391da177e4SLinus Torvalds 		bond_info->rx_ntt = 1;
7404bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
743547942caSNikolay Aleksandrov /* Caller must hold mode_lock */
rlb_init_table_entry_dst(struct rlb_client_info * entry)744e53665c6SJiri Bohac static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
745e53665c6SJiri Bohac {
746e53665c6SJiri Bohac 	entry->used_next = RLB_NULL_INDEX;
747e53665c6SJiri Bohac 	entry->used_prev = RLB_NULL_INDEX;
748e53665c6SJiri Bohac 	entry->assigned = 0;
749e53665c6SJiri Bohac 	entry->slave = NULL;
750d3ab3ffdSVeaceslav Falico 	entry->vlan_id = 0;
751e53665c6SJiri Bohac }
rlb_init_table_entry_src(struct rlb_client_info * entry)752e53665c6SJiri Bohac static void rlb_init_table_entry_src(struct rlb_client_info *entry)
753e53665c6SJiri Bohac {
754e53665c6SJiri Bohac 	entry->src_first = RLB_NULL_INDEX;
755e53665c6SJiri Bohac 	entry->src_prev = RLB_NULL_INDEX;
756e53665c6SJiri Bohac 	entry->src_next = RLB_NULL_INDEX;
757e53665c6SJiri Bohac }
758e53665c6SJiri Bohac 
rlb_init_table_entry(struct rlb_client_info * entry)7591da177e4SLinus Torvalds static void rlb_init_table_entry(struct rlb_client_info *entry)
7601da177e4SLinus Torvalds {
7611da177e4SLinus Torvalds 	memset(entry, 0, sizeof(struct rlb_client_info));
762e53665c6SJiri Bohac 	rlb_init_table_entry_dst(entry);
763e53665c6SJiri Bohac 	rlb_init_table_entry_src(entry);
764e53665c6SJiri Bohac }
765e53665c6SJiri Bohac 
rlb_delete_table_entry_dst(struct bonding * bond,u32 index)766e53665c6SJiri Bohac static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
767e53665c6SJiri Bohac {
768e53665c6SJiri Bohac 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
769e53665c6SJiri Bohac 	u32 next_index = bond_info->rx_hashtbl[index].used_next;
770e53665c6SJiri Bohac 	u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
771e53665c6SJiri Bohac 
772e53665c6SJiri Bohac 	if (index == bond_info->rx_hashtbl_used_head)
773e53665c6SJiri Bohac 		bond_info->rx_hashtbl_used_head = next_index;
774e53665c6SJiri Bohac 	if (prev_index != RLB_NULL_INDEX)
775e53665c6SJiri Bohac 		bond_info->rx_hashtbl[prev_index].used_next = next_index;
776e53665c6SJiri Bohac 	if (next_index != RLB_NULL_INDEX)
777e53665c6SJiri Bohac 		bond_info->rx_hashtbl[next_index].used_prev = prev_index;
778e53665c6SJiri Bohac }
779e53665c6SJiri Bohac 
780e53665c6SJiri Bohac /* unlink a rlb hash table entry from the src list */
rlb_src_unlink(struct bonding * bond,u32 index)781e53665c6SJiri Bohac static void rlb_src_unlink(struct bonding *bond, u32 index)
782e53665c6SJiri Bohac {
783e53665c6SJiri Bohac 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
784e53665c6SJiri Bohac 	u32 next_index = bond_info->rx_hashtbl[index].src_next;
785e53665c6SJiri Bohac 	u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
786e53665c6SJiri Bohac 
787e53665c6SJiri Bohac 	bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
788e53665c6SJiri Bohac 	bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
789e53665c6SJiri Bohac 
790e53665c6SJiri Bohac 	if (next_index != RLB_NULL_INDEX)
791e53665c6SJiri Bohac 		bond_info->rx_hashtbl[next_index].src_prev = prev_index;
792e53665c6SJiri Bohac 
793e53665c6SJiri Bohac 	if (prev_index == RLB_NULL_INDEX)
794e53665c6SJiri Bohac 		return;
795e53665c6SJiri Bohac 
796e53665c6SJiri Bohac 	/* is prev_index pointing to the head of this list? */
797e53665c6SJiri Bohac 	if (bond_info->rx_hashtbl[prev_index].src_first == index)
798e53665c6SJiri Bohac 		bond_info->rx_hashtbl[prev_index].src_first = next_index;
799e53665c6SJiri Bohac 	else
800e53665c6SJiri Bohac 		bond_info->rx_hashtbl[prev_index].src_next = next_index;
801e53665c6SJiri Bohac 
802e53665c6SJiri Bohac }
803e53665c6SJiri Bohac 
rlb_delete_table_entry(struct bonding * bond,u32 index)804e53665c6SJiri Bohac static void rlb_delete_table_entry(struct bonding *bond, u32 index)
805e53665c6SJiri Bohac {
806e53665c6SJiri Bohac 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
807e53665c6SJiri Bohac 	struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
808e53665c6SJiri Bohac 
809e53665c6SJiri Bohac 	rlb_delete_table_entry_dst(bond, index);
810e53665c6SJiri Bohac 	rlb_init_table_entry_dst(entry);
811e53665c6SJiri Bohac 
812e53665c6SJiri Bohac 	rlb_src_unlink(bond, index);
813e53665c6SJiri Bohac }
814e53665c6SJiri Bohac 
815e53665c6SJiri Bohac /* add the rx_hashtbl[ip_dst_hash] entry to the list
816e53665c6SJiri Bohac  * of entries with identical ip_src_hash
817e53665c6SJiri Bohac  */
rlb_src_link(struct bonding * bond,u32 ip_src_hash,u32 ip_dst_hash)818e53665c6SJiri Bohac static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
819e53665c6SJiri Bohac {
820e53665c6SJiri Bohac 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
821e53665c6SJiri Bohac 	u32 next;
822e53665c6SJiri Bohac 
823e53665c6SJiri Bohac 	bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
824e53665c6SJiri Bohac 	next = bond_info->rx_hashtbl[ip_src_hash].src_first;
825e53665c6SJiri Bohac 	bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
826e53665c6SJiri Bohac 	if (next != RLB_NULL_INDEX)
827e53665c6SJiri Bohac 		bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
828e53665c6SJiri Bohac 	bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
829e53665c6SJiri Bohac }
830e53665c6SJiri Bohac 
831e53665c6SJiri Bohac /* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
832547942caSNikolay Aleksandrov  * not match arp->mac_src
833547942caSNikolay Aleksandrov  */
rlb_purge_src_ip(struct bonding * bond,struct arp_pkt * arp)834e53665c6SJiri Bohac static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
835e53665c6SJiri Bohac {
836e53665c6SJiri Bohac 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
837e53665c6SJiri Bohac 	u32 ip_src_hash = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
838e53665c6SJiri Bohac 	u32 index;
839e53665c6SJiri Bohac 
8404bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
841e53665c6SJiri Bohac 
842e53665c6SJiri Bohac 	index = bond_info->rx_hashtbl[ip_src_hash].src_first;
843e53665c6SJiri Bohac 	while (index != RLB_NULL_INDEX) {
844e53665c6SJiri Bohac 		struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
845e53665c6SJiri Bohac 		u32 next_index = entry->src_next;
84686a5ad0aSYufeng Mo 
847e53665c6SJiri Bohac 		if (entry->ip_src == arp->ip_src &&
848e53665c6SJiri Bohac 		    !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
849e53665c6SJiri Bohac 			rlb_delete_table_entry(bond, index);
850e53665c6SJiri Bohac 		index = next_index;
851e53665c6SJiri Bohac 	}
8524bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
rlb_initialize(struct bonding * bond)8551da177e4SLinus Torvalds static int rlb_initialize(struct bonding *bond)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
8580d206a3aSMitch Williams 	struct rlb_client_info	*new_hashtbl;
8591da177e4SLinus Torvalds 	int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
8601da177e4SLinus Torvalds 	int i;
8611da177e4SLinus Torvalds 
8620d206a3aSMitch Williams 	new_hashtbl = kmalloc(size, GFP_KERNEL);
863e404decbSJoe Perches 	if (!new_hashtbl)
8641da177e4SLinus Torvalds 		return -1;
865e404decbSJoe Perches 
8664bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
8670d206a3aSMitch Williams 
8680d206a3aSMitch Williams 	bond_info->rx_hashtbl = new_hashtbl;
8691da177e4SLinus Torvalds 
870e53665c6SJiri Bohac 	bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
8711da177e4SLinus Torvalds 
87235d75ee4SWang Yufen 	for (i = 0; i < RLB_HASH_TABLE_SIZE; i++)
8731da177e4SLinus Torvalds 		rlb_init_table_entry(bond_info->rx_hashtbl + i);
8741da177e4SLinus Torvalds 
8754bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	/* register to receive ARPs */
8783aba891dSJiri Pirko 	bond->recv_probe = rlb_arp_recv;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	return 0;
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
rlb_deinitialize(struct bonding * bond)8831da177e4SLinus Torvalds static void rlb_deinitialize(struct bonding *bond)
8841da177e4SLinus Torvalds {
8851da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
8861da177e4SLinus Torvalds 
8874bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	kfree(bond_info->rx_hashtbl);
8901da177e4SLinus Torvalds 	bond_info->rx_hashtbl = NULL;
891e53665c6SJiri Bohac 	bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
8921da177e4SLinus Torvalds 
8934bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
rlb_clear_vlan(struct bonding * bond,unsigned short vlan_id)8961da177e4SLinus Torvalds static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
8971da177e4SLinus Torvalds {
8981da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
8991da177e4SLinus Torvalds 	u32 curr_index;
9001da177e4SLinus Torvalds 
9014bab16d7SNikolay Aleksandrov 	spin_lock_bh(&bond->mode_lock);
9021da177e4SLinus Torvalds 
903e53665c6SJiri Bohac 	curr_index = bond_info->rx_hashtbl_used_head;
9041da177e4SLinus Torvalds 	while (curr_index != RLB_NULL_INDEX) {
9051da177e4SLinus Torvalds 		struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
906e53665c6SJiri Bohac 		u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
9071da177e4SLinus Torvalds 
908d3ab3ffdSVeaceslav Falico 		if (curr->vlan_id == vlan_id)
909e53665c6SJiri Bohac 			rlb_delete_table_entry(bond, curr_index);
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 		curr_index = next_index;
9121da177e4SLinus Torvalds 	}
9131da177e4SLinus Torvalds 
9144bab16d7SNikolay Aleksandrov 	spin_unlock_bh(&bond->mode_lock);
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds /*********************** tlb/rlb shared functions *********************/
9181da177e4SLinus Torvalds 
alb_send_lp_vid(struct slave * slave,const u8 mac_addr[],__be16 vlan_proto,u16 vid)9196f238100SJakub Kicinski static void alb_send_lp_vid(struct slave *slave, const u8 mac_addr[],
920d6b694c0SVlad Yasevich 			    __be16 vlan_proto, u16 vid)
9211da177e4SLinus Torvalds {
9221da177e4SLinus Torvalds 	struct learning_pkt pkt;
9237aa64981SVeaceslav Falico 	struct sk_buff *skb;
9241da177e4SLinus Torvalds 	int size = sizeof(struct learning_pkt);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	memset(&pkt, 0, size);
927ada0f863SJoe Perches 	ether_addr_copy(pkt.mac_dst, mac_addr);
928ada0f863SJoe Perches 	ether_addr_copy(pkt.mac_src, mac_addr);
92996a0922cSVeaceslav Falico 	pkt.type = cpu_to_be16(ETH_P_LOOPBACK);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	skb = dev_alloc_skb(size);
9327aa64981SVeaceslav Falico 	if (!skb)
9331da177e4SLinus Torvalds 		return;
9341da177e4SLinus Torvalds 
935b952f4dfSyuan linyu 	skb_put_data(skb, &pkt, size);
9361da177e4SLinus Torvalds 
937459a98edSArnaldo Carvalho de Melo 	skb_reset_mac_header(skb);
938b0e380b1SArnaldo Carvalho de Melo 	skb->network_header = skb->mac_header + ETH_HLEN;
9391da177e4SLinus Torvalds 	skb->protocol = pkt.type;
9401da177e4SLinus Torvalds 	skb->priority = TC_PRIO_CONTROL;
9411da177e4SLinus Torvalds 	skb->dev = slave->dev;
9421da177e4SLinus Torvalds 
9437ea2e423SJarod Wilson 	slave_dbg(slave->bond->dev, slave->dev,
9447ea2e423SJarod Wilson 		  "Send learning packet: mac %pM vlan %d\n", mac_addr, vid);
94521706ee8SDebabrata Banerjee 
946b4bef1b5SJiri Pirko 	if (vid)
947b4bef1b5SJiri Pirko 		__vlan_hwaccel_put_tag(skb, vlan_proto, vid);
9487aa64981SVeaceslav Falico 
9497aa64981SVeaceslav Falico 	dev_queue_xmit(skb);
9507aa64981SVeaceslav Falico }
9517aa64981SVeaceslav Falico 
952b3208b20SDavid Ahern struct alb_walk_data {
953b3208b20SDavid Ahern 	struct bonding *bond;
954b3208b20SDavid Ahern 	struct slave *slave;
9556f238100SJakub Kicinski 	const u8 *mac_addr;
956b3208b20SDavid Ahern 	bool strict_match;
957b3208b20SDavid Ahern };
958b3208b20SDavid Ahern 
alb_upper_dev_walk(struct net_device * upper,struct netdev_nested_priv * priv)959eff74233STaehee Yoo static int alb_upper_dev_walk(struct net_device *upper,
960eff74233STaehee Yoo 			      struct netdev_nested_priv *priv)
9617aa64981SVeaceslav Falico {
962eff74233STaehee Yoo 	struct alb_walk_data *data = (struct alb_walk_data *)priv->data;
963b3208b20SDavid Ahern 	bool strict_match = data->strict_match;
9646f238100SJakub Kicinski 	const u8 *mac_addr = data->mac_addr;
965b3208b20SDavid Ahern 	struct bonding *bond = data->bond;
966b3208b20SDavid Ahern 	struct slave *slave = data->slave;
9673e403a77SVeaceslav Falico 	struct bond_vlan_tag *tags;
9687aa64981SVeaceslav Falico 
96921706ee8SDebabrata Banerjee 	if (is_vlan_dev(upper) &&
970f3b0a18bSTaehee Yoo 	    bond->dev->lower_level == upper->lower_level - 1) {
97121706ee8SDebabrata Banerjee 		if (upper->addr_assign_type == NET_ADDR_STOLEN) {
9725bf94b83SVeaceslav Falico 			alb_send_lp_vid(slave, mac_addr,
973d6b694c0SVlad Yasevich 					vlan_dev_vlan_proto(upper),
9745bf94b83SVeaceslav Falico 					vlan_dev_vlan_id(upper));
97521706ee8SDebabrata Banerjee 		} else {
976d0c21d43SVlad Yasevich 			alb_send_lp_vid(slave, upper->dev_addr,
977d0c21d43SVlad Yasevich 					vlan_dev_vlan_proto(upper),
978d0c21d43SVlad Yasevich 					vlan_dev_vlan_id(upper));
979d0c21d43SVlad Yasevich 		}
980d0c21d43SVlad Yasevich 	}
98114af9963SVlad Yasevich 
98214af9963SVlad Yasevich 	/* If this is a macvlan device, then only send updates
98314af9963SVlad Yasevich 	 * when strict_match is turned off.
98414af9963SVlad Yasevich 	 */
98514af9963SVlad Yasevich 	if (netif_is_macvlan(upper) && !strict_match) {
9863e403a77SVeaceslav Falico 		tags = bond_verify_device_path(bond->dev, upper, 0);
9873e403a77SVeaceslav Falico 		if (IS_ERR_OR_NULL(tags))
988*a5bea3aeSZhengchao Shao 			return -ENOMEM;
989*a5bea3aeSZhengchao Shao 
99014af9963SVlad Yasevich 		alb_send_lp_vid(slave, upper->dev_addr,
99114af9963SVlad Yasevich 				tags[0].vlan_proto, tags[0].vlan_id);
9923e403a77SVeaceslav Falico 		kfree(tags);
99314af9963SVlad Yasevich 	}
994b3208b20SDavid Ahern 
995b3208b20SDavid Ahern 	return 0;
9961da177e4SLinus Torvalds }
997b3208b20SDavid Ahern 
alb_send_learning_packets(struct slave * slave,const u8 mac_addr[],bool strict_match)9986f238100SJakub Kicinski static void alb_send_learning_packets(struct slave *slave, const u8 mac_addr[],
999b3208b20SDavid Ahern 				      bool strict_match)
1000b3208b20SDavid Ahern {
1001b3208b20SDavid Ahern 	struct bonding *bond = bond_get_bond_by_slave(slave);
1002eff74233STaehee Yoo 	struct netdev_nested_priv priv;
1003b3208b20SDavid Ahern 	struct alb_walk_data data = {
1004b3208b20SDavid Ahern 		.strict_match = strict_match,
1005b3208b20SDavid Ahern 		.mac_addr = mac_addr,
1006b3208b20SDavid Ahern 		.slave = slave,
1007b3208b20SDavid Ahern 		.bond = bond,
1008b3208b20SDavid Ahern 	};
1009b3208b20SDavid Ahern 
1010eff74233STaehee Yoo 	priv.data = (void *)&data;
1011b3208b20SDavid Ahern 	/* send untagged */
1012b3208b20SDavid Ahern 	alb_send_lp_vid(slave, mac_addr, 0, 0);
1013b3208b20SDavid Ahern 
1014b3208b20SDavid Ahern 	/* loop through all devices and see if we need to send a packet
1015b3208b20SDavid Ahern 	 * for that device.
1016b3208b20SDavid Ahern 	 */
1017b3208b20SDavid Ahern 	rcu_read_lock();
1018eff74233STaehee Yoo 	netdev_walk_all_upper_dev_rcu(bond->dev, alb_upper_dev_walk, &priv);
10195bf94b83SVeaceslav Falico 	rcu_read_unlock();
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
alb_set_slave_mac_addr(struct slave * slave,const u8 addr[],unsigned int len)10226f238100SJakub Kicinski static int alb_set_slave_mac_addr(struct slave *slave, const u8 addr[],
1023faeeb317SJarod Wilson 				  unsigned int len)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	struct net_device *dev = slave->dev;
1026faeeb317SJarod Wilson 	struct sockaddr_storage ss;
10271da177e4SLinus Torvalds 
102801844098SVeaceslav Falico 	if (BOND_MODE(slave->bond) == BOND_MODE_TLB) {
10296f238100SJakub Kicinski 		__dev_addr_set(dev, addr, len);
10301da177e4SLinus Torvalds 		return 0;
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 
1033547942caSNikolay Aleksandrov 	/* for rlb each slave must have a unique hw mac addresses so that
1034547942caSNikolay Aleksandrov 	 * each slave will receive packets destined to a different mac
1035547942caSNikolay Aleksandrov 	 */
1036faeeb317SJarod Wilson 	memcpy(ss.__data, addr, len);
1037faeeb317SJarod Wilson 	ss.ss_family = dev->type;
10383a37a963SPetr Machata 	if (dev_set_mac_address(dev, (struct sockaddr *)&ss, NULL)) {
10397ea2e423SJarod Wilson 		slave_err(slave->bond->dev, dev, "dev_set_mac_address on slave failed! ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n");
10401da177e4SLinus Torvalds 		return -EOPNOTSUPP;
10411da177e4SLinus Torvalds 	}
10421da177e4SLinus Torvalds 	return 0;
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
1045547942caSNikolay Aleksandrov /* Swap MAC addresses between two slaves.
1046059fe7a5SJay Vosburgh  *
1047059fe7a5SJay Vosburgh  * Called with RTNL held, and no other locks.
1048059fe7a5SJay Vosburgh  */
alb_swap_mac_addr(struct slave * slave1,struct slave * slave2)104943547ea6SVeaceslav Falico static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
10501da177e4SLinus Torvalds {
1051faeeb317SJarod Wilson 	u8 tmp_mac_addr[MAX_ADDR_LEN];
10521da177e4SLinus Torvalds 
1053faeeb317SJarod Wilson 	bond_hw_addr_copy(tmp_mac_addr, slave1->dev->dev_addr,
1054faeeb317SJarod Wilson 			  slave1->dev->addr_len);
1055faeeb317SJarod Wilson 	alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr,
1056faeeb317SJarod Wilson 			       slave2->dev->addr_len);
1057faeeb317SJarod Wilson 	alb_set_slave_mac_addr(slave2, tmp_mac_addr,
1058faeeb317SJarod Wilson 			       slave1->dev->addr_len);
10591da177e4SLinus Torvalds 
1060059fe7a5SJay Vosburgh }
1061059fe7a5SJay Vosburgh 
1062547942caSNikolay Aleksandrov /* Send learning packets after MAC address swap.
1063059fe7a5SJay Vosburgh  *
10642543331dSJay Vosburgh  * Called with RTNL and no other locks
1065059fe7a5SJay Vosburgh  */
alb_fasten_mac_swap(struct bonding * bond,struct slave * slave1,struct slave * slave2)1066059fe7a5SJay Vosburgh static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
1067059fe7a5SJay Vosburgh 				struct slave *slave2)
1068059fe7a5SJay Vosburgh {
10698557cd74SVeaceslav Falico 	int slaves_state_differ = (bond_slave_can_tx(slave1) != bond_slave_can_tx(slave2));
1070059fe7a5SJay Vosburgh 	struct slave *disabled_slave = NULL;
1071059fe7a5SJay Vosburgh 
10722543331dSJay Vosburgh 	ASSERT_RTNL();
10732543331dSJay Vosburgh 
10741da177e4SLinus Torvalds 	/* fasten the change in the switch */
10758557cd74SVeaceslav Falico 	if (bond_slave_can_tx(slave1)) {
1076d0c21d43SVlad Yasevich 		alb_send_learning_packets(slave1, slave1->dev->dev_addr, false);
10771da177e4SLinus Torvalds 		if (bond->alb_info.rlb_enabled) {
10781da177e4SLinus Torvalds 			/* inform the clients that the mac address
10791da177e4SLinus Torvalds 			 * has changed
10801da177e4SLinus Torvalds 			 */
10811da177e4SLinus Torvalds 			rlb_req_update_slave_clients(bond, slave1);
10821da177e4SLinus Torvalds 		}
10831da177e4SLinus Torvalds 	} else {
10841da177e4SLinus Torvalds 		disabled_slave = slave1;
10851da177e4SLinus Torvalds 	}
10861da177e4SLinus Torvalds 
10878557cd74SVeaceslav Falico 	if (bond_slave_can_tx(slave2)) {
1088d0c21d43SVlad Yasevich 		alb_send_learning_packets(slave2, slave2->dev->dev_addr, false);
10891da177e4SLinus Torvalds 		if (bond->alb_info.rlb_enabled) {
10901da177e4SLinus Torvalds 			/* inform the clients that the mac address
10911da177e4SLinus Torvalds 			 * has changed
10921da177e4SLinus Torvalds 			 */
10931da177e4SLinus Torvalds 			rlb_req_update_slave_clients(bond, slave2);
10941da177e4SLinus Torvalds 		}
10951da177e4SLinus Torvalds 	} else {
10961da177e4SLinus Torvalds 		disabled_slave = slave2;
10971da177e4SLinus Torvalds 	}
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	if (bond->alb_info.rlb_enabled && slaves_state_differ) {
11001da177e4SLinus Torvalds 		/* A disabled slave was assigned an active mac addr */
11011da177e4SLinus Torvalds 		rlb_teach_disabled_mac_on_primary(bond,
11021da177e4SLinus Torvalds 						  disabled_slave->dev->dev_addr);
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds /**
11071da177e4SLinus Torvalds  * alb_change_hw_addr_on_detach
11081da177e4SLinus Torvalds  * @bond: bonding we're working on
11091da177e4SLinus Torvalds  * @slave: the slave that was just detached
11101da177e4SLinus Torvalds  *
11111da177e4SLinus Torvalds  * We assume that @slave was already detached from the slave list.
11121da177e4SLinus Torvalds  *
11131da177e4SLinus Torvalds  * If @slave's permanent hw address is different both from its current
11141da177e4SLinus Torvalds  * address and from @bond's address, then somewhere in the bond there's
11151da177e4SLinus Torvalds  * a slave that has @slave's permanet address as its current address.
1116252b5d37SPeng Li  * We'll make sure that slave no longer uses @slave's permanent address.
11171da177e4SLinus Torvalds  *
11182543331dSJay Vosburgh  * Caller must hold RTNL and no other locks
11191da177e4SLinus Torvalds  */
alb_change_hw_addr_on_detach(struct bonding * bond,struct slave * slave)11201da177e4SLinus Torvalds static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
11211da177e4SLinus Torvalds {
11221da177e4SLinus Torvalds 	int perm_curr_diff;
11231da177e4SLinus Torvalds 	int perm_bond_diff;
1124b88ec38dSVeaceslav Falico 	struct slave *found_slave;
11251da177e4SLinus Torvalds 
1126a6700db1SJoe Perches 	perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1127885a136cSEric Dumazet 						  slave->dev->dev_addr);
1128a6700db1SJoe Perches 	perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1129885a136cSEric Dumazet 						  bond->dev->dev_addr);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	if (perm_curr_diff && perm_bond_diff) {
1132b88ec38dSVeaceslav Falico 		found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
11331da177e4SLinus Torvalds 
1134b88ec38dSVeaceslav Falico 		if (found_slave) {
1135b88ec38dSVeaceslav Falico 			alb_swap_mac_addr(slave, found_slave);
1136b88ec38dSVeaceslav Falico 			alb_fasten_mac_swap(bond, slave, found_slave);
11371da177e4SLinus Torvalds 		}
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds /**
11421da177e4SLinus Torvalds  * alb_handle_addr_collision_on_attach
11431da177e4SLinus Torvalds  * @bond: bonding we're working on
11441da177e4SLinus Torvalds  * @slave: the slave that was just attached
11451da177e4SLinus Torvalds  *
11461da177e4SLinus Torvalds  * checks uniqueness of slave's mac address and handles the case the
11471da177e4SLinus Torvalds  * new slave uses the bonds mac address.
11481da177e4SLinus Torvalds  *
11491da177e4SLinus Torvalds  * If the permanent hw address of @slave is @bond's hw address, we need to
11501da177e4SLinus Torvalds  * find a different hw address to give @slave, that isn't in use by any other
115177c8e2c0SPeter Pan(潘卫平)  * slave in the bond. This address must be, of course, one of the permanent
11521da177e4SLinus Torvalds  * addresses of the other slaves.
11531da177e4SLinus Torvalds  *
11541da177e4SLinus Torvalds  * We go over the slave list, and for each slave there we compare its
11551da177e4SLinus Torvalds  * permanent hw address with the current address of all the other slaves.
11561da177e4SLinus Torvalds  * If no match was found, then we've found a slave with a permanent address
11571da177e4SLinus Torvalds  * that isn't used by any other slave in the bond, so we can assign it to
11581da177e4SLinus Torvalds  * @slave.
11591da177e4SLinus Torvalds  *
11601da177e4SLinus Torvalds  * assumption: this function is called before @slave is attached to the
11611da177e4SLinus Torvalds  *	       bond slave list.
11621da177e4SLinus Torvalds  */
alb_handle_addr_collision_on_attach(struct bonding * bond,struct slave * slave)11631da177e4SLinus Torvalds static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
11641da177e4SLinus Torvalds {
11654740d638SEric Dumazet 	struct slave *has_bond_addr = rcu_access_pointer(bond->curr_active_slave);
11669caff1e7SVeaceslav Falico 	struct slave *tmp_slave1, *free_mac_slave = NULL;
11679caff1e7SVeaceslav Falico 	struct list_head *iter;
11681da177e4SLinus Torvalds 
11690965a1f3SVeaceslav Falico 	if (!bond_has_slaves(bond)) {
11701da177e4SLinus Torvalds 		/* this is the first slave */
11711da177e4SLinus Torvalds 		return 0;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	/* if slave's mac address differs from bond's mac address
11751da177e4SLinus Torvalds 	 * check uniqueness of slave's mac address against the other
11761da177e4SLinus Torvalds 	 * slaves in the bond.
11771da177e4SLinus Torvalds 	 */
1178a6700db1SJoe Perches 	if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1179cedb743fSVeaceslav Falico 		if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
11801da177e4SLinus Torvalds 			return 0;
11816b38aefeSJohn W. Linville 
11826b38aefeSJohn W. Linville 		/* Try setting slave mac to bond address and fall-through
1183547942caSNikolay Aleksandrov 		 * to code handling that situation below...
1184547942caSNikolay Aleksandrov 		 */
1185faeeb317SJarod Wilson 		alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1186faeeb317SJarod Wilson 				       bond->dev->addr_len);
11871da177e4SLinus Torvalds 	}
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	/* The slave's address is equal to the address of the bond.
11901da177e4SLinus Torvalds 	 * Search for a spare address in the bond for this slave.
11911da177e4SLinus Torvalds 	 */
11929caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, tmp_slave1, iter) {
1193cedb743fSVeaceslav Falico 		if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
11941da177e4SLinus Torvalds 			/* no slave has tmp_slave1's perm addr
11951da177e4SLinus Torvalds 			 * as its curr addr
11961da177e4SLinus Torvalds 			 */
11971da177e4SLinus Torvalds 			free_mac_slave = tmp_slave1;
11981da177e4SLinus Torvalds 			break;
11991da177e4SLinus Torvalds 		}
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 		if (!has_bond_addr) {
1202a6700db1SJoe Perches 			if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
1203885a136cSEric Dumazet 						    bond->dev->dev_addr)) {
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 				has_bond_addr = tmp_slave1;
12061da177e4SLinus Torvalds 			}
12071da177e4SLinus Torvalds 		}
12081da177e4SLinus Torvalds 	}
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	if (free_mac_slave) {
1211faeeb317SJarod Wilson 		alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1212faeeb317SJarod Wilson 				       free_mac_slave->dev->addr_len);
12131da177e4SLinus Torvalds 
12147ea2e423SJarod Wilson 		slave_warn(bond->dev, slave->dev, "the slave hw address is in use by the bond; giving it the hw address of %s\n",
12157ea2e423SJarod Wilson 			   free_mac_slave->dev->name);
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	} else if (has_bond_addr) {
12187ea2e423SJarod Wilson 		slave_err(bond->dev, slave->dev, "the slave hw address is in use by the bond; couldn't find a slave with a free hw address to give it (this should not have happened)\n");
12191da177e4SLinus Torvalds 		return -EFAULT;
12201da177e4SLinus Torvalds 	}
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	return 0;
12231da177e4SLinus Torvalds }
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds /**
12261da177e4SLinus Torvalds  * alb_set_mac_address
1227f6e81b89SLee Jones  * @bond: bonding we're working on
1228f6e81b89SLee Jones  * @addr: MAC address to set
12291da177e4SLinus Torvalds  *
12301da177e4SLinus Torvalds  * In TLB mode all slaves are configured to the bond's hw address, but set
12311da177e4SLinus Torvalds  * their dev_addr field to different addresses (based on their permanent hw
12321da177e4SLinus Torvalds  * addresses).
12331da177e4SLinus Torvalds  *
12341da177e4SLinus Torvalds  * For each slave, this function sets the interface to the new address and then
12351da177e4SLinus Torvalds  * changes its dev_addr field to its previous value.
12361da177e4SLinus Torvalds  *
12371da177e4SLinus Torvalds  * Unwinding assumes bond's mac address has not yet changed.
12381da177e4SLinus Torvalds  */
alb_set_mac_address(struct bonding * bond,void * addr)12391da177e4SLinus Torvalds static int alb_set_mac_address(struct bonding *bond, void *addr)
12401da177e4SLinus Torvalds {
124181f23b13SVeaceslav Falico 	struct slave *slave, *rollback_slave;
12429caff1e7SVeaceslav Falico 	struct list_head *iter;
1243faeeb317SJarod Wilson 	struct sockaddr_storage ss;
1244faeeb317SJarod Wilson 	char tmp_addr[MAX_ADDR_LEN];
12451da177e4SLinus Torvalds 	int res;
12461da177e4SLinus Torvalds 
1247dec1e90eSnikolay@redhat.com 	if (bond->alb_info.rlb_enabled)
12481da177e4SLinus Torvalds 		return 0;
12491da177e4SLinus Torvalds 
12509caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, slave, iter) {
12511da177e4SLinus Torvalds 		/* save net_device's current hw address */
1252faeeb317SJarod Wilson 		bond_hw_addr_copy(tmp_addr, slave->dev->dev_addr,
1253faeeb317SJarod Wilson 				  slave->dev->addr_len);
12541da177e4SLinus Torvalds 
12553a37a963SPetr Machata 		res = dev_set_mac_address(slave->dev, addr, NULL);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 		/* restore net_device's hw address */
12586f238100SJakub Kicinski 		dev_addr_set(slave->dev, tmp_addr);
12591da177e4SLinus Torvalds 
1260eb7cc59aSStephen Hemminger 		if (res)
12611da177e4SLinus Torvalds 			goto unwind;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	return 0;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds unwind:
1267faeeb317SJarod Wilson 	memcpy(ss.__data, bond->dev->dev_addr, bond->dev->addr_len);
1268faeeb317SJarod Wilson 	ss.ss_family = bond->dev->type;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	/* unwind from head to the slave that failed */
12719caff1e7SVeaceslav Falico 	bond_for_each_slave(bond, rollback_slave, iter) {
127281f23b13SVeaceslav Falico 		if (rollback_slave == slave)
127381f23b13SVeaceslav Falico 			break;
1274faeeb317SJarod Wilson 		bond_hw_addr_copy(tmp_addr, rollback_slave->dev->dev_addr,
1275faeeb317SJarod Wilson 				  rollback_slave->dev->addr_len);
1276faeeb317SJarod Wilson 		dev_set_mac_address(rollback_slave->dev,
12773a37a963SPetr Machata 				    (struct sockaddr *)&ss, NULL);
12786f238100SJakub Kicinski 		dev_addr_set(rollback_slave->dev, tmp_addr);
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	return res;
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds 
12840da8aa00SSun Shouxin /* determine if the packet is NA or NS */
alb_determine_nd(struct sk_buff * skb,struct bonding * bond)12850da8aa00SSun Shouxin static bool alb_determine_nd(struct sk_buff *skb, struct bonding *bond)
12860da8aa00SSun Shouxin {
12870da8aa00SSun Shouxin 	struct ipv6hdr *ip6hdr;
12880da8aa00SSun Shouxin 	struct icmp6hdr *hdr;
12890da8aa00SSun Shouxin 
12900da8aa00SSun Shouxin 	if (!pskb_network_may_pull(skb, sizeof(*ip6hdr)))
12910da8aa00SSun Shouxin 		return true;
12920da8aa00SSun Shouxin 
12930da8aa00SSun Shouxin 	ip6hdr = ipv6_hdr(skb);
12940da8aa00SSun Shouxin 	if (ip6hdr->nexthdr != IPPROTO_ICMPV6)
12950da8aa00SSun Shouxin 		return false;
12960da8aa00SSun Shouxin 
12970da8aa00SSun Shouxin 	if (!pskb_network_may_pull(skb, sizeof(*ip6hdr) + sizeof(*hdr)))
12980da8aa00SSun Shouxin 		return true;
12990da8aa00SSun Shouxin 
13000da8aa00SSun Shouxin 	hdr = icmp6_hdr(skb);
13010da8aa00SSun Shouxin 	return hdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT ||
13020da8aa00SSun Shouxin 		hdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION;
13030da8aa00SSun Shouxin }
13040da8aa00SSun Shouxin 
13054057c58dSWang Hai /************************ exported alb functions ************************/
13061da177e4SLinus Torvalds 
bond_alb_initialize(struct bonding * bond,int rlb_enabled)13071da177e4SLinus Torvalds int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
13081da177e4SLinus Torvalds {
13091da177e4SLinus Torvalds 	int res;
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	res = tlb_initialize(bond);
131235d75ee4SWang Yufen 	if (res)
13131da177e4SLinus Torvalds 		return res;
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 	if (rlb_enabled) {
13161da177e4SLinus Torvalds 		res = rlb_initialize(bond);
13171da177e4SLinus Torvalds 		if (res) {
13181da177e4SLinus Torvalds 			tlb_deinitialize(bond);
13191da177e4SLinus Torvalds 			return res;
13201da177e4SLinus Torvalds 		}
1321ab84db25SEric Dumazet 		bond->alb_info.rlb_enabled = 1;
1322b76850abSMitch Williams 	} else {
1323b76850abSMitch Williams 		bond->alb_info.rlb_enabled = 0;
13241da177e4SLinus Torvalds 	}
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	return 0;
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
bond_alb_deinitialize(struct bonding * bond)13291da177e4SLinus Torvalds void bond_alb_deinitialize(struct bonding *bond)
13301da177e4SLinus Torvalds {
13311da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	tlb_deinitialize(bond);
13341da177e4SLinus Torvalds 
1335dda0fd5cSWang Yufen 	if (bond_info->rlb_enabled)
13361da177e4SLinus Torvalds 		rlb_deinitialize(bond);
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
bond_do_alb_xmit(struct sk_buff * skb,struct bonding * bond,struct slave * tx_slave)1339dbdc8a21STonghao Zhang static netdev_tx_t bond_do_alb_xmit(struct sk_buff *skb, struct bonding *bond,
13409a49aba1SMahesh Bandewar 				    struct slave *tx_slave)
13411da177e4SLinus Torvalds {
13421da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
13439a49aba1SMahesh Bandewar 	struct ethhdr *eth_data = eth_hdr(skb);
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 	if (!tx_slave) {
13461da177e4SLinus Torvalds 		/* unbalanced or unassigned, send through primary */
134728c71926Sdingtianhong 		tx_slave = rcu_dereference(bond->curr_active_slave);
1348e9f0fb88SMahesh Bandewar 		if (bond->params.tlb_dynamic_lb)
13491da177e4SLinus Torvalds 			bond_info->unbalanced_load += skb->len;
13501da177e4SLinus Torvalds 	}
13511da177e4SLinus Torvalds 
13528557cd74SVeaceslav Falico 	if (tx_slave && bond_slave_can_tx(tx_slave)) {
1353b5091b55SAndreea-Cristina Bernat 		if (tx_slave != rcu_access_pointer(bond->curr_active_slave)) {
13542a7c183bSJoe Perches 			ether_addr_copy(eth_data->h_source,
13552a7c183bSJoe Perches 					tx_slave->dev->dev_addr);
13561da177e4SLinus Torvalds 		}
13571da177e4SLinus Torvalds 
1358ae46f184SEric Dumazet 		return bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1359157550fbSJoe Perches 	}
1360157550fbSJoe Perches 
1361e9f0fb88SMahesh Bandewar 	if (tx_slave && bond->params.tlb_dynamic_lb) {
13624bab16d7SNikolay Aleksandrov 		spin_lock(&bond->mode_lock);
1363f515e6b7SMaxim Uvarov 		__tlb_clear_slave(bond, tx_slave, 0);
13644bab16d7SNikolay Aleksandrov 		spin_unlock(&bond->mode_lock);
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 
136704502430SEric Dumazet 	/* no suitable interface, frame not sent */
1368ae46f184SEric Dumazet 	return bond_tx_drop(bond->dev, skb);
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
bond_xmit_tlb_slave_get(struct bonding * bond,struct sk_buff * skb)137134b37e20SMaor Gottlieb struct slave *bond_xmit_tlb_slave_get(struct bonding *bond,
137234b37e20SMaor Gottlieb 				      struct sk_buff *skb)
1373f05b42eaSMahesh Bandewar {
1374f05b42eaSMahesh Bandewar 	struct slave *tx_slave = NULL;
137534b37e20SMaor Gottlieb 	struct ethhdr *eth_data;
1376f05b42eaSMahesh Bandewar 	u32 hash_index;
1377f05b42eaSMahesh Bandewar 
1378f05b42eaSMahesh Bandewar 	skb_reset_mac_header(skb);
1379f05b42eaSMahesh Bandewar 	eth_data = eth_hdr(skb);
1380f05b42eaSMahesh Bandewar 
1381f05b42eaSMahesh Bandewar 	/* Do not TX balance any multicast or broadcast */
1382f05b42eaSMahesh Bandewar 	if (!is_multicast_ether_addr(eth_data->h_dest)) {
1383f05b42eaSMahesh Bandewar 		switch (skb->protocol) {
1384f05b42eaSMahesh Bandewar 		case htons(ETH_P_IPV6):
13850da8aa00SSun Shouxin 			if (alb_determine_nd(skb, bond))
13860da8aa00SSun Shouxin 				break;
13870da8aa00SSun Shouxin 			fallthrough;
13880da8aa00SSun Shouxin 		case htons(ETH_P_IP):
1389f05b42eaSMahesh Bandewar 			hash_index = bond_xmit_hash(bond, skb);
1390e9f0fb88SMahesh Bandewar 			if (bond->params.tlb_dynamic_lb) {
1391e9f0fb88SMahesh Bandewar 				tx_slave = tlb_choose_channel(bond,
1392e9f0fb88SMahesh Bandewar 							      hash_index & 0xFF,
1393e9f0fb88SMahesh Bandewar 							      skb->len);
1394e9f0fb88SMahesh Bandewar 			} else {
1395ee637714SMahesh Bandewar 				struct bond_up_slave *slaves;
1396ee637714SMahesh Bandewar 				unsigned int count;
1397e9f0fb88SMahesh Bandewar 
1398ed7d4f02SMaor Gottlieb 				slaves = rcu_dereference(bond->usable_slaves);
13996aa7de05SMark Rutland 				count = slaves ? READ_ONCE(slaves->count) : 0;
1400ee637714SMahesh Bandewar 				if (likely(count))
14016b794c1cSMahesh Bandewar 					tx_slave = slaves->arr[hash_index %
1402ee637714SMahesh Bandewar 							       count];
1403e9f0fb88SMahesh Bandewar 			}
1404f05b42eaSMahesh Bandewar 			break;
1405f05b42eaSMahesh Bandewar 		}
1406f05b42eaSMahesh Bandewar 	}
140734b37e20SMaor Gottlieb 	return tx_slave;
140834b37e20SMaor Gottlieb }
140934b37e20SMaor Gottlieb 
bond_tlb_xmit(struct sk_buff * skb,struct net_device * bond_dev)141034b37e20SMaor Gottlieb netdev_tx_t bond_tlb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
141134b37e20SMaor Gottlieb {
141234b37e20SMaor Gottlieb 	struct bonding *bond = netdev_priv(bond_dev);
141334b37e20SMaor Gottlieb 	struct slave *tx_slave;
141434b37e20SMaor Gottlieb 
141534b37e20SMaor Gottlieb 	tx_slave = bond_xmit_tlb_slave_get(bond, skb);
1416f05b42eaSMahesh Bandewar 	return bond_do_alb_xmit(skb, bond, tx_slave);
1417f05b42eaSMahesh Bandewar }
1418f05b42eaSMahesh Bandewar 
bond_xmit_alb_slave_get(struct bonding * bond,struct sk_buff * skb)141934b37e20SMaor Gottlieb struct slave *bond_xmit_alb_slave_get(struct bonding *bond,
142034b37e20SMaor Gottlieb 				      struct sk_buff *skb)
14219a49aba1SMahesh Bandewar {
14229a49aba1SMahesh Bandewar 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
14239a49aba1SMahesh Bandewar 	static const __be32 ip_bcast = htonl(0xffffffff);
142434b37e20SMaor Gottlieb 	struct slave *tx_slave = NULL;
14259a49aba1SMahesh Bandewar 	const u8 *hash_start = NULL;
142634b37e20SMaor Gottlieb 	bool do_tx_balance = true;
142734b37e20SMaor Gottlieb 	struct ethhdr *eth_data;
142834b37e20SMaor Gottlieb 	u32 hash_index = 0;
142934b37e20SMaor Gottlieb 	int hash_size = 0;
14309a49aba1SMahesh Bandewar 
14319a49aba1SMahesh Bandewar 	skb_reset_mac_header(skb);
14329a49aba1SMahesh Bandewar 	eth_data = eth_hdr(skb);
14339a49aba1SMahesh Bandewar 
14349a49aba1SMahesh Bandewar 	switch (ntohs(skb->protocol)) {
14359a49aba1SMahesh Bandewar 	case ETH_P_IP: {
143638f88c45SEric Dumazet 		const struct iphdr *iph;
14379a49aba1SMahesh Bandewar 
1438cbeeea70SDebabrata Banerjee 		if (is_broadcast_ether_addr(eth_data->h_dest) ||
143938f88c45SEric Dumazet 		    !pskb_network_may_pull(skb, sizeof(*iph))) {
144038f88c45SEric Dumazet 			do_tx_balance = false;
144138f88c45SEric Dumazet 			break;
144238f88c45SEric Dumazet 		}
144338f88c45SEric Dumazet 		iph = ip_hdr(skb);
144438f88c45SEric Dumazet 		if (iph->daddr == ip_bcast || iph->protocol == IPPROTO_IGMP) {
14459a49aba1SMahesh Bandewar 			do_tx_balance = false;
14469a49aba1SMahesh Bandewar 			break;
14479a49aba1SMahesh Bandewar 		}
14489a49aba1SMahesh Bandewar 		hash_start = (char *)&(iph->daddr);
14499a49aba1SMahesh Bandewar 		hash_size = sizeof(iph->daddr);
14509a49aba1SMahesh Bandewar 		break;
145138f88c45SEric Dumazet 	}
145238f88c45SEric Dumazet 	case ETH_P_IPV6: {
145338f88c45SEric Dumazet 		const struct ipv6hdr *ip6hdr;
145438f88c45SEric Dumazet 
14559a49aba1SMahesh Bandewar 		/* IPv6 doesn't really use broadcast mac address, but leave
14569a49aba1SMahesh Bandewar 		 * that here just in case.
14579a49aba1SMahesh Bandewar 		 */
1458cbeeea70SDebabrata Banerjee 		if (is_broadcast_ether_addr(eth_data->h_dest)) {
14599a49aba1SMahesh Bandewar 			do_tx_balance = false;
14609a49aba1SMahesh Bandewar 			break;
14619a49aba1SMahesh Bandewar 		}
14629a49aba1SMahesh Bandewar 
14639a49aba1SMahesh Bandewar 		/* IPv6 uses all-nodes multicast as an equivalent to
14649a49aba1SMahesh Bandewar 		 * broadcasts in IPv4.
14659a49aba1SMahesh Bandewar 		 */
14669a49aba1SMahesh Bandewar 		if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
14679a49aba1SMahesh Bandewar 			do_tx_balance = false;
14689a49aba1SMahesh Bandewar 			break;
14699a49aba1SMahesh Bandewar 		}
14709a49aba1SMahesh Bandewar 
14710da8aa00SSun Shouxin 		if (alb_determine_nd(skb, bond)) {
147238f88c45SEric Dumazet 			do_tx_balance = false;
147338f88c45SEric Dumazet 			break;
147438f88c45SEric Dumazet 		}
14750da8aa00SSun Shouxin 
14760da8aa00SSun Shouxin 		/* The IPv6 header is pulled by alb_determine_nd */
147738f88c45SEric Dumazet 		/* Additionally, DAD probes should not be tx-balanced as that
14789a49aba1SMahesh Bandewar 		 * will lead to false positives for duplicate addresses and
14799a49aba1SMahesh Bandewar 		 * prevent address configuration from working.
14809a49aba1SMahesh Bandewar 		 */
14819a49aba1SMahesh Bandewar 		ip6hdr = ipv6_hdr(skb);
14829a49aba1SMahesh Bandewar 		if (ipv6_addr_any(&ip6hdr->saddr)) {
14839a49aba1SMahesh Bandewar 			do_tx_balance = false;
14849a49aba1SMahesh Bandewar 			break;
14859a49aba1SMahesh Bandewar 		}
14869a49aba1SMahesh Bandewar 
148738f88c45SEric Dumazet 		hash_start = (char *)&ip6hdr->daddr;
148838f88c45SEric Dumazet 		hash_size = sizeof(ip6hdr->daddr);
14899a49aba1SMahesh Bandewar 		break;
149038f88c45SEric Dumazet 	}
14919a49aba1SMahesh Bandewar 	case ETH_P_ARP:
14929a49aba1SMahesh Bandewar 		do_tx_balance = false;
14939a49aba1SMahesh Bandewar 		if (bond_info->rlb_enabled)
14949a49aba1SMahesh Bandewar 			tx_slave = rlb_arp_xmit(skb, bond);
14959a49aba1SMahesh Bandewar 		break;
14969a49aba1SMahesh Bandewar 	default:
14979a49aba1SMahesh Bandewar 		do_tx_balance = false;
14989a49aba1SMahesh Bandewar 		break;
14999a49aba1SMahesh Bandewar 	}
15009a49aba1SMahesh Bandewar 
15019a49aba1SMahesh Bandewar 	if (do_tx_balance) {
1502e79c1055SDebabrata Banerjee 		if (bond->params.tlb_dynamic_lb) {
15039a49aba1SMahesh Bandewar 			hash_index = _simple_hash(hash_start, hash_size);
15049a49aba1SMahesh Bandewar 			tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1505e79c1055SDebabrata Banerjee 		} else {
1506e79c1055SDebabrata Banerjee 			/*
1507e79c1055SDebabrata Banerjee 			 * do_tx_balance means we are free to select the tx_slave
1508e79c1055SDebabrata Banerjee 			 * So we do exactly what tlb would do for hash selection
1509e79c1055SDebabrata Banerjee 			 */
1510e79c1055SDebabrata Banerjee 
1511e79c1055SDebabrata Banerjee 			struct bond_up_slave *slaves;
1512e79c1055SDebabrata Banerjee 			unsigned int count;
1513e79c1055SDebabrata Banerjee 
1514ed7d4f02SMaor Gottlieb 			slaves = rcu_dereference(bond->usable_slaves);
1515e79c1055SDebabrata Banerjee 			count = slaves ? READ_ONCE(slaves->count) : 0;
1516e79c1055SDebabrata Banerjee 			if (likely(count))
1517e79c1055SDebabrata Banerjee 				tx_slave = slaves->arr[bond_xmit_hash(bond, skb) %
1518e79c1055SDebabrata Banerjee 						       count];
1519e79c1055SDebabrata Banerjee 		}
15209a49aba1SMahesh Bandewar 	}
152134b37e20SMaor Gottlieb 	return tx_slave;
152234b37e20SMaor Gottlieb }
15239a49aba1SMahesh Bandewar 
bond_alb_xmit(struct sk_buff * skb,struct net_device * bond_dev)152434b37e20SMaor Gottlieb netdev_tx_t bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
152534b37e20SMaor Gottlieb {
152634b37e20SMaor Gottlieb 	struct bonding *bond = netdev_priv(bond_dev);
152734b37e20SMaor Gottlieb 	struct slave *tx_slave = NULL;
152834b37e20SMaor Gottlieb 
152934b37e20SMaor Gottlieb 	tx_slave = bond_xmit_alb_slave_get(bond, skb);
15309a49aba1SMahesh Bandewar 	return bond_do_alb_xmit(skb, bond, tx_slave);
15319a49aba1SMahesh Bandewar }
15329a49aba1SMahesh Bandewar 
bond_alb_monitor(struct work_struct * work)15331b76b316SJay Vosburgh void bond_alb_monitor(struct work_struct *work)
15341da177e4SLinus Torvalds {
15351b76b316SJay Vosburgh 	struct bonding *bond = container_of(work, struct bonding,
15361b76b316SJay Vosburgh 					    alb_work.work);
15371da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
15389caff1e7SVeaceslav Falico 	struct list_head *iter;
15391da177e4SLinus Torvalds 	struct slave *slave;
15401da177e4SLinus Torvalds 
15410965a1f3SVeaceslav Falico 	if (!bond_has_slaves(bond)) {
1542dac8e00fSEric Dumazet 		atomic_set(&bond_info->tx_rebalance_counter, 0);
15431da177e4SLinus Torvalds 		bond_info->lp_counter = 0;
15441da177e4SLinus Torvalds 		goto re_arm;
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 
1547733ab639Sdingtianhong 	rcu_read_lock();
1548733ab639Sdingtianhong 
1549dac8e00fSEric Dumazet 	atomic_inc(&bond_info->tx_rebalance_counter);
15501da177e4SLinus Torvalds 	bond_info->lp_counter++;
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds 	/* send learning packets */
15537eacd038SNeil Horman 	if (bond_info->lp_counter >= BOND_ALB_LP_TICKS(bond)) {
1554d0c21d43SVlad Yasevich 		bool strict_match;
1555d0c21d43SVlad Yasevich 
1556d0c21d43SVlad Yasevich 		bond_for_each_slave_rcu(bond, slave, iter) {
1557d0c21d43SVlad Yasevich 			/* If updating current_active, use all currently
15584057c58dSWang Hai 			 * user mac addresses (!strict_match).  Otherwise, only
1559d0c21d43SVlad Yasevich 			 * use mac of the slave device.
156014af9963SVlad Yasevich 			 * In RLB mode, we always use strict matches.
1561d0c21d43SVlad Yasevich 			 */
15624740d638SEric Dumazet 			strict_match = (slave != rcu_access_pointer(bond->curr_active_slave) ||
156314af9963SVlad Yasevich 					bond_info->rlb_enabled);
1564d0c21d43SVlad Yasevich 			alb_send_learning_packets(slave, slave->dev->dev_addr,
1565d0c21d43SVlad Yasevich 						  strict_match);
1566d0c21d43SVlad Yasevich 		}
15671da177e4SLinus Torvalds 		bond_info->lp_counter = 0;
15681da177e4SLinus Torvalds 	}
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	/* rebalance tx traffic */
1571dac8e00fSEric Dumazet 	if (atomic_read(&bond_info->tx_rebalance_counter) >= BOND_TLB_REBALANCE_TICKS) {
1572733ab639Sdingtianhong 		bond_for_each_slave_rcu(bond, slave, iter) {
15731da177e4SLinus Torvalds 			tlb_clear_slave(bond, slave, 1);
15744740d638SEric Dumazet 			if (slave == rcu_access_pointer(bond->curr_active_slave)) {
15751da177e4SLinus Torvalds 				SLAVE_TLB_INFO(slave).load =
15761da177e4SLinus Torvalds 					bond_info->unbalanced_load /
15771da177e4SLinus Torvalds 						BOND_TLB_REBALANCE_INTERVAL;
15781da177e4SLinus Torvalds 				bond_info->unbalanced_load = 0;
15791da177e4SLinus Torvalds 			}
15801da177e4SLinus Torvalds 		}
1581dac8e00fSEric Dumazet 		atomic_set(&bond_info->tx_rebalance_counter, 0);
15821da177e4SLinus Torvalds 	}
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	if (bond_info->rlb_enabled) {
15851da177e4SLinus Torvalds 		if (bond_info->primary_is_promisc &&
15861da177e4SLinus Torvalds 		    (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
15871da177e4SLinus Torvalds 
1588547942caSNikolay Aleksandrov 			/* dev_set_promiscuity requires rtnl and
15891f2cd845SDavid S. Miller 			 * nothing else.  Avoid race with bond_close.
15901f2cd845SDavid S. Miller 			 */
1591733ab639Sdingtianhong 			rcu_read_unlock();
1592733ab639Sdingtianhong 			if (!rtnl_trylock())
15931f2cd845SDavid S. Miller 				goto re_arm;
15941f2cd845SDavid S. Miller 
15951da177e4SLinus Torvalds 			bond_info->rlb_promisc_timeout_counter = 0;
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 			/* If the primary was set to promiscuous mode
15981da177e4SLinus Torvalds 			 * because a slave was disabled then
15991da177e4SLinus Torvalds 			 * it can now leave promiscuous mode.
16001da177e4SLinus Torvalds 			 */
16014740d638SEric Dumazet 			dev_set_promiscuity(rtnl_dereference(bond->curr_active_slave)->dev,
16024740d638SEric Dumazet 					    -1);
16031da177e4SLinus Torvalds 			bond_info->primary_is_promisc = 0;
16041f2cd845SDavid S. Miller 
16051f2cd845SDavid S. Miller 			rtnl_unlock();
1606733ab639Sdingtianhong 			rcu_read_lock();
1607d0e81b7eSJay Vosburgh 		}
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 		if (bond_info->rlb_rebalance) {
16101da177e4SLinus Torvalds 			bond_info->rlb_rebalance = 0;
16111da177e4SLinus Torvalds 			rlb_rebalance(bond);
16121da177e4SLinus Torvalds 		}
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 		/* check if clients need updating */
16151da177e4SLinus Torvalds 		if (bond_info->rx_ntt) {
16161da177e4SLinus Torvalds 			if (bond_info->rlb_update_delay_counter) {
16171da177e4SLinus Torvalds 				--bond_info->rlb_update_delay_counter;
16181da177e4SLinus Torvalds 			} else {
16191da177e4SLinus Torvalds 				rlb_update_rx_clients(bond);
1620dda0fd5cSWang Yufen 				if (bond_info->rlb_update_retry_counter)
16211da177e4SLinus Torvalds 					--bond_info->rlb_update_retry_counter;
1622dda0fd5cSWang Yufen 				else
16231da177e4SLinus Torvalds 					bond_info->rx_ntt = 0;
16241da177e4SLinus Torvalds 			}
16251da177e4SLinus Torvalds 		}
16261da177e4SLinus Torvalds 	}
1627733ab639Sdingtianhong 	rcu_read_unlock();
16281da177e4SLinus Torvalds re_arm:
16291b76b316SJay Vosburgh 	queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
16301da177e4SLinus Torvalds }
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds /* assumption: called before the slave is attached to the bond
16331da177e4SLinus Torvalds  * and not locked by the bond lock
16341da177e4SLinus Torvalds  */
bond_alb_init_slave(struct bonding * bond,struct slave * slave)16351da177e4SLinus Torvalds int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
16361da177e4SLinus Torvalds {
16371da177e4SLinus Torvalds 	int res;
16381da177e4SLinus Torvalds 
1639faeeb317SJarod Wilson 	res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1640faeeb317SJarod Wilson 				     slave->dev->addr_len);
1641dda0fd5cSWang Yufen 	if (res)
16421da177e4SLinus Torvalds 		return res;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	res = alb_handle_addr_collision_on_attach(bond, slave);
1645dda0fd5cSWang Yufen 	if (res)
16461da177e4SLinus Torvalds 		return res;
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds 	tlb_init_slave(slave);
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	/* order a rebalance ASAP */
1651dac8e00fSEric Dumazet 	atomic_set(&bond->alb_info.tx_rebalance_counter,
1652dac8e00fSEric Dumazet 		   BOND_TLB_REBALANCE_TICKS);
16531da177e4SLinus Torvalds 
1654dda0fd5cSWang Yufen 	if (bond->alb_info.rlb_enabled)
16551da177e4SLinus Torvalds 		bond->alb_info.rlb_rebalance = 1;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	return 0;
16581da177e4SLinus Torvalds }
16591da177e4SLinus Torvalds 
1660547942caSNikolay Aleksandrov /* Remove slave from tlb and rlb hash tables, and fix up MAC addresses
16612543331dSJay Vosburgh  * if necessary.
16622543331dSJay Vosburgh  *
16632543331dSJay Vosburgh  * Caller must hold RTNL and no other locks
16642543331dSJay Vosburgh  */
bond_alb_deinit_slave(struct bonding * bond,struct slave * slave)16651da177e4SLinus Torvalds void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
16661da177e4SLinus Torvalds {
16670965a1f3SVeaceslav Falico 	if (bond_has_slaves(bond))
16681da177e4SLinus Torvalds 		alb_change_hw_addr_on_detach(bond, slave);
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	tlb_clear_slave(bond, slave, 0);
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds 	if (bond->alb_info.rlb_enabled) {
16736475ae4cSVeaceslav Falico 		bond->alb_info.rx_slave = NULL;
16741da177e4SLinus Torvalds 		rlb_clear_slave(bond, slave);
16751da177e4SLinus Torvalds 	}
16766b794c1cSMahesh Bandewar 
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
bond_alb_handle_link_change(struct bonding * bond,struct slave * slave,char link)16791da177e4SLinus Torvalds void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
16801da177e4SLinus Torvalds {
16811da177e4SLinus Torvalds 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	if (link == BOND_LINK_DOWN) {
16841da177e4SLinus Torvalds 		tlb_clear_slave(bond, slave, 0);
168573ac0cd4SWang Yufen 		if (bond->alb_info.rlb_enabled)
16861da177e4SLinus Torvalds 			rlb_clear_slave(bond, slave);
16871da177e4SLinus Torvalds 	} else if (link == BOND_LINK_UP) {
16881da177e4SLinus Torvalds 		/* order a rebalance ASAP */
1689dac8e00fSEric Dumazet 		atomic_set(&bond_info->tx_rebalance_counter,
1690dac8e00fSEric Dumazet 			   BOND_TLB_REBALANCE_TICKS);
16911da177e4SLinus Torvalds 		if (bond->alb_info.rlb_enabled) {
16921da177e4SLinus Torvalds 			bond->alb_info.rlb_rebalance = 1;
16931da177e4SLinus Torvalds 			/* If the updelay module parameter is smaller than the
16941da177e4SLinus Torvalds 			 * forwarding delay of the switch the rebalance will
16951da177e4SLinus Torvalds 			 * not work because the rebalance arp replies will
16961da177e4SLinus Torvalds 			 * not be forwarded to the clients..
16971da177e4SLinus Torvalds 			 */
16981da177e4SLinus Torvalds 		}
16991da177e4SLinus Torvalds 	}
17006b794c1cSMahesh Bandewar 
17016b794c1cSMahesh Bandewar 	if (bond_is_nondyn_tlb(bond)) {
1702ee637714SMahesh Bandewar 		if (bond_update_slave_arr(bond, NULL))
17036b794c1cSMahesh Bandewar 			pr_err("Failed to build slave-array for TLB mode.\n");
17046b794c1cSMahesh Bandewar 	}
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds /**
17081da177e4SLinus Torvalds  * bond_alb_handle_active_change - assign new curr_active_slave
17091da177e4SLinus Torvalds  * @bond: our bonding struct
17101da177e4SLinus Torvalds  * @new_slave: new slave to assign
17111da177e4SLinus Torvalds  *
17121da177e4SLinus Torvalds  * Set the bond->curr_active_slave to @new_slave and handle
17131da177e4SLinus Torvalds  * mac address swapping and promiscuity changes as needed.
17141da177e4SLinus Torvalds  *
171562c5f518SNikolay Aleksandrov  * Caller must hold RTNL
17161da177e4SLinus Torvalds  */
bond_alb_handle_active_change(struct bonding * bond,struct slave * new_slave)17171da177e4SLinus Torvalds void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
17181da177e4SLinus Torvalds {
17191da177e4SLinus Torvalds 	struct slave *swap_slave;
17204740d638SEric Dumazet 	struct slave *curr_active;
17211da177e4SLinus Torvalds 
172262c5f518SNikolay Aleksandrov 	curr_active = rtnl_dereference(bond->curr_active_slave);
17234740d638SEric Dumazet 	if (curr_active == new_slave)
17241da177e4SLinus Torvalds 		return;
17251da177e4SLinus Torvalds 
17264740d638SEric Dumazet 	if (curr_active && bond->alb_info.primary_is_promisc) {
17274740d638SEric Dumazet 		dev_set_promiscuity(curr_active->dev, -1);
17281da177e4SLinus Torvalds 		bond->alb_info.primary_is_promisc = 0;
17291da177e4SLinus Torvalds 		bond->alb_info.rlb_promisc_timeout_counter = 0;
17301da177e4SLinus Torvalds 	}
17311da177e4SLinus Torvalds 
17324740d638SEric Dumazet 	swap_slave = curr_active;
1733278b2083Snikolay@redhat.com 	rcu_assign_pointer(bond->curr_active_slave, new_slave);
17341da177e4SLinus Torvalds 
17350965a1f3SVeaceslav Falico 	if (!new_slave || !bond_has_slaves(bond))
17361da177e4SLinus Torvalds 		return;
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	/* set the new curr_active_slave to the bonds mac address
17391da177e4SLinus Torvalds 	 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
17401da177e4SLinus Torvalds 	 */
1741b88ec38dSVeaceslav Falico 	if (!swap_slave)
1742b88ec38dSVeaceslav Falico 		swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
17431da177e4SLinus Torvalds 
1744547942caSNikolay Aleksandrov 	/* Arrange for swap_slave and new_slave to temporarily be
1745059fe7a5SJay Vosburgh 	 * ignored so we can mess with their MAC addresses without
1746059fe7a5SJay Vosburgh 	 * fear of interference from transmit activity.
1747059fe7a5SJay Vosburgh 	 */
1748dec1e90eSnikolay@redhat.com 	if (swap_slave)
1749059fe7a5SJay Vosburgh 		tlb_clear_slave(bond, swap_slave, 1);
1750059fe7a5SJay Vosburgh 	tlb_clear_slave(bond, new_slave, 1);
1751059fe7a5SJay Vosburgh 
17524996b909SVeaceslav Falico 	/* in TLB mode, the slave might flip down/up with the old dev_addr,
17534996b909SVeaceslav Falico 	 * and thus filter bond->dev_addr's packets, so force bond's mac
17544996b909SVeaceslav Falico 	 */
175501844098SVeaceslav Falico 	if (BOND_MODE(bond) == BOND_MODE_TLB) {
1756faeeb317SJarod Wilson 		struct sockaddr_storage ss;
1757faeeb317SJarod Wilson 		u8 tmp_addr[MAX_ADDR_LEN];
17584996b909SVeaceslav Falico 
1759faeeb317SJarod Wilson 		bond_hw_addr_copy(tmp_addr, new_slave->dev->dev_addr,
1760faeeb317SJarod Wilson 				  new_slave->dev->addr_len);
17614996b909SVeaceslav Falico 
1762faeeb317SJarod Wilson 		bond_hw_addr_copy(ss.__data, bond->dev->dev_addr,
1763faeeb317SJarod Wilson 				  bond->dev->addr_len);
1764faeeb317SJarod Wilson 		ss.ss_family = bond->dev->type;
17654996b909SVeaceslav Falico 		/* we don't care if it can't change its mac, best effort */
17663a37a963SPetr Machata 		dev_set_mac_address(new_slave->dev, (struct sockaddr *)&ss,
17673a37a963SPetr Machata 				    NULL);
17684996b909SVeaceslav Falico 
17696f238100SJakub Kicinski 		dev_addr_set(new_slave->dev, tmp_addr);
17704996b909SVeaceslav Falico 	}
17714996b909SVeaceslav Falico 
17721da177e4SLinus Torvalds 	/* curr_active_slave must be set before calling alb_swap_mac_addr */
17731da177e4SLinus Torvalds 	if (swap_slave) {
17741da177e4SLinus Torvalds 		/* swap mac address */
177543547ea6SVeaceslav Falico 		alb_swap_mac_addr(swap_slave, new_slave);
1776059fe7a5SJay Vosburgh 		alb_fasten_mac_swap(bond, swap_slave, new_slave);
1777059fe7a5SJay Vosburgh 	} else {
1778b88ec38dSVeaceslav Falico 		/* set the new_slave to the bond mac address */
1779faeeb317SJarod Wilson 		alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1780faeeb317SJarod Wilson 				       bond->dev->addr_len);
1781d0c21d43SVlad Yasevich 		alb_send_learning_packets(new_slave, bond->dev->dev_addr,
1782d0c21d43SVlad Yasevich 					  false);
17831da177e4SLinus Torvalds 	}
17841da177e4SLinus Torvalds }
17851da177e4SLinus Torvalds 
1786ecfede42SNikolay Aleksandrov /* Called with RTNL */
bond_alb_set_mac_address(struct net_device * bond_dev,void * addr)17871da177e4SLinus Torvalds int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
17881da177e4SLinus Torvalds {
1789454d7c9bSWang Chen 	struct bonding *bond = netdev_priv(bond_dev);
1790faeeb317SJarod Wilson 	struct sockaddr_storage *ss = addr;
17914740d638SEric Dumazet 	struct slave *curr_active;
1792b88ec38dSVeaceslav Falico 	struct slave *swap_slave;
17931da177e4SLinus Torvalds 	int res;
17941da177e4SLinus Torvalds 
1795faeeb317SJarod Wilson 	if (!is_valid_ether_addr(ss->__data))
17961da177e4SLinus Torvalds 		return -EADDRNOTAVAIL;
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds 	res = alb_set_mac_address(bond, addr);
179973ac0cd4SWang Yufen 	if (res)
18001da177e4SLinus Torvalds 		return res;
18011da177e4SLinus Torvalds 
18026f238100SJakub Kicinski 	dev_addr_set(bond_dev, ss->__data);
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds 	/* If there is no curr_active_slave there is nothing else to do.
18051da177e4SLinus Torvalds 	 * Otherwise we'll need to pass the new address to it and handle
18061da177e4SLinus Torvalds 	 * duplications.
18071da177e4SLinus Torvalds 	 */
18084740d638SEric Dumazet 	curr_active = rtnl_dereference(bond->curr_active_slave);
18094740d638SEric Dumazet 	if (!curr_active)
18101da177e4SLinus Torvalds 		return 0;
18111da177e4SLinus Torvalds 
1812b88ec38dSVeaceslav Falico 	swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	if (swap_slave) {
18154740d638SEric Dumazet 		alb_swap_mac_addr(swap_slave, curr_active);
18164740d638SEric Dumazet 		alb_fasten_mac_swap(bond, swap_slave, curr_active);
18171da177e4SLinus Torvalds 	} else {
1818faeeb317SJarod Wilson 		alb_set_slave_mac_addr(curr_active, bond_dev->dev_addr,
1819faeeb317SJarod Wilson 				       bond_dev->addr_len);
18201da177e4SLinus Torvalds 
18214740d638SEric Dumazet 		alb_send_learning_packets(curr_active,
1822d0c21d43SVlad Yasevich 					  bond_dev->dev_addr, false);
18231da177e4SLinus Torvalds 		if (bond->alb_info.rlb_enabled) {
18241da177e4SLinus Torvalds 			/* inform clients mac address has changed */
18254740d638SEric Dumazet 			rlb_req_update_slave_clients(bond, curr_active);
18261da177e4SLinus Torvalds 		}
18271da177e4SLinus Torvalds 	}
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	return 0;
18301da177e4SLinus Torvalds }
18311da177e4SLinus Torvalds 
bond_alb_clear_vlan(struct bonding * bond,unsigned short vlan_id)18321da177e4SLinus Torvalds void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
18331da177e4SLinus Torvalds {
183473ac0cd4SWang Yufen 	if (bond->alb_info.rlb_enabled)
18351da177e4SLinus Torvalds 		rlb_clear_vlan(bond, vlan_id);
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds 
1838