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