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-io.h"
31 #include "block/block_int.h"
32 #include "qapi/qmp/qdict.h"
33 #include "qapi/qmp/qstring.h"
34 #include "crypto/secret.h"
35 #include <curl/curl.h>
36 #include "qemu/cutils.h"
39 // #define DEBUG_VERBOSE
41 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
42 CURLPROTO_FTP | CURLPROTO_FTPS)
44 #define CURL_NUM_STATES 8
45 #define CURL_NUM_ACB 8
46 #define CURL_TIMEOUT_MAX 10000
48 #define CURL_BLOCK_OPT_URL "url"
49 #define CURL_BLOCK_OPT_READAHEAD "readahead"
50 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
51 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
52 #define CURL_BLOCK_OPT_COOKIE "cookie"
53 #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
54 #define CURL_BLOCK_OPT_USERNAME "username"
55 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
56 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
57 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
59 #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
60 #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
61 #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
66 static bool libcurl_initialized
;
68 typedef struct CURLAIOCB
{
80 typedef struct CURLSocket
{
82 struct BDRVCURLState
*s
;
85 typedef struct CURLState
87 struct BDRVCURLState
*s
;
88 CURLAIOCB
*acb
[CURL_NUM_ACB
];
95 char errmsg
[CURL_ERROR_SIZE
];
99 typedef struct BDRVCURLState
{
103 CURLState states
[CURL_NUM_STATES
];
104 GHashTable
*sockets
; /* GINT_TO_POINTER(fd) -> socket */
106 size_t readahead_size
;
111 AioContext
*aio_context
;
113 CoQueue free_state_waitq
;
120 static void curl_clean_state(CURLState
*s
);
121 static void curl_multi_do(void *arg
);
123 static gboolean
curl_drop_socket(void *key
, void *value
, void *opaque
)
125 CURLSocket
*socket
= value
;
126 BDRVCURLState
*s
= socket
->s
;
128 aio_set_fd_handler(s
->aio_context
, socket
->fd
, false,
129 NULL
, NULL
, NULL
, NULL
, NULL
);
133 static void curl_drop_all_sockets(GHashTable
*sockets
)
135 g_hash_table_foreach_remove(sockets
, curl_drop_socket
, NULL
);
138 /* Called from curl_multi_do_locked, with s->mutex held. */
139 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
141 BDRVCURLState
*s
= opaque
;
143 trace_curl_timer_cb(timeout_ms
);
144 if (timeout_ms
== -1) {
145 timer_del(&s
->timer
);
147 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
149 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
154 /* Called from curl_multi_do_locked, with s->mutex held. */
155 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
156 void *userp
, void *sp
)
159 CURLState
*state
= NULL
;
162 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
165 socket
= g_hash_table_lookup(s
->sockets
, GINT_TO_POINTER(fd
));
167 socket
= g_new0(CURLSocket
, 1);
170 g_hash_table_insert(s
->sockets
, GINT_TO_POINTER(fd
), socket
);
173 trace_curl_sock_cb(action
, (int)fd
);
176 aio_set_fd_handler(s
->aio_context
, fd
, false,
177 curl_multi_do
, NULL
, NULL
, NULL
, socket
);
180 aio_set_fd_handler(s
->aio_context
, fd
, false,
181 NULL
, curl_multi_do
, NULL
, NULL
, socket
);
183 case CURL_POLL_INOUT
:
184 aio_set_fd_handler(s
->aio_context
, fd
, false,
185 curl_multi_do
, curl_multi_do
,
188 case CURL_POLL_REMOVE
:
189 aio_set_fd_handler(s
->aio_context
, fd
, false,
190 NULL
, NULL
, NULL
, NULL
, NULL
);
194 if (action
== CURL_POLL_REMOVE
) {
195 g_hash_table_remove(s
->sockets
, GINT_TO_POINTER(fd
));
201 /* Called from curl_multi_do_locked, with s->mutex held. */
202 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
204 BDRVCURLState
*s
= opaque
;
205 size_t realsize
= size
* nmemb
;
206 const char *header
= (char *)ptr
;
207 const char *end
= header
+ realsize
;
208 const char *accept_ranges
= "accept-ranges:";
209 const char *bytes
= "bytes";
211 if (realsize
>= strlen(accept_ranges
)
212 && g_ascii_strncasecmp(header
, accept_ranges
,
213 strlen(accept_ranges
)) == 0) {
215 char *p
= strchr(header
, ':') + 1;
217 /* Skip whitespace between the header name and value. */
218 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
222 if (end
- p
>= strlen(bytes
)
223 && strncmp(p
, bytes
, strlen(bytes
)) == 0) {
225 /* Check that there is nothing but whitespace after the value. */
227 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
231 if (p
== end
|| !*p
) {
232 s
->accept_range
= true;
240 /* Called from curl_multi_do_locked, with s->mutex held. */
241 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
243 CURLState
*s
= ((CURLState
*)opaque
);
244 size_t realsize
= size
* nmemb
;
246 trace_curl_read_cb(realsize
);
248 if (!s
|| !s
->orig_buf
) {
252 if (s
->buf_off
>= s
->buf_len
) {
253 /* buffer full, read nothing */
256 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
257 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
258 s
->buf_off
+= realsize
;
261 /* curl will error out if we do not return this value */
265 /* Called with s->mutex held. */
266 static bool curl_find_buf(BDRVCURLState
*s
, uint64_t start
, uint64_t len
,
270 uint64_t end
= start
+ len
;
271 uint64_t clamped_end
= MIN(end
, s
->len
);
272 uint64_t clamped_len
= clamped_end
- start
;
274 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
275 CURLState
*state
= &s
->states
[i
];
276 uint64_t buf_end
= (state
->buf_start
+ state
->buf_off
);
277 uint64_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
279 if (!state
->orig_buf
)
284 // Does the existing buffer cover our section?
285 if ((start
>= state
->buf_start
) &&
286 (start
<= buf_end
) &&
287 (clamped_end
>= state
->buf_start
) &&
288 (clamped_end
<= buf_end
))
290 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
292 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, clamped_len
);
293 if (clamped_len
< len
) {
294 qemu_iovec_memset(acb
->qiov
, clamped_len
, 0, len
- clamped_len
);
300 // Wait for unfinished chunks
302 (start
>= state
->buf_start
) &&
303 (start
<= buf_fend
) &&
304 (clamped_end
>= state
->buf_start
) &&
305 (clamped_end
<= buf_fend
))
309 acb
->start
= start
- state
->buf_start
;
310 acb
->end
= acb
->start
+ clamped_len
;
312 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
313 if (!state
->acb
[j
]) {
324 /* Called with s->mutex held. */
325 static void curl_multi_check_completion(BDRVCURLState
*s
)
329 /* Try to find done transfers, so we can free the easy
333 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
335 /* Quit when there are no more completions */
339 if (msg
->msg
== CURLMSG_DONE
) {
341 CURLState
*state
= NULL
;
342 bool error
= msg
->data
.result
!= CURLE_OK
;
344 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
348 static int errcount
= 100;
350 /* Don't lose the original error message from curl, since
351 * it contains extra data.
354 error_report("curl: %s", state
->errmsg
);
355 if (--errcount
== 0) {
356 error_report("curl: further errors suppressed");
361 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
362 CURLAIOCB
*acb
= state
->acb
[i
];
369 /* Assert that we have read all data */
370 assert(state
->buf_off
>= acb
->end
);
372 qemu_iovec_from_buf(acb
->qiov
, 0,
373 state
->orig_buf
+ acb
->start
,
374 acb
->end
- acb
->start
);
376 if (acb
->end
- acb
->start
< acb
->bytes
) {
377 size_t offset
= acb
->end
- acb
->start
;
378 qemu_iovec_memset(acb
->qiov
, offset
, 0,
379 acb
->bytes
- offset
);
383 acb
->ret
= error
? -EIO
: 0;
384 state
->acb
[i
] = NULL
;
385 qemu_mutex_unlock(&s
->mutex
);
386 aio_co_wake(acb
->co
);
387 qemu_mutex_lock(&s
->mutex
);
390 curl_clean_state(state
);
396 /* Called with s->mutex held. */
397 static void curl_multi_do_locked(CURLSocket
*socket
)
399 BDRVCURLState
*s
= socket
->s
;
408 r
= curl_multi_socket_action(s
->multi
, socket
->fd
, 0, &running
);
409 } while (r
== CURLM_CALL_MULTI_PERFORM
);
412 static void curl_multi_do(void *arg
)
414 CURLSocket
*socket
= arg
;
415 BDRVCURLState
*s
= socket
->s
;
417 qemu_mutex_lock(&s
->mutex
);
418 curl_multi_do_locked(socket
);
419 curl_multi_check_completion(s
);
420 qemu_mutex_unlock(&s
->mutex
);
423 static void curl_multi_timeout_do(void *arg
)
425 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
432 qemu_mutex_lock(&s
->mutex
);
433 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
435 curl_multi_check_completion(s
);
436 qemu_mutex_unlock(&s
->mutex
);
439 /* Called with s->mutex held. */
440 static CURLState
*curl_find_state(BDRVCURLState
*s
)
442 CURLState
*state
= NULL
;
445 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
446 if (!s
->states
[i
].in_use
) {
447 state
= &s
->states
[i
];
455 static int curl_init_state(BDRVCURLState
*s
, CURLState
*state
)
458 state
->curl
= curl_easy_init();
462 if (curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
) ||
463 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
464 (long) s
->sslverify
) ||
465 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYHOST
,
466 s
->sslverify
? 2L : 0L)) {
470 if (curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
)) {
474 if (curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
) ||
475 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
476 (void *)curl_read_cb
) ||
477 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
) ||
478 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
) ||
479 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1) ||
480 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1) ||
481 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1) ||
482 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
) ||
483 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1)) {
487 if (curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
)) {
492 if (curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
)) {
496 if (s
->proxyusername
) {
497 if (curl_easy_setopt(state
->curl
,
498 CURLOPT_PROXYUSERNAME
, s
->proxyusername
)) {
502 if (s
->proxypassword
) {
503 if (curl_easy_setopt(state
->curl
,
504 CURLOPT_PROXYPASSWORD
, s
->proxypassword
)) {
509 /* Restrict supported protocols to avoid security issues in the more
510 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
513 * Restricting protocols is only supported from 7.19.4 upwards.
515 #if LIBCURL_VERSION_NUM >= 0x071304
516 if (curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
) ||
517 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
)) {
523 if (curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1)) {
534 curl_easy_cleanup(state
->curl
);
539 /* Called with s->mutex held. */
540 static void curl_clean_state(CURLState
*s
)
543 for (j
= 0; j
< CURL_NUM_ACB
; j
++) {
548 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
552 qemu_co_enter_next(&s
->s
->free_state_waitq
, &s
->s
->mutex
);
555 static void curl_parse_filename(const char *filename
, QDict
*options
,
558 qdict_put_str(options
, CURL_BLOCK_OPT_URL
, filename
);
561 static void curl_detach_aio_context(BlockDriverState
*bs
)
563 BDRVCURLState
*s
= bs
->opaque
;
566 WITH_QEMU_LOCK_GUARD(&s
->mutex
) {
567 curl_drop_all_sockets(s
->sockets
);
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
);
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 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
602 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
605 static QemuOptsList runtime_opts
= {
607 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
610 .name
= CURL_BLOCK_OPT_URL
,
611 .type
= QEMU_OPT_STRING
,
612 .help
= "URL to open",
615 .name
= CURL_BLOCK_OPT_READAHEAD
,
616 .type
= QEMU_OPT_SIZE
,
617 .help
= "Readahead size",
620 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
621 .type
= QEMU_OPT_BOOL
,
622 .help
= "Verify SSL certificate"
625 .name
= CURL_BLOCK_OPT_TIMEOUT
,
626 .type
= QEMU_OPT_NUMBER
,
627 .help
= "Curl timeout"
630 .name
= CURL_BLOCK_OPT_COOKIE
,
631 .type
= QEMU_OPT_STRING
,
632 .help
= "Pass the cookie or list of cookies with each request"
635 .name
= CURL_BLOCK_OPT_COOKIE_SECRET
,
636 .type
= QEMU_OPT_STRING
,
637 .help
= "ID of secret used as cookie passed with each request"
640 .name
= CURL_BLOCK_OPT_USERNAME
,
641 .type
= QEMU_OPT_STRING
,
642 .help
= "Username for HTTP auth"
645 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
646 .type
= QEMU_OPT_STRING
,
647 .help
= "ID of secret used as password for HTTP auth",
650 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
651 .type
= QEMU_OPT_STRING
,
652 .help
= "Username for HTTP proxy auth"
655 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
656 .type
= QEMU_OPT_STRING
,
657 .help
= "ID of secret used as password for HTTP proxy auth",
659 { /* end of list */ }
664 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
667 BDRVCURLState
*s
= bs
->opaque
;
668 CURLState
*state
= NULL
;
672 const char *cookie_secret
;
674 const char *secretid
;
675 const char *protocol_delimiter
;
678 ret
= bdrv_apply_auto_read_only(bs
, "curl driver does not support writes",
684 if (!libcurl_initialized
) {
685 ret
= curl_global_init(CURL_GLOBAL_ALL
);
687 error_setg(errp
, "libcurl initialization failed with %d", ret
);
690 libcurl_initialized
= true;
693 qemu_mutex_init(&s
->mutex
);
694 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
695 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
699 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
700 CURL_BLOCK_OPT_READAHEAD_DEFAULT
);
701 if ((s
->readahead_size
& 0x1ff) != 0) {
702 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
707 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
708 CURL_BLOCK_OPT_TIMEOUT_DEFAULT
);
709 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
710 error_setg(errp
, "timeout parameter is too large or negative");
714 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
,
715 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
);
717 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
718 cookie_secret
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE_SECRET
);
720 if (cookie
&& cookie_secret
) {
722 "curl driver cannot handle both cookie and cookie secret");
727 s
->cookie
= qcrypto_secret_lookup_as_utf8(cookie_secret
, errp
);
732 s
->cookie
= g_strdup(cookie
);
735 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
737 error_setg(errp
, "curl block driver requires an 'url' option");
741 if (!strstart(file
, bs
->drv
->protocol_name
, &protocol_delimiter
) ||
742 !strstart(protocol_delimiter
, "://", NULL
))
744 error_setg(errp
, "%s curl driver cannot handle the URL '%s' (does not "
745 "start with '%s://')", bs
->drv
->protocol_name
, file
,
746 bs
->drv
->protocol_name
);
750 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
751 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
754 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
760 s
->proxyusername
= g_strdup(
761 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
762 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
764 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
765 if (!s
->proxypassword
) {
770 trace_curl_open(file
);
771 qemu_co_queue_init(&s
->free_state_waitq
);
772 s
->aio_context
= bdrv_get_aio_context(bs
);
773 s
->url
= g_strdup(file
);
774 s
->sockets
= g_hash_table_new_full(NULL
, NULL
, NULL
, g_free
);
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) {
785 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
786 "curl library initialization failed.");
790 s
->accept_range
= false;
791 if (curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1) ||
792 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
, curl_header_cb
) ||
793 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
)) {
794 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
795 "curl library initialization failed.");
798 if (curl_easy_perform(state
->curl
))
800 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
803 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
804 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
805 * known and zero if it is really zero-length file. */
806 #if LIBCURL_VERSION_NUM >= 0x071304
808 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
809 "Server didn't report file size.");
814 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
815 "Unknown file size or zero-length file.");
822 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
823 || !strncasecmp(s
->url
, "https://", strlen("https://")))
824 && !s
->accept_range
) {
825 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
826 "Server does not support 'range' (byte ranges).");
829 trace_curl_open_size(s
->len
);
831 qemu_mutex_lock(&s
->mutex
);
832 curl_clean_state(state
);
833 qemu_mutex_unlock(&s
->mutex
);
834 curl_easy_cleanup(state
->curl
);
837 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
843 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
844 curl_easy_cleanup(state
->curl
);
847 qemu_mutex_destroy(&s
->mutex
);
851 g_free(s
->proxyusername
);
852 g_free(s
->proxypassword
);
853 curl_drop_all_sockets(s
->sockets
);
854 g_hash_table_destroy(s
->sockets
);
859 static void coroutine_fn
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 trace_curl_setup_preadv(acb
->bytes
, start
, state
->range
);
910 if (curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
) ||
911 curl_multi_add_handle(s
->multi
, state
->curl
) != CURLM_OK
) {
912 state
->acb
[0] = NULL
;
915 curl_clean_state(state
);
919 /* Tell curl it needs to kick things off */
920 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
923 qemu_mutex_unlock(&s
->mutex
);
926 static int coroutine_fn
curl_co_preadv(BlockDriverState
*bs
,
927 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
928 BdrvRequestFlags flags
)
931 .co
= qemu_coroutine_self(),
938 curl_setup_preadv(bs
, &acb
);
939 while (acb
.ret
== -EINPROGRESS
) {
940 qemu_coroutine_yield();
945 static void curl_close(BlockDriverState
*bs
)
947 BDRVCURLState
*s
= bs
->opaque
;
950 curl_detach_aio_context(bs
);
951 qemu_mutex_destroy(&s
->mutex
);
953 g_hash_table_destroy(s
->sockets
);
957 g_free(s
->proxyusername
);
958 g_free(s
->proxypassword
);
961 static int64_t coroutine_fn
curl_co_getlength(BlockDriverState
*bs
)
963 BDRVCURLState
*s
= bs
->opaque
;
967 static void curl_refresh_filename(BlockDriverState
*bs
)
969 BDRVCURLState
*s
= bs
->opaque
;
971 /* "readahead" and "timeout" do not change the guest-visible data,
973 if (s
->sslverify
!= CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
||
974 s
->cookie
|| s
->username
|| s
->password
|| s
->proxyusername
||
980 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
), s
->url
);
984 static const char *const curl_strong_runtime_opts
[] = {
986 CURL_BLOCK_OPT_SSLVERIFY
,
987 CURL_BLOCK_OPT_COOKIE
,
988 CURL_BLOCK_OPT_COOKIE_SECRET
,
989 CURL_BLOCK_OPT_USERNAME
,
990 CURL_BLOCK_OPT_PASSWORD_SECRET
,
991 CURL_BLOCK_OPT_PROXY_USERNAME
,
992 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
997 static BlockDriver bdrv_http
= {
998 .format_name
= "http",
999 .protocol_name
= "http",
1001 .instance_size
= sizeof(BDRVCURLState
),
1002 .bdrv_parse_filename
= curl_parse_filename
,
1003 .bdrv_file_open
= curl_open
,
1004 .bdrv_close
= curl_close
,
1005 .bdrv_co_getlength
= curl_co_getlength
,
1007 .bdrv_co_preadv
= curl_co_preadv
,
1009 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1010 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1012 .bdrv_refresh_filename
= curl_refresh_filename
,
1013 .strong_runtime_opts
= curl_strong_runtime_opts
,
1016 static BlockDriver bdrv_https
= {
1017 .format_name
= "https",
1018 .protocol_name
= "https",
1020 .instance_size
= sizeof(BDRVCURLState
),
1021 .bdrv_parse_filename
= curl_parse_filename
,
1022 .bdrv_file_open
= curl_open
,
1023 .bdrv_close
= curl_close
,
1024 .bdrv_co_getlength
= curl_co_getlength
,
1026 .bdrv_co_preadv
= curl_co_preadv
,
1028 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1029 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1031 .bdrv_refresh_filename
= curl_refresh_filename
,
1032 .strong_runtime_opts
= curl_strong_runtime_opts
,
1035 static BlockDriver bdrv_ftp
= {
1036 .format_name
= "ftp",
1037 .protocol_name
= "ftp",
1039 .instance_size
= sizeof(BDRVCURLState
),
1040 .bdrv_parse_filename
= curl_parse_filename
,
1041 .bdrv_file_open
= curl_open
,
1042 .bdrv_close
= curl_close
,
1043 .bdrv_co_getlength
= curl_co_getlength
,
1045 .bdrv_co_preadv
= curl_co_preadv
,
1047 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1048 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1050 .bdrv_refresh_filename
= curl_refresh_filename
,
1051 .strong_runtime_opts
= curl_strong_runtime_opts
,
1054 static BlockDriver bdrv_ftps
= {
1055 .format_name
= "ftps",
1056 .protocol_name
= "ftps",
1058 .instance_size
= sizeof(BDRVCURLState
),
1059 .bdrv_parse_filename
= curl_parse_filename
,
1060 .bdrv_file_open
= curl_open
,
1061 .bdrv_close
= curl_close
,
1062 .bdrv_co_getlength
= curl_co_getlength
,
1064 .bdrv_co_preadv
= curl_co_preadv
,
1066 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1067 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1069 .bdrv_refresh_filename
= curl_refresh_filename
,
1070 .strong_runtime_opts
= curl_strong_runtime_opts
,
1073 static void curl_block_init(void)
1075 bdrv_register(&bdrv_http
);
1076 bdrv_register(&bdrv_https
);
1077 bdrv_register(&bdrv_ftp
);
1078 bdrv_register(&bdrv_ftps
);
1081 block_init(curl_block_init
);