+ bool initial_data_sent;
} VFIOMigration;
typedef struct VFIOAddressSpace {
diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c
index cb6923ed3f..ede29ffb5c 100644
--- a/hw/vfio/migration.c
+++ b/hw/vfio/migration.c
@@ -18,6 +18,8 @@
#include "sysemu/runstate.h"
#include "hw/vfio/vfio-common.h"
#include "migration/migration.h"
+#include "migration/options.h"
+#include "migration/savevm.h"
#include "migration/vmstate.h"
#include "migration/qemu-file.h"
#include "migration/register.h"
@@ -45,6 +47,7 @@
#define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL)
#define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL)
#define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL)
+#define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL)
/*
* This is an arbitrary size based on migration of mlx5 devices,
where typically
@@ -218,6 +221,7 @@ static void vfio_migration_cleanup(VFIODevice
*vbasedev)
close(migration->data_fd);
migration->data_fd = -1;
+ migration->switchover_ack_needed = false;
}
static int vfio_query_stop_copy_size(VFIODevice *vbasedev,
@@ -350,6 +354,10 @@ static int vfio_save_setup(QEMUFile *f, void
*opaque)
if (vfio_precopy_supported(vbasedev)) {
int ret;
+ if (migrate_switchover_ack()) {
+ migration->switchover_ack_needed = true;
+ }
+
switch (migration->device_state) {
case VFIO_DEVICE_STATE_RUNNING:
ret = vfio_migration_set_state(vbasedev,
VFIO_DEVICE_STATE_PRE_COPY,
@@ -385,6 +393,7 @@ static void vfio_save_cleanup(void *opaque)
migration->data_buffer = NULL;
migration->precopy_init_size = 0;
migration->precopy_dirty_size = 0;
+ migration->initial_data_sent = false;
vfio_migration_cleanup(vbasedev);
trace_vfio_save_cleanup(vbasedev->name);
}
@@ -458,10 +467,17 @@ static int vfio_save_iterate(QEMUFile *f, void
*opaque)
if (data_size < 0) {
return data_size;
}
- qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
vfio_update_estimated_pending_data(migration, data_size);
+ if (migration->switchover_ack_needed &&
!migration->precopy_init_size &&
+ !migration->initial_data_sent) {
+ qemu_put_be64(f, VFIO_MIG_FLAG_DEV_INIT_DATA_SENT);
+ migration->initial_data_sent = true;
+ } else {
+ qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
+ }
+
trace_vfio_save_iterate(vbasedev->name,
migration->precopy_init_size,
migration->precopy_dirty_size);
@@ -526,6 +542,10 @@ static int vfio_load_setup(QEMUFile *f, void
*opaque)
{
VFIODevice *vbasedev = opaque;
+ if (migrate_switchover_ack() && vfio_precopy_supported(vbasedev)) {
+ vbasedev->migration->switchover_ack_needed = true;
+ }
+
return vfio_migration_set_state(vbasedev,
VFIO_DEVICE_STATE_RESUMING,
vbasedev->migration->device_state);
}
@@ -580,6 +600,23 @@ static int vfio_load_state(QEMUFile *f, void
*opaque, int version_id)
}
break;
}
+ case VFIO_MIG_FLAG_DEV_INIT_DATA_SENT:
+ {
+ if (!vbasedev->migration->switchover_ack_needed) {
+ error_report("%s: Received INIT_DATA_SENT but
switchover ack "
+ "is not needed", vbasedev->name);
+ return -EINVAL;
+ }
+
+ ret = qemu_loadvm_approve_switchover();
+ if (ret) {
+ error_report(
+ "%s: qemu_loadvm_approve_switchover failed,
err=%d (%s)",
+ vbasedev->name, ret, strerror(-ret));
+ }
+
+ return ret;
+ }
default:
error_report("%s: Unknown tag 0x%"PRIx64,
vbasedev->name, data);
return -EINVAL;
@@ -594,6 +631,14 @@ static int vfio_load_state(QEMUFile *f, void
*opaque, int version_id)
return ret;
}
+static bool vfio_switchover_ack_needed(void *opaque)
+{
+ VFIODevice *vbasedev = opaque;
+ VFIOMigration *migration = vbasedev->migration;
+
+ return migration->switchover_ack_needed;
+}
+
static const SaveVMHandlers savevm_vfio_handlers = {
.save_setup = vfio_save_setup,
.save_cleanup = vfio_save_cleanup,
@@ -606,6 +651,7 @@ static const SaveVMHandlers savevm_vfio_handlers = {
.load_setup = vfio_load_setup,
.load_cleanup = vfio_load_cleanup,
.load_state = vfio_load_state,
+ .switchover_ack_needed = vfio_switchover_ack_needed,
};
/*
----------------------------------------------------------------------
*/