Skip to contentSkip to conversation

virtio_net: Support RX hash XDP hint

PATCH Superseded Liang Chen 5 participants 9 messages Jan 22, 2024 – Jan 24, 2024

Subsystems (3)

v1 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Liang Chen

author
#0 lore raw

PATCH 1/1 virtio_net: Support RX hash XDP hint

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
 drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +56 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
 	}
 }
 
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+			   enum xdp_rss_hash_type *rss_type)
+{
+	const struct xdp_buff *xdp = (void *)_ctx;
+	struct virtio_net_hdr_v1_hash *hdr_hash;
+	struct virtnet_info *vi;
+
+	if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
+		return -ENODATA;
+
+	vi = netdev_priv(xdp->rxq->dev);
+	hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
+
+	switch (__le16_to_cpu(hdr_hash->hash_report)) {
+		case VIRTIO_NET_HASH_REPORT_TCPv4:
+			*rss_type = XDP_RSS_TYPE_L4_IPV4_TCP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv4:
+			*rss_type = XDP_RSS_TYPE_L4_IPV4_UDP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_TCPv6:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_TCP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv6:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_UDP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_TCP_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_UDP_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv4:
+			*rss_type = XDP_RSS_TYPE_L3_IPV4;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv6:
+			*rss_type = XDP_RSS_TYPE_L3_IPV6;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L3_IPV6_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_NONE:
+		default:
+			*rss_type = XDP_RSS_TYPE_NONE;
+	}
+
+	*hash = __le32_to_cpu(hdr_hash->hash_value);
+	return 0;
+}
+
+static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
+	.xmo_rx_hash			= virtnet_xdp_rx_hash,
+};
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int i, err = -ENOMEM;
@@ -4613,6 +4667,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 	dev->ethtool_ops = &virtnet_ethtool_ops;
 	SET_NETDEV_DEV(dev, &vdev->dev);
 
+	dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+
 	/* Do we support "hardware" checksums? */
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
 		/* This opens up the world of extra features. */
Signature · 1 line
2.40.1
#1 lore raw

Hi Liang Chen,

Quoted 18 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
  drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
  1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +0 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
  	}

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4589

Quoted 10 lines
drivers/net/virtio_net.c +8 -0
  }
 
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+			   enum xdp_rss_hash_type *rss_type)
+{
+	const struct xdp_buff *xdp = (void *)_ctx;
+	struct virtio_net_hdr_v1_hash *hdr_hash;
+	struct virtnet_info *vi;
+
+	if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))

I think 'vi->has_rss_hash_report' should be used here. NETIF_F_RXHASH cannot guarantee that the hash report feature is negotiated, and accessing hash_report and hash_value is unsafe at this time.

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4593

Quoted 4 lines
drivers/net/virtio_net.c +4 -0
+		return -ENODATA;
+
+	vi = netdev_priv(xdp->rxq->dev);
+	hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);

If the virtio-net-hdr is overrided by the XDP prog, how can this be done correctly and as expected?

Thanks,
Heng

Quoted 54 lines
drivers/net/virtio_net.c +44 -0
+
+	switch (__le16_to_cpu(hdr_hash->hash_report)) {
+		case VIRTIO_NET_HASH_REPORT_TCPv4:
+			*rss_type = XDP_RSS_TYPE_L4_IPV4_TCP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv4:
+			*rss_type = XDP_RSS_TYPE_L4_IPV4_UDP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_TCPv6:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_TCP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv6:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_UDP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_TCP_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_UDP_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv4:
+			*rss_type = XDP_RSS_TYPE_L3_IPV4;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv6:
+			*rss_type = XDP_RSS_TYPE_L3_IPV6;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L3_IPV6_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_NONE:
+		default:
+			*rss_type = XDP_RSS_TYPE_NONE;
+	}
+
+	*hash = __le32_to_cpu(hdr_hash->hash_value);
+	return 0;
+}
+
+static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
+	.xmo_rx_hash			= virtnet_xdp_rx_hash,
+};
+
  static int virtnet_probe(struct virtio_device *vdev)
  {
  	int i, err = -ENOMEM;
@@ -4613,6 +4667,8 @@ static int virtnet_probe(struct virtio_device *vdev)
  	dev->ethtool_ops = &virtnet_ethtool_ops;
  	SET_NETDEV_DEV(dev, &vdev->dev);
 
+	dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+
  	/* Do we support "hardware" checksums? */
  	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
  		/* This opens up the world of extra features. */
#5 lore raw
In reply to Liang Chen
Quoted 78 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
 drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +54 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
 	}
 }
 
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+			   enum xdp_rss_hash_type *rss_type)
+{
+	const struct xdp_buff *xdp = (void *)_ctx;
+	struct virtio_net_hdr_v1_hash *hdr_hash;
+	struct virtnet_info *vi;
+
+	if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
+		return -ENODATA;
+
+	vi = netdev_priv(xdp->rxq->dev);
+	hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
+
+	switch (__le16_to_cpu(hdr_hash->hash_report)) {
+		case VIRTIO_NET_HASH_REPORT_TCPv4:
+			*rss_type = XDP_RSS_TYPE_L4_IPV4_TCP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv4:
+			*rss_type = XDP_RSS_TYPE_L4_IPV4_UDP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_TCPv6:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_TCP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv6:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_UDP;
+			break;
+		case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_TCP_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L4_IPV6_UDP_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv4:
+			*rss_type = XDP_RSS_TYPE_L3_IPV4;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv6:
+			*rss_type = XDP_RSS_TYPE_L3_IPV6;
+			break;
+		case VIRTIO_NET_HASH_REPORT_IPv6_EX:
+			*rss_type = XDP_RSS_TYPE_L3_IPV6_EX;
+			break;
+		case VIRTIO_NET_HASH_REPORT_NONE:
+		default:
+			*rss_type = XDP_RSS_TYPE_NONE;
+	}
+
+	*hash = __le32_to_cpu(hdr_hash->hash_value);
+	return 0;
+}
+
+static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
+	.xmo_rx_hash			= virtnet_xdp_rx_hash,
+};
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int i, err = -ENOMEM;

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 4671

