1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_memory.h>
11 #include <rte_malloc.h>
14 #include "rte_table_hash.h"
16 #define KEYS_PER_BUCKET 4
23 uint16_t sig[KEYS_PER_BUCKET];
24 uint32_t key_pos[KEYS_PER_BUCKET];
27 #define BUCKET_NEXT(bucket) \
28 ((void *) ((bucket)->next & (~1LU)))
30 #define BUCKET_NEXT_VALID(bucket) \
31 ((bucket)->next & 1LU)
33 #define BUCKET_NEXT_SET(bucket, bucket_next) \
35 (bucket)->next = (((uintptr_t) ((void *) (bucket_next))) | 1LU);\
38 #define BUCKET_NEXT_SET_NULL(bucket) \
43 #define BUCKET_NEXT_COPY(bucket, bucket2) \
45 (bucket)->next = (bucket2)->next; \
48 #ifdef RTE_TABLE_STATS_COLLECT
50 #define RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(table, val) \
51 table->stats.n_pkts_in += val
52 #define RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(table, val) \
53 table->stats.n_pkts_lookup_miss += val
57 #define RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(table, val)
58 #define RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(table, val)
69 struct rte_table_hash {
70 struct rte_table_stats stats;
72 /* Input parameters */
77 uint32_t n_buckets_ext;
78 rte_table_hash_op_hash f_hash;
84 uint32_t key_size_shl;
85 uint32_t data_size_shl;
86 uint32_t key_stack_tos;
87 uint32_t bkt_ext_stack_tos;
90 struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
94 struct bucket *buckets;
95 struct bucket *buckets_ext;
99 uint32_t *bkt_ext_stack;
102 uint8_t memory[0] __rte_cache_aligned;
106 keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes)
108 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
111 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
112 if (a64[i] != (b64[i] & b_mask64[i]))
119 keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes)
121 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
124 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
125 dst64[i] = src64[i] & src_mask64[i];
129 check_params_create(struct rte_table_hash_params *params)
132 if (params->name == NULL) {
133 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
138 if ((params->key_size < sizeof(uint64_t)) ||
139 (!rte_is_power_of_2(params->key_size))) {
140 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
145 if (params->n_keys == 0) {
146 RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
151 if ((params->n_buckets == 0) ||
152 (!rte_is_power_of_2(params->n_buckets))) {
153 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
158 if (params->f_hash == NULL) {
159 RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
167 rte_table_hash_ext_create(void *params, int socket_id, uint32_t entry_size)
169 struct rte_table_hash_params *p = params;
170 struct rte_table_hash *t;
171 uint64_t table_meta_sz, key_mask_sz, bucket_sz, bucket_ext_sz, key_sz;
172 uint64_t key_stack_sz, bkt_ext_stack_sz, data_sz, total_size;
173 uint64_t key_mask_offset, bucket_offset, bucket_ext_offset, key_offset;
174 uint64_t key_stack_offset, bkt_ext_stack_offset, data_offset;
175 uint32_t n_buckets_ext, i;
177 /* Check input parameters */
178 if ((check_params_create(p) != 0) ||
179 (!rte_is_power_of_2(entry_size)) ||
180 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
181 (sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2)))
187 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
188 * it is guaranteed that n_keys keys can be stored in the table at any time.
190 * The worst case scenario takes place when all the n_keys keys fall into
191 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
192 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
193 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
194 * into a different bucket. This case defeats the purpose of the hash table.
195 * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
197 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
199 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
201 /* Memory allocation */
202 table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
203 key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size);
204 bucket_sz = RTE_CACHE_LINE_ROUNDUP(p->n_buckets * sizeof(struct bucket));
206 RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(struct bucket));
207 key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
208 key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
210 RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
211 data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
212 total_size = table_meta_sz + key_mask_sz + bucket_sz + bucket_ext_sz +
213 key_sz + key_stack_sz + bkt_ext_stack_sz + data_sz;
215 if (total_size > SIZE_MAX) {
216 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes"
217 " for hash table %s\n",
218 __func__, total_size, p->name);
222 t = rte_zmalloc_socket(p->name,
227 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes"
228 " for hash table %s\n",
229 __func__, total_size, p->name);
232 RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table %s memory "
233 "footprint is %" PRIu64 " bytes\n",
234 __func__, p->key_size, p->name, total_size);
236 /* Memory initialization */
237 t->key_size = p->key_size;
238 t->entry_size = entry_size;
239 t->n_keys = p->n_keys;
240 t->n_buckets = p->n_buckets;
241 t->n_buckets_ext = n_buckets_ext;
242 t->f_hash = p->f_hash;
244 t->key_offset = p->key_offset;
247 t->bucket_mask = t->n_buckets - 1;
248 t->key_size_shl = __builtin_ctzl(p->key_size);
249 t->data_size_shl = __builtin_ctzl(entry_size);
253 bucket_offset = key_mask_offset + key_mask_sz;
254 bucket_ext_offset = bucket_offset + bucket_sz;
255 key_offset = bucket_ext_offset + bucket_ext_sz;
256 key_stack_offset = key_offset + key_sz;
257 bkt_ext_stack_offset = key_stack_offset + key_stack_sz;
258 data_offset = bkt_ext_stack_offset + bkt_ext_stack_sz;
260 t->key_mask = (uint64_t *) &t->memory[key_mask_offset];
261 t->buckets = (struct bucket *) &t->memory[bucket_offset];
262 t->buckets_ext = (struct bucket *) &t->memory[bucket_ext_offset];
263 t->key_mem = &t->memory[key_offset];
264 t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
265 t->bkt_ext_stack = (uint32_t *) &t->memory[bkt_ext_stack_offset];
266 t->data_mem = &t->memory[data_offset];
269 if (p->key_mask == NULL)
270 memset(t->key_mask, 0xFF, p->key_size);
272 memcpy(t->key_mask, p->key_mask, p->key_size);
275 for (i = 0; i < t->n_keys; i++)
276 t->key_stack[i] = t->n_keys - 1 - i;
277 t->key_stack_tos = t->n_keys;
279 /* Bucket ext stack */
280 for (i = 0; i < t->n_buckets_ext; i++)
281 t->bkt_ext_stack[i] = t->n_buckets_ext - 1 - i;
282 t->bkt_ext_stack_tos = t->n_buckets_ext;
288 rte_table_hash_ext_free(void *table)
290 struct rte_table_hash *t = table;
292 /* Check input parameters */
301 rte_table_hash_ext_entry_add(void *table, void *key, void *entry,
302 int *key_found, void **entry_ptr)
304 struct rte_table_hash *t = table;
305 struct bucket *bkt0, *bkt, *bkt_prev;
307 uint32_t bkt_index, i;
309 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
310 bkt_index = sig & t->bucket_mask;
311 bkt0 = &t->buckets[bkt_index];
312 sig = (sig >> 16) | 1LLU;
314 /* Key is present in the bucket */
315 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
316 for (i = 0; i < KEYS_PER_BUCKET; i++) {
317 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
318 uint32_t bkt_key_index = bkt->key_pos[i];
320 &t->key_mem[bkt_key_index << t->key_size_shl];
322 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
323 t->key_size) == 0)) {
324 uint8_t *data = &t->data_mem[bkt_key_index <<
327 memcpy(data, entry, t->entry_size);
329 *entry_ptr = (void *) data;
334 /* Key is not present in the bucket */
335 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
336 bkt = BUCKET_NEXT(bkt))
337 for (i = 0; i < KEYS_PER_BUCKET; i++) {
338 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
341 uint32_t bkt_key_index;
342 uint8_t *bkt_key, *data;
344 /* Allocate new key */
345 if (t->key_stack_tos == 0) /* No free keys */
348 bkt_key_index = t->key_stack[
351 /* Install new key */
352 bkt_key = &t->key_mem[bkt_key_index <<
354 data = &t->data_mem[bkt_key_index <<
357 bkt->sig[i] = (uint16_t) sig;
358 bkt->key_pos[i] = bkt_key_index;
359 keycpy(bkt_key, key, t->key_mask, t->key_size);
360 memcpy(data, entry, t->entry_size);
363 *entry_ptr = (void *) data;
368 /* Bucket full: extend bucket */
369 if ((t->bkt_ext_stack_tos > 0) && (t->key_stack_tos > 0)) {
370 uint32_t bkt_key_index;
371 uint8_t *bkt_key, *data;
373 /* Allocate new bucket ext */
374 bkt_index = t->bkt_ext_stack[--t->bkt_ext_stack_tos];
375 bkt = &t->buckets_ext[bkt_index];
377 /* Chain the new bucket ext */
378 BUCKET_NEXT_SET(bkt_prev, bkt);
379 BUCKET_NEXT_SET_NULL(bkt);
381 /* Allocate new key */
382 bkt_key_index = t->key_stack[--t->key_stack_tos];
383 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
385 data = &t->data_mem[bkt_key_index << t->data_size_shl];
387 /* Install new key into bucket */
388 bkt->sig[0] = (uint16_t) sig;
389 bkt->key_pos[0] = bkt_key_index;
390 keycpy(bkt_key, key, t->key_mask, t->key_size);
391 memcpy(data, entry, t->entry_size);
394 *entry_ptr = (void *) data;
402 rte_table_hash_ext_entry_delete(void *table, void *key, int *key_found,
405 struct rte_table_hash *t = table;
406 struct bucket *bkt0, *bkt, *bkt_prev;
408 uint32_t bkt_index, i;
410 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
411 bkt_index = sig & t->bucket_mask;
412 bkt0 = &t->buckets[bkt_index];
413 sig = (sig >> 16) | 1LLU;
415 /* Key is present in the bucket */
416 for (bkt_prev = NULL, bkt = bkt0; bkt != NULL; bkt_prev = bkt,
417 bkt = BUCKET_NEXT(bkt))
418 for (i = 0; i < KEYS_PER_BUCKET; i++) {
419 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
420 uint32_t bkt_key_index = bkt->key_pos[i];
421 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
424 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
425 t->key_size) == 0)) {
426 uint8_t *data = &t->data_mem[bkt_key_index <<
429 /* Uninstall key from bucket */
433 memcpy(entry, data, t->entry_size);
436 t->key_stack[t->key_stack_tos++] =
439 /*Check if bucket is unused */
440 if ((bkt_prev != NULL) &&
441 (bkt->sig[0] == 0) && (bkt->sig[1] == 0) &&
442 (bkt->sig[2] == 0) && (bkt->sig[3] == 0)) {
444 BUCKET_NEXT_COPY(bkt_prev, bkt);
447 memset(bkt, 0, sizeof(struct bucket));
449 /* Free bucket back to buckets ext */
450 bkt_index = bkt - t->buckets_ext;
451 t->bkt_ext_stack[t->bkt_ext_stack_tos++]
459 /* Key is not present in the bucket */
464 static int rte_table_hash_ext_lookup_unoptimized(
466 struct rte_mbuf **pkts,
468 uint64_t *lookup_hit_mask,
471 struct rte_table_hash *t = (struct rte_table_hash *) table;
472 uint64_t pkts_mask_out = 0;
474 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
476 for ( ; pkts_mask; ) {
477 struct bucket *bkt0, *bkt;
478 struct rte_mbuf *pkt;
480 uint64_t pkt_mask, sig;
481 uint32_t pkt_index, bkt_index, i;
483 pkt_index = __builtin_ctzll(pkts_mask);
484 pkt_mask = 1LLU << pkt_index;
485 pkts_mask &= ~pkt_mask;
487 pkt = pkts[pkt_index];
488 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
489 sig = (uint64_t) t->f_hash(key, t->key_mask, t->key_size, t->seed);
491 bkt_index = sig & t->bucket_mask;
492 bkt0 = &t->buckets[bkt_index];
493 sig = (sig >> 16) | 1LLU;
495 /* Key is present in the bucket */
496 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt))
497 for (i = 0; i < KEYS_PER_BUCKET; i++) {
498 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
499 uint32_t bkt_key_index = bkt->key_pos[i];
500 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
503 if ((sig == bkt_sig) && (keycmp(bkt_key, key,
504 t->key_mask, t->key_size) == 0)) {
505 uint8_t *data = &t->data_mem[
506 bkt_key_index << t->data_size_shl];
508 pkts_mask_out |= pkt_mask;
509 entries[pkt_index] = (void *) data;
515 *lookup_hit_mask = pkts_mask_out;
521 * mask = match bitmask
522 * match = at least one match
523 * match_many = more than one match
524 * match_pos = position of first match
526 *----------------------------------------
527 * mask match match_many match_pos
528 *----------------------------------------
533 *----------------------------------------
538 *----------------------------------------
543 *----------------------------------------
548 *----------------------------------------
550 * match = 1111_1111_1111_1110
551 * match_many = 1111_1110_1110_1000
552 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
555 * match_many = 0xFEE8LLU
556 * match_pos = 0x12131210LLU
560 #define LUT_MATCH 0xFFFELLU
561 #define LUT_MATCH_MANY 0xFEE8LLU
562 #define LUT_MATCH_POS 0x12131210LLU
564 #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos) \
566 uint64_t bucket_sig[4], mask[4], mask_all; \
568 bucket_sig[0] = bucket->sig[0]; \
569 bucket_sig[1] = bucket->sig[1]; \
570 bucket_sig[2] = bucket->sig[2]; \
571 bucket_sig[3] = bucket->sig[3]; \
573 bucket_sig[0] ^= mbuf_sig; \
574 bucket_sig[1] ^= mbuf_sig; \
575 bucket_sig[2] ^= mbuf_sig; \
576 bucket_sig[3] ^= mbuf_sig; \
583 if (bucket_sig[0] == 0) \
585 if (bucket_sig[1] == 0) \
587 if (bucket_sig[2] == 0) \
589 if (bucket_sig[3] == 0) \
592 mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \
594 match = (LUT_MATCH >> mask_all) & 1; \
595 match_many = (LUT_MATCH_MANY >> mask_all) & 1; \
596 match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \
599 #define lookup_cmp_key(mbuf, key, match_key, f) \
601 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
602 uint64_t *bkt_key = (uint64_t *) key; \
603 uint64_t *key_mask = f->key_mask; \
605 switch (f->key_size) { \
608 uint64_t xor = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
617 uint64_t xor[2], or; \
619 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
620 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
621 or = xor[0] | xor[1]; \
630 uint64_t xor[4], or; \
632 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
633 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
634 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
635 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
636 or = xor[0] | xor[1] | xor[2] | xor[3]; \
645 uint64_t xor[8], or; \
647 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
648 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
649 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
650 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
651 xor[4] = (pkt_key[4] & key_mask[4]) ^ bkt_key[4]; \
652 xor[5] = (pkt_key[5] & key_mask[5]) ^ bkt_key[5]; \
653 xor[6] = (pkt_key[6] & key_mask[6]) ^ bkt_key[6]; \
654 xor[7] = (pkt_key[7] & key_mask[7]) ^ bkt_key[7]; \
655 or = xor[0] | xor[1] | xor[2] | xor[3] | \
656 xor[4] | xor[5] | xor[6] | xor[7]; \
665 if (keycmp(bkt_key, pkt_key, key_mask, f->key_size) == 0) \
670 #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index) \
672 uint64_t pkt00_mask, pkt01_mask; \
673 struct rte_mbuf *mbuf00, *mbuf01; \
674 uint32_t key_offset = t->key_offset; \
676 pkt00_index = __builtin_ctzll(pkts_mask); \
677 pkt00_mask = 1LLU << pkt00_index; \
678 pkts_mask &= ~pkt00_mask; \
679 mbuf00 = pkts[pkt00_index]; \
681 pkt01_index = __builtin_ctzll(pkts_mask); \
682 pkt01_mask = 1LLU << pkt01_index; \
683 pkts_mask &= ~pkt01_mask; \
684 mbuf01 = pkts[pkt01_index]; \
686 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
687 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
690 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
693 uint64_t pkt00_mask, pkt01_mask; \
694 struct rte_mbuf *mbuf00, *mbuf01; \
695 uint32_t key_offset = t->key_offset; \
697 pkt00_index = __builtin_ctzll(pkts_mask); \
698 pkt00_mask = 1LLU << pkt00_index; \
699 pkts_mask &= ~pkt00_mask; \
700 mbuf00 = pkts[pkt00_index]; \
702 pkt01_index = __builtin_ctzll(pkts_mask); \
703 if (pkts_mask == 0) \
704 pkt01_index = pkt00_index; \
705 pkt01_mask = 1LLU << pkt01_index; \
706 pkts_mask &= ~pkt01_mask; \
707 mbuf01 = pkts[pkt01_index]; \
709 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
710 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
713 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index) \
715 struct grinder *g10, *g11; \
716 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
717 struct rte_mbuf *mbuf10, *mbuf11; \
718 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
719 uint8_t *key10, *key11; \
720 uint64_t bucket_mask = t->bucket_mask; \
721 rte_table_hash_op_hash f_hash = t->f_hash; \
722 uint64_t seed = t->seed; \
723 uint32_t key_size = t->key_size; \
724 uint32_t key_offset = t->key_offset; \
726 mbuf10 = pkts[pkt10_index]; \
727 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset); \
728 sig10 = (uint64_t) f_hash(key10, t->key_mask, key_size, seed); \
729 bkt10_index = sig10 & bucket_mask; \
730 bkt10 = &buckets[bkt10_index]; \
732 mbuf11 = pkts[pkt11_index]; \
733 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset); \
734 sig11 = (uint64_t) f_hash(key11, t->key_mask, key_size, seed); \
735 bkt11_index = sig11 & bucket_mask; \
736 bkt11 = &buckets[bkt11_index]; \
738 rte_prefetch0(bkt10); \
739 rte_prefetch0(bkt11); \
741 g10 = &g[pkt10_index]; \
745 g11 = &g[pkt11_index]; \
750 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
752 struct grinder *g20, *g21; \
753 uint64_t sig20, sig21; \
754 struct bucket *bkt20, *bkt21; \
755 uint8_t *key20, *key21, *key_mem = t->key_mem; \
756 uint64_t match20, match21, match_many20, match_many21; \
757 uint64_t match_pos20, match_pos21; \
758 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
760 g20 = &g[pkt20_index]; \
763 sig20 = (sig20 >> 16) | 1LLU; \
764 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
765 match20 <<= pkt20_index; \
766 match_many20 |= BUCKET_NEXT_VALID(bkt20); \
767 match_many20 <<= pkt20_index; \
768 key20_index = bkt20->key_pos[match_pos20]; \
769 key20 = &key_mem[key20_index << key_size_shl]; \
771 g21 = &g[pkt21_index]; \
774 sig21 = (sig21 >> 16) | 1LLU; \
775 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
776 match21 <<= pkt21_index; \
777 match_many21 |= BUCKET_NEXT_VALID(bkt21); \
778 match_many21 <<= pkt21_index; \
779 key21_index = bkt21->key_pos[match_pos21]; \
780 key21 = &key_mem[key21_index << key_size_shl]; \
782 rte_prefetch0(key20); \
783 rte_prefetch0(key21); \
785 pkts_mask_match_many |= match_many20 | match_many21; \
787 g20->match = match20; \
788 g20->key_index = key20_index; \
790 g21->match = match21; \
791 g21->key_index = key21_index; \
794 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
797 struct grinder *g30, *g31; \
798 struct rte_mbuf *mbuf30, *mbuf31; \
799 uint8_t *key30, *key31, *key_mem = t->key_mem; \
800 uint8_t *data30, *data31, *data_mem = t->data_mem; \
801 uint64_t match30, match31, match_key30, match_key31, match_keys;\
802 uint32_t key30_index, key31_index; \
803 uint32_t key_size_shl = t->key_size_shl; \
804 uint32_t data_size_shl = t->data_size_shl; \
806 mbuf30 = pkts[pkt30_index]; \
807 g30 = &g[pkt30_index]; \
808 match30 = g30->match; \
809 key30_index = g30->key_index; \
810 key30 = &key_mem[key30_index << key_size_shl]; \
811 lookup_cmp_key(mbuf30, key30, match_key30, t); \
812 match_key30 <<= pkt30_index; \
813 match_key30 &= match30; \
814 data30 = &data_mem[key30_index << data_size_shl]; \
815 entries[pkt30_index] = data30; \
817 mbuf31 = pkts[pkt31_index]; \
818 g31 = &g[pkt31_index]; \
819 match31 = g31->match; \
820 key31_index = g31->key_index; \
821 key31 = &key_mem[key31_index << key_size_shl]; \
822 lookup_cmp_key(mbuf31, key31, match_key31, t); \
823 match_key31 <<= pkt31_index; \
824 match_key31 &= match31; \
825 data31 = &data_mem[key31_index << data_size_shl]; \
826 entries[pkt31_index] = data31; \
828 rte_prefetch0(data30); \
829 rte_prefetch0(data31); \
831 match_keys = match_key30 | match_key31; \
832 pkts_mask_out |= match_keys; \
836 * The lookup function implements a 4-stage pipeline, with each stage processing
837 * two different packets. The purpose of pipelined implementation is to hide the
838 * latency of prefetching the data structures and loosen the data dependency
839 * between instructions.
841 * p00 _______ p10 _______ p20 _______ p30 _______
842 *----->| |----->| |----->| |----->| |----->
843 * | 0 | | 1 | | 2 | | 3 |
844 *----->|_______|----->|_______|----->|_______|----->|_______|----->
847 * The naming convention is:
848 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
851 static int rte_table_hash_ext_lookup(
853 struct rte_mbuf **pkts,
855 uint64_t *lookup_hit_mask,
858 struct rte_table_hash *t = (struct rte_table_hash *) table;
859 struct grinder *g = t->grinders;
860 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
861 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
862 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
865 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
866 RTE_TABLE_HASH_EXT_STATS_PKTS_IN_ADD(t, n_pkts_in);
868 /* Cannot run the pipeline with less than 7 packets */
869 if (__builtin_popcountll(pkts_mask) < 7) {
870 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
871 pkts_mask, lookup_hit_mask, entries);
872 RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in -
873 __builtin_popcountll(*lookup_hit_mask));
877 /* Pipeline stage 0 */
878 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
881 pkt10_index = pkt00_index;
882 pkt11_index = pkt01_index;
884 /* Pipeline stage 0 */
885 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
887 /* Pipeline stage 1 */
888 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
891 pkt20_index = pkt10_index;
892 pkt21_index = pkt11_index;
893 pkt10_index = pkt00_index;
894 pkt11_index = pkt01_index;
896 /* Pipeline stage 0 */
897 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
899 /* Pipeline stage 1 */
900 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
902 /* Pipeline stage 2 */
903 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
909 for ( ; pkts_mask; ) {
911 pkt30_index = pkt20_index;
912 pkt31_index = pkt21_index;
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(t, g, pkts, pkts_mask,
920 pkt00_index, pkt01_index);
922 /* Pipeline stage 1 */
923 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
925 /* Pipeline stage 2 */
926 lookup2_stage2(t, g, pkt20_index, pkt21_index,
927 pkts_mask_match_many);
929 /* Pipeline stage 3 */
930 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
931 pkts_mask_out, entries);
935 pkt30_index = pkt20_index;
936 pkt31_index = pkt21_index;
937 pkt20_index = pkt10_index;
938 pkt21_index = pkt11_index;
939 pkt10_index = pkt00_index;
940 pkt11_index = pkt01_index;
942 /* Pipeline stage 1 */
943 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
945 /* Pipeline stage 2 */
946 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
948 /* Pipeline stage 3 */
949 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
953 pkt30_index = pkt20_index;
954 pkt31_index = pkt21_index;
955 pkt20_index = pkt10_index;
956 pkt21_index = pkt11_index;
958 /* Pipeline stage 2 */
959 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
961 /* Pipeline stage 3 */
962 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
966 pkt30_index = pkt20_index;
967 pkt31_index = pkt21_index;
969 /* Pipeline stage 3 */
970 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
974 pkts_mask_match_many &= ~pkts_mask_out;
975 if (pkts_mask_match_many) {
976 uint64_t pkts_mask_out_slow = 0;
978 status = rte_table_hash_ext_lookup_unoptimized(table, pkts,
979 pkts_mask_match_many, &pkts_mask_out_slow, entries);
980 pkts_mask_out |= pkts_mask_out_slow;
983 *lookup_hit_mask = pkts_mask_out;
984 RTE_TABLE_HASH_EXT_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
989 rte_table_hash_ext_stats_read(void *table, struct rte_table_stats *stats, int clear)
991 struct rte_table_hash *t = table;
994 memcpy(stats, &t->stats, sizeof(t->stats));
997 memset(&t->stats, 0, sizeof(t->stats));
1002 struct rte_table_ops rte_table_hash_ext_ops = {
1003 .f_create = rte_table_hash_ext_create,
1004 .f_free = rte_table_hash_ext_free,
1005 .f_add = rte_table_hash_ext_entry_add,
1006 .f_delete = rte_table_hash_ext_entry_delete,
1008 .f_delete_bulk = NULL,
1009 .f_lookup = rte_table_hash_ext_lookup,
1010 .f_stats = rte_table_hash_ext_stats_read,