Skip to contentSkip to conversation

bpf: Add mptcp_subflow bpf_iter support

PATCH Superseded Matthieu Baerts (NGI0) 2 participants 16 messages Dec 19, 2024 – Jan 30, 2025

Subsystems (7)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Matthieu Baerts (NGI0)

author maintainer
#0 lore raw

v2 cover bpf: Add mptcp_subflow bpf_iter support

Here is a series from Geliang, adding mptcp_subflow bpf_iter support.

We are working on extending MPTCP with BPF, e.g. to control the path manager -- in charge of the creation, deletion, and announcements of subflows (paths) -- and the packet scheduler -- in charge of selecting which available path the next data will be sent to. These extensions need to iterate over the list of subflows attached to an MPTCP connection, and do some specific actions via some new kfunc that will be added later on.

This preparation work is split in different patches:

- Patch 1: extend bpf_skc_to_mptcp_sock() to be called with msk.

- Patch 2: allow using skc_to_mptcp_sock() in CGroup sockopt hooks.

- Patch 3: register some "basic" MPTCP kfunc.

- Patch 4: add mptcp_subflow bpf_iter support. Note that previous versions of this single patch have already been shared to the BPF mailing list. The changelog has been kept with a comment, but the version number has been reset to avoid confusions.

- Patch 5: add kfunc to make sure the msk is valid

- Patch 6: add more MPTCP endpoints in the selftests, in order to create more than 2 subflows.

- Patch 7: add a very simple test validating mptcp_subflow bpf_iter support. This test could be written without the new bpf_iter, but it is there only to make sure this specific feature works as expected.

---
Changes in v2:
- Patches 1-2: new ones.
- Patch 3: remove two kfunc, more restrictions. (Martin)
- Patch 4: add BUILD_BUG_ON(), more restrictions. (Martin)
- Patch 7: adaptations due to modifications in patches 1-4.
- Link to v1: https://lore.kernel.org/r/20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org

---
Geliang Tang (7):
bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock
bpf: Allow use of skc_to_mptcp_sock in cg_sockopt
bpf: Register mptcp common kfunc set
bpf: Add mptcp_subflow bpf_iter
bpf: Acquire and release mptcp socket selftests/bpf: More endpoints for endpoint_init selftests/bpf: Add mptcp_subflow bpf_iter subtest

 include/net/mptcp.h                                |   4 +-
 kernel/bpf/cgroup.c                                |   2 +
 net/core/filter.c                                  |   2 +-
 net/mptcp/bpf.c                                    | 113 +++++++++++++++++-
 tools/testing/selftests/bpf/bpf_experimental.h     |   8 ++
 tools/testing/selftests/bpf/prog_tests/mptcp.c     | 129 ++++++++++++++++++++-
 tools/testing/selftests/bpf/progs/mptcp_bpf.h      |   9 ++
 .../testing/selftests/bpf/progs/mptcp_bpf_iters.c  |  63 ++++++++++
 8 files changed, 318 insertions(+), 12 deletions(-)

---
base-commit: dad704ebe38642cd405e15b9c51263356391355c
change-id: 20241108-bpf-next-net-mptcp-bpf_iter-subflows-027f6d87770e

Best regards,

Signature · 1 line
Matthieu Baerts (NGI0) <[email protected]>
author maintainer
#1 lore raw

PATCH v2 1/7 bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock

From: Geliang Tang <[email protected]>

Currently, bpf_skc_to_mptcp_sock() can only be used with sockets that are MPTCP subflows: TCP sockets with tp->is_mptcp, created by the kernel from an MPTCP socket (IPPROTO_MPTCP). Typically used with BPF sock_ops operators.

Here, this helper is extended to support MPTCP sockets, the ones created by the userspace (IPPROTO_MPTCP). This is useful for BPF hooks involving these sockets, e.g. [gs]etsocktopt.

bpf_skc_to_mptcp_sock() uses bpf_mptcp_sock_from_subflow(). The former suggests any MPTCP type/subtype can be used, but the latter only accepts subflow ones. So bpf_mptcp_sock_from_subflow is modified here to support MPTCP socket, and renamed to avoid confusions.

---
Notes:
- v2: new patch.

---
 include/net/mptcp.h |  4 ++--
 net/core/filter.c   |  2 +-
 net/mptcp/bpf.c     | 10 ++++++++--
 3 files changed, 11 insertions(+), 5 deletions(-)
include/net/mptcp.h +2 -2
index 814b5f2e3ed5e3e474a2bac5e4cca5a89abcfe1c..94d5976f7b8d8eb8b82f298f7b33ecd653937dc0 100644
@@ -322,9 +322,9 @@ static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
 #endif
 
 #if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
-struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
+struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk);
 #else
-static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
+static inline struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk) { return NULL; }
 #endif
 
 #if !IS_ENABLED(CONFIG_MPTCP)
net/core/filter.c +1 -1
index 6625b3f563a4a3110a76b9c12a46340828e16b6e..40ed3854a899bf9d11847ec0e8aee174923f03d2 100644
@@ -11833,7 +11833,7 @@ const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
 BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
 {
 	BTF_TYPE_EMIT(struct mptcp_sock);
-	return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
+	return (unsigned long)bpf_mptcp_sock_from_sock(sk);
 }
 
 const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
net/mptcp/bpf.c +8 -2
index 8a16672b94e2384f5263e1432296cbca1236bb30..988b22a06331ac293b36f3c6f9bc29ff0f6db048 100644
@@ -12,9 +12,15 @@
 #include <linux/bpf.h>
 #include "protocol.h"
 
-struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk)
+struct mptcp_sock *bpf_mptcp_sock_from_sock(struct sock *sk)
 {
-	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
+	if (unlikely(!sk || !sk_fullsock(sk)))
+		return NULL;
+
+	if (sk->sk_protocol == IPPROTO_MPTCP)
+		return mptcp_sk(sk);
+
+	if (sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
 		return mptcp_sk(mptcp_subflow_ctx(sk)->conn);
 
 	return NULL;
Signature · 1 line
2.47.1
author maintainer
#2 lore raw

PATCH v2 2/7 bpf: Allow use of skc_to_mptcp_sock in cg_sockopt

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

Currently, bpf_skc_to_mptcp_sock() helper is not allowed to be used in cg_sockopt. This patch adds this permission.

Thanks to the previous patch allowing skc_to_mptcp_sock() to be used with MPTCP sockets, this permission allows this helper to be use it in CGroup BPF hooks, e.g. [gs]etsocktopt.

---
Notes:
- v2: new patch.

---
 kernel/bpf/cgroup.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/bpf/cgroup.c +2 -0
index 46e5db65dbc8d8c6591b53dfc77bb689357f33ea..1ca22e4842cf6b6c6bdbc37bf415187e706b2b69 100644
@@ -2358,6 +2358,8 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 #ifdef CONFIG_INET
 	case BPF_FUNC_tcp_sock:
 		return &bpf_tcp_sock_proto;
+	case BPF_FUNC_skc_to_mptcp_sock:
+		return &bpf_skc_to_mptcp_sock_proto;
 #endif
 	case BPF_FUNC_perf_event_output:
 		return &bpf_event_output_data_proto;
Signature · 1 line
2.47.1
author maintainer
#3 lore raw

PATCH v2 3/7 bpf: Register mptcp common kfunc set

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

MPTCP helper mptcp_sk() is used to convert struct sock to mptcp_sock. Helpers mptcp_subflow_ctx() and mptcp_subflow_tcp_sock() are used to convert between struct mptcp_subflow_context and sock. They all will be used in MPTCP BPF programs too.

This patch defines corresponding wrappers of them, and put the wrappers into mptcp common kfunc set and register the set with the flag BPF_PROG_TYPE_UNSPEC to let them accessible to all types of BPF programs.

---
Notes:
- v2:
- Thanks to the two new previous patches, bpf_mptcp_sk() and bpf_mptcp_subflow_tcp_sock() are no longer needed.
- bpf_mptcp_subflow_ctx(): make sure the socket is an MPTCP subflow, and add KF_RET_NULL (Martin).
- Restrict this kfunc to BPF_PROG_TYPE_CGROUP_SOCKOPT for the moment.

---
 net/mptcp/bpf.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
net/mptcp/bpf.c +30 -1
index 988b22a06331ac293b36f3c6f9bc29ff0f6db048..c5bfd84c16c43230d9d8e1fd8ff781a767e647b5 100644
@@ -35,8 +35,37 @@ static const struct btf_kfunc_id_set bpf_mptcp_fmodret_set = {
 	.set   = &bpf_mptcp_fmodret_ids,
 };
 
+__bpf_kfunc_start_defs();
+
+__bpf_kfunc static struct mptcp_subflow_context *
+bpf_mptcp_subflow_ctx(const struct sock *sk)
+{
+	if (sk && sk_fullsock(sk) &&
+	    sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk))
+		return mptcp_subflow_ctx(sk);
+
+	return NULL;
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL)
+BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
+
+static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
+	.owner	= THIS_MODULE,
+	.set	= &bpf_mptcp_common_kfunc_ids,
+};
+
 static int __init bpf_mptcp_kfunc_init(void)
 {
-	return register_btf_fmodret_id_set(&bpf_mptcp_fmodret_set);
+	int ret;
+
+	ret = register_btf_fmodret_id_set(&bpf_mptcp_fmodret_set);
+	ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCKOPT,
+					       &bpf_mptcp_common_kfunc_set);
+
+	return ret;
 }
 late_initcall(bpf_mptcp_kfunc_init);
Signature · 1 line
2.47.1
author maintainer
#4 lore raw

PATCH v2 4/7 bpf: Add mptcp_subflow bpf_iter

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

It's necessary to traverse all subflows on the conn_list of an MPTCP socket and then call kfunc to modify the fields of each subflow. In kernel space, mptcp_for_each_subflow() helper is used for this:

	mptcp_for_each_subflow(msk, subflow)
		kfunc(subflow);

But in the MPTCP BPF program, this has not yet been implemented. As Martin suggested recently, this conn_list walking + modify-by-kfunc usage fits the bpf_iter use case.

So this patch adds a new bpf_iter type named "mptcp_subflow" to do this and implements its helpers bpf_iter_mptcp_subflow_new()/_next()/ _destroy(). And register these bpf_iter mptcp_subflow into mptcp common kfunc set. Then bpf_for_each() for mptcp_subflow can be used in BPF program like this:

	bpf_for_each(mptcp_subflow, subflow, msk)
		kfunc(subflow);

---
Notes:
- v2:
- Add BUILD_BUG_ON() checks, similar to the ones done with other

     bpf_iter_(...) helpers.
   - Replace msk_owned_by_me() by sock_owned_by_user_nocheck() and
     !spin_is_locked() (Martin).

A few versions of this single patch have been previously posted to the BPF mailing list by Geliang, before continuing to the MPTCP mailing list only, with other patches of this series. The version of the whole series has been reset to 1, but here is the ChangeLog for the previous ones:
- v2: remove msk->pm.lock in _new() and _destroy() (Martin) drop DEFINE_BPF_ITER_FUNC, change opaque[3] to opaque[2] (Andrii)
- v3: drop bpf_iter__mptcp_subflow
- v4: if msk is NULL, initialize kit->msk to NULL in _new() and check it in _next() (Andrii)
- v5: use list_is_last() instead of list_entry_is_head() add KF_ITER_NEW/NEXT/DESTROY flags add msk_owned_by_me in _new()
- v6: add KF_TRUSTED_ARGS flag (Andrii, Martin)

---
 net/mptcp/bpf.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
net/mptcp/bpf.c +53 -0
index c5bfd84c16c43230d9d8e1fd8ff781a767e647b5..e39f0e4fb683c1aa31ee075281daee218dac5878 100644
@@ -35,6 +35,15 @@ static const struct btf_kfunc_id_set bpf_mptcp_fmodret_set = {
 	.set   = &bpf_mptcp_fmodret_ids,
 };
 
