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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/error-report.h"
28 #include "qemu/option.h"
29 #include "block/block_int.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qstring.h"
32 #include "crypto/secret.h"
33 #include <curl/curl.h>
34 #include "qemu/cutils.h"
37 // #define DEBUG_VERBOSE
40 #define DEBUG_CURL_PRINT 1
42 #define DEBUG_CURL_PRINT 0
44 #define DPRINTF(fmt, ...) \
46 if (DEBUG_CURL_PRINT) { \
47 fprintf(stderr, fmt, ## __VA_ARGS__); \
51 #if LIBCURL_VERSION_NUM >= 0x071000
52 /* The multi interface timer callback was introduced in 7.16.0 */
53 #define NEED_CURL_TIMER_CALLBACK
54 #define HAVE_SOCKET_ACTION
57 #ifndef HAVE_SOCKET_ACTION
58 /* If curl_multi_socket_action isn't available, define it statically here in
59 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
60 * less efficient but still safe. */
61 static CURLMcode
__curl_multi_socket_action(CURLM
*multi_handle
,
66 return curl_multi_socket(multi_handle
, sockfd
, running_handles
);
68 #define curl_multi_socket_action __curl_multi_socket_action
71 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
72 CURLPROTO_FTP | CURLPROTO_FTPS)
74 #define CURL_NUM_STATES 8
75 #define CURL_NUM_ACB 8
76 #define READ_AHEAD_DEFAULT (256 * 1024)
77 #define CURL_TIMEOUT_DEFAULT 5
78 #define CURL_TIMEOUT_MAX 10000
80 #define CURL_BLOCK_OPT_URL "url"
81 #define CURL_BLOCK_OPT_READAHEAD "readahead"
82 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
83 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
84 #define CURL_BLOCK_OPT_COOKIE "cookie"
85 #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
86 #define CURL_BLOCK_OPT_USERNAME "username"
87 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
88 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
89 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
93 static bool libcurl_initialized
;
95 typedef struct CURLAIOCB
{
107 typedef struct CURLSocket
{
109 QLIST_ENTRY(CURLSocket
) next
;
112 typedef struct CURLState
114 struct BDRVCURLState
*s
;
115 CURLAIOCB
*acb
[CURL_NUM_ACB
];
117 QLIST_HEAD(, CURLSocket
) sockets
;
123 char errmsg
[CURL_ERROR_SIZE
];
127 typedef struct BDRVCURLState
{
131 CURLState states
[CURL_NUM_STATES
];
133 size_t readahead_size
;
138 AioContext
*aio_context
;
140 CoQueue free_state_waitq
;
147 static void curl_clean_state(CURLState
*s
);
148 static void curl_multi_do(void *arg
);
149 static void curl_multi_read(void *arg
);
151 #ifdef NEED_CURL_TIMER_CALLBACK
152 /* Called from curl_multi_do_locked, with s->mutex held. */
153 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
155 BDRVCURLState
*s
= opaque
;
157 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms
);
158 if (timeout_ms
== -1) {
159 timer_del(&s
->timer
);
161 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
163 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
169 /* Called from curl_multi_do_locked, with s->mutex held. */
170 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
171 void *userp
, void *sp
)
174 CURLState
*state
= NULL
;
177 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
180 QLIST_FOREACH(socket
, &state
->sockets
, next
) {
181 if (socket
->fd
== fd
) {
182 if (action
== CURL_POLL_REMOVE
) {
183 QLIST_REMOVE(socket
, next
);
190 socket
= g_new0(CURLSocket
, 1);
192 QLIST_INSERT_HEAD(&state
->sockets
, socket
, next
);
196 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action
, (int)fd
);
199 aio_set_fd_handler(s
->aio_context
, fd
, false,
200 curl_multi_read
, NULL
, NULL
, state
);
203 aio_set_fd_handler(s
->aio_context
, fd
, false,
204 NULL
, curl_multi_do
, NULL
, state
);
206 case CURL_POLL_INOUT
:
207 aio_set_fd_handler(s
->aio_context
, fd
, false,
208 curl_multi_read
, curl_multi_do
, NULL
, state
);
210 case CURL_POLL_REMOVE
:
211 aio_set_fd_handler(s
->aio_context
, fd
, false,
212 NULL
, NULL
, NULL
, NULL
);
219 /* Called from curl_multi_do_locked, with s->mutex held. */
220 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
222 BDRVCURLState
*s
= opaque
;
223 size_t realsize
= size
* nmemb
;
224 const char *accept_line
= "Accept-Ranges: bytes";
226 if (realsize
>= strlen(accept_line
)
227 && strncmp((char *)ptr
, accept_line
, strlen(accept_line
)) == 0) {
228 s
->accept_range
= true;
234 /* Called from curl_multi_do_locked, with s->mutex held. */
235 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
237 CURLState
*s
= ((CURLState
*)opaque
);
238 size_t realsize
= size
* nmemb
;
241 DPRINTF("CURL: Just reading %zd bytes\n", realsize
);
243 if (!s
|| !s
->orig_buf
) {
247 if (s
->buf_off
>= s
->buf_len
) {
248 /* buffer full, read nothing */
251 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
252 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
253 s
->buf_off
+= realsize
;
255 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
256 CURLAIOCB
*acb
= s
->acb
[i
];
261 if ((s
->buf_off
>= acb
->end
)) {
262 size_t request_length
= acb
->bytes
;
264 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
265 acb
->end
- acb
->start
);
267 if (acb
->end
- acb
->start
< request_length
) {
268 size_t offset
= acb
->end
- acb
->start
;
269 qemu_iovec_memset(acb
->qiov
, offset
, 0,
270 request_length
- offset
);
275 qemu_mutex_unlock(&s
->s
->mutex
);
276 aio_co_wake(acb
->co
);
277 qemu_mutex_lock(&s
->s
->mutex
);
282 /* curl will error out if we do not return this value */
286 /* Called with s->mutex held. */
287 static bool curl_find_buf(BDRVCURLState
*s
, uint64_t start
, uint64_t len
,
291 uint64_t end
= start
+ len
;
292 uint64_t clamped_end
= MIN(end
, s
->len
);
293 uint64_t clamped_len
= clamped_end
- start
;
295 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
296 CURLState
*state
= &s
->states
[i
];
297 uint64_t buf_end
= (state
->buf_start
+ state
->buf_off
);
298 uint64_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
300 if (!state
->orig_buf
)
305 // Does the existing buffer cover our section?
306 if ((start
>= state
->buf_start
) &&
307 (start
<= buf_end
) &&
308 (clamped_end
>= state
->buf_start
) &&
309 (clamped_end
<= buf_end
))
311 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
313 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, clamped_len
);
314 if (clamped_len
< len
) {
315 qemu_iovec_memset(acb
->qiov
, clamped_len
, 0, len
- clamped_len
);
321 // Wait for unfinished chunks
323 (start
>= state
->buf_start
) &&
324 (start
<= buf_fend
) &&
325 (clamped_end
>= state
->buf_start
) &&
326 (clamped_end
<= buf_fend
))
330 acb
->start
= start
- state
->buf_start
;
331 acb
->end
= acb
->start
+ clamped_len
;
333 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
334 if (!state
->acb
[j
]) {
345 /* Called with s->mutex held. */
346 static void curl_multi_check_completion(BDRVCURLState
*s
)
350 /* Try to find done transfers, so we can free the easy
354 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
356 /* Quit when there are no more completions */
360 if (msg
->msg
== CURLMSG_DONE
) {
361 CURLState
*state
= NULL
;
362 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
365 /* ACBs for successful messages get completed in curl_read_cb */
366 if (msg
->data
.result
!= CURLE_OK
) {
368 static int errcount
= 100;
370 /* Don't lose the original error message from curl, since
371 * it contains extra data.
374 error_report("curl: %s", state
->errmsg
);
375 if (--errcount
== 0) {
376 error_report("curl: further errors suppressed");
380 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
381 CURLAIOCB
*acb
= state
->acb
[i
];
388 state
->acb
[i
] = NULL
;
389 qemu_mutex_unlock(&s
->mutex
);
390 aio_co_wake(acb
->co
);
391 qemu_mutex_lock(&s
->mutex
);
395 curl_clean_state(state
);
401 /* Called with s->mutex held. */
402 static void curl_multi_do_locked(CURLState
*s
)
404 CURLSocket
*socket
, *next_socket
;
412 /* Need to use _SAFE because curl_multi_socket_action() may trigger
413 * curl_sock_cb() which might modify this list */
414 QLIST_FOREACH_SAFE(socket
, &s
->sockets
, next
, next_socket
) {
416 r
= curl_multi_socket_action(s
->s
->multi
, socket
->fd
, 0, &running
);
417 } while (r
== CURLM_CALL_MULTI_PERFORM
);
421 static void curl_multi_do(void *arg
)
423 CURLState
*s
= (CURLState
*)arg
;
425 qemu_mutex_lock(&s
->s
->mutex
);
426 curl_multi_do_locked(s
);
427 qemu_mutex_unlock(&s
->s
->mutex
);
430 static void curl_multi_read(void *arg
)
432 CURLState
*s
= (CURLState
*)arg
;
434 qemu_mutex_lock(&s
->s
->mutex
);
435 curl_multi_do_locked(s
);
436 curl_multi_check_completion(s
->s
);
437 qemu_mutex_unlock(&s
->s
->mutex
);
440 static void curl_multi_timeout_do(void *arg
)
442 #ifdef NEED_CURL_TIMER_CALLBACK
443 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
450 qemu_mutex_lock(&s
->mutex
);
451 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
453 curl_multi_check_completion(s
);
454 qemu_mutex_unlock(&s
->mutex
);
460 /* Called with s->mutex held. */
461 static CURLState
*curl_find_state(BDRVCURLState
*s
)
463 CURLState
*state
= NULL
;
466 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
467 if (!s
->states
[i
].in_use
) {
468 state
= &s
->states
[i
];
476 static int curl_init_state(BDRVCURLState
*s
, CURLState
*state
)
479 state
->curl
= curl_easy_init();
483 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
484 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
485 (long) s
->sslverify
);
487 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
489 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
490 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
491 (void *)curl_read_cb
);
492 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
493 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
494 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
495 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
496 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
497 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
498 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
501 curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
);
504 curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
);
506 if (s
->proxyusername
) {
507 curl_easy_setopt(state
->curl
,
508 CURLOPT_PROXYUSERNAME
, s
->proxyusername
);
510 if (s
->proxypassword
) {
511 curl_easy_setopt(state
->curl
,
512 CURLOPT_PROXYPASSWORD
, s
->proxypassword
);
515 /* Restrict supported protocols to avoid security issues in the more
516 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
519 * Restricting protocols is only supported from 7.19.4 upwards.
521 #if LIBCURL_VERSION_NUM >= 0x071304
522 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
523 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
527 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
531 QLIST_INIT(&state
->sockets
);
537 /* Called with s->mutex held. */
538 static void curl_clean_state(CURLState
*s
)
541 for (j
= 0; j
< CURL_NUM_ACB
; j
++) {
546 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
548 while (!QLIST_EMPTY(&s
->sockets
)) {
549 CURLSocket
*socket
= QLIST_FIRST(&s
->sockets
);
551 QLIST_REMOVE(socket
, next
);
557 qemu_co_enter_next(&s
->s
->free_state_waitq
, &s
->s
->mutex
);
560 static void curl_parse_filename(const char *filename
, QDict
*options
,
563 qdict_put_str(options
, CURL_BLOCK_OPT_URL
, filename
);
566 static void curl_detach_aio_context(BlockDriverState
*bs
)
568 BDRVCURLState
*s
= bs
->opaque
;
571 qemu_mutex_lock(&s
->mutex
);
572 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
573 if (s
->states
[i
].in_use
) {
574 curl_clean_state(&s
->states
[i
]);
576 if (s
->states
[i
].curl
) {
577 curl_easy_cleanup(s
->states
[i
].curl
);
578 s
->states
[i
].curl
= NULL
;
580 g_free(s
->states
[i
].orig_buf
);
581 s
->states
[i
].orig_buf
= NULL
;
584 curl_multi_cleanup(s
->multi
);
587 qemu_mutex_unlock(&s
->mutex
);
589 timer_del(&s
->timer
);
592 static void curl_attach_aio_context(BlockDriverState
*bs
,
593 AioContext
*new_context
)
595 BDRVCURLState
*s
= bs
->opaque
;
597 aio_timer_init(new_context
, &s
->timer
,
598 QEMU_CLOCK_REALTIME
, SCALE_NS
,
599 curl_multi_timeout_do
, s
);
602 s
->multi
= curl_multi_init();
603 s
->aio_context
= new_context
;
604 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
605 #ifdef NEED_CURL_TIMER_CALLBACK
606 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
607 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
611 static QemuOptsList runtime_opts
= {
613 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
616 .name
= CURL_BLOCK_OPT_URL
,
617 .type
= QEMU_OPT_STRING
,
618 .help
= "URL to open",
621 .name
= CURL_BLOCK_OPT_READAHEAD
,
622 .type
= QEMU_OPT_SIZE
,
623 .help
= "Readahead size",
626 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
627 .type
= QEMU_OPT_BOOL
,
628 .help
= "Verify SSL certificate"
631 .name
= CURL_BLOCK_OPT_TIMEOUT
,
632 .type
= QEMU_OPT_NUMBER
,
633 .help
= "Curl timeout"
636 .name
= CURL_BLOCK_OPT_COOKIE
,
637 .type
= QEMU_OPT_STRING
,
638 .help
= "Pass the cookie or list of cookies with each request"
641 .name
= CURL_BLOCK_OPT_COOKIE_SECRET
,
642 .type
= QEMU_OPT_STRING
,
643 .help
= "ID of secret used as cookie passed with each request"
646 .name
= CURL_BLOCK_OPT_USERNAME
,
647 .type
= QEMU_OPT_STRING
,
648 .help
= "Username for HTTP auth"
651 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
652 .type
= QEMU_OPT_STRING
,
653 .help
= "ID of secret used as password for HTTP auth",
656 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
657 .type
= QEMU_OPT_STRING
,
658 .help
= "Username for HTTP proxy auth"
661 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
662 .type
= QEMU_OPT_STRING
,
663 .help
= "ID of secret used as password for HTTP proxy auth",
665 { /* end of list */ }
670 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
673 BDRVCURLState
*s
= bs
->opaque
;
674 CURLState
*state
= NULL
;
676 Error
*local_err
= NULL
;
679 const char *cookie_secret
;
681 const char *secretid
;
682 const char *protocol_delimiter
;
686 if (flags
& BDRV_O_RDWR
) {
687 error_setg(errp
, "curl block device does not support writes");
691 if (!libcurl_initialized
) {
692 ret
= curl_global_init(CURL_GLOBAL_ALL
);
694 error_setg(errp
, "libcurl initialization failed with %d", ret
);
697 libcurl_initialized
= true;
700 qemu_mutex_init(&s
->mutex
);
701 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
702 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
704 error_propagate(errp
, local_err
);
708 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
710 if ((s
->readahead_size
& 0x1ff) != 0) {
711 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
716 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
717 CURL_TIMEOUT_DEFAULT
);
718 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
719 error_setg(errp
, "timeout parameter is too large or negative");
723 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
, true);
725 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
726 cookie_secret
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE_SECRET
);
728 if (cookie
&& cookie_secret
) {
730 "curl driver cannot handle both cookie and cookie secret");
735 s
->cookie
= qcrypto_secret_lookup_as_utf8(cookie_secret
, errp
);
740 s
->cookie
= g_strdup(cookie
);
743 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
745 error_setg(errp
, "curl block driver requires an 'url' option");
749 if (!strstart(file
, bs
->drv
->protocol_name
, &protocol_delimiter
) ||
750 !strstart(protocol_delimiter
, "://", NULL
))
752 error_setg(errp
, "%s curl driver cannot handle the URL '%s' (does not "
753 "start with '%s://')", bs
->drv
->protocol_name
, file
,
754 bs
->drv
->protocol_name
);
758 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
759 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
762 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
768 s
->proxyusername
= g_strdup(
769 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
770 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
772 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
773 if (!s
->proxypassword
) {
778 DPRINTF("CURL: Opening %s\n", file
);
779 qemu_co_queue_init(&s
->free_state_waitq
);
780 s
->aio_context
= bdrv_get_aio_context(bs
);
781 s
->url
= g_strdup(file
);
782 qemu_mutex_lock(&s
->mutex
);
783 state
= curl_find_state(s
);
784 qemu_mutex_unlock(&s
->mutex
);
791 if (curl_init_state(s
, state
) < 0) {
795 s
->accept_range
= false;
796 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
797 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
799 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
800 if (curl_easy_perform(state
->curl
))
802 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
805 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
806 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
807 * known and zero if it is realy zero-length file. */
808 #if LIBCURL_VERSION_NUM >= 0x071304
810 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
811 "Server didn't report file size.");
816 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
817 "Unknown file size or zero-length file.");
824 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
825 || !strncasecmp(s
->url
, "https://", strlen("https://")))
826 && !s
->accept_range
) {
827 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
828 "Server does not support 'range' (byte ranges).");
831 DPRINTF("CURL: Size = %" PRIu64
"\n", s
->len
);
833 qemu_mutex_lock(&s
->mutex
);
834 curl_clean_state(state
);
835 qemu_mutex_unlock(&s
->mutex
);
836 curl_easy_cleanup(state
->curl
);
839 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
845 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
846 curl_easy_cleanup(state
->curl
);
849 qemu_mutex_destroy(&s
->mutex
);
853 g_free(s
->proxyusername
);
854 g_free(s
->proxypassword
);
859 static void curl_setup_preadv(BlockDriverState
*bs
, CURLAIOCB
*acb
)
864 BDRVCURLState
*s
= bs
->opaque
;
866 uint64_t start
= acb
->offset
;
869 qemu_mutex_lock(&s
->mutex
);
871 // In case we have the requested data already (e.g. read-ahead),
872 // we can just call the callback and be done.
873 if (curl_find_buf(s
, start
, acb
->bytes
, acb
)) {
877 // No cache found, so let's start a new request
879 state
= curl_find_state(s
);
883 qemu_co_queue_wait(&s
->free_state_waitq
, &s
->mutex
);
886 if (curl_init_state(s
, state
) < 0) {
887 curl_clean_state(state
);
893 acb
->end
= MIN(acb
->bytes
, s
->len
- start
);
896 g_free(state
->orig_buf
);
897 state
->buf_start
= start
;
898 state
->buf_len
= MIN(acb
->end
+ s
->readahead_size
, s
->len
- start
);
899 end
= start
+ state
->buf_len
- 1;
900 state
->orig_buf
= g_try_malloc(state
->buf_len
);
901 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
902 curl_clean_state(state
);
908 snprintf(state
->range
, 127, "%" PRIu64
"-%" PRIu64
, start
, end
);
909 DPRINTF("CURL (AIO): Reading %" PRIu64
" at %" PRIu64
" (%s)\n",
910 acb
->bytes
, start
, state
->range
);
911 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
913 curl_multi_add_handle(s
->multi
, state
->curl
);
915 /* Tell curl it needs to kick things off */
916 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
919 qemu_mutex_unlock(&s
->mutex
);
922 static int coroutine_fn
curl_co_preadv(BlockDriverState
*bs
,
923 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
926 .co
= qemu_coroutine_self(),
933 curl_setup_preadv(bs
, &acb
);
934 while (acb
.ret
== -EINPROGRESS
) {
935 qemu_coroutine_yield();
940 static void curl_close(BlockDriverState
*bs
)
942 BDRVCURLState
*s
= bs
->opaque
;
944 DPRINTF("CURL: Close\n");
945 curl_detach_aio_context(bs
);
946 qemu_mutex_destroy(&s
->mutex
);
951 g_free(s
->proxyusername
);
952 g_free(s
->proxypassword
);
955 static int64_t curl_getlength(BlockDriverState
*bs
)
957 BDRVCURLState
*s
= bs
->opaque
;
961 static BlockDriver bdrv_http
= {
962 .format_name
= "http",
963 .protocol_name
= "http",
965 .instance_size
= sizeof(BDRVCURLState
),
966 .bdrv_parse_filename
= curl_parse_filename
,
967 .bdrv_file_open
= curl_open
,
968 .bdrv_close
= curl_close
,
969 .bdrv_getlength
= curl_getlength
,
971 .bdrv_co_preadv
= curl_co_preadv
,
973 .bdrv_detach_aio_context
= curl_detach_aio_context
,
974 .bdrv_attach_aio_context
= curl_attach_aio_context
,
977 static BlockDriver bdrv_https
= {
978 .format_name
= "https",
979 .protocol_name
= "https",
981 .instance_size
= sizeof(BDRVCURLState
),
982 .bdrv_parse_filename
= curl_parse_filename
,
983 .bdrv_file_open
= curl_open
,
984 .bdrv_close
= curl_close
,
985 .bdrv_getlength
= curl_getlength
,
987 .bdrv_co_preadv
= curl_co_preadv
,
989 .bdrv_detach_aio_context
= curl_detach_aio_context
,
990 .bdrv_attach_aio_context
= curl_attach_aio_context
,
993 static BlockDriver bdrv_ftp
= {
994 .format_name
= "ftp",
995 .protocol_name
= "ftp",
997 .instance_size
= sizeof(BDRVCURLState
),
998 .bdrv_parse_filename
= curl_parse_filename
,
999 .bdrv_file_open
= curl_open
,
1000 .bdrv_close
= curl_close
,
1001 .bdrv_getlength
= curl_getlength
,
1003 .bdrv_co_preadv
= curl_co_preadv
,
1005 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1006 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1009 static BlockDriver bdrv_ftps
= {
1010 .format_name
= "ftps",
1011 .protocol_name
= "ftps",
1013 .instance_size
= sizeof(BDRVCURLState
),
1014 .bdrv_parse_filename
= curl_parse_filename
,
1015 .bdrv_file_open
= curl_open
,
1016 .bdrv_close
= curl_close
,
1017 .bdrv_getlength
= curl_getlength
,
1019 .bdrv_co_preadv
= curl_co_preadv
,
1021 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1022 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1025 static void curl_block_init(void)
1027 bdrv_register(&bdrv_http
);
1028 bdrv_register(&bdrv_https
);
1029 bdrv_register(&bdrv_ftp
);
1030 bdrv_register(&bdrv_ftps
);
1033 block_init(curl_block_init
);