From nobody Fri Oct 3 01:10:47 2025 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2077.outbound.protection.outlook.com [40.107.243.77]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ACC12765DD; Tue, 9 Sep 2025 08:53:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.243.77 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757408022; cv=fail; b=s3VOYQeNHGGfA39+YFZuPqHFTsgoZ3BPMt+RWfzoS/sZ/1YnkkBt84ZOrKumlagrkURBjOfreyv+sVcfEby4ER/y0oMmTnQYJJaB5USmhe+AnHWRCypPqInSjMVU1AprQ+RPwEoLneVK2lqAU6ShrqmWxq9zHEQyybevuN6nECA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757408022; c=relaxed/simple; bh=aL97GVxM//EIu5XcWNXoEz5L11oBOT8ulClsSSRe1Xc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=eLvzgRtXQIFds6ml14AsUpC5iv/dBaWjh8nUXgyV61t6KjGW1AJSrHeVCMC19cmEt8lZeYjZnaw7v3AVx0uXCexbT9DYQdZLQkiUW5ZM2n8mlkV1jGqxt4ze8bntH+nbcrTT2gikdF0eV0D7zz7shql47BELFFxRFdQiOW1VRSg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=k7+NoyFG; arc=fail smtp.client-ip=40.107.243.77 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="k7+NoyFG" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=NT+9KIv1PJKRxKcDScD+8dk+Ac9fGSt8bC1/G/9WpGvOq6ta70XeT8tWavCU4ov4Q9YxKG8LqrT6D0t63hfecoazuuDZidb7NmSkY5RR1Sgk2bsFhQ+Z3Wk0tJizfTBf64G+j9m46T1vuMyBilgHn+BCB8sShqCup3onYgRN/g2RLyKY9GwvKgMaGLLjE1lDjDD/Rb0P5X/5DBM0ijv2NUi/Kg3C3kWhyua7P02OwFf4UzGiqJIfl001NnGU64ZKhExK4CSD7s+k59sLT+0Ql6bj5u3O52vL3JvU0mgAoatkFVR/a7b5b9c/r9z9D4vmeLJgyXl6y23mnfP2abv3BA== 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=yBQBw2IiweZKwkwCJ4XjCwgaNzKFpnWNkz2w1FLLSgk=; b=PSXLX+fL/xCWsbQOpx1NDU9H4ILBEJKN7VFDta+aMwn4nRilG7j/YgEPcHDhXcVuzLf8ShlXlP10SfMylagZmC5Xq5PX/lO/e3cUtCs3NBrHWq8+FowUGKGZr6hVipInKQgCE1ghN08lEtK/hFD6BAv+Ld9LqGfWo1cf0NJ8z6+6XJFV61w7HSFIK5c+azbrBIE7mSizwWsWTZy9CD/UrbnUSQf1FjP6SQPUNeGEW4AKnEbsyEga/QnOob0RoFPMceXSeFehyIukIs2JAv2Nssm2jIT7tYpbrF2j803l7VL3nkUyKLl8XDI+SqUGt1kUV6Ywh1qPnkEJdDUAcfgUlA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=davemloft.net smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yBQBw2IiweZKwkwCJ4XjCwgaNzKFpnWNkz2w1FLLSgk=; b=k7+NoyFGw/l1TBXLDUmigYyZTcNB3gbRXdpmC2gOpoKoTyCWFcPRD320ohEXevx6H8pQ7fO8tcqzDG1awf3Gy0ue1R591kYbMyz2KrlfnoN6/Bz6+hnSHPCJYeCunV4G/3j1182AuNO/EmQ8Tp/IlRCVjsVXEkPxi0NcN+DSt6oLgfrarrz8qEOCIEEcEzw6appsFEVaZ0sNdraFvC/8DaIpK94kZJ7gSJAS20ndYF/5PF6lR515gNudCb8dd5XR64XTsQIcyLFcWEba6n/vNHPLxhsQOQJ/d3VyAXfAt7OzFJ/3GRBaoVHqHDhVHNsEX9AsAGUQQf6kp2535yQ+Pw== Received: from BN9P223CA0007.NAMP223.PROD.OUTLOOK.COM (2603:10b6:408:10b::12) by DM4PR12MB7574.namprd12.prod.outlook.com (2603:10b6:8:10e::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9094.22; Tue, 9 Sep 2025 08:53:37 +0000 Received: from BN1PEPF0000468D.namprd05.prod.outlook.com (2603:10b6:408:10b:cafe::df) by BN9P223CA0007.outlook.office365.com (2603:10b6:408:10b::12) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9115.15 via Frontend Transport; Tue, 9 Sep 2025 08:53:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by BN1PEPF0000468D.mail.protection.outlook.com (10.167.243.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9115.13 via Frontend Transport; Tue, 9 Sep 2025 08:53:36 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Tue, 9 Sep 2025 01:53:21 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.14; Tue, 9 Sep 2025 01:53:20 -0700 Received: from vdi.nvidia.com (10.127.8.14) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.1544.14 via Frontend Transport; Tue, 9 Sep 2025 01:53:16 -0700 From: Nimrod Oren To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Shuah Khan , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , "John Fastabend" , Stanislav Fomichev , Mohsin Bashir CC: Dragos Tatulea , Tariq Toukan , Carolina Jubran , , , , , Nimrod Oren Subject: [PATCH RFC net-next 5/5] selftests: drv-net: Check XDP header data with bpf_dynptr Date: Tue, 9 Sep 2025 11:52:36 +0300 Message-ID: <20250909085236.2234306-6-noren@nvidia.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20250909085236.2234306-1-noren@nvidia.com> References: <20250909085236.2234306-1-noren@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN1PEPF0000468D:EE_|DM4PR12MB7574:EE_ X-MS-Office365-Filtering-Correlation-Id: 31d56a85-0b92-418f-c40d-08ddef7e5d25 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700013|1800799024|82310400026|7416014|376014|921020; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?gUGzcn9bF00whcncEgZZ2X6bQPbcbBfOCHRDYU3jkoB/CIkctwNl1XYRnvkx?= =?us-ascii?Q?Ker9rX4eveyny3OcFmjBM572IogebtnYvSRMzuaBoFQpQz/13gWAuv0Ar2zd?= =?us-ascii?Q?ltq0pJuFGDEAkVxReG9RjiLNFjCQKLHdLgzoGr+5iPop0kmvnCAfLIX8oxFN?= =?us-ascii?Q?tHlKoHxo3hbQC2pS8WnnBq4N+HAqk4jGDbfShzebJSEqf2lNLTibOEWTo5Tk?= =?us-ascii?Q?QYI5FV3XRyAQlRt2Ici00OfmIHcwD+iZDOldkKRJMWbnMP6ebm1rpweOHt72?= =?us-ascii?Q?8TCINBg7yfd5QXs/4otAFgbXMuv8rUN3hy8RjXCE5Qg9jU/Unh6nOP9AV0GS?= =?us-ascii?Q?3jsISV67j1GwofmkQIDqkCbwVmtcQM0MVKlbAZ/RsM6jv3WjRxziDC7jY/n3?= =?us-ascii?Q?XmN14HdySNa2AzChVa/v1Lqm6ugNkn/9vD8HpzJF80+4Z/cPUzpanBk74TK8?= =?us-ascii?Q?TIveuK0D6zb6QRA28HLTsXgn1W4DBtK22FXefzR1IrRdJVrD34hCNMUtUqZe?= =?us-ascii?Q?VwR4qK1hhdMDy744B2FuMcfdKESaeTTZK6I4o5DGgH12rEVSk/dulBKVEeuD?= =?us-ascii?Q?8skSk0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us-ascii?Q?3EZHahVUu0GZwAwkJZvbCse4aU+rTKLCmGJGkridm6g8mXms/m4PI0jE3g?= =?us-ascii?Q?=3D=3D?= X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230040)(36860700013)(1800799024)(82310400026)(7416014)(376014)(921020);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Sep 2025 08:53:36.2367 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 31d56a85-0b92-418f-c40d-08ddef7e5d25 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN1PEPF0000468D.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB7574 Content-Type: text/plain; charset="utf-8" Update filter_udphdr to use bpf_dynptr_slice to read the packet headers instead of accessing them directly. The function previously returned a pointer to the UDP header, which was then used to calculate Ethernet and IP header lengths by subtracting xdp->data. Since this only works when the UDP header is in the linear region, rework the function to return the total header length instead of a pointer. Rename filter_udphdr() to check_udphdr() to reflect the new behavior. This makes the test viable for drivers that do not store any packet data in the linear part when in multi-buffer mode. Signed-off-by: Nimrod Oren Reviewed-by: Carolina Jubran Reviewed-by: Dragos Tatulea --- .../selftests/net/lib/xdp_native.bpf.c | 80 +++++++------------ 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/tools/testing/selftests/net/lib/xdp_native.bpf.c b/tools/testi= ng/selftests/net/lib/xdp_native.bpf.c index ff63f572552b..6df5164e3791 100644 --- a/tools/testing/selftests/net/lib/xdp_native.bpf.c +++ b/tools/testing/selftests/net/lib/xdp_native.bpf.c @@ -63,53 +63,49 @@ static void record_stats(struct xdp_md *ctx, __u32 stat= _type) __sync_fetch_and_add(count, 1); } =20 -static struct udphdr *filter_udphdr(struct xdp_md *ctx, __u16 port) +static __u32 check_udphdr(struct xdp_md *ctx, __u16 port) { - void *data_end =3D (void *)(long)ctx->data_end; - void *data =3D (void *)(long)ctx->data; struct udphdr *udph =3D NULL; - struct ethhdr *eth =3D data; + struct ethhdr *eth =3D NULL; + struct bpf_dynptr ptr; =20 - if (data + sizeof(*eth) > data_end) - return NULL; + bpf_dynptr_from_xdp(ctx, 0, &ptr); + eth =3D bpf_dynptr_slice(&ptr, 0, NULL, sizeof(*eth)); + if (!eth) + return 0; =20 if (eth->h_proto =3D=3D bpf_htons(ETH_P_IP)) { - struct iphdr *iph =3D data + sizeof(*eth); + struct iphdr *iph =3D bpf_dynptr_slice(&ptr, sizeof(*eth), + NULL, sizeof(*iph)); =20 - if (iph + 1 > (struct iphdr *)data_end || - iph->protocol !=3D IPPROTO_UDP) - return NULL; + if (!iph || iph->protocol !=3D IPPROTO_UDP) + return 0; =20 - udph =3D (void *)eth + sizeof(*iph) + sizeof(*eth); - } else if (eth->h_proto =3D=3D bpf_htons(ETH_P_IPV6)) { - struct ipv6hdr *ipv6h =3D data + sizeof(*eth); + udph =3D bpf_dynptr_slice(&ptr, sizeof(*iph) + sizeof(*eth), + NULL, sizeof(*udph)); + } else if (eth->h_proto =3D=3D bpf_htons(ETH_P_IPV6)) { + struct ipv6hdr *ipv6h =3D bpf_dynptr_slice(&ptr, sizeof(*eth), + NULL, sizeof(*ipv6h)); =20 - if (ipv6h + 1 > (struct ipv6hdr *)data_end || - ipv6h->nexthdr !=3D IPPROTO_UDP) - return NULL; + if (!ipv6h || ipv6h->nexthdr !=3D IPPROTO_UDP) + return 0; =20 - udph =3D (void *)eth + sizeof(*ipv6h) + sizeof(*eth); + udph =3D bpf_dynptr_slice(&ptr, sizeof(*ipv6h) + sizeof(*eth), + NULL, sizeof(*udph)); } else { - return NULL; + return 0; } =20 - if (udph + 1 > (struct udphdr *)data_end) - return NULL; - - if (udph->dest !=3D bpf_htons(port)) - return NULL; + if (!udph || udph->dest !=3D bpf_htons(port)) + return 0; =20 record_stats(ctx, STATS_RX); - - return udph; + return (void *)udph - (void *)eth + sizeof(*udph); } =20 static int xdp_mode_pass(struct xdp_md *ctx, __u16 port) { - struct udphdr *udph =3D NULL; - - udph =3D filter_udphdr(ctx, port); - if (!udph) + if (!check_udphdr(ctx, port)) return XDP_PASS; =20 record_stats(ctx, STATS_PASS); @@ -119,10 +115,7 @@ static int xdp_mode_pass(struct xdp_md *ctx, __u16 por= t) =20 static int xdp_mode_drop_handler(struct xdp_md *ctx, __u16 port) { - struct udphdr *udph =3D NULL; - - udph =3D filter_udphdr(ctx, port); - if (!udph) + if (!check_udphdr(ctx, port)) return XDP_PASS; =20 record_stats(ctx, STATS_DROP); @@ -363,19 +356,14 @@ static int xdp_adjst_tail_grow_data(struct xdp_md *ct= x, __u16 offset) =20 static int xdp_adjst_tail(struct xdp_md *ctx, __u16 port) { - void *data =3D (void *)(long)ctx->data; - struct udphdr *udph =3D NULL; - __s32 *adjust_offset, *val; + __s32 *adjust_offset; __u32 key, hdr_len; - void *offset_ptr; - __u8 tag; int ret; =20 - udph =3D filter_udphdr(ctx, port); - if (!udph) + hdr_len =3D check_udphdr(ctx, port); + if (!hdr_len) return XDP_PASS; =20 - hdr_len =3D (void *)udph - data + sizeof(struct udphdr); key =3D XDP_ADJST_OFFSET; adjust_offset =3D bpf_map_lookup_elem(&map_xdp_setup, &key); if (!adjust_offset) @@ -504,20 +492,14 @@ static int xdp_adjst_head_grow_data(struct xdp_md *ct= x, __u64 hdr_len, =20 static int xdp_head_adjst(struct xdp_md *ctx, __u16 port) { - void *data_end =3D (void *)(long)ctx->data_end; - void *data =3D (void *)(long)ctx->data; - struct udphdr *udph_ptr =3D NULL; __u32 key, size, hdr_len; __s32 *val; int res; =20 - /* Filter packets based on UDP port */ - udph_ptr =3D filter_udphdr(ctx, port); - if (!udph_ptr) + hdr_len =3D check_udphdr(ctx, port); + if (!hdr_len) return XDP_PASS; =20 - hdr_len =3D (void *)udph_ptr - data + sizeof(struct udphdr); - key =3D XDP_ADJST_OFFSET; val =3D bpf_map_lookup_elem(&map_xdp_setup, &key); if (!val) --=20 2.45.0