xref: /openbmc/qemu/docs/devel/migration/main.rst (revision 8d60280e)
1f6bbac98SPeter Xu===================
2f6bbac98SPeter XuMigration framework
3f6bbac98SPeter Xu===================
48cb2f8b1SPeter Xu
58cb2f8b1SPeter XuQEMU has code to load/save the state of the guest that it is running.
68cb2f8b1SPeter XuThese are two complementary operations.  Saving the state just does
78cb2f8b1SPeter Xuthat, saves the state for each device that the guest is running.
88cb2f8b1SPeter XuRestoring a guest is just the opposite operation: we need to load the
98cb2f8b1SPeter Xustate of each device.
108cb2f8b1SPeter Xu
118cb2f8b1SPeter XuFor this to work, QEMU has to be launched with the same arguments the
128cb2f8b1SPeter Xutwo times.  I.e. it can only restore the state in one guest that has
138cb2f8b1SPeter Xuthe same devices that the one it was saved (this last requirement can
148cb2f8b1SPeter Xube relaxed a bit, but for now we can consider that configuration has
158cb2f8b1SPeter Xuto be exactly the same).
168cb2f8b1SPeter Xu
178cb2f8b1SPeter XuOnce that we are able to save/restore a guest, a new functionality is
188cb2f8b1SPeter Xurequested: migration.  This means that QEMU is able to start in one
198cb2f8b1SPeter Xumachine and being "migrated" to another machine.  I.e. being moved to
208cb2f8b1SPeter Xuanother machine.
218cb2f8b1SPeter Xu
228cb2f8b1SPeter XuNext was the "live migration" functionality.  This is important
238cb2f8b1SPeter Xubecause some guests run with a lot of state (specially RAM), and it
248cb2f8b1SPeter Xucan take a while to move all state from one machine to another.  Live
258cb2f8b1SPeter Xumigration allows the guest to continue running while the state is
268cb2f8b1SPeter Xutransferred.  Only while the last part of the state is transferred has
278cb2f8b1SPeter Xuthe guest to be stopped.  Typically the time that the guest is
288cb2f8b1SPeter Xuunresponsive during live migration is the low hundred of milliseconds
298cb2f8b1SPeter Xu(notice that this depends on a lot of things).
308cb2f8b1SPeter Xu
318cb2f8b1SPeter Xu.. contents::
328cb2f8b1SPeter Xu
338cb2f8b1SPeter XuTransports
348cb2f8b1SPeter Xu==========
358cb2f8b1SPeter Xu
368cb2f8b1SPeter XuThe migration stream is normally just a byte stream that can be passed
378cb2f8b1SPeter Xuover any transport.
388cb2f8b1SPeter Xu
398cb2f8b1SPeter Xu- tcp migration: do the migration using tcp sockets
408cb2f8b1SPeter Xu- unix migration: do the migration using unix sockets
418cb2f8b1SPeter Xu- exec migration: do the migration using the stdin/stdout through a process.
428cb2f8b1SPeter Xu- fd migration: do the migration using a file descriptor that is
438cb2f8b1SPeter Xu  passed to QEMU.  QEMU doesn't care how this file descriptor is opened.
44c35462f1SFabiano Rosas- file migration: do the migration using a file that is passed to QEMU
45c35462f1SFabiano Rosas  by path. A file offset option is supported to allow a management
46c35462f1SFabiano Rosas  application to add its own metadata to the start of the file without
4761dec060SFabiano Rosas  QEMU interference. Note that QEMU does not flush cached file
4861dec060SFabiano Rosas  data/metadata at the end of migration.
498cb2f8b1SPeter Xu
50*8d60280eSFabiano Rosas  The file migration also supports using a file that has already been
51*8d60280eSFabiano Rosas  opened. A set of file descriptors is passed to QEMU via an "fdset"
52*8d60280eSFabiano Rosas  (see add-fd QMP command documentation). This method allows a
53*8d60280eSFabiano Rosas  management application to have control over the migration file
54*8d60280eSFabiano Rosas  opening operation. There are, however, strict requirements to this
55*8d60280eSFabiano Rosas  interface if the multifd capability is enabled:
56*8d60280eSFabiano Rosas
57*8d60280eSFabiano Rosas    - the fdset must contain two file descriptors that are not
58*8d60280eSFabiano Rosas      duplicates between themselves;
59*8d60280eSFabiano Rosas    - if the direct-io capability is to be used, exactly one of the
60*8d60280eSFabiano Rosas      file descriptors must have the O_DIRECT flag set;
61*8d60280eSFabiano Rosas    - the file must be opened with WRONLY on the migration source side
62*8d60280eSFabiano Rosas      and RDONLY on the migration destination side.
63*8d60280eSFabiano Rosas
64*8d60280eSFabiano Rosas- rdma migration: support is included for migration using RDMA, which
65*8d60280eSFabiano Rosas  transports the page data using ``RDMA``, where the hardware takes
66*8d60280eSFabiano Rosas  care of transporting the pages, and the load on the CPU is much
67*8d60280eSFabiano Rosas  lower.  While the internals of RDMA migration are a bit different,
68*8d60280eSFabiano Rosas  this isn't really visible outside the RAM migration code.
698cb2f8b1SPeter Xu
708cb2f8b1SPeter XuAll these migration protocols use the same infrastructure to
718cb2f8b1SPeter Xusave/restore state devices.  This infrastructure is shared with the
728cb2f8b1SPeter Xusavevm/loadvm functionality.
738cb2f8b1SPeter Xu
748cb2f8b1SPeter XuCommon infrastructure
758cb2f8b1SPeter Xu=====================
768cb2f8b1SPeter Xu
778cb2f8b1SPeter XuThe files, sockets or fd's that carry the migration stream are abstracted by
788cb2f8b1SPeter Xuthe  ``QEMUFile`` type (see ``migration/qemu-file.h``).  In most cases this
798cb2f8b1SPeter Xuis connected to a subtype of ``QIOChannel`` (see ``io/``).
808cb2f8b1SPeter Xu
818cb2f8b1SPeter Xu
828cb2f8b1SPeter XuSaving the state of one device
838cb2f8b1SPeter Xu==============================
848cb2f8b1SPeter Xu
858cb2f8b1SPeter XuFor most devices, the state is saved in a single call to the migration
868cb2f8b1SPeter Xuinfrastructure; these are *non-iterative* devices.  The data for these
878cb2f8b1SPeter Xudevices is sent at the end of precopy migration, when the CPUs are paused.
888cb2f8b1SPeter XuThere are also *iterative* devices, which contain a very large amount of
898cb2f8b1SPeter Xudata (e.g. RAM or large tables).  See the iterative device section below.
908cb2f8b1SPeter Xu
918cb2f8b1SPeter XuGeneral advice for device developers
928cb2f8b1SPeter Xu------------------------------------
938cb2f8b1SPeter Xu
948cb2f8b1SPeter Xu- The migration state saved should reflect the device being modelled rather
958cb2f8b1SPeter Xu  than the way your implementation works.  That way if you change the implementation
968cb2f8b1SPeter Xu  later the migration stream will stay compatible.  That model may include
978cb2f8b1SPeter Xu  internal state that's not directly visible in a register.
988cb2f8b1SPeter Xu
998cb2f8b1SPeter Xu- When saving a migration stream the device code may walk and check
1008cb2f8b1SPeter Xu  the state of the device.  These checks might fail in various ways (e.g.
1018cb2f8b1SPeter Xu  discovering internal state is corrupt or that the guest has done something bad).
1028cb2f8b1SPeter Xu  Consider carefully before asserting/aborting at this point, since the
1038cb2f8b1SPeter Xu  normal response from users is that *migration broke their VM* since it had
1048cb2f8b1SPeter Xu  apparently been running fine until then.  In these error cases, the device
1058cb2f8b1SPeter Xu  should log a message indicating the cause of error, and should consider
1068cb2f8b1SPeter Xu  putting the device into an error state, allowing the rest of the VM to
1078cb2f8b1SPeter Xu  continue execution.
1088cb2f8b1SPeter Xu
1098cb2f8b1SPeter Xu- The migration might happen at an inconvenient point,
1108cb2f8b1SPeter Xu  e.g. right in the middle of the guest reprogramming the device, during
1118cb2f8b1SPeter Xu  guest reboot or shutdown or while the device is waiting for external IO.
1128cb2f8b1SPeter Xu  It's strongly preferred that migrations do not fail in this situation,
1138cb2f8b1SPeter Xu  since in the cloud environment migrations might happen automatically to
1148cb2f8b1SPeter Xu  VMs that the administrator doesn't directly control.
1158cb2f8b1SPeter Xu
1168cb2f8b1SPeter Xu- If you do need to fail a migration, ensure that sufficient information
1178cb2f8b1SPeter Xu  is logged to identify what went wrong.
1188cb2f8b1SPeter Xu
1198cb2f8b1SPeter Xu- The destination should treat an incoming migration stream as hostile
1208cb2f8b1SPeter Xu  (which we do to varying degrees in the existing code).  Check that offsets
1218cb2f8b1SPeter Xu  into buffers and the like can't cause overruns.  Fail the incoming migration
1228cb2f8b1SPeter Xu  in the case of a corrupted stream like this.
1238cb2f8b1SPeter Xu
1248cb2f8b1SPeter Xu- Take care with internal device state or behaviour that might become
1258cb2f8b1SPeter Xu  migration version dependent.  For example, the order of PCI capabilities
1268cb2f8b1SPeter Xu  is required to stay constant across migration.  Another example would
1278cb2f8b1SPeter Xu  be that a special case handled by subsections (see below) might become
1288cb2f8b1SPeter Xu  much more common if a default behaviour is changed.
1298cb2f8b1SPeter Xu
1308cb2f8b1SPeter Xu- The state of the source should not be changed or destroyed by the
1318cb2f8b1SPeter Xu  outgoing migration.  Migrations timing out or being failed by
1328cb2f8b1SPeter Xu  higher levels of management, or failures of the destination host are
1338cb2f8b1SPeter Xu  not unusual, and in that case the VM is restarted on the source.
1348cb2f8b1SPeter Xu  Note that the management layer can validly revert the migration
1358cb2f8b1SPeter Xu  even though the QEMU level of migration has succeeded as long as it
1368cb2f8b1SPeter Xu  does it before starting execution on the destination.
1378cb2f8b1SPeter Xu
1388cb2f8b1SPeter Xu- Buses and devices should be able to explicitly specify addresses when
1398cb2f8b1SPeter Xu  instantiated, and management tools should use those.  For example,
1408cb2f8b1SPeter Xu  when hot adding USB devices it's important to specify the ports
1418cb2f8b1SPeter Xu  and addresses, since implicit ordering based on the command line order
1428cb2f8b1SPeter Xu  may be different on the destination.  This can result in the
1438cb2f8b1SPeter Xu  device state being loaded into the wrong device.
1448cb2f8b1SPeter Xu
1458cb2f8b1SPeter XuVMState
1468cb2f8b1SPeter Xu-------
1478cb2f8b1SPeter Xu
1488cb2f8b1SPeter XuMost device data can be described using the ``VMSTATE`` macros (mostly defined
1498cb2f8b1SPeter Xuin ``include/migration/vmstate.h``).
1508cb2f8b1SPeter Xu
1518cb2f8b1SPeter XuAn example (from hw/input/pckbd.c)
1528cb2f8b1SPeter Xu
1538cb2f8b1SPeter Xu.. code:: c
1548cb2f8b1SPeter Xu
1558cb2f8b1SPeter Xu  static const VMStateDescription vmstate_kbd = {
1568cb2f8b1SPeter Xu      .name = "pckbd",
1578cb2f8b1SPeter Xu      .version_id = 3,
1588cb2f8b1SPeter Xu      .minimum_version_id = 3,
1598cb2f8b1SPeter Xu      .fields = (const VMStateField[]) {
1608cb2f8b1SPeter Xu          VMSTATE_UINT8(write_cmd, KBDState),
1618cb2f8b1SPeter Xu          VMSTATE_UINT8(status, KBDState),
1628cb2f8b1SPeter Xu          VMSTATE_UINT8(mode, KBDState),
1638cb2f8b1SPeter Xu          VMSTATE_UINT8(pending, KBDState),
1648cb2f8b1SPeter Xu          VMSTATE_END_OF_LIST()
1658cb2f8b1SPeter Xu      }
1668cb2f8b1SPeter Xu  };
1678cb2f8b1SPeter Xu
1688cb2f8b1SPeter XuWe are declaring the state with name "pckbd".  The ``version_id`` is
1698cb2f8b1SPeter Xu3, and there are 4 uint8_t fields in the KBDState structure.  We
1708cb2f8b1SPeter Xuregistered this ``VMSTATEDescription`` with one of the following
1718cb2f8b1SPeter Xufunctions.  The first one will generate a device ``instance_id``
1728cb2f8b1SPeter Xudifferent for each registration.  Use the second one if you already
1738cb2f8b1SPeter Xuhave an id that is different for each instance of the device:
1748cb2f8b1SPeter Xu
1758cb2f8b1SPeter Xu.. code:: c
1768cb2f8b1SPeter Xu
1778cb2f8b1SPeter Xu    vmstate_register_any(NULL, &vmstate_kbd, s);
1788cb2f8b1SPeter Xu    vmstate_register(NULL, instance_id, &vmstate_kbd, s);
1798cb2f8b1SPeter Xu
1808cb2f8b1SPeter XuFor devices that are ``qdev`` based, we can register the device in the class
1818cb2f8b1SPeter Xuinit function:
1828cb2f8b1SPeter Xu
1838cb2f8b1SPeter Xu.. code:: c
1848cb2f8b1SPeter Xu
1858cb2f8b1SPeter Xu    dc->vmsd = &vmstate_kbd_isa;
1868cb2f8b1SPeter Xu
1878cb2f8b1SPeter XuThe VMState macros take care of ensuring that the device data section
1888cb2f8b1SPeter Xuis formatted portably (normally big endian) and make some compile time checks
1898cb2f8b1SPeter Xuagainst the types of the fields in the structures.
1908cb2f8b1SPeter Xu
1918cb2f8b1SPeter XuVMState macros can include other VMStateDescriptions to store substructures
1928cb2f8b1SPeter Xu(see ``VMSTATE_STRUCT_``), arrays (``VMSTATE_ARRAY_``) and variable length
1938cb2f8b1SPeter Xuarrays (``VMSTATE_VARRAY_``).  Various other macros exist for special
1948cb2f8b1SPeter Xucases.
1958cb2f8b1SPeter Xu
1968cb2f8b1SPeter XuNote that the format on the wire is still very raw; i.e. a VMSTATE_UINT32
1978cb2f8b1SPeter Xuends up with a 4 byte bigendian representation on the wire; in the future
1988cb2f8b1SPeter Xuit might be possible to use a more structured format.
1998cb2f8b1SPeter Xu
2008cb2f8b1SPeter XuLegacy way
2018cb2f8b1SPeter Xu----------
2028cb2f8b1SPeter Xu
2038cb2f8b1SPeter XuThis way is going to disappear as soon as all current users are ported to VMSTATE;
2048cb2f8b1SPeter Xualthough converting existing code can be tricky, and thus 'soon' is relative.
2058cb2f8b1SPeter Xu
2068cb2f8b1SPeter XuEach device has to register two functions, one to save the state and
2078cb2f8b1SPeter Xuanother to load the state back.
2088cb2f8b1SPeter Xu
2098cb2f8b1SPeter Xu.. code:: c
2108cb2f8b1SPeter Xu
2118cb2f8b1SPeter Xu  int register_savevm_live(const char *idstr,
2128cb2f8b1SPeter Xu                           int instance_id,
2138cb2f8b1SPeter Xu                           int version_id,
2148cb2f8b1SPeter Xu                           SaveVMHandlers *ops,
2158cb2f8b1SPeter Xu                           void *opaque);
2168cb2f8b1SPeter Xu
2178cb2f8b1SPeter XuTwo functions in the ``ops`` structure are the ``save_state``
2188cb2f8b1SPeter Xuand ``load_state`` functions.  Notice that ``load_state`` receives a version_id
2198cb2f8b1SPeter Xuparameter to know what state format is receiving.  ``save_state`` doesn't
2208cb2f8b1SPeter Xuhave a version_id parameter because it always uses the latest version.
2218cb2f8b1SPeter Xu
2228cb2f8b1SPeter XuNote that because the VMState macros still save the data in a raw
2238cb2f8b1SPeter Xuformat, in many cases it's possible to replace legacy code
2248cb2f8b1SPeter Xuwith a carefully constructed VMState description that matches the
2258cb2f8b1SPeter Xubyte layout of the existing code.
2268cb2f8b1SPeter Xu
2278cb2f8b1SPeter XuChanging migration data structures
2288cb2f8b1SPeter Xu----------------------------------
2298cb2f8b1SPeter Xu
2308cb2f8b1SPeter XuWhen we migrate a device, we save/load the state as a series
2318cb2f8b1SPeter Xuof fields.  Sometimes, due to bugs or new functionality, we need to
2328cb2f8b1SPeter Xuchange the state to store more/different information.  Changing the migration
2338cb2f8b1SPeter Xustate saved for a device can break migration compatibility unless
2348cb2f8b1SPeter Xucare is taken to use the appropriate techniques.  In general QEMU tries
2358cb2f8b1SPeter Xuto maintain forward migration compatibility (i.e. migrating from
2368cb2f8b1SPeter XuQEMU n->n+1) and there are users who benefit from backward compatibility
2378cb2f8b1SPeter Xuas well.
2388cb2f8b1SPeter Xu
2398cb2f8b1SPeter XuSubsections
2408cb2f8b1SPeter Xu-----------
2418cb2f8b1SPeter Xu
2428cb2f8b1SPeter XuThe most common structure change is adding new data, e.g. when adding
2438cb2f8b1SPeter Xua newer form of device, or adding that state that you previously
2448cb2f8b1SPeter Xuforgot to migrate.  This is best solved using a subsection.
2458cb2f8b1SPeter Xu
2468cb2f8b1SPeter XuA subsection is "like" a device vmstate, but with a particularity, it
2478cb2f8b1SPeter Xuhas a Boolean function that tells if that values are needed to be sent
2488cb2f8b1SPeter Xuor not.  If this functions returns false, the subsection is not sent.
2498cb2f8b1SPeter XuSubsections have a unique name, that is looked for on the receiving
2508cb2f8b1SPeter Xuside.
2518cb2f8b1SPeter Xu
2528cb2f8b1SPeter XuOn the receiving side, if we found a subsection for a device that we
2538cb2f8b1SPeter Xudon't understand, we just fail the migration.  If we understand all
2548cb2f8b1SPeter Xuthe subsections, then we load the state with success.  There's no check
2558cb2f8b1SPeter Xuthat a subsection is loaded, so a newer QEMU that knows about a subsection
2568cb2f8b1SPeter Xucan (with care) load a stream from an older QEMU that didn't send
2578cb2f8b1SPeter Xuthe subsection.
2588cb2f8b1SPeter Xu
2598cb2f8b1SPeter XuIf the new data is only needed in a rare case, then the subsection
2608cb2f8b1SPeter Xucan be made conditional on that case and the migration will still
2618cb2f8b1SPeter Xusucceed to older QEMUs in most cases.  This is OK for data that's
2628cb2f8b1SPeter Xucritical, but in some use cases it's preferred that the migration
2638cb2f8b1SPeter Xushould succeed even with the data missing.  To support this the
2648cb2f8b1SPeter Xusubsection can be connected to a device property and from there
2658cb2f8b1SPeter Xuto a versioned machine type.
2668cb2f8b1SPeter Xu
2678cb2f8b1SPeter XuThe 'pre_load' and 'post_load' functions on subsections are only
2688cb2f8b1SPeter Xucalled if the subsection is loaded.
2698cb2f8b1SPeter Xu
2708cb2f8b1SPeter XuOne important note is that the outer post_load() function is called "after"
2718cb2f8b1SPeter Xuloading all subsections, because a newer subsection could change the same
2728cb2f8b1SPeter Xuvalue that it uses.  A flag, and the combination of outer pre_load and
2738cb2f8b1SPeter Xupost_load can be used to detect whether a subsection was loaded, and to
2748cb2f8b1SPeter Xufall back on default behaviour when the subsection isn't present.
2758cb2f8b1SPeter Xu
2768cb2f8b1SPeter XuExample:
2778cb2f8b1SPeter Xu
2788cb2f8b1SPeter Xu.. code:: c
2798cb2f8b1SPeter Xu
2808cb2f8b1SPeter Xu  static bool ide_drive_pio_state_needed(void *opaque)
2818cb2f8b1SPeter Xu  {
2828cb2f8b1SPeter Xu      IDEState *s = opaque;
2838cb2f8b1SPeter Xu
2848cb2f8b1SPeter Xu      return ((s->status & DRQ_STAT) != 0)
2858cb2f8b1SPeter Xu          || (s->bus->error_status & BM_STATUS_PIO_RETRY);
2868cb2f8b1SPeter Xu  }
2878cb2f8b1SPeter Xu
2888cb2f8b1SPeter Xu  const VMStateDescription vmstate_ide_drive_pio_state = {
2898cb2f8b1SPeter Xu      .name = "ide_drive/pio_state",
2908cb2f8b1SPeter Xu      .version_id = 1,
2918cb2f8b1SPeter Xu      .minimum_version_id = 1,
2928cb2f8b1SPeter Xu      .pre_save = ide_drive_pio_pre_save,
2938cb2f8b1SPeter Xu      .post_load = ide_drive_pio_post_load,
2948cb2f8b1SPeter Xu      .needed = ide_drive_pio_state_needed,
2958cb2f8b1SPeter Xu      .fields = (const VMStateField[]) {
2968cb2f8b1SPeter Xu          VMSTATE_INT32(req_nb_sectors, IDEState),
2978cb2f8b1SPeter Xu          VMSTATE_VARRAY_INT32(io_buffer, IDEState, io_buffer_total_len, 1,
2988cb2f8b1SPeter Xu                               vmstate_info_uint8, uint8_t),
2998cb2f8b1SPeter Xu          VMSTATE_INT32(cur_io_buffer_offset, IDEState),
3008cb2f8b1SPeter Xu          VMSTATE_INT32(cur_io_buffer_len, IDEState),
3018cb2f8b1SPeter Xu          VMSTATE_UINT8(end_transfer_fn_idx, IDEState),
3028cb2f8b1SPeter Xu          VMSTATE_INT32(elementary_transfer_size, IDEState),
3038cb2f8b1SPeter Xu          VMSTATE_INT32(packet_transfer_size, IDEState),
3048cb2f8b1SPeter Xu          VMSTATE_END_OF_LIST()
3058cb2f8b1SPeter Xu      }
3068cb2f8b1SPeter Xu  };
3078cb2f8b1SPeter Xu
3088cb2f8b1SPeter Xu  const VMStateDescription vmstate_ide_drive = {
3098cb2f8b1SPeter Xu      .name = "ide_drive",
3108cb2f8b1SPeter Xu      .version_id = 3,
3118cb2f8b1SPeter Xu      .minimum_version_id = 0,
3128cb2f8b1SPeter Xu      .post_load = ide_drive_post_load,
3138cb2f8b1SPeter Xu      .fields = (const VMStateField[]) {
3148cb2f8b1SPeter Xu          .... several fields ....
3158cb2f8b1SPeter Xu          VMSTATE_END_OF_LIST()
3168cb2f8b1SPeter Xu      },
3178cb2f8b1SPeter Xu      .subsections = (const VMStateDescription * const []) {
3188cb2f8b1SPeter Xu          &vmstate_ide_drive_pio_state,
3198cb2f8b1SPeter Xu          NULL
3208cb2f8b1SPeter Xu      }
3218cb2f8b1SPeter Xu  };
3228cb2f8b1SPeter Xu
3238cb2f8b1SPeter XuHere we have a subsection for the pio state.  We only need to
3248cb2f8b1SPeter Xusave/send this state when we are in the middle of a pio operation
3258cb2f8b1SPeter Xu(that is what ``ide_drive_pio_state_needed()`` checks).  If DRQ_STAT is
3268cb2f8b1SPeter Xunot enabled, the values on that fields are garbage and don't need to
3278cb2f8b1SPeter Xube sent.
3288cb2f8b1SPeter Xu
3298cb2f8b1SPeter XuConnecting subsections to properties
3308cb2f8b1SPeter Xu------------------------------------
3318cb2f8b1SPeter Xu
3328cb2f8b1SPeter XuUsing a condition function that checks a 'property' to determine whether
3338cb2f8b1SPeter Xuto send a subsection allows backward migration compatibility when
3348cb2f8b1SPeter Xunew subsections are added, especially when combined with versioned
3358cb2f8b1SPeter Xumachine types.
3368cb2f8b1SPeter Xu
3378cb2f8b1SPeter XuFor example:
3388cb2f8b1SPeter Xu
3398cb2f8b1SPeter Xu   a) Add a new property using ``DEFINE_PROP_BOOL`` - e.g. support-foo and
3408cb2f8b1SPeter Xu      default it to true.
3418cb2f8b1SPeter Xu   b) Add an entry to the ``hw_compat_`` for the previous version that sets
3428cb2f8b1SPeter Xu      the property to false.
3438cb2f8b1SPeter Xu   c) Add a static bool  support_foo function that tests the property.
3448cb2f8b1SPeter Xu   d) Add a subsection with a .needed set to the support_foo function
3458cb2f8b1SPeter Xu   e) (potentially) Add an outer pre_load that sets up a default value
3468cb2f8b1SPeter Xu      for 'foo' to be used if the subsection isn't loaded.
3478cb2f8b1SPeter Xu
3488cb2f8b1SPeter XuNow that subsection will not be generated when using an older
3498cb2f8b1SPeter Xumachine type and the migration stream will be accepted by older
3508cb2f8b1SPeter XuQEMU versions.
3518cb2f8b1SPeter Xu
3528cb2f8b1SPeter XuNot sending existing elements
3538cb2f8b1SPeter Xu-----------------------------
3548cb2f8b1SPeter Xu
3558cb2f8b1SPeter XuSometimes members of the VMState are no longer needed:
3568cb2f8b1SPeter Xu
3578cb2f8b1SPeter Xu  - removing them will break migration compatibility
3588cb2f8b1SPeter Xu
3598cb2f8b1SPeter Xu  - making them version dependent and bumping the version will break backward migration
3608cb2f8b1SPeter Xu    compatibility.
3618cb2f8b1SPeter Xu
3628cb2f8b1SPeter XuAdding a dummy field into the migration stream is normally the best way to preserve
3638cb2f8b1SPeter Xucompatibility.
3648cb2f8b1SPeter Xu
3658cb2f8b1SPeter XuIf the field really does need to be removed then:
3668cb2f8b1SPeter Xu
3678cb2f8b1SPeter Xu  a) Add a new property/compatibility/function in the same way for subsections above.
3688cb2f8b1SPeter Xu  b) replace the VMSTATE macro with the _TEST version of the macro, e.g.:
3698cb2f8b1SPeter Xu
3708cb2f8b1SPeter Xu   ``VMSTATE_UINT32(foo, barstruct)``
3718cb2f8b1SPeter Xu
3728cb2f8b1SPeter Xu   becomes
3738cb2f8b1SPeter Xu
3748cb2f8b1SPeter Xu   ``VMSTATE_UINT32_TEST(foo, barstruct, pre_version_baz)``
3758cb2f8b1SPeter Xu
3768cb2f8b1SPeter Xu   Sometime in the future when we no longer care about the ancient versions these can be killed off.
3778cb2f8b1SPeter Xu   Note that for backward compatibility it's important to fill in the structure with
3788cb2f8b1SPeter Xu   data that the destination will understand.
3798cb2f8b1SPeter Xu
3808cb2f8b1SPeter XuAny difference in the predicates on the source and destination will end up
3818cb2f8b1SPeter Xuwith different fields being enabled and data being loaded into the wrong
3828cb2f8b1SPeter Xufields; for this reason conditional fields like this are very fragile.
3838cb2f8b1SPeter Xu
3848cb2f8b1SPeter XuVersions
3858cb2f8b1SPeter Xu--------
3868cb2f8b1SPeter Xu
3878cb2f8b1SPeter XuVersion numbers are intended for major incompatible changes to the
3888cb2f8b1SPeter Xumigration of a device, and using them breaks backward-migration
3898cb2f8b1SPeter Xucompatibility; in general most changes can be made by adding Subsections
3908cb2f8b1SPeter Xu(see above) or _TEST macros (see above) which won't break compatibility.
3918cb2f8b1SPeter Xu
3928cb2f8b1SPeter XuEach version is associated with a series of fields saved.  The ``save_state`` always saves
3938cb2f8b1SPeter Xuthe state as the newer version.  But ``load_state`` sometimes is able to
3948cb2f8b1SPeter Xuload state from an older version.
3958cb2f8b1SPeter Xu
3968cb2f8b1SPeter XuYou can see that there are two version fields:
3978cb2f8b1SPeter Xu
3988cb2f8b1SPeter Xu- ``version_id``: the maximum version_id supported by VMState for that device.
3998cb2f8b1SPeter Xu- ``minimum_version_id``: the minimum version_id that VMState is able to understand
4008cb2f8b1SPeter Xu  for that device.
4018cb2f8b1SPeter Xu
4028cb2f8b1SPeter XuVMState is able to read versions from minimum_version_id to version_id.
4038cb2f8b1SPeter Xu
4048cb2f8b1SPeter XuThere are *_V* forms of many ``VMSTATE_`` macros to load fields for version dependent fields,
4058cb2f8b1SPeter Xue.g.
4068cb2f8b1SPeter Xu
4078cb2f8b1SPeter Xu.. code:: c
4088cb2f8b1SPeter Xu
4098cb2f8b1SPeter Xu   VMSTATE_UINT16_V(ip_id, Slirp, 2),
4108cb2f8b1SPeter Xu
4118cb2f8b1SPeter Xuonly loads that field for versions 2 and newer.
4128cb2f8b1SPeter Xu
4138cb2f8b1SPeter XuSaving state will always create a section with the 'version_id' value
4148cb2f8b1SPeter Xuand thus can't be loaded by any older QEMU.
4158cb2f8b1SPeter Xu
4168cb2f8b1SPeter XuMassaging functions
4178cb2f8b1SPeter Xu-------------------
4188cb2f8b1SPeter Xu
4198cb2f8b1SPeter XuSometimes, it is not enough to be able to save the state directly
4208cb2f8b1SPeter Xufrom one structure, we need to fill the correct values there.  One
4218cb2f8b1SPeter Xuexample is when we are using kvm.  Before saving the cpu state, we
4228cb2f8b1SPeter Xuneed to ask kvm to copy to QEMU the state that it is using.  And the
4238cb2f8b1SPeter Xuopposite when we are loading the state, we need a way to tell kvm to
4248cb2f8b1SPeter Xuload the state for the cpu that we have just loaded from the QEMUFile.
4258cb2f8b1SPeter Xu
4268cb2f8b1SPeter XuThe functions to do that are inside a vmstate definition, and are called:
4278cb2f8b1SPeter Xu
4288cb2f8b1SPeter Xu- ``int (*pre_load)(void *opaque);``
4298cb2f8b1SPeter Xu
4308cb2f8b1SPeter Xu  This function is called before we load the state of one device.
4318cb2f8b1SPeter Xu
4328cb2f8b1SPeter Xu- ``int (*post_load)(void *opaque, int version_id);``
4338cb2f8b1SPeter Xu
4348cb2f8b1SPeter Xu  This function is called after we load the state of one device.
4358cb2f8b1SPeter Xu
4368cb2f8b1SPeter Xu- ``int (*pre_save)(void *opaque);``
4378cb2f8b1SPeter Xu
4388cb2f8b1SPeter Xu  This function is called before we save the state of one device.
4398cb2f8b1SPeter Xu
4408cb2f8b1SPeter Xu- ``int (*post_save)(void *opaque);``
4418cb2f8b1SPeter Xu
4428cb2f8b1SPeter Xu  This function is called after we save the state of one device
4438cb2f8b1SPeter Xu  (even upon failure, unless the call to pre_save returned an error).
4448cb2f8b1SPeter Xu
4458cb2f8b1SPeter XuExample: You can look at hpet.c, that uses the first three functions
4468cb2f8b1SPeter Xuto massage the state that is transferred.
4478cb2f8b1SPeter Xu
4488cb2f8b1SPeter XuThe ``VMSTATE_WITH_TMP`` macro may be useful when the migration
4498cb2f8b1SPeter Xudata doesn't match the stored device data well; it allows an
4508cb2f8b1SPeter Xuintermediate temporary structure to be populated with migration
4518cb2f8b1SPeter Xudata and then transferred to the main structure.
4528cb2f8b1SPeter Xu
453ad2b6523SBernhard BeschowIf you use memory or portio_list API functions that update memory layout outside
4548cb2f8b1SPeter Xuinitialization (i.e., in response to a guest action), this is a strong
4558cb2f8b1SPeter Xuindication that you need to call these functions in a ``post_load`` callback.
456ad2b6523SBernhard BeschowExamples of such API functions are:
4578cb2f8b1SPeter Xu
4588cb2f8b1SPeter Xu  - memory_region_add_subregion()
4598cb2f8b1SPeter Xu  - memory_region_del_subregion()
4608cb2f8b1SPeter Xu  - memory_region_set_readonly()
4618cb2f8b1SPeter Xu  - memory_region_set_nonvolatile()
4628cb2f8b1SPeter Xu  - memory_region_set_enabled()
4638cb2f8b1SPeter Xu  - memory_region_set_address()
4648cb2f8b1SPeter Xu  - memory_region_set_alias_offset()
465ad2b6523SBernhard Beschow  - portio_list_set_address()
466f165cdf1SBernhard Beschow  - portio_list_set_enabled()
4678cb2f8b1SPeter Xu
4688cb2f8b1SPeter XuIterative device migration
4698cb2f8b1SPeter Xu--------------------------
4708cb2f8b1SPeter Xu
471eef0bae3SFabiano RosasSome devices, such as RAM or certain platform devices,
4728cb2f8b1SPeter Xuhave large amounts of data that would mean that the CPUs would be
4738cb2f8b1SPeter Xupaused for too long if they were sent in one section.  For these
4748cb2f8b1SPeter Xudevices an *iterative* approach is taken.
4758cb2f8b1SPeter Xu
4768cb2f8b1SPeter XuThe iterative devices generally don't use VMState macros
4778cb2f8b1SPeter Xu(although it may be possible in some cases) and instead use
4788cb2f8b1SPeter Xuqemu_put_*/qemu_get_* macros to read/write data to the stream.  Specialist
4798cb2f8b1SPeter Xuversions exist for high bandwidth IO.
4808cb2f8b1SPeter Xu
4818cb2f8b1SPeter Xu
4828cb2f8b1SPeter XuAn iterative device must provide:
4838cb2f8b1SPeter Xu
4848cb2f8b1SPeter Xu  - A ``save_setup`` function that initialises the data structures and
4858cb2f8b1SPeter Xu    transmits a first section containing information on the device.  In the
4868cb2f8b1SPeter Xu    case of RAM this transmits a list of RAMBlocks and sizes.
4878cb2f8b1SPeter Xu
4888cb2f8b1SPeter Xu  - A ``load_setup`` function that initialises the data structures on the
4898cb2f8b1SPeter Xu    destination.
4908cb2f8b1SPeter Xu
4918cb2f8b1SPeter Xu  - A ``state_pending_exact`` function that indicates how much more
4928cb2f8b1SPeter Xu    data we must save.  The core migration code will use this to
4938cb2f8b1SPeter Xu    determine when to pause the CPUs and complete the migration.
4948cb2f8b1SPeter Xu
4958cb2f8b1SPeter Xu  - A ``state_pending_estimate`` function that indicates how much more
4968cb2f8b1SPeter Xu    data we must save.  When the estimated amount is smaller than the
4978cb2f8b1SPeter Xu    threshold, we call ``state_pending_exact``.
4988cb2f8b1SPeter Xu
4998cb2f8b1SPeter Xu  - A ``save_live_iterate`` function should send a chunk of data until
5008cb2f8b1SPeter Xu    the point that stream bandwidth limits tell it to stop.  Each call
5018cb2f8b1SPeter Xu    generates one section.
5028cb2f8b1SPeter Xu
5038cb2f8b1SPeter Xu  - A ``save_live_complete_precopy`` function that must transmit the
5048cb2f8b1SPeter Xu    last section for the device containing any remaining data.
5058cb2f8b1SPeter Xu
5068cb2f8b1SPeter Xu  - A ``load_state`` function used to load sections generated by
5078cb2f8b1SPeter Xu    any of the save functions that generate sections.
5088cb2f8b1SPeter Xu
5098cb2f8b1SPeter Xu  - ``cleanup`` functions for both save and load that are called
5108cb2f8b1SPeter Xu    at the end of migration.
5118cb2f8b1SPeter Xu
5128cb2f8b1SPeter XuNote that the contents of the sections for iterative migration tend
5138cb2f8b1SPeter Xuto be open-coded by the devices; care should be taken in parsing
5148cb2f8b1SPeter Xuthe results and structuring the stream to make them easy to validate.
5158cb2f8b1SPeter Xu
5168cb2f8b1SPeter XuDevice ordering
5178cb2f8b1SPeter Xu---------------
5188cb2f8b1SPeter Xu
5198cb2f8b1SPeter XuThere are cases in which the ordering of device loading matters; for
5208cb2f8b1SPeter Xuexample in some systems where a device may assert an interrupt during loading,
5218cb2f8b1SPeter Xuif the interrupt controller is loaded later then it might lose the state.
5228cb2f8b1SPeter Xu
5238cb2f8b1SPeter XuSome ordering is implicitly provided by the order in which the machine
5248cb2f8b1SPeter Xudefinition creates devices, however this is somewhat fragile.
5258cb2f8b1SPeter Xu
5268cb2f8b1SPeter XuThe ``MigrationPriority`` enum provides a means of explicitly enforcing
5278cb2f8b1SPeter Xuordering.  Numerically higher priorities are loaded earlier.
5288cb2f8b1SPeter XuThe priority is set by setting the ``priority`` field of the top level
5298cb2f8b1SPeter Xu``VMStateDescription`` for the device.
5308cb2f8b1SPeter Xu
5318cb2f8b1SPeter XuStream structure
5328cb2f8b1SPeter Xu================
5338cb2f8b1SPeter Xu
5348cb2f8b1SPeter XuThe stream tries to be word and endian agnostic, allowing migration between hosts
5358cb2f8b1SPeter Xuof different characteristics running the same VM.
5368cb2f8b1SPeter Xu
5378cb2f8b1SPeter Xu  - Header
5388cb2f8b1SPeter Xu
5398cb2f8b1SPeter Xu    - Magic
5408cb2f8b1SPeter Xu    - Version
5418cb2f8b1SPeter Xu    - VM configuration section
5428cb2f8b1SPeter Xu
5438cb2f8b1SPeter Xu       - Machine type
5448cb2f8b1SPeter Xu       - Target page bits
5458cb2f8b1SPeter Xu  - List of sections
5468cb2f8b1SPeter Xu    Each section contains a device, or one iteration of a device save.
5478cb2f8b1SPeter Xu
5488cb2f8b1SPeter Xu    - section type
5498cb2f8b1SPeter Xu    - section id
5508cb2f8b1SPeter Xu    - ID string (First section of each device)
5518cb2f8b1SPeter Xu    - instance id (First section of each device)
5528cb2f8b1SPeter Xu    - version id (First section of each device)
5538cb2f8b1SPeter Xu    - <device data>
5548cb2f8b1SPeter Xu    - Footer mark
5558cb2f8b1SPeter Xu  - EOF mark
5568cb2f8b1SPeter Xu  - VM Description structure
5578cb2f8b1SPeter Xu    Consisting of a JSON description of the contents for analysis only
5588cb2f8b1SPeter Xu
5598cb2f8b1SPeter XuThe ``device data`` in each section consists of the data produced
5608cb2f8b1SPeter Xuby the code described above.  For non-iterative devices they have a single
5618cb2f8b1SPeter Xusection; iterative devices have an initial and last section and a set
5628cb2f8b1SPeter Xuof parts in between.
5638cb2f8b1SPeter XuNote that there is very little checking by the common code of the integrity
5648cb2f8b1SPeter Xuof the ``device data`` contents, that's up to the devices themselves.
5658cb2f8b1SPeter XuThe ``footer mark`` provides a little bit of protection for the case where
5668cb2f8b1SPeter Xuthe receiving side reads more or less data than expected.
5678cb2f8b1SPeter Xu
5688cb2f8b1SPeter XuThe ``ID string`` is normally unique, having been formed from a bus name
5698cb2f8b1SPeter Xuand device address, PCI devices and storage devices hung off PCI controllers
5708cb2f8b1SPeter Xufit this pattern well.  Some devices are fixed single instances (e.g. "pc-ram").
5718cb2f8b1SPeter XuOthers (especially either older devices or system devices which for
5728cb2f8b1SPeter Xusome reason don't have a bus concept) make use of the ``instance id``
5738cb2f8b1SPeter Xufor otherwise identically named devices.
5748cb2f8b1SPeter Xu
5758cb2f8b1SPeter XuReturn path
5768cb2f8b1SPeter Xu-----------
5778cb2f8b1SPeter Xu
5788cb2f8b1SPeter XuOnly a unidirectional stream is required for normal migration, however a
5798cb2f8b1SPeter Xu``return path`` can be created when bidirectional communication is desired.
5808cb2f8b1SPeter XuThis is primarily used by postcopy, but is also used to return a success
5818cb2f8b1SPeter Xuflag to the source at the end of migration.
5828cb2f8b1SPeter Xu
5838cb2f8b1SPeter Xu``qemu_file_get_return_path(QEMUFile* fwdpath)`` gives the QEMUFile* for the return
5848cb2f8b1SPeter Xupath.
5858cb2f8b1SPeter Xu
5868cb2f8b1SPeter Xu  Source side
5878cb2f8b1SPeter Xu
5888cb2f8b1SPeter Xu     Forward path - written by migration thread
5898cb2f8b1SPeter Xu     Return path  - opened by main thread, read by return-path thread
5908cb2f8b1SPeter Xu
5918cb2f8b1SPeter Xu  Destination side
5928cb2f8b1SPeter Xu
5938cb2f8b1SPeter Xu     Forward path - read by main thread
5948cb2f8b1SPeter Xu     Return path  - opened by main thread, written by main thread AND postcopy
5958cb2f8b1SPeter Xu     thread (protected by rp_mutex)
5968cb2f8b1SPeter Xu
597