Skip to contentSkip to conversation

virtio_net: Implement VIRTIO_NET_F_OUT_NET_HEADER from virtio spec 1.4

PATCH In review Kommula Shiva Shankar 4 participants 7 messages Oct 8, 2025 – Oct 9, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover virtio_net: Implement VIRTIO_NET_F_OUT_NET_HEADER from virtio spec 1.4

This series implements the VIRTIO_NET_F_OUT_NET_HEADER feature which was introduced in the virtio specification 1.4 recently. With this change, segmentation offload functionality now propagates outer IP header offset information to the device/apps running on NIC.

The virtio 1.4 spec introduced VIRTIO_NET_F_OUT_NET_HEADER feature[1] to address the performance issue caused by reading packet data to determine outer network header offset and to support Inline IPSec functionality,
where vendor hardware devices need to know the outer network header offset for Inline IPSec acceleration.

Currently, devices must parse through packet headers to determine the outer network header which impacts performance. This patch series implements the outer_nh_offset field in virtio_net_hdr to avoid parsing overhead.

This series was tested on ARM platform with virtio-net driver where ~4% performance improvement was measured for device applications.

V1 -> V2: Added IPv6 support to outer network header offset feature
Fixed sparse build warnings

[1] https://lore.kernel.org/virtio-comment/[email protected]/

Kommula Shiva Shankar (3):
net: implement virtio helper to handle outer nw offset
virtio_net: enable outer nw header offset support vhost/net: enable outer nw header offset support

 drivers/net/virtio_net.c        | 23 +++++++++++++++----
 drivers/vhost/net.c             |  4 ++++
 include/linux/virtio_net.h      | 40 +++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_net.h |  8 +++++++
 4 files changed, 71 insertions(+), 4 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v2 1/3 net: implement virtio helper to handle outer nw offset

virtio specification introduced support for outer network header offset broadcast.

This patch implements the needed defines and virtio header parsing capabilities.

---
 include/linux/virtio_net.h      | 40 +++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_net.h |  8 +++++++
 2 files changed, 48 insertions(+)
include/linux/virtio_net.h +40 -0
index 20e0584db1dd..7ab872a11a21 100644
@@ -374,6 +374,46 @@ static inline int virtio_net_handle_csum_offload(struct sk_buff *skb,
 	return 0;
 }
 
+static inline int
+virtio_net_out_net_header_to_skb(struct sk_buff *skb,
+				 struct virtio_net_hdr_v1_hash_tunnel_out_net_hdr *vhdr,
+				 bool out_net_hdr_negotiated,
+				 bool little_endian)
+{
+	unsigned int out_net_hdr_off;
+
+	if (!out_net_hdr_negotiated)
+		return 0;
+
+	if (vhdr->outer_nh_offset) {
+		out_net_hdr_off = le16_to_cpu(vhdr->outer_nh_offset);
+		skb_set_network_header(skb, out_net_hdr_off);
+	}
+
+	return 0;
+}
+
+static inline int
+virtio_net_out_net_header_from_skb(const struct sk_buff *skb,
+				   struct virtio_net_hdr_v1_hash_tunnel_out_net_hdr *vhdr,
+				   bool out_net_hdr_negotiated,
+				   bool little_endian)
+{
+	unsigned int out_net_hdr_off;
+
+	if (!out_net_hdr_negotiated) {
+		vhdr->outer_nh_offset = 0;
+		return 0;
+	}
+
+	out_net_hdr_off = skb_network_offset(skb);
+	if (out_net_hdr_off && (skb->protocol == htons(ETH_P_IP) ||
+				skb->protocol == htons(ETH_P_IPV6)))
+		vhdr->outer_nh_offset = cpu_to_le16(out_net_hdr_off);
+
+	return 0;
+}
+
 /*
  * vlan_hlen always refers to the outermost MAC header. That also
  * means it refers to the only MAC header, if the packet does not carry
include/uapi/linux/virtio_net.h +8 -0
index 8bf27ab8bcb4..6032b9e443bb 100644
@@ -86,6 +86,7 @@
 						  * packets with partial csum
 						  * for the outer header
 						  */
