-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuckoo_hash.h
292 lines (283 loc) · 25.3 KB
/
cuckoo_hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2023 Sebastiano Miano <[email protected]>
*/
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <linux/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#define LOG2(x) \
({ \
unsigned _x = (x); \
unsigned _result = 0; \
while (_x >>= 1) { \
_result++; \
} \
_result; \
})
#define NUMBER_OF_HASH_TABLES 2
#define HASH_TYPE_NUM 2
#if HASH_TYPE_NUM == 1
#define HASH_TYPE xxhash64
#include "xxhash64.h"
#elif HASH_TYPE_NUM == 2
#define HASH_TYPE fasthash32
#include "fasthash.h"
#endif
#include "cilium_builtin.h"
#define HASH_SEED_1 0x2d31e867
#define HASH_SEED_2 0x6ad611c4
#define MAX_LOOP(x) \
({ \
uint32_t _x = (x); \
4 + (int)(4 * LOG2(_x) / LOG2(2) + 0.5); \
})
//__attribute__((packed, aligned(4)));
#define BPF_CUCKOO_HASH(_name, _key_type, _leaf_type, _max_entries) \
static const uint32_t _name##_map_capacity = _max_entries; \
typedef _leaf_type _name##_cuckoo_val_t; \
typedef _key_type _name##_cuckoo_key_t; \
\
struct _name##_cuckoo_hash_cell { \
bool is_filled; \
_name##_cuckoo_key_t key; \
_name##_cuckoo_val_t val; \
} __attribute__((__packed__)); \
\
struct _name##_cuckoo_hash_table { \
int current_size; \
struct _name##_cuckoo_hash_cell elem_list[_max_entries]; \
} __attribute__((__packed__)); \
\
struct _name##_cuckoo_hash_map { \
int current_size; /* Current size */ \
struct _name##_cuckoo_hash_table t1; /* First hash table */ \
struct _name##_cuckoo_hash_table t2; /* Second hash table */ \
} __attribute__((__packed__)); \
\
struct { \
__uint(type, BPF_MAP_TYPE_ARRAY); \
__type(key, __u32); \
__type(value, struct _name##_cuckoo_hash_map); \
__uint(max_entries, 1); \
} _name SEC(".maps"); \
\
static __always_inline void _name##_sync_total_map_size(struct _name##_cuckoo_hash_map *map) { \
map->current_size = map->t1.current_size + map->t2.current_size; \
} \
\
struct _name##_cuckoo_insert_loop_ctx { \
struct _name##_cuckoo_hash_map *map; \
uint32_t hash1; \
uint32_t hash2; \
struct _name##_cuckoo_hash_cell new_elem; \
bool inserted; \
uint32_t idx; \
}; \
\
static int _name##_cuckoo_insert_loop(uint32_t index, void *data) { \
struct _name##_cuckoo_insert_loop_ctx *ctx = \
(struct _name##_cuckoo_insert_loop_ctx *)data; \
struct _name##_cuckoo_hash_cell *elem; \
struct _name##_cuckoo_hash_cell x, tmp; \
\
memset(&x, 0, sizeof(struct _name##_cuckoo_hash_cell)); \
memset(&tmp, 0, sizeof(struct _name##_cuckoo_hash_cell)); \
\
memcpy(&x, &ctx->new_elem, sizeof(struct _name##_cuckoo_hash_cell)); \
\
uint32_t idx = ctx->hash1 & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return 1; \
} \
elem = &ctx->map->t1.elem_list[idx]; \
memcpy(&tmp, elem, sizeof(struct _name##_cuckoo_hash_cell)); \
\
memcpy(elem, &x, sizeof(struct _name##_cuckoo_hash_cell)); \
\
if (!tmp.is_filled) { \
ctx->map->t1.current_size++; \
_name##_sync_total_map_size(ctx->map); \
ctx->inserted = true; \
ctx->idx = idx; \
return 1; \
} \
\
memcpy(&x, &tmp, sizeof(struct _name##_cuckoo_hash_cell)); \
ctx->hash2 = HASH_TYPE((void *)&x.key, sizeof(_name##_cuckoo_key_t), HASH_SEED_2); \
idx = ctx->hash2 & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return 1; \
} \
elem = &ctx->map->t2.elem_list[idx]; \
\
memcpy(&tmp, elem, sizeof(struct _name##_cuckoo_hash_cell)); \
\
memcpy(elem, &x, sizeof(struct _name##_cuckoo_hash_cell)); \
\
if (!tmp.is_filled) { \
ctx->map->t2.current_size++; \
_name##_sync_total_map_size(ctx->map); \
ctx->inserted = true; \
ctx->idx = idx; \
return 1; \
} \
\
memcpy(&ctx->new_elem, &tmp, sizeof(struct _name##_cuckoo_hash_cell)); \
ctx->hash1 = HASH_TYPE((void *)&tmp.key, sizeof(_name##_cuckoo_key_t), HASH_SEED_1); \
\
return 0; \
} \
\
bool _name##_cuckoo_insert(struct _name##_cuckoo_hash_map *map, _name##_cuckoo_key_t *key, \
_name##_cuckoo_val_t *val) { \
if (map == NULL || key == NULL || val == NULL) { \
return false; \
} \
\
struct _name##_cuckoo_hash_cell *elem; \
struct _name##_cuckoo_hash_cell x; \
uint32_t hash1, hash2; \
\
hash1 = HASH_TYPE((void *)key, sizeof(_name##_cuckoo_key_t), HASH_SEED_1); \
uint32_t idx = hash1 & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return false; \
} \
elem = &map->t1.elem_list[idx]; \
if (elem->is_filled) { \
if (memcmp(key, &(elem->key), sizeof(_name##_cuckoo_key_t)) == 0) { \
memcpy(&(elem->val), val, sizeof(_name##_cuckoo_val_t)); \
return false; \
} \
} \
\
hash2 = HASH_TYPE((void *)key, sizeof(_name##_cuckoo_key_t), HASH_SEED_2); \
idx = hash2 & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return false; \
} \
elem = &map->t2.elem_list[idx]; \
if (elem->is_filled) { \
if (memcmp(key, &(elem->key), sizeof(_name##_cuckoo_key_t)) == 0) { \
memcpy(&(elem->val), val, sizeof(_name##_cuckoo_val_t)); \
return false; \
} \
} \
\
memset(&x, 0, sizeof(struct _name##_cuckoo_hash_cell)); \
\
x.is_filled = true; \
memcpy(&x.key, key, sizeof(_name##_cuckoo_key_t)); \
memcpy(&x.val, val, sizeof(_name##_cuckoo_val_t)); \
\
struct _name##_cuckoo_insert_loop_ctx loop_ctx; \
memset(&loop_ctx, 0, sizeof(struct _name##_cuckoo_insert_loop_ctx)); \
loop_ctx.map = map; \
loop_ctx.hash1 = hash1; \
loop_ctx.hash2 = hash2; \
\
memcpy(&loop_ctx.new_elem, &x, sizeof(struct _name##_cuckoo_hash_cell)); \
loop_ctx.inserted = false; \
loop_ctx.idx = 0; \
\
bpf_loop(MAX_LOOP(_name##_map_capacity), &_name##_cuckoo_insert_loop, &loop_ctx, 0); \
\
if (loop_ctx.inserted) { \
return true; \
} \
\
return false; \
} \
\
static __always_inline _name##_cuckoo_val_t *_name##_cuckoo_lookup( \
struct _name##_cuckoo_hash_map *map, const _name##_cuckoo_key_t *key) { \
if (map == NULL || key == NULL) { \
return NULL; \
} \
\
struct _name##_cuckoo_hash_cell *elem; \
uint32_t hash; \
\
hash = HASH_TYPE((void *)key, sizeof(_name##_cuckoo_key_t), HASH_SEED_1); \
uint32_t idx = hash & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return NULL; \
} \
elem = &map->t1.elem_list[idx]; \
if (elem->is_filled) { \
if (memcmp(key, &(elem->key), sizeof(_name##_cuckoo_key_t)) == 0) { \
return &(elem->val); \
} \
} \
\
hash = HASH_TYPE((void *)key, sizeof(_name##_cuckoo_key_t), HASH_SEED_2); \
idx = hash & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return false; \
} \
elem = &map->t2.elem_list[idx]; \
if (elem->is_filled) { \
if (memcmp(key, &(elem->key), sizeof(_name##_cuckoo_key_t)) == 0) { \
return &(elem->val); \
} \
} \
\
return NULL; \
} \
\
bool _name##_cuckoo_delete(struct _name##_cuckoo_hash_map *map, \
const _name##_cuckoo_key_t *key) { \
if (map == NULL || key == NULL) { \
return false; \
} \
\
struct _name##_cuckoo_hash_cell *elem; \
uint32_t hash; \
\
hash = HASH_TYPE((void *)key, sizeof(_name##_cuckoo_key_t), HASH_SEED_1); \
uint32_t idx = hash & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return false; \
} \
elem = &map->t1.elem_list[idx]; \
if (elem->is_filled) { \
if (memcmp(key, &(elem->key), sizeof(_name##_cuckoo_key_t)) == 0) { \
elem->is_filled = false; \
map->t1.current_size--; \
_name##_sync_total_map_size(map); \
return true; \
} \
} \
\
hash = HASH_TYPE((void *)key, sizeof(_name##_cuckoo_key_t), HASH_SEED_2); \
idx = hash & (_name##_map_capacity - 1); \
if (idx >= _name##_map_capacity) { \
return false; \
} \
elem = &map->t2.elem_list[idx]; \
if (elem->is_filled) { \
if (memcmp(key, &(elem->key), sizeof(_name##_cuckoo_key_t)) == 0) { \
elem->is_filled = false; \
map->t2.current_size--; \
_name##_sync_total_map_size(map); \
return true; \
} \
} \
\
return false; \
}