xref: /openbmc/qemu/block/curl.c (revision e4661837)
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)
15838bbc0a5SMatthew Booth         return 0;
159769ce76dSAlexander Graf 
1606d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
1616d4b9e55SFam Zheng         /* buffer full, read nothing */
1626d4b9e55SFam Zheng         return 0;
1636d4b9e55SFam Zheng     }
1646d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
165769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
166769ce76dSAlexander Graf     s->buf_off += realsize;
167769ce76dSAlexander Graf 
168769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
169769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
170769ce76dSAlexander Graf 
171769ce76dSAlexander Graf         if (!acb)
172769ce76dSAlexander Graf             continue;
173769ce76dSAlexander Graf 
174769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
17503396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
176769ce76dSAlexander Graf                                 acb->end - acb->start);
177769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
178769ce76dSAlexander Graf             qemu_aio_release(acb);
179769ce76dSAlexander Graf             s->acb[i] = NULL;
180769ce76dSAlexander Graf         }
181769ce76dSAlexander Graf     }
182769ce76dSAlexander Graf 
183769ce76dSAlexander Graf     return realsize;
184769ce76dSAlexander Graf }
185769ce76dSAlexander Graf 
186769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
187769ce76dSAlexander Graf                          CURLAIOCB *acb)
188769ce76dSAlexander Graf {
189769ce76dSAlexander Graf     int i;
190769ce76dSAlexander Graf     size_t end = start + len;
191769ce76dSAlexander Graf 
192769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
193769ce76dSAlexander Graf         CURLState *state = &s->states[i];
194769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
195769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
196769ce76dSAlexander Graf 
197769ce76dSAlexander Graf         if (!state->orig_buf)
198769ce76dSAlexander Graf             continue;
199769ce76dSAlexander Graf         if (!state->buf_off)
200769ce76dSAlexander Graf             continue;
201769ce76dSAlexander Graf 
202769ce76dSAlexander Graf         // Does the existing buffer cover our section?
203769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
204769ce76dSAlexander Graf             (start <= buf_end) &&
205769ce76dSAlexander Graf             (end >= state->buf_start) &&
206769ce76dSAlexander Graf             (end <= buf_end))
207769ce76dSAlexander Graf         {
208769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
209769ce76dSAlexander Graf 
21003396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
211769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
212769ce76dSAlexander Graf 
213769ce76dSAlexander Graf             return FIND_RET_OK;
214769ce76dSAlexander Graf         }
215769ce76dSAlexander Graf 
216769ce76dSAlexander Graf         // Wait for unfinished chunks
217769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
218769ce76dSAlexander Graf             (start <= buf_fend) &&
219769ce76dSAlexander Graf             (end >= state->buf_start) &&
220769ce76dSAlexander Graf             (end <= buf_fend))
221769ce76dSAlexander Graf         {
222769ce76dSAlexander Graf             int j;
223769ce76dSAlexander Graf 
224769ce76dSAlexander Graf             acb->start = start - state->buf_start;
225769ce76dSAlexander Graf             acb->end = acb->start + len;
226769ce76dSAlexander Graf 
227769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
228769ce76dSAlexander Graf                 if (!state->acb[j]) {
229769ce76dSAlexander Graf                     state->acb[j] = acb;
230769ce76dSAlexander Graf                     return FIND_RET_WAIT;
231769ce76dSAlexander Graf                 }
232769ce76dSAlexander Graf             }
233769ce76dSAlexander Graf         }
234769ce76dSAlexander Graf     }
235769ce76dSAlexander Graf 
236769ce76dSAlexander Graf     return FIND_RET_NONE;
237769ce76dSAlexander Graf }
238769ce76dSAlexander Graf 
239031fd1beSPeter Maydell static void curl_multi_read(BDRVCURLState *s)
240769ce76dSAlexander Graf {
241769ce76dSAlexander Graf     int msgs_in_queue;
242769ce76dSAlexander Graf 
243769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
244769ce76dSAlexander Graf      * handle again. */
245769ce76dSAlexander Graf     do {
246769ce76dSAlexander Graf         CURLMsg *msg;
247769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
248769ce76dSAlexander Graf 
249769ce76dSAlexander Graf         if (!msg)
250769ce76dSAlexander Graf             break;
251769ce76dSAlexander Graf         if (msg->msg == CURLMSG_NONE)
252769ce76dSAlexander Graf             break;
253769ce76dSAlexander Graf 
254769ce76dSAlexander Graf         switch (msg->msg) {
255769ce76dSAlexander Graf             case CURLMSG_DONE:
256769ce76dSAlexander Graf             {
257769ce76dSAlexander Graf                 CURLState *state = NULL;
258f6246509SMatthew Booth                 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
259f6246509SMatthew Booth                                   (char **)&state);
260f785a5aeSNicholas Thomas 
261f785a5aeSNicholas Thomas                 /* ACBs for successful messages get completed in curl_read_cb */
262f785a5aeSNicholas Thomas                 if (msg->data.result != CURLE_OK) {
263f785a5aeSNicholas Thomas                     int i;
264f785a5aeSNicholas Thomas                     for (i = 0; i < CURL_NUM_ACB; i++) {
265f785a5aeSNicholas Thomas                         CURLAIOCB *acb = state->acb[i];
266f785a5aeSNicholas Thomas 
267f785a5aeSNicholas Thomas                         if (acb == NULL) {
268f785a5aeSNicholas Thomas                             continue;
269f785a5aeSNicholas Thomas                         }
270f785a5aeSNicholas Thomas 
271f785a5aeSNicholas Thomas                         acb->common.cb(acb->common.opaque, -EIO);
272f785a5aeSNicholas Thomas                         qemu_aio_release(acb);
273f785a5aeSNicholas Thomas                         state->acb[i] = NULL;
274f785a5aeSNicholas Thomas                     }
275f785a5aeSNicholas Thomas                 }
276f785a5aeSNicholas Thomas 
277769ce76dSAlexander Graf                 curl_clean_state(state);
278769ce76dSAlexander Graf                 break;
279769ce76dSAlexander Graf             }
280769ce76dSAlexander Graf             default:
281769ce76dSAlexander Graf                 msgs_in_queue = 0;
282769ce76dSAlexander Graf                 break;
283769ce76dSAlexander Graf         }
284769ce76dSAlexander Graf     } while(msgs_in_queue);
285769ce76dSAlexander Graf }
286769ce76dSAlexander Graf 
287031fd1beSPeter Maydell static void curl_multi_do(void *arg)
288031fd1beSPeter Maydell {
289031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
290031fd1beSPeter Maydell     int running;
291031fd1beSPeter Maydell     int r;
292031fd1beSPeter Maydell 
293031fd1beSPeter Maydell     if (!s->multi) {
294031fd1beSPeter Maydell         return;
295031fd1beSPeter Maydell     }
296031fd1beSPeter Maydell 
297031fd1beSPeter Maydell     do {
298031fd1beSPeter Maydell         r = curl_multi_socket_all(s->multi, &running);
299031fd1beSPeter Maydell     } while(r == CURLM_CALL_MULTI_PERFORM);
300031fd1beSPeter Maydell 
301031fd1beSPeter Maydell     curl_multi_read(s);
302031fd1beSPeter Maydell }
303031fd1beSPeter Maydell 
304031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
305031fd1beSPeter Maydell {
306031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
307031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
308031fd1beSPeter Maydell     int running;
309031fd1beSPeter Maydell 
310031fd1beSPeter Maydell     if (!s->multi) {
311031fd1beSPeter Maydell         return;
312031fd1beSPeter Maydell     }
313031fd1beSPeter Maydell 
314031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
315031fd1beSPeter Maydell 
316031fd1beSPeter Maydell     curl_multi_read(s);
317031fd1beSPeter Maydell #else
318031fd1beSPeter Maydell     abort();
319031fd1beSPeter Maydell #endif
320031fd1beSPeter Maydell }
321031fd1beSPeter Maydell 
322769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s)
323769ce76dSAlexander Graf {
324769ce76dSAlexander Graf     CURLState *state = NULL;
325769ce76dSAlexander Graf     int i, j;
326769ce76dSAlexander Graf 
327769ce76dSAlexander Graf     do {
328769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
329769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
330769ce76dSAlexander Graf                 if (s->states[i].acb[j])
331769ce76dSAlexander Graf                     continue;
332769ce76dSAlexander Graf             if (s->states[i].in_use)
333769ce76dSAlexander Graf                 continue;
334769ce76dSAlexander Graf 
335769ce76dSAlexander Graf             state = &s->states[i];
336769ce76dSAlexander Graf             state->in_use = 1;
337769ce76dSAlexander Graf             break;
338769ce76dSAlexander Graf         }
339769ce76dSAlexander Graf         if (!state) {
340*e4661837SMatthew Booth             qemu_aio_wait();
341769ce76dSAlexander Graf         }
342769ce76dSAlexander Graf     } while(!state);
343769ce76dSAlexander Graf 
3449e550b32SMatthew Booth     if (!state->curl) {
345769ce76dSAlexander Graf         state->curl = curl_easy_init();
3469e550b32SMatthew Booth         if (!state->curl) {
347769ce76dSAlexander Graf             return NULL;
3489e550b32SMatthew Booth         }
349769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
350769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
3519e550b32SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
3529e550b32SMatthew Booth                          (void *)curl_read_cb);
353769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
354769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
355769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
356769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
357769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
358769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
359f785a5aeSNicholas Thomas         curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
360769ce76dSAlexander Graf 
361fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
362fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
363fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
3648a8f5840SStefan Hajnoczi          *
3658a8f5840SStefan Hajnoczi          * Restricting protocols is only supported from 7.19.4 upwards.
366fb6d1bbdSStefan Hajnoczi          */
3678a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
368fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
369fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
3708a8f5840SStefan Hajnoczi #endif
371fb6d1bbdSStefan Hajnoczi 
372769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
373769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
374769ce76dSAlexander Graf #endif
3759e550b32SMatthew Booth     }
376769ce76dSAlexander Graf 
377769ce76dSAlexander Graf     state->s = s;
378769ce76dSAlexander Graf 
379769ce76dSAlexander Graf     return state;
380769ce76dSAlexander Graf }
381769ce76dSAlexander Graf 
382769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
383769ce76dSAlexander Graf {
384769ce76dSAlexander Graf     if (s->s->multi)
385769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
386769ce76dSAlexander Graf     s->in_use = 0;
387769ce76dSAlexander Graf }
388769ce76dSAlexander Graf 
3898e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
3908e6d58cdSKevin Wolf                                 Error **errp)
391769ce76dSAlexander Graf {
392c76f4952SNolan 
393c76f4952SNolan     #define RA_OPTSTR ":readahead="
394c76f4952SNolan     char *file;
395c76f4952SNolan     char *ra;
396c76f4952SNolan     const char *ra_val;
397c76f4952SNolan     int parse_state = 0;
398c76f4952SNolan 
3997267c094SAnthony Liguori     file = g_strdup(filename);
400c76f4952SNolan 
401c76f4952SNolan     /* Parse a trailing ":readahead=#:" param, if present. */
402c76f4952SNolan     ra = file + strlen(file) - 1;
403c76f4952SNolan     while (ra >= file) {
404c76f4952SNolan         if (parse_state == 0) {
4058e6d58cdSKevin Wolf             if (*ra == ':') {
406c76f4952SNolan                 parse_state++;
4078e6d58cdSKevin Wolf             } else {
408c76f4952SNolan                 break;
4098e6d58cdSKevin Wolf             }
410c76f4952SNolan         } else if (parse_state == 1) {
411c76f4952SNolan             if (*ra > '9' || *ra < '0') {
412c76f4952SNolan                 char *opt_start = ra - strlen(RA_OPTSTR) + 1;
413c76f4952SNolan                 if (opt_start > file &&
414c76f4952SNolan                     strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
415c76f4952SNolan                     ra_val = ra + 1;
416c76f4952SNolan                     ra -= strlen(RA_OPTSTR) - 1;
417c76f4952SNolan                     *ra = '\0';
4188e6d58cdSKevin Wolf                     qdict_put(options, "readahead", qstring_from_str(ra_val));
419c76f4952SNolan                 }
4208e6d58cdSKevin Wolf                 break;
421c76f4952SNolan             }
422c76f4952SNolan         }
423c76f4952SNolan         ra--;
424c76f4952SNolan     }
425c76f4952SNolan 
4268e6d58cdSKevin Wolf     qdict_put(options, "url", qstring_from_str(file));
4278e6d58cdSKevin Wolf 
4288e6d58cdSKevin Wolf     g_free(file);
4298e6d58cdSKevin Wolf }
4308e6d58cdSKevin Wolf 
4318e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
4328e6d58cdSKevin Wolf     .name = "curl",
4338e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
4348e6d58cdSKevin Wolf     .desc = {
4358e6d58cdSKevin Wolf         {
4368e6d58cdSKevin Wolf             .name = "url",
4378e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
4388e6d58cdSKevin Wolf             .help = "URL to open",
4398e6d58cdSKevin Wolf         },
4408e6d58cdSKevin Wolf         {
4418e6d58cdSKevin Wolf             .name = "readahead",
4428e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
4438e6d58cdSKevin Wolf             .help = "Readahead size",
4448e6d58cdSKevin Wolf         },
4458e6d58cdSKevin Wolf         { /* end of list */ }
4468e6d58cdSKevin Wolf     },
4478e6d58cdSKevin Wolf };
4488e6d58cdSKevin Wolf 
449015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
450015a1036SMax Reitz                      Error **errp)
4518e6d58cdSKevin Wolf {
4528e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
4538e6d58cdSKevin Wolf     CURLState *state = NULL;
4548e6d58cdSKevin Wolf     QemuOpts *opts;
4558e6d58cdSKevin Wolf     Error *local_err = NULL;
4568e6d58cdSKevin Wolf     const char *file;
4578e6d58cdSKevin Wolf     double d;
4588e6d58cdSKevin Wolf 
4598e6d58cdSKevin Wolf     static int inited = 0;
4608e6d58cdSKevin Wolf 
461a7cea2baSRichard W.M. Jones     if (flags & BDRV_O_RDWR) {
4622a94fee3SPaolo Bonzini         error_setg(errp, "curl block device does not support writes");
463a7cea2baSRichard W.M. Jones         return -EROFS;
464a7cea2baSRichard W.M. Jones     }
465a7cea2baSRichard W.M. Jones 
46687ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
4678e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
46884d18f06SMarkus Armbruster     if (local_err) {
4692a94fee3SPaolo Bonzini         error_propagate(errp, local_err);
4708e6d58cdSKevin Wolf         goto out_noclean;
4718e6d58cdSKevin Wolf     }
4728e6d58cdSKevin Wolf 
4738e6d58cdSKevin Wolf     s->readahead_size = qemu_opt_get_size(opts, "readahead", READ_AHEAD_SIZE);
474c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
4752a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
476c76f4952SNolan                    s->readahead_size);
477c76f4952SNolan         goto out_noclean;
478c76f4952SNolan     }
479c76f4952SNolan 
4808e6d58cdSKevin Wolf     file = qemu_opt_get(opts, "url");
4818e6d58cdSKevin Wolf     if (file == NULL) {
4822a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires 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:
544acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", 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