Skip to contentSkip to conversation

Introduce a new NVMe host device type to QEMU

RFC In review Changpeng Liu 5 participants 10 messages Jan 15, 2018 – Oct 24, 2018

lore
Share & tools
lore
#0 lore raw

NVMe 1.3 specification(http://nvmexpress.org/resources/specifications/) introduced a new Admin command:
Doorbell Buffer Config, which designed for emulated NVMe controllers only, Linux kernel 4.12 added the support of Doorbell Buffer Config. With this feature, when NVMe driver issues new requests to firmware, the driver will write shadow doorbell instead of MMIO writes, so the NVMe specification itself can become a great Para-virtualization protocol.

While here, similar with existing vhost-user-scsi idea, we can setup a slave I/O target which can serve Guest I/Os directly via NVMe I/O queues. Here we can route the NVMe queue's information, such as queue size/queue address etc. to a separate slave I/O target via UNIX domain socket. I took exist QEMU vhost-user protocol as reference, designed several totally new socket messages to enable the function. With this idea, an emulated virtual NVMe controller will be presented at the Guest, and native NVMe driver inside Guest can be used.

-----------------------------------------------------------------------------------------------------------------------------------------
| Unix Domain Socket Messages | Description |
-----------------------------------------------------------------------------------------------------------------------------------------
| Get Controller Capabilities | Controller capabilitiy register of NVMe specification |
-----------------------------------------------------------------------------------------------------------------------------------------
| Get/Set Controller Configuration | Enable/Disable NVMe controller |
-----------------------------------------------------------------------------------------------------------------------------------------
| Admin passthrough | Mandatory NVMe Admin commands routed to slave I/O target |
-----------------------------------------------------------------------------------------------------------------------------------------
| IO passthrough | IO messages before the shadow doorbell buffer being configured |
-----------------------------------------------------------------------------------------------------------------------------------------
| Set memory table | Same with exist vhost-user message, used for memory translation |
-----------------------------------------------------------------------------------------------------------------------------------------
| Set Guest Notifier | Completion queue interrupt, interrupt Guest when I/O completed |
-----------------------------------------------------------------------------------------------------------------------------------------

With those messages, slave I/O target can access all the I/O queues of NVMe include submission queue and completion queue. After finished the Admin Shadow Doorbell command, the slave I/O target can start to process the I/O requests sent from Guest.

Currently I implemented both QEMU driver and slave I/O target which largely reused the code from QEMU NVMe driver and vhost-user driver for performance evaluation:

Optional slave I/O target(SPDK Vhost Target) patches: https://review.gerrithub.io/#/c/384213/

User space NVMe driver is implemented at the slave I/O target so that NVMe controller can be shared with multiple VMs, and the namespaces presented to the guest VM are virtual namespaces, meaning the slave I/O target can back these namespaces with any kind of storage. Guest OS must be 4.12 or later(with Admin Doorbell Buffer Config support), tests from my side used Fedora 27 with 4.13 kernel.

Currently this still is an ongoing work, there are some opens need to be addressed:
-Reused a lot of code from QEMU/nvme driver, need to think about abstracting a common NVMe library; -Reused a lot of code from QEMU/vhost-user driver, for this idea, we just want to use UNIX domain
socket to deliver mandatory messages, of course Set memory table and Set guest notifier is exactly same with vhost-user driver;
-Can support Guest OS kernel > 4.12 with Admin Doorbell Buffer feature enabled inside Guest, for BIOS
stage IO requests and older Linux kernel without Admin Doorbell Buffer support, it can forward the IO requests through socket message, but this will have huge performance drop;

Any feedback is appreciated.

Changpeng Liu (1):
block/NVMe: introduce a new vhost NVMe host device to QEMU

 hw/block/Makefile.objs     |   3 +
 hw/block/nvme.h            |  28 ++
 hw/block/vhost.c           | 439 ++++++++++++++++++++++
 hw/block/vhost_user.c      | 588 +++++++++++++++++++++++++++++
 hw/block/vhost_user_nvme.c | 902 +++++++++++++++++++++++++++++++++++++++++++++
 hw/block/vhost_user_nvme.h |  38 ++
 6 files changed, 1998 insertions(+)
 create mode 100644 hw/block/vhost.c
 create mode 100644 hw/block/vhost_user.c
 create mode 100644 hw/block/vhost_user_nvme.c
 create mode 100644 hw/block/vhost_user_nvme.h
Signature · 1 line
1.9.3
#1 lore raw

RFC 1/1 block/NVMe: introduce a new vhost NVMe host device to QEMU

NVMe 1.3 specification introduces a new NVMe ADMIN command:
doorbell buffer config, which can write shadow doorbell buffer instead of MMIO registers, so it can improve the Guest performance a lot for emulated NVMe devices inside VM.

Similar with existing vhost-user-scsi solution, this commit builds a new vhost_user_nvme host device to VM and the I/O is processed at the slave I/O target, so users can implement a user space NVMe driver in the slave I/O target.

Users can start QEMU with: -chardev socket,id=char0,path=/path/vhost.0 \
-device vhost-user-nvme,chardev=char0,num_io_queues=2.

Currently Guest OS must use 4.12 kernel or later.

---
 hw/block/Makefile.objs     |   3 +
 hw/block/nvme.h            |  28 ++
 hw/block/vhost.c           | 439 ++++++++++++++++++++++
 hw/block/vhost_user.c      | 588 +++++++++++++++++++++++++++++
 hw/block/vhost_user_nvme.c | 902 +++++++++++++++++++++++++++++++++++++++++++++
 hw/block/vhost_user_nvme.h |  38 ++
 6 files changed, 1998 insertions(+)
 create mode 100644 hw/block/vhost.c
 create mode 100644 hw/block/vhost_user.c
 create mode 100644 hw/block/vhost_user_nvme.c
 create mode 100644 hw/block/vhost_user_nvme.h
hw/block/Makefile.objs +3 -0
index e0ed980..0b27529 100644
@@ -8,6 +8,9 @@ common-obj-$(CONFIG_XEN) += xen_disk.o
 common-obj-$(CONFIG_ECC) += ecc.o
 common-obj-$(CONFIG_ONENAND) += onenand.o
 common-obj-$(CONFIG_NVME_PCI) += nvme.o
+ifeq ($(CONFIG_VIRTIO),y)
+common-obj-$(CONFIG_LINUX) += vhost_user_nvme.o vhost.o vhost_user.o
+endif
 
 obj-$(CONFIG_SH4) += tc58128.o
 
hw/block/nvme.h +28 -0
index 6aab338..aa468fb 100644
@@ -1,6 +1,8 @@
 #ifndef HW_NVME_H
 #define HW_NVME_H
 #include "qemu/cutils.h"
+#include "hw/virtio/vhost.h"
+#include "chardev/char-fe.h"
 
 typedef struct NvmeBar {
     uint64_t    cap;
@@ -236,6 +238,7 @@ enum NvmeAdminCommands {
     NVME_ADM_CMD_ASYNC_EV_REQ   = 0x0c,
     NVME_ADM_CMD_ACTIVATE_FW    = 0x10,
     NVME_ADM_CMD_DOWNLOAD_FW    = 0x11,
+    NVME_ADM_CMD_DB_BUFFER_CFG  = 0x7c,
     NVME_ADM_CMD_FORMAT_NVM     = 0x80,
     NVME_ADM_CMD_SECURITY_SEND  = 0x81,
     NVME_ADM_CMD_SECURITY_RECV  = 0x82,
@@ -414,6 +417,18 @@ typedef struct NvmeCqe {
     uint16_t    status;
 } NvmeCqe;
 
+typedef struct NvmeStatus {
+    uint16_t p:1;     /* phase tag */
+    uint16_t sc:8;    /* status code */
+    uint16_t sct:3;   /* status code type */
+    uint16_t rsvd2:2;
+    uint16_t m:1;     /* more */
+    uint16_t dnr:1;   /* do not retry */
+} NvmeStatus;
+
+#define nvme_cpl_is_error(status) \
+        (((status & 0x01fe) != 0) || ((status & 0x0e00) != 0))
+
 enum NvmeStatusCodes {
     NVME_SUCCESS                = 0x0000,
     NVME_INVALID_OPCODE         = 0x0001,
@@ -573,6 +588,7 @@ enum NvmeIdCtrlOacs {
     NVME_OACS_SECURITY  = 1 << 0,
     NVME_OACS_FORMAT    = 1 << 1,
     NVME_OACS_FW        = 1 << 2,
+    NVME_OACS_DB_BUF    = 1 << 8,
 };
 
 enum NvmeIdCtrlOncs {
@@ -739,8 +755,10 @@ typedef struct NvmeCQueue {
     uint32_t    head;
     uint32_t    tail;
     uint32_t    vector;
+    int32_t     virq;
     uint32_t    size;
     uint64_t    dma_addr;
+    EventNotifier guest_notifier;
     QEMUTimer   *timer;
     QTAILQ_HEAD(sq_list, NvmeSQueue) sq_list;
     QTAILQ_HEAD(cq_req_list, NvmeRequest) req_list;
@@ -754,6 +772,10 @@ typedef struct NvmeNamespace {
 #define NVME(obj) \
         OBJECT_CHECK(NvmeCtrl, (obj), TYPE_NVME)
 
+#define TYPE_VHOST_NVME "vhost-user-nvme"
+#define NVME_VHOST(obj) \
+        OBJECT_CHECK(NvmeCtrl, (obj), TYPE_VHOST_NVME)
+
 typedef struct NvmeCtrl {
     PCIDevice    parent_obj;
     MemoryRegion iomem;
@@ -761,6 +783,12 @@ typedef struct NvmeCtrl {
     NvmeBar      bar;
     BlockConf    conf;
 
+    int32_t    bootindex;
+    CharBackend chardev;
+    struct vhost_dev dev;
+    uint32_t    num_io_queues;
+    bool        dataplane_started;
+
     uint32_t    page_size;
     uint16_t    page_bits;
     uint16_t    max_prp_ents;
hw/block/vhost.c new file +439 -0

439 lines not on this page · raw message

hw/block/vhost_user.c new file +588 -0

588 lines not on this page · raw message

hw/block/vhost_user_nvme.c new file +902 -0

902 lines not on this page · raw message

hw/block/vhost_user_nvme.h new file +38 -0
new file mode 100644
index 0000000..623338d
@@ -0,0 +1,38 @@
+#ifndef HW_VHOST_USER_NVME_H
+#define HW_VHOST_USER_NVME_H
+/*
+ * vhost-user-nvme
+ *
+ * Copyright (c) 2017 Intel Corporation. All rights reserved.
+ *
+ *  Author:
+ *  Changpeng Liu <[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.
+ *
+ */
+
+#include "hw/pci/pci.h"
+#include "hw/block/block.h"
+#include "nvme.h"
+
+int vhost_dev_nvme_set_guest_notifier(struct vhost_dev *hdev,
+                                      EventNotifier *notifier, uint32_t qid);
+int vhost_dev_nvme_init(struct vhost_dev *hdev, void *opaque,
+                   VhostBackendType backend_type, uint32_t busyloop_timeout);
+void vhost_dev_nvme_cleanup(struct vhost_dev *hdev);
+
+
+int
+vhost_user_nvme_io_cmd_pass(struct vhost_dev *dev, uint16_t qid,
+                            uint16_t tail_head, bool submission_queue);
+int vhost_user_nvme_admin_cmd_raw(struct vhost_dev *dev, NvmeCmd *cmd,
+                                  void *buf, uint32_t len);
+int vhost_user_nvme_get_cap(struct vhost_dev *dev, uint64_t *cap);
+int vhost_dev_nvme_set_backend_type(struct vhost_dev *dev,
+                                    VhostBackendType backend_type);
+int vhost_dev_nvme_start(struct vhost_dev *hdev, VirtIODevice *vdev);
+int vhost_dev_nvme_stop(struct vhost_dev *hdev);
+
+#endif
Signature · 1 line
1.9.3
#2 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

Quoted 5 lines · Changpeng Liu

NVMe 1.3 specification introduces a new NVMe ADMIN command:
doorbell buffer config, which can write shadow doorbell buffer instead of MMIO registers, so it can improve the Guest performance a lot for emulated NVMe devices inside VM.

Quoted 7 lines

Similar with existing vhost-user-scsi solution, this commit builds a new vhost_user_nvme host device to VM and the I/O is processed at the slave I/O target, so users can implement a user space NVMe driver in the slave I/O target.

Users can start QEMU with: -chardev socket,id=char0,path=/path/vhost.0 \
-device vhost-user-nvme,chardev=char0,num_io_queues=2.

Hi Changpeng,

I have two comments on this series.

First, the new command in NVMe 1.3 is great. However, please first add support for the doorbell buffer config in hw/block/nvme.c. There is no need to tie support for the new command to a completely new external server architecture. Emulated NVMe can be enhanced to use iothreads and (when the doorbell buffer is configured) ioeventfd, and that should come before enhancements for external vhost-like servers.

Second, virtio-based vhost-user remains QEMU's preferred method for high-performance I/O in guests. Discard support is missing and that is important for SSDs; that should be fixed in the virtio spec. Are there any other features where virtio-blk is lagging behind NVMe?

Thanks,

Paolo

#3 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

Quoted 28 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: Wednesday, January 17, 2018 1:07 AM
To: Liu, Changpeng <[email protected]>; [email protected]
Cc: Harris, James R <[email protected]>; Busch, Keith <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

Quoted 5 lines · Changpeng Liu

NVMe 1.3 specification introduces a new NVMe ADMIN command:
doorbell buffer config, which can write shadow doorbell buffer instead of MMIO registers, so it can improve the Guest performance a lot for emulated NVMe devices inside VM.

Quoted 7 lines

Similar with existing vhost-user-scsi solution, this commit builds a new vhost_user_nvme host device to VM and the I/O is processed at the slave I/O target, so users can implement a user space NVMe driver in the slave I/O target.

Users can start QEMU with: -chardev socket,id=char0,path=/path/vhost.0 \
-device vhost-user-nvme,chardev=char0,num_io_queues=2.

Hi Changpeng,

Quoted 8 lines

I have two comments on this series.

First, the new command in NVMe 1.3 is great. However, please first add support for the doorbell buffer config in hw/block/nvme.c. There is no need to tie support for the new command to a completely new external server architecture. Emulated NVMe can be enhanced to use iothreads and (when the doorbell buffer is configured) ioeventfd, and that should come before enhancements for external vhost-like servers.

Yes, adding doorbell buffer config support at hw/block/nvme.c is great to improve the efficiency, this vhost-like idea is another solution which can provide end-to-end userspace software stack.

Quoted 4 lines

Second, virtio-based vhost-user remains QEMU's preferred method for high-performance I/O in guests. Discard support is missing and that is important for SSDs; that should be fixed in the virtio spec. Are there

Previously I have a patch adding DISCARD support to virtio-blk, but I didn't find the way using svn to update the spec patch, any git repository I can use to update the virtio-blk spec ? I think I can pick up the feature again.

Quoted 1 line

any other features where virtio-blk is lagging behind NVMe?

From the efficiency consideration, I compared the solution with vhost-user-blk,
Indeed, the two solution are almost at the same level, IOPS/CPU utilization inside Guest/KVM events(VM_EXIT, KVM_FAST_MMIO,KVM_MSI_SET_IRQ,KVM_MSR) are almost same.

Quoted 4 lines

Thanks,

Paolo

#4 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

Quoted 6 lines · Changpeng
Quoted 3 lines

Second, virtio-based vhost-user remains QEMU's preferred method for high-performance I/O in guests. Discard support is missing and that is important for SSDs; that should be fixed in the virtio spec. Are there

Previously I have a patch adding DISCARD support to virtio-blk, but I didn't find the way using svn to update the spec patch, any git repository I can use to update the virtio-blk spec ? I think I can pick up the feature again.

There is https://github.com/oasis-tcs/virtio-spec.

You should add support for all of:

1) a command that can do WRITE ZERO, WRITE ZERO with discard, and advisory DISCARD of a range of blocks. The command must be controlled by a new feature bit.

2) a configuration space element for discard granularity, controlled by the same feature bit as (1)

3) a feature bit that is 1 if WRITE ZERO with discard can actually discard parts that are aligned to the granularity

Thanks,

Paolo

#5 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

In reply to Paolo Bonzini
Quoted 3 lines · Paolo Bonzini

Second, virtio-based vhost-user remains QEMU's preferred method for high-performance I/O in guests. Discard support is missing and that is important for SSDs; that should be fixed in the virtio spec.

BTW could you reply on the thread of the patch
virtio_blk: add discard and write zeroes support

Christoph Hellwig thinks we should change the spec and defer implementation until we do. What's your take on this?

Signature · 1 line
MST
#6 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

The latest thread for supporting DISCARD and WRITE ZEROES feature for virtio-blk is here:

virtio_blk: add discard and write zeroes support
https://lists.linuxfoundation.org/pipermail/virtualization/2018-October/039534.html

I don't take further more steps about it, please go ahead and make the specification more clear, thanks.

Quoted 24 lines

-----Original Message-----
From: Michael S. Tsirkin [mailto:[email protected]]
Sent: Wednesday, October 24, 2018 1:39 AM
To: Paolo Bonzini <[email protected]>
Cc: Liu, Changpeng <[email protected]>; [email protected]; Harris, James R <[email protected]>; Busch, Keith <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

Quoted 3 lines · Paolo Bonzini

Second, virtio-based vhost-user remains QEMU's preferred method for high-performance I/O in guests. Discard support is missing and that is important for SSDs; that should be fixed in the virtio spec.

BTW could you reply on the thread of the patch
virtio_blk: add discard and write zeroes support

Christoph Hellwig thinks we should change the spec and defer implementation until we do. What's your take on this?

--
MST

#7 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

In reply to Changpeng Liu
Quoted 4 lines · Changpeng Liu

NVMe 1.3 specification introduces a new NVMe ADMIN command:
doorbell buffer config, which can write shadow doorbell buffer instead of MMIO registers, so it can improve the Guest performance a lot for emulated NVMe devices inside VM.

If I understand correctly the Shadow Doorbell Buffer offers two optimizations:

1. The guest driver only writes to the MMIO register when EventIdx has been reached. This eliminates some MMIO writes.

2. The device may poll the Shadow Doorbell Buffer so that command processing can begin before guest driver performs an MMIO write.

Is this correct?

Quoted 7 lines

Similar with existing vhost-user-scsi solution, this commit builds a new vhost_user_nvme host device to VM and the I/O is processed at the slave I/O target, so users can implement a user space NVMe driver in the slave I/O target.

Users can start QEMU with: -chardev socket,id=char0,path=/path/vhost.0 \
-device vhost-user-nvme,chardev=char0,num_io_queues=2.

Each new feature has a cost in terms of maintainance, testing, documentation, and support. Users need to be educated about the role of each available storage controller and how to choose between them.

I'm not sure why QEMU should go in this direction since it makes the landscape more complex and harder to support. You've said the performance is comparable to vhost-user-blk. So what does NVMe offer that makes this worthwhile?

A cool NVMe feature would be the ability to pass through invididual queues to different guests without SR-IOV. In other words, bind a queue to namespace subset so that multiple guests can be isolated from each other. That way the data path would not require vmexits. The control path and device initialization would still be emulated by QEMU so the hardware does not need to provide the full resources and state needed for SR-IOV. I looked into this but came to the conclusion that it would require changes to the NVMe specification because the namespace is a per-command field.

#8 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

On 1/29/18, 8:29 AM, "Stefan Hajnoczi" <[email protected]> wrote:

<trim>

Each new feature has a cost in terms of maintainance, testing, documentation, and support. Users need to be educated about the role of each available storage controller and how to choose between them.

I'm not sure why QEMU should go in this direction since it makes the landscape more complex and harder to support. You've said the performance is comparable to vhost-user-blk. So what does NVMe offer that makes this worthwhile?

A cool NVMe feature would be the ability to pass through invididual queues to different guests without SR-IOV. In other words, bind a queue to namespace subset so that multiple guests can be isolated from each other. That way the data path would not require vmexits. The control path and device initialization would still be emulated by QEMU so the hardware does not need to provide the full resources and state needed for SR-IOV. I looked into this but came to the conclusion that it would require changes to the NVMe specification because the namespace is a per-command field.

Correct – any command from any queue can access any namespace on the controller.

Another reason this is not possible is that most (if not all?) controllers have CAP.DSTRD (Doorbell Stride) = 0, meaning doorbell registers for all queues fall within the same page.

#9 lore raw

Re: [Qemu-devel] [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

In reply to Stefan Hajnoczi
Quoted 18 lines

-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Monday, January 29, 2018 11:29 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; Harris, James R <[email protected]>; Busch, Keith <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [RFC v1] block/NVMe: introduce a new vhost NVMe host device to QEMU

Quoted 4 lines · Changpeng Liu

NVMe 1.3 specification introduces a new NVMe ADMIN command:
doorbell buffer config, which can write shadow doorbell buffer instead of MMIO registers, so it can improve the Guest performance a lot for emulated NVMe devices inside VM.

Quoted 5 lines

If I understand correctly the Shadow Doorbell Buffer offers two optimizations:

1. The guest driver only writes to the MMIO register when EventIdx has been reached. This eliminates some MMIO writes.

Correct.

Quoted 5 lines

2. The device may poll the Shadow Doorbell Buffer so that command processing can begin before guest driver performs an MMIO write.

Is this correct?

Guest should write shadow doorbell memory every time(each new request from Guest), Guest may write PCI registers or not, depends on slave target's feedback. Slave target can poll the shadow doorbell for new requests.
This can eliminate the MMIO writes for submission data path.

Quoted 9 lines
Quoted 8 lines

Similar with existing vhost-user-scsi solution, this commit builds a new vhost_user_nvme host device to VM and the I/O is processed at the slave I/O target, so users can implement a user space NVMe driver in the slave I/O target.

Users can start QEMU with: -chardev socket,id=char0,path=/path/vhost.0 \
-device vhost-user-nvme,chardev=char0,num_io_queues=2.

Quoted 8 lines

Each new feature has a cost in terms of maintainance, testing, documentation, and support. Users need to be educated about the role of each available storage controller and how to choose between them.

I'm not sure why QEMU should go in this direction since it makes the landscape more complex and harder to support. You've said the performance is comparable to vhost-user-blk. So what does NVMe offer that makes this worthwhile?

Good question, from the test results, this solution is almost same with vhost-blk, this is still an ongoing work, I don't have a *MUST* justification now.

Quoted 10 lines

A cool NVMe feature would be the ability to pass through invididual queues to different guests without SR-IOV. In other words, bind a queue to namespace subset so that multiple guests can be isolated from each other. That way the data path would not require vmexits. The control path and device initialization would still be emulated by QEMU so the hardware does not need to provide the full resources and state needed for SR-IOV. I looked into this but came to the conclusion that it would require changes to the NVMe specification because the namespace is a per-command field.