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 "net/net.h" 32 #include "migration.h" 33 #include "migration/snapshot.h" 34 #include "migration-stats.h" 35 #include "migration/vmstate.h" 36 #include "migration/misc.h" 37 #include "migration/register.h" 38 #include "migration/global_state.h" 39 #include "migration/channel-block.h" 40 #include "ram.h" 41 #include "qemu-file.h" 42 #include "savevm.h" 43 #include "postcopy-ram.h" 44 #include "qapi/error.h" 45 #include "qapi/qapi-commands-migration.h" 46 #include "qapi/clone-visitor.h" 47 #include "qapi/qapi-builtin-visit.h" 48 #include "qapi/qmp/qerror.h" 49 #include "qemu/error-report.h" 50 #include "sysemu/cpus.h" 51 #include "exec/memory.h" 52 #include "exec/target_page.h" 53 #include "trace.h" 54 #include "qemu/iov.h" 55 #include "qemu/job.h" 56 #include "qemu/main-loop.h" 57 #include "block/snapshot.h" 58 #include "qemu/cutils.h" 59 #include "io/channel-buffer.h" 60 #include "io/channel-file.h" 61 #include "sysemu/replay.h" 62 #include "sysemu/runstate.h" 63 #include "sysemu/sysemu.h" 64 #include "sysemu/xen.h" 65 #include "migration/colo.h" 66 #include "qemu/bitmap.h" 67 #include "net/announce.h" 68 #include "qemu/yank.h" 69 #include "yank_functions.h" 70 #include "sysemu/qtest.h" 71 #include "options.h" 72 73 const unsigned int postcopy_ram_discard_version; 74 75 /* Subcommands for QEMU_VM_COMMAND */ 76 enum qemu_vm_cmd { 77 MIG_CMD_INVALID = 0, /* Must be 0 */ 78 MIG_CMD_OPEN_RETURN_PATH, /* Tell the dest to open the Return path */ 79 MIG_CMD_PING, /* Request a PONG on the RP */ 80 81 MIG_CMD_POSTCOPY_ADVISE, /* Prior to any page transfers, just 82 warn we might want to do PC */ 83 MIG_CMD_POSTCOPY_LISTEN, /* Start listening for incoming 84 pages as it's running. */ 85 MIG_CMD_POSTCOPY_RUN, /* Start execution */ 86 87 MIG_CMD_POSTCOPY_RAM_DISCARD, /* A list of pages to discard that 88 were previously sent during 89 precopy but are dirty. */ 90 MIG_CMD_PACKAGED, /* Send a wrapped stream within this stream */ 91 MIG_CMD_ENABLE_COLO, /* Enable COLO */ 92 MIG_CMD_POSTCOPY_RESUME, /* resume postcopy on dest */ 93 MIG_CMD_RECV_BITMAP, /* Request for recved bitmap on dst */ 94 MIG_CMD_MAX 95 }; 96 97 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX 98 static struct mig_cmd_args { 99 ssize_t len; /* -1 = variable */ 100 const char *name; 101 } mig_cmd_args[] = { 102 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" }, 103 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" }, 104 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" }, 105 [MIG_CMD_POSTCOPY_ADVISE] = { .len = -1, .name = "POSTCOPY_ADVISE" }, 106 [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" }, 107 [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" }, 108 [MIG_CMD_POSTCOPY_RAM_DISCARD] = { 109 .len = -1, .name = "POSTCOPY_RAM_DISCARD" }, 110 [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" }, 111 [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" }, 112 [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, 113 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" }, 114 }; 115 116 /* Note for MIG_CMD_POSTCOPY_ADVISE: 117 * The format of arguments is depending on postcopy mode: 118 * - postcopy RAM only 119 * uint64_t host page size 120 * uint64_t target page size 121 * 122 * - postcopy RAM and postcopy dirty bitmaps 123 * format is the same as for postcopy RAM only 124 * 125 * - postcopy dirty bitmaps only 126 * Nothing. Command length field is 0. 127 * 128 * Be careful: adding a new postcopy entity with some other parameters should 129 * not break format self-description ability. Good way is to introduce some 130 * generic extendable format with an exception for two old entities. 131 */ 132 133 /***********************************************************/ 134 /* savevm/loadvm support */ 135 136 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) 137 { 138 if (is_writable) { 139 return qemu_file_new_output(QIO_CHANNEL(qio_channel_block_new(bs))); 140 } else { 141 return qemu_file_new_input(QIO_CHANNEL(qio_channel_block_new(bs))); 142 } 143 } 144 145 146 /* QEMUFile timer support. 147 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c 148 */ 149 150 void timer_put(QEMUFile *f, QEMUTimer *ts) 151 { 152 uint64_t expire_time; 153 154 expire_time = timer_expire_time_ns(ts); 155 qemu_put_be64(f, expire_time); 156 } 157 158 void timer_get(QEMUFile *f, QEMUTimer *ts) 159 { 160 uint64_t expire_time; 161 162 expire_time = qemu_get_be64(f); 163 if (expire_time != -1) { 164 timer_mod_ns(ts, expire_time); 165 } else { 166 timer_del(ts); 167 } 168 } 169 170 171 /* VMState timer support. 172 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c 173 */ 174 175 static int get_timer(QEMUFile *f, void *pv, size_t size, 176 const VMStateField *field) 177 { 178 QEMUTimer *v = pv; 179 timer_get(f, v); 180 return 0; 181 } 182 183 static int put_timer(QEMUFile *f, void *pv, size_t size, 184 const VMStateField *field, JSONWriter *vmdesc) 185 { 186 QEMUTimer *v = pv; 187 timer_put(f, v); 188 189 return 0; 190 } 191 192 const VMStateInfo vmstate_info_timer = { 193 .name = "timer", 194 .get = get_timer, 195 .put = put_timer, 196 }; 197 198 199 typedef struct CompatEntry { 200 char idstr[256]; 201 int instance_id; 202 } CompatEntry; 203 204 typedef struct SaveStateEntry { 205 QTAILQ_ENTRY(SaveStateEntry) entry; 206 char idstr[256]; 207 uint32_t instance_id; 208 int alias_id; 209 int version_id; 210 /* version id read from the stream */ 211 int load_version_id; 212 int section_id; 213 /* section id read from the stream */ 214 int load_section_id; 215 const SaveVMHandlers *ops; 216 const VMStateDescription *vmsd; 217 void *opaque; 218 CompatEntry *compat; 219 int is_ram; 220 } SaveStateEntry; 221 222 typedef struct SaveState { 223 QTAILQ_HEAD(, SaveStateEntry) handlers; 224 SaveStateEntry *handler_pri_head[MIG_PRI_MAX + 1]; 225 int global_section_id; 226 uint32_t len; 227 const char *name; 228 uint32_t target_page_bits; 229 uint32_t caps_count; 230 MigrationCapability *capabilities; 231 QemuUUID uuid; 232 } SaveState; 233 234 static SaveState savevm_state = { 235 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers), 236 .handler_pri_head = { [MIG_PRI_DEFAULT ... MIG_PRI_MAX] = NULL }, 237 .global_section_id = 0, 238 }; 239 240 static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id); 241 242 static bool should_validate_capability(int capability) 243 { 244 assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX); 245 /* Validate only new capabilities to keep compatibility. */ 246 switch (capability) { 247 case MIGRATION_CAPABILITY_X_IGNORE_SHARED: 248 return true; 249 default: 250 return false; 251 } 252 } 253 254 static uint32_t get_validatable_capabilities_count(void) 255 { 256 MigrationState *s = migrate_get_current(); 257 uint32_t result = 0; 258 int i; 259 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 260 if (should_validate_capability(i) && s->capabilities[i]) { 261 result++; 262 } 263 } 264 return result; 265 } 266 267 static int configuration_pre_save(void *opaque) 268 { 269 SaveState *state = opaque; 270 const char *current_name = MACHINE_GET_CLASS(current_machine)->name; 271 MigrationState *s = migrate_get_current(); 272 int i, j; 273 274 state->len = strlen(current_name); 275 state->name = current_name; 276 state->target_page_bits = qemu_target_page_bits(); 277 278 state->caps_count = get_validatable_capabilities_count(); 279 state->capabilities = g_renew(MigrationCapability, state->capabilities, 280 state->caps_count); 281 for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 282 if (should_validate_capability(i) && s->capabilities[i]) { 283 state->capabilities[j++] = i; 284 } 285 } 286 state->uuid = qemu_uuid; 287 288 return 0; 289 } 290 291 static int configuration_post_save(void *opaque) 292 { 293 SaveState *state = opaque; 294 295 g_free(state->capabilities); 296 state->capabilities = NULL; 297 state->caps_count = 0; 298 return 0; 299 } 300 301 static int configuration_pre_load(void *opaque) 302 { 303 SaveState *state = opaque; 304 305 /* If there is no target-page-bits subsection it means the source 306 * predates the variable-target-page-bits support and is using the 307 * minimum possible value for this CPU. 308 */ 309 state->target_page_bits = qemu_target_page_bits_min(); 310 return 0; 311 } 312 313 static bool configuration_validate_capabilities(SaveState *state) 314 { 315 bool ret = true; 316 MigrationState *s = migrate_get_current(); 317 unsigned long *source_caps_bm; 318 int i; 319 320 source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX); 321 for (i = 0; i < state->caps_count; i++) { 322 MigrationCapability capability = state->capabilities[i]; 323 set_bit(capability, source_caps_bm); 324 } 325 326 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 327 bool source_state, target_state; 328 if (!should_validate_capability(i)) { 329 continue; 330 } 331 source_state = test_bit(i, source_caps_bm); 332 target_state = s->capabilities[i]; 333 if (source_state != target_state) { 334 error_report("Capability %s is %s, but received capability is %s", 335 MigrationCapability_str(i), 336 target_state ? "on" : "off", 337 source_state ? "on" : "off"); 338 ret = false; 339 /* Don't break here to report all failed capabilities */ 340 } 341 } 342 343 g_free(source_caps_bm); 344 return ret; 345 } 346 347 static int configuration_post_load(void *opaque, int version_id) 348 { 349 SaveState *state = opaque; 350 const char *current_name = MACHINE_GET_CLASS(current_machine)->name; 351 int ret = 0; 352 353 if (strncmp(state->name, current_name, state->len) != 0) { 354 error_report("Machine type received is '%.*s' and local is '%s'", 355 (int) state->len, state->name, current_name); 356 ret = -EINVAL; 357 goto out; 358 } 359 360 if (state->target_page_bits != qemu_target_page_bits()) { 361 error_report("Received TARGET_PAGE_BITS is %d but local is %d", 362 state->target_page_bits, qemu_target_page_bits()); 363 ret = -EINVAL; 364 goto out; 365 } 366 367 if (!configuration_validate_capabilities(state)) { 368 ret = -EINVAL; 369 goto out; 370 } 371 372 out: 373 g_free((void *)state->name); 374 state->name = NULL; 375 state->len = 0; 376 g_free(state->capabilities); 377 state->capabilities = NULL; 378 state->caps_count = 0; 379 380 return ret; 381 } 382 383 static int get_capability(QEMUFile *f, void *pv, size_t size, 384 const VMStateField *field) 385 { 386 MigrationCapability *capability = pv; 387 char capability_str[UINT8_MAX + 1]; 388 uint8_t len; 389 int i; 390 391 len = qemu_get_byte(f); 392 qemu_get_buffer(f, (uint8_t *)capability_str, len); 393 capability_str[len] = '\0'; 394 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 395 if (!strcmp(MigrationCapability_str(i), capability_str)) { 396 *capability = i; 397 return 0; 398 } 399 } 400 error_report("Received unknown capability %s", capability_str); 401 return -EINVAL; 402 } 403 404 static int put_capability(QEMUFile *f, void *pv, size_t size, 405 const VMStateField *field, JSONWriter *vmdesc) 406 { 407 MigrationCapability *capability = pv; 408 const char *capability_str = MigrationCapability_str(*capability); 409 size_t len = strlen(capability_str); 410 assert(len <= UINT8_MAX); 411 412 qemu_put_byte(f, len); 413 qemu_put_buffer(f, (uint8_t *)capability_str, len); 414 return 0; 415 } 416 417 static const VMStateInfo vmstate_info_capability = { 418 .name = "capability", 419 .get = get_capability, 420 .put = put_capability, 421 }; 422 423 /* The target-page-bits subsection is present only if the 424 * target page size is not the same as the default (ie the 425 * minimum page size for a variable-page-size guest CPU). 426 * If it is present then it contains the actual target page 427 * bits for the machine, and migration will fail if the 428 * two ends don't agree about it. 429 */ 430 static bool vmstate_target_page_bits_needed(void *opaque) 431 { 432 return qemu_target_page_bits() 433 > qemu_target_page_bits_min(); 434 } 435 436 static const VMStateDescription vmstate_target_page_bits = { 437 .name = "configuration/target-page-bits", 438 .version_id = 1, 439 .minimum_version_id = 1, 440 .needed = vmstate_target_page_bits_needed, 441 .fields = (VMStateField[]) { 442 VMSTATE_UINT32(target_page_bits, SaveState), 443 VMSTATE_END_OF_LIST() 444 } 445 }; 446 447 static bool vmstate_capabilites_needed(void *opaque) 448 { 449 return get_validatable_capabilities_count() > 0; 450 } 451 452 static const VMStateDescription vmstate_capabilites = { 453 .name = "configuration/capabilities", 454 .version_id = 1, 455 .minimum_version_id = 1, 456 .needed = vmstate_capabilites_needed, 457 .fields = (VMStateField[]) { 458 VMSTATE_UINT32_V(caps_count, SaveState, 1), 459 VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1, 460 vmstate_info_capability, 461 MigrationCapability), 462 VMSTATE_END_OF_LIST() 463 } 464 }; 465 466 static bool vmstate_uuid_needed(void *opaque) 467 { 468 return qemu_uuid_set && migrate_validate_uuid(); 469 } 470 471 static int vmstate_uuid_post_load(void *opaque, int version_id) 472 { 473 SaveState *state = opaque; 474 char uuid_src[UUID_FMT_LEN + 1]; 475 char uuid_dst[UUID_FMT_LEN + 1]; 476 477 if (!qemu_uuid_set) { 478 /* 479 * It's warning because user might not know UUID in some cases, 480 * e.g. load an old snapshot 481 */ 482 qemu_uuid_unparse(&state->uuid, uuid_src); 483 warn_report("UUID is received %s, but local uuid isn't set", 484 uuid_src); 485 return 0; 486 } 487 if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) { 488 qemu_uuid_unparse(&state->uuid, uuid_src); 489 qemu_uuid_unparse(&qemu_uuid, uuid_dst); 490 error_report("UUID received is %s and local is %s", uuid_src, uuid_dst); 491 return -EINVAL; 492 } 493 return 0; 494 } 495 496 static const VMStateDescription vmstate_uuid = { 497 .name = "configuration/uuid", 498 .version_id = 1, 499 .minimum_version_id = 1, 500 .needed = vmstate_uuid_needed, 501 .post_load = vmstate_uuid_post_load, 502 .fields = (VMStateField[]) { 503 VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1), 504 VMSTATE_END_OF_LIST() 505 } 506 }; 507 508 static const VMStateDescription vmstate_configuration = { 509 .name = "configuration", 510 .version_id = 1, 511 .pre_load = configuration_pre_load, 512 .post_load = configuration_post_load, 513 .pre_save = configuration_pre_save, 514 .post_save = configuration_post_save, 515 .fields = (VMStateField[]) { 516 VMSTATE_UINT32(len, SaveState), 517 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len), 518 VMSTATE_END_OF_LIST() 519 }, 520 .subsections = (const VMStateDescription *[]) { 521 &vmstate_target_page_bits, 522 &vmstate_capabilites, 523 &vmstate_uuid, 524 NULL 525 } 526 }; 527 528 static void dump_vmstate_vmsd(FILE *out_file, 529 const VMStateDescription *vmsd, int indent, 530 bool is_subsection); 531 532 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field, 533 int indent) 534 { 535 fprintf(out_file, "%*s{\n", indent, ""); 536 indent += 2; 537 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name); 538 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", 539 field->version_id); 540 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "", 541 field->field_exists ? "true" : "false"); 542 if (field->flags & VMS_ARRAY) { 543 fprintf(out_file, "%*s\"num\": %d,\n", indent, "", field->num); 544 } 545 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size); 546 if (field->vmsd != NULL) { 547 fprintf(out_file, ",\n"); 548 dump_vmstate_vmsd(out_file, field->vmsd, indent, false); 549 } 550 fprintf(out_file, "\n%*s}", indent - 2, ""); 551 } 552 553 static void dump_vmstate_vmss(FILE *out_file, 554 const VMStateDescription **subsection, 555 int indent) 556 { 557 if (*subsection != NULL) { 558 dump_vmstate_vmsd(out_file, *subsection, indent, true); 559 } 560 } 561 562 static void dump_vmstate_vmsd(FILE *out_file, 563 const VMStateDescription *vmsd, int indent, 564 bool is_subsection) 565 { 566 if (is_subsection) { 567 fprintf(out_file, "%*s{\n", indent, ""); 568 } else { 569 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description"); 570 } 571 indent += 2; 572 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name); 573 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", 574 vmsd->version_id); 575 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "", 576 vmsd->minimum_version_id); 577 if (vmsd->fields != NULL) { 578 const VMStateField *field = vmsd->fields; 579 bool first; 580 581 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, ""); 582 first = true; 583 while (field->name != NULL) { 584 if (field->flags & VMS_MUST_EXIST) { 585 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */ 586 field++; 587 continue; 588 } 589 if (!first) { 590 fprintf(out_file, ",\n"); 591 } 592 dump_vmstate_vmsf(out_file, field, indent + 2); 593 field++; 594 first = false; 595 } 596 assert(field->flags == VMS_END); 597 fprintf(out_file, "\n%*s]", indent, ""); 598 } 599 if (vmsd->subsections != NULL) { 600 const VMStateDescription **subsection = vmsd->subsections; 601 bool first; 602 603 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, ""); 604 first = true; 605 while (*subsection != NULL) { 606 if (!first) { 607 fprintf(out_file, ",\n"); 608 } 609 dump_vmstate_vmss(out_file, subsection, indent + 2); 610 subsection++; 611 first = false; 612 } 613 fprintf(out_file, "\n%*s]", indent, ""); 614 } 615 fprintf(out_file, "\n%*s}", indent - 2, ""); 616 } 617 618 static void dump_machine_type(FILE *out_file) 619 { 620 MachineClass *mc; 621 622 mc = MACHINE_GET_CLASS(current_machine); 623 624 fprintf(out_file, " \"vmschkmachine\": {\n"); 625 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name); 626 fprintf(out_file, " },\n"); 627 } 628 629 void dump_vmstate_json_to_file(FILE *out_file) 630 { 631 GSList *list, *elt; 632 bool first; 633 634 fprintf(out_file, "{\n"); 635 dump_machine_type(out_file); 636 637 first = true; 638 list = object_class_get_list(TYPE_DEVICE, true); 639 for (elt = list; elt; elt = elt->next) { 640 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, 641 TYPE_DEVICE); 642 const char *name; 643 int indent = 2; 644 645 if (!dc->vmsd) { 646 continue; 647 } 648 649 if (!first) { 650 fprintf(out_file, ",\n"); 651 } 652 name = object_class_get_name(OBJECT_CLASS(dc)); 653 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name); 654 indent += 2; 655 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name); 656 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", 657 dc->vmsd->version_id); 658 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "", 659 dc->vmsd->minimum_version_id); 660 661 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false); 662 663 fprintf(out_file, "\n%*s}", indent - 2, ""); 664 first = false; 665 } 666 fprintf(out_file, "\n}\n"); 667 fclose(out_file); 668 g_slist_free(list); 669 } 670 671 static uint32_t calculate_new_instance_id(const char *idstr) 672 { 673 SaveStateEntry *se; 674 uint32_t instance_id = 0; 675 676 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 677 if (strcmp(idstr, se->idstr) == 0 678 && instance_id <= se->instance_id) { 679 instance_id = se->instance_id + 1; 680 } 681 } 682 /* Make sure we never loop over without being noticed */ 683 assert(instance_id != VMSTATE_INSTANCE_ID_ANY); 684 return instance_id; 685 } 686 687 static int calculate_compat_instance_id(const char *idstr) 688 { 689 SaveStateEntry *se; 690 int instance_id = 0; 691 692 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 693 if (!se->compat) { 694 continue; 695 } 696 697 if (strcmp(idstr, se->compat->idstr) == 0 698 && instance_id <= se->compat->instance_id) { 699 instance_id = se->compat->instance_id + 1; 700 } 701 } 702 return instance_id; 703 } 704 705 static inline MigrationPriority save_state_priority(SaveStateEntry *se) 706 { 707 if (se->vmsd) { 708 return se->vmsd->priority; 709 } 710 return MIG_PRI_DEFAULT; 711 } 712 713 static void savevm_state_handler_insert(SaveStateEntry *nse) 714 { 715 MigrationPriority priority = save_state_priority(nse); 716 SaveStateEntry *se; 717 int i; 718 719 assert(priority <= MIG_PRI_MAX); 720 721 /* 722 * This should never happen otherwise migration will probably fail 723 * silently somewhere because we can be wrongly applying one 724 * object properties upon another one. Bail out ASAP. 725 */ 726 if (find_se(nse->idstr, nse->instance_id)) { 727 error_report("%s: Detected duplicate SaveStateEntry: " 728 "id=%s, instance_id=0x%"PRIx32, __func__, 729 nse->idstr, nse->instance_id); 730 exit(EXIT_FAILURE); 731 } 732 733 for (i = priority - 1; i >= 0; i--) { 734 se = savevm_state.handler_pri_head[i]; 735 if (se != NULL) { 736 assert(save_state_priority(se) < priority); 737 break; 738 } 739 } 740 741 if (i >= 0) { 742 QTAILQ_INSERT_BEFORE(se, nse, entry); 743 } else { 744 QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry); 745 } 746 747 if (savevm_state.handler_pri_head[priority] == NULL) { 748 savevm_state.handler_pri_head[priority] = nse; 749 } 750 } 751 752 static void savevm_state_handler_remove(SaveStateEntry *se) 753 { 754 SaveStateEntry *next; 755 MigrationPriority priority = save_state_priority(se); 756 757 if (se == savevm_state.handler_pri_head[priority]) { 758 next = QTAILQ_NEXT(se, entry); 759 if (next != NULL && save_state_priority(next) == priority) { 760 savevm_state.handler_pri_head[priority] = next; 761 } else { 762 savevm_state.handler_pri_head[priority] = NULL; 763 } 764 } 765 QTAILQ_REMOVE(&savevm_state.handlers, se, entry); 766 } 767 768 /* TODO: Individual devices generally have very little idea about the rest 769 of the system, so instance_id should be removed/replaced. 770 Meanwhile pass -1 as instance_id if you do not already have a clearly 771 distinguishing id for all instances of your device class. */ 772 int register_savevm_live(const char *idstr, 773 uint32_t instance_id, 774 int version_id, 775 const SaveVMHandlers *ops, 776 void *opaque) 777 { 778 SaveStateEntry *se; 779 780 se = g_new0(SaveStateEntry, 1); 781 se->version_id = version_id; 782 se->section_id = savevm_state.global_section_id++; 783 se->ops = ops; 784 se->opaque = opaque; 785 se->vmsd = NULL; 786 /* if this is a live_savem then set is_ram */ 787 if (ops->save_setup != NULL) { 788 se->is_ram = 1; 789 } 790 791 pstrcat(se->idstr, sizeof(se->idstr), idstr); 792 793 if (instance_id == VMSTATE_INSTANCE_ID_ANY) { 794 se->instance_id = calculate_new_instance_id(se->idstr); 795 } else { 796 se->instance_id = instance_id; 797 } 798 assert(!se->compat || se->instance_id == 0); 799 savevm_state_handler_insert(se); 800 return 0; 801 } 802 803 void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque) 804 { 805 SaveStateEntry *se, *new_se; 806 char id[256] = ""; 807 808 if (obj) { 809 char *oid = vmstate_if_get_id(obj); 810 if (oid) { 811 pstrcpy(id, sizeof(id), oid); 812 pstrcat(id, sizeof(id), "/"); 813 g_free(oid); 814 } 815 } 816 pstrcat(id, sizeof(id), idstr); 817 818 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { 819 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { 820 savevm_state_handler_remove(se); 821 g_free(se->compat); 822 g_free(se); 823 } 824 } 825 } 826 827 /* 828 * Perform some basic checks on vmsd's at registration 829 * time. 830 */ 831 static void vmstate_check(const VMStateDescription *vmsd) 832 { 833 const VMStateField *field = vmsd->fields; 834 const VMStateDescription **subsection = vmsd->subsections; 835 836 if (field) { 837 while (field->name) { 838 if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) { 839 /* Recurse to sub structures */ 840 vmstate_check(field->vmsd); 841 } 842 /* Carry on */ 843 field++; 844 } 845 /* Check for the end of field list canary */ 846 if (field->flags != VMS_END) { 847 error_report("VMSTATE not ending with VMS_END: %s", vmsd->name); 848 g_assert_not_reached(); 849 } 850 } 851 852 while (subsection && *subsection) { 853 /* 854 * The name of a subsection should start with the name of the 855 * current object. 856 */ 857 assert(!strncmp(vmsd->name, (*subsection)->name, strlen(vmsd->name))); 858 vmstate_check(*subsection); 859 subsection++; 860 } 861 } 862 863 /* 864 * See comment in hw/intc/xics.c:icp_realize() 865 * 866 * This function can be removed when 867 * pre_2_10_vmstate_register_dummy_icp() is removed. 868 */ 869 int vmstate_replace_hack_for_ppc(VMStateIf *obj, int instance_id, 870 const VMStateDescription *vmsd, 871 void *opaque) 872 { 873 SaveStateEntry *se = find_se(vmsd->name, instance_id); 874 875 if (se) { 876 savevm_state_handler_remove(se); 877 } 878 return vmstate_register(obj, instance_id, vmsd, opaque); 879 } 880 881 int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id, 882 const VMStateDescription *vmsd, 883 void *opaque, int alias_id, 884 int required_for_version, 885 Error **errp) 886 { 887 SaveStateEntry *se; 888 889 /* If this triggers, alias support can be dropped for the vmsd. */ 890 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); 891 892 se = g_new0(SaveStateEntry, 1); 893 se->version_id = vmsd->version_id; 894 se->section_id = savevm_state.global_section_id++; 895 se->opaque = opaque; 896 se->vmsd = vmsd; 897 se->alias_id = alias_id; 898 899 if (obj) { 900 char *id = vmstate_if_get_id(obj); 901 if (id) { 902 if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >= 903 sizeof(se->idstr)) { 904 error_setg(errp, "Path too long for VMState (%s)", id); 905 g_free(id); 906 g_free(se); 907 908 return -1; 909 } 910 g_free(id); 911 912 se->compat = g_new0(CompatEntry, 1); 913 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); 914 se->compat->instance_id = instance_id == VMSTATE_INSTANCE_ID_ANY ? 915 calculate_compat_instance_id(vmsd->name) : instance_id; 916 instance_id = VMSTATE_INSTANCE_ID_ANY; 917 } 918 } 919 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); 920 921 if (instance_id == VMSTATE_INSTANCE_ID_ANY) { 922 se->instance_id = calculate_new_instance_id(se->idstr); 923 } else { 924 se->instance_id = instance_id; 925 } 926 927 /* Perform a recursive sanity check during the test runs */ 928 if (qtest_enabled()) { 929 vmstate_check(vmsd); 930 } 931 assert(!se->compat || se->instance_id == 0); 932 savevm_state_handler_insert(se); 933 return 0; 934 } 935 936 void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd, 937 void *opaque) 938 { 939 SaveStateEntry *se, *new_se; 940 941 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { 942 if (se->vmsd == vmsd && se->opaque == opaque) { 943 savevm_state_handler_remove(se); 944 g_free(se->compat); 945 g_free(se); 946 } 947 } 948 } 949 950 static int vmstate_load(QEMUFile *f, SaveStateEntry *se) 951 { 952 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); 953 if (!se->vmsd) { /* Old style */ 954 return se->ops->load_state(f, se->opaque, se->load_version_id); 955 } 956 return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id); 957 } 958 959 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, 960 JSONWriter *vmdesc) 961 { 962 uint64_t old_offset = qemu_file_transferred(f); 963 se->ops->save_state(f, se->opaque); 964 uint64_t size = qemu_file_transferred(f) - old_offset; 965 966 if (vmdesc) { 967 json_writer_int64(vmdesc, "size", size); 968 json_writer_start_array(vmdesc, "fields"); 969 json_writer_start_object(vmdesc, NULL); 970 json_writer_str(vmdesc, "name", "data"); 971 json_writer_int64(vmdesc, "size", size); 972 json_writer_str(vmdesc, "type", "buffer"); 973 json_writer_end_object(vmdesc); 974 json_writer_end_array(vmdesc); 975 } 976 } 977 978 /* 979 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL) 980 */ 981 static void save_section_header(QEMUFile *f, SaveStateEntry *se, 982 uint8_t section_type) 983 { 984 qemu_put_byte(f, section_type); 985 qemu_put_be32(f, se->section_id); 986 987 if (section_type == QEMU_VM_SECTION_FULL || 988 section_type == QEMU_VM_SECTION_START) { 989 /* ID string */ 990 size_t len = strlen(se->idstr); 991 qemu_put_byte(f, len); 992 qemu_put_buffer(f, (uint8_t *)se->idstr, len); 993 994 qemu_put_be32(f, se->instance_id); 995 qemu_put_be32(f, se->version_id); 996 } 997 } 998 999 /* 1000 * Write a footer onto device sections that catches cases misformatted device 1001 * sections. 1002 */ 1003 static void save_section_footer(QEMUFile *f, SaveStateEntry *se) 1004 { 1005 if (migrate_get_current()->send_section_footer) { 1006 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER); 1007 qemu_put_be32(f, se->section_id); 1008 } 1009 } 1010 1011 static int vmstate_save(QEMUFile *f, SaveStateEntry *se, JSONWriter *vmdesc) 1012 { 1013 int ret; 1014 Error *local_err = NULL; 1015 MigrationState *s = migrate_get_current(); 1016 1017 if ((!se->ops || !se->ops->save_state) && !se->vmsd) { 1018 return 0; 1019 } 1020 if (se->vmsd && !vmstate_section_needed(se->vmsd, se->opaque)) { 1021 trace_savevm_section_skip(se->idstr, se->section_id); 1022 return 0; 1023 } 1024 1025 trace_savevm_section_start(se->idstr, se->section_id); 1026 save_section_header(f, se, QEMU_VM_SECTION_FULL); 1027 if (vmdesc) { 1028 json_writer_start_object(vmdesc, NULL); 1029 json_writer_str(vmdesc, "name", se->idstr); 1030 json_writer_int64(vmdesc, "instance_id", se->instance_id); 1031 } 1032 1033 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); 1034 if (!se->vmsd) { 1035 vmstate_save_old_style(f, se, vmdesc); 1036 } else { 1037 ret = vmstate_save_state_with_err(f, se->vmsd, se->opaque, vmdesc, &local_err); 1038 if (ret) { 1039 migrate_set_error(s, local_err); 1040 error_report_err(local_err); 1041 return ret; 1042 } 1043 } 1044 1045 trace_savevm_section_end(se->idstr, se->section_id, 0); 1046 save_section_footer(f, se); 1047 if (vmdesc) { 1048 json_writer_end_object(vmdesc); 1049 } 1050 return 0; 1051 } 1052 /** 1053 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the 1054 * command and associated data. 1055 * 1056 * @f: File to send command on 1057 * @command: Command type to send 1058 * @len: Length of associated data 1059 * @data: Data associated with command. 1060 */ 1061 static void qemu_savevm_command_send(QEMUFile *f, 1062 enum qemu_vm_cmd command, 1063 uint16_t len, 1064 uint8_t *data) 1065 { 1066 trace_savevm_command_send(command, len); 1067 qemu_put_byte(f, QEMU_VM_COMMAND); 1068 qemu_put_be16(f, (uint16_t)command); 1069 qemu_put_be16(f, len); 1070 qemu_put_buffer(f, data, len); 1071 qemu_fflush(f); 1072 } 1073 1074 void qemu_savevm_send_colo_enable(QEMUFile *f) 1075 { 1076 trace_savevm_send_colo_enable(); 1077 qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL); 1078 } 1079 1080 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value) 1081 { 1082 uint32_t buf; 1083 1084 trace_savevm_send_ping(value); 1085 buf = cpu_to_be32(value); 1086 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf); 1087 } 1088 1089 void qemu_savevm_send_open_return_path(QEMUFile *f) 1090 { 1091 trace_savevm_send_open_return_path(); 1092 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL); 1093 } 1094 1095 /* We have a buffer of data to send; we don't want that all to be loaded 1096 * by the command itself, so the command contains just the length of the 1097 * extra buffer that we then send straight after it. 1098 * TODO: Must be a better way to organise that 1099 * 1100 * Returns: 1101 * 0 on success 1102 * -ve on error 1103 */ 1104 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len) 1105 { 1106 uint32_t tmp; 1107 MigrationState *ms = migrate_get_current(); 1108 Error *local_err = NULL; 1109 1110 if (len > MAX_VM_CMD_PACKAGED_SIZE) { 1111 error_setg(&local_err, "%s: Unreasonably large packaged state: %zu", 1112 __func__, len); 1113 migrate_set_error(ms, local_err); 1114 error_report_err(local_err); 1115 return -1; 1116 } 1117 1118 tmp = cpu_to_be32(len); 1119 1120 trace_qemu_savevm_send_packaged(); 1121 qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp); 1122 1123 qemu_put_buffer(f, buf, len); 1124 1125 return 0; 1126 } 1127 1128 /* Send prior to any postcopy transfer */ 1129 void qemu_savevm_send_postcopy_advise(QEMUFile *f) 1130 { 1131 if (migrate_postcopy_ram()) { 1132 uint64_t tmp[2]; 1133 tmp[0] = cpu_to_be64(ram_pagesize_summary()); 1134 tmp[1] = cpu_to_be64(qemu_target_page_size()); 1135 1136 trace_qemu_savevm_send_postcopy_advise(); 1137 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 1138 16, (uint8_t *)tmp); 1139 } else { 1140 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL); 1141 } 1142 } 1143 1144 /* Sent prior to starting the destination running in postcopy, discard pages 1145 * that have already been sent but redirtied on the source. 1146 * CMD_POSTCOPY_RAM_DISCARD consist of: 1147 * byte version (0) 1148 * byte Length of name field (not including 0) 1149 * n x byte RAM block name 1150 * byte 0 terminator (just for safety) 1151 * n x Byte ranges within the named RAMBlock 1152 * be64 Start of the range 1153 * be64 Length 1154 * 1155 * name: RAMBlock name that these entries are part of 1156 * len: Number of page entries 1157 * start_list: 'len' addresses 1158 * length_list: 'len' addresses 1159 * 1160 */ 1161 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name, 1162 uint16_t len, 1163 uint64_t *start_list, 1164 uint64_t *length_list) 1165 { 1166 uint8_t *buf; 1167 uint16_t tmplen; 1168 uint16_t t; 1169 size_t name_len = strlen(name); 1170 1171 trace_qemu_savevm_send_postcopy_ram_discard(name, len); 1172 assert(name_len < 256); 1173 buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len); 1174 buf[0] = postcopy_ram_discard_version; 1175 buf[1] = name_len; 1176 memcpy(buf + 2, name, name_len); 1177 tmplen = 2 + name_len; 1178 buf[tmplen++] = '\0'; 1179 1180 for (t = 0; t < len; t++) { 1181 stq_be_p(buf + tmplen, start_list[t]); 1182 tmplen += 8; 1183 stq_be_p(buf + tmplen, length_list[t]); 1184 tmplen += 8; 1185 } 1186 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf); 1187 g_free(buf); 1188 } 1189 1190 /* Get the destination into a state where it can receive postcopy data. */ 1191 void qemu_savevm_send_postcopy_listen(QEMUFile *f) 1192 { 1193 trace_savevm_send_postcopy_listen(); 1194 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL); 1195 } 1196 1197 /* Kick the destination into running */ 1198 void qemu_savevm_send_postcopy_run(QEMUFile *f) 1199 { 1200 trace_savevm_send_postcopy_run(); 1201 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL); 1202 } 1203 1204 void qemu_savevm_send_postcopy_resume(QEMUFile *f) 1205 { 1206 trace_savevm_send_postcopy_resume(); 1207 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL); 1208 } 1209 1210 void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name) 1211 { 1212 size_t len; 1213 char buf[256]; 1214 1215 trace_savevm_send_recv_bitmap(block_name); 1216 1217 buf[0] = len = strlen(block_name); 1218 memcpy(buf + 1, block_name, len); 1219 1220 qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf); 1221 } 1222 1223 bool qemu_savevm_state_blocked(Error **errp) 1224 { 1225 SaveStateEntry *se; 1226 1227 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1228 if (se->vmsd && se->vmsd->unmigratable) { 1229 error_setg(errp, "State blocked by non-migratable device '%s'", 1230 se->idstr); 1231 return true; 1232 } 1233 } 1234 return false; 1235 } 1236 1237 void qemu_savevm_non_migratable_list(strList **reasons) 1238 { 1239 SaveStateEntry *se; 1240 1241 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1242 if (se->vmsd && se->vmsd->unmigratable) { 1243 QAPI_LIST_PREPEND(*reasons, 1244 g_strdup_printf("non-migratable device: %s", 1245 se->idstr)); 1246 } 1247 } 1248 } 1249 1250 void qemu_savevm_state_header(QEMUFile *f) 1251 { 1252 MigrationState *s = migrate_get_current(); 1253 1254 s->vmdesc = json_writer_new(false); 1255 1256 trace_savevm_state_header(); 1257 qemu_put_be32(f, QEMU_VM_FILE_MAGIC); 1258 qemu_put_be32(f, QEMU_VM_FILE_VERSION); 1259 1260 if (s->send_configuration) { 1261 qemu_put_byte(f, QEMU_VM_CONFIGURATION); 1262 1263 /* 1264 * This starts the main json object and is paired with the 1265 * json_writer_end_object in 1266 * qemu_savevm_state_complete_precopy_non_iterable 1267 */ 1268 json_writer_start_object(s->vmdesc, NULL); 1269 1270 json_writer_start_object(s->vmdesc, "configuration"); 1271 vmstate_save_state(f, &vmstate_configuration, &savevm_state, s->vmdesc); 1272 json_writer_end_object(s->vmdesc); 1273 } 1274 } 1275 1276 bool qemu_savevm_state_guest_unplug_pending(void) 1277 { 1278 SaveStateEntry *se; 1279 1280 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1281 if (se->vmsd && se->vmsd->dev_unplug_pending && 1282 se->vmsd->dev_unplug_pending(se->opaque)) { 1283 return true; 1284 } 1285 } 1286 1287 return false; 1288 } 1289 1290 int qemu_savevm_state_prepare(Error **errp) 1291 { 1292 SaveStateEntry *se; 1293 int ret; 1294 1295 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1296 if (!se->ops || !se->ops->save_prepare) { 1297 continue; 1298 } 1299 if (se->ops->is_active) { 1300 if (!se->ops->is_active(se->opaque)) { 1301 continue; 1302 } 1303 } 1304 1305 ret = se->ops->save_prepare(se->opaque, errp); 1306 if (ret < 0) { 1307 return ret; 1308 } 1309 } 1310 1311 return 0; 1312 } 1313 1314 void qemu_savevm_state_setup(QEMUFile *f) 1315 { 1316 MigrationState *ms = migrate_get_current(); 1317 SaveStateEntry *se; 1318 Error *local_err = NULL; 1319 int ret; 1320 1321 json_writer_int64(ms->vmdesc, "page_size", qemu_target_page_size()); 1322 json_writer_start_array(ms->vmdesc, "devices"); 1323 1324 trace_savevm_state_setup(); 1325 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1326 if (se->vmsd && se->vmsd->early_setup) { 1327 ret = vmstate_save(f, se, ms->vmdesc); 1328 if (ret) { 1329 qemu_file_set_error(f, ret); 1330 break; 1331 } 1332 continue; 1333 } 1334 1335 if (!se->ops || !se->ops->save_setup) { 1336 continue; 1337 } 1338 if (se->ops->is_active) { 1339 if (!se->ops->is_active(se->opaque)) { 1340 continue; 1341 } 1342 } 1343 save_section_header(f, se, QEMU_VM_SECTION_START); 1344 1345 ret = se->ops->save_setup(f, se->opaque); 1346 save_section_footer(f, se); 1347 if (ret < 0) { 1348 qemu_file_set_error(f, ret); 1349 break; 1350 } 1351 } 1352 1353 if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) { 1354 error_report_err(local_err); 1355 } 1356 } 1357 1358 int qemu_savevm_state_resume_prepare(MigrationState *s) 1359 { 1360 SaveStateEntry *se; 1361 int ret; 1362 1363 trace_savevm_state_resume_prepare(); 1364 1365 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1366 if (!se->ops || !se->ops->resume_prepare) { 1367 continue; 1368 } 1369 if (se->ops->is_active) { 1370 if (!se->ops->is_active(se->opaque)) { 1371 continue; 1372 } 1373 } 1374 ret = se->ops->resume_prepare(s, se->opaque); 1375 if (ret < 0) { 1376 return ret; 1377 } 1378 } 1379 1380 return 0; 1381 } 1382 1383 /* 1384 * this function has three return values: 1385 * negative: there was one error, and we have -errno. 1386 * 0 : We haven't finished, caller have to go again 1387 * 1 : We have finished, we can go to complete phase 1388 */ 1389 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy) 1390 { 1391 SaveStateEntry *se; 1392 int ret = 1; 1393 1394 trace_savevm_state_iterate(); 1395 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1396 if (!se->ops || !se->ops->save_live_iterate) { 1397 continue; 1398 } 1399 if (se->ops->is_active && 1400 !se->ops->is_active(se->opaque)) { 1401 continue; 1402 } 1403 if (se->ops->is_active_iterate && 1404 !se->ops->is_active_iterate(se->opaque)) { 1405 continue; 1406 } 1407 /* 1408 * In the postcopy phase, any device that doesn't know how to 1409 * do postcopy should have saved it's state in the _complete 1410 * call that's already run, it might get confused if we call 1411 * iterate afterwards. 1412 */ 1413 if (postcopy && 1414 !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) { 1415 continue; 1416 } 1417 if (migration_rate_exceeded(f)) { 1418 return 0; 1419 } 1420 trace_savevm_section_start(se->idstr, se->section_id); 1421 1422 save_section_header(f, se, QEMU_VM_SECTION_PART); 1423 1424 ret = se->ops->save_live_iterate(f, se->opaque); 1425 trace_savevm_section_end(se->idstr, se->section_id, ret); 1426 save_section_footer(f, se); 1427 1428 if (ret < 0) { 1429 error_report("failed to save SaveStateEntry with id(name): " 1430 "%d(%s): %d", 1431 se->section_id, se->idstr, ret); 1432 qemu_file_set_error(f, ret); 1433 } 1434 if (ret <= 0) { 1435 /* Do not proceed to the next vmstate before this one reported 1436 completion of the current stage. This serializes the migration 1437 and reduces the probability that a faster changing state is 1438 synchronized over and over again. */ 1439 break; 1440 } 1441 } 1442 return ret; 1443 } 1444 1445 static bool should_send_vmdesc(void) 1446 { 1447 MachineState *machine = MACHINE(qdev_get_machine()); 1448 bool in_postcopy = migration_in_postcopy(); 1449 return !machine->suppress_vmdesc && !in_postcopy; 1450 } 1451 1452 /* 1453 * Calls the save_live_complete_postcopy methods 1454 * causing the last few pages to be sent immediately and doing any associated 1455 * cleanup. 1456 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete 1457 * all the other devices, but that happens at the point we switch to postcopy. 1458 */ 1459 void qemu_savevm_state_complete_postcopy(QEMUFile *f) 1460 { 1461 SaveStateEntry *se; 1462 int ret; 1463 1464 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1465 if (!se->ops || !se->ops->save_live_complete_postcopy) { 1466 continue; 1467 } 1468 if (se->ops->is_active) { 1469 if (!se->ops->is_active(se->opaque)) { 1470 continue; 1471 } 1472 } 1473 trace_savevm_section_start(se->idstr, se->section_id); 1474 /* Section type */ 1475 qemu_put_byte(f, QEMU_VM_SECTION_END); 1476 qemu_put_be32(f, se->section_id); 1477 1478 ret = se->ops->save_live_complete_postcopy(f, se->opaque); 1479 trace_savevm_section_end(se->idstr, se->section_id, ret); 1480 save_section_footer(f, se); 1481 if (ret < 0) { 1482 qemu_file_set_error(f, ret); 1483 return; 1484 } 1485 } 1486 1487 qemu_put_byte(f, QEMU_VM_EOF); 1488 qemu_fflush(f); 1489 } 1490 1491 static 1492 int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy) 1493 { 1494 SaveStateEntry *se; 1495 int ret; 1496 1497 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1498 if (!se->ops || 1499 (in_postcopy && se->ops->has_postcopy && 1500 se->ops->has_postcopy(se->opaque)) || 1501 !se->ops->save_live_complete_precopy) { 1502 continue; 1503 } 1504 1505 if (se->ops->is_active) { 1506 if (!se->ops->is_active(se->opaque)) { 1507 continue; 1508 } 1509 } 1510 trace_savevm_section_start(se->idstr, se->section_id); 1511 1512 save_section_header(f, se, QEMU_VM_SECTION_END); 1513 1514 ret = se->ops->save_live_complete_precopy(f, se->opaque); 1515 trace_savevm_section_end(se->idstr, se->section_id, ret); 1516 save_section_footer(f, se); 1517 if (ret < 0) { 1518 qemu_file_set_error(f, ret); 1519 return -1; 1520 } 1521 } 1522 1523 return 0; 1524 } 1525 1526 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f, 1527 bool in_postcopy, 1528 bool inactivate_disks) 1529 { 1530 MigrationState *ms = migrate_get_current(); 1531 JSONWriter *vmdesc = ms->vmdesc; 1532 int vmdesc_len; 1533 SaveStateEntry *se; 1534 int ret; 1535 1536 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1537 if (se->vmsd && se->vmsd->early_setup) { 1538 /* Already saved during qemu_savevm_state_setup(). */ 1539 continue; 1540 } 1541 1542 ret = vmstate_save(f, se, vmdesc); 1543 if (ret) { 1544 qemu_file_set_error(f, ret); 1545 return ret; 1546 } 1547 } 1548 1549 if (inactivate_disks) { 1550 /* Inactivate before sending QEMU_VM_EOF so that the 1551 * bdrv_activate_all() on the other end won't fail. */ 1552 ret = bdrv_inactivate_all(); 1553 if (ret) { 1554 Error *local_err = NULL; 1555 error_setg(&local_err, "%s: bdrv_inactivate_all() failed (%d)", 1556 __func__, ret); 1557 migrate_set_error(ms, local_err); 1558 error_report_err(local_err); 1559 qemu_file_set_error(f, ret); 1560 return ret; 1561 } 1562 } 1563 if (!in_postcopy) { 1564 /* Postcopy stream will still be going */ 1565 qemu_put_byte(f, QEMU_VM_EOF); 1566 } 1567 1568 json_writer_end_array(vmdesc); 1569 json_writer_end_object(vmdesc); 1570 vmdesc_len = strlen(json_writer_get(vmdesc)); 1571 1572 if (should_send_vmdesc()) { 1573 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION); 1574 qemu_put_be32(f, vmdesc_len); 1575 qemu_put_buffer(f, (uint8_t *)json_writer_get(vmdesc), vmdesc_len); 1576 } 1577 1578 /* Free it now to detect any inconsistencies. */ 1579 json_writer_free(vmdesc); 1580 ms->vmdesc = NULL; 1581 1582 return 0; 1583 } 1584 1585 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only, 1586 bool inactivate_disks) 1587 { 1588 int ret; 1589 Error *local_err = NULL; 1590 bool in_postcopy = migration_in_postcopy(); 1591 1592 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) { 1593 error_report_err(local_err); 1594 } 1595 1596 trace_savevm_state_complete_precopy(); 1597 1598 cpu_synchronize_all_states(); 1599 1600 if (!in_postcopy || iterable_only) { 1601 ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy); 1602 if (ret) { 1603 return ret; 1604 } 1605 } 1606 1607 if (iterable_only) { 1608 goto flush; 1609 } 1610 1611 ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy, 1612 inactivate_disks); 1613 if (ret) { 1614 return ret; 1615 } 1616 1617 flush: 1618 return qemu_fflush(f); 1619 } 1620 1621 /* Give an estimate of the amount left to be transferred, 1622 * the result is split into the amount for units that can and 1623 * for units that can't do postcopy. 1624 */ 1625 void qemu_savevm_state_pending_estimate(uint64_t *must_precopy, 1626 uint64_t *can_postcopy) 1627 { 1628 SaveStateEntry *se; 1629 1630 *must_precopy = 0; 1631 *can_postcopy = 0; 1632 1633 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1634 if (!se->ops || !se->ops->state_pending_estimate) { 1635 continue; 1636 } 1637 if (se->ops->is_active) { 1638 if (!se->ops->is_active(se->opaque)) { 1639 continue; 1640 } 1641 } 1642 se->ops->state_pending_estimate(se->opaque, must_precopy, can_postcopy); 1643 } 1644 } 1645 1646 void qemu_savevm_state_pending_exact(uint64_t *must_precopy, 1647 uint64_t *can_postcopy) 1648 { 1649 SaveStateEntry *se; 1650 1651 *must_precopy = 0; 1652 *can_postcopy = 0; 1653 1654 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1655 if (!se->ops || !se->ops->state_pending_exact) { 1656 continue; 1657 } 1658 if (se->ops->is_active) { 1659 if (!se->ops->is_active(se->opaque)) { 1660 continue; 1661 } 1662 } 1663 se->ops->state_pending_exact(se->opaque, must_precopy, can_postcopy); 1664 } 1665 } 1666 1667 void qemu_savevm_state_cleanup(void) 1668 { 1669 SaveStateEntry *se; 1670 Error *local_err = NULL; 1671 1672 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) { 1673 error_report_err(local_err); 1674 } 1675 1676 trace_savevm_state_cleanup(); 1677 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1678 if (se->ops && se->ops->save_cleanup) { 1679 se->ops->save_cleanup(se->opaque); 1680 } 1681 } 1682 } 1683 1684 static int qemu_savevm_state(QEMUFile *f, Error **errp) 1685 { 1686 int ret; 1687 MigrationState *ms = migrate_get_current(); 1688 MigrationStatus status; 1689 1690 if (migration_is_running(ms->state)) { 1691 error_setg(errp, QERR_MIGRATION_ACTIVE); 1692 return -EINVAL; 1693 } 1694 1695 if (migrate_block()) { 1696 error_setg(errp, "Block migration and snapshots are incompatible"); 1697 return -EINVAL; 1698 } 1699 1700 ret = migrate_init(ms, errp); 1701 if (ret) { 1702 return ret; 1703 } 1704 ms->to_dst_file = f; 1705 1706 qemu_savevm_state_header(f); 1707 qemu_savevm_state_setup(f); 1708 1709 while (qemu_file_get_error(f) == 0) { 1710 if (qemu_savevm_state_iterate(f, false) > 0) { 1711 break; 1712 } 1713 } 1714 1715 ret = qemu_file_get_error(f); 1716 if (ret == 0) { 1717 qemu_savevm_state_complete_precopy(f, false, false); 1718 ret = qemu_file_get_error(f); 1719 } 1720 qemu_savevm_state_cleanup(); 1721 if (ret != 0) { 1722 error_setg_errno(errp, -ret, "Error while writing VM state"); 1723 } 1724 1725 if (ret != 0) { 1726 status = MIGRATION_STATUS_FAILED; 1727 } else { 1728 status = MIGRATION_STATUS_COMPLETED; 1729 } 1730 migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status); 1731 1732 /* f is outer parameter, it should not stay in global migration state after 1733 * this function finished */ 1734 ms->to_dst_file = NULL; 1735 1736 return ret; 1737 } 1738 1739 void qemu_savevm_live_state(QEMUFile *f) 1740 { 1741 /* save QEMU_VM_SECTION_END section */ 1742 qemu_savevm_state_complete_precopy(f, true, false); 1743 qemu_put_byte(f, QEMU_VM_EOF); 1744 } 1745 1746 int qemu_save_device_state(QEMUFile *f) 1747 { 1748 SaveStateEntry *se; 1749 1750 if (!migration_in_colo_state()) { 1751 qemu_put_be32(f, QEMU_VM_FILE_MAGIC); 1752 qemu_put_be32(f, QEMU_VM_FILE_VERSION); 1753 } 1754 cpu_synchronize_all_states(); 1755 1756 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1757 int ret; 1758 1759 if (se->is_ram) { 1760 continue; 1761 } 1762 ret = vmstate_save(f, se, NULL); 1763 if (ret) { 1764 return ret; 1765 } 1766 } 1767 1768 qemu_put_byte(f, QEMU_VM_EOF); 1769 1770 return qemu_file_get_error(f); 1771 } 1772 1773 static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id) 1774 { 1775 SaveStateEntry *se; 1776 1777 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 1778 if (!strcmp(se->idstr, idstr) && 1779 (instance_id == se->instance_id || 1780 instance_id == se->alias_id)) 1781 return se; 1782 /* Migrating from an older version? */ 1783 if (strstr(se->idstr, idstr) && se->compat) { 1784 if (!strcmp(se->compat->idstr, idstr) && 1785 (instance_id == se->compat->instance_id || 1786 instance_id == se->alias_id)) 1787 return se; 1788 } 1789 } 1790 return NULL; 1791 } 1792 1793 enum LoadVMExitCodes { 1794 /* Allow a command to quit all layers of nested loadvm loops */ 1795 LOADVM_QUIT = 1, 1796 }; 1797 1798 /* ------ incoming postcopy messages ------ */ 1799 /* 'advise' arrives before any transfers just to tell us that a postcopy 1800 * *might* happen - it might be skipped if precopy transferred everything 1801 * quickly. 1802 */ 1803 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis, 1804 uint16_t len) 1805 { 1806 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE); 1807 uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps; 1808 size_t page_size = qemu_target_page_size(); 1809 Error *local_err = NULL; 1810 1811 trace_loadvm_postcopy_handle_advise(); 1812 if (ps != POSTCOPY_INCOMING_NONE) { 1813 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps); 1814 return -1; 1815 } 1816 1817 switch (len) { 1818 case 0: 1819 if (migrate_postcopy_ram()) { 1820 error_report("RAM postcopy is enabled but have 0 byte advise"); 1821 return -EINVAL; 1822 } 1823 return 0; 1824 case 8 + 8: 1825 if (!migrate_postcopy_ram()) { 1826 error_report("RAM postcopy is disabled but have 16 byte advise"); 1827 return -EINVAL; 1828 } 1829 break; 1830 default: 1831 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len); 1832 return -EINVAL; 1833 } 1834 1835 if (!postcopy_ram_supported_by_host(mis, &local_err)) { 1836 error_report_err(local_err); 1837 postcopy_state_set(POSTCOPY_INCOMING_NONE); 1838 return -1; 1839 } 1840 1841 remote_pagesize_summary = qemu_get_be64(mis->from_src_file); 1842 local_pagesize_summary = ram_pagesize_summary(); 1843 1844 if (remote_pagesize_summary != local_pagesize_summary) { 1845 /* 1846 * This detects two potential causes of mismatch: 1847 * a) A mismatch in host page sizes 1848 * Some combinations of mismatch are probably possible but it gets 1849 * a bit more complicated. In particular we need to place whole 1850 * host pages on the dest at once, and we need to ensure that we 1851 * handle dirtying to make sure we never end up sending part of 1852 * a hostpage on it's own. 1853 * b) The use of different huge page sizes on source/destination 1854 * a more fine grain test is performed during RAM block migration 1855 * but this test here causes a nice early clear failure, and 1856 * also fails when passed to an older qemu that doesn't 1857 * do huge pages. 1858 */ 1859 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64 1860 " d=%" PRIx64 ")", 1861 remote_pagesize_summary, local_pagesize_summary); 1862 return -1; 1863 } 1864 1865 remote_tps = qemu_get_be64(mis->from_src_file); 1866 if (remote_tps != page_size) { 1867 /* 1868 * Again, some differences could be dealt with, but for now keep it 1869 * simple. 1870 */ 1871 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)", 1872 (int)remote_tps, page_size); 1873 return -1; 1874 } 1875 1876 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) { 1877 error_report_err(local_err); 1878 return -1; 1879 } 1880 1881 if (ram_postcopy_incoming_init(mis)) { 1882 return -1; 1883 } 1884 1885 return 0; 1886 } 1887 1888 /* After postcopy we will be told to throw some pages away since they're 1889 * dirty and will have to be demand fetched. Must happen before CPU is 1890 * started. 1891 * There can be 0..many of these messages, each encoding multiple pages. 1892 */ 1893 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis, 1894 uint16_t len) 1895 { 1896 int tmp; 1897 char ramid[256]; 1898 PostcopyState ps = postcopy_state_get(); 1899 1900 trace_loadvm_postcopy_ram_handle_discard(); 1901 1902 switch (ps) { 1903 case POSTCOPY_INCOMING_ADVISE: 1904 /* 1st discard */ 1905 tmp = postcopy_ram_prepare_discard(mis); 1906 if (tmp) { 1907 return tmp; 1908 } 1909 break; 1910 1911 case POSTCOPY_INCOMING_DISCARD: 1912 /* Expected state */ 1913 break; 1914 1915 default: 1916 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)", 1917 ps); 1918 return -1; 1919 } 1920 /* We're expecting a 1921 * Version (0) 1922 * a RAM ID string (length byte, name, 0 term) 1923 * then at least 1 16 byte chunk 1924 */ 1925 if (len < (1 + 1 + 1 + 1 + 2 * 8)) { 1926 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); 1927 return -1; 1928 } 1929 1930 tmp = qemu_get_byte(mis->from_src_file); 1931 if (tmp != postcopy_ram_discard_version) { 1932 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp); 1933 return -1; 1934 } 1935 1936 if (!qemu_get_counted_string(mis->from_src_file, ramid)) { 1937 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID"); 1938 return -1; 1939 } 1940 tmp = qemu_get_byte(mis->from_src_file); 1941 if (tmp != 0) { 1942 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp); 1943 return -1; 1944 } 1945 1946 len -= 3 + strlen(ramid); 1947 if (len % 16) { 1948 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len); 1949 return -1; 1950 } 1951 trace_loadvm_postcopy_ram_handle_discard_header(ramid, len); 1952 while (len) { 1953 uint64_t start_addr, block_length; 1954 start_addr = qemu_get_be64(mis->from_src_file); 1955 block_length = qemu_get_be64(mis->from_src_file); 1956 1957 len -= 16; 1958 int ret = ram_discard_range(ramid, start_addr, block_length); 1959 if (ret) { 1960 return ret; 1961 } 1962 } 1963 trace_loadvm_postcopy_ram_handle_discard_end(); 1964 1965 return 0; 1966 } 1967 1968 /* 1969 * Triggered by a postcopy_listen command; this thread takes over reading 1970 * the input stream, leaving the main thread free to carry on loading the rest 1971 * of the device state (from RAM). 1972 * (TODO:This could do with being in a postcopy file - but there again it's 1973 * just another input loop, not that postcopy specific) 1974 */ 1975 static void *postcopy_ram_listen_thread(void *opaque) 1976 { 1977 MigrationIncomingState *mis = migration_incoming_get_current(); 1978 QEMUFile *f = mis->from_src_file; 1979 int load_res; 1980 MigrationState *migr = migrate_get_current(); 1981 1982 object_ref(OBJECT(migr)); 1983 1984 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 1985 MIGRATION_STATUS_POSTCOPY_ACTIVE); 1986 qemu_sem_post(&mis->thread_sync_sem); 1987 trace_postcopy_ram_listen_thread_start(); 1988 1989 rcu_register_thread(); 1990 /* 1991 * Because we're a thread and not a coroutine we can't yield 1992 * in qemu_file, and thus we must be blocking now. 1993 */ 1994 qemu_file_set_blocking(f, true); 1995 load_res = qemu_loadvm_state_main(f, mis); 1996 1997 /* 1998 * This is tricky, but, mis->from_src_file can change after it 1999 * returns, when postcopy recovery happened. In the future, we may 2000 * want a wrapper for the QEMUFile handle. 2001 */ 2002 f = mis->from_src_file; 2003 2004 /* And non-blocking again so we don't block in any cleanup */ 2005 qemu_file_set_blocking(f, false); 2006 2007 trace_postcopy_ram_listen_thread_exit(); 2008 if (load_res < 0) { 2009 qemu_file_set_error(f, load_res); 2010 dirty_bitmap_mig_cancel_incoming(); 2011 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING && 2012 !migrate_postcopy_ram() && migrate_dirty_bitmaps()) 2013 { 2014 error_report("%s: loadvm failed during postcopy: %d. All states " 2015 "are migrated except dirty bitmaps. Some dirty " 2016 "bitmaps may be lost, and present migrated dirty " 2017 "bitmaps are correctly migrated and valid.", 2018 __func__, load_res); 2019 load_res = 0; /* prevent further exit() */ 2020 } else { 2021 error_report("%s: loadvm failed: %d", __func__, load_res); 2022 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, 2023 MIGRATION_STATUS_FAILED); 2024 } 2025 } 2026 if (load_res >= 0) { 2027 /* 2028 * This looks good, but it's possible that the device loading in the 2029 * main thread hasn't finished yet, and so we might not be in 'RUN' 2030 * state yet; wait for the end of the main thread. 2031 */ 2032 qemu_event_wait(&mis->main_thread_load_event); 2033 } 2034 postcopy_ram_incoming_cleanup(mis); 2035 2036 if (load_res < 0) { 2037 /* 2038 * If something went wrong then we have a bad state so exit; 2039 * depending how far we got it might be possible at this point 2040 * to leave the guest running and fire MCEs for pages that never 2041 * arrived as a desperate recovery step. 2042 */ 2043 rcu_unregister_thread(); 2044 exit(EXIT_FAILURE); 2045 } 2046 2047 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, 2048 MIGRATION_STATUS_COMPLETED); 2049 /* 2050 * If everything has worked fine, then the main thread has waited 2051 * for us to start, and we're the last use of the mis. 2052 * (If something broke then qemu will have to exit anyway since it's 2053 * got a bad migration state). 2054 */ 2055 migration_incoming_state_destroy(); 2056 qemu_loadvm_state_cleanup(); 2057 2058 rcu_unregister_thread(); 2059 mis->have_listen_thread = false; 2060 postcopy_state_set(POSTCOPY_INCOMING_END); 2061 2062 object_unref(OBJECT(migr)); 2063 2064 return NULL; 2065 } 2066 2067 /* After this message we must be able to immediately receive postcopy data */ 2068 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis) 2069 { 2070 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING); 2071 Error *local_err = NULL; 2072 2073 trace_loadvm_postcopy_handle_listen("enter"); 2074 2075 if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) { 2076 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps); 2077 return -1; 2078 } 2079 if (ps == POSTCOPY_INCOMING_ADVISE) { 2080 /* 2081 * A rare case, we entered listen without having to do any discards, 2082 * so do the setup that's normally done at the time of the 1st discard. 2083 */ 2084 if (migrate_postcopy_ram()) { 2085 postcopy_ram_prepare_discard(mis); 2086 } 2087 } 2088 2089 trace_loadvm_postcopy_handle_listen("after discard"); 2090 2091 /* 2092 * Sensitise RAM - can now generate requests for blocks that don't exist 2093 * However, at this point the CPU shouldn't be running, and the IO 2094 * shouldn't be doing anything yet so don't actually expect requests 2095 */ 2096 if (migrate_postcopy_ram()) { 2097 if (postcopy_ram_incoming_setup(mis)) { 2098 postcopy_ram_incoming_cleanup(mis); 2099 return -1; 2100 } 2101 } 2102 2103 trace_loadvm_postcopy_handle_listen("after uffd"); 2104 2105 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) { 2106 error_report_err(local_err); 2107 return -1; 2108 } 2109 2110 mis->have_listen_thread = true; 2111 postcopy_thread_create(mis, &mis->listen_thread, "postcopy/listen", 2112 postcopy_ram_listen_thread, QEMU_THREAD_DETACHED); 2113 trace_loadvm_postcopy_handle_listen("return"); 2114 2115 return 0; 2116 } 2117 2118 static void loadvm_postcopy_handle_run_bh(void *opaque) 2119 { 2120 Error *local_err = NULL; 2121 MigrationIncomingState *mis = opaque; 2122 2123 trace_loadvm_postcopy_handle_run_bh("enter"); 2124 2125 /* TODO we should move all of this lot into postcopy_ram.c or a shared code 2126 * in migration.c 2127 */ 2128 cpu_synchronize_all_post_init(); 2129 2130 trace_loadvm_postcopy_handle_run_bh("after cpu sync"); 2131 2132 qemu_announce_self(&mis->announce_timer, migrate_announce_params()); 2133 2134 trace_loadvm_postcopy_handle_run_bh("after announce"); 2135 2136 /* Make sure all file formats throw away their mutable metadata. 2137 * If we get an error here, just don't restart the VM yet. */ 2138 bdrv_activate_all(&local_err); 2139 if (local_err) { 2140 error_report_err(local_err); 2141 local_err = NULL; 2142 autostart = false; 2143 } 2144 2145 trace_loadvm_postcopy_handle_run_bh("after invalidate cache"); 2146 2147 dirty_bitmap_mig_before_vm_start(); 2148 2149 if (autostart) { 2150 /* Hold onto your hats, starting the CPU */ 2151 vm_start(); 2152 } else { 2153 /* leave it paused and let management decide when to start the CPU */ 2154 runstate_set(RUN_STATE_PAUSED); 2155 } 2156 2157 qemu_bh_delete(mis->bh); 2158 2159 trace_loadvm_postcopy_handle_run_bh("return"); 2160 } 2161 2162 /* After all discards we can start running and asking for pages */ 2163 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis) 2164 { 2165 PostcopyState ps = postcopy_state_get(); 2166 2167 trace_loadvm_postcopy_handle_run(); 2168 if (ps != POSTCOPY_INCOMING_LISTENING) { 2169 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps); 2170 return -1; 2171 } 2172 2173 postcopy_state_set(POSTCOPY_INCOMING_RUNNING); 2174 mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis); 2175 qemu_bh_schedule(mis->bh); 2176 2177 /* We need to finish reading the stream from the package 2178 * and also stop reading anything more from the stream that loaded the 2179 * package (since it's now being read by the listener thread). 2180 * LOADVM_QUIT will quit all the layers of nested loadvm loops. 2181 */ 2182 return LOADVM_QUIT; 2183 } 2184 2185 /* We must be with page_request_mutex held */ 2186 static gboolean postcopy_sync_page_req(gpointer key, gpointer value, 2187 gpointer data) 2188 { 2189 MigrationIncomingState *mis = data; 2190 void *host_addr = (void *) key; 2191 ram_addr_t rb_offset; 2192 RAMBlock *rb; 2193 int ret; 2194 2195 rb = qemu_ram_block_from_host(host_addr, true, &rb_offset); 2196 if (!rb) { 2197 /* 2198 * This should _never_ happen. However be nice for a migrating VM to 2199 * not crash/assert. Post an error (note: intended to not use *_once 2200 * because we do want to see all the illegal addresses; and this can 2201 * never be triggered by the guest so we're safe) and move on next. 2202 */ 2203 error_report("%s: illegal host addr %p", __func__, host_addr); 2204 /* Try the next entry */ 2205 return FALSE; 2206 } 2207 2208 ret = migrate_send_rp_message_req_pages(mis, rb, rb_offset); 2209 if (ret) { 2210 /* Please refer to above comment. */ 2211 error_report("%s: send rp message failed for addr %p", 2212 __func__, host_addr); 2213 return FALSE; 2214 } 2215 2216 trace_postcopy_page_req_sync(host_addr); 2217 2218 return FALSE; 2219 } 2220 2221 static void migrate_send_rp_req_pages_pending(MigrationIncomingState *mis) 2222 { 2223 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) { 2224 g_tree_foreach(mis->page_requested, postcopy_sync_page_req, mis); 2225 } 2226 } 2227 2228 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis) 2229 { 2230 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { 2231 error_report("%s: illegal resume received", __func__); 2232 /* Don't fail the load, only for this. */ 2233 return 0; 2234 } 2235 2236 /* 2237 * Reset the last_rb before we resend any page req to source again, since 2238 * the source should have it reset already. 2239 */ 2240 mis->last_rb = NULL; 2241 2242 /* 2243 * This means source VM is ready to resume the postcopy migration. 2244 */ 2245 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER, 2246 MIGRATION_STATUS_POSTCOPY_ACTIVE); 2247 2248 trace_loadvm_postcopy_handle_resume(); 2249 2250 /* Tell source that "we are ready" */ 2251 migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE); 2252 2253 /* 2254 * After a postcopy recovery, the source should have lost the postcopy 2255 * queue, or potentially the requested pages could have been lost during 2256 * the network down phase. Let's re-sync with the source VM by re-sending 2257 * all the pending pages that we eagerly need, so these threads won't get 2258 * blocked too long due to the recovery. 2259 * 2260 * Without this procedure, the faulted destination VM threads (waiting for 2261 * page requests right before the postcopy is interrupted) can keep hanging 2262 * until the pages are sent by the source during the background copying of 2263 * pages, or another thread faulted on the same address accidentally. 2264 */ 2265 migrate_send_rp_req_pages_pending(mis); 2266 2267 /* 2268 * It's time to switch state and release the fault thread to continue 2269 * service page faults. Note that this should be explicitly after the 2270 * above call to migrate_send_rp_req_pages_pending(). In short: 2271 * migrate_send_rp_message_req_pages() is not thread safe, yet. 2272 */ 2273 qemu_sem_post(&mis->postcopy_pause_sem_fault); 2274 2275 if (migrate_postcopy_preempt()) { 2276 /* 2277 * The preempt channel will be created in async manner, now let's 2278 * wait for it and make sure it's created. 2279 */ 2280 qemu_sem_wait(&mis->postcopy_qemufile_dst_done); 2281 assert(mis->postcopy_qemufile_dst); 2282 /* Kick the fast ram load thread too */ 2283 qemu_sem_post(&mis->postcopy_pause_sem_fast_load); 2284 } 2285 2286 return 0; 2287 } 2288 2289 /** 2290 * Immediately following this command is a blob of data containing an embedded 2291 * chunk of migration stream; read it and load it. 2292 * 2293 * @mis: Incoming state 2294 * @length: Length of packaged data to read 2295 * 2296 * Returns: Negative values on error 2297 * 2298 */ 2299 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis) 2300 { 2301 int ret; 2302 size_t length; 2303 QIOChannelBuffer *bioc; 2304 2305 length = qemu_get_be32(mis->from_src_file); 2306 trace_loadvm_handle_cmd_packaged(length); 2307 2308 if (length > MAX_VM_CMD_PACKAGED_SIZE) { 2309 error_report("Unreasonably large packaged state: %zu", length); 2310 return -1; 2311 } 2312 2313 bioc = qio_channel_buffer_new(length); 2314 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer"); 2315 ret = qemu_get_buffer(mis->from_src_file, 2316 bioc->data, 2317 length); 2318 if (ret != length) { 2319 object_unref(OBJECT(bioc)); 2320 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu", 2321 ret, length); 2322 return (ret < 0) ? ret : -EAGAIN; 2323 } 2324 bioc->usage += length; 2325 trace_loadvm_handle_cmd_packaged_received(ret); 2326 2327 QEMUFile *packf = qemu_file_new_input(QIO_CHANNEL(bioc)); 2328 2329 ret = qemu_loadvm_state_main(packf, mis); 2330 trace_loadvm_handle_cmd_packaged_main(ret); 2331 qemu_fclose(packf); 2332 object_unref(OBJECT(bioc)); 2333 2334 return ret; 2335 } 2336 2337 /* 2338 * Handle request that source requests for recved_bitmap on 2339 * destination. Payload format: 2340 * 2341 * len (1 byte) + ramblock_name (<255 bytes) 2342 */ 2343 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis, 2344 uint16_t len) 2345 { 2346 QEMUFile *file = mis->from_src_file; 2347 RAMBlock *rb; 2348 char block_name[256]; 2349 size_t cnt; 2350 2351 cnt = qemu_get_counted_string(file, block_name); 2352 if (!cnt) { 2353 error_report("%s: failed to read block name", __func__); 2354 return -EINVAL; 2355 } 2356 2357 /* Validate before using the data */ 2358 if (qemu_file_get_error(file)) { 2359 return qemu_file_get_error(file); 2360 } 2361 2362 if (len != cnt + 1) { 2363 error_report("%s: invalid payload length (%d)", __func__, len); 2364 return -EINVAL; 2365 } 2366 2367 rb = qemu_ram_block_by_name(block_name); 2368 if (!rb) { 2369 error_report("%s: block '%s' not found", __func__, block_name); 2370 return -EINVAL; 2371 } 2372 2373 migrate_send_rp_recv_bitmap(mis, block_name); 2374 2375 trace_loadvm_handle_recv_bitmap(block_name); 2376 2377 return 0; 2378 } 2379 2380 static int loadvm_process_enable_colo(MigrationIncomingState *mis) 2381 { 2382 int ret = migration_incoming_enable_colo(); 2383 2384 if (!ret) { 2385 ret = colo_init_ram_cache(); 2386 if (ret) { 2387 migration_incoming_disable_colo(); 2388 } 2389 } 2390 return ret; 2391 } 2392 2393 /* 2394 * Process an incoming 'QEMU_VM_COMMAND' 2395 * 0 just a normal return 2396 * LOADVM_QUIT All good, but exit the loop 2397 * <0 Error 2398 */ 2399 static int loadvm_process_command(QEMUFile *f) 2400 { 2401 MigrationIncomingState *mis = migration_incoming_get_current(); 2402 uint16_t cmd; 2403 uint16_t len; 2404 uint32_t tmp32; 2405 2406 cmd = qemu_get_be16(f); 2407 len = qemu_get_be16(f); 2408 2409 /* Check validity before continue processing of cmds */ 2410 if (qemu_file_get_error(f)) { 2411 return qemu_file_get_error(f); 2412 } 2413 2414 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) { 2415 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len); 2416 return -EINVAL; 2417 } 2418 2419 trace_loadvm_process_command(mig_cmd_args[cmd].name, len); 2420 2421 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) { 2422 error_report("%s received with bad length - expecting %zu, got %d", 2423 mig_cmd_args[cmd].name, 2424 (size_t)mig_cmd_args[cmd].len, len); 2425 return -ERANGE; 2426 } 2427 2428 switch (cmd) { 2429 case MIG_CMD_OPEN_RETURN_PATH: 2430 if (mis->to_src_file) { 2431 error_report("CMD_OPEN_RETURN_PATH called when RP already open"); 2432 /* Not really a problem, so don't give up */ 2433 return 0; 2434 } 2435 mis->to_src_file = qemu_file_get_return_path(f); 2436 if (!mis->to_src_file) { 2437 error_report("CMD_OPEN_RETURN_PATH failed"); 2438 return -1; 2439 } 2440 2441 /* 2442 * Switchover ack is enabled but no device uses it, so send an ACK to 2443 * source that it's OK to switchover. Do it here, after return path has 2444 * been created. 2445 */ 2446 if (migrate_switchover_ack() && !mis->switchover_ack_pending_num) { 2447 int ret = migrate_send_rp_switchover_ack(mis); 2448 if (ret) { 2449 error_report( 2450 "Could not send switchover ack RP MSG, err %d (%s)", ret, 2451 strerror(-ret)); 2452 return ret; 2453 } 2454 } 2455 break; 2456 2457 case MIG_CMD_PING: 2458 tmp32 = qemu_get_be32(f); 2459 trace_loadvm_process_command_ping(tmp32); 2460 if (!mis->to_src_file) { 2461 error_report("CMD_PING (0x%x) received with no return path", 2462 tmp32); 2463 return -1; 2464 } 2465 migrate_send_rp_pong(mis, tmp32); 2466 break; 2467 2468 case MIG_CMD_PACKAGED: 2469 return loadvm_handle_cmd_packaged(mis); 2470 2471 case MIG_CMD_POSTCOPY_ADVISE: 2472 return loadvm_postcopy_handle_advise(mis, len); 2473 2474 case MIG_CMD_POSTCOPY_LISTEN: 2475 return loadvm_postcopy_handle_listen(mis); 2476 2477 case MIG_CMD_POSTCOPY_RUN: 2478 return loadvm_postcopy_handle_run(mis); 2479 2480 case MIG_CMD_POSTCOPY_RAM_DISCARD: 2481 return loadvm_postcopy_ram_handle_discard(mis, len); 2482 2483 case MIG_CMD_POSTCOPY_RESUME: 2484 return loadvm_postcopy_handle_resume(mis); 2485 2486 case MIG_CMD_RECV_BITMAP: 2487 return loadvm_handle_recv_bitmap(mis, len); 2488 2489 case MIG_CMD_ENABLE_COLO: 2490 return loadvm_process_enable_colo(mis); 2491 } 2492 2493 return 0; 2494 } 2495 2496 /* 2497 * Read a footer off the wire and check that it matches the expected section 2498 * 2499 * Returns: true if the footer was good 2500 * false if there is a problem (and calls error_report to say why) 2501 */ 2502 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se) 2503 { 2504 int ret; 2505 uint8_t read_mark; 2506 uint32_t read_section_id; 2507 2508 if (!migrate_get_current()->send_section_footer) { 2509 /* No footer to check */ 2510 return true; 2511 } 2512 2513 read_mark = qemu_get_byte(f); 2514 2515 ret = qemu_file_get_error(f); 2516 if (ret) { 2517 error_report("%s: Read section footer failed: %d", 2518 __func__, ret); 2519 return false; 2520 } 2521 2522 if (read_mark != QEMU_VM_SECTION_FOOTER) { 2523 error_report("Missing section footer for %s", se->idstr); 2524 return false; 2525 } 2526 2527 read_section_id = qemu_get_be32(f); 2528 if (read_section_id != se->load_section_id) { 2529 error_report("Mismatched section id in footer for %s -" 2530 " read 0x%x expected 0x%x", 2531 se->idstr, read_section_id, se->load_section_id); 2532 return false; 2533 } 2534 2535 /* All good */ 2536 return true; 2537 } 2538 2539 static int 2540 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis) 2541 { 2542 uint32_t instance_id, version_id, section_id; 2543 SaveStateEntry *se; 2544 char idstr[256]; 2545 int ret; 2546 2547 /* Read section start */ 2548 section_id = qemu_get_be32(f); 2549 if (!qemu_get_counted_string(f, idstr)) { 2550 error_report("Unable to read ID string for section %u", 2551 section_id); 2552 return -EINVAL; 2553 } 2554 instance_id = qemu_get_be32(f); 2555 version_id = qemu_get_be32(f); 2556 2557 ret = qemu_file_get_error(f); 2558 if (ret) { 2559 error_report("%s: Failed to read instance/version ID: %d", 2560 __func__, ret); 2561 return ret; 2562 } 2563 2564 trace_qemu_loadvm_state_section_startfull(section_id, idstr, 2565 instance_id, version_id); 2566 /* Find savevm section */ 2567 se = find_se(idstr, instance_id); 2568 if (se == NULL) { 2569 error_report("Unknown savevm section or instance '%s' %"PRIu32". " 2570 "Make sure that your current VM setup matches your " 2571 "saved VM setup, including any hotplugged devices", 2572 idstr, instance_id); 2573 return -EINVAL; 2574 } 2575 2576 /* Validate version */ 2577 if (version_id > se->version_id) { 2578 error_report("savevm: unsupported version %d for '%s' v%d", 2579 version_id, idstr, se->version_id); 2580 return -EINVAL; 2581 } 2582 se->load_version_id = version_id; 2583 se->load_section_id = section_id; 2584 2585 /* Validate if it is a device's state */ 2586 if (xen_enabled() && se->is_ram) { 2587 error_report("loadvm: %s RAM loading not allowed on Xen", idstr); 2588 return -EINVAL; 2589 } 2590 2591 ret = vmstate_load(f, se); 2592 if (ret < 0) { 2593 error_report("error while loading state for instance 0x%"PRIx32" of" 2594 " device '%s'", instance_id, idstr); 2595 return ret; 2596 } 2597 if (!check_section_footer(f, se)) { 2598 return -EINVAL; 2599 } 2600 2601 return 0; 2602 } 2603 2604 static int 2605 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis) 2606 { 2607 uint32_t section_id; 2608 SaveStateEntry *se; 2609 int ret; 2610 2611 section_id = qemu_get_be32(f); 2612 2613 ret = qemu_file_get_error(f); 2614 if (ret) { 2615 error_report("%s: Failed to read section ID: %d", 2616 __func__, ret); 2617 return ret; 2618 } 2619 2620 trace_qemu_loadvm_state_section_partend(section_id); 2621 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 2622 if (se->load_section_id == section_id) { 2623 break; 2624 } 2625 } 2626 if (se == NULL) { 2627 error_report("Unknown savevm section %d", section_id); 2628 return -EINVAL; 2629 } 2630 2631 ret = vmstate_load(f, se); 2632 if (ret < 0) { 2633 error_report("error while loading state section id %d(%s)", 2634 section_id, se->idstr); 2635 return ret; 2636 } 2637 if (!check_section_footer(f, se)) { 2638 return -EINVAL; 2639 } 2640 2641 return 0; 2642 } 2643 2644 static int qemu_loadvm_state_header(QEMUFile *f) 2645 { 2646 unsigned int v; 2647 int ret; 2648 2649 v = qemu_get_be32(f); 2650 if (v != QEMU_VM_FILE_MAGIC) { 2651 error_report("Not a migration stream"); 2652 return -EINVAL; 2653 } 2654 2655 v = qemu_get_be32(f); 2656 if (v == QEMU_VM_FILE_VERSION_COMPAT) { 2657 error_report("SaveVM v2 format is obsolete and don't work anymore"); 2658 return -ENOTSUP; 2659 } 2660 if (v != QEMU_VM_FILE_VERSION) { 2661 error_report("Unsupported migration stream version"); 2662 return -ENOTSUP; 2663 } 2664 2665 if (migrate_get_current()->send_configuration) { 2666 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) { 2667 error_report("Configuration section missing"); 2668 qemu_loadvm_state_cleanup(); 2669 return -EINVAL; 2670 } 2671 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0); 2672 2673 if (ret) { 2674 qemu_loadvm_state_cleanup(); 2675 return ret; 2676 } 2677 } 2678 return 0; 2679 } 2680 2681 static void qemu_loadvm_state_switchover_ack_needed(MigrationIncomingState *mis) 2682 { 2683 SaveStateEntry *se; 2684 2685 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 2686 if (!se->ops || !se->ops->switchover_ack_needed) { 2687 continue; 2688 } 2689 2690 if (se->ops->switchover_ack_needed(se->opaque)) { 2691 mis->switchover_ack_pending_num++; 2692 } 2693 } 2694 2695 trace_loadvm_state_switchover_ack_needed(mis->switchover_ack_pending_num); 2696 } 2697 2698 static int qemu_loadvm_state_setup(QEMUFile *f) 2699 { 2700 SaveStateEntry *se; 2701 int ret; 2702 2703 trace_loadvm_state_setup(); 2704 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 2705 if (!se->ops || !se->ops->load_setup) { 2706 continue; 2707 } 2708 if (se->ops->is_active) { 2709 if (!se->ops->is_active(se->opaque)) { 2710 continue; 2711 } 2712 } 2713 2714 ret = se->ops->load_setup(f, se->opaque); 2715 if (ret < 0) { 2716 qemu_file_set_error(f, ret); 2717 error_report("Load state of device %s failed", se->idstr); 2718 return ret; 2719 } 2720 } 2721 return 0; 2722 } 2723 2724 void qemu_loadvm_state_cleanup(void) 2725 { 2726 SaveStateEntry *se; 2727 2728 trace_loadvm_state_cleanup(); 2729 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 2730 if (se->ops && se->ops->load_cleanup) { 2731 se->ops->load_cleanup(se->opaque); 2732 } 2733 } 2734 } 2735 2736 /* Return true if we should continue the migration, or false. */ 2737 static bool postcopy_pause_incoming(MigrationIncomingState *mis) 2738 { 2739 int i; 2740 2741 trace_postcopy_pause_incoming(); 2742 2743 assert(migrate_postcopy_ram()); 2744 2745 /* 2746 * Unregister yank with either from/to src would work, since ioc behind it 2747 * is the same 2748 */ 2749 migration_ioc_unregister_yank_from_file(mis->from_src_file); 2750 2751 assert(mis->from_src_file); 2752 qemu_file_shutdown(mis->from_src_file); 2753 qemu_fclose(mis->from_src_file); 2754 mis->from_src_file = NULL; 2755 2756 assert(mis->to_src_file); 2757 qemu_file_shutdown(mis->to_src_file); 2758 qemu_mutex_lock(&mis->rp_mutex); 2759 qemu_fclose(mis->to_src_file); 2760 mis->to_src_file = NULL; 2761 qemu_mutex_unlock(&mis->rp_mutex); 2762 2763 /* 2764 * NOTE: this must happen before reset the PostcopyTmpPages below, 2765 * otherwise it's racy to reset those fields when the fast load thread 2766 * can be accessing it in parallel. 2767 */ 2768 if (mis->postcopy_qemufile_dst) { 2769 qemu_file_shutdown(mis->postcopy_qemufile_dst); 2770 /* Take the mutex to make sure the fast ram load thread halted */ 2771 qemu_mutex_lock(&mis->postcopy_prio_thread_mutex); 2772 migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst); 2773 qemu_fclose(mis->postcopy_qemufile_dst); 2774 mis->postcopy_qemufile_dst = NULL; 2775 qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex); 2776 } 2777 2778 /* Current state can be either ACTIVE or RECOVER */ 2779 migrate_set_state(&mis->state, mis->state, 2780 MIGRATION_STATUS_POSTCOPY_PAUSED); 2781 2782 /* Notify the fault thread for the invalidated file handle */ 2783 postcopy_fault_thread_notify(mis); 2784 2785 /* 2786 * If network is interrupted, any temp page we received will be useless 2787 * because we didn't mark them as "received" in receivedmap. After a 2788 * proper recovery later (which will sync src dirty bitmap with receivedmap 2789 * on dest) these cached small pages will be resent again. 2790 */ 2791 for (i = 0; i < mis->postcopy_channels; i++) { 2792 postcopy_temp_page_reset(&mis->postcopy_tmp_pages[i]); 2793 } 2794 2795 error_report("Detected IO failure for postcopy. " 2796 "Migration paused."); 2797 2798 while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { 2799 qemu_sem_wait(&mis->postcopy_pause_sem_dst); 2800 } 2801 2802 trace_postcopy_pause_incoming_continued(); 2803 2804 return true; 2805 } 2806 2807 int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis) 2808 { 2809 uint8_t section_type; 2810 int ret = 0; 2811 2812 retry: 2813 while (true) { 2814 section_type = qemu_get_byte(f); 2815 2816 ret = qemu_file_get_error_obj_any(f, mis->postcopy_qemufile_dst, NULL); 2817 if (ret) { 2818 break; 2819 } 2820 2821 trace_qemu_loadvm_state_section(section_type); 2822 switch (section_type) { 2823 case QEMU_VM_SECTION_START: 2824 case QEMU_VM_SECTION_FULL: 2825 ret = qemu_loadvm_section_start_full(f, mis); 2826 if (ret < 0) { 2827 goto out; 2828 } 2829 break; 2830 case QEMU_VM_SECTION_PART: 2831 case QEMU_VM_SECTION_END: 2832 ret = qemu_loadvm_section_part_end(f, mis); 2833 if (ret < 0) { 2834 goto out; 2835 } 2836 break; 2837 case QEMU_VM_COMMAND: 2838 ret = loadvm_process_command(f); 2839 trace_qemu_loadvm_state_section_command(ret); 2840 if ((ret < 0) || (ret == LOADVM_QUIT)) { 2841 goto out; 2842 } 2843 break; 2844 case QEMU_VM_EOF: 2845 /* This is the end of migration */ 2846 goto out; 2847 default: 2848 error_report("Unknown savevm section type %d", section_type); 2849 ret = -EINVAL; 2850 goto out; 2851 } 2852 } 2853 2854 out: 2855 if (ret < 0) { 2856 qemu_file_set_error(f, ret); 2857 2858 /* Cancel bitmaps incoming regardless of recovery */ 2859 dirty_bitmap_mig_cancel_incoming(); 2860 2861 /* 2862 * If we are during an active postcopy, then we pause instead 2863 * of bail out to at least keep the VM's dirty data. Note 2864 * that POSTCOPY_INCOMING_LISTENING stage is still not enough, 2865 * during which we're still receiving device states and we 2866 * still haven't yet started the VM on destination. 2867 * 2868 * Only RAM postcopy supports recovery. Still, if RAM postcopy is 2869 * enabled, canceled bitmaps postcopy will not affect RAM postcopy 2870 * recovering. 2871 */ 2872 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING && 2873 migrate_postcopy_ram() && postcopy_pause_incoming(mis)) { 2874 /* Reset f to point to the newly created channel */ 2875 f = mis->from_src_file; 2876 goto retry; 2877 } 2878 } 2879 return ret; 2880 } 2881 2882 int qemu_loadvm_state(QEMUFile *f) 2883 { 2884 MigrationIncomingState *mis = migration_incoming_get_current(); 2885 Error *local_err = NULL; 2886 int ret; 2887 2888 if (qemu_savevm_state_blocked(&local_err)) { 2889 error_report_err(local_err); 2890 return -EINVAL; 2891 } 2892 2893 ret = qemu_loadvm_state_header(f); 2894 if (ret) { 2895 return ret; 2896 } 2897 2898 if (qemu_loadvm_state_setup(f) != 0) { 2899 return -EINVAL; 2900 } 2901 2902 if (migrate_switchover_ack()) { 2903 qemu_loadvm_state_switchover_ack_needed(mis); 2904 } 2905 2906 cpu_synchronize_all_pre_loadvm(); 2907 2908 ret = qemu_loadvm_state_main(f, mis); 2909 qemu_event_set(&mis->main_thread_load_event); 2910 2911 trace_qemu_loadvm_state_post_main(ret); 2912 2913 if (mis->have_listen_thread) { 2914 /* Listen thread still going, can't clean up yet */ 2915 return ret; 2916 } 2917 2918 if (ret == 0) { 2919 ret = qemu_file_get_error(f); 2920 } 2921 2922 /* 2923 * Try to read in the VMDESC section as well, so that dumping tools that 2924 * intercept our migration stream have the chance to see it. 2925 */ 2926 2927 /* We've got to be careful; if we don't read the data and just shut the fd 2928 * then the sender can error if we close while it's still sending. 2929 * We also mustn't read data that isn't there; some transports (RDMA) 2930 * will stall waiting for that data when the source has already closed. 2931 */ 2932 if (ret == 0 && should_send_vmdesc()) { 2933 uint8_t *buf; 2934 uint32_t size; 2935 uint8_t section_type = qemu_get_byte(f); 2936 2937 if (section_type != QEMU_VM_VMDESCRIPTION) { 2938 error_report("Expected vmdescription section, but got %d", 2939 section_type); 2940 /* 2941 * It doesn't seem worth failing at this point since 2942 * we apparently have an otherwise valid VM state 2943 */ 2944 } else { 2945 buf = g_malloc(0x1000); 2946 size = qemu_get_be32(f); 2947 2948 while (size > 0) { 2949 uint32_t read_chunk = MIN(size, 0x1000); 2950 qemu_get_buffer(f, buf, read_chunk); 2951 size -= read_chunk; 2952 } 2953 g_free(buf); 2954 } 2955 } 2956 2957 qemu_loadvm_state_cleanup(); 2958 cpu_synchronize_all_post_init(); 2959 2960 return ret; 2961 } 2962 2963 int qemu_load_device_state(QEMUFile *f) 2964 { 2965 MigrationIncomingState *mis = migration_incoming_get_current(); 2966 int ret; 2967 2968 /* Load QEMU_VM_SECTION_FULL section */ 2969 ret = qemu_loadvm_state_main(f, mis); 2970 if (ret < 0) { 2971 error_report("Failed to load device state: %d", ret); 2972 return ret; 2973 } 2974 2975 cpu_synchronize_all_post_init(); 2976 return 0; 2977 } 2978 2979 int qemu_loadvm_approve_switchover(void) 2980 { 2981 MigrationIncomingState *mis = migration_incoming_get_current(); 2982 2983 if (!mis->switchover_ack_pending_num) { 2984 return -EINVAL; 2985 } 2986 2987 mis->switchover_ack_pending_num--; 2988 trace_loadvm_approve_switchover(mis->switchover_ack_pending_num); 2989 2990 if (mis->switchover_ack_pending_num) { 2991 return 0; 2992 } 2993 2994 return migrate_send_rp_switchover_ack(mis); 2995 } 2996 2997 bool save_snapshot(const char *name, bool overwrite, const char *vmstate, 2998 bool has_devices, strList *devices, Error **errp) 2999 { 3000 BlockDriverState *bs; 3001 QEMUSnapshotInfo sn1, *sn = &sn1; 3002 int ret = -1, ret2; 3003 QEMUFile *f; 3004 int saved_vm_running; 3005 uint64_t vm_state_size; 3006 g_autoptr(GDateTime) now = g_date_time_new_now_local(); 3007 AioContext *aio_context; 3008 3009 GLOBAL_STATE_CODE(); 3010 3011 if (migration_is_blocked(errp)) { 3012 return false; 3013 } 3014 3015 if (!replay_can_snapshot()) { 3016 error_setg(errp, "Record/replay does not allow making snapshot " 3017 "right now. Try once more later."); 3018 return false; 3019 } 3020 3021 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) { 3022 return false; 3023 } 3024 3025 /* Delete old snapshots of the same name */ 3026 if (name) { 3027 if (overwrite) { 3028 if (bdrv_all_delete_snapshot(name, has_devices, 3029 devices, errp) < 0) { 3030 return false; 3031 } 3032 } else { 3033 ret2 = bdrv_all_has_snapshot(name, has_devices, devices, errp); 3034 if (ret2 < 0) { 3035 return false; 3036 } 3037 if (ret2 == 1) { 3038 error_setg(errp, 3039 "Snapshot '%s' already exists in one or more devices", 3040 name); 3041 return false; 3042 } 3043 } 3044 } 3045 3046 bs = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp); 3047 if (bs == NULL) { 3048 return false; 3049 } 3050 aio_context = bdrv_get_aio_context(bs); 3051 3052 saved_vm_running = runstate_is_running(); 3053 3054 global_state_store(); 3055 vm_stop(RUN_STATE_SAVE_VM); 3056 3057 bdrv_drain_all_begin(); 3058 3059 aio_context_acquire(aio_context); 3060 3061 memset(sn, 0, sizeof(*sn)); 3062 3063 /* fill auxiliary fields */ 3064 sn->date_sec = g_date_time_to_unix(now); 3065 sn->date_nsec = g_date_time_get_microsecond(now) * 1000; 3066 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 3067 if (replay_mode != REPLAY_MODE_NONE) { 3068 sn->icount = replay_get_current_icount(); 3069 } else { 3070 sn->icount = -1ULL; 3071 } 3072 3073 if (name) { 3074 pstrcpy(sn->name, sizeof(sn->name), name); 3075 } else { 3076 g_autofree char *autoname = g_date_time_format(now, "vm-%Y%m%d%H%M%S"); 3077 pstrcpy(sn->name, sizeof(sn->name), autoname); 3078 } 3079 3080 /* save the VM state */ 3081 f = qemu_fopen_bdrv(bs, 1); 3082 if (!f) { 3083 error_setg(errp, "Could not open VM state file"); 3084 goto the_end; 3085 } 3086 ret = qemu_savevm_state(f, errp); 3087 vm_state_size = qemu_file_transferred(f); 3088 ret2 = qemu_fclose(f); 3089 if (ret < 0) { 3090 goto the_end; 3091 } 3092 if (ret2 < 0) { 3093 ret = ret2; 3094 goto the_end; 3095 } 3096 3097 /* The bdrv_all_create_snapshot() call that follows acquires the AioContext 3098 * for itself. BDRV_POLL_WHILE() does not support nested locking because 3099 * it only releases the lock once. Therefore synchronous I/O will deadlock 3100 * unless we release the AioContext before bdrv_all_create_snapshot(). 3101 */ 3102 aio_context_release(aio_context); 3103 aio_context = NULL; 3104 3105 ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, 3106 has_devices, devices, errp); 3107 if (ret < 0) { 3108 bdrv_all_delete_snapshot(sn->name, has_devices, devices, NULL); 3109 goto the_end; 3110 } 3111 3112 ret = 0; 3113 3114 the_end: 3115 if (aio_context) { 3116 aio_context_release(aio_context); 3117 } 3118 3119 bdrv_drain_all_end(); 3120 3121 if (saved_vm_running) { 3122 vm_start(); 3123 } 3124 return ret == 0; 3125 } 3126 3127 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live, 3128 Error **errp) 3129 { 3130 QEMUFile *f; 3131 QIOChannelFile *ioc; 3132 int saved_vm_running; 3133 int ret; 3134 3135 if (!has_live) { 3136 /* live default to true so old version of Xen tool stack can have a 3137 * successful live migration */ 3138 live = true; 3139 } 3140 3141 saved_vm_running = runstate_is_running(); 3142 vm_stop(RUN_STATE_SAVE_VM); 3143 global_state_store_running(); 3144 3145 ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT | O_TRUNC, 3146 0660, errp); 3147 if (!ioc) { 3148 goto the_end; 3149 } 3150 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state"); 3151 f = qemu_file_new_output(QIO_CHANNEL(ioc)); 3152 object_unref(OBJECT(ioc)); 3153 ret = qemu_save_device_state(f); 3154 if (ret < 0 || qemu_fclose(f) < 0) { 3155 error_setg(errp, QERR_IO_ERROR); 3156 } else { 3157 /* libxl calls the QMP command "stop" before calling 3158 * "xen-save-devices-state" and in case of migration failure, libxl 3159 * would call "cont". 3160 * So call bdrv_inactivate_all (release locks) here to let the other 3161 * side of the migration take control of the images. 3162 */ 3163 if (live && !saved_vm_running) { 3164 ret = bdrv_inactivate_all(); 3165 if (ret) { 3166 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)", 3167 __func__, ret); 3168 } 3169 } 3170 } 3171 3172 the_end: 3173 if (saved_vm_running) { 3174 vm_start(); 3175 } 3176 } 3177 3178 void qmp_xen_load_devices_state(const char *filename, Error **errp) 3179 { 3180 QEMUFile *f; 3181 QIOChannelFile *ioc; 3182 int ret; 3183 3184 /* Guest must be paused before loading the device state; the RAM state 3185 * will already have been loaded by xc 3186 */ 3187 if (runstate_is_running()) { 3188 error_setg(errp, "Cannot update device state while vm is running"); 3189 return; 3190 } 3191 vm_stop(RUN_STATE_RESTORE_VM); 3192 3193 ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp); 3194 if (!ioc) { 3195 return; 3196 } 3197 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state"); 3198 f = qemu_file_new_input(QIO_CHANNEL(ioc)); 3199 object_unref(OBJECT(ioc)); 3200 3201 ret = qemu_loadvm_state(f); 3202 qemu_fclose(f); 3203 if (ret < 0) { 3204 error_setg(errp, QERR_IO_ERROR); 3205 } 3206 migration_incoming_state_destroy(); 3207 } 3208 3209 bool load_snapshot(const char *name, const char *vmstate, 3210 bool has_devices, strList *devices, Error **errp) 3211 { 3212 BlockDriverState *bs_vm_state; 3213 QEMUSnapshotInfo sn; 3214 QEMUFile *f; 3215 int ret; 3216 AioContext *aio_context; 3217 MigrationIncomingState *mis = migration_incoming_get_current(); 3218 3219 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) { 3220 return false; 3221 } 3222 ret = bdrv_all_has_snapshot(name, has_devices, devices, errp); 3223 if (ret < 0) { 3224 return false; 3225 } 3226 if (ret == 0) { 3227 error_setg(errp, "Snapshot '%s' does not exist in one or more devices", 3228 name); 3229 return false; 3230 } 3231 3232 bs_vm_state = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp); 3233 if (!bs_vm_state) { 3234 return false; 3235 } 3236 aio_context = bdrv_get_aio_context(bs_vm_state); 3237 3238 /* Don't even try to load empty VM states */ 3239 aio_context_acquire(aio_context); 3240 ret = bdrv_snapshot_find(bs_vm_state, &sn, name); 3241 aio_context_release(aio_context); 3242 if (ret < 0) { 3243 return false; 3244 } else if (sn.vm_state_size == 0) { 3245 error_setg(errp, "This is a disk-only snapshot. Revert to it " 3246 " offline using qemu-img"); 3247 return false; 3248 } 3249 3250 /* 3251 * Flush the record/replay queue. Now the VM state is going 3252 * to change. Therefore we don't need to preserve its consistency 3253 */ 3254 replay_flush_events(); 3255 3256 /* Flush all IO requests so they don't interfere with the new state. */ 3257 bdrv_drain_all_begin(); 3258 3259 ret = bdrv_all_goto_snapshot(name, has_devices, devices, errp); 3260 if (ret < 0) { 3261 goto err_drain; 3262 } 3263 3264 /* restore the VM state */ 3265 f = qemu_fopen_bdrv(bs_vm_state, 0); 3266 if (!f) { 3267 error_setg(errp, "Could not open VM state file"); 3268 goto err_drain; 3269 } 3270 3271 qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD); 3272 mis->from_src_file = f; 3273 3274 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) { 3275 ret = -EINVAL; 3276 goto err_drain; 3277 } 3278 aio_context_acquire(aio_context); 3279 ret = qemu_loadvm_state(f); 3280 migration_incoming_state_destroy(); 3281 aio_context_release(aio_context); 3282 3283 bdrv_drain_all_end(); 3284 3285 if (ret < 0) { 3286 error_setg(errp, "Error %d while loading VM state", ret); 3287 return false; 3288 } 3289 3290 return true; 3291 3292 err_drain: 3293 bdrv_drain_all_end(); 3294 return false; 3295 } 3296 3297 bool delete_snapshot(const char *name, bool has_devices, 3298 strList *devices, Error **errp) 3299 { 3300 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) { 3301 return false; 3302 } 3303 3304 if (bdrv_all_delete_snapshot(name, has_devices, devices, errp) < 0) { 3305 return false; 3306 } 3307 3308 return true; 3309 } 3310 3311 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) 3312 { 3313 qemu_ram_set_idstr(mr->ram_block, 3314 memory_region_name(mr), dev); 3315 qemu_ram_set_migratable(mr->ram_block); 3316 } 3317 3318 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) 3319 { 3320 qemu_ram_unset_idstr(mr->ram_block); 3321 qemu_ram_unset_migratable(mr->ram_block); 3322 } 3323 3324 void vmstate_register_ram_global(MemoryRegion *mr) 3325 { 3326 vmstate_register_ram(mr, NULL); 3327 } 3328 3329 bool vmstate_check_only_migratable(const VMStateDescription *vmsd) 3330 { 3331 /* check needed if --only-migratable is specified */ 3332 if (!only_migratable) { 3333 return true; 3334 } 3335 3336 return !(vmsd && vmsd->unmigratable); 3337 } 3338 3339 typedef struct SnapshotJob { 3340 Job common; 3341 char *tag; 3342 char *vmstate; 3343 strList *devices; 3344 Coroutine *co; 3345 Error **errp; 3346 bool ret; 3347 } SnapshotJob; 3348 3349 static void qmp_snapshot_job_free(SnapshotJob *s) 3350 { 3351 g_free(s->tag); 3352 g_free(s->vmstate); 3353 qapi_free_strList(s->devices); 3354 } 3355 3356 3357 static void snapshot_load_job_bh(void *opaque) 3358 { 3359 Job *job = opaque; 3360 SnapshotJob *s = container_of(job, SnapshotJob, common); 3361 int orig_vm_running; 3362 3363 job_progress_set_remaining(&s->common, 1); 3364 3365 orig_vm_running = runstate_is_running(); 3366 vm_stop(RUN_STATE_RESTORE_VM); 3367 3368 s->ret = load_snapshot(s->tag, s->vmstate, true, s->devices, s->errp); 3369 if (s->ret && orig_vm_running) { 3370 vm_start(); 3371 } 3372 3373 job_progress_update(&s->common, 1); 3374 3375 qmp_snapshot_job_free(s); 3376 aio_co_wake(s->co); 3377 } 3378 3379 static void snapshot_save_job_bh(void *opaque) 3380 { 3381 Job *job = opaque; 3382 SnapshotJob *s = container_of(job, SnapshotJob, common); 3383 3384 job_progress_set_remaining(&s->common, 1); 3385 s->ret = save_snapshot(s->tag, false, s->vmstate, 3386 true, s->devices, s->errp); 3387 job_progress_update(&s->common, 1); 3388 3389 qmp_snapshot_job_free(s); 3390 aio_co_wake(s->co); 3391 } 3392 3393 static void snapshot_delete_job_bh(void *opaque) 3394 { 3395 Job *job = opaque; 3396 SnapshotJob *s = container_of(job, SnapshotJob, common); 3397 3398 job_progress_set_remaining(&s->common, 1); 3399 s->ret = delete_snapshot(s->tag, true, s->devices, s->errp); 3400 job_progress_update(&s->common, 1); 3401 3402 qmp_snapshot_job_free(s); 3403 aio_co_wake(s->co); 3404 } 3405 3406 static int coroutine_fn snapshot_save_job_run(Job *job, Error **errp) 3407 { 3408 SnapshotJob *s = container_of(job, SnapshotJob, common); 3409 s->errp = errp; 3410 s->co = qemu_coroutine_self(); 3411 aio_bh_schedule_oneshot(qemu_get_aio_context(), 3412 snapshot_save_job_bh, job); 3413 qemu_coroutine_yield(); 3414 return s->ret ? 0 : -1; 3415 } 3416 3417 static int coroutine_fn snapshot_load_job_run(Job *job, Error **errp) 3418 { 3419 SnapshotJob *s = container_of(job, SnapshotJob, common); 3420 s->errp = errp; 3421 s->co = qemu_coroutine_self(); 3422 aio_bh_schedule_oneshot(qemu_get_aio_context(), 3423 snapshot_load_job_bh, job); 3424 qemu_coroutine_yield(); 3425 return s->ret ? 0 : -1; 3426 } 3427 3428 static int coroutine_fn snapshot_delete_job_run(Job *job, Error **errp) 3429 { 3430 SnapshotJob *s = container_of(job, SnapshotJob, common); 3431 s->errp = errp; 3432 s->co = qemu_coroutine_self(); 3433 aio_bh_schedule_oneshot(qemu_get_aio_context(), 3434 snapshot_delete_job_bh, job); 3435 qemu_coroutine_yield(); 3436 return s->ret ? 0 : -1; 3437 } 3438 3439 3440 static const JobDriver snapshot_load_job_driver = { 3441 .instance_size = sizeof(SnapshotJob), 3442 .job_type = JOB_TYPE_SNAPSHOT_LOAD, 3443 .run = snapshot_load_job_run, 3444 }; 3445 3446 static const JobDriver snapshot_save_job_driver = { 3447 .instance_size = sizeof(SnapshotJob), 3448 .job_type = JOB_TYPE_SNAPSHOT_SAVE, 3449 .run = snapshot_save_job_run, 3450 }; 3451 3452 static const JobDriver snapshot_delete_job_driver = { 3453 .instance_size = sizeof(SnapshotJob), 3454 .job_type = JOB_TYPE_SNAPSHOT_DELETE, 3455 .run = snapshot_delete_job_run, 3456 }; 3457 3458 3459 void qmp_snapshot_save(const char *job_id, 3460 const char *tag, 3461 const char *vmstate, 3462 strList *devices, 3463 Error **errp) 3464 { 3465 SnapshotJob *s; 3466 3467 s = job_create(job_id, &snapshot_save_job_driver, NULL, 3468 qemu_get_aio_context(), JOB_MANUAL_DISMISS, 3469 NULL, NULL, errp); 3470 if (!s) { 3471 return; 3472 } 3473 3474 s->tag = g_strdup(tag); 3475 s->vmstate = g_strdup(vmstate); 3476 s->devices = QAPI_CLONE(strList, devices); 3477 3478 job_start(&s->common); 3479 } 3480 3481 void qmp_snapshot_load(const char *job_id, 3482 const char *tag, 3483 const char *vmstate, 3484 strList *devices, 3485 Error **errp) 3486 { 3487 SnapshotJob *s; 3488 3489 s = job_create(job_id, &snapshot_load_job_driver, NULL, 3490 qemu_get_aio_context(), JOB_MANUAL_DISMISS, 3491 NULL, NULL, errp); 3492 if (!s) { 3493 return; 3494 } 3495 3496 s->tag = g_strdup(tag); 3497 s->vmstate = g_strdup(vmstate); 3498 s->devices = QAPI_CLONE(strList, devices); 3499 3500 job_start(&s->common); 3501 } 3502 3503 void qmp_snapshot_delete(const char *job_id, 3504 const char *tag, 3505 strList *devices, 3506 Error **errp) 3507 { 3508 SnapshotJob *s; 3509 3510 s = job_create(job_id, &snapshot_delete_job_driver, NULL, 3511 qemu_get_aio_context(), JOB_MANUAL_DISMISS, 3512 NULL, NULL, errp); 3513 if (!s) { 3514 return; 3515 } 3516 3517 s->tag = g_strdup(tag); 3518 s->devices = QAPI_CLONE(strList, devices); 3519 3520 job_start(&s->common); 3521 } 3522