selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program
Subsystems (6)
PATCH 1/1 selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program
Add a test that will attach a FENTRY and FEXIT program to the XDP test program. It will also verify data from the XDP context on FENTRY and verifies the return code on exit.
--- .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c | 95 ++++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c | 44 +++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +95 -0
@@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include <net/if.h> + +#define PROG_CNT 2 + +void test_xdp_bpf2bpf (void ) +{ +const char *prog_name[PROG_CNT ] = { +"fentry/_xdp_tx_iptunnel" , +"fexit/_xdp_tx_iptunnel" + }; +struct bpf_link *link[PROG_CNT ] = {}; +struct bpf_program *prog[PROG_CNT ]; +struct bpf_map *data_map; +const int zero =0 ; +u64 result[PROG_CNT ]; +struct vip key4 = {.protocol =6 , .family =AF_INET }; +struct iptnl_info value4 = {.family =AF_INET }; +const char *file ="./test_xdp.o" ; +struct bpf_object *obj, *tracer_obj =NULL ; +char buf[128 ]; +struct iphdr *iph = (void *)buf +sizeof (struct ethhdr ); +__u32 duration, retval, size; +int err, prog_fd, map_fd; + +/* Load XDP program to introspect */ + err =bpf_prog_load (file,BPF_PROG_TYPE_XDP , &obj, &prog_fd); +if (CHECK_FAIL (err)) +return ; + + map_fd =bpf_find_map (__func__, obj,"vip2tnl" ); +if (map_fd <0 ) + goto out; +bpf_map_update_elem (map_fd, &key4, &value4,0 ); + +/* Load eBPF trace program */ +DECLARE_LIBBPF_OPTS (bpf_object_open_opts, opts, + .attach_prog_fd = prog_fd, + ); + + tracer_obj =bpf_object__open_file ("./test_xdp_bpf2bpf.o" , &opts); +if (CHECK (IS_ERR_OR_NULL (tracer_obj),"obj_open" , +"failed to open test_xdp_bpf2bpf: %ld\n" , +PTR_ERR (tracer_obj))) + goto out; + + err =bpf_object__load (tracer_obj); +if (CHECK (err,"obj_load" ,"err %d\n" , err)) + goto out; + +for (int i =0 ; i <PROG_CNT ; i++) { + prog[i] =bpf_object__find_program_by_title (tracer_obj, + prog_name[i]); +if (CHECK (!prog[i],"find_prog" ,"prog %s not found\n" , + prog_name[i])) + goto out; + link[i] =bpf_program__attach_trace (prog[i]); +if (CHECK (IS_ERR (link[i]),"attach_trace" ,"failed to link\n" )) + goto out; + } + data_map =bpf_object__find_map_by_name (tracer_obj,"test_xdp.bss" ); +if (CHECK (!data_map,"find_data_map" ,"data map not found\n" )) + goto out; + +/* Run test program */ + err =bpf_prog_test_run (prog_fd,1 , &pkt_v4,sizeof (pkt_v4), + buf, &size, &retval, &duration); + +CHECK (err || retval !=XDP_TX || size !=74 || + iph->protocol !=IPPROTO_IPIP ,"ipv4" , +"err %d errno %d retval %d size %d\n" , + err, errno, retval, size); + +/* Verify test results */ + err =bpf_map_lookup_elem (bpf_map__fd (data_map), &zero, &result); +if (CHECK (err,"get_result" , +"failed to get output data: %d\n" , err)) + goto out; + +if (CHECK (result[0 ] !=if_nametoindex ("lo" ), +"result" ,"%s failed err %ld\n" , prog_name[0 ], result[0 ])) + goto out; + +if (CHECK (result[1 ] !=XDP_TX ,"result" ,"%s failed err %ld\n" , + prog_name[1 ], result[1 ])) + goto out; +out: +for (int i =0 ; i <PROG_CNT ; i++) +if (!IS_ERR_OR_NULL (link[i])) +bpf_link__destroy (link[i]); +if (!IS_ERR_OR_NULL (tracer_obj)) +bpf_object__close (tracer_obj); +bpf_object__close (obj); +}
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c new file +44 -0
44 lines not on this page · raw message
Quoted 13 lines · Eelco Chaudron
Add a test that will attach a FENTRY and FEXIT program to the XDP test program. It will also verify data from the XDP context on FENTRY and verifies the return code on exit.
Signed-off-by Eelco Chaudron <[email protected]>--- .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c | 95 ++++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c | 44 +++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
[...]
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c line 28
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +2
-0+/* Load XDP program to introspect */ + err =bpf_prog_load (file,BPF_PROG_TYPE_XDP , &obj, &prog_fd);
Please use BPF skeleton for this test. It will make it significantly shorter and clearer. See other fentry_fexit selftest for example.
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +3
-0+if (CHECK_FAIL (err)) +return ; +
[...]
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static volatile __u64 test_result_fentry;
no need for static volatile anymore, just use global var
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c line 38
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c new file +8
-0+BPF_TRACE_1 ("fentry/_xdp_tx_iptunnel" , trace_on_entry, + struct xdp_buff *, xdp) +{ + test_result_fentry = xdp->rxq->dev->ifindex; +return 0 ; +} + +static volatile __u64 test_result_fexit;
same here
Quoted 7 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c new file +6
-0+BPF_TRACE_2 ("fexit/_xdp_tx_iptunnel" , trace_on_exit, + struct xdp_buff*, xdp, int, ret) +{ + test_result_fexit = ret; +return 0 ; +}
Quoted 18 lines · Andrii Nakryiko
Quoted 14 lines · Eelco Chaudron
Add a test that will attach a FENTRY and FEXIT program to the XDP test program. It will also verify data from the XDP context on FENTRY and verifies the return code on exit.
Signed-off-by Eelco Chaudron <[email protected]>--- .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c | 95 ++++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c | 44 +++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c create mode 100644Quoted 2 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
Quoted 1 line
[...]
Thanks for the review, updated my kernel to the latest bfp-next, but now I get the following build issue for the test suite:
GEN-SKEL [test_progs] loop3.skel.h
GEN-SKEL [test_progs] test_skeleton.skel.h
libbpf: failed to find BTF for extern 'CONFIG_BPF_SYSCALL': -2
Error: failed to open BPF object file: 0
make: *** [Makefile:333: /data/linux_kernel/tools/testing/selftests/bpf/test_skeleton.skel.h] Error 255
make: *** Deleting file '/data/linux_kernel/tools/testing/selftests/bpf/test_skeleton.skel.h'
Verified, and I still have all the correct config and CLANG version. Something else I need to update? I have pahole v1.15 in my search path…
Quoted 36 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c line 28
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +2
-0+/* Load XDP program to introspect */ + err =bpf_prog_load (file,BPF_PROG_TYPE_XDP , &obj, &prog_fd);Please use BPF skeleton for this test. It will make it significantly shorter and clearer. See other
fentry_fexitselftest for example.Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +3
-0+if (CHECK_FAIL (err)) +return ; +[...]
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static volatile __u64 test_result_fentry;no need for static volatile anymore, just use global var
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c line 38
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c new file +8
-0+BPF_TRACE_1 ("fentry/_xdp_tx_iptunnel" , trace_on_entry, + struct xdp_buff *, xdp) +{ + test_result_fentry = xdp->rxq->dev->ifindex; +return 0 ; +} + +static volatile __u64 test_result_fexit;same here
Quoted 7 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c new file +6
-0+BPF_TRACE_2 ("fexit/_xdp_tx_iptunnel" , trace_on_exit, + struct xdp_buff*, xdp, int, ret) +{ + test_result_fexit = ret; +return 0 ; +}
Quoted 25 lines · Eelco Chaudron
Quoted 20 lines · Andrii Nakryiko
Quoted 17 lines · Eelco Chaudron
Add a test that will attach a FENTRY and FEXIT program to the XDP test
program. It will also verify data from the XDP context on FENTRY and verifies the return code on exit.Signed-off-by Eelco Chaudron <[email protected]>--- .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c | 95 ++++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c | 44 +++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c create mode 100644
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
Quoted 8 lines
Quoted 1 line
[...]
Thanks for the review, updated my kernel to the latest bfp-next, but now I get the following build issue for the test suite:
GEN-SKEL [
test_progs]loop3.skel.h
GEN-SKEL [test_progs]test_skeleton.skel.h
libbpf: failed to find BTF for extern 'CONFIG_BPF_SYSCALL': -2
This looks like you have Clang without BTF types for extern. Can you try pull the very latest Clang/LLVM and try again? The latest commit you should have is e3d8ee35e4ad ("reland "[DebugInfo] Support to emit debugInfo for extern variables"").
Quoted 48 lines
Error: failed to open BPF object file: 0
make: *** [Makefile:333:
/data/linux_kernel/tools/testing/selftests/bpf/test_skeleton.skel.h] Error 255
make: *** Deleting file '/data/linux_kernel/tools/testing/selftests/bpf/test_skeleton.skel.h'Verified, and I still have all the correct config and CLANG version. Something else I need to update?
I have pahole v1.15 in my search path…Quoted 36 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c line 28
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +2
-0+/* Load XDP program to introspect */ + err =bpf_prog_load (file,BPF_PROG_TYPE_XDP , &obj, &prog_fd);Please use BPF skeleton for this test. It will make it significantly shorter and clearer. See other
fentry_fexitselftest for example.Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +3
-0+if (CHECK_FAIL (err)) +return ; +[...]
Review
Quoted 2 lines
(quoted diff) +2
-0+ +static volatile __u64 test_result_fentry;no need for static volatile anymore, just use global var
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c line 38
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c new file +8
-0+BPF_TRACE_1 ("fentry/_xdp_tx_iptunnel" , trace_on_entry, + struct xdp_buff *, xdp) +{ + test_result_fentry = xdp->rxq->dev->ifindex; +return 0 ; +} + +static volatile __u64 test_result_fexit;same here
Quoted 7 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c new file +6
-0+BPF_TRACE_2 ("fexit/_xdp_tx_iptunnel" , trace_on_exit, + struct xdp_buff*, xdp, int, ret) +{ + test_result_fexit = ret; +return 0 ; +}
Quoted 1 line · Andrii Nakryiko
On Mon, Dec 23, 2019 at 4:54 AM Eelco Chaudron <[email protected]> wrote:
<SNIP<
Quoted 4 lines
Quoted 4 lines
Thanks for the review, updated my kernel to the latest bfp-next, but now I get the following build issue for the test suite:
Quoted 9 lines
Quoted 3 lines
GEN-SKEL [
test_progs]loop3.skel.h
GEN-SKEL [test_progs]test_skeleton.skel.h
libbpf: failed to find BTF for extern 'CONFIG_BPF_SYSCALL': -2This looks like you have Clang without BTF types for extern. Can you try pull the very latest Clang/LLVM and try again? The latest commit you should have is
e3d8ee35e4ad("reland "[DebugInfo] Support to emit debugInfo for extern variables"").
Thanks, rebuilding the latest master LLVM fixed the problem!
//Eelco
Quoted 18 lines · Andrii Nakryiko
Quoted 14 lines · Eelco Chaudron
Add a test that will attach a FENTRY and FEXIT program to the XDP test program. It will also verify data from the XDP context on FENTRY and verifies the return code on exit.
Signed-off-by Eelco Chaudron <[email protected]>--- .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c | 95 ++++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c | 44 +++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c create mode 100644Quoted 2 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
Quoted 8 lines
[...]
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c line 28
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +2
-0+/* Load XDP program to introspect */ + err =bpf_prog_load (file,BPF_PROG_TYPE_XDP , &obj, &prog_fd);Please use BPF skeleton for this test. It will make it significantly shorter and clearer. See other
fentry_fexitselftest for example.
Trying to do this, however, I’m getting the following when trying to execute the test:
test_xdp_bpf2bpf:PASS:pkt_skel_load 0 nsec
libbpf: fentry/_xdp_tx_iptunnel is not found in vmlinux BTF
libbpf: failed to load object 'test_xdp_bpf2bpf'
libbpf: failed to load BPF skeleton 'test_xdp_bpf2bpf': -2 test_xdp_bpf2bpf:FAIL:ftrace_skel_load ftrace skeleton failed
My program is straight forward following the fentry_fexit.c example:
pkt_skel = test_xdp__open_and_load();
if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton
failed\n"))
return;
map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl);
bpf_map_update_elem(map_fd, &key4, &value4, 0);
/* Load eBPF trace program */
ftrace_skel = test_xdp_bpf2bpf__open_and_load();
if (CHECK(!ftrace_skel, "ftrace_skel_load", "ftrace skeleton
failed\n"))
goto out;I assume this is due to the missing link from the XDP program to the eBPF trace program. Previously I did this trough:
+ DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
+ .attach_prog_fd = prog_fd,
+ );
+
+ tracer_obj = bpf_object__open_file("./test_xdp_bpf2bpf.o", &opts);
If I use this approach as before it works, i.e.:
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
.attach_prog_fd = pkt_fd,
);
ftrace_skel = test_xdp_bpf2bpf__open_opts(&opts); if (CHECK(!ftrace_skel, "__open_opts”, "ftrace skeleton
failed\n"))
goto out;
if (CHECK(test_xdp_bpf2bpf__load(ftrace_skel), "__load",
"ftrace skeleton failed\n"))
goto out;But I do not see this in the fentry_fexit.c example, guess I might be missing something that is right in front of me :(
[...]
Quoted 60 lines · Eelco Chaudron
Quoted 20 lines · Andrii Nakryiko
Quoted 15 lines · Eelco Chaudron
Add a test that will attach a FENTRY and FEXIT program to the XDP test
program. It will also verify data from the XDP context on FENTRY and verifies the return code on exit.Signed-off-by Eelco Chaudron <[email protected]>--- .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c | 95 ++++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c | 44 +++++++++ 2 files changed, 139 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c create mode 100644Quoted 2 lines
tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.cQuoted 8 lines
[...]
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c line 28
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +2
-0+/* Load XDP program to introspect */ + err =bpf_prog_load (file,BPF_PROG_TYPE_XDP , &obj, &prog_fd);Please use BPF skeleton for this test. It will make it significantly shorter and clearer. See other
fentry_fexitselftest for example.Trying to do this, however, I’m getting the following when trying to execute the test:
test_xdp_bpf2bpf:PASS:pkt_skel_load 0 nsec
libbpf: fentry/_xdp_tx_iptunnel is not found in vmlinux BTF
libbpf: failed to load object 'test_xdp_bpf2bpf'
libbpf: failed to load BPF skeleton 'test_xdp_bpf2bpf': -2 test_xdp_bpf2bpf:FAIL:ftrace_skel_load ftrace skeleton failedMy program is straight forward following the
fentry_fexit.cexample:
pkt_skel=test_xdp__open_and_load()
if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdpskeleton
failed\n"))return; map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl); bpf_map_update_elem(map_fd, &key4, &value4, 0); /* Load eBPF trace program */ ftrace_skel = test_xdp_bpf2bpf__open_and_load(); if (CHECK(!ftrace_skel, "ftrace_skel_load", "ftrace skeleton failed\n")) goto out;
Quoted 2 lines
I assume this is due to the missing link from the XDP program to the eBPF trace program.
yes, exactly
Quoted 18 lines
Previously I did this trough:
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c new file +5
-0+DECLARE_LIBBPF_OPTS (bpf_object_open_opts, opts, + .attach_prog_fd = prog_fd, + ); + + tracer_obj =bpf_object__open_file ("./test_xdp_bpf2bpf.o" , &opts);If I use this approach as before it works, i.e.:
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts,
.attach_prog_fd =pkt_fd,
);
ftrace_skel=test_xdp_bpf2bpf__open_opts(&opts); if (CHECK(!ftrace_skel, "__open_opts”, "ftrace skeleton
failed\n"))
Quoted 7 lines
goto out; if (CHECK(test_xdp_bpf2bpf__load(ftrace_skel), "__load", "ftrace skeleton failed\n")) goto out;But I do not see this in the
fentry_fexit.cexample, guess I might be missing something that is right in front of me :(
you are not missing anything, and this second variant is what you have to do. __open_and_load() is just an convenience wrapper around separate __open() followed by __load(), for cases where user doesn't have to specify any extra options.
Quoted 4 lines
[...]