Skip to contentSkip to conversation

optimize vhost enqueue

PATCH Superseded Zhihong Wang 7 participants 141 messages Aug 16, 2016 – Oct 26, 2016 Page 2 of 2

lore
Share & tools
lore
#130 lore raw

PATCH v7 2/7 vhost: optimize cache access

In reply to Yuanhan Liu

From: Zhihong Wang <[email protected]>

This patch reorders the code to delay virtio header write to improve cache access efficiency for cases where the mrg_rxbuf feature is turned on. CPU pipeline stall cycles can be significantly reduced.

Virtio header write and mbuf data copy are all remote store operations which takes a long time to finish. It's a good idea to put them together to remove bubbles in between, to let as many remote store instructions as possible go into store buffer at the same time to hide latency, and to let the H/W prefetcher goes to work as early as possible.

On a Haswell machine, about 100 cycles can be saved per packet by this patch alone. Taking 64B packets traffic for example, this means about 60% efficiency improvement for the enqueue operation.

---
 lib/librte_vhost/virtio_net.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
lib/librte_vhost/virtio_net.c +16 -6
index 812e5d3..d4fc62a 100644
@@ -390,6 +390,8 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	uint32_t desc_offset, desc_avail;
 	uint32_t cpy_len;
 	uint16_t desc_idx, used_idx;
+	uint64_t hdr_addr, hdr_phys_addr;
+	struct rte_mbuf *hdr_mbuf;
 
 	if (unlikely(m == NULL))
 		return 0;
@@ -401,17 +403,15 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
 		return 0;
 
-	rte_prefetch0((void *)(uintptr_t)desc_addr);
+	hdr_mbuf = m;
+	hdr_addr = desc_addr;
+	hdr_phys_addr = buf_vec[vec_idx].buf_addr;
+	rte_prefetch0((void *)(uintptr_t)hdr_addr);
 
 	virtio_hdr.num_buffers = end_idx - start_idx;
 	LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
 		dev->vid, virtio_hdr.num_buffers);
 
-	virtio_enqueue_offload(m, &virtio_hdr.hdr);
-	copy_virtio_net_hdr(dev, desc_addr, virtio_hdr);
-	vhost_log_write(dev, buf_vec[vec_idx].buf_addr, dev->vhost_hlen);
-	PRINT_PACKET(dev, (uintptr_t)desc_addr, dev->vhost_hlen, 0);
-
 	desc_avail  = buf_vec[vec_idx].buf_len - dev->vhost_hlen;
 	desc_offset = dev->vhost_hlen;
 	desc_chain_head = buf_vec[vec_idx].desc_idx;
@@ -456,6 +456,16 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 			mbuf_avail  = rte_pktmbuf_data_len(m);
 		}
 
+		if (hdr_addr) {
+			virtio_enqueue_offload(hdr_mbuf, &virtio_hdr.hdr);
+			copy_virtio_net_hdr(dev, hdr_addr, virtio_hdr);
+			vhost_log_write(dev, hdr_phys_addr, dev->vhost_hlen);
+			PRINT_PACKET(dev, (uintptr_t)hdr_addr,
+				     dev->vhost_hlen, 0);
+
+			hdr_addr = 0;
+		}
+
 		cpy_len = RTE_MIN(desc_avail, mbuf_avail);
 		rte_memcpy((void *)((uintptr_t)(desc_addr + desc_offset)),
 			rte_pktmbuf_mtod_offset(m, void *, mbuf_offset),
Signature · 1 line
1.9.0
#131 lore raw

PATCH v7 3/7 vhost: simplify mergeable Rx vring reservation

In reply to Yuanhan Liu

Let it return "num_buffers" we reserved, so that we could re-use it with copy_mbuf_to_desc_mergeable() directly, instead of calculating it again there.

Meanwhile, the return type of copy_mbuf_to_desc_mergeable is changed to "int". -1 will be return on error.

---
 lib/librte_vhost/virtio_net.c | 41 +++++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 20 deletions(-)
lib/librte_vhost/virtio_net.c +21 -20
index d4fc62a..1a40c91 100644
@@ -336,7 +336,7 @@ fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
  */
 static inline int
 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
-			    uint16_t *end, struct buf_vector *buf_vec)
+			    struct buf_vector *buf_vec, uint16_t *num_buffers)
 {
 	uint16_t cur_idx;
 	uint16_t avail_idx;
@@ -370,19 +370,18 @@ reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
 			return -1;
 	}
 
