[PATCH mptcp-next v2] Squash to "selftests/bpf: Add bpf_first test"

Geliang Tang posted 1 patch 11 months, 1 week ago
Failed in applying to current master (apply log)
.../testing/selftests/bpf/prog_tests/mptcp.c  | 50 ++++++++-----------
1 file changed, 22 insertions(+), 28 deletions(-)
[PATCH mptcp-next v2] Squash to "selftests/bpf: Add bpf_first test"
Posted by Geliang Tang 11 months, 1 week ago
Run mptcp sched test in a dedicated netns.

Signed-off-by: Geliang Tang <geliang.tang@suse.com>
---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 50 ++++++++-----------
 1 file changed, 22 insertions(+), 28 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c
index 77a6997fc82f..bea9c799a531 100644
--- a/tools/testing/selftests/bpf/prog_tests/mptcp.c
+++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c
@@ -253,40 +253,34 @@ static void send_data(int lfd, int fd)
 #define ADDR_1	"10.0.1.1"
 #define ADDR_2	"10.0.1.2"
 
-static void sched_init(char *flags, char *sched)
+static void sched_init(char *ns, char *flags, char *sched)
 {
-	char cmd[64];
-
-	system("ip link add veth1 type veth peer name veth2");
-	snprintf(cmd, sizeof(cmd), "ip addr add %s/24 dev veth1", ADDR_1);
-	system(cmd);
-	system("ip link set veth1 up");
-	snprintf(cmd, sizeof(cmd), "ip addr add %s/24 dev veth2", ADDR_2);
-	system(cmd);
-	system("ip link set veth2 up");
-
-	snprintf(cmd, sizeof(cmd), "ip mptcp endpoint add %s %s", ADDR_2, flags);
-	system(cmd);
-	snprintf(cmd, sizeof(cmd), "sysctl -qw net.mptcp.scheduler=%s", sched);
-	system(cmd);
+	SYS_NOFAIL("ip -net %s link add veth1 type veth peer name veth2", ns);
+	SYS_NOFAIL("ip -net %s addr add %s/24 dev veth1", ns, ADDR_1);
+	SYS_NOFAIL("ip -net %s link set dev veth1 up", ns);
+	SYS_NOFAIL("ip -net %s addr add %s/24 dev veth2", ns, ADDR_2);
+	SYS_NOFAIL("ip -net %s link set dev veth2 up", ns);
+	SYS_NOFAIL("ip -net %s mptcp endpoint add %s %s", ns, ADDR_2, flags);
+	SYS_NOFAIL("ip netns exec %s sysctl -qw net.mptcp.scheduler=%s", ns, sched);
 }
 
-static void sched_cleanup(void)
+static void sched_cleanup(char *ns)
 {
-	system("sysctl -qw net.mptcp.scheduler=default");
-	system("ip mptcp endpoint flush");
-	system("ip link del veth1");
+	SYS_NOFAIL("ip netns exec %s sysctl -qw net.mptcp.scheduler=default", ns);
+	SYS_NOFAIL("ip -net %s mptcp endpoint flush", ns);
+	SYS_NOFAIL("ip -net %s link del veth1", ns);
 }
 
-static int has_bytes_sent(char *addr)
+static int has_bytes_sent(char *ns, char *addr)
 {
-	char cmd[64];
+	char cmd[128];
 
-	snprintf(cmd, sizeof(cmd), "ss -it dst %s | grep -q 'bytes_sent:'", addr);
+	snprintf(cmd, sizeof(cmd), "ip netns exec %s ss -it dst %s | grep -q bytes_sent:",
+		 ns, addr);
 	return system(cmd);
 }
 
-static void test_first(void)
+static void test_first(char *ns)
 {
 	struct mptcp_bpf_first *first_skel;
 	int server_fd, client_fd;
@@ -302,17 +296,17 @@ static void test_first(void)
 		return;
 	}
 
-	sched_init("subflow", "bpf_first");
+	sched_init(ns, "subflow", "bpf_first");
 	server_fd = start_mptcp_server(AF_INET, ADDR_1, 0, 0);
 	client_fd = connect_to_fd(server_fd, 0);
 
 	send_data(server_fd, client_fd);
