From patchwork Mon Dec 4 20:56:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173556 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033151vqy; Mon, 4 Dec 2023 12:57:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IFCSsmCClT7GPJ0bWh4Q4t15JPVLPQWJLgqfm/cdY5HQ42DjH+U0TIU9aCOqz+TKH2y5z4c X-Received: by 2002:a17:90b:33c7:b0:286:9b69:a0f6 with SMTP id lk7-20020a17090b33c700b002869b69a0f6mr193950pjb.39.1701723428546; Mon, 04 Dec 2023 12:57:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723428; cv=none; d=google.com; s=arc-20160816; b=WnSJhHZQFN7yrNN1ItJHC7Lb+2GkRg7u9/tHVTzACuWugK/GdCRtBzzRZE7amOs0T4 d0/xvyMMXh1Q4C91H043bfss1Iuq30aJweb/YUFSzS7mSwrDhciI5nUQz+FeR1iz+Rm1 gNZ8MAKba6raaqLirds8gUPjmL2mPK7vWy1CDafJ34AsGEuazpkIT7+xFisYegYLcCYw 6bw2oJe4spzUb48tYfnnx+8HQY2cBPLCY0CgnpDopU59ccT3m0LYEG8Arbv3ob0gY5W0 O/o6v7Z0H8KChPN9JqCdyelUtUk8TMxLmrNp15R9g3j2i7eQkBtILZ+6k6NndqawyZBf eTHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=ODyk+ttANQamRuubWNRCDO8I/D2JxBDiCAh0nttU7d0=; fh=JT3DbXCycnDgjng2egvIc916cuzoOMwYuenI3anQzBw=; b=gVSuQtFQLe7mIVpl7cAd8vhZvGrYoZhdmAmEWSA0/UH5HLJRUlOv4o2axkDlaNwtGQ s90lMKZfebIGwN6EtnOn6VaNBuwDnKMPQkfFfIV5hIuwGSmHeZjKC7IfNcrRyR5NO901 n6kudbVn5IjSx3K0gAsPhlBkEOp9/6Cc08tpaVXd5kaCfv5xW2gehGvYUhokoljPZWGB OVpSClpGQQ4xt96iEUB7yTjI2QxJiZ9+VUcVsXig+LJygjgMDlfKXj2CZl8XxUh18IZj YzMirRDJd701Ejk1dfXb7nIalke4Er2QhMc1yaY1B257ioQZGLX+EJ8Zu4wMjrQxt6bT jtKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=r4oVnJbK; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=0epMzhcX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id kb2-20020a17090ae7c200b0028676beec1bsi4084488pjb.152.2023.12.04.12.57.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:57:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=r4oVnJbK; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=0epMzhcX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 543E080ADF3F; Mon, 4 Dec 2023 12:57:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233697AbjLDU45 (ORCPT + 99 others); Mon, 4 Dec 2023 15:56:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233753AbjLDU4w (ORCPT ); Mon, 4 Dec 2023 15:56:52 -0500 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B965C109; Mon, 4 Dec 2023 12:56:55 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id DB9615C022F; Mon, 4 Dec 2023 15:56:54 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 04 Dec 2023 15:56:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723414; x= 1701809814; bh=ODyk+ttANQamRuubWNRCDO8I/D2JxBDiCAh0nttU7d0=; b=r 4oVnJbKuvQFHQYEX8R56ZVtXrlkgzIKKVAXWi8AlDWZHZKFL8p/mnFE5y5wV+eK+ 9lOOSI2esM4aFE0BbGS+7tnSOFL+m4vzr8yDkEMwTSD2pPfCEVIpYPlT9ziUuffn AHS8Ww/tjVQFKDK7/+WCPd9NT/sVUjbECVwj6yFz5F7+DDNVHBK0ozgNQ1b8UXD7 HfqcPnc7d0/60iP1zeAHUz15pZSzCg0qDt6ChLSwMzsWqsWLQbafj5MZi/4d+U9M QRPeT4rc6NUiahJWVStSkybkfJTxRFH8iu27phHt1pKSxOr3z9YqslbyUFFZ2jjT Fprwp2xUClTWgZg2SqHvQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723414; x= 1701809814; bh=ODyk+ttANQamRuubWNRCDO8I/D2JxBDiCAh0nttU7d0=; b=0 epMzhcXlEAJh9hIhfZQNfzrbBwP6o7+vnL+hC48PCUaQtGZI2fOrXx/8caIwa5wq htQ/sixbVUcrurbCJzWkg9TxH7N/3+9CQ0eK3LIDRTlO4CdjM9kQDTzzNgULXWwx M/DiIVflIr3aKq2z8cQzheaYTDb9wVzOKr9qWw5VdbGItrnQnekMKpMbOqOUtpk5 ueNwd33L0Fwj40BKvK6X5TT2/uw07ldQO2fs86DAt9mavAqB0CAeoPPnGcQUPGTe 5vL4nb14Kp7jOvdr0dcwiIRMxmjO4u0asiV0FyIxCAGO0Z5rfl91H/fXU9U3Ie38 VZSe4IM+U6iZECSED1QdQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdejtddmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpefgfefggeejhfduieekvdeuteffleeifeeuvdfhheejleej jeekgfffgefhtddtteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:56:52 -0500 (EST) From: Daniel Xu To: davem@davemloft.net, Herbert Xu , steffen.klassert@secunet.com, pabeni@redhat.com, kuba@kernel.org, edumazet@google.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, devel@linux-ipsec.org Subject: [PATCH bpf-next v4 01/10] xfrm: bpf: Move xfrm_interface_bpf.c to xfrm_bpf.c Date: Mon, 4 Dec 2023 13:56:21 -0700 Message-ID: X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:57:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386345729761579 X-GMAIL-MSGID: 1784386345729761579 This commit moves the contents of xfrm_interface_bpf.c into a new file, xfrm_bpf.c This is in preparation for adding more xfrm kfuncs. We'd like to keep all the bpf integrations in a single file. Signed-off-by: Daniel Xu Acked-by: Steffen Klassert --- net/xfrm/Makefile | 7 +------ net/xfrm/{xfrm_interface_bpf.c => xfrm_bpf.c} | 12 ++++++++---- 2 files changed, 9 insertions(+), 10 deletions(-) rename net/xfrm/{xfrm_interface_bpf.c => xfrm_bpf.c} (88%) diff --git a/net/xfrm/Makefile b/net/xfrm/Makefile index cd47f88921f5..29fff452280d 100644 --- a/net/xfrm/Makefile +++ b/net/xfrm/Makefile @@ -5,12 +5,6 @@ xfrm_interface-$(CONFIG_XFRM_INTERFACE) += xfrm_interface_core.o -ifeq ($(CONFIG_XFRM_INTERFACE),m) -xfrm_interface-$(CONFIG_DEBUG_INFO_BTF_MODULES) += xfrm_interface_bpf.o -else ifeq ($(CONFIG_XFRM_INTERFACE),y) -xfrm_interface-$(CONFIG_DEBUG_INFO_BTF) += xfrm_interface_bpf.o -endif - obj-$(CONFIG_XFRM) := xfrm_policy.o xfrm_state.o xfrm_hash.o \ xfrm_input.o xfrm_output.o \ xfrm_sysctl.o xfrm_replay.o xfrm_device.o @@ -21,3 +15,4 @@ obj-$(CONFIG_XFRM_USER_COMPAT) += xfrm_compat.o obj-$(CONFIG_XFRM_IPCOMP) += xfrm_ipcomp.o obj-$(CONFIG_XFRM_INTERFACE) += xfrm_interface.o obj-$(CONFIG_XFRM_ESPINTCP) += espintcp.o +obj-$(CONFIG_DEBUG_INFO_BTF) += xfrm_bpf.o diff --git a/net/xfrm/xfrm_interface_bpf.c b/net/xfrm/xfrm_bpf.c similarity index 88% rename from net/xfrm/xfrm_interface_bpf.c rename to net/xfrm/xfrm_bpf.c index 7d5e920141e9..3d3018b87f96 100644 --- a/net/xfrm/xfrm_interface_bpf.c +++ b/net/xfrm/xfrm_bpf.c @@ -1,9 +1,8 @@ // SPDX-License-Identifier: GPL-2.0-only -/* Unstable XFRM Helpers for TC-BPF hook +/* Unstable XFRM BPF helpers. * - * These are called from SCHED_CLS BPF programs. Note that it is - * allowed to break compatibility for these functions since the interface they - * are exposed through to BPF programs is explicitly unstable. + * Note that it is allowed to break compatibility for these functions since the + * interface they are exposed through to BPF programs is explicitly unstable. */ #include @@ -12,6 +11,9 @@ #include #include +#if IS_BUILTIN(CONFIG_XFRM_INTERFACE) || \ + (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)) + /* bpf_xfrm_info - XFRM metadata information * * Members: @@ -108,3 +110,5 @@ int __init register_xfrm_interface_bpf(void) return register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &xfrm_interface_kfunc_set); } + +#endif /* xfrm interface */ From patchwork Mon Dec 4 20:56:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173557 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033187vqy; Mon, 4 Dec 2023 12:57:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFapLSjwpRL1dLEp2gjMQIFbKXiVJPdgGFlt1WdYAu/nw1QsOyngku0t7EKHsQYoq+V6rc4 X-Received: by 2002:a05:6a20:5497:b0:18c:3065:8048 with SMTP id i23-20020a056a20549700b0018c30658048mr2598097pzk.42.1701723434829; Mon, 04 Dec 2023 12:57:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723434; cv=none; d=google.com; s=arc-20160816; b=Xg4QkWiTW2NK47Av83RX9wanz21h3U0MfEVrmvZt44VB3iRqawegIGSVOVh7qAav0U w9JjYLwjMct9fIaoy/yNirtGP6kpSnJABypLqZR5xBGCLBz8B3xHVJsKMAXBy8Ukdkdn XE5vogFMUl2UZo+4YLBBQzK/Rb0Ygb0ijqdsfDXY9RmOCaFLE7TsBSZkQg1OTzHpjEv4 E1mcjZaN/ntitaZaOHQNQNlTJLNSHuI34HKbPF78RRAeRZZjuL1qczfe/FIuRsBPVHAu MvTbwTZQLOSVuOIDeL1ndDa2v/IziwBVF6+HMnIK1r0X05cRitDv1Tk3vP0xvvFAif1z vzUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=UcXo1ArXPqQ9bSXiMAigIass8f59dei4sHavEoNqsjQ=; fh=8nn86nDL6CrA1oLZrFYZeLdoRCjg4mIZWo7Zca0gzXU=; b=gHoVeiZsWOS7MH1Yav/F1CnSzLXrBWAp1Lq/yZD79whdd+dhkiO0gHwQDsVl1xw8YU RsP1CkkIaIPaR0MQ5W1vLkRxep0Jc/4A+Hs8OWJX5Z6RiKOOenLYSYeEqvStyw1JuJZ+ rzwc309cPgVE0X7GYVvmr4RpD0VyjSrgEbg4OG1o8kJEjJl9GjtuJHeKC8Hf3rn+m567 lN9heZrX4WHo57KSvChUJ/pvQrCv9EeezColwSKw9Zfz85Wzr1aU0rOUbbtha0fo5Gf3 NoH44PU0j+/fpmDX8PcASgX65Th+UqiWyl8gpS6FaSmqP39Ymqby48W/cFB/3Hiq7MsQ d1eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=c0OTYxpp; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=2EZLbM7X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id bg2-20020a056a02010200b005c201eb7e85si8790337pgb.541.2023.12.04.12.57.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:57:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=c0OTYxpp; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=2EZLbM7X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A051F80ADF26; Mon, 4 Dec 2023 12:57:13 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234197AbjLDU5C (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233865AbjLDU45 (ORCPT ); Mon, 4 Dec 2023 15:56:57 -0500 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E80D138; Mon, 4 Dec 2023 12:56:57 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 9504C5C0238; Mon, 4 Dec 2023 15:56:56 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 04 Dec 2023 15:56:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723416; x= 1701809816; bh=UcXo1ArXPqQ9bSXiMAigIass8f59dei4sHavEoNqsjQ=; b=c 0OTYxpp26YKzc32ZOiRJF//hyv+3DhxRi7QfGrwB0zs0s5UFfcPRBJLf1vQ5Sq+o WfqSka+3EKQx7EgLlHJ2sJPMnYKWLts0ITDv2ZuFKh6zXjSkOLd+AyTw8kw5iZij Tte6OTJPANmkM+9g03vuD+m2DhN0ViNQX230e9j4vGWXmJDcZ+gt46b+0Ls/5hGy 192j6YeoavpycENV2pEK4A2CVQx+1eZWvQleIlSL804SqVj0uByArBQKnOBa7AQs Y91e7a+hAiNqGIJTgIeBuaKnDRXfgi9kCo+ypYj6ngukz21dsJdVHKggDPjxwfYZ 1LzUKz1/e4c7Yju6UzK8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723416; x= 1701809816; bh=UcXo1ArXPqQ9bSXiMAigIass8f59dei4sHavEoNqsjQ=; b=2 EZLbM7XdJ77A2rIAaZN0pdUhDtynCbJCX3TwVdaGivcTey1LwXKQZQXkwfqaWbDF WMuXbku35SVwOTxTNwQ2OiBSZBoPPQa63W48hzIBdE102n6bvm/ai5y1mwFQiCRk TVQGBxUILiR+qb8Y/qb9CIpYuTNNW5vS0pkzkgApfNxQRDXA3dcrEW0BpuUfCTSA K2/m8pk98r98akzxzrMs3LzFpMJqkvKjKeyTXk+ghmlc0wxnoTY0yl9jN/QR6uOi pWwls3cAatqcL5uG3QHuy6//xDfIRdxxdEB8Yyv/QNpMr6W141ifg7ufTLmAWlRr q4rvXX2izNAfDuZIuQmiw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdefhedmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpefgfefggeejhfduieekvdeuteffleeifeeuvdfhheejleej jeekgfffgefhtddtteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:56:54 -0500 (EST) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, davem@davemloft.net, Herbert Xu , steffen.klassert@secunet.com, pabeni@redhat.com, hawk@kernel.org, john.fastabend@gmail.com, kuba@kernel.org, edumazet@google.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, devel@linux-ipsec.org Subject: [PATCH bpf-next v4 02/10] bpf: xfrm: Add bpf_xdp_get_xfrm_state() kfunc Date: Mon, 4 Dec 2023 13:56:22 -0700 Message-ID: X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:57:13 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386352341107602 X-GMAIL-MSGID: 1784386352341107602 This commit adds an unstable kfunc helper to access internal xfrm_state associated with an SA. This is intended to be used for the upcoming IPsec pcpu work to assign special pcpu SAs to a particular CPU. In other words: for custom software RSS. That being said, the function that this kfunc wraps is fairly generic and used for a lot of xfrm tasks. I'm sure people will find uses elsewhere over time. Co-developed-by: Antony Antony Signed-off-by: Antony Antony Signed-off-by: Daniel Xu Acked-by: Steffen Klassert --- include/net/xfrm.h | 9 ++++ net/xfrm/xfrm_bpf.c | 102 +++++++++++++++++++++++++++++++++++++++++ net/xfrm/xfrm_policy.c | 2 + 3 files changed, 113 insertions(+) diff --git a/include/net/xfrm.h b/include/net/xfrm.h index c9bb0f892f55..1d107241b901 100644 --- a/include/net/xfrm.h +++ b/include/net/xfrm.h @@ -2190,4 +2190,13 @@ static inline int register_xfrm_interface_bpf(void) #endif +#if IS_ENABLED(CONFIG_DEBUG_INFO_BTF) +int register_xfrm_state_bpf(void); +#else +static inline int register_xfrm_state_bpf(void) +{ + return 0; +} +#endif + #endif /* _NET_XFRM_H */ diff --git a/net/xfrm/xfrm_bpf.c b/net/xfrm/xfrm_bpf.c index 3d3018b87f96..3d6cac7345ca 100644 --- a/net/xfrm/xfrm_bpf.c +++ b/net/xfrm/xfrm_bpf.c @@ -6,9 +6,11 @@ */ #include +#include #include #include +#include #include #if IS_BUILTIN(CONFIG_XFRM_INTERFACE) || \ @@ -112,3 +114,103 @@ int __init register_xfrm_interface_bpf(void) } #endif /* xfrm interface */ + +/* bpf_xfrm_state_opts - Options for XFRM state lookup helpers + * + * Members: + * @error - Out parameter, set for any errors encountered + * Values: + * -EINVAL - netns_id is less than -1 + * -EINVAL - opts__sz isn't BPF_XFRM_STATE_OPTS_SZ + * -ENONET - No network namespace found for netns_id + * @netns_id - Specify the network namespace for lookup + * Values: + * BPF_F_CURRENT_NETNS (-1) + * Use namespace associated with ctx + * [0, S32_MAX] + * Network Namespace ID + * @mark - XFRM mark to match on + * @daddr - Destination address to match on + * @spi - Security parameter index to match on + * @proto - L3 protocol to match on + * @family - L3 protocol family to match on + */ +struct bpf_xfrm_state_opts { + s32 error; + s32 netns_id; + u32 mark; + xfrm_address_t daddr; + __be32 spi; + u8 proto; + u16 family; +}; + +enum { + BPF_XFRM_STATE_OPTS_SZ = sizeof(struct bpf_xfrm_state_opts), +}; + +__bpf_kfunc_start_defs(); + +/* bpf_xdp_get_xfrm_state - Get XFRM state + * + * Parameters: + * @ctx - Pointer to ctx (xdp_md) in XDP program + * Cannot be NULL + * @opts - Options for lookup (documented above) + * Cannot be NULL + * @opts__sz - Length of the bpf_xfrm_state_opts structure + * Must be BPF_XFRM_STATE_OPTS_SZ + */ +__bpf_kfunc struct xfrm_state * +bpf_xdp_get_xfrm_state(struct xdp_md *ctx, struct bpf_xfrm_state_opts *opts, u32 opts__sz) +{ + struct xdp_buff *xdp = (struct xdp_buff *)ctx; + struct net *net = dev_net(xdp->rxq->dev); + struct xfrm_state *x; + + if (!opts || opts__sz < sizeof(opts->error)) + return NULL; + + if (opts__sz != BPF_XFRM_STATE_OPTS_SZ) { + opts->error = -EINVAL; + return NULL; + } + + if (unlikely(opts->netns_id < BPF_F_CURRENT_NETNS)) { + opts->error = -EINVAL; + return NULL; + } + + if (opts->netns_id >= 0) { + net = get_net_ns_by_id(net, opts->netns_id); + if (unlikely(!net)) { + opts->error = -ENONET; + return NULL; + } + } + + x = xfrm_state_lookup(net, opts->mark, &opts->daddr, opts->spi, + opts->proto, opts->family); + + if (opts->netns_id >= 0) + put_net(net); + + return x; +} + +__bpf_kfunc_end_defs(); + +BTF_SET8_START(xfrm_state_kfunc_set) +BTF_ID_FLAGS(func, bpf_xdp_get_xfrm_state, KF_RET_NULL | KF_ACQUIRE) +BTF_SET8_END(xfrm_state_kfunc_set) + +static const struct btf_kfunc_id_set xfrm_state_xdp_kfunc_set = { + .owner = THIS_MODULE, + .set = &xfrm_state_kfunc_set, +}; + +int __init register_xfrm_state_bpf(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, + &xfrm_state_xdp_kfunc_set); +} diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index c13dc3ef7910..1b7e75159727 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -4218,6 +4218,8 @@ void __init xfrm_init(void) #ifdef CONFIG_XFRM_ESPINTCP espintcp_init(); #endif + + register_xfrm_state_bpf(); } #ifdef CONFIG_AUDITSYSCALL From patchwork Mon Dec 4 20:56:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173563 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033589vqy; Mon, 4 Dec 2023 12:58:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXH1fl12F0jwoxd3Ea5bmf7k3Y8+LZZYzWYxWO225JJ79FLWv7A4FMv9PZsegsSL1nA88+ X-Received: by 2002:a05:6a00:1494:b0:6ce:39d5:b6 with SMTP id v20-20020a056a00149400b006ce39d500b6mr163482pfu.32.1701723496578; Mon, 04 Dec 2023 12:58:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723496; cv=none; d=google.com; s=arc-20160816; b=kDw6psWI5SL4ZFYbxtjbX/pKvaebtpEKx0wY/hVLFoMTaIbJqaJy8pCWOU8oTRO7Ja LeskZHZDCw1nDHwnFYeC5tNniczZ2m/cNJruFsgTBcrof46LAxOoZocHTfZDHXvsNO8P LL9nsfs/XOkleaDmgw3mD5lEOKBu181PhFQEodexQm3lPIAzzIRh4AMWEaRu7V6JPhxp aP/eBHoUVfp3Hp32ER2E4K29sNoxxHWV9u7UxzusYc8rUrd912L45pxk44eHk7STW9Qf 1r+bgEcsWmfL8KNtJFkAVxV426LtHlcb8/eKdLd299o9XWRH5yYCZkjXgOvSTbG8T0HS fGvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=tXj3x6eY8vsT5+TocaSwBKV9SbhnMSnULNqJ8i4jISo=; fh=8nn86nDL6CrA1oLZrFYZeLdoRCjg4mIZWo7Zca0gzXU=; b=NNhN7b9wcJITaWoA1fkG36RBC0Ihd3SJoNIHHHvs+2PGmQuvfzHN2NlKGh8tvvCPlK 2bN4bvfd3TuL+lCAKzKolQy3HO528x9lC9gr1XMby36AJ1xT/5eIigVZTTL0m9OzEVpB kIDoMbiJMPjvoegSWRvNrIJvnzf4JfH2qtKMEBg3HDlU1+3Zp+nl6HCTabEk8Uk3Hrgm 25iopznw0aw6aklMJvn5kBSkigHnVxLnhtT3vS+gFz/xYw4ws384BA2bH5CFfslIbzwd /2yN7kpz+EcNg6Na5KaBuFP8SJ7S7DPGPTNWG3fyDd6/exaRwp16l6zxlp8NBNcOI5Nr ZnlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=rHkSSEtY; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=F5fkODck; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id ay26-20020a056a00301a00b006ce3c611131si3605919pfb.122.2023.12.04.12.58.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:58:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=rHkSSEtY; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=F5fkODck; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id CA14E809EE1D; Mon, 4 Dec 2023 12:58:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345671AbjLDU5G (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234010AbjLDU46 (ORCPT ); Mon, 4 Dec 2023 15:56:58 -0500 Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E44FA18F; Mon, 4 Dec 2023 12:56:58 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 46C2C5C022C; Mon, 4 Dec 2023 15:56:58 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 04 Dec 2023 15:56:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723418; x= 1701809818; bh=tXj3x6eY8vsT5+TocaSwBKV9SbhnMSnULNqJ8i4jISo=; b=r HkSSEtYynt0S9qFqRUmbLLODgbBYDPjNBFS8JA5hFCVDhjGZvwkNYPlRcwC5NBsa PV7r8hmSoohudI4tpRRyxoJTCjfo/X1kpLFGOFwEb782e0zj+oBmPiweq0IayVDX BdPd4aiNo/uDmmXEldcMIS+C0IYYiafxVwjLRavIHzwPXjiuCuJ8w+foKIO9WDuN 5xKqHTddUTCIE+qelsnNtCoPPW+oAwpVmBM2r8g1nviyTLU9t1ivkv45xPxYF3u1 M6tJYHvZgzIGc3XRyGEPnq5rVRBozkWI0LTXEpBP1Mhjn1Pz3CvMica8SfGdOOHk 1i94nJODY6Zp8gJpab5NQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723418; x= 1701809818; bh=tXj3x6eY8vsT5+TocaSwBKV9SbhnMSnULNqJ8i4jISo=; b=F 5fkODckQaeWCoS2rJ2cLpXu2ZTXbG2c05MIp0yxtQRuhklvn4t+PEicjnOOLBsIk hYU3IR53AaLMHAUb8hdkeGF7q+gnBX9nGQH2jIdmz/nddTYNzPfmomWbu5ed1Q/n tfHO7Dkby6Etz0bn6WGutzLiwvijotdCWIxFWdrvt/Vv98vbqObqQtLNCNFp4bDt i8DIP6D7rRezaOP2yx8IHUFEapHKpiz3RDeS4ho2tYw7rfSU4OtvocKabGsUasor WkiM8F/ggkw862/IZKwFpU4lblb4PbLe7ReoEdoHF8P56CCoGpg2xB3HngJpsWHp PnGw+I6uj56ZfEDek/NqQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdefhedmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpefgfefggeejhfduieekvdeuteffleeifeeuvdfhheejleej jeekgfffgefhtddtteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:56:56 -0500 (EST) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, davem@davemloft.net, Herbert Xu , steffen.klassert@secunet.com, pabeni@redhat.com, hawk@kernel.org, john.fastabend@gmail.com, kuba@kernel.org, edumazet@google.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, devel@linux-ipsec.org Subject: [PATCH bpf-next v4 03/10] bpf: xfrm: Add bpf_xdp_xfrm_state_release() kfunc Date: Mon, 4 Dec 2023 13:56:23 -0700 Message-ID: <66e92984df48e03a518580f2d416a6fdb5bd4b0d.1701722991.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:58:06 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386417547825743 X-GMAIL-MSGID: 1784386417547825743 This kfunc releases a previously acquired xfrm_state from bpf_xdp_get_xfrm_state(). Co-developed-by: Antony Antony Signed-off-by: Antony Antony Signed-off-by: Daniel Xu Acked-by: Steffen Klassert --- net/xfrm/xfrm_bpf.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/net/xfrm/xfrm_bpf.c b/net/xfrm/xfrm_bpf.c index 3d6cac7345ca..5ca780526607 100644 --- a/net/xfrm/xfrm_bpf.c +++ b/net/xfrm/xfrm_bpf.c @@ -198,10 +198,26 @@ bpf_xdp_get_xfrm_state(struct xdp_md *ctx, struct bpf_xfrm_state_opts *opts, u32 return x; } +/* bpf_xdp_xfrm_state_release - Release acquired xfrm_state object + * + * This must be invoked for referenced PTR_TO_BTF_ID, and the verifier rejects + * the program if any references remain in the program in all of the explored + * states. + * + * Parameters: + * @x - Pointer to referenced xfrm_state object, obtained using + * bpf_xdp_get_xfrm_state. + */ +__bpf_kfunc void bpf_xdp_xfrm_state_release(struct xfrm_state *x) +{ + xfrm_state_put(x); +} + __bpf_kfunc_end_defs(); BTF_SET8_START(xfrm_state_kfunc_set) BTF_ID_FLAGS(func, bpf_xdp_get_xfrm_state, KF_RET_NULL | KF_ACQUIRE) +BTF_ID_FLAGS(func, bpf_xdp_xfrm_state_release, KF_RELEASE) BTF_SET8_END(xfrm_state_kfunc_set) static const struct btf_kfunc_id_set xfrm_state_xdp_kfunc_set = { From patchwork Mon Dec 4 20:56:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173559 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033332vqy; Mon, 4 Dec 2023 12:57:38 -0800 (PST) X-Google-Smtp-Source: AGHT+IH9EbM6yNIe2yAtwR+lXj5xnvZ+I+2mZNAFDwh67IdTCyd8vrx2IQex5WEe/FPeGf6qc0RJ X-Received: by 2002:a17:90b:4c11:b0:285:ada6:cc11 with SMTP id na17-20020a17090b4c1100b00285ada6cc11mr113729pjb.1.1701723457862; Mon, 04 Dec 2023 12:57:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723457; cv=none; d=google.com; s=arc-20160816; b=StXKNBoRbKBlfH6kLLs0l/NuUZ2yVXR+5J+8JDtFXAcub1/XVrQEDVHtuLgdQdATwz MWevDHDuHmuuMkGcS4IVCB6xxbPqf8zt4QdPm1kFvNJ8sbMVHFimtRAOH8IbwsKdBteK aZi/L456rgbp/gsmzBzR+4Q3dSP1lEiFkb5JFZ05QaoptyUaBiKeF7ytCO8IPdMzppnZ C5TabPMMnsuNyKvPwKYzd+hLsY9bi4ZPdWfIuTsVQn26Cce8eQNDOBpi41bqO6VVSD0T afZDeNfPCwqhfZmotgdLkT+Gbrl5Uc0CmVVSpq8MLaPlIBvo1G+OeCKzPremjF/8ltTp fbXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=frFWRgIM/BeMgIIvESHMUoKVQK0r3Pl2bvU4ZuSshRc=; fh=OlXOGBdxvOneRja1Q9BZRzlnanKvQL6gu1zHac3eNgs=; b=0ZrpLwIpNEAcDi2kgHTuesseET9J6frhvpovLyc4QEz0rQ2MvFSqN2Zn5K0PTnog+q fCYAS5EPkhBC+4uYkkJMV3Su95D7YC489Z8ncregtspCxx3dpMq9JMNDs94lcn/FAmXR wyGHYrCncPnyl/WYsNXEWvCTRi64lVoYqYO9tzr4bXJ+s6FFTXeWGtdj3H+NieLT894o CDd0031kV1uF3x+5Qifl35tg48cDc9QC7xciKLIJvaFgsgepGZvNnTwkcDZ/88DrD1u9 +O2a8v8UmY4CjO8Vubs8hQ10YUNgEtXcIjS5FxuJpC043gY/QXdiiOfVGP2G9rDeIME9 45EA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=mSqkINcQ; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=nx00iUpT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id t6-20020a17090ad50600b0028659a5d68fsi6061490pju.48.2023.12.04.12.57.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:57:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=mSqkINcQ; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=nx00iUpT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5118E80BEDC9; Mon, 4 Dec 2023 12:57:34 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233873AbjLDU5S (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49070 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234127AbjLDU5C (ORCPT ); Mon, 4 Dec 2023 15:57:02 -0500 Received: from new4-smtp.messagingengine.com (new4-smtp.messagingengine.com [66.111.4.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E2E01BC; Mon, 4 Dec 2023 12:57:01 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.nyi.internal (Postfix) with ESMTP id 3CEA658045D; Mon, 4 Dec 2023 15:57:00 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 04 Dec 2023 15:57:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723420; x= 1701730620; bh=frFWRgIM/BeMgIIvESHMUoKVQK0r3Pl2bvU4ZuSshRc=; b=m SqkINcQHxbWSUBUJzPxK8wksAFZ+b5OXhV4a/ZwNfWXPYFyuAk1Rh+Al8G+WEjh8 F4dfDce1gwAViusNsDX3iarLdX5vmKvAN4bVpaJ+tayuTocuXFUoqCKt1E4Sq6zN HjOWNEOLmIMCd3HFIOQI5vGPqtld8OAqLdEyyelfBoloSKd+7zpO4SxzLt5Er+Rc zF2x2s2u1JpiX+r01GwBREN86b4o1bl1PC2RW//ejGIkLs4dDSzxGV/V2nZ1x53+ 9shbaXQnbIJpj743DimxJ0vNzJSMsNAzQ55EznkrCCviTA0UMkTFjWa605ToSmOg nr2b7piRQGQsEo7pITZwQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723420; x= 1701730620; bh=frFWRgIM/BeMgIIvESHMUoKVQK0r3Pl2bvU4ZuSshRc=; b=n x00iUpTzWbiSsyAoM77a2vxQPTLImeX/GE4aSA9MNIoLeMkDoPLeA1QwesW2Dclk KCpuwzGYg/eFz4KNtDV3l6nZ4Rpo7FEbgoZvLg886oZ8qEszarMeMCCZqD4g1hL/ WEqhI62m5+aR3FHDOOOsyWwQH15ksykVswpldm+FcnSdX3AilNFH6WZSV4srqISo vQFqicMePoZmNZCTueXltWV4HeDqBCrwW1c7swhNc2VU9yy5HjFq78SFOoTv1RL1 ITxheXrM/WtsZaqQX07efy/0V7J/nnbaI/plaNgO5Kp67yJxUG9geGmp2dkTHF/N GleKFo46Qsi6G+0YWwvPw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculddvfedmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpeeigeffteehteffheejkeefjeeuudfgvdekkeetudeghedu gffgleffhefgjeevgfenucffohhmrghinheplhhlvhhmrdhorhhgnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiih ii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:56:58 -0500 (EST) From: Daniel Xu To: daniel@iogearbox.net, ast@kernel.org, nathan@kernel.org, andrii@kernel.org, ndesaulniers@google.com, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: martin.lau@linux.dev, song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, trix@redhat.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, devel@linux-ipsec.org, netdev@vger.kernel.org, Jonathan Lemon Subject: [PATCH bpf-next v4 04/10] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro Date: Mon, 4 Dec 2023 13:56:24 -0700 Message-ID: X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:57:34 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386376323911692 X-GMAIL-MSGID: 1784386376323911692 === Motivation === Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy. Two alternatives to this approach are: 1. Use the upcoming `preserve_static_offset` [0] attribute to disable CO-RE on specific structs. 2. Use broader byte-sized writes to write to bitfields. (1) is a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated vmlinux.h. It's also not generally available in released LLVM versions yet. (2) makes the code quite hard to read and write. And especially if BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to to have an inverse helper for writing. === Implementation details === Since the logic is a bit non-obvious, I thought it would be helpful to explain exactly what's going on. To start, it helps by explaining what LSHIFT_U64 (lshift) and RSHIFT_U64 (rshift) is designed to mean. Consider the core of the BPF_CORE_READ_BITFIELD() algorithm: val <<= __CORE_RELO(s, field, LSHIFT_U64); val = val >> __CORE_RELO(s, field, RSHIFT_U64); Basically what happens is we lshift to clear the non-relevant (blank) higher order bits. Then we rshift to bring the relevant bits (bitfield) down to LSB position (while also clearing blank lower order bits). To illustrate: Start: ........XXX...... Lshift: XXX......00000000 Rshift: 00000000000000XXX where `.` means blank bit, `0` means 0 bit, and `X` means bitfield bit. After the two operations, the bitfield is ready to be interpreted as a regular integer. Next, we want to build an alternative (but more helpful) mental model on lshift and rshift. That is, to consider: * rshift as the total number of blank bits in the u64 * lshift as number of blank bits left of the bitfield in the u64 Take a moment to consider why that is true by consulting the above diagram. With this insight, we can now define the following relationship: bitfield _ | | 0.....00XXX0...00 | | | | |______| | | lshift | | |____| (rshift - lshift) That is, we know the number of higher order blank bits is just lshift. And the number of lower order blank bits is (rshift - lshift). Finally, we can examine the core of the write side algorithm: mask = (~0ULL << rshift) >> lshift; // 1 val = (val & ~mask) | ((nval << rpad) & mask); // 2 1. Compute a mask where the set bits are the bitfield bits. The first left shift zeros out exactly the number of blank bits, leaving a bitfield sized set of 1s. The subsequent right shift inserts the correct amount of higher order blank bits. 2. On the left of the `|`, mask out the bitfield bits. This creates 0s where the new bitfield bits will go. On the right of the `|`, bring nval into the correct bit position and mask out any bits that fall outside of the bitfield. Finally, by bor'ing the two halves, we get the final set of bits to write back. [0]: https://reviews.llvm.org/D133361 Co-developed-by: Eduard Zingerman Signed-off-by: Eduard Zingerman Co-developed-by: Jonathan Lemon Signed-off-by: Jonathan Lemon Signed-off-by: Daniel Xu Acked-by: Andrii Nakryiko --- tools/lib/bpf/bpf_core_read.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h index 1ac57bb7ac55..7325a12692a3 100644 --- a/tools/lib/bpf/bpf_core_read.h +++ b/tools/lib/bpf/bpf_core_read.h @@ -111,6 +111,38 @@ enum bpf_enum_value_kind { val; \ }) +/* + * Write to a bitfield, identified by s->field. + * This is the inverse of BPF_CORE_WRITE_BITFIELD(). + */ +#define BPF_CORE_WRITE_BITFIELD(s, field, new_val) ({ \ + void *p = (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ + unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ + unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ + unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ + unsigned long long mask, val, nval = new_val; \ + unsigned int rpad = rshift - lshift; \ + \ + asm volatile("" : "+r"(p)); \ + \ + switch (byte_size) { \ + case 1: val = *(unsigned char *)p; break; \ + case 2: val = *(unsigned short *)p; break; \ + case 4: val = *(unsigned int *)p; break; \ + case 8: val = *(unsigned long long *)p; break; \ + } \ + \ + mask = (~0ULL << rshift) >> lshift; \ + val = (val & ~mask) | ((nval << rpad) & mask); \ + \ + switch (byte_size) { \ + case 1: *(unsigned char *)p = val; break; \ + case 2: *(unsigned short *)p = val; break; \ + case 4: *(unsigned int *)p = val; break; \ + case 8: *(unsigned long long *)p = val; break; \ + } \ +}) + #define ___bpf_field_ref1(field) (field) #define ___bpf_field_ref2(type, field) (((typeof(type) *)0)->field) #define ___bpf_field_ref(args...) \ From patchwork Mon Dec 4 20:56:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173558 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033312vqy; Mon, 4 Dec 2023 12:57:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IFAji2U60At4abfgZQxqwZqjCjkX11sjbR4GVNSCGKczzfcsC6k821cg9Z6hSEvPSlc1Ynm X-Received: by 2002:a17:902:aa96:b0:1cc:5db8:7e92 with SMTP id d22-20020a170902aa9600b001cc5db87e92mr21654938plr.58.1701723453526; Mon, 04 Dec 2023 12:57:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723453; cv=none; d=google.com; s=arc-20160816; b=eKmSnng7coZ7KfG3P12EGEK4xhGtnAHeF2R5EuxafSKTeO6y1fv7qOqxdoiNsZSVdY N/VGaZZy3/QvgoFw8j34XnVKL9+ZMfvB4FYxZAIlrG0M/Czgbc4pK+X/PysqLXW/i3EQ DQxsVdfVC1bYrV9Oni2BiahiOGeZDEX5c856TTzxE6awLuSfiHtMNYLUrJgg4AJGB8Lo hyC8opcHvMC0N++5Ss1KGf+PJkEj0D6iyoGRV+FVn5WyIjKdPI59YIj52IRxD/uMxZcK rBmaBT+yEYPVnvZ5727Pgk10L0mCeQoNVvyUY1NQQ1jCRPetODkKtkwGuTFM7gD9KTKi /hrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=Z+7Afm7t3HxDVtoQyXLZsXvB2QVInlcd4KmkW3EtfRk=; fh=ZAfxtzMnp2hdRZglVL8+/tHyTizX7HwNltmSipEX0vA=; b=mLQovPAj2wMwqiGznbBzviMHXJMwBD0k3onWGxpF2YMP4awaaK85Dl+gRSjXCPbRES fZIOZByeP/oWEnfIRHHXHd3cutIkD8BVU59/388H0gIqynfQiq8yB20DZGUvKbfYtzUx ac9juLIASGP/0LTU29PRDwpJAxvLa6NgHlynDLY3TBTg1wNqJmSbgw3SxEJc6ZZelREx 2565wK6lAW5h4E/ppX9YPvixMv7S/vQLRdUPyaF1ZXuH6PoPn3a8lJ2sW9grt/BWnlCv 9dDE8y0ObNqVki++ftA+w+JYe0Fk5Lonb2RkLkd7u9/zW0TS/aXczsHlOu7MmEh9MRRU Velw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=BHnJ+fx+; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=X8zDzbql; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id x22-20020a170902821600b001cfd52a2266si5419935pln.403.2023.12.04.12.57.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:57:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=BHnJ+fx+; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=X8zDzbql; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 22CB480ADF2E; Mon, 4 Dec 2023 12:57:32 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346134AbjLDU5W (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234021AbjLDU5G (ORCPT ); Mon, 4 Dec 2023 15:57:06 -0500 Received: from new4-smtp.messagingengine.com (new4-smtp.messagingengine.com [66.111.4.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5AEED65; Mon, 4 Dec 2023 12:57:02 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.nyi.internal (Postfix) with ESMTP id 23A39580A13; Mon, 4 Dec 2023 15:57:02 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 04 Dec 2023 15:57:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723422; x= 1701730622; bh=Z+7Afm7t3HxDVtoQyXLZsXvB2QVInlcd4KmkW3EtfRk=; b=B HnJ+fx+0FdYXhJNFTepTS8dgk8/FV5xe82ChHPFkZM0vgsOl7kqB/LJGQNwKSEgQ gWnEcUfwsJWB3q7ylWh9bqsyiw0G0azF415Y70zo/7h8fZfhQUCSNjuJroHHjdDY yjqiDbvYRQ6ojKe0QH48phnhnqjYcDunfnyr1VK+mraWa8JB6s6dE8F9NrgC/Oqx LZIiP8x8f6tnuHqAEMtpn6K6z1/UiVw7hxr0Mx0Maf0kUXIK00y3unwl5ptm/xmV zmrmpf7YNAwqrPu50dOAZ07Uui10fF6SzqG7z/xDSIELv7Fvmu4p7vLwzJ8XpqN8 eIltoRmlbaXEdBRteIdvA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723422; x= 1701730622; bh=Z+7Afm7t3HxDVtoQyXLZsXvB2QVInlcd4KmkW3EtfRk=; b=X 8zDzbqlktm9phtZLiEx7t2HmUnqILJP9B7xAZglUWUssyZU6OyfOHH+K9FOUdvuZ VI6kxzuXEX52AuAmUgdKD7XK8QVsqJYqvxORM+k130h8dUuNckTF5R6iKwc0sC84 WOMu0/skQER+Gm7d55kc2/6TxgYwztXxlS66LoXAVXTkf/hHERS5eHQJKwEakNgj OB/eNCzZMzLyroQEKXeBL1nzJIdbj09iSqjZxNKcE9bVIx7L7SRe+JVhjKB0C2nc rID7r3Y9U7uEat3dQ4jY2pnlUHFh39Ry55RCd3y+SGIf4/TObNsrGB54Ju55Hf5Q KOeF6jyYw53IuHvKg4E7g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdefhedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:57:00 -0500 (EST) From: Daniel Xu To: daniel@iogearbox.net, shuah@kernel.org, andrii@kernel.org, ast@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: mykolal@fb.com, martin.lau@linux.dev, song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, devel@linux-ipsec.org, netdev@vger.kernel.org Subject: [PATCH bpf-next v4 05/10] bpf: selftests: test_loader: Support __btf_path() annotation Date: Mon, 4 Dec 2023 13:56:25 -0700 Message-ID: <16ffaa4173791228ed2274c1d06c0eaf159f5d9c.1701722991.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:57:32 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386371963610466 X-GMAIL-MSGID: 1784386371963610466 This commit adds support for per-prog btf_custom_path. This is necessary for testing CO-RE relocations on non-vmlinux types using test_loader infrastructure. Acked-by: Andrii Nakryiko Signed-off-by: Daniel Xu --- tools/testing/selftests/bpf/progs/bpf_misc.h | 1 + tools/testing/selftests/bpf/test_loader.c | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/tools/testing/selftests/bpf/progs/bpf_misc.h b/tools/testing/selftests/bpf/progs/bpf_misc.h index 799fff4995d8..2fd59970c43a 100644 --- a/tools/testing/selftests/bpf/progs/bpf_misc.h +++ b/tools/testing/selftests/bpf/progs/bpf_misc.h @@ -71,6 +71,7 @@ #define __retval_unpriv(val) __attribute__((btf_decl_tag("comment:test_retval_unpriv="#val))) #define __auxiliary __attribute__((btf_decl_tag("comment:test_auxiliary"))) #define __auxiliary_unpriv __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv"))) +#define __btf_path(path) __attribute__((btf_decl_tag("comment:test_btf_path=" path))) /* Convenience macro for use with 'asm volatile' blocks */ #define __naked __attribute__((naked)) diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c index a350ecdfba4a..74ceb7877ae2 100644 --- a/tools/testing/selftests/bpf/test_loader.c +++ b/tools/testing/selftests/bpf/test_loader.c @@ -27,6 +27,7 @@ #define TEST_TAG_RETVAL_PFX_UNPRIV "comment:test_retval_unpriv=" #define TEST_TAG_AUXILIARY "comment:test_auxiliary" #define TEST_TAG_AUXILIARY_UNPRIV "comment:test_auxiliary_unpriv" +#define TEST_BTF_PATH "comment:test_btf_path=" /* Warning: duplicated in bpf_misc.h */ #define POINTER_VALUE 0xcafe4all @@ -58,6 +59,7 @@ struct test_spec { const char *prog_name; struct test_subspec priv; struct test_subspec unpriv; + const char *btf_custom_path; int log_level; int prog_flags; int mode_mask; @@ -288,6 +290,8 @@ static int parse_test_spec(struct test_loader *tester, goto cleanup; update_flags(&spec->prog_flags, flags, clear); } + } else if (str_has_pfx(s, TEST_BTF_PATH)) { + spec->btf_custom_path = s + sizeof(TEST_BTF_PATH) - 1; } } @@ -578,6 +582,9 @@ void run_subtest(struct test_loader *tester, } } + /* Implicitly reset to NULL if next test case doesn't specify */ + open_opts->btf_custom_path = spec->btf_custom_path; + tobj = bpf_object__open_mem(obj_bytes, obj_byte_cnt, open_opts); if (!ASSERT_OK_PTR(tobj, "obj_open_mem")) /* shouldn't happen */ goto subtest_cleanup; From patchwork Mon Dec 4 20:56:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173560 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033395vqy; Mon, 4 Dec 2023 12:57:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHydtVwhKpZ8DQ6exCRKFn0A0FOZJNjFOSIcROm2dEEf7CGwL1OUcMtFFYVq2WYxdsCI7Q8 X-Received: by 2002:a17:902:6902:b0:1d0:6ffe:1e8d with SMTP id j2-20020a170902690200b001d06ffe1e8dmr2307476plk.112.1701723466898; Mon, 04 Dec 2023 12:57:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723466; cv=none; d=google.com; s=arc-20160816; b=Vejf8RyopfcIoIBfHq8p8L4hfUPE26FV8uDsdZ5gXu/SpJLcm5AdqVnPdRZnIWzu9E bSBv7FfUdsnzsqZHB1z8uGdqolMpcp2OSgWH8vNmt5KzSxrJo5z5jSQaO6U7B/CpIDbs CFTZS4Qa1cSWQLWASH4GFED01UGydcQw77FnDoqOqkPAvvtAMAEMqOlIBtpAj9G838+g sf0/i2okO2/7bgeOg4CxK8uhIwn1rkZauE8Dkyfgi865faQaH6y9KVWy8FLF+jmX4ucE Qeso4VFXT3rZWh8WWH4eAU9UwhVDKFl0cS32aHsMz4vtqS8eb139Y/rng/UQlwg9hhk1 jUow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=/AYNOAxNgVK/QVMc0AlL/YbO7UaSJXnY0NdrYpUsy9Q=; fh=xIF657EBHaDzCTTPigB7jMYtV72qZK+LdEjX4dC+Jmc=; b=m4WxoIv8SW9i3j780qWt5QCNnzsTNjPaOWoODftspW3vkTQapF1VKFwBeSjLfKK+ES 2SEY6XxgY8gAKEBZBNO3EsNm8rzUaZFyfhdVvbiTLGg2QCaORTP/qat9QCVtH/ClLLnq qRny8lf1iswLsFixtmtLcLZpBEOgiXmta5GPv5dqdtPxAkJrWdMdajvcJQS69663rlQr YitidRlPuBScc5KFUrFwxEHm8cVQR2tgZBB9Xyahdw47vWNSojrmiln+rwWPW73pHash rXHZE3ZQXHx7YppZ/GSikTmMFjaEWSDJhJ4oIVwI0zBWpNtkBy7LzkrKztvsd35/LpET Z1iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=iaWCqT43; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=zTSs0rUb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id h6-20020a170902704600b001c3411c9b83si8361547plt.454.2023.12.04.12.57.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:57:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=iaWCqT43; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=zTSs0rUb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 8348880BDE39; Mon, 4 Dec 2023 12:57:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234040AbjLDU5c (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234039AbjLDU5O (ORCPT ); Mon, 4 Dec 2023 15:57:14 -0500 Received: from new4-smtp.messagingengine.com (new4-smtp.messagingengine.com [66.111.4.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7046D109; Mon, 4 Dec 2023 12:57:04 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.nyi.internal (Postfix) with ESMTP id D52F5580A24; Mon, 4 Dec 2023 15:57:03 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 04 Dec 2023 15:57:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723423; x= 1701730623; bh=/AYNOAxNgVK/QVMc0AlL/YbO7UaSJXnY0NdrYpUsy9Q=; b=i aWCqT43SnhJ5KTCoNaEy0tR9bDLEH3vIzv7YXNoSTng5i9R21EoQhYYRDL39YTyx 14I32NFORjRllKNYrAK4XvlR3SJiTZTFF4sPF3ZxSd6sUWPILhIl070EKuY9GZDN D5GE67zTCF/9PMeJ2EYeN8B0YHa7nA82869B7hTQHI7lJtPdr6YAK0gy4tIgTggB hVfYJK7PDGT5lBWRB/Ag7+pypY+wPe3YqWUspkzlRI/AZJM+Bvdq7dTEGofPIbe6 /PUbTYGVBWZvxeiS7gHv0xWz4PHjhc/GvXqOJYaqjxuAkxAahVkTQNCYp14dh6Ku HaFwt/UpJSrj+tPmRDqdA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723423; x= 1701730623; bh=/AYNOAxNgVK/QVMc0AlL/YbO7UaSJXnY0NdrYpUsy9Q=; b=z TSs0rUbTwxt2RjQKOSbpuhsKpMFA+3+x1XBr1ZxqJnJeRDQ5A30omOfDcDl/O0jr aMcmuVWHQNmr+KoKjOjhhqTT+boPx9eUi5gz39A4Axere3B5oR4BWIAsU/APi4Vt r2z777pJuknmjtLkCKXlWYFoN0vk7fEN1x4XU4GEhlkWUgL4eUMf9e66RzQ+fFya blNuxbaIRAPyfQnYJgFLm1SfKrbXpTP+14htQEXZzsTOgmnEC0aAG0Ogi9rbx9im epn+L/fpu+Fh+tdh4WNGDqFqjdsOhF4zqsPQqTQwTr9hYl6JH6DSGGBvwRA0+37Z M451XpXZZDP2h2QiugtlA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdefhedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:57:02 -0500 (EST) From: Daniel Xu To: daniel@iogearbox.net, shuah@kernel.org, andrii@kernel.org, ast@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: mykolal@fb.com, martin.lau@linux.dev, song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, devel@linux-ipsec.org, netdev@vger.kernel.org Subject: [PATCH bpf-next v4 06/10] libbpf: selftests: Add verifier tests for CO-RE bitfield writes Date: Mon, 4 Dec 2023 13:56:26 -0700 Message-ID: X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:57:44 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386386154073590 X-GMAIL-MSGID: 1784386386154073590 Add some tests that exercise BPF_CORE_WRITE_BITFIELD() macro. Since some non-trivial bit fiddling is going on, make sure various edge cases (such as adjacent bitfields and bitfields at the edge of structs) are exercised. Acked-by: Andrii Nakryiko Signed-off-by: Daniel Xu --- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_bitfield_write.c | 100 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_bitfield_write.c diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c index 8d746642cbd7..ac49ec25211d 100644 --- a/tools/testing/selftests/bpf/prog_tests/verifier.c +++ b/tools/testing/selftests/bpf/prog_tests/verifier.c @@ -6,6 +6,7 @@ #include "verifier_and.skel.h" #include "verifier_array_access.skel.h" #include "verifier_basic_stack.skel.h" +#include "verifier_bitfield_write.skel.h" #include "verifier_bounds.skel.h" #include "verifier_bounds_deduction.skel.h" #include "verifier_bounds_deduction_non_const.skel.h" @@ -116,6 +117,7 @@ static void run_tests_aux(const char *skel_name, void test_verifier_and(void) { RUN(verifier_and); } void test_verifier_basic_stack(void) { RUN(verifier_basic_stack); } +void test_verifier_bitfield_write(void) { RUN(verifier_bitfield_write); } void test_verifier_bounds(void) { RUN(verifier_bounds); } void test_verifier_bounds_deduction(void) { RUN(verifier_bounds_deduction); } void test_verifier_bounds_deduction_non_const(void) { RUN(verifier_bounds_deduction_non_const); } diff --git a/tools/testing/selftests/bpf/progs/verifier_bitfield_write.c b/tools/testing/selftests/bpf/progs/verifier_bitfield_write.c new file mode 100644 index 000000000000..623f130a3198 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/verifier_bitfield_write.c @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +#include +#include + +#include "bpf_misc.h" + +struct core_reloc_bitfields { + /* unsigned bitfields */ + uint8_t ub1: 1; + uint8_t ub2: 2; + uint32_t ub7: 7; + /* signed bitfields */ + int8_t sb4: 4; + int32_t sb20: 20; + /* non-bitfields */ + uint32_t u32; + int32_t s32; +} __attribute__((preserve_access_index)); + +SEC("tc") +__description("single CO-RE bitfield roundtrip") +__btf_path("btf__core_reloc_bitfields.bpf.o") +__success +__retval(3) +int single_field_roundtrip(struct __sk_buff *ctx) +{ + struct core_reloc_bitfields bitfields; + + __builtin_memset(&bitfields, 0, sizeof(bitfields)); + BPF_CORE_WRITE_BITFIELD(&bitfields, ub2, 3); + return BPF_CORE_READ_BITFIELD(&bitfields, ub2); +} + +SEC("tc") +__description("multiple CO-RE bitfield roundtrip") +__btf_path("btf__core_reloc_bitfields.bpf.o") +__success +__retval(0x3FD) +int multiple_field_roundtrip(struct __sk_buff *ctx) +{ + struct core_reloc_bitfields bitfields; + uint8_t ub2; + int8_t sb4; + + __builtin_memset(&bitfields, 0, sizeof(bitfields)); + BPF_CORE_WRITE_BITFIELD(&bitfields, ub2, 1); + BPF_CORE_WRITE_BITFIELD(&bitfields, sb4, -1); + + ub2 = BPF_CORE_READ_BITFIELD(&bitfields, ub2); + sb4 = BPF_CORE_READ_BITFIELD(&bitfields, sb4); + + return (((uint8_t)sb4) << 2) | ub2; +} + +SEC("tc") +__description("adjacent CO-RE bitfield roundtrip") +__btf_path("btf__core_reloc_bitfields.bpf.o") +__success +__retval(7) +int adjacent_field_roundtrip(struct __sk_buff *ctx) +{ + struct core_reloc_bitfields bitfields; + uint8_t ub1, ub2; + + __builtin_memset(&bitfields, 0, sizeof(bitfields)); + BPF_CORE_WRITE_BITFIELD(&bitfields, ub1, 1); + BPF_CORE_WRITE_BITFIELD(&bitfields, ub2, 3); + + ub1 = BPF_CORE_READ_BITFIELD(&bitfields, ub1); + ub2 = BPF_CORE_READ_BITFIELD(&bitfields, ub2); + + return (ub2 << 1) | ub1; +} + +SEC("tc") +__description("multibyte CO-RE bitfield roundtrip") +__btf_path("btf__core_reloc_bitfields.bpf.o") +__success +__retval(0x21) +int multibyte_field_roundtrip(struct __sk_buff *ctx) +{ + struct core_reloc_bitfields bitfields; + uint32_t ub7; + uint8_t ub1; + + __builtin_memset(&bitfields, 0, sizeof(bitfields)); + BPF_CORE_WRITE_BITFIELD(&bitfields, ub1, 1); + BPF_CORE_WRITE_BITFIELD(&bitfields, ub7, 16); + + ub1 = BPF_CORE_READ_BITFIELD(&bitfields, ub1); + ub7 = BPF_CORE_READ_BITFIELD(&bitfields, ub7); + + return (ub7 << 1) | ub1; +} + +char _license[] SEC("license") = "GPL"; From patchwork Mon Dec 4 20:56:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173561 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033471vqy; Mon, 4 Dec 2023 12:57:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IH94b2gyqty9IeBfqFUQYRVPI5noW65fpFyf2hQhF8qIxuCKQ/SrLmoFYvLRdWCMjt/zTxH X-Received: by 2002:a05:6a20:979b:b0:18b:208b:7043 with SMTP id hx27-20020a056a20979b00b0018b208b7043mr4554107pzc.49.1701723478416; Mon, 04 Dec 2023 12:57:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723478; cv=none; d=google.com; s=arc-20160816; b=iU0V8wRSoUoLZmpktbMu6j+i1StqS5Aq2JDdmNPxfeJv8ye1roeJEWGnLtzoiVKNT+ XvNAX2+jHe+734BZoO0YoySsICJcpOm6eCzHo4lLiHbWp14clyPjPx85/wmXXCkJYCLc b1zscTlncawOA+A5Lt+noYeznPg7o27J/4DMMXUWDFZRS/8OJ/t13imcRic8np7aYyR5 2ZDHCZIyialPbWiaoodhM67raHog784ceoAPYvoEyZK7IUn3FgKTYAdLLj0AISkOapGx 6Sgw48wN10V8NClSfDYH7gzMF+qAEkkp+VeOWolyZrZ1EJq3RL1Ugqr2kWQH8c/8yq+/ sBUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=mulrQKzv1eO6blHCVRuuCfG2CgE/rU/CR7wkoBWNDIE=; fh=MAo5rn/iZDhyZgoYtcxdejdXZMUS1JnKG5NSUpwlKTs=; b=UrdDP9KxjcXSJzkww9YKdLqyh3JCk1MuNlk/zfHMn/Q2mufD7ns+1ThUWVH3QPIDvq uz6owRrnLDCYtog+AmFQhvo0q8XZPup9AkI6qpAC+BDD3VXjqckyhjUaVXTbRtdNbtnB crjIz1BHQqaGI0heXesmZsAy2ZB1pHzqQSt2NesgK00K7bjqUebXpDHOe9CXvDmlvDzI QRuyxxFWudu64rmO/+AUCbuhdIHMzebbuHURRR3gmpf0AD2ttDCff4+zeC9kZ7cda+cU 4fMQXronUIUKrahBiUP7AdO9yUjk6goNbMW6+XWEhDOeDtU7MDSOpOu4lluZZ9n102oB 5NBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=AgScb7V5; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=F1MPREaG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id h2-20020a655182000000b005c64e8141b8si5167056pgq.101.2023.12.04.12.57.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:57:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=AgScb7V5; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=F1MPREaG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 10DE780D0CF5; Mon, 4 Dec 2023 12:57:55 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233697AbjLDU5f (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235485AbjLDU5Q (ORCPT ); Mon, 4 Dec 2023 15:57:16 -0500 Received: from new4-smtp.messagingengine.com (new4-smtp.messagingengine.com [66.111.4.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87A2010F7; Mon, 4 Dec 2023 12:57:06 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id BEB83580A68; Mon, 4 Dec 2023 15:57:05 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 04 Dec 2023 15:57:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723425; x= 1701730625; bh=mulrQKzv1eO6blHCVRuuCfG2CgE/rU/CR7wkoBWNDIE=; b=A gScb7V5WIAslO1HjIrDWIOvb5iJD0GhTZulvOD+5k76bLl/jucRiHMlIu7WfI6De bPnTmR7sjhdFLDd2SvI1DdTJs3gRwJAUVW46A1cR7J5HdEG1behaz28XyUgTdD6c tvDfCWxGOmOR2ekl8i+yJSBo0pMHeYNns26q32sfztNUFmiR5KFsi5DiRuqBPGPB iL0RsqdaY8j4BrXJx1k8hrD7HeoyabdYFdvXn0ulv4HBWlr2kie4HDCPg77lQul0 nYMzk4BAj7f7mDC3cB9Z5RKHIkh52ziYDGnuUUK5vKB7TBJBICk4x/Gn6Tk836wi ukQtLkXsYC0DQ4G2to9DA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723425; x= 1701730625; bh=mulrQKzv1eO6blHCVRuuCfG2CgE/rU/CR7wkoBWNDIE=; b=F 1MPREaGpmSLEewYF+SIQtBc2H3li+0vIj2Y3sRjnMdlfM7ps+UHfEyd2TG8ayUKX tPdozUI7cRf7ULWMvF86ox011b26p9y1v56szjGo0tsXZg+kItWKq22If5q8tysv wIHZWEADuEa8jcLn4Zpvs9VaTGQgxZSAFQM8dEdzxImnD/ZvZwwWvmTa021AKX7F 6FMckoe1ba9JDZ2sCKY2+0UBDsfFgfuF71eW0gEFUSgnS7X95YGbNobFOdDUOx7c wi2g/BbWzzBeJbTysRYj50KGK1ANeFn1q+GWQBH5qDrUunc9BAYInzWOkLM/c8UB 34OEgpMS9jZKFQcFwq5/g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdejtddmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:57:03 -0500 (EST) From: Daniel Xu To: daniel@iogearbox.net, shuah@kernel.org, andrii@kernel.org, ast@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: martin.lau@linux.dev, song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, devel@linux-ipsec.org, netdev@vger.kernel.org Subject: [PATCH bpf-next v4 07/10] bpf: selftests: test_tunnel: Setup fresh topology for each subtest Date: Mon, 4 Dec 2023 13:56:27 -0700 Message-ID: <5292165f1f64883b5fc6354a9a24fb8074e3c2c6.1701722991.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:57:55 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386397881877719 X-GMAIL-MSGID: 1784386397881877719 This helps with determinism b/c individual setup/teardown prevents leaking state between different subtests. Signed-off-by: Daniel Xu --- tools/testing/selftests/bpf/prog_tests/test_tunnel.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index d149ab98798d..b57d48219d0b 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c @@ -535,23 +535,20 @@ static void test_ipip_tunnel(enum ipip_encap encap) #define RUN_TEST(name, ...) \ ({ \ if (test__start_subtest(#name)) { \ + config_device(); \ test_ ## name(__VA_ARGS__); \ + cleanup(); \ } \ }) static void *test_tunnel_run_tests(void *arg) { - cleanup(); - config_device(); - RUN_TEST(vxlan_tunnel); RUN_TEST(ip6vxlan_tunnel); RUN_TEST(ipip_tunnel, NONE); RUN_TEST(ipip_tunnel, FOU); RUN_TEST(ipip_tunnel, GUE); - cleanup(); - return NULL; } From patchwork Mon Dec 4 20:56:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173562 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033537vqy; Mon, 4 Dec 2023 12:58:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IEP8kmTyeyn3JUVpyxW+V4ESZtmlJuUDbM+fqWlxjg3jG8Ht/6PoGf71i0mnkVxzHxBk4bo X-Received: by 2002:a05:6a00:2d1b:b0:6cd:f6e6:2f4e with SMTP id fa27-20020a056a002d1b00b006cdf6e62f4emr223058pfb.4.1701723490175; Mon, 04 Dec 2023 12:58:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723490; cv=none; d=google.com; s=arc-20160816; b=xqC4T+IC6xq7PT5pnlHGmy2BZE/Sipplsf4RmfHWOncQkUYPq38oq7OQWvdkznn5vh BQrcyHPObepAtOtXdIQ+lAV3dxmI8m4q37F2yOASG2zVrP7yYeSFJrbqPblwv5Uwe1WJ ijycr4xXRNp4z4ef0yDWtZN4AYklGUDOT7xk92vR4TCZgG6fK5zfjKiNmB8EYr7diIfp GWtSodANjr5Si/vaqpI5q/6uy6xHbV+prEHOKPfo3AR0TpF7kIPhKJC5Vte/SjPEKgp/ DCZfx/OBD/OIEJgQMhzEoe0B+VxW67KaNlcEkK+/yWXqb+u/MuzrIaErCuEMFTGxjZhO 7qAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=9Zj48xwH3RKj/ArgA1aCuMUGNgo6h0Y1fWoiEhljy1A=; fh=MAo5rn/iZDhyZgoYtcxdejdXZMUS1JnKG5NSUpwlKTs=; b=S+g2Wv121E1wm7/UH7qTHpGp0VZGLQCYSeQSk6Y7+pcEZ9NiP5aob5jBIhaIpgehu7 4NkC5jSo32r77zmE1ECmxQlKgHFvGWE3c1GWrLHPIAaTZOStPXWiFaT2yrpG7TPC3cGQ TFB3td8Nqap+70P3dOEzcAU8Te/pd0QobnsdFSch9oAuEfIwbVZw6I6J+xWK/QMgt68X EcjWLVy7jhkZw6G05hforaWMuY8I5d1edHQ7W5ZSRsYSZEXy2Sr+d6fbYGQMMqZEIczg aAbvSIVJpa8QLbn2kvknONP9Ce/m8WfF9NSx7tAI/NzuJUMuQS4bSgP4tWbJR4AKoZuF h7wQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=YRNFHjWu; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=hA9LeaKx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id x28-20020a634a1c000000b00585a5e9a965si8253634pga.161.2023.12.04.12.58.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:58:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=YRNFHjWu; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=hA9LeaKx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 78B868034A51; Mon, 4 Dec 2023 12:58:06 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235508AbjLDU5o (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233774AbjLDU5R (ORCPT ); Mon, 4 Dec 2023 15:57:17 -0500 Received: from new4-smtp.messagingengine.com (new4-smtp.messagingengine.com [66.111.4.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 574CF170E; Mon, 4 Dec 2023 12:57:08 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailnew.nyi.internal (Postfix) with ESMTP id B4F42580A25; Mon, 4 Dec 2023 15:57:07 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 04 Dec 2023 15:57:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723427; x= 1701730627; bh=9Zj48xwH3RKj/ArgA1aCuMUGNgo6h0Y1fWoiEhljy1A=; b=Y RNFHjWuvz62cHaHBqNVtt23HjB9DJfbsIsMz5mtvQYCOAbgZ4Sq+ZJgIlxE2e8a9 P4tflkyTht3suLl8DfCLTpLMgLvOeORxcs3n2KwUeHMUIlqvWUM3xnEnene+kmI1 hEyo5H4ngR9VIg3JyirTN7v8L8PXa8+HHQNsLkbAU+k+haWrowYdlwua74ep4P1k 0foMFFqDDlBrxxJlHWzGD1oUjLjgk4cSlg/7T6WTP56z6UWbqNjBKS+4kIPkEqoS 8XZYNKxoOnzTIw961M3EiY5XSa5ke9qtgtG80HJQyClK0qiFq5B+m4O9sZIql62s xSla47eugTRO8cet5NT/A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723427; x= 1701730627; bh=9Zj48xwH3RKj/ArgA1aCuMUGNgo6h0Y1fWoiEhljy1A=; b=h A9LeaKxKwqWVu3TTqKfQeBpC/Bcs75pl9s20RcGpHsDQ2Hq75t4QeT6RuAXfo9Yb SNo8Kwgs9YqY609+nbgoIqIvAfFrBkSGOoGGBiMs5xRe1QyC7b5ii0+hQJjjpE1K S1mosax2Y9pJtBt4ojIvd1s/4Vst87ikhBLQwLi282kQBxmP2oWKtlTwAhHiyE0J XIFp+zgbGPyf2Cvs6iVJysg3P0i83K09/jC0gCJZtz+8tt5awUL2Ol28vpy/QQlD Gx3As31z6/8MrA65EKx7ffcgAoFbWMWvIr6bI8EFjg8B/QQ7oKCr39YpfSHTiSl5 d1gKbaLT6vcTQAQTA/sKg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdefhedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:57:05 -0500 (EST) From: Daniel Xu To: daniel@iogearbox.net, shuah@kernel.org, andrii@kernel.org, ast@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: martin.lau@linux.dev, song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, devel@linux-ipsec.org, netdev@vger.kernel.org Subject: [PATCH bpf-next v4 08/10] bpf: selftests: test_tunnel: Use vmlinux.h declarations Date: Mon, 4 Dec 2023 13:56:28 -0700 Message-ID: <325a9b767f4e0c2acf2f66dafd3e16d560f760de.1701722991.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:58:06 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386410490863143 X-GMAIL-MSGID: 1784386410490863143 vmlinux.h declarations are more ergnomic, especially when working with kfuncs. The uapi headers are often incomplete for kfunc definitions. This commit also switches bitfield accesses to use CO-RE helpers. Switching to vmlinux.h definitions makes the verifier very unhappy with raw bitfield accesses. The error is: ; md.u.md2.dir = direction; 33: (69) r1 = *(u16 *)(r2 +11) misaligned stack access off (0x0; 0x0)+-64+11 size 2 Fix by using CO-RE-aware bitfield reads and writes. Co-developed-by: Antony Antony Signed-off-by: Antony Antony Signed-off-by: Daniel Xu --- .../selftests/bpf/progs/bpf_tracing_net.h | 1 + .../selftests/bpf/progs/test_tunnel_kern.c | 76 +++++-------------- 2 files changed, 22 insertions(+), 55 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/bpf_tracing_net.h b/tools/testing/selftests/bpf/progs/bpf_tracing_net.h index 0b793a102791..1bdc680b0e0e 100644 --- a/tools/testing/selftests/bpf/progs/bpf_tracing_net.h +++ b/tools/testing/selftests/bpf/progs/bpf_tracing_net.h @@ -26,6 +26,7 @@ #define IPV6_AUTOFLOWLABEL 70 #define TC_ACT_UNSPEC (-1) +#define TC_ACT_OK 0 #define TC_ACT_SHOT 2 #define SOL_TCP 6 diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c index f66af753bbbb..b320fb7bb080 100644 --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c @@ -6,62 +6,26 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "vmlinux.h" +#include #include #include +#include "bpf_kfuncs.h" +#include "bpf_tracing_net.h" #define log_err(__ret) bpf_printk("ERROR line:%d ret:%d\n", __LINE__, __ret) -#define VXLAN_UDP_PORT 4789 +#define VXLAN_UDP_PORT 4789 +#define ETH_P_IP 0x0800 +#define PACKET_HOST 0 +#define TUNNEL_CSUM bpf_htons(0x01) +#define TUNNEL_KEY bpf_htons(0x04) /* Only IPv4 address assigned to veth1. * 172.16.1.200 */ #define ASSIGNED_ADDR_VETH1 0xac1001c8 -struct geneve_opt { - __be16 opt_class; - __u8 type; - __u8 length:5; - __u8 r3:1; - __u8 r2:1; - __u8 r1:1; - __u8 opt_data[8]; /* hard-coded to 8 byte */ -}; - -struct vxlanhdr { - __be32 vx_flags; - __be32 vx_vni; -} __attribute__((packed)); - -struct vxlan_metadata { - __u32 gbp; -}; - -struct bpf_fou_encap { - __be16 sport; - __be16 dport; -}; - -enum bpf_fou_encap_type { - FOU_BPF_ENCAP_FOU, - FOU_BPF_ENCAP_GUE, -}; - int bpf_skb_set_fou_encap(struct __sk_buff *skb_ctx, struct bpf_fou_encap *encap, int type) __ksym; int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx, @@ -205,9 +169,9 @@ int erspan_set_tunnel(struct __sk_buff *skb) __u8 hwid = 7; md.version = 2; - md.u.md2.dir = direction; - md.u.md2.hwid = hwid & 0xf; - md.u.md2.hwid_upper = (hwid >> 4) & 0x3; + BPF_CORE_WRITE_BITFIELD(&md.u.md2, dir, direction); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid, (hwid & 0xf)); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid_upper, (hwid >> 4) & 0x3); #endif ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); @@ -246,8 +210,9 @@ int erspan_get_tunnel(struct __sk_buff *skb) bpf_printk("\tindex %x\n", index); #else bpf_printk("\tdirection %d hwid %x timestamp %u\n", - md.u.md2.dir, - (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, + BPF_CORE_READ_BITFIELD(&md.u.md2, dir), + (BPF_CORE_READ_BITFIELD(&md.u.md2, hwid_upper) << 4) + + BPF_CORE_READ_BITFIELD(&md.u.md2, hwid), bpf_ntohl(md.u.md2.timestamp)); #endif @@ -284,9 +249,9 @@ int ip4ip6erspan_set_tunnel(struct __sk_buff *skb) __u8 hwid = 17; md.version = 2; - md.u.md2.dir = direction; - md.u.md2.hwid = hwid & 0xf; - md.u.md2.hwid_upper = (hwid >> 4) & 0x3; + BPF_CORE_WRITE_BITFIELD(&md.u.md2, dir, direction); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid, (hwid & 0xf)); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid_upper, (hwid >> 4) & 0x3); #endif ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); @@ -326,8 +291,9 @@ int ip4ip6erspan_get_tunnel(struct __sk_buff *skb) bpf_printk("\tindex %x\n", index); #else bpf_printk("\tdirection %d hwid %x timestamp %u\n", - md.u.md2.dir, - (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, + BPF_CORE_READ_BITFIELD(&md.u.md2, dir), + (BPF_CORE_READ_BITFIELD(&md.u.md2, hwid_upper) << 4) + + BPF_CORE_READ_BITFIELD(&md.u.md2, hwid), bpf_ntohl(md.u.md2.timestamp)); #endif From patchwork Mon Dec 4 20:56:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 173564 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3033683vqy; Mon, 4 Dec 2023 12:58:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IGmyXsxbseuJhr394DMH99OAJGQb2Aju/7dmimnuRR+3x7KRrtW5x/IbBaGGstCNfSqJd8R X-Received: by 2002:a05:6a21:6da0:b0:18f:e1f:32ce with SMTP id wl32-20020a056a216da000b0018f0e1f32cemr5282479pzb.115.1701723509521; Mon, 04 Dec 2023 12:58:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701723509; cv=none; d=google.com; s=arc-20160816; b=nuczkhU3R7T19RU53/r4+rpSka/Rm3ikbTQ39BLjFOtfRWwBW6wqCZIMWm1zRYqYmp h83vM4+ADtCRGBMiEWYUgMJDvx7PpZ2IQNjkqoDYVX5zUhDcwOta5Bt5R/bJzhKWQkp2 /kBeNdN0YXi3Ekelxjwwchoae4nW/AARcCnCE6GokGfvj4/O1jZuys04Ig3xpnhB4XXK WJDtEquAY6f1RnQs8KSagRf8CJiLkJFwnYkgUjiE8T4Mxde9GXYdijSUsb817UV1TlsC 8pqVm+r25XXqReMVR1yR28UADHgmiBXPRIjeyE6pOUOKjEp0JtYgn9qvRr4C09rz6S1e 0Guw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :feedback-id:dkim-signature:dkim-signature; bh=Xy5DWhMcuaQWz9hFzj8IU9Hd0UISUG5+/EyW9nquCao=; fh=nFQ0kexnShkqY52BbEtBgUSIC28o0KYPsJlEetq4css=; b=xwRvBTgAqkaQ6eb/gFgM7p5ZwaANSpRjcLjGezJpLqDbHYlJKs4NG8OtLMkEk/Ju67 Ck4dn2MoNdJlVDS9+D8Q/8d3ZUUL2jWCzGaHGfkVxkT2be9/6kqFc36yalGcHVcI2gs7 fhs5Dwhvk6TWN4v0Wu1xvjWQR+J5VNuc3f4AzKqZUuTHxInRLoU6nVVRI2vjuZJblren 0fzTmBpr+yTYbw04tSzqSXnOZ9pF00ySoMD7XdcKCyZs1PyQgdmDPgn722GDy6jC8Mxe PTAwcb9VAiRe9nJ7SxZFBSo2+Smak36o6sagA+yopCtc2sAxhL5LXlfxpitCZmx29/74 9xqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=fQoXufqY; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=a0G1dJFA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id b19-20020a6567d3000000b005bddb4be6fcsi3754299pgs.520.2023.12.04.12.58.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Dec 2023 12:58:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=fQoXufqY; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=a0G1dJFA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 6C8388034A4D; Mon, 4 Dec 2023 12:58:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233724AbjLDU56 (ORCPT + 99 others); Mon, 4 Dec 2023 15:57:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235458AbjLDU5a (ORCPT ); Mon, 4 Dec 2023 15:57:30 -0500 Received: from new4-smtp.messagingengine.com (new4-smtp.messagingengine.com [66.111.4.230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E456173B; Mon, 4 Dec 2023 12:57:12 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailnew.nyi.internal (Postfix) with ESMTP id 90418580A6A; Mon, 4 Dec 2023 15:57:11 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 04 Dec 2023 15:57:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701723431; x= 1701730631; bh=Xy5DWhMcuaQWz9hFzj8IU9Hd0UISUG5+/EyW9nquCao=; b=f QoXufqYcQIqHDb6el7DTUJd0fBEdJBUVU5fChFUUxqhhNptIPWA2GRfZjS5By+kM TSZmQy0jNI4S4C3Xzcf3SQxzfJaIp/4rzSJwhtM3BLXoAcPtTYwhRdUtsc7Cq+YQ /vNbJhiEN+Ih45VmOsGphKUYIfa4A4q/ZPylcUTjClRfYMa/DICvhVYDcC4HIZ8H K8hgEexMFRkSPklKC373I8YYyKagom68fe9bWDqJZIEI3n92ydmOfPDPNJKSS9tb e4gjLLKfV8FYwNwiYqCqvEtH5YJKa3aMV4e1WyPkqejMUzTinX5ew+VlBFTNVdSv 7Em7dEUtrLlCGLAo/lEkQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701723431; x= 1701730631; bh=Xy5DWhMcuaQWz9hFzj8IU9Hd0UISUG5+/EyW9nquCao=; b=a 0G1dJFA48CvQe6gUE0On96JanSsLf7wueO3F1B4613z0EoYzIg2yB63UoGRCsIsQ NHF3yMLdOovVFVpDNXfv1ncdAlBYvn2Zns/c/XcqOYOc9+Z7y3YQYE9pvp6pVlhf FYyPUI/egIzvJMLHKr/1/CPKLENAfHBOLsbJH5XcwoxrE+8u6du607mPmAE+3HLv BzrhJinolZmi0agqWryGkTo/eGouP5Hzkhr6GJIjXTE4+HXreHRA6NVoIFWqMctv gPaZFilQ9twbCtVNEIFHXehy43p3fzOp/Ryynnqo1IOhwaVTd0/X70xKevH4cZKD mkJXAU7wBQtbF5mIEKDVw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejiedgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdefhedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 4 Dec 2023 15:57:09 -0500 (EST) From: Daniel Xu To: daniel@iogearbox.net, ast@kernel.org, davem@davemloft.net, hawk@kernel.org, john.fastabend@gmail.com, andrii@kernel.org, kuba@kernel.org, shuah@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: mykolal@fb.com, martin.lau@linux.dev, song@kernel.org, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, devel@linux-ipsec.org Subject: [PATCH bpf-next v4 10/10] bpf: xfrm: Add selftest for bpf_xdp_get_xfrm_state() Date: Mon, 4 Dec 2023 13:56:30 -0700 Message-ID: X-Mailer: git-send-email 2.42.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 04 Dec 2023 12:58:26 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784386430737663427 X-GMAIL-MSGID: 1784386430737663427 This commit extends test_tunnel selftest to test the new XDP xfrm state lookup kfunc. Co-developed-by: Antony Antony Signed-off-by: Antony Antony Signed-off-by: Daniel Xu --- .../selftests/bpf/prog_tests/test_tunnel.c | 20 ++++++-- .../selftests/bpf/progs/test_tunnel_kern.c | 51 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index 2d7f8fa82ebd..fc804095d578 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c @@ -278,7 +278,7 @@ static int add_xfrm_tunnel(void) SYS(fail, "ip netns exec at_ns0 " "ip xfrm state add src %s dst %s proto esp " - "spi %d reqid 1 mode tunnel " + "spi %d reqid 1 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s", IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT, XFRM_AUTH, XFRM_ENC); SYS(fail, @@ -292,7 +292,7 @@ static int add_xfrm_tunnel(void) SYS(fail, "ip netns exec at_ns0 " "ip xfrm state add src %s dst %s proto esp " - "spi %d reqid 2 mode tunnel " + "spi %d reqid 2 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s", IP4_ADDR1_VETH1, IP4_ADDR_VETH0, XFRM_SPI_OUT_TO_IN, XFRM_AUTH, XFRM_ENC); SYS(fail, @@ -313,7 +313,7 @@ static int add_xfrm_tunnel(void) */ SYS(fail, "ip xfrm state add src %s dst %s proto esp " - "spi %d reqid 1 mode tunnel " + "spi %d reqid 1 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s", IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT, XFRM_AUTH, XFRM_ENC); SYS(fail, @@ -325,7 +325,7 @@ static int add_xfrm_tunnel(void) /* root -> at_ns0 */ SYS(fail, "ip xfrm state add src %s dst %s proto esp " - "spi %d reqid 2 mode tunnel " + "spi %d reqid 2 mode tunnel replay-window 42 " "auth-trunc 'hmac(sha1)' %s 96 enc 'cbc(aes)' %s", IP4_ADDR1_VETH1, IP4_ADDR_VETH0, XFRM_SPI_OUT_TO_IN, XFRM_AUTH, XFRM_ENC); SYS(fail, @@ -628,8 +628,10 @@ static void test_xfrm_tunnel(void) { DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS); + LIBBPF_OPTS(bpf_xdp_attach_opts, opts); struct test_tunnel_kern *skel = NULL; struct nstoken *nstoken; + int xdp_prog_fd; int tc_prog_fd; int ifindex; int err; @@ -654,6 +656,14 @@ static void test_xfrm_tunnel(void) if (attach_tc_prog(&tc_hook, tc_prog_fd, -1)) goto done; + /* attach xdp prog to tunnel dev */ + xdp_prog_fd = bpf_program__fd(skel->progs.xfrm_get_state_xdp); + if (!ASSERT_GE(xdp_prog_fd, 0, "bpf_program__fd")) + goto done; + err = bpf_xdp_attach(ifindex, xdp_prog_fd, XDP_FLAGS_REPLACE, &opts); + if (!ASSERT_OK(err, "bpf_xdp_attach")) + goto done; + /* ping from at_ns0 namespace test */ nstoken = open_netns("at_ns0"); err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); @@ -667,6 +677,8 @@ static void test_xfrm_tunnel(void) goto done; if (!ASSERT_EQ(skel->bss->xfrm_remote_ip, 0xac100164, "remote_ip")) goto done; + if (!ASSERT_EQ(skel->bss->xfrm_replay_window, 42, "replay_window")) + goto done; done: delete_xfrm_tunnel(); diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c index 3a59eb9c34de..c0dd38616562 100644 --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c @@ -30,6 +30,10 @@ int bpf_skb_set_fou_encap(struct __sk_buff *skb_ctx, struct bpf_fou_encap *encap, int type) __ksym; int bpf_skb_get_fou_encap(struct __sk_buff *skb_ctx, struct bpf_fou_encap *encap) __ksym; +struct xfrm_state * +bpf_xdp_get_xfrm_state(struct xdp_md *ctx, struct bpf_xfrm_state_opts *opts, + u32 opts__sz) __ksym; +void bpf_xdp_xfrm_state_release(struct xfrm_state *x) __ksym; struct { __uint(type, BPF_MAP_TYPE_ARRAY); @@ -950,4 +954,51 @@ int xfrm_get_state(struct __sk_buff *skb) return TC_ACT_OK; } +volatile int xfrm_replay_window = 0; + +SEC("xdp") +int xfrm_get_state_xdp(struct xdp_md *xdp) +{ + struct bpf_xfrm_state_opts opts = {}; + struct xfrm_state *x = NULL; + struct ip_esp_hdr *esph; + struct bpf_dynptr ptr; + u8 esph_buf[8] = {}; + u8 iph_buf[20] = {}; + struct iphdr *iph; + u32 off; + + if (bpf_dynptr_from_xdp(xdp, 0, &ptr)) + goto out; + + off = sizeof(struct ethhdr); + iph = bpf_dynptr_slice(&ptr, off, iph_buf, sizeof(iph_buf)); + if (!iph || iph->protocol != IPPROTO_ESP) + goto out; + + off += sizeof(struct iphdr); + esph = bpf_dynptr_slice(&ptr, off, esph_buf, sizeof(esph_buf)); + if (!esph) + goto out; + + opts.netns_id = BPF_F_CURRENT_NETNS; + opts.daddr.a4 = iph->daddr; + opts.spi = esph->spi; + opts.proto = IPPROTO_ESP; + opts.family = AF_INET; + + x = bpf_xdp_get_xfrm_state(xdp, &opts, sizeof(opts)); + if (!x || opts.error) + goto out; + + if (!x->replay_esn) + goto out; + + xfrm_replay_window = x->replay_esn->replay_window; +out: + if (x) + bpf_xdp_xfrm_state_release(x); + return XDP_PASS; +} + char _license[] SEC("license") = "GPL";