4 * Copyright(c) 2010-2014 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.
36 #include <rte_common.h>
38 #include <rte_memory.h>
39 #include <rte_malloc.h>
42 #include "rte_table_hash.h"
45 #define RTE_TABLE_HASH_KEY_SIZE 16
47 #define RTE_BUCKET_ENTRY_VALID 0x1LLU
49 #ifdef RTE_TABLE_STATS_COLLECT
51 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val) \
52 table->stats.n_pkts_in += val
53 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val) \
54 table->stats.n_pkts_lookup_miss += val
58 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(table, val)
59 #define RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(table, val)
63 struct rte_bucket_4_16 {
65 uint64_t signature[4 + 1];
67 struct rte_bucket_4_16 *next;
77 struct rte_table_hash {
78 struct rte_table_stats stats;
80 /* Input parameters */
82 uint32_t n_entries_per_bucket;
86 uint32_t signature_offset;
88 rte_table_hash_op_hash f_hash;
91 /* Extendible buckets */
92 uint32_t n_buckets_ext;
97 uint8_t memory[0] __rte_cache_aligned;
101 check_params_create_lru(struct rte_table_hash_key16_lru_params *params) {
103 if (params->n_entries == 0) {
104 RTE_LOG(ERR, TABLE, "%s: n_entries is zero\n", __func__);
109 if (params->f_hash == NULL) {
111 "%s: f_hash function pointer is NULL\n", __func__);
119 rte_table_hash_create_key16_lru(void *params,
123 struct rte_table_hash_key16_lru_params *p =
124 (struct rte_table_hash_key16_lru_params *) params;
125 struct rte_table_hash *f;
126 uint32_t n_buckets, n_entries_per_bucket,
127 key_size, bucket_size_cl, total_size, i;
129 /* Check input parameters */
130 if ((check_params_create_lru(p) != 0) ||
131 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
132 ((sizeof(struct rte_bucket_4_16) % RTE_CACHE_LINE_SIZE) != 0))
134 n_entries_per_bucket = 4;
137 /* Memory allocation */
138 n_buckets = rte_align32pow2((p->n_entries + n_entries_per_bucket - 1) /
139 n_entries_per_bucket);
140 bucket_size_cl = (sizeof(struct rte_bucket_4_16) + n_entries_per_bucket
141 * entry_size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
142 total_size = sizeof(struct rte_table_hash) + n_buckets *
143 bucket_size_cl * RTE_CACHE_LINE_SIZE;
145 f = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
148 "%s: Cannot allocate %u bytes for hash table\n",
149 __func__, total_size);
153 "%s: Hash table memory footprint is %u bytes\n",
154 __func__, total_size);
156 /* Memory initialization */
157 f->n_buckets = n_buckets;
158 f->n_entries_per_bucket = n_entries_per_bucket;
159 f->key_size = key_size;
160 f->entry_size = entry_size;
161 f->bucket_size = bucket_size_cl * RTE_CACHE_LINE_SIZE;
162 f->signature_offset = p->signature_offset;
163 f->key_offset = p->key_offset;
164 f->f_hash = p->f_hash;
167 for (i = 0; i < n_buckets; i++) {
168 struct rte_bucket_4_16 *bucket;
170 bucket = (struct rte_bucket_4_16 *) &f->memory[i *
179 rte_table_hash_free_key16_lru(void *table)
181 struct rte_table_hash *f = (struct rte_table_hash *) table;
183 /* Check input parameters */
185 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
194 rte_table_hash_entry_add_key16_lru(
201 struct rte_table_hash *f = (struct rte_table_hash *) table;
202 struct rte_bucket_4_16 *bucket;
203 uint64_t signature, pos;
204 uint32_t bucket_index, i;
206 signature = f->f_hash(key, f->key_size, f->seed);
207 bucket_index = signature & (f->n_buckets - 1);
208 bucket = (struct rte_bucket_4_16 *)
209 &f->memory[bucket_index * f->bucket_size];
210 signature |= RTE_BUCKET_ENTRY_VALID;
212 /* Key is present in the bucket */
213 for (i = 0; i < 4; i++) {
214 uint64_t bucket_signature = bucket->signature[i];
215 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
217 if ((bucket_signature == signature) &&
218 (memcmp(key, bucket_key, f->key_size) == 0)) {
219 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
221 memcpy(bucket_data, entry, f->entry_size);
222 lru_update(bucket, i);
224 *entry_ptr = (void *) bucket_data;
229 /* Key is not present in the bucket */
230 for (i = 0; i < 4; i++) {
231 uint64_t bucket_signature = bucket->signature[i];
232 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
234 if (bucket_signature == 0) {
235 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
237 bucket->signature[i] = signature;
238 memcpy(bucket_key, key, f->key_size);
239 memcpy(bucket_data, entry, f->entry_size);
240 lru_update(bucket, i);
242 *entry_ptr = (void *) bucket_data;
248 /* Bucket full: replace LRU entry */
249 pos = lru_pos(bucket);
250 bucket->signature[pos] = signature;
251 memcpy(bucket->key[pos], key, f->key_size);
252 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
253 lru_update(bucket, pos);
255 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
261 rte_table_hash_entry_delete_key16_lru(
267 struct rte_table_hash *f = (struct rte_table_hash *) table;
268 struct rte_bucket_4_16 *bucket;
270 uint32_t bucket_index, i;
272 signature = f->f_hash(key, f->key_size, f->seed);
273 bucket_index = signature & (f->n_buckets - 1);
274 bucket = (struct rte_bucket_4_16 *)
275 &f->memory[bucket_index * f->bucket_size];
276 signature |= RTE_BUCKET_ENTRY_VALID;
278 /* Key is present in the bucket */
279 for (i = 0; i < 4; i++) {
280 uint64_t bucket_signature = bucket->signature[i];
281 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
283 if ((bucket_signature == signature) &&
284 (memcmp(key, bucket_key, f->key_size) == 0)) {
285 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
287 bucket->signature[i] = 0;
290 memcpy(entry, bucket_data, f->entry_size);
295 /* Key is not present in the bucket */
301 check_params_create_ext(struct rte_table_hash_key16_ext_params *params) {
303 if (params->n_entries == 0) {
304 RTE_LOG(ERR, TABLE, "%s: n_entries is zero\n", __func__);
309 if (params->n_entries_ext == 0) {
310 RTE_LOG(ERR, TABLE, "%s: n_entries_ext is zero\n", __func__);
315 if (params->f_hash == NULL) {
317 "%s: f_hash function pointer is NULL\n", __func__);
325 rte_table_hash_create_key16_ext(void *params,
329 struct rte_table_hash_key16_ext_params *p =
330 (struct rte_table_hash_key16_ext_params *) params;
331 struct rte_table_hash *f;
332 uint32_t n_buckets, n_buckets_ext, n_entries_per_bucket, key_size,
333 bucket_size_cl, stack_size_cl, total_size, i;
335 /* Check input parameters */
336 if ((check_params_create_ext(p) != 0) ||
337 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
338 ((sizeof(struct rte_bucket_4_16) % RTE_CACHE_LINE_SIZE) != 0))
341 n_entries_per_bucket = 4;
344 /* Memory allocation */
345 n_buckets = rte_align32pow2((p->n_entries + n_entries_per_bucket - 1) /
346 n_entries_per_bucket);
347 n_buckets_ext = (p->n_entries_ext + n_entries_per_bucket - 1) /
348 n_entries_per_bucket;
349 bucket_size_cl = (sizeof(struct rte_bucket_4_16) + n_entries_per_bucket
350 * entry_size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
351 stack_size_cl = (n_buckets_ext * sizeof(uint32_t) + RTE_CACHE_LINE_SIZE - 1)
352 / RTE_CACHE_LINE_SIZE;
353 total_size = sizeof(struct rte_table_hash) +
354 ((n_buckets + n_buckets_ext) * bucket_size_cl + stack_size_cl) *
357 f = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
360 "%s: Cannot allocate %u bytes for hash table\n",
361 __func__, total_size);
365 "%s: Hash table memory footprint is %u bytes\n",
366 __func__, total_size);
368 /* Memory initialization */
369 f->n_buckets = n_buckets;
370 f->n_entries_per_bucket = n_entries_per_bucket;
371 f->key_size = key_size;
372 f->entry_size = entry_size;
373 f->bucket_size = bucket_size_cl * RTE_CACHE_LINE_SIZE;
374 f->signature_offset = p->signature_offset;
375 f->key_offset = p->key_offset;
376 f->f_hash = p->f_hash;
379 f->n_buckets_ext = n_buckets_ext;
380 f->stack_pos = n_buckets_ext;
381 f->stack = (uint32_t *)
382 &f->memory[(n_buckets + n_buckets_ext) * f->bucket_size];
384 for (i = 0; i < n_buckets_ext; i++)
391 rte_table_hash_free_key16_ext(void *table)
393 struct rte_table_hash *f = (struct rte_table_hash *) table;
395 /* Check input parameters */
397 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
406 rte_table_hash_entry_add_key16_ext(
413 struct rte_table_hash *f = (struct rte_table_hash *) table;
414 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
416 uint32_t bucket_index, i;
418 signature = f->f_hash(key, f->key_size, f->seed);
419 bucket_index = signature & (f->n_buckets - 1);
420 bucket0 = (struct rte_bucket_4_16 *)
421 &f->memory[bucket_index * f->bucket_size];
422 signature |= RTE_BUCKET_ENTRY_VALID;
424 /* Key is present in the bucket */
425 for (bucket = bucket0; bucket != NULL; bucket = bucket->next)
426 for (i = 0; i < 4; i++) {
427 uint64_t bucket_signature = bucket->signature[i];
428 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
430 if ((bucket_signature == signature) &&
431 (memcmp(key, bucket_key, f->key_size) == 0)) {
432 uint8_t *bucket_data = &bucket->data[i *
435 memcpy(bucket_data, entry, f->entry_size);
437 *entry_ptr = (void *) bucket_data;
442 /* Key is not present in the bucket */
443 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
444 bucket_prev = bucket, bucket = bucket->next)
445 for (i = 0; i < 4; i++) {
446 uint64_t bucket_signature = bucket->signature[i];
447 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
449 if (bucket_signature == 0) {
450 uint8_t *bucket_data = &bucket->data[i *
453 bucket->signature[i] = signature;
454 memcpy(bucket_key, key, f->key_size);
455 memcpy(bucket_data, entry, f->entry_size);
457 *entry_ptr = (void *) bucket_data;
463 /* Bucket full: extend bucket */
464 if (f->stack_pos > 0) {
465 bucket_index = f->stack[--f->stack_pos];
467 bucket = (struct rte_bucket_4_16 *) &f->memory[(f->n_buckets +
468 bucket_index) * f->bucket_size];
469 bucket_prev->next = bucket;
470 bucket_prev->next_valid = 1;
472 bucket->signature[0] = signature;
473 memcpy(bucket->key[0], key, f->key_size);
474 memcpy(&bucket->data[0], entry, f->entry_size);
476 *entry_ptr = (void *) &bucket->data[0];
484 rte_table_hash_entry_delete_key16_ext(
490 struct rte_table_hash *f = (struct rte_table_hash *) table;
491 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
493 uint32_t bucket_index, i;
495 signature = f->f_hash(key, f->key_size, f->seed);
496 bucket_index = signature & (f->n_buckets - 1);
497 bucket0 = (struct rte_bucket_4_16 *)
498 &f->memory[bucket_index * f->bucket_size];
499 signature |= RTE_BUCKET_ENTRY_VALID;
501 /* Key is present in the bucket */
502 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
503 bucket_prev = bucket, bucket = bucket->next)
504 for (i = 0; i < 4; i++) {
505 uint64_t bucket_signature = bucket->signature[i];
506 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
508 if ((bucket_signature == signature) &&
509 (memcmp(key, bucket_key, f->key_size) == 0)) {
510 uint8_t *bucket_data = &bucket->data[i *
513 bucket->signature[i] = 0;
516 memcpy(entry, bucket_data,
519 if ((bucket->signature[0] == 0) &&
520 (bucket->signature[1] == 0) &&
521 (bucket->signature[2] == 0) &&
522 (bucket->signature[3] == 0) &&
523 (bucket_prev != NULL)) {
524 bucket_prev->next = bucket->next;
525 bucket_prev->next_valid =
529 sizeof(struct rte_bucket_4_16));
530 bucket_index = (((uint8_t *)bucket -
531 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
532 f->stack[f->stack_pos++] = bucket_index;
539 /* Key is not present in the bucket */
544 #define lookup_key16_cmp(key_in, bucket, pos) \
546 uint64_t xor[4][2], or[4], signature[4]; \
548 signature[0] = (~bucket->signature[0]) & 1; \
549 signature[1] = (~bucket->signature[1]) & 1; \
550 signature[2] = (~bucket->signature[2]) & 1; \
551 signature[3] = (~bucket->signature[3]) & 1; \
553 xor[0][0] = key_in[0] ^ bucket->key[0][0]; \
554 xor[0][1] = key_in[1] ^ bucket->key[0][1]; \
556 xor[1][0] = key_in[0] ^ bucket->key[1][0]; \
557 xor[1][1] = key_in[1] ^ bucket->key[1][1]; \
559 xor[2][0] = key_in[0] ^ bucket->key[2][0]; \
560 xor[2][1] = key_in[1] ^ bucket->key[2][1]; \
562 xor[3][0] = key_in[0] ^ bucket->key[3][0]; \
563 xor[3][1] = key_in[1] ^ bucket->key[3][1]; \
565 or[0] = xor[0][0] | xor[0][1] | signature[0]; \
566 or[1] = xor[1][0] | xor[1][1] | signature[1]; \
567 or[2] = xor[2][0] | xor[2][1] | signature[2]; \
568 or[3] = xor[3][0] | xor[3][1] | signature[3]; \
581 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask) \
585 pkt0_index = __builtin_ctzll(pkts_mask); \
586 pkt_mask = 1LLU << pkt0_index; \
587 pkts_mask &= ~pkt_mask; \
589 mbuf0 = pkts[pkt0_index]; \
590 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, 0)); \
593 #define lookup1_stage1(mbuf1, bucket1, f) \
595 uint64_t signature; \
596 uint32_t bucket_index; \
598 signature = RTE_MBUF_METADATA_UINT32(mbuf1, f->signature_offset);\
599 bucket_index = signature & (f->n_buckets - 1); \
600 bucket1 = (struct rte_bucket_4_16 *) \
601 &f->memory[bucket_index * f->bucket_size]; \
602 rte_prefetch0(bucket1); \
603 rte_prefetch0((void *)(((uintptr_t) bucket1) + RTE_CACHE_LINE_SIZE));\
606 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
607 pkts_mask_out, entries, f) \
614 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
616 lookup_key16_cmp(key, bucket2, pos); \
618 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
619 pkts_mask_out |= pkt_mask; \
621 a = (void *) &bucket2->data[pos * f->entry_size]; \
623 entries[pkt2_index] = a; \
624 lru_update(bucket2, pos); \
627 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out, entries, \
628 buckets_mask, buckets, keys, f) \
630 struct rte_bucket_4_16 *bucket_next; \
632 uint64_t pkt_mask, bucket_mask; \
636 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
638 lookup_key16_cmp(key, bucket2, pos); \
640 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
641 pkts_mask_out |= pkt_mask; \
643 a = (void *) &bucket2->data[pos * f->entry_size]; \
645 entries[pkt2_index] = a; \
647 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
648 buckets_mask |= bucket_mask; \
649 bucket_next = bucket2->next; \
650 buckets[pkt2_index] = bucket_next; \
651 keys[pkt2_index] = key; \
654 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\
657 struct rte_bucket_4_16 *bucket, *bucket_next; \
659 uint64_t pkt_mask, bucket_mask; \
663 bucket = buckets[pkt_index]; \
664 key = keys[pkt_index]; \
666 lookup_key16_cmp(key, bucket, pos); \
668 pkt_mask = (bucket->signature[pos] & 1LLU) << pkt_index;\
669 pkts_mask_out |= pkt_mask; \
671 a = (void *) &bucket->data[pos * f->entry_size]; \
673 entries[pkt_index] = a; \
675 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
676 buckets_mask |= bucket_mask; \
677 bucket_next = bucket->next; \
678 rte_prefetch0(bucket_next); \
679 rte_prefetch0((void *)(((uintptr_t) bucket_next) + RTE_CACHE_LINE_SIZE));\
680 buckets[pkt_index] = bucket_next; \
681 keys[pkt_index] = key; \
684 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
687 uint64_t pkt00_mask, pkt01_mask; \
689 pkt00_index = __builtin_ctzll(pkts_mask); \
690 pkt00_mask = 1LLU << pkt00_index; \
691 pkts_mask &= ~pkt00_mask; \
693 mbuf00 = pkts[pkt00_index]; \
694 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
696 pkt01_index = __builtin_ctzll(pkts_mask); \
697 pkt01_mask = 1LLU << pkt01_index; \
698 pkts_mask &= ~pkt01_mask; \
700 mbuf01 = pkts[pkt01_index]; \
701 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
704 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
705 mbuf00, mbuf01, pkts, pkts_mask) \
707 uint64_t pkt00_mask, pkt01_mask; \
709 pkt00_index = __builtin_ctzll(pkts_mask); \
710 pkt00_mask = 1LLU << pkt00_index; \
711 pkts_mask &= ~pkt00_mask; \
713 mbuf00 = pkts[pkt00_index]; \
714 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, 0)); \
716 pkt01_index = __builtin_ctzll(pkts_mask); \
717 if (pkts_mask == 0) \
718 pkt01_index = pkt00_index; \
719 pkt01_mask = 1LLU << pkt01_index; \
720 pkts_mask &= ~pkt01_mask; \
722 mbuf01 = pkts[pkt01_index]; \
723 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, 0)); \
726 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f) \
728 uint64_t signature10, signature11; \
729 uint32_t bucket10_index, bucket11_index; \
731 signature10 = RTE_MBUF_METADATA_UINT32(mbuf10, f->signature_offset);\
732 bucket10_index = signature10 & (f->n_buckets - 1); \
733 bucket10 = (struct rte_bucket_4_16 *) \
734 &f->memory[bucket10_index * f->bucket_size]; \
735 rte_prefetch0(bucket10); \
736 rte_prefetch0((void *)(((uintptr_t) bucket10) + RTE_CACHE_LINE_SIZE));\
738 signature11 = RTE_MBUF_METADATA_UINT32(mbuf11, f->signature_offset);\
739 bucket11_index = signature11 & (f->n_buckets - 1); \
740 bucket11 = (struct rte_bucket_4_16 *) \
741 &f->memory[bucket11_index * f->bucket_size]; \
742 rte_prefetch0(bucket11); \
743 rte_prefetch0((void *)(((uintptr_t) bucket11) + RTE_CACHE_LINE_SIZE));\
746 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
747 bucket20, bucket21, pkts_mask_out, entries, f) \
750 uint64_t pkt20_mask, pkt21_mask; \
751 uint64_t *key20, *key21; \
752 uint32_t pos20, pos21; \
754 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
755 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
757 lookup_key16_cmp(key20, bucket20, pos20); \
758 lookup_key16_cmp(key21, bucket21, pos21); \
760 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
761 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
762 pkts_mask_out |= pkt20_mask | pkt21_mask; \
764 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
765 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
766 rte_prefetch0(a20); \
767 rte_prefetch0(a21); \
768 entries[pkt20_index] = a20; \
769 entries[pkt21_index] = a21; \
770 lru_update(bucket20, pos20); \
771 lru_update(bucket21, pos21); \
774 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
775 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f) \
777 struct rte_bucket_4_16 *bucket20_next, *bucket21_next; \
779 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
780 uint64_t *key20, *key21; \
781 uint32_t pos20, pos21; \
783 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
784 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
786 lookup_key16_cmp(key20, bucket20, pos20); \
787 lookup_key16_cmp(key21, bucket21, pos21); \
789 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
790 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
791 pkts_mask_out |= pkt20_mask | pkt21_mask; \
793 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
794 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
795 rte_prefetch0(a20); \
796 rte_prefetch0(a21); \
797 entries[pkt20_index] = a20; \
798 entries[pkt21_index] = a21; \
800 bucket20_mask = (~pkt20_mask) & (bucket20->next_valid << pkt20_index);\
801 bucket21_mask = (~pkt21_mask) & (bucket21->next_valid << pkt21_index);\
802 buckets_mask |= bucket20_mask | bucket21_mask; \
803 bucket20_next = bucket20->next; \
804 bucket21_next = bucket21->next; \
805 buckets[pkt20_index] = bucket20_next; \
806 buckets[pkt21_index] = bucket21_next; \
807 keys[pkt20_index] = key20; \
808 keys[pkt21_index] = key21; \
812 rte_table_hash_lookup_key16_lru(
814 struct rte_mbuf **pkts,
816 uint64_t *lookup_hit_mask,
819 struct rte_table_hash *f = (struct rte_table_hash *) table;
820 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
821 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
822 uint32_t pkt00_index, pkt01_index, pkt10_index;
823 uint32_t pkt11_index, pkt20_index, pkt21_index;
824 uint64_t pkts_mask_out = 0;
826 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
827 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
829 /* Cannot run the pipeline with less than 5 packets */
830 if (__builtin_popcountll(pkts_mask) < 5) {
831 for ( ; pkts_mask; ) {
832 struct rte_bucket_4_16 *bucket;
833 struct rte_mbuf *mbuf;
836 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask);
837 lookup1_stage1(mbuf, bucket, f);
838 lookup1_stage2_lru(pkt_index, mbuf, bucket,
839 pkts_mask_out, entries, f);
842 *lookup_hit_mask = pkts_mask_out;
843 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
851 /* Pipeline stage 0 */
852 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
858 pkt10_index = pkt00_index;
859 pkt11_index = pkt01_index;
861 /* Pipeline stage 0 */
862 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
865 /* Pipeline stage 1 */
866 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
872 for ( ; pkts_mask; ) {
880 pkt20_index = pkt10_index;
881 pkt21_index = pkt11_index;
882 pkt10_index = pkt00_index;
883 pkt11_index = pkt01_index;
885 /* Pipeline stage 0 */
886 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
887 mbuf00, mbuf01, pkts, pkts_mask);
889 /* Pipeline stage 1 */
890 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
892 /* Pipeline stage 2 */
893 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
894 bucket20, bucket21, pkts_mask_out, entries, f);
908 pkt20_index = pkt10_index;
909 pkt21_index = pkt11_index;
910 pkt10_index = pkt00_index;
911 pkt11_index = pkt01_index;
913 /* Pipeline stage 1 */
914 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
916 /* Pipeline stage 2 */
917 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
918 bucket20, bucket21, pkts_mask_out, entries, f);
925 pkt20_index = pkt10_index;
926 pkt21_index = pkt11_index;
928 /* Pipeline stage 2 */
929 lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,
930 bucket20, bucket21, pkts_mask_out, entries, f);
932 *lookup_hit_mask = pkts_mask_out;
933 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
935 } /* rte_table_hash_lookup_key16_lru() */
938 rte_table_hash_lookup_key16_ext(
940 struct rte_mbuf **pkts,
942 uint64_t *lookup_hit_mask,
945 struct rte_table_hash *f = (struct rte_table_hash *) table;
946 struct rte_bucket_4_16 *bucket10, *bucket11, *bucket20, *bucket21;
947 struct rte_mbuf *mbuf00, *mbuf01, *mbuf10, *mbuf11, *mbuf20, *mbuf21;
948 uint32_t pkt00_index, pkt01_index, pkt10_index;
949 uint32_t pkt11_index, pkt20_index, pkt21_index;
950 uint64_t pkts_mask_out = 0, buckets_mask = 0;
951 struct rte_bucket_4_16 *buckets[RTE_PORT_IN_BURST_SIZE_MAX];
952 uint64_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
954 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
955 RTE_TABLE_HASH_KEY16_STATS_PKTS_IN_ADD(f, n_pkts_in);
957 /* Cannot run the pipeline with less than 5 packets */
958 if (__builtin_popcountll(pkts_mask) < 5) {
959 for ( ; pkts_mask; ) {
960 struct rte_bucket_4_16 *bucket;
961 struct rte_mbuf *mbuf;
964 lookup1_stage0(pkt_index, mbuf, pkts, pkts_mask);
965 lookup1_stage1(mbuf, bucket, f);
966 lookup1_stage2_ext(pkt_index, mbuf, bucket,
967 pkts_mask_out, entries, buckets_mask,
971 goto grind_next_buckets;
978 /* Pipeline stage 0 */
979 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
985 pkt10_index = pkt00_index;
986 pkt11_index = pkt01_index;
988 /* Pipeline stage 0 */
989 lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01, pkts,
992 /* Pipeline stage 1 */
993 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
999 for ( ; pkts_mask; ) {
1001 bucket20 = bucket10;
1002 bucket21 = bucket11;
1007 pkt20_index = pkt10_index;
1008 pkt21_index = pkt11_index;
1009 pkt10_index = pkt00_index;
1010 pkt11_index = pkt01_index;
1012 /* Pipeline stage 0 */
1013 lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,
1014 mbuf00, mbuf01, pkts, pkts_mask);
1016 /* Pipeline stage 1 */
1017 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1019 /* Pipeline stage 2 */
1020 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1021 bucket20, bucket21, pkts_mask_out, entries,
1022 buckets_mask, buckets, keys, f);
1030 bucket20 = bucket10;
1031 bucket21 = bucket11;
1036 pkt20_index = pkt10_index;
1037 pkt21_index = pkt11_index;
1038 pkt10_index = pkt00_index;
1039 pkt11_index = pkt01_index;
1041 /* Pipeline stage 1 */
1042 lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f);
1044 /* Pipeline stage 2 */
1045 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1046 bucket20, bucket21, pkts_mask_out, entries,
1047 buckets_mask, buckets, keys, f);
1050 bucket20 = bucket10;
1051 bucket21 = bucket11;
1054 pkt20_index = pkt10_index;
1055 pkt21_index = pkt11_index;
1057 /* Pipeline stage 2 */
1058 lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21,
1059 bucket20, bucket21, pkts_mask_out, entries,
1060 buckets_mask, buckets, keys, f);
1063 /* Grind next buckets */
1064 for ( ; buckets_mask; ) {
1065 uint64_t buckets_mask_next = 0;
1067 for ( ; buckets_mask; ) {
1071 pkt_index = __builtin_ctzll(buckets_mask);
1072 pkt_mask = 1LLU << pkt_index;
1073 buckets_mask &= ~pkt_mask;
1075 lookup_grinder(pkt_index, buckets, keys, pkts_mask_out,
1076 entries, buckets_mask_next, f);
1079 buckets_mask = buckets_mask_next;
1082 *lookup_hit_mask = pkts_mask_out;
1083 RTE_TABLE_HASH_KEY16_STATS_PKTS_LOOKUP_MISS(f, n_pkts_in - __builtin_popcountll(pkts_mask_out));
1085 } /* rte_table_hash_lookup_key16_ext() */
1088 rte_table_hash_key16_stats_read(void *table, struct rte_table_stats *stats, int clear)
1090 struct rte_table_hash *t = (struct rte_table_hash *) table;
1093 memcpy(stats, &t->stats, sizeof(t->stats));
1096 memset(&t->stats, 0, sizeof(t->stats));
1101 struct rte_table_ops rte_table_hash_key16_lru_ops = {
1102 .f_create = rte_table_hash_create_key16_lru,
1103 .f_free = rte_table_hash_free_key16_lru,
1104 .f_add = rte_table_hash_entry_add_key16_lru,
1105 .f_delete = rte_table_hash_entry_delete_key16_lru,
1106 .f_lookup = rte_table_hash_lookup_key16_lru,
1107 .f_stats = rte_table_hash_key16_stats_read,
1110 struct rte_table_ops rte_table_hash_key16_ext_ops = {
1111 .f_create = rte_table_hash_create_key16_ext,
1112 .f_free = rte_table_hash_free_key16_ext,
1113 .f_add = rte_table_hash_entry_add_key16_ext,
1114 .f_delete = rte_table_hash_entry_delete_key16_ext,
1115 .f_lookup = rte_table_hash_lookup_key16_ext,
1116 .f_stats = rte_table_hash_key16_stats_read,