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