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