2 * QEMU Block driver for CURL images
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include <curl/curl.h>
29 // #define DEBUG_VERBOSE
32 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) do { } while (0)
37 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
38 CURLPROTO_FTP | CURLPROTO_FTPS | \
41 #define CURL_NUM_STATES 8
42 #define CURL_NUM_ACB 8
43 #define SECTOR_SIZE 512
44 #define READ_AHEAD_SIZE (256 * 1024)
46 #define FIND_RET_NONE 0
48 #define FIND_RET_WAIT 2
52 typedef struct CURLAIOCB
{
53 BlockDriverAIOCB common
;
64 typedef struct CURLState
66 struct BDRVCURLState
*s
;
67 CURLAIOCB
*acb
[CURL_NUM_ACB
];
74 char errmsg
[CURL_ERROR_SIZE
];
78 typedef struct BDRVCURLState
{
81 CURLState states
[CURL_NUM_STATES
];
83 size_t readahead_size
;
86 static void curl_clean_state(CURLState
*s
);
87 static void curl_multi_do(void *arg
);
88 static int curl_aio_flush(void *opaque
);
90 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
93 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action
, fd
);
96 qemu_aio_set_fd_handler(fd
, curl_multi_do
, NULL
, curl_aio_flush
, s
);
99 qemu_aio_set_fd_handler(fd
, NULL
, curl_multi_do
, curl_aio_flush
, s
);
101 case CURL_POLL_INOUT
:
102 qemu_aio_set_fd_handler(fd
, curl_multi_do
, curl_multi_do
,
105 case CURL_POLL_REMOVE
:
106 qemu_aio_set_fd_handler(fd
, NULL
, NULL
, NULL
, NULL
);
113 static size_t curl_size_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
115 CURLState
*s
= ((CURLState
*)opaque
);
116 size_t realsize
= size
* nmemb
;
119 if(sscanf(ptr
, "Content-Length: %zd", &fsize
) == 1) {
126 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
128 CURLState
*s
= ((CURLState
*)opaque
);
129 size_t realsize
= size
* nmemb
;
132 DPRINTF("CURL: Just reading %zd bytes\n", realsize
);
134 if (!s
|| !s
->orig_buf
)
137 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
138 s
->buf_off
+= realsize
;
140 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
141 CURLAIOCB
*acb
= s
->acb
[i
];
146 if ((s
->buf_off
>= acb
->end
)) {
147 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
148 acb
->end
- acb
->start
);
149 acb
->common
.cb(acb
->common
.opaque
, 0);
150 qemu_aio_release(acb
);
159 static int curl_find_buf(BDRVCURLState
*s
, size_t start
, size_t len
,
163 size_t end
= start
+ len
;
165 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
166 CURLState
*state
= &s
->states
[i
];
167 size_t buf_end
= (state
->buf_start
+ state
->buf_off
);
168 size_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
170 if (!state
->orig_buf
)
175 // Does the existing buffer cover our section?
176 if ((start
>= state
->buf_start
) &&
177 (start
<= buf_end
) &&
178 (end
>= state
->buf_start
) &&
181 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
183 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, len
);
184 acb
->common
.cb(acb
->common
.opaque
, 0);
189 // Wait for unfinished chunks
190 if ((start
>= state
->buf_start
) &&
191 (start
<= buf_fend
) &&
192 (end
>= state
->buf_start
) &&
197 acb
->start
= start
- state
->buf_start
;
198 acb
->end
= acb
->start
+ len
;
200 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
201 if (!state
->acb
[j
]) {
203 return FIND_RET_WAIT
;
209 return FIND_RET_NONE
;
212 static void curl_multi_do(void *arg
)
214 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
223 r
= curl_multi_socket_all(s
->multi
, &running
);
224 } while(r
== CURLM_CALL_MULTI_PERFORM
);
226 /* Try to find done transfers, so we can free the easy
230 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
234 if (msg
->msg
== CURLMSG_NONE
)
240 CURLState
*state
= NULL
;
241 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
, (char**)&state
);
243 /* ACBs for successful messages get completed in curl_read_cb */
244 if (msg
->data
.result
!= CURLE_OK
) {
246 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
247 CURLAIOCB
*acb
= state
->acb
[i
];
253 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
254 qemu_aio_release(acb
);
255 state
->acb
[i
] = NULL
;
259 curl_clean_state(state
);
266 } while(msgs_in_queue
);
269 static CURLState
*curl_init_state(BDRVCURLState
*s
)
271 CURLState
*state
= NULL
;
275 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
276 for (j
=0; j
<CURL_NUM_ACB
; j
++)
277 if (s
->states
[i
].acb
[j
])
279 if (s
->states
[i
].in_use
)
282 state
= &s
->states
[i
];
295 state
->curl
= curl_easy_init();
298 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
299 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, 5);
300 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_read_cb
);
301 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
302 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
303 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
304 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
305 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
306 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
307 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
309 /* Restrict supported protocols to avoid security issues in the more
310 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
313 * Restricting protocols is only supported from 7.19.4 upwards.
315 #if LIBCURL_VERSION_NUM >= 0x071304
316 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
317 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
321 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
331 static void curl_clean_state(CURLState
*s
)
334 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
338 static void curl_parse_filename(const char *filename
, QDict
*options
,
342 #define RA_OPTSTR ":readahead="
348 file
= g_strdup(filename
);
350 /* Parse a trailing ":readahead=#:" param, if present. */
351 ra
= file
+ strlen(file
) - 1;
353 if (parse_state
== 0) {
359 } else if (parse_state
== 1) {
360 if (*ra
> '9' || *ra
< '0') {
361 char *opt_start
= ra
- strlen(RA_OPTSTR
) + 1;
362 if (opt_start
> file
&&
363 strncmp(opt_start
, RA_OPTSTR
, strlen(RA_OPTSTR
)) == 0) {
365 ra
-= strlen(RA_OPTSTR
) - 1;
367 qdict_put(options
, "readahead", qstring_from_str(ra_val
));
375 qdict_put(options
, "url", qstring_from_str(file
));
380 static QemuOptsList runtime_opts
= {
382 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
386 .type
= QEMU_OPT_STRING
,
387 .help
= "URL to open",
391 .type
= QEMU_OPT_SIZE
,
392 .help
= "Readahead size",
394 { /* end of list */ }
398 static int curl_open(BlockDriverState
*bs
, const char *dummy
,
399 QDict
*options
, int flags
)
401 BDRVCURLState
*s
= bs
->opaque
;
402 CURLState
*state
= NULL
;
404 Error
*local_err
= NULL
;
408 static int inited
= 0;
410 opts
= qemu_opts_create_nofail(&runtime_opts
);
411 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
412 if (error_is_set(&local_err
)) {
413 qerror_report_err(local_err
);
414 error_free(local_err
);
418 s
->readahead_size
= qemu_opt_get_size(opts
, "readahead", READ_AHEAD_SIZE
);
419 if ((s
->readahead_size
& 0x1ff) != 0) {
420 fprintf(stderr
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
425 file
= qemu_opt_get(opts
, "url");
427 qerror_report(ERROR_CLASS_GENERIC_ERROR
, "curl block driver requires "
433 curl_global_init(CURL_GLOBAL_ALL
);
437 DPRINTF("CURL: Opening %s\n", file
);
438 s
->url
= g_strdup(file
);
439 state
= curl_init_state(s
);
445 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
446 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_size_cb
);
447 if (curl_easy_perform(state
->curl
))
449 curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
);
450 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_read_cb
);
451 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 0);
456 DPRINTF("CURL: Size = %zd\n", s
->len
);
458 curl_clean_state(state
);
459 curl_easy_cleanup(state
->curl
);
462 // Now we know the file exists and its size, so let's
463 // initialize the multi interface!
465 s
->multi
= curl_multi_init();
466 curl_multi_setopt( s
->multi
, CURLMOPT_SOCKETDATA
, s
);
467 curl_multi_setopt( s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
474 fprintf(stderr
, "CURL: Error opening file: %s\n", state
->errmsg
);
475 curl_easy_cleanup(state
->curl
);
483 static int curl_aio_flush(void *opaque
)
485 BDRVCURLState
*s
= opaque
;
488 for (i
=0; i
< CURL_NUM_STATES
; i
++) {
489 for(j
=0; j
< CURL_NUM_ACB
; j
++) {
490 if (s
->states
[i
].acb
[j
]) {
498 static void curl_aio_cancel(BlockDriverAIOCB
*blockacb
)
500 // Do we have to implement canceling? Seems to work without...
503 static const AIOCBInfo curl_aiocb_info
= {
504 .aiocb_size
= sizeof(CURLAIOCB
),
505 .cancel
= curl_aio_cancel
,
509 static void curl_readv_bh_cb(void *p
)
514 BDRVCURLState
*s
= acb
->common
.bs
->opaque
;
516 qemu_bh_delete(acb
->bh
);
519 size_t start
= acb
->sector_num
* SECTOR_SIZE
;
522 // In case we have the requested data already (e.g. read-ahead),
523 // we can just call the callback and be done.
524 switch (curl_find_buf(s
, start
, acb
->nb_sectors
* SECTOR_SIZE
, acb
)) {
526 qemu_aio_release(acb
);
534 // No cache found, so let's start a new request
535 state
= curl_init_state(s
);
537 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
538 qemu_aio_release(acb
);
543 acb
->end
= (acb
->nb_sectors
* SECTOR_SIZE
);
547 g_free(state
->orig_buf
);
548 state
->buf_start
= start
;
549 state
->buf_len
= acb
->end
+ s
->readahead_size
;
550 end
= MIN(start
+ state
->buf_len
, s
->len
) - 1;
551 state
->orig_buf
= g_malloc(state
->buf_len
);
554 snprintf(state
->range
, 127, "%zd-%zd", start
, end
);
555 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
556 (acb
->nb_sectors
* SECTOR_SIZE
), start
, state
->range
);
557 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
559 curl_multi_add_handle(s
->multi
, state
->curl
);
564 static BlockDriverAIOCB
*curl_aio_readv(BlockDriverState
*bs
,
565 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
566 BlockDriverCompletionFunc
*cb
, void *opaque
)
570 acb
= qemu_aio_get(&curl_aiocb_info
, bs
, cb
, opaque
);
573 acb
->sector_num
= sector_num
;
574 acb
->nb_sectors
= nb_sectors
;
576 acb
->bh
= qemu_bh_new(curl_readv_bh_cb
, acb
);
579 DPRINTF("CURL: qemu_bh_new failed\n");
583 qemu_bh_schedule(acb
->bh
);
587 static void curl_close(BlockDriverState
*bs
)
589 BDRVCURLState
*s
= bs
->opaque
;
592 DPRINTF("CURL: Close\n");
593 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
594 if (s
->states
[i
].in_use
)
595 curl_clean_state(&s
->states
[i
]);
596 if (s
->states
[i
].curl
) {
597 curl_easy_cleanup(s
->states
[i
].curl
);
598 s
->states
[i
].curl
= NULL
;
600 if (s
->states
[i
].orig_buf
) {
601 g_free(s
->states
[i
].orig_buf
);
602 s
->states
[i
].orig_buf
= NULL
;
606 curl_multi_cleanup(s
->multi
);
610 static int64_t curl_getlength(BlockDriverState
*bs
)
612 BDRVCURLState
*s
= bs
->opaque
;
616 static BlockDriver bdrv_http
= {
617 .format_name
= "http",
618 .protocol_name
= "http",
620 .instance_size
= sizeof(BDRVCURLState
),
621 .bdrv_parse_filename
= curl_parse_filename
,
622 .bdrv_file_open
= curl_open
,
623 .bdrv_close
= curl_close
,
624 .bdrv_getlength
= curl_getlength
,
626 .bdrv_aio_readv
= curl_aio_readv
,
629 static BlockDriver bdrv_https
= {
630 .format_name
= "https",
631 .protocol_name
= "https",
633 .instance_size
= sizeof(BDRVCURLState
),
634 .bdrv_parse_filename
= curl_parse_filename
,
635 .bdrv_file_open
= curl_open
,
636 .bdrv_close
= curl_close
,
637 .bdrv_getlength
= curl_getlength
,
639 .bdrv_aio_readv
= curl_aio_readv
,
642 static BlockDriver bdrv_ftp
= {
643 .format_name
= "ftp",
644 .protocol_name
= "ftp",
646 .instance_size
= sizeof(BDRVCURLState
),
647 .bdrv_parse_filename
= curl_parse_filename
,
648 .bdrv_file_open
= curl_open
,
649 .bdrv_close
= curl_close
,
650 .bdrv_getlength
= curl_getlength
,
652 .bdrv_aio_readv
= curl_aio_readv
,
655 static BlockDriver bdrv_ftps
= {
656 .format_name
= "ftps",
657 .protocol_name
= "ftps",
659 .instance_size
= sizeof(BDRVCURLState
),
660 .bdrv_parse_filename
= curl_parse_filename
,
661 .bdrv_file_open
= curl_open
,
662 .bdrv_close
= curl_close
,
663 .bdrv_getlength
= curl_getlength
,
665 .bdrv_aio_readv
= curl_aio_readv
,
668 static BlockDriver bdrv_tftp
= {
669 .format_name
= "tftp",
670 .protocol_name
= "tftp",
672 .instance_size
= sizeof(BDRVCURLState
),
673 .bdrv_parse_filename
= curl_parse_filename
,
674 .bdrv_file_open
= curl_open
,
675 .bdrv_close
= curl_close
,
676 .bdrv_getlength
= curl_getlength
,
678 .bdrv_aio_readv
= curl_aio_readv
,
681 static void curl_block_init(void)
683 bdrv_register(&bdrv_http
);
684 bdrv_register(&bdrv_https
);
685 bdrv_register(&bdrv_ftp
);
686 bdrv_register(&bdrv_ftps
);
687 bdrv_register(&bdrv_tftp
);
690 block_init(curl_block_init
);