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 "libvhost-user-glib.h"
22 #if defined(__linux__)
24 #include <sys/ioctl.h>
28 VHOST_USER_BLK_MAX_QUEUES
= 8,
31 struct virtio_blk_inhdr
{
35 /* vhost user block device */
36 typedef struct VubDev
{
39 struct virtio_blk_config blkcfg
;
45 typedef struct VubReq
{
49 struct virtio_blk_inhdr
*in
;
50 struct virtio_blk_outhdr
*out
;
55 /* refer util/iov.c */
56 static size_t vub_iov_size(const struct iovec
*iov
,
57 const unsigned int iov_cnt
)
63 for (i
= 0; i
< iov_cnt
; i
++) {
64 len
+= iov
[i
].iov_len
;
69 static size_t vub_iov_to_buf(const struct iovec
*iov
,
70 const unsigned int iov_cnt
, void *buf
)
76 for (i
= 0; i
< iov_cnt
; i
++) {
77 memcpy(buf
+ len
, iov
[i
].iov_base
, iov
[i
].iov_len
);
78 len
+= iov
[i
].iov_len
;
83 static void vub_panic_cb(VuDev
*vu_dev
, const char *buf
)
90 gdev
= container_of(vu_dev
, VugDev
, parent
);
91 vdev_blk
= container_of(gdev
, VubDev
, parent
);
93 g_warning("vu_panic: %s", buf
);
96 g_main_loop_quit(vdev_blk
->loop
);
99 static void vub_req_complete(VubReq
*req
)
101 VugDev
*gdev
= &req
->vdev_blk
->parent
;
102 VuDev
*vu_dev
= &gdev
->parent
;
104 /* IO size with 1 extra status byte */
105 vu_queue_push(vu_dev
, req
->vq
, req
->elem
,
107 vu_queue_notify(vu_dev
, req
->vq
);
113 static int vub_open(const char *file_name
, bool wce
)
122 fd
= open(file_name
, flags
);
124 fprintf(stderr
, "Cannot open file %s, %s\n", file_name
,
133 vub_readv(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
135 VubDev
*vdev_blk
= req
->vdev_blk
;
139 fprintf(stderr
, "Invalid Read IOV count\n");
143 req
->size
= vub_iov_size(iov
, iovcnt
);
144 rc
= preadv(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
146 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %zu failed with %s\n",
147 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
156 vub_writev(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
158 VubDev
*vdev_blk
= req
->vdev_blk
;
162 fprintf(stderr
, "Invalid Write IOV count\n");
166 req
->size
= vub_iov_size(iov
, iovcnt
);
167 rc
= pwritev(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
169 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %zu failed with %s\n",
170 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
179 vub_discard_write_zeroes(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
,
182 struct virtio_blk_discard_write_zeroes
*desc
;
186 size
= vub_iov_size(iov
, iovcnt
);
187 if (size
!= sizeof(*desc
)) {
188 fprintf(stderr
, "Invalid size %zd, expect %zd\n", size
, sizeof(*desc
));
191 buf
= g_new0(char, size
);
192 vub_iov_to_buf(iov
, iovcnt
, buf
);
194 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
195 VubDev
*vdev_blk
= req
->vdev_blk
;
196 desc
= (struct virtio_blk_discard_write_zeroes
*)buf
;
197 uint64_t range
[2] = { le64toh(desc
->sector
) << 9,
198 le32toh(desc
->num_sectors
) << 9 };
199 if (type
== VIRTIO_BLK_T_DISCARD
) {
200 if (ioctl(vdev_blk
->blk_fd
, BLKDISCARD
, range
) == 0) {
204 } else if (type
== VIRTIO_BLK_T_WRITE_ZEROES
) {
205 if (ioctl(vdev_blk
->blk_fd
, BLKZEROOUT
, range
) == 0) {
217 vub_flush(VubReq
*req
)
219 VubDev
*vdev_blk
= req
->vdev_blk
;
221 fdatasync(vdev_blk
->blk_fd
);
224 static int vub_virtio_process_req(VubDev
*vdev_blk
,
227 VugDev
*gdev
= &vdev_blk
->parent
;
228 VuDev
*vu_dev
= &gdev
->parent
;
229 VuVirtqElement
*elem
;
235 elem
= vu_queue_pop(vu_dev
, vq
, sizeof(VuVirtqElement
) + sizeof(VubReq
));
240 /* refer to hw/block/virtio_blk.c */
241 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
242 fprintf(stderr
, "virtio-blk request missing headers\n");
247 req
= g_new0(VubReq
, 1);
248 req
->vdev_blk
= vdev_blk
;
252 in_num
= elem
->in_num
;
253 out_num
= elem
->out_num
;
255 /* don't support VIRTIO_F_ANY_LAYOUT and virtio 1.0 only */
256 if (elem
->out_sg
[0].iov_len
< sizeof(struct virtio_blk_outhdr
)) {
257 fprintf(stderr
, "Invalid outhdr size\n");
260 req
->out
= (struct virtio_blk_outhdr
*)elem
->out_sg
[0].iov_base
;
263 if (elem
->in_sg
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
264 fprintf(stderr
, "Invalid inhdr size\n");
267 req
->in
= (struct virtio_blk_inhdr
*)elem
->in_sg
[in_num
- 1].iov_base
;
270 type
= le32toh(req
->out
->type
);
271 switch (type
& ~VIRTIO_BLK_T_BARRIER
) {
272 case VIRTIO_BLK_T_IN
:
273 case VIRTIO_BLK_T_OUT
: {
275 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
276 req
->sector_num
= le64toh(req
->out
->sector
);
278 ret
= vub_writev(req
, &elem
->out_sg
[1], out_num
);
280 ret
= vub_readv(req
, &elem
->in_sg
[0], in_num
);
283 req
->in
->status
= VIRTIO_BLK_S_OK
;
285 req
->in
->status
= VIRTIO_BLK_S_IOERR
;
287 vub_req_complete(req
);
290 case VIRTIO_BLK_T_FLUSH
:
292 req
->in
->status
= VIRTIO_BLK_S_OK
;
293 vub_req_complete(req
);
295 case VIRTIO_BLK_T_GET_ID
: {
296 size_t size
= MIN(vub_iov_size(&elem
->in_sg
[0], in_num
),
297 VIRTIO_BLK_ID_BYTES
);
298 snprintf(elem
->in_sg
[0].iov_base
, size
, "%s", "vhost_user_blk");
299 req
->in
->status
= VIRTIO_BLK_S_OK
;
300 req
->size
= elem
->in_sg
[0].iov_len
;
301 vub_req_complete(req
);
304 case VIRTIO_BLK_T_DISCARD
:
305 case VIRTIO_BLK_T_WRITE_ZEROES
: {
307 rc
= vub_discard_write_zeroes(req
, &elem
->out_sg
[1], out_num
, type
);
309 req
->in
->status
= VIRTIO_BLK_S_OK
;
311 req
->in
->status
= VIRTIO_BLK_S_IOERR
;
313 vub_req_complete(req
);
317 req
->in
->status
= VIRTIO_BLK_S_UNSUPP
;
318 vub_req_complete(req
);
330 static void vub_process_vq(VuDev
*vu_dev
, int idx
)
337 gdev
= container_of(vu_dev
, VugDev
, parent
);
338 vdev_blk
= container_of(gdev
, VubDev
, parent
);
341 vq
= vu_get_queue(vu_dev
, idx
);
345 ret
= vub_virtio_process_req(vdev_blk
, vq
);
352 static void vub_queue_set_started(VuDev
*vu_dev
, int idx
, bool started
)
358 vq
= vu_get_queue(vu_dev
, idx
);
359 vu_set_queue_handler(vu_dev
, vq
, started
? vub_process_vq
: NULL
);
363 vub_get_features(VuDev
*dev
)
369 gdev
= container_of(dev
, VugDev
, parent
);
370 vdev_blk
= container_of(gdev
, VubDev
, parent
);
372 features
= 1ull << VIRTIO_BLK_F_SIZE_MAX
|
373 1ull << VIRTIO_BLK_F_SEG_MAX
|
374 1ull << VIRTIO_BLK_F_TOPOLOGY
|
375 1ull << VIRTIO_BLK_F_BLK_SIZE
|
376 1ull << VIRTIO_BLK_F_FLUSH
|
377 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
378 1ull << VIRTIO_BLK_F_DISCARD
|
379 1ull << VIRTIO_BLK_F_WRITE_ZEROES
|
381 1ull << VIRTIO_BLK_F_CONFIG_WCE
;
383 if (vdev_blk
->enable_ro
) {
384 features
|= 1ull << VIRTIO_BLK_F_RO
;
391 vub_get_protocol_features(VuDev
*dev
)
393 return 1ull << VHOST_USER_PROTOCOL_F_CONFIG
|
394 1ull << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
;
398 vub_get_config(VuDev
*vu_dev
, uint8_t *config
, uint32_t len
)
403 if (len
> sizeof(struct virtio_blk_config
)) {
407 gdev
= container_of(vu_dev
, VugDev
, parent
);
408 vdev_blk
= container_of(gdev
, VubDev
, parent
);
409 memcpy(config
, &vdev_blk
->blkcfg
, len
);
415 vub_set_config(VuDev
*vu_dev
, const uint8_t *data
,
416 uint32_t offset
, uint32_t size
, uint32_t flags
)
423 /* don't support live migration */
424 if (flags
!= VHOST_SET_CONFIG_TYPE_MASTER
) {
428 gdev
= container_of(vu_dev
, VugDev
, parent
);
429 vdev_blk
= container_of(gdev
, VubDev
, parent
);
431 if (offset
!= offsetof(struct virtio_blk_config
, wce
) ||
437 if (wce
== vdev_blk
->blkcfg
.wce
) {
438 /* Do nothing as same with old configuration */
442 vdev_blk
->blkcfg
.wce
= wce
;
443 fprintf(stdout
, "Write Cache Policy Changed\n");
444 if (vdev_blk
->blk_fd
>= 0) {
445 close(vdev_blk
->blk_fd
);
446 vdev_blk
->blk_fd
= -1;
449 fd
= vub_open(vdev_blk
->blk_name
, wce
);
451 fprintf(stderr
, "Error to open block device %s\n", vdev_blk
->blk_name
);
452 vdev_blk
->blk_fd
= -1;
455 vdev_blk
->blk_fd
= fd
;
460 static const VuDevIface vub_iface
= {
461 .get_features
= vub_get_features
,
462 .queue_set_started
= vub_queue_set_started
,
463 .get_protocol_features
= vub_get_protocol_features
,
464 .get_config
= vub_get_config
,
465 .set_config
= vub_set_config
,
468 static int unix_sock_new(char *unix_fn
)
471 struct sockaddr_un un
;
476 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
482 un
.sun_family
= AF_UNIX
;
483 (void)snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", unix_fn
);
484 len
= sizeof(un
.sun_family
) + strlen(un
.sun_path
);
486 (void)unlink(unix_fn
);
487 if (bind(sock
, (struct sockaddr
*)&un
, len
) < 0) {
492 if (listen(sock
, 1) < 0) {
505 static void vub_free(struct VubDev
*vdev_blk
)
511 g_main_loop_unref(vdev_blk
->loop
);
512 if (vdev_blk
->blk_fd
>= 0) {
513 close(vdev_blk
->blk_fd
);
519 vub_get_blocksize(int fd
)
521 uint32_t blocksize
= 512;
523 #if defined(__linux__) && defined(BLKSSZGET)
524 if (ioctl(fd
, BLKSSZGET
, &blocksize
) == 0) {
533 vub_initialize_config(int fd
, struct virtio_blk_config
*config
)
537 capacity
= lseek64(fd
, 0, SEEK_END
);
538 config
->capacity
= capacity
>> 9;
539 config
->blk_size
= vub_get_blocksize(fd
);
540 config
->size_max
= 65536;
541 config
->seg_max
= 128 - 2;
542 config
->min_io_size
= 1;
543 config
->opt_io_size
= 1;
544 config
->num_queues
= 1;
545 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
546 config
->max_discard_sectors
= 32768;
547 config
->max_discard_seg
= 1;
548 config
->discard_sector_alignment
= config
->blk_size
>> 9;
549 config
->max_write_zeroes_sectors
= 32768;
550 config
->max_write_zeroes_seg
= 1;
555 vub_new(char *blk_file
)
559 vdev_blk
= g_new0(VubDev
, 1);
560 vdev_blk
->loop
= g_main_loop_new(NULL
, FALSE
);
561 vdev_blk
->blk_fd
= vub_open(blk_file
, 0);
562 if (vdev_blk
->blk_fd
< 0) {
563 fprintf(stderr
, "Error to open block device %s\n", blk_file
);
567 vdev_blk
->enable_ro
= false;
568 vdev_blk
->blkcfg
.wce
= 0;
569 vdev_blk
->blk_name
= blk_file
;
571 /* fill virtio_blk_config with block parameters */
572 vub_initialize_config(vdev_blk
->blk_fd
, &vdev_blk
->blkcfg
);
577 static int opt_fdnum
= -1;
578 static char *opt_socket_path
;
579 static char *opt_blk_file
;
580 static gboolean opt_print_caps
;
581 static gboolean opt_read_only
;
583 static GOptionEntry entries
[] = {
584 { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE
, &opt_print_caps
,
585 "Print capabilities", NULL
},
586 { "fd", 'f', 0, G_OPTION_ARG_INT
, &opt_fdnum
,
587 "Use inherited fd socket", "FDNUM" },
588 { "socket-path", 's', 0, G_OPTION_ARG_FILENAME
, &opt_socket_path
,
589 "Use UNIX socket path", "PATH" },
590 {"blk-file", 'b', 0, G_OPTION_ARG_FILENAME
, &opt_blk_file
,
591 "block device or file path", "PATH"},
592 { "read-only", 'r', 0, G_OPTION_ARG_NONE
, &opt_read_only
,
593 "Enable read-only", NULL
},
597 int main(int argc
, char **argv
)
599 int lsock
= -1, csock
= -1;
600 VubDev
*vdev_blk
= NULL
;
601 GError
*error
= NULL
;
602 GOptionContext
*context
;
604 context
= g_option_context_new(NULL
);
605 g_option_context_add_main_entries(context
, entries
, NULL
);
606 if (!g_option_context_parse(context
, &argc
, &argv
, &error
)) {
607 g_printerr("Option parsing failed: %s\n", error
->message
);
610 if (opt_print_caps
) {
612 g_print(" \"type\": \"block\",\n");
613 g_print(" \"features\": [\n");
614 g_print(" \"read-only\",\n");
615 g_print(" \"blk-file\"\n");
622 g_print("%s\n", g_option_context_get_help(context
, true, NULL
));
626 if (opt_socket_path
) {
627 lsock
= unix_sock_new(opt_socket_path
);
631 } else if (opt_fdnum
< 0) {
632 g_print("%s\n", g_option_context_get_help(context
, true, NULL
));
638 csock
= accept(lsock
, NULL
, NULL
);
640 g_printerr("Accept error %s\n", strerror(errno
));
644 vdev_blk
= vub_new(opt_blk_file
);
649 vdev_blk
->enable_ro
= true;
652 if (!vug_init(&vdev_blk
->parent
, VHOST_USER_BLK_MAX_QUEUES
, csock
,
653 vub_panic_cb
, &vub_iface
)) {
654 g_printerr("Failed to initialize libvhost-user-glib\n");
658 g_main_loop_run(vdev_blk
->loop
);
659 g_main_loop_unref(vdev_blk
->loop
);
660 g_option_context_free(context
);
661 vug_deinit(&vdev_blk
->parent
);
669 g_free(opt_socket_path
);
670 g_free(opt_blk_file
);