]> git.proxmox.com Git - qemu.git/blob - block/iscsi.c
iscsi: add support for iSCSI NOPs [v2]
[qemu.git] / block / iscsi.c
1 /*
2 * QEMU Block driver for iSCSI images
3 *
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #include "config-host.h"
26
27 #include <poll.h>
28 #include <arpa/inet.h>
29 #include "qemu-common.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "block/block_int.h"
33 #include "trace.h"
34 #include "hw/scsi-defs.h"
35
36 #include <iscsi/iscsi.h>
37 #include <iscsi/scsi-lowlevel.h>
38
39 #ifdef __linux__
40 #include <scsi/sg.h>
41 #include <hw/scsi-defs.h>
42 #endif
43
44 typedef struct IscsiLun {
45 struct iscsi_context *iscsi;
46 int lun;
47 enum scsi_inquiry_peripheral_device_type type;
48 int block_size;
49 uint64_t num_blocks;
50 int events;
51 QEMUTimer *nop_timer;
52 } IscsiLun;
53
54 typedef struct IscsiAIOCB {
55 BlockDriverAIOCB common;
56 QEMUIOVector *qiov;
57 QEMUBH *bh;
58 IscsiLun *iscsilun;
59 struct scsi_task *task;
60 uint8_t *buf;
61 int status;
62 int canceled;
63 size_t read_size;
64 size_t read_offset;
65 #ifdef __linux__
66 sg_io_hdr_t *ioh;
67 #endif
68 } IscsiAIOCB;
69
70 #define NOP_INTERVAL 5000
71 #define MAX_NOP_FAILURES 3
72
73 static void
74 iscsi_bh_cb(void *p)
75 {
76 IscsiAIOCB *acb = p;
77
78 qemu_bh_delete(acb->bh);
79
80 if (acb->canceled == 0) {
81 acb->common.cb(acb->common.opaque, acb->status);
82 }
83
84 if (acb->task != NULL) {
85 scsi_free_scsi_task(acb->task);
86 acb->task = NULL;
87 }
88
89 qemu_aio_release(acb);
90 }
91
92 static void
93 iscsi_schedule_bh(IscsiAIOCB *acb)
94 {
95 if (acb->bh) {
96 return;
97 }
98 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
99 qemu_bh_schedule(acb->bh);
100 }
101
102
103 static void
104 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
105 void *private_data)
106 {
107 IscsiAIOCB *acb = private_data;
108
109 acb->status = -ECANCELED;
110 iscsi_schedule_bh(acb);
111 }
112
113 static void
114 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
115 {
116 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
117 IscsiLun *iscsilun = acb->iscsilun;
118
119 if (acb->status != -EINPROGRESS) {
120 return;
121 }
122
123 acb->canceled = 1;
124
125 /* send a task mgmt call to the target to cancel the task on the target */
126 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
127 iscsi_abort_task_cb, acb);
128
129 while (acb->status == -EINPROGRESS) {
130 qemu_aio_wait();
131 }
132 }
133
134 static const AIOCBInfo iscsi_aiocb_info = {
135 .aiocb_size = sizeof(IscsiAIOCB),
136 .cancel = iscsi_aio_cancel,
137 };
138
139
140 static void iscsi_process_read(void *arg);
141 static void iscsi_process_write(void *arg);
142
143 static int iscsi_process_flush(void *arg)
144 {
145 IscsiLun *iscsilun = arg;
146
147 return iscsi_queue_length(iscsilun->iscsi) > 0;
148 }
149
150 static void
151 iscsi_set_events(IscsiLun *iscsilun)
152 {
153 struct iscsi_context *iscsi = iscsilun->iscsi;
154 int ev;
155
156 /* We always register a read handler. */
157 ev = POLLIN;
158 ev |= iscsi_which_events(iscsi);
159 if (ev != iscsilun->events) {
160 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
161 iscsi_process_read,
162 (ev & POLLOUT) ? iscsi_process_write : NULL,
163 iscsi_process_flush,
164 iscsilun);
165
166 }
167
168 iscsilun->events = ev;
169 }
170
171 static void
172 iscsi_process_read(void *arg)
173 {
174 IscsiLun *iscsilun = arg;
175 struct iscsi_context *iscsi = iscsilun->iscsi;
176
177 iscsi_service(iscsi, POLLIN);
178 iscsi_set_events(iscsilun);
179 }
180
181 static void
182 iscsi_process_write(void *arg)
183 {
184 IscsiLun *iscsilun = arg;
185 struct iscsi_context *iscsi = iscsilun->iscsi;
186
187 iscsi_service(iscsi, POLLOUT);
188 iscsi_set_events(iscsilun);
189 }
190
191
192 static void
193 iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
194 void *command_data, void *opaque)
195 {
196 IscsiAIOCB *acb = opaque;
197
198 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
199
200 g_free(acb->buf);
201
202 if (acb->canceled != 0) {
203 return;
204 }
205
206 acb->status = 0;
207 if (status < 0) {
208 error_report("Failed to write16 data to iSCSI lun. %s",
209 iscsi_get_error(iscsi));
210 acb->status = -EIO;
211 }
212
213 iscsi_schedule_bh(acb);
214 }
215
216 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
217 {
218 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
219 }
220
221 static BlockDriverAIOCB *
222 iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
223 QEMUIOVector *qiov, int nb_sectors,
224 BlockDriverCompletionFunc *cb,
225 void *opaque)
226 {
227 IscsiLun *iscsilun = bs->opaque;
228 struct iscsi_context *iscsi = iscsilun->iscsi;
229 IscsiAIOCB *acb;
230 size_t size;
231 uint32_t num_sectors;
232 uint64_t lba;
233 struct iscsi_data data;
234
235 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
236 trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
237
238 acb->iscsilun = iscsilun;
239 acb->qiov = qiov;
240
241 acb->canceled = 0;
242 acb->bh = NULL;
243 acb->status = -EINPROGRESS;
244
245 /* XXX we should pass the iovec to write16 to avoid the extra copy */
246 /* this will allow us to get rid of 'buf' completely */
247 size = nb_sectors * BDRV_SECTOR_SIZE;
248 data.size = MIN(size, acb->qiov->size);
249
250 /* if the iovec only contains one buffer we can pass it directly */
251 if (acb->qiov->niov == 1) {
252 acb->buf = NULL;
253 data.data = acb->qiov->iov[0].iov_base;
254 } else {
255 acb->buf = g_malloc(data.size);
256 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
257 data.data = acb->buf;
258 }
259
260 acb->task = malloc(sizeof(struct scsi_task));
261 if (acb->task == NULL) {
262 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
263 "command. %s", iscsi_get_error(iscsi));
264 qemu_aio_release(acb);
265 return NULL;
266 }
267 memset(acb->task, 0, sizeof(struct scsi_task));
268
269 acb->task->xfer_dir = SCSI_XFER_WRITE;
270 acb->task->cdb_size = 16;
271 acb->task->cdb[0] = 0x8a;
272 lba = sector_qemu2lun(sector_num, iscsilun);
273 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
274 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
275 num_sectors = size / iscsilun->block_size;
276 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
277 acb->task->expxferlen = size;
278
279 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
280 iscsi_aio_write16_cb,
281 &data,
282 acb) != 0) {
283 scsi_free_scsi_task(acb->task);
284 g_free(acb->buf);
285 qemu_aio_release(acb);
286 return NULL;
287 }
288
289 iscsi_set_events(iscsilun);
290
291 return &acb->common;
292 }
293
294 static void
295 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
296 void *command_data, void *opaque)
297 {
298 IscsiAIOCB *acb = opaque;
299
300 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
301
302 if (acb->canceled != 0) {
303 return;
304 }
305
306 acb->status = 0;
307 if (status != 0) {
308 error_report("Failed to read16 data from iSCSI lun. %s",
309 iscsi_get_error(iscsi));
310 acb->status = -EIO;
311 }
312
313 iscsi_schedule_bh(acb);
314 }
315
316 static BlockDriverAIOCB *
317 iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
318 QEMUIOVector *qiov, int nb_sectors,
319 BlockDriverCompletionFunc *cb,
320 void *opaque)
321 {
322 IscsiLun *iscsilun = bs->opaque;
323 struct iscsi_context *iscsi = iscsilun->iscsi;
324 IscsiAIOCB *acb;
325 size_t qemu_read_size;
326 int i;
327 uint64_t lba;
328 uint32_t num_sectors;
329
330 qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
331
332 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
333 trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
334
335 acb->iscsilun = iscsilun;
336 acb->qiov = qiov;
337
338 acb->canceled = 0;
339 acb->bh = NULL;
340 acb->status = -EINPROGRESS;
341 acb->read_size = qemu_read_size;
342 acb->buf = NULL;
343
344 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
345 * may be misaligned to the LUN, so we may need to read some extra
346 * data.
347 */
348 acb->read_offset = 0;
349 if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
350 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
351
352 acb->read_offset = bdrv_offset % iscsilun->block_size;
353 }
354
355 num_sectors = (qemu_read_size + iscsilun->block_size
356 + acb->read_offset - 1)
357 / iscsilun->block_size;
358
359 acb->task = malloc(sizeof(struct scsi_task));
360 if (acb->task == NULL) {
361 error_report("iSCSI: Failed to allocate task for scsi READ16 "
362 "command. %s", iscsi_get_error(iscsi));
363 qemu_aio_release(acb);
364 return NULL;
365 }
366 memset(acb->task, 0, sizeof(struct scsi_task));
367
368 acb->task->xfer_dir = SCSI_XFER_READ;
369 lba = sector_qemu2lun(sector_num, iscsilun);
370 acb->task->expxferlen = qemu_read_size;
371
372 switch (iscsilun->type) {
373 case TYPE_DISK:
374 acb->task->cdb_size = 16;
375 acb->task->cdb[0] = 0x88;
376 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
377 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
378 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
379 break;
380 default:
381 acb->task->cdb_size = 10;
382 acb->task->cdb[0] = 0x28;
383 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
384 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
385 break;
386 }
387
388 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
389 iscsi_aio_read16_cb,
390 NULL,
391 acb) != 0) {
392 scsi_free_scsi_task(acb->task);
393 qemu_aio_release(acb);
394 return NULL;
395 }
396
397 for (i = 0; i < acb->qiov->niov; i++) {
398 scsi_task_add_data_in_buffer(acb->task,
399 acb->qiov->iov[i].iov_len,
400 acb->qiov->iov[i].iov_base);
401 }
402
403 iscsi_set_events(iscsilun);
404
405 return &acb->common;
406 }
407
408
409 static void
410 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
411 void *command_data, void *opaque)
412 {
413 IscsiAIOCB *acb = opaque;
414
415 if (acb->canceled != 0) {
416 return;
417 }
418
419 acb->status = 0;
420 if (status < 0) {
421 error_report("Failed to sync10 data on iSCSI lun. %s",
422 iscsi_get_error(iscsi));
423 acb->status = -EIO;
424 }
425
426 iscsi_schedule_bh(acb);
427 }
428
429 static BlockDriverAIOCB *
430 iscsi_aio_flush(BlockDriverState *bs,
431 BlockDriverCompletionFunc *cb, void *opaque)
432 {
433 IscsiLun *iscsilun = bs->opaque;
434 struct iscsi_context *iscsi = iscsilun->iscsi;
435 IscsiAIOCB *acb;
436
437 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
438
439 acb->iscsilun = iscsilun;
440 acb->canceled = 0;
441 acb->bh = NULL;
442 acb->status = -EINPROGRESS;
443
444 acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
445 0, 0, 0, 0,
446 iscsi_synccache10_cb,
447 acb);
448 if (acb->task == NULL) {
449 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
450 iscsi_get_error(iscsi));
451 qemu_aio_release(acb);
452 return NULL;
453 }
454
455 iscsi_set_events(iscsilun);
456
457 return &acb->common;
458 }
459
460 static void
461 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
462 void *command_data, void *opaque)
463 {
464 IscsiAIOCB *acb = opaque;
465
466 if (acb->canceled != 0) {
467 return;
468 }
469
470 acb->status = 0;
471 if (status < 0) {
472 error_report("Failed to unmap data on iSCSI lun. %s",
473 iscsi_get_error(iscsi));
474 acb->status = -EIO;
475 }
476
477 iscsi_schedule_bh(acb);
478 }
479
480 static BlockDriverAIOCB *
481 iscsi_aio_discard(BlockDriverState *bs,
482 int64_t sector_num, int nb_sectors,
483 BlockDriverCompletionFunc *cb, void *opaque)
484 {
485 IscsiLun *iscsilun = bs->opaque;
486 struct iscsi_context *iscsi = iscsilun->iscsi;
487 IscsiAIOCB *acb;
488 struct unmap_list list[1];
489
490 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
491
492 acb->iscsilun = iscsilun;
493 acb->canceled = 0;
494 acb->bh = NULL;
495 acb->status = -EINPROGRESS;
496
497 list[0].lba = sector_qemu2lun(sector_num, iscsilun);
498 list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
499
500 acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
501 0, 0, &list[0], 1,
502 iscsi_unmap_cb,
503 acb);
504 if (acb->task == NULL) {
505 error_report("iSCSI: Failed to send unmap command. %s",
506 iscsi_get_error(iscsi));
507 qemu_aio_release(acb);
508 return NULL;
509 }
510
511 iscsi_set_events(iscsilun);
512
513 return &acb->common;
514 }
515
516 #ifdef __linux__
517 static void
518 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
519 void *command_data, void *opaque)
520 {
521 IscsiAIOCB *acb = opaque;
522
523 if (acb->canceled != 0) {
524 return;
525 }
526
527 acb->status = 0;
528 if (status < 0) {
529 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
530 iscsi_get_error(iscsi));
531 acb->status = -EIO;
532 }
533
534 acb->ioh->driver_status = 0;
535 acb->ioh->host_status = 0;
536 acb->ioh->resid = 0;
537
538 #define SG_ERR_DRIVER_SENSE 0x08
539
540 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
541 int ss;
542
543 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
544
545 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
546 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
547 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
548 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
549 }
550
551 iscsi_schedule_bh(acb);
552 }
553
554 static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
555 unsigned long int req, void *buf,
556 BlockDriverCompletionFunc *cb, void *opaque)
557 {
558 IscsiLun *iscsilun = bs->opaque;
559 struct iscsi_context *iscsi = iscsilun->iscsi;
560 struct iscsi_data data;
561 IscsiAIOCB *acb;
562
563 assert(req == SG_IO);
564
565 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
566
567 acb->iscsilun = iscsilun;
568 acb->canceled = 0;
569 acb->bh = NULL;
570 acb->status = -EINPROGRESS;
571 acb->buf = NULL;
572 acb->ioh = buf;
573
574 acb->task = malloc(sizeof(struct scsi_task));
575 if (acb->task == NULL) {
576 error_report("iSCSI: Failed to allocate task for scsi command. %s",
577 iscsi_get_error(iscsi));
578 qemu_aio_release(acb);
579 return NULL;
580 }
581 memset(acb->task, 0, sizeof(struct scsi_task));
582
583 switch (acb->ioh->dxfer_direction) {
584 case SG_DXFER_TO_DEV:
585 acb->task->xfer_dir = SCSI_XFER_WRITE;
586 break;
587 case SG_DXFER_FROM_DEV:
588 acb->task->xfer_dir = SCSI_XFER_READ;
589 break;
590 default:
591 acb->task->xfer_dir = SCSI_XFER_NONE;
592 break;
593 }
594
595 acb->task->cdb_size = acb->ioh->cmd_len;
596 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
597 acb->task->expxferlen = acb->ioh->dxfer_len;
598
599 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
600 data.data = acb->ioh->dxferp;
601 data.size = acb->ioh->dxfer_len;
602 }
603 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
604 iscsi_aio_ioctl_cb,
605 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
606 &data : NULL,
607 acb) != 0) {
608 scsi_free_scsi_task(acb->task);
609 qemu_aio_release(acb);
610 return NULL;
611 }
612
613 /* tell libiscsi to read straight into the buffer we got from ioctl */
614 if (acb->task->xfer_dir == SCSI_XFER_READ) {
615 scsi_task_add_data_in_buffer(acb->task,
616 acb->ioh->dxfer_len,
617 acb->ioh->dxferp);
618 }
619
620 iscsi_set_events(iscsilun);
621
622 return &acb->common;
623 }
624
625
626 static void ioctl_cb(void *opaque, int status)
627 {
628 int *p_status = opaque;
629 *p_status = status;
630 }
631
632 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
633 {
634 IscsiLun *iscsilun = bs->opaque;
635 int status;
636
637 switch (req) {
638 case SG_GET_VERSION_NUM:
639 *(int *)buf = 30000;
640 break;
641 case SG_GET_SCSI_ID:
642 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
643 break;
644 case SG_IO:
645 status = -EINPROGRESS;
646 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
647
648 while (status == -EINPROGRESS) {
649 qemu_aio_wait();
650 }
651
652 return 0;
653 default:
654 return -1;
655 }
656 return 0;
657 }
658 #endif
659
660 static int64_t
661 iscsi_getlength(BlockDriverState *bs)
662 {
663 IscsiLun *iscsilun = bs->opaque;
664 int64_t len;
665
666 len = iscsilun->num_blocks;
667 len *= iscsilun->block_size;
668
669 return len;
670 }
671
672 static int parse_chap(struct iscsi_context *iscsi, const char *target)
673 {
674 QemuOptsList *list;
675 QemuOpts *opts;
676 const char *user = NULL;
677 const char *password = NULL;
678
679 list = qemu_find_opts("iscsi");
680 if (!list) {
681 return 0;
682 }
683
684 opts = qemu_opts_find(list, target);
685 if (opts == NULL) {
686 opts = QTAILQ_FIRST(&list->head);
687 if (!opts) {
688 return 0;
689 }
690 }
691
692 user = qemu_opt_get(opts, "user");
693 if (!user) {
694 return 0;
695 }
696
697 password = qemu_opt_get(opts, "password");
698 if (!password) {
699 error_report("CHAP username specified but no password was given");
700 return -1;
701 }
702
703 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
704 error_report("Failed to set initiator username and password");
705 return -1;
706 }
707
708 return 0;
709 }
710
711 static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
712 {
713 QemuOptsList *list;
714 QemuOpts *opts;
715 const char *digest = NULL;
716
717 list = qemu_find_opts("iscsi");
718 if (!list) {
719 return;
720 }
721
722 opts = qemu_opts_find(list, target);
723 if (opts == NULL) {
724 opts = QTAILQ_FIRST(&list->head);
725 if (!opts) {
726 return;
727 }
728 }
729
730 digest = qemu_opt_get(opts, "header-digest");
731 if (!digest) {
732 return;
733 }
734
735 if (!strcmp(digest, "CRC32C")) {
736 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
737 } else if (!strcmp(digest, "NONE")) {
738 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
739 } else if (!strcmp(digest, "CRC32C-NONE")) {
740 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
741 } else if (!strcmp(digest, "NONE-CRC32C")) {
742 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
743 } else {
744 error_report("Invalid header-digest setting : %s", digest);
745 }
746 }
747
748 static char *parse_initiator_name(const char *target)
749 {
750 QemuOptsList *list;
751 QemuOpts *opts;
752 const char *name = NULL;
753 const char *iscsi_name = qemu_get_vm_name();
754
755 list = qemu_find_opts("iscsi");
756 if (list) {
757 opts = qemu_opts_find(list, target);
758 if (!opts) {
759 opts = QTAILQ_FIRST(&list->head);
760 }
761 if (opts) {
762 name = qemu_opt_get(opts, "initiator-name");
763 }
764 }
765
766 if (name) {
767 return g_strdup(name);
768 } else {
769 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
770 iscsi_name ? ":" : "",
771 iscsi_name ? iscsi_name : "");
772 }
773 }
774
775 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
776 static void iscsi_nop_timed_event(void *opaque)
777 {
778 IscsiLun *iscsilun = opaque;
779
780 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
781 error_report("iSCSI: NOP timeout. Reconnecting...");
782 iscsi_reconnect(iscsilun->iscsi);
783 }
784
785 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
786 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
787 return;
788 }
789
790 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
791 iscsi_set_events(iscsilun);
792 }
793 #endif
794
795 /*
796 * We support iscsi url's on the form
797 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
798 */
799 static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
800 {
801 IscsiLun *iscsilun = bs->opaque;
802 struct iscsi_context *iscsi = NULL;
803 struct iscsi_url *iscsi_url = NULL;
804 struct scsi_task *task = NULL;
805 struct scsi_inquiry_standard *inq = NULL;
806 struct scsi_readcapacity10 *rc10 = NULL;
807 struct scsi_readcapacity16 *rc16 = NULL;
808 char *initiator_name = NULL;
809 int ret;
810
811 if ((BDRV_SECTOR_SIZE % 512) != 0) {
812 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
813 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
814 "of 512", BDRV_SECTOR_SIZE);
815 return -EINVAL;
816 }
817
818 iscsi_url = iscsi_parse_full_url(iscsi, filename);
819 if (iscsi_url == NULL) {
820 error_report("Failed to parse URL : %s", filename);
821 ret = -EINVAL;
822 goto out;
823 }
824
825 memset(iscsilun, 0, sizeof(IscsiLun));
826
827 initiator_name = parse_initiator_name(iscsi_url->target);
828
829 iscsi = iscsi_create_context(initiator_name);
830 if (iscsi == NULL) {
831 error_report("iSCSI: Failed to create iSCSI context.");
832 ret = -ENOMEM;
833 goto out;
834 }
835
836 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
837 error_report("iSCSI: Failed to set target name.");
838 ret = -EINVAL;
839 goto out;
840 }
841
842 if (iscsi_url->user != NULL) {
843 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
844 iscsi_url->passwd);
845 if (ret != 0) {
846 error_report("Failed to set initiator username and password");
847 ret = -EINVAL;
848 goto out;
849 }
850 }
851
852 /* check if we got CHAP username/password via the options */
853 if (parse_chap(iscsi, iscsi_url->target) != 0) {
854 error_report("iSCSI: Failed to set CHAP user/password");
855 ret = -EINVAL;
856 goto out;
857 }
858
859 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
860 error_report("iSCSI: Failed to set session type to normal.");
861 ret = -EINVAL;
862 goto out;
863 }
864
865 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
866
867 /* check if we got HEADER_DIGEST via the options */
868 parse_header_digest(iscsi, iscsi_url->target);
869
870 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
871 error_report("iSCSI: Failed to connect to LUN : %s",
872 iscsi_get_error(iscsi));
873 ret = -EINVAL;
874 goto out;
875 }
876
877 iscsilun->iscsi = iscsi;
878 iscsilun->lun = iscsi_url->lun;
879
880 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
881
882 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
883 error_report("iSCSI: failed to send inquiry command.");
884 ret = -EINVAL;
885 goto out;
886 }
887
888 inq = scsi_datain_unmarshall(task);
889 if (inq == NULL) {
890 error_report("iSCSI: Failed to unmarshall inquiry data.");
891 ret = -EINVAL;
892 goto out;
893 }
894
895 iscsilun->type = inq->periperal_device_type;
896
897 scsi_free_scsi_task(task);
898
899 switch (iscsilun->type) {
900 case TYPE_DISK:
901 task = iscsi_readcapacity16_sync(iscsi, iscsilun->lun);
902 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
903 error_report("iSCSI: failed to send readcapacity16 command.");
904 ret = -EINVAL;
905 goto out;
906 }
907 rc16 = scsi_datain_unmarshall(task);
908 if (rc16 == NULL) {
909 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
910 ret = -EINVAL;
911 goto out;
912 }
913 iscsilun->block_size = rc16->block_length;
914 iscsilun->num_blocks = rc16->returned_lba + 1;
915 break;
916 case TYPE_ROM:
917 task = iscsi_readcapacity10_sync(iscsi, iscsilun->lun, 0, 0);
918 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
919 error_report("iSCSI: failed to send readcapacity10 command.");
920 ret = -EINVAL;
921 goto out;
922 }
923 rc10 = scsi_datain_unmarshall(task);
924 if (rc10 == NULL) {
925 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
926 ret = -EINVAL;
927 goto out;
928 }
929 iscsilun->block_size = rc10->block_size;
930 if (rc10->lba == 0) {
931 /* blank disk loaded */
932 iscsilun->num_blocks = 0;
933 } else {
934 iscsilun->num_blocks = rc10->lba + 1;
935 }
936 break;
937 default:
938 break;
939 }
940
941 bs->total_sectors = iscsilun->num_blocks *
942 iscsilun->block_size / BDRV_SECTOR_SIZE ;
943
944 /* Medium changer or tape. We dont have any emulation for this so this must
945 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
946 * to read from the device to guess the image format.
947 */
948 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
949 iscsilun->type == TYPE_TAPE) {
950 bs->sg = 1;
951 }
952
953 ret = 0;
954
955 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
956 /* Set up a timer for sending out iSCSI NOPs */
957 iscsilun->nop_timer = qemu_new_timer_ms(rt_clock, iscsi_nop_timed_event, iscsilun);
958 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
959 #endif
960
961 out:
962 if (initiator_name != NULL) {
963 g_free(initiator_name);
964 }
965 if (iscsi_url != NULL) {
966 iscsi_destroy_url(iscsi_url);
967 }
968 if (task != NULL) {
969 scsi_free_scsi_task(task);
970 }
971
972 if (ret) {
973 if (iscsi != NULL) {
974 iscsi_destroy_context(iscsi);
975 }
976 memset(iscsilun, 0, sizeof(IscsiLun));
977 }
978 return ret;
979 }
980
981 static void iscsi_close(BlockDriverState *bs)
982 {
983 IscsiLun *iscsilun = bs->opaque;
984 struct iscsi_context *iscsi = iscsilun->iscsi;
985
986 if (iscsilun->nop_timer) {
987 qemu_del_timer(iscsilun->nop_timer);
988 qemu_free_timer(iscsilun->nop_timer);
989 }
990 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
991 iscsi_destroy_context(iscsi);
992 memset(iscsilun, 0, sizeof(IscsiLun));
993 }
994
995 static int iscsi_has_zero_init(BlockDriverState *bs)
996 {
997 return 0;
998 }
999
1000 static int iscsi_create(const char *filename, QEMUOptionParameter *options)
1001 {
1002 int ret = 0;
1003 int64_t total_size = 0;
1004 BlockDriverState bs;
1005 IscsiLun *iscsilun = NULL;
1006
1007 memset(&bs, 0, sizeof(BlockDriverState));
1008
1009 /* Read out options */
1010 while (options && options->name) {
1011 if (!strcmp(options->name, "size")) {
1012 total_size = options->value.n / BDRV_SECTOR_SIZE;
1013 }
1014 options++;
1015 }
1016
1017 bs.opaque = g_malloc0(sizeof(struct IscsiLun));
1018 iscsilun = bs.opaque;
1019
1020 ret = iscsi_open(&bs, filename, 0);
1021 if (ret != 0) {
1022 goto out;
1023 }
1024 if (iscsilun->nop_timer) {
1025 qemu_del_timer(iscsilun->nop_timer);
1026 qemu_free_timer(iscsilun->nop_timer);
1027 }
1028 if (iscsilun->type != TYPE_DISK) {
1029 ret = -ENODEV;
1030 goto out;
1031 }
1032 if (bs.total_sectors < total_size) {
1033 ret = -ENOSPC;
1034 }
1035
1036 ret = 0;
1037 out:
1038 if (iscsilun->iscsi != NULL) {
1039 iscsi_destroy_context(iscsilun->iscsi);
1040 }
1041 g_free(bs.opaque);
1042 return ret;
1043 }
1044
1045 static QEMUOptionParameter iscsi_create_options[] = {
1046 {
1047 .name = BLOCK_OPT_SIZE,
1048 .type = OPT_SIZE,
1049 .help = "Virtual disk size"
1050 },
1051 { NULL }
1052 };
1053
1054 static BlockDriver bdrv_iscsi = {
1055 .format_name = "iscsi",
1056 .protocol_name = "iscsi",
1057
1058 .instance_size = sizeof(IscsiLun),
1059 .bdrv_file_open = iscsi_open,
1060 .bdrv_close = iscsi_close,
1061 .bdrv_create = iscsi_create,
1062 .create_options = iscsi_create_options,
1063
1064 .bdrv_getlength = iscsi_getlength,
1065
1066 .bdrv_aio_readv = iscsi_aio_readv,
1067 .bdrv_aio_writev = iscsi_aio_writev,
1068 .bdrv_aio_flush = iscsi_aio_flush,
1069
1070 .bdrv_aio_discard = iscsi_aio_discard,
1071 .bdrv_has_zero_init = iscsi_has_zero_init,
1072
1073 #ifdef __linux__
1074 .bdrv_ioctl = iscsi_ioctl,
1075 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1076 #endif
1077 };
1078
1079 static void iscsi_block_init(void)
1080 {
1081 bdrv_register(&bdrv_iscsi);
1082 }
1083
1084 block_init(iscsi_block_init);