blob: 25c14c6236f588fa84b8b93888a924d16a054993 [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
47#define ALB_TIMER_TICKS_PER_SEC 10 /* should be a divisor of HZ */
48#define BOND_TLB_REBALANCE_INTERVAL 10 /* In seconds, periodic re-balancing.
49 * Used for division - never set
50 * to zero !!!
51 */
52#define BOND_ALB_LP_INTERVAL 1 /* In seconds, periodic send of
53 * learning packets to the switch
54 */
55
56#define BOND_TLB_REBALANCE_TICKS (BOND_TLB_REBALANCE_INTERVAL \
57 * ALB_TIMER_TICKS_PER_SEC)
58
59#define BOND_ALB_LP_TICKS (BOND_ALB_LP_INTERVAL \
60 * ALB_TIMER_TICKS_PER_SEC)
61
62#define TLB_HASH_TABLE_SIZE 256 /* The size of the clients hash table.
63 * Note that this value MUST NOT be smaller
64 * because the key hash table is BYTE wide !
65 */
66
67
68#define TLB_NULL_INDEX 0xffffffff
69#define MAX_LP_BURST 3
70
71/* rlb defs */
72#define RLB_HASH_TABLE_SIZE 256
73#define RLB_NULL_INDEX 0xffffffff
74#define RLB_UPDATE_DELAY 2*ALB_TIMER_TICKS_PER_SEC /* 2 seconds */
75#define RLB_ARP_BURST_SIZE 2
76#define RLB_UPDATE_RETRY 3 /* 3-ticks - must be smaller than the rlb
77 * rebalance interval (5 min).
78 */
79/* RLB_PROMISC_TIMEOUT = 10 sec equals the time that the current slave is
80 * promiscuous after failover
81 */
82#define RLB_PROMISC_TIMEOUT 10*ALB_TIMER_TICKS_PER_SEC
83
Eric Dumazet885a1362009-09-01 06:31:18 +000084#ifndef __long_aligned
85#define __long_aligned __attribute__((aligned((sizeof(long)))))
86#endif
87static const u8 mac_bcast[ETH_ALEN] __long_aligned = {
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
89};
90static const u8 mac_v6_allmcast[ETH_ALEN] __long_aligned = {
91 0x33, 0x33, 0x00, 0x00, 0x00, 0x01
92};
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC;
94
95#pragma pack(1)
96struct learning_pkt {
97 u8 mac_dst[ETH_ALEN];
98 u8 mac_src[ETH_ALEN];
Al Virod3bb52b2007-08-22 20:06:58 -040099 __be16 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 u8 padding[ETH_ZLEN - ETH_HLEN];
101};
102
103struct arp_pkt {
Al Virod3bb52b2007-08-22 20:06:58 -0400104 __be16 hw_addr_space;
105 __be16 prot_addr_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 u8 hw_addr_len;
107 u8 prot_addr_len;
Al Virod3bb52b2007-08-22 20:06:58 -0400108 __be16 op_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 u8 mac_src[ETH_ALEN]; /* sender hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -0400110 __be32 ip_src; /* sender IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 u8 mac_dst[ETH_ALEN]; /* target hardware address */
Al Virod3bb52b2007-08-22 20:06:58 -0400112 __be32 ip_dst; /* target IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114#pragma pack()
115
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300116static inline struct arp_pkt *arp_pkt(const struct sk_buff *skb)
117{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700118 return (struct arp_pkt *)skb_network_header(skb);
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/* Forward declaration */
122static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[]);
123
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700124static inline u8 _simple_hash(const u8 *hash_start, int hash_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 int i;
127 u8 hash = 0;
128
129 for (i = 0; i < hash_size; i++) {
130 hash ^= hash_start[i];
131 }
132
133 return hash;
134}
135
136/*********************** tlb specific functions ***************************/
137
138static inline void _lock_tx_hashtbl(struct bonding *bond)
139{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700140 spin_lock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static inline void _unlock_tx_hashtbl(struct bonding *bond)
144{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700145 spin_unlock_bh(&(BOND_ALB_INFO(bond).tx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/* Caller must hold tx_hashtbl lock */
149static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load)
150{
151 if (save_load) {
152 entry->load_history = 1 + entry->tx_bytes /
153 BOND_TLB_REBALANCE_INTERVAL;
154 entry->tx_bytes = 0;
155 }
156
157 entry->tx_slave = NULL;
158 entry->next = TLB_NULL_INDEX;
159 entry->prev = TLB_NULL_INDEX;
160}
161
162static inline void tlb_init_slave(struct slave *slave)
163{
164 SLAVE_TLB_INFO(slave).load = 0;
165 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX;
166}
167
168/* Caller must hold bond lock for read */
169static void tlb_clear_slave(struct bonding *bond, struct slave *slave, int save_load)
170{
171 struct tlb_client_info *tx_hash_table;
172 u32 index;
173
174 _lock_tx_hashtbl(bond);
175
176 /* clear slave from tx_hashtbl */
177 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl;
178
Andy Gospodarekce39a802008-10-30 17:41:16 -0700179 /* skip this if we've already freed the tx hash table */
180 if (tx_hash_table) {
181 index = SLAVE_TLB_INFO(slave).head;
182 while (index != TLB_NULL_INDEX) {
183 u32 next_index = tx_hash_table[index].next;
184 tlb_init_table_entry(&tx_hash_table[index], save_load);
185 index = next_index;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 tlb_init_slave(slave);
Jay Vosburgh5af47b22006-01-09 12:14:00 -0800190
191 _unlock_tx_hashtbl(bond);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194/* Must be called before starting the monitor timer */
195static int tlb_initialize(struct bonding *bond)
196{
197 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
198 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info);
Mitch Williams0d206a32005-11-09 10:35:30 -0800199 struct tlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int i;
201
202 spin_lock_init(&(bond_info->tx_hashtbl_lock));
203
Joe Jin243cb4e2007-02-06 14:16:40 -0800204 new_hashtbl = kzalloc(size, GFP_KERNEL);
Mitch Williams0d206a32005-11-09 10:35:30 -0800205 if (!new_hashtbl) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800206 pr_err("%s: Error: Failed to allocate TLB hash table\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -1;
209 }
Mitch Williams0d206a32005-11-09 10:35:30 -0800210 _lock_tx_hashtbl(bond);
211
212 bond_info->tx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) {
215 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 1);
216 }
217
218 _unlock_tx_hashtbl(bond);
219
220 return 0;
221}
222
223/* Must be called only after all slaves have been released */
224static void tlb_deinitialize(struct bonding *bond)
225{
226 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
227
228 _lock_tx_hashtbl(bond);
229
230 kfree(bond_info->tx_hashtbl);
231 bond_info->tx_hashtbl = NULL;
232
233 _unlock_tx_hashtbl(bond);
234}
235
Jiri Pirko097811b2010-05-19 03:26:39 +0000236static long long compute_gap(struct slave *slave)
237{
238 return (s64) (slave->speed << 20) - /* Convert to Megabit per sec */
239 (s64) (SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/* Caller must hold bond lock for read */
243static struct slave *tlb_get_least_loaded_slave(struct bonding *bond)
244{
245 struct slave *slave, *least_loaded;
Jiri Pirko097811b2010-05-19 03:26:39 +0000246 long long max_gap;
247 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jiri Pirko097811b2010-05-19 03:26:39 +0000249 least_loaded = NULL;
250 max_gap = LLONG_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Find the slave with the largest gap */
Jiri Pirko097811b2010-05-19 03:26:39 +0000253 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (SLAVE_IS_OK(slave)) {
Jiri Pirko097811b2010-05-19 03:26:39 +0000255 long long gap = compute_gap(slave);
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (max_gap < gap) {
258 least_loaded = slave;
259 max_gap = gap;
260 }
261 }
262 }
263
264 return least_loaded;
265}
266
267/* Caller must hold bond lock for read */
268static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, u32 skb_len)
269{
270 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
271 struct tlb_client_info *hash_table;
272 struct slave *assigned_slave;
273
274 _lock_tx_hashtbl(bond);
275
276 hash_table = bond_info->tx_hashtbl;
277 assigned_slave = hash_table[hash_index].tx_slave;
278 if (!assigned_slave) {
279 assigned_slave = tlb_get_least_loaded_slave(bond);
280
281 if (assigned_slave) {
282 struct tlb_slave_info *slave_info =
283 &(SLAVE_TLB_INFO(assigned_slave));
284 u32 next_index = slave_info->head;
285
286 hash_table[hash_index].tx_slave = assigned_slave;
287 hash_table[hash_index].next = next_index;
288 hash_table[hash_index].prev = TLB_NULL_INDEX;
289
290 if (next_index != TLB_NULL_INDEX) {
291 hash_table[next_index].prev = hash_index;
292 }
293
294 slave_info->head = hash_index;
295 slave_info->load +=
296 hash_table[hash_index].load_history;
297 }
298 }
299
300 if (assigned_slave) {
301 hash_table[hash_index].tx_bytes += skb_len;
302 }
303
304 _unlock_tx_hashtbl(bond);
305
306 return assigned_slave;
307}
308
309/*********************** rlb specific functions ***************************/
310static inline void _lock_rx_hashtbl(struct bonding *bond)
311{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700312 spin_lock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315static inline void _unlock_rx_hashtbl(struct bonding *bond)
316{
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700317 spin_unlock_bh(&(BOND_ALB_INFO(bond).rx_hashtbl_lock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320/* when an ARP REPLY is received from a client update its info
321 * in the rx_hashtbl
322 */
323static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp)
324{
325 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
326 struct rlb_client_info *client_info;
327 u32 hash_index;
328
329 _lock_rx_hashtbl(bond);
330
331 hash_index = _simple_hash((u8*)&(arp->ip_src), sizeof(arp->ip_src));
332 client_info = &(bond_info->rx_hashtbl[hash_index]);
333
334 if ((client_info->assigned) &&
335 (client_info->ip_src == arp->ip_dst) &&
336 (client_info->ip_dst == arp->ip_src)) {
337 /* update the clients MAC address */
338 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
339 client_info->ntt = 1;
340 bond_info->rx_ntt = 1;
341 }
342
343 _unlock_rx_hashtbl(bond);
344}
345
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700346static int rlb_arp_recv(struct sk_buff *skb, struct net_device *bond_dev, struct packet_type *ptype, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800348 struct bonding *bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
350 int res = NET_RX_DROP;
351
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800352 while (bond_dev->priv_flags & IFF_802_1Q_VLAN)
353 bond_dev = vlan_dev_real_dev(bond_dev);
354
355 if (!(bond_dev->priv_flags & IFF_BONDING) ||
356 !(bond_dev->flags & IFF_MASTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 if (!arp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800360 pr_debug("Packet has no ARP data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 goto out;
362 }
363
364 if (skb->len < sizeof(struct arp_pkt)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800365 pr_debug("Packet is too small to be an ARP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto out;
367 }
368
369 if (arp->op_code == htons(ARPOP_REPLY)) {
370 /* update rx hash table for this ARP */
Wang Chen454d7c92008-11-12 23:37:49 -0800371 bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 rlb_update_entry_from_arp(bond, arp);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800373 pr_debug("Server received an ARP Reply from client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 res = NET_RX_SUCCESS;
377
378out:
379 dev_kfree_skb(skb);
380
381 return res;
382}
383
384/* Caller must hold bond lock for read */
385static struct slave *rlb_next_rx_slave(struct bonding *bond)
386{
387 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
388 struct slave *rx_slave, *slave, *start_at;
389 int i = 0;
390
391 if (bond_info->next_rx_slave) {
392 start_at = bond_info->next_rx_slave;
393 } else {
394 start_at = bond->first_slave;
395 }
396
397 rx_slave = NULL;
398
399 bond_for_each_slave_from(bond, slave, i, start_at) {
400 if (SLAVE_IS_OK(slave)) {
401 if (!rx_slave) {
402 rx_slave = slave;
403 } else if (slave->speed > rx_slave->speed) {
404 rx_slave = slave;
405 }
406 }
407 }
408
409 if (rx_slave) {
410 bond_info->next_rx_slave = rx_slave->next;
411 }
412
413 return rx_slave;
414}
415
416/* teach the switch the mac of a disabled slave
417 * on the primary for fault tolerance
418 *
419 * Caller must hold bond->curr_slave_lock for write or bond lock for write
420 */
421static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
422{
423 if (!bond->curr_active_slave) {
424 return;
425 }
426
427 if (!bond->alb_info.primary_is_promisc) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700428 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
429 bond->alb_info.primary_is_promisc = 1;
430 else
431 bond->alb_info.primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
434 bond->alb_info.rlb_promisc_timeout_counter = 0;
435
436 alb_send_learning_packets(bond->curr_active_slave, addr);
437}
438
439/* slave being removed should not be active at this point
440 *
441 * Caller must hold bond lock for read
442 */
443static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
444{
445 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
446 struct rlb_client_info *rx_hash_table;
447 u32 index, next_index;
448
449 /* clear slave from rx_hashtbl */
450 _lock_rx_hashtbl(bond);
451
452 rx_hash_table = bond_info->rx_hashtbl;
453 index = bond_info->rx_hashtbl_head;
454 for (; index != RLB_NULL_INDEX; index = next_index) {
455 next_index = rx_hash_table[index].next;
456 if (rx_hash_table[index].slave == slave) {
457 struct slave *assigned_slave = rlb_next_rx_slave(bond);
458
459 if (assigned_slave) {
460 rx_hash_table[index].slave = assigned_slave;
Eric Dumazet885a1362009-09-01 06:31:18 +0000461 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
462 mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 bond_info->rx_hashtbl[index].ntt = 1;
464 bond_info->rx_ntt = 1;
465 /* A slave has been removed from the
466 * table because it is either disabled
467 * or being released. We must retry the
468 * update to avoid clients from not
469 * being updated & disconnecting when
470 * there is stress
471 */
472 bond_info->rlb_update_retry_counter =
473 RLB_UPDATE_RETRY;
474 }
475 } else { /* there is no active slave */
476 rx_hash_table[index].slave = NULL;
477 }
478 }
479 }
480
481 _unlock_rx_hashtbl(bond);
482
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700483 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (slave != bond->curr_active_slave) {
486 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
487 }
488
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700489 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static void rlb_update_client(struct rlb_client_info *client_info)
493{
494 int i;
495
496 if (!client_info->slave) {
497 return;
498 }
499
500 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
501 struct sk_buff *skb;
502
503 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
504 client_info->ip_dst,
505 client_info->slave->dev,
506 client_info->ip_src,
507 client_info->mac_dst,
508 client_info->slave->dev->dev_addr,
509 client_info->mac_dst);
510 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800511 pr_err("%s: Error: failed to create an ARP packet\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 skb->dev = client_info->slave->dev;
517
518 if (client_info->tag) {
519 skb = vlan_put_tag(skb, client_info->vlan_id);
520 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800521 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800522 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 continue;
524 }
525 }
526
527 arp_xmit(skb);
528 }
529}
530
531/* sends ARP REPLIES that update the clients that need updating */
532static void rlb_update_rx_clients(struct bonding *bond)
533{
534 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
535 struct rlb_client_info *client_info;
536 u32 hash_index;
537
538 _lock_rx_hashtbl(bond);
539
540 hash_index = bond_info->rx_hashtbl_head;
541 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
542 client_info = &(bond_info->rx_hashtbl[hash_index]);
543 if (client_info->ntt) {
544 rlb_update_client(client_info);
545 if (bond_info->rlb_update_retry_counter == 0) {
546 client_info->ntt = 0;
547 }
548 }
549 }
550
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200551 /* do not update the entries again until this counter is zero so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 * not to confuse the clients.
553 */
554 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
555
556 _unlock_rx_hashtbl(bond);
557}
558
559/* The slave was assigned a new mac address - update the clients */
560static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
561{
562 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
563 struct rlb_client_info *client_info;
564 int ntt = 0;
565 u32 hash_index;
566
567 _lock_rx_hashtbl(bond);
568
569 hash_index = bond_info->rx_hashtbl_head;
570 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
571 client_info = &(bond_info->rx_hashtbl[hash_index]);
572
573 if ((client_info->slave == slave) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000574 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 client_info->ntt = 1;
576 ntt = 1;
577 }
578 }
579
580 // update the team's flag only after the whole iteration
581 if (ntt) {
582 bond_info->rx_ntt = 1;
583 //fasten the change
584 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
585 }
586
587 _unlock_rx_hashtbl(bond);
588}
589
590/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400591static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
594 struct rlb_client_info *client_info;
595 u32 hash_index;
596
597 _lock_rx_hashtbl(bond);
598
599 hash_index = bond_info->rx_hashtbl_head;
600 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
601 client_info = &(bond_info->rx_hashtbl[hash_index]);
602
603 if (!client_info->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800604 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800605 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 continue;
607 }
608 /*update all clients using this src_ip, that are not assigned
609 * to the team's address (curr_active_slave) and have a known
610 * unicast mac address.
611 */
612 if ((client_info->ip_src == src_ip) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000613 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
614 bond->dev->dev_addr) &&
615 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 client_info->ntt = 1;
617 bond_info->rx_ntt = 1;
618 }
619 }
620
621 _unlock_rx_hashtbl(bond);
622}
623
624/* Caller must hold both bond and ptr locks for read */
625static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
626{
627 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300628 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct slave *assigned_slave;
630 struct rlb_client_info *client_info;
631 u32 hash_index = 0;
632
633 _lock_rx_hashtbl(bond);
634
635 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
636 client_info = &(bond_info->rx_hashtbl[hash_index]);
637
638 if (client_info->assigned) {
639 if ((client_info->ip_src == arp->ip_src) &&
640 (client_info->ip_dst == arp->ip_dst)) {
641 /* the entry is already assigned to this client */
Eric Dumazet885a1362009-09-01 06:31:18 +0000642 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /* update mac address from arp */
644 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
645 }
646
647 assigned_slave = client_info->slave;
648 if (assigned_slave) {
649 _unlock_rx_hashtbl(bond);
650 return assigned_slave;
651 }
652 } else {
653 /* the entry is already assigned to some other client,
654 * move the old client to primary (curr_active_slave) so
655 * that the new client can be assigned to this entry.
656 */
657 if (bond->curr_active_slave &&
658 client_info->slave != bond->curr_active_slave) {
659 client_info->slave = bond->curr_active_slave;
660 rlb_update_client(client_info);
661 }
662 }
663 }
664 /* assign a new slave */
665 assigned_slave = rlb_next_rx_slave(bond);
666
667 if (assigned_slave) {
668 client_info->ip_src = arp->ip_src;
669 client_info->ip_dst = arp->ip_dst;
670 /* arp->mac_dst is broadcast for arp reqeusts.
671 * will be updated with clients actual unicast mac address
672 * upon receiving an arp reply.
673 */
674 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
675 client_info->slave = assigned_slave;
676
Eric Dumazet885a1362009-09-01 06:31:18 +0000677 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 client_info->ntt = 1;
679 bond->alb_info.rx_ntt = 1;
680 } else {
681 client_info->ntt = 0;
682 }
683
684 if (!list_empty(&bond->vlan_list)) {
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700685 if (!vlan_get_tag(skb, &client_info->vlan_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 client_info->tag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 if (!client_info->assigned) {
690 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
691 bond_info->rx_hashtbl_head = hash_index;
692 client_info->next = prev_tbl_head;
693 if (prev_tbl_head != RLB_NULL_INDEX) {
694 bond_info->rx_hashtbl[prev_tbl_head].prev =
695 hash_index;
696 }
697 client_info->assigned = 1;
698 }
699 }
700
701 _unlock_rx_hashtbl(bond);
702
703 return assigned_slave;
704}
705
706/* chooses (and returns) transmit channel for arp reply
707 * does not choose channel for other arp types since they are
708 * sent on the curr_active_slave
709 */
710static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
711{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300712 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct slave *tx_slave = NULL;
714
Brian Haleyf14c4e42008-09-02 10:08:08 -0400715 if (arp->op_code == htons(ARPOP_REPLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* the arp must be sent on the selected
717 * rx channel
718 */
719 tx_slave = rlb_choose_channel(skb, bond);
720 if (tx_slave) {
721 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
722 }
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800723 pr_debug("Server sent ARP Reply packet\n");
Brian Haleyf14c4e42008-09-02 10:08:08 -0400724 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* Create an entry in the rx_hashtbl for this client as a
726 * place holder.
727 * When the arp reply is received the entry will be updated
728 * with the correct unicast address of the client.
729 */
730 rlb_choose_channel(skb, bond);
731
732 /* The ARP relpy packets must be delayed so that
733 * they can cancel out the influence of the ARP request.
734 */
735 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
736
737 /* arp requests are broadcast and are sent on the primary
738 * the arp request will collapse all clients on the subnet to
739 * the primary slave. We must register these clients to be
740 * updated with their assigned mac.
741 */
742 rlb_req_update_subnet_clients(bond, arp->ip_src);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800743 pr_debug("Server sent ARP Request packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
746 return tx_slave;
747}
748
749/* Caller must hold bond lock for read */
750static void rlb_rebalance(struct bonding *bond)
751{
752 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
753 struct slave *assigned_slave;
754 struct rlb_client_info *client_info;
755 int ntt;
756 u32 hash_index;
757
758 _lock_rx_hashtbl(bond);
759
760 ntt = 0;
761 hash_index = bond_info->rx_hashtbl_head;
762 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
763 client_info = &(bond_info->rx_hashtbl[hash_index]);
764 assigned_slave = rlb_next_rx_slave(bond);
765 if (assigned_slave && (client_info->slave != assigned_slave)) {
766 client_info->slave = assigned_slave;
767 client_info->ntt = 1;
768 ntt = 1;
769 }
770 }
771
772 /* update the team's flag only after the whole iteration */
773 if (ntt) {
774 bond_info->rx_ntt = 1;
775 }
776 _unlock_rx_hashtbl(bond);
777}
778
779/* Caller must hold rx_hashtbl lock */
780static void rlb_init_table_entry(struct rlb_client_info *entry)
781{
782 memset(entry, 0, sizeof(struct rlb_client_info));
783 entry->next = RLB_NULL_INDEX;
784 entry->prev = RLB_NULL_INDEX;
785}
786
787static int rlb_initialize(struct bonding *bond)
788{
789 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
790 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
Mitch Williams0d206a32005-11-09 10:35:30 -0800791 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
793 int i;
794
795 spin_lock_init(&(bond_info->rx_hashtbl_lock));
796
Mitch Williams0d206a32005-11-09 10:35:30 -0800797 new_hashtbl = kmalloc(size, GFP_KERNEL);
798 if (!new_hashtbl) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800799 pr_err("%s: Error: Failed to allocate RLB hash table\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -1;
802 }
Mitch Williams0d206a32005-11-09 10:35:30 -0800803 _lock_rx_hashtbl(bond);
804
805 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
808
809 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
810 rlb_init_table_entry(bond_info->rx_hashtbl + i);
811 }
812
813 _unlock_rx_hashtbl(bond);
814
815 /*initialize packet type*/
Harvey Harrison09640e62009-02-01 00:45:17 -0800816 pk_type->type = cpu_to_be16(ETH_P_ARP);
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800817 pk_type->dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 pk_type->func = rlb_arp_recv;
819
820 /* register to receive ARPs */
821 dev_add_pack(pk_type);
822
823 return 0;
824}
825
826static void rlb_deinitialize(struct bonding *bond)
827{
828 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
829
830 dev_remove_pack(&(bond_info->rlb_pkt_type));
831
832 _lock_rx_hashtbl(bond);
833
834 kfree(bond_info->rx_hashtbl);
835 bond_info->rx_hashtbl = NULL;
836 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
837
838 _unlock_rx_hashtbl(bond);
839}
840
841static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
842{
843 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
844 u32 curr_index;
845
846 _lock_rx_hashtbl(bond);
847
848 curr_index = bond_info->rx_hashtbl_head;
849 while (curr_index != RLB_NULL_INDEX) {
850 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
851 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
852 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
853
854 if (curr->tag && (curr->vlan_id == vlan_id)) {
855 if (curr_index == bond_info->rx_hashtbl_head) {
856 bond_info->rx_hashtbl_head = next_index;
857 }
858 if (prev_index != RLB_NULL_INDEX) {
859 bond_info->rx_hashtbl[prev_index].next = next_index;
860 }
861 if (next_index != RLB_NULL_INDEX) {
862 bond_info->rx_hashtbl[next_index].prev = prev_index;
863 }
864
865 rlb_init_table_entry(curr);
866 }
867
868 curr_index = next_index;
869 }
870
871 _unlock_rx_hashtbl(bond);
872}
873
874/*********************** tlb/rlb shared functions *********************/
875
876static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
877{
878 struct bonding *bond = bond_get_bond_by_slave(slave);
879 struct learning_pkt pkt;
880 int size = sizeof(struct learning_pkt);
881 int i;
882
883 memset(&pkt, 0, size);
884 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
885 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
Harvey Harrison09640e62009-02-01 00:45:17 -0800886 pkt.type = cpu_to_be16(ETH_P_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 for (i = 0; i < MAX_LP_BURST; i++) {
889 struct sk_buff *skb;
890 char *data;
891
892 skb = dev_alloc_skb(size);
893 if (!skb) {
894 return;
895 }
896
897 data = skb_put(skb, size);
898 memcpy(data, &pkt, size);
899
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700900 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700901 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 skb->protocol = pkt.type;
903 skb->priority = TC_PRIO_CONTROL;
904 skb->dev = slave->dev;
905
906 if (!list_empty(&bond->vlan_list)) {
907 struct vlan_entry *vlan;
908
909 vlan = bond_next_vlan(bond,
910 bond->alb_info.current_alb_vlan);
911
912 bond->alb_info.current_alb_vlan = vlan;
913 if (!vlan) {
914 kfree_skb(skb);
915 continue;
916 }
917
918 skb = vlan_put_tag(skb, vlan->vlan_id);
919 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800920 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800921 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 continue;
923 }
924 }
925
926 dev_queue_xmit(skb);
927 }
928}
929
930/* hw is a boolean parameter that determines whether we should try and
931 * set the hw address of the device as well as the hw address of the
932 * net_device
933 */
934static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
935{
936 struct net_device *dev = slave->dev;
937 struct sockaddr s_addr;
938
939 if (!hw) {
940 memcpy(dev->dev_addr, addr, dev->addr_len);
941 return 0;
942 }
943
944 /* for rlb each slave must have a unique hw mac addresses so that */
945 /* each slave will receive packets destined to a different mac */
946 memcpy(s_addr.sa_data, addr, dev->addr_len);
947 s_addr.sa_family = dev->type;
948 if (dev_set_mac_address(dev, &s_addr)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800949 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
950 "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 -0800951 dev->master->name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return -EOPNOTSUPP;
953 }
954 return 0;
955}
956
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700957/*
958 * Swap MAC addresses between two slaves.
959 *
960 * Called with RTNL held, and no other locks.
961 *
962 */
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
965{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
969 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
970 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
971
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700972}
973
974/*
975 * Send learning packets after MAC address swap.
976 *
Jay Vosburgh25433312008-01-17 16:24:59 -0800977 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700978 */
979static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
980 struct slave *slave2)
981{
982 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
983 struct slave *disabled_slave = NULL;
984
Jay Vosburgh25433312008-01-17 16:24:59 -0800985 ASSERT_RTNL();
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* fasten the change in the switch */
988 if (SLAVE_IS_OK(slave1)) {
989 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
990 if (bond->alb_info.rlb_enabled) {
991 /* inform the clients that the mac address
992 * has changed
993 */
994 rlb_req_update_slave_clients(bond, slave1);
995 }
996 } else {
997 disabled_slave = slave1;
998 }
999
1000 if (SLAVE_IS_OK(slave2)) {
1001 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1002 if (bond->alb_info.rlb_enabled) {
1003 /* inform the clients that the mac address
1004 * has changed
1005 */
1006 rlb_req_update_slave_clients(bond, slave2);
1007 }
1008 } else {
1009 disabled_slave = slave2;
1010 }
1011
1012 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1013 /* A disabled slave was assigned an active mac addr */
1014 rlb_teach_disabled_mac_on_primary(bond,
1015 disabled_slave->dev->dev_addr);
1016 }
1017}
1018
1019/**
1020 * alb_change_hw_addr_on_detach
1021 * @bond: bonding we're working on
1022 * @slave: the slave that was just detached
1023 *
1024 * We assume that @slave was already detached from the slave list.
1025 *
1026 * If @slave's permanent hw address is different both from its current
1027 * address and from @bond's address, then somewhere in the bond there's
1028 * a slave that has @slave's permanet address as its current address.
1029 * We'll make sure that that slave no longer uses @slave's permanent address.
1030 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001031 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
1033static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1034{
1035 int perm_curr_diff;
1036 int perm_bond_diff;
1037
Eric Dumazet885a1362009-09-01 06:31:18 +00001038 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1039 slave->dev->dev_addr);
1040 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1041 bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (perm_curr_diff && perm_bond_diff) {
1044 struct slave *tmp_slave;
1045 int i, found = 0;
1046
1047 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001048 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
1049 tmp_slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 found = 1;
1051 break;
1052 }
1053 }
1054
1055 if (found) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001056 /* locking: needs RTNL and nothing else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 alb_swap_mac_addr(bond, slave, tmp_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001058 alb_fasten_mac_swap(bond, slave, tmp_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 }
1061}
1062
1063/**
1064 * alb_handle_addr_collision_on_attach
1065 * @bond: bonding we're working on
1066 * @slave: the slave that was just attached
1067 *
1068 * checks uniqueness of slave's mac address and handles the case the
1069 * new slave uses the bonds mac address.
1070 *
1071 * If the permanent hw address of @slave is @bond's hw address, we need to
1072 * find a different hw address to give @slave, that isn't in use by any other
1073 * slave in the bond. This address must be, of course, one of the premanent
1074 * addresses of the other slaves.
1075 *
1076 * We go over the slave list, and for each slave there we compare its
1077 * permanent hw address with the current address of all the other slaves.
1078 * If no match was found, then we've found a slave with a permanent address
1079 * that isn't used by any other slave in the bond, so we can assign it to
1080 * @slave.
1081 *
1082 * assumption: this function is called before @slave is attached to the
1083 * bond slave list.
1084 *
1085 * caller must hold the bond lock for write since the mac addresses are compared
1086 * and may be swapped.
1087 */
1088static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1089{
1090 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1091 struct slave *has_bond_addr = bond->curr_active_slave;
1092 int i, j, found = 0;
1093
1094 if (bond->slave_cnt == 0) {
1095 /* this is the first slave */
1096 return 0;
1097 }
1098
1099 /* if slave's mac address differs from bond's mac address
1100 * check uniqueness of slave's mac address against the other
1101 * slaves in the bond.
1102 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001103 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 bond_for_each_slave(bond, tmp_slave1, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001105 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1106 slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 found = 1;
1108 break;
1109 }
1110 }
1111
John W. Linville6b38aef2005-07-28 15:00:15 -04001112 if (!found)
1113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
John W. Linville6b38aef2005-07-28 15:00:15 -04001115 /* Try setting slave mac to bond address and fall-through
1116 to code handling that situation below... */
1117 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1118 bond->alb_info.rlb_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
1121 /* The slave's address is equal to the address of the bond.
1122 * Search for a spare address in the bond for this slave.
1123 */
1124 free_mac_slave = NULL;
1125
1126 bond_for_each_slave(bond, tmp_slave1, i) {
1127 found = 0;
1128 bond_for_each_slave(bond, tmp_slave2, j) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001129 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1130 tmp_slave2->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 found = 1;
1132 break;
1133 }
1134 }
1135
1136 if (!found) {
1137 /* no slave has tmp_slave1's perm addr
1138 * as its curr addr
1139 */
1140 free_mac_slave = tmp_slave1;
1141 break;
1142 }
1143
1144 if (!has_bond_addr) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001145 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1146 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 has_bond_addr = tmp_slave1;
1149 }
1150 }
1151 }
1152
1153 if (free_mac_slave) {
1154 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1155 bond->alb_info.rlb_enabled);
1156
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001157 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 +00001158 bond->dev->name, slave->dev->name,
1159 free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 } else if (has_bond_addr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001162 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 -08001163 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return -EFAULT;
1165 }
1166
1167 return 0;
1168}
1169
1170/**
1171 * alb_set_mac_address
1172 * @bond:
1173 * @addr:
1174 *
1175 * In TLB mode all slaves are configured to the bond's hw address, but set
1176 * their dev_addr field to different addresses (based on their permanent hw
1177 * addresses).
1178 *
1179 * For each slave, this function sets the interface to the new address and then
1180 * changes its dev_addr field to its previous value.
1181 *
1182 * Unwinding assumes bond's mac address has not yet changed.
1183 */
1184static int alb_set_mac_address(struct bonding *bond, void *addr)
1185{
1186 struct sockaddr sa;
1187 struct slave *slave, *stop_at;
1188 char tmp_addr[ETH_ALEN];
1189 int res;
1190 int i;
1191
1192 if (bond->alb_info.rlb_enabled) {
1193 return 0;
1194 }
1195
1196 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* save net_device's current hw address */
1198 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1199
1200 res = dev_set_mac_address(slave->dev, addr);
1201
1202 /* restore net_device's hw address */
1203 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1204
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001205 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
1209 return 0;
1210
1211unwind:
1212 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1213 sa.sa_family = bond->dev->type;
1214
1215 /* unwind from head to the slave that failed */
1216 stop_at = slave;
1217 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1218 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1219 dev_set_mac_address(slave->dev, &sa);
1220 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1221 }
1222
1223 return res;
1224}
1225
1226/************************ exported alb funcions ************************/
1227
1228int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1229{
1230 int res;
1231
1232 res = tlb_initialize(bond);
1233 if (res) {
1234 return res;
1235 }
1236
1237 if (rlb_enabled) {
1238 bond->alb_info.rlb_enabled = 1;
1239 /* initialize rlb */
1240 res = rlb_initialize(bond);
1241 if (res) {
1242 tlb_deinitialize(bond);
1243 return res;
1244 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001245 } else {
1246 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
1249 return 0;
1250}
1251
1252void bond_alb_deinitialize(struct bonding *bond)
1253{
1254 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1255
1256 tlb_deinitialize(bond);
1257
1258 if (bond_info->rlb_enabled) {
1259 rlb_deinitialize(bond);
1260 }
1261}
1262
1263int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1264{
Wang Chen454d7c92008-11-12 23:37:49 -08001265 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct ethhdr *eth_data;
1267 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1268 struct slave *tx_slave = NULL;
Al Virod3bb52b2007-08-22 20:06:58 -04001269 static const __be32 ip_bcast = htonl(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 int hash_size = 0;
1271 int do_tx_balance = 1;
1272 u32 hash_index = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001273 const u8 *hash_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int res = 1;
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001275 struct ipv6hdr *ip6hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001277 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 eth_data = eth_hdr(skb);
1279
1280 /* make sure that the curr_active_slave and the slaves list do
1281 * not change during tx
1282 */
1283 read_lock(&bond->lock);
1284 read_lock(&bond->curr_slave_lock);
1285
1286 if (!BOND_IS_OK(bond)) {
1287 goto out;
1288 }
1289
1290 switch (ntohs(skb->protocol)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001291 case ETH_P_IP: {
1292 const struct iphdr *iph = ip_hdr(skb);
1293
Eric Dumazet885a1362009-09-01 06:31:18 +00001294 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001295 (iph->daddr == ip_bcast) ||
1296 (iph->protocol == IPPROTO_IGMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 do_tx_balance = 0;
1298 break;
1299 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001300 hash_start = (char *)&(iph->daddr);
1301 hash_size = sizeof(iph->daddr);
1302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304 case ETH_P_IPV6:
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001305 /* IPv6 doesn't really use broadcast mac address, but leave
1306 * that here just in case.
1307 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001308 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 do_tx_balance = 0;
1310 break;
1311 }
1312
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001313 /* IPv6 uses all-nodes multicast as an equivalent to
1314 * broadcasts in IPv4.
1315 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001316 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001317 do_tx_balance = 0;
1318 break;
1319 }
1320
1321 /* Additianally, DAD probes should not be tx-balanced as that
1322 * will lead to false positives for duplicate addresses and
1323 * prevent address configuration from working.
1324 */
1325 ip6hdr = ipv6_hdr(skb);
1326 if (ipv6_addr_any(&ip6hdr->saddr)) {
1327 do_tx_balance = 0;
1328 break;
1329 }
1330
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001331 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1332 hash_size = sizeof(ipv6_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 break;
1334 case ETH_P_IPX:
Al Virod3bb52b2007-08-22 20:06:58 -04001335 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /* something is wrong with this packet */
1337 do_tx_balance = 0;
1338 break;
1339 }
1340
1341 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1342 /* The only protocol worth balancing in
1343 * this family since it has an "ARP" like
1344 * mechanism
1345 */
1346 do_tx_balance = 0;
1347 break;
1348 }
1349
1350 hash_start = (char*)eth_data->h_dest;
1351 hash_size = ETH_ALEN;
1352 break;
1353 case ETH_P_ARP:
1354 do_tx_balance = 0;
1355 if (bond_info->rlb_enabled) {
1356 tx_slave = rlb_arp_xmit(skb, bond);
1357 }
1358 break;
1359 default:
1360 do_tx_balance = 0;
1361 break;
1362 }
1363
1364 if (do_tx_balance) {
1365 hash_index = _simple_hash(hash_start, hash_size);
1366 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1367 }
1368
1369 if (!tx_slave) {
1370 /* unbalanced or unassigned, send through primary */
1371 tx_slave = bond->curr_active_slave;
1372 bond_info->unbalanced_load += skb->len;
1373 }
1374
1375 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1376 if (tx_slave != bond->curr_active_slave) {
1377 memcpy(eth_data->h_source,
1378 tx_slave->dev->dev_addr,
1379 ETH_ALEN);
1380 }
1381
1382 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1383 } else {
1384 if (tx_slave) {
1385 tlb_clear_slave(bond, tx_slave, 0);
1386 }
1387 }
1388
1389out:
1390 if (res) {
1391 /* no suitable interface, frame not sent */
1392 dev_kfree_skb(skb);
1393 }
1394 read_unlock(&bond->curr_slave_lock);
1395 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07001396 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001399void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001401 struct bonding *bond = container_of(work, struct bonding,
1402 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1404 struct slave *slave;
1405 int i;
1406
1407 read_lock(&bond->lock);
1408
1409 if (bond->kill_timers) {
1410 goto out;
1411 }
1412
1413 if (bond->slave_cnt == 0) {
1414 bond_info->tx_rebalance_counter = 0;
1415 bond_info->lp_counter = 0;
1416 goto re_arm;
1417 }
1418
1419 bond_info->tx_rebalance_counter++;
1420 bond_info->lp_counter++;
1421
1422 /* send learning packets */
1423 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1424 /* change of curr_active_slave involves swapping of mac addresses.
1425 * in order to avoid this swapping from happening while
1426 * sending the learning packets, the curr_slave_lock must be held for
1427 * read.
1428 */
1429 read_lock(&bond->curr_slave_lock);
1430
1431 bond_for_each_slave(bond, slave, i) {
Mitch Williamse944ef72005-11-09 10:36:50 -08001432 alb_send_learning_packets(slave, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
1435 read_unlock(&bond->curr_slave_lock);
1436
1437 bond_info->lp_counter = 0;
1438 }
1439
1440 /* rebalance tx traffic */
1441 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1442
1443 read_lock(&bond->curr_slave_lock);
1444
1445 bond_for_each_slave(bond, slave, i) {
1446 tlb_clear_slave(bond, slave, 1);
1447 if (slave == bond->curr_active_slave) {
1448 SLAVE_TLB_INFO(slave).load =
1449 bond_info->unbalanced_load /
1450 BOND_TLB_REBALANCE_INTERVAL;
1451 bond_info->unbalanced_load = 0;
1452 }
1453 }
1454
1455 read_unlock(&bond->curr_slave_lock);
1456
1457 bond_info->tx_rebalance_counter = 0;
1458 }
1459
1460 /* handle rlb stuff */
1461 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (bond_info->primary_is_promisc &&
1463 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1464
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001465 /*
1466 * dev_set_promiscuity requires rtnl and
1467 * nothing else.
1468 */
1469 read_unlock(&bond->lock);
1470 rtnl_lock();
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 bond_info->rlb_promisc_timeout_counter = 0;
1473
1474 /* If the primary was set to promiscuous mode
1475 * because a slave was disabled then
1476 * it can now leave promiscuous mode.
1477 */
1478 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1479 bond_info->primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001481 rtnl_unlock();
1482 read_lock(&bond->lock);
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 if (bond_info->rlb_rebalance) {
1486 bond_info->rlb_rebalance = 0;
1487 rlb_rebalance(bond);
1488 }
1489
1490 /* check if clients need updating */
1491 if (bond_info->rx_ntt) {
1492 if (bond_info->rlb_update_delay_counter) {
1493 --bond_info->rlb_update_delay_counter;
1494 } else {
1495 rlb_update_rx_clients(bond);
1496 if (bond_info->rlb_update_retry_counter) {
1497 --bond_info->rlb_update_retry_counter;
1498 } else {
1499 bond_info->rx_ntt = 0;
1500 }
1501 }
1502 }
1503 }
1504
1505re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001506 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507out:
1508 read_unlock(&bond->lock);
1509}
1510
1511/* assumption: called before the slave is attached to the bond
1512 * and not locked by the bond lock
1513 */
1514int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1515{
1516 int res;
1517
1518 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1519 bond->alb_info.rlb_enabled);
1520 if (res) {
1521 return res;
1522 }
1523
1524 /* caller must hold the bond lock for write since the mac addresses
1525 * are compared and may be swapped.
1526 */
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001527 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 res = alb_handle_addr_collision_on_attach(bond, slave);
1530
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001531 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (res) {
1534 return res;
1535 }
1536
1537 tlb_init_slave(slave);
1538
1539 /* order a rebalance ASAP */
1540 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1541
1542 if (bond->alb_info.rlb_enabled) {
1543 bond->alb_info.rlb_rebalance = 1;
1544 }
1545
1546 return 0;
1547}
1548
Jay Vosburgh25433312008-01-17 16:24:59 -08001549/*
1550 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1551 * if necessary.
1552 *
1553 * Caller must hold RTNL and no other locks
1554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1556{
1557 if (bond->slave_cnt > 1) {
1558 alb_change_hw_addr_on_detach(bond, slave);
1559 }
1560
1561 tlb_clear_slave(bond, slave, 0);
1562
1563 if (bond->alb_info.rlb_enabled) {
1564 bond->alb_info.next_rx_slave = NULL;
1565 rlb_clear_slave(bond, slave);
1566 }
1567}
1568
1569/* Caller must hold bond lock for read */
1570void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1571{
1572 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1573
1574 if (link == BOND_LINK_DOWN) {
1575 tlb_clear_slave(bond, slave, 0);
1576 if (bond->alb_info.rlb_enabled) {
1577 rlb_clear_slave(bond, slave);
1578 }
1579 } else if (link == BOND_LINK_UP) {
1580 /* order a rebalance ASAP */
1581 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1582 if (bond->alb_info.rlb_enabled) {
1583 bond->alb_info.rlb_rebalance = 1;
1584 /* If the updelay module parameter is smaller than the
1585 * forwarding delay of the switch the rebalance will
1586 * not work because the rebalance arp replies will
1587 * not be forwarded to the clients..
1588 */
1589 }
1590 }
1591}
1592
1593/**
1594 * bond_alb_handle_active_change - assign new curr_active_slave
1595 * @bond: our bonding struct
1596 * @new_slave: new slave to assign
1597 *
1598 * Set the bond->curr_active_slave to @new_slave and handle
1599 * mac address swapping and promiscuity changes as needed.
1600 *
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001601 * If new_slave is NULL, caller must hold curr_slave_lock or
1602 * bond->lock for write.
1603 *
1604 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1605 * read and curr_slave_lock for write. Processing here may sleep, so
1606 * no other locks may be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
1608void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001609 __releases(&bond->curr_slave_lock)
1610 __releases(&bond->lock)
1611 __acquires(&bond->lock)
1612 __acquires(&bond->curr_slave_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 struct slave *swap_slave;
1615 int i;
1616
1617 if (bond->curr_active_slave == new_slave) {
1618 return;
1619 }
1620
1621 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1622 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1623 bond->alb_info.primary_is_promisc = 0;
1624 bond->alb_info.rlb_promisc_timeout_counter = 0;
1625 }
1626
1627 swap_slave = bond->curr_active_slave;
1628 bond->curr_active_slave = new_slave;
1629
1630 if (!new_slave || (bond->slave_cnt == 0)) {
1631 return;
1632 }
1633
1634 /* set the new curr_active_slave to the bonds mac address
1635 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1636 */
1637 if (!swap_slave) {
1638 struct slave *tmp_slave;
1639 /* find slave that is holding the bond's mac address */
1640 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001641 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1642 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 swap_slave = tmp_slave;
1644 break;
1645 }
1646 }
1647 }
1648
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001649 /*
1650 * Arrange for swap_slave and new_slave to temporarily be
1651 * ignored so we can mess with their MAC addresses without
1652 * fear of interference from transmit activity.
1653 */
1654 if (swap_slave) {
1655 tlb_clear_slave(bond, swap_slave, 1);
1656 }
1657 tlb_clear_slave(bond, new_slave, 1);
1658
1659 write_unlock_bh(&bond->curr_slave_lock);
1660 read_unlock(&bond->lock);
1661
Jay Vosburghe0138a62008-01-17 16:24:58 -08001662 ASSERT_RTNL();
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1665 if (swap_slave) {
1666 /* swap mac address */
1667 alb_swap_mac_addr(bond, swap_slave, new_slave);
1668 } else {
1669 /* set the new_slave to the bond mac address */
1670 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1671 bond->alb_info.rlb_enabled);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001672 }
1673
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001674 if (swap_slave) {
1675 alb_fasten_mac_swap(bond, swap_slave, new_slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001676 read_lock(&bond->lock);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001677 } else {
Jay Vosburgh25433312008-01-17 16:24:59 -08001678 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1680 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001681
1682 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001685/*
1686 * Called with RTNL
1687 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001689 __acquires(&bond->lock)
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001690 __releases(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Wang Chen454d7c92008-11-12 23:37:49 -08001692 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct sockaddr *sa = addr;
1694 struct slave *slave, *swap_slave;
1695 int res;
1696 int i;
1697
1698 if (!is_valid_ether_addr(sa->sa_data)) {
1699 return -EADDRNOTAVAIL;
1700 }
1701
1702 res = alb_set_mac_address(bond, addr);
1703 if (res) {
1704 return res;
1705 }
1706
1707 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1708
1709 /* If there is no curr_active_slave there is nothing else to do.
1710 * Otherwise we'll need to pass the new address to it and handle
1711 * duplications.
1712 */
1713 if (!bond->curr_active_slave) {
1714 return 0;
1715 }
1716
1717 swap_slave = NULL;
1718
1719 bond_for_each_slave(bond, slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001720 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1721 bond_dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 swap_slave = slave;
1723 break;
1724 }
1725 }
1726
1727 if (swap_slave) {
1728 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001729 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 } else {
1731 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1732 bond->alb_info.rlb_enabled);
1733
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001734 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1736 if (bond->alb_info.rlb_enabled) {
1737 /* inform clients mac address has changed */
1738 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1739 }
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001740 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 return 0;
1744}
1745
1746void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1747{
1748 if (bond->alb_info.current_alb_vlan &&
1749 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1750 bond->alb_info.current_alb_vlan = NULL;
1751 }
1752
1753 if (bond->alb_info.rlb_enabled) {
1754 rlb_clear_vlan(bond, vlan_id);
1755 }
1756}
1757