]> git.proxmox.com Git - mirror_qemu.git/blame - block/curl.c
Add HTTP protocol using curl v6
[mirror_qemu.git] / block / curl.c
CommitLineData
769ce76d
AG
1/*
2 * QEMU Block driver for CURL images
3 *
4 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24#include "qemu-common.h"
25#include "block_int.h"
26#include <curl/curl.h>
27
28// #define DEBUG
29// #define DEBUG_VERBOSE
30
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
36
37#define CURL_NUM_STATES 8
38#define CURL_NUM_ACB 8
39#define SECTOR_SIZE 512
40#define READ_AHEAD_SIZE (256 * 1024)
41
42#define FIND_RET_NONE 0
43#define FIND_RET_OK 1
44#define FIND_RET_WAIT 2
45
46struct BDRVCURLState;
47
48typedef struct CURLAIOCB {
49 BlockDriverAIOCB common;
50 QEMUIOVector *qiov;
51 size_t start;
52 size_t end;
53} CURLAIOCB;
54
55typedef struct CURLState
56{
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;
68
69typedef struct BDRVCURLState {
70 CURLM *multi;
71 size_t len;
72 CURLState states[CURL_NUM_STATES];
73 char *url;
74} BDRVCURLState;
75
76static void curl_clean_state(CURLState *s);
77static void curl_multi_do(void *arg);
78
79static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
80 void *s, void *sp)
81{
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;
97 }
98
99 return 0;
100}
101
102static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
103{
104 CURLState *s = ((CURLState*)opaque);
105 size_t realsize = size * nmemb;
106 long long fsize;
107
108 if(sscanf(ptr, "Content-Length: %lld", &fsize) == 1)
109 s->s->len = fsize;
110
111 return realsize;
112}
113
114static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
115{
116 CURLState *s = ((CURLState*)opaque);
117 size_t realsize = size * nmemb;
118 int i;
119
120 dprintf("CURL: Just reading %lld bytes\n", (unsigned long long)realsize);
121
122 if (!s || !s->orig_buf)
123 goto read_end;
124
125 memcpy(s->orig_buf + s->buf_off, ptr, realsize);
126 s->buf_off += realsize;
127
128 for(i=0; i<CURL_NUM_ACB; i++) {
129 CURLAIOCB *acb = s->acb[i];
130
131 if (!acb)
132 continue;
133
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;
140 }
141 }
142
143read_end:
144 return realsize;
145}
146
147static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
148 CURLAIOCB *acb)
149{
150 int i;
151 size_t end = start + len;
152
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);
157
158 if (!state->orig_buf)
159 continue;
160 if (!state->buf_off)
161 continue;
162
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))
168 {
169 char *buf = state->orig_buf + (start - state->buf_start);
170
171 qemu_iovec_from_buffer(acb->qiov, buf, len);
172 acb->common.cb(acb->common.opaque, 0);
173
174 return FIND_RET_OK;
175 }
176
177 // Wait for unfinished chunks
178 if ((start >= state->buf_start) &&
179 (start <= buf_fend) &&
180 (end >= state->buf_start) &&
181 (end <= buf_fend))
182 {
183 int j;
184
185 acb->start = start - state->buf_start;
186 acb->end = acb->start + len;
187
188 for (j=0; j<CURL_NUM_ACB; j++) {
189 if (!state->acb[j]) {
190 state->acb[j] = acb;
191 return FIND_RET_WAIT;
192 }
193 }
194 }
195 }
196
197 return FIND_RET_NONE;
198}
199
200static void curl_multi_do(void *arg)
201{
202 BDRVCURLState *s = (BDRVCURLState *)arg;
203 int running;
204 int r;
205 int msgs_in_queue;
206
207 if (!s->multi)
208 return;
209
210 do {
211 r = curl_multi_socket_all(s->multi, &running);
212 } while(r == CURLM_CALL_MULTI_PERFORM);
213
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);
219
220 if (!msg)
221 break;
222 if (msg->msg == CURLMSG_NONE)
223 break;
224
225 switch (msg->msg) {
226 case CURLMSG_DONE:
227 {
228 CURLState *state = NULL;
229 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
230 curl_clean_state(state);
231 break;
232 }
233 default:
234 msgs_in_queue = 0;
235 break;
236 }
237 } while(msgs_in_queue);
238}
239
240static CURLState *curl_init_state(BDRVCURLState *s)
241{
242 CURLState *state = NULL;
243 int i, j;
244
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;
252
253 state = &s->states[i];
254 state->in_use = 1;
255 break;
256 }
257 if (!state) {
258 usleep(100);
259 curl_multi_do(s);
260 }
261 } while(!state);
262
263 if (state->curl)
264 goto has_curl;
265
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);
278
279#ifdef DEBUG_VERBOSE
280 curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
281#endif
282
283has_curl:
284
285 state->s = s;
286
287 return state;
288}
289
290static void curl_clean_state(CURLState *s)
291{
292 if (s->s->multi)
293 curl_multi_remove_handle(s->s->multi, s->curl);
294 s->in_use = 0;
295}
296
297static int curl_open(BlockDriverState *bs, const char *filename, int flags)
298{
299 BDRVCURLState *s = bs->opaque;
300 CURLState *state = NULL;
301 double d;
302 static int inited = 0;
303
304 if (!inited) {
305 curl_global_init(CURL_GLOBAL_ALL);
306 inited = 1;
307 }
308
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;
314
315 // Get file size
316
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);
329
330 curl_clean_state(state);
331 curl_easy_cleanup(state->curl);
332 state->curl = NULL;
333
334 // Now we know the file exists and its size, so let's
335 // initialize the multi interface!
336
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);
341
342 return 0;
343
344out:
345 fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
346 curl_easy_cleanup(state->curl);
347 state->curl = NULL;
348out_noclean:
349 return -EINVAL;
350}
351
352static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
353 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
354 BlockDriverCompletionFunc *cb, void *opaque)
355{
356 BDRVCURLState *s = bs->opaque;
357 CURLAIOCB *acb;
358 size_t start = sector_num * SECTOR_SIZE;
359 size_t end;
360 CURLState *state;
361
362 acb = qemu_aio_get(bs, cb, opaque);
363 if (!acb)
364 return NULL;
365
366 acb->qiov = qiov;
367
368 // In case we have the requested data already (e.g. read-ahead),
369 // we can just call the callback and be done.
370
371 switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) {
372 case FIND_RET_OK:
373 qemu_aio_release(acb);
374 // fall through
375 case FIND_RET_WAIT:
376 return &acb->common;
377 default:
378 break;
379 }
380
381 // No cache found, so let's start a new request
382
383 state = curl_init_state(s);
384 if (!state)
385 return NULL;
386
387 acb->start = 0;
388 acb->end = (nb_sectors * SECTOR_SIZE);
389
390 state->buf_off = 0;
391 if (state->orig_buf)
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);
397 state->acb[0] = acb;
398
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);
402
403 curl_multi_add_handle(s->multi, state->curl);
404 curl_multi_do(s);
405
406 return &acb->common;
407}
408
409static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
410{
411 // Do we have to implement canceling? Seems to work without...
412}
413
414static void curl_close(BlockDriverState *bs)
415{
416 BDRVCURLState *s = bs->opaque;
417 int i;
418
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;
426 }
427 if (s->states[i].orig_buf) {
428 qemu_free(s->states[i].orig_buf);
429 s->states[i].orig_buf = NULL;
430 }
431 }
432 if (s->multi)
433 curl_multi_cleanup(s->multi);
434 if (s->url)
435 free(s->url);
436}
437
438static int64_t curl_getlength(BlockDriverState *bs)
439{
440 BDRVCURLState *s = bs->opaque;
441 return s->len;
442}
443
444static BlockDriver bdrv_http = {
445 .format_name = "http",
446 .protocol_name = "http",
447
448 .instance_size = sizeof(BDRVCURLState),
449 .bdrv_open = curl_open,
450 .bdrv_close = curl_close,
451 .bdrv_getlength = curl_getlength,
452
453 .aiocb_size = sizeof(CURLAIOCB),
454 .bdrv_aio_readv = curl_aio_readv,
455 .bdrv_aio_cancel = curl_aio_cancel,
456};
457
458static BlockDriver bdrv_https = {
459 .format_name = "https",
460 .protocol_name = "https",
461
462 .instance_size = sizeof(BDRVCURLState),
463 .bdrv_open = curl_open,
464 .bdrv_close = curl_close,
465 .bdrv_getlength = curl_getlength,
466
467 .aiocb_size = sizeof(CURLAIOCB),
468 .bdrv_aio_readv = curl_aio_readv,
469 .bdrv_aio_cancel = curl_aio_cancel,
470};
471
472static BlockDriver bdrv_ftp = {
473 .format_name = "ftp",
474 .protocol_name = "ftp",
475
476 .instance_size = sizeof(BDRVCURLState),
477 .bdrv_open = curl_open,
478 .bdrv_close = curl_close,
479 .bdrv_getlength = curl_getlength,
480
481 .aiocb_size = sizeof(CURLAIOCB),
482 .bdrv_aio_readv = curl_aio_readv,
483 .bdrv_aio_cancel = curl_aio_cancel,
484};
485
486static BlockDriver bdrv_ftps = {
487 .format_name = "ftps",
488 .protocol_name = "ftps",
489
490 .instance_size = sizeof(BDRVCURLState),
491 .bdrv_open = curl_open,
492 .bdrv_close = curl_close,
493 .bdrv_getlength = curl_getlength,
494
495 .aiocb_size = sizeof(CURLAIOCB),
496 .bdrv_aio_readv = curl_aio_readv,
497 .bdrv_aio_cancel = curl_aio_cancel,
498};
499
500static BlockDriver bdrv_tftp = {
501 .format_name = "tftp",
502 .protocol_name = "tftp",
503
504 .instance_size = sizeof(BDRVCURLState),
505 .bdrv_open = curl_open,
506 .bdrv_close = curl_close,
507 .bdrv_getlength = curl_getlength,
508
509 .aiocb_size = sizeof(CURLAIOCB),
510 .bdrv_aio_readv = curl_aio_readv,
511 .bdrv_aio_cancel = curl_aio_cancel,
512};
513
514static void curl_block_init(void)
515{
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);
521}
522
523block_init(curl_block_init);