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