Skip to contentSkip to conversation

setsockopt per subflow: BPF

PATCH Superseded Geliang Tang 2 participants 6 messages Mar 21, 2024

Subsystems (4)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Geliang Tang

author reviewer
#0 lore raw

v2 cover setsockopt per subflow: BPF

From: Geliang Tang <[email protected]>

Address #76.

v2:
- fix checkpatch warnings.
- cleanups.

Geliang Tang (4):
Squash to "selftests/bpf: Add bpf scheduler test" fix selftests/bpf: Add mptcp subflow example selftests/bpf: Add mptcp subflow subtest
Squash to "selftests/bpf: Add bpf scheduler test" cleanup

 .../testing/selftests/bpf/prog_tests/mptcp.c  | 131 +++++++++++++++---
 .../selftests/bpf/progs/mptcp_subflow.c       |  73 ++++++++++
 2 files changed, 183 insertions(+), 21 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_subflow.c
Signature · 1 line
2.40.1
author reviewer
#1 lore raw

PATCH v2 1/4 Squash to "selftests/bpf: Add bpf scheduler test" fix

From: Geliang Tang <[email protected]>

BPF tests fail sometimes with "bytes != total_bytes" errors:

# test_default:PASS:sched_init:default 0 nsec
# send_data:PASS:pthread_create 0 nsec
# send_data:FAIL:recv 936000 != 10485760 nr_recv:-1 errno:11 # default: 3041 ms
# server:FAIL:send 7579500 != 10485760 nr_sent:-1 errno:11 # send_data:FAIL:pthread_join thread_ret:-11 \
test_default:PASS:has_bytes_sent addr_1 0 nsec
# test_default:PASS:has_bytes_sent addr_2 0 nsec
# close_netns:PASS:setns 0 nsec

"bytes != total_bytes" is not a fatal error for BPF test, just print out the error info and skip it.

This patch makes BPF tests stable.

---
 tools/testing/selftests/bpf/prog_tests/mptcp.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/mptcp.c +10 -6
index 6e28215d7404..4c180839ff1d 100644
@@ -362,8 +362,10 @@ static void *server(void *arg)
 		bytes += nr_sent;
 	}
 
-	CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n",
-	      bytes, total_bytes, nr_sent, errno);
+	if (bytes != total_bytes) {
+		printf("FAIL:send %zd != %u nr_sent:%zd errno:%d\n",
+		       bytes, total_bytes, nr_sent, errno);
+	}
 
 done:
 	if (fd >= 0)
@@ -409,16 +411,18 @@ static void send_data(int lfd, int fd, char *msg)
 
 	delta_ms = (end.tv_sec - start.tv_sec) * 1000 + (end.tv_nsec - start.tv_nsec) / 1000000;
 
-	CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
-	      bytes, total_bytes, nr_recv, errno);
+	if (bytes != total_bytes) {
+		printf("FAIL:recv %zd != %u nr_recv:%zd errno:%d\n",
+		       bytes, total_bytes, nr_recv, errno);
+	}
 
 	printf("%s: %u ms\n", msg, delta_ms);
 
 	WRITE_ONCE(stop, 1);
 
 	pthread_join(srv_thread, &thread_ret);
-	CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld",
-	      PTR_ERR(thread_ret));
+	if (IS_ERR(thread_ret))
+		printf("FAIL:pthread_join thread_ret:%ld\n", PTR_ERR(thread_ret));
 }
 
 #define ADDR_1	"10.0.1.1"
Signature · 1 line
2.40.1
author reviewer
#2 lore raw

PATCH v2 2/4 selftests/bpf: Add mptcp subflow example

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Move Nicolas's patch into bpf selftests directory. This example added a test that was adding a different mark (SO_MARK) on each subflow, and changing the TCP CC only on the first subflow.

This example shows how it is possible to:

Identify the parent msk of an MPTCP subflow.
Put different sockopt for each subflow of a same MPTCP connection.

