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