[dpdk-dev,v2,1/4] net/virtio: fix queue flushing with vector Rx enabled

Message ID 20180119155556.32597-2-olivier.matz@6wind.com (mailing list archive)
State Superseded, archived
Delegated to: Yuanhan Liu
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation fail Compilation issues

Commit Message

Olivier Matz Jan. 19, 2018, 3:55 p.m. UTC
  When using vector Rx mode (use_simple_rx = 1), vq->vq_descx[] is not
kept up to date. To properly detach the mbufs in this case, browse
sw_ring[] instead, as it's done in virtqueue_rxvq_flush().

Since we need virtio_get_queue_type(), also move this function in
virtqueue.h as a static inline.

Fixes: fc3d66212fed ("virtio: add vector Rx")
Cc: stable@dpdk.org

Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
---

Tiwei,

While it passes my test plan, please carefully check that what I did in
virtqueue_detatch_unused() is correct. I tried to reproduce what is done
in virtqueue_rxvq_flush(), but I may be mistaking due to the different
ring layout assumption and mbuf management between standard and vector.

Thanks
Olivier


 drivers/net/virtio/virtio_ethdev.c | 11 -----------
 drivers/net/virtio/virtqueue.c     | 17 +++++++++++++++--
 drivers/net/virtio/virtqueue.h     | 11 +++++++++++
 3 files changed, 26 insertions(+), 13 deletions(-)
  

Comments

