4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <rte_eal_memconfig.h>
38 #include <rte_memory.h>
39 #include <rte_malloc.h>
40 #include <rte_errno.h>
42 #include "rte_member.h"
43 #include "rte_member_ht.h"
44 #include "rte_member_vbf.h"
46 int librte_member_logtype;
48 TAILQ_HEAD(rte_member_list, rte_tailq_entry);
49 static struct rte_tailq_elem rte_member_tailq = {
52 EAL_REGISTER_TAILQ(rte_member_tailq)
54 struct rte_member_setsum *
55 rte_member_find_existing(const char *name)
57 struct rte_member_setsum *setsum = NULL;
58 struct rte_tailq_entry *te;
59 struct rte_member_list *member_list;
61 member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
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)
69 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
79 rte_member_free(struct rte_member_setsum *setsum)
81 struct rte_member_list *member_list;
82 struct rte_tailq_entry *te;
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)
93 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
96 TAILQ_REMOVE(member_list, te, next);
97 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
99 switch (setsum->type) {
100 case RTE_MEMBER_TYPE_HT:
101 rte_member_free_ht(setsum);
103 case RTE_MEMBER_TYPE_VBF:
104 rte_member_free_vbf(setsum);
113 struct rte_member_setsum *
114 rte_member_create(const struct rte_member_parameters *params)
116 struct rte_tailq_entry *te;
117 struct rte_member_list *member_list;
118 struct rte_member_setsum *setsum;
121 if (params == NULL) {
126 if (params->key_len == 0 ||
127 params->prim_hash_seed == params->sec_hash_seed) {
129 RTE_MEMBER_LOG(ERR, "Create setsummary with "
130 "invalid parameters\n");
134 member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
136 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
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)
148 goto error_unlock_exit;
150 te = rte_zmalloc("MEMBER_TAILQ_ENTRY", sizeof(*te), 0);
152 RTE_MEMBER_LOG(ERR, "tailq entry allocation failed\n");
153 goto error_unlock_exit;
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,
160 if (setsum == NULL) {
161 RTE_MEMBER_LOG(ERR, "Create setsummary failed\n");
162 goto error_unlock_exit;
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;
172 switch (setsum->type) {
173 case RTE_MEMBER_TYPE_HT:
174 ret = rte_member_create_ht(setsum, params);
176 case RTE_MEMBER_TYPE_VBF:
177 ret = rte_member_create_vbf(setsum, params);
180 goto error_unlock_exit;
183 goto error_unlock_exit;
185 RTE_MEMBER_LOG(DEBUG, "Creating a setsummary table with "
186 "mode %u\n", setsum->type);
188 te->data = (void *)setsum;
189 TAILQ_INSERT_TAIL(member_list, te, next);
190 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
194 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
195 rte_member_free(setsum);
200 rte_member_add(const struct rte_member_setsum *setsum, const void *key,
203 if (setsum == NULL || key == NULL)
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);
217 rte_member_lookup(const struct rte_member_setsum *setsum, const void *key,
218 member_set_t *set_id)
220 if (setsum == NULL || key == NULL || set_id == NULL)
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);
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)
238 if (setsum == NULL || keys == NULL || set_ids == NULL)
241 switch (setsum->type) {
242 case RTE_MEMBER_TYPE_HT:
243 return rte_member_lookup_bulk_ht(setsum, keys, num_keys,
245 case RTE_MEMBER_TYPE_VBF:
246 return rte_member_lookup_bulk_vbf(setsum, keys, num_keys,
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)
257 if (setsum == NULL || key == NULL || set_id == NULL)
260 switch (setsum->type) {
261 case RTE_MEMBER_TYPE_HT:
262 return rte_member_lookup_multi_ht(setsum, key, match_per_key,
264 case RTE_MEMBER_TYPE_VBF:
265 return rte_member_lookup_multi_vbf(setsum, key, match_per_key,
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)
278 if (setsum == NULL || keys == NULL || set_ids == NULL ||
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);
295 rte_member_delete(const struct rte_member_setsum *setsum, const void *key,
298 if (setsum == NULL || key == NULL)
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:
312 rte_member_reset(const struct rte_member_setsum *setsum)
316 switch (setsum->type) {
317 case RTE_MEMBER_TYPE_HT:
318 rte_member_reset_ht(setsum);
320 case RTE_MEMBER_TYPE_VBF:
321 rte_member_reset_vbf(setsum);
328 RTE_INIT(librte_member_init_log);
331 librte_member_init_log(void)
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);