bpf: Introduce BPF_TRACE_x helper for the tracing tests
Subsystems (6)
PATCH 1/1 bpf: Introduce BPF_TRACE_x helper for the tracing tests
For BPF_PROG_TYPE_TRACING, the bpf_prog's ctx is an array of u64. This patch borrows the idea from BPF_CALL_x in filter.h to convert a u64 to the arg type of the traced function.
The new BPF_TRACE_x has an arg to specify the return type of a bpf_prog. It will be used in the future TCP-ops bpf_prog that may return "void".
The new macros are defined in the new header file "bpf_trace_helpers.h". It is under selftests/bpf/ for now. It could be moved to libbpf later after seeing more upcoming non-tracing use cases.
The tests are changed to use these new macros also. Hence, the k[s]u8/16/32/64 are no longer needed and they are removed from the bpf_helpers.h.
--- tools/lib/bpf/bpf_helpers.h | 13 --- .../testing/selftests/bpf/bpf_trace_helpers.h | 58 +++++++++++++ .../testing/selftests/bpf/progs/fentry_test.c | 72 ++++------------ .../selftests/bpf/progs/fexit_bpf2bpf.c | 27 ++---- .../testing/selftests/bpf/progs/fexit_test.c | 83 +++++-------------- tools/testing/selftests/bpf/progs/kfree_skb.c | 43 +++------- .../selftests/bpf/progs/test_overhead.c | 16 ++-- 7 files changed, 125 insertions(+), 187 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_trace_helpers.h
tools/lib/bpf/bpf_helpers.h +0 -13
@@ -44,17 +44,4 @@ enum libbpf_pin_type { LIBBPF_PIN_BY_NAME, }; -/* The following types should be used by BPF_PROG_TYPE_TRACING program to -* access kernel function arguments. BPF trampoline and raw tracepoints -* typecast arguments to 'unsigned long long'. -*/ -typedef int __attribute__((aligned(8))) ks32; -typedef char __attribute__((aligned(8))) ks8; -typedef short __attribute__((aligned(8))) ks16; -typedef long long __attribute__((aligned(8))) ks64; -typedef unsigned int __attribute__((aligned(8))) ku32; -typedef unsigned char __attribute__((aligned(8))) ku8; -typedef unsigned short __attribute__((aligned(8))) ku16; -typedef unsigned long long __attribute__((aligned(8))) ku64; -#endif
tools/testing/selftests/bpf/bpf_trace_helpers.h new file +58 -0
58 lines not on this page · raw message
tools/testing/selftests/bpf/progs/fentry_test.c +18 -54
107 lines not on this page · raw message
tools/testing/selftests/bpf/progs/fexit_bpf2bpf.c +9 -18
@@ -2,46 +2,37 @@/* Copyright (c) 2019 Facebook */ #include <linux/bpf.h> #include "bpf_helpers.h" +#include "bpf_trace_helpers.h" struct sk_buff {unsigned int len; }; -struct args { -struct sk_buff *skb; -ks32 ret; -};static volatile __u64 test_result; -SEC("fexit/test_pkt_access") -int test_main (struct args *ctx) +BPF_TRACE_2 ("fexit/test_pkt_access" , test_main, + struct sk_buff *, skb, int, ret) { -struct sk_buff *skb = ctx->skb;int len;__builtin_preserve_access_index (({ len = skb->len; })); -if (len !=74 || ctx->ret !=0 ) +if (len !=74 || ret !=0 )return 0 ; test_result =1 ;return 0 ; } -struct args_subprog1 { -struct sk_buff *skb; -ks32 ret; -};static volatile __u64 test_result_subprog1; -SEC("fexit/test_pkt_access_subprog1") -int test_subprog1 (struct args_subprog1 *ctx) +BPF_TRACE_2 ("fexit/test_pkt_access_subprog1" , test_subprog1, + struct sk_buff *, skb, int, ret) { -struct sk_buff *skb = ctx->skb;int len;__builtin_preserve_access_index (({ len = skb->len; })); -if (len !=74 || ctx->ret !=148 ) +if (len !=74 || ret !=148 )return 0 ; test_result_subprog1 =1 ;return 0 ;
@@ -62,8 +53,8 @@ int test_subprog1(struct args_subprog1 *ctx)* instead of accurate types. */ struct args_subprog2 { -ku64 args[5 ]; -ku64 ret; +__u64 args[5 ]; +__u64 ret; };static volatile __u64 test_result_subprog2;SEC ("fexit/test_pkt_access_subprog2" )
tools/testing/selftests/bpf/progs/fexit_test.c +21 -62
118 lines not on this page · raw message
tools/testing/selftests/bpf/progs/kfree_skb.c +13 -30
82 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_overhead.c +6 -10
@@ -3,6 +3,7 @@#include <linux/bpf.h> #include "bpf_helpers.h" #include "bpf_tracing.h" +#include "bpf_trace_helpers.h" SEC("kprobe/__set_task_comm")int prog1 (struct pt_regs *ctx)
@@ -22,20 +23,15 @@ int prog3(struct bpf_raw_tracepoint_args *ctx)return 0 ; } -struct __set_task_comm_args { -struct task_struct *tsk; -const char *buf; -ku8 exec; -}; - -SEC("fentry/__set_task_comm") -int prog4 (struct __set_task_comm_args *ctx) +struct task_struct ; +BPF_TRACE_3 ("fentry/__set_task_comm" , prog4, + struct task_struct *, tsk, const char *, buf, __u8, exec) {return 0 ; } -SEC("fexit/__set_task_comm") -int prog5 (struct __set_task_comm_args *ctx) +BPF_TRACE_3 ("fexit/__set_task_comm" , prog5, + struct task_struct *, tsk, const char *, buf, __u8, exec) {return 0 ; }
Signature · 1 line
2.17.1
Quoted 17 lines · Martin KaFai Lau
For
BPF_PROG_TYPE_TRACING, the bpf_prog's ctx is an array of u64. This patch borrows the idea fromBPF_CALL_xinfilter.hto convert a u64 to the arg type of the traced function.The new
BPF_TRACE_xhas an arg to specify the return type of abpf_prog. It will be used in the future TCP-opsbpf_progthat may return "void".The new macros are defined in the new header file "
bpf_trace_helpers.h". It is under selftests/bpf/ for now. It could be moved to libbpf later after seeing more upcoming non-tracing use cases.The tests are changed to use these new macros also. Hence, the k[s]u8/16/32/64 are no longer needed and they are removed from the
bpf_helpers.h.Signed-off-by Martin KaFai Lau <[email protected]>
Quoted 9 lines
--- tools/lib/bpf/bpf_helpers.h | 13 --- .../testing/selftests/bpf/bpf_trace_helpers.h | 58 +++++++++++++ .../testing/selftests/bpf/progs/fentry_test.c | 72 ++++------------ .../selftests/bpf/progs/fexit_bpf2bpf.c | 27 ++---- .../testing/selftests/bpf/progs/fexit_test.c | 83 +++++-------------- tools/testing/selftests/bpf/progs/kfree_skb.c | 43 +++------- .../selftests/bpf/progs/test_overhead.c | 16 ++-- 7 files changed, 125 insertions(+), 187 deletions(-)
Very nice cleanup!
Applied. Thanks!