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