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
39 #if LIBCURL_VERSION_NUM >= 0x071000
40 /* The multi interface timer callback was introduced in 7.16.0 */
41 #define NEED_CURL_TIMER_CALLBACK
42 #define HAVE_SOCKET_ACTION
45 #ifndef HAVE_SOCKET_ACTION
46 /* If curl_multi_socket_action isn't available, define it statically here in
47 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
48 * less efficient but still safe. */
49 static CURLMcode
__curl_multi_socket_action(CURLM
*multi_handle
,
54 return curl_multi_socket(multi_handle
, sockfd
, running_handles
);
56 #define curl_multi_socket_action __curl_multi_socket_action
59 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
60 CURLPROTO_FTP | CURLPROTO_FTPS)
62 #define CURL_NUM_STATES 8
63 #define CURL_NUM_ACB 8
64 #define CURL_TIMEOUT_MAX 10000
66 #define CURL_BLOCK_OPT_URL "url"
67 #define CURL_BLOCK_OPT_READAHEAD "readahead"
68 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
69 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
70 #define CURL_BLOCK_OPT_COOKIE "cookie"
71 #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
72 #define CURL_BLOCK_OPT_USERNAME "username"
73 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
74 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
75 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
77 #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
78 #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
79 #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
83 static bool libcurl_initialized
;
85 typedef struct CURLAIOCB
{
97 typedef struct CURLSocket
{
99 QLIST_ENTRY(CURLSocket
) next
;
102 typedef struct CURLState
104 struct BDRVCURLState
*s
;
105 CURLAIOCB
*acb
[CURL_NUM_ACB
];
107 QLIST_HEAD(, CURLSocket
) sockets
;
113 char errmsg
[CURL_ERROR_SIZE
];
117 typedef struct BDRVCURLState
{
121 CURLState states
[CURL_NUM_STATES
];
123 size_t readahead_size
;
128 AioContext
*aio_context
;
130 CoQueue free_state_waitq
;
137 static void curl_clean_state(CURLState
*s
);
138 static void curl_multi_do(void *arg
);
139 static void curl_multi_read(void *arg
);
141 #ifdef NEED_CURL_TIMER_CALLBACK
142 /* Called from curl_multi_do_locked, with s->mutex held. */
143 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
145 BDRVCURLState
*s
= opaque
;
147 trace_curl_timer_cb(timeout_ms
);
148 if (timeout_ms
== -1) {
149 timer_del(&s
->timer
);
151 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
153 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
159 /* Called from curl_multi_do_locked, with s->mutex held. */
160 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
161 void *userp
, void *sp
)
164 CURLState
*state
= NULL
;
167 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
170 QLIST_FOREACH(socket
, &state
->sockets
, next
) {
171 if (socket
->fd
== fd
) {
172 if (action
== CURL_POLL_REMOVE
) {
173 QLIST_REMOVE(socket
, next
);
180 socket
= g_new0(CURLSocket
, 1);
182 QLIST_INSERT_HEAD(&state
->sockets
, socket
, next
);
186 trace_curl_sock_cb(action
, (int)fd
);
189 aio_set_fd_handler(s
->aio_context
, fd
, false,
190 curl_multi_read
, NULL
, NULL
, state
);
193 aio_set_fd_handler(s
->aio_context
, fd
, false,
194 NULL
, curl_multi_do
, NULL
, state
);
196 case CURL_POLL_INOUT
:
197 aio_set_fd_handler(s
->aio_context
, fd
, false,
198 curl_multi_read
, curl_multi_do
, NULL
, state
);
200 case CURL_POLL_REMOVE
:
201 aio_set_fd_handler(s
->aio_context
, fd
, false,
202 NULL
, NULL
, NULL
, NULL
);
209 /* Called from curl_multi_do_locked, with s->mutex held. */
210 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
212 BDRVCURLState
*s
= opaque
;
213 size_t realsize
= size
* nmemb
;
214 const char *accept_line
= "Accept-Ranges: bytes";
216 if (realsize
>= strlen(accept_line
)
217 && strncmp((char *)ptr
, accept_line
, strlen(accept_line
)) == 0) {
218 s
->accept_range
= true;
224 /* Called from curl_multi_do_locked, with s->mutex held. */
225 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
227 CURLState
*s
= ((CURLState
*)opaque
);
228 size_t realsize
= size
* nmemb
;
231 trace_curl_read_cb(realsize
);
233 if (!s
|| !s
->orig_buf
) {
237 if (s
->buf_off
>= s
->buf_len
) {
238 /* buffer full, read nothing */
241 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
242 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
243 s
->buf_off
+= realsize
;
245 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
246 CURLAIOCB
*acb
= s
->acb
[i
];
251 if ((s
->buf_off
>= acb
->end
)) {
252 size_t request_length
= acb
->bytes
;
254 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
255 acb
->end
- acb
->start
);
257 if (acb
->end
- acb
->start
< request_length
) {
258 size_t offset
= acb
->end
- acb
->start
;
259 qemu_iovec_memset(acb
->qiov
, offset
, 0,
260 request_length
- offset
);
265 qemu_mutex_unlock(&s
->s
->mutex
);
266 aio_co_wake(acb
->co
);
267 qemu_mutex_lock(&s
->s
->mutex
);
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
) {
351 CURLState
*state
= NULL
;
352 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
355 /* ACBs for successful messages get completed in curl_read_cb */
356 if (msg
->data
.result
!= CURLE_OK
) {
358 static int errcount
= 100;
360 /* Don't lose the original error message from curl, since
361 * it contains extra data.
364 error_report("curl: %s", state
->errmsg
);
365 if (--errcount
== 0) {
366 error_report("curl: further errors suppressed");
370 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
371 CURLAIOCB
*acb
= state
->acb
[i
];
378 state
->acb
[i
] = NULL
;
379 qemu_mutex_unlock(&s
->mutex
);
380 aio_co_wake(acb
->co
);
381 qemu_mutex_lock(&s
->mutex
);
385 curl_clean_state(state
);
391 /* Called with s->mutex held. */
392 static void curl_multi_do_locked(CURLState
*s
)
394 CURLSocket
*socket
, *next_socket
;
402 /* Need to use _SAFE because curl_multi_socket_action() may trigger
403 * curl_sock_cb() which might modify this list */
404 QLIST_FOREACH_SAFE(socket
, &s
->sockets
, next
, next_socket
) {
406 r
= curl_multi_socket_action(s
->s
->multi
, socket
->fd
, 0, &running
);
407 } while (r
== CURLM_CALL_MULTI_PERFORM
);
411 static void curl_multi_do(void *arg
)
413 CURLState
*s
= (CURLState
*)arg
;
415 qemu_mutex_lock(&s
->s
->mutex
);
416 curl_multi_do_locked(s
);
417 qemu_mutex_unlock(&s
->s
->mutex
);
420 static void curl_multi_read(void *arg
)
422 CURLState
*s
= (CURLState
*)arg
;
424 qemu_mutex_lock(&s
->s
->mutex
);
425 curl_multi_do_locked(s
);
426 curl_multi_check_completion(s
->s
);
427 qemu_mutex_unlock(&s
->s
->mutex
);
430 static void curl_multi_timeout_do(void *arg
)
432 #ifdef NEED_CURL_TIMER_CALLBACK
433 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
440 qemu_mutex_lock(&s
->mutex
);
441 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
443 curl_multi_check_completion(s
);
444 qemu_mutex_unlock(&s
->mutex
);
450 /* Called with s->mutex held. */
451 static CURLState
*curl_find_state(BDRVCURLState
*s
)
453 CURLState
*state
= NULL
;
456 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
457 if (!s
->states
[i
].in_use
) {
458 state
= &s
->states
[i
];
466 static int curl_init_state(BDRVCURLState
*s
, CURLState
*state
)
469 state
->curl
= curl_easy_init();
473 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
474 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
475 (long) s
->sslverify
);
476 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYHOST
,
477 s
->sslverify
? 2L : 0L);
479 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
481 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
482 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
483 (void *)curl_read_cb
);
484 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
485 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
486 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
487 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
488 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
489 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
490 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
493 curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
);
496 curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
);
498 if (s
->proxyusername
) {
499 curl_easy_setopt(state
->curl
,
500 CURLOPT_PROXYUSERNAME
, s
->proxyusername
);
502 if (s
->proxypassword
) {
503 curl_easy_setopt(state
->curl
,
504 CURLOPT_PROXYPASSWORD
, s
->proxypassword
);
507 /* Restrict supported protocols to avoid security issues in the more
508 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
511 * Restricting protocols is only supported from 7.19.4 upwards.
513 #if LIBCURL_VERSION_NUM >= 0x071304
514 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
515 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
519 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
523 QLIST_INIT(&state
->sockets
);
529 /* Called with s->mutex held. */
530 static void curl_clean_state(CURLState
*s
)
533 for (j
= 0; j
< CURL_NUM_ACB
; j
++) {
538 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
540 while (!QLIST_EMPTY(&s
->sockets
)) {
541 CURLSocket
*socket
= QLIST_FIRST(&s
->sockets
);
543 QLIST_REMOVE(socket
, next
);
549 qemu_co_enter_next(&s
->s
->free_state_waitq
, &s
->s
->mutex
);
552 static void curl_parse_filename(const char *filename
, QDict
*options
,
555 qdict_put_str(options
, CURL_BLOCK_OPT_URL
, filename
);
558 static void curl_detach_aio_context(BlockDriverState
*bs
)
560 BDRVCURLState
*s
= bs
->opaque
;
563 qemu_mutex_lock(&s
->mutex
);
564 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
565 if (s
->states
[i
].in_use
) {
566 curl_clean_state(&s
->states
[i
]);
568 if (s
->states
[i
].curl
) {
569 curl_easy_cleanup(s
->states
[i
].curl
);
570 s
->states
[i
].curl
= NULL
;
572 g_free(s
->states
[i
].orig_buf
);
573 s
->states
[i
].orig_buf
= NULL
;
576 curl_multi_cleanup(s
->multi
);
579 qemu_mutex_unlock(&s
->mutex
);
581 timer_del(&s
->timer
);
584 static void curl_attach_aio_context(BlockDriverState
*bs
,
585 AioContext
*new_context
)
587 BDRVCURLState
*s
= bs
->opaque
;
589 aio_timer_init(new_context
, &s
->timer
,
590 QEMU_CLOCK_REALTIME
, SCALE_NS
,
591 curl_multi_timeout_do
, s
);
594 s
->multi
= curl_multi_init();
595 s
->aio_context
= new_context
;
596 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
597 #ifdef NEED_CURL_TIMER_CALLBACK
598 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
599 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
603 static QemuOptsList runtime_opts
= {
605 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
608 .name
= CURL_BLOCK_OPT_URL
,
609 .type
= QEMU_OPT_STRING
,
610 .help
= "URL to open",
613 .name
= CURL_BLOCK_OPT_READAHEAD
,
614 .type
= QEMU_OPT_SIZE
,
615 .help
= "Readahead size",
618 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
619 .type
= QEMU_OPT_BOOL
,
620 .help
= "Verify SSL certificate"
623 .name
= CURL_BLOCK_OPT_TIMEOUT
,
624 .type
= QEMU_OPT_NUMBER
,
625 .help
= "Curl timeout"
628 .name
= CURL_BLOCK_OPT_COOKIE
,
629 .type
= QEMU_OPT_STRING
,
630 .help
= "Pass the cookie or list of cookies with each request"
633 .name
= CURL_BLOCK_OPT_COOKIE_SECRET
,
634 .type
= QEMU_OPT_STRING
,
635 .help
= "ID of secret used as cookie passed with each request"
638 .name
= CURL_BLOCK_OPT_USERNAME
,
639 .type
= QEMU_OPT_STRING
,
640 .help
= "Username for HTTP auth"
643 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
644 .type
= QEMU_OPT_STRING
,
645 .help
= "ID of secret used as password for HTTP auth",
648 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
649 .type
= QEMU_OPT_STRING
,
650 .help
= "Username for HTTP proxy auth"
653 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
654 .type
= QEMU_OPT_STRING
,
655 .help
= "ID of secret used as password for HTTP proxy auth",
657 { /* end of list */ }
662 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
665 BDRVCURLState
*s
= bs
->opaque
;
666 CURLState
*state
= NULL
;
668 Error
*local_err
= NULL
;
671 const char *cookie_secret
;
673 const char *secretid
;
674 const char *protocol_delimiter
;
677 ret
= bdrv_apply_auto_read_only(bs
, "curl driver does not support writes",
683 if (!libcurl_initialized
) {
684 ret
= curl_global_init(CURL_GLOBAL_ALL
);
686 error_setg(errp
, "libcurl initialization failed with %d", ret
);
689 libcurl_initialized
= true;
692 qemu_mutex_init(&s
->mutex
);
693 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
694 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
696 error_propagate(errp
, local_err
);
700 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
701 CURL_BLOCK_OPT_READAHEAD_DEFAULT
);
702 if ((s
->readahead_size
& 0x1ff) != 0) {
703 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
708 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
709 CURL_BLOCK_OPT_TIMEOUT_DEFAULT
);
710 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
711 error_setg(errp
, "timeout parameter is too large or negative");
715 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
,
716 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
);
718 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
719 cookie_secret
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE_SECRET
);
721 if (cookie
&& cookie_secret
) {
723 "curl driver cannot handle both cookie and cookie secret");
728 s
->cookie
= qcrypto_secret_lookup_as_utf8(cookie_secret
, errp
);
733 s
->cookie
= g_strdup(cookie
);
736 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
738 error_setg(errp
, "curl block driver requires an 'url' option");
742 if (!strstart(file
, bs
->drv
->protocol_name
, &protocol_delimiter
) ||
743 !strstart(protocol_delimiter
, "://", NULL
))
745 error_setg(errp
, "%s curl driver cannot handle the URL '%s' (does not "
746 "start with '%s://')", bs
->drv
->protocol_name
, file
,
747 bs
->drv
->protocol_name
);
751 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
752 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
755 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
761 s
->proxyusername
= g_strdup(
762 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
763 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
765 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
766 if (!s
->proxypassword
) {
771 trace_curl_open(file
);
772 qemu_co_queue_init(&s
->free_state_waitq
);
773 s
->aio_context
= bdrv_get_aio_context(bs
);
774 s
->url
= g_strdup(file
);
775 qemu_mutex_lock(&s
->mutex
);
776 state
= curl_find_state(s
);
777 qemu_mutex_unlock(&s
->mutex
);
784 if (curl_init_state(s
, state
) < 0) {
788 s
->accept_range
= false;
789 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
790 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
792 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
793 if (curl_easy_perform(state
->curl
))
795 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
798 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
799 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
800 * known and zero if it is really zero-length file. */
801 #if LIBCURL_VERSION_NUM >= 0x071304
803 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
804 "Server didn't report file size.");
809 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
810 "Unknown file size or zero-length file.");
817 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
818 || !strncasecmp(s
->url
, "https://", strlen("https://")))
819 && !s
->accept_range
) {
820 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
821 "Server does not support 'range' (byte ranges).");
824 trace_curl_open_size(s
->len
);
826 qemu_mutex_lock(&s
->mutex
);
827 curl_clean_state(state
);
828 qemu_mutex_unlock(&s
->mutex
);
829 curl_easy_cleanup(state
->curl
);
832 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
838 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
839 curl_easy_cleanup(state
->curl
);
842 qemu_mutex_destroy(&s
->mutex
);
846 g_free(s
->proxyusername
);
847 g_free(s
->proxypassword
);
852 static void curl_setup_preadv(BlockDriverState
*bs
, CURLAIOCB
*acb
)
857 BDRVCURLState
*s
= bs
->opaque
;
859 uint64_t start
= acb
->offset
;
862 qemu_mutex_lock(&s
->mutex
);
864 // In case we have the requested data already (e.g. read-ahead),
865 // we can just call the callback and be done.
866 if (curl_find_buf(s
, start
, acb
->bytes
, acb
)) {
870 // No cache found, so let's start a new request
872 state
= curl_find_state(s
);
876 qemu_co_queue_wait(&s
->free_state_waitq
, &s
->mutex
);
879 if (curl_init_state(s
, state
) < 0) {
880 curl_clean_state(state
);
886 acb
->end
= MIN(acb
->bytes
, s
->len
- start
);
889 g_free(state
->orig_buf
);
890 state
->buf_start
= start
;
891 state
->buf_len
= MIN(acb
->end
+ s
->readahead_size
, s
->len
- start
);
892 end
= start
+ state
->buf_len
- 1;
893 state
->orig_buf
= g_try_malloc(state
->buf_len
);
894 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
895 curl_clean_state(state
);
901 snprintf(state
->range
, 127, "%" PRIu64
"-%" PRIu64
, start
, end
);
902 trace_curl_setup_preadv(acb
->bytes
, start
, state
->range
);
903 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
905 curl_multi_add_handle(s
->multi
, state
->curl
);
907 /* Tell curl it needs to kick things off */
908 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
911 qemu_mutex_unlock(&s
->mutex
);
914 static int coroutine_fn
curl_co_preadv(BlockDriverState
*bs
,
915 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
918 .co
= qemu_coroutine_self(),
925 curl_setup_preadv(bs
, &acb
);
926 while (acb
.ret
== -EINPROGRESS
) {
927 qemu_coroutine_yield();
932 static void curl_close(BlockDriverState
*bs
)
934 BDRVCURLState
*s
= bs
->opaque
;
937 curl_detach_aio_context(bs
);
938 qemu_mutex_destroy(&s
->mutex
);
943 g_free(s
->proxyusername
);
944 g_free(s
->proxypassword
);
947 static int64_t curl_getlength(BlockDriverState
*bs
)
949 BDRVCURLState
*s
= bs
->opaque
;
953 static void curl_refresh_filename(BlockDriverState
*bs
)
955 BDRVCURLState
*s
= bs
->opaque
;
957 /* "readahead" and "timeout" do not change the guest-visible data,
959 if (s
->sslverify
!= CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
||
960 s
->cookie
|| s
->username
|| s
->password
|| s
->proxyusername
||
966 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
), s
->url
);
970 static const char *const curl_strong_runtime_opts
[] = {
972 CURL_BLOCK_OPT_SSLVERIFY
,
973 CURL_BLOCK_OPT_COOKIE
,
974 CURL_BLOCK_OPT_COOKIE_SECRET
,
975 CURL_BLOCK_OPT_USERNAME
,
976 CURL_BLOCK_OPT_PASSWORD_SECRET
,
977 CURL_BLOCK_OPT_PROXY_USERNAME
,
978 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
983 static BlockDriver bdrv_http
= {
984 .format_name
= "http",
985 .protocol_name
= "http",
987 .instance_size
= sizeof(BDRVCURLState
),
988 .bdrv_parse_filename
= curl_parse_filename
,
989 .bdrv_file_open
= curl_open
,
990 .bdrv_close
= curl_close
,
991 .bdrv_getlength
= curl_getlength
,
993 .bdrv_co_preadv
= curl_co_preadv
,
995 .bdrv_detach_aio_context
= curl_detach_aio_context
,
996 .bdrv_attach_aio_context
= curl_attach_aio_context
,
998 .bdrv_refresh_filename
= curl_refresh_filename
,
999 .strong_runtime_opts
= curl_strong_runtime_opts
,
1002 static BlockDriver bdrv_https
= {
1003 .format_name
= "https",
1004 .protocol_name
= "https",
1006 .instance_size
= sizeof(BDRVCURLState
),
1007 .bdrv_parse_filename
= curl_parse_filename
,
1008 .bdrv_file_open
= curl_open
,
1009 .bdrv_close
= curl_close
,
1010 .bdrv_getlength
= curl_getlength
,
1012 .bdrv_co_preadv
= curl_co_preadv
,
1014 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1015 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1017 .bdrv_refresh_filename
= curl_refresh_filename
,
1018 .strong_runtime_opts
= curl_strong_runtime_opts
,
1021 static BlockDriver bdrv_ftp
= {
1022 .format_name
= "ftp",
1023 .protocol_name
= "ftp",
1025 .instance_size
= sizeof(BDRVCURLState
),
1026 .bdrv_parse_filename
= curl_parse_filename
,
1027 .bdrv_file_open
= curl_open
,
1028 .bdrv_close
= curl_close
,
1029 .bdrv_getlength
= curl_getlength
,
1031 .bdrv_co_preadv
= curl_co_preadv
,
1033 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1034 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1036 .bdrv_refresh_filename
= curl_refresh_filename
,
1037 .strong_runtime_opts
= curl_strong_runtime_opts
,
1040 static BlockDriver bdrv_ftps
= {
1041 .format_name
= "ftps",
1042 .protocol_name
= "ftps",
1044 .instance_size
= sizeof(BDRVCURLState
),
1045 .bdrv_parse_filename
= curl_parse_filename
,
1046 .bdrv_file_open
= curl_open
,
1047 .bdrv_close
= curl_close
,
1048 .bdrv_getlength
= curl_getlength
,
1050 .bdrv_co_preadv
= curl_co_preadv
,
1052 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1053 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1055 .bdrv_refresh_filename
= curl_refresh_filename
,
1056 .strong_runtime_opts
= curl_strong_runtime_opts
,
1059 static void curl_block_init(void)
1061 bdrv_register(&bdrv_http
);
1062 bdrv_register(&bdrv_https
);
1063 bdrv_register(&bdrv_ftp
);
1064 bdrv_register(&bdrv_ftps
);
1067 block_init(curl_block_init
);