-	*end = cur_idx;
+	*num_buffers = cur_idx - vq->last_used_idx;
 	return 0;
 }
 
-static inline uint32_t __attribute__((always_inline))
+static inline int __attribute__((always_inline))
 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
-			    uint16_t end_idx, struct rte_mbuf *m,
-			    struct buf_vector *buf_vec)
+			    struct rte_mbuf *m, struct buf_vector *buf_vec,
+			    uint16_t num_buffers)
 {
 	struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
 	uint32_t vec_idx = 0;
-	uint16_t start_idx = vq->last_used_idx;
-	uint16_t cur_idx = start_idx;
+	uint16_t cur_idx = vq->last_used_idx;
 	uint64_t desc_addr;
 	uint32_t desc_chain_head;
 	uint32_t desc_chain_len;
@@ -394,21 +393,21 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	struct rte_mbuf *hdr_mbuf;
 
 	if (unlikely(m == NULL))
-		return 0;
+		return -1;
 
 	LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
 		dev->vid, cur_idx, end_idx);
 
 	desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
 	if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
-		return 0;
+		return -1;
 
 	hdr_mbuf = m;
 	hdr_addr = desc_addr;
 	hdr_phys_addr = buf_vec[vec_idx].buf_addr;
 	rte_prefetch0((void *)(uintptr_t)hdr_addr);
 
-	virtio_hdr.num_buffers = end_idx - start_idx;
+	virtio_hdr.num_buffers = num_buffers;
 	LOG_DEBUG(VHOST_DATA, "(%d) RX: num merge buffers %d\n",
 		dev->vid, virtio_hdr.num_buffers);
 
@@ -440,7 +439,7 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 
 			desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
 			if (unlikely(!desc_addr))
-				return 0;
+				return -1;
 
 			/* Prefetch buffer address. */
 			rte_prefetch0((void *)(uintptr_t)desc_addr);
@@ -489,7 +488,7 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 		offsetof(struct vring_used, ring[used_idx]),
 		sizeof(vq->used->ring[used_idx]));
 
-	return end_idx - start_idx;
+	return 0;
 }
 
 static inline uint32_t __attribute__((always_inline))
@@ -497,8 +496,8 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 	struct rte_mbuf **pkts, uint32_t count)
 {
 	struct vhost_virtqueue *vq;
-	uint32_t pkt_idx = 0, nr_used = 0;
-	uint16_t end;
+	uint32_t pkt_idx = 0;
+	uint16_t num_buffers;
 	struct buf_vector buf_vec[BUF_VECTOR_MAX];
 
 	LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
@@ -519,22 +518,24 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 	for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
 		uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
 
-		if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len,
-							 &end, buf_vec) < 0)) {
+		if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len, buf_vec,
+							 &num_buffers) < 0)) {
 			LOG_DEBUG(VHOST_DATA,
 				"(%d) failed to get enough desc from vring\n",
 				dev->vid);
 			break;
 		}
 
-		nr_used = copy_mbuf_to_desc_mergeable(dev, vq, end,
-						      pkts[pkt_idx], buf_vec);
+		if (copy_mbuf_to_desc_mergeable(dev, vq, pkts[pkt_idx],
+						buf_vec, num_buffers) < 0)
+			break;
+
 		rte_smp_wmb();
 
-		*(volatile uint16_t *)&vq->used->idx += nr_used;
+		*(volatile uint16_t *)&vq->used->idx += num_buffers;
 		vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
 			sizeof(vq->used->idx));
