tun: remove unnecessary tun_xdp_hdr structure
Subsystems (6)
PATCH v2 1/2 tun: remove unnecessary tun_xdp_hdr structure
With f95f0f95cfb7("net, xdp: Introduce xdp_init_buff utility routine"), buffer length could be stored as frame size so there's no need to have a dedicated tun_xdp_hdr structure. We can simply store virtio net header instead.
--- drivers/net/tap.c | 5 ++--- drivers/net/tun.c | 5 ++--- drivers/vhost/net.c | 8 ++------ include/linux/if_tun.h | 5 ----- 4 files changed, 6 insertions(+), 17 deletions(-)
drivers/net/tap.c +2 -3
@@ -1044,9 +1044,8 @@ static const struct file_operations tap_fops = {static int tap_get_user_xdp (struct tap_queue *q,struct xdp_buff *xdp) { -struct tun_xdp_hdr *hdr = xdp->data_hard_start; -struct virtio_net_hdr *gso = &hdr->gso; -int buflen = hdr->buflen; +struct virtio_net_hdr *gso = xdp->data_hard_start; +int buflen = xdp->frame_sz;int vnet_hdr_len =0 ;struct tap_dev *tap;struct sk_buff *skb;
drivers/net/tun.c +2 -3
@@ -2356,13 +2356,12 @@ static int tun_xdp_one(struct tun_struct *tun, struct tun_page *tpage) {unsigned int datasize = xdp->data_end - xdp->data; -struct tun_xdp_hdr *hdr = xdp->data_hard_start; -struct virtio_net_hdr *gso = &hdr->gso; +struct virtio_net_hdr *gso = xdp->data_hard_start;struct bpf_prog *xdp_prog;struct sk_buff *skb =NULL ;struct sk_buff_head *queue;u32 rxhash =0 , act; -int buflen = hdr->buflen; +int buflen = xdp->frame_sz;int metasize =0 ;int ret =0 ;bool skb_xdp = false;
drivers/vhost/net.c +2 -6
@@ -668,7 +668,6 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,struct socket *sock =vhost_vq_get_backend (vq);struct virtio_net_hdr *gso;struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp]; -struct tun_xdp_hdr *hdr;size_t len =iov_iter_count (from);int headroom =vhost_sock_xdp (sock) ?XDP_PACKET_HEADROOM :0 ;int buflen =SKB_DATA_ALIGN (sizeof (struct skb_shared_info ));
@@ -691,15 +690,13 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,if (unlikely (!buf))return -ENOMEM ; - copied =copy_from_iter (buf + offsetof(struct tun_xdp_hdr , gso), - sock_hlen, from); + copied =copy_from_iter (buf, sock_hlen, from);if (copied != sock_hlen) { ret = -EFAULT ; goto err; } - hdr = buf; - gso = &hdr->gso; + gso = buf;if (!sock_hlen)memset (buf,0 , pad);
@@ -727,7 +724,6 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,xdp_init_buff (xdp, buflen,NULL );xdp_prepare_buff (xdp, buf, pad, len, true); - hdr->buflen = buflen; ++nvq->batched_xdp;
include/linux/if_tun.h +0 -5
@@ -19,11 +19,6 @@ struct tun_msg_ctl {void *ptr; }; -struct tun_xdp_hdr { -int buflen; -struct virtio_net_hdr gso; -}; -#if defined(CONFIG_TUN ) || defined(CONFIG_TUN_MODULE )struct socket *tun_get_socket (struct file *);struct ptr_ring *tun_get_tx_ring (struct file *file);
Signature · 1 line
2.34.1
PATCH v2 2/2 vhost-net: reduce one userspace copy when building XDP buff
We used to do twice copy_from_iter() to copy virtio-net and packet separately. This introduce overheads for userspace access hardening as well as SMAP (for x86 it's stac/clac). So this patch tries to use one copy_from_iter() to copy them once and move the virtio-net header afterwards to reduce overheads.
Testpmd + vhost_net shows 10% improvement from 5.45Mpps to 6.0Mpps.
---
Changes since V1:
- Add a comment to explain no overlapping when using memcpy
--- drivers/vhost/net.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/vhost/net.c +5 -9
@@ -690,13 +690,13 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,if (unlikely (!buf))return -ENOMEM ; - copied =copy_from_iter (buf, sock_hlen, from); -if (copied != sock_hlen) { + copied =copy_from_iter (buf + pad - sock_hlen, len, from); +if (copied != len) { ret = -EFAULT ; goto err; } - gso = buf; + gso = buf + pad - sock_hlen;if (!sock_hlen)memset (buf,0 , pad);
@@ -715,12 +715,8 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, } } - len -= sock_hlen; - copied =copy_from_iter (buf + pad, len, from); -if (copied != len) { - ret = -EFAULT ; - goto err; - } +/* pad contains sock_hlen */ +memcpy (buf, buf + pad - sock_hlen, sock_hlen);xdp_init_buff (xdp, buflen,NULL );xdp_prepare_buff (xdp, buf, pad, len, true);
Signature · 1 line
2.34.1
Jakub Kicinski
maintainerRe: [PATCH V2 net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff
Quoted 20 lines · On Thu, 26 Jun 2025 10:14:45 +0800 Jason Wang wrote:
drivers/vhost/net.c +3
-3@@ -690,13 +690,13 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,if (unlikely (!buf))return -ENOMEM ; - copied =copy_from_iter (buf, sock_hlen, from); -if (copied != sock_hlen) { + copied =copy_from_iter (buf + pad - sock_hlen, len, from); +if (copied != len) { ret = -EFAULT ; goto err; } - gso = buf; + gso = buf + pad - sock_hlen;if (!sock_hlen)memset (buf,0 , pad);
drivers/vhost/net.c static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, · line 718
Quoted 4 lines
drivers/vhost/net.c +0
-1@@ -715,12 +715,8 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, } } - len -= sock_hlen;
we used to adjust @len here, now we don't..
drivers/vhost/net.c static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, · line 722
Quoted 10 lines
drivers/vhost/net.c +2
-5- copied =copy_from_iter (buf + pad, len, from); -if (copied != len) { - ret = -EFAULT ; - goto err; - } +/* pad contains sock_hlen */ +memcpy (buf, buf + pad - sock_hlen, sock_hlen);xdp_init_buff (xdp, buflen,NULL );xdp_prepare_buff (xdp, buf, pad, len, true);
.. yet we still use len as the packet size here.
Signature · 1 line
pw-bot: cr
Re: [PATCH V2 net-next 2/2] vhost-net: reduce one userspace copy when building XDP buff
Quoted 37 lines · Jakub Kicinski
Quoted 20 lines · On Thu, 26 Jun 2025 10:14:45 +0800 Jason Wang wrote:
drivers/vhost/net.c +3
-3@@ -690,13 +690,13 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,if (unlikely (!buf))return -ENOMEM ; - copied =copy_from_iter (buf, sock_hlen, from); -if (copied != sock_hlen) { + copied =copy_from_iter (buf + pad - sock_hlen, len, from); +if (copied != len) { ret = -EFAULT ; goto err; } - gso = buf; + gso = buf + pad - sock_hlen;if (!sock_hlen)memset (buf,0 , pad);drivers/vhost/net.c static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, · line 718
Quoted 4 lines
drivers/vhost/net.c +0
-1@@ -715,12 +715,8 @@ static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, } } - len -= sock_hlen;we used to adjust @len here, now we don't..
Quoted 8 lines
drivers/vhost/net.c +2
-5- copied =copy_from_iter (buf + pad, len, from); -if (copied != len) { - ret = -EFAULT ; - goto err; - } +/* pad contains sock_hlen */ +memcpy (buf, buf + pad - sock_hlen, sock_hlen);
Quoted 4 lines
drivers/vhost/net.c static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq, · line 722
Quoted 2 lines
drivers/vhost/net.c +0
-0xdp_init_buff (xdp, buflen,NULL );xdp_prepare_buff (xdp, buf, pad, len, true);.. yet we still use len as the packet size here.
Exactly, it should be len - sock_hlen here.
Thanks
Quoted 3 lines
--
pw-bot: cr