Skip to contentSkip to conversation

net/virtio: update stats when in order xmit done

PATCH Superseded Marvin Liu 3 participants 18 messages Aug 27, 2019 – Sep 27, 2019 Network drivers

lore
Share & tools
lore

Marvin Liu

author
#0 lore raw

PATCH 1/2 net/virtio: update stats when in order xmit done

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. It has possibility that no free room in avail ring, thus some buffered packets can't be transmitted. So move stats update just after successful avail ring updates.

---
 drivers/net/virtio/virtio_rxtx.c | 86 ++++++++++++++++----------------
 1 file changed, 43 insertions(+), 43 deletions(-)
drivers/net/virtio/virtio_rxtx.c +43 -43
index 27ead19fb..5d4ed524e 100644
@@ -575,6 +575,48 @@ virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
 	}
 }
 
+static inline void
+virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
+{
+	uint32_t s = mbuf->pkt_len;
+	struct rte_ether_addr *ea;
+
+	stats->bytes += s;
+
+	if (s == 64) {
+		stats->size_bins[1]++;
+	} else if (s > 64 && s < 1024) {
+		uint32_t bin;
+
+		/* count zeros, and offset into correct bin */
+		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
+		stats->size_bins[bin]++;
+	} else {
+		if (s < 64)
+			stats->size_bins[0]++;
+		else if (s < 1519)
+			stats->size_bins[6]++;
+		else
+			stats->size_bins[7]++;
+	}
+
+	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
+	if (rte_is_multicast_ether_addr(ea)) {
+		if (rte_is_broadcast_ether_addr(ea))
+			stats->broadcast++;
+		else
+			stats->multicast++;
+	}
+}
+
+static inline void
+virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
+{
+	VIRTIO_DUMP_PACKET(m, m->data_len);
+
+	virtio_update_packet_stats(&rxvq->stats, m);
+}
+
 static inline void
 virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
 			struct rte_mbuf **cookies,
@@ -596,6 +638,7 @@ virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
 		dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
 		dxp->cookie = (void *)cookies[i];
 		dxp->ndescs = 1;
+		virtio_update_packet_stats(&txvq->stats, cookies[i]);
 
 		hdr = (struct virtio_net_hdr *)
 			rte_pktmbuf_prepend(cookies[i], head_size);
@@ -1083,48 +1126,6 @@ virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
 	}
 }
 
-static inline void
-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
-{
-	uint32_t s = mbuf->pkt_len;
-	struct rte_ether_addr *ea;
-
-	stats->bytes += s;
-
-	if (s == 64) {
-		stats->size_bins[1]++;
-	} else if (s > 64 && s < 1024) {
-		uint32_t bin;
-
-		/* count zeros, and offset into correct bin */
-		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
-		stats->size_bins[bin]++;
-	} else {
-		if (s < 64)
-			stats->size_bins[0]++;
-		else if (s < 1519)
-			stats->size_bins[6]++;
-		else
-			stats->size_bins[7]++;
-	}
-
-	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
-	if (rte_is_multicast_ether_addr(ea)) {
-		if (rte_is_broadcast_ether_addr(ea))
-			stats->broadcast++;
-		else
-			stats->multicast++;
-	}
-}
-
-static inline void
-virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
-{
-	VIRTIO_DUMP_PACKET(m, m->data_len);
-
-	virtio_update_packet_stats(&rxvq->stats, m);
-}
-
 /* Optionally fill offload information in structure */
 static inline int
 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
@@ -2198,7 +2199,6 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 			inorder_pkts[nb_inorder_pkts] = txm;
 			nb_inorder_pkts++;
 
-			virtio_update_packet_stats(&txvq->stats, txm);
 			continue;
 		}
 
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH 2/2 net/virtio: on demand cleanup when doing in order xmit

Check whether freed descriptors are enough before enqueue operation. If more space is needed, will try to cleanup used ring on demand. It can give more chances to cleanup used ring, thus help RFC2544 perf.

---
 drivers/net/virtio/virtio_rxtx.c | 73 +++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 19 deletions(-)
drivers/net/virtio/virtio_rxtx.c +54 -19
index 5d4ed524e..550b0aa62 100644
@@ -317,7 +317,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;
@@ -2152,6 +2152,21 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 	return nb_tx;
 }
 
+static __rte_always_inline int
+virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
+{
+	uint16_t nb_used;
+	struct virtio_hw *hw = vq->hw;
+
+	nb_used = VIRTQUEUE_NUSED(vq);
+	virtio_rmb(hw->weak_barriers);
+	need = RTE_MIN(need, (int)nb_used);
+
+	virtio_xmit_cleanup_inorder(vq, need);
+
+	return (need - vq->vq_free_cnt);
+}
+
 uint16_t
 virtio_xmit_pkts_inorder(void *tx_queue,
 			struct rte_mbuf **tx_pkts,
@@ -2161,8 +2176,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	struct virtqueue *vq = txvq->vq;
 	struct virtio_hw *hw = vq->hw;
 	uint16_t hdr_size = hw->vtnet_hdr_size;
-	uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
+	uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
 	struct rte_mbuf *inorder_pkts[nb_pkts];
+	int need, nb_left;
 
 	if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
 		return nb_tx;
@@ -2175,17 +2191,12 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	nb_used = VIRTQUEUE_NUSED(vq);
 
 	virtio_rmb(hw->weak_barriers);
-	if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
-		virtio_xmit_cleanup_inorder(vq, nb_used);
-
-	if (unlikely(!vq->vq_free_cnt))
+	if (likely(nb_used > (vq->vq_nentries - vq->vq_free_thresh)))
 		virtio_xmit_cleanup_inorder(vq, nb_used);
 
-	nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
-
-	for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
+	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
 		struct rte_mbuf *txm = tx_pkts[nb_tx];
-		int slots, need;
+		int slots;
 
 		/* optimize ring usage */
 		if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
@@ -2203,6 +2214,22 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		}
 
 		if (nb_inorder_pkts) {
+			need = nb_inorder_pkts - vq->vq_free_cnt;
+
+
+			if (unlikely(need > 0)) {
+				nb_left = virtio_xmit_try_cleanup_inorder(vq,
+									need);
+
+				if (unlikely(nb_left > 0)) {
+					PMD_TX_LOG(ERR,
+						"No free tx descriptors to "
+						"transmit");
+					nb_inorder_pkts = vq->vq_free_cnt;
+					break;
+				}
+			}
+
 			virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 							nb_inorder_pkts);
 			nb_inorder_pkts = 0;
@@ -2211,15 +2238,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		slots = txm->nb_segs + 1;
 		need = slots - vq->vq_free_cnt;
 		if (unlikely(need > 0)) {
-			nb_used = VIRTQUEUE_NUSED(vq);
-			virtio_rmb(hw->weak_barriers);
-			need = RTE_MIN(need, (int)nb_used);
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, need);
 
-			virtio_xmit_cleanup_inorder(vq, need);
-
-			need = slots - vq->vq_free_cnt;
-
-			if (unlikely(need > 0)) {
+			if (unlikely(nb_left > 0)) {
 				PMD_TX_LOG(ERR,
 					"No free tx descriptors to transmit");
 				break;
@@ -2232,9 +2253,23 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	}
 
 	/* Transmit all inorder packets */
-	if (nb_inorder_pkts)
+	if (nb_inorder_pkts) {
+		need = nb_inorder_pkts - vq->vq_free_cnt;
+
+		if (unlikely(need > 0)) {
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, need);
+
+			if (unlikely(nb_left > 0)) {
+				PMD_TX_LOG(ERR,
+					"No free tx descriptors to transmit");
+				nb_inorder_pkts = vq->vq_free_cnt;
+				nb_tx -= nb_left;
+			}
+		}
+
 		virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 						nb_inorder_pkts);
+	}
 
 	txvq->stats.packets += nb_tx;
 
Signature · 1 line
2.17.1
#2 lore raw

Re: [dpdk-dev] [PATCH 2/2] net/virtio: on demand cleanup when doing in order xmit

Quoted 29 lines · Marvin Liu

Check whether freed descriptors are enough before enqueue operation. If more space is needed, will try to cleanup used ring on demand. It can give more chances to cleanup used ring, thus help RFC2544 perf.

---
 drivers/net/virtio/virtio_rxtx.c | 73 +++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 19 deletions(-)
