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
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
27 #include "qemu/error-report.h"
28 #include "block/block_int.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qstring.h"
31 #include "crypto/secret.h"
32 #include <curl/curl.h>
33 #include "qemu/cutils.h"
36 // #define DEBUG_VERBOSE
39 #define DEBUG_CURL_PRINT 1
41 #define DEBUG_CURL_PRINT 0
43 #define DPRINTF(fmt, ...) \
45 if (DEBUG_CURL_PRINT) { \
46 fprintf(stderr, fmt, ## __VA_ARGS__); \
50 #if LIBCURL_VERSION_NUM >= 0x071000
51 /* The multi interface timer callback was introduced in 7.16.0 */
52 #define NEED_CURL_TIMER_CALLBACK
53 #define HAVE_SOCKET_ACTION
56 #ifndef HAVE_SOCKET_ACTION
57 /* If curl_multi_socket_action isn't available, define it statically here in
58 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
59 * less efficient but still safe. */
60 static CURLMcode
__curl_multi_socket_action(CURLM
*multi_handle
,
65 return curl_multi_socket(multi_handle
, sockfd
, running_handles
);
67 #define curl_multi_socket_action __curl_multi_socket_action
70 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
71 CURLPROTO_FTP | CURLPROTO_FTPS)
73 #define CURL_NUM_STATES 8
74 #define CURL_NUM_ACB 8
75 #define READ_AHEAD_DEFAULT (256 * 1024)
76 #define CURL_TIMEOUT_DEFAULT 5
77 #define CURL_TIMEOUT_MAX 10000
79 #define FIND_RET_NONE 0
81 #define FIND_RET_WAIT 2
83 #define CURL_BLOCK_OPT_URL "url"
84 #define CURL_BLOCK_OPT_READAHEAD "readahead"
85 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
86 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
87 #define CURL_BLOCK_OPT_COOKIE "cookie"
88 #define CURL_BLOCK_OPT_USERNAME "username"
89 #define CURL_BLOCK_OPT_PASSWORD_SECRET "password-secret"
90 #define CURL_BLOCK_OPT_PROXY_USERNAME "proxy-username"
91 #define CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET "proxy-password-secret"
95 typedef struct CURLAIOCB
{
106 typedef struct CURLSocket
{
108 QLIST_ENTRY(CURLSocket
) next
;
111 typedef struct CURLState
113 struct BDRVCURLState
*s
;
114 CURLAIOCB
*acb
[CURL_NUM_ACB
];
116 QLIST_HEAD(, CURLSocket
) sockets
;
122 char errmsg
[CURL_ERROR_SIZE
];
126 typedef struct BDRVCURLState
{
130 CURLState states
[CURL_NUM_STATES
];
132 size_t readahead_size
;
137 AioContext
*aio_context
;
144 static void curl_clean_state(CURLState
*s
);
145 static void curl_multi_do(void *arg
);
146 static void curl_multi_read(void *arg
);
148 #ifdef NEED_CURL_TIMER_CALLBACK
149 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
151 BDRVCURLState
*s
= opaque
;
153 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms
);
154 if (timeout_ms
== -1) {
155 timer_del(&s
->timer
);
157 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
159 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
165 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
166 void *userp
, void *sp
)
169 CURLState
*state
= NULL
;
172 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
175 QLIST_FOREACH(socket
, &state
->sockets
, next
) {
176 if (socket
->fd
== fd
) {
177 if (action
== CURL_POLL_REMOVE
) {
178 QLIST_REMOVE(socket
, next
);
185 socket
= g_new0(CURLSocket
, 1);
187 QLIST_INSERT_HEAD(&state
->sockets
, socket
, next
);
191 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action
, (int)fd
);
194 aio_set_fd_handler(s
->aio_context
, fd
, false,
195 curl_multi_read
, NULL
, NULL
, state
);
198 aio_set_fd_handler(s
->aio_context
, fd
, false,
199 NULL
, curl_multi_do
, NULL
, state
);
201 case CURL_POLL_INOUT
:
202 aio_set_fd_handler(s
->aio_context
, fd
, false,
203 curl_multi_read
, curl_multi_do
, NULL
, state
);
205 case CURL_POLL_REMOVE
:
206 aio_set_fd_handler(s
->aio_context
, fd
, false,
207 NULL
, NULL
, NULL
, NULL
);
214 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
216 BDRVCURLState
*s
= opaque
;
217 size_t realsize
= size
* nmemb
;
218 const char *accept_line
= "Accept-Ranges: bytes";
220 if (realsize
>= strlen(accept_line
)
221 && strncmp((char *)ptr
, accept_line
, strlen(accept_line
)) == 0) {
222 s
->accept_range
= true;
228 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
230 CURLState
*s
= ((CURLState
*)opaque
);
231 size_t realsize
= size
* nmemb
;
234 DPRINTF("CURL: Just reading %zd bytes\n", realsize
);
236 if (!s
|| !s
->orig_buf
) {
240 if (s
->buf_off
>= s
->buf_len
) {
241 /* buffer full, read nothing */
244 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
245 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
246 s
->buf_off
+= realsize
;
248 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
249 CURLAIOCB
*acb
= s
->acb
[i
];
254 if ((s
->buf_off
>= acb
->end
)) {
255 size_t request_length
= acb
->nb_sectors
* BDRV_SECTOR_SIZE
;
257 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
258 acb
->end
- acb
->start
);
260 if (acb
->end
- acb
->start
< request_length
) {
261 size_t offset
= acb
->end
- acb
->start
;
262 qemu_iovec_memset(acb
->qiov
, offset
, 0,
263 request_length
- offset
);
266 acb
->common
.cb(acb
->common
.opaque
, 0);
273 /* curl will error out if we do not return this value */
277 static int curl_find_buf(BDRVCURLState
*s
, size_t start
, size_t len
,
281 size_t end
= start
+ len
;
282 size_t clamped_end
= MIN(end
, s
->len
);
283 size_t clamped_len
= clamped_end
- start
;
285 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
286 CURLState
*state
= &s
->states
[i
];
287 size_t buf_end
= (state
->buf_start
+ state
->buf_off
);
288 size_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
290 if (!state
->orig_buf
)
295 // Does the existing buffer cover our section?
296 if ((start
>= state
->buf_start
) &&
297 (start
<= buf_end
) &&
298 (clamped_end
>= state
->buf_start
) &&
299 (clamped_end
<= buf_end
))
301 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
303 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, clamped_len
);
304 if (clamped_len
< len
) {
305 qemu_iovec_memset(acb
->qiov
, clamped_len
, 0, len
- clamped_len
);
307 acb
->common
.cb(acb
->common
.opaque
, 0);
312 // Wait for unfinished chunks
314 (start
>= state
->buf_start
) &&
315 (start
<= buf_fend
) &&
316 (clamped_end
>= state
->buf_start
) &&
317 (clamped_end
<= buf_fend
))
321 acb
->start
= start
- state
->buf_start
;
322 acb
->end
= acb
->start
+ clamped_len
;
324 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
325 if (!state
->acb
[j
]) {
327 return FIND_RET_WAIT
;
333 return FIND_RET_NONE
;
336 static void curl_multi_check_completion(BDRVCURLState
*s
)
340 /* Try to find done transfers, so we can free the easy
344 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
346 /* Quit when there are no more completions */
350 if (msg
->msg
== CURLMSG_DONE
) {
351 CURLState
*state
= NULL
;
352 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
355 /* ACBs for successful messages get completed in curl_read_cb */
356 if (msg
->data
.result
!= CURLE_OK
) {
358 static int errcount
= 100;
360 /* Don't lose the original error message from curl, since
361 * it contains extra data.
364 error_report("curl: %s", state
->errmsg
);
365 if (--errcount
== 0) {
366 error_report("curl: further errors suppressed");
370 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
371 CURLAIOCB
*acb
= state
->acb
[i
];
377 acb
->common
.cb(acb
->common
.opaque
, -EPROTO
);
379 state
->acb
[i
] = NULL
;
383 curl_clean_state(state
);
389 static void curl_multi_do(void *arg
)
391 CURLState
*s
= (CURLState
*)arg
;
392 CURLSocket
*socket
, *next_socket
;
400 /* Need to use _SAFE because curl_multi_socket_action() may trigger
401 * curl_sock_cb() which might modify this list */
402 QLIST_FOREACH_SAFE(socket
, &s
->sockets
, next
, next_socket
) {
404 r
= curl_multi_socket_action(s
->s
->multi
, socket
->fd
, 0, &running
);
405 } while (r
== CURLM_CALL_MULTI_PERFORM
);
409 static void curl_multi_read(void *arg
)
411 CURLState
*s
= (CURLState
*)arg
;
414 curl_multi_check_completion(s
->s
);
417 static void curl_multi_timeout_do(void *arg
)
419 #ifdef NEED_CURL_TIMER_CALLBACK
420 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
427 aio_context_acquire(s
->aio_context
);
428 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
430 curl_multi_check_completion(s
);
431 aio_context_release(s
->aio_context
);
437 static CURLState
*curl_init_state(BlockDriverState
*bs
, BDRVCURLState
*s
)
439 CURLState
*state
= NULL
;
443 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
444 for (j
=0; j
<CURL_NUM_ACB
; j
++)
445 if (s
->states
[i
].acb
[j
])
447 if (s
->states
[i
].in_use
)
450 state
= &s
->states
[i
];
455 aio_poll(bdrv_get_aio_context(bs
), true);
460 state
->curl
= curl_easy_init();
464 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
465 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
466 (long) s
->sslverify
);
468 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
470 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
471 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
472 (void *)curl_read_cb
);
473 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
474 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
475 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
476 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
477 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
478 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
479 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
482 curl_easy_setopt(state
->curl
, CURLOPT_USERNAME
, s
->username
);
485 curl_easy_setopt(state
->curl
, CURLOPT_PASSWORD
, s
->password
);
487 if (s
->proxyusername
) {
488 curl_easy_setopt(state
->curl
,
489 CURLOPT_PROXYUSERNAME
, s
->proxyusername
);
491 if (s
->proxypassword
) {
492 curl_easy_setopt(state
->curl
,
493 CURLOPT_PROXYPASSWORD
, s
->proxypassword
);
496 /* Restrict supported protocols to avoid security issues in the more
497 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
500 * Restricting protocols is only supported from 7.19.4 upwards.
502 #if LIBCURL_VERSION_NUM >= 0x071304
503 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
504 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
508 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
512 QLIST_INIT(&state
->sockets
);
518 static void curl_clean_state(CURLState
*s
)
521 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
523 while (!QLIST_EMPTY(&s
->sockets
)) {
524 CURLSocket
*socket
= QLIST_FIRST(&s
->sockets
);
526 QLIST_REMOVE(socket
, next
);
533 static void curl_parse_filename(const char *filename
, QDict
*options
,
536 qdict_put(options
, CURL_BLOCK_OPT_URL
, qstring_from_str(filename
));
539 static void curl_detach_aio_context(BlockDriverState
*bs
)
541 BDRVCURLState
*s
= bs
->opaque
;
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
);
560 timer_del(&s
->timer
);
563 static void curl_attach_aio_context(BlockDriverState
*bs
,
564 AioContext
*new_context
)
566 BDRVCURLState
*s
= bs
->opaque
;
568 aio_timer_init(new_context
, &s
->timer
,
569 QEMU_CLOCK_REALTIME
, SCALE_NS
,
570 curl_multi_timeout_do
, s
);
573 s
->multi
= curl_multi_init();
574 s
->aio_context
= new_context
;
575 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
576 #ifdef NEED_CURL_TIMER_CALLBACK
577 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
578 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_USERNAME
,
613 .type
= QEMU_OPT_STRING
,
614 .help
= "Username for HTTP auth"
617 .name
= CURL_BLOCK_OPT_PASSWORD_SECRET
,
618 .type
= QEMU_OPT_STRING
,
619 .help
= "ID of secret used as password for HTTP auth",
622 .name
= CURL_BLOCK_OPT_PROXY_USERNAME
,
623 .type
= QEMU_OPT_STRING
,
624 .help
= "Username for HTTP proxy auth"
627 .name
= CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
,
628 .type
= QEMU_OPT_STRING
,
629 .help
= "ID of secret used as password for HTTP proxy auth",
631 { /* end of list */ }
636 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
639 BDRVCURLState
*s
= bs
->opaque
;
640 CURLState
*state
= NULL
;
642 Error
*local_err
= NULL
;
646 const char *secretid
;
648 static int inited
= 0;
650 if (flags
& BDRV_O_RDWR
) {
651 error_setg(errp
, "curl block device does not support writes");
655 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
656 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
658 error_propagate(errp
, local_err
);
662 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
664 if ((s
->readahead_size
& 0x1ff) != 0) {
665 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
670 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
671 CURL_TIMEOUT_DEFAULT
);
672 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
673 error_setg(errp
, "timeout parameter is too large or negative");
677 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
, true);
679 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
680 s
->cookie
= g_strdup(cookie
);
682 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
684 error_setg(errp
, "curl block driver requires an 'url' option");
688 s
->username
= g_strdup(qemu_opt_get(opts
, CURL_BLOCK_OPT_USERNAME
));
689 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PASSWORD_SECRET
);
692 s
->password
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
698 s
->proxyusername
= g_strdup(
699 qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_USERNAME
));
700 secretid
= qemu_opt_get(opts
, CURL_BLOCK_OPT_PROXY_PASSWORD_SECRET
);
702 s
->proxypassword
= qcrypto_secret_lookup_as_utf8(secretid
, errp
);
703 if (!s
->proxypassword
) {
709 curl_global_init(CURL_GLOBAL_ALL
);
713 DPRINTF("CURL: Opening %s\n", file
);
714 s
->aio_context
= bdrv_get_aio_context(bs
);
715 s
->url
= g_strdup(file
);
716 state
= curl_init_state(bs
, s
);
722 s
->accept_range
= false;
723 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
724 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
726 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
727 if (curl_easy_perform(state
->curl
))
729 if (curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
732 /* Prior CURL 7.19.4 return value of 0 could mean that the file size is not
733 * know or the size is zero. From 7.19.4 CURL returns -1 if size is not
734 * known and zero if it is realy zero-length file. */
735 #if LIBCURL_VERSION_NUM >= 0x071304
737 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
738 "Server didn't report file size.");
743 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
744 "Unknown file size or zero-length file.");
751 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
752 || !strncasecmp(s
->url
, "https://", strlen("https://")))
753 && !s
->accept_range
) {
754 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
755 "Server does not support 'range' (byte ranges).");
758 DPRINTF("CURL: Size = %zd\n", s
->len
);
760 curl_clean_state(state
);
761 curl_easy_cleanup(state
->curl
);
764 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
770 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
771 curl_easy_cleanup(state
->curl
);
780 static const AIOCBInfo curl_aiocb_info
= {
781 .aiocb_size
= sizeof(CURLAIOCB
),
785 static void curl_readv_bh_cb(void *p
)
791 BDRVCURLState
*s
= acb
->common
.bs
->opaque
;
793 size_t start
= acb
->sector_num
* BDRV_SECTOR_SIZE
;
796 // In case we have the requested data already (e.g. read-ahead),
797 // we can just call the callback and be done.
798 switch (curl_find_buf(s
, start
, acb
->nb_sectors
* BDRV_SECTOR_SIZE
, acb
)) {
808 // No cache found, so let's start a new request
809 state
= curl_init_state(acb
->common
.bs
, s
);
811 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
817 acb
->end
= MIN(acb
->nb_sectors
* BDRV_SECTOR_SIZE
, s
->len
- start
);
820 g_free(state
->orig_buf
);
821 state
->buf_start
= start
;
822 state
->buf_len
= MIN(acb
->end
+ s
->readahead_size
, s
->len
- start
);
823 end
= start
+ state
->buf_len
- 1;
824 state
->orig_buf
= g_try_malloc(state
->buf_len
);
825 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
826 curl_clean_state(state
);
827 acb
->common
.cb(acb
->common
.opaque
, -ENOMEM
);
833 snprintf(state
->range
, 127, "%zd-%zd", start
, end
);
834 DPRINTF("CURL (AIO): Reading %llu at %zd (%s)\n",
835 (acb
->nb_sectors
* BDRV_SECTOR_SIZE
), start
, state
->range
);
836 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
838 curl_multi_add_handle(s
->multi
, state
->curl
);
840 /* Tell curl it needs to kick things off */
841 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
844 static BlockAIOCB
*curl_aio_readv(BlockDriverState
*bs
,
845 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
846 BlockCompletionFunc
*cb
, void *opaque
)
850 acb
= qemu_aio_get(&curl_aiocb_info
, bs
, cb
, opaque
);
853 acb
->sector_num
= sector_num
;
854 acb
->nb_sectors
= nb_sectors
;
856 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
), curl_readv_bh_cb
, acb
);
860 static void curl_close(BlockDriverState
*bs
)
862 BDRVCURLState
*s
= bs
->opaque
;
864 DPRINTF("CURL: Close\n");
865 curl_detach_aio_context(bs
);
871 static int64_t curl_getlength(BlockDriverState
*bs
)
873 BDRVCURLState
*s
= bs
->opaque
;
877 static BlockDriver bdrv_http
= {
878 .format_name
= "http",
879 .protocol_name
= "http",
881 .instance_size
= sizeof(BDRVCURLState
),
882 .bdrv_parse_filename
= curl_parse_filename
,
883 .bdrv_file_open
= curl_open
,
884 .bdrv_close
= curl_close
,
885 .bdrv_getlength
= curl_getlength
,
887 .bdrv_aio_readv
= curl_aio_readv
,
889 .bdrv_detach_aio_context
= curl_detach_aio_context
,
890 .bdrv_attach_aio_context
= curl_attach_aio_context
,
893 static BlockDriver bdrv_https
= {
894 .format_name
= "https",
895 .protocol_name
= "https",
897 .instance_size
= sizeof(BDRVCURLState
),
898 .bdrv_parse_filename
= curl_parse_filename
,
899 .bdrv_file_open
= curl_open
,
900 .bdrv_close
= curl_close
,
901 .bdrv_getlength
= curl_getlength
,
903 .bdrv_aio_readv
= curl_aio_readv
,
905 .bdrv_detach_aio_context
= curl_detach_aio_context
,
906 .bdrv_attach_aio_context
= curl_attach_aio_context
,
909 static BlockDriver bdrv_ftp
= {
910 .format_name
= "ftp",
911 .protocol_name
= "ftp",
913 .instance_size
= sizeof(BDRVCURLState
),
914 .bdrv_parse_filename
= curl_parse_filename
,
915 .bdrv_file_open
= curl_open
,
916 .bdrv_close
= curl_close
,
917 .bdrv_getlength
= curl_getlength
,
919 .bdrv_aio_readv
= curl_aio_readv
,
921 .bdrv_detach_aio_context
= curl_detach_aio_context
,
922 .bdrv_attach_aio_context
= curl_attach_aio_context
,
925 static BlockDriver bdrv_ftps
= {
926 .format_name
= "ftps",
927 .protocol_name
= "ftps",
929 .instance_size
= sizeof(BDRVCURLState
),
930 .bdrv_parse_filename
= curl_parse_filename
,
931 .bdrv_file_open
= curl_open
,
932 .bdrv_close
= curl_close
,
933 .bdrv_getlength
= curl_getlength
,
935 .bdrv_aio_readv
= curl_aio_readv
,
937 .bdrv_detach_aio_context
= curl_detach_aio_context
,
938 .bdrv_attach_aio_context
= curl_attach_aio_context
,
941 static void curl_block_init(void)
943 bdrv_register(&bdrv_http
);
944 bdrv_register(&bdrv_https
);
945 bdrv_register(&bdrv_ftp
);
946 bdrv_register(&bdrv_ftps
);
949 block_init(curl_block_init
);