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