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-common.h"
25 #include "block/block_int.h"
26 #include "qapi/qmp/qbool.h"
27 #include "qapi/qmp/qstring.h"
28 #include <curl/curl.h>
31 // #define DEBUG_VERBOSE
34 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
36 #define DPRINTF(fmt, ...) do { } while (0)
39 #if LIBCURL_VERSION_NUM >= 0x071000
40 /* The multi interface timer callback was introduced in 7.16.0 */
41 #define NEED_CURL_TIMER_CALLBACK
42 #define HAVE_SOCKET_ACTION
45 #ifndef HAVE_SOCKET_ACTION
46 /* If curl_multi_socket_action isn't available, define it statically here in
47 * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
48 * less efficient but still safe. */
49 static CURLMcode
__curl_multi_socket_action(CURLM
*multi_handle
,
54 return curl_multi_socket(multi_handle
, sockfd
, running_handles
);
56 #define curl_multi_socket_action __curl_multi_socket_action
59 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
60 CURLPROTO_FTP | CURLPROTO_FTPS | \
63 #define CURL_NUM_STATES 8
64 #define CURL_NUM_ACB 8
65 #define SECTOR_SIZE 512
66 #define READ_AHEAD_DEFAULT (256 * 1024)
67 #define CURL_TIMEOUT_DEFAULT 5
68 #define CURL_TIMEOUT_MAX 10000
70 #define FIND_RET_NONE 0
72 #define FIND_RET_WAIT 2
74 #define CURL_BLOCK_OPT_URL "url"
75 #define CURL_BLOCK_OPT_READAHEAD "readahead"
76 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
77 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
78 #define CURL_BLOCK_OPT_COOKIE "cookie"
82 typedef struct CURLAIOCB
{
94 typedef struct CURLState
96 struct BDRVCURLState
*s
;
97 CURLAIOCB
*acb
[CURL_NUM_ACB
];
99 curl_socket_t sock_fd
;
105 char errmsg
[CURL_ERROR_SIZE
];
109 typedef struct BDRVCURLState
{
113 CURLState states
[CURL_NUM_STATES
];
115 size_t readahead_size
;
120 AioContext
*aio_context
;
123 static void curl_clean_state(CURLState
*s
);
124 static void curl_multi_do(void *arg
);
125 static void curl_multi_read(void *arg
);
127 #ifdef NEED_CURL_TIMER_CALLBACK
128 static int curl_timer_cb(CURLM
*multi
, long timeout_ms
, void *opaque
)
130 BDRVCURLState
*s
= opaque
;
132 DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms
);
133 if (timeout_ms
== -1) {
134 timer_del(&s
->timer
);
136 int64_t timeout_ns
= (int64_t)timeout_ms
* 1000 * 1000;
138 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + timeout_ns
);
144 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
145 void *userp
, void *sp
)
148 CURLState
*state
= NULL
;
149 curl_easy_getinfo(curl
, CURLINFO_PRIVATE
, (char **)&state
);
153 DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action
, fd
);
156 aio_set_fd_handler(s
->aio_context
, fd
, curl_multi_read
,
160 aio_set_fd_handler(s
->aio_context
, fd
, NULL
, curl_multi_do
, state
);
162 case CURL_POLL_INOUT
:
163 aio_set_fd_handler(s
->aio_context
, fd
, curl_multi_read
,
164 curl_multi_do
, state
);
166 case CURL_POLL_REMOVE
:
167 aio_set_fd_handler(s
->aio_context
, fd
, NULL
, NULL
, NULL
);
174 static size_t curl_header_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
176 BDRVCURLState
*s
= opaque
;
177 size_t realsize
= size
* nmemb
;
178 const char *accept_line
= "Accept-Ranges: bytes";
180 if (realsize
>= strlen(accept_line
)
181 && strncmp((char *)ptr
, accept_line
, strlen(accept_line
)) == 0) {
182 s
->accept_range
= true;
188 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
190 CURLState
*s
= ((CURLState
*)opaque
);
191 size_t realsize
= size
* nmemb
;
194 DPRINTF("CURL: Just reading %zd bytes\n", realsize
);
196 if (!s
|| !s
->orig_buf
)
199 if (s
->buf_off
>= s
->buf_len
) {
200 /* buffer full, read nothing */
203 realsize
= MIN(realsize
, s
->buf_len
- s
->buf_off
);
204 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
205 s
->buf_off
+= realsize
;
207 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
208 CURLAIOCB
*acb
= s
->acb
[i
];
213 if ((s
->buf_off
>= acb
->end
)) {
214 qemu_iovec_from_buf(acb
->qiov
, 0, s
->orig_buf
+ acb
->start
,
215 acb
->end
- acb
->start
);
216 acb
->common
.cb(acb
->common
.opaque
, 0);
225 static int curl_find_buf(BDRVCURLState
*s
, size_t start
, size_t len
,
229 size_t end
= start
+ len
;
231 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
232 CURLState
*state
= &s
->states
[i
];
233 size_t buf_end
= (state
->buf_start
+ state
->buf_off
);
234 size_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
236 if (!state
->orig_buf
)
241 // Does the existing buffer cover our section?
242 if ((start
>= state
->buf_start
) &&
243 (start
<= buf_end
) &&
244 (end
>= state
->buf_start
) &&
247 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
249 qemu_iovec_from_buf(acb
->qiov
, 0, buf
, len
);
250 acb
->common
.cb(acb
->common
.opaque
, 0);
255 // Wait for unfinished chunks
257 (start
>= state
->buf_start
) &&
258 (start
<= buf_fend
) &&
259 (end
>= state
->buf_start
) &&
264 acb
->start
= start
- state
->buf_start
;
265 acb
->end
= acb
->start
+ len
;
267 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
268 if (!state
->acb
[j
]) {
270 return FIND_RET_WAIT
;
276 return FIND_RET_NONE
;
279 static void curl_multi_check_completion(BDRVCURLState
*s
)
283 /* Try to find done transfers, so we can free the easy
287 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
289 /* Quit when there are no more completions */
293 if (msg
->msg
== CURLMSG_DONE
) {
294 CURLState
*state
= NULL
;
295 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
,
298 /* ACBs for successful messages get completed in curl_read_cb */
299 if (msg
->data
.result
!= CURLE_OK
) {
301 for (i
= 0; i
< CURL_NUM_ACB
; i
++) {
302 CURLAIOCB
*acb
= state
->acb
[i
];
308 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
310 state
->acb
[i
] = NULL
;
314 curl_clean_state(state
);
320 static void curl_multi_do(void *arg
)
322 CURLState
*s
= (CURLState
*)arg
;
331 r
= curl_multi_socket_action(s
->s
->multi
, s
->sock_fd
, 0, &running
);
332 } while(r
== CURLM_CALL_MULTI_PERFORM
);
336 static void curl_multi_read(void *arg
)
338 CURLState
*s
= (CURLState
*)arg
;
341 curl_multi_check_completion(s
->s
);
344 static void curl_multi_timeout_do(void *arg
)
346 #ifdef NEED_CURL_TIMER_CALLBACK
347 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
354 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
356 curl_multi_check_completion(s
);
362 static CURLState
*curl_init_state(BlockDriverState
*bs
, BDRVCURLState
*s
)
364 CURLState
*state
= NULL
;
368 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
369 for (j
=0; j
<CURL_NUM_ACB
; j
++)
370 if (s
->states
[i
].acb
[j
])
372 if (s
->states
[i
].in_use
)
375 state
= &s
->states
[i
];
380 aio_poll(bdrv_get_aio_context(bs
), true);
385 state
->curl
= curl_easy_init();
389 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
390 curl_easy_setopt(state
->curl
, CURLOPT_SSL_VERIFYPEER
,
391 (long) s
->sslverify
);
393 curl_easy_setopt(state
->curl
, CURLOPT_COOKIE
, s
->cookie
);
395 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, (long)s
->timeout
);
396 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
,
397 (void *)curl_read_cb
);
398 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
399 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
400 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
401 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
402 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
403 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
404 curl_easy_setopt(state
->curl
, CURLOPT_FAILONERROR
, 1);
406 /* Restrict supported protocols to avoid security issues in the more
407 * obscure protocols. For example, do not allow POP3/SMTP/IMAP see
410 * Restricting protocols is only supported from 7.19.4 upwards.
412 #if LIBCURL_VERSION_NUM >= 0x071304
413 curl_easy_setopt(state
->curl
, CURLOPT_PROTOCOLS
, PROTOCOLS
);
414 curl_easy_setopt(state
->curl
, CURLOPT_REDIR_PROTOCOLS
, PROTOCOLS
);
418 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
427 static void curl_clean_state(CURLState
*s
)
430 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
434 static void curl_parse_filename(const char *filename
, QDict
*options
,
437 qdict_put(options
, CURL_BLOCK_OPT_URL
, qstring_from_str(filename
));
440 static void curl_detach_aio_context(BlockDriverState
*bs
)
442 BDRVCURLState
*s
= bs
->opaque
;
445 for (i
= 0; i
< CURL_NUM_STATES
; i
++) {
446 if (s
->states
[i
].in_use
) {
447 curl_clean_state(&s
->states
[i
]);
449 if (s
->states
[i
].curl
) {
450 curl_easy_cleanup(s
->states
[i
].curl
);
451 s
->states
[i
].curl
= NULL
;
453 g_free(s
->states
[i
].orig_buf
);
454 s
->states
[i
].orig_buf
= NULL
;
457 curl_multi_cleanup(s
->multi
);
461 timer_del(&s
->timer
);
464 static void curl_attach_aio_context(BlockDriverState
*bs
,
465 AioContext
*new_context
)
467 BDRVCURLState
*s
= bs
->opaque
;
469 aio_timer_init(new_context
, &s
->timer
,
470 QEMU_CLOCK_REALTIME
, SCALE_NS
,
471 curl_multi_timeout_do
, s
);
474 s
->multi
= curl_multi_init();
475 s
->aio_context
= new_context
;
476 curl_multi_setopt(s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
477 #ifdef NEED_CURL_TIMER_CALLBACK
478 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERDATA
, s
);
479 curl_multi_setopt(s
->multi
, CURLMOPT_TIMERFUNCTION
, curl_timer_cb
);
483 static QemuOptsList runtime_opts
= {
485 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
488 .name
= CURL_BLOCK_OPT_URL
,
489 .type
= QEMU_OPT_STRING
,
490 .help
= "URL to open",
493 .name
= CURL_BLOCK_OPT_READAHEAD
,
494 .type
= QEMU_OPT_SIZE
,
495 .help
= "Readahead size",
498 .name
= CURL_BLOCK_OPT_SSLVERIFY
,
499 .type
= QEMU_OPT_BOOL
,
500 .help
= "Verify SSL certificate"
503 .name
= CURL_BLOCK_OPT_TIMEOUT
,
504 .type
= QEMU_OPT_NUMBER
,
505 .help
= "Curl timeout"
508 .name
= CURL_BLOCK_OPT_COOKIE
,
509 .type
= QEMU_OPT_STRING
,
510 .help
= "Pass the cookie or list of cookies with each request"
512 { /* end of list */ }
516 static int curl_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
519 BDRVCURLState
*s
= bs
->opaque
;
520 CURLState
*state
= NULL
;
522 Error
*local_err
= NULL
;
527 static int inited
= 0;
529 if (flags
& BDRV_O_RDWR
) {
530 error_setg(errp
, "curl block device does not support writes");
534 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
535 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
537 error_propagate(errp
, local_err
);
541 s
->readahead_size
= qemu_opt_get_size(opts
, CURL_BLOCK_OPT_READAHEAD
,
543 if ((s
->readahead_size
& 0x1ff) != 0) {
544 error_setg(errp
, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
549 s
->timeout
= qemu_opt_get_number(opts
, CURL_BLOCK_OPT_TIMEOUT
,
550 CURL_TIMEOUT_DEFAULT
);
551 if (s
->timeout
> CURL_TIMEOUT_MAX
) {
552 error_setg(errp
, "timeout parameter is too large or negative");
556 s
->sslverify
= qemu_opt_get_bool(opts
, CURL_BLOCK_OPT_SSLVERIFY
, true);
558 cookie
= qemu_opt_get(opts
, CURL_BLOCK_OPT_COOKIE
);
559 s
->cookie
= g_strdup(cookie
);
561 file
= qemu_opt_get(opts
, CURL_BLOCK_OPT_URL
);
563 error_setg(errp
, "curl block driver requires an 'url' option");
568 curl_global_init(CURL_GLOBAL_ALL
);
572 DPRINTF("CURL: Opening %s\n", file
);
573 s
->aio_context
= bdrv_get_aio_context(bs
);
574 s
->url
= g_strdup(file
);
575 state
= curl_init_state(bs
, s
);
581 s
->accept_range
= false;
582 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
583 curl_easy_setopt(state
->curl
, CURLOPT_HEADERFUNCTION
,
585 curl_easy_setopt(state
->curl
, CURLOPT_HEADERDATA
, s
);
586 if (curl_easy_perform(state
->curl
))
588 curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
);
593 if ((!strncasecmp(s
->url
, "http://", strlen("http://"))
594 || !strncasecmp(s
->url
, "https://", strlen("https://")))
595 && !s
->accept_range
) {
596 pstrcpy(state
->errmsg
, CURL_ERROR_SIZE
,
597 "Server does not support 'range' (byte ranges).");
600 DPRINTF("CURL: Size = %zd\n", s
->len
);
602 curl_clean_state(state
);
603 curl_easy_cleanup(state
->curl
);
606 curl_attach_aio_context(bs
, bdrv_get_aio_context(bs
));
612 error_setg(errp
, "CURL: Error opening file: %s", state
->errmsg
);
613 curl_easy_cleanup(state
->curl
);
622 static const AIOCBInfo curl_aiocb_info
= {
623 .aiocb_size
= sizeof(CURLAIOCB
),
627 static void curl_readv_bh_cb(void *p
)
633 BDRVCURLState
*s
= acb
->common
.bs
->opaque
;
635 qemu_bh_delete(acb
->bh
);
638 size_t start
= acb
->sector_num
* SECTOR_SIZE
;
641 // In case we have the requested data already (e.g. read-ahead),
642 // we can just call the callback and be done.
643 switch (curl_find_buf(s
, start
, acb
->nb_sectors
* SECTOR_SIZE
, acb
)) {
653 // No cache found, so let's start a new request
654 state
= curl_init_state(acb
->common
.bs
, s
);
656 acb
->common
.cb(acb
->common
.opaque
, -EIO
);
662 acb
->end
= (acb
->nb_sectors
* SECTOR_SIZE
);
665 g_free(state
->orig_buf
);
666 state
->buf_start
= start
;
667 state
->buf_len
= acb
->end
+ s
->readahead_size
;
668 end
= MIN(start
+ state
->buf_len
, s
->len
) - 1;
669 state
->orig_buf
= g_try_malloc(state
->buf_len
);
670 if (state
->buf_len
&& state
->orig_buf
== NULL
) {
671 curl_clean_state(state
);
672 acb
->common
.cb(acb
->common
.opaque
, -ENOMEM
);
678 snprintf(state
->range
, 127, "%zd-%zd", start
, end
);
679 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
680 (acb
->nb_sectors
* SECTOR_SIZE
), start
, state
->range
);
681 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
683 curl_multi_add_handle(s
->multi
, state
->curl
);
685 /* Tell curl it needs to kick things off */
686 curl_multi_socket_action(s
->multi
, CURL_SOCKET_TIMEOUT
, 0, &running
);
689 static BlockAIOCB
*curl_aio_readv(BlockDriverState
*bs
,
690 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
691 BlockCompletionFunc
*cb
, void *opaque
)
695 acb
= qemu_aio_get(&curl_aiocb_info
, bs
, cb
, opaque
);
698 acb
->sector_num
= sector_num
;
699 acb
->nb_sectors
= nb_sectors
;
701 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), curl_readv_bh_cb
, acb
);
702 qemu_bh_schedule(acb
->bh
);
706 static void curl_close(BlockDriverState
*bs
)
708 BDRVCURLState
*s
= bs
->opaque
;
710 DPRINTF("CURL: Close\n");
711 curl_detach_aio_context(bs
);
717 static int64_t curl_getlength(BlockDriverState
*bs
)
719 BDRVCURLState
*s
= bs
->opaque
;
723 static BlockDriver bdrv_http
= {
724 .format_name
= "http",
725 .protocol_name
= "http",
727 .instance_size
= sizeof(BDRVCURLState
),
728 .bdrv_parse_filename
= curl_parse_filename
,
729 .bdrv_file_open
= curl_open
,
730 .bdrv_close
= curl_close
,
731 .bdrv_getlength
= curl_getlength
,
733 .bdrv_aio_readv
= curl_aio_readv
,
735 .bdrv_detach_aio_context
= curl_detach_aio_context
,
736 .bdrv_attach_aio_context
= curl_attach_aio_context
,
739 static BlockDriver bdrv_https
= {
740 .format_name
= "https",
741 .protocol_name
= "https",
743 .instance_size
= sizeof(BDRVCURLState
),
744 .bdrv_parse_filename
= curl_parse_filename
,
745 .bdrv_file_open
= curl_open
,
746 .bdrv_close
= curl_close
,
747 .bdrv_getlength
= curl_getlength
,
749 .bdrv_aio_readv
= curl_aio_readv
,
751 .bdrv_detach_aio_context
= curl_detach_aio_context
,
752 .bdrv_attach_aio_context
= curl_attach_aio_context
,
755 static BlockDriver bdrv_ftp
= {
756 .format_name
= "ftp",
757 .protocol_name
= "ftp",
759 .instance_size
= sizeof(BDRVCURLState
),
760 .bdrv_parse_filename
= curl_parse_filename
,
761 .bdrv_file_open
= curl_open
,
762 .bdrv_close
= curl_close
,
763 .bdrv_getlength
= curl_getlength
,
765 .bdrv_aio_readv
= curl_aio_readv
,
767 .bdrv_detach_aio_context
= curl_detach_aio_context
,
768 .bdrv_attach_aio_context
= curl_attach_aio_context
,
771 static BlockDriver bdrv_ftps
= {
772 .format_name
= "ftps",
773 .protocol_name
= "ftps",
775 .instance_size
= sizeof(BDRVCURLState
),
776 .bdrv_parse_filename
= curl_parse_filename
,
777 .bdrv_file_open
= curl_open
,
778 .bdrv_close
= curl_close
,
779 .bdrv_getlength
= curl_getlength
,
781 .bdrv_aio_readv
= curl_aio_readv
,
783 .bdrv_detach_aio_context
= curl_detach_aio_context
,
784 .bdrv_attach_aio_context
= curl_attach_aio_context
,
787 static BlockDriver bdrv_tftp
= {
788 .format_name
= "tftp",
789 .protocol_name
= "tftp",
791 .instance_size
= sizeof(BDRVCURLState
),
792 .bdrv_parse_filename
= curl_parse_filename
,
793 .bdrv_file_open
= curl_open
,
794 .bdrv_close
= curl_close
,
795 .bdrv_getlength
= curl_getlength
,
797 .bdrv_aio_readv
= curl_aio_readv
,
799 .bdrv_detach_aio_context
= curl_detach_aio_context
,
800 .bdrv_attach_aio_context
= curl_attach_aio_context
,
803 static void curl_block_init(void)
805 bdrv_register(&bdrv_http
);
806 bdrv_register(&bdrv_https
);
807 bdrv_register(&bdrv_ftp
);
808 bdrv_register(&bdrv_ftps
);
809 bdrv_register(&bdrv_tftp
);
812 block_init(curl_block_init
);