netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Daniel Xu <dxu@dxuuu.xyz>,
	john.fastabend@gmail.com,
	Herbert Xu <herbert@gondor.apana.org.au>,
	davem@davemloft.net, ast@kernel.org, daniel@iogearbox.net,
	pabeni@redhat.com, hawk@kernel.org, kuba@kernel.org,
	edumazet@google.com, steffen.klassert@secunet.com,
	antony.antony@secunet.com, alexei.starovoitov@gmail.com
Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	bpf@vger.kernel.org, devel@linux-ipsec.org
Subject: Re: [PATCH ipsec-next v1 1/7] bpf: xfrm: Add bpf_xdp_get_xfrm_state() kfunc
Date: Sat, 25 Nov 2023 12:36:29 -0800	[thread overview]
Message-ID: <0e72fb5b-2e26-4c28-b139-68203cd72e59@linux.dev> (raw)
In-Reply-To: <2443b6093691c7ae9dace98b0257f61ff2ff30ec.1700676682.git.dxu@dxuuu.xyz>


On 11/22/23 1:20 PM, Daniel Xu wrote:
> 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 <antony.antony@secunet.com>
> Signed-off-by: Antony Antony <antony.antony@secunet.com>
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> ---
>   include/net/xfrm.h        |   9 ++++
>   net/xfrm/Makefile         |   1 +
>   net/xfrm/xfrm_policy.c    |   2 +
>   net/xfrm/xfrm_state_bpf.c | 111 ++++++++++++++++++++++++++++++++++++++
>   4 files changed, 123 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..0c1f2f91125c
> --- /dev/null
> +++ b/net/xfrm/xfrm_state_bpf.c
> @@ -0,0 +1,111 @@
> +// 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 <linux/bpf.h>
> +#include <linux/btf_ids.h>
> +#include <net/xdp.h>
> +#include <net/xfrm.h>
> +
> +/* 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);
> +	struct xfrm_state *x;
> +
> +	if (!opts || opts__sz != BPF_XFRM_STATE_OPTS_SZ) {
> +		opts->error = -EINVAL;

If opts is NULL, obvious we have issue opts->error access.
If opts is not NULL and opts_sz < 4, we also have issue with
opts->error access since it may override some other stuff
on the stack.

In such cases, we do not need to do 'opts->error = -EINVAL'
and can simply 'return NULL'. bpf program won't be able
to check opts->error anyway since the opts is either NULL
or opts_sz < 4.

> +		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;
> +}
> +
> +__diag_pop()

[...]


  parent reply	other threads:[~2023-11-25 20:36 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-22 18:20 [PATCH ipsec-next v1 0/7] Add bpf_xdp_get_xfrm_state() kfunc Daniel Xu
2023-11-22 18:20 ` [PATCH ipsec-next v1 1/7] bpf: xfrm: " Daniel Xu
2023-11-22 23:26   ` Alexei Starovoitov
2023-11-25 20:36   ` Yonghong Song [this message]
2023-11-26  4:38     ` Daniel Xu
2023-11-22 18:20 ` [PATCH ipsec-next v1 2/7] bpf: xfrm: Add bpf_xdp_xfrm_state_release() kfunc Daniel Xu
2023-11-22 18:20 ` [PATCH ipsec-next v1 3/7] bpf: selftests: test_tunnel: Use ping -6 over ping6 Daniel Xu
2023-11-22 18:20 ` [PATCH ipsec-next v1 4/7] bpf: selftests: test_tunnel: Mount bpffs if necessary Daniel Xu
2023-11-22 18:20 ` [PATCH ipsec-next v1 5/7] bpf: selftests: test_tunnel: Use vmlinux.h declarations Daniel Xu
2023-11-26  0:34   ` Yonghong Song
2023-11-26  4:34     ` Daniel Xu
2023-11-22 18:20 ` [PATCH ipsec-next v1 6/7] bpf: selftests: test_tunnel: Disable CO-RE relocations Daniel Xu
2023-11-26  0:51   ` Yonghong Song
2023-11-26  0:54     ` Alexei Starovoitov
2023-11-26  4:22       ` Yonghong Song
2023-11-26 20:14         ` Eduard Zingerman
2023-11-27  0:04           ` Daniel Xu
2023-11-27  1:52             ` Eduard Zingerman
2023-11-27  5:44               ` Yonghong Song
2023-11-27  5:53                 ` Yonghong Song
2023-11-27 20:45                   ` Daniel Xu
2023-11-27 21:32                     ` Eduard Zingerman
2023-11-28  0:01                     ` Daniel Xu
2023-11-28  4:06                       ` Yonghong Song
2023-11-28 16:02                         ` Andrii Nakryiko
2023-11-28 16:13                         ` Daniel Xu
2023-11-28 16:17                           ` Daniel Xu
2023-11-28 16:56                             ` Yonghong Song
2023-11-28 16:19                           ` Eduard Zingerman
2023-11-27  5:20           ` Yonghong Song
2023-11-22 18:20 ` [PATCH ipsec-next v1 7/7] bpf: xfrm: Add selftest for bpf_xdp_get_xfrm_state() Daniel Xu
2023-11-22 23:28   ` Alexei Starovoitov
2023-11-24 20:59     ` Daniel Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0e72fb5b-2e26-4c28-b139-68203cd72e59@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=alexei.starovoitov@gmail.com \
    --cc=antony.antony@secunet.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=devel@linux-ipsec.org \
    --cc=dxu@dxuuu.xyz \
    --cc=edumazet@google.com \
    --cc=hawk@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=steffen.klassert@secunet.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).