2016-10-27 09:42:52 +03:00
|
|
|
/*
|
|
|
|
* COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO)
|
|
|
|
* (a.k.a. Fault Tolerance or Continuous Replication)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
|
|
|
|
* Copyright (c) 2016 FUJITSU LIMITED
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
|
|
* later. See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2016-10-27 09:42:54 +03:00
|
|
|
#include "sysemu/sysemu.h"
|
2017-04-17 20:34:36 +03:00
|
|
|
#include "qemu-file-channel.h"
|
2017-04-24 21:07:27 +03:00
|
|
|
#include "migration.h"
|
2017-04-20 19:52:18 +03:00
|
|
|
#include "qemu-file.h"
|
2017-04-20 15:48:46 +03:00
|
|
|
#include "savevm.h"
|
2016-10-27 09:42:52 +03:00
|
|
|
#include "migration/colo.h"
|
2017-04-21 15:31:22 +03:00
|
|
|
#include "block.h"
|
2016-10-27 09:42:59 +03:00
|
|
|
#include "io/channel-buffer.h"
|
2016-10-27 09:42:54 +03:00
|
|
|
#include "trace.h"
|
2016-10-27 09:42:56 +03:00
|
|
|
#include "qemu/error-report.h"
|
COLO: Add 'x-colo-lost-heartbeat' command to trigger failover
We leave users to choose whatever heartbeat solution they want,
if the heartbeat is lost, or other errors they detect, they can use
experimental command 'x_colo_lost_heartbeat' to tell COLO to do failover,
COLO will do operations accordingly.
For example, if the command is sent to the Primary side,
the Primary side will exit COLO mode, does cleanup work,
and then, PVM will take over the service work. If sent to the Secondary side,
the Secondary side will run failover work, then takes over PVM's service work.
Cc: Luiz Capitulino <lcapitulino@redhat.com>
Cc: Eric Blake <eblake@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Amit Shah <amit.shah@redhat.com>
Signed-off-by: Amit Shah <amit@amitshah.net>
2016-10-27 09:43:03 +03:00
|
|
|
#include "migration/failover.h"
|
2017-02-28 22:01:56 +03:00
|
|
|
#include "replication.h"
|
|
|
|
#include "qmp-commands.h"
|
2016-10-27 09:42:52 +03:00
|
|
|
|
2017-01-17 15:57:44 +03:00
|
|
|
static bool vmstate_loading;
|
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
#define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024)
|
|
|
|
|
2016-10-27 09:42:54 +03:00
|
|
|
bool migration_in_colo_state(void)
|
|
|
|
{
|
|
|
|
MigrationState *s = migrate_get_current();
|
|
|
|
|
|
|
|
return (s->state == MIGRATION_STATUS_COLO);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:55 +03:00
|
|
|
bool migration_incoming_in_colo_state(void)
|
|
|
|
{
|
|
|
|
MigrationIncomingState *mis = migration_incoming_get_current();
|
|
|
|
|
|
|
|
return mis && (mis->state == MIGRATION_STATUS_COLO);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:05 +03:00
|
|
|
static bool colo_runstate_is_stopped(void)
|
|
|
|
{
|
|
|
|
return runstate_check(RUN_STATE_COLO) || !runstate_is_running();
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:06 +03:00
|
|
|
static void secondary_vm_do_failover(void)
|
|
|
|
{
|
|
|
|
int old_state;
|
|
|
|
MigrationIncomingState *mis = migration_incoming_get_current();
|
|
|
|
|
2017-01-17 15:57:44 +03:00
|
|
|
/* Can not do failover during the process of VM's loading VMstate, Or
|
|
|
|
* it will break the secondary VM.
|
|
|
|
*/
|
|
|
|
if (vmstate_loading) {
|
|
|
|
old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
|
|
|
|
FAILOVER_STATUS_RELAUNCH);
|
|
|
|
if (old_state != FAILOVER_STATUS_ACTIVE) {
|
|
|
|
error_report("Unknown error while do failover for secondary VM,"
|
|
|
|
"old_state: %s", FailoverStatus_lookup[old_state]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:06 +03:00
|
|
|
migrate_set_state(&mis->state, MIGRATION_STATUS_COLO,
|
|
|
|
MIGRATION_STATUS_COMPLETED);
|
|
|
|
|
|
|
|
if (!autostart) {
|
|
|
|
error_report("\"-S\" qemu option will be ignored in secondary side");
|
|
|
|
/* recover runstate to normal migration finish state */
|
|
|
|
autostart = true;
|
|
|
|
}
|
2017-01-17 15:57:43 +03:00
|
|
|
/*
|
|
|
|
* Make sure COLO incoming thread not block in recv or send,
|
|
|
|
* If mis->from_src_file and mis->to_src_file use the same fd,
|
|
|
|
* The second shutdown() will return -1, we ignore this value,
|
|
|
|
* It is harmless.
|
|
|
|
*/
|
|
|
|
if (mis->from_src_file) {
|
|
|
|
qemu_file_shutdown(mis->from_src_file);
|
|
|
|
}
|
|
|
|
if (mis->to_src_file) {
|
|
|
|
qemu_file_shutdown(mis->to_src_file);
|
|
|
|
}
|
2016-10-27 09:43:06 +03:00
|
|
|
|
|
|
|
old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
|
|
|
|
FAILOVER_STATUS_COMPLETED);
|
|
|
|
if (old_state != FAILOVER_STATUS_ACTIVE) {
|
|
|
|
error_report("Incorrect state (%s) while doing failover for "
|
|
|
|
"secondary VM", FailoverStatus_lookup[old_state]);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-17 15:57:43 +03:00
|
|
|
/* Notify COLO incoming thread that failover work is finished */
|
|
|
|
qemu_sem_post(&mis->colo_incoming_sem);
|
2016-10-27 09:43:06 +03:00
|
|
|
/* For Secondary VM, jump to incoming co */
|
|
|
|
if (mis->migration_incoming_co) {
|
|
|
|
qemu_coroutine_enter(mis->migration_incoming_co);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:05 +03:00
|
|
|
static void primary_vm_do_failover(void)
|
|
|
|
{
|
|
|
|
MigrationState *s = migrate_get_current();
|
|
|
|
int old_state;
|
|
|
|
|
|
|
|
migrate_set_state(&s->state, MIGRATION_STATUS_COLO,
|
|
|
|
MIGRATION_STATUS_COMPLETED);
|
|
|
|
|
2017-01-17 15:57:43 +03:00
|
|
|
/*
|
|
|
|
* Wake up COLO thread which may blocked in recv() or send(),
|
|
|
|
* The s->rp_state.from_dst_file and s->to_dst_file may use the
|
|
|
|
* same fd, but we still shutdown the fd for twice, it is harmless.
|
|
|
|
*/
|
|
|
|
if (s->to_dst_file) {
|
|
|
|
qemu_file_shutdown(s->to_dst_file);
|
|
|
|
}
|
|
|
|
if (s->rp_state.from_dst_file) {
|
|
|
|
qemu_file_shutdown(s->rp_state.from_dst_file);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:05 +03:00
|
|
|
old_state = failover_set_state(FAILOVER_STATUS_ACTIVE,
|
|
|
|
FAILOVER_STATUS_COMPLETED);
|
|
|
|
if (old_state != FAILOVER_STATUS_ACTIVE) {
|
|
|
|
error_report("Incorrect state (%s) while doing failover for Primary VM",
|
|
|
|
FailoverStatus_lookup[old_state]);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-17 15:57:43 +03:00
|
|
|
/* Notify COLO thread that failover work is finished */
|
|
|
|
qemu_sem_post(&s->colo_exit_sem);
|
2016-10-27 09:43:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void colo_do_failover(MigrationState *s)
|
|
|
|
{
|
|
|
|
/* Make sure VM stopped while failover happened. */
|
|
|
|
if (!colo_runstate_is_stopped()) {
|
|
|
|
vm_stop_force_state(RUN_STATE_COLO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_colo_mode() == COLO_MODE_PRIMARY) {
|
|
|
|
primary_vm_do_failover();
|
2016-10-27 09:43:06 +03:00
|
|
|
} else {
|
|
|
|
secondary_vm_do_failover();
|
2016-10-27 09:43:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 22:01:56 +03:00
|
|
|
void qmp_xen_set_replication(bool enable, bool primary,
|
|
|
|
bool has_failover, bool failover,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2017-04-27 16:00:53 +03:00
|
|
|
#ifdef CONFIG_REPLICATION
|
2017-02-28 22:01:56 +03:00
|
|
|
ReplicationMode mode = primary ?
|
|
|
|
REPLICATION_MODE_PRIMARY :
|
|
|
|
REPLICATION_MODE_SECONDARY;
|
|
|
|
|
|
|
|
if (has_failover && enable) {
|
|
|
|
error_setg(errp, "Parameter 'failover' is only for"
|
|
|
|
" stopping replication");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
replication_start_all(mode, errp);
|
|
|
|
} else {
|
|
|
|
if (!has_failover) {
|
|
|
|
failover = NULL;
|
|
|
|
}
|
|
|
|
replication_stop_all(failover, failover ? NULL : errp);
|
|
|
|
}
|
2017-04-27 16:00:53 +03:00
|
|
|
#else
|
|
|
|
abort();
|
|
|
|
#endif
|
2017-02-28 22:01:56 +03:00
|
|
|
}
|
|
|
|
|
2017-02-25 05:46:04 +03:00
|
|
|
ReplicationStatus *qmp_query_xen_replication_status(Error **errp)
|
|
|
|
{
|
2017-04-27 16:00:53 +03:00
|
|
|
#ifdef CONFIG_REPLICATION
|
2017-02-25 05:46:04 +03:00
|
|
|
Error *err = NULL;
|
|
|
|
ReplicationStatus *s = g_new0(ReplicationStatus, 1);
|
|
|
|
|
|
|
|
replication_get_error_all(&err);
|
|
|
|
if (err) {
|
|
|
|
s->error = true;
|
|
|
|
s->has_desc = true;
|
|
|
|
s->desc = g_strdup(error_get_pretty(err));
|
|
|
|
} else {
|
|
|
|
s->error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_free(err);
|
|
|
|
return s;
|
2017-04-27 16:00:53 +03:00
|
|
|
#else
|
|
|
|
abort();
|
|
|
|
#endif
|
2017-02-25 05:46:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void qmp_xen_colo_do_checkpoint(Error **errp)
|
|
|
|
{
|
2017-04-27 16:00:53 +03:00
|
|
|
#ifdef CONFIG_REPLICATION
|
2017-02-25 05:46:04 +03:00
|
|
|
replication_do_checkpoint_all(errp);
|
2017-04-27 16:00:53 +03:00
|
|
|
#else
|
|
|
|
abort();
|
|
|
|
#endif
|
2017-02-25 05:46:04 +03:00
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
static void colo_send_message(QEMUFile *f, COLOMessage msg,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (msg >= COLO_MESSAGE__MAX) {
|
|
|
|
error_setg(errp, "%s: Invalid message", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qemu_put_be32(f, msg);
|
|
|
|
qemu_fflush(f);
|
|
|
|
|
|
|
|
ret = qemu_file_get_error(f);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Can't send COLO message");
|
|
|
|
}
|
|
|
|
trace_colo_send_message(COLOMessage_lookup[msg]);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
static void colo_send_message_value(QEMUFile *f, COLOMessage msg,
|
|
|
|
uint64_t value, Error **errp)
|
|
|
|
{
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
colo_send_message(f, msg, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qemu_put_be64(f, value);
|
|
|
|
qemu_fflush(f);
|
|
|
|
|
|
|
|
ret = qemu_file_get_error(f);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Failed to send value for message:%s",
|
|
|
|
COLOMessage_lookup[msg]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
static COLOMessage colo_receive_message(QEMUFile *f, Error **errp)
|
|
|
|
{
|
|
|
|
COLOMessage msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg = qemu_get_be32(f);
|
|
|
|
ret = qemu_file_get_error(f);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Can't receive COLO message");
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
if (msg >= COLO_MESSAGE__MAX) {
|
|
|
|
error_setg(errp, "%s: Invalid message", __func__);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
trace_colo_receive_message(COLOMessage_lookup[msg]);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
COLOMessage msg;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
msg = colo_receive_message(f, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (msg != expect_msg) {
|
|
|
|
error_setg(errp, "Unexpected COLO message %d, expected %d",
|
|
|
|
msg, expect_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:00 +03:00
|
|
|
static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
Error *local_err = NULL;
|
|
|
|
uint64_t value;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
colo_receive_check_message(f, expect_msg, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = qemu_get_be64(f);
|
|
|
|
ret = qemu_file_get_error(f);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s",
|
|
|
|
COLOMessage_lookup[expect_msg]);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
static int colo_do_checkpoint_transaction(MigrationState *s,
|
|
|
|
QIOChannelBuffer *bioc,
|
|
|
|
QEMUFile *fb)
|
2016-10-27 09:42:57 +03:00
|
|
|
{
|
|
|
|
Error *local_err = NULL;
|
2016-10-27 09:42:59 +03:00
|
|
|
int ret = -1;
|
2016-10-27 09:42:57 +03:00
|
|
|
|
|
|
|
colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST,
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
colo_receive_check_message(s->rp_state.from_dst_file,
|
|
|
|
COLO_MESSAGE_CHECKPOINT_REPLY, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-27 09:42:59 +03:00
|
|
|
/* Reset channel-buffer directly */
|
|
|
|
qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
|
|
|
|
bioc->usage = 0;
|
2016-10-27 09:42:57 +03:00
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
qemu_mutex_lock_iothread();
|
2016-10-27 09:43:05 +03:00
|
|
|
if (failover_get_state() != FAILOVER_STATUS_NONE) {
|
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-27 09:42:59 +03:00
|
|
|
vm_stop_force_state(RUN_STATE_COLO);
|
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
trace_colo_vm_state_change("run", "stop");
|
2016-10-27 09:43:05 +03:00
|
|
|
/*
|
|
|
|
* Failover request bh could be called after vm_stop_force_state(),
|
|
|
|
* So we need check failover_request_is_active() again.
|
|
|
|
*/
|
|
|
|
if (failover_get_state() != FAILOVER_STATUS_NONE) {
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-27 09:42:59 +03:00
|
|
|
|
|
|
|
/* Disable block migration */
|
2017-04-05 21:45:22 +03:00
|
|
|
migrate_set_block_enabled(false, &local_err);
|
2016-10-27 09:42:59 +03:00
|
|
|
qemu_savevm_state_header(fb);
|
2017-06-28 12:52:24 +03:00
|
|
|
qemu_savevm_state_setup(fb);
|
2016-10-27 09:42:59 +03:00
|
|
|
qemu_mutex_lock_iothread();
|
2017-06-16 19:06:58 +03:00
|
|
|
qemu_savevm_state_complete_precopy(fb, false, false);
|
2016-10-27 09:42:59 +03:00
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
|
|
|
|
qemu_fflush(fb);
|
2016-10-27 09:42:57 +03:00
|
|
|
|
|
|
|
colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-27 09:42:59 +03:00
|
|
|
/*
|
|
|
|
* We need the size of the VMstate data in Secondary side,
|
|
|
|
* With which we can decide how much data should be read.
|
|
|
|
*/
|
|
|
|
colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE,
|
|
|
|
bioc->usage, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-27 09:42:57 +03:00
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage);
|
|
|
|
qemu_fflush(s->to_dst_file);
|
|
|
|
ret = qemu_file_get_error(s->to_dst_file);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-27 09:42:57 +03:00
|
|
|
|
|
|
|
colo_receive_check_message(s->rp_state.from_dst_file,
|
|
|
|
COLO_MESSAGE_VMSTATE_RECEIVED, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
colo_receive_check_message(s->rp_state.from_dst_file,
|
|
|
|
COLO_MESSAGE_VMSTATE_LOADED, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
qemu_mutex_lock_iothread();
|
|
|
|
vm_start();
|
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
trace_colo_vm_state_change("stop", "run");
|
2016-10-27 09:42:57 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
}
|
2016-10-27 09:42:59 +03:00
|
|
|
return ret;
|
2016-10-27 09:42:57 +03:00
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:54 +03:00
|
|
|
static void colo_process_checkpoint(MigrationState *s)
|
|
|
|
{
|
2016-10-27 09:42:59 +03:00
|
|
|
QIOChannelBuffer *bioc;
|
|
|
|
QEMUFile *fb = NULL;
|
2017-01-17 15:57:42 +03:00
|
|
|
int64_t current_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
|
2016-10-27 09:42:57 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2016-10-27 09:43:04 +03:00
|
|
|
failover_init_state();
|
|
|
|
|
2016-10-27 09:42:56 +03:00
|
|
|
s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file);
|
|
|
|
if (!s->rp_state.from_dst_file) {
|
|
|
|
error_report("Open QEMUFile from_dst_file failed");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
/*
|
|
|
|
* Wait for Secondary finish loading VM states and enter COLO
|
|
|
|
* restore.
|
|
|
|
*/
|
|
|
|
colo_receive_check_message(s->rp_state.from_dst_file,
|
|
|
|
COLO_MESSAGE_CHECKPOINT_READY, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-27 09:42:59 +03:00
|
|
|
bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
|
|
|
|
fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc));
|
|
|
|
object_unref(OBJECT(bioc));
|
2016-10-27 09:42:57 +03:00
|
|
|
|
2016-10-27 09:42:54 +03:00
|
|
|
qemu_mutex_lock_iothread();
|
|
|
|
vm_start();
|
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
trace_colo_vm_state_change("stop", "run");
|
|
|
|
|
2017-01-17 15:57:42 +03:00
|
|
|
timer_mod(s->colo_delay_timer,
|
|
|
|
current_time + s->parameters.x_checkpoint_delay);
|
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
while (s->state == MIGRATION_STATUS_COLO) {
|
2016-10-27 09:43:05 +03:00
|
|
|
if (failover_get_state() != FAILOVER_STATUS_NONE) {
|
|
|
|
error_report("failover request");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-01-17 15:57:42 +03:00
|
|
|
qemu_sem_wait(&s->colo_checkpoint_sem);
|
2016-10-27 09:43:02 +03:00
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
ret = colo_do_checkpoint_transaction(s, bioc, fb);
|
2016-10-27 09:42:57 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 09:42:54 +03:00
|
|
|
|
2016-10-27 09:42:56 +03:00
|
|
|
out:
|
2016-10-27 09:42:57 +03:00
|
|
|
/* Throw the unreported error message after exited from loop */
|
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:59 +03:00
|
|
|
if (fb) {
|
|
|
|
qemu_fclose(fb);
|
|
|
|
}
|
|
|
|
|
2017-01-17 15:57:42 +03:00
|
|
|
timer_del(s->colo_delay_timer);
|
|
|
|
|
2017-01-17 15:57:43 +03:00
|
|
|
/* Hope this not to be too long to wait here */
|
|
|
|
qemu_sem_wait(&s->colo_exit_sem);
|
|
|
|
qemu_sem_destroy(&s->colo_exit_sem);
|
|
|
|
/*
|
|
|
|
* Must be called after failover BH is completed,
|
|
|
|
* Or the failover BH may shutdown the wrong fd that
|
|
|
|
* re-used by other threads after we release here.
|
|
|
|
*/
|
2016-10-27 09:42:56 +03:00
|
|
|
if (s->rp_state.from_dst_file) {
|
|
|
|
qemu_fclose(s->rp_state.from_dst_file);
|
|
|
|
}
|
2016-10-27 09:42:54 +03:00
|
|
|
}
|
|
|
|
|
2017-01-17 15:57:42 +03:00
|
|
|
void colo_checkpoint_notify(void *opaque)
|
|
|
|
{
|
|
|
|
MigrationState *s = opaque;
|
|
|
|
int64_t next_notify_time;
|
|
|
|
|
|
|
|
qemu_sem_post(&s->colo_checkpoint_sem);
|
|
|
|
s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST);
|
|
|
|
next_notify_time = s->colo_checkpoint_time +
|
|
|
|
s->parameters.x_checkpoint_delay;
|
|
|
|
timer_mod(s->colo_delay_timer, next_notify_time);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:54 +03:00
|
|
|
void migrate_start_colo_process(MigrationState *s)
|
|
|
|
{
|
|
|
|
qemu_mutex_unlock_iothread();
|
2017-01-17 15:57:42 +03:00
|
|
|
qemu_sem_init(&s->colo_checkpoint_sem, 0);
|
|
|
|
s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST,
|
|
|
|
colo_checkpoint_notify, s);
|
|
|
|
|
2017-01-17 15:57:43 +03:00
|
|
|
qemu_sem_init(&s->colo_exit_sem, 0);
|
2016-10-27 09:42:54 +03:00
|
|
|
migrate_set_state(&s->state, MIGRATION_STATUS_ACTIVE,
|
|
|
|
MIGRATION_STATUS_COLO);
|
|
|
|
colo_process_checkpoint(s);
|
|
|
|
qemu_mutex_lock_iothread();
|
|
|
|
}
|
2016-10-27 09:42:55 +03:00
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
static void colo_wait_handle_message(QEMUFile *f, int *checkpoint_request,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
COLOMessage msg;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
msg = colo_receive_message(f, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg) {
|
|
|
|
case COLO_MESSAGE_CHECKPOINT_REQUEST:
|
|
|
|
*checkpoint_request = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*checkpoint_request = 0;
|
|
|
|
error_setg(errp, "Got unknown COLO message: %d", msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:55 +03:00
|
|
|
void *colo_process_incoming_thread(void *opaque)
|
|
|
|
{
|
|
|
|
MigrationIncomingState *mis = opaque;
|
2016-10-27 09:43:00 +03:00
|
|
|
QEMUFile *fb = NULL;
|
|
|
|
QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */
|
|
|
|
uint64_t total_size;
|
|
|
|
uint64_t value;
|
2016-10-27 09:42:57 +03:00
|
|
|
Error *local_err = NULL;
|
2016-10-27 09:42:55 +03:00
|
|
|
|
2017-01-17 15:57:43 +03:00
|
|
|
qemu_sem_init(&mis->colo_incoming_sem, 0);
|
|
|
|
|
2016-10-27 09:42:55 +03:00
|
|
|
migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
|
|
|
|
MIGRATION_STATUS_COLO);
|
|
|
|
|
2016-10-27 09:43:04 +03:00
|
|
|
failover_init_state();
|
|
|
|
|
2016-10-27 09:42:56 +03:00
|
|
|
mis->to_src_file = qemu_file_get_return_path(mis->from_src_file);
|
|
|
|
if (!mis->to_src_file) {
|
|
|
|
error_report("COLO incoming thread: Open QEMUFile to_src_file failed");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Note: the communication between Primary side and Secondary side
|
|
|
|
* should be sequential, we set the fd to unblocked in migration incoming
|
|
|
|
* coroutine, and here we are in the COLO incoming thread, so it is ok to
|
|
|
|
* set the fd back to blocked.
|
|
|
|
*/
|
|
|
|
qemu_file_set_blocking(mis->from_src_file, true);
|
|
|
|
|
2016-10-27 09:43:00 +03:00
|
|
|
bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE);
|
|
|
|
fb = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
|
|
|
|
object_unref(OBJECT(bioc));
|
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY,
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mis->state == MIGRATION_STATUS_COLO) {
|
2016-11-01 00:50:44 +03:00
|
|
|
int request = 0;
|
2016-10-27 09:42:57 +03:00
|
|
|
|
|
|
|
colo_wait_handle_message(mis->from_src_file, &request, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
assert(request);
|
2016-10-27 09:43:06 +03:00
|
|
|
if (failover_get_state() != FAILOVER_STATUS_NONE) {
|
|
|
|
error_report("failover request");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
/* FIXME: This is unnecessary for periodic checkpoint mode */
|
|
|
|
colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY,
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
colo_receive_check_message(mis->from_src_file,
|
|
|
|
COLO_MESSAGE_VMSTATE_SEND, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:00 +03:00
|
|
|
value = colo_receive_message_value(mis->from_src_file,
|
|
|
|
COLO_MESSAGE_VMSTATE_SIZE, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read VM device state data into channel buffer,
|
|
|
|
* It's better to re-use the memory allocated.
|
|
|
|
* Here we need to handle the channel buffer directly.
|
|
|
|
*/
|
|
|
|
if (value > bioc->capacity) {
|
|
|
|
bioc->capacity = value;
|
|
|
|
bioc->data = g_realloc(bioc->data, bioc->capacity);
|
|
|
|
}
|
|
|
|
total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value);
|
|
|
|
if (total_size != value) {
|
|
|
|
error_report("Got %" PRIu64 " VMState data, less than expected"
|
|
|
|
" %" PRIu64, total_size, value);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
bioc->usage = total_size;
|
|
|
|
qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL);
|
2016-10-27 09:42:57 +03:00
|
|
|
|
|
|
|
colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED,
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:00 +03:00
|
|
|
qemu_mutex_lock_iothread();
|
shutdown: Prepare for use of an enum in reset/shutdown_request
We want to track why a guest was shutdown; in particular, being able
to tell the difference between a guest request (such as ACPI request)
and host request (such as SIGINT) will prove useful to libvirt.
Since all requests eventually end up changing shutdown_requested in
vl.c, the logical change is to make that value track the reason,
rather than its current 0/1 contents.
Since command-line options control whether a reset request is turned
into a shutdown request instead, the same treatment is given to
reset_requested.
This patch adds an internal enum ShutdownCause that describes reasons
that a shutdown can be requested, and changes qemu_system_reset() to
pass the reason through, although for now nothing is actually changed
with regards to what gets reported. The enum could be exported via
QAPI at a later date, if deemed necessary, but for now, there has not
been a request to expose that much detail to end clients.
For the most part, we turn 0 into SHUTDOWN_CAUSE_NONE, and 1 into
SHUTDOWN_CAUSE_HOST_ERROR; the only specific case where we have enough
information right now to use a different value is when we are reacting
to a host signal. It will take a further patch to edit all call-sites
that can trigger a reset or shutdown request to properly pass in any
other reasons; this patch includes TODOs to point such places out.
qemu_system_reset() trades its 'bool report' parameter for a
'ShutdownCause reason', with all non-zero values having the same
effect; this lets us get rid of the weird #defines for VMRESET_*
as synonyms for bools.
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20170515214114.15442-3-eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2017-05-16 00:41:11 +03:00
|
|
|
qemu_system_reset(SHUTDOWN_CAUSE_NONE);
|
2017-01-17 15:57:44 +03:00
|
|
|
vmstate_loading = true;
|
2016-10-27 09:43:00 +03:00
|
|
|
if (qemu_loadvm_state(fb) < 0) {
|
|
|
|
error_report("COLO: loadvm failed");
|
|
|
|
qemu_mutex_unlock_iothread();
|
|
|
|
goto out;
|
|
|
|
}
|
2017-01-17 15:57:44 +03:00
|
|
|
|
|
|
|
vmstate_loading = false;
|
2016-10-27 09:43:00 +03:00
|
|
|
qemu_mutex_unlock_iothread();
|
2016-10-27 09:42:57 +03:00
|
|
|
|
2017-01-17 15:57:44 +03:00
|
|
|
if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) {
|
|
|
|
failover_set_state(FAILOVER_STATUS_RELAUNCH,
|
|
|
|
FAILOVER_STATUS_NONE);
|
|
|
|
failover_request_active(NULL);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:42:57 +03:00
|
|
|
colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED,
|
|
|
|
&local_err);
|
|
|
|
if (local_err) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 09:42:55 +03:00
|
|
|
|
2016-10-27 09:42:56 +03:00
|
|
|
out:
|
2017-01-17 15:57:44 +03:00
|
|
|
vmstate_loading = false;
|
2016-10-27 09:42:57 +03:00
|
|
|
/* Throw the unreported error message after exited from loop */
|
|
|
|
if (local_err) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
}
|
|
|
|
|
2016-10-27 09:43:00 +03:00
|
|
|
if (fb) {
|
|
|
|
qemu_fclose(fb);
|
|
|
|
}
|
|
|
|
|
2017-01-17 15:57:43 +03:00
|
|
|
/* Hope this not to be too long to loop here */
|
|
|
|
qemu_sem_wait(&mis->colo_incoming_sem);
|
|
|
|
qemu_sem_destroy(&mis->colo_incoming_sem);
|
|
|
|
/* Must be called after failover BH is completed */
|
2016-10-27 09:42:56 +03:00
|
|
|
if (mis->to_src_file) {
|
|
|
|
qemu_fclose(mis->to_src_file);
|
|
|
|
}
|
2016-10-27 09:42:55 +03:00
|
|
|
migration_incoming_exit_colo();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|