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