xref: /openbmc/qemu/block/curl.c (revision 8e6d58cd)
1769ce76dSAlexander Graf /*
2769ce76dSAlexander Graf  * QEMU Block driver for CURL images
3769ce76dSAlexander Graf  *
4769ce76dSAlexander Graf  * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
5769ce76dSAlexander Graf  *
6769ce76dSAlexander Graf  * Permission is hereby granted, free of charge, to any person obtaining a copy
7769ce76dSAlexander Graf  * of this software and associated documentation files (the "Software"), to deal
8769ce76dSAlexander Graf  * in the Software without restriction, including without limitation the rights
9769ce76dSAlexander Graf  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10769ce76dSAlexander Graf  * copies of the Software, and to permit persons to whom the Software is
11769ce76dSAlexander Graf  * furnished to do so, subject to the following conditions:
12769ce76dSAlexander Graf  *
13769ce76dSAlexander Graf  * The above copyright notice and this permission notice shall be included in
14769ce76dSAlexander Graf  * all copies or substantial portions of the Software.
15769ce76dSAlexander Graf  *
16769ce76dSAlexander Graf  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17769ce76dSAlexander Graf  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18769ce76dSAlexander Graf  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19769ce76dSAlexander Graf  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20769ce76dSAlexander Graf  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21769ce76dSAlexander Graf  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22769ce76dSAlexander Graf  * THE SOFTWARE.
23769ce76dSAlexander Graf  */
24769ce76dSAlexander Graf #include "qemu-common.h"
25737e150eSPaolo Bonzini #include "block/block_int.h"
26769ce76dSAlexander Graf #include <curl/curl.h>
27769ce76dSAlexander Graf 
28769ce76dSAlexander Graf // #define DEBUG
29769ce76dSAlexander Graf // #define DEBUG_VERBOSE
30769ce76dSAlexander Graf 
31769ce76dSAlexander Graf #ifdef DEBUG_CURL
32d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
33769ce76dSAlexander Graf #else
34d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { } while (0)
35769ce76dSAlexander Graf #endif
36769ce76dSAlexander Graf 
37fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
38fb6d1bbdSStefan Hajnoczi                    CURLPROTO_FTP | CURLPROTO_FTPS | \
39fb6d1bbdSStefan Hajnoczi                    CURLPROTO_TFTP)
40fb6d1bbdSStefan Hajnoczi 
41769ce76dSAlexander Graf #define CURL_NUM_STATES 8
42769ce76dSAlexander Graf #define CURL_NUM_ACB    8
43769ce76dSAlexander Graf #define SECTOR_SIZE     512
44769ce76dSAlexander Graf #define READ_AHEAD_SIZE (256 * 1024)
45769ce76dSAlexander Graf 
46769ce76dSAlexander Graf #define FIND_RET_NONE   0
47769ce76dSAlexander Graf #define FIND_RET_OK     1
48769ce76dSAlexander Graf #define FIND_RET_WAIT   2
49769ce76dSAlexander Graf 
50769ce76dSAlexander Graf struct BDRVCURLState;
51769ce76dSAlexander Graf 
52769ce76dSAlexander Graf typedef struct CURLAIOCB {
53769ce76dSAlexander Graf     BlockDriverAIOCB common;
54363c3c85SNick Thomas     QEMUBH *bh;
55769ce76dSAlexander Graf     QEMUIOVector *qiov;
56363c3c85SNick Thomas 
57363c3c85SNick Thomas     int64_t sector_num;
58363c3c85SNick Thomas     int nb_sectors;
59363c3c85SNick Thomas 
60769ce76dSAlexander Graf     size_t start;
61769ce76dSAlexander Graf     size_t end;
62769ce76dSAlexander Graf } CURLAIOCB;
63769ce76dSAlexander Graf 
64769ce76dSAlexander Graf typedef struct CURLState
65769ce76dSAlexander Graf {
66769ce76dSAlexander Graf     struct BDRVCURLState *s;
67769ce76dSAlexander Graf     CURLAIOCB *acb[CURL_NUM_ACB];
68769ce76dSAlexander Graf     CURL *curl;
69769ce76dSAlexander Graf     char *orig_buf;
70769ce76dSAlexander Graf     size_t buf_start;
71769ce76dSAlexander Graf     size_t buf_off;
72769ce76dSAlexander Graf     size_t buf_len;
73769ce76dSAlexander Graf     char range[128];
74769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
75769ce76dSAlexander Graf     char in_use;
76769ce76dSAlexander Graf } CURLState;
77769ce76dSAlexander Graf 
78769ce76dSAlexander Graf typedef struct BDRVCURLState {
79769ce76dSAlexander Graf     CURLM *multi;
80769ce76dSAlexander Graf     size_t len;
81769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
82769ce76dSAlexander Graf     char *url;
83c76f4952SNolan     size_t readahead_size;
84769ce76dSAlexander Graf } BDRVCURLState;
85769ce76dSAlexander Graf 
86769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
87769ce76dSAlexander Graf static void curl_multi_do(void *arg);
88c84dcdc1SNick Thomas static int curl_aio_flush(void *opaque);
89769ce76dSAlexander Graf 
90769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
91769ce76dSAlexander Graf                         void *s, void *sp)
92769ce76dSAlexander Graf {
93d0f2c4c6Smalc     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
94769ce76dSAlexander Graf     switch (action) {
95769ce76dSAlexander Graf         case CURL_POLL_IN:
96bafbd6a1SPaolo Bonzini             qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, curl_aio_flush, s);
97769ce76dSAlexander Graf             break;
98769ce76dSAlexander Graf         case CURL_POLL_OUT:
99bafbd6a1SPaolo Bonzini             qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, curl_aio_flush, s);
100769ce76dSAlexander Graf             break;
101769ce76dSAlexander Graf         case CURL_POLL_INOUT:
102c84dcdc1SNick Thomas             qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do,
103bafbd6a1SPaolo Bonzini                                     curl_aio_flush, s);
104769ce76dSAlexander Graf             break;
105769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
106bafbd6a1SPaolo Bonzini             qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL);
107769ce76dSAlexander Graf             break;
108769ce76dSAlexander Graf     }
109769ce76dSAlexander Graf 
110769ce76dSAlexander Graf     return 0;
111769ce76dSAlexander Graf }
112769ce76dSAlexander Graf 
113769ce76dSAlexander Graf static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
114769ce76dSAlexander Graf {
115769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
116769ce76dSAlexander Graf     size_t realsize = size * nmemb;
1170bfcd599SBlue Swirl     size_t fsize;
118769ce76dSAlexander Graf 
1190bfcd599SBlue Swirl     if(sscanf(ptr, "Content-Length: %zd", &fsize) == 1) {
120769ce76dSAlexander Graf         s->s->len = fsize;
1210bfcd599SBlue Swirl     }
122769ce76dSAlexander Graf 
123769ce76dSAlexander Graf     return realsize;
124769ce76dSAlexander Graf }
125769ce76dSAlexander Graf 
126769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
127769ce76dSAlexander Graf {
128769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
129769ce76dSAlexander Graf     size_t realsize = size * nmemb;
130769ce76dSAlexander Graf     int i;
131769ce76dSAlexander Graf 
1320bfcd599SBlue Swirl     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
133769ce76dSAlexander Graf 
134769ce76dSAlexander Graf     if (!s || !s->orig_buf)
135769ce76dSAlexander Graf         goto read_end;
136769ce76dSAlexander Graf 
137769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
138769ce76dSAlexander Graf     s->buf_off += realsize;
139769ce76dSAlexander Graf 
140769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
141769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
142769ce76dSAlexander Graf 
143769ce76dSAlexander Graf         if (!acb)
144769ce76dSAlexander Graf             continue;
145769ce76dSAlexander Graf 
146769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
14703396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
148769ce76dSAlexander Graf                                 acb->end - acb->start);
149769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
150769ce76dSAlexander Graf             qemu_aio_release(acb);
151769ce76dSAlexander Graf             s->acb[i] = NULL;
152769ce76dSAlexander Graf         }
153769ce76dSAlexander Graf     }
154769ce76dSAlexander Graf 
155769ce76dSAlexander Graf read_end:
156769ce76dSAlexander Graf     return realsize;
157769ce76dSAlexander Graf }
158769ce76dSAlexander Graf 
159769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
160769ce76dSAlexander Graf                          CURLAIOCB *acb)
161769ce76dSAlexander Graf {
162769ce76dSAlexander Graf     int i;
163769ce76dSAlexander Graf     size_t end = start + len;
164769ce76dSAlexander Graf 
165769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
166769ce76dSAlexander Graf         CURLState *state = &s->states[i];
167769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
168769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
169769ce76dSAlexander Graf 
170769ce76dSAlexander Graf         if (!state->orig_buf)
171769ce76dSAlexander Graf             continue;
172769ce76dSAlexander Graf         if (!state->buf_off)
173769ce76dSAlexander Graf             continue;
174769ce76dSAlexander Graf 
175769ce76dSAlexander Graf         // Does the existing buffer cover our section?
176769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
177769ce76dSAlexander Graf             (start <= buf_end) &&
178769ce76dSAlexander Graf             (end >= state->buf_start) &&
179769ce76dSAlexander Graf             (end <= buf_end))
180769ce76dSAlexander Graf         {
181769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
182769ce76dSAlexander Graf 
18303396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
184769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
185769ce76dSAlexander Graf 
186769ce76dSAlexander Graf             return FIND_RET_OK;
187769ce76dSAlexander Graf         }
188769ce76dSAlexander Graf 
189769ce76dSAlexander Graf         // Wait for unfinished chunks
190769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
191769ce76dSAlexander Graf             (start <= buf_fend) &&
192769ce76dSAlexander Graf             (end >= state->buf_start) &&
193769ce76dSAlexander Graf             (end <= buf_fend))
194769ce76dSAlexander Graf         {
195769ce76dSAlexander Graf             int j;
196769ce76dSAlexander Graf 
197769ce76dSAlexander Graf             acb->start = start - state->buf_start;
198769ce76dSAlexander Graf             acb->end = acb->start + len;
199769ce76dSAlexander Graf 
200769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
201769ce76dSAlexander Graf                 if (!state->acb[j]) {
202769ce76dSAlexander Graf                     state->acb[j] = acb;
203769ce76dSAlexander Graf                     return FIND_RET_WAIT;
204769ce76dSAlexander Graf                 }
205769ce76dSAlexander Graf             }
206769ce76dSAlexander Graf         }
207769ce76dSAlexander Graf     }
208769ce76dSAlexander Graf 
209769ce76dSAlexander Graf     return FIND_RET_NONE;
210769ce76dSAlexander Graf }
211769ce76dSAlexander Graf 
212769ce76dSAlexander Graf static void curl_multi_do(void *arg)
213769ce76dSAlexander Graf {
214769ce76dSAlexander Graf     BDRVCURLState *s = (BDRVCURLState *)arg;
215769ce76dSAlexander Graf     int running;
216769ce76dSAlexander Graf     int r;
217769ce76dSAlexander Graf     int msgs_in_queue;
218769ce76dSAlexander Graf 
219769ce76dSAlexander Graf     if (!s->multi)
220769ce76dSAlexander Graf         return;
221769ce76dSAlexander Graf 
222769ce76dSAlexander Graf     do {
223769ce76dSAlexander Graf         r = curl_multi_socket_all(s->multi, &running);
224769ce76dSAlexander Graf     } while(r == CURLM_CALL_MULTI_PERFORM);
225769ce76dSAlexander Graf 
226769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
227769ce76dSAlexander Graf      * handle again. */
228769ce76dSAlexander Graf     do {
229769ce76dSAlexander Graf         CURLMsg *msg;
230769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
231769ce76dSAlexander Graf 
232769ce76dSAlexander Graf         if (!msg)
233769ce76dSAlexander Graf             break;
234769ce76dSAlexander Graf         if (msg->msg == CURLMSG_NONE)
235769ce76dSAlexander Graf             break;
236769ce76dSAlexander Graf 
237769ce76dSAlexander Graf         switch (msg->msg) {
238769ce76dSAlexander Graf             case CURLMSG_DONE:
239769ce76dSAlexander Graf             {
240769ce76dSAlexander Graf                 CURLState *state = NULL;
241769ce76dSAlexander Graf                 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
242f785a5aeSNicholas Thomas 
243f785a5aeSNicholas Thomas                 /* ACBs for successful messages get completed in curl_read_cb */
244f785a5aeSNicholas Thomas                 if (msg->data.result != CURLE_OK) {
245f785a5aeSNicholas Thomas                     int i;
246f785a5aeSNicholas Thomas                     for (i = 0; i < CURL_NUM_ACB; i++) {
247f785a5aeSNicholas Thomas                         CURLAIOCB *acb = state->acb[i];
248f785a5aeSNicholas Thomas 
249f785a5aeSNicholas Thomas                         if (acb == NULL) {
250f785a5aeSNicholas Thomas                             continue;
251f785a5aeSNicholas Thomas                         }
252f785a5aeSNicholas Thomas 
253f785a5aeSNicholas Thomas                         acb->common.cb(acb->common.opaque, -EIO);
254f785a5aeSNicholas Thomas                         qemu_aio_release(acb);
255f785a5aeSNicholas Thomas                         state->acb[i] = NULL;
256f785a5aeSNicholas Thomas                     }
257f785a5aeSNicholas Thomas                 }
258f785a5aeSNicholas Thomas 
259769ce76dSAlexander Graf                 curl_clean_state(state);
260769ce76dSAlexander Graf                 break;
261769ce76dSAlexander Graf             }
262769ce76dSAlexander Graf             default:
263769ce76dSAlexander Graf                 msgs_in_queue = 0;
264769ce76dSAlexander Graf                 break;
265769ce76dSAlexander Graf         }
266769ce76dSAlexander Graf     } while(msgs_in_queue);
267769ce76dSAlexander Graf }
268769ce76dSAlexander Graf 
269769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s)
270769ce76dSAlexander Graf {
271769ce76dSAlexander Graf     CURLState *state = NULL;
272769ce76dSAlexander Graf     int i, j;
273769ce76dSAlexander Graf 
274769ce76dSAlexander Graf     do {
275769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
276769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
277769ce76dSAlexander Graf                 if (s->states[i].acb[j])
278769ce76dSAlexander Graf                     continue;
279769ce76dSAlexander Graf             if (s->states[i].in_use)
280769ce76dSAlexander Graf                 continue;
281769ce76dSAlexander Graf 
282769ce76dSAlexander Graf             state = &s->states[i];
283769ce76dSAlexander Graf             state->in_use = 1;
284769ce76dSAlexander Graf             break;
285769ce76dSAlexander Graf         }
286769ce76dSAlexander Graf         if (!state) {
287fb7c8e8aSStefan Weil             g_usleep(100);
288769ce76dSAlexander Graf             curl_multi_do(s);
289769ce76dSAlexander Graf         }
290769ce76dSAlexander Graf     } while(!state);
291769ce76dSAlexander Graf 
292769ce76dSAlexander Graf     if (state->curl)
293769ce76dSAlexander Graf         goto has_curl;
294769ce76dSAlexander Graf 
295769ce76dSAlexander Graf     state->curl = curl_easy_init();
296769ce76dSAlexander Graf     if (!state->curl)
297769ce76dSAlexander Graf         return NULL;
298769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
299769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
300df3cee1aSBlue Swirl     curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
301769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
302769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
303769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
304769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
305769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
306769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
307f785a5aeSNicholas Thomas     curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
308769ce76dSAlexander Graf 
309fb6d1bbdSStefan Hajnoczi     /* Restrict supported protocols to avoid security issues in the more
310fb6d1bbdSStefan Hajnoczi      * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
311fb6d1bbdSStefan Hajnoczi      * CVE-2013-0249.
3128a8f5840SStefan Hajnoczi      *
3138a8f5840SStefan Hajnoczi      * Restricting protocols is only supported from 7.19.4 upwards.
314fb6d1bbdSStefan Hajnoczi      */
3158a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
316fb6d1bbdSStefan Hajnoczi     curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
317fb6d1bbdSStefan Hajnoczi     curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
3188a8f5840SStefan Hajnoczi #endif
319fb6d1bbdSStefan Hajnoczi 
320769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
321769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
322769ce76dSAlexander Graf #endif
323769ce76dSAlexander Graf 
324769ce76dSAlexander Graf has_curl:
325769ce76dSAlexander Graf 
326769ce76dSAlexander Graf     state->s = s;
327769ce76dSAlexander Graf 
328769ce76dSAlexander Graf     return state;
329769ce76dSAlexander Graf }
330769ce76dSAlexander Graf 
331769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
332769ce76dSAlexander Graf {
333769ce76dSAlexander Graf     if (s->s->multi)
334769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
335769ce76dSAlexander Graf     s->in_use = 0;
336769ce76dSAlexander Graf }
337769ce76dSAlexander Graf 
338*8e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
339*8e6d58cdSKevin Wolf                                 Error **errp)
340769ce76dSAlexander Graf {
341c76f4952SNolan 
342c76f4952SNolan     #define RA_OPTSTR ":readahead="
343c76f4952SNolan     char *file;
344c76f4952SNolan     char *ra;
345c76f4952SNolan     const char *ra_val;
346c76f4952SNolan     int parse_state = 0;
347c76f4952SNolan 
3487267c094SAnthony Liguori     file = g_strdup(filename);
349c76f4952SNolan 
350c76f4952SNolan     /* Parse a trailing ":readahead=#:" param, if present. */
351c76f4952SNolan     ra = file + strlen(file) - 1;
352c76f4952SNolan     while (ra >= file) {
353c76f4952SNolan         if (parse_state == 0) {
354*8e6d58cdSKevin Wolf             if (*ra == ':') {
355c76f4952SNolan                 parse_state++;
356*8e6d58cdSKevin Wolf             } else {
357c76f4952SNolan                 break;
358*8e6d58cdSKevin Wolf             }
359c76f4952SNolan         } else if (parse_state == 1) {
360c76f4952SNolan             if (*ra > '9' || *ra < '0') {
361c76f4952SNolan                 char *opt_start = ra - strlen(RA_OPTSTR) + 1;
362c76f4952SNolan                 if (opt_start > file &&
363c76f4952SNolan                     strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
364c76f4952SNolan                     ra_val = ra + 1;
365c76f4952SNolan                     ra -= strlen(RA_OPTSTR) - 1;
366c76f4952SNolan                     *ra = '\0';
367*8e6d58cdSKevin Wolf                     qdict_put(options, "readahead", qstring_from_str(ra_val));
368c76f4952SNolan                 }
369*8e6d58cdSKevin Wolf                 break;
370c76f4952SNolan             }
371c76f4952SNolan         }
372c76f4952SNolan         ra--;
373c76f4952SNolan     }
374c76f4952SNolan 
375*8e6d58cdSKevin Wolf     qdict_put(options, "url", qstring_from_str(file));
376*8e6d58cdSKevin Wolf 
377*8e6d58cdSKevin Wolf     g_free(file);
378*8e6d58cdSKevin Wolf }
379*8e6d58cdSKevin Wolf 
380*8e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
381*8e6d58cdSKevin Wolf     .name = "curl",
382*8e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
383*8e6d58cdSKevin Wolf     .desc = {
384*8e6d58cdSKevin Wolf         {
385*8e6d58cdSKevin Wolf             .name = "url",
386*8e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
387*8e6d58cdSKevin Wolf             .help = "URL to open",
388*8e6d58cdSKevin Wolf         },
389*8e6d58cdSKevin Wolf         {
390*8e6d58cdSKevin Wolf             .name = "readahead",
391*8e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
392*8e6d58cdSKevin Wolf             .help = "Readahead size",
393*8e6d58cdSKevin Wolf         },
394*8e6d58cdSKevin Wolf         { /* end of list */ }
395*8e6d58cdSKevin Wolf     },
396*8e6d58cdSKevin Wolf };
397*8e6d58cdSKevin Wolf 
398*8e6d58cdSKevin Wolf static int curl_open(BlockDriverState *bs, const char *dummy,
399*8e6d58cdSKevin Wolf                      QDict *options, int flags)
400*8e6d58cdSKevin Wolf {
401*8e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
402*8e6d58cdSKevin Wolf     CURLState *state = NULL;
403*8e6d58cdSKevin Wolf     QemuOpts *opts;
404*8e6d58cdSKevin Wolf     Error *local_err = NULL;
405*8e6d58cdSKevin Wolf     const char *file;
406*8e6d58cdSKevin Wolf     double d;
407*8e6d58cdSKevin Wolf 
408*8e6d58cdSKevin Wolf     static int inited = 0;
409*8e6d58cdSKevin Wolf 
410*8e6d58cdSKevin Wolf     opts = qemu_opts_create_nofail(&runtime_opts);
411*8e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
412*8e6d58cdSKevin Wolf     if (error_is_set(&local_err)) {
413*8e6d58cdSKevin Wolf         qerror_report_err(local_err);
414*8e6d58cdSKevin Wolf         error_free(local_err);
415*8e6d58cdSKevin Wolf         goto out_noclean;
416*8e6d58cdSKevin Wolf     }
417*8e6d58cdSKevin Wolf 
418*8e6d58cdSKevin Wolf     s->readahead_size = qemu_opt_get_size(opts, "readahead", READ_AHEAD_SIZE);
419c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
42048a402e6Smalc         fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
421c76f4952SNolan                 s->readahead_size);
422c76f4952SNolan         goto out_noclean;
423c76f4952SNolan     }
424c76f4952SNolan 
425*8e6d58cdSKevin Wolf     file = qemu_opt_get(opts, "url");
426*8e6d58cdSKevin Wolf     if (file == NULL) {
427*8e6d58cdSKevin Wolf         qerror_report(ERROR_CLASS_GENERIC_ERROR, "curl block driver requires "
428*8e6d58cdSKevin Wolf                       "an 'url' option");
429*8e6d58cdSKevin Wolf         goto out_noclean;
430*8e6d58cdSKevin Wolf     }
431*8e6d58cdSKevin Wolf 
432769ce76dSAlexander Graf     if (!inited) {
433769ce76dSAlexander Graf         curl_global_init(CURL_GLOBAL_ALL);
434769ce76dSAlexander Graf         inited = 1;
435769ce76dSAlexander Graf     }
436769ce76dSAlexander Graf 
437d0f2c4c6Smalc     DPRINTF("CURL: Opening %s\n", file);
438*8e6d58cdSKevin Wolf     s->url = g_strdup(file);
439769ce76dSAlexander Graf     state = curl_init_state(s);
440769ce76dSAlexander Graf     if (!state)
441769ce76dSAlexander Graf         goto out_noclean;
442769ce76dSAlexander Graf 
443769ce76dSAlexander Graf     // Get file size
444769ce76dSAlexander Graf 
445769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
446df3cee1aSBlue Swirl     curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb);
447769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
448769ce76dSAlexander Graf         goto out;
449769ce76dSAlexander Graf     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
450df3cee1aSBlue Swirl     curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
451769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
452769ce76dSAlexander Graf     if (d)
453769ce76dSAlexander Graf         s->len = (size_t)d;
454769ce76dSAlexander Graf     else if(!s->len)
455769ce76dSAlexander Graf         goto out;
4560bfcd599SBlue Swirl     DPRINTF("CURL: Size = %zd\n", s->len);
457769ce76dSAlexander Graf 
458769ce76dSAlexander Graf     curl_clean_state(state);
459769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
460769ce76dSAlexander Graf     state->curl = NULL;
461769ce76dSAlexander Graf 
462769ce76dSAlexander Graf     // Now we know the file exists and its size, so let's
463769ce76dSAlexander Graf     // initialize the multi interface!
464769ce76dSAlexander Graf 
465769ce76dSAlexander Graf     s->multi = curl_multi_init();
466769ce76dSAlexander Graf     curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s);
467769ce76dSAlexander Graf     curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb );
468769ce76dSAlexander Graf     curl_multi_do(s);
469769ce76dSAlexander Graf 
470*8e6d58cdSKevin Wolf     qemu_opts_del(opts);
471769ce76dSAlexander Graf     return 0;
472769ce76dSAlexander Graf 
473769ce76dSAlexander Graf out:
474769ce76dSAlexander Graf     fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
475769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
476769ce76dSAlexander Graf     state->curl = NULL;
477769ce76dSAlexander Graf out_noclean:
478*8e6d58cdSKevin Wolf     g_free(s->url);
479*8e6d58cdSKevin Wolf     qemu_opts_del(opts);
480769ce76dSAlexander Graf     return -EINVAL;
481769ce76dSAlexander Graf }
482769ce76dSAlexander Graf 
483c84dcdc1SNick Thomas static int curl_aio_flush(void *opaque)
484c84dcdc1SNick Thomas {
485c84dcdc1SNick Thomas     BDRVCURLState *s = opaque;
486c84dcdc1SNick Thomas     int i, j;
487c84dcdc1SNick Thomas 
488c84dcdc1SNick Thomas     for (i=0; i < CURL_NUM_STATES; i++) {
489c84dcdc1SNick Thomas         for(j=0; j < CURL_NUM_ACB; j++) {
490c84dcdc1SNick Thomas             if (s->states[i].acb[j]) {
491c84dcdc1SNick Thomas                 return 1;
492c84dcdc1SNick Thomas             }
493c84dcdc1SNick Thomas         }
494c84dcdc1SNick Thomas     }
495c84dcdc1SNick Thomas     return 0;
496c84dcdc1SNick Thomas }
497c84dcdc1SNick Thomas 
498c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
499c16b5a2cSChristoph Hellwig {
500c16b5a2cSChristoph Hellwig     // Do we have to implement canceling? Seems to work without...
501c16b5a2cSChristoph Hellwig }
502c16b5a2cSChristoph Hellwig 
503d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = {
504c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(CURLAIOCB),
505c16b5a2cSChristoph Hellwig     .cancel             = curl_aio_cancel,
506c16b5a2cSChristoph Hellwig };
507c16b5a2cSChristoph Hellwig 
508363c3c85SNick Thomas 
509363c3c85SNick Thomas static void curl_readv_bh_cb(void *p)
510769ce76dSAlexander Graf {
511769ce76dSAlexander Graf     CURLState *state;
512769ce76dSAlexander Graf 
513363c3c85SNick Thomas     CURLAIOCB *acb = p;
514363c3c85SNick Thomas     BDRVCURLState *s = acb->common.bs->opaque;
515769ce76dSAlexander Graf 
516363c3c85SNick Thomas     qemu_bh_delete(acb->bh);
517363c3c85SNick Thomas     acb->bh = NULL;
518363c3c85SNick Thomas 
519363c3c85SNick Thomas     size_t start = acb->sector_num * SECTOR_SIZE;
520363c3c85SNick Thomas     size_t end;
521769ce76dSAlexander Graf 
522769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
523769ce76dSAlexander Graf     // we can just call the callback and be done.
524363c3c85SNick Thomas     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
525769ce76dSAlexander Graf         case FIND_RET_OK:
526769ce76dSAlexander Graf             qemu_aio_release(acb);
527769ce76dSAlexander Graf             // fall through
528769ce76dSAlexander Graf         case FIND_RET_WAIT:
529363c3c85SNick Thomas             return;
530769ce76dSAlexander Graf         default:
531769ce76dSAlexander Graf             break;
532769ce76dSAlexander Graf     }
533769ce76dSAlexander Graf 
534769ce76dSAlexander Graf     // No cache found, so let's start a new request
535769ce76dSAlexander Graf     state = curl_init_state(s);
536363c3c85SNick Thomas     if (!state) {
537363c3c85SNick Thomas         acb->common.cb(acb->common.opaque, -EIO);
538363c3c85SNick Thomas         qemu_aio_release(acb);
539363c3c85SNick Thomas         return;
540363c3c85SNick Thomas     }
541769ce76dSAlexander Graf 
542769ce76dSAlexander Graf     acb->start = 0;
543363c3c85SNick Thomas     acb->end = (acb->nb_sectors * SECTOR_SIZE);
544769ce76dSAlexander Graf 
545769ce76dSAlexander Graf     state->buf_off = 0;
546769ce76dSAlexander Graf     if (state->orig_buf)
5477267c094SAnthony Liguori         g_free(state->orig_buf);
548769ce76dSAlexander Graf     state->buf_start = start;
549c76f4952SNolan     state->buf_len = acb->end + s->readahead_size;
550769ce76dSAlexander Graf     end = MIN(start + state->buf_len, s->len) - 1;
5517267c094SAnthony Liguori     state->orig_buf = g_malloc(state->buf_len);
552769ce76dSAlexander Graf     state->acb[0] = acb;
553769ce76dSAlexander Graf 
5540bfcd599SBlue Swirl     snprintf(state->range, 127, "%zd-%zd", start, end);
5550bfcd599SBlue Swirl     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
556363c3c85SNick Thomas             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
557769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
558769ce76dSAlexander Graf 
559769ce76dSAlexander Graf     curl_multi_add_handle(s->multi, state->curl);
560769ce76dSAlexander Graf     curl_multi_do(s);
561769ce76dSAlexander Graf 
562363c3c85SNick Thomas }
563363c3c85SNick Thomas 
564363c3c85SNick Thomas static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
565363c3c85SNick Thomas         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
566363c3c85SNick Thomas         BlockDriverCompletionFunc *cb, void *opaque)
567363c3c85SNick Thomas {
568363c3c85SNick Thomas     CURLAIOCB *acb;
569363c3c85SNick Thomas 
570d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
571363c3c85SNick Thomas 
572363c3c85SNick Thomas     acb->qiov = qiov;
573363c3c85SNick Thomas     acb->sector_num = sector_num;
574363c3c85SNick Thomas     acb->nb_sectors = nb_sectors;
575363c3c85SNick Thomas 
576363c3c85SNick Thomas     acb->bh = qemu_bh_new(curl_readv_bh_cb, acb);
577363c3c85SNick Thomas 
578363c3c85SNick Thomas     if (!acb->bh) {
579363c3c85SNick Thomas         DPRINTF("CURL: qemu_bh_new failed\n");
580363c3c85SNick Thomas         return NULL;
581363c3c85SNick Thomas     }
582363c3c85SNick Thomas 
583363c3c85SNick Thomas     qemu_bh_schedule(acb->bh);
584769ce76dSAlexander Graf     return &acb->common;
585769ce76dSAlexander Graf }
586769ce76dSAlexander Graf 
587769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
588769ce76dSAlexander Graf {
589769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
590769ce76dSAlexander Graf     int i;
591769ce76dSAlexander Graf 
592d0f2c4c6Smalc     DPRINTF("CURL: Close\n");
593769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
594769ce76dSAlexander Graf         if (s->states[i].in_use)
595769ce76dSAlexander Graf             curl_clean_state(&s->states[i]);
596769ce76dSAlexander Graf         if (s->states[i].curl) {
597769ce76dSAlexander Graf             curl_easy_cleanup(s->states[i].curl);
598769ce76dSAlexander Graf             s->states[i].curl = NULL;
599769ce76dSAlexander Graf         }
600769ce76dSAlexander Graf         if (s->states[i].orig_buf) {
6017267c094SAnthony Liguori             g_free(s->states[i].orig_buf);
602769ce76dSAlexander Graf             s->states[i].orig_buf = NULL;
603769ce76dSAlexander Graf         }
604769ce76dSAlexander Graf     }
605769ce76dSAlexander Graf     if (s->multi)
606769ce76dSAlexander Graf         curl_multi_cleanup(s->multi);
60745724d6dSStefan Weil     g_free(s->url);
608769ce76dSAlexander Graf }
609769ce76dSAlexander Graf 
610769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs)
611769ce76dSAlexander Graf {
612769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
613769ce76dSAlexander Graf     return s->len;
614769ce76dSAlexander Graf }
615769ce76dSAlexander Graf 
616769ce76dSAlexander Graf static BlockDriver bdrv_http = {
617769ce76dSAlexander Graf     .format_name            = "http",
618769ce76dSAlexander Graf     .protocol_name          = "http",
619769ce76dSAlexander Graf 
620769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
621*8e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
62266f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
623769ce76dSAlexander Graf     .bdrv_close             = curl_close,
624769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
625769ce76dSAlexander Graf 
626769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
627769ce76dSAlexander Graf };
628769ce76dSAlexander Graf 
629769ce76dSAlexander Graf static BlockDriver bdrv_https = {
630769ce76dSAlexander Graf     .format_name            = "https",
631769ce76dSAlexander Graf     .protocol_name          = "https",
632769ce76dSAlexander Graf 
633769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
634*8e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
63566f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
636769ce76dSAlexander Graf     .bdrv_close             = curl_close,
637769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
638769ce76dSAlexander Graf 
639769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
640769ce76dSAlexander Graf };
641769ce76dSAlexander Graf 
642769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
643769ce76dSAlexander Graf     .format_name            = "ftp",
644769ce76dSAlexander Graf     .protocol_name          = "ftp",
645769ce76dSAlexander Graf 
646769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
647*8e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
64866f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
649769ce76dSAlexander Graf     .bdrv_close             = curl_close,
650769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
651769ce76dSAlexander Graf 
652769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
653769ce76dSAlexander Graf };
654769ce76dSAlexander Graf 
655769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
656769ce76dSAlexander Graf     .format_name            = "ftps",
657769ce76dSAlexander Graf     .protocol_name          = "ftps",
658769ce76dSAlexander Graf 
659769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
660*8e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
66166f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
662769ce76dSAlexander Graf     .bdrv_close             = curl_close,
663769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
664769ce76dSAlexander Graf 
665769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
666769ce76dSAlexander Graf };
667769ce76dSAlexander Graf 
668769ce76dSAlexander Graf static BlockDriver bdrv_tftp = {
669769ce76dSAlexander Graf     .format_name            = "tftp",
670769ce76dSAlexander Graf     .protocol_name          = "tftp",
671769ce76dSAlexander Graf 
672769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
673*8e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
67466f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
675769ce76dSAlexander Graf     .bdrv_close             = curl_close,
676769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
677769ce76dSAlexander Graf 
678769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
679769ce76dSAlexander Graf };
680769ce76dSAlexander Graf 
681769ce76dSAlexander Graf static void curl_block_init(void)
682769ce76dSAlexander Graf {
683769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
684769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
685769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
686769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
687769ce76dSAlexander Graf     bdrv_register(&bdrv_tftp);
688769ce76dSAlexander Graf }
689769ce76dSAlexander Graf 
690769ce76dSAlexander Graf block_init(curl_block_init);
691