blob: 615f3bebd01902e46338f6c0def81579c3647d23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080015 * with this program; if not, see <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * The full GNU General Public License is included in this distribution in the
18 * file called LICENSE.
19 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/skbuff.h>
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/pkt_sched.h>
26#include <linux/spinlock.h>
27#include <linux/slab.h>
28#include <linux/timer.h>
29#include <linux/ip.h>
30#include <linux/ipv6.h>
31#include <linux/if_arp.h>
32#include <linux/if_ether.h>
33#include <linux/if_bonding.h>
34#include <linux/if_vlan.h>
35#include <linux/in.h>
36#include <net/ipx.h>
37#include <net/arp.h>
Vlad Yasevich2d1ea192008-08-28 15:38:41 -040038#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/byteorder.h>
40#include "bonding.h"
41#include "bond_alb.h"
42
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Eric Dumazet885a1362009-09-01 06:31:18 +000045#ifndef __long_aligned
46#define __long_aligned __attribute__((aligned((sizeof(long)))))
47#endif
48static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
49 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
50};
51static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
52 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
53};
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
55
56#pragma pack(1)
57struct learning_pkt {
58 u8 mac_dst[ETH_ALEN];
59 u8 mac_src[ETH_ALEN];
Al Virod3bb52b2007-08-22 20:06:58 -040060 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 u8 padding[ETH_ZLEN - ETH_HLEN];
62};
63
64struct arp_pkt {
Al Virod3bb52b2007-08-22 20:06:58 -040065 __be16 hw_addr_space;
66 __be16 prot_addr_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 u8 hw_addr_len;
68 u8 prot_addr_len;
Al Virod3bb52b2007-08-22 20:06:58 -040069 __be16 op_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 u8 mac_src[ETH_ALEN]; /* sender hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -040071 __be32 ip_src; /* sender IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 u8 mac_dst[ETH_ALEN]; /* target hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -040073 __be32 ip_dst; /* target IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75#pragma pack()
76
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -030077static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
78{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070079 return (struct arp_pkt *)skb_network_header(skb);
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -030080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Forward declaration */
Vlad Yasevichd0c21d42014-05-21 13:19:48 -040083static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[],
84 bool strict_match);
Jiri Bohace53665c2012-11-28 04:42:14 +000085static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp);
86static void rlb_src_unlink(struct bonding *bond, u32 index);
87static void rlb_src_link(struct bonding *bond, u32 ip_src_hash,
88 u32 ip_dst_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070090static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int i;
93 u8 hash = 0;
94
Wang Yufenfdb89d72014-02-14 17:15:14 +080095 for (i = 0; i < hash_size; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 hash ^= hash_start[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 return hash;
99}
100
101/*********************** tlb specific functions ***************************/
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
104{
105 if (save_load) {
106 entry->load_history = 1 + entry->tx_bytes /
107 BOND_TLB_REBALANCE_INTERVAL;
108 entry->tx_bytes = 0;
109 }
110
111 entry->tx_slave = NULL;
112 entry->next = TLB_NULL_INDEX;
113 entry->prev = TLB_NULL_INDEX;
114}
115
116static inline void tlb_init_slave(struct slave *slave)
117{
118 SLAVE_TLB_INFO(slave).load = 0;
119 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
120}
121
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000122static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
123 int save_load)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 struct tlb_client_info *tx_hash_table;
126 u32 index;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 /* clear slave from tx_hashtbl */
129 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
130
Andy Gospodarekce39a802008-10-30 17:41:16 -0700131 /* skip this if we've already freed the tx hash table */
132 if (tx_hash_table) {
133 index = SLAVE_TLB_INFO(slave).head;
134 while (index != TLB_NULL_INDEX) {
135 u32 next_index = tx_hash_table[index].next;
136 tlb_init_table_entry(&tx_hash_table[index], save_load);
137 index = next_index;
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 tlb_init_slave(slave);
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000142}
Jay Vosburgh5af47b22006-01-09 12:14:00 -0800143
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000144static void tlb_clear_slave(struct bonding *bond, struct slave *slave,
145 int save_load)
146{
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200147 spin_lock_bh(&bond->mode_lock);
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000148 __tlb_clear_slave(bond, slave, save_load);
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200149 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152/* Must be called before starting the monitor timer */
153static int tlb_initialize(struct bonding *bond)
154{
155 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
156 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
Mitch Williams0d206a32005-11-09 10:35:30 -0800157 struct tlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 int i;
159
Joe Jin243cb4e2007-02-06 14:16:40 -0800160 new_hashtbl = kzalloc(size, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000161 if (!new_hashtbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -1;
Joe Perchese404dec2012-01-29 12:56:23 +0000163
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200164 spin_lock_bh(&bond->mode_lock);
Mitch Williams0d206a32005-11-09 10:35:30 -0800165
166 bond_info->tx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Wang Yufenfdb89d72014-02-14 17:15:14 +0800168 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++)
Peter Pan(潘卫平)38dbaf02011-04-08 03:40:19 +0000169 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200171 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 return 0;
174}
175
176/* Must be called only after all slaves have been released */
177static void tlb_deinitialize(struct bonding *bond)
178{
179 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mahesh Bandewar6b794c12014-07-16 11:10:36 -0700180 struct tlb_up_slave *arr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200182 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 kfree(bond_info->tx_hashtbl);
185 bond_info->tx_hashtbl = NULL;
186
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200187 spin_unlock_bh(&bond->mode_lock);
Mahesh Bandewar6b794c12014-07-16 11:10:36 -0700188
189 arr = rtnl_dereference(bond_info->slave_arr);
190 if (arr)
191 kfree_rcu(arr, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Jiri Pirko097811b2010-05-19 03:26:39 +0000194static long long compute_gap(struct slave *slave)
195{
196 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
197 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
201{
202 struct slave *slave, *least_loaded;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +0200203 struct list_head *iter;
Jiri Pirko097811b2010-05-19 03:26:39 +0000204 long long max_gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Jiri Pirko097811b2010-05-19 03:26:39 +0000206 least_loaded = NULL;
207 max_gap = LLONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Find the slave with the largest gap */
dingtianhong28c71922013-10-15 16:28:39 +0800210 bond_for_each_slave_rcu(bond, slave, iter) {
Veaceslav Falico8557cd72014-05-15 21:39:59 +0200211 if (bond_slave_can_tx(slave)) {
Jiri Pirko097811b2010-05-19 03:26:39 +0000212 long long gap = compute_gap(slave);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (max_gap < gap) {
215 least_loaded = slave;
216 max_gap = gap;
217 }
218 }
219 }
220
221 return least_loaded;
222}
223
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000224static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
225 u32 skb_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
228 struct tlb_client_info *hash_table;
229 struct slave *assigned_slave;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 hash_table = bond_info->tx_hashtbl;
232 assigned_slave = hash_table[hash_index].tx_slave;
233 if (!assigned_slave) {
234 assigned_slave = tlb_get_least_loaded_slave(bond);
235
236 if (assigned_slave) {
237 struct tlb_slave_info *slave_info =
238 &(SLAVE_TLB_INFO(assigned_slave));
239 u32 next_index = slave_info->head;
240
241 hash_table[hash_index].tx_slave = assigned_slave;
242 hash_table[hash_index].next = next_index;
243 hash_table[hash_index].prev = TLB_NULL_INDEX;
244
Wang Yufenfdb89d72014-02-14 17:15:14 +0800245 if (next_index != TLB_NULL_INDEX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 hash_table[next_index].prev = hash_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 slave_info->head = hash_index;
249 slave_info->load +=
250 hash_table[hash_index].load_history;
251 }
252 }
253
Wang Yufenfdb89d72014-02-14 17:15:14 +0800254 if (assigned_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 hash_table[hash_index].tx_bytes += skb_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return assigned_slave;
258}
259
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000260static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
261 u32 skb_len)
262{
263 struct slave *tx_slave;
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200264
265 /* We don't need to disable softirq here, becase
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000266 * tlb_choose_channel() is only called by bond_alb_xmit()
267 * which already has softirq disabled.
268 */
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200269 spin_lock(&bond->mode_lock);
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000270 tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200271 spin_unlock(&bond->mode_lock);
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200272
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000273 return tx_slave;
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*********************** rlb specific functions ***************************/
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/* when an ARP REPLY is received from a client update its info
279 * in the rx_hashtbl
280 */
281static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
282{
283 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
284 struct rlb_client_info *client_info;
285 u32 hash_index;
286
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200287 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Wang Yufenb85b6fb2014-02-14 17:15:12 +0800289 hash_index = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 client_info = &(bond_info->rx_hashtbl[hash_index]);
291
292 if ((client_info->assigned) &&
293 (client_info->ip_src == arp->ip_dst) &&
Flavio Leitner42d782a2010-06-29 08:24:39 +0000294 (client_info->ip_dst == arp->ip_src) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000295 (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* update the clients MAC address */
Joe Perchesada0f862014-02-15 16:02:17 -0800297 ether_addr_copy(client_info->mac_dst, arp->mac_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 client_info->ntt = 1;
299 bond_info->rx_ntt = 1;
300 }
301
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200302 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Eric Dumazetde063b72012-06-11 19:23:07 +0000305static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond,
306 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Eric Dumazetde063b72012-06-11 19:23:07 +0000308 struct arp_pkt *arp, _arp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Jiri Pirko3aba8912011-04-19 03:48:16 +0000310 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
David S. Millerb99215c2012-05-13 15:45:13 -0400311 goto out;
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800312
Eric Dumazetde063b72012-06-11 19:23:07 +0000313 arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp);
314 if (!arp)
David S. Millerb99215c2012-05-13 15:45:13 -0400315 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jiri Bohace53665c2012-11-28 04:42:14 +0000317 /* We received an ARP from arp->ip_src.
318 * We might have used this IP address previously (on the bonding host
319 * itself or on a system that is bridged together with the bond).
320 * However, if arp->mac_src is different than what is stored in
321 * rx_hashtbl, some other host is now using the IP and we must prevent
322 * sending out client updates with this IP address and the old MAC
323 * address.
324 * Clean up all hash table entries that have this address as ip_src but
325 * have a different mac_src.
326 */
327 rlb_purge_src_ip(bond, arp);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (arp->op_code == htons(ARPOP_REPLY)) {
330 /* update rx hash table for this ARP */
331 rlb_update_entry_from_arp(bond, arp);
Veaceslav Falico0a111a02014-07-15 19:36:02 +0200332 netdev_dbg(bond->dev, "Server received an ARP Reply from client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
David S. Millerb99215c2012-05-13 15:45:13 -0400334out:
335 return RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Nikolay Aleksandrov8c0bc552014-09-11 22:49:28 +0200338/* Caller must hold rcu_read_lock() */
dingtianhong28c71922013-10-15 16:28:39 +0800339static struct slave *__rlb_next_rx_slave(struct bonding *bond)
340{
341 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
342 struct slave *before = NULL, *rx_slave = NULL, *slave;
343 struct list_head *iter;
344 bool found = false;
345
346 bond_for_each_slave_rcu(bond, slave, iter) {
Veaceslav Falico8557cd72014-05-15 21:39:59 +0200347 if (!bond_slave_can_tx(slave))
dingtianhong28c71922013-10-15 16:28:39 +0800348 continue;
349 if (!found) {
350 if (!before || before->speed < slave->speed)
351 before = slave;
352 } else {
353 if (!rx_slave || rx_slave->speed < slave->speed)
354 rx_slave = slave;
355 }
356 if (slave == bond_info->rx_slave)
357 found = true;
358 }
359 /* we didn't find anything after the current or we have something
360 * better before and up to the current slave
361 */
362 if (!rx_slave || (before && rx_slave->speed < before->speed))
363 rx_slave = before;
364
365 if (rx_slave)
366 bond_info->rx_slave = rx_slave;
367
368 return rx_slave;
369}
370
Nikolay Aleksandrov56924c32014-09-15 17:19:33 +0200371/* Caller must hold RTNL, rcu_read_lock is obtained only to silence checkers */
372static struct slave *rlb_next_rx_slave(struct bonding *bond)
373{
374 struct slave *rx_slave;
375
376 ASSERT_RTNL();
377
378 rcu_read_lock();
379 rx_slave = __rlb_next_rx_slave(bond);
380 rcu_read_unlock();
381
382 return rx_slave;
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/* teach the switch the mac of a disabled slave
386 * on the primary for fault tolerance
387 *
Nikolay Aleksandrov62c5f512014-09-11 22:49:23 +0200388 * Caller must hold RTNL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
390static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
391{
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200392 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave);
Eric Dumazet4740d632014-07-15 06:56:55 -0700393
394 if (!curr_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (!bond->alb_info.primary_is_promisc) {
Eric Dumazet4740d632014-07-15 06:56:55 -0700398 if (!dev_set_promiscuity(curr_active->dev, 1))
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700399 bond->alb_info.primary_is_promisc = 1;
400 else
401 bond->alb_info.primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 bond->alb_info.rlb_promisc_timeout_counter = 0;
405
Eric Dumazet4740d632014-07-15 06:56:55 -0700406 alb_send_learning_packets(curr_active, addr, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409/* slave being removed should not be active at this point
410 *
dingtianhongb2e7ace2013-12-13 10:19:32 +0800411 * Caller must hold rtnl.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
413static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
414{
415 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
416 struct rlb_client_info *rx_hash_table;
417 u32 index, next_index;
418
419 /* clear slave from rx_hashtbl */
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200420 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 rx_hash_table = bond_info->rx_hashtbl;
Jiri Bohace53665c2012-11-28 04:42:14 +0000423 index = bond_info->rx_hashtbl_used_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 for (; index != RLB_NULL_INDEX; index = next_index) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000425 next_index = rx_hash_table[index].used_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (rx_hash_table[index].slave == slave) {
427 struct slave *assigned_slave = rlb_next_rx_slave(bond);
428
429 if (assigned_slave) {
430 rx_hash_table[index].slave = assigned_slave;
Joe Perchesa6700db2012-05-09 17:04:04 +0000431 if (!ether_addr_equal_64bits(rx_hash_table[index].mac_dst,
432 mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 bond_info->rx_hashtbl[index].ntt = 1;
434 bond_info->rx_ntt = 1;
435 /* A slave has been removed from the
436 * table because it is either disabled
437 * or being released. We must retry the
438 * update to avoid clients from not
439 * being updated & disconnecting when
440 * there is stress
441 */
442 bond_info->rlb_update_retry_counter =
443 RLB_UPDATE_RETRY;
444 }
445 } else { /* there is no active slave */
446 rx_hash_table[index].slave = NULL;
447 }
448 }
449 }
450
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200451 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Nikolay Aleksandrov1c72cfdc92014-09-11 22:49:24 +0200453 if (slave != rtnl_dereference(bond->curr_active_slave))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457static void rlb_update_client(struct rlb_client_info *client_info)
458{
459 int i;
460
Wang Yufen35d75ee2014-02-14 17:15:15 +0800461 if (!client_info->slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
465 struct sk_buff *skb;
466
467 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
468 client_info->ip_dst,
469 client_info->slave->dev,
470 client_info->ip_src,
471 client_info->mac_dst,
472 client_info->slave->dev->dev_addr,
473 client_info->mac_dst);
474 if (!skb) {
Veaceslav Falico0a111a02014-07-15 19:36:02 +0200475 netdev_err(client_info->slave->bond->dev,
476 "failed to create an ARP packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 continue;
478 }
479
480 skb->dev = client_info->slave->dev;
481
Veaceslav Falicod3ab3ff2013-08-29 23:38:57 +0200482 if (client_info->vlan_id) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000483 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), client_info->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!skb) {
Veaceslav Falico0a111a02014-07-15 19:36:02 +0200485 netdev_err(client_info->slave->bond->dev,
486 "failed to insert VLAN tag\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 continue;
488 }
489 }
490
491 arp_xmit(skb);
492 }
493}
494
495/* sends ARP REPLIES that update the clients that need updating */
496static void rlb_update_rx_clients(struct bonding *bond)
497{
498 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
499 struct rlb_client_info *client_info;
500 u32 hash_index;
501
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200502 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jiri Bohace53665c2012-11-28 04:42:14 +0000504 hash_index = bond_info->rx_hashtbl_used_head;
505 for (; hash_index != RLB_NULL_INDEX;
506 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 client_info = &(bond_info->rx_hashtbl[hash_index]);
508 if (client_info->ntt) {
509 rlb_update_client(client_info);
Wang Yufen35d75ee2014-02-14 17:15:15 +0800510 if (bond_info->rlb_update_retry_counter == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 client_info->ntt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 }
514
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200515 /* do not update the entries again until this counter is zero so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * not to confuse the clients.
517 */
518 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
519
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200520 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/* The slave was assigned a new mac address - update the clients */
524static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
525{
526 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
527 struct rlb_client_info *client_info;
528 int ntt = 0;
529 u32 hash_index;
530
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200531 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jiri Bohace53665c2012-11-28 04:42:14 +0000533 hash_index = bond_info->rx_hashtbl_used_head;
534 for (; hash_index != RLB_NULL_INDEX;
535 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 client_info = &(bond_info->rx_hashtbl[hash_index]);
537
538 if ((client_info->slave == slave) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000539 !ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 client_info->ntt = 1;
541 ntt = 1;
542 }
543 }
544
Wang Yufen4708a1b2014-02-14 17:15:13 +0800545 /* update the team's flag only after the whole iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (ntt) {
547 bond_info->rx_ntt = 1;
Wang Yufen4708a1b2014-02-14 17:15:13 +0800548 /* fasten the change */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
550 }
551
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200552 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400556static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
559 struct rlb_client_info *client_info;
560 u32 hash_index;
561
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200562 spin_lock(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Jiri Bohace53665c2012-11-28 04:42:14 +0000564 hash_index = bond_info->rx_hashtbl_used_head;
565 for (; hash_index != RLB_NULL_INDEX;
566 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 client_info = &(bond_info->rx_hashtbl[hash_index]);
568
569 if (!client_info->slave) {
Veaceslav Falico0a111a02014-07-15 19:36:02 +0200570 netdev_err(bond->dev, "found a client with no channel in the client's hash table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 continue;
572 }
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200573 /* update all clients using this src_ip, that are not assigned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * to the team's address (curr_active_slave) and have a known
575 * unicast mac address.
576 */
577 if ((client_info->ip_src == src_ip) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000578 !ether_addr_equal_64bits(client_info->slave->dev->dev_addr,
579 bond->dev->dev_addr) &&
580 !ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 client_info->ntt = 1;
582 bond_info->rx_ntt = 1;
583 }
584 }
585
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200586 spin_unlock(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
590{
591 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300592 struct arp_pkt *arp = arp_pkt(skb);
dingtianhong28c71922013-10-15 16:28:39 +0800593 struct slave *assigned_slave, *curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct rlb_client_info *client_info;
595 u32 hash_index = 0;
596
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200597 spin_lock(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
dingtianhong28c71922013-10-15 16:28:39 +0800599 curr_active_slave = rcu_dereference(bond->curr_active_slave);
600
Amerigo Wange364a342011-02-27 23:34:28 +0000601 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 client_info = &(bond_info->rx_hashtbl[hash_index]);
603
604 if (client_info->assigned) {
605 if ((client_info->ip_src == arp->ip_src) &&
606 (client_info->ip_dst == arp->ip_dst)) {
607 /* the entry is already assigned to this client */
Joe Perchesa6700db2012-05-09 17:04:04 +0000608 if (!ether_addr_equal_64bits(arp->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* update mac address from arp */
Joe Perchesada0f862014-02-15 16:02:17 -0800610 ether_addr_copy(client_info->mac_dst, arp->mac_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Joe Perchesada0f862014-02-15 16:02:17 -0800612 ether_addr_copy(client_info->mac_src, arp->mac_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 assigned_slave = client_info->slave;
615 if (assigned_slave) {
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200616 spin_unlock(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return assigned_slave;
618 }
619 } else {
620 /* the entry is already assigned to some other client,
621 * move the old client to primary (curr_active_slave) so
622 * that the new client can be assigned to this entry.
623 */
Eric Dumazet4740d632014-07-15 06:56:55 -0700624 if (curr_active_slave &&
dingtianhong28c71922013-10-15 16:28:39 +0800625 client_info->slave != curr_active_slave) {
626 client_info->slave = curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 rlb_update_client(client_info);
628 }
629 }
630 }
631 /* assign a new slave */
dingtianhong28c71922013-10-15 16:28:39 +0800632 assigned_slave = __rlb_next_rx_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (assigned_slave) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000635 if (!(client_info->assigned &&
636 client_info->ip_src == arp->ip_src)) {
637 /* ip_src is going to be updated,
638 * fix the src hash list
639 */
640 u32 hash_src = _simple_hash((u8 *)&arp->ip_src,
641 sizeof(arp->ip_src));
642 rlb_src_unlink(bond, hash_index);
643 rlb_src_link(bond, hash_src, hash_index);
644 }
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 client_info->ip_src = arp->ip_src;
647 client_info->ip_dst = arp->ip_dst;
648 /* arp->mac_dst is broadcast for arp reqeusts.
649 * will be updated with clients actual unicast mac address
650 * upon receiving an arp reply.
651 */
Joe Perchesada0f862014-02-15 16:02:17 -0800652 ether_addr_copy(client_info->mac_dst, arp->mac_dst);
653 ether_addr_copy(client_info->mac_src, arp->mac_src);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 client_info->slave = assigned_slave;
655
Joe Perchesa6700db2012-05-09 17:04:04 +0000656 if (!ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 client_info->ntt = 1;
658 bond->alb_info.rx_ntt = 1;
659 } else {
660 client_info->ntt = 0;
661 }
662
dingtianhongfb00bc22014-03-12 17:31:59 +0800663 if (vlan_get_tag(skb, &client_info->vlan_id))
Veaceslav Falicod3ab3ff2013-08-29 23:38:57 +0200664 client_info->vlan_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (!client_info->assigned) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000667 u32 prev_tbl_head = bond_info->rx_hashtbl_used_head;
668 bond_info->rx_hashtbl_used_head = hash_index;
669 client_info->used_next = prev_tbl_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (prev_tbl_head != RLB_NULL_INDEX) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000671 bond_info->rx_hashtbl[prev_tbl_head].used_prev =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 hash_index;
673 }
674 client_info->assigned = 1;
675 }
676 }
677
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200678 spin_unlock(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 return assigned_slave;
681}
682
683/* chooses (and returns) transmit channel for arp reply
684 * does not choose channel for other arp types since they are
685 * sent on the curr_active_slave
686 */
687static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
688{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300689 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct slave *tx_slave = NULL;
691
zheng.li567b8712012-11-27 23:57:04 +0000692 /* Don't modify or load balance ARPs that do not originate locally
693 * (e.g.,arrive via a bridge).
694 */
Vlad Yasevich14af9962014-06-04 16:23:38 -0400695 if (!bond_slave_has_mac_rx(bond, arp->mac_src))
zheng.li567b8712012-11-27 23:57:04 +0000696 return NULL;
697
Brian Haleyf14c4e42008-09-02 10:08:08 -0400698 if (arp->op_code == htons(ARPOP_REPLY)) {
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200699 /* the arp must be sent on the selected rx channel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 tx_slave = rlb_choose_channel(skb, bond);
Wang Yufen35d75ee2014-02-14 17:15:15 +0800701 if (tx_slave)
Joe Perchesada0f862014-02-15 16:02:17 -0800702 ether_addr_copy(arp->mac_src, tx_slave->dev->dev_addr);
Veaceslav Falico0a111a02014-07-15 19:36:02 +0200703 netdev_dbg(bond->dev, "Server sent ARP Reply packet\n");
Brian Haleyf14c4e42008-09-02 10:08:08 -0400704 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /* Create an entry in the rx_hashtbl for this client as a
706 * place holder.
707 * When the arp reply is received the entry will be updated
708 * with the correct unicast address of the client.
709 */
710 rlb_choose_channel(skb, bond);
711
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +0000712 /* The ARP reply packets must be delayed so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * they can cancel out the influence of the ARP request.
714 */
715 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
716
717 /* arp requests are broadcast and are sent on the primary
718 * the arp request will collapse all clients on the subnet to
719 * the primary slave. We must register these clients to be
720 * updated with their assigned mac.
721 */
722 rlb_req_update_subnet_clients(bond, arp->ip_src);
Veaceslav Falico0a111a02014-07-15 19:36:02 +0200723 netdev_dbg(bond->dev, "Server sent ARP Request packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
726 return tx_slave;
727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static void rlb_rebalance(struct bonding *bond)
730{
731 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
732 struct slave *assigned_slave;
733 struct rlb_client_info *client_info;
734 int ntt;
735 u32 hash_index;
736
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200737 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 ntt = 0;
Jiri Bohace53665c2012-11-28 04:42:14 +0000740 hash_index = bond_info->rx_hashtbl_used_head;
741 for (; hash_index != RLB_NULL_INDEX;
742 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 client_info = &(bond_info->rx_hashtbl[hash_index]);
dingtianhong733ab632013-12-13 10:19:45 +0800744 assigned_slave = __rlb_next_rx_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (assigned_slave && (client_info->slave != assigned_slave)) {
746 client_info->slave = assigned_slave;
747 client_info->ntt = 1;
748 ntt = 1;
749 }
750 }
751
752 /* update the team's flag only after the whole iteration */
Wang Yufen35d75ee2014-02-14 17:15:15 +0800753 if (ntt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 bond_info->rx_ntt = 1;
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200755 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200758/* Caller must hold mode_lock */
Jiri Bohace53665c2012-11-28 04:42:14 +0000759static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
760{
761 entry->used_next = RLB_NULL_INDEX;
762 entry->used_prev = RLB_NULL_INDEX;
763 entry->assigned = 0;
764 entry->slave = NULL;
Veaceslav Falicod3ab3ff2013-08-29 23:38:57 +0200765 entry->vlan_id = 0;
Jiri Bohace53665c2012-11-28 04:42:14 +0000766}
767static void rlb_init_table_entry_src(struct rlb_client_info *entry)
768{
769 entry->src_first = RLB_NULL_INDEX;
770 entry->src_prev = RLB_NULL_INDEX;
771 entry->src_next = RLB_NULL_INDEX;
772}
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static void rlb_init_table_entry(struct rlb_client_info *entry)
775{
776 memset(entry, 0, sizeof(struct rlb_client_info));
Jiri Bohace53665c2012-11-28 04:42:14 +0000777 rlb_init_table_entry_dst(entry);
778 rlb_init_table_entry_src(entry);
779}
780
781static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
782{
783 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
784 u32 next_index = bond_info->rx_hashtbl[index].used_next;
785 u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
786
787 if (index == bond_info->rx_hashtbl_used_head)
788 bond_info->rx_hashtbl_used_head = next_index;
789 if (prev_index != RLB_NULL_INDEX)
790 bond_info->rx_hashtbl[prev_index].used_next = next_index;
791 if (next_index != RLB_NULL_INDEX)
792 bond_info->rx_hashtbl[next_index].used_prev = prev_index;
793}
794
795/* unlink a rlb hash table entry from the src list */
796static void rlb_src_unlink(struct bonding *bond, u32 index)
797{
798 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
799 u32 next_index = bond_info->rx_hashtbl[index].src_next;
800 u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
801
802 bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
803 bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
804
805 if (next_index != RLB_NULL_INDEX)
806 bond_info->rx_hashtbl[next_index].src_prev = prev_index;
807
808 if (prev_index == RLB_NULL_INDEX)
809 return;
810
811 /* is prev_index pointing to the head of this list? */
812 if (bond_info->rx_hashtbl[prev_index].src_first == index)
813 bond_info->rx_hashtbl[prev_index].src_first = next_index;
814 else
815 bond_info->rx_hashtbl[prev_index].src_next = next_index;
816
817}
818
819static void rlb_delete_table_entry(struct bonding *bond, u32 index)
820{
821 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
822 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
823
824 rlb_delete_table_entry_dst(bond, index);
825 rlb_init_table_entry_dst(entry);
826
827 rlb_src_unlink(bond, index);
828}
829
830/* add the rx_hashtbl[ip_dst_hash] entry to the list
831 * of entries with identical ip_src_hash
832 */
833static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
834{
835 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
836 u32 next;
837
838 bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
839 next = bond_info->rx_hashtbl[ip_src_hash].src_first;
840 bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
841 if (next != RLB_NULL_INDEX)
842 bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
843 bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
844}
845
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +0200846/* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
847 * not match arp->mac_src
848 */
Jiri Bohace53665c2012-11-28 04:42:14 +0000849static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
850{
851 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Wang Yufenb85b6fb2014-02-14 17:15:12 +0800852 u32 ip_src_hash = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src));
Jiri Bohace53665c2012-11-28 04:42:14 +0000853 u32 index;
854
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200855 spin_lock_bh(&bond->mode_lock);
Jiri Bohace53665c2012-11-28 04:42:14 +0000856
857 index = bond_info->rx_hashtbl[ip_src_hash].src_first;
858 while (index != RLB_NULL_INDEX) {
859 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
860 u32 next_index = entry->src_next;
861 if (entry->ip_src == arp->ip_src &&
862 !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
863 rlb_delete_table_entry(bond, index);
864 index = next_index;
865 }
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200866 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static int rlb_initialize(struct bonding *bond)
870{
871 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mitch Williams0d206a32005-11-09 10:35:30 -0800872 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
874 int i;
875
Mitch Williams0d206a32005-11-09 10:35:30 -0800876 new_hashtbl = kmalloc(size, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000877 if (!new_hashtbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return -1;
Joe Perchese404dec2012-01-29 12:56:23 +0000879
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200880 spin_lock_bh(&bond->mode_lock);
Mitch Williams0d206a32005-11-09 10:35:30 -0800881
882 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Jiri Bohace53665c2012-11-28 04:42:14 +0000884 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Wang Yufen35d75ee2014-02-14 17:15:15 +0800886 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 rlb_init_table_entry(bond_info->rx_hashtbl + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200889 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* register to receive ARPs */
Jiri Pirko3aba8912011-04-19 03:48:16 +0000892 bond->recv_probe = rlb_arp_recv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 return 0;
895}
896
897static void rlb_deinitialize(struct bonding *bond)
898{
899 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
900
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200901 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 kfree(bond_info->rx_hashtbl);
904 bond_info->rx_hashtbl = NULL;
Jiri Bohace53665c2012-11-28 04:42:14 +0000905 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200907 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
911{
912 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
913 u32 curr_index;
914
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200915 spin_lock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Jiri Bohace53665c2012-11-28 04:42:14 +0000917 curr_index = bond_info->rx_hashtbl_used_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 while (curr_index != RLB_NULL_INDEX) {
919 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
Jiri Bohace53665c2012-11-28 04:42:14 +0000920 u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Veaceslav Falicod3ab3ff2013-08-29 23:38:57 +0200922 if (curr->vlan_id == vlan_id)
Jiri Bohace53665c2012-11-28 04:42:14 +0000923 rlb_delete_table_entry(bond, curr_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 curr_index = next_index;
926 }
927
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +0200928 spin_unlock_bh(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
931/*********************** tlb/rlb shared functions *********************/
932
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200933static void alb_send_lp_vid(struct slave *slave, u8 mac_addr[],
Vlad Yasevichd6b694c2014-05-21 11:24:39 -0400934 __be16 vlan_proto, u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct learning_pkt pkt;
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200937 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 int size = sizeof(struct learning_pkt);
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200939 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 memset(&pkt, 0, size);
Joe Perchesada0f862014-02-15 16:02:17 -0800942 ether_addr_copy(pkt.mac_dst, mac_addr);
943 ether_addr_copy(pkt.mac_src, mac_addr);
Veaceslav Falico96a09222014-03-13 12:41:58 +0100944 pkt.type = cpu_to_be16(ETH_P_LOOPBACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200946 skb = dev_alloc_skb(size);
947 if (!skb)
948 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200950 data = skb_put(skb, size);
951 memcpy(data, &pkt, size);
952
953 skb_reset_mac_header(skb);
954 skb->network_header = skb->mac_header + ETH_HLEN;
955 skb->protocol = pkt.type;
956 skb->priority = TC_PRIO_CONTROL;
957 skb->dev = slave->dev;
958
959 if (vid) {
Vlad Yasevichd6b694c2014-05-21 11:24:39 -0400960 skb = vlan_put_tag(skb, vlan_proto, vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (!skb) {
Veaceslav Falico0a111a02014-07-15 19:36:02 +0200962 netdev_err(slave->bond->dev, "failed to insert VLAN tag\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return;
964 }
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200967 dev_queue_xmit(skb);
968}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Vlad Yasevichd0c21d42014-05-21 13:19:48 -0400970static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[],
971 bool strict_match)
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200972{
973 struct bonding *bond = bond_get_bond_by_slave(slave);
Veaceslav Falico5bf94b82013-08-28 23:25:14 +0200974 struct net_device *upper;
975 struct list_head *iter;
Veaceslav Falico3e403a72014-07-17 17:02:23 +0200976 struct bond_vlan_tag *tags;
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200977
Veaceslav Falico5bf94b82013-08-28 23:25:14 +0200978 /* send untagged */
Vlad Yasevichd6b694c2014-05-21 11:24:39 -0400979 alb_send_lp_vid(slave, mac_addr, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Vlad Yasevich14af9962014-06-04 16:23:38 -0400981 /* loop through all devices and see if we need to send a packet
982 * for that device.
983 */
Veaceslav Falico5bf94b82013-08-28 23:25:14 +0200984 rcu_read_lock();
Veaceslav Falico2f268f12013-09-25 09:20:07 +0200985 netdev_for_each_all_upper_dev_rcu(bond->dev, upper, iter) {
Vlad Yasevichd0c21d42014-05-21 13:19:48 -0400986 if (is_vlan_dev(upper) && vlan_get_encap_level(upper) == 0) {
987 if (strict_match &&
988 ether_addr_equal_64bits(mac_addr,
989 upper->dev_addr)) {
990 alb_send_lp_vid(slave, mac_addr,
991 vlan_dev_vlan_proto(upper),
992 vlan_dev_vlan_id(upper));
993 } else if (!strict_match) {
994 alb_send_lp_vid(slave, upper->dev_addr,
995 vlan_dev_vlan_proto(upper),
996 vlan_dev_vlan_id(upper));
997 }
998 }
Vlad Yasevich14af9962014-06-04 16:23:38 -0400999
1000 /* If this is a macvlan device, then only send updates
1001 * when strict_match is turned off.
1002 */
1003 if (netif_is_macvlan(upper) && !strict_match) {
Veaceslav Falico3e403a72014-07-17 17:02:23 +02001004 tags = bond_verify_device_path(bond->dev, upper, 0);
1005 if (IS_ERR_OR_NULL(tags))
1006 BUG();
Vlad Yasevich14af9962014-06-04 16:23:38 -04001007 alb_send_lp_vid(slave, upper->dev_addr,
1008 tags[0].vlan_proto, tags[0].vlan_id);
Veaceslav Falico3e403a72014-07-17 17:02:23 +02001009 kfree(tags);
Vlad Yasevich14af9962014-06-04 16:23:38 -04001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Veaceslav Falico5bf94b82013-08-28 23:25:14 +02001012 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
Jiri Bohacb9245512012-01-18 12:24:54 +00001015static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 struct net_device *dev = slave->dev;
1018 struct sockaddr s_addr;
1019
Veaceslav Falico01844092014-05-15 21:39:55 +02001020 if (BOND_MODE(slave->bond) == BOND_MODE_TLB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 memcpy(dev->dev_addr, addr, dev->addr_len);
1022 return 0;
1023 }
1024
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001025 /* for rlb each slave must have a unique hw mac addresses so that
1026 * each slave will receive packets destined to a different mac
1027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 memcpy(s_addr.sa_data, addr, dev->addr_len);
1029 s_addr.sa_family = dev->type;
1030 if (dev_set_mac_address(dev, &s_addr)) {
Veaceslav Falico0a111a02014-07-15 19:36:02 +02001031 netdev_err(slave->bond->dev, "dev_set_mac_address of dev %s failed! ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
1032 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return -EOPNOTSUPP;
1034 }
1035 return 0;
1036}
1037
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001038/* Swap MAC addresses between two slaves.
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001039 *
1040 * Called with RTNL held, and no other locks.
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001041 */
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001042static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Joe Perchesada0f862014-02-15 16:02:17 -08001046 ether_addr_copy(tmp_mac_addr, slave1->dev->dev_addr);
Jiri Bohacb9245512012-01-18 12:24:54 +00001047 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
1048 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001050}
1051
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001052/* Send learning packets after MAC address swap.
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001053 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001054 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001055 */
1056static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
1057 struct slave *slave2)
1058{
Veaceslav Falico8557cd72014-05-15 21:39:59 +02001059 int slaves_state_differ = (bond_slave_can_tx(slave1) != bond_slave_can_tx(slave2));
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001060 struct slave *disabled_slave = NULL;
1061
Jay Vosburgh25433312008-01-17 16:24:59 -08001062 ASSERT_RTNL();
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* fasten the change in the switch */
Veaceslav Falico8557cd72014-05-15 21:39:59 +02001065 if (bond_slave_can_tx(slave1)) {
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001066 alb_send_learning_packets(slave1, slave1->dev->dev_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (bond->alb_info.rlb_enabled) {
1068 /* inform the clients that the mac address
1069 * has changed
1070 */
1071 rlb_req_update_slave_clients(bond, slave1);
1072 }
1073 } else {
1074 disabled_slave = slave1;
1075 }
1076
Veaceslav Falico8557cd72014-05-15 21:39:59 +02001077 if (bond_slave_can_tx(slave2)) {
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001078 alb_send_learning_packets(slave2, slave2->dev->dev_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (bond->alb_info.rlb_enabled) {
1080 /* inform the clients that the mac address
1081 * has changed
1082 */
1083 rlb_req_update_slave_clients(bond, slave2);
1084 }
1085 } else {
1086 disabled_slave = slave2;
1087 }
1088
1089 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1090 /* A disabled slave was assigned an active mac addr */
1091 rlb_teach_disabled_mac_on_primary(bond,
1092 disabled_slave->dev->dev_addr);
1093 }
1094}
1095
1096/**
1097 * alb_change_hw_addr_on_detach
1098 * @bond: bonding we're working on
1099 * @slave: the slave that was just detached
1100 *
1101 * We assume that @slave was already detached from the slave list.
1102 *
1103 * If @slave's permanent hw address is different both from its current
1104 * address and from @bond's address, then somewhere in the bond there's
1105 * a slave that has @slave's permanet address as its current address.
1106 * We'll make sure that that slave no longer uses @slave's permanent address.
1107 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001108 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 */
1110static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1111{
1112 int perm_curr_diff;
1113 int perm_bond_diff;
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001114 struct slave *found_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Joe Perchesa6700db2012-05-09 17:04:04 +00001116 perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1117 slave->dev->dev_addr);
1118 perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1119 bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (perm_curr_diff && perm_bond_diff) {
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001122 found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001124 if (found_slave) {
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001125 alb_swap_mac_addr(slave, found_slave);
1126 alb_fasten_mac_swap(bond, slave, found_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 }
1129}
1130
1131/**
1132 * alb_handle_addr_collision_on_attach
1133 * @bond: bonding we're working on
1134 * @slave: the slave that was just attached
1135 *
1136 * checks uniqueness of slave's mac address and handles the case the
1137 * new slave uses the bonds mac address.
1138 *
1139 * If the permanent hw address of @slave is @bond's hw address, we need to
1140 * find a different hw address to give @slave, that isn't in use by any other
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +00001141 * slave in the bond. This address must be, of course, one of the permanent
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * addresses of the other slaves.
1143 *
1144 * We go over the slave list, and for each slave there we compare its
1145 * permanent hw address with the current address of all the other slaves.
1146 * If no match was found, then we've found a slave with a permanent address
1147 * that isn't used by any other slave in the bond, so we can assign it to
1148 * @slave.
1149 *
1150 * assumption: this function is called before @slave is attached to the
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001151 * bond slave list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
1153static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1154{
Eric Dumazet4740d632014-07-15 06:56:55 -07001155 struct slave *has_bond_addr = rcu_access_pointer(bond->curr_active_slave);
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001156 struct slave *tmp_slave1, *free_mac_slave = NULL;
1157 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001159 if (!bond_has_slaves(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /* this is the first slave */
1161 return 0;
1162 }
1163
1164 /* if slave's mac address differs from bond's mac address
1165 * check uniqueness of slave's mac address against the other
1166 * slaves in the bond.
1167 */
Joe Perchesa6700db2012-05-09 17:04:04 +00001168 if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001169 if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
John W. Linville6b38aef2005-07-28 15:00:15 -04001170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
John W. Linville6b38aef2005-07-28 15:00:15 -04001172 /* Try setting slave mac to bond address and fall-through
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001173 * to code handling that situation below...
1174 */
Jiri Bohacb9245512012-01-18 12:24:54 +00001175 alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
1178 /* The slave's address is equal to the address of the bond.
1179 * Search for a spare address in the bond for this slave.
1180 */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001181 bond_for_each_slave(bond, tmp_slave1, iter) {
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001182 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* no slave has tmp_slave1's perm addr
1184 * as its curr addr
1185 */
1186 free_mac_slave = tmp_slave1;
1187 break;
1188 }
1189
1190 if (!has_bond_addr) {
Joe Perchesa6700db2012-05-09 17:04:04 +00001191 if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
1192 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 has_bond_addr = tmp_slave1;
1195 }
1196 }
1197 }
1198
1199 if (free_mac_slave) {
Jiri Bohacb9245512012-01-18 12:24:54 +00001200 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Veaceslav Falico0a111a02014-07-15 19:36:02 +02001202 netdev_warn(bond->dev, "the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1203 slave->dev->name, free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 } else if (has_bond_addr) {
Veaceslav Falico0a111a02014-07-15 19:36:02 +02001206 netdev_err(bond->dev, "the hw address of slave %s 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",
1207 slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return -EFAULT;
1209 }
1210
1211 return 0;
1212}
1213
1214/**
1215 * alb_set_mac_address
1216 * @bond:
1217 * @addr:
1218 *
1219 * In TLB mode all slaves are configured to the bond's hw address, but set
1220 * their dev_addr field to different addresses (based on their permanent hw
1221 * addresses).
1222 *
1223 * For each slave, this function sets the interface to the new address and then
1224 * changes its dev_addr field to its previous value.
1225 *
1226 * Unwinding assumes bond's mac address has not yet changed.
1227 */
1228static int alb_set_mac_address(struct bonding *bond, void *addr)
1229{
Veaceslav Falico81f23b12013-09-25 09:20:13 +02001230 struct slave *slave, *rollback_slave;
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001231 struct list_head *iter;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001232 struct sockaddr sa;
Veaceslav Falico81f23b12013-09-25 09:20:13 +02001233 char tmp_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001236 if (bond->alb_info.rlb_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001239 bond_for_each_slave(bond, slave, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* save net_device's current hw address */
Joe Perchesada0f862014-02-15 16:02:17 -08001241 ether_addr_copy(tmp_addr, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 res = dev_set_mac_address(slave->dev, addr);
1244
1245 /* restore net_device's hw address */
Joe Perchesada0f862014-02-15 16:02:17 -08001246 ether_addr_copy(slave->dev->dev_addr, tmp_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001248 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
1252 return 0;
1253
1254unwind:
1255 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1256 sa.sa_family = bond->dev->type;
1257
1258 /* unwind from head to the slave that failed */
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001259 bond_for_each_slave(bond, rollback_slave, iter) {
Veaceslav Falico81f23b12013-09-25 09:20:13 +02001260 if (rollback_slave == slave)
1261 break;
Joe Perchesada0f862014-02-15 16:02:17 -08001262 ether_addr_copy(tmp_addr, rollback_slave->dev->dev_addr);
Veaceslav Falico81f23b12013-09-25 09:20:13 +02001263 dev_set_mac_address(rollback_slave->dev, &sa);
Joe Perchesada0f862014-02-15 16:02:17 -08001264 ether_addr_copy(rollback_slave->dev->dev_addr, tmp_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
1267 return res;
1268}
1269
1270/************************ exported alb funcions ************************/
1271
1272int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1273{
1274 int res;
1275
1276 res = tlb_initialize(bond);
Wang Yufen35d75ee2014-02-14 17:15:15 +08001277 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (rlb_enabled) {
1281 bond->alb_info.rlb_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 res = rlb_initialize(bond);
1283 if (res) {
1284 tlb_deinitialize(bond);
1285 return res;
1286 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001287 } else {
1288 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291 return 0;
1292}
1293
1294void bond_alb_deinitialize(struct bonding *bond)
1295{
1296 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1297
1298 tlb_deinitialize(bond);
1299
Wang Yufendda0fd52014-02-14 17:15:16 +08001300 if (bond_info->rlb_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 rlb_deinitialize(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Mahesh Bandewar9a49aba2014-04-22 16:30:18 -07001304static int bond_do_alb_xmit(struct sk_buff *skb, struct bonding *bond,
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001305 struct slave *tx_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mahesh Bandewar9a49aba2014-04-22 16:30:18 -07001308 struct ethhdr *eth_data = eth_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (!tx_slave) {
1311 /* unbalanced or unassigned, send through primary */
dingtianhong28c71922013-10-15 16:28:39 +08001312 tx_slave = rcu_dereference(bond->curr_active_slave);
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07001313 if (bond->params.tlb_dynamic_lb)
1314 bond_info->unbalanced_load += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
Veaceslav Falico8557cd72014-05-15 21:39:59 +02001317 if (tx_slave && bond_slave_can_tx(tx_slave)) {
Andreea-Cristina Bernatb5091b52014-08-17 13:21:45 +03001318 if (tx_slave != rcu_access_pointer(bond->curr_active_slave)) {
Joe Perches2a7c1832014-02-18 09:42:45 -08001319 ether_addr_copy(eth_data->h_source,
1320 tx_slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
dingtianhong4d4ac1b2014-01-02 09:13:02 +08001323 bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1324 goto out;
Joe Perches157550fb2014-02-18 09:42:46 -08001325 }
1326
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07001327 if (tx_slave && bond->params.tlb_dynamic_lb) {
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +02001328 spin_lock(&bond->mode_lock);
Joe Perches157550fb2014-02-18 09:42:46 -08001329 __tlb_clear_slave(bond, tx_slave, 0);
Nikolay Aleksandrov4bab16d2014-09-11 22:49:26 +02001330 spin_unlock(&bond->mode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
dingtianhong4d4ac1b2014-01-02 09:13:02 +08001333 /* no suitable interface, frame not sent */
Eric W. Biederman2bb77ab2014-03-11 14:16:58 -07001334 dev_kfree_skb_any(skb);
dingtianhong4d4ac1b2014-01-02 09:13:02 +08001335out:
Patrick McHardyec634fe2009-07-05 19:23:38 -07001336 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Mahesh Bandewar6b794c12014-07-16 11:10:36 -07001339static int bond_tlb_update_slave_arr(struct bonding *bond,
1340 struct slave *skipslave)
1341{
1342 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1343 struct slave *tx_slave;
1344 struct list_head *iter;
1345 struct tlb_up_slave *new_arr, *old_arr;
1346
1347 new_arr = kzalloc(offsetof(struct tlb_up_slave, arr[bond->slave_cnt]),
1348 GFP_ATOMIC);
1349 if (!new_arr)
1350 return -ENOMEM;
1351
1352 bond_for_each_slave(bond, tx_slave, iter) {
1353 if (!bond_slave_can_tx(tx_slave))
1354 continue;
1355 if (skipslave == tx_slave)
1356 continue;
1357 new_arr->arr[new_arr->count++] = tx_slave;
1358 }
1359
1360 old_arr = rtnl_dereference(bond_info->slave_arr);
1361 rcu_assign_pointer(bond_info->slave_arr, new_arr);
1362 if (old_arr)
1363 kfree_rcu(old_arr, rcu);
1364
1365 return 0;
1366}
1367
Mahesh Bandewarf05b42e2014-04-22 16:30:20 -07001368int bond_tlb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1369{
1370 struct bonding *bond = netdev_priv(bond_dev);
Mahesh Bandewar6b794c12014-07-16 11:10:36 -07001371 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mahesh Bandewarf05b42e2014-04-22 16:30:20 -07001372 struct ethhdr *eth_data;
1373 struct slave *tx_slave = NULL;
1374 u32 hash_index;
1375
1376 skb_reset_mac_header(skb);
1377 eth_data = eth_hdr(skb);
1378
1379 /* Do not TX balance any multicast or broadcast */
1380 if (!is_multicast_ether_addr(eth_data->h_dest)) {
1381 switch (skb->protocol) {
1382 case htons(ETH_P_IP):
1383 case htons(ETH_P_IPX):
1384 /* In case of IPX, it will falback to L2 hash */
1385 case htons(ETH_P_IPV6):
1386 hash_index = bond_xmit_hash(bond, skb);
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07001387 if (bond->params.tlb_dynamic_lb) {
1388 tx_slave = tlb_choose_channel(bond,
1389 hash_index & 0xFF,
1390 skb->len);
1391 } else {
Mahesh Bandewar6b794c12014-07-16 11:10:36 -07001392 struct tlb_up_slave *slaves;
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07001393
Mahesh Bandewar6b794c12014-07-16 11:10:36 -07001394 slaves = rcu_dereference(bond_info->slave_arr);
1395 if (slaves && slaves->count)
1396 tx_slave = slaves->arr[hash_index %
1397 slaves->count];
Mahesh Bandeware9f0fb82014-04-22 16:30:22 -07001398 }
Mahesh Bandewarf05b42e2014-04-22 16:30:20 -07001399 break;
1400 }
1401 }
Mahesh Bandewarf05b42e2014-04-22 16:30:20 -07001402 return bond_do_alb_xmit(skb, bond, tx_slave);
1403}
1404
Mahesh Bandewar9a49aba2014-04-22 16:30:18 -07001405int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1406{
1407 struct bonding *bond = netdev_priv(bond_dev);
1408 struct ethhdr *eth_data;
1409 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1410 struct slave *tx_slave = NULL;
1411 static const __be32 ip_bcast = htonl(0xffffffff);
1412 int hash_size = 0;
1413 bool do_tx_balance = true;
1414 u32 hash_index = 0;
1415 const u8 *hash_start = NULL;
1416 struct ipv6hdr *ip6hdr;
1417
1418 skb_reset_mac_header(skb);
1419 eth_data = eth_hdr(skb);
1420
1421 switch (ntohs(skb->protocol)) {
1422 case ETH_P_IP: {
1423 const struct iphdr *iph = ip_hdr(skb);
1424
1425 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
1426 (iph->daddr == ip_bcast) ||
1427 (iph->protocol == IPPROTO_IGMP)) {
1428 do_tx_balance = false;
1429 break;
1430 }
1431 hash_start = (char *)&(iph->daddr);
1432 hash_size = sizeof(iph->daddr);
1433 }
1434 break;
1435 case ETH_P_IPV6:
1436 /* IPv6 doesn't really use broadcast mac address, but leave
1437 * that here just in case.
1438 */
1439 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast)) {
1440 do_tx_balance = false;
1441 break;
1442 }
1443
1444 /* IPv6 uses all-nodes multicast as an equivalent to
1445 * broadcasts in IPv4.
1446 */
1447 if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1448 do_tx_balance = false;
1449 break;
1450 }
1451
1452 /* Additianally, DAD probes should not be tx-balanced as that
1453 * will lead to false positives for duplicate addresses and
1454 * prevent address configuration from working.
1455 */
1456 ip6hdr = ipv6_hdr(skb);
1457 if (ipv6_addr_any(&ip6hdr->saddr)) {
1458 do_tx_balance = false;
1459 break;
1460 }
1461
1462 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1463 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1464 break;
1465 case ETH_P_IPX:
1466 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1467 /* something is wrong with this packet */
1468 do_tx_balance = false;
1469 break;
1470 }
1471
1472 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1473 /* The only protocol worth balancing in
1474 * this family since it has an "ARP" like
1475 * mechanism
1476 */
1477 do_tx_balance = false;
1478 break;
1479 }
1480
1481 hash_start = (char *)eth_data->h_dest;
1482 hash_size = ETH_ALEN;
1483 break;
1484 case ETH_P_ARP:
1485 do_tx_balance = false;
1486 if (bond_info->rlb_enabled)
1487 tx_slave = rlb_arp_xmit(skb, bond);
1488 break;
1489 default:
1490 do_tx_balance = false;
1491 break;
1492 }
1493
1494 if (do_tx_balance) {
1495 hash_index = _simple_hash(hash_start, hash_size);
1496 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1497 }
1498
1499 return bond_do_alb_xmit(skb, bond, tx_slave);
1500}
1501
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001502void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001504 struct bonding *bond = container_of(work, struct bonding,
1505 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Veaceslav Falico9caff1e72013-09-25 09:20:14 +02001507 struct list_head *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001510 if (!bond_has_slaves(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 bond_info->tx_rebalance_counter = 0;
1512 bond_info->lp_counter = 0;
1513 goto re_arm;
1514 }
1515
dingtianhong733ab632013-12-13 10:19:45 +08001516 rcu_read_lock();
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 bond_info->tx_rebalance_counter++;
1519 bond_info->lp_counter++;
1520
1521 /* send learning packets */
Neil Horman7eacd032013-09-13 11:05:33 -04001522 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS(bond)) {
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001523 bool strict_match;
1524
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001525 bond_for_each_slave_rcu(bond, slave, iter) {
1526 /* If updating current_active, use all currently
1527 * user mac addreses (!strict_match). Otherwise, only
1528 * use mac of the slave device.
Vlad Yasevich14af9962014-06-04 16:23:38 -04001529 * In RLB mode, we always use strict matches.
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001530 */
Eric Dumazet4740d632014-07-15 06:56:55 -07001531 strict_match = (slave != rcu_access_pointer(bond->curr_active_slave) ||
Vlad Yasevich14af9962014-06-04 16:23:38 -04001532 bond_info->rlb_enabled);
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001533 alb_send_learning_packets(slave, slave->dev->dev_addr,
1534 strict_match);
1535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 bond_info->lp_counter = 0;
1537 }
1538
1539 /* rebalance tx traffic */
1540 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
dingtianhong733ab632013-12-13 10:19:45 +08001541 bond_for_each_slave_rcu(bond, slave, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 tlb_clear_slave(bond, slave, 1);
Eric Dumazet4740d632014-07-15 06:56:55 -07001543 if (slave == rcu_access_pointer(bond->curr_active_slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 SLAVE_TLB_INFO(slave).load =
1545 bond_info->unbalanced_load /
1546 BOND_TLB_REBALANCE_INTERVAL;
1547 bond_info->unbalanced_load = 0;
1548 }
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 bond_info->tx_rebalance_counter = 0;
1551 }
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (bond_info->primary_is_promisc &&
1555 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1556
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001557 /* dev_set_promiscuity requires rtnl and
David S. Miller1f2cd842013-10-28 00:11:22 -04001558 * nothing else. Avoid race with bond_close.
1559 */
dingtianhong733ab632013-12-13 10:19:45 +08001560 rcu_read_unlock();
1561 if (!rtnl_trylock())
David S. Miller1f2cd842013-10-28 00:11:22 -04001562 goto re_arm;
David S. Miller1f2cd842013-10-28 00:11:22 -04001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 bond_info->rlb_promisc_timeout_counter = 0;
1565
1566 /* If the primary was set to promiscuous mode
1567 * because a slave was disabled then
1568 * it can now leave promiscuous mode.
1569 */
Eric Dumazet4740d632014-07-15 06:56:55 -07001570 dev_set_promiscuity(rtnl_dereference(bond->curr_active_slave)->dev,
1571 -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 bond_info->primary_is_promisc = 0;
David S. Miller1f2cd842013-10-28 00:11:22 -04001573
1574 rtnl_unlock();
dingtianhong733ab632013-12-13 10:19:45 +08001575 rcu_read_lock();
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 if (bond_info->rlb_rebalance) {
1579 bond_info->rlb_rebalance = 0;
1580 rlb_rebalance(bond);
1581 }
1582
1583 /* check if clients need updating */
1584 if (bond_info->rx_ntt) {
1585 if (bond_info->rlb_update_delay_counter) {
1586 --bond_info->rlb_update_delay_counter;
1587 } else {
1588 rlb_update_rx_clients(bond);
Wang Yufendda0fd52014-02-14 17:15:16 +08001589 if (bond_info->rlb_update_retry_counter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 --bond_info->rlb_update_retry_counter;
Wang Yufendda0fd52014-02-14 17:15:16 +08001591 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 bond_info->rx_ntt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594 }
1595 }
dingtianhong733ab632013-12-13 10:19:45 +08001596 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001598 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601/* assumption: called before the slave is attached to the bond
1602 * and not locked by the bond lock
1603 */
1604int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1605{
1606 int res;
1607
Jiri Bohacb9245512012-01-18 12:24:54 +00001608 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
Wang Yufendda0fd52014-02-14 17:15:16 +08001609 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 res = alb_handle_addr_collision_on_attach(bond, slave);
Wang Yufendda0fd52014-02-14 17:15:16 +08001613 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 tlb_init_slave(slave);
1617
1618 /* order a rebalance ASAP */
1619 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1620
Wang Yufendda0fd52014-02-14 17:15:16 +08001621 if (bond->alb_info.rlb_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 bond->alb_info.rlb_rebalance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 return 0;
1625}
1626
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001627/* Remove slave from tlb and rlb hash tables, and fix up MAC addresses
Jay Vosburgh25433312008-01-17 16:24:59 -08001628 * if necessary.
1629 *
1630 * Caller must hold RTNL and no other locks
1631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1633{
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001634 if (bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 alb_change_hw_addr_on_detach(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 tlb_clear_slave(bond, slave, 0);
1638
1639 if (bond->alb_info.rlb_enabled) {
Veaceslav Falico6475ae42013-09-25 09:20:17 +02001640 bond->alb_info.rx_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 rlb_clear_slave(bond, slave);
1642 }
Mahesh Bandewar6b794c12014-07-16 11:10:36 -07001643
1644 if (bond_is_nondyn_tlb(bond))
1645 if (bond_tlb_update_slave_arr(bond, slave))
1646 pr_err("Failed to build slave-array for TLB mode.\n");
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1651{
1652 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1653
1654 if (link == BOND_LINK_DOWN) {
1655 tlb_clear_slave(bond, slave, 0);
Wang Yufen73ac0cd2014-02-14 17:15:17 +08001656 if (bond->alb_info.rlb_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 rlb_clear_slave(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 } else if (link == BOND_LINK_UP) {
1659 /* order a rebalance ASAP */
1660 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1661 if (bond->alb_info.rlb_enabled) {
1662 bond->alb_info.rlb_rebalance = 1;
1663 /* If the updelay module parameter is smaller than the
1664 * forwarding delay of the switch the rebalance will
1665 * not work because the rebalance arp replies will
1666 * not be forwarded to the clients..
1667 */
1668 }
1669 }
Mahesh Bandewar6b794c12014-07-16 11:10:36 -07001670
1671 if (bond_is_nondyn_tlb(bond)) {
1672 if (bond_tlb_update_slave_arr(bond, NULL))
1673 pr_err("Failed to build slave-array for TLB mode.\n");
1674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
1677/**
1678 * bond_alb_handle_active_change - assign new curr_active_slave
1679 * @bond: our bonding struct
1680 * @new_slave: new slave to assign
1681 *
1682 * Set the bond->curr_active_slave to @new_slave and handle
1683 * mac address swapping and promiscuity changes as needed.
1684 *
Nikolay Aleksandrov62c5f512014-09-11 22:49:23 +02001685 * Caller must hold RTNL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
1687void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1688{
1689 struct slave *swap_slave;
Eric Dumazet4740d632014-07-15 06:56:55 -07001690 struct slave *curr_active;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Nikolay Aleksandrov62c5f512014-09-11 22:49:23 +02001692 curr_active = rtnl_dereference(bond->curr_active_slave);
Eric Dumazet4740d632014-07-15 06:56:55 -07001693 if (curr_active == new_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Eric Dumazet4740d632014-07-15 06:56:55 -07001696 if (curr_active && bond->alb_info.primary_is_promisc) {
1697 dev_set_promiscuity(curr_active->dev, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 bond->alb_info.primary_is_promisc = 0;
1699 bond->alb_info.rlb_promisc_timeout_counter = 0;
1700 }
1701
Eric Dumazet4740d632014-07-15 06:56:55 -07001702 swap_slave = curr_active;
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001703 rcu_assign_pointer(bond->curr_active_slave, new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Veaceslav Falico0965a1f2013-09-25 09:20:21 +02001705 if (!new_slave || !bond_has_slaves(bond))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 /* set the new curr_active_slave to the bonds mac address
1709 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1710 */
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001711 if (!swap_slave)
1712 swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Nikolay Aleksandrov547942c2014-09-15 17:19:34 +02001714 /* Arrange for swap_slave and new_slave to temporarily be
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001715 * ignored so we can mess with their MAC addresses without
1716 * fear of interference from transmit activity.
1717 */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001718 if (swap_slave)
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001719 tlb_clear_slave(bond, swap_slave, 1);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001720 tlb_clear_slave(bond, new_slave, 1);
1721
Veaceslav Falico4996b902013-10-07 09:17:20 +02001722 /* in TLB mode, the slave might flip down/up with the old dev_addr,
1723 * and thus filter bond->dev_addr's packets, so force bond's mac
1724 */
Veaceslav Falico01844092014-05-15 21:39:55 +02001725 if (BOND_MODE(bond) == BOND_MODE_TLB) {
Veaceslav Falico4996b902013-10-07 09:17:20 +02001726 struct sockaddr sa;
1727 u8 tmp_addr[ETH_ALEN];
1728
Joe Perchesada0f862014-02-15 16:02:17 -08001729 ether_addr_copy(tmp_addr, new_slave->dev->dev_addr);
Veaceslav Falico4996b902013-10-07 09:17:20 +02001730
1731 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1732 sa.sa_family = bond->dev->type;
1733 /* we don't care if it can't change its mac, best effort */
1734 dev_set_mac_address(new_slave->dev, &sa);
1735
Joe Perchesada0f862014-02-15 16:02:17 -08001736 ether_addr_copy(new_slave->dev->dev_addr, tmp_addr);
Veaceslav Falico4996b902013-10-07 09:17:20 +02001737 }
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1740 if (swap_slave) {
1741 /* swap mac address */
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001742 alb_swap_mac_addr(swap_slave, new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001743 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1744 } else {
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001745 /* set the new_slave to the bond mac address */
1746 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001747 alb_send_learning_packets(new_slave, bond->dev->dev_addr,
1748 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750}
1751
Nikolay Aleksandrovecfede42014-09-09 23:16:59 +02001752/* Called with RTNL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1754{
Wang Chen454d7c92008-11-12 23:37:49 -08001755 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 struct sockaddr *sa = addr;
Eric Dumazet4740d632014-07-15 06:56:55 -07001757 struct slave *curr_active;
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001758 struct slave *swap_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Wang Yufen73ac0cd2014-02-14 17:15:17 +08001761 if (!is_valid_ether_addr(sa->sa_data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 res = alb_set_mac_address(bond, addr);
Wang Yufen73ac0cd2014-02-14 17:15:17 +08001765 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1769
1770 /* If there is no curr_active_slave there is nothing else to do.
1771 * Otherwise we'll need to pass the new address to it and handle
1772 * duplications.
1773 */
Eric Dumazet4740d632014-07-15 06:56:55 -07001774 curr_active = rtnl_dereference(bond->curr_active_slave);
1775 if (!curr_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001778 swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 if (swap_slave) {
Eric Dumazet4740d632014-07-15 06:56:55 -07001781 alb_swap_mac_addr(swap_slave, curr_active);
1782 alb_fasten_mac_swap(bond, swap_slave, curr_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 } else {
Eric Dumazet4740d632014-07-15 06:56:55 -07001784 alb_set_slave_mac_addr(curr_active, bond_dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Eric Dumazet4740d632014-07-15 06:56:55 -07001786 alb_send_learning_packets(curr_active,
Vlad Yasevichd0c21d42014-05-21 13:19:48 -04001787 bond_dev->dev_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (bond->alb_info.rlb_enabled) {
1789 /* inform clients mac address has changed */
Eric Dumazet4740d632014-07-15 06:56:55 -07001790 rlb_req_update_slave_clients(bond, curr_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792 }
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return 0;
1795}
1796
1797void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1798{
Wang Yufen73ac0cd2014-02-14 17:15:17 +08001799 if (bond->alb_info.rlb_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 rlb_clear_vlan(bond, vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802