bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eelco Chaudron <echaudro@redhat.com>
To: bpf@vger.kernel.org
Cc: davem@davemloft.net, ast@kernel.org, netdev@vger.kernel.org
Subject: [PATCH bpf-next v2] selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program
Date: Tue, 14 Jan 2020 15:58:16 +0000	[thread overview]
Message-ID: <157901745600.30872.10096561620432101095.stgit@xdp-tutorial> (raw)

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>

---
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 |   69 ++++++++++++++++++++
 .../testing/selftests/bpf/progs/test_xdp_bpf2bpf.c |   44 +++++++++++++
 2 files changed, 113 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..e6e849df2632
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
@@ -0,0 +1,69 @@
+// 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)
+{
+
+	struct test_xdp *pkt_skel = NULL;
+        struct test_xdp_bpf2bpf *ftrace_skel = NULL;
+	__u64 *ftrace_res;
+
+	struct vip key4 = {.protocol = 6, .family = AF_INET};
+	struct iptnl_info value4 = {.family = AF_INET};
+	char buf[128];
+	struct iphdr *iph = (void *)buf + sizeof(struct ethhdr);
+	__u32 duration = 0, retval, size;
+	int err, pkt_fd, map_fd;
+
+	/* 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);
+
+	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", "ftrace skeleton failed\n"))
+	  goto out;
+
+	if (CHECK(test_xdp_bpf2bpf__load(ftrace_skel), "__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);
+
+	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 */
+	ftrace_res = (__u64 *)ftrace_skel->bss;
+
+	if (CHECK(ftrace_res[0] != if_nametoindex("lo"), "result",
+		  "fentry failed err %llu\n", ftrace_res[0]))
+		goto out;
+
+	if (CHECK(ftrace_res[1] != XDP_TX, "result",
+		  "fexit failed err %llu\n", ftrace_res[1]))
+		goto out;
+
+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..74c78b30ae07
--- /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;
+BPF_TRACE_1("fentry/_xdp_tx_iptunnel", trace_on_entry,
+	    struct xdp_buff *, xdp)
+{
+	test_result_fentry = xdp->rxq->dev->ifindex;
+	return 0;
+}
+
+__u64 test_result_fexit = 0;
+BPF_TRACE_2("fexit/_xdp_tx_iptunnel", trace_on_exit,
+	    struct xdp_buff*, xdp, int, ret)
+{
+	test_result_fexit = ret;
+	return 0;
+}


             reply	other threads:[~2020-01-14 15:58 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-14 15:58 Eelco Chaudron [this message]
2020-01-14  9:33 ` [PATCH bpf-next v2] selftests/bpf: Add a test for attaching a bpf fentry/fexit trace to an XDP program Maciej Fijalkowski
2020-01-14 18:49 ` Andrii Nakryiko
2020-01-15 13:12   ` Eelco Chaudron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=157901745600.30872.10096561620432101095.stgit@xdp-tutorial \
    --to=echaudro@redhat.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).