1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_malloc.h>
12 #include "rte_table_hash.h"
15 #define KEYS_PER_BUCKET 4
17 #ifdef RTE_TABLE_STATS_COLLECT
19 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val) \
20 table->stats.n_pkts_in += val
21 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val) \
22 table->stats.n_pkts_lookup_miss += val
26 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val)
27 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val)
36 uint16_t sig[KEYS_PER_BUCKET];
37 uint32_t key_pos[KEYS_PER_BUCKET];
48 struct rte_table_hash {
49 struct rte_table_stats stats;
51 /* Input parameters */
56 rte_table_hash_op_hash f_hash;
62 uint32_t key_size_shl;
63 uint32_t data_size_shl;
64 uint32_t key_stack_tos;
67 struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
71 struct bucket *buckets;
77 uint8_t memory[0] __rte_cache_aligned;
81 keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes)
83 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
86 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
87 if (a64[i] != (b64[i] & b_mask64[i]))
94 keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes)
96 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
99 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
100 dst64[i] = src64[i] & src_mask64[i];
104 check_params_create(struct rte_table_hash_params *params)
107 if (params->name == NULL) {
108 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
113 if ((params->key_size < sizeof(uint64_t)) ||
114 (!rte_is_power_of_2(params->key_size))) {
115 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
120 if (params->n_keys == 0) {
121 RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
126 if ((params->n_buckets == 0) ||
127 (!rte_is_power_of_2(params->n_buckets))) {
128 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
133 if (params->f_hash == NULL) {
134 RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
142 rte_table_hash_lru_create(void *params, int socket_id, uint32_t entry_size)
144 struct rte_table_hash_params *p = params;
145 struct rte_table_hash *t;
146 uint64_t table_meta_sz, key_mask_sz, bucket_sz, key_sz, key_stack_sz;
147 uint64_t data_sz, total_size;
148 uint64_t key_mask_offset, bucket_offset, key_offset, key_stack_offset;
149 uint64_t data_offset;
150 uint32_t n_buckets, i;
152 /* Check input parameters */
153 if ((check_params_create(p) != 0) ||
154 (!rte_is_power_of_2(entry_size)) ||
155 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
156 (sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2))) {
163 * Objective: Pick the number of buckets (n_buckets) so that there a chance
164 * to store n_keys keys in the table.
166 * Note: Since the buckets do not get extended, it is not possible to
167 * guarantee that n_keys keys can be stored in the table at any time. In the
168 * worst case scenario when all the n_keys fall into the same bucket, only
169 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
170 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
171 * n_keys to n_buckets ratio.
173 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
175 n_buckets = rte_align32pow2(
176 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
177 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
179 /* Memory allocation */
180 table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
181 key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size);
182 bucket_sz = RTE_CACHE_LINE_ROUNDUP(n_buckets * sizeof(struct bucket));
183 key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
184 key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
185 data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
186 total_size = table_meta_sz + key_mask_sz + bucket_sz + key_sz +
187 key_stack_sz + data_sz;
189 if (total_size > SIZE_MAX) {
191 "%s: Cannot allocate %" PRIu64 " bytes for hash "
193 __func__, total_size, p->name);
197 t = rte_zmalloc_socket(p->name,
203 "%s: Cannot allocate %" PRIu64 " bytes for hash "
205 __func__, total_size, p->name);
208 RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table %s memory footprint"
209 " is %" PRIu64 " bytes\n",
210 __func__, p->key_size, p->name, total_size);
212 /* Memory initialization */
213 t->key_size = p->key_size;
214 t->entry_size = entry_size;
215 t->n_keys = p->n_keys;
216 t->n_buckets = n_buckets;
217 t->f_hash = p->f_hash;
219 t->key_offset = p->key_offset;
222 t->bucket_mask = t->n_buckets - 1;
223 t->key_size_shl = __builtin_ctzl(p->key_size);
224 t->data_size_shl = __builtin_ctzl(entry_size);
228 bucket_offset = key_mask_offset + key_mask_sz;
229 key_offset = bucket_offset + bucket_sz;
230 key_stack_offset = key_offset + key_sz;
231 data_offset = key_stack_offset + key_stack_sz;
233 t->key_mask = (uint64_t *) &t->memory[key_mask_offset];
234 t->buckets = (struct bucket *) &t->memory[bucket_offset];
235 t->key_mem = &t->memory[key_offset];
236 t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
237 t->data_mem = &t->memory[data_offset];
240 if (p->key_mask == NULL)
241 memset(t->key_mask, 0xFF, p->key_size);
243 memcpy(t->key_mask, p->key_mask, p->key_size);
246 for (i = 0; i < t->n_keys; i++)
247 t->key_stack[i] = t->n_keys - 1 - i;
248 t->key_stack_tos = t->n_keys;
251 for (i = 0; i < t->n_buckets; i++) {
252 struct bucket *bkt = &t->buckets[i];
261 rte_table_hash_lru_free(void *table)
263 struct rte_table_hash *t = table;
265 /* Check input parameters */
274 rte_table_hash_lru_entry_add(void *table, void *key, void *entry,
275 int *key_found, void **entry_ptr)
277 struct rte_table_hash *t = table;
280 uint32_t bkt_index, i;
282 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
283 bkt_index = sig & t->bucket_mask;
284 bkt = &t->buckets[bkt_index];
285 sig = (sig >> 16) | 1LLU;
287 /* Key is present in the bucket */
288 for (i = 0; i < KEYS_PER_BUCKET; i++) {
289 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
290 uint32_t bkt_key_index = bkt->key_pos[i];
291 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
294 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
295 t->key_size) == 0)) {
296 uint8_t *data = &t->data_mem[bkt_key_index <<
299 memcpy(data, entry, t->entry_size);
302 *entry_ptr = (void *) data;
307 /* Key is not present in the bucket */
308 for (i = 0; i < KEYS_PER_BUCKET; i++) {
309 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
312 uint32_t bkt_key_index;
313 uint8_t *bkt_key, *data;
315 /* Allocate new key */
316 if (t->key_stack_tos == 0) {
317 /* No keys available */
320 bkt_key_index = t->key_stack[--t->key_stack_tos];
322 /* Install new key */
323 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
324 data = &t->data_mem[bkt_key_index << t->data_size_shl];
326 bkt->sig[i] = (uint16_t) sig;
327 bkt->key_pos[i] = bkt_key_index;
328 keycpy(bkt_key, key, t->key_mask, t->key_size);
329 memcpy(data, entry, t->entry_size);
333 *entry_ptr = (void *) data;
340 uint64_t pos = lru_pos(bkt);
341 uint32_t bkt_key_index = bkt->key_pos[pos];
342 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
344 uint8_t *data = &t->data_mem[bkt_key_index << t->data_size_shl];
346 bkt->sig[pos] = (uint16_t) sig;
347 keycpy(bkt_key, key, t->key_mask, t->key_size);
348 memcpy(data, entry, t->entry_size);
349 lru_update(bkt, pos);
352 *entry_ptr = (void *) data;
358 rte_table_hash_lru_entry_delete(void *table, void *key, int *key_found,
361 struct rte_table_hash *t = table;
364 uint32_t bkt_index, i;
366 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
367 bkt_index = sig & t->bucket_mask;
368 bkt = &t->buckets[bkt_index];
369 sig = (sig >> 16) | 1LLU;
371 /* Key is present in the bucket */
372 for (i = 0; i < KEYS_PER_BUCKET; i++) {
373 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
374 uint32_t bkt_key_index = bkt->key_pos[i];
375 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
378 if ((sig == bkt_sig) &&
379 (keycmp(bkt_key, key, t->key_mask, t->key_size) == 0)) {
380 uint8_t *data = &t->data_mem[bkt_key_index <<
384 t->key_stack[t->key_stack_tos++] = bkt_key_index;
387 memcpy(entry, data, t->entry_size);
392 /* Key is not present in the bucket */
397 static int rte_table_hash_lru_lookup_unoptimized(
399 struct rte_mbuf **pkts,
401 uint64_t *lookup_hit_mask,
404 struct rte_table_hash *t = (struct rte_table_hash *) table;
405 uint64_t pkts_mask_out = 0;
407 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
408 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
410 for ( ; pkts_mask; ) {
412 struct rte_mbuf *pkt;
414 uint64_t pkt_mask, sig;
415 uint32_t pkt_index, bkt_index, i;
417 pkt_index = __builtin_ctzll(pkts_mask);
418 pkt_mask = 1LLU << pkt_index;
419 pkts_mask &= ~pkt_mask;
421 pkt = pkts[pkt_index];
422 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
423 sig = (uint64_t) t->f_hash(key, t->key_mask, t->key_size, t->seed);
425 bkt_index = sig & t->bucket_mask;
426 bkt = &t->buckets[bkt_index];
427 sig = (sig >> 16) | 1LLU;
429 /* Key is present in the bucket */
430 for (i = 0; i < KEYS_PER_BUCKET; i++) {
431 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
432 uint32_t bkt_key_index = bkt->key_pos[i];
433 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
436 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
437 t->key_size) == 0)) {
438 uint8_t *data = &t->data_mem[bkt_key_index <<
442 pkts_mask_out |= pkt_mask;
443 entries[pkt_index] = (void *) data;
449 *lookup_hit_mask = pkts_mask_out;
450 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
456 * mask = match bitmask
457 * match = at least one match
458 * match_many = more than one match
459 * match_pos = position of first match
461 * ----------------------------------------
462 * mask match match_many match_pos
463 * ----------------------------------------
468 * ----------------------------------------
473 * ----------------------------------------
478 * ----------------------------------------
483 * ----------------------------------------
485 * match = 1111_1111_1111_1110
486 * match_many = 1111_1110_1110_1000
487 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
490 * match_many = 0xFEE8LLU
491 * match_pos = 0x12131210LLU
495 #define LUT_MATCH 0xFFFELLU
496 #define LUT_MATCH_MANY 0xFEE8LLU
497 #define LUT_MATCH_POS 0x12131210LLU
499 #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos)\
501 uint64_t bucket_sig[4], mask[4], mask_all; \
503 bucket_sig[0] = bucket->sig[0]; \
504 bucket_sig[1] = bucket->sig[1]; \
505 bucket_sig[2] = bucket->sig[2]; \
506 bucket_sig[3] = bucket->sig[3]; \
508 bucket_sig[0] ^= mbuf_sig; \
509 bucket_sig[1] ^= mbuf_sig; \
510 bucket_sig[2] ^= mbuf_sig; \
511 bucket_sig[3] ^= mbuf_sig; \
518 if (bucket_sig[0] == 0) \
520 if (bucket_sig[1] == 0) \
522 if (bucket_sig[2] == 0) \
524 if (bucket_sig[3] == 0) \
527 mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \
529 match = (LUT_MATCH >> mask_all) & 1; \
530 match_many = (LUT_MATCH_MANY >> mask_all) & 1; \
531 match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \
534 #define lookup_cmp_key(mbuf, key, match_key, f) \
536 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
537 uint64_t *bkt_key = (uint64_t *) key; \
538 uint64_t *key_mask = f->key_mask; \
540 switch (f->key_size) { \
543 uint64_t xor = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
552 uint64_t xor[2], or; \
554 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
555 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
556 or = xor[0] | xor[1]; \
565 uint64_t xor[4], or; \
567 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
568 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
569 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
570 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
571 or = xor[0] | xor[1] | xor[2] | xor[3]; \
580 uint64_t xor[8], or; \
582 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
583 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
584 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
585 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
586 xor[4] = (pkt_key[4] & key_mask[4]) ^ bkt_key[4]; \
587 xor[5] = (pkt_key[5] & key_mask[5]) ^ bkt_key[5]; \
588 xor[6] = (pkt_key[6] & key_mask[6]) ^ bkt_key[6]; \
589 xor[7] = (pkt_key[7] & key_mask[7]) ^ bkt_key[7]; \
590 or = xor[0] | xor[1] | xor[2] | xor[3] | \
591 xor[4] | xor[5] | xor[6] | xor[7]; \
600 if (keycmp(bkt_key, pkt_key, key_mask, f->key_size) == 0) \
605 #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index)\
607 uint64_t pkt00_mask, pkt01_mask; \
608 struct rte_mbuf *mbuf00, *mbuf01; \
609 uint32_t key_offset = t->key_offset; \
611 pkt00_index = __builtin_ctzll(pkts_mask); \
612 pkt00_mask = 1LLU << pkt00_index; \
613 pkts_mask &= ~pkt00_mask; \
614 mbuf00 = pkts[pkt00_index]; \
616 pkt01_index = __builtin_ctzll(pkts_mask); \
617 pkt01_mask = 1LLU << pkt01_index; \
618 pkts_mask &= ~pkt01_mask; \
619 mbuf01 = pkts[pkt01_index]; \
621 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
622 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
625 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
628 uint64_t pkt00_mask, pkt01_mask; \
629 struct rte_mbuf *mbuf00, *mbuf01; \
630 uint32_t key_offset = t->key_offset; \
632 pkt00_index = __builtin_ctzll(pkts_mask); \
633 pkt00_mask = 1LLU << pkt00_index; \
634 pkts_mask &= ~pkt00_mask; \
635 mbuf00 = pkts[pkt00_index]; \
637 pkt01_index = __builtin_ctzll(pkts_mask); \
638 if (pkts_mask == 0) \
639 pkt01_index = pkt00_index; \
641 pkt01_mask = 1LLU << pkt01_index; \
642 pkts_mask &= ~pkt01_mask; \
643 mbuf01 = pkts[pkt01_index]; \
645 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
646 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
649 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index)\
651 struct grinder *g10, *g11; \
652 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
653 struct rte_mbuf *mbuf10, *mbuf11; \
654 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
655 uint8_t *key10, *key11; \
656 uint64_t bucket_mask = t->bucket_mask; \
657 rte_table_hash_op_hash f_hash = t->f_hash; \
658 uint64_t seed = t->seed; \
659 uint32_t key_size = t->key_size; \
660 uint32_t key_offset = t->key_offset; \
662 mbuf10 = pkts[pkt10_index]; \
663 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset);\
664 sig10 = (uint64_t) f_hash(key10, t->key_mask, key_size, seed);\
665 bkt10_index = sig10 & bucket_mask; \
666 bkt10 = &buckets[bkt10_index]; \
668 mbuf11 = pkts[pkt11_index]; \
669 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset);\
670 sig11 = (uint64_t) f_hash(key11, t->key_mask, key_size, seed);\
671 bkt11_index = sig11 & bucket_mask; \
672 bkt11 = &buckets[bkt11_index]; \
674 rte_prefetch0(bkt10); \
675 rte_prefetch0(bkt11); \
677 g10 = &g[pkt10_index]; \
681 g11 = &g[pkt11_index]; \
686 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
688 struct grinder *g20, *g21; \
689 uint64_t sig20, sig21; \
690 struct bucket *bkt20, *bkt21; \
691 uint8_t *key20, *key21, *key_mem = t->key_mem; \
692 uint64_t match20, match21, match_many20, match_many21; \
693 uint64_t match_pos20, match_pos21; \
694 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
696 g20 = &g[pkt20_index]; \
699 sig20 = (sig20 >> 16) | 1LLU; \
700 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
701 match20 <<= pkt20_index; \
702 match_many20 <<= pkt20_index; \
703 key20_index = bkt20->key_pos[match_pos20]; \
704 key20 = &key_mem[key20_index << key_size_shl]; \
706 g21 = &g[pkt21_index]; \
709 sig21 = (sig21 >> 16) | 1LLU; \
710 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
711 match21 <<= pkt21_index; \
712 match_many21 <<= pkt21_index; \
713 key21_index = bkt21->key_pos[match_pos21]; \
714 key21 = &key_mem[key21_index << key_size_shl]; \
716 rte_prefetch0(key20); \
717 rte_prefetch0(key21); \
719 pkts_mask_match_many |= match_many20 | match_many21; \
721 g20->match = match20; \
722 g20->match_pos = match_pos20; \
723 g20->key_index = key20_index; \
725 g21->match = match21; \
726 g21->match_pos = match_pos21; \
727 g21->key_index = key21_index; \
730 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
733 struct grinder *g30, *g31; \
734 struct rte_mbuf *mbuf30, *mbuf31; \
735 struct bucket *bkt30, *bkt31; \
736 uint8_t *key30, *key31, *key_mem = t->key_mem; \
737 uint8_t *data30, *data31, *data_mem = t->data_mem; \
738 uint64_t match30, match31, match_pos30, match_pos31; \
739 uint64_t match_key30, match_key31, match_keys; \
740 uint32_t key30_index, key31_index; \
741 uint32_t key_size_shl = t->key_size_shl; \
742 uint32_t data_size_shl = t->data_size_shl; \
744 mbuf30 = pkts[pkt30_index]; \
745 g30 = &g[pkt30_index]; \
747 match30 = g30->match; \
748 match_pos30 = g30->match_pos; \
749 key30_index = g30->key_index; \
750 key30 = &key_mem[key30_index << key_size_shl]; \
751 lookup_cmp_key(mbuf30, key30, match_key30, t); \
752 match_key30 <<= pkt30_index; \
753 match_key30 &= match30; \
754 data30 = &data_mem[key30_index << data_size_shl]; \
755 entries[pkt30_index] = data30; \
757 mbuf31 = pkts[pkt31_index]; \
758 g31 = &g[pkt31_index]; \
760 match31 = g31->match; \
761 match_pos31 = g31->match_pos; \
762 key31_index = g31->key_index; \
763 key31 = &key_mem[key31_index << key_size_shl]; \
764 lookup_cmp_key(mbuf31, key31, match_key31, t); \
765 match_key31 <<= pkt31_index; \
766 match_key31 &= match31; \
767 data31 = &data_mem[key31_index << data_size_shl]; \
768 entries[pkt31_index] = data31; \
770 rte_prefetch0(data30); \
771 rte_prefetch0(data31); \
773 match_keys = match_key30 | match_key31; \
774 pkts_mask_out |= match_keys; \
776 if (match_key30 == 0) \
778 lru_update(bkt30, match_pos30); \
780 if (match_key31 == 0) \
782 lru_update(bkt31, match_pos31); \
786 * The lookup function implements a 4-stage pipeline, with each stage processing
787 * two different packets. The purpose of pipelined implementation is to hide the
788 * latency of prefetching the data structures and loosen the data dependency
789 * between instructions.
791 * p00 _______ p10 _______ p20 _______ p30 _______
792 * ----->| |----->| |----->| |----->| |----->
793 * | 0 | | 1 | | 2 | | 3 |
794 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
797 * The naming convention is:
798 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
801 static int rte_table_hash_lru_lookup(
803 struct rte_mbuf **pkts,
805 uint64_t *lookup_hit_mask,
808 struct rte_table_hash *t = (struct rte_table_hash *) table;
809 struct grinder *g = t->grinders;
810 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
811 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
812 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
815 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
816 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
818 /* Cannot run the pipeline with less than 7 packets */
819 if (__builtin_popcountll(pkts_mask) < 7)
820 return rte_table_hash_lru_lookup_unoptimized(table, pkts,
821 pkts_mask, lookup_hit_mask, entries);
823 /* Pipeline stage 0 */
824 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
827 pkt10_index = pkt00_index;
828 pkt11_index = pkt01_index;
830 /* Pipeline stage 0 */
831 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
833 /* Pipeline stage 1 */
834 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
837 pkt20_index = pkt10_index;
838 pkt21_index = pkt11_index;
839 pkt10_index = pkt00_index;
840 pkt11_index = pkt01_index;
842 /* Pipeline stage 0 */
843 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
845 /* Pipeline stage 1 */
846 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
848 /* Pipeline stage 2 */
849 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
855 for ( ; pkts_mask; ) {
857 pkt30_index = pkt20_index;
858 pkt31_index = pkt21_index;
859 pkt20_index = pkt10_index;
860 pkt21_index = pkt11_index;
861 pkt10_index = pkt00_index;
862 pkt11_index = pkt01_index;
864 /* Pipeline stage 0 */
865 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
866 pkt00_index, pkt01_index);
868 /* Pipeline stage 1 */
869 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
871 /* Pipeline stage 2 */
872 lookup2_stage2(t, g, pkt20_index, pkt21_index,
873 pkts_mask_match_many);
875 /* Pipeline stage 3 */
876 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
877 pkts_mask_out, entries);
881 pkt30_index = pkt20_index;
882 pkt31_index = pkt21_index;
883 pkt20_index = pkt10_index;
884 pkt21_index = pkt11_index;
885 pkt10_index = pkt00_index;
886 pkt11_index = pkt01_index;
888 /* Pipeline stage 1 */
889 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
891 /* Pipeline stage 2 */
892 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
894 /* Pipeline stage 3 */
895 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
899 pkt30_index = pkt20_index;
900 pkt31_index = pkt21_index;
901 pkt20_index = pkt10_index;
902 pkt21_index = pkt11_index;
904 /* Pipeline stage 2 */
905 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
907 /* Pipeline stage 3 */
908 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
912 pkt30_index = pkt20_index;
913 pkt31_index = pkt21_index;
915 /* Pipeline stage 3 */
916 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
920 pkts_mask_match_many &= ~pkts_mask_out;
921 if (pkts_mask_match_many) {
922 uint64_t pkts_mask_out_slow = 0;
924 status = rte_table_hash_lru_lookup_unoptimized(table, pkts,
925 pkts_mask_match_many, &pkts_mask_out_slow, entries);
926 pkts_mask_out |= pkts_mask_out_slow;
929 *lookup_hit_mask = pkts_mask_out;
930 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
935 rte_table_hash_lru_stats_read(void *table, struct rte_table_stats *stats, int clear)
937 struct rte_table_hash *t = table;
940 memcpy(stats, &t->stats, sizeof(t->stats));
943 memset(&t->stats, 0, sizeof(t->stats));
948 struct rte_table_ops rte_table_hash_lru_ops = {
949 .f_create = rte_table_hash_lru_create,
950 .f_free = rte_table_hash_lru_free,
951 .f_add = rte_table_hash_lru_entry_add,
952 .f_delete = rte_table_hash_lru_entry_delete,
954 .f_delete_bulk = NULL,
955 .f_lookup = rte_table_hash_lru_lookup,
956 .f_stats = rte_table_hash_lru_stats_read,