blob: 19e829b567d0e7735908b5562e8e0a9214803896 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
John W. Linville6b38aef2005-07-28 15:00:15 -04001109 if (!found)
1110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
John W. Linville6b38aef2005-07-28 15:00:15 -04001112 /* Try setting slave mac to bond address and fall-through
1113 to code handling that situation below... */
1114 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1115 bond->alb_info.rlb_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
1118 /* The slave's address is equal to the address of the bond.
1119 * Search for a spare address in the bond for this slave.
1120 */
1121 free_mac_slave = NULL;
1122
1123 bond_for_each_slave(bond, tmp_slave1, i) {
1124 found = 0;
1125 bond_for_each_slave(bond, tmp_slave2, j) {
1126 if (!memcmp(tmp_slave1->perm_hwaddr,
1127 tmp_slave2->dev->dev_addr,
1128 ETH_ALEN)) {
1129 found = 1;
1130 break;
1131 }
1132 }
1133
1134 if (!found) {
1135 /* no slave has tmp_slave1's perm addr
1136 * as its curr addr
1137 */
1138 free_mac_slave = tmp_slave1;
1139 break;
1140 }
1141
1142 if (!has_bond_addr) {
1143 if (!memcmp(tmp_slave1->dev->dev_addr,
1144 bond->dev->dev_addr,
1145 ETH_ALEN)) {
1146
1147 has_bond_addr = tmp_slave1;
1148 }
1149 }
1150 }
1151
1152 if (free_mac_slave) {
1153 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1154 bond->alb_info.rlb_enabled);
1155
1156 printk(KERN_WARNING DRV_NAME
1157 ": Warning: the hw address of slave %s is in use by "
1158 "the bond; giving it the hw address of %s\n",
1159 slave->dev->name, free_mac_slave->dev->name);
1160
1161 } else if (has_bond_addr) {
1162 printk(KERN_ERR DRV_NAME
1163 ": Error: the hw address of slave %s is in use by the "
1164 "bond; couldn't find a slave with a free hw address to "
1165 "give it (this should not have happened)\n",
1166 slave->dev->name);
1167 return -EFAULT;
1168 }
1169
1170 return 0;
1171}
1172
1173/**
1174 * alb_set_mac_address
1175 * @bond:
1176 * @addr:
1177 *
1178 * In TLB mode all slaves are configured to the bond's hw address, but set
1179 * their dev_addr field to different addresses (based on their permanent hw
1180 * addresses).
1181 *
1182 * For each slave, this function sets the interface to the new address and then
1183 * changes its dev_addr field to its previous value.
1184 *
1185 * Unwinding assumes bond's mac address has not yet changed.
1186 */
1187static int alb_set_mac_address(struct bonding *bond, void *addr)
1188{
1189 struct sockaddr sa;
1190 struct slave *slave, *stop_at;
1191 char tmp_addr[ETH_ALEN];
1192 int res;
1193 int i;
1194
1195 if (bond->alb_info.rlb_enabled) {
1196 return 0;
1197 }
1198
1199 bond_for_each_slave(bond, slave, i) {
1200 if (slave->dev->set_mac_address == NULL) {
1201 res = -EOPNOTSUPP;
1202 goto unwind;
1203 }
1204
1205 /* save net_device's current hw address */
1206 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1207
1208 res = dev_set_mac_address(slave->dev, addr);
1209
1210 /* restore net_device's hw address */
1211 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1212
1213 if (res) {
1214 goto unwind;
1215 }
1216 }
1217
1218 return 0;
1219
1220unwind:
1221 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1222 sa.sa_family = bond->dev->type;
1223
1224 /* unwind from head to the slave that failed */
1225 stop_at = slave;
1226 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1227 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1228 dev_set_mac_address(slave->dev, &sa);
1229 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1230 }
1231
1232 return res;
1233}
1234
1235/************************ exported alb funcions ************************/
1236
1237int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1238{
1239 int res;
1240
1241 res = tlb_initialize(bond);
1242 if (res) {
1243 return res;
1244 }
1245
1246 if (rlb_enabled) {
1247 bond->alb_info.rlb_enabled = 1;
1248 /* initialize rlb */
1249 res = rlb_initialize(bond);
1250 if (res) {
1251 tlb_deinitialize(bond);
1252 return res;
1253 }
1254 }
1255
1256 return 0;
1257}
1258
1259void bond_alb_deinitialize(struct bonding *bond)
1260{
1261 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1262
1263 tlb_deinitialize(bond);
1264
1265 if (bond_info->rlb_enabled) {
1266 rlb_deinitialize(bond);
1267 }
1268}
1269
1270int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1271{
1272 struct bonding *bond = bond_dev->priv;
1273 struct ethhdr *eth_data;
1274 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1275 struct slave *tx_slave = NULL;
1276 static u32 ip_bcast = 0xffffffff;
1277 int hash_size = 0;
1278 int do_tx_balance = 1;
1279 u32 hash_index = 0;
1280 u8 *hash_start = NULL;
1281 int res = 1;
1282
1283 skb->mac.raw = (unsigned char *)skb->data;
1284 eth_data = eth_hdr(skb);
1285
1286 /* make sure that the curr_active_slave and the slaves list do
1287 * not change during tx
1288 */
1289 read_lock(&bond->lock);
1290 read_lock(&bond->curr_slave_lock);
1291
1292 if (!BOND_IS_OK(bond)) {
1293 goto out;
1294 }
1295
1296 switch (ntohs(skb->protocol)) {
1297 case ETH_P_IP:
1298 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
1299 (skb->nh.iph->daddr == ip_bcast) ||
1300 (skb->nh.iph->protocol == IPPROTO_IGMP)) {
1301 do_tx_balance = 0;
1302 break;
1303 }
1304 hash_start = (char*)&(skb->nh.iph->daddr);
1305 hash_size = sizeof(skb->nh.iph->daddr);
1306 break;
1307 case ETH_P_IPV6:
1308 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1309 do_tx_balance = 0;
1310 break;
1311 }
1312
1313 hash_start = (char*)&(skb->nh.ipv6h->daddr);
1314 hash_size = sizeof(skb->nh.ipv6h->daddr);
1315 break;
1316 case ETH_P_IPX:
1317 if (ipx_hdr(skb)->ipx_checksum !=
1318 __constant_htons(IPX_NO_CHECKSUM)) {
1319 /* something is wrong with this packet */
1320 do_tx_balance = 0;
1321 break;
1322 }
1323
1324 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1325 /* The only protocol worth balancing in
1326 * this family since it has an "ARP" like
1327 * mechanism
1328 */
1329 do_tx_balance = 0;
1330 break;
1331 }
1332
1333 hash_start = (char*)eth_data->h_dest;
1334 hash_size = ETH_ALEN;
1335 break;
1336 case ETH_P_ARP:
1337 do_tx_balance = 0;
1338 if (bond_info->rlb_enabled) {
1339 tx_slave = rlb_arp_xmit(skb, bond);
1340 }
1341 break;
1342 default:
1343 do_tx_balance = 0;
1344 break;
1345 }
1346
1347 if (do_tx_balance) {
1348 hash_index = _simple_hash(hash_start, hash_size);
1349 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1350 }
1351
1352 if (!tx_slave) {
1353 /* unbalanced or unassigned, send through primary */
1354 tx_slave = bond->curr_active_slave;
1355 bond_info->unbalanced_load += skb->len;
1356 }
1357
1358 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1359 if (tx_slave != bond->curr_active_slave) {
1360 memcpy(eth_data->h_source,
1361 tx_slave->dev->dev_addr,
1362 ETH_ALEN);
1363 }
1364
1365 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1366 } else {
1367 if (tx_slave) {
1368 tlb_clear_slave(bond, tx_slave, 0);
1369 }
1370 }
1371
1372out:
1373 if (res) {
1374 /* no suitable interface, frame not sent */
1375 dev_kfree_skb(skb);
1376 }
1377 read_unlock(&bond->curr_slave_lock);
1378 read_unlock(&bond->lock);
1379 return 0;
1380}
1381
1382void bond_alb_monitor(struct bonding *bond)
1383{
1384 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1385 struct slave *slave;
1386 int i;
1387
1388 read_lock(&bond->lock);
1389
1390 if (bond->kill_timers) {
1391 goto out;
1392 }
1393
1394 if (bond->slave_cnt == 0) {
1395 bond_info->tx_rebalance_counter = 0;
1396 bond_info->lp_counter = 0;
1397 goto re_arm;
1398 }
1399
1400 bond_info->tx_rebalance_counter++;
1401 bond_info->lp_counter++;
1402
1403 /* send learning packets */
1404 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1405 /* change of curr_active_slave involves swapping of mac addresses.
1406 * in order to avoid this swapping from happening while
1407 * sending the learning packets, the curr_slave_lock must be held for
1408 * read.
1409 */
1410 read_lock(&bond->curr_slave_lock);
1411
1412 bond_for_each_slave(bond, slave, i) {
1413 alb_send_learning_packets(slave,slave->dev->dev_addr);
1414 }
1415
1416 read_unlock(&bond->curr_slave_lock);
1417
1418 bond_info->lp_counter = 0;
1419 }
1420
1421 /* rebalance tx traffic */
1422 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1423
1424 read_lock(&bond->curr_slave_lock);
1425
1426 bond_for_each_slave(bond, slave, i) {
1427 tlb_clear_slave(bond, slave, 1);
1428 if (slave == bond->curr_active_slave) {
1429 SLAVE_TLB_INFO(slave).load =
1430 bond_info->unbalanced_load /
1431 BOND_TLB_REBALANCE_INTERVAL;
1432 bond_info->unbalanced_load = 0;
1433 }
1434 }
1435
1436 read_unlock(&bond->curr_slave_lock);
1437
1438 bond_info->tx_rebalance_counter = 0;
1439 }
1440
1441 /* handle rlb stuff */
1442 if (bond_info->rlb_enabled) {
1443 /* the following code changes the promiscuity of the
1444 * the curr_active_slave. It needs to be locked with a
1445 * write lock to protect from other code that also
1446 * sets the promiscuity.
1447 */
1448 write_lock(&bond->curr_slave_lock);
1449
1450 if (bond_info->primary_is_promisc &&
1451 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1452
1453 bond_info->rlb_promisc_timeout_counter = 0;
1454
1455 /* If the primary was set to promiscuous mode
1456 * because a slave was disabled then
1457 * it can now leave promiscuous mode.
1458 */
1459 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1460 bond_info->primary_is_promisc = 0;
1461 }
1462
1463 write_unlock(&bond->curr_slave_lock);
1464
1465 if (bond_info->rlb_rebalance) {
1466 bond_info->rlb_rebalance = 0;
1467 rlb_rebalance(bond);
1468 }
1469
1470 /* check if clients need updating */
1471 if (bond_info->rx_ntt) {
1472 if (bond_info->rlb_update_delay_counter) {
1473 --bond_info->rlb_update_delay_counter;
1474 } else {
1475 rlb_update_rx_clients(bond);
1476 if (bond_info->rlb_update_retry_counter) {
1477 --bond_info->rlb_update_retry_counter;
1478 } else {
1479 bond_info->rx_ntt = 0;
1480 }
1481 }
1482 }
1483 }
1484
1485re_arm:
1486 mod_timer(&(bond_info->alb_timer), jiffies + alb_delta_in_ticks);
1487out:
1488 read_unlock(&bond->lock);
1489}
1490
1491/* assumption: called before the slave is attached to the bond
1492 * and not locked by the bond lock
1493 */
1494int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1495{
1496 int res;
1497
1498 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1499 bond->alb_info.rlb_enabled);
1500 if (res) {
1501 return res;
1502 }
1503
1504 /* caller must hold the bond lock for write since the mac addresses
1505 * are compared and may be swapped.
1506 */
1507 write_lock_bh(&bond->lock);
1508
1509 res = alb_handle_addr_collision_on_attach(bond, slave);
1510
1511 write_unlock_bh(&bond->lock);
1512
1513 if (res) {
1514 return res;
1515 }
1516
1517 tlb_init_slave(slave);
1518
1519 /* order a rebalance ASAP */
1520 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1521
1522 if (bond->alb_info.rlb_enabled) {
1523 bond->alb_info.rlb_rebalance = 1;
1524 }
1525
1526 return 0;
1527}
1528
1529/* Caller must hold bond lock for write */
1530void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1531{
1532 if (bond->slave_cnt > 1) {
1533 alb_change_hw_addr_on_detach(bond, slave);
1534 }
1535
1536 tlb_clear_slave(bond, slave, 0);
1537
1538 if (bond->alb_info.rlb_enabled) {
1539 bond->alb_info.next_rx_slave = NULL;
1540 rlb_clear_slave(bond, slave);
1541 }
1542}
1543
1544/* Caller must hold bond lock for read */
1545void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1546{
1547 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1548
1549 if (link == BOND_LINK_DOWN) {
1550 tlb_clear_slave(bond, slave, 0);
1551 if (bond->alb_info.rlb_enabled) {
1552 rlb_clear_slave(bond, slave);
1553 }
1554 } else if (link == BOND_LINK_UP) {
1555 /* order a rebalance ASAP */
1556 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1557 if (bond->alb_info.rlb_enabled) {
1558 bond->alb_info.rlb_rebalance = 1;
1559 /* If the updelay module parameter is smaller than the
1560 * forwarding delay of the switch the rebalance will
1561 * not work because the rebalance arp replies will
1562 * not be forwarded to the clients..
1563 */
1564 }
1565 }
1566}
1567
1568/**
1569 * bond_alb_handle_active_change - assign new curr_active_slave
1570 * @bond: our bonding struct
1571 * @new_slave: new slave to assign
1572 *
1573 * Set the bond->curr_active_slave to @new_slave and handle
1574 * mac address swapping and promiscuity changes as needed.
1575 *
1576 * Caller must hold bond curr_slave_lock for write (or bond lock for write)
1577 */
1578void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1579{
1580 struct slave *swap_slave;
1581 int i;
1582
1583 if (bond->curr_active_slave == new_slave) {
1584 return;
1585 }
1586
1587 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1588 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1589 bond->alb_info.primary_is_promisc = 0;
1590 bond->alb_info.rlb_promisc_timeout_counter = 0;
1591 }
1592
1593 swap_slave = bond->curr_active_slave;
1594 bond->curr_active_slave = new_slave;
1595
1596 if (!new_slave || (bond->slave_cnt == 0)) {
1597 return;
1598 }
1599
1600 /* set the new curr_active_slave to the bonds mac address
1601 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1602 */
1603 if (!swap_slave) {
1604 struct slave *tmp_slave;
1605 /* find slave that is holding the bond's mac address */
1606 bond_for_each_slave(bond, tmp_slave, i) {
1607 if (!memcmp(tmp_slave->dev->dev_addr,
1608 bond->dev->dev_addr, ETH_ALEN)) {
1609 swap_slave = tmp_slave;
1610 break;
1611 }
1612 }
1613 }
1614
1615 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1616 if (swap_slave) {
1617 /* swap mac address */
1618 alb_swap_mac_addr(bond, swap_slave, new_slave);
1619 } else {
1620 /* set the new_slave to the bond mac address */
1621 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1622 bond->alb_info.rlb_enabled);
1623 /* fasten bond mac on new current slave */
1624 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1625 }
1626}
1627
1628int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1629{
1630 struct bonding *bond = bond_dev->priv;
1631 struct sockaddr *sa = addr;
1632 struct slave *slave, *swap_slave;
1633 int res;
1634 int i;
1635
1636 if (!is_valid_ether_addr(sa->sa_data)) {
1637 return -EADDRNOTAVAIL;
1638 }
1639
1640 res = alb_set_mac_address(bond, addr);
1641 if (res) {
1642 return res;
1643 }
1644
1645 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1646
1647 /* If there is no curr_active_slave there is nothing else to do.
1648 * Otherwise we'll need to pass the new address to it and handle
1649 * duplications.
1650 */
1651 if (!bond->curr_active_slave) {
1652 return 0;
1653 }
1654
1655 swap_slave = NULL;
1656
1657 bond_for_each_slave(bond, slave, i) {
1658 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1659 swap_slave = slave;
1660 break;
1661 }
1662 }
1663
1664 if (swap_slave) {
1665 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
1666 } else {
1667 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1668 bond->alb_info.rlb_enabled);
1669
1670 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1671 if (bond->alb_info.rlb_enabled) {
1672 /* inform clients mac address has changed */
1673 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1674 }
1675 }
1676
1677 return 0;
1678}
1679
1680void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1681{
1682 if (bond->alb_info.current_alb_vlan &&
1683 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1684 bond->alb_info.current_alb_vlan = NULL;
1685 }
1686
1687 if (bond->alb_info.rlb_enabled) {
1688 rlb_clear_vlan(bond, vlan_id);
1689 }
1690}
1691