+struct bpf_iter_mptcp_subflow {
+	__u64 __opaque[2];
+} __aligned(8);
+
+struct bpf_iter_mptcp_subflow_kern {
+	struct mptcp_sock *msk;
+	struct list_head *pos;
+} __aligned(8);
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc static struct mptcp_subflow_context *
@@ -47,10 +56,54 @@ bpf_mptcp_subflow_ctx(const struct sock *sk)
 	return NULL;
 }
 
+__bpf_kfunc static int
+bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
+			   struct mptcp_sock *msk)
+{
+	struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;
+	struct sock *sk = (struct sock *)msk;
+
+	BUILD_BUG_ON(sizeof(struct bpf_iter_mptcp_subflow_kern) >
+		     sizeof(struct bpf_iter_mptcp_subflow));
+	BUILD_BUG_ON(__alignof__(struct bpf_iter_mptcp_subflow_kern) !=
+		     __alignof__(struct bpf_iter_mptcp_subflow));
+
+	kit->msk = msk;
+	if (!msk)
+		return -EINVAL;
+
+	if (!sock_owned_by_user_nocheck(sk) &&
+	    !spin_is_locked(&sk->sk_lock.slock))
+		return -EINVAL;
+
+	kit->pos = &msk->conn_list;
+	return 0;
+}
+
+__bpf_kfunc static struct mptcp_subflow_context *
+bpf_iter_mptcp_subflow_next(struct bpf_iter_mptcp_subflow *it)
+{
+	struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;
+
+	if (!kit->msk || list_is_last(kit->pos, &kit->msk->conn_list))
+		return NULL;
+
+	kit->pos = kit->pos->next;
+	return list_entry(kit->pos, struct mptcp_subflow_context, node);
+}
+
+__bpf_kfunc static void
+bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it)
+{
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids)
 BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_next, KF_ITER_NEXT | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_destroy, KF_ITER_DESTROY)
 BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
 
 static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
Signature · 1 line
2.47.1
#5 lore raw

Re: [PATCH bpf-next/net v2 4/7] bpf: Add mptcp_subflow bpf_iter

Quoted 78 lines · On 12/19/24 7:46 AM, Matthieu Baerts (NGI0) wrote:

From: Geliang Tang <[email protected]>

It's necessary to traverse all subflows on the conn_list of an MPTCP socket and then call kfunc to modify the fields of each subflow. In kernel space, mptcp_for_each_subflow() helper is used for this:

	mptcp_for_each_subflow(msk, subflow)
		kfunc(subflow);

But in the MPTCP BPF program, this has not yet been implemented. As Martin suggested recently, this conn_list walking + modify-by-kfunc usage fits the bpf_iter use case.

So this patch adds a new bpf_iter type named "mptcp_subflow" to do this and implements its helpers bpf_iter_mptcp_subflow_new()/_next()/ _destroy(). And register these bpf_iter mptcp_subflow into mptcp common kfunc set. Then bpf_for_each() for mptcp_subflow can be used in BPF program like this:

	bpf_for_each(mptcp_subflow, subflow, msk)
		kfunc(subflow);

---
Notes:
- v2:
- Add BUILD_BUG_ON() checks, similar to the ones done with other bpf_iter_(...) helpers.
- Replace msk_owned_by_me() by sock_owned_by_user_nocheck() and !spin_is_locked() (Martin).

A few versions of this single patch have been previously posted to the BPF mailing list by Geliang, before continuing to the MPTCP mailing list only, with other patches of this series. The version of the whole series has been reset to 1, but here is the ChangeLog for the previous ones:
- v2: remove msk->pm.lock in _new() and _destroy() (Martin) drop DEFINE_BPF_ITER_FUNC, change opaque[3] to opaque[2] (Andrii)
- v3: drop bpf_iter__mptcp_subflow
- v4: if msk is NULL, initialize kit->msk to NULL in _new() and check it in _next() (Andrii)
- v5: use list_is_last() instead of list_entry_is_head() add KF_ITER_NEW/NEXT/DESTROY flags add msk_owned_by_me in _new()
- v6: add KF_TRUSTED_ARGS flag (Andrii, Martin)

---
  net/mptcp/bpf.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 53 insertions(+)
net/mptcp/bpf.c +13 -0
index c5bfd84c16c43230d9d8e1fd8ff781a767e647b5..e39f0e4fb683c1aa31ee075281daee218dac5878 100644
@@ -35,6 +35,15 @@ static const struct btf_kfunc_id_set bpf_mptcp_fmodret_set = {
  	.set   = &bpf_mptcp_fmodret_ids,
  };
 
+struct bpf_iter_mptcp_subflow {
+	__u64 __opaque[2];
+} __aligned(8);
+
+struct bpf_iter_mptcp_subflow_kern {
+	struct mptcp_sock *msk;
+	struct list_head *pos;
+} __aligned(8);
+
  __bpf_kfunc_start_defs();
 
  __bpf_kfunc static struct mptcp_subflow_context *
@@ -47,10 +56,54 @@ bpf_mptcp_subflow_ctx(const struct sock *sk)
  	return NULL;
  }
 
+__bpf_kfunc static int
+bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
+			   struct mptcp_sock *msk)
+{

net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 72

Quoted 10 lines
net/mptcp/bpf.c +10 -0
+	struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;
+	struct sock *sk = (struct sock *)msk;
+
+	BUILD_BUG_ON(sizeof(struct bpf_iter_mptcp_subflow_kern) >
+		     sizeof(struct bpf_iter_mptcp_subflow));
+	BUILD_BUG_ON(__alignof__(struct bpf_iter_mptcp_subflow_kern) !=
+		     __alignof__(struct bpf_iter_mptcp_subflow));
+
+	kit->msk = msk;
+	if (!msk)

NULL check is not needed. verifier should have rejected it for KF_TRUSTED_ARGS.

net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 76

Quoted 4 lines
net/mptcp/bpf.c +4 -0
+		return -EINVAL;
+
+	if (!sock_owned_by_user_nocheck(sk) &&
+	    !spin_is_locked(&sk->sk_lock.slock))

I could have missed something. If it is to catch bug, should it be sock_owned_by_me() that has the lockdep splat? For the cg get/setsockopt hook here, the lock should have already been held earlier in the kernel.

This set is only showing the cg sockopt bpf prog but missing the major struct_ops piece. It is hard to comment. I assumed the lock situation is the same for the struct_ops where the lock will be held before calling the struct_ops prog?

Quoted 34 lines
net/mptcp/bpf.c +26 -0
+		return -EINVAL;
+
+	kit->pos = &msk->conn_list;
+	return 0;
+}
+
+__bpf_kfunc static struct mptcp_subflow_context *
+bpf_iter_mptcp_subflow_next(struct bpf_iter_mptcp_subflow *it)
+{
+	struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;
+
+	if (!kit->msk || list_is_last(kit->pos, &kit->msk->conn_list))
+		return NULL;
+
+	kit->pos = kit->pos->next;
+	return list_entry(kit->pos, struct mptcp_subflow_context, node);
+}
+
+__bpf_kfunc static void
+bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it)
+{
+}
+
  __bpf_kfunc_end_defs();
 
  BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids)
  BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_next, KF_ITER_NEXT | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_destroy, KF_ITER_DESTROY)
  BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
 
  static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
