|
| 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_trace_ext.skel.h" |
| 11 | +#include "test_trace_ext_tracing.skel.h" |
| 12 | + |
| 13 | +static __u32 duration; |
| 14 | + |
| 15 | +void test_trace_ext(void) |
| 16 | +{ |
| 17 | + struct test_trace_ext_tracing *skel_trace = NULL; |
| 18 | + struct test_trace_ext_tracing__bss *bss_trace; |
| 19 | + const char *file = "./test_pkt_md_access.o"; |
| 20 | + struct test_trace_ext *skel_ext = NULL; |
| 21 | + struct test_trace_ext__bss *bss_ext; |
| 22 | + int err, prog_fd, ext_fd; |
| 23 | + struct bpf_object *obj; |
| 24 | + char buf[100]; |
| 25 | + __u32 retval; |
| 26 | + __u64 len; |
| 27 | + |
| 28 | + err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); |
| 29 | + if (CHECK_FAIL(err)) |
| 30 | + return; |
| 31 | + |
| 32 | + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, |
| 33 | + .attach_prog_fd = prog_fd, |
| 34 | + ); |
| 35 | + |
| 36 | + skel_ext = test_trace_ext__open_opts(&opts); |
| 37 | + if (CHECK(!skel_ext, "setup", "freplace/test_pkt_md_access open failed\n")) |
| 38 | + goto cleanup; |
| 39 | + |
| 40 | + err = test_trace_ext__load(skel_ext); |
| 41 | + if (CHECK(err, "setup", "freplace/test_pkt_md_access load failed\n")) { |
| 42 | + libbpf_strerror(err, buf, sizeof(buf)); |
| 43 | + fprintf(stderr, "%s\n", buf); |
| 44 | + goto cleanup; |
| 45 | + } |
| 46 | + |
| 47 | + err = test_trace_ext__attach(skel_ext); |
| 48 | + if (CHECK(err, "setup", "freplace/test_pkt_md_access attach failed: %d\n", err)) |
| 49 | + goto cleanup; |
| 50 | + |
| 51 | + ext_fd = bpf_program__fd(skel_ext->progs.test_pkt_md_access_new); |
| 52 | + |
| 53 | + DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts_trace, |
| 54 | + .attach_prog_fd = ext_fd, |
| 55 | + ); |
| 56 | + |
| 57 | + skel_trace = test_trace_ext_tracing__open_opts(&opts_trace); |
| 58 | + if (CHECK(!skel_trace, "setup", "tracing/test_pkt_md_access_new open failed\n")) |
| 59 | + goto cleanup; |
| 60 | + |
| 61 | + err = test_trace_ext_tracing__load(skel_trace); |
| 62 | + if (CHECK(err, "setup", "tracing/test_pkt_md_access_new load failed\n")) { |
| 63 | + libbpf_strerror(err, buf, sizeof(buf)); |
| 64 | + fprintf(stderr, "%s\n", buf); |
| 65 | + goto cleanup; |
| 66 | + } |
| 67 | + |
| 68 | + err = test_trace_ext_tracing__attach(skel_trace); |
| 69 | + if (CHECK(err, "setup", "tracing/test_pkt_md_access_new attach failed: %d\n", err)) |
| 70 | + goto cleanup; |
| 71 | + |
| 72 | + err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), |
| 73 | + NULL, NULL, &retval, &duration); |
| 74 | + CHECK(err || retval, "", |
| 75 | + "err %d errno %d retval %d duration %d\n", |
| 76 | + err, errno, retval, duration); |
| 77 | + |
| 78 | + bss_ext = skel_ext->bss; |
| 79 | + bss_trace = skel_trace->bss; |
| 80 | + |
| 81 | + len = bss_ext->ext_called; |
| 82 | + |
| 83 | + CHECK(bss_ext->ext_called == 0, |
| 84 | + "check", "failed to trigger freplace/test_pkt_md_access\n"); |
| 85 | + CHECK(bss_trace->fentry_called != len, |
| 86 | + "check", "failed to trigger fentry/test_pkt_md_access_new\n"); |
| 87 | + CHECK(bss_trace->fexit_called != len, |
| 88 | + "check", "failed to trigger fexit/test_pkt_md_access_new\n"); |
| 89 | + |
| 90 | +cleanup: |
| 91 | + test_trace_ext__destroy(skel_ext); |
| 92 | + bpf_object__close(obj); |
| 93 | +} |
0 commit comments