]> Git Repo - qemu.git/blob - block/iscsi.c
qcow2: introduce check_refcounts_l1/l2() flags
[qemu.git] / block / iscsi.c
1 /*
2  * QEMU Block driver for iSCSI images
3  *
4  * Copyright (c) 2010-2011 Ronnie Sahlberg <[email protected]>
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/config-file.h"
31 #include "qemu/error-report.h"
32 #include "block/block_int.h"
33 #include "trace.h"
34 #include "hw/scsi-defs.h"
35
36 #include <iscsi/iscsi.h>
37 #include <iscsi/scsi-lowlevel.h>
38
39 #ifdef __linux__
40 #include <scsi/sg.h>
41 #include <hw/scsi-defs.h>
42 #endif
43
44 typedef struct IscsiLun {
45     struct iscsi_context *iscsi;
46     int lun;
47     enum scsi_inquiry_peripheral_device_type type;
48     int block_size;
49     uint64_t num_blocks;
50     int events;
51     QEMUTimer *nop_timer;
52 } IscsiLun;
53
54 typedef 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;
65 #ifdef __linux__
66     sg_io_hdr_t *ioh;
67 #endif
68 } IscsiAIOCB;
69
70 #define NOP_INTERVAL 5000
71 #define MAX_NOP_FAILURES 3
72
73 static void
74 iscsi_bh_cb(void *p)
75 {
76     IscsiAIOCB *acb = p;
77
78     qemu_bh_delete(acb->bh);
79
80     g_free(acb->buf);
81     acb->buf = NULL;
82
83     if (acb->canceled == 0) {
84         acb->common.cb(acb->common.opaque, acb->status);
85     }
86
87     if (acb->task != NULL) {
88         scsi_free_scsi_task(acb->task);
89         acb->task = NULL;
90     }
91
92     qemu_aio_release(acb);
93 }
94
95 static void
96 iscsi_schedule_bh(IscsiAIOCB *acb)
97 {
98     if (acb->bh) {
99         return;
100     }
101     acb->bh = qemu_bh_new(iscsi_bh_cb, acb);
102     qemu_bh_schedule(acb->bh);
103 }
104
105
106 static void
107 iscsi_abort_task_cb(struct iscsi_context *iscsi, int status, void *command_data,
108                     void *private_data)
109 {
110     IscsiAIOCB *acb = private_data;
111
112     acb->status = -ECANCELED;
113     iscsi_schedule_bh(acb);
114 }
115
116 static void
117 iscsi_aio_cancel(BlockDriverAIOCB *blockacb)
118 {
119     IscsiAIOCB *acb = (IscsiAIOCB *)blockacb;
120     IscsiLun *iscsilun = acb->iscsilun;
121
122     if (acb->status != -EINPROGRESS) {
123         return;
124     }
125
126     acb->canceled = 1;
127
128     /* send a task mgmt call to the target to cancel the task on the target */
129     iscsi_task_mgmt_abort_task_async(iscsilun->iscsi, acb->task,
130                                      iscsi_abort_task_cb, acb);
131
132     while (acb->status == -EINPROGRESS) {
133         qemu_aio_wait();
134     }
135 }
136
137 static const AIOCBInfo iscsi_aiocb_info = {
138     .aiocb_size         = sizeof(IscsiAIOCB),
139     .cancel             = iscsi_aio_cancel,
140 };
141
142
143 static void iscsi_process_read(void *arg);
144 static void iscsi_process_write(void *arg);
145
146 static int iscsi_process_flush(void *arg)
147 {
148     IscsiLun *iscsilun = arg;
149
150     return iscsi_queue_length(iscsilun->iscsi) > 0;
151 }
152
153 static void
154 iscsi_set_events(IscsiLun *iscsilun)
155 {
156     struct iscsi_context *iscsi = iscsilun->iscsi;
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
171     iscsilun->events = ev;
172 }
173
174 static void
175 iscsi_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
184 static void
185 iscsi_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
195 static void
196 iscsi_aio_write16_cb(struct iscsi_context *iscsi, int status,
197                      void *command_data, void *opaque)
198 {
199     IscsiAIOCB *acb = opaque;
200
201     trace_iscsi_aio_write16_cb(iscsi, status, acb, acb->canceled);
202
203     g_free(acb->buf);
204     acb->buf = NULL;
205
206     if (acb->canceled != 0) {
207         return;
208     }
209
210     acb->status = 0;
211     if (status < 0) {
212         error_report("Failed to write16 data to iSCSI lun. %s",
213                      iscsi_get_error(iscsi));
214         acb->status = -EIO;
215     }
216
217     iscsi_schedule_bh(acb);
218 }
219
220 static int64_t sector_qemu2lun(int64_t sector, IscsiLun *iscsilun)
221 {
222     return sector * BDRV_SECTOR_SIZE / iscsilun->block_size;
223 }
224
225 static BlockDriverAIOCB *
226 iscsi_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;
235     uint32_t num_sectors;
236     uint64_t lba;
237 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
238     struct iscsi_data data;
239 #endif
240     int ret;
241
242     acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
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;
249     acb->bh         = NULL;
250     acb->status     = -EINPROGRESS;
251     acb->buf        = NULL;
252
253     /* this will allow us to get rid of 'buf' completely */
254     size = nb_sectors * BDRV_SECTOR_SIZE;
255
256 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
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) {
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     }
267 #endif
268
269     acb->task = malloc(sizeof(struct scsi_task));
270     if (acb->task == NULL) {
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;
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
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) {
300         scsi_free_scsi_task(acb->task);
301         g_free(acb->buf);
302         qemu_aio_release(acb);
303         return NULL;
304     }
305
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
310     iscsi_set_events(iscsilun);
311
312     return &acb->common;
313 }
314
315 static void
316 iscsi_aio_read16_cb(struct iscsi_context *iscsi, int status,
317                     void *command_data, void *opaque)
318 {
319     IscsiAIOCB *acb = opaque;
320
321     trace_iscsi_aio_read16_cb(iscsi, status, acb, acb->canceled);
322
323     if (acb->canceled != 0) {
324         return;
325     }
326
327     acb->status = 0;
328     if (status != 0) {
329         error_report("Failed to read16 data from iSCSI lun. %s",
330                      iscsi_get_error(iscsi));
331         acb->status = -EIO;
332     }
333
334     iscsi_schedule_bh(acb);
335 }
336
337 static BlockDriverAIOCB *
338 iscsi_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;
346     size_t qemu_read_size;
347 #if !defined(LIBISCSI_FEATURE_IOVECTOR)
348     int i;
349 #endif
350     int ret;
351     uint64_t lba;
352     uint32_t num_sectors;
353
354     qemu_read_size = BDRV_SECTOR_SIZE * (size_t)nb_sectors;
355
356     acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
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;
363     acb->bh          = NULL;
364     acb->status      = -EINPROGRESS;
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
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));
384     if (acb->task == NULL) {
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     }
411
412     ret = iscsi_scsi_command_async(iscsi, iscsilun->lun, acb->task,
413                                    iscsi_aio_read16_cb,
414                                    NULL,
415                                    acb);
416     if (ret != 0) {
417         scsi_free_scsi_task(acb->task);
418         qemu_aio_release(acb);
419         return NULL;
420     }
421
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
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     }
430 #endif
431
432     iscsi_set_events(iscsilun);
433
434     return &acb->common;
435 }
436
437
438 static void
439 iscsi_synccache10_cb(struct iscsi_context *iscsi, int status,
440                      void *command_data, void *opaque)
441 {
442     IscsiAIOCB *acb = opaque;
443
444     if (acb->canceled != 0) {
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
455     iscsi_schedule_bh(acb);
456 }
457
458 static BlockDriverAIOCB *
459 iscsi_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
466     acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
467
468     acb->iscsilun = iscsilun;
469     acb->canceled   = 0;
470     acb->bh         = NULL;
471     acb->status     = -EINPROGRESS;
472     acb->buf        = NULL;
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
490 static void
491 iscsi_unmap_cb(struct iscsi_context *iscsi, int status,
492                      void *command_data, void *opaque)
493 {
494     IscsiAIOCB *acb = opaque;
495
496     if (acb->canceled != 0) {
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
507     iscsi_schedule_bh(acb);
508 }
509
510 static BlockDriverAIOCB *
511 iscsi_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
520     acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
521
522     acb->iscsilun = iscsilun;
523     acb->canceled   = 0;
524     acb->bh         = NULL;
525     acb->status     = -EINPROGRESS;
526     acb->buf        = NULL;
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
547 #ifdef __linux__
548 static void
549 iscsi_aio_ioctl_cb(struct iscsi_context *iscsi, int status,
550                      void *command_data, void *opaque)
551 {
552     IscsiAIOCB *acb = opaque;
553
554     if (acb->canceled != 0) {
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
582     iscsi_schedule_bh(acb);
583 }
584
585 static 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
596     acb = qemu_aio_get(&iscsi_aiocb_info, bs, cb, opaque);
597
598     acb->iscsilun = iscsilun;
599     acb->canceled    = 0;
600     acb->bh          = NULL;
601     acb->status      = -EINPROGRESS;
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
656
657 static void ioctl_cb(void *opaque, int status)
658 {
659     int *p_status = opaque;
660     *p_status = status;
661 }
662
663 static int iscsi_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
664 {
665     IscsiLun *iscsilun = bs->opaque;
666     int status;
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;
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;
684     default:
685         return -1;
686     }
687     return 0;
688 }
689 #endif
690
691 static int64_t
692 iscsi_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
703 static 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
742 static 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
779 static char *parse_initiator_name(const char *target)
780 {
781     QemuOptsList *list;
782     QemuOpts *opts;
783     const char *name = NULL;
784     const char *iscsi_name = qemu_get_vm_name();
785
786     list = qemu_find_opts("iscsi");
787     if (list) {
788         opts = qemu_opts_find(list, target);
789         if (!opts) {
790             opts = QTAILQ_FIRST(&list->head);
791         }
792         if (opts) {
793             name = qemu_opt_get(opts, "initiator-name");
794         }
795     }
796
797     if (name) {
798         return g_strdup(name);
799     } else {
800         return g_strdup_printf("iqn.2008-11.org.linux-kvm%s%s",
801                                iscsi_name ? ":" : "",
802                                iscsi_name ? iscsi_name : "");
803     }
804 }
805
806 #if defined(LIBISCSI_FEATURE_NOP_COUNTER)
807 static 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
826 /*
827  * We support iscsi url's on the form
828  * iscsi://[<username>%<password>@]<host>[:<port>]/<targetname>/<lun>
829  */
830 static 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;
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;
839     char *initiator_name = NULL;
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
849     iscsi_url = iscsi_parse_full_url(iscsi, filename);
850     if (iscsi_url == NULL) {
851         error_report("Failed to parse URL : %s", filename);
852         ret = -EINVAL;
853         goto out;
854     }
855
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;
864         goto out;
865     }
866
867     if (iscsi_set_targetname(iscsi, iscsi_url->target)) {
868         error_report("iSCSI: Failed to set target name.");
869         ret = -EINVAL;
870         goto out;
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;
879             goto out;
880         }
881     }
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;
887         goto out;
888     }
889
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;
893         goto out;
894     }
895
896     iscsi_set_header_digest(iscsi, ISCSI_HEADER_DIGEST_NONE_CRC32C);
897
898     /* check if we got HEADER_DIGEST via the options */
899     parse_header_digest(iscsi, iscsi_url->target);
900
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     }
907
908     iscsilun->iscsi = iscsi;
909     iscsilun->lun   = iscsi_url->lun;
910
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.");
915         ret = -EINVAL;
916         goto out;
917     }
918
919     inq = scsi_datain_unmarshall(task);
920     if (inq == NULL) {
921         error_report("iSCSI: Failed to unmarshall inquiry data.");
922         ret = -EINVAL;
923         goto out;
924     }
925
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
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
984     ret = 0;
985
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
992 out:
993     if (initiator_name != NULL) {
994         g_free(initiator_name);
995     }
996     if (iscsi_url != NULL) {
997         iscsi_destroy_url(iscsi_url);
998     }
999     if (task != NULL) {
1000         scsi_free_scsi_task(task);
1001     }
1002
1003     if (ret) {
1004         if (iscsi != NULL) {
1005             iscsi_destroy_context(iscsi);
1006         }
1007         memset(iscsilun, 0, sizeof(IscsiLun));
1008     }
1009     return ret;
1010 }
1011
1012 static void iscsi_close(BlockDriverState *bs)
1013 {
1014     IscsiLun *iscsilun = bs->opaque;
1015     struct iscsi_context *iscsi = iscsilun->iscsi;
1016
1017     if (iscsilun->nop_timer) {
1018         qemu_del_timer(iscsilun->nop_timer);
1019         qemu_free_timer(iscsilun->nop_timer);
1020     }
1021     qemu_aio_set_fd_handler(iscsi_get_fd(iscsi), NULL, NULL, NULL, NULL);
1022     iscsi_destroy_context(iscsi);
1023     memset(iscsilun, 0, sizeof(IscsiLun));
1024 }
1025
1026 static int iscsi_has_zero_init(BlockDriverState *bs)
1027 {
1028     return 0;
1029 }
1030
1031 static 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     }
1055     if (iscsilun->nop_timer) {
1056         qemu_del_timer(iscsilun->nop_timer);
1057         qemu_free_timer(iscsilun->nop_timer);
1058     }
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;
1068 out:
1069     if (iscsilun->iscsi != NULL) {
1070         iscsi_destroy_context(iscsilun->iscsi);
1071     }
1072     g_free(bs.opaque);
1073     return ret;
1074 }
1075
1076 static QEMUOptionParameter iscsi_create_options[] = {
1077     {
1078         .name = BLOCK_OPT_SIZE,
1079         .type = OPT_SIZE,
1080         .help = "Virtual disk size"
1081     },
1082     { NULL }
1083 };
1084
1085 static 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,
1092     .bdrv_create     = iscsi_create,
1093     .create_options  = iscsi_create_options,
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,
1100
1101     .bdrv_aio_discard = iscsi_aio_discard,
1102     .bdrv_has_zero_init = iscsi_has_zero_init,
1103
1104 #ifdef __linux__
1105     .bdrv_ioctl       = iscsi_ioctl,
1106     .bdrv_aio_ioctl   = iscsi_aio_ioctl,
1107 #endif
1108 };
1109
1110 static 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
1136 static void iscsi_block_init(void)
1137 {
1138     bdrv_register(&bdrv_iscsi);
1139     qemu_add_opts(&qemu_iscsi_opts);
1140 }
1141
1142 block_init(iscsi_block_init);
This page took 0.084065 seconds and 4 git commands to generate.