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 static const u8 mac_bcast[ETH_ALEN + 2] __long_aligned = {
49 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
51 static const u8 mac_v6_allmcast[ETH_ALEN + 2] __long_aligned = {
52 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
54 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
61 u8 padding[ETH_ZLEN - ETH_HLEN];
66 __be16 prot_addr_space;
70 u8 mac_src[ETH_ALEN]; /* sender hardware address */
71 __be32 ip_src; /* sender IP address */
72 u8 mac_dst[ETH_ALEN]; /* target hardware address */
73 __be32 ip_dst; /* target IP address */
77 static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
79 return (struct arp_pkt *)skb_network_header(skb);
82 /* Forward declaration */
83 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
85 static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
90 for (i = 0; i < hash_size; i++) {
91 hash ^= hash_start[i];
97 /*********************** tlb specific functions ***************************/
99 static inline void _lock_tx_hashtbl(struct bonding *bond)
101 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
104 static inline void _unlock_tx_hashtbl(struct bonding *bond)
106 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
109 /* Caller must hold tx_hashtbl lock */
110 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
113 entry->load_history = 1 + entry->tx_bytes /
114 BOND_TLB_REBALANCE_INTERVAL;
118 entry->tx_slave = NULL;
119 entry->next = TLB_NULL_INDEX;
120 entry->prev = TLB_NULL_INDEX;
123 static inline void tlb_init_slave(struct slave *slave)
125 SLAVE_TLB_INFO(slave).load = 0;
126 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
129 /* Caller must hold bond lock for read */
130 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
132 struct tlb_client_info *tx_hash_table;
135 _lock_tx_hashtbl(bond);
137 /* clear slave from tx_hashtbl */
138 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
140 /* skip this if we've already freed the tx hash table */
142 index = SLAVE_TLB_INFO(slave).head;
143 while (index != TLB_NULL_INDEX) {
144 u32 next_index = tx_hash_table[index].next;
145 tlb_init_table_entry(&tx_hash_table[index], save_load);
150 tlb_init_slave(slave);
152 _unlock_tx_hashtbl(bond);
155 /* Must be called before starting the monitor timer */
156 static int tlb_initialize(struct bonding *bond)
158 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
159 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
160 struct tlb_client_info *new_hashtbl;
163 new_hashtbl = kzalloc(size, GFP_KERNEL);
165 pr_err("%s: Error: Failed to allocate TLB hash table\n",
169 _lock_tx_hashtbl(bond);
171 bond_info->tx_hashtbl = new_hashtbl;
173 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
174 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
177 _unlock_tx_hashtbl(bond);
182 /* Must be called only after all slaves have been released */
183 static void tlb_deinitialize(struct bonding *bond)
185 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
187 _lock_tx_hashtbl(bond);
189 kfree(bond_info->tx_hashtbl);
190 bond_info->tx_hashtbl = NULL;
192 _unlock_tx_hashtbl(bond);
195 static long long compute_gap(struct slave *slave)
197 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
198 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
201 /* Caller must hold bond lock for read */
202 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
204 struct slave *slave, *least_loaded;
211 /* Find the slave with the largest gap */
212 bond_for_each_slave(bond, slave, i) {
213 if (SLAVE_IS_OK(slave)) {
214 long long gap = compute_gap(slave);
217 least_loaded = slave;
226 /* Caller must hold bond lock for read */
227 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
229 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
230 struct tlb_client_info *hash_table;
231 struct slave *assigned_slave;
233 _lock_tx_hashtbl(bond);
235 hash_table = bond_info->tx_hashtbl;
236 assigned_slave = hash_table[hash_index].tx_slave;
237 if (!assigned_slave) {
238 assigned_slave = tlb_get_least_loaded_slave(bond);
240 if (assigned_slave) {
241 struct tlb_slave_info *slave_info =
242 &(SLAVE_TLB_INFO(assigned_slave));
243 u32 next_index = slave_info->head;
245 hash_table[hash_index].tx_slave = assigned_slave;
246 hash_table[hash_index].next = next_index;
247 hash_table[hash_index].prev = TLB_NULL_INDEX;
249 if (next_index != TLB_NULL_INDEX) {
250 hash_table[next_index].prev = hash_index;
253 slave_info->head = hash_index;
255 hash_table[hash_index].load_history;
259 if (assigned_slave) {
260 hash_table[hash_index].tx_bytes += skb_len;
263 _unlock_tx_hashtbl(bond);
265 return assigned_slave;
268 /*********************** rlb specific functions ***************************/
269 static inline void _lock_rx_hashtbl(struct bonding *bond)
271 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
274 static inline void _unlock_rx_hashtbl(struct bonding *bond)
276 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
279 /* when an ARP REPLY is received from a client update its info
282 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
284 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
285 struct rlb_client_info *client_info;
288 _lock_rx_hashtbl(bond);
290 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
291 client_info = &(bond_info->rx_hashtbl[hash_index]);
293 if ((client_info->assigned) &&
294 (client_info->ip_src == arp->ip_dst) &&
295 (client_info->ip_dst == arp->ip_src) &&
296 (compare_ether_addr_64bits(client_info->mac_dst, arp->mac_src))) {
297 /* update the clients MAC address */
298 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
299 client_info->ntt = 1;
300 bond_info->rx_ntt = 1;
303 _unlock_rx_hashtbl(bond);
306 static void rlb_arp_recv(struct sk_buff *skb, struct bonding *bond,
311 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
314 arp = (struct arp_pkt *) skb->data;
316 pr_debug("Packet has no ARP data\n");
320 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
323 if (skb->len < sizeof(struct arp_pkt)) {
324 pr_debug("Packet is too small to be an ARP\n");
328 if (arp->op_code == htons(ARPOP_REPLY)) {
329 /* update rx hash table for this ARP */
330 rlb_update_entry_from_arp(bond, arp);
331 pr_debug("Server received an ARP Reply from client\n");
335 /* Caller must hold bond lock for read */
336 static struct slave *rlb_next_rx_slave(struct bonding *bond)
338 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
339 struct slave *rx_slave, *slave, *start_at;
342 if (bond_info->next_rx_slave) {
343 start_at = bond_info->next_rx_slave;
345 start_at = bond->first_slave;
350 bond_for_each_slave_from(bond, slave, i, start_at) {
351 if (SLAVE_IS_OK(slave)) {
354 } else if (slave->speed > rx_slave->speed) {
361 bond_info->next_rx_slave = rx_slave->next;
367 /* teach the switch the mac of a disabled slave
368 * on the primary for fault tolerance
370 * Caller must hold bond->curr_slave_lock for write or bond lock for write
372 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
374 if (!bond->curr_active_slave) {
378 if (!bond->alb_info.primary_is_promisc) {
379 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
380 bond->alb_info.primary_is_promisc = 1;
382 bond->alb_info.primary_is_promisc = 0;
385 bond->alb_info.rlb_promisc_timeout_counter = 0;
387 alb_send_learning_packets(bond->curr_active_slave, addr);
390 /* slave being removed should not be active at this point
392 * Caller must hold bond lock for read
394 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
396 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
397 struct rlb_client_info *rx_hash_table;
398 u32 index, next_index;
400 /* clear slave from rx_hashtbl */
401 _lock_rx_hashtbl(bond);
403 rx_hash_table = bond_info->rx_hashtbl;
404 index = bond_info->rx_hashtbl_head;
405 for (; index != RLB_NULL_INDEX; index = next_index) {
406 next_index = rx_hash_table[index].next;
407 if (rx_hash_table[index].slave == slave) {
408 struct slave *assigned_slave = rlb_next_rx_slave(bond);
410 if (assigned_slave) {
411 rx_hash_table[index].slave = assigned_slave;
412 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
414 bond_info->rx_hashtbl[index].ntt = 1;
415 bond_info->rx_ntt = 1;
416 /* A slave has been removed from the
417 * table because it is either disabled
418 * or being released. We must retry the
419 * update to avoid clients from not
420 * being updated & disconnecting when
423 bond_info->rlb_update_retry_counter =
426 } else { /* there is no active slave */
427 rx_hash_table[index].slave = NULL;
432 _unlock_rx_hashtbl(bond);
434 write_lock_bh(&bond->curr_slave_lock);
436 if (slave != bond->curr_active_slave) {
437 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
440 write_unlock_bh(&bond->curr_slave_lock);
443 static void rlb_update_client(struct rlb_client_info *client_info)
447 if (!client_info->slave) {
451 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
454 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
456 client_info->slave->dev,
458 client_info->mac_dst,
459 client_info->slave->dev->dev_addr,
460 client_info->mac_dst);
462 pr_err("%s: Error: failed to create an ARP packet\n",
463 client_info->slave->dev->master->name);
467 skb->dev = client_info->slave->dev;
469 if (client_info->tag) {
470 skb = vlan_put_tag(skb, client_info->vlan_id);
472 pr_err("%s: Error: failed to insert VLAN tag\n",
473 client_info->slave->dev->master->name);
482 /* sends ARP REPLIES that update the clients that need updating */
483 static void rlb_update_rx_clients(struct bonding *bond)
485 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
486 struct rlb_client_info *client_info;
489 _lock_rx_hashtbl(bond);
491 hash_index = bond_info->rx_hashtbl_head;
492 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
493 client_info = &(bond_info->rx_hashtbl[hash_index]);
494 if (client_info->ntt) {
495 rlb_update_client(client_info);
496 if (bond_info->rlb_update_retry_counter == 0) {
497 client_info->ntt = 0;
502 /* do not update the entries again until this counter is zero so that
503 * not to confuse the clients.
505 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
507 _unlock_rx_hashtbl(bond);
510 /* The slave was assigned a new mac address - update the clients */
511 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
513 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
514 struct rlb_client_info *client_info;
518 _lock_rx_hashtbl(bond);
520 hash_index = bond_info->rx_hashtbl_head;
521 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
522 client_info = &(bond_info->rx_hashtbl[hash_index]);
524 if ((client_info->slave == slave) &&
525 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
526 client_info->ntt = 1;
531 // update the team's flag only after the whole iteration
533 bond_info->rx_ntt = 1;
535 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
538 _unlock_rx_hashtbl(bond);
541 /* mark all clients using src_ip to be updated */
542 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
544 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
545 struct rlb_client_info *client_info;
548 _lock_rx_hashtbl(bond);
550 hash_index = bond_info->rx_hashtbl_head;
551 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
552 client_info = &(bond_info->rx_hashtbl[hash_index]);
554 if (!client_info->slave) {
555 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
559 /*update all clients using this src_ip, that are not assigned
560 * to the team's address (curr_active_slave) and have a known
561 * unicast mac address.
563 if ((client_info->ip_src == src_ip) &&
564 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
565 bond->dev->dev_addr) &&
566 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
567 client_info->ntt = 1;
568 bond_info->rx_ntt = 1;
572 _unlock_rx_hashtbl(bond);
575 /* Caller must hold both bond and ptr locks for read */
576 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
578 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
579 struct arp_pkt *arp = arp_pkt(skb);
580 struct slave *assigned_slave;
581 struct rlb_client_info *client_info;
584 _lock_rx_hashtbl(bond);
586 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
587 client_info = &(bond_info->rx_hashtbl[hash_index]);
589 if (client_info->assigned) {
590 if ((client_info->ip_src == arp->ip_src) &&
591 (client_info->ip_dst == arp->ip_dst)) {
592 /* the entry is already assigned to this client */
593 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
594 /* update mac address from arp */
595 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
598 assigned_slave = client_info->slave;
599 if (assigned_slave) {
600 _unlock_rx_hashtbl(bond);
601 return assigned_slave;
604 /* the entry is already assigned to some other client,
605 * move the old client to primary (curr_active_slave) so
606 * that the new client can be assigned to this entry.
608 if (bond->curr_active_slave &&
609 client_info->slave != bond->curr_active_slave) {
610 client_info->slave = bond->curr_active_slave;
611 rlb_update_client(client_info);
615 /* assign a new slave */
616 assigned_slave = rlb_next_rx_slave(bond);
618 if (assigned_slave) {
619 client_info->ip_src = arp->ip_src;
620 client_info->ip_dst = arp->ip_dst;
621 /* arp->mac_dst is broadcast for arp reqeusts.
622 * will be updated with clients actual unicast mac address
623 * upon receiving an arp reply.
625 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
626 client_info->slave = assigned_slave;
628 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
629 client_info->ntt = 1;
630 bond->alb_info.rx_ntt = 1;
632 client_info->ntt = 0;
635 if (bond_vlan_used(bond)) {
636 if (!vlan_get_tag(skb, &client_info->vlan_id))
637 client_info->tag = 1;
640 if (!client_info->assigned) {
641 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
642 bond_info->rx_hashtbl_head = hash_index;
643 client_info->next = prev_tbl_head;
644 if (prev_tbl_head != RLB_NULL_INDEX) {
645 bond_info->rx_hashtbl[prev_tbl_head].prev =
648 client_info->assigned = 1;
652 _unlock_rx_hashtbl(bond);
654 return assigned_slave;
657 /* chooses (and returns) transmit channel for arp reply
658 * does not choose channel for other arp types since they are
659 * sent on the curr_active_slave
661 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
663 struct arp_pkt *arp = arp_pkt(skb);
664 struct slave *tx_slave = NULL;
666 if (arp->op_code == htons(ARPOP_REPLY)) {
667 /* the arp must be sent on the selected
670 tx_slave = rlb_choose_channel(skb, bond);
672 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
674 pr_debug("Server sent ARP Reply packet\n");
675 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
676 /* Create an entry in the rx_hashtbl for this client as a
678 * When the arp reply is received the entry will be updated
679 * with the correct unicast address of the client.
681 rlb_choose_channel(skb, bond);
683 /* The ARP reply packets must be delayed so that
684 * they can cancel out the influence of the ARP request.
686 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
688 /* arp requests are broadcast and are sent on the primary
689 * the arp request will collapse all clients on the subnet to
690 * the primary slave. We must register these clients to be
691 * updated with their assigned mac.
693 rlb_req_update_subnet_clients(bond, arp->ip_src);
694 pr_debug("Server sent ARP Request packet\n");
700 /* Caller must hold bond lock for read */
701 static void rlb_rebalance(struct bonding *bond)
703 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
704 struct slave *assigned_slave;
705 struct rlb_client_info *client_info;
709 _lock_rx_hashtbl(bond);
712 hash_index = bond_info->rx_hashtbl_head;
713 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
714 client_info = &(bond_info->rx_hashtbl[hash_index]);
715 assigned_slave = rlb_next_rx_slave(bond);
716 if (assigned_slave && (client_info->slave != assigned_slave)) {
717 client_info->slave = assigned_slave;
718 client_info->ntt = 1;
723 /* update the team's flag only after the whole iteration */
725 bond_info->rx_ntt = 1;
727 _unlock_rx_hashtbl(bond);
730 /* Caller must hold rx_hashtbl lock */
731 static void rlb_init_table_entry(struct rlb_client_info *entry)
733 memset(entry, 0, sizeof(struct rlb_client_info));
734 entry->next = RLB_NULL_INDEX;
735 entry->prev = RLB_NULL_INDEX;
738 static int rlb_initialize(struct bonding *bond)
740 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
741 struct rlb_client_info *new_hashtbl;
742 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
745 new_hashtbl = kmalloc(size, GFP_KERNEL);
747 pr_err("%s: Error: Failed to allocate RLB hash table\n",
751 _lock_rx_hashtbl(bond);
753 bond_info->rx_hashtbl = new_hashtbl;
755 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
757 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
758 rlb_init_table_entry(bond_info->rx_hashtbl + i);
761 _unlock_rx_hashtbl(bond);
763 /* register to receive ARPs */
764 bond->recv_probe = rlb_arp_recv;
769 static void rlb_deinitialize(struct bonding *bond)
771 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
773 _lock_rx_hashtbl(bond);
775 kfree(bond_info->rx_hashtbl);
776 bond_info->rx_hashtbl = NULL;
777 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
779 _unlock_rx_hashtbl(bond);
782 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
784 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
787 _lock_rx_hashtbl(bond);
789 curr_index = bond_info->rx_hashtbl_head;
790 while (curr_index != RLB_NULL_INDEX) {
791 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
792 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
793 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
795 if (curr->tag && (curr->vlan_id == vlan_id)) {
796 if (curr_index == bond_info->rx_hashtbl_head) {
797 bond_info->rx_hashtbl_head = next_index;
799 if (prev_index != RLB_NULL_INDEX) {
800 bond_info->rx_hashtbl[prev_index].next = next_index;
802 if (next_index != RLB_NULL_INDEX) {
803 bond_info->rx_hashtbl[next_index].prev = prev_index;
806 rlb_init_table_entry(curr);
809 curr_index = next_index;
812 _unlock_rx_hashtbl(bond);
815 /*********************** tlb/rlb shared functions *********************/
817 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
819 struct bonding *bond = bond_get_bond_by_slave(slave);
820 struct learning_pkt pkt;
821 int size = sizeof(struct learning_pkt);
824 memset(&pkt, 0, size);
825 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
826 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
827 pkt.type = cpu_to_be16(ETH_P_LOOP);
829 for (i = 0; i < MAX_LP_BURST; i++) {
833 skb = dev_alloc_skb(size);
838 data = skb_put(skb, size);
839 memcpy(data, &pkt, size);
841 skb_reset_mac_header(skb);
842 skb->network_header = skb->mac_header + ETH_HLEN;
843 skb->protocol = pkt.type;
844 skb->priority = TC_PRIO_CONTROL;
845 skb->dev = slave->dev;
847 if (bond_vlan_used(bond)) {
848 struct vlan_entry *vlan;
850 vlan = bond_next_vlan(bond,
851 bond->alb_info.current_alb_vlan);
853 bond->alb_info.current_alb_vlan = vlan;
859 skb = vlan_put_tag(skb, vlan->vlan_id);
861 pr_err("%s: Error: failed to insert VLAN tag\n",
871 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
873 struct net_device *dev = slave->dev;
874 struct sockaddr s_addr;
876 if (slave->bond->params.mode == BOND_MODE_TLB) {
877 memcpy(dev->dev_addr, addr, dev->addr_len);
881 /* for rlb each slave must have a unique hw mac addresses so that */
882 /* each slave will receive packets destined to a different mac */
883 memcpy(s_addr.sa_data, addr, dev->addr_len);
884 s_addr.sa_family = dev->type;
885 if (dev_set_mac_address(dev, &s_addr)) {
886 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
887 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
888 dev->master->name, dev->name);
895 * Swap MAC addresses between two slaves.
897 * Called with RTNL held, and no other locks.
901 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
903 u8 tmp_mac_addr[ETH_ALEN];
905 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
906 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
907 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
912 * Send learning packets after MAC address swap.
914 * Called with RTNL and no other locks
916 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
917 struct slave *slave2)
919 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
920 struct slave *disabled_slave = NULL;
924 /* fasten the change in the switch */
925 if (SLAVE_IS_OK(slave1)) {
926 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
927 if (bond->alb_info.rlb_enabled) {
928 /* inform the clients that the mac address
931 rlb_req_update_slave_clients(bond, slave1);
934 disabled_slave = slave1;
937 if (SLAVE_IS_OK(slave2)) {
938 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
939 if (bond->alb_info.rlb_enabled) {
940 /* inform the clients that the mac address
943 rlb_req_update_slave_clients(bond, slave2);
946 disabled_slave = slave2;
949 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
950 /* A disabled slave was assigned an active mac addr */
951 rlb_teach_disabled_mac_on_primary(bond,
952 disabled_slave->dev->dev_addr);
957 * alb_change_hw_addr_on_detach
958 * @bond: bonding we're working on
959 * @slave: the slave that was just detached
961 * We assume that @slave was already detached from the slave list.
963 * If @slave's permanent hw address is different both from its current
964 * address and from @bond's address, then somewhere in the bond there's
965 * a slave that has @slave's permanet address as its current address.
966 * We'll make sure that that slave no longer uses @slave's permanent address.
968 * Caller must hold RTNL and no other locks
970 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
975 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
976 slave->dev->dev_addr);
977 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
978 bond->dev->dev_addr);
980 if (perm_curr_diff && perm_bond_diff) {
981 struct slave *tmp_slave;
984 bond_for_each_slave(bond, tmp_slave, i) {
985 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
986 tmp_slave->dev->dev_addr)) {
993 /* locking: needs RTNL and nothing else */
994 alb_swap_mac_addr(bond, slave, tmp_slave);
995 alb_fasten_mac_swap(bond, slave, tmp_slave);
1001 * alb_handle_addr_collision_on_attach
1002 * @bond: bonding we're working on
1003 * @slave: the slave that was just attached
1005 * checks uniqueness of slave's mac address and handles the case the
1006 * new slave uses the bonds mac address.
1008 * If the permanent hw address of @slave is @bond's hw address, we need to
1009 * find a different hw address to give @slave, that isn't in use by any other
1010 * slave in the bond. This address must be, of course, one of the permanent
1011 * addresses of the other slaves.
1013 * We go over the slave list, and for each slave there we compare its
1014 * permanent hw address with the current address of all the other slaves.
1015 * If no match was found, then we've found a slave with a permanent address
1016 * that isn't used by any other slave in the bond, so we can assign it to
1019 * assumption: this function is called before @slave is attached to the
1022 * caller must hold the bond lock for write since the mac addresses are compared
1023 * and may be swapped.
1025 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1027 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1028 struct slave *has_bond_addr = bond->curr_active_slave;
1029 int i, j, found = 0;
1031 if (bond->slave_cnt == 0) {
1032 /* this is the first slave */
1036 /* if slave's mac address differs from bond's mac address
1037 * check uniqueness of slave's mac address against the other
1038 * slaves in the bond.
1040 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1041 bond_for_each_slave(bond, tmp_slave1, i) {
1042 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1043 slave->dev->dev_addr)) {
1052 /* Try setting slave mac to bond address and fall-through
1053 to code handling that situation below... */
1054 alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
1057 /* The slave's address is equal to the address of the bond.
1058 * Search for a spare address in the bond for this slave.
1060 free_mac_slave = NULL;
1062 bond_for_each_slave(bond, tmp_slave1, i) {
1064 bond_for_each_slave(bond, tmp_slave2, j) {
1065 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1066 tmp_slave2->dev->dev_addr)) {
1073 /* no slave has tmp_slave1's perm addr
1076 free_mac_slave = tmp_slave1;
1080 if (!has_bond_addr) {
1081 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1082 bond->dev->dev_addr)) {
1084 has_bond_addr = tmp_slave1;
1089 if (free_mac_slave) {
1090 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
1092 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1093 bond->dev->name, slave->dev->name,
1094 free_mac_slave->dev->name);
1096 } else if (has_bond_addr) {
1097 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",
1098 bond->dev->name, slave->dev->name);
1106 * alb_set_mac_address
1110 * In TLB mode all slaves are configured to the bond's hw address, but set
1111 * their dev_addr field to different addresses (based on their permanent hw
1114 * For each slave, this function sets the interface to the new address and then
1115 * changes its dev_addr field to its previous value.
1117 * Unwinding assumes bond's mac address has not yet changed.
1119 static int alb_set_mac_address(struct bonding *bond, void *addr)
1122 struct slave *slave, *stop_at;
1123 char tmp_addr[ETH_ALEN];
1127 if (bond->alb_info.rlb_enabled) {
1131 bond_for_each_slave(bond, slave, i) {
1132 /* save net_device's current hw address */
1133 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1135 res = dev_set_mac_address(slave->dev, addr);
1137 /* restore net_device's hw address */
1138 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1147 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1148 sa.sa_family = bond->dev->type;
1150 /* unwind from head to the slave that failed */
1152 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1153 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1154 dev_set_mac_address(slave->dev, &sa);
1155 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1161 /************************ exported alb funcions ************************/
1163 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1167 res = tlb_initialize(bond);
1173 bond->alb_info.rlb_enabled = 1;
1174 /* initialize rlb */
1175 res = rlb_initialize(bond);
1177 tlb_deinitialize(bond);
1181 bond->alb_info.rlb_enabled = 0;
1187 void bond_alb_deinitialize(struct bonding *bond)
1189 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1191 tlb_deinitialize(bond);
1193 if (bond_info->rlb_enabled) {
1194 rlb_deinitialize(bond);
1198 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1200 struct bonding *bond = netdev_priv(bond_dev);
1201 struct ethhdr *eth_data;
1202 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1203 struct slave *tx_slave = NULL;
1204 static const __be32 ip_bcast = htonl(0xffffffff);
1206 int do_tx_balance = 1;
1208 const u8 *hash_start = NULL;
1210 struct ipv6hdr *ip6hdr;
1212 skb_reset_mac_header(skb);
1213 eth_data = eth_hdr(skb);
1215 /* make sure that the curr_active_slave do not change during tx
1217 read_lock(&bond->curr_slave_lock);
1219 switch (ntohs(skb->protocol)) {
1221 const struct iphdr *iph = ip_hdr(skb);
1223 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
1224 (iph->daddr == ip_bcast) ||
1225 (iph->protocol == IPPROTO_IGMP)) {
1229 hash_start = (char *)&(iph->daddr);
1230 hash_size = sizeof(iph->daddr);
1234 /* IPv6 doesn't really use broadcast mac address, but leave
1235 * that here just in case.
1237 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
1242 /* IPv6 uses all-nodes multicast as an equivalent to
1243 * broadcasts in IPv4.
1245 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1250 /* Additianally, DAD probes should not be tx-balanced as that
1251 * will lead to false positives for duplicate addresses and
1252 * prevent address configuration from working.
1254 ip6hdr = ipv6_hdr(skb);
1255 if (ipv6_addr_any(&ip6hdr->saddr)) {
1260 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1261 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1264 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1265 /* something is wrong with this packet */
1270 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1271 /* The only protocol worth balancing in
1272 * this family since it has an "ARP" like
1279 hash_start = (char*)eth_data->h_dest;
1280 hash_size = ETH_ALEN;
1284 if (bond_info->rlb_enabled) {
1285 tx_slave = rlb_arp_xmit(skb, bond);
1293 if (do_tx_balance) {
1294 hash_index = _simple_hash(hash_start, hash_size);
1295 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1299 /* unbalanced or unassigned, send through primary */
1300 tx_slave = bond->curr_active_slave;
1301 bond_info->unbalanced_load += skb->len;
1304 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1305 if (tx_slave != bond->curr_active_slave) {
1306 memcpy(eth_data->h_source,
1307 tx_slave->dev->dev_addr,
1311 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1314 tlb_clear_slave(bond, tx_slave, 0);
1319 /* no suitable interface, frame not sent */
1322 read_unlock(&bond->curr_slave_lock);
1324 return NETDEV_TX_OK;
1327 void bond_alb_monitor(struct work_struct *work)
1329 struct bonding *bond = container_of(work, struct bonding,
1331 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1332 struct slave *slave;
1335 read_lock(&bond->lock);
1337 if (bond->slave_cnt == 0) {
1338 bond_info->tx_rebalance_counter = 0;
1339 bond_info->lp_counter = 0;
1343 bond_info->tx_rebalance_counter++;
1344 bond_info->lp_counter++;
1346 /* send learning packets */
1347 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1348 /* change of curr_active_slave involves swapping of mac addresses.
1349 * in order to avoid this swapping from happening while
1350 * sending the learning packets, the curr_slave_lock must be held for
1353 read_lock(&bond->curr_slave_lock);
1355 bond_for_each_slave(bond, slave, i) {
1356 alb_send_learning_packets(slave, slave->dev->dev_addr);
1359 read_unlock(&bond->curr_slave_lock);
1361 bond_info->lp_counter = 0;
1364 /* rebalance tx traffic */
1365 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1367 read_lock(&bond->curr_slave_lock);
1369 bond_for_each_slave(bond, slave, i) {
1370 tlb_clear_slave(bond, slave, 1);
1371 if (slave == bond->curr_active_slave) {
1372 SLAVE_TLB_INFO(slave).load =
1373 bond_info->unbalanced_load /
1374 BOND_TLB_REBALANCE_INTERVAL;
1375 bond_info->unbalanced_load = 0;
1379 read_unlock(&bond->curr_slave_lock);
1381 bond_info->tx_rebalance_counter = 0;
1384 /* handle rlb stuff */
1385 if (bond_info->rlb_enabled) {
1386 if (bond_info->primary_is_promisc &&
1387 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1390 * dev_set_promiscuity requires rtnl and
1391 * nothing else. Avoid race with bond_close.
1393 read_unlock(&bond->lock);
1394 if (!rtnl_trylock()) {
1395 read_lock(&bond->lock);
1399 bond_info->rlb_promisc_timeout_counter = 0;
1401 /* If the primary was set to promiscuous mode
1402 * because a slave was disabled then
1403 * it can now leave promiscuous mode.
1405 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1406 bond_info->primary_is_promisc = 0;
1409 read_lock(&bond->lock);
1412 if (bond_info->rlb_rebalance) {
1413 bond_info->rlb_rebalance = 0;
1414 rlb_rebalance(bond);
1417 /* check if clients need updating */
1418 if (bond_info->rx_ntt) {
1419 if (bond_info->rlb_update_delay_counter) {
1420 --bond_info->rlb_update_delay_counter;
1422 rlb_update_rx_clients(bond);
1423 if (bond_info->rlb_update_retry_counter) {
1424 --bond_info->rlb_update_retry_counter;
1426 bond_info->rx_ntt = 0;
1433 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1435 read_unlock(&bond->lock);
1438 /* assumption: called before the slave is attached to the bond
1439 * and not locked by the bond lock
1441 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1445 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
1450 /* caller must hold the bond lock for write since the mac addresses
1451 * are compared and may be swapped.
1453 read_lock(&bond->lock);
1455 res = alb_handle_addr_collision_on_attach(bond, slave);
1457 read_unlock(&bond->lock);
1463 tlb_init_slave(slave);
1465 /* order a rebalance ASAP */
1466 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1468 if (bond->alb_info.rlb_enabled) {
1469 bond->alb_info.rlb_rebalance = 1;
1476 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1479 * Caller must hold RTNL and no other locks
1481 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1483 if (bond->slave_cnt > 1) {
1484 alb_change_hw_addr_on_detach(bond, slave);
1487 tlb_clear_slave(bond, slave, 0);
1489 if (bond->alb_info.rlb_enabled) {
1490 bond->alb_info.next_rx_slave = NULL;
1491 rlb_clear_slave(bond, slave);
1495 /* Caller must hold bond lock for read */
1496 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1498 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1500 if (link == BOND_LINK_DOWN) {
1501 tlb_clear_slave(bond, slave, 0);
1502 if (bond->alb_info.rlb_enabled) {
1503 rlb_clear_slave(bond, slave);
1505 } else if (link == BOND_LINK_UP) {
1506 /* order a rebalance ASAP */
1507 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1508 if (bond->alb_info.rlb_enabled) {
1509 bond->alb_info.rlb_rebalance = 1;
1510 /* If the updelay module parameter is smaller than the
1511 * forwarding delay of the switch the rebalance will
1512 * not work because the rebalance arp replies will
1513 * not be forwarded to the clients..
1520 * bond_alb_handle_active_change - assign new curr_active_slave
1521 * @bond: our bonding struct
1522 * @new_slave: new slave to assign
1524 * Set the bond->curr_active_slave to @new_slave and handle
1525 * mac address swapping and promiscuity changes as needed.
1527 * If new_slave is NULL, caller must hold curr_slave_lock or
1528 * bond->lock for write.
1530 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1531 * read and curr_slave_lock for write. Processing here may sleep, so
1532 * no other locks may be held.
1534 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1535 __releases(&bond->curr_slave_lock)
1536 __releases(&bond->lock)
1537 __acquires(&bond->lock)
1538 __acquires(&bond->curr_slave_lock)
1540 struct slave *swap_slave;
1543 if (bond->curr_active_slave == new_slave) {
1547 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1548 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1549 bond->alb_info.primary_is_promisc = 0;
1550 bond->alb_info.rlb_promisc_timeout_counter = 0;
1553 swap_slave = bond->curr_active_slave;
1554 bond->curr_active_slave = new_slave;
1556 if (!new_slave || (bond->slave_cnt == 0)) {
1560 /* set the new curr_active_slave to the bonds mac address
1561 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1564 struct slave *tmp_slave;
1565 /* find slave that is holding the bond's mac address */
1566 bond_for_each_slave(bond, tmp_slave, i) {
1567 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1568 bond->dev->dev_addr)) {
1569 swap_slave = tmp_slave;
1576 * Arrange for swap_slave and new_slave to temporarily be
1577 * ignored so we can mess with their MAC addresses without
1578 * fear of interference from transmit activity.
1581 tlb_clear_slave(bond, swap_slave, 1);
1583 tlb_clear_slave(bond, new_slave, 1);
1585 write_unlock_bh(&bond->curr_slave_lock);
1586 read_unlock(&bond->lock);
1590 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1592 /* swap mac address */
1593 alb_swap_mac_addr(bond, swap_slave, new_slave);
1595 /* set the new_slave to the bond mac address */
1596 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
1600 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1601 read_lock(&bond->lock);
1603 read_lock(&bond->lock);
1604 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1607 write_lock_bh(&bond->curr_slave_lock);
1613 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1614 __acquires(&bond->lock)
1615 __releases(&bond->lock)
1617 struct bonding *bond = netdev_priv(bond_dev);
1618 struct sockaddr *sa = addr;
1619 struct slave *slave, *swap_slave;
1623 if (!is_valid_ether_addr(sa->sa_data)) {
1624 return -EADDRNOTAVAIL;
1627 res = alb_set_mac_address(bond, addr);
1632 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1634 /* If there is no curr_active_slave there is nothing else to do.
1635 * Otherwise we'll need to pass the new address to it and handle
1638 if (!bond->curr_active_slave) {
1644 bond_for_each_slave(bond, slave, i) {
1645 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1646 bond_dev->dev_addr)) {
1653 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1654 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1656 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr);
1658 read_lock(&bond->lock);
1659 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1660 if (bond->alb_info.rlb_enabled) {
1661 /* inform clients mac address has changed */
1662 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1664 read_unlock(&bond->lock);
1670 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1672 if (bond->alb_info.current_alb_vlan &&
1673 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1674 bond->alb_info.current_alb_vlan = NULL;
1677 if (bond->alb_info.rlb_enabled) {
1678 rlb_clear_vlan(bond, vlan_id);