2 * vhost-user-scsi sample application
4 * Copyright (c) 2016 Nutanix Inc. All rights reserved.
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"
20 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
22 typedef struct VusIscsiLun {
23 struct iscsi_context *iscsi_ctx;
27 typedef struct VusDev {
34 /** libiscsi integration **/
36 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq;
37 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp;
39 static int vus_iscsi_add_lun(VusIscsiLun *lun, char *iscsi_uri)
41 struct iscsi_url *iscsi_url;
42 struct iscsi_context *iscsi_ctx;
47 assert(!lun->iscsi_ctx);
49 iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
51 g_warning("Unable to create iSCSI context");
55 iscsi_url = iscsi_parse_full_url(iscsi_ctx, iscsi_uri);
57 g_warning("Unable to parse iSCSI URL: %s", iscsi_get_error(iscsi_ctx));
61 iscsi_set_session_type(iscsi_ctx, ISCSI_SESSION_NORMAL);
62 iscsi_set_header_digest(iscsi_ctx, ISCSI_HEADER_DIGEST_NONE_CRC32C);
63 if (iscsi_full_connect_sync(iscsi_ctx, iscsi_url->portal, iscsi_url->lun)) {
64 g_warning("Unable to login to iSCSI portal: %s",
65 iscsi_get_error(iscsi_ctx));
69 lun->iscsi_ctx = iscsi_ctx;
70 lun->iscsi_lun = iscsi_url->lun;
72 g_debug("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
76 iscsi_destroy_url(iscsi_url);
81 (void)iscsi_destroy_context(iscsi_ctx);
86 static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
89 struct scsi_task *task;
94 task = g_new0(struct scsi_task, 1);
95 memcpy(task->cdb, cdb, cdb_len);
96 task->cdb_size = cdb_len;
98 task->expxferlen = xfer_len;
103 static int get_cdb_len(uint8_t *cdb)
107 switch (cdb[0] >> 5) {
109 case 1: /* fall through */
114 g_warning("Unable to determine cdb len (0x%02hhX)", cdb[0] >> 5);
118 static int handle_cmd_sync(struct iscsi_context *ctx,
119 VirtIOSCSICmdReq *req,
120 struct iovec *out, unsigned int out_len,
121 VirtIOSCSICmdResp *rsp,
122 struct iovec *in, unsigned int in_len)
124 struct scsi_task *task;
134 if (!(!req->lun[1] && req->lun[2] == 0x40 && !req->lun[3])) {
135 /* Ignore anything different than target=0, lun=0 */
136 g_debug("Ignoring unconnected lun (0x%hhX, 0x%hhX)",
137 req->lun[1], req->lun[3]);
138 rsp->status = SCSI_STATUS_CHECK_CONDITION;
139 memset(rsp->sense, 0, sizeof(rsp->sense));
141 rsp->sense[0] = 0x70;
142 rsp->sense[2] = SCSI_SENSE_ILLEGAL_REQUEST;
144 rsp->sense[12] = 0x24;
149 cdb_len = get_cdb_len(req->cdb);
155 if (!out_len && !in_len) {
156 dir = SCSI_XFER_NONE;
157 } else if (out_len) {
158 dir = SCSI_XFER_WRITE;
159 for (i = 0; i < out_len; i++) {
160 len += out[i].iov_len;
163 dir = SCSI_XFER_READ;
164 for (i = 0; i < in_len; i++) {
165 len += in[i].iov_len;
169 task = scsi_task_new(cdb_len, req->cdb, dir, len);
171 if (dir == SCSI_XFER_WRITE) {
172 task->iovector_out.iov = (struct scsi_iovec *)out;
173 task->iovector_out.niov = out_len;
174 } else if (dir == SCSI_XFER_READ) {
175 task->iovector_in.iov = (struct scsi_iovec *)in;
176 task->iovector_in.niov = in_len;
179 g_debug("Sending iscsi cmd (cdb_len=%d, dir=%d, task=%p)",
181 if (!iscsi_scsi_command_sync(ctx, 0, task, NULL)) {
182 g_warning("Error serving SCSI command");
187 memset(rsp, 0, sizeof(*rsp));
189 rsp->status = task->status;
190 rsp->resid = task->residual;
192 if (task->status == SCSI_STATUS_CHECK_CONDITION) {
193 rsp->response = VIRTIO_SCSI_S_FAILURE;
194 rsp->sense_len = task->datain.size - 2;
195 memcpy(rsp->sense, &task->datain.data[2], rsp->sense_len);
200 g_debug("Filled in rsp: status=%hhX, resid=%u, response=%hhX, sense_len=%u",
201 rsp->status, rsp->resid, rsp->response, rsp->sense_len);
206 /** libvhost-user callbacks **/
208 static void vus_panic_cb(VuDev *vu_dev, const char *buf)
215 gdev = container_of(vu_dev, VugDev, parent);
216 vdev_scsi = container_of(gdev, VusDev, parent);
218 g_warning("vu_panic: %s", buf);
221 g_main_loop_quit(vdev_scsi->loop);
224 static void vus_proc_req(VuDev *vu_dev, int idx)
232 gdev = container_of(vu_dev, VugDev, parent);
233 vdev_scsi = container_of(gdev, VusDev, parent);
234 if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
235 g_warning("VQ Index out of range: %d", idx);
236 vus_panic_cb(vu_dev, NULL);
240 vq = vu_get_queue(vu_dev, idx);
242 g_warning("Error fetching VQ (dev=%p, idx=%d)", vu_dev, idx);
243 vus_panic_cb(vu_dev, NULL);
247 g_debug("Got kicked on vq[%d]@%p", idx, vq);
250 VuVirtqElement *elem;
251 VirtIOSCSICmdReq *req;
252 VirtIOSCSICmdResp *rsp;
254 elem = vu_queue_pop(vu_dev, vq, sizeof(VuVirtqElement));
256 g_debug("No more elements pending on vq[%d]@%p", idx, vq);
259 g_debug("Popped elem@%p", elem);
261 assert(!(elem->out_num > 1 && elem->in_num > 1));
262 assert(elem->out_num > 0 && elem->in_num > 0);
264 if (elem->out_sg[0].iov_len < sizeof(VirtIOSCSICmdReq)) {
265 g_warning("Invalid virtio-scsi req header");
266 vus_panic_cb(vu_dev, NULL);
269 req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
271 if (elem->in_sg[0].iov_len < sizeof(VirtIOSCSICmdResp)) {
272 g_warning("Invalid virtio-scsi rsp header");
273 vus_panic_cb(vu_dev, NULL);
276 rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
278 if (handle_cmd_sync(vdev_scsi->lun.iscsi_ctx,
279 req, &elem->out_sg[1], elem->out_num - 1,
280 rsp, &elem->in_sg[1], elem->in_num - 1) != 0) {
281 vus_panic_cb(vu_dev, NULL);
285 vu_queue_push(vu_dev, vq, elem, 0);
286 vu_queue_notify(vu_dev, vq);
292 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
298 if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
299 g_warning("VQ Index out of range: %d", idx);
300 vus_panic_cb(vu_dev, NULL);
304 vq = vu_get_queue(vu_dev, idx);
306 if (idx == 0 || idx == 1) {
307 g_debug("queue %d unimplemented", idx);
309 vu_set_queue_handler(vu_dev, vq, started ? vus_proc_req : NULL);
313 static const VuDevIface vus_iface = {
314 .queue_set_started = vus_queue_set_started,
319 static int unix_sock_new(char *unix_fn)
322 struct sockaddr_un un;
327 sock = socket(AF_UNIX, SOCK_STREAM, 0);
333 un.sun_family = AF_UNIX;
334 (void)snprintf(un.sun_path, sizeof(un.sun_path), "%s", unix_fn);
335 len = sizeof(un.sun_family) + strlen(un.sun_path);
337 (void)unlink(unix_fn);
338 if (bind(sock, (struct sockaddr *)&un, len) < 0) {
343 if (listen(sock, 1) < 0) {
356 /** vhost-user-scsi **/
358 int main(int argc, char **argv)
360 VusDev *vdev_scsi = NULL;
361 char *unix_fn = NULL;
362 char *iscsi_uri = NULL;
363 int lsock = -1, csock = -1, opt, err = EXIT_SUCCESS;
365 while ((opt = getopt(argc, argv, "u:i:")) != -1) {
370 unix_fn = g_strdup(optarg);
373 iscsi_uri = g_strdup(optarg);
379 if (!unix_fn || !iscsi_uri) {
383 lsock = unix_sock_new(unix_fn);
388 csock = accept(lsock, NULL, NULL);
394 vdev_scsi = g_new0(VusDev, 1);
395 vdev_scsi->loop = g_main_loop_new(NULL, FALSE);
397 if (vus_iscsi_add_lun(&vdev_scsi->lun, iscsi_uri) != 0) {
401 vug_init(&vdev_scsi->parent, csock, vus_panic_cb, &vus_iface);
403 g_main_loop_run(vdev_scsi->loop);
405 vug_deinit(&vdev_scsi->parent);
409 g_main_loop_unref(vdev_scsi->loop);
429 fprintf(stderr, "Usage: %s [ -u unix_sock_path -i iscsi_uri ] | [ -h ]\n",
431 fprintf(stderr, " -u path to unix socket\n");
432 fprintf(stderr, " -i iscsi uri for lun 0\n");
433 fprintf(stderr, " -h print help and quit\n");