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