Skip to contentSkip to conversation

vhost: add unsafe API to get DMA inflight packets

PATCH Applied [email protected] 2 participants 6 messages Apr 8, 2022 – May 6, 2022

lore
Share & tools
lore
#0 lore raw

cover vhost: add unsafe API to get DMA inflight packets

From: Xuan Ding <[email protected]>

This patchset introduces an unsafe API to get the number of inflight packets in DMA engine. It should be only used within the vhost ops which already holds the lock. Like vring state changes or device is destroyed. Compared with rte_vhost_async_get_inflight(), this is a lock free version.

RFC v1->v1:
* refine the doc and commit log

Xuan Ding (2):
vhost: add unsafe API to check inflight packets examples/vhost: use API to check inflight packets

 doc/guides/prog_guide/vhost_lib.rst    |  6 ++++++
 doc/guides/rel_notes/release_22_07.rst |  4 ++++
 examples/vhost/main.c                  | 28 ++++++++++++++------------
 examples/vhost/main.h                  |  1 -
 lib/vhost/rte_vhost_async.h            | 17 ++++++++++++++++
 lib/vhost/version.map                  |  4 ++++
 lib/vhost/vhost.c                      | 26 ++++++++++++++++++++++++
 7 files changed, 72 insertions(+), 14 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 vhost: add unsafe API to check inflight packets

From: Xuan Ding <[email protected]>

In async data path, when vring state changes or device is destroyed, it is necessary to know the number of inflight packets in DMA engine. This patch provides a thread unsafe API to return the number of inflight packets for a vhost queue without using any lock.

---
 doc/guides/prog_guide/vhost_lib.rst    |  6 ++++++
 doc/guides/rel_notes/release_22_07.rst |  4 ++++
 lib/vhost/rte_vhost_async.h            | 17 +++++++++++++++++
 lib/vhost/version.map                  |  4 ++++
 lib/vhost/vhost.c                      | 26 ++++++++++++++++++++++++++
 5 files changed, 57 insertions(+)
doc/guides/prog_guide/vhost_lib.rst +6 -0
index 886f8f5e72..f287b76ebf 100644
@@ -271,6 +271,12 @@ The following is an overview of some key Vhost API functions:
   This function returns the amount of in-flight packets for the vhost
   queue using async acceleration.
 
+ * ``rte_vhost_async_get_inflight_thread_unsafe(vid, queue_id)``
+
+  Get the number of inflight packets for a vhost queue without performing
+  any locking. It should only be used within the vhost ops, which already
+  holds the lock.
+
 * ``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
doc/guides/rel_notes/release_22_07.rst +4 -0
index 42a5f2d990..a0c5d9459b 100644
@@ -55,6 +55,10 @@ New Features
      Also, make sure to start the actual text at the margin.
      =======================================================
 
+* **Added vhost API to get the number of inflight packets.**
+
+  Added an API which can get the number of inflight packets in
+  vhost async data path without using lock.
 
 Removed Items
 -------------
lib/vhost/rte_vhost_async.h +17 -0
index f1293c6a9d..70234debf9 100644
@@ -139,6 +139,23 @@ uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
 __rte_experimental
 int rte_vhost_async_get_inflight(int vid, uint16_t queue_id);
 
