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