bonding: fix alb mode locking regression
[pandora-kernel.git] / drivers / net / bonding / bond_alb.c
1 /*
2  * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  */
22
23 //#define BONDING_DEBUG 1
24
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 <asm/byteorder.h>
42 #include "bonding.h"
43 #include "bond_alb.h"
44
45
46 #define ALB_TIMER_TICKS_PER_SEC     10  /* should be a divisor of HZ */
47 #define BOND_TLB_REBALANCE_INTERVAL 10  /* In seconds, periodic re-balancing.
48                                          * Used for division - never set
49                                          * to zero !!!
50                                          */
51 #define BOND_ALB_LP_INTERVAL        1   /* In seconds, periodic send of
52                                          * learning packets to the switch
53                                          */
54
55 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
56                                   * ALB_TIMER_TICKS_PER_SEC)
57
58 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
59                            * ALB_TIMER_TICKS_PER_SEC)
60
61 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
62                                  * Note that this value MUST NOT be smaller
63                                  * because the key hash table is BYTE wide !
64                                  */
65
66
67 #define TLB_NULL_INDEX          0xffffffff
68 #define MAX_LP_BURST            3
69
70 /* rlb defs */
71 #define RLB_HASH_TABLE_SIZE     256
72 #define RLB_NULL_INDEX          0xffffffff
73 #define RLB_UPDATE_DELAY        2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
74 #define RLB_ARP_BURST_SIZE      2
75 #define RLB_UPDATE_RETRY        3       /* 3-ticks - must be smaller than the rlb
76                                          * rebalance interval (5 min).
77                                          */
78 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
79  * promiscuous after failover
80  */
81 #define RLB_PROMISC_TIMEOUT     10*ALB_TIMER_TICKS_PER_SEC
82
83 static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
84 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
85
86 #pragma pack(1)
87 struct learning_pkt {
88         u8 mac_dst[ETH_ALEN];
89         u8 mac_src[ETH_ALEN];
90         __be16 type;
91         u8 padding[ETH_ZLEN - ETH_HLEN];
92 };
93
94 struct arp_pkt {
95         __be16  hw_addr_space;
96         __be16  prot_addr_space;
97         u8      hw_addr_len;
98         u8      prot_addr_len;
99         __be16  op_code;
100         u8      mac_src[ETH_ALEN];      /* sender hardware address */
101         __be32  ip_src;                 /* sender IP address */
102         u8      mac_dst[ETH_ALEN];      /* target hardware address */
103         __be32  ip_dst;                 /* target IP address */
104 };
105 #pragma pack()
106
107 static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
108 {
109         return (struct arp_pkt *)skb_network_header(skb);
110 }
111
112 /* Forward declaration */
113 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
114
115 static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
116 {
117         int i;
118         u8 hash = 0;
119
120         for (i = 0; i < hash_size; i++) {
121                 hash ^= hash_start[i];
122         }
123
124         return hash;
125 }
126
127 /*********************** tlb specific functions ***************************/
128
129 static inline void _lock_tx_hashtbl(struct bonding *bond)
130 {
131         spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
132 }
133
134 static inline void _unlock_tx_hashtbl(struct bonding *bond)
135 {
136         spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
137 }
138
139 /* Caller must hold tx_hashtbl lock */
140 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
141 {
142         if (save_load) {
143                 entry->load_history = 1 + entry->tx_bytes /
144                                       BOND_TLB_REBALANCE_INTERVAL;
145                 entry->tx_bytes = 0;
146         }
147
148         entry->tx_slave = NULL;
149         entry->next = TLB_NULL_INDEX;
150         entry->prev = TLB_NULL_INDEX;
151 }
152
153 static inline void tlb_init_slave(struct slave *slave)
154 {
155         SLAVE_TLB_INFO(slave).load = 0;
156         SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
157 }
158
159 /* Caller must hold bond lock for read */
160 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
161 {
162         struct tlb_client_info *tx_hash_table;
163         u32 index;
164
165         _lock_tx_hashtbl(bond);
166
167         /* clear slave from tx_hashtbl */
168         tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
169
170         /* skip this if we've already freed the tx hash table */
171         if (tx_hash_table) {
172                 index = SLAVE_TLB_INFO(slave).head;
173                 while (index != TLB_NULL_INDEX) {
174                         u32 next_index = tx_hash_table[index].next;
175                         tlb_init_table_entry(&tx_hash_table[index], save_load);
176                         index = next_index;
177                 }
178         }
179
180         tlb_init_slave(slave);
181
182         _unlock_tx_hashtbl(bond);
183 }
184
185 /* Must be called before starting the monitor timer */
186 static int tlb_initialize(struct bonding *bond)
187 {
188         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
189         int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
190         struct tlb_client_info *new_hashtbl;
191         int i;
192
193         spin_lock_init(&(bond_info->tx_hashtbl_lock));
194
195         new_hashtbl = kzalloc(size, GFP_KERNEL);
196         if (!new_hashtbl) {
197                 printk(KERN_ERR DRV_NAME
198                        ": %s: Error: Failed to allocate TLB hash table\n",
199                        bond->dev->name);
200                 return -1;
201         }
202         _lock_tx_hashtbl(bond);
203
204         bond_info->tx_hashtbl = new_hashtbl;
205
206         for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
207                 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
208         }
209
210         _unlock_tx_hashtbl(bond);
211
212         return 0;
213 }
214
215 /* Must be called only after all slaves have been released */
216 static void tlb_deinitialize(struct bonding *bond)
217 {
218         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
219
220         _lock_tx_hashtbl(bond);
221
222         kfree(bond_info->tx_hashtbl);
223         bond_info->tx_hashtbl = NULL;
224
225         _unlock_tx_hashtbl(bond);
226 }
227
228 /* Caller must hold bond lock for read */
229 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
230 {
231         struct slave *slave, *least_loaded;
232         s64 max_gap;
233         int i, found = 0;
234
235         /* Find the first enabled slave */
236         bond_for_each_slave(bond, slave, i) {
237                 if (SLAVE_IS_OK(slave)) {
238                         found = 1;
239                         break;
240                 }
241         }
242
243         if (!found) {
244                 return NULL;
245         }
246
247         least_loaded = slave;
248         max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
249                         (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
250
251         /* Find the slave with the largest gap */
252         bond_for_each_slave_from(bond, slave, i, least_loaded) {
253                 if (SLAVE_IS_OK(slave)) {
254                         s64 gap = (s64)(slave->speed << 20) -
255                                         (s64)(SLAVE_TLB_INFO(slave).load << 3);
256                         if (max_gap < gap) {
257                                 least_loaded = slave;
258                                 max_gap = gap;
259                         }
260                 }
261         }
262
263         return least_loaded;
264 }
265
266 /* Caller must hold bond lock for read */
267 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
268 {
269         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
270         struct tlb_client_info *hash_table;
271         struct slave *assigned_slave;
272
273         _lock_tx_hashtbl(bond);
274
275         hash_table = bond_info->tx_hashtbl;
276         assigned_slave = hash_table[hash_index].tx_slave;
277         if (!assigned_slave) {
278                 assigned_slave = tlb_get_least_loaded_slave(bond);
279
280                 if (assigned_slave) {
281                         struct tlb_slave_info *slave_info =
282                                 &(SLAVE_TLB_INFO(assigned_slave));
283                         u32 next_index = slave_info->head;
284
285                         hash_table[hash_index].tx_slave = assigned_slave;
286                         hash_table[hash_index].next = next_index;
287                         hash_table[hash_index].prev = TLB_NULL_INDEX;
288
289                         if (next_index != TLB_NULL_INDEX) {
290                                 hash_table[next_index].prev = hash_index;
291                         }
292
293                         slave_info->head = hash_index;
294                         slave_info->load +=
295                                 hash_table[hash_index].load_history;
296                 }
297         }
298
299         if (assigned_slave) {
300                 hash_table[hash_index].tx_bytes += skb_len;
301         }
302
303         _unlock_tx_hashtbl(bond);
304
305         return assigned_slave;
306 }
307
308 /*********************** rlb specific functions ***************************/
309 static inline void _lock_rx_hashtbl(struct bonding *bond)
310 {
311         spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
312 }
313
314 static inline void _unlock_rx_hashtbl(struct bonding *bond)
315 {
316         spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
317 }
318
319 /* when an ARP REPLY is received from a client update its info
320  * in the rx_hashtbl
321  */
322 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
323 {
324         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
325         struct rlb_client_info *client_info;
326         u32 hash_index;
327
328         _lock_rx_hashtbl(bond);
329
330         hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
331         client_info = &(bond_info->rx_hashtbl[hash_index]);
332
333         if ((client_info->assigned) &&
334             (client_info->ip_src == arp->ip_dst) &&
335             (client_info->ip_dst == arp->ip_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;
340         }
341
342         _unlock_rx_hashtbl(bond);
343 }
344
345 static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
346 {
347         struct bonding *bond = bond_dev->priv;
348         struct arp_pkt *arp = (struct arp_pkt *)skb->data;
349         int res = NET_RX_DROP;
350
351         if (dev_net(bond_dev) != &init_net)
352                 goto out;
353
354         if (!(bond_dev->flags & IFF_MASTER))
355                 goto out;
356
357         if (!arp) {
358                 dprintk("Packet has no ARP data\n");
359                 goto out;
360         }
361
362         if (skb->len < sizeof(struct arp_pkt)) {
363                 dprintk("Packet is too small to be an ARP\n");
364                 goto out;
365         }
366
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                 dprintk("Server received an ARP Reply from client\n");
371         }
372
373         res = NET_RX_SUCCESS;
374
375 out:
376         dev_kfree_skb(skb);
377
378         return res;
379 }
380
381 /* Caller must hold bond lock for read */
382 static struct slave *rlb_next_rx_slave(struct bonding *bond)
383 {
384         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
385         struct slave *rx_slave, *slave, *start_at;
386         int i = 0;
387
388         if (bond_info->next_rx_slave) {
389                 start_at = bond_info->next_rx_slave;
390         } else {
391                 start_at = bond->first_slave;
392         }
393
394         rx_slave = NULL;
395
396         bond_for_each_slave_from(bond, slave, i, start_at) {
397                 if (SLAVE_IS_OK(slave)) {
398                         if (!rx_slave) {
399                                 rx_slave = slave;
400                         } else if (slave->speed > rx_slave->speed) {
401                                 rx_slave = slave;
402                         }
403                 }
404         }
405
406         if (rx_slave) {
407                 bond_info->next_rx_slave = rx_slave->next;
408         }
409
410         return rx_slave;
411 }
412
413 /* teach the switch the mac of a disabled slave
414  * on the primary for fault tolerance
415  *
416  * Caller must hold bond->curr_slave_lock for write or bond lock for write
417  */
418 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
419 {
420         if (!bond->curr_active_slave) {
421                 return;
422         }
423
424         if (!bond->alb_info.primary_is_promisc) {
425                 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
426                         bond->alb_info.primary_is_promisc = 1;
427                 else
428                         bond->alb_info.primary_is_promisc = 0;
429         }
430
431         bond->alb_info.rlb_promisc_timeout_counter = 0;
432
433         alb_send_learning_packets(bond->curr_active_slave, addr);
434 }
435
436 /* slave being removed should not be active at this point
437  *
438  * Caller must hold bond lock for read
439  */
440 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
441 {
442         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
443         struct rlb_client_info *rx_hash_table;
444         u32 index, next_index;
445
446         /* clear slave from rx_hashtbl */
447         _lock_rx_hashtbl(bond);
448
449         rx_hash_table = bond_info->rx_hashtbl;
450         index = bond_info->rx_hashtbl_head;
451         for (; index != RLB_NULL_INDEX; index = next_index) {
452                 next_index = rx_hash_table[index].next;
453                 if (rx_hash_table[index].slave == slave) {
454                         struct slave *assigned_slave = rlb_next_rx_slave(bond);
455
456                         if (assigned_slave) {
457                                 rx_hash_table[index].slave = assigned_slave;
458                                 if (memcmp(rx_hash_table[index].mac_dst,
459                                            mac_bcast, ETH_ALEN)) {
460                                         bond_info->rx_hashtbl[index].ntt = 1;
461                                         bond_info->rx_ntt = 1;
462                                         /* A slave has been removed from the
463                                          * table because it is either disabled
464                                          * or being released. We must retry the
465                                          * update to avoid clients from not
466                                          * being updated & disconnecting when
467                                          * there is stress
468                                          */
469                                         bond_info->rlb_update_retry_counter =
470                                                 RLB_UPDATE_RETRY;
471                                 }
472                         } else {  /* there is no active slave */
473                                 rx_hash_table[index].slave = NULL;
474                         }
475                 }
476         }
477
478         _unlock_rx_hashtbl(bond);
479
480         write_lock_bh(&bond->curr_slave_lock);
481
482         if (slave != bond->curr_active_slave) {
483                 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
484         }
485
486         write_unlock_bh(&bond->curr_slave_lock);
487 }
488
489 static void rlb_update_client(struct rlb_client_info *client_info)
490 {
491         int i;
492
493         if (!client_info->slave) {
494                 return;
495         }
496
497         for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
498                 struct sk_buff *skb;
499
500                 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
501                                  client_info->ip_dst,
502                                  client_info->slave->dev,
503                                  client_info->ip_src,
504                                  client_info->mac_dst,
505                                  client_info->slave->dev->dev_addr,
506                                  client_info->mac_dst);
507                 if (!skb) {
508                         printk(KERN_ERR DRV_NAME
509                                ": %s: Error: failed to create an ARP packet\n",
510                                client_info->slave->dev->master->name);
511                         continue;
512                 }
513
514                 skb->dev = client_info->slave->dev;
515
516                 if (client_info->tag) {
517                         skb = vlan_put_tag(skb, client_info->vlan_id);
518                         if (!skb) {
519                                 printk(KERN_ERR DRV_NAME
520                                        ": %s: Error: failed to insert VLAN tag\n",
521                                        client_info->slave->dev->master->name);
522                                 continue;
523                         }
524                 }
525
526                 arp_xmit(skb);
527         }
528 }
529
530 /* sends ARP REPLIES that update the clients that need updating */
531 static void rlb_update_rx_clients(struct bonding *bond)
532 {
533         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
534         struct rlb_client_info *client_info;
535         u32 hash_index;
536
537         _lock_rx_hashtbl(bond);
538
539         hash_index = bond_info->rx_hashtbl_head;
540         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
541                 client_info = &(bond_info->rx_hashtbl[hash_index]);
542                 if (client_info->ntt) {
543                         rlb_update_client(client_info);
544                         if (bond_info->rlb_update_retry_counter == 0) {
545                                 client_info->ntt = 0;
546                         }
547                 }
548         }
549
550         /* do not update the entries again untill this counter is zero so that
551          * not to confuse the clients.
552          */
553         bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
554
555         _unlock_rx_hashtbl(bond);
556 }
557
558 /* The slave was assigned a new mac address - update the clients */
559 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
560 {
561         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
562         struct rlb_client_info *client_info;
563         int ntt = 0;
564         u32 hash_index;
565
566         _lock_rx_hashtbl(bond);
567
568         hash_index = bond_info->rx_hashtbl_head;
569         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
570                 client_info = &(bond_info->rx_hashtbl[hash_index]);
571
572                 if ((client_info->slave == slave) &&
573                     memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
574                         client_info->ntt = 1;
575                         ntt = 1;
576                 }
577         }
578
579         // update the team's flag only after the whole iteration
580         if (ntt) {
581                 bond_info->rx_ntt = 1;
582                 //fasten the change
583                 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
584         }
585
586         _unlock_rx_hashtbl(bond);
587 }
588
589 /* mark all clients using src_ip to be updated */
590 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
591 {
592         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
593         struct rlb_client_info *client_info;
594         u32 hash_index;
595
596         _lock_rx_hashtbl(bond);
597
598         hash_index = bond_info->rx_hashtbl_head;
599         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
600                 client_info = &(bond_info->rx_hashtbl[hash_index]);
601
602                 if (!client_info->slave) {
603                         printk(KERN_ERR DRV_NAME
604                                ": %s: Error: found a client with no channel in "
605                                "the client's hash table\n",
606                                bond->dev->name);
607                         continue;
608                 }
609                 /*update all clients using this src_ip, that are not assigned
610                  * to the team's address (curr_active_slave) and have a known
611                  * unicast mac address.
612                  */
613                 if ((client_info->ip_src == src_ip) &&
614                     memcmp(client_info->slave->dev->dev_addr,
615                            bond->dev->dev_addr, ETH_ALEN) &&
616                     memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
617                         client_info->ntt = 1;
618                         bond_info->rx_ntt = 1;
619                 }
620         }
621
622         _unlock_rx_hashtbl(bond);
623 }
624
625 /* Caller must hold both bond and ptr locks for read */
626 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
627 {
628         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
629         struct arp_pkt *arp = arp_pkt(skb);
630         struct slave *assigned_slave;
631         struct rlb_client_info *client_info;
632         u32 hash_index = 0;
633
634         _lock_rx_hashtbl(bond);
635
636         hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
637         client_info = &(bond_info->rx_hashtbl[hash_index]);
638
639         if (client_info->assigned) {
640                 if ((client_info->ip_src == arp->ip_src) &&
641                     (client_info->ip_dst == arp->ip_dst)) {
642                         /* the entry is already assigned to this client */
643                         if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
644                                 /* update mac address from arp */
645                                 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
646                         }
647
648                         assigned_slave = client_info->slave;
649                         if (assigned_slave) {
650                                 _unlock_rx_hashtbl(bond);
651                                 return assigned_slave;
652                         }
653                 } else {
654                         /* the entry is already assigned to some other client,
655                          * move the old client to primary (curr_active_slave) so
656                          * that the new client can be assigned to this entry.
657                          */
658                         if (bond->curr_active_slave &&
659                             client_info->slave != bond->curr_active_slave) {
660                                 client_info->slave = bond->curr_active_slave;
661                                 rlb_update_client(client_info);
662                         }
663                 }
664         }
665         /* assign a new slave */
666         assigned_slave = rlb_next_rx_slave(bond);
667
668         if (assigned_slave) {
669                 client_info->ip_src = arp->ip_src;
670                 client_info->ip_dst = arp->ip_dst;
671                 /* arp->mac_dst is broadcast for arp reqeusts.
672                  * will be updated with clients actual unicast mac address
673                  * upon receiving an arp reply.
674                  */
675                 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
676                 client_info->slave = assigned_slave;
677
678                 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
679                         client_info->ntt = 1;
680                         bond->alb_info.rx_ntt = 1;
681                 } else {
682                         client_info->ntt = 0;
683                 }
684
685                 if (!list_empty(&bond->vlan_list)) {
686                         if (!vlan_get_tag(skb, &client_info->vlan_id))
687                                 client_info->tag = 1;
688                 }
689
690                 if (!client_info->assigned) {
691                         u32 prev_tbl_head = bond_info->rx_hashtbl_head;
692                         bond_info->rx_hashtbl_head = hash_index;
693                         client_info->next = prev_tbl_head;
694                         if (prev_tbl_head != RLB_NULL_INDEX) {
695                                 bond_info->rx_hashtbl[prev_tbl_head].prev =
696                                         hash_index;
697                         }
698                         client_info->assigned = 1;
699                 }
700         }
701
702         _unlock_rx_hashtbl(bond);
703
704         return assigned_slave;
705 }
706
707 /* chooses (and returns) transmit channel for arp reply
708  * does not choose channel for other arp types since they are
709  * sent on the curr_active_slave
710  */
711 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
712 {
713         struct arp_pkt *arp = arp_pkt(skb);
714         struct slave *tx_slave = NULL;
715
716         if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
717                 /* the arp must be sent on the selected
718                 * rx channel
719                 */
720                 tx_slave = rlb_choose_channel(skb, bond);
721                 if (tx_slave) {
722                         memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
723                 }
724                 dprintk("Server sent ARP Reply packet\n");
725         } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
726                 /* Create an entry in the rx_hashtbl for this client as a
727                  * place holder.
728                  * When the arp reply is received the entry will be updated
729                  * with the correct unicast address of the client.
730                  */
731                 rlb_choose_channel(skb, bond);
732
733                 /* The ARP relpy packets must be delayed so that
734                  * they can cancel out the influence of the ARP request.
735                  */
736                 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
737
738                 /* arp requests are broadcast and are sent on the primary
739                  * the arp request will collapse all clients on the subnet to
740                  * the primary slave. We must register these clients to be
741                  * updated with their assigned mac.
742                  */
743                 rlb_req_update_subnet_clients(bond, arp->ip_src);
744                 dprintk("Server sent ARP Request packet\n");
745         }
746
747         return tx_slave;
748 }
749
750 /* Caller must hold bond lock for read */
751 static void rlb_rebalance(struct bonding *bond)
752 {
753         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
754         struct slave *assigned_slave;
755         struct rlb_client_info *client_info;
756         int ntt;
757         u32 hash_index;
758
759         _lock_rx_hashtbl(bond);
760
761         ntt = 0;
762         hash_index = bond_info->rx_hashtbl_head;
763         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
764                 client_info = &(bond_info->rx_hashtbl[hash_index]);
765                 assigned_slave = rlb_next_rx_slave(bond);
766                 if (assigned_slave && (client_info->slave != assigned_slave)) {
767                         client_info->slave = assigned_slave;
768                         client_info->ntt = 1;
769                         ntt = 1;
770                 }
771         }
772
773         /* update the team's flag only after the whole iteration */
774         if (ntt) {
775                 bond_info->rx_ntt = 1;
776         }
777         _unlock_rx_hashtbl(bond);
778 }
779
780 /* Caller must hold rx_hashtbl lock */
781 static void rlb_init_table_entry(struct rlb_client_info *entry)
782 {
783         memset(entry, 0, sizeof(struct rlb_client_info));
784         entry->next = RLB_NULL_INDEX;
785         entry->prev = RLB_NULL_INDEX;
786 }
787
788 static int rlb_initialize(struct bonding *bond)
789 {
790         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
791         struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
792         struct rlb_client_info  *new_hashtbl;
793         int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
794         int i;
795
796         spin_lock_init(&(bond_info->rx_hashtbl_lock));
797
798         new_hashtbl = kmalloc(size, GFP_KERNEL);
799         if (!new_hashtbl) {
800                 printk(KERN_ERR DRV_NAME
801                        ": %s: Error: Failed to allocate RLB hash table\n",
802                        bond->dev->name);
803                 return -1;
804         }
805         _lock_rx_hashtbl(bond);
806
807         bond_info->rx_hashtbl = new_hashtbl;
808
809         bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
810
811         for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
812                 rlb_init_table_entry(bond_info->rx_hashtbl + i);
813         }
814
815         _unlock_rx_hashtbl(bond);
816
817         /*initialize packet type*/
818         pk_type->type = __constant_htons(ETH_P_ARP);
819         pk_type->dev = bond->dev;
820         pk_type->func = rlb_arp_recv;
821
822         /* register to receive ARPs */
823         dev_add_pack(pk_type);
824
825         return 0;
826 }
827
828 static void rlb_deinitialize(struct bonding *bond)
829 {
830         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
831
832         dev_remove_pack(&(bond_info->rlb_pkt_type));
833
834         _lock_rx_hashtbl(bond);
835
836         kfree(bond_info->rx_hashtbl);
837         bond_info->rx_hashtbl = NULL;
838         bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
839
840         _unlock_rx_hashtbl(bond);
841 }
842
843 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
844 {
845         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
846         u32 curr_index;
847
848         _lock_rx_hashtbl(bond);
849
850         curr_index = bond_info->rx_hashtbl_head;
851         while (curr_index != RLB_NULL_INDEX) {
852                 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
853                 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
854                 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
855
856                 if (curr->tag && (curr->vlan_id == vlan_id)) {
857                         if (curr_index == bond_info->rx_hashtbl_head) {
858                                 bond_info->rx_hashtbl_head = next_index;
859                         }
860                         if (prev_index != RLB_NULL_INDEX) {
861                                 bond_info->rx_hashtbl[prev_index].next = next_index;
862                         }
863                         if (next_index != RLB_NULL_INDEX) {
864                                 bond_info->rx_hashtbl[next_index].prev = prev_index;
865                         }
866
867                         rlb_init_table_entry(curr);
868                 }
869
870                 curr_index = next_index;
871         }
872
873         _unlock_rx_hashtbl(bond);
874 }
875
876 /*********************** tlb/rlb shared functions *********************/
877
878 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
879 {
880         struct bonding *bond = bond_get_bond_by_slave(slave);
881         struct learning_pkt pkt;
882         int size = sizeof(struct learning_pkt);
883         int i;
884
885         memset(&pkt, 0, size);
886         memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
887         memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
888         pkt.type = __constant_htons(ETH_P_LOOP);
889
890         for (i = 0; i < MAX_LP_BURST; i++) {
891                 struct sk_buff *skb;
892                 char *data;
893
894                 skb = dev_alloc_skb(size);
895                 if (!skb) {
896                         return;
897                 }
898
899                 data = skb_put(skb, size);
900                 memcpy(data, &pkt, size);
901
902                 skb_reset_mac_header(skb);
903                 skb->network_header = skb->mac_header + ETH_HLEN;
904                 skb->protocol = pkt.type;
905                 skb->priority = TC_PRIO_CONTROL;
906                 skb->dev = slave->dev;
907
908                 if (!list_empty(&bond->vlan_list)) {
909                         struct vlan_entry *vlan;
910
911                         vlan = bond_next_vlan(bond,
912                                               bond->alb_info.current_alb_vlan);
913
914                         bond->alb_info.current_alb_vlan = vlan;
915                         if (!vlan) {
916                                 kfree_skb(skb);
917                                 continue;
918                         }
919
920                         skb = vlan_put_tag(skb, vlan->vlan_id);
921                         if (!skb) {
922                                 printk(KERN_ERR DRV_NAME
923                                        ": %s: Error: failed to insert VLAN tag\n",
924                                        bond->dev->name);
925                                 continue;
926                         }
927                 }
928
929                 dev_queue_xmit(skb);
930         }
931 }
932
933 /* hw is a boolean parameter that determines whether we should try and
934  * set the hw address of the device as well as the hw address of the
935  * net_device
936  */
937 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
938 {
939         struct net_device *dev = slave->dev;
940         struct sockaddr s_addr;
941
942         if (!hw) {
943                 memcpy(dev->dev_addr, addr, dev->addr_len);
944                 return 0;
945         }
946
947         /* for rlb each slave must have a unique hw mac addresses so that */
948         /* each slave will receive packets destined to a different mac */
949         memcpy(s_addr.sa_data, addr, dev->addr_len);
950         s_addr.sa_family = dev->type;
951         if (dev_set_mac_address(dev, &s_addr)) {
952                 printk(KERN_ERR DRV_NAME
953                        ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
954                        "mode requires that the base driver support setting "
955                        "the hw address also when the network device's "
956                        "interface is open\n",
957                        dev->master->name, dev->name);
958                 return -EOPNOTSUPP;
959         }
960         return 0;
961 }
962
963 /*
964  * Swap MAC addresses between two slaves.
965  *
966  * Called with RTNL held, and no other locks.
967  *
968  */
969
970 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
971 {
972         u8 tmp_mac_addr[ETH_ALEN];
973
974         memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
975         alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
976         alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
977
978 }
979
980 /*
981  * Send learning packets after MAC address swap.
982  *
983  * Called with RTNL and no other locks
984  */
985 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
986                                 struct slave *slave2)
987 {
988         int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
989         struct slave *disabled_slave = NULL;
990
991         ASSERT_RTNL();
992
993         /* fasten the change in the switch */
994         if (SLAVE_IS_OK(slave1)) {
995                 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
996                 if (bond->alb_info.rlb_enabled) {
997                         /* inform the clients that the mac address
998                          * has changed
999                          */
1000                         rlb_req_update_slave_clients(bond, slave1);
1001                 }
1002         } else {
1003                 disabled_slave = slave1;
1004         }
1005
1006         if (SLAVE_IS_OK(slave2)) {
1007                 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1008                 if (bond->alb_info.rlb_enabled) {
1009                         /* inform the clients that the mac address
1010                          * has changed
1011                          */
1012                         rlb_req_update_slave_clients(bond, slave2);
1013                 }
1014         } else {
1015                 disabled_slave = slave2;
1016         }
1017
1018         if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1019                 /* A disabled slave was assigned an active mac addr */
1020                 rlb_teach_disabled_mac_on_primary(bond,
1021                                                   disabled_slave->dev->dev_addr);
1022         }
1023 }
1024
1025 /**
1026  * alb_change_hw_addr_on_detach
1027  * @bond: bonding we're working on
1028  * @slave: the slave that was just detached
1029  *
1030  * We assume that @slave was already detached from the slave list.
1031  *
1032  * If @slave's permanent hw address is different both from its current
1033  * address and from @bond's address, then somewhere in the bond there's
1034  * a slave that has @slave's permanet address as its current address.
1035  * We'll make sure that that slave no longer uses @slave's permanent address.
1036  *
1037  * Caller must hold RTNL and no other locks
1038  */
1039 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1040 {
1041         int perm_curr_diff;
1042         int perm_bond_diff;
1043
1044         perm_curr_diff = memcmp(slave->perm_hwaddr,
1045                                 slave->dev->dev_addr,
1046                                 ETH_ALEN);
1047         perm_bond_diff = memcmp(slave->perm_hwaddr,
1048                                 bond->dev->dev_addr,
1049                                 ETH_ALEN);
1050
1051         if (perm_curr_diff && perm_bond_diff) {
1052                 struct slave *tmp_slave;
1053                 int i, found = 0;
1054
1055                 bond_for_each_slave(bond, tmp_slave, i) {
1056                         if (!memcmp(slave->perm_hwaddr,
1057                                     tmp_slave->dev->dev_addr,
1058                                     ETH_ALEN)) {
1059                                 found = 1;
1060                                 break;
1061                         }
1062                 }
1063
1064                 if (found) {
1065                         /* locking: needs RTNL and nothing else */
1066                         alb_swap_mac_addr(bond, slave, tmp_slave);
1067                         alb_fasten_mac_swap(bond, slave, tmp_slave);
1068                 }
1069         }
1070 }
1071
1072 /**
1073  * alb_handle_addr_collision_on_attach
1074  * @bond: bonding we're working on
1075  * @slave: the slave that was just attached
1076  *
1077  * checks uniqueness of slave's mac address and handles the case the
1078  * new slave uses the bonds mac address.
1079  *
1080  * If the permanent hw address of @slave is @bond's hw address, we need to
1081  * find a different hw address to give @slave, that isn't in use by any other
1082  * slave in the bond. This address must be, of course, one of the premanent
1083  * addresses of the other slaves.
1084  *
1085  * We go over the slave list, and for each slave there we compare its
1086  * permanent hw address with the current address of all the other slaves.
1087  * If no match was found, then we've found a slave with a permanent address
1088  * that isn't used by any other slave in the bond, so we can assign it to
1089  * @slave.
1090  *
1091  * assumption: this function is called before @slave is attached to the
1092  *             bond slave list.
1093  *
1094  * caller must hold the bond lock for write since the mac addresses are compared
1095  * and may be swapped.
1096  */
1097 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1098 {
1099         struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1100         struct slave *has_bond_addr = bond->curr_active_slave;
1101         int i, j, found = 0;
1102
1103         if (bond->slave_cnt == 0) {
1104                 /* this is the first slave */
1105                 return 0;
1106         }
1107
1108         /* if slave's mac address differs from bond's mac address
1109          * check uniqueness of slave's mac address against the other
1110          * slaves in the bond.
1111          */
1112         if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1113                 bond_for_each_slave(bond, tmp_slave1, i) {
1114                         if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1115                                     ETH_ALEN)) {
1116                                 found = 1;
1117                                 break;
1118                         }
1119                 }
1120
1121                 if (!found)
1122                         return 0;
1123
1124                 /* Try setting slave mac to bond address and fall-through
1125                    to code handling that situation below... */
1126                 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1127                                        bond->alb_info.rlb_enabled);
1128         }
1129
1130         /* The slave's address is equal to the address of the bond.
1131          * Search for a spare address in the bond for this slave.
1132          */
1133         free_mac_slave = NULL;
1134
1135         bond_for_each_slave(bond, tmp_slave1, i) {
1136                 found = 0;
1137                 bond_for_each_slave(bond, tmp_slave2, j) {
1138                         if (!memcmp(tmp_slave1->perm_hwaddr,
1139                                     tmp_slave2->dev->dev_addr,
1140                                     ETH_ALEN)) {
1141                                 found = 1;
1142                                 break;
1143                         }
1144                 }
1145
1146                 if (!found) {
1147                         /* no slave has tmp_slave1's perm addr
1148                          * as its curr addr
1149                          */
1150                         free_mac_slave = tmp_slave1;
1151                         break;
1152                 }
1153
1154                 if (!has_bond_addr) {
1155                         if (!memcmp(tmp_slave1->dev->dev_addr,
1156                                     bond->dev->dev_addr,
1157                                     ETH_ALEN)) {
1158
1159                                 has_bond_addr = tmp_slave1;
1160                         }
1161                 }
1162         }
1163
1164         if (free_mac_slave) {
1165                 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1166                                        bond->alb_info.rlb_enabled);
1167
1168                 printk(KERN_WARNING DRV_NAME
1169                        ": %s: Warning: the hw address of slave %s is in use by "
1170                        "the bond; giving it the hw address of %s\n",
1171                        bond->dev->name, slave->dev->name, free_mac_slave->dev->name);
1172
1173         } else if (has_bond_addr) {
1174                 printk(KERN_ERR DRV_NAME
1175                        ": %s: Error: the hw address of slave %s is in use by the "
1176                        "bond; couldn't find a slave with a free hw address to "
1177                        "give it (this should not have happened)\n",
1178                        bond->dev->name, slave->dev->name);
1179                 return -EFAULT;
1180         }
1181
1182         return 0;
1183 }
1184
1185 /**
1186  * alb_set_mac_address
1187  * @bond:
1188  * @addr:
1189  *
1190  * In TLB mode all slaves are configured to the bond's hw address, but set
1191  * their dev_addr field to different addresses (based on their permanent hw
1192  * addresses).
1193  *
1194  * For each slave, this function sets the interface to the new address and then
1195  * changes its dev_addr field to its previous value.
1196  *
1197  * Unwinding assumes bond's mac address has not yet changed.
1198  */
1199 static int alb_set_mac_address(struct bonding *bond, void *addr)
1200 {
1201         struct sockaddr sa;
1202         struct slave *slave, *stop_at;
1203         char tmp_addr[ETH_ALEN];
1204         int res;
1205         int i;
1206
1207         if (bond->alb_info.rlb_enabled) {
1208                 return 0;
1209         }
1210
1211         bond_for_each_slave(bond, slave, i) {
1212                 if (slave->dev->set_mac_address == NULL) {
1213                         res = -EOPNOTSUPP;
1214                         goto unwind;
1215                 }
1216
1217                 /* save net_device's current hw address */
1218                 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1219
1220                 res = dev_set_mac_address(slave->dev, addr);
1221
1222                 /* restore net_device's hw address */
1223                 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1224
1225                 if (res) {
1226                         goto unwind;
1227                 }
1228         }
1229
1230         return 0;
1231
1232 unwind:
1233         memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1234         sa.sa_family = bond->dev->type;
1235
1236         /* unwind from head to the slave that failed */
1237         stop_at = slave;
1238         bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1239                 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1240                 dev_set_mac_address(slave->dev, &sa);
1241                 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1242         }
1243
1244         return res;
1245 }
1246
1247 /************************ exported alb funcions ************************/
1248
1249 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1250 {
1251         int res;
1252
1253         res = tlb_initialize(bond);
1254         if (res) {
1255                 return res;
1256         }
1257
1258         if (rlb_enabled) {
1259                 bond->alb_info.rlb_enabled = 1;
1260                 /* initialize rlb */
1261                 res = rlb_initialize(bond);
1262                 if (res) {
1263                         tlb_deinitialize(bond);
1264                         return res;
1265                 }
1266         } else {
1267                 bond->alb_info.rlb_enabled = 0;
1268         }
1269
1270         return 0;
1271 }
1272
1273 void bond_alb_deinitialize(struct bonding *bond)
1274 {
1275         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1276
1277         tlb_deinitialize(bond);
1278
1279         if (bond_info->rlb_enabled) {
1280                 rlb_deinitialize(bond);
1281         }
1282 }
1283
1284 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1285 {
1286         struct bonding *bond = bond_dev->priv;
1287         struct ethhdr *eth_data;
1288         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1289         struct slave *tx_slave = NULL;
1290         static const __be32 ip_bcast = htonl(0xffffffff);
1291         int hash_size = 0;
1292         int do_tx_balance = 1;
1293         u32 hash_index = 0;
1294         const u8 *hash_start = NULL;
1295         int res = 1;
1296
1297         skb_reset_mac_header(skb);
1298         eth_data = eth_hdr(skb);
1299
1300         /* make sure that the curr_active_slave and the slaves list do
1301          * not change during tx
1302          */
1303         read_lock(&bond->lock);
1304         read_lock(&bond->curr_slave_lock);
1305
1306         if (!BOND_IS_OK(bond)) {
1307                 goto out;
1308         }
1309
1310         switch (ntohs(skb->protocol)) {
1311         case ETH_P_IP: {
1312                 const struct iphdr *iph = ip_hdr(skb);
1313
1314                 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1315                     (iph->daddr == ip_bcast) ||
1316                     (iph->protocol == IPPROTO_IGMP)) {
1317                         do_tx_balance = 0;
1318                         break;
1319                 }
1320                 hash_start = (char *)&(iph->daddr);
1321                 hash_size = sizeof(iph->daddr);
1322         }
1323                 break;
1324         case ETH_P_IPV6:
1325                 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1326                         do_tx_balance = 0;
1327                         break;
1328                 }
1329
1330                 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1331                 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1332                 break;
1333         case ETH_P_IPX:
1334                 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1335                         /* something is wrong with this packet */
1336                         do_tx_balance = 0;
1337                         break;
1338                 }
1339
1340                 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1341                         /* The only protocol worth balancing in
1342                          * this family since it has an "ARP" like
1343                          * mechanism
1344                          */
1345                         do_tx_balance = 0;
1346                         break;
1347                 }
1348
1349                 hash_start = (char*)eth_data->h_dest;
1350                 hash_size = ETH_ALEN;
1351                 break;
1352         case ETH_P_ARP:
1353                 do_tx_balance = 0;
1354                 if (bond_info->rlb_enabled) {
1355                         tx_slave = rlb_arp_xmit(skb, bond);
1356                 }
1357                 break;
1358         default:
1359                 do_tx_balance = 0;
1360                 break;
1361         }
1362
1363         if (do_tx_balance) {
1364                 hash_index = _simple_hash(hash_start, hash_size);
1365                 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1366         }
1367
1368         if (!tx_slave) {
1369                 /* unbalanced or unassigned, send through primary */
1370                 tx_slave = bond->curr_active_slave;
1371                 bond_info->unbalanced_load += skb->len;
1372         }
1373
1374         if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1375                 if (tx_slave != bond->curr_active_slave) {
1376                         memcpy(eth_data->h_source,
1377                                tx_slave->dev->dev_addr,
1378                                ETH_ALEN);
1379                 }
1380
1381                 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1382         } else {
1383                 if (tx_slave) {
1384                         tlb_clear_slave(bond, tx_slave, 0);
1385                 }
1386         }
1387
1388 out:
1389         if (res) {
1390                 /* no suitable interface, frame not sent */
1391                 dev_kfree_skb(skb);
1392         }
1393         read_unlock(&bond->curr_slave_lock);
1394         read_unlock(&bond->lock);
1395         return 0;
1396 }
1397
1398 void bond_alb_monitor(struct work_struct *work)
1399 {
1400         struct bonding *bond = container_of(work, struct bonding,
1401                                             alb_work.work);
1402         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1403         struct slave *slave;
1404         int i;
1405
1406         read_lock(&bond->lock);
1407
1408         if (bond->kill_timers) {
1409                 goto out;
1410         }
1411
1412         if (bond->slave_cnt == 0) {
1413                 bond_info->tx_rebalance_counter = 0;
1414                 bond_info->lp_counter = 0;
1415                 goto re_arm;
1416         }
1417
1418         bond_info->tx_rebalance_counter++;
1419         bond_info->lp_counter++;
1420
1421         /* send learning packets */
1422         if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1423                 /* change of curr_active_slave involves swapping of mac addresses.
1424                  * in order to avoid this swapping from happening while
1425                  * sending the learning packets, the curr_slave_lock must be held for
1426                  * read.
1427                  */
1428                 read_lock(&bond->curr_slave_lock);
1429
1430                 bond_for_each_slave(bond, slave, i) {
1431                         alb_send_learning_packets(slave, slave->dev->dev_addr);
1432                 }
1433
1434                 read_unlock(&bond->curr_slave_lock);
1435
1436                 bond_info->lp_counter = 0;
1437         }
1438
1439         /* rebalance tx traffic */
1440         if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1441
1442                 read_lock(&bond->curr_slave_lock);
1443
1444                 bond_for_each_slave(bond, slave, i) {
1445                         tlb_clear_slave(bond, slave, 1);
1446                         if (slave == bond->curr_active_slave) {
1447                                 SLAVE_TLB_INFO(slave).load =
1448                                         bond_info->unbalanced_load /
1449                                                 BOND_TLB_REBALANCE_INTERVAL;
1450                                 bond_info->unbalanced_load = 0;
1451                         }
1452                 }
1453
1454                 read_unlock(&bond->curr_slave_lock);
1455
1456                 bond_info->tx_rebalance_counter = 0;
1457         }
1458
1459         /* handle rlb stuff */
1460         if (bond_info->rlb_enabled) {
1461                 if (bond_info->primary_is_promisc &&
1462                     (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1463
1464                         /*
1465                          * dev_set_promiscuity requires rtnl and
1466                          * nothing else.
1467                          */
1468                         read_unlock(&bond->lock);
1469                         rtnl_lock();
1470
1471                         bond_info->rlb_promisc_timeout_counter = 0;
1472
1473                         /* If the primary was set to promiscuous mode
1474                          * because a slave was disabled then
1475                          * it can now leave promiscuous mode.
1476                          */
1477                         dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1478                         bond_info->primary_is_promisc = 0;
1479
1480                         rtnl_unlock();
1481                         read_lock(&bond->lock);
1482                 }
1483
1484                 if (bond_info->rlb_rebalance) {
1485                         bond_info->rlb_rebalance = 0;
1486                         rlb_rebalance(bond);
1487                 }
1488
1489                 /* check if clients need updating */
1490                 if (bond_info->rx_ntt) {
1491                         if (bond_info->rlb_update_delay_counter) {
1492                                 --bond_info->rlb_update_delay_counter;
1493                         } else {
1494                                 rlb_update_rx_clients(bond);
1495                                 if (bond_info->rlb_update_retry_counter) {
1496                                         --bond_info->rlb_update_retry_counter;
1497                                 } else {
1498                                         bond_info->rx_ntt = 0;
1499                                 }
1500                         }
1501                 }
1502         }
1503
1504 re_arm:
1505         queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1506 out:
1507         read_unlock(&bond->lock);
1508 }
1509
1510 /* assumption: called before the slave is attached to the bond
1511  * and not locked by the bond lock
1512  */
1513 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1514 {
1515         int res;
1516
1517         res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1518                                      bond->alb_info.rlb_enabled);
1519         if (res) {
1520                 return res;
1521         }
1522
1523         /* caller must hold the bond lock for write since the mac addresses
1524          * are compared and may be swapped.
1525          */
1526         read_lock(&bond->lock);
1527
1528         res = alb_handle_addr_collision_on_attach(bond, slave);
1529
1530         read_unlock(&bond->lock);
1531
1532         if (res) {
1533                 return res;
1534         }
1535
1536         tlb_init_slave(slave);
1537
1538         /* order a rebalance ASAP */
1539         bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1540
1541         if (bond->alb_info.rlb_enabled) {
1542                 bond->alb_info.rlb_rebalance = 1;
1543         }
1544
1545         return 0;
1546 }
1547
1548 /*
1549  * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1550  * if necessary.
1551  *
1552  * Caller must hold RTNL and no other locks
1553  */
1554 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1555 {
1556         if (bond->slave_cnt > 1) {
1557                 alb_change_hw_addr_on_detach(bond, slave);
1558         }
1559
1560         tlb_clear_slave(bond, slave, 0);
1561
1562         if (bond->alb_info.rlb_enabled) {
1563                 bond->alb_info.next_rx_slave = NULL;
1564                 rlb_clear_slave(bond, slave);
1565         }
1566 }
1567
1568 /* Caller must hold bond lock for read */
1569 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1570 {
1571         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1572
1573         if (link == BOND_LINK_DOWN) {
1574                 tlb_clear_slave(bond, slave, 0);
1575                 if (bond->alb_info.rlb_enabled) {
1576                         rlb_clear_slave(bond, slave);
1577                 }
1578         } else if (link == BOND_LINK_UP) {
1579                 /* order a rebalance ASAP */
1580                 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1581                 if (bond->alb_info.rlb_enabled) {
1582                         bond->alb_info.rlb_rebalance = 1;
1583                         /* If the updelay module parameter is smaller than the
1584                          * forwarding delay of the switch the rebalance will
1585                          * not work because the rebalance arp replies will
1586                          * not be forwarded to the clients..
1587                          */
1588                 }
1589         }
1590 }
1591
1592 /**
1593  * bond_alb_handle_active_change - assign new curr_active_slave
1594  * @bond: our bonding struct
1595  * @new_slave: new slave to assign
1596  *
1597  * Set the bond->curr_active_slave to @new_slave and handle
1598  * mac address swapping and promiscuity changes as needed.
1599  *
1600  * If new_slave is NULL, caller must hold curr_slave_lock or
1601  * bond->lock for write.
1602  *
1603  * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1604  * read and curr_slave_lock for write.  Processing here may sleep, so
1605  * no other locks may be held.
1606  */
1607 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1608 {
1609         struct slave *swap_slave;
1610         int i;
1611
1612         if (bond->curr_active_slave == new_slave) {
1613                 return;
1614         }
1615
1616         if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1617                 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1618                 bond->alb_info.primary_is_promisc = 0;
1619                 bond->alb_info.rlb_promisc_timeout_counter = 0;
1620         }
1621
1622         swap_slave = bond->curr_active_slave;
1623         bond->curr_active_slave = new_slave;
1624
1625         if (!new_slave || (bond->slave_cnt == 0)) {
1626                 return;
1627         }
1628
1629         /* set the new curr_active_slave to the bonds mac address
1630          * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1631          */
1632         if (!swap_slave) {
1633                 struct slave *tmp_slave;
1634                 /* find slave that is holding the bond's mac address */
1635                 bond_for_each_slave(bond, tmp_slave, i) {
1636                         if (!memcmp(tmp_slave->dev->dev_addr,
1637                                     bond->dev->dev_addr, ETH_ALEN)) {
1638                                 swap_slave = tmp_slave;
1639                                 break;
1640                         }
1641                 }
1642         }
1643
1644         /*
1645          * Arrange for swap_slave and new_slave to temporarily be
1646          * ignored so we can mess with their MAC addresses without
1647          * fear of interference from transmit activity.
1648          */
1649         if (swap_slave) {
1650                 tlb_clear_slave(bond, swap_slave, 1);
1651         }
1652         tlb_clear_slave(bond, new_slave, 1);
1653
1654         write_unlock_bh(&bond->curr_slave_lock);
1655         read_unlock(&bond->lock);
1656
1657         ASSERT_RTNL();
1658
1659         /* curr_active_slave must be set before calling alb_swap_mac_addr */
1660         if (swap_slave) {
1661                 /* swap mac address */
1662                 alb_swap_mac_addr(bond, swap_slave, new_slave);
1663         } else {
1664                 /* set the new_slave to the bond mac address */
1665                 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1666                                        bond->alb_info.rlb_enabled);
1667         }
1668
1669         if (swap_slave) {
1670                 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1671                 read_lock(&bond->lock);
1672         } else {
1673                 read_lock(&bond->lock);
1674                 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1675         }
1676
1677         write_lock_bh(&bond->curr_slave_lock);
1678 }
1679
1680 /*
1681  * Called with RTNL
1682  */
1683 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1684 {
1685         struct bonding *bond = bond_dev->priv;
1686         struct sockaddr *sa = addr;
1687         struct slave *slave, *swap_slave;
1688         int res;
1689         int i;
1690
1691         if (!is_valid_ether_addr(sa->sa_data)) {
1692                 return -EADDRNOTAVAIL;
1693         }
1694
1695         res = alb_set_mac_address(bond, addr);
1696         if (res) {
1697                 return res;
1698         }
1699
1700         memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1701
1702         /* If there is no curr_active_slave there is nothing else to do.
1703          * Otherwise we'll need to pass the new address to it and handle
1704          * duplications.
1705          */
1706         if (!bond->curr_active_slave) {
1707                 return 0;
1708         }
1709
1710         swap_slave = NULL;
1711
1712         bond_for_each_slave(bond, slave, i) {
1713                 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1714                         swap_slave = slave;
1715                         break;
1716                 }
1717         }
1718
1719         if (swap_slave) {
1720                 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1721                 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1722         } else {
1723                 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1724                                        bond->alb_info.rlb_enabled);
1725
1726                 read_lock(&bond->lock);
1727                 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1728                 if (bond->alb_info.rlb_enabled) {
1729                         /* inform clients mac address has changed */
1730                         rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1731                 }
1732                 read_unlock(&bond->lock);
1733         }
1734
1735         return 0;
1736 }
1737
1738 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1739 {
1740         if (bond->alb_info.current_alb_vlan &&
1741             (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1742                 bond->alb_info.current_alb_vlan = NULL;
1743         }
1744
1745         if (bond->alb_info.rlb_enabled) {
1746                 rlb_clear_vlan(bond, vlan_id);
1747         }
1748 }
1749