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