Skip to contentSkip to conversation

BPF round-robin scheduler

PATCH Superseded Geliang Tang 2 participants 5 messages Apr 22, 2022 – Apr 26, 2022

Subsystems (5)

v8 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v8 cover BPF round-robin scheduler

v8:
- use struct mptcp_sched_data.
- deponds on: "BPF packet scheduler" series v14.

v7:
- rename retrans to reinject.
- drop last_snd setting.
- deponds on: "BPF packet scheduler" series v13.

v6:
- set call_me_again flag.
- deponds on: "BPF packet scheduler" series v12.

v5:
- update patch 2, use temporary storage instead.
- update patch 3, use new helpers.
- deponds on: "BPF packet scheduler" series v11.

v4:
- add retrans argment for get_subflow()

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 (3):
mptcp: add bpf get_subflows helper
selftests: bpf: add bpf_rr scheduler
selftests: bpf: add bpf_rr test

 net/mptcp/bpf.c                               | 47 ++++++++++++++++
 net/mptcp/protocol.h                          |  7 +++
 .../testing/selftests/bpf/bpf_mptcp_helpers.h | 13 +++++
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 37 +++++++++++++
 .../selftests/bpf/progs/mptcp_bpf_rr.c        | 55 +++++++++++++++++++
 5 files changed, 159 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v8 1/3 mptcp: add bpf get_subflows helper

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      | 47 ++++++++++++++++++++++++++++++++++++++++++++
 net/mptcp/protocol.h |  7 +++++++
 2 files changed, 54 insertions(+)
net/mptcp/bpf.c +47 -0
index 6c01f6b959a3..3367541b353c 100644
@@ -160,6 +160,23 @@ 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_array)
+BTF_ID(func, bpf_mptcp_put_subflows_array)
+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))
@@ -168,3 +185,33 @@ struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
 	return NULL;
 }
 EXPORT_SYMBOL(bpf_mptcp_sock_from_subflow);
+
+struct mptcp_subflows_array *bpf_mptcp_get_subflows_array(struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct mptcp_subflows_array *array;
+
+	array = kzalloc(sizeof(*array), GFP_KERNEL);
+	if (!array)
+		return array;
+
+	mptcp_for_each_subflow(msk, subflow)
+		array->subflows[array->nr++] = subflow;
+
+	return array;
+}
+EXPORT_SYMBOL(bpf_mptcp_get_subflows_array);
+
+void bpf_mptcp_put_subflows_array(struct mptcp_subflows_array *array)
+{
+	int i;
+
+	if (!array)
+		return;
+
+	for (i = 0; i < MPTCP_SUBFLOWS_MAX; i++)
+		array->subflows[i] = NULL;
+	array->nr = 0;
+	kfree(array);
+}
+EXPORT_SYMBOL(bpf_mptcp_put_subflows_array);
net/mptcp/protocol.h +7 -0
index 006914cb78de..c42fb54298ef 100644
@@ -500,6 +500,13 @@ struct mptcp_subflow_context {
 	struct	rcu_head rcu;
 };
 
