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_memzone.h>
40 #include <rte_malloc.h>
41 #include <rte_errno.h>
43 #include "rte_member.h"
44 #include "rte_member_ht.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);
110 struct rte_member_setsum *
111 rte_member_create(const struct rte_member_parameters *params)
113 struct rte_tailq_entry *te;
114 struct rte_member_list *member_list;
115 struct rte_member_setsum *setsum;
118 if (params == NULL) {
123 if (params->key_len == 0 ||
124 params->prim_hash_seed == params->sec_hash_seed) {
126 RTE_MEMBER_LOG(ERR, "Create setsummary with "
127 "invalid parameters\n");
131 member_list = RTE_TAILQ_CAST(rte_member_tailq.head, rte_member_list);
133 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
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)
145 goto error_unlock_exit;
147 te = rte_zmalloc("MEMBER_TAILQ_ENTRY", sizeof(*te), 0);
149 RTE_MEMBER_LOG(ERR, "tailq entry allocation failed\n");
150 goto error_unlock_exit;
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,
157 if (setsum == NULL) {
158 RTE_MEMBER_LOG(ERR, "Create setsummary failed\n");
159 goto error_unlock_exit;
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;
169 switch (setsum->type) {
170 case RTE_MEMBER_TYPE_HT:
171 ret = rte_member_create_ht(setsum, params);
174 goto error_unlock_exit;
177 goto error_unlock_exit;
179 RTE_MEMBER_LOG(DEBUG, "Creating a setsummary table with "
180 "mode %u\n", setsum->type);
182 te->data = (void *)setsum;
183 TAILQ_INSERT_TAIL(member_list, te, next);
184 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
188 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
189 rte_member_free(setsum);
194 rte_member_add(const struct rte_member_setsum *setsum, const void *key,
197 if (setsum == NULL || key == NULL)
200 switch (setsum->type) {
201 case RTE_MEMBER_TYPE_HT:
202 return rte_member_add_ht(setsum, key, set_id);
209 rte_member_lookup(const struct rte_member_setsum *setsum, const void *key,
210 member_set_t *set_id)
212 if (setsum == NULL || key == NULL || set_id == NULL)
215 switch (setsum->type) {
216 case RTE_MEMBER_TYPE_HT:
217 return rte_member_lookup_ht(setsum, key, set_id);
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)
228 if (setsum == NULL || keys == NULL || set_ids == NULL)
231 switch (setsum->type) {
232 case RTE_MEMBER_TYPE_HT:
233 return rte_member_lookup_bulk_ht(setsum, keys, num_keys,
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)
244 if (setsum == NULL || key == NULL || set_id == NULL)
247 switch (setsum->type) {
248 case RTE_MEMBER_TYPE_HT:
249 return rte_member_lookup_multi_ht(setsum, key, match_per_key,
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)
262 if (setsum == NULL || keys == NULL || set_ids == NULL ||
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);
276 rte_member_delete(const struct rte_member_setsum *setsum, const void *key,
279 if (setsum == NULL || key == NULL)
282 switch (setsum->type) {
283 case RTE_MEMBER_TYPE_HT:
284 return rte_member_delete_ht(setsum, key, set_id);
291 rte_member_reset(const struct rte_member_setsum *setsum)
295 switch (setsum->type) {
296 case RTE_MEMBER_TYPE_HT:
297 rte_member_reset_ht(setsum);
304 RTE_INIT(librte_member_init_log);
307 librte_member_init_log(void)
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);