sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / net / bonding / bond_alb.c
blob8f2d2e7c70e5d2d24ff36a864f528eb6da5f6bc5
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 #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>
32 #include <linux/ip.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>
38 #include <linux/in.h>
39 #include <net/ipx.h>
40 #include <net/arp.h>
41 #include <net/ipv6.h>
42 #include <asm/byteorder.h>
43 #include "bonding.h"
44 #include "bond_alb.h"
48 #ifndef __long_aligned
49 #define __long_aligned __attribute__((aligned((sizeof(long)))))
50 #endif
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;
59 #pragma pack(1)
60 struct learning_pkt {
61 u8 mac_dst[ETH_ALEN];
62 u8 mac_src[ETH_ALEN];
63 __be16 type;
64 u8 padding[ETH_ZLEN - ETH_HLEN];
67 struct arp_pkt {
68 __be16 hw_addr_space;
69 __be16 prot_addr_space;
70 u8 hw_addr_len;
71 u8 prot_addr_len;
72 __be16 op_code;
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 */
78 #pragma pack()
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)
90 int i;
91 u8 hash = 0;
93 for (i = 0; i < hash_size; i++) {
94 hash ^= hash_start[i];
97 return hash;
100 /*********************** tlb specific functions ***************************/
102 static inline void _lock_tx_hashtbl(struct bonding *bond)
104 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
107 static inline void _unlock_tx_hashtbl(struct bonding *bond)
109 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
112 /* Caller must hold tx_hashtbl lock */
113 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
115 if (save_load) {
116 entry->load_history = 1 + entry->tx_bytes /
117 BOND_TLB_REBALANCE_INTERVAL;
118 entry->tx_bytes = 0;
121 entry->tx_slave = NULL;
122 entry->next = TLB_NULL_INDEX;
123 entry->prev = TLB_NULL_INDEX;
126 static inline void tlb_init_slave(struct slave *slave)
128 SLAVE_TLB_INFO(slave).load = 0;
129 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
132 /* Caller must hold bond lock for read */
133 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
135 struct tlb_client_info *tx_hash_table;
136 u32 index;
138 _lock_tx_hashtbl(bond);
140 /* clear slave from tx_hashtbl */
141 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
143 /* skip this if we've already freed the tx hash table */
144 if (tx_hash_table) {
145 index = SLAVE_TLB_INFO(slave).head;
146 while (index != TLB_NULL_INDEX) {
147 u32 next_index = tx_hash_table[index].next;
148 tlb_init_table_entry(&tx_hash_table[index], save_load);
149 index = next_index;
153 tlb_init_slave(slave);
155 _unlock_tx_hashtbl(bond);
158 /* Must be called before starting the monitor timer */
159 static int tlb_initialize(struct bonding *bond)
161 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
162 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
163 struct tlb_client_info *new_hashtbl;
164 int i;
166 spin_lock_init(&(bond_info->tx_hashtbl_lock));
168 new_hashtbl = kzalloc(size, GFP_KERNEL);
169 if (!new_hashtbl) {
170 pr_err("%s: Error: Failed to allocate TLB hash table\n",
171 bond->dev->name);
172 return -1;
174 _lock_tx_hashtbl(bond);
176 bond_info->tx_hashtbl = new_hashtbl;
178 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
179 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
182 _unlock_tx_hashtbl(bond);
184 return 0;
187 /* Must be called only after all slaves have been released */
188 static void tlb_deinitialize(struct bonding *bond)
190 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
192 _lock_tx_hashtbl(bond);
194 kfree(bond_info->tx_hashtbl);
195 bond_info->tx_hashtbl = NULL;
197 _unlock_tx_hashtbl(bond);
200 static long long compute_gap(struct slave *slave)
202 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
203 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
206 /* Caller must hold bond lock for read */
207 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
209 struct slave *slave, *least_loaded;
210 long long max_gap;
211 int i;
213 least_loaded = NULL;
214 max_gap = LLONG_MIN;
216 /* Find the slave with the largest gap */
217 bond_for_each_slave(bond, slave, i) {
218 if (SLAVE_IS_OK(slave)) {
219 long long gap = compute_gap(slave);
221 if (max_gap < gap) {
222 least_loaded = slave;
223 max_gap = gap;
228 return least_loaded;
231 /* Caller must hold bond lock for read */
232 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
234 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
235 struct tlb_client_info *hash_table;
236 struct slave *assigned_slave;
238 _lock_tx_hashtbl(bond);
240 hash_table = bond_info->tx_hashtbl;
241 assigned_slave = hash_table[hash_index].tx_slave;
242 if (!assigned_slave) {
243 assigned_slave = tlb_get_least_loaded_slave(bond);
245 if (assigned_slave) {
246 struct tlb_slave_info *slave_info =
247 &(SLAVE_TLB_INFO(assigned_slave));
248 u32 next_index = slave_info->head;
250 hash_table[hash_index].tx_slave = assigned_slave;
251 hash_table[hash_index].next = next_index;
252 hash_table[hash_index].prev = TLB_NULL_INDEX;
254 if (next_index != TLB_NULL_INDEX) {
255 hash_table[next_index].prev = hash_index;
258 slave_info->head = hash_index;
259 slave_info->load +=
260 hash_table[hash_index].load_history;
264 if (assigned_slave) {
265 hash_table[hash_index].tx_bytes += skb_len;
268 _unlock_tx_hashtbl(bond);
270 return assigned_slave;
273 /*********************** rlb specific functions ***************************/
274 static inline void _lock_rx_hashtbl(struct bonding *bond)
276 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
279 static inline void _unlock_rx_hashtbl(struct bonding *bond)
281 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
284 /* when an ARP REPLY is received from a client update its info
285 * in the rx_hashtbl
287 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
289 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
290 struct rlb_client_info *client_info;
291 u32 hash_index;
293 _lock_rx_hashtbl(bond);
295 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
296 client_info = &(bond_info->rx_hashtbl[hash_index]);
298 if ((client_info->assigned) &&
299 (client_info->ip_src == arp->ip_dst) &&
300 (client_info->ip_dst == arp->ip_src) &&
301 (compare_ether_addr_64bits(client_info->mac_dst, arp->mac_src))) {
302 /* update the clients MAC address */
303 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
304 client_info->ntt = 1;
305 bond_info->rx_ntt = 1;
308 _unlock_rx_hashtbl(bond);
311 static void rlb_arp_recv(struct sk_buff *skb, struct bonding *bond,
312 struct slave *slave)
314 struct arp_pkt *arp;
316 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
317 return;
319 arp = (struct arp_pkt *) skb->data;
320 if (!arp) {
321 pr_debug("Packet has no ARP data\n");
322 return;
325 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
326 return;
328 if (skb->len < sizeof(struct arp_pkt)) {
329 pr_debug("Packet is too small to be an ARP\n");
330 return;
333 if (arp->op_code == htons(ARPOP_REPLY)) {
334 /* update rx hash table for this ARP */
335 rlb_update_entry_from_arp(bond, arp);
336 pr_debug("Server received an ARP Reply from client\n");
340 /* Caller must hold bond lock for read */
341 static struct slave *rlb_next_rx_slave(struct bonding *bond)
343 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
344 struct slave *rx_slave, *slave, *start_at;
345 int i = 0;
347 if (bond_info->next_rx_slave) {
348 start_at = bond_info->next_rx_slave;
349 } else {
350 start_at = bond->first_slave;
353 rx_slave = NULL;
355 bond_for_each_slave_from(bond, slave, i, start_at) {
356 if (SLAVE_IS_OK(slave)) {
357 if (!rx_slave) {
358 rx_slave = slave;
359 } else if (slave->speed > rx_slave->speed) {
360 rx_slave = slave;
365 if (rx_slave) {
366 bond_info->next_rx_slave = rx_slave->next;
369 return rx_slave;
372 /* teach the switch the mac of a disabled slave
373 * on the primary for fault tolerance
375 * Caller must hold bond->curr_slave_lock for write or bond lock for write
377 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
379 if (!bond->curr_active_slave) {
380 return;
383 if (!bond->alb_info.primary_is_promisc) {
384 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
385 bond->alb_info.primary_is_promisc = 1;
386 else
387 bond->alb_info.primary_is_promisc = 0;
390 bond->alb_info.rlb_promisc_timeout_counter = 0;
392 alb_send_learning_packets(bond->curr_active_slave, addr);
395 /* slave being removed should not be active at this point
397 * Caller must hold bond lock for read
399 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
401 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
402 struct rlb_client_info *rx_hash_table;
403 u32 index, next_index;
405 /* clear slave from rx_hashtbl */
406 _lock_rx_hashtbl(bond);
408 rx_hash_table = bond_info->rx_hashtbl;
409 index = bond_info->rx_hashtbl_head;
410 for (; index != RLB_NULL_INDEX; index = next_index) {
411 next_index = rx_hash_table[index].next;
412 if (rx_hash_table[index].slave == slave) {
413 struct slave *assigned_slave = rlb_next_rx_slave(bond);
415 if (assigned_slave) {
416 rx_hash_table[index].slave = assigned_slave;
417 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
418 mac_bcast)) {
419 bond_info->rx_hashtbl[index].ntt = 1;
420 bond_info->rx_ntt = 1;
421 /* A slave has been removed from the
422 * table because it is either disabled
423 * or being released. We must retry the
424 * update to avoid clients from not
425 * being updated & disconnecting when
426 * there is stress
428 bond_info->rlb_update_retry_counter =
429 RLB_UPDATE_RETRY;
431 } else { /* there is no active slave */
432 rx_hash_table[index].slave = NULL;
437 _unlock_rx_hashtbl(bond);
439 write_lock_bh(&bond->curr_slave_lock);
441 if (slave != bond->curr_active_slave) {
442 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
445 write_unlock_bh(&bond->curr_slave_lock);
448 static void rlb_update_client(struct rlb_client_info *client_info)
450 int i;
452 if (!client_info->slave) {
453 return;
456 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
457 struct sk_buff *skb;
459 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
460 client_info->ip_dst,
461 client_info->slave->dev,
462 client_info->ip_src,
463 client_info->mac_dst,
464 client_info->slave->dev->dev_addr,
465 client_info->mac_dst);
466 if (!skb) {
467 pr_err("%s: Error: failed to create an ARP packet\n",
468 client_info->slave->dev->master->name);
469 continue;
472 skb->dev = client_info->slave->dev;
474 if (client_info->tag) {
475 skb = vlan_put_tag(skb, client_info->vlan_id);
476 if (!skb) {
477 pr_err("%s: Error: failed to insert VLAN tag\n",
478 client_info->slave->dev->master->name);
479 continue;
483 arp_xmit(skb);
487 /* sends ARP REPLIES that update the clients that need updating */
488 static void rlb_update_rx_clients(struct bonding *bond)
490 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
491 struct rlb_client_info *client_info;
492 u32 hash_index;
494 _lock_rx_hashtbl(bond);
496 hash_index = bond_info->rx_hashtbl_head;
497 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
498 client_info = &(bond_info->rx_hashtbl[hash_index]);
499 if (client_info->ntt) {
500 rlb_update_client(client_info);
501 if (bond_info->rlb_update_retry_counter == 0) {
502 client_info->ntt = 0;
507 /* do not update the entries again until this counter is zero so that
508 * not to confuse the clients.
510 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
512 _unlock_rx_hashtbl(bond);
515 /* The slave was assigned a new mac address - update the clients */
516 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
518 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
519 struct rlb_client_info *client_info;
520 int ntt = 0;
521 u32 hash_index;
523 _lock_rx_hashtbl(bond);
525 hash_index = bond_info->rx_hashtbl_head;
526 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
527 client_info = &(bond_info->rx_hashtbl[hash_index]);
529 if ((client_info->slave == slave) &&
530 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
531 client_info->ntt = 1;
532 ntt = 1;
536 // update the team's flag only after the whole iteration
537 if (ntt) {
538 bond_info->rx_ntt = 1;
539 //fasten the change
540 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
543 _unlock_rx_hashtbl(bond);
546 /* mark all clients using src_ip to be updated */
547 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
549 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
550 struct rlb_client_info *client_info;
551 u32 hash_index;
553 _lock_rx_hashtbl(bond);
555 hash_index = bond_info->rx_hashtbl_head;
556 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
557 client_info = &(bond_info->rx_hashtbl[hash_index]);
559 if (!client_info->slave) {
560 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
561 bond->dev->name);
562 continue;
564 /*update all clients using this src_ip, that are not assigned
565 * to the team's address (curr_active_slave) and have a known
566 * unicast mac address.
568 if ((client_info->ip_src == src_ip) &&
569 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
570 bond->dev->dev_addr) &&
571 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
572 client_info->ntt = 1;
573 bond_info->rx_ntt = 1;
577 _unlock_rx_hashtbl(bond);
580 /* Caller must hold both bond and ptr locks for read */
581 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
583 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
584 struct arp_pkt *arp = arp_pkt(skb);
585 struct slave *assigned_slave;
586 struct rlb_client_info *client_info;
587 u32 hash_index = 0;
589 _lock_rx_hashtbl(bond);
591 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
592 client_info = &(bond_info->rx_hashtbl[hash_index]);
594 if (client_info->assigned) {
595 if ((client_info->ip_src == arp->ip_src) &&
596 (client_info->ip_dst == arp->ip_dst)) {
597 /* the entry is already assigned to this client */
598 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
599 /* update mac address from arp */
600 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
603 assigned_slave = client_info->slave;
604 if (assigned_slave) {
605 _unlock_rx_hashtbl(bond);
606 return assigned_slave;
608 } else {
609 /* the entry is already assigned to some other client,
610 * move the old client to primary (curr_active_slave) so
611 * that the new client can be assigned to this entry.
613 if (bond->curr_active_slave &&
614 client_info->slave != bond->curr_active_slave) {
615 client_info->slave = bond->curr_active_slave;
616 rlb_update_client(client_info);
620 /* assign a new slave */
621 assigned_slave = rlb_next_rx_slave(bond);
623 if (assigned_slave) {
624 client_info->ip_src = arp->ip_src;
625 client_info->ip_dst = arp->ip_dst;
626 /* arp->mac_dst is broadcast for arp reqeusts.
627 * will be updated with clients actual unicast mac address
628 * upon receiving an arp reply.
630 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
631 client_info->slave = assigned_slave;
633 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
634 client_info->ntt = 1;
635 bond->alb_info.rx_ntt = 1;
636 } else {
637 client_info->ntt = 0;
640 if (bond->vlgrp) {
641 if (!vlan_get_tag(skb, &client_info->vlan_id))
642 client_info->tag = 1;
645 if (!client_info->assigned) {
646 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
647 bond_info->rx_hashtbl_head = hash_index;
648 client_info->next = prev_tbl_head;
649 if (prev_tbl_head != RLB_NULL_INDEX) {
650 bond_info->rx_hashtbl[prev_tbl_head].prev =
651 hash_index;
653 client_info->assigned = 1;
657 _unlock_rx_hashtbl(bond);
659 return assigned_slave;
662 /* chooses (and returns) transmit channel for arp reply
663 * does not choose channel for other arp types since they are
664 * sent on the curr_active_slave
666 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
668 struct arp_pkt *arp = arp_pkt(skb);
669 struct slave *tx_slave = NULL;
671 if (arp->op_code == htons(ARPOP_REPLY)) {
672 /* the arp must be sent on the selected
673 * rx channel
675 tx_slave = rlb_choose_channel(skb, bond);
676 if (tx_slave) {
677 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
679 pr_debug("Server sent ARP Reply packet\n");
680 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
681 /* Create an entry in the rx_hashtbl for this client as a
682 * place holder.
683 * When the arp reply is received the entry will be updated
684 * with the correct unicast address of the client.
686 rlb_choose_channel(skb, bond);
688 /* The ARP reply packets must be delayed so that
689 * they can cancel out the influence of the ARP request.
691 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
693 /* arp requests are broadcast and are sent on the primary
694 * the arp request will collapse all clients on the subnet to
695 * the primary slave. We must register these clients to be
696 * updated with their assigned mac.
698 rlb_req_update_subnet_clients(bond, arp->ip_src);
699 pr_debug("Server sent ARP Request packet\n");
702 return tx_slave;
705 /* Caller must hold bond lock for read */
706 static void rlb_rebalance(struct bonding *bond)
708 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
709 struct slave *assigned_slave;
710 struct rlb_client_info *client_info;
711 int ntt;
712 u32 hash_index;
714 _lock_rx_hashtbl(bond);
716 ntt = 0;
717 hash_index = bond_info->rx_hashtbl_head;
718 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
719 client_info = &(bond_info->rx_hashtbl[hash_index]);
720 assigned_slave = rlb_next_rx_slave(bond);
721 if (assigned_slave && (client_info->slave != assigned_slave)) {
722 client_info->slave = assigned_slave;
723 client_info->ntt = 1;
724 ntt = 1;
728 /* update the team's flag only after the whole iteration */
729 if (ntt) {
730 bond_info->rx_ntt = 1;
732 _unlock_rx_hashtbl(bond);
735 /* Caller must hold rx_hashtbl lock */
736 static void rlb_init_table_entry(struct rlb_client_info *entry)
738 memset(entry, 0, sizeof(struct rlb_client_info));
739 entry->next = RLB_NULL_INDEX;
740 entry->prev = RLB_NULL_INDEX;
743 static int rlb_initialize(struct bonding *bond)
745 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
746 struct rlb_client_info *new_hashtbl;
747 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
748 int i;
750 spin_lock_init(&(bond_info->rx_hashtbl_lock));
752 new_hashtbl = kmalloc(size, GFP_KERNEL);
753 if (!new_hashtbl) {
754 pr_err("%s: Error: Failed to allocate RLB hash table\n",
755 bond->dev->name);
756 return -1;
758 _lock_rx_hashtbl(bond);
760 bond_info->rx_hashtbl = new_hashtbl;
762 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
764 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
765 rlb_init_table_entry(bond_info->rx_hashtbl + i);
768 _unlock_rx_hashtbl(bond);
770 /* register to receive ARPs */
771 bond->recv_probe = rlb_arp_recv;
773 return 0;
776 static void rlb_deinitialize(struct bonding *bond)
778 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
780 _lock_rx_hashtbl(bond);
782 kfree(bond_info->rx_hashtbl);
783 bond_info->rx_hashtbl = NULL;
784 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
786 _unlock_rx_hashtbl(bond);
789 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
791 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
792 u32 curr_index;
794 _lock_rx_hashtbl(bond);
796 curr_index = bond_info->rx_hashtbl_head;
797 while (curr_index != RLB_NULL_INDEX) {
798 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
799 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
800 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
802 if (curr->tag && (curr->vlan_id == vlan_id)) {
803 if (curr_index == bond_info->rx_hashtbl_head) {
804 bond_info->rx_hashtbl_head = next_index;
806 if (prev_index != RLB_NULL_INDEX) {
807 bond_info->rx_hashtbl[prev_index].next = next_index;
809 if (next_index != RLB_NULL_INDEX) {
810 bond_info->rx_hashtbl[next_index].prev = prev_index;
813 rlb_init_table_entry(curr);
816 curr_index = next_index;
819 _unlock_rx_hashtbl(bond);
822 /*********************** tlb/rlb shared functions *********************/
824 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
826 struct bonding *bond = bond_get_bond_by_slave(slave);
827 struct learning_pkt pkt;
828 int size = sizeof(struct learning_pkt);
829 int i;
831 memset(&pkt, 0, size);
832 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
833 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
834 pkt.type = cpu_to_be16(ETH_P_LOOP);
836 for (i = 0; i < MAX_LP_BURST; i++) {
837 struct sk_buff *skb;
838 char *data;
840 skb = dev_alloc_skb(size);
841 if (!skb) {
842 return;
845 data = skb_put(skb, size);
846 memcpy(data, &pkt, size);
848 skb_reset_mac_header(skb);
849 skb->network_header = skb->mac_header + ETH_HLEN;
850 skb->protocol = pkt.type;
851 skb->priority = TC_PRIO_CONTROL;
852 skb->dev = slave->dev;
854 if (bond->vlgrp) {
855 struct vlan_entry *vlan;
857 vlan = bond_next_vlan(bond,
858 bond->alb_info.current_alb_vlan);
860 bond->alb_info.current_alb_vlan = vlan;
861 if (!vlan) {
862 kfree_skb(skb);
863 continue;
866 skb = vlan_put_tag(skb, vlan->vlan_id);
867 if (!skb) {
868 pr_err("%s: Error: failed to insert VLAN tag\n",
869 bond->dev->name);
870 continue;
874 dev_queue_xmit(skb);
878 /* hw is a boolean parameter that determines whether we should try and
879 * set the hw address of the device as well as the hw address of the
880 * net_device
882 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
884 struct net_device *dev = slave->dev;
885 struct sockaddr s_addr;
887 if (!hw) {
888 memcpy(dev->dev_addr, addr, dev->addr_len);
889 return 0;
892 /* for rlb each slave must have a unique hw mac addresses so that */
893 /* each slave will receive packets destined to a different mac */
894 memcpy(s_addr.sa_data, addr, dev->addr_len);
895 s_addr.sa_family = dev->type;
896 if (dev_set_mac_address(dev, &s_addr)) {
897 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
898 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
899 dev->master->name, dev->name);
900 return -EOPNOTSUPP;
902 return 0;
906 * Swap MAC addresses between two slaves.
908 * Called with RTNL held, and no other locks.
912 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
914 u8 tmp_mac_addr[ETH_ALEN];
916 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
917 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
918 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
923 * Send learning packets after MAC address swap.
925 * Called with RTNL and no other locks
927 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
928 struct slave *slave2)
930 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
931 struct slave *disabled_slave = NULL;
933 ASSERT_RTNL();
935 /* fasten the change in the switch */
936 if (SLAVE_IS_OK(slave1)) {
937 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
938 if (bond->alb_info.rlb_enabled) {
939 /* inform the clients that the mac address
940 * has changed
942 rlb_req_update_slave_clients(bond, slave1);
944 } else {
945 disabled_slave = slave1;
948 if (SLAVE_IS_OK(slave2)) {
949 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
950 if (bond->alb_info.rlb_enabled) {
951 /* inform the clients that the mac address
952 * has changed
954 rlb_req_update_slave_clients(bond, slave2);
956 } else {
957 disabled_slave = slave2;
960 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
961 /* A disabled slave was assigned an active mac addr */
962 rlb_teach_disabled_mac_on_primary(bond,
963 disabled_slave->dev->dev_addr);
968 * alb_change_hw_addr_on_detach
969 * @bond: bonding we're working on
970 * @slave: the slave that was just detached
972 * We assume that @slave was already detached from the slave list.
974 * If @slave's permanent hw address is different both from its current
975 * address and from @bond's address, then somewhere in the bond there's
976 * a slave that has @slave's permanet address as its current address.
977 * We'll make sure that that slave no longer uses @slave's permanent address.
979 * Caller must hold RTNL and no other locks
981 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
983 int perm_curr_diff;
984 int perm_bond_diff;
986 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
987 slave->dev->dev_addr);
988 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
989 bond->dev->dev_addr);
991 if (perm_curr_diff && perm_bond_diff) {
992 struct slave *tmp_slave;
993 int i, found = 0;
995 bond_for_each_slave(bond, tmp_slave, i) {
996 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
997 tmp_slave->dev->dev_addr)) {
998 found = 1;
999 break;
1003 if (found) {
1004 /* locking: needs RTNL and nothing else */
1005 alb_swap_mac_addr(bond, slave, tmp_slave);
1006 alb_fasten_mac_swap(bond, slave, tmp_slave);
1012 * alb_handle_addr_collision_on_attach
1013 * @bond: bonding we're working on
1014 * @slave: the slave that was just attached
1016 * checks uniqueness of slave's mac address and handles the case the
1017 * new slave uses the bonds mac address.
1019 * If the permanent hw address of @slave is @bond's hw address, we need to
1020 * find a different hw address to give @slave, that isn't in use by any other
1021 * slave in the bond. This address must be, of course, one of the permanent
1022 * addresses of the other slaves.
1024 * We go over the slave list, and for each slave there we compare its
1025 * permanent hw address with the current address of all the other slaves.
1026 * If no match was found, then we've found a slave with a permanent address
1027 * that isn't used by any other slave in the bond, so we can assign it to
1028 * @slave.
1030 * assumption: this function is called before @slave is attached to the
1031 * bond slave list.
1033 * caller must hold the bond lock for write since the mac addresses are compared
1034 * and may be swapped.
1036 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1038 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1039 struct slave *has_bond_addr = bond->curr_active_slave;
1040 int i, j, found = 0;
1042 if (bond->slave_cnt == 0) {
1043 /* this is the first slave */
1044 return 0;
1047 /* if slave's mac address differs from bond's mac address
1048 * check uniqueness of slave's mac address against the other
1049 * slaves in the bond.
1051 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1052 bond_for_each_slave(bond, tmp_slave1, i) {
1053 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1054 slave->dev->dev_addr)) {
1055 found = 1;
1056 break;
1060 if (!found)
1061 return 0;
1063 /* Try setting slave mac to bond address and fall-through
1064 to code handling that situation below... */
1065 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1066 bond->alb_info.rlb_enabled);
1069 /* The slave's address is equal to the address of the bond.
1070 * Search for a spare address in the bond for this slave.
1072 free_mac_slave = NULL;
1074 bond_for_each_slave(bond, tmp_slave1, i) {
1075 found = 0;
1076 bond_for_each_slave(bond, tmp_slave2, j) {
1077 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1078 tmp_slave2->dev->dev_addr)) {
1079 found = 1;
1080 break;
1084 if (!found) {
1085 /* no slave has tmp_slave1's perm addr
1086 * as its curr addr
1088 free_mac_slave = tmp_slave1;
1089 break;
1092 if (!has_bond_addr) {
1093 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1094 bond->dev->dev_addr)) {
1096 has_bond_addr = tmp_slave1;
1101 if (free_mac_slave) {
1102 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1103 bond->alb_info.rlb_enabled);
1105 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1106 bond->dev->name, slave->dev->name,
1107 free_mac_slave->dev->name);
1109 } else if (has_bond_addr) {
1110 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",
1111 bond->dev->name, slave->dev->name);
1112 return -EFAULT;
1115 return 0;
1119 * alb_set_mac_address
1120 * @bond:
1121 * @addr:
1123 * In TLB mode all slaves are configured to the bond's hw address, but set
1124 * their dev_addr field to different addresses (based on their permanent hw
1125 * addresses).
1127 * For each slave, this function sets the interface to the new address and then
1128 * changes its dev_addr field to its previous value.
1130 * Unwinding assumes bond's mac address has not yet changed.
1132 static int alb_set_mac_address(struct bonding *bond, void *addr)
1134 struct sockaddr sa;
1135 struct slave *slave, *stop_at;
1136 char tmp_addr[ETH_ALEN];
1137 int res;
1138 int i;
1140 if (bond->alb_info.rlb_enabled) {
1141 return 0;
1144 bond_for_each_slave(bond, slave, i) {
1145 /* save net_device's current hw address */
1146 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1148 res = dev_set_mac_address(slave->dev, addr);
1150 /* restore net_device's hw address */
1151 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1153 if (res)
1154 goto unwind;
1157 return 0;
1159 unwind:
1160 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1161 sa.sa_family = bond->dev->type;
1163 /* unwind from head to the slave that failed */
1164 stop_at = slave;
1165 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1166 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1167 dev_set_mac_address(slave->dev, &sa);
1168 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1171 return res;
1174 /************************ exported alb funcions ************************/
1176 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1178 int res;
1180 res = tlb_initialize(bond);
1181 if (res) {
1182 return res;
1185 if (rlb_enabled) {
1186 bond->alb_info.rlb_enabled = 1;
1187 /* initialize rlb */
1188 res = rlb_initialize(bond);
1189 if (res) {
1190 tlb_deinitialize(bond);
1191 return res;
1193 } else {
1194 bond->alb_info.rlb_enabled = 0;
1197 return 0;
1200 void bond_alb_deinitialize(struct bonding *bond)
1202 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1204 tlb_deinitialize(bond);
1206 if (bond_info->rlb_enabled) {
1207 rlb_deinitialize(bond);
1211 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1213 struct bonding *bond = netdev_priv(bond_dev);
1214 struct ethhdr *eth_data;
1215 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1216 struct slave *tx_slave = NULL;
1217 static const __be32 ip_bcast = htonl(0xffffffff);
1218 int hash_size = 0;
1219 int do_tx_balance = 1;
1220 u32 hash_index = 0;
1221 const u8 *hash_start = NULL;
1222 int res = 1;
1223 struct ipv6hdr *ip6hdr;
1225 skb_reset_mac_header(skb);
1226 eth_data = eth_hdr(skb);
1228 /* make sure that the curr_active_slave do not change during tx
1230 read_lock(&bond->curr_slave_lock);
1232 switch (ntohs(skb->protocol)) {
1233 case ETH_P_IP: {
1234 const struct iphdr *iph = ip_hdr(skb);
1236 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
1237 (iph->daddr == ip_bcast) ||
1238 (iph->protocol == IPPROTO_IGMP)) {
1239 do_tx_balance = 0;
1240 break;
1242 hash_start = (char *)&(iph->daddr);
1243 hash_size = sizeof(iph->daddr);
1245 break;
1246 case ETH_P_IPV6:
1247 /* IPv6 doesn't really use broadcast mac address, but leave
1248 * that here just in case.
1250 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
1251 do_tx_balance = 0;
1252 break;
1255 /* IPv6 uses all-nodes multicast as an equivalent to
1256 * broadcasts in IPv4.
1258 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1259 do_tx_balance = 0;
1260 break;
1263 /* Additianally, DAD probes should not be tx-balanced as that
1264 * will lead to false positives for duplicate addresses and
1265 * prevent address configuration from working.
1267 ip6hdr = ipv6_hdr(skb);
1268 if (ipv6_addr_any(&ip6hdr->saddr)) {
1269 do_tx_balance = 0;
1270 break;
1273 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1274 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1275 break;
1276 case ETH_P_IPX:
1277 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1278 /* something is wrong with this packet */
1279 do_tx_balance = 0;
1280 break;
1283 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1284 /* The only protocol worth balancing in
1285 * this family since it has an "ARP" like
1286 * mechanism
1288 do_tx_balance = 0;
1289 break;
1292 hash_start = (char*)eth_data->h_dest;
1293 hash_size = ETH_ALEN;
1294 break;
1295 case ETH_P_ARP:
1296 do_tx_balance = 0;
1297 if (bond_info->rlb_enabled) {
1298 tx_slave = rlb_arp_xmit(skb, bond);
1300 break;
1301 default:
1302 do_tx_balance = 0;
1303 break;
1306 if (do_tx_balance) {
1307 hash_index = _simple_hash(hash_start, hash_size);
1308 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1311 if (!tx_slave) {
1312 /* unbalanced or unassigned, send through primary */
1313 tx_slave = bond->curr_active_slave;
1314 bond_info->unbalanced_load += skb->len;
1317 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1318 if (tx_slave != bond->curr_active_slave) {
1319 memcpy(eth_data->h_source,
1320 tx_slave->dev->dev_addr,
1321 ETH_ALEN);
1324 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1325 } else {
1326 if (tx_slave) {
1327 tlb_clear_slave(bond, tx_slave, 0);
1331 if (res) {
1332 /* no suitable interface, frame not sent */
1333 dev_kfree_skb(skb);
1335 read_unlock(&bond->curr_slave_lock);
1337 return NETDEV_TX_OK;
1340 void bond_alb_monitor(struct work_struct *work)
1342 struct bonding *bond = container_of(work, struct bonding,
1343 alb_work.work);
1344 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1345 struct slave *slave;
1346 int i;
1348 read_lock(&bond->lock);
1350 if (bond->kill_timers) {
1351 goto out;
1354 if (bond->slave_cnt == 0) {
1355 bond_info->tx_rebalance_counter = 0;
1356 bond_info->lp_counter = 0;
1357 goto re_arm;
1360 bond_info->tx_rebalance_counter++;
1361 bond_info->lp_counter++;
1363 /* send learning packets */
1364 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1365 /* change of curr_active_slave involves swapping of mac addresses.
1366 * in order to avoid this swapping from happening while
1367 * sending the learning packets, the curr_slave_lock must be held for
1368 * read.
1370 read_lock(&bond->curr_slave_lock);
1372 bond_for_each_slave(bond, slave, i) {
1373 alb_send_learning_packets(slave, slave->dev->dev_addr);
1376 read_unlock(&bond->curr_slave_lock);
1378 bond_info->lp_counter = 0;
1381 /* rebalance tx traffic */
1382 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1384 read_lock(&bond->curr_slave_lock);
1386 bond_for_each_slave(bond, slave, i) {
1387 tlb_clear_slave(bond, slave, 1);
1388 if (slave == bond->curr_active_slave) {
1389 SLAVE_TLB_INFO(slave).load =
1390 bond_info->unbalanced_load /
1391 BOND_TLB_REBALANCE_INTERVAL;
1392 bond_info->unbalanced_load = 0;
1396 read_unlock(&bond->curr_slave_lock);
1398 bond_info->tx_rebalance_counter = 0;
1401 /* handle rlb stuff */
1402 if (bond_info->rlb_enabled) {
1403 if (bond_info->primary_is_promisc &&
1404 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1407 * dev_set_promiscuity requires rtnl and
1408 * nothing else.
1410 read_unlock(&bond->lock);
1411 rtnl_lock();
1413 bond_info->rlb_promisc_timeout_counter = 0;
1415 /* If the primary was set to promiscuous mode
1416 * because a slave was disabled then
1417 * it can now leave promiscuous mode.
1419 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1420 bond_info->primary_is_promisc = 0;
1422 rtnl_unlock();
1423 read_lock(&bond->lock);
1426 if (bond_info->rlb_rebalance) {
1427 bond_info->rlb_rebalance = 0;
1428 rlb_rebalance(bond);
1431 /* check if clients need updating */
1432 if (bond_info->rx_ntt) {
1433 if (bond_info->rlb_update_delay_counter) {
1434 --bond_info->rlb_update_delay_counter;
1435 } else {
1436 rlb_update_rx_clients(bond);
1437 if (bond_info->rlb_update_retry_counter) {
1438 --bond_info->rlb_update_retry_counter;
1439 } else {
1440 bond_info->rx_ntt = 0;
1446 re_arm:
1447 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1448 out:
1449 read_unlock(&bond->lock);
1452 /* assumption: called before the slave is attached to the bond
1453 * and not locked by the bond lock
1455 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1457 int res;
1459 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1460 bond->alb_info.rlb_enabled);
1461 if (res) {
1462 return res;
1465 /* caller must hold the bond lock for write since the mac addresses
1466 * are compared and may be swapped.
1468 read_lock(&bond->lock);
1470 res = alb_handle_addr_collision_on_attach(bond, slave);
1472 read_unlock(&bond->lock);
1474 if (res) {
1475 return res;
1478 tlb_init_slave(slave);
1480 /* order a rebalance ASAP */
1481 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1483 if (bond->alb_info.rlb_enabled) {
1484 bond->alb_info.rlb_rebalance = 1;
1487 return 0;
1491 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1492 * if necessary.
1494 * Caller must hold RTNL and no other locks
1496 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1498 if (bond->slave_cnt > 1) {
1499 alb_change_hw_addr_on_detach(bond, slave);
1502 tlb_clear_slave(bond, slave, 0);
1504 if (bond->alb_info.rlb_enabled) {
1505 bond->alb_info.next_rx_slave = NULL;
1506 rlb_clear_slave(bond, slave);
1510 /* Caller must hold bond lock for read */
1511 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1513 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1515 if (link == BOND_LINK_DOWN) {
1516 tlb_clear_slave(bond, slave, 0);
1517 if (bond->alb_info.rlb_enabled) {
1518 rlb_clear_slave(bond, slave);
1520 } else if (link == BOND_LINK_UP) {
1521 /* order a rebalance ASAP */
1522 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1523 if (bond->alb_info.rlb_enabled) {
1524 bond->alb_info.rlb_rebalance = 1;
1525 /* If the updelay module parameter is smaller than the
1526 * forwarding delay of the switch the rebalance will
1527 * not work because the rebalance arp replies will
1528 * not be forwarded to the clients..
1535 * bond_alb_handle_active_change - assign new curr_active_slave
1536 * @bond: our bonding struct
1537 * @new_slave: new slave to assign
1539 * Set the bond->curr_active_slave to @new_slave and handle
1540 * mac address swapping and promiscuity changes as needed.
1542 * If new_slave is NULL, caller must hold curr_slave_lock or
1543 * bond->lock for write.
1545 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1546 * read and curr_slave_lock for write. Processing here may sleep, so
1547 * no other locks may be held.
1549 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1550 __releases(&bond->curr_slave_lock)
1551 __releases(&bond->lock)
1552 __acquires(&bond->lock)
1553 __acquires(&bond->curr_slave_lock)
1555 struct slave *swap_slave;
1556 int i;
1558 if (bond->curr_active_slave == new_slave) {
1559 return;
1562 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1563 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1564 bond->alb_info.primary_is_promisc = 0;
1565 bond->alb_info.rlb_promisc_timeout_counter = 0;
1568 swap_slave = bond->curr_active_slave;
1569 bond->curr_active_slave = new_slave;
1571 if (!new_slave || (bond->slave_cnt == 0)) {
1572 return;
1575 /* set the new curr_active_slave to the bonds mac address
1576 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1578 if (!swap_slave) {
1579 struct slave *tmp_slave;
1580 /* find slave that is holding the bond's mac address */
1581 bond_for_each_slave(bond, tmp_slave, i) {
1582 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1583 bond->dev->dev_addr)) {
1584 swap_slave = tmp_slave;
1585 break;
1591 * Arrange for swap_slave and new_slave to temporarily be
1592 * ignored so we can mess with their MAC addresses without
1593 * fear of interference from transmit activity.
1595 if (swap_slave) {
1596 tlb_clear_slave(bond, swap_slave, 1);
1598 tlb_clear_slave(bond, new_slave, 1);
1600 write_unlock_bh(&bond->curr_slave_lock);
1601 read_unlock(&bond->lock);
1603 ASSERT_RTNL();
1605 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1606 if (swap_slave) {
1607 /* swap mac address */
1608 alb_swap_mac_addr(bond, swap_slave, new_slave);
1609 } else {
1610 /* set the new_slave to the bond mac address */
1611 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1612 bond->alb_info.rlb_enabled);
1615 if (swap_slave) {
1616 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1617 read_lock(&bond->lock);
1618 } else {
1619 read_lock(&bond->lock);
1620 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1623 write_lock_bh(&bond->curr_slave_lock);
1627 * Called with RTNL
1629 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1630 __acquires(&bond->lock)
1631 __releases(&bond->lock)
1633 struct bonding *bond = netdev_priv(bond_dev);
1634 struct sockaddr *sa = addr;
1635 struct slave *slave, *swap_slave;
1636 int res;
1637 int i;
1639 if (!is_valid_ether_addr(sa->sa_data)) {
1640 return -EADDRNOTAVAIL;
1643 res = alb_set_mac_address(bond, addr);
1644 if (res) {
1645 return res;
1648 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1650 /* If there is no curr_active_slave there is nothing else to do.
1651 * Otherwise we'll need to pass the new address to it and handle
1652 * duplications.
1654 if (!bond->curr_active_slave) {
1655 return 0;
1658 swap_slave = NULL;
1660 bond_for_each_slave(bond, slave, i) {
1661 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1662 bond_dev->dev_addr)) {
1663 swap_slave = slave;
1664 break;
1668 if (swap_slave) {
1669 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1670 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1671 } else {
1672 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1673 bond->alb_info.rlb_enabled);
1675 read_lock(&bond->lock);
1676 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1677 if (bond->alb_info.rlb_enabled) {
1678 /* inform clients mac address has changed */
1679 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1681 read_unlock(&bond->lock);
1684 return 0;
1687 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1689 if (bond->alb_info.current_alb_vlan &&
1690 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1691 bond->alb_info.current_alb_vlan = NULL;
1694 if (bond->alb_info.rlb_enabled) {
1695 rlb_clear_vlan(bond, vlan_id);