+#define MPTCP_SUBFLOWS_MAX	8
+
+struct mptcp_subflows_array {
+	u8 nr;
+	struct mptcp_subflow_context *subflows[MPTCP_SUBFLOWS_MAX];
+};
+
 static inline struct mptcp_subflow_context *
 mptcp_subflow_ctx(const struct sock *sk)
 {
Signature · 1 line
2.34.1
#3 lore raw

PATCH v8 2/3 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 | 13 +++++
 .../selftests/bpf/progs/mptcp_bpf_rr.c        | 55 +++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c
tools/testing/selftests/bpf/bpf_mptcp_helpers.h +13 -0
index a0b83fbe8133..f0e120dfe48d 100644
@@ -27,10 +27,23 @@ struct mptcp_sched_ops {
 struct mptcp_sock {
 	struct inet_connection_sock	sk;
 
+	struct sock	*last_snd;
 	__u32		token;
 	struct sock	*first;
 	struct mptcp_sched_ops *sched;
 	char		ca_name[TCP_CA_NAME_MAX];
 } __attribute__((preserve_access_index));
 
+struct mptcp_subflow_context {
+	__u32	token;
+	struct	sock *tcp_sock;	    /* tcp sk backpointer */
+} __attribute__((preserve_access_index));
+
+#define MPTCP_SUBFLOWS_MAX	8
+
+struct mptcp_subflows_array {
+	__u8 nr;
+	struct mptcp_subflow_context *subflows[MPTCP_SUBFLOWS_MAX];
+};
+
 #endif
tools/testing/selftests/bpf/progs/mptcp_bpf_rr.c new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH v8 3/3 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
#2 lore raw

Re: [PATCH mptcp-next v8 1/3] mptcp: add bpf get_subflows helper

In reply to Geliang Tang
Quoted 64 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 | 47 ++++++++++++++++++++++++++++++++++++++++++++
net/mptcp/protocol.h | 7 +++++++

2 files changed, 54 insertions(+)
net/mptcp/bpf.c +35 -0
index 6c01f6b959a3..3367541b353c 100644
@@ -160,6 +160,23 @@ 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_array)
+BTF_ID(func, bpf_mptcp_put_subflows_array)
+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))
@@ -168,3 +185,33 @@ struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
 	return NULL;
 }
 EXPORT_SYMBOL(bpf_mptcp_sock_from_subflow);
+
+struct mptcp_subflows_array *bpf_mptcp_get_subflows_array(struct mptcp_sock *msk)
+{
+	struct mptcp_subflow_context *subflow;
+	struct mptcp_subflows_array *array;
+
+	array = kzalloc(sizeof(*array), GFP_KERNEL);
+	if (!array)
+		return array;
+
+	mptcp_for_each_subflow(msk, subflow)
+		array->subflows[array->nr++] = subflow;
+
+	return array;
+}
+EXPORT_SYMBOL(bpf_mptcp_get_subflows_array);
+
+void bpf_mptcp_put_subflows_array(struct mptcp_subflows_array *array)

net/mptcp/bpf.c struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) · line 215

Quoted 10 lines
net/mptcp/bpf.c +10 -0
+{
+	int i;
+
+	if (!array)
+		return;
+
+	for (i = 0; i < MPTCP_SUBFLOWS_MAX; i++)
+		array->subflows[i] = NULL;
+	array->nr = 0;
+	kfree(array);

We can't trust the caller to always call this function, since we don't want to allow userspace to leak this array memory either accidentally or intentionally.

Can BPF code call a helper with a pointer to a struct on the BPF stack?

net/mptcp/protocol.h struct mptcp_subflow_context { · line 503

Quoted 11 lines
net/mptcp/bpf.c +2 -0
+}
+EXPORT_SYMBOL(bpf_mptcp_put_subflows_array);
net/mptcp/protocol.h +1 -0
index 006914cb78de..c42fb54298ef 100644
@@ -500,6 +500,13 @@ struct mptcp_subflow_context {
 	struct	rcu_head rcu;
 };
 
+#define MPTCP_SUBFLOWS_MAX	8

I noticed that this value is separately defined here and in the next commit (tools/testing/selftests/bpf/bpf_mptcp_helpers.h). That seems fragile to me, there's no way for bpf_mptcp_sched_btf_struct_access() to enforce access limits correctly.

Do you have some other ideas for how to share this data between kernel and BPF contexts? Maybe mptcp_sched_data could be expanded to pass the subflow list/array in to the scheduler function.

Quoted 6 lines
net/mptcp/protocol.h +6 -0
+
+struct mptcp_subflows_array {
+	u8 nr;
+	struct mptcp_subflow_context *subflows[MPTCP_SUBFLOWS_MAX];
+};
+
Quoted 8 lines

static inline struct mptcp_subflow_context *
mptcp_subflow_ctx(const struct sock *sk)
{

Signature · 1 line
2.34.1

--
Mat Martineau
Intel