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"
17 #define KEYS_PER_BUCKET 4
19 #ifdef RTE_TABLE_STATS_COLLECT
21 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val) \
22 table->stats.n_pkts_in += val
23 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val) \
24 table->stats.n_pkts_lookup_miss += val
28 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val)
29 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val)
38 uint16_t sig[KEYS_PER_BUCKET];
39 uint32_t key_pos[KEYS_PER_BUCKET];
50 struct rte_table_hash {
51 struct rte_table_stats stats;
53 /* Input parameters */
58 rte_table_hash_op_hash f_hash;
64 uint32_t key_size_shl;
65 uint32_t data_size_shl;
66 uint32_t key_stack_tos;
69 struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
73 struct bucket *buckets;
79 uint8_t memory[0] __rte_cache_aligned;
83 keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes)
85 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
88 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
89 if (a64[i] != (b64[i] & b_mask64[i]))
96 keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes)
98 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
101 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
102 dst64[i] = src64[i] & src_mask64[i];
106 check_params_create(struct rte_table_hash_params *params)
109 if (params->name == NULL) {
110 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
115 if ((params->key_size < sizeof(uint64_t)) ||
116 (!rte_is_power_of_2(params->key_size))) {
117 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
122 if (params->n_keys == 0) {
123 RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
128 if ((params->n_buckets == 0) ||
129 (!rte_is_power_of_2(params->n_buckets))) {
130 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
135 if (params->f_hash == NULL) {
136 RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
144 rte_table_hash_lru_create(void *params, int socket_id, uint32_t entry_size)
146 struct rte_table_hash_params *p = params;
147 struct rte_table_hash *t;
148 uint64_t table_meta_sz, key_mask_sz, bucket_sz, key_sz, key_stack_sz;
149 uint64_t data_sz, total_size;
150 uint64_t key_mask_offset, bucket_offset, key_offset, key_stack_offset;
151 uint64_t data_offset;
152 uint32_t n_buckets, i;
154 /* Check input parameters */
155 if ((check_params_create(p) != 0) ||
156 (!rte_is_power_of_2(entry_size)) ||
157 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
158 (sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2))) {
165 * Objective: Pick the number of buckets (n_buckets) so that there a chance
166 * to store n_keys keys in the table.
168 * Note: Since the buckets do not get extended, it is not possible to
169 * guarantee that n_keys keys can be stored in the table at any time. In the
170 * worst case scenario when all the n_keys fall into the same bucket, only
171 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
172 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
173 * n_keys to n_buckets ratio.
175 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
177 n_buckets = rte_align32pow2(
178 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
179 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
181 /* Memory allocation */
182 table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
183 key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size);
184 bucket_sz = RTE_CACHE_LINE_ROUNDUP(n_buckets * sizeof(struct bucket));
185 key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
186 key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
187 data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
188 total_size = table_meta_sz + key_mask_sz + bucket_sz + key_sz +
189 key_stack_sz + data_sz;
191 if (total_size > SIZE_MAX) {
193 "%s: Cannot allocate %" PRIu64 " bytes for hash "
195 __func__, total_size, p->name);
199 t = rte_zmalloc_socket(p->name,
205 "%s: Cannot allocate %" PRIu64 " bytes for hash "
207 __func__, total_size, p->name);
210 RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table %s memory footprint"
211 " is %" PRIu64 " bytes\n",
212 __func__, p->key_size, p->name, total_size);
214 /* Memory initialization */
215 t->key_size = p->key_size;
216 t->entry_size = entry_size;
217 t->n_keys = p->n_keys;
218 t->n_buckets = n_buckets;
219 t->f_hash = p->f_hash;
221 t->key_offset = p->key_offset;
224 t->bucket_mask = t->n_buckets - 1;
225 t->key_size_shl = __builtin_ctzl(p->key_size);
226 t->data_size_shl = __builtin_ctzl(entry_size);
230 bucket_offset = key_mask_offset + key_mask_sz;
231 key_offset = bucket_offset + bucket_sz;
232 key_stack_offset = key_offset + key_sz;
233 data_offset = key_stack_offset + key_stack_sz;
235 t->key_mask = (uint64_t *) &t->memory[key_mask_offset];
236 t->buckets = (struct bucket *) &t->memory[bucket_offset];
237 t->key_mem = &t->memory[key_offset];
238 t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
239 t->data_mem = &t->memory[data_offset];
242 if (p->key_mask == NULL)
243 memset(t->key_mask, 0xFF, p->key_size);
245 memcpy(t->key_mask, p->key_mask, p->key_size);
248 for (i = 0; i < t->n_keys; i++)
249 t->key_stack[i] = t->n_keys - 1 - i;
250 t->key_stack_tos = t->n_keys;
253 for (i = 0; i < t->n_buckets; i++) {
254 struct bucket *bkt = &t->buckets[i];
263 rte_table_hash_lru_free(void *table)
265 struct rte_table_hash *t = table;
267 /* Check input parameters */
276 rte_table_hash_lru_entry_add(void *table, void *key, void *entry,
277 int *key_found, void **entry_ptr)
279 struct rte_table_hash *t = table;
282 uint32_t bkt_index, i;
284 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
285 bkt_index = sig & t->bucket_mask;
286 bkt = &t->buckets[bkt_index];
287 sig = (sig >> 16) | 1LLU;
289 /* Key is present in the bucket */
290 for (i = 0; i < KEYS_PER_BUCKET; i++) {
291 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
292 uint32_t bkt_key_index = bkt->key_pos[i];
293 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
296 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
297 t->key_size) == 0)) {
298 uint8_t *data = &t->data_mem[bkt_key_index <<
301 memcpy(data, entry, t->entry_size);
304 *entry_ptr = (void *) data;
309 /* Key is not present in the bucket */
310 for (i = 0; i < KEYS_PER_BUCKET; i++) {
311 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
314 uint32_t bkt_key_index;
315 uint8_t *bkt_key, *data;
317 /* Allocate new key */
318 if (t->key_stack_tos == 0) {
319 /* No keys available */
322 bkt_key_index = t->key_stack[--t->key_stack_tos];
324 /* Install new key */
325 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
326 data = &t->data_mem[bkt_key_index << t->data_size_shl];
328 bkt->sig[i] = (uint16_t) sig;
329 bkt->key_pos[i] = bkt_key_index;
330 keycpy(bkt_key, key, t->key_mask, t->key_size);
331 memcpy(data, entry, t->entry_size);
335 *entry_ptr = (void *) data;
342 uint64_t pos = lru_pos(bkt);
343 uint32_t bkt_key_index = bkt->key_pos[pos];
344 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
346 uint8_t *data = &t->data_mem[bkt_key_index << t->data_size_shl];
348 bkt->sig[pos] = (uint16_t) sig;
349 keycpy(bkt_key, key, t->key_mask, t->key_size);
350 memcpy(data, entry, t->entry_size);
351 lru_update(bkt, pos);
354 *entry_ptr = (void *) data;
360 rte_table_hash_lru_entry_delete(void *table, void *key, int *key_found,
363 struct rte_table_hash *t = table;
366 uint32_t bkt_index, i;
368 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
369 bkt_index = sig & t->bucket_mask;
370 bkt = &t->buckets[bkt_index];
371 sig = (sig >> 16) | 1LLU;
373 /* Key is present in the bucket */
374 for (i = 0; i < KEYS_PER_BUCKET; i++) {
375 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
376 uint32_t bkt_key_index = bkt->key_pos[i];
377 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
380 if ((sig == bkt_sig) &&
381 (keycmp(bkt_key, key, t->key_mask, t->key_size) == 0)) {
382 uint8_t *data = &t->data_mem[bkt_key_index <<
386 t->key_stack[t->key_stack_tos++] = bkt_key_index;
389 memcpy(entry, data, t->entry_size);
394 /* Key is not present in the bucket */
399 static int rte_table_hash_lru_lookup_unoptimized(
401 struct rte_mbuf **pkts,
403 uint64_t *lookup_hit_mask,
406 struct rte_table_hash *t = (struct rte_table_hash *) table;
407 uint64_t pkts_mask_out = 0;
409 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
410 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
412 for ( ; pkts_mask; ) {
414 struct rte_mbuf *pkt;
416 uint64_t pkt_mask, sig;
417 uint32_t pkt_index, bkt_index, i;
419 pkt_index = __builtin_ctzll(pkts_mask);
420 pkt_mask = 1LLU << pkt_index;
421 pkts_mask &= ~pkt_mask;
423 pkt = pkts[pkt_index];
424 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
425 sig = (uint64_t) t->f_hash(key, t->key_mask, t->key_size, t->seed);
427 bkt_index = sig & t->bucket_mask;
428 bkt = &t->buckets[bkt_index];
429 sig = (sig >> 16) | 1LLU;
431 /* Key is present in the bucket */
432 for (i = 0; i < KEYS_PER_BUCKET; i++) {
433 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
434 uint32_t bkt_key_index = bkt->key_pos[i];
435 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
438 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
439 t->key_size) == 0)) {
440 uint8_t *data = &t->data_mem[bkt_key_index <<
444 pkts_mask_out |= pkt_mask;
445 entries[pkt_index] = (void *) data;
451 *lookup_hit_mask = pkts_mask_out;
452 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
458 * mask = match bitmask
459 * match = at least one match
460 * match_many = more than one match
461 * match_pos = position of first match
463 * ----------------------------------------
464 * mask match match_many match_pos
465 * ----------------------------------------
470 * ----------------------------------------
475 * ----------------------------------------
480 * ----------------------------------------
485 * ----------------------------------------
487 * match = 1111_1111_1111_1110
488 * match_many = 1111_1110_1110_1000
489 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
492 * match_many = 0xFEE8LLU
493 * match_pos = 0x12131210LLU
497 #define LUT_MATCH 0xFFFELLU
498 #define LUT_MATCH_MANY 0xFEE8LLU
499 #define LUT_MATCH_POS 0x12131210LLU
501 #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos)\
503 uint64_t bucket_sig[4], mask[4], mask_all; \
505 bucket_sig[0] = bucket->sig[0]; \
506 bucket_sig[1] = bucket->sig[1]; \
507 bucket_sig[2] = bucket->sig[2]; \
508 bucket_sig[3] = bucket->sig[3]; \
510 bucket_sig[0] ^= mbuf_sig; \
511 bucket_sig[1] ^= mbuf_sig; \
512 bucket_sig[2] ^= mbuf_sig; \
513 bucket_sig[3] ^= mbuf_sig; \
520 if (bucket_sig[0] == 0) \
522 if (bucket_sig[1] == 0) \
524 if (bucket_sig[2] == 0) \
526 if (bucket_sig[3] == 0) \
529 mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \
531 match = (LUT_MATCH >> mask_all) & 1; \
532 match_many = (LUT_MATCH_MANY >> mask_all) & 1; \
533 match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \
536 #define lookup_cmp_key(mbuf, key, match_key, f) \
538 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
539 uint64_t *bkt_key = (uint64_t *) key; \
540 uint64_t *key_mask = f->key_mask; \
542 switch (f->key_size) { \
545 uint64_t xor = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
554 uint64_t xor[2], or; \
556 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
557 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
558 or = xor[0] | xor[1]; \
567 uint64_t xor[4], or; \
569 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
570 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
571 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
572 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
573 or = xor[0] | xor[1] | xor[2] | xor[3]; \
582 uint64_t xor[8], or; \
584 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
585 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
586 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
587 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
588 xor[4] = (pkt_key[4] & key_mask[4]) ^ bkt_key[4]; \
589 xor[5] = (pkt_key[5] & key_mask[5]) ^ bkt_key[5]; \
590 xor[6] = (pkt_key[6] & key_mask[6]) ^ bkt_key[6]; \
591 xor[7] = (pkt_key[7] & key_mask[7]) ^ bkt_key[7]; \
592 or = xor[0] | xor[1] | xor[2] | xor[3] | \
593 xor[4] | xor[5] | xor[6] | xor[7]; \
602 if (keycmp(bkt_key, pkt_key, key_mask, f->key_size) == 0) \
607 #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index)\
609 uint64_t pkt00_mask, pkt01_mask; \
610 struct rte_mbuf *mbuf00, *mbuf01; \
611 uint32_t key_offset = t->key_offset; \
613 pkt00_index = __builtin_ctzll(pkts_mask); \
614 pkt00_mask = 1LLU << pkt00_index; \
615 pkts_mask &= ~pkt00_mask; \
616 mbuf00 = pkts[pkt00_index]; \
618 pkt01_index = __builtin_ctzll(pkts_mask); \
619 pkt01_mask = 1LLU << pkt01_index; \
620 pkts_mask &= ~pkt01_mask; \
621 mbuf01 = pkts[pkt01_index]; \
623 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
624 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
627 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
630 uint64_t pkt00_mask, pkt01_mask; \
631 struct rte_mbuf *mbuf00, *mbuf01; \
632 uint32_t key_offset = t->key_offset; \
634 pkt00_index = __builtin_ctzll(pkts_mask); \
635 pkt00_mask = 1LLU << pkt00_index; \
636 pkts_mask &= ~pkt00_mask; \
637 mbuf00 = pkts[pkt00_index]; \
639 pkt01_index = __builtin_ctzll(pkts_mask); \
640 if (pkts_mask == 0) \
641 pkt01_index = pkt00_index; \
643 pkt01_mask = 1LLU << pkt01_index; \
644 pkts_mask &= ~pkt01_mask; \
645 mbuf01 = pkts[pkt01_index]; \
647 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
648 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
651 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index)\
653 struct grinder *g10, *g11; \
654 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
655 struct rte_mbuf *mbuf10, *mbuf11; \
656 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
657 uint8_t *key10, *key11; \
658 uint64_t bucket_mask = t->bucket_mask; \
659 rte_table_hash_op_hash f_hash = t->f_hash; \
660 uint64_t seed = t->seed; \
661 uint32_t key_size = t->key_size; \
662 uint32_t key_offset = t->key_offset; \
664 mbuf10 = pkts[pkt10_index]; \
665 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset);\
666 sig10 = (uint64_t) f_hash(key10, t->key_mask, key_size, seed);\
667 bkt10_index = sig10 & bucket_mask; \
668 bkt10 = &buckets[bkt10_index]; \
670 mbuf11 = pkts[pkt11_index]; \
671 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset);\
672 sig11 = (uint64_t) f_hash(key11, t->key_mask, key_size, seed);\
673 bkt11_index = sig11 & bucket_mask; \
674 bkt11 = &buckets[bkt11_index]; \
676 rte_prefetch0(bkt10); \
677 rte_prefetch0(bkt11); \
679 g10 = &g[pkt10_index]; \
683 g11 = &g[pkt11_index]; \
688 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
690 struct grinder *g20, *g21; \
691 uint64_t sig20, sig21; \
692 struct bucket *bkt20, *bkt21; \
693 uint8_t *key20, *key21, *key_mem = t->key_mem; \
694 uint64_t match20, match21, match_many20, match_many21; \
695 uint64_t match_pos20, match_pos21; \
696 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
698 g20 = &g[pkt20_index]; \
701 sig20 = (sig20 >> 16) | 1LLU; \
702 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
703 match20 <<= pkt20_index; \
704 match_many20 <<= pkt20_index; \
705 key20_index = bkt20->key_pos[match_pos20]; \
706 key20 = &key_mem[key20_index << key_size_shl]; \
708 g21 = &g[pkt21_index]; \
711 sig21 = (sig21 >> 16) | 1LLU; \
712 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
713 match21 <<= pkt21_index; \
714 match_many21 <<= pkt21_index; \
715 key21_index = bkt21->key_pos[match_pos21]; \
716 key21 = &key_mem[key21_index << key_size_shl]; \
718 rte_prefetch0(key20); \
719 rte_prefetch0(key21); \
721 pkts_mask_match_many |= match_many20 | match_many21; \
723 g20->match = match20; \
724 g20->match_pos = match_pos20; \
725 g20->key_index = key20_index; \
727 g21->match = match21; \
728 g21->match_pos = match_pos21; \
729 g21->key_index = key21_index; \
732 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
735 struct grinder *g30, *g31; \
736 struct rte_mbuf *mbuf30, *mbuf31; \
737 struct bucket *bkt30, *bkt31; \
738 uint8_t *key30, *key31, *key_mem = t->key_mem; \
739 uint8_t *data30, *data31, *data_mem = t->data_mem; \
740 uint64_t match30, match31, match_pos30, match_pos31; \
741 uint64_t match_key30, match_key31, match_keys; \
742 uint32_t key30_index, key31_index; \
743 uint32_t key_size_shl = t->key_size_shl; \
744 uint32_t data_size_shl = t->data_size_shl; \
746 mbuf30 = pkts[pkt30_index]; \
747 g30 = &g[pkt30_index]; \
749 match30 = g30->match; \
750 match_pos30 = g30->match_pos; \
751 key30_index = g30->key_index; \
752 key30 = &key_mem[key30_index << key_size_shl]; \
753 lookup_cmp_key(mbuf30, key30, match_key30, t); \
754 match_key30 <<= pkt30_index; \
755 match_key30 &= match30; \
756 data30 = &data_mem[key30_index << data_size_shl]; \
757 entries[pkt30_index] = data30; \
759 mbuf31 = pkts[pkt31_index]; \
760 g31 = &g[pkt31_index]; \
762 match31 = g31->match; \
763 match_pos31 = g31->match_pos; \
764 key31_index = g31->key_index; \
765 key31 = &key_mem[key31_index << key_size_shl]; \
766 lookup_cmp_key(mbuf31, key31, match_key31, t); \
767 match_key31 <<= pkt31_index; \
768 match_key31 &= match31; \
769 data31 = &data_mem[key31_index << data_size_shl]; \
770 entries[pkt31_index] = data31; \
772 rte_prefetch0(data30); \
773 rte_prefetch0(data31); \
775 match_keys = match_key30 | match_key31; \
776 pkts_mask_out |= match_keys; \
778 if (match_key30 == 0) \
780 lru_update(bkt30, match_pos30); \
782 if (match_key31 == 0) \
784 lru_update(bkt31, match_pos31); \
788 * The lookup function implements a 4-stage pipeline, with each stage processing
789 * two different packets. The purpose of pipelined implementation is to hide the
790 * latency of prefetching the data structures and loosen the data dependency
791 * between instructions.
793 * p00 _______ p10 _______ p20 _______ p30 _______
794 * ----->| |----->| |----->| |----->| |----->
795 * | 0 | | 1 | | 2 | | 3 |
796 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
799 * The naming convention is:
800 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
803 static int rte_table_hash_lru_lookup(
805 struct rte_mbuf **pkts,
807 uint64_t *lookup_hit_mask,
810 struct rte_table_hash *t = (struct rte_table_hash *) table;
811 struct grinder *g = t->grinders;
812 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
813 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
814 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
817 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
818 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
820 /* Cannot run the pipeline with less than 7 packets */
821 if (__builtin_popcountll(pkts_mask) < 7)
822 return rte_table_hash_lru_lookup_unoptimized(table, pkts,
823 pkts_mask, lookup_hit_mask, entries);
825 /* Pipeline stage 0 */
826 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
829 pkt10_index = pkt00_index;
830 pkt11_index = pkt01_index;
832 /* Pipeline stage 0 */
833 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
835 /* Pipeline stage 1 */
836 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
839 pkt20_index = pkt10_index;
840 pkt21_index = pkt11_index;
841 pkt10_index = pkt00_index;
842 pkt11_index = pkt01_index;
844 /* Pipeline stage 0 */
845 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
847 /* Pipeline stage 1 */
848 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
850 /* Pipeline stage 2 */
851 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
857 for ( ; pkts_mask; ) {
859 pkt30_index = pkt20_index;
860 pkt31_index = pkt21_index;
861 pkt20_index = pkt10_index;
862 pkt21_index = pkt11_index;
863 pkt10_index = pkt00_index;
864 pkt11_index = pkt01_index;
866 /* Pipeline stage 0 */
867 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
868 pkt00_index, pkt01_index);
870 /* Pipeline stage 1 */
871 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
873 /* Pipeline stage 2 */
874 lookup2_stage2(t, g, pkt20_index, pkt21_index,
875 pkts_mask_match_many);
877 /* Pipeline stage 3 */
878 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
879 pkts_mask_out, entries);
883 pkt30_index = pkt20_index;
884 pkt31_index = pkt21_index;
885 pkt20_index = pkt10_index;
886 pkt21_index = pkt11_index;
887 pkt10_index = pkt00_index;
888 pkt11_index = pkt01_index;
890 /* Pipeline stage 1 */
891 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
893 /* Pipeline stage 2 */
894 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
896 /* Pipeline stage 3 */
897 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
901 pkt30_index = pkt20_index;
902 pkt31_index = pkt21_index;
903 pkt20_index = pkt10_index;
904 pkt21_index = pkt11_index;
906 /* Pipeline stage 2 */
907 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
909 /* Pipeline stage 3 */
910 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
914 pkt30_index = pkt20_index;
915 pkt31_index = pkt21_index;
917 /* Pipeline stage 3 */
918 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
922 pkts_mask_match_many &= ~pkts_mask_out;
923 if (pkts_mask_match_many) {
924 uint64_t pkts_mask_out_slow = 0;
926 status = rte_table_hash_lru_lookup_unoptimized(table, pkts,
927 pkts_mask_match_many, &pkts_mask_out_slow, entries);
928 pkts_mask_out |= pkts_mask_out_slow;
931 *lookup_hit_mask = pkts_mask_out;
932 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
937 rte_table_hash_lru_stats_read(void *table, struct rte_table_stats *stats, int clear)
939 struct rte_table_hash *t = table;
942 memcpy(stats, &t->stats, sizeof(t->stats));
945 memset(&t->stats, 0, sizeof(t->stats));
950 struct rte_table_ops rte_table_hash_lru_ops = {
951 .f_create = rte_table_hash_lru_create,
952 .f_free = rte_table_hash_lru_free,
953 .f_add = rte_table_hash_lru_entry_add,
954 .f_delete = rte_table_hash_lru_entry_delete,
956 .f_delete_bulk = NULL,
957 .f_lookup = rte_table_hash_lru_lookup,
958 .f_stats = rte_table_hash_lru_stats_read,