tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_hash / rte_fbk_hash.c
1 /**
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 <stdint.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include <sys/queue.h>
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_eal.h>
44 #include <rte_eal_memconfig.h>
45 #include <rte_malloc.h>
46 #include <rte_common.h>
47 #include <rte_per_lcore.h>
48 #include <rte_errno.h>
49 #include <rte_string_fns.h>
50 #include <rte_cpuflags.h>
51 #include <rte_log.h>
52 #include <rte_spinlock.h>
53
54 #include "rte_fbk_hash.h"
55
56 TAILQ_HEAD(rte_fbk_hash_list, rte_tailq_entry);
57
58 /**
59  * Performs a lookup for an existing hash table, and returns a pointer to
60  * the table if found.
61  *
62  * @param name
63  *   Name of the hash table to find
64  *
65  * @return
66  *   pointer to hash table structure or NULL on error.
67  */
68 struct rte_fbk_hash_table *
69 rte_fbk_hash_find_existing(const char *name)
70 {
71         struct rte_fbk_hash_table *h = NULL;
72         struct rte_tailq_entry *te;
73         struct rte_fbk_hash_list *fbk_hash_list;
74
75         /* check that we have an initialised tail queue */
76         if ((fbk_hash_list =
77                         RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH,
78                                         rte_fbk_hash_list)) == NULL) {
79                 rte_errno = E_RTE_NO_TAILQ;
80                 return NULL;
81         }
82
83         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
84         TAILQ_FOREACH(te, fbk_hash_list, next) {
85                 h = (struct rte_fbk_hash_table *) te->data;
86                 if (strncmp(name, h->name, RTE_FBK_HASH_NAMESIZE) == 0)
87                         break;
88         }
89         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
90         if (te == NULL) {
91                 rte_errno = ENOENT;
92                 return NULL;
93         }
94         return h;
95 }
96
97 /**
98  * Create a new hash table for use with four byte keys.
99  *
100  * @param params
101  *   Parameters used in creation of hash table.
102  *
103  * @return
104  *   Pointer to hash table structure that is used in future hash table
105  *   operations, or NULL on error.
106  */
107 struct rte_fbk_hash_table *
108 rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
109 {
110         struct rte_fbk_hash_table *ht = NULL;
111         struct rte_tailq_entry *te;
112         char hash_name[RTE_FBK_HASH_NAMESIZE];
113         const uint32_t mem_size =
114                         sizeof(*ht) + (sizeof(ht->t[0]) * params->entries);
115         uint32_t i;
116         struct rte_fbk_hash_list *fbk_hash_list;
117
118         /* check that we have an initialised tail queue */
119         if ((fbk_hash_list =
120                         RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH,
121                                         rte_fbk_hash_list)) == NULL) {
122                 rte_errno = E_RTE_NO_TAILQ;
123                 return NULL;
124         }
125
126         /* Error checking of parameters. */
127         if ((!rte_is_power_of_2(params->entries)) ||
128                         (!rte_is_power_of_2(params->entries_per_bucket)) ||
129                         (params->entries == 0) ||
130                         (params->entries_per_bucket == 0) ||
131                         (params->entries_per_bucket > params->entries) ||
132                         (params->entries > RTE_FBK_HASH_ENTRIES_MAX) ||
133                         (params->entries_per_bucket > RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX)){
134                 rte_errno = EINVAL;
135                 return NULL;
136         }
137
138         snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
139
140         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
141
142         /* guarantee there's no existing */
143         TAILQ_FOREACH(te, fbk_hash_list, next) {
144                 ht = (struct rte_fbk_hash_table *) te->data;
145                 if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
146                         break;
147         }
148         if (te != NULL)
149                 goto exit;
150
151         te = rte_zmalloc("FBK_HASH_TAILQ_ENTRY", sizeof(*te), 0);
152         if (te == NULL) {
153                 RTE_LOG(ERR, HASH, "Failed to allocate tailq entry\n");
154                 goto exit;
155         }
156
157         /* Allocate memory for table. */
158         ht = (struct rte_fbk_hash_table *)rte_zmalloc_socket(hash_name, mem_size,
159                         0, params->socket_id);
160         if (ht == NULL) {
161                 RTE_LOG(ERR, HASH, "Failed to allocate fbk hash table\n");
162                 rte_free(te);
163                 goto exit;
164         }
165
166         /* Set up hash table context. */
167         snprintf(ht->name, sizeof(ht->name), "%s", params->name);
168         ht->entries = params->entries;
169         ht->entries_per_bucket = params->entries_per_bucket;
170         ht->used_entries = 0;
171         ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
172         for (ht->bucket_shift = 0, i = 1;
173             (params->entries_per_bucket & i) == 0;
174             ht->bucket_shift++, i <<= 1)
175                 ; /* empty loop body */
176
177         if (params->hash_func != NULL) {
178                 ht->hash_func = params->hash_func;
179                 ht->init_val = params->init_val;
180         }
181         else {
182                 ht->hash_func = RTE_FBK_HASH_FUNC_DEFAULT;
183                 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
184         }
185
186         te->data = (void *) ht;
187
188         TAILQ_INSERT_TAIL(fbk_hash_list, te, next);
189
190 exit:
191         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
192
193         return ht;
194 }
195
196 /**
197  * Free all memory used by a hash table.
198  *
199  * @param ht
200  *   Hash table to deallocate.
201  */
202 void
203 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
204 {
205         struct rte_tailq_entry *te;
206         struct rte_fbk_hash_list *fbk_hash_list;
207
208         if (ht == NULL)
209                 return;
210
211         /* check that we have an initialised tail queue */
212         if ((fbk_hash_list =
213                         RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH,
214                                         rte_fbk_hash_list)) == NULL) {
215                 rte_errno = E_RTE_NO_TAILQ;
216                 return;
217         }
218
219         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
220
221         /* find out tailq entry */
222         TAILQ_FOREACH(te, fbk_hash_list, next) {
223                 if (te->data == (void *) ht)
224                         break;
225         }
226
227         if (te == NULL) {
228                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
229                 return;
230         }
231
232         TAILQ_REMOVE(fbk_hash_list, te, next);
233
234         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
235
236         rte_free(ht);
237         rte_free(te);
238 }
239