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