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.
37 #include <rte_common.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
43 #include "rte_table_hash.h"
46 #define KEYS_PER_BUCKET 4
48 #ifdef RTE_TABLE_STATS_COLLECT
50 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val) \
51 table->stats.n_pkts_in += val
52 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val) \
53 table->stats.n_pkts_lookup_miss += val
57 #define RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(table, val)
58 #define RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(table, val)
67 uint16_t sig[KEYS_PER_BUCKET];
68 uint32_t key_pos[KEYS_PER_BUCKET];
79 struct rte_table_hash {
80 struct rte_table_stats stats;
82 /* Input parameters */
87 rte_table_hash_op_hash f_hash;
93 uint32_t key_size_shl;
94 uint32_t data_size_shl;
95 uint32_t key_stack_tos;
98 struct grinder grinders[RTE_PORT_IN_BURST_SIZE_MAX];
102 struct bucket *buckets;
108 uint8_t memory[0] __rte_cache_aligned;
112 keycmp(void *a, void *b, void *b_mask, uint32_t n_bytes)
114 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
117 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
118 if (a64[i] != (b64[i] & b_mask64[i]))
125 keycpy(void *dst, void *src, void *src_mask, uint32_t n_bytes)
127 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
130 for (i = 0; i < n_bytes / sizeof(uint64_t); i++)
131 dst64[i] = src64[i] & src_mask64[i];
135 check_params_create(struct rte_table_hash_params *params)
138 if (params->name == NULL) {
139 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
144 if ((params->key_size < sizeof(uint64_t)) ||
145 (!rte_is_power_of_2(params->key_size))) {
146 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
151 if (params->n_keys == 0) {
152 RTE_LOG(ERR, TABLE, "%s: n_keys invalid value\n", __func__);
157 if ((params->n_buckets == 0) ||
158 (!rte_is_power_of_2(params->n_buckets))) {
159 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
164 if (params->f_hash == NULL) {
165 RTE_LOG(ERR, TABLE, "%s: f_hash invalid value\n", __func__);
173 rte_table_hash_lru_create(void *params, int socket_id, uint32_t entry_size)
175 struct rte_table_hash_params *p = params;
176 struct rte_table_hash *t;
177 uint64_t table_meta_sz, key_mask_sz, bucket_sz, key_sz, key_stack_sz;
178 uint64_t data_sz, total_size;
179 uint64_t key_mask_offset, bucket_offset, key_offset, key_stack_offset;
180 uint64_t data_offset;
181 uint32_t n_buckets, i;
183 /* Check input parameters */
184 if ((check_params_create(p) != 0) ||
185 (!rte_is_power_of_2(entry_size)) ||
186 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
187 (sizeof(struct bucket) != (RTE_CACHE_LINE_SIZE / 2))) {
194 * Objective: Pick the number of buckets (n_buckets) so that there a chance
195 * to store n_keys keys in the table.
197 * Note: Since the buckets do not get extended, it is not possible to
198 * guarantee that n_keys keys can be stored in the table at any time. In the
199 * worst case scenario when all the n_keys fall into the same bucket, only
200 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
201 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
202 * n_keys to n_buckets ratio.
204 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
206 n_buckets = rte_align32pow2(
207 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
208 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
210 /* Memory allocation */
211 table_meta_sz = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_table_hash));
212 key_mask_sz = RTE_CACHE_LINE_ROUNDUP(p->key_size);
213 bucket_sz = RTE_CACHE_LINE_ROUNDUP(n_buckets * sizeof(struct bucket));
214 key_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * p->key_size);
215 key_stack_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * sizeof(uint32_t));
216 data_sz = RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
217 total_size = table_meta_sz + key_mask_sz + bucket_sz + key_sz +
218 key_stack_sz + data_sz;
220 if (total_size > SIZE_MAX) {
222 "%s: Cannot allocate %" PRIu64 " bytes for hash "
224 __func__, total_size, p->name);
228 t = rte_zmalloc_socket(p->name,
234 "%s: Cannot allocate %" PRIu64 " bytes for hash "
236 __func__, total_size, p->name);
239 RTE_LOG(INFO, TABLE, "%s (%u-byte key): Hash table %s memory footprint"
240 " is %" PRIu64 " bytes\n",
241 __func__, p->key_size, p->name, total_size);
243 /* Memory initialization */
244 t->key_size = p->key_size;
245 t->entry_size = entry_size;
246 t->n_keys = p->n_keys;
247 t->n_buckets = n_buckets;
248 t->f_hash = p->f_hash;
250 t->key_offset = p->key_offset;
253 t->bucket_mask = t->n_buckets - 1;
254 t->key_size_shl = __builtin_ctzl(p->key_size);
255 t->data_size_shl = __builtin_ctzl(entry_size);
259 bucket_offset = key_mask_offset + key_mask_sz;
260 key_offset = bucket_offset + bucket_sz;
261 key_stack_offset = key_offset + key_sz;
262 data_offset = key_stack_offset + key_stack_sz;
264 t->key_mask = (uint64_t *) &t->memory[key_mask_offset];
265 t->buckets = (struct bucket *) &t->memory[bucket_offset];
266 t->key_mem = &t->memory[key_offset];
267 t->key_stack = (uint32_t *) &t->memory[key_stack_offset];
268 t->data_mem = &t->memory[data_offset];
271 if (p->key_mask == NULL)
272 memset(t->key_mask, 0xFF, p->key_size);
274 memcpy(t->key_mask, p->key_mask, p->key_size);
277 for (i = 0; i < t->n_keys; i++)
278 t->key_stack[i] = t->n_keys - 1 - i;
279 t->key_stack_tos = t->n_keys;
282 for (i = 0; i < t->n_buckets; i++) {
283 struct bucket *bkt = &t->buckets[i];
292 rte_table_hash_lru_free(void *table)
294 struct rte_table_hash *t = table;
296 /* Check input parameters */
305 rte_table_hash_lru_entry_add(void *table, void *key, void *entry,
306 int *key_found, void **entry_ptr)
308 struct rte_table_hash *t = table;
311 uint32_t bkt_index, i;
313 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
314 bkt_index = sig & t->bucket_mask;
315 bkt = &t->buckets[bkt_index];
316 sig = (sig >> 16) | 1LLU;
318 /* Key is present in the bucket */
319 for (i = 0; i < KEYS_PER_BUCKET; i++) {
320 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
321 uint32_t bkt_key_index = bkt->key_pos[i];
322 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
325 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
326 t->key_size) == 0)) {
327 uint8_t *data = &t->data_mem[bkt_key_index <<
330 memcpy(data, entry, t->entry_size);
333 *entry_ptr = (void *) data;
338 /* Key is not present in the bucket */
339 for (i = 0; i < KEYS_PER_BUCKET; i++) {
340 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
343 uint32_t bkt_key_index;
344 uint8_t *bkt_key, *data;
346 /* Allocate new key */
347 if (t->key_stack_tos == 0) {
348 /* No keys available */
351 bkt_key_index = t->key_stack[--t->key_stack_tos];
353 /* Install new key */
354 bkt_key = &t->key_mem[bkt_key_index << t->key_size_shl];
355 data = &t->data_mem[bkt_key_index << t->data_size_shl];
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);
364 *entry_ptr = (void *) data;
371 uint64_t pos = lru_pos(bkt);
372 uint32_t bkt_key_index = bkt->key_pos[pos];
373 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
375 uint8_t *data = &t->data_mem[bkt_key_index << t->data_size_shl];
377 bkt->sig[pos] = (uint16_t) sig;
378 keycpy(bkt_key, key, t->key_mask, t->key_size);
379 memcpy(data, entry, t->entry_size);
380 lru_update(bkt, pos);
383 *entry_ptr = (void *) data;
389 rte_table_hash_lru_entry_delete(void *table, void *key, int *key_found,
392 struct rte_table_hash *t = table;
395 uint32_t bkt_index, i;
397 sig = t->f_hash(key, t->key_mask, t->key_size, t->seed);
398 bkt_index = sig & t->bucket_mask;
399 bkt = &t->buckets[bkt_index];
400 sig = (sig >> 16) | 1LLU;
402 /* Key is present in the bucket */
403 for (i = 0; i < KEYS_PER_BUCKET; i++) {
404 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
405 uint32_t bkt_key_index = bkt->key_pos[i];
406 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
409 if ((sig == bkt_sig) &&
410 (keycmp(bkt_key, key, t->key_mask, t->key_size) == 0)) {
411 uint8_t *data = &t->data_mem[bkt_key_index <<
415 t->key_stack[t->key_stack_tos++] = bkt_key_index;
418 memcpy(entry, data, t->entry_size);
423 /* Key is not present in the bucket */
428 static int rte_table_hash_lru_lookup_unoptimized(
430 struct rte_mbuf **pkts,
432 uint64_t *lookup_hit_mask,
435 struct rte_table_hash *t = (struct rte_table_hash *) table;
436 uint64_t pkts_mask_out = 0;
438 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
439 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
441 for ( ; pkts_mask; ) {
443 struct rte_mbuf *pkt;
445 uint64_t pkt_mask, sig;
446 uint32_t pkt_index, bkt_index, i;
448 pkt_index = __builtin_ctzll(pkts_mask);
449 pkt_mask = 1LLU << pkt_index;
450 pkts_mask &= ~pkt_mask;
452 pkt = pkts[pkt_index];
453 key = RTE_MBUF_METADATA_UINT8_PTR(pkt, t->key_offset);
454 sig = (uint64_t) t->f_hash(key, t->key_mask, t->key_size, t->seed);
456 bkt_index = sig & t->bucket_mask;
457 bkt = &t->buckets[bkt_index];
458 sig = (sig >> 16) | 1LLU;
460 /* Key is present in the bucket */
461 for (i = 0; i < KEYS_PER_BUCKET; i++) {
462 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
463 uint32_t bkt_key_index = bkt->key_pos[i];
464 uint8_t *bkt_key = &t->key_mem[bkt_key_index <<
467 if ((sig == bkt_sig) && (keycmp(bkt_key, key, t->key_mask,
468 t->key_size) == 0)) {
469 uint8_t *data = &t->data_mem[bkt_key_index <<
473 pkts_mask_out |= pkt_mask;
474 entries[pkt_index] = (void *) data;
480 *lookup_hit_mask = pkts_mask_out;
481 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
487 * mask = match bitmask
488 * match = at least one match
489 * match_many = more than one match
490 * match_pos = position of first match
492 * ----------------------------------------
493 * mask match match_many match_pos
494 * ----------------------------------------
499 * ----------------------------------------
504 * ----------------------------------------
509 * ----------------------------------------
514 * ----------------------------------------
516 * match = 1111_1111_1111_1110
517 * match_many = 1111_1110_1110_1000
518 * match_pos = 0001_0010_0001_0011__0001_0010_0001_0000
521 * match_many = 0xFEE8LLU
522 * match_pos = 0x12131210LLU
526 #define LUT_MATCH 0xFFFELLU
527 #define LUT_MATCH_MANY 0xFEE8LLU
528 #define LUT_MATCH_POS 0x12131210LLU
530 #define lookup_cmp_sig(mbuf_sig, bucket, match, match_many, match_pos)\
532 uint64_t bucket_sig[4], mask[4], mask_all; \
534 bucket_sig[0] = bucket->sig[0]; \
535 bucket_sig[1] = bucket->sig[1]; \
536 bucket_sig[2] = bucket->sig[2]; \
537 bucket_sig[3] = bucket->sig[3]; \
539 bucket_sig[0] ^= mbuf_sig; \
540 bucket_sig[1] ^= mbuf_sig; \
541 bucket_sig[2] ^= mbuf_sig; \
542 bucket_sig[3] ^= mbuf_sig; \
549 if (bucket_sig[0] == 0) \
551 if (bucket_sig[1] == 0) \
553 if (bucket_sig[2] == 0) \
555 if (bucket_sig[3] == 0) \
558 mask_all = (mask[0] | mask[1]) | (mask[2] | mask[3]); \
560 match = (LUT_MATCH >> mask_all) & 1; \
561 match_many = (LUT_MATCH_MANY >> mask_all) & 1; \
562 match_pos = (LUT_MATCH_POS >> (mask_all << 1)) & 3; \
565 #define lookup_cmp_key(mbuf, key, match_key, f) \
567 uint64_t *pkt_key = RTE_MBUF_METADATA_UINT64_PTR(mbuf, f->key_offset);\
568 uint64_t *bkt_key = (uint64_t *) key; \
569 uint64_t *key_mask = f->key_mask; \
571 switch (f->key_size) { \
574 uint64_t xor = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
583 uint64_t xor[2], or; \
585 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
586 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
587 or = xor[0] | xor[1]; \
596 uint64_t xor[4], or; \
598 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
599 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
600 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
601 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
602 or = xor[0] | xor[1] | xor[2] | xor[3]; \
611 uint64_t xor[8], or; \
613 xor[0] = (pkt_key[0] & key_mask[0]) ^ bkt_key[0]; \
614 xor[1] = (pkt_key[1] & key_mask[1]) ^ bkt_key[1]; \
615 xor[2] = (pkt_key[2] & key_mask[2]) ^ bkt_key[2]; \
616 xor[3] = (pkt_key[3] & key_mask[3]) ^ bkt_key[3]; \
617 xor[4] = (pkt_key[4] & key_mask[4]) ^ bkt_key[4]; \
618 xor[5] = (pkt_key[5] & key_mask[5]) ^ bkt_key[5]; \
619 xor[6] = (pkt_key[6] & key_mask[6]) ^ bkt_key[6]; \
620 xor[7] = (pkt_key[7] & key_mask[7]) ^ bkt_key[7]; \
621 or = xor[0] | xor[1] | xor[2] | xor[3] | \
622 xor[4] | xor[5] | xor[6] | xor[7]; \
631 if (keycmp(bkt_key, pkt_key, key_mask, f->key_size) == 0) \
636 #define lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index)\
638 uint64_t pkt00_mask, pkt01_mask; \
639 struct rte_mbuf *mbuf00, *mbuf01; \
640 uint32_t key_offset = t->key_offset; \
642 pkt00_index = __builtin_ctzll(pkts_mask); \
643 pkt00_mask = 1LLU << pkt00_index; \
644 pkts_mask &= ~pkt00_mask; \
645 mbuf00 = pkts[pkt00_index]; \
647 pkt01_index = __builtin_ctzll(pkts_mask); \
648 pkt01_mask = 1LLU << pkt01_index; \
649 pkts_mask &= ~pkt01_mask; \
650 mbuf01 = pkts[pkt01_index]; \
652 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
653 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
656 #define lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask, pkt00_index, \
659 uint64_t pkt00_mask, pkt01_mask; \
660 struct rte_mbuf *mbuf00, *mbuf01; \
661 uint32_t key_offset = t->key_offset; \
663 pkt00_index = __builtin_ctzll(pkts_mask); \
664 pkt00_mask = 1LLU << pkt00_index; \
665 pkts_mask &= ~pkt00_mask; \
666 mbuf00 = pkts[pkt00_index]; \
668 pkt01_index = __builtin_ctzll(pkts_mask); \
669 if (pkts_mask == 0) \
670 pkt01_index = pkt00_index; \
672 pkt01_mask = 1LLU << pkt01_index; \
673 pkts_mask &= ~pkt01_mask; \
674 mbuf01 = pkts[pkt01_index]; \
676 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
677 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
680 #define lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index)\
682 struct grinder *g10, *g11; \
683 uint64_t sig10, sig11, bkt10_index, bkt11_index; \
684 struct rte_mbuf *mbuf10, *mbuf11; \
685 struct bucket *bkt10, *bkt11, *buckets = t->buckets; \
686 uint8_t *key10, *key11; \
687 uint64_t bucket_mask = t->bucket_mask; \
688 rte_table_hash_op_hash f_hash = t->f_hash; \
689 uint64_t seed = t->seed; \
690 uint32_t key_size = t->key_size; \
691 uint32_t key_offset = t->key_offset; \
693 mbuf10 = pkts[pkt10_index]; \
694 key10 = RTE_MBUF_METADATA_UINT8_PTR(mbuf10, key_offset);\
695 sig10 = (uint64_t) f_hash(key10, t->key_mask, key_size, seed);\
696 bkt10_index = sig10 & bucket_mask; \
697 bkt10 = &buckets[bkt10_index]; \
699 mbuf11 = pkts[pkt11_index]; \
700 key11 = RTE_MBUF_METADATA_UINT8_PTR(mbuf11, key_offset);\
701 sig11 = (uint64_t) f_hash(key11, t->key_mask, key_size, seed);\
702 bkt11_index = sig11 & bucket_mask; \
703 bkt11 = &buckets[bkt11_index]; \
705 rte_prefetch0(bkt10); \
706 rte_prefetch0(bkt11); \
708 g10 = &g[pkt10_index]; \
712 g11 = &g[pkt11_index]; \
717 #define lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many)\
719 struct grinder *g20, *g21; \
720 uint64_t sig20, sig21; \
721 struct bucket *bkt20, *bkt21; \
722 uint8_t *key20, *key21, *key_mem = t->key_mem; \
723 uint64_t match20, match21, match_many20, match_many21; \
724 uint64_t match_pos20, match_pos21; \
725 uint32_t key20_index, key21_index, key_size_shl = t->key_size_shl;\
727 g20 = &g[pkt20_index]; \
730 sig20 = (sig20 >> 16) | 1LLU; \
731 lookup_cmp_sig(sig20, bkt20, match20, match_many20, match_pos20);\
732 match20 <<= pkt20_index; \
733 match_many20 <<= pkt20_index; \
734 key20_index = bkt20->key_pos[match_pos20]; \
735 key20 = &key_mem[key20_index << key_size_shl]; \
737 g21 = &g[pkt21_index]; \
740 sig21 = (sig21 >> 16) | 1LLU; \
741 lookup_cmp_sig(sig21, bkt21, match21, match_many21, match_pos21);\
742 match21 <<= pkt21_index; \
743 match_many21 <<= pkt21_index; \
744 key21_index = bkt21->key_pos[match_pos21]; \
745 key21 = &key_mem[key21_index << key_size_shl]; \
747 rte_prefetch0(key20); \
748 rte_prefetch0(key21); \
750 pkts_mask_match_many |= match_many20 | match_many21; \
752 g20->match = match20; \
753 g20->match_pos = match_pos20; \
754 g20->key_index = key20_index; \
756 g21->match = match21; \
757 g21->match_pos = match_pos21; \
758 g21->key_index = key21_index; \
761 #define lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out, \
764 struct grinder *g30, *g31; \
765 struct rte_mbuf *mbuf30, *mbuf31; \
766 struct bucket *bkt30, *bkt31; \
767 uint8_t *key30, *key31, *key_mem = t->key_mem; \
768 uint8_t *data30, *data31, *data_mem = t->data_mem; \
769 uint64_t match30, match31, match_pos30, match_pos31; \
770 uint64_t match_key30, match_key31, match_keys; \
771 uint32_t key30_index, key31_index; \
772 uint32_t key_size_shl = t->key_size_shl; \
773 uint32_t data_size_shl = t->data_size_shl; \
775 mbuf30 = pkts[pkt30_index]; \
776 g30 = &g[pkt30_index]; \
778 match30 = g30->match; \
779 match_pos30 = g30->match_pos; \
780 key30_index = g30->key_index; \
781 key30 = &key_mem[key30_index << key_size_shl]; \
782 lookup_cmp_key(mbuf30, key30, match_key30, t); \
783 match_key30 <<= pkt30_index; \
784 match_key30 &= match30; \
785 data30 = &data_mem[key30_index << data_size_shl]; \
786 entries[pkt30_index] = data30; \
788 mbuf31 = pkts[pkt31_index]; \
789 g31 = &g[pkt31_index]; \
791 match31 = g31->match; \
792 match_pos31 = g31->match_pos; \
793 key31_index = g31->key_index; \
794 key31 = &key_mem[key31_index << key_size_shl]; \
795 lookup_cmp_key(mbuf31, key31, match_key31, t); \
796 match_key31 <<= pkt31_index; \
797 match_key31 &= match31; \
798 data31 = &data_mem[key31_index << data_size_shl]; \
799 entries[pkt31_index] = data31; \
801 rte_prefetch0(data30); \
802 rte_prefetch0(data31); \
804 match_keys = match_key30 | match_key31; \
805 pkts_mask_out |= match_keys; \
807 if (match_key30 == 0) \
809 lru_update(bkt30, match_pos30); \
811 if (match_key31 == 0) \
813 lru_update(bkt31, match_pos31); \
817 * The lookup function implements a 4-stage pipeline, with each stage processing
818 * two different packets. The purpose of pipelined implementation is to hide the
819 * latency of prefetching the data structures and loosen the data dependency
820 * between instructions.
822 * p00 _______ p10 _______ p20 _______ p30 _______
823 * ----->| |----->| |----->| |----->| |----->
824 * | 0 | | 1 | | 2 | | 3 |
825 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
828 * The naming convention is:
829 * pXY = packet Y of stage X, X = 0 .. 3, Y = 0 .. 1
832 static int rte_table_hash_lru_lookup(
834 struct rte_mbuf **pkts,
836 uint64_t *lookup_hit_mask,
839 struct rte_table_hash *t = (struct rte_table_hash *) table;
840 struct grinder *g = t->grinders;
841 uint64_t pkt00_index, pkt01_index, pkt10_index, pkt11_index;
842 uint64_t pkt20_index, pkt21_index, pkt30_index, pkt31_index;
843 uint64_t pkts_mask_out = 0, pkts_mask_match_many = 0;
846 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
847 RTE_TABLE_HASH_LRU_STATS_PKTS_IN_ADD(t, n_pkts_in);
849 /* Cannot run the pipeline with less than 7 packets */
850 if (__builtin_popcountll(pkts_mask) < 7)
851 return rte_table_hash_lru_lookup_unoptimized(table, pkts,
852 pkts_mask, lookup_hit_mask, entries);
854 /* Pipeline stage 0 */
855 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
858 pkt10_index = pkt00_index;
859 pkt11_index = pkt01_index;
861 /* Pipeline stage 0 */
862 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
864 /* Pipeline stage 1 */
865 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
868 pkt20_index = pkt10_index;
869 pkt21_index = pkt11_index;
870 pkt10_index = pkt00_index;
871 pkt11_index = pkt01_index;
873 /* Pipeline stage 0 */
874 lookup2_stage0(t, g, pkts, pkts_mask, pkt00_index, pkt01_index);
876 /* Pipeline stage 1 */
877 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
879 /* Pipeline stage 2 */
880 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
886 for ( ; pkts_mask; ) {
888 pkt30_index = pkt20_index;
889 pkt31_index = pkt21_index;
890 pkt20_index = pkt10_index;
891 pkt21_index = pkt11_index;
892 pkt10_index = pkt00_index;
893 pkt11_index = pkt01_index;
895 /* Pipeline stage 0 */
896 lookup2_stage0_with_odd_support(t, g, pkts, pkts_mask,
897 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,
904 pkts_mask_match_many);
906 /* Pipeline stage 3 */
907 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index,
908 pkts_mask_out, entries);
912 pkt30_index = pkt20_index;
913 pkt31_index = pkt21_index;
914 pkt20_index = pkt10_index;
915 pkt21_index = pkt11_index;
916 pkt10_index = pkt00_index;
917 pkt11_index = pkt01_index;
919 /* Pipeline stage 1 */
920 lookup2_stage1(t, g, pkts, pkt10_index, pkt11_index);
922 /* Pipeline stage 2 */
923 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
925 /* Pipeline stage 3 */
926 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
930 pkt30_index = pkt20_index;
931 pkt31_index = pkt21_index;
932 pkt20_index = pkt10_index;
933 pkt21_index = pkt11_index;
935 /* Pipeline stage 2 */
936 lookup2_stage2(t, g, pkt20_index, pkt21_index, pkts_mask_match_many);
938 /* Pipeline stage 3 */
939 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
943 pkt30_index = pkt20_index;
944 pkt31_index = pkt21_index;
946 /* Pipeline stage 3 */
947 lookup2_stage3(t, g, pkts, pkt30_index, pkt31_index, pkts_mask_out,
951 pkts_mask_match_many &= ~pkts_mask_out;
952 if (pkts_mask_match_many) {
953 uint64_t pkts_mask_out_slow = 0;
955 status = rte_table_hash_lru_lookup_unoptimized(table, pkts,
956 pkts_mask_match_many, &pkts_mask_out_slow, entries);
957 pkts_mask_out |= pkts_mask_out_slow;
960 *lookup_hit_mask = pkts_mask_out;
961 RTE_TABLE_HASH_LRU_STATS_PKTS_LOOKUP_MISS(t, n_pkts_in - __builtin_popcountll(pkts_mask_out));
966 rte_table_hash_lru_stats_read(void *table, struct rte_table_stats *stats, int clear)
968 struct rte_table_hash *t = table;
971 memcpy(stats, &t->stats, sizeof(t->stats));
974 memset(&t->stats, 0, sizeof(t->stats));
979 struct rte_table_ops rte_table_hash_lru_ops = {
980 .f_create = rte_table_hash_lru_create,
981 .f_free = rte_table_hash_lru_free,
982 .f_add = rte_table_hash_lru_entry_add,
983 .f_delete = rte_table_hash_lru_entry_delete,
985 .f_delete_bulk = NULL,
986 .f_lookup = rte_table_hash_lru_lookup,
987 .f_stats = rte_table_hash_lru_stats_read,