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 "qapi/qmp/qbool.h"
27 #include <curl/curl.h>
30 // #define DEBUG_VERBOSE
33 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
35 #define DPRINTF(fmt, ...) do { } while (0)
38 #if LIBCURL_VERSION_NUM >= 0x071000
39 /* The multi interface timer callback was introduced in 7.16.0 */
40 #define NEED_CURL_TIMER_CALLBACK
41 #define HAVE_SOCKET_ACTION
44 #ifndef HAVE_SOCKET_ACTION
45 /* If curl_multi_socket_action isn't available, define it statically here in
46 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
47 * less efficient but still safe. */
48 static CURLMcode
__curl_multi_socket_action(CURLM
*multi_handle
,
53 return curl_multi_socket(multi_handle
, sockfd
, running_handles
);
55 #define curl_multi_socket_action __curl_multi_socket_action
58 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
59 CURLPROTO_FTP | CURLPROTO_FTPS | \
62 #define CURL_NUM_STATES 8
63 #define CURL_NUM_ACB 8
64 #define SECTOR_SIZE 512
65 #define READ_AHEAD_DEFAULT (256 * 1024)
66 #define CURL_TIMEOUT_DEFAULT 5
68 #define FIND_RET_NONE 0
70 #define FIND_RET_WAIT 2
72 #define CURL_BLOCK_OPT_URL "url"
73 #define CURL_BLOCK_OPT_READAHEAD "readahead"
74 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
75 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
76 #define CURL_BLOCK_OPT_COOKIE "cookie"
80 typedef struct CURLAIOCB
{
81 BlockDriverAIOCB common
;
92 typedef struct CURLState
94 struct BDRVCURLState
*s
;
95 CURLAIOCB
*acb
[CURL_NUM_ACB
];
97 curl_socket_t sock_fd
;
103 char errmsg
[CURL_ERROR_SIZE
];
107 typedef struct BDRVCURLState
{
111 CURLState states
[CURL_NUM_STATES
];
113 size_t readahead_size
;
118 AioContext
*aio_context
;
121 static void curl_clean_state(CURLState
*s
);
122 static void curl_multi_do(void *arg
);
123 static void curl_multi_read(void *arg
);
125 #ifdef NEED_CURL_TIMER_CALLBACK
126 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
128 BDRVCURLState
*s
= opaque
;
130 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms
);
131 if (timeout_ms
== -1) {
132 timer_del(&s
->timer
);
134 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
136 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
142 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
143 void *userp
, void *sp
)
146 CURLState
*state
= NULL
;
147 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
151 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action
, fd
);
154 aio_set_fd_handler(s
->aio_context
, fd
, curl_multi_read
,
158 aio_set_fd_handler(s
->aio_context
, fd
, NULL
, curl_multi_do
, state
);
160 case CURL_POLL_INOUT
:
161 aio_set_fd_handler(s
->aio_context
, fd
, curl_multi_read
,
162 curl_multi_do
, state
);
164 case CURL_POLL_REMOVE
:
165 aio_set_fd_handler(s
->aio_context
, fd
, NULL
, NULL
, NULL
);
172 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
174 BDRVCURLState
*s
= opaque
;
175 size_t realsize
= size
* nmemb
;
176 const char *accept_line
= "Accept-Ranges: bytes";
178 if (realsize
>= strlen(accept_line
)
179 && strncmp((char *)ptr
, accept_line
, strlen(accept_line
)) == 0) {
180 s
->accept_range
= true;
186 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
188 CURLState
*s
= ((CURLState
*)opaque
);
189 size_t realsize
= size
* nmemb
;
192 DPRINTF("CURL: Just reading %zd bytes\n", realsize
);
194 if (!s
|| !s
->orig_buf
)
197 if (s
->buf_off
>= s
->buf_len
) {
198 /* buffer full, read nothing */
201 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
202 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
203 s
->buf_off
+= realsize
;
205 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
206 CURLAIOCB
*acb
= s
->acb
[i
];
211 if ((s
->buf_off
>= acb
->end
)) {
212 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
213 acb
->end
- acb
->start
);
214 acb
->common
.cb(acb
->common
.opaque
, 0);
223 static int curl_find_buf(BDRVCURLState
*s
, size_t start
, size_t len
,
227 size_t end
= start
+ len
;
229 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
230 CURLState
*state
= &s
->states
[i
];
231 size_t buf_end
= (state
->buf_start
+ state
->buf_off
);
232 size_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
234 if (!state
->orig_buf
)
239 // Does the existing buffer cover our section?
240 if ((start
>= state
->buf_start
) &&
241 (start
<= buf_end
) &&
242 (end
>= state
->buf_start
) &&
245 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
247 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, len
);
248 acb
->common
.cb(acb
->common
.opaque
, 0);
253 // Wait for unfinished chunks
255 (start
>= state
->buf_start
) &&
256 (start
<= buf_fend
) &&
257 (end
>= state
->buf_start
) &&
262 acb
->start
= start
- state
->buf_start
;
263 acb
->end
= acb
->start
+ len
;
265 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
266 if (!state
->acb
[j
]) {
268 return FIND_RET_WAIT
;
274 return FIND_RET_NONE
;
277 static void curl_multi_check_completion(BDRVCURLState
*s
)
281 /* Try to find done transfers, so we can free the easy
285 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
287 /* Quit when there are no more completions */
291 if (msg
->msg
== CURLMSG_DONE
) {
292 CURLState
*state
= NULL
;
293 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
296 /* ACBs for successful messages get completed in curl_read_cb */
297 if (msg
->data
.result
!= CURLE_OK
) {
299 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
300 CURLAIOCB
*acb
= state
->acb
[i
];
306 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
308 state
->acb
[i
] = NULL
;
312 curl_clean_state(state
);
318 static void curl_multi_do(void *arg
)
320 CURLState
*s
= (CURLState
*)arg
;
329 r
= curl_multi_socket_action(s
->s
->multi
, s
->sock_fd
, 0, &running
);
330 } while(r
== CURLM_CALL_MULTI_PERFORM
);
334 static void curl_multi_read(void *arg
)
336 CURLState
*s
= (CURLState
*)arg
;
339 curl_multi_check_completion(s
->s
);
342 static void curl_multi_timeout_do(void *arg
)
344 #ifdef NEED_CURL_TIMER_CALLBACK
345 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
352 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
354 curl_multi_check_completion(s
);
360 static CURLState
*curl_init_state(BlockDriverState
*bs
, BDRVCURLState
*s
)
362 CURLState
*state
= NULL
;
366 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
367 for (j
=0; j
<CURL_NUM_ACB
; j
++)
368 if (s
->states
[i
].acb
[j
])
370 if (s
->states
[i
].in_use
)
373 state
= &s
->states
[i
];
378 aio_poll(bdrv_get_aio_context(bs
), true);
383 state
->curl
= curl_easy_init();
387 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
388 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
389 (long) s
->sslverify
);
391 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
393 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, s
->timeout
);
394 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
395 (void *)curl_read_cb
);
396 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
397 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
398 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
399 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
400 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
401 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
402 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
404 /* Restrict supported protocols to avoid security issues in the more
405 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
408 * Restricting protocols is only supported from 7.19.4 upwards.
410 #if LIBCURL_VERSION_NUM >= 0x071304
411 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
412 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
416 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
425 static void curl_clean_state(CURLState
*s
)
428 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
432 static void curl_parse_filename(const char *filename
, QDict
*options
,
435 qdict_put(options
, CURL_BLOCK_OPT_URL
, qstring_from_str(filename
));
438 static void curl_detach_aio_context(BlockDriverState
*bs
)
440 BDRVCURLState
*s
= bs
->opaque
;
443 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
444 if (s
->states
[i
].in_use
) {
445 curl_clean_state(&s
->states
[i
]);
447 if (s
->states
[i
].curl
) {
448 curl_easy_cleanup(s
->states
[i
].curl
);
449 s
->states
[i
].curl
= NULL
;
451 g_free(s
->states
[i
].orig_buf
);
452 s
->states
[i
].orig_buf
= NULL
;
455 curl_multi_cleanup(s
->multi
);
459 timer_del(&s
->timer
);
462 static void curl_attach_aio_context(BlockDriverState
*bs
,
463 AioContext
*new_context
)
465 BDRVCURLState
*s
= bs
->opaque
;
467 aio_timer_init(new_context
, &s
->timer
,
468 QEMU_CLOCK_REALTIME
, SCALE_NS
,
469 curl_multi_timeout_do
, s
);
472 s
->multi
= curl_multi_init();
473 s
->aio_context
= new_context
;
474 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
475 #ifdef NEED_CURL_TIMER_CALLBACK
476 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
477 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
481 static QemuOptsList runtime_opts
= {
483 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
486 .name
= CURL_BLOCK_OPT_URL
,
487 .type
= QEMU_OPT_STRING
,
488 .help
= "URL to open",
491 .name
= CURL_BLOCK_OPT_READAHEAD
,
492 .type
= QEMU_OPT_SIZE
,
493 .help
= "Readahead size",
496 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
497 .type
= QEMU_OPT_BOOL
,
498 .help
= "Verify SSL certificate"
501 .name
= CURL_BLOCK_OPT_TIMEOUT
,
502 .type
= QEMU_OPT_NUMBER
,
503 .help
= "Curl timeout"
506 .name
= CURL_BLOCK_OPT_COOKIE
,
507 .type
= QEMU_OPT_STRING
,
508 .help
= "Pass the cookie or list of cookies with each request"
510 { /* end of list */ }
514 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
517 BDRVCURLState
*s
= bs
->opaque
;
518 CURLState
*state
= NULL
;
520 Error
*local_err
= NULL
;
525 static int inited
= 0;
527 if (flags
& BDRV_O_RDWR
) {
528 error_setg(errp
, "curl block device does not support writes");
532 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
533 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
535 error_propagate(errp
, local_err
);
539 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
541 if ((s
->readahead_size
& 0x1ff) != 0) {
542 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
547 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
548 CURL_TIMEOUT_DEFAULT
);
550 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
, true);
552 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
553 s
->cookie
= g_strdup(cookie
);
555 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
557 error_setg(errp
, "curl block driver requires an 'url' option");
562 curl_global_init(CURL_GLOBAL_ALL
);
566 DPRINTF("CURL: Opening %s\n", file
);
567 s
->aio_context
= bdrv_get_aio_context(bs
);
568 s
->url
= g_strdup(file
);
569 state
= curl_init_state(bs
, s
);
575 s
->accept_range
= false;
576 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
577 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
579 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
580 if (curl_easy_perform(state
->curl
))
582 curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
);
587 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
588 || !strncasecmp(s
->url
, "https://", strlen("https://")))
589 && !s
->accept_range
) {
590 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
591 "Server does not support 'range' (byte ranges).");
594 DPRINTF("CURL: Size = %zd\n", s
->len
);
596 curl_clean_state(state
);
597 curl_easy_cleanup(state
->curl
);
600 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
606 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
607 curl_easy_cleanup(state
->curl
);
616 static const AIOCBInfo curl_aiocb_info
= {
617 .aiocb_size
= sizeof(CURLAIOCB
),
621 static void curl_readv_bh_cb(void *p
)
627 BDRVCURLState
*s
= acb
->common
.bs
->opaque
;
629 qemu_bh_delete(acb
->bh
);
632 size_t start
= acb
->sector_num
* SECTOR_SIZE
;
635 // In case we have the requested data already (e.g. read-ahead),
636 // we can just call the callback and be done.
637 switch (curl_find_buf(s
, start
, acb
->nb_sectors
* SECTOR_SIZE
, acb
)) {
647 // No cache found, so let's start a new request
648 state
= curl_init_state(acb
->common
.bs
, s
);
650 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
656 acb
->end
= (acb
->nb_sectors
* SECTOR_SIZE
);
659 g_free(state
->orig_buf
);
660 state
->buf_start
= start
;
661 state
->buf_len
= acb
->end
+ s
->readahead_size
;
662 end
= MIN(start
+ state
->buf_len
, s
->len
) - 1;
663 state
->orig_buf
= g_try_malloc(state
->buf_len
);
664 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
665 curl_clean_state(state
);
666 acb
->common
.cb(acb
->common
.opaque
, -ENOMEM
);
672 snprintf(state
->range
, 127, "%zd-%zd", start
, end
);
673 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
674 (acb
->nb_sectors
* SECTOR_SIZE
), start
, state
->range
);
675 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
677 curl_multi_add_handle(s
->multi
, state
->curl
);
679 /* Tell curl it needs to kick things off */
680 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
683 static BlockDriverAIOCB
*curl_aio_readv(BlockDriverState
*bs
,
684 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
685 BlockDriverCompletionFunc
*cb
, void *opaque
)
689 acb
= qemu_aio_get(&curl_aiocb_info
, bs
, cb
, opaque
);
692 acb
->sector_num
= sector_num
;
693 acb
->nb_sectors
= nb_sectors
;
695 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), curl_readv_bh_cb
, acb
);
696 qemu_bh_schedule(acb
->bh
);
700 static void curl_close(BlockDriverState
*bs
)
702 BDRVCURLState
*s
= bs
->opaque
;
704 DPRINTF("CURL: Close\n");
705 curl_detach_aio_context(bs
);
711 static int64_t curl_getlength(BlockDriverState
*bs
)
713 BDRVCURLState
*s
= bs
->opaque
;
717 static BlockDriver bdrv_http
= {
718 .format_name
= "http",
719 .protocol_name
= "http",
721 .instance_size
= sizeof(BDRVCURLState
),
722 .bdrv_parse_filename
= curl_parse_filename
,
723 .bdrv_file_open
= curl_open
,
724 .bdrv_close
= curl_close
,
725 .bdrv_getlength
= curl_getlength
,
727 .bdrv_aio_readv
= curl_aio_readv
,
729 .bdrv_detach_aio_context
= curl_detach_aio_context
,
730 .bdrv_attach_aio_context
= curl_attach_aio_context
,
733 static BlockDriver bdrv_https
= {
734 .format_name
= "https",
735 .protocol_name
= "https",
737 .instance_size
= sizeof(BDRVCURLState
),
738 .bdrv_parse_filename
= curl_parse_filename
,
739 .bdrv_file_open
= curl_open
,
740 .bdrv_close
= curl_close
,
741 .bdrv_getlength
= curl_getlength
,
743 .bdrv_aio_readv
= curl_aio_readv
,
745 .bdrv_detach_aio_context
= curl_detach_aio_context
,
746 .bdrv_attach_aio_context
= curl_attach_aio_context
,
749 static BlockDriver bdrv_ftp
= {
750 .format_name
= "ftp",
751 .protocol_name
= "ftp",
753 .instance_size
= sizeof(BDRVCURLState
),
754 .bdrv_parse_filename
= curl_parse_filename
,
755 .bdrv_file_open
= curl_open
,
756 .bdrv_close
= curl_close
,
757 .bdrv_getlength
= curl_getlength
,
759 .bdrv_aio_readv
= curl_aio_readv
,
761 .bdrv_detach_aio_context
= curl_detach_aio_context
,
762 .bdrv_attach_aio_context
= curl_attach_aio_context
,
765 static BlockDriver bdrv_ftps
= {
766 .format_name
= "ftps",
767 .protocol_name
= "ftps",
769 .instance_size
= sizeof(BDRVCURLState
),
770 .bdrv_parse_filename
= curl_parse_filename
,
771 .bdrv_file_open
= curl_open
,
772 .bdrv_close
= curl_close
,
773 .bdrv_getlength
= curl_getlength
,
775 .bdrv_aio_readv
= curl_aio_readv
,
777 .bdrv_detach_aio_context
= curl_detach_aio_context
,
778 .bdrv_attach_aio_context
= curl_attach_aio_context
,
781 static BlockDriver bdrv_tftp
= {
782 .format_name
= "tftp",
783 .protocol_name
= "tftp",
785 .instance_size
= sizeof(BDRVCURLState
),
786 .bdrv_parse_filename
= curl_parse_filename
,
787 .bdrv_file_open
= curl_open
,
788 .bdrv_close
= curl_close
,
789 .bdrv_getlength
= curl_getlength
,
791 .bdrv_aio_readv
= curl_aio_readv
,
793 .bdrv_detach_aio_context
= curl_detach_aio_context
,
794 .bdrv_attach_aio_context
= curl_attach_aio_context
,
797 static void curl_block_init(void)
799 bdrv_register(&bdrv_http
);
800 bdrv_register(&bdrv_https
);
801 bdrv_register(&bdrv_ftp
);
802 bdrv_register(&bdrv_ftps
);
803 bdrv_register(&bdrv_tftp
);
806 block_init(curl_block_init
);