-
Notifications
You must be signed in to change notification settings - Fork 205
/
ebpf_maps.c
327 lines (269 loc) · 9.2 KB
/
ebpf_maps.c
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) Microsoft Corporation
* SPDX-License-Identifier: MIT
*/
#include "ebpf_maps.h"
#include "ebpf_epoch.h"
#include "ebpf_object.h"
typedef struct _ebpf_core_map
{
ebpf_object_t object;
struct _ebpf_map_definition ebpf_map_definition;
ebpf_lock_t lock;
uint8_t* data;
} ebpf_core_map_t;
typedef struct _ebpf_map_function_table
{
ebpf_core_map_t* (*create_map)(_In_ const ebpf_map_definition_t* map_definition);
void (*delete_map)(_In_ ebpf_core_map_t* map);
uint8_t* (*find_entry)(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key);
ebpf_error_code_t (*update_entry)(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key, _In_ const uint8_t* value);
ebpf_error_code_t (*delete_entry)(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key);
ebpf_error_code_t (*next_key)(_In_ ebpf_core_map_t* map, _In_ const uint8_t* previous_key, _Out_ uint8_t* next_key);
} ebpf_map_function_table_t;
extern ebpf_map_function_table_t ebpf_map_function_tables[EBPF_MAP_TYPE_ARRAY + 1];
ebpf_error_code_t
ebpf_map_create(const ebpf_map_definition_t* ebpf_map_definition, ebpf_map_t** ebpf_map)
{
ebpf_map_t* local_map = NULL;
size_t type = ebpf_map_definition->type;
if (ebpf_map_definition->type > EBPF_MAP_TYPE_ARRAY)
return EBPF_ERROR_INVALID_PARAMETER;
if (!ebpf_map_function_tables[type].create_map)
return EBPF_ERROR_NOT_SUPPORTED;
local_map = ebpf_map_function_tables[type].create_map(ebpf_map_definition);
if (!local_map)
return EBPF_ERROR_OUT_OF_RESOURCES;
ebpf_object_initialize(
&local_map->object,
EBPF_OBJECT_MAP,
(ebpf_free_object_t)ebpf_map_function_tables[local_map->ebpf_map_definition.type].delete_map);
*ebpf_map = local_map;
return EBPF_ERROR_SUCCESS;
}
ebpf_map_definition_t*
ebpf_map_get_definition(ebpf_map_t* map)
{
return &map->ebpf_map_definition;
}
uint8_t*
ebpf_map_find_entry(ebpf_map_t* map, const uint8_t* key)
{
return ebpf_map_function_tables[map->ebpf_map_definition.type].find_entry(map, key);
}
ebpf_error_code_t
ebpf_map_update_entry(ebpf_map_t* map, const uint8_t* key, const uint8_t* value)
{
return ebpf_map_function_tables[map->ebpf_map_definition.type].update_entry(map, key, value);
}
ebpf_error_code_t
ebpf_map_delete_entry(ebpf_map_t* map, const uint8_t* key)
{
return ebpf_map_function_tables[map->ebpf_map_definition.type].delete_entry(map, key);
}
ebpf_error_code_t
ebpf_map_next_key(ebpf_map_t* map, const uint8_t* previous_key, uint8_t* next_key)
{
return ebpf_map_function_tables[map->ebpf_map_definition.type].next_key(map, previous_key, next_key);
}
static ebpf_core_map_t*
ebpf_create_array_map(_In_ const ebpf_map_definition_t* map_definition)
{
ebpf_error_code_t retval;
size_t map_entry_size = sizeof(ebpf_core_map_t);
size_t map_data_size = 0;
ebpf_core_map_t* map = NULL;
retval = ebpf_safe_size_t_multiply(map_definition->max_entries, map_definition->value_size, &map_data_size);
if (retval != EBPF_ERROR_SUCCESS) {
goto Done;
}
retval = ebpf_safe_size_t_multiply(map_data_size, map_entry_size, &map_entry_size);
if (retval != EBPF_ERROR_SUCCESS) {
goto Done;
}
// allocate
map = ebpf_allocate(map_entry_size, EBPF_MEMORY_NO_EXECUTE);
if (map == NULL) {
goto Done;
}
memset(map, 0, map_entry_size);
map->ebpf_map_definition = *map_definition;
map->data = (uint8_t*)(map + 1);
Done:
return map;
}
static void
ebpf_delete_array_map(_In_ ebpf_core_map_t* map)
{
ebpf_free(map);
}
static uint8_t*
ebpf_find_array_map_entry(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key)
{
uint32_t key_value;
if (!map || !key)
return NULL;
key_value = *(uint32_t*)key;
if (key_value > map->ebpf_map_definition.max_entries)
return NULL;
return &map->data[key_value * map->ebpf_map_definition.value_size];
}
static ebpf_error_code_t
ebpf_update_array_map_entry(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key, _In_ const uint8_t* data)
{
uint32_t key_value;
if (!map || !key)
return EBPF_ERROR_INVALID_PARAMETER;
key_value = *(uint32_t*)key;
if (key_value > map->ebpf_map_definition.max_entries)
return EBPF_ERROR_INVALID_PARAMETER;
uint8_t* entry = &map->data[*key * map->ebpf_map_definition.value_size];
memcpy(entry, data, map->ebpf_map_definition.value_size);
return EBPF_ERROR_SUCCESS;
}
static ebpf_error_code_t
ebpf_delete_array_map_entry(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key)
{
uint32_t key_value;
if (!map || !key)
return EBPF_ERROR_INVALID_PARAMETER;
key_value = *(uint32_t*)key;
if (key_value > map->ebpf_map_definition.max_entries)
return EBPF_ERROR_NOT_FOUND;
uint8_t* entry = &map->data[key_value * map->ebpf_map_definition.value_size];
memset(entry, 0, map->ebpf_map_definition.value_size);
return EBPF_ERROR_SUCCESS;
}
static ebpf_error_code_t
ebpf_next_array_map_key(_In_ ebpf_core_map_t* map, _In_ const uint8_t* previous_key, _Out_ uint8_t* next_key)
{
uint32_t key_value;
if (!map || !next_key)
return EBPF_ERROR_INVALID_PARAMETER;
if (previous_key) {
key_value = *(uint32_t*)previous_key;
key_value++;
} else
key_value = 0;
if (key_value >= map->ebpf_map_definition.max_entries)
return EBPF_ERROR_NO_MORE_KEYS;
*(uint32_t*)next_key = key_value;
return EBPF_ERROR_SUCCESS;
}
static ebpf_core_map_t*
ebpf_create_hash_map(_In_ const ebpf_map_definition_t* map_definition)
{
ebpf_error_code_t retval;
size_t map_size = sizeof(ebpf_core_map_t);
ebpf_core_map_t* map = NULL;
map = ebpf_allocate(map_size, EBPF_MEMORY_NO_EXECUTE);
if (map == NULL) {
retval = EBPF_ERROR_OUT_OF_RESOURCES;
goto Done;
}
map->ebpf_map_definition = *map_definition;
map->data = NULL;
retval = ebpf_hash_table_create(
(ebpf_hash_table_t**)&map->data,
ebpf_epoch_allocate,
ebpf_epoch_free,
map->ebpf_map_definition.key_size,
map->ebpf_map_definition.value_size,
NULL);
if (retval != EBPF_ERROR_SUCCESS) {
goto Done;
}
ebpf_lock_create(&map->lock);
retval = EBPF_ERROR_SUCCESS;
Done:
if (retval != EBPF_ERROR_SUCCESS) {
if (map && map->data) {
ebpf_hash_table_destroy((ebpf_hash_table_t*)map->data);
}
ebpf_free(map);
map = NULL;
}
return map;
}
static void
ebpf_delete_hash_map(_In_ ebpf_core_map_t* map)
{
ebpf_lock_destroy(&map->lock);
ebpf_hash_table_destroy((ebpf_hash_table_t*)map->data);
ebpf_free(map);
}
static uint8_t*
ebpf_find_hash_map_entry(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key)
{
ebpf_lock_state_t lock_state;
uint8_t* value = NULL;
if (!map || !key)
return NULL;
ebpf_lock_lock(&map->lock, &lock_state);
if (ebpf_hash_table_find((ebpf_hash_table_t*)map->data, key, &value) != EBPF_ERROR_SUCCESS) {
value = NULL;
}
ebpf_lock_unlock(&map->lock, &lock_state);
return value;
}
static ebpf_error_code_t
ebpf_update_hash_map_entry(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key, _In_ const uint8_t* data)
{
ebpf_error_code_t result;
ebpf_lock_state_t lock_state;
size_t entry_count = 0;
uint8_t* value;
if (!map || !key || !data)
return EBPF_ERROR_INVALID_PARAMETER;
ebpf_lock_lock(&map->lock, &lock_state);
entry_count = ebpf_hash_table_key_count((ebpf_hash_table_t*)map->data);
if ((entry_count == map->ebpf_map_definition.max_entries) &&
(ebpf_hash_table_find((ebpf_hash_table_t*)map->data, key, &value) != EBPF_ERROR_SUCCESS))
result = EBPF_ERROR_INVALID_PARAMETER;
else
result = ebpf_hash_table_update((ebpf_hash_table_t*)map->data, key, data);
ebpf_lock_unlock(&map->lock, &lock_state);
return result;
}
static ebpf_error_code_t
ebpf_delete_hash_map_entry(_In_ ebpf_core_map_t* map, _In_ const uint8_t* key)
{
ebpf_error_code_t result;
ebpf_lock_state_t lock_state;
if (!map || !key)
return EBPF_ERROR_INVALID_PARAMETER;
ebpf_lock_lock(&map->lock, &lock_state);
result = ebpf_hash_table_delete((ebpf_hash_table_t*)map->data, key);
ebpf_lock_unlock(&map->lock, &lock_state);
return result;
}
static ebpf_error_code_t
ebpf_next_hash_map_key(_In_ ebpf_core_map_t* map, _In_ const uint8_t* previous_key, _Out_ uint8_t* next_key)
{
ebpf_error_code_t result;
ebpf_lock_state_t lock_state;
if (!map || !next_key)
return EBPF_ERROR_INVALID_PARAMETER;
ebpf_lock_lock(&map->lock, &lock_state);
result = ebpf_hash_table_next_key((ebpf_hash_table_t*)map->data, previous_key, next_key);
ebpf_lock_unlock(&map->lock, &lock_state);
return result;
}
ebpf_map_function_table_t ebpf_map_function_tables[] = {
{// EBPF_MAP_TYPE_UNSPECIFIED
NULL},
{// EBPF_MAP_TYPE_HASH
ebpf_create_hash_map,
ebpf_delete_hash_map,
ebpf_find_hash_map_entry,
ebpf_update_hash_map_entry,
ebpf_delete_hash_map_entry,
ebpf_next_hash_map_key},
{// EBPF_MAP_TYPE_ARRAY
ebpf_create_array_map,
ebpf_delete_array_map,
ebpf_find_array_map_entry,
ebpf_update_array_map_entry,
ebpf_delete_array_map_entry,
ebpf_next_array_map_key},
};