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
|
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include <linux/if_ether.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/in6.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <sys/syscall.h>
#include <bpf/bpf.h>
#include "bpf_gotox.skel.h"
static void __test_run(struct bpf_program *prog, void *ctx_in, size_t ctx_size_in)
{
LIBBPF_OPTS(bpf_test_run_opts, topts,
.ctx_in = ctx_in,
.ctx_size_in = ctx_size_in,
);
int err, prog_fd;
prog_fd = bpf_program__fd(prog);
err = bpf_prog_test_run_opts(prog_fd, &topts);
ASSERT_OK(err, "test_run_opts err");
}
static void __subtest(struct bpf_gotox *skel, void (*check)(struct bpf_gotox *))
{
if (skel->data->skip)
test__skip();
else
check(skel);
}
static void check_simple(struct bpf_gotox *skel,
struct bpf_program *prog,
__u64 ctx_in,
__u64 expected)
{
skel->bss->ret_user = 0;
__test_run(prog, &ctx_in, sizeof(ctx_in));
if (!ASSERT_EQ(skel->bss->ret_user, expected, "skel->bss->ret_user"))
return;
}
static void check_simple_fentry(struct bpf_gotox *skel,
struct bpf_program *prog,
__u64 ctx_in,
__u64 expected)
{
skel->bss->in_user = ctx_in;
skel->bss->ret_user = 0;
/* trigger */
usleep(1);
if (!ASSERT_EQ(skel->bss->ret_user, expected, "skel->bss->ret_user"))
return;
}
/* validate that for two loads of the same jump table libbpf generates only one map */
static void check_one_map_two_jumps(struct bpf_gotox *skel)
{
struct bpf_prog_info prog_info;
struct bpf_map_info map_info;
__u32 len;
__u32 map_ids[16];
int prog_fd, map_fd;
int ret;
int i;
bool seen = false;
memset(&prog_info, 0, sizeof(prog_info));
prog_info.map_ids = (long)map_ids;
prog_info.nr_map_ids = ARRAY_SIZE(map_ids);
prog_fd = bpf_program__fd(skel->progs.one_map_two_jumps);
if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd(one_map_two_jumps)"))
return;
len = sizeof(prog_info);
ret = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &len);
if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd(prog_fd)"))
return;
for (i = 0; i < prog_info.nr_map_ids; i++) {
map_fd = bpf_map_get_fd_by_id(map_ids[i]);
if (!ASSERT_GE(map_fd, 0, "bpf_map_get_fd_by_id"))
return;
len = sizeof(map_info);
memset(&map_info, 0, len);
ret = bpf_obj_get_info_by_fd(map_fd, &map_info, &len);
if (!ASSERT_OK(ret, "bpf_obj_get_info_by_fd(map_fd)")) {
close(map_fd);
return;
}
if (map_info.type == BPF_MAP_TYPE_INSN_ARRAY) {
if (!ASSERT_EQ(seen, false, "more than one INSN_ARRAY map")) {
close(map_fd);
return;
}
seen = true;
}
close(map_fd);
}
ASSERT_EQ(seen, true, "no INSN_ARRAY map");
}
static void check_one_switch(struct bpf_gotox *skel)
{
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
int i;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.one_switch, in[i], out[i]);
}
static void check_one_switch_non_zero_sec_off(struct bpf_gotox *skel)
{
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
int i;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.one_switch_non_zero_sec_off, in[i], out[i]);
}
static void check_two_switches(struct bpf_gotox *skel)
{
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {103, 104, 107, 205, 115, 1019, 1019};
int i;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.two_switches, in[i], out[i]);
}
static void check_big_jump_table(struct bpf_gotox *skel)
{
__u64 in[] = {0, 11, 27, 31, 22, 45, 99};
__u64 out[] = {2, 3, 4, 5, 19, 19, 19};
int i;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.big_jump_table, in[i], out[i]);
}
static void check_one_jump_two_maps(struct bpf_gotox *skel)
{
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {12, 15, 7 , 15, 12, 15, 15};
int i;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.one_jump_two_maps, in[i], out[i]);
}
static void check_static_global(struct bpf_gotox *skel)
{
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
int i;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.use_static_global1, in[i], out[i]);
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.use_static_global2, in[i], out[i]);
}
static void check_nonstatic_global(struct bpf_gotox *skel)
{
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
int i;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.use_nonstatic_global1, in[i], out[i]);
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple(skel, skel->progs.use_nonstatic_global2, in[i], out[i]);
}
static void check_other_sec(struct bpf_gotox *skel)
{
struct bpf_link *link;
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
int i;
link = bpf_program__attach(skel->progs.simple_test_other_sec);
if (!ASSERT_OK_PTR(link, "link"))
return;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple_fentry(skel, skel->progs.simple_test_other_sec, in[i], out[i]);
bpf_link__destroy(link);
}
static void check_static_global_other_sec(struct bpf_gotox *skel)
{
struct bpf_link *link;
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
int i;
link = bpf_program__attach(skel->progs.use_static_global_other_sec);
if (!ASSERT_OK_PTR(link, "link"))
return;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple_fentry(skel, skel->progs.use_static_global_other_sec, in[i], out[i]);
bpf_link__destroy(link);
}
static void check_nonstatic_global_other_sec(struct bpf_gotox *skel)
{
struct bpf_link *link;
__u64 in[] = {0, 1, 2, 3, 4, 5, 77};
__u64 out[] = {2, 3, 4, 5, 7, 19, 19};
int i;
link = bpf_program__attach(skel->progs.use_nonstatic_global_other_sec);
if (!ASSERT_OK_PTR(link, "link"))
return;
for (i = 0; i < ARRAY_SIZE(in); i++)
check_simple_fentry(skel, skel->progs.use_nonstatic_global_other_sec, in[i], out[i]);
bpf_link__destroy(link);
}
void test_bpf_gotox(void)
{
struct bpf_gotox *skel;
int ret;
skel = bpf_gotox__open();
if (!ASSERT_NEQ(skel, NULL, "bpf_gotox__open"))
return;
ret = bpf_gotox__load(skel);
if (!ASSERT_OK(ret, "bpf_gotox__load"))
return;
skel->bss->pid = getpid();
if (test__start_subtest("one-switch"))
__subtest(skel, check_one_switch);
if (test__start_subtest("one-switch-non-zero-sec-offset"))
__subtest(skel, check_one_switch_non_zero_sec_off);
if (test__start_subtest("two-switches"))
__subtest(skel, check_two_switches);
if (test__start_subtest("big-jump-table"))
__subtest(skel, check_big_jump_table);
if (test__start_subtest("static-global"))
__subtest(skel, check_static_global);
if (test__start_subtest("nonstatic-global"))
__subtest(skel, check_nonstatic_global);
if (test__start_subtest("other-sec"))
__subtest(skel, check_other_sec);
if (test__start_subtest("static-global-other-sec"))
__subtest(skel, check_static_global_other_sec);
if (test__start_subtest("nonstatic-global-other-sec"))
__subtest(skel, check_nonstatic_global_other_sec);
if (test__start_subtest("one-jump-two-maps"))
__subtest(skel, check_one_jump_two_maps);
if (test__start_subtest("one-map-two-jumps"))
__subtest(skel, check_one_map_two_jumps);
bpf_gotox__destroy(skel);
}
|