From patchwork Wed Nov 1 21:57:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 160702 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:abcd:0:b0:403:3b70:6f57 with SMTP id f13csp733732vqx; Wed, 1 Nov 2023 15:00:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7T3nUjX/uMmO2D/10oWUgYZvO6BxuD2DX2n3ibEDYKVi9ETf1d7cSveh11DNnbsPRaq8E X-Received: by 2002:a05:6a00:2490:b0:6be:559:d013 with SMTP id c16-20020a056a00249000b006be0559d013mr16811424pfv.10.1698876048168; Wed, 01 Nov 2023 15:00:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698876048; cv=none; d=google.com; s=arc-20160816; b=pjdPEPVyEpZetp523bx+WlZ4N5jFe7kQDxh32jN63jr7RqBRp+7XNN07zdNQ6lTc9E FeDbQ6wmtPnrN4ybFX7ziMTY8EUBQOySZ4qPrzeqWe0pj0e15+P2mZBSjsTApjaflGIJ o3rbEi7GGd2ZY39FyKsK9/dUgkmFTxrPAJF4y43n3FlDpFyE+qbqULqGb1BQD81cszpl BFdwnACaBqJpTbzK7N0XXZhSdv+NzStqCNN51wWtFWY0YXFGkRTqPAcjoTyh7pr0Oy4p t89FOUopYVZ+Cf2/gcXnFnQ8CwHad6ajPqBfn4Hg0/DNKsxtRex+gtn3XCk/hJWznz8E 8GFA== 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=ANBKhwQmdDIDOHgpgq3kljDlQpc0eJVFRKcOW0tgGg8=; fh=8mGbqrNRq9rEjRBNKSMnlKhOyKmBajHjRovpnKQ+fRM=; b=pl6Usu9tvAicMVF9mVLbC3BisYh+1Ay0hqROo0znGY+0nnUtze7O08wsymoZ45xHGM zP9i4thUtKiBPFyl8efAOqKCgIyTiuCI0yf0sIlx4O2zoULMy1JsGuTBIYFmuBqwPqjN Nrs/JPfCf0ugJJ+k0gvMHqa8JFgFzRKYl9qAli+5qu5Bk/b/X1WGJqvRZBGPUdw3UGhY KVCGBpB1hdDke4cUi81fjcIfa1wT6WcutXJPEwKLtM06TweRvHFcYjAA00jBgYLaMbUH XYlAl/CUgrgWnpQU8V6oCEEsxJuTtDsA2aZcc/mVz9B55JDPOam9/T3jQVBwDYwFjHvB b6pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm2 header.b=qP0+u1Gw; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=TeBHo38G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id by16-20020a056a02059000b005702257f32esi912009pgb.185.2023.11.01.15.00.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 15:00:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm2 header.b=qP0+u1Gw; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=TeBHo38G; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (Postfix) with ESMTP id B9E60802269D; Wed, 1 Nov 2023 14:59:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344391AbjKAV6p (ORCPT + 35 others); Wed, 1 Nov 2023 17:58:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233601AbjKAV6n (ORCPT ); Wed, 1 Nov 2023 17:58:43 -0400 Received: from wout5-smtp.messagingengine.com (wout5-smtp.messagingengine.com [64.147.123.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 091E7121; Wed, 1 Nov 2023 14:58:36 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 4953732009EA; Wed, 1 Nov 2023 17:58:34 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 01 Nov 2023 17:58:35 -0400 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=fm2; t=1698875913; x= 1698962313; bh=ANBKhwQmdDIDOHgpgq3kljDlQpc0eJVFRKcOW0tgGg8=; b=q P0+u1GwV2LZ/wnlWqYM1ULYjTAs0aKKFhcg/zx9xjyNFbsWew6+65IWTvnhxxkWU n14WjYsB13OxXJEOJpb+iOYfcF7d+bA9cYx7saVViohSOYUWujrZqPRntbdUELHn T/ozf/5kYySNcw+/ya/IlfdW/aPODV4WtqQezbqMzgm3m6gqM1ck5e9Dj+A/llGm 0uYGP5pxn39zYdhcHZuSUo1ZEMqq1umBoLSCR9dsNTPZjnKWXgaHPAsuZ1c3r00Z vkREsBHd8x9mvC/Ofo2S7V65KXp42ECjwd+bf4ZftIZYAuOF6HHBbm2jE5bklFhB hVSmoDb5jKeSOccKoXbbA== 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=fm3; t=1698875913; x= 1698962313; bh=ANBKhwQmdDIDOHgpgq3kljDlQpc0eJVFRKcOW0tgGg8=; b=T eBHo38Gav81MAd5aVgFIKKgosxPLuS4Z9fiC4KE8ionboCVqoidmRKX3gTJcS8Ns 6WjiEM9+HL2RjqPQfa50aZBLlrRFObXBXDKTC6CzLb1wBbsKIjPC3w4/8w3J4WNL eM9IxaYMv1Lm3RiGE1rRuyy2iQxZzbDYDE7XbuOFajsluSgtavsWK4cCVHZ6GUNw ix1YiSZJ499JVzf7LBhrcik6lilVH7UCVNqsANJcOdJEt/X/kBvCDBKcVcjmx5ol bNAfB7IpyleUVpw4ME0N7w0uvBlX7FNQkQ1knnJ36qddU4WvTakU05QDoh0Ipwmf sWs2lSNmbCYnWQVG+nXNA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedruddtgedgudehgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdefhedmnecujfgurhephffvvefufffkofgjfhgggfestdekredt redttdenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpefgfefggeejhfduieekvdeuteffleeifeeuvdfhheejleej jeekgfffgefhtddtteenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 1 Nov 2023 17:58:30 -0400 (EDT) From: Daniel Xu To: kuba@kernel.org, hawk@kernel.org, edumazet@google.com, steffen.klassert@secunet.com, daniel@iogearbox.net, Herbert Xu , ast@kernel.org, john.fastabend@gmail.com, pabeni@redhat.com, davem@davemloft.net, antony.antony@secunet.com Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, devel@linux-ipsec.org Subject: [RFCv2 bpf-next 1/7] bpf: xfrm: Add bpf_xdp_get_xfrm_state() kfunc Date: Wed, 1 Nov 2023 14:57:45 -0700 Message-ID: <0a5dc090a098b911bdd19ed0e63c7e466f7054f6.1698875025.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.42.0 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,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Wed, 01 Nov 2023 14:59:49 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781400650990321388 X-GMAIL-MSGID: 1781400650990321388 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 | 105 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 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..4aaac134b97a --- /dev/null +++ b/net/xfrm/xfrm_state_bpf.c @@ -0,0 +1,105 @@ +// 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 + +/* 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 - Passed NULL for opts + * -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), +}; + +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "Global functions as their definitions will be in xfrm_state BTF"); + +/* 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); + + if (!opts || 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; + } + } + + return xfrm_state_lookup(net, opts->mark, &opts->daddr, opts->spi, + opts->proto, opts->family); +} + +__diag_pop() + +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); +}