Here especially, we implemented two different behaviours:

A socket mark (SOL_SOCKET SO_MARK) is put on each subflow of a same MPTCP connection. The order of creation of the current subflow defines its mark. The TCP CC algorithm of the very first subflow of an MPTCP connection is set to "reno".

---
 .../selftests/bpf/progs/mptcp_subflow.c       | 73 +++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_subflow.c
tools/testing/selftests/bpf/progs/mptcp_subflow.c new file +73 -0
new file mode 100644
index 000000000000..9553099ffd5f
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020, Tessares SA. */
+/* Copyright (c) 2024, Kylin Software */
+/* Author: Nicolas Rybowski */
+
+#include <sys/socket.h> // SOL_SOCKET, SO_MARK, ...
+#include <linux/tcp.h>  // TCP_CONGESTION
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_tcp_helpers.h"
+
+char _license[] SEC("license") = "GPL";
+
+#ifndef SOL_TCP
+#define SOL_TCP 6
+#endif
+
+#ifndef TCP_CA_NAME_MAX
+#define TCP_CA_NAME_MAX 16
+#endif
+
+char cc[TCP_CA_NAME_MAX] = "reno";
+
+/* Associate a subflow counter to each token */
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(key_size, sizeof(__u32));
+	__uint(value_size, sizeof(__u32));
+	__uint(max_entries, 100);
+} mptcp_sf SEC(".maps");
+
+SEC("sockops")
+int mptcp_subflow(struct bpf_sock_ops *skops)
+{
+	__u32 init = 1, key, mark, *cnt;
+	struct mptcp_sock *msk;
+	struct bpf_sock *sk;
+	int err;
+
+	if (skops->op != BPF_SOCK_OPS_TCP_CONNECT_CB)
+		return 1;
+
+	sk = skops->sk;
+	if (!sk)
+		return 1;
+
+	msk = bpf_skc_to_mptcp_sock(sk);
+	if (!msk)
+		return 1;
+
+	key = msk->token;
+	cnt = bpf_map_lookup_elem(&mptcp_sf, &key);
+	if (cnt) {
+		/* A new subflow is added to an existing MPTCP connection */
+		__sync_fetch_and_add(cnt, 1);
+		mark = *cnt;
+	} else {
+		/* A new MPTCP connection is just initiated and this is its primary
+		 *  subflow
+		 */
+		bpf_map_update_elem(&mptcp_sf, &key, &init, BPF_ANY);
+		mark = init;
+	}
+
+	/* Set the mark of the subflow's socket to its apparition order */
+	err = bpf_setsockopt(skops, SOL_SOCKET, SO_MARK, &mark, sizeof(mark));
+	if (err < 0)
+		return 1;
+	if (mark == 1)
+		err = bpf_setsockopt(skops, SOL_TCP, TCP_CONGESTION, cc, TCP_CA_NAME_MAX);
+
+	return 1;
+}
Signature · 1 line
2.40.1
author reviewer
#3 lore raw

PATCH v2 3/4 selftests/bpf: Add mptcp subflow subtest

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

This patch adds a subtest named test_subflow to load and verify the newly added mptcp subflow example in test_mptcp. Add a helper endpoint_init() to add a new subflow endpoint. Add another helper has_in_subflow() to verify the fwmark and congestion values set by mptcp_subflow prog using setsockopts.

---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 90 +++++++++++++++++++
 1 file changed, 90 insertions(+)
tools/testing/selftests/bpf/prog_tests/mptcp.c +90 -0
index 4c180839ff1d..c6ce23b73d06 100644
@@ -10,6 +10,7 @@
 #include "network_helpers.h"
 #include "mptcp_sock.skel.h"
 #include "mptcpify.skel.h"
+#include "mptcp_subflow.skel.h"
 #include "mptcp_bpf_first.skel.h"
 #include "mptcp_bpf_bkup.skel.h"
 #include "mptcp_bpf_rr.skel.h"