drivers/net/virtio/virtio_rxtx.c +4 -1
index 5d4ed524e..550b0aa62 100644
@@ -317,7 +317,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;
@@ -2152,6 +2152,21 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 	return nb_tx;
 }
 
+static __rte_always_inline int
+virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
+{

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts) · line 2167

Quoted 10 lines
drivers/net/virtio/virtio_rxtx.c +10 -0
+	uint16_t nb_used;
+	struct virtio_hw *hw = vq->hw;
+
+	nb_used = VIRTQUEUE_NUSED(vq);
+	virtio_rmb(hw->weak_barriers);
+	need = RTE_MIN(need, (int)nb_used);
+
+	virtio_xmit_cleanup_inorder(vq, need);
+
+	return (need - vq->vq_free_cnt);

It's possible that the need has been changed by

	need = RTE_MIN(need, (int)nb_used);

So it can't reflect the actual needs.

Besides, you are passing (nb_inorder_pkts - vq->vq_free_cnt) as the need, here you can't subtract vq->vq_free_cnt to see whether the needs have been met.

Quoted 38 lines
drivers/net/virtio/virtio_rxtx.c +7 -9
+}
+
 uint16_t
 virtio_xmit_pkts_inorder(void *tx_queue,
 			struct rte_mbuf **tx_pkts,
@@ -2161,8 +2176,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	struct virtqueue *vq = txvq->vq;
 	struct virtio_hw *hw = vq->hw;
 	uint16_t hdr_size = hw->vtnet_hdr_size;
-	uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
+	uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
 	struct rte_mbuf *inorder_pkts[nb_pkts];
+	int need, nb_left;
 
 	if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
 		return nb_tx;
@@ -2175,17 +2191,12 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	nb_used = VIRTQUEUE_NUSED(vq);
 
 	virtio_rmb(hw->weak_barriers);
-	if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
-		virtio_xmit_cleanup_inorder(vq, nb_used);
-
-	if (unlikely(!vq->vq_free_cnt))
+	if (likely(nb_used > (vq->vq_nentries - vq->vq_free_thresh)))
 		virtio_xmit_cleanup_inorder(vq, nb_used);
 
-	nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
-
-	for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
+	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
 		struct rte_mbuf *txm = tx_pkts[nb_tx];
-		int slots, need;
+		int slots;
 
 		/* optimize ring usage */
 		if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2219

Quoted 6 lines
drivers/net/virtio/virtio_rxtx.c +3 -0
@@ -2203,6 +2214,22 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		}
 
 		if (nb_inorder_pkts) {
+			need = nb_inorder_pkts - vq->vq_free_cnt;
+
+

There is no need to add blank lines here.

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2228

Quoted 9 lines
drivers/net/virtio/virtio_rxtx.c +9 -0
+			if (unlikely(need > 0)) {
+				nb_left = virtio_xmit_try_cleanup_inorder(vq,
+									need);
+
+				if (unlikely(nb_left > 0)) {
+					PMD_TX_LOG(ERR,
+						"No free tx descriptors to "
+						"transmit");
+					nb_inorder_pkts = vq->vq_free_cnt;

You need to handle nb_tx as well.

Quoted 53 lines
drivers/net/virtio/virtio_rxtx.c +21 -9
+					break;
+				}
+			}
+
 			virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 							nb_inorder_pkts);
 			nb_inorder_pkts = 0;
@@ -2211,15 +2238,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		slots = txm->nb_segs + 1;
 		need = slots - vq->vq_free_cnt;
 		if (unlikely(need > 0)) {
-			nb_used = VIRTQUEUE_NUSED(vq);
-			virtio_rmb(hw->weak_barriers);
-			need = RTE_MIN(need, (int)nb_used);
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, need);
 
-			virtio_xmit_cleanup_inorder(vq, need);
-
-			need = slots - vq->vq_free_cnt;
-
-			if (unlikely(need > 0)) {
+			if (unlikely(nb_left > 0)) {
 				PMD_TX_LOG(ERR,
 					"No free tx descriptors to transmit");
 				break;
@@ -2232,9 +2253,23 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	}
 
 	/* Transmit all inorder packets */
-	if (nb_inorder_pkts)
+	if (nb_inorder_pkts) {
+		need = nb_inorder_pkts - vq->vq_free_cnt;
+
+		if (unlikely(need > 0)) {
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, need);
+
+			if (unlikely(nb_left > 0)) {
+				PMD_TX_LOG(ERR,
+					"No free tx descriptors to transmit");
+				nb_inorder_pkts = vq->vq_free_cnt;
+				nb_tx -= nb_left;
+			}
+		}
+
 		virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 						nb_inorder_pkts);
+	}
 
 	txvq->stats.packets += nb_tx;
 
Signature · 1 line
2.17.1
author
#3 lore raw

Re: [dpdk-dev] [PATCH 2/2] net/virtio: on demand cleanup when doing in order xmit

Quoted 57 lines

-----Original Message-----
From: Bie, Tiwei
Sent: Tuesday, September 10, 2019 2:17 PM
To: Liu, Yong <[email protected]>
Cc: [email protected]; [email protected]; Wang, Zhihong <[email protected]>
Subject: Re: [PATCH 2/2] net/virtio: on demand cleanup when doing in order xmit

Review

Quoted 10 lines · Marvin Liu

Check whether freed descriptors are enough before enqueue operation. If more space is needed, will try to cleanup used ring on demand. It can give more chances to cleanup used ring, thus help RFC2544 perf.

---
 drivers/net/virtio/virtio_rxtx.c | 73 +++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/drivers/net/virtio/virtio_rxtx.c

drivers/net/virtio/virtio_rxtx.c virtio_xmit_cleanup(struct virtqueue *vq, uint16_t

Quoted 4 lines

index 5d4ed524e..550b0aa62 100644

drivers/net/virtio/virtio_rxtx.c +0 -0
@@ -317,7 +317,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t

num)

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts(void *tx_queue, struct rte_mbuf

Quoted 9 lines
drivers/net/virtio/virtio_rxtx.c +1 -1
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;
@@ -2152,6 +2152,21 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf

**tx_pkts, uint16_t nb_pkts)

Quoted 6 lines
drivers/net/virtio/virtio_rxtx.c +3 -0
 	return nb_tx;
 }
 
+static __rte_always_inline int
+virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
+{

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts) · line 2167

Quoted 10 lines
drivers/net/virtio/virtio_rxtx.c +10 -0
+	uint16_t nb_used;
+	struct virtio_hw *hw = vq->hw;
+
+	nb_used = VIRTQUEUE_NUSED(vq);
+	virtio_rmb(hw->weak_barriers);
+	need = RTE_MIN(need, (int)nb_used);
+
+	virtio_xmit_cleanup_inorder(vq, need);
+
+	return (need - vq->vq_free_cnt);

It's possible that the need has been changed by

Quoted 8 lines
	need = RTE_MIN(need, (int)nb_used);

So it can't reflect the actual needs.

Besides, you are passing (nb_inorder_pkts - vq->vq_free_cnt) as the need, here you can't subtract vq->vq_free_cnt to see whether the needs have been met.

Tiwei,
Thanks for reminder, this calculation can't reflect the number of left packets. I will fix it in v2.

Regards,
Marvin

