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 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
41 CURLPROTO_FTP | CURLPROTO_FTPS)
43 #define CURL_NUM_STATES 8
44 #define CURL_NUM_ACB 8
45 #define CURL_TIMEOUT_MAX 10000
47 #define CURL_BLOCK_OPT_URL "url"
48 #define CURL_BLOCK_OPT_READAHEAD "readahead"
49 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
50 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
51 #define CURL_BLOCK_OPT_COOKIE "cookie"
52 #define CURL_BLOCK_OPT_COOKIE_SECRET "cookie-secret"
53 #define CURL_BLOCK_OPT_USERNAME "username"
54 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
55 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
56 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
58 #define CURL_BLOCK_OPT_READAHEAD_DEFAULT (256 * 1024)
59 #define CURL_BLOCK_OPT_SSLVERIFY_DEFAULT true
60 #define CURL_BLOCK_OPT_TIMEOUT_DEFAULT 5
65 static bool libcurl_initialized
;
67 typedef struct CURLAIOCB
{
79 typedef struct CURLSocket
{
81 struct BDRVCURLState
*s
;
84 typedef struct CURLState
86 struct BDRVCURLState
*s
;
87 CURLAIOCB
*acb
[CURL_NUM_ACB
];
94 char errmsg
[CURL_ERROR_SIZE
];
98 typedef struct BDRVCURLState
{
102 CURLState states
[CURL_NUM_STATES
];
103 GHashTable
*sockets
; /* GINT_TO_POINTER(fd) -> socket */
105 size_t readahead_size
;
110 AioContext
*aio_context
;
112 CoQueue free_state_waitq
;
119 static void curl_clean_state(CURLState
*s
);
120 static void curl_multi_do(void *arg
);
122 static gboolean
curl_drop_socket(void *key
, void *value
, void *opaque
)
124 CURLSocket
*socket
= value
;
125 BDRVCURLState
*s
= socket
->s
;
127 aio_set_fd_handler(s
->aio_context
, socket
->fd
, false,
128 NULL
, NULL
, NULL
, NULL
, NULL
);
132 static void curl_drop_all_sockets(GHashTable
*sockets
)
134 g_hash_table_foreach_remove(sockets
, curl_drop_socket
, NULL
);
137 /* Called from curl_multi_do_locked, with s->mutex held. */
138 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
140 BDRVCURLState
*s
= opaque
;
142 trace_curl_timer_cb(timeout_ms
);
143 if (timeout_ms
== -1) {
144 timer_del(&s
->timer
);
146 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
148 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
153 /* Called from curl_multi_do_locked, with s->mutex held. */
154 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
155 void *userp
, void *sp
)
158 CURLState
*state
= NULL
;
161 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
164 socket
= g_hash_table_lookup(s
->sockets
, GINT_TO_POINTER(fd
));
166 socket
= g_new0(CURLSocket
, 1);
169 g_hash_table_insert(s
->sockets
, GINT_TO_POINTER(fd
), socket
);
172 trace_curl_sock_cb(action
, (int)fd
);
175 aio_set_fd_handler(s
->aio_context
, fd
, false,
176 curl_multi_do
, NULL
, NULL
, NULL
, socket
);
179 aio_set_fd_handler(s
->aio_context
, fd
, false,
180 NULL
, curl_multi_do
, NULL
, NULL
, socket
);
182 case CURL_POLL_INOUT
:
183 aio_set_fd_handler(s
->aio_context
, fd
, false,
184 curl_multi_do
, curl_multi_do
,
187 case CURL_POLL_REMOVE
:
188 aio_set_fd_handler(s
->aio_context
, fd
, false,
189 NULL
, NULL
, NULL
, NULL
, NULL
);
193 if (action
== CURL_POLL_REMOVE
) {
194 g_hash_table_remove(s
->sockets
, GINT_TO_POINTER(fd
));
200 /* Called from curl_multi_do_locked, with s->mutex held. */
201 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
203 BDRVCURLState
*s
= opaque
;
204 size_t realsize
= size
* nmemb
;
205 const char *header
= (char *)ptr
;
206 const char *end
= header
+ realsize
;
207 const char *accept_ranges
= "accept-ranges:";
208 const char *bytes
= "bytes";
210 if (realsize
>= strlen(accept_ranges
)
211 && g_ascii_strncasecmp(header
, accept_ranges
,
212 strlen(accept_ranges
)) == 0) {
214 char *p
= strchr(header
, ':') + 1;
216 /* Skip whitespace between the header name and value. */
217 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
221 if (end
- p
>= strlen(bytes
)
222 && strncmp(p
, bytes
, strlen(bytes
)) == 0) {
224 /* Check that there is nothing but whitespace after the value. */
226 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
230 if (p
== end
|| !*p
) {
231 s
->accept_range
= true;
239 /* Called from curl_multi_do_locked, with s->mutex held. */
240 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
242 CURLState
*s
= ((CURLState
*)opaque
);
243 size_t realsize
= size
* nmemb
;
245 trace_curl_read_cb(realsize
);
247 if (!s
|| !s
->orig_buf
) {
251 if (s
->buf_off
>= s
->buf_len
) {
252 /* buffer full, read nothing */
255 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
256 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
257 s
->buf_off
+= realsize
;
260 /* curl will error out if we do not return this value */
264 /* Called with s->mutex held. */
265 static bool curl_find_buf(BDRVCURLState
*s
, uint64_t start
, uint64_t len
,
269 uint64_t end
= start
+ len
;
270 uint64_t clamped_end
= MIN(end
, s
->len
);
271 uint64_t clamped_len
= clamped_end
- start
;
273 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
274 CURLState
*state
= &s
->states
[i
];
275 uint64_t buf_end
= (state
->buf_start
+ state
->buf_off
);
276 uint64_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
278 if (!state
->orig_buf
)
283 // Does the existing buffer cover our section?
284 if ((start
>= state
->buf_start
) &&
285 (start
<= buf_end
) &&
286 (clamped_end
>= state
->buf_start
) &&
287 (clamped_end
<= buf_end
))
289 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
291 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, clamped_len
);
292 if (clamped_len
< len
) {
293 qemu_iovec_memset(acb
->qiov
, clamped_len
, 0, len
- clamped_len
);
299 // Wait for unfinished chunks
301 (start
>= state
->buf_start
) &&
302 (start
<= buf_fend
) &&
303 (clamped_end
>= state
->buf_start
) &&
304 (clamped_end
<= buf_fend
))
308 acb
->start
= start
- state
->buf_start
;
309 acb
->end
= acb
->start
+ clamped_len
;
311 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
312 if (!state
->acb
[j
]) {
323 /* Called with s->mutex held. */
324 static void curl_multi_check_completion(BDRVCURLState
*s
)
328 /* Try to find done transfers, so we can free the easy
332 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
334 /* Quit when there are no more completions */
338 if (msg
->msg
== CURLMSG_DONE
) {
340 CURLState
*state
= NULL
;
341 bool error
= msg
->data
.result
!= CURLE_OK
;
343 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
347 static int errcount
= 100;
349 /* Don't lose the original error message from curl, since
350 * it contains extra data.
353 error_report("curl: %s", state
->errmsg
);
354 if (--errcount
== 0) {
355 error_report("curl: further errors suppressed");
360 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
361 CURLAIOCB
*acb
= state
->acb
[i
];
368 /* Assert that we have read all data */
369 assert(state
->buf_off
>= acb
->end
);
371 qemu_iovec_from_buf(acb
->qiov
, 0,
372 state
->orig_buf
+ acb
->start
,
373 acb
->end
- acb
->start
);
375 if (acb
->end
- acb
->start
< acb
->bytes
) {
376 size_t offset
= acb
->end
- acb
->start
;
377 qemu_iovec_memset(acb
->qiov
, offset
, 0,
378 acb
->bytes
- offset
);
382 acb
->ret
= error
? -EIO
: 0;
383 state
->acb
[i
] = NULL
;
384 qemu_mutex_unlock(&s
->mutex
);
385 aio_co_wake(acb
->co
);
386 qemu_mutex_lock(&s
->mutex
);
389 curl_clean_state(state
);
395 /* Called with s->mutex held. */
396 static void curl_multi_do_locked(CURLSocket
*socket
)
398 BDRVCURLState
*s
= socket
->s
;
407 r
= curl_multi_socket_action(s
->multi
, socket
->fd
, 0, &running
);
408 } while (r
== CURLM_CALL_MULTI_PERFORM
);
411 static void curl_multi_do(void *arg
)
413 CURLSocket
*socket
= arg
;
414 BDRVCURLState
*s
= socket
->s
;
416 qemu_mutex_lock(&s
->mutex
);
417 curl_multi_do_locked(socket
);
418 curl_multi_check_completion(s
);
419 qemu_mutex_unlock(&s
->mutex
);
422 static void curl_multi_timeout_do(void *arg
)
424 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
431 qemu_mutex_lock(&s
->mutex
);
432 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
434 curl_multi_check_completion(s
);
435 qemu_mutex_unlock(&s
->mutex
);
438 /* Called with s->mutex held. */
439 static CURLState
*curl_find_state(BDRVCURLState
*s
)
441 CURLState
*state
= NULL
;
444 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
445 if (!s
->states
[i
].in_use
) {
446 state
= &s
->states
[i
];
454 static int curl_init_state(BDRVCURLState
*s
, CURLState
*state
)
457 state
->curl
= curl_easy_init();
461 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
462 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
463 (long) s
->sslverify
);
464 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYHOST
,
465 s
->sslverify
? 2L : 0L);
467 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
469 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
470 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
471 (void *)curl_read_cb
);
472 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
473 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
474 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
475 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
476 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
477 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
478 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
481 curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
);
484 curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
);
486 if (s
->proxyusername
) {
487 curl_easy_setopt(state
->curl
,
488 CURLOPT_PROXYUSERNAME
, s
->proxyusername
);
490 if (s
->proxypassword
) {
491 curl_easy_setopt(state
->curl
,
492 CURLOPT_PROXYPASSWORD
, s
->proxypassword
);
495 /* Restrict supported protocols to avoid security issues in the more
496 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
499 * Restricting protocols is only supported from 7.19.4 upwards.
501 #if LIBCURL_VERSION_NUM >= 0x071304
502 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
503 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
507 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
516 /* Called with s->mutex held. */
517 static void curl_clean_state(CURLState
*s
)
520 for (j
= 0; j
< CURL_NUM_ACB
; j
++) {
525 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
529 qemu_co_enter_next(&s
->s
->free_state_waitq
, &s
->s
->mutex
);
532 static void curl_parse_filename(const char *filename
, QDict
*options
,
535 qdict_put_str(options
, CURL_BLOCK_OPT_URL
, filename
);
538 static void curl_detach_aio_context(BlockDriverState
*bs
)
540 BDRVCURLState
*s
= bs
->opaque
;
543 WITH_QEMU_LOCK_GUARD(&s
->mutex
) {
544 curl_drop_all_sockets(s
->sockets
);
545 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
546 if (s
->states
[i
].in_use
) {
547 curl_clean_state(&s
->states
[i
]);
549 if (s
->states
[i
].curl
) {
550 curl_easy_cleanup(s
->states
[i
].curl
);
551 s
->states
[i
].curl
= NULL
;
553 g_free(s
->states
[i
].orig_buf
);
554 s
->states
[i
].orig_buf
= NULL
;
557 curl_multi_cleanup(s
->multi
);
562 timer_del(&s
->timer
);
565 static void curl_attach_aio_context(BlockDriverState
*bs
,
566 AioContext
*new_context
)
568 BDRVCURLState
*s
= bs
->opaque
;
570 aio_timer_init(new_context
, &s
->timer
,
571 QEMU_CLOCK_REALTIME
, SCALE_NS
,
572 curl_multi_timeout_do
, s
);
575 s
->multi
= curl_multi_init();
576 s
->aio_context
= new_context
;
577 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
578 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
579 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
582 static QemuOptsList runtime_opts
= {
584 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
587 .name
= CURL_BLOCK_OPT_URL
,
588 .type
= QEMU_OPT_STRING
,
589 .help
= "URL to open",
592 .name
= CURL_BLOCK_OPT_READAHEAD
,
593 .type
= QEMU_OPT_SIZE
,
594 .help
= "Readahead size",
597 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
598 .type
= QEMU_OPT_BOOL
,
599 .help
= "Verify SSL certificate"
602 .name
= CURL_BLOCK_OPT_TIMEOUT
,
603 .type
= QEMU_OPT_NUMBER
,
604 .help
= "Curl timeout"
607 .name
= CURL_BLOCK_OPT_COOKIE
,
608 .type
= QEMU_OPT_STRING
,
609 .help
= "Pass the cookie or list of cookies with each request"
612 .name
= CURL_BLOCK_OPT_COOKIE_SECRET
,
613 .type
= QEMU_OPT_STRING
,
614 .help
= "ID of secret used as cookie passed with each request"
617 .name
= CURL_BLOCK_OPT_USERNAME
,
618 .type
= QEMU_OPT_STRING
,
619 .help
= "Username for HTTP auth"
622 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
623 .type
= QEMU_OPT_STRING
,
624 .help
= "ID of secret used as password for HTTP auth",
627 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
628 .type
= QEMU_OPT_STRING
,
629 .help
= "Username for HTTP proxy auth"
632 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
633 .type
= QEMU_OPT_STRING
,
634 .help
= "ID of secret used as password for HTTP proxy auth",
636 { /* end of list */ }
641 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
644 BDRVCURLState
*s
= bs
->opaque
;
645 CURLState
*state
= NULL
;
649 const char *cookie_secret
;
651 const char *secretid
;
652 const char *protocol_delimiter
;
655 ret
= bdrv_apply_auto_read_only(bs
, "curl driver does not support writes",
661 if (!libcurl_initialized
) {
662 ret
= curl_global_init(CURL_GLOBAL_ALL
);
664 error_setg(errp
, "libcurl initialization failed with %d", ret
);
667 libcurl_initialized
= true;
670 qemu_mutex_init(&s
->mutex
);
671 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
672 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
676 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
677 CURL_BLOCK_OPT_READAHEAD_DEFAULT
);
678 if ((s
->readahead_size
& 0x1ff) != 0) {
679 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
684 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
685 CURL_BLOCK_OPT_TIMEOUT_DEFAULT
);
686 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
687 error_setg(errp
, "timeout parameter is too large or negative");
691 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
,
692 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
);
694 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
695 cookie_secret
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE_SECRET
);
697 if (cookie
&& cookie_secret
) {
699 "curl driver cannot handle both cookie and cookie secret");
704 s
->cookie
= qcrypto_secret_lookup_as_utf8(cookie_secret
, errp
);
709 s
->cookie
= g_strdup(cookie
);
712 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
714 error_setg(errp
, "curl block driver requires an 'url' option");
718 if (!strstart(file
, bs
->drv
->protocol_name
, &protocol_delimiter
) ||
719 !strstart(protocol_delimiter
, "://", NULL
))
721 error_setg(errp
, "%s curl driver cannot handle the URL '%s' (does not "
722 "start with '%s://')", bs
->drv
->protocol_name
, file
,
723 bs
->drv
->protocol_name
);
727 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
728 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
731 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
737 s
->proxyusername
= g_strdup(
738 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
739 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
741 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
742 if (!s
->proxypassword
) {
747 trace_curl_open(file
);
748 qemu_co_queue_init(&s
->free_state_waitq
);
749 s
->aio_context
= bdrv_get_aio_context(bs
);
750 s
->url
= g_strdup(file
);
751 s
->sockets
= g_hash_table_new_full(NULL
, NULL
, NULL
, g_free
);
752 qemu_mutex_lock(&s
->mutex
);
753 state
= curl_find_state(s
);
754 qemu_mutex_unlock(&s
->mutex
);
761 if (curl_init_state(s
, state
) < 0) {
765 s
->accept_range
= false;
766 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
767 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
769 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
770 if (curl_easy_perform(state
->curl
))
772 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
775 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
776 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
777 * known and zero if it is really zero-length file. */
778 #if LIBCURL_VERSION_NUM >= 0x071304
780 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
781 "Server didn't report file size.");
786 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
787 "Unknown file size or zero-length file.");
794 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
795 || !strncasecmp(s
->url
, "https://", strlen("https://")))
796 && !s
->accept_range
) {
797 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
798 "Server does not support 'range' (byte ranges).");
801 trace_curl_open_size(s
->len
);
803 qemu_mutex_lock(&s
->mutex
);
804 curl_clean_state(state
);
805 qemu_mutex_unlock(&s
->mutex
);
806 curl_easy_cleanup(state
->curl
);
809 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
815 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
816 curl_easy_cleanup(state
->curl
);
819 qemu_mutex_destroy(&s
->mutex
);
823 g_free(s
->proxyusername
);
824 g_free(s
->proxypassword
);
825 curl_drop_all_sockets(s
->sockets
);
826 g_hash_table_destroy(s
->sockets
);
831 static void curl_setup_preadv(BlockDriverState
*bs
, CURLAIOCB
*acb
)
836 BDRVCURLState
*s
= bs
->opaque
;
838 uint64_t start
= acb
->offset
;
841 qemu_mutex_lock(&s
->mutex
);
843 // In case we have the requested data already (e.g. read-ahead),
844 // we can just call the callback and be done.
845 if (curl_find_buf(s
, start
, acb
->bytes
, acb
)) {
849 // No cache found, so let's start a new request
851 state
= curl_find_state(s
);
855 qemu_co_queue_wait(&s
->free_state_waitq
, &s
->mutex
);
858 if (curl_init_state(s
, state
) < 0) {
859 curl_clean_state(state
);
865 acb
->end
= MIN(acb
->bytes
, s
->len
- start
);
868 g_free(state
->orig_buf
);
869 state
->buf_start
= start
;
870 state
->buf_len
= MIN(acb
->end
+ s
->readahead_size
, s
->len
- start
);
871 end
= start
+ state
->buf_len
- 1;
872 state
->orig_buf
= g_try_malloc(state
->buf_len
);
873 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
874 curl_clean_state(state
);
880 snprintf(state
->range
, 127, "%" PRIu64
"-%" PRIu64
, start
, end
);
881 trace_curl_setup_preadv(acb
->bytes
, start
, state
->range
);
882 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
884 if (curl_multi_add_handle(s
->multi
, state
->curl
) != CURLM_OK
) {
885 state
->acb
[0] = NULL
;
888 curl_clean_state(state
);
892 /* Tell curl it needs to kick things off */
893 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
896 qemu_mutex_unlock(&s
->mutex
);
899 static int coroutine_fn
curl_co_preadv(BlockDriverState
*bs
,
900 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
,
901 BdrvRequestFlags flags
)
904 .co
= qemu_coroutine_self(),
911 curl_setup_preadv(bs
, &acb
);
912 while (acb
.ret
== -EINPROGRESS
) {
913 qemu_coroutine_yield();
918 static void curl_close(BlockDriverState
*bs
)
920 BDRVCURLState
*s
= bs
->opaque
;
923 curl_detach_aio_context(bs
);
924 qemu_mutex_destroy(&s
->mutex
);
926 g_hash_table_destroy(s
->sockets
);
930 g_free(s
->proxyusername
);
931 g_free(s
->proxypassword
);
934 static int64_t curl_getlength(BlockDriverState
*bs
)
936 BDRVCURLState
*s
= bs
->opaque
;
940 static void curl_refresh_filename(BlockDriverState
*bs
)
942 BDRVCURLState
*s
= bs
->opaque
;
944 /* "readahead" and "timeout" do not change the guest-visible data,
946 if (s
->sslverify
!= CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
||
947 s
->cookie
|| s
->username
|| s
->password
|| s
->proxyusername
||
953 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
), s
->url
);
957 static const char *const curl_strong_runtime_opts
[] = {
959 CURL_BLOCK_OPT_SSLVERIFY
,
960 CURL_BLOCK_OPT_COOKIE
,
961 CURL_BLOCK_OPT_COOKIE_SECRET
,
962 CURL_BLOCK_OPT_USERNAME
,
963 CURL_BLOCK_OPT_PASSWORD_SECRET
,
964 CURL_BLOCK_OPT_PROXY_USERNAME
,
965 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
970 static BlockDriver bdrv_http
= {
971 .format_name
= "http",
972 .protocol_name
= "http",
974 .instance_size
= sizeof(BDRVCURLState
),
975 .bdrv_parse_filename
= curl_parse_filename
,
976 .bdrv_file_open
= curl_open
,
977 .bdrv_close
= curl_close
,
978 .bdrv_getlength
= curl_getlength
,
980 .bdrv_co_preadv
= curl_co_preadv
,
982 .bdrv_detach_aio_context
= curl_detach_aio_context
,
983 .bdrv_attach_aio_context
= curl_attach_aio_context
,
985 .bdrv_refresh_filename
= curl_refresh_filename
,
986 .strong_runtime_opts
= curl_strong_runtime_opts
,
989 static BlockDriver bdrv_https
= {
990 .format_name
= "https",
991 .protocol_name
= "https",
993 .instance_size
= sizeof(BDRVCURLState
),
994 .bdrv_parse_filename
= curl_parse_filename
,
995 .bdrv_file_open
= curl_open
,
996 .bdrv_close
= curl_close
,
997 .bdrv_getlength
= curl_getlength
,
999 .bdrv_co_preadv
= curl_co_preadv
,
1001 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1002 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1004 .bdrv_refresh_filename
= curl_refresh_filename
,
1005 .strong_runtime_opts
= curl_strong_runtime_opts
,
1008 static BlockDriver bdrv_ftp
= {
1009 .format_name
= "ftp",
1010 .protocol_name
= "ftp",
1012 .instance_size
= sizeof(BDRVCURLState
),
1013 .bdrv_parse_filename
= curl_parse_filename
,
1014 .bdrv_file_open
= curl_open
,
1015 .bdrv_close
= curl_close
,
1016 .bdrv_getlength
= curl_getlength
,
1018 .bdrv_co_preadv
= curl_co_preadv
,
1020 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1021 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1023 .bdrv_refresh_filename
= curl_refresh_filename
,
1024 .strong_runtime_opts
= curl_strong_runtime_opts
,
1027 static BlockDriver bdrv_ftps
= {
1028 .format_name
= "ftps",
1029 .protocol_name
= "ftps",
1031 .instance_size
= sizeof(BDRVCURLState
),
1032 .bdrv_parse_filename
= curl_parse_filename
,
1033 .bdrv_file_open
= curl_open
,
1034 .bdrv_close
= curl_close
,
1035 .bdrv_getlength
= curl_getlength
,
1037 .bdrv_co_preadv
= curl_co_preadv
,
1039 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1040 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1042 .bdrv_refresh_filename
= curl_refresh_filename
,
1043 .strong_runtime_opts
= curl_strong_runtime_opts
,
1046 static void curl_block_init(void)
1048 bdrv_register(&bdrv_http
);
1049 bdrv_register(&bdrv_https
);
1050 bdrv_register(&bdrv_ftp
);
1051 bdrv_register(&bdrv_ftps
);
1054 block_init(curl_block_init
);