qemu/pci: helper routines for pci access
[qemu.git] / block / curl.c
blobe1a553f7a2f76796f4319c21356184e07ccb4fb0
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "block_int.h"
26 #include <curl/curl.h>
28 // #define DEBUG
29 // #define DEBUG_VERBOSE
31 #ifdef DEBUG_CURL
32 #define dprintf(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
33 #else
34 #define dprintf(fmt, ...) do { } while (0)
35 #endif
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
43 #define FIND_RET_OK 1
44 #define FIND_RET_WAIT 2
46 struct BDRVCURLState;
48 typedef struct CURLAIOCB {
49 BlockDriverAIOCB common;
50 QEMUIOVector *qiov;
51 size_t start;
52 size_t end;
53 } CURLAIOCB;
55 typedef struct CURLState
57 struct BDRVCURLState *s;
58 CURLAIOCB *acb[CURL_NUM_ACB];
59 CURL *curl;
60 char *orig_buf;
61 size_t buf_start;
62 size_t buf_off;
63 size_t buf_len;
64 char range[128];
65 char errmsg[CURL_ERROR_SIZE];
66 char in_use;
67 } CURLState;
69 typedef struct BDRVCURLState {
70 CURLM *multi;
71 size_t len;
72 CURLState states[CURL_NUM_STATES];
73 char *url;
74 } BDRVCURLState;
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,
80 void *s, void *sp)
82 dprintf("CURL (AIO): Sock action %d on fd %d\n", action, fd);
83 switch (action) {
84 case CURL_POLL_IN:
85 qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, s);
86 break;
87 case CURL_POLL_OUT:
88 qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, s);
89 break;
90 case CURL_POLL_INOUT:
91 qemu_aio_set_fd_handler(fd, curl_multi_do,
92 curl_multi_do, NULL, s);
93 break;
94 case CURL_POLL_REMOVE:
95 qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL);
96 break;
99 return 0;
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;
106 long long fsize;
108 if(sscanf(ptr, "Content-Length: %lld", &fsize) == 1)
109 s->s->len = fsize;
111 return realsize;
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;
118 int i;
120 dprintf("CURL: Just reading %lld bytes\n", (unsigned long long)realsize);
122 if (!s || !s->orig_buf)
123 goto read_end;
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];
131 if (!acb)
132 continue;
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);
139 s->acb[i] = NULL;
143 read_end:
144 return realsize;
147 static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
148 CURLAIOCB *acb)
150 int i;
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)
159 continue;
160 if (!state->buf_off)
161 continue;
163 // Does the existing buffer cover our section?
164 if ((start >= state->buf_start) &&
165 (start <= buf_end) &&
166 (end >= state->buf_start) &&
167 (end <= buf_end))
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);
174 return FIND_RET_OK;
177 // Wait for unfinished chunks
178 if ((start >= state->buf_start) &&
179 (start <= buf_fend) &&
180 (end >= state->buf_start) &&
181 (end <= buf_fend))
183 int j;
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]) {
190 state->acb[j] = acb;
191 return FIND_RET_WAIT;
197 return FIND_RET_NONE;
200 static void curl_multi_do(void *arg)
202 BDRVCURLState *s = (BDRVCURLState *)arg;
203 int running;
204 int r;
205 int msgs_in_queue;
207 if (!s->multi)
208 return;
210 do {
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
215 * handle again. */
216 do {
217 CURLMsg *msg;
218 msg = curl_multi_info_read(s->multi, &msgs_in_queue);
220 if (!msg)
221 break;
222 if (msg->msg == CURLMSG_NONE)
223 break;
225 switch (msg->msg) {
226 case CURLMSG_DONE:
228 CURLState *state = NULL;
229 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
230 curl_clean_state(state);
231 break;
233 default:
234 msgs_in_queue = 0;
235 break;
237 } while(msgs_in_queue);
240 static CURLState *curl_init_state(BDRVCURLState *s)
242 CURLState *state = NULL;
243 int i, j;
245 do {
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])
249 continue;
250 if (s->states[i].in_use)
251 continue;
253 state = &s->states[i];
254 state->in_use = 1;
255 break;
257 if (!state) {
258 usleep(100);
259 curl_multi_do(s);
261 } while(!state);
263 if (state->curl)
264 goto has_curl;
266 state->curl = curl_easy_init();
267 if (!state->curl)
268 return NULL;
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);
279 #ifdef DEBUG_VERBOSE
280 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
281 #endif
283 has_curl:
285 state->s = s;
287 return state;
290 static void curl_clean_state(CURLState *s)
292 if (s->s->multi)
293 curl_multi_remove_handle(s->s->multi, s->curl);
294 s->in_use = 0;
297 static int curl_open(BlockDriverState *bs, const char *filename, int flags)
299 BDRVCURLState *s = bs->opaque;
300 CURLState *state = NULL;
301 double d;
302 static int inited = 0;
304 if (!inited) {
305 curl_global_init(CURL_GLOBAL_ALL);
306 inited = 1;
309 dprintf("CURL: Opening %s\n", filename);
310 s->url = strdup(filename);
311 state = curl_init_state(s);
312 if (!state)
313 goto out_noclean;
315 // Get file size
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))
320 goto out;
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);
324 if (d)
325 s->len = (size_t)d;
326 else if(!s->len)
327 goto out;
328 dprintf("CURL: Size = %lld\n", (long long)s->len);
330 curl_clean_state(state);
331 curl_easy_cleanup(state->curl);
332 state->curl = NULL;
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 );
340 curl_multi_do(s);
342 return 0;
344 out:
345 fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
346 curl_easy_cleanup(state->curl);
347 state->curl = NULL;
348 out_noclean:
349 return -EINVAL;
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;
367 CURLAIOCB *acb;
368 size_t start = sector_num * SECTOR_SIZE;
369 size_t end;
370 CURLState *state;
372 acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque);
373 if (!acb)
374 return NULL;
376 acb->qiov = qiov;
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)) {
382 case FIND_RET_OK:
383 qemu_aio_release(acb);
384 // fall through
385 case FIND_RET_WAIT:
386 return &acb->common;
387 default:
388 break;
391 // No cache found, so let's start a new request
393 state = curl_init_state(s);
394 if (!state)
395 return NULL;
397 acb->start = 0;
398 acb->end = (nb_sectors * SECTOR_SIZE);
400 state->buf_off = 0;
401 if (state->orig_buf)
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);
407 state->acb[0] = acb;
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);
414 curl_multi_do(s);
416 return &acb->common;
419 static void curl_close(BlockDriverState *bs)
421 BDRVCURLState *s = bs->opaque;
422 int i;
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;
437 if (s->multi)
438 curl_multi_cleanup(s->multi);
439 if (s->url)
440 free(s->url);
443 static int64_t curl_getlength(BlockDriverState *bs)
445 BDRVCURLState *s = bs->opaque;
446 return s->len;
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);