xref: /openbmc/qemu/block/curl.c (revision 8dc7a772)
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"
2697a3ea57SMatthew Booth #include "qapi/qmp/qbool.h"
27769ce76dSAlexander Graf #include <curl/curl.h>
28769ce76dSAlexander Graf 
29769ce76dSAlexander Graf // #define DEBUG
30769ce76dSAlexander Graf // #define DEBUG_VERBOSE
31769ce76dSAlexander Graf 
32769ce76dSAlexander Graf #ifdef DEBUG_CURL
33d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
34769ce76dSAlexander Graf #else
35d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { } while (0)
36769ce76dSAlexander Graf #endif
37769ce76dSAlexander Graf 
38031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000
39031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */
40031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK
419aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION
429aedd5a5SMatthew Booth #endif
439aedd5a5SMatthew Booth 
449aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION
459aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in
469aedd5a5SMatthew Booth  * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
479aedd5a5SMatthew Booth  * less efficient but still safe. */
489aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
499aedd5a5SMatthew Booth                                             curl_socket_t sockfd,
509aedd5a5SMatthew Booth                                             int ev_bitmask,
519aedd5a5SMatthew Booth                                             int *running_handles)
529aedd5a5SMatthew Booth {
539aedd5a5SMatthew Booth     return curl_multi_socket(multi_handle, sockfd, running_handles);
549aedd5a5SMatthew Booth }
559aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action
56031fd1beSPeter Maydell #endif
57031fd1beSPeter Maydell 
58fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
59fb6d1bbdSStefan Hajnoczi                    CURLPROTO_FTP | CURLPROTO_FTPS | \
60fb6d1bbdSStefan Hajnoczi                    CURLPROTO_TFTP)
61fb6d1bbdSStefan Hajnoczi 
62769ce76dSAlexander Graf #define CURL_NUM_STATES 8
63769ce76dSAlexander Graf #define CURL_NUM_ACB    8
64769ce76dSAlexander Graf #define SECTOR_SIZE     512
65e3542c67SMatthew Booth #define READ_AHEAD_DEFAULT (256 * 1024)
66769ce76dSAlexander Graf 
67769ce76dSAlexander Graf #define FIND_RET_NONE   0
68769ce76dSAlexander Graf #define FIND_RET_OK     1
69769ce76dSAlexander Graf #define FIND_RET_WAIT   2
70769ce76dSAlexander Graf 
71e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL       "url"
72e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead"
7397a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
74e3542c67SMatthew Booth 
75769ce76dSAlexander Graf struct BDRVCURLState;
76769ce76dSAlexander Graf 
77769ce76dSAlexander Graf typedef struct CURLAIOCB {
78769ce76dSAlexander Graf     BlockDriverAIOCB common;
79363c3c85SNick Thomas     QEMUBH *bh;
80769ce76dSAlexander Graf     QEMUIOVector *qiov;
81363c3c85SNick Thomas 
82363c3c85SNick Thomas     int64_t sector_num;
83363c3c85SNick Thomas     int nb_sectors;
84363c3c85SNick Thomas 
85769ce76dSAlexander Graf     size_t start;
86769ce76dSAlexander Graf     size_t end;
87769ce76dSAlexander Graf } CURLAIOCB;
88769ce76dSAlexander Graf 
89769ce76dSAlexander Graf typedef struct CURLState
90769ce76dSAlexander Graf {
91769ce76dSAlexander Graf     struct BDRVCURLState *s;
92769ce76dSAlexander Graf     CURLAIOCB *acb[CURL_NUM_ACB];
93769ce76dSAlexander Graf     CURL *curl;
94838ef602SMatthew Booth     curl_socket_t sock_fd;
95769ce76dSAlexander Graf     char *orig_buf;
96769ce76dSAlexander Graf     size_t buf_start;
97769ce76dSAlexander Graf     size_t buf_off;
98769ce76dSAlexander Graf     size_t buf_len;
99769ce76dSAlexander Graf     char range[128];
100769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
101769ce76dSAlexander Graf     char in_use;
102769ce76dSAlexander Graf } CURLState;
103769ce76dSAlexander Graf 
104769ce76dSAlexander Graf typedef struct BDRVCURLState {
105769ce76dSAlexander Graf     CURLM *multi;
106031fd1beSPeter Maydell     QEMUTimer timer;
107769ce76dSAlexander Graf     size_t len;
108769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
109769ce76dSAlexander Graf     char *url;
110c76f4952SNolan     size_t readahead_size;
11197a3ea57SMatthew Booth     bool sslverify;
1123494d650SFam Zheng     bool accept_range;
11363f0f45fSStefan Hajnoczi     AioContext *aio_context;
114769ce76dSAlexander Graf } BDRVCURLState;
115769ce76dSAlexander Graf 
116769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
117769ce76dSAlexander Graf static void curl_multi_do(void *arg);
118838ef602SMatthew Booth static void curl_multi_read(void *arg);
119769ce76dSAlexander Graf 
120031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
121031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
122031fd1beSPeter Maydell {
123031fd1beSPeter Maydell     BDRVCURLState *s = opaque;
124031fd1beSPeter Maydell 
125031fd1beSPeter Maydell     DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
126031fd1beSPeter Maydell     if (timeout_ms == -1) {
127031fd1beSPeter Maydell         timer_del(&s->timer);
128031fd1beSPeter Maydell     } else {
129031fd1beSPeter Maydell         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
130031fd1beSPeter Maydell         timer_mod(&s->timer,
131031fd1beSPeter Maydell                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
132031fd1beSPeter Maydell     }
133031fd1beSPeter Maydell     return 0;
134031fd1beSPeter Maydell }
135031fd1beSPeter Maydell #endif
136031fd1beSPeter Maydell 
137769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
13863f0f45fSStefan Hajnoczi                         void *userp, void *sp)
139769ce76dSAlexander Graf {
14063f0f45fSStefan Hajnoczi     BDRVCURLState *s;
141838ef602SMatthew Booth     CURLState *state = NULL;
142838ef602SMatthew Booth     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
143838ef602SMatthew Booth     state->sock_fd = fd;
14463f0f45fSStefan Hajnoczi     s = state->s;
145838ef602SMatthew Booth 
146d0f2c4c6Smalc     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
147769ce76dSAlexander Graf     switch (action) {
148769ce76dSAlexander Graf         case CURL_POLL_IN:
14963f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
15063f0f45fSStefan Hajnoczi                                NULL, state);
151769ce76dSAlexander Graf             break;
152769ce76dSAlexander Graf         case CURL_POLL_OUT:
15363f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, NULL, curl_multi_do, state);
154769ce76dSAlexander Graf             break;
155769ce76dSAlexander Graf         case CURL_POLL_INOUT:
15663f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
15763f0f45fSStefan Hajnoczi                                curl_multi_do, state);
158769ce76dSAlexander Graf             break;
159769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
16063f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, NULL, NULL, NULL);
161769ce76dSAlexander Graf             break;
162769ce76dSAlexander Graf     }
163769ce76dSAlexander Graf 
164769ce76dSAlexander Graf     return 0;
165769ce76dSAlexander Graf }
166769ce76dSAlexander Graf 
1673494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
168769ce76dSAlexander Graf {
1693494d650SFam Zheng     BDRVCURLState *s = opaque;
170769ce76dSAlexander Graf     size_t realsize = size * nmemb;
1713494d650SFam Zheng     const char *accept_line = "Accept-Ranges: bytes";
172769ce76dSAlexander Graf 
1733494d650SFam Zheng     if (realsize >= strlen(accept_line)
1743494d650SFam Zheng         && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
1753494d650SFam Zheng         s->accept_range = true;
1760bfcd599SBlue Swirl     }
177769ce76dSAlexander Graf 
178769ce76dSAlexander Graf     return realsize;
179769ce76dSAlexander Graf }
180769ce76dSAlexander Graf 
181769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
182769ce76dSAlexander Graf {
183769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
184769ce76dSAlexander Graf     size_t realsize = size * nmemb;
185769ce76dSAlexander Graf     int i;
186769ce76dSAlexander Graf 
1870bfcd599SBlue Swirl     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
188769ce76dSAlexander Graf 
189769ce76dSAlexander Graf     if (!s || !s->orig_buf)
19038bbc0a5SMatthew Booth         return 0;
191769ce76dSAlexander Graf 
1926d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
1936d4b9e55SFam Zheng         /* buffer full, read nothing */
1946d4b9e55SFam Zheng         return 0;
1956d4b9e55SFam Zheng     }
1966d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
197769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
198769ce76dSAlexander Graf     s->buf_off += realsize;
199769ce76dSAlexander Graf 
200769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
201769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
202769ce76dSAlexander Graf 
203769ce76dSAlexander Graf         if (!acb)
204769ce76dSAlexander Graf             continue;
205769ce76dSAlexander Graf 
206769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
20703396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
208769ce76dSAlexander Graf                                 acb->end - acb->start);
209769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
210769ce76dSAlexander Graf             qemu_aio_release(acb);
211769ce76dSAlexander Graf             s->acb[i] = NULL;
212769ce76dSAlexander Graf         }
213769ce76dSAlexander Graf     }
214769ce76dSAlexander Graf 
215769ce76dSAlexander Graf     return realsize;
216769ce76dSAlexander Graf }
217769ce76dSAlexander Graf 
218769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
219769ce76dSAlexander Graf                          CURLAIOCB *acb)
220769ce76dSAlexander Graf {
221769ce76dSAlexander Graf     int i;
222769ce76dSAlexander Graf     size_t end = start + len;
223769ce76dSAlexander Graf 
224769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
225769ce76dSAlexander Graf         CURLState *state = &s->states[i];
226769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
227769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
228769ce76dSAlexander Graf 
229769ce76dSAlexander Graf         if (!state->orig_buf)
230769ce76dSAlexander Graf             continue;
231769ce76dSAlexander Graf         if (!state->buf_off)
232769ce76dSAlexander Graf             continue;
233769ce76dSAlexander Graf 
234769ce76dSAlexander Graf         // Does the existing buffer cover our section?
235769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
236769ce76dSAlexander Graf             (start <= buf_end) &&
237769ce76dSAlexander Graf             (end >= state->buf_start) &&
238769ce76dSAlexander Graf             (end <= buf_end))
239769ce76dSAlexander Graf         {
240769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
241769ce76dSAlexander Graf 
24203396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
243769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
244769ce76dSAlexander Graf 
245769ce76dSAlexander Graf             return FIND_RET_OK;
246769ce76dSAlexander Graf         }
247769ce76dSAlexander Graf 
248769ce76dSAlexander Graf         // Wait for unfinished chunks
249b7079df4SMatthew Booth         if (state->in_use &&
250b7079df4SMatthew Booth             (start >= state->buf_start) &&
251769ce76dSAlexander Graf             (start <= buf_fend) &&
252769ce76dSAlexander Graf             (end >= state->buf_start) &&
253769ce76dSAlexander Graf             (end <= buf_fend))
254769ce76dSAlexander Graf         {
255769ce76dSAlexander Graf             int j;
256769ce76dSAlexander Graf 
257769ce76dSAlexander Graf             acb->start = start - state->buf_start;
258769ce76dSAlexander Graf             acb->end = acb->start + len;
259769ce76dSAlexander Graf 
260769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
261769ce76dSAlexander Graf                 if (!state->acb[j]) {
262769ce76dSAlexander Graf                     state->acb[j] = acb;
263769ce76dSAlexander Graf                     return FIND_RET_WAIT;
264769ce76dSAlexander Graf                 }
265769ce76dSAlexander Graf             }
266769ce76dSAlexander Graf         }
267769ce76dSAlexander Graf     }
268769ce76dSAlexander Graf 
269769ce76dSAlexander Graf     return FIND_RET_NONE;
270769ce76dSAlexander Graf }
271769ce76dSAlexander Graf 
272838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s)
273769ce76dSAlexander Graf {
274769ce76dSAlexander Graf     int msgs_in_queue;
275769ce76dSAlexander Graf 
276769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
277769ce76dSAlexander Graf      * handle again. */
2781f2cead3SMatthew Booth     for (;;) {
279769ce76dSAlexander Graf         CURLMsg *msg;
280769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
281769ce76dSAlexander Graf 
2821f2cead3SMatthew Booth         /* Quit when there are no more completions */
283769ce76dSAlexander Graf         if (!msg)
284769ce76dSAlexander Graf             break;
285769ce76dSAlexander Graf 
2861f2cead3SMatthew Booth         if (msg->msg == CURLMSG_DONE) {
287769ce76dSAlexander Graf             CURLState *state = NULL;
288f6246509SMatthew Booth             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
289f6246509SMatthew Booth                               (char **)&state);
290f785a5aeSNicholas Thomas 
291f785a5aeSNicholas Thomas             /* ACBs for successful messages get completed in curl_read_cb */
292f785a5aeSNicholas Thomas             if (msg->data.result != CURLE_OK) {
293f785a5aeSNicholas Thomas                 int i;
294f785a5aeSNicholas Thomas                 for (i = 0; i < CURL_NUM_ACB; i++) {
295f785a5aeSNicholas Thomas                     CURLAIOCB *acb = state->acb[i];
296f785a5aeSNicholas Thomas 
297f785a5aeSNicholas Thomas                     if (acb == NULL) {
298f785a5aeSNicholas Thomas                         continue;
299f785a5aeSNicholas Thomas                     }
300f785a5aeSNicholas Thomas 
301f785a5aeSNicholas Thomas                     acb->common.cb(acb->common.opaque, -EIO);
302f785a5aeSNicholas Thomas                     qemu_aio_release(acb);
303f785a5aeSNicholas Thomas                     state->acb[i] = NULL;
304f785a5aeSNicholas Thomas                 }
305f785a5aeSNicholas Thomas             }
306f785a5aeSNicholas Thomas 
307769ce76dSAlexander Graf             curl_clean_state(state);
308769ce76dSAlexander Graf             break;
309769ce76dSAlexander Graf         }
310769ce76dSAlexander Graf     }
311769ce76dSAlexander Graf }
312769ce76dSAlexander Graf 
313031fd1beSPeter Maydell static void curl_multi_do(void *arg)
314031fd1beSPeter Maydell {
315838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
316031fd1beSPeter Maydell     int running;
317031fd1beSPeter Maydell     int r;
318031fd1beSPeter Maydell 
319838ef602SMatthew Booth     if (!s->s->multi) {
320031fd1beSPeter Maydell         return;
321031fd1beSPeter Maydell     }
322031fd1beSPeter Maydell 
323031fd1beSPeter Maydell     do {
324838ef602SMatthew Booth         r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
325031fd1beSPeter Maydell     } while(r == CURLM_CALL_MULTI_PERFORM);
326031fd1beSPeter Maydell 
327838ef602SMatthew Booth }
328838ef602SMatthew Booth 
329838ef602SMatthew Booth static void curl_multi_read(void *arg)
330838ef602SMatthew Booth {
331838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
332838ef602SMatthew Booth 
333838ef602SMatthew Booth     curl_multi_do(arg);
334838ef602SMatthew Booth     curl_multi_check_completion(s->s);
335031fd1beSPeter Maydell }
336031fd1beSPeter Maydell 
337031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
338031fd1beSPeter Maydell {
339031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
340031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
341031fd1beSPeter Maydell     int running;
342031fd1beSPeter Maydell 
343031fd1beSPeter Maydell     if (!s->multi) {
344031fd1beSPeter Maydell         return;
345031fd1beSPeter Maydell     }
346031fd1beSPeter Maydell 
347031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
348031fd1beSPeter Maydell 
349838ef602SMatthew Booth     curl_multi_check_completion(s);
350031fd1beSPeter Maydell #else
351031fd1beSPeter Maydell     abort();
352031fd1beSPeter Maydell #endif
353031fd1beSPeter Maydell }
354031fd1beSPeter Maydell 
355769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s)
356769ce76dSAlexander Graf {
357769ce76dSAlexander Graf     CURLState *state = NULL;
358769ce76dSAlexander Graf     int i, j;
359769ce76dSAlexander Graf 
360769ce76dSAlexander Graf     do {
361769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
362769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
363769ce76dSAlexander Graf                 if (s->states[i].acb[j])
364769ce76dSAlexander Graf                     continue;
365769ce76dSAlexander Graf             if (s->states[i].in_use)
366769ce76dSAlexander Graf                 continue;
367769ce76dSAlexander Graf 
368769ce76dSAlexander Graf             state = &s->states[i];
369769ce76dSAlexander Graf             state->in_use = 1;
370769ce76dSAlexander Graf             break;
371769ce76dSAlexander Graf         }
372769ce76dSAlexander Graf         if (!state) {
37363f0f45fSStefan Hajnoczi             aio_poll(state->s->aio_context, true);
374769ce76dSAlexander Graf         }
375769ce76dSAlexander Graf     } while(!state);
376769ce76dSAlexander Graf 
3779e550b32SMatthew Booth     if (!state->curl) {
378769ce76dSAlexander Graf         state->curl = curl_easy_init();
3799e550b32SMatthew Booth         if (!state->curl) {
380769ce76dSAlexander Graf             return NULL;
3819e550b32SMatthew Booth         }
382769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
38397a3ea57SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
38497a3ea57SMatthew Booth                          (long) s->sslverify);
385769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
3869e550b32SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
3879e550b32SMatthew Booth                          (void *)curl_read_cb);
388769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
389769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
390769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
391769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
392769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
393769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
394f785a5aeSNicholas Thomas         curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
395769ce76dSAlexander Graf 
396fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
397fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
398fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
3998a8f5840SStefan Hajnoczi          *
4008a8f5840SStefan Hajnoczi          * Restricting protocols is only supported from 7.19.4 upwards.
401fb6d1bbdSStefan Hajnoczi          */
4028a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
403fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
404fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
4058a8f5840SStefan Hajnoczi #endif
406fb6d1bbdSStefan Hajnoczi 
407769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
408769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
409769ce76dSAlexander Graf #endif
4109e550b32SMatthew Booth     }
411769ce76dSAlexander Graf 
412769ce76dSAlexander Graf     state->s = s;
413769ce76dSAlexander Graf 
414769ce76dSAlexander Graf     return state;
415769ce76dSAlexander Graf }
416769ce76dSAlexander Graf 
417769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
418769ce76dSAlexander Graf {
419769ce76dSAlexander Graf     if (s->s->multi)
420769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
421769ce76dSAlexander Graf     s->in_use = 0;
422769ce76dSAlexander Graf }
423769ce76dSAlexander Graf 
4248e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
4258e6d58cdSKevin Wolf                                 Error **errp)
426769ce76dSAlexander Graf {
427e3542c67SMatthew Booth     qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
4288e6d58cdSKevin Wolf }
4298e6d58cdSKevin Wolf 
43063f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs)
43163f0f45fSStefan Hajnoczi {
43263f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
43363f0f45fSStefan Hajnoczi     int i;
43463f0f45fSStefan Hajnoczi 
43563f0f45fSStefan Hajnoczi     for (i = 0; i < CURL_NUM_STATES; i++) {
43663f0f45fSStefan Hajnoczi         if (s->states[i].in_use) {
43763f0f45fSStefan Hajnoczi             curl_clean_state(&s->states[i]);
43863f0f45fSStefan Hajnoczi         }
43963f0f45fSStefan Hajnoczi         if (s->states[i].curl) {
44063f0f45fSStefan Hajnoczi             curl_easy_cleanup(s->states[i].curl);
44163f0f45fSStefan Hajnoczi             s->states[i].curl = NULL;
44263f0f45fSStefan Hajnoczi         }
44363f0f45fSStefan Hajnoczi         g_free(s->states[i].orig_buf);
44463f0f45fSStefan Hajnoczi         s->states[i].orig_buf = NULL;
44563f0f45fSStefan Hajnoczi     }
44663f0f45fSStefan Hajnoczi     if (s->multi) {
44763f0f45fSStefan Hajnoczi         curl_multi_cleanup(s->multi);
44863f0f45fSStefan Hajnoczi         s->multi = NULL;
44963f0f45fSStefan Hajnoczi     }
45063f0f45fSStefan Hajnoczi 
45163f0f45fSStefan Hajnoczi     timer_del(&s->timer);
45263f0f45fSStefan Hajnoczi }
45363f0f45fSStefan Hajnoczi 
45463f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs,
45563f0f45fSStefan Hajnoczi                                     AioContext *new_context)
45663f0f45fSStefan Hajnoczi {
45763f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
45863f0f45fSStefan Hajnoczi 
45963f0f45fSStefan Hajnoczi     aio_timer_init(new_context, &s->timer,
46063f0f45fSStefan Hajnoczi                    QEMU_CLOCK_REALTIME, SCALE_NS,
46163f0f45fSStefan Hajnoczi                    curl_multi_timeout_do, s);
46263f0f45fSStefan Hajnoczi 
46363f0f45fSStefan Hajnoczi     assert(!s->multi);
46463f0f45fSStefan Hajnoczi     s->multi = curl_multi_init();
46563f0f45fSStefan Hajnoczi     s->aio_context = new_context;
46663f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
46763f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK
46863f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
46963f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
47063f0f45fSStefan Hajnoczi #endif
47163f0f45fSStefan Hajnoczi }
47263f0f45fSStefan Hajnoczi 
4738e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
4748e6d58cdSKevin Wolf     .name = "curl",
4758e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
4768e6d58cdSKevin Wolf     .desc = {
4778e6d58cdSKevin Wolf         {
478e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_URL,
4798e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
4808e6d58cdSKevin Wolf             .help = "URL to open",
4818e6d58cdSKevin Wolf         },
4828e6d58cdSKevin Wolf         {
483e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_READAHEAD,
4848e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
4858e6d58cdSKevin Wolf             .help = "Readahead size",
4868e6d58cdSKevin Wolf         },
48797a3ea57SMatthew Booth         {
48897a3ea57SMatthew Booth             .name = CURL_BLOCK_OPT_SSLVERIFY,
48997a3ea57SMatthew Booth             .type = QEMU_OPT_BOOL,
49097a3ea57SMatthew Booth             .help = "Verify SSL certificate"
49197a3ea57SMatthew Booth         },
4928e6d58cdSKevin Wolf         { /* end of list */ }
4938e6d58cdSKevin Wolf     },
4948e6d58cdSKevin Wolf };
4958e6d58cdSKevin Wolf 
496015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
497015a1036SMax Reitz                      Error **errp)
4988e6d58cdSKevin Wolf {
4998e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
5008e6d58cdSKevin Wolf     CURLState *state = NULL;
5018e6d58cdSKevin Wolf     QemuOpts *opts;
5028e6d58cdSKevin Wolf     Error *local_err = NULL;
5038e6d58cdSKevin Wolf     const char *file;
5048e6d58cdSKevin Wolf     double d;
5058e6d58cdSKevin Wolf 
5068e6d58cdSKevin Wolf     static int inited = 0;
5078e6d58cdSKevin Wolf 
508a7cea2baSRichard W.M. Jones     if (flags & BDRV_O_RDWR) {
5092a94fee3SPaolo Bonzini         error_setg(errp, "curl block device does not support writes");
510a7cea2baSRichard W.M. Jones         return -EROFS;
511a7cea2baSRichard W.M. Jones     }
512a7cea2baSRichard W.M. Jones 
51387ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
5148e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
51584d18f06SMarkus Armbruster     if (local_err) {
5162a94fee3SPaolo Bonzini         error_propagate(errp, local_err);
5178e6d58cdSKevin Wolf         goto out_noclean;
5188e6d58cdSKevin Wolf     }
5198e6d58cdSKevin Wolf 
520e3542c67SMatthew Booth     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
521e3542c67SMatthew Booth                                           READ_AHEAD_DEFAULT);
522c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
5232a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
524c76f4952SNolan                    s->readahead_size);
525c76f4952SNolan         goto out_noclean;
526c76f4952SNolan     }
527c76f4952SNolan 
52897a3ea57SMatthew Booth     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
52997a3ea57SMatthew Booth 
530e3542c67SMatthew Booth     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
5318e6d58cdSKevin Wolf     if (file == NULL) {
5322a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires an 'url' option");
5338e6d58cdSKevin Wolf         goto out_noclean;
5348e6d58cdSKevin Wolf     }
5358e6d58cdSKevin Wolf 
536769ce76dSAlexander Graf     if (!inited) {
537769ce76dSAlexander Graf         curl_global_init(CURL_GLOBAL_ALL);
538769ce76dSAlexander Graf         inited = 1;
539769ce76dSAlexander Graf     }
540769ce76dSAlexander Graf 
541d0f2c4c6Smalc     DPRINTF("CURL: Opening %s\n", file);
54263f0f45fSStefan Hajnoczi     s->aio_context = bdrv_get_aio_context(bs);
5438e6d58cdSKevin Wolf     s->url = g_strdup(file);
544769ce76dSAlexander Graf     state = curl_init_state(s);
545769ce76dSAlexander Graf     if (!state)
546769ce76dSAlexander Graf         goto out_noclean;
547769ce76dSAlexander Graf 
548769ce76dSAlexander Graf     // Get file size
549769ce76dSAlexander Graf 
5503494d650SFam Zheng     s->accept_range = false;
551769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
5523494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
5533494d650SFam Zheng                      curl_header_cb);
5543494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
555769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
556769ce76dSAlexander Graf         goto out;
557769ce76dSAlexander Graf     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
558769ce76dSAlexander Graf     if (d)
559769ce76dSAlexander Graf         s->len = (size_t)d;
560769ce76dSAlexander Graf     else if(!s->len)
561769ce76dSAlexander Graf         goto out;
5623494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
5633494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
5643494d650SFam Zheng         && !s->accept_range) {
5653494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
5663494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
5673494d650SFam Zheng         goto out;
5683494d650SFam Zheng     }
5690bfcd599SBlue Swirl     DPRINTF("CURL: Size = %zd\n", s->len);
570769ce76dSAlexander Graf 
571769ce76dSAlexander Graf     curl_clean_state(state);
572769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
573769ce76dSAlexander Graf     state->curl = NULL;
574769ce76dSAlexander Graf 
57563f0f45fSStefan Hajnoczi     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
576769ce76dSAlexander Graf 
5778e6d58cdSKevin Wolf     qemu_opts_del(opts);
578769ce76dSAlexander Graf     return 0;
579769ce76dSAlexander Graf 
580769ce76dSAlexander Graf out:
581acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
582769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
583769ce76dSAlexander Graf     state->curl = NULL;
584769ce76dSAlexander Graf out_noclean:
5858e6d58cdSKevin Wolf     g_free(s->url);
5868e6d58cdSKevin Wolf     qemu_opts_del(opts);
587769ce76dSAlexander Graf     return -EINVAL;
588769ce76dSAlexander Graf }
589769ce76dSAlexander Graf 
590c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
591c16b5a2cSChristoph Hellwig {
592c16b5a2cSChristoph Hellwig     // Do we have to implement canceling? Seems to work without...
593c16b5a2cSChristoph Hellwig }
594c16b5a2cSChristoph Hellwig 
595d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = {
596c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(CURLAIOCB),
597c16b5a2cSChristoph Hellwig     .cancel             = curl_aio_cancel,
598c16b5a2cSChristoph Hellwig };
599c16b5a2cSChristoph Hellwig 
600363c3c85SNick Thomas 
601363c3c85SNick Thomas static void curl_readv_bh_cb(void *p)
602769ce76dSAlexander Graf {
603769ce76dSAlexander Graf     CURLState *state;
604b69cdef8SMatthew Booth     int running;
605769ce76dSAlexander Graf 
606363c3c85SNick Thomas     CURLAIOCB *acb = p;
607363c3c85SNick Thomas     BDRVCURLState *s = acb->common.bs->opaque;
608769ce76dSAlexander Graf 
609363c3c85SNick Thomas     qemu_bh_delete(acb->bh);
610363c3c85SNick Thomas     acb->bh = NULL;
611363c3c85SNick Thomas 
612363c3c85SNick Thomas     size_t start = acb->sector_num * SECTOR_SIZE;
613363c3c85SNick Thomas     size_t end;
614769ce76dSAlexander Graf 
615769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
616769ce76dSAlexander Graf     // we can just call the callback and be done.
617363c3c85SNick Thomas     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
618769ce76dSAlexander Graf         case FIND_RET_OK:
619769ce76dSAlexander Graf             qemu_aio_release(acb);
620769ce76dSAlexander Graf             // fall through
621769ce76dSAlexander Graf         case FIND_RET_WAIT:
622363c3c85SNick Thomas             return;
623769ce76dSAlexander Graf         default:
624769ce76dSAlexander Graf             break;
625769ce76dSAlexander Graf     }
626769ce76dSAlexander Graf 
627769ce76dSAlexander Graf     // No cache found, so let's start a new request
628769ce76dSAlexander Graf     state = curl_init_state(s);
629363c3c85SNick Thomas     if (!state) {
630363c3c85SNick Thomas         acb->common.cb(acb->common.opaque, -EIO);
631363c3c85SNick Thomas         qemu_aio_release(acb);
632363c3c85SNick Thomas         return;
633363c3c85SNick Thomas     }
634769ce76dSAlexander Graf 
635769ce76dSAlexander Graf     acb->start = 0;
636363c3c85SNick Thomas     acb->end = (acb->nb_sectors * SECTOR_SIZE);
637769ce76dSAlexander Graf 
638769ce76dSAlexander Graf     state->buf_off = 0;
6397267c094SAnthony Liguori     g_free(state->orig_buf);
640769ce76dSAlexander Graf     state->buf_start = start;
641c76f4952SNolan     state->buf_len = acb->end + s->readahead_size;
642769ce76dSAlexander Graf     end = MIN(start + state->buf_len, s->len) - 1;
643*8dc7a772SKevin Wolf     state->orig_buf = g_try_malloc(state->buf_len);
644*8dc7a772SKevin Wolf     if (state->buf_len && state->orig_buf == NULL) {
645*8dc7a772SKevin Wolf         curl_clean_state(state);
646*8dc7a772SKevin Wolf         acb->common.cb(acb->common.opaque, -ENOMEM);
647*8dc7a772SKevin Wolf         qemu_aio_release(acb);
648*8dc7a772SKevin Wolf         return;
649*8dc7a772SKevin Wolf     }
650769ce76dSAlexander Graf     state->acb[0] = acb;
651769ce76dSAlexander Graf 
6520bfcd599SBlue Swirl     snprintf(state->range, 127, "%zd-%zd", start, end);
6530bfcd599SBlue Swirl     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
654363c3c85SNick Thomas             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
655769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
656769ce76dSAlexander Graf 
657769ce76dSAlexander Graf     curl_multi_add_handle(s->multi, state->curl);
658769ce76dSAlexander Graf 
659b69cdef8SMatthew Booth     /* Tell curl it needs to kick things off */
660b69cdef8SMatthew Booth     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
661363c3c85SNick Thomas }
662363c3c85SNick Thomas 
663363c3c85SNick Thomas static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
664363c3c85SNick Thomas         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
665363c3c85SNick Thomas         BlockDriverCompletionFunc *cb, void *opaque)
666363c3c85SNick Thomas {
667363c3c85SNick Thomas     CURLAIOCB *acb;
668363c3c85SNick Thomas 
669d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
670363c3c85SNick Thomas 
671363c3c85SNick Thomas     acb->qiov = qiov;
672363c3c85SNick Thomas     acb->sector_num = sector_num;
673363c3c85SNick Thomas     acb->nb_sectors = nb_sectors;
674363c3c85SNick Thomas 
67563f0f45fSStefan Hajnoczi     acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
676363c3c85SNick Thomas     qemu_bh_schedule(acb->bh);
677769ce76dSAlexander Graf     return &acb->common;
678769ce76dSAlexander Graf }
679769ce76dSAlexander Graf 
680769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
681769ce76dSAlexander Graf {
682769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
683769ce76dSAlexander Graf 
684d0f2c4c6Smalc     DPRINTF("CURL: Close\n");
68563f0f45fSStefan Hajnoczi     curl_detach_aio_context(bs);
686031fd1beSPeter Maydell 
68745724d6dSStefan Weil     g_free(s->url);
688769ce76dSAlexander Graf }
689769ce76dSAlexander Graf 
690769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs)
691769ce76dSAlexander Graf {
692769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
693769ce76dSAlexander Graf     return s->len;
694769ce76dSAlexander Graf }
695769ce76dSAlexander Graf 
696769ce76dSAlexander Graf static BlockDriver bdrv_http = {
697769ce76dSAlexander Graf     .format_name                = "http",
698769ce76dSAlexander Graf     .protocol_name              = "http",
699769ce76dSAlexander Graf 
700769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7018e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
70266f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
703769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
704769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
705769ce76dSAlexander Graf 
706769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
70763f0f45fSStefan Hajnoczi 
70863f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
70963f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
710769ce76dSAlexander Graf };
711769ce76dSAlexander Graf 
712769ce76dSAlexander Graf static BlockDriver bdrv_https = {
713769ce76dSAlexander Graf     .format_name                = "https",
714769ce76dSAlexander Graf     .protocol_name              = "https",
715769ce76dSAlexander Graf 
716769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7178e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
71866f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
719769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
720769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
721769ce76dSAlexander Graf 
722769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
72363f0f45fSStefan Hajnoczi 
72463f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
72563f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
726769ce76dSAlexander Graf };
727769ce76dSAlexander Graf 
728769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
729769ce76dSAlexander Graf     .format_name                = "ftp",
730769ce76dSAlexander Graf     .protocol_name              = "ftp",
731769ce76dSAlexander Graf 
732769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7338e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
73466f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
735769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
736769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
737769ce76dSAlexander Graf 
738769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
73963f0f45fSStefan Hajnoczi 
74063f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
74163f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
742769ce76dSAlexander Graf };
743769ce76dSAlexander Graf 
744769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
745769ce76dSAlexander Graf     .format_name                = "ftps",
746769ce76dSAlexander Graf     .protocol_name              = "ftps",
747769ce76dSAlexander Graf 
748769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7498e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
75066f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
751769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
752769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
753769ce76dSAlexander Graf 
754769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
75563f0f45fSStefan Hajnoczi 
75663f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
75763f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
758769ce76dSAlexander Graf };
759769ce76dSAlexander Graf 
760769ce76dSAlexander Graf static BlockDriver bdrv_tftp = {
761769ce76dSAlexander Graf     .format_name                = "tftp",
762769ce76dSAlexander Graf     .protocol_name              = "tftp",
763769ce76dSAlexander Graf 
764769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7658e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
76666f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
767769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
768769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
769769ce76dSAlexander Graf 
770769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
77163f0f45fSStefan Hajnoczi 
77263f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
77363f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
774769ce76dSAlexander Graf };
775769ce76dSAlexander Graf 
776769ce76dSAlexander Graf static void curl_block_init(void)
777769ce76dSAlexander Graf {
778769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
779769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
780769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
781769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
782769ce76dSAlexander Graf     bdrv_register(&bdrv_tftp);
783769ce76dSAlexander Graf }
784769ce76dSAlexander Graf 
785769ce76dSAlexander Graf block_init(curl_block_init);
786