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];
76 static void curl_clean_state(CURLState *s);
77 static void curl_multi_do(void *arg);
79 static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
82 dprintf("CURL (AIO): Sock action %d on fd %d\n", action, fd);
85 qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, s);
88 qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, s);
91 qemu_aio_set_fd_handler(fd, curl_multi_do,
92 curl_multi_do, NULL, s);
94 case CURL_POLL_REMOVE:
95 qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL);
102 static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
104 CURLState *s = ((CURLState*)opaque);
105 size_t realsize = size * nmemb;
108 if(sscanf(ptr, "Content-Length: %lld", &fsize) == 1)
114 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
116 CURLState *s = ((CURLState*)opaque);
117 size_t realsize = size * nmemb;
120 dprintf("CURL: Just reading %lld bytes\n", (unsigned long long)realsize);
122 if (!s || !s->orig_buf)
125 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
126 s->buf_off += realsize;
128 for(i=0; i<CURL_NUM_ACB; i++) {
129 CURLAIOCB *acb = s->acb[i];
134 if ((s->buf_off >= acb->end)) {
135 qemu_iovec_from_buffer(acb->qiov, s->orig_buf + acb->start,
136 acb->end - acb->start);
137 acb->common.cb(acb->common.opaque, 0);
138 qemu_aio_release(acb);
147 static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
151 size_t end = start + len;
153 for (i=0; i<CURL_NUM_STATES; i++) {
154 CURLState *state = &s->states[i];
155 size_t buf_end = (state->buf_start + state->buf_off);
156 size_t buf_fend = (state->buf_start + state->buf_len);
158 if (!state->orig_buf)
163 // Does the existing buffer cover our section?
164 if ((start >= state->buf_start) &&
165 (start <= buf_end) &&
166 (end >= state->buf_start) &&
169 char *buf = state->orig_buf + (start - state->buf_start);
171 qemu_iovec_from_buffer(acb->qiov, buf, len);
172 acb->common.cb(acb->common.opaque, 0);
177 // Wait for unfinished chunks
178 if ((start >= state->buf_start) &&
179 (start <= buf_fend) &&
180 (end >= state->buf_start) &&
185 acb->start = start - state->buf_start;
186 acb->end = acb->start + len;
188 for (j=0; j<CURL_NUM_ACB; j++) {
189 if (!state->acb[j]) {
191 return FIND_RET_WAIT;
197 return FIND_RET_NONE;
200 static void curl_multi_do(void *arg)
202 BDRVCURLState *s = (BDRVCURLState *)arg;
211 r = curl_multi_socket_all(s->multi, &running);
212 } while(r == CURLM_CALL_MULTI_PERFORM);
214 /* Try to find done transfers, so we can free the easy
218 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
222 if (msg->msg == CURLMSG_NONE)
228 CURLState *state = NULL;
229 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
230 curl_clean_state(state);
237 } while(msgs_in_queue);
240 static CURLState *curl_init_state(BDRVCURLState *s)
242 CURLState *state = NULL;
246 for (i=0; i<CURL_NUM_STATES; i++) {
247 for (j=0; j<CURL_NUM_ACB; j++)
248 if (s->states[i].acb[j])
250 if (s->states[i].in_use)
253 state = &s->states[i];
266 state->curl = curl_easy_init();
269 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
270 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
271 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, curl_read_cb);
272 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
273 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
274 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
275 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
276 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
277 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
280 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
290 static void curl_clean_state(CURLState *s)
293 curl_multi_remove_handle(s->s->multi, s->curl);
297 static int curl_open(BlockDriverState *bs, const char *filename, int flags)
299 BDRVCURLState *s = bs->opaque;
300 CURLState *state = NULL;
302 static int inited = 0;
305 curl_global_init(CURL_GLOBAL_ALL);
309 dprintf("CURL: Opening %s\n", filename);
310 s->url = strdup(filename);
311 state = curl_init_state(s);
317 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
318 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, curl_size_cb);
319 if (curl_easy_perform(state->curl))
321 curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
322 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, curl_read_cb);
323 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
328 dprintf("CURL: Size = %lld\n", (long long)s->len);
330 curl_clean_state(state);
331 curl_easy_cleanup(state->curl);
334 // Now we know the file exists and its size, so let's
335 // initialize the multi interface!
337 s->multi = curl_multi_init();
338 curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s);
339 curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb );
345 fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
346 curl_easy_cleanup(state->curl);
352 static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
354 // Do we have to implement canceling? Seems to work without...
357 static AIOPool curl_aio_pool = {
358 .aiocb_size = sizeof(CURLAIOCB),
359 .cancel = curl_aio_cancel,
362 static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
363 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
364 BlockDriverCompletionFunc *cb, void *opaque)
366 BDRVCURLState *s = bs->opaque;
368 size_t start = sector_num * SECTOR_SIZE;
372 acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque);
378 // In case we have the requested data already (e.g. read-ahead),
379 // we can just call the callback and be done.
381 switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) {
383 qemu_aio_release(acb);
391 // No cache found, so let's start a new request
393 state = curl_init_state(s);
398 acb->end = (nb_sectors * SECTOR_SIZE);
402 qemu_free(state->orig_buf);
403 state->buf_start = start;
404 state->buf_len = acb->end + READ_AHEAD_SIZE;
405 end = MIN(start + state->buf_len, s->len) - 1;
406 state->orig_buf = qemu_malloc(state->buf_len);
409 snprintf(state->range, 127, "%lld-%lld", (long long)start, (long long)end);
410 dprintf("CURL (AIO): Reading %d at %lld (%s)\n", (nb_sectors * SECTOR_SIZE), start, state->range);
411 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
413 curl_multi_add_handle(s->multi, state->curl);
419 static void curl_close(BlockDriverState *bs)
421 BDRVCURLState *s = bs->opaque;
424 dprintf("CURL: Close\n");
425 for (i=0; i<CURL_NUM_STATES; i++) {
426 if (s->states[i].in_use)
427 curl_clean_state(&s->states[i]);
428 if (s->states[i].curl) {
429 curl_easy_cleanup(s->states[i].curl);
430 s->states[i].curl = NULL;
432 if (s->states[i].orig_buf) {
433 qemu_free(s->states[i].orig_buf);
434 s->states[i].orig_buf = NULL;
438 curl_multi_cleanup(s->multi);
443 static int64_t curl_getlength(BlockDriverState *bs)
445 BDRVCURLState *s = bs->opaque;
449 static BlockDriver bdrv_http = {
450 .format_name = "http",
451 .protocol_name = "http",
453 .instance_size = sizeof(BDRVCURLState),
454 .bdrv_open = curl_open,
455 .bdrv_close = curl_close,
456 .bdrv_getlength = curl_getlength,
458 .bdrv_aio_readv = curl_aio_readv,
461 static BlockDriver bdrv_https = {
462 .format_name = "https",
463 .protocol_name = "https",
465 .instance_size = sizeof(BDRVCURLState),
466 .bdrv_open = curl_open,
467 .bdrv_close = curl_close,
468 .bdrv_getlength = curl_getlength,
470 .bdrv_aio_readv = curl_aio_readv,
473 static BlockDriver bdrv_ftp = {
474 .format_name = "ftp",
475 .protocol_name = "ftp",
477 .instance_size = sizeof(BDRVCURLState),
478 .bdrv_open = curl_open,
479 .bdrv_close = curl_close,
480 .bdrv_getlength = curl_getlength,
482 .bdrv_aio_readv = curl_aio_readv,
485 static BlockDriver bdrv_ftps = {
486 .format_name = "ftps",
487 .protocol_name = "ftps",
489 .instance_size = sizeof(BDRVCURLState),
490 .bdrv_open = curl_open,
491 .bdrv_close = curl_close,
492 .bdrv_getlength = curl_getlength,
494 .bdrv_aio_readv = curl_aio_readv,
497 static BlockDriver bdrv_tftp = {
498 .format_name = "tftp",
499 .protocol_name = "tftp",
501 .instance_size = sizeof(BDRVCURLState),
502 .bdrv_open = curl_open,
503 .bdrv_close = curl_close,
504 .bdrv_getlength = curl_getlength,
506 .bdrv_aio_readv = curl_aio_readv,
509 static void curl_block_init(void)
511 bdrv_register(&bdrv_http);
512 bdrv_register(&bdrv_https);
513 bdrv_register(&bdrv_ftp);
514 bdrv_register(&bdrv_ftps);
515 bdrv_register(&bdrv_tftp);
518 block_init(curl_block_init);