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