2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 #include <rte_hash_crc.h>
19 #include <rte_table_hash.h>
20 #include <rte_version.h>
22 #include "hash_utils.h"
24 /* These opaque structure definitions were copied from DPDK lib/librte_table/rte_table_hash_key8.c */
26 struct rte_bucket_4_8 {
30 struct rte_bucket_4_8 *next;
39 struct rte_table_hash_key8 {
40 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
41 struct rte_table_stats stats;
43 /* Input parameters */
45 #if RTE_VERSION < RTE_VERSION_NUM(17,11,0,0)
46 uint32_t n_entries_per_bucket;
51 #if RTE_VERSION < RTE_VERSION_NUM(17,11,0,0)
52 uint32_t signature_offset;
55 #if RTE_VERSION >= RTE_VERSION_NUM(2,2,0,0)
58 rte_table_hash_op_hash f_hash;
61 /* Extendible buckets */
62 uint32_t n_buckets_ext;
67 uint8_t memory[0] __rte_cache_aligned;
70 /* These opaque structure definitions were copied from DPDK lib/librte_table/rte_table_hash_ext.c */
81 #define BUCKET_NEXT(bucket) \
82 ((void *) ((bucket)->next & (~1LU)))
91 struct rte_table_hash_ext {
92 #if RTE_VERSION >= RTE_VERSION_NUM(2,1,0,0)
93 struct rte_table_stats stats;
95 /* Input parameters */
100 uint32_t n_buckets_ext;
101 rte_table_hash_op_hash f_hash;
103 uint32_t signature_offset;
107 uint64_t bucket_mask;
108 uint32_t key_size_shl;
109 uint32_t data_size_shl;
110 uint32_t key_stack_tos;
111 uint32_t bkt_ext_stack_tos;
114 struct grinder grinders[64];
117 struct bucket *buckets;
118 struct bucket *buckets_ext;
122 uint32_t *bkt_ext_stack;
125 uint8_t memory[0] __rte_cache_aligned;
128 uint64_t get_bucket(void* table, uint32_t bucket_idx, void** key, void** entries)
130 struct rte_table_hash_ext *t = (struct rte_table_hash_ext *) table;
131 struct bucket *bkt0, *bkt, *bkt_prev;
133 uint32_t bkt_index, i;
135 bkt_index = bucket_idx & t->bucket_mask;
136 bkt0 = &t->buckets[bkt_index];
137 sig = (bucket_idx >> 16) | 1LLU;
139 /* Key is present in the bucket */
140 for (bkt = bkt0; bkt != NULL; bkt = BUCKET_NEXT(bkt)) {
141 for (i = 0; i < 4; i++) {
142 uint64_t bkt_sig = (uint64_t) bkt->sig[i];
143 uint32_t bkt_key_index = bkt->key_pos[i];
145 &t->key_mem[bkt_key_index << t->key_size_shl];
147 if (sig == bkt_sig) {
149 entries[n++] = &t->data_mem[bkt_key_index << t->data_size_shl];
150 /* Assume no more than 4 entries in total (including extended state) */
159 uint64_t get_bucket_key8(void* table, uint32_t bucket_idx, void** key, void** entries)
161 struct rte_bucket_4_8 *bucket, *bucket0;
162 struct rte_table_hash_key8* f = table;
165 bucket0 = (struct rte_bucket_4_8 *) &f->memory[bucket_idx * f->bucket_size];
166 for (bucket = bucket0; bucket != NULL; bucket = bucket->next) {
169 for (uint8_t i = 0, mask = 1LLU; i < 4; i++, mask <<= 1) {
170 uint64_t bucket_signature = bucket->signature;
172 if (bucket_signature & mask) {
173 key[n] = &bucket->key[i];
174 entries[n++] = &bucket->data[i *f->entry_size];
175 /* Assume no more than 4 entries
176 in total (including extended state) */
185 uint64_t hash_crc32(void* key, __attribute__((unused))void *key_mask, uint32_t key_size, uint64_t seed)
187 return rte_hash_crc(key, key_size, seed);