Skip to contentSkip to conversation

virtio: implement suspend and resume feature

RFC New Zhu Lingshan 1 participant 3 messages Sep 12, 2025

lore
Share & tools
lore
#0 lore raw

cover virtio: implement suspend and resume feature

According to the spec changes[1,2], this RFC series implements suspend and resume facility for virtio, and a reference design on virtio-net.

This suspend and resume feature will be supported on more device types after the basic facilities settling down.

Plese help review, Thanks!!!

[1] https://lore.kernel.org/virtio-comment/[email protected]
[2] https://github.com/oasis-tcs/virtio-spec/issues/229

Zhu Lingshan (2):
virtio: implement virtio data-plane suspend and resume facility
virtio: implement virtio control-plane suspend and resume facility

 hw/net/virtio-net.c                           | 115 ++++++++++++++++++
 hw/virtio/virtio-pci.c                        |   5 +
 hw/virtio/virtio.c                            |  24 ++++
 include/hw/virtio/virtio.h                    |   1 +
 .../standard-headers/linux/virtio_config.h    |   6 +
 5 files changed, 151 insertions(+)
Signature · 1 line
2.51.0
#1 lore raw

RFC 1/2 virtio: implement virtio data-plane suspend and resume facility

This commit implements suspend and resume
facility of virtio data-plane according to the new spec changes, and a reference design on virtio-net.

---
 hw/net/virtio-net.c                           | 115 ++++++++++++++++++
 hw/virtio/virtio.c                            |   8 ++
 .../standard-headers/linux/virtio_config.h    |   6 +
 3 files changed, 129 insertions(+)
hw/net/virtio-net.c +115 -0
index 6b5b5dace3..5989349d2b 100644
@@ -382,6 +382,109 @@ static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
     }
 }
 
+static void virtio_net_suspend_dataplane(VirtIONet *n)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+    VirtIONetQueue *q;
+    NetClientState *nc;
+    int queue_pairs, cvq, i;
+
+    // suspend vhost
+    if (n->vhost_started) {
+        queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
+        cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
+              n->max_ncs - n->max_queue_pairs : 0;
+
+        vhost_net_stop(vdev, n->nic->ncs, queue_pairs, cvq);
+        n->vhost_started = 0;
+    }
+
+    // suspend qemu emulated queues
+    for (i = 0; i < n->max_queue_pairs; i++) {
+        q = &n->vqs[i];
+        nc = qemu_get_subqueue(n->nic, i);
+
+        // disable qeueu notifications
+        //virtio_queue_set_notification(q->rx_vq, 0);
+        virtio_queue_set_notification(q->tx_vq, 0);
+
+        // disable timer and BH
+        if (q->tx_timer) {
+            timer_del(q->tx_timer);
+        } else {
+            qemu_bh_cancel(q->tx_bh);
+        }
+        q->tx_waiting = 0;
+
+        // clean queues
+        qemu_purge_queued_packets(nc);
+
+        // clean async transitions
+        if (q->async_tx.elem) {
+            virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
+            virtio_notify(vdev, q->tx_vq);
+            g_free(q->async_tx.elem);
+            q->async_tx.elem = NULL;
+        }
+    }
+}
+
+static int virtio_net_resume_dataplane(VirtIONet *n)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(n);
+    NetClientState *nc = qemu_get_queue(n->nic);
+    VirtIONetQueue *q;
+    NetClientState *qnc;
+    int queue_pairs, cvq, i, r;
+
+    if (!(vdev->status & VIRTIO_CONFIG_S_SUSPEND))
+        return -1;
+
+    // resume vhost
+    if (get_vhost_net(nc->peer)) {
+        queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
+        cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
+              n->max_ncs - n->max_queue_pairs : 0;
+        r = vhost_net_start(vdev, n->nic->ncs, queue_pairs, cvq);
+        if (r < 0) {
+            error_report("unable to start vhost net: %d", r);
+            n->vhost_started = 1;
+
+            return -1;
+        }
+
+        n->vhost_started = 1;
+
+        for (i = 0;  i < queue_pairs; i++) {
+            qnc = qemu_get_subqueue(n->nic, i);
+
+            /* Purge both directions: TX and RX. */
+            qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
+            qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
+        }
+    }
+
+    // resume qemu emulated queues
+    if (!n->vhost_started) {
+        for (i = 0; i < n->max_queue_pairs; i++) {
+            q = &n->vqs[i];
+            nc = qemu_get_subqueue(n->nic, i);
+
+            if (q->tx_timer) {
+                timer_mod(q->tx_timer,
+                          qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
+            } else {
+                replay_bh_schedule_event(q->tx_bh);
+                virtio_queue_set_notification(q->tx_vq, 1);
+            }
+            // flush packets
+            qemu_flush_queued_packets(nc);
+        }
+    }
+
+    return 0;
+}
+
 static int virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
@@ -389,6 +492,18 @@ static int virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
     int i;
     uint8_t queue_status;
 
