From nobody Thu Nov 6 12:15:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1541426744315856.2504153102635; Mon, 5 Nov 2018 06:05:44 -0800 (PST) Received: from localhost ([::1]:35464 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gJfVe-0008IK-Ts for importer@patchew.org; Mon, 05 Nov 2018 09:05:42 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gJfTt-0007VS-2q for qemu-devel@nongnu.org; Mon, 05 Nov 2018 09:03:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gJfTo-0005oe-TV for qemu-devel@nongnu.org; Mon, 05 Nov 2018 09:03:52 -0500 Received: from mail-qk1-x744.google.com ([2607:f8b0:4864:20::744]:43237) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gJfTm-0005hA-RK for qemu-devel@nongnu.org; Mon, 05 Nov 2018 09:03:46 -0500 Received: by mail-qk1-x744.google.com with SMTP id r71so14743854qkr.10 for ; Mon, 05 Nov 2018 06:03:44 -0800 (PST) Received: from u1804-ini.default (pool-96-233-74-252.bstnma.fios.verizon.net. [96.233.74.252]) by smtp.googlemail.com with ESMTPSA id q36-v6sm24118344qte.94.2018.11.05.06.03.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Nov 2018 06:03:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vh68S7E9C328s/YTWMsy5uWsUFBalBuc0JnOUE80cUI=; b=iAhUWlRpTM1QFAL5pYTN/jTbNE99rfcRMjSP1SPhu1QYQiKOBwfzVzL7eU+KJcOdZY HFBARY3rxg+ysbyJE0hf5fD1PtaKDZidXn7mLH1EI3SsG87rFtO6fgcx2fSX8lgj8dED CCD2adqhbok1SbEusF3is5eJDvBn2zCFpLQMgGOMjEwLPJWdGUwAEXw9rXAr/YfzevoA 8Q/Sl56Cl4CwMbKkQzYlvOseRuqSjvHm1lnKBZYWkY4eeXXNKf7fFnjZeCWJTkhL4s9q QJN3p+mLRVjEsZRiWrTiOw1DHuQ4TcVKOyvyxsYCldZ3WxasdSYjbJ9ysG4+v9120Hy6 VoGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vh68S7E9C328s/YTWMsy5uWsUFBalBuc0JnOUE80cUI=; b=gMieGWLKfretzWjH/VBqmUN74mxJ7kIDti8KUDe2z9r7vpig/RdBPXu8Z9QOk0JJoP C6UEWicyJFYLYcsiPe5H040ZWlzZw8W6ykLk8RVvMoO9UXOe7AvTkh02T/eKXAhBP1tm 5BWK3LCBjUXiOI0PBPKOvLyva1414T20TqiavDhG3v6CJD3gFwgniHfdISnhP6/KeISv EmS+bFCJqeTvAKJVREj2BJ7oNiTmjDPY2SAdpNlRpj5L+5Wl4zTfIaWoU1o7IanJ5rJk yXyh+/KNEbsq1LGT1oaKkFAbki7u135XRZoiva5FWwGTUs+qTw5j8ahoAutM6MTtnSPV D6QA== X-Gm-Message-State: AGRZ1gLL6sj3jSgzKi4entyB8zR3Ln6D9Ge5Tvjx8WTnVPEdcyE+jkRm qM30pPA1lQNIO4fHXwBjRaZRVr0m35w= X-Google-Smtp-Source: AJdET5fWHgWqDODD5i27R15jI/67rwlE0PdP8ZtIQ4rgGWRrTTP04xxkpo+cSdS17666reASfZNRoA== X-Received: by 2002:ac8:3fc9:: with SMTP id v9mr21064487qtk.108.1541426622934; Mon, 05 Nov 2018 06:03:42 -0800 (PST) From: Vitaly Mayatskikh To: qemu-devel@nongnu.org Date: Mon, 5 Nov 2018 14:03:27 +0000 Message-Id: <20181105140327.8363-2-v.mayatskih@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181105140327.8363-1-v.mayatskih@gmail.com> References: <20181105140327.8363-1-v.mayatskih@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::744 Subject: [Qemu-devel] [PATCH 1/1 resend] Add vhost-pci-blk driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , Maxim Levitsky , Vitaly Mayatskikh , "Michael S . Tsirkin" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This driver uses the kernel-mode acceleration for virtio-blk and allows to get a near bare metal disk performance inside a VM. Signed-off-by: Vitaly Mayatskikh --- configure | 10 + default-configs/virtio.mak | 1 + hw/block/Makefile.objs | 1 + hw/block/vhost-blk.c | 411 ++++++++++++++++++++++++++++++++++ hw/virtio/virtio-pci.c | 60 +++++ hw/virtio/virtio-pci.h | 19 ++ include/hw/virtio/vhost-blk.h | 43 ++++ 7 files changed, 545 insertions(+) create mode 100644 hw/block/vhost-blk.c create mode 100644 include/hw/virtio/vhost-blk.h diff --git a/configure b/configure index 46ae1e8c76..787bc780da 100755 --- a/configure +++ b/configure @@ -371,6 +371,7 @@ vhost_crypto=3D"no" vhost_scsi=3D"no" vhost_vsock=3D"no" vhost_user=3D"" +vhost_blk=3D"" kvm=3D"no" hax=3D"no" hvf=3D"no" @@ -869,6 +870,7 @@ Linux) vhost_crypto=3D"yes" vhost_scsi=3D"yes" vhost_vsock=3D"yes" + vhost_blk=3D"yes" QEMU_INCLUDES=3D"-I\$(SRC_PATH)/linux-headers -I$(pwd)/linux-headers $QE= MU_INCLUDES" supported_os=3D"yes" libudev=3D"yes" @@ -1263,6 +1265,10 @@ for opt do ;; --enable-vhost-vsock) vhost_vsock=3D"yes" ;; + --disable-vhost-blk) vhost_blk=3D"no" + ;; + --enable-vhost-blk) vhost_blk=3D"yes" + ;; --disable-opengl) opengl=3D"no" ;; --enable-opengl) opengl=3D"yes" @@ -6000,6 +6006,7 @@ echo "vhost-crypto support $vhost_crypto" echo "vhost-scsi support $vhost_scsi" echo "vhost-vsock support $vhost_vsock" echo "vhost-user support $vhost_user" +echo "vhost-blk support $vhost_blk" echo "Trace backends $trace_backends" if have_backend "simple"; then echo "Trace output file $trace_file-" @@ -6461,6 +6468,9 @@ fi if test "$vhost_user" =3D "yes" ; then echo "CONFIG_VHOST_USER=3Dy" >> $config_host_mak fi +if test "$vhost_blk" =3D "yes" ; then + echo "CONFIG_VHOST_BLK=3Dy" >> $config_host_mak +fi if test "$blobs" =3D "yes" ; then echo "INSTALL_BLOBS=3Dyes" >> $config_host_mak fi diff --git a/default-configs/virtio.mak b/default-configs/virtio.mak index 1304849018..765c0a2a04 100644 --- a/default-configs/virtio.mak +++ b/default-configs/virtio.mak @@ -1,5 +1,6 @@ CONFIG_VHOST_USER_SCSI=3D$(call land,$(CONFIG_VHOST_USER),$(CONFIG_LINUX)) CONFIG_VHOST_USER_BLK=3D$(call land,$(CONFIG_VHOST_USER),$(CONFIG_LINUX)) +CONFIG_VHOST_BLK=3D$(CONFIG_LINUX) CONFIG_VIRTIO=3Dy CONFIG_VIRTIO_9P=3Dy CONFIG_VIRTIO_BALLOON=3Dy diff --git a/hw/block/Makefile.objs b/hw/block/Makefile.objs index 53ce5751ae..857ce823fc 100644 --- a/hw/block/Makefile.objs +++ b/hw/block/Makefile.objs @@ -14,3 +14,4 @@ obj-$(CONFIG_SH4) +=3D tc58128.o obj-$(CONFIG_VIRTIO_BLK) +=3D virtio-blk.o obj-$(CONFIG_VIRTIO_BLK) +=3D dataplane/ obj-$(CONFIG_VHOST_USER_BLK) +=3D vhost-user-blk.o +obj-$(CONFIG_VHOST_BLK) +=3D vhost-blk.o diff --git a/hw/block/vhost-blk.c b/hw/block/vhost-blk.c new file mode 100644 index 0000000000..6d30b3c8fa --- /dev/null +++ b/hw/block/vhost-blk.c @@ -0,0 +1,411 @@ +/* + * vhost-blk host device + * + * Copyright(C) 2018 IBM Corporation + * + * Authors: + * Vitaly Mayatskikh + * + * Largely based on the "vhost-user-blk.c" implemented by: + * Changpeng Liu + * + * This work is licensed under the terms of the GNU LGPL, version 2 or lat= er. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/cutils.h" +#include "qom/object.h" +#include "hw/qdev-core.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/vhost-blk.h" +#include "hw/virtio/virtio.h" +#include "hw/virtio/virtio-bus.h" +#include "hw/virtio/virtio-access.h" +#include +#include + +static const int feature_bits[] =3D { + VIRTIO_BLK_F_SIZE_MAX, + VIRTIO_BLK_F_SEG_MAX, + VIRTIO_BLK_F_BLK_SIZE, + VIRTIO_BLK_F_TOPOLOGY, + VIRTIO_BLK_F_MQ, + VIRTIO_BLK_F_RO, + VIRTIO_BLK_F_FLUSH, + VIRTIO_BLK_F_CONFIG_WCE, + VIRTIO_F_VERSION_1, + VIRTIO_RING_F_INDIRECT_DESC, + VIRTIO_RING_F_EVENT_IDX, + VIRTIO_F_NOTIFY_ON_EMPTY, + VHOST_INVALID_FEATURE_BIT +}; + +static void vhost_blk_get_config(VirtIODevice *vdev, uint8_t *config) +{ + VHostBlk *s =3D VHOST_BLK(vdev); + memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config)); +} + +static void vhost_blk_set_config(VirtIODevice *vdev, const uint8_t *config) +{ + VHostBlk *s =3D VHOST_BLK(vdev); + struct virtio_blk_config *blkcfg =3D (struct virtio_blk_config *)confi= g; + int ret; + + if (blkcfg->wce =3D=3D s->blkcfg.wce) { + return; + } + + ret =3D vhost_dev_set_config(&s->dev, &blkcfg->wce, + offsetof(struct virtio_blk_config, wce), + sizeof(blkcfg->wce), + VHOST_SET_CONFIG_TYPE_MASTER); + if (ret) { + error_report("set device config space failed"); + return; + } + + s->blkcfg.wce =3D blkcfg->wce; +} + +static int vhost_blk_handle_config_change(struct vhost_dev *dev) +{ + int ret; + struct virtio_blk_config blkcfg; + VHostBlk *s =3D VHOST_BLK(dev->vdev); + + ret =3D vhost_dev_get_config(dev, (uint8_t *)&blkcfg, + sizeof(struct virtio_blk_config)); + if (ret < 0) { + error_report("get config space failed"); + return -1; + } + + /* valid for resize only */ + if (blkcfg.capacity !=3D s->blkcfg.capacity) { + s->blkcfg.capacity =3D blkcfg.capacity; + memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_con= fig)); + virtio_notify_config(dev->vdev); + } + + return 0; +} + +const VhostDevConfigOps vhost_blk_ops =3D { + .vhost_dev_config_notifier =3D vhost_blk_handle_config_change, +}; + +static void vhost_blk_start(VirtIODevice *vdev) +{ + VHostBlk *s =3D VHOST_BLK(vdev); + BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + int i, ret; + + if (!k->set_guest_notifiers) { + error_report("binding does not support guest notifiers"); + return; + } + + ret =3D vhost_dev_enable_notifiers(&s->dev, vdev); + if (ret < 0) { + error_report("Error enabling host notifiers: %d", -ret); + return; + } + + ret =3D k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); + if (ret < 0) { + error_report("Error binding guest notifier: %d", -ret); + goto err_host_notifiers; + } + + s->dev.acked_features =3D vdev->guest_features; + printf("hdev %p vqs %d\n", &s->dev, s->dev.nvqs); + ret =3D vhost_dev_start(&s->dev, vdev); + if (ret < 0) { + error_report("Error starting vhost: %d", -ret); + goto err_guest_notifiers; + } + /* guest_notifier_mask/pending not used yet, so just unmask + * everything here. virtio-pci will do the right thing by + * enabling/disabling irqfd. + */ + for (i =3D 0; i < s->dev.nvqs; i++) { + vhost_virtqueue_mask(&s->dev, vdev, i, false); + } + + s->bs_fd =3D open(blk_bs(s->blk)->filename, O_RDWR); + if (s->bs_fd < 0) { + error_report("Error opening backing store: %d\n", -errno); + goto err_ioctl; + } + ret =3D ioctl(s->vhostfd, _IOW(0xaf, 0x50, int), &s->bs_fd); + if (ret < 0) { + error_report("Error setting up backend: %d", -errno); + goto err_ioctl; + } + + return; + +err_ioctl: + if (s->bs_fd > 0) + close(s->bs_fd); + vhost_dev_stop(&s->dev, vdev); +err_guest_notifiers: + k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); +err_host_notifiers: + vhost_dev_disable_notifiers(&s->dev, vdev); +} + +static void vhost_blk_stop(VirtIODevice *vdev) +{ + VHostBlk *s =3D VHOST_BLK(vdev); + BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); + VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); + int ret; + + if (!k->set_guest_notifiers) { + return; + } + + vhost_dev_stop(&s->dev, vdev); + + ret =3D k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); + if (ret < 0) { + error_report("vhost guest notifier cleanup failed: %d", ret); + return; + } + + vhost_dev_disable_notifiers(&s->dev, vdev); + close(s->bs_fd); +} + +static void vhost_blk_set_status(VirtIODevice *vdev, uint8_t status) +{ + VHostBlk *s =3D VHOST_BLK(vdev); + bool should_start =3D status & VIRTIO_CONFIG_S_DRIVER_OK; + + if (!vdev->vm_running) { + should_start =3D false; + } + + if (s->dev.started =3D=3D should_start) { + return; + } + + if (should_start) { + vhost_blk_start(vdev); + } else { + vhost_blk_stop(vdev); + } + +} + +static uint64_t vhost_blk_get_features(VirtIODevice *vdev, + uint64_t features, + Error **errp) +{ + VHostBlk *s =3D VHOST_BLK(vdev); + + /* Turn on pre-defined features */ + virtio_add_feature(&features, VIRTIO_BLK_F_SIZE_MAX); + virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); + virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); + virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); + virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH); + virtio_add_feature(&features, VIRTIO_BLK_F_RO); + + if (s->config_wce) { + virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE); + } + if (s->num_queues > 1) { + virtio_add_feature(&features, VIRTIO_BLK_F_MQ); + } + + return vhost_get_features(&s->dev, feature_bits, features); +} + +static void vhost_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) +{ +} + +static int vhost_blk_cfg_init(VHostBlk *s) +{ + int ret; + uint64_t var64; + int var; + int fd =3D - 1; + + fd =3D open(blk_bs(s->blk)->filename, O_RDWR); + if (fd < 0) { + error_report("Can't open device %s: %d\n", blk_bs(s->blk)->filename, = errno); + goto out; + } + ret =3D ioctl(fd, BLKGETSIZE64, &var64); + if (ret !=3D 0 && (errno =3D=3D ENOTTY)) { + ret =3D ioctl(fd, BLKGETSIZE, &var); + var64 =3D var; + } + if (ret !=3D 0) { + error_report("Can't get drive size: %d\n", errno); + goto out; + } + s->blkcfg.capacity =3D var64 / 512; + + ret =3D ioctl(fd, BLKSSZGET, &var); + if (ret !=3D 0) { + error_report("Can't get drive logical sector size, assuming 512: %d\n= ", errno); + var =3D 512; + } + s->blkcfg.blk_size =3D var; + s->blkcfg.physical_block_exp =3D 0; + s->blkcfg.num_queues =3D s->num_queues; + /* TODO query actual block device */ + s->blkcfg.size_max =3D 8192; + s->blkcfg.seg_max =3D 8192 / 512; + s->blkcfg.min_io_size =3D 512; + s->blkcfg.opt_io_size =3D 8192; + +out: + if (fd > 0) + close(fd); + return ret; +} + +static void vhost_blk_device_realize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostBlk *s =3D VHOST_BLK(vdev); + int i, ret; + + if (!s->blk) { + error_setg(errp, "drive property not set"); + return; + } + if (!blk_is_inserted(s->blk)) { + error_setg(errp, "Device needs media, but drive is empty"); + return; + } + + if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) { + error_setg(errp, "vhost-blk: invalid number of IO queues"); + return; + } + + if (!s->queue_size) { + error_setg(errp, "vhost-blk: queue size must be non-zero"); + return; + } + + virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, + sizeof(struct virtio_blk_config)); + + s->dev.max_queues =3D s->num_queues; + s->dev.nvqs =3D s->num_queues; + s->dev.vqs =3D g_new(struct vhost_virtqueue, s->dev.nvqs); + s->dev.vq_index =3D 0; + s->dev.backend_features =3D 0; + + vhost_dev_set_config_notifier(&s->dev, &vhost_blk_ops); + + for (i =3D 0; i < s->dev.max_queues; i++) { + virtio_add_queue(vdev, s->queue_size, + vhost_blk_handle_output); + } + + s->vhostfd =3D open("/dev/vhost-blk", O_RDWR); + if (s->vhostfd < 0) { + error_setg_errno(errp, -errno, + "vhost-blk: failed to open vhost device"); + goto virtio_err; + } + + ret =3D vhost_dev_init(&s->dev, (void *)(uintptr_t)s->vhostfd, VHOST_B= ACKEND_TYPE_KERNEL, 0); + if (ret < 0) { + error_setg(errp, "vhost-blk: vhost initialization failed: %s", + strerror(-ret)); + goto virtio_err; + } + + vhost_blk_cfg_init(s); + blk_iostatus_enable(s->blk); + return; + +virtio_err: + g_free(s->dev.vqs); + virtio_cleanup(vdev); + close(s->vhostfd); +} + +static void vhost_blk_device_unrealize(DeviceState *dev, Error **errp) +{ + VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VHostBlk *s =3D VHOST_BLK(dev); + + vhost_blk_set_status(vdev, 0); + close(s->vhostfd); + vhost_dev_cleanup(&s->dev); + g_free(s->dev.vqs); + virtio_cleanup(vdev); +} + +static void vhost_blk_instance_init(Object *obj) +{ + VHostBlk *s =3D VHOST_BLK(obj); + + device_add_bootindex_property(obj, &s->bootindex, "bootindex", + "/disk@0,0", DEVICE(obj), NULL); +} + +static const VMStateDescription vmstate_vhost_blk =3D { + .name =3D "vhost-blk", + .minimum_version_id =3D 1, + .version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_VIRTIO_DEVICE, + VMSTATE_END_OF_LIST() + }, +}; + +static Property vhost_blk_properties[] =3D { + DEFINE_PROP_DRIVE("drive", VHostBlk, blk), + DEFINE_PROP_UINT16("num-queues", VHostBlk, num_queues, 1), + DEFINE_PROP_UINT32("queue-size", VHostBlk, queue_size, 128), + DEFINE_PROP_BIT("config-wce", VHostBlk, config_wce, 0, true), + DEFINE_PROP_END_OF_LIST(), +}; + +static void vhost_blk_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioDeviceClass *vdc =3D VIRTIO_DEVICE_CLASS(klass); + + dc->props =3D vhost_blk_properties; + dc->vmsd =3D &vmstate_vhost_blk; + set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); + vdc->realize =3D vhost_blk_device_realize; + vdc->unrealize =3D vhost_blk_device_unrealize; + vdc->get_config =3D vhost_blk_get_config; + vdc->set_config =3D vhost_blk_set_config; + vdc->get_features =3D vhost_blk_get_features; + vdc->set_status =3D vhost_blk_set_status; +} + +static const TypeInfo vhost_blk_info =3D { + .name =3D TYPE_VHOST_BLK, + .parent =3D TYPE_VIRTIO_DEVICE, + .instance_size =3D sizeof(VHostBlk), + .instance_init =3D vhost_blk_instance_init, + .class_init =3D vhost_blk_class_init, +}; + +static void virtio_register_types(void) +{ + type_register_static(&vhost_blk_info); +} + +type_init(virtio_register_types) diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index a954799267..ec00b54424 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -2060,6 +2060,63 @@ static const TypeInfo vhost_user_blk_pci_info =3D { }; #endif =20 +#ifdef CONFIG_VHOST_BLK +/* vhost-blk */ + +static Property vhost_blk_pci_properties[] =3D { + DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), + DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, + DEV_NVECTORS_UNSPECIFIED), + DEFINE_PROP_END_OF_LIST(), +}; + +static void vhost_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) +{ + VHostBlkPCI *dev =3D VHOST_BLK_PCI(vpci_dev); + DeviceState *vdev =3D DEVICE(&dev->vdev); + + if (vpci_dev->nvectors =3D=3D DEV_NVECTORS_UNSPECIFIED) { + vpci_dev->nvectors =3D dev->vdev.num_queues + 1; + } + + qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus)); + object_property_set_bool(OBJECT(vdev), true, "realized", errp); +} + +static void vhost_blk_pci_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + VirtioPCIClass *k =3D VIRTIO_PCI_CLASS(klass); + PCIDeviceClass *pcidev_k =3D PCI_DEVICE_CLASS(klass); + + set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); + dc->props =3D vhost_blk_pci_properties; + k->realize =3D vhost_blk_pci_realize; + pcidev_k->vendor_id =3D PCI_VENDOR_ID_REDHAT_QUMRANET; + pcidev_k->device_id =3D PCI_DEVICE_ID_VIRTIO_BLOCK; + pcidev_k->revision =3D VIRTIO_PCI_ABI_VERSION; + pcidev_k->class_id =3D PCI_CLASS_STORAGE_SCSI; +} + +static void vhost_blk_pci_instance_init(Object *obj) +{ + VHostBlkPCI *dev =3D VHOST_BLK_PCI(obj); + + virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), + TYPE_VHOST_BLK); + object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), + "bootindex", &error_abort); +} + +static const TypeInfo vhost_blk_pci_info =3D { + .name =3D TYPE_VHOST_BLK_PCI, + .parent =3D TYPE_VIRTIO_PCI, + .instance_size =3D sizeof(VHostBlkPCI), + .instance_init =3D vhost_blk_pci_instance_init, + .class_init =3D vhost_blk_pci_class_init, +}; +#endif + /* virtio-scsi-pci */ =20 static Property virtio_scsi_pci_properties[] =3D { @@ -2723,6 +2780,9 @@ static void virtio_pci_register_types(void) #ifdef CONFIG_VHOST_VSOCK type_register_static(&vhost_vsock_pci_info); #endif +#ifdef CONFIG_VHOST_BLK + type_register_static(&vhost_blk_pci_info); +#endif } =20 type_init(virtio_pci_register_types) diff --git a/hw/virtio/virtio-pci.h b/hw/virtio/virtio-pci.h index 813082b0d7..4cca04a45a 100644 --- a/hw/virtio/virtio-pci.h +++ b/hw/virtio/virtio-pci.h @@ -31,6 +31,10 @@ #include "hw/virtio/vhost-user-blk.h" #endif =20 +#ifdef CONFIG_VHOST_BLK +#include "hw/virtio/vhost-blk.h" +#endif + #ifdef CONFIG_VIRTFS #include "hw/9pfs/virtio-9p.h" #endif @@ -50,6 +54,7 @@ typedef struct VirtIONetPCI VirtIONetPCI; typedef struct VHostSCSIPCI VHostSCSIPCI; typedef struct VHostUserSCSIPCI VHostUserSCSIPCI; typedef struct VHostUserBlkPCI VHostUserBlkPCI; +typedef struct VHostBlkPCI VHostBlkPCI; typedef struct VirtIORngPCI VirtIORngPCI; typedef struct VirtIOInputPCI VirtIOInputPCI; typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI; @@ -262,6 +267,20 @@ struct VHostUserBlkPCI { }; #endif =20 +#ifdef CONFIG_VHOST_BLK +/* + * vhost-blk-pci: This extends VirtioPCIProxy. + */ +#define TYPE_VHOST_BLK_PCI "vhost-blk-pci" +#define VHOST_BLK_PCI(obj) \ + OBJECT_CHECK(VHostBlkPCI, (obj), TYPE_VHOST_BLK_PCI) + +struct VHostBlkPCI { + VirtIOPCIProxy parent_obj; + VHostBlk vdev; +}; +#endif + /* * virtio-blk-pci: This extends VirtioPCIProxy. */ diff --git a/include/hw/virtio/vhost-blk.h b/include/hw/virtio/vhost-blk.h new file mode 100644 index 0000000000..6dc5d9634a --- /dev/null +++ b/include/hw/virtio/vhost-blk.h @@ -0,0 +1,43 @@ +/* + * vhost-blk host device + * Copyright(C) 2018 IBM Corporation. + * + * Authors: + * Vitaly Mayatskikh + * + * Based on vhost-user-blk.h, Copyright Intel, Corp. 2017 + * + * This work is licensed under the terms of the GNU LGPL, version 2 or lat= er. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#ifndef VHOST_BLK_H +#define VHOST_BLK_H + +#include "standard-headers/linux/virtio_blk.h" +#include "qemu-common.h" +#include "hw/qdev.h" +#include "hw/block/block.h" +#include "hw/virtio/vhost.h" +#include "sysemu/block-backend.h" + +#define TYPE_VHOST_BLK "vhost-blk" +#define VHOST_BLK(obj) \ + OBJECT_CHECK(VHostBlk, (obj), TYPE_VHOST_BLK) + +typedef struct VHostBlk { + VirtIODevice parent_obj; + BlockConf conf; + BlockBackend *blk; + int32_t bootindex; + struct virtio_blk_config blkcfg; + uint16_t num_queues; + uint32_t queue_size; + uint32_t config_wce; + int vhostfd; + int bs_fd; + struct vhost_dev dev; +} VHostBlk; + +#endif --=20 2.17.1