Remove the deprecated -nodefconfig option
[qemu.git] / contrib / vhost-user-scsi / vhost-user-scsi.c
blob02c29019d17c6dd0fcb797057a4d1b1c2ed4fdd0
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 <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"
19 #include <glib.h>
21 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
23 typedef struct VusIscsiLun {
24 struct iscsi_context *iscsi_ctx;
25 int iscsi_lun;
26 } VusIscsiLun;
28 typedef struct VusDev {
29 VugDev parent;
31 VusIscsiLun lun;
32 GMainLoop *loop;
33 } VusDev;
35 /** libiscsi integration **/
37 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq;
38 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp;
40 static int vus_iscsi_add_lun(VusIscsiLun *lun, char *iscsi_uri)
42 struct iscsi_url *iscsi_url;
43 struct iscsi_context *iscsi_ctx;
44 int ret = 0;
46 assert(lun);
47 assert(iscsi_uri);
48 assert(!lun->iscsi_ctx);
50 iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
51 if (!iscsi_ctx) {
52 g_warning("Unable to create iSCSI context");
53 return -1;
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;
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;
70 lun->iscsi_ctx = iscsi_ctx;
71 lun->iscsi_lun = iscsi_url->lun;
73 g_debug("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
75 out:
76 if (iscsi_url) {
77 iscsi_destroy_url(iscsi_url);
79 return ret;
81 fail:
82 (void)iscsi_destroy_context(iscsi_ctx);
83 ret = -1;
84 goto out;
87 static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
88 int xfer_len)
90 struct scsi_task *task;
92 assert(cdb_len > 0);
93 assert(cdb);
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;
101 return task;
104 static int get_cdb_len(uint8_t *cdb)
106 assert(cdb);
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;
115 g_warning("Unable to determine cdb len (0x%02hhX)", cdb[0] >> 5);
116 return -1;
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)
125 struct scsi_task *task;
126 uint32_t dir;
127 uint32_t len;
128 int cdb_len;
129 int i;
131 assert(ctx);
132 assert(req);
133 assert(rsp);
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;
147 return 0;
150 cdb_len = get_cdb_len(req->cdb);
151 if (cdb_len == -1) {
152 return -1;
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;
163 } else {
164 dir = SCSI_XFER_READ;
165 for (i = 0; i < in_len; i++) {
166 len += in[i].iov_len;
170 task = scsi_task_new(cdb_len, req->cdb, dir, len);
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;
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;
188 memset(rsp, 0, sizeof(*rsp));
190 rsp->status = task->status;
191 rsp->resid = task->residual;
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);
199 g_free(task);
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);
204 return 0;
207 /** libvhost-user callbacks **/
209 static void vus_panic_cb(VuDev *vu_dev, const char *buf)
211 VugDev *gdev;
212 VusDev *vdev_scsi;
214 assert(vu_dev);
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);
222 g_main_loop_quit(vdev_scsi->loop);
225 static void vus_proc_req(VuDev *vu_dev, int idx)
227 VugDev *gdev;
228 VusDev *vdev_scsi;
229 VuVirtq *vq;
231 assert(vu_dev);
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;
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;
248 g_debug("Got kicked on vq[%d]@%p", idx, vq);
250 while (1) {
251 VuVirtqElement *elem;
252 VirtIOSCSICmdReq *req;
253 VirtIOSCSICmdResp *rsp;
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;
260 g_debug("Popped elem@%p", elem);
262 assert(!(elem->out_num > 1 && elem->in_num > 1));
263 assert(elem->out_num > 0 && elem->in_num > 0);
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;
270 req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
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;
277 rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
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;
286 vu_queue_push(vu_dev, vq, elem, 0);
287 vu_queue_notify(vu_dev, vq);
289 free(elem);
293 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
295 VuVirtq *vq;
297 assert(vu_dev);
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;
305 vq = vu_get_queue(vu_dev, idx);
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);
314 static const VuDevIface vus_iface = {
315 .queue_set_started = vus_queue_set_started,
318 /** misc helpers **/
320 static int unix_sock_new(char *unix_fn)
322 int sock;
323 struct sockaddr_un un;
324 size_t len;
326 assert(unix_fn);
328 sock = socket(AF_UNIX, SOCK_STREAM, 0);
329 if (sock <= 0) {
330 perror("socket");
331 return -1;
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);
338 (void)unlink(unix_fn);
339 if (bind(sock, (struct sockaddr *)&un, len) < 0) {
340 perror("bind");
341 goto fail;
344 if (listen(sock, 1) < 0) {
345 perror("listen");
346 goto fail;
349 return sock;
351 fail:
352 (void)close(sock);
354 return -1;
357 /** vhost-user-scsi **/
359 int main(int argc, char **argv)
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;
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;
380 if (!unix_fn || !iscsi_uri) {
381 goto help;
384 lsock = unix_sock_new(unix_fn);
385 if (lsock < 0) {
386 goto err;
389 csock = accept(lsock, NULL, NULL);
390 if (csock < 0) {
391 perror("accept");
392 goto err;
395 vdev_scsi = g_new0(VusDev, 1);
396 vdev_scsi->loop = g_main_loop_new(NULL, FALSE);
398 if (vus_iscsi_add_lun(&vdev_scsi->lun, iscsi_uri) != 0) {
399 goto err;
402 vug_init(&vdev_scsi->parent, csock, vus_panic_cb, &vus_iface);
404 g_main_loop_run(vdev_scsi->loop);
406 vug_deinit(&vdev_scsi->parent);
408 out:
409 if (vdev_scsi) {
410 g_main_loop_unref(vdev_scsi->loop);
411 g_free(vdev_scsi);
412 unlink(unix_fn);
414 if (csock >= 0) {
415 close(csock);
417 if (lsock >= 0) {
418 close(lsock);
420 g_free(unix_fn);
421 g_free(iscsi_uri);
423 return err;
425 err:
426 err = EXIT_FAILURE;
427 goto out;
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");
436 goto err;