From patchwork Wed Jul 10 18:40:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729573 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011079.outbound.protection.outlook.com [52.103.32.79]) (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 58F6119754D; Wed, 10 Jul 2024 18:42:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636932; cv=fail; b=T8QhO70qcPh5KHSPxsZMJCxVTACCGJ1qvsprgciSZk3+7skpIT1jeuQE7FuACZ55eLoF3TdD5hxHAm++ouAz5HKl1nckMFr4v3qTvCBEVGJO0lrARjw1efaLHYgjsbOFPrVZnNO6MvYk/2pMcOcN5W5VlN2qIxv+LshRi44NQzY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636932; c=relaxed/simple; bh=SWCAw0vxxBbwypSNpxzrPoVaZe25u4XvQL4Z9MsWc4c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=DZ2FdC+axwNejNkb+HT0kXf7CsGsSCdRzjn1oRePh1B1A3ObFSll52x1ixA1MplH8EI4Pzlocaajh+bulvzAcln6491VdBZ/LRS4snd8ZddRkDKAC2DWYEb6dliL937YYzaovGjAbm5gZ3srlMoCrwF58CQxkGzlHeAsbZ7p1NA= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=Lo+A9YNL; arc=fail smtp.client-ip=52.103.32.79 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="Lo+A9YNL" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ie7gFM3l8WmO2FkTSHx8w1QrXJa5GaoEkImPQE78kmKRc2AUF9hwbNTLOoXS1Dysj8W03QQhGam9m5fKOaTu8iT73F2/hJeepUKlqQ0HYYBlhDbiOSkqY+LFkLxJnaFcy9PtPF7Cq6zL1q6uhMZoMVQU/nGtSpz/CC1HjOdnOX0jKqYmHlXaeQSPKTghRQHpZ95CaUSLKc2tvedn/ZA2RQZUQJIoGRo57DpQskTLT6i4JkdHUX25ITq2L49BL/4y+Kj3mqpatVZ1eIiRB/rsoqJPecVBMzzYshPN6JbJuTiItn4fdChR0bKTTTWBnPEcNbufWptLbYrBVvGQs+8WNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hUTTySXX7ENbwFzAPBR/n6DwWWoqRqJO0M+cOB8m4iU=; b=AvT0zxWn+BfU5UDiR59/KPkwXlteAOChnzkk6O4BVKWhS+HKPWs5Np95E9eAiBdxED4PIgABlgzZQvdAfZportcSbsS7lTToeQCUdblaWWLNegnST1exF/0q1CGuSYRhrds03Rr6C8ozAaJ/HnlxAUuLdUOPO6Y7kChlL8C7GeghS0eEwc2pIIhipfmUtKP8vS+TdyZuYDYXj+IHfEsRBgmUJeaQHNIWLH1QkhjiCFwwaDWz03iSolZU79y/cNHZ4QyTgYIbKH+IQG6u9K1n2NiiD/T1pKfOna9dy2kR+vUY/omRBz+976IDpDTwB9ZHAJ4/GfJSwIC1JwgayZ3AXA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hUTTySXX7ENbwFzAPBR/n6DwWWoqRqJO0M+cOB8m4iU=; b=Lo+A9YNLAHJu96vXRMhatWoSEuja1nkiOxGd9o9fD0V4X6uHYksNMrJ7L+FA8XopgS9ZqD+vk6fWj7gcxJPJVZQEwM3O5G4s2vvQZS8sjZdAkioexKCPgeIsTRN/JfpvljaWMwOEjA0k53/3t16WV7S0AOogRCi9KJcg2JSYVac6mPbWEs2JSEzFcZ9T+k/Ar0XIRPxr2vrLmdmjFVRbLqDpjb7ePvt+tvKxY66dVSAAPZiONLR/XLGjGY/14Bue6o/uA7Yqv8MQpK+JAvlNj4LcsOgYnxSHNDtdpLwH/PIRYH1G/ztxk4IQy9PQqaPz2h2C73eprD8kC7qHXUOLXA== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:42:07 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:42:07 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 01/16] bpf: Introduce BPF_PROG_TYPE_CRIB Date: Wed, 10 Jul 2024 19:40:45 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [ZW/My4h02Zdb1QbAqkGZIUbHmc3EP8tY] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-2-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: c715ea73-2521-45f8-81f8-08dca10fffdf X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: CjExa8k0E93/2I25U0gz9kQkeEXUcciw9oEiRRFmiU+HBmrsMr0KHFYBqPMQnr6WBjc+CmcubLMqJ8Hg0rk9O+cjLvrfWNiCvmDYXMCDQl0hiany0iaL94uXM4xRCenRxh2KaBo3mxMKMEv/5jx/Ni6mM2St1H2oWcKWXGwj437RhnI+cz7LqYTppuEjfVHYNT6RVcIfmjEaJRjftXEiBcSELZjBDlvLHuaI22OJR6oWgAQtNtxw1LgCvBeVSCjme3sHVz/Ge1o7GjOPLRmaIl3ee0Oh7ySa/OYQNylPYNBBXjFR3kinORLAHlhAkhwqMilp0CJs4TvtoqwlRXCsab3wFkizRUicui/zOPtvfbMhcS5c5dtNy3U5439cXtJRJLISDU2U+Q1SCTe8UxgszlJhiNRtSJ7otPUPKUTAR1aXvqyZTRIBBA9pvdk73sgX+Z9guSgg9nijBLLfrMskm36alOmHZ65vr7L2cRkMt3SB0gJhE0Qe+YyNR9eiS9E05c6qSYi0LKyCtmGWSRsONp2RUOPc9omaa1ZCfWnTO8+VILU+a7dsHBSJmKIiTTYTJzledpXj4mMduMNthqG/Ufm+zh8TQkq7MKyWiIlhaMJEGCKA2bx4NZCwq4UgyNhsqCQM7tV/JpkXTQOqODA/Nw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7rxffpPUcVKmsXo3xM0CBvYoEUFvYDm6/pk9l6/hiGZ9HO+FbMQLljhQxwB976NTQr7xT+mlxk77fcR8rg93nxPZCHzpYvJQzziIjUWmS4wuu6jJXpqiic8/zx14/welmu1qVol2FpIUydPYH0Pit+AQYVrNgOdqm49ThFOx/SozzTfBYRQtzqVyt+TtlbyBcEkeRqHRWo5Sb3+1jW/EPBQXlJH7ebEH2y1sXqHBFpPNutKe0hS0aSyvMATOYRqsa+piYT2cifWXiwLeV6Jxg5/JA/gaWGK2nt66xwCxws/CAO5rqOGBFw+zJOxP1x/ik27m2l7TRi6BciP4lmFABi5sPc/rLSfFVg68AySPtMUngNLlr+urBmH8tVSOUtr4zWuoUakABjSpooV1e+a993QvI1PewEts+09CmtRzqOw5ZHCIYGl5PDKXQVXY8DGxbn/ZiUypJI0taLmg3wKPwttdcgh40j8Z4Bu78+S+s4RgDam5HJzntVEV157Vd+LrFvHkH2TiCewjd+NaOjiFqnA02TAArlQh2uCUBhbI2eT98TC8ObLWOgFdGg167GAXQ4b+ssl88VY4U2Fw4K3fLU5oafvv5iVjgjvkNq3DeuO/TFj725I/zQUpLQrbpH1gOC1JQW2Vw02HLdW/gcTu9/MLenggzNkGG74H8Vb/N4Y8jgodPE8OhFKBDgtojK/5SlYX+4ts/hW+17uGqPAZKbUg/4U419Fo6PsDwGbK8q15zGSNVXDleMD0d2MGHEfdHpnzSmr4WbmythHGmHfudmrbkPG3uim5lT3WorfRnNDg1um88TonKqj0FUjDr20Wru2PuFN8z73O4X4sAfpvc7CND7ZEHqFj5Na5sVaanwTutefn3HB2AU9q5EER1Ybl6xRgQQBGEAN/bUBx5xuN6qXSaHF27FmmxNoo677kYCFw1Sh1sK7P2woY60ufioICYrL2dtY+DdwRb7/mhHyiO0CiDCA/etGLxxKcK3tkQDwEf4HrdQPwvnTDChKrtcbzVy8FsFbA6fc28YYAV1bhHVQ0wsBWab3N8rKw9YxlNwOcHvl+IxNpT9+AncBgitdi0UwqJklTAEp57/vYLwXsVPkYxC8wU3e8o4hhvYc2e1ebPJeD/V8Ug+AsZHa5mAZXahv3THanyN3D6j6brpVaFbIdkLb4f20HDwqR/RWO7+vRMXF8w8RY2go0dIsJ1G3JAYNs+Lapbww46PlqFFLowytVMOIJ16+8Q5QgIT0sxiU= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: c715ea73-2521-45f8-81f8-08dca10fffdf X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:42:07.1591 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds a new BPF program type CRIB (Checkpoint/Restore In eBPF) for checkpointing/restoring processes through eBPF. CRIB BPF programs are not attached to any hooks, run through BPF_PROG_RUN, and are called by userspace programs as eBPF APIs for dumping/restoring process information. CRIB BPF programs dump/restore process information through CRIB kfunc APIs. Signed-off-by: Juntong Deng --- include/linux/bpf_crib.h | 16 +++++ include/linux/bpf_types.h | 4 ++ include/uapi/linux/bpf.h | 1 + kernel/bpf/Kconfig | 2 + kernel/bpf/Makefile | 2 + kernel/bpf/btf.c | 4 ++ kernel/bpf/crib/Kconfig | 14 ++++ kernel/bpf/crib/Makefile | 3 + kernel/bpf/crib/bpf_checkpoint.c | 13 ++++ kernel/bpf/crib/bpf_crib.c | 109 +++++++++++++++++++++++++++++++ kernel/bpf/crib/bpf_restore.c | 13 ++++ kernel/bpf/helpers.c | 1 + kernel/bpf/syscall.c | 1 + tools/include/uapi/linux/bpf.h | 1 + tools/lib/bpf/libbpf.c | 2 + tools/lib/bpf/libbpf_probes.c | 1 + 16 files changed, 187 insertions(+) create mode 100644 include/linux/bpf_crib.h create mode 100644 kernel/bpf/crib/Kconfig create mode 100644 kernel/bpf/crib/Makefile create mode 100644 kernel/bpf/crib/bpf_checkpoint.c create mode 100644 kernel/bpf/crib/bpf_crib.c create mode 100644 kernel/bpf/crib/bpf_restore.c diff --git a/include/linux/bpf_crib.h b/include/linux/bpf_crib.h new file mode 100644 index 000000000000..f667b740fcc2 --- /dev/null +++ b/include/linux/bpf_crib.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Checkpoint/Restore In eBPF (CRIB) + * + * Author: + * Juntong Deng + */ +#ifndef _BPF_CRIB_H +#define _BPF_CRIB_H + +#include +#include +#include +#include + +#endif /* _BPF_CRIB_H */ diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h index 9f2a6b83b49e..a6feddfd17e2 100644 --- a/include/linux/bpf_types.h +++ b/include/linux/bpf_types.h @@ -83,6 +83,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_SYSCALL, bpf_syscall, BPF_PROG_TYPE(BPF_PROG_TYPE_NETFILTER, netfilter, struct bpf_nf_ctx, struct bpf_nf_ctx) #endif +#ifdef CONFIG_BPF_CRIB +BPF_PROG_TYPE(BPF_PROG_TYPE_CRIB, bpf_crib, + void *, void *) +#endif /* CONFIG_BPF_CRIB */ BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_PERCPU_ARRAY, percpu_array_map_ops) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 35bcf52dbc65..cb67a9cad8c6 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1055,6 +1055,7 @@ enum bpf_prog_type { BPF_PROG_TYPE_SK_LOOKUP, BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */ BPF_PROG_TYPE_NETFILTER, + BPF_PROG_TYPE_CRIB, __MAX_BPF_PROG_TYPE }; diff --git a/kernel/bpf/Kconfig b/kernel/bpf/Kconfig index 17067dcb4386..a129677a03e3 100644 --- a/kernel/bpf/Kconfig +++ b/kernel/bpf/Kconfig @@ -101,4 +101,6 @@ config BPF_LSM If you are unsure how to answer this question, answer N. +source "kernel/bpf/crib/Kconfig" + endmenu # "BPF subsystem" diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 0291eef9ce92..8c350d159d3c 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -58,3 +58,5 @@ vpath %.c $(srctree)/kernel/bpf:$(srctree)/tools/lib/bpf $(obj)/%.o: %.c FORCE $(call if_changed_rule,cc_o_c) + +obj-$(CONFIG_BPF_CRIB) += crib/ diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 4ff11779699e..306349ee3d6a 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -219,6 +219,7 @@ enum btf_kfunc_hook { BTF_KFUNC_HOOK_LWT, BTF_KFUNC_HOOK_NETFILTER, BTF_KFUNC_HOOK_KPROBE, + BTF_KFUNC_HOOK_CRIB, BTF_KFUNC_HOOK_MAX, }; @@ -6037,6 +6038,7 @@ static int btf_validate_prog_ctx_type(struct bpf_verifier_log *log, const struct case BPF_PROG_TYPE_TRACEPOINT: case BPF_PROG_TYPE_SYSCALL: case BPF_PROG_TYPE_EXT: + case BPF_PROG_TYPE_CRIB: return 0; /* anything goes */ default: break; @@ -8326,6 +8328,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type) return BTF_KFUNC_HOOK_NETFILTER; case BPF_PROG_TYPE_KPROBE: return BTF_KFUNC_HOOK_KPROBE; + case BPF_PROG_TYPE_CRIB: + return BTF_KFUNC_HOOK_CRIB; default: return BTF_KFUNC_HOOK_MAX; } diff --git a/kernel/bpf/crib/Kconfig b/kernel/bpf/crib/Kconfig new file mode 100644 index 000000000000..346304f65db6 --- /dev/null +++ b/kernel/bpf/crib/Kconfig @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config BPF_CRIB + bool "Checkpoint/Restore In eBPF (CRIB)" + depends on BPF_SYSCALL + depends on BPF_JIT + depends on DEBUG_INFO_BTF + help + Enable CRIB (Checkpoint/Restore In eBPF), which allows + checkpointing/restoring of processes through BPF programs. + + Compared to procfs and system call interfaces, CRIB achieves + higher performance and supports dumping/restoring more + comprehensive process status information. diff --git a/kernel/bpf/crib/Makefile b/kernel/bpf/crib/Makefile new file mode 100644 index 000000000000..abd43c76140b --- /dev/null +++ b/kernel/bpf/crib/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_BPF_CRIB) += bpf_crib.o bpf_checkpoint.o bpf_restore.o diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c new file mode 100644 index 000000000000..efaca6bcdfe4 --- /dev/null +++ b/kernel/bpf/crib/bpf_checkpoint.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Checkpoint/Restore In eBPF (CRIB): Checkpoint + * + * Author: + * Juntong Deng + */ + +#include + +__bpf_kfunc_start_defs(); + +__bpf_kfunc_end_defs(); diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c new file mode 100644 index 000000000000..9ef2d61955bf --- /dev/null +++ b/kernel/bpf/crib/bpf_crib.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Checkpoint/Restore In eBPF (CRIB): Common + * + * Author: + * Juntong Deng + */ + +#include +#include + +__bpf_kfunc_start_defs(); + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(bpf_crib_kfuncs) + +BTF_KFUNCS_END(bpf_crib_kfuncs) + +static int bpf_prog_run_crib(struct bpf_prog *prog, + const union bpf_attr *kattr, + union bpf_attr __user *uattr) +{ + void __user *ctx_in = u64_to_user_ptr(kattr->test.ctx_in); + __u32 ctx_size_in = kattr->test.ctx_size_in; + void *ctx = NULL; + u32 retval; + int err = 0; + + /* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ + if (kattr->test.data_in || kattr->test.data_out || + kattr->test.ctx_out || kattr->test.duration || + kattr->test.repeat || kattr->test.flags || + kattr->test.batch_size) + return -EINVAL; + + if (ctx_size_in < prog->aux->max_ctx_offset || + ctx_size_in > U16_MAX) + return -EINVAL; + + if (ctx_size_in) { + ctx = memdup_user(ctx_in, ctx_size_in); + if (IS_ERR(ctx)) + return PTR_ERR(ctx); + } + + rcu_read_lock_trace(); + retval = bpf_prog_run_pin_on_cpu(prog, ctx); + rcu_read_unlock_trace(); + + if (copy_to_user(&uattr->test.retval, &retval, sizeof(u32))) { + err = -EFAULT; + goto out; + } +out: + if (ctx_size_in) + kfree(ctx); + + return err; +} + +static const struct bpf_func_proto * +bpf_crib_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) +{ + switch (func_id) { + default: + return bpf_base_func_proto(func_id, prog); + } +} + +static bool bpf_crib_is_valid_access(int off, int size, + enum bpf_access_type type, + const struct bpf_prog *prog, + struct bpf_insn_access_aux *info) +{ + /* + * Changing the context is not allowed, and all dumped data + * is returned to userspace via ringbuf. + */ + if (type != BPF_READ) + return false; + if (off < 0 || off >= U16_MAX) + return false; + if (off % size != 0) + return false; + + return true; +} + +const struct bpf_prog_ops bpf_crib_prog_ops = { + .test_run = bpf_prog_run_crib, +}; + +const struct bpf_verifier_ops bpf_crib_verifier_ops = { + .get_func_proto = bpf_crib_func_proto, + .is_valid_access = bpf_crib_is_valid_access, +}; + +static const struct btf_kfunc_id_set bpf_crib_kfunc_set = { + .owner = THIS_MODULE, + .set = &bpf_crib_kfuncs, +}; + +static int __init bpf_crib_init(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_CRIB, &bpf_crib_kfunc_set); +} + +late_initcall(bpf_crib_init); diff --git a/kernel/bpf/crib/bpf_restore.c b/kernel/bpf/crib/bpf_restore.c new file mode 100644 index 000000000000..6bbb4b01e34b --- /dev/null +++ b/kernel/bpf/crib/bpf_restore.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Checkpoint/Restore In eBPF (CRIB): Restore + * + * Author: + * Juntong Deng + */ + +#include + +__bpf_kfunc_start_defs(); + +__bpf_kfunc_end_defs(); diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 5241ba671c5a..bcd3ce9da00c 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2986,6 +2986,7 @@ static int __init kfunc_init(void) ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &generic_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &generic_kfunc_set); ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &generic_kfunc_set); + ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CRIB, &generic_kfunc_set); ret = ret ?: register_btf_id_dtor_kfuncs(generic_dtors, ARRAY_SIZE(generic_dtors), THIS_MODULE); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 0719192a3482..faf99e53d706 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2633,6 +2633,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type, return -EINVAL; case BPF_PROG_TYPE_SYSCALL: case BPF_PROG_TYPE_EXT: + case BPF_PROG_TYPE_CRIB: if (expected_attach_type) return -EINVAL; fallthrough; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 35bcf52dbc65..cb67a9cad8c6 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1055,6 +1055,7 @@ enum bpf_prog_type { BPF_PROG_TYPE_SK_LOOKUP, BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */ BPF_PROG_TYPE_NETFILTER, + BPF_PROG_TYPE_CRIB, __MAX_BPF_PROG_TYPE }; diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 30f121754d83..4e1451901b7d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -224,6 +224,7 @@ static const char * const prog_type_name[] = { [BPF_PROG_TYPE_SK_LOOKUP] = "sk_lookup", [BPF_PROG_TYPE_SYSCALL] = "syscall", [BPF_PROG_TYPE_NETFILTER] = "netfilter", + [BPF_PROG_TYPE_CRIB] = "crib", }; static int __base_pr(enum libbpf_print_level level, const char *format, @@ -9449,6 +9450,7 @@ static const struct bpf_sec_def section_defs[] = { SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE), SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE), SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE), + SEC_DEF("crib", CRIB, 0, SEC_NONE), }; int libbpf_register_prog_handler(const char *sec, diff --git a/tools/lib/bpf/libbpf_probes.c b/tools/lib/bpf/libbpf_probes.c index 9dfbe7750f56..2e087280c5f0 100644 --- a/tools/lib/bpf/libbpf_probes.c +++ b/tools/lib/bpf/libbpf_probes.c @@ -180,6 +180,7 @@ static int probe_prog_load(enum bpf_prog_type prog_type, case BPF_PROG_TYPE_SK_REUSEPORT: case BPF_PROG_TYPE_FLOW_DISSECTOR: case BPF_PROG_TYPE_CGROUP_SYSCTL: + case BPF_PROG_TYPE_CRIB: break; case BPF_PROG_TYPE_NETFILTER: opts.expected_attach_type = BPF_NETFILTER; From patchwork Wed Jul 10 18:40:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729574 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011079.outbound.protection.outlook.com [52.103.32.79]) (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 DD83619754D; Wed, 10 Jul 2024 18:42:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.79 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636942; cv=fail; b=OciiEI2fHGaFveU548lq0Indys1awXnISu6v4kh0Xs31bmoRIypjtD4ooe6xXlIntXPQShtoVOZPFuOQpE9dxFD9JFe64w4+xsqRqjokxmhfi0Pg4a7JIB8M3m38pd6A3BoPT99TVDA9dB6KKuePAZ2XKqkGNMrKwMn10JIO1WY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636942; c=relaxed/simple; bh=2VzsNC8RjhBFsaoETzi1HALEC+WpHCOX/RqlEcxIgQQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=LwQGMnHMXX3Pfh+On8lTOOewza92SEjY9NTZ0+XKdDdfs7unHrfFjI8/iR93bowFtY3XNW12wNXIm7grZTVpv2RpIFQAyP/vDaXG7G6oGrbVSCKrhDfwu3Ut12+nBDnvUYcqtCvuYTgk8CgGTA+wZhwSq6VWDc27t901+lJcziM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=ebCR2ZWr; arc=fail smtp.client-ip=52.103.32.79 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="ebCR2ZWr" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nz/XN824Opt/fn2mr+NL6luxvRDrumLMP9YzyixRQr62st/OGvJlXXs5rmTCtFuOwHVppxvzkk0MA4w4Dgpryx8GAUyJS8DGDaC/KZ35DPgO1JRh5oJtbv/F2hvaQ4g269vmn4HmvvHt0SN6paeFhvPI8PqrUA2LUcHkRJao9xwBAwKVVKO9S0XLNcSpNew4yEFwbuOLAL601UeK1Uw7XhRfMgdscYRRCcpDGSRCzsBpGlr0kzmfh2xlpEzJq+eqTtsrcPZTEEs9Af3hKR89PIs6971Mh8Tyigy/D/+hcBJimOM8KFzGBa+C3f3HLU63DnDypjuTvU8pqwNuyAek1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QfFwhFFn2kM4OMK4kA50yiuRJuIhkfv0Ti3LY/dxRIk=; b=F7gfSzlXM5O06DDgOTThR8nMF/JdF/U89s+FVIrehBHhXorA9lkO4VG/sVekbBAaxB4bfXsFMohTYBROQBV/CajRNMQolkP62J44VvZN4WASPD2FZJ/cBtBqd3j3uMZy0/Huvz3KVSOMczX5w9TaCLq7gpLPPRAaU9OlRXWcxBCnp3G/AcIcuHZSQ8h1sGMlu1yYNqFA7P6s0h/pB3STyTciTNXqa4aig6QfoX8cWGbWP8hh/wAVdOtovPDEASDKngyP+2UtguauYVJH4uxOv/ugOmyLwxR8RNikUcxqr/UEG0gxrXsE4IbbmpxA8K1LTG6vs1+3JbtUze9wGIpiRw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QfFwhFFn2kM4OMK4kA50yiuRJuIhkfv0Ti3LY/dxRIk=; b=ebCR2ZWrs/FI6gzOEOnkTLPBcdGRlCwcM1NFf1A5faZPYvMwBeWbP0xaviNaPdaFiOeN1qWO7IXuFwbaRs9o3eo7g9L5Vf+wgHZoXSJKcp2DnJl8lULbMe7n4AejY1qO0hN2RSQuNF558FN2/suxhmV0mM3qxTsjRlvOwFmAQ2rTiDqn3E/c1I1z6l5vqFH3oTXVjTVGi5sjWFwF6DpQNkH20bw9UCc+T2RETzleLlkw9z6trAQobUzqFJzQ1bWe9SzkAgsluOwG5/uCJOm5bxmvlAmebJItYP8hZSTQ6jO+RW/u7esIf94TZKRDZcnTzklLroHSt5zHByTuYc2CAQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:42:17 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:42:17 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 02/16] bpf: Add KF_ITER_GETTER and KF_ITER_SETTER flags Date: Wed, 10 Jul 2024 19:40:46 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [IP88/lLZhQyzCEjrgSlvpEcLTR2RiuZU] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-3-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: 9918cc04-1cdd-4f38-c494-08dca11005cd X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: C0T+6Z35XRWRykX113fZtP2wqKY4DiIk/WKSvNzHFi7e2qckWGZ0oJH4poQ094SQWs4tOkrkvuTG9tleLTMBZ/g9LvBuW/2WU6xC2AA7MmQyVM6q+pXdbX+C52fzgrQsHTSC6C0EP+tDQ3o5PiXiqAWYovnKcXYC+px5O6TkJdoqNV9DZ3SrSDU1lQRdtdxJ5Ek1b+Io7usMlsbxbPE22EBYIjYqC9r2WmsWX3nk1Iu8Bi4Cnw3Pf5gSHqCSRUYY7zdv6U0/zIm4hNOXRFsvmXGz4aeEIUw+aTzUxct5Sq9IrTzswGs32s4QysITDyrBZZr4lXRLs5Nquq3jSSrtpXR94RCV/tzFZNq0wan+Kbn5zpi8v0Byg00zmAQQM2FONyS1chZIhMGBtKSFDHCD5/WZRb8iQB3ryHOeh3W+B96Obs3c0KSL5NrUYafRuBtczXORLmjerzxNj72Ewk2Hor9pOc0t/19eZ3gHwsK5vvylAYm4UgDyMD/6mL5xmCh0ZEgKrHRNl0eVvC3JgoHfeckHRMT5CVY1Ho4n8XFv3WTxbK+NK+nICpsk1c0THcDEZOJ59hEQB28jlsFZemwEQ9oRtU2UasOcBAa8tf6ehs7pGE40+/5uhdbSlwTOu3x2Drui6cQ/x17t0KvcY2Xo8g== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9kcTc7MgQCHhchPsPJEcfVhd47u+JjyNWJHiQGWIVXFjCACmudcb2toOEzC2e85ENAgbUYFugFTUbxkJoSvdyAQXghkw29PwNgDzXK9YOKUBAB0jkPpcqTVsCTpdimlQeD53z7adAMOZe+tPL684GIaVR4vOf2BmNvn3E5fnEpqtuRxv27ycyJqxtM5XcV35IOZCOqdYNKQMnLzQOmoBxGwseAlwgYTADjMfDwLYuVMs8zgYpWEMTp/wPtIY43SJSCBFKyCC4kivBFtlpOq3ayrzkpDCBQI/g6Q3vzQ/apz8YdTK6uHqqrIgdGKFADnZpBGjCd2alVEI22OTd7cz/dfDtSgIFDN1Yd72kZbJeop8Sf5jxUngHGbmIEOtTucB026V5dI3xtWmnRsJS3GE6AGbsvwepqkT9yn5nRXRjPsgGcz2KHc6QDQzQa99PtYCSbQdwSMm2STfSFSZKoWCzPfB/TLujIhwgIZ30o2BeThPBTYCNDYIbjnqSW7VfOPZJebjhqtTCyxbwX87HvXE57Y+7JSVmlHPm25eJ8HZPB+XMqNrW5+NRHeERaYuf/cYhseXwm+aWPYh3a00oemAe/UAcOV5PYylcFFyHm/ynLojoBJYL9TJHwMxEi3m83mzg5HLVvbJLWqtr8FLu9Rw4LNm9/HjynW6e0JSG2aS+gv8X3PfjljfOWIGmAEP9XusA7la4sSfqPdeb4uWpLkLTiWK9fxo8C0/Z8P3DusI51hZuGXHui5/+Q87HEsZJTDLvZsqdo62bJsZn6cf285gK/qEsjDbR5wTALyaebUXCnEb8xUl2fnWsNpn8Kx2jKIVJtN8sBwc+IBHYHpcezfDY9U+Hn8FnRy00CY/hLqTpw+Y0+Mh0smzqqy6Yfdpoig0jDeAG/TUSuCuECsIak2mGAJL908OZ3onQPxovQPQOuE1wufCSYVOD76xt0ajtW5OVi51va/qWQGP5F+mjQhflIB1/3+f6Kv9O7FKuVAP/CX7fvM3XqwCqxJpSr/MC+kb9cv3ZINof81SUszEl7xNpmsM4964vPNR5+wmomRT7jWkT7L7PeExDpgoxh0h/Fdf46wU0eXQU64rAQgeoasbzEKwp1gGODlzjs0/00+V9aBOxOb5Qt+JVXF2FlnuB4TK8CpmC+hfv3gEzailsdWu8B8flPppyrPSFY7OCh4we7u4R8/aJEKBm1zJUMLmpYiKDqPAFOnw7xc/5hRBNX+M+mdYie7TfC4dyIL15w4TTOk= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9918cc04-1cdd-4f38-c494-08dca11005cd X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:42:17.1110 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Currently the only iterator flags are KF_ITER_NEW, KF_ITER_NEXT, KF_ITER_DESTROY, but we cannot get the iterator status information or change the iterator status through constructor, next method, destructor. For example, when iterating over process files, in addition to getting a pointer to struct file, we may also want to get the file descriptor corresponding to struct file. Another example is when iterating over packet data, in addition to getting the data, we may want to change the buffer we set. In this patch, add KF_ITER_GETTER for getting iterator status information and KF_ITER_SETTER for changing iterator status. Signed-off-by: Juntong Deng --- include/linux/btf.h | 4 +++- kernel/bpf/btf.c | 30 +++++++++++++++++++++--------- kernel/bpf/verifier.c | 3 ++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index cffb43133c68..323a74489562 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -70,11 +70,13 @@ #define KF_SLEEPABLE (1 << 5) /* kfunc may sleep */ #define KF_DESTRUCTIVE (1 << 6) /* kfunc performs destructive actions */ #define KF_RCU (1 << 7) /* kfunc takes either rcu or trusted pointer arguments */ -/* only one of KF_ITER_{NEW,NEXT,DESTROY} could be specified per kfunc */ +/* only one of KF_ITER_{NEW,NEXT,DESTROY,GETTER,SETTER} could be specified per kfunc */ #define KF_ITER_NEW (1 << 8) /* kfunc implements BPF iter constructor */ #define KF_ITER_NEXT (1 << 9) /* kfunc implements BPF iter next method */ #define KF_ITER_DESTROY (1 << 10) /* kfunc implements BPF iter destructor */ #define KF_RCU_PROTECTED (1 << 11) /* kfunc should be protected by rcu cs when they are invoked */ +#define KF_ITER_GETTER (1 << 12) /* kfunc implements BPF iter getter */ +#define KF_ITER_SETTER (1 << 13) /* kfunc implements BPF iter setter */ /* * Tag marking a kernel function as a kfunc. This is meant to minimize the diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 306349ee3d6a..d053f058bd91 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8054,14 +8054,15 @@ BTF_TRACING_TYPE_xxx static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, const struct btf_type *func, u32 func_flags) { - u32 flags = func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY); + u32 flags = func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY | + KF_ITER_GETTER | KF_ITER_SETTER); const char *name, *sfx, *iter_name; const struct btf_param *arg; const struct btf_type *t; char exp_name[128]; u32 nr_args; - /* exactly one of KF_ITER_{NEW,NEXT,DESTROY} can be set */ + /* exactly one of KF_ITER_{NEW,NEXT,DESTROY,GETTER,SETTER} can be set */ if (!flags || (flags & (flags - 1))) return -EINVAL; @@ -8088,7 +8089,7 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, if (t->size == 0 || (t->size % 8)) return -EINVAL; - /* validate bpf_iter__{new,next,destroy}(struct bpf_iter_ *) + /* validate bpf_iter__{new,next,destroy,get,set}(struct bpf_iter_ *) * naming pattern */ iter_name = name + sizeof(ITER_PREFIX) - 1; @@ -8096,15 +8097,25 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name, sfx = "new"; else if (flags & KF_ITER_NEXT) sfx = "next"; - else /* (flags & KF_ITER_DESTROY) */ + else if (flags & KF_ITER_DESTROY) sfx = "destroy"; + else if (flags & KF_ITER_GETTER) + sfx = "get"; + else /* (flags & KF_ITER_SETTER) */ + sfx = "set"; snprintf(exp_name, sizeof(exp_name), "bpf_iter_%s_%s", iter_name, sfx); - if (strcmp(func_name, exp_name)) - return -EINVAL; + if (flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY)) { + if (strcmp(func_name, exp_name)) + return -EINVAL; + } else { /* (flags & (KF_ITER_GETTER | KF_ITER_SETTER)) */ + /* only check prefix */ + if (strncmp(func_name, exp_name, strlen(exp_name))) + return -EINVAL; + } - /* only iter constructor should have extra arguments */ - if (!(flags & KF_ITER_NEW) && nr_args != 1) + /* only iter constructor and setter should have extra arguments */ + if (!(flags & (KF_ITER_NEW | KF_ITER_SETTER)) && nr_args != 1) return -EINVAL; if (flags & KF_ITER_NEXT) { @@ -8144,7 +8155,8 @@ static int btf_check_kfunc_protos(struct btf *btf, u32 func_id, u32 func_flags) if (!func || !btf_type_is_func_proto(func)) return -EINVAL; - if (func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY)) { + if (func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY | + KF_ITER_GETTER | KF_ITER_SETTER)) { err = btf_check_iter_kfuncs(btf, func_name, func, func_flags); if (err) return err; diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 3d6306c363b7..51302a256c30 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -7813,7 +7813,8 @@ static u32 iter_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *r static bool is_iter_kfunc(struct bpf_kfunc_call_arg_meta *meta) { - return meta->kfunc_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY); + return meta->kfunc_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY | + KF_ITER_GETTER | KF_ITER_SETTER); } static bool is_iter_new_kfunc(struct bpf_kfunc_call_arg_meta *meta) From patchwork Wed Jul 10 18:40:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729575 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011023.outbound.protection.outlook.com [52.103.32.23]) (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 B0679195FEC; Wed, 10 Jul 2024 18:42:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.23 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636949; cv=fail; b=Uzep7RW3pV90tR1cmjutN2DbmtdVrhJ6sj7Mwe98BMXppnLRPpjJ6biBLmSlhDblPZdN4XSKrcQ4Twvnh7MqkhFH4Sgymv8gbCbefaHJODWmQXv8/P4VIusAu7Z3g/H/Sle8DdRKmtH13ryZ9Mra++zhdNwFfynRAtGD1cy6q2E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636949; c=relaxed/simple; bh=68ZeJqTLuBJYkzmaoh0YN9JiKhSbyeUT2TEtHG/vdUM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=hd3u0FLty2AyR9yx4mh1rst8SBS2RBw+Yitg4qDyZNsgacEcB3t9+PuyXfPpCdW7k3xGLzugWoIpcGreoPHofBnKhCTXjFN1NBtCTLwJ60zLS3VZt47zxtB7jI7stpgJNgztJceBaOMEsW6Cr6BjPsxg4HaX3WS7fgUCVKPWC3k= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=A11IPz8r; arc=fail smtp.client-ip=52.103.32.23 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="A11IPz8r" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N21z5BLASZ216tw7BwUzTaNB980C2SN03rvnc3uVhgBLhZ8U0/v2xhLRyTBxYAdRhgMiWSa48wGDbQ6PXhAu2I9i8P5KC9LVsGhgevngtDCtcCwcG30J73tzb5AQyA/3mgbh9CgpCumjNudYlGVI4SHYU4F0z0mF3zMmZSFNeF9Jbd4dd6rrDAOu3/B2Jek2j5940j10MxMeLvGhyvizdc5wFMEkGx0DewzAcbjkTutE6gSxlzQxieXWlG3T61WMZhcOKbV4UtwYbSM6pDumzK/JVS78GgS9Xrvz5TxdiJrkiSts/D9SdkEEnOyUBWo8qg+Dh/c+22Rrk6c3wwopEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=T1gZgNFQyoMOGPcHOBcOcZNQdBZH7a55AdBJ79c033M=; b=dETI/SrEp/Af/0TtN/3JSA1Rz9AUifQfUsZNwGrWTDsfNawwWGSFd3PjRCVdLKMuDhfuU81NotcCceQ+uGDGKcuraqQcKfHFqcfUiWpLmBTW2HTyrgJC+nASaNfqxN16+9yWj+qOSzye+lt6Hk5/VCBuBDm3Gqz7KZjZ8qs9aLa4uFKDT9BOXX2Vi7CRYSYFaW73brweMw0z0o+iiXQRQMsR1bujtC5C3LF3pEBTnhwbNSJ33/MMDASQGhkA9zLQT4J2ueS2k5uXGaP2zobfJweo6B3f+vC/EQgWfTHwNNEQqq1W9qtoRrdW1Gtd42j8QmS/ZVocsXHDoW+rqpbBIQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T1gZgNFQyoMOGPcHOBcOcZNQdBZH7a55AdBJ79c033M=; b=A11IPz8rjQn3r7/VUzfMBBkmgN7lgkwcmkcoS5FwvIreIUYMchQSsBy07gyXB1GCClIndS1dHVHenRrRrIRqKulK6GSp4P0iWlTPxdWwGO4le5OWLvL4Ycx8Iyuq1YHS1Z5jVcRe8q71h+gXMrRCtXZt3+QUceBxdg61p5I0QJP5wypG0QayiGJDka36lHGa+5BiTy3jFbXXP3k5Vwy2BSQXW9hXR6LqQ/Xc5upMSFyHPV140hyG7JXu5P/JaBz+0DDHUTxkiDh1kEC2YNvNSSt9J5XAIRAGlvcbCkp9OnRwWFTAXzcNtCF+bR39Bs7PANeqqJVHgD6t/8f5lYwXUQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:42:23 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:42:23 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 03/16] bpf: Improve bpf kfuncs pointer arguments chain of trust Date: Wed, 10 Jul 2024 19:40:47 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [fSDCC2H3DBWTKCHG/fa2Vl1QvsEmDSPf] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-4-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: e3de36e3-0e28-436d-2bb6-08dca11009a5 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: BsdVF2tcuhSJwPGhl+Or9BK1KayexPcb47cahfvWIa/DMLxKAdk9AdsisBqpHvvaDy9W6iVu/mlOG98b0nHPAZBElMtdOIdXWgfJ8mCt+PeKWVsa3CsqehRI8E3VuRsmvV4dDG3Rp+6r0HZJb12VUqoBqPlA418OZPg/ZBASkrPj0A+aejoGPrnMIVikPtUm8Pq6n1KeZSuzPtiEfKFHGmBpwfs6HwYVSRw0BUeE9s3Q3Ycs4sM5+WIqjji9Hv7gghpAXsvNQemdTspUZuQ/JWUkIxjWQ09N+5ODmZQAIhOK1Sxdn8eJMx/hOuLcpJbMrVG7OIfGV/7g+hv75chfWz8GxCawg9CmA/C3Adsj/NflJTKiRDPL8tb6F33lGl327iDLw32dxWUioE/2XE/DlkaleYakCw+hzziRCsPfnCLS2CBRXImgjtUzQOb0YLEAmGbybg/4N3W/Kmbq9zgWwSLfAxus1Ru4t2QtV/fT2qphl9o1KlCYBXzZALp60sjOTjQAdfSZ5AohXC3/aU4CKchWv2wPKiczNB+2ghb/qdqEUtEA3pxh2RfHn5+NApUiYqMWF5UYqvUEQfDtHj9Kf+rnYGgR0uID1dw8EU3GmdTpx5zgE2Z5F2s/cpoCGlTByno2/C7Ez0y6nu430TLc3g== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dvGI7R/NMgQJIMNY+/9UKmndh2Fmm+JPwThFb0I539FiVfvuQDgvjDqPHne5uEIBOArudwQkqQ6dKDWa8tWZ3aZ6P8TyS3yy6FRoyz8+WEjdU98vFYMSrQeLkLtGMJkNeomL37cV6dVeVRMFbM4kwSrcLVX+TjkBUJoU5aMKKxbOiE8JfiYC30HyNvwfwIzCxVYu6ZfM3wYWbNT7EVprJj9L5KKuCQ7rZBkLohOUDDjuJ66lZopQFSm/rG1660nfsaTwO9mz39EPFmDiwzMXG4KqN4fEboKJ0mHFe/sMSR+N3cjViW51j4JajEMqwQ8rdNeHkCuV7kv+lDa0V7OU8gxKOGAbU1mIdV2lHwk9Uyemyp6S1m+ICYOA/mKYilzl4vkmDC+YhpmUFJr49YOSRcn1KGpHzAufIvRYiEvDnG+B+Xz3iY63SSlj0sb0IC0wLSKxLvSM1WPd2JDSVdbodsTeJ5UuhWGBuwVHIYKZJ6EOhChfB5tQHrh9RpFYz6/FFAKyrcV3IOS7Kz3/qeyvM5D9DS/mks8K10z2o/mpg6jlQAcMQVJMy+k83RQLG5A+Y0XWQ+wEKzrvhJCwLhn0Vb4h2DwBo93d5ctpW6eD5PuNNcrdUF+Xj8nYcOCbO23tX0CiexS3oGQIhx+/018qP2iVE/Dmp207rE5nscFu1nGnksbhzGnYaAX+rFMOw0sltbC+8RW2oUqsDtFNV6Z2/a12X+ObZ2SkYMfUK0ag/5FD83hd+p+RSsdTe5/C41qjKrKXsGkvPazAMvQt0NgHF8SSU4jPqu3SDWCg3a1qhDZPtFk69MDViEq4xeDtI1xYSCPYTtSNaVOdXU9/Vh5L3PRLZdwQgQFRGCf4B/PKg54VQgAfsbq+7B6xe6K3SQlU3WnAxMvOoJgAnaM2f6QwoneO93TbVNyhYmn1qGJGryMTA2hW2B1baSHQAeMP6JwiwHGjGSu2mKR6NUQT4F8xVZHDALmnbSOvsv96blZqOpwWzQGlSlpXqFO3wYiStppxGNVDK/p4ui+aAcctaYQQMxXl104xpdsAvse0DSuezthw7ggwp7kfLxmb7q/m5ipyQhGc9G2wbiAd9saTG5BM4Wg2DMvqXsQOkxyI9Lbn+5uuQRGywmyqMmhlpT9GPnvdhQahQvZki4NkxBmuCvfnju4kYNh7e5MYGp7/LBbyg17A4bRq8iDdC3fPq8vob1YioUFhvbaAx12SghpdFNftm5X1xwfxdax/c7aFxjI9l0Y= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e3de36e3-0e28-436d-2bb6-08dca11009a5 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:42:23.6569 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Currently we have only three ways to get valid pointers: 1. Pointers which are passed as tracepoint or struct_ops callback arguments. 2. Pointers which were returned from a KF_ACQUIRE kfunc. 3. Guaranteed valid nested pointers (e.g. using the BTF_TYPE_SAFE_TRUSTED macro) But this does not cover all cases and we cannot get valid pointers to some objects, causing the chain of trust to be broken (we cannot get a valid object pointer from another valid object pointer). The following are some examples of cases that are not covered: 1. struct socket There is no reference counting in a struct socket, the reference counting is actually in the struct file, so it does not make sense to use a combination of KF_ACQUIRE and KF_RELEASE to trick the verifier to make the pointer to struct socket valid. 2. sk_write_queue in struct sock sk_write_queue is a struct member in struct sock, not a pointer member, so we cannot use the guaranteed valid nested pointer method to get a valid pointer to sk_write_queue. 3. The pointer returned by iterator next method Currently we cannot pass the pointer returned by the iterator next method as argument to the KF_TRUSTED_ARGS kfuncs, because the pointer returned by the iterator next method is not "valid". This patch adds the KF_OBTAIN flag to solve examples 1 and 2, for cases where a valid pointer can be obtained without manipulating the reference count. For KF_OBTAIN kfuncs, the arguments must be valid pointers. KF_OBTAIN kfuncs guarantees that if the passed pointer argument is valid, then the pointer returned by KF_OBTAIN kfuncs is also valid. For example, bpf_socket_from_file() is KF_OBTAIN, and if the struct file pointer passed in is valid (KF_ACQUIRE), then the struct socket pointer returned is also valid. Another example, bpf_receive_queue_from_sock() is KF_OBTAIN, and if the struct sock pointer passed in is valid, then the sk_receive_queue pointer returned is also valid. In addition, this patch sets the pointer returned by the iterator next method to be valid. This is based on the fact that if the iterator is implemented correctly, then the pointer returned from the iterator next method should be valid. This does not make the NULL pointer valid. If the iterator next method has the KF_RET_NULL flag, then the verifier will ask the ebpf program to check the NULL pointer. Signed-off-by: Juntong Deng --- include/linux/btf.h | 1 + kernel/bpf/verifier.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/linux/btf.h b/include/linux/btf.h index 323a74489562..624f1e3d6287 100644 --- a/include/linux/btf.h +++ b/include/linux/btf.h @@ -77,6 +77,7 @@ #define KF_RCU_PROTECTED (1 << 11) /* kfunc should be protected by rcu cs when they are invoked */ #define KF_ITER_GETTER (1 << 12) /* kfunc implements BPF iter getter */ #define KF_ITER_SETTER (1 << 13) /* kfunc implements BPF iter setter */ +#define KF_OBTAIN (1 << 14) /* kfunc is an obtain function */ /* * Tag marking a kernel function as a kfunc. This is meant to minimize the diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 51302a256c30..177c98448b05 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -10819,9 +10819,15 @@ static bool is_kfunc_release(struct bpf_kfunc_call_arg_meta *meta) return meta->kfunc_flags & KF_RELEASE; } +static bool is_kfunc_obtain(struct bpf_kfunc_call_arg_meta *meta) +{ + return meta->kfunc_flags & KF_OBTAIN; +} + static bool is_kfunc_trusted_args(struct bpf_kfunc_call_arg_meta *meta) { - return (meta->kfunc_flags & KF_TRUSTED_ARGS) || is_kfunc_release(meta); + return (meta->kfunc_flags & KF_TRUSTED_ARGS) || is_kfunc_release(meta) || + is_kfunc_obtain(meta); } static bool is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta) @@ -12682,6 +12688,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, /* For mark_ptr_or_null_reg, see 93c230e3f5bd6 */ regs[BPF_REG_0].id = ++env->id_gen; } + + if (is_kfunc_obtain(&meta) || is_iter_next_kfunc(&meta)) + regs[BPF_REG_0].type |= PTR_TRUSTED; + mark_btf_func_reg_size(env, BPF_REG_0, sizeof(void *)); if (is_kfunc_acquire(&meta)) { int id = acquire_reference_state(env, insn_idx); From patchwork Wed Jul 10 18:40:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729576 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011073.outbound.protection.outlook.com [52.103.32.73]) (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 1B23D195FEC; Wed, 10 Jul 2024 18:42:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.73 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636957; cv=fail; b=u/uAwu7O+h0qUiXexdzaeOlTgC2u9O11RBohqStfzowf0y6G9IEpMt5l0kPAB1PuKCTdLUCWhoOVflq8vrnIRH4IBt07ZKTxmC6IrsdPK1zrMeuovwiALRzk0ZPyb7z/Lz5Gma/qRVrsWmkG0+Ubxnd+lQXTMpECOQLEoILndYw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636957; c=relaxed/simple; bh=toLCOAeQcWKVCy6tBoaJfr6rl22TOb92yFiiPxWFopU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ZOcexvWkntCCeegGMCvDyA7w0aSQAaTWcCUl1msIfbUOC2TWcX7+TSyvSs4KN5tDoxRD3I0AMujB9ukrjrn2HJYMWRse89jWE+Bbqt88xRkQ6muWEdItHwC+K7MowSbkj6itBdvQcObO6xZhKPWK2inpKREI8k1IjHOnJtCUWZY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=OeQQJZWw; arc=fail smtp.client-ip=52.103.32.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="OeQQJZWw" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U7lAMBcVsZdQf2t1XT/1KYbIYzdSRB9dUTJZ0thSJ4Ofsh4Cm2wkshfSfM5Nc5DT+M5Q5oKFT1XFyuVEQD6cKVIRTG6wR92TJz2g/EOZJCB/eyoaWXtlbDid/o3EsuHpOz/KXavCDkYGpKDc3SBQytCl9UIwj39pjpRscYYQ2GUHqyYkgsBeUQuoqe6GK52EgEp2zTDe7MuVawK+57ncX1fKSVrFZQSj8NUz+Lb0poK4UsglIPxzeeHvBZmLtkllA0hnjmFT+dgitGvkThjvTcFfGqLslw6dauxDjUe40hER9N9lldVY9e/d03qO0RleQBu10UCBWRPAvOyx/Xc8xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Fs0q5gAh9cQAldUvCtX/itxD7miVjLG4u3q1LUKA9nQ=; b=GsS5d1NLdytKmYmOHBJkMJZuNan2PItnadA437R/JBWokeloXEye1TkURHxARZR7BZjPZpALVjszVk49x29a+0z+ABViaVF42fCdq69V6P5RHaowzoKJB4HaLMkFjiQq9y8s+e0p1wPllDs7cP58Njr5MoiUSLLL1ezdtE6aZxoWFtdWKqminmvzrWHphk9ULpf+CjHKYYoy1CbRfIgeU32WFMw3LU6mZMTOXrYSvF0uGQ++vllq6ndlW7pardwkKB0qE5KHhW0PXSSjFnqbNIM/OMmIHZXU8Jr5T4itFJmc4N/+1bAbFWBLz3VCHJNV6zIAIfq8u/CeADSEwLLaYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Fs0q5gAh9cQAldUvCtX/itxD7miVjLG4u3q1LUKA9nQ=; b=OeQQJZWwcyH/tXg6uDuOyFtITN2M0ktqo+U+vGLpFZY+zzw93rkMy2aKqM6/2Uu3h2KIfa1ZDZZvRAmEFud8ndATVwPz7r1udQs/rK2Lyn3dRyMN6BrpHZtzSMl8vPgT0RRXLIQ6iQkiRSTyk4yVuNdW/Y9HhVZtkKKvP9Dup1shj5gaTUprS++/8cHUrIyzkJHsorImyJweUakY+AO+upw8w0UMx29hzRgxtOJH8erMgP2xZliNHOTwNY3qFWHYOzuRz1Ms7jLwJ048ij1tQZM6TBzgmA4To/b+rzkIxbjrJ8HZ4DwM+GnkA+W1OpxajbGrXmPMN2vJh3iV9FxriQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:42:30 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:42:30 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 04/16] bpf: Add bpf_task_from_vpid() kfunc Date: Wed, 10 Jul 2024 19:40:48 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [/25l0NBcp70PH7AITPtlDf9bHH56O41R] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-5-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: 83c60e7c-4b35-4faa-5e03-08dca1100d7d X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: xc2dbvyvtEA9kWLwYQG2M7yFp0cu8ZuGo+Ah+XTSHOUNnbA8axJOtN0aw9KcsLVWKWg2nkZkpKkIsSavC1mHJDnmo6O0fPdBVwn46uIIhDu08E5H9v1M4+gQhEAoO5tcOS0QRfQmv5oH4y2hJvkf+vABYLBHEheXAqJkK9E9fVheqTuvyN9gKyEki1oiOpefxJrR7AIqvB8WCTDDjEcJiyQll8td6E1MsWNjvcIEAF2rlmowVa5vBNSOniZ4J/AoCQ+iSvif4TrA96d+A/aoUZyC24vSNuLa8HQk5pDjVU+owEoR4Va0DYoqWPh/njYSul9dwFnP4tAsnGIR7DxMlTdrnBVu10xcpDPjmPEwIdWISV0p/q2gLi1uau/B0N2gx57lIm0CBlm842j/Uc2fdWEUhhKACNSH7PgVrimDvubawdW0XgIsNn+TKoYh86cKvN8efPMJevzCs/1V6TSRf6fOyh2XY49BfE38qmTRXu5U4unLudi+BUnmGojjbTk9kCtBL226x1gywBFltHCPpN249Ao7MiWV56Lil6qlQZXhL3oEG/fgdlXgDx5RLKKAPcpK9Ntg0KOpSWGwcTTZIy9g47yPwTifJApI+6eRSFYMWBxOoMNFoOA5KBOQBhPgeNDBYjHjxVHy/sZaHW5DQg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: aomytwXsIu8EEGWFuyxOE1Nle/iCsCrwgwxtNL/IWGshFggbufH4rJuXVscWravZjGoEEofay3vyyf+FTak7X9yMC9MDGbkaaESYIAMy68QkldzFso19iqUByUY02fXtujsWueHXz3WVF1/B0ksr55XjyX1d8ga3yVxzHmViJsm8z4tIYbTnTJBvh52vCm4XladqHDHos/wq/QVv7L0t550tuFqA/xIkPdLf5g7OBALircq1AKkWPo+o5CEbAtsKkowWBSRtnYgSBCuwlz5aDTSG6rTyuzTGumIczHhxy9hov4jnYGGuvHv7PjtnvmtvWImnc4HL7NjvSqPeMNfFfKGuVqCMeNfdqgeNo+oWleRpMpgWHEcsCfkYOKkSnbkILsa4FDNAUF6bq9hlfzMslbhTxguvlaPNq1mglnLBhmi9QCxuAiahPn68kAn9x9VfUONYbw6lcDtqoZ66BzsTWDurX0yqruotcC1j2J+5YjiDKRE+MtbIhNANr9zTtv5lfzzlAKeDRfRN+LJhKrYeM21IhuKT6NljffWnjJrP8if1PZY48Bi7WA2Vtr4qK4+bX612zJwn0jI5SIp5lULhCNRTEWIJU3P0LlP+2KBFyMrqCISFmAY378BnrBTIrlJKlIjm0NZ55qQrryahqWgfbu1MOd6rjG+O91X9bzouag0L/T4zwqLL43aLmJckkTLBENrZUQ5m5z+Z+HKojgklLBBrZV0BqfhjgqhDdRHoSxod4fze5g6xHBW6Efm1ULw3kGdV59aNSMgLVM1dYZzfWGg5JCAl1/ywWpyGISBEgaI8XJ5sllOOH1F00EyHFmI+7Umv2pBeQ6CvNNgsISdNStO8VYbum//kCx8EuOzpk8s2Z7T7I9p+wvHFK07q9XjivBcWoN4VVzAwQaSZyvFdO0snddO8etfSzr1WKZA1Pmk133p2gHdWaBUeQCOAX0HAFpH1ihBCBjc28tgKvtGr3I2Z3/J/qGEl1AOUEGXu5oQ0nqHcCi12/7Q3KpNoJT44rwepRWYmMJAiHgoqAc/xAlgDy+8KYfcoZa37sABoF9P+4370QdWmYt6J4q+h1gNHZAIhZqslbB19uNbL3GwIGL/5WwD0f925h+4eewlRWWg9VrmBxYZVOZFIx81R4MoN8jIYCUD3F9JX3nqebw5dTnSpZdyeK1kdqm33RbSBPTachOgPj6VGAscjybGJBcsGrvnWro58zRw64zdqV+swOYpu0y69ShI1SKPD+u64+Ko= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 83c60e7c-4b35-4faa-5e03-08dca1100d7d X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:42:30.0129 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC bpf_task_from_pid() that currently exists looks up the struct task_struct corresponding to the pid in the root pid namespace (init_pid_ns). This patch adds bpf_task_from_vpid() which looks up the struct task_struct corresponding to vpid in the pid namespace of the current process. Signed-off-by: Juntong Deng --- kernel/bpf/helpers.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index bcd3ce9da00c..15be713f6495 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2431,6 +2431,25 @@ __bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid) return p; } +/** + * bpf_task_from_vpid - Find a struct task_struct from its vpid by looking it up + * in the pid namespace of the current task. If a task is returned, it must + * either be stored in a map, or released with bpf_task_release(). + * @vpid: The vpid of the task being looked up. + */ +__bpf_kfunc struct task_struct *bpf_task_from_vpid(pid_t vpid) +{ + struct task_struct *task; + + rcu_read_lock(); + task = find_task_by_vpid(vpid); + if (task) + task = bpf_task_acquire(task); + rcu_read_unlock(); + + return task; +} + /** * bpf_dynptr_slice() - Obtain a read-only pointer to the dynptr data. * @p: The dynptr whose data slice to retrieve @@ -2903,6 +2922,7 @@ BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU) BTF_ID_FLAGS(func, bpf_task_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_task_from_vpid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) BTF_KFUNCS_END(generic_btf_ids) From patchwork Wed Jul 10 18:40:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729577 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011017.outbound.protection.outlook.com [52.103.32.17]) (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 AF4A8195FE0; Wed, 10 Jul 2024 18:42:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636979; cv=fail; b=KQjiQ8SAa3fxq2uHtg0KrLQoRCnea3D07rrrNnZDURe12nbiq/JY+IAvEvxb+F/0u2mG7ifKxxAORRTu4QsydBxwuHB05YRIm6qiL29GaxYY/NLBm/j0gYUcyvQ4tt6r9rRqNcydR1LOifWJzNNeAvKEXzzU1z6zqC1T8AL4Lfs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636979; c=relaxed/simple; bh=nhvKRb+Yls5UR7AMA4Dx8jWAvsk+gA+QOEFw0H1E2Jc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=BkgKQEHx7UYAP2FZ2umQ0TvEHBttEuAtGWbq39KNwJaOsh3A0aEYVd5jnG+D7NqqbKlATiIKhO3wnCgivdZsao9e60y0g87owxR1eJSBnMSnbyw3gUR1PgP3ORTmadPCguUWdn2d7RgyVmsi+3Vv9WcNnKrRoaevIwJB6n+aZBw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=lri7HN3G; arc=fail smtp.client-ip=52.103.32.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="lri7HN3G" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G2t5MlMMYvEDSdgP50bEfXS/ekmTt377yOMrP+2Xe3k3eBQO11QbPpmKgGKeHk183cx6gx7izdlE64qBNSQ2xobzNwwKcAeLW10QsQ5DUiFRqz6QY1F2BzFwR/UmC4nw+Pq/Zqi+/zl3wFgin7U0uKgT9g8VOAehvlHJhPG5UNYIRIXUOmfAHObkFeTGjWGZ5mh9YXUsgJQ7p8tph0ui93T48ZPecOzW2BEHPe2fNV44nyLp2Qm0bFTXs1BRro5I3u+8MALiRNZQ/nvTbED3QWQ/MdZecbEyC1oIg/X4EkF5q6rb94QE80RZ+0ZGbnAEw+8kFwP4mZ1KAOCgK89hJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=D01jDLQAupn+yFXzKYkZ/4dpOZ4AixU+vSFOGqybkRU=; b=BgGUc8/ba0TOqnjM/MAmQIkOXSB1c5wRu7fA4E68JNT0kcQPO3zryaY9wWccmIK9xIY5t9LcBEf7mK11d38+HbWY72R8KT8RfyuRqAvy52OCAlOLCBOsozgv9W4Gx536DOMwWmbri+Ad8da7dW4Jvcd0iJgVAAsIeT+Pke6qtTkmj/5C4fm8UWdfitxP4iXkwpXM4kdbe3kfjLzUzAPQPaRU7lalCa2pkSfTeyLAIzBI4hy5uQCRfRfO8PKXJGNakxuWnFeBza2lMydWJRAZzhPiRJchDKo5VT4A5AxF8XWg5HWWRZkgOZkGDYXwmbOabtNMch190gAK8XFnvl/q9Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=D01jDLQAupn+yFXzKYkZ/4dpOZ4AixU+vSFOGqybkRU=; b=lri7HN3GlwPef5jCU+37FvkwkC7Qk/LNukmBv94d5bBBE3yE2xOCpChB3czMksGwguJ2JNbdsEnOqxNgNzSmW+zhcjl2KiI1a6ZzsPoG8/sgvVAqAXJB1zE/Ya8q3ZKnkG4jkQqrJAqaI5MUoBwmuVIJs+E4lqTsPFhB5JHul+CEpiFhI+B0WjMtcTHiulF1mkXQyoaor9vFWJsoeFKpBVGWAFIUAa+K35C/c/f85Ucs6LFl3f8fIYx/khvj9tANfFvpcfX4ObRuuK9icr9PCTQD0ZMLzuCIVLv7qlYgFPTvsIuHIOKEEAHLcKHA/STxDi3E7RKC5mGwQSuvQPKpCg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:42:54 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:42:54 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 05/16] bpf/crib: Add struct file related CRIB kfuncs Date: Wed, 10 Jul 2024 19:40:49 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [4x1z7pS1xOztjUvq6mtEagD+eorIqI+n] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-6-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: 322fd70f-dc05-4eb8-0166-08dca1101c54 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: wu0oA7RPaFBO6nRHuJcvG0E11fd/wmWFGedEixEw0eVs2tC2N4RwRK+BlhM3oYltdnaXho5a0HHdJ00BK3/gmgU/Yu7u8sfX7NjvBHNGwsTukd2Yc2nXjHpfHX9OzAraY75MgyRKXvzoh0UxBDZyDaewn5ie3fgsijP/pfTK7rkEf4QPS0x6WpIBKKV3eab5NgIzy+hD8U3qz7BUXWPkw5FBj7PGLzYiVPpeTP89TwmS09QlAxyWC7ZCKpewvXRDVb/QRzn/R8HYlCfWaPJPc6ETb89LAuiqlqHsxom8b9Z3lQ6s18Zrg3uv4L4FzQS05qrRsVspfxe0zVgLx1FinAFxX3yfNWGdXj+NaVtGdX19QhwB+gFmDVHj632YJXJE2CMIbbniq7oWRRAizlL7UjrKe6LL7wBDW7R7uW4E47olZ8i+rFQIidDxd7IzqVrJ4QAfzHr+s4e0ewwxshTHihC7MMk/DQAiuwVXJGMVDtUIQwNV7lwcmqEgKmnNupUjxjQ5k8HLGgxTkFJ3K/n2NSVxePohVJO+4QsXJ93wz2tBG+TRlrnBQgsbzLeE9RheiZb6Gh0/gEyPT+vI3lKU2iVR3sIFP/u7gzU5U+BUVZ2MdsntkLVtYR6+QlJ4GoxNPV3xqHb5uEUj2F1vKQVuoQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ECmLdgpXJiSixD3SDlyVfogBJQi+EQKDEbfA/cUx6XGbVnukpQtkRZg2ZKwajCw3LFi1xRf/QIsRaCypHdZcuZAixQ4zSSDlZgQsYVI/uPNdBcCFvNmQVd5i7jYnpZkL4C2zSyW7nhP/JVdFb57yL06tH0ZFaqVeELaucfPtL4vzipHDvYMXlfDGFB9ziwrwxDYeyrrc6tzZG8gTpT+DZovwAsslH8aARICtIWsIVL+vY5p/FRL/Vm2iCqSxHF6Hq4UTXulAA9U5kfWR+nxa9BAiSUUZyLotz2dDuDx+p3JTFzuj9GGC/hjT0diidvXukqFtWMgdYhidug1Q10nGqcepWYHdo5C2u5rWpPahUzdyzE0sPwUlHlRYzsgrqCvRlmHGDHiK1A5KiV8ogebQ5rHiojolBMoMIZAkvtFYT1iXSa+UrrW3fhrnNT+ooJamKQP2d+9RZDmuK1td7R1EXJxKPb1v1bxqhkn6SLkTxQSSWk0L7oQxUayFvPpUhVtot9eLTj+8NkOmECcrOOPI46ohE6EfL3CLDQf4/fhDVdonTPoBpzu0FTjzM9+PcAj8mraPGSIFaT+GUur0RreUNjs1rLp96ernFO3H11F7vdY5bWjZWqGzSbNYAl3dPhtg5vKWzwYEcD4/dJnvTFLo0oELHiFweenZf9LjYa4bjkHSbNMYGWut0VoM53YIuJMqC8jYGkhMH6pDsmc7B6CMdw9QiFoh7CwRdBCS2GmaJZA4NBU0XSq9HBDIIPt/I7Faacz9UF7BFUPXsR/VC6Q5MG2orlLX95EC+FFWuP6y4R/j0CCQf8LDSFCvMLmrlq3n/O/rFc7rxXwukd4qPewRrM3FVBCJNMZupscQ+aEh3YBFNefoH5C42/de8HMqaTJqA0DjaVJcS8feH9W6pBYiUGHa06ZedvnI27cA62dxT9HIH6K5xN/bOfo8fkHOgnIZVzjBaV9Znbfu+LP5AHZyA2TbcYH7NMeeRa8HXrI/41kvHUjyP4Kwo1HokV3dW+K9+FL3nJPgJNfqBf/kFr9lmhwe5Fwb15eb1X4wzoZwRH/ZNA4UdeqXHMHUO6tb2nZIcTwlB3QRzRph9Yao2ugF8Cq7pDTptSEL9MGQhcAyw8EGB9F1x+DPCXAPyrBP/HCScUQ1IZ8o6nSoGMKK9IKcJ3eU2sPYUWbzfeerr8oWjkT9NNVbIeInGvaHYor/cl9Z+8f9NvGE256uKKxSG1VAj2kVgwvljMxSE43RMhAlFJ0= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 322fd70f-dc05-4eb8-0166-08dca1101c54 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:42:54.9021 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds struct file related CRIB kfuncs. bpf_file_from_task_fd() is used to get a pointer to the struct file corresponding to the task file descriptor. Note that this function acquires a reference to struct file. bpf_file_release() is used to release the reference acquired on struct file. Signed-off-by: Juntong Deng --- kernel/bpf/crib/bpf_crib.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index 9ef2d61955bf..1c1729ddf233 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -8,13 +8,50 @@ #include #include +#include __bpf_kfunc_start_defs(); +/** + * bpf_file_from_task_fd() - Get a pointer to the struct file + * corresponding to the task file descriptor. + * + * Note that this function acquires a reference to struct file. + * + * @task: specified struct task_struct + * @fd: file descriptor + * + * @returns the corresponding struct file pointer if found, + * otherwise returns NULL. + */ +__bpf_kfunc struct file *bpf_file_from_task_fd(struct task_struct *task, int fd) +{ + struct file *file; + + rcu_read_lock(); + file = task_lookup_fdget_rcu(task, fd); + rcu_read_unlock(); + + return file; +} + +/** + * bpf_file_release() - Release the reference acquired on struct file. + * + * @file: struct file that has acquired the reference + */ +__bpf_kfunc void bpf_file_release(struct file *file) +{ + fput(file); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_crib_kfuncs) +BTF_ID_FLAGS(func, bpf_file_from_task_fd, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_file_release, KF_RELEASE) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729578 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011017.outbound.protection.outlook.com [52.103.32.17]) (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 92D59197A9D; Wed, 10 Jul 2024 18:43:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636989; cv=fail; b=TELymdu/YiRh0cIEQWnHKhdCDsTEnox/Hbi1FtULrxlrJeEIg1/l212SLL3/ejXiiKm4ggFnhUdqYZGOBYtrDKzV2EHxEplPUoq8TTllc8jYF9DkAEHt/BIG4FECxzs77qPbdYRGYwaPQLok9sxrk416sr4w1M2lq6C9lWUQ9VY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636989; c=relaxed/simple; bh=CagETmbQzxQy7tHMFmKMp8hlYorlYA/ZSNKsjsKz7P4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=DkY5FGM2q+8GJTvfhujYNOKdvzZaMOjZ8pJz0Y02yza5JhrCEBboQdA8wyn7F+nklSce0eVr0MLxvJVK3eI8wmpBaN7PphXLDELeefIrfLau0asWbw5cWoj59D1arpoPMHavUUoOQuBCi7H3xxEh3PjSw2jPwCD7SG7NXRNzuc8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=f/gwW6EB; arc=fail smtp.client-ip=52.103.32.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="f/gwW6EB" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=M56pI++t09lCeT61ytShZHoXX6Mc+nFSba3xKMfmGMagmeYcT1iH17b6nVrF1B6KRBwZJ7NRwuLCfgqDQa+tA6MfNGhhTAkkjfTs6T1Xod1LhFTi0TxfbT48dW5a3V+Ud6hOmkKbT5Udn73ibhoPlZ7R9Jc3yOLvp+1JsAS9TLNjY8XdeBFzUbPRw+i+ZFxHXXpCnLGqdt34VM3GQgij4q3gHjTn3yl+jQ23HlADp45LfMvySj5/l4abZQYUQKJcwT/SOMfp4fIwTcMQj5xCZ1KHiku7WzaivaZrWpM3IiXjlVp4LvUlhRNBzriXS8bu/s5sRHHwjarZbwDHKVFp/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uRPnIsO7k3xpL5wwp/N9T6QoD19hlcJIS2GQY7S4vmY=; b=D0XTe+h8dXcMmpm/cROJTB5iEzMnlAQ8lx43BWLMn4CJTOqQiV+jTCHEUsL38rx7THXsDiFjXrEgZ6uMdq1h0reD9p7ffphSVYOVJ+vP2LkFl0r5NxhKD3AiR5/VrvcRM652S3zv6FihC/PGwQvdZiatYVaw7Q9QzWF5tnGLPQWGUYG3SAWy1jKZK8wzIE8VAuiCZpgo7jOPy6hp5FCiZXn3UZ5uDaBLi614OxlssMlyvCJnl+R/es27K7JkFyawq3uVIc5KQ5zUizPvHZqZGYFRGVpXLVwS4K90cAkOeD1cpH1mknVsTpItKmvC3/KT7AMlP6WCPhvUQjXnZlzRpg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uRPnIsO7k3xpL5wwp/N9T6QoD19hlcJIS2GQY7S4vmY=; b=f/gwW6EBXcdlcXaFTDQbpd2QTDj8shl+3KXLzWSjwKIIYzymTLTAAhP6uX26kHCYMv5GKBve+YEvX60SjGjUjcjZYuFH6cfJPnsdXI7kvCF3mpoywwD1QUtvurrsTkJZnRLWbur2f6XsgEMtgvfP37wTRuES1BAZVKV7O9gVADiiqazrpW94rY3D2ZWtndtGoJg+zFDddMMyXhBgFN0K1BgFpZEXH6F5qePxF4KbK3Kj77e3ItYJzBQpt5i1CoKCG3yLWO9lAxRWuMARvjabuzVUGlV/dUd/qwmicv3Vp6uqkLX6WWcfPQBSTaT6yUZMeVp5jufpSQnKJ5FGTsXk5A== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:05 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:05 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 06/16] bpf/crib: Introduce task_file open-coded iterator kfuncs Date: Wed, 10 Jul 2024 19:40:50 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [joxeLc0fB8DnMmeFv/4yBULradmxBYYn] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-7-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: 1ac533ba-6db6-4169-9303-08dca1102263 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: /cdZY+31pM6pHxbeiz6Q7+872YMFfIJToyaT16r6tQRhoHWr8fhn+NwPkZUVqWz9kuWFWfFjLF3H73lne9HtZfG9a+N4I2EK42bBlI4P/di35eGb+ukLQNBM2LDJXyn3LEhIGPn7rW9z0pt8ODmLL0zMzt/UV+pCmQZKRrUz70ROBuE4bB7DhxQyhDSJW6q9ORPx2f9agoU/d1zeyIGizYehIKv9jRw4pjT9i32/2UisCCYnmLTml/I/YWLRezlqUpssZv6MPD53Vj2jNBa+x/nV4Xg53HqSGiTnAoEsmWd0Tzf4h3w6PDV2T1hBLKowVsaB1j8xRhbS3wQKWa+tFKUkHZHIQrgmz1NY4B/FSE6uRZ1OQTYGpx826g7tXmhCxYhydsdmmSMbWCGoz2FKxaAtDN5G/4froV0OFaRk/A/3ZBM1gMCWNFreptlrM15b7k6UGjTTp7G3LPVN28HvXx0X93s8Ra+/fvgeR5tryjsF9pFj8ASqpDN8Qm0Js3lEQJfZxpakvl9bwX5gwAYjsz3ZdP7B1wfr+95pj+DR2+CzQ8XiRjkiQsuReAunQ46DoUxMIgs+YnstYDBjxIjPlhJnzJsPosLjHgVt8tSVgm15Iaxvx//zddsSoeLD7YBlD7ffPbjvchQVts8YY+NHoA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WwltLrgQp/nyQwf695Le0SvxyQi282oS+uGepPVmLo1WR2uTJhos521P04qnZ+6yt8Tyn6CfOg6N6ZFfSlZdJ5N5TFIuwMeYNVJWvoZGEVK1Mted4YaVJ5v2tTNuL4//7x+bs3y/9aEycRWGuZDPLfqq3PP53tt5QhEk4D11Xk5TS2TkExy4jTXm2uafzMc4V0tQU0oxXWd8/EePN5dLNeF1zJKznLZuU1lmb+tOQ5fHAA8h4dSoqbwcUhZUoIf4d2SqrsnCeoI7vNGxMxs0IkLeLMyp9JRN2V4bH4RWkkZD8INwibLoR2HsBDKiq/+CWy/v2D5dbbzywqMM1P4H8+JHxwji+RokZbj2RGFTyQdvvRSfPHKdVqHAk+3+L8oQH2uB4+9w7RLY/sDT7MHRVue9nh28Gx2JMsOm5wUiK6Vodj9zWnrEHaexojAGG47ML3M0R/GpDW+eQPKt4QEQOQQ+VylCFAYZZc+X2Es70b/YwmIVHx5JvqTi1lFc6qhRbwv3D2z+aFLvYIuP7CzNT3b+0QWN7D/Ah91oTiMlqXoR/bQP/1cTZx0x0Adq6AnqW942yUNf3uYKt9LyTkcSJYRKCGs7fCGjzsT2CWrRfHGzSAuRdo+iFK8xynwMBqBOKPqY6OoymHaKbe0097gRSpXUUHqPnebPA5fMx8fDVbt91WdCozdT5VyLLVtHaJKk3mrrEwKRHe1N55XAeYyO4Cy94/wONOS0067Jw9QkTXPb2GFDQRfL95NTV1PCzV+hxBo1SGx2g2vGWNJeQPrifrYjRFY3/Sj+CkFIkNFaqsApruFDg9deKbK5ydMnGWaKHDoo+T3ey9qWdutYTciBXpTau7KBCax6UatK2lKMhoNuV0uuHci6GAZRKdk66GoIr38sXPdFzJx8gHaWYfVqOjb61FaCPJzVoy3gtwjWOuPKDa4Ak2S16WLwithDSCoW0x6RNlyv9IO4TOhE2dTfkprgjicMkm5s8QGgwy8B+HcfP4qNZmzIg6psJO+jJdlPsdPPjuQdSZ6QzyaxmcrNTXmhK5RwvJHV5oHi0mdjqxOB7sGnaNKmlUU3TCcX82SUCBkPl0NWHwHk72gGzK+2xxLl/aoW6wGrnmT5/iYnbIdkkIru2ADx4Uyg4thgnoxCSVrYg49mhVFskfHQ727TU+F1JC+7H99i4UEOwNtNtSqItXF3s2PHzm1WPVELo8q9irkjCsOo448qpX6+DszJt6Um605r+tc1Vtl/xRobBQA= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1ac533ba-6db6-4169-9303-08dca1102263 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:05.0768 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds the open-coded iterator style process file iterator kfuncs bpf_iter_task_file_{new,next,destroy} that iterates over all files opened by the specified process. In addition, this patch adds bpf_iter_task_file_get_fd() getter to get the file descriptor corresponding to the file in the current iteration. The reference to struct file acquired by the previous bpf_iter_task_file_next() is released in the next bpf_iter_task_file_next(), and the last reference is released in the last bpf_iter_task_file_next() that returns NULL. In the bpf_iter_task_file_destroy(), if the iterator does not iterate to the end, then the last struct file reference is released at this time. Signed-off-by: Juntong Deng --- include/linux/bpf_crib.h | 10 ++++ kernel/bpf/crib/bpf_checkpoint.c | 88 ++++++++++++++++++++++++++++++++ kernel/bpf/crib/bpf_crib.c | 5 ++ 3 files changed, 103 insertions(+) diff --git a/include/linux/bpf_crib.h b/include/linux/bpf_crib.h index f667b740fcc2..468ae87fa1a5 100644 --- a/include/linux/bpf_crib.h +++ b/include/linux/bpf_crib.h @@ -13,4 +13,14 @@ #include #include +struct bpf_iter_task_file { + __u64 __opaque[3]; +} __aligned(8); + +struct bpf_iter_task_file_kern { + struct task_struct *task; + struct file *file; + int fd; +} __aligned(8); + #endif /* _BPF_CRIB_H */ diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c index efaca6bcdfe4..28ad26986053 100644 --- a/kernel/bpf/crib/bpf_checkpoint.c +++ b/kernel/bpf/crib/bpf_checkpoint.c @@ -7,7 +7,95 @@ */ #include +#include + +extern void bpf_file_release(struct file *file); __bpf_kfunc_start_defs(); +/** + * bpf_iter_task_file_new() - Initialize a new task file iterator for a task, + * used to iterate over all files opened by a specified task + * + * @it: The new bpf_iter_task_file to be created + * @task: A pointer pointing to a task to be iterated over + */ +__bpf_kfunc int bpf_iter_task_file_new(struct bpf_iter_task_file *it, + struct task_struct *task) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_task_file_kern) > sizeof(struct bpf_iter_task_file)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_task_file_kern) != + __alignof__(struct bpf_iter_task_file)); + + kit->task = task; + kit->fd = -1; + kit->file = NULL; + + return 0; +} + +/** + * bpf_iter_task_file_next() - Get the next file in bpf_iter_task_file + * + * bpf_iter_task_file_next() acquires a reference to the returned struct file. + * + * The reference to struct file acquired by the previous + * bpf_iter_task_file_next() is released in the next bpf_iter_task_file_next(), + * and the last reference is released in the last bpf_iter_task_file_next() + * that returns NULL. + * + * @it: The bpf_iter_task_file to be checked + * + * @returns a pointer to the struct file of the next file if further files + * are available, otherwise returns NULL. + */ +__bpf_kfunc struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + if (kit->file) + bpf_file_release(kit->file); + + kit->fd++; + + rcu_read_lock(); + kit->file = task_lookup_next_fdget_rcu(kit->task, &kit->fd); + rcu_read_unlock(); + + return kit->file; +} + +/** + * bpf_iter_task_file_get_fd() - Get the file descriptor + * corresponding to the file in the current iteration + * + * @it: The bpf_iter_task_file to be checked + * + * @returns the file descriptor + */ +__bpf_kfunc int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + return kit->fd; +} + +/** + * bpf_iter_task_file_destroy() - Destroy a bpf_iter_task_file + * + * If the iterator does not iterate to the end, then the last + * struct file reference is released at this time. + * + * @it: The bpf_iter_task_file to be destroyed + */ +__bpf_kfunc void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) +{ + struct bpf_iter_task_file_kern *kit = (void *)it; + + if (kit->file) + bpf_file_release(kit->file); +} + __bpf_kfunc_end_defs(); diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index 1c1729ddf233..b901d7d60290 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -52,6 +52,11 @@ BTF_KFUNCS_START(bpf_crib_kfuncs) BTF_ID_FLAGS(func, bpf_file_from_task_fd, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_file_release, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_iter_task_file_new, KF_ITER_NEW | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_task_file_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_task_file_get_fd, KF_ITER_GETTER) +BTF_ID_FLAGS(func, bpf_iter_task_file_destroy, KF_ITER_DESTROY) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729579 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011017.outbound.protection.outlook.com [52.103.32.17]) (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 4D8781990D2; Wed, 10 Jul 2024 18:43:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636995; cv=fail; b=VbmS7ELqjJuM/hCSb1oJKoJuwhb15WAjsg0BHc9Azo8piE705JqORGnALeNAk0CxBGO2lAzTjqfHBkER53rveunpS1dT5XMqWq84oA/DjgE4BzuguEClt9B4eq3VVeSTU+Zjz0VsOOPf8zX982iPTlpOujKnQ2qMgOiIXNdDSlU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720636995; c=relaxed/simple; bh=cDuPnEMg04Uv1rTocjZFvg8Dxz5QqjYI4XdbER057dQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=AFQyyKTp2BUo2hNUjc3yAcLEKzFkLMRgLYeXWFKwekS7zmM86WQqZhZAFtGZ7rWuSDd0QfF/DB8SFvJ/p4qVM6hyx9lN3hqaEkPGtt5fJZ1lh/VJeAz5NFzBQYLx7hHcyw5QxJ9llj8mGtkYiCSZqMBjRfiCo73GeTnj+Ep1OE0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=elpCludf; arc=fail smtp.client-ip=52.103.32.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="elpCludf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hS2IzLwjoOSGjN4za+F67b8H5Cv0QXzdCU6S6Sc/tXSz+6G/Wn4YupYP4oWxwWAUXKloPqptL90v3adYMyRcKxIp11GknHB3cP7qdhMdge4WiXG+agdwVKahKiPueeEa0Y/tAVaLjhnJYRqa+Vzf0ASXfBjRiX988KXPH4bGCv3wMB0QOvhBk/uND1rZDY95exGIrvNqULIsY4+52X1+gI1U3wWspDAaFUMyUMwAHzmBsAiqt0Rv65Qds4mHgxNgCTgIAJu10p4lAXPlO1jiIdM+Um7YGtNshUe2sWoCyq/MU6xpHeY20OKSdaLGPe0XcgXYegW/cHjPSFQ8IMyNFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=epPvZUVJLFCfITLfmee8fBtxZMqqIsLm5Kg+Eba8emw=; b=keedbXYmDiSiksB60TrpcWukR5Gd2CDy2B2eSzzcB91l2JXV/cwibtyUflp8s010Esivroxjb7Besxnz6UNHmj4g2Ujb00HHS9KjYTkTlxijcufx8f44+sYqnCFu6aPUA+lXZ6BIcR7JFboP6OrTJnuHIVQE1OXgMrNLOBe4azVmH13oiFz4gBsDHff5LU+b6sduxd39JWFMPUH5cwmd5H5O/lSmMHfM2VXCi+vW+qQbzUmfA8xx8WWCqPejGr9y7lo+49qPfcMi/E0gqqyW5MlCCTXtm/kH30N1/DfT1iC1/ybnIFPTGwf78AIhMcjUevcEEa9W+HmFuSOcTYFh5Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=epPvZUVJLFCfITLfmee8fBtxZMqqIsLm5Kg+Eba8emw=; b=elpCludf0Q4yvMtJBxv/tvM9niOVWvg0EFgpTq5Mx+KB0K19UWqd0fT5DC+7JdDNal5R40EfG0dfQ+dLU9Q0Y8rAGtEff70MSckSFm+D8O/UZhtjYDMdBDC6313HQO1X4rhT8nTa5TmduBwfHo25wCz7OVbi8V1nVljTCXI3ugFr1iSR8p/Pt2cHuHgvS6gv7sujDrUtFK7/S9h08ydiFAWCmvJBbWm45SIbqDpcLESF5RzQNf8Sp8LJgLrFzk/QgwP//t4zZFixbLZWJ04+P30sGxqfxhUFj3SdeUT7U9hILZzYL7Ga6yIw8wr+qwGpgp3BS2kNrn5nIl6lr1ENpw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:11 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:11 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 07/16] bpf/crib: Add struct sock related CRIB kfuncs Date: Wed, 10 Jul 2024 19:40:51 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [fokEbX/i7rUmAYuzBTEOim+fURLUgbnE] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-8-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: c6192381-07fa-4324-fff8-08dca11025f3 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: Yh0385nkqz/GMZeEVpvXIMjiSiAFfyrZPtAPRXAZFsYEqtm2Gw2qTA092DQj5JiVDTjQ/Z8MWfbinKGYVeocvIHs+NOXdA6iMQt3/nO4ZbwUAiwB2hCQ2zmbTqamerjJF4ar0dt0pJFgVF+bkk7o+DlizwRuS8Tmen+1Y9Ia8IGkXVXeV1oUFN0uutgM9FvMSisl+5qTFpAXtH9pKt0yGHPiEJIer8IQIXU/t/HOeQZFDDmSeY7y6bzrLlvcuOKMBHAKQ8YAMJRwYBQteFqD4kpPGXMVLbanVxuwdDzQEzaa9MEfMmzFAlNJiWqI4msjNaujVyFklI+dc2Q0BzwvgYRlEWJYdGg79IvGDG//pkzaCydEl/pGfmQL2aj7OYAqKiahcsgqv/kCfj7PfijQzjnpreXMB0G+4kUCWXjc2o6FXQE8X7JdMJRtSVJFuzA2H6BAvNy9X+F5B6IhFQNr+j/tiXnIlwu0hq7++Tk/mdW9jVzBNyjfoQW/dHIqpU3LmzDWDcNav0n8Dx/xssCacY2UOejHatDZZtz4aRj5RBBW7ZuAoPDrNTEEc/yz13CttNGouyuXEhm9nDWALzu5SwxSRmCkLTO5bWMRaE0IDMKyPpYuU4nuV7Btqxjv1T5Ssjg6DmoLAImRSwZRQO1obg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PvifMMyrLj4O7hHc7gDebdyWBeaNKrpvRLFZNwmBThh7gZ1DnB/ytClv0Pr32oFQ0wHWxdEkFEumiElFkVrGEpNpIwQxi3ZCMpFN2wm3RxsS263j/OGbsjTq+BRp5RC6zsvp/28wjU4l8LlzMis5K4Ow0xAOTpGjykF06QIsTsaOn+wr1kon2bPl9+QPuyQdPcaUGK2D8YnhJ/z76Ty9MTyRwBGPpzWPj1+ngWIlYr4wCqZ7y5filbuD+NbKMPc3M4YrKCUAXNHvzklSgrry5DATYveywNZeY8HxLk+FiAO5Q6lPGUXRlh9IoD2hAOJAv1lF0OJpgs+CCer807o8vW0lr0iOg9Up9KG+z7t4QXHQJgGM7uTg6UEXNYaMAXNev3sFbqI29H3cFNEBn0QBSH3tLLZJ7TnS49btodekDZXg/ldljq4ZuBnu76og+jER0yLqqUG9pKM+tAMGc88+IZ2MflzjXD07FjernBa7mnDpeIfa28ylwi+ub1Hnedngs4bO9Fp0alLAs8PCU2VMhnCpGZUncZPsWQwyGzYe0VP0wRrEmLTYxu/ngE91t0LgF+VgDDaVoQJ0a+VORx168zjG1c7fJHBYmSpyGp9+7HWld2AtF3cdA5f6PbC8/E8LfhNZ8vOoVV2sWEqDfuorO65TfWEl5hadtU8tszpV+32Trv2rNywOnGn4OhCZ0ODz3VisPs6QoUEUNSAOnt3HY17viBvjEfdUUKbZ2qUunpAwnItaAvzHhDQYghj0QomrzASw0jETnu17IRCLf+2VMcpDeFhT+veRVECElUcGL3SmRr1R0/3siqiBnJh/fOoAt1BCTjCDlpNWh1+EGf9MzivuBwgEUD+u6k0FcpPa0id3qQrzCUWAVB1tffk1D52Mowj4n3neGIwqxidDt49HkTvlt3mKjzhTF4Yp4Q9+fm7mkXTaNI7Gz0YeHZ9/wkMXI1x8t5vGS18U3DKVKh6a5h6eClkEyUNatHhn0YqhTngr++htTRAw3L5ZcRAyUjY+wKUKWTASNBFDFx2sD4GeD7M+mIa8Fv57XiaNN4EI4wRd2IxVXOcH3457gmtx2jVWaIV6+FMNzbeIM2H5AdqbULys8n9SCIIFBSocvhdo0mFIq2Ug3f3McdKnSW4ZF1gVyeFZYD8mljXXyYGfP6K+YUewQWUE6FcUGteIL4EBZ9uIqpBIM6XY9D8Vdhm9y8FB4sHtOJaiVYjs8DuFJarL+aArMjnC/j2JKnNg4ZukV2c= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: c6192381-07fa-4324-fff8-08dca11025f3 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:11.0955 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds struct sock related CRIB kfuncs. bpf_sock_from_socket() is used to get struct sock from struct socket and bpf_sock_from_task_fd() is used to get the struct sock corresponding to the task file descriptor. Both kfuncs will acquires a reference to struct sock. bpf_sock_acquire()/bpf_sock_release() are used to acquire/release reference on struct sock. Signed-off-by: Juntong Deng --- kernel/bpf/crib/bpf_crib.c | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index b901d7d60290..d83e5f0e8bfc 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -9,6 +9,7 @@ #include #include #include +#include __bpf_kfunc_start_defs(); @@ -45,6 +46,82 @@ __bpf_kfunc void bpf_file_release(struct file *file) fput(file); } +/** + * bpf_sock_acquire() - Acquire a reference to struct sock + * + * @sk: struct sock that needs to acquire a reference + * + * @returns struct sock that has acquired the reference + */ +__bpf_kfunc struct sock *bpf_sock_acquire(struct sock *sk) +{ + sock_hold(sk); + return sk; +} + +/** + * bpf_sock_release() - Release the reference acquired on struct sock. + * + * @sk: struct sock that has acquired the reference + */ +__bpf_kfunc void bpf_sock_release(struct sock *sk) +{ + sock_put(sk); +} + +/** + * bpf_sock_from_socket() - Get struct sock from struct socket, and acquire + * a reference to struct sock. + * + * Note that this function acquires a reference to struct sock. + * + * @sock: specified struct socket + * + * @returns a pointer to the struct sock + */ +__bpf_kfunc struct sock *bpf_sock_from_socket(struct socket *sock) +{ + struct sock *sk = sock->sk; + + bpf_sock_acquire(sk); + return sk; +} + +/** + * bpf_sock_from_task_fd() - Get a pointer to the struct sock + * corresponding to the task file descriptor. + * + * Note that this function acquires a reference to struct sock. + * + * @task: specified struct task_struct + * @fd: file descriptor + * + * @returns the corresponding struct sock pointer if found, + * otherwise returns NULL. + */ +__bpf_kfunc struct sock *bpf_sock_from_task_fd(struct task_struct *task, int fd) +{ + struct file *file; + struct socket *sock; + struct sock *sk; + + file = bpf_file_from_task_fd(task, fd); + if (!file) + return NULL; + + sock = sock_from_file(file); + if (!sock) { + bpf_file_release(file); + return NULL; + } + + sk = sock->sk; + + bpf_sock_acquire(sk); + bpf_file_release(file); + return sk; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_crib_kfuncs) @@ -57,6 +134,11 @@ BTF_ID_FLAGS(func, bpf_iter_task_file_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_task_file_get_fd, KF_ITER_GETTER) BTF_ID_FLAGS(func, bpf_iter_task_file_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_sock_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_sock_release, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_sock_from_socket, KF_ACQUIRE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_sock_from_task_fd, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729580 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011017.outbound.protection.outlook.com [52.103.32.17]) (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 1871B1990D2; Wed, 10 Jul 2024 18:43:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637002; cv=fail; b=hKCrvg3JIEUpxECLlpLpMYg0LHrIKqihcMUmx30UHj6tLswWUShryyyhAZDoRq12syoQmDjK5o3EjtJphpQGYF+O/zorOSrN1p2we6Mwa3aySN2zf75ZkhlJ5sY0ZjjuEnU+vYH4KCAylsOzCKPIWIeyoZNrnVyautpPN9KFP48= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637002; c=relaxed/simple; bh=51Os35jl1lGplW66FjBCF6qvLrHvOXLZJlcfv3H8lEc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=L3x82vetuwVZsD9o4BBhOI98SeHm8SylEYYpiisZ7KlH0NGrlCxr0xBcULtGRrvaaEo50NzFdLscy0b0sDuTlS/PGFkUqSaTxygk4r82V4yHaAuP8lrbdhAsK0vKrXWxsT51UKCqRIn62o09YqSwiiD+mTvU6BP8cxVZT0TnKdE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=GxBQkwOV; arc=fail smtp.client-ip=52.103.32.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="GxBQkwOV" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KWS/VKZV77GysHO4EUIrFJuwIMe3pHb0JybBz9G4zxJS9nA0agjEkKBye78r8SLmqAIhuB+tkaHG5eOsVCiZj75bMgOdH7ILx2rVhak8mjW17zJdUWjBUrComCruQS6TBlaEZoyClDQ4gd48wWs0tL8f+of7sCXvuqJVx/4GVPH9/9czVqUnOrFb1n6YGZkGhdCcQFe5xz6DfqXC4uVK2QPYA66bX2lRkZ5ilp1mwTvx9wJnACJU9bhK8Jv7O7u7QnGpyQBB2Jg8Mc15+s+4d2ARhpS3E1ne+Ee5M3ossRgCeTC/BACBdXeI76KR6y4D4nowLuLS28CZ/t/I2327YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ThTiG67/N9QE6o18/tB8rU26NfvZheBdUxUgVRFGjfk=; b=eJ09tAKccXJeBtp0IVFfecGrJHqgi5NKmj9ESKhHNx5nS9unFWZaCfOxOClsL6zFSO1j6qCoOd3iwVW3T7pM2cZyFdcLFzlRubbfCrJw2svy1ZkYxHBiY+3Lo9xzy3fZ0VwOGMPnQxvPlcBAC2i5aqEe1BLmRSLsJ6mIYA/XRyU1Pj0K6UcQbIuNeWTnAnQ5ui/OJnDwYPOdsvG6GYhJcV3WmANkUccNYGAZ43uIstpO0YtacTrduNH0GtSe8cg18UuAXI2VQKpALM+QGxzigeEBDH7NxpLxdoQIyellNYah10gdYOv5ltjmzN43cLdLucvaIm58d3DSUHwoZSrp+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ThTiG67/N9QE6o18/tB8rU26NfvZheBdUxUgVRFGjfk=; b=GxBQkwOVK7PwgjH5fPCn8NCyAo3KDojLjkgFc4IKhJKdjwafvI6pHm1/DSJn4794la2rU8jZlXdoDux8d9lBAPj/fsR1bympaeStiZTDWtVf7kWotDWLYXkG15bfs0cQs66W2+5ze6ZfFUD2jlNPwIhJCWAUNLpglG6ZmYVT5KuPsJotGJwVp8IyPqIzgK0VfvI7lD3/RVe9wwKOy0sagO/C5r86mS2wXS/6zjZQ+dBdslHfEt2LdTVOCyZhfYLoVjsnWAViBUHwFVz5qcp54AVW03YlQAvMJMnc1TxRMCT3IPdQL2MDjRq4PRafXdogDFheORB/nI9Hw9E+uLz04g== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:18 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:18 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 08/16] bpf/crib: Add CRIB kfuncs for getting pointer to often-used socket-related structures Date: Wed, 10 Jul 2024 19:40:52 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [DB7+WKk8xi/xbrGwD9FfahlhoxKJoLpy] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-9-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: 1cb34e6f-ebce-4a1b-aff8-08dca1102a77 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: kSQrGsb82LGtZJvcXkRRaFpwO2vIFRtBLRtiTd6hX7SPSpmW/UiAhQ0g+klHSdbhfF32zlFh3wWTPmFNUmH78VQ35Kha77I/YIk9luSXoCV6cbxdWSCFKp7pUmQH1EnqYDo21AiaglMYSm9MXYU/MBXlBO92VNveesPC+safUnwrjeBhdMe5sgAUEXkfX+aJ9LfjkELpb2vHv+PSwj/9ahDpCD+afGzBFApm2myk/SPVzX5zRHpUL+sxj3BDoVhXHAqJImP3WpqPtgeJ3DNTluKd2D6BAMU2jQpoveCqauJLfg3oZECb3rrMNQpywoyk3DEYVkjNF+KeeMkZh39RWXuOrwLtrgXLYubEV//O3vTNgEGw1KqtCJOo7aFPqjO3HsNJKJphnG/F7sTaz07/K2SFgVh0VWoLzpRKpZ/9TTrOWHb/kro+tn4ymO4vGy9y6mRnMNPkF4nQIkg8HWJMjL+B2ZkazwZlHTpq9dmNe4Dim97XuEPfvF6Em3lrGXuquHLL52cLUxEEohCqM1PNGlhPe/ZowpPAAgVNIasIStjTUCVKDy4KI2Wk4gGSITyf+UnoXF1tZeHwwDoloIwHVG3asKeoD9y6tNd902LxLi9T9VmpoDzKj5EEvaYBtamjpf1MhMnI+x1rYnoIq5z1gQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4zHG4EPq6Lv8f/yrLaT6+pTsf6LvHtT+ulshbFlE/RB0Aq9yUdTeguc70AMAcVpJTQRF0n7oA9MM6q/3vnayM76yB104NC4yFfHZX0QoXOZgkzrZjpaZZuCJ9ZWYTz61PDEQn6HCI4+g3Vr0I4fKCUhsnXVfaXvsY9cRxIiT3g1oc8RvrYMNG342ZlRClsfY1z4AfkrBS61BREKndsw1zuyJ8xveXYa28B1jMUh856ixV8M78t+IGAJZ3jX8yhlAfJOUnLalTzWd+0NY5MSCg4vx2Ob4puDV38QNppuh19bijLLnuxH0s7U+62tTKWjyAlxM/UbwhNM/4rjus18O+ciQoEEzNyDWnQXHuzj+DD3TLAm0N89ZPTLaIpNDU97RFgtvLPx/q9tfPNEMvjsYm6XT6dKQY6+/g7rUsZ/U21d8gHycxdGN4cjOVZNnBo8FtTkaNbUIaWkw6rEpmWDw08PH9benHLYMfaKWWx7dcgfnmbIJTeDrZOvejNHRFS8LxPz3qZJQYSjWtWyIq00FN/6KXxKZr41oJPRi+Ev6lFxXTT8Mb8Poh0+JMWRsQfeuaEybY21au+nVWxkBcFZPN4Xpozwn16vdP693/Io07J8hetlm1UjY0TxE4g/nt834pl+p11q1rfvgqPoOf3JBdl/WMyt0473xDhs3kU8eTQvjne3K0fnilQ63+bs3zrhJOnAsVUPxopn7HYIpATphoMRBjF/wFlfjrTUP6LrCJVIUO8daJ9WJeOzz3D1GkCUtcFJHOp/Mqkums0yK8Czl63Gwp297Ltv5KmkpSCGmbqHOO+ycwJ9P0svQ3M8uY6i1t1jbaIfzfc9kvrVwsADK5EmbA7Z3o65oQaDGHk+oXNOawoMOjuX+YSS2lM9aaAunWZSE9xtwAxQVLGGWh3ChnydypoTwJ263CjHqoLp+eUtwWICbL69kFoTcxVRt9xFzkfeqZozLQK6CZ54WjLq+p5Ksy515nf6U89c6aP3M8Q2RgJ0mWuZ/9dBPI+y4//ll38BvfyVC+jh83FcVVNtBnSDd4b/Cp/evVh1OshdJpHyH/k/dqrICbKrzZ/d4JOm0lWgKflknZ6kT3LnVoUuWQ7XVLChahnLu8TZelnFoHOM4LX27W3hxMXEZQImpyMfJcMxr9fDjF5O9oUahyXuTn6ukzOvb+Yu8gfxu/rz3k0b/hRl3OFDfUvJdWjle1eaAugLRA86C0scT9DJaE5ewuoZYKtAzVLgTi2HygZOKOVM= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1cb34e6f-ebce-4a1b-aff8-08dca1102a77 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:18.6061 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds CRIB kfuncs for getting pointers to often-used socket-related structures, including struct socket, struct sock_common, struct tcp_sock, struct udp_sock, sk_receive_queue, sk_write_queue, reader_queue. All kfuncs use KF_OBTAIN and do not need to handle references. Signed-off-by: Juntong Deng --- kernel/bpf/crib/bpf_crib.c | 95 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index d83e5f0e8bfc..da545f55b4eb 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -10,6 +10,9 @@ #include #include #include +#include +#include +#include __bpf_kfunc_start_defs(); @@ -122,6 +125,90 @@ __bpf_kfunc struct sock *bpf_sock_from_task_fd(struct task_struct *task, int fd) return sk; } +/** + * bpf_socket_from_file() - Get struct socket from struct file + * + * @file: specified struct file + * + * @returns struct socket from struct file + */ +__bpf_kfunc struct socket *bpf_socket_from_file(struct file *file) +{ + return sock_from_file(file); +} + +/** + * bpf_sock_common_from_sock() - Get struct sock_common from struct sock + * + * @sk: specified struct sock + * + * @returns struct sock_common from struct sock + */ +__bpf_kfunc struct sock_common *bpf_sock_common_from_sock(struct sock *sk) +{ + return &sk->__sk_common; +} + +/** + * bpf_tcp_sock_from_sock() - Get struct tcp_sock from struct sock + * + * @sk: specified struct sock + * + * @returns struct tcp_sock from struct sock + */ +__bpf_kfunc struct tcp_sock *bpf_tcp_sock_from_sock(struct sock *sk) +{ + return tcp_sk(sk); +} + +/** + * bpf_udp_sock_from_sock() - Get struct udp_sock from struct sock + * + * @sk: specified struct sock + * + * @returns struct udp_sock from struct sock + */ +__bpf_kfunc struct udp_sock *bpf_udp_sock_from_sock(struct sock *sk) +{ + return udp_sk(sk); +} + +/** + * bpf_receive_queue_from_sock() - Get receive queue in struct sock + * + * @sk: specified struct sock + * + * @returns receive queue in struct sock + */ +__bpf_kfunc struct sk_buff_head *bpf_receive_queue_from_sock(struct sock *sk) +{ + return &sk->sk_receive_queue; +} + +/** + * bpf_write_queue_from_sock() - Get write queue in struct sock + * + * @sk: specified struct sock + * + * @returns write queue in struct sock + */ +__bpf_kfunc struct sk_buff_head *bpf_write_queue_from_sock(struct sock *sk) +{ + return &sk->sk_write_queue; +} + +/** + * bpf_reader_queue_from_udp_sock() - Get reader queue in struct udp_sock + * + * @up: specified struct udp_sock + * + * @returns reader queue in struct udp_sock + */ +__bpf_kfunc struct sk_buff_head *bpf_reader_queue_from_udp_sock(struct udp_sock *up) +{ + return &up->reader_queue; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_crib_kfuncs) @@ -139,6 +226,14 @@ BTF_ID_FLAGS(func, bpf_sock_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_sock_from_socket, KF_ACQUIRE | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_sock_from_task_fd, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_socket_from_file, KF_OBTAIN | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_sock_common_from_sock, KF_OBTAIN) +BTF_ID_FLAGS(func, bpf_tcp_sock_from_sock, KF_OBTAIN) +BTF_ID_FLAGS(func, bpf_udp_sock_from_sock, KF_OBTAIN) +BTF_ID_FLAGS(func, bpf_receive_queue_from_sock, KF_OBTAIN) +BTF_ID_FLAGS(func, bpf_write_queue_from_sock, KF_OBTAIN) +BTF_ID_FLAGS(func, bpf_reader_queue_from_udp_sock, KF_OBTAIN) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729581 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011074.outbound.protection.outlook.com [52.103.32.74]) (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 596CA199230; Wed, 10 Jul 2024 18:43:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.74 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637009; cv=fail; b=NSNRas+xhvOkKd7lN/Fgg4YOw7tAbgNumGR+/YEDkdjaQK84vnGy9nD8LVCcPsMfQn32HrrwXvDXGAIt/6Nd700cgtP51eYFKXlbwgBYcdgoC9VceJCzBzt3XscW3xPbeORsFknRkUh5y0EHCb3JKOBx56UUCookuDdCe3Rookw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637009; c=relaxed/simple; bh=BZ+ao6oDtD30qjRDcCY34g+/0uU9zp5HdwclRLhI7EY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=MROvgsjPDdzm7NI0LekMUqo0Daa25s3+SN8ADqRtv6LvDrEav96z9nbBfvIVPAlvjgXmVjUGNTW3YQYRyXLPolV2udV8FDmVr6Kurx2mU3SL4JXKRKSdrQGGmbOqFEyK0pw41jMp+T7cCyaGvtvH0mXVznSiaNC9mdoThvA8+EQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=gff9MYaF; arc=fail smtp.client-ip=52.103.32.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="gff9MYaF" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PzaZK3Y2FfZZwIz1mMaYnVq9YPiD4MKwNjUeHgiOOVwj+bSXgRVu6yUnDK7n8SRtIKBeALu3cLSg9tbmZswAxqJ9CuA1BCTF5IrHr4OcLGAbVyHjqOAoWQLkLl5NWfbydgrGgMELiQU8K3Y481ojhCh+3++HP3hjzsB1JRmHt4KNwoWeF+Ih4jXIuJhZ/R10Cifsk906/lVH7Z8vrTy9hc2uRFD5jSBVbCfTBfuaLDbTRFbeknlTZPTMsWY+pJm8QJHmZ/ast1F2pEtmwxijqfLB/O7BpgQfBT9bRXR+1qHnPbhJtzDKBkcA9yLvHLbdqAPUY7BRmuMfMMiAzHcVAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=qEUp/ew7Ko4H7jVboFNJcm9Y9hE5ozL9AGtg1Unr0io=; b=ivtKwXEWa+H70twP2g5GtB2t6YEQIwJdYjSigqt/UvsskW7LTKBVPaEg73NjldwLdLScLWgVq5VHYRJkg61VrJqMNloqwuKPeRLPV1NVBN997u9W1TNoCoYTYGPWYpqPZXgR3FiUC3//P15X9NDLPHqT5P3lXGM3TstVgsdSRk43zx85tFulg0WDgeqlP15/9dfI5f5ruoJ9yE5GbjGyebT9TTn/n5RyvPBwrzH5oVIuBq4Q09VLKZu7fcyLg2n4wcjFrSeo83D742csHk6GPcBr4S3BXQup8OIZfzDBOns3c7DQVHGiZTu9tMa37JywDRAroBZ1CvQPi8GfNF0H3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qEUp/ew7Ko4H7jVboFNJcm9Y9hE5ozL9AGtg1Unr0io=; b=gff9MYaFIJ31GMgbB8MdSmJCv+d5MnTveBolr0nj86sewHsKcOBzeO0CnXXsnDdjA0T0sSsZYs2SfghZwszJ8BuhfoTXJhADAi3jEBWm85NAetUXYGo7tiHn6T4mYjESVWnzALbcq04QQb+JbdifliGWrg19VbPqphkOBja/k/TRAE3IpiEN68te0aTJRTYfyu/DakAM4SxE7CXG6eHocoFZubXzvmfdp5T9f6WQu6IcyffRmC3CblMKQhAOQM2nz/1pzGChACg6ICj4GNVqsp7P4nMWMyNiTyK0acKKKBlyxjmJe5KtQbUhgLqEKmoZ2G+faZuPkKajcUgn0uHyxQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:25 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:25 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 09/16] bpf/crib: Add CRIB kfuncs for getting socket source/destination addresses Date: Wed, 10 Jul 2024 19:40:53 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [JfDxfI3Cpj/ZvVw/PBq76pHGNZmtcN1H] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-10-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: b9260d70-723b-4073-69af-08dca1102e46 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: QT23+QXJl30IR2w9aSowJoGOJFEFfhtjeAR8Q0KK9OeO3gVFunTgUYqec9aK2r9UZMvVPI+pzxjx/O7PQJ1FU+xNgopDlcn3RXqgCwTnY5SqBgA9Tbjn8F2ztENR1+Xes6GRR+D5OQRZGBSOngm4Jih3DGnTq91cuR5nJP1yQCGidiQyIKYvLUmLMyxtEfkVc/h4qmuerEnDA/uCE33taF5Km0Ohk40VrJZ69GjHIl6svThqyBASx1UKHLAwKDnAoxnRDgkJE8ZcNnz0mPnRd6d8f5w7h11gHd1aDwPxEvjbSib+p6WkzgOr7N809YtGxOTvlBmBmhfV/c8+IAtveHzDbNhYtvfpYdy9PAz1t8N5m69mUkP2tE0PH3IE8wLgWpI4MlZ/bTepmymW3GY5zYYKOMz7wkSCOX81UlzRLVsUDK6dBt/6BMpkqdysp67XMVz/Iwk2S2/gzPg7IeWydYEXEi4SIiC4AiYjWvyTBXxHzPT754/QoMm8xaE4PxEH6xpPMbEg3ANhba/PRnL96AiCUlcowFlYNwFZ8TXD5xUHrSr1FXFTuM+BPEFDGL00pmjSe8OWM21dW1xMHwDdPiBSiQl0x7JV5FkXd+58KxroQbD3/oBdOBF/rSf+MSPu4pc0JrhBJsnnhf8FzHGQOQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UGqW936WEjkiOWvBvIPUoI4LPenSdjbM2U7ky7wB4/BRMbhCsqefzsBaiqiXHbhGZzTxMcMjB+09tuCYwk4PmG8hFsbo9SQqnHcfkM2KsXPlHnQe+H10BZ5W9ldx3T5yQTLQJRuiinpkinW7Pxx5ppVz3++mdqZhIHw5UGqkmsWTdk9rt1MJqdra8QuAZIpKplZbDU2RUXQmtyeGX5bf93+yjY4eXduIhTghWFecrR9phWuvKTftK1tuPr5ZXV/7MxYjOyAyiwLjRmiOIAjsgBsgh/kjv/fNMbjemdJKQmOQNk8bpL9FuUuzYAhXOKLO06mzVgdP5aHUthMdBuynH3/by6L10uLA1bZmFFw0Myif2iLvORrlExHG7UmOd7SJ8VEiSn78aouIQnRsZqseOmJxNNiIX6oEQTE/cU3zt8wXTYW5aVrcQxuVecDFLmynIra1z48rAuzXuKl3j3BeW3Zz7nMcHbX27654lc/ZaKF+0MoPfi/ym2IGndHLCdlJGERyT4j3W+21ldMjJr7/vaTlJfNZq7zlSsHWx8b6hLB0iwNWwTZqks9yTRbnwPQOr7JXbqLnZWyPQp/UOEI6dkVBF7k9XhiUS76koA7NowKPVCeAe4qje2rW7Q/uXKsQcO0ypfPY596pDAUfKbNe7yfdm/nRPpuLz9yFzXOb0v9zEBIVUuQrK9GOPRGdjoL5BGI+fiIzblCaiLNKu1Okm1VgloKJMesQdRpjanDF1lXx+vKrHmNx8EoNqKI+XxepB2uuXajrxCF4czckvo/iu8PD1UuKdzMk2JXSeVhC6RSzo4R5ve6WlgTVclfexGPg0MEJw5D62InSlVdmxyOwfwoH8sFs9Cx5Ssv2nAUOMoZcMUzDV1+ZGldgWWBJ31GuLembyF5Y8K4OBvtl/tnM70o2KRQoFLa4gcGBUHxoWvkh/4r38uIy/0eyhtPck/yjleFlZh7nR2KZ0EomV1itK+nAdKwVryhbTsuq/Pp30h81V1sxqpyY0kwi2EJQkxArgx97iqNbKdBYRwM8DhOjHfUqrObiaXHDNB5S8HMOqXiLTjnTZFw41WoVwebBhcXw0F4Uof6WpjcqsuOzkM7mI3rdF1LEfYLhs4g6wiRekcAAPFHlELtA8LA/EXQvyOP68NTS9/ZX71vKYJEyaXU8crRNGq92Kb3S0HgV1p42jihHsufj/s3us9Ny1d7Vr/9DibFkj/e5GCR+/cqtqag/miHbeabsrZjOh26YNZ61b/0= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b9260d70-723b-4073-69af-08dca1102e46 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:24.9788 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds CRIB kfuncs for getting socket source/destination addresses, which are wrappers for inet_getname() and inet6_getname(). Signed-off-by: Juntong Deng --- kernel/bpf/crib/bpf_checkpoint.c | 50 ++++++++++++++++++++++++++++++++ kernel/bpf/crib/bpf_crib.c | 5 ++++ 2 files changed, 55 insertions(+) diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c index 28ad26986053..4d48f08324ef 100644 --- a/kernel/bpf/crib/bpf_checkpoint.c +++ b/kernel/bpf/crib/bpf_checkpoint.c @@ -8,6 +8,8 @@ #include #include +#include +#include extern void bpf_file_release(struct file *file); @@ -98,4 +100,52 @@ __bpf_kfunc void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) bpf_file_release(kit->file); } +/** + * bpf_inet_src_addr_from_socket() - Wrap inet_getname to get the source + * IPv4 address and source port of the specified socket + * + * @sock: specified socket + * @addr: buffer + */ +__bpf_kfunc int bpf_inet_src_addr_from_socket(struct socket *sock, struct sockaddr_in *addr) +{ + return inet_getname(sock, (struct sockaddr *)addr, 0); +} + +/** + * bpf_inet_dst_addr_from_socket() - Wrap inet_getname to get the destination + * IPv4 address and destination port of the specified socket + * + * @sock: specified socket + * @addr: buffer + */ +__bpf_kfunc int bpf_inet_dst_addr_from_socket(struct socket *sock, struct sockaddr_in *addr) +{ + return inet_getname(sock, (struct sockaddr *)addr, 1); +} + +/** + * bpf_inet6_src_addr_from_socket() - Wrap inet6_getname to get the source + * IPv6 address and source port of the specified socket + * + * @sock: specified socket + * @addr: buffer + */ +__bpf_kfunc int bpf_inet6_src_addr_from_socket(struct socket *sock, struct sockaddr_in6 *addr) +{ + return inet6_getname(sock, (struct sockaddr *)addr, 0); +} + +/** + * bpf_inet6_dst_addr_from_socket() - Wrap inet6_getname to get the destination + * IPv6 address and destination port of the specified socket + * + * @sock: specified socket + * @addr: buffer + */ +__bpf_kfunc int bpf_inet6_dst_addr_from_socket(struct socket *sock, struct sockaddr_in6 *addr) +{ + return inet6_getname(sock, (struct sockaddr *)addr, 1); +} + __bpf_kfunc_end_defs(); diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index da545f55b4eb..e33fa37f8f72 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -234,6 +234,11 @@ BTF_ID_FLAGS(func, bpf_receive_queue_from_sock, KF_OBTAIN) BTF_ID_FLAGS(func, bpf_write_queue_from_sock, KF_OBTAIN) BTF_ID_FLAGS(func, bpf_reader_queue_from_udp_sock, KF_OBTAIN) +BTF_ID_FLAGS(func, bpf_inet_src_addr_from_socket, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_inet_dst_addr_from_socket, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_inet6_src_addr_from_socket, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_inet6_dst_addr_from_socket, KF_TRUSTED_ARGS) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729582 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011022.outbound.protection.outlook.com [52.103.32.22]) (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 92E5619924E; Wed, 10 Jul 2024 18:43:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.22 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637015; cv=fail; b=W0E8+rasyEumMeZG5T4dKQCIS3Gr7LdugHvtW/ySgjQCJcC2qfA+LJJ9SRyWch5c5HxmFAE3rIAyxbAkOqQEKhQlNhU/0touNKKCD1kuD2vTvGBfuJ2xjaSdlPDgFLsJgZ0GzD+DfL5r0KxTYUzfbKvDG9R8LiDuhZ/OLxRcSac= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637015; c=relaxed/simple; bh=HQFydNqnDpEVEqqyYpysIENwzRq083Ki37ql3puGGEU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Lax40pIK0fqC0FsSzO4/HVt1287vn9LsNUVqWCTNSEw3vaLTYfiwtb3X4jaYoxOoRa2hE+bHwA0mC0sqNUARnUDGFwozapbdYdBanGX+BvaFsW2buU4MEa4nOCQQ+EE3JnljsqIo7oqY6kf+ZAUqi9EQZgUBF+To9a0ArNJir3M= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=ruyxM49b; arc=fail smtp.client-ip=52.103.32.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="ruyxM49b" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fTxKjLlX+aImEvcYu45ks+wSdeNmvLI5HHU3zcnPdyyEKMsYXx1RJriPzYjkQuPN0JuIITRUkWBvXIdTfQnqeyZISKYRGg63yQPuUPs5L9+HS/0mAYG0Vy6BbynpigjcBWLo7iALkxLD5f5AOgnrH+AxMXl8dzB/ebQRhjWeIBYsWZfEAohGrT2Nk97qZlNSevsTUXnBLUPZKEc32Dnd4V6FGmfbcplZcn8AYivN2w/sw9T+nEbhJqwrfwZtwMWB4oHWwU02a1Id/bf67Tw3ln8ZJx0WZBz6BjfvWF9O3g0eZJd1N2L1ylSlOTeqggPEdFwLLpVzlWbmi+IngLo4PQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=T5nHZceVkH3kpV0UIy7v/XCBmKwfmdeJyMCpuufML9E=; b=MWLdg9Xbm1brCdDrabsRfxEeDxBZsvwSE8Lhx0+xrri6+niGwe2wKybkT2EKkyv9KRZED4hx9HBEeSqniYvScGgGcmKqWa6T2Xsb+C4HLSISVi25QX0womvsGrjwOqnYf6KoGcIMhe1URmYM6tkkC4HEP42w0uifDnGtUch6nAaqjI2vMiSetbYlSqUqlzLfqFNyISomrQFILarBEOy880JY1EK3HvSctoBNG5crnp4WpygLiq4YNaWnT6/LS0DMpqIFZpOEOY9gjna+bZeRxqwK4BXTnW5h3RsGNbG3MiwoLFQTitwLQ9X7FcaSoXGJxRrH3Y7p582xvC6fYDTn5A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=T5nHZceVkH3kpV0UIy7v/XCBmKwfmdeJyMCpuufML9E=; b=ruyxM49bUBBVe1ohWdMMG7Ojmr7JQMdGCVnG0B1QGZEG9/Yu3ze3W0XYiqsuEw+Bq8d6VXs7wdHvfMQUB12comIa1uHomB5XAKYGEtSaflY5fjwUPQfoHYX1BXB2OjK0STNsfXm8uOtslWLzmvjYfqz9GZ9G4R+QyUtxG3ppVDfqi/yudqXzlAdk0YJenPdo9Q9kp1+wMm9cipA5uyfkYQlNXDt2M7xmMcPw0NAdzafDxgJBu5mVXcTmOm468T4S4zF6eyT70IXbnX+exAfYpheAyKqWGEuhXGEnf+/4U6W6xe4CIsMqVEp5ML4Wqd/YX381VVyGzaL/Ps96bsyniQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:31 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:31 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 10/16] bpf/crib: Add struct sk_buff related CRIB kfuncs Date: Wed, 10 Jul 2024 19:40:54 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [5RMbQ5sfUP8c0lp1GsFryvoPtvWlVvsg] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-11-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: c2e4a7e0-7088-4ea3-80d4-08dca11031ea X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: q1XQ8E+V/T9uX7cBeSdsClKnh4on20C9y0Qzcxinp3kasoEhJolgJL+5PVSdv15/hHE/850KFcw34TIeupPa7yjM391ltVxTJfKnHQL7AejLxN/QggMz9Zf1pzO40TPC33HjW6iPK3vvc1PESoiW6gc9iXF853JRmnLcYtAz/B7ltb2B39xoagMxRRPUeAiceB39pHoxDpu6s4aCk9sRTSfSobOJ1rK8NqObYH9j+OeZJLA6M0i4w6Or/yh7li0BSNliaZS+BBDJsBuRj2ujihIhNPZd53YDNhC4zWZXM7b7S20V4oQ6EDwlKbO4ehzY6QkfWLpERbY4FKOJATFRfrGaXdCrT6R7BK8ycub8RMxIhkn9DUFrHflUfp0SOZkj7hjkn0+idyjnWzKiscS1JDA0MdX/hRBUYb4zGkZfcMJwnxwQIFruULi9Vrifg0Va7AvBIbG+aBXoU8LZxQR3fLgzcFT6a6dUUOZEaBDgmDJUrfk+vJGw+pNujln9mdIwJ/znKzkB5iYEYQOnYwSzEK5pOR667aqVs9Zif7txNnKGID1jBUJkBrtq9hOJYkrRGaCk/sV0bIZr2zF6dnws6oNulrKcsJ6581dO1aJ8gX+5yiMr5oA/XhQw8TxUlBaS14Fs5ochy7loVQ14q982bg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7PM5Dt4Cy+NIGng6n9SZwvFj6I5spQR0tDh9Vfjc3ki5rmPrsECV1/8qT9AiVKVGVLVDv188pVpdjF+QHYjBGdAQgR5EIXCOOOjrWXQWJ0E5r0hjlDm58x40Nv8KnkR7GHBnp1RCrAZMcQmkRNWBvWPDA3NwNGAEETvyhPFJg1EtasLgeNTaeKchs5uhvUxFG+Pag0xZBFtfbfuGG4lv/HyKmCbNLUy/vtQHMkLWvpJmYdg2jxwcrTuKWHgJiSuvd/u90rANqgfa9AXzZ+2mNuazi+Ab6qPo0o7ertMk8329AC0UUpapDK9pG6LM7zv4pF0eLK7bcBvkz/TOXd8Qj807p12oj46Sg0uLPoG30kLqtTZSIp0q/pLlVLg7y2W9tztPnLVFPMK7MD6OxlVoDDWMpbfoXktMCtfadUJpxCyPST0rIDHmLMDCVPNQZD8tpHHReV7IUy1aNtWNlOIEzEjvyTXkXzzsLOyx3XnH2sBhew4YJ8mFm4jNTFqNIIIW82yU/+naz9Izt5pS4fYcfNdbUagPhOhoV4Tre0OiTbhittwBEVDJy6DRVIfldqmsbckcjbSziMtZN22laF0m4SkepwRkUVsykq7YfpC4lh1FIXDCiVQx81C/sdN3G3wG1iC8oOeHKnyOG78N68xX9EVC0/DMlixXbYy5BTStuE2ckBEOgwgqJu9KK9MZlHeQCH6XZTuOohZrV9hI4N49JVRpRXu016Ub0d4GFqfNjsCKppMlhgSQ/zlU++fJXFrAioOF4zqZhk4ke4dShWuV/VFpZSi4SmjxJaRw1G/OFweqIe73aiffY7dZvjytdGBYbyK8JUuaURhsmDLgIIwGKXLby9/b4sNN+BWMFf9UqHMY4PlAeoD+izk+NH3hF/LNMlswZCEy16kSr2KSZqfnQFWvuuTnvaL+IqLkx+PfhsvFQ1TK5dXnjaK8ukfAIUOrALSl6zKChgxW80HNhtiw/wK1t6gPvnFx+Q1M8GvelPKt0u0ykHD6XvaFlxosVg968YMYGwx7/onB8sT5pUMMzUEgrkHVtqh65YI6a9BV+zZ9vkzL6kKLZ+KsGUHE2qy8JdyqMB0NF3/tpo/fgpZi666HuU5nIGJdLW/E8drNdJtpG+aEFWdt0hAz/fomkCh8nZNFHPTU9sXXvzgkyUZHSPbiFIcUMap/FayQPsB8/RdU4sTmWrM6NDmYUadTOZ538+mk8/x2Ti79vZTW08rZbXEnmotSXiv44w3jvlYIRVc= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: c2e4a7e0-7088-4ea3-80d4-08dca11031ea X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:31.1665 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds struct sk_buff related CRIB kfuncs. bpf_skb_peek_tail() is used to get a pointer to the skb at the tail of the socket queue. bpf_cal_skb_size() is used to calculate the overall size of the skb data (starting from the head). bpf_skb_acquire()/bpf_skb_release() are used to acquire/release references on struct sk_buff. Signed-off-by: Juntong Deng --- kernel/bpf/crib/bpf_checkpoint.c | 14 +++++++++ kernel/bpf/crib/bpf_crib.c | 50 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c index 4d48f08324ef..d8cd4a1b73dc 100644 --- a/kernel/bpf/crib/bpf_checkpoint.c +++ b/kernel/bpf/crib/bpf_checkpoint.c @@ -10,6 +10,7 @@ #include #include #include +#include extern void bpf_file_release(struct file *file); @@ -148,4 +149,17 @@ __bpf_kfunc int bpf_inet6_dst_addr_from_socket(struct socket *sock, struct socka return inet6_getname(sock, (struct sockaddr *)addr, 1); } +/** + * bpf_cal_skb_size() - Calculate the overall size of the data of specified skb + * (starting from the head) + * + * @skb: specified skb + * + * @returns the overall size of the data + */ +__bpf_kfunc int bpf_cal_skb_size(struct sk_buff *skb) +{ + return skb_end_offset(skb) + skb->data_len; +} + __bpf_kfunc_end_defs(); diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index e33fa37f8f72..21889efa620c 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -13,6 +13,8 @@ #include #include #include +#include +#include __bpf_kfunc_start_defs(); @@ -209,6 +211,49 @@ __bpf_kfunc struct sk_buff_head *bpf_reader_queue_from_udp_sock(struct udp_sock return &up->reader_queue; } +/** + * bpf_skb_acquire() - Acquire a reference to struct sk_buff + * + * @skb: struct sk_buff that needs to acquire a reference + * + * @returns struct sk_buff that has acquired the reference + */ +__bpf_kfunc struct sk_buff *bpf_skb_acquire(struct sk_buff *skb) +{ + return skb_get(skb); +} + +/** + * bpf_skb_release() - Release the reference acquired on struct sk_buff + * + * @skb: struct sk_buff that has acquired the reference + */ +__bpf_kfunc void bpf_skb_release(struct sk_buff *skb) +{ + consume_skb(skb); +} + +/** + * bpf_skb_peek_tail() - peek at the tail of socket queue (sk_buff_head) + * + * Note that this function acquires a reference to struct sk_buff. + * + * @head: socket queue + * + * @returns pointer to the tail skb (sk_buff) + */ +__bpf_kfunc struct sk_buff *bpf_skb_peek_tail(struct sk_buff_head *head) +{ + struct sk_buff *skb; + unsigned long flags; + + spin_lock_irqsave(&head->lock, flags); + skb = skb_peek_tail(head); + spin_unlock_irqrestore(&head->lock, flags); + + return bpf_skb_acquire(skb); +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_crib_kfuncs) @@ -239,6 +284,11 @@ BTF_ID_FLAGS(func, bpf_inet_dst_addr_from_socket, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_inet6_src_addr_from_socket, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_inet6_dst_addr_from_socket, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_skb_acquire, KF_ACQUIRE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_skb_release, KF_RELEASE) +BTF_ID_FLAGS(func, bpf_cal_skb_size, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_skb_peek_tail, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729583 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011018.outbound.protection.outlook.com [52.103.32.18]) (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 337C619939E; Wed, 10 Jul 2024 18:43:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.18 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637021; cv=fail; b=Or1sxWwTNBLb2PlV2SuCPWqSO727YfjN9KoyO1sWOaMmVfqyxLhl/3572T5EpH7fuotq4K8gMxUMu5qy2yg2T5EsS7pDSJWfRfZTJKnY+VgmfQqGLF8wMW2HDrdz97PCgblWLp5+7Lbo4TlSk/6ChsMVXo0MOJ59ZCnsukXYE9M= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637021; c=relaxed/simple; bh=FvHTQDE3p16wuWz9aM7RjyEAYfk1Q5AMjLIflanPGJc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=fxXs7hhpeHn4KRtkYOym12EM9OzldBHbOGS3nmhJ53/VzkTUrTAoTqJsocC9tPZQek9kmxT5ic3lF+0jFUgRO9KDkM/V8e+sYZmCJtu5NRePR+T+LLfNXbeOlVCxFW/sS8iS5UDtae42EQuEDfU6HwOXbabnezFollleoNsyS0g= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=IunBHVvS; arc=fail smtp.client-ip=52.103.32.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="IunBHVvS" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=O6bRnaojVU1eT/Y0IAwBlt8mD2815Ou1kf3wPd9dRmwbh34IwDxVY9T56aaga9tSWZNJGuk2gWcyBE16lAtr9fuQZ0VtspzsM3EnkvXRKXaAy/gyuagiZrJXU5u0dMXe3voDP48OUFx2wKCL3HJopqC7jvl2YDvSkeTtjUg+qVNSDoQleDGtA/F3JVuqvH44cYpevcnUZo3hpNIt9+ZQpyDryhibWu0cai7exrwUTWNIUhGSDlWNkx5AZXfxBGsnI2PmprbW0OJlemPqI+sQtB1xlJc4CFHXU3elf690ko3D6wSvBsKwtjTZm95YcX88G/q866d4U0vkSAOdrvOyIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=5BuvugXW+Fdk3yqLfSThp7L96+nhopl+ZSPgDBW6qaY=; b=FJgBpWgOzwvyDBy6aJcLhLb7hzd8U7XW9pQYdySZRkzOtjyfpwDRp7h0Nvt2UToRqillgkQId06WYrsUof1+cRvSrc9Su//CMEbQSZsaCFYFQOVuuU3o7dfIERqYVwlJgjgZpER6iIVrgphgeqRdNFZ1CupXChKT1L51wWqBtC3nyV9iY+2mGYa8zmx3WIn449G7gz57XQHfHBW5HUaLvFfB+H3oI4XPOpRnx+9z9YutSJj9w5PnAn0Lr4cxzFdyqrs32F37NZ9uG4mkzzk6cs8J5qLm+xclMu9tqoudItV9XSXefZBwEerVXrxQtCBNrkgufLqDbeDAH9FLDleLHQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5BuvugXW+Fdk3yqLfSThp7L96+nhopl+ZSPgDBW6qaY=; b=IunBHVvSedOaJ3g64mDGmhEkwvkOjlk8QoNn4VVi408JLXCjvGLRIibevopESEfN8xEa61ruAJ4H/vlZ4tjVpzWw5noFZ0YVLaL9vSRuwC/ZEZSBYQoS266XetWFU3ZgBtG/yfR6d5908OiNda0WQQIcqDGBtfGYhWotI/X5vXygXG8iU4fI1uL/mr4vrePS/12+VJWBqSN6Q1OLmpLZqlgLKgBs4efIvtuozY7z1r963q5dCZAEYm4u7K8w/n1b8h+AXnTVtBLuvQoPYKGoXb7vCToCf4N/sQMTOD/Rm62U58Wud4cSjCzoSwd+V0HeSuN8NOCmuk10q7BGfUyNHQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:37 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:37 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 11/16] bpf/crib: Introduce skb open-coded iterator kfuncs Date: Wed, 10 Jul 2024 19:40:55 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [jQ5rzj3ixYuWcjReUsk3G+ByAAC3Olgy] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-12-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: 898f276a-90ec-49d5-803a-08dca1103574 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: g3UqrDqLpT/ODVTI8YCJiUz3g5GR/IwykCHoR2k5PyYk/Zhks26GYM4xWdHQ3QMFAhsxCEIVVQZGq5VJWILcTbYRvunkdF70Pzy8lq+kHEkapG6qpbDKNgRkl/ANIeLSNa5+vnNt6GhmxKV2teE5Pr9XVJ6uLIu6zcldKpZ50rwpxlgFEldS9j5F9TpaKG/4LL9r+w/dXX4Yl8/i1r8cypWkfonSfLZzpHhcybblJ6XuWBRWenAnwZ2NTzDwAyTeLxTSCQiw/zSsT8RaL9GcY9VN751Er+Wpb6JIsh1rhjdz0Nekfcw0cK49f0yShQgHzaMWP8TOBXqVUohAj6Bjxn/h2ipyM9R/tLVh84PebfZ/iPT3vigom53ZBxxyQpP+KFjrzGE7qworDx4lp5FIE5lG0O8LrITf4C3CLh7dGa3VvAZr7uiBaT5hJ3VRbbnJN1amUH2VliOPDpLPCe/YeyMP0j5jxIgA07rozgR9qYWNuahV73Yfu0PKDU5Y+ccE6YAB/yXtJkMoEWyOlNfCWYUjSsIsNlngbE0PLiVaIZ8y7xsOD6Nh4W/yl33b8qlhQxC4sx+Oe43ldaGMpfe1sG3VIA+NCQuyiScC4Zg1dthitY2GPhUI2MedQ6XBMusqWkRv3lZHr1RRhZwVpVo/iA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Alo70uSdV8G+oRbAssw2N5Tp3lMc6f/LubAwAny04RFE3Gz4HSH9OLvkSqp42U+jIc0Nx/F5DvdDrKu8rV2hbhaVQ+lKPYIL7cnzMyW6wBgMv8hotokjK3IIejGGdPYgJuUd2ddF5WFcAsDvgZROYKyaeKXKJlTQCdHcMbV6pLT6Ajfdy8rkSAK/d8oUmfHfyvWxMLhplO/hpEWWXZRvTrdTzjR3AG5+WrGosu30msQhNMkKOMOA3qw6ij6lZML1GBkxZ1GUIxOzeU6UppCE1CjadylpreuNi3PtFi7ltxMjYzcjmXBSxkLt13OW11e2Taf0NE/cT1RtGqNzq6mgJblJ5ToAfXIz7wgDAITWZt+w29feVcGshV7v9bal3zJdZZgPVMvnjNWNIXR8R/60/WonNkn6jTnEF9U6bt1EAAg57QlNSvUjwp8kco1Tx97PSMuw0X7N97JfCBHbiORxIPA18lQP8kEbAjxFGKNe86Y+CYuh+RpkJH+53HsvyxLfG/fN/AgmjdwwkDK2YR0jXmdNQkHhSRyLzNusyxq2Fwid+zdoa4+2hElCbY9NBoXWYOV0EKi/TU0YqZis8csiuSKm5bNpzxQYsNzwwon9Zry2fyEGe6QRqNWwluWfSV6jptLGyqFdH5yYOHwIcBDK5xUzIbblMtLgjyzCIHhQlZlNFvUW/Bk0zkGLQLvYkbstljMiOX6mtlVrWpjARpbXTvCGwQ//iMAmVAHIw2CkqwPWS2VNdByaJqRqwbpuuCaxnRh0XNyst4KJWu0xUTmbgt9/gNTNRSw3GQuZQS4bHUkQ9eI5bd9rzv/PQ8Fk+V+vyQ3OFK1NMmAoGZmdRGfkqCZ2VoSWKMLnEbcPc7uqAjFPmj8keLnyMQ4zKEEC9iCkg/xMyqCYJS3pTTOTwts2BwI4Byl8KZQiI6xBInJ9jGl+9CINGaGKywBvDS3HNpW00lWg463HCBMRYwpsuy7Kst1FxSkhJJPMMuQgIj8Z2MWwS7W1pQACoo2RLPMlTw30bpTnyXU79fYfhmwqQsOTUvjDlKg1C7QaV6dW3r/EebeTrfthIHbfAj5SJWWD6dAwCIRTliJ0CcH5bcsOp6aQVQ2ZTb4lm8e2gI7X7QESoG6an40yNaAcull+kLfm40soOj7z1rQau0jV0/ZU0KuQdfAqobMpQPX5luzbF1vrsldqKWimqRRh5F8exVJwl+HMDmKhLUZU1cjTClLtoDkyW6e8dIJnS/JDuKh2g1oFfAA= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 898f276a-90ec-49d5-803a-08dca1103574 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:37.0293 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds open-coded iterator style socket queue skb iterator kfuncs bpf_iter_skb_{new,next,destroy} that iterates over all skb (struct sk_buff) in the specified socket queue (struct sk_buff_head) . The reference to struct sk_buff acquired by the previous bpf_iter_skb_next() is released in the next bpf_iter_skb_next(), and the last reference is released in the last bpf_iter_skb_next() that returns NULL. In the bpf_iter_skb_destroy(), if the iterator does not iterate to the end, then the last struct sk_buff reference is released at this time. Signed-off-by: Juntong Deng --- include/linux/bpf_crib.h | 9 ++++ kernel/bpf/crib/bpf_checkpoint.c | 79 ++++++++++++++++++++++++++++++++ kernel/bpf/crib/bpf_crib.c | 4 ++ 3 files changed, 92 insertions(+) diff --git a/include/linux/bpf_crib.h b/include/linux/bpf_crib.h index 468ae87fa1a5..e7cfa9c1ae6b 100644 --- a/include/linux/bpf_crib.h +++ b/include/linux/bpf_crib.h @@ -23,4 +23,13 @@ struct bpf_iter_task_file_kern { int fd; } __aligned(8); +struct bpf_iter_skb { + __u64 __opaque[2]; +} __aligned(8); + +struct bpf_iter_skb_kern { + struct sk_buff_head *head; + struct sk_buff *skb; +} __aligned(8); + #endif /* _BPF_CRIB_H */ diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c index d8cd4a1b73dc..c95844faecbc 100644 --- a/kernel/bpf/crib/bpf_checkpoint.c +++ b/kernel/bpf/crib/bpf_checkpoint.c @@ -14,6 +14,10 @@ extern void bpf_file_release(struct file *file); +extern struct sk_buff *bpf_skb_acquire(struct sk_buff *skb); + +extern void bpf_skb_release(struct sk_buff *skb); + __bpf_kfunc_start_defs(); /** @@ -162,4 +166,79 @@ __bpf_kfunc int bpf_cal_skb_size(struct sk_buff *skb) return skb_end_offset(skb) + skb->data_len; } +/** + * bpf_iter_skb_new() - Initialize a new skb iterator for a socket + * queue (sk_buff_head), used to iterates over all skb in the specified + * socket queue + * + * @it: The new bpf_iter_skb to be created + * @head: A pointer pointing to a sk_buff_head to be iterated over + */ +__bpf_kfunc int bpf_iter_skb_new(struct bpf_iter_skb *it, + struct sk_buff_head *head) +{ + struct bpf_iter_skb_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_skb_kern) != sizeof(struct bpf_iter_skb)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_skb_kern) != __alignof__(struct bpf_iter_skb)); + + kit->head = head; + kit->skb = NULL; + + return 0; +} + +/** + * bpf_iter_skb_next() - Get the next skb in bpf_iter_skb + * + * bpf_iter_skb_next() acquires a reference to the returned struct sk_buff. + * + * The reference to struct sk_buff acquired by the previous bpf_iter_skb_next() + * is released in the next bpf_iter_skb_next(), and the last reference is + * released in the last bpf_iter_skb_next() that returns NULL. + * + * @it: bpf_iter_skb to be checked + * + * @returns a pointer to the struct sk_buff of the next skb if further skbs + * are available, otherwise returns NULL. + */ +__bpf_kfunc struct sk_buff *bpf_iter_skb_next(struct bpf_iter_skb *it) +{ + struct bpf_iter_skb_kern *kit = (void *)it; + unsigned long flags; + + if (kit->skb) + bpf_skb_release(kit->skb); + + spin_lock_irqsave(&kit->head->lock, flags); + + if (!kit->skb) + kit->skb = skb_peek(kit->head); + else + kit->skb = skb_peek_next(kit->skb, kit->head); + + spin_unlock_irqrestore(&kit->head->lock, flags); + + if (kit->skb) + bpf_skb_acquire(kit->skb); + + return kit->skb; +} + +/** + * bpf_iter_skb_destroy() - Destroy a bpf_iter_skb + * + * If the iterator does not iterate to the end, then the last + * struct sk_buff reference is released at this time. + * + * @it: bpf_iter_skb to be destroyed + */ +__bpf_kfunc void bpf_iter_skb_destroy(struct bpf_iter_skb *it) +{ + struct bpf_iter_skb_kern *kit = (void *)it; + + if (kit->skb) + bpf_skb_release(kit->skb); +} + __bpf_kfunc_end_defs(); diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index 21889efa620c..fda34d8143f1 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -289,6 +289,10 @@ BTF_ID_FLAGS(func, bpf_skb_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_cal_skb_size, KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_skb_peek_tail, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_skb_new, KF_ITER_NEW | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_skb_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_skb_destroy, KF_ITER_DESTROY) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729584 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011017.outbound.protection.outlook.com [52.103.32.17]) (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 7AC08198A2C; Wed, 10 Jul 2024 18:43:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637030; cv=fail; b=ASF6LwRKNQCrvHjxBhE9uwiLSGiUZ7v+wGBhfL22p6RaU3vxaBZ3fvr9z0x6fpktyNozY55QKi5JQtRBWff8DZZu7KudF2SYSshlwmMMN9P6lnVwar7txxej1GvwQ7YTbHFrcMc4mtzQdRE4uKyWk+dH18sYLCZKSgx6FHAy8Nc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637030; c=relaxed/simple; bh=b4E7pEvHSlooW5kM/GHXqHjYvTC2iOpkyNSk6ILcu44=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=D0qa4DKnZrpyshh+ihZxWxYeB1bkH2CZvaeu39oXyHKKgfTVUezR2Q4sNatPJ8t9r7l/zXOgZiL+1YhoJMdWOaEXwcyIzBV8AYKpaMpx6zTHAc39xHsBKAQE3Z942UfudU12rgbP8KG0zgL+t/hAodapCONhsM3yXMPHPeG/eR8= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=fkEMjE7f; arc=fail smtp.client-ip=52.103.32.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="fkEMjE7f" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nXJdvDQEToPzGc3vFucEZatch2RyscTjeVbYbIIASgZmPqv54ZixUag8lIBsB1HPLpr+8Er1IydbnAAG2qnBmUbEBMl137NjiPAqejKNoUqWcaw2shJI96sGzfPzpScEZx9JyJxubPXM5bciUCLtOhAjg7lPL1jgfc4cWph6TaOe4xqkfDRvVbU/rR6/04vTjFImfvGO+A5r/DVEpE8HzJiQGbJiZamDyr6tfjUJN2s5dzWwrx3LDEAr63t/fT320Y7GoRb17CkisJcChElHPjq1h2Vqu7LQ3gWOERJmFAnO82UjV5+CV8nk/h2Sq9vJndlNmZmK1dP3iqdJnT+a8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=j6i4MicK2b/3Q7z+U/fTVZjgwdZKerVi+nFpvOSivug=; b=nj0UqJ6AXhWKqIKGM7yx5awRzB4T2rx+3RxLwSs8/RsaVy3DsCcRn4O1YNwRY4K+eYUuFde8YdcBQY7C2s/RUPKSS/EKzasDLVcYWjeMWDe7G41lZPjMOlglAhCWf5PeWuES2/2AfGkGL+87J9AbTckrJ0scJGcG2d97ULCAIbB0tp9zaLNSbYvuIdveX04nO4CHGpagrx65vgM6kFGri5l3pTtbOV06vqp4NSApF95c465Tdp0x1Nu5qWXQxqcjeo3Vj9cnqxZLqOx22AucWcowvqrcLEldVSd+iPfv0odgdnWQzeWARO8fOCwQ5Zkr97hrAxVPLlky3zuH3IqwOA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=j6i4MicK2b/3Q7z+U/fTVZjgwdZKerVi+nFpvOSivug=; b=fkEMjE7fZmz0CCRua2KtLXm19gbZd1g6xeyCjAGQzr9KN08a7kLEJlHtCWLxMhLkTSQzIMInbG++yiJyV6oNOttCHA25vMkpYDUxzTzKUOt7UmU21Olm8QFvGPz44izS5j3zNrb8HdEuxKuQ1KVOwfNdM6cZDe7GNWyoqteFecdSkXeaStXHvHANME84BWcREpO1eAQII8ncLSKLk2cRQveM2kBpqAkHDf/A7Qh+KDVh9UNHB7wMFB+WjMwWKS4FmCUNe/BN7q8FQEouOdtfNjOb0BpZcpkwb/bZ0lEnhNsaczn+tuKN6GuijDncg51qi+FDBNrh0xTek4H9TILuww== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:44 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:44 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 12/16] bpf/crib: Introduce skb_data open-coded iterator kfuncs Date: Wed, 10 Jul 2024 19:40:56 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [Wde/bjNnppQAvSf2dE9aynfWEd+9nTaU] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-13-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: a37f28be-2276-49b1-b93d-08dca11039ba X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: FeO9hBUa90zzGdAqOagJE3WDrYV5TMa3qLW3U3Xd4bRwxUWVb+cVhrVTiyCh/V+TqfEDIO1TAjy2LJFMDrsOmuzRvfWvuOH0UJWJhfZh75svEJ6/VL18hucY2hFHXdoZWMnwEzjmoKqPW1Lt8UfmKoh5xOsPRZ0w4jJTtxYNGp/PFQ5gmee8JNnltLnegw/NBQLStyjQ6zzreGzarBi/ytT4+wW8LZCNZaxPVoL7HO8AHyQkTRLcEFrYUzifyDKR5WQwazy6fcyPUyemtT1BAs+PjXk/kWt30EzBmJkuDZHClIxvnVBubegxV6AXbPRqqCp12mt1TienZL9bVvOxgxLJCzT8TTJpYF/AtgidvRAiuLOkn/k86bzcId8jemO2PKuizNbRxtzcLiYPjoLwAqyl86FmQABAHV868zkfHT2FIUoKH21Sggm48a63aGVasCUX1CrBbM70uy0yAobwXlTS+i+ibXA0bI6M23VP0wy6OzW1Yrr01D9qrZjPTGW+qgXvVZAH5livpn0wBMaHxGc/oRJbYYYV3EhQzjZaNWwlkskRbfNZF5tGBL/FCnXI9xEmUEdluFJ8FkqBdUcXVrqQwYu7S4W/ONdFXu8Xd0D1DyT9A4ZgQYfc/G/eDjOLxgqA9SyS1Mf3HDUPN0LuFQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: w4HEHyI4Vk+mhufZ/wtKvwEwFIJpdGLOIn3r6g6+bTer2SeB6pFVm6NN6QgzZZ+NYOCzHHQmsn7XGxMSITjfpCmdLWEydZjLz1aSyZE5g/UO1VlAsAv6qVRZZvdkE2tnvFuDgJnf0B1vpaxkOVZIEL1PDCfUx+RQeHmgYNgU3lkpVWnH1NmwWqs/fsW7gcp9g8EQsLSFpep3jtTTHEe7xaLCW4KcLByuaASRLJot0l4Ee2OjWzO9Ew/RI83dbgtxxVVd2KHD1mqRiGBonVYGqLotbXolgr6ZR6NXdFfhUxh7jvHhIAN0iQuECvLS+OWrXsvCzRlG0b2OfRovPmr5rGwDyQqSPZAyYm9000MdFuYc9IQ3t4AWGMrhdMiL3zXVOzyBFHwvBTdGyyP9cZ1W07awk2ywBQGWVA/6MyqaPqA+1RwTUc9J+Lvg/aaU0rbIyW4bUczIYiu2QHRCAGMJoE/pInrF9BBUCXXt4MZJyOZVeE82IANxjQHpFl4nrM5iRs3gbfHNA4xlT+suVnVdw1Bqp0j17V+gyhrF6/Te9o94XOLyBGS8qbmFpC3l+e7JIAO3GL0nOuxQq1UF9h1kcyy7jQhzNafA7lv2+zvvyfGmuv4+jr+OBDwpXnqdQRU/A0ZHOEdQS1KZgZNOr9LmUT0mWeEiRwyGRKhzLtvxGQ50oMa2lwV5Q5CCgQzv6PfY+h9CpYfxWoNeCq+COCTNNLsqCRu6I84jHFhuf3+MGMRf366fc/Lgn+eDrlDRtqntCyZ5lUGtR4n1hXbh2wLcd5iPFj2ueLRRkJDPuM6QDJMZHrrLoD4fTGhrzI2JQ+ZNyv5+nNdkj9zpqARLXNMyaR7NUXhWLgrmuYG7cUeBEBMeN6tTwYh2WWPt2gzgVJ3LLCalrWc2YvkmgvYp+PlDDISNI8CYvtachZX471ff0IAa2NgxxsjtgllcU32vKLcj0AdYaZPgxKj6/7CAhOPRBvIF9u4PaWVq+cu0J/9iao69x/pQ81linNp/kbuBAqm+vjOC2LXBhHEzxaBcn+nh+Oloy95YUk4P5SULFNfvEQDsvPEawyQxDsHln390NwNz1RKPBU+AeKxV34q4AVvUUCXpccgct0Ag6dRWi6NnpaD1Z2bPeEM1FF1RPSBczUHoSNX8PLhMWuwg1WCXaYIYOdcFcbxn6SKyevjQ9u/HqfAEN18TnjP5nRmOTi3IRL25YrOlfjO+7oZ0/2M5nLcbrUpTA1u7U08jrpotVdDgzG4= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: a37f28be-2276-49b1-b93d-08dca11039ba X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:44.1173 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds open-coded iterator style skb data iterator kfuncs bpf_iter_skb_data_{new,next,destroy} that iterates over all data in the specified skb (struct sk_buff). This iterator is designed for checkpointing, and thus iterates over all data in the skb (starting with head), not just application-level data (starting with data). Each iteration (next) copies the data to the specified buffer and updates the offset. The skb_data iterator has two getters bpf_iter_skb_data_get_offset()/bpf_iter_skb_data_get_chunk_len(), which are used to get the offset/the size of the chunk read in the current iteration. The skb_data iterator has a setter bpf_iter_skb_data_set_buf(), which is used to set the buffer and the size of the buffer for dumping the skb data during iteration. Signed-off-by: Juntong Deng --- include/linux/bpf_crib.h | 14 ++++ kernel/bpf/crib/bpf_checkpoint.c | 116 +++++++++++++++++++++++++++++++ kernel/bpf/crib/bpf_crib.c | 7 ++ 3 files changed, 137 insertions(+) diff --git a/include/linux/bpf_crib.h b/include/linux/bpf_crib.h index e7cfa9c1ae6b..c073166e60a0 100644 --- a/include/linux/bpf_crib.h +++ b/include/linux/bpf_crib.h @@ -32,4 +32,18 @@ struct bpf_iter_skb_kern { struct sk_buff *skb; } __aligned(8); +struct bpf_iter_skb_data { + __u64 __opaque[5]; +} __aligned(8); + +struct bpf_iter_skb_data_kern { + struct sk_buff *skb; + char *buf; + unsigned int buflen; + int offset; + unsigned int headerlen; + unsigned int size; + unsigned int chunklen; +} __aligned(8); + #endif /* _BPF_CRIB_H */ diff --git a/kernel/bpf/crib/bpf_checkpoint.c b/kernel/bpf/crib/bpf_checkpoint.c index c95844faecbc..5c56f1cbf3c8 100644 --- a/kernel/bpf/crib/bpf_checkpoint.c +++ b/kernel/bpf/crib/bpf_checkpoint.c @@ -241,4 +241,120 @@ __bpf_kfunc void bpf_iter_skb_destroy(struct bpf_iter_skb *it) bpf_skb_release(kit->skb); } +/** + * bpf_iter_skb_data_new() - Initialize a new skb data iterator for a skb + * (sk_buff), used to iterates over all skb data in the specified skb + * + * @it: new bpf_iter_skb_data to be created + * @skb: a pointer to a sk_buff to be iterated over + * @buf: buffer for dumping skb data + * @buflen: buffer length + */ +__bpf_kfunc int bpf_iter_skb_data_new(struct bpf_iter_skb_data *it, + struct sk_buff *skb, char *buf, int buflen) +{ + struct bpf_iter_skb_data_kern *kit = (void *)it; + + BUILD_BUG_ON(sizeof(struct bpf_iter_skb_data_kern) > sizeof(struct bpf_iter_skb_data)); + BUILD_BUG_ON(__alignof__(struct bpf_iter_skb_data_kern) != + __alignof__(struct bpf_iter_skb_data)); + + int headerlen = skb_headroom(skb); + + kit->skb = skb; + kit->headerlen = headerlen; + kit->offset = 0; + kit->chunklen = 0; + kit->size = headerlen + skb->len; + kit->buf = buf; + kit->buflen = buflen; + + return 0; +} + +/** + * bpf_iter_skb_data_next() - Dumps the corresponding data in skb to + * the buffer based on the current offset and buffer size, and updates + * the offset after copying the data + * + * @it: bpf_iter_skb_data to be checked + * + * @returns a pointer to the buffer if further data is available, + * otherwise returns NULL + */ +__bpf_kfunc char *bpf_iter_skb_data_next(struct bpf_iter_skb_data *it) +{ + struct bpf_iter_skb_data_kern *kit = (void *)it; + + if (!kit->buf || kit->buflen <= 0) + return NULL; + + if (kit->offset >= kit->size) + return NULL; + + kit->chunklen = (kit->offset + kit->buflen > kit->size) ? + kit->size - kit->offset : kit->buflen; + + skb_copy_bits(kit->skb, kit->offset - kit->headerlen, kit->buf, kit->chunklen); + + kit->offset += kit->chunklen; + + return kit->buf; +} + +/** + * bpf_iter_skb_data_set_buf() - Set the buffer for dumping the skb data + * during iteration + * + * @it: bpf_iter_skb_data to be set + * @buf: buffer + * @buflen: buffer length + */ +__bpf_kfunc void bpf_iter_skb_data_set_buf(struct bpf_iter_skb_data *it, char *buf, int buflen) +{ + struct bpf_iter_skb_data_kern *kit = (void *)it; + + kit->buf = buf; + kit->buflen = buflen; +} + +/** + * bpf_iter_skb_data_get_chunk_len() - get the size of the chunk read + * in the current iteration + * + * @it: bpf_iter_skb_data to be checked + * + * @returns read size in the current iteration + */ +__bpf_kfunc int bpf_iter_skb_data_get_chunk_len(struct bpf_iter_skb_data *it) +{ + struct bpf_iter_skb_data_kern *kit = (void *)it; + + return kit->chunklen; +} + +/** + * bpf_iter_skb_data_get_offset() - get the offset of the chunk read + * in the current iteration + * + * @it: bpf_iter_skb_data to be checked + * + * @returns offset in the current iteration + */ +__bpf_kfunc int bpf_iter_skb_data_get_offset(struct bpf_iter_skb_data *it) +{ + struct bpf_iter_skb_data_kern *kit = (void *)it; + + return kit->offset - kit->chunklen; +} + +/** + * bpf_iter_skb_destroy() - Destroy a bpf_iter_skb_data + * + * @it: bpf_iter_skb_data to be destroyed + */ +__bpf_kfunc void bpf_iter_skb_data_destroy(struct bpf_iter_skb_data *it) +{ +} + __bpf_kfunc_end_defs(); diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index fda34d8143f1..462ae1ab50e5 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -293,6 +293,13 @@ BTF_ID_FLAGS(func, bpf_iter_skb_new, KF_ITER_NEW | KF_TRUSTED_ARGS) BTF_ID_FLAGS(func, bpf_iter_skb_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_skb_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_iter_skb_data_new, KF_ITER_NEW | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_skb_data_next, KF_ITER_NEXT | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_iter_skb_data_set_buf, KF_ITER_SETTER | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_iter_skb_data_get_chunk_len, KF_ITER_GETTER) +BTF_ID_FLAGS(func, bpf_iter_skb_data_get_offset, KF_ITER_GETTER) +BTF_ID_FLAGS(func, bpf_iter_skb_data_destroy, KF_ITER_DESTROY) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, From patchwork Wed Jul 10 18:40:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729585 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011017.outbound.protection.outlook.com [52.103.32.17]) (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 7128B198A32; Wed, 10 Jul 2024 18:43:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.17 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637034; cv=fail; b=NGO97ks6QaP91zVNhURJY9QFZvoNgBvjqzD5htk3FiANyEShknPyTlVMqtqjQSVGktVvOQybugZOLYnu/o5wr1InVGvVG3BMoarv14IL5Fbgu35Et6sC3zuQcZammwoITtKi0ufUJW7qfW5lkux+Fkvov+xSAUpu2pIhttFNMMk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637034; c=relaxed/simple; bh=VMtAYDhtgHGRgwlDQFX3tZNyn2Zt5lY5JD65kF0Nnb8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=qFQizqMP18ruuQ4Sg3h4a7vkkxn5N/MDeKKzFKQOrCBpnN43zJxcvjBVNjCubB7dmnJrzlwiFEirnSlY44DxRyJZy0boPzxfzn4b6J2f1IZ8SM+RisfSaAIG0wHXkC1M4QQn3SOqKHh6w0m9gag7E7cicaSIhxk+KkxlvkP0O+0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=X2ZH4uOY; arc=fail smtp.client-ip=52.103.32.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="X2ZH4uOY" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dBCmuVpSmP1x+g+h79N/0J/ml9spGP0GQwHOWGFfZ2UpFc4zf3yF6IDnvc10M2SZT4Xfbacpvq6aZeUFQHTreAqpA9xDJuaaMSsYd6SOCV0O8iq3qXyjNEEsB1N1zshEfRQN/SUcT1LJU/Ah2vyPbYtd6LvrcbPuPmXWPjQcqJH+8f1MoH5vMgExqgovKrhwa+q7aBWtYE/T1udr3vGBi8qj0c77ZaPgNnfQtAs+Ue8ocXsZiMqUa/w0QQ6k4LajD6M9RzgAfZc4DwSJjYPbS3Qcc5BeZRAx8DDjxb774YUpRav2lXA6iRY6mccvMQ5kWbcsVfVxk1Gq5KbjM7vIqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=blZrm13GUso40hHswj2V1eWhQe/RYEiayKzuIpi2toE=; b=Yhfq1/o5gWJFUzyvvKJIYT5F9jLwnKW/daBZIGP5oD0+Cr8lniGsLHW0rYSJ8WPfysV5cQlSLmfvqwjIw2TNEVZJznNifO1NawaO4Phe6aCCFXQ0clKNrgGhYMjBIicaeGu1ibrASi8zhnC3DI+1baCfBKCxB510T+kNulDeGmKO0R5FJpsV6/k3Jkr+3AQx8jVxyxhlw6C1JA2jFYBVWOxm5CDcig7dGHoP1C6AdWnGobzfgh2shF4dgLrkIfQFcjslV+Gi2sTrRajuH+g/5+PFnnifKp1qxH9apiF7Z232gzEAR6EmdOXd6onSNIKjHoTh7jxuy6Ey1+AS/o8Yjw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=blZrm13GUso40hHswj2V1eWhQe/RYEiayKzuIpi2toE=; b=X2ZH4uOYzhOK6yQdo+SfnDoUyrKJx1gUjKaA7pfdv+9Hd9B9LCGsVx+XqXZ5csJ84dCvP306uPoFXvGy+aot1C/u+lAcolWdeYVNHJ+OPkDZumXXR5C2mNgbZkmMJ6A0Sf1JM3ish5kVzEOfJcULgBf2NI8X8NAYEws3ImUcnoQttSxZs9hA/mrPeOmr5l5XHp8mSb0OA11U96Vo+qoTmUQ3yWGm1aGe3ppStcO/G2kinngdCLCf1cjcxPrk82sTjeTBK64EebSIKbhkyXa1mdpONkI1wm5RnyMFSpx53xO+oUXTGb53cdCG15OWxhYxRDDbLmaAtZdTvZA6SSDnUA== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:50 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:50 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 13/16] bpf/crib: Add CRIB kfuncs for restoring data in skb Date: Wed, 10 Jul 2024 19:40:57 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [tiuzTlMuxXUaVQw9ZmKqW2zVFqf4bFuy] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-14-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: f4c65e89-b463-4b99-26ac-08dca1103d52 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: DSEwffJvZMXkIfidbj/r8YqXNhaTFPeodqUEHku6sVz8IVGeUAbQBFUtUANxmpcroVS64is7vJt6sAe+lKmdQj3Zfhq9MMYj7Iq/26ws7XFJx5NeI+S6OeA2mznMoF1f4pEWwhh6uDKkX1xqLs6/zM7mSMl9jyYM2W+Bhs+9ubWKluqk0WYSq5NH5p/NNKvgJ8C8whpVOa6Ag7zQlX//G0+C1rPJf8ThNr2Ed7hqhgQ3Xc+QQV11uzWiwMRx9B3ckFWtzdaAkBMAqHGHb/W1iALeUKACUzyi73LVUabvHxgKMB19jEMK3QiftHweAYfDFan1UcTKUC5zvciD5DFveD3E/to+owM2bgsBoe0OdA/ld/CbvmPRFmWmDuUkgoYYVCAsgVOAD60PQhxy3NlVXabHz4svTUO0qUCrglEfh0wG95D/ClWvTh19McDc9hoYMZsKeki2+Aj9FO0rgw6cwnR3vxLzpnTyImwSTZSdD7zYlOVxmfZAwv5YyWOzBtGvj41kTe5bLa8B48Dwte9gUdXNS754biUv73I+XVozcng4JHdccrnHH04NizySOeeY1b4oUGjKb/VgM2xRQh8vVlDs94AbqDlhcH2ZYyc6Gpxr+/mGoU2ohiu00YNLzsPdbucnTnVsMjKyFgXqTSImnQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PPimNz8K3ZQIoibeL0Jn7KpGsttwYuyJUDoTnTS5OAr4e37/8xln/7xOmufVxXVkhqwQmRAnn3rv1Ed436rOCq5LLl/aczzYLqAZzX13563u821EirkUygweSHxlRSj270BN5fi0fn0yHK3ntwXpdlJcJ6c4uCtKvPco7rxIi2KgGYPchf7kYck6jxq8DdDu3tn6XiwcwObTjrH5WNbXHH90ImBsgkW1NdGhAi3T5TDtsr1WjuDkJY5ncr+KJE5W+QOvXLslSQw2b4qDeUNUYg29K1PClRujIUevpuIYhQjRRl96SbgBF8E8wx0O7iVEP9z0VR7TyBdqXJQJBTf7Db4xU0/lKC+UnS+aDJ1eS8fiHKSnRQKKvetXfcHZRlgbmVtxoOS0Py36Rb/XAV89iaojvhtRJ0D3b+gvtoj9PLcSI/Vgw4ME/4tmOuQsn91w9MJQ1ebTfnK2q6ZZsI/X/bZrW7X/trz1pa7pxF596QD7DsRlO0YfQErBNu1UUoWLQEnRg4C63NdfKrn/8qkAieMgXK/+/q+c6Nze1f7AfXzW3A3z0owGx+TkyZdLNwWyaHGm5TE0p3sdjcDCe3SUVsmcfNFS9TOWpk2y1InjHlvJQmUZj/bC2zlW2hNI9dfly1B+bsq95ppMrbeQSGu625kPdDKdNrL6J5VrAn3VsR0/NYLEygbkLSPwGu3lRtXRalMiwW6RfQ/QE0d5iPSRPrJt4OovE3faa8mV7Kov04sr7Vdr8keUjEgvqVLBLw+Q+jRE2yjcg7UfLOUbVuHllhwE1Ri+FgL2o/P59Ki9sNu6HWiGiv6mwg4HBbg1I2JA7XmNfa3hL8QnPBC8oBwrFw+QRTvByp11JEMgaaU/qPinGBjB0Gtc2mX9GEdRmlFpZabLjaAT6hE93H02Q8XZsXCLBL++HdJPWVGR0j1i8g/Yn3onQ1yoyzsmGnVDrJPkbr8DVds+lS0XDFHhHZrXumN0YhtzqRctF1pmCQlcbp+styd5ahOeHsuRKfpRnnqwjEZ+A944uYTjymHO1DmCd8KaZbQuM4VaPku6XdXV71YELxZVoLT6v3WrVtHngvG7hSiBek1VFsdPHBt2EKSAH1L6nmdK3ePNQN1/iNQDwtpHWdBX7WqXzF2dESF7MpWw206hR9nT76SnEobEhclb1quMHHAbvIw8xbyG7RXVYzXtOf7zPnm3AZWTO+Zb9kZ2bInEd9tUhHzFkmUekIfNSV8u8zom6kforK9oEPZ82rY= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f4c65e89-b463-4b99-26ac-08dca1103d52 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:50.2491 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC This patch adds CRIB kfuncs for restoring data in skb. bpf_restore_skb_rcv_queue() is used to create a new skb based on the previously dumped skb information and add it to the tail of the specified receive queue. bpf_restore_skb_data() is used to restore data in the skb and must be used after bpf_restore_skb_rcv_queue(). Signed-off-by: Juntong Deng --- include/linux/bpf_crib.h | 13 +++++++ kernel/bpf/crib/bpf_crib.c | 3 ++ kernel/bpf/crib/bpf_restore.c | 67 +++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/include/linux/bpf_crib.h b/include/linux/bpf_crib.h index c073166e60a0..5b7d44642f80 100644 --- a/include/linux/bpf_crib.h +++ b/include/linux/bpf_crib.h @@ -46,4 +46,17 @@ struct bpf_iter_skb_data_kern { unsigned int chunklen; } __aligned(8); +struct bpf_crib_skb_info { + int headerlen; + int len; + int size; + int tstamp; + int dev_scratch; + int protocol; + int csum; + int transport_header; + int network_header; + int mac_header; +} __aligned(8); + #endif /* _BPF_CRIB_H */ diff --git a/kernel/bpf/crib/bpf_crib.c b/kernel/bpf/crib/bpf_crib.c index 462ae1ab50e5..beada526021a 100644 --- a/kernel/bpf/crib/bpf_crib.c +++ b/kernel/bpf/crib/bpf_crib.c @@ -300,6 +300,9 @@ BTF_ID_FLAGS(func, bpf_iter_skb_data_get_chunk_len, KF_ITER_GETTER) BTF_ID_FLAGS(func, bpf_iter_skb_data_get_offset, KF_ITER_GETTER) BTF_ID_FLAGS(func, bpf_iter_skb_data_destroy, KF_ITER_DESTROY) +BTF_ID_FLAGS(func, bpf_restore_skb_rcv_queue, KF_ACQUIRE | KF_TRUSTED_ARGS | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_restore_skb_data, KF_TRUSTED_ARGS) + BTF_KFUNCS_END(bpf_crib_kfuncs) static int bpf_prog_run_crib(struct bpf_prog *prog, diff --git a/kernel/bpf/crib/bpf_restore.c b/kernel/bpf/crib/bpf_restore.c index 6bbb4b01e34b..7966c02e416e 100644 --- a/kernel/bpf/crib/bpf_restore.c +++ b/kernel/bpf/crib/bpf_restore.c @@ -7,7 +7,74 @@ */ #include +#include +#include + +extern struct sk_buff *bpf_skb_acquire(struct sk_buff *skb); __bpf_kfunc_start_defs(); +/** + * bpf_restore_skb_rcv_queue() - Create a new skb based on the previously + * dumped skb information and add it to the tail of the specified queue to + * achieve restoring the skb + * + * Note that this function acquires a reference to struct sk_buff. + * + * @head: queue that needs to restore the skb + * @sk: struct sock where the queue is located. + * @skb_info: previously dumped skb information + * + * @returns a pointer to the skb if the restoration of the skb + * was successful, otherwise returns NULL. + */ +__bpf_kfunc struct sk_buff * +bpf_restore_skb_rcv_queue(struct sk_buff_head *head, struct sock *sk, + struct bpf_crib_skb_info *skb_info) +{ + struct sk_buff *skb; + + skb = alloc_skb(skb_info->size, GFP_KERNEL); + if (!skb) + return NULL; + + skb_reserve(skb, skb_info->headerlen); + skb_put(skb, skb_info->len); + + skb->tstamp = skb_info->tstamp; + skb->dev_scratch = skb_info->dev_scratch; + skb->protocol = skb_info->protocol; + skb->csum = skb_info->csum; + skb->transport_header = skb_info->transport_header; + skb->network_header = skb_info->network_header; + skb->mac_header = skb_info->mac_header; + + lock_sock(sk); + skb_queue_tail(head, skb); + skb_set_owner_r(skb, sk); + release_sock(sk); + + return bpf_skb_acquire(skb); +} + +/** + * bpf_restore_skb_data() - Restore the data in skb + * + * @skb: skb that needs to restore data + * @offset: data offset in skb + * @data: data buffer + * @len: data length + * + * @returns the number of bytes of data restored if + * the restoration was successful, otherwise returns -1. + */ +__bpf_kfunc int bpf_restore_skb_data(struct sk_buff *skb, int offset, char *data, int len) +{ + if (offset + len > skb_headroom(skb) + skb->len) + return -1; + + memcpy(skb->head + offset, data, len); + return len; +} + __bpf_kfunc_end_defs(); From patchwork Wed Jul 10 18:40:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729586 Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011072.outbound.protection.outlook.com [52.103.32.72]) (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 8F692198A32; Wed, 10 Jul 2024 18:43:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.72 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637042; cv=fail; b=HLwew9V3NDZUqT7XgJcY1Ed2HBlHwCJs68pZjrc32a4fcLMa9CNsdH0rJvvpe3ucu/M3dyH8cdwbPeNagjWj3UkbEsPS8ibnh7YySHkRR+nc9V/p/scp4xh+xDX3ih3yor73sq2DLnGTSYm4sLosVAXsX9D29YZr2TNuOOb+Xm0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637042; c=relaxed/simple; bh=BBkzFBSDzlPXi+C+Y/6QYa8rH+OnlA5s9vjc7e8K27Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=thsJkByD88B+GVgavoKILrudjY+fAnvkTfa8hyTUvtpI+/cxGcViUGgPlL6kK4QE2Z0gjkj7nmAX0gA2JulZDvJoBg//OaMlEIP9aSlhLP53UGkLwtHMlz/J4dPgsRZZGt7HSNArChqozBH4gqB5v84PIaib6+1Di3yfcfIl4Fw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=aSrxsnfq; arc=fail smtp.client-ip=52.103.32.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="aSrxsnfq" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PPdMDFadxtiOUqBaKkdBmdPuRrhojzz02hxn548iZIbl/i0d8ZkVPVzrtiXomA5pcDZyOhX9R17SRnyHQyMt86Z5nelKW/6Fxm02FqDL4m8jqAXnRN8qg4LPNR3wrhSZmSfGV7iWFVDXs7eEayhgDVK7jlQmqMVy6x95CoCzn+WfFGnHV2ByApzK2jJ7tt9dF4qPMLcocCLwhse2dTtZjAJKmVAdK/i6gIcUku1Kju+tdUmCEpTGa+T9XgKwNa5DOpsQe/7trMpPb4+zQGn1duremfRvPTJGQCeqyLvq86YPo2xqQLEa/bwnOnAAWU501r+S4cFbN2wqmZA14PvtNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=K/ImEdVd8bLZVZuiwbSUIP9BiNKop478/SCug29q27Y=; b=BHHtAy5aiHztD/JV6uKHAqqcIfZqdwyc3q21M16STtemmhDAKNrs39D1R4b/gnx5fO/1lLBhB+9YPZbQjtGiDkD2eaJ6H802I5/dwyxVsJBMuSnVW2NiaWQJw2e1TaqATjF3kfm+hHuk6LXtXVPGruu2qSdEBIU3lYgm5QAEpOX1qsmx9jw4ZviUiStMYnzJ0a4zrMDnCv3cUZlddSHXWULINYTg8kb/Aqvs8c0Fq1egKTJHXaOHPdTnZT4FrF7ed3BZA4ZWDgA7FiBHpczprrbOAN52varkkPm85dS295oG2woJpe3M2JBrQIKf0iyG0ztJTVM18dUhuyvDtx/Yyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=K/ImEdVd8bLZVZuiwbSUIP9BiNKop478/SCug29q27Y=; b=aSrxsnfqm19sUkRnwpRT4aC+Qof3Trm5Q5SahSlikIm5ZMgm4VcLkeQEt/3cXNwaQrhM1PDDZesTXEEBHuiTVbUHceCnozZWv0hLXFB/4aark7cS4KbSBjPQLijUWiInz+rk41AxN1u1x/VeH0QxRUu1DPK9kisIjs91rDNX82NJIHxRo4XFbzDJnZrvVkF82WQdvLEjzgskRhtddOF+W78Y4rRk21GF6KoAQZ5Q3lJlymJD3/eUUdtwMMfFJVM8dUAE95Ap+Vv2RiCaJu01oI/s+LXTIAKxeFGVuS12xyEwmC65lSfY6aNYsPETjgn2s2a11jJssiPyu1BDXEiCLw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:43:56 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:43:56 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 14/16] selftests/crib: Add test for getting basic information of the process Date: Wed, 10 Jul 2024 19:40:58 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [kl5V/14AD11JL9JVU91637orERWo+t9t] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-15-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: a5b541f3-81a8-4352-575f-08dca1104120 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: Bu8Wdsj+OH3Eg3FlLCG3C1vsaU72ygsx3rhebrXXhF05sZhauRDpQy6+3r5jt8vFL855VnXpEYXhYAn0bYnR8THU9t/P023Fyyg96xnmQ/kr+DGgmLho8yNqgkvtoFAzj1YLqEMp4xEx0UbYPh0F8mxCnsJiAEov68fA+J3HJgM3AHukf70GG3Jqzv9eTiV7nRSphNhxT7pNhwNksHNW3oDOL9NYdTAO5Bm1rzUEmuoyO0uLxVwWWgf6+XyeOojpObhOhReK53evuJwE9bumYVd2rsRcighWiR/Y1sGH5vhiIbh5DfaAHdwAB8ZQhT6dm2LRgzRqYxAApD/PKvCJmFDmRXhzILJsZHNJhsE6qA3ah1N77PMNJ66vBD37lykAMZP0++qIDcrz4fnL5HPvJklTfuFPILbnCm9syQzOO8fvjthdnjO/5mfCujevEEh0bXM3PHk1vEIcNlfdPZVyWuTapX28JyqCV2ShEbFCLI0lQADG2aVZBYY8cPWlkP6OJdaA2i22SQSEoGVROyCh2TyvlIpbcIkDbnKqMbPZjYrpPx7g8bAz//izm+ie/9ijap7vkREfNu5pCp5uEHLuPy3IU0MRF47syDfUrxjgvPpruzXb97x7ooRNQ31RNmIZRE3GjxYsJpYsNZuqcSUCMA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: P8eFJV/80Ma9sOnuMJT0APVHQc7NkWKZiLXISBVfziIQE1KF6BS9MUnQkiGesxjHFHNklDYZze04cmRyOuO1B2pvk7QMO18j+Ij8l6IKZrin1+DpGjmERQqQ4Giq1Oizf61B0AX1JxpWrJKdvGkgABqR8+VL/aLeWC5yfwhDoKSBf1PyhyfeTbGE503qMbK1bGlWOuckYtCyE/C31Bl2dX6UhQ1K/OVZFIUsBDCwlNdbrgzJCJXLzuMnE3ipQYej6w/6/cAaOWiSstA1EwFA9kPooCOGfBMrHA1Pyj+D0jHDaZzRcH2WfR9Bp2gxxYVRatdI+IsFEeT4Vh34f+G84LHDpt43V5iPgQl4JhVkhla4Bj0tX8MNG13JQsd6mX8lt8tJW6kfbe6dfwIYpYgtE/fxbRELg8jfZUtcAWWfK+IJFwwZ3wDQRoCTd1htPuuQxGZoZjHcphST321GqjG5+Qt1Fo8yDcCka7kjQTMyrae245lVLaA75SIFvE8UeFSI2kwHrsogF0JNo3h+iW4hAecu1pgBrQQeep6ofr5CNEu9N4W+EbUh4tMrWP9c6ogOJzj7jFop/0KAdWoDAzF/jnfc7XnJWH2p3/pI6VlMLorCNxrIF7LqvFTP1E6zekVBy095lnxI+0s2HHUgxhnvux6da/504ikkOk5PSQMQmcrcFMQQ0Z82xcA21jxFdYwcSeIgA5Hu7ymfPXYLMF3RYfkgvAIbtGq2s5QW+9wl6t3rsZ/sB5dheRRGD15PFB/ahLuItq9bZi7OXmzGWmvDM/p+Ajxs0cblW1jF0crnVYzYzqZ2HO70fELW2dtWwaebsbCTCdJ7Czb1QBgdKjjsPiXkOeHBZ0FBIwg9FO9XhgH3NMgSiKOIh9Epx9RENpksUI5qjPGIN6tGAY3+tQKTVuezAuwZ49+CSB6EDUoPPmcSuHn4cfRrZeyzZAZF0Uy9z0ycyyLPORbNPaDo5bNl5p13SU/Ppsiy5vVcO2HQNj6CLGsWe6KHMLhgZahOnt5wIvwevfpjPDzHPC+kK4VpMD9mSOdkhcHVb1PvrscYWm0OBMbijrymIW6pAAYASKOZU+99GhRVgd38lFwglXksbUVdLmXfXH69UNaKtWzv7zBN5RjVz1jKPbaBbv4cgKrHcbkB1FFNJkDEzHXhic2RCsmx6NIemw0BABky0Tr5KdGGu4hdlrRB8y7ixxq8psuRVUGg3Nf9H4dJzwS/RR3/5Y+PKNf6iIIrtQoaTWb5KcQ= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: a5b541f3-81a8-4352-575f-08dca1104120 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:43:56.8364 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-State: RFC In this test, the basic information of the process is obtained through CRIB, including priority, comm, address space, all VMAs, etc. The process information obtained through CRIB will be compared with the process information obtained through procfs to verify the correctness of the information. In addition, the performance of CRIB and procfs will be compared in this test, usually CRIB takes only 20-30% of the time of procfs. The following is an example of test results: CRIB dump took 0.001349 seconds PROC dump took 0.005516 seconds Signed-off-by: Juntong Deng --- tools/testing/selftests/crib/.gitignore | 1 + tools/testing/selftests/crib/Makefile | 136 +++++++ tools/testing/selftests/crib/config | 7 + .../selftests/crib/test_dump_task.bpf.c | 125 +++++++ tools/testing/selftests/crib/test_dump_task.c | 337 ++++++++++++++++++ tools/testing/selftests/crib/test_dump_task.h | 90 +++++ 6 files changed, 696 insertions(+) create mode 100644 tools/testing/selftests/crib/.gitignore create mode 100644 tools/testing/selftests/crib/Makefile create mode 100644 tools/testing/selftests/crib/config create mode 100644 tools/testing/selftests/crib/test_dump_task.bpf.c create mode 100644 tools/testing/selftests/crib/test_dump_task.c create mode 100644 tools/testing/selftests/crib/test_dump_task.h diff --git a/tools/testing/selftests/crib/.gitignore b/tools/testing/selftests/crib/.gitignore new file mode 100644 index 000000000000..378eac25d311 --- /dev/null +++ b/tools/testing/selftests/crib/.gitignore @@ -0,0 +1 @@ +build diff --git a/tools/testing/selftests/crib/Makefile b/tools/testing/selftests/crib/Makefile new file mode 100644 index 000000000000..9d0553f1ff5c --- /dev/null +++ b/tools/testing/selftests/crib/Makefile @@ -0,0 +1,136 @@ +# SPDX-License-Identifier: GPL-2.0 +include ../../../build/Build.include +include ../../../scripts/Makefile.arch +include ../../../scripts/Makefile.include +include ../lib.mk + +CUR_DIR := $(abspath .) +REPO_ROOT := $(abspath ../../../..) +TOOLS_DIR := $(REPO_ROOT)/tools +TOOLSINC_DIR := $(TOOLS_DIR)/include +BPFTOOL_DIR := $(TOOLS_DIR)/bpf/bpftool +UAPI_DIR := $(TOOLSINC_DIR)/uapi +LIB_DIR := $(TOOLS_DIR)/lib +LIBBPF_DIR := $(LIB_DIR)/bpf +GEN_DIR := $(REPO_ROOT)/include/generated +GEN_HDR := $(GEN_DIR)/autoconf.h + +OUTPUT_DIR := $(CUR_DIR)/build +INCLUDE_DIR := $(OUTPUT_DIR)/include +SBIN_DIR:= $(OUTPUT_DIR)/sbin +OBJ_DIR := $(OUTPUT_DIR)/obj +CRIBOBJ_DIR := $(OBJ_DIR)/crib +LIBBPF_OUTPUT := $(OBJ_DIR)/libbpf/libbpf.a +LIBBPF_OBJ_DIR := $(OBJ_DIR)/libbpf +LIBBPF_OBJ := $(LIBBPF_OBJ_DIR)/libbpf.a + +DEFAULT_BPFTOOL := $(SBIN_DIR)/bpftool +BPFTOOL ?= $(DEFAULT_BPFTOOL) + +VMLINUX_BTF_PATHS ?= ../../../../vmlinux \ + /sys/kernel/btf/vmlinux \ + /boot/vmlinux-$(shell uname -r) +VMLINUX_BTF ?= $(abspath $(firstword $(wildcard $(VMLINUX_BTF_PATHS)))) +ifeq ($(VMLINUX_BTF),) +$(error Cannot find a vmlinux for VMLINUX_BTF at any of "$(VMLINUX_BTF_PATHS)") +endif + +ifneq ($(wildcard $(GEN_HDR)),) + GENFLAGS := -DHAVE_GENHDR +endif + +CFLAGS += -g -O2 -rdynamic -pthread -Wall -Werror $(GENFLAGS) \ + -I$(INCLUDE_DIR) -I$(GEN_DIR) -I$(LIB_DIR) \ + -I$(TOOLSINC_DIR) -I$(UAPI_DIR) -I$(CUR_DIR)/include \ + -Wno-unused-command-line-argument + +LDFLAGS = -lelf -lz + +IS_LITTLE_ENDIAN = $(shell $(CC) -dM -E - &1 \ + | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \ +$(shell $(1) -dM -E - $@ +else + $(call msg,CP,,$@) + $(Q)cp "$(VMLINUX_H)" $@ +endif + +$(CRIBOBJ_DIR)/%.bpf.o: %.bpf.c %.h $(INCLUDE_DIR)/vmlinux.h | $(LIBBPF_OBJ) $(CRIBOBJ_DIR) + $(call msg,CLANG-BPF,,$(notdir $@)) + $(Q)$(CLANG) $(BPF_CFLAGS) -target bpf -c $< -o $@ + +$(INCLUDE_DIR)/%.bpf.skel.h: $(CRIBOBJ_DIR)/%.bpf.o $(INCLUDE_DIR)/vmlinux.h $(BPFTOOL) | $(INCLUDE_DIR) + $(call msg,GEN-SKEL,,$(notdir $@)) + $(Q)$(BPFTOOL) gen skeleton $< > $@ + +$(TEST_GEN_PROGS_OBJ): $(CRIBOBJ_DIR)/%.o: %.c %.h $(INCLUDE_DIR)/%.bpf.skel.h | $(CRIBOBJ_DIR) + $(call msg,CLANG,,$(notdir $@)) + $(Q)$(CLANG) $(CFLAGS) -c $< -o $@ + +$(TEST_GEN_PROGS): $(SBIN_DIR)/%: $(CRIBOBJ_DIR)/%.o $(LIBBPF_OBJ) | $(SBIN_DIR) + $(call msg,CLANG-LINK,,$(notdir $@)) + $(Q)$(CLANG) $(CFLAGS) $(LDFLAGS) $^ -o $@ + +override define CLEAN + rm -rf $(OUTPUT_DIR) +endef + +all: $(TEST_GEN_PROGS) + +.PHONY: all clean help + +.DEFAULT_GOAL := all + +.DELETE_ON_ERROR: + +.SECONDARY: diff --git a/tools/testing/selftests/crib/config b/tools/testing/selftests/crib/config new file mode 100644 index 000000000000..61684f763df0 --- /dev/null +++ b/tools/testing/selftests/crib/config @@ -0,0 +1,7 @@ +CONFIG_BPF=y +CONFIG_BPF_EVENTS=y +CONFIG_BPF_JIT=y +CONFIG_BPF_CRIB=y +CONFIG_DEBUG_INFO_BTF=y +CONFIG_IPV6=y +CONFIG_NET=y diff --git a/tools/testing/selftests/crib/test_dump_task.bpf.c b/tools/testing/selftests/crib/test_dump_task.bpf.c new file mode 100644 index 000000000000..ee9843a79dac --- /dev/null +++ b/tools/testing/selftests/crib/test_dump_task.bpf.c @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#include "vmlinux.h" + +#include +#include +#include + +#include "test_dump_task.h" + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 100000); +} rb SEC(".maps"); + +extern struct task_struct *bpf_task_from_vpid(pid_t vpid) __ksym; +extern void bpf_task_release(struct task_struct *p) __ksym; + +extern int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it, + struct task_struct *task, + unsigned long addr) __ksym; +extern struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_vma *it) __ksym; +extern void bpf_iter_task_vma_destroy(struct bpf_iter_task_vma *it) __ksym; + +SEC("crib") +int dump_all_vma(struct prog_args *arg) +{ + int err = 0; + + struct task_struct *task = bpf_task_from_vpid(arg->pid); + if (!task) { + err = -1; + goto error; + } + + struct vm_area_struct *cur_vma; + struct bpf_iter_task_vma vma_it; + + bpf_iter_task_vma_new(&vma_it, task, 0); + while ((cur_vma = bpf_iter_task_vma_next(&vma_it))) { + struct event_vma *e_vma = bpf_ringbuf_reserve(&rb, sizeof(struct event_vma), 0); + if (!e_vma) { + err = -2; + goto error_buf; + } + + e_vma->hdr.type = EVENT_TYPE_VMA; + e_vma->vm_start = BPF_CORE_READ(cur_vma, vm_start); + e_vma->vm_end = BPF_CORE_READ(cur_vma, vm_end); + e_vma->vm_flags = BPF_CORE_READ(cur_vma, vm_flags); + + if (cur_vma->vm_file) + e_vma->vm_pgoff = BPF_CORE_READ(cur_vma, vm_pgoff); + + bpf_ringbuf_submit(e_vma, 0); + } + +error_buf: + bpf_iter_task_vma_destroy(&vma_it); + bpf_task_release(task); +error: + return err; +} + +SEC("crib") +int dump_task_stat(struct prog_args *arg) +{ + int err = 0; + + struct task_struct *task = bpf_task_from_vpid(arg->pid); + if (!task) { + err = -1; + goto error; + } + + struct event_task *e_task = bpf_ringbuf_reserve(&rb, sizeof(struct event_task), 0); + if (!e_task) { + err = -2; + goto error_buf; + } + + e_task->hdr.type = EVENT_TYPE_TASK; + e_task->pid = BPF_CORE_READ(task, pid); + e_task->prio = BPF_CORE_READ(task, prio); + e_task->policy = BPF_CORE_READ(task, policy); + e_task->flags = BPF_CORE_READ(task, flags); + e_task->exit_code = BPF_CORE_READ(task, exit_code); + BPF_CORE_READ_STR_INTO(&e_task->comm, task, comm); + + bpf_ringbuf_submit(e_task, 0); + + struct event_mm *e_mm = bpf_ringbuf_reserve(&rb, sizeof(struct event_mm), 0); + if (!e_mm) { + err = -2; + goto error_buf; + } + + struct mm_struct *mm = BPF_CORE_READ(task, mm); + e_mm->hdr.type = EVENT_TYPE_MM; + e_mm->start_code = BPF_CORE_READ(mm, start_code); + e_mm->end_code = BPF_CORE_READ(mm, end_code); + e_mm->start_data = BPF_CORE_READ(mm, start_data); + e_mm->end_data = BPF_CORE_READ(mm, end_data); + e_mm->start_brk = BPF_CORE_READ(mm, start_brk); + e_mm->brk = BPF_CORE_READ(mm, brk); + e_mm->start_stack = BPF_CORE_READ(mm, start_stack); + e_mm->arg_start = BPF_CORE_READ(mm, arg_start); + e_mm->arg_end = BPF_CORE_READ(mm, arg_end); + e_mm->env_start = BPF_CORE_READ(mm, env_start); + e_mm->env_end = BPF_CORE_READ(mm, env_end); + e_mm->map_count = BPF_CORE_READ(mm, map_count); + + bpf_ringbuf_submit(e_mm, 0); + +error_buf: + bpf_task_release(task); +error: + return err; +} diff --git a/tools/testing/selftests/crib/test_dump_task.c b/tools/testing/selftests/crib/test_dump_task.c new file mode 100644 index 000000000000..1b9f234ac292 --- /dev/null +++ b/tools/testing/selftests/crib/test_dump_task.c @@ -0,0 +1,337 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#include "test_dump_task.h" +#include "test_dump_task.bpf.skel.h" + +struct task { + int pid; + unsigned int flags; + int prio; + unsigned int policy; + int exit_code; + char comm[16]; +}; + +struct mm { + unsigned long start_code; + unsigned long end_code; + unsigned long start_data; + unsigned long end_data; + unsigned long start_brk; + unsigned long brk; + unsigned long start_stack; + unsigned long arg_start; + unsigned long arg_end; + unsigned long env_start; + unsigned long env_end; + int map_count; +}; + +struct vma { + unsigned long vm_start; + unsigned long vm_end; + unsigned long vm_flags; + unsigned long vm_pgoff; +}; + +struct dump_info { + struct task task; + struct mm mm; + struct vma *vma; + unsigned int vma_count; +}; + +static int handle_vma_event(struct dump_info *info, struct event_vma *e_vma) +{ + struct vma *vma = &info->vma[info->vma_count]; + vma->vm_start = e_vma->vm_start; + vma->vm_end = e_vma->vm_end; + vma->vm_flags = e_vma->vm_flags; + vma->vm_pgoff = e_vma->vm_pgoff; + info->vma_count++; + return 0; +} + +static int handle_mm_event(struct dump_info *info, struct event_mm *e_mm) +{ + info->mm.start_code = e_mm->start_code; + info->mm.end_code = e_mm->end_code; + info->mm.start_data = e_mm->start_data; + info->mm.end_data = e_mm->end_data; + info->mm.start_brk = e_mm->start_brk; + info->mm.brk = e_mm->brk; + info->mm.start_stack = e_mm->start_stack; + info->mm.arg_start = e_mm->arg_start; + info->mm.arg_end = e_mm->arg_end; + info->mm.env_start = e_mm->env_start; + info->mm.env_end = e_mm->env_end; + info->mm.map_count = e_mm->map_count; + info->vma = (struct vma *)malloc(sizeof(struct vma) * e_mm->map_count); + info->vma_count = 0; + return 0; +} + +static int handle_task_event(struct dump_info *info, struct event_task *e_task) +{ + info->task.pid = e_task->pid; + info->task.flags = e_task->flags; + info->task.prio = e_task->prio; + info->task.policy = e_task->policy; + info->task.exit_code = e_task->exit_code; + memcpy(info->task.comm, e_task->comm, sizeof(info->task.comm)); + return 0; +} + +static int handle_event(void *ctx, void *data, size_t data_sz) +{ + struct dump_info *info = (struct dump_info *)ctx; + const struct event_hdr *e_hdr = data; + int err = 0; + + switch (e_hdr->type) { + case EVENT_TYPE_TASK: + handle_task_event(info, (struct event_task *)data); + break; + case EVENT_TYPE_VMA: + handle_vma_event(info, (struct event_vma *)data); + break; + case EVENT_TYPE_MM: + handle_mm_event(info, (struct event_mm *)data); + break; + default: + err = -1; + printf("Unknown event type!\n"); + break; + } + return err; +} + +static int dump_task_and_mm_struct_from_proc(struct dump_info *info) +{ + FILE *file = fopen("/proc/self/stat", "r"); + if (!file) + return -1; + + fscanf(file, "%d %s %*c %*d %*d %*d %*d %*d %u %*lu %*lu %*lu %*lu " + "%*lu %*lu %*ld %*ld %d %*ld %*d %*d %*llu %*lu %*ld %*lu %lu %lu %lu " + "%*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*lu %*d %*d %*u %u %*llu %*lu %*ld " + "%lu %lu %lu %lu %lu %lu %lu %d", + &info->task.pid, info->task.comm, &info->task.flags, &info->task.prio, + &info->mm.start_code, &info->mm.end_code, &info->mm.start_stack, + &info->task.policy, &info->mm.start_data, &info->mm.end_data, + &info->mm.start_brk, &info->mm.arg_start, &info->mm.arg_end, + &info->mm.env_start, &info->mm.env_end, &info->task.exit_code); + + fclose(file); + return 0; +} + +static void parse_vma_vmflags(char *buf, struct vma *vma) +{ + vma->vm_flags = 0; + char *token = strtok(buf, " "); + do { + if (!strncmp(token, "rd", 2)) + vma->vm_flags |= VM_READ; + else if (!strncmp(token, "wr", 2)) + vma->vm_flags |= VM_WRITE; + else if (!strncmp(token, "ex", 2)) + vma->vm_flags |= VM_EXEC; + else if (!strncmp(token, "sh", 2)) + vma->vm_flags |= VM_SHARED; + else if (!strncmp(token, "mr", 2)) + vma->vm_flags |= VM_MAYREAD; + else if (!strncmp(token, "mw", 2)) + vma->vm_flags |= VM_MAYWRITE; + else if (!strncmp(token, "me", 2)) + vma->vm_flags |= VM_MAYEXEC; + else if (!strncmp(token, "ms", 2)) + vma->vm_flags |= VM_MAYSHARE; + else if (!strncmp(token, "gd", 2)) + vma->vm_flags |= VM_GROWSDOWN; + else if (!strncmp(token, "pf", 2)) + vma->vm_flags |= VM_PFNMAP; + else if (!strncmp(token, "lo", 2)) + vma->vm_flags |= VM_LOCKED; + else if (!strncmp(token, "io", 2)) + vma->vm_flags |= VM_IO; + else if (!strncmp(token, "sr", 2)) + vma->vm_flags |= VM_SEQ_READ; + else if (!strncmp(token, "rr", 2)) + vma->vm_flags |= VM_RAND_READ; + else if (!strncmp(token, "dc", 2)) + vma->vm_flags |= VM_DONTCOPY; + else if (!strncmp(token, "de", 2)) + vma->vm_flags |= VM_DONTEXPAND; + else if (!strncmp(token, "lf", 2)) + vma->vm_flags |= VM_LOCKONFAULT; + else if (!strncmp(token, "ac", 2)) + vma->vm_flags |= VM_ACCOUNT; + else if (!strncmp(token, "nr", 2)) + vma->vm_flags |= VM_NORESERVE; + else if (!strncmp(token, "ht", 2)) + vma->vm_flags |= VM_HUGETLB; + else if (!strncmp(token, "sf", 2)) + vma->vm_flags |= VM_SYNC; + else if (!strncmp(token, "ar", 2)) + vma->vm_flags |= VM_ARCH_1; + else if (!strncmp(token, "wf", 2)) + vma->vm_flags |= VM_WIPEONFORK; + else if (!strncmp(token, "dd", 2)) + vma->vm_flags |= VM_DONTDUMP; + else if (!strncmp(token, "sd", 2)) + vma->vm_flags |= VM_SOFTDIRTY; + else if (!strncmp(token, "mm", 2)) + vma->vm_flags |= VM_MIXEDMAP; + else if (!strncmp(token, "hg", 2)) + vma->vm_flags |= VM_HUGEPAGE; + else if (!strncmp(token, "nh", 2)) + vma->vm_flags |= VM_NOHUGEPAGE; + else if (!strncmp(token, "mg", 2)) + vma->vm_flags |= VM_MERGEABLE; + else if (!strncmp(token, "um", 2)) + vma->vm_flags |= VM_UFFD_MISSING; + else if (!strncmp(token, "uw", 2)) + vma->vm_flags |= VM_UFFD_WP; + } + while ((token = strtok(NULL, " ")) != NULL); +} + +static int dump_vma_from_proc(struct dump_info *info) +{ + FILE *file = fopen("/proc/self/smaps", "r"); + if (!file) + return -1; + + char *line = NULL; + size_t len = 0; + ssize_t nread; + while ((nread = getline(&line, &len, file)) != -1) { + struct vma *vma = &info->vma[info->vma_count]; + if (isupper(*line)) { + if (!strncmp(line, "VmFlags: ", 9)) { + parse_vma_vmflags(&line[9], vma); + info->vma_count++; + } + } else { + sscanf(line, "%lx-%lx %*c%*c%*c%*c %lx", + &vma->vm_start, &vma->vm_end, &vma->vm_pgoff); + } + } + + fclose(file); + return 0; +} + +static int check_dump_info_correctness(struct dump_info *crib_info, struct dump_info *proc_info) +{ + if (crib_info->task.pid != proc_info->task.pid || + crib_info->task.flags != proc_info->task.flags || + crib_info->task.prio - 100 != proc_info->task.prio || + crib_info->task.policy != proc_info->task.policy || + crib_info->task.exit_code != proc_info->task.exit_code || + strncmp(crib_info->task.comm, proc_info->task.comm + 1, + strlen(crib_info->task.comm))) + return -1; + + if (crib_info->mm.start_code != proc_info->mm.start_code || + crib_info->mm.end_code != proc_info->mm.end_code || + crib_info->mm.start_data != proc_info->mm.start_data || + crib_info->mm.end_data != proc_info->mm.end_data || + crib_info->mm.start_brk != proc_info->mm.start_brk || + crib_info->mm.arg_start != proc_info->mm.arg_start || + crib_info->mm.arg_end != proc_info->mm.arg_end || + crib_info->mm.env_start != proc_info->mm.env_start || + crib_info->mm.env_end != proc_info->mm.env_end || + crib_info->mm.start_stack != proc_info->mm.start_stack) + return -1; + + struct vma *crib_vma, *proc_vma; + for (int i = 0; i < crib_info->mm.map_count; i++) { + crib_vma = &crib_info->vma[i]; + proc_vma = &proc_info->vma[i]; + if (crib_vma->vm_start != proc_vma->vm_start || + crib_vma->vm_end != proc_vma->vm_end || + crib_vma->vm_flags != proc_vma->vm_flags || + crib_vma->vm_pgoff << CONFIG_PAGE_SHIFT != proc_vma->vm_pgoff) + return -1; + } + return 0; +} + +TEST(dump_task) +{ + struct prog_args args = { + .pid = getpid(), + }; + ASSERT_GT(args.pid, 0); + + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + + struct test_dump_task_bpf *skel = test_dump_task_bpf__open_and_load(); + ASSERT_NE(skel, NULL); + + int dump_task_stat_fd = bpf_program__fd(skel->progs.dump_task_stat); + ASSERT_GT(dump_task_stat_fd, 0); + + int dump_all_vma_fd = bpf_program__fd(skel->progs.dump_all_vma); + ASSERT_GT(dump_all_vma_fd, 0); + + struct dump_info crib_info, proc_info; + memset(&crib_info, 0, sizeof(struct dump_info)); + memset(&proc_info, 0, sizeof(struct dump_info)); + + struct ring_buffer *rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), handle_event, + &crib_info, NULL); + ASSERT_NE(rb, NULL); + + clock_t crib_begin = clock(); + + ASSERT_EQ(bpf_prog_test_run_opts(dump_task_stat_fd, &opts), 0); + ASSERT_EQ(bpf_prog_test_run_opts(dump_all_vma_fd, &opts), 0); + + ASSERT_GT(ring_buffer__poll(rb, 100), 0); + + clock_t crib_end = clock(); + + printf("CRIB dump took %f seconds\n", (double)(crib_end - crib_begin) / CLOCKS_PER_SEC); + + clock_t proc_begin = clock(); + + proc_info.vma = (struct vma *)malloc(sizeof(struct vma) * (crib_info.mm.map_count + 1)); + ASSERT_EQ(dump_task_and_mm_struct_from_proc(&proc_info), 0); + ASSERT_EQ(dump_vma_from_proc(&proc_info), 0); + + clock_t proc_end = clock(); + + printf("PROC dump took %f seconds\n", (double)(proc_end - proc_begin) / CLOCKS_PER_SEC); + + ASSERT_EQ(check_dump_info_correctness(&crib_info, &proc_info), 0); + + free(crib_info.vma); + free(proc_info.vma); + ring_buffer__free(rb); + test_dump_task_bpf__destroy(skel); +} + +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/crib/test_dump_task.h b/tools/testing/selftests/crib/test_dump_task.h new file mode 100644 index 000000000000..b3cf3df852bf --- /dev/null +++ b/tools/testing/selftests/crib/test_dump_task.h @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#ifndef __TEST_DUMP_TASK_H +#define __TEST_DUMP_TASK_H + +#define EVENT_TYPE_VMA 0 +#define EVENT_TYPE_TASK 1 +#define EVENT_TYPE_MM 2 + +#define VM_READ 0x00000001 +#define VM_WRITE 0x00000002 +#define VM_EXEC 0x00000004 +#define VM_SHARED 0x00000008 +#define VM_MAYREAD 0x00000010 +#define VM_MAYWRITE 0x00000020 +#define VM_MAYEXEC 0x00000040 +#define VM_MAYSHARE 0x00000080 +#define VM_GROWSDOWN 0x00000100 +#define VM_UFFD_MISSING 0x00000200 +#define VM_MAYOVERLAY 0x00000200 +#define VM_PFNMAP 0x00000400 +#define VM_UFFD_WP 0x00001000 +#define VM_LOCKED 0x00002000 +#define VM_IO 0x00004000 +#define VM_SEQ_READ 0x00008000 +#define VM_RAND_READ 0x00010000 +#define VM_DONTCOPY 0x00020000 +#define VM_DONTEXPAND 0x00040000 +#define VM_LOCKONFAULT 0x00080000 +#define VM_ACCOUNT 0x00100000 +#define VM_NORESERVE 0x00200000 +#define VM_HUGETLB 0x00400000 +#define VM_SYNC 0x00800000 +#define VM_ARCH_1 0x01000000 +#define VM_WIPEONFORK 0x02000000 +#define VM_DONTDUMP 0x04000000 +#define VM_SOFTDIRTY 0x08000000 +#define VM_MIXEDMAP 0x10000000 +#define VM_HUGEPAGE 0x20000000 +#define VM_NOHUGEPAGE 0x40000000 +#define VM_MERGEABLE 0x80000000 + +struct prog_args { + int pid; +}; + +struct event_hdr { + int type; + int subtype; +}; + +struct event_task { + struct event_hdr hdr; + int pid; + unsigned int flags; + int prio; + unsigned int policy; + int exit_code; + char comm[16]; +}; + +struct event_vma { + struct event_hdr hdr; + unsigned long vm_start; + unsigned long vm_end; + unsigned long vm_flags; + unsigned long vm_pgoff; +}; + +struct event_mm { + struct event_hdr hdr; + unsigned long start_code; + unsigned long end_code; + unsigned long start_data; + unsigned long end_data; + unsigned long start_brk; + unsigned long brk; + unsigned long start_stack; + unsigned long arg_start; + unsigned long arg_end; + unsigned long env_start; + unsigned long env_end; + int map_count; +}; + +#endif /* __TEST_DUMP_TASK_H */ From patchwork Wed Jul 10 18:40:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729587 Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011077.outbound.protection.outlook.com [52.103.32.77]) (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 BC62B198E75; Wed, 10 Jul 2024 18:44:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637048; cv=fail; b=S0hfpruu3V0ji7QkVZciWG7wUSbXOIlOwYa5K1/mVfePpyrf9Vjv5tB2tPOgp2gaK1w5yiyrnzsCQxk5+EzznmlfmP1pAFK+5hStZ15kx2IoMdvsP3xHVgAYw7wDioi7+o5OOfmVuISE1Y7f1tvrkuSV1ftX2a8CV44xlhjW2z4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637048; c=relaxed/simple; bh=5TW4kvWjzIu1SEuTl/snc51X+iT54VWLIrNbBmuVuqc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=DwEXn0JQ9dg82RvD7CvTqo5QosLCRR0LsIGNmmXmJ9U79MO1eHP79t/QfHSwLp7uQnPFpPcDdtwn7a/8oX7B25IZq47NFl5CcVBngoe153ARi+sHX7TwCZZcSNW9wbX/EighOTCGv1BoG/cvDGyjpE55BZDbOKP2mPdOvIcliFo= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=SbL+B/Ma; arc=fail smtp.client-ip=52.103.32.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="SbL+B/Ma" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BRgdA3dy25uNxeTxYxO03Hp0MGO8nzn7PR9gHga3kMRyx0iXNQpTyizRaV6WntohgHWTS6EITJpgBgaRHMc7kwTnxrUrf4c/8u5skWGjSLAOHwEJGYF0NfGWAtSoGFOK/fQZlurD39dXJ66FGCVvRwr33njfjHAIMQWxwv7eBUKonkz7ayVTF3802gQjDP3V4gyJ6muCHivTh9P9l+YVhaWzBZmVN/v2sKSBhVioGUBDhIbe8IcsXeZk3bnvS56aZBZ96hkP6eHbVMXdWWP9m16ZQPf4lff/mRsohDxw7dvQuazEfRrhVWaS4E80APYp1yaBwA0rYAHsvOKUREgYhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=6lSfBlFkQvQHqEJMnh5buyMUegVuR9j0s9Qj5N1B+78=; b=SqtGWh5ItGcsYAGBLEaHb3G4HxKKyk6Eoj2/11LzE+E5pmCnj3vMrEWz8QPUQbywAUHryJcC2t+VDZmIXjBsYUh/ftdmPf7IuIjW7gxlKIe4jiDL6DLTYTmsvHp94eYCDxEnP+rBNqZ10FbBab9wyBS1YYTNePrSZNIZ+4CcBwdOLnF/y19wAKZFOGkIN3gBE0wQ9jGdZwIQ/PXWIuULfmx9KSPePwyVTh4kSY6IlEjKF2x6QX85RjOAtL8n21PNBzGF8gpTd4Ntd2RQShUd1thClrnp1sAl130N+Bjd4nLo6lWk6Rg+XjjXpd0P9fAYWF+Rd1bjAkkSGST/wW/hvA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=6lSfBlFkQvQHqEJMnh5buyMUegVuR9j0s9Qj5N1B+78=; b=SbL+B/Ma24QR+MGSokU3O+QLLocNjWNhxtfD3I86Xv/21FZAMlDqrXXXgcAF2iO2iMvAjCY0eh6Pn6QcXad/7j2TCOvC/KIpxTsdiDRUPzednWBb+PERG91XwRotpsRRCiZ8lE0eZThlUkwBDDaZ+HQWaFhi7fOM9krTeHKwJ75mWQet29YXSDbP3cIMbb5Ie3odmkY8kANxXOAOxOT7K94HIT1I61jiXV3Nz+3nzPe3izvZjdtAuQFlP7Dv16eVBJd/nvz8JT5zu8oa1BBugrxusWb1pB9iz7KJEx0zcM2WToFY9/5W63fYt/BrUXse6sOcuIe4og7J0mJ8ObRrtg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AS8PR03MB9935.eurprd03.prod.outlook.com (2603:10a6:20b:628::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Wed, 10 Jul 2024 18:44:03 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:44:03 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 15/16] selftests/crib: Add test for getting all socket information of the process Date: Wed, 10 Jul 2024 19:40:59 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [44+gGIy6Dbi1B3jWJ2wUQBuYICrzCPVd] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-16-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|AS8PR03MB9935:EE_ X-MS-Office365-Filtering-Correlation-Id: d461cb6c-df04-44f9-fc89-08dca11044d2 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|19110799003|461199028|3412199025|440099028; X-Microsoft-Antispam-Message-Info: y4Y73mDBRlC+9hETk9PHDxFXYqVKJethSUz9fNRShtiYVWcvb2K5fylMJ4e+ngUMVGjT+CfkUugv+OqKv7CRj3iJsMN9kv/RandyZlqMhaFlr5VvffU/XqQ8NCFnQpv+7Jfh2d80DnnUnxDYojiamllpBWacGxYWCT1USZQScwWVTG9bJp9Gi9H4iojirmvkO63olEt0ub7uZ3Qm+v3Ey0yIu/Zorb27/aeyT0gyjK0ECcnuJDPrfZ4DVSvRX33BKGhGaUvXYn3q6JfX+WcLdK5AjKKoKO8ZTJNzZ3dFrT+D//w7TcKTirPaKXL6nKYUX2ZzfmgbP2K2zkDtX3QmTk/28c2fvooUCyhnWO/5Mu2ufzXRLe53RfGe/BNSmOIDzo/qpkxB0OlvYxSxyfT3lsmatka7TZJje4ab5OycvI24Y5HmjrHNNTJzxSIvkIgMZn+yE5qrgChM2iUrj4oHyLYRc9VZwfWHMqiwnYLBHhtHmYDBaWDDUAB8a+0gc4him0bL+4WyeVJHuA/UERtHkUvIblK7BSM3FdUHRBVPIVWnkYY7rpVw+Cu0Yr0g5xHsQZ85jh2Sd5X+Fgv6MOPKDBa4QGH8V556c0p5YXIA+KirXOeKbj3BOwrcJtPb6u/0q6AjoxvtG8qfXpbZOsgOBg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: JUMplXfp9PCj1xCZCYcOrPzVsJXlgF/ojTk23FrZduvIq++Eb0fit0CQNSVpKMtOpcXelwiXRn1pdokNDd9cIzhVn2DRavj9PxzFYcITF99baxtAk3dga/vE3itoUmjgMuTEG5M4y346YnW16JubItLObUDaDhbvsbkJ8k0bW6wDDnckigdQsvRC9kPdZsWA7gJbnREMGksjx8P/DPuTdFR2HcaBBMx0GHSV8fDq5qDTzoSPX5NuMPgVwvG7jGDROPLLKnN5zcdy76ry413wrH/N+dbQCoofQMJ03Q0BhtxVeXcha7by395puVGghLyQR+goE+X4v/4tet9a2K5xO7phMQ9MDqT5ZQWhvA36ZdeetL2ssO72kzRpGjGkQiJAVO2AWFgn8dhrfED74KugNQyKSvO7zUt7yAIoPOUvZg9Qmbtjxn+R3b5O5Vn33bcRdKqR2jnsmhGlWt53/819DyISNiZE7nQF5hF5KwyVAREWnTRErlfBUBt/uFZB0vKwLJ8rvs8jV0fjwGsGL2b6tGrgrOYFrxYWVys6sdSXy4YxD/Mua31D20IprCZ2S0GmgXDfWA3FP/Cg9eZL16DGMj9w4/k/DEbhvoNKKgycTbcJ1V2lhfBsOgM+dbmhZMX2fXFdLN9g3BKbCXhjhcZHL8LEioLrFITb16jlXW0EqTySuOJGLi4rpIYfThYz2pDL7YDyH58ouGVZTTNhkreza/RwjJRuowLbQWAnorDG8h5FnWW+MDtrD1qmY9e2IQgXAmQ8dcwTTN6F0zPNRrm4O2ktQiBv5OsBdBjns4S8SZrn5YT6HvRGUuhWEyussygrmwt29I9EZWTZEKxTlGf1mKVAGpbWSUyVzNQyW77piZduRNmdYSZUqujbdGu5o8I2iqyci7u9KG03Y3zw5NfrjXGzz79tTrZVFYJ8tpj2PEXqCkNDj4v9qIxpZygiBV7e/3brd2xG/YxX980lqDSHO9DJ11R5JDpk1uzq3cU/S6ywrebSyO6kJFzgkNygL/dzQ8pfM8OzjH3BkaGsWrMALSrzxHkgrSMwM450V15+72npDf+FEBy4a4Zu8G70pFegTbEgHZvRyJcuPA0EErUP6bsSb0/0mSceAg5uk9p1A+bDI/Ib1zQbSI+uEIMonvGXPPrj6rumhOofVq+EA+OS6XqFqpz/vlfHPbfP/dOXU8aMznQcVrtQUuxfFRJdLOUh2x4NMAFtQHEHn8vXvHUg1PIFJesdfSkkR66CVz/AfUY= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: d461cb6c-df04-44f9-fc89-08dca11044d2 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:44:02.9794 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR03MB9935 X-Patchwork-State: RFC In this test, all socket information of the process is obtained through CRIB, including socket type, socket protocol, destination/source address, TCP underlying protocol information, etc. The socket information obtained through CRIB will be compared with known information and with information obtained through getsockopt to verify the correctness of the information. Signed-off-by: Juntong Deng --- .../selftests/crib/test_dump_all_socket.bpf.c | 252 ++++++++++++ .../selftests/crib/test_dump_all_socket.c | 375 ++++++++++++++++++ .../selftests/crib/test_dump_all_socket.h | 69 ++++ 3 files changed, 696 insertions(+) create mode 100644 tools/testing/selftests/crib/test_dump_all_socket.bpf.c create mode 100644 tools/testing/selftests/crib/test_dump_all_socket.c create mode 100644 tools/testing/selftests/crib/test_dump_all_socket.h diff --git a/tools/testing/selftests/crib/test_dump_all_socket.bpf.c b/tools/testing/selftests/crib/test_dump_all_socket.bpf.c new file mode 100644 index 000000000000..c124a0491ca3 --- /dev/null +++ b/tools/testing/selftests/crib/test_dump_all_socket.bpf.c @@ -0,0 +1,252 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#include "vmlinux.h" + +#include +#include +#include + +#include "test_dump_all_socket.h" + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 100000); +} rb SEC(".maps"); + +extern struct task_struct *bpf_task_from_vpid(pid_t vpid) __ksym; +extern void bpf_task_release(struct task_struct *p) __ksym; + +extern struct sock *bpf_sock_from_task_fd(struct task_struct *task, int fd) __ksym; +extern struct sock *bpf_sock_from_socket(struct socket *sock) __ksym; +extern void bpf_sock_release(struct sock *sk) __ksym; + +extern struct socket *bpf_socket_from_file(struct file *file) __ksym; +extern struct sock_common *bpf_sock_common_from_sock(struct sock *sk) __ksym; +extern struct tcp_sock *bpf_tcp_sock_from_sock(struct sock *sk) __ksym; +extern struct udp_sock *bpf_udp_sock_from_sock(struct sock *sk) __ksym; + +extern int bpf_inet_src_addr_from_socket(struct socket *sock, struct sockaddr_in *addr) __ksym; +extern int bpf_inet_dst_addr_from_socket(struct socket *sock, struct sockaddr_in *addr) __ksym; +extern int bpf_inet6_src_addr_from_socket(struct socket *sock, struct sockaddr_in6 *addr) __ksym; +extern int bpf_inet6_dst_addr_from_socket(struct socket *sock, struct sockaddr_in6 *addr) __ksym; + +extern int bpf_iter_task_file_new(struct bpf_iter_task_file *it, struct task_struct *task) __ksym; +extern struct file *bpf_iter_task_file_next(struct bpf_iter_task_file *it) __ksym; +extern int bpf_iter_task_file_get_fd(struct bpf_iter_task_file *it) __ksym; +extern void bpf_iter_task_file_destroy(struct bpf_iter_task_file *it) __ksym; + +SEC("crib") +int dump_udp_socket(struct prog_args *arg) +{ + int err = 0; + + struct task_struct *task = bpf_task_from_vpid(arg->pid); + if (!task) { + err = -1; + goto error; + } + + struct sock *sk = bpf_sock_from_task_fd(task, arg->sockfd); + if (!sk) { + err = -1; + goto error_sock; + } + + struct event_udp *e_udp = bpf_ringbuf_reserve(&rb, sizeof(struct event_udp), 0); + if (!e_udp) { + err = -2; + goto error_buf; + } + + struct udp_sock *up = bpf_udp_sock_from_sock(sk); + + e_udp->hdr.type = EVENT_TYPE_UDP; + e_udp->hdr.sockfd = arg->sockfd; + e_udp->udp_flags = BPF_CORE_READ(up, udp_flags); + e_udp->len = BPF_CORE_READ(up, len); + e_udp->pending = BPF_CORE_READ(up, pending); + + bpf_ringbuf_submit(e_udp, 0); + +error_buf: + bpf_sock_release(sk); +error_sock: + bpf_task_release(task); +error: + return err; +} + +SEC("crib") +int dump_tcp_socket(struct prog_args *arg) +{ + int err = 0; + + struct task_struct *task = bpf_task_from_vpid(arg->pid); + if (!task) { + err = -1; + goto error; + } + + struct sock *sk = bpf_sock_from_task_fd(task, arg->sockfd); + if (!sk) { + err = -1; + goto error_sock; + } + + struct event_tcp *e_tcp = bpf_ringbuf_reserve(&rb, sizeof(struct event_tcp), 0); + if (!e_tcp) { + err = -2; + goto error_buf; + } + + struct tcp_sock *tp = bpf_tcp_sock_from_sock(sk); + + e_tcp->hdr.type = EVENT_TYPE_TCP; + e_tcp->hdr.sockfd = arg->sockfd; + e_tcp->snd_wl1 = BPF_CORE_READ(tp, snd_wl1); + e_tcp->snd_wnd = BPF_CORE_READ(tp, snd_wnd); + e_tcp->max_window = BPF_CORE_READ(tp, max_window); + e_tcp->rcv_wnd = BPF_CORE_READ(tp, rcv_wnd); + e_tcp->rcv_wup = BPF_CORE_READ(tp, rcv_wup); + e_tcp->write_seq = BPF_CORE_READ(tp, write_seq); + e_tcp->rcv_nxt = BPF_CORE_READ(tp, rcv_nxt); + + bpf_ringbuf_submit(e_tcp, 0); + +error_buf: + bpf_sock_release(sk); +error_sock: + bpf_task_release(task); +error: + return err; +} + +static int dump_inet_addr(struct socket *sock, int sockfd) +{ + struct event_inet_addr *e_src_addr = bpf_ringbuf_reserve(&rb, sizeof(struct event_inet_addr), 0); + if (!e_src_addr) { + return -2; + } + + struct event_inet_addr *e_dst_addr = bpf_ringbuf_reserve(&rb, sizeof(struct event_inet_addr), 0); + if (!e_dst_addr) { + bpf_ringbuf_discard(e_src_addr, 0); + return -2; + } + + e_src_addr->hdr.type = EVENT_TYPE_INET_ADDR; + e_src_addr->hdr.subtype = EVENT_SUBTYPE_ADDR_SRC; + e_src_addr->hdr.sockfd = sockfd; + + e_dst_addr->hdr.type = EVENT_TYPE_INET_ADDR; + e_dst_addr->hdr.subtype = EVENT_SUBTYPE_ADDR_DST; + e_dst_addr->hdr.sockfd = sockfd; + + bpf_inet_src_addr_from_socket(sock, &e_src_addr->addr); + bpf_inet_dst_addr_from_socket(sock, &e_dst_addr->addr); + + bpf_ringbuf_submit(e_src_addr, 0); + bpf_ringbuf_submit(e_dst_addr, 0); + + return 0; +} + +static int dump_inet6_addr(struct socket *sock, int sockfd) +{ + struct event_inet6_addr *e_src_addr = bpf_ringbuf_reserve(&rb, sizeof(struct event_inet6_addr), 0); + if (!e_src_addr) { + return -2; + } + + struct event_inet6_addr *e_dst_addr = bpf_ringbuf_reserve(&rb, sizeof(struct event_inet6_addr), 0); + if (!e_dst_addr) { + bpf_ringbuf_discard(e_src_addr, 0); + return -2; + } + + e_src_addr->hdr.type = EVENT_TYPE_INET6_ADDR; + e_src_addr->hdr.subtype = EVENT_SUBTYPE_ADDR_SRC; + e_src_addr->hdr.sockfd = sockfd; + + e_dst_addr->hdr.type = EVENT_TYPE_INET6_ADDR; + e_dst_addr->hdr.subtype = EVENT_SUBTYPE_ADDR_DST; + e_dst_addr->hdr.sockfd = sockfd; + + bpf_inet6_src_addr_from_socket(sock, &e_src_addr->addr); + bpf_inet6_dst_addr_from_socket(sock, &e_dst_addr->addr); + + bpf_ringbuf_submit(e_src_addr, 0); + bpf_ringbuf_submit(e_dst_addr, 0); + + return 0; +} + +SEC("crib") +int dump_all_socket(struct prog_args *arg) +{ + int err = 0; + + struct task_struct *task = bpf_task_from_vpid(arg->pid); + if (!task) { + err = -1; + goto error; + } + + struct bpf_iter_task_file file_it; + struct file *cur_file; + + bpf_iter_task_file_new(&file_it, task); + while ((cur_file = bpf_iter_task_file_next(&file_it))) { + struct socket *sock = bpf_socket_from_file(cur_file); + if (!sock) { + continue; + } + + struct event_socket *e_socket = bpf_ringbuf_reserve(&rb, sizeof(struct event_socket), 0); + if (!e_socket) { + err = -2; + goto error_buf; + } + + struct sock *sk = bpf_sock_from_socket(sock); + struct sock_common *sk_cm = bpf_sock_common_from_sock(sk); + + int sock_family = BPF_CORE_READ(sk_cm, skc_family); + int sock_state = BPF_CORE_READ(sk_cm, skc_state); + int sock_type = BPF_CORE_READ(sk, sk_type); + int sock_protocol = BPF_CORE_READ(sk, sk_protocol); + int fd = bpf_iter_task_file_get_fd(&file_it); + + bpf_sock_release(sk); + + e_socket->hdr.type = EVENT_TYPE_SOCKET; + e_socket->hdr.sockfd = fd; + e_socket->family = sock_family; + e_socket->state = sock_state; + e_socket->type = sock_type; + e_socket->protocol = sock_protocol; + + bpf_ringbuf_submit(e_socket, 0); + + if (sock_family == PF_INET) + err = dump_inet_addr(sock, fd); + else if (sock_family == PF_INET6) + err = dump_inet6_addr(sock, fd); + + if (err) { + goto error_buf; + } + } + +error_buf: + bpf_iter_task_file_destroy(&file_it); + bpf_task_release(task); +error: + return err; +} diff --git a/tools/testing/selftests/crib/test_dump_all_socket.c b/tools/testing/selftests/crib/test_dump_all_socket.c new file mode 100644 index 000000000000..15add7fbf4f4 --- /dev/null +++ b/tools/testing/selftests/crib/test_dump_all_socket.c @@ -0,0 +1,375 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#include "test_dump_all_socket.h" +#include "test_dump_all_socket.bpf.skel.h" + +struct tcp_dump_info { + unsigned int snd_wl1; + unsigned int snd_wnd; + unsigned int max_window; + unsigned int rcv_wnd; + unsigned int rcv_wup; + unsigned int write_seq; + unsigned int rcv_nxt; +}; + +struct udp_dump_info { + int udp_flags; + int len; + int pending; +}; + +struct socket_dump_info { + int sockfd; + int family; + int type; + int protocol; + union { + struct sockaddr_in src_addr4; + struct sockaddr_in6 src_addr6; + }; + union { + struct sockaddr_in dst_addr4; + struct sockaddr_in6 dst_addr6; + }; + union { + struct tcp_dump_info tcp; + struct udp_dump_info udp; + }; +}; + +static int dump_all_socket_fd; +static int dump_tcp_socket_fd; +static int dump_udp_socket_fd; + +static int tcp_client_fd; +static int tcp_server_fd; +static int tcp_accept_fd; +static int udp_client_fd; + +static int socket_count; + +static struct socket_dump_info *find_dump_info_by_sockfd(struct socket_dump_info *all_info, int sockfd) +{ + struct socket_dump_info *info; + for (int i = 0; i < 4; i++) { + info = &all_info[i]; + if (info->sockfd == sockfd) + return info; + } + return NULL; +} + +static int handle_tcp_event(struct socket_dump_info *all_info, struct event_tcp *e_tcp) +{ + struct socket_dump_info *info = find_dump_info_by_sockfd(all_info, e_tcp->hdr.sockfd); + info->tcp.snd_wl1 = e_tcp->snd_wl1; + info->tcp.snd_wnd = e_tcp->snd_wnd; + info->tcp.max_window = e_tcp->max_window; + info->tcp.rcv_wnd = e_tcp->rcv_wnd; + info->tcp.rcv_wup = e_tcp->rcv_wup; + info->tcp.write_seq = e_tcp->write_seq; + info->tcp.rcv_nxt = e_tcp->rcv_nxt; + return 0; +} + +static int handle_udp_event(struct socket_dump_info *all_info, struct event_udp *e_udp) +{ + struct socket_dump_info *info = find_dump_info_by_sockfd(all_info, e_udp->hdr.sockfd); + info->udp.udp_flags = e_udp->udp_flags; + info->udp.len = e_udp->len; + info->udp.pending = e_udp->pending; + return 0; +} + +static int handle_inet_addr_event(struct socket_dump_info *all_info, struct event_inet_addr *e_inet_addr) +{ + struct socket_dump_info *info = &all_info[socket_count - 1]; + if (e_inet_addr->hdr.subtype == EVENT_SUBTYPE_ADDR_SRC) + memcpy(&info->src_addr4, &e_inet_addr->addr, sizeof(struct sockaddr_in)); + else if (e_inet_addr->hdr.subtype == EVENT_SUBTYPE_ADDR_DST) + memcpy(&info->dst_addr4, &e_inet_addr->addr, sizeof(struct sockaddr_in)); + return 0; +} + +static int handle_inet6_addr_event(struct socket_dump_info *all_info, struct event_inet6_addr *e_inet6_addr) +{ + struct socket_dump_info *info = &all_info[socket_count - 1]; + if (e_inet6_addr->hdr.subtype == EVENT_SUBTYPE_ADDR_SRC) + memcpy(&info->src_addr6, &e_inet6_addr->addr, sizeof(struct sockaddr_in6)); + else if (e_inet6_addr->hdr.subtype == EVENT_SUBTYPE_ADDR_DST) + memcpy(&info->dst_addr6, &e_inet6_addr->addr, sizeof(struct sockaddr_in6)); + return 0; +} + +static int handle_socket_event(struct socket_dump_info *all_info, struct event_socket *e_socket) +{ + struct prog_args arg = { + .pid = getpid(), + .sockfd = e_socket->hdr.sockfd + }; + + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, tattrs, + .ctx_in = &arg, + .ctx_size_in = sizeof(arg), + ); + + struct socket_dump_info *info = &all_info[socket_count]; + info->sockfd = e_socket->hdr.sockfd; + info->family = e_socket->family; + info->type = e_socket->type; + info->protocol = e_socket->protocol; + + int err = 0; + if (e_socket->protocol == IPPROTO_TCP) + err = bpf_prog_test_run_opts(dump_tcp_socket_fd, &tattrs); + else if (e_socket->protocol == IPPROTO_UDP) + err = bpf_prog_test_run_opts(dump_udp_socket_fd, &tattrs); + + socket_count++; + + return err; +} + +static int handle_event(void *ctx, void *data, size_t data_sz) +{ + struct socket_dump_info *all_info = (struct socket_dump_info *)ctx; + const struct event_hdr *e_hdr = data; + int err = 0; + + switch (e_hdr->type) { + case EVENT_TYPE_TCP: + handle_tcp_event(all_info, (struct event_tcp *)data); + break; + case EVENT_TYPE_UDP: + handle_udp_event(all_info, (struct event_udp *)data); + break; + case EVENT_TYPE_SOCKET: + handle_socket_event(all_info, (struct event_socket *)data); + break; + case EVENT_TYPE_INET_ADDR: + handle_inet_addr_event(all_info, (struct event_inet_addr *)data); + break; + case EVENT_TYPE_INET6_ADDR: + handle_inet6_addr_event(all_info, (struct event_inet6_addr *)data); + break; + default: + err = -1; + printf("Unknown event type!\n"); + break; + } + return err; +} + +static int check_tcp_dump_info_correctness(struct socket_dump_info *info) +{ + const int enable = 1; + if (info->family != AF_INET || info->type != SOCK_STREAM || + info->protocol != IPPROTO_TCP) + return -1; + + if (info->dst_addr4.sin_family != AF_INET || info->src_addr4.sin_family != AF_INET) + return -1; + + if (info->sockfd == tcp_client_fd && (info->dst_addr4.sin_addr.s_addr != htonl(INADDR_LOOPBACK) || + info->dst_addr4.sin_port != htons(5555))) + return -1; + + if (info->sockfd == tcp_server_fd && (info->src_addr4.sin_addr.s_addr != htonl(INADDR_ANY) || + info->src_addr4.sin_port != htons(5555))) + return -1; + + if (info->sockfd == tcp_accept_fd && (info->src_addr4.sin_addr.s_addr != htonl(INADDR_LOOPBACK) || + info->dst_addr4.sin_addr.s_addr != htonl(INADDR_LOOPBACK) || + info->src_addr4.sin_port != htons(5555))) + return -1; + + if (info->sockfd != tcp_server_fd) { + if (setsockopt(info->sockfd, IPPROTO_TCP, TCP_REPAIR, &enable, sizeof(enable))) + return -1; + + struct tcp_repair_window opt; + socklen_t optlen = sizeof(opt); + if (getsockopt(info->sockfd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &opt, &optlen)) + return -1; + + if (opt.snd_wl1 != info->tcp.snd_wl1 || opt.snd_wnd != info->tcp.snd_wnd || + opt.max_window != info->tcp.max_window || opt.rcv_wnd != info->tcp.rcv_wnd || + opt.rcv_wup != info->tcp.rcv_wup) + return -1; + + int queue = TCP_SEND_QUEUE; + if (setsockopt(info->sockfd, IPPROTO_TCP, TCP_REPAIR_QUEUE, &queue, sizeof(queue))) + return -1; + + unsigned int write_seq; + optlen = sizeof(write_seq); + if (getsockopt(info->sockfd, IPPROTO_TCP, TCP_QUEUE_SEQ, &write_seq, &optlen)) + return -1; + + if (write_seq != info->tcp.write_seq) + return -1; + + queue = TCP_RECV_QUEUE; + if (setsockopt(info->sockfd, IPPROTO_TCP, TCP_REPAIR_QUEUE, &queue, sizeof(queue))) + return -1; + + unsigned int rcv_nxt; + if (getsockopt(info->sockfd, IPPROTO_TCP, TCP_QUEUE_SEQ, &rcv_nxt, &optlen)) + return -1; + + if (rcv_nxt != info->tcp.rcv_nxt) + return -1; + } + return 0; +} + +static int check_udp_dump_info_correctness(struct socket_dump_info *info) +{ + if (info->family != AF_INET6 || info->type != SOCK_DGRAM || + info->protocol != IPPROTO_UDP) + return -1; + + if (info->dst_addr6.sin6_family != AF_INET6 || info->dst_addr6.sin6_port != htons(7777) || + memcmp(&info->dst_addr6.sin6_addr, &in6addr_loopback, sizeof(struct in6_addr)) != 0) + return -1; + + return 0; +} + +static int check_dump_info_correctness(struct socket_dump_info *all_info) +{ + struct socket_dump_info *info; + for (int i = 0; i < 4; i++) { + info = &all_info[i]; + + if (info->sockfd <= 0) + return -1; + + if (info->sockfd == udp_client_fd) { + if (check_udp_dump_info_correctness(info) != 0) + return -1; + } else { + if (check_tcp_dump_info_correctness(info) != 0) + return -1; + } + + } + return 0; +} + +TEST(dump_all_socket) +{ + struct prog_args args = { + .pid = getpid(), + }; + ASSERT_GT(args.pid, 0); + + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + + tcp_client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + ASSERT_GT(tcp_client_fd, 0); + + tcp_server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + ASSERT_GT(tcp_server_fd, 0); + + udp_client_fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + ASSERT_GT(udp_client_fd, 0); + + const int enable = 1; + ASSERT_EQ(setsockopt(tcp_server_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)), 0); + + struct sockaddr_in tcp_src_addr, tcp_dst_addr; + struct sockaddr_in6 udp_dst_addr; + memset(&tcp_src_addr, 0, sizeof(struct sockaddr_in)); + memset(&tcp_dst_addr, 0, sizeof(struct sockaddr_in)); + memset(&udp_dst_addr, 0, sizeof(struct sockaddr_in6)); + + tcp_src_addr.sin_family = AF_INET; + tcp_src_addr.sin_addr.s_addr = htonl(INADDR_ANY); + tcp_src_addr.sin_port = htons(5555); + + tcp_dst_addr.sin_family = AF_INET; + tcp_dst_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + tcp_dst_addr.sin_port = htons(5555); + + udp_dst_addr.sin6_family = AF_INET6; + udp_dst_addr.sin6_addr = in6addr_loopback; + udp_dst_addr.sin6_port = htons(7777); + + ASSERT_EQ(bind(tcp_server_fd, (struct sockaddr *)&tcp_src_addr, sizeof(struct sockaddr_in)), 0); + ASSERT_EQ(listen(tcp_server_fd, 100), 0); + + ASSERT_EQ(connect(tcp_client_fd, (struct sockaddr *)&tcp_dst_addr, sizeof(struct sockaddr_in)), 0); + + tcp_accept_fd = accept(tcp_server_fd, NULL, NULL); + ASSERT_GT(tcp_accept_fd, 0); + + char buf[20]; + memset(buf, 'a', 20); + ASSERT_EQ(send(tcp_client_fd, buf, 20, 0), 20); + + ASSERT_EQ(connect(udp_client_fd, (struct sockaddr *)&udp_dst_addr, sizeof(struct sockaddr_in6)), 0); + + struct test_dump_all_socket_bpf *skel = test_dump_all_socket_bpf__open_and_load(); + ASSERT_NE(skel, NULL); + + dump_all_socket_fd = bpf_program__fd(skel->progs.dump_all_socket); + ASSERT_GT(dump_all_socket_fd, 0); + + dump_tcp_socket_fd = bpf_program__fd(skel->progs.dump_tcp_socket); + ASSERT_GT(dump_tcp_socket_fd, 0); + + dump_udp_socket_fd = bpf_program__fd(skel->progs.dump_udp_socket); + ASSERT_GT(dump_udp_socket_fd, 0); + + struct socket_dump_info *all_info = (struct socket_dump_info *)malloc(sizeof(struct socket_dump_info) * 4); + + struct ring_buffer *rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), handle_event, all_info, NULL); + ASSERT_NE(rb, NULL); + + ASSERT_EQ(bpf_prog_test_run_opts(dump_all_socket_fd, &opts), 0); + + ASSERT_GT(ring_buffer__poll(rb, 100), 0); + + ASSERT_EQ(check_dump_info_correctness(all_info), 0); + + ASSERT_EQ(close(tcp_client_fd), 0); + ASSERT_EQ(close(tcp_accept_fd), 0); + ASSERT_EQ(close(tcp_server_fd), 0); + ASSERT_EQ(close(udp_client_fd), 0); + ring_buffer__free(rb); + test_dump_all_socket_bpf__destroy(skel); +} + +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/crib/test_dump_all_socket.h b/tools/testing/selftests/crib/test_dump_all_socket.h new file mode 100644 index 000000000000..04453e650469 --- /dev/null +++ b/tools/testing/selftests/crib/test_dump_all_socket.h @@ -0,0 +1,69 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#ifndef __TEST_DUMP_ALL_SOCKET_H +#define __TEST_DUMP_ALL_SOCKET_H + +#define PF_INET 2 +#define PF_INET6 10 + +#define EVENT_TYPE_TCP 0 +#define EVENT_TYPE_UDP 1 +#define EVENT_TYPE_SOCKET 2 +#define EVENT_TYPE_INET_ADDR 3 +#define EVENT_TYPE_INET6_ADDR 4 + +#define EVENT_SUBTYPE_ADDR_SRC 0 +#define EVENT_SUBTYPE_ADDR_DST 1 + +struct prog_args { + int pid; + int sockfd; +}; + +struct event_hdr { + int type; + int subtype; + int sockfd; +}; + +struct event_socket { + struct event_hdr hdr; + int family; + int state; + int type; + int protocol; +}; + +struct event_inet6_addr { + struct event_hdr hdr; + struct sockaddr_in6 addr; +}; + +struct event_inet_addr { + struct event_hdr hdr; + struct sockaddr_in addr; +}; + +struct event_tcp { + struct event_hdr hdr; + unsigned int snd_wl1; + unsigned int snd_wnd; + unsigned int max_window; + unsigned int rcv_wnd; + unsigned int rcv_wup; + unsigned int write_seq; + unsigned int rcv_nxt; +}; + +struct event_udp { + struct event_hdr hdr; + int udp_flags; + int len; + int pending; +}; + +#endif /* __TEST_DUMP_ALL_SOCKET_H */ From patchwork Wed Jul 10 18:41:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13729588 Received: from DU2PR03CU002.outbound.protection.outlook.com (mail-northeuropeazolkn19012026.outbound.protection.outlook.com [52.103.32.26]) (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 4904E19A2B4; Wed, 10 Jul 2024 18:44:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.26 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637056; cv=fail; b=jrINf+Ls8Ivqj/mViSW0OFVnIFVedxXvXm5UThx34rqpgKrSyhkXMdMu0p8u/VlrSdsNyzTA2qYr7AVu4qLdGjIdjwW/+wRbJ1UnmZJlzVmaef9O1TPpVZInxc2J/Y/MBEqcI5Z32P3297MILi6brUKgOgUKy7pMad9TwyvkvKM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720637056; c=relaxed/simple; bh=M3SGqNnbVRg01VCLB+2RQpAxbl8Mi6TvFhviVlREHDw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ZMqDE62CgZ8AjuqpelK5ecacBZmCAUbSzFiXedxaKufmomxobzFGRSTCiMwdCs/2fYnxkqeIYdXw0QLimsgovWQU4kHXCLu9sjZtlUqIZll8Cw30yxUVgM52waGOdyBockwFHYxOz7YlkjjTiu2umTp/jwAFsp6hx3dEyNCh2Kg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=bccn1Exu; arc=fail smtp.client-ip=52.103.32.26 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="bccn1Exu" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MwlGMkP3WuCRlqFXzAM4PmKDWPa38Jc+/RC9dj7wJryzNGjY/6uoD4skk3L4zSccbYbhPJbOkdS6wFR8q/42z1/mBGZaa7lzVoW8zXpYoEtmCdg9UUvVcs+RGkG8VBXnGxiWyk/LzMAnvCEFB0g3V5300tHpH0/g7OMUclxF4dJXhY1Ei1Ht3F11g8kHAvRhNIp3X4cXnmMPW3TBbTa/kbtrisco+8osqTR8sk/02f31nxhrtEV5/wlnz7qwPG0KEXAHrlK4YCqeNCmsOoFYLsuhT7niyG7JmLPL43a3QPyGrAkRDhjQhH78/IP8wnOAnTe5SkBuwGZ7vwJp4LvsVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uioMZANDC1OC1BmOa403llfPM9kYL4OgGj/M2FJpCuY=; b=Z4o3A0bIpKwOy4QmI74MPRuQXYCGbjIxQVrPITCYs9Azyzj1Ez8eQ209Le90XajXVU5wbRrQt8ycIw5Q1mkQGlvpCTBUIC6GSIpt0AmMzRQNjM+kTTIXgT3jCBHxP2sBrrCEDzA1hixK7qCdu8lvL9n+7HqqBEHYKOvcv2gAYQLEsbeM2LEtbu34nGp1+PYwwH71J243x0h1EJ6J2x+78LdxC3aU8ogwK7VyEd9oPJw/nzg+uNIOMD1rHJr/Xr9/MxSDdXfiU5va8sV6kda2ilARcFccjNiIFnoxKWDEafc5G8qJSEvOM89N+le6GkxPzS+hRlcTPv48KEl6IbclcQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uioMZANDC1OC1BmOa403llfPM9kYL4OgGj/M2FJpCuY=; b=bccn1Exuunrcah6BmETFkY2Gc8uqvOcchv7gPCCOMRpIpSI7HYv1JQZUWXbt6s3oFfTYPR71IpTzFF5sxu1esT0sZG+2FP3y4KlC5SRlZEKhNLpSctLFgnttIk06jh9fkaJo9KPX0odr8/su8pU7wwwH4xd3kcB0efHlxRuxk2CfNxOdRvLw/6bH+YCT/VfOTMNpUHZLrc9o25BWq/DcpA4TR65K2ETaY4EAIq5plmO5RuidY16nsJUsdP5ffT449fjoAo5jqvfUC5AgbfBj/MLEEg9DQVR8sM9dNi8YjgMY+yAHtv/uRwDBmlwV60rUiXlz/2yczJceLo3NvfUOdQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GVXPR03MB8308.eurprd03.prod.outlook.com (2603:10a6:150:6e::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.34; Wed, 10 Jul 2024 18:44:09 +0000 Received: from AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7]) by AM6PR03MB5848.eurprd03.prod.outlook.com ([fe80::4b97:bbdb:e0ac:6f7%6]) with mapi id 15.20.7741.033; Wed, 10 Jul 2024 18:44:09 +0000 From: Juntong Deng To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, andrii@kernel.org, avagin@gmail.com, snorcht@gmail.com Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 16/16] selftests/crib: Add test for dumping/restoring UDP socket packets Date: Wed, 10 Jul 2024 19:41:00 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240710184100.297261-1-juntong.deng@outlook.com> References: <20240710184100.297261-1-juntong.deng@outlook.com> X-TMN: [OwUYHtp8hVmzwddwmdXmXOgPRCsKbPnG] X-ClientProxiedBy: SI2PR02CA0027.apcprd02.prod.outlook.com (2603:1096:4:195::14) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240710184100.297261-17-juntong.deng@outlook.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AM6PR03MB5848:EE_|GVXPR03MB8308:EE_ X-MS-Office365-Filtering-Correlation-Id: d6e4ae4b-2d1d-412b-c51b-08dca11048c0 X-MS-Exchange-SLBlob-MailProps: znQPCv1HvwV1Kpn5lySAyQJsGKcMyMICJxe2bSatuTGDkpijd4+ipyI9P/aobk6EOwJRTP00L8Z3dNYzyzJxTmrSvIpnXEG4sUgmTKuBw1ar4onOAbq3tdlAiJynImWB7erxwJ3QPKYQc/GduuQz79cp28oJBMAzGqJhABPHbV1zgLJRLe3qKZanges8upguqYVD1YNCdsRAEx/mmkO5fnR1Lj/JweVXzUq0JwEhEywYtDEor9fjJxAmuYyrPPiegX120gpw59iXw9VdDwbNQQfqzF723y/o/xoqKLHgLwkfDSfHGxIN/xzedymv7QS2alA4pYZWeG30Nmzsg/zR2UXtaKTL6vwTNACIDlR8bB3CLPGmjrRx91ZYKqqNJgmUqfE+Cj4sSNVk1606dHYvQwPMpsqplbBiakwy5at5o6uv2oWdYWnNdlY2Z2bH80W2LTaBe8XxXeCGbV03BbvgqcxgFFtU7hw35m6aGkR4PJNcLfA45iiB/87r6VL/X/8bTcGekB3zgBVZttd8fT0ecSUxUUPBFFbVV5KcU9jUwkcEf3ssA8CzlGNfEQ1KW2hUquhjztaocIMpzcEFoROaMqKCU4ZaW6wDDMgw2wYi3zfKXEXejndJP4tG6PLDjK+EGdq0YsBLn9onba8oZw5PpCwErXaL7cDfWP/QPU8G1QPyRFDNw1qGn7i7iW6FbUt07gS/bBVte4Yy8NnOpqdLbEDRuLg0Yfhf7C9aAb8DZqi1ID+12EdjxdpWT8ekIoaWFDw9TFXzqrQ= X-Microsoft-Antispam: BCL:0;ARA:14566002|19110799003|461199028|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: Snzi4mSnl5K8awbwfbdD8NfVNDNWmoG8OWkO4zm5zoYuyEmxf3uOi7Cuon1l0oHcpnoVv5igH1ml7MUcQxLlDor0U8++zI8TEjtuRz6L788TLyWleS+BZ/wGZiKTq2ZynCUkc4uZRNFFxZI4hjGAU/H7/wjNoCq8P022Il+/2cjlJC7JoUKqpqTE5BcFfiM+cKWLTGvOZSrWJJwRa7SORYxhM8HCruVNYbvFxvDzNkIvQ+QB6pUS+GZO+brjkEWY9hTc3QVfiym5TJcq832JCiEDDg/lI2r3WUxtfIzhb/O9xjFuAdGZd1wNYm888/RN3dRUjxM0dayidNHfcuJZleUJWozbGH9J08ZM7gJ9M69mQPsK9FVSu4PsjyjDyd2NikgAQQN+g272USZj9QZVa2mXfkwtE81DqtgDnlyzqw18wTo2LRpVXCpwqI+felfMar980HvXygW2yPrke2yszsIy4mlm2wy7LppHgRYB1P4gduogjoUku2l6GhNIV+VXei2kgOIS7jVtujpKmjsQnHjK2kQ4mG46YOtfhdpMqwbxIN0zRfLuBnTfcPjjKt/033kJu7f32+44enQ2Bpj2rkBVUOdIkqADrYe4EiHkc4HvKjOaCzzR++ZVNo5n0RVVTyZp5ZD4ipwjHONkvIWtkA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: iXv7x73kqMX5xSvht8v2LJyHF2iMB4yeq5Tnj8F2wb2++L+edP2mtOcGgb4nxG47dCizrXgdFWHN+eIFSnyAm2YDlSwNuvTZC+e+6BO0ozY3L0bz/I0TGfWh63YukZl4OG1qmnV2uHS+xliI/4AV+i7B8PVrXhT1bjHPevizmZRia1kAorj6Y+T7dlpfKxG/tIfaQyaeLq5GGOC6+VkxKpLHdnaXDrnXs9VbJBN5NIQ1WWleKrC3FEplEcq7m5/EyVw5DsG7jiItwV/WifTDoU98EOwJ1+ZfUDDHCX6DOHDBV8k7c0s0qF7u1oo8uWcvdXGZE3svMQJ/V6JZb07WorVCHg8bwY3KDM/Vrgfe2krbfz8ST1DuCF84DzhGjCIExR2q6PcUdPKkfXAnzZ6tkaBdkrI5Cu5ZUm20aFf/GeeJnElIsJ0lSAwojeUoaISKWU95Lln2L2mLNwWgWo+JTIYCCeoPMBflrKQdcFam/rQm4ySBinHPmvo53xoivzDdgvKYPbUJGe4u7ncmxyqv2aiY7F/r5txQglxvAGrqKDc2x+nrLUPc63jja+zjW3GgL+pQk29iVWO1sQmLw64t9rJDUAcVT0kQzJ9gr/ftveJexTjwrk5GHVCEwxz+ntb8lPTRJz8STG3iX9rsEL7OZ4MhKyZ+aKlqmHmqQCR5x4ZNCfOyI+sCM0ZoHgXTuMLrrDaekApkZcydXQWitpdLhXXTgJdPpBWu/tmkhYtr2r4Ua+1jF/PPgYYVr5/B9gMPyUUcX0bdOmCGxpktMXUpj7YU3Ck0lSEM+CQjodXSadOfMye0sMN9Mi5rnWFdy+wuOBnc16rb4Sqo9CPF5EEHtf5jYGapD0w5fdzFfr8T2YSn0zqjEcC5EQqgisFHGBxAmpv1lQKyF3cVu4HV+xU6fX3Xw6MVZFhAAtcQT7d/DaduilWSfLgkiDUH5BT7iA39PF6ypPhXA4E0TdpUv5Xom2agkcTh2nFS+yUovRlNuqAhMp2waD4bAuMr0B6XdBbnN0AOOc/oMv5joNsBvFu6KR7QyK0dwjmAr6VC01ZTwX7gawFIpG/8qPNQgPxZ9coZS3VUuxXIb3awM6BzZHfIUmwMhjwl9GNJ9Sk30bsy/8hU2qH7TUe2+yGVYYIjScJkwAUw/NBsI1K3oBpCf1ak5CVoccVicykPES40kwj6G7ufbNucmJPeZc7CFtUvz5cZFRVnHro3Biu50y474kp0j8tNf2JvMQepu5H6WhQ5Eqo= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: d6e4ae4b-2d1d-412b-c51b-08dca11048c0 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2024 18:44:09.5937 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: GVXPR03MB8308 X-Patchwork-State: RFC In this test, UDP socket packets are dumped/restored through CRIB, including write queue and receive queue (and reader queue). A "checkpoint socket" and a "restore socket" are created, the write/receive queue packets of the "checkpoint socket" are dumped and restored to the "restore socket", and after that the "restore socket" will be checked to see if it can normally receive and send the packets that were restored to the queue. Write queue packets are not restored through the CRIB ebpf program in this test, because it is not wise to rewrite the entire UDP send process. Using regular send() is a better choice. Signed-off-by: Juntong Deng --- .../crib/test_restore_udp_socket.bpf.c | 311 ++++++++++++++++ .../selftests/crib/test_restore_udp_socket.c | 333 ++++++++++++++++++ .../selftests/crib/test_restore_udp_socket.h | 51 +++ 3 files changed, 695 insertions(+) create mode 100644 tools/testing/selftests/crib/test_restore_udp_socket.bpf.c create mode 100644 tools/testing/selftests/crib/test_restore_udp_socket.c create mode 100644 tools/testing/selftests/crib/test_restore_udp_socket.h diff --git a/tools/testing/selftests/crib/test_restore_udp_socket.bpf.c b/tools/testing/selftests/crib/test_restore_udp_socket.bpf.c new file mode 100644 index 000000000000..527ee6d72256 --- /dev/null +++ b/tools/testing/selftests/crib/test_restore_udp_socket.bpf.c @@ -0,0 +1,311 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#include "vmlinux.h" + +#include +#include +#include + +#include "test_restore_udp_socket.h" + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_RINGBUF); + __uint(max_entries, 100000); +} rb SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_USER_RINGBUF); + __uint(max_entries, 100000); +} urb SEC(".maps"); + +extern struct task_struct *bpf_task_from_vpid(pid_t vpid) __ksym; +extern void bpf_task_release(struct task_struct *p) __ksym; + +extern struct sock *bpf_sock_from_task_fd(struct task_struct *task, int fd) __ksym; +extern void bpf_sock_release(struct sock *sk) __ksym; + +extern struct udp_sock *bpf_udp_sock_from_sock(struct sock *sk) __ksym; +extern struct sk_buff_head *bpf_receive_queue_from_sock(struct sock *sk) __ksym; +extern struct sk_buff_head *bpf_write_queue_from_sock(struct sock *sk) __ksym; +extern struct sk_buff_head *bpf_reader_queue_from_udp_sock(struct udp_sock *up) __ksym; + +extern int bpf_iter_skb_new(struct bpf_iter_skb *it, struct sk_buff_head *head) __ksym; +extern struct sk_buff *bpf_iter_skb_next(struct bpf_iter_skb *it) __ksym; +extern void bpf_iter_skb_destroy(struct bpf_iter_skb *it) __ksym; + +extern int bpf_iter_skb_data_new(struct bpf_iter_skb_data *it, struct sk_buff *skb, char *buf, int buflen) __ksym; +extern char *bpf_iter_skb_data_next(struct bpf_iter_skb_data *it) __ksym; +extern void bpf_iter_skb_data_set_buf(struct bpf_iter_skb_data *it, char *buf, int buflen) __ksym; +extern int bpf_iter_skb_data_get_chunk_len(struct bpf_iter_skb_data *it) __ksym; +extern int bpf_iter_skb_data_get_offset(struct bpf_iter_skb_data *it) __ksym; +extern void bpf_iter_skb_data_destroy(struct bpf_iter_skb_data *it) __ksym; + +extern int bpf_cal_skb_size(struct sk_buff *skb) __ksym; +extern struct sk_buff *bpf_skb_peek_tail(struct sk_buff_head *head) __ksym; +extern void bpf_skb_release(struct sk_buff *skb) __ksym; + +extern struct sk_buff *bpf_restore_skb_rcv_queue(struct sk_buff_head *head, struct sock *sk, + struct bpf_crib_skb_info *skb_info) __ksym; +extern int bpf_restore_skb_data(struct sk_buff *skb, int offset, char *data, int len) __ksym; + +static int dump_skb_data(struct sk_buff *skb, int subtype, int skb_num) +{ + struct bpf_iter_skb_data skb_data_it; + int err = 0; + + /* + * Since bpf_iter_skb_data_next will dump the skb data into the buffer, + * the buffer needs to be allocated in advance + */ + struct event_skb_data *e_skb_data; + e_skb_data = bpf_ringbuf_reserve(&rb, sizeof(struct event_skb_data), 0); + if (!e_skb_data) { + err = -2; + goto error_buf; + } + + bpf_iter_skb_data_new(&skb_data_it, skb, e_skb_data->buf, sizeof(e_skb_data->buf)); + while (bpf_iter_skb_data_next(&skb_data_it)) { + e_skb_data->hdr.type = EVENT_TYPE_SKB_DATA; + e_skb_data->hdr.subtype = subtype; + e_skb_data->skb_num = skb_num; + e_skb_data->chunk_length = bpf_iter_skb_data_get_chunk_len(&skb_data_it); + e_skb_data->offset = bpf_iter_skb_data_get_offset(&skb_data_it); + bpf_ringbuf_submit(e_skb_data, 0); + + /* + * For the same reason as above, the buffer used in + * the next iteration needs to be allocated now + */ + e_skb_data = bpf_ringbuf_reserve(&rb, sizeof(struct event_skb_data), 0); + if (!e_skb_data) { + err = -2; + goto error_in_buf; + } + + bpf_iter_skb_data_set_buf(&skb_data_it, e_skb_data->buf, sizeof(e_skb_data->buf)); + } + /* Discard the pre-allocated buffer in the last iteration (it will not be used) */ + bpf_ringbuf_discard(e_skb_data, 0); + +error_in_buf: + bpf_iter_skb_data_destroy(&skb_data_it); +error_buf: + return err; +} + +static int dump_all_queue_skb(struct sk_buff_head *head, int subtype) +{ + struct bpf_iter_skb skb_it; + struct sk_buff *cur_skb; + int skb_num = 0; + int err = 0; + + bpf_iter_skb_new(&skb_it, head); + while ((cur_skb = bpf_iter_skb_next(&skb_it))) { + struct event_skb *e_skb = bpf_ringbuf_reserve(&rb, sizeof(struct event_skb), 0); + if (!e_skb) { + err = -2; + goto error; + } + + e_skb->hdr.type = EVENT_TYPE_SKB; + e_skb->hdr.subtype = subtype; + e_skb->skb_num = skb_num; + e_skb->len = BPF_CORE_READ(cur_skb, len); + e_skb->tstamp = BPF_CORE_READ(cur_skb, tstamp); + e_skb->dev_scratch = BPF_CORE_READ(cur_skb, dev_scratch); + e_skb->protocol = BPF_CORE_READ(cur_skb, protocol); + e_skb->transport_header = BPF_CORE_READ(cur_skb, transport_header); + e_skb->network_header = BPF_CORE_READ(cur_skb, network_header); + e_skb->mac_header = BPF_CORE_READ(cur_skb, mac_header); + e_skb->csum = BPF_CORE_READ(cur_skb, csum); + e_skb->csum = BPF_CORE_READ(cur_skb, csum); + e_skb->size = bpf_cal_skb_size(cur_skb); + + unsigned char *head = BPF_CORE_READ(cur_skb, head); + unsigned char *data = BPF_CORE_READ(cur_skb, data); + e_skb->headerlen = data - head; //skb_headroom + + bpf_ringbuf_submit(e_skb, 0); + + if (dump_skb_data(cur_skb, subtype, skb_num) != 0) { + err = -1; + goto error; + } + + skb_num++; + } +error: + bpf_iter_skb_destroy(&skb_it); + return err; +} + +int dump_write_queue_skb(struct sock *sk) +{ + struct sk_buff_head *write_queue_head = bpf_write_queue_from_sock(sk); + return dump_all_queue_skb(write_queue_head, EVENT_SUBTYPE_WRITE_QUEUE); +} + +int dump_receive_queue_skb(struct sock *sk) +{ + struct sk_buff_head *receive_queue_head = bpf_receive_queue_from_sock(sk); + return dump_all_queue_skb(receive_queue_head, EVENT_SUBTYPE_RECEIVE_QUEUE); +} + +int dump_reader_queue_skb(struct sock *sk) +{ + struct udp_sock *up = bpf_udp_sock_from_sock(sk); + struct sk_buff_head *reader_queue_head = bpf_reader_queue_from_udp_sock(up); + return dump_all_queue_skb(reader_queue_head, EVENT_SUBTYPE_READER_QUEUE); +} + +SEC("crib") +int dump_socket_queue(struct prog_args *arg) +{ + int err = 0; + + struct task_struct *task = bpf_task_from_vpid(arg->pid); + if (!task) { + err = -1; + goto error; + } + + struct sock *sk = bpf_sock_from_task_fd(task, arg->fd); + if (!sk) { + err = -1; + goto error_sock; + } + + dump_write_queue_skb(sk); + dump_receive_queue_skb(sk); + dump_reader_queue_skb(sk); + + struct event_hdr *e_dump_end = bpf_ringbuf_reserve(&rb, sizeof(struct event_hdr), 0); + if (!e_dump_end) { + err = -2; + goto error_buf; + } + + e_dump_end->type = EVENT_TYPE_END; + bpf_ringbuf_submit(e_dump_end, 0); + +error_buf: + bpf_sock_release(sk); +error_sock: + bpf_task_release(task); +error: + return err; +} + +static int handle_restore_skb_data(struct event_skb_data *e_skb_data, struct sk_buff_head *head) +{ + struct sk_buff *skb = bpf_skb_peek_tail(head); + if (!skb) + return -1; + + bpf_restore_skb_data(skb, e_skb_data->offset, e_skb_data->buf, e_skb_data->chunk_length); + + bpf_skb_release(skb); + return 0; +} + +static int handle_restore_skb(struct event_skb *e_skb, struct sk_buff_head *head, struct sock *sk) +{ + struct bpf_crib_skb_info skb_info; + skb_info.headerlen = e_skb->headerlen; + skb_info.len = e_skb->len; + skb_info.size = e_skb->size; + skb_info.tstamp = e_skb->tstamp; + skb_info.dev_scratch = e_skb->dev_scratch; + skb_info.protocol = e_skb->protocol; + skb_info.csum = e_skb->csum; + skb_info.transport_header = e_skb->transport_header; + skb_info.network_header = e_skb->network_header; + skb_info.mac_header = e_skb->mac_header; + + struct sk_buff *skb = bpf_restore_skb_rcv_queue(head, sk, &skb_info); + if (!skb) + return -1; + + bpf_skb_release(skb); + return 0; +} + +static long handle_restore_event(struct bpf_dynptr *dynptr, void *context) +{ + struct prog_args *arg_context = (struct prog_args *)context; + int err = 0; + + struct task_struct *task = bpf_task_from_vpid(arg_context->pid); + if (!task) { + err = 1; + goto error; + } + + struct sock *sk = bpf_sock_from_task_fd(task, arg_context->fd); + if (!sk) { + err = 1; + goto error_sock; + } + + struct udp_sock *up = bpf_udp_sock_from_sock(sk); + + struct sk_buff_head *reader_queue = bpf_reader_queue_from_udp_sock(up); + struct sk_buff_head *receive_queue = bpf_receive_queue_from_sock(sk); + + struct event_hdr *e_hdr = bpf_dynptr_data(dynptr, 0, sizeof(struct event_hdr)); + if (!e_hdr) { + err = 1; + goto error_dynptr; + } + + if (e_hdr->type == EVENT_TYPE_SKB) { + struct event_skb *e_skb = bpf_dynptr_data(dynptr, 0, sizeof(struct event_skb)); + if (!e_skb) { + err = 1; + goto error_dynptr; + } + + if (e_hdr->subtype == EVENT_SUBTYPE_RECEIVE_QUEUE) + handle_restore_skb(e_skb, receive_queue, sk); + else if (e_hdr->subtype == EVENT_SUBTYPE_READER_QUEUE) + handle_restore_skb(e_skb, reader_queue, sk); + } else if (e_hdr->type == EVENT_TYPE_SKB_DATA) { + struct event_skb_data *e_skb_data = bpf_dynptr_data(dynptr, 0, sizeof(struct event_skb_data)); + if (!e_skb_data) { + err = 1; + goto error_dynptr; + } + + if (e_hdr->subtype == EVENT_SUBTYPE_RECEIVE_QUEUE) + handle_restore_skb_data(e_skb_data, receive_queue); + else if (e_hdr->subtype == EVENT_SUBTYPE_READER_QUEUE) + handle_restore_skb_data(e_skb_data, reader_queue); + } + +error_dynptr: + bpf_sock_release(sk); +error_sock: + bpf_task_release(task); +error: + return err; +} + +SEC("crib") +int restore_socket_queue(struct prog_args *arg) +{ + struct prog_args arg_context = { + .fd = arg->fd, + .pid = arg->pid + }; + + bpf_user_ringbuf_drain(&urb, handle_restore_event, &arg_context, 0); + return 0; +} diff --git a/tools/testing/selftests/crib/test_restore_udp_socket.c b/tools/testing/selftests/crib/test_restore_udp_socket.c new file mode 100644 index 000000000000..f986ff4dfc49 --- /dev/null +++ b/tools/testing/selftests/crib/test_restore_udp_socket.c @@ -0,0 +1,333 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#include "test_restore_udp_socket.h" +#include "test_restore_udp_socket.bpf.skel.h" + +static int sockfd_checkpoint; +static int sockfd_restore; +static int sockfd_client; +static int sockfd_server; + +static int dump_socket_queue_fd; +static int restore_socket_queue_fd; + +static struct ring_buffer *rb; +static struct user_ring_buffer *urb; + +char buffer_send1[1000], buffer_send2[1000]; +char buffer_recv1[1000], buffer_recv2[1000]; + +static int last_skb_num = -1; +static int last_skb_transport_header; + +static int handle_dump_end_event(void) +{ + struct prog_args arg_restore = { + .pid = getpid(), + .fd = sockfd_restore + }; + + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .ctx_in = &arg_restore, + .ctx_size_in = sizeof(arg_restore), + ); + + int err = bpf_prog_test_run_opts(restore_socket_queue_fd, &opts); + return err; +} + +static int handle_dump_skb_data_event(struct event_skb_data *e_skb_data) +{ + if (e_skb_data->hdr.subtype == EVENT_SUBTYPE_WRITE_QUEUE) { + if (last_skb_num != e_skb_data->skb_num) { + send(sockfd_restore, e_skb_data->buf + last_skb_transport_header + 8, + e_skb_data->chunk_length - last_skb_transport_header - 8, 0); + last_skb_num = e_skb_data->skb_num; + } else { + send(sockfd_restore, e_skb_data->buf, e_skb_data->chunk_length, 0); + } + } else { + struct event_skb_data *e_restore_skb_data = (struct event_skb_data *)user_ring_buffer__reserve(urb, sizeof(struct event_skb_data)); + if (!e_restore_skb_data) { + printf("user_ring_buffer__reserve error\n"); + return -2; + } + + e_restore_skb_data->hdr.type = EVENT_TYPE_SKB_DATA; + e_restore_skb_data->hdr.subtype = e_skb_data->hdr.subtype; + e_restore_skb_data->skb_num = e_skb_data->skb_num; + e_restore_skb_data->chunk_length = e_skb_data->chunk_length; + e_restore_skb_data->offset = e_skb_data->offset; + memcpy(e_restore_skb_data->buf, e_skb_data->buf, e_skb_data->chunk_length); + + user_ring_buffer__submit(urb, e_restore_skb_data); + } + return 0; +} + +static int handle_dump_skb_event(struct event_skb *e_skb) +{ + if (e_skb->hdr.subtype == EVENT_SUBTYPE_WRITE_QUEUE) { + last_skb_transport_header = e_skb->transport_header; + return 0; + } + + struct event_skb *e_restore_skb = (struct event_skb *)user_ring_buffer__reserve(urb, sizeof(struct event_skb)); + if (!e_restore_skb) { + printf("user_ring_buffer__reserve error\n"); + return -2; + } + + e_restore_skb->hdr.type = EVENT_TYPE_SKB; + e_restore_skb->hdr.subtype = e_skb->hdr.subtype; + e_restore_skb->skb_num = e_skb->skb_num; + e_restore_skb->len = e_skb->len; + e_restore_skb->headerlen = e_skb->headerlen; + e_restore_skb->size = e_skb->size; + e_restore_skb->tstamp = e_skb->tstamp; + e_restore_skb->dev_scratch = e_skb->dev_scratch; + e_restore_skb->protocol = e_skb->protocol; + e_restore_skb->csum = e_skb->csum; + e_restore_skb->transport_header = e_skb->transport_header; + e_restore_skb->network_header = e_skb->network_header; + e_restore_skb->mac_header = e_skb->mac_header; + + user_ring_buffer__submit(urb, e_restore_skb); + return 0; +} + +static int handle_event(void *ctx, void *data, size_t data_sz) +{ + const struct event_hdr *e_hdr = data; + int err = 0; + + switch (e_hdr->type) { + case EVENT_TYPE_SKB: + handle_dump_skb_event((struct event_skb *)data); + break; + case EVENT_TYPE_SKB_DATA: + handle_dump_skb_data_event((struct event_skb_data *)data); + break; + case EVENT_TYPE_END: + handle_dump_end_event(); + break; + default: + err = -1; + printf("Unknown event type!\n"); + break; + } + return err; +} + +static int check_restore_data_correctness(void) +{ + const int disable = 0; + if (setsockopt(sockfd_restore, IPPROTO_UDP, UDP_CORK, &disable, sizeof(disable))) + return -1; + + char buffer1[1000], buffer2[2000]; + memset(buffer1, 0, sizeof(buffer1)); + memset(buffer2, 0, sizeof(buffer2)); + + struct sockaddr_in src_addr, client_src_addr; + socklen_t sockaddr_len = sizeof(struct sockaddr_in); + memset(&src_addr, 0, sizeof(struct sockaddr_in)); + memset(&client_src_addr, 0, sizeof(struct sockaddr_in)); + + if (getsockname(sockfd_client, (struct sockaddr *)&client_src_addr, &sockaddr_len)) + return -1; + + if (recvfrom(sockfd_restore, buffer1, sizeof(buffer1), 0, (struct sockaddr *)&src_addr, &sockaddr_len) <= 0) + return -1; + + if (memcmp(buffer1, buffer_recv1, sizeof(buffer_recv1)) != 0) + return -1; + + if (src_addr.sin_addr.s_addr != htonl(INADDR_LOOPBACK) || src_addr.sin_port != client_src_addr.sin_port) + return -1; + + if (recvfrom(sockfd_restore, buffer1, sizeof(buffer1), 0, (struct sockaddr *)&src_addr, &sockaddr_len) <= 0) + return -1; + + if (memcmp(buffer1, buffer_recv2, sizeof(buffer_recv2)) != 0) + return -1; + + if (src_addr.sin_addr.s_addr != htonl(INADDR_LOOPBACK) || src_addr.sin_port != client_src_addr.sin_port) + return -1; + + if (recvfrom(sockfd_server, buffer2, sizeof(buffer2), 0, (struct sockaddr *)&src_addr, &sockaddr_len) <= 0) + return -1; + + if (memcmp(buffer2, buffer_send1, sizeof(buffer_send1)) != 0) + return -1; + + if (memcmp(buffer2 + sizeof(buffer_send1), buffer_send2, sizeof(buffer_send2)) != 0) + return -1; + + return 0; +} + +static int check_restore_socket(void) +{ + /* + * Check that the restore socket can continue to work properly + * (the restore process did not damage the socket) + */ + char buffer[1000]; + memset(buffer, 0, sizeof(buffer)); + + struct sockaddr_in src_addr, restore_src_addr; + socklen_t sockaddr_len = sizeof(struct sockaddr_in); + memset(&src_addr, 0, sizeof(struct sockaddr_in)); + memset(&restore_src_addr, 0, sizeof(struct sockaddr_in)); + + if (getsockname(sockfd_restore, (struct sockaddr *)&restore_src_addr, &sockaddr_len)) + return -1; + + if (connect(sockfd_server, (struct sockaddr *)&restore_src_addr, sizeof(struct sockaddr_in)) < 0) + return -1; + + if (send(sockfd_restore, buffer_send1, sizeof(buffer_send1), 0) <= 0) + return -1; + + if (send(sockfd_server, buffer_send2, sizeof(buffer_send2), 0) <= 0) + return -1; + + if (recvfrom(sockfd_server, buffer, sizeof(buffer), 0, (struct sockaddr *)&src_addr, &sockaddr_len) <= 0) + return -1; + + if (memcmp(buffer, buffer_send1, sizeof(buffer_send1)) != 0) + return -1; + + if (recvfrom(sockfd_restore, buffer, sizeof(buffer), 0, (struct sockaddr *)&src_addr, &sockaddr_len) <= 0) + return -1; + + if (memcmp(buffer, buffer_send2, sizeof(buffer_send2)) != 0) + return -1; + + if (src_addr.sin_addr.s_addr != htonl(INADDR_LOOPBACK) || src_addr.sin_port != htons(6003)) + return -1; + + return 0; +} + +TEST(restore_udp_socket) +{ + sockfd_checkpoint = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); + ASSERT_GT(sockfd_checkpoint, 0); + + sockfd_restore = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); + ASSERT_GT(sockfd_restore, 0); + + sockfd_client = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); + ASSERT_GT(sockfd_client, 0); + + sockfd_server = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP); + ASSERT_GT(sockfd_server, 0); + + struct sockaddr_in checkpoint_src_addr = { + .sin_family = AF_INET, + .sin_addr.s_addr = htonl(INADDR_ANY), + .sin_port = htons(6001) + }; + + struct sockaddr_in checkpoint_dst_addr = { + .sin_family = AF_INET, + .sin_addr.s_addr = htonl(INADDR_LOOPBACK), + .sin_port = htons(6002) + }; + + struct sockaddr_in restore_dst_addr = { + .sin_family = AF_INET, + .sin_addr.s_addr = htonl(INADDR_LOOPBACK), + .sin_port = htons(6003) + }; + + const int enable = 1; + ASSERT_EQ(setsockopt(sockfd_checkpoint, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)), 0); + ASSERT_EQ(setsockopt(sockfd_server, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)), 0); + + ASSERT_EQ(setsockopt(sockfd_checkpoint, IPPROTO_UDP, UDP_CORK, &enable, sizeof(enable)), 0); + ASSERT_EQ(setsockopt(sockfd_restore, IPPROTO_UDP, UDP_CORK, &enable, sizeof(enable)), 0); + + ASSERT_EQ(bind(sockfd_checkpoint, (struct sockaddr *)&checkpoint_src_addr, sizeof(struct sockaddr_in)), 0); + ASSERT_EQ(bind(sockfd_server, (struct sockaddr *)&restore_dst_addr, sizeof(struct sockaddr_in)), 0); + + memset(buffer_send1, 'a', 1000); + memset(buffer_send2, 'b', 1000); + memset(buffer_recv1, 'c', 1000); + memset(buffer_recv2, 'd', 1000); + + ASSERT_EQ(connect(sockfd_client, (struct sockaddr *)&checkpoint_src_addr, sizeof(struct sockaddr_in)), 0); + ASSERT_EQ(send(sockfd_client, buffer_recv1, sizeof(buffer_recv1), 0), sizeof(buffer_recv1)); + ASSERT_EQ(send(sockfd_client, buffer_recv2, sizeof(buffer_recv2), 0), sizeof(buffer_recv2)); + + ASSERT_EQ(connect(sockfd_checkpoint, (struct sockaddr *)&checkpoint_dst_addr, sizeof(struct sockaddr_in)), 0); + ASSERT_EQ(connect(sockfd_restore, (struct sockaddr *)&restore_dst_addr, sizeof(struct sockaddr_in)), 0); + + ASSERT_EQ(send(sockfd_checkpoint, buffer_send1, sizeof(buffer_send1), 0), sizeof(buffer_send1)); + ASSERT_EQ(send(sockfd_checkpoint, buffer_send2, sizeof(buffer_send2), 0), sizeof(buffer_send2)); + + struct prog_args arg_checkpoint = { + .pid = getpid(), + .fd = sockfd_checkpoint + }; + + DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, + .ctx_in = &arg_checkpoint, + .ctx_size_in = sizeof(arg_checkpoint), + ); + + struct test_restore_udp_socket_bpf *skel = test_restore_udp_socket_bpf__open_and_load(); + dump_socket_queue_fd = bpf_program__fd(skel->progs.dump_socket_queue); + restore_socket_queue_fd = bpf_program__fd(skel->progs.restore_socket_queue); + + rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), handle_event, NULL, NULL); + ASSERT_NE(rb, NULL); + + urb = user_ring_buffer__new(bpf_map__fd(skel->maps.urb), NULL); + ASSERT_NE(urb, NULL); + + ASSERT_EQ(bpf_prog_test_run_opts(dump_socket_queue_fd, &opts), 0); + + ASSERT_GT(ring_buffer__poll(rb, 100), 0); + + ASSERT_EQ(check_restore_data_correctness(), 0); + ASSERT_EQ(check_restore_socket(), 0); + + ASSERT_EQ(close(sockfd_checkpoint), 0); + ASSERT_EQ(close(sockfd_restore), 0); + ASSERT_EQ(close(sockfd_client), 0); + ASSERT_EQ(close(sockfd_server), 0); + ring_buffer__free(rb); + user_ring_buffer__free(urb); + test_restore_udp_socket_bpf__destroy(skel); +} + +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/crib/test_restore_udp_socket.h b/tools/testing/selftests/crib/test_restore_udp_socket.h new file mode 100644 index 000000000000..0ea5d3cb1b81 --- /dev/null +++ b/tools/testing/selftests/crib/test_restore_udp_socket.h @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Author: + * Juntong Deng + */ + +#ifndef __TEST_RESTORE_UDP_SOCKET_H +#define __TEST_RESTORE_UDP_SOCKET_H + +#define EVENT_TYPE_SKB 0 +#define EVENT_TYPE_SKB_DATA 1 +#define EVENT_TYPE_END 2 + +#define EVENT_SUBTYPE_RECEIVE_QUEUE 0 +#define EVENT_SUBTYPE_WRITE_QUEUE 1 +#define EVENT_SUBTYPE_READER_QUEUE 2 + +struct prog_args { + int pid; + int fd; +}; + +struct event_hdr { + int type; + int subtype; +}; + +struct event_skb { + struct event_hdr hdr; + int skb_num; + int headerlen; + int len; + int size; + int tstamp; + int dev_scratch; + int protocol; + int csum; + int transport_header; + int network_header; + int mac_header; +}; + +struct event_skb_data { + struct event_hdr hdr; + int skb_num; + int chunk_length; + int offset; + char buf[500]; +}; + +#endif /* __TEST_RESTORE_UDP_SOCKET_H */