author maintainer
#6 lore raw

Re: [PATCH bpf-next/net v2 4/7] bpf: Add mptcp_subflow bpf_iter

Hi Geliang,

(only with the MPTCP list)

Quoted 93 lines · Martin KaFai Lau
Quoted 78 lines · On 12/19/24 7:46 AM, Matthieu Baerts (NGI0) wrote:

From: Geliang Tang <[email protected]>

It's necessary to traverse all subflows on the conn_list of an MPTCP socket and then call kfunc to modify the fields of each subflow. In kernel space, mptcp_for_each_subflow() helper is used for this:

    mptcp_for_each_subflow(msk, subflow)
        kfunc(subflow);

But in the MPTCP BPF program, this has not yet been implemented. As Martin suggested recently, this conn_list walking + modify-by-kfunc usage fits the bpf_iter use case.

So this patch adds a new bpf_iter type named "mptcp_subflow" to do this and implements its helpers bpf_iter_mptcp_subflow_new()/_next()/ _destroy(). And register these bpf_iter mptcp_subflow into mptcp common kfunc set. Then bpf_for_each() for mptcp_subflow can be used in BPF program like this:

    bpf_for_each(mptcp_subflow, subflow, msk)
        kfunc(subflow);

---
Notes:
  - v2:
    - Add BUILD_BUG_ON() checks, similar to the ones done with other bpf_iter_(...) helpers.
    - Replace msk_owned_by_me() by sock_owned_by_user_nocheck() and !spin_is_locked() (Martin).

A few versions of this single patch have been previously posted to the BPF mailing list by Geliang, before continuing to the MPTCP mailing list only, with other patches of this series. The version of the whole series has been reset to 1, but here is the ChangeLog for the previous ones:
  - v2: remove msk->pm.lock in _new() and _destroy() (Martin) drop DEFINE_BPF_ITER_FUNC, change opaque[3] to opaque[2] (Andrii)
  - v3: drop bpf_iter__mptcp_subflow
  - v4: if msk is NULL, initialize kit->msk to NULL in _new() and check it in _next() (Andrii)
  - v5: use list_is_last() instead of list_entry_is_head() add KF_ITER_NEW/NEXT/DESTROY flags add msk_owned_by_me in _new()
  - v6: add KF_TRUSTED_ARGS flag (Andrii, Martin)
---
  net/mptcp/bpf.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++
++++++

  1 file changed, 53 insertions(+)
net/mptcp/bpf.c +0 -0

index c5bfd84c16c43230d9d8e1fd8ff781a767e647b5..e39f0e4fb683c1aa31ee075281daee218dac5878 100644

net/mptcp/bpf.c +8 -0
@@ -35,6 +35,15 @@ static const struct btf_kfunc_id_set
 bpf_mptcp_fmodret_set = {
       .set   = &bpf_mptcp_fmodret_ids,
   };
   +struct bpf_iter_mptcp_subflow {
+    __u64 __opaque[2];
+} __aligned(8);
+
+struct bpf_iter_mptcp_subflow_kern {
+    struct mptcp_sock *msk;
+    struct list_head *pos;
+} __aligned(8);
+
   __bpf_kfunc_start_defs();

    __bpf_kfunc static struct mptcp_subflow_context *

net/mptcp/bpf.c +0 -0
@@ -47,10 +56,54 @@ bpf_mptcp_subflow_ctx(const struct sock *sk)
       return NULL;
   }

  +__bpf_kfunc static int

net/mptcp/bpf.c +3 -0
+bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
+               struct mptcp_sock *msk)
+{

net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 72

Quoted 10 lines
net/mptcp/bpf.c +10 -0
+    struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;
+    struct sock *sk = (struct sock *)msk;
+
+    BUILD_BUG_ON(sizeof(struct bpf_iter_mptcp_subflow_kern) >
+             sizeof(struct bpf_iter_mptcp_subflow));
+    BUILD_BUG_ON(__alignof__(struct bpf_iter_mptcp_subflow_kern) !=
+             __alignof__(struct bpf_iter_mptcp_subflow));
+
+    kit->msk = msk;
+    if (!msk)

NULL check is not needed. verifier should have rejected it for KF_TRUSTED_ARGS.

Quoted 8 lines
Quoted 4 lines

+        return -EINVAL;
+
+    if (!sock_owned_by_user_nocheck(sk) &&
+        !spin_is_locked(&sk->sk_lock.slock))

I could have missed something. If it is to catch bug, should it be sock_owned_by_me() that has the lockdep splat? For the cg get/setsockopt hook here, the lock should have already been held earlier in the kernel.

:)

So from what I understand: either we need these checks because this helper could technically be called when these conditions are not met. Or we don't need them, and we can have sock_owned_by_me() (without the return that was in the v1) for lockdep.

Quoted 4 lines

This set is only showing the cg sockopt bpf prog but missing the major struct_ops piece. It is hard to comment. I assumed the lock situation is the same for the struct_ops where the lock will be held before calling the struct_ops prog?

