Skip to contentSkip to conversation

block: Add VFIO based driver for NVMe device

PATCH In review Fam Zheng 3 participants 12 messages Jan 16, 2018 – Feb 13, 2018 Block layer

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

v6 cover block: Add VFIO based driver for NVMe device

v6: Fix the cleaning up around nvme_init() and write cache setup in

    nvme_file_open(). [Stefan]
    Add Stefan's rev-by to the doc patch.
    Rebase on to Paolo's coroutine series for q->free_req_queue:

Based-on: [email protected] ([PATCH 0/4] coroutine-lock: polymorphic CoQueue)

v5: Add Stefan's rev-bys.
Add Paolo's sob line.
Address Stefan's, Alex's and Eric's comments, including:
- Add PCI Bar MMIO offset/size parameter to VFIO utility API. [Alex]
- Fix various lock and memory leakings. [Stefan]
- Improve documentation. [Stefan]
- Add one missing "Since 2.12" in QAPI schema. [Eric]
- Fix locking of BDRVNVMeState->inflight. [Stefan]
- Add one missing endianness conversion for cmd.nsid. [Stefan]
- Use bdrv_get_aio_context() instead of qemu_get_current_aio_context().
- When init, issue "Set Features" command to enable/disable volatile write cache according to "flags & BDRV_O_NOCACHE".

v4: - Rebase, address Stefan's comments.
- Add QAPI and doc patches.

v3: Rebase, small tweaks/fixes and add locks to provide basic thread safety
(basic because it is not really tested).

v2:
- Implement "split vfio addr space" appraoch. [Paolo]
- Add back 'device reset' in nvme_close(). [Paolo]
- Better variable namings. [Stefan]
- "Reuse" macro definitions from NVMe emulation code.
- Rebase onto current master which has polling by default and update performance results accordingly.
- Update MAINTAINERS.
- Specify namespace in URI.
- The sporadical I/O error from v1 "disappeared" in this version.
- Tests one: qemu-img bench, fio, bonnie++ and installation of ubuntu/fedora/rhel on QEMU emulated nvme and a Intel P3700 card.

Fam Zheng (9):
stubs: Add stubs for ram block API
util: Introduce vfio helpers
block: Add VFIO based NVMe driver
block: Introduce buf register API
block/nvme: Implement .bdrv_(un)register_buf
qemu-img: Map bench buffer
block: Move NVMe constants to a separate header
docs: Add section for NVMe VFIO driver
qapi: Add NVMe driver options to the schema

 MAINTAINERS                    |    6 +
 block/Makefile.objs            |    1 +
 block/block-backend.c          |   10 +
 block/io.c                     |   24 +
 block/nvme.c                   | 1199 ++++++++++++++++++++++++++++++++++++++++
 block/trace-events             |   21 +
 docs/qemu-block-drivers.texi   |   37 ++
 hw/block/nvme.h                |  698 +----------------------
 include/block/block.h          |   11 +-
 include/block/block_int.h      |    9 +
 include/block/nvme.h           |  700 +++++++++++++++++++++++
 include/qemu/vfio-helpers.h    |   33 ++
 include/sysemu/block-backend.h |    3 +
 qapi/block-core.json           |   17 +-
 qemu-img.c                     |    9 +-
 stubs/Makefile.objs            |    1 +
 stubs/ram-block.c              |   16 +
 util/Makefile.objs             |    1 +
 util/trace-events              |   11 +
 util/vfio-helpers.c            |  726 ++++++++++++++++++++++++
 20 files changed, 2833 insertions(+), 700 deletions(-)
 create mode 100644 block/nvme.c
 create mode 100644 include/block/nvme.h
 create mode 100644 include/qemu/vfio-helpers.h
 create mode 100644 stubs/ram-block.c
 create mode 100644 util/vfio-helpers.c
Signature · 1 line
2.14.3
author
#1 lore raw

PATCH v6 1/9 stubs: Add stubs for ram block API

These functions will be wanted by block-obj-y but the actual definition is in obj-y, so stub them to keep the linker happy.

