blob: 3f58c3d0b7106ae02452ff77f1e41c28ae2ded67 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23//#define BONDING_DEBUG 1
24
25#include <linux/skbuff.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/pkt_sched.h>
29#include <linux/spinlock.h>
30#include <linux/slab.h>
31#include <linux/timer.h>
32#include <linux/ip.h>
33#include <linux/ipv6.h>
34#include <linux/if_arp.h>
35#include <linux/if_ether.h>
36#include <linux/if_bonding.h>
37#include <linux/if_vlan.h>
38#include <linux/in.h>
39#include <net/ipx.h>
40#include <net/arp.h>
41#include <asm/byteorder.h>
42#include "bonding.h"
43#include "bond_alb.h"
44
45
46#define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
47#define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
48 * Used for division - never set
49 * to zero !!!
50 */
51#define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
52 * learning packets to the switch
53 */
54
55#define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
56 * ALB_TIMER_TICKS_PER_SEC)
57
58#define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
59 * ALB_TIMER_TICKS_PER_SEC)
60
61#define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
62 * Note that this value MUST NOT be smaller
63 * because the key hash table is BYTE wide !
64 */
65
66
67#define TLB_NULL_INDEX 0xffffffff
68#define MAX_LP_BURST 3
69
70/* rlb defs */
71#define RLB_HASH_TABLE_SIZE 256
72#define RLB_NULL_INDEX 0xffffffff
73#define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
74#define RLB_ARP_BURST_SIZE 2
75#define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
76 * rebalance interval (5 min).
77 */
78/* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
79 * promiscuous after failover
80 */
81#define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
82
83static const u8 mac_bcast[ETH_ALEN] = {0xff,0xff,0xff,0xff,0xff,0xff};
84static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
85
86#pragma pack(1)
87struct learning_pkt {
88 u8 mac_dst[ETH_ALEN];
89 u8 mac_src[ETH_ALEN];
Al Virod3bb52b2007-08-22 20:06:58 -040090 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 u8 padding[ETH_ZLEN - ETH_HLEN];
92};
93
94struct arp_pkt {
Al Virod3bb52b2007-08-22 20:06:58 -040095 __be16 hw_addr_space;
96 __be16 prot_addr_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 u8 hw_addr_len;
98 u8 prot_addr_len;
Al Virod3bb52b2007-08-22 20:06:58 -040099 __be16 op_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 u8 mac_src[ETH_ALEN]; /* sender hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -0400101 __be32 ip_src; /* sender IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 u8 mac_dst[ETH_ALEN]; /* target hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -0400103 __be32 ip_dst; /* target IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105#pragma pack()
106
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300107static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
108{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700109 return (struct arp_pkt *)skb_network_header(skb);
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* Forward declaration */
113static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
114
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700115static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 int i;
118 u8 hash = 0;
119
120 for (i = 0; i < hash_size; i++) {
121 hash ^= hash_start[i];
122 }
123
124 return hash;
125}
126
127/*********************** tlb specific functions ***************************/
128
129static inline void _lock_tx_hashtbl(struct bonding *bond)
130{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700131 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static inline void _unlock_tx_hashtbl(struct bonding *bond)
135{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700136 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/* Caller must hold tx_hashtbl lock */
140static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
141{
142 if (save_load) {
143 entry->load_history = 1 + entry->tx_bytes /
144 BOND_TLB_REBALANCE_INTERVAL;
145 entry->tx_bytes = 0;
146 }
147
148 entry->tx_slave = NULL;
149 entry->next = TLB_NULL_INDEX;
150 entry->prev = TLB_NULL_INDEX;
151}
152
153static inline void tlb_init_slave(struct slave *slave)
154{
155 SLAVE_TLB_INFO(slave).load = 0;
156 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
157}
158
159/* Caller must hold bond lock for read */
160static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
161{
162 struct tlb_client_info *tx_hash_table;
163 u32 index;
164
165 _lock_tx_hashtbl(bond);
166
167 /* clear slave from tx_hashtbl */
168 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
169
170 index = SLAVE_TLB_INFO(slave).head;
171 while (index != TLB_NULL_INDEX) {
172 u32 next_index = tx_hash_table[index].next;
173 tlb_init_table_entry(&tx_hash_table[index], save_load);
174 index = next_index;
175 }
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 tlb_init_slave(slave);
Jay Vosburgh5af47b22006-01-09 12:14:00 -0800178
179 _unlock_tx_hashtbl(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/* Must be called before starting the monitor timer */
183static int tlb_initialize(struct bonding *bond)
184{
185 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
186 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
Mitch Williams0d206a32005-11-09 10:35:30 -0800187 struct tlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int i;
189
190 spin_lock_init(&(bond_info->tx_hashtbl_lock));
191
Joe Jin243cb4e2007-02-06 14:16:40 -0800192 new_hashtbl = kzalloc(size, GFP_KERNEL);
Mitch Williams0d206a32005-11-09 10:35:30 -0800193 if (!new_hashtbl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800195 ": %s: Error: Failed to allocate TLB hash table\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return -1;
198 }
Mitch Williams0d206a32005-11-09 10:35:30 -0800199 _lock_tx_hashtbl(bond);
200
201 bond_info->tx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
204 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
205 }
206
207 _unlock_tx_hashtbl(bond);
208
209 return 0;
210}
211
212/* Must be called only after all slaves have been released */
213static void tlb_deinitialize(struct bonding *bond)
214{
215 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
216
217 _lock_tx_hashtbl(bond);
218
219 kfree(bond_info->tx_hashtbl);
220 bond_info->tx_hashtbl = NULL;
221
222 _unlock_tx_hashtbl(bond);
223}
224
225/* Caller must hold bond lock for read */
226static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
227{
228 struct slave *slave, *least_loaded;
229 s64 max_gap;
230 int i, found = 0;
231
232 /* Find the first enabled slave */
233 bond_for_each_slave(bond, slave, i) {
234 if (SLAVE_IS_OK(slave)) {
235 found = 1;
236 break;
237 }
238 }
239
240 if (!found) {
241 return NULL;
242 }
243
244 least_loaded = slave;
245 max_gap = (s64)(slave->speed << 20) - /* Convert to Megabit per sec */
246 (s64)(SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
247
248 /* Find the slave with the largest gap */
249 bond_for_each_slave_from(bond, slave, i, least_loaded) {
250 if (SLAVE_IS_OK(slave)) {
251 s64 gap = (s64)(slave->speed << 20) -
252 (s64)(SLAVE_TLB_INFO(slave).load << 3);
253 if (max_gap < gap) {
254 least_loaded = slave;
255 max_gap = gap;
256 }
257 }
258 }
259
260 return least_loaded;
261}
262
263/* Caller must hold bond lock for read */
264static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
265{
266 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
267 struct tlb_client_info *hash_table;
268 struct slave *assigned_slave;
269
270 _lock_tx_hashtbl(bond);
271
272 hash_table = bond_info->tx_hashtbl;
273 assigned_slave = hash_table[hash_index].tx_slave;
274 if (!assigned_slave) {
275 assigned_slave = tlb_get_least_loaded_slave(bond);
276
277 if (assigned_slave) {
278 struct tlb_slave_info *slave_info =
279 &(SLAVE_TLB_INFO(assigned_slave));
280 u32 next_index = slave_info->head;
281
282 hash_table[hash_index].tx_slave = assigned_slave;
283 hash_table[hash_index].next = next_index;
284 hash_table[hash_index].prev = TLB_NULL_INDEX;
285
286 if (next_index != TLB_NULL_INDEX) {
287 hash_table[next_index].prev = hash_index;
288 }
289
290 slave_info->head = hash_index;
291 slave_info->load +=
292 hash_table[hash_index].load_history;
293 }
294 }
295
296 if (assigned_slave) {
297 hash_table[hash_index].tx_bytes += skb_len;
298 }
299
300 _unlock_tx_hashtbl(bond);
301
302 return assigned_slave;
303}
304
305/*********************** rlb specific functions ***************************/
306static inline void _lock_rx_hashtbl(struct bonding *bond)
307{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700308 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311static inline void _unlock_rx_hashtbl(struct bonding *bond)
312{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700313 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/* when an ARP REPLY is received from a client update its info
317 * in the rx_hashtbl
318 */
319static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
320{
321 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
322 struct rlb_client_info *client_info;
323 u32 hash_index;
324
325 _lock_rx_hashtbl(bond);
326
327 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
328 client_info = &(bond_info->rx_hashtbl[hash_index]);
329
330 if ((client_info->assigned) &&
331 (client_info->ip_src == arp->ip_dst) &&
332 (client_info->ip_dst == arp->ip_src)) {
333 /* update the clients MAC address */
334 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
335 client_info->ntt = 1;
336 bond_info->rx_ntt = 1;
337 }
338
339 _unlock_rx_hashtbl(bond);
340}
341
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700342static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct bonding *bond = bond_dev->priv;
345 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
346 int res = NET_RX_DROP;
347
Eric W. Biedermane730c152007-09-17 11:53:39 -0700348 if (bond_dev->nd_net != &init_net)
349 goto out;
350
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700351 if (!(bond_dev->flags & IFF_MASTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (!arp) {
355 dprintk("Packet has no ARP data\n");
356 goto out;
357 }
358
359 if (skb->len < sizeof(struct arp_pkt)) {
360 dprintk("Packet is too small to be an ARP\n");
361 goto out;
362 }
363
364 if (arp->op_code == htons(ARPOP_REPLY)) {
365 /* update rx hash table for this ARP */
366 rlb_update_entry_from_arp(bond, arp);
367 dprintk("Server received an ARP Reply from client\n");
368 }
369
370 res = NET_RX_SUCCESS;
371
372out:
373 dev_kfree_skb(skb);
374
375 return res;
376}
377
378/* Caller must hold bond lock for read */
379static struct slave *rlb_next_rx_slave(struct bonding *bond)
380{
381 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
382 struct slave *rx_slave, *slave, *start_at;
383 int i = 0;
384
385 if (bond_info->next_rx_slave) {
386 start_at = bond_info->next_rx_slave;
387 } else {
388 start_at = bond->first_slave;
389 }
390
391 rx_slave = NULL;
392
393 bond_for_each_slave_from(bond, slave, i, start_at) {
394 if (SLAVE_IS_OK(slave)) {
395 if (!rx_slave) {
396 rx_slave = slave;
397 } else if (slave->speed > rx_slave->speed) {
398 rx_slave = slave;
399 }
400 }
401 }
402
403 if (rx_slave) {
404 bond_info->next_rx_slave = rx_slave->next;
405 }
406
407 return rx_slave;
408}
409
410/* teach the switch the mac of a disabled slave
411 * on the primary for fault tolerance
412 *
413 * Caller must hold bond->curr_slave_lock for write or bond lock for write
414 */
415static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
416{
417 if (!bond->curr_active_slave) {
418 return;
419 }
420
421 if (!bond->alb_info.primary_is_promisc) {
422 bond->alb_info.primary_is_promisc = 1;
423 dev_set_promiscuity(bond->curr_active_slave->dev, 1);
424 }
425
426 bond->alb_info.rlb_promisc_timeout_counter = 0;
427
428 alb_send_learning_packets(bond->curr_active_slave, addr);
429}
430
431/* slave being removed should not be active at this point
432 *
433 * Caller must hold bond lock for read
434 */
435static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
436{
437 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
438 struct rlb_client_info *rx_hash_table;
439 u32 index, next_index;
440
441 /* clear slave from rx_hashtbl */
442 _lock_rx_hashtbl(bond);
443
444 rx_hash_table = bond_info->rx_hashtbl;
445 index = bond_info->rx_hashtbl_head;
446 for (; index != RLB_NULL_INDEX; index = next_index) {
447 next_index = rx_hash_table[index].next;
448 if (rx_hash_table[index].slave == slave) {
449 struct slave *assigned_slave = rlb_next_rx_slave(bond);
450
451 if (assigned_slave) {
452 rx_hash_table[index].slave = assigned_slave;
453 if (memcmp(rx_hash_table[index].mac_dst,
454 mac_bcast, ETH_ALEN)) {
455 bond_info->rx_hashtbl[index].ntt = 1;
456 bond_info->rx_ntt = 1;
457 /* A slave has been removed from the
458 * table because it is either disabled
459 * or being released. We must retry the
460 * update to avoid clients from not
461 * being updated & disconnecting when
462 * there is stress
463 */
464 bond_info->rlb_update_retry_counter =
465 RLB_UPDATE_RETRY;
466 }
467 } else { /* there is no active slave */
468 rx_hash_table[index].slave = NULL;
469 }
470 }
471 }
472
473 _unlock_rx_hashtbl(bond);
474
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700475 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (slave != bond->curr_active_slave) {
478 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
479 }
480
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700481 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484static void rlb_update_client(struct rlb_client_info *client_info)
485{
486 int i;
487
488 if (!client_info->slave) {
489 return;
490 }
491
492 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
493 struct sk_buff *skb;
494
495 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
496 client_info->ip_dst,
497 client_info->slave->dev,
498 client_info->ip_src,
499 client_info->mac_dst,
500 client_info->slave->dev->dev_addr,
501 client_info->mac_dst);
502 if (!skb) {
503 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800504 ": %s: Error: failed to create an ARP packet\n",
505 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 continue;
507 }
508
509 skb->dev = client_info->slave->dev;
510
511 if (client_info->tag) {
512 skb = vlan_put_tag(skb, client_info->vlan_id);
513 if (!skb) {
514 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800515 ": %s: Error: failed to insert VLAN tag\n",
516 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 continue;
518 }
519 }
520
521 arp_xmit(skb);
522 }
523}
524
525/* sends ARP REPLIES that update the clients that need updating */
526static void rlb_update_rx_clients(struct bonding *bond)
527{
528 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
529 struct rlb_client_info *client_info;
530 u32 hash_index;
531
532 _lock_rx_hashtbl(bond);
533
534 hash_index = bond_info->rx_hashtbl_head;
535 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
536 client_info = &(bond_info->rx_hashtbl[hash_index]);
537 if (client_info->ntt) {
538 rlb_update_client(client_info);
539 if (bond_info->rlb_update_retry_counter == 0) {
540 client_info->ntt = 0;
541 }
542 }
543 }
544
545 /* do not update the entries again untill this counter is zero so that
546 * not to confuse the clients.
547 */
548 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
549
550 _unlock_rx_hashtbl(bond);
551}
552
553/* The slave was assigned a new mac address - update the clients */
554static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
555{
556 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
557 struct rlb_client_info *client_info;
558 int ntt = 0;
559 u32 hash_index;
560
561 _lock_rx_hashtbl(bond);
562
563 hash_index = bond_info->rx_hashtbl_head;
564 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
565 client_info = &(bond_info->rx_hashtbl[hash_index]);
566
567 if ((client_info->slave == slave) &&
568 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
569 client_info->ntt = 1;
570 ntt = 1;
571 }
572 }
573
574 // update the team's flag only after the whole iteration
575 if (ntt) {
576 bond_info->rx_ntt = 1;
577 //fasten the change
578 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
579 }
580
581 _unlock_rx_hashtbl(bond);
582}
583
584/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400585static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
588 struct rlb_client_info *client_info;
589 u32 hash_index;
590
591 _lock_rx_hashtbl(bond);
592
593 hash_index = bond_info->rx_hashtbl_head;
594 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
595 client_info = &(bond_info->rx_hashtbl[hash_index]);
596
597 if (!client_info->slave) {
598 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800599 ": %s: Error: found a client with no channel in "
600 "the client's hash table\n",
601 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 continue;
603 }
604 /*update all clients using this src_ip, that are not assigned
605 * to the team's address (curr_active_slave) and have a known
606 * unicast mac address.
607 */
608 if ((client_info->ip_src == src_ip) &&
609 memcmp(client_info->slave->dev->dev_addr,
610 bond->dev->dev_addr, ETH_ALEN) &&
611 memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
612 client_info->ntt = 1;
613 bond_info->rx_ntt = 1;
614 }
615 }
616
617 _unlock_rx_hashtbl(bond);
618}
619
620/* Caller must hold both bond and ptr locks for read */
621static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
622{
623 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300624 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct slave *assigned_slave;
626 struct rlb_client_info *client_info;
627 u32 hash_index = 0;
628
629 _lock_rx_hashtbl(bond);
630
631 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
632 client_info = &(bond_info->rx_hashtbl[hash_index]);
633
634 if (client_info->assigned) {
635 if ((client_info->ip_src == arp->ip_src) &&
636 (client_info->ip_dst == arp->ip_dst)) {
637 /* the entry is already assigned to this client */
638 if (memcmp(arp->mac_dst, mac_bcast, ETH_ALEN)) {
639 /* update mac address from arp */
640 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
641 }
642
643 assigned_slave = client_info->slave;
644 if (assigned_slave) {
645 _unlock_rx_hashtbl(bond);
646 return assigned_slave;
647 }
648 } else {
649 /* the entry is already assigned to some other client,
650 * move the old client to primary (curr_active_slave) so
651 * that the new client can be assigned to this entry.
652 */
653 if (bond->curr_active_slave &&
654 client_info->slave != bond->curr_active_slave) {
655 client_info->slave = bond->curr_active_slave;
656 rlb_update_client(client_info);
657 }
658 }
659 }
660 /* assign a new slave */
661 assigned_slave = rlb_next_rx_slave(bond);
662
663 if (assigned_slave) {
664 client_info->ip_src = arp->ip_src;
665 client_info->ip_dst = arp->ip_dst;
666 /* arp->mac_dst is broadcast for arp reqeusts.
667 * will be updated with clients actual unicast mac address
668 * upon receiving an arp reply.
669 */
670 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
671 client_info->slave = assigned_slave;
672
673 if (memcmp(client_info->mac_dst, mac_bcast, ETH_ALEN)) {
674 client_info->ntt = 1;
675 bond->alb_info.rx_ntt = 1;
676 } else {
677 client_info->ntt = 0;
678 }
679
680 if (!list_empty(&bond->vlan_list)) {
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700681 if (!vlan_get_tag(skb, &client_info->vlan_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 client_info->tag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 if (!client_info->assigned) {
686 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
687 bond_info->rx_hashtbl_head = hash_index;
688 client_info->next = prev_tbl_head;
689 if (prev_tbl_head != RLB_NULL_INDEX) {
690 bond_info->rx_hashtbl[prev_tbl_head].prev =
691 hash_index;
692 }
693 client_info->assigned = 1;
694 }
695 }
696
697 _unlock_rx_hashtbl(bond);
698
699 return assigned_slave;
700}
701
702/* chooses (and returns) transmit channel for arp reply
703 * does not choose channel for other arp types since they are
704 * sent on the curr_active_slave
705 */
706static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
707{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300708 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 struct slave *tx_slave = NULL;
710
711 if (arp->op_code == __constant_htons(ARPOP_REPLY)) {
712 /* the arp must be sent on the selected
713 * rx channel
714 */
715 tx_slave = rlb_choose_channel(skb, bond);
716 if (tx_slave) {
717 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
718 }
719 dprintk("Server sent ARP Reply packet\n");
720 } else if (arp->op_code == __constant_htons(ARPOP_REQUEST)) {
721 /* Create an entry in the rx_hashtbl for this client as a
722 * place holder.
723 * When the arp reply is received the entry will be updated
724 * with the correct unicast address of the client.
725 */
726 rlb_choose_channel(skb, bond);
727
728 /* The ARP relpy packets must be delayed so that
729 * they can cancel out the influence of the ARP request.
730 */
731 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
732
733 /* arp requests are broadcast and are sent on the primary
734 * the arp request will collapse all clients on the subnet to
735 * the primary slave. We must register these clients to be
736 * updated with their assigned mac.
737 */
738 rlb_req_update_subnet_clients(bond, arp->ip_src);
739 dprintk("Server sent ARP Request packet\n");
740 }
741
742 return tx_slave;
743}
744
745/* Caller must hold bond lock for read */
746static void rlb_rebalance(struct bonding *bond)
747{
748 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
749 struct slave *assigned_slave;
750 struct rlb_client_info *client_info;
751 int ntt;
752 u32 hash_index;
753
754 _lock_rx_hashtbl(bond);
755
756 ntt = 0;
757 hash_index = bond_info->rx_hashtbl_head;
758 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
759 client_info = &(bond_info->rx_hashtbl[hash_index]);
760 assigned_slave = rlb_next_rx_slave(bond);
761 if (assigned_slave && (client_info->slave != assigned_slave)) {
762 client_info->slave = assigned_slave;
763 client_info->ntt = 1;
764 ntt = 1;
765 }
766 }
767
768 /* update the team's flag only after the whole iteration */
769 if (ntt) {
770 bond_info->rx_ntt = 1;
771 }
772 _unlock_rx_hashtbl(bond);
773}
774
775/* Caller must hold rx_hashtbl lock */
776static void rlb_init_table_entry(struct rlb_client_info *entry)
777{
778 memset(entry, 0, sizeof(struct rlb_client_info));
779 entry->next = RLB_NULL_INDEX;
780 entry->prev = RLB_NULL_INDEX;
781}
782
783static int rlb_initialize(struct bonding *bond)
784{
785 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
786 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
Mitch Williams0d206a32005-11-09 10:35:30 -0800787 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
789 int i;
790
791 spin_lock_init(&(bond_info->rx_hashtbl_lock));
792
Mitch Williams0d206a32005-11-09 10:35:30 -0800793 new_hashtbl = kmalloc(size, GFP_KERNEL);
794 if (!new_hashtbl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800796 ": %s: Error: Failed to allocate RLB hash table\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -1;
799 }
Mitch Williams0d206a32005-11-09 10:35:30 -0800800 _lock_rx_hashtbl(bond);
801
802 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
805
806 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
807 rlb_init_table_entry(bond_info->rx_hashtbl + i);
808 }
809
810 _unlock_rx_hashtbl(bond);
811
812 /*initialize packet type*/
813 pk_type->type = __constant_htons(ETH_P_ARP);
814 pk_type->dev = bond->dev;
815 pk_type->func = rlb_arp_recv;
816
817 /* register to receive ARPs */
818 dev_add_pack(pk_type);
819
820 return 0;
821}
822
823static void rlb_deinitialize(struct bonding *bond)
824{
825 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
826
827 dev_remove_pack(&(bond_info->rlb_pkt_type));
828
829 _lock_rx_hashtbl(bond);
830
831 kfree(bond_info->rx_hashtbl);
832 bond_info->rx_hashtbl = NULL;
833 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
834
835 _unlock_rx_hashtbl(bond);
836}
837
838static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
839{
840 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
841 u32 curr_index;
842
843 _lock_rx_hashtbl(bond);
844
845 curr_index = bond_info->rx_hashtbl_head;
846 while (curr_index != RLB_NULL_INDEX) {
847 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
848 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
849 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
850
851 if (curr->tag && (curr->vlan_id == vlan_id)) {
852 if (curr_index == bond_info->rx_hashtbl_head) {
853 bond_info->rx_hashtbl_head = next_index;
854 }
855 if (prev_index != RLB_NULL_INDEX) {
856 bond_info->rx_hashtbl[prev_index].next = next_index;
857 }
858 if (next_index != RLB_NULL_INDEX) {
859 bond_info->rx_hashtbl[next_index].prev = prev_index;
860 }
861
862 rlb_init_table_entry(curr);
863 }
864
865 curr_index = next_index;
866 }
867
868 _unlock_rx_hashtbl(bond);
869}
870
871/*********************** tlb/rlb shared functions *********************/
872
873static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
874{
875 struct bonding *bond = bond_get_bond_by_slave(slave);
876 struct learning_pkt pkt;
877 int size = sizeof(struct learning_pkt);
878 int i;
879
880 memset(&pkt, 0, size);
881 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
882 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
883 pkt.type = __constant_htons(ETH_P_LOOP);
884
885 for (i = 0; i < MAX_LP_BURST; i++) {
886 struct sk_buff *skb;
887 char *data;
888
889 skb = dev_alloc_skb(size);
890 if (!skb) {
891 return;
892 }
893
894 data = skb_put(skb, size);
895 memcpy(data, &pkt, size);
896
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700897 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700898 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 skb->protocol = pkt.type;
900 skb->priority = TC_PRIO_CONTROL;
901 skb->dev = slave->dev;
902
903 if (!list_empty(&bond->vlan_list)) {
904 struct vlan_entry *vlan;
905
906 vlan = bond_next_vlan(bond,
907 bond->alb_info.current_alb_vlan);
908
909 bond->alb_info.current_alb_vlan = vlan;
910 if (!vlan) {
911 kfree_skb(skb);
912 continue;
913 }
914
915 skb = vlan_put_tag(skb, vlan->vlan_id);
916 if (!skb) {
917 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800918 ": %s: Error: failed to insert VLAN tag\n",
919 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 continue;
921 }
922 }
923
924 dev_queue_xmit(skb);
925 }
926}
927
928/* hw is a boolean parameter that determines whether we should try and
929 * set the hw address of the device as well as the hw address of the
930 * net_device
931 */
932static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
933{
934 struct net_device *dev = slave->dev;
935 struct sockaddr s_addr;
936
937 if (!hw) {
938 memcpy(dev->dev_addr, addr, dev->addr_len);
939 return 0;
940 }
941
942 /* for rlb each slave must have a unique hw mac addresses so that */
943 /* each slave will receive packets destined to a different mac */
944 memcpy(s_addr.sa_data, addr, dev->addr_len);
945 s_addr.sa_family = dev->type;
946 if (dev_set_mac_address(dev, &s_addr)) {
947 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -0800948 ": %s: Error: dev_set_mac_address of dev %s failed! ALB "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 "mode requires that the base driver support setting "
950 "the hw address also when the network device's "
951 "interface is open\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800952 dev->master->name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -EOPNOTSUPP;
954 }
955 return 0;
956}
957
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700958/*
959 * Swap MAC addresses between two slaves.
960 *
961 * Called with RTNL held, and no other locks.
962 *
963 */
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
966{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
970 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
971 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
972
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700973}
974
975/*
976 * Send learning packets after MAC address swap.
977 *
Jay Vosburgh25433312008-01-17 16:24:59 -0800978 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700979 */
980static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
981 struct slave *slave2)
982{
983 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
984 struct slave *disabled_slave = NULL;
985
Jay Vosburgh25433312008-01-17 16:24:59 -0800986 ASSERT_RTNL();
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* fasten the change in the switch */
989 if (SLAVE_IS_OK(slave1)) {
990 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
991 if (bond->alb_info.rlb_enabled) {
992 /* inform the clients that the mac address
993 * has changed
994 */
995 rlb_req_update_slave_clients(bond, slave1);
996 }
997 } else {
998 disabled_slave = slave1;
999 }
1000
1001 if (SLAVE_IS_OK(slave2)) {
1002 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1003 if (bond->alb_info.rlb_enabled) {
1004 /* inform the clients that the mac address
1005 * has changed
1006 */
1007 rlb_req_update_slave_clients(bond, slave2);
1008 }
1009 } else {
1010 disabled_slave = slave2;
1011 }
1012
1013 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1014 /* A disabled slave was assigned an active mac addr */
1015 rlb_teach_disabled_mac_on_primary(bond,
1016 disabled_slave->dev->dev_addr);
1017 }
1018}
1019
1020/**
1021 * alb_change_hw_addr_on_detach
1022 * @bond: bonding we're working on
1023 * @slave: the slave that was just detached
1024 *
1025 * We assume that @slave was already detached from the slave list.
1026 *
1027 * If @slave's permanent hw address is different both from its current
1028 * address and from @bond's address, then somewhere in the bond there's
1029 * a slave that has @slave's permanet address as its current address.
1030 * We'll make sure that that slave no longer uses @slave's permanent address.
1031 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001032 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
1034static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1035{
1036 int perm_curr_diff;
1037 int perm_bond_diff;
1038
1039 perm_curr_diff = memcmp(slave->perm_hwaddr,
1040 slave->dev->dev_addr,
1041 ETH_ALEN);
1042 perm_bond_diff = memcmp(slave->perm_hwaddr,
1043 bond->dev->dev_addr,
1044 ETH_ALEN);
1045
1046 if (perm_curr_diff && perm_bond_diff) {
1047 struct slave *tmp_slave;
1048 int i, found = 0;
1049
1050 bond_for_each_slave(bond, tmp_slave, i) {
1051 if (!memcmp(slave->perm_hwaddr,
1052 tmp_slave->dev->dev_addr,
1053 ETH_ALEN)) {
1054 found = 1;
1055 break;
1056 }
1057 }
1058
1059 if (found) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001060 /* locking: needs RTNL and nothing else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 alb_swap_mac_addr(bond, slave, tmp_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001062 alb_fasten_mac_swap(bond, slave, tmp_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 }
1065}
1066
1067/**
1068 * alb_handle_addr_collision_on_attach
1069 * @bond: bonding we're working on
1070 * @slave: the slave that was just attached
1071 *
1072 * checks uniqueness of slave's mac address and handles the case the
1073 * new slave uses the bonds mac address.
1074 *
1075 * If the permanent hw address of @slave is @bond's hw address, we need to
1076 * find a different hw address to give @slave, that isn't in use by any other
1077 * slave in the bond. This address must be, of course, one of the premanent
1078 * addresses of the other slaves.
1079 *
1080 * We go over the slave list, and for each slave there we compare its
1081 * permanent hw address with the current address of all the other slaves.
1082 * If no match was found, then we've found a slave with a permanent address
1083 * that isn't used by any other slave in the bond, so we can assign it to
1084 * @slave.
1085 *
1086 * assumption: this function is called before @slave is attached to the
1087 * bond slave list.
1088 *
1089 * caller must hold the bond lock for write since the mac addresses are compared
1090 * and may be swapped.
1091 */
1092static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1093{
1094 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1095 struct slave *has_bond_addr = bond->curr_active_slave;
1096 int i, j, found = 0;
1097
1098 if (bond->slave_cnt == 0) {
1099 /* this is the first slave */
1100 return 0;
1101 }
1102
1103 /* if slave's mac address differs from bond's mac address
1104 * check uniqueness of slave's mac address against the other
1105 * slaves in the bond.
1106 */
1107 if (memcmp(slave->perm_hwaddr, bond->dev->dev_addr, ETH_ALEN)) {
1108 bond_for_each_slave(bond, tmp_slave1, i) {
1109 if (!memcmp(tmp_slave1->dev->dev_addr, slave->dev->dev_addr,
1110 ETH_ALEN)) {
1111 found = 1;
1112 break;
1113 }
1114 }
1115
John W. Linville6b38aef2005-07-28 15:00:15 -04001116 if (!found)
1117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
John W. Linville6b38aef2005-07-28 15:00:15 -04001119 /* Try setting slave mac to bond address and fall-through
1120 to code handling that situation below... */
1121 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1122 bond->alb_info.rlb_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
1125 /* The slave's address is equal to the address of the bond.
1126 * Search for a spare address in the bond for this slave.
1127 */
1128 free_mac_slave = NULL;
1129
1130 bond_for_each_slave(bond, tmp_slave1, i) {
1131 found = 0;
1132 bond_for_each_slave(bond, tmp_slave2, j) {
1133 if (!memcmp(tmp_slave1->perm_hwaddr,
1134 tmp_slave2->dev->dev_addr,
1135 ETH_ALEN)) {
1136 found = 1;
1137 break;
1138 }
1139 }
1140
1141 if (!found) {
1142 /* no slave has tmp_slave1's perm addr
1143 * as its curr addr
1144 */
1145 free_mac_slave = tmp_slave1;
1146 break;
1147 }
1148
1149 if (!has_bond_addr) {
1150 if (!memcmp(tmp_slave1->dev->dev_addr,
1151 bond->dev->dev_addr,
1152 ETH_ALEN)) {
1153
1154 has_bond_addr = tmp_slave1;
1155 }
1156 }
1157 }
1158
1159 if (free_mac_slave) {
1160 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1161 bond->alb_info.rlb_enabled);
1162
1163 printk(KERN_WARNING DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001164 ": %s: Warning: the hw address of slave %s is in use by "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 "the bond; giving it the hw address of %s\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001166 bond->dev->name, slave->dev->name, free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 } else if (has_bond_addr) {
1169 printk(KERN_ERR DRV_NAME
Mitch Williams4e0952c2005-11-09 10:34:57 -08001170 ": %s: Error: the hw address of slave %s is in use by the "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 "bond; couldn't find a slave with a free hw address to "
1172 "give it (this should not have happened)\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001173 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return -EFAULT;
1175 }
1176
1177 return 0;
1178}
1179
1180/**
1181 * alb_set_mac_address
1182 * @bond:
1183 * @addr:
1184 *
1185 * In TLB mode all slaves are configured to the bond's hw address, but set
1186 * their dev_addr field to different addresses (based on their permanent hw
1187 * addresses).
1188 *
1189 * For each slave, this function sets the interface to the new address and then
1190 * changes its dev_addr field to its previous value.
1191 *
1192 * Unwinding assumes bond's mac address has not yet changed.
1193 */
1194static int alb_set_mac_address(struct bonding *bond, void *addr)
1195{
1196 struct sockaddr sa;
1197 struct slave *slave, *stop_at;
1198 char tmp_addr[ETH_ALEN];
1199 int res;
1200 int i;
1201
1202 if (bond->alb_info.rlb_enabled) {
1203 return 0;
1204 }
1205
1206 bond_for_each_slave(bond, slave, i) {
1207 if (slave->dev->set_mac_address == NULL) {
1208 res = -EOPNOTSUPP;
1209 goto unwind;
1210 }
1211
1212 /* save net_device's current hw address */
1213 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1214
1215 res = dev_set_mac_address(slave->dev, addr);
1216
1217 /* restore net_device's hw address */
1218 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1219
1220 if (res) {
1221 goto unwind;
1222 }
1223 }
1224
1225 return 0;
1226
1227unwind:
1228 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1229 sa.sa_family = bond->dev->type;
1230
1231 /* unwind from head to the slave that failed */
1232 stop_at = slave;
1233 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1234 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1235 dev_set_mac_address(slave->dev, &sa);
1236 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1237 }
1238
1239 return res;
1240}
1241
1242/************************ exported alb funcions ************************/
1243
1244int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1245{
1246 int res;
1247
1248 res = tlb_initialize(bond);
1249 if (res) {
1250 return res;
1251 }
1252
1253 if (rlb_enabled) {
1254 bond->alb_info.rlb_enabled = 1;
1255 /* initialize rlb */
1256 res = rlb_initialize(bond);
1257 if (res) {
1258 tlb_deinitialize(bond);
1259 return res;
1260 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001261 } else {
1262 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
1265 return 0;
1266}
1267
1268void bond_alb_deinitialize(struct bonding *bond)
1269{
1270 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1271
1272 tlb_deinitialize(bond);
1273
1274 if (bond_info->rlb_enabled) {
1275 rlb_deinitialize(bond);
1276 }
1277}
1278
1279int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1280{
1281 struct bonding *bond = bond_dev->priv;
1282 struct ethhdr *eth_data;
1283 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1284 struct slave *tx_slave = NULL;
Al Virod3bb52b2007-08-22 20:06:58 -04001285 static const __be32 ip_bcast = htonl(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int hash_size = 0;
1287 int do_tx_balance = 1;
1288 u32 hash_index = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001289 const u8 *hash_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 int res = 1;
1291
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001292 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 eth_data = eth_hdr(skb);
1294
1295 /* make sure that the curr_active_slave and the slaves list do
1296 * not change during tx
1297 */
1298 read_lock(&bond->lock);
1299 read_lock(&bond->curr_slave_lock);
1300
1301 if (!BOND_IS_OK(bond)) {
1302 goto out;
1303 }
1304
1305 switch (ntohs(skb->protocol)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001306 case ETH_P_IP: {
1307 const struct iphdr *iph = ip_hdr(skb);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if ((memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001310 (iph->daddr == ip_bcast) ||
1311 (iph->protocol == IPPROTO_IGMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 do_tx_balance = 0;
1313 break;
1314 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001315 hash_start = (char *)&(iph->daddr);
1316 hash_size = sizeof(iph->daddr);
1317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
1319 case ETH_P_IPV6:
1320 if (memcmp(eth_data->h_dest, mac_bcast, ETH_ALEN) == 0) {
1321 do_tx_balance = 0;
1322 break;
1323 }
1324
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001325 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1326 hash_size = sizeof(ipv6_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
1328 case ETH_P_IPX:
Al Virod3bb52b2007-08-22 20:06:58 -04001329 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* something is wrong with this packet */
1331 do_tx_balance = 0;
1332 break;
1333 }
1334
1335 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1336 /* The only protocol worth balancing in
1337 * this family since it has an "ARP" like
1338 * mechanism
1339 */
1340 do_tx_balance = 0;
1341 break;
1342 }
1343
1344 hash_start = (char*)eth_data->h_dest;
1345 hash_size = ETH_ALEN;
1346 break;
1347 case ETH_P_ARP:
1348 do_tx_balance = 0;
1349 if (bond_info->rlb_enabled) {
1350 tx_slave = rlb_arp_xmit(skb, bond);
1351 }
1352 break;
1353 default:
1354 do_tx_balance = 0;
1355 break;
1356 }
1357
1358 if (do_tx_balance) {
1359 hash_index = _simple_hash(hash_start, hash_size);
1360 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1361 }
1362
1363 if (!tx_slave) {
1364 /* unbalanced or unassigned, send through primary */
1365 tx_slave = bond->curr_active_slave;
1366 bond_info->unbalanced_load += skb->len;
1367 }
1368
1369 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1370 if (tx_slave != bond->curr_active_slave) {
1371 memcpy(eth_data->h_source,
1372 tx_slave->dev->dev_addr,
1373 ETH_ALEN);
1374 }
1375
1376 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1377 } else {
1378 if (tx_slave) {
1379 tlb_clear_slave(bond, tx_slave, 0);
1380 }
1381 }
1382
1383out:
1384 if (res) {
1385 /* no suitable interface, frame not sent */
1386 dev_kfree_skb(skb);
1387 }
1388 read_unlock(&bond->curr_slave_lock);
1389 read_unlock(&bond->lock);
1390 return 0;
1391}
1392
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001393void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001395 struct bonding *bond = container_of(work, struct bonding,
1396 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1398 struct slave *slave;
1399 int i;
1400
1401 read_lock(&bond->lock);
1402
1403 if (bond->kill_timers) {
1404 goto out;
1405 }
1406
1407 if (bond->slave_cnt == 0) {
1408 bond_info->tx_rebalance_counter = 0;
1409 bond_info->lp_counter = 0;
1410 goto re_arm;
1411 }
1412
1413 bond_info->tx_rebalance_counter++;
1414 bond_info->lp_counter++;
1415
1416 /* send learning packets */
1417 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1418 /* change of curr_active_slave involves swapping of mac addresses.
1419 * in order to avoid this swapping from happening while
1420 * sending the learning packets, the curr_slave_lock must be held for
1421 * read.
1422 */
1423 read_lock(&bond->curr_slave_lock);
1424
1425 bond_for_each_slave(bond, slave, i) {
Mitch Williamse944ef72005-11-09 10:36:50 -08001426 alb_send_learning_packets(slave, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
1429 read_unlock(&bond->curr_slave_lock);
1430
1431 bond_info->lp_counter = 0;
1432 }
1433
1434 /* rebalance tx traffic */
1435 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1436
1437 read_lock(&bond->curr_slave_lock);
1438
1439 bond_for_each_slave(bond, slave, i) {
1440 tlb_clear_slave(bond, slave, 1);
1441 if (slave == bond->curr_active_slave) {
1442 SLAVE_TLB_INFO(slave).load =
1443 bond_info->unbalanced_load /
1444 BOND_TLB_REBALANCE_INTERVAL;
1445 bond_info->unbalanced_load = 0;
1446 }
1447 }
1448
1449 read_unlock(&bond->curr_slave_lock);
1450
1451 bond_info->tx_rebalance_counter = 0;
1452 }
1453
1454 /* handle rlb stuff */
1455 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (bond_info->primary_is_promisc &&
1457 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1458
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001459 /*
1460 * dev_set_promiscuity requires rtnl and
1461 * nothing else.
1462 */
1463 read_unlock(&bond->lock);
1464 rtnl_lock();
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 bond_info->rlb_promisc_timeout_counter = 0;
1467
1468 /* If the primary was set to promiscuous mode
1469 * because a slave was disabled then
1470 * it can now leave promiscuous mode.
1471 */
1472 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1473 bond_info->primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001475 rtnl_unlock();
1476 read_lock(&bond->lock);
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 if (bond_info->rlb_rebalance) {
1480 bond_info->rlb_rebalance = 0;
1481 rlb_rebalance(bond);
1482 }
1483
1484 /* check if clients need updating */
1485 if (bond_info->rx_ntt) {
1486 if (bond_info->rlb_update_delay_counter) {
1487 --bond_info->rlb_update_delay_counter;
1488 } else {
1489 rlb_update_rx_clients(bond);
1490 if (bond_info->rlb_update_retry_counter) {
1491 --bond_info->rlb_update_retry_counter;
1492 } else {
1493 bond_info->rx_ntt = 0;
1494 }
1495 }
1496 }
1497 }
1498
1499re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001500 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501out:
1502 read_unlock(&bond->lock);
1503}
1504
1505/* assumption: called before the slave is attached to the bond
1506 * and not locked by the bond lock
1507 */
1508int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1509{
1510 int res;
1511
1512 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1513 bond->alb_info.rlb_enabled);
1514 if (res) {
1515 return res;
1516 }
1517
1518 /* caller must hold the bond lock for write since the mac addresses
1519 * are compared and may be swapped.
1520 */
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001521 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 res = alb_handle_addr_collision_on_attach(bond, slave);
1524
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001525 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 if (res) {
1528 return res;
1529 }
1530
1531 tlb_init_slave(slave);
1532
1533 /* order a rebalance ASAP */
1534 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1535
1536 if (bond->alb_info.rlb_enabled) {
1537 bond->alb_info.rlb_rebalance = 1;
1538 }
1539
1540 return 0;
1541}
1542
Jay Vosburgh25433312008-01-17 16:24:59 -08001543/*
1544 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1545 * if necessary.
1546 *
1547 * Caller must hold RTNL and no other locks
1548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1550{
1551 if (bond->slave_cnt > 1) {
1552 alb_change_hw_addr_on_detach(bond, slave);
1553 }
1554
1555 tlb_clear_slave(bond, slave, 0);
1556
1557 if (bond->alb_info.rlb_enabled) {
1558 bond->alb_info.next_rx_slave = NULL;
1559 rlb_clear_slave(bond, slave);
1560 }
1561}
1562
1563/* Caller must hold bond lock for read */
1564void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1565{
1566 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1567
1568 if (link == BOND_LINK_DOWN) {
1569 tlb_clear_slave(bond, slave, 0);
1570 if (bond->alb_info.rlb_enabled) {
1571 rlb_clear_slave(bond, slave);
1572 }
1573 } else if (link == BOND_LINK_UP) {
1574 /* order a rebalance ASAP */
1575 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1576 if (bond->alb_info.rlb_enabled) {
1577 bond->alb_info.rlb_rebalance = 1;
1578 /* If the updelay module parameter is smaller than the
1579 * forwarding delay of the switch the rebalance will
1580 * not work because the rebalance arp replies will
1581 * not be forwarded to the clients..
1582 */
1583 }
1584 }
1585}
1586
1587/**
1588 * bond_alb_handle_active_change - assign new curr_active_slave
1589 * @bond: our bonding struct
1590 * @new_slave: new slave to assign
1591 *
1592 * Set the bond->curr_active_slave to @new_slave and handle
1593 * mac address swapping and promiscuity changes as needed.
1594 *
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001595 * If new_slave is NULL, caller must hold curr_slave_lock or
1596 * bond->lock for write.
1597 *
1598 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1599 * read and curr_slave_lock for write. Processing here may sleep, so
1600 * no other locks may be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
1602void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1603{
1604 struct slave *swap_slave;
1605 int i;
1606
1607 if (bond->curr_active_slave == new_slave) {
1608 return;
1609 }
1610
1611 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1612 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1613 bond->alb_info.primary_is_promisc = 0;
1614 bond->alb_info.rlb_promisc_timeout_counter = 0;
1615 }
1616
1617 swap_slave = bond->curr_active_slave;
1618 bond->curr_active_slave = new_slave;
1619
1620 if (!new_slave || (bond->slave_cnt == 0)) {
1621 return;
1622 }
1623
1624 /* set the new curr_active_slave to the bonds mac address
1625 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1626 */
1627 if (!swap_slave) {
1628 struct slave *tmp_slave;
1629 /* find slave that is holding the bond's mac address */
1630 bond_for_each_slave(bond, tmp_slave, i) {
1631 if (!memcmp(tmp_slave->dev->dev_addr,
1632 bond->dev->dev_addr, ETH_ALEN)) {
1633 swap_slave = tmp_slave;
1634 break;
1635 }
1636 }
1637 }
1638
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001639 /*
1640 * Arrange for swap_slave and new_slave to temporarily be
1641 * ignored so we can mess with their MAC addresses without
1642 * fear of interference from transmit activity.
1643 */
1644 if (swap_slave) {
1645 tlb_clear_slave(bond, swap_slave, 1);
1646 }
1647 tlb_clear_slave(bond, new_slave, 1);
1648
1649 write_unlock_bh(&bond->curr_slave_lock);
1650 read_unlock(&bond->lock);
1651
Jay Vosburghe0138a62008-01-17 16:24:58 -08001652 ASSERT_RTNL();
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1655 if (swap_slave) {
1656 /* swap mac address */
1657 alb_swap_mac_addr(bond, swap_slave, new_slave);
1658 } else {
1659 /* set the new_slave to the bond mac address */
1660 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1661 bond->alb_info.rlb_enabled);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001662 }
1663
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001664 if (swap_slave) {
1665 alb_fasten_mac_swap(bond, swap_slave, new_slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001666 read_lock(&bond->lock);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001667 } else {
Jay Vosburgh25433312008-01-17 16:24:59 -08001668 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1670 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001671
1672 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001675/*
1676 * Called with RTNL
1677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1679{
1680 struct bonding *bond = bond_dev->priv;
1681 struct sockaddr *sa = addr;
1682 struct slave *slave, *swap_slave;
1683 int res;
1684 int i;
1685
1686 if (!is_valid_ether_addr(sa->sa_data)) {
1687 return -EADDRNOTAVAIL;
1688 }
1689
1690 res = alb_set_mac_address(bond, addr);
1691 if (res) {
1692 return res;
1693 }
1694
1695 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1696
1697 /* If there is no curr_active_slave there is nothing else to do.
1698 * Otherwise we'll need to pass the new address to it and handle
1699 * duplications.
1700 */
1701 if (!bond->curr_active_slave) {
1702 return 0;
1703 }
1704
1705 swap_slave = NULL;
1706
1707 bond_for_each_slave(bond, slave, i) {
1708 if (!memcmp(slave->dev->dev_addr, bond_dev->dev_addr, ETH_ALEN)) {
1709 swap_slave = slave;
1710 break;
1711 }
1712 }
1713
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001714 write_unlock_bh(&bond->curr_slave_lock);
1715 read_unlock(&bond->lock);
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (swap_slave) {
1718 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001719 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 } else {
1721 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1722 bond->alb_info.rlb_enabled);
1723
1724 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1725 if (bond->alb_info.rlb_enabled) {
1726 /* inform clients mac address has changed */
1727 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1728 }
1729 }
1730
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001731 read_lock(&bond->lock);
1732 write_lock_bh(&bond->curr_slave_lock);
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return 0;
1735}
1736
1737void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1738{
1739 if (bond->alb_info.current_alb_vlan &&
1740 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1741 bond->alb_info.current_alb_vlan = NULL;
1742 }
1743
1744 if (bond->alb_info.rlb_enabled) {
1745 rlb_clear_vlan(bond, vlan_id);
1746 }
1747}
1748