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