1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
7 #include <rte_common.h>
9 #include <rte_memory.h>
10 #include <rte_malloc.h>
13 #include "rte_table_hash.h"
18 #define KEYS_PER_BUCKET 4
20 #define RTE_BUCKET_ENTRY_VALID 0x1LLU
22 #ifdef RTE_TABLE_STATS_COLLECT
24 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val) \
25 table->stats.n_pkts_in += val
26 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val) \
27 table->stats.n_pkts_lookup_miss += val
31 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val)
32 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val)
37 struct rte_bucket_4_16 {
39 uint64_t signature[4 + 1];
41 struct rte_bucket_4_16 *next;
51 struct rte_bucket_4_16 {
53 uint64_t signature[4 + 1];
55 struct rte_bucket_4_16 *next;
67 struct rte_table_hash {
68 struct rte_table_stats stats;
70 /* Input parameters */
77 rte_table_hash_op_hash f_hash;
80 /* Extendible buckets */
81 uint32_t n_buckets_ext;
86 uint8_t memory[0] __rte_cache_aligned;
90 keycmp(void *a, void *b, void *b_mask)
92 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
94 return (a64[0] != (b64[0] & b_mask64[0])) ||
95 (a64[1] != (b64[1] & b_mask64[1]));
99 keycpy(void *dst, void *src, void *src_mask)
101 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
103 dst64[0] = src64[0] & src_mask64[0];
104 dst64[1] = src64[1] & src_mask64[1];
108 check_params_create(struct rte_table_hash_params *params)
111 if (params->name == NULL) {
112 RTE_LOG(ERR, TABLE, "%s: name invalid value\n", __func__);
117 if (params->key_size != KEY_SIZE) {
118 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
123 if (params->n_keys == 0) {
124 RTE_LOG(ERR, TABLE, "%s: n_keys is zero\n", __func__);
129 if ((params->n_buckets == 0) ||
130 (!rte_is_power_of_2(params->n_buckets))) {
131 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
136 if (params->f_hash == NULL) {
137 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
146 rte_table_hash_create_key16_lru(void *params,
150 struct rte_table_hash_params *p = params;
151 struct rte_table_hash *f;
152 uint64_t bucket_size, total_size;
153 uint32_t n_buckets, i;
155 /* Check input parameters */
156 if ((check_params_create(p) != 0) ||
157 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
158 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
164 * Objective: Pick the number of buckets (n_buckets) so that there a chance
165 * to store n_keys keys in the table.
167 * Note: Since the buckets do not get extended, it is not possible to
168 * guarantee that n_keys keys can be stored in the table at any time. In the
169 * worst case scenario when all the n_keys fall into the same bucket, only
170 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
171 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
172 * n_keys to n_buckets ratio.
174 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
176 n_buckets = rte_align32pow2(
177 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
178 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
180 /* Memory allocation */
181 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_16) +
182 KEYS_PER_BUCKET * entry_size);
183 total_size = sizeof(struct rte_table_hash) + n_buckets * bucket_size;
185 if (total_size > SIZE_MAX) {
186 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
187 "for hash table %s\n",
188 __func__, total_size, p->name);
192 f = rte_zmalloc_socket(p->name,
197 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
198 "for hash table %s\n",
199 __func__, total_size, p->name);
202 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
203 "is %" PRIu64 " bytes\n",
204 __func__, p->name, total_size);
206 /* Memory initialization */
207 f->n_buckets = n_buckets;
208 f->key_size = KEY_SIZE;
209 f->entry_size = entry_size;
210 f->bucket_size = bucket_size;
211 f->key_offset = p->key_offset;
212 f->f_hash = p->f_hash;
215 if (p->key_mask != NULL) {
216 f->key_mask[0] = ((uint64_t *)p->key_mask)[0];
217 f->key_mask[1] = ((uint64_t *)p->key_mask)[1];
219 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
220 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
223 for (i = 0; i < n_buckets; i++) {
224 struct rte_bucket_4_16 *bucket;
226 bucket = (struct rte_bucket_4_16 *) &f->memory[i *
235 rte_table_hash_free_key16_lru(void *table)
237 struct rte_table_hash *f = table;
239 /* Check input parameters */
241 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
250 rte_table_hash_entry_add_key16_lru(
257 struct rte_table_hash *f = table;
258 struct rte_bucket_4_16 *bucket;
259 uint64_t signature, pos;
260 uint32_t bucket_index, i;
262 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
263 bucket_index = signature & (f->n_buckets - 1);
264 bucket = (struct rte_bucket_4_16 *)
265 &f->memory[bucket_index * f->bucket_size];
266 signature |= RTE_BUCKET_ENTRY_VALID;
268 /* Key is present in the bucket */
269 for (i = 0; i < 4; i++) {
270 uint64_t bucket_signature = bucket->signature[i];
271 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
273 if ((bucket_signature == signature) &&
274 (keycmp(bucket_key, key, f->key_mask) == 0)) {
275 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
277 memcpy(bucket_data, entry, f->entry_size);
278 lru_update(bucket, i);
280 *entry_ptr = (void *) bucket_data;
285 /* Key is not present in the bucket */
286 for (i = 0; i < 4; i++) {
287 uint64_t bucket_signature = bucket->signature[i];
288 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
290 if (bucket_signature == 0) {
291 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
293 bucket->signature[i] = signature;
294 keycpy(bucket_key, key, f->key_mask);
295 memcpy(bucket_data, entry, f->entry_size);
296 lru_update(bucket, i);
298 *entry_ptr = (void *) bucket_data;
304 /* Bucket full: replace LRU entry */
305 pos = lru_pos(bucket);
306 bucket->signature[pos] = signature;
307 keycpy(&bucket->key[pos], key, f->key_mask);
308 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
309 lru_update(bucket, pos);
311 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
317 rte_table_hash_entry_delete_key16_lru(
323 struct rte_table_hash *f = table;
324 struct rte_bucket_4_16 *bucket;
326 uint32_t bucket_index, i;
328 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
329 bucket_index = signature & (f->n_buckets - 1);
330 bucket = (struct rte_bucket_4_16 *)
331 &f->memory[bucket_index * f->bucket_size];
332 signature |= RTE_BUCKET_ENTRY_VALID;
334 /* Key is present in the bucket */
335 for (i = 0; i < 4; i++) {
336 uint64_t bucket_signature = bucket->signature[i];
337 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
339 if ((bucket_signature == signature) &&
340 (keycmp(bucket_key, key, f->key_mask) == 0)) {
341 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
343 bucket->signature[i] = 0;
346 memcpy(entry, bucket_data, f->entry_size);
351 /* Key is not present in the bucket */
357 rte_table_hash_create_key16_ext(void *params,
361 struct rte_table_hash_params *p = params;
362 struct rte_table_hash *f;
363 uint64_t bucket_size, stack_size, total_size;
364 uint32_t n_buckets_ext, i;
366 /* Check input parameters */
367 if ((check_params_create(p) != 0) ||
368 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
369 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
375 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
376 * it is guaranteed that n_keys keys can be stored in the table at any time.
378 * The worst case scenario takes place when all the n_keys keys fall into
379 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
380 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
381 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
382 * into a different bucket. This case defeats the purpose of the hash table.
383 * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
385 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
387 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
389 /* Memory allocation */
390 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_16) +
391 KEYS_PER_BUCKET * entry_size);
392 stack_size = RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
393 total_size = sizeof(struct rte_table_hash) +
394 (p->n_buckets + n_buckets_ext) * bucket_size + stack_size;
395 if (total_size > SIZE_MAX) {
396 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
397 "for hash table %s\n",
398 __func__, total_size, p->name);
402 f = rte_zmalloc_socket(p->name,
407 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
408 "for hash table %s\n",
409 __func__, total_size, p->name);
412 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
413 "is %" PRIu64 " bytes\n",
414 __func__, p->name, total_size);
416 /* Memory initialization */
417 f->n_buckets = p->n_buckets;
418 f->key_size = KEY_SIZE;
419 f->entry_size = entry_size;
420 f->bucket_size = bucket_size;
421 f->key_offset = p->key_offset;
422 f->f_hash = p->f_hash;
425 f->n_buckets_ext = n_buckets_ext;
426 f->stack_pos = n_buckets_ext;
427 f->stack = (uint32_t *)
428 &f->memory[(p->n_buckets + n_buckets_ext) * f->bucket_size];
430 if (p->key_mask != NULL) {
431 f->key_mask[0] = (((uint64_t *)p->key_mask)[0]);
432 f->key_mask[1] = (((uint64_t *)p->key_mask)[1]);
434 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
435 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
438 for (i = 0; i < n_buckets_ext; i++)
445 rte_table_hash_free_key16_ext(void *table)
447 struct rte_table_hash *f = table;
449 /* Check input parameters */
451 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
460 rte_table_hash_entry_add_key16_ext(
467 struct rte_table_hash *f = table;
468 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
470 uint32_t bucket_index, i;
472 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
473 bucket_index = signature & (f->n_buckets - 1);
474 bucket0 = (struct rte_bucket_4_16 *)
475 &f->memory[bucket_index * f->bucket_size];
476 signature |= RTE_BUCKET_ENTRY_VALID;
478 /* Key is present in the bucket */
479 for (bucket = bucket0; bucket != NULL; bucket = bucket->next)
480 for (i = 0; i < 4; i++) {
481 uint64_t bucket_signature = bucket->signature[i];
482 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
484 if ((bucket_signature == signature) &&
485 (keycmp(bucket_key, key, f->key_mask) == 0)) {
486 uint8_t *bucket_data = &bucket->data[i *
489 memcpy(bucket_data, entry, f->entry_size);
491 *entry_ptr = (void *) bucket_data;
496 /* Key is not present in the bucket */
497 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
498 bucket_prev = bucket, bucket = bucket->next)
499 for (i = 0; i < 4; i++) {
500 uint64_t bucket_signature = bucket->signature[i];
501 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
503 if (bucket_signature == 0) {
504 uint8_t *bucket_data = &bucket->data[i *
507 bucket->signature[i] = signature;
508 keycpy(bucket_key, key, f->key_mask);
509 memcpy(bucket_data, entry, f->entry_size);
511 *entry_ptr = (void *) bucket_data;
517 /* Bucket full: extend bucket */
518 if (f->stack_pos > 0) {
519 bucket_index = f->stack[--f->stack_pos];
521 bucket = (struct rte_bucket_4_16 *) &f->memory[(f->n_buckets +
522 bucket_index) * f->bucket_size];
523 bucket_prev->next = bucket;
524 bucket_prev->next_valid = 1;
526 bucket->signature[0] = signature;
527 keycpy(&bucket->key[0], key, f->key_mask);
528 memcpy(&bucket->data[0], entry, f->entry_size);
530 *entry_ptr = (void *) &bucket->data[0];
538 rte_table_hash_entry_delete_key16_ext(
544 struct rte_table_hash *f = table;
545 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
547 uint32_t bucket_index, i;
549 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
550 bucket_index = signature & (f->n_buckets - 1);
551 bucket0 = (struct rte_bucket_4_16 *)
552 &f->memory[bucket_index * f->bucket_size];
553 signature |= RTE_BUCKET_ENTRY_VALID;
555 /* Key is present in the bucket */
556 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
557 bucket_prev = bucket, bucket = bucket->next)
558 for (i = 0; i < 4; i++) {
559 uint64_t bucket_signature = bucket->signature[i];
560 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
562 if ((bucket_signature == signature) &&
563 (keycmp(bucket_key, key, f->key_mask) == 0)) {
564 uint8_t *bucket_data = &bucket->data[i *
567 bucket->signature[i] = 0;
570 memcpy(entry, bucket_data, f->entry_size);
572 if ((bucket->signature[0] == 0) &&
573 (bucket->signature[1] == 0) &&
574 (bucket->signature[2] == 0) &&
575 (bucket->signature[3] == 0) &&
576 (bucket_prev != NULL)) {
577 bucket_prev->next = bucket->next;
578 bucket_prev->next_valid =
582 sizeof(struct rte_bucket_4_16));
583 bucket_index = (((uint8_t *)bucket -
584 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
585 f->stack[f->stack_pos++] = bucket_index;
592 /* Key is not present in the bucket */
597 #define lookup_key16_cmp(key_in, bucket, pos, f) \
599 uint64_t xor[4][2], or[4], signature[4], k[2]; \
601 k[0] = key_in[0] & f->key_mask[0]; \
602 k[1] = key_in[1] & f->key_mask[1]; \
603 signature[0] = (~bucket->signature[0]) & 1; \
604 signature[1] = (~bucket->signature[1]) & 1; \
605 signature[2] = (~bucket->signature[2]) & 1; \
606 signature[3] = (~bucket->signature[3]) & 1; \
608 xor[0][0] = k[0] ^ bucket->key[0][0]; \
609 xor[0][1] = k[1] ^ bucket->key[0][1]; \
611 xor[1][0] = k[0] ^ bucket->key[1][0]; \
612 xor[1][1] = k[1] ^ bucket->key[1][1]; \
614 xor[2][0] = k[0] ^ bucket->key[2][0]; \
615 xor[2][1] = k[1] ^ bucket->key[2][1]; \
617 xor[3][0] = k[0] ^ bucket->key[3][0]; \
618 xor[3][1] = k[1] ^ bucket->key[3][1]; \
620 or[0] = xor[0][0] | xor[0][1] | signature[0]; \
621 or[1] = xor[1][0] | xor[1][1] | signature[1]; \
622 or[2] = xor[2][0] | xor[2][1] | signature[2]; \
623 or[3] = xor[3][0] | xor[3][1] | signature[3]; \
636 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \
639 uint32_t key_offset = f->key_offset;\
641 pkt0_index = __builtin_ctzll(pkts_mask); \
642 pkt_mask = 1LLU << pkt0_index; \
643 pkts_mask &= ~pkt_mask; \
645 mbuf0 = pkts[pkt0_index]; \
646 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset));\
649 #define lookup1_stage1(mbuf1, bucket1, f) \
652 uint64_t signature = 0; \
653 uint32_t bucket_index; \
655 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset);\
656 signature = f->f_hash(key, f->key_mask, KEY_SIZE, f->seed); \
658 bucket_index = signature & (f->n_buckets - 1); \
659 bucket1 = (struct rte_bucket_4_16 *) \
660 &f->memory[bucket_index * f->bucket_size]; \
661 rte_prefetch0(bucket1); \
662 rte_prefetch0((void *)(((uintptr_t) bucket1) + RTE_CACHE_LINE_SIZE));\
665 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
666 pkts_mask_out, entries, f) \
673 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
674 lookup_key16_cmp(key, bucket2, pos, f); \
676 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
677 pkts_mask_out |= pkt_mask; \
679 a = (void *) &bucket2->data[pos * f->entry_size]; \
681 entries[pkt2_index] = a; \
682 lru_update(bucket2, pos); \
685 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out, entries, \
686 buckets_mask, buckets, keys, f) \
688 struct rte_bucket_4_16 *bucket_next; \
690 uint64_t pkt_mask, bucket_mask; \
694 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
695 lookup_key16_cmp(key, bucket2, pos, f); \
697 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
698 pkts_mask_out |= pkt_mask; \
700 a = (void *) &bucket2->data[pos * f->entry_size]; \
702 entries[pkt2_index] = a; \
704 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
705 buckets_mask |= bucket_mask; \
706 bucket_next = bucket2->next; \
707 buckets[pkt2_index] = bucket_next; \
708 keys[pkt2_index] = key; \
711 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\
714 struct rte_bucket_4_16 *bucket, *bucket_next; \
716 uint64_t pkt_mask, bucket_mask; \
720 bucket = buckets[pkt_index]; \
721 key = keys[pkt_index]; \
722 lookup_key16_cmp(key, bucket, pos, f); \
724 pkt_mask = (bucket->signature[pos] & 1LLU) << pkt_index;\
725 pkts_mask_out |= pkt_mask; \
727 a = (void *) &bucket->data[pos * f->entry_size]; \
729 entries[pkt_index] = a; \
731 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
732 buckets_mask |= bucket_mask; \
733 bucket_next = bucket->next; \
734 rte_prefetch0(bucket_next); \
735 rte_prefetch0((void *)(((uintptr_t) bucket_next) + RTE_CACHE_LINE_SIZE));\
736 buckets[pkt_index] = bucket_next; \
737 keys[pkt_index] = key; \
740 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
741 pkts, pkts_mask, f) \
743 uint64_t pkt00_mask, pkt01_mask; \
744 uint32_t key_offset = f->key_offset; \
746 pkt00_index = __builtin_ctzll(pkts_mask); \
747 pkt00_mask = 1LLU << pkt00_index; \
748 pkts_mask &= ~pkt00_mask; \
750 mbuf00 = pkts[pkt00_index]; \
751 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
753 pkt01_index = __builtin_ctzll(pkts_mask); \
754 pkt01_mask = 1LLU << pkt01_index; \
755 pkts_mask &= ~pkt01_mask; \
757 mbuf01 = pkts[pkt01_index]; \
758 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
761 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
762 mbuf00, mbuf01, pkts, pkts_mask, f) \
764 uint64_t pkt00_mask, pkt01_mask; \
765 uint32_t key_offset = f->key_offset; \
767 pkt00_index = __builtin_ctzll(pkts_mask); \
768 pkt00_mask = 1LLU << pkt00_index; \
769 pkts_mask &= ~pkt00_mask; \
771 mbuf00 = pkts[pkt00_index]; \
772 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset)); \
774 pkt01_index = __builtin_ctzll(pkts_mask); \
775 if (pkts_mask == 0) \
776 pkt01_index = pkt00_index; \
777 pkt01_mask = 1LLU << pkt01_index; \
778 pkts_mask &= ~pkt01_mask; \
780 mbuf01 = pkts[pkt01_index]; \
781 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset)); \
784 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f) \
786 uint64_t *key10, *key11; \
787 uint64_t signature10, signature11; \
788 uint32_t bucket10_index, bucket11_index; \
790 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, f->key_offset);\
791 signature10 = f->f_hash(key10, f->key_mask, KEY_SIZE, f->seed);\
792 bucket10_index = signature10 & (f->n_buckets - 1); \
793 bucket10 = (struct rte_bucket_4_16 *) \
794 &f->memory[bucket10_index * f->bucket_size]; \
795 rte_prefetch0(bucket10); \
796 rte_prefetch0((void *)(((uintptr_t) bucket10) + RTE_CACHE_LINE_SIZE));\
798 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, f->key_offset);\
799 signature11 = f->f_hash(key11, f->key_mask, KEY_SIZE, f->seed);\
800 bucket11_index = signature11 & (f->n_buckets - 1); \
801 bucket11 = (struct rte_bucket_4_16 *) \
802 &f->memory[bucket11_index * f->bucket_size]; \
803 rte_prefetch0(bucket11); \
804 rte_prefetch0((void *)(((uintptr_t) bucket11) + RTE_CACHE_LINE_SIZE));\
807 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
808 bucket20, bucket21, pkts_mask_out, entries, f) \
811 uint64_t pkt20_mask, pkt21_mask; \
812 uint64_t *key20, *key21; \
813 uint32_t pos20, pos21; \
815 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
816 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
818 lookup_key16_cmp(key20, bucket20, pos20, f); \
819 lookup_key16_cmp(key21, bucket21, pos21, f); \
821 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
822 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
823 pkts_mask_out |= pkt20_mask | pkt21_mask; \
825 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
826 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
827 rte_prefetch0(a20); \
828 rte_prefetch0(a21); \
829 entries[pkt20_index] = a20; \
830 entries[pkt21_index] = a21; \
831 lru_update(bucket20, pos20); \
832 lru_update(bucket21, pos21); \
835 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
836 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f) \
838 struct rte_bucket_4_16 *bucket20_next, *bucket21_next; \
840 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
841 uint64_t *key20, *key21; \
842 uint32_t pos20, pos21; \
844 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
845 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
847 lookup_key16_cmp(key20, bucket20, pos20, f); \
848 lookup_key16_cmp(key21, bucket21, pos21, f); \
850 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
851 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
852 pkts_mask_out |= pkt20_mask | pkt21_mask; \
854 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
855 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
856 rte_prefetch0(a20); \
857 rte_prefetch0(a21); \
858 entries[pkt20_index] = a20; \
859 entries[pkt21_index] = a21; \
861 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
862 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
863 buckets_mask |= bucket20_mask | bucket21_mask; \
864 bucket20_next = bucket20->next; \
865 bucket21_next = bucket21->next; \
866 buckets[pkt20_index] = bucket20_next; \
867 buckets[pkt21_index] = bucket21_next; \
868 keys[pkt20_index] = key20; \
869 keys[pkt21_index] = key21; \
873 rte_table_hash_lookup_key16_lru(
875 struct rte_mbuf **pkts,
877 uint64_t *lookup_hit_mask,
880 struct rte_table_hash *f = (struct rte_table_hash *) table;
881 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
882 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
883 uint32_t pkt00_index, pkt01_index, pkt10_index;
884 uint32_t pkt11_index, pkt20_index, pkt21_index;
885 uint64_t pkts_mask_out = 0;
887 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
889 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
891 /* Cannot run the pipeline with less than 5 packets */
892 if (__builtin_popcountll(pkts_mask) < 5) {
893 for ( ; pkts_mask; ) {
894 struct rte_bucket_4_16 *bucket;
895 struct rte_mbuf *mbuf;
898 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
899 lookup1_stage1(mbuf, bucket, f);
900 lookup1_stage2_lru(pkt_index, mbuf, bucket,
901 pkts_mask_out, entries, f);
904 *lookup_hit_mask = pkts_mask_out;
905 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
906 __builtin_popcountll(pkts_mask_out));
914 /* Pipeline stage 0 */
915 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
921 pkt10_index = pkt00_index;
922 pkt11_index = pkt01_index;
924 /* Pipeline stage 0 */
925 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
928 /* Pipeline stage 1 */
929 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
935 for ( ; pkts_mask; ) {
943 pkt20_index = pkt10_index;
944 pkt21_index = pkt11_index;
945 pkt10_index = pkt00_index;
946 pkt11_index = pkt01_index;
948 /* Pipeline stage 0 */
949 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
950 mbuf00, mbuf01, pkts, pkts_mask, f);
952 /* Pipeline stage 1 */
953 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
955 /* Pipeline stage 2 */
956 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
957 bucket20, bucket21, pkts_mask_out, entries, f);
971 pkt20_index = pkt10_index;
972 pkt21_index = pkt11_index;
973 pkt10_index = pkt00_index;
974 pkt11_index = pkt01_index;
976 /* Pipeline stage 1 */
977 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
979 /* Pipeline stage 2 */
980 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
981 bucket20, bucket21, pkts_mask_out, entries, f);
988 pkt20_index = pkt10_index;
989 pkt21_index = pkt11_index;
991 /* Pipeline stage 2 */
992 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
993 bucket20, bucket21, pkts_mask_out, entries, f);
995 *lookup_hit_mask = pkts_mask_out;
996 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
997 __builtin_popcountll(pkts_mask_out));
1002 rte_table_hash_lookup_key16_ext(
1004 struct rte_mbuf **pkts,
1006 uint64_t *lookup_hit_mask,
1009 struct rte_table_hash *f = (struct rte_table_hash *) table;
1010 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
1011 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
1012 uint32_t pkt00_index, pkt01_index, pkt10_index;
1013 uint32_t pkt11_index, pkt20_index, pkt21_index;
1014 uint64_t pkts_mask_out = 0, buckets_mask = 0;
1015 struct rte_bucket_4_16 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
1016 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
1018 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
1020 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
1022 /* Cannot run the pipeline with less than 5 packets */
1023 if (__builtin_popcountll(pkts_mask) < 5) {
1024 for ( ; pkts_mask; ) {
1025 struct rte_bucket_4_16 *bucket;
1026 struct rte_mbuf *mbuf;
1029 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
1030 lookup1_stage1(mbuf, bucket, f);
1031 lookup1_stage2_ext(pkt_index, mbuf, bucket,
1032 pkts_mask_out, entries, buckets_mask,
1036 goto grind_next_buckets;
1043 /* Pipeline stage 0 */
1044 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1050 pkt10_index = pkt00_index;
1051 pkt11_index = pkt01_index;
1053 /* Pipeline stage 0 */
1054 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1057 /* Pipeline stage 1 */
1058 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1064 for ( ; pkts_mask; ) {
1066 bucket20 = bucket10;
1067 bucket21 = bucket11;
1072 pkt20_index = pkt10_index;
1073 pkt21_index = pkt11_index;
1074 pkt10_index = pkt00_index;
1075 pkt11_index = pkt01_index;
1077 /* Pipeline stage 0 */
1078 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1079 mbuf00, mbuf01, pkts, pkts_mask, f);
1081 /* Pipeline stage 1 */
1082 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1084 /* Pipeline stage 2 */
1085 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1086 bucket20, bucket21, pkts_mask_out, entries,
1087 buckets_mask, buckets, keys, f);
1095 bucket20 = bucket10;
1096 bucket21 = bucket11;
1101 pkt20_index = pkt10_index;
1102 pkt21_index = pkt11_index;
1103 pkt10_index = pkt00_index;
1104 pkt11_index = pkt01_index;
1106 /* Pipeline stage 1 */
1107 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1109 /* Pipeline stage 2 */
1110 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1111 bucket20, bucket21, pkts_mask_out, entries,
1112 buckets_mask, buckets, keys, f);
1115 bucket20 = bucket10;
1116 bucket21 = bucket11;
1119 pkt20_index = pkt10_index;
1120 pkt21_index = pkt11_index;
1122 /* Pipeline stage 2 */
1123 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1124 bucket20, bucket21, pkts_mask_out, entries,
1125 buckets_mask, buckets, keys, f);
1128 /* Grind next buckets */
1129 for ( ; buckets_mask; ) {
1130 uint64_t buckets_mask_next = 0;
1132 for ( ; buckets_mask; ) {
1136 pkt_index = __builtin_ctzll(buckets_mask);
1137 pkt_mask = 1LLU << pkt_index;
1138 buckets_mask &= ~pkt_mask;
1140 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1141 entries, buckets_mask_next, f);
1144 buckets_mask = buckets_mask_next;
1147 *lookup_hit_mask = pkts_mask_out;
1148 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
1149 __builtin_popcountll(pkts_mask_out));
1154 rte_table_hash_key16_stats_read(void *table, struct rte_table_stats *stats, int clear)
1156 struct rte_table_hash *t = table;
1159 memcpy(stats, &t->stats, sizeof(t->stats));
1162 memset(&t->stats, 0, sizeof(t->stats));
1167 struct rte_table_ops rte_table_hash_key16_lru_ops = {
1168 .f_create = rte_table_hash_create_key16_lru,
1169 .f_free = rte_table_hash_free_key16_lru,
1170 .f_add = rte_table_hash_entry_add_key16_lru,
1171 .f_delete = rte_table_hash_entry_delete_key16_lru,
1173 .f_delete_bulk = NULL,
1174 .f_lookup = rte_table_hash_lookup_key16_lru,
1175 .f_stats = rte_table_hash_key16_stats_read,
1178 struct rte_table_ops rte_table_hash_key16_ext_ops = {
1179 .f_create = rte_table_hash_create_key16_ext,
1180 .f_free = rte_table_hash_free_key16_ext,
1181 .f_add = rte_table_hash_entry_add_key16_ext,
1182 .f_delete = rte_table_hash_entry_delete_key16_ext,
1184 .f_delete_bulk = NULL,
1185 .f_lookup = rte_table_hash_lookup_key16_ext,
1186 .f_stats = rte_table_hash_key16_stats_read,