[RFC 02/10] virtio: Add set_vq_handler

Eugenio Pérez posted 10 patches 1 year, 6 months ago
[RFC 02/10] virtio: Add set_vq_handler
Posted by Eugenio Pérez 1 year, 6 months ago
So other subsystem can override vq handler and device can reset it.

Signed-off-by: Eugenio Pérez <eperezma@redhat.com>
---
 include/hw/virtio/virtio.h |  5 +++++
 hw/net/virtio-net.c        | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
index 9b5479e256..9988c6d5c9 100644
--- a/include/hw/virtio/virtio.h
+++ b/include/hw/virtio/virtio.h
@@ -149,6 +149,11 @@ struct VirtioDeviceClass {
     void (*guest_notifier_mask)(VirtIODevice *vdev, int n, bool mask);
     int (*start_ioeventfd)(VirtIODevice *vdev);
     void (*stop_ioeventfd)(VirtIODevice *vdev);
+    /*
+     * Set handler for a vq. NULL handler for reset to default.
+     */
+    bool (*set_vq_handler)(VirtIODevice *vdev, unsigned int n,
+                           VirtIOHandleOutput handle_output);
     /* Saving and loading of a device; trying to deprecate save/load
      * use vmsd for new devices.
      */
diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 5150f295e8..f7b2998fb1 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -2699,6 +2699,31 @@ static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
     virtio_net_set_queues(n);
 }
 
+static bool virtio_net_set_vq_handler(VirtIODevice *vdev, unsigned int i,
+                                      VirtIOHandleOutput handle_output)
+{
+    const VirtIONet *n = VIRTIO_NET(vdev);
+    const unsigned max_queues = n->multiqueue ? n->max_queues : 1;
+    VirtQueue *vq;
+
+    /* Reset control queue also not supported */
+    assert(i < max_queues * 2);
+
+    vq = virtio_get_queue(vdev, i);
+    if (handle_output == NULL) {
+        if (i % 2) {
+            handle_output = virtio_net_handle_rx;
+        } else {
+            const VirtIONetQueue *q = &n->vqs[i / 2];
+            handle_output = q->tx_timer ? virtio_net_handle_tx_timer
+                                        : virtio_net_handle_tx_bh;
+        }
+    }
+
+    virtqueue_set_handler(vq, handle_output);
+    return true;
+}
+
 static int virtio_net_post_load_device(void *opaque, int version_id)
 {
     VirtIONet *n = opaque;
@@ -3519,6 +3544,7 @@ static void virtio_net_class_init(ObjectClass *klass, void *data)
     vdc->set_status = virtio_net_set_status;
     vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
     vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
+    vdc->set_vq_handler = virtio_net_set_vq_handler;
     vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
     vdc->post_load = virtio_net_post_load_virtio;
     vdc->vmsd = &vmstate_virtio_net_device;
-- 
2.27.0