Message-Id: <[email protected]>

---
 stubs/Makefile.objs |  1 +
 stubs/ram-block.c   | 16 ++++++++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 stubs/ram-block.c
stubs/Makefile.objs +1 -0
index 8cfe34328a..2d59d84091 100644
@@ -42,3 +42,4 @@ stub-obj-y += vmgenid.o
 stub-obj-y += xen-common.o
 stub-obj-y += xen-hvm.o
 stub-obj-y += pci-host-piix.o
+stub-obj-y += ram-block.o
stubs/ram-block.c new file +16 -0
new file mode 100644
index 0000000000..cfa5d8678f
@@ -0,0 +1,16 @@
+#include "qemu/osdep.h"
+#include "exec/ramlist.h"
+#include "exec/cpu-common.h"
+
+void ram_block_notifier_add(RAMBlockNotifier *n)
+{
+}
+
+void ram_block_notifier_remove(RAMBlockNotifier *n)
+{
+}
+
+int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
+{
+    return 0;
+}
Signature · 1 line
2.14.3
author
#2 lore raw

PATCH v6 2/9 util: Introduce vfio helpers

In reply to Fam Zheng

This is a library to manage the host vfio interface, which could be used to implement userspace device driver code in QEMU such as NVMe or net controllers.

Message-Id: <[email protected]>

---
 include/qemu/vfio-helpers.h |  33 ++
 util/Makefile.objs          |   1 +
 util/trace-events           |  11 +
 util/vfio-helpers.c         | 726 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 771 insertions(+)
 create mode 100644 include/qemu/vfio-helpers.h
 create mode 100644 util/vfio-helpers.c
