xref: /linux/drivers/net/bonding/bond_alb.c (revision 13abf8130139c2ccd4962a7e5a8902be5e6cb5a7)
1 /*
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  *
21  *
22  * Changes:
23  *
24  * 2003/06/25 - Shmulik Hen <shmulik.hen at intel dot com>
25  *	- Fixed signed/unsigned calculation errors that caused load sharing
26  *	  to collapse to one slave under very heavy UDP Tx stress.
27  *
28  * 2003/08/06 - Amir Noam <amir.noam at intel dot com>
29  *	- Add support for setting bond's MAC address with special
30  *	  handling required for ALB/TLB.
31  *
32  * 2003/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
33  *	- Code cleanup and style changes
34  *
35  * 2003/12/30 - Amir Noam <amir.noam at intel dot com>
36  *	- Fixed: Cannot remove and re-enslave the original active slave.
37  *
38  * 2004/01/14 - Shmulik Hen <shmulik.hen at intel dot com>
39  *	- Add capability to tag self generated packets in ALB/TLB modes.
40  */
41 
42 //#define BONDING_DEBUG 1
43 
44 #include <linux/skbuff.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/pkt_sched.h>
48 #include <linux/spinlock.h>
49 #include <linux/slab.h>
50 #include <linux/timer.h>
51 #include <linux/ip.h>
52 #include <linux/ipv6.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_ether.h>
55 #include <linux/if_bonding.h>
56 #include <linux/if_vlan.h>
57 #include <linux/in.h>
58 #include <net/ipx.h>
59 #include <net/arp.h>
60 #include <asm/byteorder.h>
61 #include "bonding.h"
62 #include "bond_alb.h"
63 
64 
65 #define ALB_TIMER_TICKS_PER_SEC	    10	/* should be a divisor of HZ */
66 #define BOND_TLB_REBALANCE_INTERVAL 10	/* In seconds, periodic re-balancing.
67 					 * Used for division - never set
68 					 * to zero !!!
69 					 */
70 #define BOND_ALB_LP_INTERVAL	    1	/* In seconds, periodic send of
71 					 * learning packets to the switch
72 					 */
73 
74 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
75 				  * ALB_TIMER_TICKS_PER_SEC)
76 
77 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
78 			   * ALB_TIMER_TICKS_PER_SEC)
79 
80 #define TLB_HASH_TABLE_SIZE 256	/* The size of the clients hash table.
81 				 * Note that this value MUST NOT be smaller
82 				 * because the key hash table is BYTE wide !
83 				 */
84 
85 
86 #define TLB_NULL_INDEX		0xffffffff
87 #define MAX_LP_BURST		3
88 
89 /* rlb defs */
90 #define RLB_HASH_TABLE_SIZE	256
91 #define RLB_NULL_INDEX		0xffffffff
92 #define RLB_UPDATE_DELAY	2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
93 #define RLB_ARP_BURST_SIZE	2
94 #define RLB_UPDATE_RETRY	3	/* 3-ticks - must be smaller than the rlb
95 					 * rebalance interval (5 min).
96 					 */
97 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
98  * promiscuous after failover
99  */
100 #define RLB_PROMISC_TIMEOUT	10*ALB_TIMER_TICKS_PER_SEC
101 
102 static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
103 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
104 
105 #pragma pack(1)
106 struct learning_pkt {
107 	u8 mac_dst[ETH_ALEN];
108 	u8 mac_src[ETH_ALEN];
109 	u16 type;
110 	u8 padding[ETH_ZLEN - ETH_HLEN];
111 };
112 
113 struct arp_pkt {
114 	u16     hw_addr_space;
115 	u16     prot_addr_space;
116 	u8      hw_addr_len;
117 	u8      prot_addr_len;
118 	u16     op_code;
119 	u8      mac_src[ETH_ALEN];	/* sender hardware address */
120 	u32     ip_src;			/* sender IP address */
121 	u8      mac_dst[ETH_ALEN];	/* target hardware address */
122 	u32     ip_dst;			/* target IP address */
123 };
124 #pragma pack()
125 
126 /* Forward declaration */
127 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
128 
129 static inline u8 _simple_hash(u8 *hash_start, int hash_size)
130 {
131 	int i;
132 	u8 hash = 0;
133 
134 	for (i = 0; i < hash_size; i++) {
135 		hash ^= hash_start[i];
136 	}
137 
138 	return hash;
139 }
140 
141 /*********************** tlb specific functions ***************************/
142 
143 static inline void _lock_tx_hashtbl(struct bonding *bond)
144 {
145 	spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
146 }
147 
148 static inline void _unlock_tx_hashtbl(struct bonding *bond)
149 {
150 	spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
151 }
152 
153 /* Caller must hold tx_hashtbl lock */
154 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
155 {
156 	if (save_load) {
157 		entry->load_history = 1 + entry->tx_bytes /
158 				      BOND_TLB_REBALANCE_INTERVAL;
159 		entry->tx_bytes = 0;
160 	}
161 
162 	entry->tx_slave = NULL;
163 	entry->next = TLB_NULL_INDEX;
164 	entry->prev = TLB_NULL_INDEX;
165 }
166 
167 static inline void tlb_init_slave(struct slave *slave)
168 {
169 	SLAVE_TLB_INFO(slave).load = 0;
170 	SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
171 }
172 
173 /* Caller must hold bond lock for read */
174 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
175 {
176 	struct tlb_client_info *tx_hash_table;
177 	u32 index;
178 
179 	_lock_tx_hashtbl(bond);
180 
181 	/* clear slave from tx_hashtbl */
182 	tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
183 
184 	index = SLAVE_TLB_INFO(slave).head;
185 	while (index != TLB_NULL_INDEX) {
186 		u32 next_index = tx_hash_table[index].next;
187 		tlb_init_table_entry(&tx_hash_table[index], save_load);
188 		index = next_index;
189 	}
190 
191 	_unlock_tx_hashtbl(bond);
192 
193 	tlb_init_slave(slave);
194 }
195 
196 /* Must be called before starting the monitor timer */
197 static int tlb_initialize(struct bonding *bond)
198 {
199 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
200 	int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
201 	int i;
202 
203 	spin_lock_init(&(bond_info->tx_hashtbl_lock));
204 
205 	_lock_tx_hashtbl(bond);
206 
207 	bond_info->tx_hashtbl = kmalloc(size, GFP_KERNEL);
208 	if (!bond_info->tx_hashtbl) {
209 		printk(KERN_ERR DRV_NAME
210 		       ": Error: %s: Failed to allocate TLB hash table\n",
211 		       bond->dev->name);
212 		_unlock_tx_hashtbl(bond);
213 		return -1;
214 	}
215 
216 	memset(bond_info->tx_hashtbl, 0, size);
217 
218 	for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
219 		tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
220 	}
221 
222 	_unlock_tx_hashtbl(bond);
223 
224 	return 0;
225 }
226 
227 /* Must be called only after all slaves have been released */
228 static void tlb_deinitialize(struct bonding *bond)
229 {
230 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
231 
232 	_lock_tx_hashtbl(bond);
233 
234 	kfree(bond_info->tx_hashtbl);
235 	bond_info->tx_hashtbl = NULL;
236 
237 	_unlock_tx_hashtbl(bond);
238 }
239 
240 /* Caller must hold bond lock for read */
241 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
242 {
243 	struct slave *slave, *least_loaded;
244 	s64 max_gap;
245 	int i, found = 0;
246 
247 	/* Find the first enabled slave */
248 	bond_for_each_slave(bond, slave, i) {
249 		if (SLAVE_IS_OK(slave)) {
250 			found = 1;
251 			break;
252 		}
253 	}
254 
255 	if (!found) {
256 		return NULL;
257 	}
258 
259 	least_loaded = slave;
260 	max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
261 			(s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
262 
263 	/* Find the slave with the largest gap */
264 	bond_for_each_slave_from(bond, slave, i, least_loaded) {
265 		if (SLAVE_IS_OK(slave)) {
266 			s64 gap = (s64)(slave->speed << 20) -
267 					(s64)(SLAVE_TLB_INFO(slave).load << 3);
268 			if (max_gap < gap) {
269 				least_loaded = slave;
270 				max_gap = gap;
271 			}
272 		}
273 	}
274 
275 	return least_loaded;
276 }
277 
278 /* Caller must hold bond lock for read */
279 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
280 {
281 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
282 	struct tlb_client_info *hash_table;
283 	struct slave *assigned_slave;
284 
285 	_lock_tx_hashtbl(bond);
286 
287 	hash_table = bond_info->tx_hashtbl;
288 	assigned_slave = hash_table[hash_index].tx_slave;
289 	if (!assigned_slave) {
290 		assigned_slave = tlb_get_least_loaded_slave(bond);
291 
292 		if (assigned_slave) {
293 			struct tlb_slave_info *slave_info =
294 				&(SLAVE_TLB_INFO(assigned_slave));
295 			u32 next_index = slave_info->head;
296 
297 			hash_table[hash_index].tx_slave = assigned_slave;
298 			hash_table[hash_index].next = next_index;
299 			hash_table[hash_index].prev = TLB_NULL_INDEX;
300 
301 			if (next_index != TLB_NULL_INDEX) {
302 				hash_table[next_index].prev = hash_index;
303 			}
304 
305 			slave_info->head = hash_index;
306 			slave_info->load +=
307 				hash_table[hash_index].load_history;
308 		}
309 	}
310 
311 	if (assigned_slave) {
312 		hash_table[hash_index].tx_bytes += skb_len;
313 	}
314 
315 	_unlock_tx_hashtbl(bond);
316 
317 	return assigned_slave;
318 }
319 
320 /*********************** rlb specific functions ***************************/
321 static inline void _lock_rx_hashtbl(struct bonding *bond)
322 {
323 	spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
324 }
325 
326 static inline void _unlock_rx_hashtbl(struct bonding *bond)
327 {
328 	spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
329 }
330 
331 /* when an ARP REPLY is received from a client update its info
332  * in the rx_hashtbl
333  */
334 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
335 {
336 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
337 	struct rlb_client_info *client_info;
338 	u32 hash_index;
339 
340 	_lock_rx_hashtbl(bond);
341 
342 	hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
343 	client_info = &(bond_info->rx_hashtbl[hash_index]);
344 
345 	if ((client_info->assigned) &&
346 	    (client_info->ip_src == arp->ip_dst) &&
347 	    (client_info->ip_dst == arp->ip_src)) {
348 		/* update the clients MAC address */
349 		memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
350 		client_info->ntt = 1;
351 		bond_info->rx_ntt = 1;
352 	}
353 
354 	_unlock_rx_hashtbl(bond);
355 }
356 
357 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
358 {
359 	struct bonding *bond = bond_dev->priv;
360 	struct arp_pkt *arp = (struct arp_pkt *)skb->data;
361 	int res = NET_RX_DROP;
362 
363 	if (!(bond_dev->flags & IFF_MASTER))
364 		goto out;
365 
366 	if (!arp) {
367 		dprintk("Packet has no ARP data\n");
368 		goto out;
369 	}
370 
371 	if (skb->len < sizeof(struct arp_pkt)) {
372 		dprintk("Packet is too small to be an ARP\n");
373 		goto out;
374 	}
375 
376 	if (arp->op_code == htons(ARPOP_REPLY)) {
377 		/* update rx hash table for this ARP */
378 		rlb_update_entry_from_arp(bond, arp);
379 		dprintk("Server received an ARP Reply from client\n");
380 	}
381 
382 	res = NET_RX_SUCCESS;
383 
384 out:
385 	dev_kfree_skb(skb);
386 
387 	return res;
388 }
389 
390 /* Caller must hold bond lock for read */
391 static struct slave *rlb_next_rx_slave(struct bonding *bond)
392 {
393 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
394 	struct slave *rx_slave, *slave, *start_at;
395 	int i = 0;
396 
397 	if (bond_info->next_rx_slave) {
398 		start_at = bond_info->next_rx_slave;
399 	} else {
400 		start_at = bond->first_slave;
401 	}
402 
403 	rx_slave = NULL;
404 
405 	bond_for_each_slave_from(bond, slave, i, start_at) {
406 		if (SLAVE_IS_OK(slave)) {
407 			if (!rx_slave) {
408 				rx_slave = slave;
409 			} else if (slave->speed > rx_slave->speed) {
410 				rx_slave = slave;
411 			}
412 		}
413 	}
414 
415 	if (rx_slave) {
416 		bond_info->next_rx_slave = rx_slave->next;
417 	}
418 
419 	return rx_slave;
420 }
421 
422 /* teach the switch the mac of a disabled slave
423  * on the primary for fault tolerance
424  *
425  * Caller must hold bond->curr_slave_lock for write or bond lock for write
426  */
427 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
428 {
429 	if (!bond->curr_active_slave) {
430 		return;
431 	}
432 
433 	if (!bond->alb_info.primary_is_promisc) {
434 		bond->alb_info.primary_is_promisc = 1;
435 		dev_set_promiscuity(bond->curr_active_slave->dev, 1);
436 	}
437 
438 	bond->alb_info.rlb_promisc_timeout_counter = 0;
439 
440 	alb_send_learning_packets(bond->curr_active_slave, addr);
441 }
442 
443 /* slave being removed should not be active at this point
444  *
445  * Caller must hold bond lock for read
446  */
447 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
448 {
449 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
450 	struct rlb_client_info *rx_hash_table;
451 	u32 index, next_index;
452 
453 	/* clear slave from rx_hashtbl */
454 	_lock_rx_hashtbl(bond);
455 
456 	rx_hash_table = bond_info->rx_hashtbl;
457 	index = bond_info->rx_hashtbl_head;
458 	for (; index != RLB_NULL_INDEX; index = next_index) {
459 		next_index = rx_hash_table[index].next;
460 		if (rx_hash_table[index].slave == slave) {
461 			struct slave *assigned_slave = rlb_next_rx_slave(bond);
462 
463 			if (assigned_slave) {
464 				rx_hash_table[index].slave = assigned_slave;
465 				if (memcmp(rx_hash_table[index].mac_dst,
466 					   mac_bcast, ETH_ALEN)) {
467 					bond_info->rx_hashtbl[index].ntt = 1;
468 					bond_info->rx_ntt = 1;
469 					/* A slave has been removed from the
470 					 * table because it is either disabled
471 					 * or being released. We must retry the
472 					 * update to avoid clients from not
473 					 * being updated & disconnecting when
474 					 * there is stress
475 					 */
476 					bond_info->rlb_update_retry_counter =
477 						RLB_UPDATE_RETRY;
478 				}
479 			} else {  /* there is no active slave */
480 				rx_hash_table[index].slave = NULL;
481 			}
482 		}
483 	}
484 
485 	_unlock_rx_hashtbl(bond);
486 
487 	write_lock(&bond->curr_slave_lock);
488 
489 	if (slave != bond->curr_active_slave) {
490 		rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
491 	}
492 
493 	write_unlock(&bond->curr_slave_lock);
494 }
495 
496 static void rlb_update_client(struct rlb_client_info *client_info)
497 {
498 	int i;
499 
500 	if (!client_info->slave) {
501 		return;
502 	}
503 
504 	for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
505 		struct sk_buff *skb;
506 
507 		skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
508 				 client_info->ip_dst,
509 				 client_info->slave->dev,
510 				 client_info->ip_src,
511 				 client_info->mac_dst,
512 				 client_info->slave->dev->dev_addr,
513 				 client_info->mac_dst);
514 		if (!skb) {
515 			printk(KERN_ERR DRV_NAME
516 			       ": Error: failed to create an ARP packet\n");
517 			continue;
518 		}
519 
520 		skb->dev = client_info->slave->dev;
521 
522 		if (client_info->tag) {
523 			skb = vlan_put_tag(skb, client_info->vlan_id);
524 			if (!skb) {
525 				printk(KERN_ERR DRV_NAME
526 				       ": Error: failed to insert VLAN tag\n");
527 				continue;
528 			}
529 		}
530 
531 		arp_xmit(skb);
532 	}
533 }
534 
535 /* sends ARP REPLIES that update the clients that need updating */
536 static void rlb_update_rx_clients(struct bonding *bond)
537 {
538 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
539 	struct rlb_client_info *client_info;
540 	u32 hash_index;
541 
542 	_lock_rx_hashtbl(bond);
543 
544 	hash_index = bond_info->rx_hashtbl_head;
545 	for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
546 		client_info = &(bond_info->rx_hashtbl[hash_index]);
547 		if (client_info->ntt) {
548 			rlb_update_client(client_info);
549 			if (bond_info->rlb_update_retry_counter == 0) {
550 				client_info->ntt = 0;
551 			}
552 		}
553 	}
554 
555 	/* do not update the entries again untill this counter is zero so that
556 	 * not to confuse the clients.
557 	 */
558 	bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
559 
560 	_unlock_rx_hashtbl(bond);
561 }
562 
563 /* The slave was assigned a new mac address - update the clients */
564 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
565 {
566 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
567 	struct rlb_client_info *client_info;
568 	int ntt = 0;
569 	u32 hash_index;
570 
571 	_lock_rx_hashtbl(bond);
572 
573 	hash_index = bond_info->rx_hashtbl_head;
574 	for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
575 		client_info = &(bond_info->rx_hashtbl[hash_index]);
576 
577 		if ((client_info->slave == slave) &&
578 		    memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
579 			client_info->ntt = 1;
580 			ntt = 1;
581 		}
582 	}
583 
584 	// update the team's flag only after the whole iteration
585 	if (ntt) {
586 		bond_info->rx_ntt = 1;
587 		//fasten the change
588 		bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
589 	}
590 
591 	_unlock_rx_hashtbl(bond);
592 }
593 
594 /* mark all clients using src_ip to be updated */
595 static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
596 {
597 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
598 	struct rlb_client_info *client_info;
599 	u32 hash_index;
600 
601 	_lock_rx_hashtbl(bond);
602 
603 	hash_index = bond_info->rx_hashtbl_head;
604 	for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
605 		client_info = &(bond_info->rx_hashtbl[hash_index]);
606 
607 		if (!client_info->slave) {
608 			printk(KERN_ERR DRV_NAME
609 			       ": Error: found a client with no channel in "
610 			       "the client's hash table\n");
611 			continue;
612 		}
613 		/*update all clients using this src_ip, that are not assigned
614 		 * to the team's address (curr_active_slave) and have a known
615 		 * unicast mac address.
616 		 */
617 		if ((client_info->ip_src == src_ip) &&
618 		    memcmp(client_info->slave->dev->dev_addr,
619 			   bond->dev->dev_addr, ETH_ALEN) &&
620 		    memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
621 			client_info->ntt = 1;
622 			bond_info->rx_ntt = 1;
623 		}
624 	}
625 
626 	_unlock_rx_hashtbl(bond);
627 }
628 
629 /* Caller must hold both bond and ptr locks for read */
630 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
631 {
632 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
633 	struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
634 	struct slave *assigned_slave;
635 	struct rlb_client_info *client_info;
636 	u32 hash_index = 0;
637 
638 	_lock_rx_hashtbl(bond);
639 
640 	hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
641 	client_info = &(bond_info->rx_hashtbl[hash_index]);
642 
643 	if (client_info->assigned) {
644 		if ((client_info->ip_src == arp->ip_src) &&
645 		    (client_info->ip_dst == arp->ip_dst)) {
646 			/* the entry is already assigned to this client */
647 			if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
648 				/* update mac address from arp */
649 				memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
650 			}
651 
652 			assigned_slave = client_info->slave;
653 			if (assigned_slave) {
654 				_unlock_rx_hashtbl(bond);
655 				return assigned_slave;
656 			}
657 		} else {
658 			/* the entry is already assigned to some other client,
659 			 * move the old client to primary (curr_active_slave) so
660 			 * that the new client can be assigned to this entry.
661 			 */
662 			if (bond->curr_active_slave &&
663 			    client_info->slave != bond->curr_active_slave) {
664 				client_info->slave = bond->curr_active_slave;
665 				rlb_update_client(client_info);
666 			}
667 		}
668 	}
669 	/* assign a new slave */
670 	assigned_slave = rlb_next_rx_slave(bond);
671 
672 	if (assigned_slave) {
673 		client_info->ip_src = arp->ip_src;
674 		client_info->ip_dst = arp->ip_dst;
675 		/* arp->mac_dst is broadcast for arp reqeusts.
676 		 * will be updated with clients actual unicast mac address
677 		 * upon receiving an arp reply.
678 		 */
679 		memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
680 		client_info->slave = assigned_slave;
681 
682 		if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
683 			client_info->ntt = 1;
684 			bond->alb_info.rx_ntt = 1;
685 		} else {
686 			client_info->ntt = 0;
687 		}
688 
689 		if (!list_empty(&bond->vlan_list)) {
690 			unsigned short vlan_id;
691 			int res = vlan_get_tag(skb, &vlan_id);
692 			if (!res) {
693 				client_info->tag = 1;
694 				client_info->vlan_id = vlan_id;
695 			}
696 		}
697 
698 		if (!client_info->assigned) {
699 			u32 prev_tbl_head = bond_info->rx_hashtbl_head;
700 			bond_info->rx_hashtbl_head = hash_index;
701 			client_info->next = prev_tbl_head;
702 			if (prev_tbl_head != RLB_NULL_INDEX) {
703 				bond_info->rx_hashtbl[prev_tbl_head].prev =
704 					hash_index;
705 			}
706 			client_info->assigned = 1;
707 		}
708 	}
709 
710 	_unlock_rx_hashtbl(bond);
711 
712 	return assigned_slave;
713 }
714 
715 /* chooses (and returns) transmit channel for arp reply
716  * does not choose channel for other arp types since they are
717  * sent on the curr_active_slave
718  */
719 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
720 {
721 	struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
722 	struct slave *tx_slave = NULL;
723 
724 	if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
725 		/* the arp must be sent on the selected
726 		* rx channel
727 		*/
728 		tx_slave = rlb_choose_channel(skb, bond);
729 		if (tx_slave) {
730 			memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
731 		}
732 		dprintk("Server sent ARP Reply packet\n");
733 	} else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
734 		/* Create an entry in the rx_hashtbl for this client as a
735 		 * place holder.
736 		 * When the arp reply is received the entry will be updated
737 		 * with the correct unicast address of the client.
738 		 */
739 		rlb_choose_channel(skb, bond);
740 
741 		/* The ARP relpy packets must be delayed so that
742 		 * they can cancel out the influence of the ARP request.
743 		 */
744 		bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
745 
746 		/* arp requests are broadcast and are sent on the primary
747 		 * the arp request will collapse all clients on the subnet to
748 		 * the primary slave. We must register these clients to be
749 		 * updated with their assigned mac.
750 		 */
751 		rlb_req_update_subnet_clients(bond, arp->ip_src);
752 		dprintk("Server sent ARP Request packet\n");
753 	}
754 
755 	return tx_slave;
756 }
757 
758 /* Caller must hold bond lock for read */
759 static void rlb_rebalance(struct bonding *bond)
760 {
761 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
762 	struct slave *assigned_slave;
763 	struct rlb_client_info *client_info;
764 	int ntt;
765 	u32 hash_index;
766 
767 	_lock_rx_hashtbl(bond);
768 
769 	ntt = 0;
770 	hash_index = bond_info->rx_hashtbl_head;
771 	for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
772 		client_info = &(bond_info->rx_hashtbl[hash_index]);
773 		assigned_slave = rlb_next_rx_slave(bond);
774 		if (assigned_slave && (client_info->slave != assigned_slave)) {
775 			client_info->slave = assigned_slave;
776 			client_info->ntt = 1;
777 			ntt = 1;
778 		}
779 	}
780 
781 	/* update the team's flag only after the whole iteration */
782 	if (ntt) {
783 		bond_info->rx_ntt = 1;
784 	}
785 	_unlock_rx_hashtbl(bond);
786 }
787 
788 /* Caller must hold rx_hashtbl lock */
789 static void rlb_init_table_entry(struct rlb_client_info *entry)
790 {
791 	memset(entry, 0, sizeof(struct rlb_client_info));
792 	entry->next = RLB_NULL_INDEX;
793 	entry->prev = RLB_NULL_INDEX;
794 }
795 
796 static int rlb_initialize(struct bonding *bond)
797 {
798 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
799 	struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
800 	int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
801 	int i;
802 
803 	spin_lock_init(&(bond_info->rx_hashtbl_lock));
804 
805 	_lock_rx_hashtbl(bond);
806 
807 	bond_info->rx_hashtbl = kmalloc(size, GFP_KERNEL);
808 	if (!bond_info->rx_hashtbl) {
809 		printk(KERN_ERR DRV_NAME
810 		       ": Error: %s: Failed to allocate RLB hash table\n",
811 		       bond->dev->name);
812 		_unlock_rx_hashtbl(bond);
813 		return -1;
814 	}
815 
816 	bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
817 
818 	for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
819 		rlb_init_table_entry(bond_info->rx_hashtbl + i);
820 	}
821 
822 	_unlock_rx_hashtbl(bond);
823 
824 	/*initialize packet type*/
825 	pk_type->type = __constant_htons(ETH_P_ARP);
826 	pk_type->dev = bond->dev;
827 	pk_type->func = rlb_arp_recv;
828 
829 	/* register to receive ARPs */
830 	dev_add_pack(pk_type);
831 
832 	return 0;
833 }
834 
835 static void rlb_deinitialize(struct bonding *bond)
836 {
837 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
838 
839 	dev_remove_pack(&(bond_info->rlb_pkt_type));
840 
841 	_lock_rx_hashtbl(bond);
842 
843 	kfree(bond_info->rx_hashtbl);
844 	bond_info->rx_hashtbl = NULL;
845 	bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
846 
847 	_unlock_rx_hashtbl(bond);
848 }
849 
850 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
851 {
852 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
853 	u32 curr_index;
854 
855 	_lock_rx_hashtbl(bond);
856 
857 	curr_index = bond_info->rx_hashtbl_head;
858 	while (curr_index != RLB_NULL_INDEX) {
859 		struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
860 		u32 next_index = bond_info->rx_hashtbl[curr_index].next;
861 		u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
862 
863 		if (curr->tag && (curr->vlan_id == vlan_id)) {
864 			if (curr_index == bond_info->rx_hashtbl_head) {
865 				bond_info->rx_hashtbl_head = next_index;
866 			}
867 			if (prev_index != RLB_NULL_INDEX) {
868 				bond_info->rx_hashtbl[prev_index].next = next_index;
869 			}
870 			if (next_index != RLB_NULL_INDEX) {
871 				bond_info->rx_hashtbl[next_index].prev = prev_index;
872 			}
873 
874 			rlb_init_table_entry(curr);
875 		}
876 
877 		curr_index = next_index;
878 	}
879 
880 	_unlock_rx_hashtbl(bond);
881 }
882 
883 /*********************** tlb/rlb shared functions *********************/
884 
885 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
886 {
887 	struct bonding *bond = bond_get_bond_by_slave(slave);
888 	struct learning_pkt pkt;
889 	int size = sizeof(struct learning_pkt);
890 	int i;
891 
892 	memset(&pkt, 0, size);
893 	memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
894 	memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
895 	pkt.type = __constant_htons(ETH_P_LOOP);
896 
897 	for (i = 0; i < MAX_LP_BURST; i++) {
898 		struct sk_buff *skb;
899 		char *data;
900 
901 		skb = dev_alloc_skb(size);
902 		if (!skb) {
903 			return;
904 		}
905 
906 		data = skb_put(skb, size);
907 		memcpy(data, &pkt, size);
908 
909 		skb->mac.raw = data;
910 		skb->nh.raw = data + ETH_HLEN;
911 		skb->protocol = pkt.type;
912 		skb->priority = TC_PRIO_CONTROL;
913 		skb->dev = slave->dev;
914 
915 		if (!list_empty(&bond->vlan_list)) {
916 			struct vlan_entry *vlan;
917 
918 			vlan = bond_next_vlan(bond,
919 					      bond->alb_info.current_alb_vlan);
920 
921 			bond->alb_info.current_alb_vlan = vlan;
922 			if (!vlan) {
923 				kfree_skb(skb);
924 				continue;
925 			}
926 
927 			skb = vlan_put_tag(skb, vlan->vlan_id);
928 			if (!skb) {
929 				printk(KERN_ERR DRV_NAME
930 				       ": Error: failed to insert VLAN tag\n");
931 				continue;
932 			}
933 		}
934 
935 		dev_queue_xmit(skb);
936 	}
937 }
938 
939 /* hw is a boolean parameter that determines whether we should try and
940  * set the hw address of the device as well as the hw address of the
941  * net_device
942  */
943 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
944 {
945 	struct net_device *dev = slave->dev;
946 	struct sockaddr s_addr;
947 
948 	if (!hw) {
949 		memcpy(dev->dev_addr, addr, dev->addr_len);
950 		return 0;
951 	}
952 
953 	/* for rlb each slave must have a unique hw mac addresses so that */
954 	/* each slave will receive packets destined to a different mac */
955 	memcpy(s_addr.sa_data, addr, dev->addr_len);
956 	s_addr.sa_family = dev->type;
957 	if (dev_set_mac_address(dev, &s_addr)) {
958 		printk(KERN_ERR DRV_NAME
959 		       ": Error: dev_set_mac_address of dev %s failed! ALB "
960 		       "mode requires that the base driver support setting "
961 		       "the hw address also when the network device's "
962 		       "interface is open\n",
963 		       dev->name);
964 		return -EOPNOTSUPP;
965 	}
966 	return 0;
967 }
968 
969 /* Caller must hold bond lock for write or curr_slave_lock for write*/
970 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
971 {
972 	struct slave *disabled_slave = NULL;
973 	u8 tmp_mac_addr[ETH_ALEN];
974 	int slaves_state_differ;
975 
976 	slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
977 
978 	memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
979 	alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
980 	alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
981 
982 	/* fasten the change in the switch */
983 	if (SLAVE_IS_OK(slave1)) {
984 		alb_send_learning_packets(slave1, slave1->dev->dev_addr);
985 		if (bond->alb_info.rlb_enabled) {
986 			/* inform the clients that the mac address
987 			 * has changed
988 			 */
989 			rlb_req_update_slave_clients(bond, slave1);
990 		}
991 	} else {
992 		disabled_slave = slave1;
993 	}
994 
995 	if (SLAVE_IS_OK(slave2)) {
996 		alb_send_learning_packets(slave2, slave2->dev->dev_addr);
997 		if (bond->alb_info.rlb_enabled) {
998 			/* inform the clients that the mac address
999 			 * has changed
1000 			 */
1001 			rlb_req_update_slave_clients(bond, slave2);
1002 		}
1003 	} else {
1004 		disabled_slave = slave2;
1005 	}
1006 
1007 	if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1008 		/* A disabled slave was assigned an active mac addr */
1009 		rlb_teach_disabled_mac_on_primary(bond,
1010 						  disabled_slave->dev->dev_addr);
1011 	}
1012 }
1013 
1014 /**
1015  * alb_change_hw_addr_on_detach
1016  * @bond: bonding we're working on
1017  * @slave: the slave that was just detached
1018  *
1019  * We assume that @slave was already detached from the slave list.
1020  *
1021  * If @slave's permanent hw address is different both from its current
1022  * address and from @bond's address, then somewhere in the bond there's
1023  * a slave that has @slave's permanet address as its current address.
1024  * We'll make sure that that slave no longer uses @slave's permanent address.
1025  *
1026  * Caller must hold bond lock
1027  */
1028 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1029 {
1030 	int perm_curr_diff;
1031 	int perm_bond_diff;
1032 
1033 	perm_curr_diff = memcmp(slave->perm_hwaddr,
1034 				slave->dev->dev_addr,
1035 				ETH_ALEN);
1036 	perm_bond_diff = memcmp(slave->perm_hwaddr,
1037 				bond->dev->dev_addr,
1038 				ETH_ALEN);
1039 
1040 	if (perm_curr_diff && perm_bond_diff) {
1041 		struct slave *tmp_slave;
1042 		int i, found = 0;
1043 
1044 		bond_for_each_slave(bond, tmp_slave, i) {
1045 			if (!memcmp(slave->perm_hwaddr,
1046 				    tmp_slave->dev->dev_addr,
1047 				    ETH_ALEN)) {
1048 				found = 1;
1049 				break;
1050 			}
1051 		}
1052 
1053 		if (found) {
1054 			alb_swap_mac_addr(bond, slave, tmp_slave);
1055 		}
1056 	}
1057 }
1058 
1059 /**
1060  * alb_handle_addr_collision_on_attach
1061  * @bond: bonding we're working on
1062  * @slave: the slave that was just attached
1063  *
1064  * checks uniqueness of slave's mac address and handles the case the
1065  * new slave uses the bonds mac address.
1066  *
1067  * If the permanent hw address of @slave is @bond's hw address, we need to
1068  * find a different hw address to give @slave, that isn't in use by any other
1069  * slave in the bond. This address must be, of course, one of the premanent
1070  * addresses of the other slaves.
1071  *
1072  * We go over the slave list, and for each slave there we compare its
1073  * permanent hw address with the current address of all the other slaves.
1074  * If no match was found, then we've found a slave with a permanent address
1075  * that isn't used by any other slave in the bond, so we can assign it to
1076  * @slave.
1077  *
1078  * assumption: this function is called before @slave is attached to the
1079  * 	       bond slave list.
1080  *
1081  * caller must hold the bond lock for write since the mac addresses are compared
1082  * and may be swapped.
1083  */
1084 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1085 {
1086 	struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1087 	struct slave *has_bond_addr = bond->curr_active_slave;
1088 	int i, j, found = 0;
1089 
1090 	if (bond->slave_cnt == 0) {
1091 		/* this is the first slave */
1092 		return 0;
1093 	}
1094 
1095 	/* if slave's mac address differs from bond's mac address
1096 	 * check uniqueness of slave's mac address against the other
1097 	 * slaves in the bond.
1098 	 */
1099 	if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1100 		bond_for_each_slave(bond, tmp_slave1, i) {
1101 			if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1102 				    ETH_ALEN)) {
1103 				found = 1;
1104 				break;
1105 			}
1106 		}
1107 
1108 		if (!found)
1109 			return 0;
1110 
1111 		/* Try setting slave mac to bond address and fall-through
1112 		   to code handling that situation below... */
1113 		alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1114 				       bond->alb_info.rlb_enabled);
1115 	}
1116 
1117 	/* The slave's address is equal to the address of the bond.
1118 	 * Search for a spare address in the bond for this slave.
1119 	 */
1120 	free_mac_slave = NULL;
1121 
1122 	bond_for_each_slave(bond, tmp_slave1, i) {
1123 		found = 0;
1124 		bond_for_each_slave(bond, tmp_slave2, j) {
1125 			if (!memcmp(tmp_slave1->perm_hwaddr,
1126 				    tmp_slave2->dev->dev_addr,
1127 				    ETH_ALEN)) {
1128 				found = 1;
1129 				break;
1130 			}
1131 		}
1132 
1133 		if (!found) {
1134 			/* no slave has tmp_slave1's perm addr
1135 			 * as its curr addr
1136 			 */
1137 			free_mac_slave = tmp_slave1;
1138 			break;
1139 		}
1140 
1141 		if (!has_bond_addr) {
1142 			if (!memcmp(tmp_slave1->dev->dev_addr,
1143 				    bond->dev->dev_addr,
1144 				    ETH_ALEN)) {
1145 
1146 				has_bond_addr = tmp_slave1;
1147 			}
1148 		}
1149 	}
1150 
1151 	if (free_mac_slave) {
1152 		alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1153 				       bond->alb_info.rlb_enabled);
1154 
1155 		printk(KERN_WARNING DRV_NAME
1156 		       ": Warning: the hw address of slave %s is in use by "
1157 		       "the bond; giving it the hw address of %s\n",
1158 		       slave->dev->name, free_mac_slave->dev->name);
1159 
1160 	} else if (has_bond_addr) {
1161 		printk(KERN_ERR DRV_NAME
1162 		       ": Error: the hw address of slave %s is in use by the "
1163 		       "bond; couldn't find a slave with a free hw address to "
1164 		       "give it (this should not have happened)\n",
1165 		       slave->dev->name);
1166 		return -EFAULT;
1167 	}
1168 
1169 	return 0;
1170 }
1171 
1172 /**
1173  * alb_set_mac_address
1174  * @bond:
1175  * @addr:
1176  *
1177  * In TLB mode all slaves are configured to the bond's hw address, but set
1178  * their dev_addr field to different addresses (based on their permanent hw
1179  * addresses).
1180  *
1181  * For each slave, this function sets the interface to the new address and then
1182  * changes its dev_addr field to its previous value.
1183  *
1184  * Unwinding assumes bond's mac address has not yet changed.
1185  */
1186 static int alb_set_mac_address(struct bonding *bond, void *addr)
1187 {
1188 	struct sockaddr sa;
1189 	struct slave *slave, *stop_at;
1190 	char tmp_addr[ETH_ALEN];
1191 	int res;
1192 	int i;
1193 
1194 	if (bond->alb_info.rlb_enabled) {
1195 		return 0;
1196 	}
1197 
1198 	bond_for_each_slave(bond, slave, i) {
1199 		if (slave->dev->set_mac_address == NULL) {
1200 			res = -EOPNOTSUPP;
1201 			goto unwind;
1202 		}
1203 
1204 		/* save net_device's current hw address */
1205 		memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1206 
1207 		res = dev_set_mac_address(slave->dev, addr);
1208 
1209 		/* restore net_device's hw address */
1210 		memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1211 
1212 		if (res) {
1213 			goto unwind;
1214 		}
1215 	}
1216 
1217 	return 0;
1218 
1219 unwind:
1220 	memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1221 	sa.sa_family = bond->dev->type;
1222 
1223 	/* unwind from head to the slave that failed */
1224 	stop_at = slave;
1225 	bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1226 		memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1227 		dev_set_mac_address(slave->dev, &sa);
1228 		memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1229 	}
1230 
1231 	return res;
1232 }
1233 
1234 /************************ exported alb funcions ************************/
1235 
1236 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1237 {
1238 	int res;
1239 
1240 	res = tlb_initialize(bond);
1241 	if (res) {
1242 		return res;
1243 	}
1244 
1245 	if (rlb_enabled) {
1246 		bond->alb_info.rlb_enabled = 1;
1247 		/* initialize rlb */
1248 		res = rlb_initialize(bond);
1249 		if (res) {
1250 			tlb_deinitialize(bond);
1251 			return res;
1252 		}
1253 	}
1254 
1255 	return 0;
1256 }
1257 
1258 void bond_alb_deinitialize(struct bonding *bond)
1259 {
1260 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1261 
1262 	tlb_deinitialize(bond);
1263 
1264 	if (bond_info->rlb_enabled) {
1265 		rlb_deinitialize(bond);
1266 	}
1267 }
1268 
1269 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1270 {
1271 	struct bonding *bond = bond_dev->priv;
1272 	struct ethhdr *eth_data;
1273 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1274 	struct slave *tx_slave = NULL;
1275 	static u32 ip_bcast = 0xffffffff;
1276 	int hash_size = 0;
1277 	int do_tx_balance = 1;
1278 	u32 hash_index = 0;
1279 	u8 *hash_start = NULL;
1280 	int res = 1;
1281 
1282 	skb->mac.raw = (unsigned char *)skb->data;
1283 	eth_data = eth_hdr(skb);
1284 
1285 	/* make sure that the curr_active_slave and the slaves list do
1286 	 * not change during tx
1287 	 */
1288 	read_lock(&bond->lock);
1289 	read_lock(&bond->curr_slave_lock);
1290 
1291 	if (!BOND_IS_OK(bond)) {
1292 		goto out;
1293 	}
1294 
1295 	switch (ntohs(skb->protocol)) {
1296 	case ETH_P_IP:
1297 		if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1298 		    (skb->nh.iph->daddr == ip_bcast) ||
1299 		    (skb->nh.iph->protocol == IPPROTO_IGMP)) {
1300 			do_tx_balance = 0;
1301 			break;
1302 		}
1303 		hash_start = (char*)&(skb->nh.iph->daddr);
1304 		hash_size = sizeof(skb->nh.iph->daddr);
1305 		break;
1306 	case ETH_P_IPV6:
1307 		if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1308 			do_tx_balance = 0;
1309 			break;
1310 		}
1311 
1312 		hash_start = (char*)&(skb->nh.ipv6h->daddr);
1313 		hash_size = sizeof(skb->nh.ipv6h->daddr);
1314 		break;
1315 	case ETH_P_IPX:
1316 		if (ipx_hdr(skb)->ipx_checksum !=
1317 		    __constant_htons(IPX_NO_CHECKSUM)) {
1318 			/* something is wrong with this packet */
1319 			do_tx_balance = 0;
1320 			break;
1321 		}
1322 
1323 		if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1324 			/* The only protocol worth balancing in
1325 			 * this family since it has an "ARP" like
1326 			 * mechanism
1327 			 */
1328 			do_tx_balance = 0;
1329 			break;
1330 		}
1331 
1332 		hash_start = (char*)eth_data->h_dest;
1333 		hash_size = ETH_ALEN;
1334 		break;
1335 	case ETH_P_ARP:
1336 		do_tx_balance = 0;
1337 		if (bond_info->rlb_enabled) {
1338 			tx_slave = rlb_arp_xmit(skb, bond);
1339 		}
1340 		break;
1341 	default:
1342 		do_tx_balance = 0;
1343 		break;
1344 	}
1345 
1346 	if (do_tx_balance) {
1347 		hash_index = _simple_hash(hash_start, hash_size);
1348 		tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1349 	}
1350 
1351 	if (!tx_slave) {
1352 		/* unbalanced or unassigned, send through primary */
1353 		tx_slave = bond->curr_active_slave;
1354 		bond_info->unbalanced_load += skb->len;
1355 	}
1356 
1357 	if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1358 		if (tx_slave != bond->curr_active_slave) {
1359 			memcpy(eth_data->h_source,
1360 			       tx_slave->dev->dev_addr,
1361 			       ETH_ALEN);
1362 		}
1363 
1364 		res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1365 	} else {
1366 		if (tx_slave) {
1367 			tlb_clear_slave(bond, tx_slave, 0);
1368 		}
1369 	}
1370 
1371 out:
1372 	if (res) {
1373 		/* no suitable interface, frame not sent */
1374 		dev_kfree_skb(skb);
1375 	}
1376 	read_unlock(&bond->curr_slave_lock);
1377 	read_unlock(&bond->lock);
1378 	return 0;
1379 }
1380 
1381 void bond_alb_monitor(struct bonding *bond)
1382 {
1383 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1384 	struct slave *slave;
1385 	int i;
1386 
1387 	read_lock(&bond->lock);
1388 
1389 	if (bond->kill_timers) {
1390 		goto out;
1391 	}
1392 
1393 	if (bond->slave_cnt == 0) {
1394 		bond_info->tx_rebalance_counter = 0;
1395 		bond_info->lp_counter = 0;
1396 		goto re_arm;
1397 	}
1398 
1399 	bond_info->tx_rebalance_counter++;
1400 	bond_info->lp_counter++;
1401 
1402 	/* send learning packets */
1403 	if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1404 		/* change of curr_active_slave involves swapping of mac addresses.
1405 		 * in order to avoid this swapping from happening while
1406 		 * sending the learning packets, the curr_slave_lock must be held for
1407 		 * read.
1408 		 */
1409 		read_lock(&bond->curr_slave_lock);
1410 
1411 		bond_for_each_slave(bond, slave, i) {
1412 			alb_send_learning_packets(slave,slave->dev->dev_addr);
1413 		}
1414 
1415 		read_unlock(&bond->curr_slave_lock);
1416 
1417 		bond_info->lp_counter = 0;
1418 	}
1419 
1420 	/* rebalance tx traffic */
1421 	if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1422 
1423 		read_lock(&bond->curr_slave_lock);
1424 
1425 		bond_for_each_slave(bond, slave, i) {
1426 			tlb_clear_slave(bond, slave, 1);
1427 			if (slave == bond->curr_active_slave) {
1428 				SLAVE_TLB_INFO(slave).load =
1429 					bond_info->unbalanced_load /
1430 						BOND_TLB_REBALANCE_INTERVAL;
1431 				bond_info->unbalanced_load = 0;
1432 			}
1433 		}
1434 
1435 		read_unlock(&bond->curr_slave_lock);
1436 
1437 		bond_info->tx_rebalance_counter = 0;
1438 	}
1439 
1440 	/* handle rlb stuff */
1441 	if (bond_info->rlb_enabled) {
1442 		/* the following code changes the promiscuity of the
1443 		 * the curr_active_slave. It needs to be locked with a
1444 		 * write lock to protect from other code that also
1445 		 * sets the promiscuity.
1446 		 */
1447 		write_lock(&bond->curr_slave_lock);
1448 
1449 		if (bond_info->primary_is_promisc &&
1450 		    (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1451 
1452 			bond_info->rlb_promisc_timeout_counter = 0;
1453 
1454 			/* If the primary was set to promiscuous mode
1455 			 * because a slave was disabled then
1456 			 * it can now leave promiscuous mode.
1457 			 */
1458 			dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1459 			bond_info->primary_is_promisc = 0;
1460 		}
1461 
1462 		write_unlock(&bond->curr_slave_lock);
1463 
1464 		if (bond_info->rlb_rebalance) {
1465 			bond_info->rlb_rebalance = 0;
1466 			rlb_rebalance(bond);
1467 		}
1468 
1469 		/* check if clients need updating */
1470 		if (bond_info->rx_ntt) {
1471 			if (bond_info->rlb_update_delay_counter) {
1472 				--bond_info->rlb_update_delay_counter;
1473 			} else {
1474 				rlb_update_rx_clients(bond);
1475 				if (bond_info->rlb_update_retry_counter) {
1476 					--bond_info->rlb_update_retry_counter;
1477 				} else {
1478 					bond_info->rx_ntt = 0;
1479 				}
1480 			}
1481 		}
1482 	}
1483 
1484 re_arm:
1485 	mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1486 out:
1487 	read_unlock(&bond->lock);
1488 }
1489 
1490 /* assumption: called before the slave is attached to the bond
1491  * and not locked by the bond lock
1492  */
1493 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1494 {
1495 	int res;
1496 
1497 	res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1498 				     bond->alb_info.rlb_enabled);
1499 	if (res) {
1500 		return res;
1501 	}
1502 
1503 	/* caller must hold the bond lock for write since the mac addresses
1504 	 * are compared and may be swapped.
1505 	 */
1506 	write_lock_bh(&bond->lock);
1507 
1508 	res = alb_handle_addr_collision_on_attach(bond, slave);
1509 
1510 	write_unlock_bh(&bond->lock);
1511 
1512 	if (res) {
1513 		return res;
1514 	}
1515 
1516 	tlb_init_slave(slave);
1517 
1518 	/* order a rebalance ASAP */
1519 	bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1520 
1521 	if (bond->alb_info.rlb_enabled) {
1522 		bond->alb_info.rlb_rebalance = 1;
1523 	}
1524 
1525 	return 0;
1526 }
1527 
1528 /* Caller must hold bond lock for write */
1529 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1530 {
1531 	if (bond->slave_cnt > 1) {
1532 		alb_change_hw_addr_on_detach(bond, slave);
1533 	}
1534 
1535 	tlb_clear_slave(bond, slave, 0);
1536 
1537 	if (bond->alb_info.rlb_enabled) {
1538 		bond->alb_info.next_rx_slave = NULL;
1539 		rlb_clear_slave(bond, slave);
1540 	}
1541 }
1542 
1543 /* Caller must hold bond lock for read */
1544 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1545 {
1546 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1547 
1548 	if (link == BOND_LINK_DOWN) {
1549 		tlb_clear_slave(bond, slave, 0);
1550 		if (bond->alb_info.rlb_enabled) {
1551 			rlb_clear_slave(bond, slave);
1552 		}
1553 	} else if (link == BOND_LINK_UP) {
1554 		/* order a rebalance ASAP */
1555 		bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1556 		if (bond->alb_info.rlb_enabled) {
1557 			bond->alb_info.rlb_rebalance = 1;
1558 			/* If the updelay module parameter is smaller than the
1559 			 * forwarding delay of the switch the rebalance will
1560 			 * not work because the rebalance arp replies will
1561 			 * not be forwarded to the clients..
1562 			 */
1563 		}
1564 	}
1565 }
1566 
1567 /**
1568  * bond_alb_handle_active_change - assign new curr_active_slave
1569  * @bond: our bonding struct
1570  * @new_slave: new slave to assign
1571  *
1572  * Set the bond->curr_active_slave to @new_slave and handle
1573  * mac address swapping and promiscuity changes as needed.
1574  *
1575  * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1576  */
1577 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1578 {
1579 	struct slave *swap_slave;
1580 	int i;
1581 
1582 	if (bond->curr_active_slave == new_slave) {
1583 		return;
1584 	}
1585 
1586 	if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1587 		dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1588 		bond->alb_info.primary_is_promisc = 0;
1589 		bond->alb_info.rlb_promisc_timeout_counter = 0;
1590 	}
1591 
1592 	swap_slave = bond->curr_active_slave;
1593 	bond->curr_active_slave = new_slave;
1594 
1595 	if (!new_slave || (bond->slave_cnt == 0)) {
1596 		return;
1597 	}
1598 
1599 	/* set the new curr_active_slave to the bonds mac address
1600 	 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1601 	 */
1602 	if (!swap_slave) {
1603 		struct slave *tmp_slave;
1604 		/* find slave that is holding the bond's mac address */
1605 		bond_for_each_slave(bond, tmp_slave, i) {
1606 			if (!memcmp(tmp_slave->dev->dev_addr,
1607 				    bond->dev->dev_addr, ETH_ALEN)) {
1608 				swap_slave = tmp_slave;
1609 				break;
1610 			}
1611 		}
1612 	}
1613 
1614 	/* curr_active_slave must be set before calling alb_swap_mac_addr */
1615 	if (swap_slave) {
1616 		/* swap mac address */
1617 		alb_swap_mac_addr(bond, swap_slave, new_slave);
1618 	} else {
1619 		/* set the new_slave to the bond mac address */
1620 		alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1621 				       bond->alb_info.rlb_enabled);
1622 		/* fasten bond mac on new current slave */
1623 		alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1624 	}
1625 }
1626 
1627 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1628 {
1629 	struct bonding *bond = bond_dev->priv;
1630 	struct sockaddr *sa = addr;
1631 	struct slave *slave, *swap_slave;
1632 	int res;
1633 	int i;
1634 
1635 	if (!is_valid_ether_addr(sa->sa_data)) {
1636 		return -EADDRNOTAVAIL;
1637 	}
1638 
1639 	res = alb_set_mac_address(bond, addr);
1640 	if (res) {
1641 		return res;
1642 	}
1643 
1644 	memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1645 
1646 	/* If there is no curr_active_slave there is nothing else to do.
1647 	 * Otherwise we'll need to pass the new address to it and handle
1648 	 * duplications.
1649 	 */
1650 	if (!bond->curr_active_slave) {
1651 		return 0;
1652 	}
1653 
1654 	swap_slave = NULL;
1655 
1656 	bond_for_each_slave(bond, slave, i) {
1657 		if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1658 			swap_slave = slave;
1659 			break;
1660 		}
1661 	}
1662 
1663 	if (swap_slave) {
1664 		alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1665 	} else {
1666 		alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1667 				       bond->alb_info.rlb_enabled);
1668 
1669 		alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1670 		if (bond->alb_info.rlb_enabled) {
1671 			/* inform clients mac address has changed */
1672 			rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1673 		}
1674 	}
1675 
1676 	return 0;
1677 }
1678 
1679 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1680 {
1681 	if (bond->alb_info.current_alb_vlan &&
1682 	    (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1683 		bond->alb_info.current_alb_vlan = NULL;
1684 	}
1685 
1686 	if (bond->alb_info.rlb_enabled) {
1687 		rlb_clear_vlan(bond, vlan_id);
1688 	}
1689 }
1690 
1691