4 * Copyright(c) 2010-2017 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>
43 #include "rte_table_hash.h"
45 #ifdef RTE_TABLE_STATS_COLLECT
47 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(table, val) \
48 (table->stats.n_pkts_in += val)
49 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(table, val) \
50 (table->stats.n_pkts_lookup_miss += val)
54 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(table, val)
55 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(table, val)
60 struct rte_table_hash {
61 struct rte_table_stats stats;
63 /* Input parameters */
67 rte_table_hash_op_hash f_hash;
71 /* cuckoo hash table object */
72 struct rte_hash *h_table;
75 uint8_t memory[0] __rte_cache_aligned;
79 check_params_create_hash_cuckoo(struct rte_table_hash_params *params)
82 RTE_LOG(ERR, TABLE, "NULL Input Parameters.\n");
86 if (params->name == NULL) {
87 RTE_LOG(ERR, TABLE, "Table name is NULL.\n");
91 if (params->key_size == 0) {
92 RTE_LOG(ERR, TABLE, "Invalid key_size.\n");
96 if (params->n_keys == 0) {
97 RTE_LOG(ERR, TABLE, "Invalid n_keys.\n");
101 if (params->f_hash == NULL) {
102 RTE_LOG(ERR, TABLE, "f_hash is NULL.\n");
110 rte_table_hash_cuckoo_create(void *params,
114 struct rte_table_hash_params *p = params;
115 struct rte_hash *h_table;
116 struct rte_table_hash *t;
119 /* Check input parameters */
120 if (check_params_create_hash_cuckoo(params))
123 /* Memory allocation */
124 total_size = sizeof(struct rte_table_hash) +
125 RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
127 t = rte_zmalloc_socket(p->name, total_size, RTE_CACHE_LINE_SIZE, socket_id);
130 "%s: Cannot allocate %u bytes for cuckoo hash table %s\n",
131 __func__, total_size, p->name);
135 /* Create cuckoo hash table */
136 struct rte_hash_parameters hash_cuckoo_params = {
137 .entries = p->n_keys,
138 .key_len = p->key_size,
139 .hash_func = (rte_hash_function)(p->f_hash),
140 .hash_func_init_val = p->seed,
141 .socket_id = socket_id,
145 h_table = rte_hash_find_existing(p->name);
146 if (h_table == NULL) {
147 h_table = rte_hash_create(&hash_cuckoo_params);
148 if (h_table == NULL) {
150 "%s: failed to create cuckoo hash table %s\n",
157 /* initialize the cuckoo hash parameters */
158 t->key_size = p->key_size;
159 t->entry_size = entry_size;
160 t->n_keys = p->n_keys;
161 t->f_hash = p->f_hash;
163 t->key_offset = p->key_offset;
164 t->h_table = h_table;
167 "%s: Cuckoo hash table %s memory footprint is %u bytes\n",
168 __func__, p->name, total_size);
173 rte_table_hash_cuckoo_free(void *table) {
174 struct rte_table_hash *t = table;
179 rte_hash_free(t->h_table);
186 rte_table_hash_cuckoo_entry_add(void *table, void *key, void *entry,
187 int *key_found, void **entry_ptr)
189 struct rte_table_hash *t = table;
192 /* Check input parameters */
193 if ((table == NULL) ||
196 (key_found == NULL) ||
200 /* Find Existing entries */
201 pos = rte_hash_lookup(t->h_table, key);
203 uint8_t *existing_entry;
206 existing_entry = &t->memory[pos * t->entry_size];
207 memcpy(existing_entry, entry, t->entry_size);
208 *entry_ptr = existing_entry;
213 if (pos == -ENOENT) {
214 /* Entry not found. Adding new entry */
217 pos = rte_hash_add_key(t->h_table, key);
221 new_entry = &t->memory[pos * t->entry_size];
222 memcpy(new_entry, entry, t->entry_size);
225 *entry_ptr = new_entry;
233 rte_table_hash_cuckoo_entry_delete(void *table, void *key,
234 int *key_found, void *entry)
236 struct rte_table_hash *t = table;
239 /* Check input parameters */
240 if ((table == NULL) ||
245 pos = rte_hash_del_key(t->h_table, key);
248 uint8_t *entry_ptr = &t->memory[pos * t->entry_size];
251 memcpy(entry, entry_ptr, t->entry_size);
253 memset(&t->memory[pos * t->entry_size], 0, t->entry_size);
262 rte_table_hash_cuckoo_lookup(void *table,
263 struct rte_mbuf **pkts,
265 uint64_t *lookup_hit_mask,
268 struct rte_table_hash *t = table;
269 uint64_t pkts_mask_out = 0;
272 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
274 RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(t, n_pkts_in);
276 if ((pkts_mask & (pkts_mask + 1)) == 0) {
277 const uint8_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
278 int32_t positions[RTE_PORT_IN_BURST_SIZE_MAX], status;
280 /* Keys for bulk lookup */
281 for (i = 0; i < n_pkts_in; i++)
282 keys[i] = RTE_MBUF_METADATA_UINT8_PTR(pkts[i],
286 status = rte_hash_lookup_bulk(t->h_table,
287 (const void **) keys,
291 for (i = 0; i < n_pkts_in; i++) {
292 if (likely(positions[i] >= 0)) {
293 uint64_t pkt_mask = 1LLU << i;
295 entries[i] = &t->memory[positions[i]
297 pkts_mask_out |= pkt_mask;
302 for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX
303 - __builtin_clzll(pkts_mask)); i++) {
304 uint64_t pkt_mask = 1LLU << i;
306 if (pkt_mask & pkts_mask) {
307 struct rte_mbuf *pkt = pkts[i];
308 uint8_t *key = RTE_MBUF_METADATA_UINT8_PTR(pkt,
312 pos = rte_hash_lookup(t->h_table, key);
313 if (likely(pos >= 0)) {
314 entries[i] = &t->memory[pos
316 pkts_mask_out |= pkt_mask;
321 *lookup_hit_mask = pkts_mask_out;
322 RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(t,
323 n_pkts_in - __builtin_popcountll(pkts_mask_out));
330 rte_table_hash_cuckoo_stats_read(void *table, struct rte_table_stats *stats,
333 struct rte_table_hash *t = table;
336 memcpy(stats, &t->stats, sizeof(t->stats));
339 memset(&t->stats, 0, sizeof(t->stats));
344 struct rte_table_ops rte_table_hash_cuckoo_ops = {
345 .f_create = rte_table_hash_cuckoo_create,
346 .f_free = rte_table_hash_cuckoo_free,
347 .f_add = rte_table_hash_cuckoo_entry_add,
348 .f_delete = rte_table_hash_cuckoo_entry_delete,
350 .f_delete_bulk = NULL,
351 .f_lookup = rte_table_hash_cuckoo_lookup,
352 .f_stats = rte_table_hash_cuckoo_stats_read,