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