include/qemu/vfio-helpers.h new file +33 -0
new file mode 100644
index 0000000000..ce7e7b057f
@@ -0,0 +1,33 @@
+/*
+ * QEMU VFIO helpers
+ *
+ * Copyright 2016 - 2018 Red Hat, Inc.
+ *
+ * Authors:
+ *   Fam Zheng <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef QEMU_VFIO_HELPERS_H
+#define QEMU_VFIO_HELPERS_H
+#include "qemu/typedefs.h"
+
+typedef struct QEMUVFIOState QEMUVFIOState;
+
+QEMUVFIOState *qemu_vfio_open_pci(const char *device, Error **errp);
+void qemu_vfio_close(QEMUVFIOState *s);
+int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size,
+                      bool temporary, uint64_t *iova_list);
+int qemu_vfio_dma_reset_temporary(QEMUVFIOState *s);
+void qemu_vfio_dma_unmap(QEMUVFIOState *s, void *host);
+void *qemu_vfio_pci_map_bar(QEMUVFIOState *s, int index,
+                            uint64_t offset, uint64_t size,
+                            Error **errp);
+void qemu_vfio_pci_unmap_bar(QEMUVFIOState *s, int index, void *bar,
+                             uint64_t offset, uint64_t size);
+int qemu_vfio_pci_init_irq(QEMUVFIOState *s, EventNotifier *e,
+                           int irq_type, Error **errp);
+
+#endif
util/Makefile.objs +1 -0
index 2973b0a323..3fb611631f 100644
@@ -46,3 +46,4 @@ util-obj-y += qht.o
 util-obj-y += range.o
 util-obj-y += stats64.o
 util-obj-y += systemd.o
+util-obj-$(CONFIG_LINUX) += vfio-helpers.o
util/trace-events +11 -0
index 025499f83f..2f57bf2337 100644
@@ -59,3 +59,14 @@ lockcnt_futex_wake(const void *lockcnt) "lockcnt %p waking up one waiter"
 # util/qemu-thread-posix.c
 qemu_mutex_locked(void *lock) "locked mutex %p"
 qemu_mutex_unlocked(void *lock) "unlocked mutex %p"
+
+# util/vfio-helpers.c
+qemu_vfio_dma_reset_temporary(void *s) "s %p"
+qemu_vfio_ram_block_added(void *s, void *p, size_t size) "s %p host %p size 0x%zx"
+qemu_vfio_ram_block_removed(void *s, void *p, size_t size) "s %p host %p size 0x%zx"
+qemu_vfio_find_mapping(void *s, void *p) "s %p host %p"
+qemu_vfio_new_mapping(void *s, void *host, size_t size, int index, uint64_t iova) "s %p host %p size %zu index %d iova 0x%"PRIx64
+qemu_vfio_do_mapping(void *s, void *host, size_t size, uint64_t iova) "s %p host %p size %zu iova 0x%"PRIx64
+qemu_vfio_dma_map(void *s, void *host, size_t size, bool temporary, uint64_t *iova) "s %p host %p size %zu temporary %d iova %p"
+qemu_vfio_dma_map_invalid(void *s, void *mapping_host, size_t mapping_size, void *host, size_t size) "s %p mapping %p %zu requested %p %zu"
+qemu_vfio_dma_unmap(void *s, void *host) "s %p host %p"
util/vfio-helpers.c new file +726 -0

726 lines not on this page · raw message

Signature · 1 line
2.14.3
author
#5 lore raw

PATCH v6 4/9 block: Introduce buf register API

In reply to Fam Zheng

Allow block driver to map and unmap a buffer for later I/O, as a performance hint.

Message-Id: <[email protected]>

---
 block/block-backend.c          | 10 ++++++++++
 block/io.c                     | 24 ++++++++++++++++++++++++
 include/block/block.h          | 11 ++++++++++-
 include/block/block_int.h      |  9 +++++++++
 include/sysemu/block-backend.h |  3 +++
 5 files changed, 56 insertions(+), 1 deletion(-)
block/block-backend.c +10 -0
index baef8e7abc..f66349c2c9 100644
@@ -2096,3 +2096,13 @@ static void blk_root_drained_end(BdrvChild *child)
         }
     }
 }
+
+void blk_register_buf(BlockBackend *blk, void *host, size_t size)
+{
+    bdrv_register_buf(blk_bs(blk), host, size);
+}
+
+void blk_unregister_buf(BlockBackend *blk, void *host)
+{
+    bdrv_unregister_buf(blk_bs(blk), host);
+}
block/io.c +24 -0
index 7ea402352e..89d0745e95 100644
@@ -2825,3 +2825,27 @@ void bdrv_io_unplug(BlockDriverState *bs)
         bdrv_io_unplug(child->bs);
     }
 }
+
+void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size)
+{
+    BdrvChild *child;
+
+    if (bs->drv && bs->drv->bdrv_register_buf) {
+        bs->drv->bdrv_register_buf(bs, host, size);
+    }
+    QLIST_FOREACH(child, &bs->children, next) {
+        bdrv_register_buf(child->bs, host, size);
+    }
+}
+
+void bdrv_unregister_buf(BlockDriverState *bs, void *host)
+{
+    BdrvChild *child;
+
+    if (bs->drv && bs->drv->bdrv_unregister_buf) {
+        bs->drv->bdrv_unregister_buf(bs, host);
+    }
+    QLIST_FOREACH(child, &bs->children, next) {
+        bdrv_unregister_buf(child->bs, host);
+    }
+}
include/block/block.h +10 -1
index 9b12774ddf..2025d7ed19 100644
@@ -631,5 +631,14 @@ void bdrv_del_child(BlockDriverState *parent, BdrvChild *child, Error **errp);
 
 bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
                                      uint32_t granularity, Error **errp);
-
+/**
+ *
+ * bdrv_register_buf/bdrv_unregister_buf:
+ *
+ * Register/unregister a buffer for I/O. For example, VFIO drivers are
+ * interested to know the memory areas that would later be used for I/O, so
+ * that they can prepare IOMMU mapping etc., to get better performance.
+ */
+void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size);
+void bdrv_unregister_buf(BlockDriverState *bs, void *host);
 #endif
include/block/block_int.h +9 -0
index 29cafa4236..99b9190627 100644
@@ -446,6 +446,15 @@ struct BlockDriver {
                                                 const char *name,
                                                 Error **errp);
 
