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 <iscsi/iscsi.h>
15 #include <iscsi/scsi-lowlevel.h>
16 #include "contrib/libvhost-user/libvhost-user-glib.h"
17 #include "standard-headers/linux/virtio_scsi.h"
21 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
23 typedef struct VusIscsiLun
{
24 struct iscsi_context
*iscsi_ctx
;
28 typedef struct VusDev
{
35 /** libiscsi integration **/
37 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq
;
38 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp
;
40 static int vus_iscsi_add_lun(VusIscsiLun
*lun
, char *iscsi_uri
)
42 struct iscsi_url
*iscsi_url
;
43 struct iscsi_context
*iscsi_ctx
;
48 assert(!lun
->iscsi_ctx
);
50 iscsi_ctx
= iscsi_create_context(VUS_ISCSI_INITIATOR
);
52 g_warning("Unable to create iSCSI context");
56 iscsi_url
= iscsi_parse_full_url(iscsi_ctx
, iscsi_uri
);
58 g_warning("Unable to parse iSCSI URL: %s", iscsi_get_error(iscsi_ctx
));
62 iscsi_set_session_type(iscsi_ctx
, ISCSI_SESSION_NORMAL
);
63 iscsi_set_header_digest(iscsi_ctx
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
64 if (iscsi_full_connect_sync(iscsi_ctx
, iscsi_url
->portal
, iscsi_url
->lun
)) {
65 g_warning("Unable to login to iSCSI portal: %s",
66 iscsi_get_error(iscsi_ctx
));
70 lun
->iscsi_ctx
= iscsi_ctx
;
71 lun
->iscsi_lun
= iscsi_url
->lun
;
73 g_debug("Context %p created for lun 0: %s", iscsi_ctx
, iscsi_uri
);
77 iscsi_destroy_url(iscsi_url
);
82 (void)iscsi_destroy_context(iscsi_ctx
);
87 static struct scsi_task
*scsi_task_new(int cdb_len
, uint8_t *cdb
, int dir
,
90 struct scsi_task
*task
;
95 task
= g_new0(struct scsi_task
, 1);
96 memcpy(task
->cdb
, cdb
, cdb_len
);
97 task
->cdb_size
= cdb_len
;
99 task
->expxferlen
= xfer_len
;
104 static int get_cdb_len(uint8_t *cdb
)
108 switch (cdb
[0] >> 5) {
110 case 1: /* fall through */
115 g_warning("Unable to determine cdb len (0x%02hhX)", cdb
[0] >> 5);
119 static int handle_cmd_sync(struct iscsi_context
*ctx
,
120 VirtIOSCSICmdReq
*req
,
121 struct iovec
*out
, unsigned int out_len
,
122 VirtIOSCSICmdResp
*rsp
,
123 struct iovec
*in
, unsigned int in_len
)
125 struct scsi_task
*task
;
135 if (!(!req
->lun
[1] && req
->lun
[2] == 0x40 && !req
->lun
[3])) {
136 /* Ignore anything different than target=0, lun=0 */
137 g_debug("Ignoring unconnected lun (0x%hhX, 0x%hhX)",
138 req
->lun
[1], req
->lun
[3]);
139 rsp
->status
= SCSI_STATUS_CHECK_CONDITION
;
140 memset(rsp
->sense
, 0, sizeof(rsp
->sense
));
142 rsp
->sense
[0] = 0x70;
143 rsp
->sense
[2] = SCSI_SENSE_ILLEGAL_REQUEST
;
145 rsp
->sense
[12] = 0x24;
150 cdb_len
= get_cdb_len(req
->cdb
);
156 if (!out_len
&& !in_len
) {
157 dir
= SCSI_XFER_NONE
;
158 } else if (out_len
) {
159 dir
= SCSI_XFER_WRITE
;
160 for (i
= 0; i
< out_len
; i
++) {
161 len
+= out
[i
].iov_len
;
164 dir
= SCSI_XFER_READ
;
165 for (i
= 0; i
< in_len
; i
++) {
166 len
+= in
[i
].iov_len
;
170 task
= scsi_task_new(cdb_len
, req
->cdb
, dir
, len
);
172 if (dir
== SCSI_XFER_WRITE
) {
173 task
->iovector_out
.iov
= (struct scsi_iovec
*)out
;
174 task
->iovector_out
.niov
= out_len
;
175 } else if (dir
== SCSI_XFER_READ
) {
176 task
->iovector_in
.iov
= (struct scsi_iovec
*)in
;
177 task
->iovector_in
.niov
= in_len
;
180 g_debug("Sending iscsi cmd (cdb_len=%d, dir=%d, task=%p)",
182 if (!iscsi_scsi_command_sync(ctx
, 0, task
, NULL
)) {
183 g_warning("Error serving SCSI command");
188 memset(rsp
, 0, sizeof(*rsp
));
190 rsp
->status
= task
->status
;
191 rsp
->resid
= task
->residual
;
193 if (task
->status
== SCSI_STATUS_CHECK_CONDITION
) {
194 rsp
->response
= VIRTIO_SCSI_S_FAILURE
;
195 rsp
->sense_len
= task
->datain
.size
- 2;
196 memcpy(rsp
->sense
, &task
->datain
.data
[2], rsp
->sense_len
);
201 g_debug("Filled in rsp: status=%hhX, resid=%u, response=%hhX, sense_len=%u",
202 rsp
->status
, rsp
->resid
, rsp
->response
, rsp
->sense_len
);
207 /** libvhost-user callbacks **/
209 static void vus_panic_cb(VuDev
*vu_dev
, const char *buf
)
216 gdev
= container_of(vu_dev
, VugDev
, parent
);
217 vdev_scsi
= container_of(gdev
, VusDev
, parent
);
219 g_warning("vu_panic: %s", buf
);
222 g_main_loop_quit(vdev_scsi
->loop
);
225 static void vus_proc_req(VuDev
*vu_dev
, int idx
)
233 gdev
= container_of(vu_dev
, VugDev
, parent
);
234 vdev_scsi
= container_of(gdev
, VusDev
, parent
);
235 if (idx
< 0 || idx
>= VHOST_MAX_NR_VIRTQUEUE
) {
236 g_warning("VQ Index out of range: %d", idx
);
237 vus_panic_cb(vu_dev
, NULL
);
241 vq
= vu_get_queue(vu_dev
, idx
);
243 g_warning("Error fetching VQ (dev=%p, idx=%d)", vu_dev
, idx
);
244 vus_panic_cb(vu_dev
, NULL
);
248 g_debug("Got kicked on vq[%d]@%p", idx
, vq
);
251 VuVirtqElement
*elem
;
252 VirtIOSCSICmdReq
*req
;
253 VirtIOSCSICmdResp
*rsp
;
255 elem
= vu_queue_pop(vu_dev
, vq
, sizeof(VuVirtqElement
));
257 g_debug("No more elements pending on vq[%d]@%p", idx
, vq
);
260 g_debug("Popped elem@%p", elem
);
262 assert(!(elem
->out_num
> 1 && elem
->in_num
> 1));
263 assert(elem
->out_num
> 0 && elem
->in_num
> 0);
265 if (elem
->out_sg
[0].iov_len
< sizeof(VirtIOSCSICmdReq
)) {
266 g_warning("Invalid virtio-scsi req header");
267 vus_panic_cb(vu_dev
, NULL
);
270 req
= (VirtIOSCSICmdReq
*)elem
->out_sg
[0].iov_base
;
272 if (elem
->in_sg
[0].iov_len
< sizeof(VirtIOSCSICmdResp
)) {
273 g_warning("Invalid virtio-scsi rsp header");
274 vus_panic_cb(vu_dev
, NULL
);
277 rsp
= (VirtIOSCSICmdResp
*)elem
->in_sg
[0].iov_base
;
279 if (handle_cmd_sync(vdev_scsi
->lun
.iscsi_ctx
,
280 req
, &elem
->out_sg
[1], elem
->out_num
- 1,
281 rsp
, &elem
->in_sg
[1], elem
->in_num
- 1) != 0) {
282 vus_panic_cb(vu_dev
, NULL
);
286 vu_queue_push(vu_dev
, vq
, elem
, 0);
287 vu_queue_notify(vu_dev
, vq
);
293 static void vus_queue_set_started(VuDev
*vu_dev
, int idx
, bool started
)
299 if (idx
< 0 || idx
>= VHOST_MAX_NR_VIRTQUEUE
) {
300 g_warning("VQ Index out of range: %d", idx
);
301 vus_panic_cb(vu_dev
, NULL
);
305 vq
= vu_get_queue(vu_dev
, idx
);
307 if (idx
== 0 || idx
== 1) {
308 g_debug("queue %d unimplemented", idx
);
310 vu_set_queue_handler(vu_dev
, vq
, started
? vus_proc_req
: NULL
);
314 static const VuDevIface vus_iface
= {
315 .queue_set_started
= vus_queue_set_started
,
320 static int unix_sock_new(char *unix_fn
)
323 struct sockaddr_un un
;
328 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
334 un
.sun_family
= AF_UNIX
;
335 (void)snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", unix_fn
);
336 len
= sizeof(un
.sun_family
) + strlen(un
.sun_path
);
338 (void)unlink(unix_fn
);
339 if (bind(sock
, (struct sockaddr
*)&un
, len
) < 0) {
344 if (listen(sock
, 1) < 0) {
357 /** vhost-user-scsi **/
359 int main(int argc
, char **argv
)
361 VusDev
*vdev_scsi
= NULL
;
362 char *unix_fn
= NULL
;
363 char *iscsi_uri
= NULL
;
364 int lsock
= -1, csock
= -1, opt
, err
= EXIT_SUCCESS
;
366 while ((opt
= getopt(argc
, argv
, "u:i:")) != -1) {
371 unix_fn
= g_strdup(optarg
);
374 iscsi_uri
= g_strdup(optarg
);
380 if (!unix_fn
|| !iscsi_uri
) {
384 lsock
= unix_sock_new(unix_fn
);
389 csock
= accept(lsock
, NULL
, NULL
);
395 vdev_scsi
= g_new0(VusDev
, 1);
396 vdev_scsi
->loop
= g_main_loop_new(NULL
, FALSE
);
398 if (vus_iscsi_add_lun(&vdev_scsi
->lun
, iscsi_uri
) != 0) {
402 vug_init(&vdev_scsi
->parent
, csock
, vus_panic_cb
, &vus_iface
);
404 g_main_loop_run(vdev_scsi
->loop
);
406 vug_deinit(&vdev_scsi
->parent
);
410 g_main_loop_unref(vdev_scsi
->loop
);
430 fprintf(stderr
, "Usage: %s [ -u unix_sock_path -i iscsi_uri ] | [ -h ]\n",
432 fprintf(stderr
, " -u path to unix socket\n");
433 fprintf(stderr
, " -i iscsi uri for lun 0\n");
434 fprintf(stderr
, " -h print help and quit\n");