1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2017 Intel Corporation 3 */ 4 5 #include <string.h> 6 #include <stdio.h> 7 8 #include <rte_common.h> 9 #include <rte_malloc.h> 10 #include <rte_log.h> 11 12 #include "rte_table_hash.h" 13 #include "rte_lru.h" 14 15 #include "table_log.h" 16 17 #define KEYS_PER_BUCKET 4 18 19 #ifdef RTE_TABLE_STATS_COLLECT 20 21 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val) \ 22 table->stats.n_pkts_in += val 23 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val) \ 24 table->stats.n_pkts_lookup_miss += val 25 26 #else 27 28 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val) 29 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val) 30 31 #endif 32 33 struct bucket { 34 union { 35 struct bucket *next; 36 uint64_t lru_list; 37 }; 38 uint16_t sig[KEYS_PER_BUCKET]; 39 uint32_t key_pos[KEYS_PER_BUCKET]; 40 }; 41 42 struct grinder { 43 struct bucket *bkt; 44 uint64_t sig; 45 uint64_t match; 46 uint64_t match_pos; 47 uint32_t key_index; 48 }; 49 50 struct rte_table_hash { 51 struct rte_table_stats stats; 52 53 /* Input parameters */ 54 uint32_t key_size; 55 uint32_t entry_size; 56 uint32_t n_keys; 57 uint32_t n_buckets; 58 rte_table_hash_op_hash f_hash; 59 uint64_t seed; 60 uint32_t key_offset; 61 62 /* Internal */ 63 uint64_t bucket_mask; 64 uint32_t key_size_shl; 65 uint32_t data_size_shl; 66 uint32_t key_stack_tos; 67 68 /* Grinder */ 69 struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX]; 70 71 /* Tables */ 72 uint64_t *key_mask; 73 struct bucket *buckets; 74 uint8_t *key_mem; 75 uint8_t *data_mem; 76 uint32_t *key_stack; 77 78 /* Table memory */ 79 uint8_t memory[0] __rte_cache_aligned; 80 }; 81 82 static int 83 keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes) 84 { 85 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask; 86 uint32_t i; 87 88 for (i = 0; i < n_bytes / sizeof(uint64_t); i++) 89 if (a64[i] != (b64[i] & b_mask64[i])) 90 return 1; 91 92 return 0; 93 } 94 95 static void 96 keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes) 97 { 98 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask; 99 uint32_t i; 100 101 for (i = 0; i < n_bytes / sizeof(uint64_t); i++) 102 dst64[i] = src64[i] & src_mask64[i]; 103 } 104 105 static int 106 check_params_create(struct rte_table_hash_params *params) 107 { 108 /* name */ 109 if (params->name == NULL) { 110 TABLE_LOG(ERR, "%s: name invalid value", __func__); 111 return -EINVAL; 112 } 113 114 /* key_size */ 115 if ((params->key_size < sizeof(uint64_t)) || 116 (!rte_is_power_of_2(params->key_size))) { 117 TABLE_LOG(ERR, "%s: key_size invalid value", __func__); 118 return -EINVAL; 119 } 120 121 /* n_keys */ 122 if (params->n_keys == 0) { 123 TABLE_LOG(ERR, "%s: n_keys invalid value", __func__); 124 return -EINVAL; 125 } 126 127 /* n_buckets */ 128 if ((params->n_buckets == 0) || 129 (!rte_is_power_of_2(params->n_buckets))) { 130 TABLE_LOG(ERR, "%s: n_buckets invalid value", __func__); 131 return -EINVAL; 132 } 133 134 /* f_hash */ 135 if (params->f_hash == NULL) { 136 TABLE_LOG(ERR, "%s: f_hash invalid value", __func__); 137 return -EINVAL; 138 } 139 140 return 0; 141 } 142 143 static void * 144 rte_table_hash_lru_create(void *params, int socket_id, uint32_t entry_size) 145 { 146 struct rte_table_hash_params *p = params; 147 struct rte_table_hash *t; 148 uint64_t table_meta_sz, key_mask_sz, bucket_sz, key_sz, key_stack_sz; 149 uint64_t data_sz, total_size; 150 uint64_t key_mask_offset, bucket_offset, key_offset, key_stack_offset; 151 uint64_t data_offset; 152 uint32_t n_buckets, i; 153 154 /* Check input parameters */ 155 if ((check_params_create(p) != 0) || 156 (!rte_is_power_of_2(entry_size)) || 157 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) || 158 (sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2))) { 159 return NULL; 160 } 161 162 /* 163 * Table dimensioning 164 * 165 * Objective: Pick the number of buckets (n_buckets) so that there a chance 166 * to store n_keys keys in the table. 167 * 168 * Note: Since the buckets do not get extended, it is not possible to 169 * guarantee that n_keys keys can be stored in the table at any time. In the 170 * worst case scenario when all the n_keys fall into the same bucket, only 171 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case 172 * defeats the purpose of the hash table. It indicates unsuitable f_hash or 173 * n_keys to n_buckets ratio. 174 * 175 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET 176 */ 177 n_buckets = rte_align32pow2( 178 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET); 179 n_buckets = RTE_MAX(n_buckets, p->n_buckets); 180 181 /* Memory allocation */ 182 table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash)); 183 key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size); 184 bucket_sz = RTE_CACHE_LINE_ROUNDUP(n_buckets * sizeof(struct bucket)); 185 key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size); 186 key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t)); 187 data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size); 188 total_size = table_meta_sz + key_mask_sz + bucket_sz + key_sz + 189 key_stack_sz + data_sz; 190 191 if (total_size > SIZE_MAX) { 192 TABLE_LOG(ERR, 193 "%s: Cannot allocate %" PRIu64 " bytes for hash " 194 "table %s", 195 __func__, total_size, p->name); 196 return NULL; 197 } 198 199 t = rte_zmalloc_socket(p->name, 200 (size_t)total_size, 201 RTE_CACHE_LINE_SIZE, 202 socket_id); 203 if (t == NULL) { 204 TABLE_LOG(ERR, 205 "%s: Cannot allocate %" PRIu64 " bytes for hash " 206 "table %s", 207 __func__, total_size, p->name); 208 return NULL; 209 } 210 TABLE_LOG(INFO, "%s (%u-byte key): Hash table %s memory footprint" 211 " is %" PRIu64 " bytes", 212 __func__, p->key_size, p->name, total_size); 213 214 /* Memory initialization */ 215 t->key_size = p->key_size; 216 t->entry_size = entry_size; 217 t->n_keys = p->n_keys; 218 t->n_buckets = n_buckets; 219 t->f_hash = p->f_hash; 220 t->seed = p->seed; 221 t->key_offset = p->key_offset; 222 223 /* Internal */ 224 t->bucket_mask = t->n_buckets - 1; 225 t->key_size_shl = rte_ctz32(p->key_size); 226 t->data_size_shl = rte_ctz32(entry_size); 227 228 /* Tables */ 229 key_mask_offset = 0; 230 bucket_offset = key_mask_offset + key_mask_sz; 231 key_offset = bucket_offset + bucket_sz; 232 key_stack_offset = key_offset + key_sz; 233 data_offset = key_stack_offset + key_stack_sz; 234 235 t->key_mask = (uint64_t *) &t->memory[key_mask_offset]; 236 t->buckets = (struct bucket *) &t->memory[bucket_offset]; 237 t->key_mem = &t->memory[key_offset]; 238 t->key_stack = (uint32_t *) &t->memory[key_stack_offset]; 239 t->data_mem = &t->memory[data_offset]; 240 241 /* Key mask */ 242 if (p->key_mask == NULL) 243 memset(t->key_mask, 0xFF, p->key_size); 244 else 245 memcpy(t->key_mask, p->key_mask, p->key_size); 246 247 /* Key stack */ 248 for (i = 0; i < t->n_keys; i++) 249 t->key_stack[i] = t->n_keys - 1 - i; 250 t->key_stack_tos = t->n_keys; 251 252 /* LRU */ 253 for (i = 0; i < t->n_buckets; i++) { 254 struct bucket *bkt = &t->buckets[i]; 255 256 lru_init(bkt); 257 } 258 259 return t; 260 } 261 262 static int 263 rte_table_hash_lru_free(void *table) 264 { 265 struct rte_table_hash *t = table; 266 267 /* Check input parameters */ 268 if (t == NULL) 269 return -EINVAL; 270 271 rte_free(t); 272 return 0; 273 } 274 275 static int 276 rte_table_hash_lru_entry_add(void *table, void *key, void *entry, 277 int *key_found, void **entry_ptr) 278 { 279 struct rte_table_hash *t = table; 280 struct bucket *bkt; 281 uint64_t sig; 282 uint32_t bkt_index, i; 283 284 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed); 285 bkt_index = sig & t->bucket_mask; 286 bkt = &t->buckets[bkt_index]; 287 sig = (sig >> 16) | 1LLU; 288 289 /* Key is present in the bucket */ 290 for (i = 0; i < KEYS_PER_BUCKET; i++) { 291 uint64_t bkt_sig = (uint64_t) bkt->sig[i]; 292 uint32_t bkt_key_index = bkt->key_pos[i]; 293 uint8_t *bkt_key = &t->key_mem[bkt_key_index << 294 t->key_size_shl]; 295 296 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask, 297 t->key_size) == 0)) { 298 uint8_t *data = &t->data_mem[bkt_key_index << 299 t->data_size_shl]; 300 301 memcpy(data, entry, t->entry_size); 302 lru_update(bkt, i); 303 *key_found = 1; 304 *entry_ptr = (void *) data; 305 return 0; 306 } 307 } 308 309 /* Key is not present in the bucket */ 310 for (i = 0; i < KEYS_PER_BUCKET; i++) { 311 uint64_t bkt_sig = (uint64_t) bkt->sig[i]; 312 313 if (bkt_sig == 0) { 314 uint32_t bkt_key_index; 315 uint8_t *bkt_key, *data; 316 317 /* Allocate new key */ 318 if (t->key_stack_tos == 0) { 319 /* No keys available */ 320 return -ENOSPC; 321 } 322 bkt_key_index = t->key_stack[--t->key_stack_tos]; 323 324 /* Install new key */ 325 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl]; 326 data = &t->data_mem[bkt_key_index << t->data_size_shl]; 327 328 bkt->sig[i] = (uint16_t) sig; 329 bkt->key_pos[i] = bkt_key_index; 330 keycpy(bkt_key, key, t->key_mask, t->key_size); 331 memcpy(data, entry, t->entry_size); 332 lru_update(bkt, i); 333 334 *key_found = 0; 335 *entry_ptr = (void *) data; 336 return 0; 337 } 338 } 339 340 /* Bucket full */ 341 { 342 uint64_t pos = lru_pos(bkt); 343 uint32_t bkt_key_index = bkt->key_pos[pos]; 344 uint8_t *bkt_key = &t->key_mem[bkt_key_index << 345 t->key_size_shl]; 346 uint8_t *data = &t->data_mem[bkt_key_index << t->data_size_shl]; 347 348 bkt->sig[pos] = (uint16_t) sig; 349 keycpy(bkt_key, key, t->key_mask, t->key_size); 350 memcpy(data, entry, t->entry_size); 351 lru_update(bkt, pos); 352 353 *key_found = 0; 354 *entry_ptr = (void *) data; 355 return 0; 356 } 357 } 358 359 static int 360 rte_table_hash_lru_entry_delete(void *table, void *key, int *key_found, 361 void *entry) 362 { 363 struct rte_table_hash *t = table; 364 struct bucket *bkt; 365 uint64_t sig; 366 uint32_t bkt_index, i; 367 368 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed); 369 bkt_index = sig & t->bucket_mask; 370 bkt = &t->buckets[bkt_index]; 371 sig = (sig >> 16) | 1LLU; 372 373 /* Key is present in the bucket */ 374 for (i = 0; i < KEYS_PER_BUCKET; i++) { 375 uint64_t bkt_sig = (uint64_t) bkt->sig[i]; 376 uint32_t bkt_key_index = bkt->key_pos[i]; 377 uint8_t *bkt_key = &t->key_mem[bkt_key_index << 378 t->key_size_shl]; 379 380 if ((sig == bkt_sig) && 381 (keycmp(bkt_key, key, t->key_mask, t->key_size) == 0)) { 382 uint8_t *data = &t->data_mem[bkt_key_index << 383 t->data_size_shl]; 384 385 bkt->sig[i] = 0; 386 t->key_stack[t->key_stack_tos++] = bkt_key_index; 387 *key_found = 1; 388 if (entry) 389 memcpy(entry, data, t->entry_size); 390 return 0; 391 } 392 } 393 394 /* Key is not present in the bucket */ 395 *key_found = 0; 396 return 0; 397 } 398 399 static int rte_table_hash_lru_lookup_unoptimized( 400 void *table, 401 struct rte_mbuf **pkts, 402 uint64_t pkts_mask, 403 uint64_t *lookup_hit_mask, 404 void **entries) 405 { 406 struct rte_table_hash *t = (struct rte_table_hash *) table; 407 uint64_t pkts_mask_out = 0; 408 409 __rte_unused uint32_t n_pkts_in = rte_popcount64(pkts_mask); 410 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in); 411 412 for ( ; pkts_mask; ) { 413 struct bucket *bkt; 414 struct rte_mbuf *pkt; 415 uint8_t *key; 416 uint64_t pkt_mask, sig; 417 uint32_t pkt_index, bkt_index, i; 418 419 pkt_index = rte_ctz64(pkts_mask); 420 pkt_mask = 1LLU << pkt_index; 421 pkts_mask &= ~pkt_mask; 422 423 pkt = pkts[pkt_index]; 424 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset); 425 sig = (uint64_t) t->f_hash(key, t->key_mask, t->key_size, t->seed); 426 427 bkt_index = sig & t->bucket_mask; 428 bkt = &t->buckets[bkt_index]; 429 sig = (sig >> 16) | 1LLU; 430 431 /* Key is present in the bucket */ 432 for (i = 0; i < KEYS_PER_BUCKET; i++) { 433 uint64_t bkt_sig = (uint64_t) bkt->sig[i]; 434 uint32_t bkt_key_index = bkt->key_pos[i]; 435 uint8_t *bkt_key = &t->key_mem[bkt_key_index << 436 t->key_size_shl]; 437 438 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask, 439 t->key_size) == 0)) { 440 uint8_t *data = &t->data_mem[bkt_key_index << 441 t->data_size_shl]; 442 443 lru_update(bkt, i); 444 pkts_mask_out |= pkt_mask; 445 entries[pkt_index] = (void *) data; 446 break; 447 } 448 } 449 } 450 451 *lookup_hit_mask = pkts_mask_out; 452 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - rte_popcount64(pkts_mask_out)); 453 return 0; 454 } 455 456 /* 457 * mask = match bitmask 458 * match = at least one match 459 * match_many = more than one match 460 * match_pos = position of first match 461 * 462 * ---------------------------------------- 463 * mask match match_many match_pos 464 * ---------------------------------------- 465 * 0000 0 0 00 466 * 0001 1 0 00 467 * 0010 1 0 01 468 * 0011 1 1 00 469 * ---------------------------------------- 470 * 0100 1 0 10 471 * 0101 1 1 00 472 * 0110 1 1 01 473 * 0111 1 1 00 474 * ---------------------------------------- 475 * 1000 1 0 11 476 * 1001 1 1 00 477 * 1010 1 1 01 478 * 1011 1 1 00 479 * ---------------------------------------- 480 * 1100 1 1 10 481 * 1101 1 1 00 482 * 1110 1 1 01 483 * 1111 1 1 00 484 * ---------------------------------------- 485 * 486 * match = 1111_1111_1111_1110 487 * match_many = 1111_1110_1110_1000 488 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000 489 * 490 * match = 0xFFFELLU 491 * match_many = 0xFEE8LLU 492 * match_pos = 0x12131210LLU 493 */ 494 495 #define LUT_MATCH 0xFFFELLU 496 #define LUT_MATCH_MANY 0xFEE8LLU 497 #define LUT_MATCH_POS 0x12131210LLU 498 499 #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos)\ 500 { \ 501 uint64_t bucket_sig[4], mask[4], mask_all; \ 502 \ 503 bucket_sig[0] = bucket->sig[0]; \ 504 bucket_sig[1] = bucket->sig[1]; \ 505 bucket_sig[2] = bucket->sig[2]; \ 506 bucket_sig[3] = bucket->sig[3]; \ 507 \ 508 bucket_sig[0] ^= mbuf_sig; \ 509 bucket_sig[1] ^= mbuf_sig; \ 510 bucket_sig[2] ^= mbuf_sig; \ 511 bucket_sig[3] ^= mbuf_sig; \ 512 \ 513 mask[0] = 0; \ 514 mask[1] = 0; \ 515 mask[2] = 0; \ 516 mask[3] = 0; \ 517 \ 518 if (bucket_sig[0] == 0) \ 519 mask[0] = 1; \ 520 if (bucket_sig[1] == 0) \ 521 mask[1] = 2; \ 522 if (bucket_sig[2] == 0) \ 523 mask[2] = 4; \ 524 if (bucket_sig[3] == 0) \ 525 mask[3] = 8; \ 526 \ 527 mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \ 528 \ 529 match = (LUT_MATCH >> mask_all) & 1; \ 530 match_many = (LUT_MATCH_MANY >> mask_all) & 1; \ 531 match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \ 532 } 533 534 #define lookup_cmp_key(mbuf, key, match_key, f) \ 535 { \ 536 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\ 537 uint64_t *bkt_key = (uint64_t *) key; \ 538 uint64_t *key_mask = f->key_mask; \ 539 \ 540 switch (f->key_size) { \ 541 case 8: \ 542 { \ 543 uint64_t xor = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \ 544 match_key = 0; \ 545 if (xor == 0) \ 546 match_key = 1; \ 547 } \ 548 break; \ 549 \ 550 case 16: \ 551 { \ 552 uint64_t xor[2], or; \ 553 \ 554 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \ 555 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \ 556 or = xor[0] | xor[1]; \ 557 match_key = 0; \ 558 if (or == 0) \ 559 match_key = 1; \ 560 } \ 561 break; \ 562 \ 563 case 32: \ 564 { \ 565 uint64_t xor[4], or; \ 566 \ 567 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \ 568 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \ 569 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \ 570 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \ 571 or = xor[0] | xor[1] | xor[2] | xor[3]; \ 572 match_key = 0; \ 573 if (or == 0) \ 574 match_key = 1; \ 575 } \ 576 break; \ 577 \ 578 case 64: \ 579 { \ 580 uint64_t xor[8], or; \ 581 \ 582 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \ 583 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \ 584 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \ 585 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \ 586 xor[4] = (pkt_key[4] & key_mask[4]) ^ bkt_key[4]; \ 587 xor[5] = (pkt_key[5] & key_mask[5]) ^ bkt_key[5]; \ 588 xor[6] = (pkt_key[6] & key_mask[6]) ^ bkt_key[6]; \ 589 xor[7] = (pkt_key[7] & key_mask[7]) ^ bkt_key[7]; \ 590 or = xor[0] | xor[1] | xor[2] | xor[3] | \ 591 xor[4] | xor[5] | xor[6] | xor[7]; \ 592 match_key = 0; \ 593 if (or == 0) \ 594 match_key = 1; \ 595 } \ 596 break; \ 597 \ 598 default: \ 599 match_key = 0; \ 600 if (keycmp(bkt_key, pkt_key, key_mask, f->key_size) == 0) \ 601 match_key = 1; \ 602 } \ 603 } 604 605 #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index)\ 606 { \ 607 uint64_t pkt00_mask, pkt01_mask; \ 608 struct rte_mbuf *mbuf00, *mbuf01; \ 609 uint32_t key_offset = t->key_offset; \ 610 \ 611 pkt00_index = rte_ctz64(pkts_mask); \ 612 pkt00_mask = 1LLU << pkt00_index; \ 613 pkts_mask &= ~pkt00_mask; \ 614 mbuf00 = pkts[pkt00_index]; \ 615 \ 616 pkt01_index = rte_ctz64(pkts_mask); \ 617 pkt01_mask = 1LLU << pkt01_index; \ 618 pkts_mask &= ~pkt01_mask; \ 619 mbuf01 = pkts[pkt01_index]; \ 620 \ 621 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\ 622 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\ 623 } 624 625 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \ 626 pkt01_index) \ 627 { \ 628 uint64_t pkt00_mask, pkt01_mask; \ 629 struct rte_mbuf *mbuf00, *mbuf01; \ 630 uint32_t key_offset = t->key_offset; \ 631 \ 632 pkt00_index = rte_ctz64(pkts_mask); \ 633 pkt00_mask = 1LLU << pkt00_index; \ 634 pkts_mask &= ~pkt00_mask; \ 635 mbuf00 = pkts[pkt00_index]; \ 636 \ 637 pkt01_index = rte_ctz64(pkts_mask); \ 638 if (pkts_mask == 0) \ 639 pkt01_index = pkt00_index; \ 640 \ 641 pkt01_mask = 1LLU << pkt01_index; \ 642 pkts_mask &= ~pkt01_mask; \ 643 mbuf01 = pkts[pkt01_index]; \ 644 \ 645 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\ 646 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\ 647 } 648 649 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index)\ 650 { \ 651 struct grinder *g10, *g11; \ 652 uint64_t sig10, sig11, bkt10_index, bkt11_index; \ 653 struct rte_mbuf *mbuf10, *mbuf11; \ 654 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \ 655 uint8_t *key10, *key11; \ 656 uint64_t bucket_mask = t->bucket_mask; \ 657 rte_table_hash_op_hash f_hash = t->f_hash; \ 658 uint64_t seed = t->seed; \ 659 uint32_t key_size = t->key_size; \ 660 uint32_t key_offset = t->key_offset; \ 661 \ 662 mbuf10 = pkts[pkt10_index]; \ 663 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset);\ 664 sig10 = (uint64_t) f_hash(key10, t->key_mask, key_size, seed);\ 665 bkt10_index = sig10 & bucket_mask; \ 666 bkt10 = &buckets[bkt10_index]; \ 667 \ 668 mbuf11 = pkts[pkt11_index]; \ 669 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset);\ 670 sig11 = (uint64_t) f_hash(key11, t->key_mask, key_size, seed);\ 671 bkt11_index = sig11 & bucket_mask; \ 672 bkt11 = &buckets[bkt11_index]; \ 673 \ 674 rte_prefetch0(bkt10); \ 675 rte_prefetch0(bkt11); \ 676 \ 677 g10 = &g[pkt10_index]; \ 678 g10->sig = sig10; \ 679 g10->bkt = bkt10; \ 680 \ 681 g11 = &g[pkt11_index]; \ 682 g11->sig = sig11; \ 683 g11->bkt = bkt11; \ 684 } 685 686 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\ 687 { \ 688 struct grinder *g20, *g21; \ 689 uint64_t sig20, sig21; \ 690 struct bucket *bkt20, *bkt21; \ 691 uint8_t *key20, *key21, *key_mem = t->key_mem; \ 692 uint64_t match20, match21, match_many20, match_many21; \ 693 uint64_t match_pos20, match_pos21; \ 694 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\ 695 \ 696 g20 = &g[pkt20_index]; \ 697 sig20 = g20->sig; \ 698 bkt20 = g20->bkt; \ 699 sig20 = (sig20 >> 16) | 1LLU; \ 700 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\ 701 match20 <<= pkt20_index; \ 702 match_many20 <<= pkt20_index; \ 703 key20_index = bkt20->key_pos[match_pos20]; \ 704 key20 = &key_mem[key20_index << key_size_shl]; \ 705 \ 706 g21 = &g[pkt21_index]; \ 707 sig21 = g21->sig; \ 708 bkt21 = g21->bkt; \ 709 sig21 = (sig21 >> 16) | 1LLU; \ 710 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\ 711 match21 <<= pkt21_index; \ 712 match_many21 <<= pkt21_index; \ 713 key21_index = bkt21->key_pos[match_pos21]; \ 714 key21 = &key_mem[key21_index << key_size_shl]; \ 715 \ 716 rte_prefetch0(key20); \ 717 rte_prefetch0(key21); \ 718 \ 719 pkts_mask_match_many |= match_many20 | match_many21; \ 720 \ 721 g20->match = match20; \ 722 g20->match_pos = match_pos20; \ 723 g20->key_index = key20_index; \ 724 \ 725 g21->match = match21; \ 726 g21->match_pos = match_pos21; \ 727 g21->key_index = key21_index; \ 728 } 729 730 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \ 731 entries) \ 732 { \ 733 struct grinder *g30, *g31; \ 734 struct rte_mbuf *mbuf30, *mbuf31; \ 735 struct bucket *bkt30, *bkt31; \ 736 uint8_t *key30, *key31, *key_mem = t->key_mem; \ 737 uint8_t *data30, *data31, *data_mem = t->data_mem; \ 738 uint64_t match30, match31, match_pos30, match_pos31; \ 739 uint64_t match_key30, match_key31, match_keys; \ 740 uint32_t key30_index, key31_index; \ 741 uint32_t key_size_shl = t->key_size_shl; \ 742 uint32_t data_size_shl = t->data_size_shl; \ 743 \ 744 mbuf30 = pkts[pkt30_index]; \ 745 g30 = &g[pkt30_index]; \ 746 bkt30 = g30->bkt; \ 747 match30 = g30->match; \ 748 match_pos30 = g30->match_pos; \ 749 key30_index = g30->key_index; \ 750 key30 = &key_mem[key30_index << key_size_shl]; \ 751 lookup_cmp_key(mbuf30, key30, match_key30, t); \ 752 match_key30 <<= pkt30_index; \ 753 match_key30 &= match30; \ 754 data30 = &data_mem[key30_index << data_size_shl]; \ 755 entries[pkt30_index] = data30; \ 756 \ 757 mbuf31 = pkts[pkt31_index]; \ 758 g31 = &g[pkt31_index]; \ 759 bkt31 = g31->bkt; \ 760 match31 = g31->match; \ 761 match_pos31 = g31->match_pos; \ 762 key31_index = g31->key_index; \ 763 key31 = &key_mem[key31_index << key_size_shl]; \ 764 lookup_cmp_key(mbuf31, key31, match_key31, t); \ 765 match_key31 <<= pkt31_index; \ 766 match_key31 &= match31; \ 767 data31 = &data_mem[key31_index << data_size_shl]; \ 768 entries[pkt31_index] = data31; \ 769 \ 770 rte_prefetch0(data30); \ 771 rte_prefetch0(data31); \ 772 \ 773 match_keys = match_key30 | match_key31; \ 774 pkts_mask_out |= match_keys; \ 775 \ 776 if (match_key30 == 0) \ 777 match_pos30 = 4; \ 778 lru_update(bkt30, match_pos30); \ 779 \ 780 if (match_key31 == 0) \ 781 match_pos31 = 4; \ 782 lru_update(bkt31, match_pos31); \ 783 } 784 785 /* 786 * The lookup function implements a 4-stage pipeline, with each stage processing 787 * two different packets. The purpose of pipelined implementation is to hide the 788 * latency of prefetching the data structures and loosen the data dependency 789 * between instructions. 790 * 791 * p00 _______ p10 _______ p20 _______ p30 _______ 792 * ----->| |----->| |----->| |----->| |-----> 793 * | 0 | | 1 | | 2 | | 3 | 794 * ----->|_______|----->|_______|----->|_______|----->|_______|-----> 795 * p01 p11 p21 p31 796 * 797 * The naming convention is: 798 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1 799 */ 800 static int rte_table_hash_lru_lookup( 801 void *table, 802 struct rte_mbuf **pkts, 803 uint64_t pkts_mask, 804 uint64_t *lookup_hit_mask, 805 void **entries) 806 { 807 struct rte_table_hash *t = (struct rte_table_hash *) table; 808 struct grinder *g = t->grinders; 809 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index; 810 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index; 811 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0; 812 int status = 0; 813 814 __rte_unused uint32_t n_pkts_in = rte_popcount64(pkts_mask); 815 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in); 816 817 /* Cannot run the pipeline with less than 7 packets */ 818 if (rte_popcount64(pkts_mask) < 7) 819 return rte_table_hash_lru_lookup_unoptimized(table, pkts, 820 pkts_mask, lookup_hit_mask, entries); 821 822 /* Pipeline stage 0 */ 823 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index); 824 825 /* Pipeline feed */ 826 pkt10_index = pkt00_index; 827 pkt11_index = pkt01_index; 828 829 /* Pipeline stage 0 */ 830 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index); 831 832 /* Pipeline stage 1 */ 833 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index); 834 835 /* Pipeline feed */ 836 pkt20_index = pkt10_index; 837 pkt21_index = pkt11_index; 838 pkt10_index = pkt00_index; 839 pkt11_index = pkt01_index; 840 841 /* Pipeline stage 0 */ 842 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index); 843 844 /* Pipeline stage 1 */ 845 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index); 846 847 /* Pipeline stage 2 */ 848 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many); 849 850 /* 851 * Pipeline run 852 * 853 */ 854 for ( ; pkts_mask; ) { 855 /* Pipeline feed */ 856 pkt30_index = pkt20_index; 857 pkt31_index = pkt21_index; 858 pkt20_index = pkt10_index; 859 pkt21_index = pkt11_index; 860 pkt10_index = pkt00_index; 861 pkt11_index = pkt01_index; 862 863 /* Pipeline stage 0 */ 864 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, 865 pkt00_index, pkt01_index); 866 867 /* Pipeline stage 1 */ 868 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index); 869 870 /* Pipeline stage 2 */ 871 lookup2_stage2(t, g, pkt20_index, pkt21_index, 872 pkts_mask_match_many); 873 874 /* Pipeline stage 3 */ 875 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, 876 pkts_mask_out, entries); 877 } 878 879 /* Pipeline feed */ 880 pkt30_index = pkt20_index; 881 pkt31_index = pkt21_index; 882 pkt20_index = pkt10_index; 883 pkt21_index = pkt11_index; 884 pkt10_index = pkt00_index; 885 pkt11_index = pkt01_index; 886 887 /* Pipeline stage 1 */ 888 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index); 889 890 /* Pipeline stage 2 */ 891 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many); 892 893 /* Pipeline stage 3 */ 894 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, 895 entries); 896 897 /* Pipeline feed */ 898 pkt30_index = pkt20_index; 899 pkt31_index = pkt21_index; 900 pkt20_index = pkt10_index; 901 pkt21_index = pkt11_index; 902 903 /* Pipeline stage 2 */ 904 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many); 905 906 /* Pipeline stage 3 */ 907 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, 908 entries); 909 910 /* Pipeline feed */ 911 pkt30_index = pkt20_index; 912 pkt31_index = pkt21_index; 913 914 /* Pipeline stage 3 */ 915 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, 916 entries); 917 918 /* Slow path */ 919 pkts_mask_match_many &= ~pkts_mask_out; 920 if (pkts_mask_match_many) { 921 uint64_t pkts_mask_out_slow = 0; 922 923 status = rte_table_hash_lru_lookup_unoptimized(table, pkts, 924 pkts_mask_match_many, &pkts_mask_out_slow, entries); 925 pkts_mask_out |= pkts_mask_out_slow; 926 } 927 928 *lookup_hit_mask = pkts_mask_out; 929 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - rte_popcount64(pkts_mask_out)); 930 return status; 931 } 932 933 static int 934 rte_table_hash_lru_stats_read(void *table, struct rte_table_stats *stats, int clear) 935 { 936 struct rte_table_hash *t = table; 937 938 if (stats != NULL) 939 memcpy(stats, &t->stats, sizeof(t->stats)); 940 941 if (clear) 942 memset(&t->stats, 0, sizeof(t->stats)); 943 944 return 0; 945 } 946 947 struct rte_table_ops rte_table_hash_lru_ops = { 948 .f_create = rte_table_hash_lru_create, 949 .f_free = rte_table_hash_lru_free, 950 .f_add = rte_table_hash_lru_entry_add, 951 .f_delete = rte_table_hash_lru_entry_delete, 952 .f_add_bulk = NULL, 953 .f_delete_bulk = NULL, 954 .f_lookup = rte_table_hash_lru_lookup, 955 .f_stats = rte_table_hash_lru_stats_read, 956 }; 957