Skip to contentSkip to conversation

BPF packet scheduler

PATCH Superseded Geliang Tang 4 participants 16 messages Mar 29, 2022 – Apr 2, 2022

Subsystems (6)

v8 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#0 lore raw

v8 cover BPF packet scheduler

v8:
- use global sched_list instead of pernet sched_list.
- drop synchronize_rcu() in mptcp_unregister_scheduler().
- update mptcp_init_sched and mptcp_release_sched as Mat and Florian suggested.
- fix the build break in patch 8.
- depends on: "add skc_to_mptcp_sock" v14.
- export/20220325T055307

v7:
- add bpf_try_module_get in mptcp_init_sched.
- add bpf_module_put in mptcp_release_sched.
- rename bpf_first to mptcp_bpf_first.
- update commit logs.

v6:
- still use pernet sched_list, use current->nsproxy->net_ns in BPF context instead of using init_net.
- patch 1:
- use rcu_read_lock instead of spin_lock in mptcp_sched_find as Florian suggested.
- drop synchronize_rcu in sched_exit_net as Florian suggested.
- keep synchronize_rcu in mptcp_unregister_scheduler, otherwise, got a workqueue lockup in my test.
- update Makefile as Mat suggested.
- patch 2:
- add mptcp_sched_data_init to register default sched, instead of registering it in init_net.
- patch 5:
- move mptcp_sched_get_subflow to protocol.h as Mat suggested.
- patch 6:
- use current->nsproxy->net_ns instead of init_net.
- patch 8:
- add send_data to send more data, instead of send_byte.

v5:
- patch 1: define per-namespace sched_list (but only used init_net namespace. It is difficult to get 'net' in bpf_mptcp_sched_reg and bpf_mptcp_sched_unreg. I need some suggestions here.)
- patch 2: skip mptcp_sched_default in mptcp_unregister_scheduler.
- patch 8: add tests into mptcp.c, instead of bpf_tcp_ca.c.

v4:
- set msk->sched to &mptcp_sched_default when the sched argument is NULL
in mptcp_init_sched().

v3:
- add mptcp_release_sched helper in patch 4.
- rename mptcp_set_sched to mptcp_init_sched in patch 4.
- add mptcp_sched_first_release in patch 7.
- do some cleanups.

v2:
- split into more small patches.
- change all parameters of mptcp_sched_ops from sk to msk:

       void (*init)(struct mptcp_sock *msk);
       void (*release)(struct mptcp_sock *msk);
       struct sock *   (*get_subflow)(struct mptcp_sock *msk);

- add tests in bpf_tcp_ca.c, instead of adding a new one.

v1:
- Addressed to the commends in the RFC version.

Geliang Tang (8):
mptcp: add struct mptcp_sched_ops
mptcp: register default scheduler
mptcp: add a new sysctl scheduler
mptcp: add sched in mptcp_sock
mptcp: add get_subflow wrapper
mptcp: add bpf_mptcp_sched_ops
selftests: bpf: add bpf_first scheduler
selftests: bpf: add bpf_first test

 Documentation/networking/mptcp-sysctl.rst     |   8 ++
 include/net/mptcp.h                           |  13 ++
 kernel/bpf/bpf_struct_ops_types.h             |   4 +
 net/mptcp/Makefile                            |   2 +-
 net/mptcp/bpf.c                               | 102 ++++++++++++++++
 net/mptcp/ctrl.c                              |  14 +++
 net/mptcp/protocol.c                          |  15 ++-
 net/mptcp/protocol.h                          |  16 +++
 net/mptcp/sched.c                             | 104 ++++++++++++++++
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  12 ++
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 114 ++++++++++++++++++
 .../selftests/bpf/progs/mptcp_bpf_first.c     |  30 +++++
 12 files changed, 429 insertions(+), 5 deletions(-)
 create mode 100644 net/mptcp/sched.c
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_first.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v8 1/8 mptcp: add struct mptcp_sched_ops

This patch defines struct mptcp_sched_ops, which has three struct members, name, owner and list, and three function pointers, init, release and get_subflow.

Add the scheduler registering, unregistering and finding functions to add or delete or find a packet scheduler on mptcp_sched_list.

---
 include/net/mptcp.h  | 13 ++++++++++
 net/mptcp/Makefile   |  2 +-
 net/mptcp/protocol.h |  3 +++
 net/mptcp/sched.c    | 56 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 net/mptcp/sched.c
include/net/mptcp.h +13 -0
index 8b1afd6f5cc4..e3a0baa8dbd7 100644
@@ -95,6 +95,19 @@ struct mptcp_out_options {
 #endif
 };
 
+#define MPTCP_SCHED_NAME_MAX 16
+
+struct mptcp_sched_ops {
+	struct sock *	(*get_subflow)(struct mptcp_sock *msk);
+
+	char			name[MPTCP_SCHED_NAME_MAX];
+	struct module		*owner;
+	struct list_head	list;
+
+	void (*init)(struct mptcp_sock *msk);
+	void (*release)(struct mptcp_sock *msk);
+} ____cacheline_aligned_in_smp;
+
 #ifdef CONFIG_MPTCP
 extern struct request_sock_ops mptcp_subflow_request_sock_ops;
 
net/mptcp/Makefile +1 -1
index 168c55d1c917..a37330760b0c 100644
@@ -2,7 +2,7 @@
 obj-$(CONFIG_MPTCP) += mptcp.o
 
 mptcp-y := protocol.o subflow.o options.o token.o crypto.o ctrl.o pm.o diag.o \