Quoted 112 lines
Quoted 38 lines
drivers/net/virtio/virtio_rxtx.c +7 -9
+}
+
 uint16_t
 virtio_xmit_pkts_inorder(void *tx_queue,
 			struct rte_mbuf **tx_pkts,
@@ -2161,8 +2176,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	struct virtqueue *vq = txvq->vq;
 	struct virtio_hw *hw = vq->hw;
 	uint16_t hdr_size = hw->vtnet_hdr_size;
-	uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
+	uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
 	struct rte_mbuf *inorder_pkts[nb_pkts];
+	int need, nb_left;
 
 	if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
 		return nb_tx;
@@ -2175,17 +2191,12 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	nb_used = VIRTQUEUE_NUSED(vq);
 
 	virtio_rmb(hw->weak_barriers);
-	if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
-		virtio_xmit_cleanup_inorder(vq, nb_used);
-
-	if (unlikely(!vq->vq_free_cnt))
+	if (likely(nb_used > (vq->vq_nentries - vq->vq_free_thresh)))
 		virtio_xmit_cleanup_inorder(vq, nb_used);
 
-	nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
-
-	for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
+	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
 		struct rte_mbuf *txm = tx_pkts[nb_tx];
-		int slots, need;
+		int slots;
 
 		/* optimize ring usage */
 		if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2219

Quoted 6 lines
drivers/net/virtio/virtio_rxtx.c +3 -0
@@ -2203,6 +2214,22 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		}
 
 		if (nb_inorder_pkts) {
+			need = nb_inorder_pkts - vq->vq_free_cnt;
+
+

There is no need to add blank lines here.

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2228

Quoted 9 lines
drivers/net/virtio/virtio_rxtx.c +9 -0
+			if (unlikely(need > 0)) {
+				nb_left = virtio_xmit_try_cleanup_inorder(vq,
+									need);
+
+				if (unlikely(nb_left > 0)) {
+					PMD_TX_LOG(ERR,
+						"No free tx descriptors to "
+						"transmit");
+					nb_inorder_pkts = vq->vq_free_cnt;

You need to handle nb_tx as well.

Quoted 53 lines
drivers/net/virtio/virtio_rxtx.c +21 -10
+					break;
+				}
+			}
+
 			virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 							nb_inorder_pkts);
 			nb_inorder_pkts = 0;
@@ -2211,15 +2238,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		slots = txm->nb_segs + 1;
 		need = slots - vq->vq_free_cnt;
 		if (unlikely(need > 0)) {
-			nb_used = VIRTQUEUE_NUSED(vq);
-			virtio_rmb(hw->weak_barriers);
-			need = RTE_MIN(need, (int)nb_used);
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, need);
 
-			virtio_xmit_cleanup_inorder(vq, need);
-
-			need = slots - vq->vq_free_cnt;
-
-			if (unlikely(need > 0)) {
+			if (unlikely(nb_left > 0)) {
 				PMD_TX_LOG(ERR,
 					"No free tx descriptors to transmit");
 				break;
@@ -2232,9 +2253,23 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	}
 
 	/* Transmit all inorder packets */
-	if (nb_inorder_pkts)
+	if (nb_inorder_pkts) {
+		need = nb_inorder_pkts - vq->vq_free_cnt;
+
+		if (unlikely(need > 0)) {
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, need);
+
+			if (unlikely(nb_left > 0)) {
+				PMD_TX_LOG(ERR,
+					"No free tx descriptors to transmit");
+				nb_inorder_pkts = vq->vq_free_cnt;
+				nb_tx -= nb_left;
+			}
+		}
+
 		virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 						nb_inorder_pkts);
+	}
 
 	txvq->stats.packets += nb_tx;
 
--

2.17.1

#4 lore raw
In reply to Marvin Liu
Quoted 49 lines · Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. It has possibility that no free room in avail ring, thus some buffered packets can't be transmitted. So move stats update just after successful avail ring updates.

---
 drivers/net/virtio/virtio_rxtx.c | 86 ++++++++++++++++----------------
 1 file changed, 43 insertions(+), 43 deletions(-)
drivers/net/virtio/virtio_rxtx.c +31 -0
index 27ead19fb..5d4ed524e 100644
@@ -575,6 +575,48 @@ virtqueue_xmit_offload(struct virtio_net_hdr *hdr,
 	}
 }
 
+static inline void
+virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
+{
+	uint32_t s = mbuf->pkt_len;
+	struct rte_ether_addr *ea;
+
+	stats->bytes += s;
+
+	if (s == 64) {
+		stats->size_bins[1]++;
+	} else if (s > 64 && s < 1024) {
+		uint32_t bin;
+
+		/* count zeros, and offset into correct bin */
+		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
+		stats->size_bins[bin]++;
+	} else {
+		if (s < 64)
+			stats->size_bins[0]++;
+		else if (s < 1519)
+			stats->size_bins[6]++;
+		else
+			stats->size_bins[7]++;
+	}
+
+	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
+	if (rte_is_multicast_ether_addr(ea)) {
+		if (rte_is_broadcast_ether_addr(ea))
+			stats->broadcast++;
+		else
+			stats->multicast++;

drivers/net/virtio/virtio_rxtx.c virtqueue_xmit_offload(struct virtio_net_hdr *hdr, · line 618

Quoted 10 lines
drivers/net/virtio/virtio_rxtx.c +10 -0
+	}
+}
+
+static inline void
+virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
+{
+	VIRTIO_DUMP_PACKET(m, m->data_len);
+
+	virtio_update_packet_stats(&rxvq->stats, m);
+}

If we move above helpers, it's better to just move them to the top of this file.

Thanks,
Tiwei

author
#5 lore raw

PATCH v2 1/2 net/virtio: update stats when in order xmit done

In reply to Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. Buffered packets can be dropped due to insufficient space. Moving stats update action just after successful avail ring updates can guarantee correctness.

---
 drivers/net/virtio/virtio_rxtx.c | 87 ++++++++++++++++----------------
 1 file changed, 44 insertions(+), 43 deletions(-)
drivers/net/virtio/virtio_rxtx.c +44 -43
index 27ead19fb..d3ca36831 100644
@@ -106,6 +106,48 @@ vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)
 	dxp->next = VQ_RING_DESC_CHAIN_END;
 }
 
+static inline void
+virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
+{
+	uint32_t s = mbuf->pkt_len;
+	struct rte_ether_addr *ea;
+
+	stats->bytes += s;
+
+	if (s == 64) {
+		stats->size_bins[1]++;
+	} else if (s > 64 && s < 1024) {
+		uint32_t bin;
+
+		/* count zeros, and offset into correct bin */
+		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
+		stats->size_bins[bin]++;
+	} else {
+		if (s < 64)
+			stats->size_bins[0]++;
+		else if (s < 1519)
+			stats->size_bins[6]++;
+		else
+			stats->size_bins[7]++;
+	}
+
+	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
+	if (rte_is_multicast_ether_addr(ea)) {
+		if (rte_is_broadcast_ether_addr(ea))
+			stats->broadcast++;
+		else
+			stats->multicast++;
+	}
+}
+
+static inline void
+virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
+{
+	VIRTIO_DUMP_PACKET(m, m->data_len);
+
+	virtio_update_packet_stats(&rxvq->stats, m);
+}
+
 static uint16_t
 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
 				  struct rte_mbuf **rx_pkts,
@@ -317,7 +359,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;
@@ -596,6 +638,7 @@ virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
 		dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
 		dxp->cookie = (void *)cookies[i];
 		dxp->ndescs = 1;
+		virtio_update_packet_stats(&txvq->stats, cookies[i]);
 
 		hdr = (struct virtio_net_hdr *)
 			rte_pktmbuf_prepend(cookies[i], head_size);
@@ -1083,48 +1126,6 @@ virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
 	}
 }
 
-static inline void
-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
-{
-	uint32_t s = mbuf->pkt_len;
-	struct rte_ether_addr *ea;
-
-	stats->bytes += s;
-
-	if (s == 64) {
-		stats->size_bins[1]++;
-	} else if (s > 64 && s < 1024) {
-		uint32_t bin;
-
-		/* count zeros, and offset into correct bin */
-		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
-		stats->size_bins[bin]++;
-	} else {
-		if (s < 64)
-			stats->size_bins[0]++;
-		else if (s < 1519)
-			stats->size_bins[6]++;
-		else
-			stats->size_bins[7]++;
-	}
-
-	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
-	if (rte_is_multicast_ether_addr(ea)) {
-		if (rte_is_broadcast_ether_addr(ea))
-			stats->broadcast++;
-		else
-			stats->multicast++;
-	}
-}
-
-static inline void
-virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
-{
-	VIRTIO_DUMP_PACKET(m, m->data_len);
-
-	virtio_update_packet_stats(&rxvq->stats, m);
-}
-
 /* Optionally fill offload information in structure */
 static inline int
 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
Signature · 1 line
2.17.1
author
#6 lore raw

PATCH v2 2/2 net/virtio: on demand cleanup when doing in order xmit

Check whether space are enough before burst enqueue operation. If more space is needed, will try to cleanup used descriptors for space on demand. It can give more chances to free used descriptors, thus will help RFC2544 performance.

---
 drivers/net/virtio/virtio_rxtx.c | 73 +++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 19 deletions(-)
drivers/net/virtio/virtio_rxtx.c +54 -19
index d3ca36831..842b600c3 100644
@@ -2152,6 +2152,22 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 	return nb_tx;
 }
 
