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;
995 struct slave *has_bond_addr = bond->curr_active_slave;
998 if (bond->slave_cnt == 0) {
999 /* this is the first slave */
1003 /* if slave's mac address differs from bond's mac address
1004 * check uniqueness of slave's mac address against the other
1005 * slaves in the bond.
1007 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1008 bond_for_each_slave(bond, tmp_slave1, i) {
1009 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1017 /* a slave was found that is using the mac address
1020 printk(KERN_ERR DRV_NAME
1021 ": Error: the hw address of slave %s is not "
1022 "unique - cannot enslave it!",
1030 /* The slave's address is equal to the address of the bond.
1031 * Search for a spare address in the bond for this slave.
1033 free_mac_slave = NULL;
1035 bond_for_each_slave(bond, tmp_slave1, i) {
1037 bond_for_each_slave(bond, tmp_slave2, j) {
1038 if (!memcmp(tmp_slave1->perm_hwaddr,
1039 tmp_slave2->dev->dev_addr,
1047 /* no slave has tmp_slave1's perm addr
1050 free_mac_slave = tmp_slave1;
1054 if (!has_bond_addr) {
1055 if (!memcmp(tmp_slave1->dev->dev_addr,
1056 bond->dev->dev_addr,
1059 has_bond_addr = tmp_slave1;
1064 if (free_mac_slave) {
1065 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1066 bond->alb_info.rlb_enabled);
1068 printk(KERN_WARNING DRV_NAME
1069 ": Warning: the hw address of slave %s is in use by "
1070 "the bond; giving it the hw address of %s\n",
1071 slave->dev->name, free_mac_slave->dev->name);
1073 } else if (has_bond_addr) {
1074 printk(KERN_ERR DRV_NAME
1075 ": Error: the hw address of slave %s is in use by the "
1076 "bond; couldn't find a slave with a free hw address to "
1077 "give it (this should not have happened)\n",
1086 * alb_set_mac_address
1090 * In TLB mode all slaves are configured to the bond's hw address, but set
1091 * their dev_addr field to different addresses (based on their permanent hw
1094 * For each slave, this function sets the interface to the new address and then
1095 * changes its dev_addr field to its previous value.
1097 * Unwinding assumes bond's mac address has not yet changed.
1099 static int alb_set_mac_address(struct bonding *bond, void *addr)
1102 struct slave *slave, *stop_at;
1103 char tmp_addr[ETH_ALEN];
1107 if (bond->alb_info.rlb_enabled) {
1111 bond_for_each_slave(bond, slave, i) {
1112 if (slave->dev->set_mac_address == NULL) {
1117 /* save net_device's current hw address */
1118 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1120 res = slave->dev->set_mac_address(slave->dev, addr);
1122 /* restore net_device's hw address */
1123 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1133 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1134 sa.sa_family = bond->dev->type;
1136 /* unwind from head to the slave that failed */
1138 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1139 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1140 slave->dev->set_mac_address(slave->dev, &sa);
1141 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1147 /************************ exported alb funcions ************************/
1149 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1153 res = tlb_initialize(bond);
1159 bond->alb_info.rlb_enabled = 1;
1160 /* initialize rlb */
1161 res = rlb_initialize(bond);
1163 tlb_deinitialize(bond);
1171 void bond_alb_deinitialize(struct bonding *bond)
1173 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1175 tlb_deinitialize(bond);
1177 if (bond_info->rlb_enabled) {
1178 rlb_deinitialize(bond);
1182 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1184 struct bonding *bond = bond_dev->priv;
1185 struct ethhdr *eth_data = (struct ethhdr *)skb->mac.raw = skb->data;
1186 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1187 struct slave *tx_slave = NULL;
1188 static u32 ip_bcast = 0xffffffff;
1190 int do_tx_balance = 1;
1192 u8 *hash_start = NULL;
1194 /* make sure that the curr_active_slave and the slaves list do
1195 * not change during tx
1197 read_lock(&bond->lock);
1198 read_lock(&bond->curr_slave_lock);
1200 if (!BOND_IS_OK(bond)) {
1204 switch (ntohs(skb->protocol)) {
1206 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1207 (skb->nh.iph->daddr == ip_bcast)) {
1211 hash_start = (char*)&(skb->nh.iph->daddr);
1212 hash_size = sizeof(skb->nh.iph->daddr);
1215 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1220 hash_start = (char*)&(skb->nh.ipv6h->daddr);
1221 hash_size = sizeof(skb->nh.ipv6h->daddr);
1224 if (ipx_hdr(skb)->ipx_checksum !=
1225 __constant_htons(IPX_NO_CHECKSUM)) {
1226 /* something is wrong with this packet */
1231 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1232 /* The only protocol worth balancing in
1233 * this family since it has an "ARP" like
1240 hash_start = (char*)eth_data->h_dest;
1241 hash_size = ETH_ALEN;
1245 if (bond_info->rlb_enabled) {
1246 tx_slave = rlb_arp_xmit(skb, bond);
1254 if (do_tx_balance) {
1255 hash_index = _simple_hash(hash_start, hash_size);
1256 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1260 /* unbalanced or unassigned, send through primary */
1261 tx_slave = bond->curr_active_slave;
1262 bond_info->unbalanced_load += skb->len;
1265 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1266 skb->dev = tx_slave->dev;
1267 if (tx_slave != bond->curr_active_slave) {
1268 memcpy(eth_data->h_source,
1269 tx_slave->dev->dev_addr,
1272 dev_queue_xmit(skb);
1274 /* no suitable interface, frame not sent */
1276 tlb_clear_slave(bond, tx_slave, 0);
1282 read_unlock(&bond->curr_slave_lock);
1283 read_unlock(&bond->lock);
1291 void bond_alb_monitor(struct bonding *bond)
1293 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1294 struct slave *slave;
1297 read_lock(&bond->lock);
1299 if (bond->kill_timers) {
1303 if (bond->slave_cnt == 0) {
1304 bond_info->tx_rebalance_counter = 0;
1305 bond_info->lp_counter = 0;
1309 bond_info->tx_rebalance_counter++;
1310 bond_info->lp_counter++;
1312 /* send learning packets */
1313 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1314 /* change of curr_active_slave involves swapping of mac addresses.
1315 * in order to avoid this swapping from happening while
1316 * sending the learning packets, the curr_slave_lock must be held for
1319 read_lock(&bond->curr_slave_lock);
1321 bond_for_each_slave(bond, slave, i) {
1322 alb_send_learning_packets(slave,slave->dev->dev_addr);
1325 read_unlock(&bond->curr_slave_lock);
1327 bond_info->lp_counter = 0;
1330 /* rebalance tx traffic */
1331 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1333 read_lock(&bond->curr_slave_lock);
1335 bond_for_each_slave(bond, slave, i) {
1336 tlb_clear_slave(bond, slave, 1);
1337 if (slave == bond->curr_active_slave) {
1338 SLAVE_TLB_INFO(slave).load =
1339 bond_info->unbalanced_load /
1340 BOND_TLB_REBALANCE_INTERVAL;
1341 bond_info->unbalanced_load = 0;
1345 read_unlock(&bond->curr_slave_lock);
1347 bond_info->tx_rebalance_counter = 0;
1350 /* handle rlb stuff */
1351 if (bond_info->rlb_enabled) {
1352 /* the following code changes the promiscuity of the
1353 * the curr_active_slave. It needs to be locked with a
1354 * write lock to protect from other code that also
1355 * sets the promiscuity.
1357 write_lock(&bond->curr_slave_lock);
1359 if (bond_info->primary_is_promisc &&
1360 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1362 bond_info->rlb_promisc_timeout_counter = 0;
1364 /* If the primary was set to promiscuous mode
1365 * because a slave was disabled then
1366 * it can now leave promiscuous mode.
1368 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1369 bond_info->primary_is_promisc = 0;
1372 write_unlock(&bond->curr_slave_lock);
1374 if (bond_info->rlb_rebalance) {
1375 bond_info->rlb_rebalance = 0;
1376 rlb_rebalance(bond);
1379 /* check if clients need updating */
1380 if (bond_info->rx_ntt) {
1381 if (bond_info->rlb_update_delay_counter) {
1382 --bond_info->rlb_update_delay_counter;
1384 rlb_update_rx_clients(bond);
1385 if (bond_info->rlb_update_retry_counter) {
1386 --bond_info->rlb_update_retry_counter;
1388 bond_info->rx_ntt = 0;
1395 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1397 read_unlock(&bond->lock);
1400 /* assumption: called before the slave is attached to the bond
1401 * and not locked by the bond lock
1403 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1407 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1408 bond->alb_info.rlb_enabled);
1413 /* caller must hold the bond lock for write since the mac addresses
1414 * are compared and may be swapped.
1416 write_lock_bh(&bond->lock);
1418 res = alb_handle_addr_collision_on_attach(bond, slave);
1420 write_unlock_bh(&bond->lock);
1426 tlb_init_slave(slave);
1428 /* order a rebalance ASAP */
1429 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1431 if (bond->alb_info.rlb_enabled) {
1432 bond->alb_info.rlb_rebalance = 1;
1438 /* Caller must hold bond lock for write */
1439 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1441 if (bond->slave_cnt > 1) {
1442 alb_change_hw_addr_on_detach(bond, slave);
1445 tlb_clear_slave(bond, slave, 0);
1447 if (bond->alb_info.rlb_enabled) {
1448 bond->alb_info.next_rx_slave = NULL;
1449 rlb_clear_slave(bond, slave);
1453 /* Caller must hold bond lock for read */
1454 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1456 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1458 if (link == BOND_LINK_DOWN) {
1459 tlb_clear_slave(bond, slave, 0);
1460 if (bond->alb_info.rlb_enabled) {
1461 rlb_clear_slave(bond, slave);
1463 } else if (link == BOND_LINK_UP) {
1464 /* order a rebalance ASAP */
1465 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1466 if (bond->alb_info.rlb_enabled) {
1467 bond->alb_info.rlb_rebalance = 1;
1468 /* If the updelay module parameter is smaller than the
1469 * forwarding delay of the switch the rebalance will
1470 * not work because the rebalance arp replies will
1471 * not be forwarded to the clients..
1478 * bond_alb_handle_active_change - assign new curr_active_slave
1479 * @bond: our bonding struct
1480 * @new_slave: new slave to assign
1482 * Set the bond->curr_active_slave to @new_slave and handle
1483 * mac address swapping and promiscuity changes as needed.
1485 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1487 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1489 struct slave *swap_slave;
1492 if (bond->curr_active_slave == new_slave) {
1496 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1497 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1498 bond->alb_info.primary_is_promisc = 0;
1499 bond->alb_info.rlb_promisc_timeout_counter = 0;
1502 swap_slave = bond->curr_active_slave;
1503 bond->curr_active_slave = new_slave;
1505 if (!new_slave || (bond->slave_cnt == 0)) {
1509 /* set the new curr_active_slave to the bonds mac address
1510 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1513 struct slave *tmp_slave;
1514 /* find slave that is holding the bond's mac address */
1515 bond_for_each_slave(bond, tmp_slave, i) {
1516 if (!memcmp(tmp_slave->dev->dev_addr,
1517 bond->dev->dev_addr, ETH_ALEN)) {
1518 swap_slave = tmp_slave;
1524 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1526 /* swap mac address */
1527 alb_swap_mac_addr(bond, swap_slave, new_slave);
1529 /* set the new_slave to the bond mac address */
1530 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1531 bond->alb_info.rlb_enabled);
1532 /* fasten bond mac on new current slave */
1533 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1537 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1539 struct bonding *bond = bond_dev->priv;
1540 struct sockaddr *sa = addr;
1541 struct slave *slave, *swap_slave;
1545 if (!is_valid_ether_addr(sa->sa_data)) {
1546 return -EADDRNOTAVAIL;
1549 res = alb_set_mac_address(bond, addr);
1554 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1556 /* If there is no curr_active_slave there is nothing else to do.
1557 * Otherwise we'll need to pass the new address to it and handle
1560 if (!bond->curr_active_slave) {
1566 bond_for_each_slave(bond, slave, i) {
1567 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1574 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1576 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1577 bond->alb_info.rlb_enabled);
1579 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1580 if (bond->alb_info.rlb_enabled) {
1581 /* inform clients mac address has changed */
1582 rlb_req_update_slave_clients(bond, bond->curr_active_slave);