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

Geliang Tang posted 3 patches 3 years, 3 months ago
Maintainers: Song Liu <songliubraving@fb.com>, John Fastabend <john.fastabend@gmail.com>, KP Singh <kpsingh@kernel.org>, Andrii Nakryiko <andrii@kernel.org>, Martin KaFai Lau <kafai@fb.com>, Shuah Khan <shuah@kernel.org>, Matthieu Baerts <matthieu.baerts@tessares.net>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Yonghong Song <yhs@fb.com>, Mat Martineau <mathew.j.martineau@linux.intel.com>
[PATCH mptcp-next 1/3] Squash to "selftests/bpf: Add bpf_first test"
Posted by Geliang Tang 3 years, 3 months ago
Please update the commit log:

'''
This patch expends the MPTCP test base to support MPTCP packet scheduler
tests. Add the bpf_first scheduler test in it.

In the new helper sched_init(), add two veth net devices to simulate the
multiple addresses case. Use 'ip mptcp endpoint' command to add the new
endpoint ADDR_2 to PM netlink. Use sysctl to set net.mptcp.scheduler to
use the given sched.

Invoke start_mptcp_server() to start the server on ADDR_1, and invoke
connect_to_fd() to connect with the server from the client. Then invoke
send_data() to send data.

Some code in send_data() is from prog_tests/bpf_tcp_ca.c.

Use the new helper has_bytes_sent() to check the bytes_sent filed of 'ss'
output after send_data() to make sure no data has been sent on ADDR_2.
All data has been sent on the first subflow.

Invoke the new helper sched_cleanup() to set back net.mptcp.scheduler to
default, flush all mptcp endpoints, and delete the veth net devices.
'''

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

diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c
index 1ecc8a2b76b6..3122279d0b9d 100644
--- a/tools/testing/selftests/bpf/prog_tests/mptcp.c
+++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c
@@ -14,6 +14,9 @@
 #define TCP_CA_NAME_MAX	16
 #endif
 
+#define ADDR_1	"10.0.1.1"
+#define ADDR_2	"10.0.1.2"
+
 struct mptcp_storage {
 	__u32 invoked;
 	__u32 is_mptcp;
@@ -252,20 +255,39 @@ static void send_data(int lfd, int fd)
 	      PTR_ERR(thread_ret));
 }
 
-static void add_veth(void)
+static void sched_init(char *flags, char *sched)
 {
-	system("ip link add veth1 type veth");
-	system("ip addr add 10.0.1.1/24 dev veth1");
+	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);
 }
 
-static void cleanup(void)
+static void sched_cleanup(void)
 {
 	system("sysctl -qw net.mptcp.scheduler=default");
 	system("ip mptcp endpoint flush");
 	system("ip link del veth1");
 }
 
+static int has_bytes_sent(char *addr)
+{
+	char cmd[64];
+
+	snprintf(cmd, sizeof(cmd), "ss -it dst %s | grep -q 'bytes_sent:'", addr);
+	return system(cmd);
+}
+
 static void test_first(void)
 {
 	struct mptcp_bpf_first *first_skel;
@@ -282,18 +304,17 @@ static void test_first(void)
 		return;
 	}
 
-	add_veth();
-	system("ip mptcp endpoint add 10.0.1.1 subflow");
-	system("sysctl -qw net.mptcp.scheduler=bpf_first");
-	server_fd = start_mptcp_server(AF_INET, NULL, 0, 0);
+	sched_init("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_GT(system("ss -MOenita | grep '10.0.1.1' | grep 'bytes_sent:'"), 0, "ss");
+	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");
 
 	close(client_fd);
 	close(server_fd);
-	cleanup();
+	sched_cleanup();
 	bpf_link__destroy(link);
 	mptcp_bpf_first__destroy(first_skel);
 }
-- 
2.34.1