xref: /openbmc/qemu/migration/savevm.c (revision 7d405b2f)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2009-2015 Red Hat Inc
6  *
7  * Authors:
8  *  Juan Quintela <quintela@redhat.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28 
29 #include "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "hw/xen/xen.h"
32 #include "net/net.h"
33 #include "migration.h"
34 #include "migration/snapshot.h"
35 #include "migration/misc.h"
36 #include "migration/register.h"
37 #include "migration/global_state.h"
38 #include "ram.h"
39 #include "qemu-file-channel.h"
40 #include "qemu-file.h"
41 #include "savevm.h"
42 #include "postcopy-ram.h"
43 #include "qapi/error.h"
44 #include "qapi/qapi-commands-migration.h"
45 #include "qapi/qapi-commands-misc.h"
46 #include "qapi/qmp/qerror.h"
47 #include "qemu/error-report.h"
48 #include "sysemu/cpus.h"
49 #include "exec/memory.h"
50 #include "exec/target_page.h"
51 #include "trace.h"
52 #include "qemu/iov.h"
53 #include "block/snapshot.h"
54 #include "qemu/cutils.h"
55 #include "io/channel-buffer.h"
56 #include "io/channel-file.h"
57 #include "sysemu/replay.h"
58 
59 #ifndef ETH_P_RARP
60 #define ETH_P_RARP 0x8035
61 #endif
62 #define ARP_HTYPE_ETH 0x0001
63 #define ARP_PTYPE_IP 0x0800
64 #define ARP_OP_REQUEST_REV 0x3
65 
66 const unsigned int postcopy_ram_discard_version = 0;
67 
68 /* Subcommands for QEMU_VM_COMMAND */
69 enum qemu_vm_cmd {
70     MIG_CMD_INVALID = 0,   /* Must be 0 */
71     MIG_CMD_OPEN_RETURN_PATH,  /* Tell the dest to open the Return path */
72     MIG_CMD_PING,              /* Request a PONG on the RP */
73 
74     MIG_CMD_POSTCOPY_ADVISE,       /* Prior to any page transfers, just
75                                       warn we might want to do PC */
76     MIG_CMD_POSTCOPY_LISTEN,       /* Start listening for incoming
77                                       pages as it's running. */
78     MIG_CMD_POSTCOPY_RUN,          /* Start execution */
79 
80     MIG_CMD_POSTCOPY_RAM_DISCARD,  /* A list of pages to discard that
81                                       were previously sent during
82                                       precopy but are dirty. */
83     MIG_CMD_POSTCOPY_RESUME,       /* resume postcopy on dest */
84     MIG_CMD_PACKAGED,          /* Send a wrapped stream within this stream */
85     MIG_CMD_RECV_BITMAP,       /* Request for recved bitmap on dst */
86     MIG_CMD_MAX
87 };
88 
89 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
90 static struct mig_cmd_args {
91     ssize_t     len; /* -1 = variable */
92     const char *name;
93 } mig_cmd_args[] = {
94     [MIG_CMD_INVALID]          = { .len = -1, .name = "INVALID" },
95     [MIG_CMD_OPEN_RETURN_PATH] = { .len =  0, .name = "OPEN_RETURN_PATH" },
96     [MIG_CMD_PING]             = { .len = sizeof(uint32_t), .name = "PING" },
97     [MIG_CMD_POSTCOPY_ADVISE]  = { .len = -1, .name = "POSTCOPY_ADVISE" },
98     [MIG_CMD_POSTCOPY_LISTEN]  = { .len =  0, .name = "POSTCOPY_LISTEN" },
99     [MIG_CMD_POSTCOPY_RUN]     = { .len =  0, .name = "POSTCOPY_RUN" },
100     [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
101                                    .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
102     [MIG_CMD_POSTCOPY_RESUME]  = { .len =  0, .name = "POSTCOPY_RESUME" },
103     [MIG_CMD_PACKAGED]         = { .len =  4, .name = "PACKAGED" },
104     [MIG_CMD_RECV_BITMAP]      = { .len = -1, .name = "RECV_BITMAP" },
105     [MIG_CMD_MAX]              = { .len = -1, .name = "MAX" },
106 };
107 
108 /* Note for MIG_CMD_POSTCOPY_ADVISE:
109  * The format of arguments is depending on postcopy mode:
110  * - postcopy RAM only
111  *   uint64_t host page size
112  *   uint64_t taget page size
113  *
114  * - postcopy RAM and postcopy dirty bitmaps
115  *   format is the same as for postcopy RAM only
116  *
117  * - postcopy dirty bitmaps only
118  *   Nothing. Command length field is 0.
119  *
120  * Be careful: adding a new postcopy entity with some other parameters should
121  * not break format self-description ability. Good way is to introduce some
122  * generic extendable format with an exception for two old entities.
123  */
124 
125 static int announce_self_create(uint8_t *buf,
126                                 uint8_t *mac_addr)
127 {
128     /* Ethernet header. */
129     memset(buf, 0xff, 6);         /* destination MAC addr */
130     memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
131     *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
132 
133     /* RARP header. */
134     *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
135     *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
136     *(buf + 18) = 6; /* hardware addr length (ethernet) */
137     *(buf + 19) = 4; /* protocol addr length (IPv4) */
138     *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
139     memcpy(buf + 22, mac_addr, 6); /* source hw addr */
140     memset(buf + 28, 0x00, 4);     /* source protocol addr */
141     memcpy(buf + 32, mac_addr, 6); /* target hw addr */
142     memset(buf + 38, 0x00, 4);     /* target protocol addr */
143 
144     /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
145     memset(buf + 42, 0x00, 18);
146 
147     return 60; /* len (FCS will be added by hardware) */
148 }
149 
150 static void qemu_announce_self_iter(NICState *nic, void *opaque)
151 {
152     uint8_t buf[60];
153     int len;
154 
155     trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
156     len = announce_self_create(buf, nic->conf->macaddr.a);
157 
158     qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
159 }
160 
161 
162 static void qemu_announce_self_once(void *opaque)
163 {
164     static int count = SELF_ANNOUNCE_ROUNDS;
165     QEMUTimer *timer = *(QEMUTimer **)opaque;
166 
167     qemu_foreach_nic(qemu_announce_self_iter, NULL);
168 
169     if (--count) {
170         /* delay 50ms, 150ms, 250ms, ... */
171         timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
172                   self_announce_delay(count));
173     } else {
174             timer_del(timer);
175             timer_free(timer);
176     }
177 }
178 
179 void qemu_announce_self(void)
180 {
181     static QEMUTimer *timer;
182     timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
183     qemu_announce_self_once(&timer);
184 }
185 
186 /***********************************************************/
187 /* savevm/loadvm support */
188 
189 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
190                                    int64_t pos)
191 {
192     int ret;
193     QEMUIOVector qiov;
194 
195     qemu_iovec_init_external(&qiov, iov, iovcnt);
196     ret = bdrv_writev_vmstate(opaque, &qiov, pos);
197     if (ret < 0) {
198         return ret;
199     }
200 
201     return qiov.size;
202 }
203 
204 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
205                                 size_t size)
206 {
207     return bdrv_load_vmstate(opaque, buf, pos, size);
208 }
209 
210 static int bdrv_fclose(void *opaque)
211 {
212     return bdrv_flush(opaque);
213 }
214 
215 static const QEMUFileOps bdrv_read_ops = {
216     .get_buffer = block_get_buffer,
217     .close =      bdrv_fclose
218 };
219 
220 static const QEMUFileOps bdrv_write_ops = {
221     .writev_buffer  = block_writev_buffer,
222     .close          = bdrv_fclose
223 };
224 
225 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
226 {
227     if (is_writable) {
228         return qemu_fopen_ops(bs, &bdrv_write_ops);
229     }
230     return qemu_fopen_ops(bs, &bdrv_read_ops);
231 }
232 
233 
234 /* QEMUFile timer support.
235  * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
236  */
237 
238 void timer_put(QEMUFile *f, QEMUTimer *ts)
239 {
240     uint64_t expire_time;
241 
242     expire_time = timer_expire_time_ns(ts);
243     qemu_put_be64(f, expire_time);
244 }
245 
246 void timer_get(QEMUFile *f, QEMUTimer *ts)
247 {
248     uint64_t expire_time;
249 
250     expire_time = qemu_get_be64(f);
251     if (expire_time != -1) {
252         timer_mod_ns(ts, expire_time);
253     } else {
254         timer_del(ts);
255     }
256 }
257 
258 
259 /* VMState timer support.
260  * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
261  */
262 
263 static int get_timer(QEMUFile *f, void *pv, size_t size, VMStateField *field)
264 {
265     QEMUTimer *v = pv;
266     timer_get(f, v);
267     return 0;
268 }
269 
270 static int put_timer(QEMUFile *f, void *pv, size_t size, VMStateField *field,
271                      QJSON *vmdesc)
272 {
273     QEMUTimer *v = pv;
274     timer_put(f, v);
275 
276     return 0;
277 }
278 
279 const VMStateInfo vmstate_info_timer = {
280     .name = "timer",
281     .get  = get_timer,
282     .put  = put_timer,
283 };
284 
285 
286 typedef struct CompatEntry {
287     char idstr[256];
288     int instance_id;
289 } CompatEntry;
290 
291 typedef struct SaveStateEntry {
292     QTAILQ_ENTRY(SaveStateEntry) entry;
293     char idstr[256];
294     int instance_id;
295     int alias_id;
296     int version_id;
297     /* version id read from the stream */
298     int load_version_id;
299     int section_id;
300     /* section id read from the stream */
301     int load_section_id;
302     SaveVMHandlers *ops;
303     const VMStateDescription *vmsd;
304     void *opaque;
305     CompatEntry *compat;
306     int is_ram;
307 } SaveStateEntry;
308 
309 typedef struct SaveState {
310     QTAILQ_HEAD(, SaveStateEntry) handlers;
311     int global_section_id;
312     uint32_t len;
313     const char *name;
314     uint32_t target_page_bits;
315 } SaveState;
316 
317 static SaveState savevm_state = {
318     .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
319     .global_section_id = 0,
320 };
321 
322 static int configuration_pre_save(void *opaque)
323 {
324     SaveState *state = opaque;
325     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
326 
327     state->len = strlen(current_name);
328     state->name = current_name;
329     state->target_page_bits = qemu_target_page_bits();
330 
331     return 0;
332 }
333 
334 static int configuration_pre_load(void *opaque)
335 {
336     SaveState *state = opaque;
337 
338     /* If there is no target-page-bits subsection it means the source
339      * predates the variable-target-page-bits support and is using the
340      * minimum possible value for this CPU.
341      */
342     state->target_page_bits = qemu_target_page_bits_min();
343     return 0;
344 }
345 
346 static int configuration_post_load(void *opaque, int version_id)
347 {
348     SaveState *state = opaque;
349     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
350 
351     if (strncmp(state->name, current_name, state->len) != 0) {
352         error_report("Machine type received is '%.*s' and local is '%s'",
353                      (int) state->len, state->name, current_name);
354         return -EINVAL;
355     }
356 
357     if (state->target_page_bits != qemu_target_page_bits()) {
358         error_report("Received TARGET_PAGE_BITS is %d but local is %d",
359                      state->target_page_bits, qemu_target_page_bits());
360         return -EINVAL;
361     }
362 
363     return 0;
364 }
365 
366 /* The target-page-bits subsection is present only if the
367  * target page size is not the same as the default (ie the
368  * minimum page size for a variable-page-size guest CPU).
369  * If it is present then it contains the actual target page
370  * bits for the machine, and migration will fail if the
371  * two ends don't agree about it.
372  */
373 static bool vmstate_target_page_bits_needed(void *opaque)
374 {
375     return qemu_target_page_bits()
376         > qemu_target_page_bits_min();
377 }
378 
379 static const VMStateDescription vmstate_target_page_bits = {
380     .name = "configuration/target-page-bits",
381     .version_id = 1,
382     .minimum_version_id = 1,
383     .needed = vmstate_target_page_bits_needed,
384     .fields = (VMStateField[]) {
385         VMSTATE_UINT32(target_page_bits, SaveState),
386         VMSTATE_END_OF_LIST()
387     }
388 };
389 
390 static const VMStateDescription vmstate_configuration = {
391     .name = "configuration",
392     .version_id = 1,
393     .pre_load = configuration_pre_load,
394     .post_load = configuration_post_load,
395     .pre_save = configuration_pre_save,
396     .fields = (VMStateField[]) {
397         VMSTATE_UINT32(len, SaveState),
398         VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
399         VMSTATE_END_OF_LIST()
400     },
401     .subsections = (const VMStateDescription*[]) {
402         &vmstate_target_page_bits,
403         NULL
404     }
405 };
406 
407 static void dump_vmstate_vmsd(FILE *out_file,
408                               const VMStateDescription *vmsd, int indent,
409                               bool is_subsection);
410 
411 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
412                               int indent)
413 {
414     fprintf(out_file, "%*s{\n", indent, "");
415     indent += 2;
416     fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
417     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
418             field->version_id);
419     fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
420             field->field_exists ? "true" : "false");
421     fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
422     if (field->vmsd != NULL) {
423         fprintf(out_file, ",\n");
424         dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
425     }
426     fprintf(out_file, "\n%*s}", indent - 2, "");
427 }
428 
429 static void dump_vmstate_vmss(FILE *out_file,
430                               const VMStateDescription **subsection,
431                               int indent)
432 {
433     if (*subsection != NULL) {
434         dump_vmstate_vmsd(out_file, *subsection, indent, true);
435     }
436 }
437 
438 static void dump_vmstate_vmsd(FILE *out_file,
439                               const VMStateDescription *vmsd, int indent,
440                               bool is_subsection)
441 {
442     if (is_subsection) {
443         fprintf(out_file, "%*s{\n", indent, "");
444     } else {
445         fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
446     }
447     indent += 2;
448     fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
449     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
450             vmsd->version_id);
451     fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
452             vmsd->minimum_version_id);
453     if (vmsd->fields != NULL) {
454         const VMStateField *field = vmsd->fields;
455         bool first;
456 
457         fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
458         first = true;
459         while (field->name != NULL) {
460             if (field->flags & VMS_MUST_EXIST) {
461                 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
462                 field++;
463                 continue;
464             }
465             if (!first) {
466                 fprintf(out_file, ",\n");
467             }
468             dump_vmstate_vmsf(out_file, field, indent + 2);
469             field++;
470             first = false;
471         }
472         fprintf(out_file, "\n%*s]", indent, "");
473     }
474     if (vmsd->subsections != NULL) {
475         const VMStateDescription **subsection = vmsd->subsections;
476         bool first;
477 
478         fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
479         first = true;
480         while (*subsection != NULL) {
481             if (!first) {
482                 fprintf(out_file, ",\n");
483             }
484             dump_vmstate_vmss(out_file, subsection, indent + 2);
485             subsection++;
486             first = false;
487         }
488         fprintf(out_file, "\n%*s]", indent, "");
489     }
490     fprintf(out_file, "\n%*s}", indent - 2, "");
491 }
492 
493 static void dump_machine_type(FILE *out_file)
494 {
495     MachineClass *mc;
496 
497     mc = MACHINE_GET_CLASS(current_machine);
498 
499     fprintf(out_file, "  \"vmschkmachine\": {\n");
500     fprintf(out_file, "    \"Name\": \"%s\"\n", mc->name);
501     fprintf(out_file, "  },\n");
502 }
503 
504 void dump_vmstate_json_to_file(FILE *out_file)
505 {
506     GSList *list, *elt;
507     bool first;
508 
509     fprintf(out_file, "{\n");
510     dump_machine_type(out_file);
511 
512     first = true;
513     list = object_class_get_list(TYPE_DEVICE, true);
514     for (elt = list; elt; elt = elt->next) {
515         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
516                                              TYPE_DEVICE);
517         const char *name;
518         int indent = 2;
519 
520         if (!dc->vmsd) {
521             continue;
522         }
523 
524         if (!first) {
525             fprintf(out_file, ",\n");
526         }
527         name = object_class_get_name(OBJECT_CLASS(dc));
528         fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
529         indent += 2;
530         fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
531         fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
532                 dc->vmsd->version_id);
533         fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
534                 dc->vmsd->minimum_version_id);
535 
536         dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
537 
538         fprintf(out_file, "\n%*s}", indent - 2, "");
539         first = false;
540     }
541     fprintf(out_file, "\n}\n");
542     fclose(out_file);
543 }
544 
545 static int calculate_new_instance_id(const char *idstr)
546 {
547     SaveStateEntry *se;
548     int instance_id = 0;
549 
550     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
551         if (strcmp(idstr, se->idstr) == 0
552             && instance_id <= se->instance_id) {
553             instance_id = se->instance_id + 1;
554         }
555     }
556     return instance_id;
557 }
558 
559 static int calculate_compat_instance_id(const char *idstr)
560 {
561     SaveStateEntry *se;
562     int instance_id = 0;
563 
564     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
565         if (!se->compat) {
566             continue;
567         }
568 
569         if (strcmp(idstr, se->compat->idstr) == 0
570             && instance_id <= se->compat->instance_id) {
571             instance_id = se->compat->instance_id + 1;
572         }
573     }
574     return instance_id;
575 }
576 
577 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
578 {
579     if (se->vmsd) {
580         return se->vmsd->priority;
581     }
582     return MIG_PRI_DEFAULT;
583 }
584 
585 static void savevm_state_handler_insert(SaveStateEntry *nse)
586 {
587     MigrationPriority priority = save_state_priority(nse);
588     SaveStateEntry *se;
589 
590     assert(priority <= MIG_PRI_MAX);
591 
592     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
593         if (save_state_priority(se) < priority) {
594             break;
595         }
596     }
597 
598     if (se) {
599         QTAILQ_INSERT_BEFORE(se, nse, entry);
600     } else {
601         QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
602     }
603 }
604 
605 /* TODO: Individual devices generally have very little idea about the rest
606    of the system, so instance_id should be removed/replaced.
607    Meanwhile pass -1 as instance_id if you do not already have a clearly
608    distinguishing id for all instances of your device class. */
609 int register_savevm_live(DeviceState *dev,
610                          const char *idstr,
611                          int instance_id,
612                          int version_id,
613                          SaveVMHandlers *ops,
614                          void *opaque)
615 {
616     SaveStateEntry *se;
617 
618     se = g_new0(SaveStateEntry, 1);
619     se->version_id = version_id;
620     se->section_id = savevm_state.global_section_id++;
621     se->ops = ops;
622     se->opaque = opaque;
623     se->vmsd = NULL;
624     /* if this is a live_savem then set is_ram */
625     if (ops->save_setup != NULL) {
626         se->is_ram = 1;
627     }
628 
629     if (dev) {
630         char *id = qdev_get_dev_path(dev);
631         if (id) {
632             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
633                 sizeof(se->idstr)) {
634                 error_report("Path too long for VMState (%s)", id);
635                 g_free(id);
636                 g_free(se);
637 
638                 return -1;
639             }
640             g_free(id);
641 
642             se->compat = g_new0(CompatEntry, 1);
643             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
644             se->compat->instance_id = instance_id == -1 ?
645                          calculate_compat_instance_id(idstr) : instance_id;
646             instance_id = -1;
647         }
648     }
649     pstrcat(se->idstr, sizeof(se->idstr), idstr);
650 
651     if (instance_id == -1) {
652         se->instance_id = calculate_new_instance_id(se->idstr);
653     } else {
654         se->instance_id = instance_id;
655     }
656     assert(!se->compat || se->instance_id == 0);
657     savevm_state_handler_insert(se);
658     return 0;
659 }
660 
661 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
662 {
663     SaveStateEntry *se, *new_se;
664     char id[256] = "";
665 
666     if (dev) {
667         char *path = qdev_get_dev_path(dev);
668         if (path) {
669             pstrcpy(id, sizeof(id), path);
670             pstrcat(id, sizeof(id), "/");
671             g_free(path);
672         }
673     }
674     pstrcat(id, sizeof(id), idstr);
675 
676     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
677         if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
678             QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
679             g_free(se->compat);
680             g_free(se);
681         }
682     }
683 }
684 
685 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
686                                    const VMStateDescription *vmsd,
687                                    void *opaque, int alias_id,
688                                    int required_for_version,
689                                    Error **errp)
690 {
691     SaveStateEntry *se;
692 
693     /* If this triggers, alias support can be dropped for the vmsd. */
694     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
695 
696     se = g_new0(SaveStateEntry, 1);
697     se->version_id = vmsd->version_id;
698     se->section_id = savevm_state.global_section_id++;
699     se->opaque = opaque;
700     se->vmsd = vmsd;
701     se->alias_id = alias_id;
702 
703     if (dev) {
704         char *id = qdev_get_dev_path(dev);
705         if (id) {
706             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
707                 sizeof(se->idstr)) {
708                 error_setg(errp, "Path too long for VMState (%s)", id);
709                 g_free(id);
710                 g_free(se);
711 
712                 return -1;
713             }
714             g_free(id);
715 
716             se->compat = g_new0(CompatEntry, 1);
717             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
718             se->compat->instance_id = instance_id == -1 ?
719                          calculate_compat_instance_id(vmsd->name) : instance_id;
720             instance_id = -1;
721         }
722     }
723     pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
724 
725     if (instance_id == -1) {
726         se->instance_id = calculate_new_instance_id(se->idstr);
727     } else {
728         se->instance_id = instance_id;
729     }
730     assert(!se->compat || se->instance_id == 0);
731     savevm_state_handler_insert(se);
732     return 0;
733 }
734 
735 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
736                         void *opaque)
737 {
738     SaveStateEntry *se, *new_se;
739 
740     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
741         if (se->vmsd == vmsd && se->opaque == opaque) {
742             QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
743             g_free(se->compat);
744             g_free(se);
745         }
746     }
747 }
748 
749 static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
750 {
751     trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
752     if (!se->vmsd) {         /* Old style */
753         return se->ops->load_state(f, se->opaque, se->load_version_id);
754     }
755     return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
756 }
757 
758 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
759 {
760     int64_t old_offset, size;
761 
762     old_offset = qemu_ftell_fast(f);
763     se->ops->save_state(f, se->opaque);
764     size = qemu_ftell_fast(f) - old_offset;
765 
766     if (vmdesc) {
767         json_prop_int(vmdesc, "size", size);
768         json_start_array(vmdesc, "fields");
769         json_start_object(vmdesc, NULL);
770         json_prop_str(vmdesc, "name", "data");
771         json_prop_int(vmdesc, "size", size);
772         json_prop_str(vmdesc, "type", "buffer");
773         json_end_object(vmdesc);
774         json_end_array(vmdesc);
775     }
776 }
777 
778 static int vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
779 {
780     trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
781     if (!se->vmsd) {
782         vmstate_save_old_style(f, se, vmdesc);
783         return 0;
784     }
785     return vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
786 }
787 
788 /*
789  * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
790  */
791 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
792                                 uint8_t section_type)
793 {
794     qemu_put_byte(f, section_type);
795     qemu_put_be32(f, se->section_id);
796 
797     if (section_type == QEMU_VM_SECTION_FULL ||
798         section_type == QEMU_VM_SECTION_START) {
799         /* ID string */
800         size_t len = strlen(se->idstr);
801         qemu_put_byte(f, len);
802         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
803 
804         qemu_put_be32(f, se->instance_id);
805         qemu_put_be32(f, se->version_id);
806     }
807 }
808 
809 /*
810  * Write a footer onto device sections that catches cases misformatted device
811  * sections.
812  */
813 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
814 {
815     if (migrate_get_current()->send_section_footer) {
816         qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
817         qemu_put_be32(f, se->section_id);
818     }
819 }
820 
821 /**
822  * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
823  *                           command and associated data.
824  *
825  * @f: File to send command on
826  * @command: Command type to send
827  * @len: Length of associated data
828  * @data: Data associated with command.
829  */
830 static void qemu_savevm_command_send(QEMUFile *f,
831                                      enum qemu_vm_cmd command,
832                                      uint16_t len,
833                                      uint8_t *data)
834 {
835     trace_savevm_command_send(command, len);
836     qemu_put_byte(f, QEMU_VM_COMMAND);
837     qemu_put_be16(f, (uint16_t)command);
838     qemu_put_be16(f, len);
839     qemu_put_buffer(f, data, len);
840     qemu_fflush(f);
841 }
842 
843 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
844 {
845     uint32_t buf;
846 
847     trace_savevm_send_ping(value);
848     buf = cpu_to_be32(value);
849     qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
850 }
851 
852 void qemu_savevm_send_open_return_path(QEMUFile *f)
853 {
854     trace_savevm_send_open_return_path();
855     qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
856 }
857 
858 /* We have a buffer of data to send; we don't want that all to be loaded
859  * by the command itself, so the command contains just the length of the
860  * extra buffer that we then send straight after it.
861  * TODO: Must be a better way to organise that
862  *
863  * Returns:
864  *    0 on success
865  *    -ve on error
866  */
867 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
868 {
869     uint32_t tmp;
870 
871     if (len > MAX_VM_CMD_PACKAGED_SIZE) {
872         error_report("%s: Unreasonably large packaged state: %zu",
873                      __func__, len);
874         return -1;
875     }
876 
877     tmp = cpu_to_be32(len);
878 
879     trace_qemu_savevm_send_packaged();
880     qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
881 
882     qemu_put_buffer(f, buf, len);
883 
884     return 0;
885 }
886 
887 /* Send prior to any postcopy transfer */
888 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
889 {
890     if (migrate_postcopy_ram()) {
891         uint64_t tmp[2];
892         tmp[0] = cpu_to_be64(ram_pagesize_summary());
893         tmp[1] = cpu_to_be64(qemu_target_page_size());
894 
895         trace_qemu_savevm_send_postcopy_advise();
896         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
897                                  16, (uint8_t *)tmp);
898     } else {
899         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
900     }
901 }
902 
903 /* Sent prior to starting the destination running in postcopy, discard pages
904  * that have already been sent but redirtied on the source.
905  * CMD_POSTCOPY_RAM_DISCARD consist of:
906  *      byte   version (0)
907  *      byte   Length of name field (not including 0)
908  *  n x byte   RAM block name
909  *      byte   0 terminator (just for safety)
910  *  n x        Byte ranges within the named RAMBlock
911  *      be64   Start of the range
912  *      be64   Length
913  *
914  *  name:  RAMBlock name that these entries are part of
915  *  len: Number of page entries
916  *  start_list: 'len' addresses
917  *  length_list: 'len' addresses
918  *
919  */
920 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
921                                            uint16_t len,
922                                            uint64_t *start_list,
923                                            uint64_t *length_list)
924 {
925     uint8_t *buf;
926     uint16_t tmplen;
927     uint16_t t;
928     size_t name_len = strlen(name);
929 
930     trace_qemu_savevm_send_postcopy_ram_discard(name, len);
931     assert(name_len < 256);
932     buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
933     buf[0] = postcopy_ram_discard_version;
934     buf[1] = name_len;
935     memcpy(buf + 2, name, name_len);
936     tmplen = 2 + name_len;
937     buf[tmplen++] = '\0';
938 
939     for (t = 0; t < len; t++) {
940         stq_be_p(buf + tmplen, start_list[t]);
941         tmplen += 8;
942         stq_be_p(buf + tmplen, length_list[t]);
943         tmplen += 8;
944     }
945     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
946     g_free(buf);
947 }
948 
949 /* Get the destination into a state where it can receive postcopy data. */
950 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
951 {
952     trace_savevm_send_postcopy_listen();
953     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
954 }
955 
956 /* Kick the destination into running */
957 void qemu_savevm_send_postcopy_run(QEMUFile *f)
958 {
959     trace_savevm_send_postcopy_run();
960     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
961 }
962 
963 void qemu_savevm_send_postcopy_resume(QEMUFile *f)
964 {
965     trace_savevm_send_postcopy_resume();
966     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
967 }
968 
969 void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
970 {
971     size_t len;
972     char buf[256];
973 
974     trace_savevm_send_recv_bitmap(block_name);
975 
976     buf[0] = len = strlen(block_name);
977     memcpy(buf + 1, block_name, len);
978 
979     qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
980 }
981 
982 bool qemu_savevm_state_blocked(Error **errp)
983 {
984     SaveStateEntry *se;
985 
986     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
987         if (se->vmsd && se->vmsd->unmigratable) {
988             error_setg(errp, "State blocked by non-migratable device '%s'",
989                        se->idstr);
990             return true;
991         }
992     }
993     return false;
994 }
995 
996 void qemu_savevm_state_header(QEMUFile *f)
997 {
998     trace_savevm_state_header();
999     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1000     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1001 
1002     if (migrate_get_current()->send_configuration) {
1003         qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1004         vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
1005     }
1006 }
1007 
1008 void qemu_savevm_state_setup(QEMUFile *f)
1009 {
1010     SaveStateEntry *se;
1011     int ret;
1012 
1013     trace_savevm_state_setup();
1014     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1015         if (!se->ops || !se->ops->save_setup) {
1016             continue;
1017         }
1018         if (se->ops && se->ops->is_active) {
1019             if (!se->ops->is_active(se->opaque)) {
1020                 continue;
1021             }
1022         }
1023         save_section_header(f, se, QEMU_VM_SECTION_START);
1024 
1025         ret = se->ops->save_setup(f, se->opaque);
1026         save_section_footer(f, se);
1027         if (ret < 0) {
1028             qemu_file_set_error(f, ret);
1029             break;
1030         }
1031     }
1032 }
1033 
1034 int qemu_savevm_state_resume_prepare(MigrationState *s)
1035 {
1036     SaveStateEntry *se;
1037     int ret;
1038 
1039     trace_savevm_state_resume_prepare();
1040 
1041     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1042         if (!se->ops || !se->ops->resume_prepare) {
1043             continue;
1044         }
1045         if (se->ops && se->ops->is_active) {
1046             if (!se->ops->is_active(se->opaque)) {
1047                 continue;
1048             }
1049         }
1050         ret = se->ops->resume_prepare(s, se->opaque);
1051         if (ret < 0) {
1052             return ret;
1053         }
1054     }
1055 
1056     return 0;
1057 }
1058 
1059 /*
1060  * this function has three return values:
1061  *   negative: there was one error, and we have -errno.
1062  *   0 : We haven't finished, caller have to go again
1063  *   1 : We have finished, we can go to complete phase
1064  */
1065 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
1066 {
1067     SaveStateEntry *se;
1068     int ret = 1;
1069 
1070     trace_savevm_state_iterate();
1071     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1072         if (!se->ops || !se->ops->save_live_iterate) {
1073             continue;
1074         }
1075         if (se->ops && se->ops->is_active) {
1076             if (!se->ops->is_active(se->opaque)) {
1077                 continue;
1078             }
1079         }
1080         if (se->ops && se->ops->is_active_iterate) {
1081             if (!se->ops->is_active_iterate(se->opaque)) {
1082                 continue;
1083             }
1084         }
1085         /*
1086          * In the postcopy phase, any device that doesn't know how to
1087          * do postcopy should have saved it's state in the _complete
1088          * call that's already run, it might get confused if we call
1089          * iterate afterwards.
1090          */
1091         if (postcopy &&
1092             !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
1093             continue;
1094         }
1095         if (qemu_file_rate_limit(f)) {
1096             return 0;
1097         }
1098         trace_savevm_section_start(se->idstr, se->section_id);
1099 
1100         save_section_header(f, se, QEMU_VM_SECTION_PART);
1101 
1102         ret = se->ops->save_live_iterate(f, se->opaque);
1103         trace_savevm_section_end(se->idstr, se->section_id, ret);
1104         save_section_footer(f, se);
1105 
1106         if (ret < 0) {
1107             qemu_file_set_error(f, ret);
1108         }
1109         if (ret <= 0) {
1110             /* Do not proceed to the next vmstate before this one reported
1111                completion of the current stage. This serializes the migration
1112                and reduces the probability that a faster changing state is
1113                synchronized over and over again. */
1114             break;
1115         }
1116     }
1117     return ret;
1118 }
1119 
1120 static bool should_send_vmdesc(void)
1121 {
1122     MachineState *machine = MACHINE(qdev_get_machine());
1123     bool in_postcopy = migration_in_postcopy();
1124     return !machine->suppress_vmdesc && !in_postcopy;
1125 }
1126 
1127 /*
1128  * Calls the save_live_complete_postcopy methods
1129  * causing the last few pages to be sent immediately and doing any associated
1130  * cleanup.
1131  * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1132  * all the other devices, but that happens at the point we switch to postcopy.
1133  */
1134 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1135 {
1136     SaveStateEntry *se;
1137     int ret;
1138 
1139     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1140         if (!se->ops || !se->ops->save_live_complete_postcopy) {
1141             continue;
1142         }
1143         if (se->ops && se->ops->is_active) {
1144             if (!se->ops->is_active(se->opaque)) {
1145                 continue;
1146             }
1147         }
1148         trace_savevm_section_start(se->idstr, se->section_id);
1149         /* Section type */
1150         qemu_put_byte(f, QEMU_VM_SECTION_END);
1151         qemu_put_be32(f, se->section_id);
1152 
1153         ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1154         trace_savevm_section_end(se->idstr, se->section_id, ret);
1155         save_section_footer(f, se);
1156         if (ret < 0) {
1157             qemu_file_set_error(f, ret);
1158             return;
1159         }
1160     }
1161 
1162     qemu_put_byte(f, QEMU_VM_EOF);
1163     qemu_fflush(f);
1164 }
1165 
1166 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1167                                        bool inactivate_disks)
1168 {
1169     QJSON *vmdesc;
1170     int vmdesc_len;
1171     SaveStateEntry *se;
1172     int ret;
1173     bool in_postcopy = migration_in_postcopy();
1174 
1175     trace_savevm_state_complete_precopy();
1176 
1177     cpu_synchronize_all_states();
1178 
1179     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1180         if (!se->ops ||
1181             (in_postcopy && se->ops->has_postcopy &&
1182              se->ops->has_postcopy(se->opaque)) ||
1183             (in_postcopy && !iterable_only) ||
1184             !se->ops->save_live_complete_precopy) {
1185             continue;
1186         }
1187 
1188         if (se->ops && se->ops->is_active) {
1189             if (!se->ops->is_active(se->opaque)) {
1190                 continue;
1191             }
1192         }
1193         trace_savevm_section_start(se->idstr, se->section_id);
1194 
1195         save_section_header(f, se, QEMU_VM_SECTION_END);
1196 
1197         ret = se->ops->save_live_complete_precopy(f, se->opaque);
1198         trace_savevm_section_end(se->idstr, se->section_id, ret);
1199         save_section_footer(f, se);
1200         if (ret < 0) {
1201             qemu_file_set_error(f, ret);
1202             return -1;
1203         }
1204     }
1205 
1206     if (iterable_only) {
1207         return 0;
1208     }
1209 
1210     vmdesc = qjson_new();
1211     json_prop_int(vmdesc, "page_size", qemu_target_page_size());
1212     json_start_array(vmdesc, "devices");
1213     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1214 
1215         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1216             continue;
1217         }
1218         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1219             trace_savevm_section_skip(se->idstr, se->section_id);
1220             continue;
1221         }
1222 
1223         trace_savevm_section_start(se->idstr, se->section_id);
1224 
1225         json_start_object(vmdesc, NULL);
1226         json_prop_str(vmdesc, "name", se->idstr);
1227         json_prop_int(vmdesc, "instance_id", se->instance_id);
1228 
1229         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1230         ret = vmstate_save(f, se, vmdesc);
1231         if (ret) {
1232             qemu_file_set_error(f, ret);
1233             return ret;
1234         }
1235         trace_savevm_section_end(se->idstr, se->section_id, 0);
1236         save_section_footer(f, se);
1237 
1238         json_end_object(vmdesc);
1239     }
1240 
1241     if (inactivate_disks) {
1242         /* Inactivate before sending QEMU_VM_EOF so that the
1243          * bdrv_invalidate_cache_all() on the other end won't fail. */
1244         ret = bdrv_inactivate_all();
1245         if (ret) {
1246             error_report("%s: bdrv_inactivate_all() failed (%d)",
1247                          __func__, ret);
1248             qemu_file_set_error(f, ret);
1249             return ret;
1250         }
1251     }
1252     if (!in_postcopy) {
1253         /* Postcopy stream will still be going */
1254         qemu_put_byte(f, QEMU_VM_EOF);
1255     }
1256 
1257     json_end_array(vmdesc);
1258     qjson_finish(vmdesc);
1259     vmdesc_len = strlen(qjson_get_str(vmdesc));
1260 
1261     if (should_send_vmdesc()) {
1262         qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1263         qemu_put_be32(f, vmdesc_len);
1264         qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1265     }
1266     qjson_destroy(vmdesc);
1267 
1268     qemu_fflush(f);
1269     return 0;
1270 }
1271 
1272 /* Give an estimate of the amount left to be transferred,
1273  * the result is split into the amount for units that can and
1274  * for units that can't do postcopy.
1275  */
1276 void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
1277                                uint64_t *res_precopy_only,
1278                                uint64_t *res_compatible,
1279                                uint64_t *res_postcopy_only)
1280 {
1281     SaveStateEntry *se;
1282 
1283     *res_precopy_only = 0;
1284     *res_compatible = 0;
1285     *res_postcopy_only = 0;
1286 
1287 
1288     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1289         if (!se->ops || !se->ops->save_live_pending) {
1290             continue;
1291         }
1292         if (se->ops && se->ops->is_active) {
1293             if (!se->ops->is_active(se->opaque)) {
1294                 continue;
1295             }
1296         }
1297         se->ops->save_live_pending(f, se->opaque, threshold_size,
1298                                    res_precopy_only, res_compatible,
1299                                    res_postcopy_only);
1300     }
1301 }
1302 
1303 void qemu_savevm_state_cleanup(void)
1304 {
1305     SaveStateEntry *se;
1306 
1307     trace_savevm_state_cleanup();
1308     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1309         if (se->ops && se->ops->save_cleanup) {
1310             se->ops->save_cleanup(se->opaque);
1311         }
1312     }
1313 }
1314 
1315 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1316 {
1317     int ret;
1318     MigrationState *ms = migrate_get_current();
1319     MigrationStatus status;
1320 
1321     migrate_init(ms);
1322 
1323     ms->to_dst_file = f;
1324 
1325     if (migration_is_blocked(errp)) {
1326         ret = -EINVAL;
1327         goto done;
1328     }
1329 
1330     if (migrate_use_block()) {
1331         error_setg(errp, "Block migration and snapshots are incompatible");
1332         ret = -EINVAL;
1333         goto done;
1334     }
1335 
1336     qemu_mutex_unlock_iothread();
1337     qemu_savevm_state_header(f);
1338     qemu_savevm_state_setup(f);
1339     qemu_mutex_lock_iothread();
1340 
1341     while (qemu_file_get_error(f) == 0) {
1342         if (qemu_savevm_state_iterate(f, false) > 0) {
1343             break;
1344         }
1345     }
1346 
1347     ret = qemu_file_get_error(f);
1348     if (ret == 0) {
1349         qemu_savevm_state_complete_precopy(f, false, false);
1350         ret = qemu_file_get_error(f);
1351     }
1352     qemu_savevm_state_cleanup();
1353     if (ret != 0) {
1354         error_setg_errno(errp, -ret, "Error while writing VM state");
1355     }
1356 
1357 done:
1358     if (ret != 0) {
1359         status = MIGRATION_STATUS_FAILED;
1360     } else {
1361         status = MIGRATION_STATUS_COMPLETED;
1362     }
1363     migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1364 
1365     /* f is outer parameter, it should not stay in global migration state after
1366      * this function finished */
1367     ms->to_dst_file = NULL;
1368 
1369     return ret;
1370 }
1371 
1372 static int qemu_save_device_state(QEMUFile *f)
1373 {
1374     SaveStateEntry *se;
1375 
1376     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1377     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1378 
1379     cpu_synchronize_all_states();
1380 
1381     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1382         int ret;
1383 
1384         if (se->is_ram) {
1385             continue;
1386         }
1387         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1388             continue;
1389         }
1390         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1391             continue;
1392         }
1393 
1394         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1395 
1396         ret = vmstate_save(f, se, NULL);
1397         if (ret) {
1398             return ret;
1399         }
1400 
1401         save_section_footer(f, se);
1402     }
1403 
1404     qemu_put_byte(f, QEMU_VM_EOF);
1405 
1406     return qemu_file_get_error(f);
1407 }
1408 
1409 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1410 {
1411     SaveStateEntry *se;
1412 
1413     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1414         if (!strcmp(se->idstr, idstr) &&
1415             (instance_id == se->instance_id ||
1416              instance_id == se->alias_id))
1417             return se;
1418         /* Migrating from an older version? */
1419         if (strstr(se->idstr, idstr) && se->compat) {
1420             if (!strcmp(se->compat->idstr, idstr) &&
1421                 (instance_id == se->compat->instance_id ||
1422                  instance_id == se->alias_id))
1423                 return se;
1424         }
1425     }
1426     return NULL;
1427 }
1428 
1429 enum LoadVMExitCodes {
1430     /* Allow a command to quit all layers of nested loadvm loops */
1431     LOADVM_QUIT     =  1,
1432 };
1433 
1434 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis);
1435 
1436 /* ------ incoming postcopy messages ------ */
1437 /* 'advise' arrives before any transfers just to tell us that a postcopy
1438  * *might* happen - it might be skipped if precopy transferred everything
1439  * quickly.
1440  */
1441 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1442                                          uint16_t len)
1443 {
1444     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1445     uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1446     Error *local_err = NULL;
1447 
1448     trace_loadvm_postcopy_handle_advise();
1449     if (ps != POSTCOPY_INCOMING_NONE) {
1450         error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1451         return -1;
1452     }
1453 
1454     switch (len) {
1455     case 0:
1456         if (migrate_postcopy_ram()) {
1457             error_report("RAM postcopy is enabled but have 0 byte advise");
1458             return -EINVAL;
1459         }
1460         return 0;
1461     case 8 + 8:
1462         if (!migrate_postcopy_ram()) {
1463             error_report("RAM postcopy is disabled but have 16 byte advise");
1464             return -EINVAL;
1465         }
1466         break;
1467     default:
1468         error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1469         return -EINVAL;
1470     }
1471 
1472     if (!postcopy_ram_supported_by_host(mis)) {
1473         postcopy_state_set(POSTCOPY_INCOMING_NONE);
1474         return -1;
1475     }
1476 
1477     remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1478     local_pagesize_summary = ram_pagesize_summary();
1479 
1480     if (remote_pagesize_summary != local_pagesize_summary)  {
1481         /*
1482          * This detects two potential causes of mismatch:
1483          *   a) A mismatch in host page sizes
1484          *      Some combinations of mismatch are probably possible but it gets
1485          *      a bit more complicated.  In particular we need to place whole
1486          *      host pages on the dest at once, and we need to ensure that we
1487          *      handle dirtying to make sure we never end up sending part of
1488          *      a hostpage on it's own.
1489          *   b) The use of different huge page sizes on source/destination
1490          *      a more fine grain test is performed during RAM block migration
1491          *      but this test here causes a nice early clear failure, and
1492          *      also fails when passed to an older qemu that doesn't
1493          *      do huge pages.
1494          */
1495         error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1496                                                              " d=%" PRIx64 ")",
1497                      remote_pagesize_summary, local_pagesize_summary);
1498         return -1;
1499     }
1500 
1501     remote_tps = qemu_get_be64(mis->from_src_file);
1502     if (remote_tps != qemu_target_page_size()) {
1503         /*
1504          * Again, some differences could be dealt with, but for now keep it
1505          * simple.
1506          */
1507         error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1508                      (int)remote_tps, qemu_target_page_size());
1509         return -1;
1510     }
1511 
1512     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1513         error_report_err(local_err);
1514         return -1;
1515     }
1516 
1517     if (ram_postcopy_incoming_init(mis)) {
1518         return -1;
1519     }
1520 
1521     postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1522 
1523     return 0;
1524 }
1525 
1526 /* After postcopy we will be told to throw some pages away since they're
1527  * dirty and will have to be demand fetched.  Must happen before CPU is
1528  * started.
1529  * There can be 0..many of these messages, each encoding multiple pages.
1530  */
1531 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1532                                               uint16_t len)
1533 {
1534     int tmp;
1535     char ramid[256];
1536     PostcopyState ps = postcopy_state_get();
1537 
1538     trace_loadvm_postcopy_ram_handle_discard();
1539 
1540     switch (ps) {
1541     case POSTCOPY_INCOMING_ADVISE:
1542         /* 1st discard */
1543         tmp = postcopy_ram_prepare_discard(mis);
1544         if (tmp) {
1545             return tmp;
1546         }
1547         break;
1548 
1549     case POSTCOPY_INCOMING_DISCARD:
1550         /* Expected state */
1551         break;
1552 
1553     default:
1554         error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1555                      ps);
1556         return -1;
1557     }
1558     /* We're expecting a
1559      *    Version (0)
1560      *    a RAM ID string (length byte, name, 0 term)
1561      *    then at least 1 16 byte chunk
1562     */
1563     if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1564         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1565         return -1;
1566     }
1567 
1568     tmp = qemu_get_byte(mis->from_src_file);
1569     if (tmp != postcopy_ram_discard_version) {
1570         error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1571         return -1;
1572     }
1573 
1574     if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1575         error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1576         return -1;
1577     }
1578     tmp = qemu_get_byte(mis->from_src_file);
1579     if (tmp != 0) {
1580         error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1581         return -1;
1582     }
1583 
1584     len -= 3 + strlen(ramid);
1585     if (len % 16) {
1586         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1587         return -1;
1588     }
1589     trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1590     while (len) {
1591         uint64_t start_addr, block_length;
1592         start_addr = qemu_get_be64(mis->from_src_file);
1593         block_length = qemu_get_be64(mis->from_src_file);
1594 
1595         len -= 16;
1596         int ret = ram_discard_range(ramid, start_addr, block_length);
1597         if (ret) {
1598             return ret;
1599         }
1600     }
1601     trace_loadvm_postcopy_ram_handle_discard_end();
1602 
1603     return 0;
1604 }
1605 
1606 /*
1607  * Triggered by a postcopy_listen command; this thread takes over reading
1608  * the input stream, leaving the main thread free to carry on loading the rest
1609  * of the device state (from RAM).
1610  * (TODO:This could do with being in a postcopy file - but there again it's
1611  * just another input loop, not that postcopy specific)
1612  */
1613 static void *postcopy_ram_listen_thread(void *opaque)
1614 {
1615     MigrationIncomingState *mis = migration_incoming_get_current();
1616     QEMUFile *f = mis->from_src_file;
1617     int load_res;
1618 
1619     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1620                                    MIGRATION_STATUS_POSTCOPY_ACTIVE);
1621     qemu_sem_post(&mis->listen_thread_sem);
1622     trace_postcopy_ram_listen_thread_start();
1623 
1624     /*
1625      * Because we're a thread and not a coroutine we can't yield
1626      * in qemu_file, and thus we must be blocking now.
1627      */
1628     qemu_file_set_blocking(f, true);
1629     load_res = qemu_loadvm_state_main(f, mis);
1630 
1631     /*
1632      * This is tricky, but, mis->from_src_file can change after it
1633      * returns, when postcopy recovery happened. In the future, we may
1634      * want a wrapper for the QEMUFile handle.
1635      */
1636     f = mis->from_src_file;
1637 
1638     /* And non-blocking again so we don't block in any cleanup */
1639     qemu_file_set_blocking(f, false);
1640 
1641     trace_postcopy_ram_listen_thread_exit();
1642     if (load_res < 0) {
1643         error_report("%s: loadvm failed: %d", __func__, load_res);
1644         qemu_file_set_error(f, load_res);
1645         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1646                                        MIGRATION_STATUS_FAILED);
1647     } else {
1648         /*
1649          * This looks good, but it's possible that the device loading in the
1650          * main thread hasn't finished yet, and so we might not be in 'RUN'
1651          * state yet; wait for the end of the main thread.
1652          */
1653         qemu_event_wait(&mis->main_thread_load_event);
1654     }
1655     postcopy_ram_incoming_cleanup(mis);
1656 
1657     if (load_res < 0) {
1658         /*
1659          * If something went wrong then we have a bad state so exit;
1660          * depending how far we got it might be possible at this point
1661          * to leave the guest running and fire MCEs for pages that never
1662          * arrived as a desperate recovery step.
1663          */
1664         exit(EXIT_FAILURE);
1665     }
1666 
1667     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1668                                    MIGRATION_STATUS_COMPLETED);
1669     /*
1670      * If everything has worked fine, then the main thread has waited
1671      * for us to start, and we're the last use of the mis.
1672      * (If something broke then qemu will have to exit anyway since it's
1673      * got a bad migration state).
1674      */
1675     migration_incoming_state_destroy();
1676     qemu_loadvm_state_cleanup();
1677 
1678     return NULL;
1679 }
1680 
1681 /* After this message we must be able to immediately receive postcopy data */
1682 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1683 {
1684     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1685     trace_loadvm_postcopy_handle_listen();
1686     Error *local_err = NULL;
1687 
1688     if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1689         error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1690         return -1;
1691     }
1692     if (ps == POSTCOPY_INCOMING_ADVISE) {
1693         /*
1694          * A rare case, we entered listen without having to do any discards,
1695          * so do the setup that's normally done at the time of the 1st discard.
1696          */
1697         if (migrate_postcopy_ram()) {
1698             postcopy_ram_prepare_discard(mis);
1699         }
1700     }
1701 
1702     /*
1703      * Sensitise RAM - can now generate requests for blocks that don't exist
1704      * However, at this point the CPU shouldn't be running, and the IO
1705      * shouldn't be doing anything yet so don't actually expect requests
1706      */
1707     if (migrate_postcopy_ram()) {
1708         if (postcopy_ram_enable_notify(mis)) {
1709             return -1;
1710         }
1711     }
1712 
1713     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
1714         error_report_err(local_err);
1715         return -1;
1716     }
1717 
1718     if (mis->have_listen_thread) {
1719         error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread");
1720         return -1;
1721     }
1722 
1723     mis->have_listen_thread = true;
1724     /* Start up the listening thread and wait for it to signal ready */
1725     qemu_sem_init(&mis->listen_thread_sem, 0);
1726     qemu_thread_create(&mis->listen_thread, "postcopy/listen",
1727                        postcopy_ram_listen_thread, NULL,
1728                        QEMU_THREAD_DETACHED);
1729     qemu_sem_wait(&mis->listen_thread_sem);
1730     qemu_sem_destroy(&mis->listen_thread_sem);
1731 
1732     return 0;
1733 }
1734 
1735 
1736 typedef struct {
1737     QEMUBH *bh;
1738 } HandleRunBhData;
1739 
1740 static void loadvm_postcopy_handle_run_bh(void *opaque)
1741 {
1742     Error *local_err = NULL;
1743     HandleRunBhData *data = opaque;
1744 
1745     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1746      * in migration.c
1747      */
1748     cpu_synchronize_all_post_init();
1749 
1750     qemu_announce_self();
1751 
1752     /* Make sure all file formats flush their mutable metadata.
1753      * If we get an error here, just don't restart the VM yet. */
1754     bdrv_invalidate_cache_all(&local_err);
1755     if (local_err) {
1756         error_report_err(local_err);
1757         local_err = NULL;
1758         autostart = false;
1759     }
1760 
1761     trace_loadvm_postcopy_handle_run_cpu_sync();
1762     cpu_synchronize_all_post_init();
1763 
1764     trace_loadvm_postcopy_handle_run_vmstart();
1765 
1766     dirty_bitmap_mig_before_vm_start();
1767 
1768     if (autostart) {
1769         /* Hold onto your hats, starting the CPU */
1770         vm_start();
1771     } else {
1772         /* leave it paused and let management decide when to start the CPU */
1773         runstate_set(RUN_STATE_PAUSED);
1774     }
1775 
1776     qemu_bh_delete(data->bh);
1777     g_free(data);
1778 }
1779 
1780 /* After all discards we can start running and asking for pages */
1781 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1782 {
1783     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1784     HandleRunBhData *data;
1785 
1786     trace_loadvm_postcopy_handle_run();
1787     if (ps != POSTCOPY_INCOMING_LISTENING) {
1788         error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
1789         return -1;
1790     }
1791 
1792     data = g_new(HandleRunBhData, 1);
1793     data->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, data);
1794     qemu_bh_schedule(data->bh);
1795 
1796     /* We need to finish reading the stream from the package
1797      * and also stop reading anything more from the stream that loaded the
1798      * package (since it's now being read by the listener thread).
1799      * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1800      */
1801     return LOADVM_QUIT;
1802 }
1803 
1804 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
1805 {
1806     if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
1807         error_report("%s: illegal resume received", __func__);
1808         /* Don't fail the load, only for this. */
1809         return 0;
1810     }
1811 
1812     /*
1813      * This means source VM is ready to resume the postcopy migration.
1814      * It's time to switch state and release the fault thread to
1815      * continue service page faults.
1816      */
1817     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1818                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
1819     qemu_sem_post(&mis->postcopy_pause_sem_fault);
1820 
1821     trace_loadvm_postcopy_handle_resume();
1822 
1823     /* Tell source that "we are ready" */
1824     migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
1825 
1826     return 0;
1827 }
1828 
1829 /**
1830  * Immediately following this command is a blob of data containing an embedded
1831  * chunk of migration stream; read it and load it.
1832  *
1833  * @mis: Incoming state
1834  * @length: Length of packaged data to read
1835  *
1836  * Returns: Negative values on error
1837  *
1838  */
1839 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
1840 {
1841     int ret;
1842     size_t length;
1843     QIOChannelBuffer *bioc;
1844 
1845     length = qemu_get_be32(mis->from_src_file);
1846     trace_loadvm_handle_cmd_packaged(length);
1847 
1848     if (length > MAX_VM_CMD_PACKAGED_SIZE) {
1849         error_report("Unreasonably large packaged state: %zu", length);
1850         return -1;
1851     }
1852 
1853     bioc = qio_channel_buffer_new(length);
1854     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
1855     ret = qemu_get_buffer(mis->from_src_file,
1856                           bioc->data,
1857                           length);
1858     if (ret != length) {
1859         object_unref(OBJECT(bioc));
1860         error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
1861                      ret, length);
1862         return (ret < 0) ? ret : -EAGAIN;
1863     }
1864     bioc->usage += length;
1865     trace_loadvm_handle_cmd_packaged_received(ret);
1866 
1867     QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
1868 
1869     ret = qemu_loadvm_state_main(packf, mis);
1870     trace_loadvm_handle_cmd_packaged_main(ret);
1871     qemu_fclose(packf);
1872     object_unref(OBJECT(bioc));
1873 
1874     return ret;
1875 }
1876 
1877 /*
1878  * Handle request that source requests for recved_bitmap on
1879  * destination. Payload format:
1880  *
1881  * len (1 byte) + ramblock_name (<255 bytes)
1882  */
1883 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
1884                                      uint16_t len)
1885 {
1886     QEMUFile *file = mis->from_src_file;
1887     RAMBlock *rb;
1888     char block_name[256];
1889     size_t cnt;
1890 
1891     cnt = qemu_get_counted_string(file, block_name);
1892     if (!cnt) {
1893         error_report("%s: failed to read block name", __func__);
1894         return -EINVAL;
1895     }
1896 
1897     /* Validate before using the data */
1898     if (qemu_file_get_error(file)) {
1899         return qemu_file_get_error(file);
1900     }
1901 
1902     if (len != cnt + 1) {
1903         error_report("%s: invalid payload length (%d)", __func__, len);
1904         return -EINVAL;
1905     }
1906 
1907     rb = qemu_ram_block_by_name(block_name);
1908     if (!rb) {
1909         error_report("%s: block '%s' not found", __func__, block_name);
1910         return -EINVAL;
1911     }
1912 
1913     migrate_send_rp_recv_bitmap(mis, block_name);
1914 
1915     trace_loadvm_handle_recv_bitmap(block_name);
1916 
1917     return 0;
1918 }
1919 
1920 /*
1921  * Process an incoming 'QEMU_VM_COMMAND'
1922  * 0           just a normal return
1923  * LOADVM_QUIT All good, but exit the loop
1924  * <0          Error
1925  */
1926 static int loadvm_process_command(QEMUFile *f)
1927 {
1928     MigrationIncomingState *mis = migration_incoming_get_current();
1929     uint16_t cmd;
1930     uint16_t len;
1931     uint32_t tmp32;
1932 
1933     cmd = qemu_get_be16(f);
1934     len = qemu_get_be16(f);
1935 
1936     /* Check validity before continue processing of cmds */
1937     if (qemu_file_get_error(f)) {
1938         return qemu_file_get_error(f);
1939     }
1940 
1941     trace_loadvm_process_command(cmd, len);
1942     if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
1943         error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
1944         return -EINVAL;
1945     }
1946 
1947     if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
1948         error_report("%s received with bad length - expecting %zu, got %d",
1949                      mig_cmd_args[cmd].name,
1950                      (size_t)mig_cmd_args[cmd].len, len);
1951         return -ERANGE;
1952     }
1953 
1954     switch (cmd) {
1955     case MIG_CMD_OPEN_RETURN_PATH:
1956         if (mis->to_src_file) {
1957             error_report("CMD_OPEN_RETURN_PATH called when RP already open");
1958             /* Not really a problem, so don't give up */
1959             return 0;
1960         }
1961         mis->to_src_file = qemu_file_get_return_path(f);
1962         if (!mis->to_src_file) {
1963             error_report("CMD_OPEN_RETURN_PATH failed");
1964             return -1;
1965         }
1966         break;
1967 
1968     case MIG_CMD_PING:
1969         tmp32 = qemu_get_be32(f);
1970         trace_loadvm_process_command_ping(tmp32);
1971         if (!mis->to_src_file) {
1972             error_report("CMD_PING (0x%x) received with no return path",
1973                          tmp32);
1974             return -1;
1975         }
1976         migrate_send_rp_pong(mis, tmp32);
1977         break;
1978 
1979     case MIG_CMD_PACKAGED:
1980         return loadvm_handle_cmd_packaged(mis);
1981 
1982     case MIG_CMD_POSTCOPY_ADVISE:
1983         return loadvm_postcopy_handle_advise(mis, len);
1984 
1985     case MIG_CMD_POSTCOPY_LISTEN:
1986         return loadvm_postcopy_handle_listen(mis);
1987 
1988     case MIG_CMD_POSTCOPY_RUN:
1989         return loadvm_postcopy_handle_run(mis);
1990 
1991     case MIG_CMD_POSTCOPY_RAM_DISCARD:
1992         return loadvm_postcopy_ram_handle_discard(mis, len);
1993 
1994     case MIG_CMD_POSTCOPY_RESUME:
1995         return loadvm_postcopy_handle_resume(mis);
1996 
1997     case MIG_CMD_RECV_BITMAP:
1998         return loadvm_handle_recv_bitmap(mis, len);
1999     }
2000 
2001     return 0;
2002 }
2003 
2004 /*
2005  * Read a footer off the wire and check that it matches the expected section
2006  *
2007  * Returns: true if the footer was good
2008  *          false if there is a problem (and calls error_report to say why)
2009  */
2010 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
2011 {
2012     int ret;
2013     uint8_t read_mark;
2014     uint32_t read_section_id;
2015 
2016     if (!migrate_get_current()->send_section_footer) {
2017         /* No footer to check */
2018         return true;
2019     }
2020 
2021     read_mark = qemu_get_byte(f);
2022 
2023     ret = qemu_file_get_error(f);
2024     if (ret) {
2025         error_report("%s: Read section footer failed: %d",
2026                      __func__, ret);
2027         return false;
2028     }
2029 
2030     if (read_mark != QEMU_VM_SECTION_FOOTER) {
2031         error_report("Missing section footer for %s", se->idstr);
2032         return false;
2033     }
2034 
2035     read_section_id = qemu_get_be32(f);
2036     if (read_section_id != se->load_section_id) {
2037         error_report("Mismatched section id in footer for %s -"
2038                      " read 0x%x expected 0x%x",
2039                      se->idstr, read_section_id, se->load_section_id);
2040         return false;
2041     }
2042 
2043     /* All good */
2044     return true;
2045 }
2046 
2047 static int
2048 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
2049 {
2050     uint32_t instance_id, version_id, section_id;
2051     SaveStateEntry *se;
2052     char idstr[256];
2053     int ret;
2054 
2055     /* Read section start */
2056     section_id = qemu_get_be32(f);
2057     if (!qemu_get_counted_string(f, idstr)) {
2058         error_report("Unable to read ID string for section %u",
2059                      section_id);
2060         return -EINVAL;
2061     }
2062     instance_id = qemu_get_be32(f);
2063     version_id = qemu_get_be32(f);
2064 
2065     ret = qemu_file_get_error(f);
2066     if (ret) {
2067         error_report("%s: Failed to read instance/version ID: %d",
2068                      __func__, ret);
2069         return ret;
2070     }
2071 
2072     trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2073             instance_id, version_id);
2074     /* Find savevm section */
2075     se = find_se(idstr, instance_id);
2076     if (se == NULL) {
2077         error_report("Unknown savevm section or instance '%s' %d",
2078                      idstr, instance_id);
2079         return -EINVAL;
2080     }
2081 
2082     /* Validate version */
2083     if (version_id > se->version_id) {
2084         error_report("savevm: unsupported version %d for '%s' v%d",
2085                      version_id, idstr, se->version_id);
2086         return -EINVAL;
2087     }
2088     se->load_version_id = version_id;
2089     se->load_section_id = section_id;
2090 
2091     /* Validate if it is a device's state */
2092     if (xen_enabled() && se->is_ram) {
2093         error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2094         return -EINVAL;
2095     }
2096 
2097     ret = vmstate_load(f, se);
2098     if (ret < 0) {
2099         error_report("error while loading state for instance 0x%x of"
2100                      " device '%s'", instance_id, idstr);
2101         return ret;
2102     }
2103     if (!check_section_footer(f, se)) {
2104         return -EINVAL;
2105     }
2106 
2107     return 0;
2108 }
2109 
2110 static int
2111 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
2112 {
2113     uint32_t section_id;
2114     SaveStateEntry *se;
2115     int ret;
2116 
2117     section_id = qemu_get_be32(f);
2118 
2119     ret = qemu_file_get_error(f);
2120     if (ret) {
2121         error_report("%s: Failed to read section ID: %d",
2122                      __func__, ret);
2123         return ret;
2124     }
2125 
2126     trace_qemu_loadvm_state_section_partend(section_id);
2127     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2128         if (se->load_section_id == section_id) {
2129             break;
2130         }
2131     }
2132     if (se == NULL) {
2133         error_report("Unknown savevm section %d", section_id);
2134         return -EINVAL;
2135     }
2136 
2137     ret = vmstate_load(f, se);
2138     if (ret < 0) {
2139         error_report("error while loading state section id %d(%s)",
2140                      section_id, se->idstr);
2141         return ret;
2142     }
2143     if (!check_section_footer(f, se)) {
2144         return -EINVAL;
2145     }
2146 
2147     return 0;
2148 }
2149 
2150 static int qemu_loadvm_state_setup(QEMUFile *f)
2151 {
2152     SaveStateEntry *se;
2153     int ret;
2154 
2155     trace_loadvm_state_setup();
2156     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2157         if (!se->ops || !se->ops->load_setup) {
2158             continue;
2159         }
2160         if (se->ops && se->ops->is_active) {
2161             if (!se->ops->is_active(se->opaque)) {
2162                 continue;
2163             }
2164         }
2165 
2166         ret = se->ops->load_setup(f, se->opaque);
2167         if (ret < 0) {
2168             qemu_file_set_error(f, ret);
2169             error_report("Load state of device %s failed", se->idstr);
2170             return ret;
2171         }
2172     }
2173     return 0;
2174 }
2175 
2176 void qemu_loadvm_state_cleanup(void)
2177 {
2178     SaveStateEntry *se;
2179 
2180     trace_loadvm_state_cleanup();
2181     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2182         if (se->ops && se->ops->load_cleanup) {
2183             se->ops->load_cleanup(se->opaque);
2184         }
2185     }
2186 }
2187 
2188 /* Return true if we should continue the migration, or false. */
2189 static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2190 {
2191     trace_postcopy_pause_incoming();
2192 
2193     /* Clear the triggered bit to allow one recovery */
2194     mis->postcopy_recover_triggered = false;
2195 
2196     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2197                       MIGRATION_STATUS_POSTCOPY_PAUSED);
2198 
2199     assert(mis->from_src_file);
2200     qemu_file_shutdown(mis->from_src_file);
2201     qemu_fclose(mis->from_src_file);
2202     mis->from_src_file = NULL;
2203 
2204     assert(mis->to_src_file);
2205     qemu_file_shutdown(mis->to_src_file);
2206     qemu_mutex_lock(&mis->rp_mutex);
2207     qemu_fclose(mis->to_src_file);
2208     mis->to_src_file = NULL;
2209     qemu_mutex_unlock(&mis->rp_mutex);
2210 
2211     /* Notify the fault thread for the invalidated file handle */
2212     postcopy_fault_thread_notify(mis);
2213 
2214     error_report("Detected IO failure for postcopy. "
2215                  "Migration paused.");
2216 
2217     while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2218         qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2219     }
2220 
2221     trace_postcopy_pause_incoming_continued();
2222 
2223     return true;
2224 }
2225 
2226 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
2227 {
2228     uint8_t section_type;
2229     int ret = 0;
2230 
2231 retry:
2232     while (true) {
2233         section_type = qemu_get_byte(f);
2234 
2235         if (qemu_file_get_error(f)) {
2236             ret = qemu_file_get_error(f);
2237             break;
2238         }
2239 
2240         trace_qemu_loadvm_state_section(section_type);
2241         switch (section_type) {
2242         case QEMU_VM_SECTION_START:
2243         case QEMU_VM_SECTION_FULL:
2244             ret = qemu_loadvm_section_start_full(f, mis);
2245             if (ret < 0) {
2246                 goto out;
2247             }
2248             break;
2249         case QEMU_VM_SECTION_PART:
2250         case QEMU_VM_SECTION_END:
2251             ret = qemu_loadvm_section_part_end(f, mis);
2252             if (ret < 0) {
2253                 goto out;
2254             }
2255             break;
2256         case QEMU_VM_COMMAND:
2257             ret = loadvm_process_command(f);
2258             trace_qemu_loadvm_state_section_command(ret);
2259             if ((ret < 0) || (ret & LOADVM_QUIT)) {
2260                 goto out;
2261             }
2262             break;
2263         case QEMU_VM_EOF:
2264             /* This is the end of migration */
2265             goto out;
2266         default:
2267             error_report("Unknown savevm section type %d", section_type);
2268             ret = -EINVAL;
2269             goto out;
2270         }
2271     }
2272 
2273 out:
2274     if (ret < 0) {
2275         qemu_file_set_error(f, ret);
2276 
2277         /*
2278          * Detect whether it is:
2279          *
2280          * 1. postcopy running (after receiving all device data, which
2281          *    must be in POSTCOPY_INCOMING_RUNNING state.  Note that
2282          *    POSTCOPY_INCOMING_LISTENING is still not enough, it's
2283          *    still receiving device states).
2284          * 2. network failure (-EIO)
2285          *
2286          * If so, we try to wait for a recovery.
2287          */
2288         if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2289             ret == -EIO && postcopy_pause_incoming(mis)) {
2290             /* Reset f to point to the newly created channel */
2291             f = mis->from_src_file;
2292             goto retry;
2293         }
2294     }
2295     return ret;
2296 }
2297 
2298 int qemu_loadvm_state(QEMUFile *f)
2299 {
2300     MigrationIncomingState *mis = migration_incoming_get_current();
2301     Error *local_err = NULL;
2302     unsigned int v;
2303     int ret;
2304 
2305     if (qemu_savevm_state_blocked(&local_err)) {
2306         error_report_err(local_err);
2307         return -EINVAL;
2308     }
2309 
2310     v = qemu_get_be32(f);
2311     if (v != QEMU_VM_FILE_MAGIC) {
2312         error_report("Not a migration stream");
2313         return -EINVAL;
2314     }
2315 
2316     v = qemu_get_be32(f);
2317     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2318         error_report("SaveVM v2 format is obsolete and don't work anymore");
2319         return -ENOTSUP;
2320     }
2321     if (v != QEMU_VM_FILE_VERSION) {
2322         error_report("Unsupported migration stream version");
2323         return -ENOTSUP;
2324     }
2325 
2326     if (qemu_loadvm_state_setup(f) != 0) {
2327         return -EINVAL;
2328     }
2329 
2330     if (migrate_get_current()->send_configuration) {
2331         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2332             error_report("Configuration section missing");
2333             return -EINVAL;
2334         }
2335         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2336 
2337         if (ret) {
2338             return ret;
2339         }
2340     }
2341 
2342     cpu_synchronize_all_pre_loadvm();
2343 
2344     ret = qemu_loadvm_state_main(f, mis);
2345     qemu_event_set(&mis->main_thread_load_event);
2346 
2347     trace_qemu_loadvm_state_post_main(ret);
2348 
2349     if (mis->have_listen_thread) {
2350         /* Listen thread still going, can't clean up yet */
2351         return ret;
2352     }
2353 
2354     if (ret == 0) {
2355         ret = qemu_file_get_error(f);
2356     }
2357 
2358     /*
2359      * Try to read in the VMDESC section as well, so that dumping tools that
2360      * intercept our migration stream have the chance to see it.
2361      */
2362 
2363     /* We've got to be careful; if we don't read the data and just shut the fd
2364      * then the sender can error if we close while it's still sending.
2365      * We also mustn't read data that isn't there; some transports (RDMA)
2366      * will stall waiting for that data when the source has already closed.
2367      */
2368     if (ret == 0 && should_send_vmdesc()) {
2369         uint8_t *buf;
2370         uint32_t size;
2371         uint8_t  section_type = qemu_get_byte(f);
2372 
2373         if (section_type != QEMU_VM_VMDESCRIPTION) {
2374             error_report("Expected vmdescription section, but got %d",
2375                          section_type);
2376             /*
2377              * It doesn't seem worth failing at this point since
2378              * we apparently have an otherwise valid VM state
2379              */
2380         } else {
2381             buf = g_malloc(0x1000);
2382             size = qemu_get_be32(f);
2383 
2384             while (size > 0) {
2385                 uint32_t read_chunk = MIN(size, 0x1000);
2386                 qemu_get_buffer(f, buf, read_chunk);
2387                 size -= read_chunk;
2388             }
2389             g_free(buf);
2390         }
2391     }
2392 
2393     qemu_loadvm_state_cleanup();
2394     cpu_synchronize_all_post_init();
2395 
2396     return ret;
2397 }
2398 
2399 int save_snapshot(const char *name, Error **errp)
2400 {
2401     BlockDriverState *bs, *bs1;
2402     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2403     int ret = -1;
2404     QEMUFile *f;
2405     int saved_vm_running;
2406     uint64_t vm_state_size;
2407     qemu_timeval tv;
2408     struct tm tm;
2409     AioContext *aio_context;
2410 
2411     if (!replay_can_snapshot()) {
2412         error_report("Record/replay does not allow making snapshot "
2413                      "right now. Try once more later.");
2414         return ret;
2415     }
2416 
2417     if (!bdrv_all_can_snapshot(&bs)) {
2418         error_setg(errp, "Device '%s' is writable but does not support "
2419                    "snapshots", bdrv_get_device_name(bs));
2420         return ret;
2421     }
2422 
2423     /* Delete old snapshots of the same name */
2424     if (name) {
2425         ret = bdrv_all_delete_snapshot(name, &bs1, errp);
2426         if (ret < 0) {
2427             error_prepend(errp, "Error while deleting snapshot on device "
2428                           "'%s': ", bdrv_get_device_name(bs1));
2429             return ret;
2430         }
2431     }
2432 
2433     bs = bdrv_all_find_vmstate_bs();
2434     if (bs == NULL) {
2435         error_setg(errp, "No block device can accept snapshots");
2436         return ret;
2437     }
2438     aio_context = bdrv_get_aio_context(bs);
2439 
2440     saved_vm_running = runstate_is_running();
2441 
2442     ret = global_state_store();
2443     if (ret) {
2444         error_setg(errp, "Error saving global state");
2445         return ret;
2446     }
2447     vm_stop(RUN_STATE_SAVE_VM);
2448 
2449     bdrv_drain_all_begin();
2450 
2451     aio_context_acquire(aio_context);
2452 
2453     memset(sn, 0, sizeof(*sn));
2454 
2455     /* fill auxiliary fields */
2456     qemu_gettimeofday(&tv);
2457     sn->date_sec = tv.tv_sec;
2458     sn->date_nsec = tv.tv_usec * 1000;
2459     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2460 
2461     if (name) {
2462         ret = bdrv_snapshot_find(bs, old_sn, name);
2463         if (ret >= 0) {
2464             pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2465             pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2466         } else {
2467             pstrcpy(sn->name, sizeof(sn->name), name);
2468         }
2469     } else {
2470         /* cast below needed for OpenBSD where tv_sec is still 'long' */
2471         localtime_r((const time_t *)&tv.tv_sec, &tm);
2472         strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2473     }
2474 
2475     /* save the VM state */
2476     f = qemu_fopen_bdrv(bs, 1);
2477     if (!f) {
2478         error_setg(errp, "Could not open VM state file");
2479         goto the_end;
2480     }
2481     ret = qemu_savevm_state(f, errp);
2482     vm_state_size = qemu_ftell(f);
2483     qemu_fclose(f);
2484     if (ret < 0) {
2485         goto the_end;
2486     }
2487 
2488     /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2489      * for itself.  BDRV_POLL_WHILE() does not support nested locking because
2490      * it only releases the lock once.  Therefore synchronous I/O will deadlock
2491      * unless we release the AioContext before bdrv_all_create_snapshot().
2492      */
2493     aio_context_release(aio_context);
2494     aio_context = NULL;
2495 
2496     ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs);
2497     if (ret < 0) {
2498         error_setg(errp, "Error while creating snapshot on '%s'",
2499                    bdrv_get_device_name(bs));
2500         goto the_end;
2501     }
2502 
2503     ret = 0;
2504 
2505  the_end:
2506     if (aio_context) {
2507         aio_context_release(aio_context);
2508     }
2509 
2510     bdrv_drain_all_end();
2511 
2512     if (saved_vm_running) {
2513         vm_start();
2514     }
2515     return ret;
2516 }
2517 
2518 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
2519                                 Error **errp)
2520 {
2521     QEMUFile *f;
2522     QIOChannelFile *ioc;
2523     int saved_vm_running;
2524     int ret;
2525 
2526     if (!has_live) {
2527         /* live default to true so old version of Xen tool stack can have a
2528          * successfull live migration */
2529         live = true;
2530     }
2531 
2532     saved_vm_running = runstate_is_running();
2533     vm_stop(RUN_STATE_SAVE_VM);
2534     global_state_store_running();
2535 
2536     ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2537     if (!ioc) {
2538         goto the_end;
2539     }
2540     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
2541     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
2542     object_unref(OBJECT(ioc));
2543     ret = qemu_save_device_state(f);
2544     if (ret < 0 || qemu_fclose(f) < 0) {
2545         error_setg(errp, QERR_IO_ERROR);
2546     } else {
2547         /* libxl calls the QMP command "stop" before calling
2548          * "xen-save-devices-state" and in case of migration failure, libxl
2549          * would call "cont".
2550          * So call bdrv_inactivate_all (release locks) here to let the other
2551          * side of the migration take controle of the images.
2552          */
2553         if (live && !saved_vm_running) {
2554             ret = bdrv_inactivate_all();
2555             if (ret) {
2556                 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
2557                            __func__, ret);
2558             }
2559         }
2560     }
2561 
2562  the_end:
2563     if (saved_vm_running) {
2564         vm_start();
2565     }
2566 }
2567 
2568 void qmp_xen_load_devices_state(const char *filename, Error **errp)
2569 {
2570     QEMUFile *f;
2571     QIOChannelFile *ioc;
2572     int ret;
2573 
2574     /* Guest must be paused before loading the device state; the RAM state
2575      * will already have been loaded by xc
2576      */
2577     if (runstate_is_running()) {
2578         error_setg(errp, "Cannot update device state while vm is running");
2579         return;
2580     }
2581     vm_stop(RUN_STATE_RESTORE_VM);
2582 
2583     ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2584     if (!ioc) {
2585         return;
2586     }
2587     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
2588     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
2589     object_unref(OBJECT(ioc));
2590 
2591     ret = qemu_loadvm_state(f);
2592     qemu_fclose(f);
2593     if (ret < 0) {
2594         error_setg(errp, QERR_IO_ERROR);
2595     }
2596     migration_incoming_state_destroy();
2597 }
2598 
2599 int load_snapshot(const char *name, Error **errp)
2600 {
2601     BlockDriverState *bs, *bs_vm_state;
2602     QEMUSnapshotInfo sn;
2603     QEMUFile *f;
2604     int ret;
2605     AioContext *aio_context;
2606     MigrationIncomingState *mis = migration_incoming_get_current();
2607 
2608     if (!replay_can_snapshot()) {
2609         error_report("Record/replay does not allow loading snapshot "
2610                      "right now. Try once more later.");
2611         return -EINVAL;
2612     }
2613 
2614     if (!bdrv_all_can_snapshot(&bs)) {
2615         error_setg(errp,
2616                    "Device '%s' is writable but does not support snapshots",
2617                    bdrv_get_device_name(bs));
2618         return -ENOTSUP;
2619     }
2620     ret = bdrv_all_find_snapshot(name, &bs);
2621     if (ret < 0) {
2622         error_setg(errp,
2623                    "Device '%s' does not have the requested snapshot '%s'",
2624                    bdrv_get_device_name(bs), name);
2625         return ret;
2626     }
2627 
2628     bs_vm_state = bdrv_all_find_vmstate_bs();
2629     if (!bs_vm_state) {
2630         error_setg(errp, "No block device supports snapshots");
2631         return -ENOTSUP;
2632     }
2633     aio_context = bdrv_get_aio_context(bs_vm_state);
2634 
2635     /* Don't even try to load empty VM states */
2636     aio_context_acquire(aio_context);
2637     ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2638     aio_context_release(aio_context);
2639     if (ret < 0) {
2640         return ret;
2641     } else if (sn.vm_state_size == 0) {
2642         error_setg(errp, "This is a disk-only snapshot. Revert to it "
2643                    " offline using qemu-img");
2644         return -EINVAL;
2645     }
2646 
2647     /* Flush all IO requests so they don't interfere with the new state.  */
2648     bdrv_drain_all_begin();
2649 
2650     ret = bdrv_all_goto_snapshot(name, &bs, errp);
2651     if (ret < 0) {
2652         error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
2653                       name, bdrv_get_device_name(bs));
2654         goto err_drain;
2655     }
2656 
2657     /* restore the VM state */
2658     f = qemu_fopen_bdrv(bs_vm_state, 0);
2659     if (!f) {
2660         error_setg(errp, "Could not open VM state file");
2661         ret = -EINVAL;
2662         goto err_drain;
2663     }
2664 
2665     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
2666     mis->from_src_file = f;
2667 
2668     aio_context_acquire(aio_context);
2669     ret = qemu_loadvm_state(f);
2670     migration_incoming_state_destroy();
2671     aio_context_release(aio_context);
2672 
2673     bdrv_drain_all_end();
2674 
2675     if (ret < 0) {
2676         error_setg(errp, "Error %d while loading VM state", ret);
2677         return ret;
2678     }
2679 
2680     return 0;
2681 
2682 err_drain:
2683     bdrv_drain_all_end();
2684     return ret;
2685 }
2686 
2687 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2688 {
2689     qemu_ram_set_idstr(mr->ram_block,
2690                        memory_region_name(mr), dev);
2691 }
2692 
2693 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2694 {
2695     qemu_ram_unset_idstr(mr->ram_block);
2696 }
2697 
2698 void vmstate_register_ram_global(MemoryRegion *mr)
2699 {
2700     vmstate_register_ram(mr, NULL);
2701 }
2702 
2703 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
2704 {
2705     /* check needed if --only-migratable is specified */
2706     if (!migrate_get_current()->only_migratable) {
2707         return true;
2708     }
2709 
2710     return !(vmsd && vmsd->unmigratable);
2711 }
2712