+    if ((vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        (status & VIRTIO_CONFIG_S_SUSPEND) &&
+        (virtio_vdev_has_feature(vdev, VIRTIO_F_SUSPEND))) {
+        virtio_net_suspend_dataplane(n);
+    }
+
+    if ((!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) &&
+        (vdev->status & VIRTIO_CONFIG_S_SUSPEND) &&
+        (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
+        virtio_net_resume_dataplane(n);
+        }
+
     virtio_net_vnet_endian_status(n, status);
     virtio_net_vhost_status(n, status);
 
hw/virtio/virtio.c +8 -0
index 9a81ad912e..e5abbc4601 100644
@@ -2268,6 +2268,14 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val)
     }
     vdev->status = val;
 
+    /* The driver suspends the device */
+    if (virtio_vdev_has_feature(vdev, VIRTIO_F_SUSPEND) &&
+        (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        (val & VIRTIO_CONFIG_S_SUSPEND)  && !ret) {
+            virtio_set_started(vdev, false);
+            vdev->status &= !VIRTIO_CONFIG_S_DRIVER_OK;
+    }
+
     return ret;
 }
 
include/standard-headers/linux/virtio_config.h +6 -0
index 45be0fa1bc..ba380f2b52 100644
@@ -40,6 +40,8 @@
 #define VIRTIO_CONFIG_S_DRIVER_OK	4
 /* Driver has finished configuring features */
 #define VIRTIO_CONFIG_S_FEATURES_OK	8
+/* Driver has suspended the device. */
+#define VIRTIO_CONFIG_S_SUSPEND		16
 /* Device entered invalid state, driver must reset it */
 #define VIRTIO_CONFIG_S_NEEDS_RESET	0x40
 /* We've given up on this device. */
@@ -117,5 +119,9 @@
  * This feature indicates that the device support administration virtqueues.
  */
 #define VIRTIO_F_ADMIN_VQ		41
+/*
+ * This feature indicates that the driver can suspend the device
+ */
+#define VIRTIO_F_SUSPEND		43
 
 #endif /* _LINUX_VIRTIO_CONFIG_H */
Signature · 1 line
2.51.0
#2 lore raw

RFC 2/2 virtio: implement virtio control-plane suspend and resume facility

In reply to Zhu Lingshan

This commit implements suspend and resume facility of virtio control-plane according to the new spec changes, and a reference design on virtio-net.

---
 hw/virtio/virtio-pci.c     |  5 +++++
 hw/virtio/virtio.c         | 16 ++++++++++++++++
 include/hw/virtio/virtio.h |  1 +
 3 files changed, 22 insertions(+)
hw/virtio/virtio-pci.c +5 -0
index 767216d795..bc9189746f 100644
@@ -1580,6 +1580,11 @@ static void virtio_pci_common_write(void *opaque, hwaddr addr,
         return;
     }
 
+    /* only allow writing device status while suspended */
+    if ((vdev->status & VIRTIO_CONFIG_S_SUSPEND) && (addr != VIRTIO_PCI_COMMON_STATUS)) {
+        return;
+    }
+
     switch (addr) {
     case VIRTIO_PCI_COMMON_DFSELECT:
         proxy->dfselect = val;
hw/virtio/virtio.c +16 -0
index e5abbc4601..9d632d72be 100644
@@ -2242,8 +2242,10 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val)
 {
     VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
     trace_virtio_set_status(vdev, val);
+    uint8_t old_status;
     int ret = 0;
 
+    old_status = vdev->status;
     if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) {
         if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) &&
             val & VIRTIO_CONFIG_S_FEATURES_OK) {
@@ -2276,6 +2278,15 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val)
             vdev->status &= !VIRTIO_CONFIG_S_DRIVER_OK;
     }
 
+    /* The device has been resumed */
+    if ((!(vdev->status & VIRTIO_CONFIG_S_SUSPEND)) &&
+        (old_status & VIRTIO_CONFIG_S_SUSPEND) &&
+        (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
+        (vdev->config_interrupt_pending)) {
+            virtio_notify_config(vdev);
+            vdev->config_interrupt_pending = false;
+    }
+
     return ret;
 }
 
@@ -2714,6 +2725,11 @@ void virtio_notify_config(VirtIODevice *vdev)
     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))
         return;
 
+    if (vdev->status & VIRTIO_CONFIG_S_SUSPEND) {
+        vdev->config_interrupt_pending = true;
+        return;
+    }
+
     virtio_set_isr(vdev, 0x3);
     vdev->generation++;
     virtio_notify_vector(vdev, vdev->config_vector);
include/hw/virtio/virtio.h +1 -0
index c594764f23..9d66076f75 100644
@@ -167,6 +167,7 @@ struct VirtIODevice
      */
     EventNotifier config_notifier;
     bool device_iotlb_enabled;
+    bool config_interrupt_pending;
 };
 
 struct VirtioDeviceClass {
Signature · 1 line
2.51.0