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