+static __rte_always_inline int
+virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
+{
+	uint16_t nb_used, nb_clean, nb_descs;
+	struct virtio_hw *hw = vq->hw;
+
+	nb_descs = vq->vq_free_cnt + need;
+	nb_used = VIRTQUEUE_NUSED(vq);
+	virtio_rmb(hw->weak_barriers);
+	nb_clean = RTE_MIN(need, (int)nb_used);
+
+	virtio_xmit_cleanup_inorder(vq, nb_clean);
+
+	return (nb_descs - vq->vq_free_cnt);
+}
+
 uint16_t
 virtio_xmit_pkts_inorder(void *tx_queue,
 			struct rte_mbuf **tx_pkts,
@@ -2161,8 +2177,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	struct virtqueue *vq = txvq->vq;
 	struct virtio_hw *hw = vq->hw;
 	uint16_t hdr_size = hw->vtnet_hdr_size;
-	uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
+	uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
 	struct rte_mbuf *inorder_pkts[nb_pkts];
+	int need, nb_left;
 
 	if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
 		return nb_tx;
@@ -2175,17 +2192,12 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	nb_used = VIRTQUEUE_NUSED(vq);
 
 	virtio_rmb(hw->weak_barriers);
-	if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
-		virtio_xmit_cleanup_inorder(vq, nb_used);
-
-	if (unlikely(!vq->vq_free_cnt))
+	if (likely(nb_used > (vq->vq_nentries - vq->vq_free_thresh)))
 		virtio_xmit_cleanup_inorder(vq, nb_used);
 
-	nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
-
-	for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
+	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
 		struct rte_mbuf *txm = tx_pkts[nb_tx];
-		int slots, need;
+		int slots;
 
 		/* optimize ring usage */
 		if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
@@ -2199,11 +2211,25 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 			inorder_pkts[nb_inorder_pkts] = txm;
 			nb_inorder_pkts++;
 
-			virtio_update_packet_stats(&txvq->stats, txm);
 			continue;
 		}
 
 		if (nb_inorder_pkts) {
+			need = nb_inorder_pkts - vq->vq_free_cnt;
+
+			if (unlikely(need > 0)) {
+				nb_left = virtio_xmit_try_cleanup_inorder(vq,
+									need);
+
+				if (unlikely(nb_left > 0)) {
+					PMD_TX_LOG(ERR,
+						"No free tx descriptors to "
+						"transmit");
+					nb_inorder_pkts = vq->vq_free_cnt;
+					break;
+				}
+			}
+
 			virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 							nb_inorder_pkts);
 			nb_inorder_pkts = 0;
@@ -2212,15 +2238,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		slots = txm->nb_segs + 1;
 		need = slots - vq->vq_free_cnt;
 		if (unlikely(need > 0)) {
-			nb_used = VIRTQUEUE_NUSED(vq);
-			virtio_rmb(hw->weak_barriers);
-			need = RTE_MIN(need, (int)nb_used);
-
-			virtio_xmit_cleanup_inorder(vq, need);
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, slots);
 
-			need = slots - vq->vq_free_cnt;
-
-			if (unlikely(need > 0)) {
+			if (unlikely(nb_left > 0)) {
 				PMD_TX_LOG(ERR,
 					"No free tx descriptors to transmit");
 				break;
@@ -2233,9 +2253,24 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	}
 
 	/* Transmit all inorder packets */
-	if (nb_inorder_pkts)
+	if (nb_inorder_pkts) {
+		need = nb_inorder_pkts - vq->vq_free_cnt;
+
+		if (unlikely(need > 0)) {
+			nb_left = virtio_xmit_try_cleanup_inorder(vq,
+								  need);
+
+			if (unlikely(nb_left > 0)) {
+				PMD_TX_LOG(ERR,
+					"No free tx descriptors to transmit");
+				nb_inorder_pkts = vq->vq_free_cnt;
+				nb_tx -= nb_left;
+			}
+		}
+
 		virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 						nb_inorder_pkts);
+	}
 
 	txvq->stats.packets += nb_tx;
 
Signature · 1 line
2.17.1
#7 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] net/virtio: on demand cleanup when doing in order xmit

Quoted 73 lines · Marvin Liu

Check whether space are enough before burst enqueue operation. If more space is needed, will try to cleanup used descriptors for space on demand. It can give more chances to free used descriptors, thus will help RFC2544 performance.

---
 drivers/net/virtio/virtio_rxtx.c | 73 +++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 19 deletions(-)
drivers/net/virtio/virtio_rxtx.c +21 -9
index d3ca36831..842b600c3 100644
@@ -2152,6 +2152,22 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 	return nb_tx;
 }
 
+static __rte_always_inline int
+virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
+{
+	uint16_t nb_used, nb_clean, nb_descs;
+	struct virtio_hw *hw = vq->hw;
+
+	nb_descs = vq->vq_free_cnt + need;
+	nb_used = VIRTQUEUE_NUSED(vq);
+	virtio_rmb(hw->weak_barriers);
+	nb_clean = RTE_MIN(need, (int)nb_used);
+
+	virtio_xmit_cleanup_inorder(vq, nb_clean);
+
+	return (nb_descs - vq->vq_free_cnt);
+}
+
 uint16_t
 virtio_xmit_pkts_inorder(void *tx_queue,
 			struct rte_mbuf **tx_pkts,
@@ -2161,8 +2177,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	struct virtqueue *vq = txvq->vq;
 	struct virtio_hw *hw = vq->hw;
 	uint16_t hdr_size = hw->vtnet_hdr_size;
-	uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
+	uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
 	struct rte_mbuf *inorder_pkts[nb_pkts];
+	int need, nb_left;
 
 	if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
 		return nb_tx;
@@ -2175,17 +2192,12 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	nb_used = VIRTQUEUE_NUSED(vq);
 
 	virtio_rmb(hw->weak_barriers);
-	if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
-		virtio_xmit_cleanup_inorder(vq, nb_used);
-
-	if (unlikely(!vq->vq_free_cnt))
+	if (likely(nb_used > (vq->vq_nentries - vq->vq_free_thresh)))
 		virtio_xmit_cleanup_inorder(vq, nb_used);
 
-	nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
-
-	for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
+	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
 		struct rte_mbuf *txm = tx_pkts[nb_tx];
-		int slots, need;
+		int slots;
 
 		/* optimize ring usage */
 		if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
@@ -2199,11 +2211,25 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 			inorder_pkts[nb_inorder_pkts] = txm;
 			nb_inorder_pkts++;
 

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2222

Quoted 10 lines
drivers/net/virtio/virtio_rxtx.c +5 -1
-			virtio_update_packet_stats(&txvq->stats, txm);
 			continue;
 		}
 
 		if (nb_inorder_pkts) {
+			need = nb_inorder_pkts - vq->vq_free_cnt;
+
+			if (unlikely(need > 0)) {
+				nb_left = virtio_xmit_try_cleanup_inorder(vq,
+									need);

There is no need to introduce nb_left. Looks better to just reuse need.

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2228

Quoted 6 lines
drivers/net/virtio/virtio_rxtx.c +6 -0
+
+				if (unlikely(nb_left > 0)) {
+					PMD_TX_LOG(ERR,
+						"No free tx descriptors to "
+						"transmit");
+					nb_inorder_pkts = vq->vq_free_cnt;

You need to handle nb_tx as well, otherwise mbufs will leak. Or maybe just leave nb_inorder_pkts unchanged, and let the code outside the loop handle it.

Quoted 54 lines
drivers/net/virtio/virtio_rxtx.c +22 -9
+					break;
+				}
+			}
+
 			virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 							nb_inorder_pkts);
 			nb_inorder_pkts = 0;
@@ -2212,15 +2238,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		slots = txm->nb_segs + 1;
 		need = slots - vq->vq_free_cnt;
 		if (unlikely(need > 0)) {
-			nb_used = VIRTQUEUE_NUSED(vq);
-			virtio_rmb(hw->weak_barriers);
-			need = RTE_MIN(need, (int)nb_used);
-
-			virtio_xmit_cleanup_inorder(vq, need);
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, slots);
 
