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