public inbox for qemu-devel@nongnu.org
 help / color / mirror / Atom feed
From: Eugenio Perez Martin <eperezma@redhat.com>
To: Jonah Palmer <jonah.palmer@oracle.com>
Cc: qemu-devel@nongnu.org, eduardo@habkost.net,
	marcel.apfelbaum@gmail.com,  philmd@linaro.org,
	wangyanan55@huawei.com, zhao1.liu@intel.com,  mst@redhat.com,
	sgarzare@redhat.com, jasowang@redhat.com, leiyang@redhat.com,
	 si-wei.liu@oracle.com, boris.ostrovsky@oracle.com,
	armbru@redhat.com
Subject: Re: [RFC v2 02/14] virtio,virtio-net: add initial early VMSD for setup-phase migration
Date: Tue, 24 Mar 2026 10:27:59 +0100	[thread overview]
Message-ID: <CAJaqyWfsr8GUfcXPz6xwcXn3TA2_k4o8+_ySr6znExj34p5VtA@mail.gmail.com> (raw)
In-Reply-To: <20260320142015.3856652-3-jonah.palmer@oracle.com>

On Fri, Mar 20, 2026 at 3:25 PM Jonah Palmer <jonah.palmer@oracle.com> wrote:
>
> Adds a separate VMStateDescription for virtio-net that uses the
> .early_setup feature. With this feature, we can migrate a virtio-net
> device's state earlier, before the stop-and-copy phase.
>
> Future patches will utilize this to move control plane operations out of
> the stop-and-copy phase to reduce the downtime latency caused by
> migrating a virtio-net device.
>
> A VirtIODevMigration migration data structure is also introduced here
> for VirtIODevices to help track the current state of a migration.
>
> The early_load member is used to signal that a VirtIODevice is being
> loaded early and to not throw an error regarding vring indices.
> Inconsistent indices shouldn't be an issue for a device so long as the
> final indices are eventually loaded before the device starts.
>
> Signed-off-by: Jonah Palmer <jonah.palmer@oracle.com>
> ---
>  hw/net/virtio-net.c        | 53 ++++++++++++++++++++++++++++++++++++++
>  hw/virtio/virtio.c         | 14 +++++++++-
>  include/hw/virtio/virtio.h |  9 +++++++
>  3 files changed, 75 insertions(+), 1 deletion(-)
>
> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> index 12b3456ca2..ddd6ed6e62 100644
> --- a/hw/net/virtio-net.c
> +++ b/hw/net/virtio-net.c
> @@ -3864,6 +3864,37 @@ static bool failover_hide_primary_device(DeviceListener *listener,
>      return qatomic_read(&n->failover_primary_hidden);
>  }
>
> +static int virtio_net_early_pre_load(void *opaque)
> +{
> +    VirtIONet *n = opaque;
> +    VirtIODevice *vdev = VIRTIO_DEVICE(n);
> +
> +    vdev->migration->early_load = true;
> +    return 0;
> +}
> +
> +static int virtio_net_early_post_load(void *opaque, int version_id)
> +{
> +    VirtIONet *n = opaque;
> +    VirtIODevice *vdev = VIRTIO_DEVICE(n);
> +
> +    vdev->migration->early_load = false;
> +    return 0;
> +}
> +
> +static const VMStateDescription vmstate_virtio_net_early = {
> +    .name = "virtio-net-early",
> +    .minimum_version_id = VIRTIO_NET_VM_VERSION,
> +    .version_id = VIRTIO_NET_VM_VERSION,
> +    .early_setup = true,
> +    .pre_load = virtio_net_early_pre_load,
> +    .post_load = virtio_net_early_post_load,
> +    .fields = (const VMStateField[]) {
> +        VMSTATE_VIRTIO_DEVICE,
> +        VMSTATE_END_OF_LIST()
> +    },
> +};
> +
>  static void virtio_net_device_realize(DeviceState *dev, Error **errp)
>  {
>      VirtIODevice *vdev = VIRTIO_DEVICE(dev);
> @@ -4046,6 +4077,21 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
>              n->rss_data.specified_hash_types.on_bits |
>              n->rss_data.specified_hash_types.auto_bits;
>      }
> +
> +    if (n->early_mig) {
> +        if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
> +            /*
> +             * vhost-user backend is not currently supported for the early
> +             * migration path.
> +             */
> +            n->early_mig = false;
> +        } else {
> +            vdev->migration = g_new0(VirtIODevMigration, 1);
> +            vdev->migration->early_load = false;
> +
> +            vmstate_register_any(VMSTATE_IF(n), &vmstate_virtio_net_early, n);
> +        }
> +    }
>  }
>
>  static void virtio_net_device_unrealize(DeviceState *dev)
> @@ -4090,6 +4136,13 @@ static void virtio_net_device_unrealize(DeviceState *dev)
>      g_free(n->rss_data.indirections_table);
>      net_rx_pkt_uninit(n->rx_pkt);
>      virtio_cleanup(vdev);
> +
> +    if (n->early_mig) {
> +        g_free(vdev->migration);
> +        vdev->migration = NULL;

Nit: You can use g_clear_pointer(vdev->migration, g_free) here.

> +
> +        vmstate_unregister(VMSTATE_IF(n), &vmstate_virtio_net_early, n);
> +    }
>  }
>
>  static void virtio_net_reset(VirtIODevice *vdev)
> diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
> index 8fcf6cfd0b..48de4a430b 100644
> --- a/hw/virtio/virtio.c
> +++ b/hw/virtio/virtio.c
> @@ -3323,6 +3323,7 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
>      int32_t config_len;
>      uint32_t num;
>      uint32_t features;
> +    bool inconsistent_indices;
>      BusState *qbus = qdev_get_parent_bus(DEVICE(vdev));
>      VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
>      VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
> @@ -3460,6 +3461,14 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
>          if (vdev->vq[i].vring.desc) {
>              uint16_t nheads;
>
> +            /*
> +             * Ring indices will be inconsistent for a VMStateDescription
> +             * performing an early load. This shouldn't be an issue as the
> +             * final indices will get sent later once the source has been
> +             * stopped.
> +             */
> +            inconsistent_indices = vdev->migration && vdev->migration->early_load;
> +
>              /*
>               * VIRTIO-1 devices migrate desc, used, and avail ring addresses so
>               * only the region cache needs to be set up.  Legacy devices need
> @@ -3481,12 +3490,15 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id)
>
>              nheads = vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_avail_idx;
>              /* Check it isn't doing strange things with descriptor numbers. */
> -            if (nheads > vdev->vq[i].vring.num) {
> +            if (!inconsistent_indices && nheads > vdev->vq[i].vring.num) {
>                  virtio_error(vdev, "VQ %d size 0x%x Guest index 0x%x "
>                               "inconsistent with Host index 0x%x: delta 0x%x",
>                               i, vdev->vq[i].vring.num,
>                               vring_avail_idx(&vdev->vq[i]),
>                               vdev->vq[i].last_avail_idx, nheads);
> +                inconsistent_indices = true;
> +            }
> +            if (inconsistent_indices) {
>                  vdev->vq[i].used_idx = 0;
>                  vdev->vq[i].shadow_avail_idx = 0;
>                  vdev->vq[i].inuse = 0;
> diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
> index 6344bd7b68..4c886eb48b 100644
> --- a/include/hw/virtio/virtio.h
> +++ b/include/hw/virtio/virtio.h
> @@ -99,6 +99,14 @@ enum virtio_device_endian {
>      VIRTIO_DEVICE_ENDIAN_BIG,
>  };
>
> +/**
> + * struct VirtIODevMigration - Common VirtIODevice migration structure
> + * @early_load: Flag to indicate an early virtio_load for the device.
> + */
> +typedef struct VirtIODevMigration {
> +    bool early_load;
> +} VirtIODevMigration;
> +
>  /**
>   * struct VirtIODevice - common VirtIO structure
>   * @name: name of the device
> @@ -168,6 +176,7 @@ struct VirtIODevice
>       */
>      EventNotifier config_notifier;
>      bool device_iotlb_enabled;
> +    VirtIODevMigration *migration;

Can we use something like net "struct VirtIONetMigTmp" for this so
VirtIODevice does not need to be expanded?

>  };
>
>  struct VirtioDeviceClass {
> --
> 2.51.0
>



  reply	other threads:[~2026-03-24  9:29 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-20 14:20 [RFC v2 00/14] virtio-net: early VMStateDescription live migration support Jonah Palmer
2026-03-20 14:20 ` [RFC v2 01/14] machine,virtio-net: add early-mig property Jonah Palmer
2026-03-23 10:25   ` Eugenio Perez Martin
2026-03-24 14:07     ` Jonah Palmer
2026-03-26  8:02       ` Eugenio Perez Martin
2026-03-20 14:20 ` [RFC v2 02/14] virtio, virtio-net: add initial early VMSD for setup-phase migration Jonah Palmer via qemu development
2026-03-24  9:27   ` Eugenio Perez Martin [this message]
2026-03-24 14:28     ` [RFC v2 02/14] virtio,virtio-net: " Jonah Palmer
2026-03-24 14:38       ` Eugenio Perez Martin
2026-03-24 17:16         ` Jonah Palmer
2026-03-20 14:20 ` [RFC v2 03/14] virtio,virtio-net: virtio-delta VMSD - VQ state Jonah Palmer
2026-03-20 14:20 ` [RFC v2 04/14] virtio-net: detect VirtIODevice status mid-migration change Jonah Palmer
2026-03-24 10:45   ` Eugenio Perez Martin
2026-03-24 15:01     ` Jonah Palmer
2026-03-26 10:08       ` Eugenio Perez Martin
2026-03-20 14:20 ` [RFC v2 05/14] virtio-net: detect VirtIODevice config buffer " Jonah Palmer
2026-03-24 10:48   ` Eugenio Perez Martin
2026-03-24 15:25     ` Jonah Palmer
2026-03-20 14:20 ` [RFC v2 06/14] virtio-net: detect VirtIONet MAC addr " Jonah Palmer
2026-03-20 14:20 ` [RFC v2 07/14] virtio-net: detect VirtIONet MAC table mid-migration changes Jonah Palmer
2026-03-20 14:20 ` [RFC v2 08/14] virtio-net: detect VirtIONet status mid-migration change Jonah Palmer
2026-03-24 11:26   ` Eugenio Perez Martin
2026-03-24 16:23     ` Jonah Palmer
2026-03-26 10:20       ` Eugenio Perez Martin
2026-03-20 14:20 ` [RFC v2 09/14] virtio-net: detect VirtIONet Rx filter mid-migration changes Jonah Palmer
2026-03-20 14:20 ` [RFC v2 10/14] virtio-net: detect VirtIONet VLAN filter table changes Jonah Palmer
2026-03-20 14:20 ` [RFC v2 11/14] virtio-net: detect VirtIONet guest offload & MQ mid-migration changes Jonah Palmer
2026-03-20 14:20 ` [RFC v2 12/14] virtio-net: detect RSS state " Jonah Palmer
2026-03-20 14:20 ` [RFC v2 13/14] virtio-net: detect pending Tx work for VQs " Jonah Palmer
2026-03-24 11:35   ` Eugenio Perez Martin
2026-03-24 16:47     ` Jonah Palmer
2026-03-26 10:30       ` Eugenio Perez Martin
2026-03-20 14:20 ` [RFC v2 14/14] virtio-net, vhost-net: early migration support for vhost-net Jonah Palmer via qemu development

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAJaqyWfsr8GUfcXPz6xwcXn3TA2_k4o8+_ySr6znExj34p5VtA@mail.gmail.com \
    --to=eperezma@redhat.com \
    --cc=armbru@redhat.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=eduardo@habkost.net \
    --cc=jasowang@redhat.com \
    --cc=jonah.palmer@oracle.com \
    --cc=leiyang@redhat.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sgarzare@redhat.com \
    --cc=si-wei.liu@oracle.com \
    --cc=wangyanan55@huawei.com \
    --cc=zhao1.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox