Skip to content

Commit cb52425

Browse files
committed
selftests/bpf: Add tracing multi intersect attach tests
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
1 parent ba6f824 commit cb52425

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c
481481
LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \
482482
linked_vars.skel.h linked_maps.skel.h \
483483
test_subskeleton.skel.h test_subskeleton_lib.skel.h \
484-
test_usdt.skel.h tracing_multi.skel.h
484+
test_usdt.skel.h tracing_multi.skel.h \
485+
tracing_multi_intersect.skel.h
485486

486487
LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c \
487488
core_kern.c core_kern_overflow.c test_ringbuf.c \
@@ -508,6 +509,7 @@ xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o
508509
xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
509510
xdp_features.skel.h-deps := xdp_features.bpf.o
510511
tracing_multi.skel.h-deps := tracing_multi_attach.bpf.o tracing_multi_check.bpf.o
512+
tracing_multi_intersect.skel.h-deps := tracing_multi_intersect_attach.bpf.o tracing_multi_check.bpf.o
511513

512514
LINKED_BPF_OBJS := $(foreach skel,$(LINKED_SKELS),$($(skel)-deps))
513515
LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(LINKED_BPF_OBJS))

tools/testing/selftests/bpf/prog_tests/tracing_multi.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <search.h>
66
#include "bpf/libbpf_internal.h"
77
#include "tracing_multi.skel.h"
8+
#include "tracing_multi_intersect.skel.h"
89
#include "trace_helpers.h"
910

1011
static const char *bpf_fentry_test[] = {
@@ -18,6 +19,20 @@ static const char *bpf_fentry_test[] = {
1819
"bpf_fentry_test8",
1920
};
2021

22+
static int get_random_funcs(const char **funcs)
23+
{
24+
int i, cnt = 0;
25+
26+
for (i = 0; i < 8; i++) {
27+
if (rand() % 2)
28+
funcs[cnt++] = bpf_fentry_test[i];
29+
}
30+
/* we always need at least one.. */
31+
if (!cnt)
32+
funcs[cnt++] = bpf_fentry_test[rand() % 8];
33+
return cnt;
34+
}
35+
2136
static int compare(const void *pa, const void *pb)
2237
{
2338
return strcmp((char *) pa, (char *) pb);
@@ -167,6 +182,82 @@ static void test_link_api_ids(void)
167182
tracing_multi__destroy(skel);
168183
}
169184

185+
static bool is_set(__u32 mask, __u32 bit)
186+
{
187+
return (1 << bit) & mask;
188+
}
189+
190+
static void __test_intersect(__u32 mask, const struct bpf_program *progs[4], __u64 *test_results[4])
191+
{
192+
LIBBPF_OPTS(bpf_tracing_multi_opts, opts);
193+
LIBBPF_OPTS(bpf_test_run_opts, topts);
194+
struct bpf_link *links[4] = {};
195+
const char *funcs[8];
196+
__u64 expected[4];
197+
__u32 *ids, i;
198+
int err, cnt;
199+
200+
for (i = 0; i < 4; i++) {
201+
if (!is_set(mask, i))
202+
continue;
203+
204+
cnt = get_random_funcs(funcs);
205+
ids = get_ids(funcs, cnt);
206+
if (!ASSERT_OK_PTR(ids, "get_ids"))
207+
goto cleanup;
208+
209+
opts.ids = ids;
210+
opts.cnt = cnt;
211+
links[i] = bpf_program__attach_tracing_multi(progs[i], NULL, &opts);
212+
free(ids);
213+
214+
if (!ASSERT_OK_PTR(links[i], "bpf_program__attach_tracing_multi"))
215+
goto cleanup;
216+
217+
expected[i] = *test_results[i] + cnt;
218+
}
219+
220+
err = bpf_prog_test_run_opts(bpf_program__fd(progs[0]), &topts);
221+
ASSERT_OK(err, "test_run");
222+
223+
for (i = 0; i < 4; i++) {
224+
if (!is_set(mask, i))
225+
continue;
226+
ASSERT_EQ(*test_results[i], expected[i], "test_results");
227+
}
228+
229+
cleanup:
230+
for (i = 0; i < 4; i++)
231+
bpf_link__destroy(links[i]);
232+
}
233+
234+
static void test_intersect(void)
235+
{
236+
const struct bpf_program *progs[4];
237+
struct tracing_multi_intersect *skel;
238+
__u64 *test_results[4];
239+
__u32 i;
240+
241+
skel = tracing_multi_intersect__open_and_load();
242+
if (!ASSERT_OK_PTR(skel, "tracing_multi_intersect__open_and_load"))
243+
return;
244+
245+
progs[0] = skel->progs.fentry_1;
246+
progs[1] = skel->progs.fexit_1;
247+
progs[2] = skel->progs.fentry_2;
248+
progs[3] = skel->progs.fexit_2;
249+
250+
test_results[0] = &skel->bss->test_result_fentry_1;
251+
test_results[1] = &skel->bss->test_result_fexit_1;
252+
test_results[2] = &skel->bss->test_result_fentry_2;
253+
test_results[3] = &skel->bss->test_result_fexit_2;
254+
255+
for (i = 1; i < 16; i++)
256+
__test_intersect(i, progs, test_results);
257+
258+
tracing_multi_intersect__destroy(skel);
259+
}
260+
170261
void test_tracing_multi_test(void)
171262
{
172263
#ifndef __x86_64__
@@ -180,4 +271,6 @@ void test_tracing_multi_test(void)
180271
test_link_api_pattern();
181272
if (test__start_subtest("link_api_ids"))
182273
test_link_api_ids();
274+
if (test__start_subtest("intersect"))
275+
test_intersect();
183276
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stdbool.h>
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
__hidden extern int tracing_multi_arg_check(__u64 *ctx, __u64 *test_result, bool is_return);
10+
11+
__u64 test_result_fentry_1 = 0;
12+
__u64 test_result_fentry_2 = 0;
13+
__u64 test_result_fexit_1 = 0;
14+
__u64 test_result_fexit_2 = 0;
15+
16+
SEC("fentry.multi")
17+
int BPF_PROG(fentry_1)
18+
{
19+
tracing_multi_arg_check(ctx, &test_result_fentry_1, false);
20+
return 0;
21+
}
22+
23+
SEC("fentry.multi")
24+
int BPF_PROG(fentry_2)
25+
{
26+
tracing_multi_arg_check(ctx, &test_result_fentry_2, false);
27+
return 0;
28+
}
29+
30+
SEC("fexit.multi")
31+
int BPF_PROG(fexit_1)
32+
{
33+
tracing_multi_arg_check(ctx, &test_result_fexit_1, true);
34+
return 0;
35+
}
36+
37+
SEC("fexit.multi")
38+
int BPF_PROG(fexit_2)
39+
{
40+
tracing_multi_arg_check(ctx, &test_result_fexit_2, true);
41+
return 0;
42+
}

0 commit comments

Comments
 (0)