blob: 3b7b0409406fcefa83573a84ba854fee2e1c146a [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
102static inline void _lock_tx_hashtbl(struct bonding *bond)
103{
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
107static inline void _unlock_tx_hashtbl(struct bonding *bond)
108{
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
112/* Caller must hold tx_hashtbl lock */
113static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
114{
115 if (save_load) {
116 entry->load_history = 1 + entry->tx_bytes /
117 BOND_TLB_REBALANCE_INTERVAL;
118 entry->tx_bytes = 0;
119 }
120
121 entry->tx_slave = NULL;
122 entry->next = TLB_NULL_INDEX;
123 entry->prev = TLB_NULL_INDEX;
124}
125
126static inline void tlb_init_slave(struct slave *slave)
127{
128 SLAVE_TLB_INFO(slave).load = 0;
129 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
130}
131
132/* Caller must hold bond lock for read */
133static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
134{
135 struct tlb_client_info *tx_hash_table;
136 u32 index;
137
138 _lock_tx_hashtbl(bond);
139
140 /* clear slave from tx_hashtbl */
141 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
142
Andy Gospodarekce39a802008-10-30 17:41:16 -0700143 /* skip this if we've already freed the tx hash table */
144 if (tx_hash_table) {
145 index = SLAVE_TLB_INFO(slave).head;
146 while (index != TLB_NULL_INDEX) {
147 u32 next_index = tx_hash_table[index].next;
148 tlb_init_table_entry(&tx_hash_table[index], save_load);
149 index = next_index;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 tlb_init_slave(slave);
Jay Vosburgh5af47b22006-01-09 12:14:00 -0800154
155 _unlock_tx_hashtbl(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/* Must be called before starting the monitor timer */
159static int tlb_initialize(struct bonding *bond)
160{
161 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
162 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
Mitch Williams0d206a32005-11-09 10:35:30 -0800163 struct tlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int i;
165
166 spin_lock_init(&(bond_info->tx_hashtbl_lock));
167
Joe Jin243cb4e2007-02-06 14:16:40 -0800168 new_hashtbl = kzalloc(size, GFP_KERNEL);
Mitch Williams0d206a32005-11-09 10:35:30 -0800169 if (!new_hashtbl) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800170 pr_err("%s: Error: Failed to allocate TLB hash table\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -1;
173 }
Mitch Williams0d206a32005-11-09 10:35:30 -0800174 _lock_tx_hashtbl(bond);
175
176 bond_info->tx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
Peter Pan(潘卫平)38dbaf02011-04-08 03:40:19 +0000179 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 _unlock_tx_hashtbl(bond);
183
184 return 0;
185}
186
187/* Must be called only after all slaves have been released */
188static void tlb_deinitialize(struct bonding *bond)
189{
190 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
191
192 _lock_tx_hashtbl(bond);
193
194 kfree(bond_info->tx_hashtbl);
195 bond_info->tx_hashtbl = NULL;
196
197 _unlock_tx_hashtbl(bond);
198}
199
Jiri Pirko097811b2010-05-19 03:26:39 +0000200static long long compute_gap(struct slave *slave)
201{
202 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
203 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/* Caller must hold bond lock for read */
207static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
208{
209 struct slave *slave, *least_loaded;
Jiri Pirko097811b2010-05-19 03:26:39 +0000210 long long max_gap;
211 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jiri Pirko097811b2010-05-19 03:26:39 +0000213 least_loaded = NULL;
214 max_gap = LLONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* Find the slave with the largest gap */
Jiri Pirko097811b2010-05-19 03:26:39 +0000217 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (SLAVE_IS_OK(slave)) {
Jiri Pirko097811b2010-05-19 03:26:39 +0000219 long long gap = compute_gap(slave);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (max_gap < gap) {
222 least_loaded = slave;
223 max_gap = gap;
224 }
225 }
226 }
227
228 return least_loaded;
229}
230
231/* Caller must hold bond lock for read */
232static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
233{
234 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
235 struct tlb_client_info *hash_table;
236 struct slave *assigned_slave;
237
238 _lock_tx_hashtbl(bond);
239
240 hash_table = bond_info->tx_hashtbl;
241 assigned_slave = hash_table[hash_index].tx_slave;
242 if (!assigned_slave) {
243 assigned_slave = tlb_get_least_loaded_slave(bond);
244
245 if (assigned_slave) {
246 struct tlb_slave_info *slave_info =
247 &(SLAVE_TLB_INFO(assigned_slave));
248 u32 next_index = slave_info->head;
249
250 hash_table[hash_index].tx_slave = assigned_slave;
251 hash_table[hash_index].next = next_index;
252 hash_table[hash_index].prev = TLB_NULL_INDEX;
253
254 if (next_index != TLB_NULL_INDEX) {
255 hash_table[next_index].prev = hash_index;
256 }
257
258 slave_info->head = hash_index;
259 slave_info->load +=
260 hash_table[hash_index].load_history;
261 }
262 }
263
264 if (assigned_slave) {
265 hash_table[hash_index].tx_bytes += skb_len;
266 }
267
268 _unlock_tx_hashtbl(bond);
269
270 return assigned_slave;
271}
272
273/*********************** rlb specific functions ***************************/
274static inline void _lock_rx_hashtbl(struct bonding *bond)
275{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700276 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static inline void _unlock_rx_hashtbl(struct bonding *bond)
280{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700281 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284/* when an ARP REPLY is received from a client update its info
285 * in the rx_hashtbl
286 */
287static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
288{
289 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
290 struct rlb_client_info *client_info;
291 u32 hash_index;
292
293 _lock_rx_hashtbl(bond);
294
295 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
296 client_info = &(bond_info->rx_hashtbl[hash_index]);
297
298 if ((client_info->assigned) &&
299 (client_info->ip_src == arp->ip_dst) &&
Flavio Leitner42d782a2010-06-29 08:24:39 +0000300 (client_info->ip_dst == arp->ip_src) &&
301 (compare_ether_addr_64bits(client_info->mac_dst, arp->mac_src))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* update the clients MAC address */
303 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
304 client_info->ntt = 1;
305 bond_info->rx_ntt = 1;
306 }
307
308 _unlock_rx_hashtbl(bond);
309}
310
Jiri Pirko3aba8912011-04-19 03:48:16 +0000311static void rlb_arp_recv(struct sk_buff *skb, struct bonding *bond,
312 struct slave *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Jiri Pirko3aba8912011-04-19 03:48:16 +0000314 struct arp_pkt *arp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Jiri Pirko3aba8912011-04-19 03:48:16 +0000316 if (skb->protocol != cpu_to_be16(ETH_P_ARP))
317 return;
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800318
Jiri Pirko3aba8912011-04-19 03:48:16 +0000319 arp = (struct arp_pkt *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!arp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800321 pr_debug("Packet has no ARP data\n");
Jiri Pirko3aba8912011-04-19 03:48:16 +0000322 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324
Jiri Pirko3aba8912011-04-19 03:48:16 +0000325 if (!pskb_may_pull(skb, arp_hdr_len(bond->dev)))
326 return;
Andy Gospodarekab128112010-09-10 11:43:20 +0000327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (skb->len < sizeof(struct arp_pkt)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800329 pr_debug("Packet is too small to be an ARP\n");
Jiri Pirko3aba8912011-04-19 03:48:16 +0000330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 if (arp->op_code == htons(ARPOP_REPLY)) {
334 /* update rx hash table for this ARP */
335 rlb_update_entry_from_arp(bond, arp);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800336 pr_debug("Server received an ARP Reply from client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/* Caller must hold bond lock for read */
341static struct slave *rlb_next_rx_slave(struct bonding *bond)
342{
343 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
344 struct slave *rx_slave, *slave, *start_at;
345 int i = 0;
346
347 if (bond_info->next_rx_slave) {
348 start_at = bond_info->next_rx_slave;
349 } else {
350 start_at = bond->first_slave;
351 }
352
353 rx_slave = NULL;
354
355 bond_for_each_slave_from(bond, slave, i, start_at) {
356 if (SLAVE_IS_OK(slave)) {
357 if (!rx_slave) {
358 rx_slave = slave;
359 } else if (slave->speed > rx_slave->speed) {
360 rx_slave = slave;
361 }
362 }
363 }
364
365 if (rx_slave) {
366 bond_info->next_rx_slave = rx_slave->next;
367 }
368
369 return rx_slave;
370}
371
372/* teach the switch the mac of a disabled slave
373 * on the primary for fault tolerance
374 *
375 * Caller must hold bond->curr_slave_lock for write or bond lock for write
376 */
377static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
378{
379 if (!bond->curr_active_slave) {
380 return;
381 }
382
383 if (!bond->alb_info.primary_is_promisc) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700384 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
385 bond->alb_info.primary_is_promisc = 1;
386 else
387 bond->alb_info.primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
390 bond->alb_info.rlb_promisc_timeout_counter = 0;
391
392 alb_send_learning_packets(bond->curr_active_slave, addr);
393}
394
395/* slave being removed should not be active at this point
396 *
397 * Caller must hold bond lock for read
398 */
399static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
400{
401 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
402 struct rlb_client_info *rx_hash_table;
403 u32 index, next_index;
404
405 /* clear slave from rx_hashtbl */
406 _lock_rx_hashtbl(bond);
407
408 rx_hash_table = bond_info->rx_hashtbl;
409 index = bond_info->rx_hashtbl_head;
410 for (; index != RLB_NULL_INDEX; index = next_index) {
411 next_index = rx_hash_table[index].next;
412 if (rx_hash_table[index].slave == slave) {
413 struct slave *assigned_slave = rlb_next_rx_slave(bond);
414
415 if (assigned_slave) {
416 rx_hash_table[index].slave = assigned_slave;
Eric Dumazet885a1362009-09-01 06:31:18 +0000417 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
418 mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 bond_info->rx_hashtbl[index].ntt = 1;
420 bond_info->rx_ntt = 1;
421 /* A slave has been removed from the
422 * table because it is either disabled
423 * or being released. We must retry the
424 * update to avoid clients from not
425 * being updated & disconnecting when
426 * there is stress
427 */
428 bond_info->rlb_update_retry_counter =
429 RLB_UPDATE_RETRY;
430 }
431 } else { /* there is no active slave */
432 rx_hash_table[index].slave = NULL;
433 }
434 }
435 }
436
437 _unlock_rx_hashtbl(bond);
438
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700439 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (slave != bond->curr_active_slave) {
442 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
443 }
444
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700445 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448static void rlb_update_client(struct rlb_client_info *client_info)
449{
450 int i;
451
452 if (!client_info->slave) {
453 return;
454 }
455
456 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
457 struct sk_buff *skb;
458
459 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
460 client_info->ip_dst,
461 client_info->slave->dev,
462 client_info->ip_src,
463 client_info->mac_dst,
464 client_info->slave->dev->dev_addr,
465 client_info->mac_dst);
466 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800467 pr_err("%s: Error: failed to create an ARP packet\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800468 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 continue;
470 }
471
472 skb->dev = client_info->slave->dev;
473
474 if (client_info->tag) {
475 skb = vlan_put_tag(skb, client_info->vlan_id);
476 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800477 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800478 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 continue;
480 }
481 }
482
483 arp_xmit(skb);
484 }
485}
486
487/* sends ARP REPLIES that update the clients that need updating */
488static void rlb_update_rx_clients(struct bonding *bond)
489{
490 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
491 struct rlb_client_info *client_info;
492 u32 hash_index;
493
494 _lock_rx_hashtbl(bond);
495
496 hash_index = bond_info->rx_hashtbl_head;
497 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
498 client_info = &(bond_info->rx_hashtbl[hash_index]);
499 if (client_info->ntt) {
500 rlb_update_client(client_info);
501 if (bond_info->rlb_update_retry_counter == 0) {
502 client_info->ntt = 0;
503 }
504 }
505 }
506
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200507 /* do not update the entries again until this counter is zero so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * not to confuse the clients.
509 */
510 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
511
512 _unlock_rx_hashtbl(bond);
513}
514
515/* The slave was assigned a new mac address - update the clients */
516static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
517{
518 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
519 struct rlb_client_info *client_info;
520 int ntt = 0;
521 u32 hash_index;
522
523 _lock_rx_hashtbl(bond);
524
525 hash_index = bond_info->rx_hashtbl_head;
526 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
527 client_info = &(bond_info->rx_hashtbl[hash_index]);
528
529 if ((client_info->slave == slave) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000530 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 client_info->ntt = 1;
532 ntt = 1;
533 }
534 }
535
536 // update the team's flag only after the whole iteration
537 if (ntt) {
538 bond_info->rx_ntt = 1;
539 //fasten the change
540 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
541 }
542
543 _unlock_rx_hashtbl(bond);
544}
545
546/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400547static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
550 struct rlb_client_info *client_info;
551 u32 hash_index;
552
553 _lock_rx_hashtbl(bond);
554
555 hash_index = bond_info->rx_hashtbl_head;
556 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
557 client_info = &(bond_info->rx_hashtbl[hash_index]);
558
559 if (!client_info->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800560 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800561 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 continue;
563 }
564 /*update all clients using this src_ip, that are not assigned
565 * to the team's address (curr_active_slave) and have a known
566 * unicast mac address.
567 */
568 if ((client_info->ip_src == src_ip) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000569 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
570 bond->dev->dev_addr) &&
571 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 client_info->ntt = 1;
573 bond_info->rx_ntt = 1;
574 }
575 }
576
577 _unlock_rx_hashtbl(bond);
578}
579
580/* Caller must hold both bond and ptr locks for read */
581static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
582{
583 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300584 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct slave *assigned_slave;
586 struct rlb_client_info *client_info;
587 u32 hash_index = 0;
588
589 _lock_rx_hashtbl(bond);
590
Amerigo Wange364a342011-02-27 23:34:28 +0000591 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 client_info = &(bond_info->rx_hashtbl[hash_index]);
593
594 if (client_info->assigned) {
595 if ((client_info->ip_src == arp->ip_src) &&
596 (client_info->ip_dst == arp->ip_dst)) {
597 /* the entry is already assigned to this client */
Eric Dumazet885a1362009-09-01 06:31:18 +0000598 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* update mac address from arp */
600 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
601 }
602
603 assigned_slave = client_info->slave;
604 if (assigned_slave) {
605 _unlock_rx_hashtbl(bond);
606 return assigned_slave;
607 }
608 } else {
609 /* the entry is already assigned to some other client,
610 * move the old client to primary (curr_active_slave) so
611 * that the new client can be assigned to this entry.
612 */
613 if (bond->curr_active_slave &&
614 client_info->slave != bond->curr_active_slave) {
615 client_info->slave = bond->curr_active_slave;
616 rlb_update_client(client_info);
617 }
618 }
619 }
620 /* assign a new slave */
621 assigned_slave = rlb_next_rx_slave(bond);
622
623 if (assigned_slave) {
624 client_info->ip_src = arp->ip_src;
625 client_info->ip_dst = arp->ip_dst;
626 /* arp->mac_dst is broadcast for arp reqeusts.
627 * will be updated with clients actual unicast mac address
628 * upon receiving an arp reply.
629 */
630 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
631 client_info->slave = assigned_slave;
632
Eric Dumazet885a1362009-09-01 06:31:18 +0000633 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 client_info->ntt = 1;
635 bond->alb_info.rx_ntt = 1;
636 } else {
637 client_info->ntt = 0;
638 }
639
Jay Vosburghf35188f2010-07-21 12:14:47 +0000640 if (bond->vlgrp) {
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700641 if (!vlan_get_tag(skb, &client_info->vlan_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 client_info->tag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 if (!client_info->assigned) {
646 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
647 bond_info->rx_hashtbl_head = hash_index;
648 client_info->next = prev_tbl_head;
649 if (prev_tbl_head != RLB_NULL_INDEX) {
650 bond_info->rx_hashtbl[prev_tbl_head].prev =
651 hash_index;
652 }
653 client_info->assigned = 1;
654 }
655 }
656
657 _unlock_rx_hashtbl(bond);
658
659 return assigned_slave;
660}
661
662/* chooses (and returns) transmit channel for arp reply
663 * does not choose channel for other arp types since they are
664 * sent on the curr_active_slave
665 */
666static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
667{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300668 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct slave *tx_slave = NULL;
670
Brian Haleyf14c4e42008-09-02 10:08:08 -0400671 if (arp->op_code == htons(ARPOP_REPLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* the arp must be sent on the selected
673 * rx channel
674 */
675 tx_slave = rlb_choose_channel(skb, bond);
676 if (tx_slave) {
677 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
678 }
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800679 pr_debug("Server sent ARP Reply packet\n");
Brian Haleyf14c4e42008-09-02 10:08:08 -0400680 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Create an entry in the rx_hashtbl for this client as a
682 * place holder.
683 * When the arp reply is received the entry will be updated
684 * with the correct unicast address of the client.
685 */
686 rlb_choose_channel(skb, bond);
687
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +0000688 /* The ARP reply packets must be delayed so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * they can cancel out the influence of the ARP request.
690 */
691 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
692
693 /* arp requests are broadcast and are sent on the primary
694 * the arp request will collapse all clients on the subnet to
695 * the primary slave. We must register these clients to be
696 * updated with their assigned mac.
697 */
698 rlb_req_update_subnet_clients(bond, arp->ip_src);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800699 pr_debug("Server sent ARP Request packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
702 return tx_slave;
703}
704
705/* Caller must hold bond lock for read */
706static void rlb_rebalance(struct bonding *bond)
707{
708 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
709 struct slave *assigned_slave;
710 struct rlb_client_info *client_info;
711 int ntt;
712 u32 hash_index;
713
714 _lock_rx_hashtbl(bond);
715
716 ntt = 0;
717 hash_index = bond_info->rx_hashtbl_head;
718 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
719 client_info = &(bond_info->rx_hashtbl[hash_index]);
720 assigned_slave = rlb_next_rx_slave(bond);
721 if (assigned_slave && (client_info->slave != assigned_slave)) {
722 client_info->slave = assigned_slave;
723 client_info->ntt = 1;
724 ntt = 1;
725 }
726 }
727
728 /* update the team's flag only after the whole iteration */
729 if (ntt) {
730 bond_info->rx_ntt = 1;
731 }
732 _unlock_rx_hashtbl(bond);
733}
734
735/* Caller must hold rx_hashtbl lock */
736static void rlb_init_table_entry(struct rlb_client_info *entry)
737{
738 memset(entry, 0, sizeof(struct rlb_client_info));
739 entry->next = RLB_NULL_INDEX;
740 entry->prev = RLB_NULL_INDEX;
741}
742
743static int rlb_initialize(struct bonding *bond)
744{
745 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Mitch Williams0d206a32005-11-09 10:35:30 -0800746 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
748 int i;
749
750 spin_lock_init(&(bond_info->rx_hashtbl_lock));
751
Mitch Williams0d206a32005-11-09 10:35:30 -0800752 new_hashtbl = kmalloc(size, GFP_KERNEL);
753 if (!new_hashtbl) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800754 pr_err("%s: Error: Failed to allocate RLB hash table\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -1;
757 }
Mitch Williams0d206a32005-11-09 10:35:30 -0800758 _lock_rx_hashtbl(bond);
759
760 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
763
764 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
765 rlb_init_table_entry(bond_info->rx_hashtbl + i);
766 }
767
768 _unlock_rx_hashtbl(bond);
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /* register to receive ARPs */
Jiri Pirko3aba8912011-04-19 03:48:16 +0000771 bond->recv_probe = rlb_arp_recv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return 0;
774}
775
776static void rlb_deinitialize(struct bonding *bond)
777{
778 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 _lock_rx_hashtbl(bond);
781
782 kfree(bond_info->rx_hashtbl);
783 bond_info->rx_hashtbl = NULL;
784 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
785
786 _unlock_rx_hashtbl(bond);
787}
788
789static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
790{
791 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
792 u32 curr_index;
793
794 _lock_rx_hashtbl(bond);
795
796 curr_index = bond_info->rx_hashtbl_head;
797 while (curr_index != RLB_NULL_INDEX) {
798 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
799 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
800 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
801
802 if (curr->tag && (curr->vlan_id == vlan_id)) {
803 if (curr_index == bond_info->rx_hashtbl_head) {
804 bond_info->rx_hashtbl_head = next_index;
805 }
806 if (prev_index != RLB_NULL_INDEX) {
807 bond_info->rx_hashtbl[prev_index].next = next_index;
808 }
809 if (next_index != RLB_NULL_INDEX) {
810 bond_info->rx_hashtbl[next_index].prev = prev_index;
811 }
812
813 rlb_init_table_entry(curr);
814 }
815
816 curr_index = next_index;
817 }
818
819 _unlock_rx_hashtbl(bond);
820}
821
822/*********************** tlb/rlb shared functions *********************/
823
824static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
825{
826 struct bonding *bond = bond_get_bond_by_slave(slave);
827 struct learning_pkt pkt;
828 int size = sizeof(struct learning_pkt);
829 int i;
830
831 memset(&pkt, 0, size);
832 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
833 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
Harvey Harrison09640e632009-02-01 00:45:17 -0800834 pkt.type = cpu_to_be16(ETH_P_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 for (i = 0; i < MAX_LP_BURST; i++) {
837 struct sk_buff *skb;
838 char *data;
839
840 skb = dev_alloc_skb(size);
841 if (!skb) {
842 return;
843 }
844
845 data = skb_put(skb, size);
846 memcpy(data, &pkt, size);
847
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700848 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700849 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 skb->protocol = pkt.type;
851 skb->priority = TC_PRIO_CONTROL;
852 skb->dev = slave->dev;
853
Jay Vosburghf35188f2010-07-21 12:14:47 +0000854 if (bond->vlgrp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct vlan_entry *vlan;
856
857 vlan = bond_next_vlan(bond,
858 bond->alb_info.current_alb_vlan);
859
860 bond->alb_info.current_alb_vlan = vlan;
861 if (!vlan) {
862 kfree_skb(skb);
863 continue;
864 }
865
866 skb = vlan_put_tag(skb, vlan->vlan_id);
867 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800868 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800869 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 continue;
871 }
872 }
873
874 dev_queue_xmit(skb);
875 }
876}
877
878/* hw is a boolean parameter that determines whether we should try and
879 * set the hw address of the device as well as the hw address of the
880 * net_device
881 */
882static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
883{
884 struct net_device *dev = slave->dev;
885 struct sockaddr s_addr;
886
887 if (!hw) {
888 memcpy(dev->dev_addr, addr, dev->addr_len);
889 return 0;
890 }
891
892 /* for rlb each slave must have a unique hw mac addresses so that */
893 /* each slave will receive packets destined to a different mac */
894 memcpy(s_addr.sa_data, addr, dev->addr_len);
895 s_addr.sa_family = dev->type;
896 if (dev_set_mac_address(dev, &s_addr)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800897 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
898 "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 -0800899 dev->master->name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -EOPNOTSUPP;
901 }
902 return 0;
903}
904
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700905/*
906 * Swap MAC addresses between two slaves.
907 *
908 * Called with RTNL held, and no other locks.
909 *
910 */
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
917 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
918 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
919
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700920}
921
922/*
923 * Send learning packets after MAC address swap.
924 *
Jay Vosburgh25433312008-01-17 16:24:59 -0800925 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700926 */
927static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
928 struct slave *slave2)
929{
930 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
931 struct slave *disabled_slave = NULL;
932
Jay Vosburgh25433312008-01-17 16:24:59 -0800933 ASSERT_RTNL();
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* fasten the change in the switch */
936 if (SLAVE_IS_OK(slave1)) {
937 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
938 if (bond->alb_info.rlb_enabled) {
939 /* inform the clients that the mac address
940 * has changed
941 */
942 rlb_req_update_slave_clients(bond, slave1);
943 }
944 } else {
945 disabled_slave = slave1;
946 }
947
948 if (SLAVE_IS_OK(slave2)) {
949 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
950 if (bond->alb_info.rlb_enabled) {
951 /* inform the clients that the mac address
952 * has changed
953 */
954 rlb_req_update_slave_clients(bond, slave2);
955 }
956 } else {
957 disabled_slave = slave2;
958 }
959
960 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
961 /* A disabled slave was assigned an active mac addr */
962 rlb_teach_disabled_mac_on_primary(bond,
963 disabled_slave->dev->dev_addr);
964 }
965}
966
967/**
968 * alb_change_hw_addr_on_detach
969 * @bond: bonding we're working on
970 * @slave: the slave that was just detached
971 *
972 * We assume that @slave was already detached from the slave list.
973 *
974 * If @slave's permanent hw address is different both from its current
975 * address and from @bond's address, then somewhere in the bond there's
976 * a slave that has @slave's permanet address as its current address.
977 * We'll make sure that that slave no longer uses @slave's permanent address.
978 *
Jay Vosburgh25433312008-01-17 16:24:59 -0800979 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
981static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
982{
983 int perm_curr_diff;
984 int perm_bond_diff;
985
Eric Dumazet885a1362009-09-01 06:31:18 +0000986 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
987 slave->dev->dev_addr);
988 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
989 bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 if (perm_curr_diff && perm_bond_diff) {
992 struct slave *tmp_slave;
993 int i, found = 0;
994
995 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +0000996 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
997 tmp_slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 found = 1;
999 break;
1000 }
1001 }
1002
1003 if (found) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001004 /* locking: needs RTNL and nothing else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 alb_swap_mac_addr(bond, slave, tmp_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001006 alb_fasten_mac_swap(bond, slave, tmp_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008 }
1009}
1010
1011/**
1012 * alb_handle_addr_collision_on_attach
1013 * @bond: bonding we're working on
1014 * @slave: the slave that was just attached
1015 *
1016 * checks uniqueness of slave's mac address and handles the case the
1017 * new slave uses the bonds mac address.
1018 *
1019 * If the permanent hw address of @slave is @bond's hw address, we need to
1020 * find a different hw address to give @slave, that isn't in use by any other
Peter Pan(潘卫平)77c8e2c2011-04-11 00:16:32 +00001021 * slave in the bond. This address must be, of course, one of the permanent
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * addresses of the other slaves.
1023 *
1024 * We go over the slave list, and for each slave there we compare its
1025 * permanent hw address with the current address of all the other slaves.
1026 * If no match was found, then we've found a slave with a permanent address
1027 * that isn't used by any other slave in the bond, so we can assign it to
1028 * @slave.
1029 *
1030 * assumption: this function is called before @slave is attached to the
1031 * bond slave list.
1032 *
1033 * caller must hold the bond lock for write since the mac addresses are compared
1034 * and may be swapped.
1035 */
1036static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1037{
1038 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1039 struct slave *has_bond_addr = bond->curr_active_slave;
1040 int i, j, found = 0;
1041
1042 if (bond->slave_cnt == 0) {
1043 /* this is the first slave */
1044 return 0;
1045 }
1046
1047 /* if slave's mac address differs from bond's mac address
1048 * check uniqueness of slave's mac address against the other
1049 * slaves in the bond.
1050 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001051 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 bond_for_each_slave(bond, tmp_slave1, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001053 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1054 slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 found = 1;
1056 break;
1057 }
1058 }
1059
John W. Linville6b38aef2005-07-28 15:00:15 -04001060 if (!found)
1061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
John W. Linville6b38aef2005-07-28 15:00:15 -04001063 /* Try setting slave mac to bond address and fall-through
1064 to code handling that situation below... */
1065 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1066 bond->alb_info.rlb_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
1069 /* The slave's address is equal to the address of the bond.
1070 * Search for a spare address in the bond for this slave.
1071 */
1072 free_mac_slave = NULL;
1073
1074 bond_for_each_slave(bond, tmp_slave1, i) {
1075 found = 0;
1076 bond_for_each_slave(bond, tmp_slave2, j) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001077 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1078 tmp_slave2->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 found = 1;
1080 break;
1081 }
1082 }
1083
1084 if (!found) {
1085 /* no slave has tmp_slave1's perm addr
1086 * as its curr addr
1087 */
1088 free_mac_slave = tmp_slave1;
1089 break;
1090 }
1091
1092 if (!has_bond_addr) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001093 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1094 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 has_bond_addr = tmp_slave1;
1097 }
1098 }
1099 }
1100
1101 if (free_mac_slave) {
1102 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1103 bond->alb_info.rlb_enabled);
1104
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001105 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 +00001106 bond->dev->name, slave->dev->name,
1107 free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 } else if (has_bond_addr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001110 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 -08001111 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -EFAULT;
1113 }
1114
1115 return 0;
1116}
1117
1118/**
1119 * alb_set_mac_address
1120 * @bond:
1121 * @addr:
1122 *
1123 * In TLB mode all slaves are configured to the bond's hw address, but set
1124 * their dev_addr field to different addresses (based on their permanent hw
1125 * addresses).
1126 *
1127 * For each slave, this function sets the interface to the new address and then
1128 * changes its dev_addr field to its previous value.
1129 *
1130 * Unwinding assumes bond's mac address has not yet changed.
1131 */
1132static int alb_set_mac_address(struct bonding *bond, void *addr)
1133{
1134 struct sockaddr sa;
1135 struct slave *slave, *stop_at;
1136 char tmp_addr[ETH_ALEN];
1137 int res;
1138 int i;
1139
1140 if (bond->alb_info.rlb_enabled) {
1141 return 0;
1142 }
1143
1144 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 /* save net_device's current hw address */
1146 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1147
1148 res = dev_set_mac_address(slave->dev, addr);
1149
1150 /* restore net_device's hw address */
1151 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1152
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001153 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 return 0;
1158
1159unwind:
1160 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1161 sa.sa_family = bond->dev->type;
1162
1163 /* unwind from head to the slave that failed */
1164 stop_at = slave;
1165 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1166 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1167 dev_set_mac_address(slave->dev, &sa);
1168 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1169 }
1170
1171 return res;
1172}
1173
1174/************************ exported alb funcions ************************/
1175
1176int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1177{
1178 int res;
1179
1180 res = tlb_initialize(bond);
1181 if (res) {
1182 return res;
1183 }
1184
1185 if (rlb_enabled) {
1186 bond->alb_info.rlb_enabled = 1;
1187 /* initialize rlb */
1188 res = rlb_initialize(bond);
1189 if (res) {
1190 tlb_deinitialize(bond);
1191 return res;
1192 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001193 } else {
1194 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
1197 return 0;
1198}
1199
1200void bond_alb_deinitialize(struct bonding *bond)
1201{
1202 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1203
1204 tlb_deinitialize(bond);
1205
1206 if (bond_info->rlb_enabled) {
1207 rlb_deinitialize(bond);
1208 }
1209}
1210
1211int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1212{
Wang Chen454d7c92008-11-12 23:37:49 -08001213 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct ethhdr *eth_data;
1215 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1216 struct slave *tx_slave = NULL;
Al Virod3bb52b2007-08-22 20:06:58 -04001217 static const __be32 ip_bcast = htonl(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int hash_size = 0;
1219 int do_tx_balance = 1;
1220 u32 hash_index = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001221 const u8 *hash_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int res = 1;
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001223 struct ipv6hdr *ip6hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001225 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 eth_data = eth_hdr(skb);
1227
1228 /* make sure that the curr_active_slave and the slaves list do
1229 * not change during tx
1230 */
1231 read_lock(&bond->lock);
1232 read_lock(&bond->curr_slave_lock);
1233
1234 if (!BOND_IS_OK(bond)) {
1235 goto out;
1236 }
1237
1238 switch (ntohs(skb->protocol)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001239 case ETH_P_IP: {
1240 const struct iphdr *iph = ip_hdr(skb);
1241
Eric Dumazet885a1362009-09-01 06:31:18 +00001242 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001243 (iph->daddr == ip_bcast) ||
1244 (iph->protocol == IPPROTO_IGMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 do_tx_balance = 0;
1246 break;
1247 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001248 hash_start = (char *)&(iph->daddr);
1249 hash_size = sizeof(iph->daddr);
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
1252 case ETH_P_IPV6:
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001253 /* IPv6 doesn't really use broadcast mac address, but leave
1254 * that here just in case.
1255 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001256 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 do_tx_balance = 0;
1258 break;
1259 }
1260
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001261 /* IPv6 uses all-nodes multicast as an equivalent to
1262 * broadcasts in IPv4.
1263 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001264 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001265 do_tx_balance = 0;
1266 break;
1267 }
1268
1269 /* Additianally, DAD probes should not be tx-balanced as that
1270 * will lead to false positives for duplicate addresses and
1271 * prevent address configuration from working.
1272 */
1273 ip6hdr = ipv6_hdr(skb);
1274 if (ipv6_addr_any(&ip6hdr->saddr)) {
1275 do_tx_balance = 0;
1276 break;
1277 }
1278
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001279 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1280 hash_size = sizeof(ipv6_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 case ETH_P_IPX:
Al Virod3bb52b2007-08-22 20:06:58 -04001283 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* something is wrong with this packet */
1285 do_tx_balance = 0;
1286 break;
1287 }
1288
1289 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1290 /* The only protocol worth balancing in
1291 * this family since it has an "ARP" like
1292 * mechanism
1293 */
1294 do_tx_balance = 0;
1295 break;
1296 }
1297
1298 hash_start = (char*)eth_data->h_dest;
1299 hash_size = ETH_ALEN;
1300 break;
1301 case ETH_P_ARP:
1302 do_tx_balance = 0;
1303 if (bond_info->rlb_enabled) {
1304 tx_slave = rlb_arp_xmit(skb, bond);
1305 }
1306 break;
1307 default:
1308 do_tx_balance = 0;
1309 break;
1310 }
1311
1312 if (do_tx_balance) {
1313 hash_index = _simple_hash(hash_start, hash_size);
1314 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1315 }
1316
1317 if (!tx_slave) {
1318 /* unbalanced or unassigned, send through primary */
1319 tx_slave = bond->curr_active_slave;
1320 bond_info->unbalanced_load += skb->len;
1321 }
1322
1323 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1324 if (tx_slave != bond->curr_active_slave) {
1325 memcpy(eth_data->h_source,
1326 tx_slave->dev->dev_addr,
1327 ETH_ALEN);
1328 }
1329
1330 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1331 } else {
1332 if (tx_slave) {
1333 tlb_clear_slave(bond, tx_slave, 0);
1334 }
1335 }
1336
1337out:
1338 if (res) {
1339 /* no suitable interface, frame not sent */
1340 dev_kfree_skb(skb);
1341 }
1342 read_unlock(&bond->curr_slave_lock);
1343 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07001344 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001347void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001349 struct bonding *bond = container_of(work, struct bonding,
1350 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1352 struct slave *slave;
1353 int i;
1354
1355 read_lock(&bond->lock);
1356
1357 if (bond->kill_timers) {
1358 goto out;
1359 }
1360
1361 if (bond->slave_cnt == 0) {
1362 bond_info->tx_rebalance_counter = 0;
1363 bond_info->lp_counter = 0;
1364 goto re_arm;
1365 }
1366
1367 bond_info->tx_rebalance_counter++;
1368 bond_info->lp_counter++;
1369
1370 /* send learning packets */
1371 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1372 /* change of curr_active_slave involves swapping of mac addresses.
1373 * in order to avoid this swapping from happening while
1374 * sending the learning packets, the curr_slave_lock must be held for
1375 * read.
1376 */
1377 read_lock(&bond->curr_slave_lock);
1378
1379 bond_for_each_slave(bond, slave, i) {
Mitch Williamse944ef72005-11-09 10:36:50 -08001380 alb_send_learning_packets(slave, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383 read_unlock(&bond->curr_slave_lock);
1384
1385 bond_info->lp_counter = 0;
1386 }
1387
1388 /* rebalance tx traffic */
1389 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1390
1391 read_lock(&bond->curr_slave_lock);
1392
1393 bond_for_each_slave(bond, slave, i) {
1394 tlb_clear_slave(bond, slave, 1);
1395 if (slave == bond->curr_active_slave) {
1396 SLAVE_TLB_INFO(slave).load =
1397 bond_info->unbalanced_load /
1398 BOND_TLB_REBALANCE_INTERVAL;
1399 bond_info->unbalanced_load = 0;
1400 }
1401 }
1402
1403 read_unlock(&bond->curr_slave_lock);
1404
1405 bond_info->tx_rebalance_counter = 0;
1406 }
1407
1408 /* handle rlb stuff */
1409 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (bond_info->primary_is_promisc &&
1411 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1412
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001413 /*
1414 * dev_set_promiscuity requires rtnl and
1415 * nothing else.
1416 */
1417 read_unlock(&bond->lock);
1418 rtnl_lock();
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 bond_info->rlb_promisc_timeout_counter = 0;
1421
1422 /* If the primary was set to promiscuous mode
1423 * because a slave was disabled then
1424 * it can now leave promiscuous mode.
1425 */
1426 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1427 bond_info->primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001429 rtnl_unlock();
1430 read_lock(&bond->lock);
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (bond_info->rlb_rebalance) {
1434 bond_info->rlb_rebalance = 0;
1435 rlb_rebalance(bond);
1436 }
1437
1438 /* check if clients need updating */
1439 if (bond_info->rx_ntt) {
1440 if (bond_info->rlb_update_delay_counter) {
1441 --bond_info->rlb_update_delay_counter;
1442 } else {
1443 rlb_update_rx_clients(bond);
1444 if (bond_info->rlb_update_retry_counter) {
1445 --bond_info->rlb_update_retry_counter;
1446 } else {
1447 bond_info->rx_ntt = 0;
1448 }
1449 }
1450 }
1451 }
1452
1453re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001454 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455out:
1456 read_unlock(&bond->lock);
1457}
1458
1459/* assumption: called before the slave is attached to the bond
1460 * and not locked by the bond lock
1461 */
1462int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1463{
1464 int res;
1465
1466 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1467 bond->alb_info.rlb_enabled);
1468 if (res) {
1469 return res;
1470 }
1471
1472 /* caller must hold the bond lock for write since the mac addresses
1473 * are compared and may be swapped.
1474 */
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001475 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 res = alb_handle_addr_collision_on_attach(bond, slave);
1478
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001479 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 if (res) {
1482 return res;
1483 }
1484
1485 tlb_init_slave(slave);
1486
1487 /* order a rebalance ASAP */
1488 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1489
1490 if (bond->alb_info.rlb_enabled) {
1491 bond->alb_info.rlb_rebalance = 1;
1492 }
1493
1494 return 0;
1495}
1496
Jay Vosburgh25433312008-01-17 16:24:59 -08001497/*
1498 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1499 * if necessary.
1500 *
1501 * Caller must hold RTNL and no other locks
1502 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1504{
1505 if (bond->slave_cnt > 1) {
1506 alb_change_hw_addr_on_detach(bond, slave);
1507 }
1508
1509 tlb_clear_slave(bond, slave, 0);
1510
1511 if (bond->alb_info.rlb_enabled) {
1512 bond->alb_info.next_rx_slave = NULL;
1513 rlb_clear_slave(bond, slave);
1514 }
1515}
1516
1517/* Caller must hold bond lock for read */
1518void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1519{
1520 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1521
1522 if (link == BOND_LINK_DOWN) {
1523 tlb_clear_slave(bond, slave, 0);
1524 if (bond->alb_info.rlb_enabled) {
1525 rlb_clear_slave(bond, slave);
1526 }
1527 } else if (link == BOND_LINK_UP) {
1528 /* order a rebalance ASAP */
1529 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1530 if (bond->alb_info.rlb_enabled) {
1531 bond->alb_info.rlb_rebalance = 1;
1532 /* If the updelay module parameter is smaller than the
1533 * forwarding delay of the switch the rebalance will
1534 * not work because the rebalance arp replies will
1535 * not be forwarded to the clients..
1536 */
1537 }
1538 }
1539}
1540
1541/**
1542 * bond_alb_handle_active_change - assign new curr_active_slave
1543 * @bond: our bonding struct
1544 * @new_slave: new slave to assign
1545 *
1546 * Set the bond->curr_active_slave to @new_slave and handle
1547 * mac address swapping and promiscuity changes as needed.
1548 *
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001549 * If new_slave is NULL, caller must hold curr_slave_lock or
1550 * bond->lock for write.
1551 *
1552 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1553 * read and curr_slave_lock for write. Processing here may sleep, so
1554 * no other locks may be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
1556void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001557 __releases(&bond->curr_slave_lock)
1558 __releases(&bond->lock)
1559 __acquires(&bond->lock)
1560 __acquires(&bond->curr_slave_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct slave *swap_slave;
1563 int i;
1564
1565 if (bond->curr_active_slave == new_slave) {
1566 return;
1567 }
1568
1569 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1570 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1571 bond->alb_info.primary_is_promisc = 0;
1572 bond->alb_info.rlb_promisc_timeout_counter = 0;
1573 }
1574
1575 swap_slave = bond->curr_active_slave;
1576 bond->curr_active_slave = new_slave;
1577
1578 if (!new_slave || (bond->slave_cnt == 0)) {
1579 return;
1580 }
1581
1582 /* set the new curr_active_slave to the bonds mac address
1583 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1584 */
1585 if (!swap_slave) {
1586 struct slave *tmp_slave;
1587 /* find slave that is holding the bond's mac address */
1588 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001589 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1590 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 swap_slave = tmp_slave;
1592 break;
1593 }
1594 }
1595 }
1596
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001597 /*
1598 * Arrange for swap_slave and new_slave to temporarily be
1599 * ignored so we can mess with their MAC addresses without
1600 * fear of interference from transmit activity.
1601 */
1602 if (swap_slave) {
1603 tlb_clear_slave(bond, swap_slave, 1);
1604 }
1605 tlb_clear_slave(bond, new_slave, 1);
1606
1607 write_unlock_bh(&bond->curr_slave_lock);
1608 read_unlock(&bond->lock);
1609
Jay Vosburghe0138a62008-01-17 16:24:58 -08001610 ASSERT_RTNL();
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1613 if (swap_slave) {
1614 /* swap mac address */
1615 alb_swap_mac_addr(bond, swap_slave, new_slave);
1616 } else {
1617 /* set the new_slave to the bond mac address */
1618 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1619 bond->alb_info.rlb_enabled);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001620 }
1621
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001622 if (swap_slave) {
1623 alb_fasten_mac_swap(bond, swap_slave, new_slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001624 read_lock(&bond->lock);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001625 } else {
Jay Vosburgh25433312008-01-17 16:24:59 -08001626 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1628 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001629
1630 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001633/*
1634 * Called with RTNL
1635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001637 __acquires(&bond->lock)
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001638 __releases(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Wang Chen454d7c92008-11-12 23:37:49 -08001640 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 struct sockaddr *sa = addr;
1642 struct slave *slave, *swap_slave;
1643 int res;
1644 int i;
1645
1646 if (!is_valid_ether_addr(sa->sa_data)) {
1647 return -EADDRNOTAVAIL;
1648 }
1649
1650 res = alb_set_mac_address(bond, addr);
1651 if (res) {
1652 return res;
1653 }
1654
1655 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1656
1657 /* If there is no curr_active_slave there is nothing else to do.
1658 * Otherwise we'll need to pass the new address to it and handle
1659 * duplications.
1660 */
1661 if (!bond->curr_active_slave) {
1662 return 0;
1663 }
1664
1665 swap_slave = NULL;
1666
1667 bond_for_each_slave(bond, slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001668 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1669 bond_dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 swap_slave = slave;
1671 break;
1672 }
1673 }
1674
1675 if (swap_slave) {
1676 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001677 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 } else {
1679 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1680 bond->alb_info.rlb_enabled);
1681
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001682 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1684 if (bond->alb_info.rlb_enabled) {
1685 /* inform clients mac address has changed */
1686 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1687 }
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001688 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return 0;
1692}
1693
1694void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1695{
1696 if (bond->alb_info.current_alb_vlan &&
1697 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1698 bond->alb_info.current_alb_vlan = NULL;
1699 }
1700
1701 if (bond->alb_info.rlb_enabled) {
1702 rlb_clear_vlan(bond, vlan_id);
1703 }
1704}
1705