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