Skip to contentSkip to conversation

BPF round-robin scheduler

PATCH Superseded Geliang Tang 2 participants 6 messages Apr 8, 2022 – Apr 9, 2022

Subsystems (5)

v4 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v4 cover BPF round-robin scheduler

v4:
- add retrans argment for get_subflow()
- deponds on: "BPF packet scheduler" series v11.

v3:
- add last_snd write access.
- keep msk->last_snd setting in get_subflow().
- deponds on: "BPF packet scheduler" series v10.

v2:
- merge the squash-to patch.
- implement bpf_mptcp_get_subflows helper, instead of bpf_mptcp_get_next_subflow.
- deponds on: "BPF packet scheduler v9".

This patchset implements round-robin scheduler using BPF. Address to some commends for the RFC version:

https://patchwork.kernel.org/project/mptcp/cover/[email protected]/

Geliang Tang (4):
mptcp: add last_snd write access
mptcp: add bpf get_subflows helper
selftests: bpf: add bpf_rr scheduler
selftests: bpf: add bpf_rr test

 net/mptcp/bpf.c                               | 44 ++++++++++++++++
 net/mptcp/protocol.h                          |  3 ++
 .../testing/selftests/bpf/bpf_mptcp_helpers.h |  9 ++++
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 37 ++++++++++++++
 .../selftests/bpf/progs/mptcp_bpf_rr.c        | 50 +++++++++++++++++++
 5 files changed, 143 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/4 mptcp: add last_snd write access

This patch exports the member last_snd of struct mptcp_sock in bpf_mptcp_helpers.h, and adds BPF write access to it.

---
 net/mptcp/bpf.c                                 | 16 ++++++++++++++++
 tools/testing/selftests/bpf/bpf_mptcp_helpers.h |  1 +
 2 files changed, 17 insertions(+)