+    /**
+     * Register/unregister a buffer for I/O. For example, when the driver is
+     * interested to know the memory areas that will later be used in iovs, so
+     * that it can do IOMMU mapping with VFIO etc., in order to get better
+     * performance. In the case of VFIO drivers, this callback is used to do
+     * DMA mapping for hot buffers.
+     */
+    void (*bdrv_register_buf)(BlockDriverState *bs, void *host, size_t size);
+    void (*bdrv_unregister_buf)(BlockDriverState *bs, void *host);
     QLIST_ENTRY(BlockDriver) list;
 };
 
include/sysemu/block-backend.h +3 -0
index c4e52a5fa3..92ab624fac 100644
@@ -229,4 +229,7 @@ void blk_io_limits_enable(BlockBackend *blk, const char *group);
 void blk_io_limits_update_group(BlockBackend *blk, const char *group);
 void blk_set_force_allow_inactivate(BlockBackend *blk);
 
+void blk_register_buf(BlockBackend *blk, void *host, size_t size);
+void blk_unregister_buf(BlockBackend *blk, void *host);
+
 #endif
Signature · 1 line
2.14.3
author
#3 lore raw

PATCH v6 3/9 block: Add VFIO based NVMe driver

In reply to Fam Zheng

This is a new protocol driver that exclusively opens a host NVMe controller through VFIO. It achieves better latency than linux-aio by completely bypassing host kernel vfs/block layer.

    $rw-$bs-$iodepth  linux-aio     nvme://
    ----------------------------------------
    randread-4k-1     10.5k         21.6k
    randread-512k-1   745           1591
    randwrite-4k-1    30.7k         37.0k
    randwrite-512k-1  1945          1980

    (unit: IOPS)

The driver also integrates with the polling mechanism of iothread.

This patch is co-authored by Paolo and me.

Message-Id: <[email protected]>

---
 MAINTAINERS         |    6 +
 block/Makefile.objs |    1 +
 block/nvme.c        | 1180 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/trace-events  |   21 +
 4 files changed, 1208 insertions(+)
 create mode 100644 block/nvme.c
MAINTAINERS +6 -0
index 4770f105d4..bd636a4bff 100644
@@ -1876,6 +1876,12 @@ L: [email protected]
 S: Supported
 F: block/null.c
 
+NVMe Block Driver
+M: Fam Zheng <[email protected]>
+L: [email protected]
+S: Supported
+F: block/nvme*
+
 Bootdevice
 M: Gonglei <[email protected]>
 S: Maintained
block/Makefile.objs +1 -0
index 6eaf78a046..4c7e9d84a7 100644
@@ -11,6 +11,7 @@ block-obj-$(CONFIG_POSIX) += file-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 block-obj-y += null.o mirror.o commit.o io.o
 block-obj-y += throttle-groups.o
+block-obj-$(CONFIG_LINUX) += nvme.o
 
 block-obj-y += nbd.o nbd-client.o sheepdog.o
 block-obj-$(CONFIG_LIBISCSI) += iscsi.o
block/nvme.c new file +1180 -0

1,180 lines not on this page · raw message

block/trace-events +21 -0
index 11c8d5f590..02dd80ff0c 100644
@@ -124,3 +124,24 @@ vxhs_open_iio_open(const char *host) "Failed to connect to storage agent on host
 vxhs_parse_uri_hostinfo(char *host, int port) "Host: IP %s, Port %d"
 vxhs_close(char *vdisk_guid) "Closing vdisk %s"
 vxhs_get_creds(const char *cacert, const char *client_key, const char *client_cert) "cacert %s, client_key %s, client_cert %s"
