1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2017 Intel Corporation 3 */ 4 5 #include <stdalign.h> 6 #include <stdio.h> 7 #include <string.h> 8 9 #include <rte_common.h> 10 #include <rte_malloc.h> 11 #include <rte_log.h> 12 13 #include "rte_table_hash.h" 14 #include "rte_lru.h" 15 16 #include "table_log.h" 17 18 #define KEY_SIZE 8 19 20 #define KEYS_PER_BUCKET 4 21 22 #ifdef RTE_TABLE_STATS_COLLECT 23 24 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(table, val) \ 25 table->stats.n_pkts_in += val 26 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(table, val) \ 27 table->stats.n_pkts_lookup_miss += val 28 29 #else 30 31 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(table, val) 32 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(table, val) 33 34 #endif 35 36 #ifdef RTE_ARCH_64 37 struct rte_bucket_4_8 { 38 /* Cache line 0 */ 39 uint64_t signature; 40 uint64_t lru_list; 41 struct rte_bucket_4_8 *next; 42 uint64_t next_valid; 43 44 uint64_t key[4]; 45 46 /* Cache line 1 */ 47 uint8_t data[]; 48 }; 49 #else 50 struct rte_bucket_4_8 { 51 /* Cache line 0 */ 52 uint64_t signature; 53 uint64_t lru_list; 54 struct rte_bucket_4_8 *next; 55 uint32_t pad; 56 uint64_t next_valid; 57 58 uint64_t key[4]; 59 60 /* Cache line 1 */ 61 uint8_t data[]; 62 }; 63 #endif 64 65 struct rte_table_hash { 66 struct rte_table_stats stats; 67 68 /* Input parameters */ 69 uint32_t n_buckets; 70 uint32_t key_size; 71 uint32_t entry_size; 72 uint32_t bucket_size; 73 uint32_t key_offset; 74 uint64_t key_mask; 75 rte_table_hash_op_hash f_hash; 76 uint64_t seed; 77 78 /* Extendible buckets */ 79 uint32_t n_buckets_ext; 80 uint32_t stack_pos; 81 uint32_t *stack; 82 83 /* Lookup table */ 84 alignas(RTE_CACHE_LINE_SIZE) uint8_t memory[0]; 85 }; 86 87 static int 88 keycmp(void *a, void *b, void *b_mask) 89 { 90 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask; 91 92 return a64[0] != (b64[0] & b_mask64[0]); 93 } 94 95 static void 96 keycpy(void *dst, void *src, void *src_mask) 97 { 98 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask; 99 100 dst64[0] = src64[0] & src_mask64[0]; 101 } 102 103 static int 104 check_params_create(struct rte_table_hash_params *params) 105 { 106 /* name */ 107 if (params->name == NULL) { 108 TABLE_LOG(ERR, "%s: name invalid value", __func__); 109 return -EINVAL; 110 } 111 112 /* key_size */ 113 if (params->key_size != KEY_SIZE) { 114 TABLE_LOG(ERR, "%s: key_size invalid value", __func__); 115 return -EINVAL; 116 } 117 118 /* n_keys */ 119 if (params->n_keys == 0) { 120 TABLE_LOG(ERR, "%s: n_keys is zero", __func__); 121 return -EINVAL; 122 } 123 124 /* n_buckets */ 125 if ((params->n_buckets == 0) || 126 (!rte_is_power_of_2(params->n_buckets))) { 127 TABLE_LOG(ERR, "%s: n_buckets invalid value", __func__); 128 return -EINVAL; 129 } 130 131 /* f_hash */ 132 if (params->f_hash == NULL) { 133 TABLE_LOG(ERR, "%s: f_hash function pointer is NULL", 134 __func__); 135 return -EINVAL; 136 } 137 138 return 0; 139 } 140 141 static void * 142 rte_table_hash_create_key8_lru(void *params, int socket_id, uint32_t entry_size) 143 { 144 struct rte_table_hash_params *p = params; 145 struct rte_table_hash *f; 146 uint64_t bucket_size, total_size; 147 uint32_t n_buckets, i; 148 149 /* Check input parameters */ 150 if ((check_params_create(p) != 0) || 151 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) || 152 ((sizeof(struct rte_bucket_4_8) % 64) != 0)) 153 return NULL; 154 155 /* 156 * Table dimensioning 157 * 158 * Objective: Pick the number of buckets (n_buckets) so that there a chance 159 * to store n_keys keys in the table. 160 * 161 * Note: Since the buckets do not get extended, it is not possible to 162 * guarantee that n_keys keys can be stored in the table at any time. In the 163 * worst case scenario when all the n_keys fall into the same bucket, only 164 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case 165 * defeats the purpose of the hash table. It indicates unsuitable f_hash or 166 * n_keys to n_buckets ratio. 167 * 168 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET 169 */ 170 n_buckets = rte_align32pow2( 171 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET); 172 n_buckets = RTE_MAX(n_buckets, p->n_buckets); 173 174 /* Memory allocation */ 175 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_8) + 176 KEYS_PER_BUCKET * entry_size); 177 total_size = sizeof(struct rte_table_hash) + n_buckets * bucket_size; 178 179 if (total_size > SIZE_MAX) { 180 TABLE_LOG(ERR, "%s: Cannot allocate %" PRIu64 " bytes" 181 " for hash table %s", 182 __func__, total_size, p->name); 183 return NULL; 184 } 185 186 f = rte_zmalloc_socket(p->name, 187 (size_t)total_size, 188 RTE_CACHE_LINE_SIZE, 189 socket_id); 190 if (f == NULL) { 191 TABLE_LOG(ERR, "%s: Cannot allocate %" PRIu64 " bytes" 192 " for hash table %s", 193 __func__, total_size, p->name); 194 return NULL; 195 } 196 197 TABLE_LOG(INFO, "%s: Hash table %s memory footprint " 198 "is %" PRIu64 " bytes", 199 __func__, p->name, total_size); 200 201 /* Memory initialization */ 202 f->n_buckets = n_buckets; 203 f->key_size = KEY_SIZE; 204 f->entry_size = entry_size; 205 f->bucket_size = bucket_size; 206 f->key_offset = p->key_offset; 207 f->f_hash = p->f_hash; 208 f->seed = p->seed; 209 210 if (p->key_mask != NULL) 211 f->key_mask = ((uint64_t *)p->key_mask)[0]; 212 else 213 f->key_mask = 0xFFFFFFFFFFFFFFFFLLU; 214 215 for (i = 0; i < n_buckets; i++) { 216 struct rte_bucket_4_8 *bucket; 217 218 bucket = (struct rte_bucket_4_8 *) &f->memory[i * 219 f->bucket_size]; 220 bucket->lru_list = 0x0000000100020003LLU; 221 } 222 223 return f; 224 } 225 226 static int 227 rte_table_hash_free_key8_lru(void *table) 228 { 229 struct rte_table_hash *f = table; 230 231 /* Check input parameters */ 232 if (f == NULL) { 233 TABLE_LOG(ERR, "%s: table parameter is NULL", __func__); 234 return -EINVAL; 235 } 236 237 rte_free(f); 238 return 0; 239 } 240 241 static int 242 rte_table_hash_entry_add_key8_lru( 243 void *table, 244 void *key, 245 void *entry, 246 int *key_found, 247 void **entry_ptr) 248 { 249 struct rte_table_hash *f = table; 250 struct rte_bucket_4_8 *bucket; 251 uint64_t signature, mask, pos; 252 uint32_t bucket_index, i; 253 254 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed); 255 bucket_index = signature & (f->n_buckets - 1); 256 bucket = (struct rte_bucket_4_8 *) 257 &f->memory[bucket_index * f->bucket_size]; 258 259 /* Key is present in the bucket */ 260 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) { 261 uint64_t bucket_signature = bucket->signature; 262 uint64_t *bucket_key = &bucket->key[i]; 263 264 if ((bucket_signature & mask) && 265 (keycmp(bucket_key, key, &f->key_mask) == 0)) { 266 uint8_t *bucket_data = &bucket->data[i * f->entry_size]; 267 268 memcpy(bucket_data, entry, f->entry_size); 269 lru_update(bucket, i); 270 *key_found = 1; 271 *entry_ptr = (void *) bucket_data; 272 return 0; 273 } 274 } 275 276 /* Key is not present in the bucket */ 277 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) { 278 uint64_t bucket_signature = bucket->signature; 279 280 if ((bucket_signature & mask) == 0) { 281 uint8_t *bucket_data = &bucket->data[i * f->entry_size]; 282 283 bucket->signature |= mask; 284 keycpy(&bucket->key[i], key, &f->key_mask); 285 memcpy(bucket_data, entry, f->entry_size); 286 lru_update(bucket, i); 287 *key_found = 0; 288 *entry_ptr = (void *) bucket_data; 289 290 return 0; 291 } 292 } 293 294 /* Bucket full: replace LRU entry */ 295 pos = lru_pos(bucket); 296 keycpy(&bucket->key[pos], key, &f->key_mask); 297 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size); 298 lru_update(bucket, pos); 299 *key_found = 0; 300 *entry_ptr = (void *) &bucket->data[pos * f->entry_size]; 301 302 return 0; 303 } 304 305 static int 306 rte_table_hash_entry_delete_key8_lru( 307 void *table, 308 void *key, 309 int *key_found, 310 void *entry) 311 { 312 struct rte_table_hash *f = table; 313 struct rte_bucket_4_8 *bucket; 314 uint64_t signature, mask; 315 uint32_t bucket_index, i; 316 317 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed); 318 bucket_index = signature & (f->n_buckets - 1); 319 bucket = (struct rte_bucket_4_8 *) 320 &f->memory[bucket_index * f->bucket_size]; 321 322 /* Key is present in the bucket */ 323 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) { 324 uint64_t bucket_signature = bucket->signature; 325 uint64_t *bucket_key = &bucket->key[i]; 326 327 if ((bucket_signature & mask) && 328 (keycmp(bucket_key, key, &f->key_mask) == 0)) { 329 uint8_t *bucket_data = &bucket->data[i * f->entry_size]; 330 331 bucket->signature &= ~mask; 332 *key_found = 1; 333 if (entry) 334 memcpy(entry, bucket_data, f->entry_size); 335 336 return 0; 337 } 338 } 339 340 /* Key is not present in the bucket */ 341 *key_found = 0; 342 return 0; 343 } 344 345 static void * 346 rte_table_hash_create_key8_ext(void *params, int socket_id, uint32_t entry_size) 347 { 348 struct rte_table_hash_params *p = params; 349 struct rte_table_hash *f; 350 uint64_t bucket_size, stack_size, total_size; 351 uint32_t n_buckets_ext, i; 352 353 /* Check input parameters */ 354 if ((check_params_create(p) != 0) || 355 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) || 356 ((sizeof(struct rte_bucket_4_8) % 64) != 0)) 357 return NULL; 358 359 /* 360 * Table dimensioning 361 * 362 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that 363 * it is guaranteed that n_keys keys can be stored in the table at any time. 364 * 365 * The worst case scenario takes place when all the n_keys keys fall into 366 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst 367 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the 368 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall 369 * into a different bucket. This case defeats the purpose of the hash table. 370 * It indicates unsuitable f_hash or n_keys to n_buckets ratio. 371 * 372 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1 373 */ 374 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1; 375 376 /* Memory allocation */ 377 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_8) + 378 KEYS_PER_BUCKET * entry_size); 379 stack_size = RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t)); 380 total_size = sizeof(struct rte_table_hash) + 381 (p->n_buckets + n_buckets_ext) * bucket_size + stack_size; 382 383 if (total_size > SIZE_MAX) { 384 TABLE_LOG(ERR, "%s: Cannot allocate %" PRIu64 " bytes " 385 "for hash table %s", 386 __func__, total_size, p->name); 387 return NULL; 388 } 389 390 f = rte_zmalloc_socket(p->name, 391 (size_t)total_size, 392 RTE_CACHE_LINE_SIZE, 393 socket_id); 394 if (f == NULL) { 395 TABLE_LOG(ERR, 396 "%s: Cannot allocate %" PRIu64 " bytes " 397 "for hash table %s", 398 __func__, total_size, p->name); 399 return NULL; 400 } 401 TABLE_LOG(INFO, "%s: Hash table %s memory footprint " 402 "is %" PRIu64 " bytes", 403 __func__, p->name, total_size); 404 405 /* Memory initialization */ 406 f->n_buckets = p->n_buckets; 407 f->key_size = KEY_SIZE; 408 f->entry_size = entry_size; 409 f->bucket_size = bucket_size; 410 f->key_offset = p->key_offset; 411 f->f_hash = p->f_hash; 412 f->seed = p->seed; 413 414 f->n_buckets_ext = n_buckets_ext; 415 f->stack_pos = n_buckets_ext; 416 f->stack = (uint32_t *) 417 &f->memory[(p->n_buckets + n_buckets_ext) * f->bucket_size]; 418 419 if (p->key_mask != NULL) 420 f->key_mask = ((uint64_t *)p->key_mask)[0]; 421 else 422 f->key_mask = 0xFFFFFFFFFFFFFFFFLLU; 423 424 for (i = 0; i < n_buckets_ext; i++) 425 f->stack[i] = i; 426 427 return f; 428 } 429 430 static int 431 rte_table_hash_free_key8_ext(void *table) 432 { 433 struct rte_table_hash *f = table; 434 435 /* Check input parameters */ 436 if (f == NULL) { 437 TABLE_LOG(ERR, "%s: table parameter is NULL", __func__); 438 return -EINVAL; 439 } 440 441 rte_free(f); 442 return 0; 443 } 444 445 static int 446 rte_table_hash_entry_add_key8_ext( 447 void *table, 448 void *key, 449 void *entry, 450 int *key_found, 451 void **entry_ptr) 452 { 453 struct rte_table_hash *f = table; 454 struct rte_bucket_4_8 *bucket0, *bucket, *bucket_prev; 455 uint64_t signature; 456 uint32_t bucket_index, i; 457 458 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed); 459 bucket_index = signature & (f->n_buckets - 1); 460 bucket0 = (struct rte_bucket_4_8 *) 461 &f->memory[bucket_index * f->bucket_size]; 462 463 /* Key is present in the bucket */ 464 for (bucket = bucket0; bucket != NULL; bucket = bucket->next) { 465 uint64_t mask; 466 467 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) { 468 uint64_t bucket_signature = bucket->signature; 469 uint64_t *bucket_key = &bucket->key[i]; 470 471 if ((bucket_signature & mask) && 472 (keycmp(bucket_key, key, &f->key_mask) == 0)) { 473 uint8_t *bucket_data = &bucket->data[i * 474 f->entry_size]; 475 476 memcpy(bucket_data, entry, f->entry_size); 477 *key_found = 1; 478 *entry_ptr = (void *) bucket_data; 479 return 0; 480 } 481 } 482 } 483 484 /* Key is not present in the bucket */ 485 for (bucket_prev = NULL, bucket = bucket0; 486 bucket != NULL; bucket_prev = bucket, bucket = bucket->next) { 487 uint64_t mask; 488 489 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) { 490 uint64_t bucket_signature = bucket->signature; 491 492 if ((bucket_signature & mask) == 0) { 493 uint8_t *bucket_data = &bucket->data[i * 494 f->entry_size]; 495 496 bucket->signature |= mask; 497 keycpy(&bucket->key[i], key, &f->key_mask); 498 memcpy(bucket_data, entry, f->entry_size); 499 *key_found = 0; 500 *entry_ptr = (void *) bucket_data; 501 502 return 0; 503 } 504 } 505 } 506 507 /* Bucket full: extend bucket */ 508 if (f->stack_pos > 0) { 509 bucket_index = f->stack[--f->stack_pos]; 510 511 bucket = (struct rte_bucket_4_8 *) &f->memory[(f->n_buckets + 512 bucket_index) * f->bucket_size]; 513 bucket_prev->next = bucket; 514 bucket_prev->next_valid = 1; 515 516 bucket->signature = 1; 517 keycpy(&bucket->key[0], key, &f->key_mask); 518 memcpy(&bucket->data[0], entry, f->entry_size); 519 *key_found = 0; 520 *entry_ptr = (void *) &bucket->data[0]; 521 return 0; 522 } 523 524 return -ENOSPC; 525 } 526 527 static int 528 rte_table_hash_entry_delete_key8_ext( 529 void *table, 530 void *key, 531 int *key_found, 532 void *entry) 533 { 534 struct rte_table_hash *f = table; 535 struct rte_bucket_4_8 *bucket0, *bucket, *bucket_prev; 536 uint64_t signature; 537 uint32_t bucket_index, i; 538 539 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed); 540 bucket_index = signature & (f->n_buckets - 1); 541 bucket0 = (struct rte_bucket_4_8 *) 542 &f->memory[bucket_index * f->bucket_size]; 543 544 /* Key is present in the bucket */ 545 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL; 546 bucket_prev = bucket, bucket = bucket->next) { 547 uint64_t mask; 548 549 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) { 550 uint64_t bucket_signature = bucket->signature; 551 uint64_t *bucket_key = &bucket->key[i]; 552 553 if ((bucket_signature & mask) && 554 (keycmp(bucket_key, key, &f->key_mask) == 0)) { 555 uint8_t *bucket_data = &bucket->data[i * 556 f->entry_size]; 557 558 bucket->signature &= ~mask; 559 *key_found = 1; 560 if (entry) 561 memcpy(entry, bucket_data, 562 f->entry_size); 563 564 if ((bucket->signature == 0) && 565 (bucket_prev != NULL)) { 566 bucket_prev->next = bucket->next; 567 bucket_prev->next_valid = 568 bucket->next_valid; 569 570 memset(bucket, 0, 571 sizeof(struct rte_bucket_4_8)); 572 bucket_index = (((uint8_t *)bucket - 573 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets; 574 f->stack[f->stack_pos++] = bucket_index; 575 } 576 577 return 0; 578 } 579 } 580 } 581 582 /* Key is not present in the bucket */ 583 *key_found = 0; 584 return 0; 585 } 586 587 #define lookup_key8_cmp(key_in, bucket, pos, f) \ 588 { \ 589 uint64_t xor[4], signature, k; \ 590 \ 591 signature = ~bucket->signature; \ 592 \ 593 k = key_in[0] & f->key_mask; \ 594 xor[0] = (k ^ bucket->key[0]) | (signature & 1); \ 595 xor[1] = (k ^ bucket->key[1]) | (signature & 2); \ 596 xor[2] = (k ^ bucket->key[2]) | (signature & 4); \ 597 xor[3] = (k ^ bucket->key[3]) | (signature & 8); \ 598 \ 599 pos = 4; \ 600 if (xor[0] == 0) \ 601 pos = 0; \ 602 if (xor[1] == 0) \ 603 pos = 1; \ 604 if (xor[2] == 0) \ 605 pos = 2; \ 606 if (xor[3] == 0) \ 607 pos = 3; \ 608 } 609 610 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \ 611 { \ 612 uint64_t pkt_mask; \ 613 uint32_t key_offset = f->key_offset;\ 614 \ 615 pkt0_index = rte_ctz64(pkts_mask); \ 616 pkt_mask = 1LLU << pkt0_index; \ 617 pkts_mask &= ~pkt_mask; \ 618 \ 619 mbuf0 = pkts[pkt0_index]; \ 620 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset)); \ 621 } 622 623 #define lookup1_stage1(mbuf1, bucket1, f) \ 624 { \ 625 uint64_t *key; \ 626 uint64_t signature; \ 627 uint32_t bucket_index; \ 628 \ 629 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset);\ 630 signature = f->f_hash(key, &f->key_mask, KEY_SIZE, f->seed); \ 631 bucket_index = signature & (f->n_buckets - 1); \ 632 bucket1 = (struct rte_bucket_4_8 *) \ 633 &f->memory[bucket_index * f->bucket_size]; \ 634 rte_prefetch0(bucket1); \ 635 } 636 637 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \ 638 pkts_mask_out, entries, f) \ 639 { \ 640 void *a; \ 641 uint64_t pkt_mask; \ 642 uint64_t *key; \ 643 uint32_t pos; \ 644 \ 645 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\ 646 lookup_key8_cmp(key, bucket2, pos, f); \ 647 \ 648 pkt_mask = ((bucket2->signature >> pos) & 1LLU) << pkt2_index;\ 649 pkts_mask_out |= pkt_mask; \ 650 \ 651 a = (void *) &bucket2->data[pos * f->entry_size]; \ 652 rte_prefetch0(a); \ 653 entries[pkt2_index] = a; \ 654 lru_update(bucket2, pos); \ 655 } 656 657 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out,\ 658 entries, buckets_mask, buckets, keys, f) \ 659 { \ 660 struct rte_bucket_4_8 *bucket_next; \ 661 void *a; \ 662 uint64_t pkt_mask, bucket_mask; \ 663 uint64_t *key; \ 664 uint32_t pos; \ 665 \ 666 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\ 667 lookup_key8_cmp(key, bucket2, pos, f); \ 668 \ 669 pkt_mask = ((bucket2->signature >> pos) & 1LLU) << pkt2_index;\ 670 pkts_mask_out |= pkt_mask; \ 671 \ 672 a = (void *) &bucket2->data[pos * f->entry_size]; \ 673 rte_prefetch0(a); \ 674 entries[pkt2_index] = a; \ 675 \ 676 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\ 677 buckets_mask |= bucket_mask; \ 678 bucket_next = bucket2->next; \ 679 buckets[pkt2_index] = bucket_next; \ 680 keys[pkt2_index] = key; \ 681 } 682 683 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\ 684 buckets_mask, f) \ 685 { \ 686 struct rte_bucket_4_8 *bucket, *bucket_next; \ 687 void *a; \ 688 uint64_t pkt_mask, bucket_mask; \ 689 uint64_t *key; \ 690 uint32_t pos; \ 691 \ 692 bucket = buckets[pkt_index]; \ 693 key = keys[pkt_index]; \ 694 lookup_key8_cmp(key, bucket, pos, f); \ 695 \ 696 pkt_mask = ((bucket->signature >> pos) & 1LLU) << pkt_index;\ 697 pkts_mask_out |= pkt_mask; \ 698 \ 699 a = (void *) &bucket->data[pos * f->entry_size]; \ 700 rte_prefetch0(a); \ 701 entries[pkt_index] = a; \ 702 \ 703 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\ 704 buckets_mask |= bucket_mask; \ 705 bucket_next = bucket->next; \ 706 rte_prefetch0(bucket_next); \ 707 buckets[pkt_index] = bucket_next; \ 708 keys[pkt_index] = key; \ 709 } 710 711 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\ 712 pkts, pkts_mask, f) \ 713 { \ 714 uint64_t pkt00_mask, pkt01_mask; \ 715 uint32_t key_offset = f->key_offset; \ 716 \ 717 pkt00_index = rte_ctz64(pkts_mask); \ 718 pkt00_mask = 1LLU << pkt00_index; \ 719 pkts_mask &= ~pkt00_mask; \ 720 \ 721 mbuf00 = pkts[pkt00_index]; \ 722 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\ 723 \ 724 pkt01_index = rte_ctz64(pkts_mask); \ 725 pkt01_mask = 1LLU << pkt01_index; \ 726 pkts_mask &= ~pkt01_mask; \ 727 \ 728 mbuf01 = pkts[pkt01_index]; \ 729 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\ 730 } 731 732 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\ 733 mbuf00, mbuf01, pkts, pkts_mask, f) \ 734 { \ 735 uint64_t pkt00_mask, pkt01_mask; \ 736 uint32_t key_offset = f->key_offset; \ 737 \ 738 pkt00_index = rte_ctz64(pkts_mask); \ 739 pkt00_mask = 1LLU << pkt00_index; \ 740 pkts_mask &= ~pkt00_mask; \ 741 \ 742 mbuf00 = pkts[pkt00_index]; \ 743 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\ 744 \ 745 pkt01_index = rte_ctz64(pkts_mask); \ 746 if (pkts_mask == 0) \ 747 pkt01_index = pkt00_index; \ 748 \ 749 pkt01_mask = 1LLU << pkt01_index; \ 750 pkts_mask &= ~pkt01_mask; \ 751 \ 752 mbuf01 = pkts[pkt01_index]; \ 753 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\ 754 } 755 756 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f)\ 757 { \ 758 uint64_t *key10, *key11; \ 759 uint64_t signature10, signature11; \ 760 uint32_t bucket10_index, bucket11_index; \ 761 rte_table_hash_op_hash f_hash = f->f_hash; \ 762 uint64_t seed = f->seed; \ 763 uint32_t key_offset = f->key_offset; \ 764 \ 765 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, key_offset);\ 766 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, key_offset);\ 767 \ 768 signature10 = f_hash(key10, &f->key_mask, KEY_SIZE, seed); \ 769 bucket10_index = signature10 & (f->n_buckets - 1); \ 770 bucket10 = (struct rte_bucket_4_8 *) \ 771 &f->memory[bucket10_index * f->bucket_size]; \ 772 rte_prefetch0(bucket10); \ 773 \ 774 signature11 = f_hash(key11, &f->key_mask, KEY_SIZE, seed); \ 775 bucket11_index = signature11 & (f->n_buckets - 1); \ 776 bucket11 = (struct rte_bucket_4_8 *) \ 777 &f->memory[bucket11_index * f->bucket_size]; \ 778 rte_prefetch0(bucket11); \ 779 } 780 781 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\ 782 bucket20, bucket21, pkts_mask_out, entries, f) \ 783 { \ 784 void *a20, *a21; \ 785 uint64_t pkt20_mask, pkt21_mask; \ 786 uint64_t *key20, *key21; \ 787 uint32_t pos20, pos21; \ 788 \ 789 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\ 790 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\ 791 \ 792 lookup_key8_cmp(key20, bucket20, pos20, f); \ 793 lookup_key8_cmp(key21, bucket21, pos21, f); \ 794 \ 795 pkt20_mask = ((bucket20->signature >> pos20) & 1LLU) << pkt20_index;\ 796 pkt21_mask = ((bucket21->signature >> pos21) & 1LLU) << pkt21_index;\ 797 pkts_mask_out |= pkt20_mask | pkt21_mask; \ 798 \ 799 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \ 800 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \ 801 rte_prefetch0(a20); \ 802 rte_prefetch0(a21); \ 803 entries[pkt20_index] = a20; \ 804 entries[pkt21_index] = a21; \ 805 lru_update(bucket20, pos20); \ 806 lru_update(bucket21, pos21); \ 807 } 808 809 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \ 810 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f)\ 811 { \ 812 struct rte_bucket_4_8 *bucket20_next, *bucket21_next; \ 813 void *a20, *a21; \ 814 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\ 815 uint64_t *key20, *key21; \ 816 uint32_t pos20, pos21; \ 817 \ 818 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\ 819 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\ 820 \ 821 lookup_key8_cmp(key20, bucket20, pos20, f); \ 822 lookup_key8_cmp(key21, bucket21, pos21, f); \ 823 \ 824 pkt20_mask = ((bucket20->signature >> pos20) & 1LLU) << pkt20_index;\ 825 pkt21_mask = ((bucket21->signature >> pos21) & 1LLU) << pkt21_index;\ 826 pkts_mask_out |= pkt20_mask | pkt21_mask; \ 827 \ 828 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \ 829 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \ 830 rte_prefetch0(a20); \ 831 rte_prefetch0(a21); \ 832 entries[pkt20_index] = a20; \ 833 entries[pkt21_index] = a21; \ 834 \ 835 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\ 836 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\ 837 buckets_mask |= bucket20_mask | bucket21_mask; \ 838 bucket20_next = bucket20->next; \ 839 bucket21_next = bucket21->next; \ 840 buckets[pkt20_index] = bucket20_next; \ 841 buckets[pkt21_index] = bucket21_next; \ 842 keys[pkt20_index] = key20; \ 843 keys[pkt21_index] = key21; \ 844 } 845 846 static int 847 rte_table_hash_lookup_key8_lru( 848 void *table, 849 struct rte_mbuf **pkts, 850 uint64_t pkts_mask, 851 uint64_t *lookup_hit_mask, 852 void **entries) 853 { 854 struct rte_table_hash *f = (struct rte_table_hash *) table; 855 struct rte_bucket_4_8 *bucket10, *bucket11, *bucket20, *bucket21; 856 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21; 857 uint32_t pkt00_index, pkt01_index, pkt10_index; 858 uint32_t pkt11_index, pkt20_index, pkt21_index; 859 uint64_t pkts_mask_out = 0; 860 861 __rte_unused uint32_t n_pkts_in = rte_popcount64(pkts_mask); 862 RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(f, n_pkts_in); 863 864 /* Cannot run the pipeline with less than 5 packets */ 865 if (rte_popcount64(pkts_mask) < 5) { 866 for ( ; pkts_mask; ) { 867 struct rte_bucket_4_8 *bucket; 868 struct rte_mbuf *mbuf; 869 uint32_t pkt_index; 870 871 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f); 872 lookup1_stage1(mbuf, bucket, f); 873 lookup1_stage2_lru(pkt_index, mbuf, bucket, 874 pkts_mask_out, entries, f); 875 } 876 877 *lookup_hit_mask = pkts_mask_out; 878 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - rte_popcount64(pkts_mask_out)); 879 return 0; 880 } 881 882 /* 883 * Pipeline fill 884 * 885 */ 886 /* Pipeline stage 0 */ 887 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts, 888 pkts_mask, f); 889 890 /* Pipeline feed */ 891 mbuf10 = mbuf00; 892 mbuf11 = mbuf01; 893 pkt10_index = pkt00_index; 894 pkt11_index = pkt01_index; 895 896 /* Pipeline stage 0 */ 897 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts, 898 pkts_mask, f); 899 900 /* Pipeline stage 1 */ 901 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f); 902 903 /* 904 * Pipeline run 905 * 906 */ 907 for ( ; pkts_mask; ) { 908 /* Pipeline feed */ 909 bucket20 = bucket10; 910 bucket21 = bucket11; 911 mbuf20 = mbuf10; 912 mbuf21 = mbuf11; 913 mbuf10 = mbuf00; 914 mbuf11 = mbuf01; 915 pkt20_index = pkt10_index; 916 pkt21_index = pkt11_index; 917 pkt10_index = pkt00_index; 918 pkt11_index = pkt01_index; 919 920 /* Pipeline stage 0 */ 921 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index, 922 mbuf00, mbuf01, pkts, pkts_mask, f); 923 924 /* Pipeline stage 1 */ 925 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f); 926 927 /* Pipeline stage 2 */ 928 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21, 929 bucket20, bucket21, pkts_mask_out, entries, f); 930 } 931 932 /* 933 * Pipeline flush 934 * 935 */ 936 /* Pipeline feed */ 937 bucket20 = bucket10; 938 bucket21 = bucket11; 939 mbuf20 = mbuf10; 940 mbuf21 = mbuf11; 941 mbuf10 = mbuf00; 942 mbuf11 = mbuf01; 943 pkt20_index = pkt10_index; 944 pkt21_index = pkt11_index; 945 pkt10_index = pkt00_index; 946 pkt11_index = pkt01_index; 947 948 /* Pipeline stage 1 */ 949 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f); 950 951 /* Pipeline stage 2 */ 952 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21, 953 bucket20, bucket21, pkts_mask_out, entries, f); 954 955 /* Pipeline feed */ 956 bucket20 = bucket10; 957 bucket21 = bucket11; 958 mbuf20 = mbuf10; 959 mbuf21 = mbuf11; 960 pkt20_index = pkt10_index; 961 pkt21_index = pkt11_index; 962 963 /* Pipeline stage 2 */ 964 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21, 965 bucket20, bucket21, pkts_mask_out, entries, f); 966 967 *lookup_hit_mask = pkts_mask_out; 968 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - rte_popcount64(pkts_mask_out)); 969 return 0; 970 } /* lookup LRU */ 971 972 static int 973 rte_table_hash_lookup_key8_ext( 974 void *table, 975 struct rte_mbuf **pkts, 976 uint64_t pkts_mask, 977 uint64_t *lookup_hit_mask, 978 void **entries) 979 { 980 struct rte_table_hash *f = (struct rte_table_hash *) table; 981 struct rte_bucket_4_8 *bucket10, *bucket11, *bucket20, *bucket21; 982 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21; 983 uint32_t pkt00_index, pkt01_index, pkt10_index; 984 uint32_t pkt11_index, pkt20_index, pkt21_index; 985 uint64_t pkts_mask_out = 0, buckets_mask = 0; 986 struct rte_bucket_4_8 *buckets[RTE_PORT_IN_BURST_SIZE_MAX]; 987 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX]; 988 989 __rte_unused uint32_t n_pkts_in = rte_popcount64(pkts_mask); 990 RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(f, n_pkts_in); 991 992 /* Cannot run the pipeline with less than 5 packets */ 993 if (rte_popcount64(pkts_mask) < 5) { 994 for ( ; pkts_mask; ) { 995 struct rte_bucket_4_8 *bucket; 996 struct rte_mbuf *mbuf; 997 uint32_t pkt_index; 998 999 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f); 1000 lookup1_stage1(mbuf, bucket, f); 1001 lookup1_stage2_ext(pkt_index, mbuf, bucket, 1002 pkts_mask_out, entries, buckets_mask, 1003 buckets, keys, f); 1004 } 1005 1006 goto grind_next_buckets; 1007 } 1008 1009 /* 1010 * Pipeline fill 1011 * 1012 */ 1013 /* Pipeline stage 0 */ 1014 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts, 1015 pkts_mask, f); 1016 1017 /* Pipeline feed */ 1018 mbuf10 = mbuf00; 1019 mbuf11 = mbuf01; 1020 pkt10_index = pkt00_index; 1021 pkt11_index = pkt01_index; 1022 1023 /* Pipeline stage 0 */ 1024 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts, 1025 pkts_mask, f); 1026 1027 /* Pipeline stage 1 */ 1028 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f); 1029 1030 /* 1031 * Pipeline run 1032 * 1033 */ 1034 for ( ; pkts_mask; ) { 1035 /* Pipeline feed */ 1036 bucket20 = bucket10; 1037 bucket21 = bucket11; 1038 mbuf20 = mbuf10; 1039 mbuf21 = mbuf11; 1040 mbuf10 = mbuf00; 1041 mbuf11 = mbuf01; 1042 pkt20_index = pkt10_index; 1043 pkt21_index = pkt11_index; 1044 pkt10_index = pkt00_index; 1045 pkt11_index = pkt01_index; 1046 1047 /* Pipeline stage 0 */ 1048 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index, 1049 mbuf00, mbuf01, pkts, pkts_mask, f); 1050 1051 /* Pipeline stage 1 */ 1052 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f); 1053 1054 /* Pipeline stage 2 */ 1055 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, 1056 bucket20, bucket21, pkts_mask_out, entries, 1057 buckets_mask, buckets, keys, f); 1058 } 1059 1060 /* 1061 * Pipeline flush 1062 * 1063 */ 1064 /* Pipeline feed */ 1065 bucket20 = bucket10; 1066 bucket21 = bucket11; 1067 mbuf20 = mbuf10; 1068 mbuf21 = mbuf11; 1069 mbuf10 = mbuf00; 1070 mbuf11 = mbuf01; 1071 pkt20_index = pkt10_index; 1072 pkt21_index = pkt11_index; 1073 pkt10_index = pkt00_index; 1074 pkt11_index = pkt01_index; 1075 1076 /* Pipeline stage 1 */ 1077 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f); 1078 1079 /* Pipeline stage 2 */ 1080 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, 1081 bucket20, bucket21, pkts_mask_out, entries, 1082 buckets_mask, buckets, keys, f); 1083 1084 /* Pipeline feed */ 1085 bucket20 = bucket10; 1086 bucket21 = bucket11; 1087 mbuf20 = mbuf10; 1088 mbuf21 = mbuf11; 1089 pkt20_index = pkt10_index; 1090 pkt21_index = pkt11_index; 1091 1092 /* Pipeline stage 2 */ 1093 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, 1094 bucket20, bucket21, pkts_mask_out, entries, 1095 buckets_mask, buckets, keys, f); 1096 1097 grind_next_buckets: 1098 /* Grind next buckets */ 1099 for ( ; buckets_mask; ) { 1100 uint64_t buckets_mask_next = 0; 1101 1102 for ( ; buckets_mask; ) { 1103 uint64_t pkt_mask; 1104 uint32_t pkt_index; 1105 1106 pkt_index = rte_ctz64(buckets_mask); 1107 pkt_mask = 1LLU << pkt_index; 1108 buckets_mask &= ~pkt_mask; 1109 1110 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, 1111 entries, buckets_mask_next, f); 1112 } 1113 1114 buckets_mask = buckets_mask_next; 1115 } 1116 1117 *lookup_hit_mask = pkts_mask_out; 1118 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - rte_popcount64(pkts_mask_out)); 1119 return 0; 1120 } /* lookup EXT */ 1121 1122 static int 1123 rte_table_hash_key8_stats_read(void *table, struct rte_table_stats *stats, int clear) 1124 { 1125 struct rte_table_hash *t = table; 1126 1127 if (stats != NULL) 1128 memcpy(stats, &t->stats, sizeof(t->stats)); 1129 1130 if (clear) 1131 memset(&t->stats, 0, sizeof(t->stats)); 1132 1133 return 0; 1134 } 1135 1136 struct rte_table_ops rte_table_hash_key8_lru_ops = { 1137 .f_create = rte_table_hash_create_key8_lru, 1138 .f_free = rte_table_hash_free_key8_lru, 1139 .f_add = rte_table_hash_entry_add_key8_lru, 1140 .f_delete = rte_table_hash_entry_delete_key8_lru, 1141 .f_add_bulk = NULL, 1142 .f_delete_bulk = NULL, 1143 .f_lookup = rte_table_hash_lookup_key8_lru, 1144 .f_stats = rte_table_hash_key8_stats_read, 1145 }; 1146 1147 struct rte_table_ops rte_table_hash_key8_ext_ops = { 1148 .f_create = rte_table_hash_create_key8_ext, 1149 .f_free = rte_table_hash_free_key8_ext, 1150 .f_add = rte_table_hash_entry_add_key8_ext, 1151 .f_delete = rte_table_hash_entry_delete_key8_ext, 1152 .f_add_bulk = NULL, 1153 .f_delete_bulk = NULL, 1154 .f_lookup = rte_table_hash_lookup_key8_ext, 1155 .f_stats = rte_table_hash_key8_stats_read, 1156 }; 1157