2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "config-host.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"
34 #include "block/scsi.h"
36 #include <iscsi/iscsi.h>
37 #include <iscsi/scsi-lowlevel.h>
41 #include <block/scsi.h>
44 typedef struct IscsiLun
{
45 struct iscsi_context
*iscsi
;
47 enum scsi_inquiry_peripheral_device_type type
;
54 typedef struct IscsiAIOCB
{
55 BlockDriverAIOCB common
;
59 struct scsi_task
*task
;
73 #define NOP_INTERVAL 5000
74 #define MAX_NOP_FAILURES 3
75 #define ISCSI_CMD_RETRIES 5
82 qemu_bh_delete(acb
->bh
);
87 if (acb
->canceled
== 0) {
88 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
91 if (acb
->task
!= NULL
) {
92 scsi_free_scsi_task(acb
->task
);
96 qemu_aio_release(acb
);
100 iscsi_schedule_bh(IscsiAIOCB
*acb
)
105 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
106 qemu_bh_schedule(acb
->bh
);
111 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
114 IscsiAIOCB
*acb
= private_data
;
116 acb
->status
= -ECANCELED
;
117 iscsi_schedule_bh(acb
);
121 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
123 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
124 IscsiLun
*iscsilun
= acb
->iscsilun
;
126 if (acb
->status
!= -EINPROGRESS
) {
132 /* send a task mgmt call to the target to cancel the task on the target */
133 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
134 iscsi_abort_task_cb
, acb
);
136 while (acb
->status
== -EINPROGRESS
) {
141 static const AIOCBInfo iscsi_aiocb_info
= {
142 .aiocb_size
= sizeof(IscsiAIOCB
),
143 .cancel
= iscsi_aio_cancel
,
147 static void iscsi_process_read(void *arg
);
148 static void iscsi_process_write(void *arg
);
150 static int iscsi_process_flush(void *arg
)
152 IscsiLun
*iscsilun
= arg
;
154 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
158 iscsi_set_events(IscsiLun
*iscsilun
)
160 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
163 /* We always register a read handler. */
165 ev
|= iscsi_which_events(iscsi
);
166 if (ev
!= iscsilun
->events
) {
167 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
169 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
175 iscsilun
->events
= ev
;
179 iscsi_process_read(void *arg
)
181 IscsiLun
*iscsilun
= arg
;
182 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
184 iscsi_service(iscsi
, POLLIN
);
185 iscsi_set_events(iscsilun
);
189 iscsi_process_write(void *arg
)
191 IscsiLun
*iscsilun
= arg
;
192 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
194 iscsi_service(iscsi
, POLLOUT
);
195 iscsi_set_events(iscsilun
);
199 iscsi_aio_writev_acb(IscsiAIOCB
*acb
);
202 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
203 void *command_data
, void *opaque
)
205 IscsiAIOCB
*acb
= opaque
;
207 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
212 if (acb
->canceled
!= 0) {
218 if (status
== SCSI_STATUS_CHECK_CONDITION
219 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
220 && acb
->retries
-- > 0) {
221 scsi_free_scsi_task(acb
->task
);
223 if (iscsi_aio_writev_acb(acb
) == 0) {
224 iscsi_set_events(acb
->iscsilun
);
228 error_report("Failed to write16 data to iSCSI lun. %s",
229 iscsi_get_error(iscsi
));
233 iscsi_schedule_bh(acb
);
236 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
238 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
242 iscsi_aio_writev_acb(IscsiAIOCB
*acb
)
244 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
246 uint32_t num_sectors
;
248 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
249 struct iscsi_data data
;
255 acb
->status
= -EINPROGRESS
;
258 /* this will allow us to get rid of 'buf' completely */
259 size
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
261 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
262 data
.size
= MIN(size
, acb
->qiov
->size
);
264 /* if the iovec only contains one buffer we can pass it directly */
265 if (acb
->qiov
->niov
== 1) {
266 data
.data
= acb
->qiov
->iov
[0].iov_base
;
268 acb
->buf
= g_malloc(data
.size
);
269 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, data
.size
);
270 data
.data
= acb
->buf
;
274 acb
->task
= malloc(sizeof(struct scsi_task
));
275 if (acb
->task
== NULL
) {
276 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
277 "command. %s", iscsi_get_error(iscsi
));
280 memset(acb
->task
, 0, sizeof(struct scsi_task
));
282 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
283 acb
->task
->cdb_size
= 16;
284 acb
->task
->cdb
[0] = 0x8a;
285 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
286 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
287 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
288 num_sectors
= size
/ acb
->iscsilun
->block_size
;
289 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
290 acb
->task
->expxferlen
= size
;
292 #if defined(LIBISCSI_FEATURE_IOVECTOR)
293 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
294 iscsi_aio_write16_cb
,
298 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
299 iscsi_aio_write16_cb
,
304 scsi_free_scsi_task(acb
->task
);
309 #if defined(LIBISCSI_FEATURE_IOVECTOR)
310 scsi_task_set_iov_out(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
316 static BlockDriverAIOCB
*
317 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
318 QEMUIOVector
*qiov
, int nb_sectors
,
319 BlockDriverCompletionFunc
*cb
,
322 IscsiLun
*iscsilun
= bs
->opaque
;
325 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
326 trace_iscsi_aio_writev(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
328 acb
->iscsilun
= iscsilun
;
330 acb
->nb_sectors
= nb_sectors
;
331 acb
->sector_num
= sector_num
;
332 acb
->retries
= ISCSI_CMD_RETRIES
;
334 if (iscsi_aio_writev_acb(acb
) != 0) {
335 qemu_aio_release(acb
);
339 iscsi_set_events(iscsilun
);
344 iscsi_aio_readv_acb(IscsiAIOCB
*acb
);
347 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
348 void *command_data
, void *opaque
)
350 IscsiAIOCB
*acb
= opaque
;
352 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
354 if (acb
->canceled
!= 0) {
360 if (status
== SCSI_STATUS_CHECK_CONDITION
361 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
362 && acb
->retries
-- > 0) {
363 scsi_free_scsi_task(acb
->task
);
365 if (iscsi_aio_readv_acb(acb
) == 0) {
366 iscsi_set_events(acb
->iscsilun
);
370 error_report("Failed to read16 data from iSCSI lun. %s",
371 iscsi_get_error(iscsi
));
375 iscsi_schedule_bh(acb
);
379 iscsi_aio_readv_acb(IscsiAIOCB
*acb
)
381 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
383 uint32_t num_sectors
;
385 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
391 acb
->status
= -EINPROGRESS
;
394 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
395 * may be misaligned to the LUN, so we may need to read some extra
398 acb
->read_offset
= 0;
399 if (acb
->iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
400 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* acb
->sector_num
;
402 acb
->read_offset
= bdrv_offset
% acb
->iscsilun
->block_size
;
405 num_sectors
= (acb
->read_size
+ acb
->iscsilun
->block_size
406 + acb
->read_offset
- 1)
407 / acb
->iscsilun
->block_size
;
409 acb
->task
= malloc(sizeof(struct scsi_task
));
410 if (acb
->task
== NULL
) {
411 error_report("iSCSI: Failed to allocate task for scsi READ16 "
412 "command. %s", iscsi_get_error(iscsi
));
415 memset(acb
->task
, 0, sizeof(struct scsi_task
));
417 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
418 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
419 acb
->task
->expxferlen
= acb
->read_size
;
421 switch (acb
->iscsilun
->type
) {
423 acb
->task
->cdb_size
= 16;
424 acb
->task
->cdb
[0] = 0x88;
425 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
426 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
427 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
430 acb
->task
->cdb_size
= 10;
431 acb
->task
->cdb
[0] = 0x28;
432 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
433 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
437 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
442 scsi_free_scsi_task(acb
->task
);
446 #if defined(LIBISCSI_FEATURE_IOVECTOR)
447 scsi_task_set_iov_in(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
449 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
450 scsi_task_add_data_in_buffer(acb
->task
,
451 acb
->qiov
->iov
[i
].iov_len
,
452 acb
->qiov
->iov
[i
].iov_base
);
458 static BlockDriverAIOCB
*
459 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
460 QEMUIOVector
*qiov
, int nb_sectors
,
461 BlockDriverCompletionFunc
*cb
,
464 IscsiLun
*iscsilun
= bs
->opaque
;
467 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
468 trace_iscsi_aio_readv(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
470 acb
->nb_sectors
= nb_sectors
;
471 acb
->sector_num
= sector_num
;
472 acb
->iscsilun
= iscsilun
;
474 acb
->read_size
= BDRV_SECTOR_SIZE
* (size_t)acb
->nb_sectors
;
475 acb
->retries
= ISCSI_CMD_RETRIES
;
477 if (iscsi_aio_readv_acb(acb
) != 0) {
478 qemu_aio_release(acb
);
482 iscsi_set_events(iscsilun
);
487 iscsi_aio_flush_acb(IscsiAIOCB
*acb
);
490 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
491 void *command_data
, void *opaque
)
493 IscsiAIOCB
*acb
= opaque
;
495 if (acb
->canceled
!= 0) {
501 if (status
== SCSI_STATUS_CHECK_CONDITION
502 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
503 && acb
->retries
-- > 0) {
504 scsi_free_scsi_task(acb
->task
);
506 if (iscsi_aio_flush_acb(acb
) == 0) {
507 iscsi_set_events(acb
->iscsilun
);
511 error_report("Failed to sync10 data on iSCSI lun. %s",
512 iscsi_get_error(iscsi
));
516 iscsi_schedule_bh(acb
);
520 iscsi_aio_flush_acb(IscsiAIOCB
*acb
)
522 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
526 acb
->status
= -EINPROGRESS
;
529 acb
->task
= iscsi_synchronizecache10_task(iscsi
, acb
->iscsilun
->lun
,
531 iscsi_synccache10_cb
,
533 if (acb
->task
== NULL
) {
534 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
535 iscsi_get_error(iscsi
));
542 static BlockDriverAIOCB
*
543 iscsi_aio_flush(BlockDriverState
*bs
,
544 BlockDriverCompletionFunc
*cb
, void *opaque
)
546 IscsiLun
*iscsilun
= bs
->opaque
;
550 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
552 acb
->iscsilun
= iscsilun
;
553 acb
->retries
= ISCSI_CMD_RETRIES
;
555 if (iscsi_aio_flush_acb(acb
) != 0) {
556 qemu_aio_release(acb
);
560 iscsi_set_events(iscsilun
);
565 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
);
568 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
569 void *command_data
, void *opaque
)
571 IscsiAIOCB
*acb
= opaque
;
573 if (acb
->canceled
!= 0) {
579 if (status
== SCSI_STATUS_CHECK_CONDITION
580 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
581 && acb
->retries
-- > 0) {
582 scsi_free_scsi_task(acb
->task
);
584 if (iscsi_aio_discard_acb(acb
) == 0) {
585 iscsi_set_events(acb
->iscsilun
);
589 error_report("Failed to unmap data on iSCSI lun. %s",
590 iscsi_get_error(iscsi
));
594 iscsi_schedule_bh(acb
);
597 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
) {
598 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
599 struct unmap_list list
[1];
603 acb
->status
= -EINPROGRESS
;
606 list
[0].lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
607 list
[0].num
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
/ acb
->iscsilun
->block_size
;
609 acb
->task
= iscsi_unmap_task(iscsi
, acb
->iscsilun
->lun
,
613 if (acb
->task
== NULL
) {
614 error_report("iSCSI: Failed to send unmap command. %s",
615 iscsi_get_error(iscsi
));
622 static BlockDriverAIOCB
*
623 iscsi_aio_discard(BlockDriverState
*bs
,
624 int64_t sector_num
, int nb_sectors
,
625 BlockDriverCompletionFunc
*cb
, void *opaque
)
627 IscsiLun
*iscsilun
= bs
->opaque
;
630 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
632 acb
->iscsilun
= iscsilun
;
633 acb
->nb_sectors
= nb_sectors
;
634 acb
->sector_num
= sector_num
;
635 acb
->retries
= ISCSI_CMD_RETRIES
;
637 if (iscsi_aio_discard_acb(acb
) != 0) {
638 qemu_aio_release(acb
);
642 iscsi_set_events(iscsilun
);
649 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
650 void *command_data
, void *opaque
)
652 IscsiAIOCB
*acb
= opaque
;
654 if (acb
->canceled
!= 0) {
660 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
661 iscsi_get_error(iscsi
));
665 acb
->ioh
->driver_status
= 0;
666 acb
->ioh
->host_status
= 0;
669 #define SG_ERR_DRIVER_SENSE 0x08
671 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
674 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
676 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
677 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
678 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
679 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
682 iscsi_schedule_bh(acb
);
685 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
686 unsigned long int req
, void *buf
,
687 BlockDriverCompletionFunc
*cb
, void *opaque
)
689 IscsiLun
*iscsilun
= bs
->opaque
;
690 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
691 struct iscsi_data data
;
694 assert(req
== SG_IO
);
696 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
698 acb
->iscsilun
= iscsilun
;
701 acb
->status
= -EINPROGRESS
;
705 acb
->task
= malloc(sizeof(struct scsi_task
));
706 if (acb
->task
== NULL
) {
707 error_report("iSCSI: Failed to allocate task for scsi command. %s",
708 iscsi_get_error(iscsi
));
709 qemu_aio_release(acb
);
712 memset(acb
->task
, 0, sizeof(struct scsi_task
));
714 switch (acb
->ioh
->dxfer_direction
) {
715 case SG_DXFER_TO_DEV
:
716 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
718 case SG_DXFER_FROM_DEV
:
719 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
722 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
726 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
727 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
728 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
730 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
731 data
.data
= acb
->ioh
->dxferp
;
732 data
.size
= acb
->ioh
->dxfer_len
;
734 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
736 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
739 scsi_free_scsi_task(acb
->task
);
740 qemu_aio_release(acb
);
744 /* tell libiscsi to read straight into the buffer we got from ioctl */
745 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
746 scsi_task_add_data_in_buffer(acb
->task
,
751 iscsi_set_events(iscsilun
);
757 static void ioctl_cb(void *opaque
, int status
)
759 int *p_status
= opaque
;
763 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
765 IscsiLun
*iscsilun
= bs
->opaque
;
769 case SG_GET_VERSION_NUM
:
773 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
776 status
= -EINPROGRESS
;
777 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
779 while (status
== -EINPROGRESS
) {
792 iscsi_getlength(BlockDriverState
*bs
)
794 IscsiLun
*iscsilun
= bs
->opaque
;
797 len
= iscsilun
->num_blocks
;
798 len
*= iscsilun
->block_size
;
803 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
807 const char *user
= NULL
;
808 const char *password
= NULL
;
810 list
= qemu_find_opts("iscsi");
815 opts
= qemu_opts_find(list
, target
);
817 opts
= QTAILQ_FIRST(&list
->head
);
823 user
= qemu_opt_get(opts
, "user");
828 password
= qemu_opt_get(opts
, "password");
830 error_report("CHAP username specified but no password was given");
834 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
835 error_report("Failed to set initiator username and password");
842 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
846 const char *digest
= NULL
;
848 list
= qemu_find_opts("iscsi");
853 opts
= qemu_opts_find(list
, target
);
855 opts
= QTAILQ_FIRST(&list
->head
);
861 digest
= qemu_opt_get(opts
, "header-digest");
866 if (!strcmp(digest
, "CRC32C")) {
867 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
868 } else if (!strcmp(digest
, "NONE")) {
869 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
870 } else if (!strcmp(digest
, "CRC32C-NONE")) {
871 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
872 } else if (!strcmp(digest
, "NONE-CRC32C")) {
873 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
875 error_report("Invalid header-digest setting : %s", digest
);
879 static char *parse_initiator_name(const char *target
)
883 const char *name
= NULL
;
884 const char *iscsi_name
= qemu_get_vm_name();
886 list
= qemu_find_opts("iscsi");
888 opts
= qemu_opts_find(list
, target
);
890 opts
= QTAILQ_FIRST(&list
->head
);
893 name
= qemu_opt_get(opts
, "initiator-name");
898 return g_strdup(name
);
900 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
901 iscsi_name
? ":" : "",
902 iscsi_name
? iscsi_name
: "");
906 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
907 static void iscsi_nop_timed_event(void *opaque
)
909 IscsiLun
*iscsilun
= opaque
;
911 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
912 error_report("iSCSI: NOP timeout. Reconnecting...");
913 iscsi_reconnect(iscsilun
->iscsi
);
916 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
917 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
921 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
922 iscsi_set_events(iscsilun
);
926 static int iscsi_readcapacity_sync(IscsiLun
*iscsilun
)
928 struct scsi_task
*task
= NULL
;
929 struct scsi_readcapacity10
*rc10
= NULL
;
930 struct scsi_readcapacity16
*rc16
= NULL
;
932 int retries
= ISCSI_CMD_RETRIES
;
936 scsi_free_scsi_task(task
);
940 switch (iscsilun
->type
) {
942 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
943 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
944 rc16
= scsi_datain_unmarshall(task
);
946 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
949 iscsilun
->block_size
= rc16
->block_length
;
950 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
955 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
956 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
957 rc10
= scsi_datain_unmarshall(task
);
959 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
962 iscsilun
->block_size
= rc10
->block_size
;
963 if (rc10
->lba
== 0) {
964 /* blank disk loaded */
965 iscsilun
->num_blocks
= 0;
967 iscsilun
->num_blocks
= rc10
->lba
+ 1;
975 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
976 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
979 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
980 error_report("iSCSI: failed to send readcapacity10 command.");
984 scsi_free_scsi_task(task
);
989 /* TODO Convert to fine grained options */
990 static QemuOptsList runtime_opts
= {
992 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
996 .type
= QEMU_OPT_STRING
,
997 .help
= "URL to the iscsi image",
999 { /* end of list */ }
1004 * We support iscsi url's on the form
1005 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1007 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1009 IscsiLun
*iscsilun
= bs
->opaque
;
1010 struct iscsi_context
*iscsi
= NULL
;
1011 struct iscsi_url
*iscsi_url
= NULL
;
1012 struct scsi_task
*task
= NULL
;
1013 struct scsi_inquiry_standard
*inq
= NULL
;
1014 char *initiator_name
= NULL
;
1016 Error
*local_err
= NULL
;
1017 const char *filename
;
1020 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
1021 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1022 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1023 "of 512", BDRV_SECTOR_SIZE
);
1027 opts
= qemu_opts_create_nofail(&runtime_opts
);
1028 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1029 if (error_is_set(&local_err
)) {
1030 qerror_report_err(local_err
);
1031 error_free(local_err
);
1036 filename
= qemu_opt_get(opts
, "filename");
1039 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1040 if (iscsi_url
== NULL
) {
1041 error_report("Failed to parse URL : %s", filename
);
1046 memset(iscsilun
, 0, sizeof(IscsiLun
));
1048 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1050 iscsi
= iscsi_create_context(initiator_name
);
1051 if (iscsi
== NULL
) {
1052 error_report("iSCSI: Failed to create iSCSI context.");
1057 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1058 error_report("iSCSI: Failed to set target name.");
1063 if (iscsi_url
->user
!= NULL
) {
1064 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1067 error_report("Failed to set initiator username and password");
1073 /* check if we got CHAP username/password via the options */
1074 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
1075 error_report("iSCSI: Failed to set CHAP user/password");
1080 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1081 error_report("iSCSI: Failed to set session type to normal.");
1086 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1088 /* check if we got HEADER_DIGEST via the options */
1089 parse_header_digest(iscsi
, iscsi_url
->target
);
1091 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1092 error_report("iSCSI: Failed to connect to LUN : %s",
1093 iscsi_get_error(iscsi
));
1098 iscsilun
->iscsi
= iscsi
;
1099 iscsilun
->lun
= iscsi_url
->lun
;
1101 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
1103 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1104 error_report("iSCSI: failed to send inquiry command.");
1109 inq
= scsi_datain_unmarshall(task
);
1111 error_report("iSCSI: Failed to unmarshall inquiry data.");
1116 iscsilun
->type
= inq
->periperal_device_type
;
1118 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1121 bs
->total_sectors
= iscsilun
->num_blocks
*
1122 iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
1124 /* Medium changer or tape. We dont have any emulation for this so this must
1125 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1126 * to read from the device to guess the image format.
1128 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1129 iscsilun
->type
== TYPE_TAPE
) {
1133 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1134 /* Set up a timer for sending out iSCSI NOPs */
1135 iscsilun
->nop_timer
= qemu_new_timer_ms(rt_clock
, iscsi_nop_timed_event
, iscsilun
);
1136 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
1140 qemu_opts_del(opts
);
1141 if (initiator_name
!= NULL
) {
1142 g_free(initiator_name
);
1144 if (iscsi_url
!= NULL
) {
1145 iscsi_destroy_url(iscsi_url
);
1148 scsi_free_scsi_task(task
);
1152 if (iscsi
!= NULL
) {
1153 iscsi_destroy_context(iscsi
);
1155 memset(iscsilun
, 0, sizeof(IscsiLun
));
1160 static void iscsi_close(BlockDriverState
*bs
)
1162 IscsiLun
*iscsilun
= bs
->opaque
;
1163 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1165 if (iscsilun
->nop_timer
) {
1166 qemu_del_timer(iscsilun
->nop_timer
);
1167 qemu_free_timer(iscsilun
->nop_timer
);
1169 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1170 iscsi_destroy_context(iscsi
);
1171 memset(iscsilun
, 0, sizeof(IscsiLun
));
1174 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1176 IscsiLun
*iscsilun
= bs
->opaque
;
1179 if (iscsilun
->type
!= TYPE_DISK
) {
1183 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1187 if (offset
> iscsi_getlength(bs
)) {
1194 static int iscsi_has_zero_init(BlockDriverState
*bs
)
1199 static int iscsi_create(const char *filename
, QEMUOptionParameter
*options
)
1202 int64_t total_size
= 0;
1203 BlockDriverState bs
;
1204 IscsiLun
*iscsilun
= NULL
;
1207 memset(&bs
, 0, sizeof(BlockDriverState
));
1209 /* Read out options */
1210 while (options
&& options
->name
) {
1211 if (!strcmp(options
->name
, "size")) {
1212 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1217 bs
.opaque
= g_malloc0(sizeof(struct IscsiLun
));
1218 iscsilun
= bs
.opaque
;
1220 bs_options
= qdict_new();
1221 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1222 ret
= iscsi_open(&bs
, bs_options
, 0);
1223 QDECREF(bs_options
);
1228 if (iscsilun
->nop_timer
) {
1229 qemu_del_timer(iscsilun
->nop_timer
);
1230 qemu_free_timer(iscsilun
->nop_timer
);
1232 if (iscsilun
->type
!= TYPE_DISK
) {
1236 if (bs
.total_sectors
< total_size
) {
1242 if (iscsilun
->iscsi
!= NULL
) {
1243 iscsi_destroy_context(iscsilun
->iscsi
);
1249 static QEMUOptionParameter iscsi_create_options
[] = {
1251 .name
= BLOCK_OPT_SIZE
,
1253 .help
= "Virtual disk size"
1258 static BlockDriver bdrv_iscsi
= {
1259 .format_name
= "iscsi",
1260 .protocol_name
= "iscsi",
1262 .instance_size
= sizeof(IscsiLun
),
1263 .bdrv_file_open
= iscsi_open
,
1264 .bdrv_close
= iscsi_close
,
1265 .bdrv_create
= iscsi_create
,
1266 .create_options
= iscsi_create_options
,
1268 .bdrv_getlength
= iscsi_getlength
,
1269 .bdrv_truncate
= iscsi_truncate
,
1271 .bdrv_aio_readv
= iscsi_aio_readv
,
1272 .bdrv_aio_writev
= iscsi_aio_writev
,
1273 .bdrv_aio_flush
= iscsi_aio_flush
,
1275 .bdrv_aio_discard
= iscsi_aio_discard
,
1276 .bdrv_has_zero_init
= iscsi_has_zero_init
,
1279 .bdrv_ioctl
= iscsi_ioctl
,
1280 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1284 static QemuOptsList qemu_iscsi_opts
= {
1286 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1290 .type
= QEMU_OPT_STRING
,
1291 .help
= "username for CHAP authentication to target",
1294 .type
= QEMU_OPT_STRING
,
1295 .help
= "password for CHAP authentication to target",
1297 .name
= "header-digest",
1298 .type
= QEMU_OPT_STRING
,
1299 .help
= "HeaderDigest setting. "
1300 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1302 .name
= "initiator-name",
1303 .type
= QEMU_OPT_STRING
,
1304 .help
= "Initiator iqn name to use when connecting",
1306 { /* end of list */ }
1310 static void iscsi_block_init(void)
1312 bdrv_register(&bdrv_iscsi
);
1313 qemu_add_opts(&qemu_iscsi_opts
);
1316 block_init(iscsi_block_init
);