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_int.h"
26 #include <curl/curl.h>
29 // #define DEBUG_VERBOSE
32 #define dprintf(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
34 #define dprintf(fmt, ...) do { } while (0)
37 #define CURL_NUM_STATES 8
38 #define CURL_NUM_ACB 8
39 #define SECTOR_SIZE 512
40 #define READ_AHEAD_SIZE (256 * 1024)
42 #define FIND_RET_NONE 0
44 #define FIND_RET_WAIT 2
48 typedef struct CURLAIOCB
{
49 BlockDriverAIOCB common
;
55 typedef struct CURLState
57 struct BDRVCURLState
*s
;
58 CURLAIOCB
*acb
[CURL_NUM_ACB
];
65 char errmsg
[CURL_ERROR_SIZE
];
69 typedef struct BDRVCURLState
{
72 CURLState states
[CURL_NUM_STATES
];
74 size_t readahead_size
;
77 static void curl_clean_state(CURLState
*s
);
78 static void curl_multi_do(void *arg
);
80 static int curl_sock_cb(CURL
*curl
, curl_socket_t fd
, int action
,
83 dprintf("CURL (AIO): Sock action %d on fd %d\n", action
, fd
);
86 qemu_aio_set_fd_handler(fd
, curl_multi_do
, NULL
, NULL
, NULL
, s
);
89 qemu_aio_set_fd_handler(fd
, NULL
, curl_multi_do
, NULL
, NULL
, s
);
92 qemu_aio_set_fd_handler(fd
, curl_multi_do
,
93 curl_multi_do
, NULL
, NULL
, s
);
95 case CURL_POLL_REMOVE
:
96 qemu_aio_set_fd_handler(fd
, NULL
, NULL
, NULL
, NULL
, NULL
);
103 static size_t curl_size_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
105 CURLState
*s
= ((CURLState
*)opaque
);
106 size_t realsize
= size
* nmemb
;
109 if(sscanf(ptr
, "Content-Length: %lld", &fsize
) == 1)
115 static size_t curl_read_cb(void *ptr
, size_t size
, size_t nmemb
, void *opaque
)
117 CURLState
*s
= ((CURLState
*)opaque
);
118 size_t realsize
= size
* nmemb
;
121 dprintf("CURL: Just reading %lld bytes\n", (unsigned long long)realsize
);
123 if (!s
|| !s
->orig_buf
)
126 memcpy(s
->orig_buf
+ s
->buf_off
, ptr
, realsize
);
127 s
->buf_off
+= realsize
;
129 for(i
=0; i
<CURL_NUM_ACB
; i
++) {
130 CURLAIOCB
*acb
= s
->acb
[i
];
135 if ((s
->buf_off
>= acb
->end
)) {
136 qemu_iovec_from_buffer(acb
->qiov
, s
->orig_buf
+ acb
->start
,
137 acb
->end
- acb
->start
);
138 acb
->common
.cb(acb
->common
.opaque
, 0);
139 qemu_aio_release(acb
);
148 static int curl_find_buf(BDRVCURLState
*s
, size_t start
, size_t len
,
152 size_t end
= start
+ len
;
154 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
155 CURLState
*state
= &s
->states
[i
];
156 size_t buf_end
= (state
->buf_start
+ state
->buf_off
);
157 size_t buf_fend
= (state
->buf_start
+ state
->buf_len
);
159 if (!state
->orig_buf
)
164 // Does the existing buffer cover our section?
165 if ((start
>= state
->buf_start
) &&
166 (start
<= buf_end
) &&
167 (end
>= state
->buf_start
) &&
170 char *buf
= state
->orig_buf
+ (start
- state
->buf_start
);
172 qemu_iovec_from_buffer(acb
->qiov
, buf
, len
);
173 acb
->common
.cb(acb
->common
.opaque
, 0);
178 // Wait for unfinished chunks
179 if ((start
>= state
->buf_start
) &&
180 (start
<= buf_fend
) &&
181 (end
>= state
->buf_start
) &&
186 acb
->start
= start
- state
->buf_start
;
187 acb
->end
= acb
->start
+ len
;
189 for (j
=0; j
<CURL_NUM_ACB
; j
++) {
190 if (!state
->acb
[j
]) {
192 return FIND_RET_WAIT
;
198 return FIND_RET_NONE
;
201 static void curl_multi_do(void *arg
)
203 BDRVCURLState
*s
= (BDRVCURLState
*)arg
;
212 r
= curl_multi_socket_all(s
->multi
, &running
);
213 } while(r
== CURLM_CALL_MULTI_PERFORM
);
215 /* Try to find done transfers, so we can free the easy
219 msg
= curl_multi_info_read(s
->multi
, &msgs_in_queue
);
223 if (msg
->msg
== CURLMSG_NONE
)
229 CURLState
*state
= NULL
;
230 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
, (char**)&state
);
231 curl_clean_state(state
);
238 } while(msgs_in_queue
);
241 static CURLState
*curl_init_state(BDRVCURLState
*s
)
243 CURLState
*state
= NULL
;
247 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
248 for (j
=0; j
<CURL_NUM_ACB
; j
++)
249 if (s
->states
[i
].acb
[j
])
251 if (s
->states
[i
].in_use
)
254 state
= &s
->states
[i
];
267 state
->curl
= curl_easy_init();
270 curl_easy_setopt(state
->curl
, CURLOPT_URL
, s
->url
);
271 curl_easy_setopt(state
->curl
, CURLOPT_TIMEOUT
, 5);
272 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_read_cb
);
273 curl_easy_setopt(state
->curl
, CURLOPT_WRITEDATA
, (void *)state
);
274 curl_easy_setopt(state
->curl
, CURLOPT_PRIVATE
, (void *)state
);
275 curl_easy_setopt(state
->curl
, CURLOPT_AUTOREFERER
, 1);
276 curl_easy_setopt(state
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
277 curl_easy_setopt(state
->curl
, CURLOPT_NOSIGNAL
, 1);
278 curl_easy_setopt(state
->curl
, CURLOPT_ERRORBUFFER
, state
->errmsg
);
281 curl_easy_setopt(state
->curl
, CURLOPT_VERBOSE
, 1);
291 static void curl_clean_state(CURLState
*s
)
294 curl_multi_remove_handle(s
->s
->multi
, s
->curl
);
298 static int curl_open(BlockDriverState
*bs
, const char *filename
, int flags
)
300 BDRVCURLState
*s
= bs
->opaque
;
301 CURLState
*state
= NULL
;
304 #define RA_OPTSTR ":readahead="
310 static int inited
= 0;
312 file
= strdup(filename
);
313 s
->readahead_size
= READ_AHEAD_SIZE
;
315 /* Parse a trailing ":readahead=#:" param, if present. */
316 ra
= file
+ strlen(file
) - 1;
318 if (parse_state
== 0) {
323 } else if (parse_state
== 1) {
324 if (*ra
> '9' || *ra
< '0') {
325 char *opt_start
= ra
- strlen(RA_OPTSTR
) + 1;
326 if (opt_start
> file
&&
327 strncmp(opt_start
, RA_OPTSTR
, strlen(RA_OPTSTR
)) == 0) {
329 ra
-= strlen(RA_OPTSTR
) - 1;
331 s
->readahead_size
= atoi(ra_val
);
341 if ((s
->readahead_size
& 0x1ff) != 0) {
342 fprintf(stderr
, "HTTP_READAHEAD_SIZE %Zd is not a multiple of 512\n",
348 curl_global_init(CURL_GLOBAL_ALL
);
352 dprintf("CURL: Opening %s\n", file
);
354 state
= curl_init_state(s
);
360 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 1);
361 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_size_cb
);
362 if (curl_easy_perform(state
->curl
))
364 curl_easy_getinfo(state
->curl
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
);
365 curl_easy_setopt(state
->curl
, CURLOPT_WRITEFUNCTION
, (void *)curl_read_cb
);
366 curl_easy_setopt(state
->curl
, CURLOPT_NOBODY
, 0);
371 dprintf("CURL: Size = %lld\n", (long long)s
->len
);
373 curl_clean_state(state
);
374 curl_easy_cleanup(state
->curl
);
377 // Now we know the file exists and its size, so let's
378 // initialize the multi interface!
380 s
->multi
= curl_multi_init();
381 curl_multi_setopt( s
->multi
, CURLMOPT_SOCKETDATA
, s
);
382 curl_multi_setopt( s
->multi
, CURLMOPT_SOCKETFUNCTION
, curl_sock_cb
);
388 fprintf(stderr
, "CURL: Error opening file: %s\n", state
->errmsg
);
389 curl_easy_cleanup(state
->curl
);
396 static void curl_aio_cancel(BlockDriverAIOCB
*blockacb
)
398 // Do we have to implement canceling? Seems to work without...
401 static AIOPool curl_aio_pool
= {
402 .aiocb_size
= sizeof(CURLAIOCB
),
403 .cancel
= curl_aio_cancel
,
406 static BlockDriverAIOCB
*curl_aio_readv(BlockDriverState
*bs
,
407 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
408 BlockDriverCompletionFunc
*cb
, void *opaque
)
410 BDRVCURLState
*s
= bs
->opaque
;
412 size_t start
= sector_num
* SECTOR_SIZE
;
416 acb
= qemu_aio_get(&curl_aio_pool
, bs
, cb
, opaque
);
422 // In case we have the requested data already (e.g. read-ahead),
423 // we can just call the callback and be done.
425 switch (curl_find_buf(s
, start
, nb_sectors
* SECTOR_SIZE
, acb
)) {
427 qemu_aio_release(acb
);
435 // No cache found, so let's start a new request
437 state
= curl_init_state(s
);
442 acb
->end
= (nb_sectors
* SECTOR_SIZE
);
446 qemu_free(state
->orig_buf
);
447 state
->buf_start
= start
;
448 state
->buf_len
= acb
->end
+ s
->readahead_size
;
449 end
= MIN(start
+ state
->buf_len
, s
->len
) - 1;
450 state
->orig_buf
= qemu_malloc(state
->buf_len
);
453 snprintf(state
->range
, 127, "%lld-%lld", (long long)start
, (long long)end
);
454 dprintf("CURL (AIO): Reading %d at %lld (%s)\n", (nb_sectors
* SECTOR_SIZE
), start
, state
->range
);
455 curl_easy_setopt(state
->curl
, CURLOPT_RANGE
, state
->range
);
457 curl_multi_add_handle(s
->multi
, state
->curl
);
463 static void curl_close(BlockDriverState
*bs
)
465 BDRVCURLState
*s
= bs
->opaque
;
468 dprintf("CURL: Close\n");
469 for (i
=0; i
<CURL_NUM_STATES
; i
++) {
470 if (s
->states
[i
].in_use
)
471 curl_clean_state(&s
->states
[i
]);
472 if (s
->states
[i
].curl
) {
473 curl_easy_cleanup(s
->states
[i
].curl
);
474 s
->states
[i
].curl
= NULL
;
476 if (s
->states
[i
].orig_buf
) {
477 qemu_free(s
->states
[i
].orig_buf
);
478 s
->states
[i
].orig_buf
= NULL
;
482 curl_multi_cleanup(s
->multi
);
487 static int64_t curl_getlength(BlockDriverState
*bs
)
489 BDRVCURLState
*s
= bs
->opaque
;
493 static BlockDriver bdrv_http
= {
494 .format_name
= "http",
495 .protocol_name
= "http",
497 .instance_size
= sizeof(BDRVCURLState
),
498 .bdrv_open
= curl_open
,
499 .bdrv_close
= curl_close
,
500 .bdrv_getlength
= curl_getlength
,
502 .bdrv_aio_readv
= curl_aio_readv
,
505 static BlockDriver bdrv_https
= {
506 .format_name
= "https",
507 .protocol_name
= "https",
509 .instance_size
= sizeof(BDRVCURLState
),
510 .bdrv_open
= curl_open
,
511 .bdrv_close
= curl_close
,
512 .bdrv_getlength
= curl_getlength
,
514 .bdrv_aio_readv
= curl_aio_readv
,
517 static BlockDriver bdrv_ftp
= {
518 .format_name
= "ftp",
519 .protocol_name
= "ftp",
521 .instance_size
= sizeof(BDRVCURLState
),
522 .bdrv_open
= curl_open
,
523 .bdrv_close
= curl_close
,
524 .bdrv_getlength
= curl_getlength
,
526 .bdrv_aio_readv
= curl_aio_readv
,
529 static BlockDriver bdrv_ftps
= {
530 .format_name
= "ftps",
531 .protocol_name
= "ftps",
533 .instance_size
= sizeof(BDRVCURLState
),
534 .bdrv_open
= curl_open
,
535 .bdrv_close
= curl_close
,
536 .bdrv_getlength
= curl_getlength
,
538 .bdrv_aio_readv
= curl_aio_readv
,
541 static BlockDriver bdrv_tftp
= {
542 .format_name
= "tftp",
543 .protocol_name
= "tftp",
545 .instance_size
= sizeof(BDRVCURLState
),
546 .bdrv_open
= curl_open
,
547 .bdrv_close
= curl_close
,
548 .bdrv_getlength
= curl_getlength
,
550 .bdrv_aio_readv
= curl_aio_readv
,
553 static void curl_block_init(void)
555 bdrv_register(&bdrv_http
);
556 bdrv_register(&bdrv_https
);
557 bdrv_register(&bdrv_ftp
);
558 bdrv_register(&bdrv_ftps
);
559 bdrv_register(&bdrv_tftp
);
562 block_init(curl_block_init
);