Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild-fixes
[linux-2.6] / 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 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/pkt_sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
29 #include <linux/timer.h>
30 #include <linux/ip.h>
31 #include <linux/ipv6.h>
32 #include <linux/if_arp.h>
33 #include <linux/if_ether.h>
34 #include <linux/if_bonding.h>
35 #include <linux/if_vlan.h>
36 #include <linux/in.h>
37 #include <net/ipx.h>
38 #include <net/arp.h>
39 #include <net/ipv6.h>
40 #include <asm/byteorder.h>
41 #include "bonding.h"
42 #include "bond_alb.h"
43
44
45 #define ALB_TIMER_TICKS_PER_SEC     10  /* should be a divisor of HZ */
46 #define BOND_TLB_REBALANCE_INTERVAL 10  /* In seconds, periodic re-balancing.
47                                          * Used for division - never set
48                                          * to zero !!!
49                                          */
50 #define BOND_ALB_LP_INTERVAL        1   /* In seconds, periodic send of
51                                          * learning packets to the switch
52                                          */
53
54 #define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
55                                   * ALB_TIMER_TICKS_PER_SEC)
56
57 #define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
58                            * ALB_TIMER_TICKS_PER_SEC)
59
60 #define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
61                                  * Note that this value MUST NOT be smaller
62                                  * because the key hash table is BYTE wide !
63                                  */
64
65
66 #define TLB_NULL_INDEX          0xffffffff
67 #define MAX_LP_BURST            3
68
69 /* rlb defs */
70 #define RLB_HASH_TABLE_SIZE     256
71 #define RLB_NULL_INDEX          0xffffffff
72 #define RLB_UPDATE_DELAY        2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
73 #define RLB_ARP_BURST_SIZE      2
74 #define RLB_UPDATE_RETRY        3       /* 3-ticks - must be smaller than the rlb
75                                          * rebalance interval (5 min).
76                                          */
77 /* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
78  * promiscuous after failover
79  */
80 #define RLB_PROMISC_TIMEOUT     10*ALB_TIMER_TICKS_PER_SEC
81
82 static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
83 static const u8 mac_v6_allmcast[ETH_ALEN] = {0x33,0x33,0x00,0x00,0x00,0x01};
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;
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         while (bond_dev->priv_flags & IFF_802_1Q_VLAN)
355                 bond_dev = vlan_dev_real_dev(bond_dev);
356
357         if (!(bond_dev->priv_flags & IFF_BONDING) ||
358             !(bond_dev->flags & IFF_MASTER))
359                 goto out;
360
361         if (!arp) {
362                 pr_debug("Packet has no ARP data\n");
363                 goto out;
364         }
365
366         if (skb->len < sizeof(struct arp_pkt)) {
367                 pr_debug("Packet is too small to be an ARP\n");
368                 goto out;
369         }
370
371         if (arp->op_code == htons(ARPOP_REPLY)) {
372                 /* update rx hash table for this ARP */
373                 bond = netdev_priv(bond_dev);
374                 rlb_update_entry_from_arp(bond, arp);
375                 pr_debug("Server received an ARP Reply from client\n");
376         }
377
378         res = NET_RX_SUCCESS;
379
380 out:
381         dev_kfree_skb(skb);
382
383         return res;
384 }
385
386 /* Caller must hold bond lock for read */
387 static struct slave *rlb_next_rx_slave(struct bonding *bond)
388 {
389         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
390         struct slave *rx_slave, *slave, *start_at;
391         int i = 0;
392
393         if (bond_info->next_rx_slave) {
394                 start_at = bond_info->next_rx_slave;
395         } else {
396                 start_at = bond->first_slave;
397         }
398
399         rx_slave = NULL;
400
401         bond_for_each_slave_from(bond, slave, i, start_at) {
402                 if (SLAVE_IS_OK(slave)) {
403                         if (!rx_slave) {
404                                 rx_slave = slave;
405                         } else if (slave->speed > rx_slave->speed) {
406                                 rx_slave = slave;
407                         }
408                 }
409         }
410
411         if (rx_slave) {
412                 bond_info->next_rx_slave = rx_slave->next;
413         }
414
415         return rx_slave;
416 }
417
418 /* teach the switch the mac of a disabled slave
419  * on the primary for fault tolerance
420  *
421  * Caller must hold bond->curr_slave_lock for write or bond lock for write
422  */
423 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
424 {
425         if (!bond->curr_active_slave) {
426                 return;
427         }
428
429         if (!bond->alb_info.primary_is_promisc) {
430                 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
431                         bond->alb_info.primary_is_promisc = 1;
432                 else
433                         bond->alb_info.primary_is_promisc = 0;
434         }
435
436         bond->alb_info.rlb_promisc_timeout_counter = 0;
437
438         alb_send_learning_packets(bond->curr_active_slave, addr);
439 }
440
441 /* slave being removed should not be active at this point
442  *
443  * Caller must hold bond lock for read
444  */
445 static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
446 {
447         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
448         struct rlb_client_info *rx_hash_table;
449         u32 index, next_index;
450
451         /* clear slave from rx_hashtbl */
452         _lock_rx_hashtbl(bond);
453
454         rx_hash_table = bond_info->rx_hashtbl;
455         index = bond_info->rx_hashtbl_head;
456         for (; index != RLB_NULL_INDEX; index = next_index) {
457                 next_index = rx_hash_table[index].next;
458                 if (rx_hash_table[index].slave == slave) {
459                         struct slave *assigned_slave = rlb_next_rx_slave(bond);
460
461                         if (assigned_slave) {
462                                 rx_hash_table[index].slave = assigned_slave;
463                                 if (memcmp(rx_hash_table[index].mac_dst,
464                                            mac_bcast, ETH_ALEN)) {
465                                         bond_info->rx_hashtbl[index].ntt = 1;
466                                         bond_info->rx_ntt = 1;
467                                         /* A slave has been removed from the
468                                          * table because it is either disabled
469                                          * or being released. We must retry the
470                                          * update to avoid clients from not
471                                          * being updated & disconnecting when
472                                          * there is stress
473                                          */
474                                         bond_info->rlb_update_retry_counter =
475                                                 RLB_UPDATE_RETRY;
476                                 }
477                         } else {  /* there is no active slave */
478                                 rx_hash_table[index].slave = NULL;
479                         }
480                 }
481         }
482
483         _unlock_rx_hashtbl(bond);
484
485         write_lock_bh(&bond->curr_slave_lock);
486
487         if (slave != bond->curr_active_slave) {
488                 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
489         }
490
491         write_unlock_bh(&bond->curr_slave_lock);
492 }
493
494 static void rlb_update_client(struct rlb_client_info *client_info)
495 {
496         int i;
497
498         if (!client_info->slave) {
499                 return;
500         }
501
502         for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
503                 struct sk_buff *skb;
504
505                 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
506                                  client_info->ip_dst,
507                                  client_info->slave->dev,
508                                  client_info->ip_src,
509                                  client_info->mac_dst,
510                                  client_info->slave->dev->dev_addr,
511                                  client_info->mac_dst);
512                 if (!skb) {
513                         printk(KERN_ERR DRV_NAME
514                                ": %s: Error: failed to create an ARP packet\n",
515                                client_info->slave->dev->master->name);
516                         continue;
517                 }
518
519                 skb->dev = client_info->slave->dev;
520
521                 if (client_info->tag) {
522                         skb = vlan_put_tag(skb, client_info->vlan_id);
523                         if (!skb) {
524                                 printk(KERN_ERR DRV_NAME
525                                        ": %s: Error: failed to insert VLAN tag\n",
526                                        client_info->slave->dev->master->name);
527                                 continue;
528                         }
529                 }
530
531                 arp_xmit(skb);
532         }
533 }
534
535 /* sends ARP REPLIES that update the clients that need updating */
536 static void rlb_update_rx_clients(struct bonding *bond)
537 {
538         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
539         struct rlb_client_info *client_info;
540         u32 hash_index;
541
542         _lock_rx_hashtbl(bond);
543
544         hash_index = bond_info->rx_hashtbl_head;
545         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
546                 client_info = &(bond_info->rx_hashtbl[hash_index]);
547                 if (client_info->ntt) {
548                         rlb_update_client(client_info);
549                         if (bond_info->rlb_update_retry_counter == 0) {
550                                 client_info->ntt = 0;
551                         }
552                 }
553         }
554
555         /* do not update the entries again untill this counter is zero so that
556          * not to confuse the clients.
557          */
558         bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
559
560         _unlock_rx_hashtbl(bond);
561 }
562
563 /* The slave was assigned a new mac address - update the clients */
564 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
565 {
566         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
567         struct rlb_client_info *client_info;
568         int ntt = 0;
569         u32 hash_index;
570
571         _lock_rx_hashtbl(bond);
572
573         hash_index = bond_info->rx_hashtbl_head;
574         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
575                 client_info = &(bond_info->rx_hashtbl[hash_index]);
576
577                 if ((client_info->slave == slave) &&
578                     memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
579                         client_info->ntt = 1;
580                         ntt = 1;
581                 }
582         }
583
584         // update the team's flag only after the whole iteration
585         if (ntt) {
586                 bond_info->rx_ntt = 1;
587                 //fasten the change
588                 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
589         }
590
591         _unlock_rx_hashtbl(bond);
592 }
593
594 /* mark all clients using src_ip to be updated */
595 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
596 {
597         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
598         struct rlb_client_info *client_info;
599         u32 hash_index;
600
601         _lock_rx_hashtbl(bond);
602
603         hash_index = bond_info->rx_hashtbl_head;
604         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
605                 client_info = &(bond_info->rx_hashtbl[hash_index]);
606
607                 if (!client_info->slave) {
608                         printk(KERN_ERR DRV_NAME
609                                ": %s: Error: found a client with no channel in "
610                                "the client's hash table\n",
611                                bond->dev->name);
612                         continue;
613                 }
614                 /*update all clients using this src_ip, that are not assigned
615                  * to the team's address (curr_active_slave) and have a known
616                  * unicast mac address.
617                  */
618                 if ((client_info->ip_src == src_ip) &&
619                     memcmp(client_info->slave->dev->dev_addr,
620                            bond->dev->dev_addr, ETH_ALEN) &&
621                     memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
622                         client_info->ntt = 1;
623                         bond_info->rx_ntt = 1;
624                 }
625         }
626
627         _unlock_rx_hashtbl(bond);
628 }
629
630 /* Caller must hold both bond and ptr locks for read */
631 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
632 {
633         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
634         struct arp_pkt *arp = arp_pkt(skb);
635         struct slave *assigned_slave;
636         struct rlb_client_info *client_info;
637         u32 hash_index = 0;
638
639         _lock_rx_hashtbl(bond);
640
641         hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
642         client_info = &(bond_info->rx_hashtbl[hash_index]);
643
644         if (client_info->assigned) {
645                 if ((client_info->ip_src == arp->ip_src) &&
646                     (client_info->ip_dst == arp->ip_dst)) {
647                         /* the entry is already assigned to this client */
648                         if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
649                                 /* update mac address from arp */
650                                 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
651                         }
652
653                         assigned_slave = client_info->slave;
654                         if (assigned_slave) {
655                                 _unlock_rx_hashtbl(bond);
656                                 return assigned_slave;
657                         }
658                 } else {
659                         /* the entry is already assigned to some other client,
660                          * move the old client to primary (curr_active_slave) so
661                          * that the new client can be assigned to this entry.
662                          */
663                         if (bond->curr_active_slave &&
664                             client_info->slave != bond->curr_active_slave) {
665                                 client_info->slave = bond->curr_active_slave;
666                                 rlb_update_client(client_info);
667                         }
668                 }
669         }
670         /* assign a new slave */
671         assigned_slave = rlb_next_rx_slave(bond);
672
673         if (assigned_slave) {
674                 client_info->ip_src = arp->ip_src;
675                 client_info->ip_dst = arp->ip_dst;
676                 /* arp->mac_dst is broadcast for arp reqeusts.
677                  * will be updated with clients actual unicast mac address
678                  * upon receiving an arp reply.
679                  */
680                 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
681                 client_info->slave = assigned_slave;
682
683                 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
684                         client_info->ntt = 1;
685                         bond->alb_info.rx_ntt = 1;
686                 } else {
687                         client_info->ntt = 0;
688                 }
689
690                 if (!list_empty(&bond->vlan_list)) {
691                         if (!vlan_get_tag(skb, &client_info->vlan_id))
692                                 client_info->tag = 1;
693                 }
694
695                 if (!client_info->assigned) {
696                         u32 prev_tbl_head = bond_info->rx_hashtbl_head;
697                         bond_info->rx_hashtbl_head = hash_index;
698                         client_info->next = prev_tbl_head;
699                         if (prev_tbl_head != RLB_NULL_INDEX) {
700                                 bond_info->rx_hashtbl[prev_tbl_head].prev =
701                                         hash_index;
702                         }
703                         client_info->assigned = 1;
704                 }
705         }
706
707         _unlock_rx_hashtbl(bond);
708
709         return assigned_slave;
710 }
711
712 /* chooses (and returns) transmit channel for arp reply
713  * does not choose channel for other arp types since they are
714  * sent on the curr_active_slave
715  */
716 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
717 {
718         struct arp_pkt *arp = arp_pkt(skb);
719         struct slave *tx_slave = NULL;
720
721         if (arp->op_code == htons(ARPOP_REPLY)) {
722                 /* the arp must be sent on the selected
723                 * rx channel
724                 */
725                 tx_slave = rlb_choose_channel(skb, bond);
726                 if (tx_slave) {
727                         memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
728                 }
729                 pr_debug("Server sent ARP Reply packet\n");
730         } else if (arp->op_code == htons(ARPOP_REQUEST)) {
731                 /* Create an entry in the rx_hashtbl for this client as a
732                  * place holder.
733                  * When the arp reply is received the entry will be updated
734                  * with the correct unicast address of the client.
735                  */
736                 rlb_choose_channel(skb, bond);
737
738                 /* The ARP relpy packets must be delayed so that
739                  * they can cancel out the influence of the ARP request.
740                  */
741                 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
742
743                 /* arp requests are broadcast and are sent on the primary
744                  * the arp request will collapse all clients on the subnet to
745                  * the primary slave. We must register these clients to be
746                  * updated with their assigned mac.
747                  */
748                 rlb_req_update_subnet_clients(bond, arp->ip_src);
749                 pr_debug("Server sent ARP Request packet\n");
750         }
751
752         return tx_slave;
753 }
754
755 /* Caller must hold bond lock for read */
756 static void rlb_rebalance(struct bonding *bond)
757 {
758         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
759         struct slave *assigned_slave;
760         struct rlb_client_info *client_info;
761         int ntt;
762         u32 hash_index;
763
764         _lock_rx_hashtbl(bond);
765
766         ntt = 0;
767         hash_index = bond_info->rx_hashtbl_head;
768         for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
769                 client_info = &(bond_info->rx_hashtbl[hash_index]);
770                 assigned_slave = rlb_next_rx_slave(bond);
771                 if (assigned_slave && (client_info->slave != assigned_slave)) {
772                         client_info->slave = assigned_slave;
773                         client_info->ntt = 1;
774                         ntt = 1;
775                 }
776         }
777
778         /* update the team's flag only after the whole iteration */
779         if (ntt) {
780                 bond_info->rx_ntt = 1;
781         }
782         _unlock_rx_hashtbl(bond);
783 }
784
785 /* Caller must hold rx_hashtbl lock */
786 static void rlb_init_table_entry(struct rlb_client_info *entry)
787 {
788         memset(entry, 0, sizeof(struct rlb_client_info));
789         entry->next = RLB_NULL_INDEX;
790         entry->prev = RLB_NULL_INDEX;
791 }
792
793 static int rlb_initialize(struct bonding *bond)
794 {
795         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
796         struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
797         struct rlb_client_info  *new_hashtbl;
798         int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
799         int i;
800
801         spin_lock_init(&(bond_info->rx_hashtbl_lock));
802
803         new_hashtbl = kmalloc(size, GFP_KERNEL);
804         if (!new_hashtbl) {
805                 printk(KERN_ERR DRV_NAME
806                        ": %s: Error: Failed to allocate RLB hash table\n",
807                        bond->dev->name);
808                 return -1;
809         }
810         _lock_rx_hashtbl(bond);
811
812         bond_info->rx_hashtbl = new_hashtbl;
813
814         bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
815
816         for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
817                 rlb_init_table_entry(bond_info->rx_hashtbl + i);
818         }
819
820         _unlock_rx_hashtbl(bond);
821
822         /*initialize packet type*/
823         pk_type->type = cpu_to_be16(ETH_P_ARP);
824         pk_type->dev = NULL;
825         pk_type->func = rlb_arp_recv;
826
827         /* register to receive ARPs */
828         dev_add_pack(pk_type);
829
830         return 0;
831 }
832
833 static void rlb_deinitialize(struct bonding *bond)
834 {
835         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
836
837         dev_remove_pack(&(bond_info->rlb_pkt_type));
838
839         _lock_rx_hashtbl(bond);
840
841         kfree(bond_info->rx_hashtbl);
842         bond_info->rx_hashtbl = NULL;
843         bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
844
845         _unlock_rx_hashtbl(bond);
846 }
847
848 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
849 {
850         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
851         u32 curr_index;
852
853         _lock_rx_hashtbl(bond);
854
855         curr_index = bond_info->rx_hashtbl_head;
856         while (curr_index != RLB_NULL_INDEX) {
857                 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
858                 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
859                 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
860
861                 if (curr->tag && (curr->vlan_id == vlan_id)) {
862                         if (curr_index == bond_info->rx_hashtbl_head) {
863                                 bond_info->rx_hashtbl_head = next_index;
864                         }
865                         if (prev_index != RLB_NULL_INDEX) {
866                                 bond_info->rx_hashtbl[prev_index].next = next_index;
867                         }
868                         if (next_index != RLB_NULL_INDEX) {
869                                 bond_info->rx_hashtbl[next_index].prev = prev_index;
870                         }
871
872                         rlb_init_table_entry(curr);
873                 }
874
875                 curr_index = next_index;
876         }
877
878         _unlock_rx_hashtbl(bond);
879 }
880
881 /*********************** tlb/rlb shared functions *********************/
882
883 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
884 {
885         struct bonding *bond = bond_get_bond_by_slave(slave);
886         struct learning_pkt pkt;
887         int size = sizeof(struct learning_pkt);
888         int i;
889
890         memset(&pkt, 0, size);
891         memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
892         memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
893         pkt.type = cpu_to_be16(ETH_P_LOOP);
894
895         for (i = 0; i < MAX_LP_BURST; i++) {
896                 struct sk_buff *skb;
897                 char *data;
898
899                 skb = dev_alloc_skb(size);
900                 if (!skb) {
901                         return;
902                 }
903
904                 data = skb_put(skb, size);
905                 memcpy(data, &pkt, size);
906
907                 skb_reset_mac_header(skb);
908                 skb->network_header = skb->mac_header + ETH_HLEN;
909                 skb->protocol = pkt.type;
910                 skb->priority = TC_PRIO_CONTROL;
911                 skb->dev = slave->dev;
912
913                 if (!list_empty(&bond->vlan_list)) {
914                         struct vlan_entry *vlan;
915
916                         vlan = bond_next_vlan(bond,
917                                               bond->alb_info.current_alb_vlan);
918
919                         bond->alb_info.current_alb_vlan = vlan;
920                         if (!vlan) {
921                                 kfree_skb(skb);
922                                 continue;
923                         }
924
925                         skb = vlan_put_tag(skb, vlan->vlan_id);
926                         if (!skb) {
927                                 printk(KERN_ERR DRV_NAME
928                                        ": %s: Error: failed to insert VLAN tag\n",
929                                        bond->dev->name);
930                                 continue;
931                         }
932                 }
933
934                 dev_queue_xmit(skb);
935         }
936 }
937
938 /* hw is a boolean parameter that determines whether we should try and
939  * set the hw address of the device as well as the hw address of the
940  * net_device
941  */
942 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
943 {
944         struct net_device *dev = slave->dev;
945         struct sockaddr s_addr;
946
947         if (!hw) {
948                 memcpy(dev->dev_addr, addr, dev->addr_len);
949                 return 0;
950         }
951
952         /* for rlb each slave must have a unique hw mac addresses so that */
953         /* each slave will receive packets destined to a different mac */
954         memcpy(s_addr.sa_data, addr, dev->addr_len);
955         s_addr.sa_family = dev->type;
956         if (dev_set_mac_address(dev, &s_addr)) {
957                 printk(KERN_ERR DRV_NAME
958                        ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
959                        "mode requires that the base driver support setting "
960                        "the hw address also when the network device's "
961                        "interface is open\n",
962                        dev->master->name, dev->name);
963                 return -EOPNOTSUPP;
964         }
965         return 0;
966 }
967
968 /*
969  * Swap MAC addresses between two slaves.
970  *
971  * Called with RTNL held, and no other locks.
972  *
973  */
974
975 static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
976 {
977         u8 tmp_mac_addr[ETH_ALEN];
978
979         memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
980         alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
981         alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
982
983 }
984
985 /*
986  * Send learning packets after MAC address swap.
987  *
988  * Called with RTNL and no other locks
989  */
990 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
991                                 struct slave *slave2)
992 {
993         int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
994         struct slave *disabled_slave = NULL;
995
996         ASSERT_RTNL();
997
998         /* fasten the change in the switch */
999         if (SLAVE_IS_OK(slave1)) {
1000                 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
1001                 if (bond->alb_info.rlb_enabled) {
1002                         /* inform the clients that the mac address
1003                          * has changed
1004                          */
1005                         rlb_req_update_slave_clients(bond, slave1);
1006                 }
1007         } else {
1008                 disabled_slave = slave1;
1009         }
1010
1011         if (SLAVE_IS_OK(slave2)) {
1012                 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1013                 if (bond->alb_info.rlb_enabled) {
1014                         /* inform the clients that the mac address
1015                          * has changed
1016                          */
1017                         rlb_req_update_slave_clients(bond, slave2);
1018                 }
1019         } else {
1020                 disabled_slave = slave2;
1021         }
1022
1023         if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1024                 /* A disabled slave was assigned an active mac addr */
1025                 rlb_teach_disabled_mac_on_primary(bond,
1026                                                   disabled_slave->dev->dev_addr);
1027         }
1028 }
1029
1030 /**
1031  * alb_change_hw_addr_on_detach
1032  * @bond: bonding we're working on
1033  * @slave: the slave that was just detached
1034  *
1035  * We assume that @slave was already detached from the slave list.
1036  *
1037  * If @slave's permanent hw address is different both from its current
1038  * address and from @bond's address, then somewhere in the bond there's
1039  * a slave that has @slave's permanet address as its current address.
1040  * We'll make sure that that slave no longer uses @slave's permanent address.
1041  *
1042  * Caller must hold RTNL and no other locks
1043  */
1044 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1045 {
1046         int perm_curr_diff;
1047         int perm_bond_diff;
1048
1049         perm_curr_diff = memcmp(slave->perm_hwaddr,
1050                                 slave->dev->dev_addr,
1051                                 ETH_ALEN);
1052         perm_bond_diff = memcmp(slave->perm_hwaddr,
1053                                 bond->dev->dev_addr,
1054                                 ETH_ALEN);
1055
1056         if (perm_curr_diff && perm_bond_diff) {
1057                 struct slave *tmp_slave;
1058                 int i, found = 0;
1059
1060                 bond_for_each_slave(bond, tmp_slave, i) {
1061                         if (!memcmp(slave->perm_hwaddr,
1062                                     tmp_slave->dev->dev_addr,
1063                                     ETH_ALEN)) {
1064                                 found = 1;
1065                                 break;
1066                         }
1067                 }
1068
1069                 if (found) {
1070                         /* locking: needs RTNL and nothing else */
1071                         alb_swap_mac_addr(bond, slave, tmp_slave);
1072                         alb_fasten_mac_swap(bond, slave, tmp_slave);
1073                 }
1074         }
1075 }
1076
1077 /**
1078  * alb_handle_addr_collision_on_attach
1079  * @bond: bonding we're working on
1080  * @slave: the slave that was just attached
1081  *
1082  * checks uniqueness of slave's mac address and handles the case the
1083  * new slave uses the bonds mac address.
1084  *
1085  * If the permanent hw address of @slave is @bond's hw address, we need to
1086  * find a different hw address to give @slave, that isn't in use by any other
1087  * slave in the bond. This address must be, of course, one of the premanent
1088  * addresses of the other slaves.
1089  *
1090  * We go over the slave list, and for each slave there we compare its
1091  * permanent hw address with the current address of all the other slaves.
1092  * If no match was found, then we've found a slave with a permanent address
1093  * that isn't used by any other slave in the bond, so we can assign it to
1094  * @slave.
1095  *
1096  * assumption: this function is called before @slave is attached to the
1097  *             bond slave list.
1098  *
1099  * caller must hold the bond lock for write since the mac addresses are compared
1100  * and may be swapped.
1101  */
1102 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1103 {
1104         struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1105         struct slave *has_bond_addr = bond->curr_active_slave;
1106         int i, j, found = 0;
1107
1108         if (bond->slave_cnt == 0) {
1109                 /* this is the first slave */
1110                 return 0;
1111         }
1112
1113         /* if slave's mac address differs from bond's mac address
1114          * check uniqueness of slave's mac address against the other
1115          * slaves in the bond.
1116          */
1117         if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1118                 bond_for_each_slave(bond, tmp_slave1, i) {
1119                         if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1120                                     ETH_ALEN)) {
1121                                 found = 1;
1122                                 break;
1123                         }
1124                 }
1125
1126                 if (!found)
1127                         return 0;
1128
1129                 /* Try setting slave mac to bond address and fall-through
1130                    to code handling that situation below... */
1131                 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1132                                        bond->alb_info.rlb_enabled);
1133         }
1134
1135         /* The slave's address is equal to the address of the bond.
1136          * Search for a spare address in the bond for this slave.
1137          */
1138         free_mac_slave = NULL;
1139
1140         bond_for_each_slave(bond, tmp_slave1, i) {
1141                 found = 0;
1142                 bond_for_each_slave(bond, tmp_slave2, j) {
1143                         if (!memcmp(tmp_slave1->perm_hwaddr,
1144                                     tmp_slave2->dev->dev_addr,
1145                                     ETH_ALEN)) {
1146                                 found = 1;
1147                                 break;
1148                         }
1149                 }
1150
1151                 if (!found) {
1152                         /* no slave has tmp_slave1's perm addr
1153                          * as its curr addr
1154                          */
1155                         free_mac_slave = tmp_slave1;
1156                         break;
1157                 }
1158
1159                 if (!has_bond_addr) {
1160                         if (!memcmp(tmp_slave1->dev->dev_addr,
1161                                     bond->dev->dev_addr,
1162                                     ETH_ALEN)) {
1163
1164                                 has_bond_addr = tmp_slave1;
1165                         }
1166                 }
1167         }
1168
1169         if (free_mac_slave) {
1170                 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1171                                        bond->alb_info.rlb_enabled);
1172
1173                 printk(KERN_WARNING DRV_NAME
1174                        ": %s: Warning: the hw address of slave %s is in use by "
1175                        "the bond; giving it the hw address of %s\n",
1176                        bond->dev->name, slave->dev->name, free_mac_slave->dev->name);
1177
1178         } else if (has_bond_addr) {
1179                 printk(KERN_ERR DRV_NAME
1180                        ": %s: Error: the hw address of slave %s is in use by the "
1181                        "bond; couldn't find a slave with a free hw address to "
1182                        "give it (this should not have happened)\n",
1183                        bond->dev->name, slave->dev->name);
1184                 return -EFAULT;
1185         }
1186
1187         return 0;
1188 }
1189
1190 /**
1191  * alb_set_mac_address
1192  * @bond:
1193  * @addr:
1194  *
1195  * In TLB mode all slaves are configured to the bond's hw address, but set
1196  * their dev_addr field to different addresses (based on their permanent hw
1197  * addresses).
1198  *
1199  * For each slave, this function sets the interface to the new address and then
1200  * changes its dev_addr field to its previous value.
1201  *
1202  * Unwinding assumes bond's mac address has not yet changed.
1203  */
1204 static int alb_set_mac_address(struct bonding *bond, void *addr)
1205 {
1206         struct sockaddr sa;
1207         struct slave *slave, *stop_at;
1208         char tmp_addr[ETH_ALEN];
1209         int res;
1210         int i;
1211
1212         if (bond->alb_info.rlb_enabled) {
1213                 return 0;
1214         }
1215
1216         bond_for_each_slave(bond, slave, i) {
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         return 0;
1230
1231 unwind:
1232         memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1233         sa.sa_family = bond->dev->type;
1234
1235         /* unwind from head to the slave that failed */
1236         stop_at = slave;
1237         bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1238                 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1239                 dev_set_mac_address(slave->dev, &sa);
1240                 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1241         }
1242
1243         return res;
1244 }
1245
1246 /************************ exported alb funcions ************************/
1247
1248 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1249 {
1250         int res;
1251
1252         res = tlb_initialize(bond);
1253         if (res) {
1254                 return res;
1255         }
1256
1257         if (rlb_enabled) {
1258                 bond->alb_info.rlb_enabled = 1;
1259                 /* initialize rlb */
1260                 res = rlb_initialize(bond);
1261                 if (res) {
1262                         tlb_deinitialize(bond);
1263                         return res;
1264                 }
1265         } else {
1266                 bond->alb_info.rlb_enabled = 0;
1267         }
1268
1269         return 0;
1270 }
1271
1272 void bond_alb_deinitialize(struct bonding *bond)
1273 {
1274         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1275
1276         tlb_deinitialize(bond);
1277
1278         if (bond_info->rlb_enabled) {
1279                 rlb_deinitialize(bond);
1280         }
1281 }
1282
1283 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1284 {
1285         struct bonding *bond = netdev_priv(bond_dev);
1286         struct ethhdr *eth_data;
1287         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1288         struct slave *tx_slave = NULL;
1289         static const __be32 ip_bcast = htonl(0xffffffff);
1290         int hash_size = 0;
1291         int do_tx_balance = 1;
1292         u32 hash_index = 0;
1293         const u8 *hash_start = NULL;
1294         int res = 1;
1295         struct ipv6hdr *ip6hdr;
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                 /* IPv6 doesn't really use broadcast mac address, but leave
1326                  * that here just in case.
1327                  */
1328                 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1329                         do_tx_balance = 0;
1330                         break;
1331                 }
1332
1333                 /* IPv6 uses all-nodes multicast as an equivalent to
1334                  * broadcasts in IPv4.
1335                  */
1336                 if (memcmp(eth_data->h_dest, mac_v6_allmcast, ETH_ALEN) == 0) {
1337                         do_tx_balance = 0;
1338                         break;
1339                 }
1340
1341                 /* Additianally, DAD probes should not be tx-balanced as that
1342                  * will lead to false positives for duplicate addresses and
1343                  * prevent address configuration from working.
1344                  */
1345                 ip6hdr = ipv6_hdr(skb);
1346                 if (ipv6_addr_any(&ip6hdr->saddr)) {
1347                         do_tx_balance = 0;
1348                         break;
1349                 }
1350
1351                 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1352                 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1353                 break;
1354         case ETH_P_IPX:
1355                 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1356                         /* something is wrong with this packet */
1357                         do_tx_balance = 0;
1358                         break;
1359                 }
1360
1361                 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1362                         /* The only protocol worth balancing in
1363                          * this family since it has an "ARP" like
1364                          * mechanism
1365                          */
1366                         do_tx_balance = 0;
1367                         break;
1368                 }
1369
1370                 hash_start = (char*)eth_data->h_dest;
1371                 hash_size = ETH_ALEN;
1372                 break;
1373         case ETH_P_ARP:
1374                 do_tx_balance = 0;
1375                 if (bond_info->rlb_enabled) {
1376                         tx_slave = rlb_arp_xmit(skb, bond);
1377                 }
1378                 break;
1379         default:
1380                 do_tx_balance = 0;
1381                 break;
1382         }
1383
1384         if (do_tx_balance) {
1385                 hash_index = _simple_hash(hash_start, hash_size);
1386                 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1387         }
1388
1389         if (!tx_slave) {
1390                 /* unbalanced or unassigned, send through primary */
1391                 tx_slave = bond->curr_active_slave;
1392                 bond_info->unbalanced_load += skb->len;
1393         }
1394
1395         if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1396                 if (tx_slave != bond->curr_active_slave) {
1397                         memcpy(eth_data->h_source,
1398                                tx_slave->dev->dev_addr,
1399                                ETH_ALEN);
1400                 }
1401
1402                 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1403         } else {
1404                 if (tx_slave) {
1405                         tlb_clear_slave(bond, tx_slave, 0);
1406                 }
1407         }
1408
1409 out:
1410         if (res) {
1411                 /* no suitable interface, frame not sent */
1412                 dev_kfree_skb(skb);
1413         }
1414         read_unlock(&bond->curr_slave_lock);
1415         read_unlock(&bond->lock);
1416         return 0;
1417 }
1418
1419 void bond_alb_monitor(struct work_struct *work)
1420 {
1421         struct bonding *bond = container_of(work, struct bonding,
1422                                             alb_work.work);
1423         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1424         struct slave *slave;
1425         int i;
1426
1427         read_lock(&bond->lock);
1428
1429         if (bond->kill_timers) {
1430                 goto out;
1431         }
1432
1433         if (bond->slave_cnt == 0) {
1434                 bond_info->tx_rebalance_counter = 0;
1435                 bond_info->lp_counter = 0;
1436                 goto re_arm;
1437         }
1438
1439         bond_info->tx_rebalance_counter++;
1440         bond_info->lp_counter++;
1441
1442         /* send learning packets */
1443         if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1444                 /* change of curr_active_slave involves swapping of mac addresses.
1445                  * in order to avoid this swapping from happening while
1446                  * sending the learning packets, the curr_slave_lock must be held for
1447                  * read.
1448                  */
1449                 read_lock(&bond->curr_slave_lock);
1450
1451                 bond_for_each_slave(bond, slave, i) {
1452                         alb_send_learning_packets(slave, slave->dev->dev_addr);
1453                 }
1454
1455                 read_unlock(&bond->curr_slave_lock);
1456
1457                 bond_info->lp_counter = 0;
1458         }
1459
1460         /* rebalance tx traffic */
1461         if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1462
1463                 read_lock(&bond->curr_slave_lock);
1464
1465                 bond_for_each_slave(bond, slave, i) {
1466                         tlb_clear_slave(bond, slave, 1);
1467                         if (slave == bond->curr_active_slave) {
1468                                 SLAVE_TLB_INFO(slave).load =
1469                                         bond_info->unbalanced_load /
1470                                                 BOND_TLB_REBALANCE_INTERVAL;
1471                                 bond_info->unbalanced_load = 0;
1472                         }
1473                 }
1474
1475                 read_unlock(&bond->curr_slave_lock);
1476
1477                 bond_info->tx_rebalance_counter = 0;
1478         }
1479
1480         /* handle rlb stuff */
1481         if (bond_info->rlb_enabled) {
1482                 if (bond_info->primary_is_promisc &&
1483                     (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1484
1485                         /*
1486                          * dev_set_promiscuity requires rtnl and
1487                          * nothing else.
1488                          */
1489                         read_unlock(&bond->lock);
1490                         rtnl_lock();
1491
1492                         bond_info->rlb_promisc_timeout_counter = 0;
1493
1494                         /* If the primary was set to promiscuous mode
1495                          * because a slave was disabled then
1496                          * it can now leave promiscuous mode.
1497                          */
1498                         dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1499                         bond_info->primary_is_promisc = 0;
1500
1501                         rtnl_unlock();
1502                         read_lock(&bond->lock);
1503                 }
1504
1505                 if (bond_info->rlb_rebalance) {
1506                         bond_info->rlb_rebalance = 0;
1507                         rlb_rebalance(bond);
1508                 }
1509
1510                 /* check if clients need updating */
1511                 if (bond_info->rx_ntt) {
1512                         if (bond_info->rlb_update_delay_counter) {
1513                                 --bond_info->rlb_update_delay_counter;
1514                         } else {
1515                                 rlb_update_rx_clients(bond);
1516                                 if (bond_info->rlb_update_retry_counter) {
1517                                         --bond_info->rlb_update_retry_counter;
1518                                 } else {
1519                                         bond_info->rx_ntt = 0;
1520                                 }
1521                         }
1522                 }
1523         }
1524
1525 re_arm:
1526         queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1527 out:
1528         read_unlock(&bond->lock);
1529 }
1530
1531 /* assumption: called before the slave is attached to the bond
1532  * and not locked by the bond lock
1533  */
1534 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1535 {
1536         int res;
1537
1538         res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1539                                      bond->alb_info.rlb_enabled);
1540         if (res) {
1541                 return res;
1542         }
1543
1544         /* caller must hold the bond lock for write since the mac addresses
1545          * are compared and may be swapped.
1546          */
1547         read_lock(&bond->lock);
1548
1549         res = alb_handle_addr_collision_on_attach(bond, slave);
1550
1551         read_unlock(&bond->lock);
1552
1553         if (res) {
1554                 return res;
1555         }
1556
1557         tlb_init_slave(slave);
1558
1559         /* order a rebalance ASAP */
1560         bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1561
1562         if (bond->alb_info.rlb_enabled) {
1563                 bond->alb_info.rlb_rebalance = 1;
1564         }
1565
1566         return 0;
1567 }
1568
1569 /*
1570  * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1571  * if necessary.
1572  *
1573  * Caller must hold RTNL and no other locks
1574  */
1575 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1576 {
1577         if (bond->slave_cnt > 1) {
1578                 alb_change_hw_addr_on_detach(bond, slave);
1579         }
1580
1581         tlb_clear_slave(bond, slave, 0);
1582
1583         if (bond->alb_info.rlb_enabled) {
1584                 bond->alb_info.next_rx_slave = NULL;
1585                 rlb_clear_slave(bond, slave);
1586         }
1587 }
1588
1589 /* Caller must hold bond lock for read */
1590 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1591 {
1592         struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1593
1594         if (link == BOND_LINK_DOWN) {
1595                 tlb_clear_slave(bond, slave, 0);
1596                 if (bond->alb_info.rlb_enabled) {
1597                         rlb_clear_slave(bond, slave);
1598                 }
1599         } else if (link == BOND_LINK_UP) {
1600                 /* order a rebalance ASAP */
1601                 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1602                 if (bond->alb_info.rlb_enabled) {
1603                         bond->alb_info.rlb_rebalance = 1;
1604                         /* If the updelay module parameter is smaller than the
1605                          * forwarding delay of the switch the rebalance will
1606                          * not work because the rebalance arp replies will
1607                          * not be forwarded to the clients..
1608                          */
1609                 }
1610         }
1611 }
1612
1613 /**
1614  * bond_alb_handle_active_change - assign new curr_active_slave
1615  * @bond: our bonding struct
1616  * @new_slave: new slave to assign
1617  *
1618  * Set the bond->curr_active_slave to @new_slave and handle
1619  * mac address swapping and promiscuity changes as needed.
1620  *
1621  * If new_slave is NULL, caller must hold curr_slave_lock or
1622  * bond->lock for write.
1623  *
1624  * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1625  * read and curr_slave_lock for write.  Processing here may sleep, so
1626  * no other locks may be held.
1627  */
1628 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1629         __releases(&bond->curr_slave_lock)
1630         __releases(&bond->lock)
1631         __acquires(&bond->lock)
1632         __acquires(&bond->curr_slave_lock)
1633 {
1634         struct slave *swap_slave;
1635         int i;
1636
1637         if (bond->curr_active_slave == new_slave) {
1638                 return;
1639         }
1640
1641         if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1642                 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1643                 bond->alb_info.primary_is_promisc = 0;
1644                 bond->alb_info.rlb_promisc_timeout_counter = 0;
1645         }
1646
1647         swap_slave = bond->curr_active_slave;
1648         bond->curr_active_slave = new_slave;
1649
1650         if (!new_slave || (bond->slave_cnt == 0)) {
1651                 return;
1652         }
1653
1654         /* set the new curr_active_slave to the bonds mac address
1655          * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1656          */
1657         if (!swap_slave) {
1658                 struct slave *tmp_slave;
1659                 /* find slave that is holding the bond's mac address */
1660                 bond_for_each_slave(bond, tmp_slave, i) {
1661                         if (!memcmp(tmp_slave->dev->dev_addr,
1662                                     bond->dev->dev_addr, ETH_ALEN)) {
1663                                 swap_slave = tmp_slave;
1664                                 break;
1665                         }
1666                 }
1667         }
1668
1669         /*
1670          * Arrange for swap_slave and new_slave to temporarily be
1671          * ignored so we can mess with their MAC addresses without
1672          * fear of interference from transmit activity.
1673          */
1674         if (swap_slave) {
1675                 tlb_clear_slave(bond, swap_slave, 1);
1676         }
1677         tlb_clear_slave(bond, new_slave, 1);
1678
1679         write_unlock_bh(&bond->curr_slave_lock);
1680         read_unlock(&bond->lock);
1681
1682         ASSERT_RTNL();
1683
1684         /* curr_active_slave must be set before calling alb_swap_mac_addr */
1685         if (swap_slave) {
1686                 /* swap mac address */
1687                 alb_swap_mac_addr(bond, swap_slave, new_slave);
1688         } else {
1689                 /* set the new_slave to the bond mac address */
1690                 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1691                                        bond->alb_info.rlb_enabled);
1692         }
1693
1694         if (swap_slave) {
1695                 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1696                 read_lock(&bond->lock);
1697         } else {
1698                 read_lock(&bond->lock);
1699                 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1700         }
1701
1702         write_lock_bh(&bond->curr_slave_lock);
1703 }
1704
1705 /*
1706  * Called with RTNL
1707  */
1708 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1709         __acquires(&bond->lock)
1710         __releases(&bond->lock)
1711 {
1712         struct bonding *bond = netdev_priv(bond_dev);
1713         struct sockaddr *sa = addr;
1714         struct slave *slave, *swap_slave;
1715         int res;
1716         int i;
1717
1718         if (!is_valid_ether_addr(sa->sa_data)) {
1719                 return -EADDRNOTAVAIL;
1720         }
1721
1722         res = alb_set_mac_address(bond, addr);
1723         if (res) {
1724                 return res;
1725         }
1726
1727         memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1728
1729         /* If there is no curr_active_slave there is nothing else to do.
1730          * Otherwise we'll need to pass the new address to it and handle
1731          * duplications.
1732          */
1733         if (!bond->curr_active_slave) {
1734                 return 0;
1735         }
1736
1737         swap_slave = NULL;
1738
1739         bond_for_each_slave(bond, slave, i) {
1740                 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1741                         swap_slave = slave;
1742                         break;
1743                 }
1744         }
1745
1746         if (swap_slave) {
1747                 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1748                 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1749         } else {
1750                 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1751                                        bond->alb_info.rlb_enabled);
1752
1753                 read_lock(&bond->lock);
1754                 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1755                 if (bond->alb_info.rlb_enabled) {
1756                         /* inform clients mac address has changed */
1757                         rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1758                 }
1759                 read_unlock(&bond->lock);
1760         }
1761
1762         return 0;
1763 }
1764
1765 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1766 {
1767         if (bond->alb_info.current_alb_vlan &&
1768             (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1769                 bond->alb_info.current_alb_vlan = NULL;
1770         }
1771
1772         if (bond->alb_info.rlb_enabled) {
1773                 rlb_clear_vlan(bond, vlan_id);
1774         }
1775 }
1776