net/mptcp/bpf.c +16 -0
index e849fc3fb6c5..bd3c50b07ab2 100644
@@ -40,6 +40,7 @@ static int bpf_mptcp_sched_btf_struct_access(struct bpf_verifier_log *log,
 {
 	const struct btf_type *state;
 	u32 type_id;
+	size_t end;
 
 	if (atype == BPF_READ)
 		return btf_struct_access(log, btf, t, off, size, atype,
@@ -55,6 +56,21 @@ static int bpf_mptcp_sched_btf_struct_access(struct bpf_verifier_log *log,
 		return -EACCES;
 	}
 
+	switch (off) {
+	case offsetof(struct mptcp_sock, last_snd):
+		end = offsetofend(struct mptcp_sock, last_snd);
+		break;
+	default:
+		bpf_log(log, "no write support to mptcp_sock at off %d\n", off);
+		return -EACCES;
+	}
+
+	if (off + size > end) {
+		bpf_log(log, "access beyond mptcp_sock at off %u size %u ended at %lu",
+			off, size, end);
+		return -EACCES;
+	}
+
 	return NOT_INIT;
 }
 
tools/testing/selftests/bpf/bpf_mptcp_helpers.h +1 -0
index 9a0e3e7766b0..4f82baee998c 100644
@@ -9,6 +9,7 @@
 struct mptcp_sock {
 	struct inet_connection_sock	sk;
 
+	struct sock	*last_snd;
 	__u32		token;
 	struct sock	*first;
 	char		ca_name[TCP_CA_NAME_MAX];
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/4 mptcp: add bpf get_subflows helper

In reply to Geliang Tang

This patch implements a new helper bpf_mptcp_get_subflows() to get all the subflows of the given mptcp_sock, it returns the number of suflows. Add a new member subflows in struct mptcp_sock as a pointers array of all the subflows.

Register this helper in bpf_mptcp_sched_kfunc_init() to make sure it can be accessed from the BPF context.

---
 net/mptcp/bpf.c      | 28 ++++++++++++++++++++++++++++
 net/mptcp/protocol.h |  3 +++
 2 files changed, 31 insertions(+)
net/mptcp/bpf.c +28 -0
index bd3c50b07ab2..7f8782dbfa6d 100644
@@ -157,6 +157,22 @@ struct bpf_struct_ops bpf_mptcp_sched_ops = {
 	.name		= "mptcp_sched_ops",
 };
 
+BTF_SET_START(bpf_mptcp_sched_kfunc_ids)
+BTF_ID(func, bpf_mptcp_get_subflows)
+BTF_SET_END(bpf_mptcp_sched_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_mptcp_sched_kfunc_set = {
+	.owner		= THIS_MODULE,
+	.check_set	= &bpf_mptcp_sched_kfunc_ids,
+};
+
+static int __init bpf_mptcp_sched_kfunc_init(void)
+{
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+					 &bpf_mptcp_sched_kfunc_set);
+}
+late_initcall(bpf_mptcp_sched_kfunc_init);
+
 struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
 {
 	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
@@ -165,3 +181,15 @@ struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
 	return NULL;
 }
 EXPORT_SYMBOL(bpf_mptcp_sock_from_subflow);
+
+u8 bpf_mptcp_get_subflows(struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	u8 nr = 0;
+
+	mptcp_for_each_subflow(msk, subflow)
+		msk->subflows[nr++] = subflow;
+
+	return nr;
+}
+EXPORT_SYMBOL(bpf_mptcp_get_subflows);
net/mptcp/protocol.h +3 -0
index a58c5c84e72b..7c5bb8fb9e77 100644
@@ -238,6 +238,8 @@ struct mptcp_data_frag {
 	struct page *page;
 };
 
+#define MPTCP_SUBFLOWS_MAX	8
+
 /* MPTCP connection sock */
 struct mptcp_sock {
 	/* inet_connection_sock must be the first member */
@@ -298,6 +300,7 @@ struct mptcp_sock {
 
 	u32 setsockopt_seq;
 	char		ca_name[TCP_CA_NAME_MAX];
+	struct mptcp_subflow_context *subflows[MPTCP_SUBFLOWS_MAX];
 };
 
 #define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH mptcp-next v4 2/4] mptcp: add bpf get_subflows helper

Quoted 71 lines · Geliang Tang

This patch implements a new helper bpf_mptcp_get_subflows() to get all the subflows of the given mptcp_sock, it returns the number of suflows. Add a new member subflows in struct mptcp_sock as a pointers array of all the subflows.

Register this helper in bpf_mptcp_sched_kfunc_init() to make sure it can be accessed from the BPF context.

---
net/mptcp/bpf.c | 28 ++++++++++++++++++++++++++++
net/mptcp/protocol.h | 3 +++

2 files changed, 31 insertions(+)
net/mptcp/bpf.c +28 -0
index bd3c50b07ab2..7f8782dbfa6d 100644
@@ -157,6 +157,22 @@ struct bpf_struct_ops bpf_mptcp_sched_ops = {
 	.name		= "mptcp_sched_ops",
 };
 
+BTF_SET_START(bpf_mptcp_sched_kfunc_ids)
+BTF_ID(func, bpf_mptcp_get_subflows)
+BTF_SET_END(bpf_mptcp_sched_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_mptcp_sched_kfunc_set = {
+	.owner		= THIS_MODULE,
+	.check_set	= &bpf_mptcp_sched_kfunc_ids,
+};
+
+static int __init bpf_mptcp_sched_kfunc_init(void)
+{
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
+					 &bpf_mptcp_sched_kfunc_set);
+}
+late_initcall(bpf_mptcp_sched_kfunc_init);
+
 struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
 {
 	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
@@ -165,3 +181,15 @@ struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
 	return NULL;
 }
 EXPORT_SYMBOL(bpf_mptcp_sock_from_subflow);
+
+u8 bpf_mptcp_get_subflows(struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	u8 nr = 0;
+
+	mptcp_for_each_subflow(msk, subflow)
+		msk->subflows[nr++] = subflow;
+
+	return nr;
+}
+EXPORT_SYMBOL(bpf_mptcp_get_subflows);
net/mptcp/protocol.h +2 -0
index a58c5c84e72b..7c5bb8fb9e77 100644
@@ -238,6 +238,8 @@ struct mptcp_data_frag {
 	struct page *page;
 };
 
+#define MPTCP_SUBFLOWS_MAX	8
+
 /* MPTCP connection sock */
 struct mptcp_sock {
 	/* inet_connection_sock must be the first member */

net/mptcp/protocol.h struct mptcp_sock { · line 303

Quoted 4 lines
net/mptcp/protocol.h +1 -0
@@ -298,6 +300,7 @@ struct mptcp_sock {
 
 	u32 setsockopt_seq;
 	char		ca_name[TCP_CA_NAME_MAX];
+	struct mptcp_subflow_context *subflows[MPTCP_SUBFLOWS_MAX];

This storage shouldn't be added to struct mptcp_sock, when the values are only locally useful in a BPF hook. Besides the extra memory usage, the pointers will get stale and possibly point to invalid locations after subflows are closed. Can't the helper write to some temporary storage instead?

Quoted 8 lines
net/mptcp/protocol.h +0 -0
 };
 
 #define mptcp_data_lock(sk) spin_lock_bh(&(sk)->sk_lock.slock)
Signature · 1 line
2.34.1

--
Mat Martineau
Intel

#4 lore raw

PATCH v4 3/4 selftests: bpf: add bpf_rr scheduler

In reply to Geliang Tang

This patch implements the round-robin BPF MPTCP scheduler, named bpf_rr, which always picks the next available subflow to send data. If no such next subflow available, picks the first one.

---
 .../testing/selftests/bpf/bpf_mptcp_helpers.h |  8 +++
 .../selftests/bpf/progs/mptcp_bpf_rr.c        | 50 +++++++++++++++++++
 2 files changed, 58 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c
tools/testing/selftests/bpf/bpf_mptcp_helpers.h +8 -0
index 4f82baee998c..4c2ff78e0a9b 100644
@@ -6,6 +6,8 @@
 
 #include "bpf_tcp_helpers.h"
 
+#define MPTCP_SUBFLOWS_MAX	8
+
 struct mptcp_sock {
 	struct inet_connection_sock	sk;
 
@@ -13,6 +15,7 @@ struct mptcp_sock {
 	__u32		token;
 	struct sock	*first;
 	char		ca_name[TCP_CA_NAME_MAX];
+	struct mptcp_subflow_context *subflows[MPTCP_SUBFLOWS_MAX];
 } __attribute__((preserve_access_index));
 
 #define MPTCP_SCHED_NAME_MAX 16
@@ -27,4 +30,9 @@ struct mptcp_sched_ops {
 	void *owner;
 };
 
+struct mptcp_subflow_context {
+	__u32	token;
+	struct	sock *tcp_sock;	    /* tcp sk backpointer */
+} __attribute__((preserve_access_index));
+
 #endif
tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

PATCH v4 4/4 selftests: bpf: add bpf_rr test

In reply to Geliang Tang

This patch adds the round-robin BPF MPTCP scheduler test. Use sysctl to set net.mptcp.scheduler to use this sched. Add a veth net device to simulate the multiple addresses case. Use 'ip mptcp endpoint' command to add this new endpoint to PM netlink.

---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
tools/testing/selftests/bpf/prog_tests/mptcp.c +37 -0
index 377ebc0fbcbe..7052c520c0f7 100644
@@ -6,6 +6,7 @@
 #include "cgroup_helpers.h"
 #include "network_helpers.h"
 #include "mptcp_bpf_first.skel.h"
+#include "mptcp_bpf_rr.skel.h"
 
 #ifndef TCP_CA_NAME_MAX
 #define TCP_CA_NAME_MAX	16
@@ -362,10 +363,46 @@ static void test_first(void)
 	mptcp_bpf_first__destroy(first_skel);
 }
 
+static void test_rr(void)
+{
+	struct mptcp_bpf_rr *rr_skel;
+	int server_fd, client_fd;
+	struct bpf_link *link;
+
+	rr_skel = mptcp_bpf_rr__open_and_load();
+	if (CHECK(!rr_skel, "bpf_rr__open_and_load", "failed\n"))
+		return;
+
+	link = bpf_map__attach_struct_ops(rr_skel->maps.rr);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
+		mptcp_bpf_rr__destroy(rr_skel);
+		return;
+	}
+
+	system("ip link add veth1 type veth; \
+		ip addr add 10.0.1.1/24 dev veth1; \
+		ip link set veth1 up");
+	system("ip mptcp endpoint add 10.0.1.1 subflow");
+	system("sysctl -q net.mptcp.scheduler=bpf_rr");
+	server_fd = start_mptcp_server(AF_INET, NULL, 0, 0);
+	client_fd = connect_to_mptcp_fd(server_fd, 0);
+
+	send_data(server_fd, client_fd);
+
+	close(client_fd);
+	close(server_fd);
+	system("ip mptcp endpoint flush");
+	system("ip link del veth1");
+	bpf_link__destroy(link);
+	mptcp_bpf_rr__destroy(rr_skel);
+}
+
 void test_mptcp(void)
 {
 	if (test__start_subtest("base"))
 		test_base();
 	if (test__start_subtest("first"))
 		test_first();
+	if (test__start_subtest("rr"))
+		test_rr();
 }
Signature · 1 line
2.34.1