vhost-user-scsi: assert() in iscsi_add_lun()
[qemu.git] / contrib / vhost-user-scsi / vhost-user-scsi.c
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 "contrib/libvhost-user/libvhost-user.h"
15 #include "hw/virtio/virtio-scsi.h"
16 #include "iscsi/iscsi.h"
17
18 #include <glib.h>
19
20 /* #define VUS_DEBUG 1 */
21
22 /** Log helpers **/
23
24 #define PPRE \
25 struct timespec ts; \
26 char timebuf[64]; \
27 struct tm tm; \
28 (void)clock_gettime(CLOCK_REALTIME, &ts); \
29 (void)strftime(timebuf, 64, "%Y%m%d %T", gmtime_r(&ts.tv_sec, &tm))
30
31 #define PEXT(lvl, msg, ...) do { \
32 PPRE; \
33 fprintf(stderr, "%s.%06ld " lvl ": %s:%s():%d: " msg "\n", \
34 timebuf, ts.tv_nsec / 1000, \
35 __FILE__, __func__, __LINE__, ## __VA_ARGS__); \
36 } while (0)
37
38 #define PNOR(lvl, msg, ...) do { \
39 PPRE; \
40 fprintf(stderr, "%s.%06ld " lvl ": " msg "\n", \
41 timebuf, ts.tv_nsec / 1000, ## __VA_ARGS__); \
42 } while (0)
43
44 #ifdef VUS_DEBUG
45 #define PDBG(msg, ...) PEXT("DBG", msg, ## __VA_ARGS__)
46 #define PERR(msg, ...) PEXT("ERR", msg, ## __VA_ARGS__)
47 #define PLOG(msg, ...) PEXT("LOG", msg, ## __VA_ARGS__)
48 #else
49 #define PDBG(msg, ...) { }
50 #define PERR(msg, ...) PNOR("ERR", msg, ## __VA_ARGS__)
51 #define PLOG(msg, ...) PNOR("LOG", msg, ## __VA_ARGS__)
52 #endif
53
54 /** vhost-user-scsi specific definitions **/
55
56 /* Only 1 LUN and device supported today */
57 #define VUS_MAX_LUNS 1
58 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
59
60 typedef struct iscsi_lun {
61 struct iscsi_context *iscsi_ctx;
62 int iscsi_lun;
63 } iscsi_lun_t;
64
65 typedef struct vhost_scsi_dev {
66 VuDev vu_dev;
67 int server_sock;
68 GMainLoop *loop;
69 GTree *fdmap; /* fd -> gsource context id */
70 iscsi_lun_t luns[VUS_MAX_LUNS];
71 } vhost_scsi_dev_t;
72
73 /** glib event loop integration for libvhost-user and misc callbacks **/
74
75 QEMU_BUILD_BUG_ON((int)G_IO_IN != (int)VU_WATCH_IN);
76 QEMU_BUILD_BUG_ON((int)G_IO_OUT != (int)VU_WATCH_OUT);
77 QEMU_BUILD_BUG_ON((int)G_IO_PRI != (int)VU_WATCH_PRI);
78 QEMU_BUILD_BUG_ON((int)G_IO_ERR != (int)VU_WATCH_ERR);
79 QEMU_BUILD_BUG_ON((int)G_IO_HUP != (int)VU_WATCH_HUP);
80
81 typedef struct vus_gsrc {
82 GSource parent;
83 vhost_scsi_dev_t *vdev_scsi;
84 GPollFD gfd;
85 vu_watch_cb vu_cb;
86 } vus_gsrc_t;
87
88 static gint vus_fdmap_compare(gconstpointer a, gconstpointer b)
89 {
90 return (b > a) - (b < a);
91 }
92
93 static gboolean vus_gsrc_prepare(GSource *src, gint *timeout)
94 {
95 assert(timeout);
96
97 *timeout = -1;
98 return FALSE;
99 }
100
101 static gboolean vus_gsrc_check(GSource *src)
102 {
103 vus_gsrc_t *vus_src = (vus_gsrc_t *)src;
104
105 assert(vus_src);
106
107 return vus_src->gfd.revents & vus_src->gfd.events;
108 }
109
110 static gboolean vus_gsrc_dispatch(GSource *src, GSourceFunc cb, gpointer data)
111 {
112 vhost_scsi_dev_t *vdev_scsi;
113 vus_gsrc_t *vus_src = (vus_gsrc_t *)src;
114
115 assert(vus_src);
116 assert(!(vus_src->vu_cb && cb));
117
118 vdev_scsi = vus_src->vdev_scsi;
119
120 assert(vdev_scsi);
121
122 if (cb) {
123 return cb(data);
124 }
125 if (vus_src->vu_cb) {
126 vus_src->vu_cb(&vdev_scsi->vu_dev, vus_src->gfd.revents, data);
127 }
128 return G_SOURCE_CONTINUE;
129 }
130
131 static GSourceFuncs vus_gsrc_funcs = {
132 vus_gsrc_prepare,
133 vus_gsrc_check,
134 vus_gsrc_dispatch,
135 NULL
136 };
137
138 static void vus_gsrc_new(vhost_scsi_dev_t *vdev_scsi, int fd, GIOCondition cond,
139 vu_watch_cb vu_cb, GSourceFunc gsrc_cb, gpointer data)
140 {
141 GSource *vus_gsrc;
142 vus_gsrc_t *vus_src;
143 guint id;
144
145 assert(vdev_scsi);
146 assert(fd >= 0);
147 assert(vu_cb || gsrc_cb);
148 assert(!(vu_cb && gsrc_cb));
149
150 vus_gsrc = g_source_new(&vus_gsrc_funcs, sizeof(vus_gsrc_t));
151 vus_src = (vus_gsrc_t *)vus_gsrc;
152
153 vus_src->vdev_scsi = vdev_scsi;
154 vus_src->gfd.fd = fd;
155 vus_src->gfd.events = cond;
156 vus_src->vu_cb = vu_cb;
157
158 g_source_add_poll(vus_gsrc, &vus_src->gfd);
159 g_source_set_callback(vus_gsrc, gsrc_cb, data, NULL);
160 id = g_source_attach(vus_gsrc, NULL);
161 assert(id);
162 g_source_unref(vus_gsrc);
163
164 g_tree_insert(vdev_scsi->fdmap, (gpointer)(uintptr_t)fd,
165 (gpointer)(uintptr_t)id);
166 }
167
168 /* from libiscsi's scsi-lowlevel.h **
169 *
170 * nb. We can't directly include scsi-lowlevel.h due to a namespace conflict:
171 * QEMU's scsi.h also defines "SCSI_XFER_NONE".
172 */
173
174 #define SCSI_CDB_MAX_SIZE 16
175
176 struct scsi_iovector {
177 struct scsi_iovec *iov;
178 int niov;
179 int nalloc;
180 size_t offset;
181 int consumed;
182 };
183
184 struct scsi_allocated_memory {
185 struct scsi_allocated_memory *next;
186 char buf[0];
187 };
188
189 struct scsi_data {
190 int size;
191 unsigned char *data;
192 };
193
194 enum scsi_sense_key {
195 SCSI_SENSE_NO_SENSE = 0x00,
196 SCSI_SENSE_RECOVERED_ERROR = 0x01,
197 SCSI_SENSE_NOT_READY = 0x02,
198 SCSI_SENSE_MEDIUM_ERROR = 0x03,
199 SCSI_SENSE_HARDWARE_ERROR = 0x04,
200 SCSI_SENSE_ILLEGAL_REQUEST = 0x05,
201 SCSI_SENSE_UNIT_ATTENTION = 0x06,
202 SCSI_SENSE_DATA_PROTECTION = 0x07,
203 SCSI_SENSE_BLANK_CHECK = 0x08,
204 SCSI_SENSE_VENDOR_SPECIFIC = 0x09,
205 SCSI_SENSE_COPY_ABORTED = 0x0a,
206 SCSI_SENSE_COMMAND_ABORTED = 0x0b,
207 SCSI_SENSE_OBSOLETE_ERROR_CODE = 0x0c,
208 SCSI_SENSE_OVERFLOW_COMMAND = 0x0d,
209 SCSI_SENSE_MISCOMPARE = 0x0e
210 };
211
212 struct scsi_sense {
213 unsigned char error_type;
214 enum scsi_sense_key key;
215 int ascq;
216 unsigned sense_specific:1;
217 unsigned ill_param_in_cdb:1;
218 unsigned bit_pointer_valid:1;
219 unsigned char bit_pointer;
220 uint16_t field_pointer;
221 };
222
223 enum scsi_residual {
224 SCSI_RESIDUAL_NO_RESIDUAL = 0,
225 SCSI_RESIDUAL_UNDERFLOW,
226 SCSI_RESIDUAL_OVERFLOW
227 };
228
229 struct scsi_task {
230 int status;
231 int cdb_size;
232 int xfer_dir;
233 int expxferlen;
234 unsigned char cdb[SCSI_CDB_MAX_SIZE];
235 enum scsi_residual residual_status;
236 size_t residual;
237 struct scsi_sense sense;
238 struct scsi_data datain;
239 struct scsi_allocated_memory *mem;
240 void *ptr;
241
242 uint32_t itt;
243 uint32_t cmdsn;
244 uint32_t lun;
245
246 struct scsi_iovector iovector_in;
247 struct scsi_iovector iovector_out;
248 };
249
250 /** libiscsi integration **/
251
252 static int iscsi_add_lun(iscsi_lun_t *lun, char *iscsi_uri)
253 {
254 struct iscsi_url *iscsi_url;
255 struct iscsi_context *iscsi_ctx;
256 int ret = 0;
257
258 assert(lun);
259 assert(iscsi_uri);
260 assert(!lun->iscsi_ctx);
261
262 iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
263 if (!iscsi_ctx) {
264 PERR("Unable to create iSCSI context");
265 return -1;
266 }
267
268 iscsi_url = iscsi_parse_full_url(iscsi_ctx, iscsi_uri);
269 if (!iscsi_url) {
270 PERR("Unable to parse iSCSI URL: %s", iscsi_get_error(iscsi_ctx));
271 goto fail;
272 }
273
274 iscsi_set_session_type(iscsi_ctx, ISCSI_SESSION_NORMAL);
275 iscsi_set_header_digest(iscsi_ctx, ISCSI_HEADER_DIGEST_NONE_CRC32C);
276 if (iscsi_full_connect_sync(iscsi_ctx, iscsi_url->portal, iscsi_url->lun)) {
277 PERR("Unable to login to iSCSI portal: %s", iscsi_get_error(iscsi_ctx));
278 goto fail;
279 }
280
281 lun->iscsi_ctx = iscsi_ctx;
282 lun->iscsi_lun = iscsi_url->lun;
283
284 PDBG("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
285
286 out:
287 if (iscsi_url) {
288 iscsi_destroy_url(iscsi_url);
289 }
290 return ret;
291
292 fail:
293 (void)iscsi_destroy_context(iscsi_ctx);
294 ret = -1;
295 goto out;
296 }
297
298 static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
299 int xfer_len)
300 {
301 struct scsi_task *task;
302
303 assert(cdb_len > 0);
304 assert(cdb);
305
306 task = g_new0(struct scsi_task, 1);
307 memcpy(task->cdb, cdb, cdb_len);
308 task->cdb_size = cdb_len;
309 task->xfer_dir = dir;
310 task->expxferlen = xfer_len;
311
312 return task;
313 }
314
315 static int get_cdb_len(uint8_t *cdb)
316 {
317 assert(cdb);
318
319 switch (cdb[0] >> 5) {
320 case 0: return 6;
321 case 1: /* fall through */
322 case 2: return 10;
323 case 4: return 16;
324 case 5: return 12;
325 }
326 PERR("Unable to determine cdb len (0x%02hhX)", cdb[0] >> 5);
327 return -1;
328 }
329
330 static int handle_cmd_sync(struct iscsi_context *ctx,
331 VirtIOSCSICmdReq *req,
332 struct iovec *out, unsigned int out_len,
333 VirtIOSCSICmdResp *rsp,
334 struct iovec *in, unsigned int in_len)
335 {
336 struct scsi_task *task;
337 uint32_t dir;
338 uint32_t len;
339 int cdb_len;
340 int i;
341
342 assert(ctx);
343 assert(req);
344 assert(rsp);
345
346 if (!(!req->lun[1] && req->lun[2] == 0x40 && !req->lun[3])) {
347 /* Ignore anything different than target=0, lun=0 */
348 PDBG("Ignoring unconnected lun (0x%hhX, 0x%hhX)",
349 req->lun[1], req->lun[3]);
350 rsp->status = SCSI_STATUS_CHECK_CONDITION;
351 memset(rsp->sense, 0, sizeof(rsp->sense));
352 rsp->sense_len = 18;
353 rsp->sense[0] = 0x70;
354 rsp->sense[2] = SCSI_SENSE_ILLEGAL_REQUEST;
355 rsp->sense[7] = 10;
356 rsp->sense[12] = 0x24;
357
358 return 0;
359 }
360
361 cdb_len = get_cdb_len(req->cdb);
362 if (cdb_len == -1) {
363 return -1;
364 }
365
366 len = 0;
367 if (!out_len && !in_len) {
368 dir = SCSI_XFER_NONE;
369 } else if (out_len) {
370 dir = SCSI_XFER_TO_DEV;
371 for (i = 0; i < out_len; i++) {
372 len += out[i].iov_len;
373 }
374 } else {
375 dir = SCSI_XFER_FROM_DEV;
376 for (i = 0; i < in_len; i++) {
377 len += in[i].iov_len;
378 }
379 }
380
381 task = scsi_task_new(cdb_len, req->cdb, dir, len);
382
383 if (dir == SCSI_XFER_TO_DEV) {
384 task->iovector_out.iov = (struct scsi_iovec *)out;
385 task->iovector_out.niov = out_len;
386 } else if (dir == SCSI_XFER_FROM_DEV) {
387 task->iovector_in.iov = (struct scsi_iovec *)in;
388 task->iovector_in.niov = in_len;
389 }
390
391 PDBG("Sending iscsi cmd (cdb_len=%d, dir=%d, task=%p)",
392 cdb_len, dir, task);
393 if (!iscsi_scsi_command_sync(ctx, 0, task, NULL)) {
394 PERR("Error serving SCSI command");
395 g_free(task);
396 return -1;
397 }
398
399 memset(rsp, 0, sizeof(*rsp));
400
401 rsp->status = task->status;
402 rsp->resid = task->residual;
403
404 if (task->status == SCSI_STATUS_CHECK_CONDITION) {
405 rsp->response = VIRTIO_SCSI_S_FAILURE;
406 rsp->sense_len = task->datain.size - 2;
407 memcpy(rsp->sense, &task->datain.data[2], rsp->sense_len);
408 }
409
410 g_free(task);
411
412 PDBG("Filled in rsp: status=%hhX, resid=%u, response=%hhX, sense_len=%u",
413 rsp->status, rsp->resid, rsp->response, rsp->sense_len);
414
415 return 0;
416 }
417
418 /** libvhost-user callbacks **/
419
420 static void vus_panic_cb(VuDev *vu_dev, const char *buf)
421 {
422 vhost_scsi_dev_t *vdev_scsi;
423
424 assert(vu_dev);
425
426 vdev_scsi = container_of(vu_dev, vhost_scsi_dev_t, vu_dev);
427 if (buf) {
428 PERR("vu_panic: %s", buf);
429 }
430
431 g_main_loop_quit(vdev_scsi->loop);
432 }
433
434 static void vus_add_watch_cb(VuDev *vu_dev, int fd, int vu_evt, vu_watch_cb cb,
435 void *pvt)
436 {
437 vhost_scsi_dev_t *vdev_scsi;
438 guint id;
439
440 assert(vu_dev);
441 assert(fd >= 0);
442 assert(cb);
443
444 vdev_scsi = container_of(vu_dev, vhost_scsi_dev_t, vu_dev);
445 id = (guint)(uintptr_t)g_tree_lookup(vdev_scsi->fdmap,
446 (gpointer)(uintptr_t)fd);
447 if (id) {
448 GSource *vus_src = g_main_context_find_source_by_id(NULL, id);
449 assert(vus_src);
450 g_source_destroy(vus_src);
451 (void)g_tree_remove(vdev_scsi->fdmap, (gpointer)(uintptr_t)fd);
452 }
453
454 vus_gsrc_new(vdev_scsi, fd, vu_evt, cb, NULL, pvt);
455 }
456
457 static void vus_del_watch_cb(VuDev *vu_dev, int fd)
458 {
459 vhost_scsi_dev_t *vdev_scsi;
460 guint id;
461
462 assert(vu_dev);
463 assert(fd >= 0);
464
465 vdev_scsi = container_of(vu_dev, vhost_scsi_dev_t, vu_dev);
466 id = (guint)(uintptr_t)g_tree_lookup(vdev_scsi->fdmap,
467 (gpointer)(uintptr_t)fd);
468 if (id) {
469 GSource *vus_src = g_main_context_find_source_by_id(NULL, id);
470 assert(vus_src);
471 g_source_destroy(vus_src);
472 (void)g_tree_remove(vdev_scsi->fdmap, (gpointer)(uintptr_t)fd);
473 }
474 }
475
476 static void vus_proc_ctl(VuDev *vu_dev, int idx)
477 {
478 /* Control VQ not implemented */
479 }
480
481 static void vus_proc_evt(VuDev *vu_dev, int idx)
482 {
483 /* Event VQ not implemented */
484 }
485
486 static void vus_proc_req(VuDev *vu_dev, int idx)
487 {
488 vhost_scsi_dev_t *vdev_scsi;
489 VuVirtq *vq;
490
491 assert(vu_dev);
492
493 vdev_scsi = container_of(vu_dev, vhost_scsi_dev_t, vu_dev);
494 if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
495 PERR("VQ Index out of range: %d", idx);
496 vus_panic_cb(vu_dev, NULL);
497 return;
498 }
499
500 vq = vu_get_queue(vu_dev, idx);
501 if (!vq) {
502 PERR("Error fetching VQ (dev=%p, idx=%d)", vu_dev, idx);
503 vus_panic_cb(vu_dev, NULL);
504 return;
505 }
506
507 PDBG("Got kicked on vq[%d]@%p", idx, vq);
508
509 while (1) {
510 VuVirtqElement *elem;
511 VirtIOSCSICmdReq *req;
512 VirtIOSCSICmdResp *rsp;
513
514 elem = vu_queue_pop(vu_dev, vq, sizeof(VuVirtqElement));
515 if (!elem) {
516 PDBG("No more elements pending on vq[%d]@%p", idx, vq);
517 break;
518 }
519 PDBG("Popped elem@%p", elem);
520
521 assert(!(elem->out_num > 1 && elem->in_num > 1));
522 assert(elem->out_num > 0 && elem->in_num > 0);
523
524 if (elem->out_sg[0].iov_len < sizeof(VirtIOSCSICmdReq)) {
525 PERR("Invalid virtio-scsi req header");
526 vus_panic_cb(vu_dev, NULL);
527 break;
528 }
529 req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
530
531 if (elem->in_sg[0].iov_len < sizeof(VirtIOSCSICmdResp)) {
532 PERR("Invalid virtio-scsi rsp header");
533 vus_panic_cb(vu_dev, NULL);
534 break;
535 }
536 rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
537
538 if (handle_cmd_sync(vdev_scsi->luns[0].iscsi_ctx,
539 req, &elem->out_sg[1], elem->out_num - 1,
540 rsp, &elem->in_sg[1], elem->in_num - 1) != 0) {
541 vus_panic_cb(vu_dev, NULL);
542 break;
543 }
544
545 vu_queue_push(vu_dev, vq, elem, 0);
546 vu_queue_notify(vu_dev, vq);
547
548 free(elem);
549 }
550 }
551
552 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
553 {
554 VuVirtq *vq;
555
556 assert(vu_dev);
557
558 if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
559 PERR("VQ Index out of range: %d", idx);
560 vus_panic_cb(vu_dev, NULL);
561 return;
562 }
563
564 vq = vu_get_queue(vu_dev, idx);
565
566 switch (idx) {
567 case 0:
568 vu_set_queue_handler(vu_dev, vq, started ? vus_proc_ctl : NULL);
569 break;
570 case 1:
571 vu_set_queue_handler(vu_dev, vq, started ? vus_proc_evt : NULL);
572 break;
573 default:
574 vu_set_queue_handler(vu_dev, vq, started ? vus_proc_req : NULL);
575 }
576 }
577
578 static const VuDevIface vus_iface = {
579 .queue_set_started = vus_queue_set_started,
580 };
581
582 static gboolean vus_vhost_cb(gpointer data)
583 {
584 VuDev *vu_dev = (VuDev *)data;
585
586 assert(vu_dev);
587
588 if (!vu_dispatch(vu_dev) != 0) {
589 PERR("Error processing vhost message");
590 vus_panic_cb(vu_dev, NULL);
591 return G_SOURCE_REMOVE;
592 }
593
594 return G_SOURCE_CONTINUE;
595 }
596
597 /** misc helpers **/
598
599 static int unix_sock_new(char *unix_fn)
600 {
601 int sock;
602 struct sockaddr_un un;
603 size_t len;
604
605 assert(unix_fn);
606
607 sock = socket(AF_UNIX, SOCK_STREAM, 0);
608 if (sock <= 0) {
609 perror("socket");
610 return -1;
611 }
612
613 un.sun_family = AF_UNIX;
614 (void)snprintf(un.sun_path, sizeof(un.sun_path), "%s", unix_fn);
615 len = sizeof(un.sun_family) + strlen(un.sun_path);
616
617 (void)unlink(unix_fn);
618 if (bind(sock, (struct sockaddr *)&un, len) < 0) {
619 perror("bind");
620 goto fail;
621 }
622
623 if (listen(sock, 1) < 0) {
624 perror("listen");
625 goto fail;
626 }
627
628 return sock;
629
630 fail:
631 (void)close(sock);
632
633 return -1;
634 }
635
636 /** vhost-user-scsi **/
637
638 static void vdev_scsi_free(vhost_scsi_dev_t *vdev_scsi)
639 {
640 if (vdev_scsi->server_sock >= 0) {
641 close(vdev_scsi->server_sock);
642 }
643 g_main_loop_unref(vdev_scsi->loop);
644 g_tree_destroy(vdev_scsi->fdmap);
645 g_free(vdev_scsi);
646 }
647
648 static vhost_scsi_dev_t *vdev_scsi_new(int server_sock)
649 {
650 vhost_scsi_dev_t *vdev_scsi;
651
652 vdev_scsi = g_new0(vhost_scsi_dev_t, 1);
653 vdev_scsi->server_sock = server_sock;
654 vdev_scsi->loop = g_main_loop_new(NULL, FALSE);
655 vdev_scsi->fdmap = g_tree_new(vus_fdmap_compare);
656
657 return vdev_scsi;
658 }
659
660 static int vdev_scsi_add_iscsi_lun(vhost_scsi_dev_t *vdev_scsi,
661 char *iscsi_uri, uint32_t lun)
662 {
663 assert(vdev_scsi);
664 assert(iscsi_uri);
665 assert(lun < VUS_MAX_LUNS);
666
667 if (iscsi_add_lun(&vdev_scsi->luns[lun], iscsi_uri) != 0) {
668 return -1;
669 }
670
671 return 0;
672 }
673
674 static int vdev_scsi_run(vhost_scsi_dev_t *vdev_scsi)
675 {
676 int cli_sock;
677 int ret = 0;
678
679 assert(vdev_scsi);
680 assert(vdev_scsi->server_sock >= 0);
681 assert(vdev_scsi->loop);
682
683 cli_sock = accept(vdev_scsi->server_sock, NULL, NULL);
684 if (cli_sock < 0) {
685 perror("accept");
686 return -1;
687 }
688
689 vu_init(&vdev_scsi->vu_dev,
690 cli_sock,
691 vus_panic_cb,
692 vus_add_watch_cb,
693 vus_del_watch_cb,
694 &vus_iface);
695
696 vus_gsrc_new(vdev_scsi, cli_sock, G_IO_IN, NULL, vus_vhost_cb,
697 &vdev_scsi->vu_dev);
698
699 g_main_loop_run(vdev_scsi->loop);
700
701 vu_deinit(&vdev_scsi->vu_dev);
702
703 return ret;
704 }
705
706 int main(int argc, char **argv)
707 {
708 vhost_scsi_dev_t *vdev_scsi = NULL;
709 char *unix_fn = NULL;
710 char *iscsi_uri = NULL;
711 int sock, opt, err = EXIT_SUCCESS;
712
713 while ((opt = getopt(argc, argv, "u:i:")) != -1) {
714 switch (opt) {
715 case 'h':
716 goto help;
717 case 'u':
718 unix_fn = g_strdup(optarg);
719 break;
720 case 'i':
721 iscsi_uri = g_strdup(optarg);
722 break;
723 default:
724 goto help;
725 }
726 }
727 if (!unix_fn || !iscsi_uri) {
728 goto help;
729 }
730
731 sock = unix_sock_new(unix_fn);
732 if (sock < 0) {
733 goto err;
734 }
735 vdev_scsi = vdev_scsi_new(sock);
736
737 if (vdev_scsi_add_iscsi_lun(vdev_scsi, iscsi_uri, 0) != 0) {
738 goto err;
739 }
740
741 if (vdev_scsi_run(vdev_scsi) != 0) {
742 goto err;
743 }
744
745 out:
746 if (vdev_scsi) {
747 vdev_scsi_free(vdev_scsi);
748 unlink(unix_fn);
749 }
750 g_free(unix_fn);
751 g_free(iscsi_uri);
752
753 return err;
754
755 err:
756 err = EXIT_FAILURE;
757 goto out;
758
759 help:
760 fprintf(stderr, "Usage: %s [ -u unix_sock_path -i iscsi_uri ] | [ -h ]\n",
761 argv[0]);
762 fprintf(stderr, " -u path to unix socket\n");
763 fprintf(stderr, " -i iscsi uri for lun 0\n");
764 fprintf(stderr, " -h print help and quit\n");
765
766 goto err;
767 }