+#define VIRTIO_NET_F_OUT_NET_HEADER 69	/* Outer network header offset */
 
 /* Offloads bits corresponding to VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO{,_CSUM}
  * features
@@ -214,6 +215,13 @@ struct virtio_net_hdr_v1_hash_tunnel {
 	__le16 inner_nh_offset;
 };
 
+/* outer network header */
+struct virtio_net_hdr_v1_hash_tunnel_out_net_hdr {
+	struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
+	__le16 outer_nh_offset;
+	__u8 padding_reserved_2[6];
+};
+
 #ifndef VIRTIO_NET_NO_LEGACY
 /* This header comes first in the scatter-gather list.
  * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated, it must
Signature · 1 line
2.48.1
#2 lore raw

PATCH v2 2/3 virtio_net: enable outer nw header offset support

In reply to Kommula Shiva Shankar

If outer network header feature is set, update header offset in ingress and egress path.

If feature is not set, reset header offset to zero.

---
 drivers/net/virtio_net.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
drivers/net/virtio_net.c +19 -4
index 7da5a37917e9..0b60cbbb3d33 100644
@@ -451,6 +451,9 @@ struct virtnet_info {
 
 	bool rx_tnl_csum;
 
+	/* Outer network header support */
+	bool out_net_hdr_negotiated;
+
 	/* Is delayed refill enabled? */
 	bool refill_enabled;
 
@@ -511,6 +514,7 @@ struct virtio_net_common_hdr {
 		struct virtio_net_hdr_mrg_rxbuf	mrg_hdr;
 		struct virtio_net_hdr_v1_hash hash_v1_hdr;
 		struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
+		struct virtio_net_hdr_v1_hash_tunnel_out_net_hdr out_net_hdr;
 	};
 };
 
@@ -2576,8 +2580,8 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *
 	hdr->hdr.flags = flags;
 	if (virtio_net_handle_csum_offload(skb, &hdr->hdr, vi->rx_tnl_csum)) {
 		net_warn_ratelimited("%s: bad csum: flags: %x, gso_type: %x rx_tnl_csum %d\n",
-				     dev->name, hdr->hdr.flags,
-				     hdr->hdr.gso_type, vi->rx_tnl_csum);
+				dev->name, hdr->hdr.flags,
+				hdr->hdr.gso_type, vi->rx_tnl_csum);
 		goto frame_err;
 	}
 
@@ -2591,6 +2595,8 @@ static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *
 		goto frame_err;
 	}
 
+	virtio_net_out_net_header_to_skb(skb, &hdr->out_net_hdr, vi->out_net_hdr_negotiated,
+					 virtio_is_little_endian(vi->vdev));
 	skb_record_rx_queue(skb, vq2rxq(rq->vq));
 	skb->protocol = eth_type_trans(skb, dev);
 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
@@ -3317,6 +3323,9 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
 	else
 		hdr = &skb_vnet_common_hdr(skb)->tnl_hdr;
 
+	virtio_net_out_net_header_from_skb(skb, &skb_vnet_common_hdr(skb)->out_net_hdr,
+					   vi->out_net_hdr_negotiated,
+					   virtio_is_little_endian(vi->vdev));
 	if (virtio_net_hdr_tnl_from_skb(skb, hdr, vi->tx_tnl,
 					virtio_is_little_endian(vi->vdev), 0))
 		return -EPROTO;
@@ -6915,8 +6924,10 @@ static int virtnet_probe(struct virtio_device *vdev)
 		dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
 	}
 
-	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) ||
-	    virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
+	if (virtio_has_feature(vdev, VIRTIO_NET_F_OUT_NET_HEADER))
+		vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel_out_net_hdr);
+	else if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) ||
+		 virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
 		vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel);
 	else if (vi->has_rss_hash_report)
 		vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
@@ -6933,6 +6944,9 @@ static int virtnet_probe(struct virtio_device *vdev)
 	if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
 		vi->tx_tnl = true;
 
+	if (virtio_has_feature(vdev, VIRTIO_NET_F_OUT_NET_HEADER))
+		vi->out_net_hdr_negotiated = true;
+
 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
 		vi->any_header_sg = true;
