1 /* 2 * QEMU live migration 3 * 4 * Copyright IBM, Corp. 2008 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Contributions after 2012-01-13 are licensed under the terms of the 13 * GNU GPL, version 2 or (at your option) any later version. 14 */ 15 16 #include "qemu/osdep.h" 17 #include "qemu/cutils.h" 18 #include "qemu/error-report.h" 19 #include "qemu/main-loop.h" 20 #include "migration/blocker.h" 21 #include "migration/migration.h" 22 #include "migration/qemu-file.h" 23 #include "sysemu/sysemu.h" 24 #include "block/block.h" 25 #include "qapi/qmp/qerror.h" 26 #include "qapi/util.h" 27 #include "qemu/sockets.h" 28 #include "qemu/rcu.h" 29 #include "migration/block.h" 30 #include "postcopy-ram.h" 31 #include "qemu/thread.h" 32 #include "qmp-commands.h" 33 #include "trace.h" 34 #include "qapi-event.h" 35 #include "qom/cpu.h" 36 #include "exec/memory.h" 37 #include "exec/address-spaces.h" 38 #include "io/channel-buffer.h" 39 #include "io/channel-tls.h" 40 #include "migration/colo.h" 41 42 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */ 43 44 /* Amount of time to allocate to each "chunk" of bandwidth-throttled 45 * data. */ 46 #define BUFFER_DELAY 100 47 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) 48 49 /* Time in milliseconds we are allowed to stop the source, 50 * for sending the last part */ 51 #define DEFAULT_MIGRATE_SET_DOWNTIME 300 52 53 /* Maximum migrate downtime set to 2000 seconds */ 54 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000 55 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000) 56 57 /* Default compression thread count */ 58 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 59 /* Default decompression thread count, usually decompression is at 60 * least 4 times as fast as compression.*/ 61 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 62 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ 63 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 64 /* Define default autoconverge cpu throttle migration parameters */ 65 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20 66 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10 67 68 /* Migration XBZRLE default cache size */ 69 #define DEFAULT_MIGRATE_CACHE_SIZE (64 * 1024 * 1024) 70 71 /* The delay time (in ms) between two COLO checkpoints 72 * Note: Please change this default value to 10000 when we support hybrid mode. 73 */ 74 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY 200 75 76 static NotifierList migration_state_notifiers = 77 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); 78 79 static bool deferred_incoming; 80 81 /* 82 * Current state of incoming postcopy; note this is not part of 83 * MigrationIncomingState since it's state is used during cleanup 84 * at the end as MIS is being freed. 85 */ 86 static PostcopyState incoming_postcopy_state; 87 88 /* When we add fault tolerance, we could have several 89 migrations at once. For now we don't need to add 90 dynamic creation of migration */ 91 92 /* For outgoing */ 93 MigrationState *migrate_get_current(void) 94 { 95 static bool once; 96 static MigrationState current_migration = { 97 .state = MIGRATION_STATUS_NONE, 98 .xbzrle_cache_size = DEFAULT_MIGRATE_CACHE_SIZE, 99 .mbps = -1, 100 .parameters = { 101 .compress_level = DEFAULT_MIGRATE_COMPRESS_LEVEL, 102 .compress_threads = DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT, 103 .decompress_threads = DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT, 104 .cpu_throttle_initial = DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL, 105 .cpu_throttle_increment = DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT, 106 .max_bandwidth = MAX_THROTTLE, 107 .downtime_limit = DEFAULT_MIGRATE_SET_DOWNTIME, 108 .x_checkpoint_delay = DEFAULT_MIGRATE_X_CHECKPOINT_DELAY, 109 }, 110 }; 111 112 if (!once) { 113 current_migration.parameters.tls_creds = g_strdup(""); 114 current_migration.parameters.tls_hostname = g_strdup(""); 115 once = true; 116 } 117 return ¤t_migration; 118 } 119 120 MigrationIncomingState *migration_incoming_get_current(void) 121 { 122 static bool once; 123 static MigrationIncomingState mis_current; 124 125 if (!once) { 126 mis_current.state = MIGRATION_STATUS_NONE; 127 memset(&mis_current, 0, sizeof(MigrationIncomingState)); 128 QLIST_INIT(&mis_current.loadvm_handlers); 129 qemu_mutex_init(&mis_current.rp_mutex); 130 qemu_event_init(&mis_current.main_thread_load_event, false); 131 once = true; 132 } 133 return &mis_current; 134 } 135 136 void migration_incoming_state_destroy(void) 137 { 138 struct MigrationIncomingState *mis = migration_incoming_get_current(); 139 140 qemu_event_destroy(&mis->main_thread_load_event); 141 loadvm_free_handlers(mis); 142 } 143 144 145 typedef struct { 146 bool optional; 147 uint32_t size; 148 uint8_t runstate[100]; 149 RunState state; 150 bool received; 151 } GlobalState; 152 153 static GlobalState global_state; 154 155 int global_state_store(void) 156 { 157 if (!runstate_store((char *)global_state.runstate, 158 sizeof(global_state.runstate))) { 159 error_report("runstate name too big: %s", global_state.runstate); 160 trace_migrate_state_too_big(); 161 return -EINVAL; 162 } 163 return 0; 164 } 165 166 void global_state_store_running(void) 167 { 168 const char *state = RunState_lookup[RUN_STATE_RUNNING]; 169 strncpy((char *)global_state.runstate, 170 state, sizeof(global_state.runstate)); 171 } 172 173 static bool global_state_received(void) 174 { 175 return global_state.received; 176 } 177 178 static RunState global_state_get_runstate(void) 179 { 180 return global_state.state; 181 } 182 183 void global_state_set_optional(void) 184 { 185 global_state.optional = true; 186 } 187 188 static bool global_state_needed(void *opaque) 189 { 190 GlobalState *s = opaque; 191 char *runstate = (char *)s->runstate; 192 193 /* If it is not optional, it is mandatory */ 194 195 if (s->optional == false) { 196 return true; 197 } 198 199 /* If state is running or paused, it is not needed */ 200 201 if (strcmp(runstate, "running") == 0 || 202 strcmp(runstate, "paused") == 0) { 203 return false; 204 } 205 206 /* for any other state it is needed */ 207 return true; 208 } 209 210 static int global_state_post_load(void *opaque, int version_id) 211 { 212 GlobalState *s = opaque; 213 Error *local_err = NULL; 214 int r; 215 char *runstate = (char *)s->runstate; 216 217 s->received = true; 218 trace_migrate_global_state_post_load(runstate); 219 220 r = qapi_enum_parse(RunState_lookup, runstate, RUN_STATE__MAX, 221 -1, &local_err); 222 223 if (r == -1) { 224 if (local_err) { 225 error_report_err(local_err); 226 } 227 return -EINVAL; 228 } 229 s->state = r; 230 231 return 0; 232 } 233 234 static void global_state_pre_save(void *opaque) 235 { 236 GlobalState *s = opaque; 237 238 trace_migrate_global_state_pre_save((char *)s->runstate); 239 s->size = strlen((char *)s->runstate) + 1; 240 } 241 242 static const VMStateDescription vmstate_globalstate = { 243 .name = "globalstate", 244 .version_id = 1, 245 .minimum_version_id = 1, 246 .post_load = global_state_post_load, 247 .pre_save = global_state_pre_save, 248 .needed = global_state_needed, 249 .fields = (VMStateField[]) { 250 VMSTATE_UINT32(size, GlobalState), 251 VMSTATE_BUFFER(runstate, GlobalState), 252 VMSTATE_END_OF_LIST() 253 }, 254 }; 255 256 void register_global_state(void) 257 { 258 /* We would use it independently that we receive it */ 259 strcpy((char *)&global_state.runstate, ""); 260 global_state.received = false; 261 vmstate_register(NULL, 0, &vmstate_globalstate, &global_state); 262 } 263 264 static void migrate_generate_event(int new_state) 265 { 266 if (migrate_use_events()) { 267 qapi_event_send_migration(new_state, &error_abort); 268 } 269 } 270 271 /* 272 * Called on -incoming with a defer: uri. 273 * The migration can be started later after any parameters have been 274 * changed. 275 */ 276 static void deferred_incoming_migration(Error **errp) 277 { 278 if (deferred_incoming) { 279 error_setg(errp, "Incoming migration already deferred"); 280 } 281 deferred_incoming = true; 282 } 283 284 /* Request a range of pages from the source VM at the given 285 * start address. 286 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same 287 * as the last request (a name must have been given previously) 288 * Start: Address offset within the RB 289 * Len: Length in bytes required - must be a multiple of pagesize 290 */ 291 void migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname, 292 ram_addr_t start, size_t len) 293 { 294 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */ 295 size_t msglen = 12; /* start + len */ 296 297 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start); 298 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len); 299 300 if (rbname) { 301 int rbname_len = strlen(rbname); 302 assert(rbname_len < 256); 303 304 bufc[msglen++] = rbname_len; 305 memcpy(bufc + msglen, rbname, rbname_len); 306 msglen += rbname_len; 307 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES_ID, msglen, bufc); 308 } else { 309 migrate_send_rp_message(mis, MIG_RP_MSG_REQ_PAGES, msglen, bufc); 310 } 311 } 312 313 void qemu_start_incoming_migration(const char *uri, Error **errp) 314 { 315 const char *p; 316 317 qapi_event_send_migration(MIGRATION_STATUS_SETUP, &error_abort); 318 if (!strcmp(uri, "defer")) { 319 deferred_incoming_migration(errp); 320 } else if (strstart(uri, "tcp:", &p)) { 321 tcp_start_incoming_migration(p, errp); 322 #ifdef CONFIG_RDMA 323 } else if (strstart(uri, "rdma:", &p)) { 324 rdma_start_incoming_migration(p, errp); 325 #endif 326 } else if (strstart(uri, "exec:", &p)) { 327 exec_start_incoming_migration(p, errp); 328 } else if (strstart(uri, "unix:", &p)) { 329 unix_start_incoming_migration(p, errp); 330 } else if (strstart(uri, "fd:", &p)) { 331 fd_start_incoming_migration(p, errp); 332 } else { 333 error_setg(errp, "unknown migration protocol: %s", uri); 334 } 335 } 336 337 static void process_incoming_migration_bh(void *opaque) 338 { 339 Error *local_err = NULL; 340 MigrationIncomingState *mis = opaque; 341 342 /* Make sure all file formats flush their mutable metadata. 343 * If we get an error here, just don't restart the VM yet. */ 344 bdrv_invalidate_cache_all(&local_err); 345 if (local_err) { 346 error_report_err(local_err); 347 local_err = NULL; 348 autostart = false; 349 } 350 351 /* 352 * This must happen after all error conditions are dealt with and 353 * we're sure the VM is going to be running on this host. 354 */ 355 qemu_announce_self(); 356 357 /* If global state section was not received or we are in running 358 state, we need to obey autostart. Any other state is set with 359 runstate_set. */ 360 361 if (!global_state_received() || 362 global_state_get_runstate() == RUN_STATE_RUNNING) { 363 if (autostart) { 364 vm_start(); 365 } else { 366 runstate_set(RUN_STATE_PAUSED); 367 } 368 } else { 369 runstate_set(global_state_get_runstate()); 370 } 371 migrate_decompress_threads_join(); 372 /* 373 * This must happen after any state changes since as soon as an external 374 * observer sees this event they might start to prod at the VM assuming 375 * it's ready to use. 376 */ 377 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 378 MIGRATION_STATUS_COMPLETED); 379 qemu_bh_delete(mis->bh); 380 migration_incoming_state_destroy(); 381 } 382 383 static void process_incoming_migration_co(void *opaque) 384 { 385 QEMUFile *f = opaque; 386 MigrationIncomingState *mis = migration_incoming_get_current(); 387 PostcopyState ps; 388 int ret; 389 390 mis->from_src_file = f; 391 mis->largest_page_size = qemu_ram_pagesize_largest(); 392 postcopy_state_set(POSTCOPY_INCOMING_NONE); 393 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE, 394 MIGRATION_STATUS_ACTIVE); 395 ret = qemu_loadvm_state(f); 396 397 ps = postcopy_state_get(); 398 trace_process_incoming_migration_co_end(ret, ps); 399 if (ps != POSTCOPY_INCOMING_NONE) { 400 if (ps == POSTCOPY_INCOMING_ADVISE) { 401 /* 402 * Where a migration had postcopy enabled (and thus went to advise) 403 * but managed to complete within the precopy period, we can use 404 * the normal exit. 405 */ 406 postcopy_ram_incoming_cleanup(mis); 407 } else if (ret >= 0) { 408 /* 409 * Postcopy was started, cleanup should happen at the end of the 410 * postcopy thread. 411 */ 412 trace_process_incoming_migration_co_postcopy_end_main(); 413 return; 414 } 415 /* Else if something went wrong then just fall out of the normal exit */ 416 } 417 418 /* we get COLO info, and know if we are in COLO mode */ 419 if (!ret && migration_incoming_enable_colo()) { 420 mis->migration_incoming_co = qemu_coroutine_self(); 421 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming", 422 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE); 423 mis->have_colo_incoming_thread = true; 424 qemu_coroutine_yield(); 425 426 /* Wait checkpoint incoming thread exit before free resource */ 427 qemu_thread_join(&mis->colo_incoming_thread); 428 } 429 430 if (ret < 0) { 431 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 432 MIGRATION_STATUS_FAILED); 433 error_report("load of migration failed: %s", strerror(-ret)); 434 migrate_decompress_threads_join(); 435 exit(EXIT_FAILURE); 436 } 437 438 qemu_fclose(f); 439 free_xbzrle_decoded_buf(); 440 441 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis); 442 qemu_bh_schedule(mis->bh); 443 } 444 445 void migration_fd_process_incoming(QEMUFile *f) 446 { 447 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, f); 448 449 migrate_decompress_threads_create(); 450 qemu_file_set_blocking(f, false); 451 qemu_coroutine_enter(co); 452 } 453 454 455 void migration_channel_process_incoming(MigrationState *s, 456 QIOChannel *ioc) 457 { 458 trace_migration_set_incoming_channel( 459 ioc, object_get_typename(OBJECT(ioc))); 460 461 if (s->parameters.tls_creds && 462 *s->parameters.tls_creds && 463 !object_dynamic_cast(OBJECT(ioc), 464 TYPE_QIO_CHANNEL_TLS)) { 465 Error *local_err = NULL; 466 migration_tls_channel_process_incoming(s, ioc, &local_err); 467 if (local_err) { 468 error_report_err(local_err); 469 } 470 } else { 471 QEMUFile *f = qemu_fopen_channel_input(ioc); 472 migration_fd_process_incoming(f); 473 } 474 } 475 476 477 void migration_channel_connect(MigrationState *s, 478 QIOChannel *ioc, 479 const char *hostname) 480 { 481 trace_migration_set_outgoing_channel( 482 ioc, object_get_typename(OBJECT(ioc)), hostname); 483 484 if (s->parameters.tls_creds && 485 *s->parameters.tls_creds && 486 !object_dynamic_cast(OBJECT(ioc), 487 TYPE_QIO_CHANNEL_TLS)) { 488 Error *local_err = NULL; 489 migration_tls_channel_connect(s, ioc, hostname, &local_err); 490 if (local_err) { 491 migrate_fd_error(s, local_err); 492 error_free(local_err); 493 } 494 } else { 495 QEMUFile *f = qemu_fopen_channel_output(ioc); 496 497 s->to_dst_file = f; 498 499 migrate_fd_connect(s); 500 } 501 } 502 503 504 /* 505 * Send a message on the return channel back to the source 506 * of the migration. 507 */ 508 void migrate_send_rp_message(MigrationIncomingState *mis, 509 enum mig_rp_message_type message_type, 510 uint16_t len, void *data) 511 { 512 trace_migrate_send_rp_message((int)message_type, len); 513 qemu_mutex_lock(&mis->rp_mutex); 514 qemu_put_be16(mis->to_src_file, (unsigned int)message_type); 515 qemu_put_be16(mis->to_src_file, len); 516 qemu_put_buffer(mis->to_src_file, data, len); 517 qemu_fflush(mis->to_src_file); 518 qemu_mutex_unlock(&mis->rp_mutex); 519 } 520 521 /* 522 * Send a 'SHUT' message on the return channel with the given value 523 * to indicate that we've finished with the RP. Non-0 value indicates 524 * error. 525 */ 526 void migrate_send_rp_shut(MigrationIncomingState *mis, 527 uint32_t value) 528 { 529 uint32_t buf; 530 531 buf = cpu_to_be32(value); 532 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); 533 } 534 535 /* 536 * Send a 'PONG' message on the return channel with the given value 537 * (normally in response to a 'PING') 538 */ 539 void migrate_send_rp_pong(MigrationIncomingState *mis, 540 uint32_t value) 541 { 542 uint32_t buf; 543 544 buf = cpu_to_be32(value); 545 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); 546 } 547 548 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) 549 { 550 MigrationCapabilityStatusList *head = NULL; 551 MigrationCapabilityStatusList *caps; 552 MigrationState *s = migrate_get_current(); 553 int i; 554 555 caps = NULL; /* silence compiler warning */ 556 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 557 if (i == MIGRATION_CAPABILITY_X_COLO && !colo_supported()) { 558 continue; 559 } 560 if (head == NULL) { 561 head = g_malloc0(sizeof(*caps)); 562 caps = head; 563 } else { 564 caps->next = g_malloc0(sizeof(*caps)); 565 caps = caps->next; 566 } 567 caps->value = 568 g_malloc(sizeof(*caps->value)); 569 caps->value->capability = i; 570 caps->value->state = s->enabled_capabilities[i]; 571 } 572 573 return head; 574 } 575 576 MigrationParameters *qmp_query_migrate_parameters(Error **errp) 577 { 578 MigrationParameters *params; 579 MigrationState *s = migrate_get_current(); 580 581 params = g_malloc0(sizeof(*params)); 582 params->has_compress_level = true; 583 params->compress_level = s->parameters.compress_level; 584 params->has_compress_threads = true; 585 params->compress_threads = s->parameters.compress_threads; 586 params->has_decompress_threads = true; 587 params->decompress_threads = s->parameters.decompress_threads; 588 params->has_cpu_throttle_initial = true; 589 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial; 590 params->has_cpu_throttle_increment = true; 591 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment; 592 params->has_tls_creds = !!s->parameters.tls_creds; 593 params->tls_creds = g_strdup(s->parameters.tls_creds); 594 params->has_tls_hostname = !!s->parameters.tls_hostname; 595 params->tls_hostname = g_strdup(s->parameters.tls_hostname); 596 params->has_max_bandwidth = true; 597 params->max_bandwidth = s->parameters.max_bandwidth; 598 params->has_downtime_limit = true; 599 params->downtime_limit = s->parameters.downtime_limit; 600 params->has_x_checkpoint_delay = true; 601 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay; 602 603 return params; 604 } 605 606 /* 607 * Return true if we're already in the middle of a migration 608 * (i.e. any of the active or setup states) 609 */ 610 static bool migration_is_setup_or_active(int state) 611 { 612 switch (state) { 613 case MIGRATION_STATUS_ACTIVE: 614 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 615 case MIGRATION_STATUS_SETUP: 616 return true; 617 618 default: 619 return false; 620 621 } 622 } 623 624 static void get_xbzrle_cache_stats(MigrationInfo *info) 625 { 626 if (migrate_use_xbzrle()) { 627 info->has_xbzrle_cache = true; 628 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); 629 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); 630 info->xbzrle_cache->bytes = xbzrle_mig_bytes_transferred(); 631 info->xbzrle_cache->pages = xbzrle_mig_pages_transferred(); 632 info->xbzrle_cache->cache_miss = xbzrle_mig_pages_cache_miss(); 633 info->xbzrle_cache->cache_miss_rate = xbzrle_mig_cache_miss_rate(); 634 info->xbzrle_cache->overflow = xbzrle_mig_pages_overflow(); 635 } 636 } 637 638 static void populate_ram_info(MigrationInfo *info, MigrationState *s) 639 { 640 info->has_ram = true; 641 info->ram = g_malloc0(sizeof(*info->ram)); 642 info->ram->transferred = ram_bytes_transferred(); 643 info->ram->total = ram_bytes_total(); 644 info->ram->duplicate = dup_mig_pages_transferred(); 645 /* legacy value. It is not used anymore */ 646 info->ram->skipped = 0; 647 info->ram->normal = norm_mig_pages_transferred(); 648 info->ram->normal_bytes = norm_mig_pages_transferred() * 649 qemu_target_page_size(); 650 info->ram->mbps = s->mbps; 651 info->ram->dirty_sync_count = ram_dirty_sync_count(); 652 info->ram->postcopy_requests = ram_postcopy_requests(); 653 info->ram->page_size = qemu_target_page_size(); 654 655 if (s->state != MIGRATION_STATUS_COMPLETED) { 656 info->ram->remaining = ram_bytes_remaining(); 657 info->ram->dirty_pages_rate = ram_dirty_pages_rate(); 658 } 659 } 660 661 MigrationInfo *qmp_query_migrate(Error **errp) 662 { 663 MigrationInfo *info = g_malloc0(sizeof(*info)); 664 MigrationState *s = migrate_get_current(); 665 666 switch (s->state) { 667 case MIGRATION_STATUS_NONE: 668 /* no migration has happened ever */ 669 break; 670 case MIGRATION_STATUS_SETUP: 671 info->has_status = true; 672 info->has_total_time = false; 673 break; 674 case MIGRATION_STATUS_ACTIVE: 675 case MIGRATION_STATUS_CANCELLING: 676 info->has_status = true; 677 info->has_total_time = true; 678 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) 679 - s->total_time; 680 info->has_expected_downtime = true; 681 info->expected_downtime = s->expected_downtime; 682 info->has_setup_time = true; 683 info->setup_time = s->setup_time; 684 685 populate_ram_info(info, s); 686 687 if (blk_mig_active()) { 688 info->has_disk = true; 689 info->disk = g_malloc0(sizeof(*info->disk)); 690 info->disk->transferred = blk_mig_bytes_transferred(); 691 info->disk->remaining = blk_mig_bytes_remaining(); 692 info->disk->total = blk_mig_bytes_total(); 693 } 694 695 if (cpu_throttle_active()) { 696 info->has_cpu_throttle_percentage = true; 697 info->cpu_throttle_percentage = cpu_throttle_get_percentage(); 698 } 699 700 get_xbzrle_cache_stats(info); 701 break; 702 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 703 /* Mostly the same as active; TODO add some postcopy stats */ 704 info->has_status = true; 705 info->has_total_time = true; 706 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) 707 - s->total_time; 708 info->has_expected_downtime = true; 709 info->expected_downtime = s->expected_downtime; 710 info->has_setup_time = true; 711 info->setup_time = s->setup_time; 712 713 populate_ram_info(info, s); 714 715 if (blk_mig_active()) { 716 info->has_disk = true; 717 info->disk = g_malloc0(sizeof(*info->disk)); 718 info->disk->transferred = blk_mig_bytes_transferred(); 719 info->disk->remaining = blk_mig_bytes_remaining(); 720 info->disk->total = blk_mig_bytes_total(); 721 } 722 723 get_xbzrle_cache_stats(info); 724 break; 725 case MIGRATION_STATUS_COLO: 726 info->has_status = true; 727 /* TODO: display COLO specific information (checkpoint info etc.) */ 728 break; 729 case MIGRATION_STATUS_COMPLETED: 730 get_xbzrle_cache_stats(info); 731 732 info->has_status = true; 733 info->has_total_time = true; 734 info->total_time = s->total_time; 735 info->has_downtime = true; 736 info->downtime = s->downtime; 737 info->has_setup_time = true; 738 info->setup_time = s->setup_time; 739 740 populate_ram_info(info, s); 741 break; 742 case MIGRATION_STATUS_FAILED: 743 info->has_status = true; 744 if (s->error) { 745 info->has_error_desc = true; 746 info->error_desc = g_strdup(error_get_pretty(s->error)); 747 } 748 break; 749 case MIGRATION_STATUS_CANCELLED: 750 info->has_status = true; 751 break; 752 } 753 info->status = s->state; 754 755 return info; 756 } 757 758 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, 759 Error **errp) 760 { 761 MigrationState *s = migrate_get_current(); 762 MigrationCapabilityStatusList *cap; 763 bool old_postcopy_cap = migrate_postcopy_ram(); 764 765 if (migration_is_setup_or_active(s->state)) { 766 error_setg(errp, QERR_MIGRATION_ACTIVE); 767 return; 768 } 769 770 for (cap = params; cap; cap = cap->next) { 771 if (cap->value->capability == MIGRATION_CAPABILITY_X_COLO) { 772 if (!colo_supported()) { 773 error_setg(errp, "COLO is not currently supported, please" 774 " configure with --enable-colo option in order to" 775 " support COLO feature"); 776 continue; 777 } 778 } 779 s->enabled_capabilities[cap->value->capability] = cap->value->state; 780 } 781 782 if (migrate_postcopy_ram()) { 783 if (migrate_use_compression()) { 784 /* The decompression threads asynchronously write into RAM 785 * rather than use the atomic copies needed to avoid 786 * userfaulting. It should be possible to fix the decompression 787 * threads for compatibility in future. 788 */ 789 error_report("Postcopy is not currently compatible with " 790 "compression"); 791 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] = 792 false; 793 } 794 /* This check is reasonably expensive, so only when it's being 795 * set the first time, also it's only the destination that needs 796 * special support. 797 */ 798 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) && 799 !postcopy_ram_supported_by_host()) { 800 /* postcopy_ram_supported_by_host will have emitted a more 801 * detailed message 802 */ 803 error_report("Postcopy is not supported"); 804 s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM] = 805 false; 806 } 807 } 808 } 809 810 void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp) 811 { 812 MigrationState *s = migrate_get_current(); 813 814 if (params->has_compress_level && 815 (params->compress_level < 0 || params->compress_level > 9)) { 816 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level", 817 "is invalid, it should be in the range of 0 to 9"); 818 return; 819 } 820 if (params->has_compress_threads && 821 (params->compress_threads < 1 || params->compress_threads > 255)) { 822 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 823 "compress_threads", 824 "is invalid, it should be in the range of 1 to 255"); 825 return; 826 } 827 if (params->has_decompress_threads && 828 (params->decompress_threads < 1 || params->decompress_threads > 255)) { 829 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 830 "decompress_threads", 831 "is invalid, it should be in the range of 1 to 255"); 832 return; 833 } 834 if (params->has_cpu_throttle_initial && 835 (params->cpu_throttle_initial < 1 || 836 params->cpu_throttle_initial > 99)) { 837 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 838 "cpu_throttle_initial", 839 "an integer in the range of 1 to 99"); 840 return; 841 } 842 if (params->has_cpu_throttle_increment && 843 (params->cpu_throttle_increment < 1 || 844 params->cpu_throttle_increment > 99)) { 845 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 846 "cpu_throttle_increment", 847 "an integer in the range of 1 to 99"); 848 return; 849 } 850 if (params->has_max_bandwidth && 851 (params->max_bandwidth < 0 || params->max_bandwidth > SIZE_MAX)) { 852 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the" 853 " range of 0 to %zu bytes/second", SIZE_MAX); 854 return; 855 } 856 if (params->has_downtime_limit && 857 (params->downtime_limit < 0 || 858 params->downtime_limit > MAX_MIGRATE_DOWNTIME)) { 859 error_setg(errp, "Parameter 'downtime_limit' expects an integer in " 860 "the range of 0 to %d milliseconds", 861 MAX_MIGRATE_DOWNTIME); 862 return; 863 } 864 if (params->has_x_checkpoint_delay && (params->x_checkpoint_delay < 0)) { 865 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 866 "x_checkpoint_delay", 867 "is invalid, it should be positive"); 868 } 869 870 if (params->has_compress_level) { 871 s->parameters.compress_level = params->compress_level; 872 } 873 if (params->has_compress_threads) { 874 s->parameters.compress_threads = params->compress_threads; 875 } 876 if (params->has_decompress_threads) { 877 s->parameters.decompress_threads = params->decompress_threads; 878 } 879 if (params->has_cpu_throttle_initial) { 880 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial; 881 } 882 if (params->has_cpu_throttle_increment) { 883 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment; 884 } 885 if (params->has_tls_creds) { 886 g_free(s->parameters.tls_creds); 887 s->parameters.tls_creds = g_strdup(params->tls_creds); 888 } 889 if (params->has_tls_hostname) { 890 g_free(s->parameters.tls_hostname); 891 s->parameters.tls_hostname = g_strdup(params->tls_hostname); 892 } 893 if (params->has_max_bandwidth) { 894 s->parameters.max_bandwidth = params->max_bandwidth; 895 if (s->to_dst_file) { 896 qemu_file_set_rate_limit(s->to_dst_file, 897 s->parameters.max_bandwidth / XFER_LIMIT_RATIO); 898 } 899 } 900 if (params->has_downtime_limit) { 901 s->parameters.downtime_limit = params->downtime_limit; 902 } 903 904 if (params->has_x_checkpoint_delay) { 905 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay; 906 if (migration_in_colo_state()) { 907 colo_checkpoint_notify(s); 908 } 909 } 910 } 911 912 913 void qmp_migrate_start_postcopy(Error **errp) 914 { 915 MigrationState *s = migrate_get_current(); 916 917 if (!migrate_postcopy_ram()) { 918 error_setg(errp, "Enable postcopy with migrate_set_capability before" 919 " the start of migration"); 920 return; 921 } 922 923 if (s->state == MIGRATION_STATUS_NONE) { 924 error_setg(errp, "Postcopy must be started after migration has been" 925 " started"); 926 return; 927 } 928 /* 929 * we don't error if migration has finished since that would be racy 930 * with issuing this command. 931 */ 932 atomic_set(&s->start_postcopy, true); 933 } 934 935 /* shared migration helpers */ 936 937 void migrate_set_state(int *state, int old_state, int new_state) 938 { 939 if (atomic_cmpxchg(state, old_state, new_state) == old_state) { 940 trace_migrate_set_state(new_state); 941 migrate_generate_event(new_state); 942 } 943 } 944 945 static void migrate_fd_cleanup(void *opaque) 946 { 947 MigrationState *s = opaque; 948 949 qemu_bh_delete(s->cleanup_bh); 950 s->cleanup_bh = NULL; 951 952 migration_page_queue_free(); 953 954 if (s->to_dst_file) { 955 trace_migrate_fd_cleanup(); 956 qemu_mutex_unlock_iothread(); 957 if (s->migration_thread_running) { 958 qemu_thread_join(&s->thread); 959 s->migration_thread_running = false; 960 } 961 qemu_mutex_lock_iothread(); 962 963 migrate_compress_threads_join(); 964 qemu_fclose(s->to_dst_file); 965 s->to_dst_file = NULL; 966 } 967 968 assert((s->state != MIGRATION_STATUS_ACTIVE) && 969 (s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE)); 970 971 if (s->state == MIGRATION_STATUS_CANCELLING) { 972 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, 973 MIGRATION_STATUS_CANCELLED); 974 } 975 976 notifier_list_notify(&migration_state_notifiers, s); 977 } 978 979 void migrate_fd_error(MigrationState *s, const Error *error) 980 { 981 trace_migrate_fd_error(error_get_pretty(error)); 982 assert(s->to_dst_file == NULL); 983 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 984 MIGRATION_STATUS_FAILED); 985 if (!s->error) { 986 s->error = error_copy(error); 987 } 988 notifier_list_notify(&migration_state_notifiers, s); 989 } 990 991 static void migrate_fd_cancel(MigrationState *s) 992 { 993 int old_state ; 994 QEMUFile *f = migrate_get_current()->to_dst_file; 995 trace_migrate_fd_cancel(); 996 997 if (s->rp_state.from_dst_file) { 998 /* shutdown the rp socket, so causing the rp thread to shutdown */ 999 qemu_file_shutdown(s->rp_state.from_dst_file); 1000 } 1001 1002 do { 1003 old_state = s->state; 1004 if (!migration_is_setup_or_active(old_state)) { 1005 break; 1006 } 1007 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING); 1008 } while (s->state != MIGRATION_STATUS_CANCELLING); 1009 1010 /* 1011 * If we're unlucky the migration code might be stuck somewhere in a 1012 * send/write while the network has failed and is waiting to timeout; 1013 * if we've got shutdown(2) available then we can force it to quit. 1014 * The outgoing qemu file gets closed in migrate_fd_cleanup that is 1015 * called in a bh, so there is no race against this cancel. 1016 */ 1017 if (s->state == MIGRATION_STATUS_CANCELLING && f) { 1018 qemu_file_shutdown(f); 1019 } 1020 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) { 1021 Error *local_err = NULL; 1022 1023 bdrv_invalidate_cache_all(&local_err); 1024 if (local_err) { 1025 error_report_err(local_err); 1026 } else { 1027 s->block_inactive = false; 1028 } 1029 } 1030 } 1031 1032 void add_migration_state_change_notifier(Notifier *notify) 1033 { 1034 notifier_list_add(&migration_state_notifiers, notify); 1035 } 1036 1037 void remove_migration_state_change_notifier(Notifier *notify) 1038 { 1039 notifier_remove(notify); 1040 } 1041 1042 bool migration_in_setup(MigrationState *s) 1043 { 1044 return s->state == MIGRATION_STATUS_SETUP; 1045 } 1046 1047 bool migration_has_finished(MigrationState *s) 1048 { 1049 return s->state == MIGRATION_STATUS_COMPLETED; 1050 } 1051 1052 bool migration_has_failed(MigrationState *s) 1053 { 1054 return (s->state == MIGRATION_STATUS_CANCELLED || 1055 s->state == MIGRATION_STATUS_FAILED); 1056 } 1057 1058 bool migration_in_postcopy(void) 1059 { 1060 MigrationState *s = migrate_get_current(); 1061 1062 return (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); 1063 } 1064 1065 bool migration_in_postcopy_after_devices(MigrationState *s) 1066 { 1067 return migration_in_postcopy() && s->postcopy_after_devices; 1068 } 1069 1070 bool migration_is_idle(void) 1071 { 1072 MigrationState *s = migrate_get_current(); 1073 1074 switch (s->state) { 1075 case MIGRATION_STATUS_NONE: 1076 case MIGRATION_STATUS_CANCELLED: 1077 case MIGRATION_STATUS_COMPLETED: 1078 case MIGRATION_STATUS_FAILED: 1079 return true; 1080 case MIGRATION_STATUS_SETUP: 1081 case MIGRATION_STATUS_CANCELLING: 1082 case MIGRATION_STATUS_ACTIVE: 1083 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1084 case MIGRATION_STATUS_COLO: 1085 return false; 1086 case MIGRATION_STATUS__MAX: 1087 g_assert_not_reached(); 1088 } 1089 1090 return false; 1091 } 1092 1093 MigrationState *migrate_init(const MigrationParams *params) 1094 { 1095 MigrationState *s = migrate_get_current(); 1096 1097 /* 1098 * Reinitialise all migration state, except 1099 * parameters/capabilities that the user set, and 1100 * locks. 1101 */ 1102 s->bytes_xfer = 0; 1103 s->xfer_limit = 0; 1104 s->cleanup_bh = 0; 1105 s->to_dst_file = NULL; 1106 s->state = MIGRATION_STATUS_NONE; 1107 s->params = *params; 1108 s->rp_state.from_dst_file = NULL; 1109 s->rp_state.error = false; 1110 s->mbps = 0.0; 1111 s->downtime = 0; 1112 s->expected_downtime = 0; 1113 s->setup_time = 0; 1114 s->start_postcopy = false; 1115 s->postcopy_after_devices = false; 1116 s->migration_thread_running = false; 1117 error_free(s->error); 1118 s->error = NULL; 1119 1120 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); 1121 1122 s->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 1123 return s; 1124 } 1125 1126 static GSList *migration_blockers; 1127 1128 int migrate_add_blocker(Error *reason, Error **errp) 1129 { 1130 if (only_migratable) { 1131 error_propagate(errp, error_copy(reason)); 1132 error_prepend(errp, "disallowing migration blocker " 1133 "(--only_migratable) for: "); 1134 return -EACCES; 1135 } 1136 1137 if (migration_is_idle()) { 1138 migration_blockers = g_slist_prepend(migration_blockers, reason); 1139 return 0; 1140 } 1141 1142 error_propagate(errp, error_copy(reason)); 1143 error_prepend(errp, "disallowing migration blocker (migration in " 1144 "progress) for: "); 1145 return -EBUSY; 1146 } 1147 1148 void migrate_del_blocker(Error *reason) 1149 { 1150 migration_blockers = g_slist_remove(migration_blockers, reason); 1151 } 1152 1153 int check_migratable(Object *obj, Error **err) 1154 { 1155 DeviceClass *dc = DEVICE_GET_CLASS(obj); 1156 if (only_migratable && dc->vmsd) { 1157 if (dc->vmsd->unmigratable) { 1158 error_setg(err, "Device %s is not migratable, but " 1159 "--only-migratable was specified", 1160 object_get_typename(obj)); 1161 return -1; 1162 } 1163 } 1164 1165 return 0; 1166 } 1167 1168 void qmp_migrate_incoming(const char *uri, Error **errp) 1169 { 1170 Error *local_err = NULL; 1171 static bool once = true; 1172 1173 if (!deferred_incoming) { 1174 error_setg(errp, "For use with '-incoming defer'"); 1175 return; 1176 } 1177 if (!once) { 1178 error_setg(errp, "The incoming migration has already been started"); 1179 } 1180 1181 qemu_start_incoming_migration(uri, &local_err); 1182 1183 if (local_err) { 1184 error_propagate(errp, local_err); 1185 return; 1186 } 1187 1188 once = false; 1189 } 1190 1191 bool migration_is_blocked(Error **errp) 1192 { 1193 if (qemu_savevm_state_blocked(errp)) { 1194 return true; 1195 } 1196 1197 if (migration_blockers) { 1198 *errp = error_copy(migration_blockers->data); 1199 return true; 1200 } 1201 1202 return false; 1203 } 1204 1205 void qmp_migrate(const char *uri, bool has_blk, bool blk, 1206 bool has_inc, bool inc, bool has_detach, bool detach, 1207 Error **errp) 1208 { 1209 Error *local_err = NULL; 1210 MigrationState *s = migrate_get_current(); 1211 MigrationParams params; 1212 const char *p; 1213 1214 params.blk = has_blk && blk; 1215 params.shared = has_inc && inc; 1216 1217 if (migration_is_setup_or_active(s->state) || 1218 s->state == MIGRATION_STATUS_CANCELLING || 1219 s->state == MIGRATION_STATUS_COLO) { 1220 error_setg(errp, QERR_MIGRATION_ACTIVE); 1221 return; 1222 } 1223 if (runstate_check(RUN_STATE_INMIGRATE)) { 1224 error_setg(errp, "Guest is waiting for an incoming migration"); 1225 return; 1226 } 1227 1228 if (migration_is_blocked(errp)) { 1229 return; 1230 } 1231 1232 s = migrate_init(¶ms); 1233 1234 if (strstart(uri, "tcp:", &p)) { 1235 tcp_start_outgoing_migration(s, p, &local_err); 1236 #ifdef CONFIG_RDMA 1237 } else if (strstart(uri, "rdma:", &p)) { 1238 rdma_start_outgoing_migration(s, p, &local_err); 1239 #endif 1240 } else if (strstart(uri, "exec:", &p)) { 1241 exec_start_outgoing_migration(s, p, &local_err); 1242 } else if (strstart(uri, "unix:", &p)) { 1243 unix_start_outgoing_migration(s, p, &local_err); 1244 } else if (strstart(uri, "fd:", &p)) { 1245 fd_start_outgoing_migration(s, p, &local_err); 1246 } else { 1247 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri", 1248 "a valid migration protocol"); 1249 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 1250 MIGRATION_STATUS_FAILED); 1251 return; 1252 } 1253 1254 if (local_err) { 1255 migrate_fd_error(s, local_err); 1256 error_propagate(errp, local_err); 1257 return; 1258 } 1259 } 1260 1261 void qmp_migrate_cancel(Error **errp) 1262 { 1263 migrate_fd_cancel(migrate_get_current()); 1264 } 1265 1266 void qmp_migrate_set_cache_size(int64_t value, Error **errp) 1267 { 1268 MigrationState *s = migrate_get_current(); 1269 int64_t new_size; 1270 1271 /* Check for truncation */ 1272 if (value != (size_t)value) { 1273 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", 1274 "exceeding address space"); 1275 return; 1276 } 1277 1278 /* Cache should not be larger than guest ram size */ 1279 if (value > ram_bytes_total()) { 1280 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", 1281 "exceeds guest ram size "); 1282 return; 1283 } 1284 1285 new_size = xbzrle_cache_resize(value); 1286 if (new_size < 0) { 1287 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", 1288 "is smaller than page size"); 1289 return; 1290 } 1291 1292 s->xbzrle_cache_size = new_size; 1293 } 1294 1295 int64_t qmp_query_migrate_cache_size(Error **errp) 1296 { 1297 return migrate_xbzrle_cache_size(); 1298 } 1299 1300 void qmp_migrate_set_speed(int64_t value, Error **errp) 1301 { 1302 MigrationParameters p = { 1303 .has_max_bandwidth = true, 1304 .max_bandwidth = value, 1305 }; 1306 1307 qmp_migrate_set_parameters(&p, errp); 1308 } 1309 1310 void qmp_migrate_set_downtime(double value, Error **errp) 1311 { 1312 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) { 1313 error_setg(errp, "Parameter 'downtime_limit' expects an integer in " 1314 "the range of 0 to %d seconds", 1315 MAX_MIGRATE_DOWNTIME_SECONDS); 1316 return; 1317 } 1318 1319 value *= 1000; /* Convert to milliseconds */ 1320 value = MAX(0, MIN(INT64_MAX, value)); 1321 1322 MigrationParameters p = { 1323 .has_downtime_limit = true, 1324 .downtime_limit = value, 1325 }; 1326 1327 qmp_migrate_set_parameters(&p, errp); 1328 } 1329 1330 bool migrate_release_ram(void) 1331 { 1332 MigrationState *s; 1333 1334 s = migrate_get_current(); 1335 1336 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM]; 1337 } 1338 1339 bool migrate_postcopy_ram(void) 1340 { 1341 MigrationState *s; 1342 1343 s = migrate_get_current(); 1344 1345 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM]; 1346 } 1347 1348 bool migrate_auto_converge(void) 1349 { 1350 MigrationState *s; 1351 1352 s = migrate_get_current(); 1353 1354 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; 1355 } 1356 1357 bool migrate_zero_blocks(void) 1358 { 1359 MigrationState *s; 1360 1361 s = migrate_get_current(); 1362 1363 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; 1364 } 1365 1366 bool migrate_use_compression(void) 1367 { 1368 MigrationState *s; 1369 1370 s = migrate_get_current(); 1371 1372 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS]; 1373 } 1374 1375 int migrate_compress_level(void) 1376 { 1377 MigrationState *s; 1378 1379 s = migrate_get_current(); 1380 1381 return s->parameters.compress_level; 1382 } 1383 1384 int migrate_compress_threads(void) 1385 { 1386 MigrationState *s; 1387 1388 s = migrate_get_current(); 1389 1390 return s->parameters.compress_threads; 1391 } 1392 1393 int migrate_decompress_threads(void) 1394 { 1395 MigrationState *s; 1396 1397 s = migrate_get_current(); 1398 1399 return s->parameters.decompress_threads; 1400 } 1401 1402 bool migrate_use_events(void) 1403 { 1404 MigrationState *s; 1405 1406 s = migrate_get_current(); 1407 1408 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS]; 1409 } 1410 1411 int migrate_use_xbzrle(void) 1412 { 1413 MigrationState *s; 1414 1415 s = migrate_get_current(); 1416 1417 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; 1418 } 1419 1420 int64_t migrate_xbzrle_cache_size(void) 1421 { 1422 MigrationState *s; 1423 1424 s = migrate_get_current(); 1425 1426 return s->xbzrle_cache_size; 1427 } 1428 1429 /* migration thread support */ 1430 /* 1431 * Something bad happened to the RP stream, mark an error 1432 * The caller shall print or trace something to indicate why 1433 */ 1434 static void mark_source_rp_bad(MigrationState *s) 1435 { 1436 s->rp_state.error = true; 1437 } 1438 1439 static struct rp_cmd_args { 1440 ssize_t len; /* -1 = variable */ 1441 const char *name; 1442 } rp_cmd_args[] = { 1443 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, 1444 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, 1445 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, 1446 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" }, 1447 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" }, 1448 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, 1449 }; 1450 1451 /* 1452 * Process a request for pages received on the return path, 1453 * We're allowed to send more than requested (e.g. to round to our page size) 1454 * and we don't need to send pages that have already been sent. 1455 */ 1456 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, 1457 ram_addr_t start, size_t len) 1458 { 1459 long our_host_ps = getpagesize(); 1460 1461 trace_migrate_handle_rp_req_pages(rbname, start, len); 1462 1463 /* 1464 * Since we currently insist on matching page sizes, just sanity check 1465 * we're being asked for whole host pages. 1466 */ 1467 if (start & (our_host_ps-1) || 1468 (len & (our_host_ps-1))) { 1469 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT 1470 " len: %zd", __func__, start, len); 1471 mark_source_rp_bad(ms); 1472 return; 1473 } 1474 1475 if (ram_save_queue_pages(rbname, start, len)) { 1476 mark_source_rp_bad(ms); 1477 } 1478 } 1479 1480 /* 1481 * Handles messages sent on the return path towards the source VM 1482 * 1483 */ 1484 static void *source_return_path_thread(void *opaque) 1485 { 1486 MigrationState *ms = opaque; 1487 QEMUFile *rp = ms->rp_state.from_dst_file; 1488 uint16_t header_len, header_type; 1489 uint8_t buf[512]; 1490 uint32_t tmp32, sibling_error; 1491 ram_addr_t start = 0; /* =0 to silence warning */ 1492 size_t len = 0, expected_len; 1493 int res; 1494 1495 trace_source_return_path_thread_entry(); 1496 while (!ms->rp_state.error && !qemu_file_get_error(rp) && 1497 migration_is_setup_or_active(ms->state)) { 1498 trace_source_return_path_thread_loop_top(); 1499 header_type = qemu_get_be16(rp); 1500 header_len = qemu_get_be16(rp); 1501 1502 if (header_type >= MIG_RP_MSG_MAX || 1503 header_type == MIG_RP_MSG_INVALID) { 1504 error_report("RP: Received invalid message 0x%04x length 0x%04x", 1505 header_type, header_len); 1506 mark_source_rp_bad(ms); 1507 goto out; 1508 } 1509 1510 if ((rp_cmd_args[header_type].len != -1 && 1511 header_len != rp_cmd_args[header_type].len) || 1512 header_len > sizeof(buf)) { 1513 error_report("RP: Received '%s' message (0x%04x) with" 1514 "incorrect length %d expecting %zu", 1515 rp_cmd_args[header_type].name, header_type, header_len, 1516 (size_t)rp_cmd_args[header_type].len); 1517 mark_source_rp_bad(ms); 1518 goto out; 1519 } 1520 1521 /* We know we've got a valid header by this point */ 1522 res = qemu_get_buffer(rp, buf, header_len); 1523 if (res != header_len) { 1524 error_report("RP: Failed reading data for message 0x%04x" 1525 " read %d expected %d", 1526 header_type, res, header_len); 1527 mark_source_rp_bad(ms); 1528 goto out; 1529 } 1530 1531 /* OK, we have the message and the data */ 1532 switch (header_type) { 1533 case MIG_RP_MSG_SHUT: 1534 sibling_error = ldl_be_p(buf); 1535 trace_source_return_path_thread_shut(sibling_error); 1536 if (sibling_error) { 1537 error_report("RP: Sibling indicated error %d", sibling_error); 1538 mark_source_rp_bad(ms); 1539 } 1540 /* 1541 * We'll let the main thread deal with closing the RP 1542 * we could do a shutdown(2) on it, but we're the only user 1543 * anyway, so there's nothing gained. 1544 */ 1545 goto out; 1546 1547 case MIG_RP_MSG_PONG: 1548 tmp32 = ldl_be_p(buf); 1549 trace_source_return_path_thread_pong(tmp32); 1550 break; 1551 1552 case MIG_RP_MSG_REQ_PAGES: 1553 start = ldq_be_p(buf); 1554 len = ldl_be_p(buf + 8); 1555 migrate_handle_rp_req_pages(ms, NULL, start, len); 1556 break; 1557 1558 case MIG_RP_MSG_REQ_PAGES_ID: 1559 expected_len = 12 + 1; /* header + termination */ 1560 1561 if (header_len >= expected_len) { 1562 start = ldq_be_p(buf); 1563 len = ldl_be_p(buf + 8); 1564 /* Now we expect an idstr */ 1565 tmp32 = buf[12]; /* Length of the following idstr */ 1566 buf[13 + tmp32] = '\0'; 1567 expected_len += tmp32; 1568 } 1569 if (header_len != expected_len) { 1570 error_report("RP: Req_Page_id with length %d expecting %zd", 1571 header_len, expected_len); 1572 mark_source_rp_bad(ms); 1573 goto out; 1574 } 1575 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len); 1576 break; 1577 1578 default: 1579 break; 1580 } 1581 } 1582 if (qemu_file_get_error(rp)) { 1583 trace_source_return_path_thread_bad_end(); 1584 mark_source_rp_bad(ms); 1585 } 1586 1587 trace_source_return_path_thread_end(); 1588 out: 1589 ms->rp_state.from_dst_file = NULL; 1590 qemu_fclose(rp); 1591 return NULL; 1592 } 1593 1594 static int open_return_path_on_source(MigrationState *ms) 1595 { 1596 1597 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file); 1598 if (!ms->rp_state.from_dst_file) { 1599 return -1; 1600 } 1601 1602 trace_open_return_path_on_source(); 1603 qemu_thread_create(&ms->rp_state.rp_thread, "return path", 1604 source_return_path_thread, ms, QEMU_THREAD_JOINABLE); 1605 1606 trace_open_return_path_on_source_continue(); 1607 1608 return 0; 1609 } 1610 1611 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ 1612 static int await_return_path_close_on_source(MigrationState *ms) 1613 { 1614 /* 1615 * If this is a normal exit then the destination will send a SHUT and the 1616 * rp_thread will exit, however if there's an error we need to cause 1617 * it to exit. 1618 */ 1619 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) { 1620 /* 1621 * shutdown(2), if we have it, will cause it to unblock if it's stuck 1622 * waiting for the destination. 1623 */ 1624 qemu_file_shutdown(ms->rp_state.from_dst_file); 1625 mark_source_rp_bad(ms); 1626 } 1627 trace_await_return_path_close_on_source_joining(); 1628 qemu_thread_join(&ms->rp_state.rp_thread); 1629 trace_await_return_path_close_on_source_close(); 1630 return ms->rp_state.error; 1631 } 1632 1633 /* 1634 * Switch from normal iteration to postcopy 1635 * Returns non-0 on error 1636 */ 1637 static int postcopy_start(MigrationState *ms, bool *old_vm_running) 1638 { 1639 int ret; 1640 QIOChannelBuffer *bioc; 1641 QEMUFile *fb; 1642 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 1643 bool restart_block = false; 1644 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE, 1645 MIGRATION_STATUS_POSTCOPY_ACTIVE); 1646 1647 trace_postcopy_start(); 1648 qemu_mutex_lock_iothread(); 1649 trace_postcopy_start_set_run(); 1650 1651 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); 1652 *old_vm_running = runstate_is_running(); 1653 global_state_store(); 1654 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); 1655 if (ret < 0) { 1656 goto fail; 1657 } 1658 1659 ret = bdrv_inactivate_all(); 1660 if (ret < 0) { 1661 goto fail; 1662 } 1663 restart_block = true; 1664 1665 /* 1666 * Cause any non-postcopiable, but iterative devices to 1667 * send out their final data. 1668 */ 1669 qemu_savevm_state_complete_precopy(ms->to_dst_file, true); 1670 1671 /* 1672 * in Finish migrate and with the io-lock held everything should 1673 * be quiet, but we've potentially still got dirty pages and we 1674 * need to tell the destination to throw any pages it's already received 1675 * that are dirty 1676 */ 1677 if (ram_postcopy_send_discard_bitmap(ms)) { 1678 error_report("postcopy send discard bitmap failed"); 1679 goto fail; 1680 } 1681 1682 /* 1683 * send rest of state - note things that are doing postcopy 1684 * will notice we're in POSTCOPY_ACTIVE and not actually 1685 * wrap their state up here 1686 */ 1687 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX); 1688 /* Ping just for debugging, helps line traces up */ 1689 qemu_savevm_send_ping(ms->to_dst_file, 2); 1690 1691 /* 1692 * While loading the device state we may trigger page transfer 1693 * requests and the fd must be free to process those, and thus 1694 * the destination must read the whole device state off the fd before 1695 * it starts processing it. Unfortunately the ad-hoc migration format 1696 * doesn't allow the destination to know the size to read without fully 1697 * parsing it through each devices load-state code (especially the open 1698 * coded devices that use get/put). 1699 * So we wrap the device state up in a package with a length at the start; 1700 * to do this we use a qemu_buf to hold the whole of the device state. 1701 */ 1702 bioc = qio_channel_buffer_new(4096); 1703 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer"); 1704 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc)); 1705 object_unref(OBJECT(bioc)); 1706 1707 /* 1708 * Make sure the receiver can get incoming pages before we send the rest 1709 * of the state 1710 */ 1711 qemu_savevm_send_postcopy_listen(fb); 1712 1713 qemu_savevm_state_complete_precopy(fb, false); 1714 qemu_savevm_send_ping(fb, 3); 1715 1716 qemu_savevm_send_postcopy_run(fb); 1717 1718 /* <><> end of stuff going into the package */ 1719 1720 /* Last point of recovery; as soon as we send the package the destination 1721 * can open devices and potentially start running. 1722 * Lets just check again we've not got any errors. 1723 */ 1724 ret = qemu_file_get_error(ms->to_dst_file); 1725 if (ret) { 1726 error_report("postcopy_start: Migration stream errored (pre package)"); 1727 goto fail_closefb; 1728 } 1729 1730 restart_block = false; 1731 1732 /* Now send that blob */ 1733 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) { 1734 goto fail_closefb; 1735 } 1736 qemu_fclose(fb); 1737 1738 /* Send a notify to give a chance for anything that needs to happen 1739 * at the transition to postcopy and after the device state; in particular 1740 * spice needs to trigger a transition now 1741 */ 1742 ms->postcopy_after_devices = true; 1743 notifier_list_notify(&migration_state_notifiers, ms); 1744 1745 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop; 1746 1747 qemu_mutex_unlock_iothread(); 1748 1749 /* 1750 * Although this ping is just for debug, it could potentially be 1751 * used for getting a better measurement of downtime at the source. 1752 */ 1753 qemu_savevm_send_ping(ms->to_dst_file, 4); 1754 1755 if (migrate_release_ram()) { 1756 ram_postcopy_migrated_memory_release(ms); 1757 } 1758 1759 ret = qemu_file_get_error(ms->to_dst_file); 1760 if (ret) { 1761 error_report("postcopy_start: Migration stream errored"); 1762 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, 1763 MIGRATION_STATUS_FAILED); 1764 } 1765 1766 return ret; 1767 1768 fail_closefb: 1769 qemu_fclose(fb); 1770 fail: 1771 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, 1772 MIGRATION_STATUS_FAILED); 1773 if (restart_block) { 1774 /* A failure happened early enough that we know the destination hasn't 1775 * accessed block devices, so we're safe to recover. 1776 */ 1777 Error *local_err = NULL; 1778 1779 bdrv_invalidate_cache_all(&local_err); 1780 if (local_err) { 1781 error_report_err(local_err); 1782 } 1783 } 1784 qemu_mutex_unlock_iothread(); 1785 return -1; 1786 } 1787 1788 /** 1789 * migration_completion: Used by migration_thread when there's not much left. 1790 * The caller 'breaks' the loop when this returns. 1791 * 1792 * @s: Current migration state 1793 * @current_active_state: The migration state we expect to be in 1794 * @*old_vm_running: Pointer to old_vm_running flag 1795 * @*start_time: Pointer to time to update 1796 */ 1797 static void migration_completion(MigrationState *s, int current_active_state, 1798 bool *old_vm_running, 1799 int64_t *start_time) 1800 { 1801 int ret; 1802 1803 if (s->state == MIGRATION_STATUS_ACTIVE) { 1804 qemu_mutex_lock_iothread(); 1805 *start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 1806 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER); 1807 *old_vm_running = runstate_is_running(); 1808 ret = global_state_store(); 1809 1810 if (!ret) { 1811 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); 1812 /* 1813 * Don't mark the image with BDRV_O_INACTIVE flag if 1814 * we will go into COLO stage later. 1815 */ 1816 if (ret >= 0 && !migrate_colo_enabled()) { 1817 ret = bdrv_inactivate_all(); 1818 } 1819 if (ret >= 0) { 1820 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX); 1821 qemu_savevm_state_complete_precopy(s->to_dst_file, false); 1822 s->block_inactive = true; 1823 } 1824 } 1825 qemu_mutex_unlock_iothread(); 1826 1827 if (ret < 0) { 1828 goto fail; 1829 } 1830 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 1831 trace_migration_completion_postcopy_end(); 1832 1833 qemu_savevm_state_complete_postcopy(s->to_dst_file); 1834 trace_migration_completion_postcopy_end_after_complete(); 1835 } 1836 1837 /* 1838 * If rp was opened we must clean up the thread before 1839 * cleaning everything else up (since if there are no failures 1840 * it will wait for the destination to send it's status in 1841 * a SHUT command). 1842 * Postcopy opens rp if enabled (even if it's not avtivated) 1843 */ 1844 if (migrate_postcopy_ram()) { 1845 int rp_error; 1846 trace_migration_completion_postcopy_end_before_rp(); 1847 rp_error = await_return_path_close_on_source(s); 1848 trace_migration_completion_postcopy_end_after_rp(rp_error); 1849 if (rp_error) { 1850 goto fail_invalidate; 1851 } 1852 } 1853 1854 if (qemu_file_get_error(s->to_dst_file)) { 1855 trace_migration_completion_file_err(); 1856 goto fail_invalidate; 1857 } 1858 1859 if (!migrate_colo_enabled()) { 1860 migrate_set_state(&s->state, current_active_state, 1861 MIGRATION_STATUS_COMPLETED); 1862 } 1863 1864 return; 1865 1866 fail_invalidate: 1867 /* If not doing postcopy, vm_start() will be called: let's regain 1868 * control on images. 1869 */ 1870 if (s->state == MIGRATION_STATUS_ACTIVE) { 1871 Error *local_err = NULL; 1872 1873 qemu_mutex_lock_iothread(); 1874 bdrv_invalidate_cache_all(&local_err); 1875 if (local_err) { 1876 error_report_err(local_err); 1877 } else { 1878 s->block_inactive = false; 1879 } 1880 qemu_mutex_unlock_iothread(); 1881 } 1882 1883 fail: 1884 migrate_set_state(&s->state, current_active_state, 1885 MIGRATION_STATUS_FAILED); 1886 } 1887 1888 bool migrate_colo_enabled(void) 1889 { 1890 MigrationState *s = migrate_get_current(); 1891 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO]; 1892 } 1893 1894 /* 1895 * Master migration thread on the source VM. 1896 * It drives the migration and pumps the data down the outgoing channel. 1897 */ 1898 static void *migration_thread(void *opaque) 1899 { 1900 MigrationState *s = opaque; 1901 /* Used by the bandwidth calcs, updated later */ 1902 int64_t initial_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 1903 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); 1904 int64_t initial_bytes = 0; 1905 /* 1906 * The final stage happens when the remaining data is smaller than 1907 * this threshold; it's calculated from the requested downtime and 1908 * measured bandwidth 1909 */ 1910 int64_t threshold_size = 0; 1911 int64_t start_time = initial_time; 1912 int64_t end_time; 1913 bool old_vm_running = false; 1914 bool entered_postcopy = false; 1915 /* The active state we expect to be in; ACTIVE or POSTCOPY_ACTIVE */ 1916 enum MigrationStatus current_active_state = MIGRATION_STATUS_ACTIVE; 1917 bool enable_colo = migrate_colo_enabled(); 1918 1919 rcu_register_thread(); 1920 1921 qemu_savevm_state_header(s->to_dst_file); 1922 1923 if (migrate_postcopy_ram()) { 1924 /* Now tell the dest that it should open its end so it can reply */ 1925 qemu_savevm_send_open_return_path(s->to_dst_file); 1926 1927 /* And do a ping that will make stuff easier to debug */ 1928 qemu_savevm_send_ping(s->to_dst_file, 1); 1929 1930 /* 1931 * Tell the destination that we *might* want to do postcopy later; 1932 * if the other end can't do postcopy it should fail now, nice and 1933 * early. 1934 */ 1935 qemu_savevm_send_postcopy_advise(s->to_dst_file); 1936 } 1937 1938 qemu_savevm_state_begin(s->to_dst_file, &s->params); 1939 1940 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; 1941 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 1942 MIGRATION_STATUS_ACTIVE); 1943 1944 trace_migration_thread_setup_complete(); 1945 1946 while (s->state == MIGRATION_STATUS_ACTIVE || 1947 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 1948 int64_t current_time; 1949 uint64_t pending_size; 1950 1951 if (!qemu_file_rate_limit(s->to_dst_file)) { 1952 uint64_t pend_post, pend_nonpost; 1953 1954 qemu_savevm_state_pending(s->to_dst_file, threshold_size, 1955 &pend_nonpost, &pend_post); 1956 pending_size = pend_nonpost + pend_post; 1957 trace_migrate_pending(pending_size, threshold_size, 1958 pend_post, pend_nonpost); 1959 if (pending_size && pending_size >= threshold_size) { 1960 /* Still a significant amount to transfer */ 1961 1962 if (migrate_postcopy_ram() && 1963 s->state != MIGRATION_STATUS_POSTCOPY_ACTIVE && 1964 pend_nonpost <= threshold_size && 1965 atomic_read(&s->start_postcopy)) { 1966 1967 if (!postcopy_start(s, &old_vm_running)) { 1968 current_active_state = MIGRATION_STATUS_POSTCOPY_ACTIVE; 1969 entered_postcopy = true; 1970 } 1971 1972 continue; 1973 } 1974 /* Just another iteration step */ 1975 qemu_savevm_state_iterate(s->to_dst_file, entered_postcopy); 1976 } else { 1977 trace_migration_thread_low_pending(pending_size); 1978 migration_completion(s, current_active_state, 1979 &old_vm_running, &start_time); 1980 break; 1981 } 1982 } 1983 1984 if (qemu_file_get_error(s->to_dst_file)) { 1985 migrate_set_state(&s->state, current_active_state, 1986 MIGRATION_STATUS_FAILED); 1987 trace_migration_thread_file_err(); 1988 break; 1989 } 1990 current_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 1991 if (current_time >= initial_time + BUFFER_DELAY) { 1992 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file) - 1993 initial_bytes; 1994 uint64_t time_spent = current_time - initial_time; 1995 double bandwidth = (double)transferred_bytes / time_spent; 1996 threshold_size = bandwidth * s->parameters.downtime_limit; 1997 1998 s->mbps = (((double) transferred_bytes * 8.0) / 1999 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; 2000 2001 trace_migrate_transferred(transferred_bytes, time_spent, 2002 bandwidth, threshold_size); 2003 /* if we haven't sent anything, we don't want to recalculate 2004 10000 is a small enough number for our purposes */ 2005 if (ram_dirty_pages_rate() && transferred_bytes > 10000) { 2006 s->expected_downtime = ram_dirty_pages_rate() * 2007 qemu_target_page_size() / bandwidth; 2008 } 2009 2010 qemu_file_reset_rate_limit(s->to_dst_file); 2011 initial_time = current_time; 2012 initial_bytes = qemu_ftell(s->to_dst_file); 2013 } 2014 if (qemu_file_rate_limit(s->to_dst_file)) { 2015 /* usleep expects microseconds */ 2016 g_usleep((initial_time + BUFFER_DELAY - current_time)*1000); 2017 } 2018 } 2019 2020 trace_migration_thread_after_loop(); 2021 /* If we enabled cpu throttling for auto-converge, turn it off. */ 2022 cpu_throttle_stop(); 2023 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 2024 2025 qemu_mutex_lock_iothread(); 2026 /* 2027 * The resource has been allocated by migration will be reused in COLO 2028 * process, so don't release them. 2029 */ 2030 if (!enable_colo) { 2031 qemu_savevm_state_cleanup(); 2032 } 2033 if (s->state == MIGRATION_STATUS_COMPLETED) { 2034 uint64_t transferred_bytes = qemu_ftell(s->to_dst_file); 2035 s->total_time = end_time - s->total_time; 2036 if (!entered_postcopy) { 2037 s->downtime = end_time - start_time; 2038 } 2039 if (s->total_time) { 2040 s->mbps = (((double) transferred_bytes * 8.0) / 2041 ((double) s->total_time)) / 1000; 2042 } 2043 runstate_set(RUN_STATE_POSTMIGRATE); 2044 } else { 2045 if (s->state == MIGRATION_STATUS_ACTIVE && enable_colo) { 2046 migrate_start_colo_process(s); 2047 qemu_savevm_state_cleanup(); 2048 /* 2049 * Fixme: we will run VM in COLO no matter its old running state. 2050 * After exited COLO, we will keep running. 2051 */ 2052 old_vm_running = true; 2053 } 2054 if (old_vm_running && !entered_postcopy) { 2055 vm_start(); 2056 } else { 2057 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) { 2058 runstate_set(RUN_STATE_POSTMIGRATE); 2059 } 2060 } 2061 } 2062 qemu_bh_schedule(s->cleanup_bh); 2063 qemu_mutex_unlock_iothread(); 2064 2065 rcu_unregister_thread(); 2066 return NULL; 2067 } 2068 2069 void migrate_fd_connect(MigrationState *s) 2070 { 2071 s->expected_downtime = s->parameters.downtime_limit; 2072 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup, s); 2073 2074 qemu_file_set_blocking(s->to_dst_file, true); 2075 qemu_file_set_rate_limit(s->to_dst_file, 2076 s->parameters.max_bandwidth / XFER_LIMIT_RATIO); 2077 2078 /* Notify before starting migration thread */ 2079 notifier_list_notify(&migration_state_notifiers, s); 2080 2081 /* 2082 * Open the return path; currently for postcopy but other things might 2083 * also want it. 2084 */ 2085 if (migrate_postcopy_ram()) { 2086 if (open_return_path_on_source(s)) { 2087 error_report("Unable to open return-path for postcopy"); 2088 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 2089 MIGRATION_STATUS_FAILED); 2090 migrate_fd_cleanup(s); 2091 return; 2092 } 2093 } 2094 2095 migrate_compress_threads_create(); 2096 qemu_thread_create(&s->thread, "live_migration", migration_thread, s, 2097 QEMU_THREAD_JOINABLE); 2098 s->migration_thread_running = true; 2099 } 2100 2101 PostcopyState postcopy_state_get(void) 2102 { 2103 return atomic_mb_read(&incoming_postcopy_state); 2104 } 2105 2106 /* Set the state and return the old state */ 2107 PostcopyState postcopy_state_set(PostcopyState new_state) 2108 { 2109 return atomic_xchg(&incoming_postcopy_state, new_state); 2110 } 2111 2112