All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf-next v3] selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program
@ 2020-01-15 13:15 Eelco Chaudron
  2020-01-15 17:04 ` Andrii Nakryiko
  0 siblings, 1 reply; 3+ messages in thread
From: Eelco Chaudron @ 2020-01-15 13:15 UTC (permalink / raw)
  To: bpf; +Cc: davem, ast, netdev, andrii.nakryiko, maciej.fijalkowski

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 <echaudro@redhat.com>
---
v2 -> v3:
  - Incorporated review comments from Andrii and Maciej

v1 -> v2:
  - Changed code to use the BPF skeleton
  - Replace static volatile with global variable in eBPF code

 .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c |   65 ++++++++++++++++++++
 .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c |   44 ++++++++++++++
 2 files changed, 109 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

diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
new file mode 100644
index 000000000000..6b56bdc73ebc
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <net/if.h>
+#include "test_xdp.skel.h"
+#include "test_xdp_bpf2bpf.skel.h"
+
+void test_xdp_bpf2bpf(void)
+{
+	__u32 duration = 0, retval, size;
+	char buf[128];
+	int err, pkt_fd, map_fd;
+	struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
+	struct iptnl_info value4 = {.family = AF_INET};
+	struct test_xdp *pkt_skel = NULL;
+	struct test_xdp_bpf2bpf *ftrace_skel = NULL;
+	struct vip key4 = {.protocol = 6, .family = AF_INET};
+	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
+
+	/* Load XDP program to introspect */
+	pkt_skel = test_xdp__open_and_load();
+	if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton failed\n"))
+		return;
+
+	pkt_fd = bpf_program__fd(pkt_skel->progs._xdp_tx_iptunnel);
+
+	map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl);
+	bpf_map_update_elem(map_fd, &key4, &value4, 0);
+
+	/* Load trace program */
+	opts.attach_prog_fd = pkt_fd,
+	ftrace_skel = test_xdp_bpf2bpf__open_opts(&opts);
+	if (CHECK(!ftrace_skel, "__open", "ftrace skeleton failed\n"))
+		goto out;
+
+	err = test_xdp_bpf2bpf__load(ftrace_skel);
+	if (CHECK(err, "__load", "ftrace skeleton failed\n"))
+		goto out;
+
+	err = test_xdp_bpf2bpf__attach(ftrace_skel);
+	if (CHECK(err, "ftrace_attach", "ftrace attach failed: %d\n", err))
+		goto out;
+
+	/* Run test program */
+	err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4),
+				buf, &size, &retval, &duration);
+
+	if (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))
+		goto out;
+
+	/* Verify test results */
+	if (CHECK(ftrace_skel->bss->test_result_fentry != if_nametoindex("lo"),
+		  "result", "fentry failed err %llu\n",
+		  ftrace_skel->bss->test_result_fentry))
+		goto out;
+
+	CHECK(ftrace_skel->bss->test_result_fexit != XDP_TX, "result",
+	      "fexit failed err %llu\n", ftrace_skel->bss->test_result_fexit);
+
+out:
+	test_xdp__destroy(pkt_skel);
+	test_xdp_bpf2bpf__destroy(ftrace_skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c b/tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
new file mode 100644
index 000000000000..f8f105af6743
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_xdp_bpf2bpf.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include "bpf_helpers.h"
+#include "bpf_trace_helpers.h"
+
+struct net_device {
+	/* Structure does not need to contain all entries,
+	 * as "preserve_access_index" will use BTF to fix this...
+	 */
+	int ifindex;
+} __attribute__((preserve_access_index));
+
+struct xdp_rxq_info {
+	/* Structure does not need to contain all entries,
+	 * as "preserve_access_index" will use BTF to fix this...
+	 */
+	struct net_device *dev;
+	__u32 queue_index;
+} __attribute__((preserve_access_index));
+
+struct xdp_buff {
+	void *data;
+	void *data_end;
+	void *data_meta;
+	void *data_hard_start;
+	unsigned long handle;
+	struct xdp_rxq_info *rxq;
+} __attribute__((preserve_access_index));
+
+__u64 test_result_fentry = 0;
+SEC("fentry/_xdp_tx_iptunnel")
+int BPF_PROG(trace_on_entry, struct xdp_buff *xdp)
+{
+	test_result_fentry = xdp->rxq->dev->ifindex;
+	return 0;
+}
+
+__u64 test_result_fexit = 0;
+SEC("fexit/_xdp_tx_iptunnel")
+int BPF_PROG(trace_on_exit, struct xdp_buff *xdp, int ret)
+{
+	test_result_fexit = ret;
+	return 0;
+}


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next v3] selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program
  2020-01-15 13:15 [PATCH bpf-next v3] selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program Eelco Chaudron
@ 2020-01-15 17:04 ` Andrii Nakryiko
  2020-01-15 21:26   ` Alexei Starovoitov
  0 siblings, 1 reply; 3+ messages in thread
From: Andrii Nakryiko @ 2020-01-15 17:04 UTC (permalink / raw)
  To: Eelco Chaudron
  Cc: bpf, David S. Miller, Alexei Starovoitov, Networking, maciej.fijalkowski

On Wed, Jan 15, 2020 at 5:15 AM Eelco Chaudron <echaudro@redhat.com> wrote:
>
> 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 <echaudro@redhat.com>
> ---

Looks good, thanks! You are just missing one CHECK() for
bpf_map_update_elem below, please add it. With that:

Acked-by: Andrii Nakryiko <andriin@fb.com>

> v2 -> v3:
>   - Incorporated review comments from Andrii and Maciej
>
> v1 -> v2:
>   - Changed code to use the BPF skeleton
>   - Replace static volatile with global variable in eBPF code
>
>  .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c |   65 ++++++++++++++++++++
>  .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c |   44 ++++++++++++++
>  2 files changed, 109 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
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
> new file mode 100644
> index 000000000000..6b56bdc73ebc
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
> @@ -0,0 +1,65 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +#include <net/if.h>
> +#include "test_xdp.skel.h"
> +#include "test_xdp_bpf2bpf.skel.h"
> +
> +void test_xdp_bpf2bpf(void)
> +{
> +       __u32 duration = 0, retval, size;
> +       char buf[128];
> +       int err, pkt_fd, map_fd;
> +       struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
> +       struct iptnl_info value4 = {.family = AF_INET};
> +       struct test_xdp *pkt_skel = NULL;
> +       struct test_xdp_bpf2bpf *ftrace_skel = NULL;
> +       struct vip key4 = {.protocol = 6, .family = AF_INET};
> +       DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
> +
> +       /* Load XDP program to introspect */
> +       pkt_skel = test_xdp__open_and_load();
> +       if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton failed\n"))
> +               return;
> +
> +       pkt_fd = bpf_program__fd(pkt_skel->progs._xdp_tx_iptunnel);
> +
> +       map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl);
> +       bpf_map_update_elem(map_fd, &key4, &value4, 0);

CHECK()? Sorry, didn't spot first time.


> +
> +       /* Load trace program */
> +       opts.attach_prog_fd = pkt_fd,
> +       ftrace_skel = test_xdp_bpf2bpf__open_opts(&opts);
> +       if (CHECK(!ftrace_skel, "__open", "ftrace skeleton failed\n"))
> +               goto out;
> +

[...]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next v3] selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program
  2020-01-15 17:04 ` Andrii Nakryiko
