1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2010-2017 Intel Corporation
*/
#include <string.h>
#include <stdio.h>
#include <rte_common.h>
#include <rte_mbuf.h>
#include <rte_memory.h>
#include <rte_malloc.h>
#include <rte_log.h>
#include <rte_hash.h>
#include "rte_table_hash.h"
#ifdef RTE_TABLE_STATS_COLLECT
#define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(table, val) \
(table->stats.n_pkts_in += val)
#define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(table, val) \
(table->stats.n_pkts_lookup_miss += val)
#else
#define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(table, val)
#define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(table, val)
#endif
struct rte_table_hash {
struct rte_table_stats stats;
/* Input parameters */
uint32_t key_size;
uint32_t entry_size;
uint32_t n_keys;
rte_table_hash_op_hash f_hash;
uint32_t seed;
uint32_t key_offset;
/* cuckoo hash table object */
struct rte_hash *h_table;
/* Lookup table */
uint8_t memory[0] __rte_cache_aligned;
};
static int
check_params_create_hash_cuckoo(struct rte_table_hash_params *params)
{
if (params == NULL) {
RTE_LOG(ERR, TABLE, "NULL Input Parameters.\n");
return -EINVAL;
}
if (params->name == NULL) {
RTE_LOG(ERR, TABLE, "Table name is NULL.\n");
return -EINVAL;
}
if (params->key_size == 0) {
RTE_LOG(ERR, TABLE, "Invalid key_size.\n");
return -EINVAL;
}
if (params->n_keys == 0) {
RTE_LOG(ERR, TABLE, "Invalid n_keys.\n");
return -EINVAL;
}
if (params->f_hash == NULL) {
RTE_LOG(ERR, TABLE, "f_hash is NULL.\n");
return -EINVAL;
}
return 0;
}
static void *
rte_table_hash_cuckoo_create(void *params,
int socket_id,
uint32_t entry_size)
{
struct rte_table_hash_params *p = params;
struct rte_hash *h_table;
struct rte_table_hash *t;
uint32_t total_size;
/* Check input parameters */
if (check_params_create_hash_cuckoo(params))
return NULL;
/* Memory allocation */
total_size = sizeof(struct rte_table_hash) +
RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
t = rte_zmalloc_socket(p->name, total_size, RTE_CACHE_LINE_SIZE, socket_id);
if (t == NULL) {
RTE_LOG(ERR, TABLE,
"%s: Cannot allocate %u bytes for cuckoo hash table %s\n",
__func__, total_size, p->name);
return NULL;
}
/* Create cuckoo hash table */
struct rte_hash_parameters hash_cuckoo_params = {
.entries = p->n_keys,
.key_len = p->key_size,
.hash_func = (rte_hash_function)(p->f_hash),
.hash_func_init_val = p->seed,
.socket_id = socket_id,
.name = p->name
};
h_table = rte_hash_find_existing(p->name);
if (h_table == NULL) {
h_table = rte_hash_create(&hash_cuckoo_params);
if (h_table == NULL) {
RTE_LOG(ERR, TABLE,
"%s: failed to create cuckoo hash table %s\n",
__func__, p->name);
rte_free(t);
return NULL;
}
}
/* initialize the cuckoo hash parameters */
t->key_size = p->key_size;
t->entry_size = entry_size;
t->n_keys = p->n_keys;
t->f_hash = p->f_hash;
t->seed = p->seed;
t->key_offset = p->key_offset;
t->h_table = h_table;
RTE_LOG(INFO, TABLE,
"%s: Cuckoo hash table %s memory footprint is %u bytes\n",
__func__, p->name, total_size);
return t;
}
static int
rte_table_hash_cuckoo_free(void *table) {
struct rte_table_hash *t = table;
if (table == NULL)
return -EINVAL;
rte_hash_free(t->h_table);
rte_free(t);
return 0;
}
static int
rte_table_hash_cuckoo_entry_add(void *table, void *key, void *entry,
int *key_found, void **entry_ptr)
{
struct rte_table_hash *t = table;
int pos = 0;
/* Check input parameters */
if ((table == NULL) ||
(key == NULL) ||
(entry == NULL) ||
(key_found == NULL) ||
(entry_ptr == NULL))
return -EINVAL;
/* Find Existing entries */
pos = rte_hash_lookup(t->h_table, key);
if (pos >= 0) {
uint8_t *existing_entry;
*key_found = 1;
existing_entry = &t->memory[pos * t->entry_size];
memcpy(existing_entry, entry, t->entry_size);
*entry_ptr = existing_entry;
return 0;
}
if (pos == -ENOENT) {
/* Entry not found. Adding new entry */
uint8_t *new_entry;
pos = rte_hash_add_key(t->h_table, key);
if (pos < 0)
return pos;
new_entry = &t->memory[pos * t->entry_size];
memcpy(new_entry, entry, t->entry_size);
*key_found = 0;
*entry_ptr = new_entry;
return 0;
}
return pos;
}
static int
rte_table_hash_cuckoo_entry_delete(void *table, void *key,
int *key_found, void *entry)
{
struct rte_table_hash *t = table;
int pos = 0;
/* Check input parameters */
if ((table == NULL) ||
(key == NULL) ||
(key_found == NULL))
return -EINVAL;
pos = rte_hash_del_key(t->h_table, key);
if (pos >= 0) {
*key_found = 1;
uint8_t *entry_ptr = &t->memory[pos * t->entry_size];
if (entry)
memcpy(entry, entry_ptr, t->entry_size);
memset(&t->memory[pos * t->entry_size], 0, t->entry_size);
return 0;
}
*key_found = 0;
return pos;
}
static int
rte_table_hash_cuckoo_lookup(void *table,
struct rte_mbuf **pkts,
uint64_t pkts_mask,
uint64_t *lookup_hit_mask,
void **entries)
{
struct rte_table_hash *t = table;
uint64_t pkts_mask_out = 0;
uint32_t i;
__rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(t, n_pkts_in);
if ((pkts_mask & (pkts_mask + 1)) == 0) {
const uint8_t *keys[RTE_PORT_IN_BURST_SIZE_MAX];
int32_t positions[RTE_PORT_IN_BURST_SIZE_MAX], status;
/* Keys for bulk lookup */
for (i = 0; i < n_pkts_in; i++)
keys[i] = RTE_MBUF_METADATA_UINT8_PTR(pkts[i],
t->key_offset);
/* Bulk Lookup */
status = rte_hash_lookup_bulk(t->h_table,
(const void **) keys,
n_pkts_in,
positions);
if (status == 0) {
for (i = 0; i < n_pkts_in; i++) {
if (likely(positions[i] >= 0)) {
uint64_t pkt_mask = 1LLU << i;
entries[i] = &t->memory[positions[i]
* t->entry_size];
pkts_mask_out |= pkt_mask;
}
}
}
} else
for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX
- __builtin_clzll(pkts_mask)); i++) {
uint64_t pkt_mask = 1LLU << i;
if (pkt_mask & pkts_mask) {
struct rte_mbuf *pkt = pkts[i];
uint8_t *key = RTE_MBUF_METADATA_UINT8_PTR(pkt,
t->key_offset);
int pos;
pos = rte_hash_lookup(t->h_table, key);
if (likely(pos >= 0)) {
entries[i] = &t->memory[pos
* t->entry_size];
pkts_mask_out |= pkt_mask;
}
}
}
*lookup_hit_mask = pkts_mask_out;
RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(t,
n_pkts_in - __builtin_popcountll(pkts_mask_out));
return 0;
}
static int
rte_table_hash_cuckoo_stats_read(void *table, struct rte_table_stats *stats,
int clear)
{
struct rte_table_hash *t = table;
if (stats != NULL)
memcpy(stats, &t->stats, sizeof(t->stats));
if (clear)
memset(&t->stats, 0, sizeof(t->stats));
return 0;
}
struct rte_table_ops rte_table_hash_cuckoo_ops = {
.f_create = rte_table_hash_cuckoo_create,
.f_free = rte_table_hash_cuckoo_free,
.f_add = rte_table_hash_cuckoo_entry_add,
.f_delete = rte_table_hash_cuckoo_entry_delete,
.f_add_bulk = NULL,
.f_delete_bulk = NULL,
.f_lookup = rte_table_hash_cuckoo_lookup,
.f_stats = rte_table_hash_cuckoo_stats_read,
};
|