Quoted 5 lines
drivers/net/virtio_net.c +2 -0
@@ -4613,6 +4667,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 	dev->ethtool_ops = &virtnet_ethtool_ops;
 	SET_NETDEV_DEV(dev, &vdev->dev);
 
+	dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+

How about making this assignment depend on

xdp->rxq->dev->features & NETIF_F_RXHASH

?

Quoted 5 lines
drivers/net/virtio_net.c +0 -0
 	/* Do we support "hardware" checksums? */
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
 		/* This opens up the world of extra features. */
Signature · 1 line
2.40.1
author
#2 lore raw
In reply to Heng Qi
Quoted 24 lines · Heng Qi

Hi Liang Chen,

Quoted 20 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
  drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
  1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +0 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
      }
  }
 
Quoted 13 lines

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4589

Quoted 8 lines
drivers/net/virtio_net.c +8 -0
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+                        enum xdp_rss_hash_type *rss_type)
+{
+     const struct xdp_buff *xdp = (void *)_ctx;
+     struct virtio_net_hdr_v1_hash *hdr_hash;
+     struct virtnet_info *vi;
+
+     if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))

I think 'vi->has_rss_hash_report' should be used here. NETIF_F_RXHASH cannot guarantee that the hash report feature is negotiated, and accessing hash_report and hash_value is unsafe at this time.

My understanding is that rxhash in dev->features is turned on only if the corresponding hw feature is set. We will double check on this.

Quoted 8 lines

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4593

Quoted 4 lines
drivers/net/virtio_net.c +4 -0
+             return -ENODATA;
+
+     vi = netdev_priv(xdp->rxq->dev);
+     hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);

If the virtio-net-hdr is overrided by the XDP prog, how can this be done correctly and as expected?

Yeah, thanks for bringing up this concern. We are actively working on a fix of this issue(possibly with a wrapper structure of xdp_buff).

Thanks,
Liang

Quoted 58 lines

Thanks,
Heng

Quoted 54 lines
drivers/net/virtio_net.c +44 -0
+
+     switch (__le16_to_cpu(hdr_hash->hash_report)) {
+             case VIRTIO_NET_HASH_REPORT_TCPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv4:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV4;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_NONE:
+             default:
+                     *rss_type = XDP_RSS_TYPE_NONE;
+     }
+
+     *hash = __le32_to_cpu(hdr_hash->hash_value);
+     return 0;
+}
+
+static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
+     .xmo_rx_hash                    = virtnet_xdp_rx_hash,
+};
+
  static int virtnet_probe(struct virtio_device *vdev)
  {
      int i, err = -ENOMEM;
@@ -4613,6 +4667,8 @@ static int virtnet_probe(struct virtio_device *vdev)
      dev->ethtool_ops = &virtnet_ethtool_ops;
      SET_NETDEV_DEV(dev, &vdev->dev);
 
+     dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+
      /* Do we support "hardware" checksums? */
      if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
              /* This opens up the world of extra features. */
#7 lore raw
In reply to Liang Chen
Quoted 23 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
 drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +2 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
        }
 }
 
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+                          enum xdp_rss_hash_type *rss_type)

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4593

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+{
+       const struct xdp_buff *xdp = (void *)_ctx;
+       struct virtio_net_hdr_v1_hash *hdr_hash;
+       struct virtnet_info *vi;
+
+       if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
+               return -ENODATA;
+
+       vi = netdev_priv(xdp->rxq->dev);
+       hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);

