microvm was meant for short-lived guests [1] and thus wasn not
versioned, so migration across QEMU versions is not supported. It is now
also intersting for long-lived ones as better isolated alternative for
Linux containers though, which would be nice to allow migration to hosts
with a newer QEMU, and Paolo noted that microvm could start having
versioned variants [2].

Add microvm-X.Y machine types, starting with 12.0. "microvm" itself is
left "unversioned" as it is, instead of becoming an alias of the latest
version. An unversioned machine type is meant to be a moving target
[2], which includes resolving a CPU model name without a version, like
"Cascadelake-Server", to the latest version of the model. A versioned
type needs a fixed default for that, and an alias cannot differ from
the type it refers to. So the versioned types derive from microvm and
default to version 1, like pc and q35 do.

A machine class does not inherit the compat property array of its
parent class and TypeInfo.secure is not inherited either, so the
versioned types add microvm_properties and set the flag again.

Both sides of a migration have to use the same versioned type. The
machine type check of the migration stream does not fully enforce
this, as it only compares as many characters as the name of the source
has, i.e., it rejects microvm-12.0 to microvm, but not microvm to
microvm-12.0.

Also document that with KVM or HVF migration needs the same guest
physical address width on both hosts.

[1] 
https://lore.kernel.org/qemu-devel/[email protected]/
[2] 
https://lore.kernel.org/qemu-devel/[email protected]/

Signed-off-by: Thomas Lamprecht <[email protected]>
---
 docs/system/i386/microvm.rst                  | 21 ++++++++-
 hw/i386/microvm.c                             | 43 +++++++++++++++++++
 .../x86_64/test_cpu_model_versions.py         | 41 ++++++++++++++++++
 3 files changed, 104 insertions(+), 1 deletion(-)

diff --git a/docs/system/i386/microvm.rst b/docs/system/i386/microvm.rst
index 077ea15751..56804f1bd8 100644
--- a/docs/system/i386/microvm.rst
+++ b/docs/system/i386/microvm.rst
@@ -9,6 +9,24 @@ designed for short-lived guests. microvm also establishes a 
baseline
 for benchmarking and optimizing both QEMU and guest operating systems,
 since it is optimized for both boot time and footprint.
 
+``microvm`` itself is not versioned: its defaults may change with every
+QEMU version, so it does not support migration between QEMU versions.
+The ``microvm-X.Y`` machine types are versioned. They keep the behaviour
+they had in the QEMU X.Y release, so that VM migration works between
+QEMU versions, as long as both sides use the same versioned type. The
+first one is ``microvm-12.0``.
+
+On the versioned types, a CPU model name without a version, like
+``Cascadelake-Server``, refers to the first version of the model, as on
+the ``pc`` and ``q35`` machine types, while ``microvm`` uses the latest
+version. Use a versioned name, like ``Cascadelake-Server-v5``, to select
+a specific one.
+
+With KVM or HVF and a 64-bit CPU model, microvm uses the host's
+physical address width for the guest, so migration also requires the
+same width on both hosts, for example by capping it with the
+``host-phys-bits-limit`` CPU property to a value both hosts support.
+
 
 Supported devices
 -----------------
@@ -34,7 +52,8 @@ Currently, microvm does *not* support the following features:
 
 - PCI-only devices.
 - Hotplug of any kind.
-- Live migration across QEMU versions.
+- Live migration across QEMU versions, unless a versioned machine type
+  is used.
 
 
 Using the microvm machine type
diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c
index ae22bdf433..5bb6cd1f51 100644
--- a/hw/i386/microvm.c
+++ b/hw/i386/microvm.c
@@ -735,3 +735,46 @@ static void microvm_machine_init(void)
     type_register_static(&microvm_machine_info);
 }
 type_init(microvm_machine_init);
