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