Original kernel 2.4.37.5
[tomato.git] / release / src / linux / linux / drivers / net / bonding / bond_alb.c
blobc12924967ce3e98cb7c6bb27ee1aae1adf95573a
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.
41 * 2005/12/02 - Michael O'Donnell <Michael.ODonnell at stratus dot com>
42 * - Stratus88746: tlb_clear_slave() must tlb_init_slave() while locked.
45 //#define BONDING_DEBUG 1
47 #include <linux/skbuff.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/pkt_sched.h>
51 #include <linux/spinlock.h>
52 #include <linux/slab.h>
53 #include <linux/timer.h>
54 #include <linux/ip.h>
55 #include <linux/ipv6.h>
56 #include <linux/if_arp.h>
57 #include <linux/if_ether.h>
58 #include <linux/if_bonding.h>
59 #include <linux/if_vlan.h>
60 #include <net/ipx.h>
61 #include <net/arp.h>
62 #include <asm/byteorder.h>
63 #include "bonding.h"
64 #include "bond_alb.h"
67 #define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
68 #define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
69 * Used for division - never set
70 * to zero !!!
72 #define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
73 * learning packets to the switch
76 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
77 * ALB_TIMER_TICKS_PER_SEC)
79 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
80 * ALB_TIMER_TICKS_PER_SEC)
82 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
83 * Note that this value MUST NOT be smaller
84 * because the key hash table is BYTE wide !
88 #define TLB_NULL_INDEX 0xffffffff
89 #define MAX_LP_BURST 3
91 /* rlb defs */
92 #define RLB_HASH_TABLE_SIZE 256
93 #define RLB_NULL_INDEX 0xffffffff
94 #define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
95 #define RLB_ARP_BURST_SIZE 2
96 #define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
97 * rebalance interval (5 min).
99 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
100 * promiscuous after failover
102 #define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
104 static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
105 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
107 #pragma pack(1)
108 struct learning_pkt {
109 u8 mac_dst[ETH_ALEN];
110 u8 mac_src[ETH_ALEN];
111 u16 type;
112 u8 padding[ETH_ZLEN - ETH_HLEN];
115 struct arp_pkt {
116 u16 hw_addr_space;
117 u16 prot_addr_space;
118 u8 hw_addr_len;
119 u8 prot_addr_len;
120 u16 op_code;
121 u8 mac_src[ETH_ALEN]; /* sender hardware address */
122 u32 ip_src; /* sender IP address */
123 u8 mac_dst[ETH_ALEN]; /* target hardware address */
124 u32 ip_dst; /* target IP address */
126 #pragma pack()
128 /* Forward declaration */
129 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
131 static inline u8 _simple_hash(u8 *hash_start, int hash_size)
133 int i;
134 u8 hash = 0;
136 for (i = 0; i < hash_size; i++) {
137 hash ^= hash_start[i];
140 return hash;
143 /*********************** tlb specific functions ***************************/
145 static inline void _lock_tx_hashtbl(struct bonding *bond)
147 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
150 static inline void _unlock_tx_hashtbl(struct bonding *bond)
152 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
155 /* Caller must hold tx_hashtbl lock */
156 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
158 if (save_load) {
159 entry->load_history = 1 + entry->tx_bytes /
160 BOND_TLB_REBALANCE_INTERVAL;
161 entry->tx_bytes = 0;
164 entry->tx_slave = NULL;
165 entry->next = TLB_NULL_INDEX;
166 entry->prev = TLB_NULL_INDEX;
169 static inline void tlb_init_slave(struct slave *slave)
171 SLAVE_TLB_INFO(slave).load = 0;
172 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
175 /* Caller must hold bond lock for read */
176 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
178 struct tlb_client_info *tx_hash_table;
179 u32 index;
181 _lock_tx_hashtbl(bond);
183 /* clear slave from tx_hashtbl */
184 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
186 if (tx_hash_table) {
187 index = SLAVE_TLB_INFO(slave).head;
188 while (index != TLB_NULL_INDEX) {
189 u32 next_index = tx_hash_table[index].next;
190 tlb_init_table_entry(&tx_hash_table[index], save_load);
191 index = next_index;
195 tlb_init_slave(slave); /* Stratus88746: do this before unlocking */
197 _unlock_tx_hashtbl(bond);
200 /* Must be called before starting the monitor timer */
201 static int tlb_initialize(struct bonding *bond)
203 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
204 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
205 int i;
207 spin_lock_init(&(bond_info->tx_hashtbl_lock));
209 _lock_tx_hashtbl(bond);
211 bond_info->tx_hashtbl = kmalloc(size, GFP_KERNEL);
212 if (!bond_info->tx_hashtbl) {
213 printk(KERN_ERR DRV_NAME
214 ": Error: %s: Failed to allocate TLB hash table\n",
215 bond->dev->name);
216 _unlock_tx_hashtbl(bond);
217 return -1;
220 memset(bond_info->tx_hashtbl, 0, size);
222 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
223 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
226 _unlock_tx_hashtbl(bond);
228 return 0;
231 /* Must be called only after all slaves have been released */
232 static void tlb_deinitialize(struct bonding *bond)
234 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
236 _lock_tx_hashtbl(bond);
238 kfree(bond_info->tx_hashtbl);
239 bond_info->tx_hashtbl = NULL;
241 _unlock_tx_hashtbl(bond);
244 /* Caller must hold bond lock for read */
245 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
247 struct slave *slave, *least_loaded;
248 s64 max_gap;
249 int i, found = 0;
251 /* Find the first enabled slave */
252 bond_for_each_slave(bond, slave, i) {
253 if (SLAVE_IS_OK(slave)) {
254 found = 1;
255 break;
259 if (!found) {
260 return NULL;
263 least_loaded = slave;
264 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
265 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
267 /* Find the slave with the largest gap */
268 bond_for_each_slave_from(bond, slave, i, least_loaded) {
269 if (SLAVE_IS_OK(slave)) {
270 s64 gap = (s64)(slave->speed << 20) -
271 (s64)(SLAVE_TLB_INFO(slave).load << 3);
272 if (max_gap < gap) {
273 least_loaded = slave;
274 max_gap = gap;
279 return least_loaded;
282 /* Caller must hold bond lock for read */
283 struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
285 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
286 struct tlb_client_info *hash_table;
287 struct slave *assigned_slave;
289 _lock_tx_hashtbl(bond);
291 hash_table = bond_info->tx_hashtbl;
292 assigned_slave = hash_table[hash_index].tx_slave;
293 if (!assigned_slave) {
294 assigned_slave = tlb_get_least_loaded_slave(bond);
296 if (assigned_slave) {
297 struct tlb_slave_info *slave_info =
298 &(SLAVE_TLB_INFO(assigned_slave));
299 u32 next_index = slave_info->head;
301 hash_table[hash_index].tx_slave = assigned_slave;
302 hash_table[hash_index].next = next_index;
303 hash_table[hash_index].prev = TLB_NULL_INDEX;
305 if (next_index != TLB_NULL_INDEX) {
306 hash_table[next_index].prev = hash_index;
309 slave_info->head = hash_index;
310 slave_info->load +=
311 hash_table[hash_index].load_history;
315 if (assigned_slave) {
316 hash_table[hash_index].tx_bytes += skb_len;
319 _unlock_tx_hashtbl(bond);
321 return assigned_slave;
324 /*********************** rlb specific functions ***************************/
325 static inline void _lock_rx_hashtbl(struct bonding *bond)
327 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
330 static inline void _unlock_rx_hashtbl(struct bonding *bond)
332 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
335 /* when an ARP REPLY is received from a client update its info
336 * in the rx_hashtbl
338 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
340 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
341 struct rlb_client_info *client_info;
342 u32 hash_index;
344 _lock_rx_hashtbl(bond);
346 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
347 client_info = &(bond_info->rx_hashtbl[hash_index]);
349 if ((client_info->assigned) &&
350 (client_info->ip_src == arp->ip_dst) &&
351 (client_info->ip_dst == arp->ip_src)) {
352 /* update the clients MAC address */
353 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
354 client_info->ntt = 1;
355 bond_info->rx_ntt = 1;
358 _unlock_rx_hashtbl(bond);
361 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype)
363 struct bonding *bond = bond_dev->priv;
364 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
365 int res = NET_RX_DROP;
367 if (!(bond_dev->flags & IFF_MASTER)) {
368 goto out;
371 if (!arp) {
372 dprintk("Packet has no ARP data\n");
373 goto out;
376 if (skb->len < sizeof(struct arp_pkt)) {
377 dprintk("Packet is too small to be an ARP\n");
378 goto out;
381 if (arp->op_code == htons(ARPOP_REPLY)) {
382 /* update rx hash table for this ARP */
383 rlb_update_entry_from_arp(bond, arp);
384 dprintk("Server received an ARP Reply from client\n");
387 res = NET_RX_SUCCESS;
389 out:
390 dev_kfree_skb(skb);
392 return res;
395 /* Caller must hold bond lock for read */
396 static struct slave *rlb_next_rx_slave(struct bonding *bond)
398 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
399 struct slave *rx_slave, *slave, *start_at;
400 int i = 0;
402 if (bond_info->next_rx_slave) {
403 start_at = bond_info->next_rx_slave;
404 } else {
405 start_at = bond->first_slave;
408 rx_slave = NULL;
410 bond_for_each_slave_from(bond, slave, i, start_at) {
411 if (SLAVE_IS_OK(slave)) {
412 if (!rx_slave) {
413 rx_slave = slave;
414 } else if (slave->speed > rx_slave->speed) {
415 rx_slave = slave;
420 if (rx_slave) {
421 bond_info->next_rx_slave = rx_slave->next;
424 return rx_slave;
427 /* teach the switch the mac of a disabled slave
428 * on the primary for fault tolerance
430 * Caller must hold bond->curr_slave_lock for write or bond lock for write
432 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
434 if (!bond->curr_active_slave) {
435 return;
438 if (!bond->alb_info.primary_is_promisc) {
439 bond->alb_info.primary_is_promisc = 1;
440 dev_set_promiscuity(bond->curr_active_slave->dev, 1);
443 bond->alb_info.rlb_promisc_timeout_counter = 0;
445 alb_send_learning_packets(bond->curr_active_slave, addr);
448 /* slave being removed should not be active at this point
450 * Caller must hold bond lock for read
452 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
454 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
455 struct rlb_client_info *rx_hash_table;
456 u32 index, next_index;
458 /* clear slave from rx_hashtbl */
459 _lock_rx_hashtbl(bond);
461 rx_hash_table = bond_info->rx_hashtbl;
462 index = bond_info->rx_hashtbl_head;
463 for (; index != RLB_NULL_INDEX; index = next_index) {
464 next_index = rx_hash_table[index].next;
465 if (rx_hash_table[index].slave == slave) {
466 struct slave *assigned_slave = rlb_next_rx_slave(bond);
468 if (assigned_slave) {
469 rx_hash_table[index].slave = assigned_slave;
470 if (memcmp(rx_hash_table[index].mac_dst,
471 mac_bcast, ETH_ALEN)) {
472 bond_info->rx_hashtbl[index].ntt = 1;
473 bond_info->rx_ntt = 1;
474 /* A slave has been removed from the
475 * table because it is either disabled
476 * or being released. We must retry the
477 * update to avoid clients from not
478 * being updated & disconnecting when
479 * there is stress
481 bond_info->rlb_update_retry_counter =
482 RLB_UPDATE_RETRY;
484 } else { /* there is no active slave */
485 rx_hash_table[index].slave = NULL;
490 _unlock_rx_hashtbl(bond);
492 write_lock(&bond->curr_slave_lock);
494 if (slave != bond->curr_active_slave) {
495 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
498 write_unlock(&bond->curr_slave_lock);
501 static void rlb_update_client(struct rlb_client_info *client_info)
503 int i;
505 if (!client_info->slave) {
506 return;
509 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
510 struct sk_buff *skb;
512 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
513 client_info->ip_dst,
514 client_info->slave->dev,
515 client_info->ip_src,
516 client_info->mac_dst,
517 client_info->slave->dev->dev_addr,
518 client_info->mac_dst);
519 if (!skb) {
520 printk(KERN_ERR DRV_NAME
521 ": Error: failed to create an ARP packet\n");
522 continue;
525 skb->dev = client_info->slave->dev;
527 if (client_info->tag) {
528 skb = vlan_put_tag(skb, client_info->vlan_id);
529 if (!skb) {
530 printk(KERN_ERR DRV_NAME
531 ": Error: failed to insert VLAN tag\n");
532 continue;
536 arp_xmit(skb);
540 /* sends ARP REPLIES that update the clients that need updating */
541 static void rlb_update_rx_clients(struct bonding *bond)
543 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
544 struct rlb_client_info *client_info;
545 u32 hash_index;
547 _lock_rx_hashtbl(bond);
549 hash_index = bond_info->rx_hashtbl_head;
550 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
551 client_info = &(bond_info->rx_hashtbl[hash_index]);
552 if (client_info->ntt) {
553 rlb_update_client(client_info);
554 if (bond_info->rlb_update_retry_counter == 0) {
555 client_info->ntt = 0;
560 /* do not update the entries again untill this counter is zero so that
561 * not to confuse the clients.
563 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
565 _unlock_rx_hashtbl(bond);
568 /* The slave was assigned a new mac address - update the clients */
569 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
571 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
572 struct rlb_client_info *client_info;
573 int ntt = 0;
574 u32 hash_index;
576 _lock_rx_hashtbl(bond);
578 hash_index = bond_info->rx_hashtbl_head;
579 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
580 client_info = &(bond_info->rx_hashtbl[hash_index]);
582 if ((client_info->slave == slave) &&
583 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
584 client_info->ntt = 1;
585 ntt = 1;
589 // update the team's flag only after the whole iteration
590 if (ntt) {
591 bond_info->rx_ntt = 1;
592 //fasten the change
593 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
596 _unlock_rx_hashtbl(bond);
599 /* mark all clients using src_ip to be updated */
600 static void rlb_req_update_subnet_clients(struct bonding *bond, u32 src_ip)
602 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
603 struct rlb_client_info *client_info;
604 u32 hash_index;
606 _lock_rx_hashtbl(bond);
608 hash_index = bond_info->rx_hashtbl_head;
609 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
610 client_info = &(bond_info->rx_hashtbl[hash_index]);
612 if (!client_info->slave) {
613 printk(KERN_ERR DRV_NAME
614 ": Error: found a client with no channel in "
615 "the client's hash table\n");
616 continue;
618 /*update all clients using this src_ip, that are not assigned
619 * to the team's address (curr_active_slave) and have a known
620 * unicast mac address.
622 if ((client_info->ip_src == src_ip) &&
623 memcmp(client_info->slave->dev->dev_addr,
624 bond->dev->dev_addr, ETH_ALEN) &&
625 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
626 client_info->ntt = 1;
627 bond_info->rx_ntt = 1;
631 _unlock_rx_hashtbl(bond);
634 /* Caller must hold both bond and ptr locks for read */
635 struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
637 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
638 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
639 struct slave *assigned_slave;
640 struct rlb_client_info *client_info;
641 u32 hash_index = 0;
643 _lock_rx_hashtbl(bond);
645 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
646 client_info = &(bond_info->rx_hashtbl[hash_index]);
648 if (client_info->assigned) {
649 if ((client_info->ip_src == arp->ip_src) &&
650 (client_info->ip_dst == arp->ip_dst)) {
651 /* the entry is already assigned to this client */
652 if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
653 /* update mac address from arp */
654 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
657 assigned_slave = client_info->slave;
658 if (assigned_slave) {
659 _unlock_rx_hashtbl(bond);
660 return assigned_slave;
662 } else {
663 /* the entry is already assigned to some other client,
664 * move the old client to primary (curr_active_slave) so
665 * that the new client can be assigned to this entry.
667 if (bond->curr_active_slave &&
668 client_info->slave != bond->curr_active_slave) {
669 client_info->slave = bond->curr_active_slave;
670 rlb_update_client(client_info);
674 /* assign a new slave */
675 assigned_slave = rlb_next_rx_slave(bond);
677 if (assigned_slave) {
678 client_info->ip_src = arp->ip_src;
679 client_info->ip_dst = arp->ip_dst;
680 /* arp->mac_dst is broadcast for arp reqeusts.
681 * will be updated with clients actual unicast mac address
682 * upon receiving an arp reply.
684 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
685 client_info->slave = assigned_slave;
687 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
688 client_info->ntt = 1;
689 bond->alb_info.rx_ntt = 1;
690 } else {
691 client_info->ntt = 0;
694 if (!list_empty(&bond->vlan_list)) {
695 unsigned short vlan_id;
696 int res = vlan_get_tag(skb, &vlan_id);
697 if (!res) {
698 client_info->tag = 1;
699 client_info->vlan_id = vlan_id;
703 if (!client_info->assigned) {
704 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
705 bond_info->rx_hashtbl_head = hash_index;
706 client_info->next = prev_tbl_head;
707 if (prev_tbl_head != RLB_NULL_INDEX) {
708 bond_info->rx_hashtbl[prev_tbl_head].prev =
709 hash_index;
711 client_info->assigned = 1;
715 _unlock_rx_hashtbl(bond);
717 return assigned_slave;
720 /* chooses (and returns) transmit channel for arp reply
721 * does not choose channel for other arp types since they are
722 * sent on the curr_active_slave
724 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
726 struct arp_pkt *arp = (struct arp_pkt *)skb->nh.raw;
727 struct slave *tx_slave = NULL;
729 if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
730 /* the arp must be sent on the selected
731 * rx channel
733 tx_slave = rlb_choose_channel(skb, bond);
734 if (tx_slave) {
735 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
737 dprintk("Server sent ARP Reply packet\n");
738 } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
739 /* Create an entry in the rx_hashtbl for this client as a
740 * place holder.
741 * When the arp reply is received the entry will be updated
742 * with the correct unicast address of the client.
744 rlb_choose_channel(skb, bond);
746 /* The ARP relpy packets must be delayed so that
747 * they can cancel out the influence of the ARP request.
749 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
751 /* arp requests are broadcast and are sent on the primary
752 * the arp request will collapse all clients on the subnet to
753 * the primary slave. We must register these clients to be
754 * updated with their assigned mac.
756 rlb_req_update_subnet_clients(bond, arp->ip_src);
757 dprintk("Server sent ARP Request packet\n");
760 return tx_slave;
763 /* Caller must hold bond lock for read */
764 static void rlb_rebalance(struct bonding *bond)
766 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
767 struct slave *assigned_slave;
768 struct rlb_client_info *client_info;
769 int ntt;
770 u32 hash_index;
772 _lock_rx_hashtbl(bond);
774 ntt = 0;
775 hash_index = bond_info->rx_hashtbl_head;
776 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
777 client_info = &(bond_info->rx_hashtbl[hash_index]);
778 assigned_slave = rlb_next_rx_slave(bond);
779 if (assigned_slave && (client_info->slave != assigned_slave)) {
780 client_info->slave = assigned_slave;
781 client_info->ntt = 1;
782 ntt = 1;
786 /* update the team's flag only after the whole iteration */
787 if (ntt) {
788 bond_info->rx_ntt = 1;
790 _unlock_rx_hashtbl(bond);
793 /* Caller must hold rx_hashtbl lock */
794 static void rlb_init_table_entry(struct rlb_client_info *entry)
796 memset(entry, 0, sizeof(struct rlb_client_info));
797 entry->next = RLB_NULL_INDEX;
798 entry->prev = RLB_NULL_INDEX;
801 static int rlb_initialize(struct bonding *bond)
803 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
804 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
805 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
806 int i;
808 spin_lock_init(&(bond_info->rx_hashtbl_lock));
810 _lock_rx_hashtbl(bond);
812 bond_info->rx_hashtbl = kmalloc(size, GFP_KERNEL);
813 if (!bond_info->rx_hashtbl) {
814 printk(KERN_ERR DRV_NAME
815 ": Error: %s: Failed to allocate RLB hash table\n",
816 bond->dev->name);
817 _unlock_rx_hashtbl(bond);
818 return -1;
821 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
823 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
824 rlb_init_table_entry(bond_info->rx_hashtbl + i);
827 _unlock_rx_hashtbl(bond);
829 /*initialize packet type*/
830 pk_type->type = __constant_htons(ETH_P_ARP);
831 pk_type->dev = bond->dev;
832 pk_type->func = rlb_arp_recv;
833 pk_type->data = (void*)1; /* understand shared skbs */
835 /* register to receive ARPs */
836 dev_add_pack(pk_type);
838 return 0;
841 static void rlb_deinitialize(struct bonding *bond)
843 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
845 dev_remove_pack(&(bond_info->rlb_pkt_type));
847 _lock_rx_hashtbl(bond);
849 kfree(bond_info->rx_hashtbl);
850 bond_info->rx_hashtbl = NULL;
851 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
853 _unlock_rx_hashtbl(bond);
856 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
858 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
859 u32 curr_index;
861 _lock_rx_hashtbl(bond);
863 curr_index = bond_info->rx_hashtbl_head;
864 while (curr_index != RLB_NULL_INDEX) {
865 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
866 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
867 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
869 if (curr->tag && (curr->vlan_id == vlan_id)) {
870 if (curr_index == bond_info->rx_hashtbl_head) {
871 bond_info->rx_hashtbl_head = next_index;
873 if (prev_index != RLB_NULL_INDEX) {
874 bond_info->rx_hashtbl[prev_index].next = next_index;
876 if (next_index != RLB_NULL_INDEX) {
877 bond_info->rx_hashtbl[next_index].prev = prev_index;
880 rlb_init_table_entry(curr);
883 curr_index = next_index;
886 _unlock_rx_hashtbl(bond);
889 /*********************** tlb/rlb shared functions *********************/
891 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
893 struct bonding *bond = bond_get_bond_by_slave(slave);
894 struct learning_pkt pkt;
895 int size = sizeof(struct learning_pkt);
896 int i;
898 memset(&pkt, 0, size);
899 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
900 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
901 pkt.type = __constant_htons(ETH_P_LOOP);
903 for (i = 0; i < MAX_LP_BURST; i++) {
904 struct sk_buff *skb;
905 char *data;
907 skb = dev_alloc_skb(size);
908 if (!skb) {
909 return;
912 data = skb_put(skb, size);
913 memcpy(data, &pkt, size);
915 skb->mac.raw = data;
916 skb->nh.raw = data + ETH_HLEN;
917 skb->protocol = pkt.type;
918 skb->priority = TC_PRIO_CONTROL;
919 skb->dev = slave->dev;
921 if (!list_empty(&bond->vlan_list)) {
922 struct vlan_entry *vlan;
924 vlan = bond_next_vlan(bond,
925 bond->alb_info.current_alb_vlan);
927 bond->alb_info.current_alb_vlan = vlan;
928 if (!vlan) {
929 kfree_skb(skb);
930 continue;
933 skb = vlan_put_tag(skb, vlan->vlan_id);
934 if (!skb) {
935 printk(KERN_ERR DRV_NAME
936 ": Error: failed to insert VLAN tag\n");
937 continue;
941 dev_queue_xmit(skb);
945 /* hw is a boolean parameter that determines whether we should try and
946 * set the hw address of the device as well as the hw address of the
947 * net_device
949 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
951 struct net_device *dev = slave->dev;
952 struct sockaddr s_addr;
954 if (!hw) {
955 memcpy(dev->dev_addr, addr, dev->addr_len);
956 return 0;
959 /* for rlb each slave must have a unique hw mac addresses so that */
960 /* each slave will receive packets destined to a different mac */
961 memcpy(s_addr.sa_data, addr, dev->addr_len);
962 s_addr.sa_family = dev->type;
963 if (dev->set_mac_address(dev, &s_addr)) {
964 printk(KERN_ERR DRV_NAME
965 ": Error: dev->set_mac_address of dev %s failed! ALB "
966 "mode requires that the base driver support setting "
967 "the hw address also when the network device's "
968 "interface is open\n",
969 dev->name);
970 return -EOPNOTSUPP;
972 return 0;
975 /* Caller must hold bond lock for write or curr_slave_lock for write*/
976 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
978 struct slave *disabled_slave = NULL;
979 u8 tmp_mac_addr[ETH_ALEN];
980 int slaves_state_differ;
982 slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
984 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
985 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
986 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
988 /* fasten the change in the switch */
989 if (SLAVE_IS_OK(slave1)) {
990 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
991 if (bond->alb_info.rlb_enabled) {
992 /* inform the clients that the mac address
993 * has changed
995 rlb_req_update_slave_clients(bond, slave1);
997 } else {
998 disabled_slave = slave1;
1001 if (SLAVE_IS_OK(slave2)) {
1002 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1003 if (bond->alb_info.rlb_enabled) {
1004 /* inform the clients that the mac address
1005 * has changed
1007 rlb_req_update_slave_clients(bond, slave2);
1009 } else {
1010 disabled_slave = slave2;
1013 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1014 /* A disabled slave was assigned an active mac addr */
1015 rlb_teach_disabled_mac_on_primary(bond,
1016 disabled_slave->dev->dev_addr);
1021 * alb_change_hw_addr_on_detach
1022 * @bond: bonding we're working on
1023 * @slave: the slave that was just detached
1025 * We assume that @slave was already detached from the slave list.
1027 * If @slave's permanent hw address is different both from its current
1028 * address and from @bond's address, then somewhere in the bond there's
1029 * a slave that has @slave's permanet address as its current address.
1030 * We'll make sure that that slave no longer uses @slave's permanent address.
1032 * Caller must hold bond lock
1034 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1036 int perm_curr_diff;
1037 int perm_bond_diff;
1039 perm_curr_diff = memcmp(slave->perm_hwaddr,
1040 slave->dev->dev_addr,
1041 ETH_ALEN);
1042 perm_bond_diff = memcmp(slave->perm_hwaddr,
1043 bond->dev->dev_addr,
1044 ETH_ALEN);
1046 if (perm_curr_diff && perm_bond_diff) {
1047 struct slave *tmp_slave;
1048 int i, found = 0;
1050 bond_for_each_slave(bond, tmp_slave, i) {
1051 if (!memcmp(slave->perm_hwaddr,
1052 tmp_slave->dev->dev_addr,
1053 ETH_ALEN)) {
1054 found = 1;
1055 break;
1059 if (found) {
1060 alb_swap_mac_addr(bond, slave, tmp_slave);
1066 * alb_handle_addr_collision_on_attach
1067 * @bond: bonding we're working on
1068 * @slave: the slave that was just attached
1070 * checks uniqueness of slave's mac address and handles the case the
1071 * new slave uses the bonds mac address.
1073 * If the permanent hw address of @slave is @bond's hw address, we need to
1074 * find a different hw address to give @slave, that isn't in use by any other
1075 * slave in the bond. This address must be, of course, one of the premanent
1076 * addresses of the other slaves.
1078 * We go over the slave list, and for each slave there we compare its
1079 * permanent hw address with the current address of all the other slaves.
1080 * If no match was found, then we've found a slave with a permanent address
1081 * that isn't used by any other slave in the bond, so we can assign it to
1082 * @slave.
1084 * assumption: this function is called before @slave is attached to the
1085 * bond slave list.
1087 * caller must hold the bond lock for write since the mac addresses are compared
1088 * and may be swapped.
1090 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1092 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1093 struct slave *has_bond_addr = bond->curr_active_slave;
1094 int i, j, found = 0;
1096 if (bond->slave_cnt == 0) {
1097 /* this is the first slave */
1098 return 0;
1101 /* if slave's mac address differs from bond's mac address
1102 * check uniqueness of slave's mac address against the other
1103 * slaves in the bond.
1105 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1106 bond_for_each_slave(bond, tmp_slave1, i) {
1107 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1108 ETH_ALEN)) {
1109 found = 1;
1110 break;
1114 if (found) {
1115 /* a slave was found that is using the mac address
1116 * of the new slave
1118 printk(KERN_ERR DRV_NAME
1119 ": Error: the hw address of slave %s is not "
1120 "unique - cannot enslave it!",
1121 slave->dev->name);
1122 return -EINVAL;
1125 return 0;
1128 /* The slave's address is equal to the address of the bond.
1129 * Search for a spare address in the bond for this slave.
1131 free_mac_slave = NULL;
1133 bond_for_each_slave(bond, tmp_slave1, i) {
1134 found = 0;
1135 bond_for_each_slave(bond, tmp_slave2, j) {
1136 if (!memcmp(tmp_slave1->perm_hwaddr,
1137 tmp_slave2->dev->dev_addr,
1138 ETH_ALEN)) {
1139 found = 1;
1140 break;
1144 if (!found) {
1145 /* no slave has tmp_slave1's perm addr
1146 * as its curr addr
1148 free_mac_slave = tmp_slave1;
1149 break;
1152 if (!has_bond_addr) {
1153 if (!memcmp(tmp_slave1->dev->dev_addr,
1154 bond->dev->dev_addr,
1155 ETH_ALEN)) {
1157 has_bond_addr = tmp_slave1;
1162 if (free_mac_slave) {
1163 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1164 bond->alb_info.rlb_enabled);
1166 printk(KERN_WARNING DRV_NAME
1167 ": Warning: the hw address of slave %s is in use by "
1168 "the bond; giving it the hw address of %s\n",
1169 slave->dev->name, free_mac_slave->dev->name);
1171 } else if (has_bond_addr) {
1172 printk(KERN_ERR DRV_NAME
1173 ": Error: the hw address of slave %s is in use by the "
1174 "bond; couldn't find a slave with a free hw address to "
1175 "give it (this should not have happened)\n",
1176 slave->dev->name);
1177 return -EFAULT;
1180 return 0;
1184 * alb_set_mac_address
1185 * @bond:
1186 * @addr:
1188 * In TLB mode all slaves are configured to the bond's hw address, but set
1189 * their dev_addr field to different addresses (based on their permanent hw
1190 * addresses).
1192 * For each slave, this function sets the interface to the new address and then
1193 * changes its dev_addr field to its previous value.
1195 * Unwinding assumes bond's mac address has not yet changed.
1197 static int alb_set_mac_address(struct bonding *bond, void *addr)
1199 struct sockaddr sa;
1200 struct slave *slave, *stop_at;
1201 char tmp_addr[ETH_ALEN];
1202 int res;
1203 int i;
1205 if (bond->alb_info.rlb_enabled) {
1206 return 0;
1209 bond_for_each_slave(bond, slave, i) {
1210 if (slave->dev->set_mac_address == NULL) {
1211 res = -EOPNOTSUPP;
1212 goto unwind;
1215 /* save net_device's current hw address */
1216 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1218 res = slave->dev->set_mac_address(slave->dev, addr);
1220 /* restore net_device's hw address */
1221 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1223 if (res) {
1224 goto unwind;
1228 return 0;
1230 unwind:
1231 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1232 sa.sa_family = bond->dev->type;
1234 /* unwind from head to the slave that failed */
1235 stop_at = slave;
1236 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1237 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1238 slave->dev->set_mac_address(slave->dev, &sa);
1239 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1242 return res;
1245 /************************ exported alb funcions ************************/
1247 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1249 int res;
1251 res = tlb_initialize(bond);
1252 if (res) {
1253 return res;
1256 if (rlb_enabled) {
1257 bond->alb_info.rlb_enabled = 1;
1258 /* initialize rlb */
1259 res = rlb_initialize(bond);
1260 if (res) {
1261 tlb_deinitialize(bond);
1262 return res;
1266 return 0;
1269 void bond_alb_deinitialize(struct bonding *bond)
1271 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1273 tlb_deinitialize(bond);
1275 if (bond_info->rlb_enabled) {
1276 rlb_deinitialize(bond);
1280 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1282 struct bonding *bond = bond_dev->priv;
1283 struct ethhdr *eth_data = (struct ethhdr *)(skb->mac.raw = skb->data);
1284 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1285 struct slave *tx_slave = NULL;
1286 static u32 ip_bcast = 0xffffffff;
1287 int hash_size = 0;
1288 int do_tx_balance = 1;
1289 u32 hash_index = 0;
1290 u8 *hash_start = NULL;
1291 int res = 1;
1293 /* make sure that the curr_active_slave and the slaves list do
1294 * not change during tx
1296 read_lock(&bond->lock);
1297 read_lock(&bond->curr_slave_lock);
1299 if (!BOND_IS_OK(bond)) {
1300 goto out;
1303 switch (ntohs(skb->protocol)) {
1304 case ETH_P_IP:
1305 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1306 (skb->nh.iph->daddr == ip_bcast)) {
1307 do_tx_balance = 0;
1308 break;
1310 hash_start = (char*)&(skb->nh.iph->daddr);
1311 hash_size = sizeof(skb->nh.iph->daddr);
1312 break;
1313 case ETH_P_IPV6:
1314 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1315 do_tx_balance = 0;
1316 break;
1319 hash_start = (char*)&(skb->nh.ipv6h->daddr);
1320 hash_size = sizeof(skb->nh.ipv6h->daddr);
1321 break;
1322 case ETH_P_IPX:
1323 if (skb->nh.ipxh->ipx_checksum !=
1324 __constant_htons(IPX_NO_CHECKSUM)) {
1325 /* something is wrong with this packet */
1326 do_tx_balance = 0;
1327 break;
1330 if (skb->nh.ipxh->ipx_type != IPX_TYPE_NCP) {
1331 /* The only protocol worth balancing in
1332 * this family since it has an "ARP" like
1333 * mechanism
1335 do_tx_balance = 0;
1336 break;
1339 hash_start = (char*)eth_data->h_dest;
1340 hash_size = ETH_ALEN;
1341 break;
1342 case ETH_P_ARP:
1343 do_tx_balance = 0;
1344 if (bond_info->rlb_enabled) {
1345 tx_slave = rlb_arp_xmit(skb, bond);
1347 break;
1348 default:
1349 do_tx_balance = 0;
1350 break;
1353 if (do_tx_balance) {
1354 hash_index = _simple_hash(hash_start, hash_size);
1355 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1358 if (!tx_slave) {
1359 /* unbalanced or unassigned, send through primary */
1360 tx_slave = bond->curr_active_slave;
1361 bond_info->unbalanced_load += skb->len;
1364 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1365 if (tx_slave != bond->curr_active_slave) {
1366 memcpy(eth_data->h_source,
1367 tx_slave->dev->dev_addr,
1368 ETH_ALEN);
1371 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1372 } else {
1373 if (tx_slave) {
1374 tlb_clear_slave(bond, tx_slave, 0);
1378 out:
1379 if (res) {
1380 /* no suitable interface, frame not sent */
1381 dev_kfree_skb(skb);
1383 read_unlock(&bond->curr_slave_lock);
1384 read_unlock(&bond->lock);
1385 return 0;
1388 void bond_alb_monitor(struct bonding *bond)
1390 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1391 struct slave *slave;
1392 int i;
1394 read_lock(&bond->lock);
1396 if (bond->kill_timers) {
1397 goto out;
1400 if (bond->slave_cnt == 0) {
1401 bond_info->tx_rebalance_counter = 0;
1402 bond_info->lp_counter = 0;
1403 goto re_arm;
1406 bond_info->tx_rebalance_counter++;
1407 bond_info->lp_counter++;
1409 /* send learning packets */
1410 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1411 /* change of curr_active_slave involves swapping of mac addresses.
1412 * in order to avoid this swapping from happening while
1413 * sending the learning packets, the curr_slave_lock must be held for
1414 * read.
1416 read_lock(&bond->curr_slave_lock);
1418 bond_for_each_slave(bond, slave, i) {
1419 alb_send_learning_packets(slave,slave->dev->dev_addr);
1422 read_unlock(&bond->curr_slave_lock);
1424 bond_info->lp_counter = 0;
1427 /* rebalance tx traffic */
1428 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1430 read_lock(&bond->curr_slave_lock);
1432 bond_for_each_slave(bond, slave, i) {
1433 tlb_clear_slave(bond, slave, 1);
1434 if (slave == bond->curr_active_slave) {
1435 SLAVE_TLB_INFO(slave).load =
1436 bond_info->unbalanced_load /
1437 BOND_TLB_REBALANCE_INTERVAL;
1438 bond_info->unbalanced_load = 0;
1442 read_unlock(&bond->curr_slave_lock);
1444 bond_info->tx_rebalance_counter = 0;
1447 /* handle rlb stuff */
1448 if (bond_info->rlb_enabled) {
1449 /* the following code changes the promiscuity of the
1450 * the curr_active_slave. It needs to be locked with a
1451 * write lock to protect from other code that also
1452 * sets the promiscuity.
1454 write_lock(&bond->curr_slave_lock);
1456 if (bond_info->primary_is_promisc &&
1457 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1459 bond_info->rlb_promisc_timeout_counter = 0;
1461 /* If the primary was set to promiscuous mode
1462 * because a slave was disabled then
1463 * it can now leave promiscuous mode.
1465 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1466 bond_info->primary_is_promisc = 0;
1469 write_unlock(&bond->curr_slave_lock);
1471 if (bond_info->rlb_rebalance) {
1472 bond_info->rlb_rebalance = 0;
1473 rlb_rebalance(bond);
1476 /* check if clients need updating */
1477 if (bond_info->rx_ntt) {
1478 if (bond_info->rlb_update_delay_counter) {
1479 --bond_info->rlb_update_delay_counter;
1480 } else {
1481 rlb_update_rx_clients(bond);
1482 if (bond_info->rlb_update_retry_counter) {
1483 --bond_info->rlb_update_retry_counter;
1484 } else {
1485 bond_info->rx_ntt = 0;
1491 re_arm:
1492 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1493 out:
1494 read_unlock(&bond->lock);
1497 /* assumption: called before the slave is attached to the bond
1498 * and not locked by the bond lock
1500 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1502 int res;
1504 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1505 bond->alb_info.rlb_enabled);
1506 if (res) {
1507 return res;
1510 /* caller must hold the bond lock for write since the mac addresses
1511 * are compared and may be swapped.
1513 write_lock_bh(&bond->lock);
1515 res = alb_handle_addr_collision_on_attach(bond, slave);
1517 write_unlock_bh(&bond->lock);
1519 if (res) {
1520 return res;
1523 tlb_init_slave(slave);
1525 /* order a rebalance ASAP */
1526 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1528 if (bond->alb_info.rlb_enabled) {
1529 bond->alb_info.rlb_rebalance = 1;
1532 return 0;
1535 /* Caller must hold bond lock for write */
1536 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1538 if (bond->slave_cnt > 1) {
1539 alb_change_hw_addr_on_detach(bond, slave);
1542 tlb_clear_slave(bond, slave, 0);
1544 if (bond->alb_info.rlb_enabled) {
1545 bond->alb_info.next_rx_slave = NULL;
1546 rlb_clear_slave(bond, slave);
1550 /* Caller must hold bond lock for read */
1551 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1553 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1555 if (link == BOND_LINK_DOWN) {
1556 tlb_clear_slave(bond, slave, 0);
1557 if (bond->alb_info.rlb_enabled) {
1558 rlb_clear_slave(bond, slave);
1560 } else if (link == BOND_LINK_UP) {
1561 /* order a rebalance ASAP */
1562 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1563 if (bond->alb_info.rlb_enabled) {
1564 bond->alb_info.rlb_rebalance = 1;
1565 /* If the updelay module parameter is smaller than the
1566 * forwarding delay of the switch the rebalance will
1567 * not work because the rebalance arp replies will
1568 * not be forwarded to the clients..
1575 * bond_alb_handle_active_change - assign new curr_active_slave
1576 * @bond: our bonding struct
1577 * @new_slave: new slave to assign
1579 * Set the bond->curr_active_slave to @new_slave and handle
1580 * mac address swapping and promiscuity changes as needed.
1582 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1584 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1586 struct slave *swap_slave;
1587 int i;
1589 if (bond->curr_active_slave == new_slave) {
1590 return;
1593 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1594 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1595 bond->alb_info.primary_is_promisc = 0;
1596 bond->alb_info.rlb_promisc_timeout_counter = 0;
1599 swap_slave = bond->curr_active_slave;
1600 bond->curr_active_slave = new_slave;
1602 if (!new_slave || (bond->slave_cnt == 0)) {
1603 return;
1606 /* set the new curr_active_slave to the bonds mac address
1607 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1609 if (!swap_slave) {
1610 struct slave *tmp_slave;
1611 /* find slave that is holding the bond's mac address */
1612 bond_for_each_slave(bond, tmp_slave, i) {
1613 if (!memcmp(tmp_slave->dev->dev_addr,
1614 bond->dev->dev_addr, ETH_ALEN)) {
1615 swap_slave = tmp_slave;
1616 break;
1621 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1622 if (swap_slave) {
1623 /* swap mac address */
1624 alb_swap_mac_addr(bond, swap_slave, new_slave);
1625 } else {
1626 /* set the new_slave to the bond mac address */
1627 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1628 bond->alb_info.rlb_enabled);
1629 /* fasten bond mac on new current slave */
1630 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1634 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1636 struct bonding *bond = bond_dev->priv;
1637 struct sockaddr *sa = addr;
1638 struct slave *slave, *swap_slave;
1639 int res;
1640 int i;
1642 if (!is_valid_ether_addr(sa->sa_data)) {
1643 return -EADDRNOTAVAIL;
1646 res = alb_set_mac_address(bond, addr);
1647 if (res) {
1648 return res;
1651 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1653 /* If there is no curr_active_slave there is nothing else to do.
1654 * Otherwise we'll need to pass the new address to it and handle
1655 * duplications.
1657 if (!bond->curr_active_slave) {
1658 return 0;
1661 swap_slave = NULL;
1663 bond_for_each_slave(bond, slave, i) {
1664 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1665 swap_slave = slave;
1666 break;
1670 if (swap_slave) {
1671 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1672 } else {
1673 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1674 bond->alb_info.rlb_enabled);
1676 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1677 if (bond->alb_info.rlb_enabled) {
1678 /* inform clients mac address has changed */
1679 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1683 return 0;
1686 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1688 if (bond->alb_info.current_alb_vlan &&
1689 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1690 bond->alb_info.current_alb_vlan = NULL;
1693 if (bond->alb_info.rlb_enabled) {
1694 rlb_clear_vlan(bond, vlan_id);