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