xref: /openbmc/qemu/block/curl.c (revision 1f2cead324436da25c3607f4b957f0198a01fc01)
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;
74838ef602SMatthew Booth     curl_socket_t sock_fd;
75769ce76dSAlexander Graf     char *orig_buf;
76769ce76dSAlexander Graf     size_t buf_start;
77769ce76dSAlexander Graf     size_t buf_off;
78769ce76dSAlexander Graf     size_t buf_len;
79769ce76dSAlexander Graf     char range[128];
80769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
81769ce76dSAlexander Graf     char in_use;
82769ce76dSAlexander Graf } CURLState;
83769ce76dSAlexander Graf 
84769ce76dSAlexander Graf typedef struct BDRVCURLState {
85769ce76dSAlexander Graf     CURLM *multi;
86031fd1beSPeter Maydell     QEMUTimer timer;
87769ce76dSAlexander Graf     size_t len;
88769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
89769ce76dSAlexander Graf     char *url;
90c76f4952SNolan     size_t readahead_size;
913494d650SFam Zheng     bool accept_range;
92769ce76dSAlexander Graf } BDRVCURLState;
93769ce76dSAlexander Graf 
94769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
95769ce76dSAlexander Graf static void curl_multi_do(void *arg);
96838ef602SMatthew Booth static void curl_multi_read(void *arg);
97769ce76dSAlexander Graf 
98031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
99031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
100031fd1beSPeter Maydell {
101031fd1beSPeter Maydell     BDRVCURLState *s = opaque;
102031fd1beSPeter Maydell 
103031fd1beSPeter Maydell     DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
104031fd1beSPeter Maydell     if (timeout_ms == -1) {
105031fd1beSPeter Maydell         timer_del(&s->timer);
106031fd1beSPeter Maydell     } else {
107031fd1beSPeter Maydell         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
108031fd1beSPeter Maydell         timer_mod(&s->timer,
109031fd1beSPeter Maydell                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
110031fd1beSPeter Maydell     }
111031fd1beSPeter Maydell     return 0;
112031fd1beSPeter Maydell }
113031fd1beSPeter Maydell #endif
114031fd1beSPeter Maydell 
115769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
116769ce76dSAlexander Graf                         void *s, void *sp)
117769ce76dSAlexander Graf {
118838ef602SMatthew Booth     CURLState *state = NULL;
119838ef602SMatthew Booth     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
120838ef602SMatthew Booth     state->sock_fd = fd;
121838ef602SMatthew Booth 
122d0f2c4c6Smalc     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
123769ce76dSAlexander Graf     switch (action) {
124769ce76dSAlexander Graf         case CURL_POLL_IN:
125838ef602SMatthew Booth             qemu_aio_set_fd_handler(fd, curl_multi_read, NULL, state);
126769ce76dSAlexander Graf             break;
127769ce76dSAlexander Graf         case CURL_POLL_OUT:
128838ef602SMatthew Booth             qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, state);
129769ce76dSAlexander Graf             break;
130769ce76dSAlexander Graf         case CURL_POLL_INOUT:
131838ef602SMatthew Booth             qemu_aio_set_fd_handler(fd, curl_multi_read, curl_multi_do, state);
132769ce76dSAlexander Graf             break;
133769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
134f2e5dca4SStefan Hajnoczi             qemu_aio_set_fd_handler(fd, NULL, NULL, NULL);
135769ce76dSAlexander Graf             break;
136769ce76dSAlexander Graf     }
137769ce76dSAlexander Graf 
138769ce76dSAlexander Graf     return 0;
139769ce76dSAlexander Graf }
140769ce76dSAlexander Graf 
1413494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
142769ce76dSAlexander Graf {
1433494d650SFam Zheng     BDRVCURLState *s = opaque;
144769ce76dSAlexander Graf     size_t realsize = size * nmemb;
1453494d650SFam Zheng     const char *accept_line = "Accept-Ranges: bytes";
146769ce76dSAlexander Graf 
1473494d650SFam Zheng     if (realsize >= strlen(accept_line)
1483494d650SFam Zheng         && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
1493494d650SFam Zheng         s->accept_range = true;
1500bfcd599SBlue Swirl     }
151769ce76dSAlexander Graf 
152769ce76dSAlexander Graf     return realsize;
153769ce76dSAlexander Graf }
154769ce76dSAlexander Graf 
155769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
156769ce76dSAlexander Graf {
157769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
158769ce76dSAlexander Graf     size_t realsize = size * nmemb;
159769ce76dSAlexander Graf     int i;
160769ce76dSAlexander Graf 
1610bfcd599SBlue Swirl     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
162769ce76dSAlexander Graf 
163769ce76dSAlexander Graf     if (!s || !s->orig_buf)
16438bbc0a5SMatthew Booth         return 0;
165769ce76dSAlexander Graf 
1666d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
1676d4b9e55SFam Zheng         /* buffer full, read nothing */
1686d4b9e55SFam Zheng         return 0;
1696d4b9e55SFam Zheng     }
1706d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
171769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
172769ce76dSAlexander Graf     s->buf_off += realsize;
173769ce76dSAlexander Graf 
174769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
175769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
176769ce76dSAlexander Graf 
177769ce76dSAlexander Graf         if (!acb)
178769ce76dSAlexander Graf             continue;
179769ce76dSAlexander Graf 
180769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
18103396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
182769ce76dSAlexander Graf                                 acb->end - acb->start);
183769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
184769ce76dSAlexander Graf             qemu_aio_release(acb);
185769ce76dSAlexander Graf             s->acb[i] = NULL;
186769ce76dSAlexander Graf         }
187769ce76dSAlexander Graf     }
188769ce76dSAlexander Graf 
189769ce76dSAlexander Graf     return realsize;
190769ce76dSAlexander Graf }
191769ce76dSAlexander Graf 
192769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
193769ce76dSAlexander Graf                          CURLAIOCB *acb)
194769ce76dSAlexander Graf {
195769ce76dSAlexander Graf     int i;
196769ce76dSAlexander Graf     size_t end = start + len;
197769ce76dSAlexander Graf 
198769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
199769ce76dSAlexander Graf         CURLState *state = &s->states[i];
200769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
201769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
202769ce76dSAlexander Graf 
203769ce76dSAlexander Graf         if (!state->orig_buf)
204769ce76dSAlexander Graf             continue;
205769ce76dSAlexander Graf         if (!state->buf_off)
206769ce76dSAlexander Graf             continue;
207769ce76dSAlexander Graf 
208769ce76dSAlexander Graf         // Does the existing buffer cover our section?
209769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
210769ce76dSAlexander Graf             (start <= buf_end) &&
211769ce76dSAlexander Graf             (end >= state->buf_start) &&
212769ce76dSAlexander Graf             (end <= buf_end))
213769ce76dSAlexander Graf         {
214769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
215769ce76dSAlexander Graf 
21603396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
217769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
218769ce76dSAlexander Graf 
219769ce76dSAlexander Graf             return FIND_RET_OK;
220769ce76dSAlexander Graf         }
221769ce76dSAlexander Graf 
222769ce76dSAlexander Graf         // Wait for unfinished chunks
223769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
224769ce76dSAlexander Graf             (start <= buf_fend) &&
225769ce76dSAlexander Graf             (end >= state->buf_start) &&
226769ce76dSAlexander Graf             (end <= buf_fend))
227769ce76dSAlexander Graf         {
228769ce76dSAlexander Graf             int j;
229769ce76dSAlexander Graf 
230769ce76dSAlexander Graf             acb->start = start - state->buf_start;
231769ce76dSAlexander Graf             acb->end = acb->start + len;
232769ce76dSAlexander Graf 
233769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
234769ce76dSAlexander Graf                 if (!state->acb[j]) {
235769ce76dSAlexander Graf                     state->acb[j] = acb;
236769ce76dSAlexander Graf                     return FIND_RET_WAIT;
237769ce76dSAlexander Graf                 }
238769ce76dSAlexander Graf             }
239769ce76dSAlexander Graf         }
240769ce76dSAlexander Graf     }
241769ce76dSAlexander Graf 
242769ce76dSAlexander Graf     return FIND_RET_NONE;
243769ce76dSAlexander Graf }
244769ce76dSAlexander Graf 
245838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s)
246769ce76dSAlexander Graf {
247769ce76dSAlexander Graf     int msgs_in_queue;
248769ce76dSAlexander Graf 
249769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
250769ce76dSAlexander Graf      * handle again. */
251*1f2cead3SMatthew Booth     for (;;) {
252769ce76dSAlexander Graf         CURLMsg *msg;
253769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
254769ce76dSAlexander Graf 
255*1f2cead3SMatthew Booth         /* Quit when there are no more completions */
256769ce76dSAlexander Graf         if (!msg)
257769ce76dSAlexander Graf             break;
258769ce76dSAlexander Graf 
259*1f2cead3SMatthew Booth         if (msg->msg == CURLMSG_DONE) {
260769ce76dSAlexander Graf             CURLState *state = NULL;
261f6246509SMatthew Booth             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
262f6246509SMatthew Booth                               (char **)&state);
263f785a5aeSNicholas Thomas 
264f785a5aeSNicholas Thomas             /* ACBs for successful messages get completed in curl_read_cb */
265f785a5aeSNicholas Thomas             if (msg->data.result != CURLE_OK) {
266f785a5aeSNicholas Thomas                 int i;
267f785a5aeSNicholas Thomas                 for (i = 0; i < CURL_NUM_ACB; i++) {
268f785a5aeSNicholas Thomas                     CURLAIOCB *acb = state->acb[i];
269f785a5aeSNicholas Thomas 
270f785a5aeSNicholas Thomas                     if (acb == NULL) {
271f785a5aeSNicholas Thomas                         continue;
272f785a5aeSNicholas Thomas                     }
273f785a5aeSNicholas Thomas 
274f785a5aeSNicholas Thomas                     acb->common.cb(acb->common.opaque, -EIO);
275f785a5aeSNicholas Thomas                     qemu_aio_release(acb);
276f785a5aeSNicholas Thomas                     state->acb[i] = NULL;
277f785a5aeSNicholas Thomas                 }
278f785a5aeSNicholas Thomas             }
279f785a5aeSNicholas Thomas 
280769ce76dSAlexander Graf             curl_clean_state(state);
281769ce76dSAlexander Graf             break;
282769ce76dSAlexander Graf         }
283769ce76dSAlexander Graf     }
284769ce76dSAlexander Graf }
285769ce76dSAlexander Graf 
286031fd1beSPeter Maydell static void curl_multi_do(void *arg)
287031fd1beSPeter Maydell {
288838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
289031fd1beSPeter Maydell     int running;
290031fd1beSPeter Maydell     int r;
291031fd1beSPeter Maydell 
292838ef602SMatthew Booth     if (!s->s->multi) {
293031fd1beSPeter Maydell         return;
294031fd1beSPeter Maydell     }
295031fd1beSPeter Maydell 
296031fd1beSPeter Maydell     do {
297838ef602SMatthew Booth         r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
298031fd1beSPeter Maydell     } while(r == CURLM_CALL_MULTI_PERFORM);
299031fd1beSPeter Maydell 
300838ef602SMatthew Booth }
301838ef602SMatthew Booth 
302838ef602SMatthew Booth static void curl_multi_read(void *arg)
303838ef602SMatthew Booth {
304838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
305838ef602SMatthew Booth 
306838ef602SMatthew Booth     curl_multi_do(arg);
307838ef602SMatthew Booth     curl_multi_check_completion(s->s);
308031fd1beSPeter Maydell }
309031fd1beSPeter Maydell 
310031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
311031fd1beSPeter Maydell {
312031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
313031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
314031fd1beSPeter Maydell     int running;
315031fd1beSPeter Maydell 
316031fd1beSPeter Maydell     if (!s->multi) {
317031fd1beSPeter Maydell         return;
318031fd1beSPeter Maydell     }
319031fd1beSPeter Maydell 
320031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
321031fd1beSPeter Maydell 
322838ef602SMatthew Booth     curl_multi_check_completion(s);
323031fd1beSPeter Maydell #else
324031fd1beSPeter Maydell     abort();
325031fd1beSPeter Maydell #endif
326031fd1beSPeter Maydell }
327031fd1beSPeter Maydell 
328769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s)
329769ce76dSAlexander Graf {
330769ce76dSAlexander Graf     CURLState *state = NULL;
331769ce76dSAlexander Graf     int i, j;
332769ce76dSAlexander Graf 
333769ce76dSAlexander Graf     do {
334769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
335769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
336769ce76dSAlexander Graf                 if (s->states[i].acb[j])
337769ce76dSAlexander Graf                     continue;
338769ce76dSAlexander Graf             if (s->states[i].in_use)
339769ce76dSAlexander Graf                 continue;
340769ce76dSAlexander Graf 
341769ce76dSAlexander Graf             state = &s->states[i];
342769ce76dSAlexander Graf             state->in_use = 1;
343769ce76dSAlexander Graf             break;
344769ce76dSAlexander Graf         }
345769ce76dSAlexander Graf         if (!state) {
346e4661837SMatthew Booth             qemu_aio_wait();
347769ce76dSAlexander Graf         }
348769ce76dSAlexander Graf     } while(!state);
349769ce76dSAlexander Graf 
3509e550b32SMatthew Booth     if (!state->curl) {
351769ce76dSAlexander Graf         state->curl = curl_easy_init();
3529e550b32SMatthew Booth         if (!state->curl) {
353769ce76dSAlexander Graf             return NULL;
3549e550b32SMatthew Booth         }
355769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
356769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
3579e550b32SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
3589e550b32SMatthew Booth                          (void *)curl_read_cb);
359769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
360769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
361769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
362769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
363769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
364769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
365f785a5aeSNicholas Thomas         curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
366769ce76dSAlexander Graf 
367fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
368fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
369fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
3708a8f5840SStefan Hajnoczi          *
3718a8f5840SStefan Hajnoczi          * Restricting protocols is only supported from 7.19.4 upwards.
372fb6d1bbdSStefan Hajnoczi          */
3738a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
374fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
375fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
3768a8f5840SStefan Hajnoczi #endif
377fb6d1bbdSStefan Hajnoczi 
378769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
379769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
380769ce76dSAlexander Graf #endif
3819e550b32SMatthew Booth     }
382769ce76dSAlexander Graf 
383769ce76dSAlexander Graf     state->s = s;
384769ce76dSAlexander Graf 
385769ce76dSAlexander Graf     return state;
386769ce76dSAlexander Graf }
387769ce76dSAlexander Graf 
388769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
389769ce76dSAlexander Graf {
390769ce76dSAlexander Graf     if (s->s->multi)
391769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
392769ce76dSAlexander Graf     s->in_use = 0;
393769ce76dSAlexander Graf }
394769ce76dSAlexander Graf 
3958e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
3968e6d58cdSKevin Wolf                                 Error **errp)
397769ce76dSAlexander Graf {
398c76f4952SNolan 
399c76f4952SNolan     #define RA_OPTSTR ":readahead="
400c76f4952SNolan     char *file;
401c76f4952SNolan     char *ra;
402c76f4952SNolan     const char *ra_val;
403c76f4952SNolan     int parse_state = 0;
404c76f4952SNolan 
4057267c094SAnthony Liguori     file = g_strdup(filename);
406c76f4952SNolan 
407c76f4952SNolan     /* Parse a trailing ":readahead=#:" param, if present. */
408c76f4952SNolan     ra = file + strlen(file) - 1;
409c76f4952SNolan     while (ra >= file) {
410c76f4952SNolan         if (parse_state == 0) {
4118e6d58cdSKevin Wolf             if (*ra == ':') {
412c76f4952SNolan                 parse_state++;
4138e6d58cdSKevin Wolf             } else {
414c76f4952SNolan                 break;
4158e6d58cdSKevin Wolf             }
416c76f4952SNolan         } else if (parse_state == 1) {
417c76f4952SNolan             if (*ra > '9' || *ra < '0') {
418c76f4952SNolan                 char *opt_start = ra - strlen(RA_OPTSTR) + 1;
419c76f4952SNolan                 if (opt_start > file &&
420c76f4952SNolan                     strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
421c76f4952SNolan                     ra_val = ra + 1;
422c76f4952SNolan                     ra -= strlen(RA_OPTSTR) - 1;
423c76f4952SNolan                     *ra = '\0';
4248e6d58cdSKevin Wolf                     qdict_put(options, "readahead", qstring_from_str(ra_val));
425c76f4952SNolan                 }
4268e6d58cdSKevin Wolf                 break;
427c76f4952SNolan             }
428c76f4952SNolan         }
429c76f4952SNolan         ra--;
430c76f4952SNolan     }
431c76f4952SNolan 
4328e6d58cdSKevin Wolf     qdict_put(options, "url", qstring_from_str(file));
4338e6d58cdSKevin Wolf 
4348e6d58cdSKevin Wolf     g_free(file);
4358e6d58cdSKevin Wolf }
4368e6d58cdSKevin Wolf 
4378e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
4388e6d58cdSKevin Wolf     .name = "curl",
4398e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
4408e6d58cdSKevin Wolf     .desc = {
4418e6d58cdSKevin Wolf         {
4428e6d58cdSKevin Wolf             .name = "url",
4438e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
4448e6d58cdSKevin Wolf             .help = "URL to open",
4458e6d58cdSKevin Wolf         },
4468e6d58cdSKevin Wolf         {
4478e6d58cdSKevin Wolf             .name = "readahead",
4488e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
4498e6d58cdSKevin Wolf             .help = "Readahead size",
4508e6d58cdSKevin Wolf         },
4518e6d58cdSKevin Wolf         { /* end of list */ }
4528e6d58cdSKevin Wolf     },
4538e6d58cdSKevin Wolf };
4548e6d58cdSKevin Wolf 
455015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
456015a1036SMax Reitz                      Error **errp)
4578e6d58cdSKevin Wolf {
4588e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
4598e6d58cdSKevin Wolf     CURLState *state = NULL;
4608e6d58cdSKevin Wolf     QemuOpts *opts;
4618e6d58cdSKevin Wolf     Error *local_err = NULL;
4628e6d58cdSKevin Wolf     const char *file;
4638e6d58cdSKevin Wolf     double d;
4648e6d58cdSKevin Wolf 
4658e6d58cdSKevin Wolf     static int inited = 0;
4668e6d58cdSKevin Wolf 
467a7cea2baSRichard W.M. Jones     if (flags & BDRV_O_RDWR) {
4682a94fee3SPaolo Bonzini         error_setg(errp, "curl block device does not support writes");
469a7cea2baSRichard W.M. Jones         return -EROFS;
470a7cea2baSRichard W.M. Jones     }
471a7cea2baSRichard W.M. Jones 
47287ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
4738e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
47484d18f06SMarkus Armbruster     if (local_err) {
4752a94fee3SPaolo Bonzini         error_propagate(errp, local_err);
4768e6d58cdSKevin Wolf         goto out_noclean;
4778e6d58cdSKevin Wolf     }
4788e6d58cdSKevin Wolf 
4798e6d58cdSKevin Wolf     s->readahead_size = qemu_opt_get_size(opts, "readahead", READ_AHEAD_SIZE);
480c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
4812a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
482c76f4952SNolan                    s->readahead_size);
483c76f4952SNolan         goto out_noclean;
484c76f4952SNolan     }
485c76f4952SNolan 
4868e6d58cdSKevin Wolf     file = qemu_opt_get(opts, "url");
4878e6d58cdSKevin Wolf     if (file == NULL) {
4882a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires an 'url' option");
4898e6d58cdSKevin Wolf         goto out_noclean;
4908e6d58cdSKevin Wolf     }
4918e6d58cdSKevin Wolf 
492769ce76dSAlexander Graf     if (!inited) {
493769ce76dSAlexander Graf         curl_global_init(CURL_GLOBAL_ALL);
494769ce76dSAlexander Graf         inited = 1;
495769ce76dSAlexander Graf     }
496769ce76dSAlexander Graf 
497d0f2c4c6Smalc     DPRINTF("CURL: Opening %s\n", file);
4988e6d58cdSKevin Wolf     s->url = g_strdup(file);
499769ce76dSAlexander Graf     state = curl_init_state(s);
500769ce76dSAlexander Graf     if (!state)
501769ce76dSAlexander Graf         goto out_noclean;
502769ce76dSAlexander Graf 
503769ce76dSAlexander Graf     // Get file size
504769ce76dSAlexander Graf 
5053494d650SFam Zheng     s->accept_range = false;
506769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
5073494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
5083494d650SFam Zheng                      curl_header_cb);
5093494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
510769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
511769ce76dSAlexander Graf         goto out;
512769ce76dSAlexander Graf     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
513769ce76dSAlexander Graf     if (d)
514769ce76dSAlexander Graf         s->len = (size_t)d;
515769ce76dSAlexander Graf     else if(!s->len)
516769ce76dSAlexander Graf         goto out;
5173494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
5183494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
5193494d650SFam Zheng         && !s->accept_range) {
5203494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
5213494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
5223494d650SFam Zheng         goto out;
5233494d650SFam Zheng     }
5240bfcd599SBlue Swirl     DPRINTF("CURL: Size = %zd\n", s->len);
525769ce76dSAlexander Graf 
526769ce76dSAlexander Graf     curl_clean_state(state);
527769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
528769ce76dSAlexander Graf     state->curl = NULL;
529769ce76dSAlexander Graf 
530031fd1beSPeter Maydell     aio_timer_init(bdrv_get_aio_context(bs), &s->timer,
531031fd1beSPeter Maydell                    QEMU_CLOCK_REALTIME, SCALE_NS,
532031fd1beSPeter Maydell                    curl_multi_timeout_do, s);
533031fd1beSPeter Maydell 
534769ce76dSAlexander Graf     // Now we know the file exists and its size, so let's
535769ce76dSAlexander Graf     // initialize the multi interface!
536769ce76dSAlexander Graf 
537769ce76dSAlexander Graf     s->multi = curl_multi_init();
538769ce76dSAlexander Graf     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
539031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
540031fd1beSPeter Maydell     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
541031fd1beSPeter Maydell     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
542031fd1beSPeter Maydell #endif
543769ce76dSAlexander Graf 
5448e6d58cdSKevin Wolf     qemu_opts_del(opts);
545769ce76dSAlexander Graf     return 0;
546769ce76dSAlexander Graf 
547769ce76dSAlexander Graf out:
548acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
549769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
550769ce76dSAlexander Graf     state->curl = NULL;
551769ce76dSAlexander Graf out_noclean:
5528e6d58cdSKevin Wolf     g_free(s->url);
5538e6d58cdSKevin Wolf     qemu_opts_del(opts);
554769ce76dSAlexander Graf     return -EINVAL;
555769ce76dSAlexander Graf }
556769ce76dSAlexander Graf 
557c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
558c16b5a2cSChristoph Hellwig {
559c16b5a2cSChristoph Hellwig     // Do we have to implement canceling? Seems to work without...
560c16b5a2cSChristoph Hellwig }
561c16b5a2cSChristoph Hellwig 
562d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = {
563c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(CURLAIOCB),
564c16b5a2cSChristoph Hellwig     .cancel             = curl_aio_cancel,
565c16b5a2cSChristoph Hellwig };
566c16b5a2cSChristoph Hellwig 
567363c3c85SNick Thomas 
568363c3c85SNick Thomas static void curl_readv_bh_cb(void *p)
569769ce76dSAlexander Graf {
570769ce76dSAlexander Graf     CURLState *state;
571b69cdef8SMatthew Booth     int running;
572769ce76dSAlexander Graf 
573363c3c85SNick Thomas     CURLAIOCB *acb = p;
574363c3c85SNick Thomas     BDRVCURLState *s = acb->common.bs->opaque;
575769ce76dSAlexander Graf 
576363c3c85SNick Thomas     qemu_bh_delete(acb->bh);
577363c3c85SNick Thomas     acb->bh = NULL;
578363c3c85SNick Thomas 
579363c3c85SNick Thomas     size_t start = acb->sector_num * SECTOR_SIZE;
580363c3c85SNick Thomas     size_t end;
581769ce76dSAlexander Graf 
582769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
583769ce76dSAlexander Graf     // we can just call the callback and be done.
584363c3c85SNick Thomas     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
585769ce76dSAlexander Graf         case FIND_RET_OK:
586769ce76dSAlexander Graf             qemu_aio_release(acb);
587769ce76dSAlexander Graf             // fall through
588769ce76dSAlexander Graf         case FIND_RET_WAIT:
589363c3c85SNick Thomas             return;
590769ce76dSAlexander Graf         default:
591769ce76dSAlexander Graf             break;
592769ce76dSAlexander Graf     }
593769ce76dSAlexander Graf 
594769ce76dSAlexander Graf     // No cache found, so let's start a new request
595769ce76dSAlexander Graf     state = curl_init_state(s);
596363c3c85SNick Thomas     if (!state) {
597363c3c85SNick Thomas         acb->common.cb(acb->common.opaque, -EIO);
598363c3c85SNick Thomas         qemu_aio_release(acb);
599363c3c85SNick Thomas         return;
600363c3c85SNick Thomas     }
601769ce76dSAlexander Graf 
602769ce76dSAlexander Graf     acb->start = 0;
603363c3c85SNick Thomas     acb->end = (acb->nb_sectors * SECTOR_SIZE);
604769ce76dSAlexander Graf 
605769ce76dSAlexander Graf     state->buf_off = 0;
606769ce76dSAlexander Graf     if (state->orig_buf)
6077267c094SAnthony Liguori         g_free(state->orig_buf);
608769ce76dSAlexander Graf     state->buf_start = start;
609c76f4952SNolan     state->buf_len = acb->end + s->readahead_size;
610769ce76dSAlexander Graf     end = MIN(start + state->buf_len, s->len) - 1;
6117267c094SAnthony Liguori     state->orig_buf = g_malloc(state->buf_len);
612769ce76dSAlexander Graf     state->acb[0] = acb;
613769ce76dSAlexander Graf 
6140bfcd599SBlue Swirl     snprintf(state->range, 127, "%zd-%zd", start, end);
6150bfcd599SBlue Swirl     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
616363c3c85SNick Thomas             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
617769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
618769ce76dSAlexander Graf 
619769ce76dSAlexander Graf     curl_multi_add_handle(s->multi, state->curl);
620769ce76dSAlexander Graf 
621b69cdef8SMatthew Booth     /* Tell curl it needs to kick things off */
622b69cdef8SMatthew Booth     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
623363c3c85SNick Thomas }
624363c3c85SNick Thomas 
625363c3c85SNick Thomas static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
626363c3c85SNick Thomas         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
627363c3c85SNick Thomas         BlockDriverCompletionFunc *cb, void *opaque)
628363c3c85SNick Thomas {
629363c3c85SNick Thomas     CURLAIOCB *acb;
630363c3c85SNick Thomas 
631d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
632363c3c85SNick Thomas 
633363c3c85SNick Thomas     acb->qiov = qiov;
634363c3c85SNick Thomas     acb->sector_num = sector_num;
635363c3c85SNick Thomas     acb->nb_sectors = nb_sectors;
636363c3c85SNick Thomas 
637363c3c85SNick Thomas     acb->bh = qemu_bh_new(curl_readv_bh_cb, acb);
638363c3c85SNick Thomas     qemu_bh_schedule(acb->bh);
639769ce76dSAlexander Graf     return &acb->common;
640769ce76dSAlexander Graf }
641769ce76dSAlexander Graf 
642769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
643769ce76dSAlexander Graf {
644769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
645769ce76dSAlexander Graf     int i;
646769ce76dSAlexander Graf 
647d0f2c4c6Smalc     DPRINTF("CURL: Close\n");
648769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
649769ce76dSAlexander Graf         if (s->states[i].in_use)
650769ce76dSAlexander Graf             curl_clean_state(&s->states[i]);
651769ce76dSAlexander Graf         if (s->states[i].curl) {
652769ce76dSAlexander Graf             curl_easy_cleanup(s->states[i].curl);
653769ce76dSAlexander Graf             s->states[i].curl = NULL;
654769ce76dSAlexander Graf         }
655769ce76dSAlexander Graf         if (s->states[i].orig_buf) {
6567267c094SAnthony Liguori             g_free(s->states[i].orig_buf);
657769ce76dSAlexander Graf             s->states[i].orig_buf = NULL;
658769ce76dSAlexander Graf         }
659769ce76dSAlexander Graf     }
660769ce76dSAlexander Graf     if (s->multi)
661769ce76dSAlexander Graf         curl_multi_cleanup(s->multi);
662031fd1beSPeter Maydell 
663031fd1beSPeter Maydell     timer_del(&s->timer);
664031fd1beSPeter Maydell 
66545724d6dSStefan Weil     g_free(s->url);
666769ce76dSAlexander Graf }
667769ce76dSAlexander Graf 
668769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs)
669769ce76dSAlexander Graf {
670769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
671769ce76dSAlexander Graf     return s->len;
672769ce76dSAlexander Graf }
673769ce76dSAlexander Graf 
674769ce76dSAlexander Graf static BlockDriver bdrv_http = {
675769ce76dSAlexander Graf     .format_name            = "http",
676769ce76dSAlexander Graf     .protocol_name          = "http",
677769ce76dSAlexander Graf 
678769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
6798e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
68066f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
681769ce76dSAlexander Graf     .bdrv_close             = curl_close,
682769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
683769ce76dSAlexander Graf 
684769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
685769ce76dSAlexander Graf };
686769ce76dSAlexander Graf 
687769ce76dSAlexander Graf static BlockDriver bdrv_https = {
688769ce76dSAlexander Graf     .format_name            = "https",
689769ce76dSAlexander Graf     .protocol_name          = "https",
690769ce76dSAlexander Graf 
691769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
6928e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
69366f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
694769ce76dSAlexander Graf     .bdrv_close             = curl_close,
695769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
696769ce76dSAlexander Graf 
697769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
698769ce76dSAlexander Graf };
699769ce76dSAlexander Graf 
700769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
701769ce76dSAlexander Graf     .format_name            = "ftp",
702769ce76dSAlexander Graf     .protocol_name          = "ftp",
703769ce76dSAlexander Graf 
704769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
7058e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
70666f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
707769ce76dSAlexander Graf     .bdrv_close             = curl_close,
708769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
709769ce76dSAlexander Graf 
710769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
711769ce76dSAlexander Graf };
712769ce76dSAlexander Graf 
713769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
714769ce76dSAlexander Graf     .format_name            = "ftps",
715769ce76dSAlexander Graf     .protocol_name          = "ftps",
716769ce76dSAlexander Graf 
717769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
7188e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
71966f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
720769ce76dSAlexander Graf     .bdrv_close             = curl_close,
721769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
722769ce76dSAlexander Graf 
723769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
724769ce76dSAlexander Graf };
725769ce76dSAlexander Graf 
726769ce76dSAlexander Graf static BlockDriver bdrv_tftp = {
727769ce76dSAlexander Graf     .format_name            = "tftp",
728769ce76dSAlexander Graf     .protocol_name          = "tftp",
729769ce76dSAlexander Graf 
730769ce76dSAlexander Graf     .instance_size          = sizeof(BDRVCURLState),
7318e6d58cdSKevin Wolf     .bdrv_parse_filename    = curl_parse_filename,
73266f82ceeSKevin Wolf     .bdrv_file_open         = curl_open,
733769ce76dSAlexander Graf     .bdrv_close             = curl_close,
734769ce76dSAlexander Graf     .bdrv_getlength         = curl_getlength,
735769ce76dSAlexander Graf 
736769ce76dSAlexander Graf     .bdrv_aio_readv         = curl_aio_readv,
737769ce76dSAlexander Graf };
738769ce76dSAlexander Graf 
739769ce76dSAlexander Graf static void curl_block_init(void)
740769ce76dSAlexander Graf {
741769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
742769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
743769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
744769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
745769ce76dSAlexander Graf     bdrv_register(&bdrv_tftp);
746769ce76dSAlexander Graf }
747769ce76dSAlexander Graf 
748769ce76dSAlexander Graf block_init(curl_block_init);
749