-			need = slots - vq->vq_free_cnt;
-
-			if (unlikely(need > 0)) {
+			if (unlikely(nb_left > 0)) {
 				PMD_TX_LOG(ERR,
 					"No free tx descriptors to transmit");
 				break;
@@ -2233,9 +2253,24 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	}
 
 	/* Transmit all inorder packets */
-	if (nb_inorder_pkts)
+	if (nb_inorder_pkts) {
+		need = nb_inorder_pkts - vq->vq_free_cnt;
+
+		if (unlikely(need > 0)) {
+			nb_left = virtio_xmit_try_cleanup_inorder(vq,
+								  need);
+
+			if (unlikely(nb_left > 0)) {
+				PMD_TX_LOG(ERR,
+					"No free tx descriptors to transmit");
+				nb_inorder_pkts = vq->vq_free_cnt;
+				nb_tx -= nb_left;
+			}
+		}
+
 		virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 						nb_inorder_pkts);
+	}
 
 	txvq->stats.packets += nb_tx;
 
Signature · 1 line
2.17.1
author
#8 lore raw

Re: [dpdk-dev] [PATCH v2 2/2] net/virtio: on demand cleanup when doing in order xmit

Thanks for comments, will update in next patch.

Quoted 164 lines

-----Original Message-----
From: Bie, Tiwei
Sent: Wednesday, September 18, 2019 10:44 AM
To: Liu, Yong <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v2 2/2] net/virtio: on demand cleanup when doing in order xmit

Review

Quoted 11 lines · Marvin Liu

Check whether space are enough before burst enqueue operation. If more space is needed, will try to cleanup used descriptors for space on demand. It can give more chances to free used descriptors, thus will help RFC2544 performance.

---
 drivers/net/virtio/virtio_rxtx.c | 73 +++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/drivers/net/virtio/virtio_rxtx.c

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts(void *tx_queue, struct rte_mbuf

Quoted 4 lines

index d3ca36831..842b600c3 100644

drivers/net/virtio/virtio_rxtx.c +0 -0
@@ -2152,6 +2152,22 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf

**tx_pkts, uint16_t nb_pkts)

Quoted 58 lines
drivers/net/virtio/virtio_rxtx.c +21 -9
 	return nb_tx;
 }
 
+static __rte_always_inline int
+virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
+{
+	uint16_t nb_used, nb_clean, nb_descs;
+	struct virtio_hw *hw = vq->hw;
+
+	nb_descs = vq->vq_free_cnt + need;
+	nb_used = VIRTQUEUE_NUSED(vq);
+	virtio_rmb(hw->weak_barriers);
+	nb_clean = RTE_MIN(need, (int)nb_used);
+
+	virtio_xmit_cleanup_inorder(vq, nb_clean);
+
+	return (nb_descs - vq->vq_free_cnt);
+}
+
 uint16_t
 virtio_xmit_pkts_inorder(void *tx_queue,
 			struct rte_mbuf **tx_pkts,
@@ -2161,8 +2177,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	struct virtqueue *vq = txvq->vq;
 	struct virtio_hw *hw = vq->hw;
 	uint16_t hdr_size = hw->vtnet_hdr_size;
-	uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
+	uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
 	struct rte_mbuf *inorder_pkts[nb_pkts];
+	int need, nb_left;
 
 	if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
 		return nb_tx;
@@ -2175,17 +2192,12 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	nb_used = VIRTQUEUE_NUSED(vq);
 
 	virtio_rmb(hw->weak_barriers);
-	if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
-		virtio_xmit_cleanup_inorder(vq, nb_used);
-
-	if (unlikely(!vq->vq_free_cnt))
+	if (likely(nb_used > (vq->vq_nentries - vq->vq_free_thresh)))
 		virtio_xmit_cleanup_inorder(vq, nb_used);
 
-	nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
-
-	for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
+	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
 		struct rte_mbuf *txm = tx_pkts[nb_tx];
-		int slots, need;
+		int slots;
 
 		/* optimize ring usage */
 		if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
@@ -2199,11 +2211,25 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 			inorder_pkts[nb_inorder_pkts] = txm;
 			nb_inorder_pkts++;
 

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2222

Quoted 10 lines
drivers/net/virtio/virtio_rxtx.c +5 -1
-			virtio_update_packet_stats(&txvq->stats, txm);
 			continue;
 		}
 
 		if (nb_inorder_pkts) {
+			need = nb_inorder_pkts - vq->vq_free_cnt;
+
+			if (unlikely(need > 0)) {
+				nb_left = virtio_xmit_try_cleanup_inorder(vq,
+									need);

There is no need to introduce nb_left. Looks better to just reuse need.

drivers/net/virtio/virtio_rxtx.c virtio_xmit_pkts_inorder(void *tx_queue, · line 2228

Quoted 6 lines
drivers/net/virtio/virtio_rxtx.c +6 -0
+
+				if (unlikely(nb_left > 0)) {
+					PMD_TX_LOG(ERR,
+						"No free tx descriptors to "
+						"transmit");
+					nb_inorder_pkts = vq->vq_free_cnt;

You need to handle nb_tx as well, otherwise mbufs will leak. Or maybe just leave nb_inorder_pkts unchanged, and let the code outside the loop handle it.

Quoted 54 lines
drivers/net/virtio/virtio_rxtx.c +22 -10
+					break;
+				}
+			}
+
 			virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 							nb_inorder_pkts);
 			nb_inorder_pkts = 0;
@@ -2212,15 +2238,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		slots = txm->nb_segs + 1;
 		need = slots - vq->vq_free_cnt;
 		if (unlikely(need > 0)) {
-			nb_used = VIRTQUEUE_NUSED(vq);
-			virtio_rmb(hw->weak_barriers);
-			need = RTE_MIN(need, (int)nb_used);
-
-			virtio_xmit_cleanup_inorder(vq, need);
+			nb_left = virtio_xmit_try_cleanup_inorder(vq, slots);
 
-			need = slots - vq->vq_free_cnt;
-
-			if (unlikely(need > 0)) {
+			if (unlikely(nb_left > 0)) {
 				PMD_TX_LOG(ERR,
 					"No free tx descriptors to transmit");
 				break;
@@ -2233,9 +2253,24 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	}
 
 	/* Transmit all inorder packets */
-	if (nb_inorder_pkts)
+	if (nb_inorder_pkts) {
+		need = nb_inorder_pkts - vq->vq_free_cnt;
+
+		if (unlikely(need > 0)) {
+			nb_left = virtio_xmit_try_cleanup_inorder(vq,
+								  need);
+
+			if (unlikely(nb_left > 0)) {
+				PMD_TX_LOG(ERR,
+					"No free tx descriptors to transmit");
+				nb_inorder_pkts = vq->vq_free_cnt;
+				nb_tx -= nb_left;
+			}
+		}
+
 		virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 						nb_inorder_pkts);
+	}
 
 	txvq->stats.packets += nb_tx;
 
--

2.17.1

#9 lore raw
In reply to Marvin Liu
Quoted 73 lines · Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. Buffered packets can be dropped due to insufficient space. Moving stats update action just after successful avail ring updates can guarantee correctness.

---
 drivers/net/virtio/virtio_rxtx.c | 87 ++++++++++++++++----------------
 1 file changed, 44 insertions(+), 43 deletions(-)
drivers/net/virtio/virtio_rxtx.c +43 -1
index 27ead19fb..d3ca36831 100644
@@ -106,6 +106,48 @@ vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)
 	dxp->next = VQ_RING_DESC_CHAIN_END;
 }
 
+static inline void
+virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
+{
+	uint32_t s = mbuf->pkt_len;
+	struct rte_ether_addr *ea;
+
+	stats->bytes += s;
+
+	if (s == 64) {
+		stats->size_bins[1]++;
+	} else if (s > 64 && s < 1024) {
+		uint32_t bin;
+
+		/* count zeros, and offset into correct bin */
+		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
+		stats->size_bins[bin]++;
+	} else {
+		if (s < 64)
+			stats->size_bins[0]++;
+		else if (s < 1519)
+			stats->size_bins[6]++;
+		else
+			stats->size_bins[7]++;
+	}
+
+	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
+	if (rte_is_multicast_ether_addr(ea)) {
+		if (rte_is_broadcast_ether_addr(ea))
+			stats->broadcast++;
+		else
+			stats->multicast++;
+	}
+}
+
+static inline void
+virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
+{
+	VIRTIO_DUMP_PACKET(m, m->data_len);
+
+	virtio_update_packet_stats(&rxvq->stats, m);
+}
+
 static uint16_t
 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
 				  struct rte_mbuf **rx_pkts,
