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.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/skbuff.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/spinlock.h>
30 #include <linux/slab.h>
31 #include <linux/timer.h>
33 #include <linux/ipv6.h>
34 #include <linux/if_arp.h>
35 #include <linux/if_ether.h>
36 #include <linux/if_bonding.h>
37 #include <linux/if_vlan.h>
42 #include <asm/byteorder.h>
47 #define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
48 #define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
49 * Used for division - never set
52 #define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
53 * learning packets to the switch
56 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
57 * ALB_TIMER_TICKS_PER_SEC)
59 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
60 * ALB_TIMER_TICKS_PER_SEC)
62 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
63 * Note that this value MUST NOT be smaller
64 * because the key hash table is BYTE wide !
68 #define TLB_NULL_INDEX 0xffffffff
69 #define MAX_LP_BURST 3
72 #define RLB_HASH_TABLE_SIZE 256
73 #define RLB_NULL_INDEX 0xffffffff
74 #define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
75 #define RLB_ARP_BURST_SIZE 2
76 #define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
77 * rebalance interval (5 min).
79 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
80 * promiscuous after failover
82 #define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
84 #ifndef __long_aligned
85 #define __long_aligned __attribute__((aligned((sizeof(long)))))
87 static const u8 mac_bcast
[ETH_ALEN
] __long_aligned
= {
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
90 static const u8 mac_v6_allmcast
[ETH_ALEN
] __long_aligned
= {
91 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
93 static const int alb_delta_in_ticks
= HZ
/ ALB_TIMER_TICKS_PER_SEC
;
100 u8 padding
[ETH_ZLEN
- ETH_HLEN
];
104 __be16 hw_addr_space
;
105 __be16 prot_addr_space
;
109 u8 mac_src
[ETH_ALEN
]; /* sender hardware address */
110 __be32 ip_src
; /* sender IP address */
111 u8 mac_dst
[ETH_ALEN
]; /* target hardware address */
112 __be32 ip_dst
; /* target IP address */
116 static inline struct arp_pkt
*arp_pkt(const struct sk_buff
*skb
)
118 return (struct arp_pkt
*)skb_network_header(skb
);
121 /* Forward declaration */
122 static void alb_send_learning_packets(struct slave
*slave
, u8 mac_addr
[]);
124 static inline u8
_simple_hash(const u8
*hash_start
, int hash_size
)
129 for (i
= 0; i
< hash_size
; i
++) {
130 hash
^= hash_start
[i
];
136 /*********************** tlb specific functions ***************************/
138 static inline void _lock_tx_hashtbl(struct bonding
*bond
)
140 spin_lock_bh(&(BOND_ALB_INFO(bond
).tx_hashtbl_lock
));
143 static inline void _unlock_tx_hashtbl(struct bonding
*bond
)
145 spin_unlock_bh(&(BOND_ALB_INFO(bond
).tx_hashtbl_lock
));
148 /* Caller must hold tx_hashtbl lock */
149 static inline void tlb_init_table_entry(struct tlb_client_info
*entry
, int save_load
)
152 entry
->load_history
= 1 + entry
->tx_bytes
/
153 BOND_TLB_REBALANCE_INTERVAL
;
157 entry
->tx_slave
= NULL
;
158 entry
->next
= TLB_NULL_INDEX
;
159 entry
->prev
= TLB_NULL_INDEX
;
162 static inline void tlb_init_slave(struct slave
*slave
)
164 SLAVE_TLB_INFO(slave
).load
= 0;
165 SLAVE_TLB_INFO(slave
).head
= TLB_NULL_INDEX
;
168 /* Caller must hold bond lock for read */
169 static void tlb_clear_slave(struct bonding
*bond
, struct slave
*slave
, int save_load
)
171 struct tlb_client_info
*tx_hash_table
;
174 _lock_tx_hashtbl(bond
);
176 /* clear slave from tx_hashtbl */
177 tx_hash_table
= BOND_ALB_INFO(bond
).tx_hashtbl
;
179 /* skip this if we've already freed the tx hash table */
181 index
= SLAVE_TLB_INFO(slave
).head
;
182 while (index
!= TLB_NULL_INDEX
) {
183 u32 next_index
= tx_hash_table
[index
].next
;
184 tlb_init_table_entry(&tx_hash_table
[index
], save_load
);
189 tlb_init_slave(slave
);
191 _unlock_tx_hashtbl(bond
);
194 /* Must be called before starting the monitor timer */
195 static int tlb_initialize(struct bonding
*bond
)
197 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
198 int size
= TLB_HASH_TABLE_SIZE
* sizeof(struct tlb_client_info
);
199 struct tlb_client_info
*new_hashtbl
;
202 spin_lock_init(&(bond_info
->tx_hashtbl_lock
));
204 new_hashtbl
= kzalloc(size
, GFP_KERNEL
);
206 pr_err("%s: Error: Failed to allocate TLB hash table\n",
210 _lock_tx_hashtbl(bond
);
212 bond_info
->tx_hashtbl
= new_hashtbl
;
214 for (i
= 0; i
< TLB_HASH_TABLE_SIZE
; i
++) {
215 tlb_init_table_entry(&bond_info
->tx_hashtbl
[i
], 1);
218 _unlock_tx_hashtbl(bond
);
223 /* Must be called only after all slaves have been released */
224 static void tlb_deinitialize(struct bonding
*bond
)
226 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
228 _lock_tx_hashtbl(bond
);
230 kfree(bond_info
->tx_hashtbl
);
231 bond_info
->tx_hashtbl
= NULL
;
233 _unlock_tx_hashtbl(bond
);
236 /* Caller must hold bond lock for read */
237 static struct slave
*tlb_get_least_loaded_slave(struct bonding
*bond
)
239 struct slave
*slave
, *least_loaded
;
243 /* Find the first enabled slave */
244 bond_for_each_slave(bond
, slave
, i
) {
245 if (SLAVE_IS_OK(slave
)) {
255 least_loaded
= slave
;
256 max_gap
= (s64
)(slave
->speed
<< 20) - /* Convert to Megabit per sec */
257 (s64
)(SLAVE_TLB_INFO(slave
).load
<< 3); /* Bytes to bits */
259 /* Find the slave with the largest gap */
260 bond_for_each_slave_from(bond
, slave
, i
, least_loaded
) {
261 if (SLAVE_IS_OK(slave
)) {
262 s64 gap
= (s64
)(slave
->speed
<< 20) -
263 (s64
)(SLAVE_TLB_INFO(slave
).load
<< 3);
265 least_loaded
= slave
;
274 /* Caller must hold bond lock for read */
275 static struct slave
*tlb_choose_channel(struct bonding
*bond
, u32 hash_index
, u32 skb_len
)
277 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
278 struct tlb_client_info
*hash_table
;
279 struct slave
*assigned_slave
;
281 _lock_tx_hashtbl(bond
);
283 hash_table
= bond_info
->tx_hashtbl
;
284 assigned_slave
= hash_table
[hash_index
].tx_slave
;
285 if (!assigned_slave
) {
286 assigned_slave
= tlb_get_least_loaded_slave(bond
);
288 if (assigned_slave
) {
289 struct tlb_slave_info
*slave_info
=
290 &(SLAVE_TLB_INFO(assigned_slave
));
291 u32 next_index
= slave_info
->head
;
293 hash_table
[hash_index
].tx_slave
= assigned_slave
;
294 hash_table
[hash_index
].next
= next_index
;
295 hash_table
[hash_index
].prev
= TLB_NULL_INDEX
;
297 if (next_index
!= TLB_NULL_INDEX
) {
298 hash_table
[next_index
].prev
= hash_index
;
301 slave_info
->head
= hash_index
;
303 hash_table
[hash_index
].load_history
;
307 if (assigned_slave
) {
308 hash_table
[hash_index
].tx_bytes
+= skb_len
;
311 _unlock_tx_hashtbl(bond
);
313 return assigned_slave
;
316 /*********************** rlb specific functions ***************************/
317 static inline void _lock_rx_hashtbl(struct bonding
*bond
)
319 spin_lock_bh(&(BOND_ALB_INFO(bond
).rx_hashtbl_lock
));
322 static inline void _unlock_rx_hashtbl(struct bonding
*bond
)
324 spin_unlock_bh(&(BOND_ALB_INFO(bond
).rx_hashtbl_lock
));
327 /* when an ARP REPLY is received from a client update its info
330 static void rlb_update_entry_from_arp(struct bonding
*bond
, struct arp_pkt
*arp
)
332 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
333 struct rlb_client_info
*client_info
;
336 _lock_rx_hashtbl(bond
);
338 hash_index
= _simple_hash((u8
*)&(arp
->ip_src
), sizeof(arp
->ip_src
));
339 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
341 if ((client_info
->assigned
) &&
342 (client_info
->ip_src
== arp
->ip_dst
) &&
343 (client_info
->ip_dst
== arp
->ip_src
)) {
344 /* update the clients MAC address */
345 memcpy(client_info
->mac_dst
, arp
->mac_src
, ETH_ALEN
);
346 client_info
->ntt
= 1;
347 bond_info
->rx_ntt
= 1;
350 _unlock_rx_hashtbl(bond
);
353 static int rlb_arp_recv(struct sk_buff
*skb
, struct net_device
*bond_dev
, struct packet_type
*ptype
, struct net_device
*orig_dev
)
355 struct bonding
*bond
;
356 struct arp_pkt
*arp
= (struct arp_pkt
*)skb
->data
;
357 int res
= NET_RX_DROP
;
359 while (bond_dev
->priv_flags
& IFF_802_1Q_VLAN
)
360 bond_dev
= vlan_dev_real_dev(bond_dev
);
362 if (!(bond_dev
->priv_flags
& IFF_BONDING
) ||
363 !(bond_dev
->flags
& IFF_MASTER
))
367 pr_debug("Packet has no ARP data\n");
371 if (skb
->len
< sizeof(struct arp_pkt
)) {
372 pr_debug("Packet is too small to be an ARP\n");
376 if (arp
->op_code
== htons(ARPOP_REPLY
)) {
377 /* update rx hash table for this ARP */
378 bond
= netdev_priv(bond_dev
);
379 rlb_update_entry_from_arp(bond
, arp
);
380 pr_debug("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 if (!dev_set_promiscuity(bond
->curr_active_slave
->dev
, 1))
436 bond
->alb_info
.primary_is_promisc
= 1;
438 bond
->alb_info
.primary_is_promisc
= 0;
441 bond
->alb_info
.rlb_promisc_timeout_counter
= 0;
443 alb_send_learning_packets(bond
->curr_active_slave
, addr
);
446 /* slave being removed should not be active at this point
448 * Caller must hold bond lock for read
450 static void rlb_clear_slave(struct bonding
*bond
, struct slave
*slave
)
452 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
453 struct rlb_client_info
*rx_hash_table
;
454 u32 index
, next_index
;
456 /* clear slave from rx_hashtbl */
457 _lock_rx_hashtbl(bond
);
459 rx_hash_table
= bond_info
->rx_hashtbl
;
460 index
= bond_info
->rx_hashtbl_head
;
461 for (; index
!= RLB_NULL_INDEX
; index
= next_index
) {
462 next_index
= rx_hash_table
[index
].next
;
463 if (rx_hash_table
[index
].slave
== slave
) {
464 struct slave
*assigned_slave
= rlb_next_rx_slave(bond
);
466 if (assigned_slave
) {
467 rx_hash_table
[index
].slave
= assigned_slave
;
468 if (compare_ether_addr_64bits(rx_hash_table
[index
].mac_dst
,
470 bond_info
->rx_hashtbl
[index
].ntt
= 1;
471 bond_info
->rx_ntt
= 1;
472 /* A slave has been removed from the
473 * table because it is either disabled
474 * or being released. We must retry the
475 * update to avoid clients from not
476 * being updated & disconnecting when
479 bond_info
->rlb_update_retry_counter
=
482 } else { /* there is no active slave */
483 rx_hash_table
[index
].slave
= NULL
;
488 _unlock_rx_hashtbl(bond
);
490 write_lock_bh(&bond
->curr_slave_lock
);
492 if (slave
!= bond
->curr_active_slave
) {
493 rlb_teach_disabled_mac_on_primary(bond
, slave
->dev
->dev_addr
);
496 write_unlock_bh(&bond
->curr_slave_lock
);
499 static void rlb_update_client(struct rlb_client_info
*client_info
)
503 if (!client_info
->slave
) {
507 for (i
= 0; i
< RLB_ARP_BURST_SIZE
; i
++) {
510 skb
= arp_create(ARPOP_REPLY
, ETH_P_ARP
,
512 client_info
->slave
->dev
,
514 client_info
->mac_dst
,
515 client_info
->slave
->dev
->dev_addr
,
516 client_info
->mac_dst
);
518 pr_err("%s: Error: failed to create an ARP packet\n",
519 client_info
->slave
->dev
->master
->name
);
523 skb
->dev
= client_info
->slave
->dev
;
525 if (client_info
->tag
) {
526 skb
= vlan_put_tag(skb
, client_info
->vlan_id
);
528 pr_err("%s: Error: failed to insert VLAN tag\n",
529 client_info
->slave
->dev
->master
->name
);
538 /* sends ARP REPLIES that update the clients that need updating */
539 static void rlb_update_rx_clients(struct bonding
*bond
)
541 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
542 struct rlb_client_info
*client_info
;
545 _lock_rx_hashtbl(bond
);
547 hash_index
= bond_info
->rx_hashtbl_head
;
548 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
549 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
550 if (client_info
->ntt
) {
551 rlb_update_client(client_info
);
552 if (bond_info
->rlb_update_retry_counter
== 0) {
553 client_info
->ntt
= 0;
558 /* do not update the entries again until this counter is zero so that
559 * not to confuse the clients.
561 bond_info
->rlb_update_delay_counter
= RLB_UPDATE_DELAY
;
563 _unlock_rx_hashtbl(bond
);
566 /* The slave was assigned a new mac address - update the clients */
567 static void rlb_req_update_slave_clients(struct bonding
*bond
, struct slave
*slave
)
569 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
570 struct rlb_client_info
*client_info
;
574 _lock_rx_hashtbl(bond
);
576 hash_index
= bond_info
->rx_hashtbl_head
;
577 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
578 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
580 if ((client_info
->slave
== slave
) &&
581 compare_ether_addr_64bits(client_info
->mac_dst
, mac_bcast
)) {
582 client_info
->ntt
= 1;
587 // update the team's flag only after the whole iteration
589 bond_info
->rx_ntt
= 1;
591 bond_info
->rlb_update_retry_counter
= RLB_UPDATE_RETRY
;
594 _unlock_rx_hashtbl(bond
);
597 /* mark all clients using src_ip to be updated */
598 static void rlb_req_update_subnet_clients(struct bonding
*bond
, __be32 src_ip
)
600 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
601 struct rlb_client_info
*client_info
;
604 _lock_rx_hashtbl(bond
);
606 hash_index
= bond_info
->rx_hashtbl_head
;
607 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
608 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
610 if (!client_info
->slave
) {
611 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
615 /*update all clients using this src_ip, that are not assigned
616 * to the team's address (curr_active_slave) and have a known
617 * unicast mac address.
619 if ((client_info
->ip_src
== src_ip
) &&
620 compare_ether_addr_64bits(client_info
->slave
->dev
->dev_addr
,
621 bond
->dev
->dev_addr
) &&
622 compare_ether_addr_64bits(client_info
->mac_dst
, mac_bcast
)) {
623 client_info
->ntt
= 1;
624 bond_info
->rx_ntt
= 1;
628 _unlock_rx_hashtbl(bond
);
631 /* Caller must hold both bond and ptr locks for read */
632 static struct slave
*rlb_choose_channel(struct sk_buff
*skb
, struct bonding
*bond
)
634 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
635 struct arp_pkt
*arp
= arp_pkt(skb
);
636 struct slave
*assigned_slave
;
637 struct rlb_client_info
*client_info
;
640 _lock_rx_hashtbl(bond
);
642 hash_index
= _simple_hash((u8
*)&arp
->ip_dst
, sizeof(arp
->ip_src
));
643 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
645 if (client_info
->assigned
) {
646 if ((client_info
->ip_src
== arp
->ip_src
) &&
647 (client_info
->ip_dst
== arp
->ip_dst
)) {
648 /* the entry is already assigned to this client */
649 if (compare_ether_addr_64bits(arp
->mac_dst
, mac_bcast
)) {
650 /* update mac address from arp */
651 memcpy(client_info
->mac_dst
, arp
->mac_dst
, ETH_ALEN
);
654 assigned_slave
= client_info
->slave
;
655 if (assigned_slave
) {
656 _unlock_rx_hashtbl(bond
);
657 return assigned_slave
;
660 /* the entry is already assigned to some other client,
661 * move the old client to primary (curr_active_slave) so
662 * that the new client can be assigned to this entry.
664 if (bond
->curr_active_slave
&&
665 client_info
->slave
!= bond
->curr_active_slave
) {
666 client_info
->slave
= bond
->curr_active_slave
;
667 rlb_update_client(client_info
);
671 /* assign a new slave */
672 assigned_slave
= rlb_next_rx_slave(bond
);
674 if (assigned_slave
) {
675 client_info
->ip_src
= arp
->ip_src
;
676 client_info
->ip_dst
= arp
->ip_dst
;
677 /* arp->mac_dst is broadcast for arp reqeusts.
678 * will be updated with clients actual unicast mac address
679 * upon receiving an arp reply.
681 memcpy(client_info
->mac_dst
, arp
->mac_dst
, ETH_ALEN
);
682 client_info
->slave
= assigned_slave
;
684 if (compare_ether_addr_64bits(client_info
->mac_dst
, mac_bcast
)) {
685 client_info
->ntt
= 1;
686 bond
->alb_info
.rx_ntt
= 1;
688 client_info
->ntt
= 0;
691 if (!list_empty(&bond
->vlan_list
)) {
692 if (!vlan_get_tag(skb
, &client_info
->vlan_id
))
693 client_info
->tag
= 1;
696 if (!client_info
->assigned
) {
697 u32 prev_tbl_head
= bond_info
->rx_hashtbl_head
;
698 bond_info
->rx_hashtbl_head
= hash_index
;
699 client_info
->next
= prev_tbl_head
;
700 if (prev_tbl_head
!= RLB_NULL_INDEX
) {
701 bond_info
->rx_hashtbl
[prev_tbl_head
].prev
=
704 client_info
->assigned
= 1;
708 _unlock_rx_hashtbl(bond
);
710 return assigned_slave
;
713 /* chooses (and returns) transmit channel for arp reply
714 * does not choose channel for other arp types since they are
715 * sent on the curr_active_slave
717 static struct slave
*rlb_arp_xmit(struct sk_buff
*skb
, struct bonding
*bond
)
719 struct arp_pkt
*arp
= arp_pkt(skb
);
720 struct slave
*tx_slave
= NULL
;
722 if (arp
->op_code
== htons(ARPOP_REPLY
)) {
723 /* the arp must be sent on the selected
726 tx_slave
= rlb_choose_channel(skb
, bond
);
728 memcpy(arp
->mac_src
,tx_slave
->dev
->dev_addr
, ETH_ALEN
);
730 pr_debug("Server sent ARP Reply packet\n");
731 } else if (arp
->op_code
== htons(ARPOP_REQUEST
)) {
732 /* Create an entry in the rx_hashtbl for this client as a
734 * When the arp reply is received the entry will be updated
735 * with the correct unicast address of the client.
737 rlb_choose_channel(skb
, bond
);
739 /* The ARP relpy packets must be delayed so that
740 * they can cancel out the influence of the ARP request.
742 bond
->alb_info
.rlb_update_delay_counter
= RLB_UPDATE_DELAY
;
744 /* arp requests are broadcast and are sent on the primary
745 * the arp request will collapse all clients on the subnet to
746 * the primary slave. We must register these clients to be
747 * updated with their assigned mac.
749 rlb_req_update_subnet_clients(bond
, arp
->ip_src
);
750 pr_debug("Server sent ARP Request packet\n");
756 /* Caller must hold bond lock for read */
757 static void rlb_rebalance(struct bonding
*bond
)
759 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
760 struct slave
*assigned_slave
;
761 struct rlb_client_info
*client_info
;
765 _lock_rx_hashtbl(bond
);
768 hash_index
= bond_info
->rx_hashtbl_head
;
769 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
770 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
771 assigned_slave
= rlb_next_rx_slave(bond
);
772 if (assigned_slave
&& (client_info
->slave
!= assigned_slave
)) {
773 client_info
->slave
= assigned_slave
;
774 client_info
->ntt
= 1;
779 /* update the team's flag only after the whole iteration */
781 bond_info
->rx_ntt
= 1;
783 _unlock_rx_hashtbl(bond
);
786 /* Caller must hold rx_hashtbl lock */
787 static void rlb_init_table_entry(struct rlb_client_info
*entry
)
789 memset(entry
, 0, sizeof(struct rlb_client_info
));
790 entry
->next
= RLB_NULL_INDEX
;
791 entry
->prev
= RLB_NULL_INDEX
;
794 static int rlb_initialize(struct bonding
*bond
)
796 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
797 struct packet_type
*pk_type
= &(BOND_ALB_INFO(bond
).rlb_pkt_type
);
798 struct rlb_client_info
*new_hashtbl
;
799 int size
= RLB_HASH_TABLE_SIZE
* sizeof(struct rlb_client_info
);
802 spin_lock_init(&(bond_info
->rx_hashtbl_lock
));
804 new_hashtbl
= kmalloc(size
, GFP_KERNEL
);
806 pr_err("%s: Error: Failed to allocate RLB hash table\n",
810 _lock_rx_hashtbl(bond
);
812 bond_info
->rx_hashtbl
= new_hashtbl
;
814 bond_info
->rx_hashtbl_head
= RLB_NULL_INDEX
;
816 for (i
= 0; i
< RLB_HASH_TABLE_SIZE
; i
++) {
817 rlb_init_table_entry(bond_info
->rx_hashtbl
+ i
);
820 _unlock_rx_hashtbl(bond
);
822 /*initialize packet type*/
823 pk_type
->type
= cpu_to_be16(ETH_P_ARP
);
825 pk_type
->func
= rlb_arp_recv
;
827 /* register to receive ARPs */
828 dev_add_pack(pk_type
);
833 static void rlb_deinitialize(struct bonding
*bond
)
835 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
837 dev_remove_pack(&(bond_info
->rlb_pkt_type
));
839 _lock_rx_hashtbl(bond
);
841 kfree(bond_info
->rx_hashtbl
);
842 bond_info
->rx_hashtbl
= NULL
;
843 bond_info
->rx_hashtbl_head
= RLB_NULL_INDEX
;
845 _unlock_rx_hashtbl(bond
);
848 static void rlb_clear_vlan(struct bonding
*bond
, unsigned short vlan_id
)
850 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
853 _lock_rx_hashtbl(bond
);
855 curr_index
= bond_info
->rx_hashtbl_head
;
856 while (curr_index
!= RLB_NULL_INDEX
) {
857 struct rlb_client_info
*curr
= &(bond_info
->rx_hashtbl
[curr_index
]);
858 u32 next_index
= bond_info
->rx_hashtbl
[curr_index
].next
;
859 u32 prev_index
= bond_info
->rx_hashtbl
[curr_index
].prev
;
861 if (curr
->tag
&& (curr
->vlan_id
== vlan_id
)) {
862 if (curr_index
== bond_info
->rx_hashtbl_head
) {
863 bond_info
->rx_hashtbl_head
= next_index
;
865 if (prev_index
!= RLB_NULL_INDEX
) {
866 bond_info
->rx_hashtbl
[prev_index
].next
= next_index
;
868 if (next_index
!= RLB_NULL_INDEX
) {
869 bond_info
->rx_hashtbl
[next_index
].prev
= prev_index
;
872 rlb_init_table_entry(curr
);
875 curr_index
= next_index
;
878 _unlock_rx_hashtbl(bond
);
881 /*********************** tlb/rlb shared functions *********************/
883 static void alb_send_learning_packets(struct slave
*slave
, u8 mac_addr
[])
885 struct bonding
*bond
= bond_get_bond_by_slave(slave
);
886 struct learning_pkt pkt
;
887 int size
= sizeof(struct learning_pkt
);
890 memset(&pkt
, 0, size
);
891 memcpy(pkt
.mac_dst
, mac_addr
, ETH_ALEN
);
892 memcpy(pkt
.mac_src
, mac_addr
, ETH_ALEN
);
893 pkt
.type
= cpu_to_be16(ETH_P_LOOP
);
895 for (i
= 0; i
< MAX_LP_BURST
; i
++) {
899 skb
= dev_alloc_skb(size
);
904 data
= skb_put(skb
, size
);
905 memcpy(data
, &pkt
, size
);
907 skb_reset_mac_header(skb
);
908 skb
->network_header
= skb
->mac_header
+ ETH_HLEN
;
909 skb
->protocol
= pkt
.type
;
910 skb
->priority
= TC_PRIO_CONTROL
;
911 skb
->dev
= slave
->dev
;
913 if (!list_empty(&bond
->vlan_list
)) {
914 struct vlan_entry
*vlan
;
916 vlan
= bond_next_vlan(bond
,
917 bond
->alb_info
.current_alb_vlan
);
919 bond
->alb_info
.current_alb_vlan
= vlan
;
925 skb
= vlan_put_tag(skb
, vlan
->vlan_id
);
927 pr_err("%s: Error: failed to insert VLAN tag\n",
937 /* hw is a boolean parameter that determines whether we should try and
938 * set the hw address of the device as well as the hw address of the
941 static int alb_set_slave_mac_addr(struct slave
*slave
, u8 addr
[], int hw
)
943 struct net_device
*dev
= slave
->dev
;
944 struct sockaddr s_addr
;
947 memcpy(dev
->dev_addr
, addr
, dev
->addr_len
);
951 /* for rlb each slave must have a unique hw mac addresses so that */
952 /* each slave will receive packets destined to a different mac */
953 memcpy(s_addr
.sa_data
, addr
, dev
->addr_len
);
954 s_addr
.sa_family
= dev
->type
;
955 if (dev_set_mac_address(dev
, &s_addr
)) {
956 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
957 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
958 dev
->master
->name
, dev
->name
);
965 * Swap MAC addresses between two slaves.
967 * Called with RTNL held, and no other locks.
971 static void alb_swap_mac_addr(struct bonding
*bond
, struct slave
*slave1
, struct slave
*slave2
)
973 u8 tmp_mac_addr
[ETH_ALEN
];
975 memcpy(tmp_mac_addr
, slave1
->dev
->dev_addr
, ETH_ALEN
);
976 alb_set_slave_mac_addr(slave1
, slave2
->dev
->dev_addr
, bond
->alb_info
.rlb_enabled
);
977 alb_set_slave_mac_addr(slave2
, tmp_mac_addr
, bond
->alb_info
.rlb_enabled
);
982 * Send learning packets after MAC address swap.
984 * Called with RTNL and no other locks
986 static void alb_fasten_mac_swap(struct bonding
*bond
, struct slave
*slave1
,
987 struct slave
*slave2
)
989 int slaves_state_differ
= (SLAVE_IS_OK(slave1
) != SLAVE_IS_OK(slave2
));
990 struct slave
*disabled_slave
= NULL
;
994 /* fasten the change in the switch */
995 if (SLAVE_IS_OK(slave1
)) {
996 alb_send_learning_packets(slave1
, slave1
->dev
->dev_addr
);
997 if (bond
->alb_info
.rlb_enabled
) {
998 /* inform the clients that the mac address
1001 rlb_req_update_slave_clients(bond
, slave1
);
1004 disabled_slave
= slave1
;
1007 if (SLAVE_IS_OK(slave2
)) {
1008 alb_send_learning_packets(slave2
, slave2
->dev
->dev_addr
);
1009 if (bond
->alb_info
.rlb_enabled
) {
1010 /* inform the clients that the mac address
1013 rlb_req_update_slave_clients(bond
, slave2
);
1016 disabled_slave
= slave2
;
1019 if (bond
->alb_info
.rlb_enabled
&& slaves_state_differ
) {
1020 /* A disabled slave was assigned an active mac addr */
1021 rlb_teach_disabled_mac_on_primary(bond
,
1022 disabled_slave
->dev
->dev_addr
);
1027 * alb_change_hw_addr_on_detach
1028 * @bond: bonding we're working on
1029 * @slave: the slave that was just detached
1031 * We assume that @slave was already detached from the slave list.
1033 * If @slave's permanent hw address is different both from its current
1034 * address and from @bond's address, then somewhere in the bond there's
1035 * a slave that has @slave's permanet address as its current address.
1036 * We'll make sure that that slave no longer uses @slave's permanent address.
1038 * Caller must hold RTNL and no other locks
1040 static void alb_change_hw_addr_on_detach(struct bonding
*bond
, struct slave
*slave
)
1045 perm_curr_diff
= compare_ether_addr_64bits(slave
->perm_hwaddr
,
1046 slave
->dev
->dev_addr
);
1047 perm_bond_diff
= compare_ether_addr_64bits(slave
->perm_hwaddr
,
1048 bond
->dev
->dev_addr
);
1050 if (perm_curr_diff
&& perm_bond_diff
) {
1051 struct slave
*tmp_slave
;
1054 bond_for_each_slave(bond
, tmp_slave
, i
) {
1055 if (!compare_ether_addr_64bits(slave
->perm_hwaddr
,
1056 tmp_slave
->dev
->dev_addr
)) {
1063 /* locking: needs RTNL and nothing else */
1064 alb_swap_mac_addr(bond
, slave
, tmp_slave
);
1065 alb_fasten_mac_swap(bond
, slave
, tmp_slave
);
1071 * alb_handle_addr_collision_on_attach
1072 * @bond: bonding we're working on
1073 * @slave: the slave that was just attached
1075 * checks uniqueness of slave's mac address and handles the case the
1076 * new slave uses the bonds mac address.
1078 * If the permanent hw address of @slave is @bond's hw address, we need to
1079 * find a different hw address to give @slave, that isn't in use by any other
1080 * slave in the bond. This address must be, of course, one of the premanent
1081 * addresses of the other slaves.
1083 * We go over the slave list, and for each slave there we compare its
1084 * permanent hw address with the current address of all the other slaves.
1085 * If no match was found, then we've found a slave with a permanent address
1086 * that isn't used by any other slave in the bond, so we can assign it to
1089 * assumption: this function is called before @slave is attached to the
1092 * caller must hold the bond lock for write since the mac addresses are compared
1093 * and may be swapped.
1095 static int alb_handle_addr_collision_on_attach(struct bonding
*bond
, struct slave
*slave
)
1097 struct slave
*tmp_slave1
, *tmp_slave2
, *free_mac_slave
;
1098 struct slave
*has_bond_addr
= bond
->curr_active_slave
;
1099 int i
, j
, found
= 0;
1101 if (bond
->slave_cnt
== 0) {
1102 /* this is the first slave */
1106 /* if slave's mac address differs from bond's mac address
1107 * check uniqueness of slave's mac address against the other
1108 * slaves in the bond.
1110 if (compare_ether_addr_64bits(slave
->perm_hwaddr
, bond
->dev
->dev_addr
)) {
1111 bond_for_each_slave(bond
, tmp_slave1
, i
) {
1112 if (!compare_ether_addr_64bits(tmp_slave1
->dev
->dev_addr
,
1113 slave
->dev
->dev_addr
)) {
1122 /* Try setting slave mac to bond address and fall-through
1123 to code handling that situation below... */
1124 alb_set_slave_mac_addr(slave
, bond
->dev
->dev_addr
,
1125 bond
->alb_info
.rlb_enabled
);
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
) {
1135 bond_for_each_slave(bond
, tmp_slave2
, j
) {
1136 if (!compare_ether_addr_64bits(tmp_slave1
->perm_hwaddr
,
1137 tmp_slave2
->dev
->dev_addr
)) {
1144 /* no slave has tmp_slave1's perm addr
1147 free_mac_slave
= tmp_slave1
;
1151 if (!has_bond_addr
) {
1152 if (!compare_ether_addr_64bits(tmp_slave1
->dev
->dev_addr
,
1153 bond
->dev
->dev_addr
)) {
1155 has_bond_addr
= tmp_slave1
;
1160 if (free_mac_slave
) {
1161 alb_set_slave_mac_addr(slave
, free_mac_slave
->perm_hwaddr
,
1162 bond
->alb_info
.rlb_enabled
);
1164 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1165 bond
->dev
->name
, slave
->dev
->name
,
1166 free_mac_slave
->dev
->name
);
1168 } else if (has_bond_addr
) {
1169 pr_err("%s: Error: the hw address of slave %s is in use by the bond; couldn't find a slave with a free hw address to give it (this should not have happened)\n",
1170 bond
->dev
->name
, slave
->dev
->name
);
1178 * alb_set_mac_address
1182 * In TLB mode all slaves are configured to the bond's hw address, but set
1183 * their dev_addr field to different addresses (based on their permanent hw
1186 * For each slave, this function sets the interface to the new address and then
1187 * changes its dev_addr field to its previous value.
1189 * Unwinding assumes bond's mac address has not yet changed.
1191 static int alb_set_mac_address(struct bonding
*bond
, void *addr
)
1194 struct slave
*slave
, *stop_at
;
1195 char tmp_addr
[ETH_ALEN
];
1199 if (bond
->alb_info
.rlb_enabled
) {
1203 bond_for_each_slave(bond
, slave
, i
) {
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
);
1219 memcpy(sa
.sa_data
, bond
->dev
->dev_addr
, bond
->dev
->addr_len
);
1220 sa
.sa_family
= bond
->dev
->type
;
1222 /* unwind from head to the slave that failed */
1224 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
1225 memcpy(tmp_addr
, slave
->dev
->dev_addr
, ETH_ALEN
);
1226 dev_set_mac_address(slave
->dev
, &sa
);
1227 memcpy(slave
->dev
->dev_addr
, tmp_addr
, ETH_ALEN
);
1233 /************************ exported alb funcions ************************/
1235 int bond_alb_initialize(struct bonding
*bond
, int rlb_enabled
)
1239 res
= tlb_initialize(bond
);
1245 bond
->alb_info
.rlb_enabled
= 1;
1246 /* initialize rlb */
1247 res
= rlb_initialize(bond
);
1249 tlb_deinitialize(bond
);
1253 bond
->alb_info
.rlb_enabled
= 0;
1259 void bond_alb_deinitialize(struct bonding
*bond
)
1261 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1263 tlb_deinitialize(bond
);
1265 if (bond_info
->rlb_enabled
) {
1266 rlb_deinitialize(bond
);
1270 int bond_alb_xmit(struct sk_buff
*skb
, struct net_device
*bond_dev
)
1272 struct bonding
*bond
= netdev_priv(bond_dev
);
1273 struct ethhdr
*eth_data
;
1274 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1275 struct slave
*tx_slave
= NULL
;
1276 static const __be32 ip_bcast
= htonl(0xffffffff);
1278 int do_tx_balance
= 1;
1280 const u8
*hash_start
= NULL
;
1282 struct ipv6hdr
*ip6hdr
;
1284 skb_reset_mac_header(skb
);
1285 eth_data
= eth_hdr(skb
);
1287 /* make sure that the curr_active_slave and the slaves list do
1288 * not change during tx
1290 read_lock(&bond
->lock
);
1291 read_lock(&bond
->curr_slave_lock
);
1293 if (!BOND_IS_OK(bond
)) {
1297 switch (ntohs(skb
->protocol
)) {
1299 const struct iphdr
*iph
= ip_hdr(skb
);
1301 if (!compare_ether_addr_64bits(eth_data
->h_dest
, mac_bcast
) ||
1302 (iph
->daddr
== ip_bcast
) ||
1303 (iph
->protocol
== IPPROTO_IGMP
)) {
1307 hash_start
= (char *)&(iph
->daddr
);
1308 hash_size
= sizeof(iph
->daddr
);
1312 /* IPv6 doesn't really use broadcast mac address, but leave
1313 * that here just in case.
1315 if (!compare_ether_addr_64bits(eth_data
->h_dest
, mac_bcast
)) {
1320 /* IPv6 uses all-nodes multicast as an equivalent to
1321 * broadcasts in IPv4.
1323 if (!compare_ether_addr_64bits(eth_data
->h_dest
, mac_v6_allmcast
)) {
1328 /* Additianally, DAD probes should not be tx-balanced as that
1329 * will lead to false positives for duplicate addresses and
1330 * prevent address configuration from working.
1332 ip6hdr
= ipv6_hdr(skb
);
1333 if (ipv6_addr_any(&ip6hdr
->saddr
)) {
1338 hash_start
= (char *)&(ipv6_hdr(skb
)->daddr
);
1339 hash_size
= sizeof(ipv6_hdr(skb
)->daddr
);
1342 if (ipx_hdr(skb
)->ipx_checksum
!= IPX_NO_CHECKSUM
) {
1343 /* something is wrong with this packet */
1348 if (ipx_hdr(skb
)->ipx_type
!= IPX_TYPE_NCP
) {
1349 /* The only protocol worth balancing in
1350 * this family since it has an "ARP" like
1357 hash_start
= (char*)eth_data
->h_dest
;
1358 hash_size
= ETH_ALEN
;
1362 if (bond_info
->rlb_enabled
) {
1363 tx_slave
= rlb_arp_xmit(skb
, bond
);
1371 if (do_tx_balance
) {
1372 hash_index
= _simple_hash(hash_start
, hash_size
);
1373 tx_slave
= tlb_choose_channel(bond
, hash_index
, skb
->len
);
1377 /* unbalanced or unassigned, send through primary */
1378 tx_slave
= bond
->curr_active_slave
;
1379 bond_info
->unbalanced_load
+= skb
->len
;
1382 if (tx_slave
&& SLAVE_IS_OK(tx_slave
)) {
1383 if (tx_slave
!= bond
->curr_active_slave
) {
1384 memcpy(eth_data
->h_source
,
1385 tx_slave
->dev
->dev_addr
,
1389 res
= bond_dev_queue_xmit(bond
, skb
, tx_slave
->dev
);
1392 tlb_clear_slave(bond
, tx_slave
, 0);
1398 /* no suitable interface, frame not sent */
1401 read_unlock(&bond
->curr_slave_lock
);
1402 read_unlock(&bond
->lock
);
1403 return NETDEV_TX_OK
;
1406 void bond_alb_monitor(struct work_struct
*work
)
1408 struct bonding
*bond
= container_of(work
, struct bonding
,
1410 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1411 struct slave
*slave
;
1414 read_lock(&bond
->lock
);
1416 if (bond
->kill_timers
) {
1420 if (bond
->slave_cnt
== 0) {
1421 bond_info
->tx_rebalance_counter
= 0;
1422 bond_info
->lp_counter
= 0;
1426 bond_info
->tx_rebalance_counter
++;
1427 bond_info
->lp_counter
++;
1429 /* send learning packets */
1430 if (bond_info
->lp_counter
>= BOND_ALB_LP_TICKS
) {
1431 /* change of curr_active_slave involves swapping of mac addresses.
1432 * in order to avoid this swapping from happening while
1433 * sending the learning packets, the curr_slave_lock must be held for
1436 read_lock(&bond
->curr_slave_lock
);
1438 bond_for_each_slave(bond
, slave
, i
) {
1439 alb_send_learning_packets(slave
, slave
->dev
->dev_addr
);
1442 read_unlock(&bond
->curr_slave_lock
);
1444 bond_info
->lp_counter
= 0;
1447 /* rebalance tx traffic */
1448 if (bond_info
->tx_rebalance_counter
>= BOND_TLB_REBALANCE_TICKS
) {
1450 read_lock(&bond
->curr_slave_lock
);
1452 bond_for_each_slave(bond
, slave
, i
) {
1453 tlb_clear_slave(bond
, slave
, 1);
1454 if (slave
== bond
->curr_active_slave
) {
1455 SLAVE_TLB_INFO(slave
).load
=
1456 bond_info
->unbalanced_load
/
1457 BOND_TLB_REBALANCE_INTERVAL
;
1458 bond_info
->unbalanced_load
= 0;
1462 read_unlock(&bond
->curr_slave_lock
);
1464 bond_info
->tx_rebalance_counter
= 0;
1467 /* handle rlb stuff */
1468 if (bond_info
->rlb_enabled
) {
1469 if (bond_info
->primary_is_promisc
&&
1470 (++bond_info
->rlb_promisc_timeout_counter
>= RLB_PROMISC_TIMEOUT
)) {
1473 * dev_set_promiscuity requires rtnl and
1476 read_unlock(&bond
->lock
);
1479 bond_info
->rlb_promisc_timeout_counter
= 0;
1481 /* If the primary was set to promiscuous mode
1482 * because a slave was disabled then
1483 * it can now leave promiscuous mode.
1485 dev_set_promiscuity(bond
->curr_active_slave
->dev
, -1);
1486 bond_info
->primary_is_promisc
= 0;
1489 read_lock(&bond
->lock
);
1492 if (bond_info
->rlb_rebalance
) {
1493 bond_info
->rlb_rebalance
= 0;
1494 rlb_rebalance(bond
);
1497 /* check if clients need updating */
1498 if (bond_info
->rx_ntt
) {
1499 if (bond_info
->rlb_update_delay_counter
) {
1500 --bond_info
->rlb_update_delay_counter
;
1502 rlb_update_rx_clients(bond
);
1503 if (bond_info
->rlb_update_retry_counter
) {
1504 --bond_info
->rlb_update_retry_counter
;
1506 bond_info
->rx_ntt
= 0;
1513 queue_delayed_work(bond
->wq
, &bond
->alb_work
, alb_delta_in_ticks
);
1515 read_unlock(&bond
->lock
);
1518 /* assumption: called before the slave is attached to the bond
1519 * and not locked by the bond lock
1521 int bond_alb_init_slave(struct bonding
*bond
, struct slave
*slave
)
1525 res
= alb_set_slave_mac_addr(slave
, slave
->perm_hwaddr
,
1526 bond
->alb_info
.rlb_enabled
);
1531 /* caller must hold the bond lock for write since the mac addresses
1532 * are compared and may be swapped.
1534 read_lock(&bond
->lock
);
1536 res
= alb_handle_addr_collision_on_attach(bond
, slave
);
1538 read_unlock(&bond
->lock
);
1544 tlb_init_slave(slave
);
1546 /* order a rebalance ASAP */
1547 bond
->alb_info
.tx_rebalance_counter
= BOND_TLB_REBALANCE_TICKS
;
1549 if (bond
->alb_info
.rlb_enabled
) {
1550 bond
->alb_info
.rlb_rebalance
= 1;
1557 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1560 * Caller must hold RTNL and no other locks
1562 void bond_alb_deinit_slave(struct bonding
*bond
, struct slave
*slave
)
1564 if (bond
->slave_cnt
> 1) {
1565 alb_change_hw_addr_on_detach(bond
, slave
);
1568 tlb_clear_slave(bond
, slave
, 0);
1570 if (bond
->alb_info
.rlb_enabled
) {
1571 bond
->alb_info
.next_rx_slave
= NULL
;
1572 rlb_clear_slave(bond
, slave
);
1576 /* Caller must hold bond lock for read */
1577 void bond_alb_handle_link_change(struct bonding
*bond
, struct slave
*slave
, char link
)
1579 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1581 if (link
== BOND_LINK_DOWN
) {
1582 tlb_clear_slave(bond
, slave
, 0);
1583 if (bond
->alb_info
.rlb_enabled
) {
1584 rlb_clear_slave(bond
, slave
);
1586 } else if (link
== BOND_LINK_UP
) {
1587 /* order a rebalance ASAP */
1588 bond_info
->tx_rebalance_counter
= BOND_TLB_REBALANCE_TICKS
;
1589 if (bond
->alb_info
.rlb_enabled
) {
1590 bond
->alb_info
.rlb_rebalance
= 1;
1591 /* If the updelay module parameter is smaller than the
1592 * forwarding delay of the switch the rebalance will
1593 * not work because the rebalance arp replies will
1594 * not be forwarded to the clients..
1601 * bond_alb_handle_active_change - assign new curr_active_slave
1602 * @bond: our bonding struct
1603 * @new_slave: new slave to assign
1605 * Set the bond->curr_active_slave to @new_slave and handle
1606 * mac address swapping and promiscuity changes as needed.
1608 * If new_slave is NULL, caller must hold curr_slave_lock or
1609 * bond->lock for write.
1611 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1612 * read and curr_slave_lock for write. Processing here may sleep, so
1613 * no other locks may be held.
1615 void bond_alb_handle_active_change(struct bonding
*bond
, struct slave
*new_slave
)
1616 __releases(&bond
->curr_slave_lock
)
1617 __releases(&bond
->lock
)
1618 __acquires(&bond
->lock
)
1619 __acquires(&bond
->curr_slave_lock
)
1621 struct slave
*swap_slave
;
1624 if (bond
->curr_active_slave
== new_slave
) {
1628 if (bond
->curr_active_slave
&& bond
->alb_info
.primary_is_promisc
) {
1629 dev_set_promiscuity(bond
->curr_active_slave
->dev
, -1);
1630 bond
->alb_info
.primary_is_promisc
= 0;
1631 bond
->alb_info
.rlb_promisc_timeout_counter
= 0;
1634 swap_slave
= bond
->curr_active_slave
;
1635 bond
->curr_active_slave
= new_slave
;
1637 if (!new_slave
|| (bond
->slave_cnt
== 0)) {
1641 /* set the new curr_active_slave to the bonds mac address
1642 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1645 struct slave
*tmp_slave
;
1646 /* find slave that is holding the bond's mac address */
1647 bond_for_each_slave(bond
, tmp_slave
, i
) {
1648 if (!compare_ether_addr_64bits(tmp_slave
->dev
->dev_addr
,
1649 bond
->dev
->dev_addr
)) {
1650 swap_slave
= tmp_slave
;
1657 * Arrange for swap_slave and new_slave to temporarily be
1658 * ignored so we can mess with their MAC addresses without
1659 * fear of interference from transmit activity.
1662 tlb_clear_slave(bond
, swap_slave
, 1);
1664 tlb_clear_slave(bond
, new_slave
, 1);
1666 write_unlock_bh(&bond
->curr_slave_lock
);
1667 read_unlock(&bond
->lock
);
1671 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1673 /* swap mac address */
1674 alb_swap_mac_addr(bond
, swap_slave
, new_slave
);
1676 /* set the new_slave to the bond mac address */
1677 alb_set_slave_mac_addr(new_slave
, bond
->dev
->dev_addr
,
1678 bond
->alb_info
.rlb_enabled
);
1682 alb_fasten_mac_swap(bond
, swap_slave
, new_slave
);
1683 read_lock(&bond
->lock
);
1685 read_lock(&bond
->lock
);
1686 alb_send_learning_packets(new_slave
, bond
->dev
->dev_addr
);
1689 write_lock_bh(&bond
->curr_slave_lock
);
1695 int bond_alb_set_mac_address(struct net_device
*bond_dev
, void *addr
)
1696 __acquires(&bond
->lock
)
1697 __releases(&bond
->lock
)
1699 struct bonding
*bond
= netdev_priv(bond_dev
);
1700 struct sockaddr
*sa
= addr
;
1701 struct slave
*slave
, *swap_slave
;
1705 if (!is_valid_ether_addr(sa
->sa_data
)) {
1706 return -EADDRNOTAVAIL
;
1709 res
= alb_set_mac_address(bond
, addr
);
1714 memcpy(bond_dev
->dev_addr
, sa
->sa_data
, bond_dev
->addr_len
);
1716 /* If there is no curr_active_slave there is nothing else to do.
1717 * Otherwise we'll need to pass the new address to it and handle
1720 if (!bond
->curr_active_slave
) {
1726 bond_for_each_slave(bond
, slave
, i
) {
1727 if (!compare_ether_addr_64bits(slave
->dev
->dev_addr
,
1728 bond_dev
->dev_addr
)) {
1735 alb_swap_mac_addr(bond
, swap_slave
, bond
->curr_active_slave
);
1736 alb_fasten_mac_swap(bond
, swap_slave
, bond
->curr_active_slave
);
1738 alb_set_slave_mac_addr(bond
->curr_active_slave
, bond_dev
->dev_addr
,
1739 bond
->alb_info
.rlb_enabled
);
1741 read_lock(&bond
->lock
);
1742 alb_send_learning_packets(bond
->curr_active_slave
, bond_dev
->dev_addr
);
1743 if (bond
->alb_info
.rlb_enabled
) {
1744 /* inform clients mac address has changed */
1745 rlb_req_update_slave_clients(bond
, bond
->curr_active_slave
);
1747 read_unlock(&bond
->lock
);
1753 void bond_alb_clear_vlan(struct bonding
*bond
, unsigned short vlan_id
)
1755 if (bond
->alb_info
.current_alb_vlan
&&
1756 (bond
->alb_info
.current_alb_vlan
->vlan_id
== vlan_id
)) {
1757 bond
->alb_info
.current_alb_vlan
= NULL
;
1760 if (bond
->alb_info
.rlb_enabled
) {
1761 rlb_clear_vlan(bond
, vlan_id
);