]> git.ipfire.org Git - thirdparty/qemu.git/blob - migration/colo.c
COLO: integrate colo compare with colo frame
[thirdparty/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 "migration/failover.h"
27 #include "replication.h"
28 #include "net/colo-compare.h"
29 #include "net/colo.h"
30
31 static bool vmstate_loading;
32 static Notifier packets_compare_notifier;
33
34 #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
35
36 bool migration_in_colo_state(void)
37 {
38 MigrationState *s = migrate_get_current();
39
40 return (s->state == MIGRATION_STATUS_COLO);
41 }
42
43 bool migration_incoming_in_colo_state(void)
44 {
45 MigrationIncomingState *mis = migration_incoming_get_current();
46
47 return mis && (mis->state == MIGRATION_STATUS_COLO);
48 }
49
50 static bool colo_runstate_is_stopped(void)
51 {
52 return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
53 }
54
55 static void secondary_vm_do_failover(void)
56 {
57 int old_state;
58 MigrationIncomingState *mis = migration_incoming_get_current();
59
60 /* Can not do failover during the process of VM's loading VMstate, Or
61 * it will break the secondary VM.
62 */
63 if (vmstate_loading) {
64 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
65 FAILOVER_STATUS_RELAUNCH);
66 if (old_state != FAILOVER_STATUS_ACTIVE) {
67 error_report("Unknown error while do failover for secondary VM,"
68 "old_state: %s", FailoverStatus_str(old_state));
69 }
70 return;
71 }
72
73 migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
74 MIGRATION_STATUS_COMPLETED);
75
76 if (!autostart) {
77 error_report("\"-S\" qemu option will be ignored in secondary side");
78 /* recover runstate to normal migration finish state */
79 autostart = true;
80 }
81 /*
82 * Make sure COLO incoming thread not block in recv or send,
83 * If mis->from_src_file and mis->to_src_file use the same fd,
84 * The second shutdown() will return -1, we ignore this value,
85 * It is harmless.
86 */
87 if (mis->from_src_file) {
88 qemu_file_shutdown(mis->from_src_file);
89 }
90 if (mis->to_src_file) {
91 qemu_file_shutdown(mis->to_src_file);
92 }
93
94 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
95 FAILOVER_STATUS_COMPLETED);
96 if (old_state != FAILOVER_STATUS_ACTIVE) {
97 error_report("Incorrect state (%s) while doing failover for "
98 "secondary VM", FailoverStatus_str(old_state));
99 return;
100 }
101 /* Notify COLO incoming thread that failover work is finished */
102 qemu_sem_post(&mis->colo_incoming_sem);
103 /* For Secondary VM, jump to incoming co */
104 if (mis->migration_incoming_co) {
105 qemu_coroutine_enter(mis->migration_incoming_co);
106 }
107 }
108
109 static void primary_vm_do_failover(void)
110 {
111 MigrationState *s = migrate_get_current();
112 int old_state;
113
114 migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
115 MIGRATION_STATUS_COMPLETED);
116
117 /*
118 * Wake up COLO thread which may blocked in recv() or send(),
119 * The s->rp_state.from_dst_file and s->to_dst_file may use the
120 * same fd, but we still shutdown the fd for twice, it is harmless.
121 */
122 if (s->to_dst_file) {
123 qemu_file_shutdown(s->to_dst_file);
124 }
125 if (s->rp_state.from_dst_file) {
126 qemu_file_shutdown(s->rp_state.from_dst_file);
127 }
128
129 old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
130 FAILOVER_STATUS_COMPLETED);
131 if (old_state != FAILOVER_STATUS_ACTIVE) {
132 error_report("Incorrect state (%s) while doing failover for Primary VM",
133 FailoverStatus_str(old_state));
134 return;
135 }
136 /* Notify COLO thread that failover work is finished */
137 qemu_sem_post(&s->colo_exit_sem);
138 }
139
140 void colo_do_failover(MigrationState *s)
141 {
142 /* Make sure VM stopped while failover happened. */
143 if (!colo_runstate_is_stopped()) {
144 vm_stop_force_state(RUN_STATE_COLO);
145 }
146
147 if (get_colo_mode() == COLO_MODE_PRIMARY) {
148 primary_vm_do_failover();
149 } else {
150 secondary_vm_do_failover();
151 }
152 }
153
154 void qmp_xen_set_replication(bool enable, bool primary,
155 bool has_failover, bool failover,
156 Error **errp)
157 {
158 #ifdef CONFIG_REPLICATION
159 ReplicationMode mode = primary ?
160 REPLICATION_MODE_PRIMARY :
161 REPLICATION_MODE_SECONDARY;
162
163 if (has_failover && enable) {
164 error_setg(errp, "Parameter 'failover' is only for"
165 " stopping replication");
166 return;
167 }
168
169 if (enable) {
170 replication_start_all(mode, errp);
171 } else {
172 if (!has_failover) {
173 failover = NULL;
174 }
175 replication_stop_all(failover, failover ? NULL : errp);
176 }
177 #else
178 abort();
179 #endif
180 }
181
182 ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
183 {
184 #ifdef CONFIG_REPLICATION
185 Error *err = NULL;
186 ReplicationStatus *s = g_new0(ReplicationStatus, 1);
187
188 replication_get_error_all(&err);
189 if (err) {
190 s->error = true;
191 s->has_desc = true;
192 s->desc = g_strdup(error_get_pretty(err));
193 } else {
194 s->error = false;
195 }
196
197 error_free(err);
198 return s;
199 #else
200 abort();
201 #endif
202 }
203
204 void qmp_xen_colo_do_checkpoint(Error **errp)
205 {
206 #ifdef CONFIG_REPLICATION
207 replication_do_checkpoint_all(errp);
208 #else
209 abort();
210 #endif
211 }
212
213 static void colo_send_message(QEMUFile *f, COLOMessage msg,
214 Error **errp)
215 {
216 int ret;
217
218 if (msg >= COLO_MESSAGE__MAX) {
219 error_setg(errp, "%s: Invalid message", __func__);
220 return;
221 }
222 qemu_put_be32(f, msg);
223 qemu_fflush(f);
224
225 ret = qemu_file_get_error(f);
226 if (ret < 0) {
227 error_setg_errno(errp, -ret, "Can't send COLO message");
228 }
229 trace_colo_send_message(COLOMessage_str(msg));
230 }
231
232 static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
233 uint64_t value, Error **errp)
234 {
235 Error *local_err = NULL;
236 int ret;
237
238 colo_send_message(f, msg, &local_err);
239 if (local_err) {
240 error_propagate(errp, local_err);
241 return;
242 }
243 qemu_put_be64(f, value);
244 qemu_fflush(f);
245
246 ret = qemu_file_get_error(f);
247 if (ret < 0) {
248 error_setg_errno(errp, -ret, "Failed to send value for message:%s",
249 COLOMessage_str(msg));
250 }
251 }
252
253 static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
254 {
255 COLOMessage msg;
256 int ret;
257
258 msg = qemu_get_be32(f);
259 ret = qemu_file_get_error(f);
260 if (ret < 0) {
261 error_setg_errno(errp, -ret, "Can't receive COLO message");
262 return msg;
263 }
264 if (msg >= COLO_MESSAGE__MAX) {
265 error_setg(errp, "%s: Invalid message", __func__);
266 return msg;
267 }
268 trace_colo_receive_message(COLOMessage_str(msg));
269 return msg;
270 }
271
272 static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
273 Error **errp)
274 {
275 COLOMessage msg;
276 Error *local_err = NULL;
277
278 msg = colo_receive_message(f, &local_err);
279 if (local_err) {
280 error_propagate(errp, local_err);
281 return;
282 }
283 if (msg != expect_msg) {
284 error_setg(errp, "Unexpected COLO message %d, expected %d",
285 msg, expect_msg);
286 }
287 }
288
289 static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
290 Error **errp)
291 {
292 Error *local_err = NULL;
293 uint64_t value;
294 int ret;
295
296 colo_receive_check_message(f, expect_msg, &local_err);
297 if (local_err) {
298 error_propagate(errp, local_err);
299 return 0;
300 }
301
302 value = qemu_get_be64(f);
303 ret = qemu_file_get_error(f);
304 if (ret < 0) {
305 error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
306 COLOMessage_str(expect_msg));
307 }
308 return value;
309 }
310
311 static int colo_do_checkpoint_transaction(MigrationState *s,
312 QIOChannelBuffer *bioc,
313 QEMUFile *fb)
314 {
315 Error *local_err = NULL;
316 int ret = -1;
317
318 colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
319 &local_err);
320 if (local_err) {
321 goto out;
322 }
323
324 colo_receive_check_message(s->rp_state.from_dst_file,
325 COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
326 if (local_err) {
327 goto out;
328 }
329 /* Reset channel-buffer directly */
330 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
331 bioc->usage = 0;
332
333 qemu_mutex_lock_iothread();
334 if (failover_get_state() != FAILOVER_STATUS_NONE) {
335 qemu_mutex_unlock_iothread();
336 goto out;
337 }
338 vm_stop_force_state(RUN_STATE_COLO);
339 qemu_mutex_unlock_iothread();
340 trace_colo_vm_state_change("run", "stop");
341 /*
342 * Failover request bh could be called after vm_stop_force_state(),
343 * So we need check failover_request_is_active() again.
344 */
345 if (failover_get_state() != FAILOVER_STATUS_NONE) {
346 goto out;
347 }
348
349 colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err);
350 if (local_err) {
351 goto out;
352 }
353
354 /* Disable block migration */
355 migrate_set_block_enabled(false, &local_err);
356 qemu_savevm_state_header(fb);
357 qemu_savevm_state_setup(fb);
358 qemu_mutex_lock_iothread();
359 qemu_savevm_state_complete_precopy(fb, false, false);
360 qemu_mutex_unlock_iothread();
361
362 qemu_fflush(fb);
363
364 colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
365 if (local_err) {
366 goto out;
367 }
368 /*
369 * We need the size of the VMstate data in Secondary side,
370 * With which we can decide how much data should be read.
371 */
372 colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
373 bioc->usage, &local_err);
374 if (local_err) {
375 goto out;
376 }
377
378 qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
379 qemu_fflush(s->to_dst_file);
380 ret = qemu_file_get_error(s->to_dst_file);
381 if (ret < 0) {
382 goto out;
383 }
384
385 colo_receive_check_message(s->rp_state.from_dst_file,
386 COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
387 if (local_err) {
388 goto out;
389 }
390
391 colo_receive_check_message(s->rp_state.from_dst_file,
392 COLO_MESSAGE_VMSTATE_LOADED, &local_err);
393 if (local_err) {
394 goto out;
395 }
396
397 ret = 0;
398
399 qemu_mutex_lock_iothread();
400 vm_start();
401 qemu_mutex_unlock_iothread();
402 trace_colo_vm_state_change("stop", "run");
403
404 out:
405 if (local_err) {
406 error_report_err(local_err);
407 }
408 return ret;
409 }
410
411 static void colo_compare_notify_checkpoint(Notifier *notifier, void *data)
412 {
413 colo_checkpoint_notify(data);
414 }
415
416 static void colo_process_checkpoint(MigrationState *s)
417 {
418 QIOChannelBuffer *bioc;
419 QEMUFile *fb = NULL;
420 int64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
421 Error *local_err = NULL;
422 int ret;
423
424 failover_init_state();
425
426 s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
427 if (!s->rp_state.from_dst_file) {
428 error_report("Open QEMUFile from_dst_file failed");
429 goto out;
430 }
431
432 packets_compare_notifier.notify = colo_compare_notify_checkpoint;
433 colo_compare_register_notifier(&packets_compare_notifier);
434
435 /*
436 * Wait for Secondary finish loading VM states and enter COLO
437 * restore.
438 */
439 colo_receive_check_message(s->rp_state.from_dst_file,
440 COLO_MESSAGE_CHECKPOINT_READY, &local_err);
441 if (local_err) {
442 goto out;
443 }
444 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
445 fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
446 object_unref(OBJECT(bioc));
447
448 qemu_mutex_lock_iothread();
449 vm_start();
450 qemu_mutex_unlock_iothread();
451 trace_colo_vm_state_change("stop", "run");
452
453 timer_mod(s->colo_delay_timer,
454 current_time + s->parameters.x_checkpoint_delay);
455
456 while (s->state == MIGRATION_STATUS_COLO) {
457 if (failover_get_state() != FAILOVER_STATUS_NONE) {
458 error_report("failover request");
459 goto out;
460 }
461
462 qemu_sem_wait(&s->colo_checkpoint_sem);
463
464 ret = colo_do_checkpoint_transaction(s, bioc, fb);
465 if (ret < 0) {
466 goto out;
467 }
468 }
469
470 out:
471 /* Throw the unreported error message after exited from loop */
472 if (local_err) {
473 error_report_err(local_err);
474 }
475
476 if (fb) {
477 qemu_fclose(fb);
478 }
479
480 /* Hope this not to be too long to wait here */
481 qemu_sem_wait(&s->colo_exit_sem);
482 qemu_sem_destroy(&s->colo_exit_sem);
483
484 /*
485 * It is safe to unregister notifier after failover finished.
486 * Besides, colo_delay_timer and colo_checkpoint_sem can't be
487 * released befor unregister notifier, or there will be use-after-free
488 * error.
489 */
490 colo_compare_unregister_notifier(&packets_compare_notifier);
491 timer_del(s->colo_delay_timer);
492 timer_free(s->colo_delay_timer);
493 qemu_sem_destroy(&s->colo_checkpoint_sem);
494
495 /*
496 * Must be called after failover BH is completed,
497 * Or the failover BH may shutdown the wrong fd that
498 * re-used by other threads after we release here.
499 */
500 if (s->rp_state.from_dst_file) {
501 qemu_fclose(s->rp_state.from_dst_file);
502 }
503 }
504
505 void colo_checkpoint_notify(void *opaque)
506 {
507 MigrationState *s = opaque;
508 int64_t next_notify_time;
509
510 qemu_sem_post(&s->colo_checkpoint_sem);
511 s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
512 next_notify_time = s->colo_checkpoint_time +
513 s->parameters.x_checkpoint_delay;
514 timer_mod(s->colo_delay_timer, next_notify_time);
515 }
516
517 void migrate_start_colo_process(MigrationState *s)
518 {
519 qemu_mutex_unlock_iothread();
520 qemu_sem_init(&s->colo_checkpoint_sem, 0);
521 s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST,
522 colo_checkpoint_notify, s);
523
524 qemu_sem_init(&s->colo_exit_sem, 0);
525 migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
526 MIGRATION_STATUS_COLO);
527 colo_process_checkpoint(s);
528 qemu_mutex_lock_iothread();
529 }
530
531 static void colo_wait_handle_message(QEMUFile *f, int *checkpoint_request,
532 Error **errp)
533 {
534 COLOMessage msg;
535 Error *local_err = NULL;
536
537 msg = colo_receive_message(f, &local_err);
538 if (local_err) {
539 error_propagate(errp, local_err);
540 return;
541 }
542
543 switch (msg) {
544 case COLO_MESSAGE_CHECKPOINT_REQUEST:
545 *checkpoint_request = 1;
546 break;
547 default:
548 *checkpoint_request = 0;
549 error_setg(errp, "Got unknown COLO message: %d", msg);
550 break;
551 }
552 }
553
554 void *colo_process_incoming_thread(void *opaque)
555 {
556 MigrationIncomingState *mis = opaque;
557 QEMUFile *fb = NULL;
558 QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
559 uint64_t total_size;
560 uint64_t value;
561 Error *local_err = NULL;
562
563 rcu_register_thread();
564 qemu_sem_init(&mis->colo_incoming_sem, 0);
565
566 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
567 MIGRATION_STATUS_COLO);
568
569 failover_init_state();
570
571 mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
572 if (!mis->to_src_file) {
573 error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
574 goto out;
575 }
576 /*
577 * Note: the communication between Primary side and Secondary side
578 * should be sequential, we set the fd to unblocked in migration incoming
579 * coroutine, and here we are in the COLO incoming thread, so it is ok to
580 * set the fd back to blocked.
581 */
582 qemu_file_set_blocking(mis->from_src_file, true);
583
584 bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
585 fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
586 object_unref(OBJECT(bioc));
587
588 qemu_mutex_lock_iothread();
589 vm_start();
590 trace_colo_vm_state_change("stop", "run");
591 qemu_mutex_unlock_iothread();
592
593 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
594 &local_err);
595 if (local_err) {
596 goto out;
597 }
598
599 while (mis->state == MIGRATION_STATUS_COLO) {
600 int request = 0;
601
602 colo_wait_handle_message(mis->from_src_file, &request, &local_err);
603 if (local_err) {
604 goto out;
605 }
606 assert(request);
607 if (failover_get_state() != FAILOVER_STATUS_NONE) {
608 error_report("failover request");
609 goto out;
610 }
611
612 qemu_mutex_lock_iothread();
613 vm_stop_force_state(RUN_STATE_COLO);
614 trace_colo_vm_state_change("run", "stop");
615 qemu_mutex_unlock_iothread();
616
617 /* FIXME: This is unnecessary for periodic checkpoint mode */
618 colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
619 &local_err);
620 if (local_err) {
621 goto out;
622 }
623
624 colo_receive_check_message(mis->from_src_file,
625 COLO_MESSAGE_VMSTATE_SEND, &local_err);
626 if (local_err) {
627 goto out;
628 }
629
630 value = colo_receive_message_value(mis->from_src_file,
631 COLO_MESSAGE_VMSTATE_SIZE, &local_err);
632 if (local_err) {
633 goto out;
634 }
635
636 /*
637 * Read VM device state data into channel buffer,
638 * It's better to re-use the memory allocated.
639 * Here we need to handle the channel buffer directly.
640 */
641 if (value > bioc->capacity) {
642 bioc->capacity = value;
643 bioc->data = g_realloc(bioc->data, bioc->capacity);
644 }
645 total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
646 if (total_size != value) {
647 error_report("Got %" PRIu64 " VMState data, less than expected"
648 " %" PRIu64, total_size, value);
649 goto out;
650 }
651 bioc->usage = total_size;
652 qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
653
654 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
655 &local_err);
656 if (local_err) {
657 goto out;
658 }
659
660 qemu_mutex_lock_iothread();
661 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
662 vmstate_loading = true;
663 if (qemu_loadvm_state(fb) < 0) {
664 error_report("COLO: loadvm failed");
665 qemu_mutex_unlock_iothread();
666 goto out;
667 }
668
669 vmstate_loading = false;
670 vm_start();
671 trace_colo_vm_state_change("stop", "run");
672 qemu_mutex_unlock_iothread();
673
674 if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
675 failover_set_state(FAILOVER_STATUS_RELAUNCH,
676 FAILOVER_STATUS_NONE);
677 failover_request_active(NULL);
678 goto out;
679 }
680
681 colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
682 &local_err);
683 if (local_err) {
684 goto out;
685 }
686 }
687
688 out:
689 vmstate_loading = false;
690 /* Throw the unreported error message after exited from loop */
691 if (local_err) {
692 error_report_err(local_err);
693 }
694
695 if (fb) {
696 qemu_fclose(fb);
697 }
698
699 /* Hope this not to be too long to loop here */
700 qemu_sem_wait(&mis->colo_incoming_sem);
701 qemu_sem_destroy(&mis->colo_incoming_sem);
702 /* Must be called after failover BH is completed */
703 if (mis->to_src_file) {
704 qemu_fclose(mis->to_src_file);
705 }
706 migration_incoming_exit_colo();
707
708 rcu_unregister_thread();
709 return NULL;
710 }