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
;
80 qemu_bh_delete(acb
->bh
);
82 if (acb
->canceled
== 0) {
83 acb
->common
.cb(acb
->common
.opaque
, acb
->status
);
86 if (acb
->task
!= NULL
) {
87 scsi_free_scsi_task(acb
->task
);
91 qemu_aio_release(acb
);
95 iscsi_schedule_bh(IscsiAIOCB
*acb
)
100 acb
->bh
= qemu_bh_new(iscsi_bh_cb
, acb
);
101 qemu_bh_schedule(acb
->bh
);
106 iscsi_abort_task_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
109 IscsiAIOCB
*acb
= private_data
;
111 acb
->status
= -ECANCELED
;
112 iscsi_schedule_bh(acb
);
116 iscsi_aio_cancel(BlockDriverAIOCB
*blockacb
)
118 IscsiAIOCB
*acb
= (IscsiAIOCB
*)blockacb
;
119 IscsiLun
*iscsilun
= acb
->iscsilun
;
121 if (acb
->status
!= -EINPROGRESS
) {
127 /* send a task mgmt call to the target to cancel the task on the target */
128 iscsi_task_mgmt_abort_task_async(iscsilun
->iscsi
, acb
->task
,
129 iscsi_abort_task_cb
, acb
);
131 while (acb
->status
== -EINPROGRESS
) {
136 static AIOPool iscsi_aio_pool
= {
137 .aiocb_size
= sizeof(IscsiAIOCB
),
138 .cancel
= iscsi_aio_cancel
,
142 static void iscsi_process_read(void *arg
);
143 static void iscsi_process_write(void *arg
);
145 static int iscsi_process_flush(void *arg
)
147 IscsiLun
*iscsilun
= arg
;
149 return iscsi_queue_length(iscsilun
->iscsi
) > 0;
153 iscsi_set_events(IscsiLun
*iscsilun
)
155 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
158 /* We always register a read handler. */
160 ev
|= iscsi_which_events(iscsi
);
161 if (ev
!= iscsilun
->events
) {
162 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
),
164 (ev
& POLLOUT
) ? iscsi_process_write
: NULL
,
170 iscsilun
->events
= ev
;
174 iscsi_process_read(void *arg
)
176 IscsiLun
*iscsilun
= arg
;
177 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
179 iscsi_service(iscsi
, POLLIN
);
180 iscsi_set_events(iscsilun
);
184 iscsi_process_write(void *arg
)
186 IscsiLun
*iscsilun
= arg
;
187 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
189 iscsi_service(iscsi
, POLLOUT
);
190 iscsi_set_events(iscsilun
);
195 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
196 void *command_data
, void *opaque
)
198 IscsiAIOCB
*acb
= opaque
;
200 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
204 if (acb
->canceled
!= 0) {
210 error_report("Failed to write16 data to iSCSI lun. %s",
211 iscsi_get_error(iscsi
));
215 iscsi_schedule_bh(acb
);
218 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
220 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
223 static BlockDriverAIOCB
*
224 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
225 QEMUIOVector
*qiov
, int nb_sectors
,
226 BlockDriverCompletionFunc
*cb
,
229 IscsiLun
*iscsilun
= bs
->opaque
;
230 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
233 uint32_t num_sectors
;
235 struct iscsi_data data
;
237 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
238 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
240 acb
->iscsilun
= iscsilun
;
245 acb
->status
= -EINPROGRESS
;
247 /* XXX we should pass the iovec to write16 to avoid the extra copy */
248 /* this will allow us to get rid of 'buf' completely */
249 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
250 acb
->buf
= g_malloc(size
);
251 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, size
);
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) {
308 error_report("Failed to read16 data from iSCSI lun. %s",
309 iscsi_get_error(iscsi
));
313 iscsi_schedule_bh(acb
);
316 static BlockDriverAIOCB
*
317 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
318 QEMUIOVector
*qiov
, int nb_sectors
,
319 BlockDriverCompletionFunc
*cb
,
322 IscsiLun
*iscsilun
= bs
->opaque
;
323 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
325 size_t qemu_read_size
;
328 uint32_t num_sectors
;
330 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
332 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
333 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
335 acb
->iscsilun
= iscsilun
;
340 acb
->status
= -EINPROGRESS
;
341 acb
->read_size
= qemu_read_size
;
344 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
345 * may be misaligned to the LUN, so we may need to read some extra
348 acb
->read_offset
= 0;
349 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
350 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
352 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
355 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
356 + acb
->read_offset
- 1)
357 / iscsilun
->block_size
;
359 acb
->task
= malloc(sizeof(struct scsi_task
));
360 if (acb
->task
== NULL
) {
361 error_report("iSCSI: Failed to allocate task for scsi READ16 "
362 "command. %s", iscsi_get_error(iscsi
));
363 qemu_aio_release(acb
);
366 memset(acb
->task
, 0, sizeof(struct scsi_task
));
368 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
369 lba
= sector_qemu2lun(sector_num
, iscsilun
);
370 acb
->task
->expxferlen
= qemu_read_size
;
372 switch (iscsilun
->type
) {
374 acb
->task
->cdb_size
= 16;
375 acb
->task
->cdb
[0] = 0x88;
376 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
377 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
378 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
381 acb
->task
->cdb_size
= 10;
382 acb
->task
->cdb
[0] = 0x28;
383 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
384 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
388 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
392 scsi_free_scsi_task(acb
->task
);
393 qemu_aio_release(acb
);
397 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
398 scsi_task_add_data_in_buffer(acb
->task
,
399 acb
->qiov
->iov
[i
].iov_len
,
400 acb
->qiov
->iov
[i
].iov_base
);
403 iscsi_set_events(iscsilun
);
410 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
411 void *command_data
, void *opaque
)
413 IscsiAIOCB
*acb
= opaque
;
415 if (acb
->canceled
!= 0) {
421 error_report("Failed to sync10 data on iSCSI lun. %s",
422 iscsi_get_error(iscsi
));
426 iscsi_schedule_bh(acb
);
429 static BlockDriverAIOCB
*
430 iscsi_aio_flush(BlockDriverState
*bs
,
431 BlockDriverCompletionFunc
*cb
, void *opaque
)
433 IscsiLun
*iscsilun
= bs
->opaque
;
434 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
437 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
439 acb
->iscsilun
= iscsilun
;
442 acb
->status
= -EINPROGRESS
;
444 acb
->task
= iscsi_synchronizecache10_task(iscsi
, iscsilun
->lun
,
446 iscsi_synccache10_cb
,
448 if (acb
->task
== NULL
) {
449 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
450 iscsi_get_error(iscsi
));
451 qemu_aio_release(acb
);
455 iscsi_set_events(iscsilun
);
461 iscsi_unmap_cb(struct iscsi_context
*iscsi
, int status
,
462 void *command_data
, void *opaque
)
464 IscsiAIOCB
*acb
= opaque
;
466 if (acb
->canceled
!= 0) {
472 error_report("Failed to unmap data on iSCSI lun. %s",
473 iscsi_get_error(iscsi
));
477 iscsi_schedule_bh(acb
);
480 static BlockDriverAIOCB
*
481 iscsi_aio_discard(BlockDriverState
*bs
,
482 int64_t sector_num
, int nb_sectors
,
483 BlockDriverCompletionFunc
*cb
, void *opaque
)
485 IscsiLun
*iscsilun
= bs
->opaque
;
486 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
488 struct unmap_list list
[1];
490 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
492 acb
->iscsilun
= iscsilun
;
495 acb
->status
= -EINPROGRESS
;
497 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
498 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
500 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
504 if (acb
->task
== NULL
) {
505 error_report("iSCSI: Failed to send unmap command. %s",
506 iscsi_get_error(iscsi
));
507 qemu_aio_release(acb
);
511 iscsi_set_events(iscsilun
);
518 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
519 void *command_data
, void *opaque
)
521 IscsiAIOCB
*acb
= opaque
;
523 if (acb
->canceled
!= 0) {
529 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
530 iscsi_get_error(iscsi
));
534 acb
->ioh
->driver_status
= 0;
535 acb
->ioh
->host_status
= 0;
538 #define SG_ERR_DRIVER_SENSE 0x08
540 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
543 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
545 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
546 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
547 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
548 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
551 iscsi_schedule_bh(acb
);
554 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
555 unsigned long int req
, void *buf
,
556 BlockDriverCompletionFunc
*cb
, void *opaque
)
558 IscsiLun
*iscsilun
= bs
->opaque
;
559 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
560 struct iscsi_data data
;
563 assert(req
== SG_IO
);
565 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
567 acb
->iscsilun
= iscsilun
;
570 acb
->status
= -EINPROGRESS
;
574 acb
->task
= malloc(sizeof(struct scsi_task
));
575 if (acb
->task
== NULL
) {
576 error_report("iSCSI: Failed to allocate task for scsi command. %s",
577 iscsi_get_error(iscsi
));
578 qemu_aio_release(acb
);
581 memset(acb
->task
, 0, sizeof(struct scsi_task
));
583 switch (acb
->ioh
->dxfer_direction
) {
584 case SG_DXFER_TO_DEV
:
585 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
587 case SG_DXFER_FROM_DEV
:
588 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
591 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
595 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
596 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
597 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
599 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
600 data
.data
= acb
->ioh
->dxferp
;
601 data
.size
= acb
->ioh
->dxfer_len
;
603 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
605 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
608 scsi_free_scsi_task(acb
->task
);
609 qemu_aio_release(acb
);
613 /* tell libiscsi to read straight into the buffer we got from ioctl */
614 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
615 scsi_task_add_data_in_buffer(acb
->task
,
620 iscsi_set_events(iscsilun
);
626 static void ioctl_cb(void *opaque
, int status
)
628 int *p_status
= opaque
;
632 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
634 IscsiLun
*iscsilun
= bs
->opaque
;
638 case SG_GET_VERSION_NUM
:
642 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
645 status
= -EINPROGRESS
;
646 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
648 while (status
== -EINPROGRESS
) {
661 iscsi_getlength(BlockDriverState
*bs
)
663 IscsiLun
*iscsilun
= bs
->opaque
;
666 len
= iscsilun
->num_blocks
;
667 len
*= iscsilun
->block_size
;
673 iscsi_readcapacity16_cb(struct iscsi_context
*iscsi
, int status
,
674 void *command_data
, void *opaque
)
676 struct IscsiTask
*itask
= opaque
;
677 struct scsi_readcapacity16
*rc16
;
678 struct scsi_task
*task
= command_data
;
681 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
682 iscsi_get_error(iscsi
));
685 scsi_free_scsi_task(task
);
689 rc16
= scsi_datain_unmarshall(task
);
691 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
694 scsi_free_scsi_task(task
);
698 itask
->iscsilun
->block_size
= rc16
->block_length
;
699 itask
->iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
700 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
701 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
705 scsi_free_scsi_task(task
);
709 iscsi_readcapacity10_cb(struct iscsi_context
*iscsi
, int status
,
710 void *command_data
, void *opaque
)
712 struct IscsiTask
*itask
= opaque
;
713 struct scsi_readcapacity10
*rc10
;
714 struct scsi_task
*task
= command_data
;
717 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
718 iscsi_get_error(iscsi
));
721 scsi_free_scsi_task(task
);
725 rc10
= scsi_datain_unmarshall(task
);
727 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
730 scsi_free_scsi_task(task
);
734 itask
->iscsilun
->block_size
= rc10
->block_size
;
735 if (rc10
->lba
== 0) {
736 /* blank disk loaded */
737 itask
->iscsilun
->num_blocks
= 0;
739 itask
->iscsilun
->num_blocks
= rc10
->lba
+ 1;
741 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
742 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
746 scsi_free_scsi_task(task
);
750 iscsi_inquiry_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
753 struct IscsiTask
*itask
= opaque
;
754 struct scsi_task
*task
= command_data
;
755 struct scsi_inquiry_standard
*inq
;
760 scsi_free_scsi_task(task
);
764 inq
= scsi_datain_unmarshall(task
);
766 error_report("iSCSI: Failed to unmarshall inquiry data.");
769 scsi_free_scsi_task(task
);
773 itask
->iscsilun
->type
= inq
->periperal_device_type
;
775 scsi_free_scsi_task(task
);
777 switch (itask
->iscsilun
->type
) {
779 task
= iscsi_readcapacity16_task(iscsi
, itask
->iscsilun
->lun
,
780 iscsi_readcapacity16_cb
, opaque
);
782 error_report("iSCSI: failed to send readcapacity16 command.");
789 task
= iscsi_readcapacity10_task(iscsi
, itask
->iscsilun
->lun
,
791 iscsi_readcapacity10_cb
, opaque
);
793 error_report("iSCSI: failed to send readcapacity16 command.");
806 iscsi_connect_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
809 struct IscsiTask
*itask
= opaque
;
810 struct scsi_task
*task
;
818 task
= iscsi_inquiry_task(iscsi
, itask
->iscsilun
->lun
,
820 iscsi_inquiry_cb
, opaque
);
822 error_report("iSCSI: failed to send inquiry command.");
829 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
833 const char *user
= NULL
;
834 const char *password
= NULL
;
836 list
= qemu_find_opts("iscsi");
841 opts
= qemu_opts_find(list
, target
);
843 opts
= QTAILQ_FIRST(&list
->head
);
849 user
= qemu_opt_get(opts
, "user");
854 password
= qemu_opt_get(opts
, "password");
856 error_report("CHAP username specified but no password was given");
860 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
861 error_report("Failed to set initiator username and password");
868 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
872 const char *digest
= NULL
;
874 list
= qemu_find_opts("iscsi");
879 opts
= qemu_opts_find(list
, target
);
881 opts
= QTAILQ_FIRST(&list
->head
);
887 digest
= qemu_opt_get(opts
, "header-digest");
892 if (!strcmp(digest
, "CRC32C")) {
893 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
894 } else if (!strcmp(digest
, "NONE")) {
895 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
896 } else if (!strcmp(digest
, "CRC32C-NONE")) {
897 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
898 } else if (!strcmp(digest
, "NONE-CRC32C")) {
899 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
901 error_report("Invalid header-digest setting : %s", digest
);
905 static char *parse_initiator_name(const char *target
)
909 const char *name
= NULL
;
910 const char *iscsi_name
= qemu_get_vm_name();
912 list
= qemu_find_opts("iscsi");
914 opts
= qemu_opts_find(list
, target
);
916 opts
= QTAILQ_FIRST(&list
->head
);
919 name
= qemu_opt_get(opts
, "initiator-name");
924 return g_strdup(name
);
926 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
927 iscsi_name
? ":" : "",
928 iscsi_name
? iscsi_name
: "");
933 * We support iscsi url's on the form
934 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
936 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
938 IscsiLun
*iscsilun
= bs
->opaque
;
939 struct iscsi_context
*iscsi
= NULL
;
940 struct iscsi_url
*iscsi_url
= NULL
;
941 struct IscsiTask task
;
942 char *initiator_name
= NULL
;
945 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
946 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
947 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
948 "of 512", BDRV_SECTOR_SIZE
);
952 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
953 if (iscsi_url
== NULL
) {
954 error_report("Failed to parse URL : %s %s", filename
,
955 iscsi_get_error(iscsi
));
960 memset(iscsilun
, 0, sizeof(IscsiLun
));
962 initiator_name
= parse_initiator_name(iscsi_url
->target
);
964 iscsi
= iscsi_create_context(initiator_name
);
966 error_report("iSCSI: Failed to create iSCSI context.");
971 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
972 error_report("iSCSI: Failed to set target name.");
977 if (iscsi_url
->user
!= NULL
) {
978 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
981 error_report("Failed to set initiator username and password");
987 /* check if we got CHAP username/password via the options */
988 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
989 error_report("iSCSI: Failed to set CHAP user/password");
994 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
995 error_report("iSCSI: Failed to set session type to normal.");
1000 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1002 /* check if we got HEADER_DIGEST via the options */
1003 parse_header_digest(iscsi
, iscsi_url
->target
);
1005 task
.iscsilun
= iscsilun
;
1010 iscsilun
->iscsi
= iscsi
;
1011 iscsilun
->lun
= iscsi_url
->lun
;
1013 if (iscsi_full_connect_async(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
,
1014 iscsi_connect_cb
, &task
)
1016 error_report("iSCSI: Failed to start async connect.");
1021 while (!task
.complete
) {
1022 iscsi_set_events(iscsilun
);
1025 if (task
.status
!= 0) {
1026 error_report("iSCSI: Failed to connect to LUN : %s",
1027 iscsi_get_error(iscsi
));
1032 /* Medium changer or tape. We dont have any emulation for this so this must
1033 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1034 * to read from the device to guess the image format.
1036 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1037 iscsilun
->type
== TYPE_TAPE
) {
1044 if (initiator_name
!= NULL
) {
1045 g_free(initiator_name
);
1047 if (iscsi_url
!= NULL
) {
1048 iscsi_destroy_url(iscsi_url
);
1052 if (iscsi
!= NULL
) {
1053 iscsi_destroy_context(iscsi
);
1055 memset(iscsilun
, 0, sizeof(IscsiLun
));
1060 static void iscsi_close(BlockDriverState
*bs
)
1062 IscsiLun
*iscsilun
= bs
->opaque
;
1063 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1065 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1066 iscsi_destroy_context(iscsi
);
1067 memset(iscsilun
, 0, sizeof(IscsiLun
));
1070 static BlockDriver bdrv_iscsi
= {
1071 .format_name
= "iscsi",
1072 .protocol_name
= "iscsi",
1074 .instance_size
= sizeof(IscsiLun
),
1075 .bdrv_file_open
= iscsi_open
,
1076 .bdrv_close
= iscsi_close
,
1078 .bdrv_getlength
= iscsi_getlength
,
1080 .bdrv_aio_readv
= iscsi_aio_readv
,
1081 .bdrv_aio_writev
= iscsi_aio_writev
,
1082 .bdrv_aio_flush
= iscsi_aio_flush
,
1084 .bdrv_aio_discard
= iscsi_aio_discard
,
1087 .bdrv_ioctl
= iscsi_ioctl
,
1088 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1092 static void iscsi_block_init(void)
1094 bdrv_register(&bdrv_iscsi
);
1097 block_init(iscsi_block_init
);