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 /* If we just added an event, the callback might be delayed
171 * unless we call qemu_notify_event().
173 if (ev
& ~iscsilun
->events
) {
176 iscsilun
->events
= ev
;
180 iscsi_process_read(void *arg
)
182 IscsiLun
*iscsilun
= arg
;
183 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
185 iscsi_service(iscsi
, POLLIN
);
186 iscsi_set_events(iscsilun
);
190 iscsi_process_write(void *arg
)
192 IscsiLun
*iscsilun
= arg
;
193 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
195 iscsi_service(iscsi
, POLLOUT
);
196 iscsi_set_events(iscsilun
);
201 iscsi_aio_write16_cb(struct iscsi_context
*iscsi
, int status
,
202 void *command_data
, void *opaque
)
204 IscsiAIOCB
*acb
= opaque
;
206 trace_iscsi_aio_write16_cb(iscsi
, status
, acb
, acb
->canceled
);
210 if (acb
->canceled
!= 0) {
216 error_report("Failed to write16 data to iSCSI lun. %s",
217 iscsi_get_error(iscsi
));
221 iscsi_schedule_bh(acb
);
224 static int64_t sector_qemu2lun(int64_t sector
, IscsiLun
*iscsilun
)
226 return sector
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
229 static BlockDriverAIOCB
*
230 iscsi_aio_writev(BlockDriverState
*bs
, int64_t sector_num
,
231 QEMUIOVector
*qiov
, int nb_sectors
,
232 BlockDriverCompletionFunc
*cb
,
235 IscsiLun
*iscsilun
= bs
->opaque
;
236 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
239 uint32_t num_sectors
;
241 struct iscsi_data data
;
243 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
244 trace_iscsi_aio_writev(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
246 acb
->iscsilun
= iscsilun
;
251 acb
->status
= -EINPROGRESS
;
253 /* XXX we should pass the iovec to write16 to avoid the extra copy */
254 /* this will allow us to get rid of 'buf' completely */
255 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
256 acb
->buf
= g_malloc(size
);
257 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->buf
, size
);
259 acb
->task
= malloc(sizeof(struct scsi_task
));
260 if (acb
->task
== NULL
) {
261 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
262 "command. %s", iscsi_get_error(iscsi
));
263 qemu_aio_release(acb
);
266 memset(acb
->task
, 0, sizeof(struct scsi_task
));
268 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
269 acb
->task
->cdb_size
= 16;
270 acb
->task
->cdb
[0] = 0x8a;
271 if (!(bs
->open_flags
& BDRV_O_CACHE_WB
)) {
272 /* set FUA on writes when cache mode is write through */
273 acb
->task
->cdb
[1] |= 0x04;
275 lba
= sector_qemu2lun(sector_num
, iscsilun
);
276 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
277 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
278 num_sectors
= size
/ iscsilun
->block_size
;
279 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
280 acb
->task
->expxferlen
= size
;
282 data
.data
= acb
->buf
;
285 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
286 iscsi_aio_write16_cb
,
289 scsi_free_scsi_task(acb
->task
);
291 qemu_aio_release(acb
);
295 iscsi_set_events(iscsilun
);
301 iscsi_aio_read16_cb(struct iscsi_context
*iscsi
, int status
,
302 void *command_data
, void *opaque
)
304 IscsiAIOCB
*acb
= opaque
;
306 trace_iscsi_aio_read16_cb(iscsi
, status
, acb
, acb
->canceled
);
308 if (acb
->canceled
!= 0) {
314 error_report("Failed to read16 data from iSCSI lun. %s",
315 iscsi_get_error(iscsi
));
319 iscsi_schedule_bh(acb
);
322 static BlockDriverAIOCB
*
323 iscsi_aio_readv(BlockDriverState
*bs
, int64_t sector_num
,
324 QEMUIOVector
*qiov
, int nb_sectors
,
325 BlockDriverCompletionFunc
*cb
,
328 IscsiLun
*iscsilun
= bs
->opaque
;
329 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
331 size_t qemu_read_size
;
334 uint32_t num_sectors
;
336 qemu_read_size
= BDRV_SECTOR_SIZE
* (size_t)nb_sectors
;
338 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
339 trace_iscsi_aio_readv(iscsi
, sector_num
, nb_sectors
, opaque
, acb
);
341 acb
->iscsilun
= iscsilun
;
346 acb
->status
= -EINPROGRESS
;
347 acb
->read_size
= qemu_read_size
;
350 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
351 * may be misaligned to the LUN, so we may need to read some extra
354 acb
->read_offset
= 0;
355 if (iscsilun
->block_size
> BDRV_SECTOR_SIZE
) {
356 uint64_t bdrv_offset
= BDRV_SECTOR_SIZE
* sector_num
;
358 acb
->read_offset
= bdrv_offset
% iscsilun
->block_size
;
361 num_sectors
= (qemu_read_size
+ iscsilun
->block_size
362 + acb
->read_offset
- 1)
363 / iscsilun
->block_size
;
365 acb
->task
= malloc(sizeof(struct scsi_task
));
366 if (acb
->task
== NULL
) {
367 error_report("iSCSI: Failed to allocate task for scsi READ16 "
368 "command. %s", iscsi_get_error(iscsi
));
369 qemu_aio_release(acb
);
372 memset(acb
->task
, 0, sizeof(struct scsi_task
));
374 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
375 lba
= sector_qemu2lun(sector_num
, iscsilun
);
376 acb
->task
->expxferlen
= qemu_read_size
;
378 switch (iscsilun
->type
) {
380 acb
->task
->cdb_size
= 16;
381 acb
->task
->cdb
[0] = 0x88;
382 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
>> 32);
383 *(uint32_t *)&acb
->task
->cdb
[6] = htonl(lba
& 0xffffffff);
384 *(uint32_t *)&acb
->task
->cdb
[10] = htonl(num_sectors
);
387 acb
->task
->cdb_size
= 10;
388 acb
->task
->cdb
[0] = 0x28;
389 *(uint32_t *)&acb
->task
->cdb
[2] = htonl(lba
);
390 *(uint16_t *)&acb
->task
->cdb
[7] = htons(num_sectors
);
394 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
398 scsi_free_scsi_task(acb
->task
);
399 qemu_aio_release(acb
);
403 for (i
= 0; i
< acb
->qiov
->niov
; i
++) {
404 scsi_task_add_data_in_buffer(acb
->task
,
405 acb
->qiov
->iov
[i
].iov_len
,
406 acb
->qiov
->iov
[i
].iov_base
);
409 iscsi_set_events(iscsilun
);
416 iscsi_synccache10_cb(struct iscsi_context
*iscsi
, int status
,
417 void *command_data
, void *opaque
)
419 IscsiAIOCB
*acb
= opaque
;
421 if (acb
->canceled
!= 0) {
427 error_report("Failed to sync10 data on iSCSI lun. %s",
428 iscsi_get_error(iscsi
));
432 iscsi_schedule_bh(acb
);
435 static BlockDriverAIOCB
*
436 iscsi_aio_flush(BlockDriverState
*bs
,
437 BlockDriverCompletionFunc
*cb
, void *opaque
)
439 IscsiLun
*iscsilun
= bs
->opaque
;
440 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
443 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
445 acb
->iscsilun
= iscsilun
;
448 acb
->status
= -EINPROGRESS
;
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) {
478 error_report("Failed to unmap data on iSCSI lun. %s",
479 iscsi_get_error(iscsi
));
483 iscsi_schedule_bh(acb
);
486 static BlockDriverAIOCB
*
487 iscsi_aio_discard(BlockDriverState
*bs
,
488 int64_t sector_num
, int nb_sectors
,
489 BlockDriverCompletionFunc
*cb
, void *opaque
)
491 IscsiLun
*iscsilun
= bs
->opaque
;
492 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
494 struct unmap_list list
[1];
496 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
498 acb
->iscsilun
= iscsilun
;
501 acb
->status
= -EINPROGRESS
;
503 list
[0].lba
= sector_qemu2lun(sector_num
, iscsilun
);
504 list
[0].num
= nb_sectors
* BDRV_SECTOR_SIZE
/ iscsilun
->block_size
;
506 acb
->task
= iscsi_unmap_task(iscsi
, iscsilun
->lun
,
510 if (acb
->task
== NULL
) {
511 error_report("iSCSI: Failed to send unmap command. %s",
512 iscsi_get_error(iscsi
));
513 qemu_aio_release(acb
);
517 iscsi_set_events(iscsilun
);
524 iscsi_aio_ioctl_cb(struct iscsi_context
*iscsi
, int status
,
525 void *command_data
, void *opaque
)
527 IscsiAIOCB
*acb
= opaque
;
529 if (acb
->canceled
!= 0) {
535 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
536 iscsi_get_error(iscsi
));
540 acb
->ioh
->driver_status
= 0;
541 acb
->ioh
->host_status
= 0;
544 #define SG_ERR_DRIVER_SENSE 0x08
546 if (status
== SCSI_STATUS_CHECK_CONDITION
&& acb
->task
->datain
.size
>= 2) {
549 acb
->ioh
->driver_status
|= SG_ERR_DRIVER_SENSE
;
551 acb
->ioh
->sb_len_wr
= acb
->task
->datain
.size
- 2;
552 ss
= (acb
->ioh
->mx_sb_len
>= acb
->ioh
->sb_len_wr
) ?
553 acb
->ioh
->mx_sb_len
: acb
->ioh
->sb_len_wr
;
554 memcpy(acb
->ioh
->sbp
, &acb
->task
->datain
.data
[2], ss
);
557 iscsi_schedule_bh(acb
);
560 static BlockDriverAIOCB
*iscsi_aio_ioctl(BlockDriverState
*bs
,
561 unsigned long int req
, void *buf
,
562 BlockDriverCompletionFunc
*cb
, void *opaque
)
564 IscsiLun
*iscsilun
= bs
->opaque
;
565 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
566 struct iscsi_data data
;
569 assert(req
== SG_IO
);
571 acb
= qemu_aio_get(&iscsi_aio_pool
, bs
, cb
, opaque
);
573 acb
->iscsilun
= iscsilun
;
576 acb
->status
= -EINPROGRESS
;
580 acb
->task
= malloc(sizeof(struct scsi_task
));
581 if (acb
->task
== NULL
) {
582 error_report("iSCSI: Failed to allocate task for scsi command. %s",
583 iscsi_get_error(iscsi
));
584 qemu_aio_release(acb
);
587 memset(acb
->task
, 0, sizeof(struct scsi_task
));
589 switch (acb
->ioh
->dxfer_direction
) {
590 case SG_DXFER_TO_DEV
:
591 acb
->task
->xfer_dir
= SCSI_XFER_WRITE
;
593 case SG_DXFER_FROM_DEV
:
594 acb
->task
->xfer_dir
= SCSI_XFER_READ
;
597 acb
->task
->xfer_dir
= SCSI_XFER_NONE
;
601 acb
->task
->cdb_size
= acb
->ioh
->cmd_len
;
602 memcpy(&acb
->task
->cdb
[0], acb
->ioh
->cmdp
, acb
->ioh
->cmd_len
);
603 acb
->task
->expxferlen
= acb
->ioh
->dxfer_len
;
605 if (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) {
606 data
.data
= acb
->ioh
->dxferp
;
607 data
.size
= acb
->ioh
->dxfer_len
;
609 if (iscsi_scsi_command_async(iscsi
, iscsilun
->lun
, acb
->task
,
611 (acb
->task
->xfer_dir
== SCSI_XFER_WRITE
) ?
614 scsi_free_scsi_task(acb
->task
);
615 qemu_aio_release(acb
);
619 /* tell libiscsi to read straight into the buffer we got from ioctl */
620 if (acb
->task
->xfer_dir
== SCSI_XFER_READ
) {
621 scsi_task_add_data_in_buffer(acb
->task
,
626 iscsi_set_events(iscsilun
);
632 static void ioctl_cb(void *opaque
, int status
)
634 int *p_status
= opaque
;
638 static int iscsi_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
640 IscsiLun
*iscsilun
= bs
->opaque
;
644 case SG_GET_VERSION_NUM
:
648 ((struct sg_scsi_id
*)buf
)->scsi_type
= iscsilun
->type
;
651 status
= -EINPROGRESS
;
652 iscsi_aio_ioctl(bs
, req
, buf
, ioctl_cb
, &status
);
654 while (status
== -EINPROGRESS
) {
667 iscsi_getlength(BlockDriverState
*bs
)
669 IscsiLun
*iscsilun
= bs
->opaque
;
672 len
= iscsilun
->num_blocks
;
673 len
*= iscsilun
->block_size
;
679 iscsi_readcapacity16_cb(struct iscsi_context
*iscsi
, int status
,
680 void *command_data
, void *opaque
)
682 struct IscsiTask
*itask
= opaque
;
683 struct scsi_readcapacity16
*rc16
;
684 struct scsi_task
*task
= command_data
;
687 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
688 iscsi_get_error(iscsi
));
691 scsi_free_scsi_task(task
);
695 rc16
= scsi_datain_unmarshall(task
);
697 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
700 scsi_free_scsi_task(task
);
704 itask
->iscsilun
->block_size
= rc16
->block_length
;
705 itask
->iscsilun
->num_blocks
= rc16
->returned_lba
+ 1;
706 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
707 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
711 scsi_free_scsi_task(task
);
715 iscsi_readcapacity10_cb(struct iscsi_context
*iscsi
, int status
,
716 void *command_data
, void *opaque
)
718 struct IscsiTask
*itask
= opaque
;
719 struct scsi_readcapacity10
*rc10
;
720 struct scsi_task
*task
= command_data
;
723 error_report("iSCSI: Failed to read capacity of iSCSI lun. %s",
724 iscsi_get_error(iscsi
));
727 scsi_free_scsi_task(task
);
731 rc10
= scsi_datain_unmarshall(task
);
733 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
736 scsi_free_scsi_task(task
);
740 itask
->iscsilun
->block_size
= rc10
->block_size
;
741 if (rc10
->lba
== 0) {
742 /* blank disk loaded */
743 itask
->iscsilun
->num_blocks
= 0;
745 itask
->iscsilun
->num_blocks
= rc10
->lba
+ 1;
747 itask
->bs
->total_sectors
= itask
->iscsilun
->num_blocks
*
748 itask
->iscsilun
->block_size
/ BDRV_SECTOR_SIZE
;
752 scsi_free_scsi_task(task
);
756 iscsi_inquiry_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
759 struct IscsiTask
*itask
= opaque
;
760 struct scsi_task
*task
= command_data
;
761 struct scsi_inquiry_standard
*inq
;
766 scsi_free_scsi_task(task
);
770 inq
= scsi_datain_unmarshall(task
);
772 error_report("iSCSI: Failed to unmarshall inquiry data.");
775 scsi_free_scsi_task(task
);
779 itask
->iscsilun
->type
= inq
->periperal_device_type
;
781 scsi_free_scsi_task(task
);
783 switch (itask
->iscsilun
->type
) {
785 task
= iscsi_readcapacity16_task(iscsi
, itask
->iscsilun
->lun
,
786 iscsi_readcapacity16_cb
, opaque
);
788 error_report("iSCSI: failed to send readcapacity16 command.");
795 task
= iscsi_readcapacity10_task(iscsi
, itask
->iscsilun
->lun
,
797 iscsi_readcapacity10_cb
, opaque
);
799 error_report("iSCSI: failed to send readcapacity16 command.");
812 iscsi_connect_cb(struct iscsi_context
*iscsi
, int status
, void *command_data
,
815 struct IscsiTask
*itask
= opaque
;
816 struct scsi_task
*task
;
824 task
= iscsi_inquiry_task(iscsi
, itask
->iscsilun
->lun
,
826 iscsi_inquiry_cb
, opaque
);
828 error_report("iSCSI: failed to send inquiry command.");
835 static int parse_chap(struct iscsi_context
*iscsi
, const char *target
)
839 const char *user
= NULL
;
840 const char *password
= NULL
;
842 list
= qemu_find_opts("iscsi");
847 opts
= qemu_opts_find(list
, target
);
849 opts
= QTAILQ_FIRST(&list
->head
);
855 user
= qemu_opt_get(opts
, "user");
860 password
= qemu_opt_get(opts
, "password");
862 error_report("CHAP username specified but no password was given");
866 if (iscsi_set_initiator_username_pwd(iscsi
, user
, password
)) {
867 error_report("Failed to set initiator username and password");
874 static void parse_header_digest(struct iscsi_context
*iscsi
, const char *target
)
878 const char *digest
= NULL
;
880 list
= qemu_find_opts("iscsi");
885 opts
= qemu_opts_find(list
, target
);
887 opts
= QTAILQ_FIRST(&list
->head
);
893 digest
= qemu_opt_get(opts
, "header-digest");
898 if (!strcmp(digest
, "CRC32C")) {
899 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C
);
900 } else if (!strcmp(digest
, "NONE")) {
901 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE
);
902 } else if (!strcmp(digest
, "CRC32C-NONE")) {
903 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_CRC32C_NONE
);
904 } else if (!strcmp(digest
, "NONE-CRC32C")) {
905 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
907 error_report("Invalid header-digest setting : %s", digest
);
911 static char *parse_initiator_name(const char *target
)
915 const char *name
= NULL
;
916 const char *iscsi_name
= qemu_get_vm_name();
918 list
= qemu_find_opts("iscsi");
920 opts
= qemu_opts_find(list
, target
);
922 opts
= QTAILQ_FIRST(&list
->head
);
925 name
= qemu_opt_get(opts
, "initiator-name");
930 return g_strdup(name
);
932 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
933 iscsi_name
? ":" : "",
934 iscsi_name
? iscsi_name
: "");
939 * We support iscsi url's on the form
940 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
942 static int iscsi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
944 IscsiLun
*iscsilun
= bs
->opaque
;
945 struct iscsi_context
*iscsi
= NULL
;
946 struct iscsi_url
*iscsi_url
= NULL
;
947 struct IscsiTask task
;
948 char *initiator_name
= NULL
;
951 if ((BDRV_SECTOR_SIZE
% 512) != 0) {
952 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
953 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
954 "of 512", BDRV_SECTOR_SIZE
);
958 iscsi_url
= iscsi_parse_full_url(iscsi
, filename
);
959 if (iscsi_url
== NULL
) {
960 error_report("Failed to parse URL : %s %s", filename
,
961 iscsi_get_error(iscsi
));
966 memset(iscsilun
, 0, sizeof(IscsiLun
));
968 initiator_name
= parse_initiator_name(iscsi_url
->target
);
970 iscsi
= iscsi_create_context(initiator_name
);
972 error_report("iSCSI: Failed to create iSCSI context.");
977 if (iscsi_set_targetname(iscsi
, iscsi_url
->target
)) {
978 error_report("iSCSI: Failed to set target name.");
983 if (iscsi_url
->user
!= NULL
) {
984 ret
= iscsi_set_initiator_username_pwd(iscsi
, iscsi_url
->user
,
987 error_report("Failed to set initiator username and password");
993 /* check if we got CHAP username/password via the options */
994 if (parse_chap(iscsi
, iscsi_url
->target
) != 0) {
995 error_report("iSCSI: Failed to set CHAP user/password");
1000 if (iscsi_set_session_type(iscsi
, ISCSI_SESSION_NORMAL
) != 0) {
1001 error_report("iSCSI: Failed to set session type to normal.");
1006 iscsi_set_header_digest(iscsi
, ISCSI_HEADER_DIGEST_NONE_CRC32C
);
1008 /* check if we got HEADER_DIGEST via the options */
1009 parse_header_digest(iscsi
, iscsi_url
->target
);
1011 task
.iscsilun
= iscsilun
;
1016 iscsilun
->iscsi
= iscsi
;
1017 iscsilun
->lun
= iscsi_url
->lun
;
1019 if (iscsi_full_connect_async(iscsi
, iscsi_url
->portal
, iscsi_url
->lun
,
1020 iscsi_connect_cb
, &task
)
1022 error_report("iSCSI: Failed to start async connect.");
1027 while (!task
.complete
) {
1028 iscsi_set_events(iscsilun
);
1031 if (task
.status
!= 0) {
1032 error_report("iSCSI: Failed to connect to LUN : %s",
1033 iscsi_get_error(iscsi
));
1038 /* Medium changer or tape. We dont have any emulation for this so this must
1039 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
1040 * to read from the device to guess the image format.
1042 if (iscsilun
->type
== TYPE_MEDIUM_CHANGER
||
1043 iscsilun
->type
== TYPE_TAPE
) {
1050 if (initiator_name
!= NULL
) {
1051 g_free(initiator_name
);
1053 if (iscsi_url
!= NULL
) {
1054 iscsi_destroy_url(iscsi_url
);
1058 if (iscsi
!= NULL
) {
1059 iscsi_destroy_context(iscsi
);
1061 memset(iscsilun
, 0, sizeof(IscsiLun
));
1066 static void iscsi_close(BlockDriverState
*bs
)
1068 IscsiLun
*iscsilun
= bs
->opaque
;
1069 struct iscsi_context
*iscsi
= iscsilun
->iscsi
;
1071 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi
), NULL
, NULL
, NULL
, NULL
);
1072 iscsi_destroy_context(iscsi
);
1073 memset(iscsilun
, 0, sizeof(IscsiLun
));
1076 static BlockDriver bdrv_iscsi
= {
1077 .format_name
= "iscsi",
1078 .protocol_name
= "iscsi",
1080 .instance_size
= sizeof(IscsiLun
),
1081 .bdrv_file_open
= iscsi_open
,
1082 .bdrv_close
= iscsi_close
,
1084 .bdrv_getlength
= iscsi_getlength
,
1086 .bdrv_aio_readv
= iscsi_aio_readv
,
1087 .bdrv_aio_writev
= iscsi_aio_writev
,
1088 .bdrv_aio_flush
= iscsi_aio_flush
,
1090 .bdrv_aio_discard
= iscsi_aio_discard
,
1093 .bdrv_ioctl
= iscsi_ioctl
,
1094 .bdrv_aio_ioctl
= iscsi_aio_ioctl
,
1098 static void iscsi_block_init(void)
1100 bdrv_register(&bdrv_iscsi
);
1103 block_init(iscsi_block_init
);