diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2016-01-31 11:28:59 +0100 |
---|---|---|
committer | Michael S. Tsirkin <mst@redhat.com> | 2016-02-06 20:44:08 +0200 |
commit | ab281c1781add112a257c15924d670616a99c41a (patch) | |
tree | 4b24857c83b3c5494cecf1836c874ea385c79d04 | |
parent | 51b19ebe4320f3dcd93cea71235c1219318ddfd2 (diff) |
virtio: introduce qemu_get/put_virtqueue_element
Move allocation to virtio functions also when loading/saving a
VirtQueueElement. This will also let the load/save functions
keep backwards compatibility when the VirtQueueElement layout
is changed.
Reviewed-by: Cornelia Huck <cornelia.huck@de.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
-rw-r--r-- | hw/block/virtio-blk.c | 10 | ||||
-rw-r--r-- | hw/char/virtio-serial-bus.c | 10 | ||||
-rw-r--r-- | hw/scsi/virtio-scsi.c | 7 | ||||
-rw-r--r-- | hw/virtio/virtio.c | 13 | ||||
-rw-r--r-- | include/hw/virtio/virtio.h | 2 |
5 files changed, 23 insertions, 19 deletions
diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index bf70b52dfd..c427698fcb 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -808,8 +808,7 @@ static void virtio_blk_save_device(VirtIODevice *vdev, QEMUFile *f) while (req) { qemu_put_sbyte(f, 1); - qemu_put_buffer(f, (unsigned char *)&req->elem, - sizeof(VirtQueueElement)); + qemu_put_virtqueue_element(f, &req->elem); req = req->next; } qemu_put_sbyte(f, 0); @@ -832,14 +831,11 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, VirtIOBlock *s = VIRTIO_BLK(vdev); while (qemu_get_sbyte(f)) { - VirtIOBlockReq *req = g_new(VirtIOBlockReq, 1); + VirtIOBlockReq *req; + req = qemu_get_virtqueue_element(f, sizeof(VirtIOBlockReq)); virtio_blk_init_request(s, req); - qemu_get_buffer(f, (unsigned char *)&req->elem, - sizeof(VirtQueueElement)); req->next = s->rq; s->rq = req; - - virtqueue_map(&req->elem); } return 0; diff --git a/hw/char/virtio-serial-bus.c b/hw/char/virtio-serial-bus.c index cf3d12b9d5..99cb6836ad 100644 --- a/hw/char/virtio-serial-bus.c +++ b/hw/char/virtio-serial-bus.c @@ -646,9 +646,7 @@ static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f) if (elem_popped) { qemu_put_be32s(f, &port->iov_idx); qemu_put_be64s(f, &port->iov_offset); - - qemu_put_buffer(f, (unsigned char *)port->elem, - sizeof(VirtQueueElement)); + qemu_put_virtqueue_element(f, port->elem); } } } @@ -723,10 +721,8 @@ static int fetch_active_ports_list(QEMUFile *f, int version_id, qemu_get_be32s(f, &port->iov_idx); qemu_get_be64s(f, &port->iov_offset); - port->elem = g_new(VirtQueueElement, 1); - qemu_get_buffer(f, (unsigned char *)port->elem, - sizeof(VirtQueueElement)); - virtqueue_map(port->elem); + port->elem = + qemu_get_virtqueue_element(f, sizeof(VirtQueueElement)); /* * Port was throttled on source machine. Let's diff --git a/hw/scsi/virtio-scsi.c b/hw/scsi/virtio-scsi.c index 50a3cb28b5..5b29baccf3 100644 --- a/hw/scsi/virtio-scsi.c +++ b/hw/scsi/virtio-scsi.c @@ -189,7 +189,7 @@ static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq) assert(n < vs->conf.num_queues); qemu_put_be32s(f, &n); - qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); + qemu_put_virtqueue_element(f, &req->elem); } static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) @@ -202,12 +202,9 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) qemu_get_be32s(f, &n); assert(n < vs->conf.num_queues); - req = g_malloc(sizeof(VirtIOSCSIReq) + vs->cdb_size); - qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); + req = qemu_get_virtqueue_element(f, sizeof(VirtIOSCSIReq) + vs->cdb_size); virtio_scsi_init_req(s, vs->cmd_vqs[n], req); - virtqueue_map(&req->elem); - if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) { error_report("invalid SCSI request migration data"); diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 229a092c83..28fa7fe760 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -576,6 +576,19 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz) return elem; } +void *qemu_get_virtqueue_element(QEMUFile *f, size_t sz) +{ + VirtQueueElement *elem = g_malloc(sz); + qemu_get_buffer(f, (uint8_t *)elem, sizeof(VirtQueueElement)); + virtqueue_map(elem); + return elem; +} + +void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem) +{ + qemu_put_buffer(f, (uint8_t *)elem, sizeof(VirtQueueElement)); +} + /* virtio device */ static void virtio_notify_vector(VirtIODevice *vdev, uint16_t vector) { diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 21fda17807..44da9a8433 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -153,6 +153,8 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, void virtqueue_map(VirtQueueElement *elem); void *virtqueue_pop(VirtQueue *vq, size_t sz); +void *qemu_get_virtqueue_element(QEMUFile *f, size_t sz); +void qemu_put_virtqueue_element(QEMUFile *f, VirtQueueElement *elem); int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, unsigned int out_bytes); void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, |