+
+# block/nvme.c
+nvme_kick(void *s, int queue) "s %p queue %d"
+nvme_dma_flush_queue_wait(void *s) "s %p"
+nvme_error(int cmd_specific, int sq_head, int sqid, int cid, int status) "cmd_specific %d sq_head %d sqid %d cid %d status 0x%x"
+nvme_process_completion(void *s, int index, int inflight) "s %p queue %d inflight %d"
+nvme_process_completion_queue_busy(void *s, int index) "s %p queue %d"
+nvme_complete_command(void *s, int index, int cid) "s %p queue %d cid %d"
+nvme_submit_command(void *s, int index, int cid) "s %p queue %d cid %d"
+nvme_submit_command_raw(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7) "%02x %02x %02x %02x %02x %02x %02x %02x"
+nvme_handle_event(void *s) "s %p"
+nvme_poll_cb(void *s) "s %p"
+nvme_prw_aligned(void *s, int is_write, uint64_t offset, uint64_t bytes, int flags, int niov) "s %p is_write %d offset %"PRId64" bytes %"PRId64" flags %d niov %d"
+nvme_qiov_unaligned(const void *qiov, int n, void *base, size_t size, int align) "qiov %p n %d base %p size 0x%zx align 0x%x"
+nvme_prw_buffered(void *s, uint64_t offset, uint64_t bytes, int niov, int is_write) "s %p offset %"PRId64" bytes %"PRId64" niov %d is_write %d"
+nvme_rw_done(void *s, int is_write, uint64_t offset, uint64_t bytes, int ret) "s %p is_write %d offset %"PRId64" bytes %"PRId64" ret %d"
+nvme_dma_map_flush(void *s) "s %p"
+nvme_free_req_queue_wait(void *q) "q %p"
+nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) "s %p cmd %p req %p qiov %p entries %d"
+nvme_cmd_map_qiov_pages(void *s, int i, uint64_t page) "s %p page[%d] 0x%"PRIx64
+nvme_cmd_map_qiov_iov(void *s, int i, void *page, int pages) "s %p iov[%d] %p pages %d"
Signature · 1 line
2.14.3
author
#6 lore raw

PATCH v6 5/9 block/nvme: Implement .bdrv_(un)register_buf

In reply to Fam Zheng

Forward these two calls to the IOVA manager.

Message-Id: <[email protected]>

---
 block/nvme.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
block/nvme.c +24 -0
index 99cc7702ad..30616f50c7 100644
@@ -1145,6 +1145,27 @@ static void nvme_aio_unplug(BlockDriverState *bs)
     }
 }
 
