-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuckoo_test.bpf.c
264 lines (232 loc) · 9.02 KB
/
cuckoo_test.bpf.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
/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
/* Copyright (c) 2022 Sebastiano Miano <[email protected]> */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/in.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <linux/types.h>
#include <linux/pkt_cls.h>
#include <linux/if_vlan.h>
#include <sys/socket.h>
#include <stdint.h>
#include <stdbool.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h>
#include "lib/cilium_builtin.h"
#include "lib/cuckoo_hash.h"
#include "cuckoo_test_cfg.h"
extern __u32 LINUX_KERNEL_VERSION __kconfig;
struct flow_key {
uint32_t src_ip;
uint32_t dst_ip;
uint16_t src_port;
uint16_t dst_port;
uint8_t protocol;
};
BPF_CUCKOO_HASH(test_map, struct flow_key, uint32_t, 2097152)
static bool __always_inline test_case1(struct test_map_cuckoo_hash_map *map) {
struct flow_key flow1 = {
.protocol = 17, .src_ip = 0x10101010, .dst_ip = 0x10101011, .src_port = 1, .dst_port = 2};
test_map_cuckoo_val_t *val = test_map_cuckoo_lookup(map, &flow1);
if (val == NULL)
return true;
return false;
}
static bool __always_inline test_case2(struct test_map_cuckoo_hash_map *map) {
struct flow_key flow1 = {
.protocol = 17, .src_ip = 0x10101010, .dst_ip = 0x10101011, .src_port = 1, .dst_port = 2};
test_map_cuckoo_val_t val = 64;
if (test_map_cuckoo_insert(map, &flow1, &val)) {
return true;
}
return false;
}
static bool __always_inline test_case3(struct test_map_cuckoo_hash_map *map) {
struct flow_key flow1 = {
.protocol = 17, .src_ip = 0x10101010, .dst_ip = 0x10101011, .src_port = 1, .dst_port = 2};
test_map_cuckoo_val_t val = 64;
test_map_cuckoo_insert(map, &flow1, &val);
test_map_cuckoo_val_t *ret_val = test_map_cuckoo_lookup(map, &flow1);
if (ret_val) {
bpf_printk("ret_val: %d", *ret_val);
}
if (!ret_val || *ret_val != 64)
return false;
return true;
}
static bool __always_inline test_case4(struct test_map_cuckoo_hash_map *map) {
struct flow_key flow1 = {
.protocol = 17, .src_ip = 0x10101010, .dst_ip = 0x10101011, .src_port = 1, .dst_port = 2};
test_map_cuckoo_val_t val = 64;
test_map_cuckoo_insert(map, &flow1, &val);
test_map_cuckoo_val_t *ret_val = test_map_cuckoo_lookup(map, &flow1);
if (!ret_val || *ret_val != 64)
return false;
test_map_cuckoo_delete(map, &flow1);
ret_val = test_map_cuckoo_lookup(map, &flow1);
if (ret_val)
return false;
return true;
}
static bool __always_inline test_case5(struct test_map_cuckoo_hash_map *map) {
struct flow_key flow3 = {
.src_ip = 0x10101010, .dst_ip = 0x10101013, .src_port = 2, .dst_port = 1, .protocol = 17};
struct flow_key flow4 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 10, .dst_port = 10, .protocol = 30};
struct flow_key flow5 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 1, .dst_port = 22, .protocol = 30};
struct flow_key flow6 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 15, .dst_port = 10, .protocol = 50};
struct flow_key flow7 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 10, .dst_port = 20, .protocol = 70};
struct flow_key flow8 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 10, .dst_port = 7, .protocol = 50};
struct flow_key flow9 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 2, .dst_port = 3, .protocol = 100};
struct flow_key flow10 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 1, .dst_port = 1, .protocol = 1};
struct flow_key flow11 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 3, .dst_port = 3, .protocol = 30};
struct flow_key flow12 = {
.src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 4, .dst_port = 5, .protocol = 30};
struct flow_key flow_array[] = {flow3, flow4, flow5, flow6, flow7,
flow8, flow9, flow10, flow11, flow12};
#if __clang_major__ < 15
#pragma unroll
#endif
for (int i = 0; i < (sizeof(flow_array) / sizeof(struct flow_key)); i++) {
test_map_cuckoo_val_t value =
flow_array[i].protocol + flow_array[i].src_port + flow_array[i].dst_port;
test_map_cuckoo_insert(map, &(flow_array[i]), &value);
test_map_cuckoo_val_t *map_val = test_map_cuckoo_lookup(map, &(flow_array[i]));
if (!map_val || *map_val != value) {
return false;
}
}
return true;
}
static bool __always_inline test_case6(struct test_map_cuckoo_hash_map *map) {
struct flow_key flow3 = {
.protocol = 17, .src_ip = 0x10101010, .dst_ip = 0x10101013, .src_port = 2, .dst_port = 1};
struct flow_key flow4 = {
.protocol = 30, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 10, .dst_port = 10};
struct flow_key flow5 = {
.protocol = 30, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 1, .dst_port = 22};
struct flow_key flow6 = {
.protocol = 50, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 15, .dst_port = 10};
struct flow_key flow7 = {
.protocol = 70, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 10, .dst_port = 20};
struct flow_key flow8 = {
.protocol = 50, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 10, .dst_port = 7};
struct flow_key flow9 = {
.protocol = 100, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 2, .dst_port = 3};
struct flow_key flow10 = {
.protocol = 1, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 1, .dst_port = 1};
struct flow_key flow11 = {
.protocol = 30, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 3, .dst_port = 3};
struct flow_key flow12 = {
.protocol = 30, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 4, .dst_port = 5};
struct flow_key flow_array[] = {flow3, flow4, flow5, flow6, flow7,
flow8, flow9, flow10, flow11, flow12};
#if __clang_major__ < 15
#pragma unroll
#endif
for (int i = 0; i < (sizeof(flow_array) / sizeof(struct flow_key)); i++) {
test_map_cuckoo_val_t value =
flow_array[i].protocol + flow_array[i].src_port + flow_array[i].dst_port;
if (!test_map_cuckoo_insert(map, &(flow_array[i]), &value)) {
return false;
}
}
struct flow_key flow13 = {
.protocol = 100, .src_ip = 0x10101010, .dst_ip = 0x10101014, .src_port = 1, .dst_port = 2};
/* look up element that does not exist */
test_map_cuckoo_val_t *map_val = test_map_cuckoo_lookup(map, &flow13);
if (map_val == NULL) {
return true;
}
return false;
}
/* Test 9: Adds a large number of items to check the filter's handling of near-capacity situations.
*/
bool test_case7(struct test_map_cuckoo_hash_map *map) {
if (!map) {
return false;
}
struct flow_key flow1 = {
.protocol = 17, .src_ip = 0x10101010, .dst_ip = 0x10101011, .src_port = 1, .dst_port = 2};
int i;
int j;
bpf_for(i, 0, 1000000) {
flow1.src_ip = i;
flow1.dst_ip = i + 1;
test_map_cuckoo_val_t value = flow1.protocol + flow1.src_port + flow1.dst_port;
if (!test_map_cuckoo_insert(map, &(flow1), &value)) {
bpf_printk("Failed to insert item %d in hash table", i);
return false;
}
}
bpf_for(j, 0, 1000000) {
flow1.src_ip = j;
flow1.dst_ip = j + 1;
test_map_cuckoo_val_t *map_val = test_map_cuckoo_lookup(map, &flow1);
if (map_val == NULL) {
bpf_printk("Failed to find item %d in hash table", j);
return false;
}
if (*map_val != flow1.protocol + flow1.src_port + flow1.dst_port) {
bpf_printk("Found item %d in hash table but with wrong value", j);
return false;
}
}
return true;
}
SEC("xdp")
int xdp_cuckoo_test_prog(struct xdp_md *ctx) {
bpf_printk("xdp_cuckoo_test_prog");
uint32_t zero = 0;
struct test_map_cuckoo_hash_map *map = bpf_map_lookup_elem(&test_map, &zero);
bool ret = false;
if (!map) {
bpf_printk("map not found");
return XDP_DROP;
}
bpf_printk("Running test case %d", cuckoo_test_cfg.test_case);
switch (cuckoo_test_cfg.test_case) {
case 1:
ret = test_case1(map);
break;
case 2:
ret = test_case2(map);
break;
case 3:
ret = test_case3(map);
break;
case 4:
ret = test_case4(map);
break;
case 5:
ret = test_case5(map);
break;
case 6:
ret = test_case6(map);
break;
case 7:
ret = test_case7(map);
break;
default:
return XDP_ABORTED;
}
bpf_printk("Test case %d %s", cuckoo_test_cfg.test_case, ret ? "passed" : "failed");
if (ret) {
return XDP_PASS;
} else {
return XDP_DROP;
}
}
char LICENSE[] SEC("license") = "Dual BSD/GPL";