blob: 27fe329bdf8350cad97ab6850a7e2eb7e6195e0f [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;
227 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Jiri Pirko097811b2010-05-19 03:26:39 +0000229 least_loaded = NULL;
230 max_gap = LLONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* Find the slave with the largest gap */
Jiri Pirko097811b2010-05-19 03:26:39 +0000233 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (SLAVE_IS_OK(slave)) {
Jiri Pirko097811b2010-05-19 03:26:39 +0000235 long long gap = compute_gap(slave);
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (max_gap < gap) {
238 least_loaded = slave;
239 max_gap = gap;
240 }
241 }
242 }
243
244 return least_loaded;
245}
246
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000247static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
248 u32 skb_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
251 struct tlb_client_info *hash_table;
252 struct slave *assigned_slave;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 hash_table = bond_info->tx_hashtbl;
255 assigned_slave = hash_table[hash_index].tx_slave;
256 if (!assigned_slave) {
257 assigned_slave = tlb_get_least_loaded_slave(bond);
258
259 if (assigned_slave) {
260 struct tlb_slave_info *slave_info =
261 &(SLAVE_TLB_INFO(assigned_slave));
262 u32 next_index = slave_info->head;
263
264 hash_table[hash_index].tx_slave = assigned_slave;
265 hash_table[hash_index].next = next_index;
266 hash_table[hash_index].prev = TLB_NULL_INDEX;
267
268 if (next_index != TLB_NULL_INDEX) {
269 hash_table[next_index].prev = hash_index;
270 }
271
272 slave_info->head = hash_index;
273 slave_info->load +=
274 hash_table[hash_index].load_history;
275 }
276 }
277
278 if (assigned_slave) {
279 hash_table[hash_index].tx_bytes += skb_len;
280 }
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return assigned_slave;
283}
284
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000285/* Caller must hold bond lock for read */
286static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
287 u32 skb_len)
288{
289 struct slave *tx_slave;
290 /*
291 * We don't need to disable softirq here, becase
292 * tlb_choose_channel() is only called by bond_alb_xmit()
293 * which already has softirq disabled.
294 */
295 _lock_tx_hashtbl(bond);
296 tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
297 _unlock_tx_hashtbl(bond);
298 return tx_slave;
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*********************** rlb specific functions ***************************/
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000302static inline void _lock_rx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700304 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000307static inline void _unlock_rx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700309 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000312static inline void _lock_rx_hashtbl(struct bonding *bond)
313{
314 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
315}
316
317static inline void _unlock_rx_hashtbl(struct bonding *bond)
318{
319 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/* when an ARP REPLY is received from a client update its info
323 * in the rx_hashtbl
324 */
325static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
326{
327 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
328 struct rlb_client_info *client_info;
329 u32 hash_index;
330
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000331 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
334 client_info = &(bond_info->rx_hashtbl[hash_index]);
335
336 if ((client_info->assigned) &&
337 (client_info->ip_src == arp->ip_dst) &&
Flavio Leitner42d782a2010-06-29 08:24:39 +0000338 (client_info->ip_dst == arp->ip_src) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000339 (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* update the clients MAC address */
341 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
342 client_info->ntt = 1;
343 bond_info->rx_ntt = 1;
344 }
345
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000346 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Eric Dumazetde063b72012-06-11 19:23:07 +0000349static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond,
350 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Eric Dumazetde063b72012-06-11 19:23:07 +0000352 struct arp_pkt *arp, _arp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Jiri Pirko3aba8912011-04-19 03:48:16 +0000354 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
David S. Millerb99215c2012-05-13 15:45:13 -0400355 goto out;
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800356
Eric Dumazetde063b72012-06-11 19:23:07 +0000357 arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp);
358 if (!arp)
David S. Millerb99215c2012-05-13 15:45:13 -0400359 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jiri Bohace53665c2012-11-28 04:42:14 +0000361 /* We received an ARP from arp->ip_src.
362 * We might have used this IP address previously (on the bonding host
363 * itself or on a system that is bridged together with the bond).
364 * However, if arp->mac_src is different than what is stored in
365 * rx_hashtbl, some other host is now using the IP and we must prevent
366 * sending out client updates with this IP address and the old MAC
367 * address.
368 * Clean up all hash table entries that have this address as ip_src but
369 * have a different mac_src.
370 */
371 rlb_purge_src_ip(bond, arp);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (arp->op_code == htons(ARPOP_REPLY)) {
374 /* update rx hash table for this ARP */
375 rlb_update_entry_from_arp(bond, arp);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800376 pr_debug("Server received an ARP Reply from client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
David S. Millerb99215c2012-05-13 15:45:13 -0400378out:
379 return RX_HANDLER_ANOTHER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382/* Caller must hold bond lock for read */
383static struct slave *rlb_next_rx_slave(struct bonding *bond)
384{
385 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
386 struct slave *rx_slave, *slave, *start_at;
387 int i = 0;
388
389 if (bond_info->next_rx_slave) {
390 start_at = bond_info->next_rx_slave;
391 } else {
392 start_at = bond->first_slave;
393 }
394
395 rx_slave = NULL;
396
397 bond_for_each_slave_from(bond, slave, i, start_at) {
398 if (SLAVE_IS_OK(slave)) {
399 if (!rx_slave) {
400 rx_slave = slave;
401 } else if (slave->speed > rx_slave->speed) {
402 rx_slave = slave;
403 }
404 }
405 }
406
407 if (rx_slave) {
408 bond_info->next_rx_slave = rx_slave->next;
409 }
410
411 return rx_slave;
412}
413
414/* teach the switch the mac of a disabled slave
415 * on the primary for fault tolerance
416 *
417 * Caller must hold bond->curr_slave_lock for write or bond lock for write
418 */
419static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
420{
421 if (!bond->curr_active_slave) {
422 return;
423 }
424
425 if (!bond->alb_info.primary_is_promisc) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700426 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
427 bond->alb_info.primary_is_promisc = 1;
428 else
429 bond->alb_info.primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 bond->alb_info.rlb_promisc_timeout_counter = 0;
433
434 alb_send_learning_packets(bond->curr_active_slave, addr);
435}
436
437/* slave being removed should not be active at this point
438 *
439 * Caller must hold bond lock for read
440 */
441static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
442{
443 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
444 struct rlb_client_info *rx_hash_table;
445 u32 index, next_index;
446
447 /* clear slave from rx_hashtbl */
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000448 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 rx_hash_table = bond_info->rx_hashtbl;
Jiri Bohace53665c2012-11-28 04:42:14 +0000451 index = bond_info->rx_hashtbl_used_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 for (; index != RLB_NULL_INDEX; index = next_index) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000453 next_index = rx_hash_table[index].used_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (rx_hash_table[index].slave == slave) {
455 struct slave *assigned_slave = rlb_next_rx_slave(bond);
456
457 if (assigned_slave) {
458 rx_hash_table[index].slave = assigned_slave;
Joe Perchesa6700db2012-05-09 17:04:04 +0000459 if (!ether_addr_equal_64bits(rx_hash_table[index].mac_dst,
460 mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 bond_info->rx_hashtbl[index].ntt = 1;
462 bond_info->rx_ntt = 1;
463 /* A slave has been removed from the
464 * table because it is either disabled
465 * or being released. We must retry the
466 * update to avoid clients from not
467 * being updated & disconnecting when
468 * there is stress
469 */
470 bond_info->rlb_update_retry_counter =
471 RLB_UPDATE_RETRY;
472 }
473 } else { /* there is no active slave */
474 rx_hash_table[index].slave = NULL;
475 }
476 }
477 }
478
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000479 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700481 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (slave != bond->curr_active_slave) {
484 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
485 }
486
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700487 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490static void rlb_update_client(struct rlb_client_info *client_info)
491{
492 int i;
493
494 if (!client_info->slave) {
495 return;
496 }
497
498 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
499 struct sk_buff *skb;
500
501 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
502 client_info->ip_dst,
503 client_info->slave->dev,
504 client_info->ip_src,
505 client_info->mac_dst,
506 client_info->slave->dev->dev_addr,
507 client_info->mac_dst);
508 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800509 pr_err("%s: Error: failed to create an ARP packet\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +0000510 client_info->slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 continue;
512 }
513
514 skb->dev = client_info->slave->dev;
515
516 if (client_info->tag) {
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000517 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), client_info->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800519 pr_err("%s: Error: failed to insert VLAN tag\n",
Jiri Pirko471cb5a2013-01-03 22:49:01 +0000520 client_info->slave->bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 continue;
522 }
523 }
524
525 arp_xmit(skb);
526 }
527}
528
529/* sends ARP REPLIES that update the clients that need updating */
530static void rlb_update_rx_clients(struct bonding *bond)
531{
532 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
533 struct rlb_client_info *client_info;
534 u32 hash_index;
535
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000536 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jiri Bohace53665c2012-11-28 04:42:14 +0000538 hash_index = bond_info->rx_hashtbl_used_head;
539 for (; hash_index != RLB_NULL_INDEX;
540 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 client_info = &(bond_info->rx_hashtbl[hash_index]);
542 if (client_info->ntt) {
543 rlb_update_client(client_info);
544 if (bond_info->rlb_update_retry_counter == 0) {
545 client_info->ntt = 0;
546 }
547 }
548 }
549
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200550 /* do not update the entries again until this counter is zero so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * not to confuse the clients.
552 */
553 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
554
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000555 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558/* The slave was assigned a new mac address - update the clients */
559static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
560{
561 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
562 struct rlb_client_info *client_info;
563 int ntt = 0;
564 u32 hash_index;
565
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000566 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Jiri Bohace53665c2012-11-28 04:42:14 +0000568 hash_index = bond_info->rx_hashtbl_used_head;
569 for (; hash_index != RLB_NULL_INDEX;
570 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 client_info = &(bond_info->rx_hashtbl[hash_index]);
572
573 if ((client_info->slave == slave) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000574 !ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 client_info->ntt = 1;
576 ntt = 1;
577 }
578 }
579
580 // update the team's flag only after the whole iteration
581 if (ntt) {
582 bond_info->rx_ntt = 1;
583 //fasten the change
584 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
585 }
586
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000587 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400591static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
594 struct rlb_client_info *client_info;
595 u32 hash_index;
596
597 _lock_rx_hashtbl(bond);
598
Jiri Bohace53665c2012-11-28 04:42:14 +0000599 hash_index = bond_info->rx_hashtbl_used_head;
600 for (; hash_index != RLB_NULL_INDEX;
601 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 client_info = &(bond_info->rx_hashtbl[hash_index]);
603
604 if (!client_info->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800605 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800606 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 continue;
608 }
609 /*update all clients using this src_ip, that are not assigned
610 * to the team's address (curr_active_slave) and have a known
611 * unicast mac address.
612 */
613 if ((client_info->ip_src == src_ip) &&
Joe Perchesa6700db2012-05-09 17:04:04 +0000614 !ether_addr_equal_64bits(client_info->slave->dev->dev_addr,
615 bond->dev->dev_addr) &&
616 !ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 client_info->ntt = 1;
618 bond_info->rx_ntt = 1;
619 }
620 }
621
622 _unlock_rx_hashtbl(bond);
623}
624
625/* Caller must hold both bond and ptr locks for read */
626static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
627{
628 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300629 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct slave *assigned_slave;
631 struct rlb_client_info *client_info;
632 u32 hash_index = 0;
633
634 _lock_rx_hashtbl(bond);
635
Amerigo Wange364a342011-02-27 23:34:28 +0000636 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 client_info = &(bond_info->rx_hashtbl[hash_index]);
638
639 if (client_info->assigned) {
640 if ((client_info->ip_src == arp->ip_src) &&
641 (client_info->ip_dst == arp->ip_dst)) {
642 /* the entry is already assigned to this client */
Joe Perchesa6700db2012-05-09 17:04:04 +0000643 if (!ether_addr_equal_64bits(arp->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* update mac address from arp */
645 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
646 }
Jiri Bohace53665c2012-11-28 04:42:14 +0000647 memcpy(client_info->mac_src, arp->mac_src, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 assigned_slave = client_info->slave;
650 if (assigned_slave) {
651 _unlock_rx_hashtbl(bond);
652 return assigned_slave;
653 }
654 } else {
655 /* the entry is already assigned to some other client,
656 * move the old client to primary (curr_active_slave) so
657 * that the new client can be assigned to this entry.
658 */
659 if (bond->curr_active_slave &&
660 client_info->slave != bond->curr_active_slave) {
661 client_info->slave = bond->curr_active_slave;
662 rlb_update_client(client_info);
663 }
664 }
665 }
666 /* assign a new slave */
667 assigned_slave = rlb_next_rx_slave(bond);
668
669 if (assigned_slave) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000670 if (!(client_info->assigned &&
671 client_info->ip_src == arp->ip_src)) {
672 /* ip_src is going to be updated,
673 * fix the src hash list
674 */
675 u32 hash_src = _simple_hash((u8 *)&arp->ip_src,
676 sizeof(arp->ip_src));
677 rlb_src_unlink(bond, hash_index);
678 rlb_src_link(bond, hash_src, hash_index);
679 }
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 client_info->ip_src = arp->ip_src;
682 client_info->ip_dst = arp->ip_dst;
683 /* arp->mac_dst is broadcast for arp reqeusts.
684 * will be updated with clients actual unicast mac address
685 * upon receiving an arp reply.
686 */
687 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
Jiri Bohace53665c2012-11-28 04:42:14 +0000688 memcpy(client_info->mac_src, arp->mac_src, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 client_info->slave = assigned_slave;
690
Joe Perchesa6700db2012-05-09 17:04:04 +0000691 if (!ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 client_info->ntt = 1;
693 bond->alb_info.rx_ntt = 1;
694 } else {
695 client_info->ntt = 0;
696 }
697
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000698 if (bond_vlan_used(bond)) {
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700699 if (!vlan_get_tag(skb, &client_info->vlan_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 client_info->tag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 if (!client_info->assigned) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000704 u32 prev_tbl_head = bond_info->rx_hashtbl_used_head;
705 bond_info->rx_hashtbl_used_head = hash_index;
706 client_info->used_next = prev_tbl_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (prev_tbl_head != RLB_NULL_INDEX) {
Jiri Bohace53665c2012-11-28 04:42:14 +0000708 bond_info->rx_hashtbl[prev_tbl_head].used_prev =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 hash_index;
710 }
711 client_info->assigned = 1;
712 }
713 }
714
715 _unlock_rx_hashtbl(bond);
716
717 return assigned_slave;
718}
719
720/* chooses (and returns) transmit channel for arp reply
721 * does not choose channel for other arp types since they are
722 * sent on the curr_active_slave
723 */
724static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
725{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300726 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct slave *tx_slave = NULL;
728
zheng.li567b8712012-11-27 23:57:04 +0000729 /* Don't modify or load balance ARPs that do not originate locally
730 * (e.g.,arrive via a bridge).
731 */
732 if (!bond_slave_has_mac(bond, arp->mac_src))
733 return NULL;
734
Brian Haleyf14c4e42008-09-02 10:08:08 -0400735 if (arp->op_code == htons(ARPOP_REPLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* the arp must be sent on the selected
737 * rx channel
738 */
739 tx_slave = rlb_choose_channel(skb, bond);
740 if (tx_slave) {
741 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
742 }
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800743 pr_debug("Server sent ARP Reply packet\n");
Brian Haleyf14c4e42008-09-02 10:08:08 -0400744 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* Create an entry in the rx_hashtbl for this client as a
746 * place holder.
747 * When the arp reply is received the entry will be updated
748 * with the correct unicast address of the client.
749 */
750 rlb_choose_channel(skb, bond);
751
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +0000752 /* The ARP reply packets must be delayed so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 * they can cancel out the influence of the ARP request.
754 */
755 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
756
757 /* arp requests are broadcast and are sent on the primary
758 * the arp request will collapse all clients on the subnet to
759 * the primary slave. We must register these clients to be
760 * updated with their assigned mac.
761 */
762 rlb_req_update_subnet_clients(bond, arp->ip_src);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800763 pr_debug("Server sent ARP Request packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
766 return tx_slave;
767}
768
769/* Caller must hold bond lock for read */
770static void rlb_rebalance(struct bonding *bond)
771{
772 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
773 struct slave *assigned_slave;
774 struct rlb_client_info *client_info;
775 int ntt;
776 u32 hash_index;
777
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000778 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 ntt = 0;
Jiri Bohace53665c2012-11-28 04:42:14 +0000781 hash_index = bond_info->rx_hashtbl_used_head;
782 for (; hash_index != RLB_NULL_INDEX;
783 hash_index = client_info->used_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 client_info = &(bond_info->rx_hashtbl[hash_index]);
785 assigned_slave = rlb_next_rx_slave(bond);
786 if (assigned_slave && (client_info->slave != assigned_slave)) {
787 client_info->slave = assigned_slave;
788 client_info->ntt = 1;
789 ntt = 1;
790 }
791 }
792
793 /* update the team's flag only after the whole iteration */
794 if (ntt) {
795 bond_info->rx_ntt = 1;
796 }
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000797 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800/* Caller must hold rx_hashtbl lock */
Jiri Bohace53665c2012-11-28 04:42:14 +0000801static void rlb_init_table_entry_dst(struct rlb_client_info *entry)
802{
803 entry->used_next = RLB_NULL_INDEX;
804 entry->used_prev = RLB_NULL_INDEX;
805 entry->assigned = 0;
806 entry->slave = NULL;
807 entry->tag = 0;
808}
809static void rlb_init_table_entry_src(struct rlb_client_info *entry)
810{
811 entry->src_first = RLB_NULL_INDEX;
812 entry->src_prev = RLB_NULL_INDEX;
813 entry->src_next = RLB_NULL_INDEX;
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static void rlb_init_table_entry(struct rlb_client_info *entry)
817{
818 memset(entry, 0, sizeof(struct rlb_client_info));
Jiri Bohace53665c2012-11-28 04:42:14 +0000819 rlb_init_table_entry_dst(entry);
820 rlb_init_table_entry_src(entry);
821}
822
823static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index)
824{
825 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
826 u32 next_index = bond_info->rx_hashtbl[index].used_next;
827 u32 prev_index = bond_info->rx_hashtbl[index].used_prev;
828
829 if (index == bond_info->rx_hashtbl_used_head)
830 bond_info->rx_hashtbl_used_head = next_index;
831 if (prev_index != RLB_NULL_INDEX)
832 bond_info->rx_hashtbl[prev_index].used_next = next_index;
833 if (next_index != RLB_NULL_INDEX)
834 bond_info->rx_hashtbl[next_index].used_prev = prev_index;
835}
836
837/* unlink a rlb hash table entry from the src list */
838static void rlb_src_unlink(struct bonding *bond, u32 index)
839{
840 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
841 u32 next_index = bond_info->rx_hashtbl[index].src_next;
842 u32 prev_index = bond_info->rx_hashtbl[index].src_prev;
843
844 bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX;
845 bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX;
846
847 if (next_index != RLB_NULL_INDEX)
848 bond_info->rx_hashtbl[next_index].src_prev = prev_index;
849
850 if (prev_index == RLB_NULL_INDEX)
851 return;
852
853 /* is prev_index pointing to the head of this list? */
854 if (bond_info->rx_hashtbl[prev_index].src_first == index)
855 bond_info->rx_hashtbl[prev_index].src_first = next_index;
856 else
857 bond_info->rx_hashtbl[prev_index].src_next = next_index;
858
859}
860
861static void rlb_delete_table_entry(struct bonding *bond, u32 index)
862{
863 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
864 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
865
866 rlb_delete_table_entry_dst(bond, index);
867 rlb_init_table_entry_dst(entry);
868
869 rlb_src_unlink(bond, index);
870}
871
872/* add the rx_hashtbl[ip_dst_hash] entry to the list
873 * of entries with identical ip_src_hash
874 */
875static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash)
876{
877 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
878 u32 next;
879
880 bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash;
881 next = bond_info->rx_hashtbl[ip_src_hash].src_first;
882 bond_info->rx_hashtbl[ip_dst_hash].src_next = next;
883 if (next != RLB_NULL_INDEX)
884 bond_info->rx_hashtbl[next].src_prev = ip_dst_hash;
885 bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash;
886}
887
888/* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does
889 * not match arp->mac_src */
890static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp)
891{
892 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
893 u32 ip_src_hash = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
894 u32 index;
895
896 _lock_rx_hashtbl_bh(bond);
897
898 index = bond_info->rx_hashtbl[ip_src_hash].src_first;
899 while (index != RLB_NULL_INDEX) {
900 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]);
901 u32 next_index = entry->src_next;
902 if (entry->ip_src == arp->ip_src &&
903 !ether_addr_equal_64bits(arp->mac_src, entry->mac_src))
904 rlb_delete_table_entry(bond, index);
905 index = next_index;
906 }
907 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static int rlb_initialize(struct bonding *bond)
911{
912 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mitch Williams0d206a32005-11-09 10:35:30 -0800913 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
915 int i;
916
Mitch Williams0d206a32005-11-09 10:35:30 -0800917 new_hashtbl = kmalloc(size, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000918 if (!new_hashtbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -1;
Joe Perchese404dec2012-01-29 12:56:23 +0000920
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000921 _lock_rx_hashtbl_bh(bond);
Mitch Williams0d206a32005-11-09 10:35:30 -0800922
923 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Jiri Bohace53665c2012-11-28 04:42:14 +0000925 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
928 rlb_init_table_entry(bond_info->rx_hashtbl + i);
929 }
930
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000931 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* register to receive ARPs */
Jiri Pirko3aba8912011-04-19 03:48:16 +0000934 bond->recv_probe = rlb_arp_recv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 return 0;
937}
938
939static void rlb_deinitialize(struct bonding *bond)
940{
941 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
942
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000943 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 kfree(bond_info->rx_hashtbl);
946 bond_info->rx_hashtbl = NULL;
Jiri Bohace53665c2012-11-28 04:42:14 +0000947 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000949 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
953{
954 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
955 u32 curr_index;
956
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000957 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Jiri Bohace53665c2012-11-28 04:42:14 +0000959 curr_index = bond_info->rx_hashtbl_used_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 while (curr_index != RLB_NULL_INDEX) {
961 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
Jiri Bohace53665c2012-11-28 04:42:14 +0000962 u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Jiri Bohace53665c2012-11-28 04:42:14 +0000964 if (curr->tag && (curr->vlan_id == vlan_id))
965 rlb_delete_table_entry(bond, curr_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 curr_index = next_index;
968 }
969
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000970 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973/*********************** tlb/rlb shared functions *********************/
974
975static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
976{
977 struct bonding *bond = bond_get_bond_by_slave(slave);
978 struct learning_pkt pkt;
979 int size = sizeof(struct learning_pkt);
980 int i;
981
982 memset(&pkt, 0, size);
983 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
984 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
Harvey Harrison09640e632009-02-01 00:45:17 -0800985 pkt.type = cpu_to_be16(ETH_P_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 for (i = 0; i < MAX_LP_BURST; i++) {
988 struct sk_buff *skb;
989 char *data;
990
991 skb = dev_alloc_skb(size);
992 if (!skb) {
993 return;
994 }
995
996 data = skb_put(skb, size);
997 memcpy(data, &pkt, size);
998
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700999 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001000 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 skb->protocol = pkt.type;
1002 skb->priority = TC_PRIO_CONTROL;
1003 skb->dev = slave->dev;
1004
Jiri Pirkocc0e4072011-07-20 04:54:46 +00001005 if (bond_vlan_used(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct vlan_entry *vlan;
1007
1008 vlan = bond_next_vlan(bond,
1009 bond->alb_info.current_alb_vlan);
1010
1011 bond->alb_info.current_alb_vlan = vlan;
1012 if (!vlan) {
1013 kfree_skb(skb);
1014 continue;
1015 }
1016
Patrick McHardy86a9bad2013-04-19 02:04:30 +00001017 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), vlan->vlan_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001019 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001020 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 continue;
1022 }
1023 }
1024
1025 dev_queue_xmit(skb);
1026 }
1027}
1028
Jiri Bohacb9245512012-01-18 12:24:54 +00001029static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 struct net_device *dev = slave->dev;
1032 struct sockaddr s_addr;
1033
Jiri Bohacb9245512012-01-18 12:24:54 +00001034 if (slave->bond->params.mode == BOND_MODE_TLB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 memcpy(dev->dev_addr, addr, dev->addr_len);
1036 return 0;
1037 }
1038
1039 /* for rlb each slave must have a unique hw mac addresses so that */
1040 /* each slave will receive packets destined to a different mac */
1041 memcpy(s_addr.sa_data, addr, dev->addr_len);
1042 s_addr.sa_family = dev->type;
1043 if (dev_set_mac_address(dev, &s_addr)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001044 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
1045 "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 +00001046 slave->bond->dev->name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -EOPNOTSUPP;
1048 }
1049 return 0;
1050}
1051
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001052/*
1053 * Swap MAC addresses between two slaves.
1054 *
1055 * Called with RTNL held, and no other locks.
1056 *
1057 */
1058
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001059static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
Jiri Bohacb9245512012-01-18 12:24:54 +00001064 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
1065 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001067}
1068
1069/*
1070 * Send learning packets after MAC address swap.
1071 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001072 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001073 */
1074static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
1075 struct slave *slave2)
1076{
1077 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
1078 struct slave *disabled_slave = NULL;
1079
Jay Vosburgh25433312008-01-17 16:24:59 -08001080 ASSERT_RTNL();
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* fasten the change in the switch */
1083 if (SLAVE_IS_OK(slave1)) {
1084 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
1085 if (bond->alb_info.rlb_enabled) {
1086 /* inform the clients that the mac address
1087 * has changed
1088 */
1089 rlb_req_update_slave_clients(bond, slave1);
1090 }
1091 } else {
1092 disabled_slave = slave1;
1093 }
1094
1095 if (SLAVE_IS_OK(slave2)) {
1096 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1097 if (bond->alb_info.rlb_enabled) {
1098 /* inform the clients that the mac address
1099 * has changed
1100 */
1101 rlb_req_update_slave_clients(bond, slave2);
1102 }
1103 } else {
1104 disabled_slave = slave2;
1105 }
1106
1107 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1108 /* A disabled slave was assigned an active mac addr */
1109 rlb_teach_disabled_mac_on_primary(bond,
1110 disabled_slave->dev->dev_addr);
1111 }
1112}
1113
1114/**
1115 * alb_change_hw_addr_on_detach
1116 * @bond: bonding we're working on
1117 * @slave: the slave that was just detached
1118 *
1119 * We assume that @slave was already detached from the slave list.
1120 *
1121 * If @slave's permanent hw address is different both from its current
1122 * address and from @bond's address, then somewhere in the bond there's
1123 * a slave that has @slave's permanet address as its current address.
1124 * We'll make sure that that slave no longer uses @slave's permanent address.
1125 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001126 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
1128static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1129{
1130 int perm_curr_diff;
1131 int perm_bond_diff;
1132
Joe Perchesa6700db2012-05-09 17:04:04 +00001133 perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1134 slave->dev->dev_addr);
1135 perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1136 bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if (perm_curr_diff && perm_bond_diff) {
1139 struct slave *tmp_slave;
1140 int i, found = 0;
1141
1142 bond_for_each_slave(bond, tmp_slave, i) {
Joe Perchesa6700db2012-05-09 17:04:04 +00001143 if (ether_addr_equal_64bits(slave->perm_hwaddr,
1144 tmp_slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 found = 1;
1146 break;
1147 }
1148 }
1149
1150 if (found) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001151 /* locking: needs RTNL and nothing else */
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001152 alb_swap_mac_addr(slave, tmp_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001153 alb_fasten_mac_swap(bond, slave, tmp_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 }
1156}
1157
1158/**
1159 * alb_handle_addr_collision_on_attach
1160 * @bond: bonding we're working on
1161 * @slave: the slave that was just attached
1162 *
1163 * checks uniqueness of slave's mac address and handles the case the
1164 * new slave uses the bonds mac address.
1165 *
1166 * If the permanent hw address of @slave is @bond's hw address, we need to
1167 * find a different hw address to give @slave, that isn't in use by any other
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +00001168 * slave in the bond. This address must be, of course, one of the permanent
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * addresses of the other slaves.
1170 *
1171 * We go over the slave list, and for each slave there we compare its
1172 * permanent hw address with the current address of all the other slaves.
1173 * If no match was found, then we've found a slave with a permanent address
1174 * that isn't used by any other slave in the bond, so we can assign it to
1175 * @slave.
1176 *
1177 * assumption: this function is called before @slave is attached to the
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001178 * bond slave list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 */
1180static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1181{
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001182 struct slave *tmp_slave1, *free_mac_slave = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct slave *has_bond_addr = bond->curr_active_slave;
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001184 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (bond->slave_cnt == 0) {
1187 /* this is the first slave */
1188 return 0;
1189 }
1190
1191 /* if slave's mac address differs from bond's mac address
1192 * check uniqueness of slave's mac address against the other
1193 * slaves in the bond.
1194 */
Joe Perchesa6700db2012-05-09 17:04:04 +00001195 if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001196 if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
John W. Linville6b38aef2005-07-28 15:00:15 -04001197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
John W. Linville6b38aef2005-07-28 15:00:15 -04001199 /* Try setting slave mac to bond address and fall-through
1200 to code handling that situation below... */
Jiri Bohacb9245512012-01-18 12:24:54 +00001201 alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203
1204 /* The slave's address is equal to the address of the bond.
1205 * Search for a spare address in the bond for this slave.
1206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 bond_for_each_slave(bond, tmp_slave1, i) {
Veaceslav Falicocedb7432013-06-17 19:30:35 +02001208 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* no slave has tmp_slave1's perm addr
1210 * as its curr addr
1211 */
1212 free_mac_slave = tmp_slave1;
1213 break;
1214 }
1215
1216 if (!has_bond_addr) {
Joe Perchesa6700db2012-05-09 17:04:04 +00001217 if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
1218 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 has_bond_addr = tmp_slave1;
1221 }
1222 }
1223 }
1224
1225 if (free_mac_slave) {
Jiri Bohacb9245512012-01-18 12:24:54 +00001226 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001228 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 +00001229 bond->dev->name, slave->dev->name,
1230 free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 } else if (has_bond_addr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001233 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 -08001234 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return -EFAULT;
1236 }
1237
1238 return 0;
1239}
1240
1241/**
1242 * alb_set_mac_address
1243 * @bond:
1244 * @addr:
1245 *
1246 * In TLB mode all slaves are configured to the bond's hw address, but set
1247 * their dev_addr field to different addresses (based on their permanent hw
1248 * addresses).
1249 *
1250 * For each slave, this function sets the interface to the new address and then
1251 * changes its dev_addr field to its previous value.
1252 *
1253 * Unwinding assumes bond's mac address has not yet changed.
1254 */
1255static int alb_set_mac_address(struct bonding *bond, void *addr)
1256{
1257 struct sockaddr sa;
1258 struct slave *slave, *stop_at;
1259 char tmp_addr[ETH_ALEN];
1260 int res;
1261 int i;
1262
1263 if (bond->alb_info.rlb_enabled) {
1264 return 0;
1265 }
1266
1267 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 /* save net_device's current hw address */
1269 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1270
1271 res = dev_set_mac_address(slave->dev, addr);
1272
1273 /* restore net_device's hw address */
1274 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1275
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001276 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
1280 return 0;
1281
1282unwind:
1283 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1284 sa.sa_family = bond->dev->type;
1285
1286 /* unwind from head to the slave that failed */
1287 stop_at = slave;
1288 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1289 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1290 dev_set_mac_address(slave->dev, &sa);
1291 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1292 }
1293
1294 return res;
1295}
1296
1297/************************ exported alb funcions ************************/
1298
1299int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1300{
1301 int res;
1302
1303 res = tlb_initialize(bond);
1304 if (res) {
1305 return res;
1306 }
1307
1308 if (rlb_enabled) {
1309 bond->alb_info.rlb_enabled = 1;
1310 /* initialize rlb */
1311 res = rlb_initialize(bond);
1312 if (res) {
1313 tlb_deinitialize(bond);
1314 return res;
1315 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001316 } else {
1317 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
1320 return 0;
1321}
1322
1323void bond_alb_deinitialize(struct bonding *bond)
1324{
1325 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1326
1327 tlb_deinitialize(bond);
1328
1329 if (bond_info->rlb_enabled) {
1330 rlb_deinitialize(bond);
1331 }
1332}
1333
1334int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1335{
Wang Chen454d7c92008-11-12 23:37:49 -08001336 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 struct ethhdr *eth_data;
1338 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1339 struct slave *tx_slave = NULL;
Al Virod3bb52b2007-08-22 20:06:58 -04001340 static const __be32 ip_bcast = htonl(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int hash_size = 0;
1342 int do_tx_balance = 1;
1343 u32 hash_index = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001344 const u8 *hash_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 int res = 1;
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001346 struct ipv6hdr *ip6hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001348 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 eth_data = eth_hdr(skb);
1350
Michał Mirosław0693e882011-05-07 01:48:02 +00001351 /* make sure that the curr_active_slave do not change during tx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 read_lock(&bond->curr_slave_lock);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 switch (ntohs(skb->protocol)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001356 case ETH_P_IP: {
1357 const struct iphdr *iph = ip_hdr(skb);
1358
Joe Perchesa6700db2012-05-09 17:04:04 +00001359 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001360 (iph->daddr == ip_bcast) ||
1361 (iph->protocol == IPPROTO_IGMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 do_tx_balance = 0;
1363 break;
1364 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001365 hash_start = (char *)&(iph->daddr);
1366 hash_size = sizeof(iph->daddr);
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break;
1369 case ETH_P_IPV6:
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001370 /* IPv6 doesn't really use broadcast mac address, but leave
1371 * that here just in case.
1372 */
Joe Perchesa6700db2012-05-09 17:04:04 +00001373 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 do_tx_balance = 0;
1375 break;
1376 }
1377
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001378 /* IPv6 uses all-nodes multicast as an equivalent to
1379 * broadcasts in IPv4.
1380 */
Joe Perchesa6700db2012-05-09 17:04:04 +00001381 if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001382 do_tx_balance = 0;
1383 break;
1384 }
1385
1386 /* Additianally, DAD probes should not be tx-balanced as that
1387 * will lead to false positives for duplicate addresses and
1388 * prevent address configuration from working.
1389 */
1390 ip6hdr = ipv6_hdr(skb);
1391 if (ipv6_addr_any(&ip6hdr->saddr)) {
1392 do_tx_balance = 0;
1393 break;
1394 }
1395
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001396 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1397 hash_size = sizeof(ipv6_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 case ETH_P_IPX:
Al Virod3bb52b2007-08-22 20:06:58 -04001400 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* something is wrong with this packet */
1402 do_tx_balance = 0;
1403 break;
1404 }
1405
1406 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1407 /* The only protocol worth balancing in
1408 * this family since it has an "ARP" like
1409 * mechanism
1410 */
1411 do_tx_balance = 0;
1412 break;
1413 }
1414
1415 hash_start = (char*)eth_data->h_dest;
1416 hash_size = ETH_ALEN;
1417 break;
1418 case ETH_P_ARP:
1419 do_tx_balance = 0;
1420 if (bond_info->rlb_enabled) {
1421 tx_slave = rlb_arp_xmit(skb, bond);
1422 }
1423 break;
1424 default:
1425 do_tx_balance = 0;
1426 break;
1427 }
1428
1429 if (do_tx_balance) {
1430 hash_index = _simple_hash(hash_start, hash_size);
1431 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1432 }
1433
1434 if (!tx_slave) {
1435 /* unbalanced or unassigned, send through primary */
1436 tx_slave = bond->curr_active_slave;
1437 bond_info->unbalanced_load += skb->len;
1438 }
1439
1440 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1441 if (tx_slave != bond->curr_active_slave) {
1442 memcpy(eth_data->h_source,
1443 tx_slave->dev->dev_addr,
1444 ETH_ALEN);
1445 }
1446
1447 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1448 } else {
1449 if (tx_slave) {
Maxim Uvarovf515e6b2012-01-09 12:01:37 +00001450 _lock_tx_hashtbl(bond);
1451 __tlb_clear_slave(bond, tx_slave, 0);
1452 _unlock_tx_hashtbl(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 }
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 read_unlock(&bond->curr_slave_lock);
Michał Mirosław0693e882011-05-07 01:48:02 +00001457
Eric Dumazet04502432012-06-13 05:30:07 +00001458 if (res) {
1459 /* no suitable interface, frame not sent */
1460 kfree_skb(skb);
1461 }
Patrick McHardyec634fe2009-07-05 19:23:38 -07001462 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001465void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001467 struct bonding *bond = container_of(work, struct bonding,
1468 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1470 struct slave *slave;
1471 int i;
1472
1473 read_lock(&bond->lock);
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (bond->slave_cnt == 0) {
1476 bond_info->tx_rebalance_counter = 0;
1477 bond_info->lp_counter = 0;
1478 goto re_arm;
1479 }
1480
1481 bond_info->tx_rebalance_counter++;
1482 bond_info->lp_counter++;
1483
1484 /* send learning packets */
1485 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1486 /* change of curr_active_slave involves swapping of mac addresses.
1487 * in order to avoid this swapping from happening while
1488 * sending the learning packets, the curr_slave_lock must be held for
1489 * read.
1490 */
1491 read_lock(&bond->curr_slave_lock);
1492
1493 bond_for_each_slave(bond, slave, i) {
Mitch Williamse944ef72005-11-09 10:36:50 -08001494 alb_send_learning_packets(slave, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 read_unlock(&bond->curr_slave_lock);
1498
1499 bond_info->lp_counter = 0;
1500 }
1501
1502 /* rebalance tx traffic */
1503 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1504
1505 read_lock(&bond->curr_slave_lock);
1506
1507 bond_for_each_slave(bond, slave, i) {
1508 tlb_clear_slave(bond, slave, 1);
1509 if (slave == bond->curr_active_slave) {
1510 SLAVE_TLB_INFO(slave).load =
1511 bond_info->unbalanced_load /
1512 BOND_TLB_REBALANCE_INTERVAL;
1513 bond_info->unbalanced_load = 0;
1514 }
1515 }
1516
1517 read_unlock(&bond->curr_slave_lock);
1518
1519 bond_info->tx_rebalance_counter = 0;
1520 }
1521
1522 /* handle rlb stuff */
1523 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (bond_info->primary_is_promisc &&
1525 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1526
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001527 /*
1528 * dev_set_promiscuity requires rtnl and
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001529 * nothing else. Avoid race with bond_close.
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001530 */
1531 read_unlock(&bond->lock);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001532 if (!rtnl_trylock()) {
1533 read_lock(&bond->lock);
1534 goto re_arm;
1535 }
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 bond_info->rlb_promisc_timeout_counter = 0;
1538
1539 /* If the primary was set to promiscuous mode
1540 * because a slave was disabled then
1541 * it can now leave promiscuous mode.
1542 */
1543 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1544 bond_info->primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001546 rtnl_unlock();
1547 read_lock(&bond->lock);
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 if (bond_info->rlb_rebalance) {
1551 bond_info->rlb_rebalance = 0;
1552 rlb_rebalance(bond);
1553 }
1554
1555 /* check if clients need updating */
1556 if (bond_info->rx_ntt) {
1557 if (bond_info->rlb_update_delay_counter) {
1558 --bond_info->rlb_update_delay_counter;
1559 } else {
1560 rlb_update_rx_clients(bond);
1561 if (bond_info->rlb_update_retry_counter) {
1562 --bond_info->rlb_update_retry_counter;
1563 } else {
1564 bond_info->rx_ntt = 0;
1565 }
1566 }
1567 }
1568 }
1569
1570re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001571 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 read_unlock(&bond->lock);
1574}
1575
1576/* assumption: called before the slave is attached to the bond
1577 * and not locked by the bond lock
1578 */
1579int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1580{
1581 int res;
1582
Jiri Bohacb9245512012-01-18 12:24:54 +00001583 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (res) {
1585 return res;
1586 }
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 res = alb_handle_addr_collision_on_attach(bond, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (res) {
1590 return res;
1591 }
1592
1593 tlb_init_slave(slave);
1594
1595 /* order a rebalance ASAP */
1596 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1597
1598 if (bond->alb_info.rlb_enabled) {
1599 bond->alb_info.rlb_rebalance = 1;
1600 }
1601
1602 return 0;
1603}
1604
Jay Vosburgh25433312008-01-17 16:24:59 -08001605/*
1606 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1607 * if necessary.
1608 *
1609 * Caller must hold RTNL and no other locks
1610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1612{
1613 if (bond->slave_cnt > 1) {
1614 alb_change_hw_addr_on_detach(bond, slave);
1615 }
1616
1617 tlb_clear_slave(bond, slave, 0);
1618
1619 if (bond->alb_info.rlb_enabled) {
1620 bond->alb_info.next_rx_slave = NULL;
1621 rlb_clear_slave(bond, slave);
1622 }
1623}
1624
1625/* Caller must hold bond lock for read */
1626void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1627{
1628 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1629
1630 if (link == BOND_LINK_DOWN) {
1631 tlb_clear_slave(bond, slave, 0);
1632 if (bond->alb_info.rlb_enabled) {
1633 rlb_clear_slave(bond, slave);
1634 }
1635 } else if (link == BOND_LINK_UP) {
1636 /* order a rebalance ASAP */
1637 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1638 if (bond->alb_info.rlb_enabled) {
1639 bond->alb_info.rlb_rebalance = 1;
1640 /* If the updelay module parameter is smaller than the
1641 * forwarding delay of the switch the rebalance will
1642 * not work because the rebalance arp replies will
1643 * not be forwarded to the clients..
1644 */
1645 }
1646 }
1647}
1648
1649/**
1650 * bond_alb_handle_active_change - assign new curr_active_slave
1651 * @bond: our bonding struct
1652 * @new_slave: new slave to assign
1653 *
1654 * Set the bond->curr_active_slave to @new_slave and handle
1655 * mac address swapping and promiscuity changes as needed.
1656 *
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001657 * If new_slave is NULL, caller must hold curr_slave_lock or
1658 * bond->lock for write.
1659 *
1660 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1661 * read and curr_slave_lock for write. Processing here may sleep, so
1662 * no other locks may be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 */
1664void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001665 __releases(&bond->curr_slave_lock)
1666 __releases(&bond->lock)
1667 __acquires(&bond->lock)
1668 __acquires(&bond->curr_slave_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
1670 struct slave *swap_slave;
1671 int i;
1672
1673 if (bond->curr_active_slave == new_slave) {
1674 return;
1675 }
1676
1677 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1678 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1679 bond->alb_info.primary_is_promisc = 0;
1680 bond->alb_info.rlb_promisc_timeout_counter = 0;
1681 }
1682
1683 swap_slave = bond->curr_active_slave;
1684 bond->curr_active_slave = new_slave;
1685
1686 if (!new_slave || (bond->slave_cnt == 0)) {
1687 return;
1688 }
1689
1690 /* set the new curr_active_slave to the bonds mac address
1691 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1692 */
1693 if (!swap_slave) {
1694 struct slave *tmp_slave;
1695 /* find slave that is holding the bond's mac address */
1696 bond_for_each_slave(bond, tmp_slave, i) {
Joe Perchesa6700db2012-05-09 17:04:04 +00001697 if (ether_addr_equal_64bits(tmp_slave->dev->dev_addr,
1698 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 swap_slave = tmp_slave;
1700 break;
1701 }
1702 }
1703 }
1704
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001705 /*
1706 * Arrange for swap_slave and new_slave to temporarily be
1707 * ignored so we can mess with their MAC addresses without
1708 * fear of interference from transmit activity.
1709 */
1710 if (swap_slave) {
1711 tlb_clear_slave(bond, swap_slave, 1);
1712 }
1713 tlb_clear_slave(bond, new_slave, 1);
1714
1715 write_unlock_bh(&bond->curr_slave_lock);
1716 read_unlock(&bond->lock);
1717
Jay Vosburghe0138a62008-01-17 16:24:58 -08001718 ASSERT_RTNL();
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1721 if (swap_slave) {
1722 /* swap mac address */
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001723 alb_swap_mac_addr(swap_slave, new_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 } else {
1725 /* set the new_slave to the bond mac address */
Jiri Bohacb9245512012-01-18 12:24:54 +00001726 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001727 }
1728
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001729 if (swap_slave) {
1730 alb_fasten_mac_swap(bond, swap_slave, new_slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001731 read_lock(&bond->lock);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001732 } else {
Jay Vosburgh25433312008-01-17 16:24:59 -08001733 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1735 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001736
1737 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001740/*
1741 * Called with RTNL
1742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001744 __acquires(&bond->lock)
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001745 __releases(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Wang Chen454d7c92008-11-12 23:37:49 -08001747 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 struct sockaddr *sa = addr;
1749 struct slave *slave, *swap_slave;
1750 int res;
1751 int i;
1752
1753 if (!is_valid_ether_addr(sa->sa_data)) {
1754 return -EADDRNOTAVAIL;
1755 }
1756
1757 res = alb_set_mac_address(bond, addr);
1758 if (res) {
1759 return res;
1760 }
1761
1762 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1763
1764 /* If there is no curr_active_slave there is nothing else to do.
1765 * Otherwise we'll need to pass the new address to it and handle
1766 * duplications.
1767 */
1768 if (!bond->curr_active_slave) {
1769 return 0;
1770 }
1771
1772 swap_slave = NULL;
1773
1774 bond_for_each_slave(bond, slave, i) {
Joe Perchesa6700db2012-05-09 17:04:04 +00001775 if (ether_addr_equal_64bits(slave->dev->dev_addr,
1776 bond_dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 swap_slave = slave;
1778 break;
1779 }
1780 }
1781
1782 if (swap_slave) {
Veaceslav Falico43547ea2013-05-27 23:14:51 +00001783 alb_swap_mac_addr(swap_slave, bond->curr_active_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001784 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 } else {
Jiri Bohacb9245512012-01-18 12:24:54 +00001786 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001788 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1790 if (bond->alb_info.rlb_enabled) {
1791 /* inform clients mac address has changed */
1792 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1793 }
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001794 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
1798}
1799
1800void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1801{
1802 if (bond->alb_info.current_alb_vlan &&
1803 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1804 bond->alb_info.current_alb_vlan = NULL;
1805 }
1806
1807 if (bond->alb_info.rlb_enabled) {
1808 rlb_clear_vlan(bond, vlan_id);
1809 }
1810}
1811