Can we restrict the use of this helper only for some specific callbacks? Or for only some specific struct_ops?

Last time I looked, I thought that you could only restrict it like that:
"this helper can be used by (any) struct_ops". But then does it mean this helper could be called from a TCP BPF CC ops or another unrelated one for example? How can we avoid that?

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author maintainer
#7 lore raw

Re: [PATCH bpf-next/net v2 4/7] bpf: Add mptcp_subflow bpf_iter

In reply to Martin KaFai Lau

Hi Martin,

Thank you for your review!

Sorry for the delay here, Geliang started to work on a new version, but it might take a bit of time as he is currently off for a few days.

Quoted 22 lines · Martin KaFai Lau
Quoted 21 lines · On 12/19/24 7:46 AM, Matthieu Baerts (NGI0) wrote:

From: Geliang Tang <[email protected]>

It's necessary to traverse all subflows on the conn_list of an MPTCP socket and then call kfunc to modify the fields of each subflow. In kernel space, mptcp_for_each_subflow() helper is used for this:

    mptcp_for_each_subflow(msk, subflow)
        kfunc(subflow);

But in the MPTCP BPF program, this has not yet been implemented. As Martin suggested recently, this conn_list walking + modify-by-kfunc usage fits the bpf_iter use case.

So this patch adds a new bpf_iter type named "mptcp_subflow" to do this and implements its helpers bpf_iter_mptcp_subflow_new()/_next()/ _destroy(). And register these bpf_iter mptcp_subflow into mptcp common kfunc set. Then bpf_for_each() for mptcp_subflow can be used in BPF program like this:

    bpf_for_each(mptcp_subflow, subflow, msk)
        kfunc(subflow);

(...)

Quoted 5 lines
Quoted 5 lines
net/mptcp/bpf.c +0 -0

index c5bfd84c16c43230d9d8e1fd8ff781a767e647b5..e39f0e4fb683c1aa31ee075281daee218dac5878 100644

net/mptcp/bpf.c +0 -0

(...)

Quoted 21 lines
Quoted 7 lines
net/mptcp/bpf.c +0 -0
@@ -47,10 +56,54 @@ bpf_mptcp_subflow_ctx(const struct sock *sk)
       return NULL;
   }

  +__bpf_kfunc static int

net/mptcp/bpf.c +3 -0
+bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
+               struct mptcp_sock *msk)
+{

net/mptcp/bpf.c bpf_mptcp_subflow_ctx(const struct sock *sk) · line 72

Quoted 10 lines
net/mptcp/bpf.c +10 -0
+    struct bpf_iter_mptcp_subflow_kern *kit = (void *)it;
+    struct sock *sk = (struct sock *)msk;
+
+    BUILD_BUG_ON(sizeof(struct bpf_iter_mptcp_subflow_kern) >
+             sizeof(struct bpf_iter_mptcp_subflow));
+    BUILD_BUG_ON(__alignof__(struct bpf_iter_mptcp_subflow_kern) !=
+             __alignof__(struct bpf_iter_mptcp_subflow));
+
+    kit->msk = msk;
+    if (!msk)

NULL check is not needed. verifier should have rejected it for KF_TRUSTED_ARGS.

Quoted 8 lines
Quoted 4 lines

+        return -EINVAL;
+
+    if (!sock_owned_by_user_nocheck(sk) &&
+        !spin_is_locked(&sk->sk_lock.slock))

I could have missed something. If it is to catch bug, should it be sock_owned_by_me() that has the lockdep splat? For the cg get/setsockopt hook here, the lock should have already been held earlier in the kernel.

Good point. Because in this series, the kfunc is currently restricted to CG [gs]etsockopt hooks, we should use msk_owned_by_me(msk) here.

Quoted 4 lines

This set is only showing the cg sockopt bpf prog but missing the major struct_ops piece. It is hard to comment. I assumed the lock situation is the same for the struct_ops where the lock will be held before calling the struct_ops prog?

I understand it is hard to comment on that point. In the 'struct_ops' we are designing, the lock will indeed be held before calling the stuct_ops program. So we will just need to make sure this assumption is correct for all callbacks of our struct_ops.
Also, if I understood correctly, it is possible to restrict a kfunc to some specific struct_ops, e.g. not to call this kfunc for the TCP CA struct_ops. So these checks should indeed not be needed, but I will double-check that with Geliang.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author maintainer
#8 lore raw

PATCH v2 5/7 bpf: Acquire and release mptcp socket

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

The KF_TRUSTED_ARGS flag is used for bpf_iter_mptcp_subflow_new, it indicates that the all pointer arguments are valid. It's necessary to add a KF_ACQUIRE helper to get valid "msk".

This patch adds bpf_mptcp_sock_acquire() and bpf_mptcp_sock_release() helpers for this. Increase sk->sk_refcnt in _acquire() and decrease it in _release(). Register them with KF_ACQUIRE flag and KF_RELEASE flag.

---
 net/mptcp/bpf.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/mptcp/bpf.c +19 -0
index e39f0e4fb683c1aa31ee075281daee218dac5878..d50bd1ea7f6d0ff1abff32deef9a98b98ee8f42c 100644
@@ -97,6 +97,23 @@ bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it)
 {
 }
 
+__bpf_kfunc static struct
+mptcp_sock *bpf_mptcp_sock_acquire(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+
+	if (sk && refcount_inc_not_zero(&sk->sk_refcnt))
+		return msk;
+	return NULL;
+}
+
+__bpf_kfunc static void bpf_mptcp_sock_release(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+
+	WARN_ON_ONCE(!sk || !refcount_dec_not_one(&sk->sk_refcnt));
+}
+
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids)
@@ -104,6 +121,8 @@ BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_mptcp_sock_acquire, KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_mptcp_sock_release, KF_RELEASE)
 BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
 
 static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
Signature · 1 line
2.47.1
#9 lore raw

Re: [PATCH bpf-next/net v2 5/7] bpf: Acquire and release mptcp socket

Quoted 5 lines · On 12/19/24 7:46 AM, Matthieu Baerts (NGI0) wrote:

From: Geliang Tang <[email protected]>

