2 * QEMU Block driver for iSCSI images
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 * Copyright (c) 2012-2013 Peter Lieven <pl@kamp.de>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "config-host.h"
29 #include <arpa/inet.h>
30 #include "qemu-common.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "block/block_int.h"
35 #include "block/scsi.h"
37 #include "sysemu/sysemu.h"
38 #include "qmp-commands.h"
40 #include <iscsi/iscsi.h>
41 #include <iscsi/scsi-lowlevel.h>
45 #include <block/scsi.h>
48 typedef struct IscsiLun
{
49 struct iscsi_context
*iscsi
;
51 enum scsi_inquiry_peripheral_device_type type
;
58 uint8_t has_write_same
;
59 struct scsi_inquiry_logical_block_provisioning lbp
;
60 struct scsi_inquiry_block_limits bl
;
61 unsigned char *zeroblock
;
64 typedef struct IscsiTask
{
69 struct scsi_task
*task
;
73 typedef struct IscsiAIOCB
{
74 BlockDriverAIOCB common
;
78 struct scsi_task
*task
;
90 #define NOP_INTERVAL 5000
91 #define MAX_NOP_FAILURES 3
92 #define ISCSI_CMD_RETRIES 5
99 qemu_bh_delete(acb
->bh
);
104 if (acb
->canceled
== 0) {
105 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
108 if (acb
->task
!= NULL
) {
109 scsi_free_scsi_task(acb
->task
);
113 qemu_aio_release(acb
);
117 iscsi_schedule_bh(IscsiAIOCB
*acb
)
122 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
123 qemu_bh_schedule(acb
->bh
);
127 iscsi_co_generic_cb(struct iscsi_context
*iscsi
, int status
,
128 void *command_data
, void *opaque
)
130 struct IscsiTask
*iTask
= opaque
;
131 struct scsi_task
*task
= command_data
;
134 iTask
->status
= status
;
138 if (iTask
->retries
-- > 0 && status
== SCSI_STATUS_CHECK_CONDITION
139 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
) {
144 if (status
!= SCSI_STATUS_GOOD
) {
145 error_report("iSCSI: Failure. %s", iscsi_get_error(iscsi
));
150 qemu_coroutine_enter(iTask
->co
, NULL
);
154 static void iscsi_co_init_iscsitask(IscsiLun
*iscsilun
, struct IscsiTask
*iTask
)
156 *iTask
= (struct IscsiTask
) {
157 .co
= qemu_coroutine_self(),
158 .retries
= ISCSI_CMD_RETRIES
,
163 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
166 IscsiAIOCB
*acb
= private_data
;
168 acb
->status
= -ECANCELED
;
169 iscsi_schedule_bh(acb
);
173 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
175 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
176 IscsiLun
*iscsilun
= acb
->iscsilun
;
178 if (acb
->status
!= -EINPROGRESS
) {
184 /* send a task mgmt call to the target to cancel the task on the target */
185 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
186 iscsi_abort_task_cb
, acb
);
188 while (acb
->status
== -EINPROGRESS
) {
193 static const AIOCBInfo iscsi_aiocb_info
= {
194 .aiocb_size
= sizeof(IscsiAIOCB
),
195 .cancel
= iscsi_aio_cancel
,
199 static void iscsi_process_read(void *arg
);
200 static void iscsi_process_write(void *arg
);
203 iscsi_set_events(IscsiLun
*iscsilun
)
205 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
208 /* We always register a read handler. */
210 ev
|= iscsi_which_events(iscsi
);
211 if (ev
!= iscsilun
->events
) {
212 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
214 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
219 iscsilun
->events
= ev
;
223 iscsi_process_read(void *arg
)
225 IscsiLun
*iscsilun
= arg
;
226 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
228 iscsi_service(iscsi
, POLLIN
);
229 iscsi_set_events(iscsilun
);
233 iscsi_process_write(void *arg
)
235 IscsiLun
*iscsilun
= arg
;
236 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
238 iscsi_service(iscsi
, POLLOUT
);
239 iscsi_set_events(iscsilun
);
242 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
244 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
247 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
249 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
252 static bool is_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
255 if ((sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
||
256 (nb_sectors
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
) {
257 error_report("iSCSI misaligned request: "
258 "iscsilun->block_size %u, sector_num %" PRIi64
260 iscsilun
->block_size
, sector_num
, nb_sectors
);
266 static int coroutine_fn
iscsi_co_writev(BlockDriverState
*bs
,
267 int64_t sector_num
, int nb_sectors
,
270 IscsiLun
*iscsilun
= bs
->opaque
;
271 struct IscsiTask iTask
;
273 uint32_t num_sectors
;
274 uint8_t *data
= NULL
;
277 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
281 lba
= sector_qemu2lun(sector_num
, iscsilun
);
282 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
283 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
284 /* if the iovec only contains one buffer we can pass it directly */
285 if (iov
->niov
== 1) {
286 data
= iov
->iov
[0].iov_base
;
288 size_t size
= MIN(nb_sectors
* BDRV_SECTOR_SIZE
, iov
->size
);
289 buf
= g_malloc(size
);
290 qemu_iovec_to_buf(iov
, 0, buf
, size
);
294 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
296 iTask
.task
= iscsi_write16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
297 data
, num_sectors
* iscsilun
->block_size
,
298 iscsilun
->block_size
, 0, 0, 0, 0, 0,
299 iscsi_co_generic_cb
, &iTask
);
300 if (iTask
.task
== NULL
) {
304 #if defined(LIBISCSI_FEATURE_IOVECTOR)
305 scsi_task_set_iov_out(iTask
.task
, (struct scsi_iovec
*) iov
->iov
,
308 while (!iTask
.complete
) {
309 iscsi_set_events(iscsilun
);
310 qemu_coroutine_yield();
313 if (iTask
.task
!= NULL
) {
314 scsi_free_scsi_task(iTask
.task
);
318 if (iTask
.do_retry
) {
324 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
331 static int coroutine_fn
iscsi_co_readv(BlockDriverState
*bs
,
332 int64_t sector_num
, int nb_sectors
,
335 IscsiLun
*iscsilun
= bs
->opaque
;
336 struct IscsiTask iTask
;
338 uint32_t num_sectors
;
339 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
343 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
347 lba
= sector_qemu2lun(sector_num
, iscsilun
);
348 num_sectors
= sector_qemu2lun(nb_sectors
, iscsilun
);
350 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
352 switch (iscsilun
->type
) {
354 iTask
.task
= iscsi_read16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
355 num_sectors
* iscsilun
->block_size
,
356 iscsilun
->block_size
, 0, 0, 0, 0, 0,
357 iscsi_co_generic_cb
, &iTask
);
360 iTask
.task
= iscsi_read10_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
361 num_sectors
* iscsilun
->block_size
,
362 iscsilun
->block_size
, 0, 0, 0, 0, 0,
363 iscsi_co_generic_cb
, &iTask
);
366 if (iTask
.task
== NULL
) {
369 #if defined(LIBISCSI_FEATURE_IOVECTOR)
370 scsi_task_set_iov_in(iTask
.task
, (struct scsi_iovec
*) iov
->iov
, iov
->niov
);
372 for (i
= 0; i
< iov
->niov
; i
++) {
373 scsi_task_add_data_in_buffer(iTask
.task
,
375 iov
->iov
[i
].iov_base
);
379 while (!iTask
.complete
) {
380 iscsi_set_events(iscsilun
);
381 qemu_coroutine_yield();
384 if (iTask
.task
!= NULL
) {
385 scsi_free_scsi_task(iTask
.task
);
389 if (iTask
.do_retry
) {
393 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
400 static int coroutine_fn
iscsi_co_flush(BlockDriverState
*bs
)
402 IscsiLun
*iscsilun
= bs
->opaque
;
403 struct IscsiTask iTask
;
405 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
408 if (iscsi_synchronizecache10_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, 0,
409 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
413 while (!iTask
.complete
) {
414 iscsi_set_events(iscsilun
);
415 qemu_coroutine_yield();
418 if (iTask
.task
!= NULL
) {
419 scsi_free_scsi_task(iTask
.task
);
423 if (iTask
.do_retry
) {
427 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
436 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
437 void *command_data
, void *opaque
)
439 IscsiAIOCB
*acb
= opaque
;
444 if (acb
->canceled
!= 0) {
450 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
451 iscsi_get_error(iscsi
));
455 acb
->ioh
->driver_status
= 0;
456 acb
->ioh
->host_status
= 0;
459 #define SG_ERR_DRIVER_SENSE 0x08
461 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
464 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
466 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
467 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
468 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
469 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
472 iscsi_schedule_bh(acb
);
475 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
476 unsigned long int req
, void *buf
,
477 BlockDriverCompletionFunc
*cb
, void *opaque
)
479 IscsiLun
*iscsilun
= bs
->opaque
;
480 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
481 struct iscsi_data data
;
484 assert(req
== SG_IO
);
486 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
488 acb
->iscsilun
= iscsilun
;
491 acb
->status
= -EINPROGRESS
;
495 acb
->task
= malloc(sizeof(struct scsi_task
));
496 if (acb
->task
== NULL
) {
497 error_report("iSCSI: Failed to allocate task for scsi command. %s",
498 iscsi_get_error(iscsi
));
499 qemu_aio_release(acb
);
502 memset(acb
->task
, 0, sizeof(struct scsi_task
));
504 switch (acb
->ioh
->dxfer_direction
) {
505 case SG_DXFER_TO_DEV
:
506 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
508 case SG_DXFER_FROM_DEV
:
509 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
512 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
516 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
517 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
518 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
521 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
522 if (acb
->ioh
->iovec_count
== 0) {
523 data
.data
= acb
->ioh
->dxferp
;
524 data
.size
= acb
->ioh
->dxfer_len
;
526 #if defined(LIBISCSI_FEATURE_IOVECTOR)
527 scsi_task_set_iov_out(acb
->task
,
528 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
529 acb
->ioh
->iovec_count
);
531 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
533 acb
->buf
= g_malloc(acb
->ioh
->dxfer_len
);
534 data
.data
= acb
->buf
;
535 data
.size
= iov_to_buf(iov
, acb
->ioh
->iovec_count
, 0,
536 acb
->buf
, acb
->ioh
->dxfer_len
);
541 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
543 (data
.size
> 0) ? &data
: NULL
,
545 scsi_free_scsi_task(acb
->task
);
546 qemu_aio_release(acb
);
550 /* tell libiscsi to read straight into the buffer we got from ioctl */
551 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
552 if (acb
->ioh
->iovec_count
== 0) {
553 scsi_task_add_data_in_buffer(acb
->task
,
557 #if defined(LIBISCSI_FEATURE_IOVECTOR)
558 scsi_task_set_iov_in(acb
->task
,
559 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
560 acb
->ioh
->iovec_count
);
563 for (i
= 0; i
< acb
->ioh
->iovec_count
; i
++) {
564 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
566 scsi_task_add_data_in_buffer(acb
->task
,
574 iscsi_set_events(iscsilun
);
580 static void ioctl_cb(void *opaque
, int status
)
582 int *p_status
= opaque
;
586 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
588 IscsiLun
*iscsilun
= bs
->opaque
;
592 case SG_GET_VERSION_NUM
:
596 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
599 status
= -EINPROGRESS
;
600 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
602 while (status
== -EINPROGRESS
) {
615 iscsi_getlength(BlockDriverState
*bs
)
617 IscsiLun
*iscsilun
= bs
->opaque
;
620 len
= iscsilun
->num_blocks
;
621 len
*= iscsilun
->block_size
;
626 #if defined(LIBISCSI_FEATURE_IOVECTOR)
628 static int64_t coroutine_fn
iscsi_co_get_block_status(BlockDriverState
*bs
,
630 int nb_sectors
, int *pnum
)
632 IscsiLun
*iscsilun
= bs
->opaque
;
633 struct scsi_get_lba_status
*lbas
= NULL
;
634 struct scsi_lba_status_descriptor
*lbasd
= NULL
;
635 struct IscsiTask iTask
;
638 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
640 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
645 /* default to all sectors allocated */
646 ret
= BDRV_BLOCK_DATA
;
647 ret
|= (sector_num
<< BDRV_SECTOR_BITS
) | BDRV_BLOCK_OFFSET_VALID
;
650 /* LUN does not support logical block provisioning */
651 if (iscsilun
->lbpme
== 0) {
656 if (iscsi_get_lba_status_task(iscsilun
->iscsi
, iscsilun
->lun
,
657 sector_qemu2lun(sector_num
, iscsilun
),
658 8 + 16, iscsi_co_generic_cb
,
664 while (!iTask
.complete
) {
665 iscsi_set_events(iscsilun
);
666 qemu_coroutine_yield();
669 if (iTask
.do_retry
) {
670 if (iTask
.task
!= NULL
) {
671 scsi_free_scsi_task(iTask
.task
);
677 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
678 /* in case the get_lba_status_callout fails (i.e.
679 * because the device is busy or the cmd is not
680 * supported) we pretend all blocks are allocated
681 * for backwards compatibility */
685 lbas
= scsi_datain_unmarshall(iTask
.task
);
691 lbasd
= &lbas
->descriptors
[0];
693 if (sector_qemu2lun(sector_num
, iscsilun
) != lbasd
->lba
) {
698 *pnum
= sector_lun2qemu(lbasd
->num_blocks
, iscsilun
);
699 if (*pnum
> nb_sectors
) {
703 if (lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_DEALLOCATED
||
704 lbasd
->provisioning
== SCSI_PROVISIONING_TYPE_ANCHORED
) {
705 ret
&= ~BDRV_BLOCK_DATA
;
706 if (iscsilun
->lbprz
) {
707 ret
|= BDRV_BLOCK_ZERO
;
712 if (iTask
.task
!= NULL
) {
713 scsi_free_scsi_task(iTask
.task
);
718 #endif /* LIBISCSI_FEATURE_IOVECTOR */
721 coroutine_fn
iscsi_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
724 IscsiLun
*iscsilun
= bs
->opaque
;
725 struct IscsiTask iTask
;
726 struct unmap_list list
;
728 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
732 if (!iscsilun
->lbp
.lbpu
) {
733 /* UNMAP is not supported by the target */
737 list
.lba
= sector_qemu2lun(sector_num
, iscsilun
);
738 list
.num
= sector_qemu2lun(nb_sectors
, iscsilun
);
740 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
742 if (iscsi_unmap_task(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0, &list
, 1,
743 iscsi_co_generic_cb
, &iTask
) == NULL
) {
747 while (!iTask
.complete
) {
748 iscsi_set_events(iscsilun
);
749 qemu_coroutine_yield();
752 if (iTask
.task
!= NULL
) {
753 scsi_free_scsi_task(iTask
.task
);
757 if (iTask
.do_retry
) {
761 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
) {
762 /* the target might fail with a check condition if it
763 is not happy with the alignment of the UNMAP request
764 we silently fail in this case */
768 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
775 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
778 coroutine_fn
iscsi_co_write_zeroes(BlockDriverState
*bs
, int64_t sector_num
,
779 int nb_sectors
, BdrvRequestFlags flags
)
781 IscsiLun
*iscsilun
= bs
->opaque
;
782 struct IscsiTask iTask
;
786 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
790 if (!(flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->has_write_same
) {
791 /* WRITE SAME without UNMAP is not supported by the target */
795 if ((flags
& BDRV_REQ_MAY_UNMAP
) && !iscsilun
->lbp
.lbpws
) {
796 /* WRITE SAME with UNMAP is not supported by the target */
800 lba
= sector_qemu2lun(sector_num
, iscsilun
);
801 nb_blocks
= sector_qemu2lun(nb_sectors
, iscsilun
);
803 if (iscsilun
->zeroblock
== NULL
) {
804 iscsilun
->zeroblock
= g_malloc0(iscsilun
->block_size
);
807 iscsi_co_init_iscsitask(iscsilun
, &iTask
);
809 if (iscsi_writesame16_task(iscsilun
->iscsi
, iscsilun
->lun
, lba
,
810 iscsilun
->zeroblock
, iscsilun
->block_size
,
811 nb_blocks
, 0, !!(flags
& BDRV_REQ_MAY_UNMAP
),
812 0, 0, iscsi_co_generic_cb
, &iTask
) == NULL
) {
816 while (!iTask
.complete
) {
817 iscsi_set_events(iscsilun
);
818 qemu_coroutine_yield();
821 if (iTask
.task
!= NULL
) {
822 scsi_free_scsi_task(iTask
.task
);
826 if (iTask
.do_retry
) {
830 if (iTask
.status
!= SCSI_STATUS_GOOD
) {
831 if (iTask
.status
== SCSI_STATUS_CHECK_CONDITION
&&
832 iTask
.task
->sense
.key
== SCSI_SENSE_ILLEGAL_REQUEST
&&
833 iTask
.task
->sense
.ascq
== SCSI_SENSE_ASCQ_INVALID_OPERATION_CODE
) {
834 /* WRITE SAME is not supported by the target */
835 iscsilun
->has_write_same
= false;
845 #endif /* SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED */
847 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
851 const char *user
= NULL
;
852 const char *password
= NULL
;
854 list
= qemu_find_opts("iscsi");
859 opts
= qemu_opts_find(list
, target
);
861 opts
= QTAILQ_FIRST(&list
->head
);
867 user
= qemu_opt_get(opts
, "user");
872 password
= qemu_opt_get(opts
, "password");
874 error_report("CHAP username specified but no password was given");
878 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
879 error_report("Failed to set initiator username and password");
886 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
890 const char *digest
= NULL
;
892 list
= qemu_find_opts("iscsi");
897 opts
= qemu_opts_find(list
, target
);
899 opts
= QTAILQ_FIRST(&list
->head
);
905 digest
= qemu_opt_get(opts
, "header-digest");
910 if (!strcmp(digest
, "CRC32C")) {
911 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
912 } else if (!strcmp(digest
, "NONE")) {
913 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
914 } else if (!strcmp(digest
, "CRC32C-NONE")) {
915 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
916 } else if (!strcmp(digest
, "NONE-CRC32C")) {
917 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
919 error_report("Invalid header-digest setting : %s", digest
);
923 static char *parse_initiator_name(const char *target
)
931 list
= qemu_find_opts("iscsi");
933 opts
= qemu_opts_find(list
, target
);
935 opts
= QTAILQ_FIRST(&list
->head
);
938 name
= qemu_opt_get(opts
, "initiator-name");
940 return g_strdup(name
);
945 uuid_info
= qmp_query_uuid(NULL
);
946 if (strcmp(uuid_info
->UUID
, UUID_NONE
) == 0) {
947 name
= qemu_get_vm_name();
949 name
= uuid_info
->UUID
;
951 iscsi_name
= g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
952 name
? ":" : "", name
? name
: "");
953 qapi_free_UuidInfo(uuid_info
);
957 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
958 static void iscsi_nop_timed_event(void *opaque
)
960 IscsiLun
*iscsilun
= opaque
;
962 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
963 error_report("iSCSI: NOP timeout. Reconnecting...");
964 iscsi_reconnect(iscsilun
->iscsi
);
967 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
968 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
972 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
973 iscsi_set_events(iscsilun
);
977 static int iscsi_readcapacity_sync(IscsiLun
*iscsilun
)
979 struct scsi_task
*task
= NULL
;
980 struct scsi_readcapacity10
*rc10
= NULL
;
981 struct scsi_readcapacity16
*rc16
= NULL
;
983 int retries
= ISCSI_CMD_RETRIES
;
987 scsi_free_scsi_task(task
);
991 switch (iscsilun
->type
) {
993 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
994 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
995 rc16
= scsi_datain_unmarshall(task
);
997 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
1000 iscsilun
->block_size
= rc16
->block_length
;
1001 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
1002 iscsilun
->lbpme
= rc16
->lbpme
;
1003 iscsilun
->lbprz
= rc16
->lbprz
;
1008 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
1009 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
1010 rc10
= scsi_datain_unmarshall(task
);
1012 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1015 iscsilun
->block_size
= rc10
->block_size
;
1016 if (rc10
->lba
== 0) {
1017 /* blank disk loaded */
1018 iscsilun
->num_blocks
= 0;
1020 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1028 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1029 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1032 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1033 error_report("iSCSI: failed to send readcapacity10 command.");
1037 scsi_free_scsi_task(task
);
1042 /* TODO Convert to fine grained options */
1043 static QemuOptsList runtime_opts
= {
1045 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1049 .type
= QEMU_OPT_STRING
,
1050 .help
= "URL to the iscsi image",
1052 { /* end of list */ }
1056 static struct scsi_task
*iscsi_do_inquiry(struct iscsi_context
*iscsi
,
1057 int lun
, int evpd
, int pc
) {
1059 struct scsi_task
*task
= NULL
;
1060 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, 64);
1061 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1064 full_size
= scsi_datain_getfullsize(task
);
1065 if (full_size
> task
->datain
.size
) {
1066 scsi_free_scsi_task(task
);
1068 /* we need more data for the full list */
1069 task
= iscsi_inquiry_sync(iscsi
, lun
, evpd
, pc
, full_size
);
1070 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1078 error_report("iSCSI: Inquiry command failed : %s",
1079 iscsi_get_error(iscsi
));
1081 scsi_free_scsi_task(task
);
1088 * We support iscsi url's on the form
1089 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1091 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1094 IscsiLun
*iscsilun
= bs
->opaque
;
1095 struct iscsi_context
*iscsi
= NULL
;
1096 struct iscsi_url
*iscsi_url
= NULL
;
1097 struct scsi_task
*task
= NULL
;
1098 struct scsi_inquiry_standard
*inq
= NULL
;
1099 char *initiator_name
= NULL
;
1101 Error
*local_err
= NULL
;
1102 const char *filename
;
1105 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
1106 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1107 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1108 "of 512", BDRV_SECTOR_SIZE
);
1112 opts
= qemu_opts_create_nofail(&runtime_opts
);
1113 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1114 if (error_is_set(&local_err
)) {
1115 qerror_report_err(local_err
);
1116 error_free(local_err
);
1121 filename
= qemu_opt_get(opts
, "filename");
1124 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1125 if (iscsi_url
== NULL
) {
1126 error_report("Failed to parse URL : %s", filename
);
1131 memset(iscsilun
, 0, sizeof(IscsiLun
));
1133 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1135 iscsi
= iscsi_create_context(initiator_name
);
1136 if (iscsi
== NULL
) {
1137 error_report("iSCSI: Failed to create iSCSI context.");
1142 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1143 error_report("iSCSI: Failed to set target name.");
1148 if (iscsi_url
->user
!= NULL
) {
1149 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1152 error_report("Failed to set initiator username and password");
1158 /* check if we got CHAP username/password via the options */
1159 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
1160 error_report("iSCSI: Failed to set CHAP user/password");
1165 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1166 error_report("iSCSI: Failed to set session type to normal.");
1171 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1173 /* check if we got HEADER_DIGEST via the options */
1174 parse_header_digest(iscsi
, iscsi_url
->target
);
1176 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1177 error_report("iSCSI: Failed to connect to LUN : %s",
1178 iscsi_get_error(iscsi
));
1183 iscsilun
->iscsi
= iscsi
;
1184 iscsilun
->lun
= iscsi_url
->lun
;
1186 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
1188 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1189 error_report("iSCSI: failed to send inquiry command.");
1194 inq
= scsi_datain_unmarshall(task
);
1196 error_report("iSCSI: Failed to unmarshall inquiry data.");
1201 iscsilun
->type
= inq
->periperal_device_type
;
1202 iscsilun
->has_write_same
= true;
1204 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1207 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1209 /* Medium changer or tape. We dont have any emulation for this so this must
1210 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1211 * to read from the device to guess the image format.
1213 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1214 iscsilun
->type
== TYPE_TAPE
) {
1218 if (iscsilun
->lbpme
) {
1219 struct scsi_inquiry_logical_block_provisioning
*inq_lbp
;
1220 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1221 SCSI_INQUIRY_PAGECODE_LOGICAL_BLOCK_PROVISIONING
);
1226 inq_lbp
= scsi_datain_unmarshall(task
);
1227 if (inq_lbp
== NULL
) {
1228 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1232 memcpy(&iscsilun
->lbp
, inq_lbp
,
1233 sizeof(struct scsi_inquiry_logical_block_provisioning
));
1234 scsi_free_scsi_task(task
);
1238 if (iscsilun
->lbp
.lbpu
|| iscsilun
->lbp
.lbpws
) {
1239 struct scsi_inquiry_block_limits
*inq_bl
;
1240 task
= iscsi_do_inquiry(iscsilun
->iscsi
, iscsilun
->lun
, 1,
1241 SCSI_INQUIRY_PAGECODE_BLOCK_LIMITS
);
1246 inq_bl
= scsi_datain_unmarshall(task
);
1247 if (inq_bl
== NULL
) {
1248 error_report("iSCSI: failed to unmarshall inquiry datain blob");
1252 memcpy(&iscsilun
->bl
, inq_bl
,
1253 sizeof(struct scsi_inquiry_block_limits
));
1254 scsi_free_scsi_task(task
);
1257 if (iscsilun
->bl
.max_unmap
< 0xffffffff) {
1258 bs
->bl
.max_discard
= sector_lun2qemu(iscsilun
->bl
.max_unmap
,
1261 bs
->bl
.discard_alignment
= sector_lun2qemu(iscsilun
->bl
.opt_unmap_gran
,
1264 if (iscsilun
->bl
.max_ws_len
< 0xffffffff) {
1265 bs
->bl
.max_write_zeroes
= sector_lun2qemu(iscsilun
->bl
.max_ws_len
,
1268 bs
->bl
.write_zeroes_alignment
= sector_lun2qemu(iscsilun
->bl
.opt_unmap_gran
,
1271 bs
->bl
.opt_transfer_length
= sector_lun2qemu(iscsilun
->bl
.opt_xfer_len
,
1275 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1276 /* Set up a timer for sending out iSCSI NOPs */
1277 iscsilun
->nop_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, iscsi_nop_timed_event
, iscsilun
);
1278 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1282 qemu_opts_del(opts
);
1283 if (initiator_name
!= NULL
) {
1284 g_free(initiator_name
);
1286 if (iscsi_url
!= NULL
) {
1287 iscsi_destroy_url(iscsi_url
);
1290 scsi_free_scsi_task(task
);
1294 if (iscsi
!= NULL
) {
1295 iscsi_destroy_context(iscsi
);
1297 memset(iscsilun
, 0, sizeof(IscsiLun
));
1302 static void iscsi_close(BlockDriverState
*bs
)
1304 IscsiLun
*iscsilun
= bs
->opaque
;
1305 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1307 if (iscsilun
->nop_timer
) {
1308 timer_del(iscsilun
->nop_timer
);
1309 timer_free(iscsilun
->nop_timer
);
1311 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
);
1312 iscsi_destroy_context(iscsi
);
1313 g_free(iscsilun
->zeroblock
);
1314 memset(iscsilun
, 0, sizeof(IscsiLun
));
1317 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1319 IscsiLun
*iscsilun
= bs
->opaque
;
1322 if (iscsilun
->type
!= TYPE_DISK
) {
1326 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1330 if (offset
> iscsi_getlength(bs
)) {
1337 static int iscsi_create(const char *filename
, QEMUOptionParameter
*options
,
1341 int64_t total_size
= 0;
1342 BlockDriverState
*bs
;
1343 IscsiLun
*iscsilun
= NULL
;
1348 /* Read out options */
1349 while (options
&& options
->name
) {
1350 if (!strcmp(options
->name
, "size")) {
1351 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1356 bs
->opaque
= g_malloc0(sizeof(struct IscsiLun
));
1357 iscsilun
= bs
->opaque
;
1359 bs_options
= qdict_new();
1360 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1361 ret
= iscsi_open(bs
, bs_options
, 0, NULL
);
1362 QDECREF(bs_options
);
1367 if (iscsilun
->nop_timer
) {
1368 timer_del(iscsilun
->nop_timer
);
1369 timer_free(iscsilun
->nop_timer
);
1371 if (iscsilun
->type
!= TYPE_DISK
) {
1375 if (bs
->total_sectors
< total_size
) {
1382 if (iscsilun
->iscsi
!= NULL
) {
1383 iscsi_destroy_context(iscsilun
->iscsi
);
1391 static int iscsi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1393 IscsiLun
*iscsilun
= bs
->opaque
;
1394 bdi
->unallocated_blocks_are_zero
= !!iscsilun
->lbprz
;
1395 bdi
->can_write_zeroes_with_unmap
= iscsilun
->lbprz
&& iscsilun
->lbp
.lbpws
;
1396 /* Guess the internal cluster (page) size of the iscsi target by the means
1397 * of opt_unmap_gran. Transfer the unmap granularity only if it has a
1398 * reasonable size for bdi->cluster_size */
1399 if (iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
>= 64 * 1024 &&
1400 iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
<= 16 * 1024 * 1024) {
1401 bdi
->cluster_size
= iscsilun
->bl
.opt_unmap_gran
* iscsilun
->block_size
;
1406 static QEMUOptionParameter iscsi_create_options
[] = {
1408 .name
= BLOCK_OPT_SIZE
,
1410 .help
= "Virtual disk size"
1415 static BlockDriver bdrv_iscsi
= {
1416 .format_name
= "iscsi",
1417 .protocol_name
= "iscsi",
1419 .instance_size
= sizeof(IscsiLun
),
1420 .bdrv_needs_filename
= true,
1421 .bdrv_file_open
= iscsi_open
,
1422 .bdrv_close
= iscsi_close
,
1423 .bdrv_create
= iscsi_create
,
1424 .create_options
= iscsi_create_options
,
1426 .bdrv_getlength
= iscsi_getlength
,
1427 .bdrv_get_info
= iscsi_get_info
,
1428 .bdrv_truncate
= iscsi_truncate
,
1430 #if defined(LIBISCSI_FEATURE_IOVECTOR)
1431 .bdrv_co_get_block_status
= iscsi_co_get_block_status
,
1433 .bdrv_co_discard
= iscsi_co_discard
,
1434 #if defined(SCSI_SENSE_ASCQ_CAPACITY_DATA_HAS_CHANGED)
1435 .bdrv_co_write_zeroes
= iscsi_co_write_zeroes
,
1437 .bdrv_co_readv
= iscsi_co_readv
,
1438 .bdrv_co_writev
= iscsi_co_writev
,
1439 .bdrv_co_flush_to_disk
= iscsi_co_flush
,
1442 .bdrv_ioctl
= iscsi_ioctl
,
1443 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1447 static QemuOptsList qemu_iscsi_opts
= {
1449 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1453 .type
= QEMU_OPT_STRING
,
1454 .help
= "username for CHAP authentication to target",
1457 .type
= QEMU_OPT_STRING
,
1458 .help
= "password for CHAP authentication to target",
1460 .name
= "header-digest",
1461 .type
= QEMU_OPT_STRING
,
1462 .help
= "HeaderDigest setting. "
1463 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1465 .name
= "initiator-name",
1466 .type
= QEMU_OPT_STRING
,
1467 .help
= "Initiator iqn name to use when connecting",
1469 { /* end of list */ }
1473 static void iscsi_block_init(void)
1475 bdrv_register(&bdrv_iscsi
);
1476 qemu_add_opts(&qemu_iscsi_opts
);
1479 block_init(iscsi_block_init
);