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