Is there a guarantee that the hdr is not modified?

Thanks

author
#6 lore raw
In reply to Michael S. Tsirkin
Quoted 83 lines · Michael S. Tsirkin
Quoted 81 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
 drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +54 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
      }
 }
 
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+                        enum xdp_rss_hash_type *rss_type)
+{
+     const struct xdp_buff *xdp = (void *)_ctx;
+     struct virtio_net_hdr_v1_hash *hdr_hash;
+     struct virtnet_info *vi;
+
+     if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
+             return -ENODATA;
+
+     vi = netdev_priv(xdp->rxq->dev);
+     hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
+
+     switch (__le16_to_cpu(hdr_hash->hash_report)) {
+             case VIRTIO_NET_HASH_REPORT_TCPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv4:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV4;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_NONE:
+             default:
+                     *rss_type = XDP_RSS_TYPE_NONE;
+     }
+
+     *hash = __le32_to_cpu(hdr_hash->hash_value);
+     return 0;
+}
+
+static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
+     .xmo_rx_hash                    = virtnet_xdp_rx_hash,
+};
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
      int i, err = -ENOMEM;
@@ -4613,6 +4667,8 @@ static int virtnet_probe(struct virtio_device *vdev)
      dev->ethtool_ops = &virtnet_ethtool_ops;
      SET_NETDEV_DEV(dev, &vdev->dev);
 
Quoted 7 lines

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev) · line 4671

Quoted 2 lines
drivers/net/virtio_net.c +2 -0
+     dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+

How about making this assignment depend on

xdp->rxq->dev->features & NETIF_F_RXHASH

Thanks! How about dev->hw_features & NETIF_F_RXHASH? dev->features & NETIF_F_RXHASH has not been enabled at this point.

Quoted 8 lines

?

Quoted 5 lines
drivers/net/virtio_net.c +0 -1
      /* Do we support "hardware" checksums? */
      if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
              /* This opens up the world of extra features. */
--

2.40.1

reviewer
#3 lore raw
In reply to Liang Chen
Quoted 47 lines · Liang Chen
Quoted 24 lines · Heng Qi

Hi Liang Chen,

Quoted 20 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
  drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
  1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +0 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
      }
  }
 
Quoted 13 lines

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4589

Quoted 8 lines
drivers/net/virtio_net.c +8 -0
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+                        enum xdp_rss_hash_type *rss_type)
+{
+     const struct xdp_buff *xdp = (void *)_ctx;
+     struct virtio_net_hdr_v1_hash *hdr_hash;
+     struct virtnet_info *vi;
+
+     if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))

I think 'vi->has_rss_hash_report' should be used here. NETIF_F_RXHASH cannot guarantee that the hash report feature is negotiated, and accessing hash_report and hash_value is unsafe at this time.

My understanding is that rxhash in dev->features is turned on only if the corresponding hw feature is set. We will double check on this.

Quoted 5 lines
Quoted 4 lines
drivers/net/virtio_net.c +4 -0
+             return -ENODATA;
+
+     vi = netdev_priv(xdp->rxq->dev);
+     hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
Quoted 6 lines
Quoted 3 lines

If the virtio-net-hdr is overrided by the XDP prog, how can this be done correctly and as expected?

Yeah, thanks for bringing up this concern. We are actively working on a fix of this issue(possibly with a wrapper structure of xdp_buff).

Are there some places to save the hash before run xdp?

Thanks.

Quoted 63 lines

Thanks,
Liang

Quoted 58 lines

Thanks,
Heng

Quoted 54 lines
drivers/net/virtio_net.c +44 -0
+
+     switch (__le16_to_cpu(hdr_hash->hash_report)) {
+             case VIRTIO_NET_HASH_REPORT_TCPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv4:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV4;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_NONE:
+             default:
+                     *rss_type = XDP_RSS_TYPE_NONE;
+     }
+
+     *hash = __le32_to_cpu(hdr_hash->hash_value);
+     return 0;
+}
+
+static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
+     .xmo_rx_hash                    = virtnet_xdp_rx_hash,
+};
+
  static int virtnet_probe(struct virtio_device *vdev)
  {
      int i, err = -ENOMEM;
@@ -4613,6 +4667,8 @@ static int virtnet_probe(struct virtio_device *vdev)
      dev->ethtool_ops = &virtnet_ethtool_ops;
      SET_NETDEV_DEV(dev, &vdev->dev);
 
+     dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+
      /* Do we support "hardware" checksums? */
      if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
              /* This opens up the world of extra features. */
author
#8 lore raw
In reply to Jason Wang
Quoted 23 lines · Jason Wang
Quoted 21 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
 drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +0 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
        }
 }
 
