Message ID | 1592914031-31049-9-git-send-email-alan.maguire@oracle.com |
---|---|
State | Changes Requested |
Delegated to: | BPF Maintainers |
Headers | show |
Series | bpf, printk: add BTF-based type printing | expand |
On Tue, Jun 23, 2020 at 5:12 AM Alan Maguire <alan.maguire@oracle.com> wrote: > > tests verify we get 0 return value from bpf_trace_print() > using %pT format specifier with various modifiers/pointer > values. > > Signed-off-by: Alan Maguire <alan.maguire@oracle.com> > --- Acked-by: Andrii Nakryiko <andriin@fb.com> > .../selftests/bpf/prog_tests/trace_printk_btf.c | 45 +++++++++++++++++++++ > .../selftests/bpf/progs/netif_receive_skb.c | 47 ++++++++++++++++++++++ > 2 files changed, 92 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/trace_printk_btf.c > create mode 100644 tools/testing/selftests/bpf/progs/netif_receive_skb.c > [...] > diff --git a/tools/testing/selftests/bpf/progs/netif_receive_skb.c b/tools/testing/selftests/bpf/progs/netif_receive_skb.c > new file mode 100644 > index 0000000..03ca1d8 > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/netif_receive_skb.c > @@ -0,0 +1,47 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2020, Oracle and/or its affiliates. */ > +#include "vmlinux.h" > +#include <bpf/bpf_helpers.h> > +#include <bpf/bpf_tracing.h> > + > +char _license[] SEC("license") = "GPL"; > + > +int ret; > +int num_subtests; > +int ran_subtests; oh, interesting, so Clang doesn't put these into the COM section anymore? We used to need to explicitly zero-initialize these global vars because of that. > + > +#define CHECK_PRINTK(_fmt, _p, res) \ > + do { \ > + char fmt[] = _fmt; \ pro tip: you can use `static const char fmt[] = _fmt` and it will just work. > + ++num_subtests; \ > + if (ret >= 0) { \ > + ++ran_subtests; \ > + ret = bpf_trace_printk(fmt, sizeof(fmt), (_p)); \ > + } \ > + } while (0) > + > +/* TRACE_EVENT(netif_receive_skb, > + * TP_PROTO(struct sk_buff *skb), > + */ > +SEC("tp_btf/netif_receive_skb") > +int BPF_PROG(trace_netif_receive_skb, struct sk_buff *skb) > +{ > + char skb_type[] = "struct sk_buff"; same, `static const char` will generate more optimal code (good to have good examples in selftests for people to follow). But don't bother re-spinning just for this. > + struct btf_ptr nullp = { .ptr = 0, .type = skb_type }; > + struct btf_ptr p = { .ptr = skb, .type = skb_type }; > + > + CHECK_PRINTK("%pT\n", &p, &res); > + CHECK_PRINTK("%pTc\n", &p, &res); > + CHECK_PRINTK("%pTN\n", &p, &res); > + CHECK_PRINTK("%pTx\n", &p, &res); > + CHECK_PRINTK("%pT0\n", &p, &res); > + CHECK_PRINTK("%pTcNx0\n", &p, &res); > + CHECK_PRINTK("%pT\n", &nullp, &res); > + CHECK_PRINTK("%pTc\n", &nullp, &res); > + CHECK_PRINTK("%pTN\n", &nullp, &res); > + CHECK_PRINTK("%pTx\n", &nullp, &res); > + CHECK_PRINTK("%pT0\n", &nullp, &res); > + CHECK_PRINTK("%pTcNx0\n", &nullp, &res); > + > + return 0; > +} > -- > 1.8.3.1 >
diff --git a/tools/testing/selftests/bpf/prog_tests/trace_printk_btf.c b/tools/testing/selftests/bpf/prog_tests/trace_printk_btf.c new file mode 100644 index 0000000..791eb97 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/trace_printk_btf.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> + +#include "netif_receive_skb.skel.h" + +void test_trace_printk_btf(void) +{ + struct netif_receive_skb *skel; + struct netif_receive_skb__bss *bss; + int err, duration = 0; + + skel = netif_receive_skb__open(); + if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) + return; + + err = netif_receive_skb__load(skel); + if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err)) + goto cleanup; + + bss = skel->bss; + + err = netif_receive_skb__attach(skel); + if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) + goto cleanup; + + /* generate receive event */ + system("ping -c 1 127.0.0.1 >/dev/null"); + + /* + * Make sure netif_receive_skb program was triggered + * and it set expected return values from bpf_trace_printk()s + * and all tests ran. + */ + if (CHECK(bss->ret <= 0, + "bpf_trace_printk: got return value", + "ret <= 0 %d test %d\n", bss->ret, bss->num_subtests)) + goto cleanup; + + CHECK(bss->num_subtests != bss->ran_subtests, "check all subtests ran", + "only ran %d of %d tests\n", bss->num_subtests, + bss->ran_subtests); + +cleanup: + netif_receive_skb__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/netif_receive_skb.c b/tools/testing/selftests/bpf/progs/netif_receive_skb.c new file mode 100644 index 0000000..03ca1d8 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/netif_receive_skb.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020, Oracle and/or its affiliates. */ +#include "vmlinux.h" +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +char _license[] SEC("license") = "GPL"; + +int ret; +int num_subtests; +int ran_subtests; + +#define CHECK_PRINTK(_fmt, _p, res) \ + do { \ + char fmt[] = _fmt; \ + ++num_subtests; \ + if (ret >= 0) { \ + ++ran_subtests; \ + ret = bpf_trace_printk(fmt, sizeof(fmt), (_p)); \ + } \ + } while (0) + +/* TRACE_EVENT(netif_receive_skb, + * TP_PROTO(struct sk_buff *skb), + */ +SEC("tp_btf/netif_receive_skb") +int BPF_PROG(trace_netif_receive_skb, struct sk_buff *skb) +{ + char skb_type[] = "struct sk_buff"; + struct btf_ptr nullp = { .ptr = 0, .type = skb_type }; + struct btf_ptr p = { .ptr = skb, .type = skb_type }; + + CHECK_PRINTK("%pT\n", &p, &res); + CHECK_PRINTK("%pTc\n", &p, &res); + CHECK_PRINTK("%pTN\n", &p, &res); + CHECK_PRINTK("%pTx\n", &p, &res); + CHECK_PRINTK("%pT0\n", &p, &res); + CHECK_PRINTK("%pTcNx0\n", &p, &res); + CHECK_PRINTK("%pT\n", &nullp, &res); + CHECK_PRINTK("%pTc\n", &nullp, &res); + CHECK_PRINTK("%pTN\n", &nullp, &res); + CHECK_PRINTK("%pTx\n", &nullp, &res); + CHECK_PRINTK("%pT0\n", &nullp, &res); + CHECK_PRINTK("%pTcNx0\n", &nullp, &res); + + return 0; +}
tests verify we get 0 return value from bpf_trace_print() using %pT format specifier with various modifiers/pointer values. Signed-off-by: Alan Maguire <alan.maguire@oracle.com> --- .../selftests/bpf/prog_tests/trace_printk_btf.c | 45 +++++++++++++++++++++ .../selftests/bpf/progs/netif_receive_skb.c | 47 ++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/trace_printk_btf.c create mode 100644 tools/testing/selftests/bpf/progs/netif_receive_skb.c