+static void nvme_register_buf(BlockDriverState *bs, void *host, size_t size)
+{
+    int ret;
+    BDRVNVMeState *s = bs->opaque;
+
+    ret = qemu_vfio_dma_map(s->vfio, host, size, false, NULL);
+    if (ret) {
+        /* FIXME: we may run out of IOVA addresses after repeated
+         * bdrv_register_buf/bdrv_unregister_buf, because nvme_vfio_dma_unmap
+         * doesn't reclaim addresses for fixed mappings. */
+        error_report("nvme_register_buf failed: %s", strerror(-ret));
+    }
+}
+
+static void nvme_unregister_buf(BlockDriverState *bs, void *host)
+{
+    BDRVNVMeState *s = bs->opaque;
+
+    qemu_vfio_dma_unmap(s->vfio, host);
+}
+
 static BlockDriver bdrv_nvme = {
     .format_name              = "nvme",
     .protocol_name            = "nvme",
@@ -1170,6 +1191,9 @@ static BlockDriver bdrv_nvme = {
 
     .bdrv_io_plug             = nvme_aio_plug,
     .bdrv_io_unplug           = nvme_aio_unplug,
+
+    .bdrv_register_buf        = nvme_register_buf,
+    .bdrv_unregister_buf      = nvme_unregister_buf,
 };
 
 static void bdrv_nvme_init(void)
Signature · 1 line
2.14.3
author
#7 lore raw

PATCH v6 6/9 qemu-img: Map bench buffer

In reply to Fam Zheng

Message-Id: <[email protected]>

---
 qemu-img.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
qemu-img.c +8 -1
index 68b375f998..28d0e4e9f8 100644
@@ -3862,6 +3862,7 @@ static int img_bench(int argc, char **argv)
     struct timeval t1, t2;
     int i;
     bool force_share = false;
+    size_t buf_size;
 
     for (;;) {
         static const struct option long_options[] = {
@@ -4050,9 +4051,12 @@ static int img_bench(int argc, char **argv)
         printf("Sending flush every %d requests\n", flush_interval);
     }
 
-    data.buf = blk_blockalign(blk, data.nrreq * data.bufsize);
+    buf_size = data.nrreq * data.bufsize;
+    data.buf = blk_blockalign(blk, buf_size);
     memset(data.buf, pattern, data.nrreq * data.bufsize);
 
+    blk_register_buf(blk, data.buf, buf_size);
+
     data.qiov = g_new(QEMUIOVector, data.nrreq);
     for (i = 0; i < data.nrreq; i++) {
         qemu_iovec_init(&data.qiov[i], 1);
@@ -4073,6 +4077,9 @@ static int img_bench(int argc, char **argv)
            + ((double)(t2.tv_usec - t1.tv_usec) / 1000000));
 
 out:
+    if (data.buf) {
+        blk_unregister_buf(blk, data.buf);
+    }
     qemu_vfree(data.buf);
     blk_unref(blk);
 
Signature · 1 line
2.14.3
author
#8 lore raw

PATCH v6 7/9 block: Move NVMe constants to a separate header

In reply to Fam Zheng

Message-Id: <[email protected]>

---
 block/nvme.c         |   7 +-
 hw/block/nvme.h      | 698 +-------------------------------------------------
 include/block/nvme.h | 700 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 702 insertions(+), 703 deletions(-)
 create mode 100644 include/block/nvme.h
block/nvme.c +1 -6
index 30616f50c7..044b15e1f5 100644
@@ -22,12 +22,7 @@
 #include "block/block_int.h"
 #include "trace.h"
 
-/* TODO: Move nvme spec definitions from hw/block/nvme.h into a separate file
- * that doesn't depend on dma/pci headers. */
-#include "sysemu/dma.h"
-#include "hw/pci/pci.h"
-#include "hw/block/block.h"
-#include "hw/block/nvme.h"
+#include "block/nvme.h"
 
 #define NVME_SQ_ENTRY_BYTES 64
 #define NVME_CQ_ENTRY_BYTES 16
hw/block/nvme.h +1 -697

704 lines not on this page · raw message

include/block/nvme.h new file +700 -0

700 lines not on this page · raw message

Signature · 1 line
2.14.3
author
#9 lore raw

PATCH v6 8/9 docs: Add section for NVMe VFIO driver

In reply to Fam Zheng

Message-Id: <[email protected]>

---
 docs/qemu-block-drivers.texi | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
docs/qemu-block-drivers.texi +37 -0
index 503c1847aa..cd74767ed3 100644
@@ -785,6 +785,43 @@ warning: ssh server @code{ssh.example.com:22} does not support fsync
 With sufficiently new versions of libssh2 and OpenSSH, @code{fsync} is
 supported.
 
+@node disk_images_nvme
+@subsection NVMe disk images
+
+NVM Express (NVMe) storage controllers can be accessed directly by a userspace
+driver in QEMU.  This bypasses the host kernel file system and block layers
+while retaining QEMU block layer functionalities, such as block jobs, I/O
+throttling, image formats, etc.  Disk I/O performance is typically higher than
+with @code{-drive file=/dev/sda} using either thread pool or linux-aio.
+
+The controller will be exclusively used by the QEMU process once started. To be
+able to share storage between multiple VMs and other applications on the host,
+please use the file based protocols.
+
+Before starting QEMU, bind the host NVMe controller to the host vfio-pci
+driver.  For example:
+
+@example
+# modprobe vfio-pci
+# lspci -n -s 0000:06:0d.0
+06:0d.0 0401: 1102:0002 (rev 08)
+# echo 0000:06:0d.0 > /sys/bus/pci/devices/0000:06:0d.0/driver/unbind
+# echo 1102 0002 > /sys/bus/pci/drivers/vfio-pci/new_id
+
+# qemu-system-x86_64 -drive file=nvme://@var{host}:@var{bus}:@var{slot}.@var{func}/@var{namespace}
+@end example
+
+Alternative syntax using properties:
+
+@example
+qemu-system-x86_64 -drive file.driver=nvme,file.device=@var{host}:@var{bus}:@var{slot}.@var{func},file.namespace=@var{namespace}
+@end example
+
+@var{host}:@var{bus}:@var{slot}.@var{func} is the NVMe controller's PCI device
+address on the host.
+
+@var{namespace} is the NVMe namespace number, starting from 1.
+
 @node disk_image_locking
 @subsection Disk image file locking
 
Signature · 1 line
2.14.3
author
#10 lore raw

PATCH v6 9/9 qapi: Add NVMe driver options to the schema

In reply to Fam Zheng

Message-Id: <[email protected]>

---
 qapi/block-core.json | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
qapi/block-core.json +16 -1
index e94a6881b2..bd16440dc7 100644
@@ -2230,6 +2230,7 @@
 #
 # @vxhs: Since 2.10
 # @throttle: Since 2.11
+# @nvme: Since 2.12
 #
 # Since: 2.9
 ##
@@ -2237,7 +2238,7 @@
   'data': [ 'blkdebug', 'blkverify', 'bochs', 'cloop',
             'dmg', 'file', 'ftp', 'ftps', 'gluster', 'host_cdrom',
             'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs',
-            'null-aio', 'null-co', 'parallels', 'qcow', 'qcow2', 'qed',
+            'null-aio', 'null-co', 'nvme', 'parallels', 'qcow', 'qcow2', 'qed',
             'quorum', 'raw', 'rbd', 'replication', 'sheepdog', 'ssh',
             'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
 
@@ -2278,6 +2279,19 @@
 { 'struct': 'BlockdevOptionsNull',
   'data': { '*size': 'int', '*latency-ns': 'uint64' } }
 
+##
+# @BlockdevOptionsNVMe:
+#
+# Driver specific block device options for the NVMe backend.
+#
+# @device:    controller address of the NVMe device.
+# @namespace: namespace number of the device, starting from 1.
+#
+# Since: 2.12
+##
+{ 'struct': 'BlockdevOptionsNVMe',
+  'data': { 'device': 'str', 'namespace': 'int' } }
+
 ##
 # @BlockdevOptionsVVFAT:
 #
@@ -3183,6 +3197,7 @@
       'nfs':        'BlockdevOptionsNfs',
       'null-aio':   'BlockdevOptionsNull',
       'null-co':    'BlockdevOptionsNull',
+      'nvme':       'BlockdevOptionsNVMe',
       'parallels':  'BlockdevOptionsGenericFormat',
       'qcow2':      'BlockdevOptionsQcow2',
       'qcow':       'BlockdevOptionsQcow',
Signature · 1 line
2.14.3
Reviewed-by on the v6 series
#11 lore raw
In reply to Fam Zheng
Quoted 69 lines · Fam Zheng

v6: Fix the cleaning up around nvme_init() and write cache setup in

    nvme_file_open(). [Stefan]
    Add Stefan's rev-by to the doc patch.
    Rebase on to Paolo's coroutine series for q->free_req_queue:

Based-on: [email protected] ([PATCH 0/4] coroutine-lock: polymorphic CoQueue)

v5: Add Stefan's rev-bys.
Add Paolo's sob line.
Address Stefan's, Alex's and Eric's comments, including:
- Add PCI Bar MMIO offset/size parameter to VFIO utility API. [Alex]
- Fix various lock and memory leakings. [Stefan]
- Improve documentation. [Stefan]
- Add one missing "Since 2.12" in QAPI schema. [Eric]
- Fix locking of BDRVNVMeState->inflight. [Stefan]
- Add one missing endianness conversion for cmd.nsid. [Stefan]
- Use bdrv_get_aio_context() instead of qemu_get_current_aio_context().
- When init, issue "Set Features" command to enable/disable volatile write cache according to "flags & BDRV_O_NOCACHE".

v4: - Rebase, address Stefan's comments.
- Add QAPI and doc patches.

v3: Rebase, small tweaks/fixes and add locks to provide basic thread safety
(basic because it is not really tested).

v2:
- Implement "split vfio addr space" appraoch. [Paolo]
- Add back 'device reset' in nvme_close(). [Paolo]
- Better variable namings. [Stefan]
- "Reuse" macro definitions from NVMe emulation code.
- Rebase onto current master which has polling by default and update performance results accordingly.
- Update MAINTAINERS.
- Specify namespace in URI.
- The sporadical I/O error from v1 "disappeared" in this version.
- Tests one: qemu-img bench, fio, bonnie++ and installation of ubuntu/fedora/rhel on QEMU emulated nvme and a Intel P3700 card.

Fam Zheng (9):
stubs: Add stubs for ram block API
util: Introduce vfio helpers
block: Add VFIO based NVMe driver
block: Introduce buf register API
block/nvme: Implement .bdrv_(un)register_buf
qemu-img: Map bench buffer
block: Move NVMe constants to a separate header
docs: Add section for NVMe VFIO driver
qapi: Add NVMe driver options to the schema

 MAINTAINERS                    |    6 +
 block/Makefile.objs            |    1 +
 block/block-backend.c          |   10 +
 block/io.c                     |   24 +
 block/nvme.c                   | 1199 ++++++++++++++++++++++++++++++++++++++++
 block/trace-events             |   21 +
 docs/qemu-block-drivers.texi   |   37 ++
 hw/block/nvme.h                |  698 +----------------------
 include/block/block.h          |   11 +-
 include/block/block_int.h      |    9 +
 include/block/nvme.h           |  700 +++++++++++++++++++++++
 include/qemu/vfio-helpers.h    |   33 ++
 include/sysemu/block-backend.h |    3 +
 qapi/block-core.json           |   17 +-
 qemu-img.c                     |    9 +-
 stubs/Makefile.objs            |    1 +
 stubs/ram-block.c              |   16 +
 util/Makefile.objs             |    1 +
Quoted 12 lines
 util/trace-events              |   11 +
 util/vfio-helpers.c            |  726 ++++++++++++++++++++++++
 20 files changed, 2833 insertions(+), 700 deletions(-)
 create mode 100644 block/nvme.c
 create mode 100644 include/block/nvme.h
 create mode 100644 include/qemu/vfio-helpers.h
 create mode 100644 stubs/ram-block.c
 create mode 100644 util/vfio-helpers.c
Signature · 1 line
2.14.3
#4 lore raw

Re: [Qemu-devel] [PATCH v6 3/9] block: Add VFIO based NVMe driver

In reply to Fam Zheng
Quoted 13 lines · Fam Zheng

This is a new protocol driver that exclusively opens a host NVMe controller through VFIO. It achieves better latency than linux-aio by completely bypassing host kernel vfs/block layer.

     $rw-$bs-$iodepth  linux-aio     nvme://
     ----------------------------------------
     randread-4k-1     10.5k         21.6k
     randread-512k-1   745           1591
     randwrite-4k-1    30.7k         37.0k
     randwrite-512k-1  1945          1980

     (unit: IOPS)
Quoted 8 lines

The driver also integrates with the polling mechanism of iothread.

This patch is co-authored by Paolo and me.

Message-Id: <[email protected]>
---

Sorry for not noticing sooner, but

block/nvme.c line 1082

Quoted 10 lines
block/nvme.c new file +10 -0
+static int64_t coroutine_fn nvme_co_get_block_status(BlockDriverState *bs,
+                                                     int64_t sector_num,
+                                                     int nb_sectors, int *pnum,
+                                                     BlockDriverState **file)
+{
+    *pnum = nb_sectors;
+    *file = bs;
+
+    return BDRV_BLOCK_ALLOCATED | BDRV_BLOCK_OFFSET_VALID |
+           (sector_num << BDRV_SECTOR_BITS);

This is wrong. Drivers should only ever return BDRV_BLOCK_DATA (which io.c then _adds_ BDRV_BLOCK_ALLOCATED to, as needed). I'll fix it up as part of my byte-based block status series (v8 coming up soon).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org