-	   mib.o pm_netlink.o sockopt.o
+	   mib.o pm_netlink.o sockopt.o sched.o
 
 obj-$(CONFIG_SYN_COOKIES) += syncookies.o
 obj-$(CONFIG_INET_MPTCP_DIAG) += mptcp_diag.o
net/mptcp/protocol.h +3 -0
index fd82fd113113..9d264fd4aabe 100644
@@ -608,6 +608,9 @@ int mptcp_subflow_create_socket(struct sock *sk, struct socket **new_sock);
 void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
 			 struct sockaddr_storage *addr,
 			 unsigned short family);
+struct mptcp_sched_ops *mptcp_sched_find(const char *name);
+int mptcp_register_scheduler(struct mptcp_sched_ops *sched);
+void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched);
 
 static inline bool __mptcp_subflow_active(struct mptcp_subflow_context *subflow)
 {
net/mptcp/sched.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH v8 2/8 mptcp: register default scheduler

In reply to Geliang Tang

This patch defines the default packet scheduler mptcp_sched_default, registers it in mptcp_sched_init(). Skip deleting this default scheduler in mptcp_unregister_scheduler().

---
 net/mptcp/protocol.c |  2 +-
 net/mptcp/protocol.h |  2 ++
 net/mptcp/sched.c    | 14 ++++++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +1 -1
index d3887f628b54..6c2d6b932d25 100644
@@ -1424,7 +1424,7 @@ bool mptcp_subflow_active(struct mptcp_subflow_context *subflow)
  * returns the subflow that will transmit the next DSS
  * additionally updates the rtx timeout
  */
-static struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
+struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk)
 {
 	struct subflow_send_info send_info[SSK_MODE_MAX];
 	struct mptcp_subflow_context *subflow;
net/mptcp/protocol.h +2 -0
index 9d264fd4aabe..76b98240921f 100644
@@ -611,6 +611,8 @@ void mptcp_info2sockaddr(const struct mptcp_addr_info *info,
 struct mptcp_sched_ops *mptcp_sched_find(const char *name);
 int mptcp_register_scheduler(struct mptcp_sched_ops *sched);
 void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched);
+struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk);
+void mptcp_sched_init(void);
 
 static inline bool __mptcp_subflow_active(struct mptcp_subflow_context *subflow)
 {
net/mptcp/sched.c +14 -0
index c5d3bbafba71..52828eb741c0 100644
@@ -13,6 +13,12 @@
 #include <linux/spinlock.h>
 #include "protocol.h"
 
+static struct mptcp_sched_ops mptcp_sched_default = {
+	.get_subflow    = mptcp_subflow_get_send,
+	.name           = "default",
+	.owner          = THIS_MODULE,
+};
+
 static DEFINE_SPINLOCK(mptcp_sched_list_lock);
 static LIST_HEAD(mptcp_sched_list);
 
@@ -50,7 +56,15 @@ int mptcp_register_scheduler(struct mptcp_sched_ops *sched)
 
 void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched)
 {
+	if (sched == &mptcp_sched_default)
+		return;
+
 	spin_lock(&mptcp_sched_list_lock);
 	list_del_rcu(&sched->list);
 	spin_unlock(&mptcp_sched_list_lock);
 }
+
+void mptcp_sched_init(void)
+{
+	mptcp_register_scheduler(&mptcp_sched_default);
+}
Signature · 1 line
2.34.1
#3 lore raw

PATCH v8 3/8 mptcp: add a new sysctl scheduler

In reply to Geliang Tang

This patch adds a new sysctl, named scheduler, to support for selection of different schedulers. Export mptcp_get_scheduler helper to get this sysctl.

---
 Documentation/networking/mptcp-sysctl.rst |  8 ++++++++
 net/mptcp/ctrl.c                          | 14 ++++++++++++++
 net/mptcp/protocol.h                      |  1 +
 3 files changed, 23 insertions(+)
Documentation/networking/mptcp-sysctl.rst +8 -0
index e263dfcc4b40..d9e69fdc7ea3 100644
@@ -75,3 +75,11 @@ stale_loss_cnt - INTEGER
 	This is a per-namespace sysctl.
 
 	Default: 4
+
+scheduler - STRING
+	Select the scheduler of your choice.
+
+	Support for selection of different schedulers. This is a per-namespace
+	sysctl.
+
+	Default: "default"
net/mptcp/ctrl.c +14 -0
index ae20b7d92e28..c46c22a84d23 100644
@@ -32,6 +32,7 @@ struct mptcp_pernet {
 	u8 checksum_enabled;
 	u8 allow_join_initial_addr_port;
 	u8 pm_type;
+	char scheduler[MPTCP_SCHED_NAME_MAX];
 };
 
 static struct mptcp_pernet *mptcp_get_pernet(const struct net *net)
@@ -69,6 +70,11 @@ int mptcp_get_pm_type(const struct net *net)
 	return mptcp_get_pernet(net)->pm_type;
 }
 
+const char *mptcp_get_scheduler(const struct net *net)
+{
+	return mptcp_get_pernet(net)->scheduler;
+}
+
 static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
 {
 	pernet->mptcp_enabled = 1;
@@ -77,6 +83,7 @@ static void mptcp_pernet_set_defaults(struct mptcp_pernet *pernet)
 	pernet->allow_join_initial_addr_port = 1;
 	pernet->stale_loss_cnt = 4;
 	pernet->pm_type = MPTCP_PM_TYPE_KERNEL;
+	strcpy(pernet->scheduler, "default");
 }
 
 #ifdef CONFIG_SYSCTL