+
+/*
+ * Defaults of the versioned machine types, which derive from the
+ * unversioned one. The compat properties are added again, as a machine
+ * class does not inherit the compat_props array of its parent class.
+ */
+static void microvm_machine_options(MachineClass *mc)
+{
+    MicrovmMachineClass *mmc = MICROVM_MACHINE_CLASS(mc);
+
+    mmc->default_cpu_version = 1;
+    compat_props_add(mc->compat_props, microvm_properties,
+                     G_N_ELEMENTS(microvm_properties));
+}
+
+#define DEFINE_MICROVM_MACHINE(major, minor) \
+    static void MACHINE_VER_SYM(class_init, microvm, major, minor)( \
+        ObjectClass *oc, \
+        const void *data) \
+    { \
+        MachineClass *mc = MACHINE_CLASS(oc); \
+        microvm_machine_options(mc); \
+        MACHINE_VER_SYM(options, microvm, major, minor)(mc); \
+        MACHINE_VER_DEPRECATION(major, minor); \
+    } \
+    static const TypeInfo MACHINE_VER_SYM(info, microvm, major, minor) = \
+    { \
+        .name = MACHINE_VER_TYPE_NAME("microvm", major, minor), \
+        .parent = TYPE_MICROVM_MACHINE, \
+        .class_init = MACHINE_VER_SYM(class_init, microvm, major, minor), \
+        .secure = true, \
+    }; \
+    static void MACHINE_VER_SYM(register, microvm, major, minor)(void) \
+    { \
+        MACHINE_VER_DELETION(major, minor); \
+        type_register_static(&MACHINE_VER_SYM(info, microvm, major, minor)); \
+    } \
+    type_init(MACHINE_VER_SYM(register, microvm, major, minor));
+
+static void microvm_machine_12_0_options(MachineClass *mc)
+{
+}
+DEFINE_MICROVM_MACHINE(12, 0)
diff --git a/tests/functional/x86_64/test_cpu_model_versions.py 
b/tests/functional/x86_64/test_cpu_model_versions.py
index 36c968f1c0..18b9e934b0 100755
--- a/tests/functional/x86_64/test_cpu_model_versions.py
+++ b/tests/functional/x86_64/test_cpu_model_versions.py
@@ -178,6 +178,47 @@ def test_unversioned_alias(self):
 
         self.validate_aliases(cpus)
 
+    def test_microvm_alias(self):
+        """
+        Check if unversioned CPU model is an alias pointing to the latest
+        version on the unversioned microvm machine type
+        """
+        self.set_machine('microvm')
+        self.vm.add_args('-S')
+        self.vm.launch()
+
+        cpus = dict((m['name'], m) for m in
+                    self.vm.cmd('query-cpu-definitions'))
+
+        latest = max(int(m.group(1)) for m in
+                     (re.fullmatch(r'Cascadelake-Server-v(\d+)', name)
+                      for name in cpus) if m)
+        self.assertEqual(cpus['Cascadelake-Server'].get('alias-of'),
+                         'Cascadelake-Server-v%d' % latest,
+                         'Cascadelake-Server must be an alias of its latest '
+                         'version')
+
+        self.validate_aliases(cpus)
+
+    def test_microvm_12_0_alias(self):
+        """
+        Check if unversioned CPU model is an alias pointing to right version
+        on a versioned microvm machine type
+        """
+        self.set_machine('microvm-12.0')
+        self.vm.add_args('-S')
+        self.vm.launch()
+
+        cpus = dict((m['name'], m) for m in
+                    self.vm.cmd('query-cpu-definitions'))
+
+        self.assertEqual(cpus['Cascadelake-Server'].get('alias-of'),
+                         'Cascadelake-Server-v1',
+                         'Cascadelake-Server must be an alias of '
+                         'Cascadelake-Server-v1')
+
+        self.validate_aliases(cpus)
+
     def test_none_alias(self):
         """
         Check if unversioned CPU model is an alias pointing to some version
-- 
2.47.3



Reply via email to