-		vq->last_used_idx += nr_used;
+		vq->last_used_idx += num_buffers;
 	}
 
 	if (likely(pkt_idx)) {
Signature · 1 line
1.9.0
#132 lore raw

Re: [PATCH v7 3/7] vhost: simplify mergeable Rx vring reservation

2016-10-14 17:34, Yuanhan Liu:

Quoted 17 lines
lib/librte_vhost/virtio_net.c +4 -5
-static inline uint32_t __attribute__((always_inline))
+static inline int __attribute__((always_inline))
 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
-			    uint16_t end_idx, struct rte_mbuf *m,
-			    struct buf_vector *buf_vec)
+			    struct rte_mbuf *m, struct buf_vector *buf_vec,
+			    uint16_t num_buffers)
 {
 	struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
 	uint32_t vec_idx = 0;
-	uint16_t start_idx = vq->last_used_idx;
-	uint16_t cur_idx = start_idx;
+	uint16_t cur_idx = vq->last_used_idx;
 	uint64_t desc_addr;
 	uint32_t desc_chain_head;
 	uint32_t desc_chain_len;

lib/librte_vhost/virtio_net.c copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 399

Quoted 8 lines
lib/librte_vhost/virtio_net.c +1 -1
@@ -394,21 +393,21 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	struct rte_mbuf *hdr_mbuf;
 
 	if (unlikely(m == NULL))
-		return 0;
+		return -1;
 
 	LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
 		dev->vid, cur_idx, end_idx);

There is a build error:
lib/librte_vhost/virtio_net.c:399:22: error: ‘end_idx’ undeclared

It is probably trivial and could be fixed directly in the already applied commit in next-virtio.

#133 lore raw

Re: [PATCH v7 3/7] vhost: simplify mergeable Rx vring reservation

Quoted 24 lines · Thomas Monjalon

2016-10-14 17:34, Yuanhan Liu:

Quoted 23 lines
lib/librte_vhost/virtio_net.c +5 -6
-static inline uint32_t __attribute__((always_inline))
+static inline int __attribute__((always_inline))
 copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
-			    uint16_t end_idx, struct rte_mbuf *m,
-			    struct buf_vector *buf_vec)
+			    struct rte_mbuf *m, struct buf_vector *buf_vec,
+			    uint16_t num_buffers)
 {
 	struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0, 0, 0, 0, 0, 0}, 0};
 	uint32_t vec_idx = 0;
-	uint16_t start_idx = vq->last_used_idx;
-	uint16_t cur_idx = start_idx;
+	uint16_t cur_idx = vq->last_used_idx;
 	uint64_t desc_addr;
 	uint32_t desc_chain_head;
 	uint32_t desc_chain_len;
@@ -394,21 +393,21 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq,
 	struct rte_mbuf *hdr_mbuf;
 
 	if (unlikely(m == NULL))
-		return 0;
+		return -1;
 
Quoted 5 lines

lib/librte_vhost/virtio_net.c copy_mbuf_to_desc_mergeable(struct virtio_net *dev, struct vhost_virtqueue *vq, · line 399

Quoted 2 lines
lib/librte_vhost/virtio_net.c +0 -0
 	LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
 		dev->vid, cur_idx, end_idx);

There is a build error:
lib/librte_vhost/virtio_net.c:399:22: error: ‘end_idx’ undeclared

