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>
48 #ifndef __long_aligned
49 #define __long_aligned __attribute__((aligned((sizeof(long)))))
51 static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
52 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
54 static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
55 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
57 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
64 u8 padding[ETH_ZLEN - ETH_HLEN];
69 __be16 prot_addr_space;
73 u8 mac_src[ETH_ALEN]; /* sender hardware address */
74 __be32 ip_src; /* sender IP address */
75 u8 mac_dst[ETH_ALEN]; /* target hardware address */
76 __be32 ip_dst; /* target IP address */
80 static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
82 return (struct arp_pkt *)skb_network_header(skb);
85 /* Forward declaration */
86 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
88 static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
93 for (i = 0; i < hash_size; i++) {
94 hash ^= hash_start[i];
100 /*********************** tlb specific functions ***************************/
102 static inline void _lock_tx_hashtbl_bh(struct bonding *bond)
104 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
107 static inline void _unlock_tx_hashtbl_bh(struct bonding *bond)
109 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
112 static inline void _lock_tx_hashtbl(struct bonding *bond)
114 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
117 static inline void _unlock_tx_hashtbl(struct bonding *bond)
119 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
122 /* Caller must hold tx_hashtbl lock */
123 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
126 entry->load_history = 1 + entry->tx_bytes /
127 BOND_TLB_REBALANCE_INTERVAL;
131 entry->tx_slave = NULL;
132 entry->next = TLB_NULL_INDEX;
133 entry->prev = TLB_NULL_INDEX;
136 static inline void tlb_init_slave(struct slave *slave)
138 SLAVE_TLB_INFO(slave).load = 0;
139 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
142 /* Caller must hold bond lock for read, BH disabled */
143 static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
146 struct tlb_client_info *tx_hash_table;
149 /* clear slave from tx_hashtbl */
150 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
152 /* skip this if we've already freed the tx hash table */
154 index = SLAVE_TLB_INFO(slave).head;
155 while (index != TLB_NULL_INDEX) {
156 u32 next_index = tx_hash_table[index].next;
157 tlb_init_table_entry(&tx_hash_table[index], save_load);
162 tlb_init_slave(slave);
165 /* Caller must hold bond lock for read */
166 static void tlb_clear_slave(struct bonding *bond, struct slave *slave,
169 _lock_tx_hashtbl_bh(bond);
170 __tlb_clear_slave(bond, slave, save_load);
171 _unlock_tx_hashtbl_bh(bond);
174 /* Must be called before starting the monitor timer */
175 static int tlb_initialize(struct bonding *bond)
177 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
178 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
179 struct tlb_client_info *new_hashtbl;
182 new_hashtbl = kzalloc(size, GFP_KERNEL);
186 _lock_tx_hashtbl_bh(bond);
188 bond_info->tx_hashtbl = new_hashtbl;
190 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
191 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
194 _unlock_tx_hashtbl_bh(bond);
199 /* Must be called only after all slaves have been released */
200 static void tlb_deinitialize(struct bonding *bond)
202 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
204 _lock_tx_hashtbl_bh(bond);
206 kfree(bond_info->tx_hashtbl);
207 bond_info->tx_hashtbl = NULL;
209 _unlock_tx_hashtbl_bh(bond);
212 static long long compute_gap(struct slave *slave)
214 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
215 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
218 /* Caller must hold bond lock for read */
219 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
221 struct slave *slave, *least_loaded;
228 /* Find the slave with the largest gap */
229 bond_for_each_slave(bond, slave, i) {
230 if (SLAVE_IS_OK(slave)) {
231 long long gap = compute_gap(slave);
234 least_loaded = slave;
243 static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
246 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
247 struct tlb_client_info *hash_table;
248 struct slave *assigned_slave;
250 hash_table = bond_info->tx_hashtbl;
251 assigned_slave = hash_table[hash_index].tx_slave;
252 if (!assigned_slave) {
253 assigned_slave = tlb_get_least_loaded_slave(bond);
255 if (assigned_slave) {
256 struct tlb_slave_info *slave_info =
257 &(SLAVE_TLB_INFO(assigned_slave));
258 u32 next_index = slave_info->head;
260 hash_table[hash_index].tx_slave = assigned_slave;
261 hash_table[hash_index].next = next_index;
262 hash_table[hash_index].prev = TLB_NULL_INDEX;
264 if (next_index != TLB_NULL_INDEX) {
265 hash_table[next_index].prev = hash_index;
268 slave_info->head = hash_index;
270 hash_table[hash_index].load_history;
274 if (assigned_slave) {
275 hash_table[hash_index].tx_bytes += skb_len;
278 return assigned_slave;
281 /* Caller must hold bond lock for read */
282 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
285 struct slave *tx_slave;
287 * We don't need to disable softirq here, becase
288 * tlb_choose_channel() is only called by bond_alb_xmit()
289 * which already has softirq disabled.
291 _lock_tx_hashtbl(bond);
292 tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
293 _unlock_tx_hashtbl(bond);
297 /*********************** rlb specific functions ***************************/
298 static inline void _lock_rx_hashtbl_bh(struct bonding *bond)
300 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
303 static inline void _unlock_rx_hashtbl_bh(struct bonding *bond)
305 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
308 static inline void _lock_rx_hashtbl(struct bonding *bond)
310 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
313 static inline void _unlock_rx_hashtbl(struct bonding *bond)
315 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
318 /* when an ARP REPLY is received from a client update its info
321 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
323 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
324 struct rlb_client_info *client_info;
327 _lock_rx_hashtbl_bh(bond);
329 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
330 client_info = &(bond_info->rx_hashtbl[hash_index]);
332 if ((client_info->assigned) &&
333 (client_info->ip_src == arp->ip_dst) &&
334 (client_info->ip_dst == arp->ip_src) &&
335 (compare_ether_addr_64bits(client_info->mac_dst, arp->mac_src))) {
336 /* update the clients MAC address */
337 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
338 client_info->ntt = 1;
339 bond_info->rx_ntt = 1;
342 _unlock_rx_hashtbl_bh(bond);
345 static void rlb_arp_recv(struct sk_buff *skb, struct bonding *bond,
350 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
353 arp = (struct arp_pkt *) skb->data;
355 pr_debug("Packet has no ARP data\n");
359 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
362 if (skb->len < sizeof(struct arp_pkt)) {
363 pr_debug("Packet is too small to be an ARP\n");
367 if (arp->op_code == htons(ARPOP_REPLY)) {
368 /* update rx hash table for this ARP */
369 rlb_update_entry_from_arp(bond, arp);
370 pr_debug("Server received an ARP Reply from client\n");
374 /* Caller must hold bond lock for read */
375 static struct slave *rlb_next_rx_slave(struct bonding *bond)
377 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
378 struct slave *rx_slave, *slave, *start_at;
381 if (bond_info->next_rx_slave) {
382 start_at = bond_info->next_rx_slave;
384 start_at = bond->first_slave;
389 bond_for_each_slave_from(bond, slave, i, start_at) {
390 if (SLAVE_IS_OK(slave)) {
393 } else if (slave->speed > rx_slave->speed) {
400 bond_info->next_rx_slave = rx_slave->next;
406 /* teach the switch the mac of a disabled slave
407 * on the primary for fault tolerance
409 * Caller must hold bond->curr_slave_lock for write or bond lock for write
411 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
413 if (!bond->curr_active_slave) {
417 if (!bond->alb_info.primary_is_promisc) {
418 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
419 bond->alb_info.primary_is_promisc = 1;
421 bond->alb_info.primary_is_promisc = 0;
424 bond->alb_info.rlb_promisc_timeout_counter = 0;
426 alb_send_learning_packets(bond->curr_active_slave, addr);
429 /* slave being removed should not be active at this point
431 * Caller must hold bond lock for read
433 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
435 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
436 struct rlb_client_info *rx_hash_table;
437 u32 index, next_index;
439 /* clear slave from rx_hashtbl */
440 _lock_rx_hashtbl_bh(bond);
442 rx_hash_table = bond_info->rx_hashtbl;
443 index = bond_info->rx_hashtbl_head;
444 for (; index != RLB_NULL_INDEX; index = next_index) {
445 next_index = rx_hash_table[index].next;
446 if (rx_hash_table[index].slave == slave) {
447 struct slave *assigned_slave = rlb_next_rx_slave(bond);
449 if (assigned_slave) {
450 rx_hash_table[index].slave = assigned_slave;
451 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
453 bond_info->rx_hashtbl[index].ntt = 1;
454 bond_info->rx_ntt = 1;
455 /* A slave has been removed from the
456 * table because it is either disabled
457 * or being released. We must retry the
458 * update to avoid clients from not
459 * being updated & disconnecting when
462 bond_info->rlb_update_retry_counter =
465 } else { /* there is no active slave */
466 rx_hash_table[index].slave = NULL;
471 _unlock_rx_hashtbl_bh(bond);
473 write_lock_bh(&bond->curr_slave_lock);
475 if (slave != bond->curr_active_slave) {
476 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
479 write_unlock_bh(&bond->curr_slave_lock);
482 static void rlb_update_client(struct rlb_client_info *client_info)
486 if (!client_info->slave) {
490 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
493 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
495 client_info->slave->dev,
497 client_info->mac_dst,
498 client_info->slave->dev->dev_addr,
499 client_info->mac_dst);
501 pr_err("%s: Error: failed to create an ARP packet\n",
502 client_info->slave->dev->master->name);
506 skb->dev = client_info->slave->dev;
508 if (client_info->tag) {
509 skb = vlan_put_tag(skb, client_info->vlan_id);
511 pr_err("%s: Error: failed to insert VLAN tag\n",
512 client_info->slave->dev->master->name);
521 /* sends ARP REPLIES that update the clients that need updating */
522 static void rlb_update_rx_clients(struct bonding *bond)
524 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
525 struct rlb_client_info *client_info;
528 _lock_rx_hashtbl_bh(bond);
530 hash_index = bond_info->rx_hashtbl_head;
531 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
532 client_info = &(bond_info->rx_hashtbl[hash_index]);
533 if (client_info->ntt) {
534 rlb_update_client(client_info);
535 if (bond_info->rlb_update_retry_counter == 0) {
536 client_info->ntt = 0;
541 /* do not update the entries again until this counter is zero so that
542 * not to confuse the clients.
544 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
546 _unlock_rx_hashtbl_bh(bond);
549 /* The slave was assigned a new mac address - update the clients */
550 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
552 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
553 struct rlb_client_info *client_info;
557 _lock_rx_hashtbl_bh(bond);
559 hash_index = bond_info->rx_hashtbl_head;
560 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
561 client_info = &(bond_info->rx_hashtbl[hash_index]);
563 if ((client_info->slave == slave) &&
564 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
565 client_info->ntt = 1;
570 // update the team's flag only after the whole iteration
572 bond_info->rx_ntt = 1;
574 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
577 _unlock_rx_hashtbl_bh(bond);
580 /* mark all clients using src_ip to be updated */
581 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
583 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
584 struct rlb_client_info *client_info;
587 _lock_rx_hashtbl(bond);
589 hash_index = bond_info->rx_hashtbl_head;
590 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
591 client_info = &(bond_info->rx_hashtbl[hash_index]);
593 if (!client_info->slave) {
594 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
598 /*update all clients using this src_ip, that are not assigned
599 * to the team's address (curr_active_slave) and have a known
600 * unicast mac address.
602 if ((client_info->ip_src == src_ip) &&
603 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
604 bond->dev->dev_addr) &&
605 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
606 client_info->ntt = 1;
607 bond_info->rx_ntt = 1;
611 _unlock_rx_hashtbl(bond);
614 /* Caller must hold both bond and ptr locks for read */
615 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
617 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
618 struct arp_pkt *arp = arp_pkt(skb);
619 struct slave *assigned_slave;
620 struct rlb_client_info *client_info;
623 _lock_rx_hashtbl(bond);
625 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
626 client_info = &(bond_info->rx_hashtbl[hash_index]);
628 if (client_info->assigned) {
629 if ((client_info->ip_src == arp->ip_src) &&
630 (client_info->ip_dst == arp->ip_dst)) {
631 /* the entry is already assigned to this client */
632 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
633 /* update mac address from arp */
634 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
637 assigned_slave = client_info->slave;
638 if (assigned_slave) {
639 _unlock_rx_hashtbl(bond);
640 return assigned_slave;
643 /* the entry is already assigned to some other client,
644 * move the old client to primary (curr_active_slave) so
645 * that the new client can be assigned to this entry.
647 if (bond->curr_active_slave &&
648 client_info->slave != bond->curr_active_slave) {
649 client_info->slave = bond->curr_active_slave;
650 rlb_update_client(client_info);
654 /* assign a new slave */
655 assigned_slave = rlb_next_rx_slave(bond);
657 if (assigned_slave) {
658 client_info->ip_src = arp->ip_src;
659 client_info->ip_dst = arp->ip_dst;
660 /* arp->mac_dst is broadcast for arp reqeusts.
661 * will be updated with clients actual unicast mac address
662 * upon receiving an arp reply.
664 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
665 client_info->slave = assigned_slave;
667 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
668 client_info->ntt = 1;
669 bond->alb_info.rx_ntt = 1;
671 client_info->ntt = 0;
674 if (bond_vlan_used(bond)) {
675 if (!vlan_get_tag(skb, &client_info->vlan_id))
676 client_info->tag = 1;
679 if (!client_info->assigned) {
680 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
681 bond_info->rx_hashtbl_head = hash_index;
682 client_info->next = prev_tbl_head;
683 if (prev_tbl_head != RLB_NULL_INDEX) {
684 bond_info->rx_hashtbl[prev_tbl_head].prev =
687 client_info->assigned = 1;
691 _unlock_rx_hashtbl(bond);
693 return assigned_slave;
696 /* chooses (and returns) transmit channel for arp reply
697 * does not choose channel for other arp types since they are
698 * sent on the curr_active_slave
700 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
702 struct arp_pkt *arp = arp_pkt(skb);
703 struct slave *tx_slave = NULL;
705 if (arp->op_code == htons(ARPOP_REPLY)) {
706 /* the arp must be sent on the selected
709 tx_slave = rlb_choose_channel(skb, bond);
711 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
713 pr_debug("Server sent ARP Reply packet\n");
714 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
715 /* Create an entry in the rx_hashtbl for this client as a
717 * When the arp reply is received the entry will be updated
718 * with the correct unicast address of the client.
720 rlb_choose_channel(skb, bond);
722 /* The ARP reply packets must be delayed so that
723 * they can cancel out the influence of the ARP request.
725 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
727 /* arp requests are broadcast and are sent on the primary
728 * the arp request will collapse all clients on the subnet to
729 * the primary slave. We must register these clients to be
730 * updated with their assigned mac.
732 rlb_req_update_subnet_clients(bond, arp->ip_src);
733 pr_debug("Server sent ARP Request packet\n");
739 /* Caller must hold bond lock for read */
740 static void rlb_rebalance(struct bonding *bond)
742 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
743 struct slave *assigned_slave;
744 struct rlb_client_info *client_info;
748 _lock_rx_hashtbl_bh(bond);
751 hash_index = bond_info->rx_hashtbl_head;
752 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
753 client_info = &(bond_info->rx_hashtbl[hash_index]);
754 assigned_slave = rlb_next_rx_slave(bond);
755 if (assigned_slave && (client_info->slave != assigned_slave)) {
756 client_info->slave = assigned_slave;
757 client_info->ntt = 1;
762 /* update the team's flag only after the whole iteration */
764 bond_info->rx_ntt = 1;
766 _unlock_rx_hashtbl_bh(bond);
769 /* Caller must hold rx_hashtbl lock */
770 static void rlb_init_table_entry(struct rlb_client_info *entry)
772 memset(entry, 0, sizeof(struct rlb_client_info));
773 entry->next = RLB_NULL_INDEX;
774 entry->prev = RLB_NULL_INDEX;
777 static int rlb_initialize(struct bonding *bond)
779 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
780 struct rlb_client_info *new_hashtbl;
781 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
784 new_hashtbl = kmalloc(size, GFP_KERNEL);
788 _lock_rx_hashtbl_bh(bond);
790 bond_info->rx_hashtbl = new_hashtbl;
792 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
794 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
795 rlb_init_table_entry(bond_info->rx_hashtbl + i);
798 _unlock_rx_hashtbl_bh(bond);
800 /* register to receive ARPs */
801 bond->recv_probe = rlb_arp_recv;
806 static void rlb_deinitialize(struct bonding *bond)
808 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
810 _lock_rx_hashtbl_bh(bond);
812 kfree(bond_info->rx_hashtbl);
813 bond_info->rx_hashtbl = NULL;
814 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
816 _unlock_rx_hashtbl_bh(bond);
819 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
821 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
824 _lock_rx_hashtbl_bh(bond);
826 curr_index = bond_info->rx_hashtbl_head;
827 while (curr_index != RLB_NULL_INDEX) {
828 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
829 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
830 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
832 if (curr->tag && (curr->vlan_id == vlan_id)) {
833 if (curr_index == bond_info->rx_hashtbl_head) {
834 bond_info->rx_hashtbl_head = next_index;
836 if (prev_index != RLB_NULL_INDEX) {
837 bond_info->rx_hashtbl[prev_index].next = next_index;
839 if (next_index != RLB_NULL_INDEX) {
840 bond_info->rx_hashtbl[next_index].prev = prev_index;
843 rlb_init_table_entry(curr);
846 curr_index = next_index;
849 _unlock_rx_hashtbl_bh(bond);
852 /*********************** tlb/rlb shared functions *********************/
854 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
856 struct bonding *bond = bond_get_bond_by_slave(slave);
857 struct learning_pkt pkt;
858 int size = sizeof(struct learning_pkt);
861 memset(&pkt, 0, size);
862 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
863 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
864 pkt.type = cpu_to_be16(ETH_P_LOOP);
866 for (i = 0; i < MAX_LP_BURST; i++) {
870 skb = dev_alloc_skb(size);
875 data = skb_put(skb, size);
876 memcpy(data, &pkt, size);
878 skb_reset_mac_header(skb);
879 skb->network_header = skb->mac_header + ETH_HLEN;
880 skb->protocol = pkt.type;
881 skb->priority = TC_PRIO_CONTROL;
882 skb->dev = slave->dev;
884 if (bond_vlan_used(bond)) {
885 struct vlan_entry *vlan;
887 vlan = bond_next_vlan(bond,
888 bond->alb_info.current_alb_vlan);
890 bond->alb_info.current_alb_vlan = vlan;
896 skb = vlan_put_tag(skb, vlan->vlan_id);
898 pr_err("%s: Error: failed to insert VLAN tag\n",
908 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
910 struct net_device *dev = slave->dev;
911 struct sockaddr s_addr;
913 if (slave->bond->params.mode == BOND_MODE_TLB) {
914 memcpy(dev->dev_addr, addr, dev->addr_len);
918 /* for rlb each slave must have a unique hw mac addresses so that */
919 /* each slave will receive packets destined to a different mac */
920 memcpy(s_addr.sa_data, addr, dev->addr_len);
921 s_addr.sa_family = dev->type;
922 if (dev_set_mac_address(dev, &s_addr)) {
923 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
924 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
925 dev->master->name, dev->name);
932 * Swap MAC addresses between two slaves.
934 * Called with RTNL held, and no other locks.
938 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
940 u8 tmp_mac_addr[ETH_ALEN];
942 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
943 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
944 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
949 * Send learning packets after MAC address swap.
951 * Called with RTNL and no other locks
953 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
954 struct slave *slave2)
956 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
957 struct slave *disabled_slave = NULL;
961 /* fasten the change in the switch */
962 if (SLAVE_IS_OK(slave1)) {
963 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
964 if (bond->alb_info.rlb_enabled) {
965 /* inform the clients that the mac address
968 rlb_req_update_slave_clients(bond, slave1);
971 disabled_slave = slave1;
974 if (SLAVE_IS_OK(slave2)) {
975 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
976 if (bond->alb_info.rlb_enabled) {
977 /* inform the clients that the mac address
980 rlb_req_update_slave_clients(bond, slave2);
983 disabled_slave = slave2;
986 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
987 /* A disabled slave was assigned an active mac addr */
988 rlb_teach_disabled_mac_on_primary(bond,
989 disabled_slave->dev->dev_addr);
994 * alb_change_hw_addr_on_detach
995 * @bond: bonding we're working on
996 * @slave: the slave that was just detached
998 * We assume that @slave was already detached from the slave list.
1000 * If @slave's permanent hw address is different both from its current
1001 * address and from @bond's address, then somewhere in the bond there's
1002 * a slave that has @slave's permanet address as its current address.
1003 * We'll make sure that that slave no longer uses @slave's permanent address.
1005 * Caller must hold RTNL and no other locks
1007 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1012 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1013 slave->dev->dev_addr);
1014 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1015 bond->dev->dev_addr);
1017 if (perm_curr_diff && perm_bond_diff) {
1018 struct slave *tmp_slave;
1021 bond_for_each_slave(bond, tmp_slave, i) {
1022 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
1023 tmp_slave->dev->dev_addr)) {
1030 /* locking: needs RTNL and nothing else */
1031 alb_swap_mac_addr(bond, slave, tmp_slave);
1032 alb_fasten_mac_swap(bond, slave, tmp_slave);
1038 * alb_handle_addr_collision_on_attach
1039 * @bond: bonding we're working on
1040 * @slave: the slave that was just attached
1042 * checks uniqueness of slave's mac address and handles the case the
1043 * new slave uses the bonds mac address.
1045 * If the permanent hw address of @slave is @bond's hw address, we need to
1046 * find a different hw address to give @slave, that isn't in use by any other
1047 * slave in the bond. This address must be, of course, one of the permanent
1048 * addresses of the other slaves.
1050 * We go over the slave list, and for each slave there we compare its
1051 * permanent hw address with the current address of all the other slaves.
1052 * If no match was found, then we've found a slave with a permanent address
1053 * that isn't used by any other slave in the bond, so we can assign it to
1056 * assumption: this function is called before @slave is attached to the
1059 * caller must hold the bond lock for write since the mac addresses are compared
1060 * and may be swapped.
1062 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1064 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1065 struct slave *has_bond_addr = bond->curr_active_slave;
1066 int i, j, found = 0;
1068 if (bond->slave_cnt == 0) {
1069 /* this is the first slave */
1073 /* if slave's mac address differs from bond's mac address
1074 * check uniqueness of slave's mac address against the other
1075 * slaves in the bond.
1077 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1078 bond_for_each_slave(bond, tmp_slave1, i) {
1079 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1080 slave->dev->dev_addr)) {
1089 /* Try setting slave mac to bond address and fall-through
1090 to code handling that situation below... */
1091 alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
1094 /* The slave's address is equal to the address of the bond.
1095 * Search for a spare address in the bond for this slave.
1097 free_mac_slave = NULL;
1099 bond_for_each_slave(bond, tmp_slave1, i) {
1101 bond_for_each_slave(bond, tmp_slave2, j) {
1102 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1103 tmp_slave2->dev->dev_addr)) {
1110 /* no slave has tmp_slave1's perm addr
1113 free_mac_slave = tmp_slave1;
1117 if (!has_bond_addr) {
1118 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1119 bond->dev->dev_addr)) {
1121 has_bond_addr = tmp_slave1;
1126 if (free_mac_slave) {
1127 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
1129 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1130 bond->dev->name, slave->dev->name,
1131 free_mac_slave->dev->name);
1133 } else if (has_bond_addr) {
1134 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",
1135 bond->dev->name, slave->dev->name);
1143 * alb_set_mac_address
1147 * In TLB mode all slaves are configured to the bond's hw address, but set
1148 * their dev_addr field to different addresses (based on their permanent hw
1151 * For each slave, this function sets the interface to the new address and then
1152 * changes its dev_addr field to its previous value.
1154 * Unwinding assumes bond's mac address has not yet changed.
1156 static int alb_set_mac_address(struct bonding *bond, void *addr)
1159 struct slave *slave, *stop_at;
1160 char tmp_addr[ETH_ALEN];
1164 if (bond->alb_info.rlb_enabled) {
1168 bond_for_each_slave(bond, slave, i) {
1169 /* save net_device's current hw address */
1170 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1172 res = dev_set_mac_address(slave->dev, addr);
1174 /* restore net_device's hw address */
1175 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1184 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1185 sa.sa_family = bond->dev->type;
1187 /* unwind from head to the slave that failed */
1189 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1190 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1191 dev_set_mac_address(slave->dev, &sa);
1192 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1198 /************************ exported alb funcions ************************/
1200 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1204 res = tlb_initialize(bond);
1210 bond->alb_info.rlb_enabled = 1;
1211 /* initialize rlb */
1212 res = rlb_initialize(bond);
1214 tlb_deinitialize(bond);
1218 bond->alb_info.rlb_enabled = 0;
1224 void bond_alb_deinitialize(struct bonding *bond)
1226 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1228 tlb_deinitialize(bond);
1230 if (bond_info->rlb_enabled) {
1231 rlb_deinitialize(bond);
1235 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1237 struct bonding *bond = netdev_priv(bond_dev);
1238 struct ethhdr *eth_data;
1239 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1240 struct slave *tx_slave = NULL;
1241 static const __be32 ip_bcast = htonl(0xffffffff);
1243 int do_tx_balance = 1;
1245 const u8 *hash_start = NULL;
1247 struct ipv6hdr *ip6hdr;
1249 skb_reset_mac_header(skb);
1250 eth_data = eth_hdr(skb);
1252 /* make sure that the curr_active_slave do not change during tx
1254 read_lock(&bond->curr_slave_lock);
1256 switch (ntohs(skb->protocol)) {
1258 const struct iphdr *iph = ip_hdr(skb);
1260 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
1261 (iph->daddr == ip_bcast) ||
1262 (iph->protocol == IPPROTO_IGMP)) {
1266 hash_start = (char *)&(iph->daddr);
1267 hash_size = sizeof(iph->daddr);
1271 /* IPv6 doesn't really use broadcast mac address, but leave
1272 * that here just in case.
1274 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
1279 /* IPv6 uses all-nodes multicast as an equivalent to
1280 * broadcasts in IPv4.
1282 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1287 /* Additianally, DAD probes should not be tx-balanced as that
1288 * will lead to false positives for duplicate addresses and
1289 * prevent address configuration from working.
1291 ip6hdr = ipv6_hdr(skb);
1292 if (ipv6_addr_any(&ip6hdr->saddr)) {
1297 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1298 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1301 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1302 /* something is wrong with this packet */
1307 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1308 /* The only protocol worth balancing in
1309 * this family since it has an "ARP" like
1316 hash_start = (char*)eth_data->h_dest;
1317 hash_size = ETH_ALEN;
1321 if (bond_info->rlb_enabled) {
1322 tx_slave = rlb_arp_xmit(skb, bond);
1330 if (do_tx_balance) {
1331 hash_index = _simple_hash(hash_start, hash_size);
1332 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1336 /* unbalanced or unassigned, send through primary */
1337 tx_slave = bond->curr_active_slave;
1338 bond_info->unbalanced_load += skb->len;
1341 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1342 if (tx_slave != bond->curr_active_slave) {
1343 memcpy(eth_data->h_source,
1344 tx_slave->dev->dev_addr,
1348 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1351 _lock_tx_hashtbl(bond);
1352 __tlb_clear_slave(bond, tx_slave, 0);
1353 _unlock_tx_hashtbl(bond);
1358 /* no suitable interface, frame not sent */
1361 read_unlock(&bond->curr_slave_lock);
1363 return NETDEV_TX_OK;
1366 void bond_alb_monitor(struct work_struct *work)
1368 struct bonding *bond = container_of(work, struct bonding,
1370 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1371 struct slave *slave;
1374 read_lock(&bond->lock);
1376 if (bond->slave_cnt == 0) {
1377 bond_info->tx_rebalance_counter = 0;
1378 bond_info->lp_counter = 0;
1382 bond_info->tx_rebalance_counter++;
1383 bond_info->lp_counter++;
1385 /* send learning packets */
1386 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1387 /* change of curr_active_slave involves swapping of mac addresses.
1388 * in order to avoid this swapping from happening while
1389 * sending the learning packets, the curr_slave_lock must be held for
1392 read_lock(&bond->curr_slave_lock);
1394 bond_for_each_slave(bond, slave, i) {
1395 alb_send_learning_packets(slave, slave->dev->dev_addr);
1398 read_unlock(&bond->curr_slave_lock);
1400 bond_info->lp_counter = 0;
1403 /* rebalance tx traffic */
1404 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1406 read_lock(&bond->curr_slave_lock);
1408 bond_for_each_slave(bond, slave, i) {
1409 tlb_clear_slave(bond, slave, 1);
1410 if (slave == bond->curr_active_slave) {
1411 SLAVE_TLB_INFO(slave).load =
1412 bond_info->unbalanced_load /
1413 BOND_TLB_REBALANCE_INTERVAL;
1414 bond_info->unbalanced_load = 0;
1418 read_unlock(&bond->curr_slave_lock);
1420 bond_info->tx_rebalance_counter = 0;
1423 /* handle rlb stuff */
1424 if (bond_info->rlb_enabled) {
1425 if (bond_info->primary_is_promisc &&
1426 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1429 * dev_set_promiscuity requires rtnl and
1430 * nothing else. Avoid race with bond_close.
1432 read_unlock(&bond->lock);
1433 if (!rtnl_trylock()) {
1434 read_lock(&bond->lock);
1438 bond_info->rlb_promisc_timeout_counter = 0;
1440 /* If the primary was set to promiscuous mode
1441 * because a slave was disabled then
1442 * it can now leave promiscuous mode.
1444 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1445 bond_info->primary_is_promisc = 0;
1448 read_lock(&bond->lock);
1451 if (bond_info->rlb_rebalance) {
1452 bond_info->rlb_rebalance = 0;
1453 rlb_rebalance(bond);
1456 /* check if clients need updating */
1457 if (bond_info->rx_ntt) {
1458 if (bond_info->rlb_update_delay_counter) {
1459 --bond_info->rlb_update_delay_counter;
1461 rlb_update_rx_clients(bond);
1462 if (bond_info->rlb_update_retry_counter) {
1463 --bond_info->rlb_update_retry_counter;
1465 bond_info->rx_ntt = 0;
1472 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1474 read_unlock(&bond->lock);
1477 /* assumption: called before the slave is attached to the bond
1478 * and not locked by the bond lock
1480 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1484 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
1489 /* caller must hold the bond lock for write since the mac addresses
1490 * are compared and may be swapped.
1492 read_lock(&bond->lock);
1494 res = alb_handle_addr_collision_on_attach(bond, slave);
1496 read_unlock(&bond->lock);
1502 tlb_init_slave(slave);
1504 /* order a rebalance ASAP */
1505 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1507 if (bond->alb_info.rlb_enabled) {
1508 bond->alb_info.rlb_rebalance = 1;
1515 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1518 * Caller must hold RTNL and no other locks
1520 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1522 if (bond->slave_cnt > 1) {
1523 alb_change_hw_addr_on_detach(bond, slave);
1526 tlb_clear_slave(bond, slave, 0);
1528 if (bond->alb_info.rlb_enabled) {
1529 bond->alb_info.next_rx_slave = NULL;
1530 rlb_clear_slave(bond, slave);
1534 /* Caller must hold bond lock for read */
1535 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1537 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1539 if (link == BOND_LINK_DOWN) {
1540 tlb_clear_slave(bond, slave, 0);
1541 if (bond->alb_info.rlb_enabled) {
1542 rlb_clear_slave(bond, slave);
1544 } else if (link == BOND_LINK_UP) {
1545 /* order a rebalance ASAP */
1546 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1547 if (bond->alb_info.rlb_enabled) {
1548 bond->alb_info.rlb_rebalance = 1;
1549 /* If the updelay module parameter is smaller than the
1550 * forwarding delay of the switch the rebalance will
1551 * not work because the rebalance arp replies will
1552 * not be forwarded to the clients..
1559 * bond_alb_handle_active_change - assign new curr_active_slave
1560 * @bond: our bonding struct
1561 * @new_slave: new slave to assign
1563 * Set the bond->curr_active_slave to @new_slave and handle
1564 * mac address swapping and promiscuity changes as needed.
1566 * If new_slave is NULL, caller must hold curr_slave_lock or
1567 * bond->lock for write.
1569 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1570 * read and curr_slave_lock for write. Processing here may sleep, so
1571 * no other locks may be held.
1573 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1574 __releases(&bond->curr_slave_lock)
1575 __releases(&bond->lock)
1576 __acquires(&bond->lock)
1577 __acquires(&bond->curr_slave_lock)
1579 struct slave *swap_slave;
1582 if (bond->curr_active_slave == new_slave) {
1586 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1587 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1588 bond->alb_info.primary_is_promisc = 0;
1589 bond->alb_info.rlb_promisc_timeout_counter = 0;
1592 swap_slave = bond->curr_active_slave;
1593 bond->curr_active_slave = new_slave;
1595 if (!new_slave || (bond->slave_cnt == 0)) {
1599 /* set the new curr_active_slave to the bonds mac address
1600 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1603 struct slave *tmp_slave;
1604 /* find slave that is holding the bond's mac address */
1605 bond_for_each_slave(bond, tmp_slave, i) {
1606 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1607 bond->dev->dev_addr)) {
1608 swap_slave = tmp_slave;
1615 * Arrange for swap_slave and new_slave to temporarily be
1616 * ignored so we can mess with their MAC addresses without
1617 * fear of interference from transmit activity.
1620 tlb_clear_slave(bond, swap_slave, 1);
1622 tlb_clear_slave(bond, new_slave, 1);
1624 write_unlock_bh(&bond->curr_slave_lock);
1625 read_unlock(&bond->lock);
1629 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1631 /* swap mac address */
1632 alb_swap_mac_addr(bond, swap_slave, new_slave);
1634 /* set the new_slave to the bond mac address */
1635 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
1639 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1640 read_lock(&bond->lock);
1642 read_lock(&bond->lock);
1643 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1646 write_lock_bh(&bond->curr_slave_lock);
1652 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1653 __acquires(&bond->lock)
1654 __releases(&bond->lock)
1656 struct bonding *bond = netdev_priv(bond_dev);
1657 struct sockaddr *sa = addr;
1658 struct slave *slave, *swap_slave;
1662 if (!is_valid_ether_addr(sa->sa_data)) {
1663 return -EADDRNOTAVAIL;
1666 res = alb_set_mac_address(bond, addr);
1671 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1673 /* If there is no curr_active_slave there is nothing else to do.
1674 * Otherwise we'll need to pass the new address to it and handle
1677 if (!bond->curr_active_slave) {
1683 bond_for_each_slave(bond, slave, i) {
1684 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1685 bond_dev->dev_addr)) {
1692 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1693 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1695 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr);
1697 read_lock(&bond->lock);
1698 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1699 if (bond->alb_info.rlb_enabled) {
1700 /* inform clients mac address has changed */
1701 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1703 read_unlock(&bond->lock);
1709 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1711 if (bond->alb_info.current_alb_vlan &&
1712 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1713 bond->alb_info.current_alb_vlan = NULL;
1716 if (bond->alb_info.rlb_enabled) {
1717 rlb_clear_vlan(bond, vlan_id);