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
);
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
, socket
);
179 aio_set_fd_handler(s
->aio_context
, fd
, false,
180 NULL
, curl_multi_do
, NULL
, socket
);
182 case CURL_POLL_INOUT
:
183 aio_set_fd_handler(s
->aio_context
, fd
, false,
184 curl_multi_do
, curl_multi_do
, NULL
, socket
);
186 case CURL_POLL_REMOVE
:
187 aio_set_fd_handler(s
->aio_context
, fd
, false,
188 NULL
, NULL
, NULL
, NULL
);
192 if (action
== CURL_POLL_REMOVE
) {
193 g_hash_table_remove(s
->sockets
, GINT_TO_POINTER(fd
));
199 /* Called from curl_multi_do_locked, with s->mutex held. */
200 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
202 BDRVCURLState
*s
= opaque
;
203 size_t realsize
= size
* nmemb
;
204 const char *header
= (char *)ptr
;
205 const char *end
= header
+ realsize
;
206 const char *accept_ranges
= "accept-ranges:";
207 const char *bytes
= "bytes";
209 if (realsize
>= strlen(accept_ranges
)
210 && g_ascii_strncasecmp(header
, accept_ranges
,
211 strlen(accept_ranges
)) == 0) {
213 char *p
= strchr(header
, ':') + 1;
215 /* Skip whitespace between the header name and value. */
216 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
220 if (end
- p
>= strlen(bytes
)
221 && strncmp(p
, bytes
, strlen(bytes
)) == 0) {
223 /* Check that there is nothing but whitespace after the value. */
225 while (p
< end
&& *p
&& g_ascii_isspace(*p
)) {
229 if (p
== end
|| !*p
) {
230 s
->accept_range
= true;
238 /* Called from curl_multi_do_locked, with s->mutex held. */
239 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
241 CURLState
*s
= ((CURLState
*)opaque
);
242 size_t realsize
= size
* nmemb
;
244 trace_curl_read_cb(realsize
);
246 if (!s
|| !s
->orig_buf
) {
250 if (s
->buf_off
>= s
->buf_len
) {
251 /* buffer full, read nothing */
254 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
255 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
256 s
->buf_off
+= realsize
;
259 /* curl will error out if we do not return this value */
263 /* Called with s->mutex held. */
264 static bool curl_find_buf(BDRVCURLState
*s
, uint64_t start
, uint64_t len
,
268 uint64_t end
= start
+ len
;
269 uint64_t clamped_end
= MIN(end
, s
->len
);
270 uint64_t clamped_len
= clamped_end
- start
;
272 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
273 CURLState
*state
= &s
->states
[i
];
274 uint64_t buf_end
= (state
->buf_start
+ state
->buf_off
);
275 uint64_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
277 if (!state
->orig_buf
)
282 // Does the existing buffer cover our section?
283 if ((start
>= state
->buf_start
) &&
284 (start
<= buf_end
) &&
285 (clamped_end
>= state
->buf_start
) &&
286 (clamped_end
<= buf_end
))
288 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
290 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, clamped_len
);
291 if (clamped_len
< len
) {
292 qemu_iovec_memset(acb
->qiov
, clamped_len
, 0, len
- clamped_len
);
298 // Wait for unfinished chunks
300 (start
>= state
->buf_start
) &&
301 (start
<= buf_fend
) &&
302 (clamped_end
>= state
->buf_start
) &&
303 (clamped_end
<= buf_fend
))
307 acb
->start
= start
- state
->buf_start
;
308 acb
->end
= acb
->start
+ clamped_len
;
310 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
311 if (!state
->acb
[j
]) {
322 /* Called with s->mutex held. */
323 static void curl_multi_check_completion(BDRVCURLState
*s
)
327 /* Try to find done transfers, so we can free the easy
331 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
333 /* Quit when there are no more completions */
337 if (msg
->msg
== CURLMSG_DONE
) {
339 CURLState
*state
= NULL
;
340 bool error
= msg
->data
.result
!= CURLE_OK
;
342 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
346 static int errcount
= 100;
348 /* Don't lose the original error message from curl, since
349 * it contains extra data.
352 error_report("curl: %s", state
->errmsg
);
353 if (--errcount
== 0) {
354 error_report("curl: further errors suppressed");
359 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
360 CURLAIOCB
*acb
= state
->acb
[i
];
367 /* Assert that we have read all data */
368 assert(state
->buf_off
>= acb
->end
);
370 qemu_iovec_from_buf(acb
->qiov
, 0,
371 state
->orig_buf
+ acb
->start
,
372 acb
->end
- acb
->start
);
374 if (acb
->end
- acb
->start
< acb
->bytes
) {
375 size_t offset
= acb
->end
- acb
->start
;
376 qemu_iovec_memset(acb
->qiov
, offset
, 0,
377 acb
->bytes
- offset
);
381 acb
->ret
= error
? -EIO
: 0;
382 state
->acb
[i
] = NULL
;
383 qemu_mutex_unlock(&s
->mutex
);
384 aio_co_wake(acb
->co
);
385 qemu_mutex_lock(&s
->mutex
);
388 curl_clean_state(state
);
394 /* Called with s->mutex held. */
395 static void curl_multi_do_locked(CURLSocket
*socket
)
397 BDRVCURLState
*s
= socket
->s
;
406 r
= curl_multi_socket_action(s
->multi
, socket
->fd
, 0, &running
);
407 } while (r
== CURLM_CALL_MULTI_PERFORM
);
410 static void curl_multi_do(void *arg
)
412 CURLSocket
*socket
= arg
;
413 BDRVCURLState
*s
= socket
->s
;
415 qemu_mutex_lock(&s
->mutex
);
416 curl_multi_do_locked(socket
);
417 curl_multi_check_completion(s
);
418 qemu_mutex_unlock(&s
->mutex
);
421 static void curl_multi_timeout_do(void *arg
)
423 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
430 qemu_mutex_lock(&s
->mutex
);
431 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
433 curl_multi_check_completion(s
);
434 qemu_mutex_unlock(&s
->mutex
);
437 /* Called with s->mutex held. */
438 static CURLState
*curl_find_state(BDRVCURLState
*s
)
440 CURLState
*state
= NULL
;
443 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
444 if (!s
->states
[i
].in_use
) {
445 state
= &s
->states
[i
];
453 static int curl_init_state(BDRVCURLState
*s
, CURLState
*state
)
456 state
->curl
= curl_easy_init();
460 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
461 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
462 (long) s
->sslverify
);
463 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYHOST
,
464 s
->sslverify
? 2L : 0L);
466 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
468 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
469 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
470 (void *)curl_read_cb
);
471 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
472 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
473 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
474 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
475 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
476 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
477 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
480 curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
);
483 curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
);
485 if (s
->proxyusername
) {
486 curl_easy_setopt(state
->curl
,
487 CURLOPT_PROXYUSERNAME
, s
->proxyusername
);
489 if (s
->proxypassword
) {
490 curl_easy_setopt(state
->curl
,
491 CURLOPT_PROXYPASSWORD
, s
->proxypassword
);
494 /* Restrict supported protocols to avoid security issues in the more
495 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
498 * Restricting protocols is only supported from 7.19.4 upwards.
500 #if LIBCURL_VERSION_NUM >= 0x071304
501 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
502 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
506 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
515 /* Called with s->mutex held. */
516 static void curl_clean_state(CURLState
*s
)
519 for (j
= 0; j
< CURL_NUM_ACB
; j
++) {
524 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
528 qemu_co_enter_next(&s
->s
->free_state_waitq
, &s
->s
->mutex
);
531 static void curl_parse_filename(const char *filename
, QDict
*options
,
534 qdict_put_str(options
, CURL_BLOCK_OPT_URL
, filename
);
537 static void curl_detach_aio_context(BlockDriverState
*bs
)
539 BDRVCURLState
*s
= bs
->opaque
;
542 WITH_QEMU_LOCK_GUARD(&s
->mutex
) {
543 curl_drop_all_sockets(s
->sockets
);
544 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
545 if (s
->states
[i
].in_use
) {
546 curl_clean_state(&s
->states
[i
]);
548 if (s
->states
[i
].curl
) {
549 curl_easy_cleanup(s
->states
[i
].curl
);
550 s
->states
[i
].curl
= NULL
;
552 g_free(s
->states
[i
].orig_buf
);
553 s
->states
[i
].orig_buf
= NULL
;
556 curl_multi_cleanup(s
->multi
);
561 timer_del(&s
->timer
);
564 static void curl_attach_aio_context(BlockDriverState
*bs
,
565 AioContext
*new_context
)
567 BDRVCURLState
*s
= bs
->opaque
;
569 aio_timer_init(new_context
, &s
->timer
,
570 QEMU_CLOCK_REALTIME
, SCALE_NS
,
571 curl_multi_timeout_do
, s
);
574 s
->multi
= curl_multi_init();
575 s
->aio_context
= new_context
;
576 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
577 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
578 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
581 static QemuOptsList runtime_opts
= {
583 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
586 .name
= CURL_BLOCK_OPT_URL
,
587 .type
= QEMU_OPT_STRING
,
588 .help
= "URL to open",
591 .name
= CURL_BLOCK_OPT_READAHEAD
,
592 .type
= QEMU_OPT_SIZE
,
593 .help
= "Readahead size",
596 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
597 .type
= QEMU_OPT_BOOL
,
598 .help
= "Verify SSL certificate"
601 .name
= CURL_BLOCK_OPT_TIMEOUT
,
602 .type
= QEMU_OPT_NUMBER
,
603 .help
= "Curl timeout"
606 .name
= CURL_BLOCK_OPT_COOKIE
,
607 .type
= QEMU_OPT_STRING
,
608 .help
= "Pass the cookie or list of cookies with each request"
611 .name
= CURL_BLOCK_OPT_COOKIE_SECRET
,
612 .type
= QEMU_OPT_STRING
,
613 .help
= "ID of secret used as cookie passed with each request"
616 .name
= CURL_BLOCK_OPT_USERNAME
,
617 .type
= QEMU_OPT_STRING
,
618 .help
= "Username for HTTP auth"
621 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
622 .type
= QEMU_OPT_STRING
,
623 .help
= "ID of secret used as password for HTTP auth",
626 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
627 .type
= QEMU_OPT_STRING
,
628 .help
= "Username for HTTP proxy auth"
631 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
632 .type
= QEMU_OPT_STRING
,
633 .help
= "ID of secret used as password for HTTP proxy auth",
635 { /* end of list */ }
640 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
643 BDRVCURLState
*s
= bs
->opaque
;
644 CURLState
*state
= NULL
;
648 const char *cookie_secret
;
650 const char *secretid
;
651 const char *protocol_delimiter
;
654 ret
= bdrv_apply_auto_read_only(bs
, "curl driver does not support writes",
660 if (!libcurl_initialized
) {
661 ret
= curl_global_init(CURL_GLOBAL_ALL
);
663 error_setg(errp
, "libcurl initialization failed with %d", ret
);
666 libcurl_initialized
= true;
669 qemu_mutex_init(&s
->mutex
);
670 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
671 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
675 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
676 CURL_BLOCK_OPT_READAHEAD_DEFAULT
);
677 if ((s
->readahead_size
& 0x1ff) != 0) {
678 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
683 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
684 CURL_BLOCK_OPT_TIMEOUT_DEFAULT
);
685 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
686 error_setg(errp
, "timeout parameter is too large or negative");
690 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
,
691 CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
);
693 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
694 cookie_secret
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE_SECRET
);
696 if (cookie
&& cookie_secret
) {
698 "curl driver cannot handle both cookie and cookie secret");
703 s
->cookie
= qcrypto_secret_lookup_as_utf8(cookie_secret
, errp
);
708 s
->cookie
= g_strdup(cookie
);
711 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
713 error_setg(errp
, "curl block driver requires an 'url' option");
717 if (!strstart(file
, bs
->drv
->protocol_name
, &protocol_delimiter
) ||
718 !strstart(protocol_delimiter
, "://", NULL
))
720 error_setg(errp
, "%s curl driver cannot handle the URL '%s' (does not "
721 "start with '%s://')", bs
->drv
->protocol_name
, file
,
722 bs
->drv
->protocol_name
);
726 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
727 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
730 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
736 s
->proxyusername
= g_strdup(
737 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
738 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
740 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
741 if (!s
->proxypassword
) {
746 trace_curl_open(file
);
747 qemu_co_queue_init(&s
->free_state_waitq
);
748 s
->aio_context
= bdrv_get_aio_context(bs
);
749 s
->url
= g_strdup(file
);
750 s
->sockets
= g_hash_table_new_full(NULL
, NULL
, NULL
, g_free
);
751 qemu_mutex_lock(&s
->mutex
);
752 state
= curl_find_state(s
);
753 qemu_mutex_unlock(&s
->mutex
);
760 if (curl_init_state(s
, state
) < 0) {
764 s
->accept_range
= false;
765 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
766 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
768 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
769 if (curl_easy_perform(state
->curl
))
771 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
774 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
775 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
776 * known and zero if it is really zero-length file. */
777 #if LIBCURL_VERSION_NUM >= 0x071304
779 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
780 "Server didn't report file size.");
785 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
786 "Unknown file size or zero-length file.");
793 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
794 || !strncasecmp(s
->url
, "https://", strlen("https://")))
795 && !s
->accept_range
) {
796 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
797 "Server does not support 'range' (byte ranges).");
800 trace_curl_open_size(s
->len
);
802 qemu_mutex_lock(&s
->mutex
);
803 curl_clean_state(state
);
804 qemu_mutex_unlock(&s
->mutex
);
805 curl_easy_cleanup(state
->curl
);
808 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
814 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
815 curl_easy_cleanup(state
->curl
);
818 qemu_mutex_destroy(&s
->mutex
);
822 g_free(s
->proxyusername
);
823 g_free(s
->proxypassword
);
824 curl_drop_all_sockets(s
->sockets
);
825 g_hash_table_destroy(s
->sockets
);
830 static void curl_setup_preadv(BlockDriverState
*bs
, CURLAIOCB
*acb
)
835 BDRVCURLState
*s
= bs
->opaque
;
837 uint64_t start
= acb
->offset
;
840 qemu_mutex_lock(&s
->mutex
);
842 // In case we have the requested data already (e.g. read-ahead),
843 // we can just call the callback and be done.
844 if (curl_find_buf(s
, start
, acb
->bytes
, acb
)) {
848 // No cache found, so let's start a new request
850 state
= curl_find_state(s
);
854 qemu_co_queue_wait(&s
->free_state_waitq
, &s
->mutex
);
857 if (curl_init_state(s
, state
) < 0) {
858 curl_clean_state(state
);
864 acb
->end
= MIN(acb
->bytes
, s
->len
- start
);
867 g_free(state
->orig_buf
);
868 state
->buf_start
= start
;
869 state
->buf_len
= MIN(acb
->end
+ s
->readahead_size
, s
->len
- start
);
870 end
= start
+ state
->buf_len
- 1;
871 state
->orig_buf
= g_try_malloc(state
->buf_len
);
872 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
873 curl_clean_state(state
);
879 snprintf(state
->range
, 127, "%" PRIu64
"-%" PRIu64
, start
, end
);
880 trace_curl_setup_preadv(acb
->bytes
, start
, state
->range
);
881 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
883 if (curl_multi_add_handle(s
->multi
, state
->curl
) != CURLM_OK
) {
884 state
->acb
[0] = NULL
;
887 curl_clean_state(state
);
891 /* Tell curl it needs to kick things off */
892 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
895 qemu_mutex_unlock(&s
->mutex
);
898 static int coroutine_fn
curl_co_preadv(BlockDriverState
*bs
,
899 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
902 .co
= qemu_coroutine_self(),
909 curl_setup_preadv(bs
, &acb
);
910 while (acb
.ret
== -EINPROGRESS
) {
911 qemu_coroutine_yield();
916 static void curl_close(BlockDriverState
*bs
)
918 BDRVCURLState
*s
= bs
->opaque
;
921 curl_detach_aio_context(bs
);
922 qemu_mutex_destroy(&s
->mutex
);
924 g_hash_table_destroy(s
->sockets
);
928 g_free(s
->proxyusername
);
929 g_free(s
->proxypassword
);
932 static int64_t curl_getlength(BlockDriverState
*bs
)
934 BDRVCURLState
*s
= bs
->opaque
;
938 static void curl_refresh_filename(BlockDriverState
*bs
)
940 BDRVCURLState
*s
= bs
->opaque
;
942 /* "readahead" and "timeout" do not change the guest-visible data,
944 if (s
->sslverify
!= CURL_BLOCK_OPT_SSLVERIFY_DEFAULT
||
945 s
->cookie
|| s
->username
|| s
->password
|| s
->proxyusername
||
951 pstrcpy(bs
->exact_filename
, sizeof(bs
->exact_filename
), s
->url
);
955 static const char *const curl_strong_runtime_opts
[] = {
957 CURL_BLOCK_OPT_SSLVERIFY
,
958 CURL_BLOCK_OPT_COOKIE
,
959 CURL_BLOCK_OPT_COOKIE_SECRET
,
960 CURL_BLOCK_OPT_USERNAME
,
961 CURL_BLOCK_OPT_PASSWORD_SECRET
,
962 CURL_BLOCK_OPT_PROXY_USERNAME
,
963 CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
968 static BlockDriver bdrv_http
= {
969 .format_name
= "http",
970 .protocol_name
= "http",
972 .instance_size
= sizeof(BDRVCURLState
),
973 .bdrv_parse_filename
= curl_parse_filename
,
974 .bdrv_file_open
= curl_open
,
975 .bdrv_close
= curl_close
,
976 .bdrv_getlength
= curl_getlength
,
978 .bdrv_co_preadv
= curl_co_preadv
,
980 .bdrv_detach_aio_context
= curl_detach_aio_context
,
981 .bdrv_attach_aio_context
= curl_attach_aio_context
,
983 .bdrv_refresh_filename
= curl_refresh_filename
,
984 .strong_runtime_opts
= curl_strong_runtime_opts
,
987 static BlockDriver bdrv_https
= {
988 .format_name
= "https",
989 .protocol_name
= "https",
991 .instance_size
= sizeof(BDRVCURLState
),
992 .bdrv_parse_filename
= curl_parse_filename
,
993 .bdrv_file_open
= curl_open
,
994 .bdrv_close
= curl_close
,
995 .bdrv_getlength
= curl_getlength
,
997 .bdrv_co_preadv
= curl_co_preadv
,
999 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1000 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1002 .bdrv_refresh_filename
= curl_refresh_filename
,
1003 .strong_runtime_opts
= curl_strong_runtime_opts
,
1006 static BlockDriver bdrv_ftp
= {
1007 .format_name
= "ftp",
1008 .protocol_name
= "ftp",
1010 .instance_size
= sizeof(BDRVCURLState
),
1011 .bdrv_parse_filename
= curl_parse_filename
,
1012 .bdrv_file_open
= curl_open
,
1013 .bdrv_close
= curl_close
,
1014 .bdrv_getlength
= curl_getlength
,
1016 .bdrv_co_preadv
= curl_co_preadv
,
1018 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1019 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1021 .bdrv_refresh_filename
= curl_refresh_filename
,
1022 .strong_runtime_opts
= curl_strong_runtime_opts
,
1025 static BlockDriver bdrv_ftps
= {
1026 .format_name
= "ftps",
1027 .protocol_name
= "ftps",
1029 .instance_size
= sizeof(BDRVCURLState
),
1030 .bdrv_parse_filename
= curl_parse_filename
,
1031 .bdrv_file_open
= curl_open
,
1032 .bdrv_close
= curl_close
,
1033 .bdrv_getlength
= curl_getlength
,
1035 .bdrv_co_preadv
= curl_co_preadv
,
1037 .bdrv_detach_aio_context
= curl_detach_aio_context
,
1038 .bdrv_attach_aio_context
= curl_attach_aio_context
,
1040 .bdrv_refresh_filename
= curl_refresh_filename
,
1041 .strong_runtime_opts
= curl_strong_runtime_opts
,
1044 static void curl_block_init(void)
1046 bdrv_register(&bdrv_http
);
1047 bdrv_register(&bdrv_https
);
1048 bdrv_register(&bdrv_ftp
);
1049 bdrv_register(&bdrv_ftps
);
1052 block_init(curl_block_init
);