Introduce a new vhost-user-blk host device to QEMU
v6 cover Introduce a new vhost-user-blk host device to QEMU
Although virtio scsi specification was designed as a replacement for virtio_blk, there are still many users using virtio_blk. Qemu 2.9 introduced a new device vhost user scsi which can process I/O in user space for virtio_scsi, this commit introduces a new vhost user block host device, which can support virtio_blk in Guest OS, and I/O processing in another I/O target.
Due to the limitation for virtio_blk specification, virtio_blk device cannot get block information such as capacity, block size etc via the specification, several new vhost user messages were added to deliver virtio config space information between Qemu and I/O target, VHOST_USER_GET_CONFIG/VHOST_USER_SET_CONFIG messages used for get/set config space from/to I/O target, VHOST_USER_SET_CONFIG_FD was added for event notifier in case the change of virtio config space. Also, those messages can be used for vhost device live migration as well.
Changes:
v5-v6: add header flags for vhost-user master so that the slave can know the purpose for set config, also vhost-user get/set messages' payload doesn't contain invalid data buffers.
v4-v5: add header offset and size for virtio config space.
v3-v4: refactoring the vhost user block example patch based on new libvhost-user library.
v2-v3: add new vhost user message to get/set virtio config space.
Changpeng Liu (4):
vhost-user: add new vhost user messages to support virtio config space
vhost-user-blk: introduce a new vhost-user-blk host device contrib/libvhost-user: enable virtio config space messages contrib/vhost-user-blk: introduce a vhost-user-blk sample application
.gitignore | 1 + Makefile | 3 + Makefile.objs | 1 + contrib/libvhost-user/libvhost-user.c | 80 +++++ contrib/libvhost-user/libvhost-user.h | 36 ++- contrib/vhost-user-blk/Makefile.objs | 1 + contrib/vhost-user-blk/vhost-user-blk.c | 556 ++++++++++++++++++++++++++++++++ default-configs/pci.mak | 1 + docs/interop/vhost-user.txt | 44 +++ hw/block/Makefile.objs | 3 + hw/block/vhost-user-blk.c | 357 ++++++++++++++++++++ hw/virtio/vhost-user.c | 107 ++++++ hw/virtio/vhost.c | 64 ++++ hw/virtio/virtio-pci.c | 55 ++++ hw/virtio/virtio-pci.h | 18 ++ include/hw/virtio/vhost-backend.h | 14 + include/hw/virtio/vhost-user-blk.h | 41 +++ include/hw/virtio/vhost.h | 16 + 18 files changed, 1397 insertions(+), 1 deletion(-) create mode 100644 contrib/vhost-user-blk/Makefile.objs create mode 100644 contrib/vhost-user-blk/vhost-user-blk.c create mode 100644 hw/block/vhost-user-blk.c create mode 100644 include/hw/virtio/vhost-user-blk.h
Signature · 1 line
1.9.3
PATCH v6 1/4 vhost-user: add new vhost user messages to support virtio config space
Add VHOST_USER_GET_CONFIG/VHOST_USER_SET_CONFIG messages which can be used for live migration of vhost user devices, also vhost user devices can benefit from the messages to get/set virtio config space from/to the I/O target. For the purpose to support virtio config space change, VHOST_USER_SET_CONFIG_FD message is added as the event notifier in case virtio config space change in the I/O target.
--- docs/interop/vhost-user.txt | 44 ++++++++++++++++ hw/virtio/vhost-user.c | 107 ++++++++++++++++++++++++++++++++++++++ hw/virtio/vhost.c | 64 +++++++++++++++++++++++ include/hw/virtio/vhost-backend.h | 14 +++++ include/hw/virtio/vhost.h | 16 ++++++ 5 files changed, 245 insertions(+)
docs/interop/vhost-user.txt +44 -0
@@ -116,6 +116,19 @@ Depending on the request type, payload can be: - 3: IOTLB invalidate - 4: IOTLB access fail + * Virtio device config space + ----------------------------------- + | offset | size | flags | payload | + ----------------------------------- + + Offset: a 32-bit offset of virtio device's configuration space + Size: a 32-bit configuration space access size in bytes + Flags: a 32-bit value: + - 0: Vhost master messages used for writeable fields + - 1: Vhost master messages used for live migration + Payload: Size bytes array holding the contents of the virtio + device's configuration space + In QEMU the vhost-user message is implemented with the following struct: typedef struct VhostUserMsg {
@@ -129,6 +142,7 @@ typedef struct VhostUserMsg { VhostUserMemory memory; VhostUserLog log; struct vhost_iotlb_msg iotlb; + VhostUserConfig config; }; } QEMU_PACKED VhostUserMsg;
@@ -596,6 +610,36 @@ Master message types and expect this message once (per VQ) during device configuration (ie. before the master starts the VQ). + * VHOST_USER_GET_CONFIG + Id: 24 + Equivalent ioctl: N/A + Master payload: virtio device config space + + Submitted by the vhost-user master to fetch the contents of the virtio + device configuration space. The vhost-user master may cache the contents + to avoid repeated VHOST_USER_GET_CONFIG calls. + +* VHOST_USER_SET_CONFIG + Id: 25 + Equivalent ioctl: N/A + Master payload: virtio device config space + + Submitted by the vhost-user master when the Guest changes the virtio + device configuration space and also can be used for live migration + on the destination host. The vhost-user slave must check the flags + filed, and slaves MUST NOT accept SET_CONFIG for read-only + configuration space fields unless the live migration bit is set. + +* VHOST_USER_SET_CONFIG_FD + Id: 26 + Equivalent ioctl: N/A + Master payload: N/A + + Sets the notifier file descriptor, which is passed as ancillary data. + The vhost-user slave uses the file descriptor to notify the vhost-user + master of changes to the virtio configuration space. The vhost-user + master can read the virtio configuration space to get the latest update. + Slave message types -------------------
hw/virtio/vhost-user.c +107 -0
141 lines not on this page · raw message
hw/virtio/vhost.c +64 -0
76 lines not on this page · raw message
include/hw/virtio/vhost-backend.h +14 -0
@@ -20,6 +20,11 @@ typedef enum VhostBackendType {VHOST_BACKEND_TYPE_MAX =3 , } VhostBackendType; +typedef enum VhostSetConfigType { +VHOST_SET_CONFIG_TYPE_MASTER =0 , +VHOST_SET_CONFIG_TYPE_MIGRATION =1 , +}VhostSetConfigType ; +struct vhost_dev ;struct vhost_log ;struct vhost_memory ;
@@ -84,6 +89,12 @@ typedef void (*vhost_set_iotlb_callback_op)(struct vhost_dev *dev, int enabled);typedef int (*vhost_send_device_iotlb_msg_op )(struct vhost_dev *dev,struct vhost_iotlb_msg *imsg); +typedef int (*vhost_set_config_op )(struct vhost_dev *dev,const uint8_t *config, +uint32_t offset,uint32_t size, +uint32_t flags); +typedef int (*vhost_get_config_op )(struct vhost_dev *dev,uint8_t *config, +uint32_t config_len); +typedef int (*vhost_set_config_fd_op )(struct vhost_dev *dev,int fd); typedef struct VhostOps { VhostBackendType backend_type;
@@ -118,6 +129,9 @@ typedef struct VhostOps {vhost_vsock_set_running_op vhost_vsock_set_running;vhost_set_iotlb_callback_op vhost_set_iotlb_callback;vhost_send_device_iotlb_msg_op vhost_send_device_iotlb_msg; +vhost_get_config_op vhost_get_config; +vhost_set_config_op vhost_set_config; +vhost_set_config_fd_op vhost_set_config_fd; } VhostOps;extern const VhostOps user_ops;
include/hw/virtio/vhost.h +16 -0
@@ -46,6 +46,12 @@ struct vhost_iommu {QLIST_ENTRY (vhost_iommu) iommu_next; }; +typedef struct VhostDevConfigOps { +/* Vhost device config space changed callback +*/ +void (*vhost_dev_config_notifier)(struct vhost_dev *dev); +}VhostDevConfigOps ; +struct vhost_memory ;struct vhost_dev {VirtIODevice *vdev;
@@ -76,6 +82,8 @@ struct vhost_dev {QLIST_ENTRY (vhost_dev) entry; QLIST_HEAD(, vhost_iommu) iommu_list;IOMMUNotifier n; +EventNotifier config_notifier; +const VhostDevConfigOps *config_ops; }; int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
@@ -106,4 +114,12 @@ int vhost_net_set_backend(struct vhost_dev *hdev, struct vhost_vring_file *file);int vhost_device_iotlb_miss (struct vhost_dev *dev,uint64_t iova,int write); +int vhost_dev_get_config (struct vhost_dev *dev,uint8_t *config, +uint32_t config_len); +int vhost_dev_set_config (struct vhost_dev *dev,const uint8_t *config, +uint32_t offset,uint32_t size,uint32_t flags); +/* notifier callback in case vhost device config space changed +*/ +int vhost_dev_set_config_notifier (struct vhost_dev *dev, +const VhostDevConfigOps *ops);#endif
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v6 1/4] vhost-user: add new vhost user messages to support virtio config space
docs/interop/vhost-user.txt Master message types · line 630
Quoted 9 lines · Changpeng Liu
docs/interop/vhost-user.txt +9
-0+* VHOST_USER_SET_CONFIG + Id: 25 + Equivalent ioctl: N/A + Master payload: virtio device config space + + Submitted by the vhost-user master when the Guest changes the virtio + device configuration space and also can be used for live migration + on the destination host. The vhost-user slave must check the flags + filed, and slaves MUST NOT accept SET_CONFIG for read-only
s/filed/field/
Quoted 12 lines
hw/virtio/vhost-user.c +12
-0+static int vhost_user_set_config (struct vhost_dev *dev,const uint8_t *config, +uint32_t offset,uint32_t size,uint32_t flags) +{ +uint8_t *p; +bool reply_supported =virtio_has_feature (dev->protocol_features, +VHOST_USER_PROTOCOL_F_REPLY_ACK ); + +VhostUserMsg msg = { + msg.request =VHOST_USER_SET_CONFIG , + msg.flags =VHOST_USER_VERSION , + msg.size =VHOST_USER_CONFIG_HDR_SIZE + size, + };
hw/virtio/vhost-user.c static void vhost_user_set_iotlb_callback(struct vhost_dev *dev, int enabled) · line 1002
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+ +if (reply_supported) { + msg.flags |=VHOST_USER_NEED_REPLY_MASK ; + } + + msg.payload.config.offset = offset, + msg.payload.config.size = size, + msg.payload.config.flags = flags, + p = msg.payload.config.region; +memcpy (p, config + offset, size);
This function can be made more general by changing the semantics of the config argument:
memcpy(p, config, size);
Now the caller can pass just a single field instead of a whole 256-byte config buffer. It might be clearer to name the argument "data" or "region" instead of "config" though.
Quoted 18 lines
hw/virtio/vhost.c +14
-0@@ -1505,6 +1508,67 @@ void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, } } +int vhost_dev_get_config (struct vhost_dev *hdev,uint8_t *config, +uint32_t config_len) +{ +assert (hdev->vhost_ops); + +if (hdev->vhost_ops->vhost_get_config) { +return hdev->vhost_ops->vhost_get_config (hdev, config, config_len); + } + +return 0 ; +} + +int vhost_dev_set_config (struct vhost_dev *hdev,const uint8_t *config, +uint32_t offset,uint32_t size,uint32_t flags)
hw/virtio/vhost.c void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, · line 1534
Quoted 10 lines
hw/virtio/vhost.c +10
-0+{ +assert (hdev->vhost_ops); + +if (hdev->vhost_ops->vhost_set_config) { +return hdev->vhost_ops->vhost_set_config (hdev, config, offset, + size, flags); + } + +return 0 ; +}
Both vhost_dev_get_config() and vhost_dev_set_config() cannot fail silently. The device will not work properly if the configuration space feature is not supported.
Please make these functions return an error if the callback is NULL.
The vhost-blk code should also check that hdev->vhost_ops->vhost_set_config != NULL during realize. This way users see the error when adding the device instead of at runtime when the function gets called.
Re: [Qemu-devel] [PATCH v6 1/4] vhost-user: add new vhost user messages to support virtio config space
Quoted 13 lines
-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Monday, December 11, 2017 9:39 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Harris, James R <[email protected]>
Subject: Re: [PATCH v6 1/4] vhost-user: add new vhost user messages to support virtio config spaceOn Tue, Dec 05, 2017 at 02:27:16PM +0800, Changpeng Liu wrote:
Quoted 11 lines
docs/interop/vhost-user.txt Master message types · line 630
Quoted 9 lines
docs/interop/vhost-user.txt +9
-0+* VHOST_USER_SET_CONFIG + Id: 25 + Equivalent ioctl: N/A + Master payload: virtio device config space + + Submitted by the vhost-user master when the Guest changes the virtio + device configuration space and also can be used for live migration + on the destination host. The vhost-user slave must check the flags + filed, and slaves MUST NOT accept SET_CONFIG for read-onlys/filed/field/
Thanks.
Quoted 27 lines
Quoted 13 lines
hw/virtio/vhost-user.c +12
-0+static int vhost_user_set_config (struct vhost_dev *dev,const uint8_t *config, +uint32_t offset,uint32_t size,uint32_t flags) +{ +uint8_t *p; +bool reply_supported =virtio_has_feature (dev->protocol_features, +VHOST_USER_PROTOCOL_F_REPLY_ACK ); + +VhostUserMsg msg = { + msg.request =VHOST_USER_SET_CONFIG , + msg.flags =VHOST_USER_VERSION , + msg.size =VHOST_USER_CONFIG_HDR_SIZE + size, + };hw/virtio/vhost-user.c static void vhost_user_set_iotlb_callback(struct vhost_dev *dev, int enabled) · line 1002
Quoted 10 lines
hw/virtio/vhost-user.c +10
-0+ +if (reply_supported) { + msg.flags |=VHOST_USER_NEED_REPLY_MASK ; + } + + msg.payload.config.offset = offset, + msg.payload.config.size = size, + msg.payload.config.flags = flags, + p = msg.payload.config.region; +memcpy (p, config + offset, size);This function can be made more general by changing the semantics of the config argument:
Quoted 5 lines
memcpy(p, config, size);Now the caller can pass just a single field instead of a whole 256-byte config buffer. It might be clearer to name the argument "data" or "region" instead of "config" though.
Good suggestion, will change it.
Quoted 35 lines
Review void vhost_ack_features(struct vhost_dev *hdev,
Quoted 2 lines
(quoted diff) +0
-0@@ -1505,6 +1508,67 @@ void vhost_ack_features(struct vhost_dev *hdev,const int
*feature_bits,Quoted 17 lines
hw/virtio/vhost.c +14
-0} } +int vhost_dev_get_config (struct vhost_dev *hdev,uint8_t *config, +uint32_t config_len) +{ +assert (hdev->vhost_ops); + +if (hdev->vhost_ops->vhost_get_config) { +return hdev->vhost_ops->vhost_get_config (hdev, config, config_len); + } + +return 0 ; +} + +int vhost_dev_set_config (struct vhost_dev *hdev,const uint8_t *config, +uint32_t offset,uint32_t size,uint32_t flags)hw/virtio/vhost.c void vhost_ack_features(struct vhost_dev *hdev, const int *feature_bits, · line 1534
Quoted 10 lines
hw/virtio/vhost.c +10
-0+{ +assert (hdev->vhost_ops); + +if (hdev->vhost_ops->vhost_set_config) { +return hdev->vhost_ops->vhost_set_config (hdev, config, offset, + size, flags); + } + +return 0 ; +}Both
vhost_dev_get_config()andvhost_dev_set_config()cannot fail silently. The device will not work properly if the configuration space feature is not supported.
Quoted 1 line
Please make these functions return an error if the callback is NULL.
Ok, will add callback check.
Quoted 5 lines
The vhost-blk code should also check that
hdev->vhost_ops->vhost_set_config!= NULL during realize. This way users see the error when adding the device instead of at runtime when the function gets called.
For vhost-blk, get_config is mandatory, but for set_config, it should depend on the feature bit: VIRTIO_BLK_F_CONFIG_WCE is enabled or not. Of course, migration should be another case. So running time error process should be okay.
Re: [Qemu-devel] [PATCH v6 1/4] vhost-user: add new vhost user messages to support virtio config space
Quoted 8 lines · Changpeng
Quoted 4 lines
The vhost-blk code should also check that
hdev->vhost_ops->vhost_set_config!= NULL during realize. This way users see the error when adding the device instead of at runtime when the function gets called.For vhost-blk,
get_configis mandatory, but forset_config, it should depend on the feature bit:VIRTIO_BLK_F_CONFIG_WCEis enabled or not. Of course, migration should be another case. So running time error process should be okay.
Okay.
Stefan
PATCH v6 2/4 vhost-user-blk: introduce a new vhost-user-blk host device
In reply to Changpeng LiuThis commit introduces a new vhost-user device for block, it uses a chardev to connect with the backend, same with Qemu virito-blk device, Guest OS still uses the virtio-blk frontend driver.
To use it, start QEMU with command line like this:
qemu-system-x86_64 \
-chardev socket,id=char0,path=/path/vhost.socket \
-device vhost-user-blk-pci,chardev=char0,num-queues=2, \
bootindex=2... \Users can use different parameters for num-queues and bootindex.
Different with exist Qemu virtio-blk host device, it makes more easy for users to implement their own I/O processing logic, such as all user space I/O stack against hardware block device. It uses the new vhost messages(VHOST_USER_GET_CONFIG) to get block virtio config information from backend process.
--- default-configs/pci.mak | 1 + hw/block/Makefile.objs | 3 + hw/block/vhost-user-blk.c | 357 +++++++++++++++++++++++++++++++++++++ hw/virtio/virtio-pci.c | 55 ++++++ hw/virtio/virtio-pci.h | 18 ++ include/hw/virtio/vhost-user-blk.h | 41 +++++ 6 files changed, 475 insertions(+) create mode 100644 hw/block/vhost-user-blk.c create mode 100644 include/hw/virtio/vhost-user-blk.h
default-configs/pci.mak +1 -0
@@ -43,3 +43,4 @@ CONFIG_VGA_PCI=y CONFIG_IVSHMEM_DEVICE=$(CONFIG_IVSHMEM) CONFIG_ROCKER=y CONFIG_VHOST_USER_SCSI=$(call land,$(CONFIG_VHOST_USER),$(CONFIG_LINUX)) +CONFIG_VHOST_USER_BLK=$(call land,$(CONFIG_VHOST_USER),$(CONFIG_LINUX))
hw/block/Makefile.objs +3 -0
@@ -13,3 +13,6 @@ obj-$(CONFIG_SH4) += tc58128.o obj-$(CONFIG_VIRTIO) += virtio-blk.o obj-$(CONFIG_VIRTIO) += dataplane/ +ifeq ($(CONFIG_VIRTIO),y) +obj-$(CONFIG_VHOST_USER_BLK) += vhost-user-blk.o +endif
hw/block/vhost-user-blk.c new file +357 -0
357 lines not on this page · raw message
hw/virtio/virtio-pci.c +55 -0
67 lines not on this page · raw message
hw/virtio/virtio-pci.h +18 -0
@@ -27,6 +27,9 @@#include "hw/virtio/virtio-gpu.h" #include "hw/virtio/virtio-crypto.h" #include "hw/virtio/vhost-user-scsi.h" +#if defined(CONFIG_VHOST_USER ) && defined(CONFIG_LINUX ) +#include "hw/virtio/vhost-user-blk.h" +#endif #ifdef CONFIG_VIRTFS #include "hw/9pfs/virtio-9p.h"
@@ -46,6 +49,7 @@ typedef struct VirtIOSerialPCI VirtIOSerialPCI;typedef struct VirtIONetPCI VirtIONetPCI ;typedef struct VHostSCSIPCI VHostSCSIPCI ;typedef struct VHostUserSCSIPCI VHostUserSCSIPCI ; +typedef struct VHostUserBlkPCI VHostUserBlkPCI ;typedef struct VirtIORngPCI VirtIORngPCI ;typedef struct VirtIOInputPCI VirtIOInputPCI ;typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI ;
@@ -244,6 +248,20 @@ struct VHostUserSCSIPCI {VHostUserSCSI vdev; }; +#if defined(CONFIG_VHOST_USER ) && defined(CONFIG_LINUX ) +/* +* vhost-user-blk-pci: This extends VirtioPCIProxy. +*/ +#define TYPE_VHOST_USER_BLK_PCI "vhost-user-blk-pci" +#define VHOST_USER_BLK_PCI (obj) \ + OBJECT_CHECK(VHostUserBlkPCI, (obj), TYPE_VHOST_USER_BLK_PCI) + +struct VHostUserBlkPCI { +VirtIOPCIProxy parent_obj; +VHostUserBlk vdev; +}; +#endif +/* * virtio-blk-pci: This extends VirtioPCIProxy. */
include/hw/virtio/vhost-user-blk.h new file +41 -0
41 lines not on this page · raw message
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v6 2/4] vhost-user-blk: introduce a new vhost-user-blk host device
Quoted 20 lines · Changpeng Liu
This commit introduces a new vhost-user device for block, it uses a chardev to connect with the backend, same with Qemu virito-blk device, Guest OS still uses the virtio-blk frontend driver.
To use it, start QEMU with command line like this:
qemu-system-x86_64 \ -chardev socket,id=char0,path=/path/vhost.socket \ -device vhost-user-blk-pci,chardev=char0,num-queues=2, \ bootindex=2... \Users can use different parameters for
num-queuesandbootindex.Different with exist Qemu virtio-blk host device, it makes more easy for users to implement their own I/O processing logic, such as all user space I/O stack against hardware block device. It uses the new vhost
messages(VHOST_USER_GET_CONFIG)to get block virtio config information from backend process.Signed-off-by Changpeng Liu <[email protected]>
Quoted 10 lines
--- default-configs/pci.mak | 1 + hw/block/Makefile.objs | 3 + hw/block/vhost-user-blk.c | 357 +++++++++++++++++++++++++++++++++++++ hw/virtio/virtio-pci.c | 55 ++++++ hw/virtio/virtio-pci.h | 18 ++ include/hw/virtio/vhost-user-blk.h | 41 +++++ 6 files changed, 475 insertions(+) create mode 100644 hw/block/vhost-user-blk.c create mode 100644 include/hw/virtio/vhost-user-blk.h
PATCH v6 3/4 contrib/libvhost-user: enable virtio config space messages
In reply to Changpeng LiuEnable VHOST_USER_GET_CONFIG/VHOST_USER_SET_CONFIG/VHOST_USER_SET_CONFIG_FD messages in libvhost-user library, users can implement their own I/O target based on the library. This enable the virtio config space delivered between Qemu host device and the I/O target, also event notifier is added in case of virtio config space changed.
--- contrib/libvhost-user/libvhost-user.c | 80 +++++++++++++++++++++++++++++++++++ contrib/libvhost-user/libvhost-user.h | 36 +++++++++++++++- 2 files changed, 115 insertions(+), 1 deletion(-)
contrib/libvhost-user/libvhost-user.c +80 -0
@@ -84,6 +84,9 @@ vu_request_to_string(unsigned int req)REQ (VHOST_USER_SET_SLAVE_REQ_FD ),REQ (VHOST_USER_IOTLB_MSG ),REQ (VHOST_USER_SET_VRING_ENDIAN ), +REQ (VHOST_USER_GET_CONFIG ), +REQ (VHOST_USER_SET_CONFIG ), +REQ (VHOST_USER_SET_CONFIG_FD ), REQ(VHOST_USER_MAX), };#undef REQ
@@ -798,6 +801,70 @@ vu_set_slave_req_fd(VuDev *dev, VhostUserMsg *vmsg) }static bool +vu_get_config (VuDev *dev,VhostUserMsg *vmsg) +{ +int ret =-1 ; + +if (dev->iface->get_config) { + ret = dev->iface->get_config (dev, vmsg->payload.config.region, + vmsg->payload.config.size); + } + +if (ret) { +/* resize to zero to indicate an error to master */ + vmsg->size =0 ; + } + +return true; +} + +static bool +vu_set_config (VuDev *dev,VhostUserMsg *vmsg) +{ +int ret =-1 ; +bool reply_supported = !!(dev->protocol_features & + (1ULL <<VHOST_USER_PROTOCOL_F_REPLY_ACK )); + +if (dev->iface->set_config) { + ret = dev->iface->set_config (dev, vmsg->payload.config.region, + vmsg->payload.config.offset, + vmsg->payload.config.size, + vmsg->payload.config.flags); + } + + vmsg->size =sizeof (vmsg->payload.u64); +if (!ret) { + vmsg->payload.u64 =0 ; + }else { +/* indicate an error in case reply supported */ + vmsg->payload.u64 =1 ; + } + +if (reply_supported) { +return true; + } + +return false; +} + +static bool +vu_set_config_fd (VuDev *dev,VhostUserMsg *vmsg) +{ +if (vmsg->fd_num !=1 ) { +vu_panic (dev,"Invalid config_fd message" ); +return false; + } + +if (dev->config_fd !=-1 ) { +close (dev->config_fd); + } + dev->config_fd = vmsg->fds[0 ]; +DPRINT ("Got config_fd: %d\n" , vmsg->fds[0 ]); + +return false; +} + +static bool vu_process_message (VuDev *dev,VhostUserMsg *vmsg) {int do_reply =0 ;
@@ -862,6 +929,12 @@ vu_process_message(VuDev *dev, VhostUserMsg *vmsg)return vu_set_vring_enable_exec (dev, vmsg);case VHOST_USER_SET_SLAVE_REQ_FD :return vu_set_slave_req_fd (dev, vmsg); +case VHOST_USER_GET_CONFIG : +return vu_get_config (dev, vmsg); +case VHOST_USER_SET_CONFIG : +return vu_set_config (dev, vmsg); +case VHOST_USER_SET_CONFIG_FD : +return vu_set_config_fd (dev, vmsg);case VHOST_USER_NONE :break ;default :
@@ -940,6 +1013,11 @@ vu_deinit(VuDev *dev) dev->slave_fd =-1 ; } +if (dev->config_fd !=-1 ) { +close (dev->config_fd); + dev->config_fd =-1 ; + } +if (dev->sock !=-1 ) {close (dev->sock); }
@@ -970,6 +1048,8 @@ vu_init(VuDev *dev, dev->iface = iface; dev->log_call_fd =-1 ; dev->slave_fd =-1 ; + dev->config_fd =-1 ; +for (i =0 ; i <VHOST_MAX_NR_VIRTQUEUE ; i++) { dev->vq[i] = (VuVirtq ) { .call_fd =-1 , .kick_fd =-1 , .err_fd =-1 ,
contrib/libvhost-user/libvhost-user.h +35 -1
@@ -30,6 +30,16 @@#define VHOST_MEMORY_MAX_NREGIONS 8 +typedef enum VhostSetConfigType { +VHOST_SET_CONFIG_TYPE_MASTER =0 , +VHOST_SET_CONFIG_TYPE_MIGRATION =1 , +}VhostSetConfigType ; + +/* +* Maximum size of virtio device config space +*/ +#define VHOST_USER_MAX_CONFIG_SIZE 256 +enum VhostUserProtocolFeature {VHOST_USER_PROTOCOL_F_MQ =0 ,VHOST_USER_PROTOCOL_F_LOG_SHMFD =1 ,
@@ -38,7 +48,6 @@ enum VhostUserProtocolFeature {VHOST_USER_PROTOCOL_F_NET_MTU =4 ,VHOST_USER_PROTOCOL_F_SLAVE_REQ =5 ,VHOST_USER_PROTOCOL_F_CROSS_ENDIAN =6 , - VHOST_USER_PROTOCOL_F_MAX };
@@ -69,6 +78,9 @@ typedef enum VhostUserRequest {VHOST_USER_SET_SLAVE_REQ_FD =21 ,VHOST_USER_IOTLB_MSG =22 ,VHOST_USER_SET_VRING_ENDIAN =23 , +VHOST_USER_GET_CONFIG =24 , +VHOST_USER_SET_CONFIG =25 , +VHOST_USER_SET_CONFIG_FD =26 , VHOST_USER_MAX } VhostUserRequest;
@@ -90,6 +102,18 @@ typedef struct VhostUserLog {uint64_t mmap_offset; } VhostUserLog; +typedef struct VhostUserConfig { +uint32_t offset; +uint32_t size; +uint32_t flags; +uint8_t region[VHOST_USER_MAX_CONFIG_SIZE ]; +}VhostUserConfig ; + +static VhostUserConfig c __attribute__ ((unused)); +#define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \ + + sizeof(c.size) \ + + sizeof(c.flags)) +#if defined(_WIN32)# define VU_PACKED __attribute__((gcc_struct, packed))#else
@@ -112,6 +136,7 @@ typedef struct VhostUserMsg {struct vhost_vring_addr addr;VhostUserMemory memory;VhostUserLog log; +VhostUserConfig config; } payload;int fds[VHOST_MEMORY_MAX_NREGIONS ];
@@ -140,6 +165,10 @@ typedef int (*vu_process_msg_cb) (VuDev *dev, VhostUserMsg *vmsg, int *do_reply);typedef void (*vu_queue_set_started_cb ) (VuDev *dev,int qidx,bool started);typedef bool (*vu_queue_is_processed_in_order_cb ) (VuDev *dev,int qidx); +typedef int (*vu_get_config_cb ) (VuDev *dev,uint8_t *config,uint32_t len); +typedef int (*vu_set_config_cb ) (VuDev *dev,const uint8_t *config, +uint32_t offset,uint32_t size, +uint32_t flags); typedef struct VuDevIface { /* called by VHOST_USER_GET_FEATURES to get the features bitmask */
@@ -162,6 +191,10 @@ typedef struct VuDevIface {* on unmanaged exit/crash. */ vu_queue_is_processed_in_order_cb queue_is_processed_in_order; +/* get the config space of the device */ +vu_get_config_cb get_config; +/* set the config space of the device */ +vu_set_config_cb set_config; } VuDevIface;typedef void (*vu_queue_handler_cb ) (VuDev *dev,int qidx);
@@ -227,6 +260,7 @@ struct VuDev {VuVirtq vq[VHOST_MAX_NR_VIRTQUEUE ];int log_call_fd;int slave_fd; +int config_fd;uint64_t log_size;uint8_t *log_table;uint64_t features;
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v6 3/4] contrib/libvhost-user: enable virtio config space messages
Quoted 7 lines · Changpeng Liu
contrib/libvhost-user/libvhost-user.c +3
-0@@ -798,6 +801,70 @@ vu_set_slave_req_fd(VuDev *dev, VhostUserMsg *vmsg) }static bool +vu_get_config (VuDev *dev,VhostUserMsg *vmsg) +{ +int ret =-1 ;
contrib/libvhost-user/libvhost-user.c vu_set_slave_req_fd(VuDev *dev, VhostUserMsg *vmsg) · line 816
Quoted 10 lines
contrib/libvhost-user/libvhost-user.c +10
-0+ +if (dev->iface->get_config) { + ret = dev->iface->get_config (dev, vmsg->payload.config.region, + vmsg->payload.config.size); + } + +if (ret) { +/* resize to zero to indicate an error to master */ + vmsg->size =0 ; + }
Please document this error case in vhost-user.txt. I don't remember reading about it.
Re: [Qemu-devel] [PATCH v6 3/4] contrib/libvhost-user: enable virtio config space messages
Quoted 26 lines
-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Monday, December 11, 2017 10:00 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Harris, James R <[email protected]>
Subject: Re: [PATCH v6 3/4] contrib/libvhost-user: enable virtio config space messagesReview vu_set_slave_req_fd(VuDev *dev, VhostUserMsg
Quoted 1 line · Changpeng Liu
(quoted diff) +0
-0@@ -798,6 +801,70 @@ vu_set_slave_req_fd(VuDev *dev, VhostUserMsg*vmsg)
Quoted 11 lines
contrib/libvhost-user/libvhost-user.c +8
-0}static bool +vu_get_config (VuDev *dev,VhostUserMsg *vmsg) +{ +int ret =-1 ; + +if (dev->iface->get_config) { + ret = dev->iface->get_config (dev, vmsg->payload.config.region, + vmsg->payload.config.size); + }
Quoted 8 lines
contrib/libvhost-user/libvhost-user.c vu_set_slave_req_fd(VuDev *dev, VhostUserMsg *vmsg) · line 816
Quoted 5 lines
contrib/libvhost-user/libvhost-user.c +5
-0+ +if (ret) { +/* resize to zero to indicate an error to master */ + vmsg->size =0 ; + }Please document this error case in vhost-user.txt. I don't remember reading about it.
Thanks, will add it to vhost-user.txt.
PATCH v6 4/4 contrib/vhost-user-blk: introduce a vhost-user-blk sample application
In reply to Changpeng LiuThis commit introcudes a vhost-user-blk backend device, it uses UNIX domain socket to communicate with QEMU. The vhost-user-blk sample application should be used with QEMU vhost-user-blk-pci device.
To use it, complie with:
make vhost-user-blk
and start like this:
vhost-user-blk -b /dev/sdb -s /path/vhost.socket
--- .gitignore | 1 + Makefile | 3 + Makefile.objs | 1 + contrib/vhost-user-blk/Makefile.objs | 1 + contrib/vhost-user-blk/vhost-user-blk.c | 556 ++++++++++++++++++++++++++++++++ 5 files changed, 562 insertions(+) create mode 100644 contrib/vhost-user-blk/Makefile.objs create mode 100644 contrib/vhost-user-blk/vhost-user-blk.c
.gitignore +1 -0
@@ -55,6 +55,7 @@ /scsi/qemu-pr-helper /vscclient /vhost-user-scsi +/vhost-user-blk /fsdev/virtfs-proxy-helper *.tmp *.[1-9]
Makefile +3 -0
@@ -327,6 +327,7 @@ dummy := $(call unnest-vars,, \ ivshmem-server-obj-y \ libvhost-user-obj-y \ vhost-user-scsi-obj-y \ + vhost-user-blk-obj-y \ qga-vss-dll-obj-y \ block-obj-y \ block-obj-m \
@@ -558,6 +559,8 @@ ivshmem-server$(EXESUF): $(ivshmem-server-obj-y) $(COMMON_LDADDS) endif vhost-user-scsi$(EXESUF): $(vhost-user-scsi-obj-y) libvhost-user.a $(call LINK, $^) +vhost-user-blk$(EXESUF): $(vhost-user-blk-obj-y) libvhost-user.a + $(call LINK, $^) module_block.h: $(SRC_PATH)/scripts/modules/module_block.py config-host.mak $(call quiet-command,$(PYTHON) $< $@ \
Makefile.objs +1 -0
@@ -115,6 +115,7 @@ libvhost-user-obj-y = contrib/libvhost-user/ vhost-user-scsi.o-cflags := $(LIBISCSI_CFLAGS) vhost-user-scsi.o-libs := $(LIBISCSI_LIBS) vhost-user-scsi-obj-y = contrib/vhost-user-scsi/ +vhost-user-blk-obj-y = contrib/vhost-user-blk/ ###################################################################### trace-events-subdirs =
contrib/vhost-user-blk/Makefile.objs new file +1 -0
@@ -0,0 +1 @@ +vhost-user-blk-obj-y = vhost-user-blk.o
contrib/vhost-user-blk/vhost-user-blk.c new file +556 -0
556 lines not on this page · raw message
Signature · 1 line
1.9.3
Re: [Qemu-devel] [PATCH v6 4/4] contrib/vhost-user-blk: introduce a vhost-user-blk sample application
Quoted 26 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +26
-0+static int vub_virtio_process_req (VubDev *vdev_blk, +VuVirtq *vq) +{ +VugDev *gdev = &vdev_blk->parent; +VuDev *vu_dev = &gdev->parent; +VuVirtqElement *elem; +uint32_t type; +unsigned in_num; +unsigned out_num; +VubReq *req; + + elem =vu_queue_pop (vu_dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +return -1 ; + } + +/* refer to hw/block/virtio_blk.c */ +if (elem->out_num <1 || elem->in_num <1 ) { +fprintf (stderr,"virtio-blk request missing headers\n" ); +free (elem); +return -1 ; + } + + req =g_new0 (VubReq,1 ); + req->vdev_blk = vdev_blk; + req->vq = vq;
contrib/vhost-user-blk/vhost-user-blk.c line 210
Quoted 10 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +10
-0+ req->elem = elem; + + in_num = elem->in_num; + out_num = elem->out_num; + +/* don't support VIRTIO_F_ANY_LAYOUT and virtio 1.0 only */ +if (elem->out_sg[0 ].iov_len <sizeof (struct virtio_blk_outhdr )) { +fprintf (stderr,"Invalid outhdr size\n" ); + goto err; + }
QEMU has iov_discard_front() and iov_discard_back(). They make it pretty easy to support VIRTIO_F_ANY_LAYOUT. If you have time, please consider adding it to libvhost-user, but it's not a requirement for this patch series.
contrib/vhost-user-blk/vhost-user-blk.c line 221
Quoted 11 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +11
-0+ req->out = (struct virtio_blk_outhdr *)elem->out_sg[0 ].iov_base; + out_num--; + +if (elem->in_sg[in_num -1 ].iov_len <sizeof (struct virtio_blk_inhdr )) { +fprintf (stderr,"Invalid inhdr size\n" ); + goto err; + } + req->in = (struct virtio_blk_inhdr *)elem->in_sg[in_num -1 ].iov_base; + in_num--; + + type =le32toh (req->out->type);
Endianness is more complicated if you want to support both VIRTIO 1.0 and Legacy (0.9.7). I guess it's okay to make libvhost-user code only support VIRTIO 1.0.
contrib/vhost-user-blk/vhost-user-blk.c line 308
Quoted 11 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +11
-0+static void vub_queue_set_started (VuDev *vu_dev,int idx,bool started) +{ +VuVirtq *vq; + +assert (vu_dev); + +if ((idx <0 ) || (idx >=VHOST_MAX_NR_VIRTQUEUE )) { +fprintf (stderr,"VQ Index out of range: %d\n" , idx); +vub_panic_cb (vu_dev,NULL ); +return ; + }
Is it necessary to check num_queues? The vhost-user master should not be able to enable idx >= num_queues.
Re: [Qemu-devel] [PATCH v6 4/4] contrib/vhost-user-blk: introduce a vhost-user-blk sample application
Quoted 69 lines
-----Original Message-----
From: Stefan Hajnoczi [mailto:[email protected]]
Sent: Monday, December 11, 2017 10:33 PM
To: Liu, Changpeng <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Harris, James R <[email protected]>
Subject: Re: [PATCH v6 4/4] contrib/vhost-user-blk: introduce a vhost-user-blk sample applicationQuoted 26 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +26
-0+static int vub_virtio_process_req (VubDev *vdev_blk, +VuVirtq *vq) +{ +VugDev *gdev = &vdev_blk->parent; +VuDev *vu_dev = &gdev->parent; +VuVirtqElement *elem; +uint32_t type; +unsigned in_num; +unsigned out_num; +VubReq *req; + + elem =vu_queue_pop (vu_dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +return -1 ; + } + +/* refer to hw/block/virtio_blk.c */ +if (elem->out_num <1 || elem->in_num <1 ) { +fprintf (stderr,"virtio-blk request missing headers\n" ); +free (elem); +return -1 ; + } + + req =g_new0 (VubReq,1 ); + req->vdev_blk = vdev_blk; + req->vq = vq;contrib/vhost-user-blk/vhost-user-blk.c line 210
Quoted 10 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +10
-0+ req->elem = elem; + + in_num = elem->in_num; + out_num = elem->out_num; + +/* don't support VIRTIO_F_ANY_LAYOUT and virtio 1.0 only */ +if (elem->out_sg[0 ].iov_len <sizeof (struct virtio_blk_outhdr )) { +fprintf (stderr,"Invalid outhdr size\n" ); + goto err; + }QEMU has
iov_discard_front()andiov_discard_back(). They make it pretty easy to supportVIRTIO_F_ANY_LAYOUT. If you have time, please consider adding it to libvhost-user, but it's not a requirement for this patch series.contrib/vhost-user-blk/vhost-user-blk.c line 221
Quoted 11 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +11
-0+ req->out = (struct virtio_blk_outhdr *)elem->out_sg[0 ].iov_base; + out_num--; + +if (elem->in_sg[in_num -1 ].iov_len <sizeof (struct virtio_blk_inhdr )) { +fprintf (stderr,"Invalid inhdr size\n" ); + goto err; + } + req->in = (struct virtio_blk_inhdr *)elem->in_sg[in_num -1 ].iov_base; + in_num--; + + type =le32toh (req->out->type);Endianness is more complicated if you want to support both VIRTIO 1.0 and Legacy (0.9.7). I guess it's okay to make libvhost-user code only support VIRTIO 1.0.
Quoted 14 lines
contrib/vhost-user-blk/vhost-user-blk.c line 308
Quoted 11 lines
contrib/vhost-user-blk/vhost-user-blk.c new file +11
-0+static void vub_queue_set_started (VuDev *vu_dev,int idx,bool started) +{ +VuVirtq *vq; + +assert (vu_dev); + +if ((idx <0 ) || (idx >=VHOST_MAX_NR_VIRTQUEUE )) { +fprintf (stderr,"VQ Index out of range: %d\n" , idx); +vub_panic_cb (vu_dev,NULL ); +return ; + }Is it necessary to check
num_queues? The vhost-user master should not be able to enable idx >=num_queues.
Yes, this part of code can be removed.