public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs
@ 2025-01-03 10:10 Bastien Curutchet (eBPF Foundation)
  2025-01-03 10:10 ` [PATCH 1/3] selftests/bpf: test_xdp_redirect: Rename BPF sections Bastien Curutchet (eBPF Foundation)
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Bastien Curutchet (eBPF Foundation) @ 2025-01-03 10:10 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
	Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Mykola Lysenko, Shuah Khan
  Cc: Thomas Petazzoni, Alexis Lothore, netdev, bpf, linux-kselftest,
	linux-kernel, Bastien Curutchet (eBPF Foundation)

Hi all,

This patch series continues the work to migrate the *.sh tests into
prog_tests.

test_xdp_redirect.sh tests the XDP redirections done through
bpf_redirect().

These XDP redirections are already tested by prog_tests/xdp_do_redirect.c
but IMO it doesn't cover the exact same code path because
xdp_do_redirect.c uses bpf_prog_test_run_opts() to trigger redirections
of 'fake packets' while test_xdp_redirect.sh redirects packets coming
from the network. Also, the test_xdp_redirect.sh script tests the
redirections with both SKB and DRV modes while xdp_do_redirect.c only
tests the DRV mode.

The patch series adds two new test cases in prog_tests/xdp_do_redirect.c
to replace the test_xdp_redirect.sh script.

Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
---
Bastien Curutchet (eBPF Foundation) (3):
      selftests/bpf: test_xdp_redirect: Rename BPF sections
      selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
      selftests/bpf: Migrate test_xdp_redirect.c to test_xdp_do_redirect.c

 tools/testing/selftests/bpf/Makefile               |   1 -
 .../selftests/bpf/prog_tests/xdp_do_redirect.c     | 192 +++++++++++++++++++++
 .../selftests/bpf/progs/test_xdp_do_redirect.c     |  12 ++
 .../selftests/bpf/progs/test_xdp_redirect.c        |  26 ---
 tools/testing/selftests/bpf/test_xdp_redirect.sh   |  79 ---------
 5 files changed, 204 insertions(+), 106 deletions(-)
---
base-commit: da86bde1e6d1b887efc46af5ee1f9bbccd27233e
change-id: 20241219-xdp_redirect-2b8ec79dc24e

Best regards,
-- 
Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>


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

* [PATCH 1/3] selftests/bpf: test_xdp_redirect: Rename BPF sections
  2025-01-03 10:10 [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet (eBPF Foundation)
@ 2025-01-03 10:10 ` Bastien Curutchet (eBPF Foundation)
  2025-01-03 13:18   ` Alexis Lothoré
  2025-01-03 10:10 ` [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 11+ messages in thread
From: Bastien Curutchet (eBPF Foundation) @ 2025-01-03 10:10 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
	Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Mykola Lysenko, Shuah Khan
  Cc: Thomas Petazzoni, Alexis Lothore, netdev, bpf, linux-kselftest,
	linux-kernel, Bastien Curutchet (eBPF Foundation)

SEC("redirect_to_111") and SEC("redirect_to_222") can't be loaded by the
__load() helper.

Rename both sections SEC("xdp") so it can be interpreted by the __load()
helper in upcoming patch.
Update the test_xdp_redirect.sh to use the program name instead of the
section name to load the BPF program.

Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
---
 tools/testing/selftests/bpf/progs/test_xdp_redirect.c | 4 ++--
 tools/testing/selftests/bpf/test_xdp_redirect.sh      | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/progs/test_xdp_redirect.c b/tools/testing/selftests/bpf/progs/test_xdp_redirect.c
index b778cad454852ed3a1808aca665dd9f9cc2b6c7b..7025aee08a001cfc42e52174a4acce7869dd331b 100644
--- a/tools/testing/selftests/bpf/progs/test_xdp_redirect.c
+++ b/tools/testing/selftests/bpf/progs/test_xdp_redirect.c
@@ -12,12 +12,12 @@
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 
-SEC("redirect_to_111")
+SEC("xdp")
 int xdp_redirect_to_111(struct xdp_md *xdp)
 {
 	return bpf_redirect(111, 0);
 }
-SEC("redirect_to_222")
+SEC("xdp")
 int xdp_redirect_to_222(struct xdp_md *xdp)
 {
 	return bpf_redirect(222, 0);
diff --git a/tools/testing/selftests/bpf/test_xdp_redirect.sh b/tools/testing/selftests/bpf/test_xdp_redirect.sh
index 0746a4fde9d3181667c081698249a1b6dd1d7663..3c61a1c22b084aa5ca824ec5e8057aa2fee12b71 100755
--- a/tools/testing/selftests/bpf/test_xdp_redirect.sh
+++ b/tools/testing/selftests/bpf/test_xdp_redirect.sh
@@ -56,8 +56,8 @@ test_xdp_redirect()
 
 	ip -n ${NS1} link set veth11 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null
 	ip -n ${NS2} link set veth22 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null
-	ip link set dev veth1 $xdpmode obj test_xdp_redirect.bpf.o sec redirect_to_222 &> /dev/null
-	ip link set dev veth2 $xdpmode obj test_xdp_redirect.bpf.o sec redirect_to_111 &> /dev/null
+	ip link set dev veth1 $xdpmode obj test_xdp_redirect.bpf.o program xdp_redirect_to_222 &> /dev/null
+	ip link set dev veth2 $xdpmode obj test_xdp_redirect.bpf.o program xdp_redirect_to_111 &> /dev/null
 
 	if ip netns exec ${NS1} ping -c 1 10.1.1.22 &> /dev/null &&
 	   ip netns exec ${NS2} ping -c 1 10.1.1.11 &> /dev/null; then

-- 
2.47.1


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

* [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
  2025-01-03 10:10 [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet (eBPF Foundation)
  2025-01-03 10:10 ` [PATCH 1/3] selftests/bpf: test_xdp_redirect: Rename BPF sections Bastien Curutchet (eBPF Foundation)
@ 2025-01-03 10:10 ` Bastien Curutchet (eBPF Foundation)
  2025-01-03 12:54   ` Alexis Lothoré
                     ` (2 more replies)
  2025-01-03 10:10 ` [PATCH 3/3] selftests/bpf: Migrate test_xdp_redirect.c to test_xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
  2025-01-03 10:20 ` [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet
  3 siblings, 3 replies; 11+ messages in thread
From: Bastien Curutchet (eBPF Foundation) @ 2025-01-03 10:10 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
	Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Mykola Lysenko, Shuah Khan
  Cc: Thomas Petazzoni, Alexis Lothore, netdev, bpf, linux-kselftest,
	linux-kernel, Bastien Curutchet (eBPF Foundation)

test_xdp_redirect.sh can't be used by the BPF CI.

Migrate test_xdp_redirect.sh into two new test cases in
xdp_do_redirect.c. They use the same network topology and the same BPF
programs located in progs/test_xdp_redirect.c and progs/xdp_dummy.c.
Remove test_xdp_redirect.sh and its Makefile entry.

Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
---
 tools/testing/selftests/bpf/Makefile               |   1 -
 .../selftests/bpf/prog_tests/xdp_do_redirect.c     | 193 +++++++++++++++++++++
 tools/testing/selftests/bpf/test_xdp_redirect.sh   |  79 ---------
 3 files changed, 193 insertions(+), 80 deletions(-)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index 9e870e519c30e4a241ce05491743e1784af2bd8b..3cf571ccbde6e13535c1d199759221804710b9ff 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -127,7 +127,6 @@ TEST_FILES = xsk_prereqs.sh $(wildcard progs/btf_dump_test_case_*.c)
 
 # Order correspond to 'make run_tests' order
 TEST_PROGS := test_kmod.sh \
-	test_xdp_redirect.sh \
 	test_xdp_redirect_multi.sh \
 	test_xdp_meta.sh \
 	test_tunnel.sh \
diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c b/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
index d12f926b4b8b1fcbc2a88ef7e3bd20ef2cbbd72c..47b0ea84ae57b2c6ba3388ba865bfdf4bbe2146c 100644
--- a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
@@ -11,6 +11,8 @@
 #include <bpf/bpf_endian.h>
 #include <uapi/linux/netdev.h>
 #include "test_xdp_do_redirect.skel.h"
+#include "test_xdp_redirect.skel.h"
+#include "xdp_dummy.skel.h"
 
 struct udp_packet {
 	struct ethhdr eth;
@@ -246,3 +248,194 @@ void test_xdp_do_redirect(void)
 	SYS_NOFAIL("ip netns del testns");
 	test_xdp_do_redirect__destroy(skel);
 }
+
+#define NS_NB		3
+#define NS0		"NS0"
+#define NS1		"NS1"
+#define NS2		"NS2"
+#define IPV4_NETWORK	"10.1.1"
+
+struct test_data {
+	struct netns_obj *ns[NS_NB];
+	u32 xdp_flags;
+};
+
+static void cleanup(struct test_data *data)
+{
+	int i;
+
+	for (i = 0; i < NS_NB; i++)
+		netns_free(data->ns[i]);
+}
+
+/**
+ * ping_setup -
+ * Create two veth peers and forward packets in-between using XDP
+ *
+ *    ------------           ------------
+ *    |    NS1   |           |    NS2   |
+ *    |   veth0  |           |   veth0  |
+ *    | 10.1.1.1 |           | 10.1.1.2 |
+ *    -----|------           ------|-----
+ *         |                       |
+ *         |                       |
+ *    -----|-----------------------|-------
+ *    |  veth1                   veth2    |
+ *    | (id:111)                (id:222)  |
+ *    |    |                        |     |
+ *    |    ----- xdp forwarding -----     |
+ *    |                                   |
+ *    |               NS0                 |
+ *    -------------------------------------
+ */
+static int ping_setup(struct test_data *data)
+{
+	struct nstoken *nstoken = NULL;
+	int i;
+
+	data->ns[0] = netns_new(NS0, false);
+	if (!ASSERT_OK_PTR(data->ns[0], "create ns"))
+		return -1;
+
+	for (i = 1; i < NS_NB; i++) {
+		char ns_name[4] = {};
+
+		snprintf(ns_name, 4, "NS%d", i);
+		data->ns[i] = netns_new(ns_name, false);
+		if (!ASSERT_OK_PTR(data->ns[i], "create ns"))
+			goto fail;
+
+		nstoken = open_netns(NS0);
+		if (!ASSERT_OK_PTR(nstoken, "open NS0"))
+			goto fail;
+
+		SYS(fail, "ip link add veth%d index %d%d%d type veth peer name veth0 netns %s",
+		    i, i, i, i, ns_name);
+		SYS(fail, "ip link set veth%d up", i);
+		close_netns(nstoken);
+
+		nstoken = open_netns(ns_name);
+		if (!ASSERT_OK_PTR(nstoken, "open ns"))
+			goto fail;
+
+		SYS(fail, "ip addr add %s.%d/24 dev veth0", IPV4_NETWORK, i);
+		SYS(fail, "ip link set veth0 up");
+		close_netns(nstoken);
+	}
+
+	return 0;
+
+fail:
+	close_netns(nstoken);
+	cleanup(data);
+	return -1;
+}
+
+static void ping_test(struct test_data *data)
+{
+	struct bpf_program *prog_to_111, *prog_to_222, *dummy_prog;
+	struct test_xdp_redirect *skel = NULL;
+	struct xdp_dummy *skel_dummy = NULL;
+	struct nstoken *nstoken = NULL;
+	int i, ret;
+
+	skel_dummy = xdp_dummy__open_and_load();
+	if (!ASSERT_OK_PTR(skel_dummy, "open and load xdp_dummy skeleton"))
+		goto close;
+
+	dummy_prog = bpf_object__find_program_by_name(skel_dummy->obj, "xdp_dummy_prog");
+	if (!ASSERT_OK_PTR(dummy_prog, "get dummy_prog"))
+		goto close;
+
+	for (i = 1; i < NS_NB; i++) {
+		char ns_name[4] = {};
+
+		snprintf(ns_name, 4, "NS%d", i);
+		nstoken = open_netns(ns_name);
+		if (!ASSERT_OK_PTR(nstoken, "open ns"))
+			goto close;
+
+		ret = bpf_xdp_attach(if_nametoindex("veth0"),
+				     bpf_program__fd(dummy_prog),
+				     data->xdp_flags, NULL);
+		if (!ASSERT_GE(ret, 0, "bpf_xdp_attach dummy_prog"))
+			goto close;
+
+		close_netns(nstoken);
+	}
+
+	skel = test_xdp_redirect__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open and load skeleton"))
+		goto close;
+
+	prog_to_111 = bpf_object__find_program_by_name(skel->obj, "xdp_redirect_to_111");
+	if (!ASSERT_OK_PTR(prog_to_111, "get redirect_to_111 prog"))
+		goto close;
+
+	prog_to_222 = bpf_object__find_program_by_name(skel->obj, "xdp_redirect_to_222");
+	if (!ASSERT_OK_PTR(prog_to_222, "get redirect_to_222 prog"))
+		goto close;
+
+	nstoken = open_netns(NS0);
+	if (!ASSERT_OK_PTR(nstoken, "open NS0"))
+		goto close;
+
+	ret = bpf_xdp_attach(if_nametoindex("veth2"),
+			     bpf_program__fd(prog_to_111),
+			     data->xdp_flags, NULL);
+	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
+		goto close;
+
+	ret = bpf_xdp_attach(if_nametoindex("veth1"),
+			     bpf_program__fd(prog_to_222),
+			     data->xdp_flags, NULL);
+	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
+		goto close;
+
+	close_netns(nstoken);
+
+	nstoken = open_netns(NS1);
+	if (!ASSERT_OK_PTR(nstoken, "open NS1"))
+		goto close;
+
+	SYS(close, "ping -c 1 %s.2", IPV4_NETWORK);
+
+	close_netns(nstoken);
+
+	nstoken = open_netns(NS2);
+	if (!ASSERT_OK_PTR(nstoken, "open NS2"))
+		goto close;
+
+	SYS(close, "ping -c 1 %s.1", IPV4_NETWORK);
+
+close:
+	close_netns(nstoken);
+	xdp_dummy__destroy(skel_dummy);
+	test_xdp_redirect__destroy(skel);
+}
+
+
+void test_xdp_generic_redirect_ping(void)
+{
+	struct test_data data = {};
+
+	if (ping_setup(&data) < 0)
+		return;
+
+	data.xdp_flags = XDP_FLAGS_SKB_MODE;
+	ping_test(&data);
+	cleanup(&data);
+}
+
+void test_xdp_drv_redirect_ping(void)
+{
+	struct test_data data = {};
+
+	if (ping_setup(&data) < 0)
+		return;
+
+	data.xdp_flags = XDP_FLAGS_DRV_MODE;
+	ping_test(&data);
+	cleanup(&data);
+}
+
diff --git a/tools/testing/selftests/bpf/test_xdp_redirect.sh b/tools/testing/selftests/bpf/test_xdp_redirect.sh
deleted file mode 100755
index 3c61a1c22b084aa5ca824ec5e8057aa2fee12b71..0000000000000000000000000000000000000000
--- a/tools/testing/selftests/bpf/test_xdp_redirect.sh
+++ /dev/null
@@ -1,79 +0,0 @@
-#!/bin/bash
-# Create 2 namespaces with two veth peers, and
-# forward packets in-between using generic XDP
-#
-# NS1(veth11)     NS2(veth22)
-#     |               |
-#     |               |
-#   (veth1, ------ (veth2,
-#   id:111)         id:222)
-#     | xdp forwarding |
-#     ------------------
-
-readonly NS1="ns1-$(mktemp -u XXXXXX)"
-readonly NS2="ns2-$(mktemp -u XXXXXX)"
-ret=0
-
-setup()
-{
-
-	local xdpmode=$1
-
-	ip netns add ${NS1}
-	ip netns add ${NS2}
-
-	ip link add veth1 index 111 type veth peer name veth11 netns ${NS1}
-	ip link add veth2 index 222 type veth peer name veth22 netns ${NS2}
-
-	ip link set veth1 up
-	ip link set veth2 up
-	ip -n ${NS1} link set dev veth11 up
-	ip -n ${NS2} link set dev veth22 up
-
-	ip -n ${NS1} addr add 10.1.1.11/24 dev veth11
-	ip -n ${NS2} addr add 10.1.1.22/24 dev veth22
-}
-
-cleanup()
-{
-	ip link del veth1 2> /dev/null
-	ip link del veth2 2> /dev/null
-	ip netns del ${NS1} 2> /dev/null
-	ip netns del ${NS2} 2> /dev/null
-}
-
-test_xdp_redirect()
-{
-	local xdpmode=$1
-
-	setup
-
-	ip link set dev veth1 $xdpmode off &> /dev/null
-	if [ $? -ne 0 ];then
-		echo "selftests: test_xdp_redirect $xdpmode [SKIP]"
-		return 0
-	fi
-
-	ip -n ${NS1} link set veth11 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null
-	ip -n ${NS2} link set veth22 $xdpmode obj xdp_dummy.bpf.o sec xdp &> /dev/null
-	ip link set dev veth1 $xdpmode obj test_xdp_redirect.bpf.o program xdp_redirect_to_222 &> /dev/null
-	ip link set dev veth2 $xdpmode obj test_xdp_redirect.bpf.o program xdp_redirect_to_111 &> /dev/null
-
-	if ip netns exec ${NS1} ping -c 1 10.1.1.22 &> /dev/null &&
-	   ip netns exec ${NS2} ping -c 1 10.1.1.11 &> /dev/null; then
-		echo "selftests: test_xdp_redirect $xdpmode [PASS]";
-	else
-		ret=1
-		echo "selftests: test_xdp_redirect $xdpmode [FAILED]";
-	fi
-
-	cleanup
-}
-
-set -e
-trap cleanup 2 3 6 9
-
-test_xdp_redirect xdpgeneric
-test_xdp_redirect xdpdrv
-
-exit $ret

-- 
2.47.1


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

* [PATCH 3/3] selftests/bpf: Migrate test_xdp_redirect.c to test_xdp_do_redirect.c
  2025-01-03 10:10 [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet (eBPF Foundation)
  2025-01-03 10:10 ` [PATCH 1/3] selftests/bpf: test_xdp_redirect: Rename BPF sections Bastien Curutchet (eBPF Foundation)
  2025-01-03 10:10 ` [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
@ 2025-01-03 10:10 ` Bastien Curutchet (eBPF Foundation)
  2025-01-03 10:20 ` [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet
  3 siblings, 0 replies; 11+ messages in thread
From: Bastien Curutchet (eBPF Foundation) @ 2025-01-03 10:10 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
	Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Mykola Lysenko, Shuah Khan
  Cc: Thomas Petazzoni, Alexis Lothore, netdev, bpf, linux-kselftest,
	linux-kernel, Bastien Curutchet (eBPF Foundation)

prog_tests/xdp_do_redirect.c is the only user of the BPF programs
located in progs/test_xdp_do_redirect.c and progs/test_xdp_redirect.c.
There is no need to keep both files with such close names.

Move test_xdp_redirect.c contents to test_xdp_do_redirect.c and remove
progs/test_xdp_redirect.c

Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
---
 .../selftests/bpf/prog_tests/xdp_do_redirect.c     |  7 +++---
 .../selftests/bpf/progs/test_xdp_do_redirect.c     | 12 ++++++++++
 .../selftests/bpf/progs/test_xdp_redirect.c        | 26 ----------------------
 3 files changed, 15 insertions(+), 30 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c b/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
index 47b0ea84ae57b2c6ba3388ba865bfdf4bbe2146c..9faaacdcdb019c629ded87a57b28052923c7cea8 100644
--- a/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_do_redirect.c
@@ -11,7 +11,6 @@
 #include <bpf/bpf_endian.h>
 #include <uapi/linux/netdev.h>
 #include "test_xdp_do_redirect.skel.h"
-#include "test_xdp_redirect.skel.h"
 #include "xdp_dummy.skel.h"
 
 struct udp_packet {
@@ -334,7 +333,7 @@ static int ping_setup(struct test_data *data)
 static void ping_test(struct test_data *data)
 {
 	struct bpf_program *prog_to_111, *prog_to_222, *dummy_prog;
-	struct test_xdp_redirect *skel = NULL;
+	struct test_xdp_do_redirect *skel = NULL;
 	struct xdp_dummy *skel_dummy = NULL;
 	struct nstoken *nstoken = NULL;
 	int i, ret;
@@ -364,7 +363,7 @@ static void ping_test(struct test_data *data)
 		close_netns(nstoken);
 	}
 
-	skel = test_xdp_redirect__open_and_load();
+	skel = test_xdp_do_redirect__open_and_load();
 	if (!ASSERT_OK_PTR(skel, "open and load skeleton"))
 		goto close;
 
@@ -411,7 +410,7 @@ static void ping_test(struct test_data *data)
 close:
 	close_netns(nstoken);
 	xdp_dummy__destroy(skel_dummy);
-	test_xdp_redirect__destroy(skel);
+	test_xdp_do_redirect__destroy(skel);
 }
 
 
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_do_redirect.c b/tools/testing/selftests/bpf/progs/test_xdp_do_redirect.c
index 3abf068b84464ce0460a671abc4dfb97e1f2be4a..5928ed0911caf4d5a71ef37889d9315bfa6623ae 100644
--- a/tools/testing/selftests/bpf/progs/test_xdp_do_redirect.c
+++ b/tools/testing/selftests/bpf/progs/test_xdp_do_redirect.c
@@ -98,6 +98,18 @@ int xdp_count_pkts(struct xdp_md *xdp)
 	return XDP_DROP;
 }
 
+SEC("xdp")
+int xdp_redirect_to_111(struct xdp_md *xdp)
+{
+	return bpf_redirect(111, 0);
+}
+
+SEC("xdp")
+int xdp_redirect_to_222(struct xdp_md *xdp)
+{
+	return bpf_redirect(222, 0);
+}
+
 SEC("tc")
 int tc_count_pkts(struct __sk_buff *skb)
 {
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_redirect.c b/tools/testing/selftests/bpf/progs/test_xdp_redirect.c
deleted file mode 100644
index 7025aee08a001cfc42e52174a4acce7869dd331b..0000000000000000000000000000000000000000
--- a/tools/testing/selftests/bpf/progs/test_xdp_redirect.c
+++ /dev/null
@@ -1,26 +0,0 @@
-/* Copyright (c) 2017 VMware
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- */
-#include <linux/bpf.h>
-#include <bpf/bpf_helpers.h>
-
-SEC("xdp")
-int xdp_redirect_to_111(struct xdp_md *xdp)
-{
-	return bpf_redirect(111, 0);
-}
-SEC("xdp")
-int xdp_redirect_to_222(struct xdp_md *xdp)
-{
-	return bpf_redirect(222, 0);
-}
-
-char _license[] SEC("license") = "GPL";

-- 
2.47.1


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

* Re: [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs
  2025-01-03 10:10 [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet (eBPF Foundation)
                   ` (2 preceding siblings ...)
  2025-01-03 10:10 ` [PATCH 3/3] selftests/bpf: Migrate test_xdp_redirect.c to test_xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
@ 2025-01-03 10:20 ` Bastien Curutchet
  3 siblings, 0 replies; 11+ messages in thread
From: Bastien Curutchet @ 2025-01-03 10:20 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman, Song Liu,
	Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	Mykola Lysenko, Shuah Khan
  Cc: Thomas Petazzoni, Alexis Lothore, netdev, bpf, linux-kselftest,
	linux-kernel

Hi all,


On 1/3/25 11:10 AM, Bastien Curutchet (eBPF Foundation) wrote:
> Hi all,
> 
> This patch series continues the work to migrate the *.sh tests into
> prog_tests.
> 
> test_xdp_redirect.sh tests the XDP redirections done through
> bpf_redirect().
> 
> These XDP redirections are already tested by prog_tests/xdp_do_redirect.c
> but IMO it doesn't cover the exact same code path because
> xdp_do_redirect.c uses bpf_prog_test_run_opts() to trigger redirections
> of 'fake packets' while test_xdp_redirect.sh redirects packets coming
> from the network. Also, the test_xdp_redirect.sh script tests the
> redirections with both SKB and DRV modes while xdp_do_redirect.c only
> tests the DRV mode.
> 
> The patch series adds two new test cases in prog_tests/xdp_do_redirect.c
> to replace the test_xdp_redirect.sh script.
> 
> Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
> ---

I realize I forgot to mention 'bpf-next', sorry about that.
This work is based on the bpf-next_base branch.

Best regards,
Bastien

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

* Re: [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
  2025-01-03 10:10 ` [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
@ 2025-01-03 12:54   ` Alexis Lothoré
  2025-01-06  7:54     ` Bastien Curutchet
  2025-01-03 13:17   ` Alexis Lothoré
  2025-01-07 19:48   ` Martin KaFai Lau
  2 siblings, 1 reply; 11+ messages in thread
From: Alexis Lothoré @ 2025-01-03 12:54 UTC (permalink / raw)
  To: Bastien Curutchet (eBPF Foundation), Alexei Starovoitov,
	Daniel Borkmann, David S. Miller, Jakub Kicinski,
	Jesper Dangaard Brouer, John Fastabend, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan
  Cc: Thomas Petazzoni, netdev, bpf, linux-kselftest, linux-kernel

Hi Bastien,

On 1/3/25 11:10, Bastien Curutchet (eBPF Foundation) wrote:

[...]

> +		SYS(fail, "ip link add veth%d index %d%d%d type veth peer name veth0 netns %s",
> +		    i, i, i, i, ns_name);

nit: since you have to run an ip command through SYS anyway, you can reduce the
open ns/run command/close ns dance (and all the resulting error checks) by
running directly `SYS("ip netns exec %s ip link add [...]", NS0, [...])`

[...]

> +	ret = bpf_xdp_attach(if_nametoindex("veth2"),
> +			     bpf_program__fd(prog_to_111),
> +			     data->xdp_flags, NULL);

nit: since we are setting static if index at veth creation (which looks needed
for this test), the if_nametoindex could be replaced by the corresponding index,
which could be directly a define

> +	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
> +		goto close;
> +
> +	ret = bpf_xdp_attach(if_nametoindex("veth1"),
> +			     bpf_program__fd(prog_to_222),
> +			     data->xdp_flags, NULL);
> +	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
> +		goto close;
> +
> +	close_netns(nstoken);
> +
> +	nstoken = open_netns(NS1);
> +	if (!ASSERT_OK_PTR(nstoken, "open NS1"))
> +		goto close;
> +
> +	SYS(close, "ping -c 1 %s.2", IPV4_NETWORK);
> +
> +	close_netns(nstoken);
> +
> +	nstoken = open_netns(NS2);
> +	if (!ASSERT_OK_PTR(nstoken, "open NS2"))
> +		goto close;
> +
> +	SYS(close, "ping -c 1 %s.1", IPV4_NETWORK);

Is it really useful to check ping originating from both interfaces, isn´t a
single ping able to stimulate programs attached to both veth0 ?

Aside from those minor points, LGTM :)

-- 
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* Re: [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
  2025-01-03 10:10 ` [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
  2025-01-03 12:54   ` Alexis Lothoré
@ 2025-01-03 13:17   ` Alexis Lothoré
  2025-01-06  8:24     ` Bastien Curutchet
  2025-01-07 19:48   ` Martin KaFai Lau
  2 siblings, 1 reply; 11+ messages in thread
From: Alexis Lothoré @ 2025-01-03 13:17 UTC (permalink / raw)
  To: Bastien Curutchet (eBPF Foundation), Alexei Starovoitov,
	Daniel Borkmann, David S. Miller, Jakub Kicinski,
	Jesper Dangaard Brouer, John Fastabend, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan
  Cc: Thomas Petazzoni, netdev, bpf, linux-kselftest, linux-kernel

On 1/3/25 11:10, Bastien Curutchet (eBPF Foundation) wrote:
> test_xdp_redirect.sh can't be used by the BPF CI.

> +	dummy_prog = bpf_object__find_program_by_name(skel_dummy->obj, "xdp_dummy_prog");
Also missed this one: why not using directly skel_dummy->progs.xdp_dummy_prog ?
(same for all progs below)

Thanks,
Alexis

-- 
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* Re: [PATCH 1/3] selftests/bpf: test_xdp_redirect: Rename BPF sections
  2025-01-03 10:10 ` [PATCH 1/3] selftests/bpf: test_xdp_redirect: Rename BPF sections Bastien Curutchet (eBPF Foundation)
@ 2025-01-03 13:18   ` Alexis Lothoré
  0 siblings, 0 replies; 11+ messages in thread
From: Alexis Lothoré @ 2025-01-03 13:18 UTC (permalink / raw)
  To: Bastien Curutchet (eBPF Foundation), Alexei Starovoitov,
	Daniel Borkmann, David S. Miller, Jakub Kicinski,
	Jesper Dangaard Brouer, John Fastabend, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan
  Cc: Thomas Petazzoni, netdev, bpf, linux-kselftest, linux-kernel

On 1/3/25 11:10, Bastien Curutchet (eBPF Foundation) wrote:
> SEC("redirect_to_111") and SEC("redirect_to_222") can't be loaded by the
> __load() helper.
> 
> Rename both sections SEC("xdp") so it can be interpreted by the __load()
> helper in upcoming patch.
> Update the test_xdp_redirect.sh to use the program name instead of the
> section name to load the BPF program.
> 
> Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>

LGTM :)
Reviewed-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>

-- 
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

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

* Re: [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
  2025-01-03 12:54   ` Alexis Lothoré
@ 2025-01-06  7:54     ` Bastien Curutchet
  0 siblings, 0 replies; 11+ messages in thread
From: Bastien Curutchet @ 2025-01-06  7:54 UTC (permalink / raw)
  To: Alexis Lothoré, Alexei Starovoitov, Daniel Borkmann,
	David S. Miller, Jakub Kicinski, Jesper Dangaard Brouer,
	John Fastabend, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, KP Singh,
	Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan
  Cc: Thomas Petazzoni, netdev, bpf, linux-kselftest, linux-kernel

Hi Alexis,

On 1/3/25 1:54 PM, Alexis Lothoré wrote:
> Hi Bastien,
> 
> On 1/3/25 11:10, Bastien Curutchet (eBPF Foundation) wrote:
> 
> [...]
> 
>> +		SYS(fail, "ip link add veth%d index %d%d%d type veth peer name veth0 netns %s",
>> +		    i, i, i, i, ns_name);
> 
> nit: since you have to run an ip command through SYS anyway, you can reduce the
> open ns/run command/close ns dance (and all the resulting error checks) by
> running directly `SYS("ip netns exec %s ip link add [...]", NS0, [...])`
> 
> [...]
> 

True, thanks.

>> +	ret = bpf_xdp_attach(if_nametoindex("veth2"),
>> +			     bpf_program__fd(prog_to_111),
>> +			     data->xdp_flags, NULL);
> 
> nit: since we are setting static if index at veth creation (which looks needed
> for this test), the if_nametoindex could be replaced by the corresponding index,
> which could be directly a define
> 

Also true, thanks.

>> +	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
>> +		goto close;
>> +
>> +	ret = bpf_xdp_attach(if_nametoindex("veth1"),
>> +			     bpf_program__fd(prog_to_222),
>> +			     data->xdp_flags, NULL);
>> +	if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
>> +		goto close;
>> +
>> +	close_netns(nstoken);
>> +
>> +	nstoken = open_netns(NS1);
>> +	if (!ASSERT_OK_PTR(nstoken, "open NS1"))
>> +		goto close;
>> +
>> +	SYS(close, "ping -c 1 %s.2", IPV4_NETWORK);
>> +
>> +	close_netns(nstoken);
>> +
>> +	nstoken = open_netns(NS2);
>> +	if (!ASSERT_OK_PTR(nstoken, "open NS2"))
>> +		goto close;
>> +
>> +	SYS(close, "ping -c 1 %s.1", IPV4_NETWORK);
> 
> Is it really useful to check ping originating from both interfaces, isn´t a
> single ping able to stimulate programs attached to both veth0 ?
> 

Indeed, I think a single ping would be enough, I just wanted to stick 
with what test_xdp_redirect.sh does.

> Aside from those minor points, LGTM :)
> 

Best regards,
Bastien

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

* Re: [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
  2025-01-03 13:17   ` Alexis Lothoré
@ 2025-01-06  8:24     ` Bastien Curutchet
  0 siblings, 0 replies; 11+ messages in thread
From: Bastien Curutchet @ 2025-01-06  8:24 UTC (permalink / raw)
  To: Alexis Lothoré, Alexei Starovoitov, Daniel Borkmann,
	David S. Miller, Jakub Kicinski, Jesper Dangaard Brouer,
	John Fastabend, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, KP Singh,
	Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan
  Cc: Thomas Petazzoni, netdev, bpf, linux-kselftest, linux-kernel

Hi again,

On 1/3/25 2:17 PM, Alexis Lothoré wrote:
> On 1/3/25 11:10, Bastien Curutchet (eBPF Foundation) wrote:
>> test_xdp_redirect.sh can't be used by the BPF CI.
> 
>> +	dummy_prog = bpf_object__find_program_by_name(skel_dummy->obj, "xdp_dummy_prog");
> Also missed this one: why not using directly skel_dummy->progs.xdp_dummy_prog ?
> (same for all progs below)
> 

Indeed, that would be more straightforward

Thanks,
Bastien

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

* Re: [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c
  2025-01-03 10:10 ` [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
  2025-01-03 12:54   ` Alexis Lothoré
  2025-01-03 13:17   ` Alexis Lothoré
@ 2025-01-07 19:48   ` Martin KaFai Lau
  2 siblings, 0 replies; 11+ messages in thread
From: Martin KaFai Lau @ 2025-01-07 19:48 UTC (permalink / raw)
  To: Bastien Curutchet (eBPF Foundation)
  Cc: Alexei Starovoitov, Daniel Borkmann, David S. Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Andrii Nakryiko, Eduard Zingerman, Song Liu, Yonghong Song,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa, Mykola Lysenko,
	Shuah Khan, Thomas Petazzoni, Alexis Lothore, netdev, bpf,
	linux-kselftest, linux-kernel

On 1/3/25 2:10 AM, Bastien Curutchet (eBPF Foundation) wrote:
> +static int ping_setup(struct test_data *data)
> +{
> +	struct nstoken *nstoken = NULL;
> +	int i;
> +
> +	data->ns[0] = netns_new(NS0, false);
> +	if (!ASSERT_OK_PTR(data->ns[0], "create ns"))
> +		return -1;
> +
> +	for (i = 1; i < NS_NB; i++) {
> +		char ns_name[4] = {};
> +
> +		snprintf(ns_name, 4, "NS%d", i);
> +		data->ns[i] = netns_new(ns_name, false);
> +		if (!ASSERT_OK_PTR(data->ns[i], "create ns"))
> +			goto fail;
> +
> +		nstoken = open_netns(NS0);
> +		if (!ASSERT_OK_PTR(nstoken, "open NS0"))
> +			goto fail;
> +
> +		SYS(fail, "ip link add veth%d index %d%d%d type veth peer name veth0 netns %s",
> +		    i, i, i, i, ns_name);
> +		SYS(fail, "ip link set veth%d up", i);
> +		close_netns(nstoken);
> +
> +		nstoken = open_netns(ns_name);
> +		if (!ASSERT_OK_PTR(nstoken, "open ns"))
> +			goto fail;
> +
> +		SYS(fail, "ip addr add %s.%d/24 dev veth0", IPV4_NETWORK, i);
> +		SYS(fail, "ip link set veth0 up");
> +		close_netns(nstoken);

		nstoken = NULL;

Otherwise, the other "goto fail;" of this loop will close and free the already 
closed nstoken again.

Some of the other close_netns(nstoken) in this patch may have similar issue.

> +	}
> +
> +	return 0;
> +
> +fail:
> +	close_netns(nstoken);
> +	cleanup(data);
> +	return -1;
> +}


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

end of thread, other threads:[~2025-01-07 19:48 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-03 10:10 [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet (eBPF Foundation)
2025-01-03 10:10 ` [PATCH 1/3] selftests/bpf: test_xdp_redirect: Rename BPF sections Bastien Curutchet (eBPF Foundation)
2025-01-03 13:18   ` Alexis Lothoré
2025-01-03 10:10 ` [PATCH 2/3] selftests/bpf: Migrate test_xdp_redirect.sh to xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
2025-01-03 12:54   ` Alexis Lothoré
2025-01-06  7:54     ` Bastien Curutchet
2025-01-03 13:17   ` Alexis Lothoré
2025-01-06  8:24     ` Bastien Curutchet
2025-01-07 19:48   ` Martin KaFai Lau
2025-01-03 10:10 ` [PATCH 3/3] selftests/bpf: Migrate test_xdp_redirect.c to test_xdp_do_redirect.c Bastien Curutchet (eBPF Foundation)
2025-01-03 10:20 ` [PATCH 0/3] selftests: bpf: Migrate test_xdp_redirect.sh to test_progs Bastien Curutchet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox