vhost packed ring performance optimization
cover vhost packed ring performance optimization
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Uroll burst loop function into more pieces.
Handle descriptors in one cache line simultaneously. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost descriptor update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
Cache memory region structure for fast conversion.
Disable sofware prefetch is hardware can do better.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 40%.
Marvin Liu (14):
vhost: add single packet enqueue function
vhost: add burst enqueue function for packed ring
vhost: add single packet dequeue function
vhost: add burst dequeue function
vhost: rename flush shadow used ring functions
vhost: flush vhost enqueue shadow ring by burst
vhost: add flush function for burst enqueue
vhost: buffer vhost dequeue shadow ring
vhost: split enqueue and dequeue flush functions
vhost: optimize Rx function of packed ring
vhost: add burst and single zero dequeue functions
vhost: optimize Tx function of packed ring
vhost: cache address translation result
vhost: check whether disable software pre-fetch
lib/librte_vhost/Makefile | 6 + lib/librte_vhost/rte_vhost.h | 27 + lib/librte_vhost/vhost.h | 13 + lib/librte_vhost/virtio_net.c | 1094 +++++++++++++++++++++++++++------ 4 files changed, 944 insertions(+), 196 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/14 vhost: add single packet enqueue function
Add vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +94 -0
@@ -774,6 +774,71 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +/* +* Returns -1 on fail, 0 on success +*/ +static inline int +vhost_enqueue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt,struct buf_vector *buf_vec,uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; + +uint16_t avail_idx; +uint16_t max_tries, tries =0 ; + +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; + +uint32_t size = pkt->pkt_len + dev->vhost_hlen; + avail_idx = vq->last_avail_idx; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +uint16_t num_buffers =0 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) { +return -1 ; + } + + len =RTE_MIN (len, size); + + size -= len; + + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + + *nr_descs += desc_count; + num_buffers +=1 ; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, + buf_vec, nr_vec, + num_buffers) <0 ) { +return 0 ; + } + +return 0 ; +} + + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -831,6 +896,35 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return 0 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + + vq->last_avail_idx += nr_descs; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 1 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
v2 cover vhost packed ring performance optimization
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Uroll burst loop function into more pieces.
Handle descriptors in one cache line simultaneously. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
Cache memory region structure for fast conversion.
Disable sofware prefetch is hardware can do better.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 40%.
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (16):
vhost: add single packet enqueue function
vhost: unify unroll pragma parameter
vhost: add burst enqueue function for packed ring
vhost: add single packet dequeue function
vhost: add burst dequeue function
vhost: rename flush shadow used ring functions
vhost: flush vhost enqueue shadow ring by burst
vhost: add flush function for burst enqueue
vhost: buffer vhost dequeue shadow ring
vhost: split enqueue and dequeue flush functions
vhost: optimize enqueue function of packed ring
vhost: add burst and single zero dequeue functions
vhost: optimize dequeue function of packed ring
vhost: cache address translation result
vhost: check whether disable software pre-fetch
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 24 + lib/librte_vhost/rte_vhost.h | 27 + lib/librte_vhost/vhost.h | 33 + lib/librte_vhost/virtio_net.c | 1071 +++++++++++++++++++++++++++------ 4 files changed, 960 insertions(+), 195 deletions(-)
Signature · 1 line
2.17.1
PATCH v2 1/16 vhost: add single packet enqueue function
Add vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +93 -0
@@ -774,6 +774,70 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +/* +* Returns -1 on fail, 0 on success +*/ +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt,struct buf_vector *buf_vec,uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; + +uint16_t avail_idx; +uint16_t max_tries, tries =0 ; + +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; + +uint32_t size = pkt->pkt_len + dev->vhost_hlen; + avail_idx = vq->last_avail_idx; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +uint16_t num_buffers =0 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) { +return -1 ; + } + + len =RTE_MIN (len, size); + + size -= len; + + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + + *nr_descs += desc_count; + num_buffers +=1 ; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, + buf_vec, nr_vec, + num_buffers) <0 ) { +return 0 ; + } + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -831,6 +895,35 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + + vq->last_avail_idx += nr_descs; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v2 01/16] vhost: add single packet enqueue function
Hi Marvin,
Quoted 74 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Marvin Liu
Sent: Friday, September 20, 2019 12:36 AM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 01/16] vhost: add single packet enqueue functionAdd vhost enqueue function for single packet and meanwhile left space for flush used ring function.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/virtio_net.c +49
-0@@ -774,6 +774,70 @@ copy_mbuf_to_desc(struct virtio_net *dev, structvhost_virtqueue *vq, return error; } +/* +* Returns -1 on fail, 0 on success +*/ +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt,struct buf_vector *buf_vec,uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; + +uint16_t avail_idx; +uint16_t max_tries, tries =0 ; + +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; + +uint32_t size = pkt->pkt_len + dev->vhost_hlen; + avail_idx = vq->last_avail_idx; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +uint16_t num_buffers =0 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) { +return -1 ; + } + + len =RTE_MIN (len, size); + + size -= len; + + avail_idx += desc_count; +if (avail_idx >= vq->size)
lib/librte_vhost/virtio_net.c copy_mbuf_to_desc(struct virtio_net *dev, struct · line 838
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+ avail_idx -= vq->size; + + *nr_descs += desc_count; + num_buffers +=1 ; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, + buf_vec, nr_vec, + num_buffers) <0 ) { +return 0 ;
Why return 0, which means success, while "copy_mbuf_to_desc" encounters some problems and failed? /Gavin
Quoted 49 lines
lib/librte_vhost/virtio_net.c +5
-0+ } + +return 0 ; +} +static
__rte_noinlineuint32_t
virtio_dev_rx_split(structvirtio_net*dev, structvhost_virtqueue*vq,
structrte_mbuf**pkts,uint32_tcount)lib/librte_vhost/virtio_net.c +17
-0@@ -831,6 +895,35 @@ virtio_dev_rx_split(struct virtio_net *dev, structvhost_virtqueue *vq, return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n", + dev->vid); +return -1 ; + } + + VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | endindex %d\n",
lib/librte_vhost/virtio_net.c +12
-1+ dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + + vq->last_avail_idx += nr_descs; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count) --
Quoted 1 line
2.17.1
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Re: [dpdk-dev] [PATCH v2 01/16] vhost: add single packet enqueue function
Quoted 93 lines
-----Original Message-----
From: Gavin Hu (Arm Technology China) [mailto:[email protected]]
Sent: Monday, September 23, 2019 5:09 PM
To: Liu, Yong <[email protected]>; [email protected]; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>
Cc: [email protected]
Subject: RE: [dpdk-dev] [PATCH v2 01/16] vhost: add single packet enqueue functionHi Marvin,
Quoted 9 lines
-----Original Message-----
From: dev <[email protected]> On Behalf Of Marvin Liu
Sent: Friday, September 20, 2019 12:36 AM
To: [email protected]; [email protected]; [email protected]
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [dpdk-dev] [PATCH v2 01/16] vhost: add single packet enqueue functionReview
Quoted 6 lines
Add vhost enqueue function for single packet and meanwhile left space for flush used ring function.
Signed-off-by Marvin Liu <[email protected]>(quoted diff) +0
-0
b/lib/librte_vhost/virtio_net.cQuoted 64 lines
index
5b85b832d..2b5c47145100644lib/librte_vhost/virtio_net.c +54
-0@@ -774,6 +774,70 @@ copy_mbuf_to_desc(struct virtio_net *dev, structvhost_virtqueue *vq, return error; } +/* +* Returns -1 on fail, 0 on success +*/ +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt,struct buf_vector *buf_vec,uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; + +uint16_t avail_idx; +uint16_t max_tries, tries =0 ; + +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; + +uint32_t size = pkt->pkt_len + dev->vhost_hlen; + avail_idx = vq->last_avail_idx; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +uint16_t num_buffers =0 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) { +return -1 ; + } + + len =RTE_MIN (len, size); + + size -= len; + + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + + *nr_descs += desc_count; + num_buffers +=1 ; + }
Quoted 8 lines
lib/librte_vhost/virtio_net.c copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 835
Quoted 5 lines
lib/librte_vhost/virtio_net.c +5
-0+ +if (copy_mbuf_to_desc (dev, vq, pkt, + buf_vec, nr_vec, + num_buffers) <0 ) { +return 0 ;Why return 0, which means success, while "
copy_mbuf_to_desc" encounters some problems and failed?
/Gavin
Gavin,
Thanks for notice this typo issue. Here should return negative value -1.
Regards,
Marvin
Quoted 57 lines
Quoted 9 lines
lib/librte_vhost/virtio_net.c +5
-0+ } + +return 0 ; +} +static
__rte_noinlineuint32_t
virtio_dev_rx_split(structvirtio_net*dev, structvhost_virtqueue*vq,
structrte_mbuf**pkts,uint32_tcount)lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct · line 900
Quoted 6 lines
lib/librte_vhost/virtio_net.c +2
-0@@ -831,6 +895,35 @@ virtio_dev_rx_split(struct virtio_net *dev, structvhost_virtqueue *vq, return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed(struct virtio_net *dev, struct
vhost_virtqueueQuoted 34 lines
lib/librte_vhost/virtio_net.c +15
-0*vq, + struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n", + dev->vid); +return -1 ; + } + + VHOST_LOG_DEBUG(VHOST_DATA, "(%d) current index %d | endindex %d\n",
lib/librte_vhost/virtio_net.c +12
-1+ dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + + vq->last_avail_idx += nr_descs; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count) --Quoted 1 line
2.17.1
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
v3 cover vhost packed ring performance optimization
In reply to Marvin LiuPacked ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
Cache memory region structure for fast conversion.
Disable sofware prefetch if hardware can do better.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 40%.
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimization
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (15):
vhost: add single packet enqueue function
vhost: unify unroll pragma parameter
vhost: add batch enqueue function for packed ring
vhost: add single packet dequeue function
vhost: add batch dequeue function
vhost: flush vhost enqueue shadow ring by batch
vhost: add flush function for batch enqueue
vhost: buffer vhost dequeue shadow ring
vhost: split enqueue and dequeue flush functions
vhost: optimize enqueue function of packed ring
vhost: add batch and single zero dequeue functions
vhost: optimize dequeue function of packed ring
vhost: cache address translation result
vhost: check whether disable software pre-fetch
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 24 + lib/librte_vhost/rte_vhost.h | 27 + lib/librte_vhost/vhost.h | 33 ++ lib/librte_vhost/virtio_net.c | 994 +++++++++++++++++++++++++++------- 4 files changed, 895 insertions(+), 183 deletions(-)
Signature · 1 line
2.17.1
PATCH v3 1/15 vhost: add single packet enqueue function
Add vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +82 -0
@@ -774,6 +774,58 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt,struct buf_vector *buf_vec,uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; +uint16_t avail_idx = vq->last_avail_idx; +uint16_t max_tries, tries =0 ; +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; +uint32_t size = pkt->pkt_len + dev->vhost_hlen; +uint16_t num_buffers =0 ; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) +return -1 ; + + len =RTE_MIN (len, size); + size -= len; + + num_buffers +=1 ; + + *nr_descs += desc_count; + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 ) +return -1 ; + +return 0 ; +} + + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -831,6 +883,36 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + + vq->last_avail_idx += nr_descs; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
PATCH v3 2/15 vhost: unify unroll pragma parameter
In reply to Marvin LiuAdd macro for unifying Clang/ICC/GCC unroll pragma format. Batch functions were contained of several small loops which optimized by compiler’s loop unrolling pragma.
lib/librte_vhost/Makefile +18 -0
@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endif
lib/librte_vhost/vhost.h +18 -0
@@ -39,6 +39,24 @@#define VHOST_LOG_CACHE_NR 32 +#ifdef SUPPORT_GCC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "GCC unroll 4" +#endif + +#ifdef SUPPORT_CLANG_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll 4" +#endif + +#ifdef SUPPORT_ICC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll (4)" +#endif + +#ifdef UNROLL_PRAGMA_PARAM +#define UNROLL_PRAGMA (param) _Pragma(param) +#else +#define UNROLL_PRAGMA (param) do {} while (0); +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v3 02/15] vhost: unify unroll pragma parameter
Quoted 25 lines · Marvin Liu
Add macro for unifying Clang/ICC/GCC unroll pragma format. Batch functions were contained of several small loops which optimized by compiler’s loop unrolling pragma.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/Makefile +11
-0@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif
lib/librte_vhost/Makefile CFLAGS += -I vhost_user · line 39
Quoted 10 lines
lib/librte_vhost/Makefile +7
-0+ +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endif
You need to add meson support as well.
PATCH v3 3/15 vhost: add batch enqueue function for packed ring
In reply to Marvin LiuBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function not support chained mbufs, single packet enqueue function will handle it.
lib/librte_vhost/vhost.h +4 -0
@@ -39,6 +39,10 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) +#ifdef SUPPORT_GCC_UNROLL_PRAGMA #define UNROLL_PRAGMA_PARAM "GCC unroll 4"#endif
lib/librte_vhost/virtio_net.c +80 -0
86 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v3 03/15] vhost: add batch enqueue function for packed ring
Quoted 35 lines · Marvin Liu
Batch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function not support chained mbufs, single packet enqueue function will handle it.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +4
-0@@ -39,6 +39,10 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) +#ifdef SUPPORT_GCC_UNROLL_PRAGMA #define UNROLL_PRAGMA_PARAM "GCC unroll 4"#endif lib/librte_vhost/virtio_net.c +5
-0@@ -883,6 +883,86 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter;
lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 900
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ;
Does this really generate better code than just "avail_idx < PACKED_BATCH_SIZE"? and+jne vs cmp+jbe.
Quoted 69 lines
lib/librte_vhost/virtio_net.c +65
-0+ +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + lens[i] = descs[avail_idx + i].len; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + + vq->last_avail_idx +=PACKED_BATCH_SIZE ; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_tvirtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *pkt)
Re: [dpdk-dev] [PATCH v3 03/15] vhost: add batch enqueue function for packed ring
Quoted 48 lines
-----Original Message-----
From: Mattias Rönnblom [mailto:[email protected]]
Sent: Thursday, September 26, 2019 3:31 AM
To: Liu, Yong <[email protected]>; [email protected]; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v3 03/15] vhost: add batch enqueue function for packed ringReview
Quoted 23 lines · Marvin Liu
Batch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function not support chained mbufs, single packet enqueue function will handle it.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +4
-0@@ -39,6 +39,10 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) +#ifdef SUPPORT_GCC_UNROLL_PRAGMA #define UNROLL_PRAGMA_PARAM "GCC unroll 4"#endif (quoted diff) +0
-0
b/lib/librte_vhost/virtio_net.clib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct
Quoted 4 lines
index
520c4c6a8..5e08f7d9b100644lib/librte_vhost/virtio_net.c +0
-0@@ -883,6 +883,86 @@ virtio_dev_rx_split(struct virtio_net *dev, struct
vhost_virtqueue*vq,lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
Quoted 5 lines
lib/librte_vhost/virtio_net.c +2
-0return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed(struct virtio_net *dev, struct
vhost_virtqueue*vq,
Quoted 16 lines
lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 900
Quoted 13 lines
lib/librte_vhost/virtio_net.c +13
-0+ struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ;Does this really generate better code than just "
avail_idx<PACKED_BATCH_SIZE"? and+jne vs cmp+jbe.
Hi Mattias,
This comparison is to check whether descriptor location is cache aligned.
In x86 cache line size is 64 bytes, so here mask is 0x3. This check will be and + test + je which is very simple. Most of times the cost of execution will be eliminated as the result can be predicted.
Thanks,
Marvin
Quoted 71 lines
Quoted 58 lines
lib/librte_vhost/virtio_net.c +57
-0+ +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + lens[i] = descs[avail_idx + i].len; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + + vq->last_avail_idx +=PACKED_BATCH_SIZE ; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) {lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,
Quoted 10 lines
lib/librte_vhost/virtio_net.c +8
-0+rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_t virtio_dev_rx_single_packed(struct virtio_net *dev, struct
vhost_virtqueue*vq,Quoted 2 lines
lib/librte_vhost/virtio_net.c +0
-0struct rte_mbuf *pkt)
PATCH v3 4/15 vhost: add single packet dequeue function
In reply to Marvin LiuAdd vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
lib/librte_vhost/virtio_net.c +54 -0
@@ -1571,6 +1571,60 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_dequeue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t *buf_id, +uint16_t *desc_count) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t dummy_len; +uint16_t nr_vec =0 ; +int err; + +if (unlikely (fill_vec_buf_packed (dev, vq, + vq->last_avail_idx, desc_count, + buf_vec, &nr_vec, + buf_id, &dummy_len, +VHOST_ACCESS_RO ) <0 )) +return -1 ; + + *pkts =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (*pkts ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA , +"Failed to allocate memory for mbuf.\n" ); +return -1 ; + } + + err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, *pkts, + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + +return 0 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts) +{ + +uint16_t buf_id, desc_count; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + vq->last_avail_idx += desc_count; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v3 5/15 vhost: add batch dequeue function
In reply to Marvin LiuAdd batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
lib/librte_vhost/vhost.h +2 -0
@@ -61,6 +61,8 @@#define UNROLL_PRAGMA (param) do {} while (0);#endif +#define PACKED_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | VRING_DESC_F_INDIRECT) +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
lib/librte_vhost/virtio_net.c +113 -0
119 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 6/15 vhost: flush vhost enqueue shadow ring by batch
In reply to Marvin LiuBuffer vhost enqueue shadow ring update, flush shadow ring until buffered descriptors number exceed one batch. Thus virtio can receive packets at a faster frequency.
lib/librte_vhost/vhost.h +1 -0
@@ -163,6 +163,7 @@ struct vhost_virtqueue {struct vring_used_elem_packed *shadow_used_packed; };uint16_t shadow_used_idx; +uint16_t enqueue_shadow_count;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +43 -0
73 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 7/15 vhost: add flush function for batch enqueue
In reply to Marvin LiuFlush used flags when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_RX_USED_FLAG (0ULL | VRING_DESC_F_AVAIL | VRING_DESC_F_USED \ + | VRING_DESC_F_WRITE) +#define PACKED_RX_USED_WRAP_FLAG (VRING_DESC_F_WRITE)#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
lib/librte_vhost/virtio_net.c +50 -0
68 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v3 07/15] vhost: add flush function for batch enqueue
Quoted 99 lines
-----Original Message-----
From: Marvin Liu <[email protected]>
Sent: Thursday, September 26, 2019 1:13 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; Gavin Hu (Arm Technology China) <[email protected]>
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [PATCH v3 07/15] vhost: add flush function for batch enqueueFlush used flags when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +3
-0@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_RX_USED_FLAG (0ULL | VRING_DESC_F_AVAIL | VRING_DESC_F_USED \ + | VRING_DESC_F_WRITE) +#define PACKED_RX_USED_WRAP_FLAG (VRING_DESC_F_WRITE)#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define
PACKED_BATCH_MASK(PACKED_BATCH_SIZE- 1)lib/librte_vhost/virtio_net.c +43
-0@@ -169,6 +169,49 @@ update_shadow_used_ring_packed(struct vhost_virtqueue *vq, vq->shadow_used_packed[i].count = count; } +static __rte_always_inline void +flush_used_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint64_t *lens,uint16_t *ids,uint16_t flags) +{ +uint16_t i; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { + vq->desc_packed[vq->last_used_idx + i].id = ids[i]; + vq->desc_packed[vq->last_used_idx + i].len = lens[i]; + } + +rte_smp_wmb (); +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + vq->desc_packed[vq->last_used_idx + i].flags = flags; + +vhost_log_cache_used_vring (dev, vq, vq->last_used_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc ) * +PACKED_BATCH_SIZE ); +vhost_log_cache_sync (dev, vq); + + vq->last_used_idx +=PACKED_BATCH_SIZE ; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + +static __rte_always_inline void +flush_enqueue_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint64_t *lens,uint16_t *ids) +{ +uint16_t flags =0 ; + +if (vq->used_wrap_counter) + flags =PACKED_RX_USED_FLAG ; +else + flags =PACKED_RX_USED_WRAP_FLAG ; +flush_used_batch_packed (dev, vq, lens, ids, flags); +} +static
__rte_always_inlinevoid update_enqueue_shadow_used_ring_packed(structvhost_virtqueue*vq,lib/librte_vhost/virtio_net.c +1
-0uint16_t desc_idx, uint32_t len, uint16_t count)@@ -937,6 +980,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,struct vhost_virtqueue *vq, struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE];uint32_t buf_offset = dev->vhost_hlen;uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ];uint16_t i;lib/librte_vhost/virtio_net.c +0
-0if (unlikely (avail_idx &PACKED_BATCH_MASK ))@@ -1003,6 +1047,12 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, pkts[i]->pkt_len);
lib/librte_vhost/virtio_net.c virtio_dev_rx_batch_packed(struct virtio_net · line 1058
Quoted 10 lines
lib/librte_vhost/virtio_net.c +6
-0} +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + ids[i] = descs[avail_idx + i].id; + +flush_enqueue_batch_packed (dev, vq, lens, ids); +return 0 ; }
Quoted 2 lines
--
2.17.1
PATCH v3 8/15 vhost: buffer vhost dequeue shadow ring
In reply to Marvin LiuBuffer used ring updates as many as possible in vhost dequeue function for coordinating with virtio driver. For supporting buffer, shadow used ring element should contain descriptor index and its wrap counter. First shadowed ring index is recorded for calculating buffered number.
lib/librte_vhost/vhost.h +5 -0
@@ -42,6 +42,8 @@#define PACKED_RX_USED_FLAG (0ULL | VRING_DESC_F_AVAIL | VRING_DESC_F_USED \ | VRING_DESC_F_WRITE)#define PACKED_RX_USED_WRAP_FLAG (VRING_DESC_F_WRITE) +#define PACKED_TX_USED_FLAG (0ULL | VRING_DESC_F_AVAIL | VRING_DESC_F_USED) +#define PACKED_TX_USED_WRAP_FLAG (0x0)#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
@@ -110,9 +112,11 @@ struct log_cache_entry { };struct vring_used_elem_packed { +uint16_t used_idx;uint16_t id;uint32_t len;uint32_t count; +uint16_t used_wrap_counter; };/**
@@ -167,6 +171,7 @@ struct vhost_virtqueue { };uint16_t shadow_used_idx;uint16_t enqueue_shadow_count; +uint16_t dequeue_shadow_head;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +37 -0
@@ -230,6 +230,41 @@ update_enqueue_shadow_used_ring_packed(struct vhost_virtqueue *vq, vq->enqueue_shadow_count += count; } +static __rte_always_inline void +update_dequeue_shadow_used_ring_packed (struct vhost_virtqueue *vq, +uint16_t buf_id,uint16_t count) +{ +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + + vq->shadow_used_packed[0 ].id = buf_id; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count = count; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + + vq->shadow_used_idx =1 ; + }else { + vq->desc_packed[vq->last_used_idx].id = buf_id; + vq->desc_packed[vq->last_used_idx].len =0 ; + +if (vq->used_wrap_counter) + vq->desc_packed[vq->last_used_idx].flags = +PACKED_TX_USED_FLAG ; +else + vq->desc_packed[vq->last_used_idx].flags = +PACKED_TX_USED_WRAP_FLAG ; + } + + vq->last_used_idx += count; + +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1822,6 +1857,8 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, &desc_count))return -1 ; +update_dequeue_shadow_used_ring_packed (vq, buf_id, desc_count); + vq->last_avail_idx += desc_count;if (vq->last_avail_idx >= vq->size) { vq->last_avail_idx -= vq->size;
Signature · 1 line
2.17.1
PATCH v3 9/15 vhost: split enqueue and dequeue flush functions
In reply to Marvin LiuVhost enqueue descriptors are updated by batch number, while vhost dequeue descriptors are buffered. Meanwhile in dequeue function only first descriptor is buffered. Due to these differences, split vhost enqueue and dequeue flush functions.
lib/librte_vhost/virtio_net.c +93 -6
@@ -92,8 +92,8 @@ update_shadow_used_ring_split(struct vhost_virtqueue *vq, } static __rte_always_inline void -flush_shadow_used_ring_packed (struct virtio_net *dev, -struct vhost_virtqueue *vq) +flush_enqueue_shadow_used_ring_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq) {int i;uint16_t used_idx = vq->last_used_idx;
@@ -158,6 +158,32 @@ flush_shadow_used_ring_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } +static __rte_always_inline void +flush_dequeue_shadow_used_ring_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq) +{ +uint16_t head_idx = vq->dequeue_shadow_head; +uint16_t head_flags; +struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0 ]; + +if (used_elem->used_wrap_counter) + head_flags =PACKED_TX_USED_FLAG ; +else + head_flags =PACKED_TX_USED_WRAP_FLAG ; + + vq->desc_packed[head_idx].id = used_elem->id; + +rte_smp_wmb (); + vq->desc_packed[head_idx].flags = head_flags; + +vhost_log_cache_used_vring (dev, vq, head_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc )); + + vq->shadow_used_idx =0 ; +vhost_log_cache_sync (dev, vq); +} + static __rte_always_inline voidupdate_shadow_used_ring_packed (struct vhost_virtqueue *vq,uint16_t desc_idx,uint32_t len,uint16_t count)
@@ -199,6 +225,47 @@ flush_used_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, } } +static __rte_always_inline void +update_dequeue_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t *ids) +{ +uint16_t flags =0 ; +uint16_t i; + +if (vq->used_wrap_counter) + flags =PACKED_TX_USED_FLAG ; +else + flags =PACKED_TX_USED_WRAP_FLAG ; + +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + vq->shadow_used_packed[0 ].id = ids[0 ]; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 1; i < PACKED_BATCH_SIZE; i++) + vq->desc_packed[vq->last_used_idx + i].id = ids[i]; +rte_smp_wmb (); +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 1; i < PACKED_BATCH_SIZE; i++) + vq->desc_packed[vq->last_used_idx + i].flags = flags; + + vq->shadow_used_idx =1 ; + vq->last_used_idx +=PACKED_BATCH_SIZE ; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } + }else { +uint64_t lens[PACKED_BATCH_SIZE ] = {0 }; +flush_used_batch_packed (dev, vq, lens, ids, flags); + } +} + static __rte_always_inline voidflush_enqueue_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t *lens,uint16_t *ids)
@@ -306,11 +373,29 @@ flush_enqueue_packed(struct virtio_net *dev,if (vq->enqueue_shadow_count >=PACKED_BATCH_SIZE ) {do_data_copy_enqueue (dev, vq); -flush_shadow_used_ring_packed (dev, vq); +flush_enqueue_shadow_used_ring_packed (dev, vq); } } } +static __rte_unused void +flush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) +{ +if (!vq->shadow_used_idx) +return ; + +int16_t shadow_count = vq->last_used_idx - vq->dequeue_shadow_head; +if (shadow_count <=0 ) + shadow_count += vq->size; + +/* buffer used descs as many as possible when doing dequeue */ +if ((uint16_t )shadow_count >= (vq->size -MAX_PKT_BURST )) { +do_data_copy_dequeue (vq); +flush_dequeue_shadow_used_ring_packed (dev, vq); +vhost_vring_call_packed (dev, vq); + } +} +/* avoid write operation when necessary, to lessen cache issues */ #define ASSIGN_UNLESS_EQUAL (var, val) do { \ if ((var) != (val)) \
@@ -1165,7 +1250,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,do_data_copy_enqueue (dev, vq);if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); +flush_enqueue_shadow_used_ring_packed (dev, vq);vhost_vring_call_packed (dev, vq); }
@@ -1796,6 +1881,8 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, pkts[i]->pkt_len); } +update_dequeue_batch_packed (dev, vq, ids); +if (virtio_net_with_host_offload (dev)) {UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) {
@@ -1896,7 +1983,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, }if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); +flush_dequeue_shadow_used_ring_packed (dev, vq);vhost_vring_call_packed (dev, vq); } }
@@ -1975,7 +2062,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (i < count)) vq->shadow_used_idx = i;if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); +flush_dequeue_shadow_used_ring_packed (dev, vq);vhost_vring_call_packed (dev, vq); } }
Signature · 1 line
2.17.1
PATCH v3 10/15 vhost: optimize enqueue function of packed ring
In reply to Marvin LiuOptimize vhost device Tx datapath by separate functions. Packets can be filled into one descriptor will be handled by batch and others will be handled one by one as before.
lib/librte_vhost/virtio_net.c +22 -96
@@ -753,64 +753,6 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t size,struct buf_vector *buf_vec, -uint16_t *nr_vec,uint16_t *num_buffers, -uint16_t *nr_descs) -{ -uint16_t avail_idx; -uint16_t vec_idx =0 ; -uint16_t max_tries, tries =0 ; - -uint16_t buf_id =0 ; -uint32_t len =0 ; -uint16_t desc_count; - - *num_buffers =0 ; - avail_idx = vq->last_avail_idx; - -if (rxvq_is_mergeable (dev)) - max_tries = vq->size -1 ; -else - max_tries =1 ; - -while (size >0 ) { -/* -* if we tried all available ring items, and still -* can't get enough buf, it means something abnormal -* happened. -*/ -if (unlikely (++tries > max_tries)) -return -1 ; - -if (unlikely (fill_vec_buf_packed (dev, vq, - avail_idx, &desc_count, - buf_vec, &vec_idx, - &buf_id, &len, -VHOST_ACCESS_RW ) <0 )) -return -1 ; - - len =RTE_MIN (len, size); -update_shadow_used_ring_packed (vq, buf_id, len, desc_count); - size -= len; - - avail_idx += desc_count; -if (avail_idx >= vq->size) - avail_idx -= vq->size; - - *nr_descs += desc_count; - *num_buffers +=1 ; - } - - *nr_vec = vec_idx; - -return 0 ; -} - static __rte_noinline void copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, struct buf_vector *buf_vec,
@@ -1089,7 +1031,7 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_rx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts) {
@@ -1176,7 +1118,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -static __rte_unused int16_t +static __rte_always_inline int16_tvirtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *pkt) {
@@ -1205,52 +1147,36 @@ virtio_dev_rx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } - static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count) {uint32_t pkt_idx =0 ; -uint16_t num_buffers; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; -uint16_t nr_vec =0 ; -uint16_t nr_descs =0 ; - -if (unlikely (reserve_avail_buf_packed (dev, vq, - pkt_len, buf_vec, &nr_vec, - &num_buffers, &nr_descs) <0 )) { -VHOST_LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); - vq->shadow_used_idx -= num_buffers; -break ; +uint32_t remained = count; + +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx & + (vq->size -1 )]); +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_rx_batch_packed (dev, vq, pkts)) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, vq->last_avail_idx, - vq->last_avail_idx + num_buffers); - -if (copy_mbuf_to_desc (dev, vq, pkts[pkt_idx], - buf_vec, nr_vec, - num_buffers) <0 ) { - vq->shadow_used_idx -= num_buffers; +if (virtio_dev_rx_single_packed (dev, vq, pkts[pkt_idx]))break ; - } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } - } - -do_data_copy_enqueue (dev, vq); + pkt_idx++; + remained--; + }while (pkt_idx < count); -if (likely (vq->shadow_used_idx)) { -flush_enqueue_shadow_used_ring_packed (dev, vq); +if (pkt_idx) { +if (vq->shadow_used_idx) { +do_data_copy_enqueue (dev, vq); +flush_enqueue_shadow_used_ring_packed (dev, vq); + }vhost_vring_call_packed (dev, vq); }
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v3 10/15] vhost: optimize enqueue function of packed ring
Quoted 16 lines · Marvin Liu
static
__rte_noinlineuint32_t
virtio_dev_rx_packed(structvirtio_net*dev, structvhost_virtqueue*vq,lib/librte_vhost/virtio_net.c +0
-11struct rte_mbuf **pkts, uint32_t count) {uint32_t pkt_idx =0 ; -uint16_t num_buffers; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; - -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; -uint16_t nr_vec =0 ; -uint16_t nr_descs =0 ; - -if (unlikely (reserve_avail_buf_packed (dev, vq, - pkt_len, buf_vec, &nr_vec, - &num_buffers, &nr_descs) <0 )) {
lib/librte_vhost/virtio_net.c virtio_dev_rx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 1159
Quoted 10 lines
lib/librte_vhost/virtio_net.c +5
-5-VHOST_LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); - vq->shadow_used_idx -= num_buffers; -break ; +uint32_t remained = count; + + do { + rte_prefetch0(&vq->desc_packed[vq->last_avail_idx & + (vq->size - 1)]);
You can't assume packed ring size is a power of 2.
PATCH v3 11/15 vhost: add batch and single zero dequeue functions
In reply to Marvin LiuOptimize vhost zero copy dequeue path like normal dequeue path.
lib/librte_vhost/virtio_net.c +135 -0
@@ -1881,6 +1881,141 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } +static __rte_unused int +virtio_dev_tx_batch_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE ]; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +uint16_t avail_idx = vq->last_avail_idx; + +if (vhost_dequeue_batch_packed (dev, vq, mbuf_pool, pkts, avail_idx, + desc_addrs, ids)) +return -1 ; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + zmbufs[i] =get_zmbuf (vq); + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (!zmbufs[i]) + goto free_pkt; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { + zmbufs[i]->mbuf = pkts[i]; + zmbufs[i]->desc_idx = avail_idx + i; + zmbufs[i]->desc_count =1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +rte_mbuf_refcnt_update (pkts[i],1 ); + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbufs[i], next); + + vq->nr_zmbuf +=PACKED_BATCH_SIZE ; + vq->last_avail_idx +=PACKED_BATCH_SIZE ; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; + +free_pkt: +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t buf_id, desc_count; +struct zcopy_mbuf *zmbuf; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + zmbuf =get_zmbuf (vq); +if (!zmbuf) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + zmbuf->mbuf = *pkts; + zmbuf->desc_idx = vq->last_avail_idx; + zmbuf->desc_count = desc_count; + +rte_mbuf_refcnt_update (*pkts,1 ); + + vq->nr_zmbuf +=1 ; +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); + + vq->last_avail_idx += desc_count; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + +static __rte_always_inline void +free_zmbuf (struct vhost_virtqueue *vq) +{ +struct zcopy_mbuf *next =NULL ; +struct zcopy_mbuf *zmbuf; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +uint16_t last_used_idx = vq->last_used_idx; + +if (mbuf_is_consumed (zmbuf->mbuf)) { +uint16_t flags =0 ; + +if (vq->used_wrap_counter) + flags =PACKED_TX_USED_FLAG ; +else + flags =PACKED_TX_USED_WRAP_FLAG ; + + vq->desc_packed[last_used_idx].id = zmbuf->desc_idx; + vq->desc_packed[last_used_idx].len =0 ; + +rte_smp_wmb (); + vq->desc_packed[last_used_idx].flags = flags; + + vq->last_used_idx += zmbuf->desc_count; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +restore_mbuf (zmbuf->mbuf); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + } + } +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v3 12/15 vhost: optimize dequeue function of packed ring
In reply to Marvin LiuOptimize vhost device Rx datapath by separate functions. No-chained and direct descriptors will be handled by batch and other will be handled one by one as before.
lib/librte_vhost/virtio_net.c +63 -113
@@ -184,17 +184,6 @@ flush_dequeue_shadow_used_ring_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } -static __rte_always_inline void -update_shadow_used_ring_packed (struct vhost_virtqueue *vq, -uint16_t desc_idx,uint32_t len,uint16_t count) -{ -uint16_t i = vq->shadow_used_idx++; - - vq->shadow_used_packed[i].id = desc_idx; - vq->shadow_used_packed[i].len = len; - vq->shadow_used_packed[i].count = count; -} - static __rte_always_inline voidflush_used_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t *lens,uint16_t *ids,uint16_t flags)
@@ -378,7 +367,7 @@ flush_enqueue_packed(struct virtio_net *dev, } } -static __rte_unused void +static __rte_always_inline voidflush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) {if (!vq->shadow_used_idx)
@@ -1784,7 +1773,7 @@ vhost_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return -1 ; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_tx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts) {
@@ -1859,7 +1848,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_tx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts) {
@@ -1881,7 +1870,7 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1940,7 +1929,7 @@ virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts)
@@ -2017,118 +2006,74 @@ free_zmbuf(struct vhost_virtqueue *vq) } static __rte_noinline uint16_t -virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) +virtio_dev_tx_packed_zmbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint32_t count) { -uint16_t i; - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf, *next; - -for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); - zmbuf !=NULL ; zmbuf = next) { - next =TAILQ_NEXT (zmbuf, next); +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (mbuf_is_consumed (zmbuf->mbuf)) { -update_shadow_used_ring_packed (vq, - zmbuf->desc_idx, -0 , - zmbuf->desc_count); +free_zmbuf (vq); -TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); -restore_mbuf (zmbuf->mbuf); -rte_pktmbuf_free (zmbuf->mbuf); -put_zmbuf (zmbuf); - vq->nr_zmbuf -=1 ; +do { +if (remained >=PACKED_BATCH_SIZE ) { +if (virtio_dev_tx_batch_packed_zmbuf (dev, vq, + mbuf_pool, + &pkts[pkt_idx])) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; } } +if (virtio_dev_tx_single_packed_zmbuf (dev, vq, mbuf_pool, + &pkts[pkt_idx])) +break ; -if (likely (vq->shadow_used_idx)) { -flush_dequeue_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } - -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); + pkt_idx++; + remained--; + }while (remained); - count =RTE_MIN (count,MAX_PKT_BURST ); -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) about to dequeue %u buffers\n" , - dev->vid, count); +if (pkt_idx) +vhost_vring_call_packed (dev, vq); -for (i =0 ; i < count; i++) { -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; -uint16_t buf_id; -uint32_t dummy_len; -uint16_t desc_count, nr_vec =0 ; -int err; +return pkt_idx; +} -if (unlikely (fill_vec_buf_packed (dev, vq, - vq->last_avail_idx, &desc_count, - buf_vec, &nr_vec, - &buf_id, &dummy_len, -VHOST_ACCESS_RO ) <0 )) -break ; +static __rte_noinline uint16_t +virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint32_t count) +{ +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (likely (dev->dequeue_zero_copy ==0 )) -update_shadow_used_ring_packed (vq, buf_id,0 , - desc_count); +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx & + (vq->size -1 )]); - pkts[i] =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (pkts[i] ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) { +flush_dequeue_packed (dev, vq); + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } - err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, pkts[i], - mbuf_pool); -if (unlikely (err)) { -rte_pktmbuf_free (pkts[i]); +if (virtio_dev_tx_single_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx]))break ; - } - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf; - - zmbuf =get_zmbuf (vq); -if (!zmbuf) { -rte_pktmbuf_free (pkts[i]); -break ; - } - zmbuf->mbuf = pkts[i]; - zmbuf->desc_idx = buf_id; - zmbuf->desc_count = desc_count; -/* -* Pin lock the mbuf; we will check later to see -* whether the mbuf is freed (when we are the last -* user) or not. If that's the case, we then could -* update the used ring safely. -*/ -rte_mbuf_refcnt_update (pkts[i],1 ); - - vq->nr_zmbuf +=1 ; -TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); - } - - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } - } + pkt_idx++; + remained--; +flush_dequeue_packed (dev, vq); + }while (remained); -if (likely (dev->dequeue_zero_copy ==0 )) { -do_data_copy_dequeue (vq); -if (unlikely (i < count)) - vq->shadow_used_idx = i; -if (likely (vq->shadow_used_idx)) { -flush_dequeue_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } +if (pkt_idx) { +if (vq->shadow_used_idx) +do_data_copy_dequeue (vq); } -return i; +return pkt_idx; }uint16_t
@@ -2204,9 +2149,14 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, count -=1 ; } -if (vq_is_packed (dev)) - count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, count); -else +if (vq_is_packed (dev)) { +if (unlikely (dev->dequeue_zero_copy)) + count =virtio_dev_tx_packed_zmbuf (dev, vq, mbuf_pool, + pkts, count); +else + count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, + count); + }else count =virtio_dev_tx_split (dev, vq, mbuf_pool, pkts, count); out:
Signature · 1 line
2.17.1
PATCH v3 13/15 vhost: cache address translation result
In reply to Marvin LiuCache address translation result and use it in next translation. Due to limited regions are supported, buffers are most likely in same region when doing data transmission.
lib/librte_vhost/rte_vhost.h +27 -0
@@ -91,10 +91,18 @@ struct rte_vhost_mem_region {int fd; }; +struct rte_vhost_mem_region_cache { +uint64_t guest_phys_addr; +uint64_t guest_phys_addr_end; +int64_t host_user_addr_offset; +uint64_t size; +}; +/** * Memory structure includes region and mapping information. */ struct rte_vhost_memory { +struct rte_vhost_mem_region_cache cache_region;uint32_t nregions;struct rte_vhost_mem_region regions[]; };
@@ -232,11 +240,30 @@ rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,struct rte_vhost_mem_region *r;uint32_t i; +struct rte_vhost_mem_region_cache *r_cache; +/* check with cached region */ + r_cache = &mem->cache_region; +if (likely (gpa >= r_cache->guest_phys_addr && gpa < + r_cache->guest_phys_addr_end)) { +if (unlikely (*len > r_cache->guest_phys_addr_end - gpa)) + *len = r_cache->guest_phys_addr_end - gpa; + +return gpa - r_cache->host_user_addr_offset; + } + +for (i =0 ; i < mem->nregions; i++) { r = &mem->regions[i];if (gpa >= r->guest_phys_addr && gpa < r->guest_phys_addr + r->size) { + r_cache->guest_phys_addr = r->guest_phys_addr; + r_cache->guest_phys_addr_end = r->guest_phys_addr + + r->size; + r_cache->size = r->size; + r_cache->host_user_addr_offset = r->guest_phys_addr - + r->host_user_addr; +if (unlikely (*len > r->guest_phys_addr + r->size - gpa)) *len = r->guest_phys_addr + r->size - gpa;
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v3 13/15] vhost: cache address translation result
Quoted 16 lines · Marvin Liu
Cache address translation result and use it in next translation. Due to limited regions are supported, buffers are most likely in same region when doing data transmission.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/rte_vhost.h +2
-0@@ -91,10 +91,18 @@ struct rte_vhost_mem_region {int fd; }; +struct rte_vhost_mem_region_cache { +uint64_t guest_phys_addr;
lib/librte_vhost/rte_vhost.h struct rte_vhost_mem_region { · line 105
Quoted 10 lines
lib/librte_vhost/rte_vhost.h +6
-0+uint64_t guest_phys_addr_end; +int64_t host_user_addr_offset; +uint64_t size; +}; +/** * Memory structure includes region and mapping information. */ struct rte_vhost_memory { +struct rte_vhost_mem_region_cache cache_region;
This breaks ABI.
Quoted 7 lines
lib/librte_vhost/rte_vhost.h +0
-0uint32_t nregions;struct rte_vhost_mem_region regions[]; };@@ -232,11 +240,30 @@ rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,struct rte_vhost_mem_region *r;uint32_t i;
lib/librte_vhost/rte_vhost.h rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem, · line 252
Quoted 10 lines
lib/librte_vhost/rte_vhost.h +10
-0+struct rte_vhost_mem_region_cache *r_cache; +/* check with cached region */ + r_cache = &mem->cache_region; +if (likely (gpa >= r_cache->guest_phys_addr && gpa < + r_cache->guest_phys_addr_end)) { +if (unlikely (*len > r_cache->guest_phys_addr_end - gpa)) + *len = r_cache->guest_phys_addr_end - gpa; + +return gpa - r_cache->host_user_addr_offset; + }
Does this help a lot in performance?
We can implement this caching for builtin backend first.
Quoted 20 lines
lib/librte_vhost/rte_vhost.h +9
-0+ +for (i =0 ; i < mem->nregions; i++) { r = &mem->regions[i];if (gpa >= r->guest_phys_addr && gpa < r->guest_phys_addr + r->size) { + r_cache->guest_phys_addr = r->guest_phys_addr; + r_cache->guest_phys_addr_end = r->guest_phys_addr + + r->size; + r_cache->size = r->size; + r_cache->host_user_addr_offset = r->guest_phys_addr - + r->host_user_addr; +if (unlikely (*len > r->guest_phys_addr + r->size - gpa)) *len = r->guest_phys_addr + r->size - gpa;Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v3 13/15] vhost: cache address translation result
Quoted 25 lines
-----Original Message-----
From: Bie, Tiwei
Sent: Thursday, September 26, 2019 1:32 PM
To: Liu, Yong <[email protected]>
Cc: [email protected]; Wang, Zhihong <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3 13/15] vhost: cache address translation resultQuoted 14 lines · Marvin Liu
Cache address translation result and use it in next translation. Due to limited regions are supported, buffers are most likely in same region when doing data transmission.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/rte_vhost.h +0
-0@@ -91,10 +91,18 @@ struct rte_vhost_mem_region {int fd; };
Quoted 15 lines
lib/librte_vhost/rte_vhost.h struct rte_vhost_mem_region { · line 105
Quoted 12 lines
lib/librte_vhost/rte_vhost.h +8
-0+struct rte_vhost_mem_region_cache { +uint64_t guest_phys_addr; +uint64_t guest_phys_addr_end; +int64_t host_user_addr_offset; +uint64_t size; +}; +/** * Memory structure includes region and mapping information. */ struct rte_vhost_memory { +struct rte_vhost_mem_region_cache cache_region;This breaks ABI.
Got, will remove it as no clear performance gain with this patch.
Quoted 8 lines
lib/librte_vhost/rte_vhost.h rte_vhost_va_from_guest_pa(struct rte_vhost_memory
Quoted 4 lines
lib/librte_vhost/rte_vhost.h +0
-0uint32_t nregions;struct rte_vhost_mem_region regions[]; };@@ -232,11 +240,30 @@ rte_vhost_va_from_guest_pa(struct rte_vhost_memory*mem,
Quoted 3 lines
lib/librte_vhost/rte_vhost.h +0
-0struct rte_vhost_mem_region *r;uint32_t i;
Quoted 14 lines
lib/librte_vhost/rte_vhost.h rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem, · line 252
Quoted 10 lines
lib/librte_vhost/rte_vhost.h +10
-0+struct rte_vhost_mem_region_cache *r_cache; +/* check with cached region */ + r_cache = &mem->cache_region; +if (likely (gpa >= r_cache->guest_phys_addr && gpa < + r_cache->guest_phys_addr_end)) { +if (unlikely (*len > r_cache->guest_phys_addr_end - gpa)) + *len = r_cache->guest_phys_addr_end - gpa; + +return gpa - r_cache->host_user_addr_offset; + }Does this help a lot in performance?
We can implement this caching for builtin backend first.
Tiwei,
It won’t help too much in performance as region number will be 1 at most of times. Will remove cache function in next version.
Thanks,
Marvin
Quoted 21 lines
Quoted 21 lines
lib/librte_vhost/rte_vhost.h +9
-1+ +for (i =0 ; i < mem->nregions; i++) { r = &mem->regions[i];if (gpa >= r->guest_phys_addr && gpa < r->guest_phys_addr + r->size) { + r_cache->guest_phys_addr = r->guest_phys_addr; + r_cache->guest_phys_addr_end = r->guest_phys_addr + + r->size; + r_cache->size = r->size; + r_cache->host_user_addr_offset = r->guest_phys_addr - + r->host_user_addr; +if (unlikely (*len > r->guest_phys_addr + r->size - gpa)) *len = r->guest_phys_addr + r->size - gpa; --2.17.1
PATCH v3 14/15 vhost: check whether disable software pre-fetch
In reply to Marvin LiuDisable software pre-fetch actions on Skylake and later platforms. Hardware can fetch needed data for vhost, additional software pre-fetch will impact performance.
lib/librte_vhost/Makefile +6 -0
@@ -16,6 +16,12 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +AVX512_SUPPORT=$(shell $(CC) -march=native -dM -E - </dev/null |grep AVX512F) + +ifneq ($(AVX512_SUPPORT),) +CFLAGS += -DDISABLE_SWPREFETCH +endif + ifeq ($(RTE_TOOLCHAIN), gcc) ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA
lib/librte_vhost/virtio_net.c +8 -0
@@ -1075,7 +1075,9 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) (uintptr_t )desc_addrs[i]; lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
@@ -1144,8 +1146,10 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx & (vq->size - 1)]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) { pkt_idx += PACKED_BATCH_SIZE;
@@ -1790,7 +1794,9 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), (void *)(uintptr_t)(desc_addrs[i] + buf_offset), pkts[i]->pkt_len);
@@ -2046,8 +2052,10 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx & (vq->size - 1)]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
Signature · 1 line
2.17.1
PATCH v3 15/15 vhost: optimize packed ring dequeue when in-order
In reply to Marvin LiuWhen VIRTIO_F_IN_ORDER feature is negotiated, vhost can optimize dequeue function by only update first used descriptor.
lib/librte_vhost/virtio_net.c +65 -2
@@ -31,6 +31,12 @@ rxvq_is_mergeable(struct virtio_net *dev)return dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF ); } +static __rte_always_inline bool +virtio_net_is_inorder (struct virtio_net *dev) +{ +return dev->features & (1ULL <<VIRTIO_F_IN_ORDER ); +} +static bool is_valid_virt_queue_idx (uint32_t idx,int is_tx,uint32_t nr_vring) {
@@ -214,6 +220,29 @@ flush_used_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, } } +static __rte_always_inline void +update_dequeue_batch_packed_inorder (struct vhost_virtqueue *vq,uint16_t id) +{ + vq->shadow_used_packed[0 ].id = id; + +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + + vq->shadow_used_idx =1 ; + } + + vq->last_used_idx +=PACKED_BATCH_SIZE ; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + static __rte_always_inline voidupdate_dequeue_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,uint16_t *ids)
@@ -321,6 +350,32 @@ update_dequeue_shadow_used_ring_packed(struct vhost_virtqueue *vq, } } +static __rte_always_inline void +update_dequeue_shadow_used_ring_packed_inorder (struct vhost_virtqueue *vq, +uint16_t buf_id,uint16_t count) +{ + vq->shadow_used_packed[0 ].id = buf_id; + +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count = count; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + + vq->shadow_used_idx =1 ; + } + + vq->last_used_idx += count; + +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1801,8 +1856,12 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, (void *)(uintptr_t )(desc_addrs[i] + buf_offset), pkts[i]->pkt_len); } +if (virtio_net_is_inorder (dev)) +update_dequeue_batch_packed_inorder (vq, + ids[PACKED_BATCH_MASK ]); +else +update_dequeue_batch_packed (dev, vq, ids); -update_dequeue_batch_packed (dev, vq, ids);if (virtio_net_with_host_offload (dev)) {UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM )
@@ -1865,7 +1924,11 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, &desc_count))return -1 ; -update_dequeue_shadow_used_ring_packed (vq, buf_id, desc_count); +if (virtio_net_is_inorder (dev)) +update_dequeue_shadow_used_ring_packed_inorder (vq, buf_id, + desc_count); +else +update_dequeue_shadow_used_ring_packed (vq, buf_id, desc_count); vq->last_avail_idx += desc_count;if (vq->last_avail_idx >= vq->size) {
Signature · 1 line
2.17.1
v4 cover vhost packed ring performance optimization
In reply to Marvin LiuPacked ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
Disable sofware prefetch if hardware can do better.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 40%.
v4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of two
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimization
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (14):
vhost: add single packet enqueue function
vhost: unify unroll pragma parameter
vhost: add batch enqueue function for packed ring
vhost: add single packet dequeue function
vhost: add batch dequeue function
vhost: flush vhost enqueue shadow ring by batch
vhost: add flush function for batch enqueue
vhost: buffer vhost dequeue shadow ring
vhost: split enqueue and dequeue flush functions
vhost: optimize enqueue function of packed ring
vhost: add batch and single zero dequeue functions
vhost: optimize dequeue function of packed ring
vhost: check whether disable software pre-fetch
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 24 + lib/librte_vhost/meson.build | 11 + lib/librte_vhost/vhost.h | 33 ++ lib/librte_vhost/virtio_net.c | 993 +++++++++++++++++++++++++++------- 4 files changed, 878 insertions(+), 183 deletions(-)
Signature · 1 line
2.17.1
PATCH v4 1/14 vhost: add single packet enqueue function
Add vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +82 -0
@@ -774,6 +774,58 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt,struct buf_vector *buf_vec,uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; +uint16_t avail_idx = vq->last_avail_idx; +uint16_t max_tries, tries =0 ; +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; +uint32_t size = pkt->pkt_len + dev->vhost_hlen; +uint16_t num_buffers =0 ; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) +return -1 ; + + len =RTE_MIN (len, size); + size -= len; + + num_buffers +=1 ; + + *nr_descs += desc_count; + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 ) +return -1 ; + +return 0 ; +} + + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -831,6 +883,36 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + + vq->last_avail_idx += nr_descs; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v4 01/14] vhost: add single packet enqueue function
Quoted 5 lines · Marvin Liu
Add vhost enqueue function for single packet and meanwhile left space for flush used ring function.
Signed-off-by Marvin Liu <[email protected]>
PATCH v4 2/14 vhost: unify unroll pragma parameter
In reply to Marvin LiuAdd macro for unifying Clang/ICC/GCC unroll pragma format. Batch functions were contained of several small loops which optimized by compiler’s loop unrolling pragma.
lib/librte_vhost/Makefile +18 -0
@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endif
lib/librte_vhost/meson.build +7 -0
@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DSUPPORT_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DSUPPORT_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4
lib/librte_vhost/vhost.h +18 -0
@@ -39,6 +39,24 @@#define VHOST_LOG_CACHE_NR 32 +#ifdef SUPPORT_GCC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "GCC unroll 4" +#endif + +#ifdef SUPPORT_CLANG_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll 4" +#endif + +#ifdef SUPPORT_ICC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll (4)" +#endif + +#ifdef UNROLL_PRAGMA_PARAM +#define UNROLL_PRAGMA (param) _Pragma(param) +#else +#define UNROLL_PRAGMA (param) do {} while (0); +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v4 02/14] vhost: unify unroll pragma parameter
Quoted 58 lines · Marvin Liu
Add macro for unifying Clang/ICC/GCC unroll pragma format. Batch functions were contained of several small loops which optimized by compiler’s loop unrolling pragma.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/Makefile +18
-0@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endiflib/librte_vhost/meson.build +7
-0@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DSUPPORT_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DSUPPORT_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4lib/librte_vhost/vhost.h +0
-0
lib/librte_vhost/vhost.h line 43
Quoted 5 lines
lib/librte_vhost/vhost.h +2
-0@@ -39,6 +39,24 @@#define VHOST_LOG_CACHE_NR 32 +#ifdef SUPPORT_GCC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "GCC unroll 4"
Shouldn't al these defines be either prefixed with VHOST_, or being declared in EAL headers, so that it can be used by other DPDK libs?
I will pick it as is for now, but please consider above comment and and send a patch on top if it makes sense.
Thanks,
Maxime
Quoted 20 lines
lib/librte_vhost/vhost.h +16
-0+#endif + +#ifdef SUPPORT_CLANG_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll 4" +#endif + +#ifdef SUPPORT_ICC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll (4)" +#endif + +#ifdef UNROLL_PRAGMA_PARAM +#define UNROLL_PRAGMA (param) _Pragma(param) +#else +#define UNROLL_PRAGMA (param) do {} while (0); +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
Re: [dpdk-dev] [PATCH v4 02/14] vhost: unify unroll pragma parameter
Quoted 76 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, October 11, 2019 8:49 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v4 02/14] vhost: unify unroll pragma parameterQuoted 12 lines · Marvin Liu
Add macro for unifying Clang/ICC/GCC unroll pragma format. Batch functions were contained of several small loops which optimized by compiler’s loop unrolling pragma.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/Makefile +0
-0@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasinglib/librte_vhost/Makefile CFLAGS += -I vhost_user · line 26
Quoted 10 lines
lib/librte_vhost/Makefile +8
-0LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37&& echo 1), 1)
Quoted 39 lines
lib/librte_vhost/Makefile +10
-0+CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endiflib/librte_vhost/meson.build +7
-0@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DSUPPORT_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DSUPPORT_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4lib/librte_vhost/vhost.h +0
-0@@ -39,6 +39,24 @@#define VHOST_LOG_CACHE_NR 32
Quoted 9 lines
lib/librte_vhost/vhost.h line 43
Quoted 2 lines
lib/librte_vhost/vhost.h +2
-0+#ifdef SUPPORT_GCC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "GCC unroll 4"Shouldn't al these defines be either prefixed with
VHOST_, or being declared in EAL headers, so that it can be used by other DPDK libs?I will pick it as is for now, but please consider above comment and and send a patch on top if it makes sense.
Hi Maxime,
For making loop unroll macro more generic, modified version as below.
Since only vhost utilize the benefit of compiler's unroll feature, I'd like to keep it in vhost by now.
#ifdef SUPPORT_GCC_UNROLL_PRAGMA
#define for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
for (iter = val; iter < size; iter++)
#endif
#ifdef SUPPORT_CLANG_UNROLL_PRAGMA
#define for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
for (iter = val; iter < size; iter++)
#endif
#ifdef SUPPORT_ICC_UNROLL_PRAGMA
#define for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
for (iter = val; iter < size; iter++)
#endif
#ifndef for_each_try_unroll
#define for_each_try_unroll(iter, val, num) \
for (iter = val; iter < num; iter++)
#endif
Regards,
Marvin
Quoted 22 lines
Thanks,
MaximeQuoted 20 lines
lib/librte_vhost/vhost.h +16
-0+#endif + +#ifdef SUPPORT_CLANG_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll 4" +#endif + +#ifdef SUPPORT_ICC_UNROLL_PRAGMA +#define UNROLL_PRAGMA_PARAM "unroll (4)" +#endif + +#ifdef UNROLL_PRAGMA_PARAM +#define UNROLL_PRAGMA (param) _Pragma(param) +#else +#define UNROLL_PRAGMA (param) do {} while (0); +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
PATCH v4 3/14 vhost: add batch enqueue function for packed ring
In reply to Marvin LiuBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function not support chained mbufs, single packet enqueue function will handle it.
lib/librte_vhost/vhost.h +4 -0
@@ -39,6 +39,10 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) +#ifdef SUPPORT_GCC_UNROLL_PRAGMA #define UNROLL_PRAGMA_PARAM "GCC unroll 4"#endif
lib/librte_vhost/virtio_net.c +80 -0
86 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v4 03/14] vhost: add batch enqueue function for packed ring
Quoted 7 lines · Marvin Liu
Batch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function not support chained mbufs, single packet enqueue function will handle it.
Signed-off-by Marvin Liu <[email protected]>
Re: [dpdk-dev] [PATCH v4 03/14] vhost: add batch enqueue function for packed ring
In reply to Marvin LiuQuoted 7 lines · Marvin Liu
Batch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function not support chained mbufs, single packet enqueue function will handle it.
Signed-off-by Marvin Liu <[email protected]>
Thinking again about this patch and series in general...
So this series improves performance by 40% in cases where:
- descriptors are cache aligned
- single mbuf
But my understanding is that it will cause performance regression for the other cases, which may not be that uncommon, no?
Do you have some number about the performance impact on these other cases?
Thanks,
Maxime
Re: [dpdk-dev] [PATCH v4 03/14] vhost: add batch enqueue function for packed ring
Quoted 29 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, October 11, 2019 10:22 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v4 03/14] vhost: add batch enqueue function for packed ringQuoted 7 lines · Marvin Liu
Batch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function not support chained mbufs, single packet enqueue function will handle it.
Signed-off-by Marvin Liu <[email protected]>Thinking again about this patch and series in general...
So this series improves performance by 40% in cases where:
- descriptors are cache aligned
- single mbuf
Quoted 6 lines
But my understanding is that it will cause performance regression for the other cases, which may not be that uncommon, no?
Do you have some number about the performance impact on these other cases?
Hi Maxime,
Check prerequisites of batch handling is pretty simple and fast.
It almost has no performance impact on uncommon case.
Chained packets can slightly benefit from cache related optimization.
As shown in below table, all cases I run can benefit from vhost optimization. From our experimental, more performance gain can be seen if more packets handled by batch.
+---------------------------------------------------+ | | 19.08 | + opt | |-----------------------------------|-------|-------| | 1518B PvP | 2.63M | 2.98M | |-----------------------------------|-------|-------| | 64B loopback | 7.81M | 12.0M | |-----------------------------------|-------|-------| | 1518B loopback | 3.59M | 4.69M | |-----------------------------------|-------|-------| | 16K chained loopback | 297K | 306K | |-----------------------------------|-------|-------| | 50% 256B + 50% 16K | 296K | 309K | |-----------------------------------|-------|-------| | pktgen_sample03_burst_single_flow | 6.03M | 6.39M | +---------------------------------------------------+
Regards,
Marvin
Quoted 2 lines
Thanks,
Maxime
PATCH v4 4/14 vhost: add single packet dequeue function
In reply to Marvin LiuAdd vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
lib/librte_vhost/virtio_net.c +54 -0
@@ -1571,6 +1571,60 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_dequeue_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t *buf_id, +uint16_t *desc_count) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t dummy_len; +uint16_t nr_vec =0 ; +int err; + +if (unlikely (fill_vec_buf_packed (dev, vq, + vq->last_avail_idx, desc_count, + buf_vec, &nr_vec, + buf_id, &dummy_len, +VHOST_ACCESS_RO ) <0 )) +return -1 ; + + *pkts =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (*pkts ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA , +"Failed to allocate memory for mbuf.\n" ); +return -1 ; + } + + err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, *pkts, + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + +return 0 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts) +{ + +uint16_t buf_id, desc_count; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + vq->last_avail_idx += desc_count; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v4 04/14] vhost: add single packet dequeue function
Quoted 5 lines · Marvin Liu
Add vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
Signed-off-by Marvin Liu <[email protected]>
PATCH v4 5/14 vhost: add batch dequeue function
In reply to Marvin LiuAdd batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
lib/librte_vhost/vhost.h +2 -0
@@ -61,6 +61,8 @@#define UNROLL_PRAGMA (param) do {} while (0);#endif +#define PACKED_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | VRING_DESC_F_INDIRECT) +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
lib/librte_vhost/virtio_net.c +113 -0
119 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 6/14 vhost: flush vhost enqueue shadow ring by batch
In reply to Marvin LiuBuffer vhost enqueue shadow ring update, flush shadow ring until buffered descriptors number exceed one batch. Thus virtio can receive packets at a faster frequency.
lib/librte_vhost/vhost.h +1 -0
@@ -163,6 +163,7 @@ struct vhost_virtqueue {struct vring_used_elem_packed *shadow_used_packed; };uint16_t shadow_used_idx; +uint16_t enqueue_shadow_count;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +43 -0
73 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 7/14 vhost: add flush function for batch enqueue
In reply to Marvin LiuFlush used flags when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_RX_USED_FLAG (0ULL | VRING_DESC_F_AVAIL | VRING_DESC_F_USED \ + | VRING_DESC_F_WRITE) +#define PACKED_RX_USED_WRAP_FLAG (VRING_DESC_F_WRITE)#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
lib/librte_vhost/virtio_net.c +50 -0
68 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v4 8/14 vhost: buffer vhost dequeue shadow ring
In reply to Marvin LiuBuffer used ring updates as many as possible in vhost dequeue function for coordinating with virtio driver. For supporting buffer, shadow used ring element should contain descriptor index and its wrap counter. First shadowed ring index is recorded for calculating buffered number.
lib/librte_vhost/vhost.h +5 -0
@@ -42,6 +42,8 @@#define PACKED_RX_USED_FLAG (0ULL | VRING_DESC_F_AVAIL | VRING_DESC_F_USED \ | VRING_DESC_F_WRITE)#define PACKED_RX_USED_WRAP_FLAG (VRING_DESC_F_WRITE) +#define PACKED_TX_USED_FLAG (0ULL | VRING_DESC_F_AVAIL | VRING_DESC_F_USED) +#define PACKED_TX_USED_WRAP_FLAG (0x0)#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
@@ -110,9 +112,11 @@ struct log_cache_entry { };struct vring_used_elem_packed { +uint16_t used_idx;uint16_t id;uint32_t len;uint32_t count; +uint16_t used_wrap_counter; };/**
@@ -167,6 +171,7 @@ struct vhost_virtqueue { };uint16_t shadow_used_idx;uint16_t enqueue_shadow_count; +uint16_t dequeue_shadow_head;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +37 -0
@@ -230,6 +230,41 @@ update_enqueue_shadow_used_ring_packed(struct vhost_virtqueue *vq, vq->enqueue_shadow_count += count; } +static __rte_always_inline void +update_dequeue_shadow_used_ring_packed (struct vhost_virtqueue *vq, +uint16_t buf_id,uint16_t count) +{ +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + + vq->shadow_used_packed[0 ].id = buf_id; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count = count; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + + vq->shadow_used_idx =1 ; + }else { + vq->desc_packed[vq->last_used_idx].id = buf_id; + vq->desc_packed[vq->last_used_idx].len =0 ; + +if (vq->used_wrap_counter) + vq->desc_packed[vq->last_used_idx].flags = +PACKED_TX_USED_FLAG ; +else + vq->desc_packed[vq->last_used_idx].flags = +PACKED_TX_USED_WRAP_FLAG ; + } + + vq->last_used_idx += count; + +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1822,6 +1857,8 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, &desc_count))return -1 ; +update_dequeue_shadow_used_ring_packed (vq, buf_id, desc_count); + vq->last_avail_idx += desc_count;if (vq->last_avail_idx >= vq->size) { vq->last_avail_idx -= vq->size;
Signature · 1 line
2.17.1
PATCH v4 9/14 vhost: split enqueue and dequeue flush functions
In reply to Marvin LiuVhost enqueue descriptors are updated by batch number, while vhost dequeue descriptors are buffered. Meanwhile in dequeue function only first descriptor is buffered. Due to these differences, split vhost enqueue and dequeue flush functions.
lib/librte_vhost/virtio_net.c +93 -6
@@ -92,8 +92,8 @@ update_shadow_used_ring_split(struct vhost_virtqueue *vq, } static __rte_always_inline void -flush_shadow_used_ring_packed (struct virtio_net *dev, -struct vhost_virtqueue *vq) +flush_enqueue_shadow_used_ring_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq) {int i;uint16_t used_idx = vq->last_used_idx;
@@ -158,6 +158,32 @@ flush_shadow_used_ring_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } +static __rte_always_inline void +flush_dequeue_shadow_used_ring_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq) +{ +uint16_t head_idx = vq->dequeue_shadow_head; +uint16_t head_flags; +struct vring_used_elem_packed *used_elem = &vq->shadow_used_packed[0 ]; + +if (used_elem->used_wrap_counter) + head_flags =PACKED_TX_USED_FLAG ; +else + head_flags =PACKED_TX_USED_WRAP_FLAG ; + + vq->desc_packed[head_idx].id = used_elem->id; + +rte_smp_wmb (); + vq->desc_packed[head_idx].flags = head_flags; + +vhost_log_cache_used_vring (dev, vq, head_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc )); + + vq->shadow_used_idx =0 ; +vhost_log_cache_sync (dev, vq); +} + static __rte_always_inline voidupdate_shadow_used_ring_packed (struct vhost_virtqueue *vq,uint16_t desc_idx,uint32_t len,uint16_t count)
@@ -199,6 +225,47 @@ flush_used_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, } } +static __rte_always_inline void +update_dequeue_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +uint16_t *ids) +{ +uint16_t flags =0 ; +uint16_t i; + +if (vq->used_wrap_counter) + flags =PACKED_TX_USED_FLAG ; +else + flags =PACKED_TX_USED_WRAP_FLAG ; + +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + vq->shadow_used_packed[0 ].id = ids[0 ]; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 1; i < PACKED_BATCH_SIZE; i++) + vq->desc_packed[vq->last_used_idx + i].id = ids[i]; +rte_smp_wmb (); +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 1; i < PACKED_BATCH_SIZE; i++) + vq->desc_packed[vq->last_used_idx + i].flags = flags; + + vq->shadow_used_idx =1 ; + vq->last_used_idx +=PACKED_BATCH_SIZE ; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } + }else { +uint64_t lens[PACKED_BATCH_SIZE ] = {0 }; +flush_used_batch_packed (dev, vq, lens, ids, flags); + } +} + static __rte_always_inline voidflush_enqueue_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t *lens,uint16_t *ids)
@@ -306,11 +373,29 @@ flush_enqueue_packed(struct virtio_net *dev,if (vq->enqueue_shadow_count >=PACKED_BATCH_SIZE ) {do_data_copy_enqueue (dev, vq); -flush_shadow_used_ring_packed (dev, vq); +flush_enqueue_shadow_used_ring_packed (dev, vq); } } } +static __rte_unused void +flush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) +{ +if (!vq->shadow_used_idx) +return ; + +int16_t shadow_count = vq->last_used_idx - vq->dequeue_shadow_head; +if (shadow_count <=0 ) + shadow_count += vq->size; + +/* buffer used descs as many as possible when doing dequeue */ +if ((uint16_t )shadow_count >= (vq->size -MAX_PKT_BURST )) { +do_data_copy_dequeue (vq); +flush_dequeue_shadow_used_ring_packed (dev, vq); +vhost_vring_call_packed (dev, vq); + } +} +/* avoid write operation when necessary, to lessen cache issues */ #define ASSIGN_UNLESS_EQUAL (var, val) do { \ if ((var) != (val)) \
@@ -1165,7 +1250,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,do_data_copy_enqueue (dev, vq);if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); +flush_enqueue_shadow_used_ring_packed (dev, vq);vhost_vring_call_packed (dev, vq); }
@@ -1796,6 +1881,8 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, pkts[i]->pkt_len); } +update_dequeue_batch_packed (dev, vq, ids); +if (virtio_net_with_host_offload (dev)) {UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) {
@@ -1896,7 +1983,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, }if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); +flush_dequeue_shadow_used_ring_packed (dev, vq);vhost_vring_call_packed (dev, vq); } }
@@ -1975,7 +2062,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,if (unlikely (i < count)) vq->shadow_used_idx = i;if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); +flush_dequeue_shadow_used_ring_packed (dev, vq);vhost_vring_call_packed (dev, vq); } }
Signature · 1 line
2.17.1
PATCH v4 10/14 vhost: optimize enqueue function of packed ring
In reply to Marvin LiuOptimize vhost device Tx datapath by separate functions. Packets can be filled into one descriptor will be handled by batch and others will be handled one by one as before.
lib/librte_vhost/virtio_net.c +20 -94
@@ -753,64 +753,6 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t size,struct buf_vector *buf_vec, -uint16_t *nr_vec,uint16_t *num_buffers, -uint16_t *nr_descs) -{ -uint16_t avail_idx; -uint16_t vec_idx =0 ; -uint16_t max_tries, tries =0 ; - -uint16_t buf_id =0 ; -uint32_t len =0 ; -uint16_t desc_count; - - *num_buffers =0 ; - avail_idx = vq->last_avail_idx; - -if (rxvq_is_mergeable (dev)) - max_tries = vq->size -1 ; -else - max_tries =1 ; - -while (size >0 ) { -/* -* if we tried all available ring items, and still -* can't get enough buf, it means something abnormal -* happened. -*/ -if (unlikely (++tries > max_tries)) -return -1 ; - -if (unlikely (fill_vec_buf_packed (dev, vq, - avail_idx, &desc_count, - buf_vec, &vec_idx, - &buf_id, &len, -VHOST_ACCESS_RW ) <0 )) -return -1 ; - - len =RTE_MIN (len, size); -update_shadow_used_ring_packed (vq, buf_id, len, desc_count); - size -= len; - - avail_idx += desc_count; -if (avail_idx >= vq->size) - avail_idx -= vq->size; - - *nr_descs += desc_count; - *num_buffers +=1 ; - } - - *nr_vec = vec_idx; - -return 0 ; -} - static __rte_noinline void copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, struct buf_vector *buf_vec,
@@ -1089,7 +1031,7 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_rx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts) {
@@ -1176,7 +1118,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -static __rte_unused int16_t +static __rte_always_inline int16_tvirtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *pkt) {
@@ -1205,52 +1147,36 @@ virtio_dev_rx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } - static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count) {uint32_t pkt_idx =0 ; -uint16_t num_buffers; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t remained = count; -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; -uint16_t nr_vec =0 ; -uint16_t nr_descs =0 ; +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); -if (unlikely (reserve_avail_buf_packed (dev, vq, - pkt_len, buf_vec, &nr_vec, - &num_buffers, &nr_descs) <0 )) { -VHOST_LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); - vq->shadow_used_idx -= num_buffers; -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_rx_batch_packed (dev, vq, pkts)) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, vq->last_avail_idx, - vq->last_avail_idx + num_buffers); - -if (copy_mbuf_to_desc (dev, vq, pkts[pkt_idx], - buf_vec, nr_vec, - num_buffers) <0 ) { - vq->shadow_used_idx -= num_buffers; +if (virtio_dev_rx_single_packed (dev, vq, pkts[pkt_idx]))break ; - } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } - } - -do_data_copy_enqueue (dev, vq); + pkt_idx++; + remained--; + }while (pkt_idx < count); -if (likely (vq->shadow_used_idx)) { -flush_enqueue_shadow_used_ring_packed (dev, vq); +if (pkt_idx) { +if (vq->shadow_used_idx) { +do_data_copy_enqueue (dev, vq); +flush_enqueue_shadow_used_ring_packed (dev, vq); + }vhost_vring_call_packed (dev, vq); }
Signature · 1 line
2.17.1
PATCH v4 11/14 vhost: add batch and single zero dequeue functions
In reply to Marvin LiuOptimize vhost zero copy dequeue path like normal dequeue path.
lib/librte_vhost/virtio_net.c +135 -0
@@ -1881,6 +1881,141 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } +static __rte_unused int +virtio_dev_tx_batch_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE ]; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +uint16_t avail_idx = vq->last_avail_idx; + +if (vhost_dequeue_batch_packed (dev, vq, mbuf_pool, pkts, avail_idx, + desc_addrs, ids)) +return -1 ; + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) + zmbufs[i] =get_zmbuf (vq); + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { +if (!zmbufs[i]) + goto free_pkt; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) { + zmbufs[i]->mbuf = pkts[i]; + zmbufs[i]->desc_idx = avail_idx + i; + zmbufs[i]->desc_count =1 ; + } + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +rte_mbuf_refcnt_update (pkts[i],1 ); + +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbufs[i], next); + + vq->nr_zmbuf +=PACKED_BATCH_SIZE ; + vq->last_avail_idx +=PACKED_BATCH_SIZE ; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; + +free_pkt: +UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t buf_id, desc_count; +struct zcopy_mbuf *zmbuf; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + zmbuf =get_zmbuf (vq); +if (!zmbuf) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + zmbuf->mbuf = *pkts; + zmbuf->desc_idx = vq->last_avail_idx; + zmbuf->desc_count = desc_count; + +rte_mbuf_refcnt_update (*pkts,1 ); + + vq->nr_zmbuf +=1 ; +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); + + vq->last_avail_idx += desc_count; +if (vq->last_avail_idx >= vq->size) { + vq->last_avail_idx -= vq->size; + vq->avail_wrap_counter ^=1 ; + } + +return 0 ; +} + +static __rte_always_inline void +free_zmbuf (struct vhost_virtqueue *vq) +{ +struct zcopy_mbuf *next =NULL ; +struct zcopy_mbuf *zmbuf; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +uint16_t last_used_idx = vq->last_used_idx; + +if (mbuf_is_consumed (zmbuf->mbuf)) { +uint16_t flags =0 ; + +if (vq->used_wrap_counter) + flags =PACKED_TX_USED_FLAG ; +else + flags =PACKED_TX_USED_WRAP_FLAG ; + + vq->desc_packed[last_used_idx].id = zmbuf->desc_idx; + vq->desc_packed[last_used_idx].len =0 ; + +rte_smp_wmb (); + vq->desc_packed[last_used_idx].flags = flags; + + vq->last_used_idx += zmbuf->desc_count; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +restore_mbuf (zmbuf->mbuf); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + } + } +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v4 12/14 vhost: optimize dequeue function of packed ring
In reply to Marvin LiuOptimize vhost device Rx datapath by separate functions. No-chained and direct descriptors will be handled by batch and other will be handled one by one as before.
lib/librte_vhost/virtio_net.c +62 -113
@@ -184,17 +184,6 @@ flush_dequeue_shadow_used_ring_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } -static __rte_always_inline void -update_shadow_used_ring_packed (struct vhost_virtqueue *vq, -uint16_t desc_idx,uint32_t len,uint16_t count) -{ -uint16_t i = vq->shadow_used_idx++; - - vq->shadow_used_packed[i].id = desc_idx; - vq->shadow_used_packed[i].len = len; - vq->shadow_used_packed[i].count = count; -} - static __rte_always_inline voidflush_used_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t *lens,uint16_t *ids,uint16_t flags)
@@ -378,7 +367,7 @@ flush_enqueue_packed(struct virtio_net *dev, } } -static __rte_unused void +static __rte_always_inline voidflush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) {if (!vq->shadow_used_idx)
@@ -1784,7 +1773,7 @@ vhost_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return -1 ; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_tx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts) {
@@ -1859,7 +1848,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_tx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts) {
@@ -1881,7 +1870,7 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1940,7 +1929,7 @@ virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts)
@@ -2017,118 +2006,73 @@ free_zmbuf(struct vhost_virtqueue *vq) } static __rte_noinline uint16_t -virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) +virtio_dev_tx_packed_zmbuf (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint32_t count) { -uint16_t i; - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf, *next; - -for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); - zmbuf !=NULL ; zmbuf = next) { - next =TAILQ_NEXT (zmbuf, next); +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (mbuf_is_consumed (zmbuf->mbuf)) { -update_shadow_used_ring_packed (vq, - zmbuf->desc_idx, -0 , - zmbuf->desc_count); +free_zmbuf (vq); -TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); -restore_mbuf (zmbuf->mbuf); -rte_pktmbuf_free (zmbuf->mbuf); -put_zmbuf (zmbuf); - vq->nr_zmbuf -=1 ; +do { +if (remained >=PACKED_BATCH_SIZE ) { +if (virtio_dev_tx_batch_packed_zmbuf (dev, vq, + mbuf_pool, + &pkts[pkt_idx])) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; } } +if (virtio_dev_tx_single_packed_zmbuf (dev, vq, mbuf_pool, + &pkts[pkt_idx])) +break ; -if (likely (vq->shadow_used_idx)) { -flush_dequeue_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } - -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); + pkt_idx++; + remained--; + }while (remained); - count =RTE_MIN (count,MAX_PKT_BURST ); -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) about to dequeue %u buffers\n" , - dev->vid, count); +if (pkt_idx) +vhost_vring_call_packed (dev, vq); -for (i =0 ; i < count; i++) { -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; -uint16_t buf_id; -uint32_t dummy_len; -uint16_t desc_count, nr_vec =0 ; -int err; +return pkt_idx; +} -if (unlikely (fill_vec_buf_packed (dev, vq, - vq->last_avail_idx, &desc_count, - buf_vec, &nr_vec, - &buf_id, &dummy_len, -VHOST_ACCESS_RO ) <0 )) -break ; +static __rte_noinline uint16_t +virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint32_t count) +{ +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (likely (dev->dequeue_zero_copy ==0 )) -update_shadow_used_ring_packed (vq, buf_id,0 , - desc_count); +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); - pkts[i] =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (pkts[i] ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) { +flush_dequeue_packed (dev, vq); + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } - err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, pkts[i], - mbuf_pool); -if (unlikely (err)) { -rte_pktmbuf_free (pkts[i]); +if (virtio_dev_tx_single_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx]))break ; - } - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf; - - zmbuf =get_zmbuf (vq); -if (!zmbuf) { -rte_pktmbuf_free (pkts[i]); -break ; - } - zmbuf->mbuf = pkts[i]; - zmbuf->desc_idx = buf_id; - zmbuf->desc_count = desc_count; -/* -* Pin lock the mbuf; we will check later to see -* whether the mbuf is freed (when we are the last -* user) or not. If that's the case, we then could -* update the used ring safely. -*/ -rte_mbuf_refcnt_update (pkts[i],1 ); - - vq->nr_zmbuf +=1 ; -TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); - } - - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } - } + pkt_idx++; + remained--; +flush_dequeue_packed (dev, vq); + }while (remained); -if (likely (dev->dequeue_zero_copy ==0 )) { -do_data_copy_dequeue (vq); -if (unlikely (i < count)) - vq->shadow_used_idx = i; -if (likely (vq->shadow_used_idx)) { -flush_dequeue_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } +if (pkt_idx) { +if (vq->shadow_used_idx) +do_data_copy_dequeue (vq); } -return i; +return pkt_idx; }uint16_t
@@ -2204,9 +2148,14 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, count -=1 ; } -if (vq_is_packed (dev)) - count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, count); -else +if (vq_is_packed (dev)) { +if (unlikely (dev->dequeue_zero_copy)) + count =virtio_dev_tx_packed_zmbuf (dev, vq, mbuf_pool, + pkts, count); +else + count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, + count); + }else count =virtio_dev_tx_split (dev, vq, mbuf_pool, pkts, count); out:
Signature · 1 line
2.17.1
PATCH v4 13/14 vhost: check whether disable software pre-fetch
In reply to Marvin LiuDisable software pre-fetch actions on Skylake and later platforms. Hardware can fetch needed data for vhost, additional software pre-fetch will impact performance.
lib/librte_vhost/Makefile +6 -0
@@ -16,6 +16,12 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +AVX512_SUPPORT=$(shell $(CC) -march=native -dM -E - </dev/null |grep AVX512F) + +ifneq ($(AVX512_SUPPORT),) +CFLAGS += -DDISABLE_SWPREFETCH +endif + ifeq ($(RTE_TOOLCHAIN), gcc) ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA
lib/librte_vhost/meson.build +4 -0
@@ -15,6 +15,10 @@ elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' endif +r = run_command(toolchain, '-march=native', '-dM', '-E', '-', '</dev/null', '|', 'grep AVX512F') +if (r.stdout().strip() != '') + cflags += '-DDISABLE_SWPREFETCH' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4
lib/librte_vhost/virtio_net.c +8 -0
@@ -1075,7 +1075,9 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) (uintptr_t )desc_addrs[i]; lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;
@@ -1144,7 +1146,9 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {
@@ -1790,7 +1794,9 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), (void *)(uintptr_t)(desc_addrs[i] + buf_offset), pkts[i]->pkt_len);
@@ -2046,7 +2052,9 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v4 13/14] vhost: check whether disable software pre-fetch
Quoted 11 lines · Marvin Liu
Disable software pre-fetch actions on Skylake and later platforms. Hardware can fetch needed data for vhost, additional software pre-fetch will impact performance.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/Makefile +0
-0
lib/librte_vhost/Makefile CFLAGS += -I vhost_user · line 23
Quoted 8 lines
lib/librte_vhost/Makefile +5
-0@@ -16,6 +16,12 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +AVX512_SUPPORT=$(shell $(CC) -march=native -dM -E - </dev/null |grep AVX512F) + +ifneq ($(AVX512_SUPPORT),) +CFLAGS += -DDISABLE_SWPREFETCH +endif
That's problematic I think, because the machine running the lib may be different from the machine building it, for example distros.
In this case, a Skylake or later may be used to build the package, but with passing "-march=haswell". It would end-up prefetching being disabled whereas we would expect it to be enabled.
I see several solutions:
- Check for CONFIG_RTE_ENABLE_AVX512 flag.
- Keep prefetch instructions (what would be the impact on Skylake and later?)
- Remove prefetch instructions (what would be the impact on pre- Skylake?)
But really, I think we need some figures before applying such a patch. What performance gain do you measure with this patch?
Quoted 63 lines
lib/librte_vhost/Makefile +0
-0ifeq ($(RTE_TOOLCHAIN), gcc) ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMAlib/librte_vhost/meson.build +4
-0@@ -15,6 +15,10 @@ elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' endif +r = run_command(toolchain, '-march=native', '-dM', '-E', '-', '</dev/null', '|', 'grep AVX512F') +if (r.stdout().strip() != '') + cflags += '-DDISABLE_SWPREFETCH' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4lib/librte_vhost/virtio_net.c +8
-0@@ -1075,7 +1075,9 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) (uintptr_t )desc_addrs[i]; lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;@@ -1144,7 +1146,9 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {@@ -1790,7 +1794,9 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), (void *)(uintptr_t)(desc_addrs[i] + buf_offset), pkts[i]->pkt_len);@@ -2046,7 +2052,9 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
Re: [dpdk-dev] [PATCH v4 13/14] vhost: check whether disable software pre-fetch
Quoted 39 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Friday, October 11, 2019 10:12 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v4 13/14] vhost: check whether disable software pre- fetchQuoted 11 lines · Marvin Liu
Disable software pre-fetch actions on Skylake and later platforms. Hardware can fetch needed data for vhost, additional software pre-fetch will impact performance.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/Makefile +0
-0lib/librte_vhost/Makefile CFLAGS += -I vhost_user · line 19
Quoted 4 lines
lib/librte_vhost/Makefile +1
-0@@ -16,6 +16,12 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +AVX512_SUPPORT=$(shell $(CC) -march=native -dM -E - </dev/null |grepAVX512F)
lib/librte_vhost/Makefile CFLAGS += -I vhost_user · line 23
Quoted 4 lines
lib/librte_vhost/Makefile +4
-0+ +ifneq ($(AVX512_SUPPORT),) +CFLAGS += -DDISABLE_SWPREFETCH +endifThat's problematic I think, because the machine running the lib may be different from the machine building it, for example distros.
Quoted 4 lines
In this case, a Skylake or later may be used to build the package, but with passing "-march=haswell". It would end-up prefetching being disabled whereas we would expect it to be enabled.
Thanks, Maxime. Got your idea. Compiling environment and running environment maybe different. Performance impact on skylake is around 1% in V1 patch under vhost/virtio loopback scenario. Since the impact is very small and has no impact in later revised version.
I'd like to remove this patch.
Regards,
Marvin
Quoted 81 lines
I see several solutions:
- Check forCONFIG_RTE_ENABLE_AVX512flag.
- Keep prefetch instructions (what would be the impact on Skylake and later?)
- Remove prefetch instructions (what would be the impact on pre- Skylake?)But really, I think we need some figures before applying such a patch. What performance gain do you measure with this patch?
lib/librte_vhost/meson.build elif (toolchain == 'clang' and
Quoted 8 lines
lib/librte_vhost/Makefile +0
-0ifeq ($(RTE_TOOLCHAIN), gcc) ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMAlib/librte_vhost/meson.build +0
-0@@ -15,6 +15,10 @@ elif (toolchain == 'clang' andcc.version().version_compare('>=3.7.0'))
lib/librte_vhost/meson.build elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0'))
Quoted 4 lines
lib/librte_vhost/meson.build +1
-0elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' endif +r = run_command(toolchain, '-march=native', '-dM', '-E', '-','<
/dev/null', '|', 'grep AVX512F')Review
Quoted 7 lines
lib/librte_vhost/meson.build +3
-0+if (r.stdout().strip() != '') + cflags += '-DDISABLE_SWPREFETCH' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4(quoted diff) +0
-0
b/lib/librte_vhost/virtio_net.clib/librte_vhost/virtio_net.c virtio_dev_rx_batch_packed(struct virtio_net *dev,
Quoted 4 lines
index
56c2080fb..046e497c2100644lib/librte_vhost/virtio_net.c +0
-0@@ -1075,7 +1075,9 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,struct
vhost_virtqueue*vq,lib/librte_vhost/virtio_net.c virtio_dev_rx_packed(struct virtio_net *dev, struct
Quoted 10 lines
lib/librte_vhost/virtio_net.c +2
-0UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) (uintptr_t )desc_addrs[i]; lens[i] = pkts[i]->pkt_len + dev->vhost_hlen;@@ -1144,7 +1146,9 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct
vhost_virtqueue*vq,lib/librte_vhost/virtio_net.c virtio_dev_tx_batch_packed(struct virtio_net *dev,
Quoted 10 lines
lib/librte_vhost/virtio_net.c +2
-0uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_rx_batch_packed(dev, vq, pkts)) {@@ -1790,7 +1794,9 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev,struct
vhost_virtqueue*vq,lib/librte_vhost/virtio_net.c virtio_dev_tx_packed(struct virtio_net *dev, struct
Quoted 10 lines
lib/librte_vhost/virtio_net.c +2
-0UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM) for (i = 0; i < PACKED_BATCH_SIZE; i++) { +#ifndef DISABLE_SWPREFETCH rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); +#endif rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), (void *)(uintptr_t)(desc_addrs[i] + buf_offset), pkts[i]->pkt_len);@@ -2046,7 +2052,9 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct
vhost_virtqueue*vq,Quoted 10 lines
lib/librte_vhost/virtio_net.c +2
-0uint32_t remained = count; do { +#ifndef DISABLE_SWPREFETCH rte_prefetch0(&vq->desc_packed[vq->last_avail_idx]); +#endif if (remained >= PACKED_BATCH_SIZE) { if (!virtio_dev_tx_batch_packed(dev, vq, mbuf_pool,
PATCH v4 14/14 vhost: optimize packed ring dequeue when in-order
In reply to Marvin LiuWhen VIRTIO_F_IN_ORDER feature is negotiated, vhost can optimize dequeue function by only update first used descriptor.
lib/librte_vhost/virtio_net.c +65 -2
@@ -31,6 +31,12 @@ rxvq_is_mergeable(struct virtio_net *dev)return dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF ); } +static __rte_always_inline bool +virtio_net_is_inorder (struct virtio_net *dev) +{ +return dev->features & (1ULL <<VIRTIO_F_IN_ORDER ); +} +static bool is_valid_virt_queue_idx (uint32_t idx,int is_tx,uint32_t nr_vring) {
@@ -214,6 +220,29 @@ flush_used_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, } } +static __rte_always_inline void +update_dequeue_batch_packed_inorder (struct vhost_virtqueue *vq,uint16_t id) +{ + vq->shadow_used_packed[0 ].id = id; + +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + + vq->shadow_used_idx =1 ; + } + + vq->last_used_idx +=PACKED_BATCH_SIZE ; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + static __rte_always_inline voidupdate_dequeue_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,uint16_t *ids)
@@ -321,6 +350,32 @@ update_dequeue_shadow_used_ring_packed(struct vhost_virtqueue *vq, } } +static __rte_always_inline void +update_dequeue_shadow_used_ring_packed_inorder (struct vhost_virtqueue *vq, +uint16_t buf_id,uint16_t count) +{ + vq->shadow_used_packed[0 ].id = buf_id; + +if (!vq->shadow_used_idx) { + vq->dequeue_shadow_head = vq->last_used_idx; + + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count = count; + vq->shadow_used_packed[0 ].used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].used_wrap_counter = + vq->used_wrap_counter; + + vq->shadow_used_idx =1 ; + } + + vq->last_used_idx += count; + +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1801,8 +1856,12 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, (void *)(uintptr_t )(desc_addrs[i] + buf_offset), pkts[i]->pkt_len); } +if (virtio_net_is_inorder (dev)) +update_dequeue_batch_packed_inorder (vq, + ids[PACKED_BATCH_MASK ]); +else +update_dequeue_batch_packed (dev, vq, ids); -update_dequeue_batch_packed (dev, vq, ids);if (virtio_net_with_host_offload (dev)) {UNROLL_PRAGMA (UNROLL_PRAGMA_PARAM )
@@ -1865,7 +1924,11 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, &desc_count))return -1 ; -update_dequeue_shadow_used_ring_packed (vq, buf_id, desc_count); +if (virtio_net_is_inorder (dev)) +update_dequeue_shadow_used_ring_packed_inorder (vq, buf_id, + desc_count); +else +update_dequeue_shadow_used_ring_packed (vq, buf_id, desc_count); vq->last_avail_idx += desc_count;if (vq->last_avail_idx >= vq->size) {
Signature · 1 line
2.17.1
v5 cover vhost packed ring performance optimization
In reply to Marvin LiuPacked ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v5:
- Remove disable sw prefetch as performance impact is small
- change unroll pragma macro format
- Rename shadow counter elements names
- clean dequeue update check condition
- add inline functions replace of duplicated code
- unify code style
v4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of two
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimization
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by batch
vhost: flush batched enqueue descs flags directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 +++ lib/librte_vhost/virtio_net.c | 927 +++++++++++++++++++++++++++------- 4 files changed, 814 insertions(+), 195 deletions(-)
Signature · 1 line
2.17.1
PATCH v5 1/13 vhost: add packed ring indexes increasing function
When vhost doing [de]nqueue, vq's local variable last_[used/avail]_idx will be inceased. Adding inline functions can avoid duplicated codes.
lib/librte_vhost/vhost.h +20 -0
@@ -350,6 +350,26 @@ desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) wrap_counter != !!(flags &VRING_DESC_F_USED ); } +static inline void +vq_inc_last_used_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_used_idx += num; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + +static inline void +vq_inc_last_avail_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_avail_idx += num; +if (vq->last_avail_idx >= vq->size) { + vq->avail_wrap_counter ^=1 ; + vq->last_avail_idx -= vq->size; + } +} +void __vhost_log_cache_write (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t addr,uint64_t len);
lib/librte_vhost/virtio_net.c +3 -15
@@ -140,11 +140,7 @@ flush_shadow_used_ring_packed(struct virtio_net *dev, head_flags = flags; } - vq->last_used_idx += vq->shadow_used_packed[i].count; -if (vq->last_used_idx >= vq->size) { - vq->used_wrap_counter ^=1 ; - vq->last_used_idx -= vq->size; - } +vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); } vq->desc_packed[head_idx].flags = head_flags;
@@ -865,11 +861,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,break ; } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, nr_descs); }do_data_copy_enqueue (dev, vq);
@@ -1504,11 +1496,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); } - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, desc_count); }if (likely (dev->dequeue_zero_copy ==0 )) {
Signature · 1 line
2.17.1
PATCH v5 2/13 vhost: add packed ring single enqueue
In reply to Marvin LiuAdd vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +80 -0
@@ -770,6 +770,60 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt, +struct buf_vector *buf_vec, +uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; +uint16_t avail_idx = vq->last_avail_idx; +uint16_t max_tries, tries =0 ; +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; +uint32_t size = pkt->pkt_len + dev->vhost_hlen; +uint16_t num_buffers =0 ; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) +return -1 ; + + len =RTE_MIN (len, size); + size -= len; + + num_buffers +=1 ; + + *nr_descs += desc_count; + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 ) +return -1 ; + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -827,6 +881,32 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + +vq_inc_last_avail_packed (vq, nr_descs); + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
PATCH v5 3/13 vhost: try to unroll for each loop
In reply to Marvin LiuCreate macro for adding unroll pragma before for each loop. Batch functions will be contained of several small loops which can be optimized by compilers' loop unrolling pragma.
lib/librte_vhost/Makefile +18 -0
@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endif
lib/librte_vhost/meson.build +7 -0
@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DSUPPORT_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DSUPPORT_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4
lib/librte_vhost/vhost.h +24 -0
@@ -39,6 +39,30 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) + +#ifdef SUPPORT_GCC_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("GCC unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef SUPPORT_CLANG_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef SUPPORT_ICC_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("unroll (4)") \ + for (iter = val; iter < size; iter++) +#endif + +#ifndef for_each_try_unroll +#define for_each_try_unroll (iter, val, num) \ + for (iter = val; iter < num; iter++) +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
Signature · 1 line
2.17.1
PATCH v5 4/13 vhost: add packed ring batch enqueue
In reply to Marvin LiuBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function do not support chained mbufs, single packet enqueue function will handle it.
lib/librte_vhost/virtio_net.c +70 -0
@@ -881,6 +881,76 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; + +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_t virtio_dev_rx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
Signature · 1 line
2.17.1
PATCH v5 5/13 vhost: add packed ring single dequeue
In reply to Marvin LiuAdd vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
lib/librte_vhost/virtio_net.c +55 -0
@@ -1551,6 +1551,61 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_dequeue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t *buf_id, +uint16_t *desc_count) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t dummy_len; +uint16_t nr_vec =0 ; +int err; + +if (unlikely (fill_vec_buf_packed (dev, vq, + vq->last_avail_idx, desc_count, + buf_vec, &nr_vec, + buf_id, &dummy_len, +VHOST_ACCESS_RO ) <0 )) +return -1 ; + + *pkts =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (*pkts ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA , +"Failed to allocate memory for mbuf.\n" ); +return -1 ; + } + + err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, *pkts, + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + +return 0 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ + +uint16_t buf_id, desc_count; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + +vq_inc_last_avail_packed (vq, desc_count); + +return 0 ; +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v5 6/13 vhost: add packed ring batch dequeue
In reply to Marvin LiuAdd batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
lib/librte_vhost/virtio_net.c +107 -0
113 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 7/13 vhost: flush enqueue updates by batch
In reply to Marvin LiuBuffer vhost enqueue shadowed ring flush action buffered number exceed one batch. Thus virtio can receive packets at a faster frequency.
lib/librte_vhost/vhost.h +2 -0
@@ -166,6 +166,8 @@ struct vhost_virtqueue {struct vring_used_elem_packed *shadow_used_packed; };uint16_t shadow_used_idx; +/* Record packed ring enqueue latest desc cache aligned index */ +uint16_t shadow_aligned_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +100 -1
137 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 8/13 vhost: flush batched enqueue descs flags directly
In reply to Marvin LiuFlush used flags when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ + VRING_DESC_F_WRITE)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
lib/librte_vhost/virtio_net.c +36 -0
@@ -154,6 +154,36 @@ vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } +static __rte_always_inline void +vhost_flush_enqueue_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +uint64_t *lens, +uint16_t *ids) +{ +uint16_t i; +uint16_t flags; + + flags =PACKED_DESC_ENQUEUE_USED_FLAG (vq->used_wrap_counter); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + vq->desc_packed[vq->last_used_idx + i].id = ids[i]; + vq->desc_packed[vq->last_used_idx + i].len = lens[i]; + } + +rte_smp_wmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + vq->desc_packed[vq->last_used_idx + i].flags = flags; + +vhost_log_cache_used_vring (dev, vq, vq->last_used_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc ) * +PACKED_BATCH_SIZE ); +vhost_log_cache_sync (dev, vq); + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline voidflush_shadow_used_ring_packed (struct virtio_net *dev,struct vhost_virtqueue *vq)
@@ -992,6 +1022,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ];uint32_t buf_offset = dev->vhost_hlen;uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ];uint16_t i;if (unlikely (avail_idx &PACKED_BATCH_MASK ))
@@ -1047,6 +1078,11 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, pkts[i]->pkt_len); } +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + ids[i] = descs[avail_idx + i].id; + +vhost_flush_enqueue_batch_packed (dev, vq, lens, ids); +return 0 ; }
Signature · 1 line
2.17.1
PATCH v5 9/13 vhost: buffer packed ring dequeue updates
In reply to Marvin LiuBuffer used ring updates as many as possible in vhost dequeue function for coordinating with virtio driver. For supporting buffer, shadow used ring element should contain descriptor's flags. First shadowed ring index was recorded for calculating buffered number.
lib/librte_vhost/vhost.h +5 -0
@@ -42,6 +42,8 @@#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ VRING_DESC_F_WRITE) +#define PACKED_DESC_DEQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
@@ -114,6 +116,7 @@ struct log_cache_entry {struct vring_used_elem_packed {uint16_t id; +uint16_t flags;uint32_t len;uint32_t count; };
@@ -171,6 +174,8 @@ struct vhost_virtqueue {uint16_t shadow_used_idx;/* Record packed ring enqueue latest desc cache aligned index */ uint16_t shadow_aligned_idx; +/* Record packed ring first dequeue desc index */ +uint16_t shadow_last_used_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +105 -0
135 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v5 10/13 vhost: optimize packed ring enqueue
In reply to Marvin LiuOptimize vhost device packed ring enqueue function by splitting batch and single functions. Packets can be filled into one desc will be handled by batch and others will be handled by single as before.
lib/librte_vhost/virtio_net.c +24 -91
@@ -772,64 +772,6 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t size,struct buf_vector *buf_vec, -uint16_t *nr_vec,uint16_t *num_buffers, -uint16_t *nr_descs) -{ -uint16_t avail_idx; -uint16_t vec_idx =0 ; -uint16_t max_tries, tries =0 ; - -uint16_t buf_id =0 ; -uint32_t len =0 ; -uint16_t desc_count; - - *num_buffers =0 ; - avail_idx = vq->last_avail_idx; - -if (rxvq_is_mergeable (dev)) - max_tries = vq->size -1 ; -else - max_tries =1 ; - -while (size >0 ) { -/* -* if we tried all available ring items, and still -* can't get enough buf, it means something abnormal -* happened. -*/ -if (unlikely (++tries > max_tries)) -return -1 ; - -if (unlikely (fill_vec_buf_packed (dev, vq, - avail_idx, &desc_count, - buf_vec, &vec_idx, - &buf_id, &len, -VHOST_ACCESS_RW ) <0 )) -return -1 ; - - len =RTE_MIN (len, size); -update_shadow_used_ring_packed (vq, buf_id, len, desc_count); - size -= len; - - avail_idx += desc_count; -if (avail_idx >= vq->size) - avail_idx -= vq->size; - - *nr_descs += desc_count; - *num_buffers +=1 ; - } - - *nr_vec = vec_idx; - -return 0 ; -} - static __rte_noinline void copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, struct buf_vector *buf_vec,
@@ -1111,7 +1053,7 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_rx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts)
@@ -1187,7 +1129,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int16_t +static __rte_always_inline int16_tvirtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *pkt)
@@ -1214,49 +1156,40 @@ virtio_dev_rx_single_packed(struct virtio_net *dev, } static __rte_noinline uint32_t -virtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mbuf **pkts,uint32_t count) +virtio_dev_rx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts, +uint32_t count) {uint32_t pkt_idx =0 ; -uint16_t num_buffers; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t remained = count; -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; -uint16_t nr_vec =0 ; -uint16_t nr_descs =0 ; +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); -if (unlikely (reserve_avail_buf_packed (dev, vq, - pkt_len, buf_vec, &nr_vec, - &num_buffers, &nr_descs) <0 )) { -VHOST_LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); - vq->shadow_used_idx -= num_buffers; -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_rx_batch_packed (dev, vq, pkts)) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, vq->last_avail_idx, - vq->last_avail_idx + num_buffers); - -if (copy_mbuf_to_desc (dev, vq, pkts[pkt_idx], - buf_vec, nr_vec, - num_buffers) <0 ) { - vq->shadow_used_idx -= num_buffers; +if (virtio_dev_rx_single_packed (dev, vq, pkts[pkt_idx]))break ; - } + pkt_idx++; + remained--; -vq_inc_last_avail_packed (vq, nr_descs); - } - -do_data_copy_enqueue (dev, vq); + }while (pkt_idx < count); -if (likely (vq->shadow_used_idx)) { +if (vq->shadow_used_idx) { +do_data_copy_enqueue (dev, vq);vhost_flush_enqueue_shadow_packed (dev, vq); -vhost_vring_call_packed (dev, vq); } +if (pkt_idx) +vhost_vring_call_packed (dev, vq); +return pkt_idx; }
Signature · 1 line
2.17.1
PATCH v5 11/13 vhost: add packed ring zcopy batch and single dequeue
In reply to Marvin LiuAdd vhost packed ring zero copy batch and single dequeue functions like normal dequeue path.
lib/librte_vhost/virtio_net.c +116 -0
@@ -1886,6 +1886,122 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } +static __rte_unused int +virtio_dev_tx_batch_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE ]; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +uint16_t avail_idx = vq->last_avail_idx; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + zmbufs[i] =get_zmbuf (vq); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (!zmbufs[i]) + goto free_pkt; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + zmbufs[i]->mbuf = pkts[i]; + zmbufs[i]->desc_idx = avail_idx + i; + zmbufs[i]->desc_count =1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_mbuf_refcnt_update (pkts[i],1 ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbufs[i], next); + + vq->nr_zmbuf +=PACKED_BATCH_SIZE ; +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; + +free_pkt: +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t buf_id, desc_count; +struct zcopy_mbuf *zmbuf; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + zmbuf =get_zmbuf (vq); +if (!zmbuf) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + zmbuf->mbuf = *pkts; + zmbuf->desc_idx = vq->last_avail_idx; + zmbuf->desc_count = desc_count; + +rte_mbuf_refcnt_update (*pkts,1 ); + + vq->nr_zmbuf +=1 ; +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); + +vq_inc_last_avail_packed (vq, desc_count); +return 0 ; +} + +static __rte_always_inline void +free_zmbuf (struct vhost_virtqueue *vq) +{ +struct zcopy_mbuf *next =NULL ; +struct zcopy_mbuf *zmbuf; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +uint16_t last_used_idx = vq->last_used_idx; + +if (mbuf_is_consumed (zmbuf->mbuf)) { +uint16_t flags =0 ; +bool wrap; + + wrap = vq->used_wrap_counter; + flags =PACKED_DESC_DEQUEUE_USED_FLAG (wrap); + + vq->desc_packed[last_used_idx].id = zmbuf->desc_idx; + vq->desc_packed[last_used_idx].len =0 ; + +rte_smp_wmb (); + vq->desc_packed[last_used_idx].flags = flags; + +vq_inc_last_used_packed (vq, zmbuf->desc_count); + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +restore_mbuf (zmbuf->mbuf); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + } + } +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v5 12/13 vhost: optimize packed ring dequeue
In reply to Marvin LiuOptimize vhost device packed ring dequeue function by splitting batch and single functions. No-chained and direct descriptors will be handled by batch and other will be handled by single as before.
lib/librte_vhost/virtio_net.c +66 -171
@@ -201,69 +201,6 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } -static __rte_always_inline void -flush_shadow_used_ring_packed (struct virtio_net *dev, -struct vhost_virtqueue *vq) -{ -int i; -uint16_t used_idx = vq->last_used_idx; -uint16_t head_idx = vq->last_used_idx; -uint16_t head_flags =0 ; - -/* Split loop in two to save memory barriers */ -for (i =0 ; i < vq->shadow_used_idx; i++) { - vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id; - vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len; - - used_idx += vq->shadow_used_packed[i].count; -if (used_idx >= vq->size) - used_idx -= vq->size; - } - -rte_smp_wmb (); - -for (i =0 ; i < vq->shadow_used_idx; i++) { -uint16_t flags; - -if (vq->shadow_used_packed[i].len) - flags =VRING_DESC_F_WRITE ; -else - flags =0 ; - -if (vq->used_wrap_counter) { - flags |=VRING_DESC_F_USED ; - flags |=VRING_DESC_F_AVAIL ; - }else { - flags &= ~VRING_DESC_F_USED ; - flags &= ~VRING_DESC_F_AVAIL ; - } - -if (i >0 ) { - vq->desc_packed[vq->last_used_idx].flags = flags; - -vhost_log_cache_used_vring (dev, vq, - vq->last_used_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - }else { - head_idx = vq->last_used_idx; - head_flags = flags; - } - -vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); - } - - vq->desc_packed[head_idx].flags = head_flags; - -vhost_log_cache_used_vring (dev, vq, - head_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - - vq->shadow_used_idx =0 ; -vhost_log_cache_sync (dev, vq); -} - static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -328,17 +265,6 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } -static __rte_always_inline void -update_shadow_used_ring_packed (struct vhost_virtqueue *vq, -uint16_t desc_idx,uint32_t len,uint16_t count) -{ -uint16_t i = vq->shadow_used_idx++; - - vq->shadow_used_packed[i].id = desc_idx; - vq->shadow_used_packed[i].len = len; - vq->shadow_used_packed[i].count = count; -} -static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -395,7 +321,7 @@ vhost_shadow_enqueue_single_packed(struct virtio_net *dev, } } -static __rte_unused void +static __rte_always_inline voidvhost_flush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1799,7 +1725,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1866,7 +1792,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1886,7 +1812,7 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1935,7 +1861,7 @@ virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -2003,114 +1929,78 @@ free_zmbuf(struct vhost_virtqueue *vq) } static __rte_noinline uint16_t -virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) +virtio_dev_tx_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) { -uint16_t i; - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf, *next; +uint32_t pkt_idx =0 ; +uint32_t remained = count; -for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); - zmbuf !=NULL ; zmbuf = next) { - next =TAILQ_NEXT (zmbuf, next); +free_zmbuf (vq); -if (mbuf_is_consumed (zmbuf->mbuf)) { -update_shadow_used_ring_packed (vq, - zmbuf->desc_idx, -0 , - zmbuf->desc_count); - -TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); -restore_mbuf (zmbuf->mbuf); -rte_pktmbuf_free (zmbuf->mbuf); -put_zmbuf (zmbuf); - vq->nr_zmbuf -=1 ; +do { +if (remained >=PACKED_BATCH_SIZE ) { +if (virtio_dev_tx_batch_packed_zmbuf (dev, vq, + mbuf_pool, + &pkts[pkt_idx])) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; } } -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } +if (virtio_dev_tx_single_packed_zmbuf (dev, vq, mbuf_pool, + &pkts[pkt_idx])) +break ; + pkt_idx++; + remained--; -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); + }while (remained); - count =RTE_MIN (count,MAX_PKT_BURST ); -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) about to dequeue %u buffers\n" , - dev->vid, count); +if (pkt_idx) +vhost_vring_call_packed (dev, vq); -for (i =0 ; i < count; i++) { -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; -uint16_t buf_id; -uint32_t dummy_len; -uint16_t desc_count, nr_vec =0 ; -int err; +return pkt_idx; +} -if (unlikely (fill_vec_buf_packed (dev, vq, - vq->last_avail_idx, &desc_count, - buf_vec, &nr_vec, - &buf_id, &dummy_len, -VHOST_ACCESS_RO ) <0 )) -break ; +static __rte_noinline uint16_t +virtio_dev_tx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) +{ +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (likely (dev->dequeue_zero_copy ==0 )) -update_shadow_used_ring_packed (vq, buf_id,0 , - desc_count); +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); - pkts[i] =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (pkts[i] ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) { +vhost_flush_dequeue_packed (dev, vq); + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } - err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, pkts[i], - mbuf_pool); -if (unlikely (err)) { -rte_pktmbuf_free (pkts[i]); +if (virtio_dev_tx_single_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx]))break ; - } - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf; - - zmbuf =get_zmbuf (vq); -if (!zmbuf) { -rte_pktmbuf_free (pkts[i]); -break ; - } - zmbuf->mbuf = pkts[i]; - zmbuf->desc_idx = buf_id; - zmbuf->desc_count = desc_count; - -/* -* Pin lock the mbuf; we will check later to see -* whether the mbuf is freed (when we are the last -* user) or not. If that's the case, we then could -* update the used ring safely. -*/ -rte_mbuf_refcnt_update (pkts[i],1 ); - - vq->nr_zmbuf +=1 ; -TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); - } +vhost_flush_dequeue_packed (dev, vq); + pkt_idx++; + remained--; -vq_inc_last_avail_packed (vq, desc_count); - } + }while (remained); -if (likely (dev->dequeue_zero_copy ==0 )) { +if (vq->shadow_used_idx)do_data_copy_dequeue (vq); -if (unlikely (i < count)) - vq->shadow_used_idx = i; -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } -return i; +return pkt_idx; }uint16_t
@@ -2186,9 +2076,14 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, count -=1 ; } -if (vq_is_packed (dev)) - count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, count); -else +if (vq_is_packed (dev)) { +if (unlikely (dev->dequeue_zero_copy)) + count =virtio_dev_tx_packed_zmbuf (dev, vq, mbuf_pool, + pkts, count); +else + count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, + count); + }else count =virtio_dev_tx_split (dev, vq, mbuf_pool, pkts, count); out:
Signature · 1 line
2.17.1
PATCH v5 13/13 vhost: optimize packed ring dequeue when in-order
In reply to Marvin LiuWhen VIRTIO_F_IN_ORDER feature is negotiated, vhost can optimize dequeue function by only update first used descriptor.
lib/librte_vhost/virtio_net.c +55 -2
@@ -31,6 +31,12 @@ rxvq_is_mergeable(struct virtio_net *dev)return dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF ); } +static __rte_always_inline bool +virtio_net_is_inorder (struct virtio_net *dev) +{ +return dev->features & (1ULL <<VIRTIO_F_IN_ORDER ); +} +static bool is_valid_virt_queue_idx (uint32_t idx,int is_tx,uint32_t nr_vring) {
@@ -201,6 +207,25 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } +static __rte_always_inline void +vhost_shadow_dequeue_batch_packed_inorder (struct vhost_virtqueue *vq, +uint16_t id) +{ + vq->shadow_used_packed[0 ].id = id; + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].flags = +PACKED_DESC_DEQUEUE_USED_FLAG (vq->used_wrap_counter); + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -265,6 +290,26 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } +static __rte_always_inline void +vhost_shadow_dequeue_single_packed_inorder (struct vhost_virtqueue *vq, +uint16_t buf_id, +uint16_t count) +{ + vq->shadow_used_packed[0 ].id = buf_id; + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count = count; + vq->shadow_used_packed[0 ].flags = +PACKED_DESC_DEQUEUE_USED_FLAG (vq->used_wrap_counter); + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq, count); +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1748,7 +1793,11 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, pkts[i]->pkt_len); } -vhost_shadow_dequeue_batch_packed (dev, vq, ids); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_batch_packed_inorder (vq, + ids[PACKED_BATCH_SIZE -1 ]); +else +vhost_shadow_dequeue_batch_packed (dev, vq, ids);vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE );
@@ -1805,7 +1854,11 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, &desc_count))return -1 ; -vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_single_packed_inorder (vq, buf_id, + desc_count); +else +vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count);vq_inc_last_avail_packed (vq, desc_count);
Signature · 1 line
2.17.1
v6 cover vhost packed ring performance optimization
In reply to Marvin LiuPacked ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v6:
- Fix dequeue zcopy result check
v5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code style
v4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of two
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimization
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by batch
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 +++ lib/librte_vhost/virtio_net.c | 924 +++++++++++++++++++++++++++------- 4 files changed, 812 insertions(+), 194 deletions(-)
Signature · 1 line
2.17.1
PATCH v6 1/13 vhost: add packed ring indexes increasing function
When vhost doing [de]nqueue, vq's local variable last_[used/avail]_idx will be inceased. Adding inline functions can avoid duplicated codes.
lib/librte_vhost/vhost.h +20 -0
@@ -350,6 +350,26 @@ desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) wrap_counter != !!(flags &VRING_DESC_F_USED ); } +static inline void +vq_inc_last_used_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_used_idx += num; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + +static inline void +vq_inc_last_avail_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_avail_idx += num; +if (vq->last_avail_idx >= vq->size) { + vq->avail_wrap_counter ^=1 ; + vq->last_avail_idx -= vq->size; + } +} +void __vhost_log_cache_write (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t addr,uint64_t len);
lib/librte_vhost/virtio_net.c +3 -15
@@ -140,11 +140,7 @@ flush_shadow_used_ring_packed(struct virtio_net *dev, head_flags = flags; } - vq->last_used_idx += vq->shadow_used_packed[i].count; -if (vq->last_used_idx >= vq->size) { - vq->used_wrap_counter ^=1 ; - vq->last_used_idx -= vq->size; - } +vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); } vq->desc_packed[head_idx].flags = head_flags;
@@ -865,11 +861,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,break ; } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, nr_descs); }do_data_copy_enqueue (dev, vq);
@@ -1504,11 +1496,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); } - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, desc_count); }if (likely (dev->dequeue_zero_copy ==0 )) {
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 01/13] vhost: add packed ring indexes increasing function
Quoted 2 lines · Marvin Liu
When vhost doing [de]nqueue, vq's local variable last_[used/avail]_idx will be inceased. Adding inline functions can avoid duplicated codes.
When enqueuing or dequeuing, the virtqueue's local available and used indexes are increased.
Other than that:
Quoted 78 lines
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +20
-0@@ -350,6 +350,26 @@ desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) wrap_counter != !!(flags &VRING_DESC_F_USED ); } +static inline void +vq_inc_last_used_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_used_idx += num; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + +static inline void +vq_inc_last_avail_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_avail_idx += num; +if (vq->last_avail_idx >= vq->size) { + vq->avail_wrap_counter ^=1 ; + vq->last_avail_idx -= vq->size; + } +} +void __vhost_log_cache_write (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t addr,uint64_t len);lib/librte_vhost/virtio_net.c +3
-15@@ -140,11 +140,7 @@ flush_shadow_used_ring_packed(struct virtio_net *dev, head_flags = flags; } - vq->last_used_idx += vq->shadow_used_packed[i].count; -if (vq->last_used_idx >= vq->size) { - vq->used_wrap_counter ^=1 ; - vq->last_used_idx -= vq->size; - } +vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); } vq->desc_packed[head_idx].flags = head_flags;@@ -865,11 +861,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,break ; } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, nr_descs); }do_data_copy_enqueue (dev, vq);@@ -1504,11 +1496,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); } - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, desc_count); }if (likely (dev->dequeue_zero_copy ==0 )) {
PATCH v6 2/13 vhost: add packed ring single enqueue
In reply to Marvin LiuAdd vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +80 -0
@@ -770,6 +770,60 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt, +struct buf_vector *buf_vec, +uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; +uint16_t avail_idx = vq->last_avail_idx; +uint16_t max_tries, tries =0 ; +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; +uint32_t size = pkt->pkt_len + dev->vhost_hlen; +uint16_t num_buffers =0 ; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) +return -1 ; + + len =RTE_MIN (len, size); + size -= len; + + num_buffers +=1 ; + + *nr_descs += desc_count; + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 ) +return -1 ; + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -827,6 +881,32 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + +vq_inc_last_avail_packed (vq, nr_descs); + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
PATCH v6 3/13 vhost: try to unroll for each loop
In reply to Marvin LiuCreate macro for adding unroll pragma before for each loop. Batch functions will be contained of several small loops which can be optimized by compilers' loop unrolling pragma.
lib/librte_vhost/Makefile +18 -0
@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endif
lib/librte_vhost/meson.build +7 -0
@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DSUPPORT_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DSUPPORT_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4
lib/librte_vhost/vhost.h +24 -0
@@ -39,6 +39,30 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) + +#ifdef SUPPORT_GCC_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("GCC unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef SUPPORT_CLANG_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef SUPPORT_ICC_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("unroll (4)") \ + for (iter = val; iter < size; iter++) +#endif + +#ifndef for_each_try_unroll +#define for_each_try_unroll (iter, val, num) \ + for (iter = val; iter < num; iter++) +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 03/13] vhost: try to unroll for each loop
Hi Marvin,
Quoted 79 lines · Marvin Liu
Create macro for adding unroll pragma before for each loop. Batch functions will be contained of several small loops which can be optimized by compilers' loop unrolling pragma.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/Makefile +18
-0@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DSUPPORT_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DSUPPORT_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DSUPPORT_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endiflib/librte_vhost/meson.build +7
-0@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DSUPPORT_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DSUPPORT_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DSUPPORT_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4lib/librte_vhost/vhost.h +17
-0@@ -39,6 +39,30 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) + +#ifdef SUPPORT_GCC_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("GCC unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef SUPPORT_CLANG_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef SUPPORT_ICC_UNROLL_PRAGMA +#define for_each_try_unroll (iter, val, size) _Pragma("unroll (4)") \ + for (iter = val; iter < size; iter++)
lib/librte_vhost/vhost.h line 68
Quoted 10 lines
lib/librte_vhost/vhost.h +7
-0+#endif + +#ifndef for_each_try_unroll +#define for_each_try_unroll (iter, val, num) \ + for (iter = val; iter < num; iter++) +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
As it is Vhost specific, please prefix all the defines and macros with VHOST_.
Thanks,
Maxime
PATCH v6 4/13 vhost: add packed ring batch enqueue
In reply to Marvin LiuBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function do not support chained mbufs, single packet enqueue function will handle it.
lib/librte_vhost/virtio_net.c +70 -0
@@ -881,6 +881,76 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; + +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_t virtio_dev_rx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 04/13] vhost: add packed ring batch enqueue
Hi Marvin,
Quoted 76 lines
-----Original Message-----
From: Marvin Liu <[email protected]>
Sent: Wednesday, October 16, 2019 12:08 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; Gavin Hu (Arm Technology China) <[email protected]>
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [PATCH v6 04/13] vhost: add packed ring batch enqueueBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function do not support chained mbufs, single packet enqueue function will handle it.
Signed-off-by Marvin Liu <[email protected]>Reviewed-by Maxime Coquelin <[email protected]>lib/librte_vhost/virtio_net.c +50
-0@@ -881,6 +881,76 @@ virtio_dev_rx_split(struct virtio_net *dev, structvhost_virtqueue *vq, return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; + +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) {
lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct · line 944
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE );
Is the last_avail_idx a shared variable? Why is updated before the following payload copy? This will cause the other side get earlier-than-arrival data?
/Gavin
Quoted 15 lines
lib/librte_vhost/virtio_net.c +10
-0+ +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_t virtio_dev_rx_single_packed(struct virtio_net *dev,struct
vhost_virtqueue*vq,
--
2.17.1
Re: [dpdk-dev] [PATCH v6 04/13] vhost: add packed ring batch enqueue
Quoted 97 lines
-----Original Message-----
From: Gavin Hu (Arm Technology China) [mailto:[email protected]]
Sent: Tuesday, October 15, 2019 7:36 PM
To: Liu, Yong <[email protected]>; [email protected]; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]
Cc: [email protected]; nd <[email protected]>
Subject: RE: [PATCH v6 04/13] vhost: add packed ring batch enqueueHi Marvin,
Quoted 14 lines
-----Original Message-----
From: Marvin Liu <[email protected]>
Sent: Wednesday, October 16, 2019 12:08 AM
To: [email protected]; [email protected]; [email protected]; [email protected]; Gavin Hu (Arm Technology China) <[email protected]>
Cc: [email protected]; Marvin Liu <[email protected]>
Subject: [PATCH v6 04/13] vhost: add packed ring batch enqueueBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function do not support chained mbufs, single packet enqueue function will handle it.
Review
Quoted 4 lines
Signed-off-by Marvin Liu <[email protected]>Reviewed-by Maxime Coquelin <[email protected]>(quoted diff) +0
-0
b/lib/librte_vhost/virtio_net.cQuoted 65 lines
index
142c14e04..a8130dc06100644lib/librte_vhost/virtio_net.c +57
-0@@ -881,6 +881,76 @@ virtio_dev_rx_split(struct virtio_net *dev, structvhost_virtqueue *vq, return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; + +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE )
Quoted 8 lines
lib/librte_vhost/virtio_net.c virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 943
Quoted 3 lines
lib/librte_vhost/virtio_net.c +3
-0+virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE );Is the
last_avail_idxa shared variable? Why is updated before the following payload copy?
This will cause the other side get earlier-than-arrival data? /Gavin
Hi Gavin,Last_avail_idx and last_used_idx are all vhost local variables. They are used for tracking next available and used index of virtqueue. Last avail_idx value should increase after descs are consumed. Last used_idx value should increase after descs flags are updated.
Thanks,
Marvin
Quoted 15 lines
Quoted 15 lines
lib/librte_vhost/virtio_net.c +10
-0+ +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_t virtio_dev_rx_single_packed(struct virtio_net *dev,struct
vhost_virtqueue*vq,
--
2.17.1
PATCH v6 5/13 vhost: add packed ring single dequeue
In reply to Marvin LiuAdd vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
lib/librte_vhost/virtio_net.c +55 -0
@@ -1551,6 +1551,61 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_dequeue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t *buf_id, +uint16_t *desc_count) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t dummy_len; +uint16_t nr_vec =0 ; +int err; + +if (unlikely (fill_vec_buf_packed (dev, vq, + vq->last_avail_idx, desc_count, + buf_vec, &nr_vec, + buf_id, &dummy_len, +VHOST_ACCESS_RO ) <0 )) +return -1 ; + + *pkts =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (*pkts ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA , +"Failed to allocate memory for mbuf.\n" ); +return -1 ; + } + + err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, *pkts, + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + +return 0 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ + +uint16_t buf_id, desc_count; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + +vq_inc_last_avail_packed (vq, desc_count); + +return 0 ; +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v6 6/13 vhost: add packed ring batch dequeue
In reply to Marvin LiuAdd batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
lib/librte_vhost/virtio_net.c +107 -0
113 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 06/13] vhost: add packed ring batch dequeue
Quoted 115 lines · Marvin Liu
Add batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +3
-0@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)lib/librte_vhost/virtio_net.c +87
-0@@ -1551,6 +1551,113 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_reserve_avail_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t avail_idx, +uintptr_t *desc_addrs, +uint16_t *ids) +{ +bool wrap = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +struct virtio_net_hdr *hdr; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint64_t buf_lens[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint16_t flags, i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + flags = descs[avail_idx + i].flags; +if (unlikely ((wrap != !!(flags &VRING_DESC_F_AVAIL )) || + (wrap == !!(flags &VRING_DESC_F_USED )) || + (flags &PACKED_DESC_SINGLE_DEQUEUE_FLAG ))) +return -1 ; + } + +rte_smp_rmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i],VHOST_ACCESS_RW ); + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely ((lens[i] != descs[avail_idx + i].len))) +return -1 ; + } + +if (rte_pktmbuf_alloc_bulk (mbuf_pool, pkts,PACKED_BATCH_SIZE )) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (buf_lens[i] < (lens[i] - buf_offset))) + goto free_buf; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset; + pkts[i]->data_len = pkts[i]->pkt_len; + ids[i] = descs[avail_idx + i].id; + } + +if (virtio_net_with_host_offload (dev)) { +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + hdr = (struct virtio_net_hdr *)(desc_addrs[i]); +vhost_dequeue_offload (hdr, pkts[i]); + } + } + +return 0 ; + +free_buf: +for (i =0 ; i <PACKED_BATCH_SIZE ; i++) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t avail_idx = vq->last_avail_idx; +uint32_t buf_offset = dev->vhost_hlen;
lib/librte_vhost/virtio_net.c virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 1650
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]);
Wouldn't we have better performance with a dedicated unroll loop for the prefetches, so that desc_addrs[i+1] is being prefetched while the desc_addr[i] memcpy is done?
Quoted 14 lines
lib/librte_vhost/virtio_net.c +10
-0+ rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + (void *)(uintptr_t)(desc_addrs[i] + buf_offset), + pkts[i]->pkt_len); + } + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; +} + static __rte_always_inline int vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
Re: [dpdk-dev] [PATCH v6 06/13] vhost: add packed ring batch dequeue
Quoted 137 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Wednesday, October 16, 2019 6:36 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v6 06/13] vhost: add packed ring batch dequeueReview
Quoted 21 lines · Marvin Liu
Add batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +3
-0@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)(quoted diff) +0
-0
b/lib/librte_vhost/virtio_net.clib/librte_vhost/virtio_net.c virtio_dev_tx_split(struct virtio_net *dev,
Quoted 4 lines
index
e1b06c1ce..274a28f99100644lib/librte_vhost/virtio_net.c +0
-0@@ -1551,6 +1551,113 @@ virtio_dev_tx_split(struct virtio_net *dev,struct
vhost_virtqueue*vq,Quoted 96 lines
lib/librte_vhost/virtio_net.c +93
-0return i; } +static __rte_always_inline int +vhost_reserve_avail_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t avail_idx, +uintptr_t *desc_addrs, +uint16_t *ids) +{ +bool wrap = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +struct virtio_net_hdr *hdr; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint64_t buf_lens[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint16_t flags, i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + flags = descs[avail_idx + i].flags; +if (unlikely ((wrap != !!(flags &VRING_DESC_F_AVAIL )) || + (wrap == !!(flags &VRING_DESC_F_USED )) || + (flags &PACKED_DESC_SINGLE_DEQUEUE_FLAG ))) +return -1 ; + } + +rte_smp_rmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i],VHOST_ACCESS_RW ); + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely ((lens[i] != descs[avail_idx + i].len))) +return -1 ; + } + +if (rte_pktmbuf_alloc_bulk (mbuf_pool, pkts,PACKED_BATCH_SIZE )) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (buf_lens[i] < (lens[i] - buf_offset))) + goto free_buf; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset; + pkts[i]->data_len = pkts[i]->pkt_len; + ids[i] = descs[avail_idx + i].id; + } + +if (virtio_net_with_host_offload (dev)) { +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + hdr = (struct virtio_net_hdr *)(desc_addrs[i]); +vhost_dequeue_offload (hdr, pkts[i]); + } + } + +return 0 ; + +free_buf: +for (i =0 ; i <PACKED_BATCH_SIZE ; i++) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t avail_idx = vq->last_avail_idx; +uint32_t buf_offset = dev->vhost_hlen; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids))
Quoted 9 lines
lib/librte_vhost/virtio_net.c virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 1650
Quoted 4 lines
lib/librte_vhost/virtio_net.c +4
-0+return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]);Wouldn't we have better performance with a dedicated unroll loop for the prefetches, so that desc_addrs[i+1] is being prefetched while the desc_addr[i] memcpy is done?
Thanks, Maxime. It will be slightly better. Will have dedicated unroll loop in next version.
Regards,
marvin
Quoted 14 lines
Quoted 14 lines
lib/librte_vhost/virtio_net.c +10
-0+ rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + (void *)(uintptr_t)(desc_addrs[i] + buf_offset), + pkts[i]->pkt_len); + } + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; +} + static __rte_always_inline int vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
PATCH v6 7/13 vhost: flush enqueue updates by batch
In reply to Marvin LiuBuffer vhost enqueue shadowed ring flush action buffered number exceed one batch. Thus virtio can receive packets at a faster frequency.
lib/librte_vhost/vhost.h +2 -0
@@ -166,6 +166,8 @@ struct vhost_virtqueue {struct vring_used_elem_packed *shadow_used_packed; };uint16_t shadow_used_idx; +/* Record packed ring enqueue latest desc cache aligned index */ +uint16_t shadow_aligned_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +100 -1
137 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 07/13] vhost: flush enqueue updates by batch
Quoted 2 lines · Marvin Liu
Buffer vhost enqueue shadowed ring flush action buffered number exceed one batch. Thus virtio can receive packets at a faster frequency.
The commit message isn't very clear to me. Could you please improve it?
Quoted 124 lines
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +2
-0@@ -166,6 +166,8 @@ struct vhost_virtqueue {struct vring_used_elem_packed *shadow_used_packed; };uint16_t shadow_used_idx; +/* Record packed ring enqueue latest desc cache aligned index */ +uint16_t shadow_aligned_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;lib/librte_vhost/virtio_net.c +90
-0@@ -91,6 +91,69 @@ update_shadow_used_ring_split(struct vhost_virtqueue *vq, vq->shadow_used_split[i].len = len; } +static __rte_always_inline void +vhost_flush_enqueue_shadow_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq) +{ +int i; +uint16_t used_idx = vq->last_used_idx; +uint16_t head_idx = vq->last_used_idx; +uint16_t head_flags =0 ; + +/* Split loop in two to save memory barriers */ +for (i =0 ; i < vq->shadow_used_idx; i++) { + vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id; + vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len; + + used_idx += vq->shadow_used_packed[i].count; +if (used_idx >= vq->size) + used_idx -= vq->size; + } + +rte_smp_wmb (); + +for (i =0 ; i < vq->shadow_used_idx; i++) { +uint16_t flags; + +if (vq->shadow_used_packed[i].len) + flags =VRING_DESC_F_WRITE ; +else + flags =0 ; + +if (vq->used_wrap_counter) { + flags |=VRING_DESC_F_USED ; + flags |=VRING_DESC_F_AVAIL ; + }else { + flags &= ~VRING_DESC_F_USED ; + flags &= ~VRING_DESC_F_AVAIL ; + } + +if (i >0 ) { + vq->desc_packed[vq->last_used_idx].flags = flags; + +vhost_log_cache_used_vring (dev, vq, + vq->last_used_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc )); + }else { + head_idx = vq->last_used_idx; + head_flags = flags; + } + +vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); + } + + vq->desc_packed[head_idx].flags = head_flags; + +vhost_log_cache_used_vring (dev, vq, + head_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc )); + + vq->shadow_used_idx =0 ; +vhost_log_cache_sync (dev, vq); +} + static __rte_always_inline voidflush_shadow_used_ring_packed (struct virtio_net *dev,struct vhost_virtqueue *vq)@@ -194,6 +257,33 @@ do_data_copy_dequeue(struct vhost_virtqueue *vq) vq->batch_copy_nb_elems =0 ; } +static __rte_always_inline void +vhost_shadow_enqueue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +uint32_t len[], +uint16_t id[], +uint16_t count[], +uint16_t num_buffers) +{ +uint16_t i; +for (i =0 ; i < num_buffers; i++) { +/* enqueue shadow flush action aligned with batch num */ +if (!vq->shadow_used_idx) + vq->shadow_aligned_idx = vq->last_used_idx & +PACKED_BATCH_MASK ; + vq->shadow_used_packed[vq->shadow_used_idx].id = id[i]; + vq->shadow_used_packed[vq->shadow_used_idx].len = len[i]; + vq->shadow_used_packed[vq->shadow_used_idx].count = count[i]; + vq->shadow_aligned_idx += count[i]; + vq->shadow_used_idx++; + } + +if (vq->shadow_aligned_idx >=PACKED_BATCH_SIZE ) { +do_data_copy_enqueue (dev, vq); +vhost_flush_enqueue_shadow_packed (dev, vq); + } +} +/* avoid write operation when necessary, to lessen cache issues */ #define ASSIGN_UNLESS_EQUAL (var, val) do { \ if ((var) != (val)) \
lib/librte_vhost/virtio_net.c vhost_enqueue_single_packed(struct virtio_net *dev, · line 880
Quoted 6 lines
lib/librte_vhost/virtio_net.c +3
-0@@ -785,6 +875,9 @@ vhost_enqueue_single_packed(struct virtio_net *dev,uint16_t desc_count;uint32_t size = pkt->pkt_len + dev->vhost_hlen;uint16_t num_buffers =0 ; +uint32_t buffer_len[vq->size]; +uint16_t buffer_buf_id[vq->size]; +uint16_t buffer_desc_count[vq->size];
With rings up to 1024 elements, maybe it would be better to have that allocated as vq metadata like shadow_used_packed?
Quoted 33 lines
lib/librte_vhost/virtio_net.c +7
-1if (rxvq_is_mergeable (dev)) max_tries = vq->size -1 ;@@ -810,6 +903,9 @@ vhost_enqueue_single_packed(struct virtio_net *dev, len =RTE_MIN (len, size); size -= len; + buffer_len[num_buffers] = len; + buffer_buf_id[num_buffers] = buf_id; + buffer_desc_count[num_buffers] = desc_count; num_buffers +=1 ; *nr_descs += desc_count;@@ -821,6 +917,9 @@ vhost_enqueue_single_packed(struct virtio_net *dev,if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 )return -1 ; +vhost_shadow_enqueue_single_packed (dev, vq, buffer_len, buffer_buf_id, + buffer_desc_count, num_buffers); +return 0 ; }@@ -1017,7 +1116,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,do_data_copy_enqueue (dev, vq);if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); +vhost_flush_enqueue_shadow_packed (dev, vq);vhost_vring_call_packed (dev, vq); }
PATCH v6 8/13 vhost: flush batched enqueue descs directly
In reply to Marvin LiuFlush used elements when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ + VRING_DESC_F_WRITE)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
lib/librte_vhost/virtio_net.c +36 -0
@@ -154,6 +154,36 @@ vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } +static __rte_always_inline void +vhost_flush_enqueue_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +uint64_t *lens, +uint16_t *ids) +{ +uint16_t i; +uint16_t flags; + + flags =PACKED_DESC_ENQUEUE_USED_FLAG (vq->used_wrap_counter); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + vq->desc_packed[vq->last_used_idx + i].id = ids[i]; + vq->desc_packed[vq->last_used_idx + i].len = lens[i]; + } + +rte_smp_wmb (); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + vq->desc_packed[vq->last_used_idx + i].flags = flags; + +vhost_log_cache_used_vring (dev, vq, vq->last_used_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc ) * +PACKED_BATCH_SIZE ); +vhost_log_cache_sync (dev, vq); + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline voidflush_shadow_used_ring_packed (struct virtio_net *dev,struct vhost_virtqueue *vq)
@@ -992,6 +1022,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ];uint32_t buf_offset = dev->vhost_hlen;uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ];uint16_t i;if (unlikely (avail_idx &PACKED_BATCH_MASK ))
@@ -1047,6 +1078,11 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, pkts[i]->pkt_len); } +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + ids[i] = descs[avail_idx + i].id; + +vhost_flush_enqueue_batch_packed (dev, vq, lens, ids); +return 0 ; }
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 08/13] vhost: flush batched enqueue descs directly
Quoted 6 lines · Marvin Liu
Flush used elements when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
Signed-off-by Marvin Liu <[email protected]>Reviewed-by Gavin Hu <[email protected]>
PATCH v6 9/13 vhost: buffer packed ring dequeue updates
In reply to Marvin LiuBuffer used ring updates as many as possible in vhost dequeue function for coordinating with virtio driver. For supporting buffer, shadow used ring element should contain descriptor's flags. First shadowed ring index was recorded for calculating buffered number.
lib/librte_vhost/vhost.h +5 -0
@@ -42,6 +42,8 @@#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ VRING_DESC_F_WRITE) +#define PACKED_DESC_DEQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
@@ -114,6 +116,7 @@ struct log_cache_entry {struct vring_used_elem_packed {uint16_t id; +uint16_t flags;uint32_t len;uint32_t count; };
@@ -171,6 +174,8 @@ struct vhost_virtqueue {uint16_t shadow_used_idx;/* Record packed ring enqueue latest desc cache aligned index */ uint16_t shadow_aligned_idx; +/* Record packed ring first dequeue desc index */ +uint16_t shadow_last_used_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +105 -0
135 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 09/13] vhost: buffer packed ring dequeue updates
Quoted 7 lines · Marvin Liu
Buffer used ring updates as many as possible in vhost dequeue function for coordinating with virtio driver. For supporting buffer, shadow used ring element should contain descriptor's flags. First shadowed ring index was recorded for calculating buffered number.
Signed-off-by Marvin Liu <[email protected]>
PATCH v6 10/13 vhost: optimize packed ring enqueue
In reply to Marvin LiuOptimize vhost device packed ring enqueue function by splitting batch and single functions. Packets can be filled into one desc will be handled by batch and others will be handled by single as before.
lib/librte_vhost/virtio_net.c +24 -91
@@ -772,64 +772,6 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t size,struct buf_vector *buf_vec, -uint16_t *nr_vec,uint16_t *num_buffers, -uint16_t *nr_descs) -{ -uint16_t avail_idx; -uint16_t vec_idx =0 ; -uint16_t max_tries, tries =0 ; - -uint16_t buf_id =0 ; -uint32_t len =0 ; -uint16_t desc_count; - - *num_buffers =0 ; - avail_idx = vq->last_avail_idx; - -if (rxvq_is_mergeable (dev)) - max_tries = vq->size -1 ; -else - max_tries =1 ; - -while (size >0 ) { -/* -* if we tried all available ring items, and still -* can't get enough buf, it means something abnormal -* happened. -*/ -if (unlikely (++tries > max_tries)) -return -1 ; - -if (unlikely (fill_vec_buf_packed (dev, vq, - avail_idx, &desc_count, - buf_vec, &vec_idx, - &buf_id, &len, -VHOST_ACCESS_RW ) <0 )) -return -1 ; - - len =RTE_MIN (len, size); -update_shadow_used_ring_packed (vq, buf_id, len, desc_count); - size -= len; - - avail_idx += desc_count; -if (avail_idx >= vq->size) - avail_idx -= vq->size; - - *nr_descs += desc_count; - *num_buffers +=1 ; - } - - *nr_vec = vec_idx; - -return 0 ; -} - static __rte_noinline void copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, struct buf_vector *buf_vec,
@@ -1111,7 +1053,7 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_rx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts)
@@ -1187,7 +1129,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int16_t +static __rte_always_inline int16_tvirtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *pkt)
@@ -1214,49 +1156,40 @@ virtio_dev_rx_single_packed(struct virtio_net *dev, } static __rte_noinline uint32_t -virtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mbuf **pkts,uint32_t count) +virtio_dev_rx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts, +uint32_t count) {uint32_t pkt_idx =0 ; -uint16_t num_buffers; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t remained = count; -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; -uint16_t nr_vec =0 ; -uint16_t nr_descs =0 ; +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); -if (unlikely (reserve_avail_buf_packed (dev, vq, - pkt_len, buf_vec, &nr_vec, - &num_buffers, &nr_descs) <0 )) { -VHOST_LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); - vq->shadow_used_idx -= num_buffers; -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_rx_batch_packed (dev, vq, pkts)) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, vq->last_avail_idx, - vq->last_avail_idx + num_buffers); - -if (copy_mbuf_to_desc (dev, vq, pkts[pkt_idx], - buf_vec, nr_vec, - num_buffers) <0 ) { - vq->shadow_used_idx -= num_buffers; +if (virtio_dev_rx_single_packed (dev, vq, pkts[pkt_idx]))break ; - } + pkt_idx++; + remained--; -vq_inc_last_avail_packed (vq, nr_descs); - } - -do_data_copy_enqueue (dev, vq); + }while (pkt_idx < count); -if (likely (vq->shadow_used_idx)) { +if (vq->shadow_used_idx) { +do_data_copy_enqueue (dev, vq);vhost_flush_enqueue_shadow_packed (dev, vq); -vhost_vring_call_packed (dev, vq); } +if (pkt_idx) +vhost_vring_call_packed (dev, vq); +return pkt_idx; }
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 10/13] vhost: optimize packed ring enqueue
Quoted 6 lines · Marvin Liu
Optimize vhost device packed ring enqueue function by splitting batch and single functions. Packets can be filled into one desc will be handled by batch and others will be handled by single as before.
Signed-off-by Marvin Liu <[email protected]>
PATCH v6 11/13 vhost: add packed ring zcopy batch and single dequeue
In reply to Marvin LiuAdd vhost packed ring zero copy batch and single dequeue functions like normal dequeue path.
lib/librte_vhost/virtio_net.c +116 -0
@@ -1886,6 +1886,122 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } +static __rte_unused int +virtio_dev_tx_batch_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE ]; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +uint16_t avail_idx = vq->last_avail_idx; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + zmbufs[i] =get_zmbuf (vq); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (!zmbufs[i]) + goto free_pkt; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + zmbufs[i]->mbuf = pkts[i]; + zmbufs[i]->desc_idx = avail_idx + i; + zmbufs[i]->desc_count =1 ; + } + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_mbuf_refcnt_update (pkts[i],1 ); + +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbufs[i], next); + + vq->nr_zmbuf +=PACKED_BATCH_SIZE ; +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; + +free_pkt: +for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t buf_id, desc_count; +struct zcopy_mbuf *zmbuf; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + zmbuf =get_zmbuf (vq); +if (!zmbuf) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + zmbuf->mbuf = *pkts; + zmbuf->desc_idx = vq->last_avail_idx; + zmbuf->desc_count = desc_count; + +rte_mbuf_refcnt_update (*pkts,1 ); + + vq->nr_zmbuf +=1 ; +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); + +vq_inc_last_avail_packed (vq, desc_count); +return 0 ; +} + +static __rte_always_inline void +free_zmbuf (struct vhost_virtqueue *vq) +{ +struct zcopy_mbuf *next =NULL ; +struct zcopy_mbuf *zmbuf; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +uint16_t last_used_idx = vq->last_used_idx; + +if (mbuf_is_consumed (zmbuf->mbuf)) { +uint16_t flags =0 ; +bool wrap; + + wrap = vq->used_wrap_counter; + flags =PACKED_DESC_DEQUEUE_USED_FLAG (wrap); + + vq->desc_packed[last_used_idx].id = zmbuf->desc_idx; + vq->desc_packed[last_used_idx].len =0 ; + +rte_smp_wmb (); + vq->desc_packed[last_used_idx].flags = flags; + +vq_inc_last_used_packed (vq, zmbuf->desc_count); + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +restore_mbuf (zmbuf->mbuf); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + } + } +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 11/13] vhost: add packed ring zcopy batch and single dequeue
Quoted 5 lines · Marvin Liu
Add vhost packed ring zero copy batch and single dequeue functions like normal dequeue path.
Signed-off-by Marvin Liu <[email protected]>
Thanks,
Maxime
PATCH v6 12/13 vhost: optimize packed ring dequeue
In reply to Marvin LiuOptimize vhost device packed ring dequeue function by splitting batch and single functions. No-chained and direct descriptors will be handled by batch and other will be handled by single as before.
lib/librte_vhost/virtio_net.c +65 -171
@@ -201,69 +201,6 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } -static __rte_always_inline void -flush_shadow_used_ring_packed (struct virtio_net *dev, -struct vhost_virtqueue *vq) -{ -int i; -uint16_t used_idx = vq->last_used_idx; -uint16_t head_idx = vq->last_used_idx; -uint16_t head_flags =0 ; - -/* Split loop in two to save memory barriers */ -for (i =0 ; i < vq->shadow_used_idx; i++) { - vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id; - vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len; - - used_idx += vq->shadow_used_packed[i].count; -if (used_idx >= vq->size) - used_idx -= vq->size; - } - -rte_smp_wmb (); - -for (i =0 ; i < vq->shadow_used_idx; i++) { -uint16_t flags; - -if (vq->shadow_used_packed[i].len) - flags =VRING_DESC_F_WRITE ; -else - flags =0 ; - -if (vq->used_wrap_counter) { - flags |=VRING_DESC_F_USED ; - flags |=VRING_DESC_F_AVAIL ; - }else { - flags &= ~VRING_DESC_F_USED ; - flags &= ~VRING_DESC_F_AVAIL ; - } - -if (i >0 ) { - vq->desc_packed[vq->last_used_idx].flags = flags; - -vhost_log_cache_used_vring (dev, vq, - vq->last_used_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - }else { - head_idx = vq->last_used_idx; - head_flags = flags; - } - -vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); - } - - vq->desc_packed[head_idx].flags = head_flags; - -vhost_log_cache_used_vring (dev, vq, - head_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - - vq->shadow_used_idx =0 ; -vhost_log_cache_sync (dev, vq); -} - static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -328,17 +265,6 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } -static __rte_always_inline void -update_shadow_used_ring_packed (struct vhost_virtqueue *vq, -uint16_t desc_idx,uint32_t len,uint16_t count) -{ -uint16_t i = vq->shadow_used_idx++; - - vq->shadow_used_packed[i].id = desc_idx; - vq->shadow_used_packed[i].len = len; - vq->shadow_used_packed[i].count = count; -} -static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -395,7 +321,7 @@ vhost_shadow_enqueue_single_packed(struct virtio_net *dev, } } -static __rte_unused void +static __rte_always_inline voidvhost_flush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1799,7 +1725,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1866,7 +1792,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1886,7 +1812,7 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1935,7 +1861,7 @@ virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -2003,114 +1929,77 @@ free_zmbuf(struct vhost_virtqueue *vq) } static __rte_noinline uint16_t -virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) +virtio_dev_tx_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) { -uint16_t i; - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf, *next; +uint32_t pkt_idx =0 ; +uint32_t remained = count; -for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); - zmbuf !=NULL ; zmbuf = next) { - next =TAILQ_NEXT (zmbuf, next); +free_zmbuf (vq); -if (mbuf_is_consumed (zmbuf->mbuf)) { -update_shadow_used_ring_packed (vq, - zmbuf->desc_idx, -0 , - zmbuf->desc_count); - -TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); -restore_mbuf (zmbuf->mbuf); -rte_pktmbuf_free (zmbuf->mbuf); -put_zmbuf (zmbuf); - vq->nr_zmbuf -=1 ; +do { +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed_zmbuf (dev, vq, + mbuf_pool, &pkts[pkt_idx])) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; } } -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } +if (virtio_dev_tx_single_packed_zmbuf (dev, vq, mbuf_pool, + &pkts[pkt_idx])) +break ; + pkt_idx++; + remained--; -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); + }while (remained); - count =RTE_MIN (count,MAX_PKT_BURST ); -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) about to dequeue %u buffers\n" , - dev->vid, count); +if (pkt_idx) +vhost_vring_call_packed (dev, vq); -for (i =0 ; i < count; i++) { -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; -uint16_t buf_id; -uint32_t dummy_len; -uint16_t desc_count, nr_vec =0 ; -int err; +return pkt_idx; +} -if (unlikely (fill_vec_buf_packed (dev, vq, - vq->last_avail_idx, &desc_count, - buf_vec, &nr_vec, - &buf_id, &dummy_len, -VHOST_ACCESS_RO ) <0 )) -break ; +static __rte_noinline uint16_t +virtio_dev_tx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) +{ +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (likely (dev->dequeue_zero_copy ==0 )) -update_shadow_used_ring_packed (vq, buf_id,0 , - desc_count); +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); - pkts[i] =rte_pktmbuf_alloc (mbuf_pool); -if (unlikely (pkts[i] ==NULL )) { -RTE_LOG (ERR ,VHOST_DATA , -"Failed to allocate memory for mbuf.\n" ); -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) { +vhost_flush_dequeue_packed (dev, vq); + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } - err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, pkts[i], - mbuf_pool); -if (unlikely (err)) { -rte_pktmbuf_free (pkts[i]); +if (virtio_dev_tx_single_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx]))break ; - } - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf; - - zmbuf =get_zmbuf (vq); -if (!zmbuf) { -rte_pktmbuf_free (pkts[i]); -break ; - } - zmbuf->mbuf = pkts[i]; - zmbuf->desc_idx = buf_id; - zmbuf->desc_count = desc_count; - -/* -* Pin lock the mbuf; we will check later to see -* whether the mbuf is freed (when we are the last -* user) or not. If that's the case, we then could -* update the used ring safely. -*/ -rte_mbuf_refcnt_update (pkts[i],1 ); - - vq->nr_zmbuf +=1 ; -TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); - } +vhost_flush_dequeue_packed (dev, vq); + pkt_idx++; + remained--; -vq_inc_last_avail_packed (vq, desc_count); - } + }while (remained); -if (likely (dev->dequeue_zero_copy ==0 )) { +if (vq->shadow_used_idx)do_data_copy_dequeue (vq); -if (unlikely (i < count)) - vq->shadow_used_idx = i; -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } -return i; +return pkt_idx; }uint16_t
@@ -2186,9 +2075,14 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, count -=1 ; } -if (vq_is_packed (dev)) - count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, count); -else +if (vq_is_packed (dev)) { +if (unlikely (dev->dequeue_zero_copy)) + count =virtio_dev_tx_packed_zmbuf (dev, vq, mbuf_pool, + pkts, count); +else + count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, + count); + }else count =virtio_dev_tx_split (dev, vq, mbuf_pool, pkts, count); out:
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 12/13] vhost: optimize packed ring dequeue
Quoted 6 lines · Marvin Liu
Optimize vhost device packed ring dequeue function by splitting batch and single functions. No-chained and direct descriptors will be handled by batch and other will be handled by single as before.
Signed-off-by Marvin Liu <[email protected]>
PATCH v6 13/13 vhost: optimize packed ring dequeue when in-order
In reply to Marvin LiuWhen VIRTIO_F_IN_ORDER feature is negotiated, vhost can optimize dequeue function by only update first used descriptor.
lib/librte_vhost/virtio_net.c +55 -2
@@ -31,6 +31,12 @@ rxvq_is_mergeable(struct virtio_net *dev)return dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF ); } +static __rte_always_inline bool +virtio_net_is_inorder (struct virtio_net *dev) +{ +return dev->features & (1ULL <<VIRTIO_F_IN_ORDER ); +} +static bool is_valid_virt_queue_idx (uint32_t idx,int is_tx,uint32_t nr_vring) {
@@ -201,6 +207,25 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } +static __rte_always_inline void +vhost_shadow_dequeue_batch_packed_inorder (struct vhost_virtqueue *vq, +uint16_t id) +{ + vq->shadow_used_packed[0 ].id = id; + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].flags = +PACKED_DESC_DEQUEUE_USED_FLAG (vq->used_wrap_counter); + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -265,6 +290,26 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } +static __rte_always_inline void +vhost_shadow_dequeue_single_packed_inorder (struct vhost_virtqueue *vq, +uint16_t buf_id, +uint16_t count) +{ + vq->shadow_used_packed[0 ].id = buf_id; + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count = count; + vq->shadow_used_packed[0 ].flags = +PACKED_DESC_DEQUEUE_USED_FLAG (vq->used_wrap_counter); + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq, count); +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1748,7 +1793,11 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, pkts[i]->pkt_len); } -vhost_shadow_dequeue_batch_packed (dev, vq, ids); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_batch_packed_inorder (vq, + ids[PACKED_BATCH_SIZE -1 ]); +else +vhost_shadow_dequeue_batch_packed (dev, vq, ids);vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE );
@@ -1805,7 +1854,11 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, &desc_count))return -1 ; -vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_single_packed_inorder (vq, buf_id, + desc_count); +else +vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count);vq_inc_last_avail_packed (vq, desc_count);
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v6 13/13] vhost: optimize packed ring dequeue when in-order
Quoted 4 lines · Marvin Liu
When
VIRTIO_F_IN_ORDERfeature is negotiated, vhost can optimize dequeue function by only update first used descriptor.Signed-off-by Marvin Liu <[email protected]>
Hi Marvin,
This is almost good, just fix the small comments I made.
Also, please rebase on top of next-virtio branch, because I applied below patch from Flavio that you need to take into account:
http://patches.dpdk.org/patch/61284/
Regards,
Maxime
Quoted 84 lines · Marvin Liu
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v6:
- Fix dequeue zcopy result checkv5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code stylev4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of twov3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimizationv2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ-PKT_BURST)
- Optimize dequeue used ring update whenin_ordernegotiatedMarvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by batch
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-orderlib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 +++ lib/librte_vhost/virtio_net.c | 924 +++++++++++++++++++++++++++------- 4 files changed, 812 insertions(+), 194 deletions(-)
Quoted 11 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, October 17, 2019 3:31 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v6 00/13] vhost packed ring performance optimization
Quoted 8 lines
Hi Marvin,
This is almost good, just fix the small comments I made.
Also, please rebase on top of next-virtio branch, because I applied below patch from Flavio that you need to take into account:
Thanks, Maxime. I will start rebasing work.
Quoted 89 lines
Regards,
MaximeQuoted 84 lines · Marvin Liu
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v6:
- Fix dequeue zcopy result checkv5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code stylev4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of twov3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimizationv2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ-PKT_BURST)
- Optimize dequeue used ring update whenin_ordernegotiatedMarvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by batch
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-orderlib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 +++ lib/librte_vhost/virtio_net.c | 924 +++++++++++++++++++++++++++------- 4 files changed, 812 insertions(+), 194 deletions(-)
v7 cover vhost packed ring performance optimization
In reply to Marvin LiuPacked ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v7:
- Rebase code
- Rename unroll macro and definitions
- Calculate flags when doing single dequeue
v6:
- Fix dequeue zcopy result check
v5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code style
v4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of two
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimization
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by cacheline
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 ++ lib/librte_vhost/virtio_net.c | 945 +++++++++++++++++++++++++++------- 4 files changed, 834 insertions(+), 193 deletions(-)
Signature · 1 line
2.17.1
PATCH v7 1/13 vhost: add packed ring indexes increasing function
When enqueuing or dequeuing, the virtqueue's local available and used indexes are increased.
lib/librte_vhost/vhost.h +20 -0
@@ -367,6 +367,26 @@ desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) wrap_counter != !!(flags &VRING_DESC_F_USED ); } +static inline void +vq_inc_last_used_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_used_idx += num; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + +static inline void +vq_inc_last_avail_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_avail_idx += num; +if (vq->last_avail_idx >= vq->size) { + vq->avail_wrap_counter ^=1 ; + vq->last_avail_idx -= vq->size; + } +} +void __vhost_log_cache_write (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t addr,uint64_t len);
lib/librte_vhost/virtio_net.c +3 -15
@@ -138,11 +138,7 @@ flush_shadow_used_ring_packed(struct virtio_net *dev, head_flags = flags; } - vq->last_used_idx += vq->shadow_used_packed[i].count; -if (vq->last_used_idx >= vq->size) { - vq->used_wrap_counter ^=1 ; - vq->last_used_idx -= vq->size; - } +vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); } __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
@@ -865,11 +861,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,break ; } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, nr_descs); }do_data_copy_enqueue (dev, vq);
@@ -1585,11 +1577,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); } - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, desc_count); }if (likely (dev->dequeue_zero_copy ==0 )) {
Signature · 1 line
2.17.1
PATCH v7 2/13 vhost: add packed ring single enqueue
In reply to Marvin LiuAdd vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +80 -0
@@ -770,6 +770,60 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt, +struct buf_vector *buf_vec, +uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; +uint16_t avail_idx = vq->last_avail_idx; +uint16_t max_tries, tries =0 ; +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; +uint32_t size = pkt->pkt_len + dev->vhost_hlen; +uint16_t num_buffers =0 ; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) +return -1 ; + + len =RTE_MIN (len, size); + size -= len; + + num_buffers +=1 ; + + *nr_descs += desc_count; + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 ) +return -1 ; + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -827,6 +881,32 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + +vq_inc_last_avail_packed (vq, nr_descs); + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
PATCH v7 3/13 vhost: try to unroll for each loop
In reply to Marvin LiuCreate macro for adding unroll pragma before for each loop. Batch functions will be contained of several small loops which can be optimized by compilers' loop unrolling pragma.
lib/librte_vhost/Makefile +18 -0
@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DVHOST_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DVHOST_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DVHOST_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endif
lib/librte_vhost/meson.build +7 -0
@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DVHOST_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DVHOST_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DVHOST_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4
lib/librte_vhost/vhost.h +24 -0
@@ -39,6 +39,30 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) + +#ifdef VHOST_GCC_UNROLL_PRAGMA +#define vhost_for_each_try_unroll (iter, val, size) _Pragma("GCC unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef VHOST_CLANG_UNROLL_PRAGMA +#define vhost_for_each_try_unroll (iter, val, size) _Pragma("unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef VHOST_ICC_UNROLL_PRAGMA +#define vhost_for_each_try_unroll (iter, val, size) _Pragma("unroll (4)") \ + for (iter = val; iter < size; iter++) +#endif + +#ifndef vhost_for_each_try_unroll +#define vhost_for_each_try_unroll (iter, val, num) \ + for (iter = val; iter < num; iter++) +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
Signature · 1 line
2.17.1
PATCH v7 4/13 vhost: add packed ring batch enqueue
In reply to Marvin LiuBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function do not support chained mbufs, single packet enqueue function will handle it.
lib/librte_vhost/virtio_net.c +70 -0
@@ -881,6 +881,76 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; + +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_t virtio_dev_rx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
Signature · 1 line
2.17.1
PATCH v7 5/13 vhost: add packed ring single dequeue
In reply to Marvin LiuAdd vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
lib/librte_vhost/virtio_net.c +55 -0
@@ -1635,6 +1635,61 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_dequeue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t *buf_id, +uint16_t *desc_count) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t dummy_len; +uint16_t nr_vec =0 ; +int err; + +if (unlikely (fill_vec_buf_packed (dev, vq, + vq->last_avail_idx, desc_count, + buf_vec, &nr_vec, + buf_id, &dummy_len, +VHOST_ACCESS_RO ) <0 )) +return -1 ; + + *pkts =rte_pktmbuf_alloc (mbuf_pool); +if (unlikely (*pkts ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA , +"Failed to allocate memory for mbuf.\n" ); +return -1 ; + } + + err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, *pkts, + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + +return 0 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ + +uint16_t buf_id, desc_count; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + +vq_inc_last_avail_packed (vq, desc_count); + +return 0 ; +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v7 05/13] vhost: add packed ring single dequeue
Quoted 25 lines · Marvin Liu
Add vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
Signed-off-by Marvin Liu <[email protected]>Reviewed-by Maxime Coquelin <[email protected]>lib/librte_vhost/virtio_net.c +11
-0@@ -1635,6 +1635,61 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_dequeue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t *buf_id, +uint16_t *desc_count) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t dummy_len; +uint16_t nr_vec =0 ;
lib/librte_vhost/virtio_net.c virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 1658
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+int err; + +if (unlikely (fill_vec_buf_packed (dev, vq, + vq->last_avail_idx, desc_count, + buf_vec, &nr_vec, + buf_id, &dummy_len, +VHOST_ACCESS_RO ) <0 )) +return -1 ; + + *pkts =rte_pktmbuf_alloc (mbuf_pool);
This is not properly rebased, you should no more call this API directly, but instead virtio_dev_pktmbuf_alloc() which was introduced withg flavio's patch.
Quoted 38 lines
lib/librte_vhost/virtio_net.c +34
-0+if (unlikely (*pkts ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA , +"Failed to allocate memory for mbuf.\n" ); +return -1 ; + } + + err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, *pkts, + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + +return 0 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ + +uint16_t buf_id, desc_count; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + +vq_inc_last_avail_packed (vq, desc_count); + +return 0 ; +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
PATCH v7 6/13 vhost: add packed ring batch dequeue
In reply to Marvin LiuAdd batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
lib/librte_vhost/virtio_net.c +108 -0
114 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v7 06/13] vhost: add packed ring batch dequeue
Quoted 65 lines · Marvin Liu
Add batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +3
-0@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)lib/librte_vhost/virtio_net.c +37
-0@@ -1635,6 +1635,114 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_reserve_avail_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t avail_idx, +uintptr_t *desc_addrs, +uint16_t *ids) +{ +bool wrap = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +struct virtio_net_hdr *hdr; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint64_t buf_lens[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint16_t flags, i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + flags = descs[avail_idx + i].flags; +if (unlikely ((wrap != !!(flags &VRING_DESC_F_AVAIL )) || + (wrap == !!(flags &VRING_DESC_F_USED )) || + (flags &PACKED_DESC_SINGLE_DEQUEUE_FLAG ))) +return -1 ; + } + +rte_smp_rmb (); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + desc_addrs[i] = vhost_iova_to_vva(dev, vq,
lib/librte_vhost/virtio_net.c virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 1684
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+ descs[avail_idx + i].addr, + &lens[i], VHOST_ACCESS_RW); + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely ((lens[i] != descs[avail_idx + i].len))) +return -1 ; + } + +if (rte_pktmbuf_alloc_bulk (mbuf_pool, pkts,PACKED_BATCH_SIZE ))
Same here, you may want to create a variant of Flavio's virtio_dev_pktmbuf_alloc for bulk allocations.
Quoted 65 lines
lib/librte_vhost/virtio_net.c +61
-0+return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (buf_lens[i] < (lens[i] - buf_offset))) + goto free_buf; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset; + pkts[i]->data_len = pkts[i]->pkt_len; + ids[i] = descs[avail_idx + i].id; + } + +if (virtio_net_with_host_offload (dev)) { +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + hdr = (struct virtio_net_hdr *)(desc_addrs[i]); +vhost_dequeue_offload (hdr, pkts[i]); + } + } + +return 0 ; + +free_buf: +for (i =0 ; i <PACKED_BATCH_SIZE ; i++) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t avail_idx = vq->last_avail_idx; +uint32_t buf_offset = dev->vhost_hlen; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + (void *)(uintptr_t )(desc_addrs[i] + buf_offset), + pkts[i]->pkt_len); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; +} + static __rte_always_inline int vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
Re: [dpdk-dev] [PATCH v7 06/13] vhost: add packed ring batch dequeue
Thanks Maxime, has been modified in v8.
Quoted 159 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Monday, October 21, 2019 5:47 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v7 06/13] vhost: add packed ring batch dequeueReview
Quoted 21 lines · Marvin Liu
Add batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
Signed-off-by Marvin Liu <[email protected]>lib/librte_vhost/vhost.h +3
-0@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)(quoted diff) +0
-0
b/lib/librte_vhost/virtio_net.clib/librte_vhost/virtio_net.c virtio_dev_tx_split(struct virtio_net *dev,
Quoted 4 lines
index
317be1aed..f13fcafbb100644lib/librte_vhost/virtio_net.c +0
-0@@ -1635,6 +1635,114 @@ virtio_dev_tx_split(struct virtio_net *dev,struct
vhost_virtqueue*vq,Quoted 40 lines
lib/librte_vhost/virtio_net.c +37
-0return i; } +static __rte_always_inline int +vhost_reserve_avail_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t avail_idx, +uintptr_t *desc_addrs, +uint16_t *ids) +{ +bool wrap = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +struct virtio_net_hdr *hdr; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint64_t buf_lens[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint16_t flags, i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + flags = descs[avail_idx + i].flags; +if (unlikely ((wrap != !!(flags &VRING_DESC_F_AVAIL )) || + (wrap == !!(flags &VRING_DESC_F_USED )) || + (flags &PACKED_DESC_SINGLE_DEQUEUE_FLAG ))) +return -1 ; + } + +rte_smp_rmb (); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + desc_addrs[i] = vhost_iova_to_vva(dev, vq,lib/librte_vhost/virtio_net.c virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 1684
Quoted 10 lines
lib/librte_vhost/virtio_net.c +10
-0+ descs[avail_idx + i].addr, + &lens[i], VHOST_ACCESS_RW); + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely ((lens[i] != descs[avail_idx + i].len))) +return -1 ; + } + +if (rte_pktmbuf_alloc_bulk (mbuf_pool, pkts,PACKED_BATCH_SIZE ))Same here, you may want to create a variant of Flavio's
virtio_dev_pktmbuf_allocfor bulk allocations.Quoted 65 lines
lib/librte_vhost/virtio_net.c +61
-0+return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (buf_lens[i] < (lens[i] - buf_offset))) + goto free_buf; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset; + pkts[i]->data_len = pkts[i]->pkt_len; + ids[i] = descs[avail_idx + i].id; + } + +if (virtio_net_with_host_offload (dev)) { +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + hdr = (struct virtio_net_hdr *)(desc_addrs[i]); +vhost_dequeue_offload (hdr, pkts[i]); + } + } + +return 0 ; + +free_buf: +for (i =0 ; i <PACKED_BATCH_SIZE ; i++) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t avail_idx = vq->last_avail_idx; +uint32_t buf_offset = dev->vhost_hlen; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + (void *)(uintptr_t )(desc_addrs[i] + buf_offset), + pkts[i]->pkt_len); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; +} + static __rte_always_inline int vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
PATCH v7 7/13 vhost: flush enqueue updates by cacheline
In reply to Marvin LiuBuffer vhost packed ring enqueue updates, flush ring descs if buffered content filled up one cacheline. Thus virtio can receive packets at a faster frequency.
lib/librte_vhost/vhost.h +2 -0
@@ -174,6 +174,8 @@ struct vhost_virtqueue {struct vring_used_elem_packed *shadow_used_packed; };uint16_t shadow_used_idx; +/* Record packed ring enqueue latest desc cache aligned index */ +uint16_t shadow_aligned_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +100 -1
137 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v7 8/13 vhost: flush batched enqueue descs directly
In reply to Marvin LiuFlush used elements when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ + VRING_DESC_F_WRITE)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
lib/librte_vhost/virtio_net.c +36 -0
@@ -154,6 +154,36 @@ vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } +static __rte_always_inline void +vhost_flush_enqueue_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +uint64_t *lens, +uint16_t *ids) +{ +uint16_t i; +uint16_t flags; + + flags =PACKED_DESC_ENQUEUE_USED_FLAG (vq->used_wrap_counter); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + vq->desc_packed[vq->last_used_idx + i].id = ids[i]; + vq->desc_packed[vq->last_used_idx + i].len = lens[i]; + } + +rte_smp_wmb (); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + vq->desc_packed[vq->last_used_idx + i].flags = flags; + +vhost_log_cache_used_vring (dev, vq, vq->last_used_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc ) * +PACKED_BATCH_SIZE ); +vhost_log_cache_sync (dev, vq); + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline voidflush_shadow_used_ring_packed (struct virtio_net *dev,struct vhost_virtqueue *vq)
@@ -992,6 +1022,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ];uint32_t buf_offset = dev->vhost_hlen;uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ];uint16_t i;if (unlikely (avail_idx &PACKED_BATCH_MASK ))
@@ -1047,6 +1078,11 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, pkts[i]->pkt_len); } +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + ids[i] = descs[avail_idx + i].id; + +vhost_flush_enqueue_batch_packed (dev, vq, lens, ids); +return 0 ; }
Signature · 1 line
2.17.1
PATCH v7 9/13 vhost: buffer packed ring dequeue updates
In reply to Marvin LiuBuffer used ring updates as many as possible in vhost dequeue function for coordinating with virtio driver. For supporting buffer, shadow used ring element should contain descriptor's flags. First shadowed ring index was recorded for calculating buffered number.
lib/librte_vhost/vhost.h +5 -0
@@ -42,6 +42,8 @@#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ VRING_DESC_F_WRITE) +#define PACKED_DESC_DEQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
@@ -114,6 +116,7 @@ struct log_cache_entry {struct vring_used_elem_packed {uint16_t id; +uint16_t flags;uint32_t len;uint32_t count; };
@@ -179,6 +182,8 @@ struct vhost_virtqueue {uint16_t shadow_used_idx;/* Record packed ring enqueue latest desc cache aligned index */ uint16_t shadow_aligned_idx; +/* Record packed ring first dequeue desc index */ +uint16_t shadow_last_used_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +112 -0
142 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v7 10/13 vhost: optimize packed ring enqueue
In reply to Marvin LiuOptimize vhost device packed ring enqueue function by splitting batch and single functions. Packets can be filled into one desc will be handled by batch and others will be handled by single as before.
lib/librte_vhost/virtio_net.c +24 -91
@@ -778,64 +778,6 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t size,struct buf_vector *buf_vec, -uint16_t *nr_vec,uint16_t *num_buffers, -uint16_t *nr_descs) -{ -uint16_t avail_idx; -uint16_t vec_idx =0 ; -uint16_t max_tries, tries =0 ; - -uint16_t buf_id =0 ; -uint32_t len =0 ; -uint16_t desc_count; - - *num_buffers =0 ; - avail_idx = vq->last_avail_idx; - -if (rxvq_is_mergeable (dev)) - max_tries = vq->size -1 ; -else - max_tries =1 ; - -while (size >0 ) { -/* -* if we tried all available ring items, and still -* can't get enough buf, it means something abnormal -* happened. -*/ -if (unlikely (++tries > max_tries)) -return -1 ; - -if (unlikely (fill_vec_buf_packed (dev, vq, - avail_idx, &desc_count, - buf_vec, &vec_idx, - &buf_id, &len, -VHOST_ACCESS_RW ) <0 )) -return -1 ; - - len =RTE_MIN (len, size); -update_shadow_used_ring_packed (vq, buf_id, len, desc_count); - size -= len; - - avail_idx += desc_count; -if (avail_idx >= vq->size) - avail_idx -= vq->size; - - *nr_descs += desc_count; - *num_buffers +=1 ; - } - - *nr_vec = vec_idx; - -return 0 ; -} - static __rte_noinline void copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, struct buf_vector *buf_vec,
@@ -1118,7 +1060,7 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_rx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts)
@@ -1194,7 +1136,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int16_t +static __rte_always_inline int16_tvirtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *pkt)
@@ -1221,49 +1163,40 @@ virtio_dev_rx_single_packed(struct virtio_net *dev, } static __rte_noinline uint32_t -virtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mbuf **pkts,uint32_t count) +virtio_dev_rx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts, +uint32_t count) {uint32_t pkt_idx =0 ; -uint16_t num_buffers; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t remained = count; -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; -uint16_t nr_vec =0 ; -uint16_t nr_descs =0 ; +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); -if (unlikely (reserve_avail_buf_packed (dev, vq, - pkt_len, buf_vec, &nr_vec, - &num_buffers, &nr_descs) <0 )) { -VHOST_LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); - vq->shadow_used_idx -= num_buffers; -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_rx_batch_packed (dev, vq, pkts)) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, vq->last_avail_idx, - vq->last_avail_idx + num_buffers); - -if (copy_mbuf_to_desc (dev, vq, pkts[pkt_idx], - buf_vec, nr_vec, - num_buffers) <0 ) { - vq->shadow_used_idx -= num_buffers; +if (virtio_dev_rx_single_packed (dev, vq, pkts[pkt_idx]))break ; - } + pkt_idx++; + remained--; -vq_inc_last_avail_packed (vq, nr_descs); - } - -do_data_copy_enqueue (dev, vq); + }while (pkt_idx < count); -if (likely (vq->shadow_used_idx)) { +if (vq->shadow_used_idx) { +do_data_copy_enqueue (dev, vq);vhost_flush_enqueue_shadow_packed (dev, vq); -vhost_vring_call_packed (dev, vq); } +if (pkt_idx) +vhost_vring_call_packed (dev, vq); +return pkt_idx; }
Signature · 1 line
2.17.1
PATCH v7 11/13 vhost: add packed ring zcopy batch and single dequeue
In reply to Marvin LiuAdd vhost packed ring zero copy batch and single dequeue functions like normal dequeue path.
lib/librte_vhost/virtio_net.c +120 -0
@@ -1978,6 +1978,126 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } +static __rte_unused int +virtio_dev_tx_batch_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE ]; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +uint16_t avail_idx = vq->last_avail_idx; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + zmbufs[i] =get_zmbuf (vq); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (!zmbufs[i]) + goto free_pkt; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + zmbufs[i]->mbuf = pkts[i]; + zmbufs[i]->desc_idx = avail_idx + i; + zmbufs[i]->desc_count =1 ; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_mbuf_refcnt_update (pkts[i],1 ); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbufs[i], next); + + vq->nr_zmbuf +=PACKED_BATCH_SIZE ; +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; + +free_pkt: +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t buf_id, desc_count; +struct zcopy_mbuf *zmbuf; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + zmbuf =get_zmbuf (vq); +if (!zmbuf) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + zmbuf->mbuf = *pkts; + zmbuf->desc_idx = vq->last_avail_idx; + zmbuf->desc_count = desc_count; + +rte_mbuf_refcnt_update (*pkts,1 ); + + vq->nr_zmbuf +=1 ; +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); + +vq_inc_last_avail_packed (vq, desc_count); +return 0 ; +} + +static __rte_always_inline void +free_zmbuf (struct vhost_virtqueue *vq) +{ +struct zcopy_mbuf *next =NULL ; +struct zcopy_mbuf *zmbuf; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +uint16_t last_used_idx = vq->last_used_idx; + +if (mbuf_is_consumed (zmbuf->mbuf)) { +uint16_t flags; + flags = vq->desc_packed[last_used_idx].flags; +if (vq->used_wrap_counter) { + flags |=VRING_DESC_F_USED ; + flags |=VRING_DESC_F_AVAIL ; + }else { + flags &= ~VRING_DESC_F_USED ; + flags &= ~VRING_DESC_F_AVAIL ; + } + + vq->desc_packed[last_used_idx].id = zmbuf->desc_idx; + vq->desc_packed[last_used_idx].len =0 ; + +rte_smp_wmb (); + vq->desc_packed[last_used_idx].flags = flags; + +vq_inc_last_used_packed (vq, zmbuf->desc_count); + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +restore_mbuf (zmbuf->mbuf); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + } + } +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v7 12/13 vhost: optimize packed ring dequeue
In reply to Marvin LiuOptimize vhost device packed ring dequeue function by splitting batch and single functions. No-chained and direct descriptors will be handled by batch and other will be handled by single as before.
lib/librte_vhost/virtio_net.c +66 -168
@@ -201,68 +201,6 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } -static __rte_always_inline void -flush_shadow_used_ring_packed (struct virtio_net *dev, -struct vhost_virtqueue *vq) -{ -int i; -uint16_t used_idx = vq->last_used_idx; -uint16_t head_idx = vq->last_used_idx; -uint16_t head_flags =0 ; - -/* Split loop in two to save memory barriers */ -for (i =0 ; i < vq->shadow_used_idx; i++) { - vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id; - vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len; - - used_idx += vq->shadow_used_packed[i].count; -if (used_idx >= vq->size) - used_idx -= vq->size; - } - -for (i =0 ; i < vq->shadow_used_idx; i++) { -uint16_t flags; - -if (vq->shadow_used_packed[i].len) - flags =VRING_DESC_F_WRITE ; -else - flags =0 ; - -if (vq->used_wrap_counter) { - flags |=VRING_DESC_F_USED ; - flags |=VRING_DESC_F_AVAIL ; - }else { - flags &= ~VRING_DESC_F_USED ; - flags &= ~VRING_DESC_F_AVAIL ; - } - -if (i >0 ) { - vq->desc_packed[vq->last_used_idx].flags = flags; - -vhost_log_cache_used_vring (dev, vq, - vq->last_used_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - }else { - head_idx = vq->last_used_idx; - head_flags = flags; - } - -vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); - } - -__atomic_store_n (&vq->desc_packed[head_idx].flags, head_flags, - __ATOMIC_RELEASE); - -vhost_log_cache_used_vring (dev, vq, - head_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - - vq->shadow_used_idx =0 ; -vhost_log_cache_sync (dev, vq); -} - static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -335,17 +273,6 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } -static __rte_always_inline void -update_shadow_used_ring_packed (struct vhost_virtqueue *vq, -uint16_t desc_idx,uint32_t len,uint16_t count) -{ -uint16_t i = vq->shadow_used_idx++; - - vq->shadow_used_packed[i].id = desc_idx; - vq->shadow_used_packed[i].len = len; - vq->shadow_used_packed[i].count = count; -} -static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -403,7 +330,7 @@ vhost_shadow_enqueue_single_packed(struct virtio_net *dev, } } -static __rte_unused void +static __rte_always_inline voidvhost_flush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1890,7 +1817,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1958,7 +1885,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1978,7 +1905,7 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -2027,7 +1954,7 @@ virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -2099,111 +2026,77 @@ free_zmbuf(struct vhost_virtqueue *vq) } static __rte_noinline uint16_t -virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) +virtio_dev_tx_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) { -uint16_t i; - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf, *next; - -for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); - zmbuf !=NULL ; zmbuf = next) { - next =TAILQ_NEXT (zmbuf, next); +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (mbuf_is_consumed (zmbuf->mbuf)) { -update_shadow_used_ring_packed (vq, - zmbuf->desc_idx, -0 , - zmbuf->desc_count); +free_zmbuf (vq); -TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); -restore_mbuf (zmbuf->mbuf); -rte_pktmbuf_free (zmbuf->mbuf); -put_zmbuf (zmbuf); - vq->nr_zmbuf -=1 ; +do { +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed_zmbuf (dev, vq, + mbuf_pool, &pkts[pkt_idx])) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; } } -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } - -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); - - count =RTE_MIN (count,MAX_PKT_BURST ); -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) about to dequeue %u buffers\n" , - dev->vid, count); - -for (i =0 ; i < count; i++) { -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; -uint16_t buf_id; -uint32_t buf_len; -uint16_t desc_count, nr_vec =0 ; -int err; - -if (unlikely (fill_vec_buf_packed (dev, vq, - vq->last_avail_idx, &desc_count, - buf_vec, &nr_vec, - &buf_id, &buf_len, -VHOST_ACCESS_RO ) <0 )) +if (virtio_dev_tx_single_packed_zmbuf (dev, vq, mbuf_pool, + &pkts[pkt_idx]))break ; + pkt_idx++; + remained--; -if (likely (dev->dequeue_zero_copy ==0 )) -update_shadow_used_ring_packed (vq, buf_id,0 , - desc_count); + }while (remained); - pkts[i] =virtio_dev_pktmbuf_alloc (dev, mbuf_pool, buf_len); -if (unlikely (pkts[i] ==NULL )) -break ; - - err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, pkts[i], - mbuf_pool); -if (unlikely (err)) { -rte_pktmbuf_free (pkts[i]); -break ; - } +if (pkt_idx) +vhost_vring_call_packed (dev, vq); -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf; +return pkt_idx; +} - zmbuf =get_zmbuf (vq); -if (!zmbuf) { -rte_pktmbuf_free (pkts[i]); -break ; - } - zmbuf->mbuf = pkts[i]; - zmbuf->desc_idx = buf_id; - zmbuf->desc_count = desc_count; +static __rte_noinline uint16_t +virtio_dev_tx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) +{ +uint32_t pkt_idx =0 ; +uint32_t remained = count; -/* -* Pin lock the mbuf; we will check later to see -* whether the mbuf is freed (when we are the last -* user) or not. If that's the case, we then could -* update the used ring safely. -*/ -rte_mbuf_refcnt_update (pkts[i],1 ); +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); - vq->nr_zmbuf +=1 ; -TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) { +vhost_flush_dequeue_packed (dev, vq); + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -vq_inc_last_avail_packed (vq, desc_count); - } +if (virtio_dev_tx_single_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) +break ; +vhost_flush_dequeue_packed (dev, vq); + pkt_idx++; + remained--; -if (likely (dev->dequeue_zero_copy ==0 )) { + }while (remained); + +if (vq->shadow_used_idx)do_data_copy_dequeue (vq); -if (unlikely (i < count)) - vq->shadow_used_idx = i; -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } -return i; +return pkt_idx; }uint16_t
@@ -2279,9 +2172,14 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, count -=1 ; } -if (vq_is_packed (dev)) - count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, count); -else +if (vq_is_packed (dev)) { +if (unlikely (dev->dequeue_zero_copy)) + count =virtio_dev_tx_packed_zmbuf (dev, vq, mbuf_pool, + pkts, count); +else + count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, + count); + }else count =virtio_dev_tx_split (dev, vq, mbuf_pool, pkts, count); out:
Signature · 1 line
2.17.1
PATCH v7 13/13 vhost: optimize packed ring dequeue when in-order
In reply to Marvin LiuWhen VIRTIO_F_IN_ORDER feature is negotiated, vhost can optimize dequeue function by only update first used descriptor.
lib/librte_vhost/virtio_net.c +62 -2
@@ -31,6 +31,12 @@ rxvq_is_mergeable(struct virtio_net *dev)return dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF ); } +static __rte_always_inline bool +virtio_net_is_inorder (struct virtio_net *dev) +{ +return dev->features & (1ULL <<VIRTIO_F_IN_ORDER ); +} +static bool is_valid_virt_queue_idx (uint32_t idx,int is_tx,uint32_t nr_vring) {
@@ -201,6 +207,24 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } +static __rte_always_inline void +vhost_shadow_dequeue_batch_packed_inorder (struct vhost_virtqueue *vq, +uint16_t id) +{ + vq->shadow_used_packed[0 ].id = id; + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].flags = +PACKED_DESC_DEQUEUE_USED_FLAG (vq->used_wrap_counter); + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -273,6 +297,34 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } +static __rte_always_inline void +vhost_shadow_dequeue_single_packed_inorder (struct vhost_virtqueue *vq, +uint16_t buf_id, +uint16_t count) +{ +uint16_t flags; + + vq->shadow_used_packed[0 ].id = buf_id; + + flags = vq->desc_packed[vq->last_used_idx].flags; +if (vq->used_wrap_counter) { + flags |=VRING_DESC_F_USED ; + flags |=VRING_DESC_F_AVAIL ; + }else { + flags &= ~VRING_DESC_F_USED ; + flags &= ~VRING_DESC_F_AVAIL ; + } + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].flags = flags; + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq, count); +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1841,7 +1893,11 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, (void *)(uintptr_t )(desc_addrs[i] + buf_offset), pkts[i]->pkt_len); -vhost_shadow_dequeue_batch_packed (dev, vq, ids); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_batch_packed_inorder (vq, + ids[PACKED_BATCH_SIZE -1 ]); +else +vhost_shadow_dequeue_batch_packed (dev, vq, ids);vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE );
@@ -1898,7 +1954,11 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, &desc_count))return -1 ; -vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_single_packed_inorder (vq, buf_id, + desc_count); +else +vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count);vq_inc_last_avail_packed (vq, desc_count);
Signature · 1 line
2.17.1
v8 cover vhost packed ring performance optimization
In reply to Marvin LiuPacked ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v8:
- Allocate mbuf by virtio_dev_pktmbuf_alloc
v7:
- Rebase code
- Rename unroll macro and definitions
- Calculate flags when doing single dequeue
v6:
- Fix dequeue zcopy result check
v5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code style
v4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of two
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimization
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by cacheline
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 ++ lib/librte_vhost/virtio_net.c | 948 +++++++++++++++++++++++++++------- 4 files changed, 837 insertions(+), 193 deletions(-)
Signature · 1 line
2.17.1
PATCH v8 1/13 vhost: add packed ring indexes increasing function
When enqueuing or dequeuing, the virtqueue's local available and used indexes are increased.
lib/librte_vhost/vhost.h +20 -0
@@ -367,6 +367,26 @@ desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) wrap_counter != !!(flags &VRING_DESC_F_USED ); } +static inline void +vq_inc_last_used_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_used_idx += num; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + +static inline void +vq_inc_last_avail_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_avail_idx += num; +if (vq->last_avail_idx >= vq->size) { + vq->avail_wrap_counter ^=1 ; + vq->last_avail_idx -= vq->size; + } +} +void __vhost_log_cache_write (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t addr,uint64_t len);
lib/librte_vhost/virtio_net.c +3 -15
@@ -138,11 +138,7 @@ flush_shadow_used_ring_packed(struct virtio_net *dev, head_flags = flags; } - vq->last_used_idx += vq->shadow_used_packed[i].count; -if (vq->last_used_idx >= vq->size) { - vq->used_wrap_counter ^=1 ; - vq->last_used_idx -= vq->size; - } +vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); } __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
@@ -865,11 +861,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,break ; } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, nr_descs); }do_data_copy_enqueue (dev, vq);
@@ -1585,11 +1577,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); } - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, desc_count); }if (likely (dev->dequeue_zero_copy ==0 )) {
Signature · 1 line
2.17.1
PATCH v8 2/13 vhost: add packed ring single enqueue
In reply to Marvin LiuAdd vhost enqueue function for single packet and meanwhile left space for flush used ring function.
lib/librte_vhost/virtio_net.c +80 -0
@@ -770,6 +770,60 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt, +struct buf_vector *buf_vec, +uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; +uint16_t avail_idx = vq->last_avail_idx; +uint16_t max_tries, tries =0 ; +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; +uint32_t size = pkt->pkt_len + dev->vhost_hlen; +uint16_t num_buffers =0 ; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) +return -1 ; + + len =RTE_MIN (len, size); + size -= len; + + num_buffers +=1 ; + + *nr_descs += desc_count; + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 ) +return -1 ; + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -827,6 +881,32 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + +vq_inc_last_avail_packed (vq, nr_descs); + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1
PATCH v8 3/13 vhost: try to unroll for each loop
In reply to Marvin LiuCreate macro for adding unroll pragma before for each loop. Batch functions will be contained of several small loops which can be optimized by compilers' loop unrolling pragma.
lib/librte_vhost/Makefile +18 -0
@@ -16,6 +16,24 @@ CFLAGS += -I vhost_user CFLAGS += -fno-strict-aliasing LDLIBS += -lpthread +ifeq ($(RTE_TOOLCHAIN), gcc) +ifeq ($(shell test $(GCC_VERSION) -ge 83 && echo 1), 1) +CFLAGS += -DVHOST_GCC_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), clang) +ifeq ($(shell test $(CLANG_MAJOR_VERSION)$(CLANG_MINOR_VERSION) -ge 37 && echo 1), 1) +CFLAGS += -DVHOST_CLANG_UNROLL_PRAGMA +endif +endif + +ifeq ($(RTE_TOOLCHAIN), icc) +ifeq ($(shell test $(ICC_MAJOR_VERSION) -ge 16 && echo 1), 1) +CFLAGS += -DVHOST_ICC_UNROLL_PRAGMA +endif +endif + ifeq ($(CONFIG_RTE_LIBRTE_VHOST_NUMA),y) LDLIBS += -lnuma endif
lib/librte_vhost/meson.build +7 -0
@@ -8,6 +8,13 @@ endif if has_libnuma == 1 dpdk_conf.set10('RTE_LIBRTE_VHOST_NUMA', true) endif +if (toolchain == 'gcc' and cc.version().version_compare('>=8.3.0')) + cflags += '-DVHOST_GCC_UNROLL_PRAGMA' +elif (toolchain == 'clang' and cc.version().version_compare('>=3.7.0')) + cflags += '-DVHOST_CLANG_UNROLL_PRAGMA' +elif (toolchain == 'icc' and cc.version().version_compare('>=16.0.0')) + cflags += '-DVHOST_ICC_UNROLL_PRAGMA' +endif dpdk_conf.set('RTE_LIBRTE_VHOST_POSTCOPY', cc.has_header('linux/userfaultfd.h')) version = 4
lib/librte_vhost/vhost.h +24 -0
@@ -39,6 +39,30 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ + sizeof(struct vring_packed_desc)) +#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1) + +#ifdef VHOST_GCC_UNROLL_PRAGMA +#define vhost_for_each_try_unroll (iter, val, size) _Pragma("GCC unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef VHOST_CLANG_UNROLL_PRAGMA +#define vhost_for_each_try_unroll (iter, val, size) _Pragma("unroll 4") \ + for (iter = val; iter < size; iter++) +#endif + +#ifdef VHOST_ICC_UNROLL_PRAGMA +#define vhost_for_each_try_unroll (iter, val, size) _Pragma("unroll (4)") \ + for (iter = val; iter < size; iter++) +#endif + +#ifndef vhost_for_each_try_unroll +#define vhost_for_each_try_unroll (iter, val, num) \ + for (iter = val; iter < num; iter++) +#endif +/** * Structure contains buffer address, length and descriptor index * from vring to do scatter RX.
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v8 03/13] vhost: try to unroll for each loop
Quoted 6 lines · Marvin Liu
Create macro for adding unroll pragma before for each loop. Batch functions will be contained of several small loops which can be optimized by compilers' loop unrolling pragma.
Signed-off-by Marvin Liu <[email protected]>
Thanks,
Maxime
PATCH v8 4/13 vhost: add packed ring batch enqueue
In reply to Marvin LiuBatch enqueue function will first check whether descriptors are cache aligned. It will also check prerequisites in the beginning. Batch enqueue function do not support chained mbufs, single packet enqueue function will handle it.
lib/librte_vhost/virtio_net.c +70 -0
@@ -881,6 +881,76 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int +virtio_dev_rx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts) +{ +bool wrap_counter = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +uint16_t avail_idx = vq->last_avail_idx; +uint64_t desc_addrs[PACKED_BATCH_SIZE ]; +struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; + +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->next !=NULL )) +return -1 ; +if (unlikely (!desc_is_avail (&descs[avail_idx + i], + wrap_counter))) +return -1 ; + } + +rte_smp_rmb (); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (pkts[i]->pkt_len > (lens[i] - buf_offset))) +return -1 ; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i], +VHOST_ACCESS_RW ); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (lens[i] != descs[avail_idx + i].len)) +return -1 ; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + hdrs[i] = (struct virtio_net_hdr_mrg_rxbuf *) + (uintptr_t )desc_addrs[i]; + lens[i] = pkts[i]->pkt_len + dev->vhost_hlen; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +virtio_enqueue_offload (pkts[i], &hdrs[i]->hdr); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +rte_memcpy ((void *)(uintptr_t )(desc_addrs[i] + buf_offset), + rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + pkts[i]->pkt_len); + } + +return 0 ; +} + static __rte_unused int16_t virtio_dev_rx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
Signature · 1 line
2.17.1
PATCH v8 5/13 vhost: add packed ring single dequeue
In reply to Marvin LiuAdd vhost single packet dequeue function for packed ring and meanwhile left space for shadow used ring update function.
lib/librte_vhost/virtio_net.c +55 -0
@@ -1635,6 +1635,61 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_dequeue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t *buf_id, +uint16_t *desc_count) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t buf_len; +uint16_t nr_vec =0 ; +int err; + +if (unlikely (fill_vec_buf_packed (dev, vq, + vq->last_avail_idx, desc_count, + buf_vec, &nr_vec, + buf_id, &buf_len, +VHOST_ACCESS_RO ) <0 )) +return -1 ; + + *pkts =virtio_dev_pktmbuf_alloc (dev, mbuf_pool, buf_len); +if (unlikely (*pkts ==NULL )) { +RTE_LOG (ERR ,VHOST_DATA , +"Failed to allocate memory for mbuf.\n" ); +return -1 ; + } + + err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, *pkts, + mbuf_pool); +if (unlikely (err)) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + +return 0 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ + +uint16_t buf_id, desc_count; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + +vq_inc_last_avail_packed (vq, desc_count); + +return 0 ; +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v8 6/13 vhost: add packed ring batch dequeue
In reply to Marvin LiuAdd batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
lib/librte_vhost/virtio_net.c +111 -0
117 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v8 06/13] vhost: add packed ring batch dequeue
Quoted 5 lines · Marvin Liu
Add batch dequeue function like enqueue function for packed ring, batch dequeue function will not support chained descritpors, single packet dequeue function will handle it.
Signed-off-by Marvin Liu <[email protected]>
Thanks,
Maxime
p.s. It would be better if you could generate the series with providing the diffstat in every patch (which is default behavior of git format- patch).
Quoted 138 lines
lib/librte_vhost/vhost.h +3
-0@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ + VRING_DESC_F_INDIRECT) +#define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \ sizeof(struct vring_packed_desc))#define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)lib/librte_vhost/virtio_net.c +111
-0@@ -1635,6 +1635,117 @@ virtio_dev_tx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return i; } +static __rte_always_inline int +vhost_reserve_avail_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint16_t avail_idx, +uintptr_t *desc_addrs, +uint16_t *ids) +{ +bool wrap = vq->avail_wrap_counter; +struct vring_packed_desc *descs = vq->desc_packed; +struct virtio_net_hdr *hdr; +uint64_t lens[PACKED_BATCH_SIZE ]; +uint64_t buf_lens[PACKED_BATCH_SIZE ]; +uint32_t buf_offset = dev->vhost_hlen; +uint16_t flags, i; + +if (unlikely (avail_idx &PACKED_BATCH_MASK )) +return -1 ; +if (unlikely ((avail_idx +PACKED_BATCH_SIZE ) > vq->size)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + flags = descs[avail_idx + i].flags; +if (unlikely ((wrap != !!(flags &VRING_DESC_F_AVAIL )) || + (wrap == !!(flags &VRING_DESC_F_USED )) || + (flags &PACKED_DESC_SINGLE_DEQUEUE_FLAG ))) +return -1 ; + } + +rte_smp_rmb (); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + lens[i] = descs[avail_idx + i].len; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + desc_addrs[i] =vhost_iova_to_vva (dev, vq, + descs[avail_idx + i].addr, + &lens[i],VHOST_ACCESS_RW ); + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely ((lens[i] != descs[avail_idx + i].len))) +return -1 ; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + pkts[i] =virtio_dev_pktmbuf_alloc (dev, mbuf_pool, lens[i]); +if (!pkts[i]) + goto free_buf; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + buf_lens[i] = pkts[i]->buf_len - pkts[i]->data_off; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (unlikely (buf_lens[i] < (lens[i] - buf_offset))) + goto free_buf; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + pkts[i]->pkt_len = descs[avail_idx + i].len - buf_offset; + pkts[i]->data_len = pkts[i]->pkt_len; + ids[i] = descs[avail_idx + i].id; + } + +if (virtio_net_with_host_offload (dev)) { +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + hdr = (struct virtio_net_hdr *)(desc_addrs[i]); +vhost_dequeue_offload (hdr, pkts[i]); + } + } + +return 0 ; + +free_buf: +for (i =0 ; i <PACKED_BATCH_SIZE ; i++) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t avail_idx = vq->last_avail_idx; +uint32_t buf_offset = dev->vhost_hlen; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_prefetch0 ((void *)(uintptr_t )desc_addrs[i]); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + rte_memcpy(rte_pktmbuf_mtod_offset(pkts[i], void *, 0), + (void *)(uintptr_t )(desc_addrs[i] + buf_offset), + pkts[i]->pkt_len); + +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; +} + static __rte_always_inline int vhost_dequeue_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
PATCH v8 7/13 vhost: flush enqueue updates by cacheline
In reply to Marvin LiuBuffer vhost packed ring enqueue updates, flush ring descs if buffered content filled up one cacheline. Thus virtio can receive packets at a faster frequency.
lib/librte_vhost/vhost.h +2 -0
@@ -174,6 +174,8 @@ struct vhost_virtqueue {struct vring_used_elem_packed *shadow_used_packed; };uint16_t shadow_used_idx; +/* Record packed ring enqueue latest desc cache aligned index */ +uint16_t shadow_aligned_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +100 -1
137 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [dpdk-dev] [PATCH v8 07/13] vhost: flush enqueue updates by cacheline
Quoted 6 lines · Marvin Liu
Buffer vhost packed ring enqueue updates, flush ring descs if buffered content filled up one cacheline. Thus virtio can receive packets at a faster frequency.
Signed-off-by Marvin Liu <[email protected]>
Thanks,
Maxime
PATCH v8 8/13 vhost: flush batched enqueue descs directly
In reply to Marvin LiuFlush used elements when batched enqueue function is finished. Descriptor's flags are pre-calculated as they will be reset by vhost.
lib/librte_vhost/vhost.h +3 -0
@@ -39,6 +39,9 @@#define VHOST_LOG_CACHE_NR 32 +#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ + VRING_DESC_F_WRITE)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
lib/librte_vhost/virtio_net.c +36 -0
@@ -154,6 +154,36 @@ vhost_flush_enqueue_shadow_packed(struct virtio_net *dev,vhost_log_cache_sync (dev, vq); } +static __rte_always_inline void +vhost_flush_enqueue_batch_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +uint64_t *lens, +uint16_t *ids) +{ +uint16_t i; +uint16_t flags; + + flags =PACKED_DESC_ENQUEUE_USED_FLAG (vq->used_wrap_counter); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + vq->desc_packed[vq->last_used_idx + i].id = ids[i]; + vq->desc_packed[vq->last_used_idx + i].len = lens[i]; + } + +rte_smp_wmb (); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + vq->desc_packed[vq->last_used_idx + i].flags = flags; + +vhost_log_cache_used_vring (dev, vq, vq->last_used_idx * +sizeof (struct vring_packed_desc ), +sizeof (struct vring_packed_desc ) * +PACKED_BATCH_SIZE ); +vhost_log_cache_sync (dev, vq); + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline voidflush_shadow_used_ring_packed (struct virtio_net *dev,struct vhost_virtqueue *vq)
@@ -992,6 +1022,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,struct virtio_net_hdr_mrg_rxbuf *hdrs[PACKED_BATCH_SIZE ];uint32_t buf_offset = dev->vhost_hlen;uint64_t lens[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ];uint16_t i;if (unlikely (avail_idx &PACKED_BATCH_MASK ))
@@ -1047,6 +1078,11 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev, pkts[i]->pkt_len); } +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + ids[i] = descs[avail_idx + i].id; + +vhost_flush_enqueue_batch_packed (dev, vq, lens, ids); +return 0 ; }
Signature · 1 line
2.17.1
PATCH v8 9/13 vhost: buffer packed ring dequeue updates
In reply to Marvin LiuBuffer used ring updates as many as possible in vhost dequeue function for coordinating with virtio driver. For supporting buffer, shadow used ring element should contain descriptor's flags. First shadowed ring index was recorded for calculating buffered number.
lib/librte_vhost/vhost.h +5 -0
@@ -42,6 +42,8 @@#define PACKED_DESC_ENQUEUE_USED_FLAG (w) \ ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \ VRING_DESC_F_WRITE) +#define PACKED_DESC_DEQUEUE_USED_FLAG (w) \ + ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED) : 0x0)#define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \ VRING_DESC_F_INDIRECT)
@@ -114,6 +116,7 @@ struct log_cache_entry {struct vring_used_elem_packed {uint16_t id; +uint16_t flags;uint32_t len;uint32_t count; };
@@ -179,6 +182,8 @@ struct vhost_virtqueue {uint16_t shadow_used_idx;/* Record packed ring enqueue latest desc cache aligned index */ uint16_t shadow_aligned_idx; +/* Record packed ring first dequeue desc index */ +uint16_t shadow_last_used_idx;struct vhost_vring_addr ring_addrs;struct batch_copy_elem *batch_copy_elems;
lib/librte_vhost/virtio_net.c +112 -0
142 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v8 10/13 vhost: optimize packed ring enqueue
In reply to Marvin LiuOptimize vhost device packed ring enqueue function by splitting batch and single functions. Packets can be filled into one desc will be handled by batch and others will be handled by single as before.
lib/librte_vhost/virtio_net.c +24 -91
@@ -778,64 +778,6 @@ fill_vec_buf_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,return 0 ; } -/* -* Returns -1 on fail, 0 on success -*/ -static inline int -reserve_avail_buf_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -uint32_t size,struct buf_vector *buf_vec, -uint16_t *nr_vec,uint16_t *num_buffers, -uint16_t *nr_descs) -{ -uint16_t avail_idx; -uint16_t vec_idx =0 ; -uint16_t max_tries, tries =0 ; - -uint16_t buf_id =0 ; -uint32_t len =0 ; -uint16_t desc_count; - - *num_buffers =0 ; - avail_idx = vq->last_avail_idx; - -if (rxvq_is_mergeable (dev)) - max_tries = vq->size -1 ; -else - max_tries =1 ; - -while (size >0 ) { -/* -* if we tried all available ring items, and still -* can't get enough buf, it means something abnormal -* happened. -*/ -if (unlikely (++tries > max_tries)) -return -1 ; - -if (unlikely (fill_vec_buf_packed (dev, vq, - avail_idx, &desc_count, - buf_vec, &vec_idx, - &buf_id, &len, -VHOST_ACCESS_RW ) <0 )) -return -1 ; - - len =RTE_MIN (len, size); -update_shadow_used_ring_packed (vq, buf_id, len, desc_count); - size -= len; - - avail_idx += desc_count; -if (avail_idx >= vq->size) - avail_idx -= vq->size; - - *nr_descs += desc_count; - *num_buffers +=1 ; - } - - *nr_vec = vec_idx; - -return 0 ; -} - static __rte_noinline void copy_vnet_hdr_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq, struct buf_vector *buf_vec,
@@ -1118,7 +1060,7 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } -static __rte_unused int +static __rte_always_inline intvirtio_dev_rx_batch_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts)
@@ -1194,7 +1136,7 @@ virtio_dev_rx_batch_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int16_t +static __rte_always_inline int16_tvirtio_dev_rx_single_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf *pkt)
@@ -1221,49 +1163,40 @@ virtio_dev_rx_single_packed(struct virtio_net *dev, } static __rte_noinline uint32_t -virtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mbuf **pkts,uint32_t count) +virtio_dev_rx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf **pkts, +uint32_t count) {uint32_t pkt_idx =0 ; -uint16_t num_buffers; -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint32_t remained = count; -for (pkt_idx =0 ; pkt_idx < count; pkt_idx++) { -uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen; -uint16_t nr_vec =0 ; -uint16_t nr_descs =0 ; +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); -if (unlikely (reserve_avail_buf_packed (dev, vq, - pkt_len, buf_vec, &nr_vec, - &num_buffers, &nr_descs) <0 )) { -VHOST_LOG_DEBUG (VHOST_DATA , -"(%d) failed to get enough desc from vring\n" , - dev->vid); - vq->shadow_used_idx -= num_buffers; -break ; +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_rx_batch_packed (dev, vq, pkts)) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , - dev->vid, vq->last_avail_idx, - vq->last_avail_idx + num_buffers); - -if (copy_mbuf_to_desc (dev, vq, pkts[pkt_idx], - buf_vec, nr_vec, - num_buffers) <0 ) { - vq->shadow_used_idx -= num_buffers; +if (virtio_dev_rx_single_packed (dev, vq, pkts[pkt_idx]))break ; - } + pkt_idx++; + remained--; -vq_inc_last_avail_packed (vq, nr_descs); - } - -do_data_copy_enqueue (dev, vq); + }while (pkt_idx < count); -if (likely (vq->shadow_used_idx)) { +if (vq->shadow_used_idx) { +do_data_copy_enqueue (dev, vq);vhost_flush_enqueue_shadow_packed (dev, vq); -vhost_vring_call_packed (dev, vq); } +if (pkt_idx) +vhost_vring_call_packed (dev, vq); +return pkt_idx; }
Signature · 1 line
2.17.1
PATCH v8 11/13 vhost: add packed ring zcopy batch and single dequeue
In reply to Marvin LiuAdd vhost packed ring zero copy batch and single dequeue functions like normal dequeue path.
lib/librte_vhost/virtio_net.c +120 -0
@@ -1981,6 +1981,126 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } +static __rte_unused int +virtio_dev_tx_batch_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +struct zcopy_mbuf *zmbufs[PACKED_BATCH_SIZE ]; +uintptr_t desc_addrs[PACKED_BATCH_SIZE ]; +uint16_t ids[PACKED_BATCH_SIZE ]; +uint16_t i; + +uint16_t avail_idx = vq->last_avail_idx; + +if (vhost_reserve_avail_batch_packed (dev, vq, mbuf_pool, pkts, + avail_idx, desc_addrs, ids)) +return -1 ; + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) + zmbufs[i] =get_zmbuf (vq); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { +if (!zmbufs[i]) + goto free_pkt; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) { + zmbufs[i]->mbuf = pkts[i]; + zmbufs[i]->desc_idx = avail_idx + i; + zmbufs[i]->desc_count =1 ; + } + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_mbuf_refcnt_update (pkts[i],1 ); + +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbufs[i], next); + + vq->nr_zmbuf +=PACKED_BATCH_SIZE ; +vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE ); + +return 0 ; + +free_pkt: +vhost_for_each_try_unroll (i,0 ,PACKED_BATCH_SIZE ) +rte_pktmbuf_free (pkts[i]); + +return -1 ; +} + +static __rte_unused int +virtio_dev_tx_single_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts) +{ +uint16_t buf_id, desc_count; +struct zcopy_mbuf *zmbuf; + +if (vhost_dequeue_single_packed (dev, vq, mbuf_pool, pkts, &buf_id, + &desc_count)) +return -1 ; + + zmbuf =get_zmbuf (vq); +if (!zmbuf) { +rte_pktmbuf_free (*pkts); +return -1 ; + } + zmbuf->mbuf = *pkts; + zmbuf->desc_idx = vq->last_avail_idx; + zmbuf->desc_count = desc_count; + +rte_mbuf_refcnt_update (*pkts,1 ); + + vq->nr_zmbuf +=1 ; +TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); + +vq_inc_last_avail_packed (vq, desc_count); +return 0 ; +} + +static __rte_always_inline void +free_zmbuf (struct vhost_virtqueue *vq) +{ +struct zcopy_mbuf *next =NULL ; +struct zcopy_mbuf *zmbuf; + +for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); + zmbuf !=NULL ; zmbuf = next) { + next =TAILQ_NEXT (zmbuf, next); + +uint16_t last_used_idx = vq->last_used_idx; + +if (mbuf_is_consumed (zmbuf->mbuf)) { +uint16_t flags; + flags = vq->desc_packed[last_used_idx].flags; +if (vq->used_wrap_counter) { + flags |=VRING_DESC_F_USED ; + flags |=VRING_DESC_F_AVAIL ; + }else { + flags &= ~VRING_DESC_F_USED ; + flags &= ~VRING_DESC_F_AVAIL ; + } + + vq->desc_packed[last_used_idx].id = zmbuf->desc_idx; + vq->desc_packed[last_used_idx].len =0 ; + +rte_smp_wmb (); + vq->desc_packed[last_used_idx].flags = flags; + +vq_inc_last_used_packed (vq, zmbuf->desc_count); + +TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); +restore_mbuf (zmbuf->mbuf); +rte_pktmbuf_free (zmbuf->mbuf); +put_zmbuf (zmbuf); + vq->nr_zmbuf -=1 ; + } + } +} + static __rte_noinline uint16_tvirtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count)
Signature · 1 line
2.17.1
PATCH v8 12/13 vhost: optimize packed ring dequeue
In reply to Marvin LiuOptimize vhost device packed ring dequeue function by splitting batch and single functions. No-chained and direct descriptors will be handled by batch and other will be handled by single as before.
lib/librte_vhost/virtio_net.c +66 -168
@@ -201,68 +201,6 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } -static __rte_always_inline void -flush_shadow_used_ring_packed (struct virtio_net *dev, -struct vhost_virtqueue *vq) -{ -int i; -uint16_t used_idx = vq->last_used_idx; -uint16_t head_idx = vq->last_used_idx; -uint16_t head_flags =0 ; - -/* Split loop in two to save memory barriers */ -for (i =0 ; i < vq->shadow_used_idx; i++) { - vq->desc_packed[used_idx].id = vq->shadow_used_packed[i].id; - vq->desc_packed[used_idx].len = vq->shadow_used_packed[i].len; - - used_idx += vq->shadow_used_packed[i].count; -if (used_idx >= vq->size) - used_idx -= vq->size; - } - -for (i =0 ; i < vq->shadow_used_idx; i++) { -uint16_t flags; - -if (vq->shadow_used_packed[i].len) - flags =VRING_DESC_F_WRITE ; -else - flags =0 ; - -if (vq->used_wrap_counter) { - flags |=VRING_DESC_F_USED ; - flags |=VRING_DESC_F_AVAIL ; - }else { - flags &= ~VRING_DESC_F_USED ; - flags &= ~VRING_DESC_F_AVAIL ; - } - -if (i >0 ) { - vq->desc_packed[vq->last_used_idx].flags = flags; - -vhost_log_cache_used_vring (dev, vq, - vq->last_used_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - }else { - head_idx = vq->last_used_idx; - head_flags = flags; - } - -vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); - } - -__atomic_store_n (&vq->desc_packed[head_idx].flags, head_flags, - __ATOMIC_RELEASE); - -vhost_log_cache_used_vring (dev, vq, - head_idx * -sizeof (struct vring_packed_desc ), -sizeof (struct vring_packed_desc )); - - vq->shadow_used_idx =0 ; -vhost_log_cache_sync (dev, vq); -} - static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -335,17 +273,6 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } -static __rte_always_inline void -update_shadow_used_ring_packed (struct vhost_virtqueue *vq, -uint16_t desc_idx,uint32_t len,uint16_t count) -{ -uint16_t i = vq->shadow_used_idx++; - - vq->shadow_used_packed[i].id = desc_idx; - vq->shadow_used_packed[i].len = len; - vq->shadow_used_packed[i].count = count; -} -static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -403,7 +330,7 @@ vhost_shadow_enqueue_single_packed(struct virtio_net *dev, } } -static __rte_unused void +static __rte_always_inline voidvhost_flush_dequeue_packed (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1893,7 +1820,7 @@ vhost_reserve_avail_batch_packed(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1961,7 +1888,7 @@ vhost_dequeue_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -1981,7 +1908,7 @@ virtio_dev_tx_single_packed(struct virtio_net *dev,return 0 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -2030,7 +1957,7 @@ virtio_dev_tx_batch_packed_zmbuf(struct virtio_net *dev,return -1 ; } -static __rte_unused int +static __rte_always_inline int virtio_dev_tx_single_packed_zmbuf(struct virtio_net *dev, struct vhost_virtqueue *vq, struct rte_mempool *mbuf_pool,
@@ -2102,111 +2029,77 @@ free_zmbuf(struct vhost_virtqueue *vq) } static __rte_noinline uint16_t -virtio_dev_tx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq, -struct rte_mempool *mbuf_pool,struct rte_mbuf **pkts,uint16_t count) +virtio_dev_tx_packed_zmbuf (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) { -uint16_t i; - -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf, *next; - -for (zmbuf =TAILQ_FIRST (&vq->zmbuf_list); - zmbuf !=NULL ; zmbuf = next) { - next =TAILQ_NEXT (zmbuf, next); +uint32_t pkt_idx =0 ; +uint32_t remained = count; -if (mbuf_is_consumed (zmbuf->mbuf)) { -update_shadow_used_ring_packed (vq, - zmbuf->desc_idx, -0 , - zmbuf->desc_count); +free_zmbuf (vq); -TAILQ_REMOVE (&vq->zmbuf_list, zmbuf, next); -restore_mbuf (zmbuf->mbuf); -rte_pktmbuf_free (zmbuf->mbuf); -put_zmbuf (zmbuf); - vq->nr_zmbuf -=1 ; +do { +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed_zmbuf (dev, vq, + mbuf_pool, &pkts[pkt_idx])) { + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; } } -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } - -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) %s\n" , dev->vid, __func__); - - count =RTE_MIN (count,MAX_PKT_BURST ); -VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) about to dequeue %u buffers\n" , - dev->vid, count); - -for (i =0 ; i < count; i++) { -struct buf_vector buf_vec[BUF_VECTOR_MAX ]; -uint16_t buf_id; -uint32_t buf_len; -uint16_t desc_count, nr_vec =0 ; -int err; - -if (unlikely (fill_vec_buf_packed (dev, vq, - vq->last_avail_idx, &desc_count, - buf_vec, &nr_vec, - &buf_id, &buf_len, -VHOST_ACCESS_RO ) <0 )) +if (virtio_dev_tx_single_packed_zmbuf (dev, vq, mbuf_pool, + &pkts[pkt_idx]))break ; + pkt_idx++; + remained--; -if (likely (dev->dequeue_zero_copy ==0 )) -update_shadow_used_ring_packed (vq, buf_id,0 , - desc_count); + }while (remained); - pkts[i] =virtio_dev_pktmbuf_alloc (dev, mbuf_pool, buf_len); -if (unlikely (pkts[i] ==NULL )) -break ; - - err =copy_desc_to_mbuf (dev, vq, buf_vec, nr_vec, pkts[i], - mbuf_pool); -if (unlikely (err)) { -rte_pktmbuf_free (pkts[i]); -break ; - } +if (pkt_idx) +vhost_vring_call_packed (dev, vq); -if (unlikely (dev->dequeue_zero_copy)) { -struct zcopy_mbuf *zmbuf; +return pkt_idx; +} - zmbuf =get_zmbuf (vq); -if (!zmbuf) { -rte_pktmbuf_free (pkts[i]); -break ; - } - zmbuf->mbuf = pkts[i]; - zmbuf->desc_idx = buf_id; - zmbuf->desc_count = desc_count; +static __rte_noinline uint16_t +virtio_dev_tx_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mempool *mbuf_pool, +struct rte_mbuf **pkts, +uint32_t count) +{ +uint32_t pkt_idx =0 ; +uint32_t remained = count; -/* -* Pin lock the mbuf; we will check later to see -* whether the mbuf is freed (when we are the last -* user) or not. If that's the case, we then could -* update the used ring safely. -*/ -rte_mbuf_refcnt_update (pkts[i],1 ); +do { +rte_prefetch0 (&vq->desc_packed[vq->last_avail_idx]); - vq->nr_zmbuf +=1 ; -TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); +if (remained >=PACKED_BATCH_SIZE ) { +if (!virtio_dev_tx_batch_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) { +vhost_flush_dequeue_packed (dev, vq); + pkt_idx +=PACKED_BATCH_SIZE ; + remained -=PACKED_BATCH_SIZE ; +continue ; + } } -vq_inc_last_avail_packed (vq, desc_count); - } +if (virtio_dev_tx_single_packed (dev, vq, mbuf_pool, + &pkts[pkt_idx])) +break ; +vhost_flush_dequeue_packed (dev, vq); + pkt_idx++; + remained--; -if (likely (dev->dequeue_zero_copy ==0 )) { + }while (remained); + +if (vq->shadow_used_idx)do_data_copy_dequeue (vq); -if (unlikely (i < count)) - vq->shadow_used_idx = i; -if (likely (vq->shadow_used_idx)) { -flush_shadow_used_ring_packed (dev, vq); -vhost_vring_call_packed (dev, vq); - } - } -return i; +return pkt_idx; }uint16_t
@@ -2282,9 +2175,14 @@ rte_vhost_dequeue_burst(int vid, uint16_t queue_id, count -=1 ; } -if (vq_is_packed (dev)) - count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, count); -else +if (vq_is_packed (dev)) { +if (unlikely (dev->dequeue_zero_copy)) + count =virtio_dev_tx_packed_zmbuf (dev, vq, mbuf_pool, + pkts, count); +else + count =virtio_dev_tx_packed (dev, vq, mbuf_pool, pkts, + count); + }else count =virtio_dev_tx_split (dev, vq, mbuf_pool, pkts, count); out:
Signature · 1 line
2.17.1
PATCH v8 13/13 vhost: optimize packed ring dequeue when in-order
In reply to Marvin LiuWhen VIRTIO_F_IN_ORDER feature is negotiated, vhost can optimize dequeue function by only update first used descriptor.
lib/librte_vhost/virtio_net.c +62 -2
@@ -31,6 +31,12 @@ rxvq_is_mergeable(struct virtio_net *dev)return dev->features & (1ULL <<VIRTIO_NET_F_MRG_RXBUF ); } +static __rte_always_inline bool +virtio_net_is_inorder (struct virtio_net *dev) +{ +return dev->features & (1ULL <<VIRTIO_F_IN_ORDER ); +} +static bool is_valid_virt_queue_idx (uint32_t idx,int is_tx,uint32_t nr_vring) {
@@ -201,6 +207,24 @@ vhost_flush_enqueue_batch_packed(struct virtio_net *dev,vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); } +static __rte_always_inline void +vhost_shadow_dequeue_batch_packed_inorder (struct vhost_virtqueue *vq, +uint16_t id) +{ + vq->shadow_used_packed[0 ].id = id; + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].flags = +PACKED_DESC_DEQUEUE_USED_FLAG (vq->used_wrap_counter); + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].count =1 ; + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq,PACKED_BATCH_SIZE ); +} + static __rte_always_inline void vhost_shadow_dequeue_batch_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,
@@ -273,6 +297,34 @@ vhost_shadow_dequeue_single_packed(struct vhost_virtqueue *vq,vq_inc_last_used_packed (vq, count); } +static __rte_always_inline void +vhost_shadow_dequeue_single_packed_inorder (struct vhost_virtqueue *vq, +uint16_t buf_id, +uint16_t count) +{ +uint16_t flags; + + vq->shadow_used_packed[0 ].id = buf_id; + + flags = vq->desc_packed[vq->last_used_idx].flags; +if (vq->used_wrap_counter) { + flags |=VRING_DESC_F_USED ; + flags |=VRING_DESC_F_AVAIL ; + }else { + flags &= ~VRING_DESC_F_USED ; + flags &= ~VRING_DESC_F_AVAIL ; + } + +if (!vq->shadow_used_idx) { + vq->shadow_last_used_idx = vq->last_used_idx; + vq->shadow_used_packed[0 ].len =0 ; + vq->shadow_used_packed[0 ].flags = flags; + vq->shadow_used_idx++; + } + +vq_inc_last_used_packed (vq, count); +} +static inline void do_data_copy_enqueue (struct virtio_net *dev,struct vhost_virtqueue *vq) {
@@ -1844,7 +1896,11 @@ virtio_dev_tx_batch_packed(struct virtio_net *dev, (void *)(uintptr_t )(desc_addrs[i] + buf_offset), pkts[i]->pkt_len); -vhost_shadow_dequeue_batch_packed (dev, vq, ids); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_batch_packed_inorder (vq, + ids[PACKED_BATCH_SIZE -1 ]); +else +vhost_shadow_dequeue_batch_packed (dev, vq, ids);vq_inc_last_avail_packed (vq,PACKED_BATCH_SIZE );
@@ -1901,7 +1957,11 @@ virtio_dev_tx_single_packed(struct virtio_net *dev, &desc_count))return -1 ; -vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count); +if (virtio_net_is_inorder (dev)) +vhost_shadow_dequeue_single_packed_inorder (vq, buf_id, + desc_count); +else +vhost_shadow_dequeue_single_packed (vq, buf_id, desc_count);vq_inc_last_avail_packed (vq, desc_count);
Signature · 1 line
2.17.1
I get some checkpatch warnings, and build fails with clang. Could you please fix these issues and send v9?
Thanks,
Maxime
### [PATCH] vhost: try to unroll for each loop
WARNING:CAMELCASE: Avoid CamelCase: <_Pragma>
#78: FILE: lib/librte_vhost/vhost.h:47:
+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#78: FILE: lib/librte_vhost/vhost.h:47:
+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
+ for (iter = val; iter < size; iter++)
ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#83: FILE: lib/librte_vhost/vhost.h:52:
+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
+ for (iter = val; iter < size; iter++)
ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#88: FILE: lib/librte_vhost/vhost.h:57:
+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
+ for (iter = val; iter < size; iter++)
total: 3 errors, 1 warnings, 67 lines checked
0/1 valid patch/tmp/dpdk_build/lib/librte_vhost/virtio_net.c:2065:1:
error: unused function 'free_zmbuf' [-Werror,-Wunused-function] free_zmbuf(struct vhost_virtqueue *vq)
^
1 error generated.
make[5]: *** [virtio_net.o] Error 1
make[4]: *** [librte_vhost] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [lib] Error 2
make[2]: *** [all] Error 2
make[1]: *** [pre_install] Error 2
make: *** [install] Error 2
Quoted 92 lines · Marvin Liu
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v8:
- Allocate mbuf byvirtio_dev_pktmbuf_allocv7:
- Rebase code
- Rename unroll macro and definitions
- Calculate flags when doing single dequeuev6:
- Fix dequeue zcopy result checkv5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code stylev4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of twov3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimizationv2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ-PKT_BURST)
- Optimize dequeue used ring update whenin_ordernegotiatedMarvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by cacheline
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-orderlib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 ++ lib/librte_vhost/virtio_net.c | 948 +++++++++++++++++++++++++++------- 4 files changed, 837 insertions(+), 193 deletions(-)
Quoted 11 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, October 24, 2019 2:50 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v8 00/13] vhost packed ring performance optimization
Quoted 3 lines
I get some checkpatch warnings, and build fails with clang. Could you please fix these issues and send v9?
Hi Maxime,
Clang build fails will be fixed in v9. For checkpatch warning, it was due to pragma string inside. Previous version can avoid such warning, while format is a little messy as below. I prefer to keep code clean and more readable. How about your idea?
+#ifdef UNROLL_PRAGMA_PARAM
+#define VHOST_UNROLL_PRAGMA(param) _Pragma(param) +#else
+#define VHOST_UNROLL_PRAGMA(param) do {} while (0); +#endif
+ VHOST_UNROLL_PRAGMA(UNROLL_PRAGMA_PARAM)
+ for (i = 0; i < PACKED_BATCH_SIZE; i++)
Regards,
Marvin
Quoted 138 lines
Thanks,
Maxime### [PATCH] vhost: try to unroll for each loop
WARNING:CAMELCASE: Avoid CamelCase: <
_Pragma>
#78: FILE:lib/librte_vhost/vhost.h:47:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("GCC unroll 4")\ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#78: FILE:lib/librte_vhost/vhost.h:47:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("GCC unroll 4")\(quoted diff) +1
-0+ for (iter = val; iter < size; iter++)ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#83: FILE:lib/librte_vhost/vhost.h:52:(quoted diff) +2
-0+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \ + for (iter = val; iter < size; iter++)ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#88: FILE:lib/librte_vhost/vhost.h:57:(quoted diff) +2
-0+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \ + for (iter = val; iter < size; iter++)total: 3 errors, 1 warnings, 67 lines checked
0/1 valid
patch/tmp/dpdk_build/lib/librte_vhost/virtio_net.c:2065:1:
error: unused function 'free_zmbuf' [-Werror,-Wunused-function]free_zmbuf(struct vhost_virtqueue *vq)
^
1 error generated.
make[5]: *** [virtio_net.o] Error 1
make[4]: *** [librte_vhost] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [lib] Error 2
make[2]: *** [all] Error 2
make[1]: *** [pre_install] Error 2
make: *** [install] Error 2Quoted 92 lines · Marvin Liu
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v8:
- Allocate mbuf byvirtio_dev_pktmbuf_allocv7:
- Rebase code
- Rename unroll macro and definitions
- Calculate flags when doing single dequeuev6:
- Fix dequeue zcopy result checkv5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code stylev4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of twov3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimizationv2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ-PKT_BURST)
- Optimize dequeue used ring update whenin_ordernegotiatedMarvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by cacheline
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-orderlib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 ++ lib/librte_vhost/virtio_net.c | 948 +++++++++++++++++++++++++++------- 4 files changed, 837 insertions(+), 193 deletions(-)
Quoted 21 lines · Yong
Quoted 11 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, October 24, 2019 2:50 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v8 00/13] vhost packed ring performance optimizationQuoted 3 lines
I get some checkpatch warnings, and build fails with clang. Could you please fix these issues and send v9?
Hi Maxime,
Clang build fails will be fixed in v9. For checkpatch warning, it was due to pragma string inside. Previous version can avoid such warning, while format is a little messy as below. I prefer to keep code clean and more readable. How about your idea?
Review
Quoted 8 lines
(quoted diff) +7
-0+#ifdef UNROLL_PRAGMA_PARAM +#define VHOST_UNROLL_PRAGMA(param) _Pragma(param) +#else +#define VHOST_UNROLL_PRAGMA(param) do {} while (0); +#endif + VHOST_UNROLL_PRAGMA(UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++)
That's less clean indeed. I agree to waive the checkpatch errors. just fix the Clang build for patch 8 and we're good.
Thanks,
Maxime
Quoted 142 lines
Regards,
MarvinQuoted 138 lines
Thanks,
Maxime### [PATCH] vhost: try to unroll for each loop
WARNING:CAMELCASE: Avoid CamelCase: <
_Pragma>
#78: FILE:lib/librte_vhost/vhost.h:47:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("GCC unroll 4")\ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#78: FILE:lib/librte_vhost/vhost.h:47:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("GCC unroll 4")\(quoted diff) +1
-0+ for (iter = val; iter < size; iter++)ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#83: FILE:lib/librte_vhost/vhost.h:52:(quoted diff) +2
-0+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \ + for (iter = val; iter < size; iter++)ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#88: FILE:lib/librte_vhost/vhost.h:57:(quoted diff) +2
-0+#define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \ + for (iter = val; iter < size; iter++)total: 3 errors, 1 warnings, 67 lines checked
0/1 valid
patch/tmp/dpdk_build/lib/librte_vhost/virtio_net.c:2065:1:
error: unused function 'free_zmbuf' [-Werror,-Wunused-function]free_zmbuf(struct vhost_virtqueue *vq)
^
1 error generated.
make[5]: *** [virtio_net.o] Error 1
make[4]: *** [librte_vhost] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [lib] Error 2
make[2]: *** [all] Error 2
make[1]: *** [pre_install] Error 2
make: *** [install] Error 2Quoted 92 lines · Marvin Liu
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v8:
- Allocate mbuf byvirtio_dev_pktmbuf_allocv7:
- Rebase code
- Rename unroll macro and definitions
- Calculate flags when doing single dequeuev6:
- Fix dequeue zcopy result checkv5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code stylev4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of twov3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimizationv2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ-PKT_BURST)
- Optimize dequeue used ring update whenin_ordernegotiatedMarvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by cacheline
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-orderlib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 ++ lib/librte_vhost/virtio_net.c | 948 +++++++++++++++++++++++++++------- 4 files changed, 837 insertions(+), 193 deletions(-)
Thanks, Maxime. Just sent out v9.
Quoted 197 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, October 24, 2019 4:25 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>; Wang, Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v8 00/13] vhost packed ring performance optimizationQuoted 6 lines · Yong
Quoted 6 lines
-----Original Message-----
From: Maxime Coquelin [mailto:[email protected]]
Sent: Thursday, October 24, 2019 2:50 PM
To: Liu, Yong <[email protected]>; Bie, Tiwei <[email protected]>;Wang,
Quoted 5 lines
Quoted 5 lines
Zhihong <[email protected]>; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v8 00/13] vhost packed ring performance optimizationQuoted 7 lines
Quoted 3 lines
I get some checkpatch warnings, and build fails with clang. Could you please fix these issues and send v9?
Hi Maxime,
Clang build fails will be fixed in v9. For checkpatch warning, it was dueto pragma string inside.
Quoted 1 line
Previous version can avoid such warning, while format is a little messy
as below.
Review
Quoted 10 lines
I prefer to keep code clean and more readable. How about your idea?
(quoted diff) +7
-0+#ifdef UNROLL_PRAGMA_PARAM +#define VHOST_UNROLL_PRAGMA(param) _Pragma(param) +#else +#define VHOST_UNROLL_PRAGMA(param) do {} while (0); +#endif + VHOST_UNROLL_PRAGMA(UNROLL_PRAGMA_PARAM) + for (i = 0; i < PACKED_BATCH_SIZE; i++)That's less clean indeed. I agree to waive the checkpatch errors. just fix the Clang build for patch 8 and we're good.
Thanks,
MaximeQuoted 20 lines
Regards,
MarvinQuoted 17 lines
Thanks,
Maxime### [PATCH] vhost: try to unroll for each loop
WARNING:CAMELCASE: Avoid CamelCase: <
_Pragma>
#78: FILE:lib/librte_vhost/vhost.h:47:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("GCC unroll 4")\ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#78: FILE:lib/librte_vhost/vhost.h:47:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("GCC unroll 4")\(quoted diff) +1
-0+ for (iter = val; iter < size; iter++)Quoted 4 lines
Quoted 4 lines
ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#83: FILE:lib/librte_vhost/vhost.h:52:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("unroll 4")\
Quoted 6 lines
Quoted 6 lines
(quoted diff) +1
-0+ for (iter = val; iter < size; iter++)ERROR:COMPLEX_MACRO: Macros with complex values should be enclosed in parenthesis
#88: FILE:lib/librte_vhost/vhost.h:57:
+#definevhost_for_each_try_unroll(iter, val, size)_Pragma("unroll (4)")\
Quoted 49 lines
Quoted 49 lines
(quoted diff) +1
-0+ for (iter = val; iter < size; iter++)total: 3 errors, 1 warnings, 67 lines checked
0/1 valid
patch/tmp/dpdk_build/lib/librte_vhost/virtio_net.c:2065:1:
error: unused function 'free_zmbuf' [-Werror,-Wunused-function]free_zmbuf(struct vhost_virtqueue *vq)
^
1 error generated.
make[5]: *** [virtio_net.o] Error 1
make[4]: *** [librte_vhost] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [lib] Error 2
make[2]: *** [all] Error 2
make[1]: *** [pre_install] Error 2
make: *** [install] Error 2Quoted 30 lines · Marvin Liu
Packed ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be splitted into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.Quoted 1 line
Quoted 1 line
Quoted 1 line
After all these methods done, single core vhost PvP performance with
64B
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
packet on Xeon 8180 can boost 35%.
v8:
- Allocate mbuf byvirtio_dev_pktmbuf_allocv7:
- Rebase code
- Rename unroll macro and definitions
- Calculate flags when doing single dequeuev6:
- Fix dequeue zcopy result checkv5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code styleQuoted 3 lines
Quoted 3 lines
Quoted 3 lines
v4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABIbreak
Quoted 38 lines
Quoted 37 lines
Quoted 37 lines
- Not assume ring size is power of two
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimizationv2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ-PKT_BURST)
- Optimize dequeue used ring update whenin_ordernegotiatedMarvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by cacheline
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-orderlib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 ++ lib/librte_vhost/virtio_net.c | 948 +++++++++++++++++++++++++++------- 4 files changed, 837 insertions(+), 193 deletions(-)
v9 cover vhost packed ring performance optimization
In reply to Marvin LiuPacked ring has more compact ring format and thus can significantly reduce the number of cache miss. It can lead to better performance. This has been approved in virtio user driver, on normal E5 Xeon cpu single core performance can raise 12%.
http://mails.dpdk.org/archives/dev/2018-April/095470.html
However vhost performance with packed ring performance was decreased. Through analysis, mostly extra cost was from the calculating of each descriptor flag which depended on ring wrap counter. Moreover, both frontend and backend need to write same descriptors which will cause cache contention. Especially when doing vhost enqueue function, virtio refill packed ring function may write same cache line when vhost doing enqueue function. This kind of extra cache cost will reduce the benefit of reducing cache misses.
For optimizing vhost packed ring performance, vhost enqueue and dequeue function will be split into fast and normal path.
Several methods will be taken in fast path:
Handle descriptors in one cache line by batch.
Split loop function into more pieces and unroll them. Prerequisite check that whether I/O space can copy directly into mbuf
space and vice versa.
Prerequisite check that whether descriptor mapping is successful. Distinguish vhost used ring update function by enqueue and dequeue
function.
Buffer dequeue used descriptors as many as possible. Update enqueue used descriptors by cache line.
After all these methods done, single core vhost PvP performance with 64B packet on Xeon 8180 can boost 35%.
v9:
- Fix clang build error
v8:
- Allocate mbuf by virtio_dev_pktmbuf_alloc
v7:
- Rebase code
- Rename unroll macro and definitions
- Calculate flags when doing single dequeue
v6:
- Fix dequeue zcopy result check
v5:
- Remove disable sw prefetch as performance impact is small
- Change unroll pragma macro format
- Rename shadow counter elements names
- Clean dequeue update check condition
- Add inline functions replace of duplicated code
- Unify code style
v4:
- Support meson build
- Remove memory region cache for no clear performance gain and ABI break
- Not assume ring size is power of two
v3:
- Check available index overflow
- Remove dequeue remained descs number check
- Remove changes in split ring datapath
- Call memory write barriers once when updating used flags
- Rename some functions and macros
- Code style optimization
v2:
- Utilize compiler's pragma to unroll loop, distinguish clang/icc/gcc
- Buffered dequeue used desc number changed to (RING_SZ - PKT_BURST)
- Optimize dequeue used ring update when in_order negotiated
Marvin Liu (13):
vhost: add packed ring indexes increasing function
vhost: add packed ring single enqueue
vhost: try to unroll for each loop
vhost: add packed ring batch enqueue
vhost: add packed ring single dequeue
vhost: add packed ring batch dequeue
vhost: flush enqueue updates by cacheline
vhost: flush batched enqueue descs directly
vhost: buffer packed ring dequeue updates
vhost: optimize packed ring enqueue
vhost: add packed ring zcopy batch and single dequeue
vhost: optimize packed ring dequeue
vhost: optimize packed ring dequeue when in-order
lib/librte_vhost/Makefile | 18 + lib/librte_vhost/meson.build | 7 + lib/librte_vhost/vhost.h | 57 ++ lib/librte_vhost/virtio_net.c | 948 +++++++++++++++++++++++++++------- 4 files changed, 837 insertions(+), 193 deletions(-)
Signature · 1 line
2.17.1
PATCH v9 1/13 vhost: add packed ring indexes increasing function
When enqueuing or dequeuing, the virtqueue's local available and used indexes are increased.
--- lib/librte_vhost/vhost.h | 20 ++++++++++++++++++++ lib/librte_vhost/virtio_net.c | 18 +++--------------- 2 files changed, 23 insertions(+), 15 deletions(-)
lib/librte_vhost/vhost.h +20 -0
@@ -367,6 +367,26 @@ desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter) wrap_counter != !!(flags &VRING_DESC_F_USED ); } +static inline void +vq_inc_last_used_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_used_idx += num; +if (vq->last_used_idx >= vq->size) { + vq->used_wrap_counter ^=1 ; + vq->last_used_idx -= vq->size; + } +} + +static inline void +vq_inc_last_avail_packed (struct vhost_virtqueue *vq,uint16_t num) +{ + vq->last_avail_idx += num; +if (vq->last_avail_idx >= vq->size) { + vq->avail_wrap_counter ^=1 ; + vq->last_avail_idx -= vq->size; + } +} +void __vhost_log_cache_write (struct virtio_net *dev,struct vhost_virtqueue *vq,uint64_t addr,uint64_t len);
lib/librte_vhost/virtio_net.c +3 -15
@@ -138,11 +138,7 @@ flush_shadow_used_ring_packed(struct virtio_net *dev, head_flags = flags; } - vq->last_used_idx += vq->shadow_used_packed[i].count; -if (vq->last_used_idx >= vq->size) { - vq->used_wrap_counter ^=1 ; - vq->last_used_idx -= vq->size; - } +vq_inc_last_used_packed (vq, vq->shadow_used_packed[i].count); } __atomic_store_n(&vq->desc_packed[head_idx].flags, head_flags,
@@ -865,11 +861,7 @@ virtio_dev_rx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,break ; } - vq->last_avail_idx += nr_descs; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, nr_descs); }do_data_copy_enqueue (dev, vq);
@@ -1585,11 +1577,7 @@ virtio_dev_tx_packed(struct virtio_net *dev, struct vhost_virtqueue *vq,TAILQ_INSERT_TAIL (&vq->zmbuf_list, zmbuf, next); } - vq->last_avail_idx += desc_count; -if (vq->last_avail_idx >= vq->size) { - vq->last_avail_idx -= vq->size; - vq->avail_wrap_counter ^=1 ; - } +vq_inc_last_avail_packed (vq, desc_count); }if (likely (dev->dequeue_zero_copy ==0 )) {
Signature · 1 line
2.17.1
PATCH v9 2/13 vhost: add packed ring single enqueue
In reply to Marvin LiuAdd vhost enqueue function for single packet and meanwhile left space for flush used ring function.
--- lib/librte_vhost/virtio_net.c | 80 +++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
lib/librte_vhost/virtio_net.c +80 -0
@@ -770,6 +770,60 @@ copy_mbuf_to_desc(struct virtio_net *dev, struct vhost_virtqueue *vq,return error; } +static __rte_always_inline int +vhost_enqueue_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt, +struct buf_vector *buf_vec, +uint16_t *nr_descs) +{ +uint16_t nr_vec =0 ; +uint16_t avail_idx = vq->last_avail_idx; +uint16_t max_tries, tries =0 ; +uint16_t buf_id =0 ; +uint32_t len =0 ; +uint16_t desc_count; +uint32_t size = pkt->pkt_len + dev->vhost_hlen; +uint16_t num_buffers =0 ; + +if (rxvq_is_mergeable (dev)) + max_tries = vq->size -1 ; +else + max_tries =1 ; + +while (size >0 ) { +/* +* if we tried all available ring items, and still +* can't get enough buf, it means something abnormal +* happened. +*/ +if (unlikely (++tries > max_tries)) +return -1 ; + +if (unlikely (fill_vec_buf_packed (dev, vq, + avail_idx, &desc_count, + buf_vec, &nr_vec, + &buf_id, &len, +VHOST_ACCESS_RW ) <0 )) +return -1 ; + + len =RTE_MIN (len, size); + size -= len; + + num_buffers +=1 ; + + *nr_descs += desc_count; + avail_idx += desc_count; +if (avail_idx >= vq->size) + avail_idx -= vq->size; + } + +if (copy_mbuf_to_desc (dev, vq, pkt, buf_vec, nr_vec, num_buffers) <0 ) +return -1 ; + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_split (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
@@ -827,6 +881,32 @@ virtio_dev_rx_split(struct virtio_net *dev, struct vhost_virtqueue *vq,return pkt_idx; } +static __rte_unused int16_t +virtio_dev_rx_single_packed (struct virtio_net *dev, +struct vhost_virtqueue *vq, +struct rte_mbuf *pkt) +{ +struct buf_vector buf_vec[BUF_VECTOR_MAX ]; +uint16_t nr_descs =0 ; + +rte_smp_rmb (); +if (unlikely (vhost_enqueue_single_packed (dev, vq, pkt, buf_vec, + &nr_descs) <0 )) { +VHOST_LOG_DEBUG (VHOST_DATA , +"(%d) failed to get enough desc from vring\n" , + dev->vid); +return -1 ; + } + +VHOST_LOG_DEBUG (VHOST_DATA ,"(%d) current index %d | end index %d\n" , + dev->vid, vq->last_avail_idx, + vq->last_avail_idx + nr_descs); + +vq_inc_last_avail_packed (vq, nr_descs); + +return 0 ; +} + static __rte_noinline uint32_tvirtio_dev_rx_packed (struct virtio_net *dev,struct vhost_virtqueue *vq,struct rte_mbuf **pkts,uint32_t count)
Signature · 1 line
2.17.1