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