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 
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 	__u32 retval;
27 	__u64 len;
28 
29 	/* open/load/attach test_pkt_md_access */
30 	skel_pkt = test_pkt_md_access__open_and_load();
31 	if (CHECK(!skel_pkt, "setup", "classifier/test_pkt_md_access open failed\n"))
32 		goto cleanup;
33 
34 	err = test_pkt_md_access__attach(skel_pkt);
35 	if (CHECK(err, "setup", "classifier/test_pkt_md_access attach failed: %d\n", err))
36 		goto cleanup;
37 
38 	prog = skel_pkt->progs.test_pkt_md_access;
39 	pkt_fd = bpf_program__fd(prog);
40 
41 	/* open extension */
42 	skel_ext = test_trace_ext__open();
43 	if (CHECK(!skel_ext, "setup", "freplace/test_pkt_md_access open failed\n"))
44 		goto cleanup;
45 
46 	/* set extension's attach target - test_pkt_md_access  */
47 	prog = skel_ext->progs.test_pkt_md_access_new;
48 	bpf_program__set_attach_target(prog, pkt_fd, "test_pkt_md_access");
49 
50 	/* load/attach extension */
51 	err = test_trace_ext__load(skel_ext);
52 	if (CHECK(err, "setup", "freplace/test_pkt_md_access load failed\n")) {
53 		libbpf_strerror(err, buf, sizeof(buf));
54 		fprintf(stderr, "%s\n", buf);
55 		goto cleanup;
56 	}
57 
58 	err = test_trace_ext__attach(skel_ext);
59 	if (CHECK(err, "setup", "freplace/test_pkt_md_access attach failed: %d\n", err))
60 		goto cleanup;
61 
62 	prog = skel_ext->progs.test_pkt_md_access_new;
63 	ext_fd = bpf_program__fd(prog);
64 
65 	/* open tracing  */
66 	skel_trace = test_trace_ext_tracing__open();
67 	if (CHECK(!skel_trace, "setup", "tracing/test_pkt_md_access_new open failed\n"))
68 		goto cleanup;
69 
70 	/* set tracing's attach target - fentry */
71 	prog = skel_trace->progs.fentry;
72 	bpf_program__set_attach_target(prog, ext_fd, "test_pkt_md_access_new");
73 
74 	/* set tracing's attach target - fexit */
75 	prog = skel_trace->progs.fexit;
76 	bpf_program__set_attach_target(prog, ext_fd, "test_pkt_md_access_new");
77 
78 	/* load/attach tracing */
79 	err = test_trace_ext_tracing__load(skel_trace);
80 	if (CHECK(err, "setup", "tracing/test_pkt_md_access_new load failed\n")) {
81 		libbpf_strerror(err, buf, sizeof(buf));
82 		fprintf(stderr, "%s\n", buf);
83 		goto cleanup;
84 	}
85 
86 	err = test_trace_ext_tracing__attach(skel_trace);
87 	if (CHECK(err, "setup", "tracing/test_pkt_md_access_new attach failed: %d\n", err))
88 		goto cleanup;
89 
90 	/* trigger the test */
91 	err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
92 				NULL, NULL, &retval, &duration);
93 	CHECK(err || retval, "run", "err %d errno %d retval %d\n", err, errno, retval);
94 
95 	bss_ext = skel_ext->bss;
96 	bss_trace = skel_trace->bss;
97 
98 	len = bss_ext->ext_called;
99 
100 	CHECK(bss_ext->ext_called == 0,
101 		"check", "failed to trigger freplace/test_pkt_md_access\n");
102 	CHECK(bss_trace->fentry_called != len,
103 		"check", "failed to trigger fentry/test_pkt_md_access_new\n");
104 	CHECK(bss_trace->fexit_called != len,
105 		"check", "failed to trigger fexit/test_pkt_md_access_new\n");
106 
107 cleanup:
108 	test_trace_ext_tracing__destroy(skel_trace);
109 	test_trace_ext__destroy(skel_ext);
110 	test_pkt_md_access__destroy(skel_pkt);
111 }
112