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