DPDK
25.11.0
Toggle main menu visibility
Loading...
Searching...
No Matches
rte_fbk_hash.h
Go to the documentation of this file.
1
/* SPDX-License-Identifier: BSD-3-Clause
2
* Copyright(c) 2010-2014 Intel Corporation
3
*/
4
5
#ifndef _RTE_FBK_HASH_H_
6
#define _RTE_FBK_HASH_H_
7
17
18
#include <stdint.h>
19
#include <errno.h>
20
21
#include <string.h>
22
23
#include <
rte_common.h
>
24
#include <
rte_hash_crc.h
>
25
#include <
rte_jhash.h
>
26
27
#ifdef __cplusplus
28
extern
"C"
{
29
#endif
30
31
#ifndef RTE_FBK_HASH_INIT_VAL_DEFAULT
33
#define RTE_FBK_HASH_INIT_VAL_DEFAULT 0xFFFFFFFF
34
#endif
35
37
#define RTE_FBK_HASH_ENTRIES_MAX (1 << 20)
38
40
#define RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX 256
41
43
#define RTE_FBK_HASH_NAMESIZE 32
44
46
typedef
uint32_t (*
rte_fbk_hash_fn
)(uint32_t key, uint32_t init_val);
47
49
struct
rte_fbk_hash_params
{
50
const
char
*
name
;
51
uint32_t
entries
;
52
uint32_t
entries_per_bucket
;
53
int
socket_id
;
54
rte_fbk_hash_fn
hash_func
;
55
uint32_t
init_val
;
56
};
57
59
union
rte_fbk_hash_entry
{
60
uint64_t
whole_entry
;
61
struct
{
62
uint16_t
is_entry
;
63
uint16_t
value
;
64
uint32_t
key
;
65
}
entry
;
66
};
67
68
70
struct
rte_fbk_hash_table
{
71
char
name
[
RTE_FBK_HASH_NAMESIZE
];
72
uint32_t
entries
;
73
uint32_t
entries_per_bucket
;
74
uint32_t
used_entries
;
75
uint32_t
bucket_mask
;
76
uint32_t
bucket_shift
;
77
rte_fbk_hash_fn
hash_func
;
78
uint32_t
init_val
;
79
81
union
rte_fbk_hash_entry
t
[];
82
};
83
94
static
inline
uint32_t
95
rte_fbk_hash_get_bucket
(
const
struct
rte_fbk_hash_table
*ht, uint32_t
key
)
96
{
97
return
(ht->
hash_func
(
key
, ht->
init_val
) & ht->
bucket_mask
) <<
98
ht->
bucket_shift
;
99
}
100
117
static
inline
int
118
rte_fbk_hash_add_key_with_bucket
(
struct
rte_fbk_hash_table
*ht,
119
uint32_t
key
, uint16_t
value
, uint32_t bucket)
120
{
121
/*
122
* The writing of a new value to the hash table is done as a single
123
* 64bit operation. This should help prevent individual entries being
124
* corrupted due to race conditions, but it's still possible to
125
* overwrite entries that have just been made valid.
126
*/
127
const
uint64_t new_entry = ((uint64_t)(
key
) << 32) |
128
((uint64_t)(
value
) << 16) |
129
1;
/* 1 = is_entry bit. */
130
uint32_t i;
131
132
for
(i = 0; i < ht->
entries_per_bucket
; i++) {
133
/* Set entry if unused. */
134
if
(! ht->
t
[bucket + i].
entry
.
is_entry
) {
135
ht->
t
[bucket + i].
whole_entry
= new_entry;
136
ht->
used_entries
++;
137
return
0;
138
}
139
/* Change value if key already exists. */
140
if
(ht->
t
[bucket + i].
entry
.
key
==
key
) {
141
ht->
t
[bucket + i].
entry
.
value
=
value
;
142
return
0;
143
}
144
}
145
146
return
-ENOSPC;
/* No space in bucket. */
147
}
148
162
static
inline
int
163
rte_fbk_hash_add_key
(
struct
rte_fbk_hash_table
*ht,
164
uint32_t
key
, uint16_t
value
)
165
{
166
return
rte_fbk_hash_add_key_with_bucket
(ht,
167
key
,
value
,
rte_fbk_hash_get_bucket
(ht,
key
));
168
}
169
184
static
inline
int
185
rte_fbk_hash_delete_key_with_bucket
(
struct
rte_fbk_hash_table
*ht,
186
uint32_t
key
, uint32_t bucket)
187
{
188
uint32_t last_entry = ht->
entries_per_bucket
- 1;
189
uint32_t i, j;
190
191
for
(i = 0; i < ht->
entries_per_bucket
; i++) {
192
if
(ht->
t
[bucket + i].
entry
.
key
==
key
) {
193
/* Find last key in bucket. */
194
for
(j = ht->
entries_per_bucket
- 1; j > i; j-- ) {
195
if
(! ht->
t
[bucket + j].
entry
.
is_entry
) {
196
last_entry = j - 1;
197
}
198
}
199
/*
200
* Move the last key to the deleted key's position, and
201
* delete the last key. lastEntry and i may be same but
202
* it doesn't matter.
203
*/
204
ht->
t
[bucket + i].
whole_entry
=
205
ht->
t
[bucket + last_entry].
whole_entry
;
206
ht->
t
[bucket + last_entry].
whole_entry
= 0;
207
208
ht->
used_entries
--;
209
return
0;
210
}
211
}
212
213
return
-ENOENT;
/* Key didn't exist. */
214
}
215
227
static
inline
int
228
rte_fbk_hash_delete_key
(
struct
rte_fbk_hash_table
*ht, uint32_t
key
)
229
{
230
return
rte_fbk_hash_delete_key_with_bucket
(ht,
231
key
,
rte_fbk_hash_get_bucket
(ht,
key
));
232
}
233
247
static
inline
int
248
rte_fbk_hash_lookup_with_bucket
(
const
struct
rte_fbk_hash_table
*ht,
249
uint32_t
key
, uint32_t bucket)
250
{
251
union
rte_fbk_hash_entry
current_entry;
252
uint32_t i;
253
254
for
(i = 0; i < ht->
entries_per_bucket
; i++) {
255
/* Single read of entry, which should be atomic. */
256
current_entry.
whole_entry
= ht->
t
[bucket + i].
whole_entry
;
257
if
(! current_entry.
entry
.
is_entry
) {
258
return
-ENOENT;
/* Error once we hit an empty field. */
259
}
260
if
(current_entry.
entry
.
key
==
key
) {
261
return
current_entry.
entry
.
value
;
262
}
263
}
264
return
-ENOENT;
/* Key didn't exist. */
265
}
266
277
static
inline
int
278
rte_fbk_hash_lookup
(
const
struct
rte_fbk_hash_table
*ht, uint32_t
key
)
279
{
280
return
rte_fbk_hash_lookup_with_bucket
(ht,
281
key
,
rte_fbk_hash_get_bucket
(ht,
key
));
282
}
283
291
static
inline
void
292
rte_fbk_hash_clear_all
(
struct
rte_fbk_hash_table
*ht)
293
{
294
memset(ht->
t
, 0,
sizeof
(ht->
t
[0]) * ht->
entries
);
295
ht->
used_entries
= 0;
296
}
297
306
static
inline
double
307
rte_fbk_hash_get_load_factor
(
struct
rte_fbk_hash_table
*ht)
308
{
309
return
(
double
)ht->
used_entries
/ (double)ht->
entries
;
310
}
311
324
struct
rte_fbk_hash_table
*
rte_fbk_hash_find_existing
(
const
char
*
name
);
325
333
void
rte_fbk_hash_free
(
struct
rte_fbk_hash_table
*ht);
334
352
struct
rte_fbk_hash_table
*
353
rte_fbk_hash_create
(
const
struct
rte_fbk_hash_params
*params)
354
__rte_malloc
__rte_dealloc
(
rte_fbk_hash_free
, 1);
355
356
#ifdef __cplusplus
357
}
358
#endif
359
360
#endif
/* _RTE_FBK_HASH_H_ */
rte_common.h
__rte_dealloc
#define __rte_dealloc(dealloc, argno)
Definition
rte_common.h:339
__rte_malloc
#define __rte_malloc
Definition
rte_common.h:328
RTE_FBK_HASH_NAMESIZE
#define RTE_FBK_HASH_NAMESIZE
Definition
rte_fbk_hash.h:43
rte_fbk_hash_add_key_with_bucket
static int rte_fbk_hash_add_key_with_bucket(struct rte_fbk_hash_table *ht, uint32_t key, uint16_t value, uint32_t bucket)
Definition
rte_fbk_hash.h:118
rte_fbk_hash_add_key
static int rte_fbk_hash_add_key(struct rte_fbk_hash_table *ht, uint32_t key, uint16_t value)
Definition
rte_fbk_hash.h:163
rte_fbk_hash_free
void rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
rte_fbk_hash_lookup_with_bucket
static int rte_fbk_hash_lookup_with_bucket(const struct rte_fbk_hash_table *ht, uint32_t key, uint32_t bucket)
Definition
rte_fbk_hash.h:248
rte_fbk_hash_delete_key_with_bucket
static int rte_fbk_hash_delete_key_with_bucket(struct rte_fbk_hash_table *ht, uint32_t key, uint32_t bucket)
Definition
rte_fbk_hash.h:185
rte_fbk_hash_find_existing
struct rte_fbk_hash_table * rte_fbk_hash_find_existing(const char *name)
rte_fbk_hash_lookup
static int rte_fbk_hash_lookup(const struct rte_fbk_hash_table *ht, uint32_t key)
Definition
rte_fbk_hash.h:278
rte_fbk_hash_delete_key
static int rte_fbk_hash_delete_key(struct rte_fbk_hash_table *ht, uint32_t key)
Definition
rte_fbk_hash.h:228
rte_fbk_hash_fn
uint32_t(* rte_fbk_hash_fn)(uint32_t key, uint32_t init_val)
Definition
rte_fbk_hash.h:46
rte_fbk_hash_get_bucket
static uint32_t rte_fbk_hash_get_bucket(const struct rte_fbk_hash_table *ht, uint32_t key)
Definition
rte_fbk_hash.h:95
rte_fbk_hash_clear_all
static void rte_fbk_hash_clear_all(struct rte_fbk_hash_table *ht)
Definition
rte_fbk_hash.h:292
rte_fbk_hash_get_load_factor
static double rte_fbk_hash_get_load_factor(struct rte_fbk_hash_table *ht)
Definition
rte_fbk_hash.h:307
rte_fbk_hash_create
struct rte_fbk_hash_table * rte_fbk_hash_create(const struct rte_fbk_hash_params *params) __rte_malloc __rte_dealloc(rte_fbk_hash_free
rte_hash_crc.h
rte_jhash.h
rte_fbk_hash_params
Definition
rte_fbk_hash.h:49
rte_fbk_hash_params::socket_id
int socket_id
Definition
rte_fbk_hash.h:53
rte_fbk_hash_params::entries
uint32_t entries
Definition
rte_fbk_hash.h:51
rte_fbk_hash_params::hash_func
rte_fbk_hash_fn hash_func
Definition
rte_fbk_hash.h:54
rte_fbk_hash_params::name
const char * name
Definition
rte_fbk_hash.h:50
rte_fbk_hash_params::init_val
uint32_t init_val
Definition
rte_fbk_hash.h:55
rte_fbk_hash_params::entries_per_bucket
uint32_t entries_per_bucket
Definition
rte_fbk_hash.h:52
rte_fbk_hash_table
Definition
rte_fbk_hash.h:70
rte_fbk_hash_table::entries
uint32_t entries
Definition
rte_fbk_hash.h:72
rte_fbk_hash_table::bucket_shift
uint32_t bucket_shift
Definition
rte_fbk_hash.h:76
rte_fbk_hash_table::hash_func
rte_fbk_hash_fn hash_func
Definition
rte_fbk_hash.h:77
rte_fbk_hash_table::t
union rte_fbk_hash_entry t[]
Definition
rte_fbk_hash.h:81
rte_fbk_hash_table::bucket_mask
uint32_t bucket_mask
Definition
rte_fbk_hash.h:75
rte_fbk_hash_table::used_entries
uint32_t used_entries
Definition
rte_fbk_hash.h:74
rte_fbk_hash_table::init_val
uint32_t init_val
Definition
rte_fbk_hash.h:78
rte_fbk_hash_table::name
char name[RTE_FBK_HASH_NAMESIZE]
Definition
rte_fbk_hash.h:71
rte_fbk_hash_table::entries_per_bucket
uint32_t entries_per_bucket
Definition
rte_fbk_hash.h:73
rte_fbk_hash_entry
Definition
rte_fbk_hash.h:59
rte_fbk_hash_entry::whole_entry
uint64_t whole_entry
Definition
rte_fbk_hash.h:60
rte_fbk_hash_entry::key
uint32_t key
Definition
rte_fbk_hash.h:64
rte_fbk_hash_entry::value
uint16_t value
Definition
rte_fbk_hash.h:63
rte_fbk_hash_entry::is_entry
uint16_t is_entry
Definition
rte_fbk_hash.h:62
rte_fbk_hash_entry::entry
struct rte_fbk_hash_entry::@161375022003300173325054045162201147113363210106 entry
lib
hash
rte_fbk_hash.h
Generated by
1.17.0