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