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];
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 BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
353 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
354 BlockDriverCompletionFunc *cb, void *opaque)
356 BDRVCURLState *s = bs->opaque;
358 size_t start = sector_num * SECTOR_SIZE;
362 acb = qemu_aio_get(bs, cb, opaque);
368 // In case we have the requested data already (e.g. read-ahead),
369 // we can just call the callback and be done.
371 switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) {
373 qemu_aio_release(acb);
381 // No cache found, so let's start a new request
383 state = curl_init_state(s);
388 acb->end = (nb_sectors * SECTOR_SIZE);
392 qemu_free(state->orig_buf);
393 state->buf_start = start;
394 state->buf_len = acb->end + READ_AHEAD_SIZE;
395 end = MIN(start + state->buf_len, s->len) - 1;
396 state->orig_buf = qemu_malloc(state->buf_len);
399 snprintf(state->range, 127, "%lld-%lld", (long long)start, (long long)end);
400 dprintf("CURL (AIO): Reading %d at %lld (%s)\n", (nb_sectors * SECTOR_SIZE), start, state->range);
401 curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
403 curl_multi_add_handle(s->multi, state->curl);
409 static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
411 // Do we have to implement canceling? Seems to work without...
414 static void curl_close(BlockDriverState *bs)
416 BDRVCURLState *s = bs->opaque;
419 dprintf("CURL: Close\n");
420 for (i=0; i<CURL_NUM_STATES; i++) {
421 if (s->states[i].in_use)
422 curl_clean_state(&s->states[i]);
423 if (s->states[i].curl) {
424 curl_easy_cleanup(s->states[i].curl);
425 s->states[i].curl = NULL;
427 if (s->states[i].orig_buf) {
428 qemu_free(s->states[i].orig_buf);
429 s->states[i].orig_buf = NULL;
433 curl_multi_cleanup(s->multi);
438 static int64_t curl_getlength(BlockDriverState *bs)
440 BDRVCURLState *s = bs->opaque;
444 static BlockDriver bdrv_http = {
445 .format_name = "http",
446 .protocol_name = "http",
448 .instance_size = sizeof(BDRVCURLState),
449 .bdrv_open = curl_open,
450 .bdrv_close = curl_close,
451 .bdrv_getlength = curl_getlength,
453 .aiocb_size = sizeof(CURLAIOCB),
454 .bdrv_aio_readv = curl_aio_readv,
455 .bdrv_aio_cancel = curl_aio_cancel,
458 static BlockDriver bdrv_https = {
459 .format_name = "https",
460 .protocol_name = "https",
462 .instance_size = sizeof(BDRVCURLState),
463 .bdrv_open = curl_open,
464 .bdrv_close = curl_close,
465 .bdrv_getlength = curl_getlength,
467 .aiocb_size = sizeof(CURLAIOCB),
468 .bdrv_aio_readv = curl_aio_readv,
469 .bdrv_aio_cancel = curl_aio_cancel,
472 static BlockDriver bdrv_ftp = {
473 .format_name = "ftp",
474 .protocol_name = "ftp",
476 .instance_size = sizeof(BDRVCURLState),
477 .bdrv_open = curl_open,
478 .bdrv_close = curl_close,
479 .bdrv_getlength = curl_getlength,
481 .aiocb_size = sizeof(CURLAIOCB),
482 .bdrv_aio_readv = curl_aio_readv,
483 .bdrv_aio_cancel = curl_aio_cancel,
486 static BlockDriver bdrv_ftps = {
487 .format_name = "ftps",
488 .protocol_name = "ftps",
490 .instance_size = sizeof(BDRVCURLState),
491 .bdrv_open = curl_open,
492 .bdrv_close = curl_close,
493 .bdrv_getlength = curl_getlength,
495 .aiocb_size = sizeof(CURLAIOCB),
496 .bdrv_aio_readv = curl_aio_readv,
497 .bdrv_aio_cancel = curl_aio_cancel,
500 static BlockDriver bdrv_tftp = {
501 .format_name = "tftp",
502 .protocol_name = "tftp",
504 .instance_size = sizeof(BDRVCURLState),
505 .bdrv_open = curl_open,
506 .bdrv_close = curl_close,
507 .bdrv_getlength = curl_getlength,
509 .aiocb_size = sizeof(CURLAIOCB),
510 .bdrv_aio_readv = curl_aio_readv,
511 .bdrv_aio_cancel = curl_aio_cancel,
514 static void curl_block_init(void)
516 bdrv_register(&bdrv_http);
517 bdrv_register(&bdrv_https);
518 bdrv_register(&bdrv_ftp);
519 bdrv_register(&bdrv_ftps);
520 bdrv_register(&bdrv_tftp);
523 block_init(curl_block_init);