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