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.
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/migration.h"
21 #include "migration/options.h"
22 #include "migration/savevm.h"
23 #include "migration/vmstate.h"
24 #include "migration/qemu-file.h"
25 #include "migration/register.h"
26 #include "migration/blocker.h"
27 #include "migration/misc.h"
28 #include "qapi/error.h"
29 #include "exec/ramlist.h"
30 #include "exec/ram_addr.h"
36 * Flags to be used as unique delimiters for VFIO devices in the migration
37 * stream. These flags are composed as:
38 * 0xffffffff => MSB 32-bit all 1s
39 * 0xef10 => Magic ID, represents emulated (virtual) function IO
40 * 0x0000 => 16-bits reserved for flags
42 * The beginning of state information is marked by _DEV_CONFIG_STATE,
43 * _DEV_SETUP_STATE, or _DEV_DATA_STATE, respectively. The end of a
44 * certain state information is marked by _END_OF_STATE.
46 #define VFIO_MIG_FLAG_END_OF_STATE (0xffffffffef100001ULL)
47 #define VFIO_MIG_FLAG_DEV_CONFIG_STATE (0xffffffffef100002ULL)
48 #define VFIO_MIG_FLAG_DEV_SETUP_STATE (0xffffffffef100003ULL)
49 #define VFIO_MIG_FLAG_DEV_DATA_STATE (0xffffffffef100004ULL)
50 #define VFIO_MIG_FLAG_DEV_INIT_DATA_SENT (0xffffffffef100005ULL)
53 * This is an arbitrary size based on migration of mlx5 devices, where typically
54 * total device migration size is on the order of 100s of MB. Testing with
55 * larger values, e.g. 128MB and 1GB, did not show a performance improvement.
57 #define VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE (1 * MiB)
59 static int64_t bytes_transferred
;
61 static const char *mig_state_to_str(enum vfio_device_mig_state state
)
64 case VFIO_DEVICE_STATE_ERROR
:
66 case VFIO_DEVICE_STATE_STOP
:
68 case VFIO_DEVICE_STATE_RUNNING
:
70 case VFIO_DEVICE_STATE_STOP_COPY
:
72 case VFIO_DEVICE_STATE_RESUMING
:
74 case VFIO_DEVICE_STATE_RUNNING_P2P
:
76 case VFIO_DEVICE_STATE_PRE_COPY
:
78 case VFIO_DEVICE_STATE_PRE_COPY_P2P
:
79 return "PRE_COPY_P2P";
81 return "UNKNOWN STATE";
85 static int vfio_migration_set_state(VFIODevice
*vbasedev
,
86 enum vfio_device_mig_state new_state
,
87 enum vfio_device_mig_state recover_state
)
89 VFIOMigration
*migration
= vbasedev
->migration
;
90 uint64_t buf
[DIV_ROUND_UP(sizeof(struct vfio_device_feature
) +
91 sizeof(struct vfio_device_feature_mig_state
),
92 sizeof(uint64_t))] = {};
93 struct vfio_device_feature
*feature
= (struct vfio_device_feature
*)buf
;
94 struct vfio_device_feature_mig_state
*mig_state
=
95 (struct vfio_device_feature_mig_state
*)feature
->data
;
98 feature
->argsz
= sizeof(buf
);
100 VFIO_DEVICE_FEATURE_SET
| VFIO_DEVICE_FEATURE_MIG_DEVICE_STATE
;
101 mig_state
->device_state
= new_state
;
102 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_FEATURE
, feature
)) {
103 /* Try to set the device in some good state */
106 if (recover_state
== VFIO_DEVICE_STATE_ERROR
) {
107 error_report("%s: Failed setting device state to %s, err: %s. "
108 "Recover state is ERROR. Resetting device",
109 vbasedev
->name
, mig_state_to_str(new_state
),
116 "%s: Failed setting device state to %s, err: %s. Setting device in recover state %s",
117 vbasedev
->name
, mig_state_to_str(new_state
),
118 strerror(errno
), mig_state_to_str(recover_state
));
120 mig_state
->device_state
= recover_state
;
121 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_FEATURE
, feature
)) {
124 "%s: Failed setting device in recover state, err: %s. Resetting device",
125 vbasedev
->name
, strerror(errno
));
130 migration
->device_state
= recover_state
;
135 migration
->device_state
= new_state
;
136 if (mig_state
->data_fd
!= -1) {
137 if (migration
->data_fd
!= -1) {
139 * This can happen if the device is asynchronously reset and
140 * terminates a data transfer.
142 error_report("%s: data_fd out of sync", vbasedev
->name
);
143 close(mig_state
->data_fd
);
148 migration
->data_fd
= mig_state
->data_fd
;
151 trace_vfio_migration_set_state(vbasedev
->name
, mig_state_to_str(new_state
));
156 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_RESET
)) {
157 hw_error("%s: Failed resetting device, err: %s", vbasedev
->name
,
161 migration
->device_state
= VFIO_DEVICE_STATE_RUNNING
;
166 static int vfio_load_buffer(QEMUFile
*f
, VFIODevice
*vbasedev
,
169 VFIOMigration
*migration
= vbasedev
->migration
;
172 ret
= qemu_file_get_to_fd(f
, migration
->data_fd
, data_size
);
173 trace_vfio_load_state_device_data(vbasedev
->name
, data_size
, ret
);
178 static int vfio_save_device_config_state(QEMUFile
*f
, void *opaque
)
180 VFIODevice
*vbasedev
= opaque
;
182 qemu_put_be64(f
, VFIO_MIG_FLAG_DEV_CONFIG_STATE
);
184 if (vbasedev
->ops
&& vbasedev
->ops
->vfio_save_config
) {
185 vbasedev
->ops
->vfio_save_config(vbasedev
, f
);
188 qemu_put_be64(f
, VFIO_MIG_FLAG_END_OF_STATE
);
190 trace_vfio_save_device_config_state(vbasedev
->name
);
192 return qemu_file_get_error(f
);
195 static int vfio_load_device_config_state(QEMUFile
*f
, void *opaque
)
197 VFIODevice
*vbasedev
= opaque
;
200 if (vbasedev
->ops
&& vbasedev
->ops
->vfio_load_config
) {
203 ret
= vbasedev
->ops
->vfio_load_config(vbasedev
, f
);
205 error_report("%s: Failed to load device config space",
211 data
= qemu_get_be64(f
);
212 if (data
!= VFIO_MIG_FLAG_END_OF_STATE
) {
213 error_report("%s: Failed loading device config space, "
214 "end flag incorrect 0x%"PRIx64
, vbasedev
->name
, data
);
218 trace_vfio_load_device_config_state(vbasedev
->name
);
219 return qemu_file_get_error(f
);
222 static void vfio_migration_cleanup(VFIODevice
*vbasedev
)
224 VFIOMigration
*migration
= vbasedev
->migration
;
226 close(migration
->data_fd
);
227 migration
->data_fd
= -1;
230 static int vfio_query_stop_copy_size(VFIODevice
*vbasedev
,
231 uint64_t *stop_copy_size
)
233 uint64_t buf
[DIV_ROUND_UP(sizeof(struct vfio_device_feature
) +
234 sizeof(struct vfio_device_feature_mig_data_size
),
235 sizeof(uint64_t))] = {};
236 struct vfio_device_feature
*feature
= (struct vfio_device_feature
*)buf
;
237 struct vfio_device_feature_mig_data_size
*mig_data_size
=
238 (struct vfio_device_feature_mig_data_size
*)feature
->data
;
240 feature
->argsz
= sizeof(buf
);
242 VFIO_DEVICE_FEATURE_GET
| VFIO_DEVICE_FEATURE_MIG_DATA_SIZE
;
244 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_FEATURE
, feature
)) {
248 *stop_copy_size
= mig_data_size
->stop_copy_length
;
253 static int vfio_query_precopy_size(VFIOMigration
*migration
)
255 struct vfio_precopy_info precopy
= {
256 .argsz
= sizeof(precopy
),
259 migration
->precopy_init_size
= 0;
260 migration
->precopy_dirty_size
= 0;
262 if (ioctl(migration
->data_fd
, VFIO_MIG_GET_PRECOPY_INFO
, &precopy
)) {
266 migration
->precopy_init_size
= precopy
.initial_bytes
;
267 migration
->precopy_dirty_size
= precopy
.dirty_bytes
;
272 /* Returns the size of saved data on success and -errno on error */
273 static ssize_t
vfio_save_block(QEMUFile
*f
, VFIOMigration
*migration
)
277 data_size
= read(migration
->data_fd
, migration
->data_buffer
,
278 migration
->data_buffer_size
);
281 * Pre-copy emptied all the device state for now. For more information,
282 * please refer to the Linux kernel VFIO uAPI.
284 if (errno
== ENOMSG
) {
290 if (data_size
== 0) {
294 qemu_put_be64(f
, VFIO_MIG_FLAG_DEV_DATA_STATE
);
295 qemu_put_be64(f
, data_size
);
296 qemu_put_buffer(f
, migration
->data_buffer
, data_size
);
297 bytes_transferred
+= data_size
;
299 trace_vfio_save_block(migration
->vbasedev
->name
, data_size
);
301 return qemu_file_get_error(f
) ?: data_size
;
304 static void vfio_update_estimated_pending_data(VFIOMigration
*migration
,
309 * Pre-copy emptied all the device state for now, update estimated sizes
312 migration
->precopy_init_size
= 0;
313 migration
->precopy_dirty_size
= 0;
318 if (migration
->precopy_init_size
) {
319 uint64_t init_size
= MIN(migration
->precopy_init_size
, data_size
);
321 migration
->precopy_init_size
-= init_size
;
322 data_size
-= init_size
;
325 migration
->precopy_dirty_size
-= MIN(migration
->precopy_dirty_size
,
329 static bool vfio_precopy_supported(VFIODevice
*vbasedev
)
331 VFIOMigration
*migration
= vbasedev
->migration
;
333 return migration
->mig_flags
& VFIO_MIGRATION_PRE_COPY
;
336 /* ---------------------------------------------------------------------- */
338 static int vfio_save_prepare(void *opaque
, Error
**errp
)
340 VFIODevice
*vbasedev
= opaque
;
343 * Snapshot doesn't use postcopy nor background snapshot, so allow snapshot
344 * even if they are on.
346 if (runstate_check(RUN_STATE_SAVE_VM
)) {
350 if (migrate_postcopy_ram()) {
352 errp
, "%s: VFIO migration is not supported with postcopy migration",
357 if (migrate_background_snapshot()) {
360 "%s: VFIO migration is not supported with background snapshot",
368 static int vfio_save_setup(QEMUFile
*f
, void *opaque
)
370 VFIODevice
*vbasedev
= opaque
;
371 VFIOMigration
*migration
= vbasedev
->migration
;
372 uint64_t stop_copy_size
= VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE
;
374 qemu_put_be64(f
, VFIO_MIG_FLAG_DEV_SETUP_STATE
);
376 vfio_query_stop_copy_size(vbasedev
, &stop_copy_size
);
377 migration
->data_buffer_size
= MIN(VFIO_MIG_DEFAULT_DATA_BUFFER_SIZE
,
379 migration
->data_buffer
= g_try_malloc0(migration
->data_buffer_size
);
380 if (!migration
->data_buffer
) {
381 error_report("%s: Failed to allocate migration data buffer",
386 if (vfio_precopy_supported(vbasedev
)) {
389 switch (migration
->device_state
) {
390 case VFIO_DEVICE_STATE_RUNNING
:
391 ret
= vfio_migration_set_state(vbasedev
, VFIO_DEVICE_STATE_PRE_COPY
,
392 VFIO_DEVICE_STATE_RUNNING
);
397 vfio_query_precopy_size(migration
);
400 case VFIO_DEVICE_STATE_STOP
:
401 /* vfio_save_complete_precopy() will go to STOP_COPY */
408 trace_vfio_save_setup(vbasedev
->name
, migration
->data_buffer_size
);
410 qemu_put_be64(f
, VFIO_MIG_FLAG_END_OF_STATE
);
412 return qemu_file_get_error(f
);
415 static void vfio_save_cleanup(void *opaque
)
417 VFIODevice
*vbasedev
= opaque
;
418 VFIOMigration
*migration
= vbasedev
->migration
;
421 * Changing device state from STOP_COPY to STOP can take time. Do it here,
422 * after migration has completed, so it won't increase downtime.
424 if (migration
->device_state
== VFIO_DEVICE_STATE_STOP_COPY
) {
426 * If setting the device in STOP state fails, the device should be
427 * reset. To do so, use ERROR state as a recover state.
429 vfio_migration_set_state(vbasedev
, VFIO_DEVICE_STATE_STOP
,
430 VFIO_DEVICE_STATE_ERROR
);
433 g_free(migration
->data_buffer
);
434 migration
->data_buffer
= NULL
;
435 migration
->precopy_init_size
= 0;
436 migration
->precopy_dirty_size
= 0;
437 migration
->initial_data_sent
= false;
438 vfio_migration_cleanup(vbasedev
);
439 trace_vfio_save_cleanup(vbasedev
->name
);
442 static void vfio_state_pending_estimate(void *opaque
, uint64_t *must_precopy
,
443 uint64_t *can_postcopy
)
445 VFIODevice
*vbasedev
= opaque
;
446 VFIOMigration
*migration
= vbasedev
->migration
;
448 if (!vfio_device_state_is_precopy(vbasedev
)) {
453 migration
->precopy_init_size
+ migration
->precopy_dirty_size
;
455 trace_vfio_state_pending_estimate(vbasedev
->name
, *must_precopy
,
457 migration
->precopy_init_size
,
458 migration
->precopy_dirty_size
);
462 * Migration size of VFIO devices can be as little as a few KBs or as big as
463 * many GBs. This value should be big enough to cover the worst case.
465 #define VFIO_MIG_STOP_COPY_SIZE (100 * GiB)
467 static void vfio_state_pending_exact(void *opaque
, uint64_t *must_precopy
,
468 uint64_t *can_postcopy
)
470 VFIODevice
*vbasedev
= opaque
;
471 VFIOMigration
*migration
= vbasedev
->migration
;
472 uint64_t stop_copy_size
= VFIO_MIG_STOP_COPY_SIZE
;
475 * If getting pending migration size fails, VFIO_MIG_STOP_COPY_SIZE is
476 * reported so downtime limit won't be violated.
478 vfio_query_stop_copy_size(vbasedev
, &stop_copy_size
);
479 *must_precopy
+= stop_copy_size
;
481 if (vfio_device_state_is_precopy(vbasedev
)) {
482 vfio_query_precopy_size(migration
);
485 migration
->precopy_init_size
+ migration
->precopy_dirty_size
;
488 trace_vfio_state_pending_exact(vbasedev
->name
, *must_precopy
, *can_postcopy
,
489 stop_copy_size
, migration
->precopy_init_size
,
490 migration
->precopy_dirty_size
);
493 static bool vfio_is_active_iterate(void *opaque
)
495 VFIODevice
*vbasedev
= opaque
;
497 return vfio_device_state_is_precopy(vbasedev
);
500 static int vfio_save_iterate(QEMUFile
*f
, void *opaque
)
502 VFIODevice
*vbasedev
= opaque
;
503 VFIOMigration
*migration
= vbasedev
->migration
;
506 data_size
= vfio_save_block(f
, migration
);
511 vfio_update_estimated_pending_data(migration
, data_size
);
513 if (migrate_switchover_ack() && !migration
->precopy_init_size
&&
514 !migration
->initial_data_sent
) {
515 qemu_put_be64(f
, VFIO_MIG_FLAG_DEV_INIT_DATA_SENT
);
516 migration
->initial_data_sent
= true;
518 qemu_put_be64(f
, VFIO_MIG_FLAG_END_OF_STATE
);
521 trace_vfio_save_iterate(vbasedev
->name
, migration
->precopy_init_size
,
522 migration
->precopy_dirty_size
);
525 * A VFIO device's pre-copy dirty_bytes is not guaranteed to reach zero.
526 * Return 1 so following handlers will not be potentially blocked.
531 static int vfio_save_complete_precopy(QEMUFile
*f
, void *opaque
)
533 VFIODevice
*vbasedev
= opaque
;
537 /* We reach here with device state STOP or STOP_COPY only */
538 ret
= vfio_migration_set_state(vbasedev
, VFIO_DEVICE_STATE_STOP_COPY
,
539 VFIO_DEVICE_STATE_STOP
);
545 data_size
= vfio_save_block(f
, vbasedev
->migration
);
551 qemu_put_be64(f
, VFIO_MIG_FLAG_END_OF_STATE
);
552 ret
= qemu_file_get_error(f
);
557 trace_vfio_save_complete_precopy(vbasedev
->name
, ret
);
562 static void vfio_save_state(QEMUFile
*f
, void *opaque
)
564 VFIODevice
*vbasedev
= opaque
;
567 ret
= vfio_save_device_config_state(f
, opaque
);
569 error_report("%s: Failed to save device config space",
571 qemu_file_set_error(f
, ret
);
575 static int vfio_load_setup(QEMUFile
*f
, void *opaque
)
577 VFIODevice
*vbasedev
= opaque
;
579 return vfio_migration_set_state(vbasedev
, VFIO_DEVICE_STATE_RESUMING
,
580 vbasedev
->migration
->device_state
);
583 static int vfio_load_cleanup(void *opaque
)
585 VFIODevice
*vbasedev
= opaque
;
587 vfio_migration_cleanup(vbasedev
);
588 trace_vfio_load_cleanup(vbasedev
->name
);
593 static int vfio_load_state(QEMUFile
*f
, void *opaque
, int version_id
)
595 VFIODevice
*vbasedev
= opaque
;
599 data
= qemu_get_be64(f
);
600 while (data
!= VFIO_MIG_FLAG_END_OF_STATE
) {
602 trace_vfio_load_state(vbasedev
->name
, data
);
605 case VFIO_MIG_FLAG_DEV_CONFIG_STATE
:
607 return vfio_load_device_config_state(f
, opaque
);
609 case VFIO_MIG_FLAG_DEV_SETUP_STATE
:
611 data
= qemu_get_be64(f
);
612 if (data
== VFIO_MIG_FLAG_END_OF_STATE
) {
615 error_report("%s: SETUP STATE: EOS not found 0x%"PRIx64
,
616 vbasedev
->name
, data
);
621 case VFIO_MIG_FLAG_DEV_DATA_STATE
:
623 uint64_t data_size
= qemu_get_be64(f
);
626 ret
= vfio_load_buffer(f
, vbasedev
, data_size
);
633 case VFIO_MIG_FLAG_DEV_INIT_DATA_SENT
:
635 if (!vfio_precopy_supported(vbasedev
) ||
636 !migrate_switchover_ack()) {
637 error_report("%s: Received INIT_DATA_SENT but switchover ack "
638 "is not used", vbasedev
->name
);
642 ret
= qemu_loadvm_approve_switchover();
645 "%s: qemu_loadvm_approve_switchover failed, err=%d (%s)",
646 vbasedev
->name
, ret
, strerror(-ret
));
652 error_report("%s: Unknown tag 0x%"PRIx64
, vbasedev
->name
, data
);
656 data
= qemu_get_be64(f
);
657 ret
= qemu_file_get_error(f
);
665 static bool vfio_switchover_ack_needed(void *opaque
)
667 VFIODevice
*vbasedev
= opaque
;
669 return vfio_precopy_supported(vbasedev
);
672 static const SaveVMHandlers savevm_vfio_handlers
= {
673 .save_prepare
= vfio_save_prepare
,
674 .save_setup
= vfio_save_setup
,
675 .save_cleanup
= vfio_save_cleanup
,
676 .state_pending_estimate
= vfio_state_pending_estimate
,
677 .state_pending_exact
= vfio_state_pending_exact
,
678 .is_active_iterate
= vfio_is_active_iterate
,
679 .save_live_iterate
= vfio_save_iterate
,
680 .save_live_complete_precopy
= vfio_save_complete_precopy
,
681 .save_state
= vfio_save_state
,
682 .load_setup
= vfio_load_setup
,
683 .load_cleanup
= vfio_load_cleanup
,
684 .load_state
= vfio_load_state
,
685 .switchover_ack_needed
= vfio_switchover_ack_needed
,
688 /* ---------------------------------------------------------------------- */
690 static void vfio_vmstate_change_prepare(void *opaque
, bool running
,
693 VFIODevice
*vbasedev
= opaque
;
694 VFIOMigration
*migration
= vbasedev
->migration
;
695 enum vfio_device_mig_state new_state
;
698 new_state
= migration
->device_state
== VFIO_DEVICE_STATE_PRE_COPY
?
699 VFIO_DEVICE_STATE_PRE_COPY_P2P
:
700 VFIO_DEVICE_STATE_RUNNING_P2P
;
703 * If setting the device in new_state fails, the device should be reset.
704 * To do so, use ERROR state as a recover state.
706 ret
= vfio_migration_set_state(vbasedev
, new_state
,
707 VFIO_DEVICE_STATE_ERROR
);
710 * Migration should be aborted in this case, but vm_state_notify()
711 * currently does not support reporting failures.
713 if (migrate_get_current()->to_dst_file
) {
714 qemu_file_set_error(migrate_get_current()->to_dst_file
, ret
);
718 trace_vfio_vmstate_change_prepare(vbasedev
->name
, running
,
720 mig_state_to_str(new_state
));
723 static void vfio_vmstate_change(void *opaque
, bool running
, RunState state
)
725 VFIODevice
*vbasedev
= opaque
;
726 enum vfio_device_mig_state new_state
;
730 new_state
= VFIO_DEVICE_STATE_RUNNING
;
733 (vfio_device_state_is_precopy(vbasedev
) &&
734 (state
== RUN_STATE_FINISH_MIGRATE
|| state
== RUN_STATE_PAUSED
)) ?
735 VFIO_DEVICE_STATE_STOP_COPY
:
736 VFIO_DEVICE_STATE_STOP
;
740 * If setting the device in new_state fails, the device should be reset.
741 * To do so, use ERROR state as a recover state.
743 ret
= vfio_migration_set_state(vbasedev
, new_state
,
744 VFIO_DEVICE_STATE_ERROR
);
747 * Migration should be aborted in this case, but vm_state_notify()
748 * currently does not support reporting failures.
750 if (migrate_get_current()->to_dst_file
) {
751 qemu_file_set_error(migrate_get_current()->to_dst_file
, ret
);
755 trace_vfio_vmstate_change(vbasedev
->name
, running
, RunState_str(state
),
756 mig_state_to_str(new_state
));
759 static void vfio_migration_state_notifier(Notifier
*notifier
, void *data
)
761 MigrationState
*s
= data
;
762 VFIOMigration
*migration
= container_of(notifier
, VFIOMigration
,
764 VFIODevice
*vbasedev
= migration
->vbasedev
;
766 trace_vfio_migration_state_notifier(vbasedev
->name
,
767 MigrationStatus_str(s
->state
));
770 case MIGRATION_STATUS_CANCELLING
:
771 case MIGRATION_STATUS_CANCELLED
:
772 case MIGRATION_STATUS_FAILED
:
774 * If setting the device in RUNNING state fails, the device should
775 * be reset. To do so, use ERROR state as a recover state.
777 vfio_migration_set_state(vbasedev
, VFIO_DEVICE_STATE_RUNNING
,
778 VFIO_DEVICE_STATE_ERROR
);
782 static void vfio_migration_free(VFIODevice
*vbasedev
)
784 g_free(vbasedev
->migration
);
785 vbasedev
->migration
= NULL
;
788 static int vfio_migration_query_flags(VFIODevice
*vbasedev
, uint64_t *mig_flags
)
790 uint64_t buf
[DIV_ROUND_UP(sizeof(struct vfio_device_feature
) +
791 sizeof(struct vfio_device_feature_migration
),
792 sizeof(uint64_t))] = {};
793 struct vfio_device_feature
*feature
= (struct vfio_device_feature
*)buf
;
794 struct vfio_device_feature_migration
*mig
=
795 (struct vfio_device_feature_migration
*)feature
->data
;
797 feature
->argsz
= sizeof(buf
);
798 feature
->flags
= VFIO_DEVICE_FEATURE_GET
| VFIO_DEVICE_FEATURE_MIGRATION
;
799 if (ioctl(vbasedev
->fd
, VFIO_DEVICE_FEATURE
, feature
)) {
803 *mig_flags
= mig
->flags
;
808 static bool vfio_dma_logging_supported(VFIODevice
*vbasedev
)
810 uint64_t buf
[DIV_ROUND_UP(sizeof(struct vfio_device_feature
),
811 sizeof(uint64_t))] = {};
812 struct vfio_device_feature
*feature
= (struct vfio_device_feature
*)buf
;
814 feature
->argsz
= sizeof(buf
);
815 feature
->flags
= VFIO_DEVICE_FEATURE_PROBE
|
816 VFIO_DEVICE_FEATURE_DMA_LOGGING_START
;
818 return !ioctl(vbasedev
->fd
, VFIO_DEVICE_FEATURE
, feature
);
821 static int vfio_migration_init(VFIODevice
*vbasedev
)
825 VFIOMigration
*migration
;
827 g_autofree
char *path
= NULL
, *oid
= NULL
;
828 uint64_t mig_flags
= 0;
829 VMChangeStateHandler
*prepare_cb
;
831 if (!vbasedev
->ops
->vfio_get_object
) {
835 obj
= vbasedev
->ops
->vfio_get_object(vbasedev
);
840 ret
= vfio_migration_query_flags(vbasedev
, &mig_flags
);
845 /* Basic migration functionality must be supported */
846 if (!(mig_flags
& VFIO_MIGRATION_STOP_COPY
)) {
850 vbasedev
->migration
= g_new0(VFIOMigration
, 1);
851 migration
= vbasedev
->migration
;
852 migration
->vbasedev
= vbasedev
;
853 migration
->device_state
= VFIO_DEVICE_STATE_RUNNING
;
854 migration
->data_fd
= -1;
855 migration
->mig_flags
= mig_flags
;
857 vbasedev
->dirty_pages_supported
= vfio_dma_logging_supported(vbasedev
);
859 oid
= vmstate_if_get_id(VMSTATE_IF(DEVICE(obj
)));
861 path
= g_strdup_printf("%s/vfio", oid
);
863 path
= g_strdup("vfio");
865 strpadcpy(id
, sizeof(id
), path
, '\0');
867 register_savevm_live(id
, VMSTATE_INSTANCE_ID_ANY
, 1, &savevm_vfio_handlers
,
870 prepare_cb
= migration
->mig_flags
& VFIO_MIGRATION_P2P
?
871 vfio_vmstate_change_prepare
:
873 migration
->vm_state
= qdev_add_vm_change_state_handler_full(
874 vbasedev
->dev
, vfio_vmstate_change
, prepare_cb
, vbasedev
);
875 migration
->migration_state
.notify
= vfio_migration_state_notifier
;
876 add_migration_state_change_notifier(&migration
->migration_state
);
881 static void vfio_migration_deinit(VFIODevice
*vbasedev
)
883 VFIOMigration
*migration
= vbasedev
->migration
;
885 remove_migration_state_change_notifier(&migration
->migration_state
);
886 qemu_del_vm_change_state_handler(migration
->vm_state
);
887 unregister_savevm(VMSTATE_IF(vbasedev
->dev
), "vfio", vbasedev
);
888 vfio_migration_free(vbasedev
);
889 vfio_unblock_multiple_devices_migration();
892 static int vfio_block_migration(VFIODevice
*vbasedev
, Error
*err
, Error
**errp
)
896 if (vbasedev
->enable_migration
== ON_OFF_AUTO_ON
) {
897 error_propagate(errp
, err
);
901 vbasedev
->migration_blocker
= error_copy(err
);
904 ret
= migrate_add_blocker(vbasedev
->migration_blocker
, errp
);
906 error_free(vbasedev
->migration_blocker
);
907 vbasedev
->migration_blocker
= NULL
;
913 /* ---------------------------------------------------------------------- */
915 int64_t vfio_mig_bytes_transferred(void)
917 return bytes_transferred
;
920 void vfio_reset_bytes_transferred(void)
922 bytes_transferred
= 0;
926 * Return true when either migration initialized or blocker registered.
927 * Currently only return false when adding blocker fails which will
928 * de-register vfio device.
930 bool vfio_migration_realize(VFIODevice
*vbasedev
, Error
**errp
)
935 if (vbasedev
->enable_migration
== ON_OFF_AUTO_OFF
) {
936 error_setg(&err
, "%s: Migration is disabled for VFIO device",
938 return !vfio_block_migration(vbasedev
, err
, errp
);
941 ret
= vfio_migration_init(vbasedev
);
943 if (ret
== -ENOTTY
) {
944 error_setg(&err
, "%s: VFIO migration is not supported in kernel",
948 "%s: Migration couldn't be initialized for VFIO device, "
950 vbasedev
->name
, ret
, strerror(-ret
));
953 return !vfio_block_migration(vbasedev
, err
, errp
);
956 if (!vbasedev
->dirty_pages_supported
) {
957 if (vbasedev
->enable_migration
== ON_OFF_AUTO_AUTO
) {
959 "%s: VFIO device doesn't support device dirty tracking",
964 warn_report("%s: VFIO device doesn't support device dirty tracking",
968 ret
= vfio_block_multiple_devices_migration(vbasedev
, errp
);
973 if (vfio_viommu_preset(vbasedev
)) {
974 error_setg(&err
, "%s: Migration is currently not supported "
975 "with vIOMMU enabled", vbasedev
->name
);
979 trace_vfio_migration_realize(vbasedev
->name
);
983 ret
= vfio_block_migration(vbasedev
, err
, errp
);
986 vfio_migration_deinit(vbasedev
);
991 void vfio_migration_exit(VFIODevice
*vbasedev
)
993 if (vbasedev
->migration
) {
994 vfio_migration_deinit(vbasedev
);
997 if (vbasedev
->migration_blocker
) {
998 migrate_del_blocker(vbasedev
->migration_blocker
);
999 error_free(vbasedev
->migration_blocker
);
1000 vbasedev
->migration_blocker
= NULL
;