Oops... you know, my robot is broken since the holiday :( I just had a quick fix. Hopefully, it will start working again...

Quoted 2 lines

It is probably trivial and could be fixed directly in the already applied commit in next-virtio.

Yes, and FYI, here is the overall diffs I made to fix this bug.

	--yliu

    ---
    diff --git a/lib/librte_vhost/virtio_net.c
    b/lib/librte_vhost/virtio_net.c
    index b784dba..eed0b1c 100644
    --- a/lib/librte_vhost/virtio_net.c
    +++ b/lib/librte_vhost/virtio_net.c
    @@ -443,9 +443,6 @@ copy_mbuf_to_desc_mergeable(struct virtio_net *dev,
    struct rte_mbuf *m,
            if (unlikely(m == NULL))
                    return -1;
    
    -       LOG_DEBUG(VHOST_DATA, "(%d) current index %d | end index %d\n",
    -               dev->vid, cur_idx, end_idx);
    -
            desc_addr = gpa_to_vva(dev, buf_vec[vec_idx].buf_addr);
            if (buf_vec[vec_idx].buf_len < dev->vhost_hlen || !desc_addr)
                    return -1;
    @@ -555,6 +552,10 @@ virtio_dev_merge_rx(struct virtio_net *dev,
    uint16_t queue_id,
                            break;
                    }
    
    +               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_mergeable(dev, pkts[pkt_idx],
                                                    buf_vec, num_buffers) < 0) {
                            vq->shadow_used_idx -= num_buffers;
#134 lore raw

PATCH v7 4/7 vhost: use last avail idx for avail ring reservation

In reply to Yuanhan Liu

shadow_used_ring will be introduced later. Since then last avail idx will not be updated together with last used idx.

So, here we use last_avail_idx for avail ring reservation.

---
 lib/librte_vhost/virtio_net.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/librte_vhost/virtio_net.c +3 -2
index 1a40c91..b5ba633 100644
@@ -344,7 +344,7 @@ reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
 	uint32_t vec_idx = 0;
 	uint16_t tries = 0;
 
-	cur_idx  = vq->last_used_idx;
+	cur_idx = vq->last_avail_idx;
 
 	while (1) {
 		avail_idx = *((volatile uint16_t *)&vq->avail->idx);
@@ -370,7 +370,7 @@ reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
 			return -1;
 	}
 
-	*num_buffers = cur_idx - vq->last_used_idx;
+	*num_buffers = cur_idx - vq->last_avail_idx;
 	return 0;
 }
 
@@ -536,6 +536,7 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 		vhost_log_used_vring(dev, vq, offsetof(struct vring_used, idx),
 			sizeof(vq->used->idx));
 		vq->last_used_idx += num_buffers;
+		vq->last_avail_idx += num_buffers;
 	}
 
 	if (likely(pkt_idx)) {
Signature · 1 line
1.9.0
#135 lore raw

PATCH v7 5/7 vhost: shadow used ring update

In reply to Yuanhan Liu

From: Zhihong Wang <[email protected]>

The basic idea is to shadow the used ring update: update them into a local buffer first, and then flush them all to the virtio used vring at once in the end.

And since we do avail ring reservation before enqueuing data, we would know which and how many descs will be used. Which means we could update the shadow used ring at the reservation time. It also introduce another slight advantage: we don't need access the desc->flag any more inside copy_mbuf_to_desc_mergeable().

---
 lib/librte_vhost/vhost.c      |  13 +++-
 lib/librte_vhost/vhost.h      |   3 +
 lib/librte_vhost/vhost_user.c |  23 +++++--
 lib/librte_vhost/virtio_net.c | 138 +++++++++++++++++++++++++-----------------
 4 files changed, 113 insertions(+), 64 deletions(-)
lib/librte_vhost/vhost.c +11 -2
index 469117a..d8116ff 100644
@@ -121,9 +121,18 @@ static void
 free_device(struct virtio_net *dev)
 {
 	uint32_t i;
+	struct vhost_virtqueue *rxq, *txq;
 
-	for (i = 0; i < dev->virt_qp_nb; i++)
-		rte_free(dev->virtqueue[i * VIRTIO_QNUM]);
+	for (i = 0; i < dev->virt_qp_nb; i++) {
+		rxq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
+		txq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
+
+		rte_free(rxq->shadow_used_ring);
+		rte_free(txq->shadow_used_ring);
+
+		/* rxq and txq are allocated together as queue-pair */
+		rte_free(rxq);
+	}
 
 	rte_free(dev);
 }
lib/librte_vhost/vhost.h +3 -0
index 17c557f..acec772 100644
@@ -105,6 +105,9 @@ struct vhost_virtqueue {
 	uint16_t		last_zmbuf_idx;
 	struct zcopy_mbuf	*zmbufs;
 	struct zcopy_mbuf_list	zmbuf_list;
+
+	struct vring_used_elem  *shadow_used_ring;
+	uint16_t                shadow_used_idx;
 } __rte_cache_aligned;
 
 /* Old kernels have no such macro defined */
lib/librte_vhost/vhost_user.c +18 -5
index 3074227..6b83c15 100644
@@ -198,6 +198,15 @@ vhost_user_set_vring_num(struct virtio_net *dev,
 		}
 	}
 
