1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
7 #include <rte_common.h>
9 #include <rte_memory.h>
10 #include <rte_malloc.h>
13 #include "rte_table_hash.h"
18 #define KEYS_PER_BUCKET 4
20 #ifdef RTE_TABLE_STATS_COLLECT
22 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(table, val) \
23 table->stats.n_pkts_in += val
24 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(table, val) \
25 table->stats.n_pkts_lookup_miss += val
29 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(table, val)
30 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(table, val)
35 struct rte_bucket_4_8 {
39 struct rte_bucket_4_8 *next;
48 struct rte_bucket_4_8 {
52 struct rte_bucket_4_8 *next;
63 struct rte_table_hash {
64 struct rte_table_stats stats;
66 /* Input parameters */
73 rte_table_hash_op_hash f_hash;
76 /* Extendible buckets */
77 uint32_t n_buckets_ext;
82 uint8_t memory[0] __rte_cache_aligned;
86 keycmp(void *a, void *b, void *b_mask)
88 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
90 return a64[0] != (b64[0] & b_mask64[0]);
94 keycpy(void *dst, void *src, void *src_mask)
96 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
98 dst64[0] = src64[0] & src_mask64[0];
102 check_params_create(struct rte_table_hash_params *params)
105 if (params->name == NULL) {
106 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
111 if (params->key_size != KEY_SIZE) {
112 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
117 if (params->n_keys == 0) {
118 RTE_LOG(ERR, TABLE, "%s: n_keys is zero\n", __func__);
123 if ((params->n_buckets == 0) ||
124 (!rte_is_power_of_2(params->n_buckets))) {
125 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
130 if (params->f_hash == NULL) {
131 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
140 rte_table_hash_create_key8_lru(void *params, int socket_id, uint32_t entry_size)
142 struct rte_table_hash_params *p = params;
143 struct rte_table_hash *f;
144 uint64_t bucket_size, total_size;
145 uint32_t n_buckets, i;
147 /* Check input parameters */
148 if ((check_params_create(p) != 0) ||
149 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
150 ((sizeof(struct rte_bucket_4_8) % 64) != 0))
156 * Objective: Pick the number of buckets (n_buckets) so that there a chance
157 * to store n_keys keys in the table.
159 * Note: Since the buckets do not get extended, it is not possible to
160 * guarantee that n_keys keys can be stored in the table at any time. In the
161 * worst case scenario when all the n_keys fall into the same bucket, only
162 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
163 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
164 * n_keys to n_buckets ratio.
166 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
168 n_buckets = rte_align32pow2(
169 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
170 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
172 /* Memory allocation */
173 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_8) +
174 KEYS_PER_BUCKET * entry_size);
175 total_size = sizeof(struct rte_table_hash) + n_buckets * bucket_size;
177 if (total_size > SIZE_MAX) {
178 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes"
179 " for hash table %s\n",
180 __func__, total_size, p->name);
184 f = rte_zmalloc_socket(p->name,
189 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes"
190 " for hash table %s\n",
191 __func__, total_size, p->name);
195 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
196 "is %" PRIu64 " bytes\n",
197 __func__, p->name, total_size);
199 /* Memory initialization */
200 f->n_buckets = n_buckets;
201 f->key_size = KEY_SIZE;
202 f->entry_size = entry_size;
203 f->bucket_size = bucket_size;
204 f->key_offset = p->key_offset;
205 f->f_hash = p->f_hash;
208 if (p->key_mask != NULL)
209 f->key_mask = ((uint64_t *)p->key_mask)[0];
211 f->key_mask = 0xFFFFFFFFFFFFFFFFLLU;
213 for (i = 0; i < n_buckets; i++) {
214 struct rte_bucket_4_8 *bucket;
216 bucket = (struct rte_bucket_4_8 *) &f->memory[i *
218 bucket->lru_list = 0x0000000100020003LLU;
225 rte_table_hash_free_key8_lru(void *table)
227 struct rte_table_hash *f = table;
229 /* Check input parameters */
231 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
240 rte_table_hash_entry_add_key8_lru(
247 struct rte_table_hash *f = table;
248 struct rte_bucket_4_8 *bucket;
249 uint64_t signature, mask, pos;
250 uint32_t bucket_index, i;
252 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
253 bucket_index = signature & (f->n_buckets - 1);
254 bucket = (struct rte_bucket_4_8 *)
255 &f->memory[bucket_index * f->bucket_size];
257 /* Key is present in the bucket */
258 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
259 uint64_t bucket_signature = bucket->signature;
260 uint64_t *bucket_key = &bucket->key[i];
262 if ((bucket_signature & mask) &&
263 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
264 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
266 memcpy(bucket_data, entry, f->entry_size);
267 lru_update(bucket, i);
269 *entry_ptr = (void *) bucket_data;
274 /* Key is not present in the bucket */
275 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
276 uint64_t bucket_signature = bucket->signature;
278 if ((bucket_signature & mask) == 0) {
279 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
281 bucket->signature |= mask;
282 keycpy(&bucket->key[i], key, &f->key_mask);
283 memcpy(bucket_data, entry, f->entry_size);
284 lru_update(bucket, i);
286 *entry_ptr = (void *) bucket_data;
292 /* Bucket full: replace LRU entry */
293 pos = lru_pos(bucket);
294 keycpy(&bucket->key[pos], key, &f->key_mask);
295 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
296 lru_update(bucket, pos);
298 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
304 rte_table_hash_entry_delete_key8_lru(
310 struct rte_table_hash *f = table;
311 struct rte_bucket_4_8 *bucket;
312 uint64_t signature, mask;
313 uint32_t bucket_index, i;
315 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
316 bucket_index = signature & (f->n_buckets - 1);
317 bucket = (struct rte_bucket_4_8 *)
318 &f->memory[bucket_index * f->bucket_size];
320 /* Key is present in the bucket */
321 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
322 uint64_t bucket_signature = bucket->signature;
323 uint64_t *bucket_key = &bucket->key[i];
325 if ((bucket_signature & mask) &&
326 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
327 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
329 bucket->signature &= ~mask;
332 memcpy(entry, bucket_data, f->entry_size);
338 /* Key is not present in the bucket */
344 rte_table_hash_create_key8_ext(void *params, int socket_id, uint32_t entry_size)
346 struct rte_table_hash_params *p = params;
347 struct rte_table_hash *f;
348 uint64_t bucket_size, stack_size, total_size;
349 uint32_t n_buckets_ext, i;
351 /* Check input parameters */
352 if ((check_params_create(p) != 0) ||
353 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
354 ((sizeof(struct rte_bucket_4_8) % 64) != 0))
360 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
361 * it is guaranteed that n_keys keys can be stored in the table at any time.
363 * The worst case scenario takes place when all the n_keys keys fall into
364 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
365 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
366 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
367 * into a different bucket. This case defeats the purpose of the hash table.
368 * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
370 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
372 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
374 /* Memory allocation */
375 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_8) +
376 KEYS_PER_BUCKET * entry_size);
377 stack_size = RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
378 total_size = sizeof(struct rte_table_hash) +
379 (p->n_buckets + n_buckets_ext) * bucket_size + stack_size;
381 if (total_size > SIZE_MAX) {
382 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
383 "for hash table %s\n",
384 __func__, total_size, p->name);
388 f = rte_zmalloc_socket(p->name,
394 "%s: Cannot allocate %" PRIu64 " bytes "
395 "for hash table %s\n",
396 __func__, total_size, p->name);
399 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
400 "is %" PRIu64 " bytes\n",
401 __func__, p->name, total_size);
403 /* Memory initialization */
404 f->n_buckets = p->n_buckets;
405 f->key_size = KEY_SIZE;
406 f->entry_size = entry_size;
407 f->bucket_size = bucket_size;
408 f->key_offset = p->key_offset;
409 f->f_hash = p->f_hash;
412 f->n_buckets_ext = n_buckets_ext;
413 f->stack_pos = n_buckets_ext;
414 f->stack = (uint32_t *)
415 &f->memory[(p->n_buckets + n_buckets_ext) * f->bucket_size];
417 if (p->key_mask != NULL)
418 f->key_mask = ((uint64_t *)p->key_mask)[0];
420 f->key_mask = 0xFFFFFFFFFFFFFFFFLLU;
422 for (i = 0; i < n_buckets_ext; i++)
429 rte_table_hash_free_key8_ext(void *table)
431 struct rte_table_hash *f = table;
433 /* Check input parameters */
435 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
444 rte_table_hash_entry_add_key8_ext(
451 struct rte_table_hash *f = table;
452 struct rte_bucket_4_8 *bucket0, *bucket, *bucket_prev;
454 uint32_t bucket_index, i;
456 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
457 bucket_index = signature & (f->n_buckets - 1);
458 bucket0 = (struct rte_bucket_4_8 *)
459 &f->memory[bucket_index * f->bucket_size];
461 /* Key is present in the bucket */
462 for (bucket = bucket0; bucket != NULL; bucket = bucket->next) {
465 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
466 uint64_t bucket_signature = bucket->signature;
467 uint64_t *bucket_key = &bucket->key[i];
469 if ((bucket_signature & mask) &&
470 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
471 uint8_t *bucket_data = &bucket->data[i *
474 memcpy(bucket_data, entry, f->entry_size);
476 *entry_ptr = (void *) bucket_data;
482 /* Key is not present in the bucket */
483 for (bucket_prev = NULL, bucket = bucket0;
484 bucket != NULL; bucket_prev = bucket, bucket = bucket->next) {
487 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
488 uint64_t bucket_signature = bucket->signature;
490 if ((bucket_signature & mask) == 0) {
491 uint8_t *bucket_data = &bucket->data[i *
494 bucket->signature |= mask;
495 keycpy(&bucket->key[i], key, &f->key_mask);
496 memcpy(bucket_data, entry, f->entry_size);
498 *entry_ptr = (void *) bucket_data;
505 /* Bucket full: extend bucket */
506 if (f->stack_pos > 0) {
507 bucket_index = f->stack[--f->stack_pos];
509 bucket = (struct rte_bucket_4_8 *) &f->memory[(f->n_buckets +
510 bucket_index) * f->bucket_size];
511 bucket_prev->next = bucket;
512 bucket_prev->next_valid = 1;
514 bucket->signature = 1;
515 keycpy(&bucket->key[0], key, &f->key_mask);
516 memcpy(&bucket->data[0], entry, f->entry_size);
518 *entry_ptr = (void *) &bucket->data[0];
526 rte_table_hash_entry_delete_key8_ext(
532 struct rte_table_hash *f = table;
533 struct rte_bucket_4_8 *bucket0, *bucket, *bucket_prev;
535 uint32_t bucket_index, i;
537 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
538 bucket_index = signature & (f->n_buckets - 1);
539 bucket0 = (struct rte_bucket_4_8 *)
540 &f->memory[bucket_index * f->bucket_size];
542 /* Key is present in the bucket */
543 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
544 bucket_prev = bucket, bucket = bucket->next) {
547 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
548 uint64_t bucket_signature = bucket->signature;
549 uint64_t *bucket_key = &bucket->key[i];
551 if ((bucket_signature & mask) &&
552 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
553 uint8_t *bucket_data = &bucket->data[i *
556 bucket->signature &= ~mask;
559 memcpy(entry, bucket_data,
562 if ((bucket->signature == 0) &&
563 (bucket_prev != NULL)) {
564 bucket_prev->next = bucket->next;
565 bucket_prev->next_valid =
569 sizeof(struct rte_bucket_4_8));
570 bucket_index = (((uint8_t *)bucket -
571 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
572 f->stack[f->stack_pos++] = bucket_index;
580 /* Key is not present in the bucket */
585 #define lookup_key8_cmp(key_in, bucket, pos, f) \
587 uint64_t xor[4], signature, k; \
589 signature = ~bucket->signature; \
591 k = key_in[0] & f->key_mask; \
592 xor[0] = (k ^ bucket->key[0]) | (signature & 1); \
593 xor[1] = (k ^ bucket->key[1]) | (signature & 2); \
594 xor[2] = (k ^ bucket->key[2]) | (signature & 4); \
595 xor[3] = (k ^ bucket->key[3]) | (signature & 8); \
608 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \
611 uint32_t key_offset = f->key_offset;\
613 pkt0_index = __builtin_ctzll(pkts_mask); \
614 pkt_mask = 1LLU << pkt0_index; \
615 pkts_mask &= ~pkt_mask; \
617 mbuf0 = pkts[pkt0_index]; \
618 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset)); \
621 #define lookup1_stage1(mbuf1, bucket1, f) \
624 uint64_t signature; \
625 uint32_t bucket_index; \
627 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset);\
628 signature = f->f_hash(key, &f->key_mask, KEY_SIZE, f->seed); \
629 bucket_index = signature & (f->n_buckets - 1); \
630 bucket1 = (struct rte_bucket_4_8 *) \
631 &f->memory[bucket_index * f->bucket_size]; \
632 rte_prefetch0(bucket1); \
635 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
636 pkts_mask_out, entries, f) \
643 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
644 lookup_key8_cmp(key, bucket2, pos, f); \
646 pkt_mask = ((bucket2->signature >> pos) & 1LLU) << pkt2_index;\
647 pkts_mask_out |= pkt_mask; \
649 a = (void *) &bucket2->data[pos * f->entry_size]; \
651 entries[pkt2_index] = a; \
652 lru_update(bucket2, pos); \
655 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out,\
656 entries, buckets_mask, buckets, keys, f) \
658 struct rte_bucket_4_8 *bucket_next; \
660 uint64_t pkt_mask, bucket_mask; \
664 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
665 lookup_key8_cmp(key, bucket2, pos, f); \
667 pkt_mask = ((bucket2->signature >> pos) & 1LLU) << pkt2_index;\
668 pkts_mask_out |= pkt_mask; \
670 a = (void *) &bucket2->data[pos * f->entry_size]; \
672 entries[pkt2_index] = a; \
674 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
675 buckets_mask |= bucket_mask; \
676 bucket_next = bucket2->next; \
677 buckets[pkt2_index] = bucket_next; \
678 keys[pkt2_index] = key; \
681 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\
684 struct rte_bucket_4_8 *bucket, *bucket_next; \
686 uint64_t pkt_mask, bucket_mask; \
690 bucket = buckets[pkt_index]; \
691 key = keys[pkt_index]; \
692 lookup_key8_cmp(key, bucket, pos, f); \
694 pkt_mask = ((bucket->signature >> pos) & 1LLU) << pkt_index;\
695 pkts_mask_out |= pkt_mask; \
697 a = (void *) &bucket->data[pos * f->entry_size]; \
699 entries[pkt_index] = a; \
701 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
702 buckets_mask |= bucket_mask; \
703 bucket_next = bucket->next; \
704 rte_prefetch0(bucket_next); \
705 buckets[pkt_index] = bucket_next; \
706 keys[pkt_index] = key; \
709 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
710 pkts, pkts_mask, f) \
712 uint64_t pkt00_mask, pkt01_mask; \
713 uint32_t key_offset = f->key_offset; \
715 pkt00_index = __builtin_ctzll(pkts_mask); \
716 pkt00_mask = 1LLU << pkt00_index; \
717 pkts_mask &= ~pkt00_mask; \
719 mbuf00 = pkts[pkt00_index]; \
720 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
722 pkt01_index = __builtin_ctzll(pkts_mask); \
723 pkt01_mask = 1LLU << pkt01_index; \
724 pkts_mask &= ~pkt01_mask; \
726 mbuf01 = pkts[pkt01_index]; \
727 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
730 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
731 mbuf00, mbuf01, pkts, pkts_mask, f) \
733 uint64_t pkt00_mask, pkt01_mask; \
734 uint32_t key_offset = f->key_offset; \
736 pkt00_index = __builtin_ctzll(pkts_mask); \
737 pkt00_mask = 1LLU << pkt00_index; \
738 pkts_mask &= ~pkt00_mask; \
740 mbuf00 = pkts[pkt00_index]; \
741 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
743 pkt01_index = __builtin_ctzll(pkts_mask); \
744 if (pkts_mask == 0) \
745 pkt01_index = pkt00_index; \
747 pkt01_mask = 1LLU << pkt01_index; \
748 pkts_mask &= ~pkt01_mask; \
750 mbuf01 = pkts[pkt01_index]; \
751 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
754 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f)\
756 uint64_t *key10, *key11; \
757 uint64_t signature10, signature11; \
758 uint32_t bucket10_index, bucket11_index; \
759 rte_table_hash_op_hash f_hash = f->f_hash; \
760 uint64_t seed = f->seed; \
761 uint32_t key_offset = f->key_offset; \
763 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, key_offset);\
764 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, key_offset);\
766 signature10 = f_hash(key10, &f->key_mask, KEY_SIZE, seed); \
767 bucket10_index = signature10 & (f->n_buckets - 1); \
768 bucket10 = (struct rte_bucket_4_8 *) \
769 &f->memory[bucket10_index * f->bucket_size]; \
770 rte_prefetch0(bucket10); \
772 signature11 = f_hash(key11, &f->key_mask, KEY_SIZE, seed); \
773 bucket11_index = signature11 & (f->n_buckets - 1); \
774 bucket11 = (struct rte_bucket_4_8 *) \
775 &f->memory[bucket11_index * f->bucket_size]; \
776 rte_prefetch0(bucket11); \
779 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
780 bucket20, bucket21, pkts_mask_out, entries, f) \
783 uint64_t pkt20_mask, pkt21_mask; \
784 uint64_t *key20, *key21; \
785 uint32_t pos20, pos21; \
787 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
788 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
790 lookup_key8_cmp(key20, bucket20, pos20, f); \
791 lookup_key8_cmp(key21, bucket21, pos21, f); \
793 pkt20_mask = ((bucket20->signature >> pos20) & 1LLU) << pkt20_index;\
794 pkt21_mask = ((bucket21->signature >> pos21) & 1LLU) << pkt21_index;\
795 pkts_mask_out |= pkt20_mask | pkt21_mask; \
797 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
798 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
799 rte_prefetch0(a20); \
800 rte_prefetch0(a21); \
801 entries[pkt20_index] = a20; \
802 entries[pkt21_index] = a21; \
803 lru_update(bucket20, pos20); \
804 lru_update(bucket21, pos21); \
807 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
808 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f)\
810 struct rte_bucket_4_8 *bucket20_next, *bucket21_next; \
812 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
813 uint64_t *key20, *key21; \
814 uint32_t pos20, pos21; \
816 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
817 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
819 lookup_key8_cmp(key20, bucket20, pos20, f); \
820 lookup_key8_cmp(key21, bucket21, pos21, f); \
822 pkt20_mask = ((bucket20->signature >> pos20) & 1LLU) << pkt20_index;\
823 pkt21_mask = ((bucket21->signature >> pos21) & 1LLU) << pkt21_index;\
824 pkts_mask_out |= pkt20_mask | pkt21_mask; \
826 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
827 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
828 rte_prefetch0(a20); \
829 rte_prefetch0(a21); \
830 entries[pkt20_index] = a20; \
831 entries[pkt21_index] = a21; \
833 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
834 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
835 buckets_mask |= bucket20_mask | bucket21_mask; \
836 bucket20_next = bucket20->next; \
837 bucket21_next = bucket21->next; \
838 buckets[pkt20_index] = bucket20_next; \
839 buckets[pkt21_index] = bucket21_next; \
840 keys[pkt20_index] = key20; \
841 keys[pkt21_index] = key21; \
845 rte_table_hash_lookup_key8_lru(
847 struct rte_mbuf **pkts,
849 uint64_t *lookup_hit_mask,
852 struct rte_table_hash *f = (struct rte_table_hash *) table;
853 struct rte_bucket_4_8 *bucket10, *bucket11, *bucket20, *bucket21;
854 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
855 uint32_t pkt00_index, pkt01_index, pkt10_index;
856 uint32_t pkt11_index, pkt20_index, pkt21_index;
857 uint64_t pkts_mask_out = 0;
859 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
860 RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(f, n_pkts_in);
862 /* Cannot run the pipeline with less than 5 packets */
863 if (__builtin_popcountll(pkts_mask) < 5) {
864 for ( ; pkts_mask; ) {
865 struct rte_bucket_4_8 *bucket;
866 struct rte_mbuf *mbuf;
869 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
870 lookup1_stage1(mbuf, bucket, f);
871 lookup1_stage2_lru(pkt_index, mbuf, bucket,
872 pkts_mask_out, entries, f);
875 *lookup_hit_mask = pkts_mask_out;
876 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
884 /* Pipeline stage 0 */
885 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
891 pkt10_index = pkt00_index;
892 pkt11_index = pkt01_index;
894 /* Pipeline stage 0 */
895 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
898 /* Pipeline stage 1 */
899 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
905 for ( ; pkts_mask; ) {
913 pkt20_index = pkt10_index;
914 pkt21_index = pkt11_index;
915 pkt10_index = pkt00_index;
916 pkt11_index = pkt01_index;
918 /* Pipeline stage 0 */
919 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
920 mbuf00, mbuf01, pkts, pkts_mask, f);
922 /* Pipeline stage 1 */
923 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
925 /* Pipeline stage 2 */
926 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
927 bucket20, bucket21, pkts_mask_out, entries, f);
941 pkt20_index = pkt10_index;
942 pkt21_index = pkt11_index;
943 pkt10_index = pkt00_index;
944 pkt11_index = pkt01_index;
946 /* Pipeline stage 1 */
947 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
949 /* Pipeline stage 2 */
950 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
951 bucket20, bucket21, pkts_mask_out, entries, f);
958 pkt20_index = pkt10_index;
959 pkt21_index = pkt11_index;
961 /* Pipeline stage 2 */
962 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
963 bucket20, bucket21, pkts_mask_out, entries, f);
965 *lookup_hit_mask = pkts_mask_out;
966 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
971 rte_table_hash_lookup_key8_ext(
973 struct rte_mbuf **pkts,
975 uint64_t *lookup_hit_mask,
978 struct rte_table_hash *f = (struct rte_table_hash *) table;
979 struct rte_bucket_4_8 *bucket10, *bucket11, *bucket20, *bucket21;
980 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
981 uint32_t pkt00_index, pkt01_index, pkt10_index;
982 uint32_t pkt11_index, pkt20_index, pkt21_index;
983 uint64_t pkts_mask_out = 0, buckets_mask = 0;
984 struct rte_bucket_4_8 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
985 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
987 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
988 RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(f, n_pkts_in);
990 /* Cannot run the pipeline with less than 5 packets */
991 if (__builtin_popcountll(pkts_mask) < 5) {
992 for ( ; pkts_mask; ) {
993 struct rte_bucket_4_8 *bucket;
994 struct rte_mbuf *mbuf;
997 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
998 lookup1_stage1(mbuf, bucket, f);
999 lookup1_stage2_ext(pkt_index, mbuf, bucket,
1000 pkts_mask_out, entries, buckets_mask,
1004 goto grind_next_buckets;
1011 /* Pipeline stage 0 */
1012 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1018 pkt10_index = pkt00_index;
1019 pkt11_index = pkt01_index;
1021 /* Pipeline stage 0 */
1022 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1025 /* Pipeline stage 1 */
1026 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1032 for ( ; pkts_mask; ) {
1034 bucket20 = bucket10;
1035 bucket21 = bucket11;
1040 pkt20_index = pkt10_index;
1041 pkt21_index = pkt11_index;
1042 pkt10_index = pkt00_index;
1043 pkt11_index = pkt01_index;
1045 /* Pipeline stage 0 */
1046 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1047 mbuf00, mbuf01, pkts, pkts_mask, f);
1049 /* Pipeline stage 1 */
1050 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1052 /* Pipeline stage 2 */
1053 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1054 bucket20, bucket21, pkts_mask_out, entries,
1055 buckets_mask, buckets, keys, f);
1063 bucket20 = bucket10;
1064 bucket21 = bucket11;
1069 pkt20_index = pkt10_index;
1070 pkt21_index = pkt11_index;
1071 pkt10_index = pkt00_index;
1072 pkt11_index = pkt01_index;
1074 /* Pipeline stage 1 */
1075 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1077 /* Pipeline stage 2 */
1078 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1079 bucket20, bucket21, pkts_mask_out, entries,
1080 buckets_mask, buckets, keys, f);
1083 bucket20 = bucket10;
1084 bucket21 = bucket11;
1087 pkt20_index = pkt10_index;
1088 pkt21_index = pkt11_index;
1090 /* Pipeline stage 2 */
1091 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1092 bucket20, bucket21, pkts_mask_out, entries,
1093 buckets_mask, buckets, keys, f);
1096 /* Grind next buckets */
1097 for ( ; buckets_mask; ) {
1098 uint64_t buckets_mask_next = 0;
1100 for ( ; buckets_mask; ) {
1104 pkt_index = __builtin_ctzll(buckets_mask);
1105 pkt_mask = 1LLU << pkt_index;
1106 buckets_mask &= ~pkt_mask;
1108 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1109 entries, buckets_mask_next, f);
1112 buckets_mask = buckets_mask_next;
1115 *lookup_hit_mask = pkts_mask_out;
1116 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
1121 rte_table_hash_key8_stats_read(void *table, struct rte_table_stats *stats, int clear)
1123 struct rte_table_hash *t = table;
1126 memcpy(stats, &t->stats, sizeof(t->stats));
1129 memset(&t->stats, 0, sizeof(t->stats));
1134 struct rte_table_ops rte_table_hash_key8_lru_ops = {
1135 .f_create = rte_table_hash_create_key8_lru,
1136 .f_free = rte_table_hash_free_key8_lru,
1137 .f_add = rte_table_hash_entry_add_key8_lru,
1138 .f_delete = rte_table_hash_entry_delete_key8_lru,
1140 .f_delete_bulk = NULL,
1141 .f_lookup = rte_table_hash_lookup_key8_lru,
1142 .f_stats = rte_table_hash_key8_stats_read,
1145 struct rte_table_ops rte_table_hash_key8_ext_ops = {
1146 .f_create = rte_table_hash_create_key8_ext,
1147 .f_free = rte_table_hash_free_key8_ext,
1148 .f_add = rte_table_hash_entry_add_key8_ext,
1149 .f_delete = rte_table_hash_entry_delete_key8_ext,
1151 .f_delete_bulk = NULL,
1152 .f_lookup = rte_table_hash_lookup_key8_ext,
1153 .f_stats = rte_table_hash_key8_stats_read,