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