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/module.h"
29 #include "qemu/option.h"
30 #include "block/block_int.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qstring.h"
33 #include "crypto/secret.h"
34 #include <curl/curl.h>
35 #include "qemu/cutils.h"
38 // #define DEBUG_VERBOSE
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)
63 #define CURL_NUM_STATES 8
64 #define CURL_NUM_ACB 8
65 #define CURL_TIMEOUT_MAX 10000
67 #define CURL_BLOCK_OPT_URL "url"
68 #define CURL_BLOCK_OPT_READAHEAD "readahead"
69 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
70 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
71 #define CURL_BLOCK_OPT_COOKIE "cookie"
72 #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
73 #define CURL_BLOCK_OPT_USERNAME "username"
74 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
75 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
76 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
78 #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
79 #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
80 #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
85 static bool libcurl_initialized
;
87 typedef struct CURLAIOCB
{
99 typedef struct CURLSocket
{
101 struct CURLState
*state
;
102 QLIST_ENTRY(CURLSocket
) next
;
105 typedef struct CURLState
107 struct BDRVCURLState
*s
;
108 CURLAIOCB
*acb
[CURL_NUM_ACB
];
110 QLIST_HEAD(, CURLSocket
) sockets
;
116 char errmsg
[CURL_ERROR_SIZE
];
120 typedef struct BDRVCURLState
{
124 CURLState states
[CURL_NUM_STATES
];
126 size_t readahead_size
;
131 AioContext
*aio_context
;
133 CoQueue free_state_waitq
;
140 static void curl_clean_state(CURLState
*s
);
141 static void curl_multi_do(void *arg
);
143 #ifdef NEED_CURL_TIMER_CALLBACK
144 /* Called from curl_multi_do_locked, with s->mutex held. */
145 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
147 BDRVCURLState
*s
= opaque
;
149 trace_curl_timer_cb(timeout_ms
);
150 if (timeout_ms
== -1) {
151 timer_del(&s
->timer
);
153 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
155 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
161 /* Called from curl_multi_do_locked, with s->mutex held. */
162 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
163 void *userp
, void *sp
)
166 CURLState
*state
= NULL
;
169 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
172 QLIST_FOREACH(socket
, &state
->sockets
, next
) {
173 if (socket
->fd
== fd
) {
178 socket
= g_new0(CURLSocket
, 1);
180 socket
->state
= state
;
181 QLIST_INSERT_HEAD(&state
->sockets
, socket
, next
);
184 trace_curl_sock_cb(action
, (int)fd
);
187 aio_set_fd_handler(s
->aio_context
, fd
, false,
188 curl_multi_do
, NULL
, NULL
, socket
);
191 aio_set_fd_handler(s
->aio_context
, fd
, false,
192 NULL
, curl_multi_do
, NULL
, socket
);
194 case CURL_POLL_INOUT
:
195 aio_set_fd_handler(s
->aio_context
, fd
, false,
196 curl_multi_do
, curl_multi_do
, NULL
, socket
);
198 case CURL_POLL_REMOVE
:
199 aio_set_fd_handler(s
->aio_context
, fd
, false,
200 NULL
, NULL
, NULL
, NULL
);
204 if (action
== CURL_POLL_REMOVE
) {
205 QLIST_REMOVE(socket
, next
);
212 /* Called from curl_multi_do_locked, with s->mutex held. */
213 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
215 BDRVCURLState
*s
= opaque
;
216 size_t realsize
= size
* nmemb
;
217 const char *header
= (char *)ptr
;
218 const char *end
= header
+ realsize
;
219 const char *accept_ranges
= "accept-ranges:";
220 const char *bytes
= "bytes";
222 if (realsize
>= strlen(accept_ranges
)
223 && g_ascii_strncasecmp(header
, accept_ranges
,
224 strlen(accept_ranges
)) == 0) {
226 char *p
= strchr(header
, ':') + 1;
228 /* Skip whitespace between the header name and value. */
229 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
233 if (end
- p
>= strlen(bytes
)
234 && strncmp(p
, bytes
, strlen(bytes
)) == 0) {
236 /* Check that there is nothing but whitespace after the value. */
238 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
242 if (p
== end
|| !*p
) {
243 s
->accept_range
= true;
251 /* Called from curl_multi_do_locked, with s->mutex held. */
252 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
254 CURLState
*s
= ((CURLState
*)opaque
);
255 size_t realsize
= size
* nmemb
;
257 trace_curl_read_cb(realsize
);
259 if (!s
|| !s
->orig_buf
) {
263 if (s
->buf_off
>= s
->buf_len
) {
264 /* buffer full, read nothing */
267 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
268 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
269 s
->buf_off
+= realsize
;
272 /* curl will error out if we do not return this value */
276 /* Called with s->mutex held. */
277 static bool curl_find_buf(BDRVCURLState
*s
, uint64_t start
, uint64_t len
,
281 uint64_t end
= start
+ len
;
282 uint64_t clamped_end
= MIN(end
, s
->len
);
283 uint64_t clamped_len
= clamped_end
- start
;
285 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
286 CURLState
*state
= &s
->states
[i
];
287 uint64_t buf_end
= (state
->buf_start
+ state
->buf_off
);
288 uint64_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
290 if (!state
->orig_buf
)
295 // Does the existing buffer cover our section?
296 if ((start
>= state
->buf_start
) &&
297 (start
<= buf_end
) &&
298 (clamped_end
>= state
->buf_start
) &&
299 (clamped_end
<= buf_end
))
301 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
303 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, clamped_len
);
304 if (clamped_len
< len
) {
305 qemu_iovec_memset(acb
->qiov
, clamped_len
, 0, len
- clamped_len
);
311 // Wait for unfinished chunks
313 (start
>= state
->buf_start
) &&
314 (start
<= buf_fend
) &&
315 (clamped_end
>= state
->buf_start
) &&
316 (clamped_end
<= buf_fend
))
320 acb
->start
= start
- state
->buf_start
;
321 acb
->end
= acb
->start
+ clamped_len
;
323 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
324 if (!state
->acb
[j
]) {
335 /* Called with s->mutex held. */
336 static void curl_multi_check_completion(BDRVCURLState
*s
)
340 /* Try to find done transfers, so we can free the easy
344 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
346 /* Quit when there are no more completions */
350 if (msg
->msg
== CURLMSG_DONE
) {
352 CURLState
*state
= NULL
;
353 bool error
= msg
->data
.result
!= CURLE_OK
;
355 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
359 static int errcount
= 100;
361 /* Don't lose the original error message from curl, since
362 * it contains extra data.
365 error_report("curl: %s", state
->errmsg
);
366 if (--errcount
== 0) {
367 error_report("curl: further errors suppressed");
372 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
373 CURLAIOCB
*acb
= state
->acb
[i
];
380 /* Assert that we have read all data */
381 assert(state
->buf_off
>= acb
->end
);
383 qemu_iovec_from_buf(acb
->qiov
, 0,
384 state
->orig_buf
+ acb
->start
,
385 acb
->end
- acb
->start
);
387 if (acb
->end
- acb
->start
< acb
->bytes
) {
388 size_t offset
= acb
->end
- acb
->start
;
389 qemu_iovec_memset(acb
->qiov
, offset
, 0,
390 acb
->bytes
- offset
);
394 acb
->ret
= error
? -EIO
: 0;
395 state
->acb
[i
] = NULL
;
396 qemu_mutex_unlock(&s
->mutex
);
397 aio_co_wake(acb
->co
);
398 qemu_mutex_lock(&s
->mutex
);
401 curl_clean_state(state
);
407 /* Called with s->mutex held. */
408 static void curl_multi_do_locked(CURLSocket
*socket
)
410 BDRVCURLState
*s
= socket
->state
->s
;
419 r
= curl_multi_socket_action(s
->multi
, socket
->fd
, 0, &running
);
420 } while (r
== CURLM_CALL_MULTI_PERFORM
);
423 static void curl_multi_do(void *arg
)
425 CURLSocket
*socket
= arg
;
426 BDRVCURLState
*s
= socket
->state
->s
;
428 qemu_mutex_lock(&s
->mutex
);
429 curl_multi_do_locked(socket
);
430 curl_multi_check_completion(s
);
431 qemu_mutex_unlock(&s
->mutex
);
434 static void curl_multi_timeout_do(void *arg
)
436 #ifdef NEED_CURL_TIMER_CALLBACK
437 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
444 qemu_mutex_lock(&s
->mutex
);
445 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
447 curl_multi_check_completion(s
);
448 qemu_mutex_unlock(&s
->mutex
);
454 /* Called with s->mutex held. */
455 static CURLState
*curl_find_state(BDRVCURLState
*s
)
457 CURLState
*state
= NULL
;
460 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
461 if (!s
->states
[i
].in_use
) {
462 state
= &s
->states
[i
];
470 static int curl_init_state(BDRVCURLState
*s
, CURLState
*state
)
473 state
->curl
= curl_easy_init();
477 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
478 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
479 (long) s
->sslverify
);
480 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYHOST
,
481 s
->sslverify
? 2L : 0L);
483 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
485 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
486 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
487 (void *)curl_read_cb
);
488 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
489 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
490 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
491 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
492 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
493 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
494 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
497 curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
);
500 curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
);
502 if (s
->proxyusername
) {
503 curl_easy_setopt(state
->curl
,
504 CURLOPT_PROXYUSERNAME
, s
->proxyusername
);
506 if (s
->proxypassword
) {
507 curl_easy_setopt(state
->curl
,
508 CURLOPT_PROXYPASSWORD
, s
->proxypassword
);
511 /* Restrict supported protocols to avoid security issues in the more
512 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
515 * Restricting protocols is only supported from 7.19.4 upwards.
517 #if LIBCURL_VERSION_NUM >= 0x071304
518 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
519 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
523 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
527 QLIST_INIT(&state
->sockets
);
533 /* Called with s->mutex held. */
534 static void curl_clean_state(CURLState
*s
)
537 for (j
= 0; j
< CURL_NUM_ACB
; j
++) {
542 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
544 while (!QLIST_EMPTY(&s
->sockets
)) {
545 CURLSocket
*socket
= QLIST_FIRST(&s
->sockets
);
547 QLIST_REMOVE(socket
, next
);
553 qemu_co_enter_next(&s
->s
->free_state_waitq
, &s
->s
->mutex
);
556 static void curl_parse_filename(const char *filename
, QDict
*options
,
559 qdict_put_str(options
, CURL_BLOCK_OPT_URL
, filename
);
562 static void curl_detach_aio_context(BlockDriverState
*bs
)
564 BDRVCURLState
*s
= bs
->opaque
;
567 qemu_mutex_lock(&s
->mutex
);
568 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
569 if (s
->states
[i
].in_use
) {
570 curl_clean_state(&s
->states
[i
]);
572 if (s
->states
[i
].curl
) {
573 curl_easy_cleanup(s
->states
[i
].curl
);
574 s
->states
[i
].curl
= NULL
;
576 g_free(s
->states
[i
].orig_buf
);
577 s
->states
[i
].orig_buf
= NULL
;
580 curl_multi_cleanup(s
->multi
);
583 qemu_mutex_unlock(&s
->mutex
);
585 timer_del(&s
->timer
);
588 static void curl_attach_aio_context(BlockDriverState
*bs
,
589 AioContext
*new_context
)
591 BDRVCURLState
*s
= bs
->opaque
;
593 aio_timer_init(new_context
, &s
->timer
,
594 QEMU_CLOCK_REALTIME
, SCALE_NS
,
595 curl_multi_timeout_do
, s
);
598 s
->multi
= curl_multi_init();
599 s
->aio_context
= new_context
;
600 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
601 #ifdef NEED_CURL_TIMER_CALLBACK
602 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
603 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
607 static QemuOptsList runtime_opts
= {
609 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
612 .name
= CURL_BLOCK_OPT_URL
,
613 .type
= QEMU_OPT_STRING
,
614 .help
= "URL to open",
617 .name
= CURL_BLOCK_OPT_READAHEAD
,
618 .type
= QEMU_OPT_SIZE
,
619 .help
= "Readahead size",
622 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
623 .type
= QEMU_OPT_BOOL
,
624 .help
= "Verify SSL certificate"
627 .name
= CURL_BLOCK_OPT_TIMEOUT
,
628 .type
= QEMU_OPT_NUMBER
,
629 .help
= "Curl timeout"
632 .name
= CURL_BLOCK_OPT_COOKIE
,
633 .type
= QEMU_OPT_STRING
,
634 .help
= "Pass the cookie or list of cookies with each request"
637 .name
= CURL_BLOCK_OPT_COOKIE_SECRET
,
638 .type
= QEMU_OPT_STRING
,
639 .help
= "ID of secret used as cookie passed with each request"
642 .name
= CURL_BLOCK_OPT_USERNAME
,
643 .type
= QEMU_OPT_STRING
,
644 .help
= "Username for HTTP auth"
647 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
648 .type
= QEMU_OPT_STRING
,
649 .help
= "ID of secret used as password for HTTP auth",
652 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
653 .type
= QEMU_OPT_STRING
,
654 .help
= "Username for HTTP proxy auth"
657 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
658 .type
= QEMU_OPT_STRING
,
659 .help
= "ID of secret used as password for HTTP proxy auth",
661 { /* end of list */ }
666 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
669 BDRVCURLState
*s
= bs
->opaque
;
670 CURLState
*state
= NULL
;
672 Error
*local_err
= NULL
;
675 const char *cookie_secret
;
677 const char *secretid
;
678 const char *protocol_delimiter
;
681 ret
= bdrv_apply_auto_read_only(bs
, "curl driver does not support writes",
687 if (!libcurl_initialized
) {
688 ret
= curl_global_init(CURL_GLOBAL_ALL
);
690 error_setg(errp
, "libcurl initialization failed with %d", ret
);
693 libcurl_initialized
= true;
696 qemu_mutex_init(&s
->mutex
);
697 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
698 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
700 error_propagate(errp
, local_err
);
704 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
705 CURL_BLOCK_OPT_READAHEAD_DEFAULT
);
706 if ((s
->readahead_size
& 0x1ff) != 0) {
707 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
712 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
713 CURL_BLOCK_OPT_TIMEOUT_DEFAULT
);
714 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
715 error_setg(errp
, "timeout parameter is too large or negative");
719 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
,
720 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
);
722 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
723 cookie_secret
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE_SECRET
);
725 if (cookie
&& cookie_secret
) {
727 "curl driver cannot handle both cookie and cookie secret");
732 s
->cookie
= qcrypto_secret_lookup_as_utf8(cookie_secret
, errp
);
737 s
->cookie
= g_strdup(cookie
);
740 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
742 error_setg(errp
, "curl block driver requires an 'url' option");
746 if (!strstart(file
, bs
->drv
->protocol_name
, &protocol_delimiter
) ||
747 !strstart(protocol_delimiter
, "://", NULL
))
749 error_setg(errp
, "%s curl driver cannot handle the URL '%s' (does not "
750 "start with '%s://')", bs
->drv
->protocol_name
, file
,
751 bs
->drv
->protocol_name
);
755 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
756 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
759 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
765 s
->proxyusername
= g_strdup(
766 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
767 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
769 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
770 if (!s
->proxypassword
) {
775 trace_curl_open(file
);
776 qemu_co_queue_init(&s
->free_state_waitq
);
777 s
->aio_context
= bdrv_get_aio_context(bs
);
778 s
->url
= g_strdup(file
);
779 qemu_mutex_lock(&s
->mutex
);
780 state
= curl_find_state(s
);
781 qemu_mutex_unlock(&s
->mutex
);
788 if (curl_init_state(s
, state
) < 0) {
792 s
->accept_range
= false;
793 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
794 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
796 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
797 if (curl_easy_perform(state
->curl
))
799 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
802 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
803 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
804 * known and zero if it is really zero-length file. */
805 #if LIBCURL_VERSION_NUM >= 0x071304
807 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
808 "Server didn't report file size.");
813 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
814 "Unknown file size or zero-length file.");
821 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
822 || !strncasecmp(s
->url
, "https://", strlen("https://")))
823 && !s
->accept_range
) {
824 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
825 "Server does not support 'range' (byte ranges).");
828 trace_curl_open_size(s
->len
);
830 qemu_mutex_lock(&s
->mutex
);
831 curl_clean_state(state
);
832 qemu_mutex_unlock(&s
->mutex
);
833 curl_easy_cleanup(state
->curl
);
836 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
842 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
843 curl_easy_cleanup(state
->curl
);
846 qemu_mutex_destroy(&s
->mutex
);
850 g_free(s
->proxyusername
);
851 g_free(s
->proxypassword
);
856 static void curl_setup_preadv(BlockDriverState
*bs
, CURLAIOCB
*acb
)
861 BDRVCURLState
*s
= bs
->opaque
;
863 uint64_t start
= acb
->offset
;
866 qemu_mutex_lock(&s
->mutex
);
868 // In case we have the requested data already (e.g. read-ahead),
869 // we can just call the callback and be done.
870 if (curl_find_buf(s
, start
, acb
->bytes
, acb
)) {
874 // No cache found, so let's start a new request
876 state
= curl_find_state(s
);
880 qemu_co_queue_wait(&s
->free_state_waitq
, &s
->mutex
);
883 if (curl_init_state(s
, state
) < 0) {
884 curl_clean_state(state
);
890 acb
->end
= MIN(acb
->bytes
, s
->len
- start
);
893 g_free(state
->orig_buf
);
894 state
->buf_start
= start
;
895 state
->buf_len
= MIN(acb
->end
+ s
->readahead_size
, s
->len
- start
);
896 end
= start
+ state
->buf_len
- 1;
897 state
->orig_buf
= g_try_malloc(state
->buf_len
);
898 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
899 curl_clean_state(state
);
905 snprintf(state
->range
, 127, "%" PRIu64
"-%" PRIu64
, start
, end
);
906 trace_curl_setup_preadv(acb
->bytes
, start
, state
->range
);
907 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
909 if (curl_multi_add_handle(s
->multi
, state
->curl
) != CURLM_OK
) {
910 state
->acb
[0] = NULL
;
913 curl_clean_state(state
);
917 /* Tell curl it needs to kick things off */
918 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
921 qemu_mutex_unlock(&s
->mutex
);
924 static int coroutine_fn
curl_co_preadv(BlockDriverState
*bs
,
925 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
928 .co
= qemu_coroutine_self(),
935 curl_setup_preadv(bs
, &acb
);
936 while (acb
.ret
== -EINPROGRESS
) {
937 qemu_coroutine_yield();
942 static void curl_close(BlockDriverState
*bs
)
944 BDRVCURLState
*s
= bs
->opaque
;
947 curl_detach_aio_context(bs
);
948 qemu_mutex_destroy(&s
->mutex
);
953 g_free(s
->proxyusername
);
954 g_free(s
->proxypassword
);
957 static int64_t curl_getlength(BlockDriverState
*bs
)
959 BDRVCURLState
*s
= bs
->opaque
;
963 static void curl_refresh_filename(BlockDriverState
*bs
)
965 BDRVCURLState
*s
= bs
->opaque
;
967 /* "readahead" and "timeout" do not change the guest-visible data,
969 if (s
->sslverify
!= CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
||
970 s
->cookie
|| s
->username
|| s
->password
|| s
->proxyusername
||
976 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
), s
->url
);
980 static const char *const curl_strong_runtime_opts
[] = {
982 CURL_BLOCK_OPT_SSLVERIFY
,
983 CURL_BLOCK_OPT_COOKIE
,
984 CURL_BLOCK_OPT_COOKIE_SECRET
,
985 CURL_BLOCK_OPT_USERNAME
,
986 CURL_BLOCK_OPT_PASSWORD_SECRET
,
987 CURL_BLOCK_OPT_PROXY_USERNAME
,
988 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
993 static BlockDriver bdrv_http
= {
994 .format_name
= "http",
995 .protocol_name
= "http",
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
,
1008 .bdrv_refresh_filename
= curl_refresh_filename
,
1009 .strong_runtime_opts
= curl_strong_runtime_opts
,
1012 static BlockDriver bdrv_https
= {
1013 .format_name
= "https",
1014 .protocol_name
= "https",
1016 .instance_size
= sizeof(BDRVCURLState
),
1017 .bdrv_parse_filename
= curl_parse_filename
,
1018 .bdrv_file_open
= curl_open
,
1019 .bdrv_close
= curl_close
,
1020 .bdrv_getlength
= curl_getlength
,
1022 .bdrv_co_preadv
= curl_co_preadv
,
1024 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1025 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1027 .bdrv_refresh_filename
= curl_refresh_filename
,
1028 .strong_runtime_opts
= curl_strong_runtime_opts
,
1031 static BlockDriver bdrv_ftp
= {
1032 .format_name
= "ftp",
1033 .protocol_name
= "ftp",
1035 .instance_size
= sizeof(BDRVCURLState
),
1036 .bdrv_parse_filename
= curl_parse_filename
,
1037 .bdrv_file_open
= curl_open
,
1038 .bdrv_close
= curl_close
,
1039 .bdrv_getlength
= curl_getlength
,
1041 .bdrv_co_preadv
= curl_co_preadv
,
1043 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1044 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1046 .bdrv_refresh_filename
= curl_refresh_filename
,
1047 .strong_runtime_opts
= curl_strong_runtime_opts
,
1050 static BlockDriver bdrv_ftps
= {
1051 .format_name
= "ftps",
1052 .protocol_name
= "ftps",
1054 .instance_size
= sizeof(BDRVCURLState
),
1055 .bdrv_parse_filename
= curl_parse_filename
,
1056 .bdrv_file_open
= curl_open
,
1057 .bdrv_close
= curl_close
,
1058 .bdrv_getlength
= curl_getlength
,
1060 .bdrv_co_preadv
= curl_co_preadv
,
1062 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1063 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1065 .bdrv_refresh_filename
= curl_refresh_filename
,
1066 .strong_runtime_opts
= curl_strong_runtime_opts
,
1069 static void curl_block_init(void)
1071 bdrv_register(&bdrv_http
);
1072 bdrv_register(&bdrv_https
);
1073 bdrv_register(&bdrv_ftp
);
1074 bdrv_register(&bdrv_ftps
);
1077 block_init(curl_block_init
);