The KF_TRUSTED_ARGS flag is used for bpf_iter_mptcp_subflow_new, it indicates that the all pointer arguments are valid. It's necessary to add a KF_ACQUIRE helper to get valid "msk".

This feels wrong. It forces an unnecessary acquire to get around the verifier. bpf_sockopt->sk should be in "trusted". From looking at patch 7, the issue should be the return value of bpf_skc_to_mptcp_sock().

Quoted 41 lines

This patch adds bpf_mptcp_sock_acquire() and bpf_mptcp_sock_release() helpers for this. Increase sk->sk_refcnt in _acquire() and decrease it in _release(). Register them with KF_ACQUIRE flag and KF_RELEASE flag.

---
  net/mptcp/bpf.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
net/mptcp/bpf.c +17 -0
index e39f0e4fb683c1aa31ee075281daee218dac5878..d50bd1ea7f6d0ff1abff32deef9a98b98ee8f42c 100644
@@ -97,6 +97,23 @@ bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it)
  {
  }
 
+__bpf_kfunc static struct
+mptcp_sock *bpf_mptcp_sock_acquire(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+
+	if (sk && refcount_inc_not_zero(&sk->sk_refcnt))
+		return msk;
+	return NULL;
+}
+
+__bpf_kfunc static void bpf_mptcp_sock_release(struct mptcp_sock *msk)
+{
+	struct sock *sk = (struct sock *)msk;
+
+	WARN_ON_ONCE(!sk || !refcount_dec_not_one(&sk->sk_refcnt));
+}
+
  __bpf_kfunc_end_defs();
 
  BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids)

net/mptcp/bpf.c BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL) · line 124

Quoted 4 lines
net/mptcp/bpf.c +1 -0
@@ -104,6 +121,8 @@ BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx, KF_RET_NULL)
  BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_new, KF_ITER_NEW | KF_TRUSTED_ARGS)
  BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_next, KF_ITER_NEXT | KF_RET_NULL)
  BTF_ID_FLAGS(func, bpf_iter_mptcp_subflow_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_mptcp_sock_acquire, KF_ACQUIRE | KF_RET_NULL)

It should need a KF_TRUSTED_ARGS here but then it will hit the same problem described in the commit message.

Instead of changing the verifier to get this work, one option is to use the "struct sock *sk" instead of "struct mptcp-sock *msk" as the argument in the bpf_iter_mptcp_subflow_new, and do the bpf_mptcp_sock_from_sock check in the bpf_iter_mptcp_subflow_new.

Quoted 5 lines
net/mptcp/bpf.c +1 -0
+BTF_ID_FLAGS(func, bpf_mptcp_sock_release, KF_RELEASE)
  BTF_KFUNCS_END(bpf_mptcp_common_kfunc_ids)
 
  static const struct btf_kfunc_id_set bpf_mptcp_common_kfunc_set = {
author maintainer
#10 lore raw

PATCH v2 6/7 selftests/bpf: More endpoints for endpoint_init

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

This patch changes ADDR_2 from "10.0.1.2" to "10.0.2.1", and adds two more IPv4 test addresses ADDR_3 - ADDR_4, four IPv6 addresses ADDR6_1 - ADDR6_4. Add a new helper address_init() to initialize all these addresses.

Add a new parameter "endpoints" for endpoint_init() to control how many endpoints are used for the tests. This makes it more flexible. Update the parameters of endpoint_init() in test_subflow().

---
 tools/testing/selftests/bpf/prog_tests/mptcp.c | 56 +++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/prog_tests/mptcp.c +50 -6
index f8eb7f9d4fd20bbb7ee018728f7ae0f0a09d4d30..85f3d4119802a85c86cde7b74a0b857252bad8b8 100644
@@ -14,7 +14,13 @@
 
 #define NS_TEST "mptcp_ns"
 #define ADDR_1	"10.0.1.1"
-#define ADDR_2	"10.0.1.2"
+#define ADDR_2	"10.0.2.1"
+#define ADDR_3	"10.0.3.1"
+#define ADDR_4	"10.0.4.1"
+#define ADDR6_1	"dead:beef:1::1"
+#define ADDR6_2	"dead:beef:2::1"
+#define ADDR6_3	"dead:beef:3::1"
+#define ADDR6_4	"dead:beef:4::1"
 #define PORT_1	10001
 
 #ifndef IPPROTO_MPTCP
@@ -322,22 +328,60 @@ static void test_mptcpify(void)
 	close(cgroup_fd);
 }
 
-static int endpoint_init(char *flags)
+static int address_init(void)
 {
 	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 addr add %s/64 dev veth1 nodad", NS_TEST, ADDR6_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 addr add %s/64 dev veth2 nodad", NS_TEST, ADDR6_2);
 	SYS(fail, "ip -net %s link set dev veth2 up", NS_TEST);
-	if (SYS_NOFAIL("ip -net %s mptcp endpoint add %s %s", NS_TEST, ADDR_2, flags)) {
+
+	SYS(fail, "ip -net %s link add veth3 type veth peer name veth4", NS_TEST);
+	SYS(fail, "ip -net %s addr add %s/24 dev veth3", NS_TEST, ADDR_3);
+	SYS(fail, "ip -net %s addr add %s/64 dev veth3 nodad", NS_TEST, ADDR6_3);
+	SYS(fail, "ip -net %s link set dev veth3 up", NS_TEST);
+	SYS(fail, "ip -net %s addr add %s/24 dev veth4", NS_TEST, ADDR_4);
+	SYS(fail, "ip -net %s addr add %s/64 dev veth4 nodad", NS_TEST, ADDR6_4);
+	SYS(fail, "ip -net %s link set dev veth4 up", NS_TEST);
+
+	return 0;
+fail:
+	return -1;
+}
+
+static int endpoint_add(char *addr, char *flags)
+{
+	return SYS_NOFAIL("ip -net %s mptcp endpoint add %s %s", NS_TEST, addr, flags);
+}
+
+static int endpoint_init(char *flags, u8 endpoints)
+{
+	int ret = -1;
+
+	if (!endpoints || endpoints > 4)
+		goto fail;
+
+	if (address_init())
+		goto fail;
+
+	if (SYS_NOFAIL("ip -net %s mptcp limits set add_addr_accepted 4 subflows 4",
+		       NS_TEST)) {
 		printf("'ip mptcp' not supported, skip this test.\n");
 		test__skip();
 		goto fail;
 	}
 
-	return 0;
+	if (endpoints > 1)
+		ret = endpoint_add(ADDR_2, flags);
+	if (endpoints > 2)
+		ret = ret ?: endpoint_add(ADDR_3, flags);
+	if (endpoints > 3)
+		ret = ret ?: endpoint_add(ADDR_4, flags);
+
 fail:
-	return -1;
+	return ret;
 }
 
 static void wait_for_new_subflows(int fd)
