2 * Copyright(c) 1999 - 2003 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.
24 * 2003/06/25 - Shmulik Hen <shmulik.hen at intel dot com>
25 * - Fixed signed/unsigned calculation errors that caused load sharing
26 * to collapse to one slave under very heavy UDP Tx stress.
28 * 2003/08/06 - Amir Noam <amir.noam at intel dot com>
29 * - Add support for setting bond's MAC address with special
30 * handling required for ALB/TLB.
32 * 2003/09/24 - Shmulik Hen <shmulik.hen at intel dot com>
33 * - Code cleanup and style changes
36 //#define BONDING_DEBUG 1
38 #include <linux/skbuff.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/pkt_sched.h>
42 #include <linux/spinlock.h>
43 #include <linux/slab.h>
44 #include <linux/timer.h>
46 #include <linux/ipv6.h>
47 #include <linux/if_arp.h>
48 #include <linux/if_ether.h>
49 #include <linux/if_bonding.h>
52 #include <asm/byteorder.h>
57 #define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
58 #define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
59 * Used for division - never set
62 #define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
63 * learning packets to the switch
66 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
67 * ALB_TIMER_TICKS_PER_SEC)
69 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
70 * ALB_TIMER_TICKS_PER_SEC)
72 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
73 * Note that this value MUST NOT be smaller
74 * because the key hash table is BYTE wide !
78 #define TLB_NULL_INDEX 0xffffffff
79 #define MAX_LP_RETRY 3
82 #define RLB_HASH_TABLE_SIZE 256
83 #define RLB_NULL_INDEX 0xffffffff
84 #define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
85 #define RLB_ARP_BURST_SIZE 2
86 #define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
87 * rebalance interval (5 min).
89 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
90 * promiscuous after failover
92 #define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
94 static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
95 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
100 u8 mac_src[ETH_ALEN];
102 u8 padding[ETH_ZLEN - ETH_HLEN];
111 u8 mac_src[ETH_ALEN]; /* sender hardware address */
112 u32 ip_src; /* sender IP address */
113 u8 mac_dst[ETH_ALEN]; /* target hardware address */
114 u32 ip_dst; /* target IP address */
118 /* Forward declaration */
119 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
121 static inline u8 _simple_hash(u8 *hash_start, int hash_size)
126 for (i = 0; i < hash_size; i++) {
127 hash ^= hash_start[i];
133 /*********************** tlb specific functions ***************************/
135 static inline void _lock_tx_hashtbl(struct bonding *bond)
137 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
140 static inline void _unlock_tx_hashtbl(struct bonding *bond)
142 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
145 /* Caller must hold tx_hashtbl lock */
146 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
149 entry->load_history = 1 + entry->tx_bytes /
150 BOND_TLB_REBALANCE_INTERVAL;
154 entry->tx_slave = NULL;
155 entry->next = TLB_NULL_INDEX;
156 entry->prev = TLB_NULL_INDEX;
159 static inline void tlb_init_slave(struct slave *slave)
161 SLAVE_TLB_INFO(slave).load = 0;
162 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
165 /* Caller must hold bond lock for read */
166 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
168 struct tlb_client_info *tx_hash_table;
171 _lock_tx_hashtbl(bond);
173 /* clear slave from tx_hashtbl */
174 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
176 index = SLAVE_TLB_INFO(slave).head;
177 while (index != TLB_NULL_INDEX) {
178 u32 next_index = tx_hash_table[index].next;
179 tlb_init_table_entry(&tx_hash_table[index], save_load);
183 _unlock_tx_hashtbl(bond);
185 tlb_init_slave(slave);
188 /* Must be called before starting the monitor timer */
189 static int tlb_initialize(struct bonding *bond)
191 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
192 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
195 spin_lock_init(&(bond_info->tx_hashtbl_lock));
197 _lock_tx_hashtbl(bond);
199 bond_info->tx_hashtbl = kmalloc(size, GFP_KERNEL);
200 if (!bond_info->tx_hashtbl) {
201 printk(KERN_ERR DRV_NAME
202 ": Error: %s: Failed to allocate TLB hash table\n",
204 _unlock_tx_hashtbl(bond);
208 memset(bond_info->tx_hashtbl, 0, size);
210 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
211 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
214 _unlock_tx_hashtbl(bond);
219 /* Must be called only after all slaves have been released */
220 static void tlb_deinitialize(struct bonding *bond)
222 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
224 _lock_tx_hashtbl(bond);
226 kfree(bond_info->tx_hashtbl);
227 bond_info->tx_hashtbl = NULL;
229 _unlock_tx_hashtbl(bond);
232 /* Caller must hold bond lock for read */
233 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
235 struct slave *slave, *least_loaded;
239 /* Find the first enabled slave */
240 bond_for_each_slave(bond, slave, i) {
241 if (SLAVE_IS_OK(slave)) {
251 least_loaded = slave;
252 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
253 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
255 /* Find the slave with the largest gap */
256 bond_for_each_slave_from(bond, slave, i, least_loaded) {
257 if (SLAVE_IS_OK(slave)) {
258 s64 gap = (s64)(slave->speed << 20) -
259 (s64)(SLAVE_TLB_INFO(slave).load << 3);
261 least_loaded = slave;
270 /* Caller must hold bond lock for read */
271 struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
273 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
274 struct tlb_client_info *hash_table;
275 struct slave *assigned_slave;
277 _lock_tx_hashtbl(bond);
279 hash_table = bond_info->tx_hashtbl;
280 assigned_slave = hash_table[hash_index].tx_slave;
281 if (!assigned_slave) {
282 assigned_slave = tlb_get_least_loaded_slave(bond);
284 if (assigned_slave) {
285 struct tlb_slave_info *slave_info =
286 &(SLAVE_TLB_INFO(assigned_slave));
287 u32 next_index = slave_info->head;
289 hash_table[hash_index].tx_slave = assigned_slave;
290 hash_table[hash_index].next = next_index;
291 hash_table[hash_index].prev = TLB_NULL_INDEX;
293 if (next_index != TLB_NULL_INDEX) {
294 hash_table[next_index].prev = hash_index;
297 slave_info->head = hash_index;
299 hash_table[hash_index].load_history;
303 if (assigned_slave) {
304 hash_table[hash_index].tx_bytes += skb_len;
307 _unlock_tx_hashtbl(bond);
309 return assigned_slave;
312 /*********************** rlb specific functions ***************************/
313 static inline void _lock_rx_hashtbl(struct bonding *bond)
315 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
318 static inline void _unlock_rx_hashtbl(struct bonding *bond)
320 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
323 /* when an ARP REPLY is received from a client update its info
326 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
328 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
329 struct rlb_client_info *client_info;
332 _lock_rx_hashtbl(bond);
334 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
335 client_info = &(bond_info->rx_hashtbl[hash_index]);
337 if ((client_info->assigned) &&
338 (client_info->ip_src == arp->ip_dst) &&
339 (client_info->ip_dst == arp->ip_src)) {
340 /* update the clients MAC address */
341 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
342 client_info->ntt = 1;
343 bond_info->rx_ntt = 1;
346 _unlock_rx_hashtbl(bond);
349 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype)
351 struct bonding *bond = bond_dev->priv;
352 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
353 int res = NET_RX_DROP;
355 if (!(bond_dev->flags & IFF_MASTER)) {
360 dprintk("Packet has no ARP data\n");
364 if (skb->len < sizeof(struct arp_pkt)) {
365 dprintk("Packet is too small to be an ARP\n");
369 if (arp->op_code == htons(ARPOP_REPLY)) {
370 /* update rx hash table for this ARP */
371 rlb_update_entry_from_arp(bond, arp);
372 dprintk("Server received an ARP Reply from client\n");
375 res = NET_RX_SUCCESS;
383 /* Caller must hold bond lock for read */
384 static struct slave *rlb_next_rx_slave(struct bonding *bond)
386 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
387 struct slave *rx_slave, *slave, *start_at;
390 if (bond_info->next_rx_slave) {
391 start_at = bond_info->next_rx_slave;
393 start_at = bond->first_slave;
398 bond_for_each_slave_from(bond, slave, i, start_at) {
399 if (SLAVE_IS_OK(slave)) {
402 } else if (slave->speed > rx_slave->speed) {
409 bond_info->next_rx_slave = rx_slave->next;
415 /* teach the switch the mac of a disabled slave
416 * on the primary for fault tolerance
418 * Caller must hold bond->curr_slave_lock for write or bond lock for write
420 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
422 if (!bond->curr_active_slave) {
426 if (!bond->alb_info.primary_is_promisc) {
427 bond->alb_info.primary_is_promisc = 1;
428 dev_set_promiscuity(bond->curr_active_slave->dev, 1);
431 bond->alb_info.rlb_promisc_timeout_counter = 0;
433 alb_send_learning_packets(bond->curr_active_slave, addr);
436 /* slave being removed should not be active at this point
438 * Caller must hold bond lock for read
440 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
442 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
443 struct rlb_client_info *rx_hash_table;
444 u32 index, next_index;
446 /* clear slave from rx_hashtbl */
447 _lock_rx_hashtbl(bond);
449 rx_hash_table = bond_info->rx_hashtbl;
450 index = bond_info->rx_hashtbl_head;
451 for (; index != RLB_NULL_INDEX; index = next_index) {
452 next_index = rx_hash_table[index].next;
453 if (rx_hash_table[index].slave == slave) {
454 struct slave *assigned_slave = rlb_next_rx_slave(bond);
456 if (assigned_slave) {
457 rx_hash_table[index].slave = assigned_slave;
458 if (memcmp(rx_hash_table[index].mac_dst,
459 mac_bcast, ETH_ALEN)) {
460 bond_info->rx_hashtbl[index].ntt = 1;
461 bond_info->rx_ntt = 1;
462 /* A slave has been removed from the
463 * table because it is either disabled
464 * or being released. We must retry the
465 * update to avoid clients from not
466 * being updated & disconnecting when
469 bond_info->rlb_update_retry_counter =
472 } else { /* there is no active slave */
473 rx_hash_table[index].slave = NULL;
478 _unlock_rx_hashtbl(bond);
480 write_lock(&bond->curr_slave_lock);
482 if (slave != bond->curr_active_slave) {
483 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
486 write_unlock(&bond->curr_slave_lock);
489 static void rlb_update_client(struct rlb_client_info *client_info)
493 if (!client_info->slave) {
497 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
498 arp_send(ARPOP_REPLY, ETH_P_ARP,
500 client_info->slave->dev,
502 client_info->mac_dst,
503 client_info->slave->dev->dev_addr,
504 client_info->mac_dst);
508 /* sends ARP REPLIES that update the clients that need updating */
509 static void rlb_update_rx_clients(struct bonding *bond)
511 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
512 struct rlb_client_info *client_info;
515 _lock_rx_hashtbl(bond);
517 hash_index = bond_info->rx_hashtbl_head;
518 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
519 client_info = &(bond_info->rx_hashtbl[hash_index]);
520 if (client_info->ntt) {
521 rlb_update_client(client_info);
522 if (bond_info->rlb_update_retry_counter == 0) {
523 client_info->ntt = 0;
528 /* do not update the entries again untill this counter is zero so that
529 * not to confuse the clients.
531 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
533 _unlock_rx_hashtbl(bond);
536 /* The slave was assigned a new mac address - update the clients */
537 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
539 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
540 struct rlb_client_info *client_info;
544 _lock_rx_hashtbl(bond);
546 hash_index = bond_info->rx_hashtbl_head;
547 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
548 client_info = &(bond_info->rx_hashtbl[hash_index]);
550 if ((client_info->slave == slave) &&
551 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
552 client_info->ntt = 1;
557 // update the team's flag only after the whole iteration
559 bond_info->rx_ntt = 1;
561 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
564 _unlock_rx_hashtbl(bond);
567 /* mark all clients using src_ip to be updated */
568 static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
570 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
571 struct rlb_client_info *client_info;
574 _lock_rx_hashtbl(bond);
576 hash_index = bond_info->rx_hashtbl_head;
577 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
578 client_info = &(bond_info->rx_hashtbl[hash_index]);
580 if (!client_info->slave) {
581 printk(KERN_ERR DRV_NAME
582 ": Error: found a client with no channel in "
583 "the client's hash table\n");
586 /*update all clients using this src_ip, that are not assigned
587 * to the team's address (curr_active_slave) and have a known
588 * unicast mac address.
590 if ((client_info->ip_src == src_ip) &&
591 memcmp(client_info->slave->dev->dev_addr,
592 bond->dev->dev_addr, ETH_ALEN) &&
593 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
594 client_info->ntt = 1;
595 bond_info->rx_ntt = 1;
599 _unlock_rx_hashtbl(bond);
602 /* Caller must hold both bond and ptr locks for read */
603 struct slave *rlb_choose_channel(struct bonding *bond, struct arp_pkt *arp)
605 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
606 struct slave *assigned_slave;
607 struct rlb_client_info *client_info;
610 _lock_rx_hashtbl(bond);
612 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
613 client_info = &(bond_info->rx_hashtbl[hash_index]);
615 if (client_info->assigned) {
616 if ((client_info->ip_src == arp->ip_src) &&
617 (client_info->ip_dst == arp->ip_dst)) {
618 /* the entry is already assigned to this client */
619 if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
620 /* update mac address from arp */
621 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
624 assigned_slave = client_info->slave;
625 if (assigned_slave) {
626 _unlock_rx_hashtbl(bond);
627 return assigned_slave;
630 /* the entry is already assigned to some other client,
631 * move the old client to primary (curr_active_slave) so
632 * that the new client can be assigned to this entry.
634 if (bond->curr_active_slave &&
635 client_info->slave != bond->curr_active_slave) {
636 client_info->slave = bond->curr_active_slave;
637 rlb_update_client(client_info);
641 /* assign a new slave */
642 assigned_slave = rlb_next_rx_slave(bond);
644 if (assigned_slave) {
645 client_info->ip_src = arp->ip_src;
646 client_info->ip_dst = arp->ip_dst;
647 /* arp->mac_dst is broadcast for arp reqeusts.
648 * will be updated with clients actual unicast mac address
649 * upon receiving an arp reply.
651 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
652 client_info->slave = assigned_slave;
654 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
655 client_info->ntt = 1;
656 bond->alb_info.rx_ntt = 1;
658 client_info->ntt = 0;
661 if (!client_info->assigned) {
662 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
663 bond_info->rx_hashtbl_head = hash_index;
664 client_info->next = prev_tbl_head;
665 if (prev_tbl_head != RLB_NULL_INDEX) {
666 bond_info->rx_hashtbl[prev_tbl_head].prev =
669 client_info->assigned = 1;
673 _unlock_rx_hashtbl(bond);
675 return assigned_slave;
678 /* chooses (and returns) transmit channel for arp reply
679 * does not choose channel for other arp types since they are
680 * sent on the curr_active_slave
682 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
684 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
685 struct slave *tx_slave = NULL;
687 if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
688 /* the arp must be sent on the selected
691 tx_slave = rlb_choose_channel(bond, arp);
693 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
695 dprintk("Server sent ARP Reply packet\n");
696 } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
697 /* Create an entry in the rx_hashtbl for this client as a
699 * When the arp reply is received the entry will be updated
700 * with the correct unicast address of the client.
702 rlb_choose_channel(bond, arp);
704 /* The ARP relpy packets must be delayed so that
705 * they can cancel out the influence of the ARP request.
707 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
709 /* arp requests are broadcast and are sent on the primary
710 * the arp request will collapse all clients on the subnet to
711 * the primary slave. We must register these clients to be
712 * updated with their assigned mac.
714 rlb_req_update_subnet_clients(bond, arp->ip_src);
715 dprintk("Server sent ARP Request packet\n");
721 /* Caller must hold bond lock for read */
722 static void rlb_rebalance(struct bonding *bond)
724 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
725 struct slave *assigned_slave;
726 struct rlb_client_info *client_info;
730 _lock_rx_hashtbl(bond);
733 hash_index = bond_info->rx_hashtbl_head;
734 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
735 client_info = &(bond_info->rx_hashtbl[hash_index]);
736 assigned_slave = rlb_next_rx_slave(bond);
737 if (assigned_slave && (client_info->slave != assigned_slave)) {
738 client_info->slave = assigned_slave;
739 client_info->ntt = 1;
744 /* update the team's flag only after the whole iteration */
746 bond_info->rx_ntt = 1;
748 _unlock_rx_hashtbl(bond);
751 /* Caller must hold rx_hashtbl lock */
752 static void rlb_init_table_entry(struct rlb_client_info *entry)
754 memset(entry, 0, sizeof(struct rlb_client_info));
755 entry->next = RLB_NULL_INDEX;
756 entry->prev = RLB_NULL_INDEX;
759 static int rlb_initialize(struct bonding *bond)
761 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
762 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
763 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
766 spin_lock_init(&(bond_info->rx_hashtbl_lock));
768 _lock_rx_hashtbl(bond);
770 bond_info->rx_hashtbl = kmalloc(size, GFP_KERNEL);
771 if (!bond_info->rx_hashtbl) {
772 printk(KERN_ERR DRV_NAME
773 ": Error: %s: Failed to allocate RLB hash table\n",
775 _unlock_rx_hashtbl(bond);
779 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
781 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
782 rlb_init_table_entry(bond_info->rx_hashtbl + i);
785 _unlock_rx_hashtbl(bond);
787 /*initialize packet type*/
788 pk_type->type = __constant_htons(ETH_P_ARP);
789 pk_type->dev = bond->dev;
790 pk_type->func = rlb_arp_recv;
792 /* register to receive ARPs */
793 dev_add_pack(pk_type);
798 static void rlb_deinitialize(struct bonding *bond)
800 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
802 dev_remove_pack(&(bond_info->rlb_pkt_type));
804 _lock_rx_hashtbl(bond);
806 kfree(bond_info->rx_hashtbl);
807 bond_info->rx_hashtbl = NULL;
809 _unlock_rx_hashtbl(bond);
812 /*********************** tlb/rlb shared functions *********************/
814 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
816 struct learning_pkt pkt;
817 int size = sizeof(struct learning_pkt);
820 memset(&pkt, 0, size);
821 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
822 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
823 pkt.type = __constant_htons(ETH_P_LOOP);
825 for (i = 0; i < MAX_LP_RETRY; i++) {
829 skb = dev_alloc_skb(size);
834 data = skb_put(skb, size);
835 memcpy(data, &pkt, size);
838 skb->nh.raw = data + ETH_HLEN;
839 skb->protocol = pkt.type;
840 skb->priority = TC_PRIO_CONTROL;
841 skb->dev = slave->dev;
847 /* hw is a boolean parameter that determines whether we should try and
848 * set the hw address of the device as well as the hw address of the
851 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
853 struct net_device *dev = slave->dev;
854 struct sockaddr s_addr;
857 memcpy(dev->dev_addr, addr, dev->addr_len);
861 /* for rlb each slave must have a unique hw mac addresses so that */
862 /* each slave will receive packets destined to a different mac */
863 memcpy(s_addr.sa_data, addr, dev->addr_len);
864 s_addr.sa_family = dev->type;
865 if (dev->set_mac_address(dev, &s_addr)) {
866 printk(KERN_ERR DRV_NAME
867 ": Error: dev->set_mac_address of dev %s failed! ALB "
868 "mode requires that the base driver support setting "
869 "the hw address also when the network device's "
870 "interface is open\n",
877 /* Caller must hold bond lock for write or curr_slave_lock for write*/
878 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
880 struct slave *disabled_slave = NULL;
881 u8 tmp_mac_addr[ETH_ALEN];
882 int slaves_state_differ;
884 slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
886 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
887 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
888 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
890 /* fasten the change in the switch */
891 if (SLAVE_IS_OK(slave1)) {
892 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
893 if (bond->alb_info.rlb_enabled) {
894 /* inform the clients that the mac address
897 rlb_req_update_slave_clients(bond, slave1);
900 disabled_slave = slave1;
903 if (SLAVE_IS_OK(slave2)) {
904 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
905 if (bond->alb_info.rlb_enabled) {
906 /* inform the clients that the mac address
909 rlb_req_update_slave_clients(bond, slave2);
912 disabled_slave = slave2;
915 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
916 /* A disabled slave was assigned an active mac addr */
917 rlb_teach_disabled_mac_on_primary(bond,
918 disabled_slave->dev->dev_addr);
923 * alb_change_hw_addr_on_detach
924 * @bond: bonding we're working on
925 * @slave: the slave that was just detached
927 * We assume that @slave was already detached from the slave list.
929 * If @slave's permanent hw address is different both from its current
930 * address and from @bond's address, then somewhere in the bond there's
931 * a slave that has @slave's permanet address as its current address.
932 * We'll make sure that that slave no longer uses @slave's permanent address.
934 * Caller must hold bond lock
936 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
941 perm_curr_diff = memcmp(slave->perm_hwaddr,
942 slave->dev->dev_addr,
944 perm_bond_diff = memcmp(slave->perm_hwaddr,
948 if (perm_curr_diff && perm_bond_diff) {
949 struct slave *tmp_slave;
952 bond_for_each_slave(bond, tmp_slave, i) {
953 if (!memcmp(slave->perm_hwaddr,
954 tmp_slave->dev->dev_addr,
962 alb_swap_mac_addr(bond, slave, tmp_slave);
968 * alb_handle_addr_collision_on_attach
969 * @bond: bonding we're working on
970 * @slave: the slave that was just attached
972 * checks uniqueness of slave's mac address and handles the case the
973 * new slave uses the bonds mac address.
975 * If the permanent hw address of @slave is @bond's hw address, we need to
976 * find a different hw address to give @slave, that isn't in use by any other
977 * slave in the bond. This address must be, of course, one of the premanent
978 * addresses of the other slaves.
980 * We go over the slave list, and for each slave there we compare its
981 * permanent hw address with the current address of all the other slaves.
982 * If no match was found, then we've found a slave with a permanent address
983 * that isn't used by any other slave in the bond, so we can assign it to
986 * assumption: this function is called before @slave is attached to the
989 * caller must hold the bond lock for write since the mac addresses are compared
990 * and may be swapped.
992 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
994 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
997 if (bond->slave_cnt == 0) {
998 /* this is the first slave */
1002 /* if slave's mac address differs from bond's mac address
1003 * check uniqueness of slave's mac address against the other
1004 * slaves in the bond.
1006 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1007 bond_for_each_slave(bond, tmp_slave1, i) {
1008 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1016 /* a slave was found that is using the mac address
1019 printk(KERN_ERR DRV_NAME
1020 ": Error: the hw address of slave %s is not "
1021 "unique - cannot enslave it!",
1029 /* The slave's address is equal to the address of the bond.
1030 * Search for a spare address in the bond for this slave.
1032 free_mac_slave = NULL;
1034 bond_for_each_slave(bond, tmp_slave1, i) {
1036 bond_for_each_slave(bond, tmp_slave2, j) {
1037 if (!memcmp(tmp_slave1->perm_hwaddr,
1038 tmp_slave2->dev->dev_addr,
1046 /* no slave has tmp_slave1's perm addr
1049 free_mac_slave = tmp_slave1;
1054 if (free_mac_slave) {
1055 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1056 bond->alb_info.rlb_enabled);
1058 printk(KERN_WARNING DRV_NAME
1059 ": Warning: the hw address of slave %s is in use by "
1060 "the bond; giving it the hw address of %s\n",
1061 slave->dev->name, free_mac_slave->dev->name);
1063 printk(KERN_ERR DRV_NAME
1064 ": Error: the hw address of slave %s is in use by the "
1065 "bond; couldn't find a slave with a free hw address to "
1066 "give it (this should not have happened)\n",
1075 * alb_set_mac_address
1079 * In TLB mode all slaves are configured to the bond's hw address, but set
1080 * their dev_addr field to different addresses (based on their permanent hw
1083 * For each slave, this function sets the interface to the new address and then
1084 * changes its dev_addr field to its previous value.
1086 * Unwinding assumes bond's mac address has not yet changed.
1088 static int alb_set_mac_address(struct bonding *bond, void *addr)
1091 struct slave *slave, *stop_at;
1092 char tmp_addr[ETH_ALEN];
1096 if (bond->alb_info.rlb_enabled) {
1100 bond_for_each_slave(bond, slave, i) {
1101 if (slave->dev->set_mac_address == NULL) {
1106 /* save net_device's current hw address */
1107 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1109 res = slave->dev->set_mac_address(slave->dev, addr);
1111 /* restore net_device's hw address */
1112 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1122 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1123 sa.sa_family = bond->dev->type;
1125 /* unwind from head to the slave that failed */
1127 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1128 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1129 slave->dev->set_mac_address(slave->dev, &sa);
1130 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1136 /************************ exported alb funcions ************************/
1138 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1142 res = tlb_initialize(bond);
1148 bond->alb_info.rlb_enabled = 1;
1149 /* initialize rlb */
1150 res = rlb_initialize(bond);
1152 tlb_deinitialize(bond);
1160 void bond_alb_deinitialize(struct bonding *bond)
1162 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1164 tlb_deinitialize(bond);
1166 if (bond_info->rlb_enabled) {
1167 rlb_deinitialize(bond);
1171 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1173 struct bonding *bond = bond_dev->priv;
1174 struct ethhdr *eth_data = (struct ethhdr *)skb->mac.raw = skb->data;
1175 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1176 struct slave *tx_slave = NULL;
1177 static u32 ip_bcast = 0xffffffff;
1179 int do_tx_balance = 1;
1181 u8 *hash_start = NULL;
1183 /* make sure that the curr_active_slave and the slaves list do
1184 * not change during tx
1186 read_lock(&bond->lock);
1187 read_lock(&bond->curr_slave_lock);
1189 if (!BOND_IS_OK(bond)) {
1193 switch (ntohs(skb->protocol)) {
1195 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1196 (skb->nh.iph->daddr == ip_bcast)) {
1200 hash_start = (char*)&(skb->nh.iph->daddr);
1201 hash_size = sizeof(skb->nh.iph->daddr);
1204 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1209 hash_start = (char*)&(skb->nh.ipv6h->daddr);
1210 hash_size = sizeof(skb->nh.ipv6h->daddr);
1213 if (ipx_hdr(skb)->ipx_checksum !=
1214 __constant_htons(IPX_NO_CHECKSUM)) {
1215 /* something is wrong with this packet */
1220 if (ipx_hdr(skb)->ipx_type !=
1221 __constant_htons(IPX_TYPE_NCP)) {
1222 /* The only protocol worth balancing in
1223 * this family since it has an "ARP" like
1230 hash_start = (char*)eth_data->h_dest;
1231 hash_size = ETH_ALEN;
1235 if (bond_info->rlb_enabled) {
1236 tx_slave = rlb_arp_xmit(skb, bond);
1244 if (do_tx_balance) {
1245 hash_index = _simple_hash(hash_start, hash_size);
1246 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1250 /* unbalanced or unassigned, send through primary */
1251 tx_slave = bond->curr_active_slave;
1252 bond_info->unbalanced_load += skb->len;
1255 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1256 skb->dev = tx_slave->dev;
1257 if (tx_slave != bond->curr_active_slave) {
1258 memcpy(eth_data->h_source,
1259 tx_slave->dev->dev_addr,
1262 dev_queue_xmit(skb);
1264 /* no suitable interface, frame not sent */
1266 tlb_clear_slave(bond, tx_slave, 0);
1272 read_unlock(&bond->curr_slave_lock);
1273 read_unlock(&bond->lock);
1281 void bond_alb_monitor(struct bonding *bond)
1283 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1284 struct slave *slave;
1287 read_lock(&bond->lock);
1289 if (bond->kill_timers) {
1293 if (bond->slave_cnt == 0) {
1294 bond_info->tx_rebalance_counter = 0;
1295 bond_info->lp_counter = 0;
1299 bond_info->tx_rebalance_counter++;
1300 bond_info->lp_counter++;
1302 /* send learning packets */
1303 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1304 /* change of curr_active_slave involves swapping of mac addresses.
1305 * in order to avoid this swapping from happening while
1306 * sending the learning packets, the curr_slave_lock must be held for
1309 read_lock(&bond->curr_slave_lock);
1311 bond_for_each_slave(bond, slave, i) {
1312 alb_send_learning_packets(slave,slave->dev->dev_addr);
1315 read_unlock(&bond->curr_slave_lock);
1317 bond_info->lp_counter = 0;
1320 /* rebalance tx traffic */
1321 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1323 read_lock(&bond->curr_slave_lock);
1325 bond_for_each_slave(bond, slave, i) {
1326 tlb_clear_slave(bond, slave, 1);
1327 if (slave == bond->curr_active_slave) {
1328 SLAVE_TLB_INFO(slave).load =
1329 bond_info->unbalanced_load /
1330 BOND_TLB_REBALANCE_INTERVAL;
1331 bond_info->unbalanced_load = 0;
1335 read_unlock(&bond->curr_slave_lock);
1337 bond_info->tx_rebalance_counter = 0;
1340 /* handle rlb stuff */
1341 if (bond_info->rlb_enabled) {
1342 /* the following code changes the promiscuity of the
1343 * the curr_active_slave. It needs to be locked with a
1344 * write lock to protect from other code that also
1345 * sets the promiscuity.
1347 write_lock(&bond->curr_slave_lock);
1349 if (bond_info->primary_is_promisc &&
1350 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1352 bond_info->rlb_promisc_timeout_counter = 0;
1354 /* If the primary was set to promiscuous mode
1355 * because a slave was disabled then
1356 * it can now leave promiscuous mode.
1358 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1359 bond_info->primary_is_promisc = 0;
1362 write_unlock(&bond->curr_slave_lock);
1364 if (bond_info->rlb_rebalance) {
1365 bond_info->rlb_rebalance = 0;
1366 rlb_rebalance(bond);
1369 /* check if clients need updating */
1370 if (bond_info->rx_ntt) {
1371 if (bond_info->rlb_update_delay_counter) {
1372 --bond_info->rlb_update_delay_counter;
1374 rlb_update_rx_clients(bond);
1375 if (bond_info->rlb_update_retry_counter) {
1376 --bond_info->rlb_update_retry_counter;
1378 bond_info->rx_ntt = 0;
1385 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1387 read_unlock(&bond->lock);
1390 /* assumption: called before the slave is attached to the bond
1391 * and not locked by the bond lock
1393 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1397 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1398 bond->alb_info.rlb_enabled);
1403 /* caller must hold the bond lock for write since the mac addresses
1404 * are compared and may be swapped.
1406 write_lock_bh(&bond->lock);
1408 res = alb_handle_addr_collision_on_attach(bond, slave);
1410 write_unlock_bh(&bond->lock);
1416 tlb_init_slave(slave);
1418 /* order a rebalance ASAP */
1419 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1421 if (bond->alb_info.rlb_enabled) {
1422 bond->alb_info.rlb_rebalance = 1;
1428 /* Caller must hold bond lock for write */
1429 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1431 if (bond->slave_cnt > 1) {
1432 alb_change_hw_addr_on_detach(bond, slave);
1435 tlb_clear_slave(bond, slave, 0);
1437 if (bond->alb_info.rlb_enabled) {
1438 bond->alb_info.next_rx_slave = NULL;
1439 rlb_clear_slave(bond, slave);
1443 /* Caller must hold bond lock for read */
1444 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1446 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1448 if (link == BOND_LINK_DOWN) {
1449 tlb_clear_slave(bond, slave, 0);
1450 if (bond->alb_info.rlb_enabled) {
1451 rlb_clear_slave(bond, slave);
1453 } else if (link == BOND_LINK_UP) {
1454 /* order a rebalance ASAP */
1455 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1456 if (bond->alb_info.rlb_enabled) {
1457 bond->alb_info.rlb_rebalance = 1;
1458 /* If the updelay module parameter is smaller than the
1459 * forwarding delay of the switch the rebalance will
1460 * not work because the rebalance arp replies will
1461 * not be forwarded to the clients..
1468 * bond_alb_handle_active_change - assign new curr_active_slave
1469 * @bond: our bonding struct
1470 * @new_slave: new slave to assign
1472 * Set the bond->curr_active_slave to @new_slave and handle
1473 * mac address swapping and promiscuity changes as needed.
1475 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1477 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1479 struct slave *swap_slave;
1482 if (bond->curr_active_slave == new_slave) {
1486 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1487 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1488 bond->alb_info.primary_is_promisc = 0;
1489 bond->alb_info.rlb_promisc_timeout_counter = 0;
1492 swap_slave = bond->curr_active_slave;
1493 bond->curr_active_slave = new_slave;
1495 if (!new_slave || (bond->slave_cnt == 0)) {
1499 /* set the new curr_active_slave to the bonds mac address
1500 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1503 struct slave *tmp_slave;
1504 /* find slave that is holding the bond's mac address */
1505 bond_for_each_slave(bond, tmp_slave, i) {
1506 if (!memcmp(tmp_slave->dev->dev_addr,
1507 bond->dev->dev_addr, ETH_ALEN)) {
1508 swap_slave = tmp_slave;
1514 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1516 /* swap mac address */
1517 alb_swap_mac_addr(bond, swap_slave, new_slave);
1519 /* set the new_slave to the bond mac address */
1520 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1521 bond->alb_info.rlb_enabled);
1522 /* fasten bond mac on new current slave */
1523 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1527 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1529 struct bonding *bond = bond_dev->priv;
1530 struct sockaddr *sa = addr;
1531 struct slave *slave, *swap_slave;
1535 if (!is_valid_ether_addr(sa->sa_data)) {
1536 return -EADDRNOTAVAIL;
1539 res = alb_set_mac_address(bond, addr);
1544 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1546 /* If there is no curr_active_slave there is nothing else to do.
1547 * Otherwise we'll need to pass the new address to it and handle
1550 if (!bond->curr_active_slave) {
1556 bond_for_each_slave(bond, slave, i) {
1557 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1564 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1566 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1567 bond->alb_info.rlb_enabled);
1569 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1570 if (bond->alb_info.rlb_enabled) {
1571 /* inform clients mac address has changed */
1572 rlb_req_update_slave_clients(bond, bond->curr_active_slave);