blob: 3662d6e446a928ef544c5a45e12313c336fb2cd6 [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) &&
Flavio Leitner42d782a2010-06-29 08:24:39 +0000336 (client_info->ip_dst == arp->ip_src) &&
337 (compare_ether_addr_64bits(client_info->mac_dst, arp->mac_src))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /* update the clients MAC address */
339 memcpy(client_info->mac_dst, arp->mac_src, ETH_ALEN);
340 client_info->ntt = 1;
341 bond_info->rx_ntt = 1;
342 }
343
344 _unlock_rx_hashtbl(bond);
345}
346
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700347static 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 -0700348{
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800349 struct bonding *bond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct arp_pkt *arp = (struct arp_pkt *)skb->data;
351 int res = NET_RX_DROP;
352
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800353 while (bond_dev->priv_flags & IFF_802_1Q_VLAN)
354 bond_dev = vlan_dev_real_dev(bond_dev);
355
356 if (!(bond_dev->priv_flags & IFF_BONDING) ||
357 !(bond_dev->flags & IFF_MASTER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!arp) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800361 pr_debug("Packet has no ARP data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 goto out;
363 }
364
365 if (skb->len < sizeof(struct arp_pkt)) {
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800366 pr_debug("Packet is too small to be an ARP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto out;
368 }
369
370 if (arp->op_code == htons(ARPOP_REPLY)) {
371 /* update rx hash table for this ARP */
Wang Chen454d7c92008-11-12 23:37:49 -0800372 bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 rlb_update_entry_from_arp(bond, arp);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800374 pr_debug("Server received an ARP Reply from client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 res = NET_RX_SUCCESS;
378
379out:
380 dev_kfree_skb(skb);
381
382 return res;
383}
384
385/* Caller must hold bond lock for read */
386static struct slave *rlb_next_rx_slave(struct bonding *bond)
387{
388 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
389 struct slave *rx_slave, *slave, *start_at;
390 int i = 0;
391
392 if (bond_info->next_rx_slave) {
393 start_at = bond_info->next_rx_slave;
394 } else {
395 start_at = bond->first_slave;
396 }
397
398 rx_slave = NULL;
399
400 bond_for_each_slave_from(bond, slave, i, start_at) {
401 if (SLAVE_IS_OK(slave)) {
402 if (!rx_slave) {
403 rx_slave = slave;
404 } else if (slave->speed > rx_slave->speed) {
405 rx_slave = slave;
406 }
407 }
408 }
409
410 if (rx_slave) {
411 bond_info->next_rx_slave = rx_slave->next;
412 }
413
414 return rx_slave;
415}
416
417/* teach the switch the mac of a disabled slave
418 * on the primary for fault tolerance
419 *
420 * Caller must hold bond->curr_slave_lock for write or bond lock for write
421 */
422static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, u8 addr[])
423{
424 if (!bond->curr_active_slave) {
425 return;
426 }
427
428 if (!bond->alb_info.primary_is_promisc) {
Wang Chen7e1a1ac2008-07-14 20:51:36 -0700429 if (!dev_set_promiscuity(bond->curr_active_slave->dev, 1))
430 bond->alb_info.primary_is_promisc = 1;
431 else
432 bond->alb_info.primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 bond->alb_info.rlb_promisc_timeout_counter = 0;
436
437 alb_send_learning_packets(bond->curr_active_slave, addr);
438}
439
440/* slave being removed should not be active at this point
441 *
442 * Caller must hold bond lock for read
443 */
444static void rlb_clear_slave(struct bonding *bond, struct slave *slave)
445{
446 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
447 struct rlb_client_info *rx_hash_table;
448 u32 index, next_index;
449
450 /* clear slave from rx_hashtbl */
451 _lock_rx_hashtbl(bond);
452
453 rx_hash_table = bond_info->rx_hashtbl;
454 index = bond_info->rx_hashtbl_head;
455 for (; index != RLB_NULL_INDEX; index = next_index) {
456 next_index = rx_hash_table[index].next;
457 if (rx_hash_table[index].slave == slave) {
458 struct slave *assigned_slave = rlb_next_rx_slave(bond);
459
460 if (assigned_slave) {
461 rx_hash_table[index].slave = assigned_slave;
Eric Dumazet885a1362009-09-01 06:31:18 +0000462 if (compare_ether_addr_64bits(rx_hash_table[index].mac_dst,
463 mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 bond_info->rx_hashtbl[index].ntt = 1;
465 bond_info->rx_ntt = 1;
466 /* A slave has been removed from the
467 * table because it is either disabled
468 * or being released. We must retry the
469 * update to avoid clients from not
470 * being updated & disconnecting when
471 * there is stress
472 */
473 bond_info->rlb_update_retry_counter =
474 RLB_UPDATE_RETRY;
475 }
476 } else { /* there is no active slave */
477 rx_hash_table[index].slave = NULL;
478 }
479 }
480 }
481
482 _unlock_rx_hashtbl(bond);
483
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700484 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (slave != bond->curr_active_slave) {
487 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr);
488 }
489
Jay Vosburgh6603a6f2007-10-17 17:37:50 -0700490 write_unlock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static void rlb_update_client(struct rlb_client_info *client_info)
494{
495 int i;
496
497 if (!client_info->slave) {
498 return;
499 }
500
501 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) {
502 struct sk_buff *skb;
503
504 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
505 client_info->ip_dst,
506 client_info->slave->dev,
507 client_info->ip_src,
508 client_info->mac_dst,
509 client_info->slave->dev->dev_addr,
510 client_info->mac_dst);
511 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800512 pr_err("%s: Error: failed to create an ARP packet\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800513 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 continue;
515 }
516
517 skb->dev = client_info->slave->dev;
518
519 if (client_info->tag) {
520 skb = vlan_put_tag(skb, client_info->vlan_id);
521 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800522 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800523 client_info->slave->dev->master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 continue;
525 }
526 }
527
528 arp_xmit(skb);
529 }
530}
531
532/* sends ARP REPLIES that update the clients that need updating */
533static void rlb_update_rx_clients(struct bonding *bond)
534{
535 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
536 struct rlb_client_info *client_info;
537 u32 hash_index;
538
539 _lock_rx_hashtbl(bond);
540
541 hash_index = bond_info->rx_hashtbl_head;
542 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
543 client_info = &(bond_info->rx_hashtbl[hash_index]);
544 if (client_info->ntt) {
545 rlb_update_client(client_info);
546 if (bond_info->rlb_update_retry_counter == 0) {
547 client_info->ntt = 0;
548 }
549 }
550 }
551
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +0200552 /* do not update the entries again until this counter is zero so that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 * not to confuse the clients.
554 */
555 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY;
556
557 _unlock_rx_hashtbl(bond);
558}
559
560/* The slave was assigned a new mac address - update the clients */
561static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave)
562{
563 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
564 struct rlb_client_info *client_info;
565 int ntt = 0;
566 u32 hash_index;
567
568 _lock_rx_hashtbl(bond);
569
570 hash_index = bond_info->rx_hashtbl_head;
571 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
572 client_info = &(bond_info->rx_hashtbl[hash_index]);
573
574 if ((client_info->slave == slave) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000575 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 client_info->ntt = 1;
577 ntt = 1;
578 }
579 }
580
581 // update the team's flag only after the whole iteration
582 if (ntt) {
583 bond_info->rx_ntt = 1;
584 //fasten the change
585 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY;
586 }
587
588 _unlock_rx_hashtbl(bond);
589}
590
591/* mark all clients using src_ip to be updated */
Al Virod3bb52b2007-08-22 20:06:58 -0400592static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
595 struct rlb_client_info *client_info;
596 u32 hash_index;
597
598 _lock_rx_hashtbl(bond);
599
600 hash_index = bond_info->rx_hashtbl_head;
601 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
602 client_info = &(bond_info->rx_hashtbl[hash_index]);
603
604 if (!client_info->slave) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800605 pr_err("%s: Error: found a client with no channel in the client's hash table\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800606 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 continue;
608 }
609 /*update all clients using this src_ip, that are not assigned
610 * to the team's address (curr_active_slave) and have a known
611 * unicast mac address.
612 */
613 if ((client_info->ip_src == src_ip) &&
Eric Dumazet885a1362009-09-01 06:31:18 +0000614 compare_ether_addr_64bits(client_info->slave->dev->dev_addr,
615 bond->dev->dev_addr) &&
616 compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 client_info->ntt = 1;
618 bond_info->rx_ntt = 1;
619 }
620 }
621
622 _unlock_rx_hashtbl(bond);
623}
624
625/* Caller must hold both bond and ptr locks for read */
626static struct slave *rlb_choose_channel(struct sk_buff *skb, struct bonding *bond)
627{
628 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300629 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct slave *assigned_slave;
631 struct rlb_client_info *client_info;
632 u32 hash_index = 0;
633
634 _lock_rx_hashtbl(bond);
635
636 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_src));
637 client_info = &(bond_info->rx_hashtbl[hash_index]);
638
639 if (client_info->assigned) {
640 if ((client_info->ip_src == arp->ip_src) &&
641 (client_info->ip_dst == arp->ip_dst)) {
642 /* the entry is already assigned to this client */
Eric Dumazet885a1362009-09-01 06:31:18 +0000643 if (compare_ether_addr_64bits(arp->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* update mac address from arp */
645 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
646 }
647
648 assigned_slave = client_info->slave;
649 if (assigned_slave) {
650 _unlock_rx_hashtbl(bond);
651 return assigned_slave;
652 }
653 } else {
654 /* the entry is already assigned to some other client,
655 * move the old client to primary (curr_active_slave) so
656 * that the new client can be assigned to this entry.
657 */
658 if (bond->curr_active_slave &&
659 client_info->slave != bond->curr_active_slave) {
660 client_info->slave = bond->curr_active_slave;
661 rlb_update_client(client_info);
662 }
663 }
664 }
665 /* assign a new slave */
666 assigned_slave = rlb_next_rx_slave(bond);
667
668 if (assigned_slave) {
669 client_info->ip_src = arp->ip_src;
670 client_info->ip_dst = arp->ip_dst;
671 /* arp->mac_dst is broadcast for arp reqeusts.
672 * will be updated with clients actual unicast mac address
673 * upon receiving an arp reply.
674 */
675 memcpy(client_info->mac_dst, arp->mac_dst, ETH_ALEN);
676 client_info->slave = assigned_slave;
677
Eric Dumazet885a1362009-09-01 06:31:18 +0000678 if (compare_ether_addr_64bits(client_info->mac_dst, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 client_info->ntt = 1;
680 bond->alb_info.rx_ntt = 1;
681 } else {
682 client_info->ntt = 0;
683 }
684
685 if (!list_empty(&bond->vlan_list)) {
Jay Vosburgh966bc6f2008-03-21 22:29:34 -0700686 if (!vlan_get_tag(skb, &client_info->vlan_id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 client_info->tag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 if (!client_info->assigned) {
691 u32 prev_tbl_head = bond_info->rx_hashtbl_head;
692 bond_info->rx_hashtbl_head = hash_index;
693 client_info->next = prev_tbl_head;
694 if (prev_tbl_head != RLB_NULL_INDEX) {
695 bond_info->rx_hashtbl[prev_tbl_head].prev =
696 hash_index;
697 }
698 client_info->assigned = 1;
699 }
700 }
701
702 _unlock_rx_hashtbl(bond);
703
704 return assigned_slave;
705}
706
707/* chooses (and returns) transmit channel for arp reply
708 * does not choose channel for other arp types since they are
709 * sent on the curr_active_slave
710 */
711static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond)
712{
Arnaldo Carvalho de Meloa16aeb32007-03-10 16:07:19 -0300713 struct arp_pkt *arp = arp_pkt(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct slave *tx_slave = NULL;
715
Brian Haleyf14c4e42008-09-02 10:08:08 -0400716 if (arp->op_code == htons(ARPOP_REPLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* the arp must be sent on the selected
718 * rx channel
719 */
720 tx_slave = rlb_choose_channel(skb, bond);
721 if (tx_slave) {
722 memcpy(arp->mac_src,tx_slave->dev->dev_addr, ETH_ALEN);
723 }
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800724 pr_debug("Server sent ARP Reply packet\n");
Brian Haleyf14c4e42008-09-02 10:08:08 -0400725 } else if (arp->op_code == htons(ARPOP_REQUEST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Create an entry in the rx_hashtbl for this client as a
727 * place holder.
728 * When the arp reply is received the entry will be updated
729 * with the correct unicast address of the client.
730 */
731 rlb_choose_channel(skb, bond);
732
733 /* The ARP relpy packets must be delayed so that
734 * they can cancel out the influence of the ARP request.
735 */
736 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY;
737
738 /* arp requests are broadcast and are sent on the primary
739 * the arp request will collapse all clients on the subnet to
740 * the primary slave. We must register these clients to be
741 * updated with their assigned mac.
742 */
743 rlb_req_update_subnet_clients(bond, arp->ip_src);
Holger Eitzenberger5a03cdb2008-12-09 23:09:22 -0800744 pr_debug("Server sent ARP Request packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
747 return tx_slave;
748}
749
750/* Caller must hold bond lock for read */
751static void rlb_rebalance(struct bonding *bond)
752{
753 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
754 struct slave *assigned_slave;
755 struct rlb_client_info *client_info;
756 int ntt;
757 u32 hash_index;
758
759 _lock_rx_hashtbl(bond);
760
761 ntt = 0;
762 hash_index = bond_info->rx_hashtbl_head;
763 for (; hash_index != RLB_NULL_INDEX; hash_index = client_info->next) {
764 client_info = &(bond_info->rx_hashtbl[hash_index]);
765 assigned_slave = rlb_next_rx_slave(bond);
766 if (assigned_slave && (client_info->slave != assigned_slave)) {
767 client_info->slave = assigned_slave;
768 client_info->ntt = 1;
769 ntt = 1;
770 }
771 }
772
773 /* update the team's flag only after the whole iteration */
774 if (ntt) {
775 bond_info->rx_ntt = 1;
776 }
777 _unlock_rx_hashtbl(bond);
778}
779
780/* Caller must hold rx_hashtbl lock */
781static void rlb_init_table_entry(struct rlb_client_info *entry)
782{
783 memset(entry, 0, sizeof(struct rlb_client_info));
784 entry->next = RLB_NULL_INDEX;
785 entry->prev = RLB_NULL_INDEX;
786}
787
788static int rlb_initialize(struct bonding *bond)
789{
790 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
791 struct packet_type *pk_type = &(BOND_ALB_INFO(bond).rlb_pkt_type);
Mitch Williams0d206a32005-11-09 10:35:30 -0800792 struct rlb_client_info *new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info);
794 int i;
795
796 spin_lock_init(&(bond_info->rx_hashtbl_lock));
797
Mitch Williams0d206a32005-11-09 10:35:30 -0800798 new_hashtbl = kmalloc(size, GFP_KERNEL);
799 if (!new_hashtbl) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800800 pr_err("%s: Error: Failed to allocate RLB hash table\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -1;
803 }
Mitch Williams0d206a32005-11-09 10:35:30 -0800804 _lock_rx_hashtbl(bond);
805
806 bond_info->rx_hashtbl = new_hashtbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
809
810 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) {
811 rlb_init_table_entry(bond_info->rx_hashtbl + i);
812 }
813
814 _unlock_rx_hashtbl(bond);
815
816 /*initialize packet type*/
Harvey Harrison09640e62009-02-01 00:45:17 -0800817 pk_type->type = cpu_to_be16(ETH_P_ARP);
Jay Vosburgh6146b1a2008-11-04 17:51:15 -0800818 pk_type->dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 pk_type->func = rlb_arp_recv;
820
821 /* register to receive ARPs */
822 dev_add_pack(pk_type);
823
824 return 0;
825}
826
827static void rlb_deinitialize(struct bonding *bond)
828{
829 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
830
831 dev_remove_pack(&(bond_info->rlb_pkt_type));
832
833 _lock_rx_hashtbl(bond);
834
835 kfree(bond_info->rx_hashtbl);
836 bond_info->rx_hashtbl = NULL;
837 bond_info->rx_hashtbl_head = RLB_NULL_INDEX;
838
839 _unlock_rx_hashtbl(bond);
840}
841
842static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
843{
844 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
845 u32 curr_index;
846
847 _lock_rx_hashtbl(bond);
848
849 curr_index = bond_info->rx_hashtbl_head;
850 while (curr_index != RLB_NULL_INDEX) {
851 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]);
852 u32 next_index = bond_info->rx_hashtbl[curr_index].next;
853 u32 prev_index = bond_info->rx_hashtbl[curr_index].prev;
854
855 if (curr->tag && (curr->vlan_id == vlan_id)) {
856 if (curr_index == bond_info->rx_hashtbl_head) {
857 bond_info->rx_hashtbl_head = next_index;
858 }
859 if (prev_index != RLB_NULL_INDEX) {
860 bond_info->rx_hashtbl[prev_index].next = next_index;
861 }
862 if (next_index != RLB_NULL_INDEX) {
863 bond_info->rx_hashtbl[next_index].prev = prev_index;
864 }
865
866 rlb_init_table_entry(curr);
867 }
868
869 curr_index = next_index;
870 }
871
872 _unlock_rx_hashtbl(bond);
873}
874
875/*********************** tlb/rlb shared functions *********************/
876
877static void alb_send_learning_packets(struct slave *slave, u8 mac_addr[])
878{
879 struct bonding *bond = bond_get_bond_by_slave(slave);
880 struct learning_pkt pkt;
881 int size = sizeof(struct learning_pkt);
882 int i;
883
884 memset(&pkt, 0, size);
885 memcpy(pkt.mac_dst, mac_addr, ETH_ALEN);
886 memcpy(pkt.mac_src, mac_addr, ETH_ALEN);
Harvey Harrison09640e62009-02-01 00:45:17 -0800887 pkt.type = cpu_to_be16(ETH_P_LOOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 for (i = 0; i < MAX_LP_BURST; i++) {
890 struct sk_buff *skb;
891 char *data;
892
893 skb = dev_alloc_skb(size);
894 if (!skb) {
895 return;
896 }
897
898 data = skb_put(skb, size);
899 memcpy(data, &pkt, size);
900
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700901 skb_reset_mac_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700902 skb->network_header = skb->mac_header + ETH_HLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 skb->protocol = pkt.type;
904 skb->priority = TC_PRIO_CONTROL;
905 skb->dev = slave->dev;
906
907 if (!list_empty(&bond->vlan_list)) {
908 struct vlan_entry *vlan;
909
910 vlan = bond_next_vlan(bond,
911 bond->alb_info.current_alb_vlan);
912
913 bond->alb_info.current_alb_vlan = vlan;
914 if (!vlan) {
915 kfree_skb(skb);
916 continue;
917 }
918
919 skb = vlan_put_tag(skb, vlan->vlan_id);
920 if (!skb) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800921 pr_err("%s: Error: failed to insert VLAN tag\n",
Mitch Williams4e0952c2005-11-09 10:34:57 -0800922 bond->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 continue;
924 }
925 }
926
927 dev_queue_xmit(skb);
928 }
929}
930
931/* hw is a boolean parameter that determines whether we should try and
932 * set the hw address of the device as well as the hw address of the
933 * net_device
934 */
935static int alb_set_slave_mac_addr(struct slave *slave, u8 addr[], int hw)
936{
937 struct net_device *dev = slave->dev;
938 struct sockaddr s_addr;
939
940 if (!hw) {
941 memcpy(dev->dev_addr, addr, dev->addr_len);
942 return 0;
943 }
944
945 /* for rlb each slave must have a unique hw mac addresses so that */
946 /* each slave will receive packets destined to a different mac */
947 memcpy(s_addr.sa_data, addr, dev->addr_len);
948 s_addr.sa_family = dev->type;
949 if (dev_set_mac_address(dev, &s_addr)) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -0800950 pr_err("%s: Error: dev_set_mac_address of dev %s failed!\n"
951 "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 -0800952 dev->master->name, dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return -EOPNOTSUPP;
954 }
955 return 0;
956}
957
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700958/*
959 * Swap MAC addresses between two slaves.
960 *
961 * Called with RTNL held, and no other locks.
962 *
963 */
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965static void alb_swap_mac_addr(struct bonding *bond, struct slave *slave1, struct slave *slave2)
966{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 u8 tmp_mac_addr[ETH_ALEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 memcpy(tmp_mac_addr, slave1->dev->dev_addr, ETH_ALEN);
970 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, bond->alb_info.rlb_enabled);
971 alb_set_slave_mac_addr(slave2, tmp_mac_addr, bond->alb_info.rlb_enabled);
972
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700973}
974
975/*
976 * Send learning packets after MAC address swap.
977 *
Jay Vosburgh25433312008-01-17 16:24:59 -0800978 * Called with RTNL and no other locks
Jay Vosburgh059fe7a2007-10-17 17:37:49 -0700979 */
980static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1,
981 struct slave *slave2)
982{
983 int slaves_state_differ = (SLAVE_IS_OK(slave1) != SLAVE_IS_OK(slave2));
984 struct slave *disabled_slave = NULL;
985
Jay Vosburgh25433312008-01-17 16:24:59 -0800986 ASSERT_RTNL();
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* fasten the change in the switch */
989 if (SLAVE_IS_OK(slave1)) {
990 alb_send_learning_packets(slave1, slave1->dev->dev_addr);
991 if (bond->alb_info.rlb_enabled) {
992 /* inform the clients that the mac address
993 * has changed
994 */
995 rlb_req_update_slave_clients(bond, slave1);
996 }
997 } else {
998 disabled_slave = slave1;
999 }
1000
1001 if (SLAVE_IS_OK(slave2)) {
1002 alb_send_learning_packets(slave2, slave2->dev->dev_addr);
1003 if (bond->alb_info.rlb_enabled) {
1004 /* inform the clients that the mac address
1005 * has changed
1006 */
1007 rlb_req_update_slave_clients(bond, slave2);
1008 }
1009 } else {
1010 disabled_slave = slave2;
1011 }
1012
1013 if (bond->alb_info.rlb_enabled && slaves_state_differ) {
1014 /* A disabled slave was assigned an active mac addr */
1015 rlb_teach_disabled_mac_on_primary(bond,
1016 disabled_slave->dev->dev_addr);
1017 }
1018}
1019
1020/**
1021 * alb_change_hw_addr_on_detach
1022 * @bond: bonding we're working on
1023 * @slave: the slave that was just detached
1024 *
1025 * We assume that @slave was already detached from the slave list.
1026 *
1027 * If @slave's permanent hw address is different both from its current
1028 * address and from @bond's address, then somewhere in the bond there's
1029 * a slave that has @slave's permanet address as its current address.
1030 * We'll make sure that that slave no longer uses @slave's permanent address.
1031 *
Jay Vosburgh25433312008-01-17 16:24:59 -08001032 * Caller must hold RTNL and no other locks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
1034static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave)
1035{
1036 int perm_curr_diff;
1037 int perm_bond_diff;
1038
Eric Dumazet885a1362009-09-01 06:31:18 +00001039 perm_curr_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1040 slave->dev->dev_addr);
1041 perm_bond_diff = compare_ether_addr_64bits(slave->perm_hwaddr,
1042 bond->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (perm_curr_diff && perm_bond_diff) {
1045 struct slave *tmp_slave;
1046 int i, found = 0;
1047
1048 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001049 if (!compare_ether_addr_64bits(slave->perm_hwaddr,
1050 tmp_slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 found = 1;
1052 break;
1053 }
1054 }
1055
1056 if (found) {
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001057 /* locking: needs RTNL and nothing else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 alb_swap_mac_addr(bond, slave, tmp_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001059 alb_fasten_mac_swap(bond, slave, tmp_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
1061 }
1062}
1063
1064/**
1065 * alb_handle_addr_collision_on_attach
1066 * @bond: bonding we're working on
1067 * @slave: the slave that was just attached
1068 *
1069 * checks uniqueness of slave's mac address and handles the case the
1070 * new slave uses the bonds mac address.
1071 *
1072 * If the permanent hw address of @slave is @bond's hw address, we need to
1073 * find a different hw address to give @slave, that isn't in use by any other
1074 * slave in the bond. This address must be, of course, one of the premanent
1075 * addresses of the other slaves.
1076 *
1077 * We go over the slave list, and for each slave there we compare its
1078 * permanent hw address with the current address of all the other slaves.
1079 * If no match was found, then we've found a slave with a permanent address
1080 * that isn't used by any other slave in the bond, so we can assign it to
1081 * @slave.
1082 *
1083 * assumption: this function is called before @slave is attached to the
1084 * bond slave list.
1085 *
1086 * caller must hold the bond lock for write since the mac addresses are compared
1087 * and may be swapped.
1088 */
1089static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave)
1090{
1091 struct slave *tmp_slave1, *tmp_slave2, *free_mac_slave;
1092 struct slave *has_bond_addr = bond->curr_active_slave;
1093 int i, j, found = 0;
1094
1095 if (bond->slave_cnt == 0) {
1096 /* this is the first slave */
1097 return 0;
1098 }
1099
1100 /* if slave's mac address differs from bond's mac address
1101 * check uniqueness of slave's mac address against the other
1102 * slaves in the bond.
1103 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001104 if (compare_ether_addr_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 bond_for_each_slave(bond, tmp_slave1, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001106 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1107 slave->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 found = 1;
1109 break;
1110 }
1111 }
1112
John W. Linville6b38aef2005-07-28 15:00:15 -04001113 if (!found)
1114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
John W. Linville6b38aef2005-07-28 15:00:15 -04001116 /* Try setting slave mac to bond address and fall-through
1117 to code handling that situation below... */
1118 alb_set_slave_mac_addr(slave, bond->dev->dev_addr,
1119 bond->alb_info.rlb_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
1121
1122 /* The slave's address is equal to the address of the bond.
1123 * Search for a spare address in the bond for this slave.
1124 */
1125 free_mac_slave = NULL;
1126
1127 bond_for_each_slave(bond, tmp_slave1, i) {
1128 found = 0;
1129 bond_for_each_slave(bond, tmp_slave2, j) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001130 if (!compare_ether_addr_64bits(tmp_slave1->perm_hwaddr,
1131 tmp_slave2->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 found = 1;
1133 break;
1134 }
1135 }
1136
1137 if (!found) {
1138 /* no slave has tmp_slave1's perm addr
1139 * as its curr addr
1140 */
1141 free_mac_slave = tmp_slave1;
1142 break;
1143 }
1144
1145 if (!has_bond_addr) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001146 if (!compare_ether_addr_64bits(tmp_slave1->dev->dev_addr,
1147 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 has_bond_addr = tmp_slave1;
1150 }
1151 }
1152 }
1153
1154 if (free_mac_slave) {
1155 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr,
1156 bond->alb_info.rlb_enabled);
1157
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001158 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 +00001159 bond->dev->name, slave->dev->name,
1160 free_mac_slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 } else if (has_bond_addr) {
Joe Perchesa4aee5c2009-12-13 20:06:07 -08001163 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 -08001164 bond->dev->name, slave->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return -EFAULT;
1166 }
1167
1168 return 0;
1169}
1170
1171/**
1172 * alb_set_mac_address
1173 * @bond:
1174 * @addr:
1175 *
1176 * In TLB mode all slaves are configured to the bond's hw address, but set
1177 * their dev_addr field to different addresses (based on their permanent hw
1178 * addresses).
1179 *
1180 * For each slave, this function sets the interface to the new address and then
1181 * changes its dev_addr field to its previous value.
1182 *
1183 * Unwinding assumes bond's mac address has not yet changed.
1184 */
1185static int alb_set_mac_address(struct bonding *bond, void *addr)
1186{
1187 struct sockaddr sa;
1188 struct slave *slave, *stop_at;
1189 char tmp_addr[ETH_ALEN];
1190 int res;
1191 int i;
1192
1193 if (bond->alb_info.rlb_enabled) {
1194 return 0;
1195 }
1196
1197 bond_for_each_slave(bond, slave, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* save net_device's current hw address */
1199 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1200
1201 res = dev_set_mac_address(slave->dev, addr);
1202
1203 /* restore net_device's hw address */
1204 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1205
Stephen Hemmingereb7cc592008-11-19 21:56:05 -08001206 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 goto unwind;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
1210 return 0;
1211
1212unwind:
1213 memcpy(sa.sa_data, bond->dev->dev_addr, bond->dev->addr_len);
1214 sa.sa_family = bond->dev->type;
1215
1216 /* unwind from head to the slave that failed */
1217 stop_at = slave;
1218 bond_for_each_slave_from_to(bond, slave, i, bond->first_slave, stop_at) {
1219 memcpy(tmp_addr, slave->dev->dev_addr, ETH_ALEN);
1220 dev_set_mac_address(slave->dev, &sa);
1221 memcpy(slave->dev->dev_addr, tmp_addr, ETH_ALEN);
1222 }
1223
1224 return res;
1225}
1226
1227/************************ exported alb funcions ************************/
1228
1229int bond_alb_initialize(struct bonding *bond, int rlb_enabled)
1230{
1231 int res;
1232
1233 res = tlb_initialize(bond);
1234 if (res) {
1235 return res;
1236 }
1237
1238 if (rlb_enabled) {
1239 bond->alb_info.rlb_enabled = 1;
1240 /* initialize rlb */
1241 res = rlb_initialize(bond);
1242 if (res) {
1243 tlb_deinitialize(bond);
1244 return res;
1245 }
Mitch Williamsb76850a2005-11-09 10:35:35 -08001246 } else {
1247 bond->alb_info.rlb_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
1250 return 0;
1251}
1252
1253void bond_alb_deinitialize(struct bonding *bond)
1254{
1255 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1256
1257 tlb_deinitialize(bond);
1258
1259 if (bond_info->rlb_enabled) {
1260 rlb_deinitialize(bond);
1261 }
1262}
1263
1264int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
1265{
Wang Chen454d7c92008-11-12 23:37:49 -08001266 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct ethhdr *eth_data;
1268 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1269 struct slave *tx_slave = NULL;
Al Virod3bb52b2007-08-22 20:06:58 -04001270 static const __be32 ip_bcast = htonl(0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int hash_size = 0;
1272 int do_tx_balance = 1;
1273 u32 hash_index = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001274 const u8 *hash_start = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int res = 1;
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001276 struct ipv6hdr *ip6hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001278 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 eth_data = eth_hdr(skb);
1280
1281 /* make sure that the curr_active_slave and the slaves list do
1282 * not change during tx
1283 */
1284 read_lock(&bond->lock);
1285 read_lock(&bond->curr_slave_lock);
1286
1287 if (!BOND_IS_OK(bond)) {
1288 goto out;
1289 }
1290
1291 switch (ntohs(skb->protocol)) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001292 case ETH_P_IP: {
1293 const struct iphdr *iph = ip_hdr(skb);
1294
Eric Dumazet885a1362009-09-01 06:31:18 +00001295 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast) ||
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001296 (iph->daddr == ip_bcast) ||
1297 (iph->protocol == IPPROTO_IGMP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 do_tx_balance = 0;
1299 break;
1300 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001301 hash_start = (char *)&(iph->daddr);
1302 hash_size = sizeof(iph->daddr);
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
1305 case ETH_P_IPV6:
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001306 /* IPv6 doesn't really use broadcast mac address, but leave
1307 * that here just in case.
1308 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001309 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_bcast)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 do_tx_balance = 0;
1311 break;
1312 }
1313
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001314 /* IPv6 uses all-nodes multicast as an equivalent to
1315 * broadcasts in IPv4.
1316 */
Eric Dumazet885a1362009-09-01 06:31:18 +00001317 if (!compare_ether_addr_64bits(eth_data->h_dest, mac_v6_allmcast)) {
Vlad Yasevich2d1ea192008-08-28 15:38:41 -04001318 do_tx_balance = 0;
1319 break;
1320 }
1321
1322 /* Additianally, DAD probes should not be tx-balanced as that
1323 * will lead to false positives for duplicate addresses and
1324 * prevent address configuration from working.
1325 */
1326 ip6hdr = ipv6_hdr(skb);
1327 if (ipv6_addr_any(&ip6hdr->saddr)) {
1328 do_tx_balance = 0;
1329 break;
1330 }
1331
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001332 hash_start = (char *)&(ipv6_hdr(skb)->daddr);
1333 hash_size = sizeof(ipv6_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
1335 case ETH_P_IPX:
Al Virod3bb52b2007-08-22 20:06:58 -04001336 if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /* something is wrong with this packet */
1338 do_tx_balance = 0;
1339 break;
1340 }
1341
1342 if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
1343 /* The only protocol worth balancing in
1344 * this family since it has an "ARP" like
1345 * mechanism
1346 */
1347 do_tx_balance = 0;
1348 break;
1349 }
1350
1351 hash_start = (char*)eth_data->h_dest;
1352 hash_size = ETH_ALEN;
1353 break;
1354 case ETH_P_ARP:
1355 do_tx_balance = 0;
1356 if (bond_info->rlb_enabled) {
1357 tx_slave = rlb_arp_xmit(skb, bond);
1358 }
1359 break;
1360 default:
1361 do_tx_balance = 0;
1362 break;
1363 }
1364
1365 if (do_tx_balance) {
1366 hash_index = _simple_hash(hash_start, hash_size);
1367 tx_slave = tlb_choose_channel(bond, hash_index, skb->len);
1368 }
1369
1370 if (!tx_slave) {
1371 /* unbalanced or unassigned, send through primary */
1372 tx_slave = bond->curr_active_slave;
1373 bond_info->unbalanced_load += skb->len;
1374 }
1375
1376 if (tx_slave && SLAVE_IS_OK(tx_slave)) {
1377 if (tx_slave != bond->curr_active_slave) {
1378 memcpy(eth_data->h_source,
1379 tx_slave->dev->dev_addr,
1380 ETH_ALEN);
1381 }
1382
1383 res = bond_dev_queue_xmit(bond, skb, tx_slave->dev);
1384 } else {
1385 if (tx_slave) {
1386 tlb_clear_slave(bond, tx_slave, 0);
1387 }
1388 }
1389
1390out:
1391 if (res) {
1392 /* no suitable interface, frame not sent */
1393 dev_kfree_skb(skb);
1394 }
1395 read_unlock(&bond->curr_slave_lock);
1396 read_unlock(&bond->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -07001397 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001400void bond_alb_monitor(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001402 struct bonding *bond = container_of(work, struct bonding,
1403 alb_work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1405 struct slave *slave;
1406 int i;
1407
1408 read_lock(&bond->lock);
1409
1410 if (bond->kill_timers) {
1411 goto out;
1412 }
1413
1414 if (bond->slave_cnt == 0) {
1415 bond_info->tx_rebalance_counter = 0;
1416 bond_info->lp_counter = 0;
1417 goto re_arm;
1418 }
1419
1420 bond_info->tx_rebalance_counter++;
1421 bond_info->lp_counter++;
1422
1423 /* send learning packets */
1424 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS) {
1425 /* change of curr_active_slave involves swapping of mac addresses.
1426 * in order to avoid this swapping from happening while
1427 * sending the learning packets, the curr_slave_lock must be held for
1428 * read.
1429 */
1430 read_lock(&bond->curr_slave_lock);
1431
1432 bond_for_each_slave(bond, slave, i) {
Mitch Williamse944ef72005-11-09 10:36:50 -08001433 alb_send_learning_packets(slave, slave->dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
1436 read_unlock(&bond->curr_slave_lock);
1437
1438 bond_info->lp_counter = 0;
1439 }
1440
1441 /* rebalance tx traffic */
1442 if (bond_info->tx_rebalance_counter >= BOND_TLB_REBALANCE_TICKS) {
1443
1444 read_lock(&bond->curr_slave_lock);
1445
1446 bond_for_each_slave(bond, slave, i) {
1447 tlb_clear_slave(bond, slave, 1);
1448 if (slave == bond->curr_active_slave) {
1449 SLAVE_TLB_INFO(slave).load =
1450 bond_info->unbalanced_load /
1451 BOND_TLB_REBALANCE_INTERVAL;
1452 bond_info->unbalanced_load = 0;
1453 }
1454 }
1455
1456 read_unlock(&bond->curr_slave_lock);
1457
1458 bond_info->tx_rebalance_counter = 0;
1459 }
1460
1461 /* handle rlb stuff */
1462 if (bond_info->rlb_enabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (bond_info->primary_is_promisc &&
1464 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) {
1465
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001466 /*
1467 * dev_set_promiscuity requires rtnl and
1468 * nothing else.
1469 */
1470 read_unlock(&bond->lock);
1471 rtnl_lock();
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 bond_info->rlb_promisc_timeout_counter = 0;
1474
1475 /* If the primary was set to promiscuous mode
1476 * because a slave was disabled then
1477 * it can now leave promiscuous mode.
1478 */
1479 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1480 bond_info->primary_is_promisc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Jay Vosburghd0e81b72007-10-17 17:37:51 -07001482 rtnl_unlock();
1483 read_lock(&bond->lock);
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (bond_info->rlb_rebalance) {
1487 bond_info->rlb_rebalance = 0;
1488 rlb_rebalance(bond);
1489 }
1490
1491 /* check if clients need updating */
1492 if (bond_info->rx_ntt) {
1493 if (bond_info->rlb_update_delay_counter) {
1494 --bond_info->rlb_update_delay_counter;
1495 } else {
1496 rlb_update_rx_clients(bond);
1497 if (bond_info->rlb_update_retry_counter) {
1498 --bond_info->rlb_update_retry_counter;
1499 } else {
1500 bond_info->rx_ntt = 0;
1501 }
1502 }
1503 }
1504 }
1505
1506re_arm:
Jay Vosburgh1b76b312007-10-17 17:37:45 -07001507 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508out:
1509 read_unlock(&bond->lock);
1510}
1511
1512/* assumption: called before the slave is attached to the bond
1513 * and not locked by the bond lock
1514 */
1515int bond_alb_init_slave(struct bonding *bond, struct slave *slave)
1516{
1517 int res;
1518
1519 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr,
1520 bond->alb_info.rlb_enabled);
1521 if (res) {
1522 return res;
1523 }
1524
1525 /* caller must hold the bond lock for write since the mac addresses
1526 * are compared and may be swapped.
1527 */
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001528 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 res = alb_handle_addr_collision_on_attach(bond, slave);
1531
Jay Vosburgh6603a6f2007-10-17 17:37:50 -07001532 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (res) {
1535 return res;
1536 }
1537
1538 tlb_init_slave(slave);
1539
1540 /* order a rebalance ASAP */
1541 bond->alb_info.tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1542
1543 if (bond->alb_info.rlb_enabled) {
1544 bond->alb_info.rlb_rebalance = 1;
1545 }
1546
1547 return 0;
1548}
1549
Jay Vosburgh25433312008-01-17 16:24:59 -08001550/*
1551 * Remove slave from tlb and rlb hash tables, and fix up MAC addresses
1552 * if necessary.
1553 *
1554 * Caller must hold RTNL and no other locks
1555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave)
1557{
1558 if (bond->slave_cnt > 1) {
1559 alb_change_hw_addr_on_detach(bond, slave);
1560 }
1561
1562 tlb_clear_slave(bond, slave, 0);
1563
1564 if (bond->alb_info.rlb_enabled) {
1565 bond->alb_info.next_rx_slave = NULL;
1566 rlb_clear_slave(bond, slave);
1567 }
1568}
1569
1570/* Caller must hold bond lock for read */
1571void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link)
1572{
1573 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
1574
1575 if (link == BOND_LINK_DOWN) {
1576 tlb_clear_slave(bond, slave, 0);
1577 if (bond->alb_info.rlb_enabled) {
1578 rlb_clear_slave(bond, slave);
1579 }
1580 } else if (link == BOND_LINK_UP) {
1581 /* order a rebalance ASAP */
1582 bond_info->tx_rebalance_counter = BOND_TLB_REBALANCE_TICKS;
1583 if (bond->alb_info.rlb_enabled) {
1584 bond->alb_info.rlb_rebalance = 1;
1585 /* If the updelay module parameter is smaller than the
1586 * forwarding delay of the switch the rebalance will
1587 * not work because the rebalance arp replies will
1588 * not be forwarded to the clients..
1589 */
1590 }
1591 }
1592}
1593
1594/**
1595 * bond_alb_handle_active_change - assign new curr_active_slave
1596 * @bond: our bonding struct
1597 * @new_slave: new slave to assign
1598 *
1599 * Set the bond->curr_active_slave to @new_slave and handle
1600 * mac address swapping and promiscuity changes as needed.
1601 *
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001602 * If new_slave is NULL, caller must hold curr_slave_lock or
1603 * bond->lock for write.
1604 *
1605 * If new_slave is not NULL, caller must hold RTNL, bond->lock for
1606 * read and curr_slave_lock for write. Processing here may sleep, so
1607 * no other locks may be held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
1609void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001610 __releases(&bond->curr_slave_lock)
1611 __releases(&bond->lock)
1612 __acquires(&bond->lock)
1613 __acquires(&bond->curr_slave_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 struct slave *swap_slave;
1616 int i;
1617
1618 if (bond->curr_active_slave == new_slave) {
1619 return;
1620 }
1621
1622 if (bond->curr_active_slave && bond->alb_info.primary_is_promisc) {
1623 dev_set_promiscuity(bond->curr_active_slave->dev, -1);
1624 bond->alb_info.primary_is_promisc = 0;
1625 bond->alb_info.rlb_promisc_timeout_counter = 0;
1626 }
1627
1628 swap_slave = bond->curr_active_slave;
1629 bond->curr_active_slave = new_slave;
1630
1631 if (!new_slave || (bond->slave_cnt == 0)) {
1632 return;
1633 }
1634
1635 /* set the new curr_active_slave to the bonds mac address
1636 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave
1637 */
1638 if (!swap_slave) {
1639 struct slave *tmp_slave;
1640 /* find slave that is holding the bond's mac address */
1641 bond_for_each_slave(bond, tmp_slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001642 if (!compare_ether_addr_64bits(tmp_slave->dev->dev_addr,
1643 bond->dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 swap_slave = tmp_slave;
1645 break;
1646 }
1647 }
1648 }
1649
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001650 /*
1651 * Arrange for swap_slave and new_slave to temporarily be
1652 * ignored so we can mess with their MAC addresses without
1653 * fear of interference from transmit activity.
1654 */
1655 if (swap_slave) {
1656 tlb_clear_slave(bond, swap_slave, 1);
1657 }
1658 tlb_clear_slave(bond, new_slave, 1);
1659
1660 write_unlock_bh(&bond->curr_slave_lock);
1661 read_unlock(&bond->lock);
1662
Jay Vosburghe0138a62008-01-17 16:24:58 -08001663 ASSERT_RTNL();
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /* curr_active_slave must be set before calling alb_swap_mac_addr */
1666 if (swap_slave) {
1667 /* swap mac address */
1668 alb_swap_mac_addr(bond, swap_slave, new_slave);
1669 } else {
1670 /* set the new_slave to the bond mac address */
1671 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr,
1672 bond->alb_info.rlb_enabled);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001673 }
1674
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001675 if (swap_slave) {
1676 alb_fasten_mac_swap(bond, swap_slave, new_slave);
Jay Vosburgh25433312008-01-17 16:24:59 -08001677 read_lock(&bond->lock);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001678 } else {
Jay Vosburgh25433312008-01-17 16:24:59 -08001679 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 alb_send_learning_packets(new_slave, bond->dev->dev_addr);
1681 }
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001682
1683 write_lock_bh(&bond->curr_slave_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001686/*
1687 * Called with RTNL
1688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr)
Hannes Eder1f78d9f2009-02-14 11:15:33 +00001690 __acquires(&bond->lock)
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001691 __releases(&bond->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Wang Chen454d7c92008-11-12 23:37:49 -08001693 struct bonding *bond = netdev_priv(bond_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct sockaddr *sa = addr;
1695 struct slave *slave, *swap_slave;
1696 int res;
1697 int i;
1698
1699 if (!is_valid_ether_addr(sa->sa_data)) {
1700 return -EADDRNOTAVAIL;
1701 }
1702
1703 res = alb_set_mac_address(bond, addr);
1704 if (res) {
1705 return res;
1706 }
1707
1708 memcpy(bond_dev->dev_addr, sa->sa_data, bond_dev->addr_len);
1709
1710 /* If there is no curr_active_slave there is nothing else to do.
1711 * Otherwise we'll need to pass the new address to it and handle
1712 * duplications.
1713 */
1714 if (!bond->curr_active_slave) {
1715 return 0;
1716 }
1717
1718 swap_slave = NULL;
1719
1720 bond_for_each_slave(bond, slave, i) {
Eric Dumazet885a1362009-09-01 06:31:18 +00001721 if (!compare_ether_addr_64bits(slave->dev->dev_addr,
1722 bond_dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 swap_slave = slave;
1724 break;
1725 }
1726 }
1727
1728 if (swap_slave) {
1729 alb_swap_mac_addr(bond, swap_slave, bond->curr_active_slave);
Jay Vosburgh059fe7a2007-10-17 17:37:49 -07001730 alb_fasten_mac_swap(bond, swap_slave, bond->curr_active_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 } else {
1732 alb_set_slave_mac_addr(bond->curr_active_slave, bond_dev->dev_addr,
1733 bond->alb_info.rlb_enabled);
1734
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001735 read_lock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 alb_send_learning_packets(bond->curr_active_slave, bond_dev->dev_addr);
1737 if (bond->alb_info.rlb_enabled) {
1738 /* inform clients mac address has changed */
1739 rlb_req_update_slave_clients(bond, bond->curr_active_slave);
1740 }
Jay Vosburgh815bcc22009-05-04 09:03:37 +00001741 read_unlock(&bond->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return 0;
1745}
1746
1747void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id)
1748{
1749 if (bond->alb_info.current_alb_vlan &&
1750 (bond->alb_info.current_alb_vlan->vlan_id == vlan_id)) {
1751 bond->alb_info.current_alb_vlan = NULL;
1752 }
1753
1754 if (bond->alb_info.rlb_enabled) {
1755 rlb_clear_vlan(bond, vlan_id);
1756 }
1757}
1758