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/ctype.h" 18 #include "qemu/cutils.h" 19 #include "qemu/error-report.h" 20 #include "qemu/main-loop.h" 21 #include "migration/blocker.h" 22 #include "exec.h" 23 #include "fd.h" 24 #include "file.h" 25 #include "socket.h" 26 #include "system/runstate.h" 27 #include "system/system.h" 28 #include "system/cpu-throttle.h" 29 #include "rdma.h" 30 #include "ram.h" 31 #include "migration/cpr.h" 32 #include "migration/global_state.h" 33 #include "migration/misc.h" 34 #include "migration.h" 35 #include "migration-stats.h" 36 #include "savevm.h" 37 #include "qemu-file.h" 38 #include "channel.h" 39 #include "migration/vmstate.h" 40 #include "block/block.h" 41 #include "qapi/error.h" 42 #include "qapi/clone-visitor.h" 43 #include "qapi/qapi-visit-migration.h" 44 #include "qapi/qapi-visit-sockets.h" 45 #include "qapi/qapi-commands-migration.h" 46 #include "qapi/qapi-events-migration.h" 47 #include "qapi/qmp/qerror.h" 48 #include "qobject/qnull.h" 49 #include "qemu/rcu.h" 50 #include "postcopy-ram.h" 51 #include "qemu/thread.h" 52 #include "trace.h" 53 #include "exec/target_page.h" 54 #include "io/channel-buffer.h" 55 #include "io/channel-tls.h" 56 #include "migration/colo.h" 57 #include "hw/boards.h" 58 #include "monitor/monitor.h" 59 #include "net/announce.h" 60 #include "qemu/queue.h" 61 #include "multifd.h" 62 #include "threadinfo.h" 63 #include "qemu/yank.h" 64 #include "system/cpus.h" 65 #include "yank_functions.h" 66 #include "system/qtest.h" 67 #include "options.h" 68 #include "system/dirtylimit.h" 69 #include "qemu/sockets.h" 70 #include "system/kvm.h" 71 72 #define NOTIFIER_ELEM_INIT(array, elem) \ 73 [elem] = NOTIFIER_WITH_RETURN_LIST_INITIALIZER((array)[elem]) 74 75 #define INMIGRATE_DEFAULT_EXIT_ON_ERROR true 76 77 static NotifierWithReturnList migration_state_notifiers[] = { 78 NOTIFIER_ELEM_INIT(migration_state_notifiers, MIG_MODE_NORMAL), 79 NOTIFIER_ELEM_INIT(migration_state_notifiers, MIG_MODE_CPR_REBOOT), 80 NOTIFIER_ELEM_INIT(migration_state_notifiers, MIG_MODE_CPR_TRANSFER), 81 }; 82 83 /* Messages sent on the return path from destination to source */ 84 enum mig_rp_message_type { 85 MIG_RP_MSG_INVALID = 0, /* Must be 0 */ 86 MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */ 87 MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */ 88 89 MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */ 90 MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */ 91 MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */ 92 MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */ 93 MIG_RP_MSG_SWITCHOVER_ACK, /* Tell source it's OK to do switchover */ 94 95 MIG_RP_MSG_MAX 96 }; 97 98 /* When we add fault tolerance, we could have several 99 migrations at once. For now we don't need to add 100 dynamic creation of migration */ 101 102 static MigrationState *current_migration; 103 static MigrationIncomingState *current_incoming; 104 105 static GSList *migration_blockers[MIG_MODE__MAX]; 106 107 static bool migration_object_check(MigrationState *ms, Error **errp); 108 static bool migration_switchover_start(MigrationState *s, Error **errp); 109 static bool close_return_path_on_source(MigrationState *s); 110 static void migration_completion_end(MigrationState *s); 111 static void migrate_hup_delete(MigrationState *s); 112 113 static void migration_downtime_start(MigrationState *s) 114 { 115 trace_vmstate_downtime_checkpoint("src-downtime-start"); 116 s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 117 } 118 119 /* 120 * This is unfortunate: incoming migration actually needs the outgoing 121 * migration state (MigrationState) to be there too, e.g. to query 122 * capabilities, parameters, using locks, setup errors, etc. 123 * 124 * NOTE: when calling this, making sure current_migration exists and not 125 * been freed yet! Otherwise trying to access the refcount is already 126 * an use-after-free itself.. 127 * 128 * TODO: Move shared part of incoming / outgoing out into separate object. 129 * Then this is not needed. 130 */ 131 static void migrate_incoming_ref_outgoing_state(void) 132 { 133 object_ref(migrate_get_current()); 134 } 135 static void migrate_incoming_unref_outgoing_state(void) 136 { 137 object_unref(migrate_get_current()); 138 } 139 140 static void migration_downtime_end(MigrationState *s) 141 { 142 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 143 144 /* 145 * If downtime already set, should mean that postcopy already set it, 146 * then that should be the real downtime already. 147 */ 148 if (!s->downtime) { 149 s->downtime = now - s->downtime_start; 150 trace_vmstate_downtime_checkpoint("src-downtime-end"); 151 } 152 } 153 154 static void precopy_notify_complete(void) 155 { 156 Error *local_err = NULL; 157 158 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) { 159 error_report_err(local_err); 160 } 161 162 trace_migration_precopy_complete(); 163 } 164 165 static bool migration_needs_multiple_sockets(void) 166 { 167 return migrate_multifd() || migrate_postcopy_preempt(); 168 } 169 170 static RunState migration_get_target_runstate(void) 171 { 172 /* 173 * When the global state is not migrated, it means we don't know the 174 * runstate of the src QEMU. We don't have much choice but assuming 175 * the VM is running. NOTE: this is pretty rare case, so far only Xen 176 * uses it. 177 */ 178 if (!global_state_received()) { 179 return RUN_STATE_RUNNING; 180 } 181 182 return global_state_get_runstate(); 183 } 184 185 static bool transport_supports_multi_channels(MigrationAddress *addr) 186 { 187 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) { 188 SocketAddress *saddr = &addr->u.socket; 189 190 return (saddr->type == SOCKET_ADDRESS_TYPE_INET || 191 saddr->type == SOCKET_ADDRESS_TYPE_UNIX || 192 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK); 193 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) { 194 return migrate_mapped_ram(); 195 } else { 196 return false; 197 } 198 } 199 200 static bool migration_needs_seekable_channel(void) 201 { 202 return migrate_mapped_ram(); 203 } 204 205 static bool migration_needs_extra_fds(void) 206 { 207 /* 208 * When doing direct-io, multifd requires two different, 209 * non-duplicated file descriptors so we can use one of them for 210 * unaligned IO. 211 */ 212 return migrate_multifd() && migrate_direct_io(); 213 } 214 215 static bool transport_supports_seeking(MigrationAddress *addr) 216 { 217 if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) { 218 return true; 219 } 220 221 return false; 222 } 223 224 static bool transport_supports_extra_fds(MigrationAddress *addr) 225 { 226 /* file: works because QEMU can open it multiple times */ 227 return addr->transport == MIGRATION_ADDRESS_TYPE_FILE; 228 } 229 230 static bool 231 migration_channels_and_transport_compatible(MigrationAddress *addr, 232 Error **errp) 233 { 234 if (migration_needs_seekable_channel() && 235 !transport_supports_seeking(addr)) { 236 error_setg(errp, "Migration requires seekable transport (e.g. file)"); 237 return false; 238 } 239 240 if (migration_needs_multiple_sockets() && 241 !transport_supports_multi_channels(addr)) { 242 error_setg(errp, "Migration requires multi-channel URIs (e.g. tcp)"); 243 return false; 244 } 245 246 if (migration_needs_extra_fds() && 247 !transport_supports_extra_fds(addr)) { 248 error_setg(errp, 249 "Migration requires a transport that allows for extra fds (e.g. file)"); 250 return false; 251 } 252 253 if (migrate_mode() == MIG_MODE_CPR_TRANSFER && 254 addr->transport == MIGRATION_ADDRESS_TYPE_FILE) { 255 error_setg(errp, "Migration requires streamable transport (eg unix)"); 256 return false; 257 } 258 259 return true; 260 } 261 262 static bool 263 migration_capabilities_and_transport_compatible(MigrationAddress *addr, 264 Error **errp) 265 { 266 if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) { 267 return migrate_rdma_caps_check(migrate_get_current()->capabilities, 268 errp); 269 } 270 271 return true; 272 } 273 274 static bool migration_transport_compatible(MigrationAddress *addr, Error **errp) 275 { 276 return migration_channels_and_transport_compatible(addr, errp) && 277 migration_capabilities_and_transport_compatible(addr, errp); 278 } 279 280 static gint page_request_addr_cmp(gconstpointer ap, gconstpointer bp) 281 { 282 uintptr_t a = (uintptr_t) ap, b = (uintptr_t) bp; 283 284 return (a > b) - (a < b); 285 } 286 287 static int migration_stop_vm(MigrationState *s, RunState state) 288 { 289 int ret; 290 291 migration_downtime_start(s); 292 293 s->vm_old_state = runstate_get(); 294 global_state_store(); 295 296 ret = vm_stop_force_state(state); 297 298 trace_vmstate_downtime_checkpoint("src-vm-stopped"); 299 trace_migration_completion_vm_stop(ret); 300 301 return ret; 302 } 303 304 void migration_object_init(void) 305 { 306 /* This can only be called once. */ 307 assert(!current_migration); 308 current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION)); 309 310 /* 311 * Init the migrate incoming object as well no matter whether 312 * we'll use it or not. 313 */ 314 assert(!current_incoming); 315 current_incoming = g_new0(MigrationIncomingState, 1); 316 current_incoming->state = MIGRATION_STATUS_NONE; 317 current_incoming->postcopy_remote_fds = 318 g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD)); 319 qemu_mutex_init(¤t_incoming->rp_mutex); 320 qemu_mutex_init(¤t_incoming->postcopy_prio_thread_mutex); 321 qemu_event_init(¤t_incoming->main_thread_load_event, false); 322 qemu_sem_init(¤t_incoming->postcopy_pause_sem_dst, 0); 323 qemu_sem_init(¤t_incoming->postcopy_pause_sem_fault, 0); 324 qemu_sem_init(¤t_incoming->postcopy_pause_sem_fast_load, 0); 325 qemu_sem_init(¤t_incoming->postcopy_qemufile_dst_done, 0); 326 327 qemu_mutex_init(¤t_incoming->page_request_mutex); 328 qemu_cond_init(¤t_incoming->page_request_cond); 329 current_incoming->page_requested = g_tree_new(page_request_addr_cmp); 330 331 current_incoming->exit_on_error = INMIGRATE_DEFAULT_EXIT_ON_ERROR; 332 333 migration_object_check(current_migration, &error_fatal); 334 335 ram_mig_init(); 336 dirty_bitmap_mig_init(); 337 338 /* Initialize cpu throttle timers */ 339 cpu_throttle_init(); 340 } 341 342 typedef struct { 343 QEMUBH *bh; 344 QEMUBHFunc *cb; 345 void *opaque; 346 } MigrationBH; 347 348 static void migration_bh_dispatch_bh(void *opaque) 349 { 350 MigrationState *s = migrate_get_current(); 351 MigrationBH *migbh = opaque; 352 353 /* cleanup this BH */ 354 qemu_bh_delete(migbh->bh); 355 migbh->bh = NULL; 356 357 /* dispatch the other one */ 358 migbh->cb(migbh->opaque); 359 object_unref(OBJECT(s)); 360 361 g_free(migbh); 362 } 363 364 void migration_bh_schedule(QEMUBHFunc *cb, void *opaque) 365 { 366 MigrationState *s = migrate_get_current(); 367 MigrationBH *migbh = g_new0(MigrationBH, 1); 368 QEMUBH *bh = qemu_bh_new(migration_bh_dispatch_bh, migbh); 369 370 /* Store these to dispatch when the BH runs */ 371 migbh->bh = bh; 372 migbh->cb = cb; 373 migbh->opaque = opaque; 374 375 /* 376 * Ref the state for bh, because it may be called when 377 * there're already no other refs 378 */ 379 object_ref(OBJECT(s)); 380 qemu_bh_schedule(bh); 381 } 382 383 void migration_shutdown(void) 384 { 385 /* 386 * When the QEMU main thread exit, the COLO thread 387 * may wait a semaphore. So, we should wakeup the 388 * COLO thread before migration shutdown. 389 */ 390 colo_shutdown(); 391 /* 392 * Cancel the current migration - that will (eventually) 393 * stop the migration using this structure 394 */ 395 migration_cancel(); 396 object_unref(OBJECT(current_migration)); 397 398 /* 399 * Cancel outgoing migration of dirty bitmaps. It should 400 * at least unref used block nodes. 401 */ 402 dirty_bitmap_mig_cancel_outgoing(); 403 404 /* 405 * Cancel incoming migration of dirty bitmaps. Dirty bitmaps 406 * are non-critical data, and their loss never considered as 407 * something serious. 408 */ 409 dirty_bitmap_mig_cancel_incoming(); 410 } 411 412 /* For outgoing */ 413 MigrationState *migrate_get_current(void) 414 { 415 /* This can only be called after the object created. */ 416 assert(current_migration); 417 return current_migration; 418 } 419 420 MigrationIncomingState *migration_incoming_get_current(void) 421 { 422 assert(current_incoming); 423 return current_incoming; 424 } 425 426 void migration_incoming_transport_cleanup(MigrationIncomingState *mis) 427 { 428 if (mis->socket_address_list) { 429 qapi_free_SocketAddressList(mis->socket_address_list); 430 mis->socket_address_list = NULL; 431 } 432 433 if (mis->transport_cleanup) { 434 mis->transport_cleanup(mis->transport_data); 435 mis->transport_data = mis->transport_cleanup = NULL; 436 } 437 } 438 439 void migration_incoming_state_destroy(void) 440 { 441 struct MigrationIncomingState *mis = migration_incoming_get_current(); 442 443 multifd_recv_cleanup(); 444 445 /* 446 * RAM state cleanup needs to happen after multifd cleanup, because 447 * multifd threads can use some of its states (receivedmap). 448 * The VFIO load_cleanup() implementation is BQL-sensitive. It requires 449 * BQL must NOT be taken when recycling load threads, so that it won't 450 * block the load threads from making progress on address space 451 * modification operations. 452 * 453 * To make it work, we could try to not take BQL for all load_cleanup(), 454 * or conditionally unlock BQL only if bql_locked() in VFIO. 455 * 456 * Since most existing call sites take BQL for load_cleanup(), make 457 * it simple by taking BQL always as the rule, so that VFIO can unlock 458 * BQL and retake unconditionally. 459 */ 460 assert(bql_locked()); 461 qemu_loadvm_state_cleanup(mis); 462 463 if (mis->to_src_file) { 464 /* Tell source that we are done */ 465 migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0); 466 qemu_fclose(mis->to_src_file); 467 mis->to_src_file = NULL; 468 } 469 470 if (mis->from_src_file) { 471 migration_ioc_unregister_yank_from_file(mis->from_src_file); 472 qemu_fclose(mis->from_src_file); 473 mis->from_src_file = NULL; 474 } 475 if (mis->postcopy_remote_fds) { 476 g_array_free(mis->postcopy_remote_fds, TRUE); 477 mis->postcopy_remote_fds = NULL; 478 } 479 480 migration_incoming_transport_cleanup(mis); 481 qemu_event_reset(&mis->main_thread_load_event); 482 483 if (mis->page_requested) { 484 g_tree_destroy(mis->page_requested); 485 mis->page_requested = NULL; 486 } 487 488 if (mis->postcopy_qemufile_dst) { 489 migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst); 490 qemu_fclose(mis->postcopy_qemufile_dst); 491 mis->postcopy_qemufile_dst = NULL; 492 } 493 494 cpr_set_incoming_mode(MIG_MODE_NONE); 495 yank_unregister_instance(MIGRATION_YANK_INSTANCE); 496 } 497 498 static void migrate_generate_event(MigrationStatus new_state) 499 { 500 if (migrate_events()) { 501 qapi_event_send_migration(new_state); 502 } 503 } 504 505 /* 506 * Send a message on the return channel back to the source 507 * of the migration. 508 */ 509 static int migrate_send_rp_message(MigrationIncomingState *mis, 510 enum mig_rp_message_type message_type, 511 uint16_t len, void *data) 512 { 513 int ret = 0; 514 515 trace_migrate_send_rp_message((int)message_type, len); 516 QEMU_LOCK_GUARD(&mis->rp_mutex); 517 518 /* 519 * It's possible that the file handle got lost due to network 520 * failures. 521 */ 522 if (!mis->to_src_file) { 523 ret = -EIO; 524 return ret; 525 } 526 527 qemu_put_be16(mis->to_src_file, (unsigned int)message_type); 528 qemu_put_be16(mis->to_src_file, len); 529 qemu_put_buffer(mis->to_src_file, data, len); 530 return qemu_fflush(mis->to_src_file); 531 } 532 533 /* Request one page from the source VM at the given start address. 534 * rb: the RAMBlock to request the page in 535 * Start: Address offset within the RB 536 * Len: Length in bytes required - must be a multiple of pagesize 537 */ 538 int migrate_send_rp_message_req_pages(MigrationIncomingState *mis, 539 RAMBlock *rb, ram_addr_t start) 540 { 541 uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */ 542 size_t msglen = 12; /* start + len */ 543 size_t len = qemu_ram_pagesize(rb); 544 enum mig_rp_message_type msg_type; 545 const char *rbname; 546 int rbname_len; 547 548 *(uint64_t *)bufc = cpu_to_be64((uint64_t)start); 549 *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len); 550 551 /* 552 * We maintain the last ramblock that we requested for page. Note that we 553 * don't need locking because this function will only be called within the 554 * postcopy ram fault thread. 555 */ 556 if (rb != mis->last_rb) { 557 mis->last_rb = rb; 558 559 rbname = qemu_ram_get_idstr(rb); 560 rbname_len = strlen(rbname); 561 562 assert(rbname_len < 256); 563 564 bufc[msglen++] = rbname_len; 565 memcpy(bufc + msglen, rbname, rbname_len); 566 msglen += rbname_len; 567 msg_type = MIG_RP_MSG_REQ_PAGES_ID; 568 } else { 569 msg_type = MIG_RP_MSG_REQ_PAGES; 570 } 571 572 return migrate_send_rp_message(mis, msg_type, msglen, bufc); 573 } 574 575 int migrate_send_rp_req_pages(MigrationIncomingState *mis, 576 RAMBlock *rb, ram_addr_t start, uint64_t haddr) 577 { 578 void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb)); 579 bool received = false; 580 581 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) { 582 received = ramblock_recv_bitmap_test_byte_offset(rb, start); 583 if (!received && !g_tree_lookup(mis->page_requested, aligned)) { 584 /* 585 * The page has not been received, and it's not yet in the page 586 * request list. Queue it. Set the value of element to 1, so that 587 * things like g_tree_lookup() will return TRUE (1) when found. 588 */ 589 g_tree_insert(mis->page_requested, aligned, (gpointer)1); 590 qatomic_inc(&mis->page_requested_count); 591 trace_postcopy_page_req_add(aligned, mis->page_requested_count); 592 } 593 } 594 595 /* 596 * If the page is there, skip sending the message. We don't even need the 597 * lock because as long as the page arrived, it'll be there forever. 598 */ 599 if (received) { 600 return 0; 601 } 602 603 return migrate_send_rp_message_req_pages(mis, rb, start); 604 } 605 606 static bool migration_colo_enabled; 607 bool migration_incoming_colo_enabled(void) 608 { 609 return migration_colo_enabled; 610 } 611 612 void migration_incoming_disable_colo(void) 613 { 614 ram_block_discard_disable(false); 615 migration_colo_enabled = false; 616 } 617 618 int migration_incoming_enable_colo(void) 619 { 620 #ifndef CONFIG_REPLICATION 621 error_report("ENABLE_COLO command come in migration stream, but the " 622 "replication module is not built in"); 623 return -ENOTSUP; 624 #endif 625 626 if (!migrate_colo()) { 627 error_report("ENABLE_COLO command come in migration stream, but x-colo " 628 "capability is not set"); 629 return -EINVAL; 630 } 631 632 if (ram_block_discard_disable(true)) { 633 error_report("COLO: cannot disable RAM discard"); 634 return -EBUSY; 635 } 636 migration_colo_enabled = true; 637 return 0; 638 } 639 640 void migrate_add_address(SocketAddress *address) 641 { 642 MigrationIncomingState *mis = migration_incoming_get_current(); 643 644 QAPI_LIST_PREPEND(mis->socket_address_list, 645 QAPI_CLONE(SocketAddress, address)); 646 } 647 648 bool migrate_is_uri(const char *uri) 649 { 650 while (*uri && *uri != ':') { 651 if (!qemu_isalpha(*uri++)) { 652 return false; 653 } 654 } 655 return *uri == ':'; 656 } 657 658 bool migrate_uri_parse(const char *uri, MigrationChannel **channel, 659 Error **errp) 660 { 661 g_autoptr(MigrationChannel) val = g_new0(MigrationChannel, 1); 662 g_autoptr(MigrationAddress) addr = g_new0(MigrationAddress, 1); 663 InetSocketAddress *isock = &addr->u.rdma; 664 strList **tail = &addr->u.exec.args; 665 666 if (strstart(uri, "exec:", NULL)) { 667 addr->transport = MIGRATION_ADDRESS_TYPE_EXEC; 668 #ifdef WIN32 669 QAPI_LIST_APPEND(tail, g_strdup(exec_get_cmd_path())); 670 QAPI_LIST_APPEND(tail, g_strdup("/c")); 671 #else 672 QAPI_LIST_APPEND(tail, g_strdup("/bin/sh")); 673 QAPI_LIST_APPEND(tail, g_strdup("-c")); 674 #endif 675 QAPI_LIST_APPEND(tail, g_strdup(uri + strlen("exec:"))); 676 } else if (strstart(uri, "rdma:", NULL)) { 677 if (inet_parse(isock, uri + strlen("rdma:"), errp)) { 678 qapi_free_InetSocketAddress(isock); 679 return false; 680 } 681 addr->transport = MIGRATION_ADDRESS_TYPE_RDMA; 682 } else if (strstart(uri, "tcp:", NULL) || 683 strstart(uri, "unix:", NULL) || 684 strstart(uri, "vsock:", NULL) || 685 strstart(uri, "fd:", NULL)) { 686 addr->transport = MIGRATION_ADDRESS_TYPE_SOCKET; 687 SocketAddress *saddr = socket_parse(uri, errp); 688 if (!saddr) { 689 return false; 690 } 691 addr->u.socket.type = saddr->type; 692 addr->u.socket.u = saddr->u; 693 /* Don't free the objects inside; their ownership moved to "addr" */ 694 g_free(saddr); 695 } else if (strstart(uri, "file:", NULL)) { 696 addr->transport = MIGRATION_ADDRESS_TYPE_FILE; 697 addr->u.file.filename = g_strdup(uri + strlen("file:")); 698 if (file_parse_offset(addr->u.file.filename, &addr->u.file.offset, 699 errp)) { 700 return false; 701 } 702 } else { 703 error_setg(errp, "unknown migration protocol: %s", uri); 704 return false; 705 } 706 707 val->channel_type = MIGRATION_CHANNEL_TYPE_MAIN; 708 val->addr = g_steal_pointer(&addr); 709 *channel = g_steal_pointer(&val); 710 return true; 711 } 712 713 static bool 714 migration_incoming_state_setup(MigrationIncomingState *mis, Error **errp) 715 { 716 MigrationStatus current = mis->state; 717 718 if (current == MIGRATION_STATUS_POSTCOPY_PAUSED) { 719 /* 720 * Incoming postcopy migration will stay in PAUSED state even if 721 * reconnection happened. 722 */ 723 return true; 724 } 725 726 if (current != MIGRATION_STATUS_NONE) { 727 error_setg(errp, "Illegal migration incoming state: %s", 728 MigrationStatus_str(current)); 729 return false; 730 } 731 732 migrate_set_state(&mis->state, current, MIGRATION_STATUS_SETUP); 733 return true; 734 } 735 736 static void qemu_start_incoming_migration(const char *uri, bool has_channels, 737 MigrationChannelList *channels, 738 Error **errp) 739 { 740 g_autoptr(MigrationChannel) channel = NULL; 741 MigrationAddress *addr = NULL; 742 MigrationIncomingState *mis = migration_incoming_get_current(); 743 744 /* 745 * Having preliminary checks for uri and channel 746 */ 747 if (!uri == !channels) { 748 error_setg(errp, "need either 'uri' or 'channels' argument"); 749 return; 750 } 751 752 if (channels) { 753 /* To verify that Migrate channel list has only item */ 754 if (channels->next) { 755 error_setg(errp, "Channel list must have only one entry, " 756 "for type 'main'"); 757 return; 758 } 759 addr = channels->value->addr; 760 } 761 762 if (uri) { 763 /* caller uses the old URI syntax */ 764 if (!migrate_uri_parse(uri, &channel, errp)) { 765 return; 766 } 767 addr = channel->addr; 768 } 769 770 /* transport mechanism not suitable for migration? */ 771 if (!migration_transport_compatible(addr, errp)) { 772 return; 773 } 774 775 if (!migration_incoming_state_setup(mis, errp)) { 776 return; 777 } 778 779 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) { 780 SocketAddress *saddr = &addr->u.socket; 781 if (saddr->type == SOCKET_ADDRESS_TYPE_INET || 782 saddr->type == SOCKET_ADDRESS_TYPE_UNIX || 783 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK) { 784 socket_start_incoming_migration(saddr, errp); 785 } else if (saddr->type == SOCKET_ADDRESS_TYPE_FD) { 786 fd_start_incoming_migration(saddr->u.fd.str, errp); 787 } 788 #ifdef CONFIG_RDMA 789 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) { 790 rdma_start_incoming_migration(&addr->u.rdma, errp); 791 #endif 792 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_EXEC) { 793 exec_start_incoming_migration(addr->u.exec.args, errp); 794 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) { 795 file_start_incoming_migration(&addr->u.file, errp); 796 } else { 797 error_setg(errp, "unknown migration protocol: %s", uri); 798 } 799 800 /* Close cpr socket to tell source that we are listening */ 801 cpr_state_close(); 802 } 803 804 static void process_incoming_migration_bh(void *opaque) 805 { 806 MigrationIncomingState *mis = opaque; 807 808 trace_vmstate_downtime_checkpoint("dst-precopy-bh-enter"); 809 810 /* 811 * This must happen after all error conditions are dealt with and 812 * we're sure the VM is going to be running on this host. 813 */ 814 qemu_announce_self(&mis->announce_timer, migrate_announce_params()); 815 816 trace_vmstate_downtime_checkpoint("dst-precopy-bh-announced"); 817 818 multifd_recv_shutdown(); 819 820 dirty_bitmap_mig_before_vm_start(); 821 822 if (runstate_is_live(migration_get_target_runstate())) { 823 if (autostart) { 824 /* 825 * Block activation is always delayed until VM starts, either 826 * here (which means we need to start the dest VM right now..), 827 * or until qmp_cont() later. 828 * 829 * We used to have cap 'late-block-activate' but now we do this 830 * unconditionally, as it has no harm but only benefit. E.g., 831 * it's not part of migration ABI on the time of disk activation. 832 * 833 * Make sure all file formats throw away their mutable 834 * metadata. If error, don't restart the VM yet. 835 */ 836 if (migration_block_activate(NULL)) { 837 vm_start(); 838 } 839 } else { 840 runstate_set(RUN_STATE_PAUSED); 841 } 842 } else if (migration_incoming_colo_enabled()) { 843 migration_incoming_disable_colo(); 844 vm_start(); 845 } else { 846 runstate_set(global_state_get_runstate()); 847 } 848 trace_vmstate_downtime_checkpoint("dst-precopy-bh-vm-started"); 849 /* 850 * This must happen after any state changes since as soon as an external 851 * observer sees this event they might start to prod at the VM assuming 852 * it's ready to use. 853 */ 854 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 855 MIGRATION_STATUS_COMPLETED); 856 migration_incoming_state_destroy(); 857 } 858 859 static void coroutine_fn 860 process_incoming_migration_co(void *opaque) 861 { 862 MigrationState *s = migrate_get_current(); 863 MigrationIncomingState *mis = migration_incoming_get_current(); 864 PostcopyState ps; 865 int ret; 866 Error *local_err = NULL; 867 868 assert(mis->from_src_file); 869 870 mis->largest_page_size = qemu_ram_pagesize_largest(); 871 postcopy_state_set(POSTCOPY_INCOMING_NONE); 872 migrate_set_state(&mis->state, MIGRATION_STATUS_SETUP, 873 MIGRATION_STATUS_ACTIVE); 874 875 mis->loadvm_co = qemu_coroutine_self(); 876 ret = qemu_loadvm_state(mis->from_src_file); 877 mis->loadvm_co = NULL; 878 879 trace_vmstate_downtime_checkpoint("dst-precopy-loadvm-completed"); 880 881 ps = postcopy_state_get(); 882 trace_process_incoming_migration_co_end(ret, ps); 883 if (ps != POSTCOPY_INCOMING_NONE) { 884 if (ps == POSTCOPY_INCOMING_ADVISE) { 885 /* 886 * Where a migration had postcopy enabled (and thus went to advise) 887 * but managed to complete within the precopy period, we can use 888 * the normal exit. 889 */ 890 postcopy_ram_incoming_cleanup(mis); 891 } else if (ret >= 0) { 892 /* 893 * Postcopy was started, cleanup should happen at the end of the 894 * postcopy thread. 895 */ 896 trace_process_incoming_migration_co_postcopy_end_main(); 897 goto out; 898 } 899 /* Else if something went wrong then just fall out of the normal exit */ 900 } 901 902 if (ret < 0) { 903 error_setg(&local_err, "load of migration failed: %s", strerror(-ret)); 904 goto fail; 905 } 906 907 if (migration_incoming_colo_enabled()) { 908 /* yield until COLO exit */ 909 colo_incoming_co(); 910 } 911 912 migration_bh_schedule(process_incoming_migration_bh, mis); 913 goto out; 914 915 fail: 916 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 917 MIGRATION_STATUS_FAILED); 918 migrate_set_error(s, local_err); 919 error_free(local_err); 920 921 migration_incoming_state_destroy(); 922 923 if (mis->exit_on_error) { 924 WITH_QEMU_LOCK_GUARD(&s->error_mutex) { 925 error_report_err(s->error); 926 s->error = NULL; 927 } 928 929 exit(EXIT_FAILURE); 930 } 931 out: 932 /* Pairs with the refcount taken in qmp_migrate_incoming() */ 933 migrate_incoming_unref_outgoing_state(); 934 } 935 936 /** 937 * migration_incoming_setup: Setup incoming migration 938 * @f: file for main migration channel 939 */ 940 static void migration_incoming_setup(QEMUFile *f) 941 { 942 MigrationIncomingState *mis = migration_incoming_get_current(); 943 944 if (!mis->from_src_file) { 945 mis->from_src_file = f; 946 } 947 qemu_file_set_blocking(f, false); 948 } 949 950 void migration_incoming_process(void) 951 { 952 Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL); 953 qemu_coroutine_enter(co); 954 } 955 956 /* Returns true if recovered from a paused migration, otherwise false */ 957 static bool postcopy_try_recover(void) 958 { 959 MigrationIncomingState *mis = migration_incoming_get_current(); 960 961 if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { 962 /* Resumed from a paused postcopy migration */ 963 964 /* This should be set already in migration_incoming_setup() */ 965 assert(mis->from_src_file); 966 /* Postcopy has standalone thread to do vm load */ 967 qemu_file_set_blocking(mis->from_src_file, true); 968 969 /* Re-configure the return path */ 970 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file); 971 972 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED, 973 MIGRATION_STATUS_POSTCOPY_RECOVER); 974 975 /* 976 * Here, we only wake up the main loading thread (while the 977 * rest threads will still be waiting), so that we can receive 978 * commands from source now, and answer it if needed. The 979 * rest threads will be woken up afterwards until we are sure 980 * that source is ready to reply to page requests. 981 */ 982 qemu_sem_post(&mis->postcopy_pause_sem_dst); 983 return true; 984 } 985 986 return false; 987 } 988 989 void migration_fd_process_incoming(QEMUFile *f) 990 { 991 migration_incoming_setup(f); 992 if (postcopy_try_recover()) { 993 return; 994 } 995 migration_incoming_process(); 996 } 997 998 /* 999 * Returns true when we want to start a new incoming migration process, 1000 * false otherwise. 1001 */ 1002 static bool migration_should_start_incoming(bool main_channel) 1003 { 1004 /* Multifd doesn't start unless all channels are established */ 1005 if (migrate_multifd()) { 1006 return migration_has_all_channels(); 1007 } 1008 1009 /* Preempt channel only starts when the main channel is created */ 1010 if (migrate_postcopy_preempt()) { 1011 return main_channel; 1012 } 1013 1014 /* 1015 * For all the rest types of migration, we should only reach here when 1016 * it's the main channel that's being created, and we should always 1017 * proceed with this channel. 1018 */ 1019 assert(main_channel); 1020 return true; 1021 } 1022 1023 void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp) 1024 { 1025 MigrationIncomingState *mis = migration_incoming_get_current(); 1026 Error *local_err = NULL; 1027 QEMUFile *f; 1028 bool default_channel = true; 1029 uint32_t channel_magic = 0; 1030 int ret = 0; 1031 1032 if (migrate_multifd() && !migrate_mapped_ram() && 1033 !migrate_postcopy_ram() && 1034 qio_channel_has_feature(ioc, QIO_CHANNEL_FEATURE_READ_MSG_PEEK)) { 1035 /* 1036 * With multiple channels, it is possible that we receive channels 1037 * out of order on destination side, causing incorrect mapping of 1038 * source channels on destination side. Check channel MAGIC to 1039 * decide type of channel. Please note this is best effort, postcopy 1040 * preempt channel does not send any magic number so avoid it for 1041 * postcopy live migration. Also tls live migration already does 1042 * tls handshake while initializing main channel so with tls this 1043 * issue is not possible. 1044 */ 1045 ret = migration_channel_read_peek(ioc, (void *)&channel_magic, 1046 sizeof(channel_magic), errp); 1047 1048 if (ret != 0) { 1049 return; 1050 } 1051 1052 default_channel = (channel_magic == cpu_to_be32(QEMU_VM_FILE_MAGIC)); 1053 } else { 1054 default_channel = !mis->from_src_file; 1055 } 1056 1057 if (multifd_recv_setup(errp) != 0) { 1058 return; 1059 } 1060 1061 if (default_channel) { 1062 f = qemu_file_new_input(ioc); 1063 migration_incoming_setup(f); 1064 } else { 1065 /* Multiple connections */ 1066 assert(migration_needs_multiple_sockets()); 1067 if (migrate_multifd()) { 1068 multifd_recv_new_channel(ioc, &local_err); 1069 } else { 1070 assert(migrate_postcopy_preempt()); 1071 f = qemu_file_new_input(ioc); 1072 postcopy_preempt_new_channel(mis, f); 1073 } 1074 if (local_err) { 1075 error_propagate(errp, local_err); 1076 return; 1077 } 1078 } 1079 1080 if (migration_should_start_incoming(default_channel)) { 1081 /* If it's a recovery, we're done */ 1082 if (postcopy_try_recover()) { 1083 return; 1084 } 1085 migration_incoming_process(); 1086 } 1087 } 1088 1089 /** 1090 * @migration_has_all_channels: We have received all channels that we need 1091 * 1092 * Returns true when we have got connections to all the channels that 1093 * we need for migration. 1094 */ 1095 bool migration_has_all_channels(void) 1096 { 1097 MigrationIncomingState *mis = migration_incoming_get_current(); 1098 1099 if (!mis->from_src_file) { 1100 return false; 1101 } 1102 1103 if (migrate_multifd()) { 1104 return multifd_recv_all_channels_created(); 1105 } 1106 1107 if (migrate_postcopy_preempt()) { 1108 return mis->postcopy_qemufile_dst != NULL; 1109 } 1110 1111 return true; 1112 } 1113 1114 int migrate_send_rp_switchover_ack(MigrationIncomingState *mis) 1115 { 1116 return migrate_send_rp_message(mis, MIG_RP_MSG_SWITCHOVER_ACK, 0, NULL); 1117 } 1118 1119 /* 1120 * Send a 'SHUT' message on the return channel with the given value 1121 * to indicate that we've finished with the RP. Non-0 value indicates 1122 * error. 1123 */ 1124 void migrate_send_rp_shut(MigrationIncomingState *mis, 1125 uint32_t value) 1126 { 1127 uint32_t buf; 1128 1129 buf = cpu_to_be32(value); 1130 migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); 1131 } 1132 1133 /* 1134 * Send a 'PONG' message on the return channel with the given value 1135 * (normally in response to a 'PING') 1136 */ 1137 void migrate_send_rp_pong(MigrationIncomingState *mis, 1138 uint32_t value) 1139 { 1140 uint32_t buf; 1141 1142 buf = cpu_to_be32(value); 1143 migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); 1144 } 1145 1146 void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis, 1147 char *block_name) 1148 { 1149 char buf[512]; 1150 int len; 1151 int64_t res; 1152 1153 /* 1154 * First, we send the header part. It contains only the len of 1155 * idstr, and the idstr itself. 1156 */ 1157 len = strlen(block_name); 1158 buf[0] = len; 1159 memcpy(buf + 1, block_name, len); 1160 1161 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { 1162 error_report("%s: MSG_RP_RECV_BITMAP only used for recovery", 1163 __func__); 1164 return; 1165 } 1166 1167 migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf); 1168 1169 /* 1170 * Next, we dump the received bitmap to the stream. 1171 * 1172 * TODO: currently we are safe since we are the only one that is 1173 * using the to_src_file handle (fault thread is still paused), 1174 * and it's ok even not taking the mutex. However the best way is 1175 * to take the lock before sending the message header, and release 1176 * the lock after sending the bitmap. 1177 */ 1178 qemu_mutex_lock(&mis->rp_mutex); 1179 res = ramblock_recv_bitmap_send(mis->to_src_file, block_name); 1180 qemu_mutex_unlock(&mis->rp_mutex); 1181 1182 trace_migrate_send_rp_recv_bitmap(block_name, res); 1183 } 1184 1185 void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value) 1186 { 1187 uint32_t buf; 1188 1189 buf = cpu_to_be32(value); 1190 migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf); 1191 } 1192 1193 bool migration_is_running(void) 1194 { 1195 MigrationState *s = current_migration; 1196 1197 if (!s) { 1198 return false; 1199 } 1200 1201 switch (s->state) { 1202 case MIGRATION_STATUS_ACTIVE: 1203 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1204 case MIGRATION_STATUS_POSTCOPY_PAUSED: 1205 case MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP: 1206 case MIGRATION_STATUS_POSTCOPY_RECOVER: 1207 case MIGRATION_STATUS_SETUP: 1208 case MIGRATION_STATUS_PRE_SWITCHOVER: 1209 case MIGRATION_STATUS_DEVICE: 1210 case MIGRATION_STATUS_WAIT_UNPLUG: 1211 case MIGRATION_STATUS_CANCELLING: 1212 case MIGRATION_STATUS_COLO: 1213 return true; 1214 default: 1215 return false; 1216 } 1217 } 1218 1219 static bool migration_is_active(void) 1220 { 1221 MigrationState *s = current_migration; 1222 1223 return (s->state == MIGRATION_STATUS_ACTIVE || 1224 s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); 1225 } 1226 1227 static bool migrate_show_downtime(MigrationState *s) 1228 { 1229 return (s->state == MIGRATION_STATUS_COMPLETED) || migration_in_postcopy(); 1230 } 1231 1232 static void populate_time_info(MigrationInfo *info, MigrationState *s) 1233 { 1234 info->has_status = true; 1235 info->has_setup_time = true; 1236 info->setup_time = s->setup_time; 1237 1238 if (s->state == MIGRATION_STATUS_COMPLETED) { 1239 info->has_total_time = true; 1240 info->total_time = s->total_time; 1241 } else { 1242 info->has_total_time = true; 1243 info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - 1244 s->start_time; 1245 } 1246 1247 if (migrate_show_downtime(s)) { 1248 info->has_downtime = true; 1249 info->downtime = s->downtime; 1250 } else { 1251 info->has_expected_downtime = true; 1252 info->expected_downtime = s->expected_downtime; 1253 } 1254 } 1255 1256 static void populate_ram_info(MigrationInfo *info, MigrationState *s) 1257 { 1258 size_t page_size = qemu_target_page_size(); 1259 1260 info->ram = g_malloc0(sizeof(*info->ram)); 1261 info->ram->transferred = migration_transferred_bytes(); 1262 info->ram->total = ram_bytes_total(); 1263 info->ram->duplicate = stat64_get(&mig_stats.zero_pages); 1264 info->ram->normal = stat64_get(&mig_stats.normal_pages); 1265 info->ram->normal_bytes = info->ram->normal * page_size; 1266 info->ram->mbps = s->mbps; 1267 info->ram->dirty_sync_count = 1268 stat64_get(&mig_stats.dirty_sync_count); 1269 info->ram->dirty_sync_missed_zero_copy = 1270 stat64_get(&mig_stats.dirty_sync_missed_zero_copy); 1271 info->ram->postcopy_requests = 1272 stat64_get(&mig_stats.postcopy_requests); 1273 info->ram->page_size = page_size; 1274 info->ram->multifd_bytes = stat64_get(&mig_stats.multifd_bytes); 1275 info->ram->pages_per_second = s->pages_per_second; 1276 info->ram->precopy_bytes = stat64_get(&mig_stats.precopy_bytes); 1277 info->ram->downtime_bytes = stat64_get(&mig_stats.downtime_bytes); 1278 info->ram->postcopy_bytes = stat64_get(&mig_stats.postcopy_bytes); 1279 1280 if (migrate_xbzrle()) { 1281 info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); 1282 info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); 1283 info->xbzrle_cache->bytes = xbzrle_counters.bytes; 1284 info->xbzrle_cache->pages = xbzrle_counters.pages; 1285 info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss; 1286 info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate; 1287 info->xbzrle_cache->encoding_rate = xbzrle_counters.encoding_rate; 1288 info->xbzrle_cache->overflow = xbzrle_counters.overflow; 1289 } 1290 1291 if (cpu_throttle_active()) { 1292 info->has_cpu_throttle_percentage = true; 1293 info->cpu_throttle_percentage = cpu_throttle_get_percentage(); 1294 } 1295 1296 if (s->state != MIGRATION_STATUS_COMPLETED) { 1297 info->ram->remaining = ram_bytes_remaining(); 1298 info->ram->dirty_pages_rate = 1299 stat64_get(&mig_stats.dirty_pages_rate); 1300 } 1301 1302 if (migrate_dirty_limit() && dirtylimit_in_service()) { 1303 info->has_dirty_limit_throttle_time_per_round = true; 1304 info->dirty_limit_throttle_time_per_round = 1305 dirtylimit_throttle_time_per_round(); 1306 1307 info->has_dirty_limit_ring_full_time = true; 1308 info->dirty_limit_ring_full_time = dirtylimit_ring_full_time(); 1309 } 1310 } 1311 1312 static void fill_source_migration_info(MigrationInfo *info) 1313 { 1314 MigrationState *s = migrate_get_current(); 1315 int state = qatomic_read(&s->state); 1316 GSList *cur_blocker = migration_blockers[migrate_mode()]; 1317 1318 info->blocked_reasons = NULL; 1319 1320 /* 1321 * There are two types of reasons a migration might be blocked; 1322 * a) devices marked in VMState as non-migratable, and 1323 * b) Explicit migration blockers 1324 * We need to add both of them here. 1325 */ 1326 qemu_savevm_non_migratable_list(&info->blocked_reasons); 1327 1328 while (cur_blocker) { 1329 QAPI_LIST_PREPEND(info->blocked_reasons, 1330 g_strdup(error_get_pretty(cur_blocker->data))); 1331 cur_blocker = g_slist_next(cur_blocker); 1332 } 1333 info->has_blocked_reasons = info->blocked_reasons != NULL; 1334 1335 switch (state) { 1336 case MIGRATION_STATUS_NONE: 1337 /* no migration has happened ever */ 1338 /* do not overwrite destination migration status */ 1339 return; 1340 case MIGRATION_STATUS_SETUP: 1341 info->has_status = true; 1342 info->has_total_time = false; 1343 break; 1344 case MIGRATION_STATUS_ACTIVE: 1345 case MIGRATION_STATUS_CANCELLING: 1346 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1347 case MIGRATION_STATUS_PRE_SWITCHOVER: 1348 case MIGRATION_STATUS_DEVICE: 1349 case MIGRATION_STATUS_POSTCOPY_PAUSED: 1350 case MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP: 1351 case MIGRATION_STATUS_POSTCOPY_RECOVER: 1352 /* TODO add some postcopy stats */ 1353 populate_time_info(info, s); 1354 populate_ram_info(info, s); 1355 migration_populate_vfio_info(info); 1356 break; 1357 case MIGRATION_STATUS_COLO: 1358 info->has_status = true; 1359 /* TODO: display COLO specific information (checkpoint info etc.) */ 1360 break; 1361 case MIGRATION_STATUS_COMPLETED: 1362 populate_time_info(info, s); 1363 populate_ram_info(info, s); 1364 migration_populate_vfio_info(info); 1365 break; 1366 case MIGRATION_STATUS_FAILED: 1367 info->has_status = true; 1368 break; 1369 case MIGRATION_STATUS_CANCELLED: 1370 info->has_status = true; 1371 break; 1372 case MIGRATION_STATUS_WAIT_UNPLUG: 1373 info->has_status = true; 1374 break; 1375 } 1376 info->status = state; 1377 1378 QEMU_LOCK_GUARD(&s->error_mutex); 1379 if (s->error) { 1380 info->error_desc = g_strdup(error_get_pretty(s->error)); 1381 } 1382 } 1383 1384 static void fill_destination_migration_info(MigrationInfo *info) 1385 { 1386 MigrationIncomingState *mis = migration_incoming_get_current(); 1387 1388 if (mis->socket_address_list) { 1389 info->has_socket_address = true; 1390 info->socket_address = 1391 QAPI_CLONE(SocketAddressList, mis->socket_address_list); 1392 } 1393 1394 switch (mis->state) { 1395 case MIGRATION_STATUS_SETUP: 1396 case MIGRATION_STATUS_CANCELLING: 1397 case MIGRATION_STATUS_CANCELLED: 1398 case MIGRATION_STATUS_ACTIVE: 1399 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1400 case MIGRATION_STATUS_POSTCOPY_PAUSED: 1401 case MIGRATION_STATUS_POSTCOPY_RECOVER: 1402 case MIGRATION_STATUS_FAILED: 1403 case MIGRATION_STATUS_COLO: 1404 info->has_status = true; 1405 break; 1406 case MIGRATION_STATUS_COMPLETED: 1407 info->has_status = true; 1408 fill_destination_postcopy_migration_info(info); 1409 break; 1410 default: 1411 return; 1412 } 1413 info->status = mis->state; 1414 1415 if (!info->error_desc) { 1416 MigrationState *s = migrate_get_current(); 1417 QEMU_LOCK_GUARD(&s->error_mutex); 1418 1419 if (s->error) { 1420 info->error_desc = g_strdup(error_get_pretty(s->error)); 1421 } 1422 } 1423 } 1424 1425 MigrationInfo *qmp_query_migrate(Error **errp) 1426 { 1427 MigrationInfo *info = g_malloc0(sizeof(*info)); 1428 1429 fill_destination_migration_info(info); 1430 fill_source_migration_info(info); 1431 1432 return info; 1433 } 1434 1435 void qmp_migrate_start_postcopy(Error **errp) 1436 { 1437 MigrationState *s = migrate_get_current(); 1438 1439 if (!migrate_postcopy()) { 1440 error_setg(errp, "Enable postcopy with migrate_set_capability before" 1441 " the start of migration"); 1442 return; 1443 } 1444 1445 if (s->state == MIGRATION_STATUS_NONE) { 1446 error_setg(errp, "Postcopy must be started after migration has been" 1447 " started"); 1448 return; 1449 } 1450 /* 1451 * we don't error if migration has finished since that would be racy 1452 * with issuing this command. 1453 */ 1454 qatomic_set(&s->start_postcopy, true); 1455 } 1456 1457 /* shared migration helpers */ 1458 1459 void migrate_set_state(MigrationStatus *state, MigrationStatus old_state, 1460 MigrationStatus new_state) 1461 { 1462 assert(new_state < MIGRATION_STATUS__MAX); 1463 if (qatomic_cmpxchg(state, old_state, new_state) == old_state) { 1464 trace_migrate_set_state(MigrationStatus_str(new_state)); 1465 migrate_generate_event(new_state); 1466 } 1467 } 1468 1469 static void migration_cleanup_json_writer(MigrationState *s) 1470 { 1471 g_clear_pointer(&s->vmdesc, json_writer_free); 1472 } 1473 1474 static void migration_cleanup(MigrationState *s) 1475 { 1476 MigrationEventType type; 1477 QEMUFile *tmp = NULL; 1478 1479 trace_migration_cleanup(); 1480 1481 migration_cleanup_json_writer(s); 1482 1483 g_free(s->hostname); 1484 s->hostname = NULL; 1485 1486 qemu_savevm_state_cleanup(); 1487 cpr_state_close(); 1488 migrate_hup_delete(s); 1489 1490 close_return_path_on_source(s); 1491 1492 if (s->migration_thread_running) { 1493 bql_unlock(); 1494 qemu_thread_join(&s->thread); 1495 s->migration_thread_running = false; 1496 bql_lock(); 1497 } 1498 1499 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { 1500 /* 1501 * Close the file handle without the lock to make sure the critical 1502 * section won't block for long. 1503 */ 1504 tmp = s->to_dst_file; 1505 s->to_dst_file = NULL; 1506 } 1507 1508 if (tmp) { 1509 /* 1510 * We only need to shutdown multifd if tmp!=NULL, because if 1511 * tmp==NULL, it means the main channel isn't established, while 1512 * multifd is only setup after that (in migration_thread()). 1513 */ 1514 multifd_send_shutdown(); 1515 migration_ioc_unregister_yank_from_file(tmp); 1516 qemu_fclose(tmp); 1517 } 1518 1519 assert(!migration_is_active()); 1520 1521 if (s->state == MIGRATION_STATUS_CANCELLING) { 1522 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, 1523 MIGRATION_STATUS_CANCELLED); 1524 } 1525 1526 if (s->error) { 1527 /* It is used on info migrate. We can't free it */ 1528 error_report_err(error_copy(s->error)); 1529 } 1530 type = migration_has_failed(s) ? MIG_EVENT_PRECOPY_FAILED : 1531 MIG_EVENT_PRECOPY_DONE; 1532 migration_call_notifiers(s, type, NULL); 1533 yank_unregister_instance(MIGRATION_YANK_INSTANCE); 1534 } 1535 1536 static void migration_cleanup_bh(void *opaque) 1537 { 1538 migration_cleanup(opaque); 1539 } 1540 1541 void migrate_set_error(MigrationState *s, const Error *error) 1542 { 1543 QEMU_LOCK_GUARD(&s->error_mutex); 1544 1545 trace_migrate_error(error_get_pretty(error)); 1546 1547 if (!s->error) { 1548 s->error = error_copy(error); 1549 } 1550 } 1551 1552 bool migrate_has_error(MigrationState *s) 1553 { 1554 /* The lock is not helpful here, but still follow the rule */ 1555 QEMU_LOCK_GUARD(&s->error_mutex); 1556 return qatomic_read(&s->error); 1557 } 1558 1559 static void migrate_error_free(MigrationState *s) 1560 { 1561 QEMU_LOCK_GUARD(&s->error_mutex); 1562 if (s->error) { 1563 error_free(s->error); 1564 s->error = NULL; 1565 } 1566 } 1567 1568 static void migration_connect_set_error(MigrationState *s, const Error *error) 1569 { 1570 MigrationStatus current = s->state; 1571 MigrationStatus next; 1572 1573 assert(s->to_dst_file == NULL); 1574 1575 switch (current) { 1576 case MIGRATION_STATUS_SETUP: 1577 next = MIGRATION_STATUS_FAILED; 1578 break; 1579 case MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP: 1580 /* Never fail a postcopy migration; switch back to PAUSED instead */ 1581 next = MIGRATION_STATUS_POSTCOPY_PAUSED; 1582 break; 1583 default: 1584 /* 1585 * This really shouldn't happen. Just be careful to not crash a VM 1586 * just for this. Instead, dump something. 1587 */ 1588 error_report("%s: Illegal migration status (%s) detected", 1589 __func__, MigrationStatus_str(current)); 1590 return; 1591 } 1592 1593 migrate_set_state(&s->state, current, next); 1594 migrate_set_error(s, error); 1595 } 1596 1597 void migration_cancel(void) 1598 { 1599 MigrationState *s = migrate_get_current(); 1600 int old_state ; 1601 bool setup = (s->state == MIGRATION_STATUS_SETUP); 1602 1603 trace_migration_cancel(); 1604 1605 if (migrate_dirty_limit()) { 1606 qmp_cancel_vcpu_dirty_limit(false, -1, NULL); 1607 } 1608 1609 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { 1610 if (s->rp_state.from_dst_file) { 1611 /* shutdown the rp socket, so causing the rp thread to shutdown */ 1612 qemu_file_shutdown(s->rp_state.from_dst_file); 1613 } 1614 } 1615 1616 do { 1617 old_state = s->state; 1618 if (!migration_is_running()) { 1619 break; 1620 } 1621 /* If the migration is paused, kick it out of the pause */ 1622 if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) { 1623 qemu_sem_post(&s->pause_sem); 1624 } 1625 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING); 1626 } while (s->state != MIGRATION_STATUS_CANCELLING); 1627 1628 /* 1629 * If we're unlucky the migration code might be stuck somewhere in a 1630 * send/write while the network has failed and is waiting to timeout; 1631 * if we've got shutdown(2) available then we can force it to quit. 1632 */ 1633 if (s->state == MIGRATION_STATUS_CANCELLING) { 1634 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { 1635 if (s->to_dst_file) { 1636 qemu_file_shutdown(s->to_dst_file); 1637 } 1638 } 1639 } 1640 1641 /* 1642 * If qmp_migrate_finish has not been called, then there is no path that 1643 * will complete the cancellation. Do it now. 1644 */ 1645 if (setup && !s->to_dst_file) { 1646 migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, 1647 MIGRATION_STATUS_CANCELLED); 1648 cpr_state_close(); 1649 migrate_hup_delete(s); 1650 } 1651 } 1652 1653 void migration_add_notifier_mode(NotifierWithReturn *notify, 1654 MigrationNotifyFunc func, MigMode mode) 1655 { 1656 notify->notify = (NotifierWithReturnFunc)func; 1657 notifier_with_return_list_add(&migration_state_notifiers[mode], notify); 1658 } 1659 1660 void migration_add_notifier(NotifierWithReturn *notify, 1661 MigrationNotifyFunc func) 1662 { 1663 migration_add_notifier_mode(notify, func, MIG_MODE_NORMAL); 1664 } 1665 1666 void migration_remove_notifier(NotifierWithReturn *notify) 1667 { 1668 if (notify->notify) { 1669 notifier_with_return_remove(notify); 1670 notify->notify = NULL; 1671 } 1672 } 1673 1674 int migration_call_notifiers(MigrationState *s, MigrationEventType type, 1675 Error **errp) 1676 { 1677 MigMode mode = s->parameters.mode; 1678 MigrationEvent e; 1679 int ret; 1680 1681 e.type = type; 1682 ret = notifier_with_return_list_notify(&migration_state_notifiers[mode], 1683 &e, errp); 1684 assert(!ret || type == MIG_EVENT_PRECOPY_SETUP); 1685 return ret; 1686 } 1687 1688 bool migration_has_failed(MigrationState *s) 1689 { 1690 return (s->state == MIGRATION_STATUS_CANCELLED || 1691 s->state == MIGRATION_STATUS_FAILED); 1692 } 1693 1694 bool migration_in_postcopy(void) 1695 { 1696 MigrationState *s = migrate_get_current(); 1697 1698 switch (s->state) { 1699 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1700 case MIGRATION_STATUS_POSTCOPY_PAUSED: 1701 case MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP: 1702 case MIGRATION_STATUS_POSTCOPY_RECOVER: 1703 return true; 1704 default: 1705 return false; 1706 } 1707 } 1708 1709 bool migration_postcopy_is_alive(MigrationStatus state) 1710 { 1711 switch (state) { 1712 case MIGRATION_STATUS_POSTCOPY_ACTIVE: 1713 case MIGRATION_STATUS_POSTCOPY_RECOVER: 1714 return true; 1715 default: 1716 return false; 1717 } 1718 } 1719 1720 bool migration_in_incoming_postcopy(void) 1721 { 1722 PostcopyState ps = postcopy_state_get(); 1723 1724 return ps >= POSTCOPY_INCOMING_DISCARD && ps < POSTCOPY_INCOMING_END; 1725 } 1726 1727 bool migration_incoming_postcopy_advised(void) 1728 { 1729 PostcopyState ps = postcopy_state_get(); 1730 1731 return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END; 1732 } 1733 1734 bool migration_in_bg_snapshot(void) 1735 { 1736 return migrate_background_snapshot() && migration_is_running(); 1737 } 1738 1739 bool migration_thread_is_self(void) 1740 { 1741 MigrationState *s = current_migration; 1742 1743 return qemu_thread_is_self(&s->thread); 1744 } 1745 1746 bool migrate_mode_is_cpr(MigrationState *s) 1747 { 1748 MigMode mode = s->parameters.mode; 1749 return mode == MIG_MODE_CPR_REBOOT || 1750 mode == MIG_MODE_CPR_TRANSFER; 1751 } 1752 1753 int migrate_init(MigrationState *s, Error **errp) 1754 { 1755 int ret; 1756 1757 ret = qemu_savevm_state_prepare(errp); 1758 if (ret) { 1759 return ret; 1760 } 1761 1762 /* 1763 * Reinitialise all migration state, except 1764 * parameters/capabilities that the user set, and 1765 * locks. 1766 */ 1767 s->to_dst_file = NULL; 1768 s->state = MIGRATION_STATUS_NONE; 1769 s->rp_state.from_dst_file = NULL; 1770 s->mbps = 0.0; 1771 s->pages_per_second = 0.0; 1772 s->downtime = 0; 1773 s->expected_downtime = 0; 1774 s->setup_time = 0; 1775 s->start_postcopy = false; 1776 s->migration_thread_running = false; 1777 error_free(s->error); 1778 s->error = NULL; 1779 1780 if (should_send_vmdesc()) { 1781 s->vmdesc = json_writer_new(false); 1782 } 1783 1784 migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); 1785 1786 s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 1787 s->total_time = 0; 1788 s->vm_old_state = -1; 1789 s->iteration_initial_bytes = 0; 1790 s->threshold_size = 0; 1791 s->switchover_acked = false; 1792 s->rdma_migration = false; 1793 /* 1794 * set mig_stats memory to zero for a new migration 1795 */ 1796 memset(&mig_stats, 0, sizeof(mig_stats)); 1797 migration_reset_vfio_bytes_transferred(); 1798 1799 return 0; 1800 } 1801 1802 static bool is_busy(Error **reasonp, Error **errp) 1803 { 1804 ERRP_GUARD(); 1805 1806 /* Snapshots are similar to migrations, so check RUN_STATE_SAVE_VM too. */ 1807 if (runstate_check(RUN_STATE_SAVE_VM) || migration_is_running()) { 1808 error_propagate_prepend(errp, *reasonp, 1809 "disallowing migration blocker " 1810 "(migration/snapshot in progress) for: "); 1811 *reasonp = NULL; 1812 return true; 1813 } 1814 return false; 1815 } 1816 1817 static bool is_only_migratable(Error **reasonp, Error **errp, int modes) 1818 { 1819 ERRP_GUARD(); 1820 1821 if (only_migratable && (modes & BIT(MIG_MODE_NORMAL))) { 1822 error_propagate_prepend(errp, *reasonp, 1823 "disallowing migration blocker " 1824 "(--only-migratable) for: "); 1825 *reasonp = NULL; 1826 return true; 1827 } 1828 return false; 1829 } 1830 1831 static int get_modes(MigMode mode, va_list ap) 1832 { 1833 int modes = 0; 1834 1835 while (mode != -1 && mode != MIG_MODE_ALL) { 1836 assert(mode >= MIG_MODE_NORMAL && mode < MIG_MODE__MAX); 1837 modes |= BIT(mode); 1838 mode = va_arg(ap, MigMode); 1839 } 1840 if (mode == MIG_MODE_ALL) { 1841 modes = BIT(MIG_MODE__MAX) - 1; 1842 } 1843 return modes; 1844 } 1845 1846 static int add_blockers(Error **reasonp, Error **errp, int modes) 1847 { 1848 for (MigMode mode = 0; mode < MIG_MODE__MAX; mode++) { 1849 if (modes & BIT(mode)) { 1850 migration_blockers[mode] = g_slist_prepend(migration_blockers[mode], 1851 *reasonp); 1852 } 1853 } 1854 return 0; 1855 } 1856 1857 int migrate_add_blocker(Error **reasonp, Error **errp) 1858 { 1859 return migrate_add_blocker_modes(reasonp, errp, MIG_MODE_ALL); 1860 } 1861 1862 int migrate_add_blocker_normal(Error **reasonp, Error **errp) 1863 { 1864 return migrate_add_blocker_modes(reasonp, errp, MIG_MODE_NORMAL, -1); 1865 } 1866 1867 int migrate_add_blocker_modes(Error **reasonp, Error **errp, MigMode mode, ...) 1868 { 1869 int modes; 1870 va_list ap; 1871 1872 va_start(ap, mode); 1873 modes = get_modes(mode, ap); 1874 va_end(ap); 1875 1876 if (is_only_migratable(reasonp, errp, modes)) { 1877 return -EACCES; 1878 } else if (is_busy(reasonp, errp)) { 1879 return -EBUSY; 1880 } 1881 return add_blockers(reasonp, errp, modes); 1882 } 1883 1884 int migrate_add_blocker_internal(Error **reasonp, Error **errp) 1885 { 1886 int modes = BIT(MIG_MODE__MAX) - 1; 1887 1888 if (is_busy(reasonp, errp)) { 1889 return -EBUSY; 1890 } 1891 return add_blockers(reasonp, errp, modes); 1892 } 1893 1894 void migrate_del_blocker(Error **reasonp) 1895 { 1896 if (*reasonp) { 1897 for (MigMode mode = 0; mode < MIG_MODE__MAX; mode++) { 1898 migration_blockers[mode] = g_slist_remove(migration_blockers[mode], 1899 *reasonp); 1900 } 1901 error_free(*reasonp); 1902 *reasonp = NULL; 1903 } 1904 } 1905 1906 void qmp_migrate_incoming(const char *uri, bool has_channels, 1907 MigrationChannelList *channels, 1908 bool has_exit_on_error, bool exit_on_error, 1909 Error **errp) 1910 { 1911 Error *local_err = NULL; 1912 static bool once = true; 1913 MigrationIncomingState *mis = migration_incoming_get_current(); 1914 1915 if (!once) { 1916 error_setg(errp, "The incoming migration has already been started"); 1917 return; 1918 } 1919 if (!runstate_check(RUN_STATE_INMIGRATE)) { 1920 error_setg(errp, "'-incoming' was not specified on the command line"); 1921 return; 1922 } 1923 1924 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) { 1925 return; 1926 } 1927 1928 mis->exit_on_error = 1929 has_exit_on_error ? exit_on_error : INMIGRATE_DEFAULT_EXIT_ON_ERROR; 1930 1931 qemu_start_incoming_migration(uri, has_channels, channels, &local_err); 1932 1933 if (local_err) { 1934 yank_unregister_instance(MIGRATION_YANK_INSTANCE); 1935 error_propagate(errp, local_err); 1936 return; 1937 } 1938 1939 /* 1940 * Making sure MigrationState is available until incoming migration 1941 * completes. 1942 * 1943 * NOTE: QEMU _might_ leak this refcount in some failure paths, but 1944 * that's OK. This is the minimum change we need to at least making 1945 * sure success case is clean on the refcount. We can try harder to 1946 * make it accurate for any kind of failures, but it might be an 1947 * overkill and doesn't bring us much benefit. 1948 */ 1949 migrate_incoming_ref_outgoing_state(); 1950 once = false; 1951 } 1952 1953 void qmp_migrate_recover(const char *uri, Error **errp) 1954 { 1955 MigrationIncomingState *mis = migration_incoming_get_current(); 1956 1957 /* 1958 * Don't even bother to use ERRP_GUARD() as it _must_ always be set by 1959 * callers (no one should ignore a recover failure); if there is, it's a 1960 * programming error. 1961 */ 1962 assert(errp); 1963 1964 if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { 1965 error_setg(errp, "Migrate recover can only be run " 1966 "when postcopy is paused."); 1967 return; 1968 } 1969 1970 /* If there's an existing transport, release it */ 1971 migration_incoming_transport_cleanup(mis); 1972 1973 /* 1974 * Note that this call will never start a real migration; it will 1975 * only re-setup the migration stream and poke existing migration 1976 * to continue using that newly established channel. 1977 */ 1978 qemu_start_incoming_migration(uri, false, NULL, errp); 1979 } 1980 1981 void qmp_migrate_pause(Error **errp) 1982 { 1983 MigrationState *ms = migrate_get_current(); 1984 MigrationIncomingState *mis = migration_incoming_get_current(); 1985 int ret = 0; 1986 1987 if (migration_postcopy_is_alive(ms->state)) { 1988 /* Source side, during postcopy */ 1989 Error *error = NULL; 1990 1991 /* Tell the core migration that we're pausing */ 1992 error_setg(&error, "Postcopy migration is paused by the user"); 1993 migrate_set_error(ms, error); 1994 error_free(error); 1995 1996 qemu_mutex_lock(&ms->qemu_file_lock); 1997 if (ms->to_dst_file) { 1998 ret = qemu_file_shutdown(ms->to_dst_file); 1999 } 2000 qemu_mutex_unlock(&ms->qemu_file_lock); 2001 if (ret) { 2002 error_setg(errp, "Failed to pause source migration"); 2003 } 2004 2005 /* 2006 * Kick the migration thread out of any waiting windows (on behalf 2007 * of the rp thread). 2008 */ 2009 migration_rp_kick(ms); 2010 2011 return; 2012 } 2013 2014 if (migration_postcopy_is_alive(mis->state)) { 2015 ret = qemu_file_shutdown(mis->from_src_file); 2016 if (ret) { 2017 error_setg(errp, "Failed to pause destination migration"); 2018 } 2019 return; 2020 } 2021 2022 error_setg(errp, "migrate-pause is currently only supported " 2023 "during postcopy-active or postcopy-recover state"); 2024 } 2025 2026 bool migration_is_blocked(Error **errp) 2027 { 2028 GSList *blockers = migration_blockers[migrate_mode()]; 2029 2030 if (qemu_savevm_state_blocked(errp)) { 2031 return true; 2032 } 2033 2034 if (blockers) { 2035 error_propagate(errp, error_copy(blockers->data)); 2036 return true; 2037 } 2038 2039 return false; 2040 } 2041 2042 /* Returns true if continue to migrate, or false if error detected */ 2043 static bool migrate_prepare(MigrationState *s, bool resume, Error **errp) 2044 { 2045 if (resume) { 2046 if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { 2047 error_setg(errp, "Cannot resume if there is no " 2048 "paused migration"); 2049 return false; 2050 } 2051 2052 /* 2053 * Postcopy recovery won't work well with release-ram 2054 * capability since release-ram will drop the page buffer as 2055 * long as the page is put into the send buffer. So if there 2056 * is a network failure happened, any page buffers that have 2057 * not yet reached the destination VM but have already been 2058 * sent from the source VM will be lost forever. Let's refuse 2059 * the client from resuming such a postcopy migration. 2060 * Luckily release-ram was designed to only be used when src 2061 * and destination VMs are on the same host, so it should be 2062 * fine. 2063 */ 2064 if (migrate_release_ram()) { 2065 error_setg(errp, "Postcopy recovery cannot work " 2066 "when release-ram capability is set"); 2067 return false; 2068 } 2069 2070 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED, 2071 MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP); 2072 2073 /* This is a resume, skip init status */ 2074 return true; 2075 } 2076 2077 if (migration_is_running()) { 2078 error_setg(errp, "There's a migration process in progress"); 2079 return false; 2080 } 2081 2082 if (runstate_check(RUN_STATE_INMIGRATE)) { 2083 error_setg(errp, "Guest is waiting for an incoming migration"); 2084 return false; 2085 } 2086 2087 if (runstate_check(RUN_STATE_POSTMIGRATE)) { 2088 error_setg(errp, "Can't migrate the vm that was paused due to " 2089 "previous migration"); 2090 return false; 2091 } 2092 2093 if (kvm_hwpoisoned_mem()) { 2094 error_setg(errp, "Can't migrate this vm with hardware poisoned memory, " 2095 "please reboot the vm and try again"); 2096 return false; 2097 } 2098 2099 if (migration_is_blocked(errp)) { 2100 return false; 2101 } 2102 2103 if (migrate_mapped_ram()) { 2104 if (migrate_tls()) { 2105 error_setg(errp, "Cannot use TLS with mapped-ram"); 2106 return false; 2107 } 2108 2109 if (migrate_multifd_compression()) { 2110 error_setg(errp, "Cannot use compression with mapped-ram"); 2111 return false; 2112 } 2113 } 2114 2115 if (migrate_mode_is_cpr(s)) { 2116 const char *conflict = NULL; 2117 2118 if (migrate_postcopy()) { 2119 conflict = "postcopy"; 2120 } else if (migrate_background_snapshot()) { 2121 conflict = "background snapshot"; 2122 } else if (migrate_colo()) { 2123 conflict = "COLO"; 2124 } 2125 2126 if (conflict) { 2127 error_setg(errp, "Cannot use %s with CPR", conflict); 2128 return false; 2129 } 2130 } 2131 2132 if (migrate_init(s, errp)) { 2133 return false; 2134 } 2135 2136 return true; 2137 } 2138 2139 static void qmp_migrate_finish(MigrationAddress *addr, bool resume_requested, 2140 Error **errp); 2141 2142 static void migrate_hup_add(MigrationState *s, QIOChannel *ioc, GSourceFunc cb, 2143 void *opaque) 2144 { 2145 s->hup_source = qio_channel_create_watch(ioc, G_IO_HUP); 2146 g_source_set_callback(s->hup_source, cb, opaque, NULL); 2147 g_source_attach(s->hup_source, NULL); 2148 } 2149 2150 static void migrate_hup_delete(MigrationState *s) 2151 { 2152 if (s->hup_source) { 2153 g_source_destroy(s->hup_source); 2154 g_source_unref(s->hup_source); 2155 s->hup_source = NULL; 2156 } 2157 } 2158 2159 static gboolean qmp_migrate_finish_cb(QIOChannel *channel, 2160 GIOCondition cond, 2161 void *opaque) 2162 { 2163 MigrationAddress *addr = opaque; 2164 2165 qmp_migrate_finish(addr, false, NULL); 2166 2167 cpr_state_close(); 2168 migrate_hup_delete(migrate_get_current()); 2169 qapi_free_MigrationAddress(addr); 2170 return G_SOURCE_REMOVE; 2171 } 2172 2173 void qmp_migrate(const char *uri, bool has_channels, 2174 MigrationChannelList *channels, bool has_detach, bool detach, 2175 bool has_resume, bool resume, Error **errp) 2176 { 2177 bool resume_requested; 2178 Error *local_err = NULL; 2179 MigrationState *s = migrate_get_current(); 2180 g_autoptr(MigrationChannel) channel = NULL; 2181 MigrationAddress *addr = NULL; 2182 MigrationChannel *channelv[MIGRATION_CHANNEL_TYPE__MAX] = { NULL }; 2183 MigrationChannel *cpr_channel = NULL; 2184 2185 /* 2186 * Having preliminary checks for uri and channel 2187 */ 2188 if (!uri == !channels) { 2189 error_setg(errp, "need either 'uri' or 'channels' argument"); 2190 return; 2191 } 2192 2193 if (channels) { 2194 for ( ; channels; channels = channels->next) { 2195 MigrationChannelType type = channels->value->channel_type; 2196 2197 if (channelv[type]) { 2198 error_setg(errp, "Channel list has more than one %s entry", 2199 MigrationChannelType_str(type)); 2200 return; 2201 } 2202 channelv[type] = channels->value; 2203 } 2204 cpr_channel = channelv[MIGRATION_CHANNEL_TYPE_CPR]; 2205 addr = channelv[MIGRATION_CHANNEL_TYPE_MAIN]->addr; 2206 if (!addr) { 2207 error_setg(errp, "Channel list has no main entry"); 2208 return; 2209 } 2210 } 2211 2212 if (uri) { 2213 /* caller uses the old URI syntax */ 2214 if (!migrate_uri_parse(uri, &channel, errp)) { 2215 return; 2216 } 2217 addr = channel->addr; 2218 } 2219 2220 /* transport mechanism not suitable for migration? */ 2221 if (!migration_transport_compatible(addr, errp)) { 2222 return; 2223 } 2224 2225 if (s->parameters.mode == MIG_MODE_CPR_TRANSFER && !cpr_channel) { 2226 error_setg(errp, "missing 'cpr' migration channel"); 2227 return; 2228 } 2229 2230 resume_requested = has_resume && resume; 2231 if (!migrate_prepare(s, resume_requested, errp)) { 2232 /* Error detected, put into errp */ 2233 return; 2234 } 2235 2236 if (cpr_state_save(cpr_channel, &local_err)) { 2237 goto out; 2238 } 2239 2240 /* 2241 * For cpr-transfer, the target may not be listening yet on the migration 2242 * channel, because first it must finish cpr_load_state. The target tells 2243 * us it is listening by closing the cpr-state socket. Wait for that HUP 2244 * event before connecting in qmp_migrate_finish. 2245 * 2246 * The HUP could occur because the target fails while reading CPR state, 2247 * in which case the target will not listen for the incoming migration 2248 * connection, so qmp_migrate_finish will fail to connect, and then recover. 2249 */ 2250 if (s->parameters.mode == MIG_MODE_CPR_TRANSFER) { 2251 migrate_hup_add(s, cpr_state_ioc(), (GSourceFunc)qmp_migrate_finish_cb, 2252 QAPI_CLONE(MigrationAddress, addr)); 2253 2254 } else { 2255 qmp_migrate_finish(addr, resume_requested, errp); 2256 } 2257 2258 out: 2259 if (local_err) { 2260 migration_connect_set_error(s, local_err); 2261 error_propagate(errp, local_err); 2262 } 2263 } 2264 2265 static void qmp_migrate_finish(MigrationAddress *addr, bool resume_requested, 2266 Error **errp) 2267 { 2268 MigrationState *s = migrate_get_current(); 2269 Error *local_err = NULL; 2270 2271 if (!resume_requested) { 2272 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) { 2273 return; 2274 } 2275 } 2276 2277 if (addr->transport == MIGRATION_ADDRESS_TYPE_SOCKET) { 2278 SocketAddress *saddr = &addr->u.socket; 2279 if (saddr->type == SOCKET_ADDRESS_TYPE_INET || 2280 saddr->type == SOCKET_ADDRESS_TYPE_UNIX || 2281 saddr->type == SOCKET_ADDRESS_TYPE_VSOCK) { 2282 socket_start_outgoing_migration(s, saddr, &local_err); 2283 } else if (saddr->type == SOCKET_ADDRESS_TYPE_FD) { 2284 fd_start_outgoing_migration(s, saddr->u.fd.str, &local_err); 2285 } 2286 #ifdef CONFIG_RDMA 2287 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_RDMA) { 2288 rdma_start_outgoing_migration(s, &addr->u.rdma, &local_err); 2289 #endif 2290 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_EXEC) { 2291 exec_start_outgoing_migration(s, addr->u.exec.args, &local_err); 2292 } else if (addr->transport == MIGRATION_ADDRESS_TYPE_FILE) { 2293 file_start_outgoing_migration(s, &addr->u.file, &local_err); 2294 } else { 2295 error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, "uri", 2296 "a valid migration protocol"); 2297 migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, 2298 MIGRATION_STATUS_FAILED); 2299 } 2300 2301 if (local_err) { 2302 if (!resume_requested) { 2303 yank_unregister_instance(MIGRATION_YANK_INSTANCE); 2304 } 2305 migration_connect_set_error(s, local_err); 2306 error_propagate(errp, local_err); 2307 return; 2308 } 2309 } 2310 2311 void qmp_migrate_cancel(Error **errp) 2312 { 2313 /* 2314 * After postcopy migration has started, the source machine is not 2315 * recoverable in case of a migration error. This also means the 2316 * cancel command cannot be used as cancel should allow the 2317 * machine to continue operation. 2318 */ 2319 if (migration_in_postcopy()) { 2320 error_setg(errp, "Postcopy migration in progress, cannot cancel."); 2321 return; 2322 } 2323 2324 migration_cancel(); 2325 } 2326 2327 void qmp_migrate_continue(MigrationStatus state, Error **errp) 2328 { 2329 MigrationState *s = migrate_get_current(); 2330 if (s->state != state) { 2331 error_setg(errp, "Migration not in expected state: %s", 2332 MigrationStatus_str(s->state)); 2333 return; 2334 } 2335 qemu_sem_post(&s->pause_sem); 2336 } 2337 2338 int migration_rp_wait(MigrationState *s) 2339 { 2340 /* If migration has failure already, ignore the wait */ 2341 if (migrate_has_error(s)) { 2342 return -1; 2343 } 2344 2345 qemu_sem_wait(&s->rp_state.rp_sem); 2346 2347 /* After wait, double check that there's no failure */ 2348 if (migrate_has_error(s)) { 2349 return -1; 2350 } 2351 2352 return 0; 2353 } 2354 2355 void migration_rp_kick(MigrationState *s) 2356 { 2357 qemu_sem_post(&s->rp_state.rp_sem); 2358 } 2359 2360 static struct rp_cmd_args { 2361 ssize_t len; /* -1 = variable */ 2362 const char *name; 2363 } rp_cmd_args[] = { 2364 [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, 2365 [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, 2366 [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, 2367 [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" }, 2368 [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" }, 2369 [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, 2370 [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" }, 2371 [MIG_RP_MSG_SWITCHOVER_ACK] = { .len = 0, .name = "SWITCHOVER_ACK" }, 2372 [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, 2373 }; 2374 2375 /* 2376 * Process a request for pages received on the return path, 2377 * We're allowed to send more than requested (e.g. to round to our page size) 2378 * and we don't need to send pages that have already been sent. 2379 */ 2380 static void 2381 migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, 2382 ram_addr_t start, size_t len, Error **errp) 2383 { 2384 long our_host_ps = qemu_real_host_page_size(); 2385 2386 trace_migrate_handle_rp_req_pages(rbname, start, len); 2387 2388 /* 2389 * Since we currently insist on matching page sizes, just sanity check 2390 * we're being asked for whole host pages. 2391 */ 2392 if (!QEMU_IS_ALIGNED(start, our_host_ps) || 2393 !QEMU_IS_ALIGNED(len, our_host_ps)) { 2394 error_setg(errp, "MIG_RP_MSG_REQ_PAGES: Misaligned page request, start:" 2395 RAM_ADDR_FMT " len: %zd", start, len); 2396 return; 2397 } 2398 2399 ram_save_queue_pages(rbname, start, len, errp); 2400 } 2401 2402 static bool migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name, 2403 Error **errp) 2404 { 2405 RAMBlock *block = qemu_ram_block_by_name(block_name); 2406 2407 if (!block) { 2408 error_setg(errp, "MIG_RP_MSG_RECV_BITMAP has invalid block name '%s'", 2409 block_name); 2410 return false; 2411 } 2412 2413 /* Fetch the received bitmap and refresh the dirty bitmap */ 2414 return ram_dirty_bitmap_reload(s, block, errp); 2415 } 2416 2417 static bool migrate_handle_rp_resume_ack(MigrationState *s, 2418 uint32_t value, Error **errp) 2419 { 2420 trace_source_return_path_thread_resume_ack(value); 2421 2422 if (value != MIGRATION_RESUME_ACK_VALUE) { 2423 error_setg(errp, "illegal resume_ack value %"PRIu32, value); 2424 return false; 2425 } 2426 2427 /* Now both sides are active. */ 2428 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER, 2429 MIGRATION_STATUS_POSTCOPY_ACTIVE); 2430 2431 /* Notify send thread that time to continue send pages */ 2432 migration_rp_kick(s); 2433 2434 return true; 2435 } 2436 2437 /* 2438 * Release ms->rp_state.from_dst_file (and postcopy_qemufile_src if 2439 * existed) in a safe way. 2440 */ 2441 static void migration_release_dst_files(MigrationState *ms) 2442 { 2443 QEMUFile *file = NULL; 2444 2445 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) { 2446 /* 2447 * Reset the from_dst_file pointer first before releasing it, as we 2448 * can't block within lock section 2449 */ 2450 file = ms->rp_state.from_dst_file; 2451 ms->rp_state.from_dst_file = NULL; 2452 } 2453 2454 /* 2455 * Do the same to postcopy fast path socket too if there is. No 2456 * locking needed because this qemufile should only be managed by 2457 * return path thread. 2458 */ 2459 if (ms->postcopy_qemufile_src) { 2460 migration_ioc_unregister_yank_from_file(ms->postcopy_qemufile_src); 2461 qemu_file_shutdown(ms->postcopy_qemufile_src); 2462 qemu_fclose(ms->postcopy_qemufile_src); 2463 ms->postcopy_qemufile_src = NULL; 2464 } 2465 2466 qemu_fclose(file); 2467 } 2468 2469 /* 2470 * Handles messages sent on the return path towards the source VM 2471 * 2472 */ 2473 static void *source_return_path_thread(void *opaque) 2474 { 2475 MigrationState *ms = opaque; 2476 QEMUFile *rp = ms->rp_state.from_dst_file; 2477 uint16_t header_len, header_type; 2478 uint8_t buf[512]; 2479 uint32_t tmp32, sibling_error; 2480 ram_addr_t start = 0; /* =0 to silence warning */ 2481 size_t len = 0, expected_len; 2482 Error *err = NULL; 2483 int res; 2484 2485 trace_source_return_path_thread_entry(); 2486 rcu_register_thread(); 2487 2488 while (migration_is_running()) { 2489 trace_source_return_path_thread_loop_top(); 2490 2491 header_type = qemu_get_be16(rp); 2492 header_len = qemu_get_be16(rp); 2493 2494 if (qemu_file_get_error(rp)) { 2495 qemu_file_get_error_obj(rp, &err); 2496 goto out; 2497 } 2498 2499 if (header_type >= MIG_RP_MSG_MAX || 2500 header_type == MIG_RP_MSG_INVALID) { 2501 error_setg(&err, "Received invalid message 0x%04x length 0x%04x", 2502 header_type, header_len); 2503 goto out; 2504 } 2505 2506 if ((rp_cmd_args[header_type].len != -1 && 2507 header_len != rp_cmd_args[header_type].len) || 2508 header_len > sizeof(buf)) { 2509 error_setg(&err, "Received '%s' message (0x%04x) with" 2510 "incorrect length %d expecting %zu", 2511 rp_cmd_args[header_type].name, header_type, header_len, 2512 (size_t)rp_cmd_args[header_type].len); 2513 goto out; 2514 } 2515 2516 /* We know we've got a valid header by this point */ 2517 res = qemu_get_buffer(rp, buf, header_len); 2518 if (res != header_len) { 2519 error_setg(&err, "Failed reading data for message 0x%04x" 2520 " read %d expected %d", 2521 header_type, res, header_len); 2522 goto out; 2523 } 2524 2525 /* OK, we have the message and the data */ 2526 switch (header_type) { 2527 case MIG_RP_MSG_SHUT: 2528 sibling_error = ldl_be_p(buf); 2529 trace_source_return_path_thread_shut(sibling_error); 2530 if (sibling_error) { 2531 error_setg(&err, "Sibling indicated error %d", sibling_error); 2532 } 2533 /* 2534 * We'll let the main thread deal with closing the RP 2535 * we could do a shutdown(2) on it, but we're the only user 2536 * anyway, so there's nothing gained. 2537 */ 2538 goto out; 2539 2540 case MIG_RP_MSG_PONG: 2541 tmp32 = ldl_be_p(buf); 2542 trace_source_return_path_thread_pong(tmp32); 2543 qemu_sem_post(&ms->rp_state.rp_pong_acks); 2544 break; 2545 2546 case MIG_RP_MSG_REQ_PAGES: 2547 start = ldq_be_p(buf); 2548 len = ldl_be_p(buf + 8); 2549 migrate_handle_rp_req_pages(ms, NULL, start, len, &err); 2550 if (err) { 2551 goto out; 2552 } 2553 break; 2554 2555 case MIG_RP_MSG_REQ_PAGES_ID: 2556 expected_len = 12 + 1; /* header + termination */ 2557 2558 if (header_len >= expected_len) { 2559 start = ldq_be_p(buf); 2560 len = ldl_be_p(buf + 8); 2561 /* Now we expect an idstr */ 2562 tmp32 = buf[12]; /* Length of the following idstr */ 2563 buf[13 + tmp32] = '\0'; 2564 expected_len += tmp32; 2565 } 2566 if (header_len != expected_len) { 2567 error_setg(&err, "Req_Page_id with length %d expecting %zd", 2568 header_len, expected_len); 2569 goto out; 2570 } 2571 migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len, 2572 &err); 2573 if (err) { 2574 goto out; 2575 } 2576 break; 2577 2578 case MIG_RP_MSG_RECV_BITMAP: 2579 if (header_len < 1) { 2580 error_setg(&err, "MIG_RP_MSG_RECV_BITMAP missing block name"); 2581 goto out; 2582 } 2583 /* Format: len (1B) + idstr (<255B). This ends the idstr. */ 2584 buf[buf[0] + 1] = '\0'; 2585 if (!migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1), &err)) { 2586 goto out; 2587 } 2588 break; 2589 2590 case MIG_RP_MSG_RESUME_ACK: 2591 tmp32 = ldl_be_p(buf); 2592 if (!migrate_handle_rp_resume_ack(ms, tmp32, &err)) { 2593 goto out; 2594 } 2595 break; 2596 2597 case MIG_RP_MSG_SWITCHOVER_ACK: 2598 ms->switchover_acked = true; 2599 trace_source_return_path_thread_switchover_acked(); 2600 break; 2601 2602 default: 2603 break; 2604 } 2605 } 2606 2607 out: 2608 if (err) { 2609 migrate_set_error(ms, err); 2610 error_free(err); 2611 trace_source_return_path_thread_bad_end(); 2612 } 2613 2614 if (ms->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { 2615 /* 2616 * this will be extremely unlikely: that we got yet another network 2617 * issue during recovering of the 1st network failure.. during this 2618 * period the main migration thread can be waiting on rp_sem for 2619 * this thread to sync with the other side. 2620 * 2621 * When this happens, explicitly kick the migration thread out of 2622 * RECOVER stage and back to PAUSED, so the admin can try 2623 * everything again. 2624 */ 2625 migration_rp_kick(ms); 2626 } 2627 2628 trace_source_return_path_thread_end(); 2629 rcu_unregister_thread(); 2630 2631 return NULL; 2632 } 2633 2634 static int open_return_path_on_source(MigrationState *ms) 2635 { 2636 ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file); 2637 if (!ms->rp_state.from_dst_file) { 2638 return -1; 2639 } 2640 2641 trace_open_return_path_on_source(); 2642 2643 qemu_thread_create(&ms->rp_state.rp_thread, MIGRATION_THREAD_SRC_RETURN, 2644 source_return_path_thread, ms, QEMU_THREAD_JOINABLE); 2645 ms->rp_state.rp_thread_created = true; 2646 2647 trace_open_return_path_on_source_continue(); 2648 2649 return 0; 2650 } 2651 2652 /* Return true if error detected, or false otherwise */ 2653 static bool close_return_path_on_source(MigrationState *ms) 2654 { 2655 if (!ms->rp_state.rp_thread_created) { 2656 return false; 2657 } 2658 2659 trace_migration_return_path_end_before(); 2660 2661 /* 2662 * If this is a normal exit then the destination will send a SHUT 2663 * and the rp_thread will exit, however if there's an error we 2664 * need to cause it to exit. shutdown(2), if we have it, will 2665 * cause it to unblock if it's stuck waiting for the destination. 2666 */ 2667 WITH_QEMU_LOCK_GUARD(&ms->qemu_file_lock) { 2668 if (migrate_has_error(ms) && ms->rp_state.from_dst_file) { 2669 qemu_file_shutdown(ms->rp_state.from_dst_file); 2670 } 2671 } 2672 2673 qemu_thread_join(&ms->rp_state.rp_thread); 2674 ms->rp_state.rp_thread_created = false; 2675 migration_release_dst_files(ms); 2676 trace_migration_return_path_end_after(); 2677 2678 /* Return path will persist the error in MigrationState when quit */ 2679 return migrate_has_error(ms); 2680 } 2681 2682 static inline void 2683 migration_wait_main_channel(MigrationState *ms) 2684 { 2685 /* Wait until one PONG message received */ 2686 qemu_sem_wait(&ms->rp_state.rp_pong_acks); 2687 } 2688 2689 /* 2690 * Switch from normal iteration to postcopy 2691 * Returns non-0 on error 2692 */ 2693 static int postcopy_start(MigrationState *ms, Error **errp) 2694 { 2695 int ret; 2696 QIOChannelBuffer *bioc; 2697 QEMUFile *fb; 2698 2699 /* 2700 * Now we're 100% sure to switch to postcopy, so JSON writer won't be 2701 * useful anymore. Free the resources early if it is there. Clearing 2702 * the vmdesc also means any follow up vmstate_save()s will start to 2703 * skip all JSON operations, which can shrink postcopy downtime. 2704 */ 2705 migration_cleanup_json_writer(ms); 2706 2707 if (migrate_postcopy_preempt()) { 2708 migration_wait_main_channel(ms); 2709 if (postcopy_preempt_establish_channel(ms)) { 2710 if (ms->state != MIGRATION_STATUS_CANCELLING) { 2711 migrate_set_state(&ms->state, ms->state, 2712 MIGRATION_STATUS_FAILED); 2713 } 2714 error_setg(errp, "%s: Failed to establish preempt channel", 2715 __func__); 2716 return -1; 2717 } 2718 } 2719 2720 trace_postcopy_start(); 2721 bql_lock(); 2722 trace_postcopy_start_set_run(); 2723 2724 ret = migration_stop_vm(ms, RUN_STATE_FINISH_MIGRATE); 2725 if (ret < 0) { 2726 error_setg_errno(errp, -ret, "%s: Failed to stop the VM", __func__); 2727 goto fail; 2728 } 2729 2730 if (!migration_switchover_start(ms, errp)) { 2731 goto fail; 2732 } 2733 2734 /* 2735 * Cause any non-postcopiable, but iterative devices to 2736 * send out their final data. 2737 */ 2738 ret = qemu_savevm_state_complete_precopy_iterable(ms->to_dst_file, true); 2739 if (ret) { 2740 error_setg(errp, "Postcopy save non-postcopiable iterables failed"); 2741 goto fail; 2742 } 2743 2744 /* 2745 * in Finish migrate and with the io-lock held everything should 2746 * be quiet, but we've potentially still got dirty pages and we 2747 * need to tell the destination to throw any pages it's already received 2748 * that are dirty 2749 */ 2750 if (migrate_postcopy_ram()) { 2751 ram_postcopy_send_discard_bitmap(ms); 2752 } 2753 2754 if (migrate_postcopy_ram()) { 2755 /* Ping just for debugging, helps line traces up */ 2756 qemu_savevm_send_ping(ms->to_dst_file, 2); 2757 } 2758 2759 /* 2760 * While loading the device state we may trigger page transfer 2761 * requests and the fd must be free to process those, and thus 2762 * the destination must read the whole device state off the fd before 2763 * it starts processing it. Unfortunately the ad-hoc migration format 2764 * doesn't allow the destination to know the size to read without fully 2765 * parsing it through each devices load-state code (especially the open 2766 * coded devices that use get/put). 2767 * So we wrap the device state up in a package with a length at the start; 2768 * to do this we use a qemu_buf to hold the whole of the device state. 2769 */ 2770 bioc = qio_channel_buffer_new(4096); 2771 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer"); 2772 fb = qemu_file_new_output(QIO_CHANNEL(bioc)); 2773 object_unref(OBJECT(bioc)); 2774 2775 /* 2776 * Make sure the receiver can get incoming pages before we send the rest 2777 * of the state 2778 */ 2779 qemu_savevm_send_postcopy_listen(fb); 2780 2781 ret = qemu_savevm_state_complete_precopy_non_iterable(fb, true); 2782 if (ret) { 2783 error_setg(errp, "Postcopy save non-iterable device states failed"); 2784 goto fail_closefb; 2785 } 2786 2787 if (migrate_postcopy_ram()) { 2788 qemu_savevm_send_ping(fb, 3); 2789 } 2790 2791 qemu_savevm_send_postcopy_run(fb); 2792 2793 /* <><> end of stuff going into the package */ 2794 2795 /* Last point of recovery; as soon as we send the package the destination 2796 * can open devices and potentially start running. 2797 * Lets just check again we've not got any errors. 2798 */ 2799 ret = qemu_file_get_error(ms->to_dst_file); 2800 if (ret) { 2801 error_setg(errp, "postcopy_start: Migration stream errored (pre package)"); 2802 goto fail_closefb; 2803 } 2804 2805 /* Now send that blob */ 2806 if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) { 2807 error_setg(errp, "%s: Failed to send packaged data", __func__); 2808 goto fail_closefb; 2809 } 2810 qemu_fclose(fb); 2811 2812 /* Send a notify to give a chance for anything that needs to happen 2813 * at the transition to postcopy and after the device state; in particular 2814 * spice needs to trigger a transition now 2815 */ 2816 migration_call_notifiers(ms, MIG_EVENT_PRECOPY_DONE, NULL); 2817 2818 migration_downtime_end(ms); 2819 2820 if (migrate_postcopy_ram()) { 2821 /* 2822 * Although this ping is just for debug, it could potentially be 2823 * used for getting a better measurement of downtime at the source. 2824 */ 2825 qemu_savevm_send_ping(ms->to_dst_file, 4); 2826 } 2827 2828 if (migrate_release_ram()) { 2829 ram_postcopy_migrated_memory_release(ms); 2830 } 2831 2832 ret = qemu_file_get_error(ms->to_dst_file); 2833 if (ret) { 2834 error_setg_errno(errp, -ret, "postcopy_start: Migration stream error"); 2835 goto fail; 2836 } 2837 trace_postcopy_preempt_enabled(migrate_postcopy_preempt()); 2838 2839 /* 2840 * Now postcopy officially started, switch to postcopy bandwidth that 2841 * user specified. 2842 */ 2843 migration_rate_set(migrate_max_postcopy_bandwidth()); 2844 2845 /* Now, switchover looks all fine, switching to postcopy-active */ 2846 migrate_set_state(&ms->state, MIGRATION_STATUS_DEVICE, 2847 MIGRATION_STATUS_POSTCOPY_ACTIVE); 2848 2849 bql_unlock(); 2850 2851 return ret; 2852 2853 fail_closefb: 2854 qemu_fclose(fb); 2855 fail: 2856 migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, 2857 MIGRATION_STATUS_FAILED); 2858 migration_block_activate(NULL); 2859 migration_call_notifiers(ms, MIG_EVENT_PRECOPY_FAILED, NULL); 2860 bql_unlock(); 2861 return -1; 2862 } 2863 2864 /** 2865 * @migration_switchover_prepare: Start VM switchover procedure 2866 * 2867 * @s: The migration state object pointer 2868 * 2869 * Prepares for the switchover, depending on "pause-before-switchover" 2870 * capability. 2871 * 2872 * If cap set, state machine goes like: 2873 * [postcopy-]active -> pre-switchover -> device 2874 * 2875 * If cap not set: 2876 * [postcopy-]active -> device 2877 * 2878 * Returns: true on success, false on interruptions. 2879 */ 2880 static bool migration_switchover_prepare(MigrationState *s) 2881 { 2882 /* Concurrent cancellation? Quit */ 2883 if (s->state == MIGRATION_STATUS_CANCELLING) { 2884 return false; 2885 } 2886 2887 /* 2888 * No matter precopy or postcopy, since we still hold BQL it must not 2889 * change concurrently to CANCELLING, so it must be either ACTIVE or 2890 * POSTCOPY_ACTIVE. 2891 */ 2892 assert(migration_is_active()); 2893 2894 /* If the pre stage not requested, directly switch to DEVICE */ 2895 if (!migrate_pause_before_switchover()) { 2896 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_DEVICE); 2897 return true; 2898 } 2899 2900 /* Since leaving this state is not atomic with posting the semaphore 2901 * it's possible that someone could have issued multiple migrate_continue 2902 * and the semaphore is incorrectly positive at this point; 2903 * the docs say it's undefined to reinit a semaphore that's already 2904 * init'd, so use timedwait to eat up any existing posts. 2905 */ 2906 while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) { 2907 /* This block intentionally left blank */ 2908 } 2909 2910 /* Update [POSTCOPY_]ACTIVE to PRE_SWITCHOVER */ 2911 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_PRE_SWITCHOVER); 2912 bql_unlock(); 2913 2914 qemu_sem_wait(&s->pause_sem); 2915 2916 bql_lock(); 2917 /* 2918 * After BQL released and retaken, the state can be CANCELLING if it 2919 * happend during sem_wait().. Only change the state if it's still 2920 * pre-switchover. 2921 */ 2922 migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER, 2923 MIGRATION_STATUS_DEVICE); 2924 2925 return s->state == MIGRATION_STATUS_DEVICE; 2926 } 2927 2928 static bool migration_switchover_start(MigrationState *s, Error **errp) 2929 { 2930 ERRP_GUARD(); 2931 2932 if (!migration_switchover_prepare(s)) { 2933 error_setg(errp, "Switchover is interrupted"); 2934 return false; 2935 } 2936 2937 /* Inactivate disks except in COLO */ 2938 if (!migrate_colo()) { 2939 /* 2940 * Inactivate before sending QEMU_VM_EOF so that the 2941 * bdrv_activate_all() on the other end won't fail. 2942 */ 2943 if (!migration_block_inactivate()) { 2944 error_setg(errp, "Block inactivate failed during switchover"); 2945 return false; 2946 } 2947 } 2948 2949 migration_rate_set(RATE_LIMIT_DISABLED); 2950 2951 precopy_notify_complete(); 2952 2953 qemu_savevm_maybe_send_switchover_start(s->to_dst_file); 2954 2955 return true; 2956 } 2957 2958 static int migration_completion_precopy(MigrationState *s) 2959 { 2960 int ret; 2961 2962 bql_lock(); 2963 2964 if (!migrate_mode_is_cpr(s)) { 2965 ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE); 2966 if (ret < 0) { 2967 goto out_unlock; 2968 } 2969 } 2970 2971 if (!migration_switchover_start(s, NULL)) { 2972 ret = -EFAULT; 2973 goto out_unlock; 2974 } 2975 2976 ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false); 2977 out_unlock: 2978 bql_unlock(); 2979 return ret; 2980 } 2981 2982 static void migration_completion_postcopy(MigrationState *s) 2983 { 2984 trace_migration_completion_postcopy_end(); 2985 2986 bql_lock(); 2987 qemu_savevm_state_complete_postcopy(s->to_dst_file); 2988 bql_unlock(); 2989 2990 /* 2991 * Shutdown the postcopy fast path thread. This is only needed when dest 2992 * QEMU binary is old (7.1/7.2). QEMU 8.0+ doesn't need this. 2993 */ 2994 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) { 2995 postcopy_preempt_shutdown_file(s); 2996 } 2997 2998 trace_migration_completion_postcopy_end_after_complete(); 2999 } 3000 3001 /** 3002 * migration_completion: Used by migration_thread when there's not much left. 3003 * The caller 'breaks' the loop when this returns. 3004 * 3005 * @s: Current migration state 3006 */ 3007 static void migration_completion(MigrationState *s) 3008 { 3009 int ret = 0; 3010 Error *local_err = NULL; 3011 3012 if (s->state == MIGRATION_STATUS_ACTIVE) { 3013 ret = migration_completion_precopy(s); 3014 } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 3015 migration_completion_postcopy(s); 3016 } else { 3017 ret = -1; 3018 } 3019 3020 if (ret < 0) { 3021 goto fail; 3022 } 3023 3024 if (close_return_path_on_source(s)) { 3025 goto fail; 3026 } 3027 3028 if (qemu_file_get_error(s->to_dst_file)) { 3029 trace_migration_completion_file_err(); 3030 goto fail; 3031 } 3032 3033 if (migrate_colo() && s->state == MIGRATION_STATUS_ACTIVE) { 3034 /* COLO does not support postcopy */ 3035 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, 3036 MIGRATION_STATUS_COLO); 3037 } else { 3038 migration_completion_end(s); 3039 } 3040 3041 return; 3042 3043 fail: 3044 if (qemu_file_get_error_obj(s->to_dst_file, &local_err)) { 3045 migrate_set_error(s, local_err); 3046 error_free(local_err); 3047 } else if (ret) { 3048 error_setg_errno(&local_err, -ret, "Error in migration completion"); 3049 migrate_set_error(s, local_err); 3050 error_free(local_err); 3051 } 3052 3053 if (s->state != MIGRATION_STATUS_CANCELLING) { 3054 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED); 3055 } 3056 } 3057 3058 /** 3059 * bg_migration_completion: Used by bg_migration_thread when after all the 3060 * RAM has been saved. The caller 'breaks' the loop when this returns. 3061 * 3062 * @s: Current migration state 3063 */ 3064 static void bg_migration_completion(MigrationState *s) 3065 { 3066 int current_active_state = s->state; 3067 3068 if (s->state == MIGRATION_STATUS_ACTIVE) { 3069 /* 3070 * By this moment we have RAM content saved into the migration stream. 3071 * The next step is to flush the non-RAM content (device state) 3072 * right after the ram content. The device state has been stored into 3073 * the temporary buffer before RAM saving started. 3074 */ 3075 qemu_put_buffer(s->to_dst_file, s->bioc->data, s->bioc->usage); 3076 qemu_fflush(s->to_dst_file); 3077 } else if (s->state == MIGRATION_STATUS_CANCELLING) { 3078 return; 3079 } 3080 3081 if (qemu_file_get_error(s->to_dst_file)) { 3082 trace_migration_completion_file_err(); 3083 goto fail; 3084 } 3085 3086 migration_completion_end(s); 3087 return; 3088 3089 fail: 3090 migrate_set_state(&s->state, current_active_state, 3091 MIGRATION_STATUS_FAILED); 3092 } 3093 3094 typedef enum MigThrError { 3095 /* No error detected */ 3096 MIG_THR_ERR_NONE = 0, 3097 /* Detected error, but resumed successfully */ 3098 MIG_THR_ERR_RECOVERED = 1, 3099 /* Detected fatal error, need to exit */ 3100 MIG_THR_ERR_FATAL = 2, 3101 } MigThrError; 3102 3103 static int postcopy_resume_handshake(MigrationState *s) 3104 { 3105 qemu_savevm_send_postcopy_resume(s->to_dst_file); 3106 3107 while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { 3108 if (migration_rp_wait(s)) { 3109 return -1; 3110 } 3111 } 3112 3113 if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { 3114 return 0; 3115 } 3116 3117 return -1; 3118 } 3119 3120 /* Return zero if success, or <0 for error */ 3121 static int postcopy_do_resume(MigrationState *s) 3122 { 3123 int ret; 3124 3125 /* 3126 * Call all the resume_prepare() hooks, so that modules can be 3127 * ready for the migration resume. 3128 */ 3129 ret = qemu_savevm_state_resume_prepare(s); 3130 if (ret) { 3131 error_report("%s: resume_prepare() failure detected: %d", 3132 __func__, ret); 3133 return ret; 3134 } 3135 3136 /* 3137 * If preempt is enabled, re-establish the preempt channel. Note that 3138 * we do it after resume prepare to make sure the main channel will be 3139 * created before the preempt channel. E.g. with weak network, the 3140 * dest QEMU may get messed up with the preempt and main channels on 3141 * the order of connection setup. This guarantees the correct order. 3142 */ 3143 ret = postcopy_preempt_establish_channel(s); 3144 if (ret) { 3145 error_report("%s: postcopy_preempt_establish_channel(): %d", 3146 __func__, ret); 3147 return ret; 3148 } 3149 3150 /* 3151 * Last handshake with destination on the resume (destination will 3152 * switch to postcopy-active afterwards) 3153 */ 3154 ret = postcopy_resume_handshake(s); 3155 if (ret) { 3156 error_report("%s: handshake failed: %d", __func__, ret); 3157 return ret; 3158 } 3159 3160 return 0; 3161 } 3162 3163 /* 3164 * We don't return until we are in a safe state to continue current 3165 * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or 3166 * MIG_THR_ERR_FATAL if unrecovery failure happened. 3167 */ 3168 static MigThrError postcopy_pause(MigrationState *s) 3169 { 3170 assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); 3171 3172 while (true) { 3173 QEMUFile *file; 3174 3175 /* 3176 * We're already pausing, so ignore any errors on the return 3177 * path and just wait for the thread to finish. It will be 3178 * re-created when we resume. 3179 */ 3180 close_return_path_on_source(s); 3181 3182 /* 3183 * Current channel is possibly broken. Release it. Note that this is 3184 * guaranteed even without lock because to_dst_file should only be 3185 * modified by the migration thread. That also guarantees that the 3186 * unregister of yank is safe too without the lock. It should be safe 3187 * even to be within the qemu_file_lock, but we didn't do that to avoid 3188 * taking more mutex (yank_lock) within qemu_file_lock. TL;DR: we make 3189 * the qemu_file_lock critical section as small as possible. 3190 */ 3191 assert(s->to_dst_file); 3192 migration_ioc_unregister_yank_from_file(s->to_dst_file); 3193 qemu_mutex_lock(&s->qemu_file_lock); 3194 file = s->to_dst_file; 3195 s->to_dst_file = NULL; 3196 qemu_mutex_unlock(&s->qemu_file_lock); 3197 3198 qemu_file_shutdown(file); 3199 qemu_fclose(file); 3200 3201 migrate_set_state(&s->state, s->state, 3202 MIGRATION_STATUS_POSTCOPY_PAUSED); 3203 3204 error_report("Detected IO failure for postcopy. " 3205 "Migration paused."); 3206 3207 /* 3208 * We wait until things fixed up. Then someone will setup the 3209 * status back for us. 3210 */ 3211 do { 3212 qemu_sem_wait(&s->postcopy_pause_sem); 3213 } while (postcopy_is_paused(s->state)); 3214 3215 if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { 3216 /* Woken up by a recover procedure. Give it a shot */ 3217 3218 /* Do the resume logic */ 3219 if (postcopy_do_resume(s) == 0) { 3220 /* Let's continue! */ 3221 trace_postcopy_pause_continued(); 3222 return MIG_THR_ERR_RECOVERED; 3223 } else { 3224 /* 3225 * Something wrong happened during the recovery, let's 3226 * pause again. Pause is always better than throwing 3227 * data away. 3228 */ 3229 continue; 3230 } 3231 } else { 3232 /* This is not right... Time to quit. */ 3233 return MIG_THR_ERR_FATAL; 3234 } 3235 } 3236 } 3237 3238 void migration_file_set_error(int ret, Error *err) 3239 { 3240 MigrationState *s = current_migration; 3241 3242 WITH_QEMU_LOCK_GUARD(&s->qemu_file_lock) { 3243 if (s->to_dst_file) { 3244 qemu_file_set_error_obj(s->to_dst_file, ret, err); 3245 } else if (err) { 3246 error_report_err(err); 3247 } 3248 } 3249 } 3250 3251 static MigThrError migration_detect_error(MigrationState *s) 3252 { 3253 int ret; 3254 int state = s->state; 3255 Error *local_error = NULL; 3256 3257 if (state == MIGRATION_STATUS_CANCELLING || 3258 state == MIGRATION_STATUS_CANCELLED) { 3259 /* End the migration, but don't set the state to failed */ 3260 return MIG_THR_ERR_FATAL; 3261 } 3262 3263 /* 3264 * Try to detect any file errors. Note that postcopy_qemufile_src will 3265 * be NULL when postcopy preempt is not enabled. 3266 */ 3267 ret = qemu_file_get_error_obj_any(s->to_dst_file, 3268 s->postcopy_qemufile_src, 3269 &local_error); 3270 if (!ret) { 3271 /* Everything is fine */ 3272 assert(!local_error); 3273 return MIG_THR_ERR_NONE; 3274 } 3275 3276 if (local_error) { 3277 migrate_set_error(s, local_error); 3278 error_free(local_error); 3279 } 3280 3281 if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret) { 3282 /* 3283 * For postcopy, we allow the network to be down for a 3284 * while. After that, it can be continued by a 3285 * recovery phase. 3286 */ 3287 return postcopy_pause(s); 3288 } else { 3289 /* 3290 * For precopy (or postcopy with error outside IO), we fail 3291 * with no time. 3292 */ 3293 migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED); 3294 trace_migration_thread_file_err(); 3295 3296 /* Time to stop the migration, now. */ 3297 return MIG_THR_ERR_FATAL; 3298 } 3299 } 3300 3301 static void migration_completion_end(MigrationState *s) 3302 { 3303 uint64_t bytes = migration_transferred_bytes(); 3304 int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 3305 int64_t transfer_time; 3306 3307 /* 3308 * Take the BQL here so that query-migrate on the QMP thread sees: 3309 * - atomic update of s->total_time and s->mbps; 3310 * - correct ordering of s->mbps update vs. s->state; 3311 */ 3312 bql_lock(); 3313 migration_downtime_end(s); 3314 s->total_time = end_time - s->start_time; 3315 transfer_time = s->total_time - s->setup_time; 3316 if (transfer_time) { 3317 s->mbps = ((double) bytes * 8.0) / transfer_time / 1000; 3318 } 3319 3320 migrate_set_state(&s->state, s->state, 3321 MIGRATION_STATUS_COMPLETED); 3322 bql_unlock(); 3323 } 3324 3325 static void update_iteration_initial_status(MigrationState *s) 3326 { 3327 /* 3328 * Update these three fields at the same time to avoid mismatch info lead 3329 * wrong speed calculation. 3330 */ 3331 s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 3332 s->iteration_initial_bytes = migration_transferred_bytes(); 3333 s->iteration_initial_pages = ram_get_total_transferred_pages(); 3334 } 3335 3336 static void migration_update_counters(MigrationState *s, 3337 int64_t current_time) 3338 { 3339 uint64_t transferred, transferred_pages, time_spent; 3340 uint64_t current_bytes; /* bytes transferred since the beginning */ 3341 uint64_t switchover_bw; 3342 /* Expected bandwidth when switching over to destination QEMU */ 3343 double expected_bw_per_ms; 3344 double bandwidth; 3345 3346 if (current_time < s->iteration_start_time + BUFFER_DELAY) { 3347 return; 3348 } 3349 3350 switchover_bw = migrate_avail_switchover_bandwidth(); 3351 current_bytes = migration_transferred_bytes(); 3352 transferred = current_bytes - s->iteration_initial_bytes; 3353 time_spent = current_time - s->iteration_start_time; 3354 bandwidth = (double)transferred / time_spent; 3355 3356 if (switchover_bw) { 3357 /* 3358 * If the user specified a switchover bandwidth, let's trust the 3359 * user so that can be more accurate than what we estimated. 3360 */ 3361 expected_bw_per_ms = switchover_bw / 1000; 3362 } else { 3363 /* If the user doesn't specify bandwidth, we use the estimated */ 3364 expected_bw_per_ms = bandwidth; 3365 } 3366 3367 s->threshold_size = expected_bw_per_ms * migrate_downtime_limit(); 3368 3369 s->mbps = (((double) transferred * 8.0) / 3370 ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; 3371 3372 transferred_pages = ram_get_total_transferred_pages() - 3373 s->iteration_initial_pages; 3374 s->pages_per_second = (double) transferred_pages / 3375 (((double) time_spent / 1000.0)); 3376 3377 /* 3378 * if we haven't sent anything, we don't want to 3379 * recalculate. 10000 is a small enough number for our purposes 3380 */ 3381 if (stat64_get(&mig_stats.dirty_pages_rate) && 3382 transferred > 10000) { 3383 s->expected_downtime = 3384 stat64_get(&mig_stats.dirty_bytes_last_sync) / expected_bw_per_ms; 3385 } 3386 3387 migration_rate_reset(); 3388 3389 update_iteration_initial_status(s); 3390 3391 trace_migrate_transferred(transferred, time_spent, 3392 /* Both in unit bytes/ms */ 3393 bandwidth, switchover_bw / 1000, 3394 s->threshold_size); 3395 } 3396 3397 static bool migration_can_switchover(MigrationState *s) 3398 { 3399 if (!migrate_switchover_ack()) { 3400 return true; 3401 } 3402 3403 /* No reason to wait for switchover ACK if VM is stopped */ 3404 if (!runstate_is_running()) { 3405 return true; 3406 } 3407 3408 return s->switchover_acked; 3409 } 3410 3411 /* Migration thread iteration status */ 3412 typedef enum { 3413 MIG_ITERATE_RESUME, /* Resume current iteration */ 3414 MIG_ITERATE_SKIP, /* Skip current iteration */ 3415 MIG_ITERATE_BREAK, /* Break the loop */ 3416 } MigIterateState; 3417 3418 /* 3419 * Return true if continue to the next iteration directly, false 3420 * otherwise. 3421 */ 3422 static MigIterateState migration_iteration_run(MigrationState *s) 3423 { 3424 uint64_t must_precopy, can_postcopy, pending_size; 3425 Error *local_err = NULL; 3426 bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE; 3427 bool can_switchover = migration_can_switchover(s); 3428 3429 qemu_savevm_state_pending_estimate(&must_precopy, &can_postcopy); 3430 pending_size = must_precopy + can_postcopy; 3431 trace_migrate_pending_estimate(pending_size, must_precopy, can_postcopy); 3432 3433 if (pending_size < s->threshold_size) { 3434 qemu_savevm_state_pending_exact(&must_precopy, &can_postcopy); 3435 pending_size = must_precopy + can_postcopy; 3436 trace_migrate_pending_exact(pending_size, must_precopy, can_postcopy); 3437 } 3438 3439 if ((!pending_size || pending_size < s->threshold_size) && can_switchover) { 3440 trace_migration_thread_low_pending(pending_size); 3441 migration_completion(s); 3442 return MIG_ITERATE_BREAK; 3443 } 3444 3445 /* Still a significant amount to transfer */ 3446 if (!in_postcopy && must_precopy <= s->threshold_size && can_switchover && 3447 qatomic_read(&s->start_postcopy)) { 3448 if (postcopy_start(s, &local_err)) { 3449 migrate_set_error(s, local_err); 3450 error_report_err(local_err); 3451 } 3452 return MIG_ITERATE_SKIP; 3453 } 3454 3455 /* Just another iteration step */ 3456 qemu_savevm_state_iterate(s->to_dst_file, in_postcopy); 3457 return MIG_ITERATE_RESUME; 3458 } 3459 3460 static void migration_iteration_finish(MigrationState *s) 3461 { 3462 bql_lock(); 3463 3464 /* 3465 * If we enabled cpu throttling for auto-converge, turn it off. 3466 * Stopping CPU throttle should be serialized by BQL to avoid 3467 * racing for the throttle_dirty_sync_timer. 3468 */ 3469 if (migrate_auto_converge()) { 3470 cpu_throttle_stop(); 3471 } 3472 3473 switch (s->state) { 3474 case MIGRATION_STATUS_COMPLETED: 3475 runstate_set(RUN_STATE_POSTMIGRATE); 3476 break; 3477 case MIGRATION_STATUS_COLO: 3478 assert(migrate_colo()); 3479 migrate_start_colo_process(s); 3480 s->vm_old_state = RUN_STATE_RUNNING; 3481 /* Fallthrough */ 3482 case MIGRATION_STATUS_FAILED: 3483 case MIGRATION_STATUS_CANCELLED: 3484 case MIGRATION_STATUS_CANCELLING: 3485 /* 3486 * Re-activate the block drives if they're inactivated. Note, COLO 3487 * shouldn't use block_active at all, so it should be no-op there. 3488 */ 3489 migration_block_activate(NULL); 3490 if (runstate_is_live(s->vm_old_state)) { 3491 if (!runstate_check(RUN_STATE_SHUTDOWN)) { 3492 vm_start(); 3493 } 3494 } else { 3495 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) { 3496 runstate_set(s->vm_old_state); 3497 } 3498 } 3499 break; 3500 3501 default: 3502 /* Should not reach here, but if so, forgive the VM. */ 3503 error_report("%s: Unknown ending state %d", __func__, s->state); 3504 break; 3505 } 3506 3507 migration_bh_schedule(migration_cleanup_bh, s); 3508 bql_unlock(); 3509 } 3510 3511 static void bg_migration_iteration_finish(MigrationState *s) 3512 { 3513 /* 3514 * Stop tracking RAM writes - un-protect memory, un-register UFFD 3515 * memory ranges, flush kernel wait queues and wake up threads 3516 * waiting for write fault to be resolved. 3517 */ 3518 ram_write_tracking_stop(); 3519 3520 bql_lock(); 3521 switch (s->state) { 3522 case MIGRATION_STATUS_COMPLETED: 3523 case MIGRATION_STATUS_ACTIVE: 3524 case MIGRATION_STATUS_FAILED: 3525 case MIGRATION_STATUS_CANCELLED: 3526 case MIGRATION_STATUS_CANCELLING: 3527 break; 3528 3529 default: 3530 /* Should not reach here, but if so, forgive the VM. */ 3531 error_report("%s: Unknown ending state %d", __func__, s->state); 3532 break; 3533 } 3534 3535 migration_bh_schedule(migration_cleanup_bh, s); 3536 bql_unlock(); 3537 } 3538 3539 /* 3540 * Return true if continue to the next iteration directly, false 3541 * otherwise. 3542 */ 3543 static MigIterateState bg_migration_iteration_run(MigrationState *s) 3544 { 3545 int res; 3546 3547 res = qemu_savevm_state_iterate(s->to_dst_file, false); 3548 if (res > 0) { 3549 bg_migration_completion(s); 3550 return MIG_ITERATE_BREAK; 3551 } 3552 3553 return MIG_ITERATE_RESUME; 3554 } 3555 3556 void migration_make_urgent_request(void) 3557 { 3558 qemu_sem_post(&migrate_get_current()->rate_limit_sem); 3559 } 3560 3561 void migration_consume_urgent_request(void) 3562 { 3563 qemu_sem_wait(&migrate_get_current()->rate_limit_sem); 3564 } 3565 3566 /* Returns true if the rate limiting was broken by an urgent request */ 3567 bool migration_rate_limit(void) 3568 { 3569 int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); 3570 MigrationState *s = migrate_get_current(); 3571 3572 bool urgent = false; 3573 migration_update_counters(s, now); 3574 if (migration_rate_exceeded(s->to_dst_file)) { 3575 3576 if (qemu_file_get_error(s->to_dst_file)) { 3577 return false; 3578 } 3579 /* 3580 * Wait for a delay to do rate limiting OR 3581 * something urgent to post the semaphore. 3582 */ 3583 int ms = s->iteration_start_time + BUFFER_DELAY - now; 3584 trace_migration_rate_limit_pre(ms); 3585 if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) { 3586 /* 3587 * We were woken by one or more urgent things but 3588 * the timedwait will have consumed one of them. 3589 * The service routine for the urgent wake will dec 3590 * the semaphore itself for each item it consumes, 3591 * so add this one we just eat back. 3592 */ 3593 qemu_sem_post(&s->rate_limit_sem); 3594 urgent = true; 3595 } 3596 trace_migration_rate_limit_post(urgent); 3597 } 3598 return urgent; 3599 } 3600 3601 /* 3602 * if failover devices are present, wait they are completely 3603 * unplugged 3604 */ 3605 3606 static void qemu_savevm_wait_unplug(MigrationState *s, int old_state, 3607 int new_state) 3608 { 3609 if (qemu_savevm_state_guest_unplug_pending()) { 3610 migrate_set_state(&s->state, old_state, MIGRATION_STATUS_WAIT_UNPLUG); 3611 3612 while (s->state == MIGRATION_STATUS_WAIT_UNPLUG && 3613 qemu_savevm_state_guest_unplug_pending()) { 3614 qemu_sem_timedwait(&s->wait_unplug_sem, 250); 3615 } 3616 if (s->state != MIGRATION_STATUS_WAIT_UNPLUG) { 3617 int timeout = 120; /* 30 seconds */ 3618 /* 3619 * migration has been canceled 3620 * but as we have started an unplug we must wait the end 3621 * to be able to plug back the card 3622 */ 3623 while (timeout-- && qemu_savevm_state_guest_unplug_pending()) { 3624 qemu_sem_timedwait(&s->wait_unplug_sem, 250); 3625 } 3626 if (qemu_savevm_state_guest_unplug_pending() && 3627 !qtest_enabled()) { 3628 warn_report("migration: partially unplugged device on " 3629 "failure"); 3630 } 3631 } 3632 3633 migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, new_state); 3634 } else { 3635 migrate_set_state(&s->state, old_state, new_state); 3636 } 3637 } 3638 3639 /* 3640 * Master migration thread on the source VM. 3641 * It drives the migration and pumps the data down the outgoing channel. 3642 */ 3643 static void *migration_thread(void *opaque) 3644 { 3645 MigrationState *s = opaque; 3646 MigrationThread *thread = NULL; 3647 int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); 3648 MigThrError thr_error; 3649 bool urgent = false; 3650 Error *local_err = NULL; 3651 int ret; 3652 3653 thread = migration_threads_add(MIGRATION_THREAD_SRC_MAIN, 3654 qemu_get_thread_id()); 3655 3656 rcu_register_thread(); 3657 3658 update_iteration_initial_status(s); 3659 3660 if (!multifd_send_setup()) { 3661 goto out; 3662 } 3663 3664 bql_lock(); 3665 qemu_savevm_state_header(s->to_dst_file); 3666 bql_unlock(); 3667 3668 /* 3669 * If we opened the return path, we need to make sure dst has it 3670 * opened as well. 3671 */ 3672 if (s->rp_state.rp_thread_created) { 3673 /* Now tell the dest that it should open its end so it can reply */ 3674 qemu_savevm_send_open_return_path(s->to_dst_file); 3675 3676 /* And do a ping that will make stuff easier to debug */ 3677 qemu_savevm_send_ping(s->to_dst_file, 1); 3678 } 3679 3680 if (migrate_postcopy()) { 3681 /* 3682 * Tell the destination that we *might* want to do postcopy later; 3683 * if the other end can't do postcopy it should fail now, nice and 3684 * early. 3685 */ 3686 qemu_savevm_send_postcopy_advise(s->to_dst_file); 3687 } 3688 3689 if (migrate_colo()) { 3690 /* Notify migration destination that we enable COLO */ 3691 qemu_savevm_send_colo_enable(s->to_dst_file); 3692 } 3693 3694 if (migrate_auto_converge()) { 3695 /* Start RAMBlock dirty bitmap sync timer */ 3696 cpu_throttle_dirty_sync_timer(true); 3697 } 3698 3699 bql_lock(); 3700 ret = qemu_savevm_state_setup(s->to_dst_file, &local_err); 3701 bql_unlock(); 3702 3703 qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP, 3704 MIGRATION_STATUS_ACTIVE); 3705 3706 /* 3707 * Handle SETUP failures after waiting for virtio-net-failover 3708 * devices to unplug. This to preserve migration state transitions. 3709 */ 3710 if (ret) { 3711 migrate_set_error(s, local_err); 3712 error_free(local_err); 3713 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, 3714 MIGRATION_STATUS_FAILED); 3715 goto out; 3716 } 3717 3718 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; 3719 3720 trace_migration_thread_setup_complete(); 3721 3722 while (migration_is_active()) { 3723 if (urgent || !migration_rate_exceeded(s->to_dst_file)) { 3724 MigIterateState iter_state = migration_iteration_run(s); 3725 if (iter_state == MIG_ITERATE_SKIP) { 3726 continue; 3727 } else if (iter_state == MIG_ITERATE_BREAK) { 3728 break; 3729 } 3730 } 3731 3732 /* 3733 * Try to detect any kind of failures, and see whether we 3734 * should stop the migration now. 3735 */ 3736 thr_error = migration_detect_error(s); 3737 if (thr_error == MIG_THR_ERR_FATAL) { 3738 /* Stop migration */ 3739 break; 3740 } else if (thr_error == MIG_THR_ERR_RECOVERED) { 3741 /* 3742 * Just recovered from a e.g. network failure, reset all 3743 * the local variables. This is important to avoid 3744 * breaking transferred_bytes and bandwidth calculation 3745 */ 3746 update_iteration_initial_status(s); 3747 } 3748 3749 urgent = migration_rate_limit(); 3750 } 3751 3752 out: 3753 trace_migration_thread_after_loop(); 3754 migration_iteration_finish(s); 3755 object_unref(OBJECT(s)); 3756 rcu_unregister_thread(); 3757 migration_threads_remove(thread); 3758 return NULL; 3759 } 3760 3761 static void bg_migration_vm_start_bh(void *opaque) 3762 { 3763 MigrationState *s = opaque; 3764 3765 vm_resume(s->vm_old_state); 3766 migration_downtime_end(s); 3767 } 3768 3769 /** 3770 * Background snapshot thread, based on live migration code. 3771 * This is an alternative implementation of live migration mechanism 3772 * introduced specifically to support background snapshots. 3773 * 3774 * It takes advantage of userfault_fd write protection mechanism introduced 3775 * in v5.7 kernel. Compared to existing dirty page logging migration much 3776 * lesser stream traffic is produced resulting in smaller snapshot images, 3777 * simply cause of no page duplicates can get into the stream. 3778 * 3779 * Another key point is that generated vmstate stream reflects machine state 3780 * 'frozen' at the beginning of snapshot creation compared to dirty page logging 3781 * mechanism, which effectively results in that saved snapshot is the state of VM 3782 * at the end of the process. 3783 */ 3784 static void *bg_migration_thread(void *opaque) 3785 { 3786 MigrationState *s = opaque; 3787 int64_t setup_start; 3788 MigThrError thr_error; 3789 QEMUFile *fb; 3790 bool early_fail = true; 3791 Error *local_err = NULL; 3792 int ret; 3793 3794 rcu_register_thread(); 3795 3796 migration_rate_set(RATE_LIMIT_DISABLED); 3797 3798 setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); 3799 /* 3800 * We want to save vmstate for the moment when migration has been 3801 * initiated but also we want to save RAM content while VM is running. 3802 * The RAM content should appear first in the vmstate. So, we first 3803 * stash the non-RAM part of the vmstate to the temporary buffer, 3804 * then write RAM part of the vmstate to the migration stream 3805 * with vCPUs running and, finally, write stashed non-RAM part of 3806 * the vmstate from the buffer to the migration stream. 3807 */ 3808 s->bioc = qio_channel_buffer_new(512 * 1024); 3809 qio_channel_set_name(QIO_CHANNEL(s->bioc), "vmstate-buffer"); 3810 fb = qemu_file_new_output(QIO_CHANNEL(s->bioc)); 3811 object_unref(OBJECT(s->bioc)); 3812 3813 update_iteration_initial_status(s); 3814 3815 /* 3816 * Prepare for tracking memory writes with UFFD-WP - populate 3817 * RAM pages before protecting. 3818 */ 3819 #ifdef __linux__ 3820 ram_write_tracking_prepare(); 3821 #endif 3822 3823 bql_lock(); 3824 qemu_savevm_state_header(s->to_dst_file); 3825 ret = qemu_savevm_state_setup(s->to_dst_file, &local_err); 3826 bql_unlock(); 3827 3828 qemu_savevm_wait_unplug(s, MIGRATION_STATUS_SETUP, 3829 MIGRATION_STATUS_ACTIVE); 3830 3831 /* 3832 * Handle SETUP failures after waiting for virtio-net-failover 3833 * devices to unplug. This to preserve migration state transitions. 3834 */ 3835 if (ret) { 3836 migrate_set_error(s, local_err); 3837 error_free(local_err); 3838 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, 3839 MIGRATION_STATUS_FAILED); 3840 goto fail_setup; 3841 } 3842 3843 s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; 3844 3845 trace_migration_thread_setup_complete(); 3846 3847 bql_lock(); 3848 3849 if (migration_stop_vm(s, RUN_STATE_PAUSED)) { 3850 goto fail; 3851 } 3852 3853 if (qemu_savevm_state_complete_precopy_non_iterable(fb, false)) { 3854 goto fail; 3855 } 3856 /* 3857 * Since we are going to get non-iterable state data directly 3858 * from s->bioc->data, explicit flush is needed here. 3859 */ 3860 qemu_fflush(fb); 3861 3862 /* Now initialize UFFD context and start tracking RAM writes */ 3863 if (ram_write_tracking_start()) { 3864 goto fail; 3865 } 3866 early_fail = false; 3867 3868 /* 3869 * Start VM from BH handler to avoid write-fault lock here. 3870 * UFFD-WP protection for the whole RAM is already enabled so 3871 * calling VM state change notifiers from vm_start() would initiate 3872 * writes to virtio VQs memory which is in write-protected region. 3873 */ 3874 migration_bh_schedule(bg_migration_vm_start_bh, s); 3875 bql_unlock(); 3876 3877 while (migration_is_active()) { 3878 MigIterateState iter_state = bg_migration_iteration_run(s); 3879 if (iter_state == MIG_ITERATE_SKIP) { 3880 continue; 3881 } else if (iter_state == MIG_ITERATE_BREAK) { 3882 break; 3883 } 3884 3885 /* 3886 * Try to detect any kind of failures, and see whether we 3887 * should stop the migration now. 3888 */ 3889 thr_error = migration_detect_error(s); 3890 if (thr_error == MIG_THR_ERR_FATAL) { 3891 /* Stop migration */ 3892 break; 3893 } 3894 3895 migration_update_counters(s, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)); 3896 } 3897 3898 trace_migration_thread_after_loop(); 3899 3900 fail: 3901 if (early_fail) { 3902 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE, 3903 MIGRATION_STATUS_FAILED); 3904 bql_unlock(); 3905 } 3906 3907 fail_setup: 3908 bg_migration_iteration_finish(s); 3909 3910 qemu_fclose(fb); 3911 object_unref(OBJECT(s)); 3912 rcu_unregister_thread(); 3913 3914 return NULL; 3915 } 3916 3917 void migration_connect(MigrationState *s, Error *error_in) 3918 { 3919 Error *local_err = NULL; 3920 uint64_t rate_limit; 3921 bool resume = (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP); 3922 int ret; 3923 3924 /* 3925 * If there's a previous error, free it and prepare for another one. 3926 * Meanwhile if migration completes successfully, there won't have an error 3927 * dumped when calling migration_cleanup(). 3928 */ 3929 migrate_error_free(s); 3930 3931 s->expected_downtime = migrate_downtime_limit(); 3932 if (error_in) { 3933 migration_connect_set_error(s, error_in); 3934 if (resume) { 3935 /* 3936 * Don't do cleanup for resume if channel is invalid, but only dump 3937 * the error. We wait for another channel connect from the user. 3938 * The error_report still gives HMP user a hint on what failed. 3939 * It's normally done in migration_cleanup(), but call it here 3940 * explicitly. 3941 */ 3942 error_report_err(error_copy(s->error)); 3943 } else { 3944 migration_cleanup(s); 3945 } 3946 return; 3947 } 3948 3949 if (resume) { 3950 /* This is a resumed migration */ 3951 rate_limit = migrate_max_postcopy_bandwidth(); 3952 } else { 3953 /* This is a fresh new migration */ 3954 rate_limit = migrate_max_bandwidth(); 3955 3956 /* Notify before starting migration thread */ 3957 if (migration_call_notifiers(s, MIG_EVENT_PRECOPY_SETUP, &local_err)) { 3958 goto fail; 3959 } 3960 } 3961 3962 migration_rate_set(rate_limit); 3963 qemu_file_set_blocking(s->to_dst_file, true); 3964 3965 /* 3966 * Open the return path. For postcopy, it is used exclusively. For 3967 * precopy, only if user specified "return-path" capability would 3968 * QEMU uses the return path. 3969 */ 3970 if (migrate_postcopy_ram() || migrate_return_path()) { 3971 if (open_return_path_on_source(s)) { 3972 error_setg(&local_err, "Unable to open return-path for postcopy"); 3973 goto fail; 3974 } 3975 } 3976 3977 /* 3978 * This needs to be done before resuming a postcopy. Note: for newer 3979 * QEMUs we will delay the channel creation until postcopy_start(), to 3980 * avoid disorder of channel creations. 3981 */ 3982 if (migrate_postcopy_preempt() && s->preempt_pre_7_2) { 3983 postcopy_preempt_setup(s); 3984 } 3985 3986 if (resume) { 3987 /* Wakeup the main migration thread to do the recovery */ 3988 migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER_SETUP, 3989 MIGRATION_STATUS_POSTCOPY_RECOVER); 3990 qemu_sem_post(&s->postcopy_pause_sem); 3991 return; 3992 } 3993 3994 if (migrate_mode_is_cpr(s)) { 3995 ret = migration_stop_vm(s, RUN_STATE_FINISH_MIGRATE); 3996 if (ret < 0) { 3997 error_setg(&local_err, "migration_stop_vm failed, error %d", -ret); 3998 goto fail; 3999 } 4000 } 4001 4002 /* 4003 * Take a refcount to make sure the migration object won't get freed by 4004 * the main thread already in migration_shutdown(). 4005 * 4006 * The refcount will be released at the end of the thread function. 4007 */ 4008 object_ref(OBJECT(s)); 4009 4010 if (migrate_background_snapshot()) { 4011 qemu_thread_create(&s->thread, MIGRATION_THREAD_SNAPSHOT, 4012 bg_migration_thread, s, QEMU_THREAD_JOINABLE); 4013 } else { 4014 qemu_thread_create(&s->thread, MIGRATION_THREAD_SRC_MAIN, 4015 migration_thread, s, QEMU_THREAD_JOINABLE); 4016 } 4017 s->migration_thread_running = true; 4018 return; 4019 4020 fail: 4021 migrate_set_error(s, local_err); 4022 if (s->state != MIGRATION_STATUS_CANCELLING) { 4023 migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED); 4024 } 4025 error_report_err(local_err); 4026 migration_cleanup(s); 4027 } 4028 4029 static void migration_class_init(ObjectClass *klass, const void *data) 4030 { 4031 DeviceClass *dc = DEVICE_CLASS(klass); 4032 4033 dc->user_creatable = false; 4034 device_class_set_props_n(dc, migration_properties, 4035 migration_properties_count); 4036 } 4037 4038 static void migration_instance_finalize(Object *obj) 4039 { 4040 MigrationState *ms = MIGRATION_OBJ(obj); 4041 4042 qemu_mutex_destroy(&ms->error_mutex); 4043 qemu_mutex_destroy(&ms->qemu_file_lock); 4044 qemu_sem_destroy(&ms->wait_unplug_sem); 4045 qemu_sem_destroy(&ms->rate_limit_sem); 4046 qemu_sem_destroy(&ms->pause_sem); 4047 qemu_sem_destroy(&ms->postcopy_pause_sem); 4048 qemu_sem_destroy(&ms->rp_state.rp_sem); 4049 qemu_sem_destroy(&ms->rp_state.rp_pong_acks); 4050 qemu_sem_destroy(&ms->postcopy_qemufile_src_sem); 4051 error_free(ms->error); 4052 } 4053 4054 static void migration_instance_init(Object *obj) 4055 { 4056 MigrationState *ms = MIGRATION_OBJ(obj); 4057 4058 ms->state = MIGRATION_STATUS_NONE; 4059 ms->mbps = -1; 4060 ms->pages_per_second = -1; 4061 qemu_sem_init(&ms->pause_sem, 0); 4062 qemu_mutex_init(&ms->error_mutex); 4063 4064 migrate_params_init(&ms->parameters); 4065 4066 qemu_sem_init(&ms->postcopy_pause_sem, 0); 4067 qemu_sem_init(&ms->rp_state.rp_sem, 0); 4068 qemu_sem_init(&ms->rp_state.rp_pong_acks, 0); 4069 qemu_sem_init(&ms->rate_limit_sem, 0); 4070 qemu_sem_init(&ms->wait_unplug_sem, 0); 4071 qemu_sem_init(&ms->postcopy_qemufile_src_sem, 0); 4072 qemu_mutex_init(&ms->qemu_file_lock); 4073 } 4074 4075 /* 4076 * Return true if check pass, false otherwise. Error will be put 4077 * inside errp if provided. 4078 */ 4079 static bool migration_object_check(MigrationState *ms, Error **errp) 4080 { 4081 /* Assuming all off */ 4082 bool old_caps[MIGRATION_CAPABILITY__MAX] = { 0 }; 4083 4084 if (!migrate_params_check(&ms->parameters, errp)) { 4085 return false; 4086 } 4087 4088 return migrate_caps_check(old_caps, ms->capabilities, errp); 4089 } 4090 4091 static const TypeInfo migration_type = { 4092 .name = TYPE_MIGRATION, 4093 /* 4094 * NOTE: TYPE_MIGRATION is not really a device, as the object is 4095 * not created using qdev_new(), it is not attached to the qdev 4096 * device tree, and it is never realized. 4097 * 4098 * TODO: Make this TYPE_OBJECT once QOM provides something like 4099 * TYPE_DEVICE's "-global" properties. 4100 */ 4101 .parent = TYPE_DEVICE, 4102 .class_init = migration_class_init, 4103 .class_size = sizeof(MigrationClass), 4104 .instance_size = sizeof(MigrationState), 4105 .instance_init = migration_instance_init, 4106 .instance_finalize = migration_instance_finalize, 4107 }; 4108 4109 static void register_migration_types(void) 4110 { 4111 type_register_static(&migration_type); 4112 } 4113 4114 type_init(register_migration_types); 4115