netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@redhat.com>
To: David Miller <davem@davemloft.net>
Cc: netdev@vger.kernel.org,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Jakub Kicinski" <jakub.kicinski@netronome.com>,
	BjörnTöpel <bjorn.topel@gmail.com>
Subject: [PATCH net-next v4 6/6] selftests/bpf: Add test for default devmap allocation
Date: Mon, 08 Apr 2019 19:05:57 +0200	[thread overview]
Message-ID: <155474315697.24432.13044630044539815309.stgit@alrua-x1> (raw)
In-Reply-To: <155474315642.24432.6179239576879119104.stgit@alrua-x1>

This adds a new selftest checking the allocation and de-allocation of
default maps in different network namespaces. It loads the two different
kinds of programs that need allocation (programs using tail calls, and
programs using redirect), and moves interfaces around between namespaces to
make sure the default map is correctly allocated and de-allocated in each
of the namespaces.

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
 tools/testing/selftests/bpf/Makefile               |    3 -
 .../selftests/bpf/progs/test_xdp_tail_call.c       |   39 ++++++++
 .../testing/selftests/bpf/test_xdp_devmap_alloc.sh |   94 ++++++++++++++++++++
 3 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_tail_call.c
 create mode 100755 tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 77b73b892136..07f2f54a6a87 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -54,7 +54,8 @@ TEST_PROGS := test_kmod.sh \
 	test_lwt_ip_encap.sh \
 	test_tcp_check_syncookie.sh \
 	test_tc_tunnel.sh \
