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 "exec.h" 22 #include "fd.h" 23 #include "socket.h" 24 #include "sysemu/runstate.h" 25 #include "sysemu/sysemu.h" 26 #include "rdma.h" 27 #include "ram.h" 28 #include "migration/global_state.h" 29 #include "migration/misc.h" 30 #include "migration.h" 31 #include "savevm.h" 32 #include "qemu-file-channel.h" 33 #include "qemu-file.h" 34 #include "migration/vmstate.h" 35 #include "block/block.h" 36 #include "qapi/error.h" 37 #include "qapi/clone-visitor.h" 38 #include "qapi/qapi-visit-sockets.h" 39 #include "qapi/qapi-commands-migration.h" 40 #include "qapi/qapi-events-migration.h" 41 #include "qapi/qmp/qerror.h" 42 #include "qapi/qmp/qnull.h" 43 #include "qemu/rcu.h" 44 #include "block.h" 45 #include "postcopy-ram.h" 46 #include "qemu/thread.h" 47 #include "trace.h" 48 #include "exec/target_page.h" 49 #include "io/channel-buffer.h" 50 #include "migration/colo.h" 51 #include "hw/boards.h" 52 #include "hw/qdev-properties.h" 53 #include "monitor/monitor.h" 54 #include "net/announce.h" 55 #include "qemu/queue.h" 56 #include "multifd.h" 57 58 #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */ 59 60 /* Amount of time to allocate to each "chunk" of bandwidth-throttled 61 * data. */ 62 #define BUFFER_DELAY 100 63 #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) 64 65 /* Time in milliseconds we are allowed to stop the source, 66 * for sending the last part */ 67 #define DEFAULT_MIGRATE_SET_DOWNTIME 300 68 69 /* Maximum migrate downtime set to 2000 seconds */ 70 #define MAX_MIGRATE_DOWNTIME_SECONDS 2000 71 #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000) 72 73 /* Default compression thread count */ 74 #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 75 /* Default decompression thread count, usually decompression is at 76 * least 4 times as fast as compression.*/ 77 #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 78 /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ 79 #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 80 /* Define default autoconverge cpu throttle migration parameters */ 81 #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20 82 #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10 83 #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99 84 85 /* Migration XBZRLE default cache size */ 86 #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024) 87 88 /* The delay time (in ms) between two COLO checkpoints */ 89 #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100) 90 #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2 91 #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE 92 /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */ 93 #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1 94 /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */ 95 #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1 96 97 /* Background transfer rate for postcopy, 0 means unlimited, note 98 * that page requests can still exceed this limit. 99 */ 100 #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0 101 102 /* 103 * Parameters for self_announce_delay giving a stream of RARP/ARP 104 * packets after migration. 105 */ 106 #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50 107 #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550 108 #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5 109 #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100 110 111 static NotifierList migration_state_notifiers = 112 NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); 113 114 static bool deferred_incoming; 115 116 /* Messages sent on the return path from destination to source */ 117 enum mig_rp_message_type { 118 MIG_RP_MSG_INVALID = 0, /* Must be 0 */ 119 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */ 120 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */ 121 122 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */ 123 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */ 124 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */ 125 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */ 126 127 MIG_RP_MSG_MAX 128 }; 129 130 /* When we add fault tolerance, we could have several 131 migrations at once. For now we don't need to add 132 dynamic creation of migration */ 133 134 static MigrationState *current_migration; 135 static MigrationIncomingState *current_incoming; 136 137 static bool migration_object_check(MigrationState *ms, Error **errp); 138 static int migration_maybe_pause(MigrationState *s, 139 int *current_active_state, 140 int new_state); 141 static void migrate_fd_cancel(MigrationState *s); 142 143 void migration_object_init(void) 144 { 145 MachineState *ms = MACHINE(qdev_get_machine()); 146 Error *err = NULL; 147 148 /* This can only be called once. */ 149 assert(!current_migration); 150 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION)); 151 152 /* 153 * Init the migrate incoming object as well no matter whether 154 * we'll use it or not. 155 */ 156 assert(!current_incoming); 157 current_incoming = g_new0(MigrationIncomingState, 1); 158 current_incoming->state = MIGRATION_STATUS_NONE; 159 current_incoming->postcopy_remote_fds = 160 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD)); 161 qemu_mutex_init(¤t_incoming->rp_mutex); 162 qemu_event_init(¤t_incoming->main_thread_load_event, false); 163 qemu_sem_init(¤t_incoming->postcopy_pause_sem_dst, 0); 164 qemu_sem_init(¤t_incoming->postcopy_pause_sem_fault, 0); 165 166 init_dirty_bitmap_incoming_migration(); 167 168 if (!migration_object_check(current_migration, &err)) { 169 error_report_err(err); 170 exit(1); 171 } 172 173 /* 174 * We cannot really do this in migration_instance_init() since at 175 * that time global properties are not yet applied, then this 176 * value will be definitely replaced by something else. 177 */ 178 if (ms->enforce_config_section) { 179 current_migration->send_configuration = true; 180 } 181 } 182 183 void migration_shutdown(void) 184 { 185 /* 186 * Cancel the current migration - that will (eventually) 187 * stop the migration using this structure 188 */ 189 migrate_fd_cancel(current_migration); 190 object_unref(OBJECT(current_migration)); 191 } 192 193 /* For outgoing */ 194 MigrationState *migrate_get_current(void) 195 { 196 /* This can only be called after the object created. */ 197 assert(current_migration); 198 return current_migration; 199 } 200 201 MigrationIncomingState *migration_incoming_get_current(void) 202 { 203 assert(current_incoming); 204 return current_incoming; 205 } 206 207 void migration_incoming_state_destroy(void) 208 { 209 struct MigrationIncomingState *mis = migration_incoming_get_current(); 210 211 if (mis->to_src_file) { 212 /* Tell source that we are done */ 213 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0); 214 qemu_fclose(mis->to_src_file); 215 mis->to_src_file = NULL; 216 } 217 218 if (mis->from_src_file) { 219 qemu_fclose(mis->from_src_file); 220 mis->from_src_file = NULL; 221 } 222 if (mis->postcopy_remote_fds) { 223 g_array_free(mis->postcopy_remote_fds, TRUE); 224 mis->postcopy_remote_fds = NULL; 225 } 226 227 qemu_event_reset(&mis->main_thread_load_event); 228 229 if (mis->socket_address_list) { 230 qapi_free_SocketAddressList(mis->socket_address_list); 231 mis->socket_address_list = NULL; 232 } 233 } 234 235 static void migrate_generate_event(int new_state) 236 { 237 if (migrate_use_events()) { 238 qapi_event_send_migration(new_state); 239 } 240 } 241 242 static bool migrate_late_block_activate(void) 243 { 244 MigrationState *s; 245 246 s = migrate_get_current(); 247 248 return s->enabled_capabilities[ 249 MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE]; 250 } 251 252 /* 253 * Called on -incoming with a defer: uri. 254 * The migration can be started later after any parameters have been 255 * changed. 256 */ 257 static void deferred_incoming_migration(Error **errp) 258 { 259 if (deferred_incoming) { 260 error_setg(errp, "Incoming migration already deferred"); 261 } 262 deferred_incoming = true; 263 } 264 265 /* 266 * Send a message on the return channel back to the source 267 * of the migration. 268 */ 269 static int migrate_send_rp_message(MigrationIncomingState *mis, 270 enum mig_rp_message_type message_type, 271 uint16_t len, void *data) 272 { 273 int ret = 0; 274 275 trace_migrate_send_rp_message((int)message_type, len); 276 qemu_mutex_lock(&mis->rp_mutex); 277 278 /* 279 * It's possible that the file handle got lost due to network 280 * failures. 281 */ 282 if (!mis->to_src_file) { 283 ret = -EIO; 284 goto error; 285 } 286 287 qemu_put_be16(mis->to_src_file, (unsigned int)message_type); 288 qemu_put_be16(mis->to_src_file, len); 289 qemu_put_buffer(mis->to_src_file, data, len); 290 qemu_fflush(mis->to_src_file); 291 292 /* It's possible that qemu file got error during sending */ 293 ret = qemu_file_get_error(mis->to_src_file); 294 295 error: 296 qemu_mutex_unlock(&mis->rp_mutex); 297 return ret; 298 } 299 300 /* Request a range of pages from the source VM at the given 301 * start address. 302 * rbname: Name of the RAMBlock to request the page in, if NULL it's the same 303 * as the last request (a name must have been given previously) 304 * Start: Address offset within the RB 305 * Len: Length in bytes required - must be a multiple of pagesize 306 */ 307 int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname, 308 ram_addr_t start, size_t len) 309 { 310 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */ 311 size_t msglen = 12; /* start + len */ 312 enum mig_rp_message_type msg_type; 313 314 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start); 315 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len); 316 317 if (rbname) { 318 int rbname_len = strlen(rbname); 319 assert(rbname_len < 256); 320 321 bufc[msglen++] = rbname_len; 322 memcpy(bufc + msglen, rbname, rbname_len); 323 msglen += rbname_len; 324 msg_type = MIG_RP_MSG_REQ_PAGES_ID; 325 } else { 326 msg_type = MIG_RP_MSG_REQ_PAGES; 327 } 328 329 return migrate_send_rp_message(mis, msg_type, msglen, bufc); 330 } 331 332 static bool migration_colo_enabled; 333 bool migration_incoming_colo_enabled(void) 334 { 335 return migration_colo_enabled; 336 } 337 338 void migration_incoming_disable_colo(void) 339 { 340 migration_colo_enabled = false; 341 } 342 343 void migration_incoming_enable_colo(void) 344 { 345 migration_colo_enabled = true; 346 } 347 348 void migrate_add_address(SocketAddress *address) 349 { 350 MigrationIncomingState *mis = migration_incoming_get_current(); 351 SocketAddressList *addrs; 352 353 addrs = g_new0(SocketAddressList, 1); 354 addrs->next = mis->socket_address_list; 355 mis->socket_address_list = addrs; 356 addrs->value = QAPI_CLONE(SocketAddress, address); 357 } 358 359 void qemu_start_incoming_migration(const char *uri, Error **errp) 360 { 361 const char *p; 362 363 qapi_event_send_migration(MIGRATION_STATUS_SETUP); 364 if (!strcmp(uri, "defer")) { 365 deferred_incoming_migration(errp); 366 } else if (strstart(uri, "tcp:", &p)) { 367 tcp_start_incoming_migration(p, errp); 368 #ifdef CONFIG_RDMA 369 } else if (strstart(uri, "rdma:", &p)) { 370 rdma_start_incoming_migration(p, errp); 371 #endif 372 } else if (strstart(uri, "exec:", &p)) { 373 exec_start_incoming_migration(p, errp); 374 } else if (strstart(uri, "unix:", &p)) { 375 unix_start_incoming_migration(p, errp); 376 } else if (strstart(uri, "fd:", &p)) { 377 fd_start_incoming_migration(p, errp); 378 } else { 379 error_setg(errp, "unknown migration protocol: %s", uri); 380 } 381 } 382 383 static void process_incoming_migration_bh(void *opaque) 384 { 385 Error *local_err = NULL; 386 MigrationIncomingState *mis = opaque; 387 388 /* If capability late_block_activate is set: 389 * Only fire up the block code now if we're going to restart the 390 * VM, else 'cont' will do it. 391 * This causes file locking to happen; so we don't want it to happen 392 * unless we really are starting the VM. 393 */ 394 if (!migrate_late_block_activate() || 395 (autostart && (!global_state_received() || 396 global_state_get_runstate() == RUN_STATE_RUNNING))) { 397 /* Make sure all file formats flush their mutable metadata. 398 * If we get an error here, just don't restart the VM yet. */ 399 bdrv_invalidate_cache_all(&local_err); 400 if (local_err) { 401 error_report_err(local_err); 402 local_err = NULL; 403 autostart = false; 404 } 405 } 406 407 /* 408 * This must happen after all error conditions are dealt with and 409 * we're sure the VM is going to be running on this host. 410 */ 411 qemu_announce_self(&mis->announce_timer, migrate_announce_params()); 412 413 if (multifd_load_cleanup(&local_err) != 0) { 414 error_report_err(local_err); 415 autostart = false; 416 } 417 /* If global state section was not received or we are in running 418 state, we need to obey autostart. Any other state is set with 419 runstate_set. */ 420 421 dirty_bitmap_mig_before_vm_start(); 422 423 if (!global_state_received() || 424 global_state_get_runstate() == RUN_STATE_RUNNING) { 425 if (autostart) { 426 vm_start(); 427 } else { 428 runstate_set(RUN_STATE_PAUSED); 429 } 430 } else if (migration_incoming_colo_enabled()) { 431 migration_incoming_disable_colo(); 432 vm_start(); 433 } else { 434 runstate_set(global_state_get_runstate()); 435 } 436 /* 437 * This must happen after any state changes since as soon as an external 438 * observer sees this event they might start to prod at the VM assuming 439 * it's ready to use. 440 */ 441 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 442 MIGRATION_STATUS_COMPLETED); 443 qemu_bh_delete(mis->bh); 444 migration_incoming_state_destroy(); 445 } 446 447 static void process_incoming_migration_co(void *opaque) 448 { 449 MigrationIncomingState *mis = migration_incoming_get_current(); 450 PostcopyState ps; 451 int ret; 452 Error *local_err = NULL; 453 454 assert(mis->from_src_file); 455 mis->migration_incoming_co = qemu_coroutine_self(); 456 mis->largest_page_size = qemu_ram_pagesize_largest(); 457 postcopy_state_set(POSTCOPY_INCOMING_NONE); 458 migrate_set_state(&mis->state, MIGRATION_STATUS_NONE, 459 MIGRATION_STATUS_ACTIVE); 460 ret = qemu_loadvm_state(mis->from_src_file); 461 462 ps = postcopy_state_get(); 463 trace_process_incoming_migration_co_end(ret, ps); 464 if (ps != POSTCOPY_INCOMING_NONE) { 465 if (ps == POSTCOPY_INCOMING_ADVISE) { 466 /* 467 * Where a migration had postcopy enabled (and thus went to advise) 468 * but managed to complete within the precopy period, we can use 469 * the normal exit. 470 */ 471 postcopy_ram_incoming_cleanup(mis); 472 } else if (ret >= 0) { 473 /* 474 * Postcopy was started, cleanup should happen at the end of the 475 * postcopy thread. 476 */ 477 trace_process_incoming_migration_co_postcopy_end_main(); 478 return; 479 } 480 /* Else if something went wrong then just fall out of the normal exit */ 481 } 482 483 /* we get COLO info, and know if we are in COLO mode */ 484 if (!ret && migration_incoming_colo_enabled()) { 485 /* Make sure all file formats flush their mutable metadata */ 486 bdrv_invalidate_cache_all(&local_err); 487 if (local_err) { 488 error_report_err(local_err); 489 goto fail; 490 } 491 492 qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming", 493 colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE); 494 mis->have_colo_incoming_thread = true; 495 qemu_coroutine_yield(); 496 497 /* Wait checkpoint incoming thread exit before free resource */ 498 qemu_thread_join(&mis->colo_incoming_thread); 499 /* We hold the global iothread lock, so it is safe here */ 500 colo_release_ram_cache(); 501 } 502 503 if (ret < 0) { 504 error_report("load of migration failed: %s", strerror(-ret)); 505 goto fail; 506 } 507 mis->bh = qemu_bh_new(process_incoming_migration_bh, mis); 508 qemu_bh_schedule(mis->bh); 509 mis->migration_incoming_co = NULL; 510 return; 511 fail: 512 local_err = NULL; 513 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 514 MIGRATION_STATUS_FAILED); 515 qemu_fclose(mis->from_src_file); 516 if (multifd_load_cleanup(&local_err) != 0) { 517 error_report_err(local_err); 518 } 519 exit(EXIT_FAILURE); 520 } 521 522 /** 523 * @migration_incoming_setup: Setup incoming migration 524 * 525 * Returns 0 for no error or 1 for error 526 * 527 * @f: file for main migration channel 528 * @errp: where to put errors 529 */ 530 static int migration_incoming_setup(QEMUFile *f, Error **errp) 531 { 532 MigrationIncomingState *mis = migration_incoming_get_current(); 533 Error *local_err = NULL; 534 535 if (multifd_load_setup(&local_err) != 0) { 536 /* We haven't been able to create multifd threads 537 nothing better to do */ 538 error_report_err(local_err); 539 exit(EXIT_FAILURE); 540 } 541 542 if (!mis->from_src_file) { 543 mis->from_src_file = f; 544 } 545 qemu_file_set_blocking(f, false); 546 return 0; 547 } 548 549 void migration_incoming_process(void) 550 { 551 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL); 552 qemu_coroutine_enter(co); 553 } 554 555 /* Returns true if recovered from a paused migration, otherwise false */ 556 static bool postcopy_try_recover(QEMUFile *f) 557 { 558 MigrationIncomingState *mis = migration_incoming_get_current(); 559 560 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { 561 /* Resumed from a paused postcopy migration */ 562 563 mis->from_src_file = f; 564 /* Postcopy has standalone thread to do vm load */ 565 qemu_file_set_blocking(f, true); 566 567 /* Re-configure the return path */ 568 mis->to_src_file = qemu_file_get_return_path(f); 569 570 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED, 571 MIGRATION_STATUS_POSTCOPY_RECOVER); 572 573 /* 574 * Here, we only wake up the main loading thread (while the 575 * fault thread will still be waiting), so that we can receive 576 * commands from source now, and answer it if needed. The 577 * fault thread will be woken up afterwards until we are sure 578 * that source is ready to reply to page requests. 579 */ 580 qemu_sem_post(&mis->postcopy_pause_sem_dst); 581 return true; 582 } 583 584 return false; 585 } 586 587 void migration_fd_process_incoming(QEMUFile *f, Error **errp) 588 { 589 Error *local_err = NULL; 590 591 if (postcopy_try_recover(f)) { 592 return; 593 } 594 595 if (migration_incoming_setup(f, &local_err)) { 596 if (local_err) { 597 error_propagate(errp, local_err); 598 } 599 return; 600 } 601 migration_incoming_process(); 602 } 603 604 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp) 605 { 606 MigrationIncomingState *mis = migration_incoming_get_current(); 607 Error *local_err = NULL; 608 bool start_migration; 609 610 if (!mis->from_src_file) { 611 /* The first connection (multifd may have multiple) */ 612 QEMUFile *f = qemu_fopen_channel_input(ioc); 613 614 /* If it's a recovery, we're done */ 615 if (postcopy_try_recover(f)) { 616 return; 617 } 618 619 if (migration_incoming_setup(f, &local_err)) { 620 if (local_err) { 621 error_propagate(errp, local_err); 622 } 623 return; 624 } 625 626 /* 627 * Common migration only needs one channel, so we can start 628 * right now. Multifd needs more than one channel, we wait. 629 */ 630 start_migration = !migrate_use_multifd(); 631 } else { 632 /* Multiple connections */ 633 assert(migrate_use_multifd()); 634 start_migration = multifd_recv_new_channel(ioc, &local_err); 635 if (local_err) { 636 error_propagate(errp, local_err); 637 return; 638 } 639 } 640 641 if (start_migration) { 642 migration_incoming_process(); 643 } 644 } 645 646 /** 647 * @migration_has_all_channels: We have received all channels that we need 648 * 649 * Returns true when we have got connections to all the channels that 650 * we need for migration. 651 */ 652 bool migration_has_all_channels(void) 653 { 654 MigrationIncomingState *mis = migration_incoming_get_current(); 655 bool all_channels; 656 657 all_channels = multifd_recv_all_channels_created(); 658 659 return all_channels && mis->from_src_file != NULL; 660 } 661 662 /* 663 * Send a 'SHUT' message on the return channel with the given value 664 * to indicate that we've finished with the RP. Non-0 value indicates 665 * error. 666 */ 667 void migrate_send_rp_shut(MigrationIncomingState *mis, 668 uint32_t value) 669 { 670 uint32_t buf; 671 672 buf = cpu_to_be32(value); 673 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); 674 } 675 676 /* 677 * Send a 'PONG' message on the return channel with the given value 678 * (normally in response to a 'PING') 679 */ 680 void migrate_send_rp_pong(MigrationIncomingState *mis, 681 uint32_t value) 682 { 683 uint32_t buf; 684 685 buf = cpu_to_be32(value); 686 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); 687 } 688 689 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis, 690 char *block_name) 691 { 692 char buf[512]; 693 int len; 694 int64_t res; 695 696 /* 697 * First, we send the header part. It contains only the len of 698 * idstr, and the idstr itself. 699 */ 700 len = strlen(block_name); 701 buf[0] = len; 702 memcpy(buf + 1, block_name, len); 703 704 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { 705 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery", 706 __func__); 707 return; 708 } 709 710 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf); 711 712 /* 713 * Next, we dump the received bitmap to the stream. 714 * 715 * TODO: currently we are safe since we are the only one that is 716 * using the to_src_file handle (fault thread is still paused), 717 * and it's ok even not taking the mutex. However the best way is 718 * to take the lock before sending the message header, and release 719 * the lock after sending the bitmap. 720 */ 721 qemu_mutex_lock(&mis->rp_mutex); 722 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name); 723 qemu_mutex_unlock(&mis->rp_mutex); 724 725 trace_migrate_send_rp_recv_bitmap(block_name, res); 726 } 727 728 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value) 729 { 730 uint32_t buf; 731 732 buf = cpu_to_be32(value); 733 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf); 734 } 735 736 MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) 737 { 738 MigrationCapabilityStatusList *head = NULL; 739 MigrationCapabilityStatusList *caps; 740 MigrationState *s = migrate_get_current(); 741 int i; 742 743 caps = NULL; /* silence compiler warning */ 744 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 745 #ifndef CONFIG_LIVE_BLOCK_MIGRATION 746 if (i == MIGRATION_CAPABILITY_BLOCK) { 747 continue; 748 } 749 #endif 750 if (head == NULL) { 751 head = g_malloc0(sizeof(*caps)); 752 caps = head; 753 } else { 754 caps->next = g_malloc0(sizeof(*caps)); 755 caps = caps->next; 756 } 757 caps->value = 758 g_malloc(sizeof(*caps->value)); 759 caps->value->capability = i; 760 caps->value->state = s->enabled_capabilities[i]; 761 } 762 763 return head; 764 } 765 766 MigrationParameters *qmp_query_migrate_parameters(Error **errp) 767 { 768 MigrationParameters *params; 769 MigrationState *s = migrate_get_current(); 770 771 /* TODO use QAPI_CLONE() instead of duplicating it inline */ 772 params = g_malloc0(sizeof(*params)); 773 params->has_compress_level = true; 774 params->compress_level = s->parameters.compress_level; 775 params->has_compress_threads = true; 776 params->compress_threads = s->parameters.compress_threads; 777 params->has_compress_wait_thread = true; 778 params->compress_wait_thread = s->parameters.compress_wait_thread; 779 params->has_decompress_threads = true; 780 params->decompress_threads = s->parameters.decompress_threads; 781 params->has_cpu_throttle_initial = true; 782 params->cpu_throttle_initial = s->parameters.cpu_throttle_initial; 783 params->has_cpu_throttle_increment = true; 784 params->cpu_throttle_increment = s->parameters.cpu_throttle_increment; 785 params->has_tls_creds = true; 786 params->tls_creds = g_strdup(s->parameters.tls_creds); 787 params->has_tls_hostname = true; 788 params->tls_hostname = g_strdup(s->parameters.tls_hostname); 789 params->has_tls_authz = true; 790 params->tls_authz = g_strdup(s->parameters.tls_authz); 791 params->has_max_bandwidth = true; 792 params->max_bandwidth = s->parameters.max_bandwidth; 793 params->has_downtime_limit = true; 794 params->downtime_limit = s->parameters.downtime_limit; 795 params->has_x_checkpoint_delay = true; 796 params->x_checkpoint_delay = s->parameters.x_checkpoint_delay; 797 params->has_block_incremental = true; 798 params->block_incremental = s->parameters.block_incremental; 799 params->has_multifd_channels = true; 800 params->multifd_channels = s->parameters.multifd_channels; 801 params->has_multifd_compression = true; 802 params->multifd_compression = s->parameters.multifd_compression; 803 params->has_multifd_zlib_level = true; 804 params->multifd_zlib_level = s->parameters.multifd_zlib_level; 805 params->has_multifd_zstd_level = true; 806 params->multifd_zstd_level = s->parameters.multifd_zstd_level; 807 params->has_xbzrle_cache_size = true; 808 params->xbzrle_cache_size = s->parameters.xbzrle_cache_size; 809 params->has_max_postcopy_bandwidth = true; 810 params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth; 811 params->has_max_cpu_throttle = true; 812 params->max_cpu_throttle = s->parameters.max_cpu_throttle; 813 params->has_announce_initial = true; 814 params->announce_initial = s->parameters.announce_initial; 815 params->has_announce_max = true; 816 params->announce_max = s->parameters.announce_max; 817 params->has_announce_rounds = true; 818 params->announce_rounds = s->parameters.announce_rounds; 819 params->has_announce_step = true; 820 params->announce_step = s->parameters.announce_step; 821 822 return params; 823 } 824 825 AnnounceParameters *migrate_announce_params(void) 826 { 827 static AnnounceParameters ap; 828 829 MigrationState *s = migrate_get_current(); 830 831 ap.initial = s->parameters.announce_initial; 832 ap.max = s->parameters.announce_max; 833 ap.rounds = s->parameters.announce_rounds; 834 ap.step = s->parameters.announce_step; 835 836 return ≈ 837 } 838 839 /* 840 * Return true if we're already in the middle of a migration 841 * (i.e. any of the active or setup states) 842 */ 843 bool migration_is_setup_or_active(int state) 844 { 845 switch (state) { 846 case MIGRATION_STATUS_ACTIVE: 847 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 848 case MIGRATION_STATUS_POSTCOPY_PAUSED: 849 case MIGRATION_STATUS_POSTCOPY_RECOVER: 850 case MIGRATION_STATUS_SETUP: 851 case MIGRATION_STATUS_PRE_SWITCHOVER: 852 case MIGRATION_STATUS_DEVICE: 853 case MIGRATION_STATUS_WAIT_UNPLUG: 854 return true; 855 856 default: 857 return false; 858 859 } 860 } 861 862 bool migration_is_running(int state) 863 { 864 switch (state) { 865 case MIGRATION_STATUS_ACTIVE: 866 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 867 case MIGRATION_STATUS_POSTCOPY_PAUSED: 868 case MIGRATION_STATUS_POSTCOPY_RECOVER: 869 case MIGRATION_STATUS_SETUP: 870 case MIGRATION_STATUS_PRE_SWITCHOVER: 871 case MIGRATION_STATUS_DEVICE: 872 case MIGRATION_STATUS_WAIT_UNPLUG: 873 case MIGRATION_STATUS_CANCELLING: 874 return true; 875 876 default: 877 return false; 878 879 } 880 } 881 882 static void populate_time_info(MigrationInfo *info, MigrationState *s) 883 { 884 info->has_status = true; 885 info->has_setup_time = true; 886 info->setup_time = s->setup_time; 887 if (s->state == MIGRATION_STATUS_COMPLETED) { 888 info->has_total_time = true; 889 info->total_time = s->total_time; 890 info->has_downtime = true; 891 info->downtime = s->downtime; 892 } else { 893 info->has_total_time = true; 894 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - 895 s->start_time; 896 info->has_expected_downtime = true; 897 info->expected_downtime = s->expected_downtime; 898 } 899 } 900 901 static void populate_ram_info(MigrationInfo *info, MigrationState *s) 902 { 903 info->has_ram = true; 904 info->ram = g_malloc0(sizeof(*info->ram)); 905 info->ram->transferred = ram_counters.transferred; 906 info->ram->total = ram_bytes_total(); 907 info->ram->duplicate = ram_counters.duplicate; 908 /* legacy value. It is not used anymore */ 909 info->ram->skipped = 0; 910 info->ram->normal = ram_counters.normal; 911 info->ram->normal_bytes = ram_counters.normal * 912 qemu_target_page_size(); 913 info->ram->mbps = s->mbps; 914 info->ram->dirty_sync_count = ram_counters.dirty_sync_count; 915 info->ram->postcopy_requests = ram_counters.postcopy_requests; 916 info->ram->page_size = qemu_target_page_size(); 917 info->ram->multifd_bytes = ram_counters.multifd_bytes; 918 info->ram->pages_per_second = s->pages_per_second; 919 920 if (migrate_use_xbzrle()) { 921 info->has_xbzrle_cache = true; 922 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); 923 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); 924 info->xbzrle_cache->bytes = xbzrle_counters.bytes; 925 info->xbzrle_cache->pages = xbzrle_counters.pages; 926 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss; 927 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate; 928 info->xbzrle_cache->overflow = xbzrle_counters.overflow; 929 } 930 931 if (migrate_use_compression()) { 932 info->has_compression = true; 933 info->compression = g_malloc0(sizeof(*info->compression)); 934 info->compression->pages = compression_counters.pages; 935 info->compression->busy = compression_counters.busy; 936 info->compression->busy_rate = compression_counters.busy_rate; 937 info->compression->compressed_size = 938 compression_counters.compressed_size; 939 info->compression->compression_rate = 940 compression_counters.compression_rate; 941 } 942 943 if (cpu_throttle_active()) { 944 info->has_cpu_throttle_percentage = true; 945 info->cpu_throttle_percentage = cpu_throttle_get_percentage(); 946 } 947 948 if (s->state != MIGRATION_STATUS_COMPLETED) { 949 info->ram->remaining = ram_bytes_remaining(); 950 info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate; 951 } 952 } 953 954 static void populate_disk_info(MigrationInfo *info) 955 { 956 if (blk_mig_active()) { 957 info->has_disk = true; 958 info->disk = g_malloc0(sizeof(*info->disk)); 959 info->disk->transferred = blk_mig_bytes_transferred(); 960 info->disk->remaining = blk_mig_bytes_remaining(); 961 info->disk->total = blk_mig_bytes_total(); 962 } 963 } 964 965 static void fill_source_migration_info(MigrationInfo *info) 966 { 967 MigrationState *s = migrate_get_current(); 968 969 switch (s->state) { 970 case MIGRATION_STATUS_NONE: 971 /* no migration has happened ever */ 972 /* do not overwrite destination migration status */ 973 return; 974 break; 975 case MIGRATION_STATUS_SETUP: 976 info->has_status = true; 977 info->has_total_time = false; 978 break; 979 case MIGRATION_STATUS_ACTIVE: 980 case MIGRATION_STATUS_CANCELLING: 981 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 982 case MIGRATION_STATUS_PRE_SWITCHOVER: 983 case MIGRATION_STATUS_DEVICE: 984 case MIGRATION_STATUS_POSTCOPY_PAUSED: 985 case MIGRATION_STATUS_POSTCOPY_RECOVER: 986 /* TODO add some postcopy stats */ 987 populate_time_info(info, s); 988 populate_ram_info(info, s); 989 populate_disk_info(info); 990 break; 991 case MIGRATION_STATUS_COLO: 992 info->has_status = true; 993 /* TODO: display COLO specific information (checkpoint info etc.) */ 994 break; 995 case MIGRATION_STATUS_COMPLETED: 996 populate_time_info(info, s); 997 populate_ram_info(info, s); 998 break; 999 case MIGRATION_STATUS_FAILED: 1000 info->has_status = true; 1001 if (s->error) { 1002 info->has_error_desc = true; 1003 info->error_desc = g_strdup(error_get_pretty(s->error)); 1004 } 1005 break; 1006 case MIGRATION_STATUS_CANCELLED: 1007 info->has_status = true; 1008 break; 1009 case MIGRATION_STATUS_WAIT_UNPLUG: 1010 info->has_status = true; 1011 break; 1012 } 1013 info->status = s->state; 1014 } 1015 1016 /** 1017 * @migration_caps_check - check capability validity 1018 * 1019 * @cap_list: old capability list, array of bool 1020 * @params: new capabilities to be applied soon 1021 * @errp: set *errp if the check failed, with reason 1022 * 1023 * Returns true if check passed, otherwise false. 1024 */ 1025 static bool migrate_caps_check(bool *cap_list, 1026 MigrationCapabilityStatusList *params, 1027 Error **errp) 1028 { 1029 MigrationCapabilityStatusList *cap; 1030 bool old_postcopy_cap; 1031 MigrationIncomingState *mis = migration_incoming_get_current(); 1032 1033 old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]; 1034 1035 for (cap = params; cap; cap = cap->next) { 1036 cap_list[cap->value->capability] = cap->value->state; 1037 } 1038 1039 #ifndef CONFIG_LIVE_BLOCK_MIGRATION 1040 if (cap_list[MIGRATION_CAPABILITY_BLOCK]) { 1041 error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) " 1042 "block migration"); 1043 error_append_hint(errp, "Use drive_mirror+NBD instead.\n"); 1044 return false; 1045 } 1046 #endif 1047 1048 #ifndef CONFIG_REPLICATION 1049 if (cap_list[MIGRATION_CAPABILITY_X_COLO]) { 1050 error_setg(errp, "QEMU compiled without replication module" 1051 " can't enable COLO"); 1052 error_append_hint(errp, "Please enable replication before COLO.\n"); 1053 return false; 1054 } 1055 #endif 1056 1057 if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) { 1058 /* This check is reasonably expensive, so only when it's being 1059 * set the first time, also it's only the destination that needs 1060 * special support. 1061 */ 1062 if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) && 1063 !postcopy_ram_supported_by_host(mis)) { 1064 /* postcopy_ram_supported_by_host will have emitted a more 1065 * detailed message 1066 */ 1067 error_setg(errp, "Postcopy is not supported"); 1068 return false; 1069 } 1070 1071 if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) { 1072 error_setg(errp, "Postcopy is not compatible with ignore-shared"); 1073 return false; 1074 } 1075 } 1076 1077 return true; 1078 } 1079 1080 static void fill_destination_migration_info(MigrationInfo *info) 1081 { 1082 MigrationIncomingState *mis = migration_incoming_get_current(); 1083 1084 if (mis->socket_address_list) { 1085 info->has_socket_address = true; 1086 info->socket_address = 1087 QAPI_CLONE(SocketAddressList, mis->socket_address_list); 1088 } 1089 1090 switch (mis->state) { 1091 case MIGRATION_STATUS_NONE: 1092 return; 1093 break; 1094 case MIGRATION_STATUS_SETUP: 1095 case MIGRATION_STATUS_CANCELLING: 1096 case MIGRATION_STATUS_CANCELLED: 1097 case MIGRATION_STATUS_ACTIVE: 1098 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1099 case MIGRATION_STATUS_POSTCOPY_PAUSED: 1100 case MIGRATION_STATUS_POSTCOPY_RECOVER: 1101 case MIGRATION_STATUS_FAILED: 1102 case MIGRATION_STATUS_COLO: 1103 info->has_status = true; 1104 break; 1105 case MIGRATION_STATUS_COMPLETED: 1106 info->has_status = true; 1107 fill_destination_postcopy_migration_info(info); 1108 break; 1109 } 1110 info->status = mis->state; 1111 } 1112 1113 MigrationInfo *qmp_query_migrate(Error **errp) 1114 { 1115 MigrationInfo *info = g_malloc0(sizeof(*info)); 1116 1117 fill_destination_migration_info(info); 1118 fill_source_migration_info(info); 1119 1120 return info; 1121 } 1122 1123 void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, 1124 Error **errp) 1125 { 1126 MigrationState *s = migrate_get_current(); 1127 MigrationCapabilityStatusList *cap; 1128 bool cap_list[MIGRATION_CAPABILITY__MAX]; 1129 1130 if (migration_is_running(s->state)) { 1131 error_setg(errp, QERR_MIGRATION_ACTIVE); 1132 return; 1133 } 1134 1135 memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list)); 1136 if (!migrate_caps_check(cap_list, params, errp)) { 1137 return; 1138 } 1139 1140 for (cap = params; cap; cap = cap->next) { 1141 s->enabled_capabilities[cap->value->capability] = cap->value->state; 1142 } 1143 } 1144 1145 /* 1146 * Check whether the parameters are valid. Error will be put into errp 1147 * (if provided). Return true if valid, otherwise false. 1148 */ 1149 static bool migrate_params_check(MigrationParameters *params, Error **errp) 1150 { 1151 if (params->has_compress_level && 1152 (params->compress_level > 9)) { 1153 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level", 1154 "is invalid, it should be in the range of 0 to 9"); 1155 return false; 1156 } 1157 1158 if (params->has_compress_threads && (params->compress_threads < 1)) { 1159 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1160 "compress_threads", 1161 "is invalid, it should be in the range of 1 to 255"); 1162 return false; 1163 } 1164 1165 if (params->has_decompress_threads && (params->decompress_threads < 1)) { 1166 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1167 "decompress_threads", 1168 "is invalid, it should be in the range of 1 to 255"); 1169 return false; 1170 } 1171 1172 if (params->has_cpu_throttle_initial && 1173 (params->cpu_throttle_initial < 1 || 1174 params->cpu_throttle_initial > 99)) { 1175 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1176 "cpu_throttle_initial", 1177 "an integer in the range of 1 to 99"); 1178 return false; 1179 } 1180 1181 if (params->has_cpu_throttle_increment && 1182 (params->cpu_throttle_increment < 1 || 1183 params->cpu_throttle_increment > 99)) { 1184 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1185 "cpu_throttle_increment", 1186 "an integer in the range of 1 to 99"); 1187 return false; 1188 } 1189 1190 if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) { 1191 error_setg(errp, "Parameter 'max_bandwidth' expects an integer in the" 1192 " range of 0 to %zu bytes/second", SIZE_MAX); 1193 return false; 1194 } 1195 1196 if (params->has_downtime_limit && 1197 (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) { 1198 error_setg(errp, "Parameter 'downtime_limit' expects an integer in " 1199 "the range of 0 to %d milliseconds", 1200 MAX_MIGRATE_DOWNTIME); 1201 return false; 1202 } 1203 1204 /* x_checkpoint_delay is now always positive */ 1205 1206 if (params->has_multifd_channels && (params->multifd_channels < 1)) { 1207 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1208 "multifd_channels", 1209 "is invalid, it should be in the range of 1 to 255"); 1210 return false; 1211 } 1212 1213 if (params->has_multifd_zlib_level && 1214 (params->multifd_zlib_level > 9)) { 1215 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level", 1216 "is invalid, it should be in the range of 0 to 9"); 1217 return false; 1218 } 1219 1220 if (params->has_multifd_zstd_level && 1221 (params->multifd_zstd_level > 20)) { 1222 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level", 1223 "is invalid, it should be in the range of 0 to 20"); 1224 return false; 1225 } 1226 1227 if (params->has_xbzrle_cache_size && 1228 (params->xbzrle_cache_size < qemu_target_page_size() || 1229 !is_power_of_2(params->xbzrle_cache_size))) { 1230 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1231 "xbzrle_cache_size", 1232 "is invalid, it should be bigger than target page size" 1233 " and a power of two"); 1234 return false; 1235 } 1236 1237 if (params->has_max_cpu_throttle && 1238 (params->max_cpu_throttle < params->cpu_throttle_initial || 1239 params->max_cpu_throttle > 99)) { 1240 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1241 "max_cpu_throttle", 1242 "an integer in the range of cpu_throttle_initial to 99"); 1243 return false; 1244 } 1245 1246 if (params->has_announce_initial && 1247 params->announce_initial > 100000) { 1248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1249 "announce_initial", 1250 "is invalid, it must be less than 100000 ms"); 1251 return false; 1252 } 1253 if (params->has_announce_max && 1254 params->announce_max > 100000) { 1255 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1256 "announce_max", 1257 "is invalid, it must be less than 100000 ms"); 1258 return false; 1259 } 1260 if (params->has_announce_rounds && 1261 params->announce_rounds > 1000) { 1262 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1263 "announce_rounds", 1264 "is invalid, it must be in the range of 0 to 1000"); 1265 return false; 1266 } 1267 if (params->has_announce_step && 1268 (params->announce_step < 1 || 1269 params->announce_step > 10000)) { 1270 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 1271 "announce_step", 1272 "is invalid, it must be in the range of 1 to 10000 ms"); 1273 return false; 1274 } 1275 return true; 1276 } 1277 1278 static void migrate_params_test_apply(MigrateSetParameters *params, 1279 MigrationParameters *dest) 1280 { 1281 *dest = migrate_get_current()->parameters; 1282 1283 /* TODO use QAPI_CLONE() instead of duplicating it inline */ 1284 1285 if (params->has_compress_level) { 1286 dest->compress_level = params->compress_level; 1287 } 1288 1289 if (params->has_compress_threads) { 1290 dest->compress_threads = params->compress_threads; 1291 } 1292 1293 if (params->has_compress_wait_thread) { 1294 dest->compress_wait_thread = params->compress_wait_thread; 1295 } 1296 1297 if (params->has_decompress_threads) { 1298 dest->decompress_threads = params->decompress_threads; 1299 } 1300 1301 if (params->has_cpu_throttle_initial) { 1302 dest->cpu_throttle_initial = params->cpu_throttle_initial; 1303 } 1304 1305 if (params->has_cpu_throttle_increment) { 1306 dest->cpu_throttle_increment = params->cpu_throttle_increment; 1307 } 1308 1309 if (params->has_tls_creds) { 1310 assert(params->tls_creds->type == QTYPE_QSTRING); 1311 dest->tls_creds = g_strdup(params->tls_creds->u.s); 1312 } 1313 1314 if (params->has_tls_hostname) { 1315 assert(params->tls_hostname->type == QTYPE_QSTRING); 1316 dest->tls_hostname = g_strdup(params->tls_hostname->u.s); 1317 } 1318 1319 if (params->has_max_bandwidth) { 1320 dest->max_bandwidth = params->max_bandwidth; 1321 } 1322 1323 if (params->has_downtime_limit) { 1324 dest->downtime_limit = params->downtime_limit; 1325 } 1326 1327 if (params->has_x_checkpoint_delay) { 1328 dest->x_checkpoint_delay = params->x_checkpoint_delay; 1329 } 1330 1331 if (params->has_block_incremental) { 1332 dest->block_incremental = params->block_incremental; 1333 } 1334 if (params->has_multifd_channels) { 1335 dest->multifd_channels = params->multifd_channels; 1336 } 1337 if (params->has_multifd_compression) { 1338 dest->multifd_compression = params->multifd_compression; 1339 } 1340 if (params->has_xbzrle_cache_size) { 1341 dest->xbzrle_cache_size = params->xbzrle_cache_size; 1342 } 1343 if (params->has_max_postcopy_bandwidth) { 1344 dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth; 1345 } 1346 if (params->has_max_cpu_throttle) { 1347 dest->max_cpu_throttle = params->max_cpu_throttle; 1348 } 1349 if (params->has_announce_initial) { 1350 dest->announce_initial = params->announce_initial; 1351 } 1352 if (params->has_announce_max) { 1353 dest->announce_max = params->announce_max; 1354 } 1355 if (params->has_announce_rounds) { 1356 dest->announce_rounds = params->announce_rounds; 1357 } 1358 if (params->has_announce_step) { 1359 dest->announce_step = params->announce_step; 1360 } 1361 } 1362 1363 static void migrate_params_apply(MigrateSetParameters *params, Error **errp) 1364 { 1365 MigrationState *s = migrate_get_current(); 1366 1367 /* TODO use QAPI_CLONE() instead of duplicating it inline */ 1368 1369 if (params->has_compress_level) { 1370 s->parameters.compress_level = params->compress_level; 1371 } 1372 1373 if (params->has_compress_threads) { 1374 s->parameters.compress_threads = params->compress_threads; 1375 } 1376 1377 if (params->has_compress_wait_thread) { 1378 s->parameters.compress_wait_thread = params->compress_wait_thread; 1379 } 1380 1381 if (params->has_decompress_threads) { 1382 s->parameters.decompress_threads = params->decompress_threads; 1383 } 1384 1385 if (params->has_cpu_throttle_initial) { 1386 s->parameters.cpu_throttle_initial = params->cpu_throttle_initial; 1387 } 1388 1389 if (params->has_cpu_throttle_increment) { 1390 s->parameters.cpu_throttle_increment = params->cpu_throttle_increment; 1391 } 1392 1393 if (params->has_tls_creds) { 1394 g_free(s->parameters.tls_creds); 1395 assert(params->tls_creds->type == QTYPE_QSTRING); 1396 s->parameters.tls_creds = g_strdup(params->tls_creds->u.s); 1397 } 1398 1399 if (params->has_tls_hostname) { 1400 g_free(s->parameters.tls_hostname); 1401 assert(params->tls_hostname->type == QTYPE_QSTRING); 1402 s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s); 1403 } 1404 1405 if (params->has_tls_authz) { 1406 g_free(s->parameters.tls_authz); 1407 assert(params->tls_authz->type == QTYPE_QSTRING); 1408 s->parameters.tls_authz = g_strdup(params->tls_authz->u.s); 1409 } 1410 1411 if (params->has_max_bandwidth) { 1412 s->parameters.max_bandwidth = params->max_bandwidth; 1413 if (s->to_dst_file && !migration_in_postcopy()) { 1414 qemu_file_set_rate_limit(s->to_dst_file, 1415 s->parameters.max_bandwidth / XFER_LIMIT_RATIO); 1416 } 1417 } 1418 1419 if (params->has_downtime_limit) { 1420 s->parameters.downtime_limit = params->downtime_limit; 1421 } 1422 1423 if (params->has_x_checkpoint_delay) { 1424 s->parameters.x_checkpoint_delay = params->x_checkpoint_delay; 1425 if (migration_in_colo_state()) { 1426 colo_checkpoint_notify(s); 1427 } 1428 } 1429 1430 if (params->has_block_incremental) { 1431 s->parameters.block_incremental = params->block_incremental; 1432 } 1433 if (params->has_multifd_channels) { 1434 s->parameters.multifd_channels = params->multifd_channels; 1435 } 1436 if (params->has_multifd_compression) { 1437 s->parameters.multifd_compression = params->multifd_compression; 1438 } 1439 if (params->has_xbzrle_cache_size) { 1440 s->parameters.xbzrle_cache_size = params->xbzrle_cache_size; 1441 xbzrle_cache_resize(params->xbzrle_cache_size, errp); 1442 } 1443 if (params->has_max_postcopy_bandwidth) { 1444 s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth; 1445 if (s->to_dst_file && migration_in_postcopy()) { 1446 qemu_file_set_rate_limit(s->to_dst_file, 1447 s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO); 1448 } 1449 } 1450 if (params->has_max_cpu_throttle) { 1451 s->parameters.max_cpu_throttle = params->max_cpu_throttle; 1452 } 1453 if (params->has_announce_initial) { 1454 s->parameters.announce_initial = params->announce_initial; 1455 } 1456 if (params->has_announce_max) { 1457 s->parameters.announce_max = params->announce_max; 1458 } 1459 if (params->has_announce_rounds) { 1460 s->parameters.announce_rounds = params->announce_rounds; 1461 } 1462 if (params->has_announce_step) { 1463 s->parameters.announce_step = params->announce_step; 1464 } 1465 } 1466 1467 void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp) 1468 { 1469 MigrationParameters tmp; 1470 1471 /* TODO Rewrite "" to null instead */ 1472 if (params->has_tls_creds 1473 && params->tls_creds->type == QTYPE_QNULL) { 1474 qobject_unref(params->tls_creds->u.n); 1475 params->tls_creds->type = QTYPE_QSTRING; 1476 params->tls_creds->u.s = strdup(""); 1477 } 1478 /* TODO Rewrite "" to null instead */ 1479 if (params->has_tls_hostname 1480 && params->tls_hostname->type == QTYPE_QNULL) { 1481 qobject_unref(params->tls_hostname->u.n); 1482 params->tls_hostname->type = QTYPE_QSTRING; 1483 params->tls_hostname->u.s = strdup(""); 1484 } 1485 1486 migrate_params_test_apply(params, &tmp); 1487 1488 if (!migrate_params_check(&tmp, errp)) { 1489 /* Invalid parameter */ 1490 return; 1491 } 1492 1493 migrate_params_apply(params, errp); 1494 } 1495 1496 1497 void qmp_migrate_start_postcopy(Error **errp) 1498 { 1499 MigrationState *s = migrate_get_current(); 1500 1501 if (!migrate_postcopy()) { 1502 error_setg(errp, "Enable postcopy with migrate_set_capability before" 1503 " the start of migration"); 1504 return; 1505 } 1506 1507 if (s->state == MIGRATION_STATUS_NONE) { 1508 error_setg(errp, "Postcopy must be started after migration has been" 1509 " started"); 1510 return; 1511 } 1512 /* 1513 * we don't error if migration has finished since that would be racy 1514 * with issuing this command. 1515 */ 1516 atomic_set(&s->start_postcopy, true); 1517 } 1518 1519 /* shared migration helpers */ 1520 1521 void migrate_set_state(int *state, int old_state, int new_state) 1522 { 1523 assert(new_state < MIGRATION_STATUS__MAX); 1524 if (atomic_cmpxchg(state, old_state, new_state) == old_state) { 1525 trace_migrate_set_state(MigrationStatus_str(new_state)); 1526 migrate_generate_event(new_state); 1527 } 1528 } 1529 1530 static MigrationCapabilityStatusList *migrate_cap_add( 1531 MigrationCapabilityStatusList *list, 1532 MigrationCapability index, 1533 bool state) 1534 { 1535 MigrationCapabilityStatusList *cap; 1536 1537 cap = g_new0(MigrationCapabilityStatusList, 1); 1538 cap->value = g_new0(MigrationCapabilityStatus, 1); 1539 cap->value->capability = index; 1540 cap->value->state = state; 1541 cap->next = list; 1542 1543 return cap; 1544 } 1545 1546 void migrate_set_block_enabled(bool value, Error **errp) 1547 { 1548 MigrationCapabilityStatusList *cap; 1549 1550 cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value); 1551 qmp_migrate_set_capabilities(cap, errp); 1552 qapi_free_MigrationCapabilityStatusList(cap); 1553 } 1554 1555 static void migrate_set_block_incremental(MigrationState *s, bool value) 1556 { 1557 s->parameters.block_incremental = value; 1558 } 1559 1560 static void block_cleanup_parameters(MigrationState *s) 1561 { 1562 if (s->must_remove_block_options) { 1563 /* setting to false can never fail */ 1564 migrate_set_block_enabled(false, &error_abort); 1565 migrate_set_block_incremental(s, false); 1566 s->must_remove_block_options = false; 1567 } 1568 } 1569 1570 static void migrate_fd_cleanup(MigrationState *s) 1571 { 1572 qemu_bh_delete(s->cleanup_bh); 1573 s->cleanup_bh = NULL; 1574 1575 qemu_savevm_state_cleanup(); 1576 1577 if (s->to_dst_file) { 1578 QEMUFile *tmp; 1579 1580 trace_migrate_fd_cleanup(); 1581 qemu_mutex_unlock_iothread(); 1582 if (s->migration_thread_running) { 1583 qemu_thread_join(&s->thread); 1584 s->migration_thread_running = false; 1585 } 1586 qemu_mutex_lock_iothread(); 1587 1588 multifd_save_cleanup(); 1589 qemu_mutex_lock(&s->qemu_file_lock); 1590 tmp = s->to_dst_file; 1591 s->to_dst_file = NULL; 1592 qemu_mutex_unlock(&s->qemu_file_lock); 1593 /* 1594 * Close the file handle without the lock to make sure the 1595 * critical section won't block for long. 1596 */ 1597 qemu_fclose(tmp); 1598 } 1599 1600 assert(!migration_is_active(s)); 1601 1602 if (s->state == MIGRATION_STATUS_CANCELLING) { 1603 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, 1604 MIGRATION_STATUS_CANCELLED); 1605 } 1606 1607 if (s->error) { 1608 /* It is used on info migrate. We can't free it */ 1609 error_report_err(error_copy(s->error)); 1610 } 1611 notifier_list_notify(&migration_state_notifiers, s); 1612 block_cleanup_parameters(s); 1613 } 1614 1615 static void migrate_fd_cleanup_schedule(MigrationState *s) 1616 { 1617 /* 1618 * Ref the state for bh, because it may be called when 1619 * there're already no other refs 1620 */ 1621 object_ref(OBJECT(s)); 1622 qemu_bh_schedule(s->cleanup_bh); 1623 } 1624 1625 static void migrate_fd_cleanup_bh(void *opaque) 1626 { 1627 MigrationState *s = opaque; 1628 migrate_fd_cleanup(s); 1629 object_unref(OBJECT(s)); 1630 } 1631 1632 void migrate_set_error(MigrationState *s, const Error *error) 1633 { 1634 qemu_mutex_lock(&s->error_mutex); 1635 if (!s->error) { 1636 s->error = error_copy(error); 1637 } 1638 qemu_mutex_unlock(&s->error_mutex); 1639 } 1640 1641 void migrate_fd_error(MigrationState *s, const Error *error) 1642 { 1643 trace_migrate_fd_error(error_get_pretty(error)); 1644 assert(s->to_dst_file == NULL); 1645 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 1646 MIGRATION_STATUS_FAILED); 1647 migrate_set_error(s, error); 1648 } 1649 1650 static void migrate_fd_cancel(MigrationState *s) 1651 { 1652 int old_state ; 1653 QEMUFile *f = migrate_get_current()->to_dst_file; 1654 trace_migrate_fd_cancel(); 1655 1656 if (s->rp_state.from_dst_file) { 1657 /* shutdown the rp socket, so causing the rp thread to shutdown */ 1658 qemu_file_shutdown(s->rp_state.from_dst_file); 1659 } 1660 1661 do { 1662 old_state = s->state; 1663 if (!migration_is_running(old_state)) { 1664 break; 1665 } 1666 /* If the migration is paused, kick it out of the pause */ 1667 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) { 1668 qemu_sem_post(&s->pause_sem); 1669 } 1670 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING); 1671 } while (s->state != MIGRATION_STATUS_CANCELLING); 1672 1673 /* 1674 * If we're unlucky the migration code might be stuck somewhere in a 1675 * send/write while the network has failed and is waiting to timeout; 1676 * if we've got shutdown(2) available then we can force it to quit. 1677 * The outgoing qemu file gets closed in migrate_fd_cleanup that is 1678 * called in a bh, so there is no race against this cancel. 1679 */ 1680 if (s->state == MIGRATION_STATUS_CANCELLING && f) { 1681 qemu_file_shutdown(f); 1682 } 1683 if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) { 1684 Error *local_err = NULL; 1685 1686 bdrv_invalidate_cache_all(&local_err); 1687 if (local_err) { 1688 error_report_err(local_err); 1689 } else { 1690 s->block_inactive = false; 1691 } 1692 } 1693 } 1694 1695 void add_migration_state_change_notifier(Notifier *notify) 1696 { 1697 notifier_list_add(&migration_state_notifiers, notify); 1698 } 1699 1700 void remove_migration_state_change_notifier(Notifier *notify) 1701 { 1702 notifier_remove(notify); 1703 } 1704 1705 bool migration_in_setup(MigrationState *s) 1706 { 1707 return s->state == MIGRATION_STATUS_SETUP; 1708 } 1709 1710 bool migration_has_finished(MigrationState *s) 1711 { 1712 return s->state == MIGRATION_STATUS_COMPLETED; 1713 } 1714 1715 bool migration_has_failed(MigrationState *s) 1716 { 1717 return (s->state == MIGRATION_STATUS_CANCELLED || 1718 s->state == MIGRATION_STATUS_FAILED); 1719 } 1720 1721 bool migration_in_postcopy(void) 1722 { 1723 MigrationState *s = migrate_get_current(); 1724 1725 switch (s->state) { 1726 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1727 case MIGRATION_STATUS_POSTCOPY_PAUSED: 1728 case MIGRATION_STATUS_POSTCOPY_RECOVER: 1729 return true; 1730 default: 1731 return false; 1732 } 1733 } 1734 1735 bool migration_in_postcopy_after_devices(MigrationState *s) 1736 { 1737 return migration_in_postcopy() && s->postcopy_after_devices; 1738 } 1739 1740 bool migration_is_idle(void) 1741 { 1742 MigrationState *s = current_migration; 1743 1744 if (!s) { 1745 return true; 1746 } 1747 1748 switch (s->state) { 1749 case MIGRATION_STATUS_NONE: 1750 case MIGRATION_STATUS_CANCELLED: 1751 case MIGRATION_STATUS_COMPLETED: 1752 case MIGRATION_STATUS_FAILED: 1753 return true; 1754 case MIGRATION_STATUS_SETUP: 1755 case MIGRATION_STATUS_CANCELLING: 1756 case MIGRATION_STATUS_ACTIVE: 1757 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1758 case MIGRATION_STATUS_COLO: 1759 case MIGRATION_STATUS_PRE_SWITCHOVER: 1760 case MIGRATION_STATUS_DEVICE: 1761 case MIGRATION_STATUS_WAIT_UNPLUG: 1762 return false; 1763 case MIGRATION_STATUS__MAX: 1764 g_assert_not_reached(); 1765 } 1766 1767 return false; 1768 } 1769 1770 bool migration_is_active(MigrationState *s) 1771 { 1772 return (s->state == MIGRATION_STATUS_ACTIVE || 1773 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); 1774 } 1775 1776 void migrate_init(MigrationState *s) 1777 { 1778 /* 1779 * Reinitialise all migration state, except 1780 * parameters/capabilities that the user set, and 1781 * locks. 1782 */ 1783 s->cleanup_bh = 0; 1784 s->to_dst_file = NULL; 1785 s->state = MIGRATION_STATUS_NONE; 1786 s->rp_state.from_dst_file = NULL; 1787 s->rp_state.error = false; 1788 s->mbps = 0.0; 1789 s->pages_per_second = 0.0; 1790 s->downtime = 0; 1791 s->expected_downtime = 0; 1792 s->setup_time = 0; 1793 s->start_postcopy = false; 1794 s->postcopy_after_devices = false; 1795 s->migration_thread_running = false; 1796 error_free(s->error); 1797 s->error = NULL; 1798 1799 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); 1800 1801 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 1802 s->total_time = 0; 1803 s->vm_was_running = false; 1804 s->iteration_initial_bytes = 0; 1805 s->threshold_size = 0; 1806 } 1807 1808 static GSList *migration_blockers; 1809 1810 int migrate_add_blocker(Error *reason, Error **errp) 1811 { 1812 if (only_migratable) { 1813 error_propagate_prepend(errp, error_copy(reason), 1814 "disallowing migration blocker " 1815 "(--only-migratable) for: "); 1816 return -EACCES; 1817 } 1818 1819 if (migration_is_idle()) { 1820 migration_blockers = g_slist_prepend(migration_blockers, reason); 1821 return 0; 1822 } 1823 1824 error_propagate_prepend(errp, error_copy(reason), 1825 "disallowing migration blocker " 1826 "(migration in progress) for: "); 1827 return -EBUSY; 1828 } 1829 1830 void migrate_del_blocker(Error *reason) 1831 { 1832 migration_blockers = g_slist_remove(migration_blockers, reason); 1833 } 1834 1835 void qmp_migrate_incoming(const char *uri, Error **errp) 1836 { 1837 Error *local_err = NULL; 1838 static bool once = true; 1839 1840 if (!deferred_incoming) { 1841 error_setg(errp, "For use with '-incoming defer'"); 1842 return; 1843 } 1844 if (!once) { 1845 error_setg(errp, "The incoming migration has already been started"); 1846 return; 1847 } 1848 1849 qemu_start_incoming_migration(uri, &local_err); 1850 1851 if (local_err) { 1852 error_propagate(errp, local_err); 1853 return; 1854 } 1855 1856 once = false; 1857 } 1858 1859 void qmp_migrate_recover(const char *uri, Error **errp) 1860 { 1861 MigrationIncomingState *mis = migration_incoming_get_current(); 1862 1863 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { 1864 error_setg(errp, "Migrate recover can only be run " 1865 "when postcopy is paused."); 1866 return; 1867 } 1868 1869 if (atomic_cmpxchg(&mis->postcopy_recover_triggered, 1870 false, true) == true) { 1871 error_setg(errp, "Migrate recovery is triggered already"); 1872 return; 1873 } 1874 1875 /* 1876 * Note that this call will never start a real migration; it will 1877 * only re-setup the migration stream and poke existing migration 1878 * to continue using that newly established channel. 1879 */ 1880 qemu_start_incoming_migration(uri, errp); 1881 } 1882 1883 void qmp_migrate_pause(Error **errp) 1884 { 1885 MigrationState *ms = migrate_get_current(); 1886 MigrationIncomingState *mis = migration_incoming_get_current(); 1887 int ret; 1888 1889 if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 1890 /* Source side, during postcopy */ 1891 qemu_mutex_lock(&ms->qemu_file_lock); 1892 ret = qemu_file_shutdown(ms->to_dst_file); 1893 qemu_mutex_unlock(&ms->qemu_file_lock); 1894 if (ret) { 1895 error_setg(errp, "Failed to pause source migration"); 1896 } 1897 return; 1898 } 1899 1900 if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 1901 ret = qemu_file_shutdown(mis->from_src_file); 1902 if (ret) { 1903 error_setg(errp, "Failed to pause destination migration"); 1904 } 1905 return; 1906 } 1907 1908 error_setg(errp, "migrate-pause is currently only supported " 1909 "during postcopy-active state"); 1910 } 1911 1912 bool migration_is_blocked(Error **errp) 1913 { 1914 if (qemu_savevm_state_blocked(errp)) { 1915 return true; 1916 } 1917 1918 if (migration_blockers) { 1919 error_propagate(errp, error_copy(migration_blockers->data)); 1920 return true; 1921 } 1922 1923 return false; 1924 } 1925 1926 /* Returns true if continue to migrate, or false if error detected */ 1927 static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc, 1928 bool resume, Error **errp) 1929 { 1930 Error *local_err = NULL; 1931 1932 if (resume) { 1933 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { 1934 error_setg(errp, "Cannot resume if there is no " 1935 "paused migration"); 1936 return false; 1937 } 1938 1939 /* 1940 * Postcopy recovery won't work well with release-ram 1941 * capability since release-ram will drop the page buffer as 1942 * long as the page is put into the send buffer. So if there 1943 * is a network failure happened, any page buffers that have 1944 * not yet reached the destination VM but have already been 1945 * sent from the source VM will be lost forever. Let's refuse 1946 * the client from resuming such a postcopy migration. 1947 * Luckily release-ram was designed to only be used when src 1948 * and destination VMs are on the same host, so it should be 1949 * fine. 1950 */ 1951 if (migrate_release_ram()) { 1952 error_setg(errp, "Postcopy recovery cannot work " 1953 "when release-ram capability is set"); 1954 return false; 1955 } 1956 1957 /* This is a resume, skip init status */ 1958 return true; 1959 } 1960 1961 if (migration_is_running(s->state)) { 1962 error_setg(errp, QERR_MIGRATION_ACTIVE); 1963 return false; 1964 } 1965 1966 if (runstate_check(RUN_STATE_INMIGRATE)) { 1967 error_setg(errp, "Guest is waiting for an incoming migration"); 1968 return false; 1969 } 1970 1971 if (migration_is_blocked(errp)) { 1972 return false; 1973 } 1974 1975 if (blk || blk_inc) { 1976 if (migrate_use_block() || migrate_use_block_incremental()) { 1977 error_setg(errp, "Command options are incompatible with " 1978 "current migration capabilities"); 1979 return false; 1980 } 1981 migrate_set_block_enabled(true, &local_err); 1982 if (local_err) { 1983 error_propagate(errp, local_err); 1984 return false; 1985 } 1986 s->must_remove_block_options = true; 1987 } 1988 1989 if (blk_inc) { 1990 migrate_set_block_incremental(s, true); 1991 } 1992 1993 migrate_init(s); 1994 /* 1995 * set ram_counters memory to zero for a 1996 * new migration 1997 */ 1998 memset(&ram_counters, 0, sizeof(ram_counters)); 1999 2000 return true; 2001 } 2002 2003 void qmp_migrate(const char *uri, bool has_blk, bool blk, 2004 bool has_inc, bool inc, bool has_detach, bool detach, 2005 bool has_resume, bool resume, Error **errp) 2006 { 2007 Error *local_err = NULL; 2008 MigrationState *s = migrate_get_current(); 2009 const char *p; 2010 2011 if (!migrate_prepare(s, has_blk && blk, has_inc && inc, 2012 has_resume && resume, errp)) { 2013 /* Error detected, put into errp */ 2014 return; 2015 } 2016 2017 if (strstart(uri, "tcp:", &p)) { 2018 tcp_start_outgoing_migration(s, p, &local_err); 2019 #ifdef CONFIG_RDMA 2020 } else if (strstart(uri, "rdma:", &p)) { 2021 rdma_start_outgoing_migration(s, p, &local_err); 2022 #endif 2023 } else if (strstart(uri, "exec:", &p)) { 2024 exec_start_outgoing_migration(s, p, &local_err); 2025 } else if (strstart(uri, "unix:", &p)) { 2026 unix_start_outgoing_migration(s, p, &local_err); 2027 } else if (strstart(uri, "fd:", &p)) { 2028 fd_start_outgoing_migration(s, p, &local_err); 2029 } else { 2030 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri", 2031 "a valid migration protocol"); 2032 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 2033 MIGRATION_STATUS_FAILED); 2034 block_cleanup_parameters(s); 2035 return; 2036 } 2037 2038 if (local_err) { 2039 migrate_fd_error(s, local_err); 2040 error_propagate(errp, local_err); 2041 return; 2042 } 2043 } 2044 2045 void qmp_migrate_cancel(Error **errp) 2046 { 2047 migrate_fd_cancel(migrate_get_current()); 2048 } 2049 2050 void qmp_migrate_continue(MigrationStatus state, Error **errp) 2051 { 2052 MigrationState *s = migrate_get_current(); 2053 if (s->state != state) { 2054 error_setg(errp, "Migration not in expected state: %s", 2055 MigrationStatus_str(s->state)); 2056 return; 2057 } 2058 qemu_sem_post(&s->pause_sem); 2059 } 2060 2061 void qmp_migrate_set_cache_size(int64_t value, Error **errp) 2062 { 2063 MigrateSetParameters p = { 2064 .has_xbzrle_cache_size = true, 2065 .xbzrle_cache_size = value, 2066 }; 2067 2068 qmp_migrate_set_parameters(&p, errp); 2069 } 2070 2071 int64_t qmp_query_migrate_cache_size(Error **errp) 2072 { 2073 return migrate_xbzrle_cache_size(); 2074 } 2075 2076 void qmp_migrate_set_speed(int64_t value, Error **errp) 2077 { 2078 MigrateSetParameters p = { 2079 .has_max_bandwidth = true, 2080 .max_bandwidth = value, 2081 }; 2082 2083 qmp_migrate_set_parameters(&p, errp); 2084 } 2085 2086 void qmp_migrate_set_downtime(double value, Error **errp) 2087 { 2088 if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) { 2089 error_setg(errp, "Parameter 'downtime_limit' expects an integer in " 2090 "the range of 0 to %d seconds", 2091 MAX_MIGRATE_DOWNTIME_SECONDS); 2092 return; 2093 } 2094 2095 value *= 1000; /* Convert to milliseconds */ 2096 2097 MigrateSetParameters p = { 2098 .has_downtime_limit = true, 2099 .downtime_limit = (int64_t)value, 2100 }; 2101 2102 qmp_migrate_set_parameters(&p, errp); 2103 } 2104 2105 bool migrate_release_ram(void) 2106 { 2107 MigrationState *s; 2108 2109 s = migrate_get_current(); 2110 2111 return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM]; 2112 } 2113 2114 bool migrate_postcopy_ram(void) 2115 { 2116 MigrationState *s; 2117 2118 s = migrate_get_current(); 2119 2120 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM]; 2121 } 2122 2123 bool migrate_postcopy(void) 2124 { 2125 return migrate_postcopy_ram() || migrate_dirty_bitmaps(); 2126 } 2127 2128 bool migrate_auto_converge(void) 2129 { 2130 MigrationState *s; 2131 2132 s = migrate_get_current(); 2133 2134 return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; 2135 } 2136 2137 bool migrate_zero_blocks(void) 2138 { 2139 MigrationState *s; 2140 2141 s = migrate_get_current(); 2142 2143 return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; 2144 } 2145 2146 bool migrate_postcopy_blocktime(void) 2147 { 2148 MigrationState *s; 2149 2150 s = migrate_get_current(); 2151 2152 return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME]; 2153 } 2154 2155 bool migrate_use_compression(void) 2156 { 2157 MigrationState *s; 2158 2159 s = migrate_get_current(); 2160 2161 return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS]; 2162 } 2163 2164 int migrate_compress_level(void) 2165 { 2166 MigrationState *s; 2167 2168 s = migrate_get_current(); 2169 2170 return s->parameters.compress_level; 2171 } 2172 2173 int migrate_compress_threads(void) 2174 { 2175 MigrationState *s; 2176 2177 s = migrate_get_current(); 2178 2179 return s->parameters.compress_threads; 2180 } 2181 2182 int migrate_compress_wait_thread(void) 2183 { 2184 MigrationState *s; 2185 2186 s = migrate_get_current(); 2187 2188 return s->parameters.compress_wait_thread; 2189 } 2190 2191 int migrate_decompress_threads(void) 2192 { 2193 MigrationState *s; 2194 2195 s = migrate_get_current(); 2196 2197 return s->parameters.decompress_threads; 2198 } 2199 2200 bool migrate_dirty_bitmaps(void) 2201 { 2202 MigrationState *s; 2203 2204 s = migrate_get_current(); 2205 2206 return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS]; 2207 } 2208 2209 bool migrate_ignore_shared(void) 2210 { 2211 MigrationState *s; 2212 2213 s = migrate_get_current(); 2214 2215 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED]; 2216 } 2217 2218 bool migrate_validate_uuid(void) 2219 { 2220 MigrationState *s; 2221 2222 s = migrate_get_current(); 2223 2224 return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID]; 2225 } 2226 2227 bool migrate_use_events(void) 2228 { 2229 MigrationState *s; 2230 2231 s = migrate_get_current(); 2232 2233 return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS]; 2234 } 2235 2236 bool migrate_use_multifd(void) 2237 { 2238 MigrationState *s; 2239 2240 s = migrate_get_current(); 2241 2242 return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD]; 2243 } 2244 2245 bool migrate_pause_before_switchover(void) 2246 { 2247 MigrationState *s; 2248 2249 s = migrate_get_current(); 2250 2251 return s->enabled_capabilities[ 2252 MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER]; 2253 } 2254 2255 int migrate_multifd_channels(void) 2256 { 2257 MigrationState *s; 2258 2259 s = migrate_get_current(); 2260 2261 return s->parameters.multifd_channels; 2262 } 2263 2264 MultiFDCompression migrate_multifd_compression(void) 2265 { 2266 MigrationState *s; 2267 2268 s = migrate_get_current(); 2269 2270 return s->parameters.multifd_compression; 2271 } 2272 2273 int migrate_multifd_zlib_level(void) 2274 { 2275 MigrationState *s; 2276 2277 s = migrate_get_current(); 2278 2279 return s->parameters.multifd_zlib_level; 2280 } 2281 2282 int migrate_multifd_zstd_level(void) 2283 { 2284 MigrationState *s; 2285 2286 s = migrate_get_current(); 2287 2288 return s->parameters.multifd_zstd_level; 2289 } 2290 2291 int migrate_use_xbzrle(void) 2292 { 2293 MigrationState *s; 2294 2295 s = migrate_get_current(); 2296 2297 return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; 2298 } 2299 2300 int64_t migrate_xbzrle_cache_size(void) 2301 { 2302 MigrationState *s; 2303 2304 s = migrate_get_current(); 2305 2306 return s->parameters.xbzrle_cache_size; 2307 } 2308 2309 static int64_t migrate_max_postcopy_bandwidth(void) 2310 { 2311 MigrationState *s; 2312 2313 s = migrate_get_current(); 2314 2315 return s->parameters.max_postcopy_bandwidth; 2316 } 2317 2318 bool migrate_use_block(void) 2319 { 2320 MigrationState *s; 2321 2322 s = migrate_get_current(); 2323 2324 return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK]; 2325 } 2326 2327 bool migrate_use_return_path(void) 2328 { 2329 MigrationState *s; 2330 2331 s = migrate_get_current(); 2332 2333 return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH]; 2334 } 2335 2336 bool migrate_use_block_incremental(void) 2337 { 2338 MigrationState *s; 2339 2340 s = migrate_get_current(); 2341 2342 return s->parameters.block_incremental; 2343 } 2344 2345 /* migration thread support */ 2346 /* 2347 * Something bad happened to the RP stream, mark an error 2348 * The caller shall print or trace something to indicate why 2349 */ 2350 static void mark_source_rp_bad(MigrationState *s) 2351 { 2352 s->rp_state.error = true; 2353 } 2354 2355 static struct rp_cmd_args { 2356 ssize_t len; /* -1 = variable */ 2357 const char *name; 2358 } rp_cmd_args[] = { 2359 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, 2360 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, 2361 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, 2362 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" }, 2363 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" }, 2364 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, 2365 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" }, 2366 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, 2367 }; 2368 2369 /* 2370 * Process a request for pages received on the return path, 2371 * We're allowed to send more than requested (e.g. to round to our page size) 2372 * and we don't need to send pages that have already been sent. 2373 */ 2374 static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, 2375 ram_addr_t start, size_t len) 2376 { 2377 long our_host_ps = qemu_real_host_page_size; 2378 2379 trace_migrate_handle_rp_req_pages(rbname, start, len); 2380 2381 /* 2382 * Since we currently insist on matching page sizes, just sanity check 2383 * we're being asked for whole host pages. 2384 */ 2385 if (start & (our_host_ps-1) || 2386 (len & (our_host_ps-1))) { 2387 error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT 2388 " len: %zd", __func__, start, len); 2389 mark_source_rp_bad(ms); 2390 return; 2391 } 2392 2393 if (ram_save_queue_pages(rbname, start, len)) { 2394 mark_source_rp_bad(ms); 2395 } 2396 } 2397 2398 /* Return true to retry, false to quit */ 2399 static bool postcopy_pause_return_path_thread(MigrationState *s) 2400 { 2401 trace_postcopy_pause_return_path(); 2402 2403 qemu_sem_wait(&s->postcopy_pause_rp_sem); 2404 2405 trace_postcopy_pause_return_path_continued(); 2406 2407 return true; 2408 } 2409 2410 static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name) 2411 { 2412 RAMBlock *block = qemu_ram_block_by_name(block_name); 2413 2414 if (!block) { 2415 error_report("%s: invalid block name '%s'", __func__, block_name); 2416 return -EINVAL; 2417 } 2418 2419 /* Fetch the received bitmap and refresh the dirty bitmap */ 2420 return ram_dirty_bitmap_reload(s, block); 2421 } 2422 2423 static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value) 2424 { 2425 trace_source_return_path_thread_resume_ack(value); 2426 2427 if (value != MIGRATION_RESUME_ACK_VALUE) { 2428 error_report("%s: illegal resume_ack value %"PRIu32, 2429 __func__, value); 2430 return -1; 2431 } 2432 2433 /* Now both sides are active. */ 2434 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER, 2435 MIGRATION_STATUS_POSTCOPY_ACTIVE); 2436 2437 /* Notify send thread that time to continue send pages */ 2438 qemu_sem_post(&s->rp_state.rp_sem); 2439 2440 return 0; 2441 } 2442 2443 /* 2444 * Handles messages sent on the return path towards the source VM 2445 * 2446 */ 2447 static void *source_return_path_thread(void *opaque) 2448 { 2449 MigrationState *ms = opaque; 2450 QEMUFile *rp = ms->rp_state.from_dst_file; 2451 uint16_t header_len, header_type; 2452 uint8_t buf[512]; 2453 uint32_t tmp32, sibling_error; 2454 ram_addr_t start = 0; /* =0 to silence warning */ 2455 size_t len = 0, expected_len; 2456 int res; 2457 2458 trace_source_return_path_thread_entry(); 2459 rcu_register_thread(); 2460 2461 retry: 2462 while (!ms->rp_state.error && !qemu_file_get_error(rp) && 2463 migration_is_setup_or_active(ms->state)) { 2464 trace_source_return_path_thread_loop_top(); 2465 header_type = qemu_get_be16(rp); 2466 header_len = qemu_get_be16(rp); 2467 2468 if (qemu_file_get_error(rp)) { 2469 mark_source_rp_bad(ms); 2470 goto out; 2471 } 2472 2473 if (header_type >= MIG_RP_MSG_MAX || 2474 header_type == MIG_RP_MSG_INVALID) { 2475 error_report("RP: Received invalid message 0x%04x length 0x%04x", 2476 header_type, header_len); 2477 mark_source_rp_bad(ms); 2478 goto out; 2479 } 2480 2481 if ((rp_cmd_args[header_type].len != -1 && 2482 header_len != rp_cmd_args[header_type].len) || 2483 header_len > sizeof(buf)) { 2484 error_report("RP: Received '%s' message (0x%04x) with" 2485 "incorrect length %d expecting %zu", 2486 rp_cmd_args[header_type].name, header_type, header_len, 2487 (size_t)rp_cmd_args[header_type].len); 2488 mark_source_rp_bad(ms); 2489 goto out; 2490 } 2491 2492 /* We know we've got a valid header by this point */ 2493 res = qemu_get_buffer(rp, buf, header_len); 2494 if (res != header_len) { 2495 error_report("RP: Failed reading data for message 0x%04x" 2496 " read %d expected %d", 2497 header_type, res, header_len); 2498 mark_source_rp_bad(ms); 2499 goto out; 2500 } 2501 2502 /* OK, we have the message and the data */ 2503 switch (header_type) { 2504 case MIG_RP_MSG_SHUT: 2505 sibling_error = ldl_be_p(buf); 2506 trace_source_return_path_thread_shut(sibling_error); 2507 if (sibling_error) { 2508 error_report("RP: Sibling indicated error %d", sibling_error); 2509 mark_source_rp_bad(ms); 2510 } 2511 /* 2512 * We'll let the main thread deal with closing the RP 2513 * we could do a shutdown(2) on it, but we're the only user 2514 * anyway, so there's nothing gained. 2515 */ 2516 goto out; 2517 2518 case MIG_RP_MSG_PONG: 2519 tmp32 = ldl_be_p(buf); 2520 trace_source_return_path_thread_pong(tmp32); 2521 break; 2522 2523 case MIG_RP_MSG_REQ_PAGES: 2524 start = ldq_be_p(buf); 2525 len = ldl_be_p(buf + 8); 2526 migrate_handle_rp_req_pages(ms, NULL, start, len); 2527 break; 2528 2529 case MIG_RP_MSG_REQ_PAGES_ID: 2530 expected_len = 12 + 1; /* header + termination */ 2531 2532 if (header_len >= expected_len) { 2533 start = ldq_be_p(buf); 2534 len = ldl_be_p(buf + 8); 2535 /* Now we expect an idstr */ 2536 tmp32 = buf[12]; /* Length of the following idstr */ 2537 buf[13 + tmp32] = '\0'; 2538 expected_len += tmp32; 2539 } 2540 if (header_len != expected_len) { 2541 error_report("RP: Req_Page_id with length %d expecting %zd", 2542 header_len, expected_len); 2543 mark_source_rp_bad(ms); 2544 goto out; 2545 } 2546 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len); 2547 break; 2548 2549 case MIG_RP_MSG_RECV_BITMAP: 2550 if (header_len < 1) { 2551 error_report("%s: missing block name", __func__); 2552 mark_source_rp_bad(ms); 2553 goto out; 2554 } 2555 /* Format: len (1B) + idstr (<255B). This ends the idstr. */ 2556 buf[buf[0] + 1] = '\0'; 2557 if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) { 2558 mark_source_rp_bad(ms); 2559 goto out; 2560 } 2561 break; 2562 2563 case MIG_RP_MSG_RESUME_ACK: 2564 tmp32 = ldl_be_p(buf); 2565 if (migrate_handle_rp_resume_ack(ms, tmp32)) { 2566 mark_source_rp_bad(ms); 2567 goto out; 2568 } 2569 break; 2570 2571 default: 2572 break; 2573 } 2574 } 2575 2576 out: 2577 res = qemu_file_get_error(rp); 2578 if (res) { 2579 if (res == -EIO && migration_in_postcopy()) { 2580 /* 2581 * Maybe there is something we can do: it looks like a 2582 * network down issue, and we pause for a recovery. 2583 */ 2584 if (postcopy_pause_return_path_thread(ms)) { 2585 /* Reload rp, reset the rest */ 2586 if (rp != ms->rp_state.from_dst_file) { 2587 qemu_fclose(rp); 2588 rp = ms->rp_state.from_dst_file; 2589 } 2590 ms->rp_state.error = false; 2591 goto retry; 2592 } 2593 } 2594 2595 trace_source_return_path_thread_bad_end(); 2596 mark_source_rp_bad(ms); 2597 } 2598 2599 trace_source_return_path_thread_end(); 2600 ms->rp_state.from_dst_file = NULL; 2601 qemu_fclose(rp); 2602 rcu_unregister_thread(); 2603 return NULL; 2604 } 2605 2606 static int open_return_path_on_source(MigrationState *ms, 2607 bool create_thread) 2608 { 2609 2610 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file); 2611 if (!ms->rp_state.from_dst_file) { 2612 return -1; 2613 } 2614 2615 trace_open_return_path_on_source(); 2616 2617 if (!create_thread) { 2618 /* We're done */ 2619 return 0; 2620 } 2621 2622 qemu_thread_create(&ms->rp_state.rp_thread, "return path", 2623 source_return_path_thread, ms, QEMU_THREAD_JOINABLE); 2624 2625 trace_open_return_path_on_source_continue(); 2626 2627 return 0; 2628 } 2629 2630 /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ 2631 static int await_return_path_close_on_source(MigrationState *ms) 2632 { 2633 /* 2634 * If this is a normal exit then the destination will send a SHUT and the 2635 * rp_thread will exit, however if there's an error we need to cause 2636 * it to exit. 2637 */ 2638 if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) { 2639 /* 2640 * shutdown(2), if we have it, will cause it to unblock if it's stuck 2641 * waiting for the destination. 2642 */ 2643 qemu_file_shutdown(ms->rp_state.from_dst_file); 2644 mark_source_rp_bad(ms); 2645 } 2646 trace_await_return_path_close_on_source_joining(); 2647 qemu_thread_join(&ms->rp_state.rp_thread); 2648 trace_await_return_path_close_on_source_close(); 2649 return ms->rp_state.error; 2650 } 2651 2652 /* 2653 * Switch from normal iteration to postcopy 2654 * Returns non-0 on error 2655 */ 2656 static int postcopy_start(MigrationState *ms) 2657 { 2658 int ret; 2659 QIOChannelBuffer *bioc; 2660 QEMUFile *fb; 2661 int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 2662 int64_t bandwidth = migrate_max_postcopy_bandwidth(); 2663 bool restart_block = false; 2664 int cur_state = MIGRATION_STATUS_ACTIVE; 2665 if (!migrate_pause_before_switchover()) { 2666 migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE, 2667 MIGRATION_STATUS_POSTCOPY_ACTIVE); 2668 } 2669 2670 trace_postcopy_start(); 2671 qemu_mutex_lock_iothread(); 2672 trace_postcopy_start_set_run(); 2673 2674 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); 2675 global_state_store(); 2676 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); 2677 if (ret < 0) { 2678 goto fail; 2679 } 2680 2681 ret = migration_maybe_pause(ms, &cur_state, 2682 MIGRATION_STATUS_POSTCOPY_ACTIVE); 2683 if (ret < 0) { 2684 goto fail; 2685 } 2686 2687 ret = bdrv_inactivate_all(); 2688 if (ret < 0) { 2689 goto fail; 2690 } 2691 restart_block = true; 2692 2693 /* 2694 * Cause any non-postcopiable, but iterative devices to 2695 * send out their final data. 2696 */ 2697 qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false); 2698 2699 /* 2700 * in Finish migrate and with the io-lock held everything should 2701 * be quiet, but we've potentially still got dirty pages and we 2702 * need to tell the destination to throw any pages it's already received 2703 * that are dirty 2704 */ 2705 if (migrate_postcopy_ram()) { 2706 if (ram_postcopy_send_discard_bitmap(ms)) { 2707 error_report("postcopy send discard bitmap failed"); 2708 goto fail; 2709 } 2710 } 2711 2712 /* 2713 * send rest of state - note things that are doing postcopy 2714 * will notice we're in POSTCOPY_ACTIVE and not actually 2715 * wrap their state up here 2716 */ 2717 /* 0 max-postcopy-bandwidth means unlimited */ 2718 if (!bandwidth) { 2719 qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX); 2720 } else { 2721 qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO); 2722 } 2723 if (migrate_postcopy_ram()) { 2724 /* Ping just for debugging, helps line traces up */ 2725 qemu_savevm_send_ping(ms->to_dst_file, 2); 2726 } 2727 2728 /* 2729 * While loading the device state we may trigger page transfer 2730 * requests and the fd must be free to process those, and thus 2731 * the destination must read the whole device state off the fd before 2732 * it starts processing it. Unfortunately the ad-hoc migration format 2733 * doesn't allow the destination to know the size to read without fully 2734 * parsing it through each devices load-state code (especially the open 2735 * coded devices that use get/put). 2736 * So we wrap the device state up in a package with a length at the start; 2737 * to do this we use a qemu_buf to hold the whole of the device state. 2738 */ 2739 bioc = qio_channel_buffer_new(4096); 2740 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer"); 2741 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc)); 2742 object_unref(OBJECT(bioc)); 2743 2744 /* 2745 * Make sure the receiver can get incoming pages before we send the rest 2746 * of the state 2747 */ 2748 qemu_savevm_send_postcopy_listen(fb); 2749 2750 qemu_savevm_state_complete_precopy(fb, false, false); 2751 if (migrate_postcopy_ram()) { 2752 qemu_savevm_send_ping(fb, 3); 2753 } 2754 2755 qemu_savevm_send_postcopy_run(fb); 2756 2757 /* <><> end of stuff going into the package */ 2758 2759 /* Last point of recovery; as soon as we send the package the destination 2760 * can open devices and potentially start running. 2761 * Lets just check again we've not got any errors. 2762 */ 2763 ret = qemu_file_get_error(ms->to_dst_file); 2764 if (ret) { 2765 error_report("postcopy_start: Migration stream errored (pre package)"); 2766 goto fail_closefb; 2767 } 2768 2769 restart_block = false; 2770 2771 /* Now send that blob */ 2772 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) { 2773 goto fail_closefb; 2774 } 2775 qemu_fclose(fb); 2776 2777 /* Send a notify to give a chance for anything that needs to happen 2778 * at the transition to postcopy and after the device state; in particular 2779 * spice needs to trigger a transition now 2780 */ 2781 ms->postcopy_after_devices = true; 2782 notifier_list_notify(&migration_state_notifiers, ms); 2783 2784 ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop; 2785 2786 qemu_mutex_unlock_iothread(); 2787 2788 if (migrate_postcopy_ram()) { 2789 /* 2790 * Although this ping is just for debug, it could potentially be 2791 * used for getting a better measurement of downtime at the source. 2792 */ 2793 qemu_savevm_send_ping(ms->to_dst_file, 4); 2794 } 2795 2796 if (migrate_release_ram()) { 2797 ram_postcopy_migrated_memory_release(ms); 2798 } 2799 2800 ret = qemu_file_get_error(ms->to_dst_file); 2801 if (ret) { 2802 error_report("postcopy_start: Migration stream errored"); 2803 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, 2804 MIGRATION_STATUS_FAILED); 2805 } 2806 2807 return ret; 2808 2809 fail_closefb: 2810 qemu_fclose(fb); 2811 fail: 2812 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, 2813 MIGRATION_STATUS_FAILED); 2814 if (restart_block) { 2815 /* A failure happened early enough that we know the destination hasn't 2816 * accessed block devices, so we're safe to recover. 2817 */ 2818 Error *local_err = NULL; 2819 2820 bdrv_invalidate_cache_all(&local_err); 2821 if (local_err) { 2822 error_report_err(local_err); 2823 } 2824 } 2825 qemu_mutex_unlock_iothread(); 2826 return -1; 2827 } 2828 2829 /** 2830 * migration_maybe_pause: Pause if required to by 2831 * migrate_pause_before_switchover called with the iothread locked 2832 * Returns: 0 on success 2833 */ 2834 static int migration_maybe_pause(MigrationState *s, 2835 int *current_active_state, 2836 int new_state) 2837 { 2838 if (!migrate_pause_before_switchover()) { 2839 return 0; 2840 } 2841 2842 /* Since leaving this state is not atomic with posting the semaphore 2843 * it's possible that someone could have issued multiple migrate_continue 2844 * and the semaphore is incorrectly positive at this point; 2845 * the docs say it's undefined to reinit a semaphore that's already 2846 * init'd, so use timedwait to eat up any existing posts. 2847 */ 2848 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) { 2849 /* This block intentionally left blank */ 2850 } 2851 2852 /* 2853 * If the migration is cancelled when it is in the completion phase, 2854 * the migration state is set to MIGRATION_STATUS_CANCELLING. 2855 * So we don't need to wait a semaphore, otherwise we would always 2856 * wait for the 'pause_sem' semaphore. 2857 */ 2858 if (s->state != MIGRATION_STATUS_CANCELLING) { 2859 qemu_mutex_unlock_iothread(); 2860 migrate_set_state(&s->state, *current_active_state, 2861 MIGRATION_STATUS_PRE_SWITCHOVER); 2862 qemu_sem_wait(&s->pause_sem); 2863 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER, 2864 new_state); 2865 *current_active_state = new_state; 2866 qemu_mutex_lock_iothread(); 2867 } 2868 2869 return s->state == new_state ? 0 : -EINVAL; 2870 } 2871 2872 /** 2873 * migration_completion: Used by migration_thread when there's not much left. 2874 * The caller 'breaks' the loop when this returns. 2875 * 2876 * @s: Current migration state 2877 */ 2878 static void migration_completion(MigrationState *s) 2879 { 2880 int ret; 2881 int current_active_state = s->state; 2882 2883 if (s->state == MIGRATION_STATUS_ACTIVE) { 2884 qemu_mutex_lock_iothread(); 2885 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 2886 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); 2887 s->vm_was_running = runstate_is_running(); 2888 ret = global_state_store(); 2889 2890 if (!ret) { 2891 bool inactivate = !migrate_colo_enabled(); 2892 ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); 2893 if (ret >= 0) { 2894 ret = migration_maybe_pause(s, ¤t_active_state, 2895 MIGRATION_STATUS_DEVICE); 2896 } 2897 if (ret >= 0) { 2898 qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX); 2899 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false, 2900 inactivate); 2901 } 2902 if (inactivate && ret >= 0) { 2903 s->block_inactive = true; 2904 } 2905 } 2906 qemu_mutex_unlock_iothread(); 2907 2908 if (ret < 0) { 2909 goto fail; 2910 } 2911 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 2912 trace_migration_completion_postcopy_end(); 2913 2914 qemu_savevm_state_complete_postcopy(s->to_dst_file); 2915 trace_migration_completion_postcopy_end_after_complete(); 2916 } 2917 2918 /* 2919 * If rp was opened we must clean up the thread before 2920 * cleaning everything else up (since if there are no failures 2921 * it will wait for the destination to send it's status in 2922 * a SHUT command). 2923 */ 2924 if (s->rp_state.from_dst_file) { 2925 int rp_error; 2926 trace_migration_return_path_end_before(); 2927 rp_error = await_return_path_close_on_source(s); 2928 trace_migration_return_path_end_after(rp_error); 2929 if (rp_error) { 2930 goto fail_invalidate; 2931 } 2932 } 2933 2934 if (qemu_file_get_error(s->to_dst_file)) { 2935 trace_migration_completion_file_err(); 2936 goto fail_invalidate; 2937 } 2938 2939 if (!migrate_colo_enabled()) { 2940 migrate_set_state(&s->state, current_active_state, 2941 MIGRATION_STATUS_COMPLETED); 2942 } 2943 2944 return; 2945 2946 fail_invalidate: 2947 /* If not doing postcopy, vm_start() will be called: let's regain 2948 * control on images. 2949 */ 2950 if (s->state == MIGRATION_STATUS_ACTIVE || 2951 s->state == MIGRATION_STATUS_DEVICE) { 2952 Error *local_err = NULL; 2953 2954 qemu_mutex_lock_iothread(); 2955 bdrv_invalidate_cache_all(&local_err); 2956 if (local_err) { 2957 error_report_err(local_err); 2958 } else { 2959 s->block_inactive = false; 2960 } 2961 qemu_mutex_unlock_iothread(); 2962 } 2963 2964 fail: 2965 migrate_set_state(&s->state, current_active_state, 2966 MIGRATION_STATUS_FAILED); 2967 } 2968 2969 bool migrate_colo_enabled(void) 2970 { 2971 MigrationState *s = migrate_get_current(); 2972 return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO]; 2973 } 2974 2975 typedef enum MigThrError { 2976 /* No error detected */ 2977 MIG_THR_ERR_NONE = 0, 2978 /* Detected error, but resumed successfully */ 2979 MIG_THR_ERR_RECOVERED = 1, 2980 /* Detected fatal error, need to exit */ 2981 MIG_THR_ERR_FATAL = 2, 2982 } MigThrError; 2983 2984 static int postcopy_resume_handshake(MigrationState *s) 2985 { 2986 qemu_savevm_send_postcopy_resume(s->to_dst_file); 2987 2988 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { 2989 qemu_sem_wait(&s->rp_state.rp_sem); 2990 } 2991 2992 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 2993 return 0; 2994 } 2995 2996 return -1; 2997 } 2998 2999 /* Return zero if success, or <0 for error */ 3000 static int postcopy_do_resume(MigrationState *s) 3001 { 3002 int ret; 3003 3004 /* 3005 * Call all the resume_prepare() hooks, so that modules can be 3006 * ready for the migration resume. 3007 */ 3008 ret = qemu_savevm_state_resume_prepare(s); 3009 if (ret) { 3010 error_report("%s: resume_prepare() failure detected: %d", 3011 __func__, ret); 3012 return ret; 3013 } 3014 3015 /* 3016 * Last handshake with destination on the resume (destination will 3017 * switch to postcopy-active afterwards) 3018 */ 3019 ret = postcopy_resume_handshake(s); 3020 if (ret) { 3021 error_report("%s: handshake failed: %d", __func__, ret); 3022 return ret; 3023 } 3024 3025 return 0; 3026 } 3027 3028 /* 3029 * We don't return until we are in a safe state to continue current 3030 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or 3031 * MIG_THR_ERR_FATAL if unrecovery failure happened. 3032 */ 3033 static MigThrError postcopy_pause(MigrationState *s) 3034 { 3035 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); 3036 3037 while (true) { 3038 QEMUFile *file; 3039 3040 migrate_set_state(&s->state, s->state, 3041 MIGRATION_STATUS_POSTCOPY_PAUSED); 3042 3043 /* Current channel is possibly broken. Release it. */ 3044 assert(s->to_dst_file); 3045 qemu_mutex_lock(&s->qemu_file_lock); 3046 file = s->to_dst_file; 3047 s->to_dst_file = NULL; 3048 qemu_mutex_unlock(&s->qemu_file_lock); 3049 3050 qemu_file_shutdown(file); 3051 qemu_fclose(file); 3052 3053 error_report("Detected IO failure for postcopy. " 3054 "Migration paused."); 3055 3056 /* 3057 * We wait until things fixed up. Then someone will setup the 3058 * status back for us. 3059 */ 3060 while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { 3061 qemu_sem_wait(&s->postcopy_pause_sem); 3062 } 3063 3064 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { 3065 /* Woken up by a recover procedure. Give it a shot */ 3066 3067 /* 3068 * Firstly, let's wake up the return path now, with a new 3069 * return path channel. 3070 */ 3071 qemu_sem_post(&s->postcopy_pause_rp_sem); 3072 3073 /* Do the resume logic */ 3074 if (postcopy_do_resume(s) == 0) { 3075 /* Let's continue! */ 3076 trace_postcopy_pause_continued(); 3077 return MIG_THR_ERR_RECOVERED; 3078 } else { 3079 /* 3080 * Something wrong happened during the recovery, let's 3081 * pause again. Pause is always better than throwing 3082 * data away. 3083 */ 3084 continue; 3085 } 3086 } else { 3087 /* This is not right... Time to quit. */ 3088 return MIG_THR_ERR_FATAL; 3089 } 3090 } 3091 } 3092 3093 static MigThrError migration_detect_error(MigrationState *s) 3094 { 3095 int ret; 3096 int state = s->state; 3097 Error *local_error = NULL; 3098 3099 if (state == MIGRATION_STATUS_CANCELLING || 3100 state == MIGRATION_STATUS_CANCELLED) { 3101 /* End the migration, but don't set the state to failed */ 3102 return MIG_THR_ERR_FATAL; 3103 } 3104 3105 /* Try to detect any file errors */ 3106 ret = qemu_file_get_error_obj(s->to_dst_file, &local_error); 3107 if (!ret) { 3108 /* Everything is fine */ 3109 assert(!local_error); 3110 return MIG_THR_ERR_NONE; 3111 } 3112 3113 if (local_error) { 3114 migrate_set_error(s, local_error); 3115 error_free(local_error); 3116 } 3117 3118 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) { 3119 /* 3120 * For postcopy, we allow the network to be down for a 3121 * while. After that, it can be continued by a 3122 * recovery phase. 3123 */ 3124 return postcopy_pause(s); 3125 } else { 3126 /* 3127 * For precopy (or postcopy with error outside IO), we fail 3128 * with no time. 3129 */ 3130 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED); 3131 trace_migration_thread_file_err(); 3132 3133 /* Time to stop the migration, now. */ 3134 return MIG_THR_ERR_FATAL; 3135 } 3136 } 3137 3138 /* How many bytes have we transferred since the beginning of the migration */ 3139 static uint64_t migration_total_bytes(MigrationState *s) 3140 { 3141 return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes; 3142 } 3143 3144 static void migration_calculate_complete(MigrationState *s) 3145 { 3146 uint64_t bytes = migration_total_bytes(s); 3147 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 3148 int64_t transfer_time; 3149 3150 s->total_time = end_time - s->start_time; 3151 if (!s->downtime) { 3152 /* 3153 * It's still not set, so we are precopy migration. For 3154 * postcopy, downtime is calculated during postcopy_start(). 3155 */ 3156 s->downtime = end_time - s->downtime_start; 3157 } 3158 3159 transfer_time = s->total_time - s->setup_time; 3160 if (transfer_time) { 3161 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000; 3162 } 3163 } 3164 3165 static void update_iteration_initial_status(MigrationState *s) 3166 { 3167 /* 3168 * Update these three fields at the same time to avoid mismatch info lead 3169 * wrong speed calculation. 3170 */ 3171 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 3172 s->iteration_initial_bytes = migration_total_bytes(s); 3173 s->iteration_initial_pages = ram_get_total_transferred_pages(); 3174 } 3175 3176 static void migration_update_counters(MigrationState *s, 3177 int64_t current_time) 3178 { 3179 uint64_t transferred, transferred_pages, time_spent; 3180 uint64_t current_bytes; /* bytes transferred since the beginning */ 3181 double bandwidth; 3182 3183 if (current_time < s->iteration_start_time + BUFFER_DELAY) { 3184 return; 3185 } 3186 3187 current_bytes = migration_total_bytes(s); 3188 transferred = current_bytes - s->iteration_initial_bytes; 3189 time_spent = current_time - s->iteration_start_time; 3190 bandwidth = (double)transferred / time_spent; 3191 s->threshold_size = bandwidth * s->parameters.downtime_limit; 3192 3193 s->mbps = (((double) transferred * 8.0) / 3194 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; 3195 3196 transferred_pages = ram_get_total_transferred_pages() - 3197 s->iteration_initial_pages; 3198 s->pages_per_second = (double) transferred_pages / 3199 (((double) time_spent / 1000.0)); 3200 3201 /* 3202 * if we haven't sent anything, we don't want to 3203 * recalculate. 10000 is a small enough number for our purposes 3204 */ 3205 if (ram_counters.dirty_pages_rate && transferred > 10000) { 3206 s->expected_downtime = ram_counters.remaining / bandwidth; 3207 } 3208 3209 qemu_file_reset_rate_limit(s->to_dst_file); 3210 3211 update_iteration_initial_status(s); 3212 3213 trace_migrate_transferred(transferred, time_spent, 3214 bandwidth, s->threshold_size); 3215 } 3216 3217 /* Migration thread iteration status */ 3218 typedef enum { 3219 MIG_ITERATE_RESUME, /* Resume current iteration */ 3220 MIG_ITERATE_SKIP, /* Skip current iteration */ 3221 MIG_ITERATE_BREAK, /* Break the loop */ 3222 } MigIterateState; 3223 3224 /* 3225 * Return true if continue to the next iteration directly, false 3226 * otherwise. 3227 */ 3228 static MigIterateState migration_iteration_run(MigrationState *s) 3229 { 3230 uint64_t pending_size, pend_pre, pend_compat, pend_post; 3231 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE; 3232 3233 qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre, 3234 &pend_compat, &pend_post); 3235 pending_size = pend_pre + pend_compat + pend_post; 3236 3237 trace_migrate_pending(pending_size, s->threshold_size, 3238 pend_pre, pend_compat, pend_post); 3239 3240 if (pending_size && pending_size >= s->threshold_size) { 3241 /* Still a significant amount to transfer */ 3242 if (!in_postcopy && pend_pre <= s->threshold_size && 3243 atomic_read(&s->start_postcopy)) { 3244 if (postcopy_start(s)) { 3245 error_report("%s: postcopy failed to start", __func__); 3246 } 3247 return MIG_ITERATE_SKIP; 3248 } 3249 /* Just another iteration step */ 3250 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy); 3251 } else { 3252 trace_migration_thread_low_pending(pending_size); 3253 migration_completion(s); 3254 return MIG_ITERATE_BREAK; 3255 } 3256 3257 return MIG_ITERATE_RESUME; 3258 } 3259 3260 static void migration_iteration_finish(MigrationState *s) 3261 { 3262 /* If we enabled cpu throttling for auto-converge, turn it off. */ 3263 cpu_throttle_stop(); 3264 3265 qemu_mutex_lock_iothread(); 3266 switch (s->state) { 3267 case MIGRATION_STATUS_COMPLETED: 3268 migration_calculate_complete(s); 3269 runstate_set(RUN_STATE_POSTMIGRATE); 3270 break; 3271 3272 case MIGRATION_STATUS_ACTIVE: 3273 /* 3274 * We should really assert here, but since it's during 3275 * migration, let's try to reduce the usage of assertions. 3276 */ 3277 if (!migrate_colo_enabled()) { 3278 error_report("%s: critical error: calling COLO code without " 3279 "COLO enabled", __func__); 3280 } 3281 migrate_start_colo_process(s); 3282 /* 3283 * Fixme: we will run VM in COLO no matter its old running state. 3284 * After exited COLO, we will keep running. 3285 */ 3286 s->vm_was_running = true; 3287 /* Fallthrough */ 3288 case MIGRATION_STATUS_FAILED: 3289 case MIGRATION_STATUS_CANCELLED: 3290 case MIGRATION_STATUS_CANCELLING: 3291 if (s->vm_was_running) { 3292 vm_start(); 3293 } else { 3294 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) { 3295 runstate_set(RUN_STATE_POSTMIGRATE); 3296 } 3297 } 3298 break; 3299 3300 default: 3301 /* Should not reach here, but if so, forgive the VM. */ 3302 error_report("%s: Unknown ending state %d", __func__, s->state); 3303 break; 3304 } 3305 migrate_fd_cleanup_schedule(s); 3306 qemu_mutex_unlock_iothread(); 3307 } 3308 3309 void migration_make_urgent_request(void) 3310 { 3311 qemu_sem_post(&migrate_get_current()->rate_limit_sem); 3312 } 3313 3314 void migration_consume_urgent_request(void) 3315 { 3316 qemu_sem_wait(&migrate_get_current()->rate_limit_sem); 3317 } 3318 3319 /* Returns true if the rate limiting was broken by an urgent request */ 3320 bool migration_rate_limit(void) 3321 { 3322 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 3323 MigrationState *s = migrate_get_current(); 3324 3325 bool urgent = false; 3326 migration_update_counters(s, now); 3327 if (qemu_file_rate_limit(s->to_dst_file)) { 3328 /* 3329 * Wait for a delay to do rate limiting OR 3330 * something urgent to post the semaphore. 3331 */ 3332 int ms = s->iteration_start_time + BUFFER_DELAY - now; 3333 trace_migration_rate_limit_pre(ms); 3334 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) { 3335 /* 3336 * We were woken by one or more urgent things but 3337 * the timedwait will have consumed one of them. 3338 * The service routine for the urgent wake will dec 3339 * the semaphore itself for each item it consumes, 3340 * so add this one we just eat back. 3341 */ 3342 qemu_sem_post(&s->rate_limit_sem); 3343 urgent = true; 3344 } 3345 trace_migration_rate_limit_post(urgent); 3346 } 3347 return urgent; 3348 } 3349 3350 /* 3351 * Master migration thread on the source VM. 3352 * It drives the migration and pumps the data down the outgoing channel. 3353 */ 3354 static void *migration_thread(void *opaque) 3355 { 3356 MigrationState *s = opaque; 3357 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); 3358 MigThrError thr_error; 3359 bool urgent = false; 3360 3361 rcu_register_thread(); 3362 3363 object_ref(OBJECT(s)); 3364 update_iteration_initial_status(s); 3365 3366 qemu_savevm_state_header(s->to_dst_file); 3367 3368 /* 3369 * If we opened the return path, we need to make sure dst has it 3370 * opened as well. 3371 */ 3372 if (s->rp_state.from_dst_file) { 3373 /* Now tell the dest that it should open its end so it can reply */ 3374 qemu_savevm_send_open_return_path(s->to_dst_file); 3375 3376 /* And do a ping that will make stuff easier to debug */ 3377 qemu_savevm_send_ping(s->to_dst_file, 1); 3378 } 3379 3380 if (migrate_postcopy()) { 3381 /* 3382 * Tell the destination that we *might* want to do postcopy later; 3383 * if the other end can't do postcopy it should fail now, nice and 3384 * early. 3385 */ 3386 qemu_savevm_send_postcopy_advise(s->to_dst_file); 3387 } 3388 3389 if (migrate_colo_enabled()) { 3390 /* Notify migration destination that we enable COLO */ 3391 qemu_savevm_send_colo_enable(s->to_dst_file); 3392 } 3393 3394 qemu_savevm_state_setup(s->to_dst_file); 3395 3396 if (qemu_savevm_state_guest_unplug_pending()) { 3397 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 3398 MIGRATION_STATUS_WAIT_UNPLUG); 3399 3400 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG && 3401 qemu_savevm_state_guest_unplug_pending()) { 3402 qemu_sem_timedwait(&s->wait_unplug_sem, 250); 3403 } 3404 3405 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, 3406 MIGRATION_STATUS_ACTIVE); 3407 } 3408 3409 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; 3410 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 3411 MIGRATION_STATUS_ACTIVE); 3412 3413 trace_migration_thread_setup_complete(); 3414 3415 while (migration_is_active(s)) { 3416 if (urgent || !qemu_file_rate_limit(s->to_dst_file)) { 3417 MigIterateState iter_state = migration_iteration_run(s); 3418 if (iter_state == MIG_ITERATE_SKIP) { 3419 continue; 3420 } else if (iter_state == MIG_ITERATE_BREAK) { 3421 break; 3422 } 3423 } 3424 3425 /* 3426 * Try to detect any kind of failures, and see whether we 3427 * should stop the migration now. 3428 */ 3429 thr_error = migration_detect_error(s); 3430 if (thr_error == MIG_THR_ERR_FATAL) { 3431 /* Stop migration */ 3432 break; 3433 } else if (thr_error == MIG_THR_ERR_RECOVERED) { 3434 /* 3435 * Just recovered from a e.g. network failure, reset all 3436 * the local variables. This is important to avoid 3437 * breaking transferred_bytes and bandwidth calculation 3438 */ 3439 update_iteration_initial_status(s); 3440 } 3441 3442 urgent = migration_rate_limit(); 3443 } 3444 3445 trace_migration_thread_after_loop(); 3446 migration_iteration_finish(s); 3447 object_unref(OBJECT(s)); 3448 rcu_unregister_thread(); 3449 return NULL; 3450 } 3451 3452 void migrate_fd_connect(MigrationState *s, Error *error_in) 3453 { 3454 Error *local_err = NULL; 3455 int64_t rate_limit; 3456 bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED; 3457 3458 s->expected_downtime = s->parameters.downtime_limit; 3459 s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s); 3460 if (error_in) { 3461 migrate_fd_error(s, error_in); 3462 migrate_fd_cleanup(s); 3463 return; 3464 } 3465 3466 if (resume) { 3467 /* This is a resumed migration */ 3468 rate_limit = s->parameters.max_postcopy_bandwidth / 3469 XFER_LIMIT_RATIO; 3470 } else { 3471 /* This is a fresh new migration */ 3472 rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO; 3473 3474 /* Notify before starting migration thread */ 3475 notifier_list_notify(&migration_state_notifiers, s); 3476 } 3477 3478 qemu_file_set_rate_limit(s->to_dst_file, rate_limit); 3479 qemu_file_set_blocking(s->to_dst_file, true); 3480 3481 /* 3482 * Open the return path. For postcopy, it is used exclusively. For 3483 * precopy, only if user specified "return-path" capability would 3484 * QEMU uses the return path. 3485 */ 3486 if (migrate_postcopy_ram() || migrate_use_return_path()) { 3487 if (open_return_path_on_source(s, !resume)) { 3488 error_report("Unable to open return-path for postcopy"); 3489 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED); 3490 migrate_fd_cleanup(s); 3491 return; 3492 } 3493 } 3494 3495 if (resume) { 3496 /* Wakeup the main migration thread to do the recovery */ 3497 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED, 3498 MIGRATION_STATUS_POSTCOPY_RECOVER); 3499 qemu_sem_post(&s->postcopy_pause_sem); 3500 return; 3501 } 3502 3503 if (multifd_save_setup(&local_err) != 0) { 3504 error_report_err(local_err); 3505 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 3506 MIGRATION_STATUS_FAILED); 3507 migrate_fd_cleanup(s); 3508 return; 3509 } 3510 qemu_thread_create(&s->thread, "live_migration", migration_thread, s, 3511 QEMU_THREAD_JOINABLE); 3512 s->migration_thread_running = true; 3513 } 3514 3515 void migration_global_dump(Monitor *mon) 3516 { 3517 MigrationState *ms = migrate_get_current(); 3518 3519 monitor_printf(mon, "globals:\n"); 3520 monitor_printf(mon, "store-global-state: %s\n", 3521 ms->store_global_state ? "on" : "off"); 3522 monitor_printf(mon, "only-migratable: %s\n", 3523 only_migratable ? "on" : "off"); 3524 monitor_printf(mon, "send-configuration: %s\n", 3525 ms->send_configuration ? "on" : "off"); 3526 monitor_printf(mon, "send-section-footer: %s\n", 3527 ms->send_section_footer ? "on" : "off"); 3528 monitor_printf(mon, "decompress-error-check: %s\n", 3529 ms->decompress_error_check ? "on" : "off"); 3530 monitor_printf(mon, "clear-bitmap-shift: %u\n", 3531 ms->clear_bitmap_shift); 3532 } 3533 3534 #define DEFINE_PROP_MIG_CAP(name, x) \ 3535 DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false) 3536 3537 static Property migration_properties[] = { 3538 DEFINE_PROP_BOOL("store-global-state", MigrationState, 3539 store_global_state, true), 3540 DEFINE_PROP_BOOL("send-configuration", MigrationState, 3541 send_configuration, true), 3542 DEFINE_PROP_BOOL("send-section-footer", MigrationState, 3543 send_section_footer, true), 3544 DEFINE_PROP_BOOL("decompress-error-check", MigrationState, 3545 decompress_error_check, true), 3546 DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState, 3547 clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT), 3548 3549 /* Migration parameters */ 3550 DEFINE_PROP_UINT8("x-compress-level", MigrationState, 3551 parameters.compress_level, 3552 DEFAULT_MIGRATE_COMPRESS_LEVEL), 3553 DEFINE_PROP_UINT8("x-compress-threads", MigrationState, 3554 parameters.compress_threads, 3555 DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT), 3556 DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState, 3557 parameters.compress_wait_thread, true), 3558 DEFINE_PROP_UINT8("x-decompress-threads", MigrationState, 3559 parameters.decompress_threads, 3560 DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT), 3561 DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState, 3562 parameters.cpu_throttle_initial, 3563 DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL), 3564 DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState, 3565 parameters.cpu_throttle_increment, 3566 DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT), 3567 DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState, 3568 parameters.max_bandwidth, MAX_THROTTLE), 3569 DEFINE_PROP_UINT64("x-downtime-limit", MigrationState, 3570 parameters.downtime_limit, 3571 DEFAULT_MIGRATE_SET_DOWNTIME), 3572 DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState, 3573 parameters.x_checkpoint_delay, 3574 DEFAULT_MIGRATE_X_CHECKPOINT_DELAY), 3575 DEFINE_PROP_UINT8("multifd-channels", MigrationState, 3576 parameters.multifd_channels, 3577 DEFAULT_MIGRATE_MULTIFD_CHANNELS), 3578 DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState, 3579 parameters.multifd_compression, 3580 DEFAULT_MIGRATE_MULTIFD_COMPRESSION), 3581 DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState, 3582 parameters.multifd_zlib_level, 3583 DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL), 3584 DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState, 3585 parameters.multifd_zstd_level, 3586 DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL), 3587 DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState, 3588 parameters.xbzrle_cache_size, 3589 DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE), 3590 DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState, 3591 parameters.max_postcopy_bandwidth, 3592 DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH), 3593 DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState, 3594 parameters.max_cpu_throttle, 3595 DEFAULT_MIGRATE_MAX_CPU_THROTTLE), 3596 DEFINE_PROP_SIZE("announce-initial", MigrationState, 3597 parameters.announce_initial, 3598 DEFAULT_MIGRATE_ANNOUNCE_INITIAL), 3599 DEFINE_PROP_SIZE("announce-max", MigrationState, 3600 parameters.announce_max, 3601 DEFAULT_MIGRATE_ANNOUNCE_MAX), 3602 DEFINE_PROP_SIZE("announce-rounds", MigrationState, 3603 parameters.announce_rounds, 3604 DEFAULT_MIGRATE_ANNOUNCE_ROUNDS), 3605 DEFINE_PROP_SIZE("announce-step", MigrationState, 3606 parameters.announce_step, 3607 DEFAULT_MIGRATE_ANNOUNCE_STEP), 3608 3609 /* Migration capabilities */ 3610 DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE), 3611 DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL), 3612 DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE), 3613 DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS), 3614 DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS), 3615 DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS), 3616 DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM), 3617 DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO), 3618 DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM), 3619 DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK), 3620 DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH), 3621 DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD), 3622 3623 DEFINE_PROP_END_OF_LIST(), 3624 }; 3625 3626 static void migration_class_init(ObjectClass *klass, void *data) 3627 { 3628 DeviceClass *dc = DEVICE_CLASS(klass); 3629 3630 dc->user_creatable = false; 3631 device_class_set_props(dc, migration_properties); 3632 } 3633 3634 static void migration_instance_finalize(Object *obj) 3635 { 3636 MigrationState *ms = MIGRATION_OBJ(obj); 3637 MigrationParameters *params = &ms->parameters; 3638 3639 qemu_mutex_destroy(&ms->error_mutex); 3640 qemu_mutex_destroy(&ms->qemu_file_lock); 3641 g_free(params->tls_hostname); 3642 g_free(params->tls_creds); 3643 qemu_sem_destroy(&ms->wait_unplug_sem); 3644 qemu_sem_destroy(&ms->rate_limit_sem); 3645 qemu_sem_destroy(&ms->pause_sem); 3646 qemu_sem_destroy(&ms->postcopy_pause_sem); 3647 qemu_sem_destroy(&ms->postcopy_pause_rp_sem); 3648 qemu_sem_destroy(&ms->rp_state.rp_sem); 3649 error_free(ms->error); 3650 } 3651 3652 static void migration_instance_init(Object *obj) 3653 { 3654 MigrationState *ms = MIGRATION_OBJ(obj); 3655 MigrationParameters *params = &ms->parameters; 3656 3657 ms->state = MIGRATION_STATUS_NONE; 3658 ms->mbps = -1; 3659 ms->pages_per_second = -1; 3660 qemu_sem_init(&ms->pause_sem, 0); 3661 qemu_mutex_init(&ms->error_mutex); 3662 3663 params->tls_hostname = g_strdup(""); 3664 params->tls_creds = g_strdup(""); 3665 3666 /* Set has_* up only for parameter checks */ 3667 params->has_compress_level = true; 3668 params->has_compress_threads = true; 3669 params->has_decompress_threads = true; 3670 params->has_cpu_throttle_initial = true; 3671 params->has_cpu_throttle_increment = true; 3672 params->has_max_bandwidth = true; 3673 params->has_downtime_limit = true; 3674 params->has_x_checkpoint_delay = true; 3675 params->has_block_incremental = true; 3676 params->has_multifd_channels = true; 3677 params->has_multifd_compression = true; 3678 params->has_multifd_zlib_level = true; 3679 params->has_multifd_zstd_level = true; 3680 params->has_xbzrle_cache_size = true; 3681 params->has_max_postcopy_bandwidth = true; 3682 params->has_max_cpu_throttle = true; 3683 params->has_announce_initial = true; 3684 params->has_announce_max = true; 3685 params->has_announce_rounds = true; 3686 params->has_announce_step = true; 3687 3688 qemu_sem_init(&ms->postcopy_pause_sem, 0); 3689 qemu_sem_init(&ms->postcopy_pause_rp_sem, 0); 3690 qemu_sem_init(&ms->rp_state.rp_sem, 0); 3691 qemu_sem_init(&ms->rate_limit_sem, 0); 3692 qemu_sem_init(&ms->wait_unplug_sem, 0); 3693 qemu_mutex_init(&ms->qemu_file_lock); 3694 } 3695 3696 /* 3697 * Return true if check pass, false otherwise. Error will be put 3698 * inside errp if provided. 3699 */ 3700 static bool migration_object_check(MigrationState *ms, Error **errp) 3701 { 3702 MigrationCapabilityStatusList *head = NULL; 3703 /* Assuming all off */ 3704 bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret; 3705 int i; 3706 3707 if (!migrate_params_check(&ms->parameters, errp)) { 3708 return false; 3709 } 3710 3711 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { 3712 if (ms->enabled_capabilities[i]) { 3713 head = migrate_cap_add(head, i, true); 3714 } 3715 } 3716 3717 ret = migrate_caps_check(cap_list, head, errp); 3718 3719 /* It works with head == NULL */ 3720 qapi_free_MigrationCapabilityStatusList(head); 3721 3722 return ret; 3723 } 3724 3725 static const TypeInfo migration_type = { 3726 .name = TYPE_MIGRATION, 3727 /* 3728 * NOTE: TYPE_MIGRATION is not really a device, as the object is 3729 * not created using qdev_create(), it is not attached to the qdev 3730 * device tree, and it is never realized. 3731 * 3732 * TODO: Make this TYPE_OBJECT once QOM provides something like 3733 * TYPE_DEVICE's "-global" properties. 3734 */ 3735 .parent = TYPE_DEVICE, 3736 .class_init = migration_class_init, 3737 .class_size = sizeof(MigrationClass), 3738 .instance_size = sizeof(MigrationState), 3739 .instance_init = migration_instance_init, 3740 .instance_finalize = migration_instance_finalize, 3741 }; 3742 3743 static void register_migration_types(void) 3744 { 3745 type_register_static(&migration_type); 3746 } 3747 3748 type_init(register_migration_types); 3749