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 "qemu/error-report.h"
26 #include "block/block_int.h"
27 #include "qapi/qmp/qbool.h"
28 #include "qapi/qmp/qstring.h"
29 #include <curl/curl.h>
32 // #define DEBUG_VERBOSE
35 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
37 #define DPRINTF(fmt, ...) do { } while (0)
40 #if LIBCURL_VERSION_NUM >= 0x071000
41 /* The multi interface timer callback was introduced in 7.16.0 */
42 #define NEED_CURL_TIMER_CALLBACK
43 #define HAVE_SOCKET_ACTION
46 #ifndef HAVE_SOCKET_ACTION
47 /* If curl_multi_socket_action isn't available, define it statically here in
48 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
49 * less efficient but still safe. */
50 static CURLMcode
__curl_multi_socket_action(CURLM
*multi_handle
,
55 return curl_multi_socket(multi_handle
, sockfd
, running_handles
);
57 #define curl_multi_socket_action __curl_multi_socket_action
60 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
61 CURLPROTO_FTP | CURLPROTO_FTPS | \
64 #define CURL_NUM_STATES 8
65 #define CURL_NUM_ACB 8
66 #define SECTOR_SIZE 512
67 #define READ_AHEAD_DEFAULT (256 * 1024)
68 #define CURL_TIMEOUT_DEFAULT 5
69 #define CURL_TIMEOUT_MAX 10000
71 #define FIND_RET_NONE 0
73 #define FIND_RET_WAIT 2
75 #define CURL_BLOCK_OPT_URL "url"
76 #define CURL_BLOCK_OPT_READAHEAD "readahead"
77 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
78 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
79 #define CURL_BLOCK_OPT_COOKIE "cookie"
83 typedef struct CURLAIOCB
{
95 typedef struct CURLState
97 struct BDRVCURLState
*s
;
98 CURLAIOCB
*acb
[CURL_NUM_ACB
];
100 curl_socket_t sock_fd
;
106 char errmsg
[CURL_ERROR_SIZE
];
110 typedef struct BDRVCURLState
{
114 CURLState states
[CURL_NUM_STATES
];
116 size_t readahead_size
;
121 AioContext
*aio_context
;
124 static void curl_clean_state(CURLState
*s
);
125 static void curl_multi_do(void *arg
);
126 static void curl_multi_read(void *arg
);
128 #ifdef NEED_CURL_TIMER_CALLBACK
129 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
131 BDRVCURLState
*s
= opaque
;
133 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms
);
134 if (timeout_ms
== -1) {
135 timer_del(&s
->timer
);
137 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
139 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
145 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
146 void *userp
, void *sp
)
149 CURLState
*state
= NULL
;
150 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
154 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action
, fd
);
157 aio_set_fd_handler(s
->aio_context
, fd
, curl_multi_read
,
161 aio_set_fd_handler(s
->aio_context
, fd
, NULL
, curl_multi_do
, state
);
163 case CURL_POLL_INOUT
:
164 aio_set_fd_handler(s
->aio_context
, fd
, curl_multi_read
,
165 curl_multi_do
, state
);
167 case CURL_POLL_REMOVE
:
168 aio_set_fd_handler(s
->aio_context
, fd
, NULL
, NULL
, NULL
);
175 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
177 BDRVCURLState
*s
= opaque
;
178 size_t realsize
= size
* nmemb
;
179 const char *accept_line
= "Accept-Ranges: bytes";
181 if (realsize
>= strlen(accept_line
)
182 && strncmp((char *)ptr
, accept_line
, strlen(accept_line
)) == 0) {
183 s
->accept_range
= true;
189 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
191 CURLState
*s
= ((CURLState
*)opaque
);
192 size_t realsize
= size
* nmemb
;
195 DPRINTF("CURL: Just reading %zd bytes\n", realsize
);
197 if (!s
|| !s
->orig_buf
)
200 if (s
->buf_off
>= s
->buf_len
) {
201 /* buffer full, read nothing */
204 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
205 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
206 s
->buf_off
+= realsize
;
208 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
209 CURLAIOCB
*acb
= s
->acb
[i
];
214 if ((s
->buf_off
>= acb
->end
)) {
215 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
216 acb
->end
- acb
->start
);
217 acb
->common
.cb(acb
->common
.opaque
, 0);
226 static int curl_find_buf(BDRVCURLState
*s
, size_t start
, size_t len
,
230 size_t end
= start
+ len
;
232 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
233 CURLState
*state
= &s
->states
[i
];
234 size_t buf_end
= (state
->buf_start
+ state
->buf_off
);
235 size_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
237 if (!state
->orig_buf
)
242 // Does the existing buffer cover our section?
243 if ((start
>= state
->buf_start
) &&
244 (start
<= buf_end
) &&
245 (end
>= state
->buf_start
) &&
248 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
250 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, len
);
251 acb
->common
.cb(acb
->common
.opaque
, 0);
256 // Wait for unfinished chunks
258 (start
>= state
->buf_start
) &&
259 (start
<= buf_fend
) &&
260 (end
>= state
->buf_start
) &&
265 acb
->start
= start
- state
->buf_start
;
266 acb
->end
= acb
->start
+ len
;
268 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
269 if (!state
->acb
[j
]) {
271 return FIND_RET_WAIT
;
277 return FIND_RET_NONE
;
280 static void curl_multi_check_completion(BDRVCURLState
*s
)
284 /* Try to find done transfers, so we can free the easy
288 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
290 /* Quit when there are no more completions */
294 if (msg
->msg
== CURLMSG_DONE
) {
295 CURLState
*state
= NULL
;
296 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
299 /* ACBs for successful messages get completed in curl_read_cb */
300 if (msg
->data
.result
!= CURLE_OK
) {
302 static int errcount
= 100;
304 /* Don't lose the original error message from curl, since
305 * it contains extra data.
308 error_report("curl: %s", state
->errmsg
);
309 if (--errcount
== 0) {
310 error_report("curl: further errors suppressed");
314 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
315 CURLAIOCB
*acb
= state
->acb
[i
];
321 acb
->common
.cb(acb
->common
.opaque
, -EPROTO
);
323 state
->acb
[i
] = NULL
;
327 curl_clean_state(state
);
333 static void curl_multi_do(void *arg
)
335 CURLState
*s
= (CURLState
*)arg
;
344 r
= curl_multi_socket_action(s
->s
->multi
, s
->sock_fd
, 0, &running
);
345 } while(r
== CURLM_CALL_MULTI_PERFORM
);
349 static void curl_multi_read(void *arg
)
351 CURLState
*s
= (CURLState
*)arg
;
354 curl_multi_check_completion(s
->s
);
357 static void curl_multi_timeout_do(void *arg
)
359 #ifdef NEED_CURL_TIMER_CALLBACK
360 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
367 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
369 curl_multi_check_completion(s
);
375 static CURLState
*curl_init_state(BlockDriverState
*bs
, BDRVCURLState
*s
)
377 CURLState
*state
= NULL
;
381 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
382 for (j
=0; j
<CURL_NUM_ACB
; j
++)
383 if (s
->states
[i
].acb
[j
])
385 if (s
->states
[i
].in_use
)
388 state
= &s
->states
[i
];
393 aio_poll(bdrv_get_aio_context(bs
), true);
398 state
->curl
= curl_easy_init();
402 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
403 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
404 (long) s
->sslverify
);
406 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
408 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
409 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
410 (void *)curl_read_cb
);
411 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
412 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
413 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
414 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
415 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
416 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
417 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
419 /* Restrict supported protocols to avoid security issues in the more
420 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
423 * Restricting protocols is only supported from 7.19.4 upwards.
425 #if LIBCURL_VERSION_NUM >= 0x071304
426 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
427 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
431 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
440 static void curl_clean_state(CURLState
*s
)
443 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
447 static void curl_parse_filename(const char *filename
, QDict
*options
,
450 qdict_put(options
, CURL_BLOCK_OPT_URL
, qstring_from_str(filename
));
453 static void curl_detach_aio_context(BlockDriverState
*bs
)
455 BDRVCURLState
*s
= bs
->opaque
;
458 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
459 if (s
->states
[i
].in_use
) {
460 curl_clean_state(&s
->states
[i
]);
462 if (s
->states
[i
].curl
) {
463 curl_easy_cleanup(s
->states
[i
].curl
);
464 s
->states
[i
].curl
= NULL
;
466 g_free(s
->states
[i
].orig_buf
);
467 s
->states
[i
].orig_buf
= NULL
;
470 curl_multi_cleanup(s
->multi
);
474 timer_del(&s
->timer
);
477 static void curl_attach_aio_context(BlockDriverState
*bs
,
478 AioContext
*new_context
)
480 BDRVCURLState
*s
= bs
->opaque
;
482 aio_timer_init(new_context
, &s
->timer
,
483 QEMU_CLOCK_REALTIME
, SCALE_NS
,
484 curl_multi_timeout_do
, s
);
487 s
->multi
= curl_multi_init();
488 s
->aio_context
= new_context
;
489 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
490 #ifdef NEED_CURL_TIMER_CALLBACK
491 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
492 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
496 static QemuOptsList runtime_opts
= {
498 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
501 .name
= CURL_BLOCK_OPT_URL
,
502 .type
= QEMU_OPT_STRING
,
503 .help
= "URL to open",
506 .name
= CURL_BLOCK_OPT_READAHEAD
,
507 .type
= QEMU_OPT_SIZE
,
508 .help
= "Readahead size",
511 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
512 .type
= QEMU_OPT_BOOL
,
513 .help
= "Verify SSL certificate"
516 .name
= CURL_BLOCK_OPT_TIMEOUT
,
517 .type
= QEMU_OPT_NUMBER
,
518 .help
= "Curl timeout"
521 .name
= CURL_BLOCK_OPT_COOKIE
,
522 .type
= QEMU_OPT_STRING
,
523 .help
= "Pass the cookie or list of cookies with each request"
525 { /* end of list */ }
529 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
532 BDRVCURLState
*s
= bs
->opaque
;
533 CURLState
*state
= NULL
;
535 Error
*local_err
= NULL
;
540 static int inited
= 0;
542 if (flags
& BDRV_O_RDWR
) {
543 error_setg(errp
, "curl block device does not support writes");
547 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
548 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
550 error_propagate(errp
, local_err
);
554 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
556 if ((s
->readahead_size
& 0x1ff) != 0) {
557 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
562 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
563 CURL_TIMEOUT_DEFAULT
);
564 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
565 error_setg(errp
, "timeout parameter is too large or negative");
569 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
, true);
571 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
572 s
->cookie
= g_strdup(cookie
);
574 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
576 error_setg(errp
, "curl block driver requires an 'url' option");
581 curl_global_init(CURL_GLOBAL_ALL
);
585 DPRINTF("CURL: Opening %s\n", file
);
586 s
->aio_context
= bdrv_get_aio_context(bs
);
587 s
->url
= g_strdup(file
);
588 state
= curl_init_state(bs
, s
);
594 s
->accept_range
= false;
595 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
596 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
598 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
599 if (curl_easy_perform(state
->curl
))
601 curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
);
606 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
607 || !strncasecmp(s
->url
, "https://", strlen("https://")))
608 && !s
->accept_range
) {
609 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
610 "Server does not support 'range' (byte ranges).");
613 DPRINTF("CURL: Size = %zd\n", s
->len
);
615 curl_clean_state(state
);
616 curl_easy_cleanup(state
->curl
);
619 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
625 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
626 curl_easy_cleanup(state
->curl
);
635 static const AIOCBInfo curl_aiocb_info
= {
636 .aiocb_size
= sizeof(CURLAIOCB
),
640 static void curl_readv_bh_cb(void *p
)
646 BDRVCURLState
*s
= acb
->common
.bs
->opaque
;
648 qemu_bh_delete(acb
->bh
);
651 size_t start
= acb
->sector_num
* SECTOR_SIZE
;
654 // In case we have the requested data already (e.g. read-ahead),
655 // we can just call the callback and be done.
656 switch (curl_find_buf(s
, start
, acb
->nb_sectors
* SECTOR_SIZE
, acb
)) {
666 // No cache found, so let's start a new request
667 state
= curl_init_state(acb
->common
.bs
, s
);
669 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
675 acb
->end
= (acb
->nb_sectors
* SECTOR_SIZE
);
678 g_free(state
->orig_buf
);
679 state
->buf_start
= start
;
680 state
->buf_len
= acb
->end
+ s
->readahead_size
;
681 end
= MIN(start
+ state
->buf_len
, s
->len
) - 1;
682 state
->orig_buf
= g_try_malloc(state
->buf_len
);
683 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
684 curl_clean_state(state
);
685 acb
->common
.cb(acb
->common
.opaque
, -ENOMEM
);
691 snprintf(state
->range
, 127, "%zd-%zd", start
, end
);
692 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
693 (acb
->nb_sectors
* SECTOR_SIZE
), start
, state
->range
);
694 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
696 curl_multi_add_handle(s
->multi
, state
->curl
);
698 /* Tell curl it needs to kick things off */
699 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
702 static BlockAIOCB
*curl_aio_readv(BlockDriverState
*bs
,
703 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
704 BlockCompletionFunc
*cb
, void *opaque
)
708 acb
= qemu_aio_get(&curl_aiocb_info
, bs
, cb
, opaque
);
711 acb
->sector_num
= sector_num
;
712 acb
->nb_sectors
= nb_sectors
;
714 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), curl_readv_bh_cb
, acb
);
715 qemu_bh_schedule(acb
->bh
);
719 static void curl_close(BlockDriverState
*bs
)
721 BDRVCURLState
*s
= bs
->opaque
;
723 DPRINTF("CURL: Close\n");
724 curl_detach_aio_context(bs
);
730 static int64_t curl_getlength(BlockDriverState
*bs
)
732 BDRVCURLState
*s
= bs
->opaque
;
736 static BlockDriver bdrv_http
= {
737 .format_name
= "http",
738 .protocol_name
= "http",
740 .instance_size
= sizeof(BDRVCURLState
),
741 .bdrv_parse_filename
= curl_parse_filename
,
742 .bdrv_file_open
= curl_open
,
743 .bdrv_close
= curl_close
,
744 .bdrv_getlength
= curl_getlength
,
746 .bdrv_aio_readv
= curl_aio_readv
,
748 .bdrv_detach_aio_context
= curl_detach_aio_context
,
749 .bdrv_attach_aio_context
= curl_attach_aio_context
,
752 static BlockDriver bdrv_https
= {
753 .format_name
= "https",
754 .protocol_name
= "https",
756 .instance_size
= sizeof(BDRVCURLState
),
757 .bdrv_parse_filename
= curl_parse_filename
,
758 .bdrv_file_open
= curl_open
,
759 .bdrv_close
= curl_close
,
760 .bdrv_getlength
= curl_getlength
,
762 .bdrv_aio_readv
= curl_aio_readv
,
764 .bdrv_detach_aio_context
= curl_detach_aio_context
,
765 .bdrv_attach_aio_context
= curl_attach_aio_context
,
768 static BlockDriver bdrv_ftp
= {
769 .format_name
= "ftp",
770 .protocol_name
= "ftp",
772 .instance_size
= sizeof(BDRVCURLState
),
773 .bdrv_parse_filename
= curl_parse_filename
,
774 .bdrv_file_open
= curl_open
,
775 .bdrv_close
= curl_close
,
776 .bdrv_getlength
= curl_getlength
,
778 .bdrv_aio_readv
= curl_aio_readv
,
780 .bdrv_detach_aio_context
= curl_detach_aio_context
,
781 .bdrv_attach_aio_context
= curl_attach_aio_context
,
784 static BlockDriver bdrv_ftps
= {
785 .format_name
= "ftps",
786 .protocol_name
= "ftps",
788 .instance_size
= sizeof(BDRVCURLState
),
789 .bdrv_parse_filename
= curl_parse_filename
,
790 .bdrv_file_open
= curl_open
,
791 .bdrv_close
= curl_close
,
792 .bdrv_getlength
= curl_getlength
,
794 .bdrv_aio_readv
= curl_aio_readv
,
796 .bdrv_detach_aio_context
= curl_detach_aio_context
,
797 .bdrv_attach_aio_context
= curl_attach_aio_context
,
800 static BlockDriver bdrv_tftp
= {
801 .format_name
= "tftp",
802 .protocol_name
= "tftp",
804 .instance_size
= sizeof(BDRVCURLState
),
805 .bdrv_parse_filename
= curl_parse_filename
,
806 .bdrv_file_open
= curl_open
,
807 .bdrv_close
= curl_close
,
808 .bdrv_getlength
= curl_getlength
,
810 .bdrv_aio_readv
= curl_aio_readv
,
812 .bdrv_detach_aio_context
= curl_detach_aio_context
,
813 .bdrv_attach_aio_context
= curl_attach_aio_context
,
816 static void curl_block_init(void)
818 bdrv_register(&bdrv_http
);
819 bdrv_register(&bdrv_https
);
820 bdrv_register(&bdrv_ftp
);
821 bdrv_register(&bdrv_ftps
);
822 bdrv_register(&bdrv_tftp
);
825 block_init(curl_block_init
);