version: 17.11-rc3
[dpdk.git] / lib / librte_member / rte_member.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 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
34 #include <string.h>
35
36 #include <rte_eal.h>
37 #include <rte_eal_memconfig.h>
38 #include <rte_memory.h>
39 #include <rte_malloc.h>
40 #include <rte_errno.h>
41
42 #include "rte_member.h"
43 #include "rte_member_ht.h"
44 #include "rte_member_vbf.h"
45
46 int librte_member_logtype;
47
48 TAILQ_HEAD(rte_member_list, rte_tailq_entry);
49 static struct rte_tailq_elem rte_member_tailq = {
50         .name = "RTE_MEMBER",
51 };
52 EAL_REGISTER_TAILQ(rte_member_tailq)
53
54 struct rte_member_setsum *
55 rte_member_find_existing(const char *name)
56 {
57         struct rte_member_setsum *setsum = NULL;
58         struct rte_tailq_entry *te;
59         struct rte_member_list *member_list;
60
61         member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
62
63         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
64         TAILQ_FOREACH(te, member_list, next) {
65                 setsum = (struct rte_member_setsum *) te->data;
66                 if (strncmp(name, setsum->name, RTE_MEMBER_NAMESIZE) == 0)
67                         break;
68         }
69         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
70
71         if (te == NULL) {
72                 rte_errno = ENOENT;
73                 return NULL;
74         }
75         return setsum;
76 }
77
78 void
79 rte_member_free(struct rte_member_setsum *setsum)
80 {
81         struct rte_member_list *member_list;
82         struct rte_tailq_entry *te;
83
84         if (setsum == NULL)
85                 return;
86         member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
87         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
88         TAILQ_FOREACH(te, member_list, next) {
89                 if (te->data == (void *)setsum)
90                         break;
91         }
92         if (te == NULL) {
93                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
94                 return;
95         }
96         TAILQ_REMOVE(member_list, te, next);
97         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
98
99         switch (setsum->type) {
100         case RTE_MEMBER_TYPE_HT:
101                 rte_member_free_ht(setsum);
102                 break;
103         case RTE_MEMBER_TYPE_VBF:
104                 rte_member_free_vbf(setsum);
105                 break;
106         default:
107                 break;
108         }
109         rte_free(setsum);
110         rte_free(te);
111 }
112
113 struct rte_member_setsum *
114 rte_member_create(const struct rte_member_parameters *params)
115 {
116         struct rte_tailq_entry *te;
117         struct rte_member_list *member_list;
118         struct rte_member_setsum *setsum;
119         int ret;
120
121         if (params == NULL) {
122                 rte_errno = EINVAL;
123                 return NULL;
124         }
125
126         if (params->key_len == 0 ||
127                         params->prim_hash_seed == params->sec_hash_seed) {
128                 rte_errno = EINVAL;
129                 RTE_MEMBER_LOG(ERR, "Create setsummary with "
130                                         "invalid parameters\n");
131                 return NULL;
132         }
133
134         member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
135
136         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
137
138         TAILQ_FOREACH(te, member_list, next) {
139                 setsum = (struct rte_member_setsum *) te->data;
140                 if (strncmp(params->name, setsum->name,
141                                 RTE_MEMBER_NAMESIZE) == 0)
142                         break;
143         }
144         setsum = NULL;
145         if (te != NULL) {
146                 rte_errno = EEXIST;
147                 te = NULL;
148                 goto error_unlock_exit;
149         }
150         te = rte_zmalloc("MEMBER_TAILQ_ENTRY", sizeof(*te), 0);
151         if (te == NULL) {
152                 RTE_MEMBER_LOG(ERR, "tailq entry allocation failed\n");
153                 goto error_unlock_exit;
154         }
155
156         /* Create a new setsum structure */
157         setsum = (struct rte_member_setsum *) rte_zmalloc_socket(params->name,
158                         sizeof(struct rte_member_setsum), RTE_CACHE_LINE_SIZE,
159                         params->socket_id);
160         if (setsum == NULL) {
161                 RTE_MEMBER_LOG(ERR, "Create setsummary failed\n");
162                 goto error_unlock_exit;
163         }
164         snprintf(setsum->name, sizeof(setsum->name), "%s", params->name);
165         setsum->type = params->type;
166         setsum->socket_id = params->socket_id;
167         setsum->key_len = params->key_len;
168         setsum->num_set = params->num_set;
169         setsum->prim_hash_seed = params->prim_hash_seed;
170         setsum->sec_hash_seed = params->sec_hash_seed;
171
172         switch (setsum->type) {
173         case RTE_MEMBER_TYPE_HT:
174                 ret = rte_member_create_ht(setsum, params);
175                 break;
176         case RTE_MEMBER_TYPE_VBF:
177                 ret = rte_member_create_vbf(setsum, params);
178                 break;
179         default:
180                 goto error_unlock_exit;
181         }
182         if (ret < 0)
183                 goto error_unlock_exit;
184
185         RTE_MEMBER_LOG(DEBUG, "Creating a setsummary table with "
186                         "mode %u\n", setsum->type);
187
188         te->data = (void *)setsum;
189         TAILQ_INSERT_TAIL(member_list, te, next);
190         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
191         return setsum;
192
193 error_unlock_exit:
194         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
195         rte_member_free(setsum);
196         return NULL;
197 }
198
199 int
200 rte_member_add(const struct rte_member_setsum *setsum, const void *key,
201                         member_set_t set_id)
202 {
203         if (setsum == NULL || key == NULL)
204                 return -EINVAL;
205
206         switch (setsum->type) {
207         case RTE_MEMBER_TYPE_HT:
208                 return rte_member_add_ht(setsum, key, set_id);
209         case RTE_MEMBER_TYPE_VBF:
210                 return rte_member_add_vbf(setsum, key, set_id);
211         default:
212                 return -EINVAL;
213         }
214 }
215
216 int
217 rte_member_lookup(const struct rte_member_setsum *setsum, const void *key,
218                         member_set_t *set_id)
219 {
220         if (setsum == NULL || key == NULL || set_id == NULL)
221                 return -EINVAL;
222
223         switch (setsum->type) {
224         case RTE_MEMBER_TYPE_HT:
225                 return rte_member_lookup_ht(setsum, key, set_id);
226         case RTE_MEMBER_TYPE_VBF:
227                 return rte_member_lookup_vbf(setsum, key, set_id);
228         default:
229                 return -EINVAL;
230         }
231 }
232
233 int
234 rte_member_lookup_bulk(const struct rte_member_setsum *setsum,
235                                 const void **keys, uint32_t num_keys,
236                                 member_set_t *set_ids)
237 {
238         if (setsum == NULL || keys == NULL || set_ids == NULL)
239                 return -EINVAL;
240
241         switch (setsum->type) {
242         case RTE_MEMBER_TYPE_HT:
243                 return rte_member_lookup_bulk_ht(setsum, keys, num_keys,
244                                 set_ids);
245         case RTE_MEMBER_TYPE_VBF:
246                 return rte_member_lookup_bulk_vbf(setsum, keys, num_keys,
247                                 set_ids);
248         default:
249                 return -EINVAL;
250         }
251 }
252
253 int
254 rte_member_lookup_multi(const struct rte_member_setsum *setsum, const void *key,
255                                 uint32_t match_per_key, member_set_t *set_id)
256 {
257         if (setsum == NULL || key == NULL || set_id == NULL)
258                 return -EINVAL;
259
260         switch (setsum->type) {
261         case RTE_MEMBER_TYPE_HT:
262                 return rte_member_lookup_multi_ht(setsum, key, match_per_key,
263                                 set_id);
264         case RTE_MEMBER_TYPE_VBF:
265                 return rte_member_lookup_multi_vbf(setsum, key, match_per_key,
266                                 set_id);
267         default:
268                 return -EINVAL;
269         }
270 }
271
272 int
273 rte_member_lookup_multi_bulk(const struct rte_member_setsum *setsum,
274                         const void **keys, uint32_t num_keys,
275                         uint32_t max_match_per_key, uint32_t *match_count,
276                         member_set_t *set_ids)
277 {
278         if (setsum == NULL || keys == NULL || set_ids == NULL ||
279                         match_count == NULL)
280                 return -EINVAL;
281
282         switch (setsum->type) {
283         case RTE_MEMBER_TYPE_HT:
284                 return rte_member_lookup_multi_bulk_ht(setsum, keys, num_keys,
285                                 max_match_per_key, match_count, set_ids);
286         case RTE_MEMBER_TYPE_VBF:
287                 return rte_member_lookup_multi_bulk_vbf(setsum, keys, num_keys,
288                                 max_match_per_key, match_count, set_ids);
289         default:
290                 return -EINVAL;
291         }
292 }
293
294 int
295 rte_member_delete(const struct rte_member_setsum *setsum, const void *key,
296                         member_set_t set_id)
297 {
298         if (setsum == NULL || key == NULL)
299                 return -EINVAL;
300
301         switch (setsum->type) {
302         case RTE_MEMBER_TYPE_HT:
303                 return rte_member_delete_ht(setsum, key, set_id);
304         /* current vBF implementation does not support delete function */
305         case RTE_MEMBER_TYPE_VBF:
306         default:
307                 return -EINVAL;
308         }
309 }
310
311 void
312 rte_member_reset(const struct rte_member_setsum *setsum)
313 {
314         if (setsum == NULL)
315                 return;
316         switch (setsum->type) {
317         case RTE_MEMBER_TYPE_HT:
318                 rte_member_reset_ht(setsum);
319                 return;
320         case RTE_MEMBER_TYPE_VBF:
321                 rte_member_reset_vbf(setsum);
322                 return;
323         default:
324                 return;
325         }
326 }
327
328 RTE_INIT(librte_member_init_log);
329
330 static void
331 librte_member_init_log(void)
332 {
333         librte_member_logtype = rte_log_register("librte.member");
334         if (librte_member_logtype >= 0)
335                 rte_log_set_level(librte_member_logtype, RTE_LOG_DEBUG);
336 }