+/**
+ * This function is lock-free version to return the amount of in-flight
+ * packets for the vhost queue which uses async channel acceleration.
+ *
+ * @note This function does not perform any locking, it should only be
+ * used within the vhost ops, which already holds the lock.
+ *
+ * @param vid
+ * id of vhost device to enqueue data
+ * @param queue_id
+ * queue id to enqueue data
+ * @return
+ * the amount of in-flight packets on success; -1 on failure
+ */
+__rte_experimental
+int rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id);
+
 /**
  * This function checks async completion status and clear packets for
  * a specific vhost device queue. Packets which are inflight will be
lib/vhost/version.map +4 -0
index 0a66c5840c..5841315386 100644
@@ -87,6 +87,10 @@ EXPERIMENTAL {
 
 	# added in 22.03
 	rte_vhost_async_dma_configure;
+
+	# added in 22.07
+	rte_vhost_async_get_inflight_thread_unsafe;
+
 };
 
 INTERNAL {
lib/vhost/vhost.c +26 -0
index 2f96a28dac..df0bb9d043 100644
@@ -1907,6 +1907,32 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
 	return ret;
 }
 
+int
+rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
+{
+	struct vhost_virtqueue *vq;
+	struct virtio_net *dev = get_device(vid);
+	int ret = -1;
+
+	if (dev == NULL)
+		return ret;
+
+	if (queue_id >= VHOST_MAX_VRING)
+		return ret;
+
+	vq = dev->virtqueue[queue_id];
+
+	if (vq == NULL)
+		return ret;
+
+	if (!vq->async)
+		return ret;
+
+	ret = vq->async->pkts_inflight_n;
+
+	return ret;
+}
+
 int
 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
 		struct rte_vhost_power_monitor_cond *pmc)
Signature · 1 line
2.17.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] vhost: add unsafe API to check inflight packets

Quoted 106 lines · On 4/8/22 12:22, [email protected] wrote:

From: Xuan Ding <[email protected]>

In async data path, when vring state changes or device is destroyed, it is necessary to know the number of inflight packets in DMA engine. This patch provides a thread unsafe API to return the number of inflight packets for a vhost queue without using any lock.

---
  doc/guides/prog_guide/vhost_lib.rst    |  6 ++++++
  doc/guides/rel_notes/release_22_07.rst |  4 ++++
  lib/vhost/rte_vhost_async.h            | 17 +++++++++++++++++
  lib/vhost/version.map                  |  4 ++++
  lib/vhost/vhost.c                      | 26 ++++++++++++++++++++++++++
  5 files changed, 57 insertions(+)
doc/guides/prog_guide/vhost_lib.rst +6 -0
index 886f8f5e72..f287b76ebf 100644
@@ -271,6 +271,12 @@ The following is an overview of some key Vhost API functions:
    This function returns the amount of in-flight packets for the vhost
    queue using async acceleration.
 
+ * ``rte_vhost_async_get_inflight_thread_unsafe(vid, queue_id)``
+
+  Get the number of inflight packets for a vhost queue without performing
+  any locking. It should only be used within the vhost ops, which already
+  holds the lock.
+
  * ``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
doc/guides/rel_notes/release_22_07.rst +4 -0
index 42a5f2d990..a0c5d9459b 100644
@@ -55,6 +55,10 @@ New Features
       Also, make sure to start the actual text at the margin.
       =======================================================
 
+* **Added vhost API to get the number of inflight packets.**
+
+  Added an API which can get the number of inflight packets in
+  vhost async data path without using lock.
 
  Removed Items
  -------------
lib/vhost/rte_vhost_async.h +17 -0
index f1293c6a9d..70234debf9 100644
@@ -139,6 +139,23 @@ uint16_t rte_vhost_poll_enqueue_completed(int vid, uint16_t queue_id,
  __rte_experimental
  int rte_vhost_async_get_inflight(int vid, uint16_t queue_id);
 
+/**
+ * This function is lock-free version to return the amount of in-flight
+ * packets for the vhost queue which uses async channel acceleration.
+ *
+ * @note This function does not perform any locking, it should only be
+ * used within the vhost ops, which already holds the lock.
+ *
+ * @param vid
+ * id of vhost device to enqueue data
+ * @param queue_id
+ * queue id to enqueue data
+ * @return
+ * the amount of in-flight packets on success; -1 on failure
+ */
+__rte_experimental
+int rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id);
+
  /**
   * This function checks async completion status and clear packets for
   * a specific vhost device queue. Packets which are inflight will be
lib/vhost/version.map +4 -0
index 0a66c5840c..5841315386 100644
@@ -87,6 +87,10 @@ EXPERIMENTAL {
 
  	# added in 22.03
  	rte_vhost_async_dma_configure;
+
+	# added in 22.07
+	rte_vhost_async_get_inflight_thread_unsafe;
+
  };
 
  INTERNAL {
lib/vhost/vhost.c +7 -0
index 2f96a28dac..df0bb9d043 100644
@@ -1907,6 +1907,32 @@ rte_vhost_async_get_inflight(int vid, uint16_t queue_id)
  	return ret;
  }
 
+int
+rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t queue_id)
+{
+	struct vhost_virtqueue *vq;
+	struct virtio_net *dev = get_device(vid);
+	int ret = -1;
+

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1926

Quoted 10 lines
lib/vhost/vhost.c +10 -0
+	if (dev == NULL)
+		return ret;
+
+	if (queue_id >= VHOST_MAX_VRING)
+		return ret;
+
+	vq = dev->virtqueue[queue_id];
+
+	if (vq == NULL)
+		return ret;

FYI, I'm going to post a patch that adds a call to rte_spinlock_is_locked() to catch possible misuses of the API.

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1938

Quoted 12 lines
lib/vhost/vhost.c +8 -0
+	if (!vq->async)
+		return ret;
+
+	ret = vq->async->pkts_inflight_n;
+
+	return ret;
+}
+
  int
  rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
  		struct rte_vhost_power_monitor_cond *pmc)

Thanks,
Maxime

author
#3 lore raw

RE: [PATCH v1 1/2] vhost: add unsafe API to check inflight packets

Hi Maxime,

Quoted 129 lines

-----Original Message-----
From: Maxime Coquelin <[email protected]>
Sent: Friday, May 6, 2022 3:21 AM
To: Ding, Xuan <[email protected]>; Xia, Chenbo <[email protected]>
Cc: [email protected]; Hu, Jiayu <[email protected]>; Jiang, Cheng1 <[email protected]>; Pai G, Sunil <[email protected]>
Subject: Re: [PATCH v1 1/2] vhost: add unsafe API to check inflight packets

doc/guides/prog_guide/vhost_lib.rst The following is an overview of some key Vhost

Quoted 22 lines · On 4/8/22 12:22, [email protected] wrote:

From: Xuan Ding <[email protected]>

In async data path, when vring state changes or device is destroyed, it is necessary to know the number of inflight packets in DMA engine. This patch provides a thread unsafe API to return the number of inflight packets for a vhost queue without using any lock.

---
  doc/guides/prog_guide/vhost_lib.rst    |  6 ++++++
  doc/guides/rel_notes/release_22_07.rst |  4 ++++
  lib/vhost/rte_vhost_async.h            | 17 +++++++++++++++++
  lib/vhost/version.map                  |  4 ++++
  lib/vhost/vhost.c                      | 26 ++++++++++++++++++++++++++
  5 files changed, 57 insertions(+)
doc/guides/prog_guide/vhost_lib.rst +0 -0
index 886f8f5e72..f287b76ebf 100644
@@ -271,6 +271,12 @@ The following is an overview of some key Vhost

API functions:

lib/vhost/rte_vhost_async.h uint16_t rte_vhost_poll_enqueue_completed(int

Quoted 34 lines
doc/guides/prog_guide/vhost_lib.rst +6 -0
    This function returns the amount of in-flight packets for the vhost
    queue using async acceleration.
 
+ * ``rte_vhost_async_get_inflight_thread_unsafe(vid, queue_id)``
+
+  Get the number of inflight packets for a vhost queue without
+ performing  any locking. It should only be used within the vhost
+ ops, which already  holds the lock.
+
  * ``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 diff --git a/doc/guides/rel_notes/release_22_07.rst b/doc/guides/rel_notes/release_22_07.rst
index 42a5f2d990..a0c5d9459b 100644

doc/guides/rel_notes/release_22_07.rst +4 -0
@@ -55,6 +55,10 @@ New Features
       Also, make sure to start the actual text at the margin.
       =======================================================
 
+* **Added vhost API to get the number of inflight packets.**
+
+  Added an API which can get the number of inflight packets in  vhost
+ async data path without using lock.
 
  Removed Items
  -------------
lib/vhost/rte_vhost_async.h +0 -0
index f1293c6a9d..70234debf9 100644
@@ -139,6 +139,23 @@ uint16_t rte_vhost_poll_enqueue_completed(int

vid, uint16_t queue_id,

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t

Quoted 44 lines
lib/vhost/rte_vhost_async.h +18 -0
  __rte_experimental
  int rte_vhost_async_get_inflight(int vid, uint16_t queue_id);
 
+/**
+ * This function is lock-free version to return the amount of
+in-flight
+ * packets for the vhost queue which uses async channel acceleration.
+ *
+ * @note This function does not perform any locking, it should only
+be
+ * used within the vhost ops, which already holds the lock.
+ *
+ * @param vid
+ * id of vhost device to enqueue data
+ * @param queue_id
+ * queue id to enqueue data
+ * @return
+ * the amount of in-flight packets on success; -1 on failure  */
+__rte_experimental int rte_vhost_async_get_inflight_thread_unsafe(int
+vid, uint16_t queue_id);
+
  /**
   * This function checks async completion status and clear packets for
   * a specific vhost device queue. Packets which are inflight will be
lib/vhost/version.map index renamed from lib/vhost/version.map +0 -0

0a66c5840c..5841315386 100644

lib/vhost/version.map +4 -0
@@ -87,6 +87,10 @@ EXPERIMENTAL {
 
  	# added in 22.03
  	rte_vhost_async_dma_configure;
+
+	# added in 22.07
+	rte_vhost_async_get_inflight_thread_unsafe;
+
  };
 
  INTERNAL {
lib/vhost/vhost.c index renamed from lib/vhost/vhost.c +0 -0

2f96a28dac..df0bb9d043 100644

lib/vhost/vhost.c +0 -0
@@ -1907,6 +1907,32 @@ rte_vhost_async_get_inflight(int vid, uint16_t

queue_id)

Quoted 15 lines
lib/vhost/vhost.c +12 -0
  	return ret;
  }
 
+int
+rte_vhost_async_get_inflight_thread_unsafe(int vid, uint16_t
+queue_id) {
+	struct vhost_virtqueue *vq;
+	struct virtio_net *dev = get_device(vid);
+	int ret = -1;
+
+	if (dev == NULL)
+		return ret;
+
+	if (queue_id >= VHOST_MAX_VRING)
+		return ret;
Quoted 8 lines

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1926

Quoted 5 lines
lib/vhost/vhost.c +5 -0
+
+	vq = dev->virtqueue[queue_id];
+
+	if (vq == NULL)
+		return ret;

FYI, I'm going to post a patch that adds a call to rte_spinlock_is_locked() to catch possible misuses of the API.

Thanks for your reminder, I can help to review this patch if necessary.

Regards,
Xuan

Quoted 18 lines

lib/vhost/vhost.c rte_vhost_async_get_inflight(int vid, uint16_t queue_id) · line 1938

Quoted 13 lines
lib/vhost/vhost.c +8 -0
+	if (!vq->async)
+		return ret;
+
+	ret = vq->async->pkts_inflight_n;
+
+	return ret;
+}
+
  int
  rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
  		struct rte_vhost_power_monitor_cond *pmc)

Thanks,
Maxime

#4 lore raw

PATCH 2/2 examples/vhost: use API to check inflight packets

In reply to [email protected]

From: Xuan Ding <[email protected]>

In async data path, call rte_vhost_async_get_inflight_thread_unsafe() API to directly return the number of inflight packets instead of maintaining a local variable.

---
 examples/vhost/main.c | 28 +++++++++++++++-------------
 examples/vhost/main.h |  1 -
 2 files changed, 15 insertions(+), 14 deletions(-)
examples/vhost/main.c +15 -13
index d94fabb060..c4d46de1c5 100644
@@ -994,10 +994,8 @@ complete_async_pkts(struct vhost_dev *vdev)
 
 	complete_count = rte_vhost_poll_enqueue_completed(vdev->vid,
 					VIRTIO_RXQ, p_cpl, MAX_PKT_BURST, dma_id, 0);
-	if (complete_count) {
+	if (complete_count)
 		free_pkts(p_cpl, complete_count);
-		__atomic_sub_fetch(&vdev->pkts_inflight, complete_count, __ATOMIC_SEQ_CST);
-	}
 
 }
 
@@ -1039,7 +1037,6 @@ drain_vhost(struct vhost_dev *vdev)
 
 		complete_async_pkts(vdev);
 		ret = rte_vhost_submit_enqueue_burst(vdev->vid, VIRTIO_RXQ, m, nr_xmit, dma_id, 0);
-		__atomic_add_fetch(&vdev->pkts_inflight, ret, __ATOMIC_SEQ_CST);
 
 		enqueue_fail = nr_xmit - ret;
 		if (enqueue_fail)
@@ -1368,7 +1365,6 @@ drain_eth_rx(struct vhost_dev *vdev)
 		complete_async_pkts(vdev);
 		enqueue_count = rte_vhost_submit_enqueue_burst(vdev->vid,
 					VIRTIO_RXQ, pkts, rx_count, dma_id, 0);
-		__atomic_add_fetch(&vdev->pkts_inflight, enqueue_count, __ATOMIC_SEQ_CST);
 
 		enqueue_fail = rx_count - enqueue_count;
 		if (enqueue_fail)
@@ -1540,14 +1536,17 @@ destroy_device(int vid)
 
 	if (dma_bind[vid].dmas[VIRTIO_RXQ].async_enabled) {
 		uint16_t n_pkt = 0;
+		int pkts_inflight;
 		int16_t dma_id = dma_bind[vid].dmas[VIRTIO_RXQ].dev_id;
-		struct rte_mbuf *m_cpl[vdev->pkts_inflight];
+		pkts_inflight = rte_vhost_async_get_inflight_thread_unsafe(vid, VIRTIO_RXQ);
+		struct rte_mbuf *m_cpl[pkts_inflight];
 
-		while (vdev->pkts_inflight) {
+		while (pkts_inflight) {
 			n_pkt = rte_vhost_clear_queue_thread_unsafe(vid, VIRTIO_RXQ,
-						m_cpl, vdev->pkts_inflight, dma_id, 0);
+						m_cpl, pkts_inflight, dma_id, 0);
 			free_pkts(m_cpl, n_pkt);
-			__atomic_sub_fetch(&vdev->pkts_inflight, n_pkt, __ATOMIC_SEQ_CST);
+			pkts_inflight = rte_vhost_async_get_inflight_thread_unsafe(vid,
+										VIRTIO_RXQ);
 		}
 
 		rte_vhost_async_channel_unregister(vid, VIRTIO_RXQ);
@@ -1651,14 +1650,17 @@ vring_state_changed(int vid, uint16_t queue_id, int enable)
 	if (dma_bind[vid].dmas[queue_id].async_enabled) {
 		if (!enable) {
 			uint16_t n_pkt = 0;
+			int pkts_inflight;
+			pkts_inflight = rte_vhost_async_get_inflight_thread_unsafe(vid, queue_id);
 			int16_t dma_id = dma_bind[vid].dmas[VIRTIO_RXQ].dev_id;
-			struct rte_mbuf *m_cpl[vdev->pkts_inflight];
+			struct rte_mbuf *m_cpl[pkts_inflight];
 
-			while (vdev->pkts_inflight) {
+			while (pkts_inflight) {
 				n_pkt = rte_vhost_clear_queue_thread_unsafe(vid, queue_id,
-							m_cpl, vdev->pkts_inflight, dma_id, 0);
+							m_cpl, pkts_inflight, dma_id, 0);
 				free_pkts(m_cpl, n_pkt);
-				__atomic_sub_fetch(&vdev->pkts_inflight, n_pkt, __ATOMIC_SEQ_CST);
+				pkts_inflight = rte_vhost_async_get_inflight_thread_unsafe(vid,
+											queue_id);
 			}
 		}
 	}
examples/vhost/main.h +0 -1
index b4a453e77e..e7f395c3c9 100644
@@ -52,7 +52,6 @@ struct vhost_dev {
 	uint64_t features;
 	size_t hdr_len;
 	uint16_t nr_vrings;
-	uint16_t pkts_inflight;
 	struct rte_vhost_memory *mem;
 	struct device_statistics stats;
 	TAILQ_ENTRY(vhost_dev) global_vdev_entry;
Signature · 1 line
2.17.1
#5 lore raw
In reply to [email protected]
Quoted 13 lines · On 4/8/22 12:22, [email protected] wrote:

This patchset introduces an unsafe API to get the number of inflight packets in DMA engine. It should be only used within the vhost ops which already holds the lock. Like vring state changes or device is destroyed. Compared with rte_vhost_async_get_inflight(), this is a lock free version.

RFC v1->v1:
* refine the doc and commit log

Xuan Ding (2):
vhost: add unsafe API to check inflight packets examples/vhost: use API to check inflight packets

Quoted 8 lines
 doc/guides/prog_guide/vhost_lib.rst    |  6 ++++++
 doc/guides/rel_notes/release_22_07.rst |  4 ++++
 examples/vhost/main.c                  | 28 ++++++++++++++------------
 examples/vhost/main.h                  |  1 -
 lib/vhost/rte_vhost_async.h            | 17 ++++++++++++++++
 lib/vhost/version.map                  |  4 ++++
 lib/vhost/vhost.c                      | 26 ++++++++++++++++++++++++
 7 files changed, 72 insertions(+), 14 deletions(-)

Applied to dpdk-next-virtio/main.

Thanks,
Maxime