+	vq->shadow_used_ring = rte_malloc(NULL,
+				vq->size * sizeof(struct vring_used_elem),
+				RTE_CACHE_LINE_SIZE);
+	if (!vq->shadow_used_ring) {
+		RTE_LOG(ERR, VHOST_CONFIG,
+			"failed to allocate memory for shadow used ring.\n");
+		return -1;
+	}
+
 	return 0;
 }
 
@@ -711,6 +720,8 @@ static int
 vhost_user_get_vring_base(struct virtio_net *dev,
 			  struct vhost_vring_state *state)
 {
+	struct vhost_virtqueue *vq = dev->virtqueue[state->index];
+
 	/* We have to stop the queue (virtio) if it is running. */
 	if (dev->flags & VIRTIO_DEV_RUNNING) {
 		dev->flags &= ~VIRTIO_DEV_RUNNING;
@@ -718,7 +729,7 @@ vhost_user_get_vring_base(struct virtio_net *dev,
 	}
 
 	/* Here we are safe to get the last used index */
-	state->num = dev->virtqueue[state->index]->last_used_idx;
+	state->num = vq->last_used_idx;
 
 	RTE_LOG(INFO, VHOST_CONFIG,
 		"vring base idx:%d file:%d\n", state->index, state->num);
@@ -727,13 +738,15 @@ vhost_user_get_vring_base(struct virtio_net *dev,
 	 * sent and only sent in vhost_vring_stop.
 	 * TODO: cleanup the vring, it isn't usable since here.
 	 */
-	if (dev->virtqueue[state->index]->kickfd >= 0)
-		close(dev->virtqueue[state->index]->kickfd);
+	if (vq->kickfd >= 0)
+		close(vq->kickfd);
 
-	dev->virtqueue[state->index]->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
+	vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 
 	if (dev->dequeue_zero_copy)
-		free_zmbufs(dev->virtqueue[state->index]);
+		free_zmbufs(vq);
+	rte_free(vq->shadow_used_ring);
+	vq->shadow_used_ring = NULL;
 
 	return 0;
 }
lib/librte_vhost/virtio_net.c +81 -57

240 lines not on this page · raw message

Signature · 1 line
1.9.0
#136 lore raw

PATCH v7 6/7 vhost: prefetch avail ring

In reply to Yuanhan Liu
---
 lib/librte_vhost/virtio_net.c | 2 ++
 1 file changed, 2 insertions(+)
lib/librte_vhost/virtio_net.c +2 -0
index 2bdc2fe..12a037b 100644
@@ -539,6 +539,8 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 	if (count == 0)
 		return 0;
 
+	rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
+
 	vq->shadow_used_idx = 0;
 	for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
 		uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
Signature · 1 line
1.9.0
#137 lore raw

PATCH v7 7/7 vhost: retrieve avail head once

In reply to Yuanhan Liu

There is no need to retrieve the latest avail head every time we enqueue a packet in the mereable Rx path by

    avail_idx = *((volatile uint16_t *)&vq->avail->idx);

Instead, we could just retrieve it once at the beginning of the enqueue path. This could diminish the cache penalty slightly, because the virtio driver could be updating it while vhost is reading it (for each packet).

---
 lib/librte_vhost/virtio_net.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
lib/librte_vhost/virtio_net.c +6 -5
index 12a037b..b784dba 100644
@@ -387,10 +387,10 @@ fill_vec_buf(struct vhost_virtqueue *vq, uint32_t avail_idx,
  */
 static inline int
 reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
-			    struct buf_vector *buf_vec, uint16_t *num_buffers)
+			    struct buf_vector *buf_vec, uint16_t *num_buffers,
+			    uint16_t avail_head)
 {
 	uint16_t cur_idx;
-	uint16_t avail_idx;
 	uint32_t vec_idx = 0;
 	uint16_t tries = 0;
 
@@ -401,8 +401,7 @@ reserve_avail_buf_mergeable(struct vhost_virtqueue *vq, uint32_t size,
 	cur_idx  = vq->last_avail_idx;
 
 	while (size > 0) {
-		avail_idx = *((volatile uint16_t *)&vq->avail->idx);
-		if (unlikely(cur_idx == avail_idx))
+		if (unlikely(cur_idx == avail_head))
 			return -1;
 
 		if (unlikely(fill_vec_buf(vq, cur_idx, &vec_idx, buf_vec,
@@ -523,6 +522,7 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 	uint32_t pkt_idx = 0;
 	uint16_t num_buffers;
 	struct buf_vector buf_vec[BUF_VECTOR_MAX];
+	uint16_t avail_head;
 
 	LOG_DEBUG(VHOST_DATA, "(%d) %s\n", dev->vid, __func__);
 	if (unlikely(!is_valid_virt_queue_idx(queue_id, 0, dev->virt_qp_nb))) {
@@ -542,11 +542,12 @@ virtio_dev_merge_rx(struct virtio_net *dev, uint16_t queue_id,
 	rte_prefetch0(&vq->avail->ring[vq->last_avail_idx & (vq->size - 1)]);
 
 	vq->shadow_used_idx = 0;
+	avail_head = *((volatile uint16_t *)&vq->avail->idx);
 	for (pkt_idx = 0; pkt_idx < count; pkt_idx++) {
 		uint32_t pkt_len = pkts[pkt_idx]->pkt_len + dev->vhost_hlen;
 
 		if (unlikely(reserve_avail_buf_mergeable(vq, pkt_len, buf_vec,
-							 &num_buffers) < 0)) {
+					&num_buffers, avail_head) < 0)) {
 			LOG_DEBUG(VHOST_DATA,
 				"(%d) failed to get enough desc from vring\n",
 				dev->vid);
Signature · 1 line
1.9.0
Reviewed-by on the v7 series
#138 lore raw

Re: [PATCH v7 0/7] vhost: optimize mergeable Rx path

In reply to Yuanhan Liu
Quoted 4 lines · Yuanhan Liu

This is a new set of patches to optimize the mergeable Rx code path. No refactoring (rewrite) was made this time. It just applies some findings from Zhihong (kudos to him!) that could improve the mergeable Rx path on the old code.

......

Quoted 12 lines

---
Yuanhan Liu (4):
vhost: simplify mergeable Rx vring reservation
vhost: use last avail idx for avail ring reservation
vhost: prefetch avail ring
vhost: retrieve avail head once

Zhihong Wang (3):
vhost: remove useless volatile
vhost: optimize cache access
vhost: shadow used ring update

Quoted 6 lines
 lib/librte_vhost/vhost.c      |  13 ++-
 lib/librte_vhost/vhost.h      |   5 +-
 lib/librte_vhost/vhost_user.c |  23 +++--
 lib/librte_vhost/virtio_net.c | 193 +++++++++++++++++++++++++-----------------
 4 files changed, 149 insertions(+), 85 deletions(-)
Tested-by on the v7 series
#139 lore raw

Re: [PATCH v7 0/7] vhost: optimize mergeable Rx path

In reply to Yuanhan Liu

Hi Yuanhan,

Quoted 29 lines · Yuanhan Liu

This is a new set of patches to optimize the mergeable Rx code path. No refactoring (rewrite) was made this time. It just applies some findings from Zhihong (kudos to him!) that could improve the mergeable Rx path on the old code.

The two major factors that could improve the performance greatly are:

- copy virtio header together with packet data. This could remove the buubbles between the two copy to optimize the cache access.

This is implemented in patch 2 "vhost: optimize cache access"

- shadow used ring update and update them at once

The basic idea is to update used ring in a local buffer and flush them to the virtio used ring at once in the end. Again, this is for optimizing the cache access.

This is implemented in patch 5 "vhost: shadow used ring update"

The two optimizations could yield 40+% performance in micro testing and 20+% in PVP case testing with 64B packet size.

Besides that, there are some tiny optimizations, such as prefetch avail ring (patch 6) and retrieve avail head once (patch 7).

Note: the shadow used ring tech could also be applied to the non-mrg Rx path (and even the dequeu) path. I didn't do that for two reasons:

Quoted 6 lines

- we already update used ring in batch in both path: it's not shadowed first though.

- it's a bit too late too make many changes at this stage: RC1 is out.

Please help testing.

I tested the following use-cases without noticing any functional problems:
- Windows Guests (mergeable ON & OFF, indirect disabled): ping other VM
- Linux guests with Kernel driver (mergeable ON & OFF, indirect OFF):
iperf between 2 VMs
- Linux guest with Virtio PMD (mergeable ON & OFF): testpmd txonly on
host, rxonly on guest.

Feel free to add my:

Thanks,
Maxime

Applied
#140 lore raw

Re: [PATCH v7 0/7] vhost: optimize mergeable Rx path

In reply to Yuanhan Liu

Applied to dpdk-next-virtio.

And thanks for testing and reviewing.

	--yliu
Quoted 61 lines · Yuanhan Liu

This is a new set of patches to optimize the mergeable Rx code path. No refactoring (rewrite) was made this time. It just applies some findings from Zhihong (kudos to him!) that could improve the mergeable Rx path on the old code.

The two major factors that could improve the performance greatly are:

- copy virtio header together with packet data. This could remove the buubbles between the two copy to optimize the cache access.

This is implemented in patch 2 "vhost: optimize cache access"

- shadow used ring update and update them at once

The basic idea is to update used ring in a local buffer and flush them to the virtio used ring at once in the end. Again, this is for optimizing the cache access.

This is implemented in patch 5 "vhost: shadow used ring update"

The two optimizations could yield 40+% performance in micro testing and 20+% in PVP case testing with 64B packet size.

Besides that, there are some tiny optimizations, such as prefetch avail ring (patch 6) and retrieve avail head once (patch 7).

Note: the shadow used ring tech could also be applied to the non-mrg Rx path (and even the dequeu) path. I didn't do that for two reasons:

- we already update used ring in batch in both path: it's not shadowed first though.

- it's a bit too late too make many changes at this stage: RC1 is out.

Please help testing.

Thanks.

	--yliu
Cc Jianbo Liu <[email protected]>

---
Yuanhan Liu (4):
vhost: simplify mergeable Rx vring reservation
vhost: use last avail idx for avail ring reservation
vhost: prefetch avail ring
vhost: retrieve avail head once

Zhihong Wang (3):
vhost: remove useless volatile
vhost: optimize cache access
vhost: shadow used ring update

 lib/librte_vhost/vhost.c      |  13 ++-
 lib/librte_vhost/vhost.h      |   5 +-
 lib/librte_vhost/vhost_user.c |  23 +++--
 lib/librte_vhost/virtio_net.c | 193 +++++++++++++++++++++++++-----------------
 4 files changed, 149 insertions(+), 85 deletions(-)
Signature · 1 line
1.9.0