@ 2020-01-15 21:26   ` Alexei Starovoitov
  0 siblings, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2020-01-15 21:26 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Eelco Chaudron, bpf, David S. Miller, Alexei Starovoitov,
	Networking, Fijalkowski, Maciej

On Wed, Jan 15, 2020 at 9:04 AM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Wed, Jan 15, 2020 at 5:15 AM Eelco Chaudron <echaudro@redhat.com> wrote:
> >
> > 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 <echaudro@redhat.com>
> > ---
>
> Looks good, thanks! You are just missing one CHECK() for
> bpf_map_update_elem below, please add it. With that:
>
> Acked-by: Andrii Nakryiko <andriin@fb.com>
>
> > v2 -> v3:
> >   - Incorporated review comments from Andrii and Maciej
> >
> > v1 -> v2:
> >   - Changed code to use the BPF skeleton
> >   - Replace static volatile with global variable in eBPF code
> >
> >  .../testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c |   65 ++++++++++++++++++++
> >  .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c |   44 ++++++++++++++
> >  2 files changed, 109 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
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
> > new file mode 100644
> > index 000000000000..6b56bdc73ebc
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
> > @@ -0,0 +1,65 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +#include <test_progs.h>
> > +#include <net/if.h>
> > +#include "test_xdp.skel.h"
> > +#include "test_xdp_bpf2bpf.skel.h"
> > +
> > +void test_xdp_bpf2bpf(void)
> > +{
> > +       __u32 duration = 0, retval, size;
> > +       char buf[128];
> > +       int err, pkt_fd, map_fd;
> > +       struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
> > +       struct iptnl_info value4 = {.family = AF_INET};
> > +       struct test_xdp *pkt_skel = NULL;
> > +       struct test_xdp_bpf2bpf *ftrace_skel = NULL;
> > +       struct vip key4 = {.protocol = 6, .family = AF_INET};
> > +       DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
> > +
> > +       /* Load XDP program to introspect */
> > +       pkt_skel = test_xdp__open_and_load();
> > +       if (CHECK(!pkt_skel, "pkt_skel_load", "test_xdp skeleton failed\n"))
> > +               return;
> > +
> > +       pkt_fd = bpf_program__fd(pkt_skel->progs._xdp_tx_iptunnel);
> > +
> > +       map_fd = bpf_map__fd(pkt_skel->maps.vip2tnl);
> > +       bpf_map_update_elem(map_fd, &key4, &value4, 0);
>
> CHECK()? Sorry, didn't spot first time.

There is no such check in few other places in selftests and
I don't think it's really necessary here.
If we adjust them let's fix them all.

Applied to bpf-next. Thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-01-15 21:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-15 13:15 [PATCH bpf-next v3] selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program Eelco Chaudron
2020-01-15 17:04 ` Andrii Nakryiko
2020-01-15 21:26   ` Alexei Starovoitov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.