cff5a8fb47496ad9ec7b9f22f878e4cc3ddb53a6
[dpdk.git] / lib / librte_hash / rte_fbk_hash.c
1 /**
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <errno.h>
40
41 #include <sys/queue.h>
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_tailq.h>
45 #include <rte_eal.h>
46 #include <rte_hash_crc.h>
47 #include <rte_eal_memconfig.h>
48 #include <rte_malloc.h>
49 #include <rte_common.h>
50 #include <rte_per_lcore.h>
51 #include <rte_errno.h>
52 #include <rte_string_fns.h>
53 #include <rte_cpuflags.h>
54 #include <rte_log.h>
55
56 #include "rte_fbk_hash.h"
57 #include "rte_jhash.h"
58 #include "rte_hash_crc.h"
59
60 TAILQ_HEAD(rte_fbk_hash_list, rte_fbk_hash_table);
61
62 /**
63  * Performs a lookup for an existing hash table, and returns a pointer to
64  * the table if found.
65  *
66  * @param name
67  *   Name of the hash table to find
68  *
69  * @return
70  *   pointer to hash table structure or NULL on error.
71  */
72 struct rte_fbk_hash_table *
73 rte_fbk_hash_find_existing(const char *name)
74 {
75         struct rte_fbk_hash_table *h;
76         struct rte_fbk_hash_list *fbk_hash_list;
77
78         /* check that we have an initialised tail queue */
79         if ((fbk_hash_list = 
80              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list)) == NULL) {
81                 rte_errno = E_RTE_NO_TAILQ;
82                 return NULL;
83         }
84
85         TAILQ_FOREACH(h, fbk_hash_list, next) {
86                 if (strncmp(name, h->name, RTE_FBK_HASH_NAMESIZE) == 0)
87                         break;
88         }
89         if (h == NULL)
90                 rte_errno = ENOENT;
91         return h;
92 }
93
94 /**
95  * Create a new hash table for use with four byte keys.
96  *
97  * @param params
98  *   Parameters used in creation of hash table.
99  *
100  * @return
101  *   Pointer to hash table structure that is used in future hash table
102  *   operations, or NULL on error.
103  */
104 struct rte_fbk_hash_table *
105 rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
106 {
107         struct rte_fbk_hash_table *ht = NULL;
108         char hash_name[RTE_FBK_HASH_NAMESIZE];
109         const uint32_t mem_size =
110                         sizeof(*ht) + (sizeof(ht->t[0]) * params->entries);
111         uint32_t i;
112         struct rte_fbk_hash_list *fbk_hash_list;
113
114         /* check that we have an initialised tail queue */
115         if ((fbk_hash_list = 
116              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list)) == NULL) {
117                 rte_errno = E_RTE_NO_TAILQ;
118                 return NULL;    
119         }
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_MAX)){
129                 rte_errno = EINVAL;
130                 return NULL;
131         }
132
133         rte_snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
134
135         /* guarantee there's no existing */
136         TAILQ_FOREACH(ht, fbk_hash_list, next) {
137                 if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
138                         break;
139         }
140         if (ht != NULL)
141                 return NULL;
142
143         /* Allocate memory for table. */
144 #if defined(RTE_LIBRTE_HASH_USE_MEMZONE)
145         const struct rte_memzone *mz;
146         mz = rte_memzone_reserve(hash_name, mem_size, params->socket_id, 0);
147         if (mz == NULL)
148                 return NULL;
149         ht = (struct rte_fbk_hash_table *)mz->addr;
150 #else
151         ht = (struct rte_fbk_hash_table *)rte_malloc(hash_name, mem_size, 0);
152         if (ht == NULL)
153                 return NULL;
154 #endif
155         memset(ht, 0, mem_size);
156
157         /* Set up hash table context. */
158         rte_snprintf(ht->name, sizeof(ht->name), "%s", params->name);
159         ht->entries = params->entries;
160         ht->entries_per_bucket = params->entries_per_bucket;
161         ht->used_entries = 0;
162         ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
163         for (ht->bucket_shift = 0, i = 1;
164             (params->entries_per_bucket & i) == 0;
165             ht->bucket_shift++, i <<= 1)
166                 ; /* empty loop body */
167
168         if (params->hash_func != NULL) {
169                 ht->hash_func = params->hash_func;
170                 ht->init_val = params->init_val;
171         }
172         else {
173                 ht->hash_func = RTE_FBK_HASH_FUNC_DEFAULT;
174                 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
175         }
176
177         if (ht->hash_func == rte_hash_crc_4byte &&
178                         !rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_2)) {
179                 RTE_LOG(WARNING, HASH, "CRC32 instruction requires SSE4.2, "
180                                 "which is not supported on this system. "
181                                 "Falling back to software hash\n.");
182                 ht->hash_func = rte_jhash_1word;
183         }
184
185         TAILQ_INSERT_TAIL(fbk_hash_list, ht, next);
186         return ht;
187 }
188
189 /**
190  * Free all memory used by a hash table.
191  *
192  * @param ht
193  *   Hash table to deallocate.
194  */
195 void
196 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
197 {
198         if (ht == NULL)
199                 return;
200
201         /* No way to deallocate memzones - but can de-allocate from malloc */
202 #if !defined(RTE_LIBRTE_HASH_USE_MEMZONE)
203         RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list, ht);
204         rte_free(ht);
205 #endif
206         RTE_SET_USED(ht);
207         return;
208 }
209