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_cuckoo.h"
13 #ifdef RTE_TABLE_STATS_COLLECT
15 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(table, val) \
16 (table->stats.n_pkts_in += val)
17 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(table, val) \
18 (table->stats.n_pkts_lookup_miss += val)
22 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(table, val)
23 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(table, val)
28 struct rte_table_hash {
29 struct rte_table_stats stats;
31 /* Input parameters */
35 rte_hash_function f_hash;
39 /* cuckoo hash table object */
40 struct rte_hash *h_table;
43 uint8_t memory[0] __rte_cache_aligned;
47 check_params_create_hash_cuckoo(struct rte_table_hash_cuckoo_params *params)
50 RTE_LOG(ERR, TABLE, "NULL Input Parameters.\n");
54 if (params->name == NULL) {
55 RTE_LOG(ERR, TABLE, "Table name is NULL.\n");
59 if (params->key_size == 0) {
60 RTE_LOG(ERR, TABLE, "Invalid key_size.\n");
64 if (params->n_keys == 0) {
65 RTE_LOG(ERR, TABLE, "Invalid n_keys.\n");
69 if (params->f_hash == NULL) {
70 RTE_LOG(ERR, TABLE, "f_hash is NULL.\n");
78 rte_table_hash_cuckoo_create(void *params,
82 struct rte_table_hash_cuckoo_params *p = params;
83 struct rte_hash *h_table;
84 struct rte_table_hash *t;
87 /* Check input parameters */
88 if (check_params_create_hash_cuckoo(params))
91 /* Memory allocation */
92 total_size = sizeof(struct rte_table_hash) +
93 RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
95 t = rte_zmalloc_socket(p->name, total_size, RTE_CACHE_LINE_SIZE, socket_id);
98 "%s: Cannot allocate %u bytes for cuckoo hash table %s\n",
99 __func__, total_size, p->name);
103 /* Create cuckoo hash table */
104 struct rte_hash_parameters hash_cuckoo_params = {
105 .entries = p->n_keys,
106 .key_len = p->key_size,
107 .hash_func = p->f_hash,
108 .hash_func_init_val = p->seed,
109 .socket_id = socket_id,
113 h_table = rte_hash_find_existing(p->name);
114 if (h_table == NULL) {
115 h_table = rte_hash_create(&hash_cuckoo_params);
116 if (h_table == NULL) {
118 "%s: failed to create cuckoo hash table %s\n",
125 /* initialize the cuckoo hash parameters */
126 t->key_size = p->key_size;
127 t->entry_size = entry_size;
128 t->n_keys = p->n_keys;
129 t->f_hash = p->f_hash;
131 t->key_offset = p->key_offset;
132 t->h_table = h_table;
135 "%s: Cuckoo hash table %s memory footprint is %u bytes\n",
136 __func__, p->name, total_size);
141 rte_table_hash_cuckoo_free(void *table) {
142 struct rte_table_hash *t = table;
147 rte_hash_free(t->h_table);
154 rte_table_hash_cuckoo_entry_add(void *table, void *key, void *entry,
155 int *key_found, void **entry_ptr)
157 struct rte_table_hash *t = table;
160 /* Check input parameters */
161 if ((table == NULL) ||
164 (key_found == NULL) ||
168 /* Find Existing entries */
169 pos = rte_hash_lookup(t->h_table, key);
171 uint8_t *existing_entry;
174 existing_entry = &t->memory[pos * t->entry_size];
175 memcpy(existing_entry, entry, t->entry_size);
176 *entry_ptr = existing_entry;
181 if (pos == -ENOENT) {
182 /* Entry not found. Adding new entry */
185 pos = rte_hash_add_key(t->h_table, key);
189 new_entry = &t->memory[pos * t->entry_size];
190 memcpy(new_entry, entry, t->entry_size);
193 *entry_ptr = new_entry;
201 rte_table_hash_cuckoo_entry_delete(void *table, void *key,
202 int *key_found, void *entry)
204 struct rte_table_hash *t = table;
207 /* Check input parameters */
208 if ((table == NULL) ||
213 pos = rte_hash_del_key(t->h_table, key);
216 uint8_t *entry_ptr = &t->memory[pos * t->entry_size];
219 memcpy(entry, entry_ptr, t->entry_size);
221 memset(&t->memory[pos * t->entry_size], 0, t->entry_size);
230 rte_table_hash_cuckoo_lookup(void *table,
231 struct rte_mbuf **pkts,
233 uint64_t *lookup_hit_mask,
236 struct rte_table_hash *t = table;
237 uint64_t pkts_mask_out = 0;
240 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
242 RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(t, n_pkts_in);
244 if ((pkts_mask & (pkts_mask + 1)) == 0) {
245 const uint8_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
246 int32_t positions[RTE_PORT_IN_BURST_SIZE_MAX], status;
248 /* Keys for bulk lookup */
249 for (i = 0; i < n_pkts_in; i++)
250 keys[i] = RTE_MBUF_METADATA_UINT8_PTR(pkts[i],
254 status = rte_hash_lookup_bulk(t->h_table,
255 (const void **) keys,
259 for (i = 0; i < n_pkts_in; i++) {
260 if (likely(positions[i] >= 0)) {
261 uint64_t pkt_mask = 1LLU << i;
263 entries[i] = &t->memory[positions[i]
265 pkts_mask_out |= pkt_mask;
270 for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX
271 - __builtin_clzll(pkts_mask)); i++) {
272 uint64_t pkt_mask = 1LLU << i;
274 if (pkt_mask & pkts_mask) {
275 struct rte_mbuf *pkt = pkts[i];
276 uint8_t *key = RTE_MBUF_METADATA_UINT8_PTR(pkt,
280 pos = rte_hash_lookup(t->h_table, key);
281 if (likely(pos >= 0)) {
282 entries[i] = &t->memory[pos
284 pkts_mask_out |= pkt_mask;
289 *lookup_hit_mask = pkts_mask_out;
290 RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(t,
291 n_pkts_in - __builtin_popcountll(pkts_mask_out));
298 rte_table_hash_cuckoo_stats_read(void *table, struct rte_table_stats *stats,
301 struct rte_table_hash *t = table;
304 memcpy(stats, &t->stats, sizeof(t->stats));
307 memset(&t->stats, 0, sizeof(t->stats));
312 struct rte_table_ops rte_table_hash_cuckoo_ops = {
313 .f_create = rte_table_hash_cuckoo_create,
314 .f_free = rte_table_hash_cuckoo_free,
315 .f_add = rte_table_hash_cuckoo_entry_add,
316 .f_delete = rte_table_hash_cuckoo_entry_delete,
318 .f_delete_bulk = NULL,
319 .f_lookup = rte_table_hash_cuckoo_lookup,
320 .f_stats = rte_table_hash_cuckoo_stats_read,