[PATCH bpf-next 6/9] selftests/bpf: Export cleanup_netns helper

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Geliang Tang <tanggeliang@xxxxxxxxxx>

This patch adds a new struct member name in struct nstoken, to save
the name of the given network namespace. It dups the name string in
open_netns() and freed in close_netns().

Then move cleanup_netns() from mptcp.c into network_helpers.c as a
public helper. In it the newly added name field is passed to command
"ip netns del" to delete this network namespace. This makes it more
flexible.

Signed-off-by: Geliang Tang <tanggeliang@xxxxxxxxxx>
---
 tools/testing/selftests/bpf/network_helpers.c | 21 +++++++++++++++++++
 tools/testing/selftests/bpf/network_helpers.h |  1 +
 .../testing/selftests/bpf/prog_tests/mptcp.c  |  8 -------
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index c115aeca0a66..0b25b008f4f6 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -444,6 +444,7 @@ char *ping_command(int family)
 
 struct nstoken {
 	int orig_netns_fd;
+	char *name;
 };
 
 struct nstoken *open_netns(const char *name)
@@ -459,6 +460,13 @@ struct nstoken *open_netns(const char *name)
 		return NULL;
 	}
 
+	token->name = strdup(name);
+	if (!token->name) {
+		log_err("Failed to dup name");
+		free(token);
+		return NULL;
+	}
+
 	token->orig_netns_fd = open("/proc/self/ns/net", O_RDONLY);
 	if (token->orig_netns_fd == -1) {
 		log_err("Failed to open(/proc/self/ns/net)");
@@ -483,6 +491,7 @@ struct nstoken *open_netns(const char *name)
 fail:
 	if (token->orig_netns_fd != -1)
 		close(token->orig_netns_fd);
+	free(token->name);
 	free(token);
 	return NULL;
 }
@@ -494,10 +503,22 @@ void close_netns(struct nstoken *token)
 
 	if (setns(token->orig_netns_fd, CLONE_NEWNET))
 		log_err("Failed to setns(orig_netns_fd)");
+	if (token->name)
+		free(token->name);
 	close(token->orig_netns_fd);
 	free(token);
 }
 
+void cleanup_netns(struct nstoken *token)
+{
+	if (!token)
+		return;
+
+	if (SYS_NOFAIL("ip netns del %s", token->name))
+		log_err("del netns %s failed", token->name);
+	close_netns(token);
+}
+
 struct nstoken *create_netns(const char *name)
 {
 	struct nstoken *token = NULL;
diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h
index 5c0b082153fd..7ed6d78b3744 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);
+void cleanup_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 ae42ae41f4b4..11267f987e7e 100644
--- a/tools/testing/selftests/bpf/prog_tests/mptcp.c
+++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c
@@ -79,14 +79,6 @@ struct mptcp_storage {
 	char ca_name[TCP_CA_NAME_MAX];
 };
 
-static void cleanup_netns(struct nstoken *nstoken)
-{
-	if (nstoken)
-		close_netns(nstoken);
-
-	SYS_NOFAIL("ip netns del %s", NS_TEST);
-}
-
 static int start_mptcp_server(int family, const char *addr_str, __u16 port,
 			      int timeout_ms)
 {
-- 
2.43.0





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux