1
0
mirror of https://passt.top/passt synced 2025-01-21 03:35:17 +00:00

vhost-user: add VHOST_USER_SET_DEVICE_STATE_FD command

Set the file descriptor to use to transfer the
backend device state during migration.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
[sbrivio: Fixed nits and coding style here and there]
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Laurent Vivier 2024-12-19 12:13:59 +01:00 committed by Stefano Brivio
parent 878e163454
commit 31d70024be
6 changed files with 138 additions and 2 deletions

View File

@ -40,6 +40,8 @@ enum epoll_type {
EPOLL_TYPE_VHOST_CMD,
/* vhost-user kick event socket */
EPOLL_TYPE_VHOST_KICK,
/* vhost-user migration socket */
EPOLL_TYPE_VHOST_MIGRATION,
EPOLL_NUM_TYPES,
};

View File

@ -75,6 +75,7 @@ char *epoll_type_str[] = {
[EPOLL_TYPE_TAP_LISTEN] = "listening qemu socket",
[EPOLL_TYPE_VHOST_CMD] = "vhost-user command socket",
[EPOLL_TYPE_VHOST_KICK] = "vhost-user kick socket",
[EPOLL_TYPE_VHOST_MIGRATION] = "vhost-user migration socket",
};
static_assert(ARRAY_SIZE(epoll_type_str) == EPOLL_NUM_TYPES,
"epoll_type_str[] doesn't match enum epoll_type");
@ -356,6 +357,9 @@ loop:
case EPOLL_TYPE_VHOST_KICK:
vu_kick_cb(c.vdev, ref, &now);
break;
case EPOLL_TYPE_VHOST_MIGRATION:
vu_migrate(c.vdev, eventmask);
break;
default:
/* Can't happen */
ASSERT(0);

View File

@ -981,7 +981,78 @@ static bool vu_set_vring_enable_exec(struct vu_dev *vdev,
}
/**
* vu_check_device_state_exec() -- Return device state migration result
* vu_set_migration_watch() - Add the migration file descriptor to epoll
* @vdev: vhost-user device
* @fd: File descriptor to add
* @direction: Direction of the migration (save or load backend state)
*/
static void vu_set_migration_watch(const struct vu_dev *vdev, int fd,
uint32_t direction)
{
union epoll_ref ref = {
.type = EPOLL_TYPE_VHOST_MIGRATION,
.fd = fd,
};
struct epoll_event ev = { 0 };
ev.data.u64 = ref.u64;
switch (direction) {
case VHOST_USER_TRANSFER_STATE_DIRECTION_SAVE:
ev.events = EPOLLOUT;
break;
case VHOST_USER_TRANSFER_STATE_DIRECTION_LOAD:
ev.events = EPOLLIN;
break;
default:
ASSERT(0);
}
epoll_ctl(vdev->context->epollfd, EPOLL_CTL_ADD, ref.fd, &ev);
}
/**
* vu_set_device_state_fd_exec() - Set the device state migration channel
* @vdev: vhost-user device
* @vmsg: vhost-user message
*
* Return: True as the reply contains 0 to indicate success
* and set bit 8 as we don't provide our own fd.
*/
static bool vu_set_device_state_fd_exec(struct vu_dev *vdev,
struct vhost_user_msg *msg)
{
unsigned int direction = msg->payload.transfer_state.direction;
unsigned int phase = msg->payload.transfer_state.phase;
if (msg->fd_num != 1)
die("Invalid device_state_fd message");
if (phase != VHOST_USER_TRANSFER_STATE_PHASE_STOPPED)
die("Invalid device_state_fd phase: %d", phase);
if (direction != VHOST_USER_TRANSFER_STATE_DIRECTION_SAVE &&
direction != VHOST_USER_TRANSFER_STATE_DIRECTION_LOAD)
die("Invalide device_state_fd direction: %d", direction);
if (vdev->device_state_fd != -1) {
vu_remove_watch(vdev, vdev->device_state_fd);
close(vdev->device_state_fd);
}
vdev->device_state_fd = msg->fds[0];
vdev->device_state_result = -1;
vu_set_migration_watch(vdev, vdev->device_state_fd, direction);
debug("Got device_state_fd: %d", vdev->device_state_fd);
/* We don't provide a new fd for the data transfer */
vmsg_set_reply_u64(msg, VHOST_USER_VRING_NOFD_MASK);
return true;
}
/**
* vu_check_device_state_exec() - Return device state migration result
* @vdev: vhost-user device
* @vmsg: vhost-user message
*
@ -1018,6 +1089,7 @@ void vu_init(struct ctx *c)
}
c->vdev->log_table = NULL;
c->vdev->log_call_fd = -1;
c->vdev->device_state_fd = -1;
c->vdev->device_state_result = -1;
}
@ -1068,7 +1140,12 @@ void vu_cleanup(struct vu_dev *vdev)
vu_close_log(vdev);
vdev->device_state_result = -1;
if (vdev->device_state_fd != -1) {
vu_remove_watch(vdev, vdev->device_state_fd);
close(vdev->device_state_fd);
vdev->device_state_fd = -1;
vdev->device_state_result = -1;
}
}
/**
@ -1099,6 +1176,7 @@ static bool (*vu_handle[VHOST_USER_MAX])(struct vu_dev *vdev,
[VHOST_USER_SET_VRING_CALL] = vu_set_vring_call_exec,
[VHOST_USER_SET_VRING_ERR] = vu_set_vring_err_exec,
[VHOST_USER_SET_VRING_ENABLE] = vu_set_vring_enable_exec,
[VHOST_USER_SET_DEVICE_STATE_FD] = vu_set_device_state_fd_exec,
[VHOST_USER_CHECK_DEVICE_STATE] = vu_check_device_state_exec,
};

View File

@ -106,6 +106,7 @@ struct vu_dev_region {
* @log_call_fd: Eventfd to report logging update
* @log_size: Size of the logging memory region
* @log_table: Base of the logging memory region
* @device_state_fd: Device state migration channel
* @device_state_result: Device state migration result
*/
struct vu_dev {
@ -118,6 +119,7 @@ struct vu_dev {
int log_call_fd;
uint64_t log_size;
uint8_t *log_table;
int device_state_fd;
int device_state_result;
};

View File

@ -297,3 +297,52 @@ err:
return -1;
}
/**
* vu_migrate() - Send/receive passt insternal state to/from QEMU
* @vdev: vhost-user device
* @events: epoll events
*/
void vu_migrate(struct vu_dev *vdev, uint32_t events)
{
int ret;
/* TODO: collect/set passt internal state
* and use vdev->device_state_fd to send/receive it
*/
debug("vu_migrate fd %d events %x", vdev->device_state_fd, events);
if (events & EPOLLOUT) {
debug("Saving backend state");
/* send some stuff */
ret = write(vdev->device_state_fd, "PASST", 6);
/* value to be returned by VHOST_USER_CHECK_DEVICE_STATE */
vdev->device_state_result = ret == -1 ? -1 : 0;
/* Closing the file descriptor signals the end of transfer */
epoll_ctl(vdev->context->epollfd, EPOLL_CTL_DEL,
vdev->device_state_fd, NULL);
close(vdev->device_state_fd);
vdev->device_state_fd = -1;
} else if (events & EPOLLIN) {
char buf[6];
debug("Loading backend state");
/* read some stuff */
ret = read(vdev->device_state_fd, buf, sizeof(buf));
/* value to be returned by VHOST_USER_CHECK_DEVICE_STATE */
if (ret != sizeof(buf)) {
vdev->device_state_result = -1;
} else {
ret = strncmp(buf, "PASST", sizeof(buf));
vdev->device_state_result = ret == 0 ? 0 : -1;
}
} else if (events & EPOLLHUP) {
debug("Closing migration channel");
/* The end of file signals the end of the transfer. */
epoll_ctl(vdev->context->epollfd, EPOLL_CTL_DEL,
vdev->device_state_fd, NULL);
close(vdev->device_state_fd);
vdev->device_state_fd = -1;
}
}

View File

@ -57,4 +57,5 @@ void vu_flush(const struct vu_dev *vdev, struct vu_virtq *vq,
void vu_kick_cb(struct vu_dev *vdev, union epoll_ref ref,
const struct timespec *now);
int vu_send_single(const struct ctx *c, const void *buf, size_t size);
void vu_migrate(struct vu_dev *vdev, uint32_t events);
#endif /* VU_COMMON_H */