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
);
116 static int vub_open(const char *file_name
, bool wce
)
125 fd
= open(file_name
, flags
);
127 fprintf(stderr
, "Cannot open file %s, %s\n", file_name
,
136 vub_readv(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
138 VubDev
*vdev_blk
= req
->vdev_blk
;
142 fprintf(stderr
, "Invalid Read IOV count\n");
146 req
->size
= vub_iov_size(iov
, iovcnt
);
147 rc
= preadv(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
149 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %lu failed with %s\n",
150 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
159 vub_writev(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
161 VubDev
*vdev_blk
= req
->vdev_blk
;
165 fprintf(stderr
, "Invalid Write IOV count\n");
169 req
->size
= vub_iov_size(iov
, iovcnt
);
170 rc
= pwritev(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
172 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %lu failed with %s\n",
173 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
182 vub_discard_write_zeroes(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
,
185 struct virtio_blk_discard_write_zeroes
*desc
;
189 size
= vub_iov_size(iov
, iovcnt
);
190 if (size
!= sizeof(*desc
)) {
191 fprintf(stderr
, "Invalid size %ld, expect %ld\n", size
, sizeof(*desc
));
194 buf
= g_new0(char, size
);
195 vub_iov_to_buf(iov
, iovcnt
, buf
);
197 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
198 VubDev
*vdev_blk
= req
->vdev_blk
;
199 desc
= (struct virtio_blk_discard_write_zeroes
*)buf
;
200 uint64_t range
[2] = { le64toh(desc
->sector
) << 9,
201 le32toh(desc
->num_sectors
) << 9 };
202 if (type
== VIRTIO_BLK_T_DISCARD
) {
203 if (ioctl(vdev_blk
->blk_fd
, BLKDISCARD
, range
) == 0) {
207 } else if (type
== VIRTIO_BLK_T_WRITE_ZEROES
) {
208 if (ioctl(vdev_blk
->blk_fd
, BLKZEROOUT
, range
) == 0) {
220 vub_flush(VubReq
*req
)
222 VubDev
*vdev_blk
= req
->vdev_blk
;
224 fdatasync(vdev_blk
->blk_fd
);
227 static int vub_virtio_process_req(VubDev
*vdev_blk
,
230 VugDev
*gdev
= &vdev_blk
->parent
;
231 VuDev
*vu_dev
= &gdev
->parent
;
232 VuVirtqElement
*elem
;
238 elem
= vu_queue_pop(vu_dev
, vq
, sizeof(VuVirtqElement
) + sizeof(VubReq
));
243 /* refer to hw/block/virtio_blk.c */
244 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
245 fprintf(stderr
, "virtio-blk request missing headers\n");
250 req
= g_new0(VubReq
, 1);
251 req
->vdev_blk
= vdev_blk
;
255 in_num
= elem
->in_num
;
256 out_num
= elem
->out_num
;
258 /* don't support VIRTIO_F_ANY_LAYOUT and virtio 1.0 only */
259 if (elem
->out_sg
[0].iov_len
< sizeof(struct virtio_blk_outhdr
)) {
260 fprintf(stderr
, "Invalid outhdr size\n");
263 req
->out
= (struct virtio_blk_outhdr
*)elem
->out_sg
[0].iov_base
;
266 if (elem
->in_sg
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
267 fprintf(stderr
, "Invalid inhdr size\n");
270 req
->in
= (struct virtio_blk_inhdr
*)elem
->in_sg
[in_num
- 1].iov_base
;
273 type
= le32toh(req
->out
->type
);
274 switch (type
& ~VIRTIO_BLK_T_BARRIER
) {
275 case VIRTIO_BLK_T_IN
:
276 case VIRTIO_BLK_T_OUT
: {
278 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
279 req
->sector_num
= le64toh(req
->out
->sector
);
281 ret
= vub_writev(req
, &elem
->out_sg
[1], out_num
);
283 ret
= vub_readv(req
, &elem
->in_sg
[0], in_num
);
286 req
->in
->status
= VIRTIO_BLK_S_OK
;
288 req
->in
->status
= VIRTIO_BLK_S_IOERR
;
290 vub_req_complete(req
);
293 case VIRTIO_BLK_T_FLUSH
:
295 req
->in
->status
= VIRTIO_BLK_S_OK
;
296 vub_req_complete(req
);
298 case VIRTIO_BLK_T_GET_ID
: {
299 size_t size
= MIN(vub_iov_size(&elem
->in_sg
[0], in_num
),
300 VIRTIO_BLK_ID_BYTES
);
301 snprintf(elem
->in_sg
[0].iov_base
, size
, "%s", "vhost_user_blk");
302 req
->in
->status
= VIRTIO_BLK_S_OK
;
303 req
->size
= elem
->in_sg
[0].iov_len
;
304 vub_req_complete(req
);
307 case VIRTIO_BLK_T_DISCARD
:
308 case VIRTIO_BLK_T_WRITE_ZEROES
: {
310 rc
= vub_discard_write_zeroes(req
, &elem
->out_sg
[1], out_num
, type
);
312 req
->in
->status
= VIRTIO_BLK_S_OK
;
314 req
->in
->status
= VIRTIO_BLK_S_IOERR
;
316 vub_req_complete(req
);
320 req
->in
->status
= VIRTIO_BLK_S_UNSUPP
;
321 vub_req_complete(req
);
333 static void vub_process_vq(VuDev
*vu_dev
, int idx
)
340 gdev
= container_of(vu_dev
, VugDev
, parent
);
341 vdev_blk
= container_of(gdev
, VubDev
, parent
);
344 vq
= vu_get_queue(vu_dev
, idx
);
348 ret
= vub_virtio_process_req(vdev_blk
, vq
);
355 static void vub_queue_set_started(VuDev
*vu_dev
, int idx
, bool started
)
361 vq
= vu_get_queue(vu_dev
, idx
);
362 vu_set_queue_handler(vu_dev
, vq
, started
? vub_process_vq
: NULL
);
366 vub_get_features(VuDev
*dev
)
372 gdev
= container_of(dev
, VugDev
, parent
);
373 vdev_blk
= container_of(gdev
, VubDev
, parent
);
375 features
= 1ull << VIRTIO_BLK_F_SIZE_MAX
|
376 1ull << VIRTIO_BLK_F_SEG_MAX
|
377 1ull << VIRTIO_BLK_F_TOPOLOGY
|
378 1ull << VIRTIO_BLK_F_BLK_SIZE
|
379 1ull << VIRTIO_BLK_F_FLUSH
|
380 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
381 1ull << VIRTIO_BLK_F_DISCARD
|
382 1ull << VIRTIO_BLK_F_WRITE_ZEROES
|
384 1ull << VIRTIO_BLK_F_CONFIG_WCE
;
386 if (vdev_blk
->enable_ro
) {
387 features
|= 1ull << VIRTIO_BLK_F_RO
;
394 vub_get_protocol_features(VuDev
*dev
)
396 return 1ull << VHOST_USER_PROTOCOL_F_CONFIG
|
397 1ull << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
;
401 vub_get_config(VuDev
*vu_dev
, uint8_t *config
, uint32_t len
)
406 if (len
> sizeof(struct virtio_blk_config
)) {
410 gdev
= container_of(vu_dev
, VugDev
, parent
);
411 vdev_blk
= container_of(gdev
, VubDev
, parent
);
412 memcpy(config
, &vdev_blk
->blkcfg
, len
);
418 vub_set_config(VuDev
*vu_dev
, const uint8_t *data
,
419 uint32_t offset
, uint32_t size
, uint32_t flags
)
426 /* don't support live migration */
427 if (flags
!= VHOST_SET_CONFIG_TYPE_MASTER
) {
431 gdev
= container_of(vu_dev
, VugDev
, parent
);
432 vdev_blk
= container_of(gdev
, VubDev
, parent
);
434 if (offset
!= offsetof(struct virtio_blk_config
, wce
) ||
440 if (wce
== vdev_blk
->blkcfg
.wce
) {
441 /* Do nothing as same with old configuration */
445 vdev_blk
->blkcfg
.wce
= wce
;
446 fprintf(stdout
, "Write Cache Policy Changed\n");
447 if (vdev_blk
->blk_fd
>= 0) {
448 close(vdev_blk
->blk_fd
);
449 vdev_blk
->blk_fd
= -1;
452 fd
= vub_open(vdev_blk
->blk_name
, wce
);
454 fprintf(stderr
, "Error to open block device %s\n", vdev_blk
->blk_name
);
455 vdev_blk
->blk_fd
= -1;
458 vdev_blk
->blk_fd
= fd
;
463 static const VuDevIface vub_iface
= {
464 .get_features
= vub_get_features
,
465 .queue_set_started
= vub_queue_set_started
,
466 .get_protocol_features
= vub_get_protocol_features
,
467 .get_config
= vub_get_config
,
468 .set_config
= vub_set_config
,
471 static int unix_sock_new(char *unix_fn
)
474 struct sockaddr_un un
;
479 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
485 un
.sun_family
= AF_UNIX
;
486 (void)snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", unix_fn
);
487 len
= sizeof(un
.sun_family
) + strlen(un
.sun_path
);
489 (void)unlink(unix_fn
);
490 if (bind(sock
, (struct sockaddr
*)&un
, len
) < 0) {
495 if (listen(sock
, 1) < 0) {
508 static void vub_free(struct VubDev
*vdev_blk
)
514 g_main_loop_unref(vdev_blk
->loop
);
515 if (vdev_blk
->blk_fd
>= 0) {
516 close(vdev_blk
->blk_fd
);
522 vub_get_blocksize(int fd
)
524 uint32_t blocksize
= 512;
526 #if defined(__linux__) && defined(BLKSSZGET)
527 if (ioctl(fd
, BLKSSZGET
, &blocksize
) == 0) {
536 vub_initialize_config(int fd
, struct virtio_blk_config
*config
)
540 capacity
= lseek64(fd
, 0, SEEK_END
);
541 config
->capacity
= capacity
>> 9;
542 config
->blk_size
= vub_get_blocksize(fd
);
543 config
->size_max
= 65536;
544 config
->seg_max
= 128 - 2;
545 config
->min_io_size
= 1;
546 config
->opt_io_size
= 1;
547 config
->num_queues
= 1;
548 #if defined(__linux__) && defined(BLKDISCARD) && defined(BLKZEROOUT)
549 config
->max_discard_sectors
= 32768;
550 config
->max_discard_seg
= 1;
551 config
->discard_sector_alignment
= config
->blk_size
>> 9;
552 config
->max_write_zeroes_sectors
= 32768;
553 config
->max_write_zeroes_seg
= 1;
558 vub_new(char *blk_file
)
562 vdev_blk
= g_new0(VubDev
, 1);
563 vdev_blk
->loop
= g_main_loop_new(NULL
, FALSE
);
564 vdev_blk
->blk_fd
= vub_open(blk_file
, 0);
565 if (vdev_blk
->blk_fd
< 0) {
566 fprintf(stderr
, "Error to open block device %s\n", blk_file
);
570 vdev_blk
->enable_ro
= false;
571 vdev_blk
->blkcfg
.wce
= 0;
572 vdev_blk
->blk_name
= blk_file
;
574 /* fill virtio_blk_config with block parameters */
575 vub_initialize_config(vdev_blk
->blk_fd
, &vdev_blk
->blkcfg
);
580 static int opt_fdnum
= -1;
581 static char *opt_socket_path
;
582 static char *opt_blk_file
;
583 static gboolean opt_print_caps
;
584 static gboolean opt_read_only
;
586 static GOptionEntry entries
[] = {
587 { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE
, &opt_print_caps
,
588 "Print capabilities", NULL
},
589 { "fd", 'f', 0, G_OPTION_ARG_INT
, &opt_fdnum
,
590 "Use inherited fd socket", "FDNUM" },
591 { "socket-path", 's', 0, G_OPTION_ARG_FILENAME
, &opt_socket_path
,
592 "Use UNIX socket path", "PATH" },
593 {"blk-file", 'b', 0, G_OPTION_ARG_FILENAME
, &opt_blk_file
,
594 "block device or file path", "PATH"},
595 { "read-only", 'r', 0, G_OPTION_ARG_NONE
, &opt_read_only
,
596 "Enable read-only", NULL
}
599 int main(int argc
, char **argv
)
601 int lsock
= -1, csock
= -1;
602 VubDev
*vdev_blk
= NULL
;
603 GError
*error
= NULL
;
604 GOptionContext
*context
;
606 context
= g_option_context_new(NULL
);
607 g_option_context_add_main_entries(context
, entries
, NULL
);
608 if (!g_option_context_parse(context
, &argc
, &argv
, &error
)) {
609 g_printerr("Option parsing failed: %s\n", error
->message
);
612 if (opt_print_caps
) {
614 g_print(" \"type\": \"block\",\n");
615 g_print(" \"features\": [\n");
616 g_print(" \"read-only\",\n");
617 g_print(" \"blk-file\"\n");
624 g_print("%s\n", g_option_context_get_help(context
, true, NULL
));
628 if (opt_socket_path
) {
629 lsock
= unix_sock_new(opt_socket_path
);
633 } else if (opt_fdnum
< 0) {
634 g_print("%s\n", g_option_context_get_help(context
, true, NULL
));
640 csock
= accept(lsock
, NULL
, NULL
);
642 g_printerr("Accept error %s\n", strerror(errno
));
646 vdev_blk
= vub_new(opt_blk_file
);
651 vdev_blk
->enable_ro
= true;
654 if (!vug_init(&vdev_blk
->parent
, VHOST_USER_BLK_MAX_QUEUES
, csock
,
655 vub_panic_cb
, &vub_iface
)) {
656 g_printerr("Failed to initialize libvhost-user-glib\n");
660 g_main_loop_run(vdev_blk
->loop
);
661 g_main_loop_unref(vdev_blk
->loop
);
662 g_option_context_free(context
);
663 vug_deinit(&vdev_blk
->parent
);
671 g_free(opt_socket_path
);
672 g_free(opt_blk_file
);