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 "hw/scsi-defs.h"
36 #include <iscsi/iscsi.h>
37 #include <iscsi/scsi-lowlevel.h>
41 #include <hw/scsi-defs.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
;
70 #define NOP_INTERVAL 5000
71 #define MAX_NOP_FAILURES 3
78 qemu_bh_delete(acb
->bh
);
83 if (acb
->canceled
== 0) {
84 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
87 if (acb
->task
!= NULL
) {
88 scsi_free_scsi_task(acb
->task
);
92 qemu_aio_release(acb
);
96 iscsi_schedule_bh(IscsiAIOCB
*acb
)
101 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
102 qemu_bh_schedule(acb
->bh
);
107 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
110 IscsiAIOCB
*acb
= private_data
;
112 acb
->status
= -ECANCELED
;
113 iscsi_schedule_bh(acb
);
117 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
119 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
120 IscsiLun
*iscsilun
= acb
->iscsilun
;
122 if (acb
->status
!= -EINPROGRESS
) {
128 /* send a task mgmt call to the target to cancel the task on the target */
129 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
130 iscsi_abort_task_cb
, acb
);
132 while (acb
->status
== -EINPROGRESS
) {
137 static const AIOCBInfo iscsi_aiocb_info
= {
138 .aiocb_size
= sizeof(IscsiAIOCB
),
139 .cancel
= iscsi_aio_cancel
,
143 static void iscsi_process_read(void *arg
);
144 static void iscsi_process_write(void *arg
);
146 static int iscsi_process_flush(void *arg
)
148 IscsiLun
*iscsilun
= arg
;
150 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
154 iscsi_set_events(IscsiLun
*iscsilun
)
156 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
159 /* We always register a read handler. */
161 ev
|= iscsi_which_events(iscsi
);
162 if (ev
!= iscsilun
->events
) {
163 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
165 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
171 iscsilun
->events
= ev
;
175 iscsi_process_read(void *arg
)
177 IscsiLun
*iscsilun
= arg
;
178 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
180 iscsi_service(iscsi
, POLLIN
);
181 iscsi_set_events(iscsilun
);
185 iscsi_process_write(void *arg
)
187 IscsiLun
*iscsilun
= arg
;
188 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
190 iscsi_service(iscsi
, POLLOUT
);
191 iscsi_set_events(iscsilun
);
196 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
197 void *command_data
, void *opaque
)
199 IscsiAIOCB
*acb
= opaque
;
201 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
206 if (acb
->canceled
!= 0) {
212 error_report("Failed to write16 data to iSCSI lun. %s",
213 iscsi_get_error(iscsi
));
217 iscsi_schedule_bh(acb
);
220 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
222 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
225 static BlockDriverAIOCB
*
226 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
227 QEMUIOVector
*qiov
, int nb_sectors
,
228 BlockDriverCompletionFunc
*cb
,
231 IscsiLun
*iscsilun
= bs
->opaque
;
232 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
235 uint32_t num_sectors
;
237 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
238 struct iscsi_data data
;
242 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
243 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
245 acb
->iscsilun
= iscsilun
;
250 acb
->status
= -EINPROGRESS
;
253 /* this will allow us to get rid of 'buf' completely */
254 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
256 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
257 data
.size
= MIN(size
, acb
->qiov
->size
);
259 /* if the iovec only contains one buffer we can pass it directly */
260 if (acb
->qiov
->niov
== 1) {
261 data
.data
= acb
->qiov
->iov
[0].iov_base
;
263 acb
->buf
= g_malloc(data
.size
);
264 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, data
.size
);
265 data
.data
= acb
->buf
;
269 acb
->task
= malloc(sizeof(struct scsi_task
));
270 if (acb
->task
== NULL
) {
271 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
272 "command. %s", iscsi_get_error(iscsi
));
273 qemu_aio_release(acb
);
276 memset(acb
->task
, 0, sizeof(struct scsi_task
));
278 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
279 acb
->task
->cdb_size
= 16;
280 acb
->task
->cdb
[0] = 0x8a;
281 lba
= sector_qemu2lun(sector_num
, iscsilun
);
282 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
283 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
284 num_sectors
= size
/ iscsilun
->block_size
;
285 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
286 acb
->task
->expxferlen
= size
;
288 #if defined(LIBISCSI_FEATURE_IOVECTOR)
289 ret
= iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
290 iscsi_aio_write16_cb
,
294 ret
= iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
295 iscsi_aio_write16_cb
,
300 scsi_free_scsi_task(acb
->task
);
302 qemu_aio_release(acb
);
306 #if defined(LIBISCSI_FEATURE_IOVECTOR)
307 scsi_task_set_iov_out(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
310 iscsi_set_events(iscsilun
);
316 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
317 void *command_data
, void *opaque
)
319 IscsiAIOCB
*acb
= opaque
;
321 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
323 if (acb
->canceled
!= 0) {
329 error_report("Failed to read16 data from iSCSI lun. %s",
330 iscsi_get_error(iscsi
));
334 iscsi_schedule_bh(acb
);
337 static BlockDriverAIOCB
*
338 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
339 QEMUIOVector
*qiov
, int nb_sectors
,
340 BlockDriverCompletionFunc
*cb
,
343 IscsiLun
*iscsilun
= bs
->opaque
;
344 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
346 size_t qemu_read_size
;
347 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
352 uint32_t num_sectors
;
354 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
356 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
357 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
359 acb
->iscsilun
= iscsilun
;
364 acb
->status
= -EINPROGRESS
;
365 acb
->read_size
= qemu_read_size
;
368 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
369 * may be misaligned to the LUN, so we may need to read some extra
372 acb
->read_offset
= 0;
373 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
374 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
376 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
379 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
380 + acb
->read_offset
- 1)
381 / iscsilun
->block_size
;
383 acb
->task
= malloc(sizeof(struct scsi_task
));
384 if (acb
->task
== NULL
) {
385 error_report("iSCSI: Failed to allocate task for scsi READ16 "
386 "command. %s", iscsi_get_error(iscsi
));
387 qemu_aio_release(acb
);
390 memset(acb
->task
, 0, sizeof(struct scsi_task
));
392 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
393 lba
= sector_qemu2lun(sector_num
, iscsilun
);
394 acb
->task
->expxferlen
= qemu_read_size
;
396 switch (iscsilun
->type
) {
398 acb
->task
->cdb_size
= 16;
399 acb
->task
->cdb
[0] = 0x88;
400 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
401 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
402 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
405 acb
->task
->cdb_size
= 10;
406 acb
->task
->cdb
[0] = 0x28;
407 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
408 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
412 ret
= iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
417 scsi_free_scsi_task(acb
->task
);
418 qemu_aio_release(acb
);
422 #if defined(LIBISCSI_FEATURE_IOVECTOR)
423 scsi_task_set_iov_in(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
425 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
426 scsi_task_add_data_in_buffer(acb
->task
,
427 acb
->qiov
->iov
[i
].iov_len
,
428 acb
->qiov
->iov
[i
].iov_base
);
432 iscsi_set_events(iscsilun
);
439 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
440 void *command_data
, void *opaque
)
442 IscsiAIOCB
*acb
= opaque
;
444 if (acb
->canceled
!= 0) {
450 error_report("Failed to sync10 data on iSCSI lun. %s",
451 iscsi_get_error(iscsi
));
455 iscsi_schedule_bh(acb
);
458 static BlockDriverAIOCB
*
459 iscsi_aio_flush(BlockDriverState
*bs
,
460 BlockDriverCompletionFunc
*cb
, void *opaque
)
462 IscsiLun
*iscsilun
= bs
->opaque
;
463 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
466 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
468 acb
->iscsilun
= iscsilun
;
471 acb
->status
= -EINPROGRESS
;
474 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
476 iscsi_synccache10_cb
,
478 if (acb
->task
== NULL
) {
479 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
480 iscsi_get_error(iscsi
));
481 qemu_aio_release(acb
);
485 iscsi_set_events(iscsilun
);
491 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
492 void *command_data
, void *opaque
)
494 IscsiAIOCB
*acb
= opaque
;
496 if (acb
->canceled
!= 0) {
502 error_report("Failed to unmap data on iSCSI lun. %s",
503 iscsi_get_error(iscsi
));
507 iscsi_schedule_bh(acb
);
510 static BlockDriverAIOCB
*
511 iscsi_aio_discard(BlockDriverState
*bs
,
512 int64_t sector_num
, int nb_sectors
,
513 BlockDriverCompletionFunc
*cb
, void *opaque
)
515 IscsiLun
*iscsilun
= bs
->opaque
;
516 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
518 struct unmap_list list
[1];
520 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
522 acb
->iscsilun
= iscsilun
;
525 acb
->status
= -EINPROGRESS
;
528 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
529 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
531 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
535 if (acb
->task
== NULL
) {
536 error_report("iSCSI: Failed to send unmap command. %s",
537 iscsi_get_error(iscsi
));
538 qemu_aio_release(acb
);
542 iscsi_set_events(iscsilun
);
549 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
550 void *command_data
, void *opaque
)
552 IscsiAIOCB
*acb
= opaque
;
554 if (acb
->canceled
!= 0) {
560 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
561 iscsi_get_error(iscsi
));
565 acb
->ioh
->driver_status
= 0;
566 acb
->ioh
->host_status
= 0;
569 #define SG_ERR_DRIVER_SENSE 0x08
571 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
574 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
576 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
577 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
578 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
579 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
582 iscsi_schedule_bh(acb
);
585 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
586 unsigned long int req
, void *buf
,
587 BlockDriverCompletionFunc
*cb
, void *opaque
)
589 IscsiLun
*iscsilun
= bs
->opaque
;
590 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
591 struct iscsi_data data
;
594 assert(req
== SG_IO
);
596 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
598 acb
->iscsilun
= iscsilun
;
601 acb
->status
= -EINPROGRESS
;
605 acb
->task
= malloc(sizeof(struct scsi_task
));
606 if (acb
->task
== NULL
) {
607 error_report("iSCSI: Failed to allocate task for scsi command. %s",
608 iscsi_get_error(iscsi
));
609 qemu_aio_release(acb
);
612 memset(acb
->task
, 0, sizeof(struct scsi_task
));
614 switch (acb
->ioh
->dxfer_direction
) {
615 case SG_DXFER_TO_DEV
:
616 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
618 case SG_DXFER_FROM_DEV
:
619 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
622 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
626 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
627 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
628 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
630 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
631 data
.data
= acb
->ioh
->dxferp
;
632 data
.size
= acb
->ioh
->dxfer_len
;
634 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
636 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
639 scsi_free_scsi_task(acb
->task
);
640 qemu_aio_release(acb
);
644 /* tell libiscsi to read straight into the buffer we got from ioctl */
645 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
646 scsi_task_add_data_in_buffer(acb
->task
,
651 iscsi_set_events(iscsilun
);
657 static void ioctl_cb(void *opaque
, int status
)
659 int *p_status
= opaque
;
663 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
665 IscsiLun
*iscsilun
= bs
->opaque
;
669 case SG_GET_VERSION_NUM
:
673 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
676 status
= -EINPROGRESS
;
677 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
679 while (status
== -EINPROGRESS
) {
692 iscsi_getlength(BlockDriverState
*bs
)
694 IscsiLun
*iscsilun
= bs
->opaque
;
697 len
= iscsilun
->num_blocks
;
698 len
*= iscsilun
->block_size
;
703 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
707 const char *user
= NULL
;
708 const char *password
= NULL
;
710 list
= qemu_find_opts("iscsi");
715 opts
= qemu_opts_find(list
, target
);
717 opts
= QTAILQ_FIRST(&list
->head
);
723 user
= qemu_opt_get(opts
, "user");
728 password
= qemu_opt_get(opts
, "password");
730 error_report("CHAP username specified but no password was given");
734 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
735 error_report("Failed to set initiator username and password");
742 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
746 const char *digest
= NULL
;
748 list
= qemu_find_opts("iscsi");
753 opts
= qemu_opts_find(list
, target
);
755 opts
= QTAILQ_FIRST(&list
->head
);
761 digest
= qemu_opt_get(opts
, "header-digest");
766 if (!strcmp(digest
, "CRC32C")) {
767 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
768 } else if (!strcmp(digest
, "NONE")) {
769 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
770 } else if (!strcmp(digest
, "CRC32C-NONE")) {
771 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
772 } else if (!strcmp(digest
, "NONE-CRC32C")) {
773 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
775 error_report("Invalid header-digest setting : %s", digest
);
779 static char *parse_initiator_name(const char *target
)
783 const char *name
= NULL
;
784 const char *iscsi_name
= qemu_get_vm_name();
786 list
= qemu_find_opts("iscsi");
788 opts
= qemu_opts_find(list
, target
);
790 opts
= QTAILQ_FIRST(&list
->head
);
793 name
= qemu_opt_get(opts
, "initiator-name");
798 return g_strdup(name
);
800 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
801 iscsi_name
? ":" : "",
802 iscsi_name
? iscsi_name
: "");
806 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
807 static void iscsi_nop_timed_event(void *opaque
)
809 IscsiLun
*iscsilun
= opaque
;
811 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
812 error_report("iSCSI: NOP timeout. Reconnecting...");
813 iscsi_reconnect(iscsilun
->iscsi
);
816 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
817 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
821 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
822 iscsi_set_events(iscsilun
);
827 * We support iscsi url's on the form
828 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
830 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
832 IscsiLun
*iscsilun
= bs
->opaque
;
833 struct iscsi_context
*iscsi
= NULL
;
834 struct iscsi_url
*iscsi_url
= NULL
;
835 struct scsi_task
*task
= NULL
;
836 struct scsi_inquiry_standard
*inq
= NULL
;
837 struct scsi_readcapacity10
*rc10
= NULL
;
838 struct scsi_readcapacity16
*rc16
= NULL
;
839 char *initiator_name
= NULL
;
842 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
843 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
844 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
845 "of 512", BDRV_SECTOR_SIZE
);
849 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
850 if (iscsi_url
== NULL
) {
851 error_report("Failed to parse URL : %s", filename
);
856 memset(iscsilun
, 0, sizeof(IscsiLun
));
858 initiator_name
= parse_initiator_name(iscsi_url
->target
);
860 iscsi
= iscsi_create_context(initiator_name
);
862 error_report("iSCSI: Failed to create iSCSI context.");
867 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
868 error_report("iSCSI: Failed to set target name.");
873 if (iscsi_url
->user
!= NULL
) {
874 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
877 error_report("Failed to set initiator username and password");
883 /* check if we got CHAP username/password via the options */
884 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
885 error_report("iSCSI: Failed to set CHAP user/password");
890 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
891 error_report("iSCSI: Failed to set session type to normal.");
896 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
898 /* check if we got HEADER_DIGEST via the options */
899 parse_header_digest(iscsi
, iscsi_url
->target
);
901 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
902 error_report("iSCSI: Failed to connect to LUN : %s",
903 iscsi_get_error(iscsi
));
908 iscsilun
->iscsi
= iscsi
;
909 iscsilun
->lun
= iscsi_url
->lun
;
911 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
913 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
914 error_report("iSCSI: failed to send inquiry command.");
919 inq
= scsi_datain_unmarshall(task
);
921 error_report("iSCSI: Failed to unmarshall inquiry data.");
926 iscsilun
->type
= inq
->periperal_device_type
;
928 scsi_free_scsi_task(task
);
930 switch (iscsilun
->type
) {
932 task
= iscsi_readcapacity16_sync(iscsi
, iscsilun
->lun
);
933 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
934 error_report("iSCSI: failed to send readcapacity16 command.");
938 rc16
= scsi_datain_unmarshall(task
);
940 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
944 iscsilun
->block_size
= rc16
->block_length
;
945 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
948 task
= iscsi_readcapacity10_sync(iscsi
, iscsilun
->lun
, 0, 0);
949 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
950 error_report("iSCSI: failed to send readcapacity10 command.");
954 rc10
= scsi_datain_unmarshall(task
);
956 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
960 iscsilun
->block_size
= rc10
->block_size
;
961 if (rc10
->lba
== 0) {
962 /* blank disk loaded */
963 iscsilun
->num_blocks
= 0;
965 iscsilun
->num_blocks
= rc10
->lba
+ 1;
972 bs
->total_sectors
= iscsilun
->num_blocks
*
973 iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
975 /* Medium changer or tape. We dont have any emulation for this so this must
976 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
977 * to read from the device to guess the image format.
979 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
980 iscsilun
->type
== TYPE_TAPE
) {
986 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
987 /* Set up a timer for sending out iSCSI NOPs */
988 iscsilun
->nop_timer
= qemu_new_timer_ms(rt_clock
, iscsi_nop_timed_event
, iscsilun
);
989 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
993 if (initiator_name
!= NULL
) {
994 g_free(initiator_name
);
996 if (iscsi_url
!= NULL
) {
997 iscsi_destroy_url(iscsi_url
);
1000 scsi_free_scsi_task(task
);
1004 if (iscsi
!= NULL
) {
1005 iscsi_destroy_context(iscsi
);
1007 memset(iscsilun
, 0, sizeof(IscsiLun
));
1012 static void iscsi_close(BlockDriverState
*bs
)
1014 IscsiLun
*iscsilun
= bs
->opaque
;
1015 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1017 if (iscsilun
->nop_timer
) {
1018 qemu_del_timer(iscsilun
->nop_timer
);
1019 qemu_free_timer(iscsilun
->nop_timer
);
1021 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1022 iscsi_destroy_context(iscsi
);
1023 memset(iscsilun
, 0, sizeof(IscsiLun
));
1026 static int iscsi_has_zero_init(BlockDriverState
*bs
)
1031 static int iscsi_create(const char *filename
, QEMUOptionParameter
*options
)
1034 int64_t total_size
= 0;
1035 BlockDriverState bs
;
1036 IscsiLun
*iscsilun
= NULL
;
1038 memset(&bs
, 0, sizeof(BlockDriverState
));
1040 /* Read out options */
1041 while (options
&& options
->name
) {
1042 if (!strcmp(options
->name
, "size")) {
1043 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1048 bs
.opaque
= g_malloc0(sizeof(struct IscsiLun
));
1049 iscsilun
= bs
.opaque
;
1051 ret
= iscsi_open(&bs
, filename
, 0);
1055 if (iscsilun
->nop_timer
) {
1056 qemu_del_timer(iscsilun
->nop_timer
);
1057 qemu_free_timer(iscsilun
->nop_timer
);
1059 if (iscsilun
->type
!= TYPE_DISK
) {
1063 if (bs
.total_sectors
< total_size
) {
1069 if (iscsilun
->iscsi
!= NULL
) {
1070 iscsi_destroy_context(iscsilun
->iscsi
);
1076 static QEMUOptionParameter iscsi_create_options
[] = {
1078 .name
= BLOCK_OPT_SIZE
,
1080 .help
= "Virtual disk size"
1085 static BlockDriver bdrv_iscsi
= {
1086 .format_name
= "iscsi",
1087 .protocol_name
= "iscsi",
1089 .instance_size
= sizeof(IscsiLun
),
1090 .bdrv_file_open
= iscsi_open
,
1091 .bdrv_close
= iscsi_close
,
1092 .bdrv_create
= iscsi_create
,
1093 .create_options
= iscsi_create_options
,
1095 .bdrv_getlength
= iscsi_getlength
,
1097 .bdrv_aio_readv
= iscsi_aio_readv
,
1098 .bdrv_aio_writev
= iscsi_aio_writev
,
1099 .bdrv_aio_flush
= iscsi_aio_flush
,
1101 .bdrv_aio_discard
= iscsi_aio_discard
,
1102 .bdrv_has_zero_init
= iscsi_has_zero_init
,
1105 .bdrv_ioctl
= iscsi_ioctl
,
1106 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1110 static QemuOptsList qemu_iscsi_opts
= {
1112 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1116 .type
= QEMU_OPT_STRING
,
1117 .help
= "username for CHAP authentication to target",
1120 .type
= QEMU_OPT_STRING
,
1121 .help
= "password for CHAP authentication to target",
1123 .name
= "header-digest",
1124 .type
= QEMU_OPT_STRING
,
1125 .help
= "HeaderDigest setting. "
1126 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1128 .name
= "initiator-name",
1129 .type
= QEMU_OPT_STRING
,
1130 .help
= "Initiator iqn name to use when connecting",
1132 { /* end of list */ }
1136 static void iscsi_block_init(void)
1138 bdrv_register(&bdrv_iscsi
);
1139 qemu_add_opts(&qemu_iscsi_opts
);
1142 block_init(iscsi_block_init
);