The core of the function is useful to transform from VirtQueueElementOld to VirtQueueElement. Extract from qemu_get_virtqueue_element, and leave there the handling of QEMUFile.
No functional change intended. Signed-off-by: Eugenio Pérez <epere...@redhat.com> --- hw/virtio/virtio.c | 68 ++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 6efff3d441..b0245ce4e8 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -2297,50 +2297,66 @@ typedef struct VirtQueueElementOld { struct iovec out_sg[VIRTQUEUE_MAX_SIZE]; } VirtQueueElementOld; -void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz) +/* Convert VirtQueueElementOld to VirtQueueElement */ +static void *qemu_get_virtqueue_element_from_old(VirtIODevice *vdev, + const VirtQueueElementOld *data, + size_t sz) { - VirtQueueElement *elem; - VirtQueueElementOld data; - int i; - - qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); - - /* TODO: teach all callers that this can fail, and return failure instead - * of asserting here. - * This is just one thing (there are probably more) that must be - * fixed before we can allow NDEBUG compilation. - */ - assert(ARRAY_SIZE(data.in_addr) >= data.in_num); - assert(ARRAY_SIZE(data.out_addr) >= data.out_num); - - elem = virtqueue_alloc_element(sz, data.out_num, data.in_num); - elem->index = data.index; + VirtQueueElement *elem = virtqueue_alloc_element(sz, data->out_num, + data->in_num); + elem->index = data->index; - for (i = 0; i < elem->in_num; i++) { - elem->in_addr[i] = data.in_addr[i]; + for (uint16_t i = 0; i < elem->in_num; i++) { + elem->in_addr[i] = data->in_addr[i]; } - for (i = 0; i < elem->out_num; i++) { - elem->out_addr[i] = data.out_addr[i]; + for (uint16_t i = 0; i < elem->out_num; i++) { + elem->out_addr[i] = data->out_addr[i]; } - for (i = 0; i < elem->in_num; i++) { + for (uint16_t i = 0; i < elem->in_num; i++) { /* Base is overwritten by virtqueue_map. */ elem->in_sg[i].iov_base = 0; - elem->in_sg[i].iov_len = data.in_sg[i].iov_len; + elem->in_sg[i].iov_len = data->in_sg[i].iov_len; } - for (i = 0; i < elem->out_num; i++) { + for (uint16_t i = 0; i < elem->out_num; i++) { /* Base is overwritten by virtqueue_map. */ elem->out_sg[i].iov_base = 0; - elem->out_sg[i].iov_len = data.out_sg[i].iov_len; + elem->out_sg[i].iov_len = data->out_sg[i].iov_len; } + virtqueue_map(vdev, elem); + return elem; +} + +static bool vq_element_in_range(void *opaque, int version_id) +{ + VirtQueueElementOld *data = opaque; + + return ARRAY_SIZE(data->in_addr) >= data->in_num && + ARRAY_SIZE(data->out_addr) >= data->out_num; +} + +void *qemu_get_virtqueue_element(VirtIODevice *vdev, QEMUFile *f, size_t sz) +{ + VirtQueueElement *elem; + VirtQueueElementOld data; + + qemu_get_buffer(f, (uint8_t *)&data, sizeof(VirtQueueElementOld)); + + /* TODO: teach all callers that this can fail, and return failure instead + * of asserting here. + * This is just one thing (there are probably more) that must be + * fixed before we can allow NDEBUG compilation. + */ + assert(vq_element_in_range(&data, 0)); + + elem = qemu_get_virtqueue_element_from_old(vdev, &data, sz); if (virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED)) { qemu_get_be32s(f, &elem->ndescs); } - virtqueue_map(vdev, elem); return elem; } -- 2.31.1