From patchwork Fri Nov 8 15:52:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthieu Baerts (NGI0)" X-Patchwork-Id: 13868388 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 CC0001F4FA2; Fri, 8 Nov 2024 15:52: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=1731081165; cv=none; b=Nb1asgvwJIeMpGPKcCwdYyz5M+lu0KSwWknD/ew4BEPf/uJPYHnJiSeTsQd5Ty43Maf8ohg5lbAQRa9kNyMMwAJho++xC3dCQsRGmk+pJxIsKuRrSOi7gg5XcTKCtvlUFgryaEB1wtID3qdROVnIys+gUYNt3T6EcgrWKZ9w17s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731081165; c=relaxed/simple; bh=mEh3XOTc68kzTvDwmnzBDb0cxXxbTbSlC8szo/9HBcg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=slFUfzK64l+OSUia+izyBrKb666hY4S/4Wm3z1idU050JgUaLiJMWsz4rW/QbgO5L6pjIiQdIyMfC4Bx0+T9Ka/JsNzzDNoupQHEX19jjzPIaxcSqO8C+yNGraeO5rCH+w9FHg9Pk35lxSHPHh3yfo3baTqb2E/jT/Tu9s7pFrQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SWwvA8UO; 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="SWwvA8UO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 34F7FC4CED3; Fri, 8 Nov 2024 15:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731081165; bh=mEh3XOTc68kzTvDwmnzBDb0cxXxbTbSlC8szo/9HBcg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SWwvA8UO7DxE00k1YGaRjIRy/N480ZLqK18obWph/YSfxbFxpunP0lJqSKb1aK6nL iPQVmf3w5Mm0GbEqrQdtlfDtM72MV7pJn4rPivGOnv4TOOZ2gHGcS2bU78WOu10hnI J1/juZWmLYWfetTfjj6EyOgXnQINJlOIlv+5AqKQH60hD7WhyDrBSmt5VXOi6O3bMa SUtdK64WWTnTikaH0N3o9tZTlaIMwSbsqkzkBllSkrzlMQdohSWwzRcqRMr5HHgoxg sxuIWD0E5XmXhfdkj2iV+EXfdibOnUJbdC5t4/V7kEAJtLWajr2BWovBm+hql3SQUK suTtQkU731ZXw== From: "Matthieu Baerts (NGI0)" Date: Fri, 08 Nov 2024 16:52:30 +0100 Subject: [PATCH bpf-next/net 1/5] 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: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-1-cf16953035c1@kernel.org> References: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org> In-Reply-To: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@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=2337; i=matttbe@kernel.org; h=from:subject:message-id; bh=ZfmGoCnxT5aCkK7T9o4SzLEnGMSc8lW3wKNOypvXmZ8=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnLjPBdvjTYmNfnwSvvfe62/88Iy13zBc79tYV2 lOK6NX2RsqJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZy4zwQAKCRD2t4JPQmmg c0uzD/4mUXion0365sHlcGOAjQIz97cRKrhmfCCtmxBuu4910qPVeApn2gexVT09LOiL3EAqXHX 1BGSQlbeMggnt4NaP+FIxt9RnNJq912YuG1W3g3C5BIzDdW+qfuZnLDCGS/rCO3W8wIdTMrNb0y JrcdMPvHWxIdxmfckqDpDFcA62x6nGyohazv0BuZiLoR1hqjlNUwiSV1HW0pk83ia/NqFcH0qRk f3vB2WqTJIGRTE3uEq7v7Rm2wSB9YIFBrszTOgNo4ot2rBmURT9417dknZqf0JNNxdGii96IuQI 8j1dw/jURE1vjzMXet8108YzpnxZ0MW0EdIqHQW5A5i9n/nYfbsynIp4dAxOHc0JaDbkQyLuwPo NO1I9nTVHJbIlYuLULp7dKLf5TiCgC2PFvdipuVNdSgspgSJMeBUKHf9zs62BG1xVLdTn/cerxP d/MIDbaByut5OuejOySkLZKiP7fL+J4mFPiSVOc7WLoPn8tuq6zK55+mb8ThUbLVzaCxB9IyN1T FuFCOEPvBzfyS+RJejFnE1UieijNtonPyQ16V48PiO1CZ0kim8D2PNi3vUXlAsWKhafnKFz7y9C Ec6I5gKXUMOjq3LPRiub8rpcXaTPIygCTQCYblNGGgZwSVk4M/2hVPfV7daAKAQWM8wQ3ESnlFM FCBLIbybQCO2IpQ== 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) --- net/mptcp/bpf.c | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index 8a16672b94e2384f5263e1432296cbca1236bb30..6f96a5927fd371f8ea92cbf96c875edef9272b98 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -29,8 +29,46 @@ 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_sock *bpf_mptcp_sk(struct sock *sk) +{ + return mptcp_sk(sk); +} + +__bpf_kfunc static struct mptcp_subflow_context * +bpf_mptcp_subflow_ctx(const struct sock *sk) +{ + return mptcp_subflow_ctx(sk); +} + +__bpf_kfunc static struct sock * +bpf_mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow) +{ + return mptcp_subflow_tcp_sock(subflow); +} + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(bpf_mptcp_common_kfunc_ids) +BTF_ID_FLAGS(func, bpf_mptcp_sk) +BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx) +BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock) +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_UNSPEC, + &bpf_mptcp_common_kfunc_set); + + return ret; } late_initcall(bpf_mptcp_kfunc_init); From patchwork Fri Nov 8 15:52:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthieu Baerts (NGI0)" X-Patchwork-Id: 13868404 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 B1DAC1F1309; Fri, 8 Nov 2024 15:52: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=1731081171; cv=none; b=YsldXK0y5lubwKrOzXW5vMHySTSmlPtmyhDQxZRtpHaAlp/mtWgWWH+eAlfiuVKRzzfDwtmEvujgSBIwYLinOVzvnDob/cfgxeBl5CIn6PAHzc503BhpXZTWYZ156J29gI3cOxskEQ38H1fs93GXMRUsUmHGIuDZt3u+hjavpD4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731081171; c=relaxed/simple; bh=5YOsiNLttKWq/Ld5/UcsEwz3pOgckJnPsrplRWCy/8I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nUlflTQcujVrvF0Cp5MwCO7m0T7NJKCNk4Vm4fk6qVDx9AK47l68EpnouUe//kb/h5E/j8b9+MRaspq4je8V3HJmDGuJGOyrYL7AxEI87sRWpgOjHX0LY/pHbDxCBGfP8KqRYlYhzhlb53j7/EbLwbpbof//pzS8eC7WgIGZHdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rPWC2ITA; 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="rPWC2ITA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0E12C4CECF; Fri, 8 Nov 2024 15:52:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731081171; bh=5YOsiNLttKWq/Ld5/UcsEwz3pOgckJnPsrplRWCy/8I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rPWC2ITAror8Lf7cW4cwwjzc0+hKHbNUU5OoXRGWjq4gkn5PS6QcflF53Mn7rFBY1 k4PSz+Mhmaj+0OqnhW0eB0kov8s/8jo3tWCysXk/PZuk3LpqMaKoSJHtMxCaE67IhD +xpxtdHeIArCMNdobuQI/iCNVBc7bC3D+OiVBH/cVczFJsoPkxKJ/LoO/JUlUPx5RX eejCXBezecJNxZUGtcEy2etXOe2KHJY/UjwDHAR5VMqV6mBrac3JQbBBXDa9/yPyXH EWA9KG1q/FiqgGEpxBTwldvgtCp5kMEzFT6OguA6JQDhdV8tf+zNl9I69d/XNvJL5r yaabMG3r4LtSw== From: "Matthieu Baerts (NGI0)" Date: Fri, 08 Nov 2024 16:52:31 +0100 Subject: [PATCH bpf-next/net 2/5] bpf: Add mptcp_subflow bpf_iter Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-2-cf16953035c1@kernel.org> References: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org> In-Reply-To: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@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=4014; i=matttbe@kernel.org; h=from:subject:message-id; bh=fOkGqZrxlK74TDATl6N8SPLqzPotdmdaTyOO796PIMA=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnLjPBQpqi3TslQuEySuqdIlV0AeBwFGoYyH1An 4OxKAv1vsiJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZy4zwQAKCRD2t4JPQmmg c5aSEADuM56/WyZWe95dcwS5CL/3VBCSv+tlShGKbXwQX853Citmxfz+8r8m5YketqCZY745akl 8CSB18sWzc7DW0QqBHD3zT8n9hwPelASdxVfxwuLMuWExvwL9G0tVdGqMSNEJ/NeO+KX+GM/F9x npMhMjphvz1cggLIl1c3TpXOAtygR1ScmHapKg7Kv/rzWajCHWAwnU9oY/DxJa/jxKBdjHPjMYU GAdbiTPav5MEwkFSrSn0KoGaaV5ksi9XpJA+8tv7ys6CKq3kBG6X8+AkSntMyUQCEe3MdnhJxe/ Km8n1ukCrqt+R1wijKcXsyydNPL9QAJKy5HJ3cbLi/pnZUCnaO/u/OtFjril8bNRR4LEwCGhyrc i0RV764XBJgDLNeNGTj9inPaT/5OpXcTfufSxmVPvD8GwbwnSJ7Rx8MF1apViHKPfWzUtKAoJLz vIpGz8CgW6UFFjEd7RuV/9dbARWJNYfGByzwj+f1LnC5HpkVMKrmP4ADs9ZZ5ZOmi/DYyzDzXcZ pnEUfQnZKJtjwx4tryCPaDOUkqWOU7Nn3MaJVxMaQ4+9YCie/UWDDQQdSGuL/NN/yey6tdKNdvJ t49yAZvD+dCDFpZQZY1UO+qkGvYvL0Ta/e3ccka6tTfkBsJUI5ElW+KwGg06UVS9nU8D/cqwq4g XSR/KywZ5cGxW9A== 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: 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 this patch here: - 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 | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index 6f96a5927fd371f8ea92cbf96c875edef9272b98..d107c2865e97e6ccffb9e0720dfbbd232b63a3b8 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -29,6 +29,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_sock *bpf_mptcp_sk(struct sock *sk) @@ -48,12 +57,48 @@ bpf_mptcp_subflow_tcp_sock(const struct mptcp_subflow_context *subflow) return mptcp_subflow_tcp_sock(subflow); } +__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; + + kit->msk = msk; + if (!msk) + return -EINVAL; + + msk_owned_by_me(msk); + + 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_sk) BTF_ID_FLAGS(func, bpf_mptcp_subflow_ctx) BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock) +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 Fri Nov 8 15:52:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthieu Baerts (NGI0)" X-Patchwork-Id: 13868405 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 76FC121B452; Fri, 8 Nov 2024 15:52:57 +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=1731081177; cv=none; b=DqQcU3ksKdiZYgDN7bMoT0FGbqu6+if/PybSCqvaU4WXng8sckzNdVsujzdYXN4xgsFubT9Xc86PnoYRRWThd+KZDugg5TGc06YjWNPJYIkM4irlIB8vGDANdDs5FWEZUgkOuh1h/J+cMo8P9xDW3TI23hp8VSWbEOPJfoJKfco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731081177; c=relaxed/simple; bh=RF/R77yecZxwwcV/SQ7kt2sEMgzQozbeyKh2oAd6g0E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=eBEzRf+MasinqUxz5DE27GAoMuzWhmXgjRGuMq2CyDPOnqRcmCxbw9CAPKBmSfexjZeejkvPGkJgdwJPfkF/dRHOZ6mPPNzMa0SH+KyXwfSWuhkxeSZzz5BESKflYLsNyrAC7O//62i25PglItpE9gEWkPHmH+aeXif+LJEe+RU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kAuz11Lm; 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="kAuz11Lm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C2C8CC4CED8; Fri, 8 Nov 2024 15:52:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731081177; bh=RF/R77yecZxwwcV/SQ7kt2sEMgzQozbeyKh2oAd6g0E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kAuz11LmrgIiiq2DyAQGkw9EDrvBnoVrf7/LT+Cev/e3XtRLrB81mDJr3IfIORgRW I/psYJeBkan5oZ4+hfYdQbmpKJzeKddAYq5kae1mjy6R/OTALQcqE0gvAUfWCL7pAT bKU2DlMnF1m0KKzNyKPIcXWl9TpY9pLB2MMwiiuh5LW4E4ZfkA2aEduGd8V+NCYh6z 66ND4g7Ym0QFDu5hg+VcTGXq+56bFbR/IhhQMNinYP464LjOHMZFeHIPpF9JR3GU8C 7hpc0Dctpe/9A08T2bXVPVnuA2XKTIAZhs+G6yIzPZhnwhHKYBRFyAI0K6AGdYu2R9 dtCxfVK9EOL8g== From: "Matthieu Baerts (NGI0)" Date: Fri, 08 Nov 2024 16:52:32 +0100 Subject: [PATCH bpf-next/net 3/5] bpf: Acquire and release mptcp socket Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-3-cf16953035c1@kernel.org> References: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org> In-Reply-To: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@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=1993; i=matttbe@kernel.org; h=from:subject:message-id; bh=Mon90wYreVBu0zG7BWuM4v5dwTWYbUA1gVh0OSEBwMA=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnLjPB9y+Z3Spcbu/1rD0z32/UJ9iKr+fZG4QEk 0AI14lhVoqJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZy4zwQAKCRD2t4JPQmmg c5HeEAC+0D3ACboydhZuejXcW7ZiBI+jPyP8G7/xeVODm3EGzVZhE9jBIhgBnXmkk8H9UfeLu1S /964kUUN8ZftSBqM2fi8Nc8nE07AUdR5BPzh7oaq3YnVfeVtCMn58te7u3piYYlv0Zd3unwdfL7 NuOf+wOqSNv2V7eplnsyQiltUK9qcGB1StyqYpREAPAW2xmn0nNkpfbS7wp1VlkNqDy+uOboeva dNNw3xcyNaFmiHcT6PaOtb6asHZgtVfXwReLwnKS+rAEt+LDScBd11D6NwWr7UcPcwvlDBeDiIm ArC9udmdd9gpP4PZGs0nPs5wpJC10O/RrkZzSZemm5eVJfzCw8rnATiscWdPq8KsOAC49E745UM j4ghxzaitE6Ccbf7NiJLX6CQDbxAQeGsg5xTYivNWvF1WQ2xuENmv9IYfo/dE3io+slE8wBOcnp bq0BRV+Tyjgm0EvHAUp01u9y9XeFQichX/AOzV7NG8E0ASmffXEFIMSXu6zUOoOk66F5/djWpQj QV6QDOnAwQ/9kxE78zevW4QFMZx5Z837bZduGnUxZPlhgVbFRkP5RrEMAjhd0nfgBrAGadN+XxT gxaDBsMJJsC1lZcancObtjdN0cTF7dWJh74QmwGez+vBYrPh/R7LmZe6E7+bYjJydpvHdAIKYjl zmrnclEMWYUuBDQ== 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 d107c2865e97e6ccffb9e0720dfbbd232b63a3b8..5bd04548e846b4dc120dbc83725a604821fac772 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -90,6 +90,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) @@ -99,6 +116,8 @@ BTF_ID_FLAGS(func, bpf_mptcp_subflow_tcp_sock) 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 Fri Nov 8 15:52:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthieu Baerts (NGI0)" X-Patchwork-Id: 13868406 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 36B7221B452; Fri, 8 Nov 2024 15:53:02 +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=1731081183; cv=none; b=N/k/mBRcT2a18gbwyRAO9bzErmouimwethxE6EtesrB/y+eIMUwNZtH5dzBuuBr19vfSWbJgJGfZ+bgtiXABIQSR2AG1V0Mj/tueur5MsYEi8LvRGIpeZtqO59S2aYdvsSSYJKqdpM9Qy3Od4ElJ2U8UTJ9QfIgCLfuauhf9vbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731081183; c=relaxed/simple; bh=T33OFv4370vAaOBtPgJ8KJyDUTXlLxZgxMPvD468RKY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AHrkb4+Cldcd8vPcQoTpTcoE6qyGG/+26XnuvXEE6cctsJOyzks2u2slKgFmey0/7x13LoOkEqJNm3FfT/jeZFQDGcSsP5JRAGAZGaddA1/2u6jR6SoiI8+dUfcKTSPac1g07QV551B8nKV71H/Y90gIHe8wCKDevsiMzBUjVGI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DMUe2XF1; 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="DMUe2XF1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 73B04C4CED6; Fri, 8 Nov 2024 15:52:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731081182; bh=T33OFv4370vAaOBtPgJ8KJyDUTXlLxZgxMPvD468RKY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DMUe2XF1xBJ73XtdgruNL6Es58ahgFzrmDkwLrNUitsnhk+aIfyVq9hSJOBhIpGPp thbriNThS9oAXAbIIBotHWui41o/evDPEJi64GY8TqDk63FTKvV2FqDJCJJks3mFHB w92wnQohc/QqMC+wqMoBL4VnkCx0D6u4VOgGXjKcnQ4kVaZjz+1C9ERqZYsqCHvDA5 M1ZPVQ+iuXUxU0TFiBHRcR3lWI6isl3KDpLb2BG8u4dzgQ39ZqDvBl8Z3W10OvDwe1 sdR+Za4bwZDPtAYdqCVPhmnZiWwoSsKrr75v2fq82K9/3CJCGaxZT9QiS+cuctgXZo uT9irF16xpr3g== From: "Matthieu Baerts (NGI0)" Date: Fri, 08 Nov 2024 16:52:33 +0100 Subject: [PATCH bpf-next/net 4/5] selftests/bpf: More endpoints for endpoint_init Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-4-cf16953035c1@kernel.org> References: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org> In-Reply-To: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@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=Ru7NMIs9pH0EG9p8nIRT2i0k1cui542WqTRHPW1NGDc=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnLjPBvLS1F2fJL3rg1jMPPpVugTP/xmjodgM2B fsMevKBQgSJAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZy4zwQAKCRD2t4JPQmmg c34QD/90MAx9oKVjYEseuW9+lvXN9lUei6F7nNez0EUaOrADDt/rUCbrFxxuDkDHQamwOti1KLS e0batHCI9orTNcTFmoFtXgqw8Od2B8O8R8svJb/FA8NMtxu7yszkmdUJtSvmKZNC8EvjCvtCj9v TtcmNlXP0BEmifA2v7caGOlgeNGguHcrXlBAGBiPDpRAd5NZFlJ/yz7EEys8lC9PmRrr4e+XaVc wj7/p7EoP+oqeCVWqMvyi2R+LKsCZCd5ba7/5/meW6ypNVmUuIHrdcRVU1BxWywyu6nFNcJiW8a GyoQLgrH0DnRdSTADG/eloJTl6EsmOqYleVsxx3rqe1Hy8Et59GxjtHgLw7wgqFBIyIKLSQ98lb 7fIzx5hxvot83M2sFVdGAi+90VZAe8XZyPThNofWOow7HmK3L2jZNG4COHaRAbt2mQAY/WtCcyD jqiRi+s0Rf6DP3Cc8D+MjWlN/K5NswptwuIZ9lIo/kJ9DLwpHNM4BiyM5PWS73Jtu0t8PEx5CB1 ev7d3FoyhPhQoQxOyxpyem4qaO5zxiR5nq1Jl7dexZ2I89RZWyQe4INP+41bMrta9uO7f1kXs2n J46dC/QXTsltKSXC6vVxX0kd45YnCSnkoSs0XpCLDgBxIIVoEmcX6CdWLseq4BCSrs244nBHozO OSZayhBkDPCPR8Q== 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 Fri Nov 8 15:52:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Matthieu Baerts (NGI0)" X-Patchwork-Id: 13868407 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 B919821B436; Fri, 8 Nov 2024 15:53:08 +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=1731081188; cv=none; b=FkdDVFLMv0/fgspzBEHZP2xbVKOjxMJiuBulkc6XEBSYy6JOWfKZ5b/dvyXsJ4I7l3BngD+J8QxMkZkN+0rjyX9ADvsf4m09A6P4v77chU7RXWx9Ct5QF4xn+HFUkQ2pv3ykY+j0K5b94MAON7VhVRMeFqTz5XV33JtqbGX526U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731081188; c=relaxed/simple; bh=W0Effr/onGCmHz7sYncRbxQN685XfrSEMY/y4+G3jgs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=klMKXcsXsaY4UyfuPmlcd02L5XZrsAjDvQnErW8MqTEtqULxxga/XPRLql8mLwWOWwoXJ2e7hepk/Tt+/Wvpf2s7JTB0A+Z1x8lM6r47ORHFWRlOWyWxt0SGwIA1axP1iSrfYX9ZKzsg8RUCXVFg1VA4fjKKV0jlC35Mb9Andic= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=i8vUe+RV; 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="i8vUe+RV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27DD9C4CECD; Fri, 8 Nov 2024 15:53:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731081188; bh=W0Effr/onGCmHz7sYncRbxQN685XfrSEMY/y4+G3jgs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=i8vUe+RVl8IsE8H+aGI55gjOHVCBFnMOLrhwJytBeH9djaAhyKRSgFoRdiDXs6Cfc a8UEJ4x4X9NEBs6Q9nsmaA/kzIgdxVdOc6sOVpufVqJBH610EeyEthztb3WA8JME6S BjB7Nry8t+/Bu7To81T8qZpsNZZDSizx2/4t0zJBd7YSMzMhHtjR9KsFEDNaoA/huM 8vFGNenT92ydEMh004mQBoh7fXmOlVnsW71osyEb2Q0F+2yfQgfpsTmEzmdkcCVSVc JYUpm7Qpg8MY0NnBH1yYIGIKIR2mvubXK6IdrzhiXZNopcdA0+q/H1oTAQ1bAytrV6 3SBGrbU1bqj2A== From: "Matthieu Baerts (NGI0)" Date: Fri, 08 Nov 2024 16:52:34 +0100 Subject: [PATCH bpf-next/net 5/5] selftests/bpf: Add mptcp_subflow bpf_iter subtest Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-5-cf16953035c1@kernel.org> References: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@kernel.org> In-Reply-To: <20241108-bpf-next-net-mptcp-bpf_iter-subflows-v1-0-cf16953035c1@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=9072; i=matttbe@kernel.org; h=from:subject:message-id; bh=mok1cxbzysrOdoG47edT0d+OIHVvidumZlEd95OpRYQ=; b=owEBbQKS/ZANAwAIAfa3gk9CaaBzAcsmYgBnLjPBcDrOQ9elK2yafy+lKt/sxbGgIBGgBlkvw LCmWeYF8o6JAjMEAAEIAB0WIQToy4X3aHcFem4n93r2t4JPQmmgcwUCZy4zwQAKCRD2t4JPQmmg cyLhD/9oP7JFm0HjLF8v4CXvtEwGtwj+DbN6rwHJSHlbPc35ihBpcb4nzden0Y4dd+DfQjiIKRS e8IRxgw8MEGfY7QvZhnWkLazyqa4UpnFJY0j9pEQ7NKsGQ2ejvTN3ZzpFhFFuIDbXeRwWBsE3Li qe/8x9XUv6vz876Oyu0uYf09quy/cBpapjX5LhX6hv/TrELN906wDvg9jAde2oz0K/SNL5WqfAV RbSNahuyMtpvjObxbp97n0jW7MlPvrhPZJMcfvVFycKX2hyStOVBhlzh1ymG9gDCDS2RlLOY0mB 8adrbwe0rkkIydR0H6d9YGu9/LxmRg68i+lK0s5fhZSw68eEKH8dOCAyQeVWbKhYmtiBc+pmUSl 1DFoTvUUABsRv5qDR7ph/kfBZMijTHpnebeJAQkgnW7QwYF/PHDRFQHzNcMfvDm8s2HGk3wRhe1 xxw3/iHsGYH4pfjex2faZDOkTijk0mbNy0ZGrdwVikktEewV0YcKfupGbSFnyW+3wyWxk5dyZpq kYwzA8TNGhI/UGZeZ0uVcZnddGWZ0WptlKUxEAzVFtf4YM+UhMHkmjB00tzoHgbwo0QQw8f42CC XovP4HDz5aOH9K6d41YZbmqFeo/FCQstT1Yd+DUAlfLwzbPUvGPno6Ho3cn8jPVBhQes7j37JD2 nehkWKoeQXNGpXg== 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 the kfunc bpf_mptcp_subflow_tcp_sock() in the loop to pick a subsocket. Out of the loop, use bpf_mptcp_subflow_ctx() to get the subflow context of the picked subsocket 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) --- 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 | 10 +++ .../testing/selftests/bpf/progs/mptcp_bpf_iters.c | 64 +++++++++++++++++++ 4 files changed, 155 insertions(+) diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h index b0668f29f7b394eb5294b6c9cade28fc1b17265a..08eaa431aafd758117f8e818410c4a3e7fd0b70c 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..3210736b1c349a989c5fb5c16dc21d03d6f9b5bf 100644 --- a/tools/testing/selftests/bpf/progs/mptcp_bpf.h +++ b/tools/testing/selftests/bpf/progs/mptcp_bpf.h @@ -39,4 +39,14 @@ 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_sock *bpf_mptcp_sk(struct sock *sk) __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..1bede22a7e3d125ec357fb798059e7bbcfe0d227 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/mptcp_bpf_iters.c @@ -0,0 +1,64 @@ +// 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 (!sk || sk->protocol != IPPROTO_MPTCP || + ctx->level != SOL_TCP || ctx->optname != TCP_IS_MPTCP) + return 1; + + msk = bpf_mptcp_sk((struct sock *)sk); + if (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 = bpf_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->token != msk->token) + goto out; + + ids = local_ids; + +out: + bpf_mptcp_sock_release(msk); + return 1; +}