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 static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
109 return (struct arp_pkt *)skb_network_header(skb);
112 /* Forward declaration */
113 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
115 static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
120 for (i = 0; i < hash_size; i++) {
121 hash ^= hash_start[i];
127 /*********************** tlb specific functions ***************************/
129 static inline void _lock_tx_hashtbl(struct bonding *bond)
131 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
134 static inline void _unlock_tx_hashtbl(struct bonding *bond)
136 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
139 /* Caller must hold tx_hashtbl lock */
140 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
143 entry->load_history = 1 + entry->tx_bytes /
144 BOND_TLB_REBALANCE_INTERVAL;
148 entry->tx_slave = NULL;
149 entry->next = TLB_NULL_INDEX;
150 entry->prev = TLB_NULL_INDEX;
153 static inline void tlb_init_slave(struct slave *slave)
155 SLAVE_TLB_INFO(slave).load = 0;
156 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
159 /* Caller must hold bond lock for read */
160 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
162 struct tlb_client_info *tx_hash_table;
165 _lock_tx_hashtbl(bond);
167 /* clear slave from tx_hashtbl */
168 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
170 index = SLAVE_TLB_INFO(slave).head;
171 while (index != TLB_NULL_INDEX) {
172 u32 next_index = tx_hash_table[index].next;
173 tlb_init_table_entry(&tx_hash_table[index], save_load);
177 tlb_init_slave(slave);
179 _unlock_tx_hashtbl(bond);
182 /* Must be called before starting the monitor timer */
183 static int tlb_initialize(struct bonding *bond)
185 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
186 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
187 struct tlb_client_info *new_hashtbl;
190 spin_lock_init(&(bond_info->tx_hashtbl_lock));
192 new_hashtbl = kzalloc(size, GFP_KERNEL);
194 printk(KERN_ERR DRV_NAME
195 ": %s: Error: Failed to allocate TLB hash table\n",
199 _lock_tx_hashtbl(bond);
201 bond_info->tx_hashtbl = new_hashtbl;
203 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
204 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
207 _unlock_tx_hashtbl(bond);
212 /* Must be called only after all slaves have been released */
213 static void tlb_deinitialize(struct bonding *bond)
215 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
217 _lock_tx_hashtbl(bond);
219 kfree(bond_info->tx_hashtbl);
220 bond_info->tx_hashtbl = NULL;
222 _unlock_tx_hashtbl(bond);
225 /* Caller must hold bond lock for read */
226 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
228 struct slave *slave, *least_loaded;
232 /* Find the first enabled slave */
233 bond_for_each_slave(bond, slave, i) {
234 if (SLAVE_IS_OK(slave)) {
244 least_loaded = slave;
245 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
246 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
248 /* Find the slave with the largest gap */
249 bond_for_each_slave_from(bond, slave, i, least_loaded) {
250 if (SLAVE_IS_OK(slave)) {
251 s64 gap = (s64)(slave->speed << 20) -
252 (s64)(SLAVE_TLB_INFO(slave).load << 3);
254 least_loaded = slave;
263 /* Caller must hold bond lock for read */
264 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
266 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
267 struct tlb_client_info *hash_table;
268 struct slave *assigned_slave;
270 _lock_tx_hashtbl(bond);
272 hash_table = bond_info->tx_hashtbl;
273 assigned_slave = hash_table[hash_index].tx_slave;
274 if (!assigned_slave) {
275 assigned_slave = tlb_get_least_loaded_slave(bond);
277 if (assigned_slave) {
278 struct tlb_slave_info *slave_info =
279 &(SLAVE_TLB_INFO(assigned_slave));
280 u32 next_index = slave_info->head;
282 hash_table[hash_index].tx_slave = assigned_slave;
283 hash_table[hash_index].next = next_index;
284 hash_table[hash_index].prev = TLB_NULL_INDEX;
286 if (next_index != TLB_NULL_INDEX) {
287 hash_table[next_index].prev = hash_index;
290 slave_info->head = hash_index;
292 hash_table[hash_index].load_history;
296 if (assigned_slave) {
297 hash_table[hash_index].tx_bytes += skb_len;
300 _unlock_tx_hashtbl(bond);
302 return assigned_slave;
305 /*********************** rlb specific functions ***************************/
306 static inline void _lock_rx_hashtbl(struct bonding *bond)
308 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
311 static inline void _unlock_rx_hashtbl(struct bonding *bond)
313 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
316 /* when an ARP REPLY is received from a client update its info
319 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
321 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
322 struct rlb_client_info *client_info;
325 _lock_rx_hashtbl(bond);
327 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
328 client_info = &(bond_info->rx_hashtbl[hash_index]);
330 if ((client_info->assigned) &&
331 (client_info->ip_src == arp->ip_dst) &&
332 (client_info->ip_dst == arp->ip_src)) {
333 /* update the clients MAC address */
334 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
335 client_info->ntt = 1;
336 bond_info->rx_ntt = 1;
339 _unlock_rx_hashtbl(bond);
342 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
344 struct bonding *bond = bond_dev->priv;
345 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
346 int res = NET_RX_DROP;
348 if (!(bond_dev->flags & IFF_MASTER))
352 dprintk("Packet has no ARP data\n");
356 if (skb->len < sizeof(struct arp_pkt)) {
357 dprintk("Packet is too small to be an ARP\n");
361 if (arp->op_code == htons(ARPOP_REPLY)) {
362 /* update rx hash table for this ARP */
363 rlb_update_entry_from_arp(bond, arp);
364 dprintk("Server received an ARP Reply from client\n");
367 res = NET_RX_SUCCESS;
375 /* Caller must hold bond lock for read */
376 static struct slave *rlb_next_rx_slave(struct bonding *bond)
378 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
379 struct slave *rx_slave, *slave, *start_at;
382 if (bond_info->next_rx_slave) {
383 start_at = bond_info->next_rx_slave;
385 start_at = bond->first_slave;
390 bond_for_each_slave_from(bond, slave, i, start_at) {
391 if (SLAVE_IS_OK(slave)) {
394 } else if (slave->speed > rx_slave->speed) {
401 bond_info->next_rx_slave = rx_slave->next;
407 /* teach the switch the mac of a disabled slave
408 * on the primary for fault tolerance
410 * Caller must hold bond->curr_slave_lock for write or bond lock for write
412 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
414 if (!bond->curr_active_slave) {
418 if (!bond->alb_info.primary_is_promisc) {
419 bond->alb_info.primary_is_promisc = 1;
420 dev_set_promiscuity(bond->curr_active_slave->dev, 1);
423 bond->alb_info.rlb_promisc_timeout_counter = 0;
425 alb_send_learning_packets(bond->curr_active_slave, addr);
428 /* slave being removed should not be active at this point
430 * Caller must hold bond lock for read
432 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
434 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
435 struct rlb_client_info *rx_hash_table;
436 u32 index, next_index;
438 /* clear slave from rx_hashtbl */
439 _lock_rx_hashtbl(bond);
441 rx_hash_table = bond_info->rx_hashtbl;
442 index = bond_info->rx_hashtbl_head;
443 for (; index != RLB_NULL_INDEX; index = next_index) {
444 next_index = rx_hash_table[index].next;
445 if (rx_hash_table[index].slave == slave) {
446 struct slave *assigned_slave = rlb_next_rx_slave(bond);
448 if (assigned_slave) {
449 rx_hash_table[index].slave = assigned_slave;
450 if (memcmp(rx_hash_table[index].mac_dst,
451 mac_bcast, ETH_ALEN)) {
452 bond_info->rx_hashtbl[index].ntt = 1;
453 bond_info->rx_ntt = 1;
454 /* A slave has been removed from the
455 * table because it is either disabled
456 * or being released. We must retry the
457 * update to avoid clients from not
458 * being updated & disconnecting when
461 bond_info->rlb_update_retry_counter =
464 } else { /* there is no active slave */
465 rx_hash_table[index].slave = NULL;
470 _unlock_rx_hashtbl(bond);
472 write_lock(&bond->curr_slave_lock);
474 if (slave != bond->curr_active_slave) {
475 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
478 write_unlock(&bond->curr_slave_lock);
481 static void rlb_update_client(struct rlb_client_info *client_info)
485 if (!client_info->slave) {
489 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
492 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
494 client_info->slave->dev,
496 client_info->mac_dst,
497 client_info->slave->dev->dev_addr,
498 client_info->mac_dst);
500 printk(KERN_ERR DRV_NAME
501 ": %s: Error: failed to create an ARP packet\n",
502 client_info->slave->dev->master->name);
506 skb->dev = client_info->slave->dev;
508 if (client_info->tag) {
509 skb = vlan_put_tag(skb, client_info->vlan_id);
511 printk(KERN_ERR DRV_NAME
512 ": %s: Error: failed to insert VLAN tag\n",
513 client_info->slave->dev->master->name);
522 /* sends ARP REPLIES that update the clients that need updating */
523 static void rlb_update_rx_clients(struct bonding *bond)
525 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
526 struct rlb_client_info *client_info;
529 _lock_rx_hashtbl(bond);
531 hash_index = bond_info->rx_hashtbl_head;
532 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
533 client_info = &(bond_info->rx_hashtbl[hash_index]);
534 if (client_info->ntt) {
535 rlb_update_client(client_info);
536 if (bond_info->rlb_update_retry_counter == 0) {
537 client_info->ntt = 0;
542 /* do not update the entries again untill this counter is zero so that
543 * not to confuse the clients.
545 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
547 _unlock_rx_hashtbl(bond);
550 /* The slave was assigned a new mac address - update the clients */
551 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
553 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
554 struct rlb_client_info *client_info;
558 _lock_rx_hashtbl(bond);
560 hash_index = bond_info->rx_hashtbl_head;
561 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
562 client_info = &(bond_info->rx_hashtbl[hash_index]);
564 if ((client_info->slave == slave) &&
565 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
566 client_info->ntt = 1;
571 // update the team's flag only after the whole iteration
573 bond_info->rx_ntt = 1;
575 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
578 _unlock_rx_hashtbl(bond);
581 /* mark all clients using src_ip to be updated */
582 static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
584 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
585 struct rlb_client_info *client_info;
588 _lock_rx_hashtbl(bond);
590 hash_index = bond_info->rx_hashtbl_head;
591 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
592 client_info = &(bond_info->rx_hashtbl[hash_index]);
594 if (!client_info->slave) {
595 printk(KERN_ERR DRV_NAME
596 ": %s: Error: found a client with no channel in "
597 "the client's hash table\n",
601 /*update all clients using this src_ip, that are not assigned
602 * to the team's address (curr_active_slave) and have a known
603 * unicast mac address.
605 if ((client_info->ip_src == src_ip) &&
606 memcmp(client_info->slave->dev->dev_addr,
607 bond->dev->dev_addr, ETH_ALEN) &&
608 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
609 client_info->ntt = 1;
610 bond_info->rx_ntt = 1;
614 _unlock_rx_hashtbl(bond);
617 /* Caller must hold both bond and ptr locks for read */
618 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
620 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
621 struct arp_pkt *arp = arp_pkt(skb);
622 struct slave *assigned_slave;
623 struct rlb_client_info *client_info;
626 _lock_rx_hashtbl(bond);
628 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
629 client_info = &(bond_info->rx_hashtbl[hash_index]);
631 if (client_info->assigned) {
632 if ((client_info->ip_src == arp->ip_src) &&
633 (client_info->ip_dst == arp->ip_dst)) {
634 /* the entry is already assigned to this client */
635 if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
636 /* update mac address from arp */
637 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
640 assigned_slave = client_info->slave;
641 if (assigned_slave) {
642 _unlock_rx_hashtbl(bond);
643 return assigned_slave;
646 /* the entry is already assigned to some other client,
647 * move the old client to primary (curr_active_slave) so
648 * that the new client can be assigned to this entry.
650 if (bond->curr_active_slave &&
651 client_info->slave != bond->curr_active_slave) {
652 client_info->slave = bond->curr_active_slave;
653 rlb_update_client(client_info);
657 /* assign a new slave */
658 assigned_slave = rlb_next_rx_slave(bond);
660 if (assigned_slave) {
661 client_info->ip_src = arp->ip_src;
662 client_info->ip_dst = arp->ip_dst;
663 /* arp->mac_dst is broadcast for arp reqeusts.
664 * will be updated with clients actual unicast mac address
665 * upon receiving an arp reply.
667 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
668 client_info->slave = assigned_slave;
670 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
671 client_info->ntt = 1;
672 bond->alb_info.rx_ntt = 1;
674 client_info->ntt = 0;
677 if (!list_empty(&bond->vlan_list)) {
678 unsigned short vlan_id;
679 int res = vlan_get_tag(skb, &vlan_id);
681 client_info->tag = 1;
682 client_info->vlan_id = vlan_id;
686 if (!client_info->assigned) {
687 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
688 bond_info->rx_hashtbl_head = hash_index;
689 client_info->next = prev_tbl_head;
690 if (prev_tbl_head != RLB_NULL_INDEX) {
691 bond_info->rx_hashtbl[prev_tbl_head].prev =
694 client_info->assigned = 1;
698 _unlock_rx_hashtbl(bond);
700 return assigned_slave;
703 /* chooses (and returns) transmit channel for arp reply
704 * does not choose channel for other arp types since they are
705 * sent on the curr_active_slave
707 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
709 struct arp_pkt *arp = arp_pkt(skb);
710 struct slave *tx_slave = NULL;
712 if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
713 /* the arp must be sent on the selected
716 tx_slave = rlb_choose_channel(skb, bond);
718 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
720 dprintk("Server sent ARP Reply packet\n");
721 } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
722 /* Create an entry in the rx_hashtbl for this client as a
724 * When the arp reply is received the entry will be updated
725 * with the correct unicast address of the client.
727 rlb_choose_channel(skb, bond);
729 /* The ARP relpy packets must be delayed so that
730 * they can cancel out the influence of the ARP request.
732 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
734 /* arp requests are broadcast and are sent on the primary
735 * the arp request will collapse all clients on the subnet to
736 * the primary slave. We must register these clients to be
737 * updated with their assigned mac.
739 rlb_req_update_subnet_clients(bond, arp->ip_src);
740 dprintk("Server sent ARP Request packet\n");
746 /* Caller must hold bond lock for read */
747 static void rlb_rebalance(struct bonding *bond)
749 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
750 struct slave *assigned_slave;
751 struct rlb_client_info *client_info;
755 _lock_rx_hashtbl(bond);
758 hash_index = bond_info->rx_hashtbl_head;
759 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
760 client_info = &(bond_info->rx_hashtbl[hash_index]);
761 assigned_slave = rlb_next_rx_slave(bond);
762 if (assigned_slave && (client_info->slave != assigned_slave)) {
763 client_info->slave = assigned_slave;
764 client_info->ntt = 1;
769 /* update the team's flag only after the whole iteration */
771 bond_info->rx_ntt = 1;
773 _unlock_rx_hashtbl(bond);
776 /* Caller must hold rx_hashtbl lock */
777 static void rlb_init_table_entry(struct rlb_client_info *entry)
779 memset(entry, 0, sizeof(struct rlb_client_info));
780 entry->next = RLB_NULL_INDEX;
781 entry->prev = RLB_NULL_INDEX;
784 static int rlb_initialize(struct bonding *bond)
786 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
787 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
788 struct rlb_client_info *new_hashtbl;
789 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
792 spin_lock_init(&(bond_info->rx_hashtbl_lock));
794 new_hashtbl = kmalloc(size, GFP_KERNEL);
796 printk(KERN_ERR DRV_NAME
797 ": %s: Error: Failed to allocate RLB hash table\n",
801 _lock_rx_hashtbl(bond);
803 bond_info->rx_hashtbl = new_hashtbl;
805 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
807 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
808 rlb_init_table_entry(bond_info->rx_hashtbl + i);
811 _unlock_rx_hashtbl(bond);
813 /*initialize packet type*/
814 pk_type->type = __constant_htons(ETH_P_ARP);
815 pk_type->dev = bond->dev;
816 pk_type->func = rlb_arp_recv;
818 /* register to receive ARPs */
819 dev_add_pack(pk_type);
824 static void rlb_deinitialize(struct bonding *bond)
826 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
828 dev_remove_pack(&(bond_info->rlb_pkt_type));
830 _lock_rx_hashtbl(bond);
832 kfree(bond_info->rx_hashtbl);
833 bond_info->rx_hashtbl = NULL;
834 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
836 _unlock_rx_hashtbl(bond);
839 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
841 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
844 _lock_rx_hashtbl(bond);
846 curr_index = bond_info->rx_hashtbl_head;
847 while (curr_index != RLB_NULL_INDEX) {
848 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
849 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
850 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
852 if (curr->tag && (curr->vlan_id == vlan_id)) {
853 if (curr_index == bond_info->rx_hashtbl_head) {
854 bond_info->rx_hashtbl_head = next_index;
856 if (prev_index != RLB_NULL_INDEX) {
857 bond_info->rx_hashtbl[prev_index].next = next_index;
859 if (next_index != RLB_NULL_INDEX) {
860 bond_info->rx_hashtbl[next_index].prev = prev_index;
863 rlb_init_table_entry(curr);
866 curr_index = next_index;
869 _unlock_rx_hashtbl(bond);
872 /*********************** tlb/rlb shared functions *********************/
874 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
876 struct bonding *bond = bond_get_bond_by_slave(slave);
877 struct learning_pkt pkt;
878 int size = sizeof(struct learning_pkt);
881 memset(&pkt, 0, size);
882 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
883 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
884 pkt.type = __constant_htons(ETH_P_LOOP);
886 for (i = 0; i < MAX_LP_BURST; i++) {
890 skb = dev_alloc_skb(size);
895 data = skb_put(skb, size);
896 memcpy(data, &pkt, size);
898 skb_reset_mac_header(skb);
899 skb->network_header = skb->mac_header + ETH_HLEN;
900 skb->protocol = pkt.type;
901 skb->priority = TC_PRIO_CONTROL;
902 skb->dev = slave->dev;
904 if (!list_empty(&bond->vlan_list)) {
905 struct vlan_entry *vlan;
907 vlan = bond_next_vlan(bond,
908 bond->alb_info.current_alb_vlan);
910 bond->alb_info.current_alb_vlan = vlan;
916 skb = vlan_put_tag(skb, vlan->vlan_id);
918 printk(KERN_ERR DRV_NAME
919 ": %s: Error: failed to insert VLAN tag\n",
929 /* hw is a boolean parameter that determines whether we should try and
930 * set the hw address of the device as well as the hw address of the
933 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
935 struct net_device *dev = slave->dev;
936 struct sockaddr s_addr;
939 memcpy(dev->dev_addr, addr, dev->addr_len);
943 /* for rlb each slave must have a unique hw mac addresses so that */
944 /* each slave will receive packets destined to a different mac */
945 memcpy(s_addr.sa_data, addr, dev->addr_len);
946 s_addr.sa_family = dev->type;
947 if (dev_set_mac_address(dev, &s_addr)) {
948 printk(KERN_ERR DRV_NAME
949 ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
950 "mode requires that the base driver support setting "
951 "the hw address also when the network device's "
952 "interface is open\n",
953 dev->master->name, dev->name);
959 /* Caller must hold bond lock for write or curr_slave_lock for write*/
960 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
962 struct slave *disabled_slave = NULL;
963 u8 tmp_mac_addr[ETH_ALEN];
964 int slaves_state_differ;
966 slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
968 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
969 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
970 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
972 /* fasten the change in the switch */
973 if (SLAVE_IS_OK(slave1)) {
974 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
975 if (bond->alb_info.rlb_enabled) {
976 /* inform the clients that the mac address
979 rlb_req_update_slave_clients(bond, slave1);
982 disabled_slave = slave1;
985 if (SLAVE_IS_OK(slave2)) {
986 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
987 if (bond->alb_info.rlb_enabled) {
988 /* inform the clients that the mac address
991 rlb_req_update_slave_clients(bond, slave2);
994 disabled_slave = slave2;
997 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
998 /* A disabled slave was assigned an active mac addr */
999 rlb_teach_disabled_mac_on_primary(bond,
1000 disabled_slave->dev->dev_addr);
1005 * alb_change_hw_addr_on_detach
1006 * @bond: bonding we're working on
1007 * @slave: the slave that was just detached
1009 * We assume that @slave was already detached from the slave list.
1011 * If @slave's permanent hw address is different both from its current
1012 * address and from @bond's address, then somewhere in the bond there's
1013 * a slave that has @slave's permanet address as its current address.
1014 * We'll make sure that that slave no longer uses @slave's permanent address.
1016 * Caller must hold bond lock
1018 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1023 perm_curr_diff = memcmp(slave->perm_hwaddr,
1024 slave->dev->dev_addr,
1026 perm_bond_diff = memcmp(slave->perm_hwaddr,
1027 bond->dev->dev_addr,
1030 if (perm_curr_diff && perm_bond_diff) {
1031 struct slave *tmp_slave;
1034 bond_for_each_slave(bond, tmp_slave, i) {
1035 if (!memcmp(slave->perm_hwaddr,
1036 tmp_slave->dev->dev_addr,
1044 alb_swap_mac_addr(bond, slave, tmp_slave);
1050 * alb_handle_addr_collision_on_attach
1051 * @bond: bonding we're working on
1052 * @slave: the slave that was just attached
1054 * checks uniqueness of slave's mac address and handles the case the
1055 * new slave uses the bonds mac address.
1057 * If the permanent hw address of @slave is @bond's hw address, we need to
1058 * find a different hw address to give @slave, that isn't in use by any other
1059 * slave in the bond. This address must be, of course, one of the premanent
1060 * addresses of the other slaves.
1062 * We go over the slave list, and for each slave there we compare its
1063 * permanent hw address with the current address of all the other slaves.
1064 * If no match was found, then we've found a slave with a permanent address
1065 * that isn't used by any other slave in the bond, so we can assign it to
1068 * assumption: this function is called before @slave is attached to the
1071 * caller must hold the bond lock for write since the mac addresses are compared
1072 * and may be swapped.
1074 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1076 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1077 struct slave *has_bond_addr = bond->curr_active_slave;
1078 int i, j, found = 0;
1080 if (bond->slave_cnt == 0) {
1081 /* this is the first slave */
1085 /* if slave's mac address differs from bond's mac address
1086 * check uniqueness of slave's mac address against the other
1087 * slaves in the bond.
1089 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1090 bond_for_each_slave(bond, tmp_slave1, i) {
1091 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1101 /* Try setting slave mac to bond address and fall-through
1102 to code handling that situation below... */
1103 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1104 bond->alb_info.rlb_enabled);
1107 /* The slave's address is equal to the address of the bond.
1108 * Search for a spare address in the bond for this slave.
1110 free_mac_slave = NULL;
1112 bond_for_each_slave(bond, tmp_slave1, i) {
1114 bond_for_each_slave(bond, tmp_slave2, j) {
1115 if (!memcmp(tmp_slave1->perm_hwaddr,
1116 tmp_slave2->dev->dev_addr,
1124 /* no slave has tmp_slave1's perm addr
1127 free_mac_slave = tmp_slave1;
1131 if (!has_bond_addr) {
1132 if (!memcmp(tmp_slave1->dev->dev_addr,
1133 bond->dev->dev_addr,
1136 has_bond_addr = tmp_slave1;
1141 if (free_mac_slave) {
1142 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1143 bond->alb_info.rlb_enabled);
1145 printk(KERN_WARNING DRV_NAME
1146 ": %s: Warning: the hw address of slave %s is in use by "
1147 "the bond; giving it the hw address of %s\n",
1148 bond->dev->name, slave->dev->name, free_mac_slave->dev->name);
1150 } else if (has_bond_addr) {
1151 printk(KERN_ERR DRV_NAME
1152 ": %s: Error: the hw address of slave %s is in use by the "
1153 "bond; couldn't find a slave with a free hw address to "
1154 "give it (this should not have happened)\n",
1155 bond->dev->name, slave->dev->name);
1163 * alb_set_mac_address
1167 * In TLB mode all slaves are configured to the bond's hw address, but set
1168 * their dev_addr field to different addresses (based on their permanent hw
1171 * For each slave, this function sets the interface to the new address and then
1172 * changes its dev_addr field to its previous value.
1174 * Unwinding assumes bond's mac address has not yet changed.
1176 static int alb_set_mac_address(struct bonding *bond, void *addr)
1179 struct slave *slave, *stop_at;
1180 char tmp_addr[ETH_ALEN];
1184 if (bond->alb_info.rlb_enabled) {
1188 bond_for_each_slave(bond, slave, i) {
1189 if (slave->dev->set_mac_address == NULL) {
1194 /* save net_device's current hw address */
1195 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1197 res = dev_set_mac_address(slave->dev, addr);
1199 /* restore net_device's hw address */
1200 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1210 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1211 sa.sa_family = bond->dev->type;
1213 /* unwind from head to the slave that failed */
1215 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1216 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1217 dev_set_mac_address(slave->dev, &sa);
1218 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1224 /************************ exported alb funcions ************************/
1226 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1230 res = tlb_initialize(bond);
1236 bond->alb_info.rlb_enabled = 1;
1237 /* initialize rlb */
1238 res = rlb_initialize(bond);
1240 tlb_deinitialize(bond);
1244 bond->alb_info.rlb_enabled = 0;
1250 void bond_alb_deinitialize(struct bonding *bond)
1252 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1254 tlb_deinitialize(bond);
1256 if (bond_info->rlb_enabled) {
1257 rlb_deinitialize(bond);
1261 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1263 struct bonding *bond = bond_dev->priv;
1264 struct ethhdr *eth_data;
1265 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1266 struct slave *tx_slave = NULL;
1267 static const u32 ip_bcast = 0xffffffff;
1269 int do_tx_balance = 1;
1271 const u8 *hash_start = NULL;
1274 skb_reset_mac_header(skb);
1275 eth_data = eth_hdr(skb);
1277 /* make sure that the curr_active_slave and the slaves list do
1278 * not change during tx
1280 read_lock(&bond->lock);
1281 read_lock(&bond->curr_slave_lock);
1283 if (!BOND_IS_OK(bond)) {
1287 switch (ntohs(skb->protocol)) {
1289 const struct iphdr *iph = ip_hdr(skb);
1291 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1292 (iph->daddr == ip_bcast) ||
1293 (iph->protocol == IPPROTO_IGMP)) {
1297 hash_start = (char *)&(iph->daddr);
1298 hash_size = sizeof(iph->daddr);
1302 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1307 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1308 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1311 if (ipx_hdr(skb)->ipx_checksum !=
1312 __constant_htons(IPX_NO_CHECKSUM)) {
1313 /* something is wrong with this packet */
1318 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1319 /* The only protocol worth balancing in
1320 * this family since it has an "ARP" like
1327 hash_start = (char*)eth_data->h_dest;
1328 hash_size = ETH_ALEN;
1332 if (bond_info->rlb_enabled) {
1333 tx_slave = rlb_arp_xmit(skb, bond);
1341 if (do_tx_balance) {
1342 hash_index = _simple_hash(hash_start, hash_size);
1343 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1347 /* unbalanced or unassigned, send through primary */
1348 tx_slave = bond->curr_active_slave;
1349 bond_info->unbalanced_load += skb->len;
1352 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1353 if (tx_slave != bond->curr_active_slave) {
1354 memcpy(eth_data->h_source,
1355 tx_slave->dev->dev_addr,
1359 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1362 tlb_clear_slave(bond, tx_slave, 0);
1368 /* no suitable interface, frame not sent */
1371 read_unlock(&bond->curr_slave_lock);
1372 read_unlock(&bond->lock);
1376 void bond_alb_monitor(struct bonding *bond)
1378 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1379 struct slave *slave;
1382 read_lock(&bond->lock);
1384 if (bond->kill_timers) {
1388 if (bond->slave_cnt == 0) {
1389 bond_info->tx_rebalance_counter = 0;
1390 bond_info->lp_counter = 0;
1394 bond_info->tx_rebalance_counter++;
1395 bond_info->lp_counter++;
1397 /* send learning packets */
1398 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1399 /* change of curr_active_slave involves swapping of mac addresses.
1400 * in order to avoid this swapping from happening while
1401 * sending the learning packets, the curr_slave_lock must be held for
1404 read_lock(&bond->curr_slave_lock);
1406 bond_for_each_slave(bond, slave, i) {
1407 alb_send_learning_packets(slave, slave->dev->dev_addr);
1410 read_unlock(&bond->curr_slave_lock);
1412 bond_info->lp_counter = 0;
1415 /* rebalance tx traffic */
1416 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1418 read_lock(&bond->curr_slave_lock);
1420 bond_for_each_slave(bond, slave, i) {
1421 tlb_clear_slave(bond, slave, 1);
1422 if (slave == bond->curr_active_slave) {
1423 SLAVE_TLB_INFO(slave).load =
1424 bond_info->unbalanced_load /
1425 BOND_TLB_REBALANCE_INTERVAL;
1426 bond_info->unbalanced_load = 0;
1430 read_unlock(&bond->curr_slave_lock);
1432 bond_info->tx_rebalance_counter = 0;
1435 /* handle rlb stuff */
1436 if (bond_info->rlb_enabled) {
1437 /* the following code changes the promiscuity of the
1438 * the curr_active_slave. It needs to be locked with a
1439 * write lock to protect from other code that also
1440 * sets the promiscuity.
1442 write_lock_bh(&bond->curr_slave_lock);
1444 if (bond_info->primary_is_promisc &&
1445 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1447 bond_info->rlb_promisc_timeout_counter = 0;
1449 /* If the primary was set to promiscuous mode
1450 * because a slave was disabled then
1451 * it can now leave promiscuous mode.
1453 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1454 bond_info->primary_is_promisc = 0;
1457 write_unlock_bh(&bond->curr_slave_lock);
1459 if (bond_info->rlb_rebalance) {
1460 bond_info->rlb_rebalance = 0;
1461 rlb_rebalance(bond);
1464 /* check if clients need updating */
1465 if (bond_info->rx_ntt) {
1466 if (bond_info->rlb_update_delay_counter) {
1467 --bond_info->rlb_update_delay_counter;
1469 rlb_update_rx_clients(bond);
1470 if (bond_info->rlb_update_retry_counter) {
1471 --bond_info->rlb_update_retry_counter;
1473 bond_info->rx_ntt = 0;
1480 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1482 read_unlock(&bond->lock);
1485 /* assumption: called before the slave is attached to the bond
1486 * and not locked by the bond lock
1488 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1492 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1493 bond->alb_info.rlb_enabled);
1498 /* caller must hold the bond lock for write since the mac addresses
1499 * are compared and may be swapped.
1501 write_lock_bh(&bond->lock);
1503 res = alb_handle_addr_collision_on_attach(bond, slave);
1505 write_unlock_bh(&bond->lock);
1511 tlb_init_slave(slave);
1513 /* order a rebalance ASAP */
1514 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1516 if (bond->alb_info.rlb_enabled) {
1517 bond->alb_info.rlb_rebalance = 1;
1523 /* Caller must hold bond lock for write */
1524 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1526 if (bond->slave_cnt > 1) {
1527 alb_change_hw_addr_on_detach(bond, slave);
1530 tlb_clear_slave(bond, slave, 0);
1532 if (bond->alb_info.rlb_enabled) {
1533 bond->alb_info.next_rx_slave = NULL;
1534 rlb_clear_slave(bond, slave);
1538 /* Caller must hold bond lock for read */
1539 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1541 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1543 if (link == BOND_LINK_DOWN) {
1544 tlb_clear_slave(bond, slave, 0);
1545 if (bond->alb_info.rlb_enabled) {
1546 rlb_clear_slave(bond, slave);
1548 } else if (link == BOND_LINK_UP) {
1549 /* order a rebalance ASAP */
1550 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1551 if (bond->alb_info.rlb_enabled) {
1552 bond->alb_info.rlb_rebalance = 1;
1553 /* If the updelay module parameter is smaller than the
1554 * forwarding delay of the switch the rebalance will
1555 * not work because the rebalance arp replies will
1556 * not be forwarded to the clients..
1563 * bond_alb_handle_active_change - assign new curr_active_slave
1564 * @bond: our bonding struct
1565 * @new_slave: new slave to assign
1567 * Set the bond->curr_active_slave to @new_slave and handle
1568 * mac address swapping and promiscuity changes as needed.
1570 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1572 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1574 struct slave *swap_slave;
1577 if (bond->curr_active_slave == new_slave) {
1581 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1582 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1583 bond->alb_info.primary_is_promisc = 0;
1584 bond->alb_info.rlb_promisc_timeout_counter = 0;
1587 swap_slave = bond->curr_active_slave;
1588 bond->curr_active_slave = new_slave;
1590 if (!new_slave || (bond->slave_cnt == 0)) {
1594 /* set the new curr_active_slave to the bonds mac address
1595 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1598 struct slave *tmp_slave;
1599 /* find slave that is holding the bond's mac address */
1600 bond_for_each_slave(bond, tmp_slave, i) {
1601 if (!memcmp(tmp_slave->dev->dev_addr,
1602 bond->dev->dev_addr, ETH_ALEN)) {
1603 swap_slave = tmp_slave;
1609 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1611 /* swap mac address */
1612 alb_swap_mac_addr(bond, swap_slave, new_slave);
1614 /* set the new_slave to the bond mac address */
1615 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1616 bond->alb_info.rlb_enabled);
1617 /* fasten bond mac on new current slave */
1618 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1622 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1624 struct bonding *bond = bond_dev->priv;
1625 struct sockaddr *sa = addr;
1626 struct slave *slave, *swap_slave;
1630 if (!is_valid_ether_addr(sa->sa_data)) {
1631 return -EADDRNOTAVAIL;
1634 res = alb_set_mac_address(bond, addr);
1639 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1641 /* If there is no curr_active_slave there is nothing else to do.
1642 * Otherwise we'll need to pass the new address to it and handle
1645 if (!bond->curr_active_slave) {
1651 bond_for_each_slave(bond, slave, i) {
1652 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1659 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1661 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1662 bond->alb_info.rlb_enabled);
1664 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1665 if (bond->alb_info.rlb_enabled) {
1666 /* inform clients mac address has changed */
1667 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1674 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1676 if (bond->alb_info.current_alb_vlan &&
1677 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1678 bond->alb_info.current_alb_vlan = NULL;
1681 if (bond->alb_info.rlb_enabled) {
1682 rlb_clear_vlan(bond, vlan_id);