1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2017 Intel Corporation
7 #include <rte_common.h>
8 #include <rte_malloc.h>
11 #include "rte_table_hash.h"
16 #define KEYS_PER_BUCKET 4
18 #define RTE_BUCKET_ENTRY_VALID 0x1LLU
20 #ifdef RTE_TABLE_STATS_COLLECT
22 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val) \
23 table->stats.n_pkts_in += val
24 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val) \
25 table->stats.n_pkts_lookup_miss += val
29 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val)
30 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val)
35 struct rte_bucket_4_16 {
37 uint64_t signature[4 + 1];
39 struct rte_bucket_4_16 *next;
49 struct rte_bucket_4_16 {
51 uint64_t signature[4 + 1];
53 struct rte_bucket_4_16 *next;
65 struct rte_table_hash {
66 struct rte_table_stats stats;
68 /* Input parameters */
75 rte_table_hash_op_hash f_hash;
78 /* Extendible buckets */
79 uint32_t n_buckets_ext;
84 uint8_t memory[0] __rte_cache_aligned;
88 keycmp(void *a, void *b, void *b_mask)
90 uint64_t *a64 = a, *b64 = b, *b_mask64 = b_mask;
92 return (a64[0] != (b64[0] & b_mask64[0])) ||
93 (a64[1] != (b64[1] & b_mask64[1]));
97 keycpy(void *dst, void *src, void *src_mask)
99 uint64_t *dst64 = dst, *src64 = src, *src_mask64 = src_mask;
101 dst64[0] = src64[0] & src_mask64[0];
102 dst64[1] = src64[1] & src_mask64[1];
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 != KEY_SIZE) {
116 RTE_LOG(ERR, TABLE, "%s: key_size invalid value\n", __func__);
121 if (params->n_keys == 0) {
122 RTE_LOG(ERR, TABLE, "%s: n_keys is zero\n", __func__);
127 if ((params->n_buckets == 0) ||
128 (!rte_is_power_of_2(params->n_buckets))) {
129 RTE_LOG(ERR, TABLE, "%s: n_buckets invalid value\n", __func__);
134 if (params->f_hash == NULL) {
135 RTE_LOG(ERR, TABLE, "%s: f_hash function pointer is NULL\n",
144 rte_table_hash_create_key16_lru(void *params,
148 struct rte_table_hash_params *p = params;
149 struct rte_table_hash *f;
150 uint64_t bucket_size, total_size;
151 uint32_t n_buckets, i;
153 /* Check input parameters */
154 if ((check_params_create(p) != 0) ||
155 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
156 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
162 * Objective: Pick the number of buckets (n_buckets) so that there a chance
163 * to store n_keys keys in the table.
165 * Note: Since the buckets do not get extended, it is not possible to
166 * guarantee that n_keys keys can be stored in the table at any time. In the
167 * worst case scenario when all the n_keys fall into the same bucket, only
168 * a maximum of KEYS_PER_BUCKET keys will be stored in the table. This case
169 * defeats the purpose of the hash table. It indicates unsuitable f_hash or
170 * n_keys to n_buckets ratio.
172 * MIN(n_buckets) = (n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET
174 n_buckets = rte_align32pow2(
175 (p->n_keys + KEYS_PER_BUCKET - 1) / KEYS_PER_BUCKET);
176 n_buckets = RTE_MAX(n_buckets, p->n_buckets);
178 /* Memory allocation */
179 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_16) +
180 KEYS_PER_BUCKET * entry_size);
181 total_size = sizeof(struct rte_table_hash) + n_buckets * bucket_size;
183 if (total_size > SIZE_MAX) {
184 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
185 "for hash table %s\n",
186 __func__, total_size, p->name);
190 f = rte_zmalloc_socket(p->name,
195 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
196 "for hash table %s\n",
197 __func__, total_size, p->name);
200 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
201 "is %" PRIu64 " bytes\n",
202 __func__, p->name, total_size);
204 /* Memory initialization */
205 f->n_buckets = n_buckets;
206 f->key_size = KEY_SIZE;
207 f->entry_size = entry_size;
208 f->bucket_size = bucket_size;
209 f->key_offset = p->key_offset;
210 f->f_hash = p->f_hash;
213 if (p->key_mask != NULL) {
214 f->key_mask[0] = ((uint64_t *)p->key_mask)[0];
215 f->key_mask[1] = ((uint64_t *)p->key_mask)[1];
217 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
218 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
221 for (i = 0; i < n_buckets; i++) {
222 struct rte_bucket_4_16 *bucket;
224 bucket = (struct rte_bucket_4_16 *) &f->memory[i *
233 rte_table_hash_free_key16_lru(void *table)
235 struct rte_table_hash *f = table;
237 /* Check input parameters */
239 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
248 rte_table_hash_entry_add_key16_lru(
255 struct rte_table_hash *f = table;
256 struct rte_bucket_4_16 *bucket;
257 uint64_t signature, pos;
258 uint32_t bucket_index, i;
260 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
261 bucket_index = signature & (f->n_buckets - 1);
262 bucket = (struct rte_bucket_4_16 *)
263 &f->memory[bucket_index * f->bucket_size];
264 signature |= RTE_BUCKET_ENTRY_VALID;
266 /* Key is present in the bucket */
267 for (i = 0; i < 4; i++) {
268 uint64_t bucket_signature = bucket->signature[i];
269 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
271 if ((bucket_signature == signature) &&
272 (keycmp(bucket_key, key, f->key_mask) == 0)) {
273 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
275 memcpy(bucket_data, entry, f->entry_size);
276 lru_update(bucket, i);
278 *entry_ptr = (void *) bucket_data;
283 /* Key is not present in the bucket */
284 for (i = 0; i < 4; i++) {
285 uint64_t bucket_signature = bucket->signature[i];
286 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
288 if (bucket_signature == 0) {
289 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
291 bucket->signature[i] = signature;
292 keycpy(bucket_key, key, f->key_mask);
293 memcpy(bucket_data, entry, f->entry_size);
294 lru_update(bucket, i);
296 *entry_ptr = (void *) bucket_data;
302 /* Bucket full: replace LRU entry */
303 pos = lru_pos(bucket);
304 bucket->signature[pos] = signature;
305 keycpy(&bucket->key[pos], key, f->key_mask);
306 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
307 lru_update(bucket, pos);
309 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
315 rte_table_hash_entry_delete_key16_lru(
321 struct rte_table_hash *f = table;
322 struct rte_bucket_4_16 *bucket;
324 uint32_t bucket_index, i;
326 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
327 bucket_index = signature & (f->n_buckets - 1);
328 bucket = (struct rte_bucket_4_16 *)
329 &f->memory[bucket_index * f->bucket_size];
330 signature |= RTE_BUCKET_ENTRY_VALID;
332 /* Key is present in the bucket */
333 for (i = 0; i < 4; i++) {
334 uint64_t bucket_signature = bucket->signature[i];
335 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
337 if ((bucket_signature == signature) &&
338 (keycmp(bucket_key, key, f->key_mask) == 0)) {
339 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
341 bucket->signature[i] = 0;
344 memcpy(entry, bucket_data, f->entry_size);
349 /* Key is not present in the bucket */
355 rte_table_hash_create_key16_ext(void *params,
359 struct rte_table_hash_params *p = params;
360 struct rte_table_hash *f;
361 uint64_t bucket_size, stack_size, total_size;
362 uint32_t n_buckets_ext, i;
364 /* Check input parameters */
365 if ((check_params_create(p) != 0) ||
366 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
367 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
373 * Objective: Pick the number of bucket extensions (n_buckets_ext) so that
374 * it is guaranteed that n_keys keys can be stored in the table at any time.
376 * The worst case scenario takes place when all the n_keys keys fall into
377 * the same bucket. Actually, due to the KEYS_PER_BUCKET scheme, the worst
378 * case takes place when (n_keys - KEYS_PER_BUCKET + 1) keys fall into the
379 * same bucket, while the remaining (KEYS_PER_BUCKET - 1) keys each fall
380 * into a different bucket. This case defeats the purpose of the hash table.
381 * It indicates unsuitable f_hash or n_keys to n_buckets ratio.
383 * n_buckets_ext = n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1
385 n_buckets_ext = p->n_keys / KEYS_PER_BUCKET + KEYS_PER_BUCKET - 1;
387 /* Memory allocation */
388 bucket_size = RTE_CACHE_LINE_ROUNDUP(sizeof(struct rte_bucket_4_16) +
389 KEYS_PER_BUCKET * entry_size);
390 stack_size = RTE_CACHE_LINE_ROUNDUP(n_buckets_ext * sizeof(uint32_t));
391 total_size = sizeof(struct rte_table_hash) +
392 (p->n_buckets + n_buckets_ext) * bucket_size + stack_size;
393 if (total_size > SIZE_MAX) {
394 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
395 "for hash table %s\n",
396 __func__, total_size, p->name);
400 f = rte_zmalloc_socket(p->name,
405 RTE_LOG(ERR, TABLE, "%s: Cannot allocate %" PRIu64 " bytes "
406 "for hash table %s\n",
407 __func__, total_size, p->name);
410 RTE_LOG(INFO, TABLE, "%s: Hash table %s memory footprint "
411 "is %" PRIu64 " bytes\n",
412 __func__, p->name, total_size);
414 /* Memory initialization */
415 f->n_buckets = p->n_buckets;
416 f->key_size = KEY_SIZE;
417 f->entry_size = entry_size;
418 f->bucket_size = bucket_size;
419 f->key_offset = p->key_offset;
420 f->f_hash = p->f_hash;
423 f->n_buckets_ext = n_buckets_ext;
424 f->stack_pos = n_buckets_ext;
425 f->stack = (uint32_t *)
426 &f->memory[(p->n_buckets + n_buckets_ext) * f->bucket_size];
428 if (p->key_mask != NULL) {
429 f->key_mask[0] = (((uint64_t *)p->key_mask)[0]);
430 f->key_mask[1] = (((uint64_t *)p->key_mask)[1]);
432 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
433 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
436 for (i = 0; i < n_buckets_ext; i++)
443 rte_table_hash_free_key16_ext(void *table)
445 struct rte_table_hash *f = table;
447 /* Check input parameters */
449 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
458 rte_table_hash_entry_add_key16_ext(
465 struct rte_table_hash *f = table;
466 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
468 uint32_t bucket_index, i;
470 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
471 bucket_index = signature & (f->n_buckets - 1);
472 bucket0 = (struct rte_bucket_4_16 *)
473 &f->memory[bucket_index * f->bucket_size];
474 signature |= RTE_BUCKET_ENTRY_VALID;
476 /* Key is present in the bucket */
477 for (bucket = bucket0; bucket != NULL; bucket = bucket->next)
478 for (i = 0; i < 4; i++) {
479 uint64_t bucket_signature = bucket->signature[i];
480 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
482 if ((bucket_signature == signature) &&
483 (keycmp(bucket_key, key, f->key_mask) == 0)) {
484 uint8_t *bucket_data = &bucket->data[i *
487 memcpy(bucket_data, entry, f->entry_size);
489 *entry_ptr = (void *) bucket_data;
494 /* Key is not present in the bucket */
495 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
496 bucket_prev = bucket, bucket = bucket->next)
497 for (i = 0; i < 4; i++) {
498 uint64_t bucket_signature = bucket->signature[i];
499 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
501 if (bucket_signature == 0) {
502 uint8_t *bucket_data = &bucket->data[i *
505 bucket->signature[i] = signature;
506 keycpy(bucket_key, key, f->key_mask);
507 memcpy(bucket_data, entry, f->entry_size);
509 *entry_ptr = (void *) bucket_data;
515 /* Bucket full: extend bucket */
516 if (f->stack_pos > 0) {
517 bucket_index = f->stack[--f->stack_pos];
519 bucket = (struct rte_bucket_4_16 *) &f->memory[(f->n_buckets +
520 bucket_index) * f->bucket_size];
521 bucket_prev->next = bucket;
522 bucket_prev->next_valid = 1;
524 bucket->signature[0] = signature;
525 keycpy(&bucket->key[0], key, f->key_mask);
526 memcpy(&bucket->data[0], entry, f->entry_size);
528 *entry_ptr = (void *) &bucket->data[0];
536 rte_table_hash_entry_delete_key16_ext(
542 struct rte_table_hash *f = table;
543 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
545 uint32_t bucket_index, i;
547 signature = f->f_hash(key, f->key_mask, f->key_size, f->seed);
548 bucket_index = signature & (f->n_buckets - 1);
549 bucket0 = (struct rte_bucket_4_16 *)
550 &f->memory[bucket_index * f->bucket_size];
551 signature |= RTE_BUCKET_ENTRY_VALID;
553 /* Key is present in the bucket */
554 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
555 bucket_prev = bucket, bucket = bucket->next)
556 for (i = 0; i < 4; i++) {
557 uint64_t bucket_signature = bucket->signature[i];
558 uint8_t *bucket_key = (uint8_t *) &bucket->key[i];
560 if ((bucket_signature == signature) &&
561 (keycmp(bucket_key, key, f->key_mask) == 0)) {
562 uint8_t *bucket_data = &bucket->data[i *
565 bucket->signature[i] = 0;
568 memcpy(entry, bucket_data, f->entry_size);
570 if ((bucket->signature[0] == 0) &&
571 (bucket->signature[1] == 0) &&
572 (bucket->signature[2] == 0) &&
573 (bucket->signature[3] == 0) &&
574 (bucket_prev != NULL)) {
575 bucket_prev->next = bucket->next;
576 bucket_prev->next_valid =
580 sizeof(struct rte_bucket_4_16));
581 bucket_index = (((uint8_t *)bucket -
582 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
583 f->stack[f->stack_pos++] = bucket_index;
590 /* Key is not present in the bucket */
595 #define lookup_key16_cmp(key_in, bucket, pos, f) \
597 uint64_t xor[4][2], or[4], signature[4], k[2]; \
599 k[0] = key_in[0] & f->key_mask[0]; \
600 k[1] = key_in[1] & f->key_mask[1]; \
601 signature[0] = (~bucket->signature[0]) & 1; \
602 signature[1] = (~bucket->signature[1]) & 1; \
603 signature[2] = (~bucket->signature[2]) & 1; \
604 signature[3] = (~bucket->signature[3]) & 1; \
606 xor[0][0] = k[0] ^ bucket->key[0][0]; \
607 xor[0][1] = k[1] ^ bucket->key[0][1]; \
609 xor[1][0] = k[0] ^ bucket->key[1][0]; \
610 xor[1][1] = k[1] ^ bucket->key[1][1]; \
612 xor[2][0] = k[0] ^ bucket->key[2][0]; \
613 xor[2][1] = k[1] ^ bucket->key[2][1]; \
615 xor[3][0] = k[0] ^ bucket->key[3][0]; \
616 xor[3][1] = k[1] ^ bucket->key[3][1]; \
618 or[0] = xor[0][0] | xor[0][1] | signature[0]; \
619 or[1] = xor[1][0] | xor[1][1] | signature[1]; \
620 or[2] = xor[2][0] | xor[2][1] | signature[2]; \
621 or[3] = xor[3][0] | xor[3][1] | signature[3]; \
634 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \
637 uint32_t key_offset = f->key_offset;\
639 pkt0_index = __builtin_ctzll(pkts_mask); \
640 pkt_mask = 1LLU << pkt0_index; \
641 pkts_mask &= ~pkt_mask; \
643 mbuf0 = pkts[pkt0_index]; \
644 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset));\
647 #define lookup1_stage1(mbuf1, bucket1, f) \
650 uint64_t signature = 0; \
651 uint32_t bucket_index; \
653 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset);\
654 signature = f->f_hash(key, f->key_mask, KEY_SIZE, f->seed); \
656 bucket_index = signature & (f->n_buckets - 1); \
657 bucket1 = (struct rte_bucket_4_16 *) \
658 &f->memory[bucket_index * f->bucket_size]; \
659 rte_prefetch0(bucket1); \
660 rte_prefetch0((void *)(((uintptr_t) bucket1) + RTE_CACHE_LINE_SIZE));\
663 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
664 pkts_mask_out, entries, f) \
671 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
672 lookup_key16_cmp(key, bucket2, pos, f); \
674 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
675 pkts_mask_out |= pkt_mask; \
677 a = (void *) &bucket2->data[pos * f->entry_size]; \
679 entries[pkt2_index] = a; \
680 lru_update(bucket2, pos); \
683 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out, entries, \
684 buckets_mask, buckets, keys, f) \
686 struct rte_bucket_4_16 *bucket_next; \
688 uint64_t pkt_mask, bucket_mask; \
692 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
693 lookup_key16_cmp(key, bucket2, pos, f); \
695 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
696 pkts_mask_out |= pkt_mask; \
698 a = (void *) &bucket2->data[pos * f->entry_size]; \
700 entries[pkt2_index] = a; \
702 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
703 buckets_mask |= bucket_mask; \
704 bucket_next = bucket2->next; \
705 buckets[pkt2_index] = bucket_next; \
706 keys[pkt2_index] = key; \
709 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\
712 struct rte_bucket_4_16 *bucket, *bucket_next; \
714 uint64_t pkt_mask, bucket_mask; \
718 bucket = buckets[pkt_index]; \
719 key = keys[pkt_index]; \
720 lookup_key16_cmp(key, bucket, pos, f); \
722 pkt_mask = (bucket->signature[pos] & 1LLU) << pkt_index;\
723 pkts_mask_out |= pkt_mask; \
725 a = (void *) &bucket->data[pos * f->entry_size]; \
727 entries[pkt_index] = a; \
729 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
730 buckets_mask |= bucket_mask; \
731 bucket_next = bucket->next; \
732 rte_prefetch0(bucket_next); \
733 rte_prefetch0((void *)(((uintptr_t) bucket_next) + RTE_CACHE_LINE_SIZE));\
734 buckets[pkt_index] = bucket_next; \
735 keys[pkt_index] = key; \
738 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
739 pkts, pkts_mask, f) \
741 uint64_t pkt00_mask, pkt01_mask; \
742 uint32_t key_offset = f->key_offset; \
744 pkt00_index = __builtin_ctzll(pkts_mask); \
745 pkt00_mask = 1LLU << pkt00_index; \
746 pkts_mask &= ~pkt00_mask; \
748 mbuf00 = pkts[pkt00_index]; \
749 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
751 pkt01_index = __builtin_ctzll(pkts_mask); \
752 pkt01_mask = 1LLU << pkt01_index; \
753 pkts_mask &= ~pkt01_mask; \
755 mbuf01 = pkts[pkt01_index]; \
756 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
759 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
760 mbuf00, mbuf01, pkts, pkts_mask, f) \
762 uint64_t pkt00_mask, pkt01_mask; \
763 uint32_t key_offset = f->key_offset; \
765 pkt00_index = __builtin_ctzll(pkts_mask); \
766 pkt00_mask = 1LLU << pkt00_index; \
767 pkts_mask &= ~pkt00_mask; \
769 mbuf00 = pkts[pkt00_index]; \
770 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset)); \
772 pkt01_index = __builtin_ctzll(pkts_mask); \
773 if (pkts_mask == 0) \
774 pkt01_index = pkt00_index; \
775 pkt01_mask = 1LLU << pkt01_index; \
776 pkts_mask &= ~pkt01_mask; \
778 mbuf01 = pkts[pkt01_index]; \
779 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset)); \
782 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f) \
784 uint64_t *key10, *key11; \
785 uint64_t signature10, signature11; \
786 uint32_t bucket10_index, bucket11_index; \
788 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, f->key_offset);\
789 signature10 = f->f_hash(key10, f->key_mask, KEY_SIZE, f->seed);\
790 bucket10_index = signature10 & (f->n_buckets - 1); \
791 bucket10 = (struct rte_bucket_4_16 *) \
792 &f->memory[bucket10_index * f->bucket_size]; \
793 rte_prefetch0(bucket10); \
794 rte_prefetch0((void *)(((uintptr_t) bucket10) + RTE_CACHE_LINE_SIZE));\
796 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, f->key_offset);\
797 signature11 = f->f_hash(key11, f->key_mask, KEY_SIZE, f->seed);\
798 bucket11_index = signature11 & (f->n_buckets - 1); \
799 bucket11 = (struct rte_bucket_4_16 *) \
800 &f->memory[bucket11_index * f->bucket_size]; \
801 rte_prefetch0(bucket11); \
802 rte_prefetch0((void *)(((uintptr_t) bucket11) + RTE_CACHE_LINE_SIZE));\
805 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
806 bucket20, bucket21, pkts_mask_out, entries, f) \
809 uint64_t pkt20_mask, pkt21_mask; \
810 uint64_t *key20, *key21; \
811 uint32_t pos20, pos21; \
813 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
814 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
816 lookup_key16_cmp(key20, bucket20, pos20, f); \
817 lookup_key16_cmp(key21, bucket21, pos21, f); \
819 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
820 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
821 pkts_mask_out |= pkt20_mask | pkt21_mask; \
823 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
824 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
825 rte_prefetch0(a20); \
826 rte_prefetch0(a21); \
827 entries[pkt20_index] = a20; \
828 entries[pkt21_index] = a21; \
829 lru_update(bucket20, pos20); \
830 lru_update(bucket21, pos21); \
833 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
834 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f) \
836 struct rte_bucket_4_16 *bucket20_next, *bucket21_next; \
838 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
839 uint64_t *key20, *key21; \
840 uint32_t pos20, pos21; \
842 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
843 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
845 lookup_key16_cmp(key20, bucket20, pos20, f); \
846 lookup_key16_cmp(key21, bucket21, pos21, f); \
848 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
849 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
850 pkts_mask_out |= pkt20_mask | pkt21_mask; \
852 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
853 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
854 rte_prefetch0(a20); \
855 rte_prefetch0(a21); \
856 entries[pkt20_index] = a20; \
857 entries[pkt21_index] = a21; \
859 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
860 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
861 buckets_mask |= bucket20_mask | bucket21_mask; \
862 bucket20_next = bucket20->next; \
863 bucket21_next = bucket21->next; \
864 buckets[pkt20_index] = bucket20_next; \
865 buckets[pkt21_index] = bucket21_next; \
866 keys[pkt20_index] = key20; \
867 keys[pkt21_index] = key21; \
871 rte_table_hash_lookup_key16_lru(
873 struct rte_mbuf **pkts,
875 uint64_t *lookup_hit_mask,
878 struct rte_table_hash *f = (struct rte_table_hash *) table;
879 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
880 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
881 uint32_t pkt00_index, pkt01_index, pkt10_index;
882 uint32_t pkt11_index, pkt20_index, pkt21_index;
883 uint64_t pkts_mask_out = 0;
885 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
887 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
889 /* Cannot run the pipeline with less than 5 packets */
890 if (__builtin_popcountll(pkts_mask) < 5) {
891 for ( ; pkts_mask; ) {
892 struct rte_bucket_4_16 *bucket;
893 struct rte_mbuf *mbuf;
896 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
897 lookup1_stage1(mbuf, bucket, f);
898 lookup1_stage2_lru(pkt_index, mbuf, bucket,
899 pkts_mask_out, entries, f);
902 *lookup_hit_mask = pkts_mask_out;
903 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
904 __builtin_popcountll(pkts_mask_out));
912 /* Pipeline stage 0 */
913 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
919 pkt10_index = pkt00_index;
920 pkt11_index = pkt01_index;
922 /* Pipeline stage 0 */
923 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
926 /* Pipeline stage 1 */
927 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
933 for ( ; pkts_mask; ) {
941 pkt20_index = pkt10_index;
942 pkt21_index = pkt11_index;
943 pkt10_index = pkt00_index;
944 pkt11_index = pkt01_index;
946 /* Pipeline stage 0 */
947 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
948 mbuf00, mbuf01, pkts, pkts_mask, f);
950 /* Pipeline stage 1 */
951 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
953 /* Pipeline stage 2 */
954 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
955 bucket20, bucket21, pkts_mask_out, entries, f);
969 pkt20_index = pkt10_index;
970 pkt21_index = pkt11_index;
971 pkt10_index = pkt00_index;
972 pkt11_index = pkt01_index;
974 /* Pipeline stage 1 */
975 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
977 /* Pipeline stage 2 */
978 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
979 bucket20, bucket21, pkts_mask_out, entries, f);
986 pkt20_index = pkt10_index;
987 pkt21_index = pkt11_index;
989 /* Pipeline stage 2 */
990 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
991 bucket20, bucket21, pkts_mask_out, entries, f);
993 *lookup_hit_mask = pkts_mask_out;
994 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
995 __builtin_popcountll(pkts_mask_out));
1000 rte_table_hash_lookup_key16_ext(
1002 struct rte_mbuf **pkts,
1004 uint64_t *lookup_hit_mask,
1007 struct rte_table_hash *f = (struct rte_table_hash *) table;
1008 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
1009 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
1010 uint32_t pkt00_index, pkt01_index, pkt10_index;
1011 uint32_t pkt11_index, pkt20_index, pkt21_index;
1012 uint64_t pkts_mask_out = 0, buckets_mask = 0;
1013 struct rte_bucket_4_16 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
1014 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
1016 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
1018 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
1020 /* Cannot run the pipeline with less than 5 packets */
1021 if (__builtin_popcountll(pkts_mask) < 5) {
1022 for ( ; pkts_mask; ) {
1023 struct rte_bucket_4_16 *bucket;
1024 struct rte_mbuf *mbuf;
1027 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask, f);
1028 lookup1_stage1(mbuf, bucket, f);
1029 lookup1_stage2_ext(pkt_index, mbuf, bucket,
1030 pkts_mask_out, entries, buckets_mask,
1034 goto grind_next_buckets;
1041 /* Pipeline stage 0 */
1042 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1048 pkt10_index = pkt00_index;
1049 pkt11_index = pkt01_index;
1051 /* Pipeline stage 0 */
1052 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
1055 /* Pipeline stage 1 */
1056 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1062 for ( ; pkts_mask; ) {
1064 bucket20 = bucket10;
1065 bucket21 = bucket11;
1070 pkt20_index = pkt10_index;
1071 pkt21_index = pkt11_index;
1072 pkt10_index = pkt00_index;
1073 pkt11_index = pkt01_index;
1075 /* Pipeline stage 0 */
1076 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1077 mbuf00, mbuf01, pkts, pkts_mask, f);
1079 /* Pipeline stage 1 */
1080 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1082 /* Pipeline stage 2 */
1083 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1084 bucket20, bucket21, pkts_mask_out, entries,
1085 buckets_mask, buckets, keys, f);
1093 bucket20 = bucket10;
1094 bucket21 = bucket11;
1099 pkt20_index = pkt10_index;
1100 pkt21_index = pkt11_index;
1101 pkt10_index = pkt00_index;
1102 pkt11_index = pkt01_index;
1104 /* Pipeline stage 1 */
1105 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1107 /* Pipeline stage 2 */
1108 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1109 bucket20, bucket21, pkts_mask_out, entries,
1110 buckets_mask, buckets, keys, f);
1113 bucket20 = bucket10;
1114 bucket21 = bucket11;
1117 pkt20_index = pkt10_index;
1118 pkt21_index = pkt11_index;
1120 /* Pipeline stage 2 */
1121 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1122 bucket20, bucket21, pkts_mask_out, entries,
1123 buckets_mask, buckets, keys, f);
1126 /* Grind next buckets */
1127 for ( ; buckets_mask; ) {
1128 uint64_t buckets_mask_next = 0;
1130 for ( ; buckets_mask; ) {
1134 pkt_index = __builtin_ctzll(buckets_mask);
1135 pkt_mask = 1LLU << pkt_index;
1136 buckets_mask &= ~pkt_mask;
1138 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1139 entries, buckets_mask_next, f);
1142 buckets_mask = buckets_mask_next;
1145 *lookup_hit_mask = pkts_mask_out;
1146 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in -
1147 __builtin_popcountll(pkts_mask_out));
1152 rte_table_hash_key16_stats_read(void *table, struct rte_table_stats *stats, int clear)
1154 struct rte_table_hash *t = table;
1157 memcpy(stats, &t->stats, sizeof(t->stats));
1160 memset(&t->stats, 0, sizeof(t->stats));
1165 struct rte_table_ops rte_table_hash_key16_lru_ops = {
1166 .f_create = rte_table_hash_create_key16_lru,
1167 .f_free = rte_table_hash_free_key16_lru,
1168 .f_add = rte_table_hash_entry_add_key16_lru,
1169 .f_delete = rte_table_hash_entry_delete_key16_lru,
1171 .f_delete_bulk = NULL,
1172 .f_lookup = rte_table_hash_lookup_key16_lru,
1173 .f_stats = rte_table_hash_key16_stats_read,
1176 struct rte_table_ops rte_table_hash_key16_ext_ops = {
1177 .f_create = rte_table_hash_create_key16_ext,
1178 .f_free = rte_table_hash_free_key16_ext,
1179 .f_add = rte_table_hash_entry_add_key16_ext,
1180 .f_delete = rte_table_hash_entry_delete_key16_ext,
1182 .f_delete_bulk = NULL,
1183 .f_lookup = rte_table_hash_lookup_key16_ext,
1184 .f_stats = rte_table_hash_key16_stats_read,