From patchwork Thu Jul 11 11:19:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730515 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011021.outbound.protection.outlook.com [52.103.32.21]) (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 97DBA15B11D; Thu, 11 Jul 2024 11:20:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.21 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696827; cv=fail; b=ImYWM+I/nxa0kn79SA5OWZ/d+GWq6vcd2amHWsTf39pHZl6lYOFFXGT8aC9vgWVu4KgdmHp5zzjjVrGPZKxZPZy+WFgtaIkHdRsJRSM/qgCijExrZMD6DRbJwX++lY/cuHu4kVc73lRiu9RutqQh5zOJpENg/7LKuqirK+m0vI0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696827; c=relaxed/simple; bh=SWCAw0vxxBbwypSNpxzrPoVaZe25u4XvQL4Z9MsWc4c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=rY5HV8YxZ2l5KkxwqRpx3Tt8G47QjosG8m9b08kUsFKR1MZDHAQHdCyyTlt/6AdUqG3shYMaGnWHetfVUu8ipXr7xeeHF1BbG9ADiWxS5ZC2QLQ0rh6L4VycTRzApjcLZ0+XoSaeBlKGMqjqE/zrKIWeoUCEeWz0030CQRCV4N8= 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=W8A4zskK; arc=fail smtp.client-ip=52.103.32.21 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="W8A4zskK" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ySLnQ6nfAwVd0Cv+/pRa6ukP8vwX7WyevSrR8W9nIzFFXV9dzG577fCnQZIezM1uDidjkh5MDqPyxSpDHkxpMI/sFB3qIO2xxwrxx2cuSy5z7nB+hjtwrEO1p09z+O3guBLUPtIzOM9WNiap+p6N2FHO4R1SirAyS/L7BeWrfRATkWV+nr6xrpKoMc3iLgHMEoekhX0qH48JLhaGls86y7s/K8E7e84j8ff0kPWl1Bn4NVzEeo+MV4ZmS6+SQWBeX+rWDtxTq4Slxp1NVUC7jGsf3WH8h/b1wXUR+o93FiQgDAtv4KftsorfKeXPmVk+XgdYeZYJRznP4ITrchdYww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=s2N0HZzHp9b3xCrqnjQQC1eqtdaho8nApZdrBMpv8fDRXwVnsdcBFoIQU6S3G8sFDGEX/2LLnjnhAfuCL5NhfZfIopIFwgtyCYMP/EGvBYkWRt3WvgGlW2zgNz216GDJxpV0SGCu0B+b7nmVo+JdFPdo6iVXp/YcULIebBwg0iIF2hIQs803n2uMNRXQUPUb8k4ccHZHncK3qNJ/UPGHaYaqErLOdRKl3BUiipWBS32JStochDpTzwi+lxfuo+BqE/udCKORFaVus3VCqCmTX4Pxcwp1HTOX1uPMvx+3aqplsWeIs20OHL+BxTdnwAUOULPebqabsQxrY0PkFI80/A== 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=W8A4zskKZeFmVrHoIlGPHY7dK4mb7XzUec0VEwlVk0pwt87/3c+tv1DZp7xfMuT3qScnTs0yKeRb3Dx5ci08F/0uShuiTwk8t91niOOSuKHB1EenIHQkIFL5I+KT6erQ8AV6WcjTVq69gHixAHldvqhgFxAvuXwx/gr/XFqCWQzG8SEHaFS/+1c/wirrKmv3CSUwETYHxM+bFea5RPJOQnh62RHkE+6fTCUFb3FuyigNeJ16eCCPoja0qwCRPKKjAGfeTsm8VvvV9KpBp5Y7Xa+Xfs0OyIeTvNHgkVkzaYfdcUJlJHxn5GdbvyeIcJyK4hTRceNSyQ9bHUKyKOhvGA== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AM7PR03MB6628.eurprd03.prod.outlook.com (2603:10a6:20b:1bf::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7762.22; Thu, 11 Jul 2024 11:20:21 +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.7762.020; Thu, 11 Jul 2024 11:20:21 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 01/16] bpf: Introduce BPF_PROG_TYPE_CRIB Date: Thu, 11 Jul 2024 12:19:23 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [fDhjfl4XQ7kxjkOZWf/LUz85FXmek1tg] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-1-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_|AM7PR03MB6628:EE_ X-MS-Office365-Filtering-Correlation-Id: 89691632-7daa-4565-cf05-08dca19b739e X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|8060799006|19110799003|3412199025|440099028; X-Microsoft-Antispam-Message-Info: LlG0ohRQj/HVX6bAYRB4Z6LK/58BQPAGn4U3cBOrFK9LAsG9XvOvPJiz8e0OyG4DftFijCvzq4wwIxyreG49vYw5V7aoAcxy0BqrZeaHu+ZG28VuwNoONTyff1dSGbwQjzitgMf7+744nDJBfkvFd7Eh1/iL5k4qRaIXmeJUgLi+/y9hACNSoWOrMC1QJY5vSF3rq9LihsHGGOVf1BFUffg2I7wDnAMzzMbqEfxaJ5IvFIYsTGLEQA38gjNFJROF9j1cFfGRAfLwOTySwJOiJH4e5wUasqpm/gabWks7bO/qkibBkBfzyVfV/jRQZFIFZv8hI+2VjM9eHBgXbFEEic7ZPwjNDsh/ARonBo/I/0L2/+jWnHA79kPABCYz0/DUQ2T/DZTpcU7uJGJUOlWb7LACLNF7rYq6L/4QNO465u8cAI5uJ26GUGx4XBUZ5jjc24rriLOB6cxlVXqaUohhZd9p9evT1tBRy7j1L3M4fbHqkp7O0y/Ml6CEbyHG9tDEST6ZwxF8N78zOPuH8MaY3ZDfNDo/BxlCOKiDqM9bUn7IRflN6JZ2k5s9eirpvFXak7NWRYCLeXHD6JHOdZk28AK5Sn7DLQ6B2xTW/PZw5sjCsO2JydtaLWHtrRc+qtZ/X3RkqrloG1ls88lKgPqCGQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 79FEAK6SBwr0f10qaSgTwFqAbAq8r4EVIJyNqZRRhZmESq4wAFpIzOpUN8/O1rCpP/pw+w21j7yDw55klUaMZjVhFxRKhLdIWmpdnd49YFhYWlv5P4tyQk73hizIWrtC8hKD42srwK3MBrYsTj4Mx2SgaWKX2fgP8RhdP8pQA+b2WrYgPRTJCcFRSiJf9tmDCMExLcNtGBEh6BoZXsTuRsSv7bothYga+ftfvQ47kWIRHKJHmbgYgfTtYE7XNhAxM6rutlSdSgdoTz2yskevoZtXaTqt7OFlTkNEuIGgP9/AoHXmB/4Z5XR6Q4x+UgBFAN5gRMTQ36gHiATIo2i1ps31UoQd6I5UYRSG+VvumfUxr7Qjej4bmZM3UNHSeFX79u5qBvP56jcgdIOcScKSromjRA4FKYmIyNtviplXuLkXGzs9c7QTQd6giCaWj3szv5Nnts1zTWrKEHOEt1HIiMkLzkY9NTlVF0RmdYEL3xpWtjlJZF+YjBxiqz2kggNedZyTABqLDlwCiZgMN6c50nFUFanc5N0v7oF893ckztsT7dmT/q7rkLwcusKo5Grj1yxuOdMwvlys07k+gHLSj7KjgCVcB6ihOrrEKY1JosZ44B2tBDnMo2A4WzYcs3fFHNqMT+ceWHI2zI03AOl5ZNONODdZUYZ1ZrF7B7WGbv3A02rNXlo7rb2rL01x2Ai/dQcmK3umEhL5rug1vs6emizhLhixM/C464F3dG9OJ1VHdDZljkGhbeseodocfPUleYht3NgJv0wVlSzxZuItjTRHFrNOE1t9rQRD+9bNwEgSThQvdGF7ksKKmPpyUW9XNsG26eG3cuBZFHCFlcGp8B4XYOA6hbxghKBmcXiIZQMdBou9fAd9REGty0Y18GyFodkxtcHJPJpU1HGT21dXZ6QYp3jjEZeN8EaDCGE5IN/a+0Bg0U5yA5TGqFjOU2ass9dL3+ItyiOWL9c39DzBWklAv5J4JB5WOEuYy1YyGtSB3lB2yeu1JmN7HpksS3L9DkBd7Jh4FEbY36lmP337WsbIQCvrUPN2GBWIEpLnlm5LedWHR8HlObEcSnZ0W97m4Z0oLX5i8UjQazE6f6s1gf5+2bzrKieb1MMOQDs7O4r55zm6XGEP54NxpDfWLGqHDx7ah2fcLOfjxWju2Sce2jsy1crqYk0ewunNoy75HNTvDWrRbUmncJYyRX/tlg1IjilykolrhBlmu9t3GHJ+kl3Be8k7F1w551SPCSqtn5I= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 89691632-7daa-4565-cf05-08dca19b739e X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:20:21.7538 (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: AM7PR03MB6628 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 Thu Jul 11 11:19:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730516 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011078.outbound.protection.outlook.com [52.103.32.78]) (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 E98E1158DC8; Thu, 11 Jul 2024 11:21:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.78 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696882; cv=fail; b=bAHXs3WaY31NYCZuNa0UjHTTLrzgJipBfPl+bnEZAJzm26OMDRVh/X5zQgMQQtozV2qBQfNXRpOo3Fmg030G523HtvFRciIh7rsXSseKkrjhgLjvfGvjxnLKDmZHgiPaUGOCZUiTST4CF5SmMRrwOmdOOH3xDClilNWfgAs77FE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696882; c=relaxed/simple; bh=2VzsNC8RjhBFsaoETzi1HALEC+WpHCOX/RqlEcxIgQQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=I8J8v/VqM2G4ruDVms0oOmmOH1mRmYof4PV23xBjurtk+ii7QCHluMpIubC8lqJK9YUYSYrDagPNFQ60jNQJ5LhqqD6JOLhp6Z8Nbqce5fcw2QbGQ2uzkVtH9Ly/ukBtlGujJMPEz/5S850+037ZXHBXaYWsv2tdgX6zdSUdFAs= 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=C+hXiCQD; arc=fail smtp.client-ip=52.103.32.78 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="C+hXiCQD" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pVs4z9tpDxwjuBCkEh0ATTiQcRGAurMZKR6s0okrkIFsxSoZ8nsJsLMryf7Rhma8CCd8aL12oUrqeC1Ww8JI0JFlK6kDj/8vinzVJffdyL3v0G4wJli20ojk6cQn2KTRM9EBSvgP4DhtmF9juRLeo+OGkxmpXxkDVDYFu7r7vnyD37OHD+US+umI8Zqpyuy1Nz4bf0z6Y415NMKnViQSHGWYi2CGS2dkOAk//PvzRbo4jSvuH2cEMhiE8vq9+fXj1+Bl7AIhHzi1HkPt90h8f6bzCcjaLnKiHNfoKeQMKXIz/9+4X5xGGLyNCBrWG+n10e8QpJ/CenK48Yhvi6+OaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=zJqpIMee04bcjABPwsgAdTBN8fU22HW/GvJZT1u+FPlTeCBFit3LV2Cb4rJW9LsrDikSevlHSvQb644q3Q8EG3u8BJwAkdc+Yj3ZLiEi+RJ3t+wgDa8CI6qCT+eeMjqFIbira53wDchaG3++iptWg8Ez6bjNjt8GVsG7I03x3mqPhpzAOdfNFvzwhtIpAkKCzKrFd+DhDFj2r7JGiyAFblJutBX5j0aoZLU6nMRK9k7QCkBwbr0bzjJAuEQzX1geMNo37lh132e1+M0LGmWGO+FwcoVwm7YQ8dOapd3CJUYSW1lgAwohUVbGLwxZ2M0LI3fS0e7vqXKsnVQHKvJ0jw== 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=C+hXiCQDvl6xWYOzg4zvUWWATvH5FhRsf/jWffp+uGFnKCjEuzzYtOXFFQh5CFbw7fpnSG2fxutMcZYL9BGgceHX/gYMtp4NAQ5fx1/WwLq/P8IggWWU+wvF9reyS9dKaxxzneQEC9VR6v4nK/ylZtTZP6BFe/pxNktPU9IRyHgXuZpEuDhlfS0AeG9F/1UBuy2fYrnNnfcbc15urE6aHYNkafYe40NWdJWGjZNIoJ+7s/wiSdR9NSDQyxJQSnXxG83IFiuZ1X7Y2VcbRw2O1QAqQIeS/gEJnux5pEDtnfSs+UtPiCzP/bJ8YDfTQkAr3TEPEobHK3iXvdpJhOb6rQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:21: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.7762.020; Thu, 11 Jul 2024 11:21: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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 02/16] bpf: Add KF_ITER_GETTER and KF_ITER_SETTER flags Date: Thu, 11 Jul 2024 12:19:24 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [HAWyuuwcuLmLvH+O5r3YDIc9jLIvz6Lq] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 9e45a542-d889-4cee-f4cf-08dca19b94cd X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: Z+M9QJdfM3Kl2ygrEklSkFSLzIljxZetfWxA/rYZe8/zz2FZooQbZji0GI4tzBek9hLDx6yEMe0iKvMY+gwunQcwh2Wxkcutef9vazFnbj5MPvjjGy5E/MgvdxR8p6XlCXvxthl1OAbGJeJXIcLByP2s9qmM6/KlRBx+wbG8/wBcrTqncQmpEXHhOmKq4SSvfdBLqOgtJRddVX3ayIVRRaMSCGm/MxXcnd0aTecrFdp7B6SozRKHqEV6QVm6NuEfu+/4JOW9jh25927vTz9FO+ElO0els2EMwi1VhW4ZYxjdVuUqt0Ax+eaYrfFtX+7ZIb3eqCoaT3hTz7seSnfK/rbCusmyZiX7xwcBdOM0dHHxmcHdsjyabUeoE6lZ4+eK3OiVQ39wqfI5ADHNN3bHPAnVDJMXXMSjPnzI3/YGQc1AlV+zi8dHcZnCLFCfmpfoagYfbFkVjYLmeYLe/hn87y5reY0PO/kRqOeTWF4z7C92NxqcrksUIgQyAlWmWCJuk4ykMHQ4551VszFnGYAliqDdgZiHeT7SwqNuJo3oKBVLxjzMFSimHtEmiAaghdGlY4OYKZmHVrPNrC9bSviQXNolh6ZoZEjZbLLRLqIC1OOUiYIn5uTif00wSaI+0oFEEYn+sE12BnVhhA6PTZd4Jg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: bv0FbxNySRJrcIMvgT9XUHel3jl3ctqFAuq7DaP8bQBRuzLMOmd+t0QaHYf4xrFkIcz5qpHUwm/lClDwbovipAKWS1Lvk4a2DoPYov+aSp2c1KIRfWWwQNCF2ObvZ+DxpssIn9rhrhZXN6jP5XFuYZ/ai94FZT6q2kGG7yn2SaN7/u+VBQLjh1p5O2KaS1mesW/Afa/L8yyRcmCRE9QzM7jQYZY7BAMiM8KSYd/eqsXZiwKLFpY8UgqdKNAJCgx/D9MrJErXsSnq2hovbBMnJd6hscinBEbywYv2HZd0Qft6YSMBmKLBnjPH+m3tzNgK2ubLQg0rEvqO6FuB+XR8vj2PnFqsZ0ZI8ditStC8P80sDj7mFiRzhB2wEHr9MeJD0rndSDOxMaDoQhUk047fPPdvpyb1veOJhzx1NXEFmNDaqj3CGF5dCwAVs/Bboq0SD1Cihod+FBGehUYu9heAS8VGT0atCmj7M8TtUFh4v6BHnFi9nxd+qU7Hi9tYnSVEBVcwFgTeDefWvjWjaUokgHHvNY5Hj9VlJ/AUcvFTfhPTXFU4EnyPdYkGJUvH8rcNw0RkZCa5Z5DMVNa4I/SAuwSoGTQefxs/UEEQyMJxCTRIL+Tv1m+MLEjIRkPJJxFTOkDotwCjg6EAThTaMv+Wc1jKx1GvP3ShxO70wgHJ9iBzPbrswJo38yBr7hHfndcJpaquk/TukbC1fJjCshILVRtpaNQnVmF4LrAGjtLtthBgRTcl4NJYAUpXocD2wOliKBXVFTzGhl3LD0M0x/GSpkHqSHRqlOlSwdvp+Be8ji/TbIlNPAQg3UjPE8gcFrKOLaa0b9chyZtZ6ray/OegGLL19NnFzCxBP9WCSzQwYMDREOsgUjuVwSCO08eO4DR7yzEu3s2NsnYo8jALVnM914GuaGHKQ8i3+sHd+Qiq9ZwjWCjZVEnrIWCuv5qPanw/r/U+aDI7JKPCOJWmSpwpRUO1tQB3lFgxHWI0otX8RljrlgcWbXOY1LrcAkMmB8Zs6rzcbmGT0hlwT3GBX4uZR+ytfZ4hXRGbrm64lRxbxxQI9jsydasZbGvUxohpiuX/Zb1GIYqpyBE/9d/mrioMSgMWFmACh2n06Odv7W1Zf3Ctt2wglcEWKx5kdCKvLYnqL8HpllD50NofmqlpBQlg4dWUsMBF+sdB1L5CRW6lUOcGh3iml7O637gKq9ynQ2G0dROueICyuaL+1fvF3hg1BowupWjVn9TctxFKsxduwq8= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9e45a542-d889-4cee-f4cf-08dca19b94cd X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:21:17.0958 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730517 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AS8PR04CU009.outbound.protection.outlook.com (mail-westeuropeazolkn19011025.outbound.protection.outlook.com [52.103.33.25]) (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 7D667167DA4; Thu, 11 Jul 2024 11:21:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.25 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696891; cv=fail; b=j/n056yJcM17VTe+MXDGX/BwbwrZdz9TM0uOAoVfLdM4GUnKnBCk3eMXjZLchzUJXrl+Nnz+5ji4yUTiyT7Mw3Axl9VlSVZyWkbWRvCTQ2MO1d1sxf8SStcsoVtfKx1Wkl0F0nfNlOPiW7vGzQ6h7maI6vskIbgc/TVUyRTLpoE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696891; c=relaxed/simple; bh=68ZeJqTLuBJYkzmaoh0YN9JiKhSbyeUT2TEtHG/vdUM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=iOoyKfbgSaOIP7htHYuTSDwHB+XTwCwQK10GxhnGWO8Igia7G6bl1u8G0S53/vQpTxf9DkX5sMgVqzsP7S147Htm4rE2B5bCmEUHd4nLY2H1FMaIWe0kocYX7PgQAqiPIrmadT0g66tB7flcMtbWQlG8taU7H7+jza8p2D+IhTs= 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=KwP7v/T5; arc=fail smtp.client-ip=52.103.33.25 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="KwP7v/T5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gMxFkqRV7K1XiTwKX2IckRbNcqxUOdT6SUI9S2IsF8CTE2sYf3D4q5/4/BHN64qcF82I2K10D3he1W7Jw+0DLKwT3JQjxyj7TQjXIq56JbzwU/iNhSUNrYFhldHYdNgDIHvGm+TGSpWTX/OQWpifl5eTo7sjZl78/caNpMDchdPr6L+LH6+hKuq9MsBoopkUby60f2PP6WZZA9GA96e/g+OztHC1sMC8AOgt4TzHeLwCpTLX4wQsli2wjLzQC1cUreoAyqOT1CItEEuzDpYXjPVe1xIR7ric8yU/vgH+95uKnShC8Oo0oKAaKviTmqUZfclLlCz/rsthjdjkTPbIxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=dt9fM41LSuDpFVcHDNBbCTbLnHnYQ6nODr5FChR8Q/NeoBcFlR9EEEPIlNm3RikmftPcN8Tj4g+Q1Jz8wU+Xw2giybCXjK/qF+AcjuZaSb8WuA4SteWy0sjZ3a2hrrmPu6WKBIFAa/VbK1c23CV7rZx6Nmy3BqW/3EqbO1v3yeGofb2q8ssQPFWfIqjDXwpwdwjYvA/pkCqJJjlvDVbSVxas4gGSiwp3KCQtspS0y/rHGOhxDrrCUkKDB+8NjdmFEs54EKA6fb7HiK0WxQJS/ncZFFq6cntVMym6Y6x7ld+xy5pr5M+DcF72qrBL0BrU1azkVfrkxqxVO4egTVTTPQ== 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=KwP7v/T5kGic/2SRGkUQnIl1e3mQrGGp+0omDCacR0TVi3JmdfXruLnUOMDggnwhouHqQXek7sQCwS5GRfGCOakjgO0jxmO9dy87wtWwn6blNtT7JM2YaB5WazkOvFhVgqN4LN2QHZ7i+Q3FKF3zRxNiM0axxZhuiSzpz9XOHGiEy4mko34Y8dVy7u9IqqEFjlSa3Tg8LwQKnB02gcCOVb8tLPY7VUsd9ojBKAfbwRwOdceNqYPDiG3izsImcmWzj6Otw2xw5jOAqkL8oWxL67ELI9MaGmzefXTUtmXkko6aV26RVOpax+nNGIovLzvem2pNMozKqyL86Xg/YUN3Ag== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:21:26 +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.7762.020; Thu, 11 Jul 2024 11:21:26 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 03/16] bpf: Improve bpf kfuncs pointer arguments chain of trust Date: Thu, 11 Jul 2024 12:19:25 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [pRr7CCEUX8+ZW7EgYAMRw0eoCz7JWQUa] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 8cb0f654-729c-46d5-9593-08dca19b9a50 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: pvhBYfPuPoXPutHkeC9qCGOGmUc6bNjP3LgeNejck9rkOuntT1FcwvAtpPA3Q1rEJ0vtq0HGPr8GroDEwqX0QwJIFp40J2LK3nnuXMjnESgclbHCstbqrhjFC2Jaf6LgGYYttFv1zsAalmn6IqHNwUb/oNK2S4ioWCFQ3ZKzWTJ+gVXs/ipazF5sy9FHDF+dmbM0EhXPw3Geornseb85KpHhZmfvU5kIXNXeispY4DM7yPeHzMnxbB4AlhyPFWblFPiTOKxbhwz2lgx8dJhNIiEXFvmizrKef7Dlf5gW+DvV6Pml1+4XVGsUuCDU70w2jhATqroc3IiaQRvdlQjvGtf3cI0zDMEw7oYIVm21eHeRc8BVMNPkBu7+IpxMxkas9Op661JkYD6aDueoX0sSgd7s/4R0L3b66vgbfMlUncTtFmsqZ+X8nzhgCT29Af2ZICGZsfCzJqI40BgkDcuZzTk/+F7pZahufUQxjP840SzLsfDAuGD2FRGtVnpn/SdZQBQQ3l+ZfTkDAsC6Fa60+Vw9mlTbxl3WqpHkYDY6fgdlHKJnF5gaaK0VA5SFzdjA6wI1J53wqWTWAx4HBgb+2GOhr8nzzFLisbqJSk8f87o1PktNQS9tldDQ/Oy30H+F+/ybxXt7pAEIMmSTW6W12g== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: T7nUFniG6IWVqoOTG/g/IeeFQX8CNuZ0ho008wsk5GwzlgFEI7/8giBtO+XGAVzPGH9PJlxi5WybvpeyWPDa4nChgyhgeW3dVNzzt8E1H0rVZ9r5cRFro9EUv3or/dUJfUxnNuarDpdiY5Ss8ioEiB5Fm1A0Ufs6FtNTU7UUk+pPh4yobF+yk81CpMFFH6s+oMQ7/jDGlPeBsn29ssbSvEXbng01tn21O7IvmXtYyKoBgmieOf5C0HAj9dCHe7UYRUlF3BTb3Lq/OHhhX4w9X8GpdH3/fsEkUyOpGLqCY3FKZ5xOWkaezO+ZloRq6E2ZM5JF0lEa0oO+ZeAHBF+xOJViT97gFaa2qaYKDFDnqTCNs2HwkKOr4uFlx5ztARldQOorsy18W+gt4wLUl3MbyuwjSdqtbc8ouPh5S922dcRUOMNr+IOn3tL1Keruwxm2q99pLZ+3djjHIwjoc3+GuZK4NI9tAeSegGOw0QRBn78Ru14cpeLBPpRl4PkQnrEsdLg9MTt0HNsQiEbAvl5C3A4UWj9XvaGmcOQjiNbUyWp+LSJOVLXRgmfpDa5bIwYFAZMddJSxYUH6JnjqjkoGAFLjJr32qOxVPXT86nDkhSydmwZ5YMblfzwG77OdFX883f7D2Ybsb5hgIixmZx3mN852NFboreBgdyIxmTJxJbzgJsTh+MQMWYNgzvspgY+3tYEjxZFDzCK8Vtm+pdSeKyIbe9PtGS3It/JFJc+lwDx+CIVh+Rx7xzd5lsyHrJsqgIIxuePd0l7UYIVnQDr6JkOUSpGjGOIOsAK9giSM+g2IQbvEoxfxEM6vJ/+wiLRjy6tEhjyCFw69sxpUIFpN/9JtV4LIV8T+UwE7muF4QDBNxjl1pB4to0lJ/ZALeTEp+bLM5o9PDsjnJAYP8XrGKQ1U7Vq73I8jZEhf7gWs2f+80e1EfGtrGEsIVG4wrei/fOu5oKp3/TnHM+KfBKsqgJlNspQrTDvHaZQXGCYzkVlTjDrNZV4ET1CBbdLRU+MD/s9Xvk7CZcTiDytJckvg9mkHf3OfQ8BSxYrPdN7Kb/UDW5wDZWbQm7p+79VtEjKSf81JPz7lydV4n7dAH1KSZ2g/I8w/cNP/oh0wb6iRlWO8qrlqyc3Ak1M3lSZbNgNYTktLOWeUsPz2thQr4WvhAUEX9deDTjhWPCSCqgX+Spb/nxHh4CxlN05HrDpznam5WwpsV9X4Rh2RLLKXS/l/vYWNp3JTdx/AFNXcAn1Xu7U= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8cb0f654-729c-46d5-9593-08dca19b9a50 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:21:26.3175 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730518 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010012.outbound.protection.outlook.com [52.103.33.12]) (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 7921416A954; Thu, 11 Jul 2024 11:22:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696932; cv=fail; b=gA+OVlVvOSWJWiE35ihonWmZ/QwrUf42oHifqd+MFz8/hd2VOJ+86LkGWuxNoqosZAAneo1oHYF13l2yB/gz/s/vBSHYTHghezkZNJ227y0w9jbu8raWuSOsEB2p8tqk5Z2G1teOeuTN/WUDNDCN1K9NNpoA/LKG8LivWkZBhaM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696932; c=relaxed/simple; bh=toLCOAeQcWKVCy6tBoaJfr6rl22TOb92yFiiPxWFopU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=rDfclATgGuhkhyzSXV+eKo63DOD7sIZqVOfw5QTWi+f9lPyP633bceVvoyjQTKS6Qm3NXkeb16Uk7NQThiB4fBa9VBZVUYqcyLbi8Y3xWwyWDlqJX1J37bUEQAUJAIqLBvRYUuTl3j7ZGKT+MGHdzjDYogJm9FNML7bRL35dJIw= 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=Oz5646qp; arc=fail smtp.client-ip=52.103.33.12 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="Oz5646qp" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=O7jmNhew/JWpGqPAPJuGvHvc9ihabNDnUb7NBVf9V63QwuRu8NxdN1v84X6cSKwEOT93E3v/qgdz5mA/LQjtUjMeEgDeQs6+1LZviGN27l1i6ZbURy3+8AEbDj7j5uobRB1Xxni4HlglysriWextt5Tfxk1QqWu2Y4CnqxAFtZRjWheLGCpRZpNG08eIhSrd200i3BIoGKhZaeGTsLOEeT2zXPfb4MYeCKvjMEc5ayotnX93WBdS4UxvUbFNf1q/+Gt0qQN/CiY6GrOhetGcQAYKWEkEL4ze1nla98j/zc9FoUEjyVDXazJpjg0bT+BpiZwIuXNL43xFM0cVDpyCvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=pte1/scvLyZ5oP4R7lhH6xFLoqkzXGk21DnLFTosM2ZRewJf7KFs7nmZ/9V/V0wequwRPi8zyBtVgGkPgI3S4UJXhC2jA4YAqdGB0b7FTo96ihRJeuKykgTnWPQp9wfaNumVLOH8k4Mcx/B0gTLAAAypUqkiKKYIKDJCm5NIt5mk27K8+bwllK/qwkmJJBRYv5z3j63NUWTDbNsuACvSQDu3tMBqYa5h7tM7O/GwOlwMcPqj86CIR2j8VIuyb1+jEgsKkrplafI3KcVfhTGkxBCVvJ1TSjDpaFoAOS2tzxShTVCjJHYGReDBw+fREQGAvSPH9WslukX91pqDXeRM6w== 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=Oz5646qp0u3NUYtBYKUzT4SXkERDUw0r6V95hU70h1QJHC780AE8UQbV9xtXA/OK+/HWjNqmtIRE7EgEA14fB1IEFwoFndli1OWs8vzntDdK018nLIr/tcbITTMNfNGBLIZZZN8Y2slkegr4LFRSnKDXcRjYmPXRtoEXuMzA1YtgB5n4lOIiHyat8eu18HffR6wxo223gh+toJBqFSA8GFGEhz0rQrO6YgIquT94Cifkvwj3s5cEgbN/Q9e0tISrHiUpryrbMogEXHUD1Cc/WJ7KQas4QZHLBxTWQEHY4o3xZQQaYMnEEJVDvsfkRoYRVB8mVbcB//Gml+6tvcMs0Q== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:22: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.7762.020; Thu, 11 Jul 2024 11:22: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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 04/16] bpf: Add bpf_task_from_vpid() kfunc Date: Thu, 11 Jul 2024 12:19:26 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [60o1QDuVv4m0Af2k7fDRTJYJ4ZQE8Rhb] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 8452af9e-5a1d-4d20-0ab8-08dca19bb2a7 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: KlfKETdH5h5CyDV8LvPmJZRjcYp2tYgOB6kz8SHCj3D60E1lbeexHG+oQt5HViLGHtXBUD2Pn3/t3OcgDquz7Xgz2u4WAIzZiFDauIwQHt/IVdQWhxhiIrLa/HBIeyectKReJkEIfAou3aO+5+9YkxzmC0OtJ2hui5UvBJgkDqlKA+v0h8k7gYqPe+3VNG8ZOA7wXen2rZva9Ieb9g1+2JdzA4MVczjK17KYCq0lYAxm4qKTni0/jp0axeVZSaEtLQV7VKyB5fvvZnJHHj+9qD1B+nluU+d3PvcwJkn4Mb1m5HO8tA8iM8fV4pXDgqpL+acbesUbBOXZ4Lpf8xrahb/6JOqCr4pYkBdMw6aDNno4aTecQKM/g59ewLr/uFWMklIFOVw6ZvV3DKgPMIsPCfsigDox021zULWN/zRO6nCthFurkk2qS03KTvVQMVzpto4sjSwc4Yamib46XFdMoiPb+dyB/OA7faXxMcVFHHUpyeaSgL16EoQgaPJZQBJI0Pz7oX7eJudZHxpU4MV6qZpq9V0v4hPCQ50pJYkskaUjTQnTNph5aJtBmGYaibUi43M+P+/gHCdQpWNdlbIThlGOlPyCGEW8jhtRiFiCOV2QEGa0rHN4FDzdPkSMeYWzB0mEvvX2QJSuKPdKIPRWIw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lAl+0cwwTIk5JFBrSlKXGkNRzLufKVeCgP18/jC4gbxwXbWDOvouUrqXNYdx6JuutpjVZNUrKyPLSuPR/Puf5QdzvAW2aplaeX2pJk/7zXIrr/Sb2Q1nz87ang7B57fhfqXQR8Am4t1g1FRnFAUGrduisV2+S0i0cdmBcHcHPn19NbsodR/zYU9E3cxrw9GHEWlD6h+nD6hjN3AdQHT6oRlm8wZmvaylo8w8ga0gffMFDqzshLdaSOc7OK8+FTKXoJew6nrNVn8Hd87slQb85MY+VTpC/G6HzlYmzjr+YD39/UngdliQHlfe/5FKfxBVRIcMByFuW2K3PcdOVz0GkK0RPLIKS7fl/Cta9D2vTHv82/08zyMX1xJn3pBUt14iWMVXxHTCZLvl0SIj8nPi1BG97PC9q0aw6t90bKnPnMRXzXo4jbtRSvCkUZISQ3CzCoBPagWNCkuK89/PzRD4fsL94QmgZDNlclTWHnQhqlUBE1qqz+Kgy1NSAgfUpKX3UCsRJmLCXynqBoxkDzfvM9Kfbb4kiLdLgqdrY6cTjkixK7YnoorzD2ai8km5R7XgKqIdd/s6C4OWVDQeeAAHJ+cYPgyY9GaMLl1gRCEQ/n8CbW8/Vje9f8D8eU8jwo0Ptsu5Q7FU6Wyn4rylNBEIGx6HCC1iVYDKPR5yN/NuQEh1OsEnH8+3hhU1OeGiKP8ZAaP0f6izRAKf8YapccIYQ8pk3KtGPUyjLnBnSkUl6e4zynQFiaVxQ5xdhK7LMFaT8OmMbJjSKF20ng69qnF2AouHhq8m6c+XIfzNoIR4xSF0MtykuXjBc+2B0+ZfJZFbGneqx3EfxjxJvcdUfVKMbshJi4THbWoRCFjoapC/JXzFayHsgzArrfyeu38bxC9BEhcI/khVgudIBwwQzXa1puLCTOVxPvtFW8A5uRNklhUkXbEyI+8ZDv1SRne+btpNUUKafMYskPfVBqDB22YlsqSjuo4JSS50CBIPeP11S8nf+zlzNeuzX7UxRO1jMc5b+Xr6ZOLR9fPaLu8J6yKJU5d0YdbCV8Q1u0ICNK88IglA/5FvMfFYHq2VbNQaKk/GlsW44obI+S0aNQa264+n/sV9sY1PbnjUQxaWsETb0ggHFu1gGLUkzli7juGXZ80rSBBHApugkqgApf10AHOihnN4QMyzS0AvGxQjP/GUzL8ZI3xX3UJ0hKZeK+7SFcyXsS1vZCCZM79TR5wbv3H571nxLrHv+p7J3RoqqCzL8zM= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8452af9e-5a1d-4d20-0ab8-08dca19bb2a7 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:22:07.1444 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730519 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010014.outbound.protection.outlook.com [52.103.33.14]) (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 A09ED16B75F; Thu, 11 Jul 2024 11:22:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.14 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696938; cv=fail; b=ZZeC1/bfQ5Zw4UTyaffYB4miIhFZt9fqGTwv6zGln0fmABjHSDhyAsLjr1wBASYvlCeDK3bS6kf6PYdqasi99lKpW0k5rzPZsWLdWCDH9QNFxaRodGi8oSPmxmmx54huXNJMrUsPo+iI3ik9nA7GGlQr/54DWKKYmAtIkDgXcOQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696938; 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=sKatjHUoKX4rAKi4N8LkgLlsSVlHJ3gjbwuKjaZ+KepnNeuP+aAPOQrAibw2TdWRWo8afrXQwOicSxU2Xod8juNn7Z/Vnj3tfxk8qtg5Zd+vlyjqTr93TsgG760qrapOVTNfPvalQ8cflUZzWRD/MKlM6Eo75jDpYwfeANBgfpg= 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=tWtCykOo; arc=fail smtp.client-ip=52.103.33.14 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="tWtCykOo" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JZo2ydwzjpNGJ5160nJ/ik1fVsqPGFt5l4O5D5Y4SI+D2MtWb2NwiWSLwHQZPttECOoV5+t6Gqx4wvE1cunQuoI2c2i0mBX2KYNIHpYbq+f0mqJ8eyqZNX0L0Bqgs94PXF2mwAl7F9XdcfU7dp302LqwQpP6+xD+56RnaP6ZwnbpmuVphr0Kg0eW9zyxleKNA/Bko3kcD4CXUge2mh/nEaqeuswGgCiArM2gTwrf4THwWeaS55Tfgi6i190UdA9v/KOOsWNxUD71bcOBCn5XgDR/VsnnxHa14FHSzk6yEoSeMabSZTwRltz6EWEpcxLNYV8awxq1cREZ4ZJbk6Tyjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=Zs4bzAyAJT6j64cevxxAnp5Ol1NqoFtUYff7t/etZZ1v00HAZUDUOGoRtBqHIhRSzQdKViojn5PXt+SVKHjDUQ4gfw04LEcSm8jDrkeipwTYDbAqcjOd6Ert05p8Mn2BEQp47gjBY31APMVytQ9XoZtXQ1FiEe1ylFlsAZSnVSJ4pptJmHPOOjMCcl3pCOKax9fzY8M4KmXDAJuM1LlhnVqsGVchrOTCCadiHyk8bKEfGpr9e0JqzYc98494YnyWqD149CBvLORMC6nborpoAlRZ/Gc0qHIT38CXLzzmJXr7r7Ea/Ib7eLC7iEAhTYgTn3sKMTXq6VltvJrOKnQA4Q== 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=tWtCykOoioKGGpff62Eqcv2MlOYsdLYW+Zei3u6zf481ksC/w5UWhetp4H8ipEd5xUsaO1iGr3nmH3Kp2k8sc+y5NIs2F/zoJrgGoeCsO89jS8yBgyVWHoiy4aqFHzKaAQIMHsnAME5dd7hJxKbpS/VYBPfzVkqw/zu3NM9r6+RSt2Ka57UKOWtlCCZzpERPCTHS2TYbwXc+ttwyTY5G9ltKtieBIxc4cJwiYa+amOxcTabdPypTm3WgXqNveV7DCheSvzjUkQxJx9+c5wXK0Zw89thyrRwzeZNL0ACXAhOMh9FWxrkFePKC/yhT5lSQ2cUhVtnvfBNx9WqlYzAz4Q== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:22:13 +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.7762.020; Thu, 11 Jul 2024 11:22:13 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 05/16] bpf/crib: Add struct file related CRIB kfuncs Date: Thu, 11 Jul 2024 12:19:27 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [q/Zb/uX7ndiqEOGPuH+FUeRqgzpjd/B6] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 843fdfe0-4773-4dcd-927e-08dca19bb66f X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: GZTjokqCpZ3Bzpk5Erx9rASI4Y54akaB4m6NOyZwOuQIUlGnVAHzF8AmRLYArN4HJqurrddafTulokRjwfvELek2ibQaC3yHjdqzSI8TqdWnCYEQ0UjllaWSNALJwfkRgNUnlqV+Idx1k58seUigIdsS71emxEqrQsF8kDJC+7wkBAeet4ou2YKZgqUtXjtGMMnATWM4uUIQuyCyxj5+7VYNd9rWos84nBidjkaE8L/pRh5QqVPewhMlY5vsbeapZguoeaqbkSV8g1mdND1EDzeIgAX24cEkUgUnMKNe831jDB2/iL39db6q3Zdh9WV/112c8Q9yXJTyCbFqMQuNBTLFqPFfvEH6Hn1hItyq4yKnxuJS+yJMLcODa03qdUVcDuO0odmYKmHDLdEC6o/Kr8aM3kAmN7D0NHdqnuqwG+r+8JMlL2hiGtGaHyqHHD4Eo0GYusp4y6Er/iDTewU7+t5q3rGvZ1ko5MYvTwEodpi5d8YHTv1fzxen3dslQQn5ld8tgCRZLVWkvfdebdLrn3tBF6VFWBAPxzxMAso7tOkFiAEPbUdtfjw8wTKPLtnvp5RqapHrbl6us9Iumk9A8zeA90WD6tARvKaorqVlOduVSni3u0AoEJlL/VCbiKVjxsak8SlAB7Myy5CJQOUNqA== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: nVxyAOAHnQTeLJoADNt7ydSW/CxxyQ8MkqXqiG9wKnjj+PVIpxp4SzNbaWJo7IFqpha3n1mpaws91CJgikYsCuJemgQOSNpHdeeDHncWl4KkmgIERPQxN0vzgIU+rCQL38MeS+ZRPdwVkMP5oWKjBFBLUGfFPylW7ttx1SjZOiYHMcrOR/SKnwCsUyq71UPqtseL57KM9Ucppes3gPnU15+UB8X83UPs1VklfOnoP1clQUrwKwjatJSKUMzQpP+i18YKKwuKzN90WsUkbnG8NVT7gwHrqVzOxj1qfqY0UzIYRuPizJDrbtqPB4EvZsc2ofl7iwY99jEAGBZ2caL07BamdibQSBAh1wQWggx1fh9sNBImAJwDYmSznOBEAoSQmBGaBpE+NW69Vrg2nH5wQf97Qq3tbYcBNfexwGR8jv0Qj/xbFt4cCMaAFUWPOLr/Gx4INQakV0lrNfh/3/4cIZGwDqKMVvdJXRW8WYeywolomkOiZA2ghtnHhx86QXzsj3pPo8Tfd7AsBBuMj4xXyl3XsMMvClbKtcGXwe/CIWwaLFBe6vwBLece368dmet/gP8gG+mX/eIZ25g47WsVD2h/xzwQwOkUcBb/61tToqcs048fEI1p9ReQHY1iIzAI3LqnYd/KKdafW+VdeiXm9pCYd1BTvfrUKXRCL5n+D74hPe5TpMdZYPsmYarbFBq3R8K11CtZaT7JOYXv/HkDVRj3lehuGkCgSqxXH5kA87S2mSz4vhDUHclnkWzyR6seLhTOjNbVz24t2RYz6DXAeqolmwExuF8tkcPFPKTIlhzJ+bKufo5mC6J1Tj98vpBq7Za/rtrWGA0AJvduVPltCXGOZL86v2HkkcEdsI5JzbHlj/F37qH4LlFuNbU3+FfE6bzdAFD1JiPKXOL33HmadnHFNArBiu8iTfMpfHUmBr1Jh2jS4L4YPLAWqPdYr7cCTpsQnReac6MGbsA0l1mEVFdajaBynSbaHmSiVrVgj9ugoHaMVBfAdC4qZVjmMevbOBQL6/m3eAl37Kpwt3IOe/JpRqn2uvWObgCBxRmbvX5RLouxjDk9EjrSTaSDG0fj5MpwNSxaXzBBWonZ8GJp4nMcHRPxZppChTO7isp9kQg8r5DWYOawyMaywf33AZwq4aYOuIiDma/n49B1lcUr9qod6dFXeuitP9L3tjjRG2xkYvuaUqvvQbYMoqJWpOUEVpGZRetBisPHv7P16fiHhOreyZsH7Talm8XzpEGPP64= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 843fdfe0-4773-4dcd-927e-08dca19bb66f X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:22:13.4827 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730520 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DB3PR0202CU003.outbound.protection.outlook.com (mail-northeuropeazolkn19011020.outbound.protection.outlook.com [52.103.32.20]) (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 8E11515B147; Thu, 11 Jul 2024 11:22:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.20 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696947; cv=fail; b=ljaf06VqKVBzTXF4DcpNva+waFSco73ca1DewWM3aQfwaNehdpL4iMCOrt/8HzXRCPYPqPL+JWYOrAlyhOlXnmf4FlM9ulXpwuMpS7zWpr+6oZbS+kbL/J+2XPdH8e4BVOdJyz01Ihh6hVcjkgjHvt9fAAptebV1nlUzuxjyoWs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696947; c=relaxed/simple; bh=CagETmbQzxQy7tHMFmKMp8hlYorlYA/ZSNKsjsKz7P4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ODKrNytl+Q15qK6hK3OnOeKIXAIh2I3rHhCBvIIWLjRYuX+7kpAjbz/t1OGhpGURtZQnEy0LxQ20ISBhNyrICdxB0rKnCxDYm4syjWaoCqKF9dAfcxhxOLE+GW2rjzExx7uAkNXJKEAojDj2jKcbfT2QT3rdv0EcyqcKtueWNIw= 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=EqaUA2fX; arc=fail smtp.client-ip=52.103.32.20 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="EqaUA2fX" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=k/5+NGQ6AJ5V1QOaRrm2I/DUQ03t6qJq7NvLzCON8ae4sJyAP1NpX0hSveXy4D3XPUrt3VBr5fTueVRePglsQZOQuUL0kV1OXSFm69Qqv568FP15J7SMvcbHBckKoEsMaZCYiarKORSJ9gilaloomU9zgh/7VyF7yD38y1JJSErIekYiRQ/En8xo9q/W7okx8ayA7HLh+67oW25d/3uk9P67cTT7Exx6WLaFy1VsRS59Oq4LGzVKJI+iQoOeHHHeXDN3mYULiMFcDkTtnWhuJT8xvEkWImSJfR1q0XO288Jd93fYAmX2gZzkwznH8fnzxQP/DMNgEtxl/vboc1KrrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=T8doDR9/7ZvuGESgdci7EC/Xbhmu+tj2Pn8NeF2ZEBn/IbC4mxEPkXqC4SIrdYgzoOK4GtdU9Etv1l9/+2zDxFY29+eYuYjERaP7ssIvsCAuW6llxhfK6EMB+7LDs2Q9IVk4Wr1gTbhvtZMg3YniX9LPgfOvJcI/Vx1vO8YSCNLaQnuQGFeai8kXkzJeAIfzx57Iasg0zMrif8Q/sh6AjNK+FP2y1jEisBWMXRFNbvCKN1/be7zf/+qFtHuXytYpIxhchmWJGD4+/U1XlddbxWE0gXHSTUDDlUkSKShOu3nlhebZmuuPIQnGJBx2QEuVZ2s4xTD49APvYPXVSa0hyw== 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=EqaUA2fXiNRUlFTGs5isiYrtNkIRATglEeI14oej9FLUoiluZ9XFarEwKt7Th1F08CvbNCmB+AIm1gJUSyttmicWbmGkO+MdlizShn4FKecZrM3GYmjDi68qqdhJbxCPb3L0f3zmXs5jp51VVCX+Y1/YyR8cxONTeYljaYaZElIt3qCriVIYjwtBl6PvDd+8tDMZjV0IqFb0nnBFs7T1Hkp/aRJQX+oLpuj7voo7wuNfj1HNnBn3GPBMpwNLXcb9mYSUdTCP0Aj37l29Dgp+kn09pMeC+lZ9P/sj0AFYtca8xpdB8gypKp5sDSFdOuL5+tNrKpTH0mpGJcREFbJUYg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:22:19 +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.7762.020; Thu, 11 Jul 2024 11:22:19 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 06/16] bpf/crib: Introduce task_file open-coded iterator kfuncs Date: Thu, 11 Jul 2024 12:19:28 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [gBOAKcYG9dXbNv2GXEhduZy9wJe9BsZ2] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 412acac6-e7c7-41a5-3e53-08dca19bba24 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: wFD86fpyoqFVl5oH/kTUHykxa03gaauAXSfFb6MeEPQzebj0+b49Pefmd1oCGNTiH2wzptTFPjEazfigrukrmn1fLV0fBlSLGlHy+cE8MfVCQ9a2XNVmtOUtVeiImrR+cKEEIW2GOFJEyAeL03Ik9LBGY+H/Qx24mVm5C6RxQtygFwYTAnwQZhLKP+ck5RRRLhxBc02e7I0fTm1u48qrJyKrHtqNPPA/pJzqSYiDkZtsY91hgzyQd4mUw4DXHUrur6KxeNYZrIRB2hevMIbDHwUmVa0g0g+1Oj0Kv5UWT2Zf+jIyshbEon5iEiG91DX6yMefXyiusO7nqsqw16wq7HOARBflKeQpS4mT9YqT7WfHRP1lrs9Y8Vd3aUQpCga+73NESVcqVmAJJslHHn7wGRVyp5WgcpH97pIUu4uFLxde6X2C+xSbw0Nv/Q9HJ+StFePpmYnN4b/E5iipwdfu+929uEweJTCxyX0Rh8Fkt8j0Bd012mQnCYHEWkbQX237kmK191z7Yc9+L8XmMtQapXNYc/h/fYEfO5JOxcQF1PpOVXfG5+LD+0uTHSXPd6U6KcTe5gcJhpiBSWlS9jY/4tmCEBzVPI1TtYnSFRI8nZ+u8+xgap4+jlV8TiZwNuAtvQSYspo9gvrI7nkugO9qqw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Z3f1Br7YFZaULz/Fvd4peYJMazDv4efUT6VoiMMQOXsOp3QNH1FQ+o0WVLDGpFaitQE3Yzf5RED+zDFoKLoCdjVkysS/tIvoDs20HeETjVGr1E6FwG2RDSBRYaTP7rTnDK/lTzj/LwxyE3EHVDx8nuh/cvemvikslvOggbyadwzEu7YCbpQWvOWyiBLolX9UY9RlhIBIV4bAqZLHJrkmfQKc1MxicB3c5Iew3mOCbw51oyznPyCR+NlgrWN1C6qxmVRQTEZJWc1YvgpuiJDysqYYPxlgT4+T3I8rD6SHTXHc4viI/aKpOghCKPXbp6+dvvGkGFnDYTVG7JDuawUz2a+1rijZQliIs5s228yHnKFRhjYPSDDIHHZTkwxf5ARizoNTgXe5ZpCngrd20wTbFHribU/qah63Qh9RniNsUnwcAnmMd8HY6Zu9D287ZrNsBDdVN2LeCrQ9b4oxsnhoTxYvRCrQ4biCyOLAsoeISrWK9ABI+c2VOBXpIGO4I1KBA/4KFP3Af+m7xN8rkQ1s4DDopD1qwXo8knzZoOPmU2+kg1ZPRND49XEzYgCE3BLJ5uYRFv6cuGzKSwoen1fl/fuo/GvBSVNPbKs9s7h4yejnPosNi40Ssw348/FRvG8ea84H4aClmAXoheyt4JS7hrnnD92i+83S6mnWEp8tLzmprGVYs5/rJikOHd2owFWQKzYc4xrzr3q3ef4yF7RVR99vOHIxhkx2utxeU4ZrHNYk3+OW7ysEEZqaF2x0PUxsib4lpi0eXn4kxaM8/UW6YhgsTR3YWGXOC6Rz0SDRffsslSyUKTZBADDMe9BvQP7/2PW8n9jcb0gAshNkk92/HMp/jrIXsDNH3Tz/GscVyOzm+jDlls0Aj1UID9e19UTpw3Ww+px1EMnR0VDpiJ/MxeWzZCBpu+BITpG22l/wB7tLv/WR6hQwbiG0g/f4rUA+i4FcDMHnIZKQWgGaNDmooopXh5FqxoFB4RLVChv35hmnYaKNgBkBMyklf64rAdmaE1E2Yzpfcc3OZ3ldRUKmiIqFbTh8kAJ+OetOnfrZAT/bHJHC83Dr2f9WmUfXdrW6yDxYGDpI7rWQs5NflWVoLgwF3B40AKeWA3E0c2+YNEc6GkLEuNL2J/uSRDX84xZc6uTApek9tT73E0OJtCK50a28gT+rXI8RiGo4Aldfu0aSg1YAkBi8NfqaHDD5MmLcQwleXFFVJc3FrCdK3FC0guH06xyRg+0dP8qSsGQkyw4= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 412acac6-e7c7-41a5-3e53-08dca19bba24 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:22:19.6973 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730521 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010008.outbound.protection.outlook.com [52.103.33.8]) (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 1145815ECCF; Thu, 11 Jul 2024 11:22:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.8 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696981; cv=fail; b=TZfdVkSzZs77zW5JENt3mDkENG/xA3iH+cZnFbE+Wy3hQV6WfQAUUipieydXDMBusdBLUx9D6qD8SZUTOp+I/1FtKu3VmXLh78z8lNd4W5qMed+J+HaaM+q4Q3YPdlyswsDfnNCAjRbcQUcGMHDjnsU87It3Nbj//blbDW/UEz8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696981; c=relaxed/simple; bh=cDuPnEMg04Uv1rTocjZFvg8Dxz5QqjYI4XdbER057dQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=bvHkP4hC9GcZpYL45voy77mVdbUsXsEfvGER8yZYTjpZmjR+FWBv/ROzJtA46mYYKMJY3BC/50R08QwrzG8o99YhYmvRmOSGGwIJkZJpWlc8D2uhqhDL48hrY/he6LT+NAamRX4kfE/aRLqRp/kjKx/lQ41aEeL/pJgctLPTVCg= 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=EuizLnNk; arc=fail smtp.client-ip=52.103.33.8 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="EuizLnNk" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EPw5wpxjY2DhI3qNK+vgbTiv/kXEtNNajAUG1uG6sIBcTcdN2Nb24BqdWt8qc9e3nQW9fHA3Y7SbpV1nFp0xls4+POingmkpMVabbctZPXfmD/myogoK+Ic5hlWJTmn9Kr6yLndWfwS4h/XlYr/SwSwYkIg49ceLyrvxhMKALRqnlMfpVWtLBQpBzr0qwjXS4MXRQEo2DH38io7wjuiaHp4gTuKQfScS9/7N7RscbxjU4X69kkD70ncvfoFN9Z7JIUz7KzinjePUd3xpDei24TAMvgIjOeRanQuy4USVlz17a5ittVs9nmJnwi2l+ZDTGjayA/FrWjA1M/zFiFjD9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=akMGZ+F++xqxXysJbZX0vQXOAnUBXULMgxoQMJ2McD4GSRdQ2S/bVdChZLggJZPsPR9b1peBAc2SIaO0DY28mOIhlowTnA3KZ3+gBUbkSKGvHppBNqLK5RdEYVUxoRf6fLCqfTOC2tLd7HQgWbas63IoQkRLPR+SXEqa6xMU1zUA1A02bnE6nhta690Ltn6Y8Rc08Ow0yRZ1tBQ3T5sxMg8zmtUhljDalVyPHUz/hbqDMYFlXVdWV4T7sK0AdfH+Tt9a9A5G86YLPKmPJkv25glO2vuzv6ARnCE/XPNZC+ODdDipTsHCSbcVXzIl4xy3JJawUeZZzy5SPJui/Nyj4Q== 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=EuizLnNk29XP0hZXRCmcDCCKKPr6d6OTDfp9hXMbnNOCsJaJioN0cmVooorX4ZY7Iq1W1p+dTLjehkIIb6KfoL8E4JGbFKMo7TXW1rVeU2jcuw+PFXIMO1niyemm7XB9PaL6KznvMalKRyYDeQqVZwn6BjDYsIK1GhtHBs8Ov2NzI6ic0U2lGlbHj/6U+BxSzOJO2g9a0ypAULbrNzuGe9NGTLKhVdCb81D5e0l7U+yQSQVnY676P2ibyrPpCzXGJ+dgICsEmEfwiosWGjNHGKwH+kI/dgSVqFUU8NBIurAB1L4VmrfQE0ec5h8y4UxLwCKXFAaSdBqJ7weyIhM3Qg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:22: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.7762.020; Thu, 11 Jul 2024 11:22: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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 07/16] bpf/crib: Add struct sock related CRIB kfuncs Date: Thu, 11 Jul 2024 12:19:29 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [bapnTE9qtzAYbkzdVd24f8rZrgrxo/fY] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 66ed3f69-6d3b-414b-577a-08dca19bd029 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: 8+E1uuubuON4CR3BsKwi410zpxefQL/j4B83dBofgxmBxqcE/W7PNLVPAJMUvyBbAemoaTDahD/rLzSMAnIocZ5ijn2FQmq3fe+u+84Rj9YRfXV7BhHwwFEsvTWrvs05hIHTyc/oTpqaCI2CLXQLc0miVS2ewYnQB8MTa6Qj1VOu/YocdUatQciNLGtvmodn6B4HLbouRpQMwEbvGKlIGnyYUNViXCwKeL2bHMLYO5terFGgu06rgOtgnAXU+xw1wlhY6j08h4K5jzzk27L09eYaKdZ0uty6exqk30YwLDpMnv2SRa8mloy2i+jd/ejPw0keotOBKlNg9XP7FG763wYfPjyEKb1qlxpUngQxpRfxCELgNAUNEEbtqiC4obljLGNKcyT34UvXMpYdUrbdNSCeVLW+kkAVlzMvCSIiqKNnoKdLgHkvflLWgwMAuoc4fol7ErVGjduwDnsMTJqKsthxp+wU/Mi/0aD7B3ahkTq5GFJkdA/at4mi/9vSheIud3Kq3PcU7MJ2Ry+HzjAUH37I8iD4VxBiyt7Bwf0ucpRUg/BmGDN9sDRvwi+eJKxX9NsgYUqQX/fv9btcIvH17sg9TYv83WNorIGshhr0YyyrfogMuDcG9blzwFJCO50BrIc7owVrqErzykllYmisBQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: U3CLBYUYau637LklFEJrDeSqzcJhqt9ySwipSrdWrm5xAdJotlMDoNKYBFocAzfl0o7z91CGLDu5BIsGcXfLT3Yp5BkohHqr5+gTabC8fcpxQYsIMTMWjAq9HVMaEisr4ujNyz1KVI2N6xSJDGM7a4zrANJ+Hd5H+lPuobfrA03BEUx2ETHIVn9AOeUhDnHH5PCAQpBq1m/1Sj81T/4V20S1squZ/IM/Ne/OlSPKvHDzGl5ViB86VdgH18TfZyW4R30lz6hmUspWBj6PrVxOqVr7oQMj4tl+mmqJCMw8fBFwZNZdCLUQcuEY1aOQOZCkvcBOhfB4KP0xdt8YuXZSjNfX6Cne96VDEiRJay9TggpZ/BXGv86YYAFFHkLbiyUwHqvknYw86rPoEGTr0vLiuNhh5ZkX5BzqtN668dDlOc9K52ezQrpj1cGq5Sp83saMTjAF1ySw9/wwRlcm0CIwhg1tEpuktvE/i6r3HyjmiQmxj59FMWudGGocRdtUWdRpPO7einOrTZyJVzi4MNERf3B46dNAQHd9YKARZ7wuVO3Ttpb7z75XpPcffdlbpkE76KBcXeTHzV9MVyecsK2Y/MvBtaa+vdZ0RXpJmzEV9IK2qWyZIWq/L8Gxr+lhHHf5MH1AhyHLcYG1HltD7ZdKrgoZYKOF1943T8Uhd/WBN0f+XC8IbNUJuz7HyrJXKEpPSyrshkqiKFLFN+qWw6CwnXW3XXYUKuqyApkqdiXTnaLjTe1rbpTjKx3FbwVPBOuW/pPvRrlQTemeB35gr9X1p9szH0cDhq/eU6ENQDOFil66F7GENGS1ZpwA2Lx6CNZuiKyK32TAPNg09/8UdDASK50So2R3kZaDnXvdfXUBX0KjW+wjmZoHfeK0W0pGlDcRHCwAhXcBgGSKzMqzH9Wt+jZW4+8ETojYYalLF3csDXiytARXGLQ5NLXgI2qfrnxTY6xTaxmfonkUdG1oXIIipURr/pCiylWKxAqMpVEyX31pVl6vmdE5eUBAWwzCbfjhtuWajt+zJBX9iHI7EsYVILQA2VFOxnAi763P/t0CfP5kpPfFKGM8FUQKDprXHwd/iQrBInM1kGJbHA2P2XFEAGMZlAHu5br6jtINsgfIH/jCu/JrJwo246pEsoyX218mTLbZRc3WVNUkiRDROl2oxOX/Z218sWkCeRltAIEbVazlZE1YPgpzrh+nb3AITQV8BGEviGJO/kpDKJPDTa+8JZJf79h6zF5iA0UaY4hvrsU= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 66ed3f69-6d3b-414b-577a-08dca19bd029 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:22:56.7131 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730522 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010012.outbound.protection.outlook.com [52.103.33.12]) (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 6DBD216D4C0; Thu, 11 Jul 2024 11:23:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.12 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696988; cv=fail; b=FSUkXeHt9YLZpNMLGO1Co0rgb/J5hpJOCJE9pt/+1JvbPRUNU+pOMhdPTyjel48GJI12F/C60t8FjRc2KLqM6L9R9PZOhIlI9B4x6XyN0nfnQDSzLaN5IBXArLFRuPaRwLwc6vUheIAd/rZQx3KQR9E/HcBNtQ9q9YkXxf1F3hA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696988; c=relaxed/simple; bh=51Os35jl1lGplW66FjBCF6qvLrHvOXLZJlcfv3H8lEc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Cx1tyftnc2MsqWn7+9Z60DuLMbhvAv2vYAIxE9g1jSSs8RK38dZUkY1BVHeNhqB4Ae76yI8lICn3+O9VpW41YMYWASJWuo/WbklsSxDKqwIQyfMG9xkjVIPa8FBbzhPDuGn9bH629S1dMm9pHPwogE654LSB+vG5mfHKOZmqAAA= 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=R2UX01A8; arc=fail smtp.client-ip=52.103.33.12 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="R2UX01A8" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=MHr9bUWdwnrGQUWZkDIvoC2gtpEDzlu8Igee8pl3NmDIRxuF7W1hGcRPdL6dsuuI43yRMc6rttNG5nenFqqVCLP5WArBiETn5fL1xgQL3mP6zDMZ6+nHZHhERrDRo5dQOYAs7eRNyl1o6zLcrb98pr05XVUOpHsfqSSBJSJPZdYrkiZWc0xDrb2EbrZzEH6pTlqlYy2S5b8RlaDrs+lFj7K23X/HlJurJGE4xiqD8x6k0JGfUjvMJnAlwvsN96990OfxnPjpi5zcEbWZZIsGavcm4fTkSBHCt/LhPxS600m4brdKMSJ0RiK93AvVo/0myx9fEzhY8nex+37zDQaisA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=eHdR0Wm2uHpSPgrnOobLEZpyGLTDFpo5HKcu7DjmycaI0jD/jTWoB/Ocpy4LVeq8aiSp7WgCCWk1OOHcsbjODDt7zNs4KqlSXVpETIenkau5Xe4cFHshCU0XJ5SFTfpoUm3O2pqpPReGGmUaSwe6b+R14aOK1ybXxkUVhPvcL5Xx+3ZdZFTq5HoQNgqfnc4ayIB+tVr/jHN8TcTo+OyFC2agE2stUJTuEjduPEmUNSkJWdMWjmuxuDYPiHjZGK9BTHZfex3KWTw/wveDvLoOWdM91xvZTwIEB5ZnvTH6jsftixALj2U97KM1IDKAg5JZse6AJkqHcLHgaDA9FhFO/g== 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=R2UX01A8sF2wZDJFhMPP6gLaFZugWVFkqx/nI7OhxSTwwiN6MugAkXIgwVNPHbp500j/UXKjbzp7Ba/LHUyjo+ceDTfDFjrYmzk3qp7WADQoaHLIsa66ByZQHddbFTj5FvWGyG5zy4IqRndwDxaIwsCpfUHfVRxrozPV0yGkRphrwvl0B3mIXyCK4RCkmjk8+IYok4cX+88fOBpPaNwaEnzGBBfpt3tmOyqkfxxaX/VqnEjHVBL82yvaMlsG40OiYt53rGudKu6qKPfzya25Kq4J/hRAAjzBJZKDHtNrEwvdSfreFEl5ArgUiP2ZbbpFgwtuIDDLqwICITE7b4KmXQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:23: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.7762.020; Thu, 11 Jul 2024 11:23: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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 08/16] bpf/crib: Add CRIB kfuncs for getting pointer to often-used socket-related structures Date: Thu, 11 Jul 2024 12:19:30 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [Q1vToMV+lu++fH2ZtG5ecjeLEP4iARZS] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: cd85e690-fc86-473b-2281-08dca19bd427 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: S7ZcLnp3rWB1zBNzAcmXHZLBNdF4hdDkkAAUjDTPaJG1WjsDDTjoUAgR0nKzOLA9yt+jzzQvyKCspjBUelAS4DPp++UDlytrc9WefpySx1vZEKJLWNXsGLV8+mJ2JlCtPscV9eqpAtgeDJwcDB5dxhQbGcznXiNRtwUUMzf36H2AboM9kXErdOSNUOf+OoM5yiyOOmnGpHct8Vaat7vC5Zy5SC/IZff14PrZfTNqaxIOZrytI2rdAV3Y1MYHmR4rieGgze1Ur7csqT73zXcmVl0SmBiTlEvyE1oLyzq95LORwflwmLW2cSnrEf8+gwOzWxkjG9Ar3l7SA+XXHiY2t9iPRYLGItzK+DvR4mn/HthdKeK1RefqcxoijyOYl667gFikWZMBNtUf2bL+TUXr+Fc9477Y6foTnm6f/qgvwEeCQ9ZF/Xu9NHltSUEwjX2PwoorKNbZl0E8KGwUnLW3xZcR+0Ve5phMAYCeryIEaODsK9PLQHiGmX5HgblSm3Isu9lr36cKBqkQqRwTLmfnouNvUN3Yjjxn6FV4pUBHRvOnvE3zyqkughl/NhKA0hB2kOuEl5Mb172275/Y+qamDpLu4AFu6tJtP4dfxCn9d1SWvT28IdfqtZ9mgsbRj2CVXNPmTiZ87YMDyj7aNsJi+A== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tYZyFBHm1Yi/9YFrcABm6+82imA4xOkUy2s74H/I1eU8HoRQF55Md5AWKDdZIM7wHlV02ZUL2kmqGQlkMm3FqMHfPjbF2n1eIeqyNGPgJx/1uzgQ8oFZZKWe63zEcOI7z49/i5zuRwdjesxChQqYbEM0j2eCPNAhlGXVH22+L+liyVoTWLCa6/0Gl4MLOE4MGah0NMirW9BhsQ2n84ws66bB6vhTm1D/GB3g68IJtPvyCAMgY0Jm+X6G555xENpdNQyTdbEcAD3oODc8Qy271jP1e7rUykIsXPWkpUVpA9DZ/6SNzW/FVUip/3VxAKUFihMNiQEVT7xFZ32WS8KXdOtLP4bVTXIUPa0ugfjUSPhyvkum82obRHwXm2V7nXPAHi/3GdvqyXVTOBTJ0VRW9cNfPNHl5nkc0C8fbHm8GzT/zpfsFcxKDstP2UVhRYFXJiD5DIXs5A5DMikwEodoEu8YpeKrMorNUzaiBtyzZeHYKtkXQHydDNVKDNuHH/KSXjnnMfRdJ2OGOgFm3tx3XH2oX6EXX73wbH+rZNabJPSS3rh3CSxroWVhehRrAJ7s7cS7znm/XupTK6agcGuJNabPIFbVARGBROWtFTmpraAzzbWM+6ET1ZWgjKKRD5vhylpCYBUjV700okuCaJ7MHxNYNLsmPOXEI5eTqE0hqxnasUmL4uDCOk9r3/FuoZT7Xqs6+Vq2zPSLhFVAKRvrgBWnol5LrXk27XkPiS1rWGjN5bIr00mFcgs76lRJHqglcGFs1bMU50KoJW4mZu9g5EeDKfwHbd2ihqjXV+y762+3h714EFrYGQnf0AT4YdQp3OlVuOOeIZYRqq+vTOxIQC/3K5v2vkECNVebxLqgtTjNm9UDljhU8S7DVl7Fek+Lj9UeZUiU4QWuZbM4VjiQhOGPt8FAdHswT/GhZTf5MNbnP/31gug/ExXa+XbpRkcXGRXtF0PpyouhKCYiTEzB+KOIrPWmXnWP5cusZbk1SwtXxngx19RP8ZT3kEQA9ep6wt8S1h5XIwCqCEhcwGDgPVRn79Tej+HEGdytj9YDrANt7KrUr4D9m7vMfvkeNTkQnmU+pjxqI3UH3+wT9UQpbba94D41XujASx8E6uLQjRAKu+vLfhzGyjWppIUfvuWbd0k2w2bum/HICzv5FKiNLJ24AQG6mmJTgZqrgQPAyhJ584PZemitEfwZ5M3pTfGWxsKGN2VJp//a8ba6ll2//hWqCUQbaKb4WC7lY1Pq3d0= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: cd85e690-fc86-473b-2281-08dca19bd427 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:23:03.3348 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730523 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazolkn19013008.outbound.protection.outlook.com [52.103.32.8]) (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 7349A16D4C0; Thu, 11 Jul 2024 11:23:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.8 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696995; cv=fail; b=PU5/h4nLWmQYYZzzvoi71nAIsnlT/oiW71j9DvQ9m055tQMMY2hKON9gYL/n8ayh4hl55q0SZNnkpLZLisnNqvXy0BjfWJF5Zdz5X32wQlkjo+WAzUK/7ievfgGAEyqzXCaX/ZMCtMHzwljbaoii3mhEFMGdvj0E81xn8/MlcBQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720696995; c=relaxed/simple; bh=BZ+ao6oDtD30qjRDcCY34g+/0uU9zp5HdwclRLhI7EY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=p902c9B2BK5ZQ8i77haYOhDuohv3xNCl0n967PyihsEe2BN1J61d5f/20REcH8CL9XOend/kZtaYvjwC/dNdvL6c+pGqHZ5F1slkMKXqOESTjKUZqexdhMdaFrYNSsLvBKY4uJpTYDE++FLFw9KHAqRHYyRCRoRfNPhr+SQYg7Q= 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=L3KgH1UD; arc=fail smtp.client-ip=52.103.32.8 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="L3KgH1UD" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cDlp/bwom+gyLsqgHlp1STth3Cp3kiZ7loCsZd/Y7D7QGTDRjIURx2tFdXnfpT2/xotfmOIIh9fuJ9Y1hQR8qPrdIdFRaT6Hfv16JcQcd05xa3HCLI0Im9CeshUEuF/TCiJbiClT4NzExLOT8cn9Jyux+5aMiU+eFwN9XLLOJg6+71f/mK/Kjns1mk/NOxaMvi2Fg/jZvCsT1/KiWk5xNEvKcx4WqLSwzEvcTKK4YSozhYIIrzq6dn1RhAwYWPwrv6PjhGSnL8Ts/vTJl1KPIKQqPdw4R3s647p7SeiOjIqjW5ku2vYKoxT6NxcnKfdsIUaxsoydhf6Xs968jxIhgQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=mdHRgfWPzJ8r2n6PiSazfpzJVZftHQNXgbbnKydWykb1m4mkRSqHRasfQ0BvM8Dyyl+MiJVVn4BxdxLDKfWIjrTJvAT76PCjc9+cnzbSg22E/DsuayyMaK214F82uXl0pj2KQj6t7Fa57EW/7r8XU3Ebn6RCH5oyF6dcfTMunc6+JuXGGAlxfNfD2qesJNIjiIp4D/ORN4r53l6GqVhXp3cP37XVZI49bkPdUKM5zbYvCwggEKol/+VE9DnIAPW9eVIEft2eJW+htBuNQmG8xz3Ules69QmBGZ1pRKmESPDGUqZFC+jQP7uGW4aya1swqXAfgxx4MsJ3z+wL3oyxQA== 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=L3KgH1UDOavrqO50KDvkQYehLgJYOMbRgSMAAZONedxC8uFfriSkOPysT06CHJKwrxUWyuXAJXWOo5QrlxfaIsbQ6QzLAvW0jtPpO5QI8G5e37y0T8kSFdt7se7Z6sKxzGwTtR+s4ehN8D9tMACD3fbu5KgFsOlD16HMAffQdSNu0eVKLv+hUbwu2z8WJamdh5CYODghCP8uwNMkuGG5UgH2xRdov5zArCkBS5nIiCULz6FX+gKKLZK8meEQHxtXUZcd1b5NKtr79Xwlul5cqXfFuD3AhhSARZcVf1w1wPsz5Gt/HCXv/lIOb5Q6wkt3hfmVSaJLDufeooM2vw9k0Q== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:23:10 +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.7762.020; Thu, 11 Jul 2024 11:23:10 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 09/16] bpf/crib: Add CRIB kfuncs for getting socket source/destination addresses Date: Thu, 11 Jul 2024 12:19:31 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [TF/lA0m9xKLIaWdT4UKxxh67SJ9jrjL0] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 97bdf5cf-7c4b-4cd4-8ee1-08dca19bd856 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: uxVrWgkPB4X3AayaT9WrmI9YgEKoRXRPT9CEoGOfW7Q/FHPLGsNnucwMG9Ngp517JS/jAD4FKt4eW4CMl8H+L0eF9iYFBtiHJZHlwaDSQsKLgvKsIzCKg8EP9sNvuaPHEF5PXYDoOPHPjzNPYnOebZN4d8H/6wpSIrXmY8w0iEwsy4GQqfk7EmNkFksrdt+XpHMt84K+UlHwsb1pVvMK2WMPvx9ry0yvPil3vCKAqaAUCO4AipRCxTVn+XHxBCHl9b9CAZhKJOvm+FxdVFLwn7Z1xzrjeAGelnAjbZqacHzUki/FuwNx1s0fbFx9EuzM/qIoWTcLaegQNiZDAh8X/NHhMl3wR/bv37KTIXmj0wAo9bBofNNP1G8mUfTxT/g4ma+GgaTUV0+xTgdr6t+C8YnQq5U3vsNhArRGvrrWnsvQ2cz9d8m3ccAyvMW3KGipzEbEVYQfkNsIsmjN15iuSNvWi8yXNKFnWWwoGOh53gTaTz2U5QK0C+B7hg+5awHq5cBZ6PVlLjyoQPpK00eygzRXXVgN0cWNVMKSJbG1pLRlRWLamh+tt2ZbO3BDph+JsNdtlE/Ij2Zo2iWFt9rpt0w5Lbnlj9lHWQyqw+2O+yAUC2z13RjZiwf17+Z6KzFLqB4vyCf3/NJxJVsXzRsKlw== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5B3lHLF1s28g/+bjjyGl87MYx09QVW1Co9SwjG1pnrvqSRYM16Xpuk5XEH/zvWX5yDu3fly42rhzOlKHB1jXYqJqbMJ5Xrcy4vWDkkzOwxUnsLBMXOUOa9EoD1+O+Ecz6naYPUohz9ExTQUOP8rlXbys1E9aCRMIJ4KVlbLvu5Y/KTZUhTSWsQl0PvW78Jut2f0tEQo9GvOcrTzqtAOAs2JV4u7+KvFxZmWdODxf7YU4Xh7QIR6LMWHtYFZW8Xc76suGU+cYdvGjtdhh+xc0c97Ozn1zVZvhHNFB2fzhz9s5iaYMF87/SupIN7vBzWHRmTI4AnvaETSu49GNo0rbp0ePxkrtk92vBFb18F8aHYLsi1EzIYcdars7pbhh0FVCQbR/zcd89wd+gbpXwhb0wJIqP6+Mg1HUYjKF6HmQvMw2vVVjoyCdikgFDXn/aInYS1D4fBdv6H31+cOGDE4f/KX0RD53UnWPYTay53j4ImqexuED999YypAluZArFfHolXm4Xk8fnmyCX/ApgEW1wUG0IMREcpz4VNI08/cvlCPi77A55BmYF7815TgHniXV3aeHafmcHAdtk1EYnSKJRtCuOT/gVLPROGQHBCUrpWwXra4gKaag2oyEYYZxlD+y/abl9L8A+rMS1s1+93QW/yzTRcRnzstl2Zg8PDpu/M8+WHNw6tDLzE3lID3I2YRwQQ/ONDl+9BMEitpW2uDLOWhN3J3pUWCTtORrZHf6gVTtxsl7tkjn6Ko3gIAYEoX4EcpZ7MIOMbb1q7cGf/s/DuVvdy1n7ZzDIp4oYE5b1aiqoR960/5ZSOV0mrF6BTGtwykTn3qLIQ317GFiPViPCSqYQ+IIkNGIkxxsZYXLLrzvTsm/2Ma0NAy9mhueemhtfvSw2uH98/a8Gr2be4uAFbBDN6lJ+eIPO+yQ3bu9WzjxBKfF0kNi3cDjfUVrLxR4fA7uCOLIIK8SljXDM++1s+ovunxek0eqccRBIUCMohQoNMiRowJuY5/ZiHnKR2U2pLGIFyxCKbE+c35m7SRN7lTbJMdeTAwaN2g66zikoIlyssxJmloiuaJHgS0QHOcloAZWo8ZeL7rSSYRDBGAAQM8uJhWPAg8IwHRh7E+qkXYJjzSeyJ22B0oz6HlQpVetpbVzxwxgax0iQ1t1si9c8AI9gf0LrbRcnV9WTdrEz6Txz9JRS6pMnx8kJb+pW1RFThsA7SrcGM/0MkA4xwlbcTfqeS9EjFViJ6y1ypXN6sk= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 97bdf5cf-7c4b-4cd4-8ee1-08dca19bd856 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:23:10.3597 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730524 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazolkn19013013.outbound.protection.outlook.com [52.103.32.13]) (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 82AC014A4E5; Thu, 11 Jul 2024 11:23:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.13 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697038; cv=fail; b=X85hqC7W1p0v4NqKhLHd7kbOYVSPDlBbcO7RxlNUvwAxjqrZLfff6kUxxa6757FyKo8t/UPYTwFLabnp7JNt8TtOcoFsiDxfNJqFa+MYP8Hf6w5HZ43rn42BYyePgH51aTn797jzEydJM9h2uVEXWg4/Q9rximuj9aRC6dNuJk8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697038; c=relaxed/simple; bh=HQFydNqnDpEVEqqyYpysIENwzRq083Ki37ql3puGGEU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ADsyFooE8SsC+FB6W9SU8KIVwg/YBZ9XWvCSu6TtQHrOioB47gIXgqx5kmZaBtbpK/hY7LHcBunWJ7/mD6XIXhFR/x2w4jLuXh/hGwfAlG2P95B3FfemWLytP2ravHgrfYKZAR9bhYEedMOAQkbYnLVsB7tnHyxkylhPY5rS9RI= 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=k3OMPxit; arc=fail smtp.client-ip=52.103.32.13 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="k3OMPxit" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UPHxQEBXiL/XcvuS0oFbqCNF+unaFc9NTSTAnD5MS0aI2BEbYY9W8J0KKq1thOjzAWMuSmzOtf2UWgDV13HwHdslTxhAgvJJ1zQkKmDVg4JAYqwT9XH36RUPIjJGUmeJiJz0pOizvBYuYAXK63D+ptOQyF3DA/ms8E4wdtZ6svHwvjZ3CdXT3GW6C3lHszpi82YAFQNlwPOec+UG0glasTYXbv0/1ynUnVkrBEtIhT7bLHRW6x9jiXnpTwe9/Qem3S48HJjRLGoaHmuHwzZbXOUKOR6anRPvyvHXhDPsBqdJ7/8pt9CCiilzTUCrIVEBHQX0z3LvYfqZ+NGf8dY4aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=qXMfk9Cbgrb8pxB5LYqcgPvOZQtzTWUgiB4XFBx1ON6IJQYlleUoYai4D+OC7iuDrKMYBRYz90+W063vk+fyojTudOb4BBy4HE3u6rU2lxdeejSI8qfJ+wqmA2t5kFln8tx5A7dhpq+82gKD38gBLGi8vZ0TcDKnBaXBL0pyyhYBGHOjCfMSOOJvwjmHZPz6o4IVSkPQL0LHwfIO6y/UPE6M9WsryUtgE4cwMDsyTm7ZIHzGhPsANSeok9JGC0eyGsOkR7ePaGkpLSgop1Hwn8pmlwh/tpBLLM0NzoKq2odVEOWRw/BY4RAIbmoxxTvDhXQyh8h7e8HI6H2VifL/yg== 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=k3OMPxitlLxjL86G15+kpq91WxOQb7QN5GcwnHOje7F+hpaSr+weIJxhd2aUtMQnOS0ZJvWnzFl1IhNAKPOiCurA5TzCDfADIxItR5M/3s8lS95Sx+QF8Htg9dozwayQm/ibdFBRpIHr6kQWOiYXVo3CB+KHz+U8VU7vBowwa7CtalLbLTLDWPI0YIsnQhmjofvF+gYPbtw8qQmHMWwz2HxzUSt7TPxS8bwHTyoZ1ogJnPMHy5XcfSKTHJo1131kqv2U1scvhgMHEg6+cgjLHXSx5LAuBaO+MQ5xP1qXMB9NTThheiB4zUci6kmctBj8Jlola3wQ4N3Gg6MUJhrDuA== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:23:53 +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.7762.020; Thu, 11 Jul 2024 11:23:53 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 10/16] bpf/crib: Add struct sk_buff related CRIB kfuncs Date: Thu, 11 Jul 2024 12:19:32 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [yz1DmCL6DdSaEH18YTkp4OoDPsVtt4t0] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: b2f6566a-93d4-44e5-3e50-08dca19bf21b X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: 1GTr2RnIRTxRXAhIefU75Xk4TjlxB5FvNhA/Jkhqw986/vqeFp+wxnA1UobHLXlY0vJRxWrSMhwB+AkAe7wQNVQJByJV9owb/tikJevPfEXwnk9AzcTJiX2Jrw6vQSD/3j1K3iZJ7LTfdlnGH7P9+sTz5gVz2olzER1X+F1Gjm5F+0Z7Hmm7acCOv+hfcK5RpQA1dmEtpJbOdkz3NizIj3gXbibwbtPys0d4C/YeBvFFfFIZLTbquEi9+HX5fu4HnASwqFT2s1iDBK2SPd+DFw+hTuKv8jlYZ8y/0SVpn4BjCCK597QfiXJQqWQEW/WPkcEUAEWDGBitH8NNCgh+cX18oF90LhviFFiWHlIf0icSDHwQFdUbyyeP5nK+/meBGmsrQuntg19Kqun6nSVpDukZW2qBCV3aZs2ExKeBgZF7ZH7zEPHedDE4SHTH0UjnE09TQV69S10ayHyFqa2aP/TlJJsqUzlJa1oAkMdIMWNNv1XdzjplUrk7cfsIifyglU4YSkMNRZikzW3FM6a/4trnDapZCdcgcRzEz/GRsN44n9qcTb2fpA3tR4+sQeWlFrik0hSD8Ow1nLs9A7H9r6qo0+Q6sy9c87cd3aoCpH/Qh6FWVXMKdRX/EtZYz3DccZDBKSxGlPv1n/Iuxz5nYg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oWudlq3xWJ81wPPAdBZcNrFbZHl2fAai4J2R7brCXsFsyGCTn0FqqCalJNRFtVJswHkSjCExudkjyata3PcsJLjt4FG5fiQVNV5y0MiaIgd1TD4Oughm/Yj0b7uX8osIctwj5accXkNbbODTG0eVjrGhg4ulvsgfdMKoByqJcFx5weSty0nGWAVDTCIb2kfpA37OBETQ+e08WlBzQwon405Mf6eMcNCsRUNGBhXoiUnzzNPZrdv5AsJ21l9tCXWbpjB5hF89tNCw6XKhYMPdRIapStPVDPJgSlrIBiRQB4YyZyHvTdufcfm3+N6FbEQg2zVW5kHeaAoXpNyN7iCG69q/T67C3P4el4h/89mTrBs28qnkvRhz855eMt382vluXVPWhXdaRMP9xMnWC04ec+Mr4s2pPtqligh+OGFYslrkir0IUBp8kUc29UwJNZt2yEU1CfrfU9H5uu12H7or8oeiL8ZoEfCZ4DIQArJrsEbgaeuXIFk7Re+gV9ta0udYD/qivd2LG/PQiGplW99MqYy8MV5T2ByrzguhPK8AUg8sTaAUGEUYEER4fdvblXQscT7oUaznMv2qqHABRFjHXW1/NA4J3vDC8reKbzi0eE/zhi+P//QNbAteOW/I/Mh7YDUZypjOGiytRULP/75whjs3uAIJChtboIo8iaacWrvyjQhGNrHw/SgATK1Uh9pKqt878KitUgEFSnEOXS4DBM7dfkKzGQq9HmyH9htnhsICXP08arJc9U9UQEIANEFlSO4tqCxar4/VgTZPmwCkcx5/0WSIghMa0mTJg9osGF752o9iMHucC1KgnpuPPtEyJFArvYfGyfoQaj1qPVVy48hOZxKorHNYJLe59Y57QlocrCS75tzyC9QQlEO43fXnkkhf6V89oDHGABgP7bf102Z8AAK7Gp2W+pWLklRk4d08vfy7q4HNiyWfLyXC6nMdCsVcwyre3OlxNe/sNvutkryjIowVcooZcc4uIh8GbCsByrTLVm+yg0doRVkOLx6aLx4QSngtAE3TuFS6zhd2WtGfXccdl65+n++aeFWB1Pe14xL+qhRcLpfbVTkn57OCOWCJRmmgyNTAlmGIp22nM5FpQphG4zhwJZZcLwy/8CasMY/GVeE+YwkyoHqxYhkQO7YCkw0bNM54ix+hblfQsWKSwLGHxj/rU94Tk1qDFA+jLjvFl/7Il62ja3zOkhyDRiXzw3iej6vzxfzUmNhiq1kHA8j6Ub0v9rRSfFg+kA0= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b2f6566a-93d4-44e5-3e50-08dca19bf21b X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:23:53.6401 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730525 X-Patchwork-Delegate: bpf@iogearbox.net Received: from DUZPR83CU001.outbound.protection.outlook.com (mail-northeuropeazolkn19013067.outbound.protection.outlook.com [52.103.32.67]) (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 841A214A4E5; Thu, 11 Jul 2024 11:24:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.32.67 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697045; cv=fail; b=oB2XAaAEjcOY5JVVLUToUJ/E75GyTFvNFWV/+3HrnftPezbgvMXNmn7VtObRV0QCXJZwXuiGh77YtPFK8kFolfCJB8mPjZZrH2T0z45N0l+wnC2OuHbfn5x6apd/uMQTvhEzewGmpSqhVII8FfvsW1/qZe7Y3jlebdbf3PQC3Hc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697045; c=relaxed/simple; bh=FvHTQDE3p16wuWz9aM7RjyEAYfk1Q5AMjLIflanPGJc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=isX5DKsdxwApzChCcjmVOFWi4IWJlHw8syhbMfjSjRzVdNmyVs+SQDOiA5XWMDahsODiEBnpc+VbkmqPi2L1krW6MyBCmoJdlMaDxTcSFSSALxCdREs5cUHNkEtmcqGvR5a03VJp6AFKZqiii29OPE5ChYjCYMefimtrp0NlIL0= 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=kyAJxFAc; arc=fail smtp.client-ip=52.103.32.67 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="kyAJxFAc" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=DcEfaomjUL4Q+Ztexor93zj3586a9fTy7zbQe8B9b1i9dcxUe6sv58Gj4YQIVMOSVzglfwEzG2tDVfoxiv1iaheG8uAfECe7X1LJT/dsXUAQZIpQ9aOvkqCHCFN6hzbctbn2SpNVnEX+NOk882EDN/Gx9H9bMgCtW2bGWt++zwSL64UYGXkFL8sA2sIbc3WS4rgw+S4xkd00JMXvSLZXf7Uwx526RTjT7ENVz+xMPWwpPJt8Fn6kZz3ILgij0oDV/imOsIiEXKcHCwWsMMeRxFn9oY61wkqZg2Ig0PgUzwjMcIkSaKrXUNtM6d3FDnaIu/vkyxl1tjOSAQq3x/Bv0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=x9OaoBabqnq/zSqoOjHjMrRCh5eQpL5jfh7NKsGpqQj3kcCoGlem+X7bEVUjdKrkiWRLak4YUlLh2F3n5BfItCEUqS2MrJEea4VL81amUZe1yYYUaAPOeGiEAkDiR91BCI/exPiQ0JFhs1zvzYRDaqvXdBVtQoyEegkMW9ph29G+gFzHhuBLM7p9DOKXw1q3DHmB4KrPNN3Z9lJsaZrrblYqATMHQZJcuuvqWAGlVbTBLT7pFxz/ljfe5W1gO/gqyUVN4/SwBmHdDmCOOrAN1pcuWpCz3ANVN00tmpmOKcJu+JxND8Wm7i/BDp9SSPiFgnlqjGvrEZdolC5V6LuGcA== 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=kyAJxFAchgykgCHlMZgBixEBUQ60qN0u2HMFPod+4QncpMnnO/d4N8SatCiP3J+VOvnxlXnCRDwdpu62YhIiLA/FFumn46aRrvV3fPsv+E1LuQlO4iBkvKbdG0JWe+oU3NKX5jshj8tV0dwH4YgJxTs96Mano7hZhsMzG7PKU0tAKPTkBvXQsCsJwaVuf3QAybROcatqDgvPjQFgPo8tuhXq6YfoWZwfWJx99gK2QMVT4hLBbkyBf8VRz2WtANDqy+eRE74EAekhbI1xSG3lPb5KJRTdbnXljpYWwjStPncMsxE8tNN0OoLGuyYemSc1X7TFE00vMYVO0bBAzS/Daw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:24:00 +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.7762.020; Thu, 11 Jul 2024 11:24:00 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 11/16] bpf/crib: Introduce skb open-coded iterator kfuncs Date: Thu, 11 Jul 2024 12:19:33 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [2XDViwXCQzMIVGVmL4LRbUeX5pajzNZW] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: b697182a-fc50-41aa-256b-08dca19bf60f X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: WtT0pOnawIWNxq85JUJ4B8QihcyCyJZua/X70RPKS5lTYrMbIR4aKGJ/pkgf3pgfMAe+33KbITn8LRygxKcdiwB8gMBdWn3VjHm6UcEVK8zXJ3p7qh4Q+ajKCSwSmTMvnhhmzcLeMx6j7WdHMpKp3UB1LldG6sHJjUubHnd6uLI9ptvq9T9PiQLzcXcg3RFdYL4DuIAhu66RVBaJHTe77GFJCPr6Og7Nddr/gvEchAZtBLtwKmnXBYmIVVkuvJuMIfQr3x8llqIy7uY4npEjkZS3wB97/tbTUh4Zw8xVlGTotrqYSAdSuujWPOZpc1CftRFHcYJjvpwavEuQr5DeLhSl2Z65PPlqVdfA8VcgD50EnO+ecF2ZQupQTeoaMJ7hW0T2XzPIp2YnBunXKHYJlwzLnSH58lGBfqilbeOKgccESzbhJU0w9xEfzCH44NiytGOB+ElNSPt+MKeGTQhWRIA7BGEpOkaRNy9uI39esx/FWlmXtCBbhS2Ge/OssUAEI2pAnrf3ReulyLva2+n/fHKjNAdTrlmvlg7hbk9XzSBfln9pvAeg7QSfGkUjmyrC+qA2X2rG9VBFwYBTjMNBjoTXdxQhFfBEH77D5CkGLYv9DXjkbnKhonQQOwIVFaFSTUcHdCSG1izdrFINWj1+eg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gtVModP6AXsv1cPOVrbTejs4C8T0hfqS9z5FfpJksZTTFtDr/aQX+XBsr0cGkySXxBk6sK0COExpe3ypimnfbS0UJXpj52AV9g5lxp08E9+7EXb9vJ4/HhiU3/dLkRGOEMRYffnoQG/H1/ouKhnOrPQXWLg1aQ5OnUhFWS9Le8ZCV1n1IVHlKsBd9iNeVd/zsVn1GOEA9Auk7cqfMTOxLXYYT5OsyJvY8mdNyBH8SJ8V/ZvRfZuKbTsQOB/M25GddPczQUDCYxmUUJUNX5C9rsejfvOoheSOp2x4geBrjnIPqRdGpk/4iO4LW2FxzD+KiVYmjRIzoUjNEenQnkK/+jd9mDDJ+vZjEA5auV3uCZE+Peod6csl5IN5yvG7Qki0X3HB1mIZi+lAxflfxmZmzc37QsL43Qu/nSJcdMK6dWASmP2CuG6ogNrYRgHXs988d1UQkkVrNeDF8KeYCRXd2XZj+XaqqKnSUNbMMth8s4I/JG+YhnJiTgncJYem59K1A/kwA2Yx7ux2TaKzXfgP6Pck4/ZpT/5osbbU23X1BaMMoob7DfmHtmmwjHoexQQl21fR7cQjd35TGXbAlbc1GJfB2VKEx5LZMpOatsNFtkA1CYfhthLmLq2scgCMmDvxCIknMvhfAE4ppPEBWrPhu39ioX4JnnrVBtb+re+o6jC8E2k7AZJQ2hW2bzhXNvWlR3m6Mg8OG14mUpf3D3Kp+IXfOoY5x7oCM4Pv5fVNR0WhbhwbEcQP+kG92V5tMw7BgB5/o5WHEAqk9b7vwGkliEc2XRbtqkl2fz/A60ZxHjjV5F8gWzBlAyYWCZRqSlllTAz4xz0H+U55GiCdxnRcnrVM80TVr+wSXQ1KF632UyBjQwEuHwzg2gTDdVca2stLE5BTkTC8IyNMwCu4Lh5Y09r+T9CCltfJ+jiMjNWvxdyjhvL17C1KBnDs+LuNdiHg0T3stqrTSNOJCAFNhS9Bu1E++CWhMMDYpmWs2HQxaYr3x3Clv88OOtM1l7Bq+Mjw7CABFS3VC91sfRYA6d2d3ehCd9FV8JxUOR6Ha4/QzPpm6viDPRCHjM70Zds3ecd6M+z4IZCLODKUONS234J9INC+xqwiPEuKm6utcBXeh2gJcoJD0JLyaOJr+kkoVbRaDx+GWLeuVK6zceQdluQv7NVzel2Vx6RfXPVWMYuzFQkxz5k96U1DaD6xcVtFVOoD903O+SCT7/H3jhhG1usP3A72GMvwXfRVcuuZLv4Uuks= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: b697182a-fc50-41aa-256b-08dca19bf60f X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:24:00.2499 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730526 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 E7CFE16DC09; Thu, 11 Jul 2024 11:24:09 +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=1720697051; cv=fail; b=S39ZKKiA2vALNVK8uABLp+1PEWUKg9/6dXeTSiNiHesmryjRVkBqmOhLl6dWoPcXWTQ2hgVvzD+xGXvIrxrjczLM7nxGKtObLU2yoyxa8/ODGQ88TsVC7I5PCQSvjmap3Y+Q1E6vGoK5bVhlDfzcZm7NigR/UtmP5fEVSBpJS3o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697051; c=relaxed/simple; bh=b4E7pEvHSlooW5kM/GHXqHjYvTC2iOpkyNSk6ILcu44=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=lIR3Mr74FdWwU6bUsy68oHH4+0t0KK0BPEv1dyuWOYsBz7ipZ1JFGiAQzmLITgV73HMK53lIvUNgLYi4OH9c84MQ3d58OrwHd0ZlEabkQ+A5np44nvOp3sZY1D1ytslkQuG/O9G2XweVcsmpsZfTkKQLuGGaXf0tv+ujEgPv8Rc= 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=H/5TQ/Jd; 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="H/5TQ/Jd" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Eu5FlWyI6aNducMcHDgcFqSIwCMMSoK34Ee9jgzujCouJhdJY6fTdqp36JsxOV0e0ThqVt8Z1TPKOOGHh54XZGwbsyFq032gfhm06h8hyY/42tfBZvFUz7POEDe8FpQQIiQEdO5q2WymkNMTMqWiW80BquYxsVlvraR4ihLN7MujL5vHzp8UheAo6MobBTDNmxOUUgV8q0e5IghWe8ySVwFhqlizHjCNiZyf9sN+9YDKbeerUXcBqJgf6iZosMDu3mi5GCbSG+4bqHb10i6hAB7OAsiHkMuCgLU8tdUXU6/kc1AQwvgX15ntJXDnumdeAcwlUNZlV0VJcAzbqCxp3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=vnNN3dFx10axH3O9vsERrzDlTWpt+veC3RHSCLE2zDCeV2EFpsM6BRios+bhD0CsiHeWjpteNcP3Fp4ruajvBJLEjYZ0v2bjz3RH/w9D2s9QY0tHRQ+rycVLCxST08PhyMgQqXCPXtRGAWFX94dnzecGVXCjHErSZYo4Mrv5vZHBJO++RE0X1gFww13iXuY/ydVOio/RVTl8VDFrmzAJki/0P2TU8KwPKYPn7wmg2PJnrVsGK1cBoPbpD78xi+LpHt2x6aq4qBeGlYyHFz17op+xUeJJvWCFcfiQz2vrO5ypVpDd6jHMZlfVaH9oErqh1ovMxnGxXjKwsZ8Ccd7vHw== 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=H/5TQ/JdYQqvJOme/xD1hpuQnL2LThurjwTKyC8R6OhqgdpbQQfS1j+Xrjc+TjchwkC7fn/q4apcBwrEAxD+vBsUTt1EKLdvNizmUej/mZbTdlzpTDtxgTYro2cHIGjmtnzCsgAQmw0EiCQxvFCyzxoThS4N2zjrXp8p1Rj9dEXEFYkwgrSwGWgTzYC+/L90bfFD+L4hzlO69HTs4nADFvBxJWopvtFxumyqXuzxgyCJUgOCf0K1eAojsRvrHHei5zXZ+irdnTxQt7zHlz7M79FpiIRJQJtCJjltuiq33xZJEnAz4/BVlbbRIF2RiaUKhicxEGgbCqdJIfIIM5URVw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:24:06 +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.7762.020; Thu, 11 Jul 2024 11:24:06 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 12/16] bpf/crib: Introduce skb_data open-coded iterator kfuncs Date: Thu, 11 Jul 2024 12:19:34 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [kpVB+rpQiObCfYHj3B1EGxfjQH2TzM0w] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b337eac-ff48-4019-977f-08dca19bfa03 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: 4cUlS8i4ltiDBk1uHBVMkGbdoxuMq9jpNLQZ6Ku3MOSWnyZJEY5GUXgOjagKLY9UKVjUbX9cwDE4W5CYY95fL+mb/ggC6xSR6/VKPxyr4Djt/pma/ZraKvDuWtHV02dQSA4HBcwAlhb/xxHDFa+SwKLn+q9/MQACeiLJyq0DwzUdAwqsSPGPOzXjC7/64h6CpcGoUt4GlG2cE1FXW2jjdFsyLaKc3K+MvdZmpeWs6cZvbltjXTDT7cLb3d1/N/1XjVoOt8TQxUkFQUonw+j1kbK2tMq3oxs4Pk6jwPksKcEjT4P/JoLh9Chh0rbJpBLDAQlLCX9jjO215ujYFDOwAPM1y0g7RwFjt8uGzGE1XKCB2BfFlvvfgxFV+HP5jwXBjorh0pD3JR35iFRat859S5UFd13O1SAB10fcO/VnM81yuDxNz6twu03vsC1bEU08Y2y24higx85O4N55bpsTz0nZ+zTKPZEZMGFqPX+FWeNa7bh35870cg1g2xTFNhA3oBQK48UE5+uY9RDTeHNmjd+sab5AV8hdP2yQZAUtHqkY9cwAInIpm5p/m03ZDmrSX5CUkJku8BTNW7nhOpB+rI2bdMAU61mDiuumNDind5eZHB7TeRFuUqrJgvT2Wk7XuMNZyZTcMSU9b9z2mK5OIQ== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: F7LM5oCYxGMHhT2CscgwZxu1bmBZXPdRkdgbyHHSs9+Guf7G1tiqKX4Mccx4tUndXcgKkuEuIT8NzYVHm9C8oNdIIUfUKWV5U+zSvyoLGhnDAzCRZHHHMLd8gVJEAj9bIUkkWL7qJlmnzcfa5QTD6teJ6ex1I0vCVZgYwBo4jF+z+dJ9ylaBTurks42xXm4Z/+nFuUax7J52O0AJyMi5PZACZFyEQmqwFSyHsPaFd/xoIxF8YuLNkTuZtip+0EiFTamhB2HV4e97adbQVwzxzYeF/DNIKt0aHaUIxKABhoVei5kmttcH865l8FBsQQWC10oZ4swiKyZHYoSRSzDGfMuUqh13Ik6GC1oXwfIaQlouGTQhtc+eNzUxiwmYe3+AUNwxg291MQmzk1yuYT7i3pBZMZ37ZoDC8YI74B1woQUwE6eRwgdVrHBq4g4g+hEFwiQxxWJ6+aevyqevyP1e3ZeYa8wDc5v4xrDuQDWSHdPBGPA+TM6gfDLCfqrr3YLWoUQvSl5+2h5DDVjYIFmcFSFyZSjJqB2rLf3erOXtDr0AX5Lj2qdsbYAB3smdk0Z82W6n1hFGyGYImA4scjjoFc/vyvxJHd7U8zOBOVVYf/mtQ/+mrSrh7+AxduS4LEwUS8zgL6UfxWdX8L0QY/w1CMmy+zQKGo2DDB8S8128QxysI8H8F1SRWU0Bk7OThPbfwCFF1Luq7TSJ/WuYZGLejO1rh0zuZNg1nk/weZeq4borSiQYZCNorjmE0w5NL3gktuzxoxNax/JOhNdZAm2niTvZtqCLHiEC67lwkk0fz3hxPdD+b+5xk2jWiTmzWWUykLW0Qgn/SRrn3Nb8Ak4chU1Z73AMDV/HJAAkOSf7wLcK/xczidzzGug33BYs8SSUT85dsysQabXwPSwQ/Wt5Rc8oDKmT+e2B8WOM7qz1tU0xTADYYpuZeQh6/mu66zLL9V/GOT6j425EDO7Sm6+KZv2y2f+rVVTiWvj8k0PZwwSyOUd/Z+I6ffzWiQFc5bT3xPISWkUuixnACv2fsLZiCryeWvjWRZpLY+ih+2IU+/JQZu1MVWgHZS+aWJ/vgvxzgoHBFHCfKq9E9jB7RgsCjVt+NbqTYKg+OMqJ3LXXrvgS72NFu2q1ACEYrtGQssJtLf+xo1muyk4Uqi1TSWC4UF0Ox8SItk8gg7/uQ7AaNtvE28OXRnFbL1BMvPQ0HNkWziuyi8fjiysvaFcik5KQdE7GquMcgRoHKowKRxmvuWo= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b337eac-ff48-4019-977f-08dca19bfa03 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:24:06.8556 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730527 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AS8PR04CU009.outbound.protection.outlook.com (mail-westeuropeazolkn19011024.outbound.protection.outlook.com [52.103.33.24]) (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 85231158DC1; Thu, 11 Jul 2024 11:25:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.24 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697103; cv=fail; b=UiPqfmOlyAqoyNeq3QupYzHk2sc0PGkLcZXABICXOLHoIZJh6xdKFstqW8a2StzrCPux+1mEg2aKDeGJNiQ7AJPlaPsJ/ckZ2l5rGU9K198+y0qstQe2h4pRDybU5fvapfmmc4KLEp0fa5nzfkqbBSDa0NUFhHcrOGkmOrj0prM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697103; c=relaxed/simple; bh=VMtAYDhtgHGRgwlDQFX3tZNyn2Zt5lY5JD65kF0Nnb8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=k/wIoNuie8M6/CtxopaFcQciPV/D9Tu0yF0HaFAKkXbij9DrQ5+YFHCDXmwtPHfsLc4E3s7BoqJ5mIbZNL79Qo040qbjOFbz6WS1a+Mpvc/P0CUT2oVPjN9aFUYUbsZq40zrGphta+EXqY0XwG140gW7NaoqmCenHToB60dpUzc= 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=njhPh593; arc=fail smtp.client-ip=52.103.33.24 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="njhPh593" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AyrGysHvlbELkUz+wVQq5VMpd7NQg0O9/DU2TBX7djaPCaVUMeYfs9Ce9LkzIKYj85Ie4hpFe5ijzUZ0UGQlwUn6FrKhbj7ShIya8+H3lM/s5TE/kXwhR0H8YG1juXhi/qqDTgUNIbrl+tlY3wpyRRHgnh/IX1+lwMCQnTdO5+B3uywa3qwnNKTXOAiyBc1Z4IM/l2/O/LslpEZdsmQ49HB82F4OwGKLXdNMVSgRdIYoY6ZRKetd3iV+o6f+KA1Rq8WCCBRzQj+lODJJYLy2IAxTqoHIo0mj8BoWMuaZ54EwwXzwDalSYGgQ1pvpDSAFzfeEUvJ0YmFtPb7pKW2TFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=XDvo7a8TCFRma1qL5wdKXBxZOFBrzAk1jDuve0w04nxcNAAnm0S+FHNkI40kHxjQlAfF1I7wi5+bcfo1mr0bfDaRvH+mkUtDI1t6yraNmhwYoKzIlmQ4MhDzGyeQliFUpQMBPdlt+2GEV16DWoq81Cx//Iu9rD5TKbk91+YTxh7rXSfSwYnsUZcoNbvZzf7TLu7+wKjAyUpmtNgL+HhAXHc2nXXNQKCHRGtzmbnp4Zp9apIRhNKxOhYiHL51pW67F7pCIaeLElQhhHHA4P5d3qAUW37HabJL2tmad42A7hwI91iZ95JAChT4oG3Z1pZvQcnszssukToav3sGHiC8Fg== 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=njhPh593qKC18l0Ah/oo6Uk9DDe0l1pmNKa/yPhCVkbKbtcHiDnn7oJfqGOJX6TiBcoVfwB0AUntA0fGuy2COKI04pWnTQ/6T6MjqKfNLBlqbtFbVJWF91bDrDBMxll+J9lJkclnZPB7n9xKMsGACGDBOe7K2XNFo1odPQOnVAmId2snNANYOFyAvc600jrhUgpCe3fDQQJEHlTX2UqLK9eeqbnl96ujK92o9PaY1rClxD9WTsqwsEOEdPqNjPh90iea8bPB92jIp3PdmEbvyWu4o3IExew0EBxcJy0+QivFWmB+JeWFKSlr4bzF6lSO12xrZ30uOMa2bypbacISZg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:24:58 +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.7762.020; Thu, 11 Jul 2024 11:24:58 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 13/16] bpf/crib: Add CRIB kfuncs for restoring data in skb Date: Thu, 11 Jul 2024 12:19:35 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [soSlfipkRHSsXhA3hStu1t+WuiccJ1X+] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: df3d3151-6506-44a3-625e-08dca19c18bf X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: n2AutJ+yvIOb5DeD/v2g6veqMo/AC/zpdxWZsXvQhhCAeauiDB5EyU0Uho9GrHNDOPDHT9OuO4PhzdGGK8Qv4E8Bl3Pknkn4E1/huUzwkjTC4gYjFaqsfuQCJsOfM2+m2Hz+/YHe34C//Rvu239tfx0vEB0yJ+5dpsxWyV+BQDmzN+iWoEw/n5zdD5qbFUFiOZK/boXjB+rn+vPFEQVt9QPA7zbskFvtcVxmf22JmIhuPPkbFuRNqRJbG2s8pGweh03MftwIzUpqJreSDHCz8niuTn6Nxj7wRdlZrzvUzNOAHPw9QRTmg04GJKuYDlVxcBvSlM5dme5zavvTNGtVRjmFG+xCeY/PO1laiyKAAc+yyNNLWaks8ZQk0uVKiK0AoNpwgQy90H+fXd0Ai51lf9UPzBsPHNtqFAKtyPCXJYRPvFsQ7hWkTnQqqW9XdUKT5y3DDBerbFkjHDoU47YkcUN+3OYbcr6P3ud6oKocRzHdFmOJjglQW4oGpWDeTAKTW0L1zvenM/7otoYKCq1VBwNmdrqkvvFhXeWY6LlPCmJ+RZga4/ujMJqa8KP9I9UHWqPgyqlD6CdLJzwaR1q3ujYOGk3bn93jXXs2kIoWuaLXYK0kfF7oiV6b1huWQBO3ygRC0MiNrpJ+pzZxrPIB9Q== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PpDz9ztaWGTlCb1X48Fp4vjjLLPm6CxAJwXjUEy67v22psWV6LEOk2ICPSM5rjuKQhU248CxaCNdnpZGw9gCaTPDbQJJk7ZiBXTI9wjQjLKa1Isg5TVtOBCoJlw4fBsC2EuM+oM2xGaAnaEpgyWSyLG9yzOcJqOzDQh9v8+KiSW7FioePQqfvrA5TdfGjVNRfK0vRvP2NF97NDC+8+vmyXY3VpckkstD/0pv3W/3bFHSenMo/OOPBd03yPqR32py011njLgjgDj7lu0KSRik8aNwD85yOGzqynQ16WwowqSxAwZMo4LVYsp/y9e8tF+v5k/nt8eABiP9QWP5LA66eCo3s8/MGqcym/YCatsYDPysPejrQcYXbVQ7g6fkUN3u4/AAyp0rC0WoCgcXIaTeGnNW5yhsx1V+3hfdROUao7qu0NdzNIzapWLeUA7+wJYZFF6E5vlNH8nWl5A/gu0p8X4o9ZhQjLiSMyzccXfvjRjaJxX6FUiNEXO1OjIRV+fyhkdWa7CDQtjFGvKZJEEam8V8spC2OgObJvrVHAt4sCUblP3b9CPC7hqw+Mo9U/EI98+7DDf5QkXWsa+d1VfBYb3YL66rOxsmknhPcrqT7h3NtFDyG3LSXvCuc0rCpdkoqt42fdmneoRXOQmdZCRmFIDcVKohyG46zYSAA9zRHISI28LNmtpGanr8WS7LVffY7Vz+dx21xseBn338hkJKTflV5hVGJzr9XdInF11Q0qgCpXzbC45ECYGAI0c2aEewSsELsHXCtcT6OkjHIXB6RBFWoLD2udLAaS6+EbjSZ0RokP1QdZ1dKhYdSISr0PgkiFO6IMc8/vGgiXhT3daL1jFNlKSaJldWBW/vDvFTJ/c1z/UjyZqD52fDz7vQ/dtLhdIANtGlbjQ2LsikLdT2aLbFXvm4jX1VPamOAz/9XssfwLMV30KjdBjlIFK67ZwW6KRBBoEmE+KuQqw2ri/UZi/kWLHaZY13JlZFAPfHQoRV6XvY1oZ//NP3FPEnmy3RzgFAhfimvcSUL7O+Y+a6VRn4bCOhMcBT3rq+YGiRSDLiPvI2n143nKgtU9rWuq4qrirIIgSbFuzJKmUiP2prwjr/s6tiEro2BK91faj7JYuHlgw6z9SgbntCByMt5ZEujmO+BOxoOecgVruuNAkaYXQwJFrEdnwfpJSsT6Z2mcLQ1Ts5clO7C9UL9nDuzaH5GY/k28qshBO3G5mUpg1z/RG2RWmzfh7Fqy1poVqJOFg= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: df3d3151-6506-44a3-625e-08dca19c18bf X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:24:58.4361 (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: GV1PR03MB10233 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 Thu Jul 11 11:19:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730528 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010013.outbound.protection.outlook.com [52.103.33.13]) (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 3B672158DC4; Thu, 11 Jul 2024 11:25:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.13 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697110; cv=fail; b=ky3CJfKHV5d+RwvSYxOSxzpLTrrNbxTX+dq9HpXBFvly7hunYMPFNJlTIW93Amc2QIrF0F66PEvuacV4awxaqpyWJcMmriyPSG1rabCHSS257tnpFRtoufa9gMTNx2Qj7X6m/yn38yrLv22fJhvhztVvNcuBT+Iu6BeWanBdgv8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697110; 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=TWCsQnRoG3XSlJugiV1mt0sYMwSCym4gNXIPkJvxDpBD+1HWgnzgavLT6QQHihp5L/8UWbh+GbA7cAzdaDRwM2sQt9G8jJqWhl+OudKsRots/w2OMi1fg7CRHx/3xvE7IvqtBmWBqHOfyjxwTZa317Dy7JXBSvliXt1SYZj8R6g= 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=gYzof8Yv; arc=fail smtp.client-ip=52.103.33.13 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="gYzof8Yv" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=fg6J/5bVAn3kG7XDEWoUJRWSwgylPAsoPsWtilyUPz68AzRGRmMtO1i48hBvo0I1ifMFQbwXNAs3CjI8RnHAYa3K1/2ea3xAoHOL1FHo3VXd7G3LPf1hJX5mVqoiAzs0YCseXzaU2XOuCyJLdvDbnqwaYEQxgR9/sPx8SC6OKyYzcFyiDbczLOka+v/1hHYDw31REKLJU+NU6NZFIflWRWlNfi2DvP05esaIb0rolEQOPNNi+5/1wLLNrWb3BPKNoXG9OxI4E5aviANL05F19ospfTU86o35KcQEP44TVfJCMNk/R4ViBmYG3E6+1jh1PhaFwWWkrF+lGHgHxS8rFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=h43hJ9a/9nNGi3dzMZKE0XL1DFUMJKMq6f+lwFNHDV8YLXuwo2GgIPRqhxpOX2R0piVWuSM2uh33PnPs+9h5AZlbH+sSziJ39EAkx7c9Uhhhrv6PHFmI0mEtssHt4Hi6S/HaV8pfp62wLnI5LHP9EQEyYjsEpI+XYp1Drm9Qkk5TddKyjC6mSSn7US9PJ+3xNprnN3GNi/YidfUnvrX7do8GAiirPE9VodSYi5uPhPVnil8FHydwEbFieG8HnvP4Fl2bbU2ao2gij5u03mzNDy8hfBdyC2CY9M9Rkt4uulOuGoCMHPdALJd3lImiKrydiEpkn+6vlpYam10lwr84Iw== 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=gYzof8YvFn7qQInC2CpyK3rfE+etfHSz5xRSUnJNCfTRPjyaAYGrf/l/acsFsbvBQ/CzhhyCTsxsYvP21mmfs/NIOjiG4mGCT0nkhOQ7FtR0a3oOcTOGiO0s71H8a5QpSjAuvGLfYlBo31WpLTEIIzw152VkfSIbssqv3hy0UiBrl7nzLoY98nEKLDWUycwHll41L7mJuJmtwhK3E00wSfvK5I9BBtwc9EYFaKSeh6Q5j71ur6tHGgteClfU+wgHMRpe02/XdvzYVBL79Y4wWd+HcQ6jwbNRW4hI1+RjlWeX89CXgqIZJhBj3AA16Q5CXcNHDnQ8TLPng1kP1Zi2qg== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:25: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.7762.020; Thu, 11 Jul 2024 11:25: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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 14/16] selftests/crib: Add test for getting basic information of the process Date: Thu, 11 Jul 2024 12:19:36 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [jo+TjgD5Z2+CX5dOVhjTzthD63bkirpe] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 8a179879-2363-461a-8b12-08dca19c1c93 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: N7otg68LaWqHuEUJ+g+dDoUmPc0iN2z8CYRuZ6kNa9O70t3WA9aF7csgsPteAndSJ5ZQTQz5BsZs9OYLqzle/CyEYOe2pfRwIqiC1j0MpFdGZZGSa2heQ+GyxgXrFoPfroSM8QOwCqeBa3F/FP3mmOxuWWMQMkD+hb/+izr81gcoT7fGwmAD40AmIKYOUNR10V1XyiYERVHXJdp5PARDh5xlYO8XKUtrG7puJ/0XWWpW2kUUI/SOGpOq9r0J2vZhqjl72x7F3L+xtpR6AkIRzcK9DoIJXsAWLFs3okQC0xBSZ8UCp7xdWnpWSw8nvwKJT/ii74+zX8VuiTeiyw+gkr5L/LC2L4OUxpAiA8tejjmgR1LSl9jK1H+L9ZmqpuQkcPnirE0fqW47yGOFmRrPDzx7xXy9tec3hk8nY49ZpCvYQReIO1V7NqbaQYBUwAF8v97XCzrAMSyibH389+Ubct0jt0dyPv/D3w9FP5fX0hqesub0IaGQ1+8z0SWdZ9ieoCBfiYd7SXlUD76kuPDuJAOvj9nueMgVzGFOj0DdQWjKLr4iMBAu0smS/0OfVEKcwLif8VrgxESBf0sYRZruNu7ED3KXnrYTsPoZGSjf7zNUuaNVo+Gf/lk/SpSdL+x7IfYS5J1Z/D+AMQ06PEZ51w== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xQREcgHzUUy6RmXL5NkYxlgzpGDCKlEbAPLuwchH592j9kr8Zlq8Z+noL9665d2CRMuhKKoYTU1nWgdkK5lEiS7G/6fyMrv9n7cT1H+ozXv6nw675DmPqTnkS5xSTwoeEspuJQFTRyM2EYBH9dkExcPjiFcLoZDkuziB0WpvFmtgYZ2ScX38/tElxgV/0/IflTM/WPB4SrqTE2V8Pzgx+SJ7+a/fvzSXr62g58AtuFQDTHRLqvfqvxpr1QZ9Lhs+XehwqTGpKaX+u/1jAW6JnNRQGuzu3Fc+1twlCgsF4bPBGPX90r+Olj+5YbmmyzDJsVuZmWjuMUythX38MIFDmB/JQaZKG2rYRj5IM2G1QiqhzvO0Fay8dWYx4i0B38o/YasCXjPa1hAzG3yxlYnM3/+cRtQ6sEoO4fplfqBtjJuwXJBPTpM/kQ/H0w7klttzTxZHHTqQcUopMNzeUjpaL6WIOEwGiQRa7WnxYq7BsTXbSA9cZ8uoJFsKb0XWlEnvDDM9vmYAIXEfsf3uETEOh0GIzlu7F0MYJhw861Nmr+DJWsghGXfcuOSrbeoqvbFQrehzjYgDpsSBPOPDyDT0mbTmGtM5ems3JrsF0Qe9fgset2gpObrSV+T8Nd3N9UIf/ydeO9mox8jhyCYaIMk15TKVvQFWwKx+9yM9qLEn7Xe0ebHcfBgwHAjIf4QHf0KcBQm+hdwC4wBhq9leboEf+iO0z2m1gAjyfEBLyTvw91q/iODzw30ejV3blc3/XKO7/qQRZe5//oyZo8ry1SZs7uoM7F/CpqEDdusNlqqh33DV7Psp+7T6prPHRHMPq1fB6xHfHgg2dalPV2D9p234zgenLHr/ZP4+kVh1iOfLqIpJ1czCbnLgCUQ2rAQNl2vd21GHWYpdHG2/2SrGiS+OcGFsFX5WECs5sKJdriyWhirXu7Sedm4Zx/Uhuh6SfTiEAW5oIL8Z/gIt5H9W/Hc/DdTHmHi1h+DP9FtUw+/SQgEGwlelgPEpbpGgWs9+UK8A/IRdhm3Mea2fG8V4hybCHmwunk/nW0SGcdNqMGlKm8jAMCC9wf2tHfpw5+7N1eFP64JDg16Al/Xk8DEL7j+8PtkFee/Nx5Bizygyv99CUxnvD75EDp0/weQh2KdItPPGzik3WLsHGjt20C+I6zVHz4azd7jtR1bWhmDEe6+fgdFZpz4l8e76lbf1QlN9fXJp9q3TmxcKxw7zkvCftu+qcxEJ1RM/lSG2MbNIIOkqTSU= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8a179879-2363-461a-8b12-08dca19c1c93 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:25:05.0428 (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: GV1PR03MB10233 X-Patchwork-Delegate: bpf@iogearbox.net 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 Thu Jul 11 11:19:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730529 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010015.outbound.protection.outlook.com [52.103.33.15]) (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 EC77F161900; Thu, 11 Jul 2024 11:25:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.15 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697119; cv=fail; b=MY82SBnqnCOcOQ9o3E4xSInTgEI8nQ1qWuANsuNtyxTmD+Uj05zDJaQ6bcOaBu0DZNj893M4t9U54oYOH6GWcT+gM5yIpTcMkfrpfj2BiyJXxJP9ktCGIfQi2ojZqGGjMumXjf+XuJjdyfDrgJMcsl89ygUjQEp97Ft7slk02cI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697119; c=relaxed/simple; bh=5TW4kvWjzIu1SEuTl/snc51X+iT54VWLIrNbBmuVuqc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ATQqCUMK2jx8Zg2J9VDsGE0QF4Qzf4EnbBJTbNIkxcx4emJM7rMiP4snxZoRqXP/sMsn9C9kQ1qjNOjzku2lt68XZ9lGb1rE1NfbfgFrIyd98dqbry29jgmUdKUxNBU/xLb80IiEzdOpxdOlDZ4yn822J1TYsp1jGj9D27BdDSU= 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=bQPzEOPH; arc=fail smtp.client-ip=52.103.33.15 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="bQPzEOPH" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=EUS32zmaURcGHOAWnJ1EydOF6KJSBFf+nYl2s9O4rbPIDzfdErjthZarOAu2wtV+RR6PgU0S+HY41Yo6Lqw8ILNWpoluRxXFB5wrX9+yJYcjseWEsQh+ZNwwQFrz9cnZ9rkGsSARQFSdALfobWbiNQluUO+E9XMdKZZmnGvn6ij4kXFhwuXPygfF5Bz15DtOtZUuxsP3ZEFpY9C5zRWokEBvMUBJlpDbByn2/BlM4j9MkO9gGN3mexhfZZl53O+Cng54fZNxOZNvcoEErli3DXOSY3hSmnixKUmR3ybyGwPn6mSDK4UbmoSbhlcT4koHc9mZj4Kz2uhWQE8p6puKJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=C7etjIDndfYrSl4Hp/OV0Qgy0vVphzrEDUmB3h5iGNp93q96OyMZETdnsk7k5F7Kj6cz1umaTzR8lII63FCPltkCZK0NvhfnFmDllW2oIRQu5nKrhNScPGy/Q4IHa+xthYrieDwpFT/UIaxjwh2zaDWmllixIlA/KyNZmV3pwbuGfaNri800DdAa53aufjG+b1sYLfkTp1ot0Me5ybzkca1B6JaUW2p8Jg40aevErVX598EFJxYmuW57kKq0+AF1VEJJkjq4IVXtSxio3C8O2DRXu3USBJxKXFO4k9LjD4bNQM11SXd6rQIXCoFc6JMFphlZPX8pzi3wjGTQmQWqqA== 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=bQPzEOPHbwZS91MMnfrAwoUhO2WwU07NWJo8IuhERIKdcbzPkCcz4LCk6g6FUYd0fi8wLxJKg5GgTjdmfDEdBEREgVRniEUjDQ/1Us+DvCWspe6GtVDkXbVH9IDXEjCQlT4ABkOl0aNDeFLH+rUfiU6IXEEM3GSOKzEQklZrT0N7SjIGPrPBvD59HY0uwDizGuHXWhj6qUPml2nIUG40K6CztWjH1XXmHie3hdvswYLDHaibKt8SnuZ3yW5uCfOgUow2ztuiPkEFGYL1z6a++LM5KjFZC58wu7hTGtT4ak7/cvRZGfmkxP4mT/GtrQbZ+cSnOAwKBB7pH/onL8fzSQ== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by GV1PR03MB10233.eurprd03.prod.outlook.com (2603:10a6:150:166::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7741.35; Thu, 11 Jul 2024 11:25:13 +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.7762.020; Thu, 11 Jul 2024 11:25:13 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 15/16] selftests/crib: Add test for getting all socket information of the process Date: Thu, 11 Jul 2024 12:19:37 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [clfD0f5IaX9tvaDjE1+845Njg5G9FIOh] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|GV1PR03MB10233:EE_ X-MS-Office365-Filtering-Correlation-Id: 1243b783-3cd3-45da-e71f-08dca19c21d3 X-Microsoft-Antispam: BCL:0;ARA:14566002|461199028|19110799003|8060799006|440099028|3412199025; X-Microsoft-Antispam-Message-Info: k9q4ZndVrR5VMvfpSpc95kuuEKxBvWaQ/JuBej7p01MPTcBnxPHW86KIQ+HgY+Ppd7e1CzLlFu+iMvA/aN6gl++Kw7uOz2e0+0VD6qVkCGnYZE8QyzQozyK3QDp4PiC7EtwWWcLjojK5FvWvkb/OlVU5WUL2bZHDpkR+6+2lqatx4QG61ZBKZ1MUbL089JWxG/rGJ0888SOuJ2RKDAMGpQ2muk3lyrO9VfxjIVwotVIA730FBJig/qUHe7cLRTZm3dIPY+jXnaWgjl457AtH1LznHIrp6hUfM7g/O8E9BZTFHV33cXf9s7n5jZVasU66MP0QVbIrgg6iS2OhDHMg1kBKBJxfsmKEtWSRYQtf27CKaI/0B5YQtB2IHRM9ZCq0+eANd8lP8Mru6jPfYLP2wzLcWUr7DZc1KB/7qe8KWQ3G+vxuuyG2dHPHPenk/w9BOiuORViWfUROW0NskiRUWWpX39dDIT645niZvl6n/C4wL1bTjAEUa+9KPOht/CHtzPzwuvyx/qHjcNUEjUkWI4kSM4iV9Nlj+GS9wDRTAmkvhLYmc+CTuNaQMEkVenW6xrjn4l4BvnDCCen33HSDbPwfJQB1CqWdawIfp8SANxs8WAVyNu7H2TmzdJ3MZ1TgkhwbPedxMxUxaYIp91qKqg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: k3Qv5vx92xRk3L2V/turAX4hO4g3h3ALRwekeL3t0ObjHBlFvP5K9RGApLzuBnaiM0ph1oWKDXiHVkyILUCiIC+WVZ2QgU0726AX8AwLsxIzVlYIm+7XS1Ftsy1oRP/GdHtsbPibcDMSUuGWb4xdTcKIFdbYpAOOHCmQlqdC01V73j8QwRZjS9R5aRg4qUhe5DlK7Z63C05brx+V/OO1iyqp+TeWVM0kEA04bhdOja0o3keQXeaoelRLi10i4+NwWMtfGyRtYkxpmrWac7G90lU5/r65m3dGG37uNAkjv0RFg5DPmvxs4skzrLk7QpPM2q8ZhgneYPfP/Mq2Im7QZWd8AHd6ZlLy830qbd7wVIa1UtGoptZ7/lYHWwpkp/PZGPFSevJjPvckqKE3jKAwlZ0XJahHOItwgmEvJZMchefjozu9yDcW7T4GcD90hv5dgXytFmxM8Nh0uqyS129S0klKzcrvLCH6cmQyTPuUgVmAPzplvmujNGNIdPEvYfNgL/AtQR3/c+H6s8N6QTHFut1Mpoctuodd74cYeO7/J9lA34eDANmd0P2K6++LaZQucvy+v+WdURh7gJk0ByoxJQQ+yzRAxXjUkYcVLVz5/h7s5TmKLYJpnJowTeUjz5TMdFqLIRmcwf3SCoFh2mnNNH3mPi5KnJXqVMMjZHmGCrnlB+/1Msa6aXl8Pc3xjebMqVc+qPN/7C3JqVkprYFjO5xYxAw5DuCfaeYDShRh04AirDdHaCN6mTONfMjOfa66P+1DohfLxneCZybsjzMiz5UsvEgBa+LJcxumvDfqW5cgk5yyO7tXBRd1hUDHvfjoH4NyhjAcVXZZqAPiDR8NuIgnElbLIvJjmvVWFnXxQ2BKXMboV21C4hU16e55vqtfggQeBsunwOA9EL42fQDLCLZBoLRW8A4N52YQDD8/7x6fuZyBsSv5tzL4e1839z3CIeXVx90AFaQEwUFrhD+46F0guW3DEmDCONNSo8Qnd/uXiF1goHwQVtODa3SwaM+raQey1vEEiEcOY17Ez3FpHmhkZFsMlAffiqJlzR2PEMAvcocXbwJaO+Vo2vIQsWKx1xEFVN8H8ziUds2Lfas7v95s3Pai7Dy9dKRMD2OZCqOhjyIlwCMjzljt8S8iHMt6SA8qBpp1R9QGbR94IsYJ8i472PsHpTdLPrCMomcr0nwz3d3GFJvPcl2mJj8SSAzLqCYySq4e90Vva7K9tt2itl2JpBBfukOpXUWI1bz441s= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1243b783-3cd3-45da-e71f-08dca19c21d3 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:25:13.8460 (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: GV1PR03MB10233 X-Patchwork-Delegate: bpf@iogearbox.net 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 Thu Jul 11 11:19:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juntong Deng X-Patchwork-Id: 13730530 X-Patchwork-Delegate: bpf@iogearbox.net Received: from AM0PR83CU005.outbound.protection.outlook.com (mail-westeuropeazolkn19010004.outbound.protection.outlook.com [52.103.33.4]) (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 23BF116E88F; Thu, 11 Jul 2024 11:25:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.33.4 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697126; cv=fail; b=JD/PeN+bN0EIEFSGz1lkxlfnnG1TP4zwyZcZzIQDQ9KuF5dDCOuuy8v2IHrL822IXOQFHPrbkLT9a4zjqqYWXsddpEWX6lXAjgthn4bq1PUoPbndo99pxHqT4lMGFGGGhRUH3HaLNcPXBuO4iTPZf204EDsbZ59tewZdOooByeo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720697126; c=relaxed/simple; bh=M3SGqNnbVRg01VCLB+2RQpAxbl8Mi6TvFhviVlREHDw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=vA+riBewQDrFksxRYiJAzejOa47e/Ny3zDgv7/t9dWKH35x5nBJ30vpMoWsWJvg3Q5oNhpltKeTnTE6h5lSzG2qSq6je974Xd1eP82xZwqfT0Loe4Gravs54ttEG2umuRIp9t/+uasNuiypyua4c7QGO9SFPnffAwbQZjTLVg1g= 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=oew9SeHb; arc=fail smtp.client-ip=52.103.33.4 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="oew9SeHb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=oNVXn05ElnHhfCYU+cYJm5SId8BjtAvqw6T1x2d54AXsjP1lcjZ8QPjrvGKFmtz028VOWB1YySlzHqNnB7RnUuuHlGcBwfmi7Gox0vEYQpKlArzCKYZ0k9+TkQTxtTAT1SWAwCDGoRc7rj/L79v+8iS8wLj0iDP0Unj5Gk6jG/BQfEUCJ6y+jAXo+AUcnE+39FFmedCczhylSYs88p7AHZo2bjLh5CqIHHC+euOjrOL1GPDhnKe/E99gx5BJJZFUspPNkSwzNrcRLNvSDMIDsZ7n15f2jZRyIqShfne8ZXeCNpgDTY2GvG+7dEJ8zIlKDkfrF9XX/CdDnbuAzytiJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=KoEcXzk9msRLB/h04EAa0Wpzx34C3qd6nuNUKca+8OJQx+eIoZ08dH/1ZyIT5HL0anMM24yZ7s68RdfgaFgzQi0qpDtTkEdL4H7kIVcxE/l/lbyRA9cEaI5J9JCkxjgy3okFoni2Y7WBULqMHN1FWC0pUVDhTIYk1Q8m0V+C5/HtNzcR6Tiv+GmTe7dZz/nB2B5XyRBGvOGOHY4Knqyt0X3hKSyUmnbiAIRTYeGke7Q23vxXYmPAaIUVm8JNFmAdyKw9+QkhmzC+POklDFHs1gY38Ket0/GfIdDFIPwqnIv7Gnk0Xo/5O96XRBVibWXpLddeHNVlVPu6e9A9P1CeaA== 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=oew9SeHbv3CWaXgYEHtLTOOlrRmG7KPgXnVuyI9AgwZW/HZA4Ls0fM2hmnZyXNt06vAMAMRyUET/z+cA0BukSNV/P3aoV+f/IcsxuCOE1YJW/cvBJPoTiwCVWTPAsc6fuAUjSAtwNTuWsn3xvqmGxfUso2AgFHkFfQWCDxbN1jMnGhtBZiZxqVRUlSIRz4DAOwsfQVoAQnGVMqqMS+Nfx9WrTaZYFjNVX0kk1jgJezuESLCZxXn+K33Voh2QEs0LDzP+ERmKwfcy/cqrlEfPYQLKIlXuJA4kc5Vrj9Hmgl9fAoES/6tFxegozDlnx8puIjBgoBBUgd1DtisKZVidmw== Received: from AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) by AM9PR03MB7330.eurprd03.prod.outlook.com (2603:10a6:20b:267::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7762.19; Thu, 11 Jul 2024 11:25:20 +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.7762.020; Thu, 11 Jul 2024 11:25:20 +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, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH bpf-next RESEND 16/16] selftests/crib: Add test for dumping/restoring UDP socket packets Date: Thu, 11 Jul 2024 12:19:38 +0100 Message-ID: X-Mailer: git-send-email 2.39.2 In-Reply-To: References: X-TMN: [/UiW23+/6gfpqI0cGAL4FnIMUeFZnk2y] X-ClientProxiedBy: SI2P153CA0031.APCP153.PROD.OUTLOOK.COM (2603:1096:4:190::7) To AM6PR03MB5848.eurprd03.prod.outlook.com (2603:10a6:20b:e4::10) X-Microsoft-Original-Message-ID: <20240711111938.11722-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_|AM9PR03MB7330:EE_ X-MS-Office365-Filtering-Correlation-Id: 939c9d46-4c11-4989-8fd7-08dca19c25b9 X-Microsoft-Antispam: BCL:0;ARA:14566002|8060799006|461199028|19110799003|440099028|3412199025; X-Microsoft-Antispam-Message-Info: r2ZaEcEyNRwkLaXTTHD10l8YJfU0RzWi02UvnoKW4siK41sA5QrEKrS3u2UHTLI0yC4r3ngWIisZ/jBVmw9h5/6oN241sAuHdlPCr4kcs8F+YHd68MAyfJG53qR/9eEcVZg4ZYoj9T60TAObgOfnpZO1DjcHSDyV7T4SvA8bkJIf8X5IKb3Oo5TpHgQr1cXNCDSOfyJeLfivlk422Mk0fj4LP/uWyRfbzecUQtxcnbziczJQmANdyqu/NUxHTDo6nqxdKr5p69sBduPyufUojxexE+LwJx2NBr1SawNT50FR1stAc89Kwu1CJdl8Hh2Eq5o0ZIhuCVjB9UCWdP/S51EhvQbeX+NucE4ecHqpvXET7YAW+5O3BiyyqodKjM2yaMOupvGPFXmLQtJww/hk9gUUwlxxEtRPKodE8E94W1o1yFJ9DZKE1DfnEEH2yLbqwsHsHeiavfEVYVvYwS6jMsQVSrS2y3MxD4+m/n1H0gbhSLCgLxIQywqpGfFm6usq4FY6nDYi+k5aC5549ouNKL2+2UThYenBYYuseV5G0Gitz80HasCgXhvyl9hlQTGa+Pvy7yCfGaRC/kDaJGntFrnDgSGAXMpBSJ1h1tiEJS8JyZ5jrwL357iDY1x0iGrFZFVKkKkyfqNbUUb7q4T7Tg== X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: eUknn9R8OA5qVDj9ehnGZysxp+3HtAHJVDiZGtozs64k+MqklvhaG8fo9j4CVgCtyzg6F/KLILrYepDE8BpAft4Q+5spt7rwnwTAj2OjtUZRRzXLR1ecGH6MUrr+iw8uomms/g/xxAguDqDbNL3TYCfTvJZI+dymAxbOM5NjQKQYxGzXecEaqSnwPGsPQCvkjrKe/yJTx/iVAevKIV3FdmBtcT+RIoGsWYJa2Of7Q1cq7sBRnQu6o6OjwQ+Not9hhITrrQTEX3M/YyNvg1Ge5LzWy6NzT/801WEQVD0h4bxX4rnATa/KabirrfWOCDENMT80cvkFbAw1meYvN65oDoV/kd/yBo7/grdO5k9mTU9sTlr9eX3swTnvtZf65d/jtrt5h6bQJXXCMIREOuYYTwVeZ3thLD134ffTmLOSoSh4OmrEZiaB1kB1peyAdLl6HjqDUwiixAgOfE89sYWOR9ltyLwl0qQObvVYi9nomlDJJCEi9pISOOcEl84jPq0kD3itsp3VkR9nrgUlzZUySx7IoyoS36i4pMSYRK3rKxumZ88xGXNDj9kD9TqJgn+9cFfOXbgTqZDhhs32UI2lhPaMis4Vjl1fGCOT5GnhL1zdDAaXXFFoDhWJRlguafSgPAqjk+HOGu/pSoKuGXBv4EdGuV71wfqTC89c4fBJxhDwBw/+3pR+6auNo8BwGEqLCNwT1esvJQbbKPhZ57deIr8ZmnQm/nKnMUyZ3x8d+ko8Pyk+LyTMpS7yH3Z2Ap3+3y4ayQPP4ZsuZJDVSvKTMcrhw73E5uAhrjsxMR70L/3EF5tm4TCdke+VbO5B9kQh90vxY0GdtChBhlAbYzBbGjUkoJao2xUNI4TULISSbBYCmJB4TzQt5sxFxnvgjXMJceybT3BoaZl6d/mIugoAc5W+dhLjU66SvOS9JVtcbSMHRkJ806DiMHidsxgxbGEyfz3B7yzaKTjVCoTEw93xb3g7pwgBNwE8sRhY738mtZOgll20N6S7emiY2W8d4RKl3nvpr25mswMahvKIrK8ox3Tdamuc8MKq1DLPEUTnLoqN+hp4+FxUkM77ZffdpffbKCKJVFsIuzqkns9kP6gt7S+r3L4k0XMgzEcibLSRQN5qC0jZp8Q/HLmclZv8vxKOh+LmMAvJD0QoILhJKnbib3IPRA18ptp4z1J0AsI8dGL3albdqtdyRYCRbz6HD8tPbPRODi+7uG9dwJxGNyJz8rH7SdMJYPyZRouvME5ewEc= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 939c9d46-4c11-4989-8fd7-08dca19c25b9 X-MS-Exchange-CrossTenant-AuthSource: AM6PR03MB5848.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jul 2024 11:25:20.4192 (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: AM9PR03MB7330 X-Patchwork-Delegate: bpf@iogearbox.net 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 */