There is a need to add some more fields to VirtIODevice that should be migrated (broken status, endianness). The problem is that we do not want to break compatibility while adding a new feature... This issue has been addressed in the generic VMState code with the use of optional subsections. As a *temporary* alternative to port the whole virtio migration code to VMState, this patch mimics a similar subsectionning ability for virtio.
Since each virtio device is streamed in its own section, the idea is to stream subsections between the end of the device section and the start of the next sections. This allows an older QEMU to complain and exit when fed with subsections: Unknown savevm section type 5 Error -22 while loading VM state The virtio subsections honor the same protocol than the VMState ones: +-------------------------+ |QEMU_VM_SUBSECTION (byte)| 1 byte +-------------------------+ | subsection name length | 1 byte +-------------------------+ | | : subsection name buffer : length bytes | | +-------------------------+ | version_id | 4 bytes (be32) +-------------------------+ | | : subsection data : | | +-------------------------+ The model differs from VMState though as we don't describe the fields in the subsections. As a consequence, there is no "field exists" concept and this impacts backwards migration. Suggested-by: Alexander Graf <ag...@suse.de> Signed-off-by: Greg Kurz <gk...@linux.vnet.ibm.com> --- hw/virtio/virtio.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index cf87b44..7fbad29 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -19,6 +19,7 @@ #include "hw/virtio/virtio.h" #include "qemu/atomic.h" #include "hw/virtio/virtio-bus.h" +#include "migration/migration.h" /* * The alignment to use between consumer and producer parts of vring. @@ -833,6 +834,79 @@ void virtio_notify_config(VirtIODevice *vdev) virtio_notify_vector(vdev, vdev->config_vector); } +typedef struct VirtIOSubsection { + const char *name; + int version_id; + void (*save)(VirtIODevice *vdev, QEMUFile *f); + int (*load)(VirtIODevice *vdev, QEMUFile *f); + int (*needed)(VirtIODevice *vdev); +} VirtIOSubsection; + +static const VirtIOSubsection virtio_subsection[] = { + { .name = NULL } +}; + +static void virtio_save_subsections(VirtIODevice *vdev, QEMUFile *f) +{ + int i; + + for (i = 0; virtio_subsection[i].name; i++) { + VirtIOSubsection sub = virtio_subsection[i]; + + if (sub.needed != NULL && (*sub.needed)(vdev)) { + const char *name = sub.name; + uint8_t len = strlen(name); + + qemu_put_byte(f, QEMU_VM_SUBSECTION); + qemu_put_byte(f, len); + qemu_put_buffer(f, (uint8_t *) name, len); + qemu_put_be32(f, sub.version_id); + (*sub.save)(vdev, f); + } + } +} + +static int virtio_load_subsections(VirtIODevice *vdev, QEMUFile *f) +{ + while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) { + char idstr[256]; + uint8_t len, size; + int i; + + len = qemu_peek_byte(f, 1); + size = qemu_peek_buffer(f, (uint8_t *) idstr, len, 2); + if (size != len) { + break; + } + + idstr[size] = 0; + + for (i = 0; virtio_subsection[i].name; i++) { + VirtIOSubsection sub = virtio_subsection[i]; + + if (strcmp(sub.name, idstr) == 0) { + uint32_t version_id; + int ret; + + qemu_file_skip(f, 1); /* subsection */ + qemu_file_skip(f, 1); /* len */ + qemu_file_skip(f, len); /* idstr */ + + version_id = qemu_get_be32(f); + if (version_id > sub.version_id) { + return -EINVAL; + } + ret = (*sub.load)(vdev, f); + if (ret) { + return ret; + } + } + } + } + + return 0; +} + void virtio_save(VirtIODevice *vdev, QEMUFile *f) { BusState *qbus = qdev_get_parent_bus(DEVICE(vdev)); @@ -876,6 +950,8 @@ void virtio_save(VirtIODevice *vdev, QEMUFile *f) if (vdc->save != NULL) { vdc->save(vdev, f); } + + virtio_save_subsections(vdev, f); } int virtio_set_features(VirtIODevice *vdev, uint32_t val) @@ -967,7 +1043,7 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int version_id) return vdc->load(vdev, f, version_id); } - return 0; + return virtio_load_subsections(vdev, f); } void virtio_cleanup(VirtIODevice *vdev)