1 /*
2  * vhost-user-scsi sample application
3  *
4  * Copyright (c) 2016 Nutanix Inc. All rights reserved.
5  *
6  * Author:
7  *  Felipe Franciosi <felipe@nutanix.com>
8  *
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.
11  */
12 
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"
18 
19 #include <glib.h>
20 
21 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
22 
23 typedef struct VusIscsiLun {
24     struct iscsi_context *iscsi_ctx;
25     int iscsi_lun;
26 } VusIscsiLun;
27 
28 typedef struct VusDev {
29     VugDev parent;
30 
31     VusIscsiLun lun;
32     GMainLoop *loop;
33 } VusDev;
34 
35 /** libiscsi integration **/
36 
37 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq;
38 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp;
39 
40 static int vus_iscsi_add_lun(VusIscsiLun *lun, char *iscsi_uri)
41 {
42     struct iscsi_url *iscsi_url;
43     struct iscsi_context *iscsi_ctx;
44     int ret = 0;
45 
46     assert(lun);
47     assert(iscsi_uri);
48     assert(!lun->iscsi_ctx);
49 
50     iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
51     if (!iscsi_ctx) {
52         g_warning("Unable to create iSCSI context");
53         return -1;
54     }
55 
56     iscsi_url = iscsi_parse_full_url(iscsi_ctx, iscsi_uri);
57     if (!iscsi_url) {
58         g_warning("Unable to parse iSCSI URL: %s", iscsi_get_error(iscsi_ctx));
59         goto fail;
60     }
61 
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));
67         goto fail;
68     }
69 
70     lun->iscsi_ctx = iscsi_ctx;
71     lun->iscsi_lun = iscsi_url->lun;
72 
73     g_debug("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
74 
75 out:
76     if (iscsi_url) {
77         iscsi_destroy_url(iscsi_url);
78     }
79     return ret;
80 
81 fail:
82     (void)iscsi_destroy_context(iscsi_ctx);
83     ret = -1;
84     goto out;
85 }
86 
87 static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
88                                        int xfer_len)
89 {
90     struct scsi_task *task;
91 
92     assert(cdb_len > 0);
93     assert(cdb);
94 
95     task = g_new0(struct scsi_task, 1);
96     memcpy(task->cdb, cdb, cdb_len);
97     task->cdb_size = cdb_len;
98     task->xfer_dir = dir;
99     task->expxferlen = xfer_len;
100 
101     return task;
102 }
103 
104 static int get_cdb_len(uint8_t *cdb)
105 {
106     assert(cdb);
107 
108     switch (cdb[0] >> 5) {
109     case 0: return 6;
110     case 1: /* fall through */
111     case 2: return 10;
112     case 4: return 16;
113     case 5: return 12;
114     }
115     g_warning("Unable to determine cdb len (0x%02hhX)", cdb[0] >> 5);
116     return -1;
117 }
118 
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)
124 {
125     struct scsi_task *task;
126     uint32_t dir;
127     uint32_t len;
128     int cdb_len;
129     int i;
130 
131     assert(ctx);
132     assert(req);
133     assert(rsp);
134 
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));
141         rsp->sense_len = 18;
142         rsp->sense[0] = 0x70;
143         rsp->sense[2] = SCSI_SENSE_ILLEGAL_REQUEST;
144         rsp->sense[7] = 10;
145         rsp->sense[12] = 0x24;
146 
147         return 0;
148     }
149 
150     cdb_len = get_cdb_len(req->cdb);
151     if (cdb_len == -1) {
152         return -1;
153     }
154 
155     len = 0;
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;
162         }
163     } else {
164         dir = SCSI_XFER_READ;
165         for (i = 0; i < in_len; i++) {
166             len += in[i].iov_len;
167         }
168     }
169 
170     task = scsi_task_new(cdb_len, req->cdb, dir, len);
171 
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;
178     }
179 
180     g_debug("Sending iscsi cmd (cdb_len=%d, dir=%d, task=%p)",
181          cdb_len, dir, task);
182     if (!iscsi_scsi_command_sync(ctx, 0, task, NULL)) {
183         g_warning("Error serving SCSI command");
184         g_free(task);
185         return -1;
186     }
187 
188     memset(rsp, 0, sizeof(*rsp));
189 
190     rsp->status = task->status;
191     rsp->resid  = task->residual;
192 
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);
197     }
198 
199     g_free(task);
200 
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);
203 
204     return 0;
205 }
206 
207 /** libvhost-user callbacks **/
208 
209 static void vus_panic_cb(VuDev *vu_dev, const char *buf)
210 {
211     VugDev *gdev;
212     VusDev *vdev_scsi;
213 
214     assert(vu_dev);
215 
216     gdev = container_of(vu_dev, VugDev, parent);
217     vdev_scsi = container_of(gdev, VusDev, parent);
218     if (buf) {
219         g_warning("vu_panic: %s", buf);
220     }
221 
222     g_main_loop_quit(vdev_scsi->loop);
223 }
224 
225 static void vus_proc_req(VuDev *vu_dev, int idx)
226 {
227     VugDev *gdev;
228     VusDev *vdev_scsi;
229     VuVirtq *vq;
230 
231     assert(vu_dev);
232 
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);
238         return;
239     }
240 
241     vq = vu_get_queue(vu_dev, idx);
242     if (!vq) {
243         g_warning("Error fetching VQ (dev=%p, idx=%d)", vu_dev, idx);
244         vus_panic_cb(vu_dev, NULL);
245         return;
246     }
247 
248     g_debug("Got kicked on vq[%d]@%p", idx, vq);
249 
250     while (1) {
251         VuVirtqElement *elem;
252         VirtIOSCSICmdReq *req;
253         VirtIOSCSICmdResp *rsp;
254 
255         elem = vu_queue_pop(vu_dev, vq, sizeof(VuVirtqElement));
256         if (!elem) {
257             g_debug("No more elements pending on vq[%d]@%p", idx, vq);
258             break;
259         }
260         g_debug("Popped elem@%p", elem);
261 
262         assert(!(elem->out_num > 1 && elem->in_num > 1));
263         assert(elem->out_num > 0 && elem->in_num > 0);
264 
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);
268             break;
269         }
270         req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
271 
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);
275             break;
276         }
277         rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
278 
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);
283             break;
284         }
285 
286         vu_queue_push(vu_dev, vq, elem, 0);
287         vu_queue_notify(vu_dev, vq);
288 
289         free(elem);
290     }
291 }
292 
293 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
294 {
295     VuVirtq *vq;
296 
297     assert(vu_dev);
298 
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);
302         return;
303     }
304 
305     vq = vu_get_queue(vu_dev, idx);
306 
307     if (idx == 0 || idx == 1) {
308         g_debug("queue %d unimplemented", idx);
309     } else {
310         vu_set_queue_handler(vu_dev, vq, started ? vus_proc_req : NULL);
311     }
312 }
313 
314 static const VuDevIface vus_iface = {
315     .queue_set_started = vus_queue_set_started,
316 };
317 
318 /** misc helpers **/
319 
320 static int unix_sock_new(char *unix_fn)
321 {
322     int sock;
323     struct sockaddr_un un;
324     size_t len;
325 
326     assert(unix_fn);
327 
328     sock = socket(AF_UNIX, SOCK_STREAM, 0);
329     if (sock <= 0) {
330         perror("socket");
331         return -1;
332     }
333 
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);
337 
338     (void)unlink(unix_fn);
339     if (bind(sock, (struct sockaddr *)&un, len) < 0) {
340         perror("bind");
341         goto fail;
342     }
343 
344     if (listen(sock, 1) < 0) {
345         perror("listen");
346         goto fail;
347     }
348 
349     return sock;
350 
351 fail:
352     (void)close(sock);
353 
354     return -1;
355 }
356 
357 /** vhost-user-scsi **/
358 
359 int main(int argc, char **argv)
360 {
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;
365 
366     while ((opt = getopt(argc, argv, "u:i:")) != -1) {
367         switch (opt) {
368         case 'h':
369             goto help;
370         case 'u':
371             unix_fn = g_strdup(optarg);
372             break;
373         case 'i':
374             iscsi_uri = g_strdup(optarg);
375             break;
376         default:
377             goto help;
378         }
379     }
380     if (!unix_fn || !iscsi_uri) {
381         goto help;
382     }
383 
384     lsock = unix_sock_new(unix_fn);
385     if (lsock < 0) {
386         goto err;
387     }
388 
389     csock = accept(lsock, NULL, NULL);
390     if (csock < 0) {
391         perror("accept");
392         goto err;
393     }
394 
395     vdev_scsi = g_new0(VusDev, 1);
396     vdev_scsi->loop = g_main_loop_new(NULL, FALSE);
397 
398     if (vus_iscsi_add_lun(&vdev_scsi->lun, iscsi_uri) != 0) {
399         goto err;
400     }
401 
402     vug_init(&vdev_scsi->parent, csock, vus_panic_cb, &vus_iface);
403 
404     g_main_loop_run(vdev_scsi->loop);
405 
406     vug_deinit(&vdev_scsi->parent);
407 
408 out:
409     if (vdev_scsi) {
410         g_main_loop_unref(vdev_scsi->loop);
411         g_free(vdev_scsi);
412         unlink(unix_fn);
413     }
414     if (csock >= 0) {
415         close(csock);
416     }
417     if (lsock >= 0) {
418         close(lsock);
419     }
420     g_free(unix_fn);
421     g_free(iscsi_uri);
422 
423     return err;
424 
425 err:
426     err = EXIT_FAILURE;
427     goto out;
428 
429 help:
430     fprintf(stderr, "Usage: %s [ -u unix_sock_path -i iscsi_uri ] | [ -h ]\n",
431             argv[0]);
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");
435 
436     goto err;
437 }
438