ati-vga: Fix frame buffer endianness for big endian target
[qemu/ar7.git] / contrib / vhost-user-scsi / vhost-user-scsi.c
blob496dd6e693b3bde1e69c063bfd3107e5c7546f72
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"
20 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
22 typedef struct VusIscsiLun {
23 struct iscsi_context *iscsi_ctx;
24 int iscsi_lun;
25 } VusIscsiLun;
27 typedef struct VusDev {
28 VugDev parent;
30 VusIscsiLun lun;
31 GMainLoop *loop;
32 } VusDev;
34 /** libiscsi integration **/
36 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq;
37 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp;
39 static int vus_iscsi_add_lun(VusIscsiLun *lun, char *iscsi_uri)
41 struct iscsi_url *iscsi_url;
42 struct iscsi_context *iscsi_ctx;
43 int ret = 0;
45 assert(lun);
46 assert(iscsi_uri);
47 assert(!lun->iscsi_ctx);
49 iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
50 if (!iscsi_ctx) {
51 g_warning("Unable to create iSCSI context");
52 return -1;
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;
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;
69 lun->iscsi_ctx = iscsi_ctx;
70 lun->iscsi_lun = iscsi_url->lun;
72 g_debug("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
74 out:
75 if (iscsi_url) {
76 iscsi_destroy_url(iscsi_url);
78 return ret;
80 fail:
81 (void)iscsi_destroy_context(iscsi_ctx);
82 ret = -1;
83 goto out;
86 static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
87 int xfer_len)
89 struct scsi_task *task;
91 assert(cdb_len > 0);
92 assert(cdb);
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;
100 return task;
103 static int get_cdb_len(uint8_t *cdb)
105 assert(cdb);
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;
114 g_warning("Unable to determine cdb len (0x%02hhX)", cdb[0] >> 5);
115 return -1;
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)
124 struct scsi_task *task;
125 uint32_t dir;
126 uint32_t len;
127 int cdb_len;
128 int i;
130 assert(ctx);
131 assert(req);
132 assert(rsp);
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;
146 return 0;
149 cdb_len = get_cdb_len(req->cdb);
150 if (cdb_len == -1) {
151 return -1;
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;
162 } else {
163 dir = SCSI_XFER_READ;
164 for (i = 0; i < in_len; i++) {
165 len += in[i].iov_len;
169 task = scsi_task_new(cdb_len, req->cdb, dir, len);
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;
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;
187 memset(rsp, 0, sizeof(*rsp));
189 rsp->status = task->status;
190 rsp->resid = task->residual;
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);
198 g_free(task);
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);
203 return 0;
206 /** libvhost-user callbacks **/
208 static void vus_panic_cb(VuDev *vu_dev, const char *buf)
210 VugDev *gdev;
211 VusDev *vdev_scsi;
213 assert(vu_dev);
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);
221 g_main_loop_quit(vdev_scsi->loop);
224 static void vus_proc_req(VuDev *vu_dev, int idx)
226 VugDev *gdev;
227 VusDev *vdev_scsi;
228 VuVirtq *vq;
230 assert(vu_dev);
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;
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;
247 g_debug("Got kicked on vq[%d]@%p", idx, vq);
249 while (1) {
250 VuVirtqElement *elem;
251 VirtIOSCSICmdReq *req;
252 VirtIOSCSICmdResp *rsp;
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;
259 g_debug("Popped elem@%p", elem);
261 assert(!(elem->out_num > 1 && elem->in_num > 1));
262 assert(elem->out_num > 0 && elem->in_num > 0);
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;
269 req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
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;
276 rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
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;
285 vu_queue_push(vu_dev, vq, elem, 0);
286 vu_queue_notify(vu_dev, vq);
288 free(elem);
292 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
294 VuVirtq *vq;
296 assert(vu_dev);
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;
304 vq = vu_get_queue(vu_dev, idx);
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);
313 static const VuDevIface vus_iface = {
314 .queue_set_started = vus_queue_set_started,
317 /** misc helpers **/
319 static int unix_sock_new(char *unix_fn)
321 int sock;
322 struct sockaddr_un un;
323 size_t len;
325 assert(unix_fn);
327 sock = socket(AF_UNIX, SOCK_STREAM, 0);
328 if (sock <= 0) {
329 perror("socket");
330 return -1;
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);
337 (void)unlink(unix_fn);
338 if (bind(sock, (struct sockaddr *)&un, len) < 0) {
339 perror("bind");
340 goto fail;
343 if (listen(sock, 1) < 0) {
344 perror("listen");
345 goto fail;
348 return sock;
350 fail:
351 (void)close(sock);
353 return -1;
356 /** vhost-user-scsi **/
358 int main(int argc, char **argv)
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;
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;
379 if (!unix_fn || !iscsi_uri) {
380 goto help;
383 lsock = unix_sock_new(unix_fn);
384 if (lsock < 0) {
385 goto err;
388 csock = accept(lsock, NULL, NULL);
389 if (csock < 0) {
390 perror("accept");
391 goto err;
394 vdev_scsi = g_new0(VusDev, 1);
395 vdev_scsi->loop = g_main_loop_new(NULL, FALSE);
397 if (vus_iscsi_add_lun(&vdev_scsi->lun, iscsi_uri) != 0) {
398 goto err;
401 vug_init(&vdev_scsi->parent, csock, vus_panic_cb, &vus_iface);
403 g_main_loop_run(vdev_scsi->loop);
405 vug_deinit(&vdev_scsi->parent);
407 out:
408 if (vdev_scsi) {
409 g_main_loop_unref(vdev_scsi->loop);
410 g_free(vdev_scsi);
411 unlink(unix_fn);
413 if (csock >= 0) {
414 close(csock);
416 if (lsock >= 0) {
417 close(lsock);
419 g_free(unix_fn);
420 g_free(iscsi_uri);
422 return err;
424 err:
425 err = EXIT_FAILURE;
426 goto out;
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");
435 goto err;