New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_table / rte_table_hash_cuckoo.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 #include <string.h>
34 #include <stdio.h>
35
36 #include <rte_common.h>
37 #include <rte_mbuf.h>
38 #include <rte_memory.h>
39 #include <rte_malloc.h>
40 #include <rte_log.h>
41
42 #include <rte_hash.h>
43 #include "rte_table_hash.h"
44
45 #ifdef RTE_TABLE_STATS_COLLECT
46
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)
51
52 #else
53
54 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(table, val)
55 #define RTE_TABLE_HASH_CUCKOO_STATS_PKTS_LOOKUP_MISS(table, val)
56
57 #endif
58
59
60 struct rte_table_hash {
61         struct rte_table_stats stats;
62
63         /* Input parameters */
64         uint32_t key_size;
65         uint32_t entry_size;
66         uint32_t n_keys;
67         rte_table_hash_op_hash f_hash;
68         uint32_t seed;
69         uint32_t key_offset;
70
71         /* cuckoo hash table object */
72         struct rte_hash *h_table;
73
74         /* Lookup table */
75         uint8_t memory[0] __rte_cache_aligned;
76 };
77
78 static int
79 check_params_create_hash_cuckoo(struct rte_table_hash_params *params)
80 {
81         if (params == NULL) {
82                 RTE_LOG(ERR, TABLE, "NULL Input Parameters.\n");
83                 return -EINVAL;
84         }
85
86         if (params->name == NULL) {
87                 RTE_LOG(ERR, TABLE, "Table name is NULL.\n");
88                 return -EINVAL;
89         }
90
91         if (params->key_size == 0) {
92                 RTE_LOG(ERR, TABLE, "Invalid key_size.\n");
93                 return -EINVAL;
94         }
95
96         if (params->n_keys == 0) {
97                 RTE_LOG(ERR, TABLE, "Invalid n_keys.\n");
98                 return -EINVAL;
99         }
100
101         if (params->f_hash == NULL) {
102                 RTE_LOG(ERR, TABLE, "f_hash is NULL.\n");
103                 return -EINVAL;
104         }
105
106         return 0;
107 }
108
109 static void *
110 rte_table_hash_cuckoo_create(void *params,
111                         int socket_id,
112                         uint32_t entry_size)
113 {
114         struct rte_table_hash_params *p = params;
115         struct rte_hash *h_table;
116         struct rte_table_hash *t;
117         uint32_t total_size;
118
119         /* Check input parameters */
120         if (check_params_create_hash_cuckoo(params))
121                 return NULL;
122
123         /* Memory allocation */
124         total_size = sizeof(struct rte_table_hash) +
125                 RTE_CACHE_LINE_ROUNDUP(p->n_keys * entry_size);
126
127         t = rte_zmalloc_socket(p->name, total_size, RTE_CACHE_LINE_SIZE, socket_id);
128         if (t == NULL) {
129                 RTE_LOG(ERR, TABLE,
130                         "%s: Cannot allocate %u bytes for cuckoo hash table %s\n",
131                         __func__, total_size, p->name);
132                 return NULL;
133         }
134
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,
142                 .name = p->name
143         };
144
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) {
149                         RTE_LOG(ERR, TABLE,
150                                 "%s: failed to create cuckoo hash table %s\n",
151                                 __func__, p->name);
152                         rte_free(t);
153                         return NULL;
154                 }
155         }
156
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;
162         t->seed = p->seed;
163         t->key_offset = p->key_offset;
164         t->h_table = h_table;
165
166         RTE_LOG(INFO, TABLE,
167                 "%s: Cuckoo hash table %s memory footprint is %u bytes\n",
168                 __func__, p->name, total_size);
169         return t;
170 }
171
172 static int
173 rte_table_hash_cuckoo_free(void *table) {
174         struct rte_table_hash *t = table;
175
176         if (table == NULL)
177                 return -EINVAL;
178
179         rte_hash_free(t->h_table);
180         rte_free(t);
181
182         return 0;
183 }
184
185 static int
186 rte_table_hash_cuckoo_entry_add(void *table, void *key, void *entry,
187         int *key_found, void **entry_ptr)
188 {
189         struct rte_table_hash *t = table;
190         int pos = 0;
191
192         /* Check input parameters */
193         if ((table == NULL) ||
194                 (key == NULL) ||
195                 (entry == NULL) ||
196                 (key_found == NULL) ||
197                 (entry_ptr == NULL))
198                 return -EINVAL;
199
200         /*  Find Existing entries */
201         pos = rte_hash_lookup(t->h_table, key);
202         if (pos >= 0) {
203                 uint8_t *existing_entry;
204
205                 *key_found = 1;
206                 existing_entry = &t->memory[pos * t->entry_size];
207                 memcpy(existing_entry, entry, t->entry_size);
208                 *entry_ptr = existing_entry;
209
210                 return 0;
211         }
212
213         if (pos == -ENOENT) {
214                 /* Entry not found. Adding new entry */
215                 uint8_t *new_entry;
216
217                 pos = rte_hash_add_key(t->h_table, key);
218                 if (pos < 0)
219                         return pos;
220
221                 new_entry = &t->memory[pos * t->entry_size];
222                 memcpy(new_entry, entry, t->entry_size);
223
224                 *key_found = 0;
225                 *entry_ptr = new_entry;
226                 return 0;
227         }
228
229         return pos;
230 }
231
232 static int
233 rte_table_hash_cuckoo_entry_delete(void *table, void *key,
234         int *key_found, void *entry)
235 {
236         struct rte_table_hash *t = table;
237         int pos = 0;
238
239         /* Check input parameters */
240         if ((table == NULL) ||
241                 (key == NULL) ||
242                 (key_found == NULL))
243                 return -EINVAL;
244
245         pos = rte_hash_del_key(t->h_table, key);
246         if (pos >= 0) {
247                 *key_found = 1;
248                 uint8_t *entry_ptr = &t->memory[pos * t->entry_size];
249
250                 if (entry)
251                         memcpy(entry, entry_ptr, t->entry_size);
252
253                 memset(&t->memory[pos * t->entry_size], 0, t->entry_size);
254                 return 0;
255         }
256
257         *key_found = 0;
258         return pos;
259 }
260
261 static int
262 rte_table_hash_cuckoo_lookup(void *table,
263         struct rte_mbuf **pkts,
264         uint64_t pkts_mask,
265         uint64_t *lookup_hit_mask,
266         void **entries)
267 {
268         struct rte_table_hash *t = table;
269         uint64_t pkts_mask_out = 0;
270         uint32_t i;
271
272         __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
273
274         RTE_TABLE_HASH_CUCKOO_STATS_PKTS_IN_ADD(t, n_pkts_in);
275
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;
279
280                 /* Keys for bulk lookup */
281                 for (i = 0; i < n_pkts_in; i++)
282                         keys[i] = RTE_MBUF_METADATA_UINT8_PTR(pkts[i],
283                                 t->key_offset);
284
285                 /* Bulk Lookup */
286                 status = rte_hash_lookup_bulk(t->h_table,
287                                 (const void **) keys,
288                                 n_pkts_in,
289                                 positions);
290                 if (status == 0) {
291                         for (i = 0; i < n_pkts_in; i++) {
292                                 if (likely(positions[i] >= 0)) {
293                                         uint64_t pkt_mask = 1LLU << i;
294
295                                         entries[i] = &t->memory[positions[i]
296                                                 * t->entry_size];
297                                         pkts_mask_out |= pkt_mask;
298                                 }
299                         }
300                 }
301         } else
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;
305
306                         if (pkt_mask & pkts_mask) {
307                                 struct rte_mbuf *pkt = pkts[i];
308                                 uint8_t *key = RTE_MBUF_METADATA_UINT8_PTR(pkt,
309                                                 t->key_offset);
310                                 int pos;
311
312                                 pos = rte_hash_lookup(t->h_table, key);
313                                 if (likely(pos >= 0)) {
314                                         entries[i] = &t->memory[pos
315                                                 * t->entry_size];
316                                         pkts_mask_out |= pkt_mask;
317                                 }
318                         }
319                 }
320
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));
324
325         return 0;
326
327 }
328
329 static int
330 rte_table_hash_cuckoo_stats_read(void *table, struct rte_table_stats *stats,
331         int clear)
332 {
333         struct rte_table_hash *t = table;
334
335         if (stats != NULL)
336                 memcpy(stats, &t->stats, sizeof(t->stats));
337
338         if (clear)
339                 memset(&t->stats, 0, sizeof(t->stats));
340
341         return 0;
342 }
343
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,
349         .f_add_bulk = NULL,
350         .f_delete_bulk = NULL,
351         .f_lookup = rte_table_hash_cuckoo_lookup,
352         .f_stats = rte_table_hash_cuckoo_stats_read,
353 };