1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved. 4 */ 5 6 #include <linux/skbuff.h> 7 #include <linux/netdevice.h> 8 #include <linux/etherdevice.h> 9 #include <linux/ethtool.h> 10 #include <linux/pkt_sched.h> 11 #include <linux/spinlock.h> 12 #include <linux/slab.h> 13 #include <linux/timer.h> 14 #include <linux/ip.h> 15 #include <linux/ipv6.h> 16 #include <linux/if_arp.h> 17 #include <linux/if_ether.h> 18 #include <linux/if_bonding.h> 19 #include <linux/if_vlan.h> 20 #include <linux/in.h> 21 #include <net/arp.h> 22 #include <net/ipv6.h> 23 #include <net/ndisc.h> 24 #include <asm/byteorder.h> 25 #include <net/bonding.h> 26 #include <net/bond_alb.h> 27 28 static const u8 mac_v6_allmcast[ETH_ALEN + 2] __long_aligned = { 29 0x33, 0x33, 0x00, 0x00, 0x00, 0x01 30 }; 31 static const int alb_delta_in_ticks = HZ / ALB_TIMER_TICKS_PER_SEC; 32 33 #pragma pack(1) 34 struct learning_pkt { 35 u8 mac_dst[ETH_ALEN]; 36 u8 mac_src[ETH_ALEN]; 37 __be16 type; 38 u8 padding[ETH_ZLEN - ETH_HLEN]; 39 }; 40 41 struct arp_pkt { 42 __be16 hw_addr_space; 43 __be16 prot_addr_space; 44 u8 hw_addr_len; 45 u8 prot_addr_len; 46 __be16 op_code; 47 u8 mac_src[ETH_ALEN]; /* sender hardware address */ 48 __be32 ip_src; /* sender IP address */ 49 u8 mac_dst[ETH_ALEN]; /* target hardware address */ 50 __be32 ip_dst; /* target IP address */ 51 }; 52 #pragma pack() 53 54 /* Forward declaration */ 55 static void alb_send_learning_packets(struct slave *slave, const u8 mac_addr[], 56 bool strict_match); 57 static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp); 58 static void rlb_src_unlink(struct bonding *bond, u32 index); 59 static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, 60 u32 ip_dst_hash); 61 62 static inline u8 _simple_hash(const u8 *hash_start, int hash_size) 63 { 64 int i; 65 u8 hash = 0; 66 67 for (i = 0; i < hash_size; i++) 68 hash ^= hash_start[i]; 69 70 return hash; 71 } 72 73 /*********************** tlb specific functions ***************************/ 74 75 static inline void tlb_init_table_entry(struct tlb_client_info *entry, int save_load) 76 { 77 if (save_load) { 78 entry->load_history = 1 + div_u64(entry->tx_bytes, 79 BOND_TLB_REBALANCE_INTERVAL); 80 entry->tx_bytes = 0; 81 } 82 83 entry->tx_slave = NULL; 84 entry->next = TLB_NULL_INDEX; 85 entry->prev = TLB_NULL_INDEX; 86 } 87 88 static inline void tlb_init_slave(struct slave *slave) 89 { 90 SLAVE_TLB_INFO(slave).load = 0; 91 SLAVE_TLB_INFO(slave).head = TLB_NULL_INDEX; 92 } 93 94 static void __tlb_clear_slave(struct bonding *bond, struct slave *slave, 95 int save_load) 96 { 97 struct tlb_client_info *tx_hash_table; 98 u32 index; 99 100 /* clear slave from tx_hashtbl */ 101 tx_hash_table = BOND_ALB_INFO(bond).tx_hashtbl; 102 103 /* skip this if we've already freed the tx hash table */ 104 if (tx_hash_table) { 105 index = SLAVE_TLB_INFO(slave).head; 106 while (index != TLB_NULL_INDEX) { 107 u32 next_index = tx_hash_table[index].next; 108 109 tlb_init_table_entry(&tx_hash_table[index], save_load); 110 index = next_index; 111 } 112 } 113 114 tlb_init_slave(slave); 115 } 116 117 static void tlb_clear_slave(struct bonding *bond, struct slave *slave, 118 int save_load) 119 { 120 spin_lock_bh(&bond->mode_lock); 121 __tlb_clear_slave(bond, slave, save_load); 122 spin_unlock_bh(&bond->mode_lock); 123 } 124 125 /* Must be called before starting the monitor timer */ 126 static int tlb_initialize(struct bonding *bond) 127 { 128 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 129 int size = TLB_HASH_TABLE_SIZE * sizeof(struct tlb_client_info); 130 struct tlb_client_info *new_hashtbl; 131 int i; 132 133 new_hashtbl = kzalloc(size, GFP_KERNEL); 134 if (!new_hashtbl) 135 return -ENOMEM; 136 137 bond_info->unbalanced_load = netdev_alloc_pcpu_stats(struct unbalanced_load_stats); 138 if (!bond_info->unbalanced_load) 139 goto out; 140 141 spin_lock_bh(&bond->mode_lock); 142 143 bond_info->tx_hashtbl = new_hashtbl; 144 145 for (i = 0; i < TLB_HASH_TABLE_SIZE; i++) 146 tlb_init_table_entry(&bond_info->tx_hashtbl[i], 0); 147 148 spin_unlock_bh(&bond->mode_lock); 149 150 return 0; 151 152 out: 153 kfree(new_hashtbl); 154 return -ENOMEM; 155 } 156 157 /* Must be called only after all slaves have been released */ 158 static void tlb_deinitialize(struct bonding *bond) 159 { 160 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 161 162 spin_lock_bh(&bond->mode_lock); 163 164 kfree(bond_info->tx_hashtbl); 165 bond_info->tx_hashtbl = NULL; 166 free_percpu(bond_info->unbalanced_load); 167 bond_info->prev_total_unbalanced = 0; 168 169 spin_unlock_bh(&bond->mode_lock); 170 } 171 172 static long long compute_gap(struct slave *slave) 173 { 174 u32 raw_speed = READ_ONCE(slave->speed); 175 176 return ((s64)raw_speed << 20) - /* Convert to bits per sec */ 177 ((s64)SLAVE_TLB_INFO(slave).load << 3); /* Bytes to bits */ 178 } 179 180 static struct slave *tlb_get_least_loaded_slave(struct bonding *bond) 181 { 182 struct slave *slave, *least_loaded; 183 struct list_head *iter; 184 long long max_gap; 185 186 least_loaded = NULL; 187 max_gap = LLONG_MIN; 188 189 /* Find the slave with the largest gap */ 190 bond_for_each_slave_rcu(bond, slave, iter) { 191 if (bond_slave_can_tx(slave)) { 192 long long gap = compute_gap(slave); 193 194 if (max_gap < gap) { 195 least_loaded = slave; 196 max_gap = gap; 197 } 198 } 199 } 200 201 return least_loaded; 202 } 203 204 static struct slave *__tlb_choose_channel(struct bonding *bond, u32 hash_index, 205 u32 skb_len) 206 { 207 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 208 struct tlb_client_info *hash_table; 209 struct slave *assigned_slave; 210 211 hash_table = bond_info->tx_hashtbl; 212 assigned_slave = hash_table[hash_index].tx_slave; 213 if (!assigned_slave) { 214 assigned_slave = tlb_get_least_loaded_slave(bond); 215 216 if (assigned_slave) { 217 struct tlb_slave_info *slave_info = 218 &(SLAVE_TLB_INFO(assigned_slave)); 219 u32 next_index = slave_info->head; 220 221 hash_table[hash_index].tx_slave = assigned_slave; 222 hash_table[hash_index].next = next_index; 223 hash_table[hash_index].prev = TLB_NULL_INDEX; 224 225 if (next_index != TLB_NULL_INDEX) 226 hash_table[next_index].prev = hash_index; 227 228 slave_info->head = hash_index; 229 slave_info->load += 230 hash_table[hash_index].load_history; 231 } 232 } 233 234 if (assigned_slave) 235 hash_table[hash_index].tx_bytes += skb_len; 236 237 return assigned_slave; 238 } 239 240 static struct slave *tlb_choose_channel(struct bonding *bond, u32 hash_index, 241 u32 skb_len) 242 { 243 struct slave *tx_slave; 244 245 /* We don't need to disable softirq here, because 246 * tlb_choose_channel() is only called by bond_alb_xmit() 247 * which already has softirq disabled. 248 */ 249 spin_lock(&bond->mode_lock); 250 tx_slave = __tlb_choose_channel(bond, hash_index, skb_len); 251 spin_unlock(&bond->mode_lock); 252 253 return tx_slave; 254 } 255 256 /*********************** rlb specific functions ***************************/ 257 258 /* when an ARP REPLY is received from a client update its info 259 * in the rx_hashtbl 260 */ 261 static void rlb_update_entry_from_arp(struct bonding *bond, struct arp_pkt *arp) 262 { 263 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 264 struct rlb_client_info *client_info; 265 u32 hash_index; 266 267 spin_lock_bh(&bond->mode_lock); 268 269 hash_index = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src)); 270 client_info = &(bond_info->rx_hashtbl[hash_index]); 271 272 if ((client_info->assigned) && 273 (client_info->ip_src == arp->ip_dst) && 274 (client_info->ip_dst == arp->ip_src) && 275 (!ether_addr_equal_64bits(client_info->mac_dst, arp->mac_src))) { 276 /* update the clients MAC address */ 277 ether_addr_copy(client_info->mac_dst, arp->mac_src); 278 client_info->ntt = 1; 279 bond_info->rx_ntt = 1; 280 } 281 282 spin_unlock_bh(&bond->mode_lock); 283 } 284 285 static int rlb_arp_recv(const struct sk_buff *skb, struct bonding *bond, 286 struct slave *slave) 287 { 288 struct arp_pkt *arp, _arp; 289 290 if (skb->protocol != cpu_to_be16(ETH_P_ARP)) 291 goto out; 292 293 arp = skb_header_pointer(skb, 0, sizeof(_arp), &_arp); 294 if (!arp) 295 goto out; 296 297 /* We received an ARP from arp->ip_src. 298 * We might have used this IP address previously (on the bonding host 299 * itself or on a system that is bridged together with the bond). 300 * However, if arp->mac_src is different than what is stored in 301 * rx_hashtbl, some other host is now using the IP and we must prevent 302 * sending out client updates with this IP address and the old MAC 303 * address. 304 * Clean up all hash table entries that have this address as ip_src but 305 * have a different mac_src. 306 */ 307 rlb_purge_src_ip(bond, arp); 308 309 if (arp->op_code == htons(ARPOP_REPLY)) { 310 /* update rx hash table for this ARP */ 311 rlb_update_entry_from_arp(bond, arp); 312 slave_dbg(bond->dev, slave->dev, "Server received an ARP Reply from client\n"); 313 } 314 out: 315 return RX_HANDLER_ANOTHER; 316 } 317 318 /* Caller must hold rcu_read_lock() */ 319 static struct slave *__rlb_next_rx_slave(struct bonding *bond) 320 { 321 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 322 struct slave *before = NULL, *rx_slave = NULL, *slave; 323 struct list_head *iter; 324 bool found = false; 325 326 bond_for_each_slave_rcu(bond, slave, iter) { 327 if (!bond_slave_can_tx(slave)) 328 continue; 329 if (!found) { 330 if (!before || before->speed < slave->speed) 331 before = slave; 332 } else { 333 if (!rx_slave || rx_slave->speed < slave->speed) 334 rx_slave = slave; 335 } 336 if (slave == bond_info->rx_slave) 337 found = true; 338 } 339 /* we didn't find anything after the current or we have something 340 * better before and up to the current slave 341 */ 342 if (!rx_slave || (before && rx_slave->speed < before->speed)) 343 rx_slave = before; 344 345 if (rx_slave) 346 bond_info->rx_slave = rx_slave; 347 348 return rx_slave; 349 } 350 351 /* Caller must hold RTNL, rcu_read_lock is obtained only to silence checkers */ 352 static struct slave *rlb_next_rx_slave(struct bonding *bond) 353 { 354 struct slave *rx_slave; 355 356 ASSERT_RTNL(); 357 358 rcu_read_lock(); 359 rx_slave = __rlb_next_rx_slave(bond); 360 rcu_read_unlock(); 361 362 return rx_slave; 363 } 364 365 /* teach the switch the mac of a disabled slave 366 * on the primary for fault tolerance 367 * 368 * Caller must hold RTNL 369 */ 370 static void rlb_teach_disabled_mac_on_primary(struct bonding *bond, 371 const u8 addr[]) 372 { 373 struct slave *curr_active = rtnl_dereference(bond->curr_active_slave); 374 375 if (!curr_active) 376 return; 377 378 if (!bond->alb_info.primary_is_promisc) { 379 if (!dev_set_promiscuity(curr_active->dev, 1)) 380 bond->alb_info.primary_is_promisc = 1; 381 else 382 bond->alb_info.primary_is_promisc = 0; 383 } 384 385 bond->alb_info.rlb_promisc_timeout_counter = 0; 386 387 alb_send_learning_packets(curr_active, addr, true); 388 } 389 390 /* slave being removed should not be active at this point 391 * 392 * Caller must hold rtnl. 393 */ 394 static void rlb_clear_slave(struct bonding *bond, struct slave *slave) 395 { 396 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 397 struct rlb_client_info *rx_hash_table; 398 u32 index, next_index; 399 400 /* clear slave from rx_hashtbl */ 401 spin_lock_bh(&bond->mode_lock); 402 403 rx_hash_table = bond_info->rx_hashtbl; 404 index = bond_info->rx_hashtbl_used_head; 405 for (; index != RLB_NULL_INDEX; index = next_index) { 406 next_index = rx_hash_table[index].used_next; 407 if (rx_hash_table[index].slave == slave) { 408 struct slave *assigned_slave = rlb_next_rx_slave(bond); 409 410 if (assigned_slave) { 411 rx_hash_table[index].slave = assigned_slave; 412 if (is_valid_ether_addr(rx_hash_table[index].mac_dst)) { 413 bond_info->rx_hashtbl[index].ntt = 1; 414 bond_info->rx_ntt = 1; 415 /* A slave has been removed from the 416 * table because it is either disabled 417 * or being released. We must retry the 418 * update to avoid clients from not 419 * being updated & disconnecting when 420 * there is stress 421 */ 422 bond_info->rlb_update_retry_counter = 423 RLB_UPDATE_RETRY; 424 } 425 } else { /* there is no active slave */ 426 rx_hash_table[index].slave = NULL; 427 } 428 } 429 } 430 431 spin_unlock_bh(&bond->mode_lock); 432 433 if (slave != rtnl_dereference(bond->curr_active_slave)) 434 rlb_teach_disabled_mac_on_primary(bond, slave->dev->dev_addr); 435 } 436 437 static void rlb_update_client(struct rlb_client_info *client_info) 438 { 439 int i; 440 441 if (!client_info->slave || !is_valid_ether_addr(client_info->mac_dst)) 442 return; 443 444 for (i = 0; i < RLB_ARP_BURST_SIZE; i++) { 445 struct sk_buff *skb; 446 447 skb = arp_create(ARPOP_REPLY, ETH_P_ARP, 448 client_info->ip_dst, 449 client_info->slave->dev, 450 client_info->ip_src, 451 client_info->mac_dst, 452 client_info->slave->dev->dev_addr, 453 client_info->mac_dst); 454 if (!skb) { 455 slave_err(client_info->slave->bond->dev, 456 client_info->slave->dev, 457 "failed to create an ARP packet\n"); 458 continue; 459 } 460 461 skb->dev = client_info->slave->dev; 462 463 if (client_info->vlan_id) { 464 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), 465 client_info->vlan_id); 466 } 467 468 arp_xmit(skb); 469 } 470 } 471 472 /* sends ARP REPLIES that update the clients that need updating */ 473 static void rlb_update_rx_clients(struct bonding *bond) 474 { 475 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 476 struct rlb_client_info *client_info; 477 u32 hash_index; 478 479 spin_lock_bh(&bond->mode_lock); 480 481 hash_index = bond_info->rx_hashtbl_used_head; 482 for (; hash_index != RLB_NULL_INDEX; 483 hash_index = client_info->used_next) { 484 client_info = &(bond_info->rx_hashtbl[hash_index]); 485 if (client_info->ntt) { 486 rlb_update_client(client_info); 487 if (bond_info->rlb_update_retry_counter == 0) 488 client_info->ntt = 0; 489 } 490 } 491 492 /* do not update the entries again until this counter is zero so that 493 * not to confuse the clients. 494 */ 495 bond_info->rlb_update_delay_counter = RLB_UPDATE_DELAY; 496 497 spin_unlock_bh(&bond->mode_lock); 498 } 499 500 /* The slave was assigned a new mac address - update the clients */ 501 static void rlb_req_update_slave_clients(struct bonding *bond, struct slave *slave) 502 { 503 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 504 struct rlb_client_info *client_info; 505 int ntt = 0; 506 u32 hash_index; 507 508 spin_lock_bh(&bond->mode_lock); 509 510 hash_index = bond_info->rx_hashtbl_used_head; 511 for (; hash_index != RLB_NULL_INDEX; 512 hash_index = client_info->used_next) { 513 client_info = &(bond_info->rx_hashtbl[hash_index]); 514 515 if ((client_info->slave == slave) && 516 is_valid_ether_addr(client_info->mac_dst)) { 517 client_info->ntt = 1; 518 ntt = 1; 519 } 520 } 521 522 /* update the team's flag only after the whole iteration */ 523 if (ntt) { 524 bond_info->rx_ntt = 1; 525 /* fasten the change */ 526 bond_info->rlb_update_retry_counter = RLB_UPDATE_RETRY; 527 } 528 529 spin_unlock_bh(&bond->mode_lock); 530 } 531 532 /* mark all clients using src_ip to be updated */ 533 static void rlb_req_update_subnet_clients(struct bonding *bond, __be32 src_ip) 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 spin_lock(&bond->mode_lock); 540 541 hash_index = bond_info->rx_hashtbl_used_head; 542 for (; hash_index != RLB_NULL_INDEX; 543 hash_index = client_info->used_next) { 544 client_info = &(bond_info->rx_hashtbl[hash_index]); 545 546 if (!client_info->slave) { 547 netdev_err(bond->dev, "found a client with no channel in the client's hash table\n"); 548 continue; 549 } 550 /* update all clients using this src_ip, that are not assigned 551 * to the team's address (curr_active_slave) and have a known 552 * unicast mac address. 553 */ 554 if ((client_info->ip_src == src_ip) && 555 !ether_addr_equal_64bits(client_info->slave->dev->dev_addr, 556 bond->dev->dev_addr) && 557 is_valid_ether_addr(client_info->mac_dst)) { 558 client_info->ntt = 1; 559 bond_info->rx_ntt = 1; 560 } 561 } 562 563 spin_unlock(&bond->mode_lock); 564 } 565 566 static struct slave *rlb_choose_channel(struct sk_buff *skb, 567 struct bonding *bond, 568 const struct arp_pkt *arp) 569 { 570 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 571 struct slave *assigned_slave, *curr_active_slave; 572 struct rlb_client_info *client_info; 573 u32 hash_index = 0; 574 575 spin_lock(&bond->mode_lock); 576 577 curr_active_slave = rcu_dereference(bond->curr_active_slave); 578 579 hash_index = _simple_hash((u8 *)&arp->ip_dst, sizeof(arp->ip_dst)); 580 client_info = &(bond_info->rx_hashtbl[hash_index]); 581 582 if (client_info->assigned) { 583 if ((client_info->ip_src == arp->ip_src) && 584 (client_info->ip_dst == arp->ip_dst)) { 585 /* the entry is already assigned to this client */ 586 if (!is_broadcast_ether_addr(arp->mac_dst)) { 587 /* update mac address from arp */ 588 ether_addr_copy(client_info->mac_dst, arp->mac_dst); 589 } 590 ether_addr_copy(client_info->mac_src, arp->mac_src); 591 592 assigned_slave = client_info->slave; 593 if (assigned_slave) { 594 spin_unlock(&bond->mode_lock); 595 return assigned_slave; 596 } 597 } else { 598 /* the entry is already assigned to some other client, 599 * move the old client to primary (curr_active_slave) so 600 * that the new client can be assigned to this entry. 601 */ 602 if (curr_active_slave && 603 client_info->slave != curr_active_slave) { 604 client_info->slave = curr_active_slave; 605 rlb_update_client(client_info); 606 } 607 } 608 } 609 /* assign a new slave */ 610 assigned_slave = __rlb_next_rx_slave(bond); 611 612 if (assigned_slave) { 613 if (!(client_info->assigned && 614 client_info->ip_src == arp->ip_src)) { 615 /* ip_src is going to be updated, 616 * fix the src hash list 617 */ 618 u32 hash_src = _simple_hash((u8 *)&arp->ip_src, 619 sizeof(arp->ip_src)); 620 rlb_src_unlink(bond, hash_index); 621 rlb_src_link(bond, hash_src, hash_index); 622 } 623 624 client_info->ip_src = arp->ip_src; 625 client_info->ip_dst = arp->ip_dst; 626 /* arp->mac_dst is broadcast for arp requests. 627 * will be updated with clients actual unicast mac address 628 * upon receiving an arp reply. 629 */ 630 ether_addr_copy(client_info->mac_dst, arp->mac_dst); 631 ether_addr_copy(client_info->mac_src, arp->mac_src); 632 client_info->slave = assigned_slave; 633 634 if (is_valid_ether_addr(client_info->mac_dst)) { 635 client_info->ntt = 1; 636 bond->alb_info.rx_ntt = 1; 637 } else { 638 client_info->ntt = 0; 639 } 640 641 if (vlan_get_tag(skb, &client_info->vlan_id)) 642 client_info->vlan_id = 0; 643 644 if (!client_info->assigned) { 645 u32 prev_tbl_head = bond_info->rx_hashtbl_used_head; 646 647 bond_info->rx_hashtbl_used_head = hash_index; 648 client_info->used_next = prev_tbl_head; 649 if (prev_tbl_head != RLB_NULL_INDEX) { 650 bond_info->rx_hashtbl[prev_tbl_head].used_prev = 651 hash_index; 652 } 653 client_info->assigned = 1; 654 } 655 } 656 657 spin_unlock(&bond->mode_lock); 658 659 return assigned_slave; 660 } 661 662 /* chooses (and returns) transmit channel for arp reply 663 * does not choose channel for other arp types since they are 664 * sent on the curr_active_slave 665 */ 666 static struct slave *rlb_arp_xmit(struct sk_buff *skb, struct bonding *bond) 667 { 668 struct slave *tx_slave = NULL; 669 struct net_device *dev; 670 struct arp_pkt *arp; 671 672 if (!pskb_network_may_pull(skb, sizeof(*arp))) 673 return NULL; 674 arp = (struct arp_pkt *)skb_network_header(skb); 675 676 /* Don't modify or load balance ARPs that do not originate 677 * from the bond itself or a VLAN directly above the bond. 678 */ 679 if (!bond_slave_has_mac_rcu(bond, arp->mac_src)) 680 return NULL; 681 682 dev = ip_dev_find(dev_net(bond->dev), arp->ip_src); 683 if (dev) { 684 if (netif_is_any_bridge_master(dev)) { 685 dev_put(dev); 686 return NULL; 687 } 688 dev_put(dev); 689 } 690 691 if (arp->op_code == htons(ARPOP_REPLY)) { 692 /* the arp must be sent on the selected rx channel */ 693 tx_slave = rlb_choose_channel(skb, bond, arp); 694 if (tx_slave && 695 !ether_addr_equal_64bits(arp->mac_src, 696 tx_slave->dev->dev_addr)) { 697 if (unlikely(skb_cow_head(skb, 0))) 698 return NULL; 699 arp = (struct arp_pkt *)skb_network_header(skb); 700 bond_hw_addr_copy(arp->mac_src, tx_slave->dev->dev_addr, 701 tx_slave->dev->addr_len); 702 } 703 netdev_dbg(bond->dev, "(slave %s): Server sent ARP Reply packet\n", 704 tx_slave ? tx_slave->dev->name : "NULL"); 705 } else if (arp->op_code == htons(ARPOP_REQUEST)) { 706 /* Create an entry in the rx_hashtbl for this client as a 707 * place holder. 708 * When the arp reply is received the entry will be updated 709 * with the correct unicast address of the client. 710 */ 711 tx_slave = rlb_choose_channel(skb, bond, arp); 712 713 /* The ARP reply packets must be delayed so that 714 * they can cancel out the influence of the ARP request. 715 */ 716 bond->alb_info.rlb_update_delay_counter = RLB_UPDATE_DELAY; 717 718 /* arp requests are broadcast and are sent on the primary 719 * the arp request will collapse all clients on the subnet to 720 * the primary slave. We must register these clients to be 721 * updated with their assigned mac. 722 */ 723 rlb_req_update_subnet_clients(bond, arp->ip_src); 724 netdev_dbg(bond->dev, "(slave %s): Server sent ARP Request packet\n", 725 tx_slave ? tx_slave->dev->name : "NULL"); 726 } 727 728 return tx_slave; 729 } 730 731 static void rlb_rebalance(struct bonding *bond) 732 { 733 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 734 struct slave *assigned_slave; 735 struct rlb_client_info *client_info; 736 int ntt; 737 u32 hash_index; 738 739 spin_lock_bh(&bond->mode_lock); 740 741 ntt = 0; 742 hash_index = bond_info->rx_hashtbl_used_head; 743 for (; hash_index != RLB_NULL_INDEX; 744 hash_index = client_info->used_next) { 745 client_info = &(bond_info->rx_hashtbl[hash_index]); 746 assigned_slave = __rlb_next_rx_slave(bond); 747 if (assigned_slave && (client_info->slave != assigned_slave)) { 748 client_info->slave = assigned_slave; 749 if (!is_zero_ether_addr(client_info->mac_dst)) { 750 client_info->ntt = 1; 751 ntt = 1; 752 } 753 } 754 } 755 756 /* update the team's flag only after the whole iteration */ 757 if (ntt) 758 bond_info->rx_ntt = 1; 759 spin_unlock_bh(&bond->mode_lock); 760 } 761 762 /* Caller must hold mode_lock */ 763 static void rlb_init_table_entry_dst(struct rlb_client_info *entry) 764 { 765 entry->used_next = RLB_NULL_INDEX; 766 entry->used_prev = RLB_NULL_INDEX; 767 entry->assigned = 0; 768 entry->slave = NULL; 769 entry->vlan_id = 0; 770 } 771 static void rlb_init_table_entry_src(struct rlb_client_info *entry) 772 { 773 entry->src_first = RLB_NULL_INDEX; 774 entry->src_prev = RLB_NULL_INDEX; 775 entry->src_next = RLB_NULL_INDEX; 776 } 777 778 static void rlb_init_table_entry(struct rlb_client_info *entry) 779 { 780 memset(entry, 0, sizeof(struct rlb_client_info)); 781 rlb_init_table_entry_dst(entry); 782 rlb_init_table_entry_src(entry); 783 } 784 785 static void rlb_delete_table_entry_dst(struct bonding *bond, u32 index) 786 { 787 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 788 u32 next_index = bond_info->rx_hashtbl[index].used_next; 789 u32 prev_index = bond_info->rx_hashtbl[index].used_prev; 790 791 if (index == bond_info->rx_hashtbl_used_head) 792 bond_info->rx_hashtbl_used_head = next_index; 793 if (prev_index != RLB_NULL_INDEX) 794 bond_info->rx_hashtbl[prev_index].used_next = next_index; 795 if (next_index != RLB_NULL_INDEX) 796 bond_info->rx_hashtbl[next_index].used_prev = prev_index; 797 } 798 799 /* unlink a rlb hash table entry from the src list */ 800 static void rlb_src_unlink(struct bonding *bond, u32 index) 801 { 802 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 803 u32 next_index = bond_info->rx_hashtbl[index].src_next; 804 u32 prev_index = bond_info->rx_hashtbl[index].src_prev; 805 806 bond_info->rx_hashtbl[index].src_next = RLB_NULL_INDEX; 807 bond_info->rx_hashtbl[index].src_prev = RLB_NULL_INDEX; 808 809 if (next_index != RLB_NULL_INDEX) 810 bond_info->rx_hashtbl[next_index].src_prev = prev_index; 811 812 if (prev_index == RLB_NULL_INDEX) 813 return; 814 815 /* is prev_index pointing to the head of this list? */ 816 if (bond_info->rx_hashtbl[prev_index].src_first == index) 817 bond_info->rx_hashtbl[prev_index].src_first = next_index; 818 else 819 bond_info->rx_hashtbl[prev_index].src_next = next_index; 820 821 } 822 823 static void rlb_delete_table_entry(struct bonding *bond, u32 index) 824 { 825 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 826 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]); 827 828 rlb_delete_table_entry_dst(bond, index); 829 rlb_init_table_entry_dst(entry); 830 831 rlb_src_unlink(bond, index); 832 } 833 834 /* add the rx_hashtbl[ip_dst_hash] entry to the list 835 * of entries with identical ip_src_hash 836 */ 837 static void rlb_src_link(struct bonding *bond, u32 ip_src_hash, u32 ip_dst_hash) 838 { 839 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 840 u32 next; 841 842 bond_info->rx_hashtbl[ip_dst_hash].src_prev = ip_src_hash; 843 next = bond_info->rx_hashtbl[ip_src_hash].src_first; 844 bond_info->rx_hashtbl[ip_dst_hash].src_next = next; 845 if (next != RLB_NULL_INDEX) 846 bond_info->rx_hashtbl[next].src_prev = ip_dst_hash; 847 bond_info->rx_hashtbl[ip_src_hash].src_first = ip_dst_hash; 848 } 849 850 /* deletes all rx_hashtbl entries with arp->ip_src if their mac_src does 851 * not match arp->mac_src 852 */ 853 static void rlb_purge_src_ip(struct bonding *bond, struct arp_pkt *arp) 854 { 855 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 856 u32 ip_src_hash = _simple_hash((u8 *)&(arp->ip_src), sizeof(arp->ip_src)); 857 u32 index; 858 859 spin_lock_bh(&bond->mode_lock); 860 861 index = bond_info->rx_hashtbl[ip_src_hash].src_first; 862 while (index != RLB_NULL_INDEX) { 863 struct rlb_client_info *entry = &(bond_info->rx_hashtbl[index]); 864 u32 next_index = entry->src_next; 865 866 if (entry->ip_src == arp->ip_src && 867 !ether_addr_equal_64bits(arp->mac_src, entry->mac_src)) 868 rlb_delete_table_entry(bond, index); 869 index = next_index; 870 } 871 spin_unlock_bh(&bond->mode_lock); 872 } 873 874 static int rlb_initialize(struct bonding *bond) 875 { 876 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 877 struct rlb_client_info *new_hashtbl; 878 int size = RLB_HASH_TABLE_SIZE * sizeof(struct rlb_client_info); 879 int i; 880 881 new_hashtbl = kmalloc(size, GFP_KERNEL); 882 if (!new_hashtbl) 883 return -1; 884 885 spin_lock_bh(&bond->mode_lock); 886 887 bond_info->rx_hashtbl = new_hashtbl; 888 889 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX; 890 891 for (i = 0; i < RLB_HASH_TABLE_SIZE; i++) 892 rlb_init_table_entry(bond_info->rx_hashtbl + i); 893 894 spin_unlock_bh(&bond->mode_lock); 895 896 /* register to receive ARPs */ 897 WRITE_ONCE(bond->recv_probe, rlb_arp_recv); 898 899 return 0; 900 } 901 902 static void rlb_deinitialize(struct bonding *bond) 903 { 904 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 905 906 spin_lock_bh(&bond->mode_lock); 907 908 kfree(bond_info->rx_hashtbl); 909 bond_info->rx_hashtbl = NULL; 910 bond_info->rx_hashtbl_used_head = RLB_NULL_INDEX; 911 912 spin_unlock_bh(&bond->mode_lock); 913 } 914 915 static void rlb_clear_vlan(struct bonding *bond, unsigned short vlan_id) 916 { 917 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 918 u32 curr_index; 919 920 spin_lock_bh(&bond->mode_lock); 921 922 curr_index = bond_info->rx_hashtbl_used_head; 923 while (curr_index != RLB_NULL_INDEX) { 924 struct rlb_client_info *curr = &(bond_info->rx_hashtbl[curr_index]); 925 u32 next_index = bond_info->rx_hashtbl[curr_index].used_next; 926 927 if (curr->vlan_id == vlan_id) 928 rlb_delete_table_entry(bond, curr_index); 929 930 curr_index = next_index; 931 } 932 933 spin_unlock_bh(&bond->mode_lock); 934 } 935 936 /*********************** tlb/rlb shared functions *********************/ 937 938 static void alb_send_lp_vid(struct slave *slave, const u8 mac_addr[], 939 __be16 vlan_proto, u16 vid) 940 { 941 struct learning_pkt pkt; 942 struct sk_buff *skb; 943 int size = sizeof(struct learning_pkt); 944 945 memset(&pkt, 0, size); 946 ether_addr_copy(pkt.mac_dst, mac_addr); 947 ether_addr_copy(pkt.mac_src, mac_addr); 948 pkt.type = cpu_to_be16(ETH_P_LOOPBACK); 949 950 skb = dev_alloc_skb(size); 951 if (!skb) 952 return; 953 954 skb_put_data(skb, &pkt, size); 955 956 skb_reset_mac_header(skb); 957 skb->network_header = skb->mac_header + ETH_HLEN; 958 skb->protocol = pkt.type; 959 skb->priority = TC_PRIO_CONTROL; 960 skb->dev = slave->dev; 961 962 slave_dbg(slave->bond->dev, slave->dev, 963 "Send learning packet: mac %pM vlan %d\n", mac_addr, vid); 964 965 if (vid) 966 __vlan_hwaccel_put_tag(skb, vlan_proto, vid); 967 968 dev_queue_xmit(skb); 969 } 970 971 struct alb_walk_data { 972 struct bonding *bond; 973 struct slave *slave; 974 const u8 *mac_addr; 975 bool strict_match; 976 }; 977 978 static int alb_upper_dev_walk(struct net_device *upper, 979 struct netdev_nested_priv *priv) 980 { 981 struct alb_walk_data *data = (struct alb_walk_data *)priv->data; 982 bool strict_match = data->strict_match; 983 const u8 *mac_addr = data->mac_addr; 984 struct bonding *bond = data->bond; 985 struct slave *slave = data->slave; 986 struct bond_vlan_tag *tags; 987 988 if (is_vlan_dev(upper) && 989 bond->dev->lower_level == upper->lower_level - 1) { 990 if (upper->addr_assign_type == NET_ADDR_STOLEN) { 991 alb_send_lp_vid(slave, mac_addr, 992 vlan_dev_vlan_proto(upper), 993 vlan_dev_vlan_id(upper)); 994 } else { 995 alb_send_lp_vid(slave, upper->dev_addr, 996 vlan_dev_vlan_proto(upper), 997 vlan_dev_vlan_id(upper)); 998 } 999 } 1000 1001 /* If this is a macvlan device, then only send updates 1002 * when strict_match is turned off. 1003 */ 1004 if (netif_is_macvlan(upper) && !strict_match) { 1005 tags = bond_verify_device_path(bond->dev, upper, 0); 1006 if (IS_ERR_OR_NULL(tags)) 1007 return -ENOMEM; 1008 1009 alb_send_lp_vid(slave, upper->dev_addr, 1010 tags[0].vlan_proto, tags[0].vlan_id); 1011 kfree(tags); 1012 } 1013 1014 return 0; 1015 } 1016 1017 static void alb_send_learning_packets(struct slave *slave, const u8 mac_addr[], 1018 bool strict_match) 1019 { 1020 struct bonding *bond = bond_get_bond_by_slave(slave); 1021 struct netdev_nested_priv priv; 1022 struct alb_walk_data data = { 1023 .strict_match = strict_match, 1024 .mac_addr = mac_addr, 1025 .slave = slave, 1026 .bond = bond, 1027 }; 1028 1029 priv.data = (void *)&data; 1030 /* send untagged */ 1031 alb_send_lp_vid(slave, mac_addr, 0, 0); 1032 1033 /* loop through all devices and see if we need to send a packet 1034 * for that device. 1035 */ 1036 rcu_read_lock(); 1037 netdev_walk_all_upper_dev_rcu(bond->dev, alb_upper_dev_walk, &priv); 1038 rcu_read_unlock(); 1039 } 1040 1041 static int alb_set_slave_mac_addr(struct slave *slave, const u8 addr[], 1042 unsigned int len) 1043 { 1044 struct net_device *dev = slave->dev; 1045 struct sockaddr_storage ss; 1046 1047 if (BOND_MODE(slave->bond) == BOND_MODE_TLB) { 1048 __dev_addr_set(dev, addr, len); 1049 return 0; 1050 } 1051 1052 /* for rlb each slave must have a unique hw mac addresses so that 1053 * each slave will receive packets destined to a different mac 1054 */ 1055 memcpy(ss.__data, addr, len); 1056 ss.ss_family = dev->type; 1057 if (dev_set_mac_address(dev, &ss, NULL)) { 1058 slave_err(slave->bond->dev, dev, "dev_set_mac_address on slave failed! ALB mode requires that the base driver support setting the hw address also when the network device's interface is open\n"); 1059 return -EOPNOTSUPP; 1060 } 1061 return 0; 1062 } 1063 1064 /* Swap MAC addresses between two slaves. 1065 * 1066 * Called with RTNL held, and no other locks. 1067 */ 1068 static void alb_swap_mac_addr(struct slave *slave1, struct slave *slave2) 1069 { 1070 u8 tmp_mac_addr[MAX_ADDR_LEN]; 1071 1072 bond_hw_addr_copy(tmp_mac_addr, slave1->dev->dev_addr, 1073 slave1->dev->addr_len); 1074 alb_set_slave_mac_addr(slave1, slave2->dev->dev_addr, 1075 slave2->dev->addr_len); 1076 alb_set_slave_mac_addr(slave2, tmp_mac_addr, 1077 slave1->dev->addr_len); 1078 1079 } 1080 1081 /* Send learning packets after MAC address swap. 1082 * 1083 * Called with RTNL and no other locks 1084 */ 1085 static void alb_fasten_mac_swap(struct bonding *bond, struct slave *slave1, 1086 struct slave *slave2) 1087 { 1088 int slaves_state_differ = (bond_slave_can_tx(slave1) != bond_slave_can_tx(slave2)); 1089 struct slave *disabled_slave = NULL; 1090 1091 ASSERT_RTNL(); 1092 1093 /* fasten the change in the switch */ 1094 if (bond_slave_can_tx(slave1)) { 1095 alb_send_learning_packets(slave1, slave1->dev->dev_addr, false); 1096 if (bond->alb_info.rlb_enabled) { 1097 /* inform the clients that the mac address 1098 * has changed 1099 */ 1100 rlb_req_update_slave_clients(bond, slave1); 1101 } 1102 } else { 1103 disabled_slave = slave1; 1104 } 1105 1106 if (bond_slave_can_tx(slave2)) { 1107 alb_send_learning_packets(slave2, slave2->dev->dev_addr, false); 1108 if (bond->alb_info.rlb_enabled) { 1109 /* inform the clients that the mac address 1110 * has changed 1111 */ 1112 rlb_req_update_slave_clients(bond, slave2); 1113 } 1114 } else { 1115 disabled_slave = slave2; 1116 } 1117 1118 if (bond->alb_info.rlb_enabled && slaves_state_differ) { 1119 /* A disabled slave was assigned an active mac addr */ 1120 rlb_teach_disabled_mac_on_primary(bond, 1121 disabled_slave->dev->dev_addr); 1122 } 1123 } 1124 1125 /** 1126 * alb_change_hw_addr_on_detach 1127 * @bond: bonding we're working on 1128 * @slave: the slave that was just detached 1129 * 1130 * We assume that @slave was already detached from the slave list. 1131 * 1132 * If @slave's permanent hw address is different both from its current 1133 * address and from @bond's address, then somewhere in the bond there's 1134 * a slave that has @slave's permanet address as its current address. 1135 * We'll make sure that slave no longer uses @slave's permanent address. 1136 * 1137 * Caller must hold RTNL and no other locks 1138 */ 1139 static void alb_change_hw_addr_on_detach(struct bonding *bond, struct slave *slave) 1140 { 1141 int perm_curr_diff; 1142 int perm_bond_diff; 1143 struct slave *found_slave; 1144 1145 perm_curr_diff = !ether_addr_equal_64bits(slave->perm_hwaddr, 1146 slave->dev->dev_addr); 1147 perm_bond_diff = !ether_addr_equal_64bits(slave->perm_hwaddr, 1148 bond->dev->dev_addr); 1149 1150 if (perm_curr_diff && perm_bond_diff) { 1151 found_slave = bond_slave_has_mac(bond, slave->perm_hwaddr); 1152 1153 if (found_slave) { 1154 alb_swap_mac_addr(slave, found_slave); 1155 alb_fasten_mac_swap(bond, slave, found_slave); 1156 } 1157 } 1158 } 1159 1160 /** 1161 * alb_handle_addr_collision_on_attach 1162 * @bond: bonding we're working on 1163 * @slave: the slave that was just attached 1164 * 1165 * checks uniqueness of slave's mac address and handles the case the 1166 * new slave uses the bonds mac address. 1167 * 1168 * If the permanent hw address of @slave is @bond's hw address, we need to 1169 * find a different hw address to give @slave, that isn't in use by any other 1170 * slave in the bond. This address must be, of course, one of the permanent 1171 * addresses of the other slaves. 1172 * 1173 * We go over the slave list, and for each slave there we compare its 1174 * permanent hw address with the current address of all the other slaves. 1175 * If no match was found, then we've found a slave with a permanent address 1176 * that isn't used by any other slave in the bond, so we can assign it to 1177 * @slave. 1178 * 1179 * assumption: this function is called before @slave is attached to the 1180 * bond slave list. 1181 */ 1182 static int alb_handle_addr_collision_on_attach(struct bonding *bond, struct slave *slave) 1183 { 1184 struct slave *has_bond_addr = rcu_access_pointer(bond->curr_active_slave); 1185 struct slave *tmp_slave1, *free_mac_slave = NULL; 1186 struct list_head *iter; 1187 1188 if (!bond_has_slaves(bond)) { 1189 /* this is the first slave */ 1190 return 0; 1191 } 1192 1193 /* if slave's mac address differs from bond's mac address 1194 * check uniqueness of slave's mac address against the other 1195 * slaves in the bond. 1196 */ 1197 if (!ether_addr_equal_64bits(slave->perm_hwaddr, bond->dev->dev_addr)) { 1198 if (!bond_slave_has_mac(bond, slave->dev->dev_addr)) 1199 return 0; 1200 1201 /* Try setting slave mac to bond address and fall-through 1202 * to code handling that situation below... 1203 */ 1204 alb_set_slave_mac_addr(slave, bond->dev->dev_addr, 1205 bond->dev->addr_len); 1206 } 1207 1208 /* The slave's address is equal to the address of the bond. 1209 * Search for a spare address in the bond for this slave. 1210 */ 1211 bond_for_each_slave(bond, tmp_slave1, iter) { 1212 if (!bond_slave_has_mac(bond, tmp_slave1->perm_hwaddr)) { 1213 /* no slave has tmp_slave1's perm addr 1214 * as its curr addr 1215 */ 1216 free_mac_slave = tmp_slave1; 1217 break; 1218 } 1219 1220 if (!has_bond_addr) { 1221 if (ether_addr_equal_64bits(tmp_slave1->dev->dev_addr, 1222 bond->dev->dev_addr)) { 1223 1224 has_bond_addr = tmp_slave1; 1225 } 1226 } 1227 } 1228 1229 if (free_mac_slave) { 1230 alb_set_slave_mac_addr(slave, free_mac_slave->perm_hwaddr, 1231 free_mac_slave->dev->addr_len); 1232 1233 slave_warn(bond->dev, slave->dev, "the slave hw address is in use by the bond; giving it the hw address of %s\n", 1234 free_mac_slave->dev->name); 1235 1236 } else if (has_bond_addr) { 1237 slave_err(bond->dev, slave->dev, "the slave hw address 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"); 1238 return -EFAULT; 1239 } 1240 1241 return 0; 1242 } 1243 1244 /** 1245 * alb_set_mac_address 1246 * @bond: bonding we're working on 1247 * @addr: MAC address to set 1248 * 1249 * In TLB mode all slaves are configured to the bond's hw address, but set 1250 * their dev_addr field to different addresses (based on their permanent hw 1251 * addresses). 1252 * 1253 * For each slave, this function sets the interface to the new address and then 1254 * changes its dev_addr field to its previous value. 1255 * 1256 * Unwinding assumes bond's mac address has not yet changed. 1257 */ 1258 static int alb_set_mac_address(struct bonding *bond, void *addr) 1259 { 1260 struct slave *slave, *rollback_slave; 1261 struct list_head *iter; 1262 struct sockaddr_storage ss; 1263 char tmp_addr[MAX_ADDR_LEN]; 1264 int res; 1265 1266 if (bond->alb_info.rlb_enabled) 1267 return 0; 1268 1269 bond_for_each_slave(bond, slave, iter) { 1270 /* save net_device's current hw address */ 1271 bond_hw_addr_copy(tmp_addr, slave->dev->dev_addr, 1272 slave->dev->addr_len); 1273 1274 res = dev_set_mac_address(slave->dev, addr, NULL); 1275 1276 /* restore net_device's hw address */ 1277 dev_addr_set(slave->dev, tmp_addr); 1278 1279 if (res) 1280 goto unwind; 1281 } 1282 1283 return 0; 1284 1285 unwind: 1286 memcpy(ss.__data, bond->dev->dev_addr, bond->dev->addr_len); 1287 ss.ss_family = bond->dev->type; 1288 1289 /* unwind from head to the slave that failed */ 1290 bond_for_each_slave(bond, rollback_slave, iter) { 1291 if (rollback_slave == slave) 1292 break; 1293 bond_hw_addr_copy(tmp_addr, rollback_slave->dev->dev_addr, 1294 rollback_slave->dev->addr_len); 1295 dev_set_mac_address(rollback_slave->dev, &ss, NULL); 1296 dev_addr_set(rollback_slave->dev, tmp_addr); 1297 } 1298 1299 return res; 1300 } 1301 1302 /* determine if the packet is NA or NS */ 1303 static bool alb_determine_nd(struct sk_buff *skb) 1304 { 1305 const struct ipv6hdr *ip6hdr; 1306 const struct icmp6hdr *hdr; 1307 1308 if (!pskb_network_may_pull(skb, sizeof(*ip6hdr))) 1309 return true; 1310 1311 ip6hdr = ipv6_hdr(skb); 1312 if (ip6hdr->nexthdr != IPPROTO_ICMPV6) 1313 return false; 1314 1315 if (!pskb_network_may_pull(skb, sizeof(*ip6hdr) + sizeof(*hdr))) 1316 return true; 1317 1318 ip6hdr = ipv6_hdr(skb); 1319 hdr = (const struct icmp6hdr *)(ip6hdr + 1); 1320 return hdr->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT || 1321 hdr->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION; 1322 } 1323 1324 /************************ exported alb functions ************************/ 1325 1326 int bond_alb_initialize(struct bonding *bond, int rlb_enabled) 1327 { 1328 int res; 1329 1330 res = tlb_initialize(bond); 1331 if (res) 1332 return res; 1333 1334 if (rlb_enabled) { 1335 res = rlb_initialize(bond); 1336 if (res) { 1337 tlb_deinitialize(bond); 1338 return res; 1339 } 1340 bond->alb_info.rlb_enabled = 1; 1341 } else { 1342 bond->alb_info.rlb_enabled = 0; 1343 } 1344 1345 return 0; 1346 } 1347 1348 void bond_alb_deinitialize(struct bonding *bond) 1349 { 1350 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 1351 1352 tlb_deinitialize(bond); 1353 1354 if (bond_info->rlb_enabled) 1355 rlb_deinitialize(bond); 1356 } 1357 1358 static netdev_tx_t bond_do_alb_xmit(struct sk_buff *skb, struct bonding *bond, 1359 struct slave *tx_slave) 1360 { 1361 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 1362 1363 if (!tx_slave) { 1364 /* unbalanced or unassigned, send through primary */ 1365 tx_slave = rcu_dereference(bond->curr_active_slave); 1366 if (bond->params.tlb_dynamic_lb) { 1367 struct unbalanced_load_stats *pcpu_load; 1368 1369 pcpu_load = this_cpu_ptr(bond_info->unbalanced_load); 1370 u64_stats_update_begin(&pcpu_load->syncp); 1371 u64_stats_add(&pcpu_load->tx_bytes, skb->len); 1372 u64_stats_update_end(&pcpu_load->syncp); 1373 } 1374 } 1375 1376 if (tx_slave && bond_slave_can_tx(tx_slave)) { 1377 if (tx_slave != rcu_access_pointer(bond->curr_active_slave)) { 1378 if (unlikely(skb_cow_head(skb, 0))) 1379 return bond_tx_drop(bond->dev, skb); 1380 ether_addr_copy(skb_eth_hdr(skb)->h_source, 1381 tx_slave->dev->dev_addr); 1382 } 1383 1384 return bond_dev_queue_xmit(bond, skb, tx_slave->dev); 1385 } 1386 1387 if (tx_slave && bond->params.tlb_dynamic_lb) { 1388 spin_lock(&bond->mode_lock); 1389 __tlb_clear_slave(bond, tx_slave, 0); 1390 spin_unlock(&bond->mode_lock); 1391 } 1392 1393 /* no suitable interface, frame not sent */ 1394 return bond_tx_drop(bond->dev, skb); 1395 } 1396 1397 struct slave *bond_xmit_tlb_slave_get(struct bonding *bond, 1398 struct sk_buff *skb) 1399 { 1400 struct slave *tx_slave = NULL; 1401 struct ethhdr *eth_data; 1402 u32 hash_index; 1403 1404 eth_data = skb_eth_hdr(skb); 1405 1406 /* Do not TX balance any multicast or broadcast */ 1407 if (!is_multicast_ether_addr(eth_data->h_dest)) { 1408 switch (skb->protocol) { 1409 case htons(ETH_P_IPV6): 1410 if (alb_determine_nd(skb)) 1411 break; 1412 fallthrough; 1413 case htons(ETH_P_IP): 1414 hash_index = bond_xmit_hash(bond, skb); 1415 if (bond->params.tlb_dynamic_lb) { 1416 tx_slave = tlb_choose_channel(bond, 1417 hash_index & 0xFF, 1418 skb->len); 1419 } else { 1420 struct bond_up_slave *slaves; 1421 unsigned int count; 1422 1423 slaves = rcu_dereference(bond->usable_slaves); 1424 count = slaves ? READ_ONCE(slaves->count) : 0; 1425 if (likely(count)) 1426 tx_slave = slaves->arr[hash_index % 1427 count]; 1428 } 1429 break; 1430 } 1431 } 1432 return tx_slave; 1433 } 1434 1435 netdev_tx_t bond_tlb_xmit(struct sk_buff *skb, struct net_device *bond_dev) 1436 { 1437 struct bonding *bond = netdev_priv(bond_dev); 1438 struct slave *tx_slave; 1439 1440 tx_slave = bond_xmit_tlb_slave_get(bond, skb); 1441 return bond_do_alb_xmit(skb, bond, tx_slave); 1442 } 1443 1444 struct slave *bond_xmit_alb_slave_get(struct bonding *bond, 1445 struct sk_buff *skb) 1446 { 1447 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 1448 static const __be32 ip_bcast = htonl(0xffffffff); 1449 struct slave *tx_slave = NULL; 1450 const u8 *hash_start = NULL; 1451 bool do_tx_balance = true; 1452 struct ethhdr *eth_data; 1453 u32 hash_index = 0; 1454 int hash_size = 0; 1455 1456 eth_data = skb_eth_hdr(skb); 1457 1458 switch (ntohs(skb->protocol)) { 1459 case ETH_P_IP: { 1460 const struct iphdr *iph; 1461 1462 if (is_broadcast_ether_addr(eth_data->h_dest) || 1463 !pskb_network_may_pull(skb, sizeof(*iph))) { 1464 do_tx_balance = false; 1465 break; 1466 } 1467 iph = ip_hdr(skb); 1468 if (iph->daddr == ip_bcast || iph->protocol == IPPROTO_IGMP) { 1469 do_tx_balance = false; 1470 break; 1471 } 1472 hash_start = (char *)&(iph->daddr); 1473 hash_size = sizeof(iph->daddr); 1474 break; 1475 } 1476 case ETH_P_IPV6: { 1477 const struct ipv6hdr *ip6hdr; 1478 1479 /* IPv6 doesn't really use broadcast mac address, but leave 1480 * that here just in case. 1481 */ 1482 if (is_broadcast_ether_addr(eth_data->h_dest)) { 1483 do_tx_balance = false; 1484 break; 1485 } 1486 1487 /* IPv6 uses all-nodes multicast as an equivalent to 1488 * broadcasts in IPv4. 1489 */ 1490 if (ether_addr_equal_64bits(eth_data->h_dest, mac_v6_allmcast)) { 1491 do_tx_balance = false; 1492 break; 1493 } 1494 1495 if (alb_determine_nd(skb)) { 1496 do_tx_balance = false; 1497 break; 1498 } 1499 1500 /* The IPv6 header is pulled by alb_determine_nd */ 1501 /* Additionally, DAD probes should not be tx-balanced as that 1502 * will lead to false positives for duplicate addresses and 1503 * prevent address configuration from working. 1504 */ 1505 ip6hdr = ipv6_hdr(skb); 1506 if (ipv6_addr_any(&ip6hdr->saddr)) { 1507 do_tx_balance = false; 1508 break; 1509 } 1510 1511 hash_start = (char *)&ip6hdr->daddr; 1512 hash_size = sizeof(ip6hdr->daddr); 1513 break; 1514 } 1515 case ETH_P_ARP: 1516 do_tx_balance = false; 1517 if (bond_info->rlb_enabled) 1518 tx_slave = rlb_arp_xmit(skb, bond); 1519 break; 1520 default: 1521 do_tx_balance = false; 1522 break; 1523 } 1524 1525 if (do_tx_balance) { 1526 if (bond->params.tlb_dynamic_lb) { 1527 hash_index = _simple_hash(hash_start, hash_size); 1528 tx_slave = tlb_choose_channel(bond, hash_index, skb->len); 1529 } else { 1530 /* 1531 * do_tx_balance means we are free to select the tx_slave 1532 * So we do exactly what tlb would do for hash selection 1533 */ 1534 1535 struct bond_up_slave *slaves; 1536 unsigned int count; 1537 1538 slaves = rcu_dereference(bond->usable_slaves); 1539 count = slaves ? READ_ONCE(slaves->count) : 0; 1540 if (likely(count)) 1541 tx_slave = slaves->arr[bond_xmit_hash(bond, skb) % 1542 count]; 1543 } 1544 } 1545 return tx_slave; 1546 } 1547 1548 netdev_tx_t bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev) 1549 { 1550 struct bonding *bond = netdev_priv(bond_dev); 1551 struct slave *tx_slave = NULL; 1552 1553 tx_slave = bond_xmit_alb_slave_get(bond, skb); 1554 return bond_do_alb_xmit(skb, bond, tx_slave); 1555 } 1556 1557 static u64 reset_unbalanced_load(struct alb_bond_info *bond_info) 1558 { 1559 u64 delta, tx_bytes, total_bytes = 0; 1560 struct unbalanced_load_stats *p; 1561 unsigned int start; 1562 int i; 1563 1564 for_each_possible_cpu(i) { 1565 p = per_cpu_ptr(bond_info->unbalanced_load, i); 1566 do { 1567 start = u64_stats_fetch_begin(&p->syncp); 1568 tx_bytes = u64_stats_read(&p->tx_bytes); 1569 } while (u64_stats_fetch_retry(&p->syncp, start)); 1570 1571 total_bytes += tx_bytes; 1572 } 1573 1574 delta = total_bytes - bond_info->prev_total_unbalanced; 1575 bond_info->prev_total_unbalanced = total_bytes; 1576 1577 return div_u64(delta, BOND_TLB_REBALANCE_INTERVAL); 1578 } 1579 1580 void bond_alb_monitor(struct work_struct *work) 1581 { 1582 struct bonding *bond = container_of(work, struct bonding, 1583 alb_work.work); 1584 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 1585 struct slave *slave, *curr; 1586 struct list_head *iter; 1587 1588 if (!bond_has_slaves(bond)) { 1589 atomic_set(&bond_info->tx_rebalance_counter, 0); 1590 bond_info->lp_counter = 0; 1591 goto re_arm; 1592 } 1593 1594 rcu_read_lock(); 1595 1596 atomic_inc(&bond_info->tx_rebalance_counter); 1597 bond_info->lp_counter++; 1598 1599 /* send learning packets */ 1600 if (bond_info->lp_counter >= BOND_ALB_LP_TICKS(bond)) { 1601 bool strict_match; 1602 1603 bond_for_each_slave_rcu(bond, slave, iter) { 1604 /* If updating current_active, use all currently 1605 * user mac addresses (!strict_match). Otherwise, only 1606 * use mac of the slave device. 1607 * In RLB mode, we always use strict matches. 1608 */ 1609 strict_match = (slave != rcu_access_pointer(bond->curr_active_slave) || 1610 bond_info->rlb_enabled); 1611 alb_send_learning_packets(slave, slave->dev->dev_addr, 1612 strict_match); 1613 } 1614 bond_info->lp_counter = 0; 1615 } 1616 1617 /* rebalance tx traffic */ 1618 if (atomic_read(&bond_info->tx_rebalance_counter) >= BOND_TLB_REBALANCE_TICKS) { 1619 bond_for_each_slave_rcu(bond, slave, iter) { 1620 tlb_clear_slave(bond, slave, 1); 1621 if (slave == rcu_access_pointer(bond->curr_active_slave)) { 1622 u64 new_load = reset_unbalanced_load(bond_info); 1623 1624 spin_lock_bh(&bond->mode_lock); 1625 SLAVE_TLB_INFO(slave).load = new_load; 1626 spin_unlock_bh(&bond->mode_lock); 1627 } 1628 } 1629 atomic_set(&bond_info->tx_rebalance_counter, 0); 1630 } 1631 1632 if (bond_info->rlb_enabled) { 1633 if (bond_info->primary_is_promisc && 1634 (++bond_info->rlb_promisc_timeout_counter >= RLB_PROMISC_TIMEOUT)) { 1635 1636 /* dev_set_promiscuity requires rtnl and 1637 * nothing else. Avoid race with bond_close. 1638 */ 1639 rcu_read_unlock(); 1640 if (!rtnl_trylock()) 1641 goto re_arm; 1642 1643 bond_info->rlb_promisc_timeout_counter = 0; 1644 1645 /* If the primary was set to promiscuous mode 1646 * because a slave was disabled then 1647 * it can now leave promiscuous mode. 1648 */ 1649 curr = rtnl_dereference(bond->curr_active_slave); 1650 if (bond_info->primary_is_promisc && curr) { 1651 dev_set_promiscuity(curr->dev, -1); 1652 bond_info->primary_is_promisc = 0; 1653 } 1654 1655 rtnl_unlock(); 1656 rcu_read_lock(); 1657 } 1658 1659 if (bond_info->rlb_rebalance) { 1660 bond_info->rlb_rebalance = 0; 1661 rlb_rebalance(bond); 1662 } 1663 1664 /* check if clients need updating */ 1665 if (bond_info->rx_ntt) { 1666 if (bond_info->rlb_update_delay_counter) { 1667 --bond_info->rlb_update_delay_counter; 1668 } else { 1669 rlb_update_rx_clients(bond); 1670 if (bond_info->rlb_update_retry_counter) 1671 --bond_info->rlb_update_retry_counter; 1672 else 1673 bond_info->rx_ntt = 0; 1674 } 1675 } 1676 } 1677 rcu_read_unlock(); 1678 re_arm: 1679 queue_delayed_work(bond->wq, &bond->alb_work, alb_delta_in_ticks); 1680 } 1681 1682 /* assumption: called before the slave is attached to the bond 1683 * and not locked by the bond lock 1684 */ 1685 int bond_alb_init_slave(struct bonding *bond, struct slave *slave) 1686 { 1687 int res; 1688 1689 res = alb_set_slave_mac_addr(slave, slave->perm_hwaddr, 1690 slave->dev->addr_len); 1691 if (res) 1692 return res; 1693 1694 res = alb_handle_addr_collision_on_attach(bond, slave); 1695 if (res) 1696 return res; 1697 1698 tlb_init_slave(slave); 1699 1700 /* order a rebalance ASAP */ 1701 atomic_set(&bond->alb_info.tx_rebalance_counter, 1702 BOND_TLB_REBALANCE_TICKS); 1703 1704 if (bond->alb_info.rlb_enabled) 1705 bond->alb_info.rlb_rebalance = 1; 1706 1707 return 0; 1708 } 1709 1710 /* Remove slave from tlb and rlb hash tables, and fix up MAC addresses 1711 * if necessary. 1712 * 1713 * Caller must hold RTNL and no other locks 1714 */ 1715 void bond_alb_deinit_slave(struct bonding *bond, struct slave *slave) 1716 { 1717 if (bond_has_slaves(bond)) 1718 alb_change_hw_addr_on_detach(bond, slave); 1719 1720 tlb_clear_slave(bond, slave, 0); 1721 1722 if (bond->alb_info.rlb_enabled) { 1723 bond->alb_info.rx_slave = NULL; 1724 rlb_clear_slave(bond, slave); 1725 } 1726 1727 } 1728 1729 void bond_alb_handle_link_change(struct bonding *bond, struct slave *slave, char link) 1730 { 1731 struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond)); 1732 1733 if (link == BOND_LINK_DOWN) { 1734 tlb_clear_slave(bond, slave, 0); 1735 if (bond->alb_info.rlb_enabled) 1736 rlb_clear_slave(bond, slave); 1737 } else if (link == BOND_LINK_UP) { 1738 /* order a rebalance ASAP */ 1739 atomic_set(&bond_info->tx_rebalance_counter, 1740 BOND_TLB_REBALANCE_TICKS); 1741 if (bond->alb_info.rlb_enabled) { 1742 bond->alb_info.rlb_rebalance = 1; 1743 /* If the updelay module parameter is smaller than the 1744 * forwarding delay of the switch the rebalance will 1745 * not work because the rebalance arp replies will 1746 * not be forwarded to the clients.. 1747 */ 1748 } 1749 } 1750 1751 if (bond_is_nondyn_tlb(bond)) { 1752 if (bond_update_slave_arr(bond, NULL)) 1753 pr_err("Failed to build slave-array for TLB mode.\n"); 1754 } 1755 } 1756 1757 /** 1758 * bond_alb_handle_active_change - assign new curr_active_slave 1759 * @bond: our bonding struct 1760 * @new_slave: new slave to assign 1761 * 1762 * Set the bond->curr_active_slave to @new_slave and handle 1763 * mac address swapping and promiscuity changes as needed. 1764 * 1765 * Caller must hold RTNL 1766 */ 1767 void bond_alb_handle_active_change(struct bonding *bond, struct slave *new_slave) 1768 { 1769 struct slave *swap_slave; 1770 struct slave *curr_active; 1771 1772 curr_active = rtnl_dereference(bond->curr_active_slave); 1773 if (curr_active == new_slave) 1774 return; 1775 1776 if (curr_active && bond->alb_info.primary_is_promisc) { 1777 dev_set_promiscuity(curr_active->dev, -1); 1778 bond->alb_info.primary_is_promisc = 0; 1779 bond->alb_info.rlb_promisc_timeout_counter = 0; 1780 } 1781 1782 swap_slave = curr_active; 1783 rcu_assign_pointer(bond->curr_active_slave, new_slave); 1784 1785 if (!new_slave || !bond_has_slaves(bond)) 1786 return; 1787 1788 /* set the new curr_active_slave to the bonds mac address 1789 * i.e. swap mac addresses of old curr_active_slave and new curr_active_slave 1790 */ 1791 if (!swap_slave) 1792 swap_slave = bond_slave_has_mac(bond, bond->dev->dev_addr); 1793 1794 /* Arrange for swap_slave and new_slave to temporarily be 1795 * ignored so we can mess with their MAC addresses without 1796 * fear of interference from transmit activity. 1797 */ 1798 if (swap_slave) 1799 tlb_clear_slave(bond, swap_slave, 1); 1800 tlb_clear_slave(bond, new_slave, 1); 1801 1802 /* in TLB mode, the slave might flip down/up with the old dev_addr, 1803 * and thus filter bond->dev_addr's packets, so force bond's mac 1804 */ 1805 if (BOND_MODE(bond) == BOND_MODE_TLB) { 1806 struct sockaddr_storage ss; 1807 u8 tmp_addr[MAX_ADDR_LEN]; 1808 1809 bond_hw_addr_copy(tmp_addr, new_slave->dev->dev_addr, 1810 new_slave->dev->addr_len); 1811 1812 bond_hw_addr_copy(ss.__data, bond->dev->dev_addr, 1813 bond->dev->addr_len); 1814 ss.ss_family = bond->dev->type; 1815 /* we don't care if it can't change its mac, best effort */ 1816 dev_set_mac_address(new_slave->dev, &ss, NULL); 1817 1818 dev_addr_set(new_slave->dev, tmp_addr); 1819 } 1820 1821 /* curr_active_slave must be set before calling alb_swap_mac_addr */ 1822 if (swap_slave) { 1823 /* swap mac address */ 1824 alb_swap_mac_addr(swap_slave, new_slave); 1825 alb_fasten_mac_swap(bond, swap_slave, new_slave); 1826 } else { 1827 /* set the new_slave to the bond mac address */ 1828 alb_set_slave_mac_addr(new_slave, bond->dev->dev_addr, 1829 bond->dev->addr_len); 1830 alb_send_learning_packets(new_slave, bond->dev->dev_addr, 1831 false); 1832 } 1833 } 1834 1835 /* Called with RTNL */ 1836 int bond_alb_set_mac_address(struct net_device *bond_dev, void *addr) 1837 { 1838 struct bonding *bond = netdev_priv(bond_dev); 1839 struct sockaddr_storage *ss = addr; 1840 struct slave *curr_active; 1841 struct slave *swap_slave; 1842 int res; 1843 1844 if (!is_valid_ether_addr(ss->__data)) 1845 return -EADDRNOTAVAIL; 1846 1847 res = alb_set_mac_address(bond, addr); 1848 if (res) 1849 return res; 1850 1851 dev_addr_set(bond_dev, ss->__data); 1852 1853 /* If there is no curr_active_slave there is nothing else to do. 1854 * Otherwise we'll need to pass the new address to it and handle 1855 * duplications. 1856 */ 1857 curr_active = rtnl_dereference(bond->curr_active_slave); 1858 if (!curr_active) 1859 return 0; 1860 1861 swap_slave = bond_slave_has_mac(bond, bond_dev->dev_addr); 1862 1863 if (swap_slave) { 1864 alb_swap_mac_addr(swap_slave, curr_active); 1865 alb_fasten_mac_swap(bond, swap_slave, curr_active); 1866 } else { 1867 alb_set_slave_mac_addr(curr_active, bond_dev->dev_addr, 1868 bond_dev->addr_len); 1869 1870 alb_send_learning_packets(curr_active, 1871 bond_dev->dev_addr, false); 1872 if (bond->alb_info.rlb_enabled) { 1873 /* inform clients mac address has changed */ 1874 rlb_req_update_slave_clients(bond, curr_active); 1875 } 1876 } 1877 1878 return 0; 1879 } 1880 1881 void bond_alb_clear_vlan(struct bonding *bond, unsigned short vlan_id) 1882 { 1883 if (bond->alb_info.rlb_enabled) 1884 rlb_clear_vlan(bond, vlan_id); 1885 } 1886 1887