remove version in all files
[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_malloc.h>
48 #include <rte_common.h>
49 #include <rte_per_lcore.h>
50 #include <rte_errno.h>
51 #include <rte_string_fns.h>
52 #include <rte_cpuflags.h>
53 #include <rte_log.h>
54
55 #include "rte_fbk_hash.h"
56 #include "rte_jhash.h"
57 #include "rte_hash_crc.h"
58
59 TAILQ_HEAD(rte_fbk_hash_list, rte_fbk_hash_table);
60
61 /* global list of fbk_hashes (used for debug/dump) */
62 static struct rte_fbk_hash_list *fbk_hash_list = NULL;
63
64 /* macro to prevent duplication of list creation check code */
65 #define CHECK_FBK_HASH_LIST_CREATED() do { \
66         if (fbk_hash_list == NULL) \
67                 if ((fbk_hash_list = RTE_TAILQ_RESERVE("RTE_FBK_HASH", \
68                                 rte_fbk_hash_list)) == NULL){ \
69                         rte_errno = E_RTE_NO_TAILQ; \
70                         return NULL; \
71                 } \
72 } while (0)
73
74
75 /**
76  * Performs a lookup for an existing hash table, and returns a pointer to
77  * the table if found.
78  *
79  * @param name
80  *   Name of the hash table to find
81  *
82  * @return
83  *   pointer to hash table structure or NULL on error.
84  */
85 struct rte_fbk_hash_table *
86 rte_fbk_hash_find_existing(const char *name)
87 {
88         struct rte_fbk_hash_table *h;
89
90         /* check that we have an initialised tail queue */
91         CHECK_FBK_HASH_LIST_CREATED();
92
93         TAILQ_FOREACH(h, fbk_hash_list, next) {
94                 if (strncmp(name, h->name, RTE_FBK_HASH_NAMESIZE) == 0)
95                         break;
96         }
97         if (h == NULL)
98                 rte_errno = ENOENT;
99         return h;
100 }
101
102 /**
103  * Create a new hash table for use with four byte keys.
104  *
105  * @param params
106  *   Parameters used in creation of hash table.
107  *
108  * @return
109  *   Pointer to hash table structure that is used in future hash table
110  *   operations, or NULL on error.
111  */
112 struct rte_fbk_hash_table *
113 rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
114 {
115         struct rte_fbk_hash_table *ht;
116         char hash_name[RTE_FBK_HASH_NAMESIZE];
117         const uint32_t mem_size =
118                         sizeof(*ht) + (sizeof(ht->t[0]) * params->entries);
119         uint32_t i;
120
121         /* check that we have access to create things in shared memory. */
122         if (rte_eal_process_type() == RTE_PROC_SECONDARY){
123                 rte_errno = E_RTE_SECONDARY;
124                 return NULL;
125         }
126
127         /* check that we have an initialised tail queue */
128         CHECK_FBK_HASH_LIST_CREATED();
129
130         /* Error checking of parameters. */
131         if ((!rte_is_power_of_2(params->entries)) ||
132                         (!rte_is_power_of_2(params->entries_per_bucket)) ||
133                         (params->entries == 0) ||
134                         (params->entries_per_bucket == 0) ||
135                         (params->entries_per_bucket > params->entries) ||
136                         (params->entries > RTE_FBK_HASH_ENTRIES_MAX) ||
137                         (params->entries_per_bucket > RTE_FBK_HASH_ENTRIES_MAX)){
138                 rte_errno = EINVAL;
139                 return NULL;
140         }
141
142         rte_snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
143
144         /* Allocate memory for table. */
145 #if defined(RTE_LIBRTE_HASH_USE_MEMZONE)
146         const struct rte_memzone *mz;
147         mz = rte_memzone_reserve(hash_name, mem_size, params->socket_id, 0);
148         if (mz == NULL)
149                 return NULL;
150         ht = (struct rte_fbk_hash_table *)mz->addr;
151 #else
152         ht = (struct rte_fbk_hash_table *)rte_malloc(hash_name, mem_size, 0);
153         if (ht == NULL)
154                 return NULL;
155 #endif
156         memset(ht, 0, mem_size);
157
158         /* Set up hash table context. */
159         rte_snprintf(ht->name, sizeof(ht->name), "%s", params->name);
160         ht->entries = params->entries;
161         ht->entries_per_bucket = params->entries_per_bucket;
162         ht->used_entries = 0;
163         ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
164         for (ht->bucket_shift = 0, i = 1;
165             (params->entries_per_bucket & i) == 0;
166             ht->bucket_shift++, i <<= 1)
167                 ; /* empty loop body */
168
169         if (params->hash_func != NULL) {
170                 ht->hash_func = params->hash_func;
171                 ht->init_val = params->init_val;
172         }
173         else {
174                 ht->hash_func = RTE_FBK_HASH_FUNC_DEFAULT;
175                 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
176         }
177
178         if (ht->hash_func == rte_hash_crc_4byte &&
179                         !rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_2)) {
180                 RTE_LOG(WARNING, HASH, "CRC32 instruction requires SSE4.2, "
181                                 "which is not supported on this system. "
182                                 "Falling back to software hash\n.");
183                 ht->hash_func = rte_jhash_1word;
184         }
185
186         TAILQ_INSERT_TAIL(fbk_hash_list, ht, next);
187         return ht;
188 }
189
190 /**
191  * Free all memory used by a hash table.
192  *
193  * @param ht
194  *   Hash table to deallocate.
195  */
196 void
197 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
198 {
199         if (ht == NULL)
200                 return;
201         /* No way to deallocate memzones - but can de-allocate from malloc */
202 #if !defined(RTE_LIBRTE_HASH_USE_MEMZONE)
203         TAILQ_REMOVE(fbk_hash_list, ht, next);
204         rte_free(ht);
205 #endif
206         RTE_SET_USED(ht);
207         return;
208 }
209