@@ -423,7 +467,7 @@ static void test_subflow(void)
 	if (!ASSERT_OK_PTR(netns, "netns_new: mptcp_subflow"))
 		goto skel_destroy;
 
-	if (endpoint_init("subflow") < 0)
+	if (endpoint_init("subflow", 2) < 0)
 		goto close_netns;
 
 	run_subflow();
Signature · 1 line
2.47.1
author maintainer
#11 lore raw

PATCH v2 7/7 selftests/bpf: Add mptcp_subflow bpf_iter subtest

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

This patch adds a "cgroup/getsockopt" program "iters_subflow" to test the newly added mptcp_subflow bpf_iter.

Export mptcp_subflow helpers bpf_iter_mptcp_subflow_new/_next/_destroy, bpf_mptcp_sock_acquire/_release and other helpers into bpf_experimental.h.

Use bpf_mptcp_sock_acquire() to acquire the msk, then use bpf_for_each() to walk the subflow list of this msk. From there, future MPTCP-specific kfunc can be called in the loop. Because they are not there yet, this test doesn't do anything very useful for the moment, but it focuses on validating the 'bpf_iter' part and the basic MPTCP kfunc. That's why it simply adds all subflow ids to local variable local_ids to make sure all subflows have been seen, then invoke mptcp_subflow_tcp_sock() in the loop to pick the subflow context.

Out of the loop, use bpf_mptcp_subflow_ctx() to get the subflow context of the picked subflow context and do some verifications. Finally, assign local_ids to global variable ids so that the application can obtain this value, and release the msk.

A related subtest called test_iters_subflow is added to load and verify the newly added mptcp_subflow type bpf_iter example in test_mptcp. The endpoint_init() helper is used to add 3 new subflow endpoints. Then one byte of message is sent to trigger the creation of new subflows. getsockopt() is invoked once the subflows have been created to trigger the "cgroup/getsockopt" test program "iters_subflow". skel->bss->ids is then checked to make sure it equals 10, the sum of each subflow ID: we should have 4 subflows: 1 + 2 + 3 + 4 = 10. If that's the case, the bpf_iter loop did the job as expected.

---
Notes:
- v2:
- explicit sk protocol checks are no longer needed, implicitly done

     in bpf_skc_to_mptcp_sock().
   - use bpf_skc_to_mptcp_sock() instead of bpf_mptcp_sk(), and
     mptcp_subflow_tcp_sock() instead of bpf_mptcp_subflow_tcp_sock().
   - bpf_mptcp_subflow_ctx() can now return NULL.
---
 tools/testing/selftests/bpf/bpf_experimental.h     |  8 +++
 tools/testing/selftests/bpf/prog_tests/mptcp.c     | 73 ++++++++++++++++++++++
 tools/testing/selftests/bpf/progs/mptcp_bpf.h      |  9 +++
 .../testing/selftests/bpf/progs/mptcp_bpf_iters.c  | 63 +++++++++++++++++++
 4 files changed, 153 insertions(+)
tools/testing/selftests/bpf/bpf_experimental.h +8 -0
index cd8ecd39c3f3c68d40c6e3e1465b42ed66537027..2ab3f0063c0fd6091ee19da4787671b89b5661f0 100644
@@ -575,6 +575,14 @@ extern int bpf_iter_css_new(struct bpf_iter_css *it,
 extern struct cgroup_subsys_state *bpf_iter_css_next(struct bpf_iter_css *it) __weak __ksym;
 extern void bpf_iter_css_destroy(struct bpf_iter_css *it) __weak __ksym;
 
+struct bpf_iter_mptcp_subflow;
+extern int bpf_iter_mptcp_subflow_new(struct bpf_iter_mptcp_subflow *it,
+				      struct mptcp_sock *msk) __weak __ksym;
+extern struct mptcp_subflow_context *
+bpf_iter_mptcp_subflow_next(struct bpf_iter_mptcp_subflow *it) __weak __ksym;
+extern void
+bpf_iter_mptcp_subflow_destroy(struct bpf_iter_mptcp_subflow *it) __weak __ksym;
+
 extern int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) __weak __ksym;
 extern int bpf_wq_start(struct bpf_wq *wq, unsigned int flags) __weak __ksym;
 extern int bpf_wq_set_callback_impl(struct bpf_wq *wq,
tools/testing/selftests/bpf/prog_tests/mptcp.c +73 -0

95 lines not on this page · raw message

tools/testing/selftests/bpf/progs/mptcp_bpf.h +9 -0
index 3b188ccdcc4041acb4f7ed38ae8ddf5a7305466a..31d7ecdc4c0489c63d28a25778f91093a6a093a5 100644
@@ -39,4 +39,13 @@ mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow)
 	return subflow->tcp_sock;
 }
 
+/* ksym */
+extern struct mptcp_sock *bpf_mptcp_sock_acquire(struct mptcp_sock *msk) __ksym;
+extern void bpf_mptcp_sock_release(struct mptcp_sock *msk) __ksym;
+
+extern struct mptcp_subflow_context *
+bpf_mptcp_subflow_ctx(const struct sock *sk) __ksym;
+extern struct sock *
+bpf_mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow) __ksym;
+
 #endif
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.47.1
#12 lore raw

Re: [PATCH bpf-next/net v2 7/7] selftests/bpf: Add mptcp_subflow bpf_iter subtest

