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 CURLState
*state
;
82 QLIST_ENTRY(CURLSocket
) next
;
85 typedef struct CURLState
87 struct BDRVCURLState
*s
;
88 CURLAIOCB
*acb
[CURL_NUM_ACB
];
90 QLIST_HEAD(, CURLSocket
) sockets
;
96 char errmsg
[CURL_ERROR_SIZE
];
100 typedef struct BDRVCURLState
{
104 CURLState states
[CURL_NUM_STATES
];
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 /* Called from curl_multi_do_locked, with s->mutex held. */
124 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
126 BDRVCURLState
*s
= opaque
;
128 trace_curl_timer_cb(timeout_ms
);
129 if (timeout_ms
== -1) {
130 timer_del(&s
->timer
);
132 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
134 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
139 /* Called from curl_multi_do_locked, with s->mutex held. */
140 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
141 void *userp
, void *sp
)
144 CURLState
*state
= NULL
;
147 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
150 QLIST_FOREACH(socket
, &state
->sockets
, next
) {
151 if (socket
->fd
== fd
) {
156 socket
= g_new0(CURLSocket
, 1);
158 socket
->state
= state
;
159 QLIST_INSERT_HEAD(&state
->sockets
, socket
, next
);
162 trace_curl_sock_cb(action
, (int)fd
);
165 aio_set_fd_handler(s
->aio_context
, fd
, false,
166 curl_multi_do
, NULL
, NULL
, socket
);
169 aio_set_fd_handler(s
->aio_context
, fd
, false,
170 NULL
, curl_multi_do
, NULL
, socket
);
172 case CURL_POLL_INOUT
:
173 aio_set_fd_handler(s
->aio_context
, fd
, false,
174 curl_multi_do
, curl_multi_do
, NULL
, socket
);
176 case CURL_POLL_REMOVE
:
177 aio_set_fd_handler(s
->aio_context
, fd
, false,
178 NULL
, NULL
, NULL
, NULL
);
182 if (action
== CURL_POLL_REMOVE
) {
183 QLIST_REMOVE(socket
, next
);
190 /* Called from curl_multi_do_locked, with s->mutex held. */
191 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
193 BDRVCURLState
*s
= opaque
;
194 size_t realsize
= size
* nmemb
;
195 const char *header
= (char *)ptr
;
196 const char *end
= header
+ realsize
;
197 const char *accept_ranges
= "accept-ranges:";
198 const char *bytes
= "bytes";
200 if (realsize
>= strlen(accept_ranges
)
201 && g_ascii_strncasecmp(header
, accept_ranges
,
202 strlen(accept_ranges
)) == 0) {
204 char *p
= strchr(header
, ':') + 1;
206 /* Skip whitespace between the header name and value. */
207 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
211 if (end
- p
>= strlen(bytes
)
212 && strncmp(p
, bytes
, strlen(bytes
)) == 0) {
214 /* Check that there is nothing but whitespace after the value. */
216 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
220 if (p
== end
|| !*p
) {
221 s
->accept_range
= true;
229 /* Called from curl_multi_do_locked, with s->mutex held. */
230 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
232 CURLState
*s
= ((CURLState
*)opaque
);
233 size_t realsize
= size
* nmemb
;
235 trace_curl_read_cb(realsize
);
237 if (!s
|| !s
->orig_buf
) {
241 if (s
->buf_off
>= s
->buf_len
) {
242 /* buffer full, read nothing */
245 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
246 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
247 s
->buf_off
+= realsize
;
250 /* curl will error out if we do not return this value */
254 /* Called with s->mutex held. */
255 static bool curl_find_buf(BDRVCURLState
*s
, uint64_t start
, uint64_t len
,
259 uint64_t end
= start
+ len
;
260 uint64_t clamped_end
= MIN(end
, s
->len
);
261 uint64_t clamped_len
= clamped_end
- start
;
263 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
264 CURLState
*state
= &s
->states
[i
];
265 uint64_t buf_end
= (state
->buf_start
+ state
->buf_off
);
266 uint64_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
268 if (!state
->orig_buf
)
273 // Does the existing buffer cover our section?
274 if ((start
>= state
->buf_start
) &&
275 (start
<= buf_end
) &&
276 (clamped_end
>= state
->buf_start
) &&
277 (clamped_end
<= buf_end
))
279 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
281 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, clamped_len
);
282 if (clamped_len
< len
) {
283 qemu_iovec_memset(acb
->qiov
, clamped_len
, 0, len
- clamped_len
);
289 // Wait for unfinished chunks
291 (start
>= state
->buf_start
) &&
292 (start
<= buf_fend
) &&
293 (clamped_end
>= state
->buf_start
) &&
294 (clamped_end
<= buf_fend
))
298 acb
->start
= start
- state
->buf_start
;
299 acb
->end
= acb
->start
+ clamped_len
;
301 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
302 if (!state
->acb
[j
]) {
313 /* Called with s->mutex held. */
314 static void curl_multi_check_completion(BDRVCURLState
*s
)
318 /* Try to find done transfers, so we can free the easy
322 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
324 /* Quit when there are no more completions */
328 if (msg
->msg
== CURLMSG_DONE
) {
330 CURLState
*state
= NULL
;
331 bool error
= msg
->data
.result
!= CURLE_OK
;
333 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
337 static int errcount
= 100;
339 /* Don't lose the original error message from curl, since
340 * it contains extra data.
343 error_report("curl: %s", state
->errmsg
);
344 if (--errcount
== 0) {
345 error_report("curl: further errors suppressed");
350 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
351 CURLAIOCB
*acb
= state
->acb
[i
];
358 /* Assert that we have read all data */
359 assert(state
->buf_off
>= acb
->end
);
361 qemu_iovec_from_buf(acb
->qiov
, 0,
362 state
->orig_buf
+ acb
->start
,
363 acb
->end
- acb
->start
);
365 if (acb
->end
- acb
->start
< acb
->bytes
) {
366 size_t offset
= acb
->end
- acb
->start
;
367 qemu_iovec_memset(acb
->qiov
, offset
, 0,
368 acb
->bytes
- offset
);
372 acb
->ret
= error
? -EIO
: 0;
373 state
->acb
[i
] = NULL
;
374 qemu_mutex_unlock(&s
->mutex
);
375 aio_co_wake(acb
->co
);
376 qemu_mutex_lock(&s
->mutex
);
379 curl_clean_state(state
);
385 /* Called with s->mutex held. */
386 static void curl_multi_do_locked(CURLSocket
*socket
)
388 BDRVCURLState
*s
= socket
->state
->s
;
397 r
= curl_multi_socket_action(s
->multi
, socket
->fd
, 0, &running
);
398 } while (r
== CURLM_CALL_MULTI_PERFORM
);
401 static void curl_multi_do(void *arg
)
403 CURLSocket
*socket
= arg
;
404 BDRVCURLState
*s
= socket
->state
->s
;
406 qemu_mutex_lock(&s
->mutex
);
407 curl_multi_do_locked(socket
);
408 curl_multi_check_completion(s
);
409 qemu_mutex_unlock(&s
->mutex
);
412 static void curl_multi_timeout_do(void *arg
)
414 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
421 qemu_mutex_lock(&s
->mutex
);
422 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
424 curl_multi_check_completion(s
);
425 qemu_mutex_unlock(&s
->mutex
);
428 /* Called with s->mutex held. */
429 static CURLState
*curl_find_state(BDRVCURLState
*s
)
431 CURLState
*state
= NULL
;
434 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
435 if (!s
->states
[i
].in_use
) {
436 state
= &s
->states
[i
];
444 static int curl_init_state(BDRVCURLState
*s
, CURLState
*state
)
447 state
->curl
= curl_easy_init();
451 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
452 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
453 (long) s
->sslverify
);
454 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYHOST
,
455 s
->sslverify
? 2L : 0L);
457 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
459 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
460 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
461 (void *)curl_read_cb
);
462 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
463 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
464 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
465 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
466 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
467 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
468 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
471 curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
);
474 curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
);
476 if (s
->proxyusername
) {
477 curl_easy_setopt(state
->curl
,
478 CURLOPT_PROXYUSERNAME
, s
->proxyusername
);
480 if (s
->proxypassword
) {
481 curl_easy_setopt(state
->curl
,
482 CURLOPT_PROXYPASSWORD
, s
->proxypassword
);
485 /* Restrict supported protocols to avoid security issues in the more
486 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
489 * Restricting protocols is only supported from 7.19.4 upwards.
491 #if LIBCURL_VERSION_NUM >= 0x071304
492 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
493 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
497 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
501 QLIST_INIT(&state
->sockets
);
507 /* Called with s->mutex held. */
508 static void curl_clean_state(CURLState
*s
)
511 for (j
= 0; j
< CURL_NUM_ACB
; j
++) {
516 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
518 while (!QLIST_EMPTY(&s
->sockets
)) {
519 CURLSocket
*socket
= QLIST_FIRST(&s
->sockets
);
521 QLIST_REMOVE(socket
, next
);
527 qemu_co_enter_next(&s
->s
->free_state_waitq
, &s
->s
->mutex
);
530 static void curl_parse_filename(const char *filename
, QDict
*options
,
533 qdict_put_str(options
, CURL_BLOCK_OPT_URL
, filename
);
536 static void curl_detach_aio_context(BlockDriverState
*bs
)
538 BDRVCURLState
*s
= bs
->opaque
;
541 WITH_QEMU_LOCK_GUARD(&s
->mutex
) {
542 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
543 if (s
->states
[i
].in_use
) {
544 curl_clean_state(&s
->states
[i
]);
546 if (s
->states
[i
].curl
) {
547 curl_easy_cleanup(s
->states
[i
].curl
);
548 s
->states
[i
].curl
= NULL
;
550 g_free(s
->states
[i
].orig_buf
);
551 s
->states
[i
].orig_buf
= NULL
;
554 curl_multi_cleanup(s
->multi
);
559 timer_del(&s
->timer
);
562 static void curl_attach_aio_context(BlockDriverState
*bs
,
563 AioContext
*new_context
)
565 BDRVCURLState
*s
= bs
->opaque
;
567 aio_timer_init(new_context
, &s
->timer
,
568 QEMU_CLOCK_REALTIME
, SCALE_NS
,
569 curl_multi_timeout_do
, s
);
572 s
->multi
= curl_multi_init();
573 s
->aio_context
= new_context
;
574 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
575 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
576 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
579 static QemuOptsList runtime_opts
= {
581 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
584 .name
= CURL_BLOCK_OPT_URL
,
585 .type
= QEMU_OPT_STRING
,
586 .help
= "URL to open",
589 .name
= CURL_BLOCK_OPT_READAHEAD
,
590 .type
= QEMU_OPT_SIZE
,
591 .help
= "Readahead size",
594 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
595 .type
= QEMU_OPT_BOOL
,
596 .help
= "Verify SSL certificate"
599 .name
= CURL_BLOCK_OPT_TIMEOUT
,
600 .type
= QEMU_OPT_NUMBER
,
601 .help
= "Curl timeout"
604 .name
= CURL_BLOCK_OPT_COOKIE
,
605 .type
= QEMU_OPT_STRING
,
606 .help
= "Pass the cookie or list of cookies with each request"
609 .name
= CURL_BLOCK_OPT_COOKIE_SECRET
,
610 .type
= QEMU_OPT_STRING
,
611 .help
= "ID of secret used as cookie passed with each request"
614 .name
= CURL_BLOCK_OPT_USERNAME
,
615 .type
= QEMU_OPT_STRING
,
616 .help
= "Username for HTTP auth"
619 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
620 .type
= QEMU_OPT_STRING
,
621 .help
= "ID of secret used as password for HTTP auth",
624 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
625 .type
= QEMU_OPT_STRING
,
626 .help
= "Username for HTTP proxy auth"
629 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
630 .type
= QEMU_OPT_STRING
,
631 .help
= "ID of secret used as password for HTTP proxy auth",
633 { /* end of list */ }
638 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
641 BDRVCURLState
*s
= bs
->opaque
;
642 CURLState
*state
= NULL
;
646 const char *cookie_secret
;
648 const char *secretid
;
649 const char *protocol_delimiter
;
652 ret
= bdrv_apply_auto_read_only(bs
, "curl driver does not support writes",
658 if (!libcurl_initialized
) {
659 ret
= curl_global_init(CURL_GLOBAL_ALL
);
661 error_setg(errp
, "libcurl initialization failed with %d", ret
);
664 libcurl_initialized
= true;
667 qemu_mutex_init(&s
->mutex
);
668 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
669 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
673 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
674 CURL_BLOCK_OPT_READAHEAD_DEFAULT
);
675 if ((s
->readahead_size
& 0x1ff) != 0) {
676 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
681 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
682 CURL_BLOCK_OPT_TIMEOUT_DEFAULT
);
683 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
684 error_setg(errp
, "timeout parameter is too large or negative");
688 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
,
689 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
);
691 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
692 cookie_secret
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE_SECRET
);
694 if (cookie
&& cookie_secret
) {
696 "curl driver cannot handle both cookie and cookie secret");
701 s
->cookie
= qcrypto_secret_lookup_as_utf8(cookie_secret
, errp
);
706 s
->cookie
= g_strdup(cookie
);
709 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
711 error_setg(errp
, "curl block driver requires an 'url' option");
715 if (!strstart(file
, bs
->drv
->protocol_name
, &protocol_delimiter
) ||
716 !strstart(protocol_delimiter
, "://", NULL
))
718 error_setg(errp
, "%s curl driver cannot handle the URL '%s' (does not "
719 "start with '%s://')", bs
->drv
->protocol_name
, file
,
720 bs
->drv
->protocol_name
);
724 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
725 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
728 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
734 s
->proxyusername
= g_strdup(
735 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
736 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
738 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
739 if (!s
->proxypassword
) {
744 trace_curl_open(file
);
745 qemu_co_queue_init(&s
->free_state_waitq
);
746 s
->aio_context
= bdrv_get_aio_context(bs
);
747 s
->url
= g_strdup(file
);
748 qemu_mutex_lock(&s
->mutex
);
749 state
= curl_find_state(s
);
750 qemu_mutex_unlock(&s
->mutex
);
757 if (curl_init_state(s
, state
) < 0) {
761 s
->accept_range
= false;
762 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
763 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
765 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
766 if (curl_easy_perform(state
->curl
))
768 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
771 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
772 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
773 * known and zero if it is really zero-length file. */
774 #if LIBCURL_VERSION_NUM >= 0x071304
776 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
777 "Server didn't report file size.");
782 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
783 "Unknown file size or zero-length file.");
790 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
791 || !strncasecmp(s
->url
, "https://", strlen("https://")))
792 && !s
->accept_range
) {
793 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
794 "Server does not support 'range' (byte ranges).");
797 trace_curl_open_size(s
->len
);
799 qemu_mutex_lock(&s
->mutex
);
800 curl_clean_state(state
);
801 qemu_mutex_unlock(&s
->mutex
);
802 curl_easy_cleanup(state
->curl
);
805 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
811 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
812 curl_easy_cleanup(state
->curl
);
815 qemu_mutex_destroy(&s
->mutex
);
819 g_free(s
->proxyusername
);
820 g_free(s
->proxypassword
);
825 static void curl_setup_preadv(BlockDriverState
*bs
, CURLAIOCB
*acb
)
830 BDRVCURLState
*s
= bs
->opaque
;
832 uint64_t start
= acb
->offset
;
835 qemu_mutex_lock(&s
->mutex
);
837 // In case we have the requested data already (e.g. read-ahead),
838 // we can just call the callback and be done.
839 if (curl_find_buf(s
, start
, acb
->bytes
, acb
)) {
843 // No cache found, so let's start a new request
845 state
= curl_find_state(s
);
849 qemu_co_queue_wait(&s
->free_state_waitq
, &s
->mutex
);
852 if (curl_init_state(s
, state
) < 0) {
853 curl_clean_state(state
);
859 acb
->end
= MIN(acb
->bytes
, s
->len
- start
);
862 g_free(state
->orig_buf
);
863 state
->buf_start
= start
;
864 state
->buf_len
= MIN(acb
->end
+ s
->readahead_size
, s
->len
- start
);
865 end
= start
+ state
->buf_len
- 1;
866 state
->orig_buf
= g_try_malloc(state
->buf_len
);
867 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
868 curl_clean_state(state
);
874 snprintf(state
->range
, 127, "%" PRIu64
"-%" PRIu64
, start
, end
);
875 trace_curl_setup_preadv(acb
->bytes
, start
, state
->range
);
876 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
878 if (curl_multi_add_handle(s
->multi
, state
->curl
) != CURLM_OK
) {
879 state
->acb
[0] = NULL
;
882 curl_clean_state(state
);
886 /* Tell curl it needs to kick things off */
887 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
890 qemu_mutex_unlock(&s
->mutex
);
893 static int coroutine_fn
curl_co_preadv(BlockDriverState
*bs
,
894 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
897 .co
= qemu_coroutine_self(),
904 curl_setup_preadv(bs
, &acb
);
905 while (acb
.ret
== -EINPROGRESS
) {
906 qemu_coroutine_yield();
911 static void curl_close(BlockDriverState
*bs
)
913 BDRVCURLState
*s
= bs
->opaque
;
916 curl_detach_aio_context(bs
);
917 qemu_mutex_destroy(&s
->mutex
);
922 g_free(s
->proxyusername
);
923 g_free(s
->proxypassword
);
926 static int64_t curl_getlength(BlockDriverState
*bs
)
928 BDRVCURLState
*s
= bs
->opaque
;
932 static void curl_refresh_filename(BlockDriverState
*bs
)
934 BDRVCURLState
*s
= bs
->opaque
;
936 /* "readahead" and "timeout" do not change the guest-visible data,
938 if (s
->sslverify
!= CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
||
939 s
->cookie
|| s
->username
|| s
->password
|| s
->proxyusername
||
945 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
), s
->url
);
949 static const char *const curl_strong_runtime_opts
[] = {
951 CURL_BLOCK_OPT_SSLVERIFY
,
952 CURL_BLOCK_OPT_COOKIE
,
953 CURL_BLOCK_OPT_COOKIE_SECRET
,
954 CURL_BLOCK_OPT_USERNAME
,
955 CURL_BLOCK_OPT_PASSWORD_SECRET
,
956 CURL_BLOCK_OPT_PROXY_USERNAME
,
957 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
962 static BlockDriver bdrv_http
= {
963 .format_name
= "http",
964 .protocol_name
= "http",
966 .instance_size
= sizeof(BDRVCURLState
),
967 .bdrv_parse_filename
= curl_parse_filename
,
968 .bdrv_file_open
= curl_open
,
969 .bdrv_close
= curl_close
,
970 .bdrv_getlength
= curl_getlength
,
972 .bdrv_co_preadv
= curl_co_preadv
,
974 .bdrv_detach_aio_context
= curl_detach_aio_context
,
975 .bdrv_attach_aio_context
= curl_attach_aio_context
,
977 .bdrv_refresh_filename
= curl_refresh_filename
,
978 .strong_runtime_opts
= curl_strong_runtime_opts
,
981 static BlockDriver bdrv_https
= {
982 .format_name
= "https",
983 .protocol_name
= "https",
985 .instance_size
= sizeof(BDRVCURLState
),
986 .bdrv_parse_filename
= curl_parse_filename
,
987 .bdrv_file_open
= curl_open
,
988 .bdrv_close
= curl_close
,
989 .bdrv_getlength
= curl_getlength
,
991 .bdrv_co_preadv
= curl_co_preadv
,
993 .bdrv_detach_aio_context
= curl_detach_aio_context
,
994 .bdrv_attach_aio_context
= curl_attach_aio_context
,
996 .bdrv_refresh_filename
= curl_refresh_filename
,
997 .strong_runtime_opts
= curl_strong_runtime_opts
,
1000 static BlockDriver bdrv_ftp
= {
1001 .format_name
= "ftp",
1002 .protocol_name
= "ftp",
1004 .instance_size
= sizeof(BDRVCURLState
),
1005 .bdrv_parse_filename
= curl_parse_filename
,
1006 .bdrv_file_open
= curl_open
,
1007 .bdrv_close
= curl_close
,
1008 .bdrv_getlength
= curl_getlength
,
1010 .bdrv_co_preadv
= curl_co_preadv
,
1012 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1013 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1015 .bdrv_refresh_filename
= curl_refresh_filename
,
1016 .strong_runtime_opts
= curl_strong_runtime_opts
,
1019 static BlockDriver bdrv_ftps
= {
1020 .format_name
= "ftps",
1021 .protocol_name
= "ftps",
1023 .instance_size
= sizeof(BDRVCURLState
),
1024 .bdrv_parse_filename
= curl_parse_filename
,
1025 .bdrv_file_open
= curl_open
,
1026 .bdrv_close
= curl_close
,
1027 .bdrv_getlength
= curl_getlength
,
1029 .bdrv_co_preadv
= curl_co_preadv
,
1031 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1032 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1034 .bdrv_refresh_filename
= curl_refresh_filename
,
1035 .strong_runtime_opts
= curl_strong_runtime_opts
,
1038 static void curl_block_init(void)
1040 bdrv_register(&bdrv_http
);
1041 bdrv_register(&bdrv_https
);
1042 bdrv_register(&bdrv_ftp
);
1043 bdrv_register(&bdrv_ftps
);
1046 block_init(curl_block_init
);