Subject: Re: [PATCH bpf RFC-V3 1/5] xdp: rss hash types representation

From: Stanislav Fomichev <[email-masked]>
Date: Thu, 30 Mar 2023 11:35:38 -0700

On 03/30, Jesper Dangaard Brouer wrote:
> The RSS hash type specifies what portion of packet data NIC hardware used
> when calculating RSS hash value. The RSS types are focused on Internet
> traffic protocols at OSI layers L3 and L4. L2 (e.g. ARP) often get hash
> value zero and no RSS type. For L3 focused on IPv4 vs. IPv6, and L4
> primarily TCP vs UDP, but some hardware supports SCTP.

> Hardware RSS types are differently encoded for each hardware NIC. Most
> hardware represent RSS hash type as a number. Determining L3 vs L4 often
> requires a mapping table as there often isn't a pattern or sorting
> according to ISO layer.

> The patch introduce a XDP RSS hash type (enum xdp_rss_hash_type) that
> contain combinations to be used by drivers, which gets build up with bits
> from enum xdp_rss_type_bits. Both enum xdp_rss_type_bits and
> xdp_rss_hash_type get exposed to BPF via BTF, and it is up to the
> BPF-programmer to match using these defines.

> This proposal change the kfunc API bpf_xdp_metadata_rx_hash() adding
> a pointer value argument for provide the RSS hash type.

> Signed-off-by: Jesper Dangaard Brouer <[email-masked]>
> ---
>   include/linux/netdevice.h |    3 ++-
>   include/net/xdp.h         |   46  
> +++++++++++++++++++++++++++++++++++++++++++++
>   net/core/xdp.c            |   10 +++++++++-
>   3 files changed, 57 insertions(+), 2 deletions(-)

> diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
> index 470085b121d3..c35f04f636f1 100644
> --- a/include/linux/netdevice.h
> +++ b/include/linux/netdevice.h
> @@ -1624,7 +1624,8 @@ struct net_device_ops {

>   struct xdp_metadata_ops {
>   	int	(*xmo_rx_timestamp)(const struct xdp_md *ctx, u64 *timestamp);
> -	int	(*xmo_rx_hash)(const struct xdp_md *ctx, u32 *hash);
> +	int	(*xmo_rx_hash)(const struct xdp_md *ctx, u32 *hash,
> +			       enum xdp_rss_hash_type *rss_type);
>   };

>   /**
> diff --git a/include/net/xdp.h b/include/net/xdp.h
> index 41c57b8b1671..130091a55a6f 100644
> --- a/include/net/xdp.h
> +++ b/include/net/xdp.h
> @@ -8,6 +8,7 @@

>   #include <linux/skbuff.h> /* skb_shared_info */
>   #include <uapi/linux/netdev.h>
> +#include <linux/bitfield.h>

>   /**
>    * DOC: XDP RX-queue information
> @@ -425,6 +426,51 @@ XDP_METADATA_KFUNC_xxx
>   MAX_XDP_METADATA_KFUNC,
>   };

> +enum xdp_rss_type_bits {
> +	XDP_RSS_L3_IPV4		= BIT(0),
> +	XDP_RSS_L3_IPV6		= BIT(1),
> +
> +	/* The fixed (L3) IPv4 and IPv6 headers can both be followed by
> +	 * variable/dynamic headers, IPv4 called Options and IPv6 called
> +	 * Extension Headers. HW RSS type can contain this info.
> +	 */
> +	XDP_RSS_L3_DYNHDR	= BIT(2),
> +
> +	/* When RSS hash covers L4 then drivers MUST set XDP_RSS_L4 bit in
> +	 * addition to the protocol specific bit.  This ease interaction with
> +	 * SKBs and avoids reserving a fixed mask for future L4 protocol bits.
> +	 */
> +	XDP_RSS_L4		= BIT(3), /* L4 based hash, proto can be unknown */
> +	XDP_RSS_L4_TCP		= BIT(4),
> +	XDP_RSS_L4_UDP		= BIT(5),
> +	XDP_RSS_L4_SCTP		= BIT(6),
> +	XDP_RSS_L4_IPSEC	= BIT(7), /* L4 based hash include IPSEC SPI */
> +};
> +
> +/* RSS hash type combinations used for driver HW mapping */
> +enum xdp_rss_hash_type {
> +	XDP_RSS_TYPE_NONE            = 0,
> +	XDP_RSS_TYPE_L2              = XDP_RSS_TYPE_NONE,
> +
> +	XDP_RSS_TYPE_L3_IPV4         = XDP_RSS_L3_IPV4,
> +	XDP_RSS_TYPE_L3_IPV6         = XDP_RSS_L3_IPV6,
> +	XDP_RSS_TYPE_L3_IPV4_OPT     = XDP_RSS_L3_IPV4 | XDP_RSS_L3_DYNHDR,
> +	XDP_RSS_TYPE_L3_IPV6_EX      = XDP_RSS_L3_IPV6 | XDP_RSS_L3_DYNHDR,
> +
> +	XDP_RSS_TYPE_L4_ANY          = XDP_RSS_L4,
> +	XDP_RSS_TYPE_L4_IPV4_TCP     = XDP_RSS_L3_IPV4 | XDP_RSS_L4 |  
> XDP_RSS_L4_TCP,
> +	XDP_RSS_TYPE_L4_IPV4_UDP     = XDP_RSS_L3_IPV4 | XDP_RSS_L4 |  
> XDP_RSS_L4_UDP,
> +	XDP_RSS_TYPE_L4_IPV4_SCTP    = XDP_RSS_L3_IPV4 | XDP_RSS_L4 |  
> XDP_RSS_L4_SCTP,
> +
> +	XDP_RSS_TYPE_L4_IPV6_TCP     = XDP_RSS_L3_IPV6 | XDP_RSS_L4 |  
> XDP_RSS_L4_TCP,
> +	XDP_RSS_TYPE_L4_IPV6_UDP     = XDP_RSS_L3_IPV6 | XDP_RSS_L4 |  
> XDP_RSS_L4_UDP,
> +	XDP_RSS_TYPE_L4_IPV6_SCTP    = XDP_RSS_L3_IPV6 | XDP_RSS_L4 |  
> XDP_RSS_L4_SCTP,
> +
> +	XDP_RSS_TYPE_L4_IPV6_TCP_EX  = XDP_RSS_TYPE_L4_IPV6_TCP | 
> XDP_RSS_L3_DYNHDR,
> +	XDP_RSS_TYPE_L4_IPV6_UDP_EX  = XDP_RSS_TYPE_L4_IPV6_UDP | 
> XDP_RSS_L3_DYNHDR,
> +	XDP_RSS_TYPE_L4_IPV6_SCTP_EX = XDP_RSS_TYPE_L4_IPV6_SCTP| 
> XDP_RSS_L3_DYNHDR,
> +};
> +
>   #ifdef CONFIG_NET
>   u32 bpf_xdp_metadata_kfunc_id(int id);
>   bool bpf_dev_bound_kfunc_id(u32 btf_id);
> diff --git a/net/core/xdp.c b/net/core/xdp.c
> index 528d4b37983d..38d2dee16b47 100644
> --- a/net/core/xdp.c
> +++ b/net/core/xdp.c
> @@ -734,14 +734,22 @@ __bpf_kfunc int bpf_xdp_metadata_rx_timestamp(const  
> struct xdp_md *ctx, u64 *tim
>    * bpf_xdp_metadata_rx_hash - Read XDP frame RX hash.
>    * @ctx: XDP context pointer.
>    * @hash: Return value pointer.
> + * @rss_type: Return value pointer for RSS type.
> + *
> + * The RSS hash type (@rss_type) specifies what portion of packet  
> headers NIC
> + * hardware were used when calculating RSS hash value.  The type  
> combinations
> + * are defined via &enum xdp_rss_hash_type and individual bits can be  
> decoded
> + * via &enum xdp_rss_type_bits.
>    *
>    * Return:
>    * * Returns 0 on success or ``-errno`` on error.
>    * * ``-EOPNOTSUPP`` : means device driver doesn't implement kfunc
>    * * ``-ENODATA``    : means no RX-hash available for this frame
>    */
> -__bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32  
> *hash)
> +__bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32  
> *hash,
> +					 enum xdp_rss_hash_type *rss_type)
>   {

[..]

> +	BTF_TYPE_EMIT(enum xdp_rss_type_bits);

nit: Do we still need this with an extra argument?

>   	return -EOPNOTSUPP;
>   }