Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[deliverable/linux.git] / drivers / net / bonding / bond_alb.c
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 *
21 */
22
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/skbuff.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/pkt_sched.h>
29 #include <linux/spinlock.h>
30 #include <linux/slab.h>
31 #include <linux/timer.h>
32 #include <linux/ip.h>
33 #include <linux/ipv6.h>
34 #include <linux/if_arp.h>
35 #include <linux/if_ether.h>
36 #include <linux/if_bonding.h>
37 #include <linux/if_vlan.h>
38 #include <linux/in.h>
39 #include <net/ipx.h>
40 #include <net/arp.h>
41 #include <net/ipv6.h>
42 #include <asm/byteorder.h>
43 #include "bonding.h"
44 #include "bond_alb.h"
45
46
47
48 #ifndef __long_aligned
49 #define __long_aligned __attribute__((aligned((sizeof(long)))))
50 #endif
51 static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
52 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
53 };
54 static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
55 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
56 };
57 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
58
59 #pragma pack(1)
60 struct learning_pkt {
61 u8 mac_dst[ETH_ALEN];
62 u8 mac_src[ETH_ALEN];
63 __be16 type;
64 u8 padding[ETH_ZLEN - ETH_HLEN];
65 };
66
67 struct arp_pkt {
68 __be16 hw_addr_space;
69 __be16 prot_addr_space;
70 u8 hw_addr_len;
71 u8 prot_addr_len;
72 __be16 op_code;
73 u8 mac_src[ETH_ALEN]; /* sender hardware address */
74 __be32 ip_src; /* sender IP address */
75 u8 mac_dst[ETH_ALEN]; /* target hardware address */
76 __be32 ip_dst; /* target IP address */
77 };
78 #pragma pack()
79
80 static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
81 {
82 return (struct arp_pkt *)skb_network_header(skb);
83 }
84
85 /* Forward declaration */
86 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
87 static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp);
88 static void rlb_src_unlink(struct bonding *bond, u32 index);
89 static void rlb_src_link(struct bonding *bond, u32 ip_src_hash,
90 u32 ip_dst_hash);
91
92 static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
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
106 static inline void _lock_tx_hashtbl_bh(struct bonding *bond)
107 {
108 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
109 }
110
111 static inline void _unlock_tx_hashtbl_bh(struct bonding *bond)
112 {
113 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
114 }
115
116 static inline void _lock_tx_hashtbl(struct bonding *bond)
117 {
118 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
119 }
120
121 static inline void _unlock_tx_hashtbl(struct bonding *bond)
122 {
123 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
124 }
125
126 /* Caller must hold tx_hashtbl lock */
127 static 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
140 static 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
146 /* Caller must hold bond lock for read, BH disabled */
147 static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
148 int save_load)
149 {
150 struct tlb_client_info *tx_hash_table;
151 u32 index;
152
153 /* clear slave from tx_hashtbl */
154 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
155
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 }
164 }
165
166 tlb_init_slave(slave);
167 }
168
169 /* Caller must hold bond lock for read */
170 static 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);
176 }
177
178 /* Must be called before starting the monitor timer */
179 static 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);
183 struct tlb_client_info *new_hashtbl;
184 int i;
185
186 new_hashtbl = kzalloc(size, GFP_KERNEL);
187 if (!new_hashtbl)
188 return -1;
189
190 _lock_tx_hashtbl_bh(bond);
191
192 bond_info->tx_hashtbl = new_hashtbl;
193
194 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
195 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
196 }
197
198 _unlock_tx_hashtbl_bh(bond);
199
200 return 0;
201 }
202
203 /* Must be called only after all slaves have been released */
204 static void tlb_deinitialize(struct bonding *bond)
205 {
206 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
207
208 _lock_tx_hashtbl_bh(bond);
209
210 kfree(bond_info->tx_hashtbl);
211 bond_info->tx_hashtbl = NULL;
212
213 _unlock_tx_hashtbl_bh(bond);
214 }
215
216 static 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
222 /* Caller must hold bond lock for read */
223 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
224 {
225 struct slave *slave, *least_loaded;
226 long long max_gap;
227
228 least_loaded = NULL;
229 max_gap = LLONG_MIN;
230
231 /* Find the slave with the largest gap */
232 bond_for_each_slave(bond, slave) {
233 if (SLAVE_IS_OK(slave)) {
234 long long gap = compute_gap(slave);
235
236 if (max_gap < gap) {
237 least_loaded = slave;
238 max_gap = gap;
239 }
240 }
241 }
242
243 return least_loaded;
244 }
245
246 static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
247 u32 skb_len)
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
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
281 return assigned_slave;
282 }
283
284 /* Caller must hold bond lock for read */
285 static 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
300 /*********************** rlb specific functions ***************************/
301 static inline void _lock_rx_hashtbl_bh(struct bonding *bond)
302 {
303 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
304 }
305
306 static inline void _unlock_rx_hashtbl_bh(struct bonding *bond)
307 {
308 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
309 }
310
311 static inline void _lock_rx_hashtbl(struct bonding *bond)
312 {
313 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
314 }
315
316 static inline void _unlock_rx_hashtbl(struct bonding *bond)
317 {
318 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
319 }
320
321 /* when an ARP REPLY is received from a client update its info
322 * in the rx_hashtbl
323 */
324 static 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
330 _lock_rx_hashtbl_bh(bond);
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) &&
337 (client_info->ip_dst == arp->ip_src) &&
338 (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) {
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
345 _unlock_rx_hashtbl_bh(bond);
346 }
347
348 static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond,
349 struct slave *slave)
350 {
351 struct arp_pkt *arp, _arp;
352
353 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
354 goto out;
355
356 arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp);
357 if (!arp)
358 goto out;
359
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
372 if (arp->op_code == htons(ARPOP_REPLY)) {
373 /* update rx hash table for this ARP */
374 rlb_update_entry_from_arp(bond, arp);
375 pr_debug("Server received an ARP Reply from client\n");
376 }
377 out:
378 return RX_HANDLER_ANOTHER;
379 }
380
381 /* Caller must hold bond lock for read */
382 static 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
388 if (bond_info->next_rx_slave)
389 start_at = bond_info->next_rx_slave;
390 else
391 start_at = bond_first_slave(bond);
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) {
406 slave = bond_next_slave(bond, rx_slave);
407 bond_info->next_rx_slave = slave;
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 */
418 static 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) {
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;
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 */
440 static 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 */
447 _lock_rx_hashtbl_bh(bond);
448
449 rx_hash_table = bond_info->rx_hashtbl;
450 index = bond_info->rx_hashtbl_used_head;
451 for (; index != RLB_NULL_INDEX; index = next_index) {
452 next_index = rx_hash_table[index].used_next;
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;
458 if (!ether_addr_equal_64bits(rx_hash_table[index].mac_dst,
459 mac_bcast)) {
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
478 _unlock_rx_hashtbl_bh(bond);
479
480 write_lock_bh(&bond->curr_slave_lock);
481
482 if (slave != bond->curr_active_slave) {
483 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
484 }
485
486 write_unlock_bh(&bond->curr_slave_lock);
487 }
488
489 static 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) {
508 pr_err("%s: Error: failed to create an ARP packet\n",
509 client_info->slave->bond->dev->name);
510 continue;
511 }
512
513 skb->dev = client_info->slave->dev;
514
515 if (client_info->tag) {
516 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), client_info->vlan_id);
517 if (!skb) {
518 pr_err("%s: Error: failed to insert VLAN tag\n",
519 client_info->slave->bond->dev->name);
520 continue;
521 }
522 }
523
524 arp_xmit(skb);
525 }
526 }
527
528 /* sends ARP REPLIES that update the clients that need updating */
529 static 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
535 _lock_rx_hashtbl_bh(bond);
536
537 hash_index = bond_info->rx_hashtbl_used_head;
538 for (; hash_index != RLB_NULL_INDEX;
539 hash_index = client_info->used_next) {
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
549 /* do not update the entries again until this counter is zero so that
550 * not to confuse the clients.
551 */
552 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
553
554 _unlock_rx_hashtbl_bh(bond);
555 }
556
557 /* The slave was assigned a new mac address - update the clients */
558 static 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
565 _lock_rx_hashtbl_bh(bond);
566
567 hash_index = bond_info->rx_hashtbl_used_head;
568 for (; hash_index != RLB_NULL_INDEX;
569 hash_index = client_info->used_next) {
570 client_info = &(bond_info->rx_hashtbl[hash_index]);
571
572 if ((client_info->slave == slave) &&
573 !ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
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
586 _unlock_rx_hashtbl_bh(bond);
587 }
588
589 /* mark all clients using src_ip to be updated */
590 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
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
598 hash_index = bond_info->rx_hashtbl_used_head;
599 for (; hash_index != RLB_NULL_INDEX;
600 hash_index = client_info->used_next) {
601 client_info = &(bond_info->rx_hashtbl[hash_index]);
602
603 if (!client_info->slave) {
604 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
605 bond->dev->name);
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) &&
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)) {
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 */
625 static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
626 {
627 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
628 struct arp_pkt *arp = arp_pkt(skb);
629 struct slave *assigned_slave;
630 struct rlb_client_info *client_info;
631 u32 hash_index = 0;
632
633 _lock_rx_hashtbl(bond);
634
635 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
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 */
642 if (!ether_addr_equal_64bits(arp->mac_dst, mac_bcast)) {
643 /* update mac address from arp */
644 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
645 }
646 memcpy(client_info->mac_src, arp->mac_src, ETH_ALEN);
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) {
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
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);
687 memcpy(client_info->mac_src, arp->mac_src, ETH_ALEN);
688 client_info->slave = assigned_slave;
689
690 if (!ether_addr_equal_64bits(client_info->mac_dst, mac_bcast)) {
691 client_info->ntt = 1;
692 bond->alb_info.rx_ntt = 1;
693 } else {
694 client_info->ntt = 0;
695 }
696
697 if (bond_vlan_used(bond)) {
698 if (!vlan_get_tag(skb, &client_info->vlan_id))
699 client_info->tag = 1;
700 }
701
702 if (!client_info->assigned) {
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;
706 if (prev_tbl_head != RLB_NULL_INDEX) {
707 bond_info->rx_hashtbl[prev_tbl_head].used_prev =
708 hash_index;
709 }
710 client_info->assigned = 1;
711 }
712 }
713
714 _unlock_rx_hashtbl(bond);
715
716 return assigned_slave;
717 }
718
719 /* chooses (and returns) transmit channel for arp reply
720 * does not choose channel for other arp types since they are
721 * sent on the curr_active_slave
722 */
723 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
724 {
725 struct arp_pkt *arp = arp_pkt(skb);
726 struct slave *tx_slave = NULL;
727
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
734 if (arp->op_code == htons(ARPOP_REPLY)) {
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 }
742 pr_debug("Server sent ARP Reply packet\n");
743 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
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
751 /* The ARP reply packets must be delayed so that
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);
762 pr_debug("Server sent ARP Request packet\n");
763 }
764
765 return tx_slave;
766 }
767
768 /* Caller must hold bond lock for read */
769 static 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
777 _lock_rx_hashtbl_bh(bond);
778
779 ntt = 0;
780 hash_index = bond_info->rx_hashtbl_used_head;
781 for (; hash_index != RLB_NULL_INDEX;
782 hash_index = client_info->used_next) {
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 }
796 _unlock_rx_hashtbl_bh(bond);
797 }
798
799 /* Caller must hold rx_hashtbl lock */
800 static 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;
806 entry->tag = 0;
807 }
808 static 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
815 static void rlb_init_table_entry(struct rlb_client_info *entry)
816 {
817 memset(entry, 0, sizeof(struct rlb_client_info));
818 rlb_init_table_entry_dst(entry);
819 rlb_init_table_entry_src(entry);
820 }
821
822 static 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 */
837 static 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
860 static 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 */
874 static 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 */
889 static 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);
907 }
908
909 static int rlb_initialize(struct bonding *bond)
910 {
911 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
912 struct rlb_client_info *new_hashtbl;
913 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
914 int i;
915
916 new_hashtbl = kmalloc(size, GFP_KERNEL);
917 if (!new_hashtbl)
918 return -1;
919
920 _lock_rx_hashtbl_bh(bond);
921
922 bond_info->rx_hashtbl = new_hashtbl;
923
924 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
925
926 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
927 rlb_init_table_entry(bond_info->rx_hashtbl + i);
928 }
929
930 _unlock_rx_hashtbl_bh(bond);
931
932 /* register to receive ARPs */
933 bond->recv_probe = rlb_arp_recv;
934
935 return 0;
936 }
937
938 static void rlb_deinitialize(struct bonding *bond)
939 {
940 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
941
942 _lock_rx_hashtbl_bh(bond);
943
944 kfree(bond_info->rx_hashtbl);
945 bond_info->rx_hashtbl = NULL;
946 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX;
947
948 _unlock_rx_hashtbl_bh(bond);
949 }
950
951 static 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
956 _lock_rx_hashtbl_bh(bond);
957
958 curr_index = bond_info->rx_hashtbl_used_head;
959 while (curr_index != RLB_NULL_INDEX) {
960 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
961 u32 next_index = bond_info->rx_hashtbl[curr_index].used_next;
962
963 if (curr->tag && (curr->vlan_id == vlan_id))
964 rlb_delete_table_entry(bond, curr_index);
965
966 curr_index = next_index;
967 }
968
969 _unlock_rx_hashtbl_bh(bond);
970 }
971
972 /*********************** tlb/rlb shared functions *********************/
973
974 static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
975 {
976 struct bonding *bond = bond_get_bond_by_slave(slave);
977 struct learning_pkt pkt;
978 int size = sizeof(struct learning_pkt);
979 int i;
980
981 memset(&pkt, 0, size);
982 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
983 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
984 pkt.type = cpu_to_be16(ETH_P_LOOP);
985
986 for (i = 0; i < MAX_LP_BURST; i++) {
987 struct sk_buff *skb;
988 char *data;
989
990 skb = dev_alloc_skb(size);
991 if (!skb) {
992 return;
993 }
994
995 data = skb_put(skb, size);
996 memcpy(data, &pkt, size);
997
998 skb_reset_mac_header(skb);
999 skb->network_header = skb->mac_header + ETH_HLEN;
1000 skb->protocol = pkt.type;
1001 skb->priority = TC_PRIO_CONTROL;
1002 skb->dev = slave->dev;
1003
1004 if (bond_vlan_used(bond)) {
1005 struct vlan_entry *vlan;
1006
1007 vlan = bond_next_vlan(bond,
1008 bond->alb_info.current_alb_vlan);
1009
1010 bond->alb_info.current_alb_vlan = vlan;
1011 if (!vlan) {
1012 kfree_skb(skb);
1013 continue;
1014 }
1015
1016 skb = vlan_put_tag(skb, htons(ETH_P_8021Q), vlan->vlan_id);
1017 if (!skb) {
1018 pr_err("%s: Error: failed to insert VLAN tag\n",
1019 bond->dev->name);
1020 continue;
1021 }
1022 }
1023
1024 dev_queue_xmit(skb);
1025 }
1026 }
1027
1028 static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
1029 {
1030 struct net_device *dev = slave->dev;
1031 struct sockaddr s_addr;
1032
1033 if (slave->bond->params.mode == BOND_MODE_TLB) {
1034 memcpy(dev->dev_addr, addr, dev->addr_len);
1035 return 0;
1036 }
1037
1038 /* for rlb each slave must have a unique hw mac addresses so that */
1039 /* each slave will receive packets destined to a different mac */
1040 memcpy(s_addr.sa_data, addr, dev->addr_len);
1041 s_addr.sa_family = dev->type;
1042 if (dev_set_mac_address(dev, &s_addr)) {
1043 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
1044 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
1045 slave->bond->dev->name, dev->name);
1046 return -EOPNOTSUPP;
1047 }
1048 return 0;
1049 }
1050
1051 /*
1052 * Swap MAC addresses between two slaves.
1053 *
1054 * Called with RTNL held, and no other locks.
1055 *
1056 */
1057
1058 static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2)
1059 {
1060 u8 tmp_mac_addr[ETH_ALEN];
1061
1062 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
1063 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
1064 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
1065
1066 }
1067
1068 /*
1069 * Send learning packets after MAC address swap.
1070 *
1071 * Called with RTNL and no other locks
1072 */
1073 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
1074 struct slave *slave2)
1075 {
1076 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
1077 struct slave *disabled_slave = NULL;
1078
1079 ASSERT_RTNL();
1080
1081 /* fasten the change in the switch */
1082 if (SLAVE_IS_OK(slave1)) {
1083 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
1084 if (bond->alb_info.rlb_enabled) {
1085 /* inform the clients that the mac address
1086 * has changed
1087 */
1088 rlb_req_update_slave_clients(bond, slave1);
1089 }
1090 } else {
1091 disabled_slave = slave1;
1092 }
1093
1094 if (SLAVE_IS_OK(slave2)) {
1095 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1096 if (bond->alb_info.rlb_enabled) {
1097 /* inform the clients that the mac address
1098 * has changed
1099 */
1100 rlb_req_update_slave_clients(bond, slave2);
1101 }
1102 } else {
1103 disabled_slave = slave2;
1104 }
1105
1106 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1107 /* A disabled slave was assigned an active mac addr */
1108 rlb_teach_disabled_mac_on_primary(bond,
1109 disabled_slave->dev->dev_addr);
1110 }
1111 }
1112
1113 /**
1114 * alb_change_hw_addr_on_detach
1115 * @bond: bonding we're working on
1116 * @slave: the slave that was just detached
1117 *
1118 * We assume that @slave was already detached from the slave list.
1119 *
1120 * If @slave's permanent hw address is different both from its current
1121 * address and from @bond's address, then somewhere in the bond there's
1122 * a slave that has @slave's permanet address as its current address.
1123 * We'll make sure that that slave no longer uses @slave's permanent address.
1124 *
1125 * Caller must hold RTNL and no other locks
1126 */
1127 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1128 {
1129 int perm_curr_diff;
1130 int perm_bond_diff;
1131 struct slave *found_slave;
1132
1133 perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1134 slave->dev->dev_addr);
1135 perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr,
1136 bond->dev->dev_addr);
1137
1138 if (perm_curr_diff && perm_bond_diff) {
1139 found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr);
1140
1141 if (found_slave) {
1142 /* locking: needs RTNL and nothing else */
1143 alb_swap_mac_addr(slave, found_slave);
1144 alb_fasten_mac_swap(bond, slave, found_slave);
1145 }
1146 }
1147 }
1148
1149 /**
1150 * alb_handle_addr_collision_on_attach
1151 * @bond: bonding we're working on
1152 * @slave: the slave that was just attached
1153 *
1154 * checks uniqueness of slave's mac address and handles the case the
1155 * new slave uses the bonds mac address.
1156 *
1157 * If the permanent hw address of @slave is @bond's hw address, we need to
1158 * find a different hw address to give @slave, that isn't in use by any other
1159 * slave in the bond. This address must be, of course, one of the permanent
1160 * addresses of the other slaves.
1161 *
1162 * We go over the slave list, and for each slave there we compare its
1163 * permanent hw address with the current address of all the other slaves.
1164 * If no match was found, then we've found a slave with a permanent address
1165 * that isn't used by any other slave in the bond, so we can assign it to
1166 * @slave.
1167 *
1168 * assumption: this function is called before @slave is attached to the
1169 * bond slave list.
1170 */
1171 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1172 {
1173 struct slave *tmp_slave1, *free_mac_slave = NULL;
1174 struct slave *has_bond_addr = bond->curr_active_slave;
1175
1176 if (list_empty(&bond->slave_list)) {
1177 /* this is the first slave */
1178 return 0;
1179 }
1180
1181 /* if slave's mac address differs from bond's mac address
1182 * check uniqueness of slave's mac address against the other
1183 * slaves in the bond.
1184 */
1185 if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
1186 if (!bond_slave_has_mac(bond, slave->dev->dev_addr))
1187 return 0;
1188
1189 /* Try setting slave mac to bond address and fall-through
1190 to code handling that situation below... */
1191 alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
1192 }
1193
1194 /* The slave's address is equal to the address of the bond.
1195 * Search for a spare address in the bond for this slave.
1196 */
1197 bond_for_each_slave(bond, tmp_slave1) {
1198 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) {
1199 /* no slave has tmp_slave1's perm addr
1200 * as its curr addr
1201 */
1202 free_mac_slave = tmp_slave1;
1203 break;
1204 }
1205
1206 if (!has_bond_addr) {
1207 if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr,
1208 bond->dev->dev_addr)) {
1209
1210 has_bond_addr = tmp_slave1;
1211 }
1212 }
1213 }
1214
1215 if (free_mac_slave) {
1216 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
1217
1218 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
1219 bond->dev->name, slave->dev->name,
1220 free_mac_slave->dev->name);
1221
1222 } else if (has_bond_addr) {
1223 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",
1224 bond->dev->name, slave->dev->name);
1225 return -EFAULT;
1226 }
1227
1228 return 0;
1229 }
1230
1231 /**
1232 * alb_set_mac_address
1233 * @bond:
1234 * @addr:
1235 *
1236 * In TLB mode all slaves are configured to the bond's hw address, but set
1237 * their dev_addr field to different addresses (based on their permanent hw
1238 * addresses).
1239 *
1240 * For each slave, this function sets the interface to the new address and then
1241 * changes its dev_addr field to its previous value.
1242 *
1243 * Unwinding assumes bond's mac address has not yet changed.
1244 */
1245 static int alb_set_mac_address(struct bonding *bond, void *addr)
1246 {
1247 char tmp_addr[ETH_ALEN];
1248 struct slave *slave;
1249 struct sockaddr sa;
1250 int res;
1251
1252 if (bond->alb_info.rlb_enabled)
1253 return 0;
1254
1255 bond_for_each_slave(bond, slave) {
1256 /* save net_device's current hw address */
1257 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1258
1259 res = dev_set_mac_address(slave->dev, addr);
1260
1261 /* restore net_device's hw address */
1262 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1263
1264 if (res)
1265 goto unwind;
1266 }
1267
1268 return 0;
1269
1270 unwind:
1271 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1272 sa.sa_family = bond->dev->type;
1273
1274 /* unwind from head to the slave that failed */
1275 bond_for_each_slave_continue_reverse(bond, slave) {
1276 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1277 dev_set_mac_address(slave->dev, &sa);
1278 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1279 }
1280
1281 return res;
1282 }
1283
1284 /************************ exported alb funcions ************************/
1285
1286 int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1287 {
1288 int res;
1289
1290 res = tlb_initialize(bond);
1291 if (res) {
1292 return res;
1293 }
1294
1295 if (rlb_enabled) {
1296 bond->alb_info.rlb_enabled = 1;
1297 /* initialize rlb */
1298 res = rlb_initialize(bond);
1299 if (res) {
1300 tlb_deinitialize(bond);
1301 return res;
1302 }
1303 } else {
1304 bond->alb_info.rlb_enabled = 0;
1305 }
1306
1307 return 0;
1308 }
1309
1310 void bond_alb_deinitialize(struct bonding *bond)
1311 {
1312 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1313
1314 tlb_deinitialize(bond);
1315
1316 if (bond_info->rlb_enabled) {
1317 rlb_deinitialize(bond);
1318 }
1319 }
1320
1321 int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1322 {
1323 struct bonding *bond = netdev_priv(bond_dev);
1324 struct ethhdr *eth_data;
1325 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1326 struct slave *tx_slave = NULL;
1327 static const __be32 ip_bcast = htonl(0xffffffff);
1328 int hash_size = 0;
1329 int do_tx_balance = 1;
1330 u32 hash_index = 0;
1331 const u8 *hash_start = NULL;
1332 int res = 1;
1333 struct ipv6hdr *ip6hdr;
1334
1335 skb_reset_mac_header(skb);
1336 eth_data = eth_hdr(skb);
1337
1338 /* make sure that the curr_active_slave do not change during tx
1339 */
1340 read_lock(&bond->lock);
1341 read_lock(&bond->curr_slave_lock);
1342
1343 switch (ntohs(skb->protocol)) {
1344 case ETH_P_IP: {
1345 const struct iphdr *iph = ip_hdr(skb);
1346
1347 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
1348 (iph->daddr == ip_bcast) ||
1349 (iph->protocol == IPPROTO_IGMP)) {
1350 do_tx_balance = 0;
1351 break;
1352 }
1353 hash_start = (char *)&(iph->daddr);
1354 hash_size = sizeof(iph->daddr);
1355 }
1356 break;
1357 case ETH_P_IPV6:
1358 /* IPv6 doesn't really use broadcast mac address, but leave
1359 * that here just in case.
1360 */
1361 if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast)) {
1362 do_tx_balance = 0;
1363 break;
1364 }
1365
1366 /* IPv6 uses all-nodes multicast as an equivalent to
1367 * broadcasts in IPv4.
1368 */
1369 if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) {
1370 do_tx_balance = 0;
1371 break;
1372 }
1373
1374 /* Additianally, DAD probes should not be tx-balanced as that
1375 * will lead to false positives for duplicate addresses and
1376 * prevent address configuration from working.
1377 */
1378 ip6hdr = ipv6_hdr(skb);
1379 if (ipv6_addr_any(&ip6hdr->saddr)) {
1380 do_tx_balance = 0;
1381 break;
1382 }
1383
1384 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1385 hash_size = sizeof(ipv6_hdr(skb)->daddr);
1386 break;
1387 case ETH_P_IPX:
1388 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
1389 /* something is wrong with this packet */
1390 do_tx_balance = 0;
1391 break;
1392 }
1393
1394 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1395 /* The only protocol worth balancing in
1396 * this family since it has an "ARP" like
1397 * mechanism
1398 */
1399 do_tx_balance = 0;
1400 break;
1401 }
1402
1403 hash_start = (char*)eth_data->h_dest;
1404 hash_size = ETH_ALEN;
1405 break;
1406 case ETH_P_ARP:
1407 do_tx_balance = 0;
1408 if (bond_info->rlb_enabled) {
1409 tx_slave = rlb_arp_xmit(skb, bond);
1410 }
1411 break;
1412 default:
1413 do_tx_balance = 0;
1414 break;
1415 }
1416
1417 if (do_tx_balance) {
1418 hash_index = _simple_hash(hash_start, hash_size);
1419 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1420 }
1421
1422 if (!tx_slave) {
1423 /* unbalanced or unassigned, send through primary */
1424 tx_slave = bond->curr_active_slave;
1425 bond_info->unbalanced_load += skb->len;
1426 }
1427
1428 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1429 if (tx_slave != bond->curr_active_slave) {
1430 memcpy(eth_data->h_source,
1431 tx_slave->dev->dev_addr,
1432 ETH_ALEN);
1433 }
1434
1435 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1436 } else {
1437 if (tx_slave) {
1438 _lock_tx_hashtbl(bond);
1439 __tlb_clear_slave(bond, tx_slave, 0);
1440 _unlock_tx_hashtbl(bond);
1441 }
1442 }
1443
1444 read_unlock(&bond->curr_slave_lock);
1445 read_unlock(&bond->lock);
1446 if (res) {
1447 /* no suitable interface, frame not sent */
1448 kfree_skb(skb);
1449 }
1450
1451 return NETDEV_TX_OK;
1452 }
1453
1454 void bond_alb_monitor(struct work_struct *work)
1455 {
1456 struct bonding *bond = container_of(work, struct bonding,
1457 alb_work.work);
1458 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1459 struct slave *slave;
1460
1461 read_lock(&bond->lock);
1462
1463 if (list_empty(&bond->slave_list)) {
1464 bond_info->tx_rebalance_counter = 0;
1465 bond_info->lp_counter = 0;
1466 goto re_arm;
1467 }
1468
1469 bond_info->tx_rebalance_counter++;
1470 bond_info->lp_counter++;
1471
1472 /* send learning packets */
1473 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1474 /* change of curr_active_slave involves swapping of mac addresses.
1475 * in order to avoid this swapping from happening while
1476 * sending the learning packets, the curr_slave_lock must be held for
1477 * read.
1478 */
1479 read_lock(&bond->curr_slave_lock);
1480
1481 bond_for_each_slave(bond, slave)
1482 alb_send_learning_packets(slave, slave->dev->dev_addr);
1483
1484 read_unlock(&bond->curr_slave_lock);
1485
1486 bond_info->lp_counter = 0;
1487 }
1488
1489 /* rebalance tx traffic */
1490 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1491
1492 read_lock(&bond->curr_slave_lock);
1493
1494 bond_for_each_slave(bond, slave) {
1495 tlb_clear_slave(bond, slave, 1);
1496 if (slave == bond->curr_active_slave) {
1497 SLAVE_TLB_INFO(slave).load =
1498 bond_info->unbalanced_load /
1499 BOND_TLB_REBALANCE_INTERVAL;
1500 bond_info->unbalanced_load = 0;
1501 }
1502 }
1503
1504 read_unlock(&bond->curr_slave_lock);
1505
1506 bond_info->tx_rebalance_counter = 0;
1507 }
1508
1509 /* handle rlb stuff */
1510 if (bond_info->rlb_enabled) {
1511 if (bond_info->primary_is_promisc &&
1512 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1513
1514 /*
1515 * dev_set_promiscuity requires rtnl and
1516 * nothing else. Avoid race with bond_close.
1517 */
1518 read_unlock(&bond->lock);
1519 if (!rtnl_trylock()) {
1520 read_lock(&bond->lock);
1521 goto re_arm;
1522 }
1523
1524 bond_info->rlb_promisc_timeout_counter = 0;
1525
1526 /* If the primary was set to promiscuous mode
1527 * because a slave was disabled then
1528 * it can now leave promiscuous mode.
1529 */
1530 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1531 bond_info->primary_is_promisc = 0;
1532
1533 rtnl_unlock();
1534 read_lock(&bond->lock);
1535 }
1536
1537 if (bond_info->rlb_rebalance) {
1538 bond_info->rlb_rebalance = 0;
1539 rlb_rebalance(bond);
1540 }
1541
1542 /* check if clients need updating */
1543 if (bond_info->rx_ntt) {
1544 if (bond_info->rlb_update_delay_counter) {
1545 --bond_info->rlb_update_delay_counter;
1546 } else {
1547 rlb_update_rx_clients(bond);
1548 if (bond_info->rlb_update_retry_counter) {
1549 --bond_info->rlb_update_retry_counter;
1550 } else {
1551 bond_info->rx_ntt = 0;
1552 }
1553 }
1554 }
1555 }
1556
1557 re_arm:
1558 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1559
1560 read_unlock(&bond->lock);
1561 }
1562
1563 /* assumption: called before the slave is attached to the bond
1564 * and not locked by the bond lock
1565 */
1566 int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1567 {
1568 int res;
1569
1570 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
1571 if (res) {
1572 return res;
1573 }
1574
1575 res = alb_handle_addr_collision_on_attach(bond, slave);
1576 if (res) {
1577 return res;
1578 }
1579
1580 tlb_init_slave(slave);
1581
1582 /* order a rebalance ASAP */
1583 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1584
1585 if (bond->alb_info.rlb_enabled) {
1586 bond->alb_info.rlb_rebalance = 1;
1587 }
1588
1589 return 0;
1590 }
1591
1592 /*
1593 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1594 * if necessary.
1595 *
1596 * Caller must hold RTNL and no other locks
1597 */
1598 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1599 {
1600 if (!list_empty(&bond->slave_list))
1601 alb_change_hw_addr_on_detach(bond, slave);
1602
1603 tlb_clear_slave(bond, slave, 0);
1604
1605 if (bond->alb_info.rlb_enabled) {
1606 bond->alb_info.next_rx_slave = NULL;
1607 rlb_clear_slave(bond, slave);
1608 }
1609 }
1610
1611 /* Caller must hold bond lock for read */
1612 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1613 {
1614 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1615
1616 if (link == BOND_LINK_DOWN) {
1617 tlb_clear_slave(bond, slave, 0);
1618 if (bond->alb_info.rlb_enabled) {
1619 rlb_clear_slave(bond, slave);
1620 }
1621 } else if (link == BOND_LINK_UP) {
1622 /* order a rebalance ASAP */
1623 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1624 if (bond->alb_info.rlb_enabled) {
1625 bond->alb_info.rlb_rebalance = 1;
1626 /* If the updelay module parameter is smaller than the
1627 * forwarding delay of the switch the rebalance will
1628 * not work because the rebalance arp replies will
1629 * not be forwarded to the clients..
1630 */
1631 }
1632 }
1633 }
1634
1635 /**
1636 * bond_alb_handle_active_change - assign new curr_active_slave
1637 * @bond: our bonding struct
1638 * @new_slave: new slave to assign
1639 *
1640 * Set the bond->curr_active_slave to @new_slave and handle
1641 * mac address swapping and promiscuity changes as needed.
1642 *
1643 * If new_slave is NULL, caller must hold curr_slave_lock or
1644 * bond->lock for write.
1645 *
1646 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1647 * read and curr_slave_lock for write. Processing here may sleep, so
1648 * no other locks may be held.
1649 */
1650 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
1651 __releases(&bond->curr_slave_lock)
1652 __releases(&bond->lock)
1653 __acquires(&bond->lock)
1654 __acquires(&bond->curr_slave_lock)
1655 {
1656 struct slave *swap_slave;
1657
1658 if (bond->curr_active_slave == new_slave)
1659 return;
1660
1661 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1662 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1663 bond->alb_info.primary_is_promisc = 0;
1664 bond->alb_info.rlb_promisc_timeout_counter = 0;
1665 }
1666
1667 swap_slave = bond->curr_active_slave;
1668 rcu_assign_pointer(bond->curr_active_slave, new_slave);
1669
1670 if (!new_slave || list_empty(&bond->slave_list))
1671 return;
1672
1673 /* set the new curr_active_slave to the bonds mac address
1674 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1675 */
1676 if (!swap_slave)
1677 swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr);
1678
1679 /*
1680 * Arrange for swap_slave and new_slave to temporarily be
1681 * ignored so we can mess with their MAC addresses without
1682 * fear of interference from transmit activity.
1683 */
1684 if (swap_slave)
1685 tlb_clear_slave(bond, swap_slave, 1);
1686 tlb_clear_slave(bond, new_slave, 1);
1687
1688 write_unlock_bh(&bond->curr_slave_lock);
1689 read_unlock(&bond->lock);
1690
1691 ASSERT_RTNL();
1692
1693 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1694 if (swap_slave) {
1695 /* swap mac address */
1696 alb_swap_mac_addr(swap_slave, new_slave);
1697 alb_fasten_mac_swap(bond, swap_slave, new_slave);
1698 read_lock(&bond->lock);
1699 } else {
1700 /* set the new_slave to the bond mac address */
1701 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
1702 read_lock(&bond->lock);
1703 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1704 }
1705
1706 write_lock_bh(&bond->curr_slave_lock);
1707 }
1708
1709 /*
1710 * Called with RTNL
1711 */
1712 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
1713 __acquires(&bond->lock)
1714 __releases(&bond->lock)
1715 {
1716 struct bonding *bond = netdev_priv(bond_dev);
1717 struct sockaddr *sa = addr;
1718 struct slave *swap_slave;
1719 int res;
1720
1721 if (!is_valid_ether_addr(sa->sa_data)) {
1722 return -EADDRNOTAVAIL;
1723 }
1724
1725 res = alb_set_mac_address(bond, addr);
1726 if (res) {
1727 return res;
1728 }
1729
1730 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1731
1732 /* If there is no curr_active_slave there is nothing else to do.
1733 * Otherwise we'll need to pass the new address to it and handle
1734 * duplications.
1735 */
1736 if (!bond->curr_active_slave) {
1737 return 0;
1738 }
1739
1740 swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr);
1741
1742 if (swap_slave) {
1743 alb_swap_mac_addr(swap_slave, bond->curr_active_slave);
1744 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
1745 } else {
1746 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr);
1747
1748 read_lock(&bond->lock);
1749 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1750 if (bond->alb_info.rlb_enabled) {
1751 /* inform clients mac address has changed */
1752 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1753 }
1754 read_unlock(&bond->lock);
1755 }
1756
1757 return 0;
1758 }
1759
1760 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1761 {
1762 if (bond->alb_info.current_alb_vlan &&
1763 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1764 bond->alb_info.current_alb_vlan = NULL;
1765 }
1766
1767 if (bond->alb_info.rlb_enabled) {
1768 rlb_clear_vlan(bond, vlan_id);
1769 }
1770 }
1771
This page took 0.090111 seconds and 5 git commands to generate.