vhost-user-scsi: also free the gtree
[qemu.git] / contrib / vhost-user-scsi / vhost-user-scsi.c
blob17f676bf004cbffe8986f038873fb62bea3f6dd7
1 /*
2 * vhost-user-scsi sample application
4 * Copyright (c) 2016 Nutanix Inc. All rights reserved.
6 * Author:
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 "contrib/libvhost-user/libvhost-user.h"
15 #include "hw/virtio/virtio-scsi.h"
16 #include "iscsi/iscsi.h"
18 #include <glib.h>
20 /* #define VUS_DEBUG 1 */
22 /** Log helpers **/
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))
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)
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)
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
54 /** vhost-user-scsi specific definitions **/
56 /* Only 1 LUN and device supported today */
57 #define VUS_MAX_LUNS 1
58 #define VUS_MAX_DEVS 1
60 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
62 typedef struct iscsi_lun {
63 struct iscsi_context *iscsi_ctx;
64 int iscsi_lun;
65 } iscsi_lun_t;
67 typedef struct vhost_scsi_dev {
68 VuDev vu_dev;
69 int server_sock;
70 GMainLoop *loop;
71 GTree *fdmap; /* fd -> gsource context id */
72 iscsi_lun_t luns[VUS_MAX_LUNS];
73 } vhost_scsi_dev_t;
75 static vhost_scsi_dev_t *vhost_scsi_devs[VUS_MAX_DEVS];
77 /** glib event loop integration for libvhost-user and misc callbacks **/
79 QEMU_BUILD_BUG_ON((int)G_IO_IN != (int)VU_WATCH_IN);
80 QEMU_BUILD_BUG_ON((int)G_IO_OUT != (int)VU_WATCH_OUT);
81 QEMU_BUILD_BUG_ON((int)G_IO_PRI != (int)VU_WATCH_PRI);
82 QEMU_BUILD_BUG_ON((int)G_IO_ERR != (int)VU_WATCH_ERR);
83 QEMU_BUILD_BUG_ON((int)G_IO_HUP != (int)VU_WATCH_HUP);
85 typedef struct vus_gsrc {
86 GSource parent;
87 vhost_scsi_dev_t *vdev_scsi;
88 GPollFD gfd;
89 vu_watch_cb vu_cb;
90 } vus_gsrc_t;
92 static gint vus_fdmap_compare(gconstpointer a, gconstpointer b)
94 return (b > a) - (b < a);
97 static gboolean vus_gsrc_prepare(GSource *src, gint *timeout)
99 assert(timeout);
101 *timeout = -1;
102 return FALSE;
105 static gboolean vus_gsrc_check(GSource *src)
107 vus_gsrc_t *vus_src = (vus_gsrc_t *)src;
109 assert(vus_src);
111 return vus_src->gfd.revents & vus_src->gfd.events;
114 static gboolean vus_gsrc_dispatch(GSource *src, GSourceFunc cb, gpointer data)
116 vhost_scsi_dev_t *vdev_scsi;
117 vus_gsrc_t *vus_src = (vus_gsrc_t *)src;
119 assert(vus_src);
120 assert(!(vus_src->vu_cb && cb));
122 vdev_scsi = vus_src->vdev_scsi;
124 assert(vdev_scsi);
126 if (cb) {
127 return cb(data);
129 if (vus_src->vu_cb) {
130 vus_src->vu_cb(&vdev_scsi->vu_dev, vus_src->gfd.revents, data);
132 return G_SOURCE_CONTINUE;
135 static GSourceFuncs vus_gsrc_funcs = {
136 vus_gsrc_prepare,
137 vus_gsrc_check,
138 vus_gsrc_dispatch,
139 NULL
142 static void vus_gsrc_new(vhost_scsi_dev_t *vdev_scsi, int fd, GIOCondition cond,
143 vu_watch_cb vu_cb, GSourceFunc gsrc_cb, gpointer data)
145 GSource *vus_gsrc;
146 vus_gsrc_t *vus_src;
147 guint id;
149 assert(vdev_scsi);
150 assert(fd >= 0);
151 assert(vu_cb || gsrc_cb);
152 assert(!(vu_cb && gsrc_cb));
154 vus_gsrc = g_source_new(&vus_gsrc_funcs, sizeof(vus_gsrc_t));
155 vus_src = (vus_gsrc_t *)vus_gsrc;
157 vus_src->vdev_scsi = vdev_scsi;
158 vus_src->gfd.fd = fd;
159 vus_src->gfd.events = cond;
160 vus_src->vu_cb = vu_cb;
162 g_source_add_poll(vus_gsrc, &vus_src->gfd);
163 g_source_set_callback(vus_gsrc, gsrc_cb, data, NULL);
164 id = g_source_attach(vus_gsrc, NULL);
165 assert(id);
166 g_source_unref(vus_gsrc);
168 g_tree_insert(vdev_scsi->fdmap, (gpointer)(uintptr_t)fd,
169 (gpointer)(uintptr_t)id);
172 /* from libiscsi's scsi-lowlevel.h **
174 * nb. We can't directly include scsi-lowlevel.h due to a namespace conflict:
175 * QEMU's scsi.h also defines "SCSI_XFER_NONE".
178 #define SCSI_CDB_MAX_SIZE 16
180 struct scsi_iovector {
181 struct scsi_iovec *iov;
182 int niov;
183 int nalloc;
184 size_t offset;
185 int consumed;
188 struct scsi_allocated_memory {
189 struct scsi_allocated_memory *next;
190 char buf[0];
193 struct scsi_data {
194 int size;
195 unsigned char *data;
198 enum scsi_sense_key {
199 SCSI_SENSE_NO_SENSE = 0x00,
200 SCSI_SENSE_RECOVERED_ERROR = 0x01,
201 SCSI_SENSE_NOT_READY = 0x02,
202 SCSI_SENSE_MEDIUM_ERROR = 0x03,
203 SCSI_SENSE_HARDWARE_ERROR = 0x04,
204 SCSI_SENSE_ILLEGAL_REQUEST = 0x05,
205 SCSI_SENSE_UNIT_ATTENTION = 0x06,
206 SCSI_SENSE_DATA_PROTECTION = 0x07,
207 SCSI_SENSE_BLANK_CHECK = 0x08,
208 SCSI_SENSE_VENDOR_SPECIFIC = 0x09,
209 SCSI_SENSE_COPY_ABORTED = 0x0a,
210 SCSI_SENSE_COMMAND_ABORTED = 0x0b,
211 SCSI_SENSE_OBSOLETE_ERROR_CODE = 0x0c,
212 SCSI_SENSE_OVERFLOW_COMMAND = 0x0d,
213 SCSI_SENSE_MISCOMPARE = 0x0e
216 struct scsi_sense {
217 unsigned char error_type;
218 enum scsi_sense_key key;
219 int ascq;
220 unsigned sense_specific:1;
221 unsigned ill_param_in_cdb:1;
222 unsigned bit_pointer_valid:1;
223 unsigned char bit_pointer;
224 uint16_t field_pointer;
227 enum scsi_residual {
228 SCSI_RESIDUAL_NO_RESIDUAL = 0,
229 SCSI_RESIDUAL_UNDERFLOW,
230 SCSI_RESIDUAL_OVERFLOW
233 struct scsi_task {
234 int status;
235 int cdb_size;
236 int xfer_dir;
237 int expxferlen;
238 unsigned char cdb[SCSI_CDB_MAX_SIZE];
239 enum scsi_residual residual_status;
240 size_t residual;
241 struct scsi_sense sense;
242 struct scsi_data datain;
243 struct scsi_allocated_memory *mem;
244 void *ptr;
246 uint32_t itt;
247 uint32_t cmdsn;
248 uint32_t lun;
250 struct scsi_iovector iovector_in;
251 struct scsi_iovector iovector_out;
254 /** libiscsi integration **/
256 static int iscsi_add_lun(iscsi_lun_t *lun, char *iscsi_uri)
258 struct iscsi_url *iscsi_url;
259 struct iscsi_context *iscsi_ctx;
260 int ret = 0;
262 assert(lun);
263 assert(iscsi_uri);
265 iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
266 if (!iscsi_ctx) {
267 PERR("Unable to create iSCSI context");
268 return -1;
271 iscsi_url = iscsi_parse_full_url(iscsi_ctx, iscsi_uri);
272 if (!iscsi_url) {
273 PERR("Unable to parse iSCSI URL: %s", iscsi_get_error(iscsi_ctx));
274 goto fail;
277 iscsi_set_session_type(iscsi_ctx, ISCSI_SESSION_NORMAL);
278 iscsi_set_header_digest(iscsi_ctx, ISCSI_HEADER_DIGEST_NONE_CRC32C);
279 if (iscsi_full_connect_sync(iscsi_ctx, iscsi_url->portal, iscsi_url->lun)) {
280 PERR("Unable to login to iSCSI portal: %s", iscsi_get_error(iscsi_ctx));
281 goto fail;
284 lun->iscsi_ctx = iscsi_ctx;
285 lun->iscsi_lun = iscsi_url->lun;
287 PDBG("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
289 out:
290 if (iscsi_url) {
291 iscsi_destroy_url(iscsi_url);
293 return ret;
295 fail:
296 (void)iscsi_destroy_context(iscsi_ctx);
297 ret = -1;
298 goto out;
301 static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
302 int xfer_len)
304 struct scsi_task *task;
306 assert(cdb_len > 0);
307 assert(cdb);
309 task = g_new0(struct scsi_task, 1);
310 memcpy(task->cdb, cdb, cdb_len);
311 task->cdb_size = cdb_len;
312 task->xfer_dir = dir;
313 task->expxferlen = xfer_len;
315 return task;
318 static int get_cdb_len(uint8_t *cdb)
320 assert(cdb);
322 switch (cdb[0] >> 5) {
323 case 0: return 6;
324 case 1: /* fall through */
325 case 2: return 10;
326 case 4: return 16;
327 case 5: return 12;
329 PERR("Unable to determine cdb len (0x%02hhX)", cdb[0] >> 5);
330 return -1;
333 static int handle_cmd_sync(struct iscsi_context *ctx,
334 VirtIOSCSICmdReq *req,
335 struct iovec *out, unsigned int out_len,
336 VirtIOSCSICmdResp *rsp,
337 struct iovec *in, unsigned int in_len)
339 struct scsi_task *task;
340 uint32_t dir;
341 uint32_t len;
342 int cdb_len;
343 int i;
345 assert(ctx);
346 assert(req);
347 assert(rsp);
349 if (!(!req->lun[1] && req->lun[2] == 0x40 && !req->lun[3])) {
350 /* Ignore anything different than target=0, lun=0 */
351 PDBG("Ignoring unconnected lun (0x%hhX, 0x%hhX)",
352 req->lun[1], req->lun[3]);
353 rsp->status = SCSI_STATUS_CHECK_CONDITION;
354 memset(rsp->sense, 0, sizeof(rsp->sense));
355 rsp->sense_len = 18;
356 rsp->sense[0] = 0x70;
357 rsp->sense[2] = SCSI_SENSE_ILLEGAL_REQUEST;
358 rsp->sense[7] = 10;
359 rsp->sense[12] = 0x24;
361 return 0;
364 cdb_len = get_cdb_len(req->cdb);
365 if (cdb_len == -1) {
366 return -1;
369 len = 0;
370 if (!out_len && !in_len) {
371 dir = SCSI_XFER_NONE;
372 } else if (out_len) {
373 dir = SCSI_XFER_TO_DEV;
374 for (i = 0; i < out_len; i++) {
375 len += out[i].iov_len;
377 } else {
378 dir = SCSI_XFER_FROM_DEV;
379 for (i = 0; i < in_len; i++) {
380 len += in[i].iov_len;
384 task = scsi_task_new(cdb_len, req->cdb, dir, len);
386 if (dir == SCSI_XFER_TO_DEV) {
387 task->iovector_out.iov = (struct scsi_iovec *)out;
388 task->iovector_out.niov = out_len;
389 } else if (dir == SCSI_XFER_FROM_DEV) {
390 task->iovector_in.iov = (struct scsi_iovec *)in;
391 task->iovector_in.niov = in_len;
394 PDBG("Sending iscsi cmd (cdb_len=%d, dir=%d, task=%p)",
395 cdb_len, dir, task);
396 if (!iscsi_scsi_command_sync(ctx, 0, task, NULL)) {
397 PERR("Error serving SCSI command");
398 g_free(task);
399 return -1;
402 memset(rsp, 0, sizeof(*rsp));
404 rsp->status = task->status;
405 rsp->resid = task->residual;
407 if (task->status == SCSI_STATUS_CHECK_CONDITION) {
408 rsp->response = VIRTIO_SCSI_S_FAILURE;
409 rsp->sense_len = task->datain.size - 2;
410 memcpy(rsp->sense, &task->datain.data[2], rsp->sense_len);
413 g_free(task);
415 PDBG("Filled in rsp: status=%hhX, resid=%u, response=%hhX, sense_len=%u",
416 rsp->status, rsp->resid, rsp->response, rsp->sense_len);
418 return 0;
421 /** libvhost-user callbacks **/
423 static vhost_scsi_dev_t *vdev_scsi_find_by_vu(VuDev *vu_dev);
425 static void vus_panic_cb(VuDev *vu_dev, const char *buf)
427 vhost_scsi_dev_t *vdev_scsi;
429 assert(vu_dev);
431 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
433 if (buf) {
434 PERR("vu_panic: %s", buf);
437 g_main_loop_quit(vdev_scsi->loop);
440 static void vus_add_watch_cb(VuDev *vu_dev, int fd, int vu_evt, vu_watch_cb cb,
441 void *pvt)
443 vhost_scsi_dev_t *vdev_scsi;
444 guint id;
446 assert(vu_dev);
447 assert(fd >= 0);
448 assert(cb);
450 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
451 if (!vdev_scsi) {
452 vus_panic_cb(vu_dev, NULL);
453 return;
456 id = (guint)(uintptr_t)g_tree_lookup(vdev_scsi->fdmap,
457 (gpointer)(uintptr_t)fd);
458 if (id) {
459 GSource *vus_src = g_main_context_find_source_by_id(NULL, id);
460 assert(vus_src);
461 g_source_destroy(vus_src);
462 (void)g_tree_remove(vdev_scsi->fdmap, (gpointer)(uintptr_t)fd);
465 vus_gsrc_new(vdev_scsi, fd, vu_evt, cb, NULL, pvt);
468 static void vus_del_watch_cb(VuDev *vu_dev, int fd)
470 vhost_scsi_dev_t *vdev_scsi;
471 guint id;
473 assert(vu_dev);
474 assert(fd >= 0);
476 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
477 if (!vdev_scsi) {
478 vus_panic_cb(vu_dev, NULL);
479 return;
482 id = (guint)(uintptr_t)g_tree_lookup(vdev_scsi->fdmap,
483 (gpointer)(uintptr_t)fd);
484 if (id) {
485 GSource *vus_src = g_main_context_find_source_by_id(NULL, id);
486 assert(vus_src);
487 g_source_destroy(vus_src);
488 (void)g_tree_remove(vdev_scsi->fdmap, (gpointer)(uintptr_t)fd);
492 static void vus_proc_ctl(VuDev *vu_dev, int idx)
494 /* Control VQ not implemented */
497 static void vus_proc_evt(VuDev *vu_dev, int idx)
499 /* Event VQ not implemented */
502 static void vus_proc_req(VuDev *vu_dev, int idx)
504 vhost_scsi_dev_t *vdev_scsi;
505 VuVirtq *vq;
507 assert(vu_dev);
509 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
510 if (!vdev_scsi) {
511 vus_panic_cb(vu_dev, NULL);
512 return;
515 if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
516 PERR("VQ Index out of range: %d", idx);
517 vus_panic_cb(vu_dev, NULL);
518 return;
521 vq = vu_get_queue(vu_dev, idx);
522 if (!vq) {
523 PERR("Error fetching VQ (dev=%p, idx=%d)", vu_dev, idx);
524 vus_panic_cb(vu_dev, NULL);
525 return;
528 PDBG("Got kicked on vq[%d]@%p", idx, vq);
530 while (1) {
531 VuVirtqElement *elem;
532 VirtIOSCSICmdReq *req;
533 VirtIOSCSICmdResp *rsp;
535 elem = vu_queue_pop(vu_dev, vq, sizeof(VuVirtqElement));
536 if (!elem) {
537 PDBG("No more elements pending on vq[%d]@%p", idx, vq);
538 break;
540 PDBG("Popped elem@%p", elem);
542 assert(!(elem->out_num > 1 && elem->in_num > 1));
543 assert(elem->out_num > 0 && elem->in_num > 0);
545 if (elem->out_sg[0].iov_len < sizeof(VirtIOSCSICmdReq)) {
546 PERR("Invalid virtio-scsi req header");
547 vus_panic_cb(vu_dev, NULL);
548 break;
550 req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
552 if (elem->in_sg[0].iov_len < sizeof(VirtIOSCSICmdResp)) {
553 PERR("Invalid virtio-scsi rsp header");
554 vus_panic_cb(vu_dev, NULL);
555 break;
557 rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
559 if (handle_cmd_sync(vdev_scsi->luns[0].iscsi_ctx,
560 req, &elem->out_sg[1], elem->out_num - 1,
561 rsp, &elem->in_sg[1], elem->in_num - 1) != 0) {
562 vus_panic_cb(vu_dev, NULL);
563 break;
566 vu_queue_push(vu_dev, vq, elem, 0);
567 vu_queue_notify(vu_dev, vq);
569 free(elem);
573 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
575 VuVirtq *vq;
577 assert(vu_dev);
579 if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
580 PERR("VQ Index out of range: %d", idx);
581 vus_panic_cb(vu_dev, NULL);
582 return;
585 vq = vu_get_queue(vu_dev, idx);
587 switch (idx) {
588 case 0:
589 vu_set_queue_handler(vu_dev, vq, started ? vus_proc_ctl : NULL);
590 break;
591 case 1:
592 vu_set_queue_handler(vu_dev, vq, started ? vus_proc_evt : NULL);
593 break;
594 default:
595 vu_set_queue_handler(vu_dev, vq, started ? vus_proc_req : NULL);
599 static const VuDevIface vus_iface = {
600 .queue_set_started = vus_queue_set_started,
603 static gboolean vus_vhost_cb(gpointer data)
605 VuDev *vu_dev = (VuDev *)data;
607 assert(vu_dev);
609 if (!vu_dispatch(vu_dev) != 0) {
610 PERR("Error processing vhost message");
611 vus_panic_cb(vu_dev, NULL);
612 return G_SOURCE_REMOVE;
615 return G_SOURCE_CONTINUE;
618 /** misc helpers **/
620 static int unix_sock_new(char *unix_fn)
622 int sock;
623 struct sockaddr_un un;
624 size_t len;
626 assert(unix_fn);
628 sock = socket(AF_UNIX, SOCK_STREAM, 0);
629 if (sock <= 0) {
630 perror("socket");
631 return -1;
634 un.sun_family = AF_UNIX;
635 (void)snprintf(un.sun_path, sizeof(un.sun_path), "%s", unix_fn);
636 len = sizeof(un.sun_family) + strlen(un.sun_path);
638 (void)unlink(unix_fn);
639 if (bind(sock, (struct sockaddr *)&un, len) < 0) {
640 perror("bind");
641 goto fail;
644 if (listen(sock, 1) < 0) {
645 perror("listen");
646 goto fail;
649 return sock;
651 fail:
652 (void)close(sock);
654 return -1;
657 /** vhost-user-scsi **/
659 static vhost_scsi_dev_t *vdev_scsi_find_by_vu(VuDev *vu_dev)
661 int i;
663 assert(vu_dev);
665 for (i = 0; i < VUS_MAX_DEVS; i++) {
666 if (&vhost_scsi_devs[i]->vu_dev == vu_dev) {
667 return vhost_scsi_devs[i];
671 PERR("Unknown VuDev %p", vu_dev);
672 return NULL;
675 static void vdev_scsi_free(vhost_scsi_dev_t *vdev_scsi)
677 if (!vdev_scsi) {
678 return;
681 if (vdev_scsi->server_sock >= 0) {
682 struct sockaddr_storage ss;
683 socklen_t sslen = sizeof(ss);
685 if (getsockname(vdev_scsi->server_sock, (struct sockaddr *)&ss,
686 &sslen) == 0) {
687 struct sockaddr_un *su = (struct sockaddr_un *)&ss;
688 (void)unlink(su->sun_path);
691 (void)close(vdev_scsi->server_sock);
692 vdev_scsi->server_sock = -1;
695 g_main_loop_unref(vdev_scsi->loop);
696 g_tree_destroy(vdev_scsi->fdmap);
697 g_free(vdev_scsi);
700 static vhost_scsi_dev_t *vdev_scsi_new(int server_sock)
702 vhost_scsi_dev_t *vdev_scsi;
704 vdev_scsi = g_new0(vhost_scsi_dev_t, 1);
705 vdev_scsi->server_sock = server_sock;
706 vdev_scsi->loop = g_main_loop_new(NULL, FALSE);
707 vdev_scsi->fdmap = g_tree_new(vus_fdmap_compare);
709 return vdev_scsi;
712 static int vdev_scsi_add_iscsi_lun(vhost_scsi_dev_t *vdev_scsi,
713 char *iscsi_uri, uint32_t lun)
715 assert(vdev_scsi);
716 assert(iscsi_uri);
717 assert(lun < VUS_MAX_LUNS);
719 if (vdev_scsi->luns[lun].iscsi_ctx) {
720 PERR("Lun %d already configured", lun);
721 return -1;
724 if (iscsi_add_lun(&vdev_scsi->luns[lun], iscsi_uri) != 0) {
725 return -1;
728 return 0;
731 static int vdev_scsi_run(vhost_scsi_dev_t *vdev_scsi)
733 int cli_sock;
734 int ret = 0;
736 assert(vdev_scsi);
737 assert(vdev_scsi->server_sock >= 0);
738 assert(vdev_scsi->loop);
740 cli_sock = accept(vdev_scsi->server_sock, (void *)0, (void *)0);
741 if (cli_sock < 0) {
742 perror("accept");
743 return -1;
746 vu_init(&vdev_scsi->vu_dev,
747 cli_sock,
748 vus_panic_cb,
749 vus_add_watch_cb,
750 vus_del_watch_cb,
751 &vus_iface);
753 vus_gsrc_new(vdev_scsi, cli_sock, G_IO_IN, NULL, vus_vhost_cb,
754 &vdev_scsi->vu_dev);
756 g_main_loop_run(vdev_scsi->loop);
758 vu_deinit(&vdev_scsi->vu_dev);
760 return ret;
763 int main(int argc, char **argv)
765 vhost_scsi_dev_t *vdev_scsi = NULL;
766 char *unix_fn = NULL;
767 char *iscsi_uri = NULL;
768 int sock, opt, err = EXIT_SUCCESS;
770 while ((opt = getopt(argc, argv, "u:i:")) != -1) {
771 switch (opt) {
772 case 'h':
773 goto help;
774 case 'u':
775 unix_fn = g_strdup(optarg);
776 break;
777 case 'i':
778 iscsi_uri = g_strdup(optarg);
779 break;
780 default:
781 goto help;
784 if (!unix_fn || !iscsi_uri) {
785 goto help;
788 sock = unix_sock_new(unix_fn);
789 if (sock < 0) {
790 goto err;
792 vdev_scsi = vdev_scsi_new(sock);
793 vhost_scsi_devs[0] = vdev_scsi;
795 if (vdev_scsi_add_iscsi_lun(vdev_scsi, iscsi_uri, 0) != 0) {
796 goto err;
799 if (vdev_scsi_run(vdev_scsi) != 0) {
800 goto err;
803 out:
804 vdev_scsi_free(vdev_scsi);
805 g_free(unix_fn);
806 g_free(iscsi_uri);
808 return err;
810 err:
811 err = EXIT_FAILURE;
812 goto out;
814 help:
815 fprintf(stderr, "Usage: %s [ -u unix_sock_path -i iscsi_uri ] | [ -h ]\n",
816 argv[0]);
817 fprintf(stderr, " -u path to unix socket\n");
818 fprintf(stderr, " -i iscsi uri for lun 0\n");
819 fprintf(stderr, " -h print help and quit\n");
821 goto err;