4 * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_common.h>
38 #include <rte_memory.h>
39 #include <rte_malloc.h>
42 #include "rte_table_hash.h"
47 #define KEYS_PER_BUCKET 4
49 #ifdef RTE_TABLE_STATS_COLLECT
51 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(table, val) \
52 table->stats.n_pkts_in += val
53 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(table, val) \
54 table->stats.n_pkts_lookup_miss += val
58 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(table, val)
59 #define RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(table, val)
63 struct rte_bucket_4_8 {
67 struct rte_bucket_4_8 *next;
76 struct rte_table_hash {
77 struct rte_table_stats stats;
79 /* Input parameters */
86 rte_table_hash_op_hash f_hash;
89 /* Extendible buckets */
90 uint32_t n_buckets_ext;
95 uint8_t memory[0] __rte_cache_aligned;
99 keycmp(void *a, void *b, void *b_mask)
101 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
103 return a64[0] != (b64[0] & b_mask64[0]);
107 keycpy(void *dst, void *src, void *src_mask)
109 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
111 dst64[0] = src64[0] & src_mask64[0];
115 check_params_create(struct rte_table_hash_params *params)
118 if (params->name == NULL) {
119 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
124 if (params->key_size != KEY_SIZE) {
125 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
130 if (params->n_keys == 0) {
131 RTE_LOG(ERR, TABLE, "%s: n_keys is zero\n", __func__);
136 if ((params->n_buckets == 0) ||
137 (!rte_is_power_of_2(params->n_buckets))) {
138 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
143 if (params->f_hash == NULL) {
144 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
153 rte_table_hash_create_key8_lru(void *params, int socket_id, uint32_t entry_size)
155 struct rte_table_hash_params *p = params;
156 struct rte_table_hash *f;
157 uint64_t bucket_size, total_size;
158 uint32_t n_buckets, i;
160 /* Check input parameters */
161 if ((check_params_create(p) != 0) ||
162 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
163 ((sizeof(struct rte_bucket_4_8) % 64) != 0))
169 * Objective: Pick the number of buckets (n_buckets) so that there a chance
170 * to store n_keys keys in the table.
172 * Note: Since the buckets do not get extended, it is not possible to
173 * guarantee that n_keys keys can be stored in the table at any time. In the
174 * worst case scenario when all the n_keys fall into the same bucket, only
175 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
176 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
177 * n_keys to n_buckets ratio.
179 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
181 n_buckets = rte_align32pow2(
182 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
183 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
185 /* Memory allocation */
186 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_8) +
187 KEYS_PER_BUCKET * entry_size);
188 total_size = sizeof(struct rte_table_hash) + n_buckets * bucket_size;
190 if (total_size > SIZE_MAX) {
191 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes"
192 " for hash table %s\n",
193 __func__, total_size, p->name);
197 f = rte_zmalloc_socket(p->name,
202 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes"
203 " for hash table %s\n",
204 __func__, total_size, p->name);
208 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
209 "is %" PRIu64 " bytes\n",
210 __func__, p->name, total_size);
212 /* Memory initialization */
213 f->n_buckets = n_buckets;
214 f->key_size = KEY_SIZE;
215 f->entry_size = entry_size;
216 f->bucket_size = bucket_size;
217 f->key_offset = p->key_offset;
218 f->f_hash = p->f_hash;
221 if (p->key_mask != NULL)
222 f->key_mask = ((uint64_t *)p->key_mask)[0];
224 f->key_mask = 0xFFFFFFFFFFFFFFFFLLU;
226 for (i = 0; i < n_buckets; i++) {
227 struct rte_bucket_4_8 *bucket;
229 bucket = (struct rte_bucket_4_8 *) &f->memory[i *
231 bucket->lru_list = 0x0000000100020003LLU;
238 rte_table_hash_free_key8_lru(void *table)
240 struct rte_table_hash *f = table;
242 /* Check input parameters */
244 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
253 rte_table_hash_entry_add_key8_lru(
260 struct rte_table_hash *f = table;
261 struct rte_bucket_4_8 *bucket;
262 uint64_t signature, mask, pos;
263 uint32_t bucket_index, i;
265 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
266 bucket_index = signature & (f->n_buckets - 1);
267 bucket = (struct rte_bucket_4_8 *)
268 &f->memory[bucket_index * f->bucket_size];
270 /* Key is present in the bucket */
271 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
272 uint64_t bucket_signature = bucket->signature;
273 uint64_t *bucket_key = &bucket->key[i];
275 if ((bucket_signature & mask) &&
276 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
277 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
279 memcpy(bucket_data, entry, f->entry_size);
280 lru_update(bucket, i);
282 *entry_ptr = (void *) bucket_data;
287 /* Key is not present in the bucket */
288 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
289 uint64_t bucket_signature = bucket->signature;
291 if ((bucket_signature & mask) == 0) {
292 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
294 bucket->signature |= mask;
295 keycpy(&bucket->key[i], key, &f->key_mask);
296 memcpy(bucket_data, entry, f->entry_size);
297 lru_update(bucket, i);
299 *entry_ptr = (void *) bucket_data;
305 /* Bucket full: replace LRU entry */
306 pos = lru_pos(bucket);
307 keycpy(&bucket->key[pos], key, &f->key_mask);
308 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
309 lru_update(bucket, pos);
311 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
317 rte_table_hash_entry_delete_key8_lru(
323 struct rte_table_hash *f = table;
324 struct rte_bucket_4_8 *bucket;
325 uint64_t signature, mask;
326 uint32_t bucket_index, i;
328 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
329 bucket_index = signature & (f->n_buckets - 1);
330 bucket = (struct rte_bucket_4_8 *)
331 &f->memory[bucket_index * f->bucket_size];
333 /* Key is present in the bucket */
334 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
335 uint64_t bucket_signature = bucket->signature;
336 uint64_t *bucket_key = &bucket->key[i];
338 if ((bucket_signature & mask) &&
339 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
340 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
342 bucket->signature &= ~mask;
345 memcpy(entry, bucket_data, f->entry_size);
351 /* Key is not present in the bucket */
357 rte_table_hash_create_key8_ext(void *params, int socket_id, uint32_t entry_size)
359 struct rte_table_hash_params *p = params;
360 struct rte_table_hash *f;
361 uint64_t bucket_size, stack_size, total_size;
362 uint32_t n_buckets_ext, i;
364 /* Check input parameters */
365 if ((check_params_create(p) != 0) ||
366 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
367 ((sizeof(struct rte_bucket_4_8) % 64) != 0))
373 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
374 * it is guaranteed that n_keys keys can be stored in the table at any time.
376 * The worst case scenario takes place when all the n_keys keys fall into
377 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
378 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
379 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
380 * into a different bucket. This case defeats the purpose of the hash table.
381 * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
383 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
385 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
387 /* Memory allocation */
388 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_8) +
389 KEYS_PER_BUCKET * entry_size);
390 stack_size = RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
391 total_size = sizeof(struct rte_table_hash) +
392 (p->n_buckets + n_buckets_ext) * bucket_size + stack_size;
394 if (total_size > SIZE_MAX) {
395 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
396 "for hash table %s\n",
397 __func__, total_size, p->name);
401 f = rte_zmalloc_socket(p->name,
407 "%s: Cannot allocate %" PRIu64 " bytes "
408 "for hash table %s\n",
409 __func__, total_size, p->name);
412 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
413 "is %" PRIu64 " bytes\n",
414 __func__, p->name, total_size);
416 /* Memory initialization */
417 f->n_buckets = p->n_buckets;
418 f->key_size = KEY_SIZE;
419 f->entry_size = entry_size;
420 f->bucket_size = bucket_size;
421 f->key_offset = p->key_offset;
422 f->f_hash = p->f_hash;
425 f->n_buckets_ext = n_buckets_ext;
426 f->stack_pos = n_buckets_ext;
427 f->stack = (uint32_t *)
428 &f->memory[(p->n_buckets + n_buckets_ext) * f->bucket_size];
430 if (p->key_mask != NULL)
431 f->key_mask = ((uint64_t *)p->key_mask)[0];
433 f->key_mask = 0xFFFFFFFFFFFFFFFFLLU;
435 for (i = 0; i < n_buckets_ext; i++)
442 rte_table_hash_free_key8_ext(void *table)
444 struct rte_table_hash *f = table;
446 /* Check input parameters */
448 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
457 rte_table_hash_entry_add_key8_ext(
464 struct rte_table_hash *f = table;
465 struct rte_bucket_4_8 *bucket0, *bucket, *bucket_prev;
467 uint32_t bucket_index, i;
469 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
470 bucket_index = signature & (f->n_buckets - 1);
471 bucket0 = (struct rte_bucket_4_8 *)
472 &f->memory[bucket_index * f->bucket_size];
474 /* Key is present in the bucket */
475 for (bucket = bucket0; bucket != NULL; bucket = bucket->next) {
478 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
479 uint64_t bucket_signature = bucket->signature;
480 uint64_t *bucket_key = &bucket->key[i];
482 if ((bucket_signature & mask) &&
483 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
484 uint8_t *bucket_data = &bucket->data[i *
487 memcpy(bucket_data, entry, f->entry_size);
489 *entry_ptr = (void *) bucket_data;
495 /* Key is not present in the bucket */
496 for (bucket_prev = NULL, bucket = bucket0;
497 bucket != NULL; bucket_prev = bucket, bucket = bucket->next) {
500 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
501 uint64_t bucket_signature = bucket->signature;
503 if ((bucket_signature & mask) == 0) {
504 uint8_t *bucket_data = &bucket->data[i *
507 bucket->signature |= mask;
508 keycpy(&bucket->key[i], key, &f->key_mask);
509 memcpy(bucket_data, entry, f->entry_size);
511 *entry_ptr = (void *) bucket_data;
518 /* Bucket full: extend bucket */
519 if (f->stack_pos > 0) {
520 bucket_index = f->stack[--f->stack_pos];
522 bucket = (struct rte_bucket_4_8 *) &f->memory[(f->n_buckets +
523 bucket_index) * f->bucket_size];
524 bucket_prev->next = bucket;
525 bucket_prev->next_valid = 1;
527 bucket->signature = 1;
528 keycpy(&bucket->key[0], key, &f->key_mask);
529 memcpy(&bucket->data[0], entry, f->entry_size);
531 *entry_ptr = (void *) &bucket->data[0];
539 rte_table_hash_entry_delete_key8_ext(
545 struct rte_table_hash *f = table;
546 struct rte_bucket_4_8 *bucket0, *bucket, *bucket_prev;
548 uint32_t bucket_index, i;
550 signature = f->f_hash(key, &f->key_mask, f->key_size, f->seed);
551 bucket_index = signature & (f->n_buckets - 1);
552 bucket0 = (struct rte_bucket_4_8 *)
553 &f->memory[bucket_index * f->bucket_size];
555 /* Key is present in the bucket */
556 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
557 bucket_prev = bucket, bucket = bucket->next) {
560 for (i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
561 uint64_t bucket_signature = bucket->signature;
562 uint64_t *bucket_key = &bucket->key[i];
564 if ((bucket_signature & mask) &&
565 (keycmp(bucket_key, key, &f->key_mask) == 0)) {
566 uint8_t *bucket_data = &bucket->data[i *
569 bucket->signature &= ~mask;
572 memcpy(entry, bucket_data,
575 if ((bucket->signature == 0) &&
576 (bucket_prev != NULL)) {
577 bucket_prev->next = bucket->next;
578 bucket_prev->next_valid =
582 sizeof(struct rte_bucket_4_8));
583 bucket_index = (((uint8_t *)bucket -
584 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
585 f->stack[f->stack_pos++] = bucket_index;
593 /* Key is not present in the bucket */
598 #define lookup_key8_cmp(key_in, bucket, pos, f) \
600 uint64_t xor[4], signature, k; \
602 signature = ~bucket->signature; \
604 k = key_in[0] & f->key_mask; \
605 xor[0] = (k ^ bucket->key[0]) | (signature & 1); \
606 xor[1] = (k ^ bucket->key[1]) | (signature & 2); \
607 xor[2] = (k ^ bucket->key[2]) | (signature & 4); \
608 xor[3] = (k ^ bucket->key[3]) | (signature & 8); \
621 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \
624 uint32_t key_offset = f->key_offset;\
626 pkt0_index = __builtin_ctzll(pkts_mask); \
627 pkt_mask = 1LLU << pkt0_index; \
628 pkts_mask &= ~pkt_mask; \
630 mbuf0 = pkts[pkt0_index]; \
631 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset)); \
634 #define lookup1_stage1(mbuf1, bucket1, f) \
637 uint64_t signature; \
638 uint32_t bucket_index; \
640 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset);\
641 signature = f->f_hash(key, &f->key_mask, KEY_SIZE, f->seed); \
642 bucket_index = signature & (f->n_buckets - 1); \
643 bucket1 = (struct rte_bucket_4_8 *) \
644 &f->memory[bucket_index * f->bucket_size]; \
645 rte_prefetch0(bucket1); \
648 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
649 pkts_mask_out, entries, f) \
656 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
657 lookup_key8_cmp(key, bucket2, pos, f); \
659 pkt_mask = ((bucket2->signature >> pos) & 1LLU) << pkt2_index;\
660 pkts_mask_out |= pkt_mask; \
662 a = (void *) &bucket2->data[pos * f->entry_size]; \
664 entries[pkt2_index] = a; \
665 lru_update(bucket2, pos); \
668 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out,\
669 entries, buckets_mask, buckets, keys, f) \
671 struct rte_bucket_4_8 *bucket_next; \
673 uint64_t pkt_mask, bucket_mask; \
677 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
678 lookup_key8_cmp(key, bucket2, pos, f); \
680 pkt_mask = ((bucket2->signature >> pos) & 1LLU) << pkt2_index;\
681 pkts_mask_out |= pkt_mask; \
683 a = (void *) &bucket2->data[pos * f->entry_size]; \
685 entries[pkt2_index] = a; \
687 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
688 buckets_mask |= bucket_mask; \
689 bucket_next = bucket2->next; \
690 buckets[pkt2_index] = bucket_next; \
691 keys[pkt2_index] = key; \
694 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\
697 struct rte_bucket_4_8 *bucket, *bucket_next; \
699 uint64_t pkt_mask, bucket_mask; \
703 bucket = buckets[pkt_index]; \
704 key = keys[pkt_index]; \
705 lookup_key8_cmp(key, bucket, pos, f); \
707 pkt_mask = ((bucket->signature >> pos) & 1LLU) << pkt_index;\
708 pkts_mask_out |= pkt_mask; \
710 a = (void *) &bucket->data[pos * f->entry_size]; \
712 entries[pkt_index] = a; \
714 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
715 buckets_mask |= bucket_mask; \
716 bucket_next = bucket->next; \
717 rte_prefetch0(bucket_next); \
718 buckets[pkt_index] = bucket_next; \
719 keys[pkt_index] = key; \
722 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
723 pkts, pkts_mask, f) \
725 uint64_t pkt00_mask, pkt01_mask; \
726 uint32_t key_offset = f->key_offset; \
728 pkt00_index = __builtin_ctzll(pkts_mask); \
729 pkt00_mask = 1LLU << pkt00_index; \
730 pkts_mask &= ~pkt00_mask; \
732 mbuf00 = pkts[pkt00_index]; \
733 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
735 pkt01_index = __builtin_ctzll(pkts_mask); \
736 pkt01_mask = 1LLU << pkt01_index; \
737 pkts_mask &= ~pkt01_mask; \
739 mbuf01 = pkts[pkt01_index]; \
740 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
743 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
744 mbuf00, mbuf01, pkts, pkts_mask, f) \
746 uint64_t pkt00_mask, pkt01_mask; \
747 uint32_t key_offset = f->key_offset; \
749 pkt00_index = __builtin_ctzll(pkts_mask); \
750 pkt00_mask = 1LLU << pkt00_index; \
751 pkts_mask &= ~pkt00_mask; \
753 mbuf00 = pkts[pkt00_index]; \
754 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
756 pkt01_index = __builtin_ctzll(pkts_mask); \
757 if (pkts_mask == 0) \
758 pkt01_index = pkt00_index; \
760 pkt01_mask = 1LLU << pkt01_index; \
761 pkts_mask &= ~pkt01_mask; \
763 mbuf01 = pkts[pkt01_index]; \
764 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
767 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f)\
769 uint64_t *key10, *key11; \
770 uint64_t signature10, signature11; \
771 uint32_t bucket10_index, bucket11_index; \
772 rte_table_hash_op_hash f_hash = f->f_hash; \
773 uint64_t seed = f->seed; \
774 uint32_t key_offset = f->key_offset; \
776 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, key_offset);\
777 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, key_offset);\
779 signature10 = f_hash(key10, &f->key_mask, KEY_SIZE, seed); \
780 bucket10_index = signature10 & (f->n_buckets - 1); \
781 bucket10 = (struct rte_bucket_4_8 *) \
782 &f->memory[bucket10_index * f->bucket_size]; \
783 rte_prefetch0(bucket10); \
785 signature11 = f_hash(key11, &f->key_mask, KEY_SIZE, seed); \
786 bucket11_index = signature11 & (f->n_buckets - 1); \
787 bucket11 = (struct rte_bucket_4_8 *) \
788 &f->memory[bucket11_index * f->bucket_size]; \
789 rte_prefetch0(bucket11); \
792 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
793 bucket20, bucket21, pkts_mask_out, entries, f) \
796 uint64_t pkt20_mask, pkt21_mask; \
797 uint64_t *key20, *key21; \
798 uint32_t pos20, pos21; \
800 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
801 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
803 lookup_key8_cmp(key20, bucket20, pos20, f); \
804 lookup_key8_cmp(key21, bucket21, pos21, f); \
806 pkt20_mask = ((bucket20->signature >> pos20) & 1LLU) << pkt20_index;\
807 pkt21_mask = ((bucket21->signature >> pos21) & 1LLU) << pkt21_index;\
808 pkts_mask_out |= pkt20_mask | pkt21_mask; \
810 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
811 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
812 rte_prefetch0(a20); \
813 rte_prefetch0(a21); \
814 entries[pkt20_index] = a20; \
815 entries[pkt21_index] = a21; \
816 lru_update(bucket20, pos20); \
817 lru_update(bucket21, pos21); \
820 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
821 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f)\
823 struct rte_bucket_4_8 *bucket20_next, *bucket21_next; \
825 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
826 uint64_t *key20, *key21; \
827 uint32_t pos20, pos21; \
829 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
830 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
832 lookup_key8_cmp(key20, bucket20, pos20, f); \
833 lookup_key8_cmp(key21, bucket21, pos21, f); \
835 pkt20_mask = ((bucket20->signature >> pos20) & 1LLU) << pkt20_index;\
836 pkt21_mask = ((bucket21->signature >> pos21) & 1LLU) << pkt21_index;\
837 pkts_mask_out |= pkt20_mask | pkt21_mask; \
839 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
840 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
841 rte_prefetch0(a20); \
842 rte_prefetch0(a21); \
843 entries[pkt20_index] = a20; \
844 entries[pkt21_index] = a21; \
846 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
847 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
848 buckets_mask |= bucket20_mask | bucket21_mask; \
849 bucket20_next = bucket20->next; \
850 bucket21_next = bucket21->next; \
851 buckets[pkt20_index] = bucket20_next; \
852 buckets[pkt21_index] = bucket21_next; \
853 keys[pkt20_index] = key20; \
854 keys[pkt21_index] = key21; \
858 rte_table_hash_lookup_key8_lru(
860 struct rte_mbuf **pkts,
862 uint64_t *lookup_hit_mask,
865 struct rte_table_hash *f = (struct rte_table_hash *) table;
866 struct rte_bucket_4_8 *bucket10, *bucket11, *bucket20, *bucket21;
867 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
868 uint32_t pkt00_index, pkt01_index, pkt10_index;
869 uint32_t pkt11_index, pkt20_index, pkt21_index;
870 uint64_t pkts_mask_out = 0;
872 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
873 RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(f, n_pkts_in);
875 /* Cannot run the pipeline with less than 5 packets */
876 if (__builtin_popcountll(pkts_mask) < 5) {
877 for ( ; pkts_mask; ) {
878 struct rte_bucket_4_8 *bucket;
879 struct rte_mbuf *mbuf;
882 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
883 lookup1_stage1(mbuf, bucket, f);
884 lookup1_stage2_lru(pkt_index, mbuf, bucket,
885 pkts_mask_out, entries, f);
888 *lookup_hit_mask = pkts_mask_out;
889 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
897 /* Pipeline stage 0 */
898 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
904 pkt10_index = pkt00_index;
905 pkt11_index = pkt01_index;
907 /* Pipeline stage 0 */
908 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
911 /* Pipeline stage 1 */
912 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
918 for ( ; pkts_mask; ) {
926 pkt20_index = pkt10_index;
927 pkt21_index = pkt11_index;
928 pkt10_index = pkt00_index;
929 pkt11_index = pkt01_index;
931 /* Pipeline stage 0 */
932 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
933 mbuf00, mbuf01, pkts, pkts_mask, f);
935 /* Pipeline stage 1 */
936 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
938 /* Pipeline stage 2 */
939 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
940 bucket20, bucket21, pkts_mask_out, entries, f);
954 pkt20_index = pkt10_index;
955 pkt21_index = pkt11_index;
956 pkt10_index = pkt00_index;
957 pkt11_index = pkt01_index;
959 /* Pipeline stage 1 */
960 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
962 /* Pipeline stage 2 */
963 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
964 bucket20, bucket21, pkts_mask_out, entries, f);
971 pkt20_index = pkt10_index;
972 pkt21_index = pkt11_index;
974 /* Pipeline stage 2 */
975 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
976 bucket20, bucket21, pkts_mask_out, entries, f);
978 *lookup_hit_mask = pkts_mask_out;
979 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
984 rte_table_hash_lookup_key8_ext(
986 struct rte_mbuf **pkts,
988 uint64_t *lookup_hit_mask,
991 struct rte_table_hash *f = (struct rte_table_hash *) table;
992 struct rte_bucket_4_8 *bucket10, *bucket11, *bucket20, *bucket21;
993 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
994 uint32_t pkt00_index, pkt01_index, pkt10_index;
995 uint32_t pkt11_index, pkt20_index, pkt21_index;
996 uint64_t pkts_mask_out = 0, buckets_mask = 0;
997 struct rte_bucket_4_8 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
998 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
1000 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
1001 RTE_TABLE_HASH_KEY8_STATS_PKTS_IN_ADD(f, n_pkts_in);
1003 /* Cannot run the pipeline with less than 5 packets */
1004 if (__builtin_popcountll(pkts_mask) < 5) {
1005 for ( ; pkts_mask; ) {
1006 struct rte_bucket_4_8 *bucket;
1007 struct rte_mbuf *mbuf;
1010 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
1011 lookup1_stage1(mbuf, bucket, f);
1012 lookup1_stage2_ext(pkt_index, mbuf, bucket,
1013 pkts_mask_out, entries, buckets_mask,
1017 goto grind_next_buckets;
1024 /* Pipeline stage 0 */
1025 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1031 pkt10_index = pkt00_index;
1032 pkt11_index = pkt01_index;
1034 /* Pipeline stage 0 */
1035 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1038 /* Pipeline stage 1 */
1039 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1045 for ( ; pkts_mask; ) {
1047 bucket20 = bucket10;
1048 bucket21 = bucket11;
1053 pkt20_index = pkt10_index;
1054 pkt21_index = pkt11_index;
1055 pkt10_index = pkt00_index;
1056 pkt11_index = pkt01_index;
1058 /* Pipeline stage 0 */
1059 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1060 mbuf00, mbuf01, pkts, pkts_mask, f);
1062 /* Pipeline stage 1 */
1063 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1065 /* Pipeline stage 2 */
1066 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1067 bucket20, bucket21, pkts_mask_out, entries,
1068 buckets_mask, buckets, keys, f);
1076 bucket20 = bucket10;
1077 bucket21 = bucket11;
1082 pkt20_index = pkt10_index;
1083 pkt21_index = pkt11_index;
1084 pkt10_index = pkt00_index;
1085 pkt11_index = pkt01_index;
1087 /* Pipeline stage 1 */
1088 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
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 bucket20 = bucket10;
1097 bucket21 = bucket11;
1100 pkt20_index = pkt10_index;
1101 pkt21_index = pkt11_index;
1103 /* Pipeline stage 2 */
1104 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1105 bucket20, bucket21, pkts_mask_out, entries,
1106 buckets_mask, buckets, keys, f);
1109 /* Grind next buckets */
1110 for ( ; buckets_mask; ) {
1111 uint64_t buckets_mask_next = 0;
1113 for ( ; buckets_mask; ) {
1117 pkt_index = __builtin_ctzll(buckets_mask);
1118 pkt_mask = 1LLU << pkt_index;
1119 buckets_mask &= ~pkt_mask;
1121 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1122 entries, buckets_mask_next, f);
1125 buckets_mask = buckets_mask_next;
1128 *lookup_hit_mask = pkts_mask_out;
1129 RTE_TABLE_HASH_KEY8_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
1134 rte_table_hash_key8_stats_read(void *table, struct rte_table_stats *stats, int clear)
1136 struct rte_table_hash *t = table;
1139 memcpy(stats, &t->stats, sizeof(t->stats));
1142 memset(&t->stats, 0, sizeof(t->stats));
1147 struct rte_table_ops rte_table_hash_key8_lru_ops = {
1148 .f_create = rte_table_hash_create_key8_lru,
1149 .f_free = rte_table_hash_free_key8_lru,
1150 .f_add = rte_table_hash_entry_add_key8_lru,
1151 .f_delete = rte_table_hash_entry_delete_key8_lru,
1153 .f_delete_bulk = NULL,
1154 .f_lookup = rte_table_hash_lookup_key8_lru,
1155 .f_stats = rte_table_hash_key8_stats_read,
1158 struct rte_table_ops rte_table_hash_key8_ext_ops = {
1159 .f_create = rte_table_hash_create_key8_ext,
1160 .f_free = rte_table_hash_free_key8_ext,
1161 .f_add = rte_table_hash_entry_add_key8_ext,
1162 .f_delete = rte_table_hash_entry_delete_key8_ext,
1164 .f_delete_bulk = NULL,
1165 .f_lookup = rte_table_hash_lookup_key8_ext,
1166 .f_stats = rte_table_hash_key8_stats_read,