@@ -317,7 +359,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;

drivers/net/virtio/virtio_rxtx.c virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq, · line 641

Quoted 4 lines
drivers/net/virtio/virtio_rxtx.c +1 -0
@@ -596,6 +638,7 @@ virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
 		dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
 		dxp->cookie = (void *)cookies[i];
 		dxp->ndescs = 1;
+		virtio_update_packet_stats(&txvq->stats, cookies[i]);

The virtio_update_packet_stats() call in virtio_xmit_pkts_inorder() should be removed.

Quoted 55 lines
drivers/net/virtio/virtio_rxtx.c +0 -42
 		hdr = (struct virtio_net_hdr *)
 			rte_pktmbuf_prepend(cookies[i], head_size);
@@ -1083,48 +1126,6 @@ virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
 	}
 }
 
-static inline void
-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
-{
-	uint32_t s = mbuf->pkt_len;
-	struct rte_ether_addr *ea;
-
-	stats->bytes += s;
-
-	if (s == 64) {
-		stats->size_bins[1]++;
-	} else if (s > 64 && s < 1024) {
-		uint32_t bin;
-
-		/* count zeros, and offset into correct bin */
-		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
-		stats->size_bins[bin]++;
-	} else {
-		if (s < 64)
-			stats->size_bins[0]++;
-		else if (s < 1519)
-			stats->size_bins[6]++;
-		else
-			stats->size_bins[7]++;
-	}
-
-	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
-	if (rte_is_multicast_ether_addr(ea)) {
-		if (rte_is_broadcast_ether_addr(ea))
-			stats->broadcast++;
-		else
-			stats->multicast++;
-	}
-}
-
-static inline void
-virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
-{
-	VIRTIO_DUMP_PACKET(m, m->data_len);
-
-	virtio_update_packet_stats(&rxvq->stats, m);
-}
-
 /* Optionally fill offload information in structure */
 static inline int
 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
Signature · 1 line
2.17.1
author
#10 lore raw
Quoted 89 lines

-----Original Message-----
From: Bie, Tiwei
Sent: Wednesday, September 18, 2019 10:35 AM
To: Liu, Yong <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v2 1/2] net/virtio: update stats when in order xmit done

Review

Quoted 11 lines · Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. Buffered packets can be dropped due to insufficient space. Moving stats update action just after successful avail ring updates can guarantee correctness.

---
 drivers/net/virtio/virtio_rxtx.c | 87 ++++++++++++++++----------------
 1 file changed, 44 insertions(+), 43 deletions(-)
(quoted diff) +0 -0

b/drivers/net/virtio/virtio_rxtx.c

drivers/net/virtio/virtio_rxtx.c vq_ring_free_id_packed(struct virtqueue *vq,

Quoted 4 lines

index 27ead19fb..d3ca36831 100644

drivers/net/virtio/virtio_rxtx.c +0 -0
@@ -106,6 +106,48 @@ vq_ring_free_id_packed(struct virtqueue *vq,

uint16_t id)

drivers/net/virtio/virtio_rxtx.c vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)

Quoted 5 lines
drivers/net/virtio/virtio_rxtx.c +2 -0
 	dxp->next = VQ_RING_DESC_CHAIN_END;
 }
 
+static inline void
+virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf

*mbuf)

drivers/net/virtio/virtio_rxtx.c virtio_xmit_cleanup(struct virtqueue *vq, uint16_t

Quoted 44 lines
drivers/net/virtio/virtio_rxtx.c +40 -0
+{
+	uint32_t s = mbuf->pkt_len;
+	struct rte_ether_addr *ea;
+
+	stats->bytes += s;
+
+	if (s == 64) {
+		stats->size_bins[1]++;
+	} else if (s > 64 && s < 1024) {
+		uint32_t bin;
+
+		/* count zeros, and offset into correct bin */
+		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
+		stats->size_bins[bin]++;
+	} else {
+		if (s < 64)
+			stats->size_bins[0]++;
+		else if (s < 1519)
+			stats->size_bins[6]++;
+		else
+			stats->size_bins[7]++;
+	}
+
+	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
+	if (rte_is_multicast_ether_addr(ea)) {
+		if (rte_is_broadcast_ether_addr(ea))
+			stats->broadcast++;
+		else
+			stats->multicast++;
+	}
+}
+
+static inline void
+virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
+{
+	VIRTIO_DUMP_PACKET(m, m->data_len);
+
+	virtio_update_packet_stats(&rxvq->stats, m);
+}
+
 static uint16_t
 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
 				  struct rte_mbuf **rx_pkts,
@@ -317,7 +359,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t

num)

drivers/net/virtio/virtio_rxtx.c virtqueue_enqueue_xmit_inorder(struct virtnet_tx

Quoted 9 lines
drivers/net/virtio/virtio_rxtx.c +1 -1
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;
@@ -596,6 +638,7 @@ virtqueue_enqueue_xmit_inorder(struct virtnet_tx

*txvq,

Quoted 8 lines

drivers/net/virtio/virtio_rxtx.c virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq, · line 641

Quoted 4 lines
drivers/net/virtio/virtio_rxtx.c +1 -0
 		dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
 		dxp->cookie = (void *)cookies[i];
 		dxp->ndescs = 1;
+		virtio_update_packet_stats(&txvq->stats, cookies[i]);

The virtio_update_packet_stats() call in virtio_xmit_pkts_inorder() should be removed.

Hi Tiwei,
Function remained in virtio_xmit_pkts_inorder is for those packets not handled by burst enqueue function. Statistic of packets which handled in burst in_order enqueue function is updated in inner loop.

Thanks,
Marvin

Quoted 58 lines

drivers/net/virtio/virtio_rxtx.c virtio_discard_rxbuf_inorder(struct virtqueue *vq,

Quoted 5 lines
drivers/net/virtio/virtio_rxtx.c +0 -0
 		hdr = (struct virtio_net_hdr *)
 			rte_pktmbuf_prepend(cookies[i], head_size);
@@ -1083,48 +1126,6 @@ virtio_discard_rxbuf_inorder(struct virtqueue *vq,

struct rte_mbuf *m)

drivers/net/virtio/virtio_rxtx.c virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)

Quoted 5 lines
drivers/net/virtio/virtio_rxtx.c +0 -2
 	}
 }
 
-static inline void
-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf

*mbuf)

Quoted 46 lines
drivers/net/virtio/virtio_rxtx.c +0 -41
-{
-	uint32_t s = mbuf->pkt_len;
-	struct rte_ether_addr *ea;
-
-	stats->bytes += s;
-
-	if (s == 64) {
-		stats->size_bins[1]++;
-	} else if (s > 64 && s < 1024) {
-		uint32_t bin;
-
-		/* count zeros, and offset into correct bin */
-		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
-		stats->size_bins[bin]++;
-	} else {
-		if (s < 64)
-			stats->size_bins[0]++;
-		else if (s < 1519)
-			stats->size_bins[6]++;
-		else
-			stats->size_bins[7]++;
-	}
-
-	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
-	if (rte_is_multicast_ether_addr(ea)) {
-		if (rte_is_broadcast_ether_addr(ea))
-			stats->broadcast++;
-		else
-			stats->multicast++;
-	}
-}
-
-static inline void
-virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
-{
-	VIRTIO_DUMP_PACKET(m, m->data_len);
-
-	virtio_update_packet_stats(&rxvq->stats, m);
-}
-
 /* Optionally fill offload information in structure */
 static inline int
 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
--

2.17.1

#11 lore raw
Quoted 96 lines · Yong
Quoted 87 lines

-----Original Message-----
From: Bie, Tiwei
Sent: Wednesday, September 18, 2019 10:35 AM
To: Liu, Yong <[email protected]>
Cc: [email protected]; [email protected]
Subject: Re: [PATCH v2 1/2] net/virtio: update stats when in order xmit done

Review

Quoted 11 lines · Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. Buffered packets can be dropped due to insufficient space. Moving stats update action just after successful avail ring updates can guarantee correctness.

---
 drivers/net/virtio/virtio_rxtx.c | 87 ++++++++++++++++----------------
 1 file changed, 44 insertions(+), 43 deletions(-)
(quoted diff) +0 -0

