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