member: implement HT mode
[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_memzone.h>
40 #include <rte_malloc.h>
41 #include <rte_errno.h>
42
43 #include "rte_member.h"
44 #include "rte_member_ht.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         default:
104                 break;
105         }
106         rte_free(setsum);
107         rte_free(te);
108 }
109
110 struct rte_member_setsum *
111 rte_member_create(const struct rte_member_parameters *params)
112 {
113         struct rte_tailq_entry *te;
114         struct rte_member_list *member_list;
115         struct rte_member_setsum *setsum;
116         int ret;
117
118         if (params == NULL) {
119                 rte_errno = EINVAL;
120                 return NULL;
121         }
122
123         if (params->key_len == 0 ||
124                         params->prim_hash_seed == params->sec_hash_seed) {
125                 rte_errno = EINVAL;
126                 RTE_MEMBER_LOG(ERR, "Create setsummary with "
127                                         "invalid parameters\n");
128                 return NULL;
129         }
130
131         member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
132
133         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
134
135         TAILQ_FOREACH(te, member_list, next) {
136                 setsum = (struct rte_member_setsum *) te->data;
137                 if (strncmp(params->name, setsum->name,
138                                 RTE_MEMBER_NAMESIZE) == 0)
139                         break;
140         }
141         setsum = NULL;
142         if (te != NULL) {
143                 rte_errno = EEXIST;
144                 te = NULL;
145                 goto error_unlock_exit;
146         }
147         te = rte_zmalloc("MEMBER_TAILQ_ENTRY", sizeof(*te), 0);
148         if (te == NULL) {
149                 RTE_MEMBER_LOG(ERR, "tailq entry allocation failed\n");
150                 goto error_unlock_exit;
151         }
152
153         /* Create a new setsum structure */
154         setsum = (struct rte_member_setsum *) rte_zmalloc_socket(params->name,
155                         sizeof(struct rte_member_setsum), RTE_CACHE_LINE_SIZE,
156                         params->socket_id);
157         if (setsum == NULL) {
158                 RTE_MEMBER_LOG(ERR, "Create setsummary failed\n");
159                 goto error_unlock_exit;
160         }
161         snprintf(setsum->name, sizeof(setsum->name), "%s", params->name);
162         setsum->type = params->type;
163         setsum->socket_id = params->socket_id;
164         setsum->key_len = params->key_len;
165         setsum->num_set = params->num_set;
166         setsum->prim_hash_seed = params->prim_hash_seed;
167         setsum->sec_hash_seed = params->sec_hash_seed;
168
169         switch (setsum->type) {
170         case RTE_MEMBER_TYPE_HT:
171                 ret = rte_member_create_ht(setsum, params);
172                 break;
173         default:
174                 goto error_unlock_exit;
175         }
176         if (ret < 0)
177                 goto error_unlock_exit;
178
179         RTE_MEMBER_LOG(DEBUG, "Creating a setsummary table with "
180                         "mode %u\n", setsum->type);
181
182         te->data = (void *)setsum;
183         TAILQ_INSERT_TAIL(member_list, te, next);
184         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
185         return setsum;
186
187 error_unlock_exit:
188         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
189         rte_member_free(setsum);
190         return NULL;
191 }
192
193 int
194 rte_member_add(const struct rte_member_setsum *setsum, const void *key,
195                         member_set_t set_id)
196 {
197         if (setsum == NULL || key == NULL)
198                 return -EINVAL;
199
200         switch (setsum->type) {
201         case RTE_MEMBER_TYPE_HT:
202                 return rte_member_add_ht(setsum, key, set_id);
203         default:
204                 return -EINVAL;
205         }
206 }
207
208 int
209 rte_member_lookup(const struct rte_member_setsum *setsum, const void *key,
210                         member_set_t *set_id)
211 {
212         if (setsum == NULL || key == NULL || set_id == NULL)
213                 return -EINVAL;
214
215         switch (setsum->type) {
216         case RTE_MEMBER_TYPE_HT:
217                 return rte_member_lookup_ht(setsum, key, set_id);
218         default:
219                 return -EINVAL;
220         }
221 }
222
223 int
224 rte_member_lookup_bulk(const struct rte_member_setsum *setsum,
225                                 const void **keys, uint32_t num_keys,
226                                 member_set_t *set_ids)
227 {
228         if (setsum == NULL || keys == NULL || set_ids == NULL)
229                 return -EINVAL;
230
231         switch (setsum->type) {
232         case RTE_MEMBER_TYPE_HT:
233                 return rte_member_lookup_bulk_ht(setsum, keys, num_keys,
234                                 set_ids);
235         default:
236                 return -EINVAL;
237         }
238 }
239
240 int
241 rte_member_lookup_multi(const struct rte_member_setsum *setsum, const void *key,
242                                 uint32_t match_per_key, member_set_t *set_id)
243 {
244         if (setsum == NULL || key == NULL || set_id == NULL)
245                 return -EINVAL;
246
247         switch (setsum->type) {
248         case RTE_MEMBER_TYPE_HT:
249                 return rte_member_lookup_multi_ht(setsum, key, match_per_key,
250                                 set_id);
251         default:
252                 return -EINVAL;
253         }
254 }
255
256 int
257 rte_member_lookup_multi_bulk(const struct rte_member_setsum *setsum,
258                         const void **keys, uint32_t num_keys,
259                         uint32_t max_match_per_key, uint32_t *match_count,
260                         member_set_t *set_ids)
261 {
262         if (setsum == NULL || keys == NULL || set_ids == NULL ||
263                         match_count == NULL)
264                 return -EINVAL;
265
266         switch (setsum->type) {
267         case RTE_MEMBER_TYPE_HT:
268                 return rte_member_lookup_multi_bulk_ht(setsum, keys, num_keys,
269                                 max_match_per_key, match_count, set_ids);
270         default:
271                 return -EINVAL;
272         }
273 }
274
275 int
276 rte_member_delete(const struct rte_member_setsum *setsum, const void *key,
277                         member_set_t set_id)
278 {
279         if (setsum == NULL || key == NULL)
280                 return -EINVAL;
281
282         switch (setsum->type) {
283         case RTE_MEMBER_TYPE_HT:
284                 return rte_member_delete_ht(setsum, key, set_id);
285         default:
286                 return -EINVAL;
287         }
288 }
289
290 void
291 rte_member_reset(const struct rte_member_setsum *setsum)
292 {
293         if (setsum == NULL)
294                 return;
295         switch (setsum->type) {
296         case RTE_MEMBER_TYPE_HT:
297                 rte_member_reset_ht(setsum);
298                 return;
299         default:
300                 return;
301         }
302 }
303
304 RTE_INIT(librte_member_init_log);
305
306 static void
307 librte_member_init_log(void)
308 {
309         librte_member_logtype = rte_log_register("librte.member");
310         if (librte_member_logtype >= 0)
311                 rte_log_set_level(librte_member_logtype, RTE_LOG_DEBUG);
312 }