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