Merge tag 'v9.1.0'
[qemu/ar7.git] / hw / vfio / migration.c
blob262d42a46e583e8951184e76e47c360ac114f1f6
1 /*
2 * Migration support for VFIO devices
4 * Copyright NVIDIA, Inc. 2020
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
8 */
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
12 #include "qemu/cutils.h"
13 #include "qemu/units.h"
14 #include "qemu/error-report.h"
15 #include <linux/vfio.h>
16 #include <sys/ioctl.h>
18 #include "sysemu/runstate.h"
19 #include "hw/vfio/vfio-common.h"
20 #include "migration/misc.h"
21 #include "migration/savevm.h"
22 #include "migration/vmstate.h"
23 #include "migration/qemu-file.h"
24 #include "migration/register.h"
25 #include "migration/blocker.h"
26 #include "qapi/error.h"
27 #include "qapi/qapi-events-vfio.h"
28 #include "exec/ramlist.h"
29 #include "exec/ram_addr.h"
30 #include "pci.h"
31 #include "trace.h"
32 #include "hw/hw.h"
35 * Flags to be used as unique delimiters for VFIO devices in the migration
36 * stream. These flags are composed as:
37 * 0xffffffff => MSB 32-bit all 1s
38 * 0xef10 => Magic ID, represents emulated (virtual) function IO
39 * 0x0000 => 16-bits reserved for flags
41 * The beginning of state information is marked by _DEV_CONFIG_STATE,
42 * _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a
43 * certain state information is marked by _END_OF_STATE.
45 #define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL)
46 #define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL)
47 #define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL)
48 #define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL)
49 #define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL)
52 * This is an arbitrary size based on migration of mlx5 devices, where typically
53 * total device migration size is on the order of 100s of MB. Testing with
54 * larger values, e.g. 128MB and 1GB, did not show a performance improvement.
56 #define VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE (1 * MiB)
58 static int64_t bytes_transferred;
60 static const char *mig_state_to_str(enum vfio_device_mig_state state)
62 switch (state) {
63 case VFIO_DEVICE_STATE_ERROR:
64 return "ERROR";
65 case VFIO_DEVICE_STATE_STOP:
66 return "STOP";
67 case VFIO_DEVICE_STATE_RUNNING:
68 return "RUNNING";
69 case VFIO_DEVICE_STATE_STOP_COPY:
70 return "STOP_COPY";
71 case VFIO_DEVICE_STATE_RESUMING:
72 return "RESUMING";
73 case VFIO_DEVICE_STATE_RUNNING_P2P:
74 return "RUNNING_P2P";
75 case VFIO_DEVICE_STATE_PRE_COPY:
76 return "PRE_COPY";
77 case VFIO_DEVICE_STATE_PRE_COPY_P2P:
78 return "PRE_COPY_P2P";
79 default:
80 return "UNKNOWN STATE";
84 static VfioMigrationState
85 mig_state_to_qapi_state(enum vfio_device_mig_state state)
87 switch (state) {
88 case VFIO_DEVICE_STATE_STOP:
89 return QAPI_VFIO_MIGRATION_STATE_STOP;
90 case VFIO_DEVICE_STATE_RUNNING:
91 return QAPI_VFIO_MIGRATION_STATE_RUNNING;
92 case VFIO_DEVICE_STATE_STOP_COPY:
93 return QAPI_VFIO_MIGRATION_STATE_STOP_COPY;
94 case VFIO_DEVICE_STATE_RESUMING:
95 return QAPI_VFIO_MIGRATION_STATE_RESUMING;
96 case VFIO_DEVICE_STATE_RUNNING_P2P:
97 return QAPI_VFIO_MIGRATION_STATE_RUNNING_P2P;
98 case VFIO_DEVICE_STATE_PRE_COPY:
99 return QAPI_VFIO_MIGRATION_STATE_PRE_COPY;
100 case VFIO_DEVICE_STATE_PRE_COPY_P2P:
101 return QAPI_VFIO_MIGRATION_STATE_PRE_COPY_P2P;
102 default:
103 g_assert_not_reached();
107 static void vfio_migration_send_event(VFIODevice *vbasedev)
109 VFIOMigration *migration = vbasedev->migration;
110 DeviceState *dev = vbasedev->dev;
111 g_autofree char *qom_path = NULL;
112 Object *obj;
114 if (!vbasedev->migration_events) {
115 return;
118 g_assert(vbasedev->ops->vfio_get_object);
119 obj = vbasedev->ops->vfio_get_object(vbasedev);
120 g_assert(obj);
121 qom_path = object_get_canonical_path(obj);
123 qapi_event_send_vfio_migration(
124 dev->id, qom_path, mig_state_to_qapi_state(migration->device_state));
127 static void vfio_migration_set_device_state(VFIODevice *vbasedev,
128 enum vfio_device_mig_state state)
130 VFIOMigration *migration = vbasedev->migration;
132 trace_vfio_migration_set_device_state(vbasedev->name,
133 mig_state_to_str(state));
135 migration->device_state = state;
136 vfio_migration_send_event(vbasedev);
139 static int vfio_migration_set_state(VFIODevice *vbasedev,
140 enum vfio_device_mig_state new_state,
141 enum vfio_device_mig_state recover_state,
142 Error **errp)
144 VFIOMigration *migration = vbasedev->migration;
145 uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
146 sizeof(struct vfio_device_feature_mig_state),
147 sizeof(uint64_t))] = {};
148 struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
149 struct vfio_device_feature_mig_state *mig_state =
150 (struct vfio_device_feature_mig_state *)feature->data;
151 int ret;
152 g_autofree char *error_prefix =
153 g_strdup_printf("%s: Failed setting device state to %s.",
154 vbasedev->name, mig_state_to_str(new_state));
156 trace_vfio_migration_set_state(vbasedev->name, mig_state_to_str(new_state),
157 mig_state_to_str(recover_state));
159 if (new_state == migration->device_state) {
160 return 0;
163 feature->argsz = sizeof(buf);
164 feature->flags =
165 VFIO_DEVICE_FEATURE_SET | VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE;
166 mig_state->device_state = new_state;
167 if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
168 /* Try to set the device in some good state */
169 ret = -errno;
171 if (recover_state == VFIO_DEVICE_STATE_ERROR) {
172 error_setg_errno(errp, errno,
173 "%s Recover state is ERROR. Resetting device",
174 error_prefix);
176 goto reset_device;
179 error_setg_errno(errp, errno,
180 "%s Setting device in recover state %s",
181 error_prefix, mig_state_to_str(recover_state));
183 mig_state->device_state = recover_state;
184 if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
185 ret = -errno;
187 * If setting the device in recover state fails, report
188 * the error here and propagate the first error.
190 error_report(
191 "%s: Failed setting device in recover state, err: %s. Resetting device",
192 vbasedev->name, strerror(errno));
194 goto reset_device;
197 vfio_migration_set_device_state(vbasedev, recover_state);
199 return ret;
202 vfio_migration_set_device_state(vbasedev, new_state);
203 if (mig_state->data_fd != -1) {
204 if (migration->data_fd != -1) {
206 * This can happen if the device is asynchronously reset and
207 * terminates a data transfer.
209 error_setg(errp, "%s: data_fd out of sync", vbasedev->name);
210 close(mig_state->data_fd);
212 return -EBADF;
215 migration->data_fd = mig_state->data_fd;
218 return 0;
220 reset_device:
221 if (ioctl(vbasedev->fd, VFIO_DEVICE_RESET)) {
222 hw_error("%s: Failed resetting device, err: %s", vbasedev->name,
223 strerror(errno));
226 vfio_migration_set_device_state(vbasedev, VFIO_DEVICE_STATE_RUNNING);
228 return ret;
232 * Some device state transitions require resetting the device if they fail.
233 * This function sets the device in new_state and resets the device if that
234 * fails. Reset is done by using ERROR as the recover state.
236 static int
237 vfio_migration_set_state_or_reset(VFIODevice *vbasedev,
238 enum vfio_device_mig_state new_state,
239 Error **errp)
241 return vfio_migration_set_state(vbasedev, new_state,
242 VFIO_DEVICE_STATE_ERROR, errp);
245 static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,
246 uint64_t data_size)
248 VFIOMigration *migration = vbasedev->migration;
249 int ret;
251 ret = qemu_file_get_to_fd(f, migration->data_fd, data_size);
252 trace_vfio_load_state_device_data(vbasedev->name, data_size, ret);
254 return ret;
257 static int vfio_save_device_config_state(QEMUFile *f, void *opaque,
258 Error **errp)
260 VFIODevice *vbasedev = opaque;
261 int ret;
263 qemu_put_be64(f, VFIO_MIG_FLAG_DEV_CONFIG_STATE);
265 if (vbasedev->ops && vbasedev->ops->vfio_save_config) {
266 ret = vbasedev->ops->vfio_save_config(vbasedev, f, errp);
267 if (ret) {
268 return ret;
272 qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
274 trace_vfio_save_device_config_state(vbasedev->name);
276 ret = qemu_file_get_error(f);
277 if (ret < 0) {
278 error_setg_errno(errp, -ret, "Failed to save state");
280 return ret;
283 static int vfio_load_device_config_state(QEMUFile *f, void *opaque)
285 VFIODevice *vbasedev = opaque;
286 uint64_t data;
288 if (vbasedev->ops && vbasedev->ops->vfio_load_config) {
289 int ret;
291 ret = vbasedev->ops->vfio_load_config(vbasedev, f);
292 if (ret) {
293 error_report("%s: Failed to load device config space",
294 vbasedev->name);
295 return ret;
299 data = qemu_get_be64(f);
300 if (data != VFIO_MIG_FLAG_END_OF_STATE) {
301 error_report("%s: Failed loading device config space, "
302 "end flag incorrect 0x%"PRIx64, vbasedev->name, data);
303 return -EINVAL;
306 trace_vfio_load_device_config_state(vbasedev->name);
307 return qemu_file_get_error(f);
310 static void vfio_migration_cleanup(VFIODevice *vbasedev)
312 VFIOMigration *migration = vbasedev->migration;
314 close(migration->data_fd);
315 migration->data_fd = -1;
318 static int vfio_query_stop_copy_size(VFIODevice *vbasedev,
319 uint64_t *stop_copy_size)
321 uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
322 sizeof(struct vfio_device_feature_mig_data_size),
323 sizeof(uint64_t))] = {};
324 struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
325 struct vfio_device_feature_mig_data_size *mig_data_size =
326 (struct vfio_device_feature_mig_data_size *)feature->data;
328 feature->argsz = sizeof(buf);
329 feature->flags =
330 VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_MIG_DATA_SIZE;
332 if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
333 return -errno;
336 *stop_copy_size = mig_data_size->stop_copy_length;
338 return 0;
341 static int vfio_query_precopy_size(VFIOMigration *migration)
343 struct vfio_precopy_info precopy = {
344 .argsz = sizeof(precopy),
347 migration->precopy_init_size = 0;
348 migration->precopy_dirty_size = 0;
350 if (ioctl(migration->data_fd, VFIO_MIG_GET_PRECOPY_INFO, &precopy)) {
351 return -errno;
354 migration->precopy_init_size = precopy.initial_bytes;
355 migration->precopy_dirty_size = precopy.dirty_bytes;
357 return 0;
360 /* Returns the size of saved data on success and -errno on error */
361 static ssize_t vfio_save_block(QEMUFile *f, VFIOMigration *migration)
363 ssize_t data_size;
365 data_size = read(migration->data_fd, migration->data_buffer,
366 migration->data_buffer_size);
367 if (data_size < 0) {
369 * Pre-copy emptied all the device state for now. For more information,
370 * please refer to the Linux kernel VFIO uAPI.
372 if (errno == ENOMSG) {
373 return 0;
376 return -errno;
378 if (data_size == 0) {
379 return 0;
382 qemu_put_be64(f, VFIO_MIG_FLAG_DEV_DATA_STATE);
383 qemu_put_be64(f, data_size);
384 qemu_put_buffer(f, migration->data_buffer, data_size);
385 bytes_transferred += data_size;
387 trace_vfio_save_block(migration->vbasedev->name, data_size);
389 return qemu_file_get_error(f) ?: data_size;
392 static void vfio_update_estimated_pending_data(VFIOMigration *migration,
393 uint64_t data_size)
395 if (!data_size) {
397 * Pre-copy emptied all the device state for now, update estimated sizes
398 * accordingly.
400 migration->precopy_init_size = 0;
401 migration->precopy_dirty_size = 0;
403 return;
406 if (migration->precopy_init_size) {
407 uint64_t init_size = MIN(migration->precopy_init_size, data_size);
409 migration->precopy_init_size -= init_size;
410 data_size -= init_size;
413 migration->precopy_dirty_size -= MIN(migration->precopy_dirty_size,
414 data_size);
417 static bool vfio_precopy_supported(VFIODevice *vbasedev)
419 VFIOMigration *migration = vbasedev->migration;
421 return migration->mig_flags & VFIO_MIGRATION_PRE_COPY;
424 /* ---------------------------------------------------------------------- */
426 static int vfio_save_prepare(void *opaque, Error **errp)
428 VFIODevice *vbasedev = opaque;
431 * Snapshot doesn't use postcopy nor background snapshot, so allow snapshot
432 * even if they are on.
434 if (runstate_check(RUN_STATE_SAVE_VM)) {
435 return 0;
438 if (migrate_postcopy_ram()) {
439 error_setg(
440 errp, "%s: VFIO migration is not supported with postcopy migration",
441 vbasedev->name);
442 return -EOPNOTSUPP;
445 if (migrate_background_snapshot()) {
446 error_setg(
447 errp,
448 "%s: VFIO migration is not supported with background snapshot",
449 vbasedev->name);
450 return -EOPNOTSUPP;
453 return 0;
456 static int vfio_save_setup(QEMUFile *f, void *opaque, Error **errp)
458 VFIODevice *vbasedev = opaque;
459 VFIOMigration *migration = vbasedev->migration;
460 uint64_t stop_copy_size = VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE;
461 int ret;
463 qemu_put_be64(f, VFIO_MIG_FLAG_DEV_SETUP_STATE);
465 vfio_query_stop_copy_size(vbasedev, &stop_copy_size);
466 migration->data_buffer_size = MIN(VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE,
467 stop_copy_size);
468 migration->data_buffer = g_try_malloc0(migration->data_buffer_size);
469 if (!migration->data_buffer) {
470 error_setg(errp, "%s: Failed to allocate migration data buffer",
471 vbasedev->name);
472 return -ENOMEM;
475 if (vfio_precopy_supported(vbasedev)) {
476 switch (migration->device_state) {
477 case VFIO_DEVICE_STATE_RUNNING:
478 ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_PRE_COPY,
479 VFIO_DEVICE_STATE_RUNNING, errp);
480 if (ret) {
481 return ret;
484 vfio_query_precopy_size(migration);
486 break;
487 case VFIO_DEVICE_STATE_STOP:
488 /* vfio_save_complete_precopy() will go to STOP_COPY */
489 break;
490 default:
491 error_setg(errp, "%s: Invalid device state %d", vbasedev->name,
492 migration->device_state);
493 return -EINVAL;
497 trace_vfio_save_setup(vbasedev->name, migration->data_buffer_size);
499 qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
501 ret = qemu_file_get_error(f);
502 if (ret < 0) {
503 error_setg_errno(errp, -ret, "%s: save setup failed", vbasedev->name);
506 return ret;
509 static void vfio_save_cleanup(void *opaque)
511 VFIODevice *vbasedev = opaque;
512 VFIOMigration *migration = vbasedev->migration;
513 Error *local_err = NULL;
514 int ret;
517 * Changing device state from STOP_COPY to STOP can take time. Do it here,
518 * after migration has completed, so it won't increase downtime.
520 if (migration->device_state == VFIO_DEVICE_STATE_STOP_COPY) {
521 ret = vfio_migration_set_state_or_reset(vbasedev,
522 VFIO_DEVICE_STATE_STOP,
523 &local_err);
524 if (ret) {
525 error_report_err(local_err);
529 g_free(migration->data_buffer);
530 migration->data_buffer = NULL;
531 migration->precopy_init_size = 0;
532 migration->precopy_dirty_size = 0;
533 migration->initial_data_sent = false;
534 vfio_migration_cleanup(vbasedev);
535 trace_vfio_save_cleanup(vbasedev->name);
538 static void vfio_state_pending_estimate(void *opaque, uint64_t *must_precopy,
539 uint64_t *can_postcopy)
541 VFIODevice *vbasedev = opaque;
542 VFIOMigration *migration = vbasedev->migration;
544 if (!vfio_device_state_is_precopy(vbasedev)) {
545 return;
548 *must_precopy +=
549 migration->precopy_init_size + migration->precopy_dirty_size;
551 trace_vfio_state_pending_estimate(vbasedev->name, *must_precopy,
552 *can_postcopy,
553 migration->precopy_init_size,
554 migration->precopy_dirty_size);
558 * Migration size of VFIO devices can be as little as a few KBs or as big as
559 * many GBs. This value should be big enough to cover the worst case.
561 #define VFIO_MIG_STOP_COPY_SIZE (100 * GiB)
563 static void vfio_state_pending_exact(void *opaque, uint64_t *must_precopy,
564 uint64_t *can_postcopy)
566 VFIODevice *vbasedev = opaque;
567 VFIOMigration *migration = vbasedev->migration;
568 uint64_t stop_copy_size = VFIO_MIG_STOP_COPY_SIZE;
571 * If getting pending migration size fails, VFIO_MIG_STOP_COPY_SIZE is
572 * reported so downtime limit won't be violated.
574 vfio_query_stop_copy_size(vbasedev, &stop_copy_size);
575 *must_precopy += stop_copy_size;
577 if (vfio_device_state_is_precopy(vbasedev)) {
578 vfio_query_precopy_size(migration);
580 *must_precopy +=
581 migration->precopy_init_size + migration->precopy_dirty_size;
584 trace_vfio_state_pending_exact(vbasedev->name, *must_precopy, *can_postcopy,
585 stop_copy_size, migration->precopy_init_size,
586 migration->precopy_dirty_size);
589 static bool vfio_is_active_iterate(void *opaque)
591 VFIODevice *vbasedev = opaque;
593 return vfio_device_state_is_precopy(vbasedev);
597 * Note about migration rate limiting: VFIO migration buffer size is currently
598 * limited to 1MB, so there is no need to check if migration rate exceeded (as
599 * in the worst case it will exceed by 1MB). However, if the buffer size is
600 * later changed to a bigger value, migration rate should be enforced here.
602 static int vfio_save_iterate(QEMUFile *f, void *opaque)
604 VFIODevice *vbasedev = opaque;
605 VFIOMigration *migration = vbasedev->migration;
606 ssize_t data_size;
608 data_size = vfio_save_block(f, migration);
609 if (data_size < 0) {
610 return data_size;
613 vfio_update_estimated_pending_data(migration, data_size);
615 if (migrate_switchover_ack() && !migration->precopy_init_size &&
616 !migration->initial_data_sent) {
617 qemu_put_be64(f, VFIO_MIG_FLAG_DEV_INIT_DATA_SENT);
618 migration->initial_data_sent = true;
619 } else {
620 qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
623 trace_vfio_save_iterate(vbasedev->name, migration->precopy_init_size,
624 migration->precopy_dirty_size);
626 return !migration->precopy_init_size && !migration->precopy_dirty_size;
629 static int vfio_save_complete_precopy(QEMUFile *f, void *opaque)
631 VFIODevice *vbasedev = opaque;
632 ssize_t data_size;
633 int ret;
634 Error *local_err = NULL;
636 /* We reach here with device state STOP or STOP_COPY only */
637 ret = vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_STOP_COPY,
638 VFIO_DEVICE_STATE_STOP, &local_err);
639 if (ret) {
640 error_report_err(local_err);
641 return ret;
644 do {
645 data_size = vfio_save_block(f, vbasedev->migration);
646 if (data_size < 0) {
647 return data_size;
649 } while (data_size);
651 qemu_put_be64(f, VFIO_MIG_FLAG_END_OF_STATE);
652 ret = qemu_file_get_error(f);
654 trace_vfio_save_complete_precopy(vbasedev->name, ret);
656 return ret;
659 static void vfio_save_state(QEMUFile *f, void *opaque)
661 VFIODevice *vbasedev = opaque;
662 Error *local_err = NULL;
663 int ret;
665 ret = vfio_save_device_config_state(f, opaque, &local_err);
666 if (ret) {
667 error_prepend(&local_err,
668 "vfio: Failed to save device config space of %s - ",
669 vbasedev->name);
670 qemu_file_set_error_obj(f, ret, local_err);
674 static int vfio_load_setup(QEMUFile *f, void *opaque, Error **errp)
676 VFIODevice *vbasedev = opaque;
678 return vfio_migration_set_state(vbasedev, VFIO_DEVICE_STATE_RESUMING,
679 vbasedev->migration->device_state, errp);
682 static int vfio_load_cleanup(void *opaque)
684 VFIODevice *vbasedev = opaque;
686 vfio_migration_cleanup(vbasedev);
687 trace_vfio_load_cleanup(vbasedev->name);
689 return 0;
692 static int vfio_load_state(QEMUFile *f, void *opaque, int version_id)
694 VFIODevice *vbasedev = opaque;
695 int ret = 0;
696 uint64_t data;
698 data = qemu_get_be64(f);
699 while (data != VFIO_MIG_FLAG_END_OF_STATE) {
701 trace_vfio_load_state(vbasedev->name, data);
703 switch (data) {
704 case VFIO_MIG_FLAG_DEV_CONFIG_STATE:
706 return vfio_load_device_config_state(f, opaque);
708 case VFIO_MIG_FLAG_DEV_SETUP_STATE:
710 data = qemu_get_be64(f);
711 if (data == VFIO_MIG_FLAG_END_OF_STATE) {
712 return ret;
713 } else {
714 error_report("%s: SETUP STATE: EOS not found 0x%"PRIx64,
715 vbasedev->name, data);
716 return -EINVAL;
718 break;
720 case VFIO_MIG_FLAG_DEV_DATA_STATE:
722 uint64_t data_size = qemu_get_be64(f);
724 if (data_size) {
725 ret = vfio_load_buffer(f, vbasedev, data_size);
726 if (ret < 0) {
727 return ret;
730 break;
732 case VFIO_MIG_FLAG_DEV_INIT_DATA_SENT:
734 if (!vfio_precopy_supported(vbasedev) ||
735 !migrate_switchover_ack()) {
736 error_report("%s: Received INIT_DATA_SENT but switchover ack "
737 "is not used", vbasedev->name);
738 return -EINVAL;
741 ret = qemu_loadvm_approve_switchover();
742 if (ret) {
743 error_report(
744 "%s: qemu_loadvm_approve_switchover failed, err=%d (%s)",
745 vbasedev->name, ret, strerror(-ret));
748 return ret;
750 default:
751 error_report("%s: Unknown tag 0x%"PRIx64, vbasedev->name, data);
752 return -EINVAL;
755 data = qemu_get_be64(f);
756 ret = qemu_file_get_error(f);
757 if (ret) {
758 return ret;
761 return ret;
764 static bool vfio_switchover_ack_needed(void *opaque)
766 VFIODevice *vbasedev = opaque;
768 return vfio_precopy_supported(vbasedev);
771 static const SaveVMHandlers savevm_vfio_handlers = {
772 .save_prepare = vfio_save_prepare,
773 .save_setup = vfio_save_setup,
774 .save_cleanup = vfio_save_cleanup,
775 .state_pending_estimate = vfio_state_pending_estimate,
776 .state_pending_exact = vfio_state_pending_exact,
777 .is_active_iterate = vfio_is_active_iterate,
778 .save_live_iterate = vfio_save_iterate,
779 .save_live_complete_precopy = vfio_save_complete_precopy,
780 .save_state = vfio_save_state,
781 .load_setup = vfio_load_setup,
782 .load_cleanup = vfio_load_cleanup,
783 .load_state = vfio_load_state,
784 .switchover_ack_needed = vfio_switchover_ack_needed,
787 /* ---------------------------------------------------------------------- */
789 static void vfio_vmstate_change_prepare(void *opaque, bool running,
790 RunState state)
792 VFIODevice *vbasedev = opaque;
793 VFIOMigration *migration = vbasedev->migration;
794 enum vfio_device_mig_state new_state;
795 Error *local_err = NULL;
796 int ret;
798 new_state = migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ?
799 VFIO_DEVICE_STATE_PRE_COPY_P2P :
800 VFIO_DEVICE_STATE_RUNNING_P2P;
802 ret = vfio_migration_set_state_or_reset(vbasedev, new_state, &local_err);
803 if (ret) {
805 * Migration should be aborted in this case, but vm_state_notify()
806 * currently does not support reporting failures.
808 migration_file_set_error(ret, local_err);
811 trace_vfio_vmstate_change_prepare(vbasedev->name, running,
812 RunState_str(state),
813 mig_state_to_str(new_state));
816 static void vfio_vmstate_change(void *opaque, bool running, RunState state)
818 VFIODevice *vbasedev = opaque;
819 enum vfio_device_mig_state new_state;
820 Error *local_err = NULL;
821 int ret;
823 if (running) {
824 new_state = VFIO_DEVICE_STATE_RUNNING;
825 } else {
826 new_state =
827 (vfio_device_state_is_precopy(vbasedev) &&
828 (state == RUN_STATE_FINISH_MIGRATE || state == RUN_STATE_PAUSED)) ?
829 VFIO_DEVICE_STATE_STOP_COPY :
830 VFIO_DEVICE_STATE_STOP;
833 ret = vfio_migration_set_state_or_reset(vbasedev, new_state, &local_err);
834 if (ret) {
836 * Migration should be aborted in this case, but vm_state_notify()
837 * currently does not support reporting failures.
839 migration_file_set_error(ret, local_err);
842 trace_vfio_vmstate_change(vbasedev->name, running, RunState_str(state),
843 mig_state_to_str(new_state));
846 static int vfio_migration_state_notifier(NotifierWithReturn *notifier,
847 MigrationEvent *e, Error **errp)
849 VFIOMigration *migration = container_of(notifier, VFIOMigration,
850 migration_state);
851 VFIODevice *vbasedev = migration->vbasedev;
852 Error *local_err = NULL;
853 int ret;
855 trace_vfio_migration_state_notifier(vbasedev->name, e->type);
857 if (e->type == MIG_EVENT_PRECOPY_FAILED) {
859 * MigrationNotifyFunc may not return an error code and an Error
860 * object for MIG_EVENT_PRECOPY_FAILED. Hence, report the error
861 * locally and ignore the errp argument.
863 ret = vfio_migration_set_state_or_reset(vbasedev,
864 VFIO_DEVICE_STATE_RUNNING,
865 &local_err);
866 if (ret) {
867 error_report_err(local_err);
870 return 0;
873 static void vfio_migration_free(VFIODevice *vbasedev)
875 g_free(vbasedev->migration);
876 vbasedev->migration = NULL;
879 static int vfio_migration_query_flags(VFIODevice *vbasedev, uint64_t *mig_flags)
881 uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
882 sizeof(struct vfio_device_feature_migration),
883 sizeof(uint64_t))] = {};
884 struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
885 struct vfio_device_feature_migration *mig =
886 (struct vfio_device_feature_migration *)feature->data;
888 feature->argsz = sizeof(buf);
889 feature->flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_MIGRATION;
890 if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
891 return -errno;
894 *mig_flags = mig->flags;
896 return 0;
899 static bool vfio_dma_logging_supported(VFIODevice *vbasedev)
901 uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature),
902 sizeof(uint64_t))] = {};
903 struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
905 feature->argsz = sizeof(buf);
906 feature->flags = VFIO_DEVICE_FEATURE_PROBE |
907 VFIO_DEVICE_FEATURE_DMA_LOGGING_START;
909 return !ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature);
912 static int vfio_migration_init(VFIODevice *vbasedev)
914 int ret;
915 Object *obj;
916 VFIOMigration *migration;
917 char id[256] = "";
918 g_autofree char *path = NULL, *oid = NULL;
919 uint64_t mig_flags = 0;
920 VMChangeStateHandler *prepare_cb;
922 if (!vbasedev->ops->vfio_get_object) {
923 return -EINVAL;
926 obj = vbasedev->ops->vfio_get_object(vbasedev);
927 if (!obj) {
928 return -EINVAL;
931 ret = vfio_migration_query_flags(vbasedev, &mig_flags);
932 if (ret) {
933 return ret;
936 /* Basic migration functionality must be supported */
937 if (!(mig_flags & VFIO_MIGRATION_STOP_COPY)) {
938 return -EOPNOTSUPP;
941 vbasedev->migration = g_new0(VFIOMigration, 1);
942 migration = vbasedev->migration;
943 migration->vbasedev = vbasedev;
944 migration->device_state = VFIO_DEVICE_STATE_RUNNING;
945 migration->data_fd = -1;
946 migration->mig_flags = mig_flags;
948 vbasedev->dirty_pages_supported = vfio_dma_logging_supported(vbasedev);
950 oid = vmstate_if_get_id(VMSTATE_IF(DEVICE(obj)));
951 if (oid) {
952 path = g_strdup_printf("%s/vfio", oid);
953 } else {
954 path = g_strdup("vfio");
956 strpadcpy(id, sizeof(id), path, '\0');
958 register_savevm_live(id, VMSTATE_INSTANCE_ID_ANY, 1, &savevm_vfio_handlers,
959 vbasedev);
961 prepare_cb = migration->mig_flags & VFIO_MIGRATION_P2P ?
962 vfio_vmstate_change_prepare :
963 NULL;
964 migration->vm_state = qdev_add_vm_change_state_handler_full(
965 vbasedev->dev, vfio_vmstate_change, prepare_cb, vbasedev);
966 migration_add_notifier(&migration->migration_state,
967 vfio_migration_state_notifier);
969 return 0;
972 static void vfio_migration_deinit(VFIODevice *vbasedev)
974 VFIOMigration *migration = vbasedev->migration;
976 migration_remove_notifier(&migration->migration_state);
977 qemu_del_vm_change_state_handler(migration->vm_state);
978 unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev);
979 vfio_migration_free(vbasedev);
980 vfio_unblock_multiple_devices_migration();
983 static int vfio_block_migration(VFIODevice *vbasedev, Error *err, Error **errp)
985 if (vbasedev->enable_migration == ON_OFF_AUTO_ON) {
986 error_propagate(errp, err);
987 return -EINVAL;
990 vbasedev->migration_blocker = error_copy(err);
991 error_free(err);
993 return migrate_add_blocker_normal(&vbasedev->migration_blocker, errp);
996 /* ---------------------------------------------------------------------- */
998 int64_t vfio_mig_bytes_transferred(void)
1000 return bytes_transferred;
1003 void vfio_reset_bytes_transferred(void)
1005 bytes_transferred = 0;
1009 * Return true when either migration initialized or blocker registered.
1010 * Currently only return false when adding blocker fails which will
1011 * de-register vfio device.
1013 bool vfio_migration_realize(VFIODevice *vbasedev, Error **errp)
1015 Error *err = NULL;
1016 int ret;
1018 if (vbasedev->enable_migration == ON_OFF_AUTO_OFF) {
1019 error_setg(&err, "%s: Migration is disabled for VFIO device",
1020 vbasedev->name);
1021 return !vfio_block_migration(vbasedev, err, errp);
1024 ret = vfio_migration_init(vbasedev);
1025 if (ret) {
1026 if (ret == -ENOTTY) {
1027 error_setg(&err, "%s: VFIO migration is not supported in kernel",
1028 vbasedev->name);
1029 } else {
1030 error_setg(&err,
1031 "%s: Migration couldn't be initialized for VFIO device, "
1032 "err: %d (%s)",
1033 vbasedev->name, ret, strerror(-ret));
1036 return !vfio_block_migration(vbasedev, err, errp);
1039 if ((!vbasedev->dirty_pages_supported ||
1040 vbasedev->device_dirty_page_tracking == ON_OFF_AUTO_OFF) &&
1041 !vbasedev->iommu_dirty_tracking) {
1042 if (vbasedev->enable_migration == ON_OFF_AUTO_AUTO) {
1043 error_setg(&err,
1044 "%s: VFIO device doesn't support device and "
1045 "IOMMU dirty tracking", vbasedev->name);
1046 goto add_blocker;
1049 warn_report("%s: VFIO device doesn't support device and "
1050 "IOMMU dirty tracking", vbasedev->name);
1053 ret = vfio_block_multiple_devices_migration(vbasedev, errp);
1054 if (ret) {
1055 goto out_deinit;
1058 if (vfio_viommu_preset(vbasedev)) {
1059 error_setg(&err, "%s: Migration is currently not supported "
1060 "with vIOMMU enabled", vbasedev->name);
1061 goto add_blocker;
1064 trace_vfio_migration_realize(vbasedev->name);
1065 return true;
1067 add_blocker:
1068 ret = vfio_block_migration(vbasedev, err, errp);
1069 out_deinit:
1070 if (ret) {
1071 vfio_migration_deinit(vbasedev);
1073 return !ret;
1076 void vfio_migration_exit(VFIODevice *vbasedev)
1078 if (vbasedev->migration) {
1079 vfio_migration_deinit(vbasedev);
1082 migrate_del_blocker(&vbasedev->migration_blocker);