1 /* 2 * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO) 3 * (a.k.a. Fault Tolerance or Continuous Replication) 4 * 5 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. 6 * Copyright (c) 2016 FUJITSU LIMITED 7 * Copyright (c) 2016 Intel Corporation 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or 10 * later. See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "sysemu/sysemu.h" 15 #include "qapi/error.h" 16 #include "qapi/qapi-commands-migration.h" 17 #include "migration.h" 18 #include "qemu-file.h" 19 #include "savevm.h" 20 #include "migration/colo.h" 21 #include "block.h" 22 #include "io/channel-buffer.h" 23 #include "trace.h" 24 #include "qemu/error-report.h" 25 #include "qemu/main-loop.h" 26 #include "qemu/rcu.h" 27 #include "migration/failover.h" 28 #include "migration/ram.h" 29 #ifdef CONFIG_REPLICATION 30 #include "block/replication.h" 31 #endif 32 #include "net/colo-compare.h" 33 #include "net/colo.h" 34 #include "block/block.h" 35 #include "qapi/qapi-events-migration.h" 36 #include "sysemu/cpus.h" 37 #include "sysemu/runstate.h" 38 #include "net/filter.h" 39 40 static bool vmstate_loading; 41 static Notifier packets_compare_notifier; 42 43 /* User need to know colo mode after COLO failover */ 44 static COLOMode last_colo_mode; 45 46 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024) 47 48 bool migration_in_colo_state(void) 49 { 50 MigrationState *s = migrate_get_current(); 51 52 return (s->state == MIGRATION_STATUS_COLO); 53 } 54 55 bool migration_incoming_in_colo_state(void) 56 { 57 MigrationIncomingState *mis = migration_incoming_get_current(); 58 59 return mis && (mis->state == MIGRATION_STATUS_COLO); 60 } 61 62 static bool colo_runstate_is_stopped(void) 63 { 64 return runstate_check(RUN_STATE_COLO) || !runstate_is_running(); 65 } 66 67 static void secondary_vm_do_failover(void) 68 { 69 /* COLO needs enable block-replication */ 70 #ifdef CONFIG_REPLICATION 71 int old_state; 72 MigrationIncomingState *mis = migration_incoming_get_current(); 73 Error *local_err = NULL; 74 75 /* Can not do failover during the process of VM's loading VMstate, Or 76 * it will break the secondary VM. 77 */ 78 if (vmstate_loading) { 79 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE, 80 FAILOVER_STATUS_RELAUNCH); 81 if (old_state != FAILOVER_STATUS_ACTIVE) { 82 error_report("Unknown error while do failover for secondary VM," 83 "old_state: %s", FailoverStatus_str(old_state)); 84 } 85 return; 86 } 87 88 migrate_set_state(&mis->state, MIGRATION_STATUS_COLO, 89 MIGRATION_STATUS_COMPLETED); 90 91 replication_stop_all(true, &local_err); 92 if (local_err) { 93 error_report_err(local_err); 94 local_err = NULL; 95 } 96 97 /* Notify all filters of all NIC to do checkpoint */ 98 colo_notify_filters_event(COLO_EVENT_FAILOVER, &local_err); 99 if (local_err) { 100 error_report_err(local_err); 101 } 102 103 if (!autostart) { 104 error_report("\"-S\" qemu option will be ignored in secondary side"); 105 /* recover runstate to normal migration finish state */ 106 autostart = true; 107 } 108 /* 109 * Make sure COLO incoming thread not block in recv or send, 110 * If mis->from_src_file and mis->to_src_file use the same fd, 111 * The second shutdown() will return -1, we ignore this value, 112 * It is harmless. 113 */ 114 if (mis->from_src_file) { 115 qemu_file_shutdown(mis->from_src_file); 116 } 117 if (mis->to_src_file) { 118 qemu_file_shutdown(mis->to_src_file); 119 } 120 121 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE, 122 FAILOVER_STATUS_COMPLETED); 123 if (old_state != FAILOVER_STATUS_ACTIVE) { 124 error_report("Incorrect state (%s) while doing failover for " 125 "secondary VM", FailoverStatus_str(old_state)); 126 return; 127 } 128 /* Notify COLO incoming thread that failover work is finished */ 129 qemu_sem_post(&mis->colo_incoming_sem); 130 131 /* For Secondary VM, jump to incoming co */ 132 if (mis->migration_incoming_co) { 133 qemu_coroutine_enter(mis->migration_incoming_co); 134 } 135 #else 136 abort(); 137 #endif 138 } 139 140 static void primary_vm_do_failover(void) 141 { 142 #ifdef CONFIG_REPLICATION 143 MigrationState *s = migrate_get_current(); 144 int old_state; 145 Error *local_err = NULL; 146 147 migrate_set_state(&s->state, MIGRATION_STATUS_COLO, 148 MIGRATION_STATUS_COMPLETED); 149 /* 150 * kick COLO thread which might wait at 151 * qemu_sem_wait(&s->colo_checkpoint_sem). 152 */ 153 colo_checkpoint_notify(s); 154 155 /* 156 * Wake up COLO thread which may blocked in recv() or send(), 157 * The s->rp_state.from_dst_file and s->to_dst_file may use the 158 * same fd, but we still shutdown the fd for twice, it is harmless. 159 */ 160 if (s->to_dst_file) { 161 qemu_file_shutdown(s->to_dst_file); 162 } 163 if (s->rp_state.from_dst_file) { 164 qemu_file_shutdown(s->rp_state.from_dst_file); 165 } 166 167 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE, 168 FAILOVER_STATUS_COMPLETED); 169 if (old_state != FAILOVER_STATUS_ACTIVE) { 170 error_report("Incorrect state (%s) while doing failover for Primary VM", 171 FailoverStatus_str(old_state)); 172 return; 173 } 174 175 replication_stop_all(true, &local_err); 176 if (local_err) { 177 error_report_err(local_err); 178 local_err = NULL; 179 } 180 181 /* Notify COLO thread that failover work is finished */ 182 qemu_sem_post(&s->colo_exit_sem); 183 #else 184 abort(); 185 #endif 186 } 187 188 COLOMode get_colo_mode(void) 189 { 190 if (migration_in_colo_state()) { 191 return COLO_MODE_PRIMARY; 192 } else if (migration_incoming_in_colo_state()) { 193 return COLO_MODE_SECONDARY; 194 } else { 195 return COLO_MODE_NONE; 196 } 197 } 198 199 void colo_do_failover(void) 200 { 201 /* Make sure VM stopped while failover happened. */ 202 if (!colo_runstate_is_stopped()) { 203 vm_stop_force_state(RUN_STATE_COLO); 204 } 205 206 switch (last_colo_mode = get_colo_mode()) { 207 case COLO_MODE_PRIMARY: 208 primary_vm_do_failover(); 209 break; 210 case COLO_MODE_SECONDARY: 211 secondary_vm_do_failover(); 212 break; 213 default: 214 error_report("colo_do_failover failed because the colo mode" 215 " could not be obtained"); 216 } 217 } 218 219 #ifdef CONFIG_REPLICATION 220 void qmp_xen_set_replication(bool enable, bool primary, 221 bool has_failover, bool failover, 222 Error **errp) 223 { 224 ReplicationMode mode = primary ? 225 REPLICATION_MODE_PRIMARY : 226 REPLICATION_MODE_SECONDARY; 227 228 if (has_failover && enable) { 229 error_setg(errp, "Parameter 'failover' is only for" 230 " stopping replication"); 231 return; 232 } 233 234 if (enable) { 235 replication_start_all(mode, errp); 236 } else { 237 if (!has_failover) { 238 failover = NULL; 239 } 240 replication_stop_all(failover, failover ? NULL : errp); 241 } 242 } 243 244 ReplicationStatus *qmp_query_xen_replication_status(Error **errp) 245 { 246 Error *err = NULL; 247 ReplicationStatus *s = g_new0(ReplicationStatus, 1); 248 249 replication_get_error_all(&err); 250 if (err) { 251 s->error = true; 252 s->desc = g_strdup(error_get_pretty(err)); 253 } else { 254 s->error = false; 255 } 256 257 error_free(err); 258 return s; 259 } 260 261 void qmp_xen_colo_do_checkpoint(Error **errp) 262 { 263 Error *err = NULL; 264 265 replication_do_checkpoint_all(&err); 266 if (err) { 267 error_propagate(errp, err); 268 return; 269 } 270 /* Notify all filters of all NIC to do checkpoint */ 271 colo_notify_filters_event(COLO_EVENT_CHECKPOINT, errp); 272 } 273 #endif 274 275 COLOStatus *qmp_query_colo_status(Error **errp) 276 { 277 COLOStatus *s = g_new0(COLOStatus, 1); 278 279 s->mode = get_colo_mode(); 280 s->last_mode = last_colo_mode; 281 282 switch (failover_get_state()) { 283 case FAILOVER_STATUS_NONE: 284 s->reason = COLO_EXIT_REASON_NONE; 285 break; 286 case FAILOVER_STATUS_COMPLETED: 287 s->reason = COLO_EXIT_REASON_REQUEST; 288 break; 289 default: 290 if (migration_in_colo_state()) { 291 s->reason = COLO_EXIT_REASON_PROCESSING; 292 } else { 293 s->reason = COLO_EXIT_REASON_ERROR; 294 } 295 } 296 297 return s; 298 } 299 300 static void colo_send_message(QEMUFile *f, COLOMessage msg, 301 Error **errp) 302 { 303 int ret; 304 305 if (msg >= COLO_MESSAGE__MAX) { 306 error_setg(errp, "%s: Invalid message", __func__); 307 return; 308 } 309 qemu_put_be32(f, msg); 310 qemu_fflush(f); 311 312 ret = qemu_file_get_error(f); 313 if (ret < 0) { 314 error_setg_errno(errp, -ret, "Can't send COLO message"); 315 } 316 trace_colo_send_message(COLOMessage_str(msg)); 317 } 318 319 static void colo_send_message_value(QEMUFile *f, COLOMessage msg, 320 uint64_t value, Error **errp) 321 { 322 Error *local_err = NULL; 323 int ret; 324 325 colo_send_message(f, msg, &local_err); 326 if (local_err) { 327 error_propagate(errp, local_err); 328 return; 329 } 330 qemu_put_be64(f, value); 331 qemu_fflush(f); 332 333 ret = qemu_file_get_error(f); 334 if (ret < 0) { 335 error_setg_errno(errp, -ret, "Failed to send value for message:%s", 336 COLOMessage_str(msg)); 337 } 338 } 339 340 static COLOMessage colo_receive_message(QEMUFile *f, Error **errp) 341 { 342 COLOMessage msg; 343 int ret; 344 345 msg = qemu_get_be32(f); 346 ret = qemu_file_get_error(f); 347 if (ret < 0) { 348 error_setg_errno(errp, -ret, "Can't receive COLO message"); 349 return msg; 350 } 351 if (msg >= COLO_MESSAGE__MAX) { 352 error_setg(errp, "%s: Invalid message", __func__); 353 return msg; 354 } 355 trace_colo_receive_message(COLOMessage_str(msg)); 356 return msg; 357 } 358 359 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg, 360 Error **errp) 361 { 362 COLOMessage msg; 363 Error *local_err = NULL; 364 365 msg = colo_receive_message(f, &local_err); 366 if (local_err) { 367 error_propagate(errp, local_err); 368 return; 369 } 370 if (msg != expect_msg) { 371 error_setg(errp, "Unexpected COLO message %d, expected %d", 372 msg, expect_msg); 373 } 374 } 375 376 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg, 377 Error **errp) 378 { 379 Error *local_err = NULL; 380 uint64_t value; 381 int ret; 382 383 colo_receive_check_message(f, expect_msg, &local_err); 384 if (local_err) { 385 error_propagate(errp, local_err); 386 return 0; 387 } 388 389 value = qemu_get_be64(f); 390 ret = qemu_file_get_error(f); 391 if (ret < 0) { 392 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s", 393 COLOMessage_str(expect_msg)); 394 } 395 return value; 396 } 397 398 static int colo_do_checkpoint_transaction(MigrationState *s, 399 QIOChannelBuffer *bioc, 400 QEMUFile *fb) 401 { 402 Error *local_err = NULL; 403 int ret = -1; 404 405 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST, 406 &local_err); 407 if (local_err) { 408 goto out; 409 } 410 411 colo_receive_check_message(s->rp_state.from_dst_file, 412 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err); 413 if (local_err) { 414 goto out; 415 } 416 /* Reset channel-buffer directly */ 417 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL); 418 bioc->usage = 0; 419 420 qemu_mutex_lock_iothread(); 421 if (failover_get_state() != FAILOVER_STATUS_NONE) { 422 qemu_mutex_unlock_iothread(); 423 goto out; 424 } 425 vm_stop_force_state(RUN_STATE_COLO); 426 qemu_mutex_unlock_iothread(); 427 trace_colo_vm_state_change("run", "stop"); 428 /* 429 * Failover request bh could be called after vm_stop_force_state(), 430 * So we need check failover_request_is_active() again. 431 */ 432 if (failover_get_state() != FAILOVER_STATUS_NONE) { 433 goto out; 434 } 435 qemu_mutex_lock_iothread(); 436 437 #ifdef CONFIG_REPLICATION 438 replication_do_checkpoint_all(&local_err); 439 if (local_err) { 440 qemu_mutex_unlock_iothread(); 441 goto out; 442 } 443 #else 444 abort(); 445 #endif 446 447 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err); 448 if (local_err) { 449 qemu_mutex_unlock_iothread(); 450 goto out; 451 } 452 /* Note: device state is saved into buffer */ 453 ret = qemu_save_device_state(fb); 454 455 qemu_mutex_unlock_iothread(); 456 if (ret < 0) { 457 goto out; 458 } 459 460 if (migrate_auto_converge()) { 461 mig_throttle_counter_reset(); 462 } 463 /* 464 * Only save VM's live state, which not including device state. 465 * TODO: We may need a timeout mechanism to prevent COLO process 466 * to be blocked here. 467 */ 468 qemu_savevm_live_state(s->to_dst_file); 469 470 qemu_fflush(fb); 471 472 /* 473 * We need the size of the VMstate data in Secondary side, 474 * With which we can decide how much data should be read. 475 */ 476 colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE, 477 bioc->usage, &local_err); 478 if (local_err) { 479 goto out; 480 } 481 482 qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage); 483 qemu_fflush(s->to_dst_file); 484 ret = qemu_file_get_error(s->to_dst_file); 485 if (ret < 0) { 486 goto out; 487 } 488 489 colo_receive_check_message(s->rp_state.from_dst_file, 490 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err); 491 if (local_err) { 492 goto out; 493 } 494 495 qemu_event_reset(&s->colo_checkpoint_event); 496 colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err); 497 if (local_err) { 498 goto out; 499 } 500 501 colo_receive_check_message(s->rp_state.from_dst_file, 502 COLO_MESSAGE_VMSTATE_LOADED, &local_err); 503 if (local_err) { 504 goto out; 505 } 506 507 ret = 0; 508 509 qemu_mutex_lock_iothread(); 510 vm_start(); 511 qemu_mutex_unlock_iothread(); 512 trace_colo_vm_state_change("stop", "run"); 513 514 out: 515 if (local_err) { 516 error_report_err(local_err); 517 } 518 return ret; 519 } 520 521 static void colo_compare_notify_checkpoint(Notifier *notifier, void *data) 522 { 523 colo_checkpoint_notify(data); 524 } 525 526 static void colo_process_checkpoint(MigrationState *s) 527 { 528 QIOChannelBuffer *bioc; 529 QEMUFile *fb = NULL; 530 Error *local_err = NULL; 531 int ret; 532 533 if (get_colo_mode() != COLO_MODE_PRIMARY) { 534 error_report("COLO mode must be COLO_MODE_PRIMARY"); 535 return; 536 } 537 538 failover_init_state(); 539 540 s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file); 541 if (!s->rp_state.from_dst_file) { 542 error_report("Open QEMUFile from_dst_file failed"); 543 goto out; 544 } 545 546 packets_compare_notifier.notify = colo_compare_notify_checkpoint; 547 colo_compare_register_notifier(&packets_compare_notifier); 548 549 /* 550 * Wait for Secondary finish loading VM states and enter COLO 551 * restore. 552 */ 553 colo_receive_check_message(s->rp_state.from_dst_file, 554 COLO_MESSAGE_CHECKPOINT_READY, &local_err); 555 if (local_err) { 556 goto out; 557 } 558 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE); 559 fb = qemu_file_new_output(QIO_CHANNEL(bioc)); 560 object_unref(OBJECT(bioc)); 561 562 qemu_mutex_lock_iothread(); 563 #ifdef CONFIG_REPLICATION 564 replication_start_all(REPLICATION_MODE_PRIMARY, &local_err); 565 if (local_err) { 566 qemu_mutex_unlock_iothread(); 567 goto out; 568 } 569 #else 570 abort(); 571 #endif 572 573 vm_start(); 574 qemu_mutex_unlock_iothread(); 575 trace_colo_vm_state_change("stop", "run"); 576 577 timer_mod(s->colo_delay_timer, qemu_clock_get_ms(QEMU_CLOCK_HOST) + 578 s->parameters.x_checkpoint_delay); 579 580 while (s->state == MIGRATION_STATUS_COLO) { 581 if (failover_get_state() != FAILOVER_STATUS_NONE) { 582 error_report("failover request"); 583 goto out; 584 } 585 586 qemu_event_wait(&s->colo_checkpoint_event); 587 588 if (s->state != MIGRATION_STATUS_COLO) { 589 goto out; 590 } 591 ret = colo_do_checkpoint_transaction(s, bioc, fb); 592 if (ret < 0) { 593 goto out; 594 } 595 } 596 597 out: 598 /* Throw the unreported error message after exited from loop */ 599 if (local_err) { 600 error_report_err(local_err); 601 } 602 603 if (fb) { 604 qemu_fclose(fb); 605 } 606 607 /* 608 * There are only two reasons we can get here, some error happened 609 * or the user triggered failover. 610 */ 611 switch (failover_get_state()) { 612 case FAILOVER_STATUS_COMPLETED: 613 qapi_event_send_colo_exit(COLO_MODE_PRIMARY, 614 COLO_EXIT_REASON_REQUEST); 615 break; 616 default: 617 qapi_event_send_colo_exit(COLO_MODE_PRIMARY, 618 COLO_EXIT_REASON_ERROR); 619 } 620 621 /* Hope this not to be too long to wait here */ 622 qemu_sem_wait(&s->colo_exit_sem); 623 qemu_sem_destroy(&s->colo_exit_sem); 624 625 /* 626 * It is safe to unregister notifier after failover finished. 627 * Besides, colo_delay_timer and colo_checkpoint_sem can't be 628 * released before unregister notifier, or there will be use-after-free 629 * error. 630 */ 631 colo_compare_unregister_notifier(&packets_compare_notifier); 632 timer_free(s->colo_delay_timer); 633 qemu_event_destroy(&s->colo_checkpoint_event); 634 635 /* 636 * Must be called after failover BH is completed, 637 * Or the failover BH may shutdown the wrong fd that 638 * re-used by other threads after we release here. 639 */ 640 if (s->rp_state.from_dst_file) { 641 qemu_fclose(s->rp_state.from_dst_file); 642 s->rp_state.from_dst_file = NULL; 643 } 644 } 645 646 void colo_checkpoint_notify(void *opaque) 647 { 648 MigrationState *s = opaque; 649 int64_t next_notify_time; 650 651 qemu_event_set(&s->colo_checkpoint_event); 652 s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST); 653 next_notify_time = s->colo_checkpoint_time + 654 s->parameters.x_checkpoint_delay; 655 timer_mod(s->colo_delay_timer, next_notify_time); 656 } 657 658 void migrate_start_colo_process(MigrationState *s) 659 { 660 qemu_mutex_unlock_iothread(); 661 qemu_event_init(&s->colo_checkpoint_event, false); 662 s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST, 663 colo_checkpoint_notify, s); 664 665 qemu_sem_init(&s->colo_exit_sem, 0); 666 colo_process_checkpoint(s); 667 qemu_mutex_lock_iothread(); 668 } 669 670 static void colo_incoming_process_checkpoint(MigrationIncomingState *mis, 671 QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp) 672 { 673 uint64_t total_size; 674 uint64_t value; 675 Error *local_err = NULL; 676 int ret; 677 678 qemu_mutex_lock_iothread(); 679 vm_stop_force_state(RUN_STATE_COLO); 680 qemu_mutex_unlock_iothread(); 681 trace_colo_vm_state_change("run", "stop"); 682 683 /* FIXME: This is unnecessary for periodic checkpoint mode */ 684 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY, 685 &local_err); 686 if (local_err) { 687 error_propagate(errp, local_err); 688 return; 689 } 690 691 colo_receive_check_message(mis->from_src_file, 692 COLO_MESSAGE_VMSTATE_SEND, &local_err); 693 if (local_err) { 694 error_propagate(errp, local_err); 695 return; 696 } 697 698 qemu_mutex_lock_iothread(); 699 cpu_synchronize_all_states(); 700 ret = qemu_loadvm_state_main(mis->from_src_file, mis); 701 qemu_mutex_unlock_iothread(); 702 703 if (ret < 0) { 704 error_setg(errp, "Load VM's live state (ram) error"); 705 return; 706 } 707 708 value = colo_receive_message_value(mis->from_src_file, 709 COLO_MESSAGE_VMSTATE_SIZE, &local_err); 710 if (local_err) { 711 error_propagate(errp, local_err); 712 return; 713 } 714 715 /* 716 * Read VM device state data into channel buffer, 717 * It's better to re-use the memory allocated. 718 * Here we need to handle the channel buffer directly. 719 */ 720 if (value > bioc->capacity) { 721 bioc->capacity = value; 722 bioc->data = g_realloc(bioc->data, bioc->capacity); 723 } 724 total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value); 725 if (total_size != value) { 726 error_setg(errp, "Got %" PRIu64 " VMState data, less than expected" 727 " %" PRIu64, total_size, value); 728 return; 729 } 730 bioc->usage = total_size; 731 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL); 732 733 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED, 734 &local_err); 735 if (local_err) { 736 error_propagate(errp, local_err); 737 return; 738 } 739 740 qemu_mutex_lock_iothread(); 741 vmstate_loading = true; 742 colo_flush_ram_cache(); 743 ret = qemu_load_device_state(fb); 744 if (ret < 0) { 745 error_setg(errp, "COLO: load device state failed"); 746 vmstate_loading = false; 747 qemu_mutex_unlock_iothread(); 748 return; 749 } 750 751 #ifdef CONFIG_REPLICATION 752 replication_get_error_all(&local_err); 753 if (local_err) { 754 error_propagate(errp, local_err); 755 vmstate_loading = false; 756 qemu_mutex_unlock_iothread(); 757 return; 758 } 759 760 /* discard colo disk buffer */ 761 replication_do_checkpoint_all(&local_err); 762 if (local_err) { 763 error_propagate(errp, local_err); 764 vmstate_loading = false; 765 qemu_mutex_unlock_iothread(); 766 return; 767 } 768 #else 769 abort(); 770 #endif 771 /* Notify all filters of all NIC to do checkpoint */ 772 colo_notify_filters_event(COLO_EVENT_CHECKPOINT, &local_err); 773 774 if (local_err) { 775 error_propagate(errp, local_err); 776 vmstate_loading = false; 777 qemu_mutex_unlock_iothread(); 778 return; 779 } 780 781 vmstate_loading = false; 782 vm_start(); 783 qemu_mutex_unlock_iothread(); 784 trace_colo_vm_state_change("stop", "run"); 785 786 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) { 787 return; 788 } 789 790 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED, 791 &local_err); 792 error_propagate(errp, local_err); 793 } 794 795 static void colo_wait_handle_message(MigrationIncomingState *mis, 796 QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp) 797 { 798 COLOMessage msg; 799 Error *local_err = NULL; 800 801 msg = colo_receive_message(mis->from_src_file, &local_err); 802 if (local_err) { 803 error_propagate(errp, local_err); 804 return; 805 } 806 807 switch (msg) { 808 case COLO_MESSAGE_CHECKPOINT_REQUEST: 809 colo_incoming_process_checkpoint(mis, fb, bioc, errp); 810 break; 811 default: 812 error_setg(errp, "Got unknown COLO message: %d", msg); 813 break; 814 } 815 } 816 817 void colo_shutdown(void) 818 { 819 MigrationIncomingState *mis = NULL; 820 MigrationState *s = NULL; 821 822 switch (get_colo_mode()) { 823 case COLO_MODE_PRIMARY: 824 s = migrate_get_current(); 825 qemu_event_set(&s->colo_checkpoint_event); 826 qemu_sem_post(&s->colo_exit_sem); 827 break; 828 case COLO_MODE_SECONDARY: 829 mis = migration_incoming_get_current(); 830 qemu_sem_post(&mis->colo_incoming_sem); 831 break; 832 default: 833 break; 834 } 835 } 836 837 void *colo_process_incoming_thread(void *opaque) 838 { 839 MigrationIncomingState *mis = opaque; 840 QEMUFile *fb = NULL; 841 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */ 842 Error *local_err = NULL; 843 844 rcu_register_thread(); 845 qemu_sem_init(&mis->colo_incoming_sem, 0); 846 847 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, 848 MIGRATION_STATUS_COLO); 849 850 if (get_colo_mode() != COLO_MODE_SECONDARY) { 851 error_report("COLO mode must be COLO_MODE_SECONDARY"); 852 return NULL; 853 } 854 855 failover_init_state(); 856 857 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file); 858 if (!mis->to_src_file) { 859 error_report("COLO incoming thread: Open QEMUFile to_src_file failed"); 860 goto out; 861 } 862 /* 863 * Note: the communication between Primary side and Secondary side 864 * should be sequential, we set the fd to unblocked in migration incoming 865 * coroutine, and here we are in the COLO incoming thread, so it is ok to 866 * set the fd back to blocked. 867 */ 868 qemu_file_set_blocking(mis->from_src_file, true); 869 870 colo_incoming_start_dirty_log(); 871 872 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE); 873 fb = qemu_file_new_input(QIO_CHANNEL(bioc)); 874 object_unref(OBJECT(bioc)); 875 876 qemu_mutex_lock_iothread(); 877 #ifdef CONFIG_REPLICATION 878 replication_start_all(REPLICATION_MODE_SECONDARY, &local_err); 879 if (local_err) { 880 qemu_mutex_unlock_iothread(); 881 goto out; 882 } 883 #else 884 abort(); 885 #endif 886 vm_start(); 887 qemu_mutex_unlock_iothread(); 888 trace_colo_vm_state_change("stop", "run"); 889 890 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY, 891 &local_err); 892 if (local_err) { 893 goto out; 894 } 895 896 while (mis->state == MIGRATION_STATUS_COLO) { 897 colo_wait_handle_message(mis, fb, bioc, &local_err); 898 if (local_err) { 899 error_report_err(local_err); 900 break; 901 } 902 903 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) { 904 failover_set_state(FAILOVER_STATUS_RELAUNCH, 905 FAILOVER_STATUS_NONE); 906 failover_request_active(NULL); 907 break; 908 } 909 910 if (failover_get_state() != FAILOVER_STATUS_NONE) { 911 error_report("failover request"); 912 break; 913 } 914 } 915 916 out: 917 /* 918 * There are only two reasons we can get here, some error happened 919 * or the user triggered failover. 920 */ 921 switch (failover_get_state()) { 922 case FAILOVER_STATUS_COMPLETED: 923 qapi_event_send_colo_exit(COLO_MODE_SECONDARY, 924 COLO_EXIT_REASON_REQUEST); 925 break; 926 default: 927 qapi_event_send_colo_exit(COLO_MODE_SECONDARY, 928 COLO_EXIT_REASON_ERROR); 929 } 930 931 if (fb) { 932 qemu_fclose(fb); 933 } 934 935 /* Hope this not to be too long to loop here */ 936 qemu_sem_wait(&mis->colo_incoming_sem); 937 qemu_sem_destroy(&mis->colo_incoming_sem); 938 939 rcu_unregister_thread(); 940 return NULL; 941 } 942