member: implement main API
[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
45 int librte_member_logtype;
46
47 TAILQ_HEAD(rte_member_list, rte_tailq_entry);
48 static struct rte_tailq_elem rte_member_tailq = {
49         .name = "RTE_MEMBER",
50 };
51 EAL_REGISTER_TAILQ(rte_member_tailq)
52
53 struct rte_member_setsum *
54 rte_member_find_existing(const char *name)
55 {
56         struct rte_member_setsum *setsum = NULL;
57         struct rte_tailq_entry *te;
58         struct rte_member_list *member_list;
59
60         member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
61
62         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
63         TAILQ_FOREACH(te, member_list, next) {
64                 setsum = (struct rte_member_setsum *) te->data;
65                 if (strncmp(name, setsum->name, RTE_MEMBER_NAMESIZE) == 0)
66                         break;
67         }
68         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
69
70         if (te == NULL) {
71                 rte_errno = ENOENT;
72                 return NULL;
73         }
74         return setsum;
75 }
76
77 void
78 rte_member_free(struct rte_member_setsum *setsum)
79 {
80         struct rte_member_list *member_list;
81         struct rte_tailq_entry *te;
82
83         if (setsum == NULL)
84                 return;
85         member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
86         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
87         TAILQ_FOREACH(te, member_list, next) {
88                 if (te->data == (void *)setsum)
89                         break;
90         }
91         if (te == NULL) {
92                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
93                 return;
94         }
95         TAILQ_REMOVE(member_list, te, next);
96         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
97
98         switch (setsum->type) {
99         default:
100                 break;
101         }
102         rte_free(setsum);
103         rte_free(te);
104 }
105
106 struct rte_member_setsum *
107 rte_member_create(const struct rte_member_parameters *params)
108 {
109         struct rte_tailq_entry *te;
110         struct rte_member_list *member_list;
111         struct rte_member_setsum *setsum;
112         int ret;
113
114         if (params == NULL) {
115                 rte_errno = EINVAL;
116                 return NULL;
117         }
118
119         if (params->key_len == 0 ||
120                         params->prim_hash_seed == params->sec_hash_seed) {
121                 rte_errno = EINVAL;
122                 RTE_MEMBER_LOG(ERR, "Create setsummary with "
123                                         "invalid parameters\n");
124                 return NULL;
125         }
126
127         member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
128
129         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
130
131         TAILQ_FOREACH(te, member_list, next) {
132                 setsum = (struct rte_member_setsum *) te->data;
133                 if (strncmp(params->name, setsum->name,
134                                 RTE_MEMBER_NAMESIZE) == 0)
135                         break;
136         }
137         setsum = NULL;
138         if (te != NULL) {
139                 rte_errno = EEXIST;
140                 te = NULL;
141                 goto error_unlock_exit;
142         }
143         te = rte_zmalloc("MEMBER_TAILQ_ENTRY", sizeof(*te), 0);
144         if (te == NULL) {
145                 RTE_MEMBER_LOG(ERR, "tailq entry allocation failed\n");
146                 goto error_unlock_exit;
147         }
148
149         /* Create a new setsum structure */
150         setsum = (struct rte_member_setsum *) rte_zmalloc_socket(params->name,
151                         sizeof(struct rte_member_setsum), RTE_CACHE_LINE_SIZE,
152                         params->socket_id);
153         if (setsum == NULL) {
154                 RTE_MEMBER_LOG(ERR, "Create setsummary failed\n");
155                 goto error_unlock_exit;
156         }
157         snprintf(setsum->name, sizeof(setsum->name), "%s", params->name);
158         setsum->type = params->type;
159         setsum->socket_id = params->socket_id;
160         setsum->key_len = params->key_len;
161         setsum->num_set = params->num_set;
162         setsum->prim_hash_seed = params->prim_hash_seed;
163         setsum->sec_hash_seed = params->sec_hash_seed;
164
165         switch (setsum->type) {
166         default:
167                 goto error_unlock_exit;
168         }
169         if (ret < 0)
170                 goto error_unlock_exit;
171
172         RTE_MEMBER_LOG(DEBUG, "Creating a setsummary table with "
173                         "mode %u\n", setsum->type);
174
175         te->data = (void *)setsum;
176         TAILQ_INSERT_TAIL(member_list, te, next);
177         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
178         return setsum;
179
180 error_unlock_exit:
181         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
182         rte_member_free(setsum);
183         return NULL;
184 }
185
186 int
187 rte_member_add(const struct rte_member_setsum *setsum, const void *key,
188                         member_set_t set_id)
189 {
190         if (setsum == NULL || key == NULL)
191                 return -EINVAL;
192
193         (void) set_id;
194         switch (setsum->type) {
195         default:
196                 return -EINVAL;
197         }
198 }
199
200 int
201 rte_member_lookup(const struct rte_member_setsum *setsum, const void *key,
202                         member_set_t *set_id)
203 {
204         if (setsum == NULL || key == NULL || set_id == NULL)
205                 return -EINVAL;
206
207         switch (setsum->type) {
208         default:
209                 return -EINVAL;
210         }
211 }
212
213 int
214 rte_member_lookup_bulk(const struct rte_member_setsum *setsum,
215                                 const void **keys, uint32_t num_keys,
216                                 member_set_t *set_ids)
217 {
218         if (setsum == NULL || keys == NULL || set_ids == NULL)
219                 return -EINVAL;
220
221         (void) num_keys;
222         switch (setsum->type) {
223         default:
224                 return -EINVAL;
225         }
226 }
227
228 int
229 rte_member_lookup_multi(const struct rte_member_setsum *setsum, const void *key,
230                                 uint32_t match_per_key, member_set_t *set_id)
231 {
232         if (setsum == NULL || key == NULL || set_id == NULL)
233                 return -EINVAL;
234
235         (void) match_per_key;
236         switch (setsum->type) {
237         default:
238                 return -EINVAL;
239         }
240 }
241
242 int
243 rte_member_lookup_multi_bulk(const struct rte_member_setsum *setsum,
244                         const void **keys, uint32_t num_keys,
245                         uint32_t max_match_per_key, uint32_t *match_count,
246                         member_set_t *set_ids)
247 {
248         if (setsum == NULL || keys == NULL || set_ids == NULL ||
249                         match_count == NULL)
250                 return -EINVAL;
251
252         (void) num_keys;
253         (void) max_match_per_key;
254         switch (setsum->type) {
255         default:
256                 return -EINVAL;
257         }
258 }
259
260 int
261 rte_member_delete(const struct rte_member_setsum *setsum, const void *key,
262                         member_set_t set_id)
263 {
264         if (setsum == NULL || key == NULL)
265                 return -EINVAL;
266
267         (void) set_id;
268         switch (setsum->type) {
269         default:
270                 return -EINVAL;
271         }
272 }
273
274 void
275 rte_member_reset(const struct rte_member_setsum *setsum)
276 {
277         if (setsum == NULL)
278                 return;
279         switch (setsum->type) {
280         default:
281                 return;
282         }
283 }
284
285 RTE_INIT(librte_member_init_log);
286
287 static void
288 librte_member_init_log(void)
289 {
290         librte_member_logtype = rte_log_register("librte.member");
291         if (librte_member_logtype >= 0)
292                 rte_log_set_level(librte_member_logtype, RTE_LOG_DEBUG);
293 }