From patchwork Thu Dec 19 15:46:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Baerts X-Patchwork-Id: 13915170 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8D3F226552; Thu, 19 Dec 2024 15:50:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623417; cv=none; b=brMNocpb6zozY9IxFkSkTCk5mPVj/EIlu3E7HF4VwjiCP/+4jgwxQ9LpuCS16OFbHpJVj+8Xi9oMjheRJgHUqzIeflvayWYE+ZlPK0TE6XDAMvVlhrbcVJMq2U/IW3rL2TprvXCS7EGCUxNbJj/nvP4IIeiSixA1In4MzWYrLwU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623417; c=relaxed/simple; bh=uSOupaSl0R2bP+tGvViCkeLrCZOlUFfU6pCZQqCqeKA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fIoF/tFrtwUHXhoaoZuirCMYU/aZN4dJVzOoj0kiToStbtFESy3b7UCNaD1aVQpq1p5ZU8WNxaOKwk0MusfPpbJqq3JFjyLGq2L/7pGt/6kFTIz+euuIBrQBBQp6XVv3ucPDwqrT0CcqZgWwYdMGKmQIyouVlBRcLaicWLobacs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qBQInViE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qBQInViE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A4A2C4CEE1; Thu, 19 Dec 2024 15:50:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734623417; bh=uSOupaSl0R2bP+tGvViCkeLrCZOlUFfU6pCZQqCqeKA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qBQInViE3X3MrBuNQTZ0fawJH+eTIscHj4mTNPkqJqMDpkthDT9cMiHJichU/81iS OteNVeV1YeEgjxKxnmRgR+m5GHu4dTJngRyb+j9aJDMjmMdRUn07tmTda/ZcSUz3IP ibZfy9hoh4PUlI+PYdu1QDyBBq5TKX95H51RT/tGA3EUxQjzbzM2EyJxbOdZ2bKFxQ kvBvnr2mtz9Bnhzc07ynV17FTtxW0rUXFfIEhjuSzQJ/eJPfih28ljWOcHu9XVIw6P kzvN3xBZWrWVMytHl18vUYs3twUVwl3/SYCp22o+NYCrYIdyfGkxoiycCaQMrv9ujx 8hPGDDdWxtSmQ== From: "Matthieu Baerts (NGI0)" Date: Thu, 19 Dec 2024 16:46:40 +0100 Subject: [PATCH bpf-next/net v2 1/7] bpf: Extend bpf_skc_to_mptcp_sock to MPTCP sock Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-1-ae244d3cdbbc@kernel.org> References: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> In-Reply-To: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> To: mptcp@lists.linux.dev, Mat Martineau , Geliang Tang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, "Matthieu Baerts (NGI0)" , Geliang Tang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3112; i=matttbe@kernel.org; h=from:subject:message-id; bh=lnryqshajbYUd9cCgDiyIBOFzeb7Ox7Vw7A9hLDDvHY=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnZECtu/pzH08fKosSsCBeA/bO7d2/QiJaRXxhB BAAKUCg+U2JAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZ2RArQAKCRD2t4JPQmmg cyReEAC0BjYOvTnlmA+RuJIdOqEBdGShz7oSfVgeHZgeoS9NEDaAt50uuVuNvOhNBoCp78WV6B6 8+T6yLk3EXmYmiX7cAkAwqofnTePZ3PhbNNTrXx4AiBZcBkbjsMfGG8dPsKCAKTjj60vJI1GURL GtIkusE6YPni4VQlWrnlkLWiDtsSvx7jvaKZ/DSvyOCZNBUeklzfpqGKTNbzSvWX1mQ3Dj4sfaG ZMTywh5qpnxj96WHfj1ax2E+/xOijw6AJZSDrAKU02qxBcJVYJHaqJtr3u7Odhwce/NxAVpxioC QAgAJMdEwTgZCTMMXO7Epo4z+2bMKpiprsbk0lyFpc0M1NEnsCoLDysD4vnnvvhMno48cDulusp +570GUwrZuGUgJHjQkysfDWTpb60BPIahbZVK/NDST5/1gpvN4QpZcMaeJIcsHvN0wlFcR/M35I ZTue6+cpPAOkGHBedToehoVCk9AvCXqzWPEZYgAOU/ZhBKRtn9rVC5QntwsiM91llJzmFoCoqRR Il5d9bSe0V8WA2JLS9AkfKXnNTXKXYuC6XGTy84uMTBmt0FVkZwsyzIKDHL/eauELfsVfNnF7Pk S0r/KQhbrUYmDLM4U3sAfuybeOWfK9f5AFoUIIHiJXGegqmtXBk7G1h3l6W2NFosh2X6hW0YhDI ygFZCx+xjTaiWdQ== X-Developer-Key: i=matttbe@kernel.org; a=openpgp; fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang 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. Signed-off-by: Geliang Tang Reviewed-by: Matthieu Baerts (NGI0) Signed-off-by: Matthieu Baerts (NGI0) --- 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(-) diff --git a/include/net/mptcp.h b/include/net/mptcp.h index 814b5f2e3ed5e3e474a2bac5e4cca5a89abcfe1c..94d5976f7b8d8eb8b82f298f7b33ecd653937dc0 100644 --- a/include/net/mptcp.h +++ b/include/net/mptcp.h @@ -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) diff --git a/net/core/filter.c b/net/core/filter.c index 6625b3f563a4a3110a76b9c12a46340828e16b6e..40ed3854a899bf9d11847ec0e8aee174923f03d2 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -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 = { diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index 8a16672b94e2384f5263e1432296cbca1236bb30..988b22a06331ac293b36f3c6f9bc29ff0f6db048 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -12,9 +12,15 @@ #include #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; From patchwork Thu Dec 19 15:46:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Baerts X-Patchwork-Id: 13915171 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C78022616C; Thu, 19 Dec 2024 15:50:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623423; cv=none; b=YOV8/y2nxU+HeFc6AFp4d/rdPOPPKII8P6drw0lltTxt+VJ+2F5D6/c+DTkvxELVimZU1cjEsFHxoUJmnL6PJ2W82vY7JXDB1oSLyJ8RgS4V6OpuoCZ4Hd/2QT74fA3bo0zrQ3kf8CMBRjrUNn7koF/il/Uq6Fd8ibLBqK7wIE0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623423; c=relaxed/simple; bh=W96t/RxyrkRGBDbdV5a6GjcXQ/s600KDjdOOQeqhKcw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GIDJEt619SOdubAz4SGJgcmqA4cnSol3Ppl4Y/Kx+sRT+AQOvaGX0NChDlEsEqdxmsW0VKBOCNVdAp3LJa/YUVANdGiJi2PNu9/J17WeFCsgOJ58bjZzRdGiKRoH6i6gpfnjy0kerrRfFs1PYl4UWK0QKYD+P1r7QVbnWBy6fXw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DO4HcIth; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DO4HcIth" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F27EFC4CEE0; Thu, 19 Dec 2024 15:50:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734623423; bh=W96t/RxyrkRGBDbdV5a6GjcXQ/s600KDjdOOQeqhKcw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DO4HcIth/Q3IyqDkFpuUFwup3+MDVzFPS8Dhf2Pe/i60mejZu9Buah4WQ13amFKOi VTAF48JSSKFguNEPVmeG+oPbqGKDLIFy06xijfHtB1uWTD8JFCDzWnH9LtNF0Nh06d GEEz6stZuMrG98U65YLfcUxO6IU04o4JmSe++nXRJUjQ5qxeZPW00k83dasehp2MNv 3bIi0NiZnBadt/fkmnaRDk+MOSewgbTcdB512hRrbhjeYg7e5XYhWRYMpNLS7Xc8iK Z9AGsABGAnMcQwiLSKtBoAVruHwIslk5vto6NtJ4DgtnZH58qieQYhP0ZtbhHb8vAw 9o76j8ZjDAWuA== From: "Matthieu Baerts (NGI0)" Date: Thu, 19 Dec 2024 16:46:41 +0100 Subject: [PATCH bpf-next/net v2 2/7] bpf: Allow use of skc_to_mptcp_sock in cg_sockopt Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-2-ae244d3cdbbc@kernel.org> References: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> In-Reply-To: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> To: mptcp@lists.linux.dev, Mat Martineau , Geliang Tang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, "Matthieu Baerts (NGI0)" , Geliang Tang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=1143; i=matttbe@kernel.org; h=from:subject:message-id; bh=6JrVc2ULAFXcwJoeGrzMjRsZ5K+d/6l9XN9k4QmIJsg=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnZECusixmfR7aeSuAt+bA3fIj0UldEuyvFNW4y 0SbiAMp5o6JAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZ2RArgAKCRD2t4JPQmmg czPLEADqcXMCkCE3I8HxzUrqLApKF5oodPBxy8gItnot6SJ0kJJZqdGQ7Dkdluzj06wBWP7xvYc 9DdUtRTfCC+lQi8kBZ9UkrDS/mkVgBIRhZSI5gaSN19wTR7jeBYRyKYyt1FuKG6mo95tau+5l6j tJmRn8gTS+/1+h2oV7NDaZWGsgJ3na5Osn8abmoBRmtf6aCqWrPIGGToWQI3+jsyFOM8wyjXPY2 DTbRqPsTIFiLV4wAJ3DcW7wY/21s6AhCHu4MHAbZIkraQZzqQ1X+4+Qfww6ePWtwwQVEFmwpinR pM/gqXW+R5JNY+LtRRUiAr4IImLdLEMhjPYf4hnmQTnhRpPbqdEIkE4K8oUA/+LcleFSzYmay5R fBicig2WA4qGi3Jfmvrt+LQEqU5PZhNaeXJJ9xiO4FG14kmwHw+OmYS7j3+BpM4sxmV6OtSIBWO w9UurakX9f/+f11dFvIkSvLIxjmJpqfP0ryTaq4tkeOrOmxpTDzrij310RwolWPKr6MXsA0EUBr bwYM5ntRH67eDsq1UP5qUPmmHusphpN4vewCV4fOa/7wTs1tRt3o6mkIi0OlbXMZ/Z8qa8YlGvf zGhSynqv0Zkhslpv9BuG0iIbODAfH2239nKFk+kZ9IjI9x4t1sJ26Kht+C3kv8eREfe6K4TqZZy c8A7oElNsNbZXXg== X-Developer-Key: i=matttbe@kernel.org; a=openpgp; fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang 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. Signed-off-by: Geliang Tang Reviewed-by: Matthieu Baerts (NGI0) Signed-off-by: Matthieu Baerts (NGI0) --- Notes: - v2: new patch. --- kernel/bpf/cgroup.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c index 46e5db65dbc8d8c6591b53dfc77bb689357f33ea..1ca22e4842cf6b6c6bdbc37bf415187e706b2b69 100644 --- a/kernel/bpf/cgroup.c +++ b/kernel/bpf/cgroup.c @@ -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; From patchwork Thu Dec 19 15:46:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Baerts X-Patchwork-Id: 13915172 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CF31226199; Thu, 19 Dec 2024 15:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623429; cv=none; b=kuqjIRxIsb24FMBQZ1OWrLp68dMoTsrP9w2FmjAjw+jhxB97srYqnjrGzziDYNtAZujMgSySEkXCqHmKabdPKA25kvXFXzkwTfIlcn3nTw3CxFdGUvMfmlcVTkJWSQsd0VoYFtKrqK9D/0pm2NXWa2/G3xN6OuVhbEWU28RTKBM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623429; c=relaxed/simple; bh=tCCojsjaNMqopdUO1fLBb6qOmphgPi2i/lnsTh4xe1s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lzNSXyu/0qzWF1IfnBr2Q2Yi4yeYTnHqAOJGWqMf9VA6Hus4cczk/DGKYUVBNM5uNLVEG3dQFWRW0gT5lEhZRO9zuiqARnWR5Lr0R6ByhC/L156MWMmmbWG23vaH9TBGLIW2wjNCCxnOPhkmNNJEZSdOaORoMDRCPM6xj5itOxQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DWg1mcg9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DWg1mcg9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86B06C4CED7; Thu, 19 Dec 2024 15:50:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734623428; bh=tCCojsjaNMqopdUO1fLBb6qOmphgPi2i/lnsTh4xe1s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DWg1mcg9JfTUVSCN2nxu+ykM2LFZoGmgXdXkz4Y5P37JKH+T7fuvkDazpO4z+6zfK D9UqoCgmT9GsZXdvwuU7TxJHPh57zp63LJOtIbWFWPhPLQYq0++WU1BkXWFl+bQbsf M6l5fE+fk0xWwaf0RaU2ghUIbrsshg5/irTmrYiZftqx8W7T4FECo2b4j78vab007/ 1bW2UV6GI7R4vu1qW7kVEw5CUs3+i+dU4DvT1sx8hEUkLdJ53JtxaksKxqbxF9UUWC LFYGDWT9yaYxw0zDrvHdvZPFshdeIAKQ0x9WSjaK2dZuGbUeEAYUh6RrjC7fWEPmbg A83LsrVAcIr2A== From: "Matthieu Baerts (NGI0)" Date: Thu, 19 Dec 2024 16:46:42 +0100 Subject: [PATCH bpf-next/net v2 3/7] bpf: Register mptcp common kfunc set Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-3-ae244d3cdbbc@kernel.org> References: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> In-Reply-To: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> To: mptcp@lists.linux.dev, Mat Martineau , Geliang Tang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, "Matthieu Baerts (NGI0)" , Geliang Tang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2433; i=matttbe@kernel.org; h=from:subject:message-id; bh=sUSvut3vAWGTd7i08U/NUT9KFz0M0hzmtEUkxiYGJ1U=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnZECuHZx5LJSpxvnqSR8Mkx6mESAhE8zGDz2Wu eQzysLkZO2JAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZ2RArgAKCRD2t4JPQmmg c0+BEACEVLXWQTZqAD0ywm+lu6iZsZIdj3CZiINaslFKdItlJQlZxgu4tZDaGlvQI28kpZqSTxg D20dOjG6Au5BnDlKlGSmVDHYHhXEpYXBeBt/w/WTSE1Mrxtaabg/eke5J1DKwDBY+oFTC9OU5Ql 1/rGxjgql0EgVAA4xqTERZcLqC5vsHI79cXj+YqKNXUzSSbw/S66coapbxsaY8BgvPeTy5v9t26 Mynl8/xl4J4pNhbIb+SPH+6H+cwk1V2hxbjW6/dU+2wrTXdqHHotPBZcPLhqXhAD19MZ180Jyj2 DRdIQsY4gWI/OpVGFvvRBhm3C6QWQKQEarE2eG2iGoVANTDogT5vvJ33tec30u8meeQUtp2tvpJ SDgNJLY3eWwvtmnUO+7herkIzZmzynNv4c4I5sPuriNDID7ZKDOqkEF0ZtpqICjRJ9nDKJmQH+l /LLf8tJZ3wCihOnx1AbDHfPIr9WuYpIRmUirjDqgyGUyAGCi0PmnmpQt9Oi23Nb+6zijWczAmYO pU0iKf7JKl1NhMqtddGr9JB9Nb/da73ium7YwgWOdOLxz2z3Xl35dOhtmW9KrMYbiiQBr2B99F+ G46M37OwMBDWe669wsLaqSiXRucQvD6mCBesfGcWTDvrfkTaYGFIdrZBtQAgZCcaYUJ18RT/U23 687S6Yq+c6KwXrQ== X-Developer-Key: i=matttbe@kernel.org; a=openpgp; fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang 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. Signed-off-by: Geliang Tang Reviewed-by: Mat Martineau Reviewed-by: Matthieu Baerts (NGI0) Signed-off-by: Matthieu Baerts (NGI0) --- 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(-) diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index 988b22a06331ac293b36f3c6f9bc29ff0f6db048..c5bfd84c16c43230d9d8e1fd8ff781a767e647b5 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -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); From patchwork Thu Dec 19 15:46:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Baerts X-Patchwork-Id: 13915173 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D397F226535; Thu, 19 Dec 2024 15:50:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623435; cv=none; b=NCXMfNeCALL288cpJdEeg3ApGN9rkxzWql/YRtTppCFXo8MMTL/kE5o9keIv1dXfgjkdvBICTcpwe3b6HzUyN/86kjFU6ZOpZEr3psY8obZ3w8HQtgEXa484vKPm/rYsuhAt2dUJhLv7Pxr0pN8q1pojrzGkQESgam7NchA7VL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623435; c=relaxed/simple; bh=YbNEkDatxwQX2bJepzpJ+SkdjZp9ZYabi3reVfSA1Ug=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FeWN+fXbfQaa148xxvVtJ5sZYR2wTKxGMyufMKPiandy9KoAdsMRyu5lFU13SK/rT3kcjnHGOCzDT5LsdI9N88r4y1tjWOUClX9Q1niyXNOHBhNen/QYaLyIvIX9Fl/BwpYRV7Lt24aVLtZD8SP2m0ydX6ZR8Ci8HgtHXYCwRoM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ak6lNNq6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ak6lNNq6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1798CC4CEE0; Thu, 19 Dec 2024 15:50:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734623434; bh=YbNEkDatxwQX2bJepzpJ+SkdjZp9ZYabi3reVfSA1Ug=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Ak6lNNq6QnT8JRtbOgd7CFUtE6vfNe/cai/M9fSubIGDkZdJpgnrAixaEhlwMyl5J 2yVF1cjYqsRNwUlwzpg70DEzFVXeN0O4en0bphFsG6BY8Rb9W5Xr40ulI+VcfgXquK aRJnc3IKA35eSR/dArHsilvORiiGEuNlhuaZvj25/QfY1bEfzp5Selut6psPOVteBR TmYepnKHW31fbWYRdrrZRTvz+5Eg3AVQzNkZdp0tu/5AVVDxMSlAIX3ICfILIaWBe7 lj84r16brhr4WgMh+n7REzd8c9y5QUIRoANEfQJem9eSQEmmtdDKbw82ubwpHFl90E X5BVzxDsddvqg== From: "Matthieu Baerts (NGI0)" Date: Thu, 19 Dec 2024 16:46:43 +0100 Subject: [PATCH bpf-next/net v2 4/7] bpf: Add mptcp_subflow bpf_iter Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-4-ae244d3cdbbc@kernel.org> References: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> In-Reply-To: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> To: mptcp@lists.linux.dev, Mat Martineau , Geliang Tang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, "Matthieu Baerts (NGI0)" , Martin KaFai Lau , Geliang Tang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4448; i=matttbe@kernel.org; h=from:subject:message-id; bh=v0+LtDuhV9sdYgRZ0UMX9vl5kYAb+yNIudzVYxf80CM=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnZECuVCGNdAdOhL0Zly6Gubt2PHoYSSeHM9+3a 39IWNQTX5eJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZ2RArgAKCRD2t4JPQmmg c2vyEADOLPeU0SdqV8H5tOQeYhtW06IxLD7L/XaIWc7tF1nZoq0C0+WR9++r64dM6UfvR3vTjj8 L7LmzecqerXziCBTz6KRn4OkA8uuLlkPg+iMuhpSxX/nQLz2Rz/qSZ7NJZjAVKhPoxu1crpA1Y6 236Jd1SKx3kAgsccmmLx0uJ1lxst9l8h+VbCWJwWtuJbd/wuRjmIrDr4tRD6GgYksXUdyGEc563 lACoEKyf9L/dk6hJK006wFnPhgEo4KhgD9FunSkN9hUE0oLA06cIBXCsWHpjD7WD8MmXr2Crf6A 4STOYn1kje55T/HOeCF8DX6Jsag67VMziBbJ5P5W/fB/1xa6ZUIoDfX7O1/BXYOVnNFJi+7xJnw SAdTwrdD+R/jd9JQhHtwp7W0Bq7SRBBHTa8uOJs/L56P6abkXphMr22r3CrohW8eVtffObZ+Ouw o9G86640CqcU067yzk5kVULULMm9tTjXBFWKzv/ExvnnXZYFh7lDvPHYGtPZ08sU6CC88lU1Fiq ezMoRirmTSbKt9BIIvnfG3fHe3wDAEJhf/EYcdGN/gwxGb9MFHbeAG5AhE1lTD8dzK3+S3gOXxo Q3cqdwICni1efEUNCAu26RDVoHG0bjjXUJ9OEKKrJXCDSWknHUl2DavKW45dPC64uIC+0JkziXe cMK0LyW2/cOblmQ== X-Developer-Key: i=matttbe@kernel.org; a=openpgp; fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang 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); Suggested-by: Martin KaFai Lau Signed-off-by: Geliang Tang Reviewed-by: Mat Martineau Signed-off-by: Matthieu Baerts (NGI0) --- 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(+) diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index c5bfd84c16c43230d9d8e1fd8ff781a767e647b5..e39f0e4fb683c1aa31ee075281daee218dac5878 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -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 = { From patchwork Thu Dec 19 15:46:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Baerts X-Patchwork-Id: 13915174 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F14E226163; Thu, 19 Dec 2024 15:50:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623440; cv=none; b=CoSxb77/uEoxmUzcze0LvbXJbQd7Q8YutBIY1pcyLoXtWU6WbBrKG9n0+HBWLSBDyaAucdFblH7HnYqs80rxGCMGxaIE7oaA4Nfb8Ok5jKN7mJOKNh41n7haS0wa9k9uVWyvGoJ2AKrqWN1CD69LBNpgGaF4T6R5XQ7wrH4FDT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623440; c=relaxed/simple; bh=885bqhsxwSrHf3hr8m6L9FjjSKd1BhbPypgPwXzqfg8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=P7I8FuFT1N+MViGXmGoBPTeZZIufWYAcRjcldgDcCA+saZ3rrOjtfTe4vO0ibEV0yvnopKQS6U5UIVCpa96p5NdOYxl8rDlejgZ6MWAZTwZMI9EP3lFO/+aOS4gBR0/P8i8NlR1kqFlDTfv0hbqrQSmCnpht20vo8681gLPxhSA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KVFAE+lX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KVFAE+lX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CC76AC4CEDF; Thu, 19 Dec 2024 15:50:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734623439; bh=885bqhsxwSrHf3hr8m6L9FjjSKd1BhbPypgPwXzqfg8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KVFAE+lXFnFxPEFWLHM+mL3Pvb9Xon/BYJlxo8SafbWo/85IE4LEf23aHFO6iTwWI 6D6bqdS0H3tNG5iV0PRSncl8lmjwN0RRZJVMcOlkcjXZBlY+dRqzqM1GvQ2pl2Jd0V evcoKLZ2VLZOSvMuwySFKoP4uUTsJLTNv5uDJIkTLBuXgYkedSXp5UFK4bp9TEP40/ StOtotULv+PzCCQN9TXFDh/DpSvHOwY8alETHmTXBi0aYo5wCvFJUEXdK8335flivI L+GubkLyR0tPXPjRsm94bAdgCJUhekMFXkrBOpOCbSofChv5Eyh758ZHdF6X/mmcvJ rG7Y3TNvN/3YQ== From: "Matthieu Baerts (NGI0)" Date: Thu, 19 Dec 2024 16:46:44 +0100 Subject: [PATCH bpf-next/net v2 5/7] bpf: Acquire and release mptcp socket Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-5-ae244d3cdbbc@kernel.org> References: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> In-Reply-To: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> To: mptcp@lists.linux.dev, Mat Martineau , Geliang Tang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, "Matthieu Baerts (NGI0)" , Geliang Tang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2002; i=matttbe@kernel.org; h=from:subject:message-id; bh=EEPXf9FaRn8aAdYNTiDw9/bU62ypSH7UW8zt9P0k814=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnZECul4EIMgu1Vz0B45o5ZDIdsPYVVKHIytZiV YxQHcjGFRGJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZ2RArgAKCRD2t4JPQmmg c2vYD/9Tkzh/OtVbJRSiXvaFURaoVlPR5w+InOLbWk5p7hT/2LxKXT2zYMBRJJyLZ84nY9r1V52 VNJOLrvkHk5vXVSt7tOrtNEQkHaDqX0hAD0kaTKVBZ2SxppKMM5paua8k9RpTitKsUstr6QAIDZ fLR2c54vJZKB5TkBRrgIx5oU24Q5DILIhs+OMEquzAXc7Z3WnXqteWXcncQWi+DJ6pXt7bWFiL1 oIYPt2Vr16n2zWLjFGa1mXY8PS46JKit2eb8MhnEhArjOeWEDMPd9k5wXZqq5cOA76vsIR/onkC NX5HzPf7Iea3//EdMMatk6FwWXR214MPucoMsRXuO6BP5/vgs49XpCgQ2OPWAp4xy0CnJUjiZl4 kKWsbAnYCOoDRyvxqahvXm7DqzUJXUzt2mhLwX17cQBjINFguwgC3bYcWjUVfjLDBTPA4O0wNGB 8Jk07Em0QulHSciKNhZe3tExisppCOn+oLhZSX7nDQpvJC3d/4bUX1X4VLGJ9gRTbGT0G9dO8G8 2KSst0LVmUYzylMYsXLlaBZdHdd0xhiqKe4/rjTOzofLscNQ9hFw3fEjMqfYk4XkgUYYKhZLGx1 bwO9AE/dq2wseAkDiDTUnJOAG+FgO9eiigVeOpi1KmzVqGRBmNazNAYp8GTzqgqlxtsgH7nD6PK s9DCj4hYOaaWFxg== X-Developer-Key: i=matttbe@kernel.org; a=openpgp; fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang 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. Signed-off-by: Geliang Tang Reviewed-by: Mat Martineau Signed-off-by: Matthieu Baerts (NGI0) --- net/mptcp/bpf.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index e39f0e4fb683c1aa31ee075281daee218dac5878..d50bd1ea7f6d0ff1abff32deef9a98b98ee8f42c 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -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 = { From patchwork Thu Dec 19 15:46:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Baerts X-Patchwork-Id: 13915175 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2DC1F228CAD; Thu, 19 Dec 2024 15:50:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623446; cv=none; b=hraqTbQEWTWI/xxd/HXlxoytiqpuzhrDVpxHjfs/u60xpIIo8lKgfCffMURxJbrKlBilkKyESehnrMJudkO2HZcdHlv0EYhZSVixIfeDUNONViAlWn1TWDNcNTWNOjm4a8o994k/b0DgfQYf5xmeuBzPWIAfVYNliIsiNVoyBTg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623446; c=relaxed/simple; bh=DszTJJw7BeF4Dq9illY7Eq+noW6jmff6HalMxhu2MhM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mfOfMzSKSWZI4nSrZPjO3x+6vGcKtPidKIYZmeayV7Dqiqe0IwAuR/U6radoYqL0TDprHkYSwb9HpaScFd+EZJDLjMlGTpIS8saA3mnSzrWCBFuF3ZpQnDTOpTZSaE7K8/sAD7nhzZPkREqMe6+poFG62dX4bFK2AbzOzZMQgas= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=inodVxpZ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="inodVxpZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60BF1C4CEDE; Thu, 19 Dec 2024 15:50:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734623445; bh=DszTJJw7BeF4Dq9illY7Eq+noW6jmff6HalMxhu2MhM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=inodVxpZX+tuUtf+60UKSQKZgyCvOEy0XUtcHRbfoD8WNUfAwa4iy2FB/+xgbF8Wm eLq74MU34VcjVpanttNXc6Kw6omQ4KC4SY7NyoVt00Ht1MRVaeMZcJTA0X8lbWogJe VV1HZc3z/wEEzk/ccSVb+PYxq0ZfGJqiHAijOVkxZd8gIrDEztPPhRLbtWfmnO27ne NFz37WyWXuhsDIbJGhuweyoLLuQ/+vGrx/AGOSlPmlheg2sv4Y+UW0oTqK/2/RGxAL AKFcCuYpURAzeAEBSVVwr9CdO1U6tVhxkgoa3zF8It7njixM6WoTpg+kRVk+PxLM0I 91zUyb1tdd97g== From: "Matthieu Baerts (NGI0)" Date: Thu, 19 Dec 2024 16:46:45 +0100 Subject: [PATCH bpf-next/net v2 6/7] selftests/bpf: More endpoints for endpoint_init Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-6-ae244d3cdbbc@kernel.org> References: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> In-Reply-To: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> To: mptcp@lists.linux.dev, Mat Martineau , Geliang Tang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, "Matthieu Baerts (NGI0)" , Geliang Tang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3788; i=matttbe@kernel.org; h=from:subject:message-id; bh=Po09A6yg2QyWP12VFvj6lUg78RIYIk2YxWtr3rxOpQk=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnZECuhP2z2ITaXuAOojzKD7j3pk1TEvcNJIfDR ycxmgA053iJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZ2RArgAKCRD2t4JPQmmg c/VnD/9PhaUefizQp8URVACGbALKPMkyZ8UlEy1TfbG+K24rhewTdKY/i8OlueG88Gq06jARQR/ S0rF7wMSkUF5nZw8wOP3OXSre+IJuNLC6ccki+4x/913nAYBgkyXPsY6p6XMT3iCGz14BAIxGE8 f7gCN1quUxfWu+9xEocWCyuQDNU5higx0G66PDtVhkUDpWC/RlhfDuFEsEB8Vy6ev7F3OiOaHm4 m/ObJweQYno8e73x+4iCDcsvu76GmuxOIOW2w+Mu3SGGEgrxZRvsxIIgpJgl5AXW3+yilO49AAV 9LzuJHV3+unu0YByPqf0/pLdZdlhxVDDQCHRi8dnbFCGPbJRgZYpsuGbkW+4d/jo7YWifJA8hQJ w5GbKo9OBzjZ4mdgtyPqu1VbNkEujDVzg7GrTC+GPh7xrd5RUTTRSU3HNP3rJnCnVpReXINSDxT eNkD6zSnu15j2/cccCN51BLkji8mDjb75SskIXjmZ3Q3/OrAVXAv4J0bHTZuXL4UvRDn9s4jpJN 4i2s1GP2+faXxFNJ4+CBrvTz0MllFb1xxzE5vaCboRNwJluSu2EQ35MVMO6lNeEiY5x1WWIuARB wnu53vcpbGcBoCzJJi+PKbW94caQdh9ZLU0kVxGxWWBkyQUbx1j682tdWOxJDw/56sQeXE2Agt0 O36dLQSPw6udQGw== X-Developer-Key: i=matttbe@kernel.org; a=openpgp; fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang 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(). Signed-off-by: Geliang Tang Reviewed-by: Mat Martineau Signed-off-by: Matthieu Baerts (NGI0) --- tools/testing/selftests/bpf/prog_tests/mptcp.c | 56 +++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index f8eb7f9d4fd20bbb7ee018728f7ae0f0a09d4d30..85f3d4119802a85c86cde7b74a0b857252bad8b8 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -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(); From patchwork Thu Dec 19 15:46:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthieu Baerts X-Patchwork-Id: 13915176 X-Patchwork-Delegate: bpf@iogearbox.net Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDFFA226878; Thu, 19 Dec 2024 15:50:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623451; cv=none; b=ZmpD13G7Q99EQiMNOdnKgjixchVqUeUJ9vqvzQkkQOoxTS88yr8PpYgjUj9WQ2n1+I6b2Wwkr4jhoza/6CHptdT9awYo4aDbwgIp0dicKzCCxmLGTlXWGoM1B0t3Q0Hsu/Zr0g75cL/ts+pvljKyFc3PYFRsdD4LzXNF3H5vxZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734623451; c=relaxed/simple; bh=yOr4YVcrLbV+j2DBB+pvmmyHN5Y3wzr/J7XUepFTf/0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=V54BbqZHHEUmwrU2kyO2/Tc44rb7rwfAXtD19tmMRBUqTbPbTOkd3ZpdllDpJ/HcASi36Axvg1hnnzMGkqN7WFGc4QsHe5tMmNkE3M03IQb0R4fMVLtE7R3eb3u+pqZ/AIStDCF53MCpaSJrcpY904vF0pLcN4vG9Sl/Pz8pVig= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=W1Z3fvyE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="W1Z3fvyE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4025FC4CED7; Thu, 19 Dec 2024 15:50:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734623451; bh=yOr4YVcrLbV+j2DBB+pvmmyHN5Y3wzr/J7XUepFTf/0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=W1Z3fvyEOnOk96rrWoGzGnO4TNKEJ0JBdXkThEPz2AsVK4XDhoh6PXxtryifjRK31 VIQbhnLoErkNMZz3qqg+6Akl2oKdteEc+fm71+/1z7LT0f4hr9C7l1ovemGNvR3Eah f7BuhKGxLg9yLXlIy0qJjw/P/doDoCCFCbeQkjSjfT1stkZSTohMysmiPXZbuuC/R6 1/yb4aZXfBQ2OHVsPBaRA4wGR4SBBExQhbm0Ndo806Y6axwSfSA0t8Gf4gt/gIgoWF CE4mIGwjwtEP1Tg6h+vP9UiT0IWESsIP2ocKwVh4S0xRayuOB97RiqZcactwgVxpv5 axjwmLXVIBPew== From: "Matthieu Baerts (NGI0)" Date: Thu, 19 Dec 2024 16:46:46 +0100 Subject: [PATCH bpf-next/net v2 7/7] selftests/bpf: Add mptcp_subflow bpf_iter subtest Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-7-ae244d3cdbbc@kernel.org> References: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> In-Reply-To: <20241219-bpf-next-net-mptcp-bpf_iter-subflows-v2-0-ae244d3cdbbc@kernel.org> To: mptcp@lists.linux.dev, Mat Martineau , Geliang Tang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, "Matthieu Baerts (NGI0)" , Geliang Tang X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=9284; i=matttbe@kernel.org; h=from:subject:message-id; bh=bq/oYTxQQ+2A/IA41fcrwu1uM6Uk6qmTeqE376zM200=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnZECu1e8GkpdmZGYvyXzQArL8DlgcE6mHZlmKN MxIeil0/kqJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZ2RArgAKCRD2t4JPQmmg c+pxD/9ny3wXWFBPZiEJWambXs+hb29ypQ+IXpG/grTa8P9tb0zcqmoLG+J7MiQsyKr1keCbF6J DW1420z/XcviOZhtdJ0S2qN/pGU5D+4XS0j53v51suQMqt0b2o+uil8EAew6VmapyfPh2lOZCKv wmczAakHIzg9N5Wh12UINOCCcmrBdvaVxW0E9kJSQ+f0+7yE86BYSs8nDluDafKUZ0OtXQTUSyn O6zYvvEpV9frKPvL0uncm2rzV63C3Ix+YsGCMhGBxF+V3uo5QE7fTCATtIko0ECIql/+yXehE4c u0/Jz/iUw3YxRCRm1irHhilZMBAKj9zc4lhd0kmKREuaFhCDISGJvBfVZpqPW20KY63WcbV25vu REchtTCbZo1Te57y3JgSYQTWRal/2Jhxmg+y2lqXbzM539nuOxrFlBxOvf61ggqNi35GZ5A77AH ysWC7oOiDVyhAVrI9BqCa29l+DbvYhrDUKGlk9Jcs7Pf1TG0ScduX9RWXIXBtCwHEA750ibH2kO X7/jycj7u/KertC7E5Vyz5rD69QApSIMAMF41q4XGpfiaVabDKZuJzRMzcLyam4SkhLPtf9SZn9 FABm+JPXUKDyj1O35rnqG1OI7v2F1g2E8t71fGmF+hwsW/rsAY1gSQbd9iE8qA5jtofZ3OwbiYy 8FKJS1CTZbpe/2g== X-Developer-Key: i=matttbe@kernel.org; a=openpgp; fpr=E8CB85F76877057A6E27F77AF6B7824F4269A073 X-Patchwork-Delegate: bpf@iogearbox.net From: Geliang Tang 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. Signed-off-by: Geliang Tang Reviewed-by: Matthieu Baerts (NGI0) Signed-off-by: Matthieu Baerts (NGI0) --- 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(+) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index cd8ecd39c3f3c68d40c6e3e1465b42ed66537027..2ab3f0063c0fd6091ee19da4787671b89b5661f0 100644 --- a/tools/testing/selftests/bpf/bpf_experimental.h +++ b/tools/testing/selftests/bpf/bpf_experimental.h @@ -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, diff --git a/tools/testing/selftests/bpf/prog_tests/mptcp.c b/tools/testing/selftests/bpf/prog_tests/mptcp.c index 85f3d4119802a85c86cde7b74a0b857252bad8b8..f37574b5ef68d8f32f8002df317869dfdf1d4b2d 100644 --- a/tools/testing/selftests/bpf/prog_tests/mptcp.c +++ b/tools/testing/selftests/bpf/prog_tests/mptcp.c @@ -11,6 +11,7 @@ #include "mptcp_sock.skel.h" #include "mptcpify.skel.h" #include "mptcp_subflow.skel.h" +#include "mptcp_bpf_iters.skel.h" #define NS_TEST "mptcp_ns" #define ADDR_1 "10.0.1.1" @@ -33,6 +34,9 @@ #ifndef MPTCP_INFO #define MPTCP_INFO 1 #endif +#ifndef TCP_IS_MPTCP +#define TCP_IS_MPTCP 43 /* Is MPTCP being used? */ +#endif #ifndef MPTCP_INFO_FLAG_FALLBACK #define MPTCP_INFO_FLAG_FALLBACK _BITUL(0) #endif @@ -480,6 +484,73 @@ static void test_subflow(void) close(cgroup_fd); } +static void run_iters_subflow(void) +{ + int server_fd, client_fd; + int is_mptcp, err; + socklen_t len; + + server_fd = start_mptcp_server(AF_INET, ADDR_1, PORT_1, 0); + if (!ASSERT_OK_FD(server_fd, "start_mptcp_server")) + return; + + client_fd = connect_to_fd(server_fd, 0); + if (!ASSERT_OK_FD(client_fd, "connect_to_fd")) + goto close_server; + + send_byte(client_fd); + wait_for_new_subflows(client_fd); + + len = sizeof(is_mptcp); + /* mainly to trigger the BPF program */ + err = getsockopt(client_fd, SOL_TCP, TCP_IS_MPTCP, &is_mptcp, &len); + if (ASSERT_OK(err, "getsockopt(client_fd, TCP_IS_MPTCP)")) + ASSERT_EQ(is_mptcp, 1, "is_mptcp"); + + close(client_fd); +close_server: + close(server_fd); +} + +static void test_iters_subflow(void) +{ + struct mptcp_bpf_iters *skel; + struct netns_obj *netns; + int cgroup_fd; + + cgroup_fd = test__join_cgroup("/iters_subflow"); + if (!ASSERT_OK_FD(cgroup_fd, "join_cgroup: iters_subflow")) + return; + + skel = mptcp_bpf_iters__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open_load: iters_subflow")) + goto close_cgroup; + + skel->links.iters_subflow = bpf_program__attach_cgroup(skel->progs.iters_subflow, + cgroup_fd); + if (!ASSERT_OK_PTR(skel->links.iters_subflow, "attach getsockopt")) + goto skel_destroy; + + netns = netns_new(NS_TEST, true); + if (!ASSERT_OK_PTR(netns, "netns_new: iters_subflow")) + goto skel_destroy; + + if (endpoint_init("subflow", 4) < 0) + goto close_netns; + + run_iters_subflow(); + + /* 1 + 2 + 3 + 4 = 10 */ + ASSERT_EQ(skel->bss->ids, 10, "subflow ids"); + +close_netns: + netns_free(netns); +skel_destroy: + mptcp_bpf_iters__destroy(skel); +close_cgroup: + close(cgroup_fd); +} + void test_mptcp(void) { if (test__start_subtest("base")) @@ -488,4 +559,6 @@ void test_mptcp(void) test_mptcpify(); if (test__start_subtest("subflow")) test_subflow(); + if (test__start_subtest("iters_subflow")) + test_iters_subflow(); } diff --git a/tools/testing/selftests/bpf/progs/mptcp_bpf.h b/tools/testing/selftests/bpf/progs/mptcp_bpf.h index 3b188ccdcc4041acb4f7ed38ae8ddf5a7305466a..31d7ecdc4c0489c63d28a25778f91093a6a093a5 100644 --- a/tools/testing/selftests/bpf/progs/mptcp_bpf.h +++ b/tools/testing/selftests/bpf/progs/mptcp_bpf.h @@ -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 diff --git a/tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c b/tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c new file mode 100644 index 0000000000000000000000000000000000000000..fd5691a4073b22c24d3fa6f7ed6edb32d366492c --- /dev/null +++ b/tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024, Kylin Software */ + +/* vmlinux.h, bpf_helpers.h and other 'define' */ +#include "bpf_tracing_net.h" +#include "mptcp_bpf.h" + +char _license[] SEC("license") = "GPL"; +int ids; + +#ifndef TCP_IS_MPTCP +#define TCP_IS_MPTCP 43 /* Is MPTCP being used? */ +#endif + +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; + 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); + } + + 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; +}