From patchwork Fri Dec 1 20:23:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172649 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395640vqy; Fri, 1 Dec 2023 12:24:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHe5HTMjFF0NhEHz2mNJ5xvZ7m3YPQOcSLSioF3Mmzfjq56UxCrkh3hRCOsOPC2iXXZ19EE X-Received: by 2002:a05:6a20:2455:b0:18b:1a31:ee6a with SMTP id t21-20020a056a20245500b0018b1a31ee6amr154118pzc.23.1701462241841; Fri, 01 Dec 2023 12:24:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462241; cv=none; d=google.com; s=arc-20160816; b=g7Y/c6pvq50dcK3rl0tAaLqcmKg7iTj6fXeDptvFU/USMelm7TiADWa1EZw2HY4SA6 TNKHz49y+eJd4LmNDuEeM3DidTVAqik/R0xDPoMHKqdfCM0b+mBhcVRb74uEcPiDDkyW G8gxpmzLzE6DolJYSFor6OPoF6HoK4q83J12ElidrbZ+iLYuJQbIhkPZ7DxYhzByw05E OH4d72ZnxUmUmE0/KHG0zOgVFQtKKeD+eYrrzisO184OxQOj9J0kxF7urkgdqXt6kClY 921qaTMaIObT+L8X4xgo1nHhgyvllZ9dikp6tIieDnNZQv8ksMHqZVny/EyKMqZLWcde wjAQ== 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=xTRa+2HDeb46JzorfzVt/qXV/++HAxjSIWVmSYgsSnU=; fh=QiW2VIYL3eiNNxbiGMqfIhZ9hyWmHxNXICZHto/UKEk=; b=BKCWURrpOMVhGzn1TaqUjgiUvex0EMaIdZwqGxiaSaJgSYTBIqOSOEv0rMcmWvYu72 reXytCqwHf+V8nOGCa6FV0qbPgKxcH7C07pjSgDE/uMWFYF2ZysT3wQV7F3W5QOsMAuu 8U39FWkYRA3WEK6IRsZ5qrBrd3sUMdSLHmLp/IaJ4OUSEsvSqWepsB3qtqzm7WT4qs7w hG7EgVdPBxQmMbhHNMwqQ9pw6xUPJ5IQi6kJnx2K/rEeomNj7xhHb7IlXoqCkQoAh/87 lG/OZVBh+7Ne/7ToAEdHN7EUk77KLXDXBUfbvjzS2Mdhf0RTHvzZu9jBz5yOsil/kb+Z tjcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=ZxHE7jnh; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=l4Snbsb1; 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 g10-20020a656cca000000b005855f67e491si4247834pgw.696.2023.12.01.12.24.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:01 -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=ZxHE7jnh; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=l4Snbsb1; 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 605D38133886; Fri, 1 Dec 2023 12:24:00 -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 S1379462AbjLAUXt (ORCPT + 99 others); Fri, 1 Dec 2023 15:23:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230364AbjLAUXs (ORCPT ); Fri, 1 Dec 2023 15:23:48 -0500 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA9C110DE; Fri, 1 Dec 2023 12:23:54 -0800 (PST) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 512955C01E3; Fri, 1 Dec 2023 15:23:54 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 01 Dec 2023 15:23: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=1701462234; x= 1701548634; bh=xTRa+2HDeb46JzorfzVt/qXV/++HAxjSIWVmSYgsSnU=; b=Z xHE7jnh2/BEdBP3kS+n/nszuIAuUgVFpIRmReUYUUOIca709q3TJskWwzbnijDoK ehl7FSwMlc6oZU6QqIL6aRMdEV9rskDYxaOWS9IDdd/H6bgbDBygUxFV+ZAmfsR4 tyDhRm2sMwzQrdk9kGDuK1W/JtfCiiidALn/DqZcLJDHh58WZ8tG9TNR520TDPZT 81OYB3Y/I1EsdzGNWwvIe2EK1pMAHtJkvfmxVwklQCRp0wLYAuVJksC11OQlfSbb 12qA+vMGKuNE1yF7ucIsPKdmH9eh6ZOpGPQntYgkFzjtyzIdxPzeJfWBHRkx/8WO fhoMf0L+Ji4jMO0AY4hbw== 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=1701462234; x= 1701548634; bh=xTRa+2HDeb46JzorfzVt/qXV/++HAxjSIWVmSYgsSnU=; b=l 4Snbsb1CARmoxLPg6hpjCSVR08ogs70ShRWw5qDYC8O/rnQFcDIwLyKprWwGC5DY v9KTHAFIxbE57YYROhjPNz04dui9PG85SjwXD90t/jL7sNJ3dga4iWFM2pHHx4q1 13XT1hGzzGQ0w6Q+VdJ4RJ7EiILSNPgitX1AD7bz/y95Aj72splvvpibkamYRXT1 i9HD6EL9N5jdlcUaE3sf4L+mrkr8UlbiJLNcW6qhjp6eZmU9W1ZonVBaBW9h0ytV RIXxaODwM9GXU404UceTpveIjgALlddSnMn0//DVF+H6Z1x2gZMV9QWDNDh+Z2TE wJXLn9ZUdK7Vk+N7sEsdg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdefhedmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpefgfefggeejhfduieekvdeuteffleeifeeuvdfhheejleej jeekgfffgefhtddtteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:23:52 -0500 (EST) From: Daniel Xu To: davem@davemloft.net, daniel@iogearbox.net, kuba@kernel.org, edumazet@google.com, ast@kernel.org, john.fastabend@gmail.com, steffen.klassert@secunet.com, hawk@kernel.org, pabeni@redhat.com, "Herbert Xu steffen . klassert @ secunet . com" , antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, devel@linux-ipsec.org Subject: [PATCH ipsec-next v3 1/9] bpf: xfrm: Add bpf_xdp_get_xfrm_state() kfunc Date: Fri, 1 Dec 2023 13:23:12 -0700 Message-ID: <490d156ba0dd547329eba79bc536623fb0a2006d.1701462010.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, RCVD_IN_MSPIKE_H3,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]); Fri, 01 Dec 2023 12:24:00 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112471966346390 X-GMAIL-MSGID: 1784112471966346390 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 --- include/net/xfrm.h | 9 +++ net/xfrm/Makefile | 1 + net/xfrm/xfrm_policy.c | 2 + net/xfrm/xfrm_state_bpf.c | 112 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 net/xfrm/xfrm_state_bpf.c 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/Makefile b/net/xfrm/Makefile index cd47f88921f5..547cec77ba03 100644 --- a/net/xfrm/Makefile +++ b/net/xfrm/Makefile @@ -21,3 +21,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_state_bpf.o 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 diff --git a/net/xfrm/xfrm_state_bpf.c b/net/xfrm/xfrm_state_bpf.c new file mode 100644 index 000000000000..1681825db506 --- /dev/null +++ b/net/xfrm/xfrm_state_bpf.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Unstable XFRM state BPF helpers. + * + * 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 +#include +#include +#include +#include + +/* 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); +} From patchwork Fri Dec 1 20:23:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172650 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395676vqy; Fri, 1 Dec 2023 12:24:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IE1frOPRLKf1xCWH1PmI0jzvPhcAJJFv4grRvBzTHgOBviixlRELz3UdN341hny3Z1KToSL X-Received: by 2002:a17:902:e744:b0:1cf:bd98:633b with SMTP id p4-20020a170902e74400b001cfbd98633bmr99383plf.64.1701462246295; Fri, 01 Dec 2023 12:24:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462246; cv=none; d=google.com; s=arc-20160816; b=m3iWX5v4Q0BhQ4MP22DwB0eAiuVtuxe8Yd62aAFVy0WMkQXdrM8AbnoftB1Ok9eOvf R7iJfv88AVTQbrwGzyxWheq2hE2q48uQDQVPXPHPhC/2xqt4MfRHCibRdXHFmZFMAVsq hRXgbmWrN7MlNfOCRpY93qDjqdcGCVihWXwv22ImGKTp99eqiD8xR6C98Wt+9pnJd0Cg Mbj2tOIF4rsI9bG63bpIzD7j3fVzLcYhzaPeO0wcZbVF43a8slhk6GbK9A8iRb6I+PjE EIqW1OLeCOyhxcgncx6JHu9GIrc8JR/ScoEvBk5e6vunr91I6jl/sWwOWtAUgfd/nR7n 6ikQ== 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=gTC80DkbSwec6++klwVY+AjSyF2q8Jrio27MwPjAqCs=; fh=T8rLh7QwYEHI2JUvsunLrQ3LvszuszJaJzIJJOTwA0o=; b=sVdWtLhAYoz9RmqHYEbHDRIhP2NJWz9YgnxmInGykGMZEf76Ia+KG8waF+il8wlllx FlQGsUjfTL8zaBBPkAqEHPsd9G8FuE6n3OrC5algl89e47x0ONBqWtolBL9BLohvCxGz H2rldtVeD6y9qy2RpioRiY5s1Khv+dMpz0zwhJOG/bAAp76uiEycJPkCwhQEaibWQijv jcOQtXLwon4VOUHlz0qqgzaVpnvnJoSxYU0djCWjxSXGONMkg8eafHeZzqHr71YwPdQq KyEsp30+UkA6NQ2obp1T9tcV8YLjbqjeGO9QWsPExOcWdm6KZ8c2gKf/ztt3cfH4HiqM f/hA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=N5qepEM+; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b="Ddfw/66q"; 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 ja15-20020a170902efcf00b001cff3536969si3568875plb.468.2023.12.01.12.24.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:06 -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=N5qepEM+; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b="Ddfw/66q"; 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 EB0738133895; Fri, 1 Dec 2023 12:24:04 -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 S1379522AbjLAUXw (ORCPT + 99 others); Fri, 1 Dec 2023 15:23:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379478AbjLAUXt (ORCPT ); Fri, 1 Dec 2023 15:23:49 -0500 Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73606F7; Fri, 1 Dec 2023 12:23:56 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id D42845C01E6; Fri, 1 Dec 2023 15:23:55 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 01 Dec 2023 15:23:55 -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=1701462235; x= 1701548635; bh=gTC80DkbSwec6++klwVY+AjSyF2q8Jrio27MwPjAqCs=; b=N 5qepEM+SjCrEYitQmzwC1hP/Ky4ABTGj/pnIWWXg/Rp9jZTTKwj1gXjEzONAf5Fa LbxVnTGkblBiwQ5wZNt8R0sgOMQOCZq37OTW2IbY+Drf3/dkW9pha0LFoS598FU5 7fc3x6q0CoiAoAPd9KjTKXbPM49WDVKDfeT/H50+xWKN2xq1yxgdfiC65Jt0Smd8 h7lvEA1DvX/AILJEU9Xh6NhmH53dCg9xydKxcJz8Ibqo43WX7Z+R967j5kUgNq0w P+yGvd/bNpSAD6cZ1tdOAke9qA8LBX02jv/wj/B57Uj3OQr4dRBvtmWAaqRZDiUH 1WI0X1fPm3Iale1ZTBWMA== 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=1701462235; x= 1701548635; bh=gTC80DkbSwec6++klwVY+AjSyF2q8Jrio27MwPjAqCs=; b=D dfw/66qLjQd0Zbq9BHdmH0TjFLXYjk2WsdKiNWnjEGPXy2XNWOFJObm7HeCX8tl2 GWw2xE71fPCi5eUUe4bTyVIgx85Kkp+zcSMK/fg6W6yg5L/ucEYovtVqkk1+Oa9g buDJqCJTFtVtZmooP9wM2lp2W9F0VOKzGc3CnLHAOdzuNrKKl2B3elmW6IJd7bc0 gyVXAdpD0ZZ9ZjTmiYqlj7CD3Ae67xkoDNGNP/FxtZ1LemWs3HKueZHJqeZn8PVZ 4fxgAD+K7wuOCKybWWnWYnmdCjygb3oOnha0snU80tBCYCsxKCWGruQCQKVi8mgG 8bnqrf/UEUcj/MDUGgo8g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdefhedmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpefgfefggeejhfduieekvdeuteffleeifeeuvdfhheejleej jeekgfffgefhtddtteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:23:54 -0500 (EST) From: Daniel Xu To: davem@davemloft.net, daniel@iogearbox.net, kuba@kernel.org, edumazet@google.com, ast@kernel.org, john.fastabend@gmail.com, steffen.klassert@secunet.com, hawk@kernel.org, pabeni@redhat.com, "Herbert Xu steffen . klassert @ secunet . 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 ipsec-next v3 2/9] bpf: xfrm: Add bpf_xdp_xfrm_state_release() kfunc Date: Fri, 1 Dec 2023 13:23:13 -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_H3,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]); Fri, 01 Dec 2023 12:24:05 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112476393555385 X-GMAIL-MSGID: 1784112476393555385 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 --- net/xfrm/xfrm_state_bpf.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/net/xfrm/xfrm_state_bpf.c b/net/xfrm/xfrm_state_bpf.c index 1681825db506..1485b9da9425 100644 --- a/net/xfrm/xfrm_state_bpf.c +++ b/net/xfrm/xfrm_state_bpf.c @@ -94,10 +94,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 Fri Dec 1 20:23:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172651 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395725vqy; Fri, 1 Dec 2023 12:24:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFf7NQ1SgOXQjGWFgFwWNJA1cna/L8GV2VSgboiQC0056PaFjV10P0yKa8K53GAsAZgaYGE X-Received: by 2002:a17:90a:e7c2:b0:285:8a2a:173f with SMTP id kb2-20020a17090ae7c200b002858a2a173fmr92936pjb.0.1701462251150; Fri, 01 Dec 2023 12:24:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462251; cv=none; d=google.com; s=arc-20160816; b=o9tjymqCdrTYmdcPn+bfn2WgPhAMJJVjXv6QMNLpCxDmahJPPQS4C3DFPjb4TCKDEG /6AvvBYB2VLF/+sPnlJrxvWVnmjaJY9CoyR4ZnvXiKRRsiyR7lmyrlrI8L2Fn/wDIfIB tkbZ2AdUfaD+b3L1AbPdTZoeg/GLYNNsEaPIk6JGxsPrfbfYbcPkUtGNVbhFvwzt4275 yi4CYEFYhNqjovvQbp993HpLONp7IVYaV1u32hAnuGxZ5NB9WNtu9R9RE95axPdEg9Co VbLILkbDuVyVZANgCKpYljI7m2oHMYGGaPBT8MeP5HZ3u+n1pbEJqJ2z2XTx2/CLd9dD efMw== 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=c0SQUBYYwJzTTCGOT+Ez9K+x+hF2I698/lffpVW1VHI=; fh=rn/2rsA4poE8MfqHDOcfv4uWaSfO3DnFrL3khh8NAi8=; b=z7A/pwP/Rv8LAvHMHzOTzJCS/K649v+kV2IN7Wt3DqDl6tyHCL9q0PjGAcJAtpAZsB ui6EIcA5hIf3XLFNfswXE0FqDMhmx1Rcrgi5FtNoUcQ8iujaDaR4IEPalSz3lWlbPUAo u4uY7duggCONxl1uTew6adMmmd4VQD9qmNdu3eLJZXJO/rRSfrBZ/W8MBc/4Mx44r0+k VcDS6crw8pRF6C+qeRyyrJpevrgID47QFhs8nrNEcMYocRI5hTs2H27xheGhkhNm5UxE FJ+/70/hhXiiUW0vGaEULhV0INk94LauviJ861kwHyi7NWelOtc556gE5VDlLoeeCKv/ nhFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=YOE8nYUp; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=uVUpr5Ra; 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 a13-20020a17090ad80d00b0027769032e57si6107769pjv.52.2023.12.01.12.24.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:11 -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=YOE8nYUp; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=uVUpr5Ra; 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 0DC0E8133885; Fri, 1 Dec 2023 12:24:10 -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 S231167AbjLAUX7 (ORCPT + 99 others); Fri, 1 Dec 2023 15:23:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379534AbjLAUXw (ORCPT ); Fri, 1 Dec 2023 15:23:52 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3F10DD; Fri, 1 Dec 2023 12:23:58 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailnew.nyi.internal (Postfix) with ESMTP id E613058098F; Fri, 1 Dec 2023 15:23:57 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Fri, 01 Dec 2023 15:23:57 -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=1701462237; x= 1701469437; bh=c0SQUBYYwJzTTCGOT+Ez9K+x+hF2I698/lffpVW1VHI=; b=Y OE8nYUpixcpzmPMxMxOt4wxQ0E+uKI2F8N1A6+Es6IGnOIfEZDCaySPB7pvY4Lun 1YFNZbQlkgNdbqT0hqFG8QOT6OY0XwLURyfzy6aL+4fuSUWfxpcpgVkzFAArhXHh wijPHaVuT5oTxdy+uN4ts6OrCCseD7T+MrnjswaSWDgOy3SQQwkNnD4ytrt6oPgb vDVlQ826Z/1ey17zD0BXZOjI1z1nf3aZcZ6JaMj4aHpP2STegI2I2qFAxaN05nuk 8LkWlVLye8I9WCfxgLV4F+ovRylzG6PQNZwjI4UVX3dSEOFFFfQRHR1iCxqrd+nl kuQpdgsucmnFOouzjtl6w== 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=1701462237; x= 1701469437; bh=c0SQUBYYwJzTTCGOT+Ez9K+x+hF2I698/lffpVW1VHI=; b=u VUpr5RaeyZAcr8XiX/cUJd6y/eQ2sCZA2Cb5ZPhbUdWP2uTdzvS4MBxJbWZUlSGB W+G9tOZmC70vPeUBvKHp1Zn5FifuzhdwqwHOF2RjR1GmsB3PwR9ADWjSd8M90nXl mO9o3l0VWQRKq68GyzC5/VWgKrCKcyE1Q8FEsHPJUxEkdtdV0ND3MhHWy8o5nOX3 4hc1lRE94yUDhU9Lnk0mGHUf22ktlmvYwLrL7MvkmQi+ijtdIa/uhyNXD0OhnLVd 6p4zNBQ0JJK6xOpZTMbcrXXMA6ek7hEcdDAwuwuJYdt+SmLmSayMp/BMxjQEJCD2 I9RjQ2n0QvzgIewr27UBg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculddvfedmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpeeigeffteehteffheejkeefjeeuudfgvdekkeetudeghedu gffgleffhefgjeevgfenucffohhmrghinheplhhlvhhmrdhorhhgnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiih ii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:23:55 -0500 (EST) From: Daniel Xu To: ndesaulniers@google.com, daniel@iogearbox.net, nathan@kernel.org, ast@kernel.org, andrii@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, 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 ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro Date: Fri, 1 Dec 2023 13:23:14 -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_H3,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]); Fri, 01 Dec 2023 12:24:10 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112481817071779 X-GMAIL-MSGID: 1784112481817071779 === 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 how 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 nval = new_val; // 2 nval = (nval << rpad) & mask; // 3 val = (val & ~mask) | nval; // 4 (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): Place the new value into a word sized container, nval. (3): Place nval at the correct bit position and mask out blank bits. (4): Mix the bitfield in with original surrounding blank bits. [0]: https://reviews.llvm.org/D133361 Co-authored-by: Eduard Zingerman Signed-off-by: Eduard Zingerman Co-authored-by: Jonathan Lemon Signed-off-by: Jonathan Lemon Signed-off-by: Daniel Xu --- tools/lib/bpf/bpf_core_read.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h index 1ac57bb7ac55..a7ffb80e3539 100644 --- a/tools/lib/bpf/bpf_core_read.h +++ b/tools/lib/bpf/bpf_core_read.h @@ -111,6 +111,40 @@ 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 int rpad = rshift - lshift; \ + unsigned long long nval, mask, val; \ + \ + 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; \ + nval = new_val; \ + nval = (nval << rpad) & mask; \ + val = (val & ~mask) | nval; \ + \ + 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 Fri Dec 1 20:23:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172652 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395759vqy; Fri, 1 Dec 2023 12:24:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IHy2gCPXTI/0j2UPh/whgCfZHXwL9ncqWjWnUJ2dbJV4csOyFE+ZgQ+OVgcIbnCSy8IdGvR X-Received: by 2002:a05:6a21:99a5:b0:18f:97c:925f with SMTP id ve37-20020a056a2199a500b0018f097c925fmr72270pzb.68.1701462255273; Fri, 01 Dec 2023 12:24:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462255; cv=none; d=google.com; s=arc-20160816; b=yTi9GMGLTN8N8Bbyur6Nnv/iJL6sX4Uoj9G/P1j/aKfqg2F8VGhq00sziAhy+sOmQa Khrn4/7EzLopVfiO6hV6aOVxn+vyt2qJGRK9II8uyjXK7a7RLpnjcGSdwd396Lfyhx6w wuHLdF85I5ZtLudHvi9e+tBcc4keEF29ujTeKGEljp1VYRZgd8TSnL/+GZl2D4xdrtOm aU/dUSeNvFvD5at3gK1Er/6hNjtwayhKgspGKlRHmrHXhb0bgwDe8yqSrkL4Xc382Pdx j39tKOh5Yb7Kk7bchtXf3z3Rgd+9oRO/0e1G+eJgF9/SWtQx+G89Nm5HOjT90d+uCRvw U79w== 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=D8noEFBJZ4ewr1BGMuw+TiQmHop9t4Qe8cy7C7t2YkI=; fh=EED5Kmk4NeeAF1lciSI8z5093duwqeI1+6ucqgDjd84=; b=DRPiZeQYWghs3YRfxUSjOwl1Rspn87EaAhMewyRIKM/5PIW5ES5/8+vnOpceGTnD02 6pZfdtkyuAlisPjOxpoM6tuL38aJ0Bqorh3PMbaEzDJh20rYZ6H2Uk3zZhy9buHQ5GvQ 3l8ZpKOhRBOxGp4Q/i50YNc7Z8QDHizJJfOJ1xJlVYa/fbdtI+IWvY+d7uGR1BRHiOVC UQTAOeyS9V81nDYaCskklaYnDbeBYsd0BtiaSj619xq8WejVYjJV5If6+vesvOVrBwBH uXlYMVdykDvXkplfeH7zdYkiZzsipV1Gv2B+pklba6O3QpcwlYHukHdKyBgBeFEHIT60 R+kQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=YNgx8Hvm; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=spe0L5Kb; 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 s85-20020a632c58000000b005bde6fae09asi3852636pgs.135.2023.12.01.12.24.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:15 -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=YNgx8Hvm; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=spe0L5Kb; 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 0EA868134CCA; Fri, 1 Dec 2023 12:24:14 -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 S1379538AbjLAUYB (ORCPT + 99 others); Fri, 1 Dec 2023 15:24:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379547AbjLAUXy (ORCPT ); Fri, 1 Dec 2023 15:23:54 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CA4610E5; Fri, 1 Dec 2023 12:24:00 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailnew.nyi.internal (Postfix) with ESMTP id B5EC6580992; Fri, 1 Dec 2023 15:23:59 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 01 Dec 2023 15:23:59 -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=1701462239; x= 1701469439; bh=D8noEFBJZ4ewr1BGMuw+TiQmHop9t4Qe8cy7C7t2YkI=; b=Y Ngx8HvmT5P6l9C65V3JzMgc5wRfw6QvgfzXbCJVKZFC/XVm9fVzDErtiXdaKdpAN 6ZGMuMiz0OIoLZX7JVUZN9RheTS9OU0SjbJalCUp5bKiRlOtXUheRTrX5U6P/97z tMuLHD2bURLvEepZTPTTBofCnSXeZZYdvHMLSdbMFzwWw3urTO8Xj3V7BtJYBR2+ GP5qXVpCY9gNn0TUgurzLNIeq1lPjngGUzYAymEJhLUYNXvGQDHX1QI3fpDITl+A rAKuAwo/KBUBZA8qY6GcIsf0rWnmj9N+rWMyOglogENQMXTRVd7o3WvCEpZxtTLj Mg6TPN5s9HOjvv42raBQA== 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=1701462239; x= 1701469439; bh=D8noEFBJZ4ewr1BGMuw+TiQmHop9t4Qe8cy7C7t2YkI=; b=s pe0L5Kb5EQesoz68YAYAKkIwIFOkUWSup0tY/AqFhM1Ww/KSh2vl6GmVSCe7gkgC j2324KysdrsZG2dpsd/mceN3vE9pHo83+0P/uQrocipH/ZuL2cQyfB2PmSkEQpPw m9qh7jVTywfTEY0lBlzEPodBO8gCMQr+eF1eO0LuHRbc1fpPk1gNRsef5qzqiScX QJfTVaBep1ccgsfoufABgQCrobhMxI9mwE0SYOBsQCg4rQrjArtpLP2eODhhdAk5 ddm24JhWu0LZOF07gOcSaD3trVjp6WSRK7NSzV4t6eTb9HMR2T+hydAPghjMqDNc nQ0Zj5qvnf3/NLAEcLc5g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdejtddmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:23:58 -0500 (EST) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, shuah@kernel.org, andrii@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 ipsec-next v3 4/9] bpf: selftests: test_loader: Support __btf_path() annotation Date: Fri, 1 Dec 2023 13:23:15 -0700 Message-ID: <68419933c552bd30e92d432e287ce91deb3ed52e.1701462010.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, RCVD_IN_MSPIKE_H3,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]); Fri, 01 Dec 2023 12:24:14 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112485831249635 X-GMAIL-MSGID: 1784112485831249635 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. Signed-off-by: Daniel Xu Acked-by: Andrii Nakryiko --- 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 Fri Dec 1 20:23:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172653 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395785vqy; Fri, 1 Dec 2023 12:24:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6/nGNwsXb8//qnP+O5rcGs+dATJnxFUfvo8yFuP5cOK6yGAHwBKdTxeCwPgmnw6fBaik5 X-Received: by 2002:a05:6a00:1356:b0:6ce:2732:1e03 with SMTP id k22-20020a056a00135600b006ce27321e03mr81099pfu.61.1701462257986; Fri, 01 Dec 2023 12:24:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462257; cv=none; d=google.com; s=arc-20160816; b=gCRHjo44k2Q3Nomata9ImPxC2JuZWyKAmIc4wxcIbhaRPfEp8hS5M9PG6XP4PeK7+E LUtRjw5xvj42CbMVoVV9NDqZl4dd8JsXlY7uIIPIQ1bcXuRdAh0yC4Ac55FySXsI/k6C 95YbvBtUo78ZHCue77rqknZi2BE52EKnbSWAOqth5d5Ke2IwWk+sOHd/TCJCqb0W8f1H +26+6k+xcaocPWQwmryZo0Xp8kRSThL1VT2bas3quGnWTzeY1eJ/AxuaEJpiMV194gZe an+uBf25GoxHOymCDmrbRF2Kem4KSiwYyb+EGNUs6/gdFhqNp8CMWHCgZJIRcHsQxaO5 lBCA== 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=uvTwqTAZ/68O3KK7heo5AiD3gEgHNW4W5lf/fdX2H2o=; fh=DjCNJazmHHsMmM+xBADjNYCnZBYjbfSIJPTY5w/4vDE=; b=za9++mngHuGlo+ZAoj2x64gNcJhWFiRP5CRzFTALHRfsoEsPDlaiCLRI5qDy8GsXk3 4XFduGGkjt5YoEJjtE2PEYpKuihVpq43mMhAkpemC9I89oebn6BXGTvZQ4dHWoXVEzEF 7wdkMTOQNNflp36BTiPC+O0vTfBj2FEu901y9TsgE0YFY5FrzDSKJDP7UfQ8Pig1mKOK g57cQkEwovrgg/timqqlO18S/GB6K61nLRWCL+qePY22LpozShYCtriwHAjPSKds2vtW e3vXu+Ula27+G4TrA8Ielo196yXyJRPTURmRWlNujBOSenCpOj/kCuG63ZysR3OUhVRp Dozg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=KKxjWq6I; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=CI8LCAn0; 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 b15-20020a056a000a8f00b006cb93f13d52si3911405pfl.120.2023.12.01.12.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:17 -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=KKxjWq6I; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=CI8LCAn0; 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 EA4E281410F7; Fri, 1 Dec 2023 12:24:16 -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 S1379599AbjLAUYF (ORCPT + 99 others); Fri, 1 Dec 2023 15:24:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230488AbjLAUX6 (ORCPT ); Fri, 1 Dec 2023 15:23:58 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D21710F4; Fri, 1 Dec 2023 12:24:02 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailnew.nyi.internal (Postfix) with ESMTP id 6FB97580994; Fri, 1 Dec 2023 15:24:01 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Fri, 01 Dec 2023 15:24:01 -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=1701462241; x= 1701469441; bh=uvTwqTAZ/68O3KK7heo5AiD3gEgHNW4W5lf/fdX2H2o=; b=K KxjWq6I0Rx6qT+PjICwtMdv+khHm0zKdXc8MnzTorgJsfseEYpzPsvP+8RjmabMc peFPWIYTZCRHo9m+V7VsSUr+7N5jL06vqSR6ubrmDhpwci2C6b+QzstBnbi3CJNA l+qcQco9tUoqN06VdvZCIvFvEs/ity9Ndf1hnuSQ0gWrp/XP3FpzpUX85tiZkZI2 loxmtdCX9OYRmM8G/OZsrpXOlaZU1iad1K6Ogg1ZOub+JM/59MiWXFQJbCFhpees yAjou/5l2yj+AWDugeBUJoKX95P5LSDZbevlTklW27HaDhvw+joYBLcsomeiKzQG uEScp44bYagRepShCxjRQ== 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=1701462241; x= 1701469441; bh=uvTwqTAZ/68O3KK7heo5AiD3gEgHNW4W5lf/fdX2H2o=; b=C I8LCAn0eZsG2zJ4sBKcca3TYOfWtlOD5UfNzpbdV/9icdG4MY8Fi33fwGAH8r3e8 RG3643pqYELdxubemBKeLgSUzJbMpulD1hlhJABQdO4QAGkC4sjNg2sd72qXUfMr RY7CTFUOO6ygyC1XePaZjClGQbz3ux0BpvIwS+sXU0HNwIkRPk5CTpLhXJudN/Qf a213iXebdLi7UmCBq/kAYlQVD/C2DMcGkZIkpxjsyVPLNO+/O8LQWF5NhMB5G05G dxJbpRohGnHTbjDF4WUBmwkZa0ZkpaN+2bnsmxrdARmbt25yck2Omzn+8R0eT/wo UBHegv8Sa+Kvj/c2yBPOQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdejtddmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:23:59 -0500 (EST) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, shuah@kernel.org, andrii@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 ipsec-next v3 5/9] libbpf: selftests: Add verifier tests for CO-RE bitfield writes Date: Fri, 1 Dec 2023 13:23:16 -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_H3,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]); Fri, 01 Dec 2023 12:24:17 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112488826525115 X-GMAIL-MSGID: 1784112488826525115 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. Signed-off-by: Daniel Xu Acked-by: Andrii Nakryiko --- .../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 5cfa7a6316b6..67b4948865a3 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" @@ -115,6 +116,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..8fe355a19ba6 --- /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 __failure_unpriv +__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 __failure_unpriv +__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 __failure_unpriv +__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 __failure_unpriv +__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 Fri Dec 1 20:23:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172654 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395803vqy; Fri, 1 Dec 2023 12:24:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFs78SRLY2eb/sxGwC79Vh/vnXKHIaIJA1AGKJfnsaIs9hHSsfq7oOdsixP2M/J3YnCcVyt X-Received: by 2002:a17:902:8f8c:b0:1d0:6ffe:1e86 with SMTP id z12-20020a1709028f8c00b001d06ffe1e86mr74423plo.105.1701462260985; Fri, 01 Dec 2023 12:24:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462260; cv=none; d=google.com; s=arc-20160816; b=PxdrhCufKHR72/YTC0/StQppFH8168BtYdzG9tIf17ISEjnTkwnR68gB7A4Ipc0cj1 lsu07p4PK83CoXMHs5VWEezmWjQWp7Yt2JL2xz/fsDv+at8sEaEGiDheezrGppzmfaD5 VC1QIFMbTqkgpHz8aTxD753+C4Uf1GaldZDT9kTK/OOMvNkp14aDUftTagDlYEFv7ltv K53UNDy8rhlwgH5kCbsseqdFoy+piD6CBXlb0uws0yrjimjXEQFweLvMNhzBuRzdmPBa f1AUSFw6ROP3kFWGfMSyd8wjl8s3DP0Q8lruUKXdUf3H0XpKOEmjG5aWBL9ObuRerKZs f/oQ== 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=EED5Kmk4NeeAF1lciSI8z5093duwqeI1+6ucqgDjd84=; b=DfyvMFQBcpaQEhBB/yymxA0goHj0bouTfLYXZYm/8h5x8/99xy6vAbP0l+vYdN/ENj FmJ2TBLNwXzXIc8HIfI9mb9AqDO9I7AlsOAfwDTN3gRrdB1lSSXAgBDMZfrMPTj4SgOo jmxjXkMf/lgCt3L2qYL0W9Fvw40ax/ehTOXyYiNHjhuPvUX0neFsS/8hHVtdvOedFqnj fv8iX8aGsOvCo/jn3Tb/w4J2kwvDwMFCGvRsiZgGRM5Qq0xXUWhVHErBmcTDkPp6wZ5I 1Ra/tFdjlzoLkiHst2MdZKZxyKH96hnx/4cSlvcFuSmH1bww03u3CuOGOz7sj/HQHkMt /ItA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=EHAr4rCg; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=MDBWQLOY; 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 x2-20020a170902ec8200b001c36018fdaasi4021041plg.219.2023.12.01.12.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:20 -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=EHAr4rCg; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=MDBWQLOY; 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 0B97E8153D1B; Fri, 1 Dec 2023 12:24:20 -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 S1379600AbjLAUYI (ORCPT + 99 others); Fri, 1 Dec 2023 15:24:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230526AbjLAUX6 (ORCPT ); Fri, 1 Dec 2023 15:23:58 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D25F10FE; Fri, 1 Dec 2023 12:24:03 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailnew.nyi.internal (Postfix) with ESMTP id 40599580996; Fri, 1 Dec 2023 15:24:03 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Fri, 01 Dec 2023 15:24: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=1701462243; x= 1701469443; bh=mulrQKzv1eO6blHCVRuuCfG2CgE/rU/CR7wkoBWNDIE=; b=E HAr4rCgShLNZZ4O/7TNayEf9oOKO4GXYDh3ZOePutwcEIvReaAQcyQxASQqhSqj/ BY9Ywbiz59KGIUN7eT88YwhLmIlEvm2SFOhFMSn30mRL00iRu6wrC5BEl/5azSVV 2RJceiUKGAmWVIYFWg1+przrpLhm3v1f29s1hmyPaTzSAwGyu+L30MLHJnFn+HHF ISf3POIQOyA3voHkM4goE+HPtqd5hOIhgmZHXi6VPRpW0gqITajsGnLd2sGNf6LC oyWRhEa9STdmUAikinlFOfLho3HZGolrzHBi+Ex0r8YFOL2etle0zz0SNKTHZzOw +Arqn2dpk96LvZTdxC69g== 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=1701462243; x= 1701469443; bh=mulrQKzv1eO6blHCVRuuCfG2CgE/rU/CR7wkoBWNDIE=; b=M DBWQLOYFMi8R6XPEcWeb/joAJYbPqAt0dP9wpyRd7fzmvHPa7VPbsoEB/Rjxnggj VbrW6/TLi8376FLUEZbOKGEsQNu6X1q2K2WuJDMzZPkdZLOTqcoZRAemRApoaS/z 4lUBZMSfIOrFDfYT/rN92xb8O8/b0k71H8A6tsTPQfEq8jeoDcWPGX3A72mGioPz b9+Qq4sqM6A81UfI12vO+8rKWzGOWcF8AcaJmFre1QhIonGsjwv+TnGpq0iRqSLV JBRESn5Zg/+rGvqwbsIkIzH5uNYUdaBxyw46v87jLJEOYiI0wqfeQMK4HAynyvRX YJe5AumJq90e0gT8j+z/Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdejtddmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:24:01 -0500 (EST) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, shuah@kernel.org, andrii@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 ipsec-next v3 6/9] bpf: selftests: test_tunnel: Setup fresh topology for each subtest Date: Fri, 1 Dec 2023 13:23:17 -0700 Message-ID: <0441399633380f6ac1bd34ae6e5bf668fb7b83ce.1701462010.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, RCVD_IN_MSPIKE_H3,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]); Fri, 01 Dec 2023 12:24:20 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112491874668992 X-GMAIL-MSGID: 1784112491874668992 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 Fri Dec 1 20:23:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172655 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395819vqy; Fri, 1 Dec 2023 12:24:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IHrUyFq5l6p8He0FZNCefKOp4MfWswLnNT12ncZng+wQ23tmJDq553BRAo5B9nO43Qwfjod X-Received: by 2002:a05:6a20:13cd:b0:18f:97c:978b with SMTP id ho13-20020a056a2013cd00b0018f097c978bmr73847pzc.115.1701462263322; Fri, 01 Dec 2023 12:24:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462263; cv=none; d=google.com; s=arc-20160816; b=XFlOhb2H6glDZRJ5CDNL9bgfXP+l1/SJl6wptcfOy8cgVJdWUZuirqgHx28d4eKA3E c7bGIA/mRy4Ruo+HsZvzSeVeSXOXakqek30xuSCmv3zcvdgenAODuIhjS3NdT6hR5CF5 hj4ZAvhfWQ/CFdJuK4reqah8k/prZ2+itzJu9lmsgmHliJgOw9CpC+lg7v+WScpSPg+1 icV9aKR4F7W3RbmoVkkw8qwhb69RJK2OcFAc3KdpjCC2uogc8bid78mA9N80X/IBNB/+ rlzEtFwRPinMzvoXdl1SQWwNiZbzPcdZmLWN4JZPXjOyFqgjURuY9Y0TMeJ7hs9qFWY0 OEYA== 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=EED5Kmk4NeeAF1lciSI8z5093duwqeI1+6ucqgDjd84=; b=H5VHMl76HBbKUSXMpYPLKyqeblKRCt3j0eOFxlSpGBjtToALMc1E/WPbKsNAiHjWzU qjxobMecO4zgh3TEF/7qswPEJ8NQfMssXycPUIkJ8bN5o624i5lectouD2Fp5kQHVlpJ x3ImuBHF6/yEYHByzPQ0d5Mmu3MT3YiuOZR6RmtoJABY96EhKztEbUr+/x/Qjz9xuAku g03uuhDsU2YYUQHIAQxbDdzRKpQwAUJR+AYtji00s6iwRUWZqbfgQao8Lox8r8t1sIMZ q4kZx4pQWFryLXsDAWA1ZvJSR5haISm+Rc1JfC4iawl+O3YOxW/kdUJF840NGvv0JH2d au1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=Kb+ARw1B; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=DDwswFVU; 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 u13-20020a056a00158d00b006b4231ba465si3863912pfk.85.2023.12.01.12.24.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:23 -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=Kb+ARw1B; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=DDwswFVU; 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 14C60815A1C8; Fri, 1 Dec 2023 12:24:22 -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 S1379617AbjLAUYK (ORCPT + 99 others); Fri, 1 Dec 2023 15:24:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379558AbjLAUYA (ORCPT ); Fri, 1 Dec 2023 15:24:00 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4C6410F1; Fri, 1 Dec 2023 12:24:05 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.nyi.internal (Postfix) with ESMTP id 08FAB580997; Fri, 1 Dec 2023 15:24:05 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Fri, 01 Dec 2023 15:24: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=1701462245; x= 1701469445; bh=9Zj48xwH3RKj/ArgA1aCuMUGNgo6h0Y1fWoiEhljy1A=; b=K b+ARw1Bl0zqu0JsxW2KCRjprX0kCnx0c1xYUcep+gZ15ZrGmF+eZs2Iw7ARU10ce nQZDCnJ+4BsSBlLH7IqEz8otrV2T5nF72QBN2EgOx6dhv1lgSkj2tdToW9d7JB+B ekCyhg1FBjIdZA4MRHJXibeP4XOAmUGwq2VGaT2EaTtgvLKUsXu/ttAstmuCWvug pAQ0HQsiwVxoT2uZCQwEeCwoQTMys+KQTm/2SrJFEH/GT2MwIEKaSU6hPLdiAtTq KLeJW551jF5R/fSutShEp04RA1NYT5HMdF+pf8UMPNnKOWLOzfpmfHqG5fB+GqWB YEO9nqFkkGErYQ7OJeRAQ== 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=1701462245; x= 1701469445; bh=9Zj48xwH3RKj/ArgA1aCuMUGNgo6h0Y1fWoiEhljy1A=; b=D DwswFVUegb5ksuYTOn/VK8gbMeGPf+04s19uHIGeDMm3fkjRegoBGmoG2Jy5W9bW MsQVUD5bo9UIEpt7FSg5EyuMwZxteykrAaPFiFpG3txe/cqCr8a5CyHCG2D9BZEG ReEQtuI5Q7BmeDJoiXNm5IAMHEUJ1J5wDyi3CP9yQypxxx/fcOWHU/23Ixb/bj9U 8hi5oKpTLqVqDoymLNWmRUqNDFkdjvw8IiF7dRUyU25OCo4J0y9t4ZJj6YHJ7Uty sj7QLl54ThMyR0kH6lTztsG1al0PHrCg0B/sTwVM7YND/lzB90ufiyMoH2fyk8ox eNtGyIJPi1auGYPwzrSCQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdefhedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:24:03 -0500 (EST) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, shuah@kernel.org, andrii@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 ipsec-next v3 7/9] bpf: selftests: test_tunnel: Use vmlinux.h declarations Date: Fri, 1 Dec 2023 13:23:18 -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_H3,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]); Fri, 01 Dec 2023 12:24:22 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112494577308159 X-GMAIL-MSGID: 1784112494577308159 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 Fri Dec 1 20:23:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172656 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395862vqy; Fri, 1 Dec 2023 12:24:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IGTmf2PZmn76F4ZGX+dFdiYE/5GRI4HtHv2IsxYmghoXOIrLJxA9kY4cPbDVus5eunWfLKK X-Received: by 2002:a17:902:82c4:b0:1d0:5878:d4e7 with SMTP id u4-20020a17090282c400b001d05878d4e7mr82504plz.10.1701462268835; Fri, 01 Dec 2023 12:24:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462268; cv=none; d=google.com; s=arc-20160816; b=qfI99Cy7h1Ti8DDilgOeWpmC94h21amg6QNhFoA5w/1etlhBOYnkx0yT6ddvKA33ff l5EMjGSTCsQCUPLSyNm2Mxyoj7h8djwOizKqATyo5Q+7N3uL+XAuDpZ1XTGS/hXYzfeV +SDRmGWMmRSTdkNKRq0RDWCMsjL7YT/mmDsCZ9MLRYGCM1upOGDIz/fgX6UsQtci4jS/ czjkK0dvqG//L//xGBAl0s9PMSMGK5XYsBrsdDONm0EGuO9QHUTVoKJ+y//uJZPTWKoH 9gGbhQkfLDtY2NRsvNr3O+J/fE5OsACuMWEjCjR++Va8rSISCY4QhLkccvFXEJuRuW3h 5noA== 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=iEPiwxbzI+8Wn+djijAMA+bYTHUszD/cas2J+llupPk=; fh=EED5Kmk4NeeAF1lciSI8z5093duwqeI1+6ucqgDjd84=; b=vX1gt9WlgqKTEjsIMl+NA/QRKTE7js+X8M/hx1IyFmYZ1EkCfa+9EVolrkbqV+EOX0 m9eYX7dvcS/qocBMaEn6LmITqQmrwf+1lnzC+a24R8EEZ5nko3YC1+gglu+OGsSqi47e DkeeCJCzUGIIaDYiRLenBBD1sabBraOnTqIM5GM3e+5zHGhJ34kwjNpBEDP/Pq8Aeb0B JtnKoNntrdjvKCveYwqRLtCH1/2RISYcdvL8fSey/V3VV+QJfoovE8zsjDGHJtooi43t x0q+W8cnK1ztqCGinOSFuYjwckNgr1y9NuZuC0whW/I8A+kmzKSzwupbNkFdWwCAoApa F2lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=bZKtHPBj; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=1s1NUY3E; 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 me13-20020a170902fc4d00b001c74d844254si3900317plb.101.2023.12.01.12.24.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:28 -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=bZKtHPBj; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=1s1NUY3E; 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 A50A2815A1CC; Fri, 1 Dec 2023 12:24:27 -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 S1379578AbjLAUYM (ORCPT + 99 others); Fri, 1 Dec 2023 15:24:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379534AbjLAUYC (ORCPT ); Fri, 1 Dec 2023 15:24:02 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72A7410F9; Fri, 1 Dec 2023 12:24:07 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailnew.nyi.internal (Postfix) with ESMTP id BF2BA580999; Fri, 1 Dec 2023 15:24:06 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 01 Dec 2023 15:24:06 -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=1701462246; x= 1701469446; bh=iEPiwxbzI+8Wn+djijAMA+bYTHUszD/cas2J+llupPk=; b=b ZKtHPBjf9oCKpJ3C6lLecJ8zxWQDM2VrKM8KYtKYiCvGG0S1H5D4GbCK9yfhL4py TbSZPOm/dEkiKTYZeT9xxIHP81HtVR8/Mehti5nKNRAFKbsTaytTMeKHjoCFUl14 jXf7zHNZ721dIgeNHBfiVbm+IsqGuyY1QoZZSt3a5D+Wi6cuBlIhirplXCL3mhTd m20Ufi8CtAtamR4nmxPhVFLk3V9JLE31NozZp8Lf+XtsoKz0nDjt1UxmwxG8/NLo 6Ct3oQ+QVr5MrH6Rg7h+Xysr4WWHfNkhUvgnHRIZCuySUGRvl03cXMOC88J3AaM+ 34eEQ3nqMgK5zP6dEKy5Q== 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=1701462246; x= 1701469446; bh=iEPiwxbzI+8Wn+djijAMA+bYTHUszD/cas2J+llupPk=; b=1 s1NUY3EwgK8OWfoWSMd3x+HCY0LLc7CFuGRTdviIBUZGvTTmYRENvOi82Mg71PiA OlvL/chDPKH038LJAK8hM4+r43A3v9X8s89FDDUhkcQq28nAzrc7sBdhHz6V1E12 M5lsrXdsVV1rrFeNT+B0UTlEnpzTrWwA35Ny9dUoXRZO2PwcFMW7874GI57yVC70 9SY2t0mt6u15GbIdE4awszdPeQ//gaQc5IiWC2krr+h5ARWfMr+hoWxWvSH1MZe6 cLAiC7jmjteizsYPzHsV1u5BwhAU0A+d/YSuGq2czbtWa2h9TF1M1lEvzWLOkfk8 EoJL2SmsSVR5kzfLlUcwQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdefhedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:24:05 -0500 (EST) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, shuah@kernel.org, andrii@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 ipsec-next v3 8/9] bpf: selftests: Move xfrm tunnel test to test_progs Date: Fri, 1 Dec 2023 13:23:19 -0700 Message-ID: <0b267c30a09c951627bb2cb43f08276b2ad5ccb0.1701462010.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, RCVD_IN_MSPIKE_H3,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]); Fri, 01 Dec 2023 12:24:27 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112499849365490 X-GMAIL-MSGID: 1784112499849365490 test_progs is better than a shell script b/c C is a bit easier to maintain than shell. Also it's easier to use new infra like memory mapped global variables from C via bpf skeleton. Co-developed-by: Antony Antony Signed-off-by: Antony Antony Signed-off-by: Daniel Xu --- .../selftests/bpf/prog_tests/test_tunnel.c | 143 ++++++++++++++++++ .../selftests/bpf/progs/test_tunnel_kern.c | 11 +- tools/testing/selftests/bpf/test_tunnel.sh | 92 ----------- 3 files changed, 151 insertions(+), 95 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index b57d48219d0b..2d7f8fa82ebd 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c @@ -50,6 +50,7 @@ */ #include +#include #include #include #include @@ -92,6 +93,11 @@ #define IPIP_TUNL_DEV0 "ipip00" #define IPIP_TUNL_DEV1 "ipip11" +#define XFRM_AUTH "0x1111111111111111111111111111111111111111" +#define XFRM_ENC "0x22222222222222222222222222222222" +#define XFRM_SPI_IN_TO_OUT 0x1 +#define XFRM_SPI_OUT_TO_IN 0x2 + #define PING_ARGS "-i 0.01 -c 3 -w 10 -q" static int config_device(void) @@ -264,6 +270,92 @@ static void delete_ipip_tunnel(void) SYS_NOFAIL("ip fou del port 5555 2> /dev/null"); } +static int add_xfrm_tunnel(void) +{ + /* at_ns0 namespace + * at_ns0 -> root + */ + SYS(fail, + "ip netns exec at_ns0 " + "ip xfrm state add src %s dst %s proto esp " + "spi %d reqid 1 mode tunnel " + "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, + "ip netns exec at_ns0 " + "ip xfrm policy add src %s/32 dst %s/32 dir out " + "tmpl src %s dst %s proto esp reqid 1 " + "mode tunnel", + IP4_ADDR_TUNL_DEV0, IP4_ADDR_TUNL_DEV1, IP4_ADDR_VETH0, IP4_ADDR1_VETH1); + + /* root -> at_ns0 */ + SYS(fail, + "ip netns exec at_ns0 " + "ip xfrm state add src %s dst %s proto esp " + "spi %d reqid 2 mode tunnel " + "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, + "ip netns exec at_ns0 " + "ip xfrm policy add src %s/32 dst %s/32 dir in " + "tmpl src %s dst %s proto esp reqid 2 " + "mode tunnel", + IP4_ADDR_TUNL_DEV1, IP4_ADDR_TUNL_DEV0, IP4_ADDR1_VETH1, IP4_ADDR_VETH0); + + /* address & route */ + SYS(fail, "ip netns exec at_ns0 ip addr add dev veth0 %s/32", + IP4_ADDR_TUNL_DEV0); + SYS(fail, "ip netns exec at_ns0 ip route add %s dev veth0 via %s src %s", + IP4_ADDR_TUNL_DEV1, IP4_ADDR1_VETH1, IP4_ADDR_TUNL_DEV0); + + /* root namespace + * at_ns0 -> root + */ + SYS(fail, + "ip xfrm state add src %s dst %s proto esp " + "spi %d reqid 1 mode tunnel " + "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, + "ip xfrm policy add src %s/32 dst %s/32 dir in " + "tmpl src %s dst %s proto esp reqid 1 " + "mode tunnel", + IP4_ADDR_TUNL_DEV0, IP4_ADDR_TUNL_DEV1, IP4_ADDR_VETH0, IP4_ADDR1_VETH1); + + /* root -> at_ns0 */ + SYS(fail, + "ip xfrm state add src %s dst %s proto esp " + "spi %d reqid 2 mode tunnel " + "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, + "ip xfrm policy add src %s/32 dst %s/32 dir out " + "tmpl src %s dst %s proto esp reqid 2 " + "mode tunnel", + IP4_ADDR_TUNL_DEV1, IP4_ADDR_TUNL_DEV0, IP4_ADDR1_VETH1, IP4_ADDR_VETH0); + + /* address & route */ + SYS(fail, "ip addr add dev veth1 %s/32", IP4_ADDR_TUNL_DEV1); + SYS(fail, "ip route add %s dev veth1 via %s src %s", + IP4_ADDR_TUNL_DEV0, IP4_ADDR_VETH0, IP4_ADDR_TUNL_DEV1); + + return 0; +fail: + return -1; +} + +static void delete_xfrm_tunnel(void) +{ + SYS_NOFAIL("ip xfrm policy delete dir out src %s/32 dst %s/32 2> /dev/null", + IP4_ADDR_TUNL_DEV1, IP4_ADDR_TUNL_DEV0); + SYS_NOFAIL("ip xfrm policy delete dir in src %s/32 dst %s/32 2> /dev/null", + IP4_ADDR_TUNL_DEV0, IP4_ADDR_TUNL_DEV1); + SYS_NOFAIL("ip xfrm state delete src %s dst %s proto esp spi %d 2> /dev/null", + IP4_ADDR_VETH0, IP4_ADDR1_VETH1, XFRM_SPI_IN_TO_OUT); + SYS_NOFAIL("ip xfrm state delete src %s dst %s proto esp spi %d 2> /dev/null", + IP4_ADDR1_VETH1, IP4_ADDR_VETH0, XFRM_SPI_OUT_TO_IN); +} + static int test_ping(int family, const char *addr) { SYS(fail, "%s %s %s > /dev/null", ping_command(family), PING_ARGS, addr); @@ -532,6 +624,56 @@ static void test_ipip_tunnel(enum ipip_encap encap) test_tunnel_kern__destroy(skel); } +static void test_xfrm_tunnel(void) +{ + DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook, + .attach_point = BPF_TC_INGRESS); + struct test_tunnel_kern *skel = NULL; + struct nstoken *nstoken; + int tc_prog_fd; + int ifindex; + int err; + + err = add_xfrm_tunnel(); + if (!ASSERT_OK(err, "add_xfrm_tunnel")) + return; + + skel = test_tunnel_kern__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load")) + goto done; + + ifindex = if_nametoindex("veth1"); + if (!ASSERT_NEQ(ifindex, 0, "veth1 ifindex")) + goto done; + + /* attach tc prog to tunnel dev */ + tc_hook.ifindex = ifindex; + tc_prog_fd = bpf_program__fd(skel->progs.xfrm_get_state); + if (!ASSERT_GE(tc_prog_fd, 0, "bpf_program__fd")) + goto done; + if (attach_tc_prog(&tc_hook, tc_prog_fd, -1)) + goto done; + + /* ping from at_ns0 namespace test */ + nstoken = open_netns("at_ns0"); + err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV1); + close_netns(nstoken); + if (!ASSERT_OK(err, "test_ping")) + goto done; + + if (!ASSERT_EQ(skel->bss->xfrm_reqid, 1, "req_id")) + goto done; + if (!ASSERT_EQ(skel->bss->xfrm_spi, XFRM_SPI_IN_TO_OUT, "spi")) + goto done; + if (!ASSERT_EQ(skel->bss->xfrm_remote_ip, 0xac100164, "remote_ip")) + goto done; + +done: + delete_xfrm_tunnel(); + if (skel) + test_tunnel_kern__destroy(skel); +} + #define RUN_TEST(name, ...) \ ({ \ if (test__start_subtest(#name)) { \ @@ -548,6 +690,7 @@ static void *test_tunnel_run_tests(void *arg) RUN_TEST(ipip_tunnel, NONE); RUN_TEST(ipip_tunnel, FOU); RUN_TEST(ipip_tunnel, GUE); + RUN_TEST(xfrm_tunnel); return NULL; } diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c index b320fb7bb080..3a59eb9c34de 100644 --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c @@ -929,6 +929,10 @@ int ip6ip6_get_tunnel(struct __sk_buff *skb) return TC_ACT_OK; } +volatile int xfrm_reqid = 0; +volatile int xfrm_spi = 0; +volatile int xfrm_remote_ip = 0; + SEC("tc") int xfrm_get_state(struct __sk_buff *skb) { @@ -939,9 +943,10 @@ int xfrm_get_state(struct __sk_buff *skb) if (ret < 0) return TC_ACT_OK; - bpf_printk("reqid %d spi 0x%x remote ip 0x%x\n", - x.reqid, bpf_ntohl(x.spi), - bpf_ntohl(x.remote_ipv4)); + xfrm_reqid = x.reqid; + xfrm_spi = bpf_ntohl(x.spi); + xfrm_remote_ip = bpf_ntohl(x.remote_ipv4); + return TC_ACT_OK; } diff --git a/tools/testing/selftests/bpf/test_tunnel.sh b/tools/testing/selftests/bpf/test_tunnel.sh index 2dec7dbf29a2..d9661b9988ba 100755 --- a/tools/testing/selftests/bpf/test_tunnel.sh +++ b/tools/testing/selftests/bpf/test_tunnel.sh @@ -517,90 +517,6 @@ test_ip6ip6() echo -e ${GREEN}"PASS: ip6$TYPE"${NC} } -setup_xfrm_tunnel() -{ - auth=0x$(printf '1%.0s' {1..40}) - enc=0x$(printf '2%.0s' {1..32}) - spi_in_to_out=0x1 - spi_out_to_in=0x2 - # at_ns0 namespace - # at_ns0 -> root - ip netns exec at_ns0 \ - ip xfrm state add src 172.16.1.100 dst 172.16.1.200 proto esp \ - spi $spi_in_to_out reqid 1 mode tunnel \ - auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc - ip netns exec at_ns0 \ - ip xfrm policy add src 10.1.1.100/32 dst 10.1.1.200/32 dir out \ - tmpl src 172.16.1.100 dst 172.16.1.200 proto esp reqid 1 \ - mode tunnel - # root -> at_ns0 - ip netns exec at_ns0 \ - ip xfrm state add src 172.16.1.200 dst 172.16.1.100 proto esp \ - spi $spi_out_to_in reqid 2 mode tunnel \ - auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc - ip netns exec at_ns0 \ - ip xfrm policy add src 10.1.1.200/32 dst 10.1.1.100/32 dir in \ - tmpl src 172.16.1.200 dst 172.16.1.100 proto esp reqid 2 \ - mode tunnel - # address & route - ip netns exec at_ns0 \ - ip addr add dev veth0 10.1.1.100/32 - ip netns exec at_ns0 \ - ip route add 10.1.1.200 dev veth0 via 172.16.1.200 \ - src 10.1.1.100 - - # root namespace - # at_ns0 -> root - ip xfrm state add src 172.16.1.100 dst 172.16.1.200 proto esp \ - spi $spi_in_to_out reqid 1 mode tunnel \ - auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc - ip xfrm policy add src 10.1.1.100/32 dst 10.1.1.200/32 dir in \ - tmpl src 172.16.1.100 dst 172.16.1.200 proto esp reqid 1 \ - mode tunnel - # root -> at_ns0 - ip xfrm state add src 172.16.1.200 dst 172.16.1.100 proto esp \ - spi $spi_out_to_in reqid 2 mode tunnel \ - auth-trunc 'hmac(sha1)' $auth 96 enc 'cbc(aes)' $enc - ip xfrm policy add src 10.1.1.200/32 dst 10.1.1.100/32 dir out \ - tmpl src 172.16.1.200 dst 172.16.1.100 proto esp reqid 2 \ - mode tunnel - # address & route - ip addr add dev veth1 10.1.1.200/32 - ip route add 10.1.1.100 dev veth1 via 172.16.1.100 src 10.1.1.200 -} - -test_xfrm_tunnel() -{ - if [[ -e /sys/kernel/tracing/trace ]]; then - TRACE=/sys/kernel/tracing/trace - else - TRACE=/sys/kernel/debug/tracing/trace - fi - config_device - > ${TRACE} - setup_xfrm_tunnel - mkdir -p ${BPF_PIN_TUNNEL_DIR} - bpftool prog loadall ${BPF_FILE} ${BPF_PIN_TUNNEL_DIR} - tc qdisc add dev veth1 clsact - tc filter add dev veth1 proto ip ingress bpf da object-pinned \ - ${BPF_PIN_TUNNEL_DIR}/xfrm_get_state - ip netns exec at_ns0 ping $PING_ARG 10.1.1.200 - sleep 1 - grep "reqid 1" ${TRACE} - check_err $? - grep "spi 0x1" ${TRACE} - check_err $? - grep "remote ip 0xac100164" ${TRACE} - check_err $? - cleanup - - if [ $ret -ne 0 ]; then - echo -e ${RED}"FAIL: xfrm tunnel"${NC} - return 1 - fi - echo -e ${GREEN}"PASS: xfrm tunnel"${NC} -} - attach_bpf() { DEV=$1 @@ -630,10 +546,6 @@ cleanup() ip link del ip6geneve11 2> /dev/null ip link del erspan11 2> /dev/null ip link del ip6erspan11 2> /dev/null - ip xfrm policy delete dir out src 10.1.1.200/32 dst 10.1.1.100/32 2> /dev/null - ip xfrm policy delete dir in src 10.1.1.100/32 dst 10.1.1.200/32 2> /dev/null - ip xfrm state delete src 172.16.1.100 dst 172.16.1.200 proto esp spi 0x1 2> /dev/null - ip xfrm state delete src 172.16.1.200 dst 172.16.1.100 proto esp spi 0x2 2> /dev/null } cleanup_exit() @@ -716,10 +628,6 @@ bpf_tunnel_test() test_ip6ip6 errors=$(( $errors + $? )) - echo "Testing IPSec tunnel..." - test_xfrm_tunnel - errors=$(( $errors + $? )) - return $errors } From patchwork Fri Dec 1 20:23:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 172657 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1395893vqy; Fri, 1 Dec 2023 12:24:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IG80hFII1l8XiwSZzxAeiY0rnFkglh+Mn79vl9aYL+nvPobF3iRKmUmCiHY+h2kyJhhsZMy X-Received: by 2002:a17:903:1c5:b0:1d0:6ffd:6e53 with SMTP id e5-20020a17090301c500b001d06ffd6e53mr67239plh.75.1701462272724; Fri, 01 Dec 2023 12:24:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701462272; cv=none; d=google.com; s=arc-20160816; b=iY0zKQc5wULJcmZX6e4mXb6KOTYDyO3dyVT6i4n0uzAVDJ3j8LjwO2FQqhrlZnPpyP /Uz6ApXBHs5NHGnQBLukt5AiyP1ItCOhCnuoi1i0mgnDmM0nxfRUs8VzxJiTR0jAku/7 jWiqIjY2adnnf2wVDSbIPwoJ13LCrJILnSizZHT7p/ABQBq2EcknCfKzZTAjv/0FnInT nM7ZaX2vVZksrc28JjSSuwDpo07Uuc3ROHrhePtW6ClUJ1RXaQ45czfD1mFTjfL6pkT9 wajVVYqS3pWXGXqWbsbNMrDFXlMGi+VHdQkY3Zv4DUYzWQIYOpymx2VTmQp9Kl9Zf0zO v1Qg== 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=QGYcNEY/gzLk6oDIuQrL7RU3m/bxd2aArNXDRS35Q5k=; b=DHLXe43OZ49A3qrIxh+mIVoOnxd1XWypNRf57YuknO65Ake0+0GntEjWwrRuLP6URf NnoB+2WtfNvCIRjOX7FJ/nmsOqE4nPEPHoeKXu8cLHJY/rLgX8eiQs+DAhvbas4BdpGM pMnimI5PhIk/TUKpEbi1EIleLaSy93RP2YNFeLhgv2aXIPCnsFSmtBbqhPcZ3+F33/Cc PKd2QPMUlzn7uJ72a0GKqsidhqMDAgRIRQm3r8lA7aWWsnEAji7OvpdsZkkq1b/ct1OY 2vR4M7LKpYO4AjUNYwjtQQKpqUzYcetm7k9C1sMhoVnTnI5QgD4cBOMulqS/zrHzfMR+ FN0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=kPnplShX; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=Gmf0ih1R; 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 d16-20020a17090ac25000b00286295ab335si5142195pjx.109.2023.12.01.12.24.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:24:32 -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=kPnplShX; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=Gmf0ih1R; 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 6068D83D9B58; Fri, 1 Dec 2023 12:24:29 -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 S1379639AbjLAUYQ (ORCPT + 99 others); Fri, 1 Dec 2023 15:24:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379547AbjLAUYE (ORCPT ); Fri, 1 Dec 2023 15:24:04 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F1B010FE; Fri, 1 Dec 2023 12:24:09 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailnew.nyi.internal (Postfix) with ESMTP id B685158098F; Fri, 1 Dec 2023 15:24:08 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 01 Dec 2023 15:24:08 -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=1701462248; x= 1701469448; bh=Xy5DWhMcuaQWz9hFzj8IU9Hd0UISUG5+/EyW9nquCao=; b=k PnplShXeoT/pLs3XBMWJwo4uneVlKesk6vw4jj8pei7V3UtxTbHmyS1f8zTH0qJ3 sxtxRwHn6SXidwOX2VyjuJ91Pqf1oFSLC9eYuMSni1iVlQV6L5Kt52kVmF9yEh0A 2p49AyghvFmF7Lo1aBmr8CCkzfkmmG0kj7vOwqRGA/5qUq4o+PtL5g5FB5PqtCxw 200Ig0vKOOWd4jHEozsqfKUbdfeZlESZTVMiNQLKGxKKNlgTNJZueDHP4HcszN/b XcMv3JLc0/QpOEoA/A8iMlor9ed286mzXeCByhkA79B72bIE2D/tqOl/DB73Dbnl A251/+kKFZCBk6srZSLUA== 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=1701462248; x= 1701469448; bh=Xy5DWhMcuaQWz9hFzj8IU9Hd0UISUG5+/EyW9nquCao=; b=G mf0ih1R80LIGpElB8YeIy29SrRg551bE3NZHwJpg08xOHhwjY0hRmPmuc7Ewk8yv TirWmxJc3MfwIgLVKWlKSbpJevk98lwBNEa3C6rFMbdrqT0KlSbh3Z4y3ImmHEbp Dr4jlH1MiptBIJBWdjetpzvvRm0YPIoHCIdlFr3H31ffYsUB4Pqp5iQfqVuAh3RC zSh8AWSK/uSjp8xlvuTM1Uu7sPT5ELhQVBQphytYPJzNswaVZSHQEhiTbNh1yiG7 JXSo3ms7jGplM4P/7edBuKnS6+fgxmMyq1SIl13jVTtS94HkA1iWVI1LHJ85XnAg frysoQcuCkFyWT0kdFICg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudefjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdefhedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:24:06 -0500 (EST) From: Daniel Xu To: davem@davemloft.net, daniel@iogearbox.net, shuah@kernel.org, kuba@kernel.org, ast@kernel.org, john.fastabend@gmail.com, hawk@kernel.org, andrii@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, 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, netdev@vger.kernel.org, devel@linux-ipsec.org Subject: [PATCH ipsec-next v3 9/9] bpf: xfrm: Add selftest for bpf_xdp_get_xfrm_state() Date: Fri, 1 Dec 2023 13:23:20 -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]); Fri, 01 Dec 2023 12:24:29 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784112504025589335 X-GMAIL-MSGID: 1784112504025589335 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";