2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
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.
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
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.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 //#define BONDING_DEBUG 1
25 #include <linux/skbuff.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/spinlock.h>
30 #include <linux/slab.h>
31 #include <linux/timer.h>
33 #include <linux/ipv6.h>
34 #include <linux/if_arp.h>
35 #include <linux/if_ether.h>
36 #include <linux/if_bonding.h>
37 #include <linux/if_vlan.h>
41 #include <asm/byteorder.h>
46 #define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
47 #define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
48 * Used for division - never set
51 #define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
52 * learning packets to the switch
55 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
56 * ALB_TIMER_TICKS_PER_SEC)
58 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
59 * ALB_TIMER_TICKS_PER_SEC)
61 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
62 * Note that this value MUST NOT be smaller
63 * because the key hash table is BYTE wide !
67 #define TLB_NULL_INDEX 0xffffffff
68 #define MAX_LP_BURST 3
71 #define RLB_HASH_TABLE_SIZE 256
72 #define RLB_NULL_INDEX 0xffffffff
73 #define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
74 #define RLB_ARP_BURST_SIZE 2
75 #define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
76 * rebalance interval (5 min).
78 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
79 * promiscuous after failover
81 #define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
83 static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
84 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
91 u8 padding[ETH_ZLEN - ETH_HLEN];
100 u8 mac_src[ETH_ALEN]; /* sender hardware address */
101 u32 ip_src; /* sender IP address */
102 u8 mac_dst[ETH_ALEN]; /* target hardware address */
103 u32 ip_dst; /* target IP address */
107 /* Forward declaration */
108 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
110 static inline u8 _simple_hash(u8 *hash_start, int hash_size)
115 for (i = 0; i < hash_size; i++) {
116 hash ^= hash_start[i];
122 /*********************** tlb specific functions ***************************/
124 static inline void _lock_tx_hashtbl(struct bonding *bond)
126 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
129 static inline void _unlock_tx_hashtbl(struct bonding *bond)
131 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
134 /* Caller must hold tx_hashtbl lock */
135 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
138 entry->load_history = 1 + entry->tx_bytes /
139 BOND_TLB_REBALANCE_INTERVAL;
143 entry->tx_slave = NULL;
144 entry->next = TLB_NULL_INDEX;
145 entry->prev = TLB_NULL_INDEX;
148 static inline void tlb_init_slave(struct slave *slave)
150 SLAVE_TLB_INFO(slave).load = 0;
151 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
154 /* Caller must hold bond lock for read */
155 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
157 struct tlb_client_info *tx_hash_table;
160 _lock_tx_hashtbl(bond);
162 /* clear slave from tx_hashtbl */
163 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
165 index = SLAVE_TLB_INFO(slave).head;
166 while (index != TLB_NULL_INDEX) {
167 u32 next_index = tx_hash_table[index].next;
168 tlb_init_table_entry(&tx_hash_table[index], save_load);
172 tlb_init_slave(slave);
174 _unlock_tx_hashtbl(bond);
177 /* Must be called before starting the monitor timer */
178 static int tlb_initialize(struct bonding *bond)
180 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
181 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
182 struct tlb_client_info *new_hashtbl;
185 spin_lock_init(&(bond_info->tx_hashtbl_lock));
187 new_hashtbl = kzalloc(size, GFP_KERNEL);
189 printk(KERN_ERR DRV_NAME
190 ": %s: Error: Failed to allocate TLB hash table\n",
194 _lock_tx_hashtbl(bond);
196 bond_info->tx_hashtbl = new_hashtbl;
198 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
199 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
202 _unlock_tx_hashtbl(bond);
207 /* Must be called only after all slaves have been released */
208 static void tlb_deinitialize(struct bonding *bond)
210 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
212 _lock_tx_hashtbl(bond);
214 kfree(bond_info->tx_hashtbl);
215 bond_info->tx_hashtbl = NULL;
217 _unlock_tx_hashtbl(bond);
220 /* Caller must hold bond lock for read */
221 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
223 struct slave *slave, *least_loaded;
227 /* Find the first enabled slave */
228 bond_for_each_slave(bond, slave, i) {
229 if (SLAVE_IS_OK(slave)) {
239 least_loaded = slave;
240 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
241 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
243 /* Find the slave with the largest gap */
244 bond_for_each_slave_from(bond, slave, i, least_loaded) {
245 if (SLAVE_IS_OK(slave)) {
246 s64 gap = (s64)(slave->speed << 20) -
247 (s64)(SLAVE_TLB_INFO(slave).load << 3);
249 least_loaded = slave;
258 /* Caller must hold bond lock for read */
259 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
261 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
262 struct tlb_client_info *hash_table;
263 struct slave *assigned_slave;
265 _lock_tx_hashtbl(bond);
267 hash_table = bond_info->tx_hashtbl;
268 assigned_slave = hash_table[hash_index].tx_slave;
269 if (!assigned_slave) {
270 assigned_slave = tlb_get_least_loaded_slave(bond);
272 if (assigned_slave) {
273 struct tlb_slave_info *slave_info =
274 &(SLAVE_TLB_INFO(assigned_slave));
275 u32 next_index = slave_info->head;
277 hash_table[hash_index].tx_slave = assigned_slave;
278 hash_table[hash_index].next = next_index;
279 hash_table[hash_index].prev = TLB_NULL_INDEX;
281 if (next_index != TLB_NULL_INDEX) {
282 hash_table[next_index].prev = hash_index;
285 slave_info->head = hash_index;
287 hash_table[hash_index].load_history;
291 if (assigned_slave) {
292 hash_table[hash_index].tx_bytes += skb_len;
295 _unlock_tx_hashtbl(bond);
297 return assigned_slave;
300 /*********************** rlb specific functions ***************************/
301 static inline void _lock_rx_hashtbl(struct bonding *bond)
303 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
306 static inline void _unlock_rx_hashtbl(struct bonding *bond)
308 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
311 /* when an ARP REPLY is received from a client update its info
314 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
316 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
317 struct rlb_client_info *client_info;
320 _lock_rx_hashtbl(bond);
322 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
323 client_info = &(bond_info->rx_hashtbl[hash_index]);
325 if ((client_info->assigned) &&
326 (client_info->ip_src == arp->ip_dst) &&
327 (client_info->ip_dst == arp->ip_src)) {
328 /* update the clients MAC address */
329 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
330 client_info->ntt = 1;
331 bond_info->rx_ntt = 1;
334 _unlock_rx_hashtbl(bond);
337 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
339 struct bonding *bond = bond_dev->priv;
340 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
341 int res = NET_RX_DROP;
343 if (!(bond_dev->flags & IFF_MASTER))
347 dprintk("Packet has no ARP data\n");
351 if (skb->len < sizeof(struct arp_pkt)) {
352 dprintk("Packet is too small to be an ARP\n");
356 if (arp->op_code == htons(ARPOP_REPLY)) {
357 /* update rx hash table for this ARP */
358 rlb_update_entry_from_arp(bond, arp);
359 dprintk("Server received an ARP Reply from client\n");
362 res = NET_RX_SUCCESS;
370 /* Caller must hold bond lock for read */
371 static struct slave *rlb_next_rx_slave(struct bonding *bond)
373 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
374 struct slave *rx_slave, *slave, *start_at;
377 if (bond_info->next_rx_slave) {
378 start_at = bond_info->next_rx_slave;
380 start_at = bond->first_slave;
385 bond_for_each_slave_from(bond, slave, i, start_at) {
386 if (SLAVE_IS_OK(slave)) {
389 } else if (slave->speed > rx_slave->speed) {
396 bond_info->next_rx_slave = rx_slave->next;
402 /* teach the switch the mac of a disabled slave
403 * on the primary for fault tolerance
405 * Caller must hold bond->curr_slave_lock for write or bond lock for write
407 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
409 if (!bond->curr_active_slave) {
413 if (!bond->alb_info.primary_is_promisc) {
414 bond->alb_info.primary_is_promisc = 1;
415 dev_set_promiscuity(bond->curr_active_slave->dev, 1);
418 bond->alb_info.rlb_promisc_timeout_counter = 0;
420 alb_send_learning_packets(bond->curr_active_slave, addr);
423 /* slave being removed should not be active at this point
425 * Caller must hold bond lock for read
427 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
429 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
430 struct rlb_client_info *rx_hash_table;
431 u32 index, next_index;
433 /* clear slave from rx_hashtbl */
434 _lock_rx_hashtbl(bond);
436 rx_hash_table = bond_info->rx_hashtbl;
437 index = bond_info->rx_hashtbl_head;
438 for (; index != RLB_NULL_INDEX; index = next_index) {
439 next_index = rx_hash_table[index].next;
440 if (rx_hash_table[index].slave == slave) {
441 struct slave *assigned_slave = rlb_next_rx_slave(bond);
443 if (assigned_slave) {
444 rx_hash_table[index].slave = assigned_slave;
445 if (memcmp(rx_hash_table[index].mac_dst,
446 mac_bcast, ETH_ALEN)) {
447 bond_info->rx_hashtbl[index].ntt = 1;
448 bond_info->rx_ntt = 1;
449 /* A slave has been removed from the
450 * table because it is either disabled
451 * or being released. We must retry the
452 * update to avoid clients from not
453 * being updated & disconnecting when
456 bond_info->rlb_update_retry_counter =
459 } else { /* there is no active slave */
460 rx_hash_table[index].slave = NULL;
465 _unlock_rx_hashtbl(bond);
467 write_lock(&bond->curr_slave_lock);
469 if (slave != bond->curr_active_slave) {
470 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
473 write_unlock(&bond->curr_slave_lock);
476 static void rlb_update_client(struct rlb_client_info *client_info)
480 if (!client_info->slave) {
484 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
487 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
489 client_info->slave->dev,
491 client_info->mac_dst,
492 client_info->slave->dev->dev_addr,
493 client_info->mac_dst);
495 printk(KERN_ERR DRV_NAME
496 ": %s: Error: failed to create an ARP packet\n",
497 client_info->slave->dev->master->name);
501 skb->dev = client_info->slave->dev;
503 if (client_info->tag) {
504 skb = vlan_put_tag(skb, client_info->vlan_id);
506 printk(KERN_ERR DRV_NAME
507 ": %s: Error: failed to insert VLAN tag\n",
508 client_info->slave->dev->master->name);
517 /* sends ARP REPLIES that update the clients that need updating */
518 static void rlb_update_rx_clients(struct bonding *bond)
520 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
521 struct rlb_client_info *client_info;
524 _lock_rx_hashtbl(bond);
526 hash_index = bond_info->rx_hashtbl_head;
527 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
528 client_info = &(bond_info->rx_hashtbl[hash_index]);
529 if (client_info->ntt) {
530 rlb_update_client(client_info);
531 if (bond_info->rlb_update_retry_counter == 0) {
532 client_info->ntt = 0;
537 /* do not update the entries again untill this counter is zero so that
538 * not to confuse the clients.
540 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
542 _unlock_rx_hashtbl(bond);
545 /* The slave was assigned a new mac address - update the clients */
546 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
548 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
549 struct rlb_client_info *client_info;
553 _lock_rx_hashtbl(bond);
555 hash_index = bond_info->rx_hashtbl_head;
556 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
557 client_info = &(bond_info->rx_hashtbl[hash_index]);
559 if ((client_info->slave == slave) &&
560 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
561 client_info->ntt = 1;
566 // update the team's flag only after the whole iteration
568 bond_info->rx_ntt = 1;
570 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
573 _unlock_rx_hashtbl(bond);
576 /* mark all clients using src_ip to be updated */
577 static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
579 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
580 struct rlb_client_info *client_info;
583 _lock_rx_hashtbl(bond);
585 hash_index = bond_info->rx_hashtbl_head;
586 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
587 client_info = &(bond_info->rx_hashtbl[hash_index]);
589 if (!client_info->slave) {
590 printk(KERN_ERR DRV_NAME
591 ": %s: Error: found a client with no channel in "
592 "the client's hash table\n",
596 /*update all clients using this src_ip, that are not assigned
597 * to the team's address (curr_active_slave) and have a known
598 * unicast mac address.
600 if ((client_info->ip_src == src_ip) &&
601 memcmp(client_info->slave->dev->dev_addr,
602 bond->dev->dev_addr, ETH_ALEN) &&
603 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
604 client_info->ntt = 1;
605 bond_info->rx_ntt = 1;
609 _unlock_rx_hashtbl(bond);
612 /* Caller must hold both bond and ptr locks for read */
613 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
615 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
616 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
617 struct slave *assigned_slave;
618 struct rlb_client_info *client_info;
621 _lock_rx_hashtbl(bond);
623 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
624 client_info = &(bond_info->rx_hashtbl[hash_index]);
626 if (client_info->assigned) {
627 if ((client_info->ip_src == arp->ip_src) &&
628 (client_info->ip_dst == arp->ip_dst)) {
629 /* the entry is already assigned to this client */
630 if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
631 /* update mac address from arp */
632 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
635 assigned_slave = client_info->slave;
636 if (assigned_slave) {
637 _unlock_rx_hashtbl(bond);
638 return assigned_slave;
641 /* the entry is already assigned to some other client,
642 * move the old client to primary (curr_active_slave) so
643 * that the new client can be assigned to this entry.
645 if (bond->curr_active_slave &&
646 client_info->slave != bond->curr_active_slave) {
647 client_info->slave = bond->curr_active_slave;
648 rlb_update_client(client_info);
652 /* assign a new slave */
653 assigned_slave = rlb_next_rx_slave(bond);
655 if (assigned_slave) {
656 client_info->ip_src = arp->ip_src;
657 client_info->ip_dst = arp->ip_dst;
658 /* arp->mac_dst is broadcast for arp reqeusts.
659 * will be updated with clients actual unicast mac address
660 * upon receiving an arp reply.
662 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
663 client_info->slave = assigned_slave;
665 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
666 client_info->ntt = 1;
667 bond->alb_info.rx_ntt = 1;
669 client_info->ntt = 0;
672 if (!list_empty(&bond->vlan_list)) {
673 unsigned short vlan_id;
674 int res = vlan_get_tag(skb, &vlan_id);
676 client_info->tag = 1;
677 client_info->vlan_id = vlan_id;
681 if (!client_info->assigned) {
682 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
683 bond_info->rx_hashtbl_head = hash_index;
684 client_info->next = prev_tbl_head;
685 if (prev_tbl_head != RLB_NULL_INDEX) {
686 bond_info->rx_hashtbl[prev_tbl_head].prev =
689 client_info->assigned = 1;
693 _unlock_rx_hashtbl(bond);
695 return assigned_slave;
698 /* chooses (and returns) transmit channel for arp reply
699 * does not choose channel for other arp types since they are
700 * sent on the curr_active_slave
702 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
704 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
705 struct slave *tx_slave = NULL;
707 if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
708 /* the arp must be sent on the selected
711 tx_slave = rlb_choose_channel(skb, bond);
713 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
715 dprintk("Server sent ARP Reply packet\n");
716 } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
717 /* Create an entry in the rx_hashtbl for this client as a
719 * When the arp reply is received the entry will be updated
720 * with the correct unicast address of the client.
722 rlb_choose_channel(skb, bond);
724 /* The ARP relpy packets must be delayed so that
725 * they can cancel out the influence of the ARP request.
727 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
729 /* arp requests are broadcast and are sent on the primary
730 * the arp request will collapse all clients on the subnet to
731 * the primary slave. We must register these clients to be
732 * updated with their assigned mac.
734 rlb_req_update_subnet_clients(bond, arp->ip_src);
735 dprintk("Server sent ARP Request packet\n");
741 /* Caller must hold bond lock for read */
742 static void rlb_rebalance(struct bonding *bond)
744 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
745 struct slave *assigned_slave;
746 struct rlb_client_info *client_info;
750 _lock_rx_hashtbl(bond);
753 hash_index = bond_info->rx_hashtbl_head;
754 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
755 client_info = &(bond_info->rx_hashtbl[hash_index]);
756 assigned_slave = rlb_next_rx_slave(bond);
757 if (assigned_slave && (client_info->slave != assigned_slave)) {
758 client_info->slave = assigned_slave;
759 client_info->ntt = 1;
764 /* update the team's flag only after the whole iteration */
766 bond_info->rx_ntt = 1;
768 _unlock_rx_hashtbl(bond);
771 /* Caller must hold rx_hashtbl lock */
772 static void rlb_init_table_entry(struct rlb_client_info *entry)
774 memset(entry, 0, sizeof(struct rlb_client_info));
775 entry->next = RLB_NULL_INDEX;
776 entry->prev = RLB_NULL_INDEX;
779 static int rlb_initialize(struct bonding *bond)
781 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
782 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
783 struct rlb_client_info *new_hashtbl;
784 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
787 spin_lock_init(&(bond_info->rx_hashtbl_lock));
789 new_hashtbl = kmalloc(size, GFP_KERNEL);
791 printk(KERN_ERR DRV_NAME
792 ": %s: Error: Failed to allocate RLB hash table\n",
796 _lock_rx_hashtbl(bond);
798 bond_info->rx_hashtbl = new_hashtbl;
800 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
802 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
803 rlb_init_table_entry(bond_info->rx_hashtbl + i);
806 _unlock_rx_hashtbl(bond);
808 /*initialize packet type*/
809 pk_type->type = __constant_htons(ETH_P_ARP);
810 pk_type->dev = bond->dev;
811 pk_type->func = rlb_arp_recv;
813 /* register to receive ARPs */
814 dev_add_pack(pk_type);
819 static void rlb_deinitialize(struct bonding *bond)
821 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
823 dev_remove_pack(&(bond_info->rlb_pkt_type));
825 _lock_rx_hashtbl(bond);
827 kfree(bond_info->rx_hashtbl);
828 bond_info->rx_hashtbl = NULL;
829 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
831 _unlock_rx_hashtbl(bond);
834 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
836 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
839 _lock_rx_hashtbl(bond);
841 curr_index = bond_info->rx_hashtbl_head;
842 while (curr_index != RLB_NULL_INDEX) {
843 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
844 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
845 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
847 if (curr->tag && (curr->vlan_id == vlan_id)) {
848 if (curr_index == bond_info->rx_hashtbl_head) {
849 bond_info->rx_hashtbl_head = next_index;
851 if (prev_index != RLB_NULL_INDEX) {
852 bond_info->rx_hashtbl[prev_index].next = next_index;
854 if (next_index != RLB_NULL_INDEX) {
855 bond_info->rx_hashtbl[next_index].prev = prev_index;
858 rlb_init_table_entry(curr);
861 curr_index = next_index;
864 _unlock_rx_hashtbl(bond);
867 /*********************** tlb/rlb shared functions *********************/
869 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
871 struct bonding *bond = bond_get_bond_by_slave(slave);
872 struct learning_pkt pkt;
873 int size = sizeof(struct learning_pkt);
876 memset(&pkt, 0, size);
877 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
878 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
879 pkt.type = __constant_htons(ETH_P_LOOP);
881 for (i = 0; i < MAX_LP_BURST; i++) {
885 skb = dev_alloc_skb(size);
890 data = skb_put(skb, size);
891 memcpy(data, &pkt, size);
894 skb->nh.raw = data + ETH_HLEN;
895 skb->protocol = pkt.type;
896 skb->priority = TC_PRIO_CONTROL;
897 skb->dev = slave->dev;
899 if (!list_empty(&bond->vlan_list)) {
900 struct vlan_entry *vlan;
902 vlan = bond_next_vlan(bond,
903 bond->alb_info.current_alb_vlan);
905 bond->alb_info.current_alb_vlan = vlan;
911 skb = vlan_put_tag(skb, vlan->vlan_id);
913 printk(KERN_ERR DRV_NAME
914 ": %s: Error: failed to insert VLAN tag\n",
924 /* hw is a boolean parameter that determines whether we should try and
925 * set the hw address of the device as well as the hw address of the
928 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
930 struct net_device *dev = slave->dev;
931 struct sockaddr s_addr;
934 memcpy(dev->dev_addr, addr, dev->addr_len);
938 /* for rlb each slave must have a unique hw mac addresses so that */
939 /* each slave will receive packets destined to a different mac */
940 memcpy(s_addr.sa_data, addr, dev->addr_len);
941 s_addr.sa_family = dev->type;
942 if (dev_set_mac_address(dev, &s_addr)) {
943 printk(KERN_ERR DRV_NAME
944 ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
945 "mode requires that the base driver support setting "
946 "the hw address also when the network device's "
947 "interface is open\n",
948 dev->master->name, dev->name);
954 /* Caller must hold bond lock for write or curr_slave_lock for write*/
955 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
957 struct slave *disabled_slave = NULL;
958 u8 tmp_mac_addr[ETH_ALEN];
959 int slaves_state_differ;
961 slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
963 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
964 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
965 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
967 /* fasten the change in the switch */
968 if (SLAVE_IS_OK(slave1)) {
969 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
970 if (bond->alb_info.rlb_enabled) {
971 /* inform the clients that the mac address
974 rlb_req_update_slave_clients(bond, slave1);
977 disabled_slave = slave1;
980 if (SLAVE_IS_OK(slave2)) {
981 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
982 if (bond->alb_info.rlb_enabled) {
983 /* inform the clients that the mac address
986 rlb_req_update_slave_clients(bond, slave2);
989 disabled_slave = slave2;
992 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
993 /* A disabled slave was assigned an active mac addr */
994 rlb_teach_disabled_mac_on_primary(bond,
995 disabled_slave->dev->dev_addr);
1000 * alb_change_hw_addr_on_detach
1001 * @bond: bonding we're working on
1002 * @slave: the slave that was just detached
1004 * We assume that @slave was already detached from the slave list.
1006 * If @slave's permanent hw address is different both from its current
1007 * address and from @bond's address, then somewhere in the bond there's
1008 * a slave that has @slave's permanet address as its current address.
1009 * We'll make sure that that slave no longer uses @slave's permanent address.
1011 * Caller must hold bond lock
1013 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1018 perm_curr_diff = memcmp(slave->perm_hwaddr,
1019 slave->dev->dev_addr,
1021 perm_bond_diff = memcmp(slave->perm_hwaddr,
1022 bond->dev->dev_addr,
1025 if (perm_curr_diff && perm_bond_diff) {
1026 struct slave *tmp_slave;
1029 bond_for_each_slave(bond, tmp_slave, i) {
1030 if (!memcmp(slave->perm_hwaddr,
1031 tmp_slave->dev->dev_addr,
1039 alb_swap_mac_addr(bond, slave, tmp_slave);
1045 * alb_handle_addr_collision_on_attach
1046 * @bond: bonding we're working on
1047 * @slave: the slave that was just attached
1049 * checks uniqueness of slave's mac address and handles the case the
1050 * new slave uses the bonds mac address.
1052 * If the permanent hw address of @slave is @bond's hw address, we need to
1053 * find a different hw address to give @slave, that isn't in use by any other
1054 * slave in the bond. This address must be, of course, one of the premanent
1055 * addresses of the other slaves.
1057 * We go over the slave list, and for each slave there we compare its
1058 * permanent hw address with the current address of all the other slaves.
1059 * If no match was found, then we've found a slave with a permanent address
1060 * that isn't used by any other slave in the bond, so we can assign it to
1063 * assumption: this function is called before @slave is attached to the
1066 * caller must hold the bond lock for write since the mac addresses are compared
1067 * and may be swapped.
1069 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1071 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1072 struct slave *has_bond_addr = bond->curr_active_slave;
1073 int i, j, found = 0;
1075 if (bond->slave_cnt == 0) {
1076 /* this is the first slave */
1080 /* if slave's mac address differs from bond's mac address
1081 * check uniqueness of slave's mac address against the other
1082 * slaves in the bond.
1084 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1085 bond_for_each_slave(bond, tmp_slave1, i) {
1086 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1096 /* Try setting slave mac to bond address and fall-through
1097 to code handling that situation below... */
1098 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1099 bond->alb_info.rlb_enabled);
1102 /* The slave's address is equal to the address of the bond.
1103 * Search for a spare address in the bond for this slave.
1105 free_mac_slave = NULL;
1107 bond_for_each_slave(bond, tmp_slave1, i) {
1109 bond_for_each_slave(bond, tmp_slave2, j) {
1110 if (!memcmp(tmp_slave1->perm_hwaddr,
1111 tmp_slave2->dev->dev_addr,
1119 /* no slave has tmp_slave1's perm addr
1122 free_mac_slave = tmp_slave1;
1126 if (!has_bond_addr) {
1127 if (!memcmp(tmp_slave1->dev->dev_addr,
1128 bond->dev->dev_addr,
1131 has_bond_addr = tmp_slave1;
1136 if (free_mac_slave) {
1137 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1138 bond->alb_info.rlb_enabled);
1140 printk(KERN_WARNING DRV_NAME
1141 ": %s: Warning: the hw address of slave %s is in use by "
1142 "the bond; giving it the hw address of %s\n",
1143 bond->dev->name, slave->dev->name, free_mac_slave->dev->name);
1145 } else if (has_bond_addr) {
1146 printk(KERN_ERR DRV_NAME
1147 ": %s: Error: the hw address of slave %s is in use by the "
1148 "bond; couldn't find a slave with a free hw address to "
1149 "give it (this should not have happened)\n",
1150 bond->dev->name, slave->dev->name);
1158 * alb_set_mac_address
1162 * In TLB mode all slaves are configured to the bond's hw address, but set
1163 * their dev_addr field to different addresses (based on their permanent hw
1166 * For each slave, this function sets the interface to the new address and then
1167 * changes its dev_addr field to its previous value.
1169 * Unwinding assumes bond's mac address has not yet changed.
1171 static int alb_set_mac_address(struct bonding *bond, void *addr)
1174 struct slave *slave, *stop_at;
1175 char tmp_addr[ETH_ALEN];
1179 if (bond->alb_info.rlb_enabled) {
1183 bond_for_each_slave(bond, slave, i) {
1184 if (slave->dev->set_mac_address == NULL) {
1189 /* save net_device's current hw address */
1190 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1192 res = dev_set_mac_address(slave->dev, addr);
1194 /* restore net_device's hw address */
1195 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1205 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1206 sa.sa_family = bond->dev->type;
1208 /* unwind from head to the slave that failed */
1210 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1211 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1212 dev_set_mac_address(slave->dev, &sa);
1213 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1219 /************************ exported alb funcions ************************/
1221 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1225 res = tlb_initialize(bond);
1231 bond->alb_info.rlb_enabled = 1;
1232 /* initialize rlb */
1233 res = rlb_initialize(bond);
1235 tlb_deinitialize(bond);
1239 bond->alb_info.rlb_enabled = 0;
1245 void bond_alb_deinitialize(struct bonding *bond)
1247 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1249 tlb_deinitialize(bond);
1251 if (bond_info->rlb_enabled) {
1252 rlb_deinitialize(bond);
1256 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1258 struct bonding *bond = bond_dev->priv;
1259 struct ethhdr *eth_data;
1260 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1261 struct slave *tx_slave = NULL;
1262 static const u32 ip_bcast = 0xffffffff;
1264 int do_tx_balance = 1;
1266 u8 *hash_start = NULL;
1269 skb->mac.raw = (unsigned char *)skb->data;
1270 eth_data = eth_hdr(skb);
1272 /* make sure that the curr_active_slave and the slaves list do
1273 * not change during tx
1275 read_lock(&bond->lock);
1276 read_lock(&bond->curr_slave_lock);
1278 if (!BOND_IS_OK(bond)) {
1282 switch (ntohs(skb->protocol)) {
1284 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1285 (skb->nh.iph->daddr == ip_bcast) ||
1286 (skb->nh.iph->protocol == IPPROTO_IGMP)) {
1290 hash_start = (char*)&(skb->nh.iph->daddr);
1291 hash_size = sizeof(skb->nh.iph->daddr);
1294 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1299 hash_start = (char*)&(skb->nh.ipv6h->daddr);
1300 hash_size = sizeof(skb->nh.ipv6h->daddr);
1303 if (ipx_hdr(skb)->ipx_checksum !=
1304 __constant_htons(IPX_NO_CHECKSUM)) {
1305 /* something is wrong with this packet */
1310 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1311 /* The only protocol worth balancing in
1312 * this family since it has an "ARP" like
1319 hash_start = (char*)eth_data->h_dest;
1320 hash_size = ETH_ALEN;
1324 if (bond_info->rlb_enabled) {
1325 tx_slave = rlb_arp_xmit(skb, bond);
1333 if (do_tx_balance) {
1334 hash_index = _simple_hash(hash_start, hash_size);
1335 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1339 /* unbalanced or unassigned, send through primary */
1340 tx_slave = bond->curr_active_slave;
1341 bond_info->unbalanced_load += skb->len;
1344 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1345 if (tx_slave != bond->curr_active_slave) {
1346 memcpy(eth_data->h_source,
1347 tx_slave->dev->dev_addr,
1351 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1354 tlb_clear_slave(bond, tx_slave, 0);
1360 /* no suitable interface, frame not sent */
1363 read_unlock(&bond->curr_slave_lock);
1364 read_unlock(&bond->lock);
1368 void bond_alb_monitor(struct bonding *bond)
1370 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1371 struct slave *slave;
1374 read_lock(&bond->lock);
1376 if (bond->kill_timers) {
1380 if (bond->slave_cnt == 0) {
1381 bond_info->tx_rebalance_counter = 0;
1382 bond_info->lp_counter = 0;
1386 bond_info->tx_rebalance_counter++;
1387 bond_info->lp_counter++;
1389 /* send learning packets */
1390 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1391 /* change of curr_active_slave involves swapping of mac addresses.
1392 * in order to avoid this swapping from happening while
1393 * sending the learning packets, the curr_slave_lock must be held for
1396 read_lock(&bond->curr_slave_lock);
1398 bond_for_each_slave(bond, slave, i) {
1399 alb_send_learning_packets(slave, slave->dev->dev_addr);
1402 read_unlock(&bond->curr_slave_lock);
1404 bond_info->lp_counter = 0;
1407 /* rebalance tx traffic */
1408 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1410 read_lock(&bond->curr_slave_lock);
1412 bond_for_each_slave(bond, slave, i) {
1413 tlb_clear_slave(bond, slave, 1);
1414 if (slave == bond->curr_active_slave) {
1415 SLAVE_TLB_INFO(slave).load =
1416 bond_info->unbalanced_load /
1417 BOND_TLB_REBALANCE_INTERVAL;
1418 bond_info->unbalanced_load = 0;
1422 read_unlock(&bond->curr_slave_lock);
1424 bond_info->tx_rebalance_counter = 0;
1427 /* handle rlb stuff */
1428 if (bond_info->rlb_enabled) {
1429 /* the following code changes the promiscuity of the
1430 * the curr_active_slave. It needs to be locked with a
1431 * write lock to protect from other code that also
1432 * sets the promiscuity.
1434 write_lock_bh(&bond->curr_slave_lock);
1436 if (bond_info->primary_is_promisc &&
1437 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1439 bond_info->rlb_promisc_timeout_counter = 0;
1441 /* If the primary was set to promiscuous mode
1442 * because a slave was disabled then
1443 * it can now leave promiscuous mode.
1445 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1446 bond_info->primary_is_promisc = 0;
1449 write_unlock_bh(&bond->curr_slave_lock);
1451 if (bond_info->rlb_rebalance) {
1452 bond_info->rlb_rebalance = 0;
1453 rlb_rebalance(bond);
1456 /* check if clients need updating */
1457 if (bond_info->rx_ntt) {
1458 if (bond_info->rlb_update_delay_counter) {
1459 --bond_info->rlb_update_delay_counter;
1461 rlb_update_rx_clients(bond);
1462 if (bond_info->rlb_update_retry_counter) {
1463 --bond_info->rlb_update_retry_counter;
1465 bond_info->rx_ntt = 0;
1472 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1474 read_unlock(&bond->lock);
1477 /* assumption: called before the slave is attached to the bond
1478 * and not locked by the bond lock
1480 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1484 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1485 bond->alb_info.rlb_enabled);
1490 /* caller must hold the bond lock for write since the mac addresses
1491 * are compared and may be swapped.
1493 write_lock_bh(&bond->lock);
1495 res = alb_handle_addr_collision_on_attach(bond, slave);
1497 write_unlock_bh(&bond->lock);
1503 tlb_init_slave(slave);
1505 /* order a rebalance ASAP */
1506 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1508 if (bond->alb_info.rlb_enabled) {
1509 bond->alb_info.rlb_rebalance = 1;
1515 /* Caller must hold bond lock for write */
1516 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1518 if (bond->slave_cnt > 1) {
1519 alb_change_hw_addr_on_detach(bond, slave);
1522 tlb_clear_slave(bond, slave, 0);
1524 if (bond->alb_info.rlb_enabled) {
1525 bond->alb_info.next_rx_slave = NULL;
1526 rlb_clear_slave(bond, slave);
1530 /* Caller must hold bond lock for read */
1531 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1533 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1535 if (link == BOND_LINK_DOWN) {
1536 tlb_clear_slave(bond, slave, 0);
1537 if (bond->alb_info.rlb_enabled) {
1538 rlb_clear_slave(bond, slave);
1540 } else if (link == BOND_LINK_UP) {
1541 /* order a rebalance ASAP */
1542 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1543 if (bond->alb_info.rlb_enabled) {
1544 bond->alb_info.rlb_rebalance = 1;
1545 /* If the updelay module parameter is smaller than the
1546 * forwarding delay of the switch the rebalance will
1547 * not work because the rebalance arp replies will
1548 * not be forwarded to the clients..
1555 * bond_alb_handle_active_change - assign new curr_active_slave
1556 * @bond: our bonding struct
1557 * @new_slave: new slave to assign
1559 * Set the bond->curr_active_slave to @new_slave and handle
1560 * mac address swapping and promiscuity changes as needed.
1562 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1564 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1566 struct slave *swap_slave;
1569 if (bond->curr_active_slave == new_slave) {
1573 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1574 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1575 bond->alb_info.primary_is_promisc = 0;
1576 bond->alb_info.rlb_promisc_timeout_counter = 0;
1579 swap_slave = bond->curr_active_slave;
1580 bond->curr_active_slave = new_slave;
1582 if (!new_slave || (bond->slave_cnt == 0)) {
1586 /* set the new curr_active_slave to the bonds mac address
1587 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1590 struct slave *tmp_slave;
1591 /* find slave that is holding the bond's mac address */
1592 bond_for_each_slave(bond, tmp_slave, i) {
1593 if (!memcmp(tmp_slave->dev->dev_addr,
1594 bond->dev->dev_addr, ETH_ALEN)) {
1595 swap_slave = tmp_slave;
1601 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1603 /* swap mac address */
1604 alb_swap_mac_addr(bond, swap_slave, new_slave);
1606 /* set the new_slave to the bond mac address */
1607 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1608 bond->alb_info.rlb_enabled);
1609 /* fasten bond mac on new current slave */
1610 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1614 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1616 struct bonding *bond = bond_dev->priv;
1617 struct sockaddr *sa = addr;
1618 struct slave *slave, *swap_slave;
1622 if (!is_valid_ether_addr(sa->sa_data)) {
1623 return -EADDRNOTAVAIL;
1626 res = alb_set_mac_address(bond, addr);
1631 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1633 /* If there is no curr_active_slave there is nothing else to do.
1634 * Otherwise we'll need to pass the new address to it and handle
1637 if (!bond->curr_active_slave) {
1643 bond_for_each_slave(bond, slave, i) {
1644 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1651 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1653 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1654 bond->alb_info.rlb_enabled);
1656 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1657 if (bond->alb_info.rlb_enabled) {
1658 /* inform clients mac address has changed */
1659 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1666 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1668 if (bond->alb_info.current_alb_vlan &&
1669 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1670 bond->alb_info.current_alb_vlan = NULL;
1673 if (bond->alb_info.rlb_enabled) {
1674 rlb_clear_vlan(bond, vlan_id);