]> git.proxmox.com Git - qemu.git/blame - block/iscsi.c
iscsi: add support for iovectors
[qemu.git] / block / iscsi.c
CommitLineData
c589b249
RS
1/*
2 * QEMU Block driver for iSCSI images
3 *
4 * Copyright (c) 2010-2011 Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
25#include "config-host.h"
26
27#include <poll.h>
f4dfa67f 28#include <arpa/inet.h>
c589b249 29#include "qemu-common.h"
1de7afc9
PB
30#include "qemu/config-file.h"
31#include "qemu/error-report.h"
737e150e 32#include "block/block_int.h"
c589b249 33#include "trace.h"
dbfff6d7 34#include "hw/scsi-defs.h"
c589b249
RS
35
36#include <iscsi/iscsi.h>
37#include <iscsi/scsi-lowlevel.h>
38
98392453
RS
39#ifdef __linux__
40#include <scsi/sg.h>
41#include <hw/scsi-defs.h>
42#endif
c589b249
RS
43
44typedef struct IscsiLun {
45 struct iscsi_context *iscsi;
46 int lun;
dbfff6d7 47 enum scsi_inquiry_peripheral_device_type type;
c589b249 48 int block_size;
c7b4a952 49 uint64_t num_blocks;
c9b9f682 50 int events;
5b5d34ec 51 QEMUTimer *nop_timer;
c589b249
RS
52} IscsiLun;
53
54typedef struct IscsiAIOCB {
55 BlockDriverAIOCB common;
56 QEMUIOVector *qiov;
57 QEMUBH *bh;
58 IscsiLun *iscsilun;
59 struct scsi_task *task;
60 uint8_t *buf;
61 int status;
62 int canceled;
63 size_t read_size;
64 size_t read_offset;
98392453
RS
65#ifdef __linux__
66 sg_io_hdr_t *ioh;
67#endif
c589b249
RS
68} IscsiAIOCB;
69
5b5d34ec
PL
70#define NOP_INTERVAL 5000
71#define MAX_NOP_FAILURES 3
72
27cbd828 73static void
cfb3f506 74iscsi_bh_cb(void *p)
27cbd828
PB
75{
76 IscsiAIOCB *acb = p;
77
78 qemu_bh_delete(acb->bh);
79
4790b03d
PB
80 g_free(acb->buf);
81 acb->buf = NULL;
82
27cbd828
PB
83 if (acb->canceled == 0) {
84 acb->common.cb(acb->common.opaque, acb->status);
85 }
86
1bd075f2
PB
87 if (acb->task != NULL) {
88 scsi_free_scsi_task(acb->task);
89 acb->task = NULL;
90 }
91
27cbd828
PB
92 qemu_aio_release(acb);
93}
94
cfb3f506
PB
95static void
96iscsi_schedule_bh(IscsiAIOCB *acb)
27cbd828 97{
1bd075f2
PB
98 if (acb->bh) {
99 return;
100 }
cfb3f506 101 acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
27cbd828 102 qemu_bh_schedule(acb->bh);
27cbd828
PB
103}
104
105
c589b249
RS
106static void
107iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
108 void *private_data)
109{
1bd075f2
PB
110 IscsiAIOCB *acb = private_data;
111
112 acb->status = -ECANCELED;
113 iscsi_schedule_bh(acb);
c589b249
RS
114}
115
116static void
117iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
118{
119 IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
120 IscsiLun *iscsilun = acb->iscsilun;
121
1bd075f2
PB
122 if (acb->status != -EINPROGRESS) {
123 return;
124 }
125
b2090919 126 acb->canceled = 1;
c589b249 127
b2090919 128 /* send a task mgmt call to the target to cancel the task on the target */
64e69e80 129 iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
1bd075f2 130 iscsi_abort_task_cb, acb);
b2090919 131
1bd075f2
PB
132 while (acb->status == -EINPROGRESS) {
133 qemu_aio_wait();
134 }
c589b249
RS
135}
136
d7331bed 137static const AIOCBInfo iscsi_aiocb_info = {
c589b249
RS
138 .aiocb_size = sizeof(IscsiAIOCB),
139 .cancel = iscsi_aio_cancel,
140};
141
142
143static void iscsi_process_read(void *arg);
144static void iscsi_process_write(void *arg);
145
146static int iscsi_process_flush(void *arg)
147{
148 IscsiLun *iscsilun = arg;
149
150 return iscsi_queue_length(iscsilun->iscsi) > 0;
151}
152
153static void
154iscsi_set_events(IscsiLun *iscsilun)
155{
156 struct iscsi_context *iscsi = iscsilun->iscsi;
c9b9f682
RS
157 int ev;
158
159 /* We always register a read handler. */
160 ev = POLLIN;
161 ev |= iscsi_which_events(iscsi);
162 if (ev != iscsilun->events) {
163 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi),
164 iscsi_process_read,
165 (ev & POLLOUT) ? iscsi_process_write : NULL,
166 iscsi_process_flush,
167 iscsilun);
168
169 }
170
c9b9f682 171 iscsilun->events = ev;
c589b249
RS
172}
173
174static void
175iscsi_process_read(void *arg)
176{
177 IscsiLun *iscsilun = arg;
178 struct iscsi_context *iscsi = iscsilun->iscsi;
179
180 iscsi_service(iscsi, POLLIN);
181 iscsi_set_events(iscsilun);
182}
183
184static void
185iscsi_process_write(void *arg)
186{
187 IscsiLun *iscsilun = arg;
188 struct iscsi_context *iscsi = iscsilun->iscsi;
189
190 iscsi_service(iscsi, POLLOUT);
191 iscsi_set_events(iscsilun);
192}
193
194
c589b249 195static void
f4dfa67f 196iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
c589b249
RS
197 void *command_data, void *opaque)
198{
199 IscsiAIOCB *acb = opaque;
200
f4dfa67f 201 trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
c589b249
RS
202
203 g_free(acb->buf);
4790b03d 204 acb->buf = NULL;
c589b249 205
b2090919 206 if (acb->canceled != 0) {
c589b249
RS
207 return;
208 }
209
210 acb->status = 0;
211 if (status < 0) {
f4dfa67f 212 error_report("Failed to write16 data to iSCSI lun. %s",
c589b249
RS
213 iscsi_get_error(iscsi));
214 acb->status = -EIO;
215 }
216
cfb3f506 217 iscsi_schedule_bh(acb);
c589b249
RS
218}
219
220static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
221{
222 return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
223}
224
225static BlockDriverAIOCB *
226iscsi_aio_writev(BlockDriverState *bs, int64_t sector_num,
227 QEMUIOVector *qiov, int nb_sectors,
228 BlockDriverCompletionFunc *cb,
229 void *opaque)
230{
231 IscsiLun *iscsilun = bs->opaque;
232 struct iscsi_context *iscsi = iscsilun->iscsi;
233 IscsiAIOCB *acb;
234 size_t size;
f4dfa67f
RS
235 uint32_t num_sectors;
236 uint64_t lba;
7371d56f 237#if !defined(LIBISCSI_FEATURE_IOVECTOR)
f4dfa67f 238 struct iscsi_data data;
7371d56f
PL
239#endif
240 int ret;
c589b249 241
d7331bed 242 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
c589b249
RS
243 trace_iscsi_aio_writev(iscsi, sector_num, nb_sectors, opaque, acb);
244
245 acb->iscsilun = iscsilun;
246 acb->qiov = qiov;
247
248 acb->canceled = 0;
1bd075f2
PB
249 acb->bh = NULL;
250 acb->status = -EINPROGRESS;
4790b03d 251 acb->buf = NULL;
c589b249 252
c589b249
RS
253 /* this will allow us to get rid of 'buf' completely */
254 size = nb_sectors * BDRV_SECTOR_SIZE;
7371d56f
PL
255
256#if !defined(LIBISCSI_FEATURE_IOVECTOR)
4cc841b5
PL
257 data.size = MIN(size, acb->qiov->size);
258
259 /* if the iovec only contains one buffer we can pass it directly */
260 if (acb->qiov->niov == 1) {
4cc841b5
PL
261 data.data = acb->qiov->iov[0].iov_base;
262 } else {
263 acb->buf = g_malloc(data.size);
264 qemu_iovec_to_buf(acb->qiov, 0, acb->buf, data.size);
265 data.data = acb->buf;
266 }
7371d56f 267#endif
f4dfa67f
RS
268
269 acb->task = malloc(sizeof(struct scsi_task));
c589b249 270 if (acb->task == NULL) {
f4dfa67f
RS
271 error_report("iSCSI: Failed to allocate task for scsi WRITE16 "
272 "command. %s", iscsi_get_error(iscsi));
273 qemu_aio_release(acb);
274 return NULL;
275 }
276 memset(acb->task, 0, sizeof(struct scsi_task));
277
278 acb->task->xfer_dir = SCSI_XFER_WRITE;
279 acb->task->cdb_size = 16;
280 acb->task->cdb[0] = 0x8a;
f4dfa67f
RS
281 lba = sector_qemu2lun(sector_num, iscsilun);
282 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
283 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
284 num_sectors = size / iscsilun->block_size;
285 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
286 acb->task->expxferlen = size;
287
7371d56f
PL
288#if defined(LIBISCSI_FEATURE_IOVECTOR)
289 ret = iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
290 iscsi_aio_write16_cb,
291 NULL,
292 acb);
293#else
294 ret = iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
295 iscsi_aio_write16_cb,
296 &data,
297 acb);
298#endif
299 if (ret != 0) {
f4dfa67f 300 scsi_free_scsi_task(acb->task);
c589b249
RS
301 g_free(acb->buf);
302 qemu_aio_release(acb);
303 return NULL;
304 }
305
7371d56f
PL
306#if defined(LIBISCSI_FEATURE_IOVECTOR)
307 scsi_task_set_iov_out(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
308#endif
309
c589b249
RS
310 iscsi_set_events(iscsilun);
311
312 return &acb->common;
313}
314
315static void
f4dfa67f 316iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
c589b249
RS
317 void *command_data, void *opaque)
318{
319 IscsiAIOCB *acb = opaque;
320
f4dfa67f 321 trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
c589b249 322
b2090919 323 if (acb->canceled != 0) {
c589b249
RS
324 return;
325 }
326
327 acb->status = 0;
328 if (status != 0) {
f4dfa67f 329 error_report("Failed to read16 data from iSCSI lun. %s",
c589b249
RS
330 iscsi_get_error(iscsi));
331 acb->status = -EIO;
332 }
333
cfb3f506 334 iscsi_schedule_bh(acb);
c589b249
RS
335}
336
337static BlockDriverAIOCB *
338iscsi_aio_readv(BlockDriverState *bs, int64_t sector_num,
339 QEMUIOVector *qiov, int nb_sectors,
340 BlockDriverCompletionFunc *cb,
341 void *opaque)
342{
343 IscsiLun *iscsilun = bs->opaque;
344 struct iscsi_context *iscsi = iscsilun->iscsi;
345 IscsiAIOCB *acb;
f4dfa67f 346 size_t qemu_read_size;
7371d56f 347#if !defined(LIBISCSI_FEATURE_IOVECTOR)
c589b249 348 int i;
7371d56f
PL
349#endif
350 int ret;
f4dfa67f
RS
351 uint64_t lba;
352 uint32_t num_sectors;
c589b249
RS
353
354 qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
355
d7331bed 356 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
c589b249
RS
357 trace_iscsi_aio_readv(iscsi, sector_num, nb_sectors, opaque, acb);
358
359 acb->iscsilun = iscsilun;
360 acb->qiov = qiov;
361
362 acb->canceled = 0;
1bd075f2
PB
363 acb->bh = NULL;
364 acb->status = -EINPROGRESS;
c589b249
RS
365 acb->read_size = qemu_read_size;
366 acb->buf = NULL;
367
368 /* If LUN blocksize is bigger than BDRV_BLOCK_SIZE a read from QEMU
369 * may be misaligned to the LUN, so we may need to read some extra
370 * data.
371 */
372 acb->read_offset = 0;
373 if (iscsilun->block_size > BDRV_SECTOR_SIZE) {
374 uint64_t bdrv_offset = BDRV_SECTOR_SIZE * sector_num;
375
376 acb->read_offset = bdrv_offset % iscsilun->block_size;
377 }
378
f4dfa67f
RS
379 num_sectors = (qemu_read_size + iscsilun->block_size
380 + acb->read_offset - 1)
381 / iscsilun->block_size;
382
383 acb->task = malloc(sizeof(struct scsi_task));
c589b249 384 if (acb->task == NULL) {
f4dfa67f
RS
385 error_report("iSCSI: Failed to allocate task for scsi READ16 "
386 "command. %s", iscsi_get_error(iscsi));
387 qemu_aio_release(acb);
388 return NULL;
389 }
390 memset(acb->task, 0, sizeof(struct scsi_task));
391
392 acb->task->xfer_dir = SCSI_XFER_READ;
393 lba = sector_qemu2lun(sector_num, iscsilun);
394 acb->task->expxferlen = qemu_read_size;
395
396 switch (iscsilun->type) {
397 case TYPE_DISK:
398 acb->task->cdb_size = 16;
399 acb->task->cdb[0] = 0x88;
400 *(uint32_t *)&acb->task->cdb[2] = htonl(lba >> 32);
401 *(uint32_t *)&acb->task->cdb[6] = htonl(lba & 0xffffffff);
402 *(uint32_t *)&acb->task->cdb[10] = htonl(num_sectors);
403 break;
404 default:
405 acb->task->cdb_size = 10;
406 acb->task->cdb[0] = 0x28;
407 *(uint32_t *)&acb->task->cdb[2] = htonl(lba);
408 *(uint16_t *)&acb->task->cdb[7] = htons(num_sectors);
409 break;
410 }
e829b0bb 411
7371d56f
PL
412 ret = iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
413 iscsi_aio_read16_cb,
414 NULL,
415 acb);
416 if (ret != 0) {
f4dfa67f 417 scsi_free_scsi_task(acb->task);
c589b249
RS
418 qemu_aio_release(acb);
419 return NULL;
420 }
421
7371d56f
PL
422#if defined(LIBISCSI_FEATURE_IOVECTOR)
423 scsi_task_set_iov_in(acb->task, (struct scsi_iovec*) acb->qiov->iov, acb->qiov->niov);
424#else
c589b249
RS
425 for (i = 0; i < acb->qiov->niov; i++) {
426 scsi_task_add_data_in_buffer(acb->task,
427 acb->qiov->iov[i].iov_len,
428 acb->qiov->iov[i].iov_base);
429 }
7371d56f 430#endif
c589b249
RS
431
432 iscsi_set_events(iscsilun);
433
434 return &acb->common;
435}
436
437
438static void
439iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
440 void *command_data, void *opaque)
441{
442 IscsiAIOCB *acb = opaque;
443
b2090919 444 if (acb->canceled != 0) {
c589b249
RS
445 return;
446 }
447
448 acb->status = 0;
449 if (status < 0) {
450 error_report("Failed to sync10 data on iSCSI lun. %s",
451 iscsi_get_error(iscsi));
452 acb->status = -EIO;
453 }
454
cfb3f506 455 iscsi_schedule_bh(acb);
c589b249
RS
456}
457
458static BlockDriverAIOCB *
459iscsi_aio_flush(BlockDriverState *bs,
460 BlockDriverCompletionFunc *cb, void *opaque)
461{
462 IscsiLun *iscsilun = bs->opaque;
463 struct iscsi_context *iscsi = iscsilun->iscsi;
464 IscsiAIOCB *acb;
465
d7331bed 466 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
c589b249
RS
467
468 acb->iscsilun = iscsilun;
469 acb->canceled = 0;
1bd075f2
PB
470 acb->bh = NULL;
471 acb->status = -EINPROGRESS;
4790b03d 472 acb->buf = NULL;
c589b249
RS
473
474 acb->task = iscsi_synchronizecache10_task(iscsi, iscsilun->lun,
475 0, 0, 0, 0,
476 iscsi_synccache10_cb,
477 acb);
478 if (acb->task == NULL) {
479 error_report("iSCSI: Failed to send synchronizecache10 command. %s",
480 iscsi_get_error(iscsi));
481 qemu_aio_release(acb);
482 return NULL;
483 }
484
485 iscsi_set_events(iscsilun);
486
487 return &acb->common;
488}
489
fa6acb0c
RS
490static void
491iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
492 void *command_data, void *opaque)
493{
494 IscsiAIOCB *acb = opaque;
495
b2090919 496 if (acb->canceled != 0) {
fa6acb0c
RS
497 return;
498 }
499
500 acb->status = 0;
501 if (status < 0) {
502 error_report("Failed to unmap data on iSCSI lun. %s",
503 iscsi_get_error(iscsi));
504 acb->status = -EIO;
505 }
506
cfb3f506 507 iscsi_schedule_bh(acb);
fa6acb0c
RS
508}
509
510static BlockDriverAIOCB *
511iscsi_aio_discard(BlockDriverState *bs,
512 int64_t sector_num, int nb_sectors,
513 BlockDriverCompletionFunc *cb, void *opaque)
514{
515 IscsiLun *iscsilun = bs->opaque;
516 struct iscsi_context *iscsi = iscsilun->iscsi;
517 IscsiAIOCB *acb;
518 struct unmap_list list[1];
519
d7331bed 520 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
fa6acb0c
RS
521
522 acb->iscsilun = iscsilun;
523 acb->canceled = 0;
1bd075f2
PB
524 acb->bh = NULL;
525 acb->status = -EINPROGRESS;
4790b03d 526 acb->buf = NULL;
fa6acb0c
RS
527
528 list[0].lba = sector_qemu2lun(sector_num, iscsilun);
529 list[0].num = nb_sectors * BDRV_SECTOR_SIZE / iscsilun->block_size;
530
531 acb->task = iscsi_unmap_task(iscsi, iscsilun->lun,
532 0, 0, &list[0], 1,
533 iscsi_unmap_cb,
534 acb);
535 if (acb->task == NULL) {
536 error_report("iSCSI: Failed to send unmap command. %s",
537 iscsi_get_error(iscsi));
538 qemu_aio_release(acb);
539 return NULL;
540 }
541
542 iscsi_set_events(iscsilun);
543
544 return &acb->common;
545}
546
98392453
RS
547#ifdef __linux__
548static void
549iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
550 void *command_data, void *opaque)
551{
552 IscsiAIOCB *acb = opaque;
553
b2090919 554 if (acb->canceled != 0) {
98392453
RS
555 return;
556 }
557
558 acb->status = 0;
559 if (status < 0) {
560 error_report("Failed to ioctl(SG_IO) to iSCSI lun. %s",
561 iscsi_get_error(iscsi));
562 acb->status = -EIO;
563 }
564
565 acb->ioh->driver_status = 0;
566 acb->ioh->host_status = 0;
567 acb->ioh->resid = 0;
568
569#define SG_ERR_DRIVER_SENSE 0x08
570
571 if (status == SCSI_STATUS_CHECK_CONDITION && acb->task->datain.size >= 2) {
572 int ss;
573
574 acb->ioh->driver_status |= SG_ERR_DRIVER_SENSE;
575
576 acb->ioh->sb_len_wr = acb->task->datain.size - 2;
577 ss = (acb->ioh->mx_sb_len >= acb->ioh->sb_len_wr) ?
578 acb->ioh->mx_sb_len : acb->ioh->sb_len_wr;
579 memcpy(acb->ioh->sbp, &acb->task->datain.data[2], ss);
580 }
581
cfb3f506 582 iscsi_schedule_bh(acb);
98392453
RS
583}
584
585static BlockDriverAIOCB *iscsi_aio_ioctl(BlockDriverState *bs,
586 unsigned long int req, void *buf,
587 BlockDriverCompletionFunc *cb, void *opaque)
588{
589 IscsiLun *iscsilun = bs->opaque;
590 struct iscsi_context *iscsi = iscsilun->iscsi;
591 struct iscsi_data data;
592 IscsiAIOCB *acb;
593
594 assert(req == SG_IO);
595
d7331bed 596 acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
98392453
RS
597
598 acb->iscsilun = iscsilun;
599 acb->canceled = 0;
1bd075f2
PB
600 acb->bh = NULL;
601 acb->status = -EINPROGRESS;
98392453
RS
602 acb->buf = NULL;
603 acb->ioh = buf;
604
605 acb->task = malloc(sizeof(struct scsi_task));
606 if (acb->task == NULL) {
607 error_report("iSCSI: Failed to allocate task for scsi command. %s",
608 iscsi_get_error(iscsi));
609 qemu_aio_release(acb);
610 return NULL;
611 }
612 memset(acb->task, 0, sizeof(struct scsi_task));
613
614 switch (acb->ioh->dxfer_direction) {
615 case SG_DXFER_TO_DEV:
616 acb->task->xfer_dir = SCSI_XFER_WRITE;
617 break;
618 case SG_DXFER_FROM_DEV:
619 acb->task->xfer_dir = SCSI_XFER_READ;
620 break;
621 default:
622 acb->task->xfer_dir = SCSI_XFER_NONE;
623 break;
624 }
625
626 acb->task->cdb_size = acb->ioh->cmd_len;
627 memcpy(&acb->task->cdb[0], acb->ioh->cmdp, acb->ioh->cmd_len);
628 acb->task->expxferlen = acb->ioh->dxfer_len;
629
630 if (acb->task->xfer_dir == SCSI_XFER_WRITE) {
631 data.data = acb->ioh->dxferp;
632 data.size = acb->ioh->dxfer_len;
633 }
634 if (iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
635 iscsi_aio_ioctl_cb,
636 (acb->task->xfer_dir == SCSI_XFER_WRITE) ?
637 &data : NULL,
638 acb) != 0) {
639 scsi_free_scsi_task(acb->task);
640 qemu_aio_release(acb);
641 return NULL;
642 }
643
644 /* tell libiscsi to read straight into the buffer we got from ioctl */
645 if (acb->task->xfer_dir == SCSI_XFER_READ) {
646 scsi_task_add_data_in_buffer(acb->task,
647 acb->ioh->dxfer_len,
648 acb->ioh->dxferp);
649 }
650
651 iscsi_set_events(iscsilun);
652
653 return &acb->common;
654}
655
f1a12821
RS
656
657static void ioctl_cb(void *opaque, int status)
658{
659 int *p_status = opaque;
660 *p_status = status;
661}
662
98392453
RS
663static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
664{
665 IscsiLun *iscsilun = bs->opaque;
f1a12821 666 int status;
98392453
RS
667
668 switch (req) {
669 case SG_GET_VERSION_NUM:
670 *(int *)buf = 30000;
671 break;
672 case SG_GET_SCSI_ID:
673 ((struct sg_scsi_id *)buf)->scsi_type = iscsilun->type;
674 break;
f1a12821
RS
675 case SG_IO:
676 status = -EINPROGRESS;
677 iscsi_aio_ioctl(bs, req, buf, ioctl_cb, &status);
678
679 while (status == -EINPROGRESS) {
680 qemu_aio_wait();
681 }
682
683 return 0;
98392453
RS
684 default:
685 return -1;
686 }
687 return 0;
688}
689#endif
690
c589b249
RS
691static int64_t
692iscsi_getlength(BlockDriverState *bs)
693{
694 IscsiLun *iscsilun = bs->opaque;
695 int64_t len;
696
697 len = iscsilun->num_blocks;
698 len *= iscsilun->block_size;
699
700 return len;
701}
702
f9dadc98
RS
703static int parse_chap(struct iscsi_context *iscsi, const char *target)
704{
705 QemuOptsList *list;
706 QemuOpts *opts;
707 const char *user = NULL;
708 const char *password = NULL;
709
710 list = qemu_find_opts("iscsi");
711 if (!list) {
712 return 0;
713 }
714
715 opts = qemu_opts_find(list, target);
716 if (opts == NULL) {
717 opts = QTAILQ_FIRST(&list->head);
718 if (!opts) {
719 return 0;
720 }
721 }
722
723 user = qemu_opt_get(opts, "user");
724 if (!user) {
725 return 0;
726 }
727
728 password = qemu_opt_get(opts, "password");
729 if (!password) {
730 error_report("CHAP username specified but no password was given");
731 return -1;
732 }
733
734 if (iscsi_set_initiator_username_pwd(iscsi, user, password)) {
735 error_report("Failed to set initiator username and password");
736 return -1;
737 }
738
739 return 0;
740}
741
742static void parse_header_digest(struct iscsi_context *iscsi, const char *target)
743{
744 QemuOptsList *list;
745 QemuOpts *opts;
746 const char *digest = NULL;
747
748 list = qemu_find_opts("iscsi");
749 if (!list) {
750 return;
751 }
752
753 opts = qemu_opts_find(list, target);
754 if (opts == NULL) {
755 opts = QTAILQ_FIRST(&list->head);
756 if (!opts) {
757 return;
758 }
759 }
760
761 digest = qemu_opt_get(opts, "header-digest");
762 if (!digest) {
763 return;
764 }
765
766 if (!strcmp(digest, "CRC32C")) {
767 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C);
768 } else if (!strcmp(digest, "NONE")) {
769 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE);
770 } else if (!strcmp(digest, "CRC32C-NONE")) {
771 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_CRC32C_NONE);
772 } else if (!strcmp(digest, "NONE-CRC32C")) {
773 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
774 } else {
775 error_report("Invalid header-digest setting : %s", digest);
776 }
777}
778
779static char *parse_initiator_name(const char *target)
780{
781 QemuOptsList *list;
782 QemuOpts *opts;
783 const char *name = NULL;
31459f46 784 const char *iscsi_name = qemu_get_vm_name();
f9dadc98
RS
785
786 list = qemu_find_opts("iscsi");
f2ef4a6d
PB
787 if (list) {
788 opts = qemu_opts_find(list, target);
f9dadc98 789 if (!opts) {
f2ef4a6d
PB
790 opts = QTAILQ_FIRST(&list->head);
791 }
792 if (opts) {
793 name = qemu_opt_get(opts, "initiator-name");
f9dadc98
RS
794 }
795 }
796
f2ef4a6d
PB
797 if (name) {
798 return g_strdup(name);
799 } else {
31459f46
RS
800 return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
801 iscsi_name ? ":" : "",
802 iscsi_name ? iscsi_name : "");
f9dadc98 803 }
f9dadc98
RS
804}
805
5b5d34ec
PL
806#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
807static void iscsi_nop_timed_event(void *opaque)
808{
809 IscsiLun *iscsilun = opaque;
810
811 if (iscsi_get_nops_in_flight(iscsilun->iscsi) > MAX_NOP_FAILURES) {
812 error_report("iSCSI: NOP timeout. Reconnecting...");
813 iscsi_reconnect(iscsilun->iscsi);
814 }
815
816 if (iscsi_nop_out_async(iscsilun->iscsi, NULL, NULL, 0, NULL) != 0) {
817 error_report("iSCSI: failed to sent NOP-Out. Disabling NOP messages.");
818 return;
819 }
820
821 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
822 iscsi_set_events(iscsilun);
823}
824#endif
825
c589b249
RS
826/*
827 * We support iscsi url's on the form
828 * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
829 */
830static int iscsi_open(BlockDriverState *bs, const char *filename, int flags)
831{
832 IscsiLun *iscsilun = bs->opaque;
833 struct iscsi_context *iscsi = NULL;
834 struct iscsi_url *iscsi_url = NULL;
e829b0bb
PL
835 struct scsi_task *task = NULL;
836 struct scsi_inquiry_standard *inq = NULL;
837 struct scsi_readcapacity10 *rc10 = NULL;
838 struct scsi_readcapacity16 *rc16 = NULL;
f9dadc98 839 char *initiator_name = NULL;
c589b249
RS
840 int ret;
841
842 if ((BDRV_SECTOR_SIZE % 512) != 0) {
843 error_report("iSCSI: Invalid BDRV_SECTOR_SIZE. "
844 "BDRV_SECTOR_SIZE(%lld) is not a multiple "
845 "of 512", BDRV_SECTOR_SIZE);
846 return -EINVAL;
847 }
848
c589b249
RS
849 iscsi_url = iscsi_parse_full_url(iscsi, filename);
850 if (iscsi_url == NULL) {
8da1e18b 851 error_report("Failed to parse URL : %s", filename);
c589b249 852 ret = -EINVAL;
b93c94f7 853 goto out;
c589b249
RS
854 }
855
f9dadc98
RS
856 memset(iscsilun, 0, sizeof(IscsiLun));
857
858 initiator_name = parse_initiator_name(iscsi_url->target);
859
860 iscsi = iscsi_create_context(initiator_name);
861 if (iscsi == NULL) {
862 error_report("iSCSI: Failed to create iSCSI context.");
863 ret = -ENOMEM;
b93c94f7 864 goto out;
f9dadc98
RS
865 }
866
c589b249
RS
867 if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
868 error_report("iSCSI: Failed to set target name.");
869 ret = -EINVAL;
b93c94f7 870 goto out;
c589b249
RS
871 }
872
873 if (iscsi_url->user != NULL) {
874 ret = iscsi_set_initiator_username_pwd(iscsi, iscsi_url->user,
875 iscsi_url->passwd);
876 if (ret != 0) {
877 error_report("Failed to set initiator username and password");
878 ret = -EINVAL;
b93c94f7 879 goto out;
c589b249
RS
880 }
881 }
f9dadc98
RS
882
883 /* check if we got CHAP username/password via the options */
884 if (parse_chap(iscsi, iscsi_url->target) != 0) {
885 error_report("iSCSI: Failed to set CHAP user/password");
886 ret = -EINVAL;
b93c94f7 887 goto out;
f9dadc98
RS
888 }
889
c589b249
RS
890 if (iscsi_set_session_type(iscsi, ISCSI_SESSION_NORMAL) != 0) {
891 error_report("iSCSI: Failed to set session type to normal.");
892 ret = -EINVAL;
b93c94f7 893 goto out;
c589b249
RS
894 }
895
896 iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
897
f9dadc98
RS
898 /* check if we got HEADER_DIGEST via the options */
899 parse_header_digest(iscsi, iscsi_url->target);
900
e829b0bb
PL
901 if (iscsi_full_connect_sync(iscsi, iscsi_url->portal, iscsi_url->lun) != 0) {
902 error_report("iSCSI: Failed to connect to LUN : %s",
903 iscsi_get_error(iscsi));
904 ret = -EINVAL;
905 goto out;
906 }
c589b249
RS
907
908 iscsilun->iscsi = iscsi;
909 iscsilun->lun = iscsi_url->lun;
910
e829b0bb
PL
911 task = iscsi_inquiry_sync(iscsi, iscsilun->lun, 0, 0, 36);
912
913 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
914 error_report("iSCSI: failed to send inquiry command.");
c589b249 915 ret = -EINVAL;
b93c94f7 916 goto out;
c589b249
RS
917 }
918
e829b0bb
PL
919 inq = scsi_datain_unmarshall(task);
920 if (inq == NULL) {
921 error_report("iSCSI: Failed to unmarshall inquiry data.");
c589b249 922 ret = -EINVAL;
b93c94f7 923 goto out;
c589b249 924 }
622695a4 925
e829b0bb
PL
926 iscsilun->type = inq->periperal_device_type;
927
928 scsi_free_scsi_task(task);
929
930 switch (iscsilun->type) {
931 case TYPE_DISK:
932 task = iscsi_readcapacity16_sync(iscsi, iscsilun->lun);
933 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
934 error_report("iSCSI: failed to send readcapacity16 command.");
935 ret = -EINVAL;
936 goto out;
937 }
938 rc16 = scsi_datain_unmarshall(task);
939 if (rc16 == NULL) {
940 error_report("iSCSI: Failed to unmarshall readcapacity16 data.");
941 ret = -EINVAL;
942 goto out;
943 }
944 iscsilun->block_size = rc16->block_length;
945 iscsilun->num_blocks = rc16->returned_lba + 1;
946 break;
947 case TYPE_ROM:
948 task = iscsi_readcapacity10_sync(iscsi, iscsilun->lun, 0, 0);
949 if (task == NULL || task->status != SCSI_STATUS_GOOD) {
950 error_report("iSCSI: failed to send readcapacity10 command.");
951 ret = -EINVAL;
952 goto out;
953 }
954 rc10 = scsi_datain_unmarshall(task);
955 if (rc10 == NULL) {
956 error_report("iSCSI: Failed to unmarshall readcapacity10 data.");
957 ret = -EINVAL;
958 goto out;
959 }
960 iscsilun->block_size = rc10->block_size;
961 if (rc10->lba == 0) {
962 /* blank disk loaded */
963 iscsilun->num_blocks = 0;
964 } else {
965 iscsilun->num_blocks = rc10->lba + 1;
966 }
967 break;
968 default:
969 break;
970 }
971
972 bs->total_sectors = iscsilun->num_blocks *
973 iscsilun->block_size / BDRV_SECTOR_SIZE ;
974
622695a4
RS
975 /* Medium changer or tape. We dont have any emulation for this so this must
976 * be sg ioctl compatible. We force it to be sg, otherwise qemu will try
977 * to read from the device to guess the image format.
978 */
979 if (iscsilun->type == TYPE_MEDIUM_CHANGER ||
980 iscsilun->type == TYPE_TAPE) {
981 bs->sg = 1;
982 }
983
b93c94f7 984 ret = 0;
c589b249 985
5b5d34ec
PL
986#if defined(LIBISCSI_FEATURE_NOP_COUNTER)
987 /* Set up a timer for sending out iSCSI NOPs */
988 iscsilun->nop_timer = qemu_new_timer_ms(rt_clock, iscsi_nop_timed_event, iscsilun);
989 qemu_mod_timer(iscsilun->nop_timer, qemu_get_clock_ms(rt_clock) + NOP_INTERVAL);
990#endif
991
b93c94f7 992out:
f9dadc98
RS
993 if (initiator_name != NULL) {
994 g_free(initiator_name);
995 }
c589b249
RS
996 if (iscsi_url != NULL) {
997 iscsi_destroy_url(iscsi_url);
998 }
e829b0bb
PL
999 if (task != NULL) {
1000 scsi_free_scsi_task(task);
1001 }
b93c94f7
PB
1002
1003 if (ret) {
1004 if (iscsi != NULL) {
1005 iscsi_destroy_context(iscsi);
1006 }
1007 memset(iscsilun, 0, sizeof(IscsiLun));
c589b249 1008 }
c589b249
RS
1009 return ret;
1010}
1011
1012static void iscsi_close(BlockDriverState *bs)
1013{
1014 IscsiLun *iscsilun = bs->opaque;
1015 struct iscsi_context *iscsi = iscsilun->iscsi;
1016
5b5d34ec
PL
1017 if (iscsilun->nop_timer) {
1018 qemu_del_timer(iscsilun->nop_timer);
1019 qemu_free_timer(iscsilun->nop_timer);
1020 }
bafbd6a1 1021 qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
c589b249
RS
1022 iscsi_destroy_context(iscsi);
1023 memset(iscsilun, 0, sizeof(IscsiLun));
1024}
1025
f807ecd5
PL
1026static int iscsi_has_zero_init(BlockDriverState *bs)
1027{
1028 return 0;
1029}
1030
de8864e5
PL
1031static int iscsi_create(const char *filename, QEMUOptionParameter *options)
1032{
1033 int ret = 0;
1034 int64_t total_size = 0;
1035 BlockDriverState bs;
1036 IscsiLun *iscsilun = NULL;
1037
1038 memset(&bs, 0, sizeof(BlockDriverState));
1039
1040 /* Read out options */
1041 while (options && options->name) {
1042 if (!strcmp(options->name, "size")) {
1043 total_size = options->value.n / BDRV_SECTOR_SIZE;
1044 }
1045 options++;
1046 }
1047
1048 bs.opaque = g_malloc0(sizeof(struct IscsiLun));
1049 iscsilun = bs.opaque;
1050
1051 ret = iscsi_open(&bs, filename, 0);
1052 if (ret != 0) {
1053 goto out;
1054 }
5b5d34ec
PL
1055 if (iscsilun->nop_timer) {
1056 qemu_del_timer(iscsilun->nop_timer);
1057 qemu_free_timer(iscsilun->nop_timer);
1058 }
de8864e5
PL
1059 if (iscsilun->type != TYPE_DISK) {
1060 ret = -ENODEV;
1061 goto out;
1062 }
1063 if (bs.total_sectors < total_size) {
1064 ret = -ENOSPC;
1065 }
1066
1067 ret = 0;
1068out:
1069 if (iscsilun->iscsi != NULL) {
1070 iscsi_destroy_context(iscsilun->iscsi);
1071 }
1072 g_free(bs.opaque);
1073 return ret;
1074}
1075
1076static QEMUOptionParameter iscsi_create_options[] = {
1077 {
1078 .name = BLOCK_OPT_SIZE,
1079 .type = OPT_SIZE,
1080 .help = "Virtual disk size"
1081 },
1082 { NULL }
1083};
1084
c589b249
RS
1085static BlockDriver bdrv_iscsi = {
1086 .format_name = "iscsi",
1087 .protocol_name = "iscsi",
1088
1089 .instance_size = sizeof(IscsiLun),
1090 .bdrv_file_open = iscsi_open,
1091 .bdrv_close = iscsi_close,
de8864e5
PL
1092 .bdrv_create = iscsi_create,
1093 .create_options = iscsi_create_options,
c589b249
RS
1094
1095 .bdrv_getlength = iscsi_getlength,
1096
1097 .bdrv_aio_readv = iscsi_aio_readv,
1098 .bdrv_aio_writev = iscsi_aio_writev,
1099 .bdrv_aio_flush = iscsi_aio_flush,
fa6acb0c
RS
1100
1101 .bdrv_aio_discard = iscsi_aio_discard,
f807ecd5 1102 .bdrv_has_zero_init = iscsi_has_zero_init,
98392453
RS
1103
1104#ifdef __linux__
1105 .bdrv_ioctl = iscsi_ioctl,
1106 .bdrv_aio_ioctl = iscsi_aio_ioctl,
1107#endif
c589b249
RS
1108};
1109
4d454574
PB
1110static QemuOptsList qemu_iscsi_opts = {
1111 .name = "iscsi",
1112 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
1113 .desc = {
1114 {
1115 .name = "user",
1116 .type = QEMU_OPT_STRING,
1117 .help = "username for CHAP authentication to target",
1118 },{
1119 .name = "password",
1120 .type = QEMU_OPT_STRING,
1121 .help = "password for CHAP authentication to target",
1122 },{
1123 .name = "header-digest",
1124 .type = QEMU_OPT_STRING,
1125 .help = "HeaderDigest setting. "
1126 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
1127 },{
1128 .name = "initiator-name",
1129 .type = QEMU_OPT_STRING,
1130 .help = "Initiator iqn name to use when connecting",
1131 },
1132 { /* end of list */ }
1133 },
1134};
1135
c589b249
RS
1136static void iscsi_block_init(void)
1137{
1138 bdrv_register(&bdrv_iscsi);
4d454574 1139 qemu_add_opts(&qemu_iscsi_opts);
c589b249
RS
1140}
1141
1142block_init(iscsi_block_init);