blob: b6a68b4a1a09f0b53e36a2424943980e91850f4e [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
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Joe Perchesa4aee5c2009-12-13 20:06:07 -080023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/skbuff.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/pkt_sched.h>
29#include <linux/spinlock.h>
30#include <linux/slab.h>
31#include <linux/timer.h>
32#include <linux/ip.h>
33#include <linux/ipv6.h>
34#include <linux/if_arp.h>
35#include <linux/if_ether.h>
36#include <linux/if_bonding.h>
37#include <linux/if_vlan.h>
38#include <linux/in.h>
39#include <net/ipx.h>
40#include <net/arp.h>
Vlad Yasevich2d1ea192008-08-28 15:38:41 -040041#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/byteorder.h>
43#include "bonding.h"
44#include "bond_alb.h"
45
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Eric Dumazet885a1362009-09-01 06:31:18 +000048#ifndef __long_aligned
49#define __long_aligned __attribute__((aligned((sizeof(long)))))
50#endif
51static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
52 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
53};
54static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
55 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
56};
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
58
59#pragma pack(1)
60struct learning_pkt {
61 u8 mac_dst[ETH_ALEN];
62 u8 mac_src[ETH_ALEN];
Al Virod3bb52b2007-08-22 20:06:58 -040063 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 u8 padding[ETH_ZLEN - ETH_HLEN];
65};
66
67struct arp_pkt {
Al Virod3bb52b2007-08-22 20:06:58 -040068 __be16 hw_addr_space;
69 __be16 prot_addr_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 u8 hw_addr_len;
71 u8 prot_addr_len;
Al Virod3bb52b2007-08-22 20:06:58 -040072 __be16 op_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 u8 mac_src[ETH_ALEN]; /* sender hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -040074 __be32 ip_src; /* sender IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u8 mac_dst[ETH_ALEN]; /* target hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -040076 __be32 ip_dst; /* target IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78#pragma pack()
79
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -030080static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
81{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070082 return (struct arp_pkt *)skb_network_header(skb);
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -030083}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Forward declaration */
86static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
Jiri Bohace53665c2012-11-28 04:42:14 +000087static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp);
88static void rlb_src_unlink(struct bonding *bond, u32 index);
89static void rlb_src_link(struct bonding *bond, u32 ip_src_hash,
90 u32 ip_dst_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070092static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 int i;
95 u8 hash = 0;
96
97 for (i = 0; i < hash_size; i++) {
98 hash ^= hash_start[i];
99 }
100
101 return hash;
102}
103
104/*********************** tlb specific functions ***************************/
105
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000106static inline void _lock_tx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700108 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000111static inline void _unlock_tx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700113 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000116static inline void _lock_tx_hashtbl(struct bonding *bond)
117{
118 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
119}
120
121static inline void _unlock_tx_hashtbl(struct bonding *bond)
122{
123 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/* Caller must hold tx_hashtbl lock */
127static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
128{
129 if (save_load) {
130 entry->load_history = 1 + entry->tx_bytes /
131 BOND_TLB_REBALANCE_INTERVAL;
132 entry->tx_bytes = 0;
133 }
134
135 entry->tx_slave = NULL;
136 entry->next = TLB_NULL_INDEX;
137 entry->prev = TLB_NULL_INDEX;
138}
139
140static inline void tlb_init_slave(struct slave *slave)
141{
142 SLAVE_TLB_INFO(slave).load = 0;
143 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
144}
145
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000146/* Caller must hold bond lock for read, BH disabled */
147static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
148 int save_load)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct tlb_client_info *tx_hash_table;
151 u32 index;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* clear slave from tx_hashtbl */
154 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
155
Andy Gospodarekce39a802008-10-30 17:41:16 -0700156 /* skip this if we've already freed the tx hash table */
157 if (tx_hash_table) {
158 index = SLAVE_TLB_INFO(slave).head;
159 while (index != TLB_NULL_INDEX) {
160 u32 next_index = tx_hash_table[index].next;
161 tlb_init_table_entry(&tx_hash_table[index], save_load);
162 index = next_index;
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 tlb_init_slave(slave);
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000167}
Jay Vosburgh5af47b22006-01-09 12:14:00 -0800168
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000169/* Caller must hold bond lock for read */
170static void tlb_clear_slave(struct bonding *bond, struct slave *slave,
171 int save_load)
172{
173 _lock_tx_hashtbl_bh(bond);
174 __tlb_clear_slave(bond, slave, save_load);
175 _unlock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/* Must be called before starting the monitor timer */
179static int tlb_initialize(struct bonding *bond)
180{
181 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
182 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
Mitch Williams0d206a32005-11-09 10:35:30 -0800183 struct tlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int i;
185
Joe Jin243cb4e2007-02-06 14:16:40 -0800186 new_hashtbl = kzalloc(size, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000187 if (!new_hashtbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return -1;
Joe Perchese404dec2012-01-29 12:56:23 +0000189
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000190 _lock_tx_hashtbl_bh(bond);
Mitch Williams0d206a32005-11-09 10:35:30 -0800191
192 bond_info->tx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
Peter Pan(潘卫平)38dbaf02011-04-08 03:40:19 +0000195 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000198 _unlock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 return 0;
201}
202
203/* Must be called only after all slaves have been released */
204static void tlb_deinitialize(struct bonding *bond)
205{
206 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
207
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000208 _lock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 kfree(bond_info->tx_hashtbl);
211 bond_info->tx_hashtbl = NULL;
212
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000213 _unlock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Jiri Pirko097811b2010-05-19 03:26:39 +0000216static long long compute_gap(struct slave *slave)
217{
218 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
219 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/* Caller must hold bond lock for read */
223static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
224{
225 struct slave *slave, *least_loaded;
Jiri Pirko097811b2010-05-19 03:26:39 +0000226 long long max_gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Jiri Pirko097811b2010-05-19 03:26:39 +0000228 least_loaded = NULL;
229 max_gap = LLONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* Find the slave with the largest gap */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200232 bond_for_each_slave(bond, slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (SLAVE_IS_OK(slave)) {
Jiri Pirko097811b2010-05-19 03:26:39 +0000234 long long gap = compute_gap(slave);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (max_gap < gap) {
237 least_loaded = slave;
238 max_gap = gap;
239 }
240 }
241 }
242
243 return least_loaded;
244}
245
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000246static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
247 u32 skb_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
250 struct tlb_client_info *hash_table;
251 struct slave *assigned_slave;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 hash_table = bond_info->tx_hashtbl;
254 assigned_slave = hash_table[hash_index].tx_slave;
255 if (!assigned_slave) {
256 assigned_slave = tlb_get_least_loaded_slave(bond);
257
258 if (assigned_slave) {
259 struct tlb_slave_info *slave_info =
260 &(SLAVE_TLB_INFO(assigned_slave));
261 u32 next_index = slave_info->head;
262
263 hash_table[hash_index].tx_slave = assigned_slave;
264 hash_table[hash_index].next = next_index;
265 hash_table[hash_index].prev = TLB_NULL_INDEX;
266
267 if (next_index != TLB_NULL_INDEX) {
268 hash_table[next_index].prev = hash_index;
269 }
270
271 slave_info->head = hash_index;
272 slave_info->load +=
273 hash_table[hash_index].load_history;
274 }
275 }
276
277 if (assigned_slave) {
278 hash_table[hash_index].tx_bytes += skb_len;
279 }
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return assigned_slave;
282}
283
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000284/* Caller must hold bond lock for read */
285static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
286 u32 skb_len)
287{
288 struct slave *tx_slave;
289 /*
290 * We don't need to disable softirq here, becase
291 * tlb_choose_channel() is only called by bond_alb_xmit()
292 * which already has softirq disabled.
293 */
294 _lock_tx_hashtbl(bond);
295 tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
296 _unlock_tx_hashtbl(bond);
297 return tx_slave;
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*********************** rlb specific functions ***************************/
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000301static inline void _lock_rx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700303 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000306static inline void _unlock_rx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700308 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000311static inline void _lock_rx_hashtbl(struct bonding *bond)
312{
313 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
314}
315
316static inline void _unlock_rx_hashtbl(struct bonding *bond)
317{
318 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/* when an ARP REPLY is received from a client update its info
322 * in the rx_hashtbl
323 */
324static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
325{
326 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
327 struct rlb_client_info *client_info;
328 u32 hash_index;
329
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000330 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
333 client_info = &(bond_info->rx_hashtbl[hash_index]);
334
335 if ((client_info->assigned) &&
336 (client_info->ip_src == arp->ip_dst) &&
Flavio Leitner42d782a2010-06-29 08:24:39 +0000337 (client_info->ip_dst == arp->ip_src) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000338 (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* update the clients MAC address */
340 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
341 client_info->ntt = 1;
342 bond_info->rx_ntt = 1;
343 }
344
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000345 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Eric Dumazetde063b72012-06-11 19:23:07 +0000348static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond,
349 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Eric Dumazetde063b72012-06-11 19:23:07 +0000351 struct arp_pkt *arp, _arp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Jiri Pirko3aba8912011-04-19 03:48:16 +0000353 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
David S. Millerb99215c2012-05-13 15:45:13 -0400354 goto out;
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800355
Eric Dumazetde063b72012-06-11 19:23:07 +0000356 arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp);
357 if (!arp)
David S. Millerb99215c2012-05-13 15:45:13 -0400358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jiri Bohace53665c2012-11-28 04:42:14 +0000360 /* We received an ARP from arp->ip_src.
361 * We might have used this IP address previously (on the bonding host
362 * itself or on a system that is bridged together with the bond).
363 * However, if arp->mac_src is different than what is stored in
364 * rx_hashtbl, some other host is now using the IP and we must prevent
365 * sending out client updates with this IP address and the old MAC
366 * address.
367 * Clean up all hash table entries that have this address as ip_src but
368 * have a different mac_src.
369 */
370 rlb_purge_src_ip(bond, arp);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (arp->op_code == htons(ARPOP_REPLY)) {
373 /* update rx hash table for this ARP */
374 rlb_update_entry_from_arp(bond, arp);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800375 pr_debug("Server received an ARP Reply from client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
David S. Millerb99215c2012-05-13 15:45:13 -0400377out:
378 return RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381/* Caller must hold bond lock for read */
382static struct slave *rlb_next_rx_slave(struct bonding *bond)
383{
384 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
385 struct slave *rx_slave, *slave, *start_at;
386 int i = 0;
387
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200388 if (bond_info->next_rx_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 start_at = bond_info->next_rx_slave;
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200390 else
391 start_at = bond_first_slave(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 rx_slave = NULL;
394
395 bond_for_each_slave_from(bond, slave, i, start_at) {
396 if (SLAVE_IS_OK(slave)) {
397 if (!rx_slave) {
398 rx_slave = slave;
399 } else if (slave->speed > rx_slave->speed) {
400 rx_slave = slave;
401 }
402 }
403 }
404
405 if (rx_slave) {
nikolay@redhat.comdec1e902013-08-01 16:54:47 +0200406 slave = bond_next_slave(bond, rx_slave);
407 bond_info->next_rx_slave = slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 return rx_slave;
411}
412
413/* teach the switch the mac of a disabled slave
414 * on the primary for fault tolerance
415 *
416 * Caller must hold bond->curr_slave_lock for write or bond lock for write
417 */
418static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
419{
420 if (!bond->curr_active_slave) {
421 return;
422 }
423
424 if (!bond->alb_info.primary_is_promisc) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700425 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
426 bond->alb_info.primary_is_promisc = 1;
427 else
428 bond->alb_info.primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 bond->alb_info.rlb_promisc_timeout_counter = 0;
432
433 alb_send_learning_packets(bond->curr_active_slave, addr);
434}
435
436/* slave being removed should not be active at this point
437 *
438 * Caller must hold bond lock for read
439 */
440static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
441{
442 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
443 struct rlb_client_info *rx_hash_table;
444 u32 index, next_index;
445
446 /* clear slave from rx_hashtbl */
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000447 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 rx_hash_table = bond_info->rx_hashtbl;
Jiri Bohace53665c2012-11-28 04:42:14 +0000450 index = bond_info->rx_hashtbl_used_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 for (; index != RLB_NULL_INDEX; index = next_index) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000452 next_index = rx_hash_table[index].used_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (rx_hash_table[index].slave == slave) {
454 struct slave *assigned_slave = rlb_next_rx_slave(bond);
455
456 if (assigned_slave) {
457 rx_hash_table[index].slave = assigned_slave;
Joe Perchesa6700db2012-05-09 17:04:04 +0000458 if (!ether_addr_equal_64bits(rx_hash_table[index].mac_dst,
459 mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 bond_info->rx_hashtbl[index].ntt = 1;
461 bond_info->rx_ntt = 1;
462 /* A slave has been removed from the
463 * table because it is either disabled
464 * or being released. We must retry the
465 * update to avoid clients from not
466 * being updated & disconnecting when
467 * there is stress
468 */
469 bond_info->rlb_update_retry_counter =
470 RLB_UPDATE_RETRY;
471 }
472 } else { /* there is no active slave */
473 rx_hash_table[index].slave = NULL;
474 }
475 }
476 }
477
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000478 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700480 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (slave != bond->curr_active_slave) {
483 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
484 }
485
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700486 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489static void rlb_update_client(struct rlb_client_info *client_info)
490{
491 int i;
492
493 if (!client_info->slave) {
494 return;
495 }
496
497 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
498 struct sk_buff *skb;
499
500 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
501 client_info->ip_dst,
502 client_info->slave->dev,
503 client_info->ip_src,
504 client_info->mac_dst,
505 client_info->slave->dev->dev_addr,
506 client_info->mac_dst);
507 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800508 pr_err("%s: Error: failed to create an ARP packet\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +0000509 client_info->slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 continue;
511 }
512
513 skb->dev = client_info->slave->dev;
514
515 if (client_info->tag) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000516 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), client_info->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800518 pr_err("%s: Error: failed to insert VLAN tag\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +0000519 client_info->slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 continue;
521 }
522 }
523
524 arp_xmit(skb);
525 }
526}
527
528/* sends ARP REPLIES that update the clients that need updating */
529static void rlb_update_rx_clients(struct bonding *bond)
530{
531 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
532 struct rlb_client_info *client_info;
533 u32 hash_index;
534
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000535 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jiri Bohace53665c2012-11-28 04:42:14 +0000537 hash_index = bond_info->rx_hashtbl_used_head;
538 for (; hash_index != RLB_NULL_INDEX;
539 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 client_info = &(bond_info->rx_hashtbl[hash_index]);
541 if (client_info->ntt) {
542 rlb_update_client(client_info);
543 if (bond_info->rlb_update_retry_counter == 0) {
544 client_info->ntt = 0;
545 }
546 }
547 }
548
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200549 /* do not update the entries again until this counter is zero so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * not to confuse the clients.
551 */
552 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
553
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000554 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/* The slave was assigned a new mac address - update the clients */
558static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
559{
560 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
561 struct rlb_client_info *client_info;
562 int ntt = 0;
563 u32 hash_index;
564
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000565 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Jiri Bohace53665c2012-11-28 04:42:14 +0000567 hash_index = bond_info->rx_hashtbl_used_head;
568 for (; hash_index != RLB_NULL_INDEX;
569 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 client_info = &(bond_info->rx_hashtbl[hash_index]);
571
572 if ((client_info->slave == slave) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000573 !ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 client_info->ntt = 1;
575 ntt = 1;
576 }
577 }
578
579 // update the team's flag only after the whole iteration
580 if (ntt) {
581 bond_info->rx_ntt = 1;
582 //fasten the change
583 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
584 }
585
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000586 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400590static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
593 struct rlb_client_info *client_info;
594 u32 hash_index;
595
596 _lock_rx_hashtbl(bond);
597
Jiri Bohace53665c2012-11-28 04:42:14 +0000598 hash_index = bond_info->rx_hashtbl_used_head;
599 for (; hash_index != RLB_NULL_INDEX;
600 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 client_info = &(bond_info->rx_hashtbl[hash_index]);
602
603 if (!client_info->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800604 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800605 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 continue;
607 }
608 /*update all clients using this src_ip, that are not assigned
609 * to the team's address (curr_active_slave) and have a known
610 * unicast mac address.
611 */
612 if ((client_info->ip_src == src_ip) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000613 !ether_addr_equal_64bits(client_info->slave->dev->dev_addr,
614 bond->dev->dev_addr) &&
615 !ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 client_info->ntt = 1;
617 bond_info->rx_ntt = 1;
618 }
619 }
620
621 _unlock_rx_hashtbl(bond);
622}
623
624/* Caller must hold both bond and ptr locks for read */
625static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
626{
627 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300628 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct slave *assigned_slave;
630 struct rlb_client_info *client_info;
631 u32 hash_index = 0;
632
633 _lock_rx_hashtbl(bond);
634
Amerigo Wange364a342011-02-27 23:34:28 +0000635 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 client_info = &(bond_info->rx_hashtbl[hash_index]);
637
638 if (client_info->assigned) {
639 if ((client_info->ip_src == arp->ip_src) &&
640 (client_info->ip_dst == arp->ip_dst)) {
641 /* the entry is already assigned to this client */
Joe Perchesa6700db2012-05-09 17:04:04 +0000642 if (!ether_addr_equal_64bits(arp->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* update mac address from arp */
644 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
645 }
Jiri Bohace53665c2012-11-28 04:42:14 +0000646 memcpy(client_info->mac_src, arp->mac_src, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 assigned_slave = client_info->slave;
649 if (assigned_slave) {
650 _unlock_rx_hashtbl(bond);
651 return assigned_slave;
652 }
653 } else {
654 /* the entry is already assigned to some other client,
655 * move the old client to primary (curr_active_slave) so
656 * that the new client can be assigned to this entry.
657 */
658 if (bond->curr_active_slave &&
659 client_info->slave != bond->curr_active_slave) {
660 client_info->slave = bond->curr_active_slave;
661 rlb_update_client(client_info);
662 }
663 }
664 }
665 /* assign a new slave */
666 assigned_slave = rlb_next_rx_slave(bond);
667
668 if (assigned_slave) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000669 if (!(client_info->assigned &&
670 client_info->ip_src == arp->ip_src)) {
671 /* ip_src is going to be updated,
672 * fix the src hash list
673 */
674 u32 hash_src = _simple_hash((u8 *)&arp->ip_src,
675 sizeof(arp->ip_src));
676 rlb_src_unlink(bond, hash_index);
677 rlb_src_link(bond, hash_src, hash_index);
678 }
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 client_info->ip_src = arp->ip_src;
681 client_info->ip_dst = arp->ip_dst;
682 /* arp->mac_dst is broadcast for arp reqeusts.
683 * will be updated with clients actual unicast mac address
684 * upon receiving an arp reply.
685 */
686 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
Jiri Bohace53665c2012-11-28 04:42:14 +0000687 memcpy(client_info->mac_src, arp->mac_src, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 client_info->slave = assigned_slave;
689
Joe Perchesa6700db2012-05-09 17:04:04 +0000690 if (!ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 client_info->ntt = 1;
692 bond->alb_info.rx_ntt = 1;
693 } else {
694 client_info->ntt = 0;
695 }
696
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000697 if (bond_vlan_used(bond)) {
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700698 if (!vlan_get_tag(skb, &client_info->vlan_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 client_info->tag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
702 if (!client_info->assigned) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000703 u32 prev_tbl_head = bond_info->rx_hashtbl_used_head;
704 bond_info->rx_hashtbl_used_head = hash_index;
705 client_info->used_next = prev_tbl_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (prev_tbl_head != RLB_NULL_INDEX) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000707 bond_info->rx_hashtbl[prev_tbl_head].used_prev =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 hash_index;
709 }
710 client_info->assigned = 1;
711 }
712 }
713
714 _unlock_rx_hashtbl(bond);
715
716 return assigned_slave;
717}
718
719/* chooses (and returns) transmit channel for arp reply
720 * does not choose channel for other arp types since they are
721 * sent on the curr_active_slave
722 */
723static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
724{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300725 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct slave *tx_slave = NULL;
727
zheng.li567b8712012-11-27 23:57:04 +0000728 /* Don't modify or load balance ARPs that do not originate locally
729 * (e.g.,arrive via a bridge).
730 */
731 if (!bond_slave_has_mac(bond, arp->mac_src))
732 return NULL;
733
Brian Haleyf14c4e42008-09-02 10:08:08 -0400734 if (arp->op_code == htons(ARPOP_REPLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* the arp must be sent on the selected
736 * rx channel
737 */
738 tx_slave = rlb_choose_channel(skb, bond);
739 if (tx_slave) {
740 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
741 }
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800742 pr_debug("Server sent ARP Reply packet\n");
Brian Haleyf14c4e42008-09-02 10:08:08 -0400743 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /* Create an entry in the rx_hashtbl for this client as a
745 * place holder.
746 * When the arp reply is received the entry will be updated
747 * with the correct unicast address of the client.
748 */
749 rlb_choose_channel(skb, bond);
750
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +0000751 /* The ARP reply packets must be delayed so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * they can cancel out the influence of the ARP request.
753 */
754 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
755
756 /* arp requests are broadcast and are sent on the primary
757 * the arp request will collapse all clients on the subnet to
758 * the primary slave. We must register these clients to be
759 * updated with their assigned mac.
760 */
761 rlb_req_update_subnet_clients(bond, arp->ip_src);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800762 pr_debug("Server sent ARP Request packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 return tx_slave;
766}
767
768/* Caller must hold bond lock for read */
769static void rlb_rebalance(struct bonding *bond)
770{
771 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
772 struct slave *assigned_slave;
773 struct rlb_client_info *client_info;
774 int ntt;
775 u32 hash_index;
776
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000777 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 ntt = 0;
Jiri Bohace53665c2012-11-28 04:42:14 +0000780 hash_index = bond_info->rx_hashtbl_used_head;
781 for (; hash_index != RLB_NULL_INDEX;
782 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 client_info = &(bond_info->rx_hashtbl[hash_index]);
784 assigned_slave = rlb_next_rx_slave(bond);
785 if (assigned_slave && (client_info->slave != assigned_slave)) {
786 client_info->slave = assigned_slave;
787 client_info->ntt = 1;
788 ntt = 1;
789 }
790 }
791
792 /* update the team's flag only after the whole iteration */
793 if (ntt) {
794 bond_info->rx_ntt = 1;
795 }
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000796 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/* Caller must hold rx_hashtbl lock */
Jiri Bohace53665c2012-11-28 04:42:14 +0000800static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
801{
802 entry->used_next = RLB_NULL_INDEX;
803 entry->used_prev = RLB_NULL_INDEX;
804 entry->assigned = 0;
805 entry->slave = NULL;
806 entry->tag = 0;
807}
808static void rlb_init_table_entry_src(struct rlb_client_info *entry)
809{
810 entry->src_first = RLB_NULL_INDEX;
811 entry->src_prev = RLB_NULL_INDEX;
812 entry->src_next = RLB_NULL_INDEX;
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815static void rlb_init_table_entry(struct rlb_client_info *entry)
816{
817 memset(entry, 0, sizeof(struct rlb_client_info));
Jiri Bohace53665c2012-11-28 04:42:14 +0000818 rlb_init_table_entry_dst(entry);
819 rlb_init_table_entry_src(entry);
820}
821
822static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
823{
824 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
825 u32 next_index = bond_info->rx_hashtbl[index].used_next;
826 u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
827
828 if (index == bond_info->rx_hashtbl_used_head)
829 bond_info->rx_hashtbl_used_head = next_index;
830 if (prev_index != RLB_NULL_INDEX)
831 bond_info->rx_hashtbl[prev_index].used_next = next_index;
832 if (next_index != RLB_NULL_INDEX)
833 bond_info->rx_hashtbl[next_index].used_prev = prev_index;
834}
835
836/* unlink a rlb hash table entry from the src list */
837static void rlb_src_unlink(struct bonding *bond, u32 index)
838{
839 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
840 u32 next_index = bond_info->rx_hashtbl[index].src_next;
841 u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
842
843 bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
844 bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
845
846 if (next_index != RLB_NULL_INDEX)
847 bond_info->rx_hashtbl[next_index].src_prev = prev_index;
848
849 if (prev_index == RLB_NULL_INDEX)
850 return;
851
852 /* is prev_index pointing to the head of this list? */
853 if (bond_info->rx_hashtbl[prev_index].src_first == index)
854 bond_info->rx_hashtbl[prev_index].src_first = next_index;
855 else
856 bond_info->rx_hashtbl[prev_index].src_next = next_index;
857
858}
859
860static void rlb_delete_table_entry(struct bonding *bond, u32 index)
861{
862 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
863 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
864
865 rlb_delete_table_entry_dst(bond, index);
866 rlb_init_table_entry_dst(entry);
867
868 rlb_src_unlink(bond, index);
869}
870
871/* add the rx_hashtbl[ip_dst_hash] entry to the list
872 * of entries with identical ip_src_hash
873 */
874static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
875{
876 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
877 u32 next;
878
879 bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
880 next = bond_info->rx_hashtbl[ip_src_hash].src_first;
881 bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
882 if (next != RLB_NULL_INDEX)
883 bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
884 bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
885}
886
887/* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
888 * not match arp->mac_src */
889static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
890{
891 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
892 u32 ip_src_hash = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
893 u32 index;
894
895 _lock_rx_hashtbl_bh(bond);
896
897 index = bond_info->rx_hashtbl[ip_src_hash].src_first;
898 while (index != RLB_NULL_INDEX) {
899 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
900 u32 next_index = entry->src_next;
901 if (entry->ip_src == arp->ip_src &&
902 !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
903 rlb_delete_table_entry(bond, index);
904 index = next_index;
905 }
906 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static int rlb_initialize(struct bonding *bond)
910{
911 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mitch Williams0d206a32005-11-09 10:35:30 -0800912 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
914 int i;
915
Mitch Williams0d206a32005-11-09 10:35:30 -0800916 new_hashtbl = kmalloc(size, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000917 if (!new_hashtbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return -1;
Joe Perchese404dec2012-01-29 12:56:23 +0000919
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000920 _lock_rx_hashtbl_bh(bond);
Mitch Williams0d206a32005-11-09 10:35:30 -0800921
922 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Jiri Bohace53665c2012-11-28 04:42:14 +0000924 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
927 rlb_init_table_entry(bond_info->rx_hashtbl + i);
928 }
929
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000930 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* register to receive ARPs */
Jiri Pirko3aba8912011-04-19 03:48:16 +0000933 bond->recv_probe = rlb_arp_recv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 return 0;
936}
937
938static void rlb_deinitialize(struct bonding *bond)
939{
940 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
941
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000942 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 kfree(bond_info->rx_hashtbl);
945 bond_info->rx_hashtbl = NULL;
Jiri Bohace53665c2012-11-28 04:42:14 +0000946 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000948 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
952{
953 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
954 u32 curr_index;
955
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000956 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Jiri Bohace53665c2012-11-28 04:42:14 +0000958 curr_index = bond_info->rx_hashtbl_used_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 while (curr_index != RLB_NULL_INDEX) {
960 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
Jiri Bohace53665c2012-11-28 04:42:14 +0000961 u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Jiri Bohace53665c2012-11-28 04:42:14 +0000963 if (curr->tag && (curr->vlan_id == vlan_id))
964 rlb_delete_table_entry(bond, curr_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 curr_index = next_index;
967 }
968
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000969 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*********************** tlb/rlb shared functions *********************/
973
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200974static void alb_send_lp_vid(struct slave *slave, u8 mac_addr[],
975 u16 vid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct learning_pkt pkt;
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200978 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int size = sizeof(struct learning_pkt);
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200980 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 memset(&pkt, 0, size);
983 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
984 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
Harvey Harrison09640e62009-02-01 00:45:17 -0800985 pkt.type = cpu_to_be16(ETH_P_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200987 skb = dev_alloc_skb(size);
988 if (!skb)
989 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Veaceslav Falico7aa64982013-08-28 23:25:13 +0200991 data = skb_put(skb, size);
992 memcpy(data, &pkt, size);
993
994 skb_reset_mac_header(skb);
995 skb->network_header = skb->mac_header + ETH_HLEN;
996 skb->protocol = pkt.type;
997 skb->priority = TC_PRIO_CONTROL;
998 skb->dev = slave->dev;
999
1000 if (vid) {
1001 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (!skb) {
Veaceslav Falico7aa64982013-08-28 23:25:13 +02001003 pr_err("%s: Error: failed to insert VLAN tag\n",
1004 slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return;
1006 }
Veaceslav Falico7aa64982013-08-28 23:25:13 +02001007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Veaceslav Falico7aa64982013-08-28 23:25:13 +02001009 dev_queue_xmit(skb);
1010}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Veaceslav Falico7aa64982013-08-28 23:25:13 +02001012
1013static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
1014{
1015 struct bonding *bond = bond_get_bond_by_slave(slave);
Veaceslav Falico5bf94b82013-08-28 23:25:14 +02001016 struct net_device *upper;
1017 struct list_head *iter;
Veaceslav Falico7aa64982013-08-28 23:25:13 +02001018
Veaceslav Falico5bf94b82013-08-28 23:25:14 +02001019 /* send untagged */
1020 alb_send_lp_vid(slave, mac_addr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Veaceslav Falico5bf94b82013-08-28 23:25:14 +02001022 /* loop through vlans and send one packet for each */
1023 rcu_read_lock();
1024 netdev_for_each_upper_dev_rcu(bond->dev, upper, iter) {
1025 if (upper->priv_flags & IFF_802_1Q_VLAN)
1026 alb_send_lp_vid(slave, mac_addr,
1027 vlan_dev_vlan_id(upper));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Veaceslav Falico5bf94b82013-08-28 23:25:14 +02001029 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Jiri Bohacb9245512012-01-18 12:24:54 +00001032static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 struct net_device *dev = slave->dev;
1035 struct sockaddr s_addr;
1036
Jiri Bohacb9245512012-01-18 12:24:54 +00001037 if (slave->bond->params.mode == BOND_MODE_TLB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 memcpy(dev->dev_addr, addr, dev->addr_len);
1039 return 0;
1040 }
1041
1042 /* for rlb each slave must have a unique hw mac addresses so that */
1043 /* each slave will receive packets destined to a different mac */
1044 memcpy(s_addr.sa_data, addr, dev->addr_len);
1045 s_addr.sa_family = dev->type;
1046 if (dev_set_mac_address(dev, &s_addr)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001047 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
1048 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +00001049 slave->bond->dev->name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return -EOPNOTSUPP;
1051 }
1052 return 0;
1053}
1054
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001055/*
1056 * Swap MAC addresses between two slaves.
1057 *
1058 * Called with RTNL held, and no other locks.
1059 *
1060 */
1061
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001062static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
Jiri Bohacb9245512012-01-18 12:24:54 +00001067 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
1068 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001070}
1071
1072/*
1073 * Send learning packets after MAC address swap.
1074 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001075 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001076 */
1077static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
1078 struct slave *slave2)
1079{
1080 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
1081 struct slave *disabled_slave = NULL;
1082
Jay Vosburgh25433312008-01-17 16:24:59 -08001083 ASSERT_RTNL();
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* fasten the change in the switch */
1086 if (SLAVE_IS_OK(slave1)) {
1087 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
1088 if (bond->alb_info.rlb_enabled) {
1089 /* inform the clients that the mac address
1090 * has changed
1091 */
1092 rlb_req_update_slave_clients(bond, slave1);
1093 }
1094 } else {
1095 disabled_slave = slave1;
1096 }
1097
1098 if (SLAVE_IS_OK(slave2)) {
1099 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1100 if (bond->alb_info.rlb_enabled) {
1101 /* inform the clients that the mac address
1102 * has changed
1103 */
1104 rlb_req_update_slave_clients(bond, slave2);
1105 }
1106 } else {
1107 disabled_slave = slave2;
1108 }
1109
1110 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1111 /* A disabled slave was assigned an active mac addr */
1112 rlb_teach_disabled_mac_on_primary(bond,
1113 disabled_slave->dev->dev_addr);
1114 }
1115}
1116
1117/**
1118 * alb_change_hw_addr_on_detach
1119 * @bond: bonding we're working on
1120 * @slave: the slave that was just detached
1121 *
1122 * We assume that @slave was already detached from the slave list.
1123 *
1124 * If @slave's permanent hw address is different both from its current
1125 * address and from @bond's address, then somewhere in the bond there's
1126 * a slave that has @slave's permanet address as its current address.
1127 * We'll make sure that that slave no longer uses @slave's permanent address.
1128 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001129 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 */
1131static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1132{
1133 int perm_curr_diff;
1134 int perm_bond_diff;
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001135 struct slave *found_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Joe Perchesa6700db2012-05-09 17:04:04 +00001137 perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1138 slave->dev->dev_addr);
1139 perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1140 bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (perm_curr_diff && perm_bond_diff) {
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001143 found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001145 if (found_slave) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001146 /* locking: needs RTNL and nothing else */
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001147 alb_swap_mac_addr(slave, found_slave);
1148 alb_fasten_mac_swap(bond, slave, found_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150 }
1151}
1152
1153/**
1154 * alb_handle_addr_collision_on_attach
1155 * @bond: bonding we're working on
1156 * @slave: the slave that was just attached
1157 *
1158 * checks uniqueness of slave's mac address and handles the case the
1159 * new slave uses the bonds mac address.
1160 *
1161 * If the permanent hw address of @slave is @bond's hw address, we need to
1162 * find a different hw address to give @slave, that isn't in use by any other
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +00001163 * slave in the bond. This address must be, of course, one of the permanent
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * addresses of the other slaves.
1165 *
1166 * We go over the slave list, and for each slave there we compare its
1167 * permanent hw address with the current address of all the other slaves.
1168 * If no match was found, then we've found a slave with a permanent address
1169 * that isn't used by any other slave in the bond, so we can assign it to
1170 * @slave.
1171 *
1172 * assumption: this function is called before @slave is attached to the
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001173 * bond slave list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
1175static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1176{
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001177 struct slave *tmp_slave1, *free_mac_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 struct slave *has_bond_addr = bond->curr_active_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001180 if (list_empty(&bond->slave_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 /* this is the first slave */
1182 return 0;
1183 }
1184
1185 /* if slave's mac address differs from bond's mac address
1186 * check uniqueness of slave's mac address against the other
1187 * slaves in the bond.
1188 */
Joe Perchesa6700db2012-05-09 17:04:04 +00001189 if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001190 if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
John W. Linville6b38aef2005-07-28 15:00:15 -04001191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
John W. Linville6b38aef2005-07-28 15:00:15 -04001193 /* Try setting slave mac to bond address and fall-through
1194 to code handling that situation below... */
Jiri Bohacb9245512012-01-18 12:24:54 +00001195 alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
1198 /* The slave's address is equal to the address of the bond.
1199 * Search for a spare address in the bond for this slave.
1200 */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001201 bond_for_each_slave(bond, tmp_slave1) {
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001202 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* no slave has tmp_slave1's perm addr
1204 * as its curr addr
1205 */
1206 free_mac_slave = tmp_slave1;
1207 break;
1208 }
1209
1210 if (!has_bond_addr) {
Joe Perchesa6700db2012-05-09 17:04:04 +00001211 if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
1212 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 has_bond_addr = tmp_slave1;
1215 }
1216 }
1217 }
1218
1219 if (free_mac_slave) {
Jiri Bohacb9245512012-01-18 12:24:54 +00001220 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001222 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001223 bond->dev->name, slave->dev->name,
1224 free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 } else if (has_bond_addr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001227 pr_err("%s: Error: 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",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001228 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return -EFAULT;
1230 }
1231
1232 return 0;
1233}
1234
1235/**
1236 * alb_set_mac_address
1237 * @bond:
1238 * @addr:
1239 *
1240 * In TLB mode all slaves are configured to the bond's hw address, but set
1241 * their dev_addr field to different addresses (based on their permanent hw
1242 * addresses).
1243 *
1244 * For each slave, this function sets the interface to the new address and then
1245 * changes its dev_addr field to its previous value.
1246 *
1247 * Unwinding assumes bond's mac address has not yet changed.
1248 */
1249static int alb_set_mac_address(struct bonding *bond, void *addr)
1250{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 char tmp_addr[ETH_ALEN];
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001252 struct slave *slave;
1253 struct sockaddr sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001256 if (bond->alb_info.rlb_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001259 bond_for_each_slave(bond, slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /* save net_device's current hw address */
1261 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1262
1263 res = dev_set_mac_address(slave->dev, addr);
1264
1265 /* restore net_device's hw address */
1266 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1267
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001268 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
1272 return 0;
1273
1274unwind:
1275 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1276 sa.sa_family = bond->dev->type;
1277
1278 /* unwind from head to the slave that failed */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001279 bond_for_each_slave_continue_reverse(bond, slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1281 dev_set_mac_address(slave->dev, &sa);
1282 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1283 }
1284
1285 return res;
1286}
1287
1288/************************ exported alb funcions ************************/
1289
1290int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1291{
1292 int res;
1293
1294 res = tlb_initialize(bond);
1295 if (res) {
1296 return res;
1297 }
1298
1299 if (rlb_enabled) {
1300 bond->alb_info.rlb_enabled = 1;
1301 /* initialize rlb */
1302 res = rlb_initialize(bond);
1303 if (res) {
1304 tlb_deinitialize(bond);
1305 return res;
1306 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001307 } else {
1308 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
1311 return 0;
1312}
1313
1314void bond_alb_deinitialize(struct bonding *bond)
1315{
1316 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1317
1318 tlb_deinitialize(bond);
1319
1320 if (bond_info->rlb_enabled) {
1321 rlb_deinitialize(bond);
1322 }
1323}
1324
1325int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1326{
Wang Chen454d7c92008-11-12 23:37:49 -08001327 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct ethhdr *eth_data;
1329 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1330 struct slave *tx_slave = NULL;
Al Virod3bb52b2007-08-22 20:06:58 -04001331 static const __be32 ip_bcast = htonl(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int hash_size = 0;
1333 int do_tx_balance = 1;
1334 u32 hash_index = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001335 const u8 *hash_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int res = 1;
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001337 struct ipv6hdr *ip6hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001339 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 eth_data = eth_hdr(skb);
1341
Michał Mirosław0693e882011-05-07 01:48:02 +00001342 /* make sure that the curr_active_slave do not change during tx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001344 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 read_lock(&bond->curr_slave_lock);
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 switch (ntohs(skb->protocol)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001348 case ETH_P_IP: {
1349 const struct iphdr *iph = ip_hdr(skb);
1350
Joe Perchesa6700db2012-05-09 17:04:04 +00001351 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001352 (iph->daddr == ip_bcast) ||
1353 (iph->protocol == IPPROTO_IGMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 do_tx_balance = 0;
1355 break;
1356 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001357 hash_start = (char *)&(iph->daddr);
1358 hash_size = sizeof(iph->daddr);
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
1361 case ETH_P_IPV6:
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001362 /* IPv6 doesn't really use broadcast mac address, but leave
1363 * that here just in case.
1364 */
Joe Perchesa6700db2012-05-09 17:04:04 +00001365 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 do_tx_balance = 0;
1367 break;
1368 }
1369
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001370 /* IPv6 uses all-nodes multicast as an equivalent to
1371 * broadcasts in IPv4.
1372 */
Joe Perchesa6700db2012-05-09 17:04:04 +00001373 if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001374 do_tx_balance = 0;
1375 break;
1376 }
1377
1378 /* Additianally, DAD probes should not be tx-balanced as that
1379 * will lead to false positives for duplicate addresses and
1380 * prevent address configuration from working.
1381 */
1382 ip6hdr = ipv6_hdr(skb);
1383 if (ipv6_addr_any(&ip6hdr->saddr)) {
1384 do_tx_balance = 0;
1385 break;
1386 }
1387
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001388 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1389 hash_size = sizeof(ipv6_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 case ETH_P_IPX:
Al Virod3bb52b2007-08-22 20:06:58 -04001392 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /* something is wrong with this packet */
1394 do_tx_balance = 0;
1395 break;
1396 }
1397
1398 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1399 /* The only protocol worth balancing in
1400 * this family since it has an "ARP" like
1401 * mechanism
1402 */
1403 do_tx_balance = 0;
1404 break;
1405 }
1406
1407 hash_start = (char*)eth_data->h_dest;
1408 hash_size = ETH_ALEN;
1409 break;
1410 case ETH_P_ARP:
1411 do_tx_balance = 0;
1412 if (bond_info->rlb_enabled) {
1413 tx_slave = rlb_arp_xmit(skb, bond);
1414 }
1415 break;
1416 default:
1417 do_tx_balance = 0;
1418 break;
1419 }
1420
1421 if (do_tx_balance) {
1422 hash_index = _simple_hash(hash_start, hash_size);
1423 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1424 }
1425
1426 if (!tx_slave) {
1427 /* unbalanced or unassigned, send through primary */
1428 tx_slave = bond->curr_active_slave;
1429 bond_info->unbalanced_load += skb->len;
1430 }
1431
1432 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1433 if (tx_slave != bond->curr_active_slave) {
1434 memcpy(eth_data->h_source,
1435 tx_slave->dev->dev_addr,
1436 ETH_ALEN);
1437 }
1438
1439 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1440 } else {
1441 if (tx_slave) {
Maxim Uvarovf515e6b2012-01-09 12:01:37 +00001442 _lock_tx_hashtbl(bond);
1443 __tlb_clear_slave(bond, tx_slave, 0);
1444 _unlock_tx_hashtbl(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 }
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 read_unlock(&bond->curr_slave_lock);
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001449 read_unlock(&bond->lock);
Eric Dumazet04502432012-06-13 05:30:07 +00001450 if (res) {
1451 /* no suitable interface, frame not sent */
1452 kfree_skb(skb);
1453 }
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001454
Patrick McHardyec634fe2009-07-05 19:23:38 -07001455 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001458void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001460 struct bonding *bond = container_of(work, struct bonding,
1461 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1463 struct slave *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 read_lock(&bond->lock);
1466
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001467 if (list_empty(&bond->slave_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 bond_info->tx_rebalance_counter = 0;
1469 bond_info->lp_counter = 0;
1470 goto re_arm;
1471 }
1472
1473 bond_info->tx_rebalance_counter++;
1474 bond_info->lp_counter++;
1475
1476 /* send learning packets */
1477 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1478 /* change of curr_active_slave involves swapping of mac addresses.
1479 * in order to avoid this swapping from happening while
1480 * sending the learning packets, the curr_slave_lock must be held for
1481 * read.
1482 */
1483 read_lock(&bond->curr_slave_lock);
1484
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001485 bond_for_each_slave(bond, slave)
Mitch Williamse944ef72005-11-09 10:36:50 -08001486 alb_send_learning_packets(slave, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 read_unlock(&bond->curr_slave_lock);
1489
1490 bond_info->lp_counter = 0;
1491 }
1492
1493 /* rebalance tx traffic */
1494 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1495
1496 read_lock(&bond->curr_slave_lock);
1497
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001498 bond_for_each_slave(bond, slave) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 tlb_clear_slave(bond, slave, 1);
1500 if (slave == bond->curr_active_slave) {
1501 SLAVE_TLB_INFO(slave).load =
1502 bond_info->unbalanced_load /
1503 BOND_TLB_REBALANCE_INTERVAL;
1504 bond_info->unbalanced_load = 0;
1505 }
1506 }
1507
1508 read_unlock(&bond->curr_slave_lock);
1509
1510 bond_info->tx_rebalance_counter = 0;
1511 }
1512
1513 /* handle rlb stuff */
1514 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (bond_info->primary_is_promisc &&
1516 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1517
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001518 /*
1519 * dev_set_promiscuity requires rtnl and
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001520 * nothing else. Avoid race with bond_close.
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001521 */
1522 read_unlock(&bond->lock);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001523 if (!rtnl_trylock()) {
1524 read_lock(&bond->lock);
1525 goto re_arm;
1526 }
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 bond_info->rlb_promisc_timeout_counter = 0;
1529
1530 /* If the primary was set to promiscuous mode
1531 * because a slave was disabled then
1532 * it can now leave promiscuous mode.
1533 */
1534 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1535 bond_info->primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001537 rtnl_unlock();
1538 read_lock(&bond->lock);
1539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 if (bond_info->rlb_rebalance) {
1542 bond_info->rlb_rebalance = 0;
1543 rlb_rebalance(bond);
1544 }
1545
1546 /* check if clients need updating */
1547 if (bond_info->rx_ntt) {
1548 if (bond_info->rlb_update_delay_counter) {
1549 --bond_info->rlb_update_delay_counter;
1550 } else {
1551 rlb_update_rx_clients(bond);
1552 if (bond_info->rlb_update_retry_counter) {
1553 --bond_info->rlb_update_retry_counter;
1554 } else {
1555 bond_info->rx_ntt = 0;
1556 }
1557 }
1558 }
1559 }
1560
1561re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001562 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 read_unlock(&bond->lock);
1565}
1566
1567/* assumption: called before the slave is attached to the bond
1568 * and not locked by the bond lock
1569 */
1570int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1571{
1572 int res;
1573
Jiri Bohacb9245512012-01-18 12:24:54 +00001574 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (res) {
1576 return res;
1577 }
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 res = alb_handle_addr_collision_on_attach(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (res) {
1581 return res;
1582 }
1583
1584 tlb_init_slave(slave);
1585
1586 /* order a rebalance ASAP */
1587 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1588
1589 if (bond->alb_info.rlb_enabled) {
1590 bond->alb_info.rlb_rebalance = 1;
1591 }
1592
1593 return 0;
1594}
1595
Jay Vosburgh25433312008-01-17 16:24:59 -08001596/*
1597 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1598 * if necessary.
1599 *
1600 * Caller must hold RTNL and no other locks
1601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1603{
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001604 if (!list_empty(&bond->slave_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 alb_change_hw_addr_on_detach(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 tlb_clear_slave(bond, slave, 0);
1608
1609 if (bond->alb_info.rlb_enabled) {
1610 bond->alb_info.next_rx_slave = NULL;
1611 rlb_clear_slave(bond, slave);
1612 }
1613}
1614
1615/* Caller must hold bond lock for read */
1616void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1617{
1618 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1619
1620 if (link == BOND_LINK_DOWN) {
1621 tlb_clear_slave(bond, slave, 0);
1622 if (bond->alb_info.rlb_enabled) {
1623 rlb_clear_slave(bond, slave);
1624 }
1625 } else if (link == BOND_LINK_UP) {
1626 /* order a rebalance ASAP */
1627 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1628 if (bond->alb_info.rlb_enabled) {
1629 bond->alb_info.rlb_rebalance = 1;
1630 /* If the updelay module parameter is smaller than the
1631 * forwarding delay of the switch the rebalance will
1632 * not work because the rebalance arp replies will
1633 * not be forwarded to the clients..
1634 */
1635 }
1636 }
1637}
1638
1639/**
1640 * bond_alb_handle_active_change - assign new curr_active_slave
1641 * @bond: our bonding struct
1642 * @new_slave: new slave to assign
1643 *
1644 * Set the bond->curr_active_slave to @new_slave and handle
1645 * mac address swapping and promiscuity changes as needed.
1646 *
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001647 * If new_slave is NULL, caller must hold curr_slave_lock or
1648 * bond->lock for write.
1649 *
1650 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1651 * read and curr_slave_lock for write. Processing here may sleep, so
1652 * no other locks may be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
1654void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001655 __releases(&bond->curr_slave_lock)
1656 __releases(&bond->lock)
1657 __acquires(&bond->lock)
1658 __acquires(&bond->curr_slave_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct slave *swap_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001662 if (bond->curr_active_slave == new_slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1666 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1667 bond->alb_info.primary_is_promisc = 0;
1668 bond->alb_info.rlb_promisc_timeout_counter = 0;
1669 }
1670
1671 swap_slave = bond->curr_active_slave;
nikolay@redhat.com278b2082013-08-01 16:54:51 +02001672 rcu_assign_pointer(bond->curr_active_slave, new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001674 if (!new_slave || list_empty(&bond->slave_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* set the new curr_active_slave to the bonds mac address
1678 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1679 */
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001680 if (!swap_slave)
1681 swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001683 /*
1684 * Arrange for swap_slave and new_slave to temporarily be
1685 * ignored so we can mess with their MAC addresses without
1686 * fear of interference from transmit activity.
1687 */
nikolay@redhat.comdec1e902013-08-01 16:54:47 +02001688 if (swap_slave)
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001689 tlb_clear_slave(bond, swap_slave, 1);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001690 tlb_clear_slave(bond, new_slave, 1);
1691
1692 write_unlock_bh(&bond->curr_slave_lock);
1693 read_unlock(&bond->lock);
1694
Jay Vosburghe0138a62008-01-17 16:24:58 -08001695 ASSERT_RTNL();
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1698 if (swap_slave) {
1699 /* swap mac address */
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001700 alb_swap_mac_addr(swap_slave, new_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001701 alb_fasten_mac_swap(bond, swap_slave, new_slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001702 read_lock(&bond->lock);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001703 } else {
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001704 /* set the new_slave to the bond mac address */
1705 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
Jay Vosburgh25433312008-01-17 16:24:59 -08001706 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1708 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001709
1710 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001713/*
1714 * Called with RTNL
1715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001717 __acquires(&bond->lock)
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001718 __releases(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Wang Chen454d7c92008-11-12 23:37:49 -08001720 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct sockaddr *sa = addr;
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001722 struct slave *swap_slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (!is_valid_ether_addr(sa->sa_data)) {
1726 return -EADDRNOTAVAIL;
1727 }
1728
1729 res = alb_set_mac_address(bond, addr);
1730 if (res) {
1731 return res;
1732 }
1733
1734 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1735
1736 /* If there is no curr_active_slave there is nothing else to do.
1737 * Otherwise we'll need to pass the new address to it and handle
1738 * duplications.
1739 */
1740 if (!bond->curr_active_slave) {
1741 return 0;
1742 }
1743
Veaceslav Falicob88ec382013-06-18 13:44:52 +02001744 swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (swap_slave) {
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001747 alb_swap_mac_addr(swap_slave, bond->curr_active_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001748 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 } else {
Jiri Bohacb9245512012-01-18 12:24:54 +00001750 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001752 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1754 if (bond->alb_info.rlb_enabled) {
1755 /* inform clients mac address has changed */
1756 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1757 }
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001758 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return 0;
1762}
1763
1764void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1765{
1766 if (bond->alb_info.current_alb_vlan &&
1767 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1768 bond->alb_info.current_alb_vlan = NULL;
1769 }
1770
1771 if (bond->alb_info.rlb_enabled) {
1772 rlb_clear_vlan(bond, vlan_id);
1773 }
1774}
1775