@@ -7247,6 +7261,7 @@ static unsigned int features[] = {
 	VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM,
 	VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO,
 	VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM,
+	VIRTIO_NET_F_OUT_NET_HEADER,
 };
 
 static unsigned int features_legacy[] = {
Signature · 1 line
2.48.1
#3 lore raw

PATCH v2 3/3 vhost/net: enable outer nw header offset support

In reply to Kommula Shiva Shankar

apprise vhost net about the virtio net header size.

---
 drivers/vhost/net.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/vhost/net.c +4 -0
index 35ded4330431..8d055405746d 100644
@@ -78,6 +78,7 @@ static const u64 vhost_net_features[VIRTIO_FEATURES_DWORDS] = {
 	(1ULL << VIRTIO_F_IN_ORDER),
 	VIRTIO_BIT(VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) |
 	VIRTIO_BIT(VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO),
+	VIRTIO_BIT(VIRTIO_NET_F_OUT_NET_HEADER),
 };
 
 enum {
@@ -1655,6 +1656,9 @@ static int vhost_net_set_features(struct vhost_net *n, const u64 *features)
 		  sizeof(struct virtio_net_hdr_mrg_rxbuf) :
 		  sizeof(struct virtio_net_hdr);
 
+	if (virtio_features_test_bit(features,
+				     VIRTIO_NET_F_OUT_NET_HEADER))
+		hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel_out_net_hdr);
 	if (virtio_features_test_bit(features,
 				     VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO) ||
 	    virtio_features_test_bit(features,
Signature · 1 line
2.48.1
maintainer
#4 lore raw

Re: [PATCH v2 net-next 3/3] vhost/net: enable outer nw header offset support

Quoted 12 lines · Kommula Shiva Shankar

apprise vhost net about the virtio net header size.

---
 drivers/vhost/net.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/vhost/net.c +0 -0
index 35ded4330431..8d055405746d 100644

drivers/vhost/net.c static const u64 vhost_net_features[VIRTIO_FEATURES_DWORDS] = { · line 81

Quoted 4 lines
drivers/vhost/net.c +1 -0
@@ -78,6 +78,7 @@ static const u64 vhost_net_features[VIRTIO_FEATURES_DWORDS] = {
 	(1ULL << VIRTIO_F_IN_ORDER),
 	VIRTIO_BIT(VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) |
 	VIRTIO_BIT(VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO),
+	VIRTIO_BIT(VIRTIO_NET_F_OUT_NET_HEADER),

I guess this patch needs better testing; the above triggers a compile warning:

./drivers/vhost/net.c:81:2: warning: excess elements in array initializer [-Wexcess-initializers]
81 | VIRTIO_BIT(VIRTIO_NET_F_OUT_NET_HEADER),

which in turns hints the feature is not actually used/available. The chunk should be:

VIRTIO_BIT(VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) | VIRTIO_BIT(VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO) | VIRTIO_BIT(VIRTIO_NET_F_OUT_NET_HEADER),

/P

#5 lore raw

Re: [PATCH v2 net-next 3/3] vhost/net: enable outer nw header offset support

In reply to Kommula Shiva Shankar
Quoted 12 lines · Kommula Shiva Shankar

apprise vhost net about the virtio net header size.

---
 drivers/vhost/net.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/vhost/net.c +0 -0
index 35ded4330431..8d055405746d 100644

drivers/vhost/net.c static const u64 vhost_net_features[VIRTIO_FEATURES_DWORDS] = { · line 84

Quoted 7 lines
drivers/vhost/net.c +1 -0
@@ -78,6 +78,7 @@ static const u64 vhost_net_features[VIRTIO_FEATURES_DWORDS] = {
 	(1ULL << VIRTIO_F_IN_ORDER),
 	VIRTIO_BIT(VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) |
 	VIRTIO_BIT(VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO),
+	VIRTIO_BIT(VIRTIO_NET_F_OUT_NET_HEADER),
 };
 
 enum {

How does this even work?
VIRTIO_FEATURES_DWORDS is 2, you can't really
add more elements.

Pls include info on testing done in your next
submission.

Quoted 12 lines
drivers/vhost/net.c +3 -0
@@ -1655,6 +1656,9 @@ static int vhost_net_set_features(struct vhost_net *n, const u64 *features)
 		  sizeof(struct virtio_net_hdr_mrg_rxbuf) :
 		  sizeof(struct virtio_net_hdr);
 
+	if (virtio_features_test_bit(features,
+				     VIRTIO_NET_F_OUT_NET_HEADER))
+		hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel_out_net_hdr);
 	if (virtio_features_test_bit(features,
 				     VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO) ||
 	    virtio_features_test_bit(features,
Signature · 1 line
2.48.1