[IPV4]: Replace __in_dev_get with __in_dev_get_rcu/rtnl
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / bonding / bond_alb.c
blobf8fce39611972f18fefdd8524368955425d84466
1 /*
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
12 * for more details.
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.
22 * Changes:
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/12/01 - Shmulik Hen <shmulik.hen at intel dot com>
33 * - Code cleanup and style changes
35 * 2003/12/30 - Amir Noam <amir.noam at intel dot com>
36 * - Fixed: Cannot remove and re-enslave the original active slave.
38 * 2004/01/14 - Shmulik Hen <shmulik.hen at intel dot com>
39 * - Add capability to tag self generated packets in ALB/TLB modes.
42 //#define BONDING_DEBUG 1
44 #include <linux/skbuff.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/pkt_sched.h>
48 #include <linux/spinlock.h>
49 #include <linux/slab.h>
50 #include <linux/timer.h>
51 #include <linux/ip.h>
52 #include <linux/ipv6.h>
53 #include <linux/if_arp.h>
54 #include <linux/if_ether.h>
55 #include <linux/if_bonding.h>
56 #include <linux/if_vlan.h>
57 #include <linux/in.h>
58 #include <net/ipx.h>
59 #include <net/arp.h>
60 #include <asm/byteorder.h>
61 #include "bonding.h"
62 #include "bond_alb.h"
65 #define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
66 #define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
67 * Used for division - never set
68 * to zero !!!
70 #define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
71 * learning packets to the switch
74 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
75 * ALB_TIMER_TICKS_PER_SEC)
77 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
78 * ALB_TIMER_TICKS_PER_SEC)
80 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
81 * Note that this value MUST NOT be smaller
82 * because the key hash table is BYTE wide !
86 #define TLB_NULL_INDEX 0xffffffff
87 #define MAX_LP_BURST 3
89 /* rlb defs */
90 #define RLB_HASH_TABLE_SIZE 256
91 #define RLB_NULL_INDEX 0xffffffff
92 #define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
93 #define RLB_ARP_BURST_SIZE 2
94 #define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
95 * rebalance interval (5 min).
97 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
98 * promiscuous after failover
100 #define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
102 static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
103 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
105 #pragma pack(1)
106 struct learning_pkt {
107 u8 mac_dst[ETH_ALEN];
108 u8 mac_src[ETH_ALEN];
109 u16 type;
110 u8 padding[ETH_ZLEN - ETH_HLEN];
113 struct arp_pkt {
114 u16 hw_addr_space;
115 u16 prot_addr_space;
116 u8 hw_addr_len;
117 u8 prot_addr_len;
118 u16 op_code;
119 u8 mac_src[ETH_ALEN]; /* sender hardware address */
120 u32 ip_src; /* sender IP address */
121 u8 mac_dst[ETH_ALEN]; /* target hardware address */
122 u32 ip_dst; /* target IP address */
124 #pragma pack()
126 /* Forward declaration */
127 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
129 static inline u8 _simple_hash(u8 *hash_start, int hash_size)
131 int i;
132 u8 hash = 0;
134 for (i = 0; i < hash_size; i++) {
135 hash ^= hash_start[i];
138 return hash;
141 /*********************** tlb specific functions ***************************/
143 static inline void _lock_tx_hashtbl(struct bonding *bond)
145 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
148 static inline void _unlock_tx_hashtbl(struct bonding *bond)
150 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
153 /* Caller must hold tx_hashtbl lock */
154 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
156 if (save_load) {
157 entry->load_history = 1 + entry->tx_bytes /
158 BOND_TLB_REBALANCE_INTERVAL;
159 entry->tx_bytes = 0;
162 entry->tx_slave = NULL;
163 entry->next = TLB_NULL_INDEX;
164 entry->prev = TLB_NULL_INDEX;
167 static inline void tlb_init_slave(struct slave *slave)
169 SLAVE_TLB_INFO(slave).load = 0;
170 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
173 /* Caller must hold bond lock for read */
174 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
176 struct tlb_client_info *tx_hash_table;
177 u32 index;
179 _lock_tx_hashtbl(bond);
181 /* clear slave from tx_hashtbl */
182 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
184 index = SLAVE_TLB_INFO(slave).head;
185 while (index != TLB_NULL_INDEX) {
186 u32 next_index = tx_hash_table[index].next;
187 tlb_init_table_entry(&tx_hash_table[index], save_load);
188 index = next_index;
191 _unlock_tx_hashtbl(bond);
193 tlb_init_slave(slave);
196 /* Must be called before starting the monitor timer */
197 static int tlb_initialize(struct bonding *bond)
199 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
200 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
201 int i;
203 spin_lock_init(&(bond_info->tx_hashtbl_lock));
205 _lock_tx_hashtbl(bond);
207 bond_info->tx_hashtbl = kmalloc(size, GFP_KERNEL);
208 if (!bond_info->tx_hashtbl) {
209 printk(KERN_ERR DRV_NAME
210 ": Error: %s: Failed to allocate TLB hash table\n",
211 bond->dev->name);
212 _unlock_tx_hashtbl(bond);
213 return -1;
216 memset(bond_info->tx_hashtbl, 0, size);
218 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
219 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
222 _unlock_tx_hashtbl(bond);
224 return 0;
227 /* Must be called only after all slaves have been released */
228 static void tlb_deinitialize(struct bonding *bond)
230 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
232 _lock_tx_hashtbl(bond);
234 kfree(bond_info->tx_hashtbl);
235 bond_info->tx_hashtbl = NULL;
237 _unlock_tx_hashtbl(bond);
240 /* Caller must hold bond lock for read */
241 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
243 struct slave *slave, *least_loaded;
244 s64 max_gap;
245 int i, found = 0;
247 /* Find the first enabled slave */
248 bond_for_each_slave(bond, slave, i) {
249 if (SLAVE_IS_OK(slave)) {
250 found = 1;
251 break;
255 if (!found) {
256 return NULL;
259 least_loaded = slave;
260 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
261 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
263 /* Find the slave with the largest gap */
264 bond_for_each_slave_from(bond, slave, i, least_loaded) {
265 if (SLAVE_IS_OK(slave)) {
266 s64 gap = (s64)(slave->speed << 20) -
267 (s64)(SLAVE_TLB_INFO(slave).load << 3);
268 if (max_gap < gap) {
269 least_loaded = slave;
270 max_gap = gap;
275 return least_loaded;
278 /* Caller must hold bond lock for read */
279 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
281 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
282 struct tlb_client_info *hash_table;
283 struct slave *assigned_slave;
285 _lock_tx_hashtbl(bond);
287 hash_table = bond_info->tx_hashtbl;
288 assigned_slave = hash_table[hash_index].tx_slave;
289 if (!assigned_slave) {
290 assigned_slave = tlb_get_least_loaded_slave(bond);
292 if (assigned_slave) {
293 struct tlb_slave_info *slave_info =
294 &(SLAVE_TLB_INFO(assigned_slave));
295 u32 next_index = slave_info->head;
297 hash_table[hash_index].tx_slave = assigned_slave;
298 hash_table[hash_index].next = next_index;
299 hash_table[hash_index].prev = TLB_NULL_INDEX;
301 if (next_index != TLB_NULL_INDEX) {
302 hash_table[next_index].prev = hash_index;
305 slave_info->head = hash_index;
306 slave_info->load +=
307 hash_table[hash_index].load_history;
311 if (assigned_slave) {
312 hash_table[hash_index].tx_bytes += skb_len;
315 _unlock_tx_hashtbl(bond);
317 return assigned_slave;
320 /*********************** rlb specific functions ***************************/
321 static inline void _lock_rx_hashtbl(struct bonding *bond)
323 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
326 static inline void _unlock_rx_hashtbl(struct bonding *bond)
328 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
331 /* when an ARP REPLY is received from a client update its info
332 * in the rx_hashtbl
334 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
336 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
337 struct rlb_client_info *client_info;
338 u32 hash_index;
340 _lock_rx_hashtbl(bond);
342 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
343 client_info = &(bond_info->rx_hashtbl[hash_index]);
345 if ((client_info->assigned) &&
346 (client_info->ip_src == arp->ip_dst) &&
347 (client_info->ip_dst == arp->ip_src)) {
348 /* update the clients MAC address */
349 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
350 client_info->ntt = 1;
351 bond_info->rx_ntt = 1;
354 _unlock_rx_hashtbl(bond);
357 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
359 struct bonding *bond = bond_dev->priv;
360 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
361 int res = NET_RX_DROP;
363 if (!(bond_dev->flags & IFF_MASTER))
364 goto out;
366 if (!arp) {
367 dprintk("Packet has no ARP data\n");
368 goto out;
371 if (skb->len < sizeof(struct arp_pkt)) {
372 dprintk("Packet is too small to be an ARP\n");
373 goto out;
376 if (arp->op_code == htons(ARPOP_REPLY)) {
377 /* update rx hash table for this ARP */
378 rlb_update_entry_from_arp(bond, arp);
379 dprintk("Server received an ARP Reply from client\n");
382 res = NET_RX_SUCCESS;
384 out:
385 dev_kfree_skb(skb);
387 return res;
390 /* Caller must hold bond lock for read */
391 static struct slave *rlb_next_rx_slave(struct bonding *bond)
393 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
394 struct slave *rx_slave, *slave, *start_at;
395 int i = 0;
397 if (bond_info->next_rx_slave) {
398 start_at = bond_info->next_rx_slave;
399 } else {
400 start_at = bond->first_slave;
403 rx_slave = NULL;
405 bond_for_each_slave_from(bond, slave, i, start_at) {
406 if (SLAVE_IS_OK(slave)) {
407 if (!rx_slave) {
408 rx_slave = slave;
409 } else if (slave->speed > rx_slave->speed) {
410 rx_slave = slave;
415 if (rx_slave) {
416 bond_info->next_rx_slave = rx_slave->next;
419 return rx_slave;
422 /* teach the switch the mac of a disabled slave
423 * on the primary for fault tolerance
425 * Caller must hold bond->curr_slave_lock for write or bond lock for write
427 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
429 if (!bond->curr_active_slave) {
430 return;
433 if (!bond->alb_info.primary_is_promisc) {
434 bond->alb_info.primary_is_promisc = 1;
435 dev_set_promiscuity(bond->curr_active_slave->dev, 1);
438 bond->alb_info.rlb_promisc_timeout_counter = 0;
440 alb_send_learning_packets(bond->curr_active_slave, addr);
443 /* slave being removed should not be active at this point
445 * Caller must hold bond lock for read
447 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
449 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
450 struct rlb_client_info *rx_hash_table;
451 u32 index, next_index;
453 /* clear slave from rx_hashtbl */
454 _lock_rx_hashtbl(bond);
456 rx_hash_table = bond_info->rx_hashtbl;
457 index = bond_info->rx_hashtbl_head;
458 for (; index != RLB_NULL_INDEX; index = next_index) {
459 next_index = rx_hash_table[index].next;
460 if (rx_hash_table[index].slave == slave) {
461 struct slave *assigned_slave = rlb_next_rx_slave(bond);
463 if (assigned_slave) {
464 rx_hash_table[index].slave = assigned_slave;
465 if (memcmp(rx_hash_table[index].mac_dst,
466 mac_bcast, ETH_ALEN)) {
467 bond_info->rx_hashtbl[index].ntt = 1;
468 bond_info->rx_ntt = 1;
469 /* A slave has been removed from the
470 * table because it is either disabled
471 * or being released. We must retry the
472 * update to avoid clients from not
473 * being updated & disconnecting when
474 * there is stress
476 bond_info->rlb_update_retry_counter =
477 RLB_UPDATE_RETRY;
479 } else { /* there is no active slave */
480 rx_hash_table[index].slave = NULL;
485 _unlock_rx_hashtbl(bond);
487 write_lock(&bond->curr_slave_lock);
489 if (slave != bond->curr_active_slave) {
490 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
493 write_unlock(&bond->curr_slave_lock);
496 static void rlb_update_client(struct rlb_client_info *client_info)
498 int i;
500 if (!client_info->slave) {
501 return;
504 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
505 struct sk_buff *skb;
507 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
508 client_info->ip_dst,
509 client_info->slave->dev,
510 client_info->ip_src,
511 client_info->mac_dst,
512 client_info->slave->dev->dev_addr,
513 client_info->mac_dst);
514 if (!skb) {
515 printk(KERN_ERR DRV_NAME
516 ": Error: failed to create an ARP packet\n");
517 continue;
520 skb->dev = client_info->slave->dev;
522 if (client_info->tag) {
523 skb = vlan_put_tag(skb, client_info->vlan_id);
524 if (!skb) {
525 printk(KERN_ERR DRV_NAME
526 ": Error: failed to insert VLAN tag\n");
527 continue;
531 arp_xmit(skb);
535 /* sends ARP REPLIES that update the clients that need updating */
536 static void rlb_update_rx_clients(struct bonding *bond)
538 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
539 struct rlb_client_info *client_info;
540 u32 hash_index;
542 _lock_rx_hashtbl(bond);
544 hash_index = bond_info->rx_hashtbl_head;
545 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
546 client_info = &(bond_info->rx_hashtbl[hash_index]);
547 if (client_info->ntt) {
548 rlb_update_client(client_info);
549 if (bond_info->rlb_update_retry_counter == 0) {
550 client_info->ntt = 0;
555 /* do not update the entries again untill this counter is zero so that
556 * not to confuse the clients.
558 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
560 _unlock_rx_hashtbl(bond);
563 /* The slave was assigned a new mac address - update the clients */
564 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
566 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
567 struct rlb_client_info *client_info;
568 int ntt = 0;
569 u32 hash_index;
571 _lock_rx_hashtbl(bond);
573 hash_index = bond_info->rx_hashtbl_head;
574 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
575 client_info = &(bond_info->rx_hashtbl[hash_index]);
577 if ((client_info->slave == slave) &&
578 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
579 client_info->ntt = 1;
580 ntt = 1;
584 // update the team's flag only after the whole iteration
585 if (ntt) {
586 bond_info->rx_ntt = 1;
587 //fasten the change
588 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
591 _unlock_rx_hashtbl(bond);
594 /* mark all clients using src_ip to be updated */
595 static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
597 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
598 struct rlb_client_info *client_info;
599 u32 hash_index;
601 _lock_rx_hashtbl(bond);
603 hash_index = bond_info->rx_hashtbl_head;
604 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
605 client_info = &(bond_info->rx_hashtbl[hash_index]);
607 if (!client_info->slave) {
608 printk(KERN_ERR DRV_NAME
609 ": Error: found a client with no channel in "
610 "the client's hash table\n");
611 continue;
613 /*update all clients using this src_ip, that are not assigned
614 * to the team's address (curr_active_slave) and have a known
615 * unicast mac address.
617 if ((client_info->ip_src == src_ip) &&
618 memcmp(client_info->slave->dev->dev_addr,
619 bond->dev->dev_addr, ETH_ALEN) &&
620 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
621 client_info->ntt = 1;
622 bond_info->rx_ntt = 1;
626 _unlock_rx_hashtbl(bond);
629 /* Caller must hold both bond and ptr locks for read */
630 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
632 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
633 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
634 struct slave *assigned_slave;
635 struct rlb_client_info *client_info;
636 u32 hash_index = 0;
638 _lock_rx_hashtbl(bond);
640 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
641 client_info = &(bond_info->rx_hashtbl[hash_index]);
643 if (client_info->assigned) {
644 if ((client_info->ip_src == arp->ip_src) &&
645 (client_info->ip_dst == arp->ip_dst)) {
646 /* the entry is already assigned to this client */
647 if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
648 /* update mac address from arp */
649 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
652 assigned_slave = client_info->slave;
653 if (assigned_slave) {
654 _unlock_rx_hashtbl(bond);
655 return assigned_slave;
657 } else {
658 /* the entry is already assigned to some other client,
659 * move the old client to primary (curr_active_slave) so
660 * that the new client can be assigned to this entry.
662 if (bond->curr_active_slave &&
663 client_info->slave != bond->curr_active_slave) {
664 client_info->slave = bond->curr_active_slave;
665 rlb_update_client(client_info);
669 /* assign a new slave */
670 assigned_slave = rlb_next_rx_slave(bond);
672 if (assigned_slave) {
673 client_info->ip_src = arp->ip_src;
674 client_info->ip_dst = arp->ip_dst;
675 /* arp->mac_dst is broadcast for arp reqeusts.
676 * will be updated with clients actual unicast mac address
677 * upon receiving an arp reply.
679 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
680 client_info->slave = assigned_slave;
682 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
683 client_info->ntt = 1;
684 bond->alb_info.rx_ntt = 1;
685 } else {
686 client_info->ntt = 0;
689 if (!list_empty(&bond->vlan_list)) {
690 unsigned short vlan_id;
691 int res = vlan_get_tag(skb, &vlan_id);
692 if (!res) {
693 client_info->tag = 1;
694 client_info->vlan_id = vlan_id;
698 if (!client_info->assigned) {
699 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
700 bond_info->rx_hashtbl_head = hash_index;
701 client_info->next = prev_tbl_head;
702 if (prev_tbl_head != RLB_NULL_INDEX) {
703 bond_info->rx_hashtbl[prev_tbl_head].prev =
704 hash_index;
706 client_info->assigned = 1;
710 _unlock_rx_hashtbl(bond);
712 return assigned_slave;
715 /* chooses (and returns) transmit channel for arp reply
716 * does not choose channel for other arp types since they are
717 * sent on the curr_active_slave
719 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
721 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
722 struct slave *tx_slave = NULL;
724 if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
725 /* the arp must be sent on the selected
726 * rx channel
728 tx_slave = rlb_choose_channel(skb, bond);
729 if (tx_slave) {
730 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
732 dprintk("Server sent ARP Reply packet\n");
733 } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
734 /* Create an entry in the rx_hashtbl for this client as a
735 * place holder.
736 * When the arp reply is received the entry will be updated
737 * with the correct unicast address of the client.
739 rlb_choose_channel(skb, bond);
741 /* The ARP relpy packets must be delayed so that
742 * they can cancel out the influence of the ARP request.
744 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
746 /* arp requests are broadcast and are sent on the primary
747 * the arp request will collapse all clients on the subnet to
748 * the primary slave. We must register these clients to be
749 * updated with their assigned mac.
751 rlb_req_update_subnet_clients(bond, arp->ip_src);
752 dprintk("Server sent ARP Request packet\n");
755 return tx_slave;
758 /* Caller must hold bond lock for read */
759 static void rlb_rebalance(struct bonding *bond)
761 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
762 struct slave *assigned_slave;
763 struct rlb_client_info *client_info;
764 int ntt;
765 u32 hash_index;
767 _lock_rx_hashtbl(bond);
769 ntt = 0;
770 hash_index = bond_info->rx_hashtbl_head;
771 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
772 client_info = &(bond_info->rx_hashtbl[hash_index]);
773 assigned_slave = rlb_next_rx_slave(bond);
774 if (assigned_slave && (client_info->slave != assigned_slave)) {
775 client_info->slave = assigned_slave;
776 client_info->ntt = 1;
777 ntt = 1;
781 /* update the team's flag only after the whole iteration */
782 if (ntt) {
783 bond_info->rx_ntt = 1;
785 _unlock_rx_hashtbl(bond);
788 /* Caller must hold rx_hashtbl lock */
789 static void rlb_init_table_entry(struct rlb_client_info *entry)
791 memset(entry, 0, sizeof(struct rlb_client_info));
792 entry->next = RLB_NULL_INDEX;
793 entry->prev = RLB_NULL_INDEX;
796 static int rlb_initialize(struct bonding *bond)
798 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
799 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
800 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
801 int i;
803 spin_lock_init(&(bond_info->rx_hashtbl_lock));
805 _lock_rx_hashtbl(bond);
807 bond_info->rx_hashtbl = kmalloc(size, GFP_KERNEL);
808 if (!bond_info->rx_hashtbl) {
809 printk(KERN_ERR DRV_NAME
810 ": Error: %s: Failed to allocate RLB hash table\n",
811 bond->dev->name);
812 _unlock_rx_hashtbl(bond);
813 return -1;
816 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
818 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
819 rlb_init_table_entry(bond_info->rx_hashtbl + i);
822 _unlock_rx_hashtbl(bond);
824 /*initialize packet type*/
825 pk_type->type = __constant_htons(ETH_P_ARP);
826 pk_type->dev = bond->dev;
827 pk_type->func = rlb_arp_recv;
829 /* register to receive ARPs */
830 dev_add_pack(pk_type);
832 return 0;
835 static void rlb_deinitialize(struct bonding *bond)
837 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
839 dev_remove_pack(&(bond_info->rlb_pkt_type));
841 _lock_rx_hashtbl(bond);
843 kfree(bond_info->rx_hashtbl);
844 bond_info->rx_hashtbl = NULL;
845 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
847 _unlock_rx_hashtbl(bond);
850 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
852 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
853 u32 curr_index;
855 _lock_rx_hashtbl(bond);
857 curr_index = bond_info->rx_hashtbl_head;
858 while (curr_index != RLB_NULL_INDEX) {
859 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
860 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
861 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
863 if (curr->tag && (curr->vlan_id == vlan_id)) {
864 if (curr_index == bond_info->rx_hashtbl_head) {
865 bond_info->rx_hashtbl_head = next_index;
867 if (prev_index != RLB_NULL_INDEX) {
868 bond_info->rx_hashtbl[prev_index].next = next_index;
870 if (next_index != RLB_NULL_INDEX) {
871 bond_info->rx_hashtbl[next_index].prev = prev_index;
874 rlb_init_table_entry(curr);
877 curr_index = next_index;
880 _unlock_rx_hashtbl(bond);
883 /*********************** tlb/rlb shared functions *********************/
885 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
887 struct bonding *bond = bond_get_bond_by_slave(slave);
888 struct learning_pkt pkt;
889 int size = sizeof(struct learning_pkt);
890 int i;
892 memset(&pkt, 0, size);
893 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
894 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
895 pkt.type = __constant_htons(ETH_P_LOOP);
897 for (i = 0; i < MAX_LP_BURST; i++) {
898 struct sk_buff *skb;
899 char *data;
901 skb = dev_alloc_skb(size);
902 if (!skb) {
903 return;
906 data = skb_put(skb, size);
907 memcpy(data, &pkt, size);
909 skb->mac.raw = data;
910 skb->nh.raw = data + ETH_HLEN;
911 skb->protocol = pkt.type;
912 skb->priority = TC_PRIO_CONTROL;
913 skb->dev = slave->dev;
915 if (!list_empty(&bond->vlan_list)) {
916 struct vlan_entry *vlan;
918 vlan = bond_next_vlan(bond,
919 bond->alb_info.current_alb_vlan);
921 bond->alb_info.current_alb_vlan = vlan;
922 if (!vlan) {
923 kfree_skb(skb);
924 continue;
927 skb = vlan_put_tag(skb, vlan->vlan_id);
928 if (!skb) {
929 printk(KERN_ERR DRV_NAME
930 ": Error: failed to insert VLAN tag\n");
931 continue;
935 dev_queue_xmit(skb);
939 /* hw is a boolean parameter that determines whether we should try and
940 * set the hw address of the device as well as the hw address of the
941 * net_device
943 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
945 struct net_device *dev = slave->dev;
946 struct sockaddr s_addr;
948 if (!hw) {
949 memcpy(dev->dev_addr, addr, dev->addr_len);
950 return 0;
953 /* for rlb each slave must have a unique hw mac addresses so that */
954 /* each slave will receive packets destined to a different mac */
955 memcpy(s_addr.sa_data, addr, dev->addr_len);
956 s_addr.sa_family = dev->type;
957 if (dev_set_mac_address(dev, &s_addr)) {
958 printk(KERN_ERR DRV_NAME
959 ": Error: dev_set_mac_address of dev %s failed! ALB "
960 "mode requires that the base driver support setting "
961 "the hw address also when the network device's "
962 "interface is open\n",
963 dev->name);
964 return -EOPNOTSUPP;
966 return 0;
969 /* Caller must hold bond lock for write or curr_slave_lock for write*/
970 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
972 struct slave *disabled_slave = NULL;
973 u8 tmp_mac_addr[ETH_ALEN];
974 int slaves_state_differ;
976 slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
978 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
979 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
980 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
982 /* fasten the change in the switch */
983 if (SLAVE_IS_OK(slave1)) {
984 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
985 if (bond->alb_info.rlb_enabled) {
986 /* inform the clients that the mac address
987 * has changed
989 rlb_req_update_slave_clients(bond, slave1);
991 } else {
992 disabled_slave = slave1;
995 if (SLAVE_IS_OK(slave2)) {
996 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
997 if (bond->alb_info.rlb_enabled) {
998 /* inform the clients that the mac address
999 * has changed
1001 rlb_req_update_slave_clients(bond, slave2);
1003 } else {
1004 disabled_slave = slave2;
1007 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1008 /* A disabled slave was assigned an active mac addr */
1009 rlb_teach_disabled_mac_on_primary(bond,
1010 disabled_slave->dev->dev_addr);
1015 * alb_change_hw_addr_on_detach
1016 * @bond: bonding we're working on
1017 * @slave: the slave that was just detached
1019 * We assume that @slave was already detached from the slave list.
1021 * If @slave's permanent hw address is different both from its current
1022 * address and from @bond's address, then somewhere in the bond there's
1023 * a slave that has @slave's permanet address as its current address.
1024 * We'll make sure that that slave no longer uses @slave's permanent address.
1026 * Caller must hold bond lock
1028 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1030 int perm_curr_diff;
1031 int perm_bond_diff;
1033 perm_curr_diff = memcmp(slave->perm_hwaddr,
1034 slave->dev->dev_addr,
1035 ETH_ALEN);
1036 perm_bond_diff = memcmp(slave->perm_hwaddr,
1037 bond->dev->dev_addr,
1038 ETH_ALEN);
1040 if (perm_curr_diff && perm_bond_diff) {
1041 struct slave *tmp_slave;
1042 int i, found = 0;
1044 bond_for_each_slave(bond, tmp_slave, i) {
1045 if (!memcmp(slave->perm_hwaddr,
1046 tmp_slave->dev->dev_addr,
1047 ETH_ALEN)) {
1048 found = 1;
1049 break;
1053 if (found) {
1054 alb_swap_mac_addr(bond, slave, tmp_slave);
1060 * alb_handle_addr_collision_on_attach
1061 * @bond: bonding we're working on
1062 * @slave: the slave that was just attached
1064 * checks uniqueness of slave's mac address and handles the case the
1065 * new slave uses the bonds mac address.
1067 * If the permanent hw address of @slave is @bond's hw address, we need to
1068 * find a different hw address to give @slave, that isn't in use by any other
1069 * slave in the bond. This address must be, of course, one of the premanent
1070 * addresses of the other slaves.
1072 * We go over the slave list, and for each slave there we compare its
1073 * permanent hw address with the current address of all the other slaves.
1074 * If no match was found, then we've found a slave with a permanent address
1075 * that isn't used by any other slave in the bond, so we can assign it to
1076 * @slave.
1078 * assumption: this function is called before @slave is attached to the
1079 * bond slave list.
1081 * caller must hold the bond lock for write since the mac addresses are compared
1082 * and may be swapped.
1084 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1086 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1087 struct slave *has_bond_addr = bond->curr_active_slave;
1088 int i, j, found = 0;
1090 if (bond->slave_cnt == 0) {
1091 /* this is the first slave */
1092 return 0;
1095 /* if slave's mac address differs from bond's mac address
1096 * check uniqueness of slave's mac address against the other
1097 * slaves in the bond.
1099 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1100 bond_for_each_slave(bond, tmp_slave1, i) {
1101 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1102 ETH_ALEN)) {
1103 found = 1;
1104 break;
1108 if (!found)
1109 return 0;
1111 /* Try setting slave mac to bond address and fall-through
1112 to code handling that situation below... */
1113 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1114 bond->alb_info.rlb_enabled);
1117 /* The slave's address is equal to the address of the bond.
1118 * Search for a spare address in the bond for this slave.
1120 free_mac_slave = NULL;
1122 bond_for_each_slave(bond, tmp_slave1, i) {
1123 found = 0;
1124 bond_for_each_slave(bond, tmp_slave2, j) {
1125 if (!memcmp(tmp_slave1->perm_hwaddr,
1126 tmp_slave2->dev->dev_addr,
1127 ETH_ALEN)) {
1128 found = 1;
1129 break;
1133 if (!found) {
1134 /* no slave has tmp_slave1's perm addr
1135 * as its curr addr
1137 free_mac_slave = tmp_slave1;
1138 break;
1141 if (!has_bond_addr) {
1142 if (!memcmp(tmp_slave1->dev->dev_addr,
1143 bond->dev->dev_addr,
1144 ETH_ALEN)) {
1146 has_bond_addr = tmp_slave1;
1151 if (free_mac_slave) {
1152 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1153 bond->alb_info.rlb_enabled);
1155 printk(KERN_WARNING DRV_NAME
1156 ": Warning: the hw address of slave %s is in use by "
1157 "the bond; giving it the hw address of %s\n",
1158 slave->dev->name, free_mac_slave->dev->name);
1160 } else if (has_bond_addr) {
1161 printk(KERN_ERR DRV_NAME
1162 ": Error: the hw address of slave %s is in use by the "
1163 "bond; couldn't find a slave with a free hw address to "
1164 "give it (this should not have happened)\n",
1165 slave->dev->name);
1166 return -EFAULT;
1169 return 0;
1173 * alb_set_mac_address
1174 * @bond:
1175 * @addr:
1177 * In TLB mode all slaves are configured to the bond's hw address, but set
1178 * their dev_addr field to different addresses (based on their permanent hw
1179 * addresses).
1181 * For each slave, this function sets the interface to the new address and then
1182 * changes its dev_addr field to its previous value.
1184 * Unwinding assumes bond's mac address has not yet changed.
1186 static int alb_set_mac_address(struct bonding *bond, void *addr)
1188 struct sockaddr sa;
1189 struct slave *slave, *stop_at;
1190 char tmp_addr[ETH_ALEN];
1191 int res;
1192 int i;
1194 if (bond->alb_info.rlb_enabled) {
1195 return 0;
1198 bond_for_each_slave(bond, slave, i) {
1199 if (slave->dev->set_mac_address == NULL) {
1200 res = -EOPNOTSUPP;
1201 goto unwind;
1204 /* save net_device's current hw address */
1205 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1207 res = dev_set_mac_address(slave->dev, addr);
1209 /* restore net_device's hw address */
1210 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1212 if (res) {
1213 goto unwind;
1217 return 0;
1219 unwind:
1220 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1221 sa.sa_family = bond->dev->type;
1223 /* unwind from head to the slave that failed */
1224 stop_at = slave;
1225 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1226 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1227 dev_set_mac_address(slave->dev, &sa);
1228 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1231 return res;
1234 /************************ exported alb funcions ************************/
1236 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1238 int res;
1240 res = tlb_initialize(bond);
1241 if (res) {
1242 return res;
1245 if (rlb_enabled) {
1246 bond->alb_info.rlb_enabled = 1;
1247 /* initialize rlb */
1248 res = rlb_initialize(bond);
1249 if (res) {
1250 tlb_deinitialize(bond);
1251 return res;
1255 return 0;
1258 void bond_alb_deinitialize(struct bonding *bond)
1260 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1262 tlb_deinitialize(bond);
1264 if (bond_info->rlb_enabled) {
1265 rlb_deinitialize(bond);
1269 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1271 struct bonding *bond = bond_dev->priv;
1272 struct ethhdr *eth_data;
1273 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1274 struct slave *tx_slave = NULL;
1275 static u32 ip_bcast = 0xffffffff;
1276 int hash_size = 0;
1277 int do_tx_balance = 1;
1278 u32 hash_index = 0;
1279 u8 *hash_start = NULL;
1280 int res = 1;
1282 skb->mac.raw = (unsigned char *)skb->data;
1283 eth_data = eth_hdr(skb);
1285 /* make sure that the curr_active_slave and the slaves list do
1286 * not change during tx
1288 read_lock(&bond->lock);
1289 read_lock(&bond->curr_slave_lock);
1291 if (!BOND_IS_OK(bond)) {
1292 goto out;
1295 switch (ntohs(skb->protocol)) {
1296 case ETH_P_IP:
1297 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1298 (skb->nh.iph->daddr == ip_bcast) ||
1299 (skb->nh.iph->protocol == IPPROTO_IGMP)) {
1300 do_tx_balance = 0;
1301 break;
1303 hash_start = (char*)&(skb->nh.iph->daddr);
1304 hash_size = sizeof(skb->nh.iph->daddr);
1305 break;
1306 case ETH_P_IPV6:
1307 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1308 do_tx_balance = 0;
1309 break;
1312 hash_start = (char*)&(skb->nh.ipv6h->daddr);
1313 hash_size = sizeof(skb->nh.ipv6h->daddr);
1314 break;
1315 case ETH_P_IPX:
1316 if (ipx_hdr(skb)->ipx_checksum !=
1317 __constant_htons(IPX_NO_CHECKSUM)) {
1318 /* something is wrong with this packet */
1319 do_tx_balance = 0;
1320 break;
1323 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1324 /* The only protocol worth balancing in
1325 * this family since it has an "ARP" like
1326 * mechanism
1328 do_tx_balance = 0;
1329 break;
1332 hash_start = (char*)eth_data->h_dest;
1333 hash_size = ETH_ALEN;
1334 break;
1335 case ETH_P_ARP:
1336 do_tx_balance = 0;
1337 if (bond_info->rlb_enabled) {
1338 tx_slave = rlb_arp_xmit(skb, bond);
1340 break;
1341 default:
1342 do_tx_balance = 0;
1343 break;
1346 if (do_tx_balance) {
1347 hash_index = _simple_hash(hash_start, hash_size);
1348 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1351 if (!tx_slave) {
1352 /* unbalanced or unassigned, send through primary */
1353 tx_slave = bond->curr_active_slave;
1354 bond_info->unbalanced_load += skb->len;
1357 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1358 if (tx_slave != bond->curr_active_slave) {
1359 memcpy(eth_data->h_source,
1360 tx_slave->dev->dev_addr,
1361 ETH_ALEN);
1364 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1365 } else {
1366 if (tx_slave) {
1367 tlb_clear_slave(bond, tx_slave, 0);
1371 out:
1372 if (res) {
1373 /* no suitable interface, frame not sent */
1374 dev_kfree_skb(skb);
1376 read_unlock(&bond->curr_slave_lock);
1377 read_unlock(&bond->lock);
1378 return 0;
1381 void bond_alb_monitor(struct bonding *bond)
1383 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1384 struct slave *slave;
1385 int i;
1387 read_lock(&bond->lock);
1389 if (bond->kill_timers) {
1390 goto out;
1393 if (bond->slave_cnt == 0) {
1394 bond_info->tx_rebalance_counter = 0;
1395 bond_info->lp_counter = 0;
1396 goto re_arm;
1399 bond_info->tx_rebalance_counter++;
1400 bond_info->lp_counter++;
1402 /* send learning packets */
1403 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1404 /* change of curr_active_slave involves swapping of mac addresses.
1405 * in order to avoid this swapping from happening while
1406 * sending the learning packets, the curr_slave_lock must be held for
1407 * read.
1409 read_lock(&bond->curr_slave_lock);
1411 bond_for_each_slave(bond, slave, i) {
1412 alb_send_learning_packets(slave,slave->dev->dev_addr);
1415 read_unlock(&bond->curr_slave_lock);
1417 bond_info->lp_counter = 0;
1420 /* rebalance tx traffic */
1421 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1423 read_lock(&bond->curr_slave_lock);
1425 bond_for_each_slave(bond, slave, i) {
1426 tlb_clear_slave(bond, slave, 1);
1427 if (slave == bond->curr_active_slave) {
1428 SLAVE_TLB_INFO(slave).load =
1429 bond_info->unbalanced_load /
1430 BOND_TLB_REBALANCE_INTERVAL;
1431 bond_info->unbalanced_load = 0;
1435 read_unlock(&bond->curr_slave_lock);
1437 bond_info->tx_rebalance_counter = 0;
1440 /* handle rlb stuff */
1441 if (bond_info->rlb_enabled) {
1442 /* the following code changes the promiscuity of the
1443 * the curr_active_slave. It needs to be locked with a
1444 * write lock to protect from other code that also
1445 * sets the promiscuity.
1447 write_lock(&bond->curr_slave_lock);
1449 if (bond_info->primary_is_promisc &&
1450 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1452 bond_info->rlb_promisc_timeout_counter = 0;
1454 /* If the primary was set to promiscuous mode
1455 * because a slave was disabled then
1456 * it can now leave promiscuous mode.
1458 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1459 bond_info->primary_is_promisc = 0;
1462 write_unlock(&bond->curr_slave_lock);
1464 if (bond_info->rlb_rebalance) {
1465 bond_info->rlb_rebalance = 0;
1466 rlb_rebalance(bond);
1469 /* check if clients need updating */
1470 if (bond_info->rx_ntt) {
1471 if (bond_info->rlb_update_delay_counter) {
1472 --bond_info->rlb_update_delay_counter;
1473 } else {
1474 rlb_update_rx_clients(bond);
1475 if (bond_info->rlb_update_retry_counter) {
1476 --bond_info->rlb_update_retry_counter;
1477 } else {
1478 bond_info->rx_ntt = 0;
1484 re_arm:
1485 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1486 out:
1487 read_unlock(&bond->lock);
1490 /* assumption: called before the slave is attached to the bond
1491 * and not locked by the bond lock
1493 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1495 int res;
1497 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1498 bond->alb_info.rlb_enabled);
1499 if (res) {
1500 return res;
1503 /* caller must hold the bond lock for write since the mac addresses
1504 * are compared and may be swapped.
1506 write_lock_bh(&bond->lock);
1508 res = alb_handle_addr_collision_on_attach(bond, slave);
1510 write_unlock_bh(&bond->lock);
1512 if (res) {
1513 return res;
1516 tlb_init_slave(slave);
1518 /* order a rebalance ASAP */
1519 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1521 if (bond->alb_info.rlb_enabled) {
1522 bond->alb_info.rlb_rebalance = 1;
1525 return 0;
1528 /* Caller must hold bond lock for write */
1529 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1531 if (bond->slave_cnt > 1) {
1532 alb_change_hw_addr_on_detach(bond, slave);
1535 tlb_clear_slave(bond, slave, 0);
1537 if (bond->alb_info.rlb_enabled) {
1538 bond->alb_info.next_rx_slave = NULL;
1539 rlb_clear_slave(bond, slave);
1543 /* Caller must hold bond lock for read */
1544 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1546 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1548 if (link == BOND_LINK_DOWN) {
1549 tlb_clear_slave(bond, slave, 0);
1550 if (bond->alb_info.rlb_enabled) {
1551 rlb_clear_slave(bond, slave);
1553 } else if (link == BOND_LINK_UP) {
1554 /* order a rebalance ASAP */
1555 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1556 if (bond->alb_info.rlb_enabled) {
1557 bond->alb_info.rlb_rebalance = 1;
1558 /* If the updelay module parameter is smaller than the
1559 * forwarding delay of the switch the rebalance will
1560 * not work because the rebalance arp replies will
1561 * not be forwarded to the clients..
1568 * bond_alb_handle_active_change - assign new curr_active_slave
1569 * @bond: our bonding struct
1570 * @new_slave: new slave to assign
1572 * Set the bond->curr_active_slave to @new_slave and handle
1573 * mac address swapping and promiscuity changes as needed.
1575 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1577 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1579 struct slave *swap_slave;
1580 int i;
1582 if (bond->curr_active_slave == new_slave) {
1583 return;
1586 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1587 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1588 bond->alb_info.primary_is_promisc = 0;
1589 bond->alb_info.rlb_promisc_timeout_counter = 0;
1592 swap_slave = bond->curr_active_slave;
1593 bond->curr_active_slave = new_slave;
1595 if (!new_slave || (bond->slave_cnt == 0)) {
1596 return;
1599 /* set the new curr_active_slave to the bonds mac address
1600 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1602 if (!swap_slave) {
1603 struct slave *tmp_slave;
1604 /* find slave that is holding the bond's mac address */
1605 bond_for_each_slave(bond, tmp_slave, i) {
1606 if (!memcmp(tmp_slave->dev->dev_addr,
1607 bond->dev->dev_addr, ETH_ALEN)) {
1608 swap_slave = tmp_slave;
1609 break;
1614 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1615 if (swap_slave) {
1616 /* swap mac address */
1617 alb_swap_mac_addr(bond, swap_slave, new_slave);
1618 } else {
1619 /* set the new_slave to the bond mac address */
1620 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1621 bond->alb_info.rlb_enabled);
1622 /* fasten bond mac on new current slave */
1623 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1627 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1629 struct bonding *bond = bond_dev->priv;
1630 struct sockaddr *sa = addr;
1631 struct slave *slave, *swap_slave;
1632 int res;
1633 int i;
1635 if (!is_valid_ether_addr(sa->sa_data)) {
1636 return -EADDRNOTAVAIL;
1639 res = alb_set_mac_address(bond, addr);
1640 if (res) {
1641 return res;
1644 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1646 /* If there is no curr_active_slave there is nothing else to do.
1647 * Otherwise we'll need to pass the new address to it and handle
1648 * duplications.
1650 if (!bond->curr_active_slave) {
1651 return 0;
1654 swap_slave = NULL;
1656 bond_for_each_slave(bond, slave, i) {
1657 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1658 swap_slave = slave;
1659 break;
1663 if (swap_slave) {
1664 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1665 } else {
1666 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1667 bond->alb_info.rlb_enabled);
1669 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1670 if (bond->alb_info.rlb_enabled) {
1671 /* inform clients mac address has changed */
1672 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1676 return 0;
1679 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1681 if (bond->alb_info.current_alb_vlan &&
1682 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1683 bond->alb_info.current_alb_vlan = NULL;
1686 if (bond->alb_info.rlb_enabled) {
1687 rlb_clear_vlan(bond, vlan_id);