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"
25 struct virtio_blk_inhdr
{
29 /* vhost user block device */
30 typedef struct VubDev
{
33 struct virtio_blk_config blkcfg
;
39 typedef struct VubReq
{
43 struct virtio_blk_inhdr
*in
;
44 struct virtio_blk_outhdr
*out
;
49 /* refer util/iov.c */
50 static size_t vub_iov_size(const struct iovec
*iov
,
51 const unsigned int iov_cnt
)
57 for (i
= 0; i
< iov_cnt
; i
++) {
58 len
+= iov
[i
].iov_len
;
63 static void vub_panic_cb(VuDev
*vu_dev
, const char *buf
)
70 gdev
= container_of(vu_dev
, VugDev
, parent
);
71 vdev_blk
= container_of(gdev
, VubDev
, parent
);
73 g_warning("vu_panic: %s", buf
);
76 g_main_loop_quit(vdev_blk
->loop
);
79 static void vub_req_complete(VubReq
*req
)
81 VugDev
*gdev
= &req
->vdev_blk
->parent
;
82 VuDev
*vu_dev
= &gdev
->parent
;
84 /* IO size with 1 extra status byte */
85 vu_queue_push(vu_dev
, req
->vq
, req
->elem
,
87 vu_queue_notify(vu_dev
, req
->vq
);
96 static int vub_open(const char *file_name
, bool wce
)
105 fd
= open(file_name
, flags
);
107 fprintf(stderr
, "Cannot open file %s, %s\n", file_name
,
116 vub_readv(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
118 VubDev
*vdev_blk
= req
->vdev_blk
;
122 fprintf(stderr
, "Invalid Read IOV count\n");
126 req
->size
= vub_iov_size(iov
, iovcnt
);
127 rc
= preadv(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
129 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %lu failed with %s\n",
130 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
139 vub_writev(VubReq
*req
, struct iovec
*iov
, uint32_t iovcnt
)
141 VubDev
*vdev_blk
= req
->vdev_blk
;
145 fprintf(stderr
, "Invalid Write IOV count\n");
149 req
->size
= vub_iov_size(iov
, iovcnt
);
150 rc
= pwritev(vdev_blk
->blk_fd
, iov
, iovcnt
, req
->sector_num
* 512);
152 fprintf(stderr
, "%s, Sector %"PRIu64
", Size %lu failed with %s\n",
153 vdev_blk
->blk_name
, req
->sector_num
, req
->size
,
162 vub_flush(VubReq
*req
)
164 VubDev
*vdev_blk
= req
->vdev_blk
;
166 fdatasync(vdev_blk
->blk_fd
);
169 static int vub_virtio_process_req(VubDev
*vdev_blk
,
172 VugDev
*gdev
= &vdev_blk
->parent
;
173 VuDev
*vu_dev
= &gdev
->parent
;
174 VuVirtqElement
*elem
;
180 elem
= vu_queue_pop(vu_dev
, vq
, sizeof(VuVirtqElement
) + sizeof(VubReq
));
185 /* refer to hw/block/virtio_blk.c */
186 if (elem
->out_num
< 1 || elem
->in_num
< 1) {
187 fprintf(stderr
, "virtio-blk request missing headers\n");
192 req
= g_new0(VubReq
, 1);
193 req
->vdev_blk
= vdev_blk
;
197 in_num
= elem
->in_num
;
198 out_num
= elem
->out_num
;
200 /* don't support VIRTIO_F_ANY_LAYOUT and virtio 1.0 only */
201 if (elem
->out_sg
[0].iov_len
< sizeof(struct virtio_blk_outhdr
)) {
202 fprintf(stderr
, "Invalid outhdr size\n");
205 req
->out
= (struct virtio_blk_outhdr
*)elem
->out_sg
[0].iov_base
;
208 if (elem
->in_sg
[in_num
- 1].iov_len
< sizeof(struct virtio_blk_inhdr
)) {
209 fprintf(stderr
, "Invalid inhdr size\n");
212 req
->in
= (struct virtio_blk_inhdr
*)elem
->in_sg
[in_num
- 1].iov_base
;
215 type
= le32toh(req
->out
->type
);
216 switch (type
& ~(VIRTIO_BLK_T_OUT
| VIRTIO_BLK_T_BARRIER
)) {
217 case VIRTIO_BLK_T_IN
: {
219 bool is_write
= type
& VIRTIO_BLK_T_OUT
;
220 req
->sector_num
= le64toh(req
->out
->sector
);
222 ret
= vub_writev(req
, &elem
->out_sg
[1], out_num
);
224 ret
= vub_readv(req
, &elem
->in_sg
[0], in_num
);
227 req
->in
->status
= VIRTIO_BLK_S_OK
;
229 req
->in
->status
= VIRTIO_BLK_S_IOERR
;
231 vub_req_complete(req
);
234 case VIRTIO_BLK_T_FLUSH
: {
236 req
->in
->status
= VIRTIO_BLK_S_OK
;
237 vub_req_complete(req
);
240 case VIRTIO_BLK_T_GET_ID
: {
241 size_t size
= MIN(vub_iov_size(&elem
->in_sg
[0], in_num
),
242 VIRTIO_BLK_ID_BYTES
);
243 snprintf(elem
->in_sg
[0].iov_base
, size
, "%s", "vhost_user_blk");
244 req
->in
->status
= VIRTIO_BLK_S_OK
;
245 req
->size
= elem
->in_sg
[0].iov_len
;
246 vub_req_complete(req
);
250 req
->in
->status
= VIRTIO_BLK_S_UNSUPP
;
251 vub_req_complete(req
);
264 static void vub_process_vq(VuDev
*vu_dev
, int idx
)
271 if ((idx
< 0) || (idx
>= VHOST_MAX_NR_VIRTQUEUE
)) {
272 fprintf(stderr
, "VQ Index out of range: %d\n", idx
);
273 vub_panic_cb(vu_dev
, NULL
);
277 gdev
= container_of(vu_dev
, VugDev
, parent
);
278 vdev_blk
= container_of(gdev
, VubDev
, parent
);
281 vq
= vu_get_queue(vu_dev
, idx
);
285 ret
= vub_virtio_process_req(vdev_blk
, vq
);
292 static void vub_queue_set_started(VuDev
*vu_dev
, int idx
, bool started
)
298 vq
= vu_get_queue(vu_dev
, idx
);
299 vu_set_queue_handler(vu_dev
, vq
, started
? vub_process_vq
: NULL
);
303 vub_get_features(VuDev
*dev
)
309 gdev
= container_of(dev
, VugDev
, parent
);
310 vdev_blk
= container_of(gdev
, VubDev
, parent
);
312 features
= 1ull << VIRTIO_BLK_F_SIZE_MAX
|
313 1ull << VIRTIO_BLK_F_SEG_MAX
|
314 1ull << VIRTIO_BLK_F_TOPOLOGY
|
315 1ull << VIRTIO_BLK_F_BLK_SIZE
|
316 1ull << VIRTIO_BLK_F_FLUSH
|
317 1ull << VIRTIO_BLK_F_CONFIG_WCE
|
318 1ull << VIRTIO_F_VERSION_1
|
319 1ull << VHOST_USER_F_PROTOCOL_FEATURES
;
321 if (vdev_blk
->enable_ro
) {
322 features
|= 1ull << VIRTIO_BLK_F_RO
;
329 vub_get_protocol_features(VuDev
*dev
)
331 return 1ull << VHOST_USER_PROTOCOL_F_CONFIG
;
335 vub_get_config(VuDev
*vu_dev
, uint8_t *config
, uint32_t len
)
340 gdev
= container_of(vu_dev
, VugDev
, parent
);
341 vdev_blk
= container_of(gdev
, VubDev
, parent
);
342 memcpy(config
, &vdev_blk
->blkcfg
, len
);
348 vub_set_config(VuDev
*vu_dev
, const uint8_t *data
,
349 uint32_t offset
, uint32_t size
, uint32_t flags
)
356 /* don't support live migration */
357 if (flags
!= VHOST_SET_CONFIG_TYPE_MASTER
) {
361 gdev
= container_of(vu_dev
, VugDev
, parent
);
362 vdev_blk
= container_of(gdev
, VubDev
, parent
);
364 if (offset
!= offsetof(struct virtio_blk_config
, wce
) ||
370 if (wce
== vdev_blk
->blkcfg
.wce
) {
371 /* Do nothing as same with old configuration */
375 vdev_blk
->blkcfg
.wce
= wce
;
376 fprintf(stdout
, "Write Cache Policy Changed\n");
377 if (vdev_blk
->blk_fd
>= 0) {
378 close(vdev_blk
->blk_fd
);
379 vdev_blk
->blk_fd
= -1;
382 fd
= vub_open(vdev_blk
->blk_name
, wce
);
384 fprintf(stderr
, "Error to open block device %s\n", vdev_blk
->blk_name
);
385 vdev_blk
->blk_fd
= -1;
388 vdev_blk
->blk_fd
= fd
;
393 static const VuDevIface vub_iface
= {
394 .get_features
= vub_get_features
,
395 .queue_set_started
= vub_queue_set_started
,
396 .get_protocol_features
= vub_get_protocol_features
,
397 .get_config
= vub_get_config
,
398 .set_config
= vub_set_config
,
401 static int unix_sock_new(char *unix_fn
)
404 struct sockaddr_un un
;
409 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
415 un
.sun_family
= AF_UNIX
;
416 (void)snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", unix_fn
);
417 len
= sizeof(un
.sun_family
) + strlen(un
.sun_path
);
419 (void)unlink(unix_fn
);
420 if (bind(sock
, (struct sockaddr
*)&un
, len
) < 0) {
425 if (listen(sock
, 1) < 0) {
438 static void vub_free(struct VubDev
*vdev_blk
)
444 g_main_loop_unref(vdev_blk
->loop
);
445 if (vdev_blk
->blk_fd
>= 0) {
446 close(vdev_blk
->blk_fd
);
452 vub_get_blocksize(int fd
)
454 uint32_t blocksize
= 512;
456 #if defined(__linux__) && defined(BLKSSZGET)
457 if (ioctl(fd
, BLKSSZGET
, &blocksize
) == 0) {
466 vub_initialize_config(int fd
, struct virtio_blk_config
*config
)
470 capacity
= lseek64(fd
, 0, SEEK_END
);
471 config
->capacity
= capacity
>> 9;
472 config
->blk_size
= vub_get_blocksize(fd
);
473 config
->size_max
= 65536;
474 config
->seg_max
= 128 - 2;
475 config
->min_io_size
= 1;
476 config
->opt_io_size
= 1;
477 config
->num_queues
= 1;
481 vub_new(char *blk_file
)
485 vdev_blk
= g_new0(VubDev
, 1);
486 vdev_blk
->loop
= g_main_loop_new(NULL
, FALSE
);
487 vdev_blk
->blk_fd
= vub_open(blk_file
, 0);
488 if (vdev_blk
->blk_fd
< 0) {
489 fprintf(stderr
, "Error to open block device %s\n", blk_file
);
493 vdev_blk
->enable_ro
= false;
494 vdev_blk
->blkcfg
.wce
= 0;
495 vdev_blk
->blk_name
= blk_file
;
497 /* fill virtio_blk_config with block parameters */
498 vub_initialize_config(vdev_blk
->blk_fd
, &vdev_blk
->blkcfg
);
503 int main(int argc
, char **argv
)
506 char *unix_socket
= NULL
;
507 char *blk_file
= NULL
;
508 bool enable_ro
= false;
509 int lsock
= -1, csock
= -1;
510 VubDev
*vdev_blk
= NULL
;
512 while ((opt
= getopt(argc
, argv
, "b:rs:h")) != -1) {
515 blk_file
= g_strdup(optarg
);
518 unix_socket
= g_strdup(optarg
);
525 printf("Usage: %s [ -b block device or file, -s UNIX domain socket"
526 " | -r Enable read-only ] | [ -h ]\n", argv
[0]);
531 if (!unix_socket
|| !blk_file
) {
532 printf("Usage: %s [ -b block device or file, -s UNIX domain socket"
533 " | -r Enable read-only ] | [ -h ]\n", argv
[0]);
537 lsock
= unix_sock_new(unix_socket
);
542 csock
= accept(lsock
, (void *)0, (void *)0);
544 fprintf(stderr
, "Accept error %s\n", strerror(errno
));
548 vdev_blk
= vub_new(blk_file
);
553 vdev_blk
->enable_ro
= true;
556 vug_init(&vdev_blk
->parent
, csock
, vub_panic_cb
, &vub_iface
);
558 g_main_loop_run(vdev_blk
->loop
);
560 vug_deinit(&vdev_blk
->parent
);