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