2 * vhost-user-blk sample application
4 * Copyright (c) 2017 Intel Corporation. All rights reserved.
7 * Changpeng Liu <changpeng.liu@intel.com>
9 * This work is based on the "vhost-user-scsi" sample and "virtio-blk" driver
11 * Felipe Franciosi <felipe@nutanix.com>
12 * Anthony Liguori <aliguori@us.ibm.com>
14 * This work is licensed under the terms of the GNU GPL, version 2 only.
15 * See the COPYING file in the top-level directory.
18 #include "qemu/osdep.h"
19 #include "standard-headers/linux/virtio_blk.h"
20 #include "contrib/libvhost-user/libvhost-user-glib.h"
21 #include "contrib/libvhost-user/libvhost-user.h"
23 #if defined(__linux__)
25 #include <sys/ioctl.h>
29 VHOST_USER_BLK_MAX_QUEUES
= 8,
32 struct virtio_blk_inhdr
{
36 /* vhost user block device */
37 typedef struct VubDev
{
40 struct virtio_blk_config blkcfg
;
46 typedef struct VubReq
{
50 struct virtio_blk_inhdr
*in
;
51 struct virtio_blk_outhdr
*out
;
56 /* refer util/iov.c */
57 static size_t vub_iov_size(const struct iovec
*iov
,
58 const unsigned int iov_cnt
)
64 for (i
= 0; i
< iov_cnt
; i
++) {
65 len
+= iov
[i
].iov_len
;
70 static size_t vub_iov_to_buf(const struct iovec
*iov
,
71 const unsigned int iov_cnt
, void *buf
)
77 for (i
= 0; i
< iov_cnt
; i
++) {
78 memcpy(buf
+ len
, iov
[i
].iov_base
, iov
[i
].iov_len
);
79 len
+= iov
[i
].iov_len
;
84 static void vub_panic_cb(VuDev
*vu_dev
, const char *buf
)
91 gdev
= container_of(vu_dev
, VugDev
, parent
);
92 vdev_blk
= container_of(gdev
, VubDev
, parent
);
94 g_warning("vu_panic: %s", buf
);
97 g_main_loop_quit(vdev_blk
->loop
);
100 static void vub_req_complete(VubReq
*req
)
102 VugDev
*gdev
= &req
->vdev_blk
->parent
;
103 VuDev
*vu_dev
= &gdev
->parent
;
105 /* IO size with 1 extra status byte */
106 vu_queue_push(vu_dev
, req
->vq
, req
->elem
,
108 vu_queue_notify(vu_dev
, req
->vq
);
117 static int vub_open(const char *file_name
, bool wce
)
126 fd
= open(file_name
, flags
);
128 fprintf(stderr
, "Cannot open file %s, %s\n", file_name
,
137 vub_readv(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
139 VubDev
*vdev_blk
= req
->vdev_blk
;
143 fprintf(stderr
, "Invalid Read IOV count\n");
147 req
->size
= vub_iov_size(iov
, iovcnt
);
148 rc
= preadv(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
150 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %lu failed with %s\n",
151 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
160 vub_writev(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
162 VubDev
*vdev_blk
= req
->vdev_blk
;
166 fprintf(stderr
, "Invalid Write IOV count\n");
170 req
->size
= vub_iov_size(iov
, iovcnt
);
171 rc
= pwritev(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
173 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %lu failed with %s\n",
174 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
183 vub_discard_write_zeroes(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
,
186 struct virtio_blk_discard_write_zeroes
*desc
;
190 size
= vub_iov_size(iov
, iovcnt
);
191 if (size
!= sizeof(*desc
)) {
192 fprintf(stderr
, "Invalid size %ld, expect %ld\n", size
, sizeof(*desc
));
195 buf
= g_new0(char, size
);
196 vub_iov_to_buf(iov
, iovcnt
, buf
);
198 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
199 VubDev
*vdev_blk
= req
->vdev_blk
;
200 desc
= (struct virtio_blk_discard_write_zeroes
*)buf
;
201 uint64_t range
[2] = { le64toh(desc
->sector
) << 9,
202 le32toh(desc
->num_sectors
) << 9 };
203 if (type
== VIRTIO_BLK_T_DISCARD
) {
204 if (ioctl(vdev_blk
->blk_fd
, BLKDISCARD
, range
) == 0) {
208 } else if (type
== VIRTIO_BLK_T_WRITE_ZEROES
) {
209 if (ioctl(vdev_blk
->blk_fd
, BLKZEROOUT
, range
) == 0) {
221 vub_flush(VubReq
*req
)
223 VubDev
*vdev_blk
= req
->vdev_blk
;
225 fdatasync(vdev_blk
->blk_fd
);
228 static int vub_virtio_process_req(VubDev
*vdev_blk
,
231 VugDev
*gdev
= &vdev_blk
->parent
;
232 VuDev
*vu_dev
= &gdev
->parent
;
233 VuVirtqElement
*elem
;
239 elem
= vu_queue_pop(vu_dev
, vq
, sizeof(VuVirtqElement
) + sizeof(VubReq
));
244 /* refer to hw/block/virtio_blk.c */
245 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
246 fprintf(stderr
, "virtio-blk request missing headers\n");
251 req
= g_new0(VubReq
, 1);
252 req
->vdev_blk
= vdev_blk
;
256 in_num
= elem
->in_num
;
257 out_num
= elem
->out_num
;
259 /* don't support VIRTIO_F_ANY_LAYOUT and virtio 1.0 only */
260 if (elem
->out_sg
[0].iov_len
< sizeof(struct virtio_blk_outhdr
)) {
261 fprintf(stderr
, "Invalid outhdr size\n");
264 req
->out
= (struct virtio_blk_outhdr
*)elem
->out_sg
[0].iov_base
;
267 if (elem
->in_sg
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
268 fprintf(stderr
, "Invalid inhdr size\n");
271 req
->in
= (struct virtio_blk_inhdr
*)elem
->in_sg
[in_num
- 1].iov_base
;
274 type
= le32toh(req
->out
->type
);
275 switch (type
& ~VIRTIO_BLK_T_BARRIER
) {
276 case VIRTIO_BLK_T_IN
:
277 case VIRTIO_BLK_T_OUT
: {
279 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
280 req
->sector_num
= le64toh(req
->out
->sector
);
282 ret
= vub_writev(req
, &elem
->out_sg
[1], out_num
);
284 ret
= vub_readv(req
, &elem
->in_sg
[0], in_num
);
287 req
->in
->status
= VIRTIO_BLK_S_OK
;
289 req
->in
->status
= VIRTIO_BLK_S_IOERR
;
291 vub_req_complete(req
);
294 case VIRTIO_BLK_T_FLUSH
:
296 req
->in
->status
= VIRTIO_BLK_S_OK
;
297 vub_req_complete(req
);
299 case VIRTIO_BLK_T_GET_ID
: {
300 size_t size
= MIN(vub_iov_size(&elem
->in_sg
[0], in_num
),
301 VIRTIO_BLK_ID_BYTES
);
302 snprintf(elem
->in_sg
[0].iov_base
, size
, "%s", "vhost_user_blk");
303 req
->in
->status
= VIRTIO_BLK_S_OK
;
304 req
->size
= elem
->in_sg
[0].iov_len
;
305 vub_req_complete(req
);
308 case VIRTIO_BLK_T_DISCARD
:
309 case VIRTIO_BLK_T_WRITE_ZEROES
: {
311 rc
= vub_discard_write_zeroes(req
, &elem
->out_sg
[1], out_num
, type
);
313 req
->in
->status
= VIRTIO_BLK_S_OK
;
315 req
->in
->status
= VIRTIO_BLK_S_IOERR
;
317 vub_req_complete(req
);
321 req
->in
->status
= VIRTIO_BLK_S_UNSUPP
;
322 vub_req_complete(req
);
334 static void vub_process_vq(VuDev
*vu_dev
, int idx
)
341 gdev
= container_of(vu_dev
, VugDev
, parent
);
342 vdev_blk
= container_of(gdev
, VubDev
, parent
);
345 vq
= vu_get_queue(vu_dev
, idx
);
349 ret
= vub_virtio_process_req(vdev_blk
, vq
);
356 static void vub_queue_set_started(VuDev
*vu_dev
, int idx
, bool started
)
362 vq
= vu_get_queue(vu_dev
, idx
);
363 vu_set_queue_handler(vu_dev
, vq
, started
? vub_process_vq
: NULL
);
367 vub_get_features(VuDev
*dev
)
373 gdev
= container_of(dev
, VugDev
, parent
);
374 vdev_blk
= container_of(gdev
, VubDev
, parent
);
376 features
= 1ull << VIRTIO_BLK_F_SIZE_MAX
|
377 1ull << VIRTIO_BLK_F_SEG_MAX
|
378 1ull << VIRTIO_BLK_F_TOPOLOGY
|
379 1ull << VIRTIO_BLK_F_BLK_SIZE
|
380 1ull << VIRTIO_BLK_F_FLUSH
|
381 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
382 1ull << VIRTIO_BLK_F_DISCARD
|
383 1ull << VIRTIO_BLK_F_WRITE_ZEROES
|
385 1ull << VIRTIO_BLK_F_CONFIG_WCE
|
386 1ull << VIRTIO_F_VERSION_1
|
387 1ull << VHOST_USER_F_PROTOCOL_FEATURES
;
389 if (vdev_blk
->enable_ro
) {
390 features
|= 1ull << VIRTIO_BLK_F_RO
;
397 vub_get_protocol_features(VuDev
*dev
)
399 return 1ull << VHOST_USER_PROTOCOL_F_CONFIG
|
400 1ull << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
;
404 vub_get_config(VuDev
*vu_dev
, uint8_t *config
, uint32_t len
)
409 gdev
= container_of(vu_dev
, VugDev
, parent
);
410 vdev_blk
= container_of(gdev
, VubDev
, parent
);
411 memcpy(config
, &vdev_blk
->blkcfg
, len
);
417 vub_set_config(VuDev
*vu_dev
, const uint8_t *data
,
418 uint32_t offset
, uint32_t size
, uint32_t flags
)
425 /* don't support live migration */
426 if (flags
!= VHOST_SET_CONFIG_TYPE_MASTER
) {
430 gdev
= container_of(vu_dev
, VugDev
, parent
);
431 vdev_blk
= container_of(gdev
, VubDev
, parent
);
433 if (offset
!= offsetof(struct virtio_blk_config
, wce
) ||
439 if (wce
== vdev_blk
->blkcfg
.wce
) {
440 /* Do nothing as same with old configuration */
444 vdev_blk
->blkcfg
.wce
= wce
;
445 fprintf(stdout
, "Write Cache Policy Changed\n");
446 if (vdev_blk
->blk_fd
>= 0) {
447 close(vdev_blk
->blk_fd
);
448 vdev_blk
->blk_fd
= -1;
451 fd
= vub_open(vdev_blk
->blk_name
, wce
);
453 fprintf(stderr
, "Error to open block device %s\n", vdev_blk
->blk_name
);
454 vdev_blk
->blk_fd
= -1;
457 vdev_blk
->blk_fd
= fd
;
462 static const VuDevIface vub_iface
= {
463 .get_features
= vub_get_features
,
464 .queue_set_started
= vub_queue_set_started
,
465 .get_protocol_features
= vub_get_protocol_features
,
466 .get_config
= vub_get_config
,
467 .set_config
= vub_set_config
,
470 static int unix_sock_new(char *unix_fn
)
473 struct sockaddr_un un
;
478 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
484 un
.sun_family
= AF_UNIX
;
485 (void)snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", unix_fn
);
486 len
= sizeof(un
.sun_family
) + strlen(un
.sun_path
);
488 (void)unlink(unix_fn
);
489 if (bind(sock
, (struct sockaddr
*)&un
, len
) < 0) {
494 if (listen(sock
, 1) < 0) {
507 static void vub_free(struct VubDev
*vdev_blk
)
513 g_main_loop_unref(vdev_blk
->loop
);
514 if (vdev_blk
->blk_fd
>= 0) {
515 close(vdev_blk
->blk_fd
);
521 vub_get_blocksize(int fd
)
523 uint32_t blocksize
= 512;
525 #if defined(__linux__) && defined(BLKSSZGET)
526 if (ioctl(fd
, BLKSSZGET
, &blocksize
) == 0) {
535 vub_initialize_config(int fd
, struct virtio_blk_config
*config
)
539 capacity
= lseek64(fd
, 0, SEEK_END
);
540 config
->capacity
= capacity
>> 9;
541 config
->blk_size
= vub_get_blocksize(fd
);
542 config
->size_max
= 65536;
543 config
->seg_max
= 128 - 2;
544 config
->min_io_size
= 1;
545 config
->opt_io_size
= 1;
546 config
->num_queues
= 1;
547 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
548 config
->max_discard_sectors
= 32768;
549 config
->max_discard_seg
= 1;
550 config
->discard_sector_alignment
= config
->blk_size
>> 9;
551 config
->max_write_zeroes_sectors
= 32768;
552 config
->max_write_zeroes_seg
= 1;
557 vub_new(char *blk_file
)
561 vdev_blk
= g_new0(VubDev
, 1);
562 vdev_blk
->loop
= g_main_loop_new(NULL
, FALSE
);
563 vdev_blk
->blk_fd
= vub_open(blk_file
, 0);
564 if (vdev_blk
->blk_fd
< 0) {
565 fprintf(stderr
, "Error to open block device %s\n", blk_file
);
569 vdev_blk
->enable_ro
= false;
570 vdev_blk
->blkcfg
.wce
= 0;
571 vdev_blk
->blk_name
= blk_file
;
573 /* fill virtio_blk_config with block parameters */
574 vub_initialize_config(vdev_blk
->blk_fd
, &vdev_blk
->blkcfg
);
579 int main(int argc
, char **argv
)
582 char *unix_socket
= NULL
;
583 char *blk_file
= NULL
;
584 bool enable_ro
= false;
585 int lsock
= -1, csock
= -1;
586 VubDev
*vdev_blk
= NULL
;
588 while ((opt
= getopt(argc
, argv
, "b:rs:h")) != -1) {
591 blk_file
= g_strdup(optarg
);
594 unix_socket
= g_strdup(optarg
);
601 printf("Usage: %s [ -b block device or file, -s UNIX domain socket"
602 " | -r Enable read-only ] | [ -h ]\n", argv
[0]);
607 if (!unix_socket
|| !blk_file
) {
608 printf("Usage: %s [ -b block device or file, -s UNIX domain socket"
609 " | -r Enable read-only ] | [ -h ]\n", argv
[0]);
613 lsock
= unix_sock_new(unix_socket
);
618 csock
= accept(lsock
, (void *)0, (void *)0);
620 fprintf(stderr
, "Accept error %s\n", strerror(errno
));
624 vdev_blk
= vub_new(blk_file
);
629 vdev_blk
->enable_ro
= true;
632 if (!vug_init(&vdev_blk
->parent
, VHOST_USER_BLK_MAX_QUEUES
, csock
,
633 vub_panic_cb
, &vub_iface
)) {
634 fprintf(stderr
, "Failed to initialized libvhost-user-glib\n");
638 g_main_loop_run(vdev_blk
->loop
);
640 vug_deinit(&vdev_blk
->parent
);