blob: 9abfde4793166f8d323ad9a229d7a1ec09ab2fab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
Joe Perchesa4aee5c2009-12-13 20:06:07 -080023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Vlad Yasevich2d1ea192008-08-28 15:38:41 -040041#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/byteorder.h>
43#include "bonding.h"
44#include "bond_alb.h"
45
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Eric Dumazet885a1362009-09-01 06:31:18 +000048#ifndef __long_aligned
49#define __long_aligned __attribute__((aligned((sizeof(long)))))
50#endif
51static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
52 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
53};
54static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
55 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
56};
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
58
59#pragma pack(1)
60struct learning_pkt {
61 u8 mac_dst[ETH_ALEN];
62 u8 mac_src[ETH_ALEN];
Al Virod3bb52b2007-08-22 20:06:58 -040063 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 u8 padding[ETH_ZLEN - ETH_HLEN];
65};
66
67struct arp_pkt {
Al Virod3bb52b2007-08-22 20:06:58 -040068 __be16 hw_addr_space;
69 __be16 prot_addr_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 u8 hw_addr_len;
71 u8 prot_addr_len;
Al Virod3bb52b2007-08-22 20:06:58 -040072 __be16 op_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 u8 mac_src[ETH_ALEN]; /* sender hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -040074 __be32 ip_src; /* sender IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u8 mac_dst[ETH_ALEN]; /* target hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -040076 __be32 ip_dst; /* target IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78#pragma pack()
79
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -030080static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
81{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070082 return (struct arp_pkt *)skb_network_header(skb);
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -030083}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* Forward declaration */
86static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
87
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070088static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 int i;
91 u8 hash = 0;
92
93 for (i = 0; i < hash_size; i++) {
94 hash ^= hash_start[i];
95 }
96
97 return hash;
98}
99
100/*********************** tlb specific functions ***************************/
101
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000102static inline void _lock_tx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700104 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000107static inline void _unlock_tx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700109 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000112static inline void _lock_tx_hashtbl(struct bonding *bond)
113{
114 spin_lock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
115}
116
117static inline void _unlock_tx_hashtbl(struct bonding *bond)
118{
119 spin_unlock(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* Caller must hold tx_hashtbl lock */
123static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
124{
125 if (save_load) {
126 entry->load_history = 1 + entry->tx_bytes /
127 BOND_TLB_REBALANCE_INTERVAL;
128 entry->tx_bytes = 0;
129 }
130
131 entry->tx_slave = NULL;
132 entry->next = TLB_NULL_INDEX;
133 entry->prev = TLB_NULL_INDEX;
134}
135
136static inline void tlb_init_slave(struct slave *slave)
137{
138 SLAVE_TLB_INFO(slave).load = 0;
139 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
140}
141
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000142/* Caller must hold bond lock for read, BH disabled */
143static void __tlb_clear_slave(struct bonding *bond, struct slave *slave,
144 int save_load)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 struct tlb_client_info *tx_hash_table;
147 u32 index;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /* clear slave from tx_hashtbl */
150 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
151
Andy Gospodarekce39a802008-10-30 17:41:16 -0700152 /* skip this if we've already freed the tx hash table */
153 if (tx_hash_table) {
154 index = SLAVE_TLB_INFO(slave).head;
155 while (index != TLB_NULL_INDEX) {
156 u32 next_index = tx_hash_table[index].next;
157 tlb_init_table_entry(&tx_hash_table[index], save_load);
158 index = next_index;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 tlb_init_slave(slave);
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000163}
Jay Vosburgh5af47b22006-01-09 12:14:00 -0800164
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000165/* Caller must hold bond lock for read */
166static void tlb_clear_slave(struct bonding *bond, struct slave *slave,
167 int save_load)
168{
169 _lock_tx_hashtbl_bh(bond);
170 __tlb_clear_slave(bond, slave, save_load);
171 _unlock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174/* Must be called before starting the monitor timer */
175static int tlb_initialize(struct bonding *bond)
176{
177 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
178 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
Mitch Williams0d206a32005-11-09 10:35:30 -0800179 struct tlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int i;
181
Joe Jin243cb4e2007-02-06 14:16:40 -0800182 new_hashtbl = kzalloc(size, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000183 if (!new_hashtbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -1;
Joe Perchese404dec2012-01-29 12:56:23 +0000185
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000186 _lock_tx_hashtbl_bh(bond);
Mitch Williams0d206a32005-11-09 10:35:30 -0800187
188 bond_info->tx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
Peter Pan(潘卫平)38dbaf02011-04-08 03:40:19 +0000191 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000194 _unlock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 return 0;
197}
198
199/* Must be called only after all slaves have been released */
200static void tlb_deinitialize(struct bonding *bond)
201{
202 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
203
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000204 _lock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 kfree(bond_info->tx_hashtbl);
207 bond_info->tx_hashtbl = NULL;
208
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000209 _unlock_tx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Jiri Pirko097811b2010-05-19 03:26:39 +0000212static long long compute_gap(struct slave *slave)
213{
214 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
215 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/* Caller must hold bond lock for read */
219static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
220{
221 struct slave *slave, *least_loaded;
Jiri Pirko097811b2010-05-19 03:26:39 +0000222 long long max_gap;
223 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Jiri Pirko097811b2010-05-19 03:26:39 +0000225 least_loaded = NULL;
226 max_gap = LLONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* Find the slave with the largest gap */
Jiri Pirko097811b2010-05-19 03:26:39 +0000229 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (SLAVE_IS_OK(slave)) {
Jiri Pirko097811b2010-05-19 03:26:39 +0000231 long long gap = compute_gap(slave);
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (max_gap < gap) {
234 least_loaded = slave;
235 max_gap = gap;
236 }
237 }
238 }
239
240 return least_loaded;
241}
242
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000243static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index,
244 u32 skb_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
247 struct tlb_client_info *hash_table;
248 struct slave *assigned_slave;
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 hash_table = bond_info->tx_hashtbl;
251 assigned_slave = hash_table[hash_index].tx_slave;
252 if (!assigned_slave) {
253 assigned_slave = tlb_get_least_loaded_slave(bond);
254
255 if (assigned_slave) {
256 struct tlb_slave_info *slave_info =
257 &(SLAVE_TLB_INFO(assigned_slave));
258 u32 next_index = slave_info->head;
259
260 hash_table[hash_index].tx_slave = assigned_slave;
261 hash_table[hash_index].next = next_index;
262 hash_table[hash_index].prev = TLB_NULL_INDEX;
263
264 if (next_index != TLB_NULL_INDEX) {
265 hash_table[next_index].prev = hash_index;
266 }
267
268 slave_info->head = hash_index;
269 slave_info->load +=
270 hash_table[hash_index].load_history;
271 }
272 }
273
274 if (assigned_slave) {
275 hash_table[hash_index].tx_bytes += skb_len;
276 }
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return assigned_slave;
279}
280
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000281/* Caller must hold bond lock for read */
282static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index,
283 u32 skb_len)
284{
285 struct slave *tx_slave;
286 /*
287 * We don't need to disable softirq here, becase
288 * tlb_choose_channel() is only called by bond_alb_xmit()
289 * which already has softirq disabled.
290 */
291 _lock_tx_hashtbl(bond);
292 tx_slave = __tlb_choose_channel(bond, hash_index, skb_len);
293 _unlock_tx_hashtbl(bond);
294 return tx_slave;
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*********************** rlb specific functions ***************************/
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000298static inline void _lock_rx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700300 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000303static inline void _unlock_rx_hashtbl_bh(struct bonding *bond)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700305 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000308static inline void _lock_rx_hashtbl(struct bonding *bond)
309{
310 spin_lock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
311}
312
313static inline void _unlock_rx_hashtbl(struct bonding *bond)
314{
315 spin_unlock(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/* when an ARP REPLY is received from a client update its info
319 * in the rx_hashtbl
320 */
321static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
322{
323 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
324 struct rlb_client_info *client_info;
325 u32 hash_index;
326
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000327 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
330 client_info = &(bond_info->rx_hashtbl[hash_index]);
331
332 if ((client_info->assigned) &&
333 (client_info->ip_src == arp->ip_dst) &&
Flavio Leitner42d782a2010-06-29 08:24:39 +0000334 (client_info->ip_dst == arp->ip_src) &&
335 (compare_ether_addr_64bits(client_info->mac_dst, arp->mac_src))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* update the clients MAC address */
337 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
338 client_info->ntt = 1;
339 bond_info->rx_ntt = 1;
340 }
341
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000342 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Jiri Pirko3aba8912011-04-19 03:48:16 +0000345static void rlb_arp_recv(struct sk_buff *skb, struct bonding *bond,
346 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Jiri Pirko3aba8912011-04-19 03:48:16 +0000348 struct arp_pkt *arp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Jiri Pirko3aba8912011-04-19 03:48:16 +0000350 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
351 return;
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800352
Jiri Pirko3aba8912011-04-19 03:48:16 +0000353 arp = (struct arp_pkt *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!arp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800355 pr_debug("Packet has no ARP data\n");
Jiri Pirko3aba8912011-04-19 03:48:16 +0000356 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
Jiri Pirko3aba8912011-04-19 03:48:16 +0000359 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
360 return;
Andy Gospodarekab128112010-09-10 11:43:20 +0000361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (skb->len < sizeof(struct arp_pkt)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800363 pr_debug("Packet is too small to be an ARP\n");
Jiri Pirko3aba8912011-04-19 03:48:16 +0000364 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
367 if (arp->op_code == htons(ARPOP_REPLY)) {
368 /* update rx hash table for this ARP */
369 rlb_update_entry_from_arp(bond, arp);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800370 pr_debug("Server received an ARP Reply from client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/* Caller must hold bond lock for read */
375static struct slave *rlb_next_rx_slave(struct bonding *bond)
376{
377 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
378 struct slave *rx_slave, *slave, *start_at;
379 int i = 0;
380
381 if (bond_info->next_rx_slave) {
382 start_at = bond_info->next_rx_slave;
383 } else {
384 start_at = bond->first_slave;
385 }
386
387 rx_slave = NULL;
388
389 bond_for_each_slave_from(bond, slave, i, start_at) {
390 if (SLAVE_IS_OK(slave)) {
391 if (!rx_slave) {
392 rx_slave = slave;
393 } else if (slave->speed > rx_slave->speed) {
394 rx_slave = slave;
395 }
396 }
397 }
398
399 if (rx_slave) {
400 bond_info->next_rx_slave = rx_slave->next;
401 }
402
403 return rx_slave;
404}
405
406/* teach the switch the mac of a disabled slave
407 * on the primary for fault tolerance
408 *
409 * Caller must hold bond->curr_slave_lock for write or bond lock for write
410 */
411static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
412{
413 if (!bond->curr_active_slave) {
414 return;
415 }
416
417 if (!bond->alb_info.primary_is_promisc) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700418 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
419 bond->alb_info.primary_is_promisc = 1;
420 else
421 bond->alb_info.primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
424 bond->alb_info.rlb_promisc_timeout_counter = 0;
425
426 alb_send_learning_packets(bond->curr_active_slave, addr);
427}
428
429/* slave being removed should not be active at this point
430 *
431 * Caller must hold bond lock for read
432 */
433static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
434{
435 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
436 struct rlb_client_info *rx_hash_table;
437 u32 index, next_index;
438
439 /* clear slave from rx_hashtbl */
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000440 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 rx_hash_table = bond_info->rx_hashtbl;
443 index = bond_info->rx_hashtbl_head;
444 for (; index != RLB_NULL_INDEX; index = next_index) {
445 next_index = rx_hash_table[index].next;
446 if (rx_hash_table[index].slave == slave) {
447 struct slave *assigned_slave = rlb_next_rx_slave(bond);
448
449 if (assigned_slave) {
450 rx_hash_table[index].slave = assigned_slave;
Eric Dumazet885a1362009-09-01 06:31:18 +0000451 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
452 mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 bond_info->rx_hashtbl[index].ntt = 1;
454 bond_info->rx_ntt = 1;
455 /* A slave has been removed from the
456 * table because it is either disabled
457 * or being released. We must retry the
458 * update to avoid clients from not
459 * being updated & disconnecting when
460 * there is stress
461 */
462 bond_info->rlb_update_retry_counter =
463 RLB_UPDATE_RETRY;
464 }
465 } else { /* there is no active slave */
466 rx_hash_table[index].slave = NULL;
467 }
468 }
469 }
470
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000471 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700473 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (slave != bond->curr_active_slave) {
476 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
477 }
478
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700479 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482static void rlb_update_client(struct rlb_client_info *client_info)
483{
484 int i;
485
486 if (!client_info->slave) {
487 return;
488 }
489
490 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
491 struct sk_buff *skb;
492
493 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
494 client_info->ip_dst,
495 client_info->slave->dev,
496 client_info->ip_src,
497 client_info->mac_dst,
498 client_info->slave->dev->dev_addr,
499 client_info->mac_dst);
500 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800501 pr_err("%s: Error: failed to create an ARP packet\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800502 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 continue;
504 }
505
506 skb->dev = client_info->slave->dev;
507
508 if (client_info->tag) {
509 skb = vlan_put_tag(skb, client_info->vlan_id);
510 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800511 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800512 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 continue;
514 }
515 }
516
517 arp_xmit(skb);
518 }
519}
520
521/* sends ARP REPLIES that update the clients that need updating */
522static void rlb_update_rx_clients(struct bonding *bond)
523{
524 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
525 struct rlb_client_info *client_info;
526 u32 hash_index;
527
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000528 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 hash_index = bond_info->rx_hashtbl_head;
531 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
532 client_info = &(bond_info->rx_hashtbl[hash_index]);
533 if (client_info->ntt) {
534 rlb_update_client(client_info);
535 if (bond_info->rlb_update_retry_counter == 0) {
536 client_info->ntt = 0;
537 }
538 }
539 }
540
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200541 /* do not update the entries again until this counter is zero so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * not to confuse the clients.
543 */
544 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
545
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000546 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/* The slave was assigned a new mac address - update the clients */
550static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
551{
552 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
553 struct rlb_client_info *client_info;
554 int ntt = 0;
555 u32 hash_index;
556
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000557 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 hash_index = bond_info->rx_hashtbl_head;
560 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
561 client_info = &(bond_info->rx_hashtbl[hash_index]);
562
563 if ((client_info->slave == slave) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000564 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 client_info->ntt = 1;
566 ntt = 1;
567 }
568 }
569
570 // update the team's flag only after the whole iteration
571 if (ntt) {
572 bond_info->rx_ntt = 1;
573 //fasten the change
574 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
575 }
576
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000577 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400581static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
584 struct rlb_client_info *client_info;
585 u32 hash_index;
586
587 _lock_rx_hashtbl(bond);
588
589 hash_index = bond_info->rx_hashtbl_head;
590 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
591 client_info = &(bond_info->rx_hashtbl[hash_index]);
592
593 if (!client_info->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800594 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800595 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 continue;
597 }
598 /*update all clients using this src_ip, that are not assigned
599 * to the team's address (curr_active_slave) and have a known
600 * unicast mac address.
601 */
602 if ((client_info->ip_src == src_ip) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000603 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
604 bond->dev->dev_addr) &&
605 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 client_info->ntt = 1;
607 bond_info->rx_ntt = 1;
608 }
609 }
610
611 _unlock_rx_hashtbl(bond);
612}
613
614/* Caller must hold both bond and ptr locks for read */
615static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
616{
617 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300618 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct slave *assigned_slave;
620 struct rlb_client_info *client_info;
621 u32 hash_index = 0;
622
623 _lock_rx_hashtbl(bond);
624
Amerigo Wange364a342011-02-27 23:34:28 +0000625 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 client_info = &(bond_info->rx_hashtbl[hash_index]);
627
628 if (client_info->assigned) {
629 if ((client_info->ip_src == arp->ip_src) &&
630 (client_info->ip_dst == arp->ip_dst)) {
631 /* the entry is already assigned to this client */
Eric Dumazet885a1362009-09-01 06:31:18 +0000632 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /* update mac address from arp */
634 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
635 }
636
637 assigned_slave = client_info->slave;
638 if (assigned_slave) {
639 _unlock_rx_hashtbl(bond);
640 return assigned_slave;
641 }
642 } else {
643 /* the entry is already assigned to some other client,
644 * move the old client to primary (curr_active_slave) so
645 * that the new client can be assigned to this entry.
646 */
647 if (bond->curr_active_slave &&
648 client_info->slave != bond->curr_active_slave) {
649 client_info->slave = bond->curr_active_slave;
650 rlb_update_client(client_info);
651 }
652 }
653 }
654 /* assign a new slave */
655 assigned_slave = rlb_next_rx_slave(bond);
656
657 if (assigned_slave) {
658 client_info->ip_src = arp->ip_src;
659 client_info->ip_dst = arp->ip_dst;
660 /* arp->mac_dst is broadcast for arp reqeusts.
661 * will be updated with clients actual unicast mac address
662 * upon receiving an arp reply.
663 */
664 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
665 client_info->slave = assigned_slave;
666
Eric Dumazet885a1362009-09-01 06:31:18 +0000667 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 client_info->ntt = 1;
669 bond->alb_info.rx_ntt = 1;
670 } else {
671 client_info->ntt = 0;
672 }
673
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000674 if (bond_vlan_used(bond)) {
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700675 if (!vlan_get_tag(skb, &client_info->vlan_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 client_info->tag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 if (!client_info->assigned) {
680 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
681 bond_info->rx_hashtbl_head = hash_index;
682 client_info->next = prev_tbl_head;
683 if (prev_tbl_head != RLB_NULL_INDEX) {
684 bond_info->rx_hashtbl[prev_tbl_head].prev =
685 hash_index;
686 }
687 client_info->assigned = 1;
688 }
689 }
690
691 _unlock_rx_hashtbl(bond);
692
693 return assigned_slave;
694}
695
696/* chooses (and returns) transmit channel for arp reply
697 * does not choose channel for other arp types since they are
698 * sent on the curr_active_slave
699 */
700static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
701{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300702 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct slave *tx_slave = NULL;
704
Brian Haleyf14c4e42008-09-02 10:08:08 -0400705 if (arp->op_code == htons(ARPOP_REPLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /* the arp must be sent on the selected
707 * rx channel
708 */
709 tx_slave = rlb_choose_channel(skb, bond);
710 if (tx_slave) {
711 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
712 }
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800713 pr_debug("Server sent ARP Reply packet\n");
Brian Haleyf14c4e42008-09-02 10:08:08 -0400714 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 /* Create an entry in the rx_hashtbl for this client as a
716 * place holder.
717 * When the arp reply is received the entry will be updated
718 * with the correct unicast address of the client.
719 */
720 rlb_choose_channel(skb, bond);
721
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +0000722 /* The ARP reply packets must be delayed so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * they can cancel out the influence of the ARP request.
724 */
725 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
726
727 /* arp requests are broadcast and are sent on the primary
728 * the arp request will collapse all clients on the subnet to
729 * the primary slave. We must register these clients to be
730 * updated with their assigned mac.
731 */
732 rlb_req_update_subnet_clients(bond, arp->ip_src);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800733 pr_debug("Server sent ARP Request packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
736 return tx_slave;
737}
738
739/* Caller must hold bond lock for read */
740static void rlb_rebalance(struct bonding *bond)
741{
742 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
743 struct slave *assigned_slave;
744 struct rlb_client_info *client_info;
745 int ntt;
746 u32 hash_index;
747
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000748 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 ntt = 0;
751 hash_index = bond_info->rx_hashtbl_head;
752 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
753 client_info = &(bond_info->rx_hashtbl[hash_index]);
754 assigned_slave = rlb_next_rx_slave(bond);
755 if (assigned_slave && (client_info->slave != assigned_slave)) {
756 client_info->slave = assigned_slave;
757 client_info->ntt = 1;
758 ntt = 1;
759 }
760 }
761
762 /* update the team's flag only after the whole iteration */
763 if (ntt) {
764 bond_info->rx_ntt = 1;
765 }
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000766 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769/* Caller must hold rx_hashtbl lock */
770static void rlb_init_table_entry(struct rlb_client_info *entry)
771{
772 memset(entry, 0, sizeof(struct rlb_client_info));
773 entry->next = RLB_NULL_INDEX;
774 entry->prev = RLB_NULL_INDEX;
775}
776
777static int rlb_initialize(struct bonding *bond)
778{
779 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mitch Williams0d206a32005-11-09 10:35:30 -0800780 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
782 int i;
783
Mitch Williams0d206a32005-11-09 10:35:30 -0800784 new_hashtbl = kmalloc(size, GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000785 if (!new_hashtbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -1;
Joe Perchese404dec2012-01-29 12:56:23 +0000787
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000788 _lock_rx_hashtbl_bh(bond);
Mitch Williams0d206a32005-11-09 10:35:30 -0800789
790 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
793
794 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
795 rlb_init_table_entry(bond_info->rx_hashtbl + i);
796 }
797
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000798 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* register to receive ARPs */
Jiri Pirko3aba8912011-04-19 03:48:16 +0000801 bond->recv_probe = rlb_arp_recv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 return 0;
804}
805
806static void rlb_deinitialize(struct bonding *bond)
807{
808 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
809
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000810 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 kfree(bond_info->rx_hashtbl);
813 bond_info->rx_hashtbl = NULL;
814 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
815
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000816 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
820{
821 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
822 u32 curr_index;
823
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000824 _lock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 curr_index = bond_info->rx_hashtbl_head;
827 while (curr_index != RLB_NULL_INDEX) {
828 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
829 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
830 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
831
832 if (curr->tag && (curr->vlan_id == vlan_id)) {
833 if (curr_index == bond_info->rx_hashtbl_head) {
834 bond_info->rx_hashtbl_head = next_index;
835 }
836 if (prev_index != RLB_NULL_INDEX) {
837 bond_info->rx_hashtbl[prev_index].next = next_index;
838 }
839 if (next_index != RLB_NULL_INDEX) {
840 bond_info->rx_hashtbl[next_index].prev = prev_index;
841 }
842
843 rlb_init_table_entry(curr);
844 }
845
846 curr_index = next_index;
847 }
848
Maxim Uvarovf515e6b2012-01-09 12:01:37 +0000849 _unlock_rx_hashtbl_bh(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852/*********************** tlb/rlb shared functions *********************/
853
854static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
855{
856 struct bonding *bond = bond_get_bond_by_slave(slave);
857 struct learning_pkt pkt;
858 int size = sizeof(struct learning_pkt);
859 int i;
860
861 memset(&pkt, 0, size);
862 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
863 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
Harvey Harrison09640e62009-02-01 00:45:17 -0800864 pkt.type = cpu_to_be16(ETH_P_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 for (i = 0; i < MAX_LP_BURST; i++) {
867 struct sk_buff *skb;
868 char *data;
869
870 skb = dev_alloc_skb(size);
871 if (!skb) {
872 return;
873 }
874
875 data = skb_put(skb, size);
876 memcpy(data, &pkt, size);
877
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700878 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700879 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 skb->protocol = pkt.type;
881 skb->priority = TC_PRIO_CONTROL;
882 skb->dev = slave->dev;
883
Jiri Pirkocc0e4072011-07-20 04:54:46 +0000884 if (bond_vlan_used(bond)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct vlan_entry *vlan;
886
887 vlan = bond_next_vlan(bond,
888 bond->alb_info.current_alb_vlan);
889
890 bond->alb_info.current_alb_vlan = vlan;
891 if (!vlan) {
892 kfree_skb(skb);
893 continue;
894 }
895
896 skb = vlan_put_tag(skb, vlan->vlan_id);
897 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800898 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800899 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 continue;
901 }
902 }
903
904 dev_queue_xmit(skb);
905 }
906}
907
Jiri Bohacb9245512012-01-18 12:24:54 +0000908static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct net_device *dev = slave->dev;
911 struct sockaddr s_addr;
912
Jiri Bohacb9245512012-01-18 12:24:54 +0000913 if (slave->bond->params.mode == BOND_MODE_TLB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 memcpy(dev->dev_addr, addr, dev->addr_len);
915 return 0;
916 }
917
918 /* for rlb each slave must have a unique hw mac addresses so that */
919 /* each slave will receive packets destined to a different mac */
920 memcpy(s_addr.sa_data, addr, dev->addr_len);
921 s_addr.sa_family = dev->type;
922 if (dev_set_mac_address(dev, &s_addr)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800923 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
924 "ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800925 dev->master->name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EOPNOTSUPP;
927 }
928 return 0;
929}
930
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700931/*
932 * Swap MAC addresses between two slaves.
933 *
934 * Called with RTNL held, and no other locks.
935 *
936 */
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
939{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
Jiri Bohacb9245512012-01-18 12:24:54 +0000943 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr);
944 alb_set_slave_mac_addr(slave2, tmp_mac_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700946}
947
948/*
949 * Send learning packets after MAC address swap.
950 *
Jay Vosburgh25433312008-01-17 16:24:59 -0800951 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700952 */
953static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
954 struct slave *slave2)
955{
956 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
957 struct slave *disabled_slave = NULL;
958
Jay Vosburgh25433312008-01-17 16:24:59 -0800959 ASSERT_RTNL();
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* fasten the change in the switch */
962 if (SLAVE_IS_OK(slave1)) {
963 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
964 if (bond->alb_info.rlb_enabled) {
965 /* inform the clients that the mac address
966 * has changed
967 */
968 rlb_req_update_slave_clients(bond, slave1);
969 }
970 } else {
971 disabled_slave = slave1;
972 }
973
974 if (SLAVE_IS_OK(slave2)) {
975 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
976 if (bond->alb_info.rlb_enabled) {
977 /* inform the clients that the mac address
978 * has changed
979 */
980 rlb_req_update_slave_clients(bond, slave2);
981 }
982 } else {
983 disabled_slave = slave2;
984 }
985
986 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
987 /* A disabled slave was assigned an active mac addr */
988 rlb_teach_disabled_mac_on_primary(bond,
989 disabled_slave->dev->dev_addr);
990 }
991}
992
993/**
994 * alb_change_hw_addr_on_detach
995 * @bond: bonding we're working on
996 * @slave: the slave that was just detached
997 *
998 * We assume that @slave was already detached from the slave list.
999 *
1000 * If @slave's permanent hw address is different both from its current
1001 * address and from @bond's address, then somewhere in the bond there's
1002 * a slave that has @slave's permanet address as its current address.
1003 * We'll make sure that that slave no longer uses @slave's permanent address.
1004 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001005 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
1007static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1008{
1009 int perm_curr_diff;
1010 int perm_bond_diff;
1011
Eric Dumazet885a1362009-09-01 06:31:18 +00001012 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1013 slave->dev->dev_addr);
1014 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1015 bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (perm_curr_diff && perm_bond_diff) {
1018 struct slave *tmp_slave;
1019 int i, found = 0;
1020
1021 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001022 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
1023 tmp_slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 found = 1;
1025 break;
1026 }
1027 }
1028
1029 if (found) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001030 /* locking: needs RTNL and nothing else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 alb_swap_mac_addr(bond, slave, tmp_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001032 alb_fasten_mac_swap(bond, slave, tmp_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 }
1035}
1036
1037/**
1038 * alb_handle_addr_collision_on_attach
1039 * @bond: bonding we're working on
1040 * @slave: the slave that was just attached
1041 *
1042 * checks uniqueness of slave's mac address and handles the case the
1043 * new slave uses the bonds mac address.
1044 *
1045 * If the permanent hw address of @slave is @bond's hw address, we need to
1046 * find a different hw address to give @slave, that isn't in use by any other
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +00001047 * slave in the bond. This address must be, of course, one of the permanent
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * addresses of the other slaves.
1049 *
1050 * We go over the slave list, and for each slave there we compare its
1051 * permanent hw address with the current address of all the other slaves.
1052 * If no match was found, then we've found a slave with a permanent address
1053 * that isn't used by any other slave in the bond, so we can assign it to
1054 * @slave.
1055 *
1056 * assumption: this function is called before @slave is attached to the
1057 * bond slave list.
1058 *
1059 * caller must hold the bond lock for write since the mac addresses are compared
1060 * and may be swapped.
1061 */
1062static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1063{
1064 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1065 struct slave *has_bond_addr = bond->curr_active_slave;
1066 int i, j, found = 0;
1067
1068 if (bond->slave_cnt == 0) {
1069 /* this is the first slave */
1070 return 0;
1071 }
1072
1073 /* if slave's mac address differs from bond's mac address
1074 * check uniqueness of slave's mac address against the other
1075 * slaves in the bond.
1076 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001077 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 bond_for_each_slave(bond, tmp_slave1, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001079 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1080 slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 found = 1;
1082 break;
1083 }
1084 }
1085
John W. Linville6b38aef2005-07-28 15:00:15 -04001086 if (!found)
1087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
John W. Linville6b38aef2005-07-28 15:00:15 -04001089 /* Try setting slave mac to bond address and fall-through
1090 to code handling that situation below... */
Jiri Bohacb9245512012-01-18 12:24:54 +00001091 alb_set_slave_mac_addr(slave, bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
1094 /* The slave's address is equal to the address of the bond.
1095 * Search for a spare address in the bond for this slave.
1096 */
1097 free_mac_slave = NULL;
1098
1099 bond_for_each_slave(bond, tmp_slave1, i) {
1100 found = 0;
1101 bond_for_each_slave(bond, tmp_slave2, j) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001102 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1103 tmp_slave2->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 found = 1;
1105 break;
1106 }
1107 }
1108
1109 if (!found) {
1110 /* no slave has tmp_slave1's perm addr
1111 * as its curr addr
1112 */
1113 free_mac_slave = tmp_slave1;
1114 break;
1115 }
1116
1117 if (!has_bond_addr) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001118 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1119 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 has_bond_addr = tmp_slave1;
1122 }
1123 }
1124 }
1125
1126 if (free_mac_slave) {
Jiri Bohacb9245512012-01-18 12:24:54 +00001127 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001129 pr_warning("%s: Warning: the hw address of slave %s is in use by the bond; giving it the hw address of %s\n",
Jiri Pirkoe5e2a8f2009-08-13 04:11:52 +00001130 bond->dev->name, slave->dev->name,
1131 free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 } else if (has_bond_addr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001134 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",
Mitch Williams4e0952c2005-11-09 10:34:57 -08001135 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -EFAULT;
1137 }
1138
1139 return 0;
1140}
1141
1142/**
1143 * alb_set_mac_address
1144 * @bond:
1145 * @addr:
1146 *
1147 * In TLB mode all slaves are configured to the bond's hw address, but set
1148 * their dev_addr field to different addresses (based on their permanent hw
1149 * addresses).
1150 *
1151 * For each slave, this function sets the interface to the new address and then
1152 * changes its dev_addr field to its previous value.
1153 *
1154 * Unwinding assumes bond's mac address has not yet changed.
1155 */
1156static int alb_set_mac_address(struct bonding *bond, void *addr)
1157{
1158 struct sockaddr sa;
1159 struct slave *slave, *stop_at;
1160 char tmp_addr[ETH_ALEN];
1161 int res;
1162 int i;
1163
1164 if (bond->alb_info.rlb_enabled) {
1165 return 0;
1166 }
1167
1168 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* save net_device's current hw address */
1170 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1171
1172 res = dev_set_mac_address(slave->dev, addr);
1173
1174 /* restore net_device's hw address */
1175 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1176
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001177 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
1181 return 0;
1182
1183unwind:
1184 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1185 sa.sa_family = bond->dev->type;
1186
1187 /* unwind from head to the slave that failed */
1188 stop_at = slave;
1189 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1190 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1191 dev_set_mac_address(slave->dev, &sa);
1192 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1193 }
1194
1195 return res;
1196}
1197
1198/************************ exported alb funcions ************************/
1199
1200int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1201{
1202 int res;
1203
1204 res = tlb_initialize(bond);
1205 if (res) {
1206 return res;
1207 }
1208
1209 if (rlb_enabled) {
1210 bond->alb_info.rlb_enabled = 1;
1211 /* initialize rlb */
1212 res = rlb_initialize(bond);
1213 if (res) {
1214 tlb_deinitialize(bond);
1215 return res;
1216 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001217 } else {
1218 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 return 0;
1222}
1223
1224void bond_alb_deinitialize(struct bonding *bond)
1225{
1226 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1227
1228 tlb_deinitialize(bond);
1229
1230 if (bond_info->rlb_enabled) {
1231 rlb_deinitialize(bond);
1232 }
1233}
1234
1235int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1236{
Wang Chen454d7c92008-11-12 23:37:49 -08001237 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct ethhdr *eth_data;
1239 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1240 struct slave *tx_slave = NULL;
Al Virod3bb52b2007-08-22 20:06:58 -04001241 static const __be32 ip_bcast = htonl(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int hash_size = 0;
1243 int do_tx_balance = 1;
1244 u32 hash_index = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001245 const u8 *hash_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int res = 1;
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001247 struct ipv6hdr *ip6hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001249 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 eth_data = eth_hdr(skb);
1251
Michał Mirosław0693e882011-05-07 01:48:02 +00001252 /* make sure that the curr_active_slave do not change during tx
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 read_lock(&bond->curr_slave_lock);
1255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 switch (ntohs(skb->protocol)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001257 case ETH_P_IP: {
1258 const struct iphdr *iph = ip_hdr(skb);
1259
Eric Dumazet885a1362009-09-01 06:31:18 +00001260 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001261 (iph->daddr == ip_bcast) ||
1262 (iph->protocol == IPPROTO_IGMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 do_tx_balance = 0;
1264 break;
1265 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001266 hash_start = (char *)&(iph->daddr);
1267 hash_size = sizeof(iph->daddr);
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
1270 case ETH_P_IPV6:
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001271 /* IPv6 doesn't really use broadcast mac address, but leave
1272 * that here just in case.
1273 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001274 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 do_tx_balance = 0;
1276 break;
1277 }
1278
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001279 /* IPv6 uses all-nodes multicast as an equivalent to
1280 * broadcasts in IPv4.
1281 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001282 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001283 do_tx_balance = 0;
1284 break;
1285 }
1286
1287 /* Additianally, DAD probes should not be tx-balanced as that
1288 * will lead to false positives for duplicate addresses and
1289 * prevent address configuration from working.
1290 */
1291 ip6hdr = ipv6_hdr(skb);
1292 if (ipv6_addr_any(&ip6hdr->saddr)) {
1293 do_tx_balance = 0;
1294 break;
1295 }
1296
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001297 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1298 hash_size = sizeof(ipv6_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 break;
1300 case ETH_P_IPX:
Al Virod3bb52b2007-08-22 20:06:58 -04001301 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* something is wrong with this packet */
1303 do_tx_balance = 0;
1304 break;
1305 }
1306
1307 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1308 /* The only protocol worth balancing in
1309 * this family since it has an "ARP" like
1310 * mechanism
1311 */
1312 do_tx_balance = 0;
1313 break;
1314 }
1315
1316 hash_start = (char*)eth_data->h_dest;
1317 hash_size = ETH_ALEN;
1318 break;
1319 case ETH_P_ARP:
1320 do_tx_balance = 0;
1321 if (bond_info->rlb_enabled) {
1322 tx_slave = rlb_arp_xmit(skb, bond);
1323 }
1324 break;
1325 default:
1326 do_tx_balance = 0;
1327 break;
1328 }
1329
1330 if (do_tx_balance) {
1331 hash_index = _simple_hash(hash_start, hash_size);
1332 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1333 }
1334
1335 if (!tx_slave) {
1336 /* unbalanced or unassigned, send through primary */
1337 tx_slave = bond->curr_active_slave;
1338 bond_info->unbalanced_load += skb->len;
1339 }
1340
1341 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1342 if (tx_slave != bond->curr_active_slave) {
1343 memcpy(eth_data->h_source,
1344 tx_slave->dev->dev_addr,
1345 ETH_ALEN);
1346 }
1347
1348 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1349 } else {
1350 if (tx_slave) {
Maxim Uvarovf515e6b2012-01-09 12:01:37 +00001351 _lock_tx_hashtbl(bond);
1352 __tlb_clear_slave(bond, tx_slave, 0);
1353 _unlock_tx_hashtbl(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 }
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (res) {
1358 /* no suitable interface, frame not sent */
1359 dev_kfree_skb(skb);
1360 }
1361 read_unlock(&bond->curr_slave_lock);
Michał Mirosław0693e882011-05-07 01:48:02 +00001362
Patrick McHardyec634fe2009-07-05 19:23:38 -07001363 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001366void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001368 struct bonding *bond = container_of(work, struct bonding,
1369 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1371 struct slave *slave;
1372 int i;
1373
1374 read_lock(&bond->lock);
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (bond->slave_cnt == 0) {
1377 bond_info->tx_rebalance_counter = 0;
1378 bond_info->lp_counter = 0;
1379 goto re_arm;
1380 }
1381
1382 bond_info->tx_rebalance_counter++;
1383 bond_info->lp_counter++;
1384
1385 /* send learning packets */
1386 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1387 /* change of curr_active_slave involves swapping of mac addresses.
1388 * in order to avoid this swapping from happening while
1389 * sending the learning packets, the curr_slave_lock must be held for
1390 * read.
1391 */
1392 read_lock(&bond->curr_slave_lock);
1393
1394 bond_for_each_slave(bond, slave, i) {
Mitch Williamse944ef72005-11-09 10:36:50 -08001395 alb_send_learning_packets(slave, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398 read_unlock(&bond->curr_slave_lock);
1399
1400 bond_info->lp_counter = 0;
1401 }
1402
1403 /* rebalance tx traffic */
1404 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1405
1406 read_lock(&bond->curr_slave_lock);
1407
1408 bond_for_each_slave(bond, slave, i) {
1409 tlb_clear_slave(bond, slave, 1);
1410 if (slave == bond->curr_active_slave) {
1411 SLAVE_TLB_INFO(slave).load =
1412 bond_info->unbalanced_load /
1413 BOND_TLB_REBALANCE_INTERVAL;
1414 bond_info->unbalanced_load = 0;
1415 }
1416 }
1417
1418 read_unlock(&bond->curr_slave_lock);
1419
1420 bond_info->tx_rebalance_counter = 0;
1421 }
1422
1423 /* handle rlb stuff */
1424 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (bond_info->primary_is_promisc &&
1426 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1427
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001428 /*
1429 * dev_set_promiscuity requires rtnl and
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001430 * nothing else. Avoid race with bond_close.
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001431 */
1432 read_unlock(&bond->lock);
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001433 if (!rtnl_trylock()) {
1434 read_lock(&bond->lock);
1435 goto re_arm;
1436 }
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 bond_info->rlb_promisc_timeout_counter = 0;
1439
1440 /* If the primary was set to promiscuous mode
1441 * because a slave was disabled then
1442 * it can now leave promiscuous mode.
1443 */
1444 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1445 bond_info->primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001447 rtnl_unlock();
1448 read_lock(&bond->lock);
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 if (bond_info->rlb_rebalance) {
1452 bond_info->rlb_rebalance = 0;
1453 rlb_rebalance(bond);
1454 }
1455
1456 /* check if clients need updating */
1457 if (bond_info->rx_ntt) {
1458 if (bond_info->rlb_update_delay_counter) {
1459 --bond_info->rlb_update_delay_counter;
1460 } else {
1461 rlb_update_rx_clients(bond);
1462 if (bond_info->rlb_update_retry_counter) {
1463 --bond_info->rlb_update_retry_counter;
1464 } else {
1465 bond_info->rx_ntt = 0;
1466 }
1467 }
1468 }
1469 }
1470
1471re_arm:
Jay Vosburghe6d265e2011-10-28 15:42:50 +00001472 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 read_unlock(&bond->lock);
1475}
1476
1477/* assumption: called before the slave is attached to the bond
1478 * and not locked by the bond lock
1479 */
1480int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1481{
1482 int res;
1483
Jiri Bohacb9245512012-01-18 12:24:54 +00001484 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (res) {
1486 return res;
1487 }
1488
1489 /* caller must hold the bond lock for write since the mac addresses
1490 * are compared and may be swapped.
1491 */
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001492 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 res = alb_handle_addr_collision_on_attach(bond, slave);
1495
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001496 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 if (res) {
1499 return res;
1500 }
1501
1502 tlb_init_slave(slave);
1503
1504 /* order a rebalance ASAP */
1505 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1506
1507 if (bond->alb_info.rlb_enabled) {
1508 bond->alb_info.rlb_rebalance = 1;
1509 }
1510
1511 return 0;
1512}
1513
Jay Vosburgh25433312008-01-17 16:24:59 -08001514/*
1515 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1516 * if necessary.
1517 *
1518 * Caller must hold RTNL and no other locks
1519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1521{
1522 if (bond->slave_cnt > 1) {
1523 alb_change_hw_addr_on_detach(bond, slave);
1524 }
1525
1526 tlb_clear_slave(bond, slave, 0);
1527
1528 if (bond->alb_info.rlb_enabled) {
1529 bond->alb_info.next_rx_slave = NULL;
1530 rlb_clear_slave(bond, slave);
1531 }
1532}
1533
1534/* Caller must hold bond lock for read */
1535void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1536{
1537 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1538
1539 if (link == BOND_LINK_DOWN) {
1540 tlb_clear_slave(bond, slave, 0);
1541 if (bond->alb_info.rlb_enabled) {
1542 rlb_clear_slave(bond, slave);
1543 }
1544 } else if (link == BOND_LINK_UP) {
1545 /* order a rebalance ASAP */
1546 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1547 if (bond->alb_info.rlb_enabled) {
1548 bond->alb_info.rlb_rebalance = 1;
1549 /* If the updelay module parameter is smaller than the
1550 * forwarding delay of the switch the rebalance will
1551 * not work because the rebalance arp replies will
1552 * not be forwarded to the clients..
1553 */
1554 }
1555 }
1556}
1557
1558/**
1559 * bond_alb_handle_active_change - assign new curr_active_slave
1560 * @bond: our bonding struct
1561 * @new_slave: new slave to assign
1562 *
1563 * Set the bond->curr_active_slave to @new_slave and handle
1564 * mac address swapping and promiscuity changes as needed.
1565 *
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001566 * If new_slave is NULL, caller must hold curr_slave_lock or
1567 * bond->lock for write.
1568 *
1569 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1570 * read and curr_slave_lock for write. Processing here may sleep, so
1571 * no other locks may be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
1573void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001574 __releases(&bond->curr_slave_lock)
1575 __releases(&bond->lock)
1576 __acquires(&bond->lock)
1577 __acquires(&bond->curr_slave_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 struct slave *swap_slave;
1580 int i;
1581
1582 if (bond->curr_active_slave == new_slave) {
1583 return;
1584 }
1585
1586 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1587 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1588 bond->alb_info.primary_is_promisc = 0;
1589 bond->alb_info.rlb_promisc_timeout_counter = 0;
1590 }
1591
1592 swap_slave = bond->curr_active_slave;
1593 bond->curr_active_slave = new_slave;
1594
1595 if (!new_slave || (bond->slave_cnt == 0)) {
1596 return;
1597 }
1598
1599 /* set the new curr_active_slave to the bonds mac address
1600 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1601 */
1602 if (!swap_slave) {
1603 struct slave *tmp_slave;
1604 /* find slave that is holding the bond's mac address */
1605 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001606 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1607 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 swap_slave = tmp_slave;
1609 break;
1610 }
1611 }
1612 }
1613
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001614 /*
1615 * Arrange for swap_slave and new_slave to temporarily be
1616 * ignored so we can mess with their MAC addresses without
1617 * fear of interference from transmit activity.
1618 */
1619 if (swap_slave) {
1620 tlb_clear_slave(bond, swap_slave, 1);
1621 }
1622 tlb_clear_slave(bond, new_slave, 1);
1623
1624 write_unlock_bh(&bond->curr_slave_lock);
1625 read_unlock(&bond->lock);
1626
Jay Vosburghe0138a62008-01-17 16:24:58 -08001627 ASSERT_RTNL();
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1630 if (swap_slave) {
1631 /* swap mac address */
1632 alb_swap_mac_addr(bond, swap_slave, new_slave);
1633 } else {
1634 /* set the new_slave to the bond mac address */
Jiri Bohacb9245512012-01-18 12:24:54 +00001635 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001636 }
1637
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001638 if (swap_slave) {
1639 alb_fasten_mac_swap(bond, swap_slave, new_slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001640 read_lock(&bond->lock);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001641 } else {
Jay Vosburgh25433312008-01-17 16:24:59 -08001642 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1644 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001645
1646 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001649/*
1650 * Called with RTNL
1651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001653 __acquires(&bond->lock)
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001654 __releases(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Wang Chen454d7c92008-11-12 23:37:49 -08001656 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct sockaddr *sa = addr;
1658 struct slave *slave, *swap_slave;
1659 int res;
1660 int i;
1661
1662 if (!is_valid_ether_addr(sa->sa_data)) {
1663 return -EADDRNOTAVAIL;
1664 }
1665
1666 res = alb_set_mac_address(bond, addr);
1667 if (res) {
1668 return res;
1669 }
1670
1671 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1672
1673 /* If there is no curr_active_slave there is nothing else to do.
1674 * Otherwise we'll need to pass the new address to it and handle
1675 * duplications.
1676 */
1677 if (!bond->curr_active_slave) {
1678 return 0;
1679 }
1680
1681 swap_slave = NULL;
1682
1683 bond_for_each_slave(bond, slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001684 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1685 bond_dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 swap_slave = slave;
1687 break;
1688 }
1689 }
1690
1691 if (swap_slave) {
1692 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001693 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 } else {
Jiri Bohacb9245512012-01-18 12:24:54 +00001695 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001697 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1699 if (bond->alb_info.rlb_enabled) {
1700 /* inform clients mac address has changed */
1701 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1702 }
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001703 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 return 0;
1707}
1708
1709void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1710{
1711 if (bond->alb_info.current_alb_vlan &&
1712 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1713 bond->alb_info.current_alb_vlan = NULL;
1714 }
1715
1716 if (bond->alb_info.rlb_enabled) {
1717 rlb_clear_vlan(bond, vlan_id);
1718 }
1719}
1720