@@ -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;
@@ -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);
@@ -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;