xref: /openbmc/qemu/contrib/vhost-user-scsi/vhost-user-scsi.c (revision eec398119fc6911d99412c37af06a6bc27871f85)
149cc0340SFelipe Franciosi /*
249cc0340SFelipe Franciosi  * vhost-user-scsi sample application
349cc0340SFelipe Franciosi  *
449cc0340SFelipe Franciosi  * Copyright (c) 2016 Nutanix Inc. All rights reserved.
549cc0340SFelipe Franciosi  *
649cc0340SFelipe Franciosi  * Author:
749cc0340SFelipe Franciosi  *  Felipe Franciosi <felipe@nutanix.com>
849cc0340SFelipe Franciosi  *
949cc0340SFelipe Franciosi  * This work is licensed under the terms of the GNU GPL, version 2 only.
1049cc0340SFelipe Franciosi  * See the COPYING file in the top-level directory.
1149cc0340SFelipe Franciosi  */
1249cc0340SFelipe Franciosi 
1349cc0340SFelipe Franciosi #include "qemu/osdep.h"
14d8e39b70SMarkus Armbruster #include <iscsi/iscsi.h>
157af0dbf9SPaolo Bonzini #define inline __attribute__((gnu_inline))  /* required for libiscsi v1.9.0 */
16d8e39b70SMarkus Armbruster #include <iscsi/scsi-lowlevel.h>
177af0dbf9SPaolo Bonzini #undef inline
180df750e9SMarc-André Lureau #include "libvhost-user-glib.h"
1949a48824SMarc-André Lureau #include "standard-headers/linux/virtio_scsi.h"
2049cc0340SFelipe Franciosi 
2149cc0340SFelipe Franciosi 
2249cc0340SFelipe Franciosi #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
2349cc0340SFelipe Franciosi 
246f5fd837SStefan Hajnoczi enum {
256f5fd837SStefan Hajnoczi     VHOST_USER_SCSI_MAX_QUEUES = 8,
266f5fd837SStefan Hajnoczi };
276f5fd837SStefan Hajnoczi 
28ade9ab22SMarc-André Lureau typedef struct VusIscsiLun {
2949cc0340SFelipe Franciosi     struct iscsi_context *iscsi_ctx;
3049cc0340SFelipe Franciosi     int iscsi_lun;
31ade9ab22SMarc-André Lureau } VusIscsiLun;
3249cc0340SFelipe Franciosi 
33ade9ab22SMarc-André Lureau typedef struct VusDev {
3462ddfba0SMarc-André Lureau     VugDev parent;
3562ddfba0SMarc-André Lureau 
36ade9ab22SMarc-André Lureau     VusIscsiLun lun;
3762ddfba0SMarc-André Lureau     GMainLoop *loop;
38ade9ab22SMarc-André Lureau } VusDev;
3949cc0340SFelipe Franciosi 
4049cc0340SFelipe Franciosi /** libiscsi integration **/
4149cc0340SFelipe Franciosi 
4249a48824SMarc-André Lureau typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq;
4349a48824SMarc-André Lureau typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp;
4449a48824SMarc-André Lureau 
vus_iscsi_add_lun(VusIscsiLun * lun,char * iscsi_uri)45ca853813SMarc-André Lureau static int vus_iscsi_add_lun(VusIscsiLun *lun, char *iscsi_uri)
4649cc0340SFelipe Franciosi {
4749cc0340SFelipe Franciosi     struct iscsi_url *iscsi_url;
4849cc0340SFelipe Franciosi     struct iscsi_context *iscsi_ctx;
4949cc0340SFelipe Franciosi     int ret = 0;
5049cc0340SFelipe Franciosi 
5149cc0340SFelipe Franciosi     assert(lun);
5249cc0340SFelipe Franciosi     assert(iscsi_uri);
53fa2cd143SMarc-André Lureau     assert(!lun->iscsi_ctx);
5449cc0340SFelipe Franciosi 
5549cc0340SFelipe Franciosi     iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
5649cc0340SFelipe Franciosi     if (!iscsi_ctx) {
57422a2687SMarc-André Lureau         g_warning("Unable to create iSCSI context");
5849cc0340SFelipe Franciosi         return -1;
5949cc0340SFelipe Franciosi     }
6049cc0340SFelipe Franciosi 
6149cc0340SFelipe Franciosi     iscsi_url = iscsi_parse_full_url(iscsi_ctx, iscsi_uri);
6249cc0340SFelipe Franciosi     if (!iscsi_url) {
63422a2687SMarc-André Lureau         g_warning("Unable to parse iSCSI URL: %s", iscsi_get_error(iscsi_ctx));
6449cc0340SFelipe Franciosi         goto fail;
6549cc0340SFelipe Franciosi     }
6649cc0340SFelipe Franciosi 
6749cc0340SFelipe Franciosi     iscsi_set_session_type(iscsi_ctx, ISCSI_SESSION_NORMAL);
6849cc0340SFelipe Franciosi     iscsi_set_header_digest(iscsi_ctx, ISCSI_HEADER_DIGEST_NONE_CRC32C);
6949cc0340SFelipe Franciosi     if (iscsi_full_connect_sync(iscsi_ctx, iscsi_url->portal, iscsi_url->lun)) {
70422a2687SMarc-André Lureau         g_warning("Unable to login to iSCSI portal: %s",
71422a2687SMarc-André Lureau                   iscsi_get_error(iscsi_ctx));
7249cc0340SFelipe Franciosi         goto fail;
7349cc0340SFelipe Franciosi     }
7449cc0340SFelipe Franciosi 
7549cc0340SFelipe Franciosi     lun->iscsi_ctx = iscsi_ctx;
7649cc0340SFelipe Franciosi     lun->iscsi_lun = iscsi_url->lun;
7749cc0340SFelipe Franciosi 
78422a2687SMarc-André Lureau     g_debug("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
7949cc0340SFelipe Franciosi 
8049cc0340SFelipe Franciosi out:
8149cc0340SFelipe Franciosi     if (iscsi_url) {
8249cc0340SFelipe Franciosi         iscsi_destroy_url(iscsi_url);
8349cc0340SFelipe Franciosi     }
8449cc0340SFelipe Franciosi     return ret;
8549cc0340SFelipe Franciosi 
8649cc0340SFelipe Franciosi fail:
8749cc0340SFelipe Franciosi     (void)iscsi_destroy_context(iscsi_ctx);
8849cc0340SFelipe Franciosi     ret = -1;
8949cc0340SFelipe Franciosi     goto out;
9049cc0340SFelipe Franciosi }
9149cc0340SFelipe Franciosi 
scsi_task_new(int cdb_len,uint8_t * cdb,int dir,int xfer_len)9249cc0340SFelipe Franciosi static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
938c36ed6eSMarc-André Lureau                                        int xfer_len)
948c36ed6eSMarc-André Lureau {
9549cc0340SFelipe Franciosi     struct scsi_task *task;
9649cc0340SFelipe Franciosi 
9749cc0340SFelipe Franciosi     assert(cdb_len > 0);
9849cc0340SFelipe Franciosi     assert(cdb);
9949cc0340SFelipe Franciosi 
1003d9ad8beSMarc-André Lureau     task = g_new0(struct scsi_task, 1);
10149cc0340SFelipe Franciosi     memcpy(task->cdb, cdb, cdb_len);
10249cc0340SFelipe Franciosi     task->cdb_size = cdb_len;
10349cc0340SFelipe Franciosi     task->xfer_dir = dir;
10449cc0340SFelipe Franciosi     task->expxferlen = xfer_len;
10549cc0340SFelipe Franciosi 
10649cc0340SFelipe Franciosi     return task;
10749cc0340SFelipe Franciosi }
10849cc0340SFelipe Franciosi 
get_cdb_len(uint8_t * cdb)10949cc0340SFelipe Franciosi static int get_cdb_len(uint8_t *cdb)
11049cc0340SFelipe Franciosi {
11149cc0340SFelipe Franciosi     assert(cdb);
11249cc0340SFelipe Franciosi 
11349cc0340SFelipe Franciosi     switch (cdb[0] >> 5) {
11449cc0340SFelipe Franciosi     case 0: return 6;
11549cc0340SFelipe Franciosi     case 1: /* fall through */
11649cc0340SFelipe Franciosi     case 2: return 10;
11749cc0340SFelipe Franciosi     case 4: return 16;
11849cc0340SFelipe Franciosi     case 5: return 12;
11949cc0340SFelipe Franciosi     }
1203819af6eSMarc-André Lureau     g_warning("Unable to determine cdb len (0x%02hhX)", (uint8_t)(cdb[0] >> 5));
12149cc0340SFelipe Franciosi     return -1;
12249cc0340SFelipe Franciosi }
12349cc0340SFelipe Franciosi 
handle_cmd_sync(struct iscsi_context * ctx,VirtIOSCSICmdReq * req,struct iovec * out,unsigned int out_len,VirtIOSCSICmdResp * rsp,struct iovec * in,unsigned int in_len)12449cc0340SFelipe Franciosi static int handle_cmd_sync(struct iscsi_context *ctx,
12549cc0340SFelipe Franciosi                            VirtIOSCSICmdReq *req,
12649cc0340SFelipe Franciosi                            struct iovec *out, unsigned int out_len,
12749cc0340SFelipe Franciosi                            VirtIOSCSICmdResp *rsp,
1288c36ed6eSMarc-André Lureau                            struct iovec *in, unsigned int in_len)
1298c36ed6eSMarc-André Lureau {
13049cc0340SFelipe Franciosi     struct scsi_task *task;
13149cc0340SFelipe Franciosi     uint32_t dir;
13249cc0340SFelipe Franciosi     uint32_t len;
13349cc0340SFelipe Franciosi     int cdb_len;
13449cc0340SFelipe Franciosi     int i;
13549cc0340SFelipe Franciosi 
13649cc0340SFelipe Franciosi     assert(ctx);
13749cc0340SFelipe Franciosi     assert(req);
13849cc0340SFelipe Franciosi     assert(rsp);
13949cc0340SFelipe Franciosi 
14049cc0340SFelipe Franciosi     if (!(!req->lun[1] && req->lun[2] == 0x40 && !req->lun[3])) {
14149cc0340SFelipe Franciosi         /* Ignore anything different than target=0, lun=0 */
142422a2687SMarc-André Lureau         g_debug("Ignoring unconnected lun (0x%hhX, 0x%hhX)",
14349cc0340SFelipe Franciosi              req->lun[1], req->lun[3]);
14449cc0340SFelipe Franciosi         rsp->status = SCSI_STATUS_CHECK_CONDITION;
14549cc0340SFelipe Franciosi         memset(rsp->sense, 0, sizeof(rsp->sense));
14649cc0340SFelipe Franciosi         rsp->sense_len = 18;
14749cc0340SFelipe Franciosi         rsp->sense[0] = 0x70;
14849cc0340SFelipe Franciosi         rsp->sense[2] = SCSI_SENSE_ILLEGAL_REQUEST;
14949cc0340SFelipe Franciosi         rsp->sense[7] = 10;
15049cc0340SFelipe Franciosi         rsp->sense[12] = 0x24;
15149cc0340SFelipe Franciosi 
15249cc0340SFelipe Franciosi         return 0;
15349cc0340SFelipe Franciosi     }
15449cc0340SFelipe Franciosi 
15549cc0340SFelipe Franciosi     cdb_len = get_cdb_len(req->cdb);
15649cc0340SFelipe Franciosi     if (cdb_len == -1) {
15749cc0340SFelipe Franciosi         return -1;
15849cc0340SFelipe Franciosi     }
15949cc0340SFelipe Franciosi 
16049cc0340SFelipe Franciosi     len = 0;
16149cc0340SFelipe Franciosi     if (!out_len && !in_len) {
16249cc0340SFelipe Franciosi         dir = SCSI_XFER_NONE;
16349cc0340SFelipe Franciosi     } else if (out_len) {
16449a48824SMarc-André Lureau         dir = SCSI_XFER_WRITE;
16549cc0340SFelipe Franciosi         for (i = 0; i < out_len; i++) {
16649cc0340SFelipe Franciosi             len += out[i].iov_len;
16749cc0340SFelipe Franciosi         }
16849cc0340SFelipe Franciosi     } else {
16949a48824SMarc-André Lureau         dir = SCSI_XFER_READ;
17049cc0340SFelipe Franciosi         for (i = 0; i < in_len; i++) {
17149cc0340SFelipe Franciosi             len += in[i].iov_len;
17249cc0340SFelipe Franciosi         }
17349cc0340SFelipe Franciosi     }
17449cc0340SFelipe Franciosi 
17549cc0340SFelipe Franciosi     task = scsi_task_new(cdb_len, req->cdb, dir, len);
17649cc0340SFelipe Franciosi 
17749a48824SMarc-André Lureau     if (dir == SCSI_XFER_WRITE) {
17849cc0340SFelipe Franciosi         task->iovector_out.iov = (struct scsi_iovec *)out;
17949cc0340SFelipe Franciosi         task->iovector_out.niov = out_len;
18049a48824SMarc-André Lureau     } else if (dir == SCSI_XFER_READ) {
18149cc0340SFelipe Franciosi         task->iovector_in.iov = (struct scsi_iovec *)in;
18249cc0340SFelipe Franciosi         task->iovector_in.niov = in_len;
18349cc0340SFelipe Franciosi     }
18449cc0340SFelipe Franciosi 
185422a2687SMarc-André Lureau     g_debug("Sending iscsi cmd (cdb_len=%d, dir=%d, task=%p)",
18649cc0340SFelipe Franciosi          cdb_len, dir, task);
18749cc0340SFelipe Franciosi     if (!iscsi_scsi_command_sync(ctx, 0, task, NULL)) {
188422a2687SMarc-André Lureau         g_warning("Error serving SCSI command");
1893d9ad8beSMarc-André Lureau         g_free(task);
19049cc0340SFelipe Franciosi         return -1;
19149cc0340SFelipe Franciosi     }
19249cc0340SFelipe Franciosi 
19349cc0340SFelipe Franciosi     memset(rsp, 0, sizeof(*rsp));
19449cc0340SFelipe Franciosi 
19549cc0340SFelipe Franciosi     rsp->status = task->status;
19649cc0340SFelipe Franciosi     rsp->resid  = task->residual;
19749cc0340SFelipe Franciosi 
19849cc0340SFelipe Franciosi     if (task->status == SCSI_STATUS_CHECK_CONDITION) {
19949cc0340SFelipe Franciosi         rsp->response = VIRTIO_SCSI_S_FAILURE;
20049cc0340SFelipe Franciosi         rsp->sense_len = task->datain.size - 2;
20149cc0340SFelipe Franciosi         memcpy(rsp->sense, &task->datain.data[2], rsp->sense_len);
20249cc0340SFelipe Franciosi     }
20349cc0340SFelipe Franciosi 
2043d9ad8beSMarc-André Lureau     g_free(task);
20549cc0340SFelipe Franciosi 
206422a2687SMarc-André Lureau     g_debug("Filled in rsp: status=%hhX, resid=%u, response=%hhX, sense_len=%u",
20749cc0340SFelipe Franciosi          rsp->status, rsp->resid, rsp->response, rsp->sense_len);
20849cc0340SFelipe Franciosi 
20949cc0340SFelipe Franciosi     return 0;
21049cc0340SFelipe Franciosi }
21149cc0340SFelipe Franciosi 
21249cc0340SFelipe Franciosi /** libvhost-user callbacks **/
21349cc0340SFelipe Franciosi 
vus_panic_cb(VuDev * vu_dev,const char * buf)21449cc0340SFelipe Franciosi static void vus_panic_cb(VuDev *vu_dev, const char *buf)
21549cc0340SFelipe Franciosi {
21662ddfba0SMarc-André Lureau     VugDev *gdev;
217ade9ab22SMarc-André Lureau     VusDev *vdev_scsi;
21849cc0340SFelipe Franciosi 
21949cc0340SFelipe Franciosi     assert(vu_dev);
22049cc0340SFelipe Franciosi 
22162ddfba0SMarc-André Lureau     gdev = container_of(vu_dev, VugDev, parent);
22262ddfba0SMarc-André Lureau     vdev_scsi = container_of(gdev, VusDev, parent);
22349cc0340SFelipe Franciosi     if (buf) {
224422a2687SMarc-André Lureau         g_warning("vu_panic: %s", buf);
22549cc0340SFelipe Franciosi     }
22649cc0340SFelipe Franciosi 
22749cc0340SFelipe Franciosi     g_main_loop_quit(vdev_scsi->loop);
22849cc0340SFelipe Franciosi }
22949cc0340SFelipe Franciosi 
vus_proc_req(VuDev * vu_dev,int idx)23049cc0340SFelipe Franciosi static void vus_proc_req(VuDev *vu_dev, int idx)
23149cc0340SFelipe Franciosi {
23262ddfba0SMarc-André Lureau     VugDev *gdev;
233ade9ab22SMarc-André Lureau     VusDev *vdev_scsi;
23449cc0340SFelipe Franciosi     VuVirtq *vq;
23581ee17aaSAlex Chen     VuVirtqElement *elem = NULL;
23649cc0340SFelipe Franciosi 
23749cc0340SFelipe Franciosi     assert(vu_dev);
23849cc0340SFelipe Franciosi 
23962ddfba0SMarc-André Lureau     gdev = container_of(vu_dev, VugDev, parent);
24062ddfba0SMarc-André Lureau     vdev_scsi = container_of(gdev, VusDev, parent);
24149cc0340SFelipe Franciosi 
24249cc0340SFelipe Franciosi     vq = vu_get_queue(vu_dev, idx);
24349cc0340SFelipe Franciosi     if (!vq) {
244422a2687SMarc-André Lureau         g_warning("Error fetching VQ (dev=%p, idx=%d)", vu_dev, idx);
24549cc0340SFelipe Franciosi         vus_panic_cb(vu_dev, NULL);
24649cc0340SFelipe Franciosi         return;
24749cc0340SFelipe Franciosi     }
24849cc0340SFelipe Franciosi 
249422a2687SMarc-André Lureau     g_debug("Got kicked on vq[%d]@%p", idx, vq);
25049cc0340SFelipe Franciosi 
25149cc0340SFelipe Franciosi     while (1) {
25249cc0340SFelipe Franciosi         VirtIOSCSICmdReq *req;
25349cc0340SFelipe Franciosi         VirtIOSCSICmdResp *rsp;
25449cc0340SFelipe Franciosi 
25549cc0340SFelipe Franciosi         elem = vu_queue_pop(vu_dev, vq, sizeof(VuVirtqElement));
25649cc0340SFelipe Franciosi         if (!elem) {
257422a2687SMarc-André Lureau             g_debug("No more elements pending on vq[%d]@%p", idx, vq);
25849cc0340SFelipe Franciosi             break;
25949cc0340SFelipe Franciosi         }
260422a2687SMarc-André Lureau         g_debug("Popped elem@%p", elem);
26149cc0340SFelipe Franciosi 
2628c36ed6eSMarc-André Lureau         assert(!(elem->out_num > 1 && elem->in_num > 1));
2638c36ed6eSMarc-André Lureau         assert(elem->out_num > 0 && elem->in_num > 0);
26449cc0340SFelipe Franciosi 
26549cc0340SFelipe Franciosi         if (elem->out_sg[0].iov_len < sizeof(VirtIOSCSICmdReq)) {
266422a2687SMarc-André Lureau             g_warning("Invalid virtio-scsi req header");
26749cc0340SFelipe Franciosi             vus_panic_cb(vu_dev, NULL);
26849cc0340SFelipe Franciosi             break;
26949cc0340SFelipe Franciosi         }
27049cc0340SFelipe Franciosi         req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
27149cc0340SFelipe Franciosi 
27249cc0340SFelipe Franciosi         if (elem->in_sg[0].iov_len < sizeof(VirtIOSCSICmdResp)) {
273422a2687SMarc-André Lureau             g_warning("Invalid virtio-scsi rsp header");
27449cc0340SFelipe Franciosi             vus_panic_cb(vu_dev, NULL);
27549cc0340SFelipe Franciosi             break;
27649cc0340SFelipe Franciosi         }
27749cc0340SFelipe Franciosi         rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
27849cc0340SFelipe Franciosi 
279d0dba9f4SMarc-André Lureau         if (handle_cmd_sync(vdev_scsi->lun.iscsi_ctx,
28049cc0340SFelipe Franciosi                             req, &elem->out_sg[1], elem->out_num - 1,
28149cc0340SFelipe Franciosi                             rsp, &elem->in_sg[1], elem->in_num - 1) != 0) {
28249cc0340SFelipe Franciosi             vus_panic_cb(vu_dev, NULL);
28349cc0340SFelipe Franciosi             break;
28449cc0340SFelipe Franciosi         }
28549cc0340SFelipe Franciosi 
28649cc0340SFelipe Franciosi         vu_queue_push(vu_dev, vq, elem, 0);
28749cc0340SFelipe Franciosi         vu_queue_notify(vu_dev, vq);
28849cc0340SFelipe Franciosi 
28949cc0340SFelipe Franciosi         free(elem);
29049cc0340SFelipe Franciosi     }
29181ee17aaSAlex Chen     free(elem);
29249cc0340SFelipe Franciosi }
29349cc0340SFelipe Franciosi 
vus_queue_set_started(VuDev * vu_dev,int idx,bool started)29449cc0340SFelipe Franciosi static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
29549cc0340SFelipe Franciosi {
29649cc0340SFelipe Franciosi     VuVirtq *vq;
29749cc0340SFelipe Franciosi 
29849cc0340SFelipe Franciosi     assert(vu_dev);
29949cc0340SFelipe Franciosi 
30049cc0340SFelipe Franciosi     vq = vu_get_queue(vu_dev, idx);
30149cc0340SFelipe Franciosi 
302c7226ee7SMarc-André Lureau     if (idx == 0 || idx == 1) {
303422a2687SMarc-André Lureau         g_debug("queue %d unimplemented", idx);
304c7226ee7SMarc-André Lureau     } else {
30549cc0340SFelipe Franciosi         vu_set_queue_handler(vu_dev, vq, started ? vus_proc_req : NULL);
30649cc0340SFelipe Franciosi     }
30749cc0340SFelipe Franciosi }
30849cc0340SFelipe Franciosi 
30949cc0340SFelipe Franciosi static const VuDevIface vus_iface = {
31049cc0340SFelipe Franciosi     .queue_set_started = vus_queue_set_started,
31149cc0340SFelipe Franciosi };
31249cc0340SFelipe Franciosi 
31349cc0340SFelipe Franciosi /** misc helpers **/
31449cc0340SFelipe Franciosi 
unix_sock_new(char * unix_fn)31549cc0340SFelipe Franciosi static int unix_sock_new(char *unix_fn)
31649cc0340SFelipe Franciosi {
31749cc0340SFelipe Franciosi     int sock;
31849cc0340SFelipe Franciosi     struct sockaddr_un un;
31949cc0340SFelipe Franciosi     size_t len;
32049cc0340SFelipe Franciosi 
32149cc0340SFelipe Franciosi     assert(unix_fn);
32249cc0340SFelipe Franciosi 
32349cc0340SFelipe Franciosi     sock = socket(AF_UNIX, SOCK_STREAM, 0);
32491010f04SAlexChen     if (sock < 0) {
32549cc0340SFelipe Franciosi         perror("socket");
32649cc0340SFelipe Franciosi         return -1;
32749cc0340SFelipe Franciosi     }
32849cc0340SFelipe Franciosi 
32949cc0340SFelipe Franciosi     un.sun_family = AF_UNIX;
33049cc0340SFelipe Franciosi     (void)snprintf(un.sun_path, sizeof(un.sun_path), "%s", unix_fn);
33149cc0340SFelipe Franciosi     len = sizeof(un.sun_family) + strlen(un.sun_path);
33249cc0340SFelipe Franciosi 
33349cc0340SFelipe Franciosi     (void)unlink(unix_fn);
33449cc0340SFelipe Franciosi     if (bind(sock, (struct sockaddr *)&un, len) < 0) {
33549cc0340SFelipe Franciosi         perror("bind");
33649cc0340SFelipe Franciosi         goto fail;
33749cc0340SFelipe Franciosi     }
33849cc0340SFelipe Franciosi 
33949cc0340SFelipe Franciosi     if (listen(sock, 1) < 0) {
34049cc0340SFelipe Franciosi         perror("listen");
34149cc0340SFelipe Franciosi         goto fail;
34249cc0340SFelipe Franciosi     }
34349cc0340SFelipe Franciosi 
34449cc0340SFelipe Franciosi     return sock;
34549cc0340SFelipe Franciosi 
34649cc0340SFelipe Franciosi fail:
34749cc0340SFelipe Franciosi     (void)close(sock);
34849cc0340SFelipe Franciosi 
34949cc0340SFelipe Franciosi     return -1;
35049cc0340SFelipe Franciosi }
35149cc0340SFelipe Franciosi 
35249cc0340SFelipe Franciosi /** vhost-user-scsi **/
35349cc0340SFelipe Franciosi 
354747421e9SSakshi Kaushik static int opt_fdnum = -1;
355747421e9SSakshi Kaushik static char *opt_socket_path;
356747421e9SSakshi Kaushik static gboolean opt_print_caps;
357747421e9SSakshi Kaushik static char *iscsi_uri;
358747421e9SSakshi Kaushik 
359747421e9SSakshi Kaushik static GOptionEntry entries[] = {
360747421e9SSakshi Kaushik     { "print-capabilities", 'c', 0, G_OPTION_ARG_NONE, &opt_print_caps,
361747421e9SSakshi Kaushik       "Print capabilities", NULL },
362747421e9SSakshi Kaushik     { "fd", 'f', 0, G_OPTION_ARG_INT, &opt_fdnum,
363747421e9SSakshi Kaushik       "Use inherited fd socket", "FDNUM" },
364747421e9SSakshi Kaushik     { "iscsi-uri", 'i', 0, G_OPTION_ARG_FILENAME, &iscsi_uri,
365747421e9SSakshi Kaushik       "iSCSI URI to connect to", "FDNUM" },
366747421e9SSakshi Kaushik     { "socket-path", 's', 0, G_OPTION_ARG_FILENAME, &opt_socket_path,
367747421e9SSakshi Kaushik       "Use UNIX socket path", "PATH" },
368747421e9SSakshi Kaushik     { NULL, }
369747421e9SSakshi Kaushik };
370747421e9SSakshi Kaushik 
main(int argc,char ** argv)37149cc0340SFelipe Franciosi int main(int argc, char **argv)
37249cc0340SFelipe Franciosi {
373ade9ab22SMarc-André Lureau     VusDev *vdev_scsi = NULL;
374747421e9SSakshi Kaushik     int lsock = -1, csock = -1, err = EXIT_SUCCESS;
37549cc0340SFelipe Franciosi 
376747421e9SSakshi Kaushik     GError *error = NULL;
377747421e9SSakshi Kaushik     GOptionContext *context;
378747421e9SSakshi Kaushik 
379747421e9SSakshi Kaushik     context = g_option_context_new(NULL);
380747421e9SSakshi Kaushik     g_option_context_add_main_entries(context, entries, NULL);
381747421e9SSakshi Kaushik     if (!g_option_context_parse(context, &argc, &argv, &error)) {
382747421e9SSakshi Kaushik         g_printerr("Option parsing failed: %s\n", error->message);
383747421e9SSakshi Kaushik         exit(EXIT_FAILURE);
38449cc0340SFelipe Franciosi     }
385747421e9SSakshi Kaushik 
386747421e9SSakshi Kaushik     if (opt_print_caps) {
387747421e9SSakshi Kaushik         g_print("{\n");
388747421e9SSakshi Kaushik         g_print("  \"type\": \"scsi\"\n");
389747421e9SSakshi Kaushik         g_print("}\n");
390747421e9SSakshi Kaushik         goto out;
39149cc0340SFelipe Franciosi     }
392747421e9SSakshi Kaushik 
393747421e9SSakshi Kaushik     if (!iscsi_uri) {
39449cc0340SFelipe Franciosi         goto help;
39549cc0340SFelipe Franciosi     }
39649cc0340SFelipe Franciosi 
397747421e9SSakshi Kaushik     if (opt_socket_path) {
398747421e9SSakshi Kaushik         lsock = unix_sock_new(opt_socket_path);
39953a2e1b5SMarc-André Lureau         if (lsock < 0) {
400747421e9SSakshi Kaushik             exit(EXIT_FAILURE);
401747421e9SSakshi Kaushik         }
402747421e9SSakshi Kaushik     } else if (opt_fdnum < 0) {
403747421e9SSakshi Kaushik         g_print("%s\n", g_option_context_get_help(context, true, NULL));
404747421e9SSakshi Kaushik         exit(EXIT_FAILURE);
405747421e9SSakshi Kaushik     } else {
406747421e9SSakshi Kaushik         lsock = opt_fdnum;
4070644fe4aSMarc-André Lureau     }
40853a2e1b5SMarc-André Lureau 
40953a2e1b5SMarc-André Lureau     csock = accept(lsock, NULL, NULL);
41053a2e1b5SMarc-André Lureau     if (csock < 0) {
41153a2e1b5SMarc-André Lureau         perror("accept");
41253a2e1b5SMarc-André Lureau         goto err;
41353a2e1b5SMarc-André Lureau     }
41453a2e1b5SMarc-André Lureau 
41553a2e1b5SMarc-André Lureau     vdev_scsi = g_new0(VusDev, 1);
41653a2e1b5SMarc-André Lureau     vdev_scsi->loop = g_main_loop_new(NULL, FALSE);
41749cc0340SFelipe Franciosi 
418ca853813SMarc-André Lureau     if (vus_iscsi_add_lun(&vdev_scsi->lun, iscsi_uri) != 0) {
41949cc0340SFelipe Franciosi         goto err;
42049cc0340SFelipe Franciosi     }
42149cc0340SFelipe Franciosi 
4226f5fd837SStefan Hajnoczi     if (!vug_init(&vdev_scsi->parent, VHOST_USER_SCSI_MAX_QUEUES, csock,
4236f5fd837SStefan Hajnoczi                   vus_panic_cb, &vus_iface)) {
4246f5fd837SStefan Hajnoczi         g_printerr("Failed to initialize libvhost-user-glib\n");
4256f5fd837SStefan Hajnoczi         goto err;
4266f5fd837SStefan Hajnoczi     }
42753a2e1b5SMarc-André Lureau 
42853a2e1b5SMarc-André Lureau     g_main_loop_run(vdev_scsi->loop);
42953a2e1b5SMarc-André Lureau 
43053a2e1b5SMarc-André Lureau     vug_deinit(&vdev_scsi->parent);
43149cc0340SFelipe Franciosi 
43249cc0340SFelipe Franciosi out:
433245792ddSMarc-André Lureau     if (vdev_scsi) {
43453a2e1b5SMarc-André Lureau         g_main_loop_unref(vdev_scsi->loop);
43553a2e1b5SMarc-André Lureau         g_free(vdev_scsi);
436245792ddSMarc-André Lureau     }
43753a2e1b5SMarc-André Lureau     if (csock >= 0) {
43853a2e1b5SMarc-André Lureau         close(csock);
43953a2e1b5SMarc-André Lureau     }
44053a2e1b5SMarc-André Lureau     if (lsock >= 0) {
44153a2e1b5SMarc-André Lureau         close(lsock);
442*6852c21dSStefan Hajnoczi 
443*6852c21dSStefan Hajnoczi         if (opt_socket_path) {
444*6852c21dSStefan Hajnoczi             unlink(opt_socket_path);
445*6852c21dSStefan Hajnoczi         }
44653a2e1b5SMarc-André Lureau     }
447747421e9SSakshi Kaushik     g_free(opt_socket_path);
448b085b050SMarc-André Lureau     g_free(iscsi_uri);
44949cc0340SFelipe Franciosi 
45049cc0340SFelipe Franciosi     return err;
45149cc0340SFelipe Franciosi 
45249cc0340SFelipe Franciosi err:
45349cc0340SFelipe Franciosi     err = EXIT_FAILURE;
45449cc0340SFelipe Franciosi     goto out;
45549cc0340SFelipe Franciosi 
45649cc0340SFelipe Franciosi help:
457747421e9SSakshi Kaushik     fprintf(stderr, "Usage: %s [ -s socket-path -i iscsi-uri -f fd -p print-capabilities ] | [ -h ]\n",
45849cc0340SFelipe Franciosi             argv[0]);
459747421e9SSakshi Kaushik     fprintf(stderr, "          -s, --socket-path=SOCKET_PATH path to unix socket\n");
460747421e9SSakshi Kaushik     fprintf(stderr, "          -i, --iscsi-uri=ISCSI_URI iscsi uri for lun 0\n");
461747421e9SSakshi Kaushik     fprintf(stderr, "          -f, --fd=FILE_DESCRIPTOR file-descriptor\n");
462747421e9SSakshi Kaushik     fprintf(stderr, "          -p, --print-capabilities=PRINT_CAPABILITIES denotes print-capabilities\n");
46349cc0340SFelipe Franciosi     fprintf(stderr, "          -h print help and quit\n");
46449cc0340SFelipe Franciosi 
46549cc0340SFelipe Franciosi     goto err;
46649cc0340SFelipe Franciosi }
467