From: Geliang Tang tanggeliang@kylinos.cn
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@kylinos.cn --- 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 5555b109899f..2293eac2dcec 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,6 +503,8 @@ 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); } @@ -526,6 +537,16 @@ struct nstoken *create_netns(const char *name) return NULL; }
+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); +} + 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 5c0b082153fd..42d503a9f55e 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -82,6 +82,7 @@ struct nstoken; struct nstoken *open_netns(const char *name); void close_netns(struct nstoken *token); struct nstoken *create_netns(const char *name); +void cleanup_netns(struct nstoken *token); 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 abb4372114b6..c7f61fd9e7a5 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) {