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