xref: /openbmc/qemu/block/curl.c (revision 84d18f065fb041a1c0d78d20320d740ae0673c8a)
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 
37031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000
38031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */
39031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK
40031fd1beSPeter Maydell #endif
41031fd1beSPeter Maydell 
42fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
43fb6d1bbdSStefan Hajnoczi                    CURLPROTO_FTP | CURLPROTO_FTPS | \
44fb6d1bbdSStefan Hajnoczi                    CURLPROTO_TFTP)
45fb6d1bbdSStefan Hajnoczi 
46769ce76dSAlexander Graf #define CURL_NUM_STATES 8
47769ce76dSAlexander Graf #define CURL_NUM_ACB    8
48769ce76dSAlexander Graf #define SECTOR_SIZE     512
49769ce76dSAlexander Graf #define READ_AHEAD_SIZE (256 * 1024)
50769ce76dSAlexander Graf 
51769ce76dSAlexander Graf #define FIND_RET_NONE   0
52769ce76dSAlexander Graf #define FIND_RET_OK     1
53769ce76dSAlexander Graf #define FIND_RET_WAIT   2
54769ce76dSAlexander Graf 
55769ce76dSAlexander Graf struct BDRVCURLState;
56769ce76dSAlexander Graf 
57769ce76dSAlexander Graf typedef struct CURLAIOCB {
58769ce76dSAlexander Graf     BlockDriverAIOCB common;
59363c3c85SNick Thomas     QEMUBH *bh;
60769ce76dSAlexander Graf     QEMUIOVector *qiov;
61363c3c85SNick Thomas 
62363c3c85SNick Thomas     int64_t sector_num;
63363c3c85SNick Thomas     int nb_sectors;
64363c3c85SNick Thomas 
65769ce76dSAlexander Graf     size_t start;
66769ce76dSAlexander Graf     size_t end;
67769ce76dSAlexander Graf } CURLAIOCB;
68769ce76dSAlexander Graf 
69769ce76dSAlexander Graf typedef struct CURLState
70769ce76dSAlexander Graf {
71769ce76dSAlexander Graf     struct BDRVCURLState *s;
72769ce76dSAlexander Graf     CURLAIOCB *acb[CURL_NUM_ACB];
73769ce76dSAlexander Graf     CURL *curl;
74769ce76dSAlexander Graf     char *orig_buf;
75769ce76dSAlexander Graf     size_t buf_start;
76769ce76dSAlexander Graf     size_t buf_off;
77769ce76dSAlexander Graf     size_t buf_len;
78769ce76dSAlexander Graf     char range[128];
79769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
80769ce76dSAlexander Graf     char in_use;
81769ce76dSAlexander Graf } CURLState;
82769ce76dSAlexander Graf 
83769ce76dSAlexander Graf typedef struct BDRVCURLState {
84769ce76dSAlexander Graf     CURLM *multi;
85031fd1beSPeter Maydell     QEMUTimer timer;
86769ce76dSAlexander Graf     size_t len;
87769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
88769ce76dSAlexander Graf     char *url;
89c76f4952SNolan     size_t readahead_size;
903494d650SFam Zheng     bool accept_range;
91769ce76dSAlexander Graf } BDRVCURLState;
92769ce76dSAlexander Graf 
93769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
94769ce76dSAlexander Graf static void curl_multi_do(void *arg);
95769ce76dSAlexander Graf 
96031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
97031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
98031fd1beSPeter Maydell {
99031fd1beSPeter Maydell     BDRVCURLState *s = opaque;
100031fd1beSPeter Maydell 
101031fd1beSPeter Maydell     DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
102031fd1beSPeter Maydell     if (timeout_ms == -1) {
103031fd1beSPeter Maydell         timer_del(&s->timer);
104031fd1beSPeter Maydell     } else {
105031fd1beSPeter Maydell         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
106031fd1beSPeter Maydell         timer_mod(&s->timer,
107031fd1beSPeter Maydell                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
108031fd1beSPeter Maydell     }
109031fd1beSPeter Maydell     return 0;
110031fd1beSPeter Maydell }
111031fd1beSPeter Maydell #endif
112031fd1beSPeter Maydell 
113769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
114769ce76dSAlexander Graf                         void *s, void *sp)
115769ce76dSAlexander Graf {
116d0f2c4c6Smalc     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
117769ce76dSAlexander Graf     switch (action) {
118769ce76dSAlexander Graf         case CURL_POLL_IN:
119f2e5dca4SStefan Hajnoczi             qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, s);
120769ce76dSAlexander Graf             break;
121769ce76dSAlexander Graf         case CURL_POLL_OUT:
122f2e5dca4SStefan Hajnoczi             qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, s);
123769ce76dSAlexander Graf             break;
124769ce76dSAlexander Graf         case CURL_POLL_INOUT:
125f2e5dca4SStefan Hajnoczi             qemu_aio_set_fd_handler(fd, curl_multi_do, curl_multi_do, s);
126769ce76dSAlexander Graf             break;
127769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
128f2e5dca4SStefan Hajnoczi             qemu_aio_set_fd_handler(fd, NULL, NULL, NULL);
129769ce76dSAlexander Graf             break;
130769ce76dSAlexander Graf     }
131769ce76dSAlexander Graf 
132769ce76dSAlexander Graf     return 0;
133769ce76dSAlexander Graf }
134769ce76dSAlexander Graf 
1353494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
136769ce76dSAlexander Graf {
1373494d650SFam Zheng     BDRVCURLState *s = opaque;
138769ce76dSAlexander Graf     size_t realsize = size * nmemb;
1393494d650SFam Zheng     const char *accept_line = "Accept-Ranges: bytes";
140769ce76dSAlexander Graf 
1413494d650SFam Zheng     if (realsize >= strlen(accept_line)
1423494d650SFam Zheng         && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
1433494d650SFam Zheng         s->accept_range = true;
1440bfcd599SBlue Swirl     }
145769ce76dSAlexander Graf 
146769ce76dSAlexander Graf     return realsize;
147769ce76dSAlexander Graf }
148769ce76dSAlexander Graf 
149769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
150769ce76dSAlexander Graf {
151769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
152769ce76dSAlexander Graf     size_t realsize = size * nmemb;
153769ce76dSAlexander Graf     int i;
154769ce76dSAlexander Graf 
1550bfcd599SBlue Swirl     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
156769ce76dSAlexander Graf 
157769ce76dSAlexander Graf     if (!s || !s->orig_buf)
158769ce76dSAlexander Graf         goto read_end;
159769ce76dSAlexander Graf 
160769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
161769ce76dSAlexander Graf     s->buf_off += realsize;
162769ce76dSAlexander Graf 
163769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
164769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
165769ce76dSAlexander Graf 
166769ce76dSAlexander Graf         if (!acb)
167769ce76dSAlexander Graf             continue;
168769ce76dSAlexander Graf 
169769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
17003396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
171769ce76dSAlexander Graf                                 acb->end - acb->start);
172769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
173769ce76dSAlexander Graf             qemu_aio_release(acb);
174769ce76dSAlexander Graf             s->acb[i] = NULL;
175769ce76dSAlexander Graf         }
176769ce76dSAlexander Graf     }
177769ce76dSAlexander Graf 
178769ce76dSAlexander Graf read_end:
179769ce76dSAlexander Graf     return realsize;
180769ce76dSAlexander Graf }
181769ce76dSAlexander Graf 
182769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
183769ce76dSAlexander Graf                          CURLAIOCB *acb)
184769ce76dSAlexander Graf {
185769ce76dSAlexander Graf     int i;
186769ce76dSAlexander Graf     size_t end = start + len;
187769ce76dSAlexander Graf 
188769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
189769ce76dSAlexander Graf         CURLState *state = &s->states[i];
190769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
191769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
192769ce76dSAlexander Graf 
193769ce76dSAlexander Graf         if (!state->orig_buf)
194769ce76dSAlexander Graf             continue;
195769ce76dSAlexander Graf         if (!state->buf_off)
196769ce76dSAlexander Graf             continue;
197769ce76dSAlexander Graf 
198769ce76dSAlexander Graf         // Does the existing buffer cover our section?
199769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
200769ce76dSAlexander Graf             (start <= buf_end) &&
201769ce76dSAlexander Graf             (end >= state->buf_start) &&
202769ce76dSAlexander Graf             (end <= buf_end))
203769ce76dSAlexander Graf         {
204769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
205769ce76dSAlexander Graf 
20603396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
207769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
208769ce76dSAlexander Graf 
209769ce76dSAlexander Graf             return FIND_RET_OK;
210769ce76dSAlexander Graf         }
211769ce76dSAlexander Graf 
212769ce76dSAlexander Graf         // Wait for unfinished chunks
213769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
214769ce76dSAlexander Graf             (start <= buf_fend) &&
215769ce76dSAlexander Graf             (end >= state->buf_start) &&
216769ce76dSAlexander Graf             (end <= buf_fend))
217769ce76dSAlexander Graf         {
218769ce76dSAlexander Graf             int j;
219769ce76dSAlexander Graf 
220769ce76dSAlexander Graf             acb->start = start - state->buf_start;
221769ce76dSAlexander Graf             acb->end = acb->start + len;
222769ce76dSAlexander Graf 
223769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
224769ce76dSAlexander Graf                 if (!state->acb[j]) {
225769ce76dSAlexander Graf                     state->acb[j] = acb;
226769ce76dSAlexander Graf                     return FIND_RET_WAIT;
227769ce76dSAlexander Graf                 }
228769ce76dSAlexander Graf             }
229769ce76dSAlexander Graf         }
230769ce76dSAlexander Graf     }
231769ce76dSAlexander Graf 
232769ce76dSAlexander Graf     return FIND_RET_NONE;
233769ce76dSAlexander Graf }
234769ce76dSAlexander Graf 
235031fd1beSPeter Maydell static void curl_multi_read(BDRVCURLState *s)
236769ce76dSAlexander Graf {
237769ce76dSAlexander Graf     int msgs_in_queue;
238769ce76dSAlexander Graf 
239769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
240769ce76dSAlexander Graf      * handle again. */
241769ce76dSAlexander Graf     do {
242769ce76dSAlexander Graf         CURLMsg *msg;
243769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
244769ce76dSAlexander Graf 
245769ce76dSAlexander Graf         if (!msg)
246769ce76dSAlexander Graf             break;
247769ce76dSAlexander Graf         if (msg->msg == CURLMSG_NONE)
248769ce76dSAlexander Graf             break;
249769ce76dSAlexander Graf 
250769ce76dSAlexander Graf         switch (msg->msg) {
251769ce76dSAlexander Graf             case CURLMSG_DONE:
252769ce76dSAlexander Graf             {
253769ce76dSAlexander Graf                 CURLState *state = NULL;
254769ce76dSAlexander Graf                 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
255f785a5aeSNicholas Thomas 
256f785a5aeSNicholas Thomas                 /* ACBs for successful messages get completed in curl_read_cb */
257f785a5aeSNicholas Thomas                 if (msg->data.result != CURLE_OK) {
258f785a5aeSNicholas Thomas                     int i;
259f785a5aeSNicholas Thomas                     for (i = 0; i < CURL_NUM_ACB; i++) {
260f785a5aeSNicholas Thomas                         CURLAIOCB *acb = state->acb[i];
261f785a5aeSNicholas Thomas 
262f785a5aeSNicholas Thomas                         if (acb == NULL) {
263f785a5aeSNicholas Thomas                             continue;
264f785a5aeSNicholas Thomas                         }
265f785a5aeSNicholas Thomas 
266f785a5aeSNicholas Thomas                         acb->common.cb(acb->common.opaque, -EIO);
267f785a5aeSNicholas Thomas                         qemu_aio_release(acb);
268f785a5aeSNicholas Thomas                         state->acb[i] = NULL;
269f785a5aeSNicholas Thomas                     }
270f785a5aeSNicholas Thomas                 }
271f785a5aeSNicholas Thomas 
272769ce76dSAlexander Graf                 curl_clean_state(state);
273769ce76dSAlexander Graf                 break;
274769ce76dSAlexander Graf             }
275769ce76dSAlexander Graf             default:
276769ce76dSAlexander Graf                 msgs_in_queue = 0;
277769ce76dSAlexander Graf                 break;
278769ce76dSAlexander Graf         }
279769ce76dSAlexander Graf     } while(msgs_in_queue);
280769ce76dSAlexander Graf }
281769ce76dSAlexander Graf 
282031fd1beSPeter Maydell static void curl_multi_do(void *arg)
283031fd1beSPeter Maydell {
284031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
285031fd1beSPeter Maydell     int running;
286031fd1beSPeter Maydell     int r;
287031fd1beSPeter Maydell 
288031fd1beSPeter Maydell     if (!s->multi) {
289031fd1beSPeter Maydell         return;
290031fd1beSPeter Maydell     }
291031fd1beSPeter Maydell 
292031fd1beSPeter Maydell     do {
293031fd1beSPeter Maydell         r = curl_multi_socket_all(s->multi, &running);
294031fd1beSPeter Maydell     } while(r == CURLM_CALL_MULTI_PERFORM);
295031fd1beSPeter Maydell 
296031fd1beSPeter Maydell     curl_multi_read(s);
297031fd1beSPeter Maydell }
298031fd1beSPeter Maydell 
299031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
300031fd1beSPeter Maydell {
301031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
302031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
303031fd1beSPeter Maydell     int running;
304031fd1beSPeter Maydell 
305031fd1beSPeter Maydell     if (!s->multi) {
306031fd1beSPeter Maydell         return;
307031fd1beSPeter Maydell     }
308031fd1beSPeter Maydell 
309031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
310031fd1beSPeter Maydell 
311031fd1beSPeter Maydell     curl_multi_read(s);
312031fd1beSPeter Maydell #else
313031fd1beSPeter Maydell     abort();
314031fd1beSPeter Maydell #endif
315031fd1beSPeter Maydell }
316031fd1beSPeter Maydell 
317769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s)
318769ce76dSAlexander Graf {
319769ce76dSAlexander Graf     CURLState *state = NULL;
320769ce76dSAlexander Graf     int i, j;
321769ce76dSAlexander Graf 
322769ce76dSAlexander Graf     do {
323769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
324769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
325769ce76dSAlexander Graf                 if (s->states[i].acb[j])
326769ce76dSAlexander Graf                     continue;
327769ce76dSAlexander Graf             if (s->states[i].in_use)
328769ce76dSAlexander Graf                 continue;
329769ce76dSAlexander Graf 
330769ce76dSAlexander Graf             state = &s->states[i];
331769ce76dSAlexander Graf             state->in_use = 1;
332769ce76dSAlexander Graf             break;
333769ce76dSAlexander Graf         }
334769ce76dSAlexander Graf         if (!state) {
335fb7c8e8aSStefan Weil             g_usleep(100);
336769ce76dSAlexander Graf             curl_multi_do(s);
337769ce76dSAlexander Graf         }
338769ce76dSAlexander Graf     } while(!state);
339769ce76dSAlexander Graf 
340769ce76dSAlexander Graf     if (state->curl)
341769ce76dSAlexander Graf         goto has_curl;
342769ce76dSAlexander Graf 
343769ce76dSAlexander Graf     state->curl = curl_easy_init();
344769ce76dSAlexander Graf     if (!state->curl)
345769ce76dSAlexander Graf         return NULL;
346769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
347769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
348df3cee1aSBlue Swirl     curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
349769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
350769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
351769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
352769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
353769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
354769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
355f785a5aeSNicholas Thomas     curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
356769ce76dSAlexander Graf 
357fb6d1bbdSStefan Hajnoczi     /* Restrict supported protocols to avoid security issues in the more
358fb6d1bbdSStefan Hajnoczi      * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
359fb6d1bbdSStefan Hajnoczi      * CVE-2013-0249.
3608a8f5840SStefan Hajnoczi      *
3618a8f5840SStefan Hajnoczi      * Restricting protocols is only supported from 7.19.4 upwards.
362fb6d1bbdSStefan Hajnoczi      */
3638a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
364fb6d1bbdSStefan Hajnoczi     curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
365fb6d1bbdSStefan Hajnoczi     curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
3668a8f5840SStefan Hajnoczi #endif
367fb6d1bbdSStefan Hajnoczi 
368769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
369769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
370769ce76dSAlexander Graf #endif
371769ce76dSAlexander Graf 
372769ce76dSAlexander Graf has_curl:
373769ce76dSAlexander Graf 
374769ce76dSAlexander Graf     state->s = s;
375769ce76dSAlexander Graf 
376769ce76dSAlexander Graf     return state;
377769ce76dSAlexander Graf }
378769ce76dSAlexander Graf 
379769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
380769ce76dSAlexander Graf {
381769ce76dSAlexander Graf     if (s->s->multi)
382769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
383769ce76dSAlexander Graf     s->in_use = 0;
384769ce76dSAlexander Graf }
385769ce76dSAlexander Graf 
3868e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
3878e6d58cdSKevin Wolf                                 Error **errp)
388769ce76dSAlexander Graf {
389c76f4952SNolan 
390c76f4952SNolan     #define RA_OPTSTR ":readahead="
391c76f4952SNolan     char *file;
392c76f4952SNolan     char *ra;
393c76f4952SNolan     const char *ra_val;
394c76f4952SNolan     int parse_state = 0;
395c76f4952SNolan 
3967267c094SAnthony Liguori     file = g_strdup(filename);
397c76f4952SNolan 
398c76f4952SNolan     /* Parse a trailing ":readahead=#:" param, if present. */
399c76f4952SNolan     ra = file + strlen(file) - 1;
400c76f4952SNolan     while (ra >= file) {
401c76f4952SNolan         if (parse_state == 0) {
4028e6d58cdSKevin Wolf             if (*ra == ':') {
403c76f4952SNolan                 parse_state++;
4048e6d58cdSKevin Wolf             } else {
405c76f4952SNolan                 break;
4068e6d58cdSKevin Wolf             }
407c76f4952SNolan         } else if (parse_state == 1) {
408c76f4952SNolan             if (*ra > '9' || *ra < '0') {
409c76f4952SNolan                 char *opt_start = ra - strlen(RA_OPTSTR) + 1;
410c76f4952SNolan                 if (opt_start > file &&
411c76f4952SNolan                     strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
412c76f4952SNolan                     ra_val = ra + 1;
413c76f4952SNolan                     ra -= strlen(RA_OPTSTR) - 1;
414c76f4952SNolan                     *ra = '\0';
4158e6d58cdSKevin Wolf                     qdict_put(options, "readahead", qstring_from_str(ra_val));
416c76f4952SNolan                 }
4178e6d58cdSKevin Wolf                 break;
418c76f4952SNolan             }
419c76f4952SNolan         }
420c76f4952SNolan         ra--;
421c76f4952SNolan     }
422c76f4952SNolan 
4238e6d58cdSKevin Wolf     qdict_put(options, "url", qstring_from_str(file));
4248e6d58cdSKevin Wolf 
4258e6d58cdSKevin Wolf     g_free(file);
4268e6d58cdSKevin Wolf }
4278e6d58cdSKevin Wolf 
4288e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
4298e6d58cdSKevin Wolf     .name = "curl",
4308e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
4318e6d58cdSKevin Wolf     .desc = {
4328e6d58cdSKevin Wolf         {
4338e6d58cdSKevin Wolf             .name = "url",
4348e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
4358e6d58cdSKevin Wolf             .help = "URL to open",
4368e6d58cdSKevin Wolf         },
4378e6d58cdSKevin Wolf         {
4388e6d58cdSKevin Wolf             .name = "readahead",
4398e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
4408e6d58cdSKevin Wolf             .help = "Readahead size",
4418e6d58cdSKevin Wolf         },
4428e6d58cdSKevin Wolf         { /* end of list */ }
4438e6d58cdSKevin Wolf     },
4448e6d58cdSKevin Wolf };
4458e6d58cdSKevin Wolf 
446015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
447015a1036SMax Reitz                      Error **errp)
4488e6d58cdSKevin Wolf {
4498e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
4508e6d58cdSKevin Wolf     CURLState *state = NULL;
4518e6d58cdSKevin Wolf     QemuOpts *opts;
4528e6d58cdSKevin Wolf     Error *local_err = NULL;
4538e6d58cdSKevin Wolf     const char *file;
4548e6d58cdSKevin Wolf     double d;
4558e6d58cdSKevin Wolf 
4568e6d58cdSKevin Wolf     static int inited = 0;
4578e6d58cdSKevin Wolf 
458a7cea2baSRichard W.M. Jones     if (flags & BDRV_O_RDWR) {
459a7cea2baSRichard W.M. Jones         qerror_report(ERROR_CLASS_GENERIC_ERROR,
460a7cea2baSRichard W.M. Jones                       "curl block device does not support writes");
461a7cea2baSRichard W.M. Jones         return -EROFS;
462a7cea2baSRichard W.M. Jones     }
463a7cea2baSRichard W.M. Jones 
46487ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
4658e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
466*84d18f06SMarkus Armbruster     if (local_err) {
4678e6d58cdSKevin Wolf         qerror_report_err(local_err);
4688e6d58cdSKevin Wolf         error_free(local_err);
4698e6d58cdSKevin Wolf         goto out_noclean;
4708e6d58cdSKevin Wolf     }
4718e6d58cdSKevin Wolf 
4728e6d58cdSKevin Wolf     s->readahead_size = qemu_opt_get_size(opts, "readahead", READ_AHEAD_SIZE);
473c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
47448a402e6Smalc         fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
475c76f4952SNolan                 s->readahead_size);
476c76f4952SNolan         goto out_noclean;
477c76f4952SNolan     }
478c76f4952SNolan 
4798e6d58cdSKevin Wolf     file = qemu_opt_get(opts, "url");
4808e6d58cdSKevin Wolf     if (file == NULL) {
4818e6d58cdSKevin Wolf         qerror_report(ERROR_CLASS_GENERIC_ERROR, "curl block driver requires "
4828e6d58cdSKevin Wolf                       "an 'url' option");
4838e6d58cdSKevin Wolf         goto out_noclean;
4848e6d58cdSKevin Wolf     }
4858e6d58cdSKevin Wolf 
486769ce76dSAlexander Graf     if (!inited) {
487769ce76dSAlexander Graf         curl_global_init(CURL_GLOBAL_ALL);
488769ce76dSAlexander Graf         inited = 1;
489769ce76dSAlexander Graf     }
490769ce76dSAlexander Graf 
491d0f2c4c6Smalc     DPRINTF("CURL: Opening %s\n", file);
4928e6d58cdSKevin Wolf     s->url = g_strdup(file);
493769ce76dSAlexander Graf     state = curl_init_state(s);
494769ce76dSAlexander Graf     if (!state)
495769ce76dSAlexander Graf         goto out_noclean;
496769ce76dSAlexander Graf 
497769ce76dSAlexander Graf     // Get file size
498769ce76dSAlexander Graf 
4993494d650SFam Zheng     s->accept_range = false;
500769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
5013494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
5023494d650SFam Zheng                      curl_header_cb);
5033494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
504769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
505769ce76dSAlexander Graf         goto out;
506769ce76dSAlexander Graf     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
507769ce76dSAlexander Graf     if (d)
508769ce76dSAlexander Graf         s->len = (size_t)d;
509769ce76dSAlexander Graf     else if(!s->len)
510769ce76dSAlexander Graf         goto out;
5113494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
5123494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
5133494d650SFam Zheng         && !s->accept_range) {
5143494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
5153494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
5163494d650SFam Zheng         goto out;
5173494d650SFam Zheng     }
5180bfcd599SBlue Swirl     DPRINTF("CURL: Size = %zd\n", s->len);
519769ce76dSAlexander Graf 
520769ce76dSAlexander Graf     curl_clean_state(state);
521769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
522769ce76dSAlexander Graf     state->curl = NULL;
523769ce76dSAlexander Graf 
524031fd1beSPeter Maydell     aio_timer_init(bdrv_get_aio_context(bs), &s->timer,
525031fd1beSPeter Maydell                    QEMU_CLOCK_REALTIME, SCALE_NS,
526031fd1beSPeter Maydell                    curl_multi_timeout_do, s);
527031fd1beSPeter Maydell 
528769ce76dSAlexander Graf     // Now we know the file exists and its size, so let's
529769ce76dSAlexander Graf     // initialize the multi interface!
530769ce76dSAlexander Graf 
531769ce76dSAlexander Graf     s->multi = curl_multi_init();
532769ce76dSAlexander Graf     curl_multi_setopt(s->multi, CURLMOPT_SOCKETDATA, s);
533769ce76dSAlexander Graf     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
534031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
535031fd1beSPeter Maydell     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
536031fd1beSPeter Maydell     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
537031fd1beSPeter Maydell #endif
538769ce76dSAlexander Graf     curl_multi_do(s);
539769ce76dSAlexander Graf 
5408e6d58cdSKevin Wolf     qemu_opts_del(opts);
541769ce76dSAlexander Graf     return 0;
542769ce76dSAlexander Graf 
543769ce76dSAlexander Graf out:
544769ce76dSAlexander Graf     fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
545769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
546769ce76dSAlexander Graf     state->curl = NULL;
547769ce76dSAlexander Graf out_noclean:
5488e6d58cdSKevin Wolf     g_free(s->url);
5498e6d58cdSKevin Wolf     qemu_opts_del(opts);
550769ce76dSAlexander Graf     return -EINVAL;
551769ce76dSAlexander Graf }
552769ce76dSAlexander Graf 
553c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
554c16b5a2cSChristoph Hellwig {
555c16b5a2cSChristoph Hellwig     // Do we have to implement canceling? Seems to work without...
556c16b5a2cSChristoph Hellwig }
557c16b5a2cSChristoph Hellwig 
558d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = {
559c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(CURLAIOCB),
560c16b5a2cSChristoph Hellwig     .cancel             = curl_aio_cancel,
561c16b5a2cSChristoph Hellwig };
562c16b5a2cSChristoph Hellwig 
563363c3c85SNick Thomas 
564363c3c85SNick Thomas static void curl_readv_bh_cb(void *p)
565769ce76dSAlexander Graf {
566769ce76dSAlexander Graf     CURLState *state;
567769ce76dSAlexander Graf 
568363c3c85SNick Thomas     CURLAIOCB *acb = p;
569363c3c85SNick Thomas     BDRVCURLState *s = acb->common.bs->opaque;
570769ce76dSAlexander Graf 
571363c3c85SNick Thomas     qemu_bh_delete(acb->bh);
572363c3c85SNick Thomas     acb->bh = NULL;
573363c3c85SNick Thomas 
574363c3c85SNick Thomas     size_t start = acb->sector_num * SECTOR_SIZE;
575363c3c85SNick Thomas     size_t end;
576769ce76dSAlexander Graf 
577769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
578769ce76dSAlexander Graf     // we can just call the callback and be done.
579363c3c85SNick Thomas     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
580769ce76dSAlexander Graf         case FIND_RET_OK:
581769ce76dSAlexander Graf             qemu_aio_release(acb);
582769ce76dSAlexander Graf             // fall through
583769ce76dSAlexander Graf         case FIND_RET_WAIT:
584363c3c85SNick Thomas             return;
585769ce76dSAlexander Graf         default:
586769ce76dSAlexander Graf             break;
587769ce76dSAlexander Graf     }
588769ce76dSAlexander Graf 
589769ce76dSAlexander Graf     // No cache found, so let's start a new request
590769ce76dSAlexander Graf     state = curl_init_state(s);
591363c3c85SNick Thomas     if (!state) {
592363c3c85SNick Thomas         acb->common.cb(acb->common.opaque, -EIO);
593363c3c85SNick Thomas         qemu_aio_release(acb);
594363c3c85SNick Thomas         return;
595363c3c85SNick Thomas     }
596769ce76dSAlexander Graf 
597769ce76dSAlexander Graf     acb->start = 0;
598363c3c85SNick Thomas     acb->end = (acb->nb_sectors * SECTOR_SIZE);
599769ce76dSAlexander Graf 
600769ce76dSAlexander Graf     state->buf_off = 0;
601769ce76dSAlexander Graf     if (state->orig_buf)
6027267c094SAnthony Liguori         g_free(state->orig_buf);
603769ce76dSAlexander Graf     state->buf_start = start;
604c76f4952SNolan     state->buf_len = acb->end + s->readahead_size;
605769ce76dSAlexander Graf     end = MIN(start + state->buf_len, s->len) - 1;
6067267c094SAnthony Liguori     state->orig_buf = g_malloc(state->buf_len);
607769ce76dSAlexander Graf     state->acb[0] = acb;
608769ce76dSAlexander Graf 
6090bfcd599SBlue Swirl     snprintf(state->range, 127, "%zd-%zd", start, end);
6100bfcd599SBlue Swirl     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
611363c3c85SNick Thomas             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
612769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
613769ce76dSAlexander Graf 
614769ce76dSAlexander Graf     curl_multi_add_handle(s->multi, state->curl);
615769ce76dSAlexander Graf     curl_multi_do(s);
616769ce76dSAlexander Graf 
617363c3c85SNick Thomas }
618363c3c85SNick Thomas 
619363c3c85SNick Thomas static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
620363c3c85SNick Thomas         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
621363c3c85SNick Thomas         BlockDriverCompletionFunc *cb, void *opaque)
622363c3c85SNick Thomas {
623363c3c85SNick Thomas     CURLAIOCB *acb;
624363c3c85SNick Thomas 
625d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
626363c3c85SNick Thomas 
627363c3c85SNick Thomas     acb->qiov = qiov;
628363c3c85SNick Thomas     acb->sector_num = sector_num;
629363c3c85SNick Thomas     acb->nb_sectors = nb_sectors;
630363c3c85SNick Thomas 
631363c3c85SNick Thomas     acb->bh = qemu_bh_new(curl_readv_bh_cb, acb);
632363c3c85SNick Thomas     qemu_bh_schedule(acb->bh);
633769ce76dSAlexander Graf     return &acb->common;
634769ce76dSAlexander Graf }
635769ce76dSAlexander Graf 
636769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
637769ce76dSAlexander Graf {
638769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
639769ce76dSAlexander Graf     int i;
640769ce76dSAlexander Graf 
641d0f2c4c6Smalc     DPRINTF("CURL: Close\n");
642769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
643769ce76dSAlexander Graf         if (s->states[i].in_use)
644769ce76dSAlexander Graf             curl_clean_state(&s->states[i]);
645769ce76dSAlexander Graf         if (s->states[i].curl) {
646769ce76dSAlexander Graf             curl_easy_cleanup(s->states[i].curl);
647769ce76dSAlexander Graf             s->states[i].curl = NULL;
648769ce76dSAlexander Graf         }
649769ce76dSAlexander Graf         if (s->states[i].orig_buf) {
6507267c094SAnthony Liguori             g_free(s->states[i].orig_buf);
651769ce76dSAlexander Graf             s->states[i].orig_buf = NULL;
652769ce76dSAlexander Graf         }
653769ce76dSAlexander Graf     }
654769ce76dSAlexander Graf     if (s->multi)
655769ce76dSAlexander Graf         curl_multi_cleanup(s->multi);
656031fd1beSPeter Maydell 
657031fd1beSPeter Maydell     timer_del(&s->timer);
658031fd1beSPeter Maydell 
65945724d6dSStefan Weil     g_free(s->url);
660769ce76dSAlexander Graf }
661769ce76dSAlexander Graf 
662769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs)
663769ce76dSAlexander Graf {
664769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
665769ce76dSAlexander Graf     return s->len;
666769ce76dSAlexander Graf }
667769ce76dSAlexander Graf 
668769ce76dSAlexander Graf static BlockDriver bdrv_http = {
669769ce76dSAlexander Graf     .format_name            = "http",
670769ce76dSAlexander Graf     .protocol_name          = "http",
671769ce76dSAlexander Graf 
672769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
6738e6d58cdSKevin 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 BlockDriver bdrv_https = {
682769ce76dSAlexander Graf     .format_name            = "https",
683769ce76dSAlexander Graf     .protocol_name          = "https",
684769ce76dSAlexander Graf 
685769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
6868e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
68766f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
688769ce76dSAlexander Graf     .bdrv_close             = curl_close,
689769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
690769ce76dSAlexander Graf 
691769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
692769ce76dSAlexander Graf };
693769ce76dSAlexander Graf 
694769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
695769ce76dSAlexander Graf     .format_name            = "ftp",
696769ce76dSAlexander Graf     .protocol_name          = "ftp",
697769ce76dSAlexander Graf 
698769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
6998e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
70066f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
701769ce76dSAlexander Graf     .bdrv_close             = curl_close,
702769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
703769ce76dSAlexander Graf 
704769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
705769ce76dSAlexander Graf };
706769ce76dSAlexander Graf 
707769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
708769ce76dSAlexander Graf     .format_name            = "ftps",
709769ce76dSAlexander Graf     .protocol_name          = "ftps",
710769ce76dSAlexander Graf 
711769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
7128e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
71366f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
714769ce76dSAlexander Graf     .bdrv_close             = curl_close,
715769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
716769ce76dSAlexander Graf 
717769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
718769ce76dSAlexander Graf };
719769ce76dSAlexander Graf 
720769ce76dSAlexander Graf static BlockDriver bdrv_tftp = {
721769ce76dSAlexander Graf     .format_name            = "tftp",
722769ce76dSAlexander Graf     .protocol_name          = "tftp",
723769ce76dSAlexander Graf 
724769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
7258e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
72666f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
727769ce76dSAlexander Graf     .bdrv_close             = curl_close,
728769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
729769ce76dSAlexander Graf 
730769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
731769ce76dSAlexander Graf };
732769ce76dSAlexander Graf 
733769ce76dSAlexander Graf static void curl_block_init(void)
734769ce76dSAlexander Graf {
735769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
736769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
737769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
738769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
739769ce76dSAlexander Graf     bdrv_register(&bdrv_tftp);
740769ce76dSAlexander Graf }
741769ce76dSAlexander Graf 
742769ce76dSAlexander Graf block_init(curl_block_init);
743