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 (compare_ether_addr_64bits(client_info
->mac_dst
, arp
->mac_src
))) {
345 /* update the clients MAC address */
346 memcpy(client_info
->mac_dst
, arp
->mac_src
, ETH_ALEN
);
347 client_info
->ntt
= 1;
348 bond_info
->rx_ntt
= 1;
351 _unlock_rx_hashtbl(bond
);
354 static int rlb_arp_recv(struct sk_buff
*skb
, struct net_device
*bond_dev
, struct packet_type
*ptype
, struct net_device
*orig_dev
)
356 struct bonding
*bond
;
357 struct arp_pkt
*arp
= (struct arp_pkt
*)skb
->data
;
358 int res
= NET_RX_DROP
;
360 while (bond_dev
->priv_flags
& IFF_802_1Q_VLAN
)
361 bond_dev
= vlan_dev_real_dev(bond_dev
);
363 if (!(bond_dev
->priv_flags
& IFF_BONDING
) ||
364 !(bond_dev
->flags
& IFF_MASTER
))
368 pr_debug("Packet has no ARP data\n");
372 if (skb
->len
< sizeof(struct arp_pkt
)) {
373 pr_debug("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 bond
= netdev_priv(bond_dev
);
380 rlb_update_entry_from_arp(bond
, arp
);
381 pr_debug("Server received an ARP Reply from client\n");
384 res
= NET_RX_SUCCESS
;
392 /* Caller must hold bond lock for read */
393 static struct slave
*rlb_next_rx_slave(struct bonding
*bond
)
395 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
396 struct slave
*rx_slave
, *slave
, *start_at
;
399 if (bond_info
->next_rx_slave
) {
400 start_at
= bond_info
->next_rx_slave
;
402 start_at
= bond
->first_slave
;
407 bond_for_each_slave_from(bond
, slave
, i
, start_at
) {
408 if (SLAVE_IS_OK(slave
)) {
411 } else if (slave
->speed
> rx_slave
->speed
) {
418 bond_info
->next_rx_slave
= rx_slave
->next
;
424 /* teach the switch the mac of a disabled slave
425 * on the primary for fault tolerance
427 * Caller must hold bond->curr_slave_lock for write or bond lock for write
429 static void rlb_teach_disabled_mac_on_primary(struct bonding
*bond
, u8 addr
[])
431 if (!bond
->curr_active_slave
) {
435 if (!bond
->alb_info
.primary_is_promisc
) {
436 if (!dev_set_promiscuity(bond
->curr_active_slave
->dev
, 1))
437 bond
->alb_info
.primary_is_promisc
= 1;
439 bond
->alb_info
.primary_is_promisc
= 0;
442 bond
->alb_info
.rlb_promisc_timeout_counter
= 0;
444 alb_send_learning_packets(bond
->curr_active_slave
, addr
);
447 /* slave being removed should not be active at this point
449 * Caller must hold bond lock for read
451 static void rlb_clear_slave(struct bonding
*bond
, struct slave
*slave
)
453 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
454 struct rlb_client_info
*rx_hash_table
;
455 u32 index
, next_index
;
457 /* clear slave from rx_hashtbl */
458 _lock_rx_hashtbl(bond
);
460 rx_hash_table
= bond_info
->rx_hashtbl
;
461 index
= bond_info
->rx_hashtbl_head
;
462 for (; index
!= RLB_NULL_INDEX
; index
= next_index
) {
463 next_index
= rx_hash_table
[index
].next
;
464 if (rx_hash_table
[index
].slave
== slave
) {
465 struct slave
*assigned_slave
= rlb_next_rx_slave(bond
);
467 if (assigned_slave
) {
468 rx_hash_table
[index
].slave
= assigned_slave
;
469 if (compare_ether_addr_64bits(rx_hash_table
[index
].mac_dst
,
471 bond_info
->rx_hashtbl
[index
].ntt
= 1;
472 bond_info
->rx_ntt
= 1;
473 /* A slave has been removed from the
474 * table because it is either disabled
475 * or being released. We must retry the
476 * update to avoid clients from not
477 * being updated & disconnecting when
480 bond_info
->rlb_update_retry_counter
=
483 } else { /* there is no active slave */
484 rx_hash_table
[index
].slave
= NULL
;
489 _unlock_rx_hashtbl(bond
);
491 write_lock_bh(&bond
->curr_slave_lock
);
493 if (slave
!= bond
->curr_active_slave
) {
494 rlb_teach_disabled_mac_on_primary(bond
, slave
->dev
->dev_addr
);
497 write_unlock_bh(&bond
->curr_slave_lock
);
500 static void rlb_update_client(struct rlb_client_info
*client_info
)
504 if (!client_info
->slave
) {
508 for (i
= 0; i
< RLB_ARP_BURST_SIZE
; i
++) {
511 skb
= arp_create(ARPOP_REPLY
, ETH_P_ARP
,
513 client_info
->slave
->dev
,
515 client_info
->mac_dst
,
516 client_info
->slave
->dev
->dev_addr
,
517 client_info
->mac_dst
);
519 pr_err("%s: Error: failed to create an ARP packet\n",
520 client_info
->slave
->dev
->master
->name
);
524 skb
->dev
= client_info
->slave
->dev
;
526 if (client_info
->tag
) {
527 skb
= vlan_put_tag(skb
, client_info
->vlan_id
);
529 pr_err("%s: Error: failed to insert VLAN tag\n",
530 client_info
->slave
->dev
->master
->name
);
539 /* sends ARP REPLIES that update the clients that need updating */
540 static void rlb_update_rx_clients(struct bonding
*bond
)
542 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
543 struct rlb_client_info
*client_info
;
546 _lock_rx_hashtbl(bond
);
548 hash_index
= bond_info
->rx_hashtbl_head
;
549 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
550 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
551 if (client_info
->ntt
) {
552 rlb_update_client(client_info
);
553 if (bond_info
->rlb_update_retry_counter
== 0) {
554 client_info
->ntt
= 0;
559 /* do not update the entries again until this counter is zero so that
560 * not to confuse the clients.
562 bond_info
->rlb_update_delay_counter
= RLB_UPDATE_DELAY
;
564 _unlock_rx_hashtbl(bond
);
567 /* The slave was assigned a new mac address - update the clients */
568 static void rlb_req_update_slave_clients(struct bonding
*bond
, struct slave
*slave
)
570 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
571 struct rlb_client_info
*client_info
;
575 _lock_rx_hashtbl(bond
);
577 hash_index
= bond_info
->rx_hashtbl_head
;
578 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
579 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
581 if ((client_info
->slave
== slave
) &&
582 compare_ether_addr_64bits(client_info
->mac_dst
, mac_bcast
)) {
583 client_info
->ntt
= 1;
588 // update the team's flag only after the whole iteration
590 bond_info
->rx_ntt
= 1;
592 bond_info
->rlb_update_retry_counter
= RLB_UPDATE_RETRY
;
595 _unlock_rx_hashtbl(bond
);
598 /* mark all clients using src_ip to be updated */
599 static void rlb_req_update_subnet_clients(struct bonding
*bond
, __be32 src_ip
)
601 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
602 struct rlb_client_info
*client_info
;
605 _lock_rx_hashtbl(bond
);
607 hash_index
= bond_info
->rx_hashtbl_head
;
608 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
609 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
611 if (!client_info
->slave
) {
612 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
616 /*update all clients using this src_ip, that are not assigned
617 * to the team's address (curr_active_slave) and have a known
618 * unicast mac address.
620 if ((client_info
->ip_src
== src_ip
) &&
621 compare_ether_addr_64bits(client_info
->slave
->dev
->dev_addr
,
622 bond
->dev
->dev_addr
) &&
623 compare_ether_addr_64bits(client_info
->mac_dst
, mac_bcast
)) {
624 client_info
->ntt
= 1;
625 bond_info
->rx_ntt
= 1;
629 _unlock_rx_hashtbl(bond
);
632 /* Caller must hold both bond and ptr locks for read */
633 static struct slave
*rlb_choose_channel(struct sk_buff
*skb
, struct bonding
*bond
)
635 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
636 struct arp_pkt
*arp
= arp_pkt(skb
);
637 struct slave
*assigned_slave
;
638 struct rlb_client_info
*client_info
;
641 _lock_rx_hashtbl(bond
);
643 hash_index
= _simple_hash((u8
*)&arp
->ip_dst
, sizeof(arp
->ip_src
));
644 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
646 if (client_info
->assigned
) {
647 if ((client_info
->ip_src
== arp
->ip_src
) &&
648 (client_info
->ip_dst
== arp
->ip_dst
)) {
649 /* the entry is already assigned to this client */
650 if (compare_ether_addr_64bits(arp
->mac_dst
, mac_bcast
)) {
651 /* update mac address from arp */
652 memcpy(client_info
->mac_dst
, arp
->mac_dst
, ETH_ALEN
);
655 assigned_slave
= client_info
->slave
;
656 if (assigned_slave
) {
657 _unlock_rx_hashtbl(bond
);
658 return assigned_slave
;
661 /* the entry is already assigned to some other client,
662 * move the old client to primary (curr_active_slave) so
663 * that the new client can be assigned to this entry.
665 if (bond
->curr_active_slave
&&
666 client_info
->slave
!= bond
->curr_active_slave
) {
667 client_info
->slave
= bond
->curr_active_slave
;
668 rlb_update_client(client_info
);
672 /* assign a new slave */
673 assigned_slave
= rlb_next_rx_slave(bond
);
675 if (assigned_slave
) {
676 client_info
->ip_src
= arp
->ip_src
;
677 client_info
->ip_dst
= arp
->ip_dst
;
678 /* arp->mac_dst is broadcast for arp reqeusts.
679 * will be updated with clients actual unicast mac address
680 * upon receiving an arp reply.
682 memcpy(client_info
->mac_dst
, arp
->mac_dst
, ETH_ALEN
);
683 client_info
->slave
= assigned_slave
;
685 if (compare_ether_addr_64bits(client_info
->mac_dst
, mac_bcast
)) {
686 client_info
->ntt
= 1;
687 bond
->alb_info
.rx_ntt
= 1;
689 client_info
->ntt
= 0;
692 if (!list_empty(&bond
->vlan_list
)) {
693 if (!vlan_get_tag(skb
, &client_info
->vlan_id
))
694 client_info
->tag
= 1;
697 if (!client_info
->assigned
) {
698 u32 prev_tbl_head
= bond_info
->rx_hashtbl_head
;
699 bond_info
->rx_hashtbl_head
= hash_index
;
700 client_info
->next
= prev_tbl_head
;
701 if (prev_tbl_head
!= RLB_NULL_INDEX
) {
702 bond_info
->rx_hashtbl
[prev_tbl_head
].prev
=
705 client_info
->assigned
= 1;
709 _unlock_rx_hashtbl(bond
);
711 return assigned_slave
;
714 /* chooses (and returns) transmit channel for arp reply
715 * does not choose channel for other arp types since they are
716 * sent on the curr_active_slave
718 static struct slave
*rlb_arp_xmit(struct sk_buff
*skb
, struct bonding
*bond
)
720 struct arp_pkt
*arp
= arp_pkt(skb
);
721 struct slave
*tx_slave
= NULL
;
723 if (arp
->op_code
== htons(ARPOP_REPLY
)) {
724 /* the arp must be sent on the selected
727 tx_slave
= rlb_choose_channel(skb
, bond
);
729 memcpy(arp
->mac_src
,tx_slave
->dev
->dev_addr
, ETH_ALEN
);
731 pr_debug("Server sent ARP Reply packet\n");
732 } else if (arp
->op_code
== htons(ARPOP_REQUEST
)) {
733 /* Create an entry in the rx_hashtbl for this client as a
735 * When the arp reply is received the entry will be updated
736 * with the correct unicast address of the client.
738 rlb_choose_channel(skb
, bond
);
740 /* The ARP relpy packets must be delayed so that
741 * they can cancel out the influence of the ARP request.
743 bond
->alb_info
.rlb_update_delay_counter
= RLB_UPDATE_DELAY
;
745 /* arp requests are broadcast and are sent on the primary
746 * the arp request will collapse all clients on the subnet to
747 * the primary slave. We must register these clients to be
748 * updated with their assigned mac.
750 rlb_req_update_subnet_clients(bond
, arp
->ip_src
);
751 pr_debug("Server sent ARP Request packet\n");
757 /* Caller must hold bond lock for read */
758 static void rlb_rebalance(struct bonding
*bond
)
760 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
761 struct slave
*assigned_slave
;
762 struct rlb_client_info
*client_info
;
766 _lock_rx_hashtbl(bond
);
769 hash_index
= bond_info
->rx_hashtbl_head
;
770 for (; hash_index
!= RLB_NULL_INDEX
; hash_index
= client_info
->next
) {
771 client_info
= &(bond_info
->rx_hashtbl
[hash_index
]);
772 assigned_slave
= rlb_next_rx_slave(bond
);
773 if (assigned_slave
&& (client_info
->slave
!= assigned_slave
)) {
774 client_info
->slave
= assigned_slave
;
775 client_info
->ntt
= 1;
780 /* update the team's flag only after the whole iteration */
782 bond_info
->rx_ntt
= 1;
784 _unlock_rx_hashtbl(bond
);
787 /* Caller must hold rx_hashtbl lock */
788 static void rlb_init_table_entry(struct rlb_client_info
*entry
)
790 memset(entry
, 0, sizeof(struct rlb_client_info
));
791 entry
->next
= RLB_NULL_INDEX
;
792 entry
->prev
= RLB_NULL_INDEX
;
795 static int rlb_initialize(struct bonding
*bond
)
797 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
798 struct packet_type
*pk_type
= &(BOND_ALB_INFO(bond
).rlb_pkt_type
);
799 struct rlb_client_info
*new_hashtbl
;
800 int size
= RLB_HASH_TABLE_SIZE
* sizeof(struct rlb_client_info
);
803 spin_lock_init(&(bond_info
->rx_hashtbl_lock
));
805 new_hashtbl
= kmalloc(size
, GFP_KERNEL
);
807 pr_err("%s: Error: Failed to allocate RLB hash table\n",
811 _lock_rx_hashtbl(bond
);
813 bond_info
->rx_hashtbl
= new_hashtbl
;
815 bond_info
->rx_hashtbl_head
= RLB_NULL_INDEX
;
817 for (i
= 0; i
< RLB_HASH_TABLE_SIZE
; i
++) {
818 rlb_init_table_entry(bond_info
->rx_hashtbl
+ i
);
821 _unlock_rx_hashtbl(bond
);
823 /*initialize packet type*/
824 pk_type
->type
= cpu_to_be16(ETH_P_ARP
);
825 pk_type
->dev
= bond
->dev
;
826 pk_type
->func
= rlb_arp_recv
;
828 /* register to receive ARPs */
829 dev_add_pack(pk_type
);
834 static void rlb_deinitialize(struct bonding
*bond
)
836 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
838 dev_remove_pack(&(bond_info
->rlb_pkt_type
));
840 _lock_rx_hashtbl(bond
);
842 kfree(bond_info
->rx_hashtbl
);
843 bond_info
->rx_hashtbl
= NULL
;
844 bond_info
->rx_hashtbl_head
= RLB_NULL_INDEX
;
846 _unlock_rx_hashtbl(bond
);
849 static void rlb_clear_vlan(struct bonding
*bond
, unsigned short vlan_id
)
851 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
854 _lock_rx_hashtbl(bond
);
856 curr_index
= bond_info
->rx_hashtbl_head
;
857 while (curr_index
!= RLB_NULL_INDEX
) {
858 struct rlb_client_info
*curr
= &(bond_info
->rx_hashtbl
[curr_index
]);
859 u32 next_index
= bond_info
->rx_hashtbl
[curr_index
].next
;
860 u32 prev_index
= bond_info
->rx_hashtbl
[curr_index
].prev
;
862 if (curr
->tag
&& (curr
->vlan_id
== vlan_id
)) {
863 if (curr_index
== bond_info
->rx_hashtbl_head
) {
864 bond_info
->rx_hashtbl_head
= next_index
;
866 if (prev_index
!= RLB_NULL_INDEX
) {
867 bond_info
->rx_hashtbl
[prev_index
].next
= next_index
;
869 if (next_index
!= RLB_NULL_INDEX
) {
870 bond_info
->rx_hashtbl
[next_index
].prev
= prev_index
;
873 rlb_init_table_entry(curr
);
876 curr_index
= next_index
;
879 _unlock_rx_hashtbl(bond
);
882 /*********************** tlb/rlb shared functions *********************/
884 static void alb_send_learning_packets(struct slave
*slave
, u8 mac_addr
[])
886 struct bonding
*bond
= bond_get_bond_by_slave(slave
);
887 struct learning_pkt pkt
;
888 int size
= sizeof(struct learning_pkt
);
891 memset(&pkt
, 0, size
);
892 memcpy(pkt
.mac_dst
, mac_addr
, ETH_ALEN
);
893 memcpy(pkt
.mac_src
, mac_addr
, ETH_ALEN
);
894 pkt
.type
= cpu_to_be16(ETH_P_LOOP
);
896 for (i
= 0; i
< MAX_LP_BURST
; i
++) {
900 skb
= dev_alloc_skb(size
);
905 data
= skb_put(skb
, size
);
906 memcpy(data
, &pkt
, size
);
908 skb_reset_mac_header(skb
);
909 skb
->network_header
= skb
->mac_header
+ ETH_HLEN
;
910 skb
->protocol
= pkt
.type
;
911 skb
->priority
= TC_PRIO_CONTROL
;
912 skb
->dev
= slave
->dev
;
914 if (!list_empty(&bond
->vlan_list
)) {
915 struct vlan_entry
*vlan
;
917 vlan
= bond_next_vlan(bond
,
918 bond
->alb_info
.current_alb_vlan
);
920 bond
->alb_info
.current_alb_vlan
= vlan
;
926 skb
= vlan_put_tag(skb
, vlan
->vlan_id
);
928 pr_err("%s: Error: failed to insert VLAN tag\n",
938 /* hw is a boolean parameter that determines whether we should try and
939 * set the hw address of the device as well as the hw address of the
942 static int alb_set_slave_mac_addr(struct slave
*slave
, u8 addr
[], int hw
)
944 struct net_device
*dev
= slave
->dev
;
945 struct sockaddr s_addr
;
948 memcpy(dev
->dev_addr
, addr
, dev
->addr_len
);
952 /* for rlb each slave must have a unique hw mac addresses so that */
953 /* each slave will receive packets destined to a different mac */
954 memcpy(s_addr
.sa_data
, addr
, dev
->addr_len
);
955 s_addr
.sa_family
= dev
->type
;
956 if (dev_set_mac_address(dev
, &s_addr
)) {
957 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
958 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
959 dev
->master
->name
, dev
->name
);
966 * Swap MAC addresses between two slaves.
968 * Called with RTNL held, and no other locks.
972 static void alb_swap_mac_addr(struct bonding
*bond
, struct slave
*slave1
, struct slave
*slave2
)
974 u8 tmp_mac_addr
[ETH_ALEN
];
976 memcpy(tmp_mac_addr
, slave1
->dev
->dev_addr
, ETH_ALEN
);
977 alb_set_slave_mac_addr(slave1
, slave2
->dev
->dev_addr
, bond
->alb_info
.rlb_enabled
);
978 alb_set_slave_mac_addr(slave2
, tmp_mac_addr
, bond
->alb_info
.rlb_enabled
);
983 * Send learning packets after MAC address swap.
985 * Called with RTNL and no other locks
987 static void alb_fasten_mac_swap(struct bonding
*bond
, struct slave
*slave1
,
988 struct slave
*slave2
)
990 int slaves_state_differ
= (SLAVE_IS_OK(slave1
) != SLAVE_IS_OK(slave2
));
991 struct slave
*disabled_slave
= NULL
;
995 /* fasten the change in the switch */
996 if (SLAVE_IS_OK(slave1
)) {
997 alb_send_learning_packets(slave1
, slave1
->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
, slave1
);
1005 disabled_slave
= slave1
;
1008 if (SLAVE_IS_OK(slave2
)) {
1009 alb_send_learning_packets(slave2
, slave2
->dev
->dev_addr
);
1010 if (bond
->alb_info
.rlb_enabled
) {
1011 /* inform the clients that the mac address
1014 rlb_req_update_slave_clients(bond
, slave2
);
1017 disabled_slave
= slave2
;
1020 if (bond
->alb_info
.rlb_enabled
&& slaves_state_differ
) {
1021 /* A disabled slave was assigned an active mac addr */
1022 rlb_teach_disabled_mac_on_primary(bond
,
1023 disabled_slave
->dev
->dev_addr
);
1028 * alb_change_hw_addr_on_detach
1029 * @bond: bonding we're working on
1030 * @slave: the slave that was just detached
1032 * We assume that @slave was already detached from the slave list.
1034 * If @slave's permanent hw address is different both from its current
1035 * address and from @bond's address, then somewhere in the bond there's
1036 * a slave that has @slave's permanet address as its current address.
1037 * We'll make sure that that slave no longer uses @slave's permanent address.
1039 * Caller must hold RTNL and no other locks
1041 static void alb_change_hw_addr_on_detach(struct bonding
*bond
, struct slave
*slave
)
1046 perm_curr_diff
= compare_ether_addr_64bits(slave
->perm_hwaddr
,
1047 slave
->dev
->dev_addr
);
1048 perm_bond_diff
= compare_ether_addr_64bits(slave
->perm_hwaddr
,
1049 bond
->dev
->dev_addr
);
1051 if (perm_curr_diff
&& perm_bond_diff
) {
1052 struct slave
*tmp_slave
;
1055 bond_for_each_slave(bond
, tmp_slave
, i
) {
1056 if (!compare_ether_addr_64bits(slave
->perm_hwaddr
,
1057 tmp_slave
->dev
->dev_addr
)) {
1064 /* locking: needs RTNL and nothing else */
1065 alb_swap_mac_addr(bond
, slave
, tmp_slave
);
1066 alb_fasten_mac_swap(bond
, slave
, tmp_slave
);
1072 * alb_handle_addr_collision_on_attach
1073 * @bond: bonding we're working on
1074 * @slave: the slave that was just attached
1076 * checks uniqueness of slave's mac address and handles the case the
1077 * new slave uses the bonds mac address.
1079 * If the permanent hw address of @slave is @bond's hw address, we need to
1080 * find a different hw address to give @slave, that isn't in use by any other
1081 * slave in the bond. This address must be, of course, one of the premanent
1082 * addresses of the other slaves.
1084 * We go over the slave list, and for each slave there we compare its
1085 * permanent hw address with the current address of all the other slaves.
1086 * If no match was found, then we've found a slave with a permanent address
1087 * that isn't used by any other slave in the bond, so we can assign it to
1090 * assumption: this function is called before @slave is attached to the
1093 * caller must hold the bond lock for write since the mac addresses are compared
1094 * and may be swapped.
1096 static int alb_handle_addr_collision_on_attach(struct bonding
*bond
, struct slave
*slave
)
1098 struct slave
*tmp_slave1
, *tmp_slave2
, *free_mac_slave
;
1099 struct slave
*has_bond_addr
= bond
->curr_active_slave
;
1100 int i
, j
, found
= 0;
1102 if (bond
->slave_cnt
== 0) {
1103 /* this is the first slave */
1107 /* if slave's mac address differs from bond's mac address
1108 * check uniqueness of slave's mac address against the other
1109 * slaves in the bond.
1111 if (compare_ether_addr_64bits(slave
->perm_hwaddr
, bond
->dev
->dev_addr
)) {
1112 bond_for_each_slave(bond
, tmp_slave1
, i
) {
1113 if (!compare_ether_addr_64bits(tmp_slave1
->dev
->dev_addr
,
1114 slave
->dev
->dev_addr
)) {
1123 /* Try setting slave mac to bond address and fall-through
1124 to code handling that situation below... */
1125 alb_set_slave_mac_addr(slave
, bond
->dev
->dev_addr
,
1126 bond
->alb_info
.rlb_enabled
);
1129 /* The slave's address is equal to the address of the bond.
1130 * Search for a spare address in the bond for this slave.
1132 free_mac_slave
= NULL
;
1134 bond_for_each_slave(bond
, tmp_slave1
, i
) {
1136 bond_for_each_slave(bond
, tmp_slave2
, j
) {
1137 if (!compare_ether_addr_64bits(tmp_slave1
->perm_hwaddr
,
1138 tmp_slave2
->dev
->dev_addr
)) {
1145 /* no slave has tmp_slave1's perm addr
1148 free_mac_slave
= tmp_slave1
;
1152 if (!has_bond_addr
) {
1153 if (!compare_ether_addr_64bits(tmp_slave1
->dev
->dev_addr
,
1154 bond
->dev
->dev_addr
)) {
1156 has_bond_addr
= tmp_slave1
;
1161 if (free_mac_slave
) {
1162 alb_set_slave_mac_addr(slave
, free_mac_slave
->perm_hwaddr
,
1163 bond
->alb_info
.rlb_enabled
);
1165 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1166 bond
->dev
->name
, slave
->dev
->name
,
1167 free_mac_slave
->dev
->name
);
1169 } else if (has_bond_addr
) {
1170 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",
1171 bond
->dev
->name
, slave
->dev
->name
);
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 /* save net_device's current hw address */
1206 memcpy(tmp_addr
, slave
->dev
->dev_addr
, ETH_ALEN
);
1208 res
= dev_set_mac_address(slave
->dev
, addr
);
1210 /* restore net_device's hw address */
1211 memcpy(slave
->dev
->dev_addr
, tmp_addr
, ETH_ALEN
);
1220 memcpy(sa
.sa_data
, bond
->dev
->dev_addr
, bond
->dev
->addr_len
);
1221 sa
.sa_family
= bond
->dev
->type
;
1223 /* unwind from head to the slave that failed */
1225 bond_for_each_slave_from_to(bond
, slave
, i
, bond
->first_slave
, stop_at
) {
1226 memcpy(tmp_addr
, slave
->dev
->dev_addr
, ETH_ALEN
);
1227 dev_set_mac_address(slave
->dev
, &sa
);
1228 memcpy(slave
->dev
->dev_addr
, tmp_addr
, ETH_ALEN
);
1234 /************************ exported alb funcions ************************/
1236 int bond_alb_initialize(struct bonding
*bond
, int rlb_enabled
)
1240 res
= tlb_initialize(bond
);
1246 bond
->alb_info
.rlb_enabled
= 1;
1247 /* initialize rlb */
1248 res
= rlb_initialize(bond
);
1250 tlb_deinitialize(bond
);
1254 bond
->alb_info
.rlb_enabled
= 0;
1260 void bond_alb_deinitialize(struct bonding
*bond
)
1262 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1264 tlb_deinitialize(bond
);
1266 if (bond_info
->rlb_enabled
) {
1267 rlb_deinitialize(bond
);
1271 int bond_alb_xmit(struct sk_buff
*skb
, struct net_device
*bond_dev
)
1273 struct bonding
*bond
= netdev_priv(bond_dev
);
1274 struct ethhdr
*eth_data
;
1275 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1276 struct slave
*tx_slave
= NULL
;
1277 static const __be32 ip_bcast
= htonl(0xffffffff);
1279 int do_tx_balance
= 1;
1281 const u8
*hash_start
= NULL
;
1283 struct ipv6hdr
*ip6hdr
;
1285 skb_reset_mac_header(skb
);
1286 eth_data
= eth_hdr(skb
);
1288 /* make sure that the curr_active_slave and the slaves list do
1289 * not change during tx
1291 read_lock(&bond
->lock
);
1292 read_lock(&bond
->curr_slave_lock
);
1294 if (!BOND_IS_OK(bond
)) {
1298 switch (ntohs(skb
->protocol
)) {
1300 const struct iphdr
*iph
= ip_hdr(skb
);
1302 if (!compare_ether_addr_64bits(eth_data
->h_dest
, mac_bcast
) ||
1303 (iph
->daddr
== ip_bcast
) ||
1304 (iph
->protocol
== IPPROTO_IGMP
)) {
1308 hash_start
= (char *)&(iph
->daddr
);
1309 hash_size
= sizeof(iph
->daddr
);
1313 /* IPv6 doesn't really use broadcast mac address, but leave
1314 * that here just in case.
1316 if (!compare_ether_addr_64bits(eth_data
->h_dest
, mac_bcast
)) {
1321 /* IPv6 uses all-nodes multicast as an equivalent to
1322 * broadcasts in IPv4.
1324 if (!compare_ether_addr_64bits(eth_data
->h_dest
, mac_v6_allmcast
)) {
1329 /* Additianally, DAD probes should not be tx-balanced as that
1330 * will lead to false positives for duplicate addresses and
1331 * prevent address configuration from working.
1333 ip6hdr
= ipv6_hdr(skb
);
1334 if (ipv6_addr_any(&ip6hdr
->saddr
)) {
1339 hash_start
= (char *)&(ipv6_hdr(skb
)->daddr
);
1340 hash_size
= sizeof(ipv6_hdr(skb
)->daddr
);
1343 if (ipx_hdr(skb
)->ipx_checksum
!= IPX_NO_CHECKSUM
) {
1344 /* something is wrong with this packet */
1349 if (ipx_hdr(skb
)->ipx_type
!= IPX_TYPE_NCP
) {
1350 /* The only protocol worth balancing in
1351 * this family since it has an "ARP" like
1358 hash_start
= (char*)eth_data
->h_dest
;
1359 hash_size
= ETH_ALEN
;
1363 if (bond_info
->rlb_enabled
) {
1364 tx_slave
= rlb_arp_xmit(skb
, bond
);
1372 if (do_tx_balance
) {
1373 hash_index
= _simple_hash(hash_start
, hash_size
);
1374 tx_slave
= tlb_choose_channel(bond
, hash_index
, skb
->len
);
1378 /* unbalanced or unassigned, send through primary */
1379 tx_slave
= bond
->curr_active_slave
;
1380 bond_info
->unbalanced_load
+= skb
->len
;
1383 if (tx_slave
&& SLAVE_IS_OK(tx_slave
)) {
1384 if (tx_slave
!= bond
->curr_active_slave
) {
1385 memcpy(eth_data
->h_source
,
1386 tx_slave
->dev
->dev_addr
,
1390 res
= bond_dev_queue_xmit(bond
, skb
, tx_slave
->dev
);
1393 tlb_clear_slave(bond
, tx_slave
, 0);
1399 /* no suitable interface, frame not sent */
1402 read_unlock(&bond
->curr_slave_lock
);
1403 read_unlock(&bond
->lock
);
1404 return NETDEV_TX_OK
;
1407 void bond_alb_monitor(struct work_struct
*work
)
1409 struct bonding
*bond
= container_of(work
, struct bonding
,
1411 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1412 struct slave
*slave
;
1415 read_lock(&bond
->lock
);
1417 if (bond
->kill_timers
) {
1421 if (bond
->slave_cnt
== 0) {
1422 bond_info
->tx_rebalance_counter
= 0;
1423 bond_info
->lp_counter
= 0;
1427 bond_info
->tx_rebalance_counter
++;
1428 bond_info
->lp_counter
++;
1430 /* send learning packets */
1431 if (bond_info
->lp_counter
>= BOND_ALB_LP_TICKS
) {
1432 /* change of curr_active_slave involves swapping of mac addresses.
1433 * in order to avoid this swapping from happening while
1434 * sending the learning packets, the curr_slave_lock must be held for
1437 read_lock(&bond
->curr_slave_lock
);
1439 bond_for_each_slave(bond
, slave
, i
) {
1440 alb_send_learning_packets(slave
, slave
->dev
->dev_addr
);
1443 read_unlock(&bond
->curr_slave_lock
);
1445 bond_info
->lp_counter
= 0;
1448 /* rebalance tx traffic */
1449 if (bond_info
->tx_rebalance_counter
>= BOND_TLB_REBALANCE_TICKS
) {
1451 read_lock(&bond
->curr_slave_lock
);
1453 bond_for_each_slave(bond
, slave
, i
) {
1454 tlb_clear_slave(bond
, slave
, 1);
1455 if (slave
== bond
->curr_active_slave
) {
1456 SLAVE_TLB_INFO(slave
).load
=
1457 bond_info
->unbalanced_load
/
1458 BOND_TLB_REBALANCE_INTERVAL
;
1459 bond_info
->unbalanced_load
= 0;
1463 read_unlock(&bond
->curr_slave_lock
);
1465 bond_info
->tx_rebalance_counter
= 0;
1468 /* handle rlb stuff */
1469 if (bond_info
->rlb_enabled
) {
1470 if (bond_info
->primary_is_promisc
&&
1471 (++bond_info
->rlb_promisc_timeout_counter
>= RLB_PROMISC_TIMEOUT
)) {
1474 * dev_set_promiscuity requires rtnl and
1477 read_unlock(&bond
->lock
);
1480 bond_info
->rlb_promisc_timeout_counter
= 0;
1482 /* If the primary was set to promiscuous mode
1483 * because a slave was disabled then
1484 * it can now leave promiscuous mode.
1486 dev_set_promiscuity(bond
->curr_active_slave
->dev
, -1);
1487 bond_info
->primary_is_promisc
= 0;
1490 read_lock(&bond
->lock
);
1493 if (bond_info
->rlb_rebalance
) {
1494 bond_info
->rlb_rebalance
= 0;
1495 rlb_rebalance(bond
);
1498 /* check if clients need updating */
1499 if (bond_info
->rx_ntt
) {
1500 if (bond_info
->rlb_update_delay_counter
) {
1501 --bond_info
->rlb_update_delay_counter
;
1503 rlb_update_rx_clients(bond
);
1504 if (bond_info
->rlb_update_retry_counter
) {
1505 --bond_info
->rlb_update_retry_counter
;
1507 bond_info
->rx_ntt
= 0;
1514 queue_delayed_work(bond
->wq
, &bond
->alb_work
, alb_delta_in_ticks
);
1516 read_unlock(&bond
->lock
);
1519 /* assumption: called before the slave is attached to the bond
1520 * and not locked by the bond lock
1522 int bond_alb_init_slave(struct bonding
*bond
, struct slave
*slave
)
1526 res
= alb_set_slave_mac_addr(slave
, slave
->perm_hwaddr
,
1527 bond
->alb_info
.rlb_enabled
);
1532 /* caller must hold the bond lock for write since the mac addresses
1533 * are compared and may be swapped.
1535 read_lock(&bond
->lock
);
1537 res
= alb_handle_addr_collision_on_attach(bond
, slave
);
1539 read_unlock(&bond
->lock
);
1545 tlb_init_slave(slave
);
1547 /* order a rebalance ASAP */
1548 bond
->alb_info
.tx_rebalance_counter
= BOND_TLB_REBALANCE_TICKS
;
1550 if (bond
->alb_info
.rlb_enabled
) {
1551 bond
->alb_info
.rlb_rebalance
= 1;
1558 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1561 * Caller must hold RTNL and no other locks
1563 void bond_alb_deinit_slave(struct bonding
*bond
, struct slave
*slave
)
1565 if (bond
->slave_cnt
> 1) {
1566 alb_change_hw_addr_on_detach(bond
, slave
);
1569 tlb_clear_slave(bond
, slave
, 0);
1571 if (bond
->alb_info
.rlb_enabled
) {
1572 bond
->alb_info
.next_rx_slave
= NULL
;
1573 rlb_clear_slave(bond
, slave
);
1577 /* Caller must hold bond lock for read */
1578 void bond_alb_handle_link_change(struct bonding
*bond
, struct slave
*slave
, char link
)
1580 struct alb_bond_info
*bond_info
= &(BOND_ALB_INFO(bond
));
1582 if (link
== BOND_LINK_DOWN
) {
1583 tlb_clear_slave(bond
, slave
, 0);
1584 if (bond
->alb_info
.rlb_enabled
) {
1585 rlb_clear_slave(bond
, slave
);
1587 } else if (link
== BOND_LINK_UP
) {
1588 /* order a rebalance ASAP */
1589 bond_info
->tx_rebalance_counter
= BOND_TLB_REBALANCE_TICKS
;
1590 if (bond
->alb_info
.rlb_enabled
) {
1591 bond
->alb_info
.rlb_rebalance
= 1;
1592 /* If the updelay module parameter is smaller than the
1593 * forwarding delay of the switch the rebalance will
1594 * not work because the rebalance arp replies will
1595 * not be forwarded to the clients..
1602 * bond_alb_handle_active_change - assign new curr_active_slave
1603 * @bond: our bonding struct
1604 * @new_slave: new slave to assign
1606 * Set the bond->curr_active_slave to @new_slave and handle
1607 * mac address swapping and promiscuity changes as needed.
1609 * If new_slave is NULL, caller must hold curr_slave_lock or
1610 * bond->lock for write.
1612 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1613 * read and curr_slave_lock for write. Processing here may sleep, so
1614 * no other locks may be held.
1616 void bond_alb_handle_active_change(struct bonding
*bond
, struct slave
*new_slave
)
1617 __releases(&bond
->curr_slave_lock
)
1618 __releases(&bond
->lock
)
1619 __acquires(&bond
->lock
)
1620 __acquires(&bond
->curr_slave_lock
)
1622 struct slave
*swap_slave
;
1625 if (bond
->curr_active_slave
== new_slave
) {
1629 if (bond
->curr_active_slave
&& bond
->alb_info
.primary_is_promisc
) {
1630 dev_set_promiscuity(bond
->curr_active_slave
->dev
, -1);
1631 bond
->alb_info
.primary_is_promisc
= 0;
1632 bond
->alb_info
.rlb_promisc_timeout_counter
= 0;
1635 swap_slave
= bond
->curr_active_slave
;
1636 bond
->curr_active_slave
= new_slave
;
1638 if (!new_slave
|| (bond
->slave_cnt
== 0)) {
1642 /* set the new curr_active_slave to the bonds mac address
1643 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1646 struct slave
*tmp_slave
;
1647 /* find slave that is holding the bond's mac address */
1648 bond_for_each_slave(bond
, tmp_slave
, i
) {
1649 if (!compare_ether_addr_64bits(tmp_slave
->dev
->dev_addr
,
1650 bond
->dev
->dev_addr
)) {
1651 swap_slave
= tmp_slave
;
1658 * Arrange for swap_slave and new_slave to temporarily be
1659 * ignored so we can mess with their MAC addresses without
1660 * fear of interference from transmit activity.
1663 tlb_clear_slave(bond
, swap_slave
, 1);
1665 tlb_clear_slave(bond
, new_slave
, 1);
1667 write_unlock_bh(&bond
->curr_slave_lock
);
1668 read_unlock(&bond
->lock
);
1672 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1674 /* swap mac address */
1675 alb_swap_mac_addr(bond
, swap_slave
, new_slave
);
1677 /* set the new_slave to the bond mac address */
1678 alb_set_slave_mac_addr(new_slave
, bond
->dev
->dev_addr
,
1679 bond
->alb_info
.rlb_enabled
);
1683 alb_fasten_mac_swap(bond
, swap_slave
, new_slave
);
1684 read_lock(&bond
->lock
);
1686 read_lock(&bond
->lock
);
1687 alb_send_learning_packets(new_slave
, bond
->dev
->dev_addr
);
1690 write_lock_bh(&bond
->curr_slave_lock
);
1696 int bond_alb_set_mac_address(struct net_device
*bond_dev
, void *addr
)
1697 __acquires(&bond
->lock
)
1698 __releases(&bond
->lock
)
1700 struct bonding
*bond
= netdev_priv(bond_dev
);
1701 struct sockaddr
*sa
= addr
;
1702 struct slave
*slave
, *swap_slave
;
1706 if (!is_valid_ether_addr(sa
->sa_data
)) {
1707 return -EADDRNOTAVAIL
;
1710 res
= alb_set_mac_address(bond
, addr
);
1715 memcpy(bond_dev
->dev_addr
, sa
->sa_data
, bond_dev
->addr_len
);
1717 /* If there is no curr_active_slave there is nothing else to do.
1718 * Otherwise we'll need to pass the new address to it and handle
1721 if (!bond
->curr_active_slave
) {
1727 bond_for_each_slave(bond
, slave
, i
) {
1728 if (!compare_ether_addr_64bits(slave
->dev
->dev_addr
,
1729 bond_dev
->dev_addr
)) {
1736 alb_swap_mac_addr(bond
, swap_slave
, bond
->curr_active_slave
);
1737 alb_fasten_mac_swap(bond
, swap_slave
, bond
->curr_active_slave
);
1739 alb_set_slave_mac_addr(bond
->curr_active_slave
, bond_dev
->dev_addr
,
1740 bond
->alb_info
.rlb_enabled
);
1742 read_lock(&bond
->lock
);
1743 alb_send_learning_packets(bond
->curr_active_slave
, bond_dev
->dev_addr
);
1744 if (bond
->alb_info
.rlb_enabled
) {
1745 /* inform clients mac address has changed */
1746 rlb_req_update_slave_clients(bond
, bond
->curr_active_slave
);
1748 read_unlock(&bond
->lock
);
1754 void bond_alb_clear_vlan(struct bonding
*bond
, unsigned short vlan_id
)
1756 if (bond
->alb_info
.current_alb_vlan
&&
1757 (bond
->alb_info
.current_alb_vlan
->vlan_id
== vlan_id
)) {
1758 bond
->alb_info
.current_alb_vlan
= NULL
;
1761 if (bond
->alb_info
.rlb_enabled
) {
1762 rlb_clear_vlan(bond
, vlan_id
);