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