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