vhost-user-scsi: also free the gtree
[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_MAX_DEVS 1
59
60 #define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
61
62 typedef struct iscsi_lun {
63 struct iscsi_context *iscsi_ctx;
64 int iscsi_lun;
65 } iscsi_lun_t;
66
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;
74
75 static vhost_scsi_dev_t *vhost_scsi_devs[VUS_MAX_DEVS];
76
77 /** glib event loop integration for libvhost-user and misc callbacks **/
78
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);
84
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;
91
92 static gint vus_fdmap_compare(gconstpointer a, gconstpointer b)
93 {
94 return (b > a) - (b < a);
95 }
96
97 static gboolean vus_gsrc_prepare(GSource *src, gint *timeout)
98 {
99 assert(timeout);
100
101 *timeout = -1;
102 return FALSE;
103 }
104
105 static gboolean vus_gsrc_check(GSource *src)
106 {
107 vus_gsrc_t *vus_src = (vus_gsrc_t *)src;
108
109 assert(vus_src);
110
111 return vus_src->gfd.revents & vus_src->gfd.events;
112 }
113
114 static gboolean vus_gsrc_dispatch(GSource *src, GSourceFunc cb, gpointer data)
115 {
116 vhost_scsi_dev_t *vdev_scsi;
117 vus_gsrc_t *vus_src = (vus_gsrc_t *)src;
118
119 assert(vus_src);
120 assert(!(vus_src->vu_cb && cb));
121
122 vdev_scsi = vus_src->vdev_scsi;
123
124 assert(vdev_scsi);
125
126 if (cb) {
127 return cb(data);
128 }
129 if (vus_src->vu_cb) {
130 vus_src->vu_cb(&vdev_scsi->vu_dev, vus_src->gfd.revents, data);
131 }
132 return G_SOURCE_CONTINUE;
133 }
134
135 static GSourceFuncs vus_gsrc_funcs = {
136 vus_gsrc_prepare,
137 vus_gsrc_check,
138 vus_gsrc_dispatch,
139 NULL
140 };
141
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)
144 {
145 GSource *vus_gsrc;
146 vus_gsrc_t *vus_src;
147 guint id;
148
149 assert(vdev_scsi);
150 assert(fd >= 0);
151 assert(vu_cb || gsrc_cb);
152 assert(!(vu_cb && gsrc_cb));
153
154 vus_gsrc = g_source_new(&vus_gsrc_funcs, sizeof(vus_gsrc_t));
155 vus_src = (vus_gsrc_t *)vus_gsrc;
156
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;
161
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);
167
168 g_tree_insert(vdev_scsi->fdmap, (gpointer)(uintptr_t)fd,
169 (gpointer)(uintptr_t)id);
170 }
171
172 /* from libiscsi's scsi-lowlevel.h **
173 *
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".
176 */
177
178 #define SCSI_CDB_MAX_SIZE 16
179
180 struct scsi_iovector {
181 struct scsi_iovec *iov;
182 int niov;
183 int nalloc;
184 size_t offset;
185 int consumed;
186 };
187
188 struct scsi_allocated_memory {
189 struct scsi_allocated_memory *next;
190 char buf[0];
191 };
192
193 struct scsi_data {
194 int size;
195 unsigned char *data;
196 };
197
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
214 };
215
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;
225 };
226
227 enum scsi_residual {
228 SCSI_RESIDUAL_NO_RESIDUAL = 0,
229 SCSI_RESIDUAL_UNDERFLOW,
230 SCSI_RESIDUAL_OVERFLOW
231 };
232
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;
245
246 uint32_t itt;
247 uint32_t cmdsn;
248 uint32_t lun;
249
250 struct scsi_iovector iovector_in;
251 struct scsi_iovector iovector_out;
252 };
253
254 /** libiscsi integration **/
255
256 static int iscsi_add_lun(iscsi_lun_t *lun, char *iscsi_uri)
257 {
258 struct iscsi_url *iscsi_url;
259 struct iscsi_context *iscsi_ctx;
260 int ret = 0;
261
262 assert(lun);
263 assert(iscsi_uri);
264
265 iscsi_ctx = iscsi_create_context(VUS_ISCSI_INITIATOR);
266 if (!iscsi_ctx) {
267 PERR("Unable to create iSCSI context");
268 return -1;
269 }
270
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;
275 }
276
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;
282 }
283
284 lun->iscsi_ctx = iscsi_ctx;
285 lun->iscsi_lun = iscsi_url->lun;
286
287 PDBG("Context %p created for lun 0: %s", iscsi_ctx, iscsi_uri);
288
289 out:
290 if (iscsi_url) {
291 iscsi_destroy_url(iscsi_url);
292 }
293 return ret;
294
295 fail:
296 (void)iscsi_destroy_context(iscsi_ctx);
297 ret = -1;
298 goto out;
299 }
300
301 static struct scsi_task *scsi_task_new(int cdb_len, uint8_t *cdb, int dir,
302 int xfer_len)
303 {
304 struct scsi_task *task;
305
306 assert(cdb_len > 0);
307 assert(cdb);
308
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;
314
315 return task;
316 }
317
318 static int get_cdb_len(uint8_t *cdb)
319 {
320 assert(cdb);
321
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;
328 }
329 PERR("Unable to determine cdb len (0x%02hhX)", cdb[0] >> 5);
330 return -1;
331 }
332
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)
338 {
339 struct scsi_task *task;
340 uint32_t dir;
341 uint32_t len;
342 int cdb_len;
343 int i;
344
345 assert(ctx);
346 assert(req);
347 assert(rsp);
348
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;
360
361 return 0;
362 }
363
364 cdb_len = get_cdb_len(req->cdb);
365 if (cdb_len == -1) {
366 return -1;
367 }
368
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;
376 }
377 } else {
378 dir = SCSI_XFER_FROM_DEV;
379 for (i = 0; i < in_len; i++) {
380 len += in[i].iov_len;
381 }
382 }
383
384 task = scsi_task_new(cdb_len, req->cdb, dir, len);
385
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;
392 }
393
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;
400 }
401
402 memset(rsp, 0, sizeof(*rsp));
403
404 rsp->status = task->status;
405 rsp->resid = task->residual;
406
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);
411 }
412
413 g_free(task);
414
415 PDBG("Filled in rsp: status=%hhX, resid=%u, response=%hhX, sense_len=%u",
416 rsp->status, rsp->resid, rsp->response, rsp->sense_len);
417
418 return 0;
419 }
420
421 /** libvhost-user callbacks **/
422
423 static vhost_scsi_dev_t *vdev_scsi_find_by_vu(VuDev *vu_dev);
424
425 static void vus_panic_cb(VuDev *vu_dev, const char *buf)
426 {
427 vhost_scsi_dev_t *vdev_scsi;
428
429 assert(vu_dev);
430
431 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
432
433 if (buf) {
434 PERR("vu_panic: %s", buf);
435 }
436
437 g_main_loop_quit(vdev_scsi->loop);
438 }
439
440 static void vus_add_watch_cb(VuDev *vu_dev, int fd, int vu_evt, vu_watch_cb cb,
441 void *pvt)
442 {
443 vhost_scsi_dev_t *vdev_scsi;
444 guint id;
445
446 assert(vu_dev);
447 assert(fd >= 0);
448 assert(cb);
449
450 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
451 if (!vdev_scsi) {
452 vus_panic_cb(vu_dev, NULL);
453 return;
454 }
455
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);
463 }
464
465 vus_gsrc_new(vdev_scsi, fd, vu_evt, cb, NULL, pvt);
466 }
467
468 static void vus_del_watch_cb(VuDev *vu_dev, int fd)
469 {
470 vhost_scsi_dev_t *vdev_scsi;
471 guint id;
472
473 assert(vu_dev);
474 assert(fd >= 0);
475
476 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
477 if (!vdev_scsi) {
478 vus_panic_cb(vu_dev, NULL);
479 return;
480 }
481
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);
489 }
490 }
491
492 static void vus_proc_ctl(VuDev *vu_dev, int idx)
493 {
494 /* Control VQ not implemented */
495 }
496
497 static void vus_proc_evt(VuDev *vu_dev, int idx)
498 {
499 /* Event VQ not implemented */
500 }
501
502 static void vus_proc_req(VuDev *vu_dev, int idx)
503 {
504 vhost_scsi_dev_t *vdev_scsi;
505 VuVirtq *vq;
506
507 assert(vu_dev);
508
509 vdev_scsi = vdev_scsi_find_by_vu(vu_dev);
510 if (!vdev_scsi) {
511 vus_panic_cb(vu_dev, NULL);
512 return;
513 }
514
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;
519 }
520
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;
526 }
527
528 PDBG("Got kicked on vq[%d]@%p", idx, vq);
529
530 while (1) {
531 VuVirtqElement *elem;
532 VirtIOSCSICmdReq *req;
533 VirtIOSCSICmdResp *rsp;
534
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;
539 }
540 PDBG("Popped elem@%p", elem);
541
542 assert(!(elem->out_num > 1 && elem->in_num > 1));
543 assert(elem->out_num > 0 && elem->in_num > 0);
544
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;
549 }
550 req = (VirtIOSCSICmdReq *)elem->out_sg[0].iov_base;
551
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;
556 }
557 rsp = (VirtIOSCSICmdResp *)elem->in_sg[0].iov_base;
558
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;
564 }
565
566 vu_queue_push(vu_dev, vq, elem, 0);
567 vu_queue_notify(vu_dev, vq);
568
569 free(elem);
570 }
571 }
572
573 static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
574 {
575 VuVirtq *vq;
576
577 assert(vu_dev);
578
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;
583 }
584
585 vq = vu_get_queue(vu_dev, idx);
586
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);
596 }
597 }
598
599 static const VuDevIface vus_iface = {
600 .queue_set_started = vus_queue_set_started,
601 };
602
603 static gboolean vus_vhost_cb(gpointer data)
604 {
605 VuDev *vu_dev = (VuDev *)data;
606
607 assert(vu_dev);
608
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;
613 }
614
615 return G_SOURCE_CONTINUE;
616 }
617
618 /** misc helpers **/
619
620 static int unix_sock_new(char *unix_fn)
621 {
622 int sock;
623 struct sockaddr_un un;
624 size_t len;
625
626 assert(unix_fn);
627
628 sock = socket(AF_UNIX, SOCK_STREAM, 0);
629 if (sock <= 0) {
630 perror("socket");
631 return -1;
632 }
633
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);
637
638 (void)unlink(unix_fn);
639 if (bind(sock, (struct sockaddr *)&un, len) < 0) {
640 perror("bind");
641 goto fail;
642 }
643
644 if (listen(sock, 1) < 0) {
645 perror("listen");
646 goto fail;
647 }
648
649 return sock;
650
651 fail:
652 (void)close(sock);
653
654 return -1;
655 }
656
657 /** vhost-user-scsi **/
658
659 static vhost_scsi_dev_t *vdev_scsi_find_by_vu(VuDev *vu_dev)
660 {
661 int i;
662
663 assert(vu_dev);
664
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];
668 }
669 }
670
671 PERR("Unknown VuDev %p", vu_dev);
672 return NULL;
673 }
674
675 static void vdev_scsi_free(vhost_scsi_dev_t *vdev_scsi)
676 {
677 if (!vdev_scsi) {
678 return;
679 }
680
681 if (vdev_scsi->server_sock >= 0) {
682 struct sockaddr_storage ss;
683 socklen_t sslen = sizeof(ss);
684
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);
689 }
690
691 (void)close(vdev_scsi->server_sock);
692 vdev_scsi->server_sock = -1;
693 }
694
695 g_main_loop_unref(vdev_scsi->loop);
696 g_tree_destroy(vdev_scsi->fdmap);
697 g_free(vdev_scsi);
698 }
699
700 static vhost_scsi_dev_t *vdev_scsi_new(int server_sock)
701 {
702 vhost_scsi_dev_t *vdev_scsi;
703
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);
708
709 return vdev_scsi;
710 }
711
712 static int vdev_scsi_add_iscsi_lun(vhost_scsi_dev_t *vdev_scsi,
713 char *iscsi_uri, uint32_t lun)
714 {
715 assert(vdev_scsi);
716 assert(iscsi_uri);
717 assert(lun < VUS_MAX_LUNS);
718
719 if (vdev_scsi->luns[lun].iscsi_ctx) {
720 PERR("Lun %d already configured", lun);
721 return -1;
722 }
723
724 if (iscsi_add_lun(&vdev_scsi->luns[lun], iscsi_uri) != 0) {
725 return -1;
726 }
727
728 return 0;
729 }
730
731 static int vdev_scsi_run(vhost_scsi_dev_t *vdev_scsi)
732 {
733 int cli_sock;
734 int ret = 0;
735
736 assert(vdev_scsi);
737 assert(vdev_scsi->server_sock >= 0);
738 assert(vdev_scsi->loop);
739
740 cli_sock = accept(vdev_scsi->server_sock, (void *)0, (void *)0);
741 if (cli_sock < 0) {
742 perror("accept");
743 return -1;
744 }
745
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);
752
753 vus_gsrc_new(vdev_scsi, cli_sock, G_IO_IN, NULL, vus_vhost_cb,
754 &vdev_scsi->vu_dev);
755
756 g_main_loop_run(vdev_scsi->loop);
757
758 vu_deinit(&vdev_scsi->vu_dev);
759
760 return ret;
761 }
762
763 int main(int argc, char **argv)
764 {
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;
769
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;
782 }
783 }
784 if (!unix_fn || !iscsi_uri) {
785 goto help;
786 }
787
788 sock = unix_sock_new(unix_fn);
789 if (sock < 0) {
790 goto err;
791 }
792 vdev_scsi = vdev_scsi_new(sock);
793 vhost_scsi_devs[0] = vdev_scsi;
794
795 if (vdev_scsi_add_iscsi_lun(vdev_scsi, iscsi_uri, 0) != 0) {
796 goto err;
797 }
798
799 if (vdev_scsi_run(vdev_scsi) != 0) {
800 goto err;
801 }
802
803 out:
804 vdev_scsi_free(vdev_scsi);
805 g_free(unix_fn);
806 g_free(iscsi_uri);
807
808 return err;
809
810 err:
811 err = EXIT_FAILURE;
812 goto out;
813
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");
820
821 goto err;
822 }