@@ -128,6 +135,12 @@ static struct ctl_table mptcp_sysctl_table[] = {
 		.extra1       = SYSCTL_ZERO,
 		.extra2       = &mptcp_pm_type_max
 	},
+	{
+		.procname = "scheduler",
+		.maxlen	= MPTCP_SCHED_NAME_MAX,
+		.mode = 0644,
+		.proc_handler = proc_dostring,
+	},
 	{}
 };
 
@@ -149,6 +162,7 @@ static int mptcp_pernet_new_table(struct net *net, struct mptcp_pernet *pernet)
 	table[3].data = &pernet->allow_join_initial_addr_port;
 	table[4].data = &pernet->stale_loss_cnt;
 	table[5].data = &pernet->pm_type;
+	table[6].data = &pernet->scheduler;
 
 	hdr = register_net_sysctl(net, MPTCP_SYSCTL_PATH, table);
 	if (!hdr)
net/mptcp/protocol.h +1 -0
index 76b98240921f..e9fbed723caf 100644
@@ -586,6 +586,7 @@ int mptcp_is_checksum_enabled(const struct net *net);
 int mptcp_allow_join_id0(const struct net *net);
 unsigned int mptcp_stale_loss_cnt(const struct net *net);
 int mptcp_get_pm_type(const struct net *net);
+const char *mptcp_get_scheduler(const struct net *net);
 void mptcp_subflow_fully_established(struct mptcp_subflow_context *subflow,
 				     struct mptcp_options_received *mp_opt);
 bool __mptcp_retransmit_pending_data(struct sock *sk);
Signature · 1 line
2.34.1
#4 lore raw

PATCH v8 4/8 mptcp: add sched in mptcp_sock

In reply to Geliang Tang

This patch adds a new struct member sched in struct mptcp_sock. And two helpers mptcp_init_sched() and mptcp_release_sched() to init and release it.

Init it with the sysctl scheduler in mptcp_init_sock(), copy the scheduler from the parent in mptcp_sk_clone(), and release it in __mptcp_destroy_sock().

---
 net/mptcp/protocol.c |  7 +++++++
 net/mptcp/protocol.h |  4 ++++
 net/mptcp/sched.c    | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+)
net/mptcp/protocol.c +7 -0
index 6c2d6b932d25..e65b4dfcb758 100644
@@ -2657,6 +2657,11 @@ static int mptcp_init_sock(struct sock *sk)
 	if (ret)
 		return ret;
 
+	ret = mptcp_init_sched(mptcp_sk(sk),
+			       mptcp_sched_find(mptcp_get_scheduler(net)));
+	if (ret)
+		return ret;
+
 	/* fetch the ca name; do it outside __mptcp_init_sock(), so that clone will
 	 * propagate the correct value
 	 */
@@ -2816,6 +2821,7 @@ static void __mptcp_destroy_sock(struct sock *sk)
 	sk_stop_timer(sk, &sk->sk_timer);
 	mptcp_data_unlock(sk);
 	msk->pm.status = 0;
+	mptcp_release_sched(msk);
 
 	/* clears msk->subflow, allowing the following loop to close
 	 * even the initial subflow
@@ -2993,6 +2999,7 @@ struct sock *mptcp_sk_clone(const struct sock *sk,
 	msk->snd_una = msk->write_seq;
 	msk->wnd_end = msk->snd_nxt + req->rsk_rcv_wnd;
 	msk->setsockopt_seq = mptcp_sk(sk)->setsockopt_seq;
+	mptcp_init_sched(msk, mptcp_sk(sk)->sched);
 
 	if (mp_opt->suboptions & OPTIONS_MPTCP_MPC) {
 		msk->can_ack = true;
net/mptcp/protocol.h +4 -0
index e9fbed723caf..74d2b05f5d6b 100644
@@ -288,6 +288,7 @@ struct mptcp_sock {
 	struct socket	*subflow; /* outgoing connect/listener/!mp_capable */
 	struct sock	*first;
 	struct mptcp_pm_data	pm;
