From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:11
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Please review.
Thanks.
Xuan Zhuo (33):
virtio_ring: virtqueue_add() support premapped
virtio_ring: split: virtqueue_add_split() support premapped
virtio_ring: packed: virtqueue_add_packed() support premapped
virtio_ring: introduce virtqueue_add_outbuf_premapped()
virtio_ring: introduce virtqueue_add_inbuf_premapped()
virtio_ring: introduce virtqueue_reset()
virtio_ring: add api virtio_dma_map() for advance dma
virtio_ring: introduce dma sync api for virtio
xsk: xsk_buff_pool add callback for dma_sync
xsk: support virtio DMA map
virtio_net: rename free_old_xmit_skbs to free_old_xmit
virtio_net: unify the code for recycling the xmit ptr
virtio_net: virtnet_poll_tx support rescheduled
virtio_net: independent directory
virtio_net: move to virtio_net.h
virtio_net: introduce virtnet_xdp_handler() to seprate the logic of
run xdp
virtio_net: receive_small() use virtnet_xdp_handler()
virtio_net: receive_merageable() use virtnet_xdp_handler()
virtio_net: introduce virtnet_tx_reset()
virtio_net: xsk: introduce virtnet_rq_bind_xsk_pool()
virtio_net: xsk: introduce virtnet_xsk_pool_enable()
virtio_net: xsk: introduce xsk disable
virtio_net: xsk: support xsk setup
virtio_net: xsk: stop disable tx napi
virtio_net: xsk: __free_old_xmit distinguishes xsk buffer
virtio_net: virtnet_sq_free_unused_buf() check xsk buffer
virtio_net: virtnet_rq_free_unused_buf() check xsk buffer
net: introduce napi_tx_raise()
virtio_net: xsk: tx: support tx
virtio_net: xsk: tx: support wakeup
virtio_net: xsk: tx: auto wakeup when free old xmit
virtio_net: xsk: rx: introduce add_recvbuf_xsk()
virtio_net: xsk: rx: introduce receive_xsk() to recv xsk buffer
MAINTAINERS | 2 +-
drivers/net/Kconfig | 8 +-
drivers/net/Makefile | 2 +-
drivers/net/virtio/Kconfig | 11 +
drivers/net/virtio/Makefile | 8 +
drivers/net/{virtio_net.c => virtio/main.c} | 564 +++++++-------------
drivers/net/virtio/virtio_net.h | 317 +++++++++++
drivers/net/virtio/xsk.c | 524 ++++++++++++++++++
drivers/net/virtio/xsk.h | 33 ++
drivers/virtio/virtio_ring.c | 376 +++++++++++--
include/linux/netdevice.h | 7 +
include/linux/virtio.h | 29 +
include/net/xsk_buff_pool.h | 6 +
net/core/dev.c | 11 +
net/xdp/xsk_buff_pool.c | 79 ++-
15 files changed, 1541 insertions(+), 436 deletions(-)
create mode 100644 drivers/net/virtio/Kconfig
create mode 100644 drivers/net/virtio/Makefile
rename drivers/net/{virtio_net.c => virtio/main.c} (92%)
create mode 100644 drivers/net/virtio/virtio_net.h
create mode 100644 drivers/net/virtio/xsk.c
create mode 100644 drivers/net/virtio/xsk.h
--
2.32.0.3.g01195cf9f
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:14
virtqueue_add_split() only supports virtual addresses, dma is completed
in virtqueue_add_split().
In some scenarios (such as the AF_XDP scenario), the memory is allocated
and DMA is completed in advance, so it is necessary for us to support
passing the DMA address to virtqueue_add_split().
Record this information in desc_state, we can skip unmap based on this
when executing dma unmap.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 55 +++++++++++++++++++++++++++---------
1 file changed, 42 insertions(+), 13 deletions(-)
@@ -70,6 +70,7 @@structvring_desc_state_split{void*data;/* Data for callback. */structvring_desc*indir_desc;/* Indirect descriptor, if any. */+boolpremapped;};structvring_desc_state_packed{
@@ -521,6 +525,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,unsignedintin_sgs,void*data,void*ctx,+boolpremapped,gfp_tgfp){structvring_virtqueue*vq=to_vvq(_vq);
@@ -582,9 +587,16 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,for(n=0;n<out_sgs;n++){for(sg=sgs[n];sg;sg=sg_next(sg)){-dma_addr_taddr=vring_map_one_sg(vq,sg,DMA_TO_DEVICE);-if(vring_mapping_error(vq,addr))-gotounmap_release;+dma_addr_taddr;++if(premapped){+addr=sg_dma_address(sg);++}else{+addr=vring_map_one_sg(vq,sg,DMA_TO_DEVICE);+if(vring_mapping_error(vq,addr))+gotounmap_release;+}prev=i;/* Note that we trust indirect descriptor
@@ -597,9 +609,16 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,}for(;n<(out_sgs+in_sgs);n++){for(sg=sgs[n];sg;sg=sg_next(sg)){-dma_addr_taddr=vring_map_one_sg(vq,sg,DMA_FROM_DEVICE);-if(vring_mapping_error(vq,addr))-gotounmap_release;+dma_addr_taddr;++if(premapped){+addr=sg_dma_address(sg);++}else{+addr=vring_map_one_sg(vq,sg,DMA_FROM_DEVICE);+if(vring_mapping_error(vq,addr))+gotounmap_release;+}prev=i;/* Note that we trust indirect descriptor
@@ -644,6 +663,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,/* Store token and indirect buffer state. */vq->split.desc_state[head].data=data;+vq->split.desc_state[head].premapped=premapped;if(indirect)vq->split.desc_state[head].indir_desc=desc;else
@@ -673,6 +693,9 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,return0;unmap_release:+if(premapped)+gotounmap_free;+err_idx=i;if(indirect)
@@ -687,9 +710,10 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,vring_unmap_one_split_indirect(vq,&desc[i]);i=virtio16_to_cpu(_vq->vdev,desc[i].next);}else-i=vring_unmap_one_split(vq,i);+i=vring_unmap_one_split(vq,i,false);}+unmap_free:if(indirect)kfree(desc);
@@ -733,20 +757,23 @@ static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head,{unsignedinti,j;__virtio16nextflag=cpu_to_virtio16(vq->vq.vdev,VRING_DESC_F_NEXT);+boolpremapped;/* Clear data ptr. */vq->split.desc_state[head].data=NULL;+premapped=vq->split.desc_state[head].premapped;+/* Put back on free list: unmap first-level descriptors and find end */i=head;while(vq->split.vring.desc[i].flags&nextflag){-vring_unmap_one_split(vq,i);+vring_unmap_one_split(vq,i,premapped);i=vq->split.desc_extra[i].next;vq->vq.num_free++;}-vring_unmap_one_split(vq,i);+vring_unmap_one_split(vq,i,premapped);vq->split.desc_extra[i].next=vq->free_head;vq->free_head=head;
@@ -2735,6 +2735,56 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num,}EXPORT_SYMBOL_GPL(virtqueue_resize);+/**+*virtqueue_reset-resetthevringofvq+*@_vq:thestructvirtqueuewe'retalkingabout.+*@recycle:callbackforrecycletheuselessbuffer+*+*Callermustensurewedon'tcallthiswithothervirtqueueoperations+*atthesametime(exceptwherenoted).+*+*Returnszerooranegativeerror.+*0:success.+*-EBUSY:Failedtosyncwithdevice,vqmaynotworkproperly+*-ENOENT:Transportordevicenotsupported+*-EPERM:Operationnotpermitted+*/+intvirtqueue_reset(structvirtqueue*_vq,+void(*recycle)(structvirtqueue*vq,void*buf))+{+structvring_virtqueue*vq=to_vvq(_vq);+structvirtio_device*vdev=vq->vq.vdev;+void*buf;+interr;++if(!vq->we_own_ring)+return-EPERM;++if(!vdev->config->disable_vq_and_reset)+return-ENOENT;++if(!vdev->config->enable_vq_after_reset)+return-ENOENT;++err=vdev->config->disable_vq_and_reset(_vq);+if(err)+returnerr;++while((buf=virtqueue_detach_unused_buf(_vq))!=NULL)+recycle(_vq,buf);++if(vq->packed_ring)+virtqueue_reinit_packed(vq);+else+virtqueue_reinit_split(vq);++if(vdev->config->enable_vq_after_reset(_vq))+return-EBUSY;++return0;+}+EXPORT_SYMBOL_GPL(virtqueue_reset);+/* Only available for split ring */structvirtqueue*vring_new_virtqueue(unsignedintindex,unsignedintnum,
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:34
virtqueue_add_packed() only supports virtual addresses, dma is completed
in virtqueue_add_packed().
In some scenarios (such as the AF_XDP scenario), the memory is allocated
and DMA is completed in advance, so it is necessary for us to support
passing the DMA address to virtqueue_add_packed().
Record this information in desc_state, we can skip unmap based on this
when executing dma unmap.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 71 +++++++++++++++++++++++++-----------
1 file changed, 50 insertions(+), 21 deletions(-)
@@ -78,6 +78,7 @@ struct vring_desc_state_packed {structvring_packed_desc*indir_desc;/* Indirect descriptor, if any. */u16num;/* Descriptor list length. */u16last;/* The last desc state in a list. */+boolpremapped;};structvring_desc_extra{
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:35
In the process of dma sync, we involved whether virtio uses dma api. On
the other hand, it is also necessary to read vdev->dev.parent. So these
API has been introduced.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 61 ++++++++++++++++++++++++++++++++++++
include/linux/virtio.h | 8 +++++
2 files changed, 69 insertions(+)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:39
Since free_old_xmit_skbs not only deals with skb, but also xdp frame and
subsequent added xsk, so change the name of this function to
free_old_xmit.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio_net.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:41
virtnet_poll_tx() support to return budget when busy to be rescheduled.
When retval < budget, napi_poll() in dev.c will exit directly. And
virtqueue_napi_complete() will be called to close napi.
When retval == budget, the napi_poll() in dev.c will re-add napi to the
queue.
The purpose of this patch is to support xsk xmit in virtio_poll_tx() for
subsequent patch.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio_net.c | 6 ++++++
1 file changed, 6 insertions(+)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:43
Added virtio_dma_map() to map DMA addresses for virtual memory in
advance. The purpose of adding this function is to check
vring_use_dma_api() for virtio dma operation and get vdev->dev.parent as
the parameter of dma_map_page().
Added virtio_dma_unmap() for unmap DMA address.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 80 ++++++++++++++++++++++++++++++++++++
include/linux/virtio.h | 9 ++++
2 files changed, 89 insertions(+)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:46
Create a separate directory for virtio-net. AF_XDP support will be added
later, then a separate xsk.c file will be added, so we should create a
directory for virtio-net.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
MAINTAINERS | 2 +-
drivers/net/Kconfig | 8 +-------
drivers/net/Makefile | 2 +-
drivers/net/virtio/Kconfig | 11 +++++++++++
drivers/net/virtio/Makefile | 8 ++++++++
drivers/net/{virtio_net.c => virtio/main.c} | 0
6 files changed, 22 insertions(+), 9 deletions(-)
create mode 100644 drivers/net/virtio/Kconfig
create mode 100644 drivers/net/virtio/Makefile
rename drivers/net/{virtio_net.c => virtio/main.c} (100%)
@@ -0,0 +1,8 @@+# SPDX-License-Identifier: GPL-2.0+#+# Makefile for the virtio network device drivers.+#++obj-$(CONFIG_VIRTIO_NET)+=virtio_net.o++virtio_net-y:=main.o
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio/main.csimilarity index 100%rename from drivers/net/virtio_net.crename to drivers/net/virtio/main.c
--
2.32.0.3.g01195cf9f
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:47
There are two completely similar and independent implementations. This
is inconvenient for the subsequent addition of new types. So extract a
function from this piece of code and call this function uniformly to
recover old xmit ptr.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio_net.c | 76 +++++++++++++++++-----------------------
1 file changed, 33 insertions(+), 43 deletions(-)
@@ -318,6 +318,30 @@ static struct xdp_frame *ptr_to_xdp(void *ptr)return(structxdp_frame*)((unsignedlong)ptr&~VIRTIO_XDP_FLAG);}+staticvoid__free_old_xmit(structsend_queue*sq,boolin_napi,+structvirtnet_sq_stats*stats)+{+unsignedintlen;+void*ptr;++while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){+if(!is_xdp_frame(ptr)){+structsk_buff*skb=ptr;++pr_debug("Sent skb %p\n",skb);++stats->bytes+=skb->len;+napi_consume_skb(skb,in_napi);+}else{+structxdp_frame*frame=ptr_to_xdp(ptr);++stats->bytes+=xdp_get_frame_len(frame);+xdp_return_frame(frame);+}+stats->packets++;+}+}+/* Converting between virtqueue no. and kernel tx/rx queue no.*0:rx01:tx02:rx13:tx1...2N:rxN2N+1:txN2N+2:cvq*/
@@ -635,15 +659,12 @@ static int virtnet_xdp_xmit(struct net_device *dev,intn,structxdp_frame**frames,u32flags){structvirtnet_info*vi=netdev_priv(dev);+structvirtnet_sq_statsstats={};structreceive_queue*rq=vi->rq;structbpf_prog*xdp_prog;structsend_queue*sq;-unsignedintlen;-intpackets=0;-intbytes=0;intnxmit=0;intkicks=0;-void*ptr;intret;inti;
@@ -662,20 +683,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,}/* Free up any pending old buffers before queueing new ones. */-while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){-if(likely(is_xdp_frame(ptr))){-structxdp_frame*frame=ptr_to_xdp(ptr);--bytes+=xdp_get_frame_len(frame);-xdp_return_frame(frame);-}else{-structsk_buff*skb=ptr;--bytes+=skb->len;-napi_consume_skb(skb,false);-}-packets++;-}+__free_old_xmit(sq,false,&stats);for(i=0;i<n;i++){structxdp_frame*xdpf=frames[i];
@@ -692,8 +700,8 @@ static int virtnet_xdp_xmit(struct net_device *dev,}out:u64_stats_update_begin(&sq->stats.syncp);-sq->stats.bytes+=bytes;-sq->stats.packets+=packets;+sq->stats.bytes+=stats.bytes;+sq->stats.packets+=stats.packets;sq->stats.xdp_tx+=n;sq->stats.xdp_tx_drops+=n-nxmit;sq->stats.kicks+=kicks;
@@ -1716,37 +1724,19 @@ static int virtnet_receive(struct receive_queue *rq, int budget,staticvoidfree_old_xmit(structsend_queue*sq,boolin_napi){-unsignedintlen;-unsignedintpackets=0;-unsignedintbytes=0;-void*ptr;+structvirtnet_sq_statsstats={};-while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){-if(likely(!is_xdp_frame(ptr))){-structsk_buff*skb=ptr;--pr_debug("Sent skb %p\n",skb);--bytes+=skb->len;-napi_consume_skb(skb,in_napi);-}else{-structxdp_frame*frame=ptr_to_xdp(ptr);--bytes+=xdp_get_frame_len(frame);-xdp_return_frame(frame);-}-packets++;-}+__free_old_xmit(sq,in_napi,&stats);/* Avoid overhead when no packets have been processed*happenswhencalledspeculativelyfromstart_xmit.*/-if(!packets)+if(!stats.packets)return;u64_stats_update_begin(&sq->stats.syncp);-sq->stats.bytes+=bytes;-sq->stats.packets+=packets;+sq->stats.bytes+=stats.bytes;+sq->stats.packets+=stats.packets;u64_stats_update_end(&sq->stats.syncp);}
@@ -44,15 +28,6 @@ module_param(napi_tx, bool, 0644);#define VIRTIO_XDP_TX BIT(0)#define VIRTIO_XDP_REDIR BIT(1)-#define VIRTIO_XDP_FLAG BIT(0)--/* RX packet size EWMA. The average packet size is used to determine the packet-*buffersizewhenrefillingRXrings.AstheentireRXringmayberefilled-*atonce,theweightischosensothattheEWMAwillbeinsensitivetoshort--*term,transientchangesinpacketsize.-*/-DECLARE_EWMA(pkt_len,0,64)-#define VIRTNET_DRIVER_VERSION "1.0.0"staticconstunsignedlongguest_offloads[]={
@@ -125,57 +70,6 @@ static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {#define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)#define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)-/* Internal representation of a send virtqueue */-structsend_queue{-/* Virtqueue associated with this send _queue */-structvirtqueue*vq;--/* TX: fragments + linear part + virtio header */-structscatterlistsg[MAX_SKB_FRAGS+2];--/* Name of the send queue: output.$index */-charname[16];--structvirtnet_sq_statsstats;--structnapi_structnapi;--/* Record whether sq is in reset state. */-boolreset;-};--/* Internal representation of a receive virtqueue */-structreceive_queue{-/* Virtqueue associated with this receive_queue */-structvirtqueue*vq;--structnapi_structnapi;--structbpf_prog__rcu*xdp_prog;--structvirtnet_rq_statsstats;--/* Chain pages by the private ptr. */-structpage*pages;--/* Average packet length for mergeable receive buffers. */-structewma_pkt_lenmrg_avg_pkt_len;--/* Page frag for packet buffer allocation. */-structpage_fragalloc_frag;--/* RX: fragments + linear part + virtio header */-structscatterlistsg[MAX_SKB_FRAGS+2];--/* Min single buffer size for mergeable buffers case. */-unsignedintmin_buf_len;--/* Name of this receive queue: input.$index */-charname[16];--structxdp_rxq_infoxdp_rxq;-};-/* This structure can contain rss message with maximum settings for indirection table and keysize*Note,thatdefaultstructurethatdescribesRSSconfigurationvirtio_net_rss_config*containssameinfobutcan'thandletablevalues.
@@ -206,90 +100,6 @@ struct control_buf {structvirtio_net_ctrl_rssrss;};-structvirtnet_info{-structvirtio_device*vdev;-structvirtqueue*cvq;-structnet_device*dev;-structsend_queue*sq;-structreceive_queue*rq;-unsignedintstatus;--/* Max # of queue pairs supported by the device */-u16max_queue_pairs;--/* # of queue pairs currently used by the driver */-u16curr_queue_pairs;--/* # of XDP queue pairs currently used by the driver */-u16xdp_queue_pairs;--/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */-boolxdp_enabled;--/* I like... big packets and I cannot lie! */-boolbig_packets;--/* number of sg entries allocated for big packets */-unsignedintbig_packets_num_skbfrags;--/* Host will merge rx buffers for big packets (shake it! shake it!) */-boolmergeable_rx_bufs;--/* Host supports rss and/or hash report */-boolhas_rss;-boolhas_rss_hash_report;-u8rss_key_size;-u16rss_indir_table_size;-u32rss_hash_types_supported;-u32rss_hash_types_saved;--/* Has control virtqueue */-boolhas_cvq;--/* Host can handle any s/g split between our header and packet data */-boolany_header_sg;--/* Packet virtio header size */-u8hdr_len;--/* Work struct for delayed refilling if we run low on memory. */-structdelayed_workrefill;--/* Is delayed refill enabled? */-boolrefill_enabled;--/* The lock to synchronize the access to refill_enabled */-spinlock_trefill_lock;--/* Work struct for config space updates */-structwork_structconfig_work;--/* Does the affinity hint is set for virtqueues? */-boolaffinity_hint_set;--/* CPU hotplug instances for online & dead */-structhlist_nodenode;-structhlist_nodenode_dead;--structcontrol_buf*ctrl;--/* Ethtool settings */-u8duplex;-u32speed;--/* Interrupt coalescing settings */-u32tx_usecs;-u32rx_usecs;-u32tx_max_packets;-u32rx_max_packets;--unsignedlongguest_offloads;-unsignedlongguest_offloads_capable;--/* failover when STANDBY feature enabled */-structfailover*failover;-};-structpadded_vnet_hdr{structvirtio_net_hdr_v1_hashhdr;/*
@@ -303,45 +113,11 @@ struct padded_vnet_hdr {staticvoidvirtnet_rq_free_unused_buf(structvirtqueue*vq,void*buf);staticvoidvirtnet_sq_free_unused_buf(structvirtqueue*vq,void*buf);-staticboolis_xdp_frame(void*ptr)-{-return(unsignedlong)ptr&VIRTIO_XDP_FLAG;-}-staticvoid*xdp_to_ptr(structxdp_frame*ptr){return(void*)((unsignedlong)ptr|VIRTIO_XDP_FLAG);}-staticstructxdp_frame*ptr_to_xdp(void*ptr)-{-return(structxdp_frame*)((unsignedlong)ptr&~VIRTIO_XDP_FLAG);-}--staticvoid__free_old_xmit(structsend_queue*sq,boolin_napi,-structvirtnet_sq_stats*stats)-{-unsignedintlen;-void*ptr;--while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){-if(!is_xdp_frame(ptr)){-structsk_buff*skb=ptr;--pr_debug("Sent skb %p\n",skb);--stats->bytes+=skb->len;-napi_consume_skb(skb,in_napi);-}else{-structxdp_frame*frame=ptr_to_xdp(ptr);--stats->bytes+=xdp_get_frame_len(frame);-xdp_return_frame(frame);-}-stats->packets++;-}-}-/* Converting between virtqueue no. and kernel tx/rx queue no.*0:rx01:tx02:rx13:tx1...2N:rxN2N+1:txN2N+2:cvq*/
@@ -0,0 +1,265 @@+/* SPDX-License-Identifier: GPL-2.0-or-later */++#ifndef __VIRTIO_NET_H__+#define __VIRTIO_NET_H__+#include<linux/netdevice.h>+#include<linux/etherdevice.h>+#include<linux/ethtool.h>+#include<linux/module.h>+#include<linux/virtio.h>+#include<linux/virtio_net.h>+#include<linux/bpf.h>+#include<linux/bpf_trace.h>+#include<linux/scatterlist.h>+#include<linux/if_vlan.h>+#include<linux/slab.h>+#include<linux/cpu.h>+#include<linux/average.h>+#include<linux/filter.h>+#include<linux/kernel.h>+#include<net/route.h>+#include<net/xdp.h>+#include<net/net_failover.h>+#include<net/xdp_sock_drv.h>++#define VIRTIO_XDP_FLAG BIT(0)++structvirtnet_info{+structvirtio_device*vdev;+structvirtqueue*cvq;+structnet_device*dev;+structsend_queue*sq;+structreceive_queue*rq;+unsignedintstatus;++/* Max # of queue pairs supported by the device */+u16max_queue_pairs;++/* # of queue pairs currently used by the driver */+u16curr_queue_pairs;++/* # of XDP queue pairs currently used by the driver */+u16xdp_queue_pairs;++/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */+boolxdp_enabled;++/* I like... big packets and I cannot lie! */+boolbig_packets;++/* number of sg entries allocated for big packets */+unsignedintbig_packets_num_skbfrags;++/* Host will merge rx buffers for big packets (shake it! shake it!) */+boolmergeable_rx_bufs;++/* Host supports rss and/or hash report */+boolhas_rss;+boolhas_rss_hash_report;+u8rss_key_size;+u16rss_indir_table_size;+u32rss_hash_types_supported;+u32rss_hash_types_saved;++/* Has control virtqueue */+boolhas_cvq;++/* Host can handle any s/g split between our header and packet data */+boolany_header_sg;++/* Packet virtio header size */+u8hdr_len;++/* Work struct for delayed refilling if we run low on memory. */+structdelayed_workrefill;++/* Is delayed refill enabled? */+boolrefill_enabled;++/* The lock to synchronize the access to refill_enabled */+spinlock_trefill_lock;++/* Work struct for config space updates */+structwork_structconfig_work;++/* Does the affinity hint is set for virtqueues? */+boolaffinity_hint_set;++/* CPU hotplug instances for online & dead */+structhlist_nodenode;+structhlist_nodenode_dead;++structcontrol_buf*ctrl;++/* Ethtool settings */+u8duplex;+u32speed;++/* Interrupt coalescing settings */+u32tx_usecs;+u32rx_usecs;+u32tx_max_packets;+u32rx_max_packets;++unsignedlongguest_offloads;+unsignedlongguest_offloads_capable;++/* failover when STANDBY feature enabled */+structfailover*failover;+};++/* RX packet size EWMA. The average packet size is used to determine the packet+*buffersizewhenrefillingRXrings.AstheentireRXringmayberefilled+*atonce,theweightischosensothattheEWMAwillbeinsensitivetoshort-+*term,transientchangesinpacketsize.+*/+DECLARE_EWMA(pkt_len,0,64)++structvirtnet_stat_desc{+chardesc[ETH_GSTRING_LEN];+size_toffset;+};++structvirtnet_sq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64xdp_tx;+u64xdp_tx_drops;+u64kicks;+u64tx_timeouts;+};++structvirtnet_rq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64drops;+u64xdp_packets;+u64xdp_tx;+u64xdp_redirects;+u64xdp_drops;+u64kicks;+};++#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)+#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)++/* Internal representation of a send virtqueue */+structsend_queue{+/* Virtqueue associated with this send _queue */+structvirtqueue*vq;++/* TX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Name of the send queue: output.$index */+charname[16];++structvirtnet_sq_statsstats;++structnapi_structnapi;++/* Record whether sq is in reset state. */+boolreset;+};++/* Internal representation of a receive virtqueue */+structreceive_queue{+/* Virtqueue associated with this receive_queue */+structvirtqueue*vq;++structnapi_structnapi;++structbpf_prog__rcu*xdp_prog;++structvirtnet_rq_statsstats;++/* Chain pages by the private ptr. */+structpage*pages;++/* Average packet length for mergeable receive buffers. */+structewma_pkt_lenmrg_avg_pkt_len;++/* Page frag for packet buffer allocation. */+structpage_fragalloc_frag;++/* RX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Min single buffer size for mergeable buffers case. */+unsignedintmin_buf_len;++/* Name of this receive queue: input.$index */+charname[16];++structxdp_rxq_infoxdp_rxq;+};++staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)+{+if(q<(vi->curr_queue_pairs-vi->xdp_queue_pairs))+returnfalse;+elseif(q<vi->curr_queue_pairs)+returntrue;+else+returnfalse;+}++staticinlinevoidvirtnet_return_xdp_frame(structsend_queue*sq,+structxdp_frame*frame)+{+structvirtnet_info*vi=sq->vq->vdev->priv;+dma_addr_t*p_addr,addr;++p_addr=frame->data-sizeof(*p_addr);+addr=*p_addr;++virtio_dma_unmap(&vi->vdev->dev,addr,frame->len,DMA_TO_DEVICE);++xdp_return_frame(frame);+}++staticinlinevoidvirtqueue_napi_schedule(structnapi_struct*napi,+structvirtqueue*vq)+{+if(napi_schedule_prep(napi)){+virtqueue_disable_cb(vq);+__napi_schedule(napi);+}+}++staticinlineboolis_xdp_frame(void*ptr)+{+return(unsignedlong)ptr&VIRTIO_XDP_FLAG;+}++staticstructxdp_frame*ptr_to_xdp(void*ptr)+{+return(structxdp_frame*)((unsignedlong)ptr&~VIRTIO_XDP_FLAG);+}++staticvoid__free_old_xmit(structsend_queue*sq,boolin_napi,+structvirtnet_sq_stats*stats)+{+unsignedintlen;+void*ptr;++while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){+if(!is_xdp_frame(ptr)){+structsk_buff*skb=ptr;++pr_debug("Sent skb %p\n",skb);++stats->bytes+=skb->len;+napi_consume_skb(skb,in_napi);+}else{+structxdp_frame*frame=ptr_to_xdp(ptr);++stats->bytes+=xdp_get_frame_len(frame);+xdp_return_frame(frame);+}+stats->packets++;+}+}+#endif
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:50
At present, we have two long similar logic to perform XDP Progs. And in
the implementation of XSK, we will have this need.
Therefore, this PATCH separates the code of executing XDP, which is
conducive to later maintenance and facilitates subsequent XSK for reuse.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 53 +++++++++++++++++++++++++++++++++
drivers/net/virtio/virtio_net.h | 11 +++++++
2 files changed, 64 insertions(+)
@@ -673,46 +671,22 @@ static struct sk_buff *receive_small(struct net_device *dev,xdp_prepare_buff(&xdp,buf+VIRTNET_RX_PAD+vi->hdr_len,xdp_headroom,len,true);orig_data=xdp.data;-act=bpf_prog_run_xdp(xdp_prog,&xdp);-stats->xdp_packets++;++act=virtnet_xdp_handler(xdp_prog,&xdp,dev,xdp_xmit,stats);switch(act){-caseXDP_PASS:+caseVIRTNET_XDP_RES_PASS:/* Recalculate length in case bpf program changed it */delta=orig_data-xdp.data;len=xdp.data_end-xdp.data;metasize=xdp.data-xdp.data_meta;break;-caseXDP_TX:-stats->xdp_tx++;-xdpf=xdp_convert_buff_to_frame(&xdp);-if(unlikely(!xdpf))-gotoerr_xdp;-err=virtnet_xdp_xmit(dev,1,&xdpf,0);-if(unlikely(!err)){-xdp_return_frame_rx_napi(xdpf);-}elseif(unlikely(err<0)){-trace_xdp_exception(vi->dev,xdp_prog,act);-gotoerr_xdp;-}-*xdp_xmit|=VIRTIO_XDP_TX;-rcu_read_unlock();-gotoxdp_xmit;-caseXDP_REDIRECT:-stats->xdp_redirects++;-err=xdp_do_redirect(dev,&xdp,xdp_prog);-if(err)-gotoerr_xdp;-*xdp_xmit|=VIRTIO_XDP_REDIR;++caseVIRTNET_XDP_RES_CONSUMED:rcu_read_unlock();gotoxdp_xmit;-default:-bpf_warn_invalid_xdp_action(vi->dev,xdp_prog,act);-fallthrough;-caseXDP_ABORTED:-trace_xdp_exception(vi->dev,xdp_prog,act);-gotoerr_xdp;-caseXDP_DROP:++caseVIRTNET_XDP_RES_DROP:gotoerr_xdp;}}
@@ -809,7 +818,7 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,unsignedintxdp_frags_truesz=0;structpage*page;skb_frag_t*frag;-intoffset;+intoffset,i;void*ctx;xdp_init_buff(xdp,frame_sz,&rq->xdp_rxq);
@@ -842,7 +851,7 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,dev->name,*num_buf,virtio16_to_cpu(vi->vdev,hdr->num_buffers));dev->stats.rx_length_errors++;-return-EINVAL;+gotoerr;}stats->bytes+=len;
@@ -861,7 +870,7 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,pr_debug("%s: rx error: len %u exceeds truesize %lu\n",dev->name,len,(unsignedlong)(truesize-room));dev->stats.rx_length_errors++;-return-EINVAL;+gotoerr;}frag=&shinfo->frags[shinfo->nr_frags++];
@@ -876,6 +885,14 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,*xdp_frags_truesize=xdp_frags_truesz;return0;++err:+for(i=0;i<shinfo->nr_frags;i++){+page=skb_frag_page(&shinfo->frags[i]);+put_page(page);+}++return-EINVAL;}staticstructsk_buff*receive_mergeable(structnet_device*dev,
@@ -919,13 +936,10 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,xdp_prog=rcu_dereference(rq->xdp_prog);if(xdp_prog){unsignedintxdp_frags_truesz=0;-structskb_shared_info*shinfo;-structxdp_frame*xdpf;structpage*xdp_page;structxdp_buffxdp;void*data;u32act;-inti;/* Transient failure which in theory could occur if*in-flightpacketsfrombeforeXDPwasenabledreach
@@ -983,69 +997,33 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,err=virtnet_build_xdp_buff_mrg(dev,vi,rq,&xdp,data,len,frame_sz,&num_buf,&xdp_frags_truesz,stats);if(unlikely(err))-gotoerr_xdp_frags;+gotoerr_xdp;-act=bpf_prog_run_xdp(xdp_prog,&xdp);-stats->xdp_packets++;+act=virtnet_xdp_handler(xdp_prog,&xdp,dev,xdp_xmit,stats);switch(act){-caseXDP_PASS:+caseVIRTNET_XDP_RES_PASS:if(unlikely(xdp_page!=page))put_page(page);+head_skb=build_skb_from_xdp_buff(dev,vi,&xdp,xdp_frags_truesz);rcu_read_unlock();returnhead_skb;-caseXDP_TX:-stats->xdp_tx++;-xdpf=xdp_convert_buff_to_frame(&xdp);-if(unlikely(!xdpf)){-netdev_dbg(dev,"convert buff to frame failed for xdp\n");-gotoerr_xdp_frags;-}-err=virtnet_xdp_xmit(dev,1,&xdpf,0);-if(unlikely(!err)){-xdp_return_frame_rx_napi(xdpf);-}elseif(unlikely(err<0)){-trace_xdp_exception(vi->dev,xdp_prog,act);-gotoerr_xdp_frags;-}-*xdp_xmit|=VIRTIO_XDP_TX;-if(unlikely(xdp_page!=page))-put_page(page);-rcu_read_unlock();-gotoxdp_xmit;-caseXDP_REDIRECT:-stats->xdp_redirects++;-err=xdp_do_redirect(dev,&xdp,xdp_prog);-if(err)-gotoerr_xdp_frags;-*xdp_xmit|=VIRTIO_XDP_REDIR;++caseVIRTNET_XDP_RES_CONSUMED:if(unlikely(xdp_page!=page))put_page(page);+rcu_read_unlock();gotoxdp_xmit;-default:-bpf_warn_invalid_xdp_action(vi->dev,xdp_prog,act);-fallthrough;-caseXDP_ABORTED:-trace_xdp_exception(vi->dev,xdp_prog,act);-fallthrough;-caseXDP_DROP:-gotoerr_xdp_frags;-}-err_xdp_frags:-if(unlikely(xdp_page!=page))-__free_pages(xdp_page,0);-if(xdp_buff_has_frags(&xdp)){-shinfo=xdp_get_shared_info_from_buff(&xdp);-for(i=0;i<shinfo->nr_frags;i++){-xdp_page=skb_frag_page(&shinfo->frags[i]);+caseVIRTNET_XDP_RES_DROP:+if(unlikely(xdp_page!=page))put_page(xdp_page);-}-}-gotoerr_xdp;+rcu_read_unlock();+gotoerr_xdp;+}}rcu_read_unlock();
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:58
This function is used to bind or unbind xsk pool to virtnet rq.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/Makefile | 2 +-
drivers/net/virtio/main.c | 8 ++---
drivers/net/virtio/virtio_net.h | 16 ++++++++++
drivers/net/virtio/xsk.c | 56 +++++++++++++++++++++++++++++++++
4 files changed, 76 insertions(+), 6 deletions(-)
create mode 100644 drivers/net/virtio/xsk.c
@@ -168,6 +168,12 @@ struct send_queue {/* Record whether sq is in reset state. */boolreset;++struct{+structxsk_buff_pool__rcu*pool;++dma_addr_thdr_dma_address;+}xsk;};/* Internal representation of a receive virtqueue */
@@ -200,6 +206,13 @@ struct receive_queue {charname[16];structxdp_rxq_infoxdp_rxq;++struct{+structxsk_buff_pool__rcu*pool;++/* xdp rxq used by xsk */+structxdp_rxq_infoxdp_rxq;+}xsk;};staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:01:59
Introduce virtnet_tx_reset() to release the buffers inside virtio ring.
This is needed for xsk disable. When disable xsk, we need to relese the
buffer from xsk, so this function is needed.
This patch reuse the virtnet_tx_resize.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 21 ++++++++++++++++++---
drivers/net/virtio/virtio_net.h | 1 +
2 files changed, 19 insertions(+), 3 deletions(-)
@@ -54,3 +56,60 @@ static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queureturnerr;}++staticintvirtnet_xsk_pool_enable(structnet_device*dev,+structxsk_buff_pool*pool,+u16qid)+{+structvirtnet_info*vi=netdev_priv(dev);+structreceive_queue*rq;+structsend_queue*sq;+interr;++if(qid>=vi->curr_queue_pairs)+return-EINVAL;++sq=&vi->sq[qid];+rq=&vi->rq[qid];++/* xsk zerocopy depend on the tx napi.+*+*Allxskpacketsareactuallyconsumedandsentoutfromthexsktx+*queueunderthetxnapimechanism.+*/+if(!sq->napi.weight)+return-EPERM;++/* In big_packets mode, xdp cannot work, so there is no need to+*initializexskofrq.+*/+if(vi->big_packets&&!vi->mergeable_rx_bufs)+return-ENOENT;++sq->xsk.hdr_dma_address=virtio_dma_map(&vi->vdev->dev,&xsk_hdr,+vi->hdr_len,DMA_TO_DEVICE);+if(virtio_dma_mapping_error(&vi->vdev->dev,sq->xsk.hdr_dma_address))+return-ENOMEM;++err=xsk_pool_dma_map(pool,&vi->vdev->dev,0);+if(err)+gotoerr_xsk_map;++err=virtnet_rq_bind_xsk_pool(vi,rq,pool,dev);+if(err)+gotoerr_rxq;++/* Here is already protected by rtnl_lock, so rcu_assign_pointer+*issafe.+*/+rcu_assign_pointer(sq->xsk.pool,pool);++return0;++err_rxq:+xsk_pool_dma_unmap(pool,0);+err_xsk_map:+virtio_dma_unmap(&vi->vdev->dev,sq->xsk.hdr_dma_address,vi->hdr_len,+DMA_TO_DEVICE);+returnerr;+}
@@ -113,3 +113,32 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,DMA_TO_DEVICE);returnerr;}++staticintvirtnet_xsk_pool_disable(structnet_device*dev,u16qid)+{+structvirtnet_info*vi=netdev_priv(dev);+structreceive_queue*rq;+structsend_queue*sq;+interr1,err2;++if(qid>=vi->curr_queue_pairs)+return-EINVAL;++sq=&vi->sq[qid];+rq=&vi->rq[qid];++virtio_dma_unmap(&vi->vdev->dev,sq->xsk.hdr_dma_address,vi->hdr_len,+DMA_TO_DEVICE);++xsk_pool_dma_unmap(sq->xsk.pool,0);++rcu_assign_pointer(sq->xsk.pool,NULL);++/* Sync with the XSK wakeup and with NAPI. */+synchronize_net();++err1=virtnet_tx_reset(vi,sq);+err2=virtnet_rq_bind_xsk_pool(vi,rq,NULL,NULL);++returnerr1|err2;+}
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:06
__free_old_xmit distinguishes three type ptr(skb, xdp frame, xsk buffer)
by the last two types bits.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/virtio_net.h | 18 ++++++++++++++++--
drivers/net/virtio/xsk.h | 16 ++++++++++++++++
2 files changed, 32 insertions(+), 2 deletions(-)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:07
Since xsk's TX queue is consumed by TX NAPI, if sq is bound to xsk, then
we must stop tx napi from being disabled.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
@@ -2728,8 +2728,15 @@ static int virtnet_set_coalesce(struct net_device *dev,returnret;if(update_napi){-for(i=0;i<vi->max_queue_pairs;i++)+for(i=0;i<vi->max_queue_pairs;i++){+/* xsk xmit depend on the tx napi. So if xsk is active,+*preventmodificationstotxnapi.+*/+if(rtnl_dereference(vi->sq[i].xsk.pool))+continue;+vi->sq[i].napi.weight=napi_weight;+}}returnret;
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:08
The driver's tx napi is very important for XSK. It is responsible for
obtaining data from the XSK queue and sending it out.
At the beginning, we need to trigger tx napi.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 12 +++-
drivers/net/virtio/xsk.c | 146 ++++++++++++++++++++++++++++++++++++++
drivers/net/virtio/xsk.h | 2 +
3 files changed, 159 insertions(+), 1 deletion(-)
@@ -7,6 +7,152 @@staticstructvirtio_net_hdr_mrg_rxbufxsk_hdr;+staticvoidsg_fill_dma(structscatterlist*sg,dma_addr_taddr,u32len)+{+sg->dma_address=addr;+sg->length=len;+}++staticvoidvirtnet_xsk_check_queue(structsend_queue*sq)+{+structvirtnet_info*vi=sq->vq->vdev->priv;+structnet_device*dev=vi->dev;+intqnum=sq-vi->sq;++/* If it is a raw buffer queue, it does not check whether the status+*ofthequeueisstoppedwhensending.Sothereisnoneedtocheck+*thesituationoftherawbufferqueue.+*/+if(is_xdp_raw_buffer_queue(vi,qnum))+return;++/* If this sq is not the exclusive queue of the current cpu,+*thenitmaybecalledbystart_xmit,socheckitrunningout+*ofspace.+*+*Stopthequeuetoavoidgettingpacketsthatweare+*thenunabletotransmit.Thenwaitthetxinterrupt.+*/+if(sq->vq->num_free<2+MAX_SKB_FRAGS)+netif_stop_subqueue(dev,qnum);+}++staticintvirtnet_xsk_xmit_one(structsend_queue*sq,+structxsk_buff_pool*pool,+structxdp_desc*desc)+{+structvirtnet_info*vi;+dma_addr_taddr;++vi=sq->vq->vdev->priv;++addr=xsk_buff_raw_get_dma(pool,desc->addr);+xsk_buff_raw_dma_sync_for_device(pool,addr,desc->len);++sg_init_table(sq->sg,2);++sg_fill_dma(sq->sg,sq->xsk.hdr_dma_address,vi->hdr_len);+sg_fill_dma(sq->sg+1,addr,desc->len);++returnvirtqueue_add_outbuf_premapped(sq->vq,sq->sg,2,+xsk_to_ptr(desc->len),+GFP_ATOMIC);+}++enum{+XSK_XMIT_DONE,+XSK_XMIT_DEV_BUSY,+XSK_XMIT_NO_BUDGET+};++staticintvirtnet_xsk_xmit_batch(structsend_queue*sq,+structxsk_buff_pool*pool,+unsignedintbudget,+structvirtnet_sq_stats*stats)+{+intret=XSK_XMIT_NO_BUDGET;+structxdp_descdesc;+interr,packet=0;++while(budget-->0){+if(sq->vq->num_free<2){+__free_old_xmit(sq,true,stats);+if(sq->vq->num_free<2){+ret=XSK_XMIT_DEV_BUSY;+break;+}+}++if(!xsk_tx_peek_desc(pool,&desc)){+ret=XSK_XMIT_DONE;+break;+}++err=virtnet_xsk_xmit_one(sq,pool,&desc);+if(unlikely(err)){+ret=XSK_XMIT_DEV_BUSY;+break;+}++++packet;++if(virtqueue_kick_prepare(sq->vq)&&virtqueue_notify(sq->vq))+++stats->kicks;+}++if(packet){+stats->xdp_tx+=packet;++xsk_tx_release(pool);+}++returnret;+}++boolvirtnet_xsk_xmit(structsend_queue*sq,structxsk_buff_pool*pool,+intbudget)+{+structvirtnet_sq_statsstats={};+boolbusy;+intret;++__free_old_xmit(sq,true,&stats);++if(xsk_uses_need_wakeup(pool))+xsk_set_tx_need_wakeup(pool);++ret=virtnet_xsk_xmit_batch(sq,pool,budget,&stats);+switch(ret){+caseXSK_XMIT_DONE:+/* xsk tx qeueu has been consumed done. should complete napi. */+busy=false;+break;++caseXSK_XMIT_NO_BUDGET:+/* reach the budget limit. should let napi run again. */+busy=true;+break;++caseXSK_XMIT_DEV_BUSY:+/* sq vring is full, should complete napi. wait for tx napi been+*triggeredbyinterrupt.+*/+busy=false;+break;+}++virtnet_xsk_check_queue(sq);++u64_stats_update_begin(&sq->stats.syncp);+sq->stats.packets+=stats.packets;+sq->stats.bytes+=stats.bytes;+sq->stats.kicks+=stats.kicks;+sq->stats.xdp_tx+=stats.xdp_tx;+u64_stats_update_end(&sq->stats.syncp);++returnbusy;+}+staticintvirtnet_rq_bind_xsk_pool(structvirtnet_info*vi,structreceive_queue*rq,structxsk_buff_pool*pool,structnet_device*dev){
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:09
Raise napi tx manually without softirq/irq context.
In some cases, we hope to trigger TX Napi from the user's context.
Because it is not triggered from softirq or IRQ, softirq will not be
executed from IRQ Exit. Napi_tx_raise() here will call softirqd.
For example, in the implementation of AF_XDP ZERCOPY TX, we want TX Napi
to process packets in the XSK TX queue. But Virtio-Net does not support
to generate a interrupt from hw manually. So We hope to trigger TX NAPI
from the user's context.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
include/linux/netdevice.h | 7 +++++++
net/core/dev.c | 11 +++++++++++
2 files changed, 18 insertions(+)
@@ -6092,6 +6092,17 @@ bool napi_complete_done(struct napi_struct *n, int work_done)}EXPORT_SYMBOL(napi_complete_done);+/**+*napi_tx_raise-raisetxnapi+*+*Raisenapitxmanuallywithoutsoftirq/irqcontext.+*/+voidnapi_tx_raise(void)+{+raise_softirq(NET_TX_SOFTIRQ);+}+EXPORT_SYMBOL(napi_tx_raise);+/* must be called under rcu_read_lock(), as we dont take a reference */staticstructnapi_struct*napi_by_id(unsignedintnapi_id){
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:10
Since this will be called in other circumstances(freeze), we must check
whether it is xsk's buffer in this function. It cannot be judged outside
this function.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:11
xsk wakeup is used to trigger the logic for xsk xmit by xsk framework or
user.
Virtio-Net does not support to actively generate a interruption, so it
try to trigger tx NAPI on the tx interrupt cpu.
Consider the effect of cache. When interrupt triggers, it is
generally fixed on a CPU. It is better to start TX Napi on the same
CPU.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 3 ++
drivers/net/virtio/virtio_net.h | 2 ++
drivers/net/virtio/xsk.c | 53 +++++++++++++++++++++++++++++++++
drivers/net/virtio/xsk.h | 1 +
4 files changed, 59 insertions(+)
@@ -1613,6 +1613,8 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)intopaque;booldone;+sq->xsk.last_cpu=smp_processor_id();+if(unlikely(is_xdp_raw_buffer_queue(vi,index))){/* We don't need to enable cb for XDP */napi_complete_done(napi,0);
@@ -153,6 +153,59 @@ bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,returnbusy;}+staticvoidxsk_remote_trigger_napi(void*info)+{+structsend_queue*sq=info;++virtqueue_napi_schedule(&sq->napi,sq->vq);+}++staticvoidvirtnet_xsk_wakeup_sq(structsend_queue*sq,boolin_napi)+{+u32last_cpu,cur_cpu;++if(napi_if_scheduled_mark_missed(&sq->napi))+return;++last_cpu=sq->xsk.last_cpu;++cur_cpu=get_cpu();++/* On remote cpu, softirq will run automatically when ipi irq exit. On+*localcpu,smp_call_xxxwillnottriggeripiinterrupt,thensoftirq+*cannotbetriggeredautomaticallybyipiirqexit.+*/+if(last_cpu==cur_cpu){+virtqueue_napi_schedule(&sq->napi,sq->vq);++/* Not in softirq/irq context, we must raise napi tx manually. */+if(!in_napi)+napi_tx_raise();+}else{+smp_call_function_single(last_cpu,xsk_remote_trigger_napi,sq,true);+}++put_cpu();+}++intvirtnet_xsk_wakeup(structnet_device*dev,u32qid,u32flag)+{+structvirtnet_info*vi=netdev_priv(dev);+structsend_queue*sq;++if(!netif_running(dev))+return-ENETDOWN;++if(qid>=vi->curr_queue_pairs)+return-EINVAL;++sq=&vi->sq[qid];++virtnet_xsk_wakeup_sq(sq,false);++return0;+}+staticintvirtnet_rq_bind_xsk_pool(structvirtnet_info*vi,structreceive_queue*rq,structxsk_buff_pool*pool,structnet_device*dev){
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:12
If the XSK xmit stops because the TX queue is full, this time is
waiting for the TX interrupt to trigger the follow-up work again.
But for Virtio Net, the recycling old buf is not only completed in tx
napi, but also is called in start_xmit(), rx poll and other places.
So if xsk xmit stop by full tx queue, __free_old_xmit() will try to
wakeup tx napi.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/virtio_net.h | 5 +++--
drivers/net/virtio/xsk.c | 30 ++++++++++++++++++++++++++++++
drivers/net/virtio/xsk.h | 1 +
3 files changed, 34 insertions(+), 2 deletions(-)
@@ -138,6 +139,13 @@ bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,*triggeredbyinterrupt.*/busy=false;++/* tx poll may not be triggered by tx interruption because of+*thatstart_xmit()andrxpollwilltryfreeoldxmitthat+*causenotxinterruptionwillbegenerated.Soset+*need_wakeup,thentxpollcanbetriggeredbyfree_old_xmit.+*/+sq->xsk.need_wakeup=true;break;}
@@ -298,6 +326,8 @@ static int virtnet_xsk_pool_enable(struct net_device *dev,if(err)gotoerr_rxq;+sq->xsk.need_wakeup=false;+/* Here is already protected by rtnl_lock, so rcu_assign_pointer*issafe.*/
@@ -37,6 +37,32 @@ static void virtnet_xsk_check_queue(struct send_queue *sq)netif_stop_subqueue(dev,qnum);}+intadd_recvbuf_xsk(structvirtnet_info*vi,structreceive_queue*rq,+structxsk_buff_pool*pool,gfp_tgfp)+{+structxdp_buff*xdp;+dma_addr_taddr;+u32len;+interr;++xdp=xsk_buff_alloc(pool);+if(!xdp)+return-ENOMEM;++/* use the part of XDP_PACKET_HEADROOM as the virtnet hdr space */+addr=xsk_buff_xdp_get_dma(xdp)-vi->hdr_len;+len=xsk_pool_get_rx_frame_size(pool)+vi->hdr_len;++sg_init_table(rq->sg,1);+sg_fill_dma(rq->sg,addr,len);++err=virtqueue_add_inbuf_premapped(rq->vq,rq->sg,1,xdp,gfp);+if(err)+xsk_buff_free(xdp);++returnerr;+}+staticintvirtnet_xsk_xmit_one(structsend_queue*sq,structxsk_buff_pool*pool,structxdp_desc*desc)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:02:15
Implementing the logic of xsk rx. If this packet is not for XSK
determined in XDP, then we need to copy once to generate a SKB.
If it is for XSK, it is a zerocopy receive packet process.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 11 ++-
drivers/net/virtio/virtio_net.h | 5 ++
drivers/net/virtio/xsk.c | 116 ++++++++++++++++++++++++++++++++
drivers/net/virtio/xsk.h | 4 ++
4 files changed, 130 insertions(+), 6 deletions(-)
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-02 11:10:14
On Thu, Feb 02, 2023 at 07:00:25PM +0800, Xuan Zhuo wrote:
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good.
Great! Any numbers to share?
mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Please review.
Thanks.
Xuan Zhuo (33):
virtio_ring: virtqueue_add() support premapped
virtio_ring: split: virtqueue_add_split() support premapped
virtio_ring: packed: virtqueue_add_packed() support premapped
virtio_ring: introduce virtqueue_add_outbuf_premapped()
virtio_ring: introduce virtqueue_add_inbuf_premapped()
virtio_ring: introduce virtqueue_reset()
virtio_ring: add api virtio_dma_map() for advance dma
virtio_ring: introduce dma sync api for virtio
xsk: xsk_buff_pool add callback for dma_sync
xsk: support virtio DMA map
virtio_net: rename free_old_xmit_skbs to free_old_xmit
virtio_net: unify the code for recycling the xmit ptr
virtio_net: virtnet_poll_tx support rescheduled
virtio_net: independent directory
virtio_net: move to virtio_net.h
virtio_net: introduce virtnet_xdp_handler() to seprate the logic of
run xdp
virtio_net: receive_small() use virtnet_xdp_handler()
virtio_net: receive_merageable() use virtnet_xdp_handler()
virtio_net: introduce virtnet_tx_reset()
virtio_net: xsk: introduce virtnet_rq_bind_xsk_pool()
virtio_net: xsk: introduce virtnet_xsk_pool_enable()
virtio_net: xsk: introduce xsk disable
virtio_net: xsk: support xsk setup
virtio_net: xsk: stop disable tx napi
virtio_net: xsk: __free_old_xmit distinguishes xsk buffer
virtio_net: virtnet_sq_free_unused_buf() check xsk buffer
virtio_net: virtnet_rq_free_unused_buf() check xsk buffer
net: introduce napi_tx_raise()
virtio_net: xsk: tx: support tx
virtio_net: xsk: tx: support wakeup
virtio_net: xsk: tx: auto wakeup when free old xmit
virtio_net: xsk: rx: introduce add_recvbuf_xsk()
virtio_net: xsk: rx: introduce receive_xsk() to recv xsk buffer
MAINTAINERS | 2 +-
drivers/net/Kconfig | 8 +-
drivers/net/Makefile | 2 +-
drivers/net/virtio/Kconfig | 11 +
drivers/net/virtio/Makefile | 8 +
drivers/net/{virtio_net.c => virtio/main.c} | 564 +++++++-------------
drivers/net/virtio/virtio_net.h | 317 +++++++++++
drivers/net/virtio/xsk.c | 524 ++++++++++++++++++
drivers/net/virtio/xsk.h | 33 ++
drivers/virtio/virtio_ring.c | 376 +++++++++++--
include/linux/netdevice.h | 7 +
include/linux/virtio.h | 29 +
include/net/xsk_buff_pool.h | 6 +
net/core/dev.c | 11 +
net/xdp/xsk_buff_pool.c | 79 ++-
15 files changed, 1541 insertions(+), 436 deletions(-)
create mode 100644 drivers/net/virtio/Kconfig
create mode 100644 drivers/net/virtio/Makefile
rename drivers/net/{virtio_net.c => virtio/main.c} (92%)
create mode 100644 drivers/net/virtio/virtio_net.h
create mode 100644 drivers/net/virtio/xsk.c
create mode 100644 drivers/net/virtio/xsk.h
--
2.32.0.3.g01195cf9f
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-02 11:45:45
On Thu, 2 Feb 2023 06:08:30 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
On Thu, Feb 02, 2023 at 07:00:25PM +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good.
Great! Any numbers to share?
RESEND. Last mail has some email format error.
ENV: Qemu with vhost.
vhost cpu | Guest APP CPU |Guest Softirq CPU | PPS
-----------------------------|---------------|------------------|------------
xmit by sockperf: 90% | 100% | | 318967
xmit by xsk: 100% | 30% | 33% | 1192064
recv by sockperf: 100% | 68% | 100% | 692288
recv by xsk: 100% | 33% | 43% | 771670
Thanks.
quoted
mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Please review.
Thanks.
Xuan Zhuo (33):
virtio_ring: virtqueue_add() support premapped
virtio_ring: split: virtqueue_add_split() support premapped
virtio_ring: packed: virtqueue_add_packed() support premapped
virtio_ring: introduce virtqueue_add_outbuf_premapped()
virtio_ring: introduce virtqueue_add_inbuf_premapped()
virtio_ring: introduce virtqueue_reset()
virtio_ring: add api virtio_dma_map() for advance dma
virtio_ring: introduce dma sync api for virtio
xsk: xsk_buff_pool add callback for dma_sync
xsk: support virtio DMA map
virtio_net: rename free_old_xmit_skbs to free_old_xmit
virtio_net: unify the code for recycling the xmit ptr
virtio_net: virtnet_poll_tx support rescheduled
virtio_net: independent directory
virtio_net: move to virtio_net.h
virtio_net: introduce virtnet_xdp_handler() to seprate the logic of
run xdp
virtio_net: receive_small() use virtnet_xdp_handler()
virtio_net: receive_merageable() use virtnet_xdp_handler()
virtio_net: introduce virtnet_tx_reset()
virtio_net: xsk: introduce virtnet_rq_bind_xsk_pool()
virtio_net: xsk: introduce virtnet_xsk_pool_enable()
virtio_net: xsk: introduce xsk disable
virtio_net: xsk: support xsk setup
virtio_net: xsk: stop disable tx napi
virtio_net: xsk: __free_old_xmit distinguishes xsk buffer
virtio_net: virtnet_sq_free_unused_buf() check xsk buffer
virtio_net: virtnet_rq_free_unused_buf() check xsk buffer
net: introduce napi_tx_raise()
virtio_net: xsk: tx: support tx
virtio_net: xsk: tx: support wakeup
virtio_net: xsk: tx: auto wakeup when free old xmit
virtio_net: xsk: rx: introduce add_recvbuf_xsk()
virtio_net: xsk: rx: introduce receive_xsk() to recv xsk buffer
MAINTAINERS | 2 +-
drivers/net/Kconfig | 8 +-
drivers/net/Makefile | 2 +-
drivers/net/virtio/Kconfig | 11 +
drivers/net/virtio/Makefile | 8 +
drivers/net/{virtio_net.c => virtio/main.c} | 564 +++++++-------------
drivers/net/virtio/virtio_net.h | 317 +++++++++++
drivers/net/virtio/xsk.c | 524 ++++++++++++++++++
drivers/net/virtio/xsk.h | 33 ++
drivers/virtio/virtio_ring.c | 376 +++++++++++--
include/linux/netdevice.h | 7 +
include/linux/virtio.h | 29 +
include/net/xsk_buff_pool.h | 6 +
net/core/dev.c | 11 +
net/xdp/xsk_buff_pool.c | 79 ++-
15 files changed, 1541 insertions(+), 436 deletions(-)
create mode 100644 drivers/net/virtio/Kconfig
create mode 100644 drivers/net/virtio/Makefile
rename drivers/net/{virtio_net.c => virtio/main.c} (92%)
create mode 100644 drivers/net/virtio/virtio_net.h
create mode 100644 drivers/net/virtio/xsk.c
create mode 100644 drivers/net/virtio/xsk.h
--
2.32.0.3.g01195cf9f
From: Magnus Karlsson <hidden> Date: 2023-02-02 12:45:04
On Thu, 2 Feb 2023 at 12:05, Xuan Zhuo [off-list ref] wrote:
quoted hunk
In the process of dma sync, we involved whether virtio uses dma api. On
the other hand, it is also necessary to read vdev->dev.parent. So these
API has been introduced.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 61 ++++++++++++++++++++++++++++++++++++
include/linux/virtio.h | 8 +++++
2 files changed, 69 insertions(+)
If we put these two pointers here, the number of cache lines required
in the data path for this struct will be increased from 2 to 3 which
will likely affect performance negatively. These sync operations are
also not used on most systems. So how about we put them in the first
section of this struct labeled "Members only used in the control path
first." instead. There is a 26-byte hole at the end of it that can be
used.
From: Paolo Abeni <pabeni@redhat.com> Date: 2023-02-02 14:42:40
On Thu, 2023-02-02 at 19:00 +0800, Xuan Zhuo wrote:
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Thank you for the large effort.
Since this will likely need a few iterations, on next revision please
do split the work in multiple chunks to help the reviewer efforts -
from Documentation/process/maintainer-netdev.rst:
- don't post large series (> 15 patches), break them up
In this case I guess you can split it in 1 (or even 2) pre-req series
and another one for the actual xsk zero copy support.
Thanks!
Paolo
@@ -809,7 +818,7 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,unsignedintxdp_frags_truesz=0;structpage*page;skb_frag_t*frag;-intoffset;+intoffset,i;void*ctx;xdp_init_buff(xdp,frame_sz,&rq->xdp_rxq);
@@ -842,7 +851,7 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,dev->name,*num_buf,virtio16_to_cpu(vi->vdev,hdr->num_buffers));dev->stats.rx_length_errors++;-return-EINVAL;+gotoerr;}stats->bytes+=len;
@@ -861,7 +870,7 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,pr_debug("%s: rx error: len %u exceeds truesize %lu\n",dev->name,len,(unsignedlong)(truesize-room));dev->stats.rx_length_errors++;-return-EINVAL;+gotoerr;}frag=&shinfo->frags[shinfo->nr_frags++];
@@ -876,6 +885,14 @@ static int virtnet_build_xdp_buff_mrg(struct net_device *dev,*xdp_frags_truesize=xdp_frags_truesz;return0;++err:+for(i=0;i<shinfo->nr_frags;i++){+page=skb_frag_page(&shinfo->frags[i]);+put_page(page);+}++return-EINVAL;}staticstructsk_buff*receive_mergeable(structnet_device*dev,
@@ -919,13 +936,10 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,xdp_prog=rcu_dereference(rq->xdp_prog);if(xdp_prog){unsignedintxdp_frags_truesz=0;-structskb_shared_info*shinfo;-structxdp_frame*xdpf;structpage*xdp_page;structxdp_buffxdp;void*data;u32act;-inti;/* Transient failure which in theory could occur if*in-flightpacketsfrombeforeXDPwasenabledreach
@@ -983,69 +997,33 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,err=virtnet_build_xdp_buff_mrg(dev,vi,rq,&xdp,data,len,frame_sz,&num_buf,&xdp_frags_truesz,stats);if(unlikely(err))-gotoerr_xdp_frags;+gotoerr_xdp;-act=bpf_prog_run_xdp(xdp_prog,&xdp);-stats->xdp_packets++;+act=virtnet_xdp_handler(xdp_prog,&xdp,dev,xdp_xmit,stats);switch(act){-caseXDP_PASS:+caseVIRTNET_XDP_RES_PASS:if(unlikely(xdp_page!=page))put_page(page);+head_skb=build_skb_from_xdp_buff(dev,vi,&xdp,xdp_frags_truesz);rcu_read_unlock();returnhead_skb;-caseXDP_TX:-stats->xdp_tx++;-xdpf=xdp_convert_buff_to_frame(&xdp);-if(unlikely(!xdpf)){-netdev_dbg(dev,"convert buff to frame failed for xdp\n");-gotoerr_xdp_frags;-}-err=virtnet_xdp_xmit(dev,1,&xdpf,0);-if(unlikely(!err)){-xdp_return_frame_rx_napi(xdpf);-}elseif(unlikely(err<0)){-trace_xdp_exception(vi->dev,xdp_prog,act);-gotoerr_xdp_frags;-}-*xdp_xmit|=VIRTIO_XDP_TX;-if(unlikely(xdp_page!=page))-put_page(page);-rcu_read_unlock();-gotoxdp_xmit;-caseXDP_REDIRECT:-stats->xdp_redirects++;-err=xdp_do_redirect(dev,&xdp,xdp_prog);-if(err)-gotoerr_xdp_frags;-*xdp_xmit|=VIRTIO_XDP_REDIR;++caseVIRTNET_XDP_RES_CONSUMED:if(unlikely(xdp_page!=page))put_page(page);+rcu_read_unlock();gotoxdp_xmit;-default:-bpf_warn_invalid_xdp_action(vi->dev,xdp_prog,act);-fallthrough;-caseXDP_ABORTED:-trace_xdp_exception(vi->dev,xdp_prog,act);-fallthrough;-caseXDP_DROP:-gotoerr_xdp_frags;-}-err_xdp_frags:-if(unlikely(xdp_page!=page))-__free_pages(xdp_page,0);-if(xdp_buff_has_frags(&xdp)){-shinfo=xdp_get_shared_info_from_buff(&xdp);-for(i=0;i<shinfo->nr_frags;i++){-xdp_page=skb_frag_page(&shinfo->frags[i]);+caseVIRTNET_XDP_RES_DROP:+if(unlikely(xdp_page!=page))put_page(xdp_page);-}-}-gotoerr_xdp;+rcu_read_unlock();+gotoerr_xdp;+}}rcu_read_unlock();
This __virtnet_tx_reset is a really weird API.
Suggest just splitting the common parts:
__virtnet_tx_pause
__virtnet_tx_resume
we can then implement virtnet_tx_resize and virtnet_tx_reset
using these two.
quoted hunk
@@ -1847,6 +1851,17 @@ static int virtnet_tx_resize(struct virtnet_info *vi, return err; }+static int virtnet_tx_resize(struct virtnet_info *vi,+ struct send_queue *sq, u32 ring_num)+{+ return __virtnet_tx_reset(vi, sq, ring_num);+}++int virtnet_tx_reset(struct virtnet_info *vi, struct send_queue *sq)+{+ return __virtnet_tx_reset(vi, sq, 0);+}+ /* * Send command via the control virtqueue and check status. Commands * supported by the hypervisor, as indicated by feature bits, should
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-02 17:27:05
On Thu, Feb 02, 2023 at 07:00:49PM +0800, Xuan Zhuo wrote:
quoted hunk
Since xsk's TX queue is consumed by TX NAPI, if sq is bound to xsk, then
we must stop tx napi from being disabled.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
@@ -2728,8 +2728,15 @@ static int virtnet_set_coalesce(struct net_device *dev,returnret;if(update_napi){-for(i=0;i<vi->max_queue_pairs;i++)+for(i=0;i<vi->max_queue_pairs;i++){+/* xsk xmit depend on the tx napi. So if xsk is active,
depends.
+ * prevent modifications to tx napi.
+ */
+ if (rtnl_dereference(vi->sq[i].xsk.pool))
+ continue;
+
vi->sq[i].napi.weight = napi_weight;
I don't get it.
changing napi weight does not work then.
why is this ok?
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-03 03:26:33
On Thu, 2 Feb 2023 12:25:59 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
On Thu, Feb 02, 2023 at 07:00:49PM +0800, Xuan Zhuo wrote:
quoted
Since xsk's TX queue is consumed by TX NAPI, if sq is bound to xsk, then
we must stop tx napi from being disabled.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
@@ -2728,8 +2728,15 @@ static int virtnet_set_coalesce(struct net_device *dev,returnret;if(update_napi){-for(i=0;i<vi->max_queue_pairs;i++)+for(i=0;i<vi->max_queue_pairs;i++){+/* xsk xmit depend on the tx napi. So if xsk is active,
depends.
quoted
+ * prevent modifications to tx napi.
+ */
+ if (rtnl_dereference(vi->sq[i].xsk.pool))
+ continue;
+
vi->sq[i].napi.weight = napi_weight;
I don't get it.
changing napi weight does not work then.
why is this ok?
static void skb_xmit_done(struct virtqueue *vq)
{
struct virtnet_info *vi = vq->vdev->priv;
struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
/* Suppress further interrupts. */
virtqueue_disable_cb(vq);
if (napi->weight)
virtqueue_napi_schedule(napi, vq);
else
/* We were probably waiting for more output buffers. */
netif_wake_subqueue(vi->dev, vq2txq(vq));
}
If the weight is 0, tx napi will not be triggered again.
Thanks.
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-03 03:41:02
On Thu, 02 Feb 2023 15:41:44 +0100, Paolo Abeni [off-list ref] wrote:
On Thu, 2023-02-02 at 19:00 +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Thank you for the large effort.
Since this will likely need a few iterations, on next revision please
do split the work in multiple chunks to help the reviewer efforts -
from Documentation/process/maintainer-netdev.rst:
- don't post large series (> 15 patches), break them up
In this case I guess you can split it in 1 (or even 2) pre-req series
and another one for the actual xsk zero copy support.
OK.
I can split patch into multiple parts such as
* virtio core
* xsk
* virtio-net prepare
* virtio-net support xsk zerocopy
However, there is a problem, the virtio core part should enter the VHOST branch
of Michael. Then, should I post follow-up patches to which branch vhost or
next-next?
Thanks.
This __virtnet_tx_reset is a really weird API.
Suggest just splitting the common parts:
__virtnet_tx_pause
__virtnet_tx_resume
we can then implement virtnet_tx_resize and virtnet_tx_reset
using these two.
Good idea.
Thanks.
quoted
@@ -1847,6 +1851,17 @@ static int virtnet_tx_resize(struct virtnet_info *vi, return err; }+static int virtnet_tx_resize(struct virtnet_info *vi,+ struct send_queue *sq, u32 ring_num)+{+ return __virtnet_tx_reset(vi, sq, ring_num);+}++int virtnet_tx_reset(struct virtnet_info *vi, struct send_queue *sq)+{+ return __virtnet_tx_reset(vi, sq, 0);+}+ /* * Send command via the control virtqueue and check status. Commands * supported by the hypervisor, as indicated by feature bits, should
If we put these two pointers here, the number of cache lines required
in the data path for this struct will be increased from 2 to 3 which
will likely affect performance negatively. These sync operations are
also not used on most systems. So how about we put them in the first
section of this struct labeled "Members only used in the control path
first." instead. There is a 26-byte hole at the end of it that can be
used.
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 08:34:59
On Fri, Feb 03, 2023 at 11:24:42AM +0800, Xuan Zhuo wrote:
On Thu, 2 Feb 2023 12:25:59 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
quoted
On Thu, Feb 02, 2023 at 07:00:49PM +0800, Xuan Zhuo wrote:
quoted
Since xsk's TX queue is consumed by TX NAPI, if sq is bound to xsk, then
we must stop tx napi from being disabled.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
@@ -2728,8 +2728,15 @@ static int virtnet_set_coalesce(struct net_device *dev,returnret;if(update_napi){-for(i=0;i<vi->max_queue_pairs;i++)+for(i=0;i<vi->max_queue_pairs;i++){+/* xsk xmit depend on the tx napi. So if xsk is active,
depends.
quoted
+ * prevent modifications to tx napi.
+ */
+ if (rtnl_dereference(vi->sq[i].xsk.pool))
+ continue;
+
vi->sq[i].napi.weight = napi_weight;
I don't get it.
changing napi weight does not work then.
why is this ok?
static void skb_xmit_done(struct virtqueue *vq)
{
struct virtnet_info *vi = vq->vdev->priv;
struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
/* Suppress further interrupts. */
virtqueue_disable_cb(vq);
if (napi->weight)
virtqueue_napi_schedule(napi, vq);
else
/* We were probably waiting for more output buffers. */
netif_wake_subqueue(vi->dev, vq2txq(vq));
}
If the weight is 0, tx napi will not be triggered again.
Thanks.
This needs more thought then. First ignoring what user is requesting is
not nice. Second what if napi is first disabled and then xsk enabled?
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 08:38:27
On Fri, Feb 03, 2023 at 11:33:31AM +0800, Xuan Zhuo wrote:
On Thu, 02 Feb 2023 15:41:44 +0100, Paolo Abeni [off-list ref] wrote:
quoted
On Thu, 2023-02-02 at 19:00 +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Thank you for the large effort.
Since this will likely need a few iterations, on next revision please
do split the work in multiple chunks to help the reviewer efforts -
from Documentation/process/maintainer-netdev.rst:
- don't post large series (> 15 patches), break them up
In this case I guess you can split it in 1 (or even 2) pre-req series
and another one for the actual xsk zero copy support.
OK.
I can split patch into multiple parts such as
* virtio core
* xsk
* virtio-net prepare
* virtio-net support xsk zerocopy
However, there is a problem, the virtio core part should enter the VHOST branch
of Michael. Then, should I post follow-up patches to which branch vhost or
next-next?
Thanks.
I personally think 33 patches is still manageable no need to split.
Do try to be careful and track acks and changes: if someone sends an ack
add it in the patch if you change the patch drop the acks,
and logs this fact in the changelog in the cover letter
so people know they need to re-review.
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2023-02-03 08:39:42
On Thu, Feb 02, 2023 at 07:00:54PM +0800, Xuan Zhuo wrote:
The driver's tx napi is very important for XSK. It is responsible for
obtaining data from the XSK queue and sending it out.
At the beginning, we need to trigger tx napi.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 12 +++-
drivers/net/virtio/xsk.c | 146 ++++++++++++++++++++++++++++++++++++++
drivers/net/virtio/xsk.h | 2 +
3 files changed, 159 insertions(+), 1 deletion(-)
(...)
+static int virtnet_xsk_xmit_batch(struct send_queue *sq,
+ struct xsk_buff_pool *pool,
+ unsigned int budget,
+ struct virtnet_sq_stats *stats)
+{
+ int ret = XSK_XMIT_NO_BUDGET;
+ struct xdp_desc desc;
+ int err, packet = 0;
+
+ while (budget-- > 0) {
+ if (sq->vq->num_free < 2) {
+ __free_old_xmit(sq, true, stats);
+ if (sq->vq->num_free < 2) {
+ ret = XSK_XMIT_DEV_BUSY;
+ break;
+ }
+ }
+
+ if (!xsk_tx_peek_desc(pool, &desc)) {
anything that stopped from using xsk_tx_peek_release_desc_batch() ?
quoted hunk
+ ret = XSK_XMIT_DONE;
+ break;
+ }
+
+ err = virtnet_xsk_xmit_one(sq, pool, &desc);
+ if (unlikely(err)) {
+ ret = XSK_XMIT_DEV_BUSY;
+ break;
+ }
+
+ ++packet;
+
+ if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
+ ++stats->kicks;
+ }
+
+ if (packet) {
+ stats->xdp_tx += packet;
+
+ xsk_tx_release(pool);
+ }
+
+ return ret;
+}
+
+bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
+ int budget)
+{
+ struct virtnet_sq_stats stats = {};
+ bool busy;
+ int ret;
+
+ __free_old_xmit(sq, true, &stats);
+
+ if (xsk_uses_need_wakeup(pool))
+ xsk_set_tx_need_wakeup(pool);
+
+ ret = virtnet_xsk_xmit_batch(sq, pool, budget, &stats);
+ switch (ret) {
+ case XSK_XMIT_DONE:
+ /* xsk tx qeueu has been consumed done. should complete napi. */
+ busy = false;
+ break;
+
+ case XSK_XMIT_NO_BUDGET:
+ /* reach the budget limit. should let napi run again. */
+ busy = true;
+ break;
+
+ case XSK_XMIT_DEV_BUSY:
+ /* sq vring is full, should complete napi. wait for tx napi been
+ * triggered by interrupt.
+ */
+ busy = false;
+ break;
+ }
+
+ virtnet_xsk_check_queue(sq);
+
+ u64_stats_update_begin(&sq->stats.syncp);
+ sq->stats.packets += stats.packets;
+ sq->stats.bytes += stats.bytes;
+ sq->stats.kicks += stats.kicks;
+ sq->stats.xdp_tx += stats.xdp_tx;
+ u64_stats_update_end(&sq->stats.syncp);
+
+ return busy;
+}
+
static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queue *rq,
struct xsk_buff_pool *pool, struct net_device *dev)
{
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Date: 2023-02-03 08:47:29
On Fri, Feb 03, 2023 at 03:37:32AM -0500, Michael S. Tsirkin wrote:
On Fri, Feb 03, 2023 at 11:33:31AM +0800, Xuan Zhuo wrote:
quoted
On Thu, 02 Feb 2023 15:41:44 +0100, Paolo Abeni [off-list ref] wrote:
quoted
On Thu, 2023-02-02 at 19:00 +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Thank you for the large effort.
Since this will likely need a few iterations, on next revision please
do split the work in multiple chunks to help the reviewer efforts -
from Documentation/process/maintainer-netdev.rst:
- don't post large series (> 15 patches), break them up
In this case I guess you can split it in 1 (or even 2) pre-req series
and another one for the actual xsk zero copy support.
OK.
I can split patch into multiple parts such as
* virtio core
* xsk
* virtio-net prepare
* virtio-net support xsk zerocopy
However, there is a problem, the virtio core part should enter the VHOST branch
of Michael. Then, should I post follow-up patches to which branch vhost or
next-next?
Thanks.
I personally think 33 patches is still manageable no need to split.
Do try to be careful and track acks and changes: if someone sends an ack
add it in the patch if you change the patch drop the acks,
and logs this fact in the changelog in the cover letter
so people know they need to re-review.
To me some of the patches are too granular but probably this is related to
personal taste. However, I would like to ask to check how this series
affects existing ZC enabled driver(s), since xsk core is touched.
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 08:49:35
On Thu, Feb 02, 2023 at 07:00:45PM +0800, Xuan Zhuo wrote:
quoted hunk
This function is used to bind or unbind xsk pool to virtnet rq.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/Makefile | 2 +-
drivers/net/virtio/main.c | 8 ++---
drivers/net/virtio/virtio_net.h | 16 ++++++++++
drivers/net/virtio/xsk.c | 56 +++++++++++++++++++++++++++++++++
4 files changed, 76 insertions(+), 6 deletions(-)
create mode 100644 drivers/net/virtio/xsk.c
@@ -168,6 +168,12 @@ struct send_queue {/* Record whether sq is in reset state. */boolreset;++struct{+structxsk_buff_pool__rcu*pool;++dma_addr_thdr_dma_address;+}xsk;};/* Internal representation of a receive virtqueue */
@@ -200,6 +206,13 @@ struct receive_queue {charname[16];structxdp_rxq_infoxdp_rxq;++struct{+structxsk_buff_pool__rcu*pool;++/* xdp rxq used by xsk */+structxdp_rxq_infoxdp_rxq;+}xsk;};staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-03 08:51:10
On Fri, 3 Feb 2023 03:33:41 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
On Fri, Feb 03, 2023 at 11:24:42AM +0800, Xuan Zhuo wrote:
quoted
On Thu, 2 Feb 2023 12:25:59 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
quoted
On Thu, Feb 02, 2023 at 07:00:49PM +0800, Xuan Zhuo wrote:
quoted
Since xsk's TX queue is consumed by TX NAPI, if sq is bound to xsk, then
we must stop tx napi from being disabled.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
@@ -2728,8 +2728,15 @@ static int virtnet_set_coalesce(struct net_device *dev,returnret;if(update_napi){-for(i=0;i<vi->max_queue_pairs;i++)+for(i=0;i<vi->max_queue_pairs;i++){+/* xsk xmit depend on the tx napi. So if xsk is active,
depends.
quoted
+ * prevent modifications to tx napi.
+ */
+ if (rtnl_dereference(vi->sq[i].xsk.pool))
+ continue;
+
vi->sq[i].napi.weight = napi_weight;
I don't get it.
changing napi weight does not work then.
why is this ok?
static void skb_xmit_done(struct virtqueue *vq)
{
struct virtnet_info *vi = vq->vdev->priv;
struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
/* Suppress further interrupts. */
virtqueue_disable_cb(vq);
if (napi->weight)
virtqueue_napi_schedule(napi, vq);
else
/* We were probably waiting for more output buffers. */
netif_wake_subqueue(vi->dev, vq2txq(vq));
}
If the weight is 0, tx napi will not be triggered again.
Thanks.
This needs more thought then. First ignoring what user is requesting is
not nice.
Maybe we should return an error.
Second what if napi is first disabled and then xsk enabled?
static int virtnet_xsk_pool_enable(struct net_device *dev,
struct xsk_buff_pool *pool,
u16 qid)
{
struct virtnet_info *vi = netdev_priv(dev);
struct receive_queue *rq;
struct send_queue *sq;
int err;
if (qid >= vi->curr_queue_pairs)
return -EINVAL;
sq = &vi->sq[qid];
rq = &vi->rq[qid];
/* xsk zerocopy depend on the tx napi.
*
* All xsk packets are actually consumed and sent out from the xsk tx
* queue under the tx napi mechanism.
*/
-> if (!sq->napi.weight)
return -EPERM;
Thanks.
You should only move the headers that are actually needed not
everything.
quoted hunk
@@ -44,15 +28,6 @@ module_param(napi_tx, bool, 0644); #define VIRTIO_XDP_TX BIT(0) #define VIRTIO_XDP_REDIR BIT(1)-#define VIRTIO_XDP_FLAG BIT(0)--/* RX packet size EWMA. The average packet size is used to determine the packet- * buffer size when refilling RX rings. As the entire RX ring may be refilled- * at once, the weight is chosen so that the EWMA will be insensitive to short-- * term, transient changes in packet size.- */-DECLARE_EWMA(pkt_len, 0, 64)- #define VIRTNET_DRIVER_VERSION "1.0.0" static const unsigned long guest_offloads[] = {
@@ -125,57 +70,6 @@ static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)-/* Internal representation of a send virtqueue */-struct send_queue {- /* Virtqueue associated with this send _queue */- struct virtqueue *vq;-- /* TX: fragments + linear part + virtio header */- struct scatterlist sg[MAX_SKB_FRAGS + 2];-- /* Name of the send queue: output.$index */- char name[16];-- struct virtnet_sq_stats stats;-- struct napi_struct napi;-- /* Record whether sq is in reset state. */- bool reset;-};--/* Internal representation of a receive virtqueue */-struct receive_queue {- /* Virtqueue associated with this receive_queue */- struct virtqueue *vq;-- struct napi_struct napi;-- struct bpf_prog __rcu *xdp_prog;-- struct virtnet_rq_stats stats;-- /* Chain pages by the private ptr. */- struct page *pages;-- /* Average packet length for mergeable receive buffers. */- struct ewma_pkt_len mrg_avg_pkt_len;-- /* Page frag for packet buffer allocation. */- struct page_frag alloc_frag;-- /* RX: fragments + linear part + virtio header */- struct scatterlist sg[MAX_SKB_FRAGS + 2];-- /* Min single buffer size for mergeable buffers case. */- unsigned int min_buf_len;-- /* Name of this receive queue: input.$index */- char name[16];-- struct xdp_rxq_info xdp_rxq;-};- /* This structure can contain rss message with maximum settings for indirection table and keysize * Note, that default structure that describes RSS configuration virtio_net_rss_config * contains same info but can't handle table values.
@@ -206,90 +100,6 @@ struct control_buf { struct virtio_net_ctrl_rss rss; };-struct virtnet_info {- struct virtio_device *vdev;- struct virtqueue *cvq;- struct net_device *dev;- struct send_queue *sq;- struct receive_queue *rq;- unsigned int status;-- /* Max # of queue pairs supported by the device */- u16 max_queue_pairs;-- /* # of queue pairs currently used by the driver */- u16 curr_queue_pairs;-- /* # of XDP queue pairs currently used by the driver */- u16 xdp_queue_pairs;-- /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */- bool xdp_enabled;-- /* I like... big packets and I cannot lie! */- bool big_packets;-- /* number of sg entries allocated for big packets */- unsigned int big_packets_num_skbfrags;-- /* Host will merge rx buffers for big packets (shake it! shake it!) */- bool mergeable_rx_bufs;-- /* Host supports rss and/or hash report */- bool has_rss;- bool has_rss_hash_report;- u8 rss_key_size;- u16 rss_indir_table_size;- u32 rss_hash_types_supported;- u32 rss_hash_types_saved;-- /* Has control virtqueue */- bool has_cvq;-- /* Host can handle any s/g split between our header and packet data */- bool any_header_sg;-- /* Packet virtio header size */- u8 hdr_len;-- /* Work struct for delayed refilling if we run low on memory. */- struct delayed_work refill;-- /* Is delayed refill enabled? */- bool refill_enabled;-- /* The lock to synchronize the access to refill_enabled */- spinlock_t refill_lock;-- /* Work struct for config space updates */- struct work_struct config_work;-- /* Does the affinity hint is set for virtqueues? */- bool affinity_hint_set;-- /* CPU hotplug instances for online & dead */- struct hlist_node node;- struct hlist_node node_dead;-- struct control_buf *ctrl;-- /* Ethtool settings */- u8 duplex;- u32 speed;-- /* Interrupt coalescing settings */- u32 tx_usecs;- u32 rx_usecs;- u32 tx_max_packets;- u32 rx_max_packets;-- unsigned long guest_offloads;- unsigned long guest_offloads_capable;-- /* failover when STANDBY feature enabled */- struct failover *failover;-};- struct padded_vnet_hdr { struct virtio_net_hdr_v1_hash hdr; /*
@@ -0,0 +1,265 @@+/* SPDX-License-Identifier: GPL-2.0-or-later */++#ifndef __VIRTIO_NET_H__+#define __VIRTIO_NET_H__+#include<linux/netdevice.h>+#include<linux/etherdevice.h>+#include<linux/ethtool.h>+#include<linux/module.h>+#include<linux/virtio.h>+#include<linux/virtio_net.h>+#include<linux/bpf.h>+#include<linux/bpf_trace.h>+#include<linux/scatterlist.h>+#include<linux/if_vlan.h>+#include<linux/slab.h>+#include<linux/cpu.h>+#include<linux/average.h>+#include<linux/filter.h>+#include<linux/kernel.h>+#include<net/route.h>+#include<net/xdp.h>+#include<net/net_failover.h>+#include<net/xdp_sock_drv.h>++#define VIRTIO_XDP_FLAG BIT(0)++structvirtnet_info{+structvirtio_device*vdev;+structvirtqueue*cvq;+structnet_device*dev;+structsend_queue*sq;+structreceive_queue*rq;+unsignedintstatus;++/* Max # of queue pairs supported by the device */+u16max_queue_pairs;++/* # of queue pairs currently used by the driver */+u16curr_queue_pairs;++/* # of XDP queue pairs currently used by the driver */+u16xdp_queue_pairs;++/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */+boolxdp_enabled;++/* I like... big packets and I cannot lie! */+boolbig_packets;++/* number of sg entries allocated for big packets */+unsignedintbig_packets_num_skbfrags;++/* Host will merge rx buffers for big packets (shake it! shake it!) */+boolmergeable_rx_bufs;++/* Host supports rss and/or hash report */+boolhas_rss;+boolhas_rss_hash_report;+u8rss_key_size;+u16rss_indir_table_size;+u32rss_hash_types_supported;+u32rss_hash_types_saved;++/* Has control virtqueue */+boolhas_cvq;++/* Host can handle any s/g split between our header and packet data */+boolany_header_sg;++/* Packet virtio header size */+u8hdr_len;++/* Work struct for delayed refilling if we run low on memory. */+structdelayed_workrefill;++/* Is delayed refill enabled? */+boolrefill_enabled;++/* The lock to synchronize the access to refill_enabled */+spinlock_trefill_lock;++/* Work struct for config space updates */+structwork_structconfig_work;++/* Does the affinity hint is set for virtqueues? */+boolaffinity_hint_set;++/* CPU hotplug instances for online & dead */+structhlist_nodenode;+structhlist_nodenode_dead;++structcontrol_buf*ctrl;++/* Ethtool settings */+u8duplex;+u32speed;++/* Interrupt coalescing settings */+u32tx_usecs;+u32rx_usecs;+u32tx_max_packets;+u32rx_max_packets;++unsignedlongguest_offloads;+unsignedlongguest_offloads_capable;++/* failover when STANDBY feature enabled */+structfailover*failover;+};++/* RX packet size EWMA. The average packet size is used to determine the packet+*buffersizewhenrefillingRXrings.AstheentireRXringmayberefilled+*atonce,theweightischosensothattheEWMAwillbeinsensitivetoshort-+*term,transientchangesinpacketsize.+*/+DECLARE_EWMA(pkt_len,0,64)++structvirtnet_stat_desc{+chardesc[ETH_GSTRING_LEN];+size_toffset;+};++structvirtnet_sq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64xdp_tx;+u64xdp_tx_drops;+u64kicks;+u64tx_timeouts;+};++structvirtnet_rq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64drops;+u64xdp_packets;+u64xdp_tx;+u64xdp_redirects;+u64xdp_drops;+u64kicks;+};++#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)+#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)++/* Internal representation of a send virtqueue */+structsend_queue{+/* Virtqueue associated with this send _queue */+structvirtqueue*vq;++/* TX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Name of the send queue: output.$index */+charname[16];++structvirtnet_sq_statsstats;++structnapi_structnapi;++/* Record whether sq is in reset state. */+boolreset;+};++/* Internal representation of a receive virtqueue */+structreceive_queue{+/* Virtqueue associated with this receive_queue */+structvirtqueue*vq;++structnapi_structnapi;++structbpf_prog__rcu*xdp_prog;++structvirtnet_rq_statsstats;++/* Chain pages by the private ptr. */+structpage*pages;++/* Average packet length for mergeable receive buffers. */+structewma_pkt_lenmrg_avg_pkt_len;++/* Page frag for packet buffer allocation. */+structpage_fragalloc_frag;++/* RX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Min single buffer size for mergeable buffers case. */+unsignedintmin_buf_len;++/* Name of this receive queue: input.$index */+charname[16];++structxdp_rxq_infoxdp_rxq;+};++staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)+{+if(q<(vi->curr_queue_pairs-vi->xdp_queue_pairs))+returnfalse;+elseif(q<vi->curr_queue_pairs)+returntrue;+else+returnfalse;+}++staticinlinevoidvirtnet_return_xdp_frame(structsend_queue*sq,+structxdp_frame*frame)+{+structvirtnet_info*vi=sq->vq->vdev->priv;+dma_addr_t*p_addr,addr;++p_addr=frame->data-sizeof(*p_addr);+addr=*p_addr;++virtio_dma_unmap(&vi->vdev->dev,addr,frame->len,DMA_TO_DEVICE);++xdp_return_frame(frame);+}++staticinlinevoidvirtqueue_napi_schedule(structnapi_struct*napi,+structvirtqueue*vq)+{+if(napi_schedule_prep(napi)){+virtqueue_disable_cb(vq);+__napi_schedule(napi);+}+}++staticinlineboolis_xdp_frame(void*ptr)+{+return(unsignedlong)ptr&VIRTIO_XDP_FLAG;+}++staticstructxdp_frame*ptr_to_xdp(void*ptr)+{+return(structxdp_frame*)((unsignedlong)ptr&~VIRTIO_XDP_FLAG);+}++staticvoid__free_old_xmit(structsend_queue*sq,boolin_napi,+structvirtnet_sq_stats*stats)+{+unsignedintlen;+void*ptr;++while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){+if(!is_xdp_frame(ptr)){+structsk_buff*skb=ptr;++pr_debug("Sent skb %p\n",skb);++stats->bytes+=skb->len;+napi_consume_skb(skb,in_napi);+}else{+structxdp_frame*frame=ptr_to_xdp(ptr);++stats->bytes+=xdp_get_frame_len(frame);+xdp_return_frame(frame);+}+stats->packets++;+}+}+#endif
All these APIs not prefixed with virtnet were ok as internal
static functions. No longer ok in a header.
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-03 08:54:56
On Fri, 3 Feb 2023 03:48:33 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
On Thu, Feb 02, 2023 at 07:00:45PM +0800, Xuan Zhuo wrote:
quoted
This function is used to bind or unbind xsk pool to virtnet rq.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/Makefile | 2 +-
drivers/net/virtio/main.c | 8 ++---
drivers/net/virtio/virtio_net.h | 16 ++++++++++
drivers/net/virtio/xsk.c | 56 +++++++++++++++++++++++++++++++++
4 files changed, 76 insertions(+), 6 deletions(-)
create mode 100644 drivers/net/virtio/xsk.c
@@ -168,6 +168,12 @@ struct send_queue {/* Record whether sq is in reset state. */boolreset;++struct{+structxsk_buff_pool__rcu*pool;++dma_addr_thdr_dma_address;+}xsk;};/* Internal representation of a receive virtqueue */
@@ -200,6 +206,13 @@ struct receive_queue {charname[16];structxdp_rxq_infoxdp_rxq;++struct{+structxsk_buff_pool__rcu*pool;++/* xdp rxq used by xsk */+structxdp_rxq_infoxdp_rxq;+}xsk;};staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-03 08:55:53
On Fri, 3 Feb 2023 09:39:25 +0100, Maciej Fijalkowski [off-list ref] wrote:
On Thu, Feb 02, 2023 at 07:00:54PM +0800, Xuan Zhuo wrote:
quoted
The driver's tx napi is very important for XSK. It is responsible for
obtaining data from the XSK queue and sending it out.
At the beginning, we need to trigger tx napi.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 12 +++-
drivers/net/virtio/xsk.c | 146 ++++++++++++++++++++++++++++++++++++++
drivers/net/virtio/xsk.h | 2 +
3 files changed, 159 insertions(+), 1 deletion(-)
(...)
quoted
+static int virtnet_xsk_xmit_batch(struct send_queue *sq,
+ struct xsk_buff_pool *pool,
+ unsigned int budget,
+ struct virtnet_sq_stats *stats)
+{
+ int ret = XSK_XMIT_NO_BUDGET;
+ struct xdp_desc desc;
+ int err, packet = 0;
+
+ while (budget-- > 0) {
+ if (sq->vq->num_free < 2) {
+ __free_old_xmit(sq, true, stats);
+ if (sq->vq->num_free < 2) {
+ ret = XSK_XMIT_DEV_BUSY;
+ break;
+ }
+ }
+
+ if (!xsk_tx_peek_desc(pool, &desc)) {
anything that stopped from using xsk_tx_peek_release_desc_batch() ?
Great!
Will fix.
Thanks.
quoted
+ ret = XSK_XMIT_DONE;
+ break;
+ }
+
+ err = virtnet_xsk_xmit_one(sq, pool, &desc);
+ if (unlikely(err)) {
+ ret = XSK_XMIT_DEV_BUSY;
+ break;
+ }
+
+ ++packet;
+
+ if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
+ ++stats->kicks;
+ }
+
+ if (packet) {
+ stats->xdp_tx += packet;
+
+ xsk_tx_release(pool);
+ }
+
+ return ret;
+}
+
+bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
+ int budget)
+{
+ struct virtnet_sq_stats stats = {};
+ bool busy;
+ int ret;
+
+ __free_old_xmit(sq, true, &stats);
+
+ if (xsk_uses_need_wakeup(pool))
+ xsk_set_tx_need_wakeup(pool);
+
+ ret = virtnet_xsk_xmit_batch(sq, pool, budget, &stats);
+ switch (ret) {
+ case XSK_XMIT_DONE:
+ /* xsk tx qeueu has been consumed done. should complete napi. */
+ busy = false;
+ break;
+
+ case XSK_XMIT_NO_BUDGET:
+ /* reach the budget limit. should let napi run again. */
+ busy = true;
+ break;
+
+ case XSK_XMIT_DEV_BUSY:
+ /* sq vring is full, should complete napi. wait for tx napi been
+ * triggered by interrupt.
+ */
+ busy = false;
+ break;
+ }
+
+ virtnet_xsk_check_queue(sq);
+
+ u64_stats_update_begin(&sq->stats.syncp);
+ sq->stats.packets += stats.packets;
+ sq->stats.bytes += stats.bytes;
+ sq->stats.kicks += stats.kicks;
+ sq->stats.xdp_tx += stats.xdp_tx;
+ u64_stats_update_end(&sq->stats.syncp);
+
+ return busy;
+}
+
static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queue *rq,
struct xsk_buff_pool *pool, struct net_device *dev)
{
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 08:56:19
On Thu, Feb 02, 2023 at 07:00:41PM +0800, Xuan Zhuo wrote:
At present, we have two long similar logic to perform XDP Progs. And in
the implementation of XSK, we will have this need.
Therefore, this PATCH separates the code of executing XDP, which is
conducive to later maintenance and facilitates subsequent XSK for reuse.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
So you first add a new function then move users over.
This means that it's hard during review to make sure
nothing is lost in translation.
Do the refactoring in a single patch instead.
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-03 09:02:15
On Fri, 3 Feb 2023 03:55:26 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
On Thu, Feb 02, 2023 at 07:00:41PM +0800, Xuan Zhuo wrote:
quoted
At present, we have two long similar logic to perform XDP Progs. And in
the implementation of XSK, we will have this need.
Therefore, this PATCH separates the code of executing XDP, which is
conducive to later maintenance and facilitates subsequent XSK for reuse.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
So you first add a new function then move users over.
This means that it's hard during review to make sure
nothing is lost in translation.
Do the refactoring in a single patch instead.
@@ -2735,6 +2735,56 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num,}EXPORT_SYMBOL_GPL(virtqueue_resize);+/**+*virtqueue_reset-resetthevringofvq
..., detach and recycle all unused buffers
after all this is why we are doing this reset, right?
+ * @_vq: the struct virtqueue we're talking about.
+ * @recycle: callback for recycle the useless buffer
not useless :) unused:
callback to recycle unused buffers
I know we have the same confusion in virtqueue_resize, I will fix
that.
quoted hunk
+ *
+ * Caller must ensure we don't call this with other virtqueue operations
+ * at the same time (except where noted).
+ *
+ * Returns zero or a negative error.
+ * 0: success.
+ * -EBUSY: Failed to sync with device, vq may not work properly
+ * -ENOENT: Transport or device not supported
+ * -EPERM: Operation not permitted
+ */
+int virtqueue_reset(struct virtqueue *_vq,
+ void (*recycle)(struct virtqueue *vq, void *buf))
+{
+ struct vring_virtqueue *vq = to_vvq(_vq);
+ struct virtio_device *vdev = vq->vq.vdev;
+ void *buf;
+ int err;
+
+ if (!vq->we_own_ring)
+ return -EPERM;
+
+ if (!vdev->config->disable_vq_and_reset)
+ return -ENOENT;
+
+ if (!vdev->config->enable_vq_after_reset)
+ return -ENOENT;
+
+ err = vdev->config->disable_vq_and_reset(_vq);
+ if (err)
+ return err;
+
+ while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
+ recycle(_vq, buf);
+
+ if (vq->packed_ring)
+ virtqueue_reinit_packed(vq);
+ else
+ virtqueue_reinit_split(vq);
+
+ if (vdev->config->enable_vq_after_reset(_vq))
+ return -EBUSY;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(virtqueue_reset);
+
/* Only available for split ring */
struct virtqueue *vring_new_virtqueue(unsigned int index,
unsigned int num,
You should only move the headers that are actually needed not
everything.
You mean the "include".
I think it is a simple way to concentrate "Include" into a header file, and
other .c files reference this header file.
Do you agree?
Thanks.
quoted
@@ -44,15 +28,6 @@ module_param(napi_tx, bool, 0644); #define VIRTIO_XDP_TX BIT(0) #define VIRTIO_XDP_REDIR BIT(1)-#define VIRTIO_XDP_FLAG BIT(0)--/* RX packet size EWMA. The average packet size is used to determine the packet- * buffer size when refilling RX rings. As the entire RX ring may be refilled- * at once, the weight is chosen so that the EWMA will be insensitive to short-- * term, transient changes in packet size.- */-DECLARE_EWMA(pkt_len, 0, 64)- #define VIRTNET_DRIVER_VERSION "1.0.0" static const unsigned long guest_offloads[] = {
@@ -125,57 +70,6 @@ static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)-/* Internal representation of a send virtqueue */-struct send_queue {- /* Virtqueue associated with this send _queue */- struct virtqueue *vq;-- /* TX: fragments + linear part + virtio header */- struct scatterlist sg[MAX_SKB_FRAGS + 2];-- /* Name of the send queue: output.$index */- char name[16];-- struct virtnet_sq_stats stats;-- struct napi_struct napi;-- /* Record whether sq is in reset state. */- bool reset;-};--/* Internal representation of a receive virtqueue */-struct receive_queue {- /* Virtqueue associated with this receive_queue */- struct virtqueue *vq;-- struct napi_struct napi;-- struct bpf_prog __rcu *xdp_prog;-- struct virtnet_rq_stats stats;-- /* Chain pages by the private ptr. */- struct page *pages;-- /* Average packet length for mergeable receive buffers. */- struct ewma_pkt_len mrg_avg_pkt_len;-- /* Page frag for packet buffer allocation. */- struct page_frag alloc_frag;-- /* RX: fragments + linear part + virtio header */- struct scatterlist sg[MAX_SKB_FRAGS + 2];-- /* Min single buffer size for mergeable buffers case. */- unsigned int min_buf_len;-- /* Name of this receive queue: input.$index */- char name[16];-- struct xdp_rxq_info xdp_rxq;-};- /* This structure can contain rss message with maximum settings for indirection table and keysize * Note, that default structure that describes RSS configuration virtio_net_rss_config * contains same info but can't handle table values.
@@ -206,90 +100,6 @@ struct control_buf { struct virtio_net_ctrl_rss rss; };-struct virtnet_info {- struct virtio_device *vdev;- struct virtqueue *cvq;- struct net_device *dev;- struct send_queue *sq;- struct receive_queue *rq;- unsigned int status;-- /* Max # of queue pairs supported by the device */- u16 max_queue_pairs;-- /* # of queue pairs currently used by the driver */- u16 curr_queue_pairs;-- /* # of XDP queue pairs currently used by the driver */- u16 xdp_queue_pairs;-- /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */- bool xdp_enabled;-- /* I like... big packets and I cannot lie! */- bool big_packets;-- /* number of sg entries allocated for big packets */- unsigned int big_packets_num_skbfrags;-- /* Host will merge rx buffers for big packets (shake it! shake it!) */- bool mergeable_rx_bufs;-- /* Host supports rss and/or hash report */- bool has_rss;- bool has_rss_hash_report;- u8 rss_key_size;- u16 rss_indir_table_size;- u32 rss_hash_types_supported;- u32 rss_hash_types_saved;-- /* Has control virtqueue */- bool has_cvq;-- /* Host can handle any s/g split between our header and packet data */- bool any_header_sg;-- /* Packet virtio header size */- u8 hdr_len;-- /* Work struct for delayed refilling if we run low on memory. */- struct delayed_work refill;-- /* Is delayed refill enabled? */- bool refill_enabled;-- /* The lock to synchronize the access to refill_enabled */- spinlock_t refill_lock;-- /* Work struct for config space updates */- struct work_struct config_work;-- /* Does the affinity hint is set for virtqueues? */- bool affinity_hint_set;-- /* CPU hotplug instances for online & dead */- struct hlist_node node;- struct hlist_node node_dead;-- struct control_buf *ctrl;-- /* Ethtool settings */- u8 duplex;- u32 speed;-- /* Interrupt coalescing settings */- u32 tx_usecs;- u32 rx_usecs;- u32 tx_max_packets;- u32 rx_max_packets;-- unsigned long guest_offloads;- unsigned long guest_offloads_capable;-- /* failover when STANDBY feature enabled */- struct failover *failover;-};- struct padded_vnet_hdr { struct virtio_net_hdr_v1_hash hdr; /*
@@ -0,0 +1,265 @@+/* SPDX-License-Identifier: GPL-2.0-or-later */++#ifndef __VIRTIO_NET_H__+#define __VIRTIO_NET_H__+#include<linux/netdevice.h>+#include<linux/etherdevice.h>+#include<linux/ethtool.h>+#include<linux/module.h>+#include<linux/virtio.h>+#include<linux/virtio_net.h>+#include<linux/bpf.h>+#include<linux/bpf_trace.h>+#include<linux/scatterlist.h>+#include<linux/if_vlan.h>+#include<linux/slab.h>+#include<linux/cpu.h>+#include<linux/average.h>+#include<linux/filter.h>+#include<linux/kernel.h>+#include<net/route.h>+#include<net/xdp.h>+#include<net/net_failover.h>+#include<net/xdp_sock_drv.h>++#define VIRTIO_XDP_FLAG BIT(0)++structvirtnet_info{+structvirtio_device*vdev;+structvirtqueue*cvq;+structnet_device*dev;+structsend_queue*sq;+structreceive_queue*rq;+unsignedintstatus;++/* Max # of queue pairs supported by the device */+u16max_queue_pairs;++/* # of queue pairs currently used by the driver */+u16curr_queue_pairs;++/* # of XDP queue pairs currently used by the driver */+u16xdp_queue_pairs;++/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */+boolxdp_enabled;++/* I like... big packets and I cannot lie! */+boolbig_packets;++/* number of sg entries allocated for big packets */+unsignedintbig_packets_num_skbfrags;++/* Host will merge rx buffers for big packets (shake it! shake it!) */+boolmergeable_rx_bufs;++/* Host supports rss and/or hash report */+boolhas_rss;+boolhas_rss_hash_report;+u8rss_key_size;+u16rss_indir_table_size;+u32rss_hash_types_supported;+u32rss_hash_types_saved;++/* Has control virtqueue */+boolhas_cvq;++/* Host can handle any s/g split between our header and packet data */+boolany_header_sg;++/* Packet virtio header size */+u8hdr_len;++/* Work struct for delayed refilling if we run low on memory. */+structdelayed_workrefill;++/* Is delayed refill enabled? */+boolrefill_enabled;++/* The lock to synchronize the access to refill_enabled */+spinlock_trefill_lock;++/* Work struct for config space updates */+structwork_structconfig_work;++/* Does the affinity hint is set for virtqueues? */+boolaffinity_hint_set;++/* CPU hotplug instances for online & dead */+structhlist_nodenode;+structhlist_nodenode_dead;++structcontrol_buf*ctrl;++/* Ethtool settings */+u8duplex;+u32speed;++/* Interrupt coalescing settings */+u32tx_usecs;+u32rx_usecs;+u32tx_max_packets;+u32rx_max_packets;++unsignedlongguest_offloads;+unsignedlongguest_offloads_capable;++/* failover when STANDBY feature enabled */+structfailover*failover;+};++/* RX packet size EWMA. The average packet size is used to determine the packet+*buffersizewhenrefillingRXrings.AstheentireRXringmayberefilled+*atonce,theweightischosensothattheEWMAwillbeinsensitivetoshort-+*term,transientchangesinpacketsize.+*/+DECLARE_EWMA(pkt_len,0,64)++structvirtnet_stat_desc{+chardesc[ETH_GSTRING_LEN];+size_toffset;+};++structvirtnet_sq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64xdp_tx;+u64xdp_tx_drops;+u64kicks;+u64tx_timeouts;+};++structvirtnet_rq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64drops;+u64xdp_packets;+u64xdp_tx;+u64xdp_redirects;+u64xdp_drops;+u64kicks;+};++#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)+#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)++/* Internal representation of a send virtqueue */+structsend_queue{+/* Virtqueue associated with this send _queue */+structvirtqueue*vq;++/* TX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Name of the send queue: output.$index */+charname[16];++structvirtnet_sq_statsstats;++structnapi_structnapi;++/* Record whether sq is in reset state. */+boolreset;+};++/* Internal representation of a receive virtqueue */+structreceive_queue{+/* Virtqueue associated with this receive_queue */+structvirtqueue*vq;++structnapi_structnapi;++structbpf_prog__rcu*xdp_prog;++structvirtnet_rq_statsstats;++/* Chain pages by the private ptr. */+structpage*pages;++/* Average packet length for mergeable receive buffers. */+structewma_pkt_lenmrg_avg_pkt_len;++/* Page frag for packet buffer allocation. */+structpage_fragalloc_frag;++/* RX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Min single buffer size for mergeable buffers case. */+unsignedintmin_buf_len;++/* Name of this receive queue: input.$index */+charname[16];++structxdp_rxq_infoxdp_rxq;+};++staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)+{+if(q<(vi->curr_queue_pairs-vi->xdp_queue_pairs))+returnfalse;+elseif(q<vi->curr_queue_pairs)+returntrue;+else+returnfalse;+}++staticinlinevoidvirtnet_return_xdp_frame(structsend_queue*sq,+structxdp_frame*frame)+{+structvirtnet_info*vi=sq->vq->vdev->priv;+dma_addr_t*p_addr,addr;++p_addr=frame->data-sizeof(*p_addr);+addr=*p_addr;++virtio_dma_unmap(&vi->vdev->dev,addr,frame->len,DMA_TO_DEVICE);++xdp_return_frame(frame);+}++staticinlinevoidvirtqueue_napi_schedule(structnapi_struct*napi,+structvirtqueue*vq)+{+if(napi_schedule_prep(napi)){+virtqueue_disable_cb(vq);+__napi_schedule(napi);+}+}++staticinlineboolis_xdp_frame(void*ptr)+{+return(unsignedlong)ptr&VIRTIO_XDP_FLAG;+}++staticstructxdp_frame*ptr_to_xdp(void*ptr)+{+return(structxdp_frame*)((unsignedlong)ptr&~VIRTIO_XDP_FLAG);+}++staticvoid__free_old_xmit(structsend_queue*sq,boolin_napi,+structvirtnet_sq_stats*stats)+{+unsignedintlen;+void*ptr;++while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){+if(!is_xdp_frame(ptr)){+structsk_buff*skb=ptr;++pr_debug("Sent skb %p\n",skb);++stats->bytes+=skb->len;+napi_consume_skb(skb,in_napi);+}else{+structxdp_frame*frame=ptr_to_xdp(ptr);++stats->bytes+=xdp_get_frame_len(frame);+xdp_return_frame(frame);+}+stats->packets++;+}+}+#endif
All these APIs not prefixed with virtnet were ok as internal
static functions. No longer ok in a header.
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:08:07
On Thu, Feb 02, 2023 at 07:00:32PM +0800, Xuan Zhuo wrote:
Added virtio_dma_map() to map DMA addresses for virtual memory in
advance. The purpose of adding this function is to check
vring_use_dma_api() for virtio dma operation and get vdev->dev.parent as
the parameter of dma_map_page().
No this looks like the implementation not the purpose.
I am guessing the purpose is to keep memory mapped
across multiple add/get buf operations right?
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:09:24
On Thu, Feb 02, 2023 at 07:44:07PM +0800, Xuan Zhuo wrote:
On Thu, 2 Feb 2023 06:08:30 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
quoted
On Thu, Feb 02, 2023 at 07:00:25PM +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good.
Great! Any numbers to share?
RESEND. Last mail has some email format error.
ENV: Qemu with vhost.
vhost cpu | Guest APP CPU |Guest Softirq CPU | PPS
-----------------------------|---------------|------------------|------------
xmit by sockperf: 90% | 100% | | 318967
xmit by xsk: 100% | 30% | 33% | 1192064
recv by sockperf: 100% | 68% | 100% | 692288
recv by xsk: 100% | 33% | 43% | 771670
Thanks.
Impressive, thanks a lot for this work!
Pls remember to retest and include up to date numbers on
subsequent versions.
quoted
quoted
mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Please review.
Thanks.
Xuan Zhuo (33):
virtio_ring: virtqueue_add() support premapped
virtio_ring: split: virtqueue_add_split() support premapped
virtio_ring: packed: virtqueue_add_packed() support premapped
virtio_ring: introduce virtqueue_add_outbuf_premapped()
virtio_ring: introduce virtqueue_add_inbuf_premapped()
virtio_ring: introduce virtqueue_reset()
virtio_ring: add api virtio_dma_map() for advance dma
virtio_ring: introduce dma sync api for virtio
xsk: xsk_buff_pool add callback for dma_sync
xsk: support virtio DMA map
virtio_net: rename free_old_xmit_skbs to free_old_xmit
virtio_net: unify the code for recycling the xmit ptr
virtio_net: virtnet_poll_tx support rescheduled
virtio_net: independent directory
virtio_net: move to virtio_net.h
virtio_net: introduce virtnet_xdp_handler() to seprate the logic of
run xdp
virtio_net: receive_small() use virtnet_xdp_handler()
virtio_net: receive_merageable() use virtnet_xdp_handler()
virtio_net: introduce virtnet_tx_reset()
virtio_net: xsk: introduce virtnet_rq_bind_xsk_pool()
virtio_net: xsk: introduce virtnet_xsk_pool_enable()
virtio_net: xsk: introduce xsk disable
virtio_net: xsk: support xsk setup
virtio_net: xsk: stop disable tx napi
virtio_net: xsk: __free_old_xmit distinguishes xsk buffer
virtio_net: virtnet_sq_free_unused_buf() check xsk buffer
virtio_net: virtnet_rq_free_unused_buf() check xsk buffer
net: introduce napi_tx_raise()
virtio_net: xsk: tx: support tx
virtio_net: xsk: tx: support wakeup
virtio_net: xsk: tx: auto wakeup when free old xmit
virtio_net: xsk: rx: introduce add_recvbuf_xsk()
virtio_net: xsk: rx: introduce receive_xsk() to recv xsk buffer
MAINTAINERS | 2 +-
drivers/net/Kconfig | 8 +-
drivers/net/Makefile | 2 +-
drivers/net/virtio/Kconfig | 11 +
drivers/net/virtio/Makefile | 8 +
drivers/net/{virtio_net.c => virtio/main.c} | 564 +++++++-------------
drivers/net/virtio/virtio_net.h | 317 +++++++++++
drivers/net/virtio/xsk.c | 524 ++++++++++++++++++
drivers/net/virtio/xsk.h | 33 ++
drivers/virtio/virtio_ring.c | 376 +++++++++++--
include/linux/netdevice.h | 7 +
include/linux/virtio.h | 29 +
include/net/xsk_buff_pool.h | 6 +
net/core/dev.c | 11 +
net/xdp/xsk_buff_pool.c | 79 ++-
15 files changed, 1541 insertions(+), 436 deletions(-)
create mode 100644 drivers/net/virtio/Kconfig
create mode 100644 drivers/net/virtio/Makefile
rename drivers/net/{virtio_net.c => virtio/main.c} (92%)
create mode 100644 drivers/net/virtio/virtio_net.h
create mode 100644 drivers/net/virtio/xsk.c
create mode 100644 drivers/net/virtio/xsk.h
--
2.32.0.3.g01195cf9f
@@ -2735,6 +2735,56 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num,}EXPORT_SYMBOL_GPL(virtqueue_resize);+/**+*virtqueue_reset-resetthevringofvq
..., detach and recycle all unused buffers
after all this is why we are doing this reset, right?
quoted
+ * @_vq: the struct virtqueue we're talking about.
+ * @recycle: callback for recycle the useless buffer
not useless :) unused:
callback to recycle unused buffers
I agree. Will fix.
Thanks.
I know we have the same confusion in virtqueue_resize, I will fix
that.
quoted
+ *
+ * Caller must ensure we don't call this with other virtqueue operations
+ * at the same time (except where noted).
+ *
+ * Returns zero or a negative error.
+ * 0: success.
+ * -EBUSY: Failed to sync with device, vq may not work properly
+ * -ENOENT: Transport or device not supported
+ * -EPERM: Operation not permitted
+ */
+int virtqueue_reset(struct virtqueue *_vq,
+ void (*recycle)(struct virtqueue *vq, void *buf))
+{
+ struct vring_virtqueue *vq = to_vvq(_vq);
+ struct virtio_device *vdev = vq->vq.vdev;
+ void *buf;
+ int err;
+
+ if (!vq->we_own_ring)
+ return -EPERM;
+
+ if (!vdev->config->disable_vq_and_reset)
+ return -ENOENT;
+
+ if (!vdev->config->enable_vq_after_reset)
+ return -ENOENT;
+
+ err = vdev->config->disable_vq_and_reset(_vq);
+ if (err)
+ return err;
+
+ while ((buf = virtqueue_detach_unused_buf(_vq)) != NULL)
+ recycle(_vq, buf);
+
+ if (vq->packed_ring)
+ virtqueue_reinit_packed(vq);
+ else
+ virtqueue_reinit_split(vq);
+
+ if (vdev->config->enable_vq_after_reset(_vq))
+ return -EBUSY;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(virtqueue_reset);
+
/* Only available for split ring */
struct virtqueue *vring_new_virtqueue(unsigned int index,
unsigned int num,
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-03 09:10:16
On Fri, 3 Feb 2023 04:08:31 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
On Thu, Feb 02, 2023 at 07:44:07PM +0800, Xuan Zhuo wrote:
quoted
On Thu, 2 Feb 2023 06:08:30 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
quoted
On Thu, Feb 02, 2023 at 07:00:25PM +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good.
Great! Any numbers to share?
RESEND. Last mail has some email format error.
ENV: Qemu with vhost.
vhost cpu | Guest APP CPU |Guest Softirq CPU | PPS
-----------------------------|---------------|------------------|------------
xmit by sockperf: 90% | 100% | | 318967
xmit by xsk: 100% | 30% | 33% | 1192064
recv by sockperf: 100% | 68% | 100% | 692288
recv by xsk: 100% | 33% | 43% | 771670
Thanks.
Impressive, thanks a lot for this work!
Pls remember to retest and include up to date numbers on
subsequent versions.
OK.
Thanks.
quoted
quoted
quoted
mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Please review.
Thanks.
Xuan Zhuo (33):
virtio_ring: virtqueue_add() support premapped
virtio_ring: split: virtqueue_add_split() support premapped
virtio_ring: packed: virtqueue_add_packed() support premapped
virtio_ring: introduce virtqueue_add_outbuf_premapped()
virtio_ring: introduce virtqueue_add_inbuf_premapped()
virtio_ring: introduce virtqueue_reset()
virtio_ring: add api virtio_dma_map() for advance dma
virtio_ring: introduce dma sync api for virtio
xsk: xsk_buff_pool add callback for dma_sync
xsk: support virtio DMA map
virtio_net: rename free_old_xmit_skbs to free_old_xmit
virtio_net: unify the code for recycling the xmit ptr
virtio_net: virtnet_poll_tx support rescheduled
virtio_net: independent directory
virtio_net: move to virtio_net.h
virtio_net: introduce virtnet_xdp_handler() to seprate the logic of
run xdp
virtio_net: receive_small() use virtnet_xdp_handler()
virtio_net: receive_merageable() use virtnet_xdp_handler()
virtio_net: introduce virtnet_tx_reset()
virtio_net: xsk: introduce virtnet_rq_bind_xsk_pool()
virtio_net: xsk: introduce virtnet_xsk_pool_enable()
virtio_net: xsk: introduce xsk disable
virtio_net: xsk: support xsk setup
virtio_net: xsk: stop disable tx napi
virtio_net: xsk: __free_old_xmit distinguishes xsk buffer
virtio_net: virtnet_sq_free_unused_buf() check xsk buffer
virtio_net: virtnet_rq_free_unused_buf() check xsk buffer
net: introduce napi_tx_raise()
virtio_net: xsk: tx: support tx
virtio_net: xsk: tx: support wakeup
virtio_net: xsk: tx: auto wakeup when free old xmit
virtio_net: xsk: rx: introduce add_recvbuf_xsk()
virtio_net: xsk: rx: introduce receive_xsk() to recv xsk buffer
MAINTAINERS | 2 +-
drivers/net/Kconfig | 8 +-
drivers/net/Makefile | 2 +-
drivers/net/virtio/Kconfig | 11 +
drivers/net/virtio/Makefile | 8 +
drivers/net/{virtio_net.c => virtio/main.c} | 564 +++++++-------------
drivers/net/virtio/virtio_net.h | 317 +++++++++++
drivers/net/virtio/xsk.c | 524 ++++++++++++++++++
drivers/net/virtio/xsk.h | 33 ++
drivers/virtio/virtio_ring.c | 376 +++++++++++--
include/linux/netdevice.h | 7 +
include/linux/virtio.h | 29 +
include/net/xsk_buff_pool.h | 6 +
net/core/dev.c | 11 +
net/xdp/xsk_buff_pool.c | 79 ++-
15 files changed, 1541 insertions(+), 436 deletions(-)
create mode 100644 drivers/net/virtio/Kconfig
create mode 100644 drivers/net/virtio/Makefile
rename drivers/net/{virtio_net.c => virtio/main.c} (92%)
create mode 100644 drivers/net/virtio/virtio_net.h
create mode 100644 drivers/net/virtio/xsk.c
create mode 100644 drivers/net/virtio/xsk.h
--
2.32.0.3.g01195cf9f
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:10:29
On Fri, Feb 03, 2023 at 09:46:46AM +0100, Maciej Fijalkowski wrote:
On Fri, Feb 03, 2023 at 03:37:32AM -0500, Michael S. Tsirkin wrote:
quoted
On Fri, Feb 03, 2023 at 11:33:31AM +0800, Xuan Zhuo wrote:
quoted
On Thu, 02 Feb 2023 15:41:44 +0100, Paolo Abeni [off-list ref] wrote:
quoted
On Thu, 2023-02-02 at 19:00 +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Thank you for the large effort.
Since this will likely need a few iterations, on next revision please
do split the work in multiple chunks to help the reviewer efforts -
from Documentation/process/maintainer-netdev.rst:
- don't post large series (> 15 patches), break them up
In this case I guess you can split it in 1 (or even 2) pre-req series
and another one for the actual xsk zero copy support.
OK.
I can split patch into multiple parts such as
* virtio core
* xsk
* virtio-net prepare
* virtio-net support xsk zerocopy
However, there is a problem, the virtio core part should enter the VHOST branch
of Michael. Then, should I post follow-up patches to which branch vhost or
next-next?
Thanks.
I personally think 33 patches is still manageable no need to split.
Do try to be careful and track acks and changes: if someone sends an ack
add it in the patch if you change the patch drop the acks,
and logs this fact in the changelog in the cover letter
so people know they need to re-review.
To me some of the patches are too granular but probably this is related to
personal taste.
I agree here. Some unrelated refactoring can also be deferred.
However, I would like to ask to check how this series
affects existing ZC enabled driver(s), since xsk core is touched.
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:18:47
On Thu, Feb 02, 2023 at 07:00:28PM +0800, Xuan Zhuo wrote:
quoted hunk
virtqueue_add_packed() only supports virtual addresses, dma is completed
in virtqueue_add_packed().
In some scenarios (such as the AF_XDP scenario), the memory is allocated
and DMA is completed in advance, so it is necessary for us to support
passing the DMA address to virtqueue_add_packed().
Record this information in desc_state, we can skip unmap based on this
when executing dma unmap.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/virtio/virtio_ring.c | 71 +++++++++++++++++++++++++-----------
1 file changed, 50 insertions(+), 21 deletions(-)
@@ -78,6 +78,7 @@ struct vring_desc_state_packed {structvring_packed_desc*indir_desc;/* Indirect descriptor, if any. */u16num;/* Descriptor list length. */u16last;/* The last desc state in a list. */+boolpremapped;};structvring_desc_extra{
@@ -1485,6 +1506,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, vq->packed.desc_state[id].data = data; vq->packed.desc_state[id].indir_desc = ctx; vq->packed.desc_state[id].last = prev;+ vq->packed.desc_state[id].premapped = premapped; /* * A driver MUST NOT make the first descriptor in the list
@@ -1501,22 +1523,26 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq, return 0; unmap_release:+ vq->packed.avail_used_flags = avail_used_flags;++ if (premapped)+ goto unmap_free;+
This goto branching inside error handling is too much like spaghetti code.
See Documentation/process/coding-style.rst for when goto is ok -
basically exit/error handling. This is not error handling.
Pls find a way to avoid.
quoted hunk
err_idx = i;
i = head;
curr = vq->free_head;
- vq->packed.avail_used_flags = avail_used_flags;
-
for (n = 0; n < total_sg; n++) {
if (i == err_idx)
break;
- vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr]);
+ vring_unmap_extra_packed(vq, &vq->packed.desc_extra[curr], false);
curr = vq->packed.desc_extra[curr].next;
i++;
if (i >= vq->packed.vring.num)
i = 0;
}
+unmap_free:
END_USE(vq);
return -EIO;
}
@@ -1576,8 +1602,10 @@ static void detach_buf_packed(struct vring_virtqueue *vq, struct vring_desc_state_packed *state = NULL; struct vring_packed_desc *desc; unsigned int i, curr;+ bool premapped; state = &vq->packed.desc_state[id];+ premapped = state->premapped; /* Clear data ptr. */ state->data = NULL;
@@ -1590,7 +1618,8 @@ static void detach_buf_packed(struct vring_virtqueue *vq, curr = id; for (i = 0; i < state->num; i++) { vring_unmap_extra_packed(vq,- &vq->packed.desc_extra[curr]);+ &vq->packed.desc_extra[curr],+ premapped); curr = vq->packed.desc_extra[curr].next; } }
@@ -1603,7 +1632,7 @@ static void detach_buf_packed(struct vring_virtqueue *vq, if (!desc) return;- if (vq->use_dma_api) {+ if (vq->use_dma_api && !premapped) { len = vq->packed.desc_extra[id].len; for (i = 0; i < len / sizeof(struct vring_packed_desc); i++)
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:20:55
On Fri, Feb 03, 2023 at 11:33:31AM +0800, Xuan Zhuo wrote:
On Thu, 02 Feb 2023 15:41:44 +0100, Paolo Abeni [off-list ref] wrote:
quoted
On Thu, 2023-02-02 at 19:00 +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Thank you for the large effort.
Since this will likely need a few iterations, on next revision please
do split the work in multiple chunks to help the reviewer efforts -
from Documentation/process/maintainer-netdev.rst:
- don't post large series (> 15 patches), break them up
In this case I guess you can split it in 1 (or even 2) pre-req series
and another one for the actual xsk zero copy support.
OK.
I can split patch into multiple parts such as
* virtio core
* xsk
* virtio-net prepare
* virtio-net support xsk zerocopy
However, there is a problem, the virtio core part should enter the VHOST branch
of Michael. Then, should I post follow-up patches to which branch vhost or
next-next?
Thanks.
Here are some ideas on how to make the patchset smaller
and easier to merge:
- keep everything in virtio_net.c for now. We can split
things out later, but this way your patchset will not
conflict with every since change merged meanwhile.
Also, split up needs to be done carefully with sane
APIs between components, let's maybe not waste time
on that now, do the split-up later.
- you have patches that add APIs then other
patches use them. as long as it's only virtio net just
add and use in a single patch, review is actually easier this way.
- we can try merging pre-requisites earlier, then patchset
size will shrink.
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:26:11
On Thu, Feb 02, 2023 at 07:00:33PM +0800, Xuan Zhuo wrote:
In the process of dma sync, we involved whether virtio uses dma api. On
the other hand, it is also necessary to read vdev->dev.parent. So these
API has been introduced.
you don't need to repeat implementation here.
just list the new APIs and how they will be used
with premapped API.
You should only move the headers that are actually needed not
everything.
You mean the "include".
I think it is a simple way to concentrate "Include" into a header file, and
other .c files reference this header file.
Do you agree?
Thanks.
Not really, one ends up including unnecessary stuff in both C files
making build times longer.
quoted
quoted
@@ -44,15 +28,6 @@ module_param(napi_tx, bool, 0644); #define VIRTIO_XDP_TX BIT(0) #define VIRTIO_XDP_REDIR BIT(1)-#define VIRTIO_XDP_FLAG BIT(0)--/* RX packet size EWMA. The average packet size is used to determine the packet- * buffer size when refilling RX rings. As the entire RX ring may be refilled- * at once, the weight is chosen so that the EWMA will be insensitive to short-- * term, transient changes in packet size.- */-DECLARE_EWMA(pkt_len, 0, 64)- #define VIRTNET_DRIVER_VERSION "1.0.0" static const unsigned long guest_offloads[] = {
@@ -125,57 +70,6 @@ static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = { #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc) #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)-/* Internal representation of a send virtqueue */-struct send_queue {- /* Virtqueue associated with this send _queue */- struct virtqueue *vq;-- /* TX: fragments + linear part + virtio header */- struct scatterlist sg[MAX_SKB_FRAGS + 2];-- /* Name of the send queue: output.$index */- char name[16];-- struct virtnet_sq_stats stats;-- struct napi_struct napi;-- /* Record whether sq is in reset state. */- bool reset;-};--/* Internal representation of a receive virtqueue */-struct receive_queue {- /* Virtqueue associated with this receive_queue */- struct virtqueue *vq;-- struct napi_struct napi;-- struct bpf_prog __rcu *xdp_prog;-- struct virtnet_rq_stats stats;-- /* Chain pages by the private ptr. */- struct page *pages;-- /* Average packet length for mergeable receive buffers. */- struct ewma_pkt_len mrg_avg_pkt_len;-- /* Page frag for packet buffer allocation. */- struct page_frag alloc_frag;-- /* RX: fragments + linear part + virtio header */- struct scatterlist sg[MAX_SKB_FRAGS + 2];-- /* Min single buffer size for mergeable buffers case. */- unsigned int min_buf_len;-- /* Name of this receive queue: input.$index */- char name[16];-- struct xdp_rxq_info xdp_rxq;-};- /* This structure can contain rss message with maximum settings for indirection table and keysize * Note, that default structure that describes RSS configuration virtio_net_rss_config * contains same info but can't handle table values.
@@ -206,90 +100,6 @@ struct control_buf { struct virtio_net_ctrl_rss rss; };-struct virtnet_info {- struct virtio_device *vdev;- struct virtqueue *cvq;- struct net_device *dev;- struct send_queue *sq;- struct receive_queue *rq;- unsigned int status;-- /* Max # of queue pairs supported by the device */- u16 max_queue_pairs;-- /* # of queue pairs currently used by the driver */- u16 curr_queue_pairs;-- /* # of XDP queue pairs currently used by the driver */- u16 xdp_queue_pairs;-- /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */- bool xdp_enabled;-- /* I like... big packets and I cannot lie! */- bool big_packets;-- /* number of sg entries allocated for big packets */- unsigned int big_packets_num_skbfrags;-- /* Host will merge rx buffers for big packets (shake it! shake it!) */- bool mergeable_rx_bufs;-- /* Host supports rss and/or hash report */- bool has_rss;- bool has_rss_hash_report;- u8 rss_key_size;- u16 rss_indir_table_size;- u32 rss_hash_types_supported;- u32 rss_hash_types_saved;-- /* Has control virtqueue */- bool has_cvq;-- /* Host can handle any s/g split between our header and packet data */- bool any_header_sg;-- /* Packet virtio header size */- u8 hdr_len;-- /* Work struct for delayed refilling if we run low on memory. */- struct delayed_work refill;-- /* Is delayed refill enabled? */- bool refill_enabled;-- /* The lock to synchronize the access to refill_enabled */- spinlock_t refill_lock;-- /* Work struct for config space updates */- struct work_struct config_work;-- /* Does the affinity hint is set for virtqueues? */- bool affinity_hint_set;-- /* CPU hotplug instances for online & dead */- struct hlist_node node;- struct hlist_node node_dead;-- struct control_buf *ctrl;-- /* Ethtool settings */- u8 duplex;- u32 speed;-- /* Interrupt coalescing settings */- u32 tx_usecs;- u32 rx_usecs;- u32 tx_max_packets;- u32 rx_max_packets;-- unsigned long guest_offloads;- unsigned long guest_offloads_capable;-- /* failover when STANDBY feature enabled */- struct failover *failover;-};- struct padded_vnet_hdr { struct virtio_net_hdr_v1_hash hdr; /*
@@ -0,0 +1,265 @@+/* SPDX-License-Identifier: GPL-2.0-or-later */++#ifndef __VIRTIO_NET_H__+#define __VIRTIO_NET_H__+#include<linux/netdevice.h>+#include<linux/etherdevice.h>+#include<linux/ethtool.h>+#include<linux/module.h>+#include<linux/virtio.h>+#include<linux/virtio_net.h>+#include<linux/bpf.h>+#include<linux/bpf_trace.h>+#include<linux/scatterlist.h>+#include<linux/if_vlan.h>+#include<linux/slab.h>+#include<linux/cpu.h>+#include<linux/average.h>+#include<linux/filter.h>+#include<linux/kernel.h>+#include<net/route.h>+#include<net/xdp.h>+#include<net/net_failover.h>+#include<net/xdp_sock_drv.h>++#define VIRTIO_XDP_FLAG BIT(0)++structvirtnet_info{+structvirtio_device*vdev;+structvirtqueue*cvq;+structnet_device*dev;+structsend_queue*sq;+structreceive_queue*rq;+unsignedintstatus;++/* Max # of queue pairs supported by the device */+u16max_queue_pairs;++/* # of queue pairs currently used by the driver */+u16curr_queue_pairs;++/* # of XDP queue pairs currently used by the driver */+u16xdp_queue_pairs;++/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */+boolxdp_enabled;++/* I like... big packets and I cannot lie! */+boolbig_packets;++/* number of sg entries allocated for big packets */+unsignedintbig_packets_num_skbfrags;++/* Host will merge rx buffers for big packets (shake it! shake it!) */+boolmergeable_rx_bufs;++/* Host supports rss and/or hash report */+boolhas_rss;+boolhas_rss_hash_report;+u8rss_key_size;+u16rss_indir_table_size;+u32rss_hash_types_supported;+u32rss_hash_types_saved;++/* Has control virtqueue */+boolhas_cvq;++/* Host can handle any s/g split between our header and packet data */+boolany_header_sg;++/* Packet virtio header size */+u8hdr_len;++/* Work struct for delayed refilling if we run low on memory. */+structdelayed_workrefill;++/* Is delayed refill enabled? */+boolrefill_enabled;++/* The lock to synchronize the access to refill_enabled */+spinlock_trefill_lock;++/* Work struct for config space updates */+structwork_structconfig_work;++/* Does the affinity hint is set for virtqueues? */+boolaffinity_hint_set;++/* CPU hotplug instances for online & dead */+structhlist_nodenode;+structhlist_nodenode_dead;++structcontrol_buf*ctrl;++/* Ethtool settings */+u8duplex;+u32speed;++/* Interrupt coalescing settings */+u32tx_usecs;+u32rx_usecs;+u32tx_max_packets;+u32rx_max_packets;++unsignedlongguest_offloads;+unsignedlongguest_offloads_capable;++/* failover when STANDBY feature enabled */+structfailover*failover;+};++/* RX packet size EWMA. The average packet size is used to determine the packet+*buffersizewhenrefillingRXrings.AstheentireRXringmayberefilled+*atonce,theweightischosensothattheEWMAwillbeinsensitivetoshort-+*term,transientchangesinpacketsize.+*/+DECLARE_EWMA(pkt_len,0,64)++structvirtnet_stat_desc{+chardesc[ETH_GSTRING_LEN];+size_toffset;+};++structvirtnet_sq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64xdp_tx;+u64xdp_tx_drops;+u64kicks;+u64tx_timeouts;+};++structvirtnet_rq_stats{+structu64_stats_syncsyncp;+u64packets;+u64bytes;+u64drops;+u64xdp_packets;+u64xdp_tx;+u64xdp_redirects;+u64xdp_drops;+u64kicks;+};++#define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)+#define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)++/* Internal representation of a send virtqueue */+structsend_queue{+/* Virtqueue associated with this send _queue */+structvirtqueue*vq;++/* TX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Name of the send queue: output.$index */+charname[16];++structvirtnet_sq_statsstats;++structnapi_structnapi;++/* Record whether sq is in reset state. */+boolreset;+};++/* Internal representation of a receive virtqueue */+structreceive_queue{+/* Virtqueue associated with this receive_queue */+structvirtqueue*vq;++structnapi_structnapi;++structbpf_prog__rcu*xdp_prog;++structvirtnet_rq_statsstats;++/* Chain pages by the private ptr. */+structpage*pages;++/* Average packet length for mergeable receive buffers. */+structewma_pkt_lenmrg_avg_pkt_len;++/* Page frag for packet buffer allocation. */+structpage_fragalloc_frag;++/* RX: fragments + linear part + virtio header */+structscatterlistsg[MAX_SKB_FRAGS+2];++/* Min single buffer size for mergeable buffers case. */+unsignedintmin_buf_len;++/* Name of this receive queue: input.$index */+charname[16];++structxdp_rxq_infoxdp_rxq;+};++staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)+{+if(q<(vi->curr_queue_pairs-vi->xdp_queue_pairs))+returnfalse;+elseif(q<vi->curr_queue_pairs)+returntrue;+else+returnfalse;+}++staticinlinevoidvirtnet_return_xdp_frame(structsend_queue*sq,+structxdp_frame*frame)+{+structvirtnet_info*vi=sq->vq->vdev->priv;+dma_addr_t*p_addr,addr;++p_addr=frame->data-sizeof(*p_addr);+addr=*p_addr;++virtio_dma_unmap(&vi->vdev->dev,addr,frame->len,DMA_TO_DEVICE);++xdp_return_frame(frame);+}++staticinlinevoidvirtqueue_napi_schedule(structnapi_struct*napi,+structvirtqueue*vq)+{+if(napi_schedule_prep(napi)){+virtqueue_disable_cb(vq);+__napi_schedule(napi);+}+}++staticinlineboolis_xdp_frame(void*ptr)+{+return(unsignedlong)ptr&VIRTIO_XDP_FLAG;+}++staticstructxdp_frame*ptr_to_xdp(void*ptr)+{+return(structxdp_frame*)((unsignedlong)ptr&~VIRTIO_XDP_FLAG);+}++staticvoid__free_old_xmit(structsend_queue*sq,boolin_napi,+structvirtnet_sq_stats*stats)+{+unsignedintlen;+void*ptr;++while((ptr=virtqueue_get_buf(sq->vq,&len))!=NULL){+if(!is_xdp_frame(ptr)){+structsk_buff*skb=ptr;++pr_debug("Sent skb %p\n",skb);++stats->bytes+=skb->len;+napi_consume_skb(skb,in_napi);+}else{+structxdp_frame*frame=ptr_to_xdp(ptr);++stats->bytes+=xdp_get_frame_len(frame);+xdp_return_frame(frame);+}+stats->packets++;+}+}+#endif
All these APIs not prefixed with virtnet were ok as internal
static functions. No longer ok in a header.
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:29:08
On Fri, Feb 03, 2023 at 04:52:35PM +0800, Xuan Zhuo wrote:
On Fri, 3 Feb 2023 03:48:33 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
quoted
On Thu, Feb 02, 2023 at 07:00:45PM +0800, Xuan Zhuo wrote:
quoted
This function is used to bind or unbind xsk pool to virtnet rq.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/Makefile | 2 +-
drivers/net/virtio/main.c | 8 ++---
drivers/net/virtio/virtio_net.h | 16 ++++++++++
drivers/net/virtio/xsk.c | 56 +++++++++++++++++++++++++++++++++
4 files changed, 76 insertions(+), 6 deletions(-)
create mode 100644 drivers/net/virtio/xsk.c
@@ -168,6 +168,12 @@ struct send_queue {/* Record whether sq is in reset state. */boolreset;++struct{+structxsk_buff_pool__rcu*pool;++dma_addr_thdr_dma_address;+}xsk;};/* Internal representation of a receive virtqueue */
@@ -200,6 +206,13 @@ struct receive_queue {charname[16];structxdp_rxq_infoxdp_rxq;++struct{+structxsk_buff_pool__rcu*pool;++/* xdp rxq used by xsk */+structxdp_rxq_infoxdp_rxq;+}xsk;};staticinlineboolis_xdp_raw_buffer_queue(structvirtnet_info*vi,intq)
This static function is unused after this patch, so compiler will
complain. Yes it's just a warning but still not nice.
Otherwise, we need merge some patches, which will increase the difficulty of
review.
Is there a better way to deal with? Remove Static?
Thanks.
In this case review is not made easier because the API does not make
much sense by its own and is undocumented anyway. To review one has to
jump back and forth between multiple patches - that is harder not easier
than a single bigger patch. Others in this thread already commented that
the patches are too small.
From: "Michael S. Tsirkin" <mst@redhat.com> Date: 2023-02-03 09:30:49
On Fri, Feb 03, 2023 at 04:49:16PM +0800, Xuan Zhuo wrote:
On Fri, 3 Feb 2023 03:33:41 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
quoted
On Fri, Feb 03, 2023 at 11:24:42AM +0800, Xuan Zhuo wrote:
quoted
On Thu, 2 Feb 2023 12:25:59 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
quoted
On Thu, Feb 02, 2023 at 07:00:49PM +0800, Xuan Zhuo wrote:
quoted
Since xsk's TX queue is consumed by TX NAPI, if sq is bound to xsk, then
we must stop tx napi from being disabled.
Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
---
drivers/net/virtio/main.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
@@ -2728,8 +2728,15 @@ static int virtnet_set_coalesce(struct net_device *dev,returnret;if(update_napi){-for(i=0;i<vi->max_queue_pairs;i++)+for(i=0;i<vi->max_queue_pairs;i++){+/* xsk xmit depend on the tx napi. So if xsk is active,
depends.
quoted
+ * prevent modifications to tx napi.
+ */
+ if (rtnl_dereference(vi->sq[i].xsk.pool))
+ continue;
+
vi->sq[i].napi.weight = napi_weight;
I don't get it.
changing napi weight does not work then.
why is this ok?
static void skb_xmit_done(struct virtqueue *vq)
{
struct virtnet_info *vi = vq->vdev->priv;
struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
/* Suppress further interrupts. */
virtqueue_disable_cb(vq);
if (napi->weight)
virtqueue_napi_schedule(napi, vq);
else
/* We were probably waiting for more output buffers. */
netif_wake_subqueue(vi->dev, vq2txq(vq));
}
If the weight is 0, tx napi will not be triggered again.
Thanks.
This needs more thought then. First ignoring what user is requesting is
not nice.
Maybe we should return an error.
maybe
quoted
Second what if napi is first disabled and then xsk enabled?
static int virtnet_xsk_pool_enable(struct net_device *dev,
struct xsk_buff_pool *pool,
u16 qid)
{
struct virtnet_info *vi = netdev_priv(dev);
struct receive_queue *rq;
struct send_queue *sq;
int err;
if (qid >= vi->curr_queue_pairs)
return -EINVAL;
sq = &vi->sq[qid];
rq = &vi->rq[qid];
/* xsk zerocopy depend on the tx napi.
*
* All xsk packets are actually consumed and sent out from the xsk tx
* queue under the tx napi mechanism.
*/
-> if (!sq->napi.weight)
return -EPERM;
Thanks.
net/xdp/xsk_buff_pool.c:575: undefined reference to `is_virtio_device'
ld: net/xdp/xsk_buff_pool.c:576: undefined reference to `virtio_dma_sync_signle_range_for_device'
ld: net/xdp/xsk_buff_pool.o: in function `__xp_dma_unmap':
net/xdp/xsk_buff_pool.c:338: undefined reference to `is_virtio_device'
quoted
ld: net/xdp/xsk_buff_pool.c:339: undefined reference to `virtio_dma_unmap'
ld: net/xdp/xsk_buff_pool.o: in function `xp_dma_map':
net/xdp/xsk_buff_pool.c:443: undefined reference to `is_virtio_device'
ld: net/xdp/xsk_buff_pool.c:443: undefined reference to `virtio_dma_sync_signle_range_for_device'
quoted
ld: net/xdp/xsk_buff_pool.c:443: undefined reference to `virtio_dma_sync_signle_range_for_cpu'
ld: net/xdp/xsk_buff_pool.c:458: undefined reference to `virtio_dma_map_page'
ld: net/xdp/xsk_buff_pool.c:461: undefined reference to `virtio_dma_mapping_error'
ld: net/xdp/xsk_buff_pool.c:464: undefined reference to `virtio_dma_need_sync'
ld: net/xdp/xsk_buff_pool.c:457: undefined reference to `is_virtio_device'
From: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Date: 2023-02-06 02:46:56
On Fri, 3 Feb 2023 04:17:59 -0500, "Michael S. Tsirkin" [off-list ref] wrote:
On Fri, Feb 03, 2023 at 11:33:31AM +0800, Xuan Zhuo wrote:
quoted
On Thu, 02 Feb 2023 15:41:44 +0100, Paolo Abeni [off-list ref] wrote:
quoted
On Thu, 2023-02-02 at 19:00 +0800, Xuan Zhuo wrote:
quoted
XDP socket(AF_XDP) is an excellent bypass kernel network framework. The zero
copy feature of xsk (XDP socket) needs to be supported by the driver. The
performance of zero copy is very good. mlx5 and intel ixgbe already support
this feature, This patch set allows virtio-net to support xsk's zerocopy xmit
feature.
Virtio-net did not support per-queue reset, so it was impossible to support XDP
Socket Zerocopy. At present, we have completed the work of Virtio Spec and
Kernel in Per-Queue Reset. It is time for Virtio-Net to complete the support for
the XDP Socket Zerocopy.
Virtio-net can not increase the queue at will, so xsk shares the queue with
kernel.
On the other hand, Virtio-Net does not support generate interrupt manually, so
when we wakeup tx xmit, we used some tips. If the CPU run by TX NAPI last time
is other CPUs, use IPI to wake up NAPI on the remote CPU. If it is also the
local CPU, then we wake up sofrirqd.
Thank you for the large effort.
Since this will likely need a few iterations, on next revision please
do split the work in multiple chunks to help the reviewer efforts -
from Documentation/process/maintainer-netdev.rst:
- don't post large series (> 15 patches), break them up
In this case I guess you can split it in 1 (or even 2) pre-req series
and another one for the actual xsk zero copy support.
OK.
I can split patch into multiple parts such as
* virtio core
* xsk
* virtio-net prepare
* virtio-net support xsk zerocopy
However, there is a problem, the virtio core part should enter the VHOST branch
of Michael. Then, should I post follow-up patches to which branch vhost or
next-next?
Thanks.
Here are some ideas on how to make the patchset smaller
and easier to merge:
- keep everything in virtio_net.c for now. We can split
things out later, but this way your patchset will not
conflict with every since change merged meanwhile.
Also, split up needs to be done carefully with sane
APIs between components, let's maybe not waste time
on that now, do the split-up later.
- you have patches that add APIs then other
patches use them. as long as it's only virtio net just
add and use in a single patch, review is actually easier this way.
I will try to merge #16-#18 and #20-#23.
- we can try merging pre-requisites earlier, then patchset
size will shrink.
Do you mean the patches of virtio core? Should we put these
patches to vhost branch?
Thanks.