The callback allows SVQ users to know the VirtQueue requests and responses. QEMU can use this to synchronize virtio device model state, allowing to migrate it with minimum changes to the migration code.
If callbacks are specified at svq creation, the buffers need to be injected to the device using vhost_svq_inject. An opaque data must be given with it, and its returned to the callback at used_handler call. In the case of networking, this will be used to inspect control virtqueue messages and to recover status injection at the first time. Signed-off-by: Eugenio Pérez <epere...@redhat.com> --- hw/virtio/vhost-shadow-virtqueue.h | 5 +++++ hw/virtio/vhost-shadow-virtqueue.c | 16 +++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/hw/virtio/vhost-shadow-virtqueue.h b/hw/virtio/vhost-shadow-virtqueue.h index c8668fbdd6..296fef6f21 100644 --- a/hw/virtio/vhost-shadow-virtqueue.h +++ b/hw/virtio/vhost-shadow-virtqueue.h @@ -27,8 +27,13 @@ typedef struct VhostShadowVirtqueue VhostShadowVirtqueue; typedef int (*ShadowVirtQueueStart)(VhostShadowVirtqueue *svq, void *opaque); +typedef void (*VirtQueueUsedCallback)(VhostShadowVirtqueue *svq, + void *used_elem_opaque, + uint32_t written); + typedef struct VhostShadowVirtqueueOps { ShadowVirtQueueStart start; + VirtQueueUsedCallback used_handler; } VhostShadowVirtqueueOps; /* Shadow virtqueue to relay notifications */ diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c index ed7f1d0bc9..b92ca4a63f 100644 --- a/hw/virtio/vhost-shadow-virtqueue.c +++ b/hw/virtio/vhost-shadow-virtqueue.c @@ -506,7 +506,6 @@ static size_t vhost_svq_flush(VhostShadowVirtqueue *svq, while (true) { uint32_t len; SVQElement svq_elem; - g_autofree VirtQueueElement *elem = NULL; if (unlikely(i >= svq->vring.num)) { qemu_log_mask(LOG_GUEST_ERROR, @@ -521,13 +520,20 @@ static size_t vhost_svq_flush(VhostShadowVirtqueue *svq, break; } - elem = g_steal_pointer(&svq_elem.opaque); - virtqueue_fill(vq, elem, len, i++); + if (svq->ops) { + svq->ops->used_handler(svq, svq_elem.opaque, len); + } else { + g_autofree VirtQueueElement *elem = NULL; + elem = g_steal_pointer(&svq_elem.opaque); + virtqueue_fill(vq, elem, len, i++); + } ret++; } - virtqueue_flush(vq, i); - event_notifier_set(&svq->svq_call); + if (i > 0) { + virtqueue_flush(vq, i); + event_notifier_set(&svq->svq_call); + } if (check_for_avail_queue && svq->next_guest_avail_elem) { /* -- 2.31.1