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