586cba
From 0ab3da1092362470d256b433c546bd365d34f930 Mon Sep 17 00:00:00 2001
586cba
From: =?UTF-8?q?Eugenio=20P=C3=A9rez?= <eperezma@redhat.com>
586cba
Date: Thu, 21 Jul 2022 15:38:55 +0200
586cba
Subject: [PATCH 19/32] vhost: add vhost_svq_poll
586cba
MIME-Version: 1.0
586cba
Content-Type: text/plain; charset=UTF-8
586cba
Content-Transfer-Encoding: 8bit
586cba
586cba
RH-Author: Eugenio Pérez <eperezma@redhat.com>
586cba
RH-MergeRequest: 108: Net Control Virtqueue shadow Support
586cba
RH-Commit: [19/27] 6807bb0bb6e5183b46a03b12b4027c7d767e8555 (eperezmartin/qemu-kvm)
586cba
RH-Bugzilla: 1939363
586cba
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
586cba
RH-Acked-by: Cindy Lu <lulu@redhat.com>
586cba
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
586cba
586cba
Bugzilla: https://bugzilla.redhat.com/1939363
586cba
586cba
Upstream Status: git://git.qemu.org/qemu.git
586cba
586cba
commit 3f44d13dda83d390cc9563e56e7d337e4f6223f4
586cba
Author: Eugenio Pérez <eperezma@redhat.com>
586cba
Date:   Wed Jul 20 08:59:38 2022 +0200
586cba
586cba
    vhost: add vhost_svq_poll
586cba
586cba
    It allows the Shadow Control VirtQueue to wait for the device to use the
586cba
    available buffers.
586cba
586cba
    Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
    Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
586cba
    Signed-off-by: Jason Wang <jasowang@redhat.com>
586cba
586cba
Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
586cba
---
586cba
 hw/virtio/vhost-shadow-virtqueue.c | 27 +++++++++++++++++++++++++++
586cba
 hw/virtio/vhost-shadow-virtqueue.h |  1 +
586cba
 2 files changed, 28 insertions(+)
586cba
586cba
diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
586cba
index cb879e7b88..95d0d7a7ee 100644
586cba
--- a/hw/virtio/vhost-shadow-virtqueue.c
586cba
+++ b/hw/virtio/vhost-shadow-virtqueue.c
586cba
@@ -485,6 +485,33 @@ static void vhost_svq_flush(VhostShadowVirtqueue *svq,
586cba
     } while (!vhost_svq_enable_notification(svq));
586cba
 }
586cba
 
586cba
+/**
586cba
+ * Poll the SVQ for one device used buffer.
586cba
+ *
586cba
+ * This function race with main event loop SVQ polling, so extra
586cba
+ * synchronization is needed.
586cba
+ *
586cba
+ * Return the length written by the device.
586cba
+ */
586cba
+size_t vhost_svq_poll(VhostShadowVirtqueue *svq)
586cba
+{
586cba
+    int64_t start_us = g_get_monotonic_time();
586cba
+    do {
586cba
+        uint32_t len;
586cba
+        VirtQueueElement *elem = vhost_svq_get_buf(svq, &len;;
586cba
+        if (elem) {
586cba
+            return len;
586cba
+        }
586cba
+
586cba
+        if (unlikely(g_get_monotonic_time() - start_us > 10e6)) {
586cba
+            return 0;
586cba
+        }
586cba
+
586cba
+        /* Make sure we read new used_idx */
586cba
+        smp_rmb();
586cba
+    } while (true);
586cba
+}
586cba
+
586cba
 /**
586cba
  * Forward used buffers.
586cba
  *
586cba
diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h
586cba
index dd78f4bec2..cf442f7dea 100644
586cba
--- a/hw/virtio/vhost-shadow-virtqueue.h
586cba
+++ b/hw/virtio/vhost-shadow-virtqueue.h
586cba
@@ -89,6 +89,7 @@ void vhost_svq_push_elem(VhostShadowVirtqueue *svq,
586cba
 int vhost_svq_add(VhostShadowVirtqueue *svq, const struct iovec *out_sg,
586cba
                   size_t out_num, const struct iovec *in_sg, size_t in_num,
586cba
                   VirtQueueElement *elem);
586cba
+size_t vhost_svq_poll(VhostShadowVirtqueue *svq);
586cba
 
586cba
 void vhost_svq_set_svq_kick_fd(VhostShadowVirtqueue *svq, int svq_kick_fd);
586cba
 void vhost_svq_set_svq_call_fd(VhostShadowVirtqueue *svq, int call_fd);
586cba
-- 
586cba
2.31.1
586cba