1 // SPDX-License-Identifier: GPL-2.0
2
3 #define _GNU_SOURCE
4 #include <test_progs.h>
5 #include <network_helpers.h>
6 #include <sys/stat.h>
7 #include <linux/sched.h>
8 #include <sys/syscall.h>
9
10 #include "test_pkt_md_access.skel.h"
11 #include "test_trace_ext.skel.h"
12 #include "test_trace_ext_tracing.skel.h"
13
14 static __u32 duration;
15
test_trace_ext(void)16 void test_trace_ext(void)
17 {
18 struct test_pkt_md_access *skel_pkt = NULL;
19 struct test_trace_ext_tracing *skel_trace = NULL;
20 struct test_trace_ext_tracing__bss *bss_trace;
21 struct test_trace_ext *skel_ext = NULL;
22 struct test_trace_ext__bss *bss_ext;
23 int err, pkt_fd, ext_fd;
24 struct bpf_program *prog;
25 char buf[100];
26 __u64 len;
27 LIBBPF_OPTS(bpf_test_run_opts, topts,
28 .data_in = &pkt_v4,
29 .data_size_in = sizeof(pkt_v4),
30 .repeat = 1,
31 );
32
33 /* open/load/attach test_pkt_md_access */
34 skel_pkt = test_pkt_md_access__open_and_load();
35 if (CHECK(!skel_pkt, "setup", "classifier/test_pkt_md_access open failed\n"))
36 goto cleanup;
37
38 err = test_pkt_md_access__attach(skel_pkt);
39 if (CHECK(err, "setup", "classifier/test_pkt_md_access attach failed: %d\n", err))
40 goto cleanup;
41
42 prog = skel_pkt->progs.test_pkt_md_access;
43 pkt_fd = bpf_program__fd(prog);
44
45 /* open extension */
46 skel_ext = test_trace_ext__open();
47 if (CHECK(!skel_ext, "setup", "freplace/test_pkt_md_access open failed\n"))
48 goto cleanup;
49
50 /* set extension's attach target - test_pkt_md_access */
51 prog = skel_ext->progs.test_pkt_md_access_new;
52 bpf_program__set_attach_target(prog, pkt_fd, "test_pkt_md_access");
53
54 /* load/attach extension */
55 err = test_trace_ext__load(skel_ext);
56 if (CHECK(err, "setup", "freplace/test_pkt_md_access load failed\n")) {
57 libbpf_strerror(err, buf, sizeof(buf));
58 fprintf(stderr, "%s\n", buf);
59 goto cleanup;
60 }
61
62 err = test_trace_ext__attach(skel_ext);
63 if (CHECK(err, "setup", "freplace/test_pkt_md_access attach failed: %d\n", err))
64 goto cleanup;
65
66 prog = skel_ext->progs.test_pkt_md_access_new;
67 ext_fd = bpf_program__fd(prog);
68
69 /* open tracing */
70 skel_trace = test_trace_ext_tracing__open();
71 if (CHECK(!skel_trace, "setup", "tracing/test_pkt_md_access_new open failed\n"))
72 goto cleanup;
73
74 /* set tracing's attach target - fentry */
75 prog = skel_trace->progs.fentry;
76 bpf_program__set_attach_target(prog, ext_fd, "test_pkt_md_access_new");
77
78 /* set tracing's attach target - fexit */
79 prog = skel_trace->progs.fexit;
80 bpf_program__set_attach_target(prog, ext_fd, "test_pkt_md_access_new");
81
82 /* load/attach tracing */
83 err = test_trace_ext_tracing__load(skel_trace);
84 if (!ASSERT_OK(err, "tracing/test_pkt_md_access_new load")) {
85 libbpf_strerror(err, buf, sizeof(buf));
86 fprintf(stderr, "%s\n", buf);
87 goto cleanup;
88 }
89
90 err = test_trace_ext_tracing__attach(skel_trace);
91 if (!ASSERT_OK(err, "tracing/test_pkt_md_access_new attach"))
92 goto cleanup;
93
94 /* trigger the test */
95 err = bpf_prog_test_run_opts(pkt_fd, &topts);
96 ASSERT_OK(err, "test_run_opts err");
97 ASSERT_OK(topts.retval, "test_run_opts retval");
98
99 bss_ext = skel_ext->bss;
100 bss_trace = skel_trace->bss;
101
102 len = bss_ext->ext_called;
103
104 ASSERT_NEQ(bss_ext->ext_called, 0,
105 "failed to trigger freplace/test_pkt_md_access");
106 ASSERT_EQ(bss_trace->fentry_called, len,
107 "failed to trigger fentry/test_pkt_md_access_new");
108 ASSERT_EQ(bss_trace->fexit_called, len,
109 "failed to trigger fexit/test_pkt_md_access_new");
110
111 cleanup:
112 test_trace_ext_tracing__destroy(skel_trace);
113 test_trace_ext__destroy(skel_ext);
114 test_pkt_md_access__destroy(skel_pkt);
115 }
116