-	test_tc_edt.sh
+	test_tc_edt.sh \
+	test_xdp_devmap_alloc.sh
 
 TEST_PROGS_EXTENDED := with_addr.sh \
 	with_tunnels.sh \
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_tail_call.c b/tools/testing/selftests/bpf/progs/test_xdp_tail_call.c
new file mode 100644
index 000000000000..6c89dc4ad341
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_xdp_tail_call.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define KBUILD_MODNAME "xdp_dummy"
+#include <linux/bpf.h>
+#include "bpf_helpers.h"
+
+struct bpf_map_def SEC("maps") jmp_table = {
+	.type = BPF_MAP_TYPE_PROG_ARRAY,
+	.key_size = sizeof(__u32),
+	.value_size = sizeof(__u32),
+	.max_entries = 8,
+};
+
+struct bpf_map_def SEC("maps") arr_map = {
+	.type = BPF_MAP_TYPE_ARRAY,
+	.key_size = sizeof(__u32),
+	.value_size = sizeof(__u32),
+	.max_entries = 1,
+};
+
+
+SEC("xdp_dummy_tail_call")
+int xdp_dummy_prog(struct xdp_md *ctx)
+{
+        long *value;
+        __u32 key = 0;
+
+        /* We just need the call instruction in the program, so it is fine that
+         * this fails, but it should not be optimised out by the compiler (so
+         * can't just do if (false)).
+         */
+        value = bpf_map_lookup_elem(&arr_map, &key);
+        if (value)
+                bpf_tail_call(ctx, &jmp_table, 1);
+
+	return XDP_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh b/tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh
new file mode 100755
index 000000000000..0431f94f136a
--- /dev/null
+++ b/tools/testing/selftests/bpf/test_xdp_devmap_alloc.sh
@@ -0,0 +1,94 @@
+#!/bin/bash
+
+cleanup()
+{
+	if [ "$?" = "0" ]; then
+		echo "selftests: test_xdp_devmap_alloc [PASS]";
+	else
+		echo "selftests: test_xdp_devmap_alloc [FAILED]";
+	fi
+
+	set +e
+	ip link del veth1 2> /dev/null
+	ip netns del ns1 2> /dev/null
+	ip netns del ns2 2> /dev/null
+}
+
+check_alloc()
+{
+    ns="$1"
+    expected="$2 $3"
+
+    if [[ "$ns" == "root" ]]; then
+        actual=$(< /proc/net/default_dev_map)
+    else
+        actual=$(ip netns exec "$ns" cat /proc/net/default_dev_map)
+    fi
+
+    if [[ "$expected" != "$actual" ]]; then
+        echo "Expected allocation '$expected' got '$actual'" >&2
+        exit 1
+    fi
+}
+
+ip link set dev lo xdp off 2>/dev/null > /dev/null
+if [ $? -ne 0 ];then
+	echo "selftests: [SKIP] Could not run test without ip xdp support"
+	exit 0
+fi
+set -e
+
+ip netns add ns1
+ip netns add ns2
+
+trap cleanup 0 2 3 6 9
+
+ip link add veth1 type veth peer name veth2
+
+ip link set veth1 netns ns1
+ip link set veth2 netns ns2
+
+check_alloc ns1 0 0
+check_alloc ns2 0 0
+
+# Check that loading an xdp tail call program increases counter, but doesn't
+# load a program
+ip netns exec ns2 ip link set dev veth2 xdp obj test_xdp_tail_call.o sec xdp_dummy_tail_call
+check_alloc ns2 1 0
+
+# Check that loading a redirect program allocates a map, and
+# removing that program de-allocates the map again.
+ip netns exec ns1 ip link set dev veth1 xdp obj test_xdp_redirect.o sec redirect_to_111
+check_alloc ns1 1 1
+# Now we should have a map allocated in the other ns
+check_alloc ns2 1 1
+ip netns exec ns1 ip link set dev veth1 xdp off
+check_alloc ns1 0 0
+check_alloc ns2 1 0
+ip netns exec ns2 ip link set dev veth2 xdp off
+check_alloc ns2 0 0
+
+# Check that switching between redirect and non-redirect programs correctly
+# allocs/de-allocs map
+ip netns exec ns1 ip link set dev veth1 xdp obj xdp_dummy.o sec xdp_dummy
+check_alloc ns1 0 0
+ip netns exec ns1 ip -force link set dev veth1 xdp obj test_xdp_redirect.o sec redirect_to_111
+check_alloc ns1 1 1
+ip netns exec ns1 ip -force link set dev veth1 xdp obj xdp_dummy.o sec xdp_dummy
+check_alloc ns1 0 0
+
+ip netns exec ns1 ip link set dev veth1 xdp off
+
+# Check that moving an interface into a namespace will allocate the map
+ip netns del ns1
+ip netns add ns1
+ip link add veth1 type veth peer name veth2
+
+ip link set dev veth1 xdp obj test_xdp_redirect.o sec redirect_to_111
+check_alloc root 1 1
+check_alloc ns1 0 0
+ip link set dev veth1 netns ns1
+check_alloc ns1 1 1
+check_alloc root 0 0
+
+exit 0


      parent reply	other threads:[~2019-04-08 17:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-08 17:05 [PATCH net-next v4 0/6] xdp: Use a default map for xdp_redirect helper Toke Høiland-Jørgensen
2019-04-08 17:05 ` [PATCH net-next v4 4/6] xdp: Always use a devmap for XDP_REDIRECT to a device Toke Høiland-Jørgensen
2019-04-09  8:25   ` Jesper Dangaard Brouer
2019-04-09  9:23     ` Toke Høiland-Jørgensen
2019-04-10  7:59   ` [xdp] 9cb54e254c: kernel/bpf/devmap.c:#suspicious_rcu_dereference_check() kernel test robot
2019-04-10 10:00     ` Toke Høiland-Jørgensen
2019-04-08 17:05 ` [PATCH net-next v4 5/6] xdp: Add devmap_idx map type for looking up devices by ifindex Toke Høiland-Jørgensen
2019-04-08 17:05 ` [PATCH net-next v4 1/6] net: xdp: refactor XDP attach Toke Høiland-Jørgensen
2019-04-08 20:23   ` Daniel Borkmann
2019-05-09 10:40     ` Björn Töpel
2019-04-09 12:54   ` kbuild test robot
2019-04-08 17:05 ` [PATCH net-next v4 3/6] xdp: Refactor devmap code in preparation for subsequent additions Toke Høiland-Jørgensen
2019-04-08 17:05 ` [PATCH net-next v4 2/6] net: xdp: remove XDP_QUERY_PROG Toke Høiland-Jørgensen
2019-04-08 17:05 ` Toke Høiland-Jørgensen [this message]

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=155474315697.24432.13044630044539815309.stgit@alrua-x1 \
    --to=toke@redhat.com \
    --cc=ast@kernel.org \
    --cc=bjorn.topel@gmail.com \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=jakub.kicinski@netronome.com \
    --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).