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