2 * QEMU Block driver for CURL images
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: %zd", &fsize) == 1) {
116 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
118 CURLState *s = ((CURLState*)opaque);
119 size_t realsize = size * nmemb;
122 DPRINTF("CURL: Just reading %zd bytes\n", realsize);
124 if (!s || !s->orig_buf)
127 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
128 s->buf_off += realsize;
130 for(i=0; i<CURL_NUM_ACB; i++) {
131 CURLAIOCB *acb = s->acb[i];
136 if ((s->buf_off >= acb->end)) {
137 qemu_iovec_from_buffer(acb->qiov, s->orig_buf + acb->start,
138 acb->end - acb->start);
139 acb->common.cb(acb->common.opaque, 0);
140 qemu_aio_release(acb);
149 static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
153 size_t end = start + len;
155 for (i=0; i<CURL_NUM_STATES; i++) {
156 CURLState *state = &s->states[i];
157 size_t buf_end = (state->buf_start + state->buf_off);
158 size_t buf_fend = (state->buf_start + state->buf_len);
160 if (!state->orig_buf)
165 // Does the existing buffer cover our section?
166 if ((start >= state->buf_start) &&
167 (start <= buf_end) &&
168 (end >= state->buf_start) &&
171 char *buf = state->orig_buf + (start - state->buf_start);
173 qemu_iovec_from_buffer(acb->qiov, buf, len);
174 acb->common.cb(acb->common.opaque, 0);
179 // Wait for unfinished chunks
180 if ((start >= state->buf_start) &&
181 (start <= buf_fend) &&
182 (end >= state->buf_start) &&
187 acb->start = start - state->buf_start;
188 acb->end = acb->start + len;
190 for (j=0; j<CURL_NUM_ACB; j++) {
191 if (!state->acb[j]) {
193 return FIND_RET_WAIT;
199 return FIND_RET_NONE;
202 static void curl_multi_do(void *arg)
204 BDRVCURLState *s = (BDRVCURLState *)arg;
213 r = curl_multi_socket_all(s->multi, &running);
214 } while(r == CURLM_CALL_MULTI_PERFORM);
216 /* Try to find done transfers, so we can free the easy
220 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
224 if (msg->msg == CURLMSG_NONE)
230 CURLState *state = NULL;
231 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
233 /* ACBs for successful messages get completed in curl_read_cb */
234 if (msg->data.result != CURLE_OK) {
236 for (i = 0; i < CURL_NUM_ACB; i++) {
237 CURLAIOCB *acb = state->acb[i];
243 acb->common.cb(acb->common.opaque, -EIO);
244 qemu_aio_release(acb);
245 state->acb[i] = NULL;
249 curl_clean_state(state);
256 } while(msgs_in_queue);
259 static CURLState *curl_init_state(BDRVCURLState *s)
261 CURLState *state = NULL;
265 for (i=0; i<CURL_NUM_STATES; i++) {
266 for (j=0; j<CURL_NUM_ACB; j++)
267 if (s->states[i].acb[j])
269 if (s->states[i].in_use)
272 state = &s->states[i];
285 state->curl = curl_easy_init();
288 curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
289 curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
290 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
291 curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
292 curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
293 curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
294 curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
295 curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
296 curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
297 curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
300 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
310 static void curl_clean_state(CURLState *s)
313 curl_multi_remove_handle(s->s->multi, s->curl);
317 static int curl_open(BlockDriverState *bs, const char *filename, int flags)
319 BDRVCURLState *s = bs->opaque;
320 CURLState *state = NULL;
323 #define RA_OPTSTR ":readahead="
329 static int inited = 0;
331 file = g_strdup(filename);
332 s->readahead_size = READ_AHEAD_SIZE;
334 /* Parse a trailing ":readahead=#:" param, if present. */
335 ra = file + strlen(file) - 1;
337 if (parse_state == 0) {
342 } else if (parse_state == 1) {
343 if (*ra > '9' || *ra < '0') {
344 char *opt_start = ra - strlen(RA_OPTSTR) + 1;
345 if (opt_start > file &&
346 strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
348 ra -= strlen(RA_OPTSTR) - 1;
350 s->readahead_size = atoi(ra_val);
360 if ((s->readahead_size & 0x1ff) != 0) {
361 fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
367 curl_global_init(CURL_GLOBAL_ALL);
371 DPRINTF("CURL: Opening %s\n", file);
373 state = curl_init_state(s);
379 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
380 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb);
381 if (curl_easy_perform(state->curl))
383 curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
384 curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
385 curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
390 DPRINTF("CURL: Size = %zd\n", s->len);
392 curl_clean_state(state);
393 curl_easy_cleanup(state->curl);
396 // Now we know the file exists and its size, so let's
397 // initialize the multi interface!
399 s->multi = curl_multi_init();
400 curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s);
401 curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb );
407 fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
408 curl_easy_cleanup(state->curl);
415 static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
417 // Do we have to implement canceling? Seems to work without...
420 static AIOPool curl_aio_pool = {
421 .aiocb_size = sizeof(CURLAIOCB),
422 .cancel = curl_aio_cancel,
425 static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
426 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
427 BlockDriverCompletionFunc *cb, void *opaque)
429 BDRVCURLState *s = bs->opaque;
431 size_t start = sector_num * SECTOR_SIZE;
435 acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque);
441 // In case we have the requested data already (e.g. read-ahead),
442 // we can just call the callback and be done.
444 switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) {
446 qemu_aio_release(acb);
454 // No cache found, so let's start a new request
456 state = curl_init_state(s);
461 acb->end = (nb_sectors * SECTOR_SIZE);
465 g_free(state->orig_buf);
466 state->buf_start = start;
467 state->buf_len = acb->end + s->readahead_size;
468 end = MIN(start + state->buf_len, s->len) - 1;
469 state->orig_buf = g_malloc(state->buf_len);
472 snprintf(state->range, 127, "%zd-%zd", start, end);
473 DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
474 (nb_sectors * SECTOR_SIZE), start, state->range);
475 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
477 curl_multi_add_handle(s->multi, state->curl);
483 static void curl_close(BlockDriverState *bs)
485 BDRVCURLState *s = bs->opaque;
488 DPRINTF("CURL: Close\n");
489 for (i=0; i<CURL_NUM_STATES; i++) {
490 if (s->states[i].in_use)
491 curl_clean_state(&s->states[i]);
492 if (s->states[i].curl) {
493 curl_easy_cleanup(s->states[i].curl);
494 s->states[i].curl = NULL;
496 if (s->states[i].orig_buf) {
497 g_free(s->states[i].orig_buf);
498 s->states[i].orig_buf = NULL;
502 curl_multi_cleanup(s->multi);
507 static int64_t curl_getlength(BlockDriverState *bs)
509 BDRVCURLState *s = bs->opaque;
513 static BlockDriver bdrv_http = {
514 .format_name = "http",
515 .protocol_name = "http",
517 .instance_size = sizeof(BDRVCURLState),
518 .bdrv_file_open = curl_open,
519 .bdrv_close = curl_close,
520 .bdrv_getlength = curl_getlength,
522 .bdrv_aio_readv = curl_aio_readv,
525 static BlockDriver bdrv_https = {
526 .format_name = "https",
527 .protocol_name = "https",
529 .instance_size = sizeof(BDRVCURLState),
530 .bdrv_file_open = curl_open,
531 .bdrv_close = curl_close,
532 .bdrv_getlength = curl_getlength,
534 .bdrv_aio_readv = curl_aio_readv,
537 static BlockDriver bdrv_ftp = {
538 .format_name = "ftp",
539 .protocol_name = "ftp",
541 .instance_size = sizeof(BDRVCURLState),
542 .bdrv_file_open = curl_open,
543 .bdrv_close = curl_close,
544 .bdrv_getlength = curl_getlength,
546 .bdrv_aio_readv = curl_aio_readv,
549 static BlockDriver bdrv_ftps = {
550 .format_name = "ftps",
551 .protocol_name = "ftps",
553 .instance_size = sizeof(BDRVCURLState),
554 .bdrv_file_open = curl_open,
555 .bdrv_close = curl_close,
556 .bdrv_getlength = curl_getlength,
558 .bdrv_aio_readv = curl_aio_readv,
561 static BlockDriver bdrv_tftp = {
562 .format_name = "tftp",
563 .protocol_name = "tftp",
565 .instance_size = sizeof(BDRVCURLState),
566 .bdrv_file_open = curl_open,
567 .bdrv_close = curl_close,
568 .bdrv_getlength = curl_getlength,
570 .bdrv_aio_readv = curl_aio_readv,
573 static void curl_block_init(void)
575 bdrv_register(&bdrv_http);
576 bdrv_register(&bdrv_https);
577 bdrv_register(&bdrv_ftp);
578 bdrv_register(&bdrv_ftps);
579 bdrv_register(&bdrv_tftp);
582 block_init(curl_block_init);