hash: select default hash by looking at SSE flags
[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_eal_memconfig.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
57 TAILQ_HEAD(rte_fbk_hash_list, rte_fbk_hash_table);
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;
73         struct rte_fbk_hash_list *fbk_hash_list;
74
75         /* check that we have an initialised tail queue */
76         if ((fbk_hash_list = 
77              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list)) == NULL) {
78                 rte_errno = E_RTE_NO_TAILQ;
79                 return NULL;
80         }
81
82         TAILQ_FOREACH(h, fbk_hash_list, next) {
83                 if (strncmp(name, h->name, RTE_FBK_HASH_NAMESIZE) == 0)
84                         break;
85         }
86         if (h == NULL)
87                 rte_errno = ENOENT;
88         return h;
89 }
90
91 /**
92  * Create a new hash table for use with four byte keys.
93  *
94  * @param params
95  *   Parameters used in creation of hash table.
96  *
97  * @return
98  *   Pointer to hash table structure that is used in future hash table
99  *   operations, or NULL on error.
100  */
101 struct rte_fbk_hash_table *
102 rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
103 {
104         struct rte_fbk_hash_table *ht = NULL;
105         char hash_name[RTE_FBK_HASH_NAMESIZE];
106         const uint32_t mem_size =
107                         sizeof(*ht) + (sizeof(ht->t[0]) * params->entries);
108         uint32_t i;
109         struct rte_fbk_hash_list *fbk_hash_list;
110
111         /* check that we have an initialised tail queue */
112         if ((fbk_hash_list = 
113              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list)) == NULL) {
114                 rte_errno = E_RTE_NO_TAILQ;
115                 return NULL;    
116         }
117
118         /* Error checking of parameters. */
119         if ((!rte_is_power_of_2(params->entries)) ||
120                         (!rte_is_power_of_2(params->entries_per_bucket)) ||
121                         (params->entries == 0) ||
122                         (params->entries_per_bucket == 0) ||
123                         (params->entries_per_bucket > params->entries) ||
124                         (params->entries > RTE_FBK_HASH_ENTRIES_MAX) ||
125                         (params->entries_per_bucket > RTE_FBK_HASH_ENTRIES_MAX)){
126                 rte_errno = EINVAL;
127                 return NULL;
128         }
129
130         rte_snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
131
132         /* guarantee there's no existing */
133         TAILQ_FOREACH(ht, fbk_hash_list, next) {
134                 if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
135                         break;
136         }
137         if (ht != NULL)
138                 return NULL;
139
140         /* Allocate memory for table. */
141 #if defined(RTE_LIBRTE_HASH_USE_MEMZONE)
142         const struct rte_memzone *mz;
143         mz = rte_memzone_reserve(hash_name, mem_size, params->socket_id, 0);
144         if (mz == NULL)
145                 return NULL;
146         ht = (struct rte_fbk_hash_table *)mz->addr;
147 #else
148         ht = (struct rte_fbk_hash_table *)rte_malloc(hash_name, mem_size, 0);
149         if (ht == NULL)
150                 return NULL;
151 #endif
152         memset(ht, 0, mem_size);
153
154         /* Set up hash table context. */
155         rte_snprintf(ht->name, sizeof(ht->name), "%s", params->name);
156         ht->entries = params->entries;
157         ht->entries_per_bucket = params->entries_per_bucket;
158         ht->used_entries = 0;
159         ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
160         for (ht->bucket_shift = 0, i = 1;
161             (params->entries_per_bucket & i) == 0;
162             ht->bucket_shift++, i <<= 1)
163                 ; /* empty loop body */
164
165         if (params->hash_func != NULL) {
166                 ht->hash_func = params->hash_func;
167                 ht->init_val = params->init_val;
168         }
169         else {
170                 ht->hash_func = RTE_FBK_HASH_FUNC_DEFAULT;
171                 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
172         }
173
174         TAILQ_INSERT_TAIL(fbk_hash_list, ht, next);
175         return ht;
176 }
177
178 /**
179  * Free all memory used by a hash table.
180  *
181  * @param ht
182  *   Hash table to deallocate.
183  */
184 void
185 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
186 {
187         if (ht == NULL)
188                 return;
189
190         /* No way to deallocate memzones - but can de-allocate from malloc */
191 #if !defined(RTE_LIBRTE_HASH_USE_MEMZONE)
192         RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list, ht);
193         rte_free(ht);
194 #endif
195         RTE_SET_USED(ht);
196         return;
197 }
198