Tiwei Bie Jan. 22, 2018, 2:56 a.m. UTC | #1
On Fri, Jan 19, 2018 at 04:55:53PM +0100, Olivier Matz wrote:
> When using vector Rx mode (use_simple_rx = 1), vq->vq_descx[] is not
> kept up to date. To properly detach the mbufs in this case, browse
> sw_ring[] instead, as it's done in virtqueue_rxvq_flush().
> 
> Since we need virtio_get_queue_type(), also move this function in
> virtqueue.h as a static inline.
> 
> Fixes: fc3d66212fed ("virtio: add vector Rx")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
> ---
> 
> Tiwei,
> 
> While it passes my test plan, please carefully check that what I did in
> virtqueue_detatch_unused() is correct. I tried to reproduce what is done
> in virtqueue_rxvq_flush(), but I may be mistaking due to the different
> ring layout assumption and mbuf management between standard and vector.
> 
> Thanks
> Olivier
> 
> 
>  drivers/net/virtio/virtio_ethdev.c | 11 -----------
>  drivers/net/virtio/virtqueue.c     | 17 +++++++++++++++--
>  drivers/net/virtio/virtqueue.h     | 11 +++++++++++
>  3 files changed, 26 insertions(+), 13 deletions(-)
> 
[...]
>  struct rte_mbuf *
>  virtqueue_detatch_unused(struct virtqueue *vq)
>  {
> +	struct virtio_hw *hw = vq->hw;
>  	struct rte_mbuf *cookie;
>  	int idx;
> +	int type = virtio_get_queue_type(hw, vq->vq_queue_index);
> +
> +	if (vq == NULL)
> +		return NULL;
>  
> -	if (vq != NULL)
> -		for (idx = 0; idx < vq->vq_nentries; idx++) {
> +	for (idx = 0; idx < vq->vq_nentries; idx++) {
> +		if (hw->use_simple_rx && type == VTNET_RQ) {
> +			cookie = vq->sw_ring[idx];
> +			if (cookie != NULL) {
> +				vq->sw_ring[idx] = NULL;

Thanks for working on this!

The vq->sw_ring[idx] isn't zeroed during Rx. So besides the
check of (cookie != NULL), some other check is also needed
to avoid freeing the mbufs already delivered to application.

The mbufs in below interval belong to application:

start: sw_ring[vq->vq_avail_idx & (vq->vq_nentries - 1)] (included)
end: sw_ring[(vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1)] (excluded)

PS. (vq->vq_avail_idx & (vq->vq_nentries - 1)) can be greater than
    (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1).

Thanks,
Tiwei
  
Olivier Matz Jan. 22, 2018, 10:38 a.m. UTC | #2
On Mon, Jan 22, 2018 at 10:56:41AM +0800, Tiwei Bie wrote:
> On Fri, Jan 19, 2018 at 04:55:53PM +0100, Olivier Matz wrote:
> > When using vector Rx mode (use_simple_rx = 1), vq->vq_descx[] is not
> > kept up to date. To properly detach the mbufs in this case, browse
> > sw_ring[] instead, as it's done in virtqueue_rxvq_flush().
> > 
> > Since we need virtio_get_queue_type(), also move this function in
> > virtqueue.h as a static inline.
> > 
> > Fixes: fc3d66212fed ("virtio: add vector Rx")
> > Cc: stable@dpdk.org
> > 
> > Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
> > ---
> > 
> > Tiwei,
> > 
> > While it passes my test plan, please carefully check that what I did in
> > virtqueue_detatch_unused() is correct. I tried to reproduce what is done
> > in virtqueue_rxvq_flush(), but I may be mistaking due to the different
> > ring layout assumption and mbuf management between standard and vector.
> > 
> > Thanks
> > Olivier
> > 
> > 
> >  drivers/net/virtio/virtio_ethdev.c | 11 -----------
> >  drivers/net/virtio/virtqueue.c     | 17 +++++++++++++++--
> >  drivers/net/virtio/virtqueue.h     | 11 +++++++++++
> >  3 files changed, 26 insertions(+), 13 deletions(-)
> > 
> [...]
> >  struct rte_mbuf *
> >  virtqueue_detatch_unused(struct virtqueue *vq)
> >  {
> > +	struct virtio_hw *hw = vq->hw;
> >  	struct rte_mbuf *cookie;
> >  	int idx;
> > +	int type = virtio_get_queue_type(hw, vq->vq_queue_index);
> > +
> > +	if (vq == NULL)
> > +		return NULL;
> >  
> > -	if (vq != NULL)
> > -		for (idx = 0; idx < vq->vq_nentries; idx++) {
> > +	for (idx = 0; idx < vq->vq_nentries; idx++) {
> > +		if (hw->use_simple_rx && type == VTNET_RQ) {
> > +			cookie = vq->sw_ring[idx];
> > +			if (cookie != NULL) {
> > +				vq->sw_ring[idx] = NULL;
> 
> Thanks for working on this!
> 
> The vq->sw_ring[idx] isn't zeroed during Rx. So besides the
> check of (cookie != NULL), some other check is also needed
> to avoid freeing the mbufs already delivered to application.
> 
> The mbufs in below interval belong to application:
> 
> start: sw_ring[vq->vq_avail_idx & (vq->vq_nentries - 1)] (included)
> end: sw_ring[(vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1)] (excluded)
> 
> PS. (vq->vq_avail_idx & (vq->vq_nentries - 1)) can be greater than
>     (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1).

Thank you for the review. What about something like this?

struct rte_mbuf *
virtqueue_detach_unused(struct virtqueue *vq)
{
	struct rte_mbuf *cookie;
	struct virtio_hw *hw;
	uint16_t start, end;
	int type, idx;

	if (vq == NULL)
		return NULL;

	hw = vq->hw;
	type = virtio_get_queue_type(hw, vq->vq_queue_index);
	start = vq->vq_avail_idx & (vq->vq_nentries - 1);
	end = (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1);
	end = (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1);

	for (idx = 0; idx < vq->vq_nentries; idx++) {
		if (hw->use_simple_rx && type == VTNET_RQ) {
			if (start <= end && idx >= start && idx < end)
				continue;
			if (start > end && (idx >= start || idx < end))
				continue;
			cookie = vq->sw_ring[idx];
			if (cookie == NULL)
				continue;
			vq->sw_ring[idx] = NULL;
			return cookie;
		} else {
			cookie = vq->vq_descx[idx].cookie;
			if (cookie != NULL) {
				vq->vq_descx[idx].cookie = NULL;
				return cookie;
			}
		}
	}

	return NULL;
}


Regards,
Olivier
  
Tiwei Bie Jan. 23, 2018, 2:05 a.m. UTC | #3
On Mon, Jan 22, 2018 at 11:38:58AM +0100, Olivier Matz wrote:
> On Mon, Jan 22, 2018 at 10:56:41AM +0800, Tiwei Bie wrote:
> > On Fri, Jan 19, 2018 at 04:55:53PM +0100, Olivier Matz wrote:
> > > When using vector Rx mode (use_simple_rx = 1), vq->vq_descx[] is not
> > > kept up to date. To properly detach the mbufs in this case, browse
> > > sw_ring[] instead, as it's done in virtqueue_rxvq_flush().
> > > 
> > > Since we need virtio_get_queue_type(), also move this function in
> > > virtqueue.h as a static inline.
> > > 
> > > Fixes: fc3d66212fed ("virtio: add vector Rx")
> > > Cc: stable@dpdk.org
> > > 
> > > Signed-off-by: Olivier Matz <olivier.matz@6wind.com>
> > > ---
> > > 
> > > Tiwei,
> > > 
> > > While it passes my test plan, please carefully check that what I did in
> > > virtqueue_detatch_unused() is correct. I tried to reproduce what is done
> > > in virtqueue_rxvq_flush(), but I may be mistaking due to the different
> > > ring layout assumption and mbuf management between standard and vector.
> > > 
> > > Thanks
> > > Olivier
> > > 
> > > 
> > >  drivers/net/virtio/virtio_ethdev.c | 11 -----------
> > >  drivers/net/virtio/virtqueue.c     | 17 +++++++++++++++--
> > >  drivers/net/virtio/virtqueue.h     | 11 +++++++++++
> > >  3 files changed, 26 insertions(+), 13 deletions(-)
> > > 
> > [...]
> > >  struct rte_mbuf *
> > >  virtqueue_detatch_unused(struct virtqueue *vq)
> > >  {
> > > +	struct virtio_hw *hw = vq->hw;
> > >  	struct rte_mbuf *cookie;
> > >  	int idx;
> > > +	int type = virtio_get_queue_type(hw, vq->vq_queue_index);
> > > +
> > > +	if (vq == NULL)
> > > +		return NULL;
> > >  
> > > -	if (vq != NULL)
> > > -		for (idx = 0; idx < vq->vq_nentries; idx++) {
> > > +	for (idx = 0; idx < vq->vq_nentries; idx++) {
> > > +		if (hw->use_simple_rx && type == VTNET_RQ) {
> > > +			cookie = vq->sw_ring[idx];
> > > +			if (cookie != NULL) {
> > > +				vq->sw_ring[idx] = NULL;
> > 
> > Thanks for working on this!
> > 
> > The vq->sw_ring[idx] isn't zeroed during Rx. So besides the
> > check of (cookie != NULL), some other check is also needed
> > to avoid freeing the mbufs already delivered to application.
> > 
> > The mbufs in below interval belong to application:
> > 
> > start: sw_ring[vq->vq_avail_idx & (vq->vq_nentries - 1)] (included)
> > end: sw_ring[(vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1)] (excluded)
> > 
> > PS. (vq->vq_avail_idx & (vq->vq_nentries - 1)) can be greater than
> >     (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1).
> 
> Thank you for the review. What about something like this?
> 
> struct rte_mbuf *
> virtqueue_detach_unused(struct virtqueue *vq)
> {
> 	struct rte_mbuf *cookie;
> 	struct virtio_hw *hw;
> 	uint16_t start, end;
> 	int type, idx;
> 
> 	if (vq == NULL)
> 		return NULL;
> 
> 	hw = vq->hw;
> 	type = virtio_get_queue_type(hw, vq->vq_queue_index);
> 	start = vq->vq_avail_idx & (vq->vq_nentries - 1);
> 	end = (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1);
> 	end = (vq->vq_avail_idx + vq->vq_free_cnt) & (vq->vq_nentries - 1);
> 
> 	for (idx = 0; idx < vq->vq_nentries; idx++) {
> 		if (hw->use_simple_rx && type == VTNET_RQ) {
> 			if (start <= end && idx >= start && idx < end)
> 				continue;
> 			if (start > end && (idx >= start || idx < end))
> 				continue;
> 			cookie = vq->sw_ring[idx];
> 			if (cookie == NULL)
> 				continue;
> 			vq->sw_ring[idx] = NULL;
> 			return cookie;
> 		} else {
> 			cookie = vq->vq_descx[idx].cookie;
> 			if (cookie != NULL) {
> 				vq->vq_descx[idx].cookie = NULL;
> 				return cookie;
> 			}
> 		}
> 	}
> 
> 	return NULL;
> }

Apart from the repeated line, it looks good to me. Thanks!

Thanks,
Tiwei
  

Patch

diff --git a/drivers/net/virtio/virtio_ethdev.c b/drivers/net/virtio/virtio_ethdev.c
index ec012c2ac..2c7f902e7 100644
--- a/drivers/net/virtio/virtio_ethdev.c
+++ b/drivers/net/virtio/virtio_ethdev.c
@@ -268,17 +268,6 @@  virtio_dev_queue_release(void *queue __rte_unused)
 	/* do nothing */
 }
 
-static int
-virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
-{
-	if (vtpci_queue_idx == hw->max_queue_pairs * 2)
-		return VTNET_CQ;
-	else if (vtpci_queue_idx % 2 == 0)
-		return VTNET_RQ;
-	else
-		return VTNET_TQ;
-}
-
 static uint16_t
 virtio_get_nr_vq(struct virtio_hw *hw)
 {
diff --git a/drivers/net/virtio/virtqueue.c b/drivers/net/virtio/virtqueue.c
index 1ada4fe08..43cb51d1b 100644
--- a/drivers/net/virtio/virtqueue.c
+++ b/drivers/net/virtio/virtqueue.c
@@ -18,17 +18,30 @@ 
 struct rte_mbuf *
 virtqueue_detatch_unused(struct virtqueue *vq)
 {
+	struct virtio_hw *hw = vq->hw;
 	struct rte_mbuf *cookie;
 	int idx;
+	int type = virtio_get_queue_type(hw, vq->vq_queue_index);
+
+	if (vq == NULL)
+		return NULL;
 
-	if (vq != NULL)
-		for (idx = 0; idx < vq->vq_nentries; idx++) {
+	for (idx = 0; idx < vq->vq_nentries; idx++) {
+		if (hw->use_simple_rx && type == VTNET_RQ) {
+			cookie = vq->sw_ring[idx];
+			if (cookie != NULL) {
+				vq->sw_ring[idx] = NULL;
+				return cookie;
+			}
+		} else {
 			cookie = vq->vq_descx[idx].cookie;
 			if (cookie != NULL) {
 				vq->vq_descx[idx].cookie = NULL;
 				return cookie;
 			}
 		}
+	}
+
 	return NULL;
 }
 
diff --git a/drivers/net/virtio/virtqueue.h b/drivers/net/virtio/virtqueue.h
index fedbaa39c..cae1b318a 100644
--- a/drivers/net/virtio/virtqueue.h
+++ b/drivers/net/virtio/virtqueue.h
@@ -281,6 +281,17 @@  virtqueue_full(const struct virtqueue *vq)
 	return vq->vq_free_cnt == 0;
 }
 
+static inline int
+virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx)
+{
+	if (vtpci_queue_idx == hw->max_queue_pairs * 2)
+		return VTNET_CQ;
+	else if (vtpci_queue_idx % 2 == 0)
+		return VTNET_RQ;
+	else
+		return VTNET_TQ;
+}
+
 #define VIRTQUEUE_NUSED(vq) ((uint16_t)((vq)->vq_ring.used->idx - (vq)->vq_used_cons_idx))
 
 void vq_ring_free_chain(struct virtqueue *vq, uint16_t desc_idx);