implement psock_update_sk_prot
Subsystems (5)
Geliang Tang
reviewerv3 cover implement psock_update_sk_prot
From: Geliang Tang <[email protected]>
v3:
- Address the comments by ai review.
- use WRITE_ONCE to set sk->sk_write_space.
- update the selftest, set a new key for client socket.
- Fix line length warnings.
RESEND:
- rebased.
- to trigger ai review.
v2:
- Include mptcp_bpf_update_proto within CONFIG_BPF_SYSCALL to fix the
compilation errors reported by the kernel test robot.
- Add checks for IS_ENABLED(CONFIG_MPTCP_IPV6).
Implement psock_update_sk_prot, for basic MPTCP BPF SOCKMAP support.
Geliang Tang (2):
mptcp: implement psock_update_sk_prot
selftests/bpf: Update sockmap tests for MPTCP
net/mptcp/protocol.c | 113 ++++++++++++++++++ .../testing/selftests/bpf/prog_tests/mptcp.c | 16 +-- 2 files changed, 121 insertions(+), 8 deletions(-)
Signature · 1 line
2.51.0
Geliang Tang
reviewerPATCH v3 1/2 mptcp: implement psock_update_sk_prot
From: Geliang Tang <[email protected]>
Add MPTCP support for BPF sockmap by implementing psock_update_sk_prot callback. This allows MPTCP sockets to dynamically switch protocol handlers when attached to or detached from sockmap programs. Separate protocol structures are maintained for IPv4/IPv6 and TX/RX configurations.
tcp_bpf_update_proto() in net/ipv4/tcp_bpf.c is a frame of reference for this patch.
--- net/mptcp/protocol.c | 113 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+)
net/mptcp/protocol.c +113 -0
@@ -11,6 +11,7 @@#include <linux/netdevice.h> #include <linux/sched/signal.h> #include <linux/atomic.h> +#include <linux/skmsg.h> #include <net/aligned_data.h> #include <net/rps.h> #include <net/sock.h>
@@ -4020,6 +4021,106 @@ static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,return 0 ; } +#ifdef CONFIG_BPF_SYSCALL +enum { +MPTCP_BPF_IPV4 , +MPTCP_BPF_IPV6 , +MPTCP_BPF_NUM_PROTS , +}; + +enum { +MPTCP_BPF_BASE , +MPTCP_BPF_TX , +MPTCP_BPF_RX , +MPTCP_BPF_TXRX , +MPTCP_BPF_NUM_CFGS , +}; + +static struct proto mptcp_bpf_prots[MPTCP_BPF_NUM_PROTS ][MPTCP_BPF_NUM_CFGS ]; + +static void mptcp_bpf_rebuild_protos (struct proto prot[MPTCP_BPF_NUM_CFGS ], +struct proto *base) +{ + prot[MPTCP_BPF_BASE ] = *base; + prot[MPTCP_BPF_BASE ].destroy = sock_map_destroy; + prot[MPTCP_BPF_BASE ].close = sock_map_close; + prot[MPTCP_BPF_BASE ].sock_is_readable = sk_msg_is_readable; + + prot[MPTCP_BPF_TX ] = prot[MPTCP_BPF_BASE ]; + prot[MPTCP_BPF_RX ] = prot[MPTCP_BPF_BASE ]; + prot[MPTCP_BPF_TXRX ] = prot[MPTCP_BPF_TX ]; +} + +#if IS_ENABLED (CONFIG_MPTCP_IPV6 ) +static struct proto *mptcpv6_prot_saved__read_mostly ; +static DEFINE_SPINLOCK (mptcpv6_prot_lock); + +static void mptcp_bpf_check_v6_needs_rebuild (struct proto *ops) +{ +/* +* Load with acquire semantics to ensure we see the latest protocol +* structure before checking for rebuild. +*/ +if (unlikely (ops !=smp_load_acquire (&mptcpv6_prot_saved))) { +spin_lock_bh (&mptcpv6_prot_lock); +if (likely (ops != mptcpv6_prot_saved)) { +mptcp_bpf_rebuild_protos (mptcp_bpf_prots[MPTCP_BPF_IPV6 ], + ops); +/* Ensure mptcpv6_prot_saved update is visible before +* releasing lock +*/ +smp_store_release (&mptcpv6_prot_saved, ops); + } +spin_unlock_bh (&mptcpv6_prot_lock); + } +} + +static int mptcp_bpf_assert_proto_ops (struct proto *ops) +{ +/* In order to avoid retpoline, we make assumptions when we call +* into ops if e.g. a psock is not present. Make sure they are +* indeed valid assumptions. +*/ +return ops->recvmsg == mptcp_recvmsg && + ops->sendmsg == mptcp_sendmsg ?0 : -EOPNOTSUPP ; +} +#endif + +static int mptcp_bpf_update_proto (struct sock *sk, +struct sk_psock *psock, +bool restore) +{ +int family = sk->sk_family ==AF_INET6 ?MPTCP_BPF_IPV6 : +MPTCP_BPF_IPV4 ; +int config = psock->progs.msg_parser ?MPTCP_BPF_TX : +MPTCP_BPF_BASE ; + +if (psock->progs.stream_verdict || psock->progs.skb_verdict) + config = (config ==MPTCP_BPF_TX ) ?MPTCP_BPF_TXRX : +MPTCP_BPF_RX ; + +if (restore) { +WRITE_ONCE (sk->sk_write_space, psock->saved_write_space); +/* Pairs with lockless read in sk_clone_lock() */ +sock_replace_proto (sk, psock->sk_proto); +return 0 ; + } + +#if IS_ENABLED (CONFIG_MPTCP_IPV6 ) +if (sk->sk_family ==AF_INET6 ) { +if (mptcp_bpf_assert_proto_ops (psock->sk_proto)) +return -EINVAL ; + +mptcp_bpf_check_v6_needs_rebuild (psock->sk_proto); + } +#endif + +/* Pairs with lockless read in sk_clone_lock() */ +sock_replace_proto (sk, &mptcp_bpf_prots[family][config]); +return 0 ; +} +#endif +static struct proto mptcp_prot = { .name ="MPTCP" , .owner =THIS_MODULE ,
@@ -4051,8 +4152,20 @@ static struct proto mptcp_prot = { .obj_size = sizeof(struct mptcp_sock), .slab_flags = SLAB_TYPESAFE_BY_RCU, .no_autobind = true, +#ifdef CONFIG_BPF_SYSCALL + .psock_update_sk_prot = mptcp_bpf_update_proto, +#endif }; +#ifdef CONFIG_BPF_SYSCALL +static int __init mptcp_bpf_v4_build_proto (void ) +{ +mptcp_bpf_rebuild_protos (mptcp_bpf_prots[MPTCP_BPF_IPV4 ], &mptcp_prot); +return 0 ; +} +late_initcall (mptcp_bpf_v4_build_proto); +#endif +static int mptcp_bind (struct socket *sock,struct sockaddr_unsized *uaddr,int addr_len) {struct mptcp_sock *msk =mptcp_sk (sock->sk);
Signature · 1 line
2.51.0
Mat Martineau
maintainerRe: [PATCH mptcp-next v3 1/2] mptcp: implement psock_update_sk_prot
Quoted 53 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Add MPTCP support for BPF sockmap by implementing
psock_update_sk_protcallback. This allows MPTCP sockets to dynamically switch protocol handlers when attached to or detached from sockmap programs. Separate protocol structures are maintained for IPv4/IPv6 and TX/RX configurations.
tcp_bpf_update_proto()innet/ipv4/tcp_bpf.cis a frame of reference for this patch.Reported-by kernel test robot <[email protected]>Cc Cong Wang <[email protected]>Signed-off-by Geliang Tang <[email protected]>---
net/mptcp/protocol.c| 113 +++++++++++++++++++++++++++++++++++++++++++1 file changed, 113 insertions(+)net/mptcp/protocol.c +19
-0@@ -11,6 +11,7 @@#include <linux/netdevice.h> #include <linux/sched/signal.h> #include <linux/atomic.h> +#include <linux/skmsg.h> #include <net/aligned_data.h> #include <net/rps.h> #include <net/sock.h> @@ -4020,6 +4021,106 @@ static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,return 0 ; } +#ifdef CONFIG_BPF_SYSCALL +enum { +MPTCP_BPF_IPV4 , +MPTCP_BPF_IPV6 , +MPTCP_BPF_NUM_PROTS , +}; + +enum { +MPTCP_BPF_BASE , +MPTCP_BPF_TX , +MPTCP_BPF_RX , +MPTCP_BPF_TXRX , +MPTCP_BPF_NUM_CFGS , +}; + +static struct proto mptcp_bpf_prots[MPTCP_BPF_NUM_PROTS ][MPTCP_BPF_NUM_CFGS ]; + +static void mptcp_bpf_rebuild_protos(struct proto prot[MPTCP_BPF_NUM_CFGS],
net/mptcp/protocol.c static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr, · line 4051
Quoted 10 lines
net/mptcp/protocol.c +10
-0+ struct proto *base) +{ + prot[MPTCP_BPF_BASE ] = *base; + prot[MPTCP_BPF_BASE ].destroy = sock_map_destroy; + prot[MPTCP_BPF_BASE ].close = sock_map_close; + prot[MPTCP_BPF_BASE ].sock_is_readable = sk_msg_is_readable; + + prot[MPTCP_BPF_TX ] = prot[MPTCP_BPF_BASE ]; + prot[MPTCP_BPF_RX ] = prot[MPTCP_BPF_BASE ]; + prot[MPTCP_BPF_TXRX ] = prot[MPTCP_BPF_TX ];
Hi Geliang -
Minor thing, can you change the last line to also assign from prot[MPTCP_BPF_BASE]? The tcp_bpf.c code does copy from TCP_BPF_TX but it has a different sendmsg member value.
Related question: why does TCP have separate sendmsg/recvmsg for BPF but MPTCP does not? The cover letter mentions "basic MPTCP BPF sockmap support", so it looks like not all sockmap functionality is implemented. Can you update the commit message and add some comment explaining what sockmap functionality is missing in MPTCP relative to TCP?
Quoted 24 lines
net/mptcp/protocol.c +24
-0+} + +#if IS_ENABLED (CONFIG_MPTCP_IPV6 ) +static struct proto *mptcpv6_prot_saved__read_mostly ; +static DEFINE_SPINLOCK (mptcpv6_prot_lock); + +static void mptcp_bpf_check_v6_needs_rebuild (struct proto *ops) +{ +/* +* Load with acquire semantics to ensure we see the latest protocol +* structure before checking for rebuild. +*/ +if (unlikely (ops !=smp_load_acquire (&mptcpv6_prot_saved))) { +spin_lock_bh (&mptcpv6_prot_lock); +if (likely (ops != mptcpv6_prot_saved)) { +mptcp_bpf_rebuild_protos (mptcp_bpf_prots[MPTCP_BPF_IPV6 ], + ops); +/* Ensure mptcpv6_prot_saved update is visible before +* releasing lock +*/ +smp_store_release (&mptcpv6_prot_saved, ops); + } +spin_unlock_bh (&mptcpv6_prot_lock); + }
net/mptcp/protocol.c static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr, · line 4085
Quoted 10 lines
net/mptcp/protocol.c +10
-0+} + +static int mptcp_bpf_assert_proto_ops (struct proto *ops) +{ +/* In order to avoid retpoline, we make assumptions when we call +* into ops if e.g. a psock is not present. Make sure they are +* indeed valid assumptions. +*/ +return ops->recvmsg == mptcp_recvmsg && + ops->sendmsg == mptcp_sendmsg ?0 : -EOPNOTSUPP ;
Please fix the extra space before each "==" as well.
Quoted 23 lines
net/mptcp/protocol.c +23
-0+} +#endif + +static int mptcp_bpf_update_proto (struct sock *sk, +struct sk_psock *psock, +bool restore) +{ +int family = sk->sk_family ==AF_INET6 ?MPTCP_BPF_IPV6 : +MPTCP_BPF_IPV4 ; +int config = psock->progs.msg_parser ?MPTCP_BPF_TX : +MPTCP_BPF_BASE ; + +if (psock->progs.stream_verdict || psock->progs.skb_verdict) + config = (config ==MPTCP_BPF_TX ) ?MPTCP_BPF_TXRX : +MPTCP_BPF_RX ; + +if (restore) { +WRITE_ONCE (sk->sk_write_space, psock->saved_write_space); +/* Pairs with lockless read in sk_clone_lock() */ +sock_replace_proto (sk, psock->sk_proto); +return 0 ; + } +
net/mptcp/protocol.c static int mptcp_connect(struct sock *sk, struct sockaddr_unsized *uaddr, · line 4118
Quoted 10 lines
net/mptcp/protocol.c +10
-0+#if IS_ENABLED (CONFIG_MPTCP_IPV6 ) +if (sk->sk_family ==AF_INET6 ) { +if (mptcp_bpf_assert_proto_ops (psock->sk_proto)) +return -EINVAL ; + +mptcp_bpf_check_v6_needs_rebuild (psock->sk_proto); + } +#endif + +/* Pairs with lockless read in sk_clone_lock() */
I see where this was copied from the tcp code, but there is no lockless read in sk_clone_lock(). It's in sk_clone().
Thanks,
Mat
Quoted 34 lines
net/mptcp/protocol.c +17
-0+sock_replace_proto (sk, &mptcp_bpf_prots[family][config]); +return 0 ; +} +#endif +static struct proto mptcp_prot = { .name ="MPTCP" , .owner =THIS_MODULE ,@@ -4051,8 +4152,20 @@ static struct proto mptcp_prot = { .obj_size = sizeof(struct mptcp_sock), .slab_flags = SLAB_TYPESAFE_BY_RCU, .no_autobind = true, +#ifdef CONFIG_BPF_SYSCALL + .psock_update_sk_prot = mptcp_bpf_update_proto, +#endif }; +#ifdef CONFIG_BPF_SYSCALL +static int __init mptcp_bpf_v4_build_proto (void ) +{ +mptcp_bpf_rebuild_protos (mptcp_bpf_prots[MPTCP_BPF_IPV4 ], &mptcp_prot); +return 0 ; +} +late_initcall (mptcp_bpf_v4_build_proto); +#endif +static int mptcp_bind (struct socket *sock,struct sockaddr_unsized *uaddr,int addr_len) {struct mptcp_sock *msk =mptcp_sk (sock->sk);Signature · 1 line
2.51.0
Geliang Tang
reviewerPATCH v3 2/2 selftests/bpf: Update sockmap tests for MPTCP
In reply to Geliang TangFrom: Geliang Tang <[email protected]>
Update sockmap tests to reflect new MPTCP support. MPTCP sockets are now allowed in sockmap, so test expectations are adjusted accordingly.
Use a different key (1) for MPTCP client sockets to validate sockmap.
--- tools/testing/selftests/bpf/prog_tests/mptcp.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
tools/testing/selftests/bpf/prog_tests/mptcp.c +8 -8
@@ -545,11 +545,11 @@ static void test_sockmap_with_mptcp_fallback(struct mptcp_sockmap *skel)close (listen_fd); } -/* Test sockmap rejection of MPTCP sockets - both server and client sides. */ -static void test_sockmap_reject_mptcp (struct mptcp_sockmap *skel) +/* Test sockmap of MPTCP sockets - both server and client sides. */ +static void test_sockmap_mptcp_support (struct mptcp_sockmap *skel) {int listen_fd =-1 , server_fd =-1 , client_fd1 =-1 ; -int err, zero =0 ; +int err, zero =0 , one =1 ;/* start server with MPTCP enabled */ listen_fd =start_mptcp_server (AF_INET ,NULL ,0 ,0 );
@@ -570,13 +570,13 @@ static void test_sockmap_reject_mptcp(struct mptcp_sockmap *skel) server_fd =accept (listen_fd,NULL ,0 ); err =bpf_map_update_elem (bpf_map__fd (skel->maps.sock_map), &zero, &server_fd,BPF_NOEXIST ); -if (!ASSERT_EQ (err, -EOPNOTSUPP ,"server should be disallowed" )) +if (!ASSERT_EQ (err,0 ,"server should be allowed" )) goto end; -/* MPTCP client should also be disallowed */ +/* MPTCP client should also be allowed */ err =bpf_map_update_elem (bpf_map__fd (skel->maps.sock_map), - &zero, &client_fd1,BPF_NOEXIST ); -if (!ASSERT_EQ (err, -EOPNOTSUPP ,"client should be disallowed" )) + &one, &client_fd1,BPF_NOEXIST ); +if (!ASSERT_EQ (err,0 ,"client should be allowed" )) goto end; end:if (client_fd1 >=0 )
@@ -619,7 +619,7 @@ static void test_mptcp_sockmap(void) goto close_netns;test_sockmap_with_mptcp_fallback (skel); -test_sockmap_reject_mptcp (skel); +test_sockmap_mptcp_support (skel); close_netns:netns_free (netns);
Signature · 1 line
2.51.0
Mat Martineau
maintainerRe: [PATCH mptcp-next v3 2/2] selftests/bpf: Update sockmap tests for MPTCP
Quoted 17 lines · Geliang Tang
From: Geliang Tang <[email protected]>
Update sockmap tests to reflect new MPTCP support. MPTCP sockets are now allowed in sockmap, so test expectations are adjusted accordingly.
Use a different key (1) for MPTCP client sockets to validate sockmap.
Signed-off-by Geliang Tang <[email protected]>---
tools/testing/selftests/bpf/prog_tests/mptcp.c| 16 ++++++++--------1 file changed, 8 insertions(+), 8 deletions(-)tools/testing/selftests/bpf/prog_tests/mptcp.c +0
-0
tools/testing/selftests/bpf/prog_tests/mptcp.c static void test_sockmap_with_mptcp_fallback(struct mptcp_sockmap *skel) · line 549
Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/mptcp.c +2
-2@@ -545,11 +545,11 @@ static void test_sockmap_with_mptcp_fallback(struct mptcp_sockmap *skel)close (listen_fd); } -/* Test sockmap rejection of MPTCP sockets - both server and client sides. */ -static void test_sockmap_reject_mptcp (struct mptcp_sockmap *skel) +/* Test sockmap of MPTCP sockets - both server and client sides. */ +static void test_sockmap_mptcp_support (struct mptcp_sockmap *skel)
Hi Geliang -
Beyond the very simple tests of checking the error values for MPTCP non-fallback sockets, is there other basic functionality that should now be working? Are any of the tests from test_sockmap_with_mptcp_fallback() now expected to work?
Thanks,
Mat
Quoted 39 lines
tools/testing/selftests/bpf/prog_tests/mptcp.c +5
-5{int listen_fd =-1 , server_fd =-1 , client_fd1 =-1 ; -int err, zero =0 ; +int err, zero =0 , one =1 ;/* start server with MPTCP enabled */ listen_fd =start_mptcp_server (AF_INET ,NULL ,0 ,0 );@@ -570,13 +570,13 @@ static void test_sockmap_reject_mptcp(struct mptcp_sockmap *skel) server_fd =accept (listen_fd,NULL ,0 ); err =bpf_map_update_elem (bpf_map__fd (skel->maps.sock_map), &zero, &server_fd,BPF_NOEXIST ); -if (!ASSERT_EQ (err, -EOPNOTSUPP ,"server should be disallowed" )) +if (!ASSERT_EQ (err,0 ,"server should be allowed" )) goto end; -/* MPTCP client should also be disallowed */ +/* MPTCP client should also be allowed */ err =bpf_map_update_elem (bpf_map__fd (skel->maps.sock_map), - &zero, &client_fd1,BPF_NOEXIST ); -if (!ASSERT_EQ (err, -EOPNOTSUPP ,"client should be disallowed" )) + &one, &client_fd1,BPF_NOEXIST ); +if (!ASSERT_EQ (err,0 ,"client should be allowed" )) goto end;end:
if (client_fd1 >= 0)tools/testing/selftests/bpf/prog_tests/mptcp.c +1
-1@@ -619,7 +619,7 @@ static void test_mptcp_sockmap(void) goto close_netns;test_sockmap_with_mptcp_fallback (skel); -test_sockmap_reject_mptcp (skel); +test_sockmap_mptcp_support (skel);
close_netns:netns_free(netns);Signature · 1 line
2.51.0
Hi Geliang,
Thank you for your modifications, that's great!
Our CI did some validations and here is its report:
- KVM Validation: normal (except selftest_mptcp_join): Success! ✅
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/23239200117
Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/aee7a7eb8008
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1068551
If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:
$ cd [kernel source code]
$ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
--pull always mptcp/mptcp-upstream-virtme-docker:latest \
auto-normalFor more details:
https://github.com/multipath-tcp/mptcp-upstream-virtme-docker
Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)
Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)