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"
37 #include <iscsi/iscsi.h>
38 #include <iscsi/scsi-lowlevel.h>
42 #include <block/scsi.h>
45 typedef struct IscsiLun
{
46 struct iscsi_context
*iscsi
;
48 enum scsi_inquiry_peripheral_device_type type
;
55 typedef struct IscsiAIOCB
{
56 BlockDriverAIOCB common
;
60 struct scsi_task
*task
;
72 #define NOP_INTERVAL 5000
73 #define MAX_NOP_FAILURES 3
74 #define ISCSI_CMD_RETRIES 5
81 qemu_bh_delete(acb
->bh
);
86 if (acb
->canceled
== 0) {
87 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
90 if (acb
->task
!= NULL
) {
91 scsi_free_scsi_task(acb
->task
);
95 qemu_aio_release(acb
);
99 iscsi_schedule_bh(IscsiAIOCB
*acb
)
104 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
105 qemu_bh_schedule(acb
->bh
);
110 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
113 IscsiAIOCB
*acb
= private_data
;
115 acb
->status
= -ECANCELED
;
116 iscsi_schedule_bh(acb
);
120 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
122 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
123 IscsiLun
*iscsilun
= acb
->iscsilun
;
125 if (acb
->status
!= -EINPROGRESS
) {
131 /* send a task mgmt call to the target to cancel the task on the target */
132 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
133 iscsi_abort_task_cb
, acb
);
135 while (acb
->status
== -EINPROGRESS
) {
140 static const AIOCBInfo iscsi_aiocb_info
= {
141 .aiocb_size
= sizeof(IscsiAIOCB
),
142 .cancel
= iscsi_aio_cancel
,
146 static void iscsi_process_read(void *arg
);
147 static void iscsi_process_write(void *arg
);
150 iscsi_set_events(IscsiLun
*iscsilun
)
152 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
155 /* We always register a read handler. */
157 ev
|= iscsi_which_events(iscsi
);
158 if (ev
!= iscsilun
->events
) {
159 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
161 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
166 iscsilun
->events
= ev
;
170 iscsi_process_read(void *arg
)
172 IscsiLun
*iscsilun
= arg
;
173 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
175 iscsi_service(iscsi
, POLLIN
);
176 iscsi_set_events(iscsilun
);
180 iscsi_process_write(void *arg
)
182 IscsiLun
*iscsilun
= arg
;
183 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
185 iscsi_service(iscsi
, POLLOUT
);
186 iscsi_set_events(iscsilun
);
190 iscsi_aio_writev_acb(IscsiAIOCB
*acb
);
193 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
194 void *command_data
, void *opaque
)
196 IscsiAIOCB
*acb
= opaque
;
198 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
203 if (acb
->canceled
!= 0) {
209 if (status
== SCSI_STATUS_CHECK_CONDITION
210 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
211 && acb
->retries
-- > 0) {
212 scsi_free_scsi_task(acb
->task
);
214 if (iscsi_aio_writev_acb(acb
) == 0) {
215 iscsi_set_events(acb
->iscsilun
);
219 error_report("Failed to write16 data to iSCSI lun. %s",
220 iscsi_get_error(iscsi
));
224 iscsi_schedule_bh(acb
);
227 static int64_t sector_lun2qemu(int64_t sector
, IscsiLun
*iscsilun
)
229 return sector
* iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
232 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
234 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
237 static bool is_request_lun_aligned(int64_t sector_num
, int nb_sectors
,
240 if ((sector_num
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
||
241 (nb_sectors
* BDRV_SECTOR_SIZE
) % iscsilun
->block_size
) {
242 error_report("iSCSI misaligned request: "
243 "iscsilun->block_size %u, sector_num %" PRIi64
245 iscsilun
->block_size
, sector_num
, nb_sectors
);
252 iscsi_aio_writev_acb(IscsiAIOCB
*acb
)
254 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
256 uint32_t num_sectors
;
258 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
259 struct iscsi_data data
;
265 acb
->status
= -EINPROGRESS
;
268 /* this will allow us to get rid of 'buf' completely */
269 size
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
271 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
272 data
.size
= MIN(size
, acb
->qiov
->size
);
274 /* if the iovec only contains one buffer we can pass it directly */
275 if (acb
->qiov
->niov
== 1) {
276 data
.data
= acb
->qiov
->iov
[0].iov_base
;
278 acb
->buf
= g_malloc(data
.size
);
279 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, data
.size
);
280 data
.data
= acb
->buf
;
284 acb
->task
= malloc(sizeof(struct scsi_task
));
285 if (acb
->task
== NULL
) {
286 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
287 "command. %s", iscsi_get_error(iscsi
));
290 memset(acb
->task
, 0, sizeof(struct scsi_task
));
292 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
293 acb
->task
->cdb_size
= 16;
294 acb
->task
->cdb
[0] = 0x8a;
295 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
296 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
297 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
298 num_sectors
= sector_qemu2lun(acb
->nb_sectors
, acb
->iscsilun
);
299 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
300 acb
->task
->expxferlen
= size
;
302 #if defined(LIBISCSI_FEATURE_IOVECTOR)
303 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
304 iscsi_aio_write16_cb
,
308 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
309 iscsi_aio_write16_cb
,
314 scsi_free_scsi_task(acb
->task
);
319 #if defined(LIBISCSI_FEATURE_IOVECTOR)
320 scsi_task_set_iov_out(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
326 static BlockDriverAIOCB
*
327 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
328 QEMUIOVector
*qiov
, int nb_sectors
,
329 BlockDriverCompletionFunc
*cb
,
332 IscsiLun
*iscsilun
= bs
->opaque
;
335 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
339 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
340 trace_iscsi_aio_writev(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
342 acb
->iscsilun
= iscsilun
;
344 acb
->nb_sectors
= nb_sectors
;
345 acb
->sector_num
= sector_num
;
346 acb
->retries
= ISCSI_CMD_RETRIES
;
348 if (iscsi_aio_writev_acb(acb
) != 0) {
349 qemu_aio_release(acb
);
353 iscsi_set_events(iscsilun
);
358 iscsi_aio_readv_acb(IscsiAIOCB
*acb
);
361 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
362 void *command_data
, void *opaque
)
364 IscsiAIOCB
*acb
= opaque
;
366 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
368 if (acb
->canceled
!= 0) {
374 if (status
== SCSI_STATUS_CHECK_CONDITION
375 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
376 && acb
->retries
-- > 0) {
377 scsi_free_scsi_task(acb
->task
);
379 if (iscsi_aio_readv_acb(acb
) == 0) {
380 iscsi_set_events(acb
->iscsilun
);
384 error_report("Failed to read16 data from iSCSI lun. %s",
385 iscsi_get_error(iscsi
));
389 iscsi_schedule_bh(acb
);
393 iscsi_aio_readv_acb(IscsiAIOCB
*acb
)
395 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
398 uint32_t num_sectors
;
400 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
406 acb
->status
= -EINPROGRESS
;
409 size
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
411 acb
->task
= malloc(sizeof(struct scsi_task
));
412 if (acb
->task
== NULL
) {
413 error_report("iSCSI: Failed to allocate task for scsi READ16 "
414 "command. %s", iscsi_get_error(iscsi
));
417 memset(acb
->task
, 0, sizeof(struct scsi_task
));
419 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
420 acb
->task
->expxferlen
= size
;
421 lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
422 num_sectors
= sector_qemu2lun(acb
->nb_sectors
, acb
->iscsilun
);
424 switch (acb
->iscsilun
->type
) {
426 acb
->task
->cdb_size
= 16;
427 acb
->task
->cdb
[0] = 0x88;
428 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
429 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
430 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
433 acb
->task
->cdb_size
= 10;
434 acb
->task
->cdb
[0] = 0x28;
435 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
436 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
440 ret
= iscsi_scsi_command_async(iscsi
, acb
->iscsilun
->lun
, acb
->task
,
445 scsi_free_scsi_task(acb
->task
);
449 #if defined(LIBISCSI_FEATURE_IOVECTOR)
450 scsi_task_set_iov_in(acb
->task
, (struct scsi_iovec
*) acb
->qiov
->iov
, acb
->qiov
->niov
);
452 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
453 scsi_task_add_data_in_buffer(acb
->task
,
454 acb
->qiov
->iov
[i
].iov_len
,
455 acb
->qiov
->iov
[i
].iov_base
);
461 static BlockDriverAIOCB
*
462 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
463 QEMUIOVector
*qiov
, int nb_sectors
,
464 BlockDriverCompletionFunc
*cb
,
467 IscsiLun
*iscsilun
= bs
->opaque
;
470 if (!is_request_lun_aligned(sector_num
, nb_sectors
, iscsilun
)) {
474 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
475 trace_iscsi_aio_readv(iscsilun
->iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
477 acb
->nb_sectors
= nb_sectors
;
478 acb
->sector_num
= sector_num
;
479 acb
->iscsilun
= iscsilun
;
481 acb
->retries
= ISCSI_CMD_RETRIES
;
483 if (iscsi_aio_readv_acb(acb
) != 0) {
484 qemu_aio_release(acb
);
488 iscsi_set_events(iscsilun
);
493 iscsi_aio_flush_acb(IscsiAIOCB
*acb
);
496 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
497 void *command_data
, void *opaque
)
499 IscsiAIOCB
*acb
= opaque
;
501 if (acb
->canceled
!= 0) {
507 if (status
== SCSI_STATUS_CHECK_CONDITION
508 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
509 && acb
->retries
-- > 0) {
510 scsi_free_scsi_task(acb
->task
);
512 if (iscsi_aio_flush_acb(acb
) == 0) {
513 iscsi_set_events(acb
->iscsilun
);
517 error_report("Failed to sync10 data on iSCSI lun. %s",
518 iscsi_get_error(iscsi
));
522 iscsi_schedule_bh(acb
);
526 iscsi_aio_flush_acb(IscsiAIOCB
*acb
)
528 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
532 acb
->status
= -EINPROGRESS
;
535 acb
->task
= iscsi_synchronizecache10_task(iscsi
, acb
->iscsilun
->lun
,
537 iscsi_synccache10_cb
,
539 if (acb
->task
== NULL
) {
540 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
541 iscsi_get_error(iscsi
));
548 static BlockDriverAIOCB
*
549 iscsi_aio_flush(BlockDriverState
*bs
,
550 BlockDriverCompletionFunc
*cb
, void *opaque
)
552 IscsiLun
*iscsilun
= bs
->opaque
;
556 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
558 acb
->iscsilun
= iscsilun
;
559 acb
->retries
= ISCSI_CMD_RETRIES
;
561 if (iscsi_aio_flush_acb(acb
) != 0) {
562 qemu_aio_release(acb
);
566 iscsi_set_events(iscsilun
);
571 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
);
574 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
575 void *command_data
, void *opaque
)
577 IscsiAIOCB
*acb
= opaque
;
579 if (acb
->canceled
!= 0) {
585 if (status
== SCSI_STATUS_CHECK_CONDITION
586 && acb
->task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
587 && acb
->retries
-- > 0) {
588 scsi_free_scsi_task(acb
->task
);
590 if (iscsi_aio_discard_acb(acb
) == 0) {
591 iscsi_set_events(acb
->iscsilun
);
595 error_report("Failed to unmap data on iSCSI lun. %s",
596 iscsi_get_error(iscsi
));
600 iscsi_schedule_bh(acb
);
603 static int iscsi_aio_discard_acb(IscsiAIOCB
*acb
) {
604 struct iscsi_context
*iscsi
= acb
->iscsilun
->iscsi
;
605 struct unmap_list list
[1];
609 acb
->status
= -EINPROGRESS
;
612 list
[0].lba
= sector_qemu2lun(acb
->sector_num
, acb
->iscsilun
);
613 list
[0].num
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
/ acb
->iscsilun
->block_size
;
615 acb
->task
= iscsi_unmap_task(iscsi
, acb
->iscsilun
->lun
,
619 if (acb
->task
== NULL
) {
620 error_report("iSCSI: Failed to send unmap command. %s",
621 iscsi_get_error(iscsi
));
628 static BlockDriverAIOCB
*
629 iscsi_aio_discard(BlockDriverState
*bs
,
630 int64_t sector_num
, int nb_sectors
,
631 BlockDriverCompletionFunc
*cb
, void *opaque
)
633 IscsiLun
*iscsilun
= bs
->opaque
;
636 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
638 acb
->iscsilun
= iscsilun
;
639 acb
->nb_sectors
= nb_sectors
;
640 acb
->sector_num
= sector_num
;
641 acb
->retries
= ISCSI_CMD_RETRIES
;
643 if (iscsi_aio_discard_acb(acb
) != 0) {
644 qemu_aio_release(acb
);
648 iscsi_set_events(iscsilun
);
655 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
656 void *command_data
, void *opaque
)
658 IscsiAIOCB
*acb
= opaque
;
663 if (acb
->canceled
!= 0) {
669 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
670 iscsi_get_error(iscsi
));
674 acb
->ioh
->driver_status
= 0;
675 acb
->ioh
->host_status
= 0;
678 #define SG_ERR_DRIVER_SENSE 0x08
680 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
683 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
685 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
686 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
687 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
688 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
691 iscsi_schedule_bh(acb
);
694 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
695 unsigned long int req
, void *buf
,
696 BlockDriverCompletionFunc
*cb
, void *opaque
)
698 IscsiLun
*iscsilun
= bs
->opaque
;
699 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
700 struct iscsi_data data
;
703 assert(req
== SG_IO
);
705 acb
= qemu_aio_get(&iscsi_aiocb_info
, bs
, cb
, opaque
);
707 acb
->iscsilun
= iscsilun
;
710 acb
->status
= -EINPROGRESS
;
714 acb
->task
= malloc(sizeof(struct scsi_task
));
715 if (acb
->task
== NULL
) {
716 error_report("iSCSI: Failed to allocate task for scsi command. %s",
717 iscsi_get_error(iscsi
));
718 qemu_aio_release(acb
);
721 memset(acb
->task
, 0, sizeof(struct scsi_task
));
723 switch (acb
->ioh
->dxfer_direction
) {
724 case SG_DXFER_TO_DEV
:
725 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
727 case SG_DXFER_FROM_DEV
:
728 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
731 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
735 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
736 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
737 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
740 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
741 if (acb
->ioh
->iovec_count
== 0) {
742 data
.data
= acb
->ioh
->dxferp
;
743 data
.size
= acb
->ioh
->dxfer_len
;
745 #if defined(LIBISCSI_FEATURE_IOVECTOR)
746 scsi_task_set_iov_out(acb
->task
,
747 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
748 acb
->ioh
->iovec_count
);
750 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
752 acb
->buf
= g_malloc(acb
->ioh
->dxfer_len
);
753 data
.data
= acb
->buf
;
754 data
.size
= iov_to_buf(iov
, acb
->ioh
->iovec_count
, 0,
755 acb
->buf
, acb
->ioh
->dxfer_len
);
760 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
762 (data
.size
> 0) ? &data
: NULL
,
764 scsi_free_scsi_task(acb
->task
);
765 qemu_aio_release(acb
);
769 /* tell libiscsi to read straight into the buffer we got from ioctl */
770 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
771 if (acb
->ioh
->iovec_count
== 0) {
772 scsi_task_add_data_in_buffer(acb
->task
,
776 #if defined(LIBISCSI_FEATURE_IOVECTOR)
777 scsi_task_set_iov_in(acb
->task
,
778 (struct scsi_iovec
*) acb
->ioh
->dxferp
,
779 acb
->ioh
->iovec_count
);
782 for (i
= 0; i
< acb
->ioh
->iovec_count
; i
++) {
783 struct iovec
*iov
= (struct iovec
*)acb
->ioh
->dxferp
;
785 scsi_task_add_data_in_buffer(acb
->task
,
793 iscsi_set_events(iscsilun
);
799 static void ioctl_cb(void *opaque
, int status
)
801 int *p_status
= opaque
;
805 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
807 IscsiLun
*iscsilun
= bs
->opaque
;
811 case SG_GET_VERSION_NUM
:
815 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
818 status
= -EINPROGRESS
;
819 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
821 while (status
== -EINPROGRESS
) {
834 iscsi_getlength(BlockDriverState
*bs
)
836 IscsiLun
*iscsilun
= bs
->opaque
;
839 len
= iscsilun
->num_blocks
;
840 len
*= iscsilun
->block_size
;
845 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
849 const char *user
= NULL
;
850 const char *password
= NULL
;
852 list
= qemu_find_opts("iscsi");
857 opts
= qemu_opts_find(list
, target
);
859 opts
= QTAILQ_FIRST(&list
->head
);
865 user
= qemu_opt_get(opts
, "user");
870 password
= qemu_opt_get(opts
, "password");
872 error_report("CHAP username specified but no password was given");
876 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
877 error_report("Failed to set initiator username and password");
884 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
888 const char *digest
= NULL
;
890 list
= qemu_find_opts("iscsi");
895 opts
= qemu_opts_find(list
, target
);
897 opts
= QTAILQ_FIRST(&list
->head
);
903 digest
= qemu_opt_get(opts
, "header-digest");
908 if (!strcmp(digest
, "CRC32C")) {
909 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
910 } else if (!strcmp(digest
, "NONE")) {
911 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
912 } else if (!strcmp(digest
, "CRC32C-NONE")) {
913 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
914 } else if (!strcmp(digest
, "NONE-CRC32C")) {
915 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
917 error_report("Invalid header-digest setting : %s", digest
);
921 static char *parse_initiator_name(const char *target
)
925 const char *name
= NULL
;
926 const char *iscsi_name
= qemu_get_vm_name();
928 list
= qemu_find_opts("iscsi");
930 opts
= qemu_opts_find(list
, target
);
932 opts
= QTAILQ_FIRST(&list
->head
);
935 name
= qemu_opt_get(opts
, "initiator-name");
940 return g_strdup(name
);
942 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
943 iscsi_name
? ":" : "",
944 iscsi_name
? iscsi_name
: "");
948 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
949 static void iscsi_nop_timed_event(void *opaque
)
951 IscsiLun
*iscsilun
= opaque
;
953 if (iscsi_get_nops_in_flight(iscsilun
->iscsi
) > MAX_NOP_FAILURES
) {
954 error_report("iSCSI: NOP timeout. Reconnecting...");
955 iscsi_reconnect(iscsilun
->iscsi
);
958 if (iscsi_nop_out_async(iscsilun
->iscsi
, NULL
, NULL
, 0, NULL
) != 0) {
959 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
963 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
964 iscsi_set_events(iscsilun
);
968 static int iscsi_readcapacity_sync(IscsiLun
*iscsilun
)
970 struct scsi_task
*task
= NULL
;
971 struct scsi_readcapacity10
*rc10
= NULL
;
972 struct scsi_readcapacity16
*rc16
= NULL
;
974 int retries
= ISCSI_CMD_RETRIES
;
978 scsi_free_scsi_task(task
);
982 switch (iscsilun
->type
) {
984 task
= iscsi_readcapacity16_sync(iscsilun
->iscsi
, iscsilun
->lun
);
985 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
986 rc16
= scsi_datain_unmarshall(task
);
988 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
991 iscsilun
->block_size
= rc16
->block_length
;
992 iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
997 task
= iscsi_readcapacity10_sync(iscsilun
->iscsi
, iscsilun
->lun
, 0, 0);
998 if (task
!= NULL
&& task
->status
== SCSI_STATUS_GOOD
) {
999 rc10
= scsi_datain_unmarshall(task
);
1001 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
1004 iscsilun
->block_size
= rc10
->block_size
;
1005 if (rc10
->lba
== 0) {
1006 /* blank disk loaded */
1007 iscsilun
->num_blocks
= 0;
1009 iscsilun
->num_blocks
= rc10
->lba
+ 1;
1017 } while (task
!= NULL
&& task
->status
== SCSI_STATUS_CHECK_CONDITION
1018 && task
->sense
.key
== SCSI_SENSE_UNIT_ATTENTION
1021 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1022 error_report("iSCSI: failed to send readcapacity10 command.");
1026 scsi_free_scsi_task(task
);
1031 /* TODO Convert to fine grained options */
1032 static QemuOptsList runtime_opts
= {
1034 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
1038 .type
= QEMU_OPT_STRING
,
1039 .help
= "URL to the iscsi image",
1041 { /* end of list */ }
1046 * We support iscsi url's on the form
1047 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
1049 static int iscsi_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
1051 IscsiLun
*iscsilun
= bs
->opaque
;
1052 struct iscsi_context
*iscsi
= NULL
;
1053 struct iscsi_url
*iscsi_url
= NULL
;
1054 struct scsi_task
*task
= NULL
;
1055 struct scsi_inquiry_standard
*inq
= NULL
;
1056 char *initiator_name
= NULL
;
1058 Error
*local_err
= NULL
;
1059 const char *filename
;
1062 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
1063 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
1064 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
1065 "of 512", BDRV_SECTOR_SIZE
);
1069 opts
= qemu_opts_create_nofail(&runtime_opts
);
1070 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1071 if (error_is_set(&local_err
)) {
1072 qerror_report_err(local_err
);
1073 error_free(local_err
);
1078 filename
= qemu_opt_get(opts
, "filename");
1081 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
1082 if (iscsi_url
== NULL
) {
1083 error_report("Failed to parse URL : %s", filename
);
1088 memset(iscsilun
, 0, sizeof(IscsiLun
));
1090 initiator_name
= parse_initiator_name(iscsi_url
->target
);
1092 iscsi
= iscsi_create_context(initiator_name
);
1093 if (iscsi
== NULL
) {
1094 error_report("iSCSI: Failed to create iSCSI context.");
1099 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
1100 error_report("iSCSI: Failed to set target name.");
1105 if (iscsi_url
->user
!= NULL
) {
1106 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
1109 error_report("Failed to set initiator username and password");
1115 /* check if we got CHAP username/password via the options */
1116 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
1117 error_report("iSCSI: Failed to set CHAP user/password");
1122 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1123 error_report("iSCSI: Failed to set session type to normal.");
1128 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1130 /* check if we got HEADER_DIGEST via the options */
1131 parse_header_digest(iscsi
, iscsi_url
->target
);
1133 if (iscsi_full_connect_sync(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
) != 0) {
1134 error_report("iSCSI: Failed to connect to LUN : %s",
1135 iscsi_get_error(iscsi
));
1140 iscsilun
->iscsi
= iscsi
;
1141 iscsilun
->lun
= iscsi_url
->lun
;
1143 task
= iscsi_inquiry_sync(iscsi
, iscsilun
->lun
, 0, 0, 36);
1145 if (task
== NULL
|| task
->status
!= SCSI_STATUS_GOOD
) {
1146 error_report("iSCSI: failed to send inquiry command.");
1151 inq
= scsi_datain_unmarshall(task
);
1153 error_report("iSCSI: Failed to unmarshall inquiry data.");
1158 iscsilun
->type
= inq
->periperal_device_type
;
1160 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1163 bs
->total_sectors
= sector_lun2qemu(iscsilun
->num_blocks
, iscsilun
);
1165 /* Medium changer or tape. We dont have any emulation for this so this must
1166 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1167 * to read from the device to guess the image format.
1169 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1170 iscsilun
->type
== TYPE_TAPE
) {
1174 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
1175 /* Set up a timer for sending out iSCSI NOPs */
1176 iscsilun
->nop_timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, iscsi_nop_timed_event
, iscsilun
);
1177 timer_mod(iscsilun
->nop_timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + NOP_INTERVAL
);
1181 qemu_opts_del(opts
);
1182 if (initiator_name
!= NULL
) {
1183 g_free(initiator_name
);
1185 if (iscsi_url
!= NULL
) {
1186 iscsi_destroy_url(iscsi_url
);
1189 scsi_free_scsi_task(task
);
1193 if (iscsi
!= NULL
) {
1194 iscsi_destroy_context(iscsi
);
1196 memset(iscsilun
, 0, sizeof(IscsiLun
));
1201 static void iscsi_close(BlockDriverState
*bs
)
1203 IscsiLun
*iscsilun
= bs
->opaque
;
1204 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1206 if (iscsilun
->nop_timer
) {
1207 timer_del(iscsilun
->nop_timer
);
1208 timer_free(iscsilun
->nop_timer
);
1210 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
);
1211 iscsi_destroy_context(iscsi
);
1212 memset(iscsilun
, 0, sizeof(IscsiLun
));
1215 static int iscsi_truncate(BlockDriverState
*bs
, int64_t offset
)
1217 IscsiLun
*iscsilun
= bs
->opaque
;
1220 if (iscsilun
->type
!= TYPE_DISK
) {
1224 if ((ret
= iscsi_readcapacity_sync(iscsilun
)) != 0) {
1228 if (offset
> iscsi_getlength(bs
)) {
1235 static int iscsi_has_zero_init(BlockDriverState
*bs
)
1240 static int iscsi_create(const char *filename
, QEMUOptionParameter
*options
)
1243 int64_t total_size
= 0;
1244 BlockDriverState bs
;
1245 IscsiLun
*iscsilun
= NULL
;
1248 memset(&bs
, 0, sizeof(BlockDriverState
));
1250 /* Read out options */
1251 while (options
&& options
->name
) {
1252 if (!strcmp(options
->name
, "size")) {
1253 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
1258 bs
.opaque
= g_malloc0(sizeof(struct IscsiLun
));
1259 iscsilun
= bs
.opaque
;
1261 bs_options
= qdict_new();
1262 qdict_put(bs_options
, "filename", qstring_from_str(filename
));
1263 ret
= iscsi_open(&bs
, bs_options
, 0);
1264 QDECREF(bs_options
);
1269 if (iscsilun
->nop_timer
) {
1270 timer_del(iscsilun
->nop_timer
);
1271 timer_free(iscsilun
->nop_timer
);
1273 if (iscsilun
->type
!= TYPE_DISK
) {
1277 if (bs
.total_sectors
< total_size
) {
1284 if (iscsilun
->iscsi
!= NULL
) {
1285 iscsi_destroy_context(iscsilun
->iscsi
);
1291 static QEMUOptionParameter iscsi_create_options
[] = {
1293 .name
= BLOCK_OPT_SIZE
,
1295 .help
= "Virtual disk size"
1300 static BlockDriver bdrv_iscsi
= {
1301 .format_name
= "iscsi",
1302 .protocol_name
= "iscsi",
1304 .instance_size
= sizeof(IscsiLun
),
1305 .bdrv_file_open
= iscsi_open
,
1306 .bdrv_close
= iscsi_close
,
1307 .bdrv_create
= iscsi_create
,
1308 .create_options
= iscsi_create_options
,
1310 .bdrv_getlength
= iscsi_getlength
,
1311 .bdrv_truncate
= iscsi_truncate
,
1313 .bdrv_aio_readv
= iscsi_aio_readv
,
1314 .bdrv_aio_writev
= iscsi_aio_writev
,
1315 .bdrv_aio_flush
= iscsi_aio_flush
,
1317 .bdrv_aio_discard
= iscsi_aio_discard
,
1318 .bdrv_has_zero_init
= iscsi_has_zero_init
,
1321 .bdrv_ioctl
= iscsi_ioctl
,
1322 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1326 static QemuOptsList qemu_iscsi_opts
= {
1328 .head
= QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts
.head
),
1332 .type
= QEMU_OPT_STRING
,
1333 .help
= "username for CHAP authentication to target",
1336 .type
= QEMU_OPT_STRING
,
1337 .help
= "password for CHAP authentication to target",
1339 .name
= "header-digest",
1340 .type
= QEMU_OPT_STRING
,
1341 .help
= "HeaderDigest setting. "
1342 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1344 .name
= "initiator-name",
1345 .type
= QEMU_OPT_STRING
,
1346 .help
= "Initiator iqn name to use when connecting",
1348 { /* end of list */ }
1352 static void iscsi_block_init(void)
1354 bdrv_register(&bdrv_iscsi
);
1355 qemu_add_opts(&qemu_iscsi_opts
);
1358 block_init(iscsi_block_init
);