2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
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>
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>
60 #include <asm/byteorder.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
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
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
;
106 struct learning_pkt
{
107 u8 mac_dst
[ETH_ALEN
];
108 u8 mac_src
[ETH_ALEN
];
110 u8 padding
[ETH_ZLEN
- ETH_HLEN
];
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 */
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
)
134 for (i
= 0; i
< hash_size
; i
++) {
135 hash
^= hash_start
[i
];
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
)
157 entry
->load_history
= 1 + entry
->tx_bytes
/
158 BOND_TLB_REBALANCE_INTERVAL
;
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
;
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
);
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
);
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",
212 _unlock_tx_hashtbl(bond
);
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
);
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
;
247 /* Find the first enabled slave */
248 bond_for_each_slave(bond
, slave
, i
) {
249 if (SLAVE_IS_OK(slave
)) {
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);
269 least_loaded
= slave
;
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
;
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
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
;
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
)
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
)) {
368 dprintk("Packet has no ARP data\n");
372 if (skb
->len
< sizeof(struct arp_pkt
)) {
373 dprintk("Packet is too small to be an ARP\n");
377 if (arp
->op_code
== htons(ARPOP_REPLY
)) {
378 /* update rx hash table for this ARP */
379 rlb_update_entry_from_arp(bond
, arp
);
380 dprintk("Server received an ARP Reply from client\n");
383 res
= NET_RX_SUCCESS
;
391 /* Caller must hold bond lock for read */
392 static struct slave
*rlb_next_rx_slave(struct bonding
*bond
)
394 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
395 struct slave
*rx_slave
, *slave
, *start_at
;
398 if (bond_info
->next_rx_slave
) {
399 start_at
= bond_info
->next_rx_slave
;
401 start_at
= bond
->first_slave
;
406 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
407 if (SLAVE_IS_OK(slave
)) {
410 } else if (slave
->speed
> rx_slave
->speed
) {
417 bond_info
->next_rx_slave
= rx_slave
->next
;
423 /* teach the switch the mac of a disabled slave
424 * on the primary for fault tolerance
426 * Caller must hold bond->curr_slave_lock for write or bond lock for write
428 static void rlb_teach_disabled_mac_on_primary(struct bonding
*bond
, u8 addr
[])
430 if (!bond
->curr_active_slave
) {
434 if (!bond
->alb_info
.primary_is_promisc
) {
435 bond
->alb_info
.primary_is_promisc
= 1;
436 dev_set_promiscuity(bond
->curr_active_slave
->dev
, 1);
439 bond
->alb_info
.rlb_promisc_timeout_counter
= 0;
441 alb_send_learning_packets(bond
->curr_active_slave
, addr
);
444 /* slave being removed should not be active at this point
446 * Caller must hold bond lock for read
448 static void rlb_clear_slave(struct bonding
*bond
, struct slave
*slave
)
450 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
451 struct rlb_client_info
*rx_hash_table
;
452 u32 index
, next_index
;
454 /* clear slave from rx_hashtbl */
455 _lock_rx_hashtbl(bond
);
457 rx_hash_table
= bond_info
->rx_hashtbl
;
458 index
= bond_info
->rx_hashtbl_head
;
459 for (; index
!= RLB_NULL_INDEX
; index
= next_index
) {
460 next_index
= rx_hash_table
[index
].next
;
461 if (rx_hash_table
[index
].slave
== slave
) {
462 struct slave
*assigned_slave
= rlb_next_rx_slave(bond
);
464 if (assigned_slave
) {
465 rx_hash_table
[index
].slave
= assigned_slave
;
466 if (memcmp(rx_hash_table
[index
].mac_dst
,
467 mac_bcast
, ETH_ALEN
)) {
468 bond_info
->rx_hashtbl
[index
].ntt
= 1;
469 bond_info
->rx_ntt
= 1;
470 /* A slave has been removed from the
471 * table because it is either disabled
472 * or being released. We must retry the
473 * update to avoid clients from not
474 * being updated & disconnecting when
477 bond_info
->rlb_update_retry_counter
=
480 } else { /* there is no active slave */
481 rx_hash_table
[index
].slave
= NULL
;
486 _unlock_rx_hashtbl(bond
);
488 write_lock(&bond
->curr_slave_lock
);
490 if (slave
!= bond
->curr_active_slave
) {
491 rlb_teach_disabled_mac_on_primary(bond
, slave
->dev
->dev_addr
);
494 write_unlock(&bond
->curr_slave_lock
);
497 static void rlb_update_client(struct rlb_client_info
*client_info
)
501 if (!client_info
->slave
) {
505 for (i
= 0; i
< RLB_ARP_BURST_SIZE
; i
++) {
508 skb
= arp_create(ARPOP_REPLY
, ETH_P_ARP
,
510 client_info
->slave
->dev
,
512 client_info
->mac_dst
,
513 client_info
->slave
->dev
->dev_addr
,
514 client_info
->mac_dst
);
516 printk(KERN_ERR DRV_NAME
517 ": Error: failed to create an ARP packet\n");
521 skb
->dev
= client_info
->slave
->dev
;
523 if (client_info
->tag
) {
524 skb
= vlan_put_tag(skb
, client_info
->vlan_id
);
526 printk(KERN_ERR DRV_NAME
527 ": Error: failed to insert VLAN tag\n");
536 /* sends ARP REPLIES that update the clients that need updating */
537 static void rlb_update_rx_clients(struct bonding
*bond
)
539 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
540 struct rlb_client_info
*client_info
;
543 _lock_rx_hashtbl(bond
);
545 hash_index
= bond_info
->rx_hashtbl_head
;
546 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
547 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
548 if (client_info
->ntt
) {
549 rlb_update_client(client_info
);
550 if (bond_info
->rlb_update_retry_counter
== 0) {
551 client_info
->ntt
= 0;
556 /* do not update the entries again untill this counter is zero so that
557 * not to confuse the clients.
559 bond_info
->rlb_update_delay_counter
= RLB_UPDATE_DELAY
;
561 _unlock_rx_hashtbl(bond
);
564 /* The slave was assigned a new mac address - update the clients */
565 static void rlb_req_update_slave_clients(struct bonding
*bond
, struct slave
*slave
)
567 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
568 struct rlb_client_info
*client_info
;
572 _lock_rx_hashtbl(bond
);
574 hash_index
= bond_info
->rx_hashtbl_head
;
575 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
576 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
578 if ((client_info
->slave
== slave
) &&
579 memcmp(client_info
->mac_dst
, mac_bcast
, ETH_ALEN
)) {
580 client_info
->ntt
= 1;
585 // update the team's flag only after the whole iteration
587 bond_info
->rx_ntt
= 1;
589 bond_info
->rlb_update_retry_counter
= RLB_UPDATE_RETRY
;
592 _unlock_rx_hashtbl(bond
);
595 /* mark all clients using src_ip to be updated */
596 static void rlb_req_update_subnet_clients(struct bonding
*bond
, u32 src_ip
)
598 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
599 struct rlb_client_info
*client_info
;
602 _lock_rx_hashtbl(bond
);
604 hash_index
= bond_info
->rx_hashtbl_head
;
605 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
606 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
608 if (!client_info
->slave
) {
609 printk(KERN_ERR DRV_NAME
610 ": Error: found a client with no channel in "
611 "the client's hash table\n");
614 /*update all clients using this src_ip, that are not assigned
615 * to the team's address (curr_active_slave) and have a known
616 * unicast mac address.
618 if ((client_info
->ip_src
== src_ip
) &&
619 memcmp(client_info
->slave
->dev
->dev_addr
,
620 bond
->dev
->dev_addr
, ETH_ALEN
) &&
621 memcmp(client_info
->mac_dst
, mac_bcast
, ETH_ALEN
)) {
622 client_info
->ntt
= 1;
623 bond_info
->rx_ntt
= 1;
627 _unlock_rx_hashtbl(bond
);
630 /* Caller must hold both bond and ptr locks for read */
631 static struct slave
*rlb_choose_channel(struct sk_buff
*skb
, struct bonding
*bond
)
633 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
634 struct arp_pkt
*arp
= (struct arp_pkt
*)skb
->nh
.raw
;
635 struct slave
*assigned_slave
;
636 struct rlb_client_info
*client_info
;
639 _lock_rx_hashtbl(bond
);
641 hash_index
= _simple_hash((u8
*)&arp
->ip_dst
, sizeof(arp
->ip_src
));
642 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
644 if (client_info
->assigned
) {
645 if ((client_info
->ip_src
== arp
->ip_src
) &&
646 (client_info
->ip_dst
== arp
->ip_dst
)) {
647 /* the entry is already assigned to this client */
648 if (memcmp(arp
->mac_dst
, mac_bcast
, ETH_ALEN
)) {
649 /* update mac address from arp */
650 memcpy(client_info
->mac_dst
, arp
->mac_dst
, ETH_ALEN
);
653 assigned_slave
= client_info
->slave
;
654 if (assigned_slave
) {
655 _unlock_rx_hashtbl(bond
);
656 return assigned_slave
;
659 /* the entry is already assigned to some other client,
660 * move the old client to primary (curr_active_slave) so
661 * that the new client can be assigned to this entry.
663 if (bond
->curr_active_slave
&&
664 client_info
->slave
!= bond
->curr_active_slave
) {
665 client_info
->slave
= bond
->curr_active_slave
;
666 rlb_update_client(client_info
);
670 /* assign a new slave */
671 assigned_slave
= rlb_next_rx_slave(bond
);
673 if (assigned_slave
) {
674 client_info
->ip_src
= arp
->ip_src
;
675 client_info
->ip_dst
= arp
->ip_dst
;
676 /* arp->mac_dst is broadcast for arp reqeusts.
677 * will be updated with clients actual unicast mac address
678 * upon receiving an arp reply.
680 memcpy(client_info
->mac_dst
, arp
->mac_dst
, ETH_ALEN
);
681 client_info
->slave
= assigned_slave
;
683 if (memcmp(client_info
->mac_dst
, mac_bcast
, ETH_ALEN
)) {
684 client_info
->ntt
= 1;
685 bond
->alb_info
.rx_ntt
= 1;
687 client_info
->ntt
= 0;
690 if (!list_empty(&bond
->vlan_list
)) {
691 unsigned short vlan_id
;
692 int res
= vlan_get_tag(skb
, &vlan_id
);
694 client_info
->tag
= 1;
695 client_info
->vlan_id
= vlan_id
;
699 if (!client_info
->assigned
) {
700 u32 prev_tbl_head
= bond_info
->rx_hashtbl_head
;
701 bond_info
->rx_hashtbl_head
= hash_index
;
702 client_info
->next
= prev_tbl_head
;
703 if (prev_tbl_head
!= RLB_NULL_INDEX
) {
704 bond_info
->rx_hashtbl
[prev_tbl_head
].prev
=
707 client_info
->assigned
= 1;
711 _unlock_rx_hashtbl(bond
);
713 return assigned_slave
;
716 /* chooses (and returns) transmit channel for arp reply
717 * does not choose channel for other arp types since they are
718 * sent on the curr_active_slave
720 static struct slave
*rlb_arp_xmit(struct sk_buff
*skb
, struct bonding
*bond
)
722 struct arp_pkt
*arp
= (struct arp_pkt
*)skb
->nh
.raw
;
723 struct slave
*tx_slave
= NULL
;
725 if (arp
->op_code
== __constant_htons(ARPOP_REPLY
)) {
726 /* the arp must be sent on the selected
729 tx_slave
= rlb_choose_channel(skb
, bond
);
731 memcpy(arp
->mac_src
,tx_slave
->dev
->dev_addr
, ETH_ALEN
);
733 dprintk("Server sent ARP Reply packet\n");
734 } else if (arp
->op_code
== __constant_htons(ARPOP_REQUEST
)) {
735 /* Create an entry in the rx_hashtbl for this client as a
737 * When the arp reply is received the entry will be updated
738 * with the correct unicast address of the client.
740 rlb_choose_channel(skb
, bond
);
742 /* The ARP relpy packets must be delayed so that
743 * they can cancel out the influence of the ARP request.
745 bond
->alb_info
.rlb_update_delay_counter
= RLB_UPDATE_DELAY
;
747 /* arp requests are broadcast and are sent on the primary
748 * the arp request will collapse all clients on the subnet to
749 * the primary slave. We must register these clients to be
750 * updated with their assigned mac.
752 rlb_req_update_subnet_clients(bond
, arp
->ip_src
);
753 dprintk("Server sent ARP Request packet\n");
759 /* Caller must hold bond lock for read */
760 static void rlb_rebalance(struct bonding
*bond
)
762 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
763 struct slave
*assigned_slave
;
764 struct rlb_client_info
*client_info
;
768 _lock_rx_hashtbl(bond
);
771 hash_index
= bond_info
->rx_hashtbl_head
;
772 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
773 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
774 assigned_slave
= rlb_next_rx_slave(bond
);
775 if (assigned_slave
&& (client_info
->slave
!= assigned_slave
)) {
776 client_info
->slave
= assigned_slave
;
777 client_info
->ntt
= 1;
782 /* update the team's flag only after the whole iteration */
784 bond_info
->rx_ntt
= 1;
786 _unlock_rx_hashtbl(bond
);
789 /* Caller must hold rx_hashtbl lock */
790 static void rlb_init_table_entry(struct rlb_client_info
*entry
)
792 memset(entry
, 0, sizeof(struct rlb_client_info
));
793 entry
->next
= RLB_NULL_INDEX
;
794 entry
->prev
= RLB_NULL_INDEX
;
797 static int rlb_initialize(struct bonding
*bond
)
799 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
800 struct packet_type
*pk_type
= &(BOND_ALB_INFO(bond
).rlb_pkt_type
);
801 int size
= RLB_HASH_TABLE_SIZE
* sizeof(struct rlb_client_info
);
804 spin_lock_init(&(bond_info
->rx_hashtbl_lock
));
806 _lock_rx_hashtbl(bond
);
808 bond_info
->rx_hashtbl
= kmalloc(size
, GFP_KERNEL
);
809 if (!bond_info
->rx_hashtbl
) {
810 printk(KERN_ERR DRV_NAME
811 ": Error: %s: Failed to allocate RLB hash table\n",
813 _unlock_rx_hashtbl(bond
);
817 bond_info
->rx_hashtbl_head
= RLB_NULL_INDEX
;
819 for (i
= 0; i
< RLB_HASH_TABLE_SIZE
; i
++) {
820 rlb_init_table_entry(bond_info
->rx_hashtbl
+ i
);
823 _unlock_rx_hashtbl(bond
);
825 /*initialize packet type*/
826 pk_type
->type
= __constant_htons(ETH_P_ARP
);
827 pk_type
->dev
= bond
->dev
;
828 pk_type
->func
= rlb_arp_recv
;
830 /* register to receive ARPs */
831 dev_add_pack(pk_type
);
836 static void rlb_deinitialize(struct bonding
*bond
)
838 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
840 dev_remove_pack(&(bond_info
->rlb_pkt_type
));
842 _lock_rx_hashtbl(bond
);
844 kfree(bond_info
->rx_hashtbl
);
845 bond_info
->rx_hashtbl
= NULL
;
846 bond_info
->rx_hashtbl_head
= RLB_NULL_INDEX
;
848 _unlock_rx_hashtbl(bond
);
851 static void rlb_clear_vlan(struct bonding
*bond
, unsigned short vlan_id
)
853 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
856 _lock_rx_hashtbl(bond
);
858 curr_index
= bond_info
->rx_hashtbl_head
;
859 while (curr_index
!= RLB_NULL_INDEX
) {
860 struct rlb_client_info
*curr
= &(bond_info
->rx_hashtbl
[curr_index
]);
861 u32 next_index
= bond_info
->rx_hashtbl
[curr_index
].next
;
862 u32 prev_index
= bond_info
->rx_hashtbl
[curr_index
].prev
;
864 if (curr
->tag
&& (curr
->vlan_id
== vlan_id
)) {
865 if (curr_index
== bond_info
->rx_hashtbl_head
) {
866 bond_info
->rx_hashtbl_head
= next_index
;
868 if (prev_index
!= RLB_NULL_INDEX
) {
869 bond_info
->rx_hashtbl
[prev_index
].next
= next_index
;
871 if (next_index
!= RLB_NULL_INDEX
) {
872 bond_info
->rx_hashtbl
[next_index
].prev
= prev_index
;
875 rlb_init_table_entry(curr
);
878 curr_index
= next_index
;
881 _unlock_rx_hashtbl(bond
);
884 /*********************** tlb/rlb shared functions *********************/
886 static void alb_send_learning_packets(struct slave
*slave
, u8 mac_addr
[])
888 struct bonding
*bond
= bond_get_bond_by_slave(slave
);
889 struct learning_pkt pkt
;
890 int size
= sizeof(struct learning_pkt
);
893 memset(&pkt
, 0, size
);
894 memcpy(pkt
.mac_dst
, mac_addr
, ETH_ALEN
);
895 memcpy(pkt
.mac_src
, mac_addr
, ETH_ALEN
);
896 pkt
.type
= __constant_htons(ETH_P_LOOP
);
898 for (i
= 0; i
< MAX_LP_BURST
; i
++) {
902 skb
= dev_alloc_skb(size
);
907 data
= skb_put(skb
, size
);
908 memcpy(data
, &pkt
, size
);
911 skb
->nh
.raw
= data
+ ETH_HLEN
;
912 skb
->protocol
= pkt
.type
;
913 skb
->priority
= TC_PRIO_CONTROL
;
914 skb
->dev
= slave
->dev
;
916 if (!list_empty(&bond
->vlan_list
)) {
917 struct vlan_entry
*vlan
;
919 vlan
= bond_next_vlan(bond
,
920 bond
->alb_info
.current_alb_vlan
);
922 bond
->alb_info
.current_alb_vlan
= vlan
;
928 skb
= vlan_put_tag(skb
, vlan
->vlan_id
);
930 printk(KERN_ERR DRV_NAME
931 ": Error: failed to insert VLAN tag\n");
940 /* hw is a boolean parameter that determines whether we should try and
941 * set the hw address of the device as well as the hw address of the
944 static int alb_set_slave_mac_addr(struct slave
*slave
, u8 addr
[], int hw
)
946 struct net_device
*dev
= slave
->dev
;
947 struct sockaddr s_addr
;
950 memcpy(dev
->dev_addr
, addr
, dev
->addr_len
);
954 /* for rlb each slave must have a unique hw mac addresses so that */
955 /* each slave will receive packets destined to a different mac */
956 memcpy(s_addr
.sa_data
, addr
, dev
->addr_len
);
957 s_addr
.sa_family
= dev
->type
;
958 if (dev_set_mac_address(dev
, &s_addr
)) {
959 printk(KERN_ERR DRV_NAME
960 ": Error: dev_set_mac_address of dev %s failed! ALB "
961 "mode requires that the base driver support setting "
962 "the hw address also when the network device's "
963 "interface is open\n",
970 /* Caller must hold bond lock for write or curr_slave_lock for write*/
971 static void alb_swap_mac_addr(struct bonding
*bond
, struct slave
*slave1
, struct slave
*slave2
)
973 struct slave
*disabled_slave
= NULL
;
974 u8 tmp_mac_addr
[ETH_ALEN
];
975 int slaves_state_differ
;
977 slaves_state_differ
= (SLAVE_IS_OK(slave1
) != SLAVE_IS_OK(slave2
));
979 memcpy(tmp_mac_addr
, slave1
->dev
->dev_addr
, ETH_ALEN
);
980 alb_set_slave_mac_addr(slave1
, slave2
->dev
->dev_addr
, bond
->alb_info
.rlb_enabled
);
981 alb_set_slave_mac_addr(slave2
, tmp_mac_addr
, bond
->alb_info
.rlb_enabled
);
983 /* fasten the change in the switch */
984 if (SLAVE_IS_OK(slave1
)) {
985 alb_send_learning_packets(slave1
, slave1
->dev
->dev_addr
);
986 if (bond
->alb_info
.rlb_enabled
) {
987 /* inform the clients that the mac address
990 rlb_req_update_slave_clients(bond
, slave1
);
993 disabled_slave
= slave1
;
996 if (SLAVE_IS_OK(slave2
)) {
997 alb_send_learning_packets(slave2
, slave2
->dev
->dev_addr
);
998 if (bond
->alb_info
.rlb_enabled
) {
999 /* inform the clients that the mac address
1002 rlb_req_update_slave_clients(bond
, slave2
);
1005 disabled_slave
= slave2
;
1008 if (bond
->alb_info
.rlb_enabled
&& slaves_state_differ
) {
1009 /* A disabled slave was assigned an active mac addr */
1010 rlb_teach_disabled_mac_on_primary(bond
,
1011 disabled_slave
->dev
->dev_addr
);
1016 * alb_change_hw_addr_on_detach
1017 * @bond: bonding we're working on
1018 * @slave: the slave that was just detached
1020 * We assume that @slave was already detached from the slave list.
1022 * If @slave's permanent hw address is different both from its current
1023 * address and from @bond's address, then somewhere in the bond there's
1024 * a slave that has @slave's permanet address as its current address.
1025 * We'll make sure that that slave no longer uses @slave's permanent address.
1027 * Caller must hold bond lock
1029 static void alb_change_hw_addr_on_detach(struct bonding
*bond
, struct slave
*slave
)
1034 perm_curr_diff
= memcmp(slave
->perm_hwaddr
,
1035 slave
->dev
->dev_addr
,
1037 perm_bond_diff
= memcmp(slave
->perm_hwaddr
,
1038 bond
->dev
->dev_addr
,
1041 if (perm_curr_diff
&& perm_bond_diff
) {
1042 struct slave
*tmp_slave
;
1045 bond_for_each_slave(bond
, tmp_slave
, i
) {
1046 if (!memcmp(slave
->perm_hwaddr
,
1047 tmp_slave
->dev
->dev_addr
,
1055 alb_swap_mac_addr(bond
, slave
, tmp_slave
);
1061 * alb_handle_addr_collision_on_attach
1062 * @bond: bonding we're working on
1063 * @slave: the slave that was just attached
1065 * checks uniqueness of slave's mac address and handles the case the
1066 * new slave uses the bonds mac address.
1068 * If the permanent hw address of @slave is @bond's hw address, we need to
1069 * find a different hw address to give @slave, that isn't in use by any other
1070 * slave in the bond. This address must be, of course, one of the premanent
1071 * addresses of the other slaves.
1073 * We go over the slave list, and for each slave there we compare its
1074 * permanent hw address with the current address of all the other slaves.
1075 * If no match was found, then we've found a slave with a permanent address
1076 * that isn't used by any other slave in the bond, so we can assign it to
1079 * assumption: this function is called before @slave is attached to the
1082 * caller must hold the bond lock for write since the mac addresses are compared
1083 * and may be swapped.
1085 static int alb_handle_addr_collision_on_attach(struct bonding
*bond
, struct slave
*slave
)
1087 struct slave
*tmp_slave1
, *tmp_slave2
, *free_mac_slave
;
1088 struct slave
*has_bond_addr
= bond
->curr_active_slave
;
1089 int i
, j
, found
= 0;
1091 if (bond
->slave_cnt
== 0) {
1092 /* this is the first slave */
1096 /* if slave's mac address differs from bond's mac address
1097 * check uniqueness of slave's mac address against the other
1098 * slaves in the bond.
1100 if (memcmp(slave
->perm_hwaddr
, bond
->dev
->dev_addr
, ETH_ALEN
)) {
1101 bond_for_each_slave(bond
, tmp_slave1
, i
) {
1102 if (!memcmp(tmp_slave1
->dev
->dev_addr
, slave
->dev
->dev_addr
,
1110 /* a slave was found that is using the mac address
1113 printk(KERN_ERR DRV_NAME
1114 ": Error: the hw address of slave %s is not "
1115 "unique - cannot enslave it!",
1123 /* The slave's address is equal to the address of the bond.
1124 * Search for a spare address in the bond for this slave.
1126 free_mac_slave
= NULL
;
1128 bond_for_each_slave(bond
, tmp_slave1
, i
) {
1130 bond_for_each_slave(bond
, tmp_slave2
, j
) {
1131 if (!memcmp(tmp_slave1
->perm_hwaddr
,
1132 tmp_slave2
->dev
->dev_addr
,
1140 /* no slave has tmp_slave1's perm addr
1143 free_mac_slave
= tmp_slave1
;
1147 if (!has_bond_addr
) {
1148 if (!memcmp(tmp_slave1
->dev
->dev_addr
,
1149 bond
->dev
->dev_addr
,
1152 has_bond_addr
= tmp_slave1
;
1157 if (free_mac_slave
) {
1158 alb_set_slave_mac_addr(slave
, free_mac_slave
->perm_hwaddr
,
1159 bond
->alb_info
.rlb_enabled
);
1161 printk(KERN_WARNING DRV_NAME
1162 ": Warning: the hw address of slave %s is in use by "
1163 "the bond; giving it the hw address of %s\n",
1164 slave
->dev
->name
, free_mac_slave
->dev
->name
);
1166 } else if (has_bond_addr
) {
1167 printk(KERN_ERR DRV_NAME
1168 ": Error: the hw address of slave %s is in use by the "
1169 "bond; couldn't find a slave with a free hw address to "
1170 "give it (this should not have happened)\n",
1179 * alb_set_mac_address
1183 * In TLB mode all slaves are configured to the bond's hw address, but set
1184 * their dev_addr field to different addresses (based on their permanent hw
1187 * For each slave, this function sets the interface to the new address and then
1188 * changes its dev_addr field to its previous value.
1190 * Unwinding assumes bond's mac address has not yet changed.
1192 static int alb_set_mac_address(struct bonding
*bond
, void *addr
)
1195 struct slave
*slave
, *stop_at
;
1196 char tmp_addr
[ETH_ALEN
];
1200 if (bond
->alb_info
.rlb_enabled
) {
1204 bond_for_each_slave(bond
, slave
, i
) {
1205 if (slave
->dev
->set_mac_address
== NULL
) {
1210 /* save net_device's current hw address */
1211 memcpy(tmp_addr
, slave
->dev
->dev_addr
, ETH_ALEN
);
1213 res
= dev_set_mac_address(slave
->dev
, addr
);
1215 /* restore net_device's hw address */
1216 memcpy(slave
->dev
->dev_addr
, tmp_addr
, ETH_ALEN
);
1226 memcpy(sa
.sa_data
, bond
->dev
->dev_addr
, bond
->dev
->addr_len
);
1227 sa
.sa_family
= bond
->dev
->type
;
1229 /* unwind from head to the slave that failed */
1231 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
1232 memcpy(tmp_addr
, slave
->dev
->dev_addr
, ETH_ALEN
);
1233 dev_set_mac_address(slave
->dev
, &sa
);
1234 memcpy(slave
->dev
->dev_addr
, tmp_addr
, ETH_ALEN
);
1240 /************************ exported alb funcions ************************/
1242 int bond_alb_initialize(struct bonding
*bond
, int rlb_enabled
)
1246 res
= tlb_initialize(bond
);
1252 bond
->alb_info
.rlb_enabled
= 1;
1253 /* initialize rlb */
1254 res
= rlb_initialize(bond
);
1256 tlb_deinitialize(bond
);
1264 void bond_alb_deinitialize(struct bonding
*bond
)
1266 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1268 tlb_deinitialize(bond
);
1270 if (bond_info
->rlb_enabled
) {
1271 rlb_deinitialize(bond
);
1275 int bond_alb_xmit(struct sk_buff
*skb
, struct net_device
*bond_dev
)
1277 struct bonding
*bond
= bond_dev
->priv
;
1278 struct ethhdr
*eth_data
;
1279 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1280 struct slave
*tx_slave
= NULL
;
1281 static u32 ip_bcast
= 0xffffffff;
1283 int do_tx_balance
= 1;
1285 u8
*hash_start
= NULL
;
1288 skb
->mac
.raw
= (unsigned char *)skb
->data
;
1289 eth_data
= eth_hdr(skb
);
1291 /* make sure that the curr_active_slave and the slaves list do
1292 * not change during tx
1294 read_lock(&bond
->lock
);
1295 read_lock(&bond
->curr_slave_lock
);
1297 if (!BOND_IS_OK(bond
)) {
1301 switch (ntohs(skb
->protocol
)) {
1303 if ((memcmp(eth_data
->h_dest
, mac_bcast
, ETH_ALEN
) == 0) ||
1304 (skb
->nh
.iph
->daddr
== ip_bcast
) ||
1305 (skb
->nh
.iph
->protocol
== IPPROTO_IGMP
)) {
1309 hash_start
= (char*)&(skb
->nh
.iph
->daddr
);
1310 hash_size
= sizeof(skb
->nh
.iph
->daddr
);
1313 if (memcmp(eth_data
->h_dest
, mac_bcast
, ETH_ALEN
) == 0) {
1318 hash_start
= (char*)&(skb
->nh
.ipv6h
->daddr
);
1319 hash_size
= sizeof(skb
->nh
.ipv6h
->daddr
);
1322 if (ipx_hdr(skb
)->ipx_checksum
!=
1323 __constant_htons(IPX_NO_CHECKSUM
)) {
1324 /* something is wrong with this packet */
1329 if (ipx_hdr(skb
)->ipx_type
!= IPX_TYPE_NCP
) {
1330 /* The only protocol worth balancing in
1331 * this family since it has an "ARP" like
1338 hash_start
= (char*)eth_data
->h_dest
;
1339 hash_size
= ETH_ALEN
;
1343 if (bond_info
->rlb_enabled
) {
1344 tx_slave
= rlb_arp_xmit(skb
, bond
);
1352 if (do_tx_balance
) {
1353 hash_index
= _simple_hash(hash_start
, hash_size
);
1354 tx_slave
= tlb_choose_channel(bond
, hash_index
, skb
->len
);
1358 /* unbalanced or unassigned, send through primary */
1359 tx_slave
= bond
->curr_active_slave
;
1360 bond_info
->unbalanced_load
+= skb
->len
;
1363 if (tx_slave
&& SLAVE_IS_OK(tx_slave
)) {
1364 if (tx_slave
!= bond
->curr_active_slave
) {
1365 memcpy(eth_data
->h_source
,
1366 tx_slave
->dev
->dev_addr
,
1370 res
= bond_dev_queue_xmit(bond
, skb
, tx_slave
->dev
);
1373 tlb_clear_slave(bond
, tx_slave
, 0);
1379 /* no suitable interface, frame not sent */
1382 read_unlock(&bond
->curr_slave_lock
);
1383 read_unlock(&bond
->lock
);
1387 void bond_alb_monitor(struct bonding
*bond
)
1389 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1390 struct slave
*slave
;
1393 read_lock(&bond
->lock
);
1395 if (bond
->kill_timers
) {
1399 if (bond
->slave_cnt
== 0) {
1400 bond_info
->tx_rebalance_counter
= 0;
1401 bond_info
->lp_counter
= 0;
1405 bond_info
->tx_rebalance_counter
++;
1406 bond_info
->lp_counter
++;
1408 /* send learning packets */
1409 if (bond_info
->lp_counter
>= BOND_ALB_LP_TICKS
) {
1410 /* change of curr_active_slave involves swapping of mac addresses.
1411 * in order to avoid this swapping from happening while
1412 * sending the learning packets, the curr_slave_lock must be held for
1415 read_lock(&bond
->curr_slave_lock
);
1417 bond_for_each_slave(bond
, slave
, i
) {
1418 alb_send_learning_packets(slave
,slave
->dev
->dev_addr
);
1421 read_unlock(&bond
->curr_slave_lock
);
1423 bond_info
->lp_counter
= 0;
1426 /* rebalance tx traffic */
1427 if (bond_info
->tx_rebalance_counter
>= BOND_TLB_REBALANCE_TICKS
) {
1429 read_lock(&bond
->curr_slave_lock
);
1431 bond_for_each_slave(bond
, slave
, i
) {
1432 tlb_clear_slave(bond
, slave
, 1);
1433 if (slave
== bond
->curr_active_slave
) {
1434 SLAVE_TLB_INFO(slave
).load
=
1435 bond_info
->unbalanced_load
/
1436 BOND_TLB_REBALANCE_INTERVAL
;
1437 bond_info
->unbalanced_load
= 0;
1441 read_unlock(&bond
->curr_slave_lock
);
1443 bond_info
->tx_rebalance_counter
= 0;
1446 /* handle rlb stuff */
1447 if (bond_info
->rlb_enabled
) {
1448 /* the following code changes the promiscuity of the
1449 * the curr_active_slave. It needs to be locked with a
1450 * write lock to protect from other code that also
1451 * sets the promiscuity.
1453 write_lock(&bond
->curr_slave_lock
);
1455 if (bond_info
->primary_is_promisc
&&
1456 (++bond_info
->rlb_promisc_timeout_counter
>= RLB_PROMISC_TIMEOUT
)) {
1458 bond_info
->rlb_promisc_timeout_counter
= 0;
1460 /* If the primary was set to promiscuous mode
1461 * because a slave was disabled then
1462 * it can now leave promiscuous mode.
1464 dev_set_promiscuity(bond
->curr_active_slave
->dev
, -1);
1465 bond_info
->primary_is_promisc
= 0;
1468 write_unlock(&bond
->curr_slave_lock
);
1470 if (bond_info
->rlb_rebalance
) {
1471 bond_info
->rlb_rebalance
= 0;
1472 rlb_rebalance(bond
);
1475 /* check if clients need updating */
1476 if (bond_info
->rx_ntt
) {
1477 if (bond_info
->rlb_update_delay_counter
) {
1478 --bond_info
->rlb_update_delay_counter
;
1480 rlb_update_rx_clients(bond
);
1481 if (bond_info
->rlb_update_retry_counter
) {
1482 --bond_info
->rlb_update_retry_counter
;
1484 bond_info
->rx_ntt
= 0;
1491 mod_timer(&(bond_info
->alb_timer
), jiffies
+ alb_delta_in_ticks
);
1493 read_unlock(&bond
->lock
);
1496 /* assumption: called before the slave is attached to the bond
1497 * and not locked by the bond lock
1499 int bond_alb_init_slave(struct bonding
*bond
, struct slave
*slave
)
1503 res
= alb_set_slave_mac_addr(slave
, slave
->perm_hwaddr
,
1504 bond
->alb_info
.rlb_enabled
);
1509 /* caller must hold the bond lock for write since the mac addresses
1510 * are compared and may be swapped.
1512 write_lock_bh(&bond
->lock
);
1514 res
= alb_handle_addr_collision_on_attach(bond
, slave
);
1516 write_unlock_bh(&bond
->lock
);
1522 tlb_init_slave(slave
);
1524 /* order a rebalance ASAP */
1525 bond
->alb_info
.tx_rebalance_counter
= BOND_TLB_REBALANCE_TICKS
;
1527 if (bond
->alb_info
.rlb_enabled
) {
1528 bond
->alb_info
.rlb_rebalance
= 1;
1534 /* Caller must hold bond lock for write */
1535 void bond_alb_deinit_slave(struct bonding
*bond
, struct slave
*slave
)
1537 if (bond
->slave_cnt
> 1) {
1538 alb_change_hw_addr_on_detach(bond
, slave
);
1541 tlb_clear_slave(bond
, slave
, 0);
1543 if (bond
->alb_info
.rlb_enabled
) {
1544 bond
->alb_info
.next_rx_slave
= NULL
;
1545 rlb_clear_slave(bond
, slave
);
1549 /* Caller must hold bond lock for read */
1550 void bond_alb_handle_link_change(struct bonding
*bond
, struct slave
*slave
, char link
)
1552 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1554 if (link
== BOND_LINK_DOWN
) {
1555 tlb_clear_slave(bond
, slave
, 0);
1556 if (bond
->alb_info
.rlb_enabled
) {
1557 rlb_clear_slave(bond
, slave
);
1559 } else if (link
== BOND_LINK_UP
) {
1560 /* order a rebalance ASAP */
1561 bond_info
->tx_rebalance_counter
= BOND_TLB_REBALANCE_TICKS
;
1562 if (bond
->alb_info
.rlb_enabled
) {
1563 bond
->alb_info
.rlb_rebalance
= 1;
1564 /* If the updelay module parameter is smaller than the
1565 * forwarding delay of the switch the rebalance will
1566 * not work because the rebalance arp replies will
1567 * not be forwarded to the clients..
1574 * bond_alb_handle_active_change - assign new curr_active_slave
1575 * @bond: our bonding struct
1576 * @new_slave: new slave to assign
1578 * Set the bond->curr_active_slave to @new_slave and handle
1579 * mac address swapping and promiscuity changes as needed.
1581 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1583 void bond_alb_handle_active_change(struct bonding
*bond
, struct slave
*new_slave
)
1585 struct slave
*swap_slave
;
1588 if (bond
->curr_active_slave
== new_slave
) {
1592 if (bond
->curr_active_slave
&& bond
->alb_info
.primary_is_promisc
) {
1593 dev_set_promiscuity(bond
->curr_active_slave
->dev
, -1);
1594 bond
->alb_info
.primary_is_promisc
= 0;
1595 bond
->alb_info
.rlb_promisc_timeout_counter
= 0;
1598 swap_slave
= bond
->curr_active_slave
;
1599 bond
->curr_active_slave
= new_slave
;
1601 if (!new_slave
|| (bond
->slave_cnt
== 0)) {
1605 /* set the new curr_active_slave to the bonds mac address
1606 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1609 struct slave
*tmp_slave
;
1610 /* find slave that is holding the bond's mac address */
1611 bond_for_each_slave(bond
, tmp_slave
, i
) {
1612 if (!memcmp(tmp_slave
->dev
->dev_addr
,
1613 bond
->dev
->dev_addr
, ETH_ALEN
)) {
1614 swap_slave
= tmp_slave
;
1620 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1622 /* swap mac address */
1623 alb_swap_mac_addr(bond
, swap_slave
, new_slave
);
1625 /* set the new_slave to the bond mac address */
1626 alb_set_slave_mac_addr(new_slave
, bond
->dev
->dev_addr
,
1627 bond
->alb_info
.rlb_enabled
);
1628 /* fasten bond mac on new current slave */
1629 alb_send_learning_packets(new_slave
, bond
->dev
->dev_addr
);
1633 int bond_alb_set_mac_address(struct net_device
*bond_dev
, void *addr
)
1635 struct bonding
*bond
= bond_dev
->priv
;
1636 struct sockaddr
*sa
= addr
;
1637 struct slave
*slave
, *swap_slave
;
1641 if (!is_valid_ether_addr(sa
->sa_data
)) {
1642 return -EADDRNOTAVAIL
;
1645 res
= alb_set_mac_address(bond
, addr
);
1650 memcpy(bond_dev
->dev_addr
, sa
->sa_data
, bond_dev
->addr_len
);
1652 /* If there is no curr_active_slave there is nothing else to do.
1653 * Otherwise we'll need to pass the new address to it and handle
1656 if (!bond
->curr_active_slave
) {
1662 bond_for_each_slave(bond
, slave
, i
) {
1663 if (!memcmp(slave
->dev
->dev_addr
, bond_dev
->dev_addr
, ETH_ALEN
)) {
1670 alb_swap_mac_addr(bond
, swap_slave
, bond
->curr_active_slave
);
1672 alb_set_slave_mac_addr(bond
->curr_active_slave
, bond_dev
->dev_addr
,
1673 bond
->alb_info
.rlb_enabled
);
1675 alb_send_learning_packets(bond
->curr_active_slave
, bond_dev
->dev_addr
);
1676 if (bond
->alb_info
.rlb_enabled
) {
1677 /* inform clients mac address has changed */
1678 rlb_req_update_slave_clients(bond
, bond
->curr_active_slave
);
1685 void bond_alb_clear_vlan(struct bonding
*bond
, unsigned short vlan_id
)
1687 if (bond
->alb_info
.current_alb_vlan
&&
1688 (bond
->alb_info
.current_alb_vlan
->vlan_id
== vlan_id
)) {
1689 bond
->alb_info
.current_alb_vlan
= NULL
;
1692 if (bond
->alb_info
.rlb_enabled
) {
1693 rlb_clear_vlan(bond
, vlan_id
);