@@ -328,6 +329,93 @@ static void test_mptcpify(void)
 	close(cgroup_fd);
 }
 
+#define ADDR_1	"10.0.1.1"
+#define ADDR_2	"10.0.1.2"
+#define PORT_1	10001
+
+static int endpoint_init(char *flags)
+{
+	SYS(fail, "ip -net %s link add veth1 type veth peer name veth2", NS_TEST);
+	SYS(fail, "ip -net %s addr add %s/24 dev veth1", NS_TEST, ADDR_1);
+	SYS(fail, "ip -net %s link set dev veth1 up", NS_TEST);
+	SYS(fail, "ip -net %s addr add %s/24 dev veth2", NS_TEST, ADDR_2);
+	SYS(fail, "ip -net %s link set dev veth2 up", NS_TEST);
+	SYS(fail, "ip -net %s mptcp endpoint add %s %s", NS_TEST, ADDR_2, flags);
+
+	return 0;
+fail:
+	return -1;
+}
+
+static int has_in_subflow(char *src, char *str)
+{
+	char cmd[128];
+
+	snprintf(cmd, sizeof(cmd),
+		 "ip netns exec %s ss -Menita src %s dst %s dport %d | grep -q '%s'",
+		 NS_TEST, src, ADDR_1, PORT_1, str);
+	return system(cmd);
+}
+
+static void run_test_subflow(int cgroup_fd)
+{
+	int server_fd, client_fd, prog_fd, err;
+	struct mptcp_subflow *skel;
+
+	skel = mptcp_subflow__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_load"))
+		goto cleanup;
+
+	prog_fd = bpf_program__fd(skel->progs.mptcp_subflow);
+	err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_SOCK_OPS, 0);
+	if (!ASSERT_OK(err, "bpf_prog_attach"))
+		goto cleanup;
+
+	server_fd = start_mptcp_server(AF_INET, ADDR_1, PORT_1, 0);
+	if (!ASSERT_GE(server_fd, 0, "start_mptcp_server"))
+		goto cleanup;
+
+	client_fd = connect_to_fd(server_fd, 0);
+	if (!ASSERT_GE(client_fd, 0, "connect to fd"))
+		goto close_server;
+
+	send_byte(client_fd);
+
+	ASSERT_OK(has_in_subflow(ADDR_1, "fwmark:0x1"), "has_in_subflow fwmark:0x1");
+	ASSERT_OK(has_in_subflow(ADDR_2, "fwmark:0x2"), "has_in_subflow fwmark:0x2");
+	ASSERT_OK(has_in_subflow(ADDR_1, "reno"), "has_in_subflow reno");
+	ASSERT_OK(has_in_subflow(ADDR_2, "cubic"), "has_in_subflow cubic");
+
+	close(client_fd);
+close_server:
+	close(server_fd);
+cleanup:
+	mptcp_subflow__destroy(skel);
+}
+
+void test_subflow(void)
+{
+	struct nstoken *nstoken = NULL;
+	int cgroup_fd;
+
+	cgroup_fd = test__join_cgroup("/mptcp_subflow");
+	if (!ASSERT_GE(cgroup_fd, 0, "test__join_cgroup"))
+		return;
+
+	nstoken = create_netns();
+	if (!ASSERT_OK_PTR(nstoken, "create_netns"))
+		goto fail;
+
+	if (!ASSERT_OK(endpoint_init("subflow"), "endpoint_init"))
+		goto fail;
+
+	run_test_subflow(cgroup_fd);
+
+fail:
+	cleanup_netns(nstoken);
+	close(cgroup_fd);
+}
+
 static const unsigned int total_bytes = 10 * 1024 * 1024;
 static int stop, duration;
 
@@ -661,6 +749,8 @@ void test_mptcp(void)
 		test_base();
 	if (test__start_subtest("mptcpify"))
 		test_mptcpify();
