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