Quoted 19 lines · On 12/19/24 7:46 AM, Matthieu Baerts (NGI0) wrote:
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c new file +19 -0
+SEC("cgroup/getsockopt")
+int iters_subflow(struct bpf_sockopt *ctx)
+{
+	struct mptcp_subflow_context *subflow;
+	struct bpf_sock *sk = ctx->sk;
+	struct sock *ssk = NULL;
+	struct mptcp_sock *msk;
+	int local_ids = 0;
+
+	if (ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
+		return 1;
+
+	msk = bpf_skc_to_mptcp_sock(sk);
+	if (!msk || msk->pm.server_side || !msk->pm.subflows)
+		return 1;
+
+	msk = bpf_mptcp_sock_acquire(msk);
+	if (!msk)
+		return 1;

tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c line 43

Quoted 10 lines
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c new file +10 -0
+	bpf_for_each(mptcp_subflow, subflow, msk) {
+		/* Here MPTCP-specific packet scheduler kfunc can be called:
+		 * this test is not doing anything really useful, only to
+		 * verify the iteration works.
+		 */
+
+		local_ids += subflow->subflow_id;
+
+		/* only to check the following kfunc works */
+		ssk = mptcp_subflow_tcp_sock(subflow);

It is good to have test cases to exercise the new iter and kfunc. Thanks.

However, it seems not useful to show how it will be used in the future packet/subflow scheduler. iiuc, the core piece is in bpf_struct_ops. Without it, it is hard to comment. Any RFC patches ready to be posted?

Quoted 20 lines
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c new file +20 -0
+	}
+
+	if (!ssk)
+		goto out;
+
+	/* assert: if not OK, something wrong on the kernel side */
+	if (ssk->sk_dport != ((struct sock *)msk)->sk_dport)
+		goto out;
+
+	/* only to check the following kfunc works */
+	subflow = bpf_mptcp_subflow_ctx(ssk);
+	if (!subflow || subflow->token != msk->token)
+		goto out;
+
+	ids = local_ids;
+
+out:
+	bpf_mptcp_sock_release(msk);
+	return 1;
+}
author maintainer
#13 lore raw

Re: [PATCH bpf-next/net v2 7/7] selftests/bpf: Add mptcp_subflow bpf_iter subtest

Hi Geliang,

(only with the MPTCP list)

Quoted 33 lines · Martin KaFai Lau
Quoted 19 lines · On 12/19/24 7:46 AM, Matthieu Baerts (NGI0) wrote:
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c new file +19 -0
+SEC("cgroup/getsockopt")
+int iters_subflow(struct bpf_sockopt *ctx)
+{
+    struct mptcp_subflow_context *subflow;
+    struct bpf_sock *sk = ctx->sk;
+    struct sock *ssk = NULL;
+    struct mptcp_sock *msk;
+    int local_ids = 0;
+
+    if (ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP)
+        return 1;
+
+    msk = bpf_skc_to_mptcp_sock(sk);
+    if (!msk || msk->pm.server_side || !msk->pm.subflows)
+        return 1;
+
+    msk = bpf_mptcp_sock_acquire(msk);
+    if (!msk)
+        return 1;

tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c line 43

Quoted 10 lines
tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c new file +10 -0
+    bpf_for_each(mptcp_subflow, subflow, msk) {
+        /* Here MPTCP-specific packet scheduler kfunc can be called:
+         * this test is not doing anything really useful, only to
+         * verify the iteration works.
+         */
+
+        local_ids += subflow->subflow_id;
+
+        /* only to check the following kfunc works */
+        ssk = mptcp_subflow_tcp_sock(subflow);

It is good to have test cases to exercise the new iter and kfunc. Thanks.

Quoted 3 lines

However, it seems not useful to show how it will be used in the future packet/subflow scheduler. iiuc, the core piece is in bpf_struct_ops. Without it, it is hard to comment. Any RFC patches ready to be posted?

If it is easier, we can probably share a link to your git repo containing the WIP patches.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
author maintainer
#14 lore raw
In reply to Matthieu Baerts (NGI0)

Hello BPF maintainers and reviewers,

Quoted 9 lines · On 19/12/2024 16:46, Matthieu Baerts (NGI0) wrote:

Here is a series from Geliang, adding mptcp_subflow bpf_iter support.

We are working on extending MPTCP with BPF, e.g. to control the path manager -- in charge of the creation, deletion, and announcements of subflows (paths) -- and the packet scheduler -- in charge of selecting which available path the next data will be sent to. These extensions need to iterate over the list of subflows attached to an MPTCP connection, and do some specific actions via some new kfunc that will be added later on.

(...)

Quoted 6 lines

Changes in v2:
- Patches 1-2: new ones.
- Patch 3: remove two kfunc, more restrictions. (Martin)
- Patch 4: add BUILD_BUG_ON(), more restrictions. (Martin)
- Patch 7: adaptations due to modifications in patches 1-4.
- Link to v1: https://lore.kernel.org/r/20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org

The v2 of this series didn't get any reviews, probably because it has been sent the week before Xmas. Do you prefer if I resend it?

There is no hurry, I can also re-send it later if "now" is not a good time.

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#15 lore raw
Quoted 19 lines · Matthieu Baerts
Quoted 9 lines

Here is a series from Geliang, adding mptcp_subflow bpf_iter support.

We are working on extending MPTCP with BPF, e.g. to control the path manager -- in charge of the creation, deletion, and announcements of subflows (paths) -- and the packet scheduler -- in charge of selecting which available path the next data will be sent to. These extensions need to iterate over the list of subflows attached to an MPTCP connection, and do some specific actions via some new kfunc that will be added later on.

(...)

Quoted 6 lines

Changes in v2:
- Patches 1-2: new ones.
- Patch 3: remove two kfunc, more restrictions. (Martin)
- Patch 4: add BUILD_BUG_ON(), more restrictions. (Martin)
- Patch 7: adaptations due to modifications in patches 1-4.
- Link to v1: https://lore.kernel.org/r/20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org

Quoted 2 lines

The v2 of this series didn't get any reviews, probably because it has been sent the week before Xmas. Do you prefer if I resend it?

No need to resend. will get to it.