bonding: Ensure that we unshare skbs prior to calling pskb_may_pull
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / bonding / bond_alb.c
blob36a7ac83d9e2103e01e6293a35c7e70c40d5b961
1 /*
2 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
23 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/pkt_sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
29 #include <linux/timer.h>
30 #include <linux/ip.h>
31 #include <linux/ipv6.h>
32 #include <linux/if_arp.h>
33 #include <linux/if_ether.h>
34 #include <linux/if_bonding.h>
35 #include <linux/if_vlan.h>
36 #include <linux/in.h>
37 #include <net/ipx.h>
38 #include <net/arp.h>
39 #include <net/ipv6.h>
40 #include <asm/byteorder.h>
41 #include "bonding.h"
42 #include "bond_alb.h"
45 #define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
46 #define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
47 * Used for division - never set
48 * to zero !!!
50 #define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
51 * learning packets to the switch
54 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
55 * ALB_TIMER_TICKS_PER_SEC)
57 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
58 * ALB_TIMER_TICKS_PER_SEC)
60 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
61 * Note that this value MUST NOT be smaller
62 * because the key hash table is BYTE wide !
66 #define TLB_NULL_INDEX 0xffffffff
67 #define MAX_LP_BURST 3
69 /* rlb defs */
70 #define RLB_HASH_TABLE_SIZE 256
71 #define RLB_NULL_INDEX 0xffffffff
72 #define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
73 #define RLB_ARP_BURST_SIZE 2
74 #define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
75 * rebalance interval (5 min).
77 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
78 * promiscuous after failover
80 #define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
82 #ifndef __long_aligned
83 #define __long_aligned __attribute__((aligned((sizeof(long)))))
84 #endif
85 static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
86 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
88 static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
89 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
91 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
93 #pragma pack(1)
94 struct learning_pkt {
95 u8 mac_dst[ETH_ALEN];
96 u8 mac_src[ETH_ALEN];
97 __be16 type;
98 u8 padding[ETH_ZLEN - ETH_HLEN];
101 struct arp_pkt {
102 __be16 hw_addr_space;
103 __be16 prot_addr_space;
104 u8 hw_addr_len;
105 u8 prot_addr_len;
106 __be16 op_code;
107 u8 mac_src[ETH_ALEN]; /* sender hardware address */
108 __be32 ip_src; /* sender IP address */
109 u8 mac_dst[ETH_ALEN]; /* target hardware address */
110 __be32 ip_dst; /* target IP address */
112 #pragma pack()
114 static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
116 return (struct arp_pkt *)skb_network_header(skb);
119 /* Forward declaration */
120 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
122 static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
124 int i;
125 u8 hash = 0;
127 for (i = 0; i < hash_size; i++) {
128 hash ^= hash_start[i];
131 return hash;
134 /*********************** tlb specific functions ***************************/
136 static inline void _lock_tx_hashtbl(struct bonding *bond)
138 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
141 static inline void _unlock_tx_hashtbl(struct bonding *bond)
143 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
146 /* Caller must hold tx_hashtbl lock */
147 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
149 if (save_load) {
150 entry->load_history = 1 + entry->tx_bytes /
151 BOND_TLB_REBALANCE_INTERVAL;
152 entry->tx_bytes = 0;
155 entry->tx_slave = NULL;
156 entry->next = TLB_NULL_INDEX;
157 entry->prev = TLB_NULL_INDEX;
160 static inline void tlb_init_slave(struct slave *slave)
162 SLAVE_TLB_INFO(slave).load = 0;
163 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
166 /* Caller must hold bond lock for read */
167 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
169 struct tlb_client_info *tx_hash_table;
170 u32 index;
172 _lock_tx_hashtbl(bond);
174 /* clear slave from tx_hashtbl */
175 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
177 /* skip this if we've already freed the tx hash table */
178 if (tx_hash_table) {
179 index = SLAVE_TLB_INFO(slave).head;
180 while (index != TLB_NULL_INDEX) {
181 u32 next_index = tx_hash_table[index].next;
182 tlb_init_table_entry(&tx_hash_table[index], save_load);
183 index = next_index;
187 tlb_init_slave(slave);
189 _unlock_tx_hashtbl(bond);
192 /* Must be called before starting the monitor timer */
193 static int tlb_initialize(struct bonding *bond)
195 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
196 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
197 struct tlb_client_info *new_hashtbl;
198 int i;
200 spin_lock_init(&(bond_info->tx_hashtbl_lock));
202 new_hashtbl = kzalloc(size, GFP_KERNEL);
203 if (!new_hashtbl) {
204 pr_err(DRV_NAME
205 ": %s: Error: Failed to allocate TLB hash table\n",
206 bond->dev->name);
207 return -1;
209 _lock_tx_hashtbl(bond);
211 bond_info->tx_hashtbl = new_hashtbl;
213 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
214 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
217 _unlock_tx_hashtbl(bond);
219 return 0;
222 /* Must be called only after all slaves have been released */
223 static void tlb_deinitialize(struct bonding *bond)
225 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
227 _lock_tx_hashtbl(bond);
229 kfree(bond_info->tx_hashtbl);
230 bond_info->tx_hashtbl = NULL;
232 _unlock_tx_hashtbl(bond);
235 /* Caller must hold bond lock for read */
236 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
238 struct slave *slave, *least_loaded;
239 s64 max_gap;
240 int i, found = 0;
242 /* Find the first enabled slave */
243 bond_for_each_slave(bond, slave, i) {
244 if (SLAVE_IS_OK(slave)) {
245 found = 1;
246 break;
250 if (!found) {
251 return NULL;
254 least_loaded = slave;
255 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
256 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
258 /* Find the slave with the largest gap */
259 bond_for_each_slave_from(bond, slave, i, least_loaded) {
260 if (SLAVE_IS_OK(slave)) {
261 s64 gap = (s64)(slave->speed << 20) -
262 (s64)(SLAVE_TLB_INFO(slave).load << 3);
263 if (max_gap < gap) {
264 least_loaded = slave;
265 max_gap = gap;
270 return least_loaded;
273 /* Caller must hold bond lock for read */
274 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
276 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
277 struct tlb_client_info *hash_table;
278 struct slave *assigned_slave;
280 _lock_tx_hashtbl(bond);
282 hash_table = bond_info->tx_hashtbl;
283 assigned_slave = hash_table[hash_index].tx_slave;
284 if (!assigned_slave) {
285 assigned_slave = tlb_get_least_loaded_slave(bond);
287 if (assigned_slave) {
288 struct tlb_slave_info *slave_info =
289 &(SLAVE_TLB_INFO(assigned_slave));
290 u32 next_index = slave_info->head;
292 hash_table[hash_index].tx_slave = assigned_slave;
293 hash_table[hash_index].next = next_index;
294 hash_table[hash_index].prev = TLB_NULL_INDEX;
296 if (next_index != TLB_NULL_INDEX) {
297 hash_table[next_index].prev = hash_index;
300 slave_info->head = hash_index;
301 slave_info->load +=
302 hash_table[hash_index].load_history;
306 if (assigned_slave) {
307 hash_table[hash_index].tx_bytes += skb_len;
310 _unlock_tx_hashtbl(bond);
312 return assigned_slave;
315 /*********************** rlb specific functions ***************************/
316 static inline void _lock_rx_hashtbl(struct bonding *bond)
318 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
321 static inline void _unlock_rx_hashtbl(struct bonding *bond)
323 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
326 /* when an ARP REPLY is received from a client update its info
327 * in the rx_hashtbl
329 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
331 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
332 struct rlb_client_info *client_info;
333 u32 hash_index;
335 _lock_rx_hashtbl(bond);
337 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
338 client_info = &(bond_info->rx_hashtbl[hash_index]);
340 if ((client_info->assigned) &&
341 (client_info->ip_src == arp->ip_dst) &&
342 (client_info->ip_dst == arp->ip_src)) {
343 /* update the clients MAC address */
344 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
345 client_info->ntt = 1;
346 bond_info->rx_ntt = 1;
349 _unlock_rx_hashtbl(bond);
352 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
354 struct bonding *bond;
355 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
356 int res = NET_RX_DROP;
358 if (dev_net(bond_dev) != &init_net)
359 goto out;
361 while (bond_dev->priv_flags & IFF_802_1Q_VLAN)
362 bond_dev = vlan_dev_real_dev(bond_dev);
364 if (!(bond_dev->priv_flags & IFF_BONDING) ||
365 !(bond_dev->flags & IFF_MASTER))
366 goto out;
368 if (!arp) {
369 pr_debug("Packet has no ARP data\n");
370 goto out;
373 skb = skb_share_check(skb, GFP_ATOMIC);
374 if (!skb)
375 goto out;
377 if (!pskb_may_pull(skb, arp_hdr_len(bond_dev)))
378 goto out;
380 if (skb->len < sizeof(struct arp_pkt)) {
381 pr_debug("Packet is too small to be an ARP\n");
382 goto out;
385 if (arp->op_code == htons(ARPOP_REPLY)) {
386 /* update rx hash table for this ARP */
387 bond = netdev_priv(bond_dev);
388 rlb_update_entry_from_arp(bond, arp);
389 pr_debug("Server received an ARP Reply from client\n");
392 res = NET_RX_SUCCESS;
394 out:
395 dev_kfree_skb(skb);
397 return res;
400 /* Caller must hold bond lock for read */
401 static struct slave *rlb_next_rx_slave(struct bonding *bond)
403 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
404 struct slave *rx_slave, *slave, *start_at;
405 int i = 0;
407 if (bond_info->next_rx_slave) {
408 start_at = bond_info->next_rx_slave;
409 } else {
410 start_at = bond->first_slave;
413 rx_slave = NULL;
415 bond_for_each_slave_from(bond, slave, i, start_at) {
416 if (SLAVE_IS_OK(slave)) {
417 if (!rx_slave) {
418 rx_slave = slave;
419 } else if (slave->speed > rx_slave->speed) {
420 rx_slave = slave;
425 if (rx_slave) {
426 bond_info->next_rx_slave = rx_slave->next;
429 return rx_slave;
432 /* teach the switch the mac of a disabled slave
433 * on the primary for fault tolerance
435 * Caller must hold bond->curr_slave_lock for write or bond lock for write
437 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
439 if (!bond->curr_active_slave) {
440 return;
443 if (!bond->alb_info.primary_is_promisc) {
444 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
445 bond->alb_info.primary_is_promisc = 1;
446 else
447 bond->alb_info.primary_is_promisc = 0;
450 bond->alb_info.rlb_promisc_timeout_counter = 0;
452 alb_send_learning_packets(bond->curr_active_slave, addr);
455 /* slave being removed should not be active at this point
457 * Caller must hold bond lock for read
459 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
461 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
462 struct rlb_client_info *rx_hash_table;
463 u32 index, next_index;
465 /* clear slave from rx_hashtbl */
466 _lock_rx_hashtbl(bond);
468 rx_hash_table = bond_info->rx_hashtbl;
469 index = bond_info->rx_hashtbl_head;
470 for (; index != RLB_NULL_INDEX; index = next_index) {
471 next_index = rx_hash_table[index].next;
472 if (rx_hash_table[index].slave == slave) {
473 struct slave *assigned_slave = rlb_next_rx_slave(bond);
475 if (assigned_slave) {
476 rx_hash_table[index].slave = assigned_slave;
477 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
478 mac_bcast)) {
479 bond_info->rx_hashtbl[index].ntt = 1;
480 bond_info->rx_ntt = 1;
481 /* A slave has been removed from the
482 * table because it is either disabled
483 * or being released. We must retry the
484 * update to avoid clients from not
485 * being updated & disconnecting when
486 * there is stress
488 bond_info->rlb_update_retry_counter =
489 RLB_UPDATE_RETRY;
491 } else { /* there is no active slave */
492 rx_hash_table[index].slave = NULL;
497 _unlock_rx_hashtbl(bond);
499 write_lock_bh(&bond->curr_slave_lock);
501 if (slave != bond->curr_active_slave) {
502 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
505 write_unlock_bh(&bond->curr_slave_lock);
508 static void rlb_update_client(struct rlb_client_info *client_info)
510 int i;
512 if (!client_info->slave) {
513 return;
516 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
517 struct sk_buff *skb;
519 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
520 client_info->ip_dst,
521 client_info->slave->dev,
522 client_info->ip_src,
523 client_info->mac_dst,
524 client_info->slave->dev->dev_addr,
525 client_info->mac_dst);
526 if (!skb) {
527 pr_err(DRV_NAME
528 ": %s: Error: failed to create an ARP packet\n",
529 client_info->slave->dev->master->name);
530 continue;
533 skb->dev = client_info->slave->dev;
535 if (client_info->tag) {
536 skb = vlan_put_tag(skb, client_info->vlan_id);
537 if (!skb) {
538 pr_err(DRV_NAME
539 ": %s: Error: failed to insert VLAN tag\n",
540 client_info->slave->dev->master->name);
541 continue;
545 arp_xmit(skb);
549 /* sends ARP REPLIES that update the clients that need updating */
550 static void rlb_update_rx_clients(struct bonding *bond)
552 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
553 struct rlb_client_info *client_info;
554 u32 hash_index;
556 _lock_rx_hashtbl(bond);
558 hash_index = bond_info->rx_hashtbl_head;
559 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
560 client_info = &(bond_info->rx_hashtbl[hash_index]);
561 if (client_info->ntt) {
562 rlb_update_client(client_info);
563 if (bond_info->rlb_update_retry_counter == 0) {
564 client_info->ntt = 0;
569 /* do not update the entries again untill this counter is zero so that
570 * not to confuse the clients.
572 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
574 _unlock_rx_hashtbl(bond);
577 /* The slave was assigned a new mac address - update the clients */
578 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
580 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
581 struct rlb_client_info *client_info;
582 int ntt = 0;
583 u32 hash_index;
585 _lock_rx_hashtbl(bond);
587 hash_index = bond_info->rx_hashtbl_head;
588 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
589 client_info = &(bond_info->rx_hashtbl[hash_index]);
591 if ((client_info->slave == slave) &&
592 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
593 client_info->ntt = 1;
594 ntt = 1;
598 // update the team's flag only after the whole iteration
599 if (ntt) {
600 bond_info->rx_ntt = 1;
601 //fasten the change
602 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
605 _unlock_rx_hashtbl(bond);
608 /* mark all clients using src_ip to be updated */
609 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
611 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
612 struct rlb_client_info *client_info;
613 u32 hash_index;
615 _lock_rx_hashtbl(bond);
617 hash_index = bond_info->rx_hashtbl_head;
618 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
619 client_info = &(bond_info->rx_hashtbl[hash_index]);
621 if (!client_info->slave) {
622 pr_err(DRV_NAME
623 ": %s: Error: found a client with no channel in "
624 "the client's hash table\n",
625 bond->dev->name);
626 continue;
628 /*update all clients using this src_ip, that are not assigned
629 * to the team's address (curr_active_slave) and have a known
630 * unicast mac address.
632 if ((client_info->ip_src == src_ip) &&
633 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
634 bond->dev->dev_addr) &&
635 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
636 client_info->ntt = 1;
637 bond_info->rx_ntt = 1;
641 _unlock_rx_hashtbl(bond);
644 /* Caller must hold both bond and ptr locks for read */
645 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
647 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
648 struct arp_pkt *arp = arp_pkt(skb);
649 struct slave *assigned_slave;
650 struct rlb_client_info *client_info;
651 u32 hash_index = 0;
653 _lock_rx_hashtbl(bond);
655 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
656 client_info = &(bond_info->rx_hashtbl[hash_index]);
658 if (client_info->assigned) {
659 if ((client_info->ip_src == arp->ip_src) &&
660 (client_info->ip_dst == arp->ip_dst)) {
661 /* the entry is already assigned to this client */
662 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
663 /* update mac address from arp */
664 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
667 assigned_slave = client_info->slave;
668 if (assigned_slave) {
669 _unlock_rx_hashtbl(bond);
670 return assigned_slave;
672 } else {
673 /* the entry is already assigned to some other client,
674 * move the old client to primary (curr_active_slave) so
675 * that the new client can be assigned to this entry.
677 if (bond->curr_active_slave &&
678 client_info->slave != bond->curr_active_slave) {
679 client_info->slave = bond->curr_active_slave;
680 rlb_update_client(client_info);
684 /* assign a new slave */
685 assigned_slave = rlb_next_rx_slave(bond);
687 if (assigned_slave) {
688 client_info->ip_src = arp->ip_src;
689 client_info->ip_dst = arp->ip_dst;
690 /* arp->mac_dst is broadcast for arp reqeusts.
691 * will be updated with clients actual unicast mac address
692 * upon receiving an arp reply.
694 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
695 client_info->slave = assigned_slave;
697 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
698 client_info->ntt = 1;
699 bond->alb_info.rx_ntt = 1;
700 } else {
701 client_info->ntt = 0;
704 if (!list_empty(&bond->vlan_list)) {
705 if (!vlan_get_tag(skb, &client_info->vlan_id))
706 client_info->tag = 1;
709 if (!client_info->assigned) {
710 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
711 bond_info->rx_hashtbl_head = hash_index;
712 client_info->next = prev_tbl_head;
713 if (prev_tbl_head != RLB_NULL_INDEX) {
714 bond_info->rx_hashtbl[prev_tbl_head].prev =
715 hash_index;
717 client_info->assigned = 1;
721 _unlock_rx_hashtbl(bond);
723 return assigned_slave;
726 /* chooses (and returns) transmit channel for arp reply
727 * does not choose channel for other arp types since they are
728 * sent on the curr_active_slave
730 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
732 struct arp_pkt *arp = arp_pkt(skb);
733 struct slave *tx_slave = NULL;
735 if (arp->op_code == htons(ARPOP_REPLY)) {
736 /* the arp must be sent on the selected
737 * rx channel
739 tx_slave = rlb_choose_channel(skb, bond);
740 if (tx_slave) {
741 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
743 pr_debug("Server sent ARP Reply packet\n");
744 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
745 /* Create an entry in the rx_hashtbl for this client as a
746 * place holder.
747 * When the arp reply is received the entry will be updated
748 * with the correct unicast address of the client.
750 rlb_choose_channel(skb, bond);
752 /* The ARP relpy packets must be delayed so that
753 * they can cancel out the influence of the ARP request.
755 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
757 /* arp requests are broadcast and are sent on the primary
758 * the arp request will collapse all clients on the subnet to
759 * the primary slave. We must register these clients to be
760 * updated with their assigned mac.
762 rlb_req_update_subnet_clients(bond, arp->ip_src);
763 pr_debug("Server sent ARP Request packet\n");
766 return tx_slave;
769 /* Caller must hold bond lock for read */
770 static void rlb_rebalance(struct bonding *bond)
772 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
773 struct slave *assigned_slave;
774 struct rlb_client_info *client_info;
775 int ntt;
776 u32 hash_index;
778 _lock_rx_hashtbl(bond);
780 ntt = 0;
781 hash_index = bond_info->rx_hashtbl_head;
782 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
783 client_info = &(bond_info->rx_hashtbl[hash_index]);
784 assigned_slave = rlb_next_rx_slave(bond);
785 if (assigned_slave && (client_info->slave != assigned_slave)) {
786 client_info->slave = assigned_slave;
787 client_info->ntt = 1;
788 ntt = 1;
792 /* update the team's flag only after the whole iteration */
793 if (ntt) {
794 bond_info->rx_ntt = 1;
796 _unlock_rx_hashtbl(bond);
799 /* Caller must hold rx_hashtbl lock */
800 static void rlb_init_table_entry(struct rlb_client_info *entry)
802 memset(entry, 0, sizeof(struct rlb_client_info));
803 entry->next = RLB_NULL_INDEX;
804 entry->prev = RLB_NULL_INDEX;
807 static int rlb_initialize(struct bonding *bond)
809 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
810 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
811 struct rlb_client_info *new_hashtbl;
812 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
813 int i;
815 spin_lock_init(&(bond_info->rx_hashtbl_lock));
817 new_hashtbl = kmalloc(size, GFP_KERNEL);
818 if (!new_hashtbl) {
819 pr_err(DRV_NAME
820 ": %s: Error: Failed to allocate RLB hash table\n",
821 bond->dev->name);
822 return -1;
824 _lock_rx_hashtbl(bond);
826 bond_info->rx_hashtbl = new_hashtbl;
828 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
830 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
831 rlb_init_table_entry(bond_info->rx_hashtbl + i);
834 _unlock_rx_hashtbl(bond);
836 /*initialize packet type*/
837 pk_type->type = cpu_to_be16(ETH_P_ARP);
838 pk_type->dev = NULL;
839 pk_type->func = rlb_arp_recv;
841 /* register to receive ARPs */
842 dev_add_pack(pk_type);
844 return 0;
847 static void rlb_deinitialize(struct bonding *bond)
849 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
851 dev_remove_pack(&(bond_info->rlb_pkt_type));
853 _lock_rx_hashtbl(bond);
855 kfree(bond_info->rx_hashtbl);
856 bond_info->rx_hashtbl = NULL;
857 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
859 _unlock_rx_hashtbl(bond);
862 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
864 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
865 u32 curr_index;
867 _lock_rx_hashtbl(bond);
869 curr_index = bond_info->rx_hashtbl_head;
870 while (curr_index != RLB_NULL_INDEX) {
871 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
872 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
873 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
875 if (curr->tag && (curr->vlan_id == vlan_id)) {
876 if (curr_index == bond_info->rx_hashtbl_head) {
877 bond_info->rx_hashtbl_head = next_index;
879 if (prev_index != RLB_NULL_INDEX) {
880 bond_info->rx_hashtbl[prev_index].next = next_index;
882 if (next_index != RLB_NULL_INDEX) {
883 bond_info->rx_hashtbl[next_index].prev = prev_index;
886 rlb_init_table_entry(curr);
889 curr_index = next_index;
892 _unlock_rx_hashtbl(bond);
895 /*********************** tlb/rlb shared functions *********************/
897 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
899 struct bonding *bond = bond_get_bond_by_slave(slave);
900 struct learning_pkt pkt;
901 int size = sizeof(struct learning_pkt);
902 int i;
904 memset(&pkt, 0, size);
905 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
906 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
907 pkt.type = cpu_to_be16(ETH_P_LOOP);
909 for (i = 0; i < MAX_LP_BURST; i++) {
910 struct sk_buff *skb;
911 char *data;
913 skb = dev_alloc_skb(size);
914 if (!skb) {
915 return;
918 data = skb_put(skb, size);
919 memcpy(data, &pkt, size);
921 skb_reset_mac_header(skb);
922 skb->network_header = skb->mac_header + ETH_HLEN;
923 skb->protocol = pkt.type;
924 skb->priority = TC_PRIO_CONTROL;
925 skb->dev = slave->dev;
927 if (!list_empty(&bond->vlan_list)) {
928 struct vlan_entry *vlan;
930 vlan = bond_next_vlan(bond,
931 bond->alb_info.current_alb_vlan);
933 bond->alb_info.current_alb_vlan = vlan;
934 if (!vlan) {
935 kfree_skb(skb);
936 continue;
939 skb = vlan_put_tag(skb, vlan->vlan_id);
940 if (!skb) {
941 pr_err(DRV_NAME
942 ": %s: Error: failed to insert VLAN tag\n",
943 bond->dev->name);
944 continue;
948 dev_queue_xmit(skb);
952 /* hw is a boolean parameter that determines whether we should try and
953 * set the hw address of the device as well as the hw address of the
954 * net_device
956 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
958 struct net_device *dev = slave->dev;
959 struct sockaddr s_addr;
961 if (!hw) {
962 memcpy(dev->dev_addr, addr, dev->addr_len);
963 return 0;
966 /* for rlb each slave must have a unique hw mac addresses so that */
967 /* each slave will receive packets destined to a different mac */
968 memcpy(s_addr.sa_data, addr, dev->addr_len);
969 s_addr.sa_family = dev->type;
970 if (dev_set_mac_address(dev, &s_addr)) {
971 pr_err(DRV_NAME
972 ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
973 "mode requires that the base driver support setting "
974 "the hw address also when the network device's "
975 "interface is open\n",
976 dev->master->name, dev->name);
977 return -EOPNOTSUPP;
979 return 0;
983 * Swap MAC addresses between two slaves.
985 * Called with RTNL held, and no other locks.
989 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
991 u8 tmp_mac_addr[ETH_ALEN];
993 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
994 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
995 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
1000 * Send learning packets after MAC address swap.
1002 * Called with RTNL and no other locks
1004 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
1005 struct slave *slave2)
1007 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
1008 struct slave *disabled_slave = NULL;
1010 ASSERT_RTNL();
1012 /* fasten the change in the switch */
1013 if (SLAVE_IS_OK(slave1)) {
1014 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
1015 if (bond->alb_info.rlb_enabled) {
1016 /* inform the clients that the mac address
1017 * has changed
1019 rlb_req_update_slave_clients(bond, slave1);
1021 } else {
1022 disabled_slave = slave1;
1025 if (SLAVE_IS_OK(slave2)) {
1026 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1027 if (bond->alb_info.rlb_enabled) {
1028 /* inform the clients that the mac address
1029 * has changed
1031 rlb_req_update_slave_clients(bond, slave2);
1033 } else {
1034 disabled_slave = slave2;
1037 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1038 /* A disabled slave was assigned an active mac addr */
1039 rlb_teach_disabled_mac_on_primary(bond,
1040 disabled_slave->dev->dev_addr);
1045 * alb_change_hw_addr_on_detach
1046 * @bond: bonding we're working on
1047 * @slave: the slave that was just detached
1049 * We assume that @slave was already detached from the slave list.
1051 * If @slave's permanent hw address is different both from its current
1052 * address and from @bond's address, then somewhere in the bond there's
1053 * a slave that has @slave's permanet address as its current address.
1054 * We'll make sure that that slave no longer uses @slave's permanent address.
1056 * Caller must hold RTNL and no other locks
1058 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1060 int perm_curr_diff;
1061 int perm_bond_diff;
1063 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1064 slave->dev->dev_addr);
1065 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1066 bond->dev->dev_addr);
1068 if (perm_curr_diff && perm_bond_diff) {
1069 struct slave *tmp_slave;
1070 int i, found = 0;
1072 bond_for_each_slave(bond, tmp_slave, i) {
1073 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
1074 tmp_slave->dev->dev_addr)) {
1075 found = 1;
1076 break;
1080 if (found) {
1081 /* locking: needs RTNL and nothing else */
1082 alb_swap_mac_addr(bond, slave, tmp_slave);
1083 alb_fasten_mac_swap(bond, slave, tmp_slave);
1089 * alb_handle_addr_collision_on_attach
1090 * @bond: bonding we're working on
1091 * @slave: the slave that was just attached
1093 * checks uniqueness of slave's mac address and handles the case the
1094 * new slave uses the bonds mac address.
1096 * If the permanent hw address of @slave is @bond's hw address, we need to
1097 * find a different hw address to give @slave, that isn't in use by any other
1098 * slave in the bond. This address must be, of course, one of the premanent
1099 * addresses of the other slaves.
1101 * We go over the slave list, and for each slave there we compare its
1102 * permanent hw address with the current address of all the other slaves.
1103 * If no match was found, then we've found a slave with a permanent address
1104 * that isn't used by any other slave in the bond, so we can assign it to
1105 * @slave.
1107 * assumption: this function is called before @slave is attached to the
1108 * bond slave list.
1110 * caller must hold the bond lock for write since the mac addresses are compared
1111 * and may be swapped.
1113 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1115 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1116 struct slave *has_bond_addr = bond->curr_active_slave;
1117 int i, j, found = 0;
1119 if (bond->slave_cnt == 0) {
1120 /* this is the first slave */
1121 return 0;
1124 /* if slave's mac address differs from bond's mac address
1125 * check uniqueness of slave's mac address against the other
1126 * slaves in the bond.
1128 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1129 bond_for_each_slave(bond, tmp_slave1, i) {
1130 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1131 slave->dev->dev_addr)) {
1132 found = 1;
1133 break;
1137 if (!found)
1138 return 0;
1140 /* Try setting slave mac to bond address and fall-through
1141 to code handling that situation below... */
1142 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1143 bond->alb_info.rlb_enabled);
1146 /* The slave's address is equal to the address of the bond.
1147 * Search for a spare address in the bond for this slave.
1149 free_mac_slave = NULL;
1151 bond_for_each_slave(bond, tmp_slave1, i) {
1152 found = 0;
1153 bond_for_each_slave(bond, tmp_slave2, j) {
1154 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1155 tmp_slave2->dev->dev_addr)) {
1156 found = 1;
1157 break;
1161 if (!found) {
1162 /* no slave has tmp_slave1's perm addr
1163 * as its curr addr
1165 free_mac_slave = tmp_slave1;
1166 break;
1169 if (!has_bond_addr) {
1170 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1171 bond->dev->dev_addr)) {
1173 has_bond_addr = tmp_slave1;
1178 if (free_mac_slave) {
1179 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1180 bond->alb_info.rlb_enabled);
1182 pr_warning(DRV_NAME
1183 ": %s: Warning: the hw address of slave %s is "
1184 "in use by the bond; giving it the hw address "
1185 "of %s\n",
1186 bond->dev->name, slave->dev->name,
1187 free_mac_slave->dev->name);
1189 } else if (has_bond_addr) {
1190 pr_err(DRV_NAME
1191 ": %s: Error: the hw address of slave %s is in use by the "
1192 "bond; couldn't find a slave with a free hw address to "
1193 "give it (this should not have happened)\n",
1194 bond->dev->name, slave->dev->name);
1195 return -EFAULT;
1198 return 0;
1202 * alb_set_mac_address
1203 * @bond:
1204 * @addr:
1206 * In TLB mode all slaves are configured to the bond's hw address, but set
1207 * their dev_addr field to different addresses (based on their permanent hw
1208 * addresses).
1210 * For each slave, this function sets the interface to the new address and then
1211 * changes its dev_addr field to its previous value.
1213 * Unwinding assumes bond's mac address has not yet changed.
1215 static int alb_set_mac_address(struct bonding *bond, void *addr)
1217 struct sockaddr sa;
1218 struct slave *slave, *stop_at;
1219 char tmp_addr[ETH_ALEN];
1220 int res;
1221 int i;
1223 if (bond->alb_info.rlb_enabled) {
1224 return 0;
1227 bond_for_each_slave(bond, slave, i) {
1228 /* save net_device's current hw address */
1229 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1231 res = dev_set_mac_address(slave->dev, addr);
1233 /* restore net_device's hw address */
1234 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1236 if (res)
1237 goto unwind;
1240 return 0;
1242 unwind:
1243 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1244 sa.sa_family = bond->dev->type;
1246 /* unwind from head to the slave that failed */
1247 stop_at = slave;
1248 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1249 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1250 dev_set_mac_address(slave->dev, &sa);
1251 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1254 return res;
1257 /************************ exported alb funcions ************************/
1259 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1261 int res;
1263 res = tlb_initialize(bond);
1264 if (res) {
1265 return res;
1268 if (rlb_enabled) {
1269 bond->alb_info.rlb_enabled = 1;
1270 /* initialize rlb */
1271 res = rlb_initialize(bond);
1272 if (res) {
1273 tlb_deinitialize(bond);
1274 return res;
1276 } else {
1277 bond->alb_info.rlb_enabled = 0;
1280 return 0;
1283 void bond_alb_deinitialize(struct bonding *bond)
1285 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1287 tlb_deinitialize(bond);
1289 if (bond_info->rlb_enabled) {
1290 rlb_deinitialize(bond);
1294 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1296 struct bonding *bond = netdev_priv(bond_dev);
1297 struct ethhdr *eth_data;
1298 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1299 struct slave *tx_slave = NULL;
1300 static const __be32 ip_bcast = htonl(0xffffffff);
1301 int hash_size = 0;
1302 int do_tx_balance = 1;
1303 u32 hash_index = 0;
1304 const u8 *hash_start = NULL;
1305 int res = 1;
1306 struct ipv6hdr *ip6hdr;
1308 skb_reset_mac_header(skb);
1309 eth_data = eth_hdr(skb);
1311 /* make sure that the curr_active_slave and the slaves list do
1312 * not change during tx
1314 read_lock(&bond->lock);
1315 read_lock(&bond->curr_slave_lock);
1317 if (!BOND_IS_OK(bond)) {
1318 goto out;
1321 switch (ntohs(skb->protocol)) {
1322 case ETH_P_IP: {
1323 const struct iphdr *iph = ip_hdr(skb);
1325 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
1326 (iph->daddr == ip_bcast) ||
1327 (iph->protocol == IPPROTO_IGMP)) {
1328 do_tx_balance = 0;
1329 break;
1331 hash_start = (char *)&(iph->daddr);
1332 hash_size = sizeof(iph->daddr);
1334 break;
1335 case ETH_P_IPV6:
1336 /* IPv6 doesn't really use broadcast mac address, but leave
1337 * that here just in case.
1339 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
1340 do_tx_balance = 0;
1341 break;
1344 /* IPv6 uses all-nodes multicast as an equivalent to
1345 * broadcasts in IPv4.
1347 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1348 do_tx_balance = 0;
1349 break;
1352 /* Additianally, DAD probes should not be tx-balanced as that
1353 * will lead to false positives for duplicate addresses and
1354 * prevent address configuration from working.
1356 ip6hdr = ipv6_hdr(skb);
1357 if (ipv6_addr_any(&ip6hdr->saddr)) {
1358 do_tx_balance = 0;
1359 break;
1362 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1363 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1364 break;
1365 case ETH_P_IPX:
1366 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1367 /* something is wrong with this packet */
1368 do_tx_balance = 0;
1369 break;
1372 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1373 /* The only protocol worth balancing in
1374 * this family since it has an "ARP" like
1375 * mechanism
1377 do_tx_balance = 0;
1378 break;
1381 hash_start = (char*)eth_data->h_dest;
1382 hash_size = ETH_ALEN;
1383 break;
1384 case ETH_P_ARP:
1385 do_tx_balance = 0;
1386 if (bond_info->rlb_enabled) {
1387 tx_slave = rlb_arp_xmit(skb, bond);
1389 break;
1390 default:
1391 do_tx_balance = 0;
1392 break;
1395 if (do_tx_balance) {
1396 hash_index = _simple_hash(hash_start, hash_size);
1397 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1400 if (!tx_slave) {
1401 /* unbalanced or unassigned, send through primary */
1402 tx_slave = bond->curr_active_slave;
1403 bond_info->unbalanced_load += skb->len;
1406 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1407 if (tx_slave != bond->curr_active_slave) {
1408 memcpy(eth_data->h_source,
1409 tx_slave->dev->dev_addr,
1410 ETH_ALEN);
1413 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1414 } else {
1415 if (tx_slave) {
1416 tlb_clear_slave(bond, tx_slave, 0);
1420 out:
1421 if (res) {
1422 /* no suitable interface, frame not sent */
1423 dev_kfree_skb(skb);
1425 read_unlock(&bond->curr_slave_lock);
1426 read_unlock(&bond->lock);
1427 return NETDEV_TX_OK;
1430 void bond_alb_monitor(struct work_struct *work)
1432 struct bonding *bond = container_of(work, struct bonding,
1433 alb_work.work);
1434 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1435 struct slave *slave;
1436 int i;
1438 read_lock(&bond->lock);
1440 if (bond->kill_timers) {
1441 goto out;
1444 if (bond->slave_cnt == 0) {
1445 bond_info->tx_rebalance_counter = 0;
1446 bond_info->lp_counter = 0;
1447 goto re_arm;
1450 bond_info->tx_rebalance_counter++;
1451 bond_info->lp_counter++;
1453 /* send learning packets */
1454 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1455 /* change of curr_active_slave involves swapping of mac addresses.
1456 * in order to avoid this swapping from happening while
1457 * sending the learning packets, the curr_slave_lock must be held for
1458 * read.
1460 read_lock(&bond->curr_slave_lock);
1462 bond_for_each_slave(bond, slave, i) {
1463 alb_send_learning_packets(slave, slave->dev->dev_addr);
1466 read_unlock(&bond->curr_slave_lock);
1468 bond_info->lp_counter = 0;
1471 /* rebalance tx traffic */
1472 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1474 read_lock(&bond->curr_slave_lock);
1476 bond_for_each_slave(bond, slave, i) {
1477 tlb_clear_slave(bond, slave, 1);
1478 if (slave == bond->curr_active_slave) {
1479 SLAVE_TLB_INFO(slave).load =
1480 bond_info->unbalanced_load /
1481 BOND_TLB_REBALANCE_INTERVAL;
1482 bond_info->unbalanced_load = 0;
1486 read_unlock(&bond->curr_slave_lock);
1488 bond_info->tx_rebalance_counter = 0;
1491 /* handle rlb stuff */
1492 if (bond_info->rlb_enabled) {
1493 if (bond_info->primary_is_promisc &&
1494 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1497 * dev_set_promiscuity requires rtnl and
1498 * nothing else.
1500 read_unlock(&bond->lock);
1501 rtnl_lock();
1503 bond_info->rlb_promisc_timeout_counter = 0;
1505 /* If the primary was set to promiscuous mode
1506 * because a slave was disabled then
1507 * it can now leave promiscuous mode.
1509 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1510 bond_info->primary_is_promisc = 0;
1512 rtnl_unlock();
1513 read_lock(&bond->lock);
1516 if (bond_info->rlb_rebalance) {
1517 bond_info->rlb_rebalance = 0;
1518 rlb_rebalance(bond);
1521 /* check if clients need updating */
1522 if (bond_info->rx_ntt) {
1523 if (bond_info->rlb_update_delay_counter) {
1524 --bond_info->rlb_update_delay_counter;
1525 } else {
1526 rlb_update_rx_clients(bond);
1527 if (bond_info->rlb_update_retry_counter) {
1528 --bond_info->rlb_update_retry_counter;
1529 } else {
1530 bond_info->rx_ntt = 0;
1536 re_arm:
1537 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1538 out:
1539 read_unlock(&bond->lock);
1542 /* assumption: called before the slave is attached to the bond
1543 * and not locked by the bond lock
1545 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1547 int res;
1549 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1550 bond->alb_info.rlb_enabled);
1551 if (res) {
1552 return res;
1555 /* caller must hold the bond lock for write since the mac addresses
1556 * are compared and may be swapped.
1558 read_lock(&bond->lock);
1560 res = alb_handle_addr_collision_on_attach(bond, slave);
1562 read_unlock(&bond->lock);
1564 if (res) {
1565 return res;
1568 tlb_init_slave(slave);
1570 /* order a rebalance ASAP */
1571 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1573 if (bond->alb_info.rlb_enabled) {
1574 bond->alb_info.rlb_rebalance = 1;
1577 return 0;
1581 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1582 * if necessary.
1584 * Caller must hold RTNL and no other locks
1586 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1588 if (bond->slave_cnt > 1) {
1589 alb_change_hw_addr_on_detach(bond, slave);
1592 tlb_clear_slave(bond, slave, 0);
1594 if (bond->alb_info.rlb_enabled) {
1595 bond->alb_info.next_rx_slave = NULL;
1596 rlb_clear_slave(bond, slave);
1600 /* Caller must hold bond lock for read */
1601 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1603 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1605 if (link == BOND_LINK_DOWN) {
1606 tlb_clear_slave(bond, slave, 0);
1607 if (bond->alb_info.rlb_enabled) {
1608 rlb_clear_slave(bond, slave);
1610 } else if (link == BOND_LINK_UP) {
1611 /* order a rebalance ASAP */
1612 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1613 if (bond->alb_info.rlb_enabled) {
1614 bond->alb_info.rlb_rebalance = 1;
1615 /* If the updelay module parameter is smaller than the
1616 * forwarding delay of the switch the rebalance will
1617 * not work because the rebalance arp replies will
1618 * not be forwarded to the clients..
1625 * bond_alb_handle_active_change - assign new curr_active_slave
1626 * @bond: our bonding struct
1627 * @new_slave: new slave to assign
1629 * Set the bond->curr_active_slave to @new_slave and handle
1630 * mac address swapping and promiscuity changes as needed.
1632 * If new_slave is NULL, caller must hold curr_slave_lock or
1633 * bond->lock for write.
1635 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1636 * read and curr_slave_lock for write. Processing here may sleep, so
1637 * no other locks may be held.
1639 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1640 __releases(&bond->curr_slave_lock)
1641 __releases(&bond->lock)
1642 __acquires(&bond->lock)
1643 __acquires(&bond->curr_slave_lock)
1645 struct slave *swap_slave;
1646 int i;
1648 if (bond->curr_active_slave == new_slave) {
1649 return;
1652 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1653 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1654 bond->alb_info.primary_is_promisc = 0;
1655 bond->alb_info.rlb_promisc_timeout_counter = 0;
1658 swap_slave = bond->curr_active_slave;
1659 bond->curr_active_slave = new_slave;
1661 if (!new_slave || (bond->slave_cnt == 0)) {
1662 return;
1665 /* set the new curr_active_slave to the bonds mac address
1666 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1668 if (!swap_slave) {
1669 struct slave *tmp_slave;
1670 /* find slave that is holding the bond's mac address */
1671 bond_for_each_slave(bond, tmp_slave, i) {
1672 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1673 bond->dev->dev_addr)) {
1674 swap_slave = tmp_slave;
1675 break;
1681 * Arrange for swap_slave and new_slave to temporarily be
1682 * ignored so we can mess with their MAC addresses without
1683 * fear of interference from transmit activity.
1685 if (swap_slave) {
1686 tlb_clear_slave(bond, swap_slave, 1);
1688 tlb_clear_slave(bond, new_slave, 1);
1690 write_unlock_bh(&bond->curr_slave_lock);
1691 read_unlock(&bond->lock);
1693 ASSERT_RTNL();
1695 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1696 if (swap_slave) {
1697 /* swap mac address */
1698 alb_swap_mac_addr(bond, swap_slave, new_slave);
1699 } else {
1700 /* set the new_slave to the bond mac address */
1701 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1702 bond->alb_info.rlb_enabled);
1705 if (swap_slave) {
1706 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1707 read_lock(&bond->lock);
1708 } else {
1709 read_lock(&bond->lock);
1710 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1713 write_lock_bh(&bond->curr_slave_lock);
1717 * Called with RTNL
1719 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1720 __acquires(&bond->lock)
1721 __releases(&bond->lock)
1723 struct bonding *bond = netdev_priv(bond_dev);
1724 struct sockaddr *sa = addr;
1725 struct slave *slave, *swap_slave;
1726 int res;
1727 int i;
1729 if (!is_valid_ether_addr(sa->sa_data)) {
1730 return -EADDRNOTAVAIL;
1733 res = alb_set_mac_address(bond, addr);
1734 if (res) {
1735 return res;
1738 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1740 /* If there is no curr_active_slave there is nothing else to do.
1741 * Otherwise we'll need to pass the new address to it and handle
1742 * duplications.
1744 if (!bond->curr_active_slave) {
1745 return 0;
1748 swap_slave = NULL;
1750 bond_for_each_slave(bond, slave, i) {
1751 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1752 bond_dev->dev_addr)) {
1753 swap_slave = slave;
1754 break;
1758 if (swap_slave) {
1759 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1760 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1761 } else {
1762 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1763 bond->alb_info.rlb_enabled);
1765 read_lock(&bond->lock);
1766 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1767 if (bond->alb_info.rlb_enabled) {
1768 /* inform clients mac address has changed */
1769 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1771 read_unlock(&bond->lock);
1774 return 0;
1777 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1779 if (bond->alb_info.current_alb_vlan &&
1780 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1781 bond->alb_info.current_alb_vlan = NULL;
1784 if (bond->alb_info.rlb_enabled) {
1785 rlb_clear_vlan(bond, vlan_id);