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