2 * vhost-user-scsi sample application
4 * Copyright (c) 2016 Nutanix Inc. All rights reserved.
7 * Felipe Franciosi <felipe@nutanix.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 only.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "contrib/libvhost-user/libvhost-user.h"
15 #include "standard-headers/linux/virtio_scsi.h"
16 #include "iscsi/iscsi.h"
17 #include "iscsi/scsi-lowlevel.h"
21 /* #define VUS_DEBUG 1 */
29 (void)clock_gettime(CLOCK_REALTIME, &ts); \
30 (void)strftime(timebuf, 64, "%Y%m%d %T", gmtime_r(&ts.tv_sec, &tm))
32 #define PEXT(lvl, msg, ...) do { \
34 fprintf(stderr, "%s.%06ld " lvl ": %s:%s():%d: " msg "\n", \
35 timebuf, ts.tv_nsec / 1000, \
36 __FILE__, __func__, __LINE__, ## __VA_ARGS__); \
39 #define PNOR(lvl, msg, ...) do { \
41 fprintf(stderr, "%s.%06ld " lvl ": " msg "\n", \
42 timebuf, ts.tv_nsec / 1000, ## __VA_ARGS__); \
46 #define PDBG(msg, ...) PEXT("DBG", msg, ## __VA_ARGS__)
47 #define PERR(msg, ...) PEXT("ERR", msg, ## __VA_ARGS__)
48 #define PLOG(msg, ...) PEXT("LOG", msg, ## __VA_ARGS__)
50 #define PDBG(msg, ...) { }
51 #define PERR(msg, ...) PNOR("ERR", msg, ## __VA_ARGS__)
52 #define PLOG(msg, ...) PNOR("LOG", msg, ## __VA_ARGS__)
55 /** vhost-user-scsi specific definitions **/
57 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
59 typedef struct VusIscsiLun
{
60 struct iscsi_context
*iscsi_ctx
;
64 typedef struct VusDev
{
68 GTree
*fdmap
; /* fd -> gsource context id */
72 /** glib event loop integration for libvhost-user and misc callbacks **/
74 QEMU_BUILD_BUG_ON((int)G_IO_IN
!= (int)VU_WATCH_IN
);
75 QEMU_BUILD_BUG_ON((int)G_IO_OUT
!= (int)VU_WATCH_OUT
);
76 QEMU_BUILD_BUG_ON((int)G_IO_PRI
!= (int)VU_WATCH_PRI
);
77 QEMU_BUILD_BUG_ON((int)G_IO_ERR
!= (int)VU_WATCH_ERR
);
78 QEMU_BUILD_BUG_ON((int)G_IO_HUP
!= (int)VU_WATCH_HUP
);
80 typedef struct vus_gsrc
{
86 static gint
vus_fdmap_compare(gconstpointer a
, gconstpointer b
)
88 return (b
> a
) - (b
< a
);
91 static gboolean
vus_gsrc_prepare(GSource
*src
, gint
*timeout
)
99 static gboolean
vus_gsrc_check(GSource
*src
)
101 vus_gsrc_t
*vus_src
= (vus_gsrc_t
*)src
;
105 return vus_src
->gfd
.revents
& vus_src
->gfd
.events
;
108 static gboolean
vus_gsrc_dispatch(GSource
*src
, GSourceFunc cb
, gpointer data
)
111 vus_gsrc_t
*vus_src
= (vus_gsrc_t
*)src
;
115 vdev_scsi
= vus_src
->vdev_scsi
;
119 ((vu_watch_cb
)cb
)(&vdev_scsi
->vu_dev
, vus_src
->gfd
.revents
, data
);
121 return G_SOURCE_CONTINUE
;
124 static GSourceFuncs vus_gsrc_funcs
= {
131 static void vus_gsrc_new(VusDev
*vdev_scsi
, int fd
, GIOCondition cond
,
132 vu_watch_cb vu_cb
, GSourceFunc gsrc_cb
, gpointer data
)
140 assert(vu_cb
|| gsrc_cb
);
141 assert(!(vu_cb
&& gsrc_cb
));
143 vus_gsrc
= g_source_new(&vus_gsrc_funcs
, sizeof(vus_gsrc_t
));
144 g_source_set_callback(vus_gsrc
, (GSourceFunc
) vu_cb
, data
, NULL
);
145 vus_src
= (vus_gsrc_t
*)vus_gsrc
;
147 vus_src
->vdev_scsi
= vdev_scsi
;
148 vus_src
->gfd
.fd
= fd
;
149 vus_src
->gfd
.events
= cond
;
151 g_source_add_poll(vus_gsrc
, &vus_src
->gfd
);
152 g_source_set_callback(vus_gsrc
, gsrc_cb
, data
, NULL
);
153 id
= g_source_attach(vus_gsrc
, NULL
);
155 g_source_unref(vus_gsrc
);
157 g_tree_insert(vdev_scsi
->fdmap
, (gpointer
)(uintptr_t)fd
,
158 (gpointer
)(uintptr_t)id
);
161 /** libiscsi integration **/
163 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq
;
164 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp
;
166 static int vus_iscsi_add_lun(VusIscsiLun
*lun
, char *iscsi_uri
)
168 struct iscsi_url
*iscsi_url
;
169 struct iscsi_context
*iscsi_ctx
;
174 assert(!lun
->iscsi_ctx
);
176 iscsi_ctx
= iscsi_create_context(VUS_ISCSI_INITIATOR
);
178 PERR("Unable to create iSCSI context");
182 iscsi_url
= iscsi_parse_full_url(iscsi_ctx
, iscsi_uri
);
184 PERR("Unable to parse iSCSI URL: %s", iscsi_get_error(iscsi_ctx
));
188 iscsi_set_session_type(iscsi_ctx
, ISCSI_SESSION_NORMAL
);
189 iscsi_set_header_digest(iscsi_ctx
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
190 if (iscsi_full_connect_sync(iscsi_ctx
, iscsi_url
->portal
, iscsi_url
->lun
)) {
191 PERR("Unable to login to iSCSI portal: %s", iscsi_get_error(iscsi_ctx
));
195 lun
->iscsi_ctx
= iscsi_ctx
;
196 lun
->iscsi_lun
= iscsi_url
->lun
;
198 PDBG("Context %p created for lun 0: %s", iscsi_ctx
, iscsi_uri
);
202 iscsi_destroy_url(iscsi_url
);
207 (void)iscsi_destroy_context(iscsi_ctx
);
212 static struct scsi_task
*scsi_task_new(int cdb_len
, uint8_t *cdb
, int dir
,
215 struct scsi_task
*task
;
220 task
= g_new0(struct scsi_task
, 1);
221 memcpy(task
->cdb
, cdb
, cdb_len
);
222 task
->cdb_size
= cdb_len
;
223 task
->xfer_dir
= dir
;
224 task
->expxferlen
= xfer_len
;
229 static int get_cdb_len(uint8_t *cdb
)
233 switch (cdb
[0] >> 5) {
235 case 1: /* fall through */
240 PERR("Unable to determine cdb len (0x%02hhX)", cdb
[0] >> 5);
244 static int handle_cmd_sync(struct iscsi_context
*ctx
,
245 VirtIOSCSICmdReq
*req
,
246 struct iovec
*out
, unsigned int out_len
,
247 VirtIOSCSICmdResp
*rsp
,
248 struct iovec
*in
, unsigned int in_len
)
250 struct scsi_task
*task
;
260 if (!(!req
->lun
[1] && req
->lun
[2] == 0x40 && !req
->lun
[3])) {
261 /* Ignore anything different than target=0, lun=0 */
262 PDBG("Ignoring unconnected lun (0x%hhX, 0x%hhX)",
263 req
->lun
[1], req
->lun
[3]);
264 rsp
->status
= SCSI_STATUS_CHECK_CONDITION
;
265 memset(rsp
->sense
, 0, sizeof(rsp
->sense
));
267 rsp
->sense
[0] = 0x70;
268 rsp
->sense
[2] = SCSI_SENSE_ILLEGAL_REQUEST
;
270 rsp
->sense
[12] = 0x24;
275 cdb_len
= get_cdb_len(req
->cdb
);
281 if (!out_len
&& !in_len
) {
282 dir
= SCSI_XFER_NONE
;
283 } else if (out_len
) {
284 dir
= SCSI_XFER_WRITE
;
285 for (i
= 0; i
< out_len
; i
++) {
286 len
+= out
[i
].iov_len
;
289 dir
= SCSI_XFER_READ
;
290 for (i
= 0; i
< in_len
; i
++) {
291 len
+= in
[i
].iov_len
;
295 task
= scsi_task_new(cdb_len
, req
->cdb
, dir
, len
);
297 if (dir
== SCSI_XFER_WRITE
) {
298 task
->iovector_out
.iov
= (struct scsi_iovec
*)out
;
299 task
->iovector_out
.niov
= out_len
;
300 } else if (dir
== SCSI_XFER_READ
) {
301 task
->iovector_in
.iov
= (struct scsi_iovec
*)in
;
302 task
->iovector_in
.niov
= in_len
;
305 PDBG("Sending iscsi cmd (cdb_len=%d, dir=%d, task=%p)",
307 if (!iscsi_scsi_command_sync(ctx
, 0, task
, NULL
)) {
308 PERR("Error serving SCSI command");
313 memset(rsp
, 0, sizeof(*rsp
));
315 rsp
->status
= task
->status
;
316 rsp
->resid
= task
->residual
;
318 if (task
->status
== SCSI_STATUS_CHECK_CONDITION
) {
319 rsp
->response
= VIRTIO_SCSI_S_FAILURE
;
320 rsp
->sense_len
= task
->datain
.size
- 2;
321 memcpy(rsp
->sense
, &task
->datain
.data
[2], rsp
->sense_len
);
326 PDBG("Filled in rsp: status=%hhX, resid=%u, response=%hhX, sense_len=%u",
327 rsp
->status
, rsp
->resid
, rsp
->response
, rsp
->sense_len
);
332 /** libvhost-user callbacks **/
334 static void vus_panic_cb(VuDev
*vu_dev
, const char *buf
)
340 vdev_scsi
= container_of(vu_dev
, VusDev
, vu_dev
);
342 PERR("vu_panic: %s", buf
);
345 g_main_loop_quit(vdev_scsi
->loop
);
348 static void vus_add_watch_cb(VuDev
*vu_dev
, int fd
, int vu_evt
, vu_watch_cb cb
,
358 vdev_scsi
= container_of(vu_dev
, VusDev
, vu_dev
);
359 id
= (guint
)(uintptr_t)g_tree_lookup(vdev_scsi
->fdmap
,
360 (gpointer
)(uintptr_t)fd
);
362 GSource
*vus_src
= g_main_context_find_source_by_id(NULL
, id
);
364 g_source_destroy(vus_src
);
365 (void)g_tree_remove(vdev_scsi
->fdmap
, (gpointer
)(uintptr_t)fd
);
368 vus_gsrc_new(vdev_scsi
, fd
, vu_evt
, cb
, NULL
, pvt
);
371 static void vus_del_watch_cb(VuDev
*vu_dev
, int fd
)
379 vdev_scsi
= container_of(vu_dev
, VusDev
, vu_dev
);
380 id
= (guint
)(uintptr_t)g_tree_lookup(vdev_scsi
->fdmap
,
381 (gpointer
)(uintptr_t)fd
);
383 GSource
*vus_src
= g_main_context_find_source_by_id(NULL
, id
);
385 g_source_destroy(vus_src
);
386 (void)g_tree_remove(vdev_scsi
->fdmap
, (gpointer
)(uintptr_t)fd
);
390 static void vus_proc_req(VuDev
*vu_dev
, int idx
)
397 vdev_scsi
= container_of(vu_dev
, VusDev
, vu_dev
);
398 if (idx
< 0 || idx
>= VHOST_MAX_NR_VIRTQUEUE
) {
399 PERR("VQ Index out of range: %d", idx
);
400 vus_panic_cb(vu_dev
, NULL
);
404 vq
= vu_get_queue(vu_dev
, idx
);
406 PERR("Error fetching VQ (dev=%p, idx=%d)", vu_dev
, idx
);
407 vus_panic_cb(vu_dev
, NULL
);
411 PDBG("Got kicked on vq[%d]@%p", idx
, vq
);
414 VuVirtqElement
*elem
;
415 VirtIOSCSICmdReq
*req
;
416 VirtIOSCSICmdResp
*rsp
;
418 elem
= vu_queue_pop(vu_dev
, vq
, sizeof(VuVirtqElement
));
420 PDBG("No more elements pending on vq[%d]@%p", idx
, vq
);
423 PDBG("Popped elem@%p", elem
);
425 assert(!(elem
->out_num
> 1 && elem
->in_num
> 1));
426 assert(elem
->out_num
> 0 && elem
->in_num
> 0);
428 if (elem
->out_sg
[0].iov_len
< sizeof(VirtIOSCSICmdReq
)) {
429 PERR("Invalid virtio-scsi req header");
430 vus_panic_cb(vu_dev
, NULL
);
433 req
= (VirtIOSCSICmdReq
*)elem
->out_sg
[0].iov_base
;
435 if (elem
->in_sg
[0].iov_len
< sizeof(VirtIOSCSICmdResp
)) {
436 PERR("Invalid virtio-scsi rsp header");
437 vus_panic_cb(vu_dev
, NULL
);
440 rsp
= (VirtIOSCSICmdResp
*)elem
->in_sg
[0].iov_base
;
442 if (handle_cmd_sync(vdev_scsi
->lun
.iscsi_ctx
,
443 req
, &elem
->out_sg
[1], elem
->out_num
- 1,
444 rsp
, &elem
->in_sg
[1], elem
->in_num
- 1) != 0) {
445 vus_panic_cb(vu_dev
, NULL
);
449 vu_queue_push(vu_dev
, vq
, elem
, 0);
450 vu_queue_notify(vu_dev
, vq
);
456 static void vus_queue_set_started(VuDev
*vu_dev
, int idx
, bool started
)
462 if (idx
< 0 || idx
>= VHOST_MAX_NR_VIRTQUEUE
) {
463 PERR("VQ Index out of range: %d", idx
);
464 vus_panic_cb(vu_dev
, NULL
);
468 vq
= vu_get_queue(vu_dev
, idx
);
470 if (idx
== 0 || idx
== 1) {
471 PDBG("queue %d unimplemented", idx
);
473 vu_set_queue_handler(vu_dev
, vq
, started
? vus_proc_req
: NULL
);
477 static const VuDevIface vus_iface
= {
478 .queue_set_started
= vus_queue_set_started
,
481 static gboolean
vus_vhost_cb(gpointer data
)
483 VuDev
*vu_dev
= (VuDev
*)data
;
487 if (!vu_dispatch(vu_dev
) != 0) {
488 PERR("Error processing vhost message");
489 vus_panic_cb(vu_dev
, NULL
);
490 return G_SOURCE_REMOVE
;
493 return G_SOURCE_CONTINUE
;
498 static int unix_sock_new(char *unix_fn
)
501 struct sockaddr_un un
;
506 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
512 un
.sun_family
= AF_UNIX
;
513 (void)snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", unix_fn
);
514 len
= sizeof(un
.sun_family
) + strlen(un
.sun_path
);
516 (void)unlink(unix_fn
);
517 if (bind(sock
, (struct sockaddr
*)&un
, len
) < 0) {
522 if (listen(sock
, 1) < 0) {
535 /** vhost-user-scsi **/
537 static void vdev_scsi_free(VusDev
*vdev_scsi
)
539 if (vdev_scsi
->server_sock
>= 0) {
540 close(vdev_scsi
->server_sock
);
542 g_main_loop_unref(vdev_scsi
->loop
);
543 g_tree_destroy(vdev_scsi
->fdmap
);
547 static VusDev
*vdev_scsi_new(int server_sock
)
551 vdev_scsi
= g_new0(VusDev
, 1);
552 vdev_scsi
->server_sock
= server_sock
;
553 vdev_scsi
->loop
= g_main_loop_new(NULL
, FALSE
);
554 vdev_scsi
->fdmap
= g_tree_new(vus_fdmap_compare
);
559 static int vdev_scsi_run(VusDev
*vdev_scsi
)
565 assert(vdev_scsi
->server_sock
>= 0);
566 assert(vdev_scsi
->loop
);
568 cli_sock
= accept(vdev_scsi
->server_sock
, NULL
, NULL
);
574 vu_init(&vdev_scsi
->vu_dev
,
581 vus_gsrc_new(vdev_scsi
, cli_sock
, G_IO_IN
, NULL
, vus_vhost_cb
,
584 g_main_loop_run(vdev_scsi
->loop
);
586 vu_deinit(&vdev_scsi
->vu_dev
);
591 int main(int argc
, char **argv
)
593 VusDev
*vdev_scsi
= NULL
;
594 char *unix_fn
= NULL
;
595 char *iscsi_uri
= NULL
;
596 int sock
, opt
, err
= EXIT_SUCCESS
;
598 while ((opt
= getopt(argc
, argv
, "u:i:")) != -1) {
603 unix_fn
= g_strdup(optarg
);
606 iscsi_uri
= g_strdup(optarg
);
612 if (!unix_fn
|| !iscsi_uri
) {
616 sock
= unix_sock_new(unix_fn
);
620 vdev_scsi
= vdev_scsi_new(sock
);
622 if (vus_iscsi_add_lun(&vdev_scsi
->lun
, iscsi_uri
) != 0) {
626 if (vdev_scsi_run(vdev_scsi
) != 0) {
632 vdev_scsi_free(vdev_scsi
);
645 fprintf(stderr
, "Usage: %s [ -u unix_sock_path -i iscsi_uri ] | [ -h ]\n",
647 fprintf(stderr
, " -u path to unix socket\n");
648 fprintf(stderr
, " -i iscsi uri for lun 0\n");
649 fprintf(stderr
, " -h print help and quit\n");