55c9f358319c01adf544cce3d8b6b9280128fe49
[deb_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 static struct rte_tailq_elem rte_fbk_hash_tailq = {
59         .name = "RTE_FBK_HASH",
60 };
61 EAL_REGISTER_TAILQ(rte_fbk_hash_tailq)
62
63 /**
64  * Performs a lookup for an existing hash table, and returns a pointer to
65  * the table if found.
66  *
67  * @param name
68  *   Name of the hash table to find
69  *
70  * @return
71  *   pointer to hash table structure or NULL on error.
72  */
73 struct rte_fbk_hash_table *
74 rte_fbk_hash_find_existing(const char *name)
75 {
76         struct rte_fbk_hash_table *h = NULL;
77         struct rte_tailq_entry *te;
78         struct rte_fbk_hash_list *fbk_hash_list;
79
80         fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
81                                        rte_fbk_hash_list);
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         fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
119                                        rte_fbk_hash_list);
120
121         /* Error checking of parameters. */
122         if ((!rte_is_power_of_2(params->entries)) ||
123                         (!rte_is_power_of_2(params->entries_per_bucket)) ||
124                         (params->entries == 0) ||
125                         (params->entries_per_bucket == 0) ||
126                         (params->entries_per_bucket > params->entries) ||
127                         (params->entries > RTE_FBK_HASH_ENTRIES_MAX) ||
128                         (params->entries_per_bucket > RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX)){
129                 rte_errno = EINVAL;
130                 return NULL;
131         }
132
133         snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
134
135         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
136
137         /* guarantee there's no existing */
138         TAILQ_FOREACH(te, fbk_hash_list, next) {
139                 ht = (struct rte_fbk_hash_table *) te->data;
140                 if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
141                         break;
142         }
143         ht = NULL;
144         if (te != NULL) {
145                 rte_errno = EEXIST;
146                 goto exit;
147         }
148
149         te = rte_zmalloc("FBK_HASH_TAILQ_ENTRY", sizeof(*te), 0);
150         if (te == NULL) {
151                 RTE_LOG(ERR, HASH, "Failed to allocate tailq entry\n");
152                 goto exit;
153         }
154
155         /* Allocate memory for table. */
156         ht = (struct rte_fbk_hash_table *)rte_zmalloc_socket(hash_name, mem_size,
157                         0, params->socket_id);
158         if (ht == NULL) {
159                 RTE_LOG(ERR, HASH, "Failed to allocate fbk hash table\n");
160                 rte_free(te);
161                 goto exit;
162         }
163
164         /* Set up hash table context. */
165         snprintf(ht->name, sizeof(ht->name), "%s", params->name);
166         ht->entries = params->entries;
167         ht->entries_per_bucket = params->entries_per_bucket;
168         ht->used_entries = 0;
169         ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
170         for (ht->bucket_shift = 0, i = 1;
171             (params->entries_per_bucket & i) == 0;
172             ht->bucket_shift++, i <<= 1)
173                 ; /* empty loop body */
174
175         if (params->hash_func != NULL) {
176                 ht->hash_func = params->hash_func;
177                 ht->init_val = params->init_val;
178         }
179         else {
180                 ht->hash_func = RTE_FBK_HASH_FUNC_DEFAULT;
181                 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
182         }
183
184         te->data = (void *) ht;
185
186         TAILQ_INSERT_TAIL(fbk_hash_list, te, next);
187
188 exit:
189         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
190
191         return ht;
192 }
193
194 /**
195  * Free all memory used by a hash table.
196  *
197  * @param ht
198  *   Hash table to deallocate.
199  */
200 void
201 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
202 {
203         struct rte_tailq_entry *te;
204         struct rte_fbk_hash_list *fbk_hash_list;
205
206         if (ht == NULL)
207                 return;
208
209         fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
210                                        rte_fbk_hash_list);
211
212         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
213
214         /* find out tailq entry */
215         TAILQ_FOREACH(te, fbk_hash_list, next) {
216                 if (te->data == (void *) ht)
217                         break;
218         }
219
220         if (te == NULL) {
221                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
222                 return;
223         }
224
225         TAILQ_REMOVE(fbk_hash_list, te, next);
226
227         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
228
229         rte_free(ht);
230         rte_free(te);
231 }