2012-11-14 18:39:30 +04:00
|
|
|
/*
|
|
|
|
* Dedicated thread for virtio-blk I/O processing
|
|
|
|
*
|
|
|
|
* Copyright 2012 IBM, Corp.
|
|
|
|
* Copyright 2012 Red Hat, Inc. and/or its affiliates
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "trace.h"
|
|
|
|
#include "qemu/iov.h"
|
|
|
|
#include "qemu/thread.h"
|
2013-02-04 14:37:52 +04:00
|
|
|
#include "qemu/error-report.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/virtio/dataplane/vring.h"
|
2014-10-07 15:59:18 +04:00
|
|
|
#include "sysemu/block-backend.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/virtio/virtio-blk.h"
|
|
|
|
#include "virtio-blk.h"
|
2013-02-22 13:40:34 +04:00
|
|
|
#include "block/aio.h"
|
2013-04-24 12:21:21 +04:00
|
|
|
#include "hw/virtio/virtio-bus.h"
|
2014-03-20 18:06:32 +04:00
|
|
|
#include "qom/object_interfaces.h"
|
2012-11-14 18:39:30 +04:00
|
|
|
|
|
|
|
struct VirtIOBlockDataPlane {
|
|
|
|
bool started;
|
2013-09-04 16:16:15 +04:00
|
|
|
bool starting;
|
2013-01-15 20:19:38 +04:00
|
|
|
bool stopping;
|
2014-07-25 16:10:48 +04:00
|
|
|
bool disabled;
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-10-07 15:59:17 +04:00
|
|
|
VirtIOBlkConf *conf;
|
2012-11-14 18:39:30 +04:00
|
|
|
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
Vring vring; /* virtqueue vring */
|
|
|
|
EventNotifier *guest_notifier; /* irq */
|
2014-07-12 08:08:53 +04:00
|
|
|
QEMUBH *bh; /* bh for guest notification */
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2013-02-22 13:40:34 +04:00
|
|
|
/* Note that these EventNotifiers are assigned by value. This is
|
|
|
|
* fine as long as you do not call event_notifier_cleanup on them
|
|
|
|
* (because you don't own the file descriptor or handle; you just
|
|
|
|
* use it).
|
|
|
|
*/
|
2014-03-03 14:30:08 +04:00
|
|
|
IOThread *iothread;
|
2014-03-20 18:06:32 +04:00
|
|
|
IOThread internal_iothread_obj;
|
2013-02-22 13:40:34 +04:00
|
|
|
AioContext *ctx;
|
|
|
|
EventNotifier host_notifier; /* doorbell */
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-05-23 17:29:43 +04:00
|
|
|
/* Operation blocker on BDS */
|
|
|
|
Error *blocker;
|
2014-06-17 10:32:09 +04:00
|
|
|
void (*saved_complete_request)(struct VirtIOBlockReq *req,
|
|
|
|
unsigned char status);
|
2012-11-14 18:39:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Raise an interrupt to signal guest, if necessary */
|
|
|
|
static void notify_guest(VirtIOBlockDataPlane *s)
|
|
|
|
{
|
|
|
|
if (!vring_should_notify(s->vdev, &s->vring)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event_notifier_set(s->guest_notifier);
|
|
|
|
}
|
|
|
|
|
2014-07-12 08:08:53 +04:00
|
|
|
static void notify_guest_bh(void *opaque)
|
|
|
|
{
|
|
|
|
VirtIOBlockDataPlane *s = opaque;
|
|
|
|
|
|
|
|
notify_guest(s);
|
|
|
|
}
|
|
|
|
|
2014-06-17 10:32:10 +04:00
|
|
|
static void complete_request_vring(VirtIOBlockReq *req, unsigned char status)
|
2012-11-14 18:39:30 +04:00
|
|
|
{
|
2014-07-12 08:08:53 +04:00
|
|
|
VirtIOBlockDataPlane *s = req->dev->dataplane;
|
2014-06-11 08:11:49 +04:00
|
|
|
stb_p(&req->in->status, status);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-07-09 12:05:49 +04:00
|
|
|
vring_push(&req->dev->dataplane->vring, &req->elem,
|
2014-06-17 10:32:09 +04:00
|
|
|
req->qiov.size + sizeof(*req->in));
|
2014-07-12 08:08:53 +04:00
|
|
|
|
|
|
|
/* Suppress notification to guest by BH and its scheduled
|
|
|
|
* flag because requests are completed as a batch after io
|
|
|
|
* plug & unplug is introduced, and the BH can still be
|
|
|
|
* executed in dataplane aio context even after it is
|
|
|
|
* stopped, so needn't worry about notification loss with BH.
|
|
|
|
*/
|
|
|
|
qemu_bh_schedule(s->bh);
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
|
|
|
|
2013-02-22 13:40:34 +04:00
|
|
|
static void handle_notify(EventNotifier *e)
|
2012-11-14 18:39:30 +04:00
|
|
|
{
|
2013-02-22 13:40:34 +04:00
|
|
|
VirtIOBlockDataPlane *s = container_of(e, VirtIOBlockDataPlane,
|
|
|
|
host_notifier);
|
2014-07-09 12:05:49 +04:00
|
|
|
VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2013-02-22 13:40:34 +04:00
|
|
|
event_notifier_test_and_clear(&s->host_notifier);
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_io_plug(s->conf->conf.blk);
|
2012-11-14 18:39:30 +04:00
|
|
|
for (;;) {
|
2014-07-09 12:05:49 +04:00
|
|
|
MultiReqBuffer mrb = {
|
|
|
|
.num_writes = 0,
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
2012-11-14 18:39:30 +04:00
|
|
|
/* Disable guest->host notifies to avoid unnecessary vmexits */
|
|
|
|
vring_disable_notification(s->vdev, &s->vring);
|
|
|
|
|
|
|
|
for (;;) {
|
2014-07-09 12:05:49 +04:00
|
|
|
VirtIOBlockReq *req = virtio_blk_alloc_request(vblk);
|
|
|
|
|
|
|
|
ret = vring_pop(s->vdev, &s->vring, &req->elem);
|
2013-12-10 16:27:00 +04:00
|
|
|
if (ret < 0) {
|
2014-07-09 12:05:49 +04:00
|
|
|
virtio_blk_free_request(req);
|
2012-11-14 18:39:30 +04:00
|
|
|
break; /* no more requests */
|
|
|
|
}
|
|
|
|
|
2014-07-09 12:05:49 +04:00
|
|
|
trace_virtio_blk_data_plane_process_request(s, req->elem.out_num,
|
|
|
|
req->elem.in_num,
|
|
|
|
req->elem.index);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-06-17 10:32:09 +04:00
|
|
|
virtio_blk_handle_request(req, &mrb);
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
virtio_submit_multiwrite(s->conf->conf.blk, &mrb);
|
2014-06-17 10:32:09 +04:00
|
|
|
|
2013-12-10 16:27:00 +04:00
|
|
|
if (likely(ret == -EAGAIN)) { /* vring emptied */
|
2012-11-14 18:39:30 +04:00
|
|
|
/* Re-enable guest->host notifies and stop processing the vring.
|
|
|
|
* But if the guest has snuck in more descriptors, keep processing.
|
|
|
|
*/
|
|
|
|
if (vring_enable_notification(s->vdev, &s->vring)) {
|
|
|
|
break;
|
|
|
|
}
|
2014-05-08 18:34:55 +04:00
|
|
|
} else { /* fatal error */
|
2012-11-14 18:39:30 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_io_unplug(s->conf->conf.blk);
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
/* Context: QEMU global mutex held */
|
2014-10-07 15:59:17 +04:00
|
|
|
void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf,
|
2013-06-07 18:18:50 +04:00
|
|
|
VirtIOBlockDataPlane **dataplane,
|
|
|
|
Error **errp)
|
2012-11-14 18:39:30 +04:00
|
|
|
{
|
|
|
|
VirtIOBlockDataPlane *s;
|
2014-05-23 17:29:43 +04:00
|
|
|
Error *local_err = NULL;
|
2014-06-18 13:58:30 +04:00
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
|
|
|
*dataplane = NULL;
|
|
|
|
|
2014-10-07 15:59:17 +04:00
|
|
|
if (!conf->data_plane && !conf->iothread) {
|
2013-06-07 18:18:50 +04:00
|
|
|
return;
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 13:58:30 +04:00
|
|
|
/* Don't try if transport does not support notifiers. */
|
|
|
|
if (!k->set_guest_notifiers || !k->set_host_notifier) {
|
|
|
|
error_setg(errp,
|
|
|
|
"device is incompatible with x-data-plane "
|
|
|
|
"(transport does not support notifiers)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-29 17:02:00 +04:00
|
|
|
/* If dataplane is (re-)enabled while the guest is running there could be
|
|
|
|
* block jobs that can conflict.
|
|
|
|
*/
|
2014-10-07 15:59:18 +04:00
|
|
|
if (blk_op_is_blocked(conf->conf.blk, BLOCK_OP_TYPE_DATAPLANE,
|
|
|
|
&local_err)) {
|
2014-09-11 16:49:39 +04:00
|
|
|
error_setg(errp, "cannot start dataplane thread: %s",
|
|
|
|
error_get_pretty(local_err));
|
2014-05-23 17:29:43 +04:00
|
|
|
error_free(local_err);
|
2013-06-07 18:18:50 +04:00
|
|
|
return;
|
2013-07-29 17:02:00 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:39:30 +04:00
|
|
|
s = g_new0(VirtIOBlockDataPlane, 1);
|
|
|
|
s->vdev = vdev;
|
2014-10-07 15:59:17 +04:00
|
|
|
s->conf = conf;
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-10-07 15:59:17 +04:00
|
|
|
if (conf->iothread) {
|
|
|
|
s->iothread = conf->iothread;
|
2014-03-20 18:06:32 +04:00
|
|
|
object_ref(OBJECT(s->iothread));
|
2014-03-03 14:30:08 +04:00
|
|
|
} else {
|
2014-03-20 18:06:32 +04:00
|
|
|
/* Create per-device IOThread if none specified. This is for
|
|
|
|
* x-data-plane option compatibility. If x-data-plane is removed we
|
|
|
|
* can drop this.
|
|
|
|
*/
|
|
|
|
object_initialize(&s->internal_iothread_obj,
|
|
|
|
sizeof(s->internal_iothread_obj),
|
|
|
|
TYPE_IOTHREAD);
|
|
|
|
user_creatable_complete(OBJECT(&s->internal_iothread_obj), &error_abort);
|
|
|
|
s->iothread = &s->internal_iothread_obj;
|
2014-03-03 14:30:08 +04:00
|
|
|
}
|
|
|
|
s->ctx = iothread_get_aio_context(s->iothread);
|
2014-07-12 08:08:53 +04:00
|
|
|
s->bh = aio_bh_new(s->ctx, notify_guest_bh, s);
|
2014-03-03 14:30:08 +04:00
|
|
|
|
2014-05-23 17:29:43 +04:00
|
|
|
error_setg(&s->blocker, "block device is in use by data plane");
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_op_block_all(conf->conf.blk, s->blocker);
|
|
|
|
blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_RESIZE, s->blocker);
|
|
|
|
blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_DRIVE_DEL, s->blocker);
|
2014-10-21 15:04:00 +04:00
|
|
|
blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_BACKUP_SOURCE, s->blocker);
|
|
|
|
blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_COMMIT, s->blocker);
|
|
|
|
blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_MIRROR, s->blocker);
|
|
|
|
blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_STREAM, s->blocker);
|
|
|
|
blk_op_unblock(conf->conf.blk, BLOCK_OP_TYPE_REPLACE, s->blocker);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
|
|
|
*dataplane = s;
|
|
|
|
}
|
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
/* Context: QEMU global mutex held */
|
2012-11-14 18:39:30 +04:00
|
|
|
void virtio_blk_data_plane_destroy(VirtIOBlockDataPlane *s)
|
|
|
|
{
|
|
|
|
if (!s) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtio_blk_data_plane_stop(s);
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_op_unblock_all(s->conf->conf.blk, s->blocker);
|
2014-05-23 17:29:43 +04:00
|
|
|
error_free(s->blocker);
|
2014-03-03 14:30:08 +04:00
|
|
|
object_unref(OBJECT(s->iothread));
|
2014-07-12 08:08:53 +04:00
|
|
|
qemu_bh_delete(s->bh);
|
2012-11-14 18:39:30 +04:00
|
|
|
g_free(s);
|
|
|
|
}
|
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
/* Context: QEMU global mutex held */
|
2012-11-14 18:39:30 +04:00
|
|
|
void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)
|
|
|
|
{
|
2013-04-24 12:21:21 +04:00
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2014-07-12 08:08:52 +04:00
|
|
|
VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
|
2012-11-14 18:39:30 +04:00
|
|
|
VirtQueue *vq;
|
2014-07-25 16:10:46 +04:00
|
|
|
int r;
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-07-25 16:10:48 +04:00
|
|
|
if (s->started || s->disabled) {
|
2012-11-14 18:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-04 16:16:15 +04:00
|
|
|
if (s->starting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->starting = true;
|
|
|
|
|
2012-11-14 18:39:30 +04:00
|
|
|
vq = virtio_get_queue(s->vdev, 0);
|
|
|
|
if (!vring_setup(&s->vring, s->vdev, 0)) {
|
2014-07-25 16:10:47 +04:00
|
|
|
goto fail_vring;
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up guest notifier (irq) */
|
2014-07-25 16:10:46 +04:00
|
|
|
r = k->set_guest_notifiers(qbus->parent, 1, true);
|
|
|
|
if (r != 0) {
|
|
|
|
fprintf(stderr, "virtio-blk failed to set guest notifier (%d), "
|
|
|
|
"ensure -enable-kvm is set\n", r);
|
2014-07-25 16:10:47 +04:00
|
|
|
goto fail_guest_notifiers;
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
|
|
|
s->guest_notifier = virtio_queue_get_guest_notifier(vq);
|
|
|
|
|
|
|
|
/* Set up virtqueue notify */
|
2014-07-25 16:10:46 +04:00
|
|
|
r = k->set_host_notifier(qbus->parent, 0, true);
|
|
|
|
if (r != 0) {
|
|
|
|
fprintf(stderr, "virtio-blk failed to set host notifier (%d)\n", r);
|
2014-07-25 16:10:47 +04:00
|
|
|
goto fail_host_notifier;
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
2013-02-22 13:40:34 +04:00
|
|
|
s->host_notifier = *virtio_queue_get_host_notifier(vq);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-07-12 08:08:52 +04:00
|
|
|
s->saved_complete_request = vblk->complete_request;
|
|
|
|
vblk->complete_request = complete_request_vring;
|
|
|
|
|
2013-09-04 16:16:15 +04:00
|
|
|
s->starting = false;
|
2012-11-14 18:39:30 +04:00
|
|
|
s->started = true;
|
|
|
|
trace_virtio_blk_data_plane_start(s);
|
|
|
|
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_set_aio_context(s->conf->conf.blk, s->ctx);
|
2014-05-08 18:34:55 +04:00
|
|
|
|
2012-11-14 18:39:30 +04:00
|
|
|
/* Kick right away to begin processing requests already in vring */
|
|
|
|
event_notifier_set(virtio_queue_get_host_notifier(vq));
|
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
/* Get this show started by hooking up our callbacks */
|
|
|
|
aio_context_acquire(s->ctx);
|
|
|
|
aio_set_event_notifier(s->ctx, &s->host_notifier, handle_notify);
|
|
|
|
aio_context_release(s->ctx);
|
2014-07-25 16:10:47 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail_host_notifier:
|
|
|
|
k->set_guest_notifiers(qbus->parent, 1, false);
|
|
|
|
fail_guest_notifiers:
|
|
|
|
vring_teardown(&s->vring, s->vdev, 0);
|
2014-07-25 16:10:48 +04:00
|
|
|
s->disabled = true;
|
2014-07-25 16:10:47 +04:00
|
|
|
fail_vring:
|
|
|
|
s->starting = false;
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
/* Context: QEMU global mutex held */
|
2012-11-14 18:39:30 +04:00
|
|
|
void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)
|
|
|
|
{
|
2013-04-24 12:21:21 +04:00
|
|
|
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(s->vdev)));
|
|
|
|
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
|
2014-06-17 10:32:09 +04:00
|
|
|
VirtIOBlock *vblk = VIRTIO_BLK(s->vdev);
|
2014-07-25 16:10:48 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* Better luck next time. */
|
|
|
|
if (s->disabled) {
|
|
|
|
s->disabled = false;
|
|
|
|
return;
|
|
|
|
}
|
2013-01-15 20:19:38 +04:00
|
|
|
if (!s->started || s->stopping) {
|
2012-11-14 18:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-01-15 20:19:38 +04:00
|
|
|
s->stopping = true;
|
2014-06-17 10:32:09 +04:00
|
|
|
vblk->complete_request = s->saved_complete_request;
|
2012-11-14 18:39:30 +04:00
|
|
|
trace_virtio_blk_data_plane_stop(s);
|
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
aio_context_acquire(s->ctx);
|
|
|
|
|
|
|
|
/* Stop notifications for new requests from guest */
|
|
|
|
aio_set_event_notifier(s->ctx, &s->host_notifier, NULL);
|
|
|
|
|
2014-05-08 18:34:55 +04:00
|
|
|
/* Drain and switch bs back to the QEMU main loop */
|
2014-10-07 15:59:18 +04:00
|
|
|
blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
aio_context_release(s->ctx);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2014-03-03 14:30:08 +04:00
|
|
|
/* Sync vring state back to virtqueue so that non-dataplane request
|
|
|
|
* processing can continue when we disable the host notifier below.
|
|
|
|
*/
|
|
|
|
vring_teardown(&s->vring, s->vdev, 0);
|
|
|
|
|
|
|
|
k->set_host_notifier(qbus->parent, 0, false);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
|
|
|
/* Clean up guest notifier (irq) */
|
2013-04-24 12:21:21 +04:00
|
|
|
k->set_guest_notifiers(qbus->parent, 1, false);
|
2012-11-14 18:39:30 +04:00
|
|
|
|
2013-01-15 20:19:38 +04:00
|
|
|
s->started = false;
|
|
|
|
s->stopping = false;
|
2012-11-14 18:39:30 +04:00
|
|
|
}
|