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 = kmalloc(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 memset(bond_info->tx_hashtbl, 0, size);
200 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
201 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
204 _unlock_tx_hashtbl(bond);
209 /* Must be called only after all slaves have been released */
210 static void tlb_deinitialize(struct bonding *bond)
212 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
214 _lock_tx_hashtbl(bond);
216 kfree(bond_info->tx_hashtbl);
217 bond_info->tx_hashtbl = NULL;
219 _unlock_tx_hashtbl(bond);
222 /* Caller must hold bond lock for read */
223 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
225 struct slave *slave, *least_loaded;
229 /* Find the first enabled slave */
230 bond_for_each_slave(bond, slave, i) {
231 if (SLAVE_IS_OK(slave)) {
241 least_loaded = slave;
242 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
243 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
245 /* Find the slave with the largest gap */
246 bond_for_each_slave_from(bond, slave, i, least_loaded) {
247 if (SLAVE_IS_OK(slave)) {
248 s64 gap = (s64)(slave->speed << 20) -
249 (s64)(SLAVE_TLB_INFO(slave).load << 3);
251 least_loaded = slave;
260 /* Caller must hold bond lock for read */
261 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
263 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
264 struct tlb_client_info *hash_table;
265 struct slave *assigned_slave;
267 _lock_tx_hashtbl(bond);
269 hash_table = bond_info->tx_hashtbl;
270 assigned_slave = hash_table[hash_index].tx_slave;
271 if (!assigned_slave) {
272 assigned_slave = tlb_get_least_loaded_slave(bond);
274 if (assigned_slave) {
275 struct tlb_slave_info *slave_info =
276 &(SLAVE_TLB_INFO(assigned_slave));
277 u32 next_index = slave_info->head;
279 hash_table[hash_index].tx_slave = assigned_slave;
280 hash_table[hash_index].next = next_index;
281 hash_table[hash_index].prev = TLB_NULL_INDEX;
283 if (next_index != TLB_NULL_INDEX) {
284 hash_table[next_index].prev = hash_index;
287 slave_info->head = hash_index;
289 hash_table[hash_index].load_history;
293 if (assigned_slave) {
294 hash_table[hash_index].tx_bytes += skb_len;
297 _unlock_tx_hashtbl(bond);
299 return assigned_slave;
302 /*********************** rlb specific functions ***************************/
303 static inline void _lock_rx_hashtbl(struct bonding *bond)
305 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
308 static inline void _unlock_rx_hashtbl(struct bonding *bond)
310 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
313 /* when an ARP REPLY is received from a client update its info
316 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
318 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
319 struct rlb_client_info *client_info;
322 _lock_rx_hashtbl(bond);
324 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
325 client_info = &(bond_info->rx_hashtbl[hash_index]);
327 if ((client_info->assigned) &&
328 (client_info->ip_src == arp->ip_dst) &&
329 (client_info->ip_dst == arp->ip_src)) {
330 /* update the clients MAC address */
331 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
332 client_info->ntt = 1;
333 bond_info->rx_ntt = 1;
336 _unlock_rx_hashtbl(bond);
339 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
341 struct bonding *bond = bond_dev->priv;
342 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
343 int res = NET_RX_DROP;
345 if (!(bond_dev->flags & IFF_MASTER))
349 dprintk("Packet has no ARP data\n");
353 if (skb->len < sizeof(struct arp_pkt)) {
354 dprintk("Packet is too small to be an ARP\n");
358 if (arp->op_code == htons(ARPOP_REPLY)) {
359 /* update rx hash table for this ARP */
360 rlb_update_entry_from_arp(bond, arp);
361 dprintk("Server received an ARP Reply from client\n");
364 res = NET_RX_SUCCESS;
372 /* Caller must hold bond lock for read */
373 static struct slave *rlb_next_rx_slave(struct bonding *bond)
375 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
376 struct slave *rx_slave, *slave, *start_at;
379 if (bond_info->next_rx_slave) {
380 start_at = bond_info->next_rx_slave;
382 start_at = bond->first_slave;
387 bond_for_each_slave_from(bond, slave, i, start_at) {
388 if (SLAVE_IS_OK(slave)) {
391 } else if (slave->speed > rx_slave->speed) {
398 bond_info->next_rx_slave = rx_slave->next;
404 /* teach the switch the mac of a disabled slave
405 * on the primary for fault tolerance
407 * Caller must hold bond->curr_slave_lock for write or bond lock for write
409 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
411 if (!bond->curr_active_slave) {
415 if (!bond->alb_info.primary_is_promisc) {
416 bond->alb_info.primary_is_promisc = 1;
417 dev_set_promiscuity(bond->curr_active_slave->dev, 1);
420 bond->alb_info.rlb_promisc_timeout_counter = 0;
422 alb_send_learning_packets(bond->curr_active_slave, addr);
425 /* slave being removed should not be active at this point
427 * Caller must hold bond lock for read
429 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
431 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
432 struct rlb_client_info *rx_hash_table;
433 u32 index, next_index;
435 /* clear slave from rx_hashtbl */
436 _lock_rx_hashtbl(bond);
438 rx_hash_table = bond_info->rx_hashtbl;
439 index = bond_info->rx_hashtbl_head;
440 for (; index != RLB_NULL_INDEX; index = next_index) {
441 next_index = rx_hash_table[index].next;
442 if (rx_hash_table[index].slave == slave) {
443 struct slave *assigned_slave = rlb_next_rx_slave(bond);
445 if (assigned_slave) {
446 rx_hash_table[index].slave = assigned_slave;
447 if (memcmp(rx_hash_table[index].mac_dst,
448 mac_bcast, ETH_ALEN)) {
449 bond_info->rx_hashtbl[index].ntt = 1;
450 bond_info->rx_ntt = 1;
451 /* A slave has been removed from the
452 * table because it is either disabled
453 * or being released. We must retry the
454 * update to avoid clients from not
455 * being updated & disconnecting when
458 bond_info->rlb_update_retry_counter =
461 } else { /* there is no active slave */
462 rx_hash_table[index].slave = NULL;
467 _unlock_rx_hashtbl(bond);
469 write_lock(&bond->curr_slave_lock);
471 if (slave != bond->curr_active_slave) {
472 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
475 write_unlock(&bond->curr_slave_lock);
478 static void rlb_update_client(struct rlb_client_info *client_info)
482 if (!client_info->slave) {
486 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
489 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
491 client_info->slave->dev,
493 client_info->mac_dst,
494 client_info->slave->dev->dev_addr,
495 client_info->mac_dst);
497 printk(KERN_ERR DRV_NAME
498 ": %s: Error: failed to create an ARP packet\n",
499 client_info->slave->dev->master->name);
503 skb->dev = client_info->slave->dev;
505 if (client_info->tag) {
506 skb = vlan_put_tag(skb, client_info->vlan_id);
508 printk(KERN_ERR DRV_NAME
509 ": %s: Error: failed to insert VLAN tag\n",
510 client_info->slave->dev->master->name);
519 /* sends ARP REPLIES that update the clients that need updating */
520 static void rlb_update_rx_clients(struct bonding *bond)
522 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
523 struct rlb_client_info *client_info;
526 _lock_rx_hashtbl(bond);
528 hash_index = bond_info->rx_hashtbl_head;
529 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
530 client_info = &(bond_info->rx_hashtbl[hash_index]);
531 if (client_info->ntt) {
532 rlb_update_client(client_info);
533 if (bond_info->rlb_update_retry_counter == 0) {
534 client_info->ntt = 0;
539 /* do not update the entries again untill this counter is zero so that
540 * not to confuse the clients.
542 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
544 _unlock_rx_hashtbl(bond);
547 /* The slave was assigned a new mac address - update the clients */
548 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
550 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
551 struct rlb_client_info *client_info;
555 _lock_rx_hashtbl(bond);
557 hash_index = bond_info->rx_hashtbl_head;
558 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
559 client_info = &(bond_info->rx_hashtbl[hash_index]);
561 if ((client_info->slave == slave) &&
562 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
563 client_info->ntt = 1;
568 // update the team's flag only after the whole iteration
570 bond_info->rx_ntt = 1;
572 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
575 _unlock_rx_hashtbl(bond);
578 /* mark all clients using src_ip to be updated */
579 static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
581 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
582 struct rlb_client_info *client_info;
585 _lock_rx_hashtbl(bond);
587 hash_index = bond_info->rx_hashtbl_head;
588 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
589 client_info = &(bond_info->rx_hashtbl[hash_index]);
591 if (!client_info->slave) {
592 printk(KERN_ERR DRV_NAME
593 ": %s: Error: found a client with no channel in "
594 "the client's hash table\n",
598 /*update all clients using this src_ip, that are not assigned
599 * to the team's address (curr_active_slave) and have a known
600 * unicast mac address.
602 if ((client_info->ip_src == src_ip) &&
603 memcmp(client_info->slave->dev->dev_addr,
604 bond->dev->dev_addr, ETH_ALEN) &&
605 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
606 client_info->ntt = 1;
607 bond_info->rx_ntt = 1;
611 _unlock_rx_hashtbl(bond);
614 /* Caller must hold both bond and ptr locks for read */
615 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
617 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
618 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
619 struct slave *assigned_slave;
620 struct rlb_client_info *client_info;
623 _lock_rx_hashtbl(bond);
625 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
626 client_info = &(bond_info->rx_hashtbl[hash_index]);
628 if (client_info->assigned) {
629 if ((client_info->ip_src == arp->ip_src) &&
630 (client_info->ip_dst == arp->ip_dst)) {
631 /* the entry is already assigned to this client */
632 if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
633 /* update mac address from arp */
634 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
637 assigned_slave = client_info->slave;
638 if (assigned_slave) {
639 _unlock_rx_hashtbl(bond);
640 return assigned_slave;
643 /* the entry is already assigned to some other client,
644 * move the old client to primary (curr_active_slave) so
645 * that the new client can be assigned to this entry.
647 if (bond->curr_active_slave &&
648 client_info->slave != bond->curr_active_slave) {
649 client_info->slave = bond->curr_active_slave;
650 rlb_update_client(client_info);
654 /* assign a new slave */
655 assigned_slave = rlb_next_rx_slave(bond);
657 if (assigned_slave) {
658 client_info->ip_src = arp->ip_src;
659 client_info->ip_dst = arp->ip_dst;
660 /* arp->mac_dst is broadcast for arp reqeusts.
661 * will be updated with clients actual unicast mac address
662 * upon receiving an arp reply.
664 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
665 client_info->slave = assigned_slave;
667 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
668 client_info->ntt = 1;
669 bond->alb_info.rx_ntt = 1;
671 client_info->ntt = 0;
674 if (!list_empty(&bond->vlan_list)) {
675 unsigned short vlan_id;
676 int res = vlan_get_tag(skb, &vlan_id);
678 client_info->tag = 1;
679 client_info->vlan_id = vlan_id;
683 if (!client_info->assigned) {
684 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
685 bond_info->rx_hashtbl_head = hash_index;
686 client_info->next = prev_tbl_head;
687 if (prev_tbl_head != RLB_NULL_INDEX) {
688 bond_info->rx_hashtbl[prev_tbl_head].prev =
691 client_info->assigned = 1;
695 _unlock_rx_hashtbl(bond);
697 return assigned_slave;
700 /* chooses (and returns) transmit channel for arp reply
701 * does not choose channel for other arp types since they are
702 * sent on the curr_active_slave
704 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
706 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
707 struct slave *tx_slave = NULL;
709 if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
710 /* the arp must be sent on the selected
713 tx_slave = rlb_choose_channel(skb, bond);
715 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
717 dprintk("Server sent ARP Reply packet\n");
718 } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
719 /* Create an entry in the rx_hashtbl for this client as a
721 * When the arp reply is received the entry will be updated
722 * with the correct unicast address of the client.
724 rlb_choose_channel(skb, bond);
726 /* The ARP relpy packets must be delayed so that
727 * they can cancel out the influence of the ARP request.
729 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
731 /* arp requests are broadcast and are sent on the primary
732 * the arp request will collapse all clients on the subnet to
733 * the primary slave. We must register these clients to be
734 * updated with their assigned mac.
736 rlb_req_update_subnet_clients(bond, arp->ip_src);
737 dprintk("Server sent ARP Request packet\n");
743 /* Caller must hold bond lock for read */
744 static void rlb_rebalance(struct bonding *bond)
746 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
747 struct slave *assigned_slave;
748 struct rlb_client_info *client_info;
752 _lock_rx_hashtbl(bond);
755 hash_index = bond_info->rx_hashtbl_head;
756 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
757 client_info = &(bond_info->rx_hashtbl[hash_index]);
758 assigned_slave = rlb_next_rx_slave(bond);
759 if (assigned_slave && (client_info->slave != assigned_slave)) {
760 client_info->slave = assigned_slave;
761 client_info->ntt = 1;
766 /* update the team's flag only after the whole iteration */
768 bond_info->rx_ntt = 1;
770 _unlock_rx_hashtbl(bond);
773 /* Caller must hold rx_hashtbl lock */
774 static void rlb_init_table_entry(struct rlb_client_info *entry)
776 memset(entry, 0, sizeof(struct rlb_client_info));
777 entry->next = RLB_NULL_INDEX;
778 entry->prev = RLB_NULL_INDEX;
781 static int rlb_initialize(struct bonding *bond)
783 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
784 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
785 struct rlb_client_info *new_hashtbl;
786 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
789 spin_lock_init(&(bond_info->rx_hashtbl_lock));
791 new_hashtbl = kmalloc(size, GFP_KERNEL);
793 printk(KERN_ERR DRV_NAME
794 ": %s: Error: Failed to allocate RLB hash table\n",
798 _lock_rx_hashtbl(bond);
800 bond_info->rx_hashtbl = new_hashtbl;
802 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
804 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
805 rlb_init_table_entry(bond_info->rx_hashtbl + i);
808 _unlock_rx_hashtbl(bond);
810 /*initialize packet type*/
811 pk_type->type = __constant_htons(ETH_P_ARP);
812 pk_type->dev = bond->dev;
813 pk_type->func = rlb_arp_recv;
815 /* register to receive ARPs */
816 dev_add_pack(pk_type);
821 static void rlb_deinitialize(struct bonding *bond)
823 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
825 dev_remove_pack(&(bond_info->rlb_pkt_type));
827 _lock_rx_hashtbl(bond);
829 kfree(bond_info->rx_hashtbl);
830 bond_info->rx_hashtbl = NULL;
831 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
833 _unlock_rx_hashtbl(bond);
836 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
838 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
841 _lock_rx_hashtbl(bond);
843 curr_index = bond_info->rx_hashtbl_head;
844 while (curr_index != RLB_NULL_INDEX) {
845 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
846 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
847 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
849 if (curr->tag && (curr->vlan_id == vlan_id)) {
850 if (curr_index == bond_info->rx_hashtbl_head) {
851 bond_info->rx_hashtbl_head = next_index;
853 if (prev_index != RLB_NULL_INDEX) {
854 bond_info->rx_hashtbl[prev_index].next = next_index;
856 if (next_index != RLB_NULL_INDEX) {
857 bond_info->rx_hashtbl[next_index].prev = prev_index;
860 rlb_init_table_entry(curr);
863 curr_index = next_index;
866 _unlock_rx_hashtbl(bond);
869 /*********************** tlb/rlb shared functions *********************/
871 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
873 struct bonding *bond = bond_get_bond_by_slave(slave);
874 struct learning_pkt pkt;
875 int size = sizeof(struct learning_pkt);
878 memset(&pkt, 0, size);
879 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
880 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
881 pkt.type = __constant_htons(ETH_P_LOOP);
883 for (i = 0; i < MAX_LP_BURST; i++) {
887 skb = dev_alloc_skb(size);
892 data = skb_put(skb, size);
893 memcpy(data, &pkt, size);
896 skb->nh.raw = data + ETH_HLEN;
897 skb->protocol = pkt.type;
898 skb->priority = TC_PRIO_CONTROL;
899 skb->dev = slave->dev;
901 if (!list_empty(&bond->vlan_list)) {
902 struct vlan_entry *vlan;
904 vlan = bond_next_vlan(bond,
905 bond->alb_info.current_alb_vlan);
907 bond->alb_info.current_alb_vlan = vlan;
913 skb = vlan_put_tag(skb, vlan->vlan_id);
915 printk(KERN_ERR DRV_NAME
916 ": %s: Error: failed to insert VLAN tag\n",
926 /* hw is a boolean parameter that determines whether we should try and
927 * set the hw address of the device as well as the hw address of the
930 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
932 struct net_device *dev = slave->dev;
933 struct sockaddr s_addr;
936 memcpy(dev->dev_addr, addr, dev->addr_len);
940 /* for rlb each slave must have a unique hw mac addresses so that */
941 /* each slave will receive packets destined to a different mac */
942 memcpy(s_addr.sa_data, addr, dev->addr_len);
943 s_addr.sa_family = dev->type;
944 if (dev_set_mac_address(dev, &s_addr)) {
945 printk(KERN_ERR DRV_NAME
946 ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
947 "mode requires that the base driver support setting "
948 "the hw address also when the network device's "
949 "interface is open\n",
950 dev->master->name, dev->name);
956 /* Caller must hold bond lock for write or curr_slave_lock for write*/
957 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
959 struct slave *disabled_slave = NULL;
960 u8 tmp_mac_addr[ETH_ALEN];
961 int slaves_state_differ;
963 slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
965 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
966 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
967 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
969 /* fasten the change in the switch */
970 if (SLAVE_IS_OK(slave1)) {
971 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
972 if (bond->alb_info.rlb_enabled) {
973 /* inform the clients that the mac address
976 rlb_req_update_slave_clients(bond, slave1);
979 disabled_slave = slave1;
982 if (SLAVE_IS_OK(slave2)) {
983 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
984 if (bond->alb_info.rlb_enabled) {
985 /* inform the clients that the mac address
988 rlb_req_update_slave_clients(bond, slave2);
991 disabled_slave = slave2;
994 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
995 /* A disabled slave was assigned an active mac addr */
996 rlb_teach_disabled_mac_on_primary(bond,
997 disabled_slave->dev->dev_addr);
1002 * alb_change_hw_addr_on_detach
1003 * @bond: bonding we're working on
1004 * @slave: the slave that was just detached
1006 * We assume that @slave was already detached from the slave list.
1008 * If @slave's permanent hw address is different both from its current
1009 * address and from @bond's address, then somewhere in the bond there's
1010 * a slave that has @slave's permanet address as its current address.
1011 * We'll make sure that that slave no longer uses @slave's permanent address.
1013 * Caller must hold bond lock
1015 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1020 perm_curr_diff = memcmp(slave->perm_hwaddr,
1021 slave->dev->dev_addr,
1023 perm_bond_diff = memcmp(slave->perm_hwaddr,
1024 bond->dev->dev_addr,
1027 if (perm_curr_diff && perm_bond_diff) {
1028 struct slave *tmp_slave;
1031 bond_for_each_slave(bond, tmp_slave, i) {
1032 if (!memcmp(slave->perm_hwaddr,
1033 tmp_slave->dev->dev_addr,
1041 alb_swap_mac_addr(bond, slave, tmp_slave);
1047 * alb_handle_addr_collision_on_attach
1048 * @bond: bonding we're working on
1049 * @slave: the slave that was just attached
1051 * checks uniqueness of slave's mac address and handles the case the
1052 * new slave uses the bonds mac address.
1054 * If the permanent hw address of @slave is @bond's hw address, we need to
1055 * find a different hw address to give @slave, that isn't in use by any other
1056 * slave in the bond. This address must be, of course, one of the premanent
1057 * addresses of the other slaves.
1059 * We go over the slave list, and for each slave there we compare its
1060 * permanent hw address with the current address of all the other slaves.
1061 * If no match was found, then we've found a slave with a permanent address
1062 * that isn't used by any other slave in the bond, so we can assign it to
1065 * assumption: this function is called before @slave is attached to the
1068 * caller must hold the bond lock for write since the mac addresses are compared
1069 * and may be swapped.
1071 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1073 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1074 struct slave *has_bond_addr = bond->curr_active_slave;
1075 int i, j, found = 0;
1077 if (bond->slave_cnt == 0) {
1078 /* this is the first slave */
1082 /* if slave's mac address differs from bond's mac address
1083 * check uniqueness of slave's mac address against the other
1084 * slaves in the bond.
1086 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1087 bond_for_each_slave(bond, tmp_slave1, i) {
1088 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1098 /* Try setting slave mac to bond address and fall-through
1099 to code handling that situation below... */
1100 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1101 bond->alb_info.rlb_enabled);
1104 /* The slave's address is equal to the address of the bond.
1105 * Search for a spare address in the bond for this slave.
1107 free_mac_slave = NULL;
1109 bond_for_each_slave(bond, tmp_slave1, i) {
1111 bond_for_each_slave(bond, tmp_slave2, j) {
1112 if (!memcmp(tmp_slave1->perm_hwaddr,
1113 tmp_slave2->dev->dev_addr,
1121 /* no slave has tmp_slave1's perm addr
1124 free_mac_slave = tmp_slave1;
1128 if (!has_bond_addr) {
1129 if (!memcmp(tmp_slave1->dev->dev_addr,
1130 bond->dev->dev_addr,
1133 has_bond_addr = tmp_slave1;
1138 if (free_mac_slave) {
1139 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1140 bond->alb_info.rlb_enabled);
1142 printk(KERN_WARNING DRV_NAME
1143 ": %s: Warning: the hw address of slave %s is in use by "
1144 "the bond; giving it the hw address of %s\n",
1145 bond->dev->name, slave->dev->name, free_mac_slave->dev->name);
1147 } else if (has_bond_addr) {
1148 printk(KERN_ERR DRV_NAME
1149 ": %s: Error: the hw address of slave %s is in use by the "
1150 "bond; couldn't find a slave with a free hw address to "
1151 "give it (this should not have happened)\n",
1152 bond->dev->name, slave->dev->name);
1160 * alb_set_mac_address
1164 * In TLB mode all slaves are configured to the bond's hw address, but set
1165 * their dev_addr field to different addresses (based on their permanent hw
1168 * For each slave, this function sets the interface to the new address and then
1169 * changes its dev_addr field to its previous value.
1171 * Unwinding assumes bond's mac address has not yet changed.
1173 static int alb_set_mac_address(struct bonding *bond, void *addr)
1176 struct slave *slave, *stop_at;
1177 char tmp_addr[ETH_ALEN];
1181 if (bond->alb_info.rlb_enabled) {
1185 bond_for_each_slave(bond, slave, i) {
1186 if (slave->dev->set_mac_address == NULL) {
1191 /* save net_device's current hw address */
1192 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1194 res = dev_set_mac_address(slave->dev, addr);
1196 /* restore net_device's hw address */
1197 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1207 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1208 sa.sa_family = bond->dev->type;
1210 /* unwind from head to the slave that failed */
1212 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1213 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1214 dev_set_mac_address(slave->dev, &sa);
1215 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1221 /************************ exported alb funcions ************************/
1223 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1227 res = tlb_initialize(bond);
1233 bond->alb_info.rlb_enabled = 1;
1234 /* initialize rlb */
1235 res = rlb_initialize(bond);
1237 tlb_deinitialize(bond);
1241 bond->alb_info.rlb_enabled = 0;
1247 void bond_alb_deinitialize(struct bonding *bond)
1249 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1251 tlb_deinitialize(bond);
1253 if (bond_info->rlb_enabled) {
1254 rlb_deinitialize(bond);
1258 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1260 struct bonding *bond = bond_dev->priv;
1261 struct ethhdr *eth_data;
1262 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1263 struct slave *tx_slave = NULL;
1264 static const u32 ip_bcast = 0xffffffff;
1266 int do_tx_balance = 1;
1268 u8 *hash_start = NULL;
1271 skb->mac.raw = (unsigned char *)skb->data;
1272 eth_data = eth_hdr(skb);
1274 /* make sure that the curr_active_slave and the slaves list do
1275 * not change during tx
1277 read_lock(&bond->lock);
1278 read_lock(&bond->curr_slave_lock);
1280 if (!BOND_IS_OK(bond)) {
1284 switch (ntohs(skb->protocol)) {
1286 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1287 (skb->nh.iph->daddr == ip_bcast) ||
1288 (skb->nh.iph->protocol == IPPROTO_IGMP)) {
1292 hash_start = (char*)&(skb->nh.iph->daddr);
1293 hash_size = sizeof(skb->nh.iph->daddr);
1296 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1301 hash_start = (char*)&(skb->nh.ipv6h->daddr);
1302 hash_size = sizeof(skb->nh.ipv6h->daddr);
1305 if (ipx_hdr(skb)->ipx_checksum !=
1306 __constant_htons(IPX_NO_CHECKSUM)) {
1307 /* something is wrong with this packet */
1312 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1313 /* The only protocol worth balancing in
1314 * this family since it has an "ARP" like
1321 hash_start = (char*)eth_data->h_dest;
1322 hash_size = ETH_ALEN;
1326 if (bond_info->rlb_enabled) {
1327 tx_slave = rlb_arp_xmit(skb, bond);
1335 if (do_tx_balance) {
1336 hash_index = _simple_hash(hash_start, hash_size);
1337 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1341 /* unbalanced or unassigned, send through primary */
1342 tx_slave = bond->curr_active_slave;
1343 bond_info->unbalanced_load += skb->len;
1346 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1347 if (tx_slave != bond->curr_active_slave) {
1348 memcpy(eth_data->h_source,
1349 tx_slave->dev->dev_addr,
1353 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1356 tlb_clear_slave(bond, tx_slave, 0);
1362 /* no suitable interface, frame not sent */
1365 read_unlock(&bond->curr_slave_lock);
1366 read_unlock(&bond->lock);
1370 void bond_alb_monitor(struct bonding *bond)
1372 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1373 struct slave *slave;
1376 read_lock(&bond->lock);
1378 if (bond->kill_timers) {
1382 if (bond->slave_cnt == 0) {
1383 bond_info->tx_rebalance_counter = 0;
1384 bond_info->lp_counter = 0;
1388 bond_info->tx_rebalance_counter++;
1389 bond_info->lp_counter++;
1391 /* send learning packets */
1392 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1393 /* change of curr_active_slave involves swapping of mac addresses.
1394 * in order to avoid this swapping from happening while
1395 * sending the learning packets, the curr_slave_lock must be held for
1398 read_lock(&bond->curr_slave_lock);
1400 bond_for_each_slave(bond, slave, i) {
1401 alb_send_learning_packets(slave, slave->dev->dev_addr);
1404 read_unlock(&bond->curr_slave_lock);
1406 bond_info->lp_counter = 0;
1409 /* rebalance tx traffic */
1410 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1412 read_lock(&bond->curr_slave_lock);
1414 bond_for_each_slave(bond, slave, i) {
1415 tlb_clear_slave(bond, slave, 1);
1416 if (slave == bond->curr_active_slave) {
1417 SLAVE_TLB_INFO(slave).load =
1418 bond_info->unbalanced_load /
1419 BOND_TLB_REBALANCE_INTERVAL;
1420 bond_info->unbalanced_load = 0;
1424 read_unlock(&bond->curr_slave_lock);
1426 bond_info->tx_rebalance_counter = 0;
1429 /* handle rlb stuff */
1430 if (bond_info->rlb_enabled) {
1431 /* the following code changes the promiscuity of the
1432 * the curr_active_slave. It needs to be locked with a
1433 * write lock to protect from other code that also
1434 * sets the promiscuity.
1436 write_lock_bh(&bond->curr_slave_lock);
1438 if (bond_info->primary_is_promisc &&
1439 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1441 bond_info->rlb_promisc_timeout_counter = 0;
1443 /* If the primary was set to promiscuous mode
1444 * because a slave was disabled then
1445 * it can now leave promiscuous mode.
1447 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1448 bond_info->primary_is_promisc = 0;
1451 write_unlock_bh(&bond->curr_slave_lock);
1453 if (bond_info->rlb_rebalance) {
1454 bond_info->rlb_rebalance = 0;
1455 rlb_rebalance(bond);
1458 /* check if clients need updating */
1459 if (bond_info->rx_ntt) {
1460 if (bond_info->rlb_update_delay_counter) {
1461 --bond_info->rlb_update_delay_counter;
1463 rlb_update_rx_clients(bond);
1464 if (bond_info->rlb_update_retry_counter) {
1465 --bond_info->rlb_update_retry_counter;
1467 bond_info->rx_ntt = 0;
1474 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1476 read_unlock(&bond->lock);
1479 /* assumption: called before the slave is attached to the bond
1480 * and not locked by the bond lock
1482 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1486 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1487 bond->alb_info.rlb_enabled);
1492 /* caller must hold the bond lock for write since the mac addresses
1493 * are compared and may be swapped.
1495 write_lock_bh(&bond->lock);
1497 res = alb_handle_addr_collision_on_attach(bond, slave);
1499 write_unlock_bh(&bond->lock);
1505 tlb_init_slave(slave);
1507 /* order a rebalance ASAP */
1508 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1510 if (bond->alb_info.rlb_enabled) {
1511 bond->alb_info.rlb_rebalance = 1;
1517 /* Caller must hold bond lock for write */
1518 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1520 if (bond->slave_cnt > 1) {
1521 alb_change_hw_addr_on_detach(bond, slave);
1524 tlb_clear_slave(bond, slave, 0);
1526 if (bond->alb_info.rlb_enabled) {
1527 bond->alb_info.next_rx_slave = NULL;
1528 rlb_clear_slave(bond, slave);
1532 /* Caller must hold bond lock for read */
1533 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1535 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1537 if (link == BOND_LINK_DOWN) {
1538 tlb_clear_slave(bond, slave, 0);
1539 if (bond->alb_info.rlb_enabled) {
1540 rlb_clear_slave(bond, slave);
1542 } else if (link == BOND_LINK_UP) {
1543 /* order a rebalance ASAP */
1544 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1545 if (bond->alb_info.rlb_enabled) {
1546 bond->alb_info.rlb_rebalance = 1;
1547 /* If the updelay module parameter is smaller than the
1548 * forwarding delay of the switch the rebalance will
1549 * not work because the rebalance arp replies will
1550 * not be forwarded to the clients..
1557 * bond_alb_handle_active_change - assign new curr_active_slave
1558 * @bond: our bonding struct
1559 * @new_slave: new slave to assign
1561 * Set the bond->curr_active_slave to @new_slave and handle
1562 * mac address swapping and promiscuity changes as needed.
1564 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1566 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1568 struct slave *swap_slave;
1571 if (bond->curr_active_slave == new_slave) {
1575 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1576 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1577 bond->alb_info.primary_is_promisc = 0;
1578 bond->alb_info.rlb_promisc_timeout_counter = 0;
1581 swap_slave = bond->curr_active_slave;
1582 bond->curr_active_slave = new_slave;
1584 if (!new_slave || (bond->slave_cnt == 0)) {
1588 /* set the new curr_active_slave to the bonds mac address
1589 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1592 struct slave *tmp_slave;
1593 /* find slave that is holding the bond's mac address */
1594 bond_for_each_slave(bond, tmp_slave, i) {
1595 if (!memcmp(tmp_slave->dev->dev_addr,
1596 bond->dev->dev_addr, ETH_ALEN)) {
1597 swap_slave = tmp_slave;
1603 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1605 /* swap mac address */
1606 alb_swap_mac_addr(bond, swap_slave, new_slave);
1608 /* set the new_slave to the bond mac address */
1609 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1610 bond->alb_info.rlb_enabled);
1611 /* fasten bond mac on new current slave */
1612 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1616 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1618 struct bonding *bond = bond_dev->priv;
1619 struct sockaddr *sa = addr;
1620 struct slave *slave, *swap_slave;
1624 if (!is_valid_ether_addr(sa->sa_data)) {
1625 return -EADDRNOTAVAIL;
1628 res = alb_set_mac_address(bond, addr);
1633 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1635 /* If there is no curr_active_slave there is nothing else to do.
1636 * Otherwise we'll need to pass the new address to it and handle
1639 if (!bond->curr_active_slave) {
1645 bond_for_each_slave(bond, slave, i) {
1646 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1653 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1655 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1656 bond->alb_info.rlb_enabled);
1658 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1659 if (bond->alb_info.rlb_enabled) {
1660 /* inform clients mac address has changed */
1661 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1668 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1670 if (bond->alb_info.current_alb_vlan &&
1671 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1672 bond->alb_info.current_alb_vlan = NULL;
1675 if (bond->alb_info.rlb_enabled) {
1676 rlb_clear_vlan(bond, vlan_id);