migration: cpr helpers

Add the cpr_incoming_needed, cpr_open_fd, and cpr_resave_fd helpers,
for use when adding cpr support for vfio and iommufd.

Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Link: https://lore.kernel.org/qemu-devel/1749569991-25171-2-git-send-email-steven.sistare@oracle.com
Signed-off-by: Cédric Le Goater <clg@redhat.com>
This commit is contained in:
Steve Sistare 2025-06-10 08:39:14 -07:00 committed by Cédric Le Goater
parent a574b06144
commit f95fd60ac1
2 changed files with 41 additions and 0 deletions

View file

@ -18,6 +18,9 @@
void cpr_save_fd(const char *name, int id, int fd);
void cpr_delete_fd(const char *name, int id);
int cpr_find_fd(const char *name, int id);
void cpr_resave_fd(const char *name, int id, int fd);
int cpr_open_fd(const char *path, int flags, const char *name, int id,
Error **errp);
MigMode cpr_get_incoming_mode(void);
void cpr_set_incoming_mode(MigMode mode);
@ -28,6 +31,8 @@ int cpr_state_load(MigrationChannel *channel, Error **errp);
void cpr_state_close(void);
struct QIOChannel *cpr_state_ioc(void);
bool cpr_incoming_needed(void *opaque);
QEMUFile *cpr_transfer_output(MigrationChannel *channel, Error **errp);
QEMUFile *cpr_transfer_input(MigrationChannel *channel, Error **errp);

View file

@ -95,6 +95,36 @@ int cpr_find_fd(const char *name, int id)
trace_cpr_find_fd(name, id, fd);
return fd;
}
void cpr_resave_fd(const char *name, int id, int fd)
{
CprFd *elem = find_fd(&cpr_state.fds, name, id);
int old_fd = elem ? elem->fd : -1;
if (old_fd < 0) {
cpr_save_fd(name, id, fd);
} else if (old_fd != fd) {
error_setg(&error_fatal,
"internal error: cpr fd '%s' id %d value %d "
"already saved with a different value %d",
name, id, fd, old_fd);
}
}
int cpr_open_fd(const char *path, int flags, const char *name, int id,
Error **errp)
{
int fd = cpr_find_fd(name, id);
if (fd < 0) {
fd = qemu_open(path, flags, errp);
if (fd >= 0) {
cpr_save_fd(name, id, fd);
}
}
return fd;
}
/*************************************************************************/
#define CPR_STATE "CprState"
@ -228,3 +258,9 @@ void cpr_state_close(void)
cpr_state_file = NULL;
}
}
bool cpr_incoming_needed(void *opaque)
{
MigMode mode = migrate_mode();
return mode == MIG_MODE_CPR_TRANSFER;
}