Add qemu 2.4.0
[kvmfornfv.git] / qemu / block / curl.c
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 "qemu/error-report.h"
26 #include "block/block_int.h"
27 #include "qapi/qmp/qbool.h"
28 #include "qapi/qmp/qstring.h"
29 #include <curl/curl.h>
30
31 // #define DEBUG_CURL
32 // #define DEBUG_VERBOSE
33
34 #ifdef DEBUG_CURL
35 #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
36 #else
37 #define DPRINTF(fmt, ...) do { } while (0)
38 #endif
39
40 #if LIBCURL_VERSION_NUM >= 0x071000
41 /* The multi interface timer callback was introduced in 7.16.0 */
42 #define NEED_CURL_TIMER_CALLBACK
43 #define HAVE_SOCKET_ACTION
44 #endif
45
46 #ifndef HAVE_SOCKET_ACTION
47 /* If curl_multi_socket_action isn't available, define it statically here in
48  * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
49  * less efficient but still safe. */
50 static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
51                                             curl_socket_t sockfd,
52                                             int ev_bitmask,
53                                             int *running_handles)
54 {
55     return curl_multi_socket(multi_handle, sockfd, running_handles);
56 }
57 #define curl_multi_socket_action __curl_multi_socket_action
58 #endif
59
60 #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
61                    CURLPROTO_FTP | CURLPROTO_FTPS | \
62                    CURLPROTO_TFTP)
63
64 #define CURL_NUM_STATES 8
65 #define CURL_NUM_ACB    8
66 #define SECTOR_SIZE     512
67 #define READ_AHEAD_DEFAULT (256 * 1024)
68 #define CURL_TIMEOUT_DEFAULT 5
69 #define CURL_TIMEOUT_MAX 10000
70
71 #define FIND_RET_NONE   0
72 #define FIND_RET_OK     1
73 #define FIND_RET_WAIT   2
74
75 #define CURL_BLOCK_OPT_URL       "url"
76 #define CURL_BLOCK_OPT_READAHEAD "readahead"
77 #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
78 #define CURL_BLOCK_OPT_TIMEOUT "timeout"
79 #define CURL_BLOCK_OPT_COOKIE    "cookie"
80
81 struct BDRVCURLState;
82
83 typedef struct CURLAIOCB {
84     BlockAIOCB common;
85     QEMUBH *bh;
86     QEMUIOVector *qiov;
87
88     int64_t sector_num;
89     int nb_sectors;
90
91     size_t start;
92     size_t end;
93 } CURLAIOCB;
94
95 typedef struct CURLState
96 {
97     struct BDRVCURLState *s;
98     CURLAIOCB *acb[CURL_NUM_ACB];
99     CURL *curl;
100     curl_socket_t sock_fd;
101     char *orig_buf;
102     size_t buf_start;
103     size_t buf_off;
104     size_t buf_len;
105     char range[128];
106     char errmsg[CURL_ERROR_SIZE];
107     char in_use;
108 } CURLState;
109
110 typedef struct BDRVCURLState {
111     CURLM *multi;
112     QEMUTimer timer;
113     size_t len;
114     CURLState states[CURL_NUM_STATES];
115     char *url;
116     size_t readahead_size;
117     bool sslverify;
118     uint64_t timeout;
119     char *cookie;
120     bool accept_range;
121     AioContext *aio_context;
122 } BDRVCURLState;
123
124 static void curl_clean_state(CURLState *s);
125 static void curl_multi_do(void *arg);
126 static void curl_multi_read(void *arg);
127
128 #ifdef NEED_CURL_TIMER_CALLBACK
129 static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
130 {
131     BDRVCURLState *s = opaque;
132
133     DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
134     if (timeout_ms == -1) {
135         timer_del(&s->timer);
136     } else {
137         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
138         timer_mod(&s->timer,
139                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
140     }
141     return 0;
142 }
143 #endif
144
145 static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
146                         void *userp, void *sp)
147 {
148     BDRVCURLState *s;
149     CURLState *state = NULL;
150     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
151     state->sock_fd = fd;
152     s = state->s;
153
154     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
155     switch (action) {
156         case CURL_POLL_IN:
157             aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
158                                NULL, state);
159             break;
160         case CURL_POLL_OUT:
161             aio_set_fd_handler(s->aio_context, fd, NULL, curl_multi_do, state);
162             break;
163         case CURL_POLL_INOUT:
164             aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
165                                curl_multi_do, state);
166             break;
167         case CURL_POLL_REMOVE:
168             aio_set_fd_handler(s->aio_context, fd, NULL, NULL, NULL);
169             break;
170     }
171
172     return 0;
173 }
174
175 static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
176 {
177     BDRVCURLState *s = opaque;
178     size_t realsize = size * nmemb;
179     const char *accept_line = "Accept-Ranges: bytes";
180
181     if (realsize >= strlen(accept_line)
182         && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
183         s->accept_range = true;
184     }
185
186     return realsize;
187 }
188
189 static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
190 {
191     CURLState *s = ((CURLState*)opaque);
192     size_t realsize = size * nmemb;
193     int i;
194
195     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
196
197     if (!s || !s->orig_buf)
198         return 0;
199
200     if (s->buf_off >= s->buf_len) {
201         /* buffer full, read nothing */
202         return 0;
203     }
204     realsize = MIN(realsize, s->buf_len - s->buf_off);
205     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
206     s->buf_off += realsize;
207
208     for(i=0; i<CURL_NUM_ACB; i++) {
209         CURLAIOCB *acb = s->acb[i];
210
211         if (!acb)
212             continue;
213
214         if ((s->buf_off >= acb->end)) {
215             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
216                                 acb->end - acb->start);
217             acb->common.cb(acb->common.opaque, 0);
218             qemu_aio_unref(acb);
219             s->acb[i] = NULL;
220         }
221     }
222
223     return realsize;
224 }
225
226 static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
227                          CURLAIOCB *acb)
228 {
229     int i;
230     size_t end = start + len;
231
232     for (i=0; i<CURL_NUM_STATES; i++) {
233         CURLState *state = &s->states[i];
234         size_t buf_end = (state->buf_start + state->buf_off);
235         size_t buf_fend = (state->buf_start + state->buf_len);
236
237         if (!state->orig_buf)
238             continue;
239         if (!state->buf_off)
240             continue;
241
242         // Does the existing buffer cover our section?
243         if ((start >= state->buf_start) &&
244             (start <= buf_end) &&
245             (end >= state->buf_start) &&
246             (end <= buf_end))
247         {
248             char *buf = state->orig_buf + (start - state->buf_start);
249
250             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
251             acb->common.cb(acb->common.opaque, 0);
252
253             return FIND_RET_OK;
254         }
255
256         // Wait for unfinished chunks
257         if (state->in_use &&
258             (start >= state->buf_start) &&
259             (start <= buf_fend) &&
260             (end >= state->buf_start) &&
261             (end <= buf_fend))
262         {
263             int j;
264
265             acb->start = start - state->buf_start;
266             acb->end = acb->start + len;
267
268             for (j=0; j<CURL_NUM_ACB; j++) {
269                 if (!state->acb[j]) {
270                     state->acb[j] = acb;
271                     return FIND_RET_WAIT;
272                 }
273             }
274         }
275     }
276
277     return FIND_RET_NONE;
278 }
279
280 static void curl_multi_check_completion(BDRVCURLState *s)
281 {
282     int msgs_in_queue;
283
284     /* Try to find done transfers, so we can free the easy
285      * handle again. */
286     for (;;) {
287         CURLMsg *msg;
288         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
289
290         /* Quit when there are no more completions */
291         if (!msg)
292             break;
293
294         if (msg->msg == CURLMSG_DONE) {
295             CURLState *state = NULL;
296             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
297                               (char **)&state);
298
299             /* ACBs for successful messages get completed in curl_read_cb */
300             if (msg->data.result != CURLE_OK) {
301                 int i;
302                 static int errcount = 100;
303
304                 /* Don't lose the original error message from curl, since
305                  * it contains extra data.
306                  */
307                 if (errcount > 0) {
308                     error_report("curl: %s", state->errmsg);
309                     if (--errcount == 0) {
310                         error_report("curl: further errors suppressed");
311                     }
312                 }
313
314                 for (i = 0; i < CURL_NUM_ACB; i++) {
315                     CURLAIOCB *acb = state->acb[i];
316
317                     if (acb == NULL) {
318                         continue;
319                     }
320
321                     acb->common.cb(acb->common.opaque, -EPROTO);
322                     qemu_aio_unref(acb);
323                     state->acb[i] = NULL;
324                 }
325             }
326
327             curl_clean_state(state);
328             break;
329         }
330     }
331 }
332
333 static void curl_multi_do(void *arg)
334 {
335     CURLState *s = (CURLState *)arg;
336     int running;
337     int r;
338
339     if (!s->s->multi) {
340         return;
341     }
342
343     do {
344         r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
345     } while(r == CURLM_CALL_MULTI_PERFORM);
346
347 }
348
349 static void curl_multi_read(void *arg)
350 {
351     CURLState *s = (CURLState *)arg;
352
353     curl_multi_do(arg);
354     curl_multi_check_completion(s->s);
355 }
356
357 static void curl_multi_timeout_do(void *arg)
358 {
359 #ifdef NEED_CURL_TIMER_CALLBACK
360     BDRVCURLState *s = (BDRVCURLState *)arg;
361     int running;
362
363     if (!s->multi) {
364         return;
365     }
366
367     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
368
369     curl_multi_check_completion(s);
370 #else
371     abort();
372 #endif
373 }
374
375 static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
376 {
377     CURLState *state = NULL;
378     int i, j;
379
380     do {
381         for (i=0; i<CURL_NUM_STATES; i++) {
382             for (j=0; j<CURL_NUM_ACB; j++)
383                 if (s->states[i].acb[j])
384                     continue;
385             if (s->states[i].in_use)
386                 continue;
387
388             state = &s->states[i];
389             state->in_use = 1;
390             break;
391         }
392         if (!state) {
393             aio_poll(bdrv_get_aio_context(bs), true);
394         }
395     } while(!state);
396
397     if (!state->curl) {
398         state->curl = curl_easy_init();
399         if (!state->curl) {
400             return NULL;
401         }
402         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
403         curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
404                          (long) s->sslverify);
405         if (s->cookie) {
406             curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
407         }
408         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
409         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
410                          (void *)curl_read_cb);
411         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
412         curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
413         curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
414         curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
415         curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
416         curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
417         curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
418
419         /* Restrict supported protocols to avoid security issues in the more
420          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
421          * CVE-2013-0249.
422          *
423          * Restricting protocols is only supported from 7.19.4 upwards.
424          */
425 #if LIBCURL_VERSION_NUM >= 0x071304
426         curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
427         curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
428 #endif
429
430 #ifdef DEBUG_VERBOSE
431         curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
432 #endif
433     }
434
435     state->s = s;
436
437     return state;
438 }
439
440 static void curl_clean_state(CURLState *s)
441 {
442     if (s->s->multi)
443         curl_multi_remove_handle(s->s->multi, s->curl);
444     s->in_use = 0;
445 }
446
447 static void curl_parse_filename(const char *filename, QDict *options,
448                                 Error **errp)
449 {
450     qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
451 }
452
453 static void curl_detach_aio_context(BlockDriverState *bs)
454 {
455     BDRVCURLState *s = bs->opaque;
456     int i;
457
458     for (i = 0; i < CURL_NUM_STATES; i++) {
459         if (s->states[i].in_use) {
460             curl_clean_state(&s->states[i]);
461         }
462         if (s->states[i].curl) {
463             curl_easy_cleanup(s->states[i].curl);
464             s->states[i].curl = NULL;
465         }
466         g_free(s->states[i].orig_buf);
467         s->states[i].orig_buf = NULL;
468     }
469     if (s->multi) {
470         curl_multi_cleanup(s->multi);
471         s->multi = NULL;
472     }
473
474     timer_del(&s->timer);
475 }
476
477 static void curl_attach_aio_context(BlockDriverState *bs,
478                                     AioContext *new_context)
479 {
480     BDRVCURLState *s = bs->opaque;
481
482     aio_timer_init(new_context, &s->timer,
483                    QEMU_CLOCK_REALTIME, SCALE_NS,
484                    curl_multi_timeout_do, s);
485
486     assert(!s->multi);
487     s->multi = curl_multi_init();
488     s->aio_context = new_context;
489     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
490 #ifdef NEED_CURL_TIMER_CALLBACK
491     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
492     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
493 #endif
494 }
495
496 static QemuOptsList runtime_opts = {
497     .name = "curl",
498     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
499     .desc = {
500         {
501             .name = CURL_BLOCK_OPT_URL,
502             .type = QEMU_OPT_STRING,
503             .help = "URL to open",
504         },
505         {
506             .name = CURL_BLOCK_OPT_READAHEAD,
507             .type = QEMU_OPT_SIZE,
508             .help = "Readahead size",
509         },
510         {
511             .name = CURL_BLOCK_OPT_SSLVERIFY,
512             .type = QEMU_OPT_BOOL,
513             .help = "Verify SSL certificate"
514         },
515         {
516             .name = CURL_BLOCK_OPT_TIMEOUT,
517             .type = QEMU_OPT_NUMBER,
518             .help = "Curl timeout"
519         },
520         {
521             .name = CURL_BLOCK_OPT_COOKIE,
522             .type = QEMU_OPT_STRING,
523             .help = "Pass the cookie or list of cookies with each request"
524         },
525         { /* end of list */ }
526     },
527 };
528
529 static int curl_open(BlockDriverState *bs, QDict *options, int flags,
530                      Error **errp)
531 {
532     BDRVCURLState *s = bs->opaque;
533     CURLState *state = NULL;
534     QemuOpts *opts;
535     Error *local_err = NULL;
536     const char *file;
537     const char *cookie;
538     double d;
539
540     static int inited = 0;
541
542     if (flags & BDRV_O_RDWR) {
543         error_setg(errp, "curl block device does not support writes");
544         return -EROFS;
545     }
546
547     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
548     qemu_opts_absorb_qdict(opts, options, &local_err);
549     if (local_err) {
550         error_propagate(errp, local_err);
551         goto out_noclean;
552     }
553
554     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
555                                           READ_AHEAD_DEFAULT);
556     if ((s->readahead_size & 0x1ff) != 0) {
557         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
558                    s->readahead_size);
559         goto out_noclean;
560     }
561
562     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
563                                      CURL_TIMEOUT_DEFAULT);
564     if (s->timeout > CURL_TIMEOUT_MAX) {
565         error_setg(errp, "timeout parameter is too large or negative");
566         goto out_noclean;
567     }
568
569     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
570
571     cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
572     s->cookie = g_strdup(cookie);
573
574     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
575     if (file == NULL) {
576         error_setg(errp, "curl block driver requires an 'url' option");
577         goto out_noclean;
578     }
579
580     if (!inited) {
581         curl_global_init(CURL_GLOBAL_ALL);
582         inited = 1;
583     }
584
585     DPRINTF("CURL: Opening %s\n", file);
586     s->aio_context = bdrv_get_aio_context(bs);
587     s->url = g_strdup(file);
588     state = curl_init_state(bs, s);
589     if (!state)
590         goto out_noclean;
591
592     // Get file size
593
594     s->accept_range = false;
595     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
596     curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
597                      curl_header_cb);
598     curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
599     if (curl_easy_perform(state->curl))
600         goto out;
601     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
602     if (d)
603         s->len = (size_t)d;
604     else if(!s->len)
605         goto out;
606     if ((!strncasecmp(s->url, "http://", strlen("http://"))
607         || !strncasecmp(s->url, "https://", strlen("https://")))
608         && !s->accept_range) {
609         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
610                 "Server does not support 'range' (byte ranges).");
611         goto out;
612     }
613     DPRINTF("CURL: Size = %zd\n", s->len);
614
615     curl_clean_state(state);
616     curl_easy_cleanup(state->curl);
617     state->curl = NULL;
618
619     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
620
621     qemu_opts_del(opts);
622     return 0;
623
624 out:
625     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
626     curl_easy_cleanup(state->curl);
627     state->curl = NULL;
628 out_noclean:
629     g_free(s->cookie);
630     g_free(s->url);
631     qemu_opts_del(opts);
632     return -EINVAL;
633 }
634
635 static const AIOCBInfo curl_aiocb_info = {
636     .aiocb_size         = sizeof(CURLAIOCB),
637 };
638
639
640 static void curl_readv_bh_cb(void *p)
641 {
642     CURLState *state;
643     int running;
644
645     CURLAIOCB *acb = p;
646     BDRVCURLState *s = acb->common.bs->opaque;
647
648     qemu_bh_delete(acb->bh);
649     acb->bh = NULL;
650
651     size_t start = acb->sector_num * SECTOR_SIZE;
652     size_t end;
653
654     // In case we have the requested data already (e.g. read-ahead),
655     // we can just call the callback and be done.
656     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
657         case FIND_RET_OK:
658             qemu_aio_unref(acb);
659             // fall through
660         case FIND_RET_WAIT:
661             return;
662         default:
663             break;
664     }
665
666     // No cache found, so let's start a new request
667     state = curl_init_state(acb->common.bs, s);
668     if (!state) {
669         acb->common.cb(acb->common.opaque, -EIO);
670         qemu_aio_unref(acb);
671         return;
672     }
673
674     acb->start = 0;
675     acb->end = (acb->nb_sectors * SECTOR_SIZE);
676
677     state->buf_off = 0;
678     g_free(state->orig_buf);
679     state->buf_start = start;
680     state->buf_len = acb->end + s->readahead_size;
681     end = MIN(start + state->buf_len, s->len) - 1;
682     state->orig_buf = g_try_malloc(state->buf_len);
683     if (state->buf_len && state->orig_buf == NULL) {
684         curl_clean_state(state);
685         acb->common.cb(acb->common.opaque, -ENOMEM);
686         qemu_aio_unref(acb);
687         return;
688     }
689     state->acb[0] = acb;
690
691     snprintf(state->range, 127, "%zd-%zd", start, end);
692     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
693             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
694     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
695
696     curl_multi_add_handle(s->multi, state->curl);
697
698     /* Tell curl it needs to kick things off */
699     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
700 }
701
702 static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
703         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
704         BlockCompletionFunc *cb, void *opaque)
705 {
706     CURLAIOCB *acb;
707
708     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
709
710     acb->qiov = qiov;
711     acb->sector_num = sector_num;
712     acb->nb_sectors = nb_sectors;
713
714     acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
715     qemu_bh_schedule(acb->bh);
716     return &acb->common;
717 }
718
719 static void curl_close(BlockDriverState *bs)
720 {
721     BDRVCURLState *s = bs->opaque;
722
723     DPRINTF("CURL: Close\n");
724     curl_detach_aio_context(bs);
725
726     g_free(s->cookie);
727     g_free(s->url);
728 }
729
730 static int64_t curl_getlength(BlockDriverState *bs)
731 {
732     BDRVCURLState *s = bs->opaque;
733     return s->len;
734 }
735
736 static BlockDriver bdrv_http = {
737     .format_name                = "http",
738     .protocol_name              = "http",
739
740     .instance_size              = sizeof(BDRVCURLState),
741     .bdrv_parse_filename        = curl_parse_filename,
742     .bdrv_file_open             = curl_open,
743     .bdrv_close                 = curl_close,
744     .bdrv_getlength             = curl_getlength,
745
746     .bdrv_aio_readv             = curl_aio_readv,
747
748     .bdrv_detach_aio_context    = curl_detach_aio_context,
749     .bdrv_attach_aio_context    = curl_attach_aio_context,
750 };
751
752 static BlockDriver bdrv_https = {
753     .format_name                = "https",
754     .protocol_name              = "https",
755
756     .instance_size              = sizeof(BDRVCURLState),
757     .bdrv_parse_filename        = curl_parse_filename,
758     .bdrv_file_open             = curl_open,
759     .bdrv_close                 = curl_close,
760     .bdrv_getlength             = curl_getlength,
761
762     .bdrv_aio_readv             = curl_aio_readv,
763
764     .bdrv_detach_aio_context    = curl_detach_aio_context,
765     .bdrv_attach_aio_context    = curl_attach_aio_context,
766 };
767
768 static BlockDriver bdrv_ftp = {
769     .format_name                = "ftp",
770     .protocol_name              = "ftp",
771
772     .instance_size              = sizeof(BDRVCURLState),
773     .bdrv_parse_filename        = curl_parse_filename,
774     .bdrv_file_open             = curl_open,
775     .bdrv_close                 = curl_close,
776     .bdrv_getlength             = curl_getlength,
777
778     .bdrv_aio_readv             = curl_aio_readv,
779
780     .bdrv_detach_aio_context    = curl_detach_aio_context,
781     .bdrv_attach_aio_context    = curl_attach_aio_context,
782 };
783
784 static BlockDriver bdrv_ftps = {
785     .format_name                = "ftps",
786     .protocol_name              = "ftps",
787
788     .instance_size              = sizeof(BDRVCURLState),
789     .bdrv_parse_filename        = curl_parse_filename,
790     .bdrv_file_open             = curl_open,
791     .bdrv_close                 = curl_close,
792     .bdrv_getlength             = curl_getlength,
793
794     .bdrv_aio_readv             = curl_aio_readv,
795
796     .bdrv_detach_aio_context    = curl_detach_aio_context,
797     .bdrv_attach_aio_context    = curl_attach_aio_context,
798 };
799
800 static BlockDriver bdrv_tftp = {
801     .format_name                = "tftp",
802     .protocol_name              = "tftp",
803
804     .instance_size              = sizeof(BDRVCURLState),
805     .bdrv_parse_filename        = curl_parse_filename,
806     .bdrv_file_open             = curl_open,
807     .bdrv_close                 = curl_close,
808     .bdrv_getlength             = curl_getlength,
809
810     .bdrv_aio_readv             = curl_aio_readv,
811
812     .bdrv_detach_aio_context    = curl_detach_aio_context,
813     .bdrv_attach_aio_context    = curl_attach_aio_context,
814 };
815
816 static void curl_block_init(void)
817 {
818     bdrv_register(&bdrv_http);
819     bdrv_register(&bdrv_https);
820     bdrv_register(&bdrv_ftp);
821     bdrv_register(&bdrv_ftps);
822     bdrv_register(&bdrv_tftp);
823 }
824
825 block_init(curl_block_init);