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-error.h"
31 #include "block_int.h"
33 #include "hw/scsi-defs.h"
35 #include <iscsi/iscsi.h>
36 #include <iscsi/scsi-lowlevel.h>
40 #include <hw/scsi-defs.h>
43 typedef struct IscsiLun
{
44 struct iscsi_context
*iscsi
;
46 enum scsi_inquiry_peripheral_device_type type
;
52 typedef struct IscsiAIOCB
{
53 BlockDriverAIOCB common
;
57 struct scsi_task
*task
;
76 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
82 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
84 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
85 IscsiLun
*iscsilun
= acb
->iscsilun
;
87 acb
->common
.cb(acb
->common
.opaque
, -ECANCELED
);
90 /* send a task mgmt call to the target to cancel the task on the target */
91 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
92 iscsi_abort_task_cb
, NULL
);
94 /* then also cancel the task locally in libiscsi */
95 iscsi_scsi_task_cancel(iscsilun
->iscsi
, acb
->task
);
98 static AIOPool iscsi_aio_pool
= {
99 .aiocb_size
= sizeof(IscsiAIOCB
),
100 .cancel
= iscsi_aio_cancel
,
104 static void iscsi_process_read(void *arg
);
105 static void iscsi_process_write(void *arg
);
107 static int iscsi_process_flush(void *arg
)
109 IscsiLun
*iscsilun
= arg
;
111 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
115 iscsi_set_events(IscsiLun
*iscsilun
)
117 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
120 /* We always register a read handler. */
122 ev
|= iscsi_which_events(iscsi
);
123 if (ev
!= iscsilun
->events
) {
124 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
126 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
132 /* If we just added an event, the callback might be delayed
133 * unless we call qemu_notify_event().
135 if (ev
& ~iscsilun
->events
) {
138 iscsilun
->events
= ev
;
142 iscsi_process_read(void *arg
)
144 IscsiLun
*iscsilun
= arg
;
145 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
147 iscsi_service(iscsi
, POLLIN
);
148 iscsi_set_events(iscsilun
);
152 iscsi_process_write(void *arg
)
154 IscsiLun
*iscsilun
= arg
;
155 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
157 iscsi_service(iscsi
, POLLOUT
);
158 iscsi_set_events(iscsilun
);
163 iscsi_schedule_bh(QEMUBHFunc
*cb
, IscsiAIOCB
*acb
)
165 acb
->bh
= qemu_bh_new(cb
, acb
);
167 error_report("oom: could not create iscsi bh");
171 qemu_bh_schedule(acb
->bh
);
176 iscsi_readv_writev_bh_cb(void *p
)
180 qemu_bh_delete(acb
->bh
);
182 if (acb
->canceled
== 0) {
183 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
186 qemu_aio_release(acb
);
191 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
192 void *command_data
, void *opaque
)
194 IscsiAIOCB
*acb
= opaque
;
196 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
200 if (acb
->canceled
!= 0) {
201 qemu_aio_release(acb
);
202 scsi_free_scsi_task(acb
->task
);
209 error_report("Failed to write16 data to iSCSI lun. %s",
210 iscsi_get_error(iscsi
));
214 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
215 scsi_free_scsi_task(acb
->task
);
219 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
221 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
224 static BlockDriverAIOCB
*
225 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
226 QEMUIOVector
*qiov
, int nb_sectors
,
227 BlockDriverCompletionFunc
*cb
,
230 IscsiLun
*iscsilun
= bs
->opaque
;
231 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
234 uint32_t num_sectors
;
236 struct iscsi_data data
;
238 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
239 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
241 acb
->iscsilun
= iscsilun
;
246 /* XXX we should pass the iovec to write16 to avoid the extra copy */
247 /* this will allow us to get rid of 'buf' completely */
248 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
249 acb
->buf
= g_malloc(size
);
250 qemu_iovec_to_buffer(acb
->qiov
, acb
->buf
);
253 acb
->task
= malloc(sizeof(struct scsi_task
));
254 if (acb
->task
== NULL
) {
255 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
256 "command. %s", iscsi_get_error(iscsi
));
257 qemu_aio_release(acb
);
260 memset(acb
->task
, 0, sizeof(struct scsi_task
));
262 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
263 acb
->task
->cdb_size
= 16;
264 acb
->task
->cdb
[0] = 0x8a;
265 if (!(bs
->open_flags
& BDRV_O_CACHE_WB
)) {
266 /* set FUA on writes when cache mode is write through */
267 acb
->task
->cdb
[1] |= 0x04;
269 lba
= sector_qemu2lun(sector_num
, iscsilun
);
270 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
271 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
272 num_sectors
= size
/ iscsilun
->block_size
;
273 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
274 acb
->task
->expxferlen
= size
;
276 data
.data
= acb
->buf
;
279 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
280 iscsi_aio_write16_cb
,
283 scsi_free_scsi_task(acb
->task
);
285 qemu_aio_release(acb
);
289 iscsi_set_events(iscsilun
);
295 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
296 void *command_data
, void *opaque
)
298 IscsiAIOCB
*acb
= opaque
;
300 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
302 if (acb
->canceled
!= 0) {
303 qemu_aio_release(acb
);
304 scsi_free_scsi_task(acb
->task
);
311 error_report("Failed to read16 data from iSCSI lun. %s",
312 iscsi_get_error(iscsi
));
316 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
317 scsi_free_scsi_task(acb
->task
);
321 static BlockDriverAIOCB
*
322 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
323 QEMUIOVector
*qiov
, int nb_sectors
,
324 BlockDriverCompletionFunc
*cb
,
327 IscsiLun
*iscsilun
= bs
->opaque
;
328 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
330 size_t qemu_read_size
;
333 uint32_t num_sectors
;
335 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
337 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
338 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
340 acb
->iscsilun
= iscsilun
;
344 acb
->read_size
= qemu_read_size
;
347 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
348 * may be misaligned to the LUN, so we may need to read some extra
351 acb
->read_offset
= 0;
352 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
353 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
355 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
358 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
359 + acb
->read_offset
- 1)
360 / iscsilun
->block_size
;
362 acb
->task
= malloc(sizeof(struct scsi_task
));
363 if (acb
->task
== NULL
) {
364 error_report("iSCSI: Failed to allocate task for scsi READ16 "
365 "command. %s", iscsi_get_error(iscsi
));
366 qemu_aio_release(acb
);
369 memset(acb
->task
, 0, sizeof(struct scsi_task
));
371 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
372 lba
= sector_qemu2lun(sector_num
, iscsilun
);
373 acb
->task
->expxferlen
= qemu_read_size
;
375 switch (iscsilun
->type
) {
377 acb
->task
->cdb_size
= 16;
378 acb
->task
->cdb
[0] = 0x88;
379 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
380 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
381 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
384 acb
->task
->cdb_size
= 10;
385 acb
->task
->cdb
[0] = 0x28;
386 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
387 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
391 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
395 scsi_free_scsi_task(acb
->task
);
396 qemu_aio_release(acb
);
400 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
401 scsi_task_add_data_in_buffer(acb
->task
,
402 acb
->qiov
->iov
[i
].iov_len
,
403 acb
->qiov
->iov
[i
].iov_base
);
406 iscsi_set_events(iscsilun
);
413 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
414 void *command_data
, void *opaque
)
416 IscsiAIOCB
*acb
= opaque
;
418 if (acb
->canceled
!= 0) {
419 qemu_aio_release(acb
);
420 scsi_free_scsi_task(acb
->task
);
427 error_report("Failed to sync10 data on iSCSI lun. %s",
428 iscsi_get_error(iscsi
));
432 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
433 scsi_free_scsi_task(acb
->task
);
437 static BlockDriverAIOCB
*
438 iscsi_aio_flush(BlockDriverState
*bs
,
439 BlockDriverCompletionFunc
*cb
, void *opaque
)
441 IscsiLun
*iscsilun
= bs
->opaque
;
442 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
445 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
447 acb
->iscsilun
= iscsilun
;
450 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
452 iscsi_synccache10_cb
,
454 if (acb
->task
== NULL
) {
455 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
456 iscsi_get_error(iscsi
));
457 qemu_aio_release(acb
);
461 iscsi_set_events(iscsilun
);
467 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
468 void *command_data
, void *opaque
)
470 IscsiAIOCB
*acb
= opaque
;
472 if (acb
->canceled
!= 0) {
473 qemu_aio_release(acb
);
474 scsi_free_scsi_task(acb
->task
);
481 error_report("Failed to unmap data on iSCSI lun. %s",
482 iscsi_get_error(iscsi
));
486 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
487 scsi_free_scsi_task(acb
->task
);
491 static BlockDriverAIOCB
*
492 iscsi_aio_discard(BlockDriverState
*bs
,
493 int64_t sector_num
, int nb_sectors
,
494 BlockDriverCompletionFunc
*cb
, void *opaque
)
496 IscsiLun
*iscsilun
= bs
->opaque
;
497 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
499 struct unmap_list list
[1];
501 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
503 acb
->iscsilun
= iscsilun
;
506 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
507 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
509 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
513 if (acb
->task
== NULL
) {
514 error_report("iSCSI: Failed to send unmap command. %s",
515 iscsi_get_error(iscsi
));
516 qemu_aio_release(acb
);
520 iscsi_set_events(iscsilun
);
527 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
528 void *command_data
, void *opaque
)
530 IscsiAIOCB
*acb
= opaque
;
532 if (acb
->canceled
!= 0) {
533 qemu_aio_release(acb
);
534 scsi_free_scsi_task(acb
->task
);
541 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
542 iscsi_get_error(iscsi
));
546 acb
->ioh
->driver_status
= 0;
547 acb
->ioh
->host_status
= 0;
550 #define SG_ERR_DRIVER_SENSE 0x08
552 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
555 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
557 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
558 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
559 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
560 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
563 iscsi_schedule_bh(iscsi_readv_writev_bh_cb
, acb
);
564 scsi_free_scsi_task(acb
->task
);
568 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
569 unsigned long int req
, void *buf
,
570 BlockDriverCompletionFunc
*cb
, void *opaque
)
572 IscsiLun
*iscsilun
= bs
->opaque
;
573 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
574 struct iscsi_data data
;
577 assert(req
== SG_IO
);
579 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
581 acb
->iscsilun
= iscsilun
;
586 acb
->task
= malloc(sizeof(struct scsi_task
));
587 if (acb
->task
== NULL
) {
588 error_report("iSCSI: Failed to allocate task for scsi command. %s",
589 iscsi_get_error(iscsi
));
590 qemu_aio_release(acb
);
593 memset(acb
->task
, 0, sizeof(struct scsi_task
));
595 switch (acb
->ioh
->dxfer_direction
) {
596 case SG_DXFER_TO_DEV
:
597 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
599 case SG_DXFER_FROM_DEV
:
600 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
603 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
607 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
608 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
609 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
611 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
612 data
.data
= acb
->ioh
->dxferp
;
613 data
.size
= acb
->ioh
->dxfer_len
;
615 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
617 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
620 scsi_free_scsi_task(acb
->task
);
621 qemu_aio_release(acb
);
625 /* tell libiscsi to read straight into the buffer we got from ioctl */
626 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
627 scsi_task_add_data_in_buffer(acb
->task
,
632 iscsi_set_events(iscsilun
);
637 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
639 IscsiLun
*iscsilun
= bs
->opaque
;
642 case SG_GET_VERSION_NUM
:
646 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
656 iscsi_getlength(BlockDriverState
*bs
)
658 IscsiLun
*iscsilun
= bs
->opaque
;
661 len
= iscsilun
->num_blocks
;
662 len
*= iscsilun
->block_size
;
668 iscsi_readcapacity16_cb(struct iscsi_context
*iscsi
, int status
,
669 void *command_data
, void *opaque
)
671 struct IscsiTask
*itask
= opaque
;
672 struct scsi_readcapacity16
*rc16
;
673 struct scsi_task
*task
= command_data
;
676 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
677 iscsi_get_error(iscsi
));
680 scsi_free_scsi_task(task
);
684 rc16
= scsi_datain_unmarshall(task
);
686 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
689 scsi_free_scsi_task(task
);
693 itask
->iscsilun
->block_size
= rc16
->block_length
;
694 itask
->iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
695 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
696 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
700 scsi_free_scsi_task(task
);
704 iscsi_readcapacity10_cb(struct iscsi_context
*iscsi
, int status
,
705 void *command_data
, void *opaque
)
707 struct IscsiTask
*itask
= opaque
;
708 struct scsi_readcapacity10
*rc10
;
709 struct scsi_task
*task
= command_data
;
712 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
713 iscsi_get_error(iscsi
));
716 scsi_free_scsi_task(task
);
720 rc10
= scsi_datain_unmarshall(task
);
722 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
725 scsi_free_scsi_task(task
);
729 itask
->iscsilun
->block_size
= rc10
->block_size
;
730 itask
->iscsilun
->num_blocks
= rc10
->lba
+ 1;
731 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
732 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
736 scsi_free_scsi_task(task
);
740 iscsi_inquiry_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
743 struct IscsiTask
*itask
= opaque
;
744 struct scsi_task
*task
= command_data
;
745 struct scsi_inquiry_standard
*inq
;
750 scsi_free_scsi_task(task
);
754 inq
= scsi_datain_unmarshall(task
);
756 error_report("iSCSI: Failed to unmarshall inquiry data.");
759 scsi_free_scsi_task(task
);
763 itask
->iscsilun
->type
= inq
->periperal_device_type
;
765 scsi_free_scsi_task(task
);
767 switch (itask
->iscsilun
->type
) {
769 task
= iscsi_readcapacity16_task(iscsi
, itask
->iscsilun
->lun
,
770 iscsi_readcapacity16_cb
, opaque
);
772 error_report("iSCSI: failed to send readcapacity16 command.");
779 task
= iscsi_readcapacity10_task(iscsi
, itask
->iscsilun
->lun
,
781 iscsi_readcapacity10_cb
, opaque
);
783 error_report("iSCSI: failed to send readcapacity16 command.");
796 iscsi_connect_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
799 struct IscsiTask
*itask
= opaque
;
800 struct scsi_task
*task
;
808 task
= iscsi_inquiry_task(iscsi
, itask
->iscsilun
->lun
,
810 iscsi_inquiry_cb
, opaque
);
812 error_report("iSCSI: failed to send inquiry command.");
819 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
823 const char *user
= NULL
;
824 const char *password
= NULL
;
826 list
= qemu_find_opts("iscsi");
831 opts
= qemu_opts_find(list
, target
);
833 opts
= QTAILQ_FIRST(&list
->head
);
839 user
= qemu_opt_get(opts
, "user");
844 password
= qemu_opt_get(opts
, "password");
846 error_report("CHAP username specified but no password was given");
850 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
851 error_report("Failed to set initiator username and password");
858 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
862 const char *digest
= NULL
;
864 list
= qemu_find_opts("iscsi");
869 opts
= qemu_opts_find(list
, target
);
871 opts
= QTAILQ_FIRST(&list
->head
);
877 digest
= qemu_opt_get(opts
, "header-digest");
882 if (!strcmp(digest
, "CRC32C")) {
883 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
884 } else if (!strcmp(digest
, "NONE")) {
885 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
886 } else if (!strcmp(digest
, "CRC32C-NONE")) {
887 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
888 } else if (!strcmp(digest
, "NONE-CRC32C")) {
889 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
891 error_report("Invalid header-digest setting : %s", digest
);
895 static char *parse_initiator_name(const char *target
)
899 const char *name
= NULL
;
901 list
= qemu_find_opts("iscsi");
903 return g_strdup("iqn.2008-11.org.linux-kvm");
906 opts
= qemu_opts_find(list
, target
);
908 opts
= QTAILQ_FIRST(&list
->head
);
910 return g_strdup("iqn.2008-11.org.linux-kvm");
914 name
= qemu_opt_get(opts
, "initiator-name");
916 return g_strdup("iqn.2008-11.org.linux-kvm");
919 return g_strdup(name
);
923 * We support iscsi url's on the form
924 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
926 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
928 IscsiLun
*iscsilun
= bs
->opaque
;
929 struct iscsi_context
*iscsi
= NULL
;
930 struct iscsi_url
*iscsi_url
= NULL
;
931 struct IscsiTask task
;
932 char *initiator_name
= NULL
;
935 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
936 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
937 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
938 "of 512", BDRV_SECTOR_SIZE
);
942 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
943 if (iscsi_url
== NULL
) {
944 error_report("Failed to parse URL : %s %s", filename
,
945 iscsi_get_error(iscsi
));
950 memset(iscsilun
, 0, sizeof(IscsiLun
));
952 initiator_name
= parse_initiator_name(iscsi_url
->target
);
954 iscsi
= iscsi_create_context(initiator_name
);
956 error_report("iSCSI: Failed to create iSCSI context.");
961 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
962 error_report("iSCSI: Failed to set target name.");
967 if (iscsi_url
->user
!= NULL
) {
968 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
971 error_report("Failed to set initiator username and password");
977 /* check if we got CHAP username/password via the options */
978 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
979 error_report("iSCSI: Failed to set CHAP user/password");
984 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
985 error_report("iSCSI: Failed to set session type to normal.");
990 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
992 /* check if we got HEADER_DIGEST via the options */
993 parse_header_digest(iscsi
, iscsi_url
->target
);
995 task
.iscsilun
= iscsilun
;
1000 iscsilun
->iscsi
= iscsi
;
1001 iscsilun
->lun
= iscsi_url
->lun
;
1003 if (iscsi_full_connect_async(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
,
1004 iscsi_connect_cb
, &task
)
1006 error_report("iSCSI: Failed to start async connect.");
1011 while (!task
.complete
) {
1012 iscsi_set_events(iscsilun
);
1015 if (task
.status
!= 0) {
1016 error_report("iSCSI: Failed to connect to LUN : %s",
1017 iscsi_get_error(iscsi
));
1022 if (iscsi_url
!= NULL
) {
1023 iscsi_destroy_url(iscsi_url
);
1026 /* Medium changer or tape. We dont have any emulation for this so this must
1027 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1028 * to read from the device to guess the image format.
1030 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1031 iscsilun
->type
== TYPE_TAPE
) {
1038 if (initiator_name
!= NULL
) {
1039 g_free(initiator_name
);
1041 if (iscsi_url
!= NULL
) {
1042 iscsi_destroy_url(iscsi_url
);
1044 if (iscsi
!= NULL
) {
1045 iscsi_destroy_context(iscsi
);
1047 memset(iscsilun
, 0, sizeof(IscsiLun
));
1051 static void iscsi_close(BlockDriverState
*bs
)
1053 IscsiLun
*iscsilun
= bs
->opaque
;
1054 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1056 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1057 iscsi_destroy_context(iscsi
);
1058 memset(iscsilun
, 0, sizeof(IscsiLun
));
1061 static BlockDriver bdrv_iscsi
= {
1062 .format_name
= "iscsi",
1063 .protocol_name
= "iscsi",
1065 .instance_size
= sizeof(IscsiLun
),
1066 .bdrv_file_open
= iscsi_open
,
1067 .bdrv_close
= iscsi_close
,
1069 .bdrv_getlength
= iscsi_getlength
,
1071 .bdrv_aio_readv
= iscsi_aio_readv
,
1072 .bdrv_aio_writev
= iscsi_aio_writev
,
1073 .bdrv_aio_flush
= iscsi_aio_flush
,
1075 .bdrv_aio_discard
= iscsi_aio_discard
,
1078 .bdrv_ioctl
= iscsi_ioctl
,
1079 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1083 static void iscsi_block_init(void)
1085 bdrv_register(&bdrv_iscsi
);
1088 block_init(iscsi_block_init
);