+	if (test__start_subtest("subflow"))
+		test_subflow();
 	if (test__start_subtest("default"))
 		test_default();
 	if (test__start_subtest("first"))
Signature · 1 line
2.40.1
author reviewer
#4 lore raw

PATCH v2 4/4 Squash to "selftests/bpf: Add bpf scheduler test" cleanup

In reply to Geliang Tang

From: Geliang Tang <[email protected]>

Some cleanups.

---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 25 ++++++++-----------
 1 file changed, 10 insertions(+), 15 deletions(-)
tools/testing/selftests/bpf/prog_tests/mptcp.c +10 -15
index c6ce23b73d06..861f70dfe68f 100644
@@ -417,7 +417,7 @@ void test_subflow(void)
 }
 
 static const unsigned int total_bytes = 10 * 1024 * 1024;
-static int stop, duration;
+static int stop;
 
 static void *server(void *arg)
 {
@@ -476,11 +476,12 @@ static void send_data(int lfd, int fd, char *msg)
 	int err;
 
 	WRITE_ONCE(stop, 0);
+
 	if (clock_gettime(CLOCK_MONOTONIC, &start) < 0)
 		return;
 
 	err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd);
-	if (CHECK(err != 0, "pthread_create", "err:%d errno:%d\n", err, errno))
+	if (!ASSERT_OK(err, "pthread_create"))
 		return;
 
 	/* recv total_bytes */
@@ -513,10 +514,6 @@ static void send_data(int lfd, int fd, char *msg)
 		printf("FAIL:pthread_join thread_ret:%ld\n", PTR_ERR(thread_ret));
 }
 
-#define ADDR_1	"10.0.1.1"
-#define ADDR_2	"10.0.1.2"
-#define PORT_1	10001
-
 static struct nstoken *sched_init(char *flags, char *sched)
 {
 	struct nstoken *nstoken;
@@ -525,12 +522,9 @@ static struct nstoken *sched_init(char *flags, char *sched)
 	if (!ASSERT_OK_PTR(nstoken, "create_netns"))
 		goto fail;
 
-	SYS(fail, "ip -net %s link add veth1 type veth peer name veth2", NS_TEST);
-	SYS(fail, "ip -net %s addr add %s/24 dev veth1", NS_TEST, ADDR_1);
-	SYS(fail, "ip -net %s link set dev veth1 up", NS_TEST);
-	SYS(fail, "ip -net %s addr add %s/24 dev veth2", NS_TEST, ADDR_2);
-	SYS(fail, "ip -net %s link set dev veth2 up", NS_TEST);
-	SYS(fail, "ip -net %s mptcp endpoint add %s %s", NS_TEST, ADDR_2, flags);
+	if (!ASSERT_OK(endpoint_init("subflow"), "endpoint_init"))
+		goto fail;
+
 	SYS(fail, "ip netns exec %s sysctl -qw net.mptcp.scheduler=%s", NS_TEST, sched);
 
 	return nstoken;
@@ -538,12 +532,13 @@ static struct nstoken *sched_init(char *flags, char *sched)
 	return NULL;
 }
 
-static int has_bytes_sent(char *addr)
+static int has_bytes_sent(char *dst)
 {
 	char cmd[128];
 
-	snprintf(cmd, sizeof(cmd), "ip netns exec %s ss -it src %s sport %d dst %s | %s",
-		 NS_TEST, ADDR_1, PORT_1, addr, "grep -q bytes_sent:");
+	snprintf(cmd, sizeof(cmd),
+		 "ip netns exec %s ss -it src %s sport %d dst %s | %s",
+		 NS_TEST, ADDR_1, PORT_1, dst, "grep -q bytes_sent:");
 	return system(cmd);
 }
 
Signature · 1 line
2.40.1
#5 lore raw
In reply to Geliang Tang

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Success! ✅
- KVM Validation: btf (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/8369436657

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/6416b9452c7c
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=836914

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)