b/drivers/net/virtio/virtio_rxtx.c

drivers/net/virtio/virtio_rxtx.c vq_ring_free_id_packed(struct virtqueue *vq,

Quoted 4 lines

index 27ead19fb..d3ca36831 100644

drivers/net/virtio/virtio_rxtx.c +0 -0
@@ -106,6 +106,48 @@ vq_ring_free_id_packed(struct virtqueue *vq,

uint16_t id)

drivers/net/virtio/virtio_rxtx.c vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)

Quoted 5 lines
drivers/net/virtio/virtio_rxtx.c +2 -0
 	dxp->next = VQ_RING_DESC_CHAIN_END;
 }
 
+static inline void
+virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf

*mbuf)

drivers/net/virtio/virtio_rxtx.c virtio_xmit_cleanup(struct virtqueue *vq, uint16_t

Quoted 44 lines
drivers/net/virtio/virtio_rxtx.c +40 -0
+{
+	uint32_t s = mbuf->pkt_len;
+	struct rte_ether_addr *ea;
+
+	stats->bytes += s;
+
+	if (s == 64) {
+		stats->size_bins[1]++;
+	} else if (s > 64 && s < 1024) {
+		uint32_t bin;
+
+		/* count zeros, and offset into correct bin */
+		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
+		stats->size_bins[bin]++;
+	} else {
+		if (s < 64)
+			stats->size_bins[0]++;
+		else if (s < 1519)
+			stats->size_bins[6]++;
+		else
+			stats->size_bins[7]++;
+	}
+
+	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
+	if (rte_is_multicast_ether_addr(ea)) {
+		if (rte_is_broadcast_ether_addr(ea))
+			stats->broadcast++;
+		else
+			stats->multicast++;
+	}
+}
+
+static inline void
+virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
+{
+	VIRTIO_DUMP_PACKET(m, m->data_len);
+
+	virtio_update_packet_stats(&rxvq->stats, m);
+}
+
 static uint16_t
 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
 				  struct rte_mbuf **rx_pkts,
@@ -317,7 +359,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t

num)

drivers/net/virtio/virtio_rxtx.c virtqueue_enqueue_xmit_inorder(struct virtnet_tx

Quoted 9 lines
drivers/net/virtio/virtio_rxtx.c +1 -1
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;
@@ -596,6 +638,7 @@ virtqueue_enqueue_xmit_inorder(struct virtnet_tx

*txvq,

Quoted 8 lines

drivers/net/virtio/virtio_rxtx.c virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq, · line 641

Quoted 4 lines
drivers/net/virtio/virtio_rxtx.c +1 -0
 		dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
 		dxp->cookie = (void *)cookies[i];
 		dxp->ndescs = 1;
+		virtio_update_packet_stats(&txvq->stats, cookies[i]);

The virtio_update_packet_stats() call in virtio_xmit_pkts_inorder() should be removed.

Quoted 3 lines

Hi Tiwei,
Function remained in virtio_xmit_pkts_inorder is for those packets not handled by burst enqueue function. Statistic of packets which handled in burst in_order enqueue function is updated in inner loop.

I mean below virtio_update_packet_stats() call in virtio_xmit_pkts_inorder() should be removed while doing above change:

https://github.com/DPDK/dpdk/blob/master/drivers/net/virtio/virtio_rxtx.c#L2201

I saw above line is removed by PATCH v2 2/2, but it should be done in this patch.

Quoted 62 lines

Thanks,
Marvin

Quoted 58 lines

drivers/net/virtio/virtio_rxtx.c virtio_discard_rxbuf_inorder(struct virtqueue *vq,

Quoted 5 lines
drivers/net/virtio/virtio_rxtx.c +0 -0
 		hdr = (struct virtio_net_hdr *)
 			rte_pktmbuf_prepend(cookies[i], head_size);
@@ -1083,48 +1126,6 @@ virtio_discard_rxbuf_inorder(struct virtqueue *vq,

struct rte_mbuf *m)

drivers/net/virtio/virtio_rxtx.c virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)

Quoted 5 lines
drivers/net/virtio/virtio_rxtx.c +0 -2
 	}
 }
 
-static inline void
-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf

*mbuf)

Quoted 46 lines
drivers/net/virtio/virtio_rxtx.c +0 -41
-{
-	uint32_t s = mbuf->pkt_len;
-	struct rte_ether_addr *ea;
-
-	stats->bytes += s;
-
-	if (s == 64) {
-		stats->size_bins[1]++;
-	} else if (s > 64 && s < 1024) {
-		uint32_t bin;
-
-		/* count zeros, and offset into correct bin */
-		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
-		stats->size_bins[bin]++;
-	} else {
-		if (s < 64)
-			stats->size_bins[0]++;
-		else if (s < 1519)
-			stats->size_bins[6]++;
-		else
-			stats->size_bins[7]++;
-	}
-
-	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
-	if (rte_is_multicast_ether_addr(ea)) {
-		if (rte_is_broadcast_ether_addr(ea))
-			stats->broadcast++;
-		else
-			stats->multicast++;
-	}
-}
-
-static inline void
-virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
-{
-	VIRTIO_DUMP_PACKET(m, m->data_len);
-
-	virtio_update_packet_stats(&rxvq->stats, m);
-}
-
 /* Optionally fill offload information in structure */
 static inline int
 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
--

2.17.1

author
#12 lore raw

PATCH v3 1/2 net/virtio: update stats when in order xmit done

In reply to Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. Buffered packets can be dropped due to insufficient space. Moving stats update action just after successful avail ring updates can guarantee correctness.

Fixes e5f456a98d3c ("net/virtio: support in-order Rx and Tx")
drivers/net/virtio/virtio_rxtx.c +44 -44
index 27ead19fb..91df5b1d0 100644
@@ -106,6 +106,48 @@ vq_ring_free_id_packed(struct virtqueue *vq, uint16_t id)
 	dxp->next = VQ_RING_DESC_CHAIN_END;
 }
 
+static inline void
+virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
+{
+	uint32_t s = mbuf->pkt_len;
+	struct rte_ether_addr *ea;
+
+	stats->bytes += s;
+
+	if (s == 64) {
+		stats->size_bins[1]++;
+	} else if (s > 64 && s < 1024) {
+		uint32_t bin;
+
+		/* count zeros, and offset into correct bin */
+		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
+		stats->size_bins[bin]++;
+	} else {
+		if (s < 64)
+			stats->size_bins[0]++;
+		else if (s < 1519)
+			stats->size_bins[6]++;
+		else
+			stats->size_bins[7]++;
+	}
+
+	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
+	if (rte_is_multicast_ether_addr(ea)) {
+		if (rte_is_broadcast_ether_addr(ea))
+			stats->broadcast++;
+		else
+			stats->multicast++;
+	}
+}
+
+static inline void
+virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
+{
+	VIRTIO_DUMP_PACKET(m, m->data_len);
+
+	virtio_update_packet_stats(&rxvq->stats, m);
+}
+
 static uint16_t
 virtqueue_dequeue_burst_rx_packed(struct virtqueue *vq,
 				  struct rte_mbuf **rx_pkts,
@@ -317,7 +359,7 @@ virtio_xmit_cleanup(struct virtqueue *vq, uint16_t num)
 }
 
 /* Cleanup from completed inorder transmits. */
-static void
+static __rte_always_inline void
 virtio_xmit_cleanup_inorder(struct virtqueue *vq, uint16_t num)
 {
 	uint16_t i, idx = vq->vq_used_cons_idx;
@@ -596,6 +638,7 @@ virtqueue_enqueue_xmit_inorder(struct virtnet_tx *txvq,
 		dxp = &vq->vq_descx[vq->vq_avail_idx & (vq->vq_nentries - 1)];
 		dxp->cookie = (void *)cookies[i];
 		dxp->ndescs = 1;
+		virtio_update_packet_stats(&txvq->stats, cookies[i]);
 
 		hdr = (struct virtio_net_hdr *)
 			rte_pktmbuf_prepend(cookies[i], head_size);
@@ -1083,48 +1126,6 @@ virtio_discard_rxbuf_inorder(struct virtqueue *vq, struct rte_mbuf *m)
 	}
 }
 
