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