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 if (acb
->task
!= NULL
) {
222 scsi_free_scsi_task(acb
->task
);
225 if (iscsi_aio_writev_acb(acb
) == 0) {
226 iscsi_set_events(acb
->iscsilun
);
230 error_report("Failed to write16 data to iSCSI lun. %s",
231 iscsi_get_error(iscsi
));
235 iscsi_schedule_bh(acb
);
238 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
240 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
244 iscsi_aio_writev_acb(IscsiAIOCB
*acb
)
246 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
248 uint32_t num_sectors
;
250 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
251 struct iscsi_data data
;
257 acb
->status
= -EINPROGRESS
;
260 /* this will allow us to get rid of 'buf' completely */
261 size
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
263 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
264 data
.size
= MIN(size
, acb
->qiov
->size
);
266 /* if the iovec only contains one buffer we can pass it directly */
267 if (acb
->qiov
->niov
== 1) {
268 data
.data
= acb
->qiov
->iov
[0].iov_base
;
270 acb
->buf
= g_malloc(data
.size
);
271 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, data
.size
);
272 data
.data
= acb
->buf
;
276 acb
->task
= malloc(sizeof(struct scsi_task
));
277 if (acb
->task
== NULL
) {
278 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
279 "command. %s", iscsi_get_error(iscsi
));
282 memset(acb
->task
, 0, sizeof(struct scsi_task
));
284 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
285 acb
->task
->cdb_size
= 16;
286 acb
->task
->cdb
[0] = 0x8a;
287 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
288 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
289 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
290 num_sectors
= size
/ acb
->iscsilun
->block_size
;
291 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
292 acb
->task
->expxferlen
= size
;
294 #if defined(LIBISCSI_FEATURE_IOVECTOR)
295 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
296 iscsi_aio_write16_cb
,
300 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
301 iscsi_aio_write16_cb
,
310 #if defined(LIBISCSI_FEATURE_IOVECTOR)
311 scsi_task_set_iov_out(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
317 static BlockDriverAIOCB
*
318 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
319 QEMUIOVector
*qiov
, int nb_sectors
,
320 BlockDriverCompletionFunc
*cb
,
323 IscsiLun
*iscsilun
= bs
->opaque
;
326 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
327 trace_iscsi_aio_writev(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
329 acb
->iscsilun
= iscsilun
;
331 acb
->nb_sectors
= nb_sectors
;
332 acb
->sector_num
= sector_num
;
333 acb
->retries
= ISCSI_CMD_RETRIES
;
335 if (iscsi_aio_writev_acb(acb
) != 0) {
337 scsi_free_scsi_task(acb
->task
);
339 qemu_aio_release(acb
);
343 iscsi_set_events(iscsilun
);
348 iscsi_aio_readv_acb(IscsiAIOCB
*acb
);
351 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
352 void *command_data
, void *opaque
)
354 IscsiAIOCB
*acb
= opaque
;
356 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
358 if (acb
->canceled
!= 0) {
364 if (status
== SCSI_STATUS_CHECK_CONDITION
365 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
366 && acb
->retries
-- > 0) {
367 if (acb
->task
!= NULL
) {
368 scsi_free_scsi_task(acb
->task
);
371 if (iscsi_aio_readv_acb(acb
) == 0) {
372 iscsi_set_events(acb
->iscsilun
);
376 error_report("Failed to read16 data from iSCSI lun. %s",
377 iscsi_get_error(iscsi
));
381 iscsi_schedule_bh(acb
);
385 iscsi_aio_readv_acb(IscsiAIOCB
*acb
)
387 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
389 uint32_t num_sectors
;
391 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
397 acb
->status
= -EINPROGRESS
;
400 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
401 * may be misaligned to the LUN, so we may need to read some extra
404 acb
->read_offset
= 0;
405 if (acb
->iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
406 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* acb
->sector_num
;
408 acb
->read_offset
= bdrv_offset
% acb
->iscsilun
->block_size
;
411 num_sectors
= (acb
->read_size
+ acb
->iscsilun
->block_size
412 + acb
->read_offset
- 1)
413 / acb
->iscsilun
->block_size
;
415 acb
->task
= malloc(sizeof(struct scsi_task
));
416 if (acb
->task
== NULL
) {
417 error_report("iSCSI: Failed to allocate task for scsi READ16 "
418 "command. %s", iscsi_get_error(iscsi
));
421 memset(acb
->task
, 0, sizeof(struct scsi_task
));
423 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
424 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
425 acb
->task
->expxferlen
= acb
->read_size
;
427 switch (acb
->iscsilun
->type
) {
429 acb
->task
->cdb_size
= 16;
430 acb
->task
->cdb
[0] = 0x88;
431 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
432 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
433 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
436 acb
->task
->cdb_size
= 10;
437 acb
->task
->cdb
[0] = 0x28;
438 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
439 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
443 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
451 #if defined(LIBISCSI_FEATURE_IOVECTOR)
452 scsi_task_set_iov_in(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
454 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
455 scsi_task_add_data_in_buffer(acb
->task
,
456 acb
->qiov
->iov
[i
].iov_len
,
457 acb
->qiov
->iov
[i
].iov_base
);
463 static BlockDriverAIOCB
*
464 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
465 QEMUIOVector
*qiov
, int nb_sectors
,
466 BlockDriverCompletionFunc
*cb
,
469 IscsiLun
*iscsilun
= bs
->opaque
;
472 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
473 trace_iscsi_aio_readv(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
475 acb
->nb_sectors
= nb_sectors
;
476 acb
->sector_num
= sector_num
;
477 acb
->iscsilun
= iscsilun
;
479 acb
->read_size
= BDRV_SECTOR_SIZE
* (size_t)acb
->nb_sectors
;
480 acb
->retries
= ISCSI_CMD_RETRIES
;
482 if (iscsi_aio_readv_acb(acb
) != 0) {
484 scsi_free_scsi_task(acb
->task
);
486 qemu_aio_release(acb
);
490 iscsi_set_events(iscsilun
);
495 iscsi_aio_flush_acb(IscsiAIOCB
*acb
);
498 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
499 void *command_data
, void *opaque
)
501 IscsiAIOCB
*acb
= opaque
;
503 if (acb
->canceled
!= 0) {
509 if (status
== SCSI_STATUS_CHECK_CONDITION
510 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
511 && acb
->retries
-- > 0) {
512 if (acb
->task
!= NULL
) {
513 scsi_free_scsi_task(acb
->task
);
516 if (iscsi_aio_flush_acb(acb
) == 0) {
517 iscsi_set_events(acb
->iscsilun
);
521 error_report("Failed to sync10 data on iSCSI lun. %s",
522 iscsi_get_error(iscsi
));
526 iscsi_schedule_bh(acb
);
530 iscsi_aio_flush_acb(IscsiAIOCB
*acb
)
532 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
536 acb
->status
= -EINPROGRESS
;
539 acb
->task
= iscsi_synchronizecache10_task(iscsi
, acb
->iscsilun
->lun
,
541 iscsi_synccache10_cb
,
543 if (acb
->task
== NULL
) {
544 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
545 iscsi_get_error(iscsi
));
552 static BlockDriverAIOCB
*
553 iscsi_aio_flush(BlockDriverState
*bs
,
554 BlockDriverCompletionFunc
*cb
, void *opaque
)
556 IscsiLun
*iscsilun
= bs
->opaque
;
560 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
562 acb
->iscsilun
= iscsilun
;
563 acb
->retries
= ISCSI_CMD_RETRIES
;
565 if (iscsi_aio_flush_acb(acb
) != 0) {
566 qemu_aio_release(acb
);
570 iscsi_set_events(iscsilun
);
575 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
);
578 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
579 void *command_data
, void *opaque
)
581 IscsiAIOCB
*acb
= opaque
;
583 if (acb
->canceled
!= 0) {
589 if (status
== SCSI_STATUS_CHECK_CONDITION
590 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
591 && acb
->retries
-- > 0) {
592 if (acb
->task
!= NULL
) {
593 scsi_free_scsi_task(acb
->task
);
596 if (iscsi_aio_discard_acb(acb
) == 0) {
597 iscsi_set_events(acb
->iscsilun
);
601 error_report("Failed to unmap data on iSCSI lun. %s",
602 iscsi_get_error(iscsi
));
606 iscsi_schedule_bh(acb
);
609 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
) {
610 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
611 struct unmap_list list
[1];
615 acb
->status
= -EINPROGRESS
;
618 list
[0].lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
619 list
[0].num
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
/ acb
->iscsilun
->block_size
;
621 acb
->task
= iscsi_unmap_task(iscsi
, acb
->iscsilun
->lun
,
625 if (acb
->task
== NULL
) {
626 error_report("iSCSI: Failed to send unmap command. %s",
627 iscsi_get_error(iscsi
));
634 static BlockDriverAIOCB
*
635 iscsi_aio_discard(BlockDriverState
*bs
,
636 int64_t sector_num
, int nb_sectors
,
637 BlockDriverCompletionFunc
*cb
, void *opaque
)
639 IscsiLun
*iscsilun
= bs
->opaque
;
642 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
644 acb
->iscsilun
= iscsilun
;
645 acb
->nb_sectors
= nb_sectors
;
646 acb
->sector_num
= sector_num
;
647 acb
->retries
= ISCSI_CMD_RETRIES
;
649 if (iscsi_aio_discard_acb(acb
) != 0) {
651 scsi_free_scsi_task(acb
->task
);
653 qemu_aio_release(acb
);
657 iscsi_set_events(iscsilun
);
664 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
665 void *command_data
, void *opaque
)
667 IscsiAIOCB
*acb
= opaque
;
669 if (acb
->canceled
!= 0) {
675 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
676 iscsi_get_error(iscsi
));
680 acb
->ioh
->driver_status
= 0;
681 acb
->ioh
->host_status
= 0;
684 #define SG_ERR_DRIVER_SENSE 0x08
686 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
689 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
691 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
692 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
693 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
694 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
697 iscsi_schedule_bh(acb
);
700 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
701 unsigned long int req
, void *buf
,
702 BlockDriverCompletionFunc
*cb
, void *opaque
)
704 IscsiLun
*iscsilun
= bs
->opaque
;
705 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
706 struct iscsi_data data
;
709 assert(req
== SG_IO
);
711 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
713 acb
->iscsilun
= iscsilun
;
716 acb
->status
= -EINPROGRESS
;
720 acb
->task
= malloc(sizeof(struct scsi_task
));
721 if (acb
->task
== NULL
) {
722 error_report("iSCSI: Failed to allocate task for scsi command. %s",
723 iscsi_get_error(iscsi
));
724 qemu_aio_release(acb
);
727 memset(acb
->task
, 0, sizeof(struct scsi_task
));
729 switch (acb
->ioh
->dxfer_direction
) {
730 case SG_DXFER_TO_DEV
:
731 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
733 case SG_DXFER_FROM_DEV
:
734 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
737 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
741 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
742 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
743 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
745 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
746 data
.data
= acb
->ioh
->dxferp
;
747 data
.size
= acb
->ioh
->dxfer_len
;
749 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
751 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
754 scsi_free_scsi_task(acb
->task
);
755 qemu_aio_release(acb
);
759 /* tell libiscsi to read straight into the buffer we got from ioctl */
760 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
761 scsi_task_add_data_in_buffer(acb
->task
,
766 iscsi_set_events(iscsilun
);
772 static void ioctl_cb(void *opaque
, int status
)
774 int *p_status
= opaque
;
778 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
780 IscsiLun
*iscsilun
= bs
->opaque
;
784 case SG_GET_VERSION_NUM
:
788 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
791 status
= -EINPROGRESS
;
792 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
794 while (status
== -EINPROGRESS
) {
807 iscsi_getlength(BlockDriverState
*bs
)
809 IscsiLun
*iscsilun
= bs
->opaque
;
812 len
= iscsilun
->num_blocks
;
813 len
*= iscsilun
->block_size
;
818 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
822 const char *user
= NULL
;
823 const char *password
= NULL
;
825 list
= qemu_find_opts("iscsi");
830 opts
= qemu_opts_find(list
, target
);
832 opts
= QTAILQ_FIRST(&list
->head
);
838 user
= qemu_opt_get(opts
, "user");
843 password
= qemu_opt_get(opts
, "password");
845 error_report("CHAP username specified but no password was given");
849 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
850 error_report("Failed to set initiator username and password");
857 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
861 const char *digest
= NULL
;
863 list
= qemu_find_opts("iscsi");
868 opts
= qemu_opts_find(list
, target
);
870 opts
= QTAILQ_FIRST(&list
->head
);
876 digest
= qemu_opt_get(opts
, "header-digest");
881 if (!strcmp(digest
, "CRC32C")) {
882 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
883 } else if (!strcmp(digest
, "NONE")) {
884 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
885 } else if (!strcmp(digest
, "CRC32C-NONE")) {
886 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
887 } else if (!strcmp(digest
, "NONE-CRC32C")) {
888 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
890 error_report("Invalid header-digest setting : %s", digest
);
894 static char *parse_initiator_name(const char *target
)
898 const char *name
= NULL
;
899 const char *iscsi_name
= qemu_get_vm_name();
901 list
= qemu_find_opts("iscsi");
903 opts
= qemu_opts_find(list
, target
);
905 opts
= QTAILQ_FIRST(&list
->head
);
908 name
= qemu_opt_get(opts
, "initiator-name");
913 return g_strdup(name
);
915 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
916 iscsi_name
? ":" : "",
917 iscsi_name
? iscsi_name
: "");
921 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
922 static void iscsi_nop_timed_event(void *opaque
)
924 IscsiLun
*iscsilun
= opaque
;
926 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
927 error_report("iSCSI: NOP timeout. Reconnecting...");
928 iscsi_reconnect(iscsilun
->iscsi
);
931 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
932 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
936 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
937 iscsi_set_events(iscsilun
);
941 static int iscsi_readcapacity_sync(IscsiLun
*iscsilun
)
943 struct scsi_task
*task
= NULL
;
944 struct scsi_readcapacity10
*rc10
= NULL
;
945 struct scsi_readcapacity16
*rc16
= NULL
;
947 int retries
= ISCSI_CMD_RETRIES
;
950 switch (iscsilun
->type
) {
952 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
953 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
954 if (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
955 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
957 scsi_free_scsi_task(task
);
960 error_report("iSCSI: failed to send readcapacity16 command.");
964 rc16
= scsi_datain_unmarshall(task
);
966 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
970 iscsilun
->block_size
= rc16
->block_length
;
971 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
974 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
975 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
976 error_report("iSCSI: failed to send readcapacity10 command.");
980 rc10
= scsi_datain_unmarshall(task
);
982 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
986 iscsilun
->block_size
= rc10
->block_size
;
987 if (rc10
->lba
== 0) {
988 /* blank disk loaded */
989 iscsilun
->num_blocks
= 0;
991 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1000 scsi_free_scsi_task(task
);
1006 /* TODO Convert to fine grained options */
1007 static QemuOptsList runtime_opts
= {
1009 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1013 .type
= QEMU_OPT_STRING
,
1014 .help
= "URL to the iscsi image",
1016 { /* end of list */ }
1021 * We support iscsi url's on the form
1022 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1024 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1026 IscsiLun
*iscsilun
= bs
->opaque
;
1027 struct iscsi_context
*iscsi
= NULL
;
1028 struct iscsi_url
*iscsi_url
= NULL
;
1029 struct scsi_task
*task
= NULL
;
1030 struct scsi_inquiry_standard
*inq
= NULL
;
1031 char *initiator_name
= NULL
;
1033 Error
*local_err
= NULL
;
1034 const char *filename
;
1037 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
1038 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1039 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1040 "of 512", BDRV_SECTOR_SIZE
);
1044 opts
= qemu_opts_create_nofail(&runtime_opts
);
1045 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1046 if (error_is_set(&local_err
)) {
1047 qerror_report_err(local_err
);
1048 error_free(local_err
);
1053 filename
= qemu_opt_get(opts
, "filename");
1056 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1057 if (iscsi_url
== NULL
) {
1058 error_report("Failed to parse URL : %s", filename
);
1063 memset(iscsilun
, 0, sizeof(IscsiLun
));
1065 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1067 iscsi
= iscsi_create_context(initiator_name
);
1068 if (iscsi
== NULL
) {
1069 error_report("iSCSI: Failed to create iSCSI context.");
1074 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1075 error_report("iSCSI: Failed to set target name.");
1080 if (iscsi_url
->user
!= NULL
) {
1081 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1084 error_report("Failed to set initiator username and password");
1090 /* check if we got CHAP username/password via the options */
1091 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
1092 error_report("iSCSI: Failed to set CHAP user/password");
1097 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1098 error_report("iSCSI: Failed to set session type to normal.");
1103 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1105 /* check if we got HEADER_DIGEST via the options */
1106 parse_header_digest(iscsi
, iscsi_url
->target
);
1108 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1109 error_report("iSCSI: Failed to connect to LUN : %s",
1110 iscsi_get_error(iscsi
));
1115 iscsilun
->iscsi
= iscsi
;
1116 iscsilun
->lun
= iscsi_url
->lun
;
1118 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
1120 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1121 error_report("iSCSI: failed to send inquiry command.");
1126 inq
= scsi_datain_unmarshall(task
);
1128 error_report("iSCSI: Failed to unmarshall inquiry data.");
1133 iscsilun
->type
= inq
->periperal_device_type
;
1135 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1138 bs
->total_sectors
= iscsilun
->num_blocks
*
1139 iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
1141 /* Medium changer or tape. We dont have any emulation for this so this must
1142 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1143 * to read from the device to guess the image format.
1145 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1146 iscsilun
->type
== TYPE_TAPE
) {
1150 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1151 /* Set up a timer for sending out iSCSI NOPs */
1152 iscsilun
->nop_timer
= qemu_new_timer_ms(rt_clock
, iscsi_nop_timed_event
, iscsilun
);
1153 qemu_mod_timer(iscsilun
->nop_timer
, qemu_get_clock_ms(rt_clock
) + NOP_INTERVAL
);
1157 qemu_opts_del(opts
);
1158 if (initiator_name
!= NULL
) {
1159 g_free(initiator_name
);
1161 if (iscsi_url
!= NULL
) {
1162 iscsi_destroy_url(iscsi_url
);
1165 scsi_free_scsi_task(task
);
1169 if (iscsi
!= NULL
) {
1170 iscsi_destroy_context(iscsi
);
1172 memset(iscsilun
, 0, sizeof(IscsiLun
));
1177 static void iscsi_close(BlockDriverState
*bs
)
1179 IscsiLun
*iscsilun
= bs
->opaque
;
1180 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1182 if (iscsilun
->nop_timer
) {
1183 qemu_del_timer(iscsilun
->nop_timer
);
1184 qemu_free_timer(iscsilun
->nop_timer
);
1186 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1187 iscsi_destroy_context(iscsi
);
1188 memset(iscsilun
, 0, sizeof(IscsiLun
));
1191 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1193 IscsiLun
*iscsilun
= bs
->opaque
;
1196 if (iscsilun
->type
!= TYPE_DISK
) {
1200 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1204 if (offset
> iscsi_getlength(bs
)) {
1211 static int iscsi_has_zero_init(BlockDriverState
*bs
)
1216 static int iscsi_create(const char *filename
, QEMUOptionParameter
*options
)
1219 int64_t total_size
= 0;
1220 BlockDriverState bs
;
1221 IscsiLun
*iscsilun
= NULL
;
1224 memset(&bs
, 0, sizeof(BlockDriverState
));
1226 /* Read out options */
1227 while (options
&& options
->name
) {
1228 if (!strcmp(options
->name
, "size")) {
1229 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1234 bs
.opaque
= g_malloc0(sizeof(struct IscsiLun
));
1235 iscsilun
= bs
.opaque
;
1237 bs_options
= qdict_new();
1238 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1239 ret
= iscsi_open(&bs
, bs_options
, 0);
1240 QDECREF(bs_options
);
1245 if (iscsilun
->nop_timer
) {
1246 qemu_del_timer(iscsilun
->nop_timer
);
1247 qemu_free_timer(iscsilun
->nop_timer
);
1249 if (iscsilun
->type
!= TYPE_DISK
) {
1253 if (bs
.total_sectors
< total_size
) {
1259 if (iscsilun
->iscsi
!= NULL
) {
1260 iscsi_destroy_context(iscsilun
->iscsi
);
1266 static QEMUOptionParameter iscsi_create_options
[] = {
1268 .name
= BLOCK_OPT_SIZE
,
1270 .help
= "Virtual disk size"
1275 static BlockDriver bdrv_iscsi
= {
1276 .format_name
= "iscsi",
1277 .protocol_name
= "iscsi",
1279 .instance_size
= sizeof(IscsiLun
),
1280 .bdrv_file_open
= iscsi_open
,
1281 .bdrv_close
= iscsi_close
,
1282 .bdrv_create
= iscsi_create
,
1283 .create_options
= iscsi_create_options
,
1285 .bdrv_getlength
= iscsi_getlength
,
1286 .bdrv_truncate
= iscsi_truncate
,
1288 .bdrv_aio_readv
= iscsi_aio_readv
,
1289 .bdrv_aio_writev
= iscsi_aio_writev
,
1290 .bdrv_aio_flush
= iscsi_aio_flush
,
1292 .bdrv_aio_discard
= iscsi_aio_discard
,
1293 .bdrv_has_zero_init
= iscsi_has_zero_init
,
1296 .bdrv_ioctl
= iscsi_ioctl
,
1297 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1301 static QemuOptsList qemu_iscsi_opts
= {
1303 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1307 .type
= QEMU_OPT_STRING
,
1308 .help
= "username for CHAP authentication to target",
1311 .type
= QEMU_OPT_STRING
,
1312 .help
= "password for CHAP authentication to target",
1314 .name
= "header-digest",
1315 .type
= QEMU_OPT_STRING
,
1316 .help
= "HeaderDigest setting. "
1317 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1319 .name
= "initiator-name",
1320 .type
= QEMU_OPT_STRING
,
1321 .help
= "Initiator iqn name to use when connecting",
1323 { /* end of list */ }
1327 static void iscsi_block_init(void)
1329 bdrv_register(&bdrv_iscsi
);
1330 qemu_add_opts(&qemu_iscsi_opts
);
1333 block_init(iscsi_block_init
);