-static inline void
-virtio_update_packet_stats(struct virtnet_stats *stats, struct rte_mbuf *mbuf)
-{
-	uint32_t s = mbuf->pkt_len;
-	struct rte_ether_addr *ea;
-
-	stats->bytes += s;
-
-	if (s == 64) {
-		stats->size_bins[1]++;
-	} else if (s > 64 && s < 1024) {
-		uint32_t bin;
-
-		/* count zeros, and offset into correct bin */
-		bin = (sizeof(s) * 8) - __builtin_clz(s) - 5;
-		stats->size_bins[bin]++;
-	} else {
-		if (s < 64)
-			stats->size_bins[0]++;
-		else if (s < 1519)
-			stats->size_bins[6]++;
-		else
-			stats->size_bins[7]++;
-	}
-
-	ea = rte_pktmbuf_mtod(mbuf, struct rte_ether_addr *);
-	if (rte_is_multicast_ether_addr(ea)) {
-		if (rte_is_broadcast_ether_addr(ea))
-			stats->broadcast++;
-		else
-			stats->multicast++;
-	}
-}
-
-static inline void
-virtio_rx_stats_updated(struct virtnet_rx *rxvq, struct rte_mbuf *m)
-{
-	VIRTIO_DUMP_PACKET(m, m->data_len);
-
-	virtio_update_packet_stats(&rxvq->stats, m);
-}
-
 /* Optionally fill offload information in structure */
 static inline int
 virtio_rx_offload(struct rte_mbuf *m, struct virtio_net_hdr *hdr)
@@ -2198,7 +2199,6 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 			inorder_pkts[nb_inorder_pkts] = txm;
 			nb_inorder_pkts++;
 
-			virtio_update_packet_stats(&txvq->stats, txm);
 			continue;
 		}
 
Signature · 1 line
2.17.1
author
#13 lore raw

PATCH v3 2/2 net/virtio: on demand cleanup when in order xmit

Check whether space are enough before burst enqueue operation. If more space is needed, will try to clean up used descriptors for space on demand. It can give more chances to free used descriptors, thus will help RFC2544 performance. Also deduct failed xmit packets from total xmit number.

Fixes e5f456a98d3c ("net/virtio: support in-order Rx and Tx")
drivers/net/virtio/virtio_rxtx.c +46 -16
index 91df5b1d0..7d5c60532 100644
@@ -2152,6 +2152,22 @@ virtio_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
 	return nb_tx;
 }
 
+static __rte_always_inline int
+virtio_xmit_try_cleanup_inorder(struct virtqueue *vq, uint16_t need)
+{
+	uint16_t nb_used, nb_clean, nb_descs;
+	struct virtio_hw *hw = vq->hw;
+
+	nb_descs = vq->vq_free_cnt + need;
+	nb_used = VIRTQUEUE_NUSED(vq);
+	virtio_rmb(hw->weak_barriers);
+	nb_clean = RTE_MIN(need, (int)nb_used);
+
+	virtio_xmit_cleanup_inorder(vq, nb_clean);
+
+	return (nb_descs - vq->vq_free_cnt);
+}
+
 uint16_t
 virtio_xmit_pkts_inorder(void *tx_queue,
 			struct rte_mbuf **tx_pkts,
@@ -2161,8 +2177,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	struct virtqueue *vq = txvq->vq;
 	struct virtio_hw *hw = vq->hw;
 	uint16_t hdr_size = hw->vtnet_hdr_size;
-	uint16_t nb_used, nb_avail, nb_tx = 0, nb_inorder_pkts = 0;
+	uint16_t nb_used, nb_tx = 0, nb_inorder_pkts = 0;
 	struct rte_mbuf *inorder_pkts[nb_pkts];
+	int need;
 
 	if (unlikely(hw->started == 0 && tx_pkts != hw->inject_pkts))
 		return nb_tx;
@@ -2178,14 +2195,9 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	if (likely(nb_used > vq->vq_nentries - vq->vq_free_thresh))
 		virtio_xmit_cleanup_inorder(vq, nb_used);
 
-	if (unlikely(!vq->vq_free_cnt))
-		virtio_xmit_cleanup_inorder(vq, nb_used);
-
-	nb_avail = RTE_MIN(vq->vq_free_cnt, nb_pkts);
-
-	for (nb_tx = 0; nb_tx < nb_avail; nb_tx++) {
+	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
 		struct rte_mbuf *txm = tx_pkts[nb_tx];
-		int slots, need;
+		int slots;
 
 		/* optimize ring usage */
 		if ((vtpci_with_feature(hw, VIRTIO_F_ANY_LAYOUT) ||
@@ -2203,6 +2215,17 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		}
 
 		if (nb_inorder_pkts) {
+			need = nb_inorder_pkts - vq->vq_free_cnt;
+			if (unlikely(need > 0)) {
+				need = virtio_xmit_try_cleanup_inorder(vq,
+								       need);
+				if (unlikely(need > 0)) {
+					PMD_TX_LOG(ERR,
+						"No free tx descriptors to "
+						"transmit");
+					break;
+				}
+			}
 			virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 							nb_inorder_pkts);
 			nb_inorder_pkts = 0;
@@ -2211,13 +2234,7 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 		slots = txm->nb_segs + 1;
 		need = slots - vq->vq_free_cnt;
 		if (unlikely(need > 0)) {
-			nb_used = VIRTQUEUE_NUSED(vq);
-			virtio_rmb(hw->weak_barriers);
-			need = RTE_MIN(need, (int)nb_used);
-
-			virtio_xmit_cleanup_inorder(vq, need);
-
-			need = slots - vq->vq_free_cnt;
+			need = virtio_xmit_try_cleanup_inorder(vq, slots);
 
 			if (unlikely(need > 0)) {
 				PMD_TX_LOG(ERR,
@@ -2232,9 +2249,22 @@ virtio_xmit_pkts_inorder(void *tx_queue,
 	}
 
 	/* Transmit all inorder packets */
-	if (nb_inorder_pkts)
+	if (nb_inorder_pkts) {
+		need = nb_inorder_pkts - vq->vq_free_cnt;
+		if (unlikely(need > 0)) {
+			need = virtio_xmit_try_cleanup_inorder(vq,
+								  need);
+			if (unlikely(need > 0)) {
+				PMD_TX_LOG(ERR,
+					"No free tx descriptors to transmit");
+				nb_inorder_pkts = vq->vq_free_cnt;
+				nb_tx -= need;
+			}
+		}
+
 		virtqueue_enqueue_xmit_inorder(txvq, inorder_pkts,
 						nb_inorder_pkts);
+	}
 
 	txvq->stats.packets += nb_tx;
 
Signature · 1 line
2.17.1
Reviewed-by on v3 2/2
#14 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] net/virtio: on demand cleanup when in order xmit

Quoted 11 lines · Marvin Liu

Check whether space are enough before burst enqueue operation. If more space is needed, will try to clean up used descriptors for space on demand. It can give more chances to free used descriptors, thus will help RFC2544 performance. Also deduct failed xmit packets from total xmit number.

Fixes e5f456a98d3c ("net/virtio: support in-order Rx and Tx")

Thanks,
Maxime

#15 lore raw

Re: [dpdk-dev] [PATCH v3 2/2] net/virtio: on demand cleanup when in order xmit

In reply to Marvin Liu
Quoted 10 lines · Marvin Liu

Check whether space are enough before burst enqueue operation. If more space is needed, will try to clean up used descriptors for space on demand. It can give more chances to free used descriptors, thus will help RFC2544 performance. Also deduct failed xmit packets from total xmit number.

Fixes e5f456a98d3c ("net/virtio: support in-order Rx and Tx")

Applied to dpdk-next-virtio/master.

Thanks,
Maxime

Reviewed-by on v3 1/2
#16 lore raw
In reply to Marvin Liu
Quoted 10 lines · Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. Buffered packets can be dropped due to insufficient space. Moving stats update action just after successful avail ring updates can guarantee correctness.

Fixes e5f456a98d3c ("net/virtio: support in-order Rx and Tx")

Thanks,
Maxime

#17 lore raw
In reply to Marvin Liu
Quoted 10 lines · Marvin Liu

When doing xmit in-order enqueue, packets are buffered and then flushed into avail ring. Buffered packets can be dropped due to insufficient space. Moving stats update action just after successful avail ring updates can guarantee correctness.

Fixes e5f456a98d3c ("net/virtio: support in-order Rx and Tx")

Applied to dpdk-next-virtio/master.

Thanks,
Maxime