-	ASSERT_OK(has_bytes_sent(ADDR_1), "has_bytes_sent addr_1");
-	ASSERT_GT(has_bytes_sent(ADDR_2), 0, "has_bytes_sent addr_2");
+	ASSERT_OK(has_bytes_sent(ns, ADDR_1), "has_bytes_sent addr_1");
+	ASSERT_GT(has_bytes_sent(ns, ADDR_2), 0, "has_bytes_sent addr_2");
 
 	close(client_fd);
 	close(server_fd);
-	sched_cleanup();
+	sched_cleanup(ns);
 	bpf_link__destroy(link);
 	mptcp_bpf_first__destroy(first_skel);
 }
@@ -334,7 +328,7 @@ void test_mptcp(void)
 	if (test__start_subtest("base"))
 		test_base();
 	if (test__start_subtest("first"))
-		test_first();
+		test_first(NS_TEST);
 
 	if (nstoken)
 		close_netns(nstoken);
-- 
2.35.3
Re: [PATCH mptcp-next v2] Squash to "selftests/bpf: Add bpf_first test"
Posted by Matthieu Baerts 11 months, 1 week ago
Hi Geliang,

On 17/05/2023 10:40, Geliang Tang wrote:
> Run mptcp sched test in a dedicated netns.
> 
> Signed-off-by: Geliang Tang <geliang.tang@suse.com>
> ---
>  .../testing/selftests/bpf/prog_tests/mptcp.c  | 50 ++++++++-----------
>  1 file changed, 22 insertions(+), 28 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c
> index 77a6997fc82f..bea9c799a531 100644
> --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c
> +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c
> @@ -253,40 +253,34 @@ static void send_data(int lfd, int fd)
>  #define ADDR_1	"10.0.1.1"
>  #define ADDR_2	"10.0.1.2"
>  
> -static void sched_init(char *flags, char *sched)
> +static void sched_init(char *ns, char *flags, char *sched)

I think it is cleaner to use a new netns for each test: by doing that,
we are sure to start from a fresh and controlled environment just in
case something wrong happened with the previous test. No?

So in the sched_init(), the netns is created and in sched_cleanup(), the
netns is destroyed (and no need to do anything else I guess).

I guess you can also simply use a helper to create the netns and set
NS_TEST which would be a "global" variable. WDYT?

>  {
> -	char cmd[64];
> -
> -	system("ip link add veth1 type veth peer name veth2");
> -	snprintf(cmd, sizeof(cmd), "ip addr add %s/24 dev veth1", ADDR_1);
> -	system(cmd);
> -	system("ip link set veth1 up");
> -	snprintf(cmd, sizeof(cmd), "ip addr add %s/24 dev veth2", ADDR_2);
> -	system(cmd);
> -	system("ip link set veth2 up");
> -
> -	snprintf(cmd, sizeof(cmd), "ip mptcp endpoint add %s %s", ADDR_2, flags);
> -	system(cmd);
> -	snprintf(cmd, sizeof(cmd), "sysctl -qw net.mptcp.scheduler=%s", sched);
> -	system(cmd);
> +	SYS_NOFAIL("ip -net %s link add veth1 type veth peer name veth2", ns);
> +	SYS_NOFAIL("ip -net %s addr add %s/24 dev veth1", ns, ADDR_1);
> +	SYS_NOFAIL("ip -net %s link set dev veth1 up", ns);
> +	SYS_NOFAIL("ip -net %s addr add %s/24 dev veth2", ns, ADDR_2);
> +	SYS_NOFAIL("ip -net %s link set dev veth2 up", ns);
> +	SYS_NOFAIL("ip -net %s mptcp endpoint add %s %s", ns, ADDR_2, flags);
> +	SYS_NOFAIL("ip netns exec %s sysctl -qw net.mptcp.scheduler=%s", ns, sched);

Why not using SYS() instead? By doing that, the test will be marked as
failed if one of these commands fails.

You just need to add:

  fail:
          return;

at the end I guess.

>  }
>  
> -static void sched_cleanup(void)
> +static void sched_cleanup(char *ns)>  {
> -	system("sysctl -qw net.mptcp.scheduler=default");
> -	system("ip mptcp endpoint flush");
> -	system("ip link del veth1");
> +	SYS_NOFAIL("ip netns exec %s sysctl -qw net.mptcp.scheduler=default", ns);
> +	SYS_NOFAIL("ip -net %s mptcp endpoint flush", ns);
> +	SYS_NOFAIL("ip -net %s link del veth1", ns);

Same here with SYS() to catch unexpected behaviours.

(but see above, maybe enough to just close nstoken and delete the netns)

Cheers,
Matt
-- 
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net