Quoted 15 lines

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4593

Quoted 12 lines
drivers/net/virtio_net.c +12 -0
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+                          enum xdp_rss_hash_type *rss_type)
+{
+       const struct xdp_buff *xdp = (void *)_ctx;
+       struct virtio_net_hdr_v1_hash *hdr_hash;
+       struct virtnet_info *vi;
+
+       if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
+               return -ENODATA;
+
+       vi = netdev_priv(xdp->rxq->dev);
+       hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);

Is there a guarantee that the hdr is not modified?

We cannot guarantee the hdr is not modified by the XDP prog, So the idea is to save it in a wrapper structure before running the xdp prog. Patch is coming soon.

Thanks,
Liang

Quoted 2 lines

Thanks

author
#4 lore raw
In reply to Xuan Zhuo
Quoted 49 lines · Xuan Zhuo
Quoted 47 lines · Liang Chen
Quoted 24 lines · Heng Qi

Hi Liang Chen,

Quoted 20 lines · Liang Chen

The RSS hash report is a feature that's part of the virtio specification. Currently, virtio backends like qemu, vdpa (mlx5), and potentially vhost (still a work in progress as per [1]) support this feature. While the capability to obtain the RSS hash has been enabled in the normal path, it's currently missing in the XDP path. Therefore, we are introducing XDP hints through kfuncs to allow XDP programs to access the RSS hash.

---
  drivers/net/virtio_net.c | 56 ++++++++++++++++++++++++++++++++++++++++
  1 file changed, 56 insertions(+)
drivers/net/virtio_net.c +0 -0
index d7ce4a1011ea..1463a4709e3c 100644
@@ -4579,6 +4579,60 @@ static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
      }
  }
 
Quoted 13 lines

drivers/net/virtio_net.c static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu) · line 4589

Quoted 8 lines
drivers/net/virtio_net.c +8 -0
+static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
+                        enum xdp_rss_hash_type *rss_type)
+{
+     const struct xdp_buff *xdp = (void *)_ctx;
+     struct virtio_net_hdr_v1_hash *hdr_hash;
+     struct virtnet_info *vi;
+
+     if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))

I think 'vi->has_rss_hash_report' should be used here. NETIF_F_RXHASH cannot guarantee that the hash report feature is negotiated, and accessing hash_report and hash_value is unsafe at this time.

My understanding is that rxhash in dev->features is turned on only if the corresponding hw feature is set. We will double check on this.

Quoted 5 lines
Quoted 4 lines
drivers/net/virtio_net.c +4 -0
+             return -ENODATA;
+
+     vi = netdev_priv(xdp->rxq->dev);
+     hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

If the virtio-net-hdr is overrided by the XDP prog, how can this be done correctly and as expected?

Yeah, thanks for bringing up this concern. We are actively working on a fix of this issue(possibly with a wrapper structure of xdp_buff).

Are there some places to save the hash before run xdp?

Yeah, it will be saved in a wrapper structure. Thanks.

Quoted 66 lines

Thanks.

Quoted 63 lines

Thanks,
Liang

Quoted 58 lines

Thanks,
Heng

Quoted 54 lines
drivers/net/virtio_net.c +44 -0
+
+     switch (__le16_to_cpu(hdr_hash->hash_report)) {
+             case VIRTIO_NET_HASH_REPORT_TCPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv4:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV4_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_TCP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L4_IPV6_UDP_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv4:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV4;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_IPv6_EX:
+                     *rss_type = XDP_RSS_TYPE_L3_IPV6_EX;
+                     break;
+             case VIRTIO_NET_HASH_REPORT_NONE:
+             default:
+                     *rss_type = XDP_RSS_TYPE_NONE;
+     }
+
+     *hash = __le32_to_cpu(hdr_hash->hash_value);
+     return 0;
+}
+
+static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
+     .xmo_rx_hash                    = virtnet_xdp_rx_hash,
+};
+
  static int virtnet_probe(struct virtio_device *vdev)
  {
      int i, err = -ENOMEM;
@@ -4613,6 +4667,8 @@ static int virtnet_probe(struct virtio_device *vdev)
      dev->ethtool_ops = &virtnet_ethtool_ops;
      SET_NETDEV_DEV(dev, &vdev->dev);
 
+     dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
+
      /* Do we support "hardware" checksums? */
      if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
              /* This opens up the world of extra features. */