+	struct mptcp_sched_ops	*sched;
 	struct {
 		u32	space;	/* bytes copied in last measurement window */
 		u32	copied; /* bytes copied in this measurement window */
@@ -614,6 +615,9 @@ int mptcp_register_scheduler(struct mptcp_sched_ops *sched);
 void mptcp_unregister_scheduler(struct mptcp_sched_ops *sched);
 struct sock *mptcp_subflow_get_send(struct mptcp_sock *msk);
 void mptcp_sched_init(void);
+int mptcp_init_sched(struct mptcp_sock *msk,
+		     struct mptcp_sched_ops *sched);
+void mptcp_release_sched(struct mptcp_sock *msk);
 
 static inline bool __mptcp_subflow_active(struct mptcp_subflow_context *subflow)
 {
net/mptcp/sched.c +34 -0
index 52828eb741c0..5d20d252088f 100644
@@ -68,3 +68,37 @@ void mptcp_sched_init(void)
 {
 	mptcp_register_scheduler(&mptcp_sched_default);
 }
+
+int mptcp_init_sched(struct mptcp_sock *msk,
+		     struct mptcp_sched_ops *sched)
+{
+	struct mptcp_sched_ops *sched_init = &mptcp_sched_default;
+
+	if (sched)
+		sched_init = sched;
+
+	if (!bpf_try_module_get(sched_init, sched_init->owner))
+		return -EBUSY;
+
+	msk->sched = sched_init;
+	if (msk->sched->init)
+		msk->sched->init(msk);
+
+	pr_debug("sched=%s", msk->sched->name);
+
+	return 0;
+}
+
+void mptcp_release_sched(struct mptcp_sock *msk)
+{
+	struct mptcp_sched_ops *sched = msk->sched;
+
+	if (!sched)
+		return;
+
+	msk->sched = NULL;
+	if (sched->release)
+		sched->release(msk);
+
+	bpf_module_put(sched, sched->owner);
+}
Signature · 1 line
2.34.1
#5 lore raw

PATCH v8 5/8 mptcp: add get_subflow wrapper

In reply to Geliang Tang

This patch defines a new wrapper mptcp_sched_get_subflow(), invoke get_subflow of msk->sched in it. Use the wrapper instead of using mptcp_subflow_get_send() directly.

---
 net/mptcp/protocol.c | 6 +++---
 net/mptcp/protocol.h | 6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)
net/mptcp/protocol.c +3 -3
index e65b4dfcb758..ac00647d94cc 100644
@@ -1567,7 +1567,7 @@ void __mptcp_push_pending(struct sock *sk, unsigned int flags)
 			int ret = 0;
 
 			prev_ssk = ssk;
-			ssk = mptcp_subflow_get_send(msk);
+			ssk = mptcp_sched_get_subflow(msk);
 
 			/* First check. If the ssk has changed since
 			 * the last round, release prev_ssk
@@ -1636,7 +1636,7 @@ static void __mptcp_subflow_push_pending(struct sock *sk, struct sock *ssk)
 			 * check for a different subflow usage only after
 			 * spooling the first chunk of data
 			 */
-			xmit_ssk = first ? ssk : mptcp_subflow_get_send(mptcp_sk(sk));
+			xmit_ssk = first ? ssk : mptcp_sched_get_subflow(mptcp_sk(sk));
 			if (!xmit_ssk)
 				goto out;
 			if (xmit_ssk != ssk) {
@@ -3137,7 +3137,7 @@ void __mptcp_check_push(struct sock *sk, struct sock *ssk)
 		return;
 
 	if (!sock_owned_by_user(sk)) {
-		struct sock *xmit_ssk = mptcp_subflow_get_send(mptcp_sk(sk));
+		struct sock *xmit_ssk = mptcp_sched_get_subflow(mptcp_sk(sk));
 
 		if (xmit_ssk == ssk)
 			__mptcp_subflow_push_pending(sk, ssk);
net/mptcp/protocol.h +6 -0
index 74d2b05f5d6b..a60aa588b1a7 100644
@@ -619,6 +619,12 @@ int mptcp_init_sched(struct mptcp_sock *msk,
 		     struct mptcp_sched_ops *sched);
 void mptcp_release_sched(struct mptcp_sock *msk);
 
+static inline struct sock *mptcp_sched_get_subflow(struct mptcp_sock *msk)
+{
+	return INDIRECT_CALL_INET_1(msk->sched->get_subflow,
+				    mptcp_subflow_get_send, msk);
+}
+
 static inline bool __mptcp_subflow_active(struct mptcp_subflow_context *subflow)
 {
 	struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
Signature · 1 line
2.34.1
#6 lore raw

PATCH v8 6/8 mptcp: add bpf_mptcp_sched_ops

In reply to Geliang Tang

This patch implements a new struct bpf_struct_ops, bpf_mptcp_sched_ops. Register and unregister the bpf scheduler in .reg and .unreg.

This implementation is similar to BPF TCP CC. And some code in this patch is from bpf_tcp_ca.c

---
 kernel/bpf/bpf_struct_ops_types.h |   4 ++
 net/mptcp/bpf.c                   | 102 ++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)
kernel/bpf/bpf_struct_ops_types.h +4 -0
index 5678a9ddf817..5a6b0c0d8d3d 100644
@@ -8,5 +8,9 @@ BPF_STRUCT_OPS_TYPE(bpf_dummy_ops)
 #ifdef CONFIG_INET
 #include <net/tcp.h>
 BPF_STRUCT_OPS_TYPE(tcp_congestion_ops)
+#ifdef CONFIG_MPTCP
+#include <net/mptcp.h>
+BPF_STRUCT_OPS_TYPE(mptcp_sched_ops)
+#endif
 #endif
 #endif
net/mptcp/bpf.c +102 -0

110 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

PATCH v8 7/8 selftests: bpf: add bpf_first scheduler

In reply to Geliang Tang

This patch implements the simplest MPTCP scheduler, named bpf_first, which always picks the first subflow to send data. It's a sample of MPTCP BPF scheduler implementations.

---
 tools/testing/selftests/bpf/bpf_tcp_helpers.h | 12 ++++++++
 .../selftests/bpf/progs/mptcp_bpf_first.c     | 30 +++++++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_first.c
tools/testing/selftests/bpf/bpf_tcp_helpers.h +12 -0
index f92357597e63..5bb21b24231f 100644
@@ -197,6 +197,18 @@ struct tcp_congestion_ops {
 	void *owner;
 };
 
+#define MPTCP_SCHED_NAME_MAX 16
+
+struct mptcp_sched_ops {
+	char name[MPTCP_SCHED_NAME_MAX];
+
+	void (*init)(struct mptcp_sock *msk);
+	void (*release)(struct mptcp_sock *msk);
+
+	struct sock *	(*get_subflow)(struct mptcp_sock *msk);
+	void *owner;
+};
+
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 #define min_not_zero(x, y) ({			\
tools/testing/selftests/bpf/progs/mptcp_bpf_first.c new file +30 -0
new file mode 100644
index 000000000000..3ae5c1eab5f5
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Copyright (c) 2022, SUSE. */
+
+#include <linux/bpf.h>
+#include "bpf_tcp_helpers.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops/mptcp_sched_first_init")
+void BPF_PROG(mptcp_sched_first_init, struct mptcp_sock *msk)
+{
+}
+
+SEC("struct_ops/mptcp_sched_first_release")
+void BPF_PROG(mptcp_sched_first_release, struct mptcp_sock *msk)
+{
+}
+
+struct sock *BPF_STRUCT_OPS(bpf_first_get_subflow, struct mptcp_sock *msk)
+{
+	return msk->first;
+}
+
+SEC(".struct_ops")
+struct mptcp_sched_ops first = {
+	.init		= (void *)mptcp_sched_first_init,
+	.release	= (void *)mptcp_sched_first_release,
+	.get_subflow	= (void *)bpf_first_get_subflow,
+	.name		= "bpf_first",
+};
Signature · 1 line
2.34.1
#8 lore raw

PATCH v8 8/8 selftests: bpf: add bpf_first test

In reply to Geliang Tang

This patch expends the MPTCP test base to support MPTCP packet scheduler tests. Add the bpf_first scheduler test in it. Use sysctl to set net.mptcp.scheduler to use this sched.

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

---
 .../testing/selftests/bpf/prog_tests/mptcp.c  | 114 ++++++++++++++++++
 1 file changed, 114 insertions(+)
tools/testing/selftests/bpf/prog_tests/mptcp.c +114 -0
index 9b73ef62ee74..715d1b2d6fce 100644
@@ -4,6 +4,9 @@
 #include <test_progs.h>
 #include "cgroup_helpers.h"
 #include "network_helpers.h"
+#include "mptcp_bpf_first.skel.h"
+
+#define min(a, b) ((a) < (b) ? (a) : (b))
 
 #ifndef TCP_CA_NAME_MAX
 #define TCP_CA_NAME_MAX	16
@@ -19,6 +22,8 @@ struct mptcp_storage {
 };
 
 static char monitor_log_path[64];
+static const unsigned int total_bytes = 10 * 1024 * 1024;
+static int stop, duration;
 
 static int verify_tsk(int map_fd, int client_fd)
 {
@@ -251,8 +256,117 @@ void test_base(void)
 	close(cgroup_fd);
 }
 
+static void *server(void *arg)
+{
+	int lfd = (int)(long)arg, err = 0, fd;
+	ssize_t nr_sent = 0, bytes = 0;
+	char batch[1500];
+
+	fd = accept(lfd, NULL, NULL);
+	while (fd == -1) {
+		if (errno == EINTR)
+			continue;
+		err = -errno;
+		goto done;
+	}
+
+	if (settimeo(fd, 0)) {
+		err = -errno;
+		goto done;
+	}
+
+	while (bytes < total_bytes && !READ_ONCE(stop)) {
+		nr_sent = send(fd, &batch,
+			       min(total_bytes - bytes, sizeof(batch)), 0);
+		if (nr_sent == -1 && errno == EINTR)
+			continue;
+		if (nr_sent == -1) {
+			err = -errno;
+			break;
+		}
+		bytes += nr_sent;
+	}
+
+	CHECK(bytes != total_bytes, "send", "%zd != %u nr_sent:%zd errno:%d\n",
+	      bytes, total_bytes, nr_sent, errno);
+
+done:
+	if (fd >= 0)
+		close(fd);
+	if (err) {
+		WRITE_ONCE(stop, 1);
+		return ERR_PTR(err);
+	}
+	return NULL;
+}
+
+static void send_data(int lfd, int fd)
+{
+	ssize_t nr_recv = 0, bytes = 0;
+	pthread_t srv_thread;
+	void *thread_ret;
+	char batch[1500];
+	int err;
+
+	WRITE_ONCE(stop, 0);
+
+	err = pthread_create(&srv_thread, NULL, server, (void *)(long)lfd);
+	if (CHECK(err != 0, "pthread_create", "err:%d errno:%d\n", err, errno))
+		return;
+
+	/* recv total_bytes */
+	while (bytes < total_bytes && !READ_ONCE(stop)) {
+		nr_recv = recv(fd, &batch,
+			       min(total_bytes - bytes, sizeof(batch)), 0);
+		if (nr_recv == -1 && errno == EINTR)
+			continue;
+		if (nr_recv == -1)
+			break;
+		bytes += nr_recv;
+	}
+
+	CHECK(bytes != total_bytes, "recv", "%zd != %u nr_recv:%zd errno:%d\n",
+	      bytes, total_bytes, nr_recv, errno);
+
+	WRITE_ONCE(stop, 1);
+
+	pthread_join(srv_thread, &thread_ret);
+	CHECK(IS_ERR(thread_ret), "pthread_join", "thread_ret:%ld",
+	      PTR_ERR(thread_ret));
+}
+
+static void test_first(void)
+{
+	struct mptcp_bpf_first *first_skel;
+	int server_fd, client_fd;
+	struct bpf_link *link;
+
+	first_skel = mptcp_bpf_first__open_and_load();
+	if (CHECK(!first_skel, "bpf_first__open_and_load", "failed\n"))
+		return;
+
+	link = bpf_map__attach_struct_ops(first_skel->maps.first);
+	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
+		mptcp_bpf_first__destroy(first_skel);
+		return;
+	}
+
+	system("sysctl -q net.mptcp.scheduler=bpf_first");
+	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);
+	bpf_link__destroy(link);
+	mptcp_bpf_first__destroy(first_skel);
+}
+
 void test_mptcp(void)
 {
 	if (test__start_subtest("base"))
 		test_base();
+	if (test__start_subtest("first"))
+		test_first();
 }
Signature · 1 line
2.34.1
#9 lore raw

Re: selftests: bpf: add bpf_first test: Build Failure

Hi Geliang,

Thank you for your modifications, that's great!

But sadly, our CI spotted some issues with it when trying to build it.

You can find more details there:

https://patchwork.kernel.org/project/mptcp/patch/5ec49efb1dfd2777e779bf4362c6bbe67d89089c.1648537203.git.geliang.tang@suse.com/
https://github.com/multipath-tcp/mptcp_net-next/actions/runs/2066153213

Status: failure
Initiator: MPTCPimporter
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/8b1e8ed0e57e

Feel free to reply to this email if you cannot access logs, if you need some support to fix the error, if this doesn't seem to be caused by your modifications or if the error is a false positive one.

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

#10 lore raw

Re: selftests: bpf: add bpf_first test: Tests Results

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! ✅:
- Task: https://cirrus-ci.com/task/6543244744982528
- Summary: https://api.cirrus-ci.com/v1/artifact/task/6543244744982528/summary/summary.txt

- KVM Validation: debug:
- Success! ✅:
- Task: https://cirrus-ci.com/task/4713657396363264
- Summary: https://api.cirrus-ci.com/v1/artifact/task/4713657396363264/summary/summary.txt

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/8b1e8ed0e57e

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 (Tessares)

#11 lore raw
In reply to Geliang Tang
Quoted 2 lines · Geliang Tang

v8:
- use global sched_list instead of pernet sched_list.

Yes, I think this is fine. I had initially asked about pernet configuration with respect to registering BPF schedulers, but the important thing is that the sysctl can be set per-namespace.

Quoted 6 lines

- drop synchronize_rcu() in mptcp_unregister_scheduler().
- update mptcp_init_sched and mptcp_release_sched as Mat and Florian suggested.
- fix the build break in patch 8.
- depends on: "add skc_to_mptcp_sock" v14.
- export/20220325T055307

Thanks for updating. Builds and runs fine here.

Before we add this to the export branch, have you thought about how subflow data (including the backup bit and throughput/latency data used by the default scheduler) can be accessed in the BPF get_subflow hook? Do you think that can be cleanly added after this series, or is there anything that may need to be changed in this series?

Thanks,

Mat

Quoted 79 lines

v7:
- add bpf_try_module_get in mptcp_init_sched.
- add bpf_module_put in mptcp_release_sched.
- rename bpf_first to mptcp_bpf_first.
- update commit logs.

v6:
- still use pernet sched_list, use current->nsproxy->net_ns in BPF context instead of using init_net.
- patch 1:
- use rcu_read_lock instead of spin_lock in mptcp_sched_find as Florian suggested.
- drop synchronize_rcu in sched_exit_net as Florian suggested.
- keep synchronize_rcu in mptcp_unregister_scheduler, otherwise, got a workqueue lockup in my test.
- update Makefile as Mat suggested.
- patch 2:
- add mptcp_sched_data_init to register default sched, instead of registering it in init_net.
- patch 5:
- move mptcp_sched_get_subflow to protocol.h as Mat suggested.
- patch 6:
- use current->nsproxy->net_ns instead of init_net.
- patch 8:
- add send_data to send more data, instead of send_byte.

v5:
- patch 1: define per-namespace sched_list (but only used init_net namespace. It is difficult to get 'net' in bpf_mptcp_sched_reg and bpf_mptcp_sched_unreg. I need some suggestions here.)
- patch 2: skip mptcp_sched_default in mptcp_unregister_scheduler.
- patch 8: add tests into mptcp.c, instead of bpf_tcp_ca.c.

v4:
- set msk->sched to &mptcp_sched_default when the sched argument is NULL in mptcp_init_sched().

v3:
- add mptcp_release_sched helper in patch 4.
- rename mptcp_set_sched to mptcp_init_sched in patch 4.
- add mptcp_sched_first_release in patch 7.
- do some cleanups.

v2:
- split into more small patches.
- change all parameters of mptcp_sched_ops from sk to msk:

      void (*init)(struct mptcp_sock *msk);
      void (*release)(struct mptcp_sock *msk);
      struct sock *   (*get_subflow)(struct mptcp_sock *msk);

- add tests in bpf_tcp_ca.c, instead of adding a new one.

v1:
- Addressed to the commends in the RFC version.

Geliang Tang (8):
mptcp: add struct mptcp_sched_ops
mptcp: register default scheduler
mptcp: add a new sysctl scheduler
mptcp: add sched in mptcp_sock
mptcp: add get_subflow wrapper
mptcp: add bpf_mptcp_sched_ops
selftests: bpf: add bpf_first scheduler
selftests: bpf: add bpf_first test

Documentation/networking/mptcp-sysctl.rst     |   8 ++
include/net/mptcp.h                           |  13 ++
kernel/bpf/bpf_struct_ops_types.h             |   4 +
net/mptcp/Makefile                            |   2 +-
net/mptcp/bpf.c                               | 102 ++++++++++++++++
net/mptcp/ctrl.c                              |  14 +++
net/mptcp/protocol.c                          |  15 ++-
net/mptcp/protocol.h                          |  16 +++
net/mptcp/sched.c                             | 104 ++++++++++++++++
tools/testing/selftests/bpf/bpf_tcp_helpers.h |  12 ++
.../testing/selftests/bpf/prog_tests/mptcp.c  | 114 ++++++++++++++++++
.../selftests/bpf/progs/mptcp_bpf_first.c     |  30 +++++
12 files changed, 429 insertions(+), 5 deletions(-)
Quoted 8 lines

create mode 100644 net/mptcp/sched.c
create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_first.c

Signature · 1 line
2.34.1

--
Mat Martineau
Intel

#12 lore raw

Hi Mat & Matt,

Quoted 17 lines · Mat Martineau
Quoted 2 lines · Geliang Tang

v8:
- use global sched_list instead of pernet sched_list.

Yes, I think this is fine. I had initially asked about pernet configuration with respect to registering BPF schedulers, but the important thing is that the sysctl can be set per-namespace.

Quoted 6 lines

- drop synchronize_rcu() in mptcp_unregister_scheduler().
- update mptcp_init_sched and mptcp_release_sched as Mat and Florian suggested.
- fix the build break in patch 8.
- depends on: "add skc_to_mptcp_sock" v14.
- export/20220325T055307

Quoted 7 lines

Thanks for updating. Builds and runs fine here.

Before we add this to the export branch, have you thought about how subflow data (including the backup bit and throughput/latency data used by the default scheduler) can be accessed in the BPF get_subflow hook? Do you think that can be cleanly added after this series, or is there anything that may need to be changed in this series?

I plan to do that in the next series, BPF round-robin scheduler.

I had implemented round-robin in kernel before:

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

This time I plan to implement it using BPF.

In order to support bpf_rr, I think we need to do two more things based on this series:

1. Get the subflow data from BPF, maybe iterate over the subflows from mptcp_sock.

2. Make same members of struct mptcp_sock writable in BPF, say last_snd, then we can set it like this:

        msk->last_snd = ssk.

Neither of these things is easy to implement. I'd like to hear your opinions.

Thanks,
-Geliang

Quoted 98 lines

Thanks,

Mat

Quoted 79 lines

v7:
- add bpf_try_module_get in mptcp_init_sched.
- add bpf_module_put in mptcp_release_sched.
- rename bpf_first to mptcp_bpf_first.
- update commit logs.

v6:
- still use pernet sched_list, use current->nsproxy->net_ns in BPF context instead of using init_net.
- patch 1:
- use rcu_read_lock instead of spin_lock in mptcp_sched_find as Florian suggested.
- drop synchronize_rcu in sched_exit_net as Florian suggested.
- keep synchronize_rcu in mptcp_unregister_scheduler, otherwise, got a workqueue lockup in my test.
- update Makefile as Mat suggested.
- patch 2:
- add mptcp_sched_data_init to register default sched, instead of registering it in init_net.
- patch 5:
- move mptcp_sched_get_subflow to protocol.h as Mat suggested.
- patch 6:
- use current->nsproxy->net_ns instead of init_net.
- patch 8:
- add send_data to send more data, instead of send_byte.

v5:
- patch 1: define per-namespace sched_list (but only used init_net namespace. It is difficult to get 'net' in bpf_mptcp_sched_reg and bpf_mptcp_sched_unreg. I need some suggestions here.)
- patch 2: skip mptcp_sched_default in mptcp_unregister_scheduler.
- patch 8: add tests into mptcp.c, instead of bpf_tcp_ca.c.

v4:
- set msk->sched to &mptcp_sched_default when the sched argument is NULL in mptcp_init_sched().

v3:
- add mptcp_release_sched helper in patch 4.
- rename mptcp_set_sched to mptcp_init_sched in patch 4.
- add mptcp_sched_first_release in patch 7.
- do some cleanups.

v2:
- split into more small patches.
- change all parameters of mptcp_sched_ops from sk to msk:

      void (*init)(struct mptcp_sock *msk);
      void (*release)(struct mptcp_sock *msk);
      struct sock *   (*get_subflow)(struct mptcp_sock *msk);

- add tests in bpf_tcp_ca.c, instead of adding a new one.

v1:
- Addressed to the commends in the RFC version.

Geliang Tang (8):
mptcp: add struct mptcp_sched_ops
mptcp: register default scheduler
mptcp: add a new sysctl scheduler
mptcp: add sched in mptcp_sock
mptcp: add get_subflow wrapper
mptcp: add bpf_mptcp_sched_ops
selftests: bpf: add bpf_first scheduler
selftests: bpf: add bpf_first test

Documentation/networking/mptcp-sysctl.rst     |   8 ++
include/net/mptcp.h                           |  13 ++
kernel/bpf/bpf_struct_ops_types.h             |   4 +
net/mptcp/Makefile                            |   2 +-
net/mptcp/bpf.c                               | 102 ++++++++++++++++
net/mptcp/ctrl.c                              |  14 +++
net/mptcp/protocol.c                          |  15 ++-
net/mptcp/protocol.h                          |  16 +++
net/mptcp/sched.c                             | 104 ++++++++++++++++
tools/testing/selftests/bpf/bpf_tcp_helpers.h |  12 ++
.../testing/selftests/bpf/prog_tests/mptcp.c  | 114 ++++++++++++++++++
.../selftests/bpf/progs/mptcp_bpf_first.c     |  30 +++++
12 files changed, 429 insertions(+), 5 deletions(-)
Quoted 8 lines

create mode 100644 net/mptcp/sched.c
create mode 100644 tools/testing/selftests/bpf/progs/mptcp_bpf_first.c

Signature · 1 line
2.34.1

--
Mat Martineau
Intel

#13 lore raw

Hi Geliang,

Quoted 28 lines · Geliang Tang

Hi Mat & Matt,

Quoted 17 lines · Mat Martineau
Quoted 2 lines · Geliang Tang

v8:
- use global sched_list instead of pernet sched_list.

Yes, I think this is fine. I had initially asked about pernet configuration with respect to registering BPF schedulers, but the important thing is that the sysctl can be set per-namespace.

Quoted 6 lines

- drop synchronize_rcu() in mptcp_unregister_scheduler().
- update mptcp_init_sched and mptcp_release_sched as Mat and Florian suggested.
- fix the build break in patch 8.
- depends on: "add skc_to_mptcp_sock" v14.
- export/20220325T055307

Quoted 7 lines

Thanks for updating. Builds and runs fine here.

Before we add this to the export branch, have you thought about how subflow data (including the backup bit and throughput/latency data used by the default scheduler) can be accessed in the BPF get_subflow hook? Do you think that can be cleanly added after this series, or is there anything that may need to be changed in this series?

Quoted 7 lines

I plan to do that in the next series, BPF round-robin scheduler.

I had implemented round-robin in kernel before:

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

This time I plan to implement it using BPF.

Sounds like a good idea to exercise his new BPF packet scheduler infrastructure, thanks!

Quoted 5 lines

In order to support bpf_rr, I think we need to do two more things based on this series:

1. Get the subflow data from BPF, maybe iterate over the subflows from mptcp_sock.

Yes, sounds good to me. What is blocking to do that? Maybe some new BPF helpers will be needed?

Quoted 4 lines

2. Make same members of struct mptcp_sock writable in BPF, say last_snd, then we can set it like this:

        msk->last_snd = ssk.

I don't think this is a direction to take with BPF. I mean: not directly. You can have helper doing that with extra verifications. How is it done with TCP CA?

Or maybe for this specific case here with RR, you can also store the last snd subflow in a MAP storage, no?

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#14 lore raw
In reply to Geliang Tang
Quoted 34 lines · Geliang Tang

Hi Mat & Matt,

Quoted 17 lines · Mat Martineau
Quoted 2 lines · Geliang Tang

v8:
- use global sched_list instead of pernet sched_list.

Yes, I think this is fine. I had initially asked about pernet configuration with respect to registering BPF schedulers, but the important thing is that the sysctl can be set per-namespace.

Quoted 6 lines

- drop synchronize_rcu() in mptcp_unregister_scheduler().
- update mptcp_init_sched and mptcp_release_sched as Mat and Florian suggested.
- fix the build break in patch 8.
- depends on: "add skc_to_mptcp_sock" v14.
- export/20220325T055307

Quoted 7 lines

Thanks for updating. Builds and runs fine here.

Before we add this to the export branch, have you thought about how subflow data (including the backup bit and throughput/latency data used by the default scheduler) can be accessed in the BPF get_subflow hook? Do you think that can be cleanly added after this series, or is there anything that may need to be changed in this series?

I plan to do that in the next series, BPF round-robin scheduler.

I had implemented round-robin in kernel before:

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

Quoted 8 lines

This time I plan to implement it using BPF.

In order to support bpf_rr, I think we need to do two more things based on this series:

1. Get the subflow data from BPF, maybe iterate over the subflows from mptcp_sock.

Like we discussed in the meeting, I think a helper function that's callable from the BPF code could gather the necessary information from the subflows in a safe way.

Quoted 4 lines

2. Make same members of struct mptcp_sock writable in BPF, say last_snd, then we can set it like this:

       msk->last_snd = ssk.

The kernel C code can record which subflow was used last based on the previous value returned by the scheduler, or a BPF scheduler could track data in a BPF map (I think - I'm not a BPF expert).

For something like last_snd to work, I think we need some way to identify subflows that's not the raw ssk pointer. Are there rules about pointer usage in BPF that we need to consider?

--
Mat Martineau
Intel

#15 lore raw

Hi Mat, Geliang,

Quoted 8 lines · Mat Martineau
Quoted 3 lines · Geliang Tang

1. Get the subflow data from BPF, maybe iterate over the subflows from mptcp_sock.

Like we discussed in the meeting, I think a helper function that's callable from the BPF code could gather the necessary information from the subflows in a safe way.

Yes, similar to what is done in TCP CA, e.g. bpf_tcp_send_ack(), etc.

Quoted 8 lines
Quoted 4 lines

2. Make same members of struct mptcp_sock writable in BPF, say last_snd, then we can set it like this:

       msk->last_snd = ssk.

The kernel C code can record which subflow was used last based on the previous value returned by the scheduler, or a BPF scheduler could track data in a BPF map (I think - I'm not a BPF expert).

msk->last_snd seems quite specific to the current in-kernel packet scheduler, no?

It will be useful to store this last ssk for a RR scheduler but I think that's just a coincidence. I mean: it would probably be helpful to store everything needed for the current in-kernel packet scheduler in a dedicated structure in the msk. Then any packet scheduler can decide what to store in this dedicated memory and we can also easily clear stuff.

Quoted 3 lines

For something like last_snd to work, I think we need some way to identify subflows that's not the raw ssk pointer. Are there rules about pointer usage in BPF that we need to consider?

Yes, for BPF we might store info in a MAP linked to the msk. It would be interesting to see what is done in BPF Cubic code for example.

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net