diff mbox series

[bpf-next,4/9] selftests/bpf: Export create_netns helper

Message ID 659d1fe7e47b6c63f9648cf7c2433e29dadf458f.1715751995.git.tanggeliang@kylinos.cn (mailing list archive)
State New
Headers show
Series add netns helpers | expand

Commit Message

Geliang Tang May 15, 2024, 5:59 a.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

This patch moves create_netns() from mptcp.c into network_helpers.c, and
export it in network_helpers.h as a public helper.

The new helper accepts a string parameter, and uses SYS_NOFAIL() instead
of SYS() to execute the command, since SYS() deponds on test__fail()
which is defined in test_progs.c. SYS() is not suitable to be used in
network_helpers.c.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/bpf/network_helpers.c | 27 +++++++++++++++++++
 tools/testing/selftests/bpf/network_helpers.h |  1 +
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 14 ++--------
 3 files changed, 30 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index 919bb2a0c6a6..c115aeca0a66 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -498,6 +498,33 @@  void close_netns(struct nstoken *token)
 	free(token);
 }
 
+struct nstoken *create_netns(const char *name)
+{
+	struct nstoken *token = NULL;
+
+	if (SYS_NOFAIL("ip netns add %s", name)) {
+		log_err("add netns %s failed", name);
+		goto fail;
+	}
+
+	if (SYS_NOFAIL("ip -net %s link set dev lo up", name)) {
+		log_err("set dev lo up failed");
+		goto fail;
+	}
+
+	token = open_netns(name);
+	if (!token) {
+		log_err("open netns %s failed", name);
+		goto fail;
+	}
+
+	return token;
+
+fail:
+	SYS_NOFAIL("ip netns del %s", name);
+	return NULL;
+}
+
 int get_socket_local_port(int sock_fd)
 {
 	struct sockaddr_storage addr;
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index b087330f2861..5c0b082153fd 100644
--- a/tools/testing/selftests/bpf/network_helpers.h
+++ b/tools/testing/selftests/bpf/network_helpers.h
@@ -81,6 +81,7 @@  struct nstoken;
  */
 struct nstoken *open_netns(const char *name);
 void close_netns(struct nstoken *token);
+struct nstoken *create_netns(const char *name);
 int send_recv_data(int lfd, int fd, uint32_t total_bytes);
 int unshare_netns(void);
 
diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c
index 793b4b9c2bd2..ae42ae41f4b4 100644
--- a/tools/testing/selftests/bpf/prog_tests/mptcp.c
+++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c
@@ -79,16 +79,6 @@  struct mptcp_storage {
 	char ca_name[TCP_CA_NAME_MAX];
 };
 
-static struct nstoken *create_netns(void)
-{
-	SYS(fail, "ip netns add %s", NS_TEST);
-	SYS(fail, "ip -net %s link set dev lo up", NS_TEST);
-
-	return open_netns(NS_TEST);
-fail:
-	return NULL;
-}
-
 static void cleanup_netns(struct nstoken *nstoken)
 {
 	if (nstoken)
@@ -228,7 +218,7 @@  static void test_base(void)
 	if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
 		return;
 
-	nstoken = create_netns();
+	nstoken = create_netns(NS_TEST);
 	if (!ASSERT_OK_PTR(nstoken, "create_netns"))
 		goto fail;
 
@@ -344,7 +334,7 @@  static void test_mptcpify(void)
 	if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
 		return;
 
-	nstoken = create_netns();
+	nstoken = create_netns(NS_TEST);
 	if (!ASSERT_OK_PTR(nstoken, "create_netns"))
 		goto fail;