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;
89 rte_table_hash_op_hash_nomask f_hash;
92 /* Extendible buckets */
93 uint32_t n_buckets_ext;
98 uint8_t memory[0] __rte_cache_aligned;
102 check_params_create_lru(struct rte_table_hash_key16_lru_params *params) {
104 if (params->n_entries == 0) {
105 RTE_LOG(ERR, TABLE, "%s: n_entries is zero\n", __func__);
110 if (params->f_hash == NULL) {
112 "%s: f_hash function pointer is NULL\n", __func__);
120 rte_table_hash_create_key16_lru(void *params,
124 struct rte_table_hash_key16_lru_params *p =
125 (struct rte_table_hash_key16_lru_params *) params;
126 struct rte_table_hash *f;
127 uint32_t n_buckets, n_entries_per_bucket,
128 key_size, bucket_size_cl, total_size, i;
130 /* Check input parameters */
131 if ((check_params_create_lru(p) != 0) ||
132 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
133 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
135 n_entries_per_bucket = 4;
138 /* Memory allocation */
139 n_buckets = rte_align32pow2((p->n_entries + n_entries_per_bucket - 1) /
140 n_entries_per_bucket);
141 bucket_size_cl = (sizeof(struct rte_bucket_4_16) + n_entries_per_bucket
142 * entry_size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
143 total_size = sizeof(struct rte_table_hash) + n_buckets *
144 bucket_size_cl * RTE_CACHE_LINE_SIZE;
146 f = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
149 "%s: Cannot allocate %u bytes for hash table\n",
150 __func__, total_size);
154 "%s: Hash table memory footprint is %u bytes\n",
155 __func__, total_size);
157 /* Memory initialization */
158 f->n_buckets = n_buckets;
159 f->n_entries_per_bucket = n_entries_per_bucket;
160 f->key_size = key_size;
161 f->entry_size = entry_size;
162 f->bucket_size = bucket_size_cl * RTE_CACHE_LINE_SIZE;
163 f->signature_offset = p->signature_offset;
164 f->key_offset = p->key_offset;
165 f->f_hash = p->f_hash;
168 if (p->key_mask != NULL) {
169 f->key_mask[0] = ((uint64_t *)p->key_mask)[0];
170 f->key_mask[1] = ((uint64_t *)p->key_mask)[1];
172 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
173 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
176 for (i = 0; i < n_buckets; i++) {
177 struct rte_bucket_4_16 *bucket;
179 bucket = (struct rte_bucket_4_16 *) &f->memory[i *
188 rte_table_hash_free_key16_lru(void *table)
190 struct rte_table_hash *f = table;
192 /* Check input parameters */
194 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
203 rte_table_hash_entry_add_key16_lru(
210 struct rte_table_hash *f = table;
211 struct rte_bucket_4_16 *bucket;
212 uint64_t signature, pos;
213 uint32_t bucket_index, i;
215 signature = f->f_hash(key, f->key_size, f->seed);
216 bucket_index = signature & (f->n_buckets - 1);
217 bucket = (struct rte_bucket_4_16 *)
218 &f->memory[bucket_index * f->bucket_size];
219 signature |= RTE_BUCKET_ENTRY_VALID;
221 /* Key is present in the bucket */
222 for (i = 0; i < 4; i++) {
223 uint64_t bucket_signature = bucket->signature[i];
224 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
226 if ((bucket_signature == signature) &&
227 (memcmp(key, bucket_key, f->key_size) == 0)) {
228 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
230 memcpy(bucket_data, entry, f->entry_size);
231 lru_update(bucket, i);
233 *entry_ptr = (void *) bucket_data;
238 /* Key is not present in the bucket */
239 for (i = 0; i < 4; i++) {
240 uint64_t bucket_signature = bucket->signature[i];
241 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
243 if (bucket_signature == 0) {
244 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
246 bucket->signature[i] = signature;
247 memcpy(bucket_key, key, f->key_size);
248 memcpy(bucket_data, entry, f->entry_size);
249 lru_update(bucket, i);
251 *entry_ptr = (void *) bucket_data;
257 /* Bucket full: replace LRU entry */
258 pos = lru_pos(bucket);
259 bucket->signature[pos] = signature;
260 memcpy(bucket->key[pos], key, f->key_size);
261 memcpy(&bucket->data[pos * f->entry_size], entry, f->entry_size);
262 lru_update(bucket, pos);
264 *entry_ptr = (void *) &bucket->data[pos * f->entry_size];
270 rte_table_hash_entry_delete_key16_lru(
276 struct rte_table_hash *f = table;
277 struct rte_bucket_4_16 *bucket;
279 uint32_t bucket_index, i;
281 signature = f->f_hash(key, f->key_size, f->seed);
282 bucket_index = signature & (f->n_buckets - 1);
283 bucket = (struct rte_bucket_4_16 *)
284 &f->memory[bucket_index * f->bucket_size];
285 signature |= RTE_BUCKET_ENTRY_VALID;
287 /* Key is present in the bucket */
288 for (i = 0; i < 4; i++) {
289 uint64_t bucket_signature = bucket->signature[i];
290 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
292 if ((bucket_signature == signature) &&
293 (memcmp(key, bucket_key, f->key_size) == 0)) {
294 uint8_t *bucket_data = &bucket->data[i * f->entry_size];
296 bucket->signature[i] = 0;
299 memcpy(entry, bucket_data, f->entry_size);
304 /* Key is not present in the bucket */
310 check_params_create_ext(struct rte_table_hash_key16_ext_params *params) {
312 if (params->n_entries == 0) {
313 RTE_LOG(ERR, TABLE, "%s: n_entries is zero\n", __func__);
318 if (params->n_entries_ext == 0) {
319 RTE_LOG(ERR, TABLE, "%s: n_entries_ext is zero\n", __func__);
324 if (params->f_hash == NULL) {
326 "%s: f_hash function pointer is NULL\n", __func__);
334 rte_table_hash_create_key16_ext(void *params,
338 struct rte_table_hash_key16_ext_params *p =
339 (struct rte_table_hash_key16_ext_params *) params;
340 struct rte_table_hash *f;
341 uint32_t n_buckets, n_buckets_ext, n_entries_per_bucket, key_size,
342 bucket_size_cl, stack_size_cl, total_size, i;
344 /* Check input parameters */
345 if ((check_params_create_ext(p) != 0) ||
346 ((sizeof(struct rte_table_hash) % RTE_CACHE_LINE_SIZE) != 0) ||
347 ((sizeof(struct rte_bucket_4_16) % 64) != 0))
350 n_entries_per_bucket = 4;
353 /* Memory allocation */
354 n_buckets = rte_align32pow2((p->n_entries + n_entries_per_bucket - 1) /
355 n_entries_per_bucket);
356 n_buckets_ext = (p->n_entries_ext + n_entries_per_bucket - 1) /
357 n_entries_per_bucket;
358 bucket_size_cl = (sizeof(struct rte_bucket_4_16) + n_entries_per_bucket
359 * entry_size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE;
360 stack_size_cl = (n_buckets_ext * sizeof(uint32_t) + RTE_CACHE_LINE_SIZE - 1)
361 / RTE_CACHE_LINE_SIZE;
362 total_size = sizeof(struct rte_table_hash) +
363 ((n_buckets + n_buckets_ext) * bucket_size_cl + stack_size_cl) *
366 f = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE, socket_id);
369 "%s: Cannot allocate %u bytes for hash table\n",
370 __func__, total_size);
374 "%s: Hash table memory footprint is %u bytes\n",
375 __func__, total_size);
377 /* Memory initialization */
378 f->n_buckets = n_buckets;
379 f->n_entries_per_bucket = n_entries_per_bucket;
380 f->key_size = key_size;
381 f->entry_size = entry_size;
382 f->bucket_size = bucket_size_cl * RTE_CACHE_LINE_SIZE;
383 f->signature_offset = p->signature_offset;
384 f->key_offset = p->key_offset;
385 f->f_hash = p->f_hash;
388 f->n_buckets_ext = n_buckets_ext;
389 f->stack_pos = n_buckets_ext;
390 f->stack = (uint32_t *)
391 &f->memory[(n_buckets + n_buckets_ext) * f->bucket_size];
393 for (i = 0; i < n_buckets_ext; i++)
396 if (p->key_mask != NULL) {
397 f->key_mask[0] = (((uint64_t *)p->key_mask)[0]);
398 f->key_mask[1] = (((uint64_t *)p->key_mask)[1]);
400 f->key_mask[0] = 0xFFFFFFFFFFFFFFFFLLU;
401 f->key_mask[1] = 0xFFFFFFFFFFFFFFFFLLU;
408 rte_table_hash_free_key16_ext(void *table)
410 struct rte_table_hash *f = table;
412 /* Check input parameters */
414 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
423 rte_table_hash_entry_add_key16_ext(
430 struct rte_table_hash *f = table;
431 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
433 uint32_t bucket_index, i;
435 signature = f->f_hash(key, f->key_size, f->seed);
436 bucket_index = signature & (f->n_buckets - 1);
437 bucket0 = (struct rte_bucket_4_16 *)
438 &f->memory[bucket_index * f->bucket_size];
439 signature |= RTE_BUCKET_ENTRY_VALID;
441 /* Key is present in the bucket */
442 for (bucket = bucket0; bucket != NULL; bucket = bucket->next)
443 for (i = 0; i < 4; i++) {
444 uint64_t bucket_signature = bucket->signature[i];
445 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
447 if ((bucket_signature == signature) &&
448 (memcmp(key, bucket_key, f->key_size) == 0)) {
449 uint8_t *bucket_data = &bucket->data[i *
452 memcpy(bucket_data, entry, f->entry_size);
454 *entry_ptr = (void *) bucket_data;
459 /* Key is not present in the bucket */
460 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
461 bucket_prev = bucket, bucket = bucket->next)
462 for (i = 0; i < 4; i++) {
463 uint64_t bucket_signature = bucket->signature[i];
464 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
466 if (bucket_signature == 0) {
467 uint8_t *bucket_data = &bucket->data[i *
470 bucket->signature[i] = signature;
471 memcpy(bucket_key, key, f->key_size);
472 memcpy(bucket_data, entry, f->entry_size);
474 *entry_ptr = (void *) bucket_data;
480 /* Bucket full: extend bucket */
481 if (f->stack_pos > 0) {
482 bucket_index = f->stack[--f->stack_pos];
484 bucket = (struct rte_bucket_4_16 *) &f->memory[(f->n_buckets +
485 bucket_index) * f->bucket_size];
486 bucket_prev->next = bucket;
487 bucket_prev->next_valid = 1;
489 bucket->signature[0] = signature;
490 memcpy(bucket->key[0], key, f->key_size);
491 memcpy(&bucket->data[0], entry, f->entry_size);
493 *entry_ptr = (void *) &bucket->data[0];
501 rte_table_hash_entry_delete_key16_ext(
507 struct rte_table_hash *f = table;
508 struct rte_bucket_4_16 *bucket0, *bucket, *bucket_prev;
510 uint32_t bucket_index, i;
512 signature = f->f_hash(key, f->key_size, f->seed);
513 bucket_index = signature & (f->n_buckets - 1);
514 bucket0 = (struct rte_bucket_4_16 *)
515 &f->memory[bucket_index * f->bucket_size];
516 signature |= RTE_BUCKET_ENTRY_VALID;
518 /* Key is present in the bucket */
519 for (bucket_prev = NULL, bucket = bucket0; bucket != NULL;
520 bucket_prev = bucket, bucket = bucket->next)
521 for (i = 0; i < 4; i++) {
522 uint64_t bucket_signature = bucket->signature[i];
523 uint8_t *bucket_key = (uint8_t *) bucket->key[i];
525 if ((bucket_signature == signature) &&
526 (memcmp(key, bucket_key, f->key_size) == 0)) {
527 uint8_t *bucket_data = &bucket->data[i *
530 bucket->signature[i] = 0;
533 memcpy(entry, bucket_data,
536 if ((bucket->signature[0] == 0) &&
537 (bucket->signature[1] == 0) &&
538 (bucket->signature[2] == 0) &&
539 (bucket->signature[3] == 0) &&
540 (bucket_prev != NULL)) {
541 bucket_prev->next = bucket->next;
542 bucket_prev->next_valid =
546 sizeof(struct rte_bucket_4_16));
547 bucket_index = (((uint8_t *)bucket -
548 (uint8_t *)f->memory)/f->bucket_size) - f->n_buckets;
549 f->stack[f->stack_pos++] = bucket_index;
556 /* Key is not present in the bucket */
561 #define lookup_key16_cmp(key_in, bucket, pos) \
563 uint64_t xor[4][2], or[4], signature[4]; \
565 signature[0] = (~bucket->signature[0]) & 1; \
566 signature[1] = (~bucket->signature[1]) & 1; \
567 signature[2] = (~bucket->signature[2]) & 1; \
568 signature[3] = (~bucket->signature[3]) & 1; \
570 xor[0][0] = key_in[0] ^ bucket->key[0][0]; \
571 xor[0][1] = key_in[1] ^ bucket->key[0][1]; \
573 xor[1][0] = key_in[0] ^ bucket->key[1][0]; \
574 xor[1][1] = key_in[1] ^ bucket->key[1][1]; \
576 xor[2][0] = key_in[0] ^ bucket->key[2][0]; \
577 xor[2][1] = key_in[1] ^ bucket->key[2][1]; \
579 xor[3][0] = key_in[0] ^ bucket->key[3][0]; \
580 xor[3][1] = key_in[1] ^ bucket->key[3][1]; \
582 or[0] = xor[0][0] | xor[0][1] | signature[0]; \
583 or[1] = xor[1][0] | xor[1][1] | signature[1]; \
584 or[2] = xor[2][0] | xor[2][1] | signature[2]; \
585 or[3] = xor[3][0] | xor[3][1] | signature[3]; \
598 #define lookup1_stage0(pkt0_index, mbuf0, pkts, pkts_mask, f) \
601 uint32_t key_offset = f->key_offset;\
603 pkt0_index = __builtin_ctzll(pkts_mask); \
604 pkt_mask = 1LLU << pkt0_index; \
605 pkts_mask &= ~pkt_mask; \
607 mbuf0 = pkts[pkt0_index]; \
608 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf0, key_offset));\
611 #define lookup1_stage1(mbuf1, bucket1, f) \
614 uint64_t signature = 0; \
615 uint32_t bucket_index; \
616 uint64_t hash_key_buffer[2]; \
618 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf1, f->key_offset);\
620 hash_key_buffer[0] = key[0] & f->key_mask[0]; \
621 hash_key_buffer[1] = key[1] & f->key_mask[1]; \
622 signature = f->f_hash(hash_key_buffer, \
623 RTE_TABLE_HASH_KEY_SIZE, f->seed); \
625 bucket_index = signature & (f->n_buckets - 1); \
626 bucket1 = (struct rte_bucket_4_16 *) \
627 &f->memory[bucket_index * f->bucket_size]; \
628 rte_prefetch0(bucket1); \
629 rte_prefetch0((void *)(((uintptr_t) bucket1) + RTE_CACHE_LINE_SIZE));\
632 #define lookup1_stage2_lru(pkt2_index, mbuf2, bucket2, \
633 pkts_mask_out, entries, f) \
638 uint64_t hash_key_buffer[2]; \
641 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
642 hash_key_buffer[0] = key[0] & f->key_mask[0]; \
643 hash_key_buffer[1] = key[1] & f->key_mask[1]; \
645 lookup_key16_cmp(hash_key_buffer, bucket2, pos); \
647 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
648 pkts_mask_out |= pkt_mask; \
650 a = (void *) &bucket2->data[pos * f->entry_size]; \
652 entries[pkt2_index] = a; \
653 lru_update(bucket2, pos); \
656 #define lookup1_stage2_ext(pkt2_index, mbuf2, bucket2, pkts_mask_out, entries, \
657 buckets_mask, buckets, keys, f) \
659 struct rte_bucket_4_16 *bucket_next; \
661 uint64_t pkt_mask, bucket_mask; \
663 uint64_t hash_key_buffer[2]; \
666 key = RTE_MBUF_METADATA_UINT64_PTR(mbuf2, f->key_offset);\
667 hash_key_buffer[0] = key[0] & f->key_mask[0]; \
668 hash_key_buffer[1] = key[1] & f->key_mask[1]; \
670 lookup_key16_cmp(hash_key_buffer, bucket2, pos); \
672 pkt_mask = (bucket2->signature[pos] & 1LLU) << pkt2_index;\
673 pkts_mask_out |= pkt_mask; \
675 a = (void *) &bucket2->data[pos * f->entry_size]; \
677 entries[pkt2_index] = a; \
679 bucket_mask = (~pkt_mask) & (bucket2->next_valid << pkt2_index);\
680 buckets_mask |= bucket_mask; \
681 bucket_next = bucket2->next; \
682 buckets[pkt2_index] = bucket_next; \
683 keys[pkt2_index] = key; \
686 #define lookup_grinder(pkt_index, buckets, keys, pkts_mask_out, entries,\
689 struct rte_bucket_4_16 *bucket, *bucket_next; \
691 uint64_t pkt_mask, bucket_mask; \
693 uint64_t hash_key_buffer[2]; \
696 bucket = buckets[pkt_index]; \
697 key = keys[pkt_index]; \
698 hash_key_buffer[0] = key[0] & f->key_mask[0]; \
699 hash_key_buffer[1] = key[1] & f->key_mask[1]; \
701 lookup_key16_cmp(hash_key_buffer, bucket, pos); \
703 pkt_mask = (bucket->signature[pos] & 1LLU) << pkt_index;\
704 pkts_mask_out |= pkt_mask; \
706 a = (void *) &bucket->data[pos * f->entry_size]; \
708 entries[pkt_index] = a; \
710 bucket_mask = (~pkt_mask) & (bucket->next_valid << pkt_index);\
711 buckets_mask |= bucket_mask; \
712 bucket_next = bucket->next; \
713 rte_prefetch0(bucket_next); \
714 rte_prefetch0((void *)(((uintptr_t) bucket_next) + RTE_CACHE_LINE_SIZE));\
715 buckets[pkt_index] = bucket_next; \
716 keys[pkt_index] = key; \
719 #define lookup2_stage0(pkt00_index, pkt01_index, mbuf00, mbuf01,\
720 pkts, pkts_mask, f) \
722 uint64_t pkt00_mask, pkt01_mask; \
723 uint32_t key_offset = f->key_offset; \
725 pkt00_index = __builtin_ctzll(pkts_mask); \
726 pkt00_mask = 1LLU << pkt00_index; \
727 pkts_mask &= ~pkt00_mask; \
729 mbuf00 = pkts[pkt00_index]; \
730 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf00, key_offset));\
732 pkt01_index = __builtin_ctzll(pkts_mask); \
733 pkt01_mask = 1LLU << pkt01_index; \
734 pkts_mask &= ~pkt01_mask; \
736 mbuf01 = pkts[pkt01_index]; \
737 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset));\
740 #define lookup2_stage0_with_odd_support(pkt00_index, pkt01_index,\
741 mbuf00, mbuf01, 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 if (pkts_mask == 0) \
755 pkt01_index = pkt00_index; \
756 pkt01_mask = 1LLU << pkt01_index; \
757 pkts_mask &= ~pkt01_mask; \
759 mbuf01 = pkts[pkt01_index]; \
760 rte_prefetch0(RTE_MBUF_METADATA_UINT8_PTR(mbuf01, key_offset)); \
763 #define lookup2_stage1(mbuf10, mbuf11, bucket10, bucket11, f) \
765 uint64_t *key10, *key11; \
766 uint64_t hash_offset_buffer[2]; \
767 uint64_t signature10, signature11; \
768 uint32_t bucket10_index, bucket11_index; \
770 key10 = RTE_MBUF_METADATA_UINT64_PTR(mbuf10, f->key_offset);\
771 hash_offset_buffer[0] = key10[0] & f->key_mask[0]; \
772 hash_offset_buffer[1] = key10[1] & f->key_mask[1]; \
773 signature10 = f->f_hash(hash_offset_buffer, \
774 RTE_TABLE_HASH_KEY_SIZE, f->seed);\
775 bucket10_index = signature10 & (f->n_buckets - 1); \
776 bucket10 = (struct rte_bucket_4_16 *) \
777 &f->memory[bucket10_index * f->bucket_size]; \
778 rte_prefetch0(bucket10); \
779 rte_prefetch0((void *)(((uintptr_t) bucket10) + RTE_CACHE_LINE_SIZE));\
781 key11 = RTE_MBUF_METADATA_UINT64_PTR(mbuf11, f->key_offset);\
782 hash_offset_buffer[0] = key11[0] & f->key_mask[0]; \
783 hash_offset_buffer[1] = key11[1] & f->key_mask[1]; \
784 signature11 = f->f_hash(hash_offset_buffer, \
785 RTE_TABLE_HASH_KEY_SIZE, f->seed);\
786 bucket11_index = signature11 & (f->n_buckets - 1); \
787 bucket11 = (struct rte_bucket_4_16 *) \
788 &f->memory[bucket11_index * f->bucket_size]; \
789 rte_prefetch0(bucket11); \
790 rte_prefetch0((void *)(((uintptr_t) bucket11) + RTE_CACHE_LINE_SIZE));\
793 #define lookup2_stage2_lru(pkt20_index, pkt21_index, mbuf20, mbuf21,\
794 bucket20, bucket21, pkts_mask_out, entries, f) \
797 uint64_t pkt20_mask, pkt21_mask; \
798 uint64_t *key20, *key21; \
799 uint64_t hash_key_buffer20[2]; \
800 uint64_t hash_key_buffer21[2]; \
801 uint32_t pos20, pos21; \
803 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
804 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
805 hash_key_buffer20[0] = key20[0] & f->key_mask[0]; \
806 hash_key_buffer20[1] = key20[1] & f->key_mask[1]; \
807 hash_key_buffer21[0] = key21[0] & f->key_mask[0]; \
808 hash_key_buffer21[1] = key21[1] & f->key_mask[1]; \
810 lookup_key16_cmp(hash_key_buffer20, bucket20, pos20); \
811 lookup_key16_cmp(hash_key_buffer21, bucket21, pos21); \
813 pkt20_mask = (bucket20->signature[pos20] & 1LLU) << pkt20_index;\
814 pkt21_mask = (bucket21->signature[pos21] & 1LLU) << pkt21_index;\
815 pkts_mask_out |= pkt20_mask | pkt21_mask; \
817 a20 = (void *) &bucket20->data[pos20 * f->entry_size]; \
818 a21 = (void *) &bucket21->data[pos21 * f->entry_size]; \
819 rte_prefetch0(a20); \
820 rte_prefetch0(a21); \
821 entries[pkt20_index] = a20; \
822 entries[pkt21_index] = a21; \
823 lru_update(bucket20, pos20); \
824 lru_update(bucket21, pos21); \
827 #define lookup2_stage2_ext(pkt20_index, pkt21_index, mbuf20, mbuf21, bucket20, \
828 bucket21, pkts_mask_out, entries, buckets_mask, buckets, keys, f) \
830 struct rte_bucket_4_16 *bucket20_next, *bucket21_next; \
832 uint64_t pkt20_mask, pkt21_mask, bucket20_mask, bucket21_mask;\
833 uint64_t *key20, *key21; \
834 uint64_t hash_key_buffer20[2]; \
835 uint64_t hash_key_buffer21[2]; \
836 uint32_t pos20, pos21; \
838 key20 = RTE_MBUF_METADATA_UINT64_PTR(mbuf20, f->key_offset);\
839 key21 = RTE_MBUF_METADATA_UINT64_PTR(mbuf21, f->key_offset);\
840 hash_key_buffer20[0] = key20[0] & f->key_mask[0]; \
841 hash_key_buffer20[1] = key20[1] & f->key_mask[1]; \
842 hash_key_buffer21[0] = key21[0] & f->key_mask[0]; \
843 hash_key_buffer21[1] = key21[1] & f->key_mask[1]; \
845 lookup_key16_cmp(hash_key_buffer20, bucket20, pos20); \
846 lookup_key16_cmp(hash_key_buffer21, bucket21, pos21); \
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));
997 } /* rte_table_hash_lookup_key16_lru() */
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));
1149 } /* rte_table_hash_lookup_key16_ext() */
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,
1170 .f_lookup = rte_table_hash_lookup_key16_lru,
1171 .f_stats = rte_table_hash_key16_stats_read,
1174 struct rte_table_ops rte_table_hash_key16_ext_ops = {
1175 .f_create = rte_table_hash_create_key16_ext,
1176 .f_free = rte_table_hash_free_key16_ext,
1177 .f_add = rte_table_hash_entry_add_key16_ext,
1178 .f_delete = rte_table_hash_entry_delete_key16_ext,
1179 .f_lookup = rte_table_hash_lookup_key16_ext,
1180 .f_stats = rte_table_hash_key16_stats_read,