Virtio in order feature support for virtio-net device.
cover Virtio in order feature support for virtio-net device.
In virtio-spec 1.1, new feature bit VIRTIO_F_IN_ORDER was introduced. When this feature has been negotiated, virtio driver will use descriptors in ring order: starting from offset 0 in the table, and wrapping around at the end of the table. Virtio devices will always use descriptors in the same order in which they have been made available. This can reduce virtio accesses to used ring.
Based on updated virtio-spec, this series realized IN_ORDER prototype for virtio-net device in QEMU.
Some work haven't been done in this patch series:
1. Virtio device in_order support for packed vq is left for the future.
Related patches:
In order feature in Linux(support virtio driver, vhost_test and vsock device): https://lkml.org/lkml/2022/8/17/643
Guo Zhi (2):
virtio: expose used buffers
virtio: enable f_in_order feature for virtio-net
hw/net/virtio-net.c | 30 +++++++++++++++++++++++++++--- include/hw/virtio/virtio.h | 4 +++- 2 files changed, 30 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
RFC 1/2 virtio: expose used buffers
Follow VIRTIO 1.1 spec, we can only writing out a single used ring for a batch of descriptors, and only notify guest when the batch of descriptors have all been used.
We do that batch for tx, because the driver doesn't need to know the length of tx buffer, while for rx, we don't apply the batch strategy.
--- hw/net/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
hw/net/virtio-net.c +26 -3
@@ -2542,8 +2542,10 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)VirtIONet *n = q->n;VirtIODevice *vdev =VIRTIO_DEVICE (n);VirtQueueElement *elem; +VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE ];int32_t num_packets =0 ;int queue_index =vq2q (virtio_get_queue_index (q->tx_vq)); +size_t j;if (!(vdev->status &VIRTIO_CONFIG_S_DRIVER_OK )) {return num_packets; }
@@ -2621,14 +2623,35 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) } drop: -virtqueue_push (q->tx_vq, elem,0 ); -virtio_notify (vdev, q->tx_vq); -g_free (elem); +if (!virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) { +virtqueue_push (q->tx_vq, elem,0 ); +virtio_notify (vdev, q->tx_vq); +g_free (elem); + }else { + elems[num_packets] = elem; + }if (++num_packets >= n->tx_burst) {break ; } } + +if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER ) && num_packets) { +/** +* If in order feature negotiated, devices can notify the use of a batch +* of buffers to the driver by only writing out a single used ring entry +* with the id corresponding to the head entry of the descriptor chain +* describing the last buffer in the batch. +*/ +virtqueue_fill (q->tx_vq, elems[num_packets -1 ],0 ,0 ); +for (j =0 ; j < num_packets; j++) { +g_free (elems[j]); + } + +virtqueue_flush (q->tx_vq, num_packets); +virtio_notify (vdev, q->tx_vq); + } +return num_packets; }
Signature · 1 line
2.17.1
Re: [RFC 1/2] virtio: expose used buffers
Quoted 47 lines · Guo Zhi
Follow VIRTIO 1.1 spec, we can only writing out a single used ring for a batch of descriptors, and only notify guest when the batch of descriptors have all been used.
We do that batch for tx, because the driver doesn't need to know the length of tx buffer, while for rx, we don't apply the batch strategy.
Signed-off-by Guo Zhi <[email protected]>--- hw/net/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)hw/net/virtio-net.c +9
-3@@ -2542,8 +2542,10 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)VirtIONet *n = q->n;VirtIODevice *vdev =VIRTIO_DEVICE (n);VirtQueueElement *elem; +VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE ];int32_t num_packets =0 ;int queue_index =vq2q (virtio_get_queue_index (q->tx_vq)); +size_t j;if (!(vdev->status &VIRTIO_CONFIG_S_DRIVER_OK )) {return num_packets; }@@ -2621,14 +2623,35 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) } drop: -virtqueue_push (q->tx_vq, elem,0 ); -virtio_notify (vdev, q->tx_vq); -g_free (elem); +if (!virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) { +virtqueue_push (q->tx_vq, elem,0 ); +virtio_notify (vdev, q->tx_vq); +g_free (elem); + }else { + elems[num_packets] = elem; + }if (++num_packets >= n->tx_burst) {break ; } }
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 2647
Quoted 10 lines
hw/net/virtio-net.c +10
-0+ +if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER ) && num_packets) { +/** +* If in order feature negotiated, devices can notify the use of a batch +* of buffers to the driver by only writing out a single used ring entry +* with the id corresponding to the head entry of the descriptor chain +* describing the last buffer in the batch. +*/ +virtqueue_fill (q->tx_vq, elems[num_packets -1 ],0 ,0 ); +for (j =0 ; j < num_packets; j++) {
There are a few calls on virtqueue_pop that we need to keep cleaning here. For example, the increment on vq->inuse or dma_memory_map/unmap. Maybe it is ok to call virtqueue_detach_element here for all skipped buffers of the batch, but I haven't reviewed it in depth.
Also, if we want to batch, we must increment used idx accordingly. From the standard, "The device then skips forward in the [used] ring according to the size of the batch. Accordingly, it increments the used idx by the size of the batch."
If we are sure virtio-net device will use tx virtqueue in order, maybe it is better to enable the in order feature bit before and then do the batching on top.
Thanks!
Quoted 13 lines
hw/net/virtio-net.c +7
-1+g_free (elems[j]); + } + +virtqueue_flush (q->tx_vq, num_packets); +virtio_notify (vdev, q->tx_vq); + } +return num_packets; } --2.17.1
Re: [RFC 1/2] virtio: expose used buffers
----- Original Message -----
Quoted 64 lines
From: "eperezma" <[email protected]>
To: "Guo Zhi" <[email protected]>
Cc: "jasowang" <[email protected]>, "sgarzare" <[email protected]>, "Michael Tsirkin" <[email protected]>, "[email protected] Developers" <[email protected]>
Sent: Monday, August 22, 2022 10:08:32 PM
Subject: Re: [RFC 1/2] virtio: expose used buffersQuoted 56 lines · Guo Zhi
Follow VIRTIO 1.1 spec, we can only writing out a single used ring for a batch of descriptors, and only notify guest when the batch of descriptors have all been used.
We do that batch for tx, because the driver doesn't need to know the length of tx buffer, while for rx, we don't apply the batch strategy.
Signed-off-by Guo Zhi <[email protected]>--- hw/net/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)hw/net/virtio-net.c +13
-3@@ -2542,8 +2542,10 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)VirtIONet *n = q->n;VirtIODevice *vdev =VIRTIO_DEVICE (n);VirtQueueElement *elem; +VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE ];int32_t num_packets =0 ;int queue_index =vq2q (virtio_get_queue_index (q->tx_vq)); +size_t j;if (!(vdev->status &VIRTIO_CONFIG_S_DRIVER_OK )) {return num_packets; }@@ -2621,14 +2623,35 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) } drop: -virtqueue_push (q->tx_vq, elem,0 ); -virtio_notify (vdev, q->tx_vq); -g_free (elem); +if (!virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) { +virtqueue_push (q->tx_vq, elem,0 ); +virtio_notify (vdev, q->tx_vq); +g_free (elem); + }else { + elems[num_packets] = elem; + }if (++num_packets >= n->tx_burst) {break ; } } + +if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER ) && num_packets) { +/** +* If in order feature negotiated, devices can notify the use of a batch
+ * of buffers to the driver by only writing out a single used ring
entryhw/net/virtio-net.c +2
-0+* with the id corresponding to the head entry of the descriptor chain +* describing the last buffer in the batch.
Quoted 8 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 2647
Quoted 3 lines
hw/net/virtio-net.c +3
-0+*/ +virtqueue_fill (q->tx_vq, elems[num_packets -1 ],0 ,0 ); +for (j =0 ; j < num_packets; j++) {There are a few calls on
virtqueue_popthat we need to keep cleaning here. For example, the increment onvq->inuseor dma_memory_map/unmap. Maybe it is ok to callvirtqueue_detach_elementhere for all skipped buffers of the batch, but I haven't reviewed it in depth.
Yeah, I think I should call virtqueue_detach_element for skipped buffers.
Quoted 6 lines
Also, if we want to batch, we must increment used idx accordingly. From the standard, "The device then skips forward in the [used] ring according to the size of the batch. Accordingly, it increments the used idx by the size of the batch."
used_idx has been added by the size of the batch, because of this:
virtqueue_flush(q->tx_vq, num_packets);
Thanks!
Quoted 19 lines
If we are sure virtio-net device will use tx virtqueue in order, maybe it is better to enable the in order feature bit before and then do the batching on top.
Thanks!
Quoted 13 lines
hw/net/virtio-net.c +7
-1+g_free (elems[j]); + } + +virtqueue_flush (q->tx_vq, num_packets); +virtio_notify (vdev, q->tx_vq); + } +return num_packets; } --2.17.1
Re: [RFC 1/2] virtio: expose used buffers
Quoted 86 lines · Guo Zhi
----- Original Message -----
Quoted 64 lines
From: "eperezma" <[email protected]>
To: "Guo Zhi" <[email protected]>
Cc: "jasowang" <[email protected]>, "sgarzare" <[email protected]>, "Michael Tsirkin" <[email protected]>, "[email protected] Developers" <[email protected]>
Sent: Monday, August 22, 2022 10:08:32 PM
Subject: Re: [RFC 1/2] virtio: expose used buffersQuoted 56 lines · Guo Zhi
Follow VIRTIO 1.1 spec, we can only writing out a single used ring for a batch of descriptors, and only notify guest when the batch of descriptors have all been used.
We do that batch for tx, because the driver doesn't need to know the length of tx buffer, while for rx, we don't apply the batch strategy.
Signed-off-by Guo Zhi <[email protected]>--- hw/net/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)hw/net/virtio-net.c +13
-3@@ -2542,8 +2542,10 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)VirtIONet *n = q->n;VirtIODevice *vdev =VIRTIO_DEVICE (n);VirtQueueElement *elem; +VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE ];int32_t num_packets =0 ;int queue_index =vq2q (virtio_get_queue_index (q->tx_vq)); +size_t j;if (!(vdev->status &VIRTIO_CONFIG_S_DRIVER_OK )) {return num_packets; }@@ -2621,14 +2623,35 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) } drop: -virtqueue_push (q->tx_vq, elem,0 ); -virtio_notify (vdev, q->tx_vq); -g_free (elem); +if (!virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) { +virtqueue_push (q->tx_vq, elem,0 ); +virtio_notify (vdev, q->tx_vq); +g_free (elem); + }else { + elems[num_packets] = elem; + }if (++num_packets >= n->tx_burst) {break ; } } + +if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER ) && num_packets) { +/** +* If in order feature negotiated, devices can notify the use of a batch
+ * of buffers to the driver by only writing out a single used ring
entryhw/net/virtio-net.c +2
-0+* with the id corresponding to the head entry of the descriptor chain +* describing the last buffer in the batch. Quoted 8 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 2647
Quoted 3 lines
hw/net/virtio-net.c +3
-0+*/ +virtqueue_fill (q->tx_vq, elems[num_packets -1 ],0 ,0 ); +for (j =0 ; j < num_packets; j++) {There are a few calls on
virtqueue_popthat we need to keep cleaning here. For example, the increment onvq->inuseor dma_memory_map/unmap. Maybe it is ok to callvirtqueue_detach_elementhere for all skipped buffers of the batch, but I haven't reviewed it in depth.Yeah, I think I should call
virtqueue_detach_elementfor skipped buffers.Quoted 6 lines
Also, if we want to batch, we must increment used idx accordingly. From the standard, "The device then skips forward in the [used] ring according to the size of the batch. Accordingly, it increments the used idx by the size of the batch."
Quoted 4 lines
used_idxhas been added by the size of the batch, because of this:
virtqueue_flush(q->tx_vq, num_packets);
Oh, right, good point!
Thanks!
Re: [RFC 1/2] virtio: expose used buffers
In reply to Guo ZhiQuoted 4 lines · Guo Zhi
Follow VIRTIO 1.1 spec, we can only writing out a single used ring for a batch of descriptors, and only notify guest when the batch of descriptors have all been used.
Yes, but I don't see anything that is related to the "exposing used buffers", it looks more like batching used buffers etc.
Quoted 42 lines
We do that batch for tx, because the driver doesn't need to know the length of tx buffer, while for rx, we don't apply the batch strategy.
Signed-off-by Guo Zhi <[email protected]>--- hw/net/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)hw/net/virtio-net.c +9
-3@@ -2542,8 +2542,10 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)VirtIONet *n = q->n;VirtIODevice *vdev =VIRTIO_DEVICE (n);VirtQueueElement *elem; +VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE ];int32_t num_packets =0 ;int queue_index =vq2q (virtio_get_queue_index (q->tx_vq)); +size_t j;if (!(vdev->status &VIRTIO_CONFIG_S_DRIVER_OK )) {return num_packets; }@@ -2621,14 +2623,35 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) } drop: -virtqueue_push (q->tx_vq, elem,0 ); -virtio_notify (vdev, q->tx_vq); -g_free (elem); +if (!virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) { +virtqueue_push (q->tx_vq, elem,0 ); +virtio_notify (vdev, q->tx_vq); +g_free (elem); + }else { + elems[num_packets] = elem; + }if (++num_packets >= n->tx_burst) {break ; }
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 2646
Quoted 10 lines
hw/net/virtio-net.c +9
-0} + +if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER ) && num_packets) { +/** +* If in order feature negotiated, devices can notify the use of a batch +* of buffers to the driver by only writing out a single used ring entry +* with the id corresponding to the head entry of the descriptor chain +* describing the last buffer in the batch. +*/ +virtqueue_fill (q->tx_vq, elems[num_packets -1 ],0 ,0 );
So virtqueue_fill will call virtqueue_unmap_sg(), won't this cause mapping to be leaked?
And I think playing batching in device specific code seems sub-optimal, e.g if we want to implement in-order in block we need duplicate the logic here.
Thanks
Quoted 14 lines
hw/net/virtio-net.c +8
-1+for (j =0 ; j < num_packets; j++) { +g_free (elems[j]); + } + +virtqueue_flush (q->tx_vq, num_packets); +virtio_notify (vdev, q->tx_vq); + } +return num_packets; } --2.17.1
Re: [RFC 1/2] virtio: expose used buffers
----- Original Message -----
Quoted 65 lines
From: "jasowang" <[email protected]>
To: "Guo Zhi" <[email protected]>
Cc: "eperezma" <[email protected]>, "sgarzare" <[email protected]>, "Michael Tsirkin" <[email protected]>, "[email protected] Developers" <[email protected]>
Sent: Thursday, August 25, 2022 2:06:11 PM
Subject: Re: [RFC 1/2] virtio: expose used buffersQuoted 4 lines · Guo Zhi
Follow VIRTIO 1.1 spec, we can only writing out a single used ring for a batch of descriptors, and only notify guest when the batch of descriptors have all been used.
Yes, but I don't see anything that is related to the "exposing used buffers", it looks more like batching used buffers etc.
Quoted 49 lines
We do that batch for tx, because the driver doesn't need to know the length of tx buffer, while for rx, we don't apply the batch strategy.
Signed-off-by Guo Zhi <[email protected]>--- hw/net/virtio-net.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)hw/net/virtio-net.c +13
-3@@ -2542,8 +2542,10 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)VirtIONet *n = q->n;VirtIODevice *vdev =VIRTIO_DEVICE (n);VirtQueueElement *elem; +VirtQueueElement *elems[VIRTQUEUE_MAX_SIZE ];int32_t num_packets =0 ;int queue_index =vq2q (virtio_get_queue_index (q->tx_vq)); +size_t j;if (!(vdev->status &VIRTIO_CONFIG_S_DRIVER_OK )) {return num_packets; }@@ -2621,14 +2623,35 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) } drop: -virtqueue_push (q->tx_vq, elem,0 ); -virtio_notify (vdev, q->tx_vq); -g_free (elem); +if (!virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER )) { +virtqueue_push (q->tx_vq, elem,0 ); +virtio_notify (vdev, q->tx_vq); +g_free (elem); + }else { + elems[num_packets] = elem; + }if (++num_packets >= n->tx_burst) {break ; } } + +if (virtio_vdev_has_feature (vdev,VIRTIO_F_IN_ORDER ) && num_packets) { +/** +* If in order feature negotiated, devices can notify the use of a batch
+ * of buffers to the driver by only writing out a single used ring
Quoted 9 lines
hw/net/virtio-net.c static int32_t virtio_net_flush_tx(VirtIONetQueue *q) · line 2646
Quoted 5 lines
entry
hw/net/virtio-net.c +4
-0+* with the id corresponding to the head entry of the descriptor chain +* describing the last buffer in the batch. +*/ +virtqueue_fill (q->tx_vq, elems[num_packets -1 ],0 ,0 );So
virtqueue_fillwill callvirtqueue_unmap_sg(), won't this cause mapping to be leaked?
Virtqueue_push was virtqueue_vill + virtqueue_flush already, and I will call vq_detach_element for exposed buffers.
Quoted 4 lines
And I think playing batching in device specific code seems sub-optimal, e.g if we want to implement in-order in block we need duplicate the logic here.
Only the device knows if the batching is possible,
We have the same problem as in vhost-kernel in this regard, and it was proposed to do per-device there.
Quoted 16 lines
Thanks
Quoted 14 lines
hw/net/virtio-net.c +8
-1+for (j =0 ; j < num_packets; j++) { +g_free (elems[j]); + } + +virtqueue_flush (q->tx_vq, num_packets); +virtio_notify (vdev, q->tx_vq); + } +return num_packets; } --2.17.1
RFC 2/2 virtio: enable f_in_order feature for virtio-net
In reply to Guo ZhiIn order feature is not a transparent feature in QEMU, only specific devices(eg, virtio-net) support it.
--- hw/net/virtio-net.c | 1 + include/hw/virtio/virtio.h | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +1 -0
@@ -719,6 +719,7 @@ static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features, features |= n->host_features;virtio_add_feature (&features,VIRTIO_NET_F_MAC ); +virtio_add_feature (&features,VIRTIO_F_IN_ORDER );if (!peer_has_vnet_hdr (n)) {virtio_clear_feature (&features,VIRTIO_NET_F_CSUM );
include/hw/virtio/virtio.h +3 -1
@@ -291,7 +291,9 @@ typedef struct VirtIORNGConf VirtIORNGConf;DEFINE_PROP_BIT64 ("iommu_platform" , _state, _field, \VIRTIO_F_IOMMU_PLATFORM , false), \DEFINE_PROP_BIT64 ("packed" , _state, _field, \ - VIRTIO_F_RING_PACKED, false) +VIRTIO_F_RING_PACKED , false), \ +DEFINE_PROP_BIT64 ("in_order" , _state, _field, \ + VIRTIO_F_IN_ORDER, false)hwaddr virtio_queue_get_desc_addr (VirtIODevice *vdev,int n);bool virtio_queue_enabled_legacy (VirtIODevice *vdev,int n);
Signature · 1 line
2.17.1
Quoted 7 lines · Guo Zhi
In virtio-spec 1.1, new feature bit
VIRTIO_F_IN_ORDERwas introduced. When this feature has been negotiated, virtio driver will use descriptors in ring order: starting from offset 0 in the table, and wrapping around at the end of the table. Virtio devices will always use descriptors in the same order in which they have been made available. This can reduce virtio accesses to used ring.
So the bached used buffer seems like an optimization, I'd like re-order the patches
1) Just enable virtio-net in-order without the batching, to see the performance
2) Add the batching optimization on top.
Quoted 3 lines
Based on updated virtio-spec, this series realized
IN_ORDERprototype for virtio-net device in QEMU.
Please share with us some performance numbers. E.g pps.
Quoted 3 lines
Some work haven't been done in this patch series:
1. Virtio devicein_ordersupport for packed vq is left for the future.
Anything makes packed virtqueue special?
Thanks
Quoted 15 lines
Related patches:
In order feature inLinux(support virtio driver, vhost_test and vsock device): https://lkml.org/lkml/2022/8/17/643Guo Zhi (2):
virtio: expose used buffers
virtio: enablef_in_orderfeature for virtio-nethw/net/virtio-net.c | 30 +++++++++++++++++++++++++++--- include/hw/virtio/virtio.h | 4 +++- 2 files changed, 30 insertions(+), 4 deletions(-)--
2.17.1