Skip to contentSkip to conversation

support to clear in-flight packets for async

RFC Superseded Yuan Wang 2 participants 8 messages Jan 17, 2022 – Mar 29, 2022

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Yuan Wang

author
#0 lore raw

cover support to clear in-flight packets for async

These patches supports to clear in-flight packets for async dequeue and introduces thread-safe version of this function.

note: The patches depends on the following patches
(http://patches.dpdk.org/project/dpdk/patch/[email protected]/)
(http://patches.dpdk.org/project/dpdk/patch/[email protected]/)

Yuan Wang (2):
vhost: support clear in-flight packets for async dequeue example/vhost: support to clear in-flight packets for async dequeue

 doc/guides/prog_guide/vhost_lib.rst    |  7 ++-
 doc/guides/rel_notes/release_22_03.rst |  5 ++
 examples/vhost/main.c                  |  8 +++
 lib/vhost/rte_vhost_async.h            | 26 ++++++++++
 lib/vhost/version.map                  |  1 +
 lib/vhost/virtio_net.c                 | 71 +++++++++++++++++++++++++-
 6 files changed, 115 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

RFC 1/2 vhost: support clear in-flight packets for async dequeue

rte_vhost_clear_queue_thread_unsafe() supports to clear in-flight packets for async enqueue only. But after supporting async dequeue, this API should support async dequeue too.

This patch also adds the thread-safe version of this API, the difference between the two API is that thread safety uses lock.

These APIs maybe used to clean up packets in the async channel to prevent packet loss when the device state changes or when the device is destroyed.

---
 doc/guides/prog_guide/vhost_lib.rst    |  7 ++-
 doc/guides/rel_notes/release_22_03.rst |  5 ++
 lib/vhost/rte_vhost_async.h            | 26 ++++++++++
 lib/vhost/version.map                  |  1 +
 lib/vhost/virtio_net.c                 | 71 +++++++++++++++++++++++++-
 5 files changed, 107 insertions(+), 3 deletions(-)
doc/guides/prog_guide/vhost_lib.rst +6 -1
index bdce7cbf02..7cea2490e5 100644
@@ -269,7 +269,12 @@ The following is an overview of some key Vhost API functions:
 
 * ``rte_vhost_clear_queue_thread_unsafe(vid, queue_id, **pkts, count, dma_id, dma_vchan)``
 
-  Clear inflight packets which are submitted to DMA engine in vhost async data
+  Clear in-flight packets which are submitted to async channel in vhost
+  async data path without performing any locking. Completed packets are
+  returned to applications through ``pkts``.
+
+* ``rte_vhost_clear_queue(vid, queue_id, **pkts, count, dma_id, dma_vchan)``
+  Clear in-flight packets which are submitted to async channel in vhost async data
   path. Completed packets are returned to applications through ``pkts``.
 
 Vhost-user Implementations
doc/guides/rel_notes/release_22_03.rst +5 -0
index 6d99d1eaa9..e774919fc9 100644
@@ -55,6 +55,11 @@ New Features
      Also, make sure to start the actual text at the margin.
      =======================================================
 
+* **Added thread-safe version of inflight packet clear API in vhost library.**
+
+  Added an API which can clear the inflight packets submitted to
+  the async channel in a thread-safe manner in the vhost async data path.
+
 
 Removed Items
 -------------
lib/vhost/rte_vhost_async.h +26 -0
index b1249382cd..ed9389560b 100644
@@ -186,6 +186,32 @@ __rte_experimental
 uint16_t rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
 		struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
 		uint16_t vchan);
+
+/**
+ * This function checks async completion status and clear packets for
+ * a specific vhost device queue. Packets which are inflight will be
+ * returned in an array.
+ *
+ * @param vid
+ *  ID of vhost device to clear data
+ * @param queue_id
+ *  Queue id to clear data
+ * @param pkts
+ *  Blank array to get return packet pointer
+ * @param count
+ *  Size of the packet array
+ * @param dma_id
+ *  the identifier of the DMA device
+ * @param vchan
+ *  the identifier of virtual DMA channel
+ * @return
+ *  Number of packets returned
+ */
+__rte_experimental
+uint16_t rte_vhost_clear_queue(int vid, uint16_t queue_id,
+		struct rte_mbuf **pkts,	uint16_t count,	int16_t dma_id,
+		uint16_t vchan);
+
 /**
  * The DMA vChannels used in asynchronous data path must be configured
  * first. So this function needs to be called before enabling DMA
lib/vhost/version.map +1 -0
index 816a6dc942..16f7d57380 100644
@@ -88,6 +88,7 @@ EXPERIMENTAL {
 	# added in 22.03
 	rte_vhost_async_dma_configure;
 	rte_vhost_async_try_dequeue_burst;
+	rte_vhost_clear_queue;
 };
 
 INTERNAL {
lib/vhost/virtio_net.c +69 -2

89 lines not on this page · raw message

Signature · 1 line
2.25.1
author
#2 lore raw

RFC 2/2 example/vhost: support to clear in-flight packets for async dequeue

In reply to Yuan Wang

This patch allows vhost_clear_queue_thread_unsafe() to clear in-flight dequeue packets.

---
 examples/vhost/main.c | 8 ++++++++
 1 file changed, 8 insertions(+)
examples/vhost/main.c +8 -0
index 04a85262bc..050f983fd6 100644
@@ -1495,6 +1495,7 @@ vhost_clear_queue_thread_unsafe(struct vhost_dev *vdev, uint16_t queue_id)
 	uint16_t n_pkt = 0;
 	uint16_t dma_id = dma_bind[vid2socketid[vdev->vid]].dmas[queue_id].dev_id;
 	struct rte_mbuf *m_enq_cpl[vdev->pkts_enq_inflight];
+	struct rte_mbuf *m_deq_cpl[vdev->pkts_deq_inflight];
 
 	if (queue_id % 2 == 0) {
 		while (vdev->pkts_enq_inflight) {
@@ -1503,6 +1504,13 @@ vhost_clear_queue_thread_unsafe(struct vhost_dev *vdev, uint16_t queue_id)
 			free_pkts(m_enq_cpl, n_pkt);
 			__atomic_sub_fetch(&vdev->pkts_enq_inflight, n_pkt, __ATOMIC_SEQ_CST);
 		}
+	} else {
+		while (vdev->pkts_deq_inflight) {
+			n_pkt = rte_vhost_clear_queue_thread_unsafe(vdev->vid,
+				queue_id, m_deq_cpl, vdev->pkts_deq_inflight, dma_id, 0);
+			free_pkts(m_deq_cpl, n_pkt);
+			__atomic_sub_fetch(&vdev->pkts_deq_inflight, n_pkt, __ATOMIC_SEQ_CST);
+		}
 	}
 }
 
Signature · 1 line
2.25.1
author
#3 lore raw

v2 cover support to clear in-flight packets for async

In reply to Yuan Wang

These patches support to clear in-flight packets for async dequeue and introduce thread-safe version of this function.

note: The patches depend on the following patches
(http://patches.dpdk.org/project/dpdk/patch/[email protected]/)
(http://patches.dpdk.org/project/dpdk/patch/[email protected]/)
---
v2:
- rebase to 22.03-rc3.

Yuan Wang (2):
vhost: support clear in-flight packets for async dequeue example/vhost: support to clear in-flight packets for async dequeue

 doc/guides/prog_guide/vhost_lib.rst    |  7 ++-
 doc/guides/rel_notes/release_22_03.rst |  4 ++
 examples/vhost/main.c                  |  3 -
 lib/vhost/rte_vhost_async.h            | 25 ++++++++
 lib/vhost/version.map                  |  1 +
 lib/vhost/virtio_net.c                 | 79 +++++++++++++++++++++++++-
 6 files changed, 113 insertions(+), 6 deletions(-)
Signature · 1 line
2.25.1
author
#4 lore raw

RFC v2 1/2 vhost: support clear in-flight packets for async dequeue

rte_vhost_clear_queue_thread_unsafe() supports to clear in-flight packets for async enqueue only. But after supporting async dequeue, this API should support async dequeue too.

This patch also adds the thread-safe version of this API, the difference between the two API is that thread safety uses lock.

These APIs maybe used to clean up packets in the async channel to prevent packet loss when the device state changes or when the device is destroyed.

---
 doc/guides/prog_guide/vhost_lib.rst    |  7 ++-
 doc/guides/rel_notes/release_22_03.rst |  4 ++
 lib/vhost/rte_vhost_async.h            | 25 ++++++++
 lib/vhost/version.map                  |  1 +
 lib/vhost/virtio_net.c                 | 79 +++++++++++++++++++++++++-
 5 files changed, 113 insertions(+), 3 deletions(-)
doc/guides/prog_guide/vhost_lib.rst +6 -1
index 886f8f5e72..bbb7333d03 100644
@@ -273,7 +273,12 @@ The following is an overview of some key Vhost API functions:
 
 * ``rte_vhost_clear_queue_thread_unsafe(vid, queue_id, **pkts, count, dma_id, vchan_id)``
 
-  Clear inflight packets which are submitted to DMA engine in vhost async data
+  Clear in-flight packets which are submitted to async channel in vhost
+  async data path without performing any locking. Completed packets are
+  returned to applications through ``pkts``.
+
+* ``rte_vhost_clear_queue(vid, queue_id, **pkts, count, dma_id, vchan_id)``
+  Clear in-flight packets which are submitted to async channel in vhost async data
   path. Completed packets are returned to applications through ``pkts``.
 
 Vhost-user Implementations
doc/guides/rel_notes/release_22_03.rst +4 -0
index 60e5b4f9aa..ab754056b9 100644
@@ -232,6 +232,10 @@ New Features
   * Crypto producer mode helps to measure performance of OP_NEW and OP_FORWARD
     modes of event crypto adapter.
 
+* **Added thread-safe version of inflight packet clear API in vhost library.**
+
+  * Added an API which can clear the inflight packets submitted to
+    the async channel in a thread-safe manner in the vhost async data path.
 
 Removed Items
 -------------
lib/vhost/rte_vhost_async.h +25 -0
index b6ab0b06a2..eb1ddb2ec1 100644
@@ -166,6 +166,31 @@ uint16_t rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,
 		struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
 		uint16_t vchan_id);
 
+/**
+ * This function checks async completion status and clear packets for
+ * a specific vhost device queue. Packets which are inflight will be
+ * returned in an array.
+ *
+ * @param vid
+ *  ID of vhost device to clear data
+ * @param queue_id
+ *  Queue id to clear data
+ * @param pkts
+ *  Blank array to get return packet pointer
+ * @param count
+ *  Size of the packet array
+ * @param dma_id
+ *  The identifier of the DMA device
+ * @param vchan_id
+ *  The identifier of virtual DMA channel
+ * @return
+ *  Number of packets returned
+ */
+__rte_experimental
+uint16_t rte_vhost_clear_queue(int vid, uint16_t queue_id,
+		struct rte_mbuf **pkts, uint16_t count, int16_t dma_id,
+		uint16_t vchan_id);
+
 /**
  * The DMA vChannels used in asynchronous data path must be configured
  * first. So this function needs to be called before enabling DMA
lib/vhost/version.map +1 -0
index 968d6d4290..11c5fa635d 100644
@@ -88,6 +88,7 @@ EXPERIMENTAL {
 	# added in 22.03
 	rte_vhost_async_dma_configure;
 	rte_vhost_async_try_dequeue_burst;
+	rte_vhost_clear_queue;
 };
 
 INTERNAL {
lib/vhost/virtio_net.c +77 -2

101 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw

RE: [RFC v2 1/2] vhost: support clear in-flight packets for async dequeue

Hi Yuan,

Thanks for the patch, comment inline.

Quoted 9 lines
lib/vhost/virtio_net.c +9 -0
+uint16_t
+rte_vhost_clear_queue(int vid, uint16_t queue_id, struct rte_mbuf **pkts,
+		uint16_t count, int16_t dma_id, uint16_t vchan_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;
+	uint16_t n_pkts_cpl = 0;
+
+	if (!dev)
+		return 0;

lib/vhost/virtio_net.c rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id, · line 2147

Quoted 10 lines
lib/vhost/virtio_net.c +10 -0
+
+	VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
+	if (unlikely(queue_id >= dev->nr_vring)) {
+		VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue idx %d.\n",
+			dev->ifname, __func__, queue_id);
+		return 0;
+	}
+
+	vq = dev->virtqueue[queue_id];
+

I think the following checks must be protected by spinlock.
Similar to : https://patches.dpdk.org/project/dpdk/patch/[email protected]/

Quoted 20 lines
(quoted diff) +2 -0
+	if (unlikely(!vq->async)) {
+		VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for queue

id %d.\n",

lib/vhost/virtio_net.c +6 -0
+			dev->ifname, __func__, queue_id);
+		return 0;
+	}
+
+	if (unlikely(!dma_copy_track[dma_id].vchans ||
+
	!dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr)) {
(quoted diff) +1 -0
+		VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n", dev-
Quoted 1 line

ifname, __func__,

lib/vhost/virtio_net.c +7 -0
+				dma_id, vchan_id);
+		return 0;
+	}
+
+	if (!rte_spinlock_trylock(&vq->access_lock)) {
+		VHOST_LOG_DATA(ERR,
+			"(%d) %s: failed to clear async queue id %d, virtqueue

busy.\n",

lib/vhost/virtio_net.c rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id, · line 2167

Quoted 4 lines
lib/vhost/virtio_net.c +4 -0
+			dev->vid, __func__, queue_id);
+		return 0;
+	}
+

<snipped>

Thanks and regards,
Sunil

#6 lore raw

RE: [RFC v2 1/2] vhost: support clear in-flight packets for async dequeue

Hi Sunil,

Quoted 29 lines

-----Original Message-----
From: Pai G, Sunil <[email protected]>
Sent: Monday, March 28, 2022 2:08 PM
To: Wang, YuanX <[email protected]>; [email protected]; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Ding, Xuan <[email protected]>; Yang, YvonneX <[email protected]>
Subject: RE: [RFC v2 1/2] vhost: support clear in-flight packets for async dequeue

Hi Yuan,

Thanks for the patch, comment inline.

Review

Quoted 2 lines
(quoted diff) +2 -0
+uint16_t
+rte_vhost_clear_queue(int vid, uint16_t queue_id, struct rte_mbuf

**pkts,

lib/vhost/virtio_net.c rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,

Quoted 11 lines
lib/vhost/virtio_net.c +11 -0
+		uint16_t count, int16_t dma_id, uint16_t vchan_id) {
+	struct virtio_net *dev = get_device(vid);
+	struct vhost_virtqueue *vq;
+	uint16_t n_pkts_cpl = 0;
+
+	if (!dev)
+		return 0;
+
+	VHOST_LOG_DATA(DEBUG, "(%s) %s\n", dev->ifname, __func__);
+	if (unlikely(queue_id >= dev->nr_vring)) {
+		VHOST_LOG_DATA(ERR, "(%s) %s: invalid virtqueue

idx %d.\n",

Quoted 11 lines

lib/vhost/virtio_net.c rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id, · line 2147

Quoted 6 lines
lib/vhost/virtio_net.c +6 -0
+			dev->ifname, __func__, queue_id);
+		return 0;
+	}
+
+	vq = dev->virtqueue[queue_id];
+

I think the following checks must be protected by spinlock. Similar to :
https://patches.dpdk.org/project/dpdk/patch/20220328020754.1155063-1-
[email protected]/

Thanks for the comment.
Will fix it in next version.

Thanks,
Yuan

Quoted 34 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	if (unlikely(!vq->async)) {
+		VHOST_LOG_DATA(ERR, "(%s) %s: async not registered for

queue

Quoted 8 lines

id %d.\n",

lib/vhost/virtio_net.c +6 -0
+			dev->ifname, __func__, queue_id);
+		return 0;
+	}
+
+	if (unlikely(!dma_copy_track[dma_id].vchans ||
+
	!dma_copy_track[dma_id].vchans[vchan_id].pkts_cmpl_flag_addr))

{

Review

Quoted 1 line
(quoted diff) +1 -0
+		VHOST_LOG_DATA(ERR, "(%s) %s: invalid channel %d:%u.\n",

dev-

lib/vhost/virtio_net.c rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id,

Quoted 8 lines
Quoted 1 line

ifname, __func__,

lib/vhost/virtio_net.c +7 -0
+				dma_id, vchan_id);
+		return 0;
+	}
+
+	if (!rte_spinlock_trylock(&vq->access_lock)) {
+		VHOST_LOG_DATA(ERR,
+			"(%d) %s: failed to clear async queue id %d,

virtqueue

lib/vhost/virtio_net.c rte_vhost_clear_queue_thread_unsafe(int vid, uint16_t queue_id, · line 2167

Quoted 5 lines

busy.\n",

lib/vhost/virtio_net.c +4 -0
+			dev->vid, __func__, queue_id);
+		return 0;
+	}
+

<snipped>

Thanks and regards,
Sunil

author
#7 lore raw

RFC v2 2/2 example/vhost: support to clear in-flight packets for async dequeue

In reply to Yuan Wang

This patch allows vring_state_changed() to clear
in-flight dequeue packets.

---
 examples/vhost/main.c | 3 ---
 1 file changed, 3 deletions(-)
examples/vhost/main.c +0 -3
index d26e40ab73..04e7821322 100644
@@ -1767,9 +1767,6 @@ vring_state_changed(int vid, uint16_t queue_id, int enable)
 	if (!vdev)
 		return -1;
 
-	if (queue_id != VIRTIO_RXQ)
-		return 0;
-
 	if (dma_bind[vid2socketid[vid]].dmas[queue_id].async_enabled) {
 		if (!enable)
 			vhost_clear_queue_thread_unsafe(vdev, queue_id);
Signature · 1 line
2.25.1