xref: /openbmc/qemu/block/curl.c (revision a94f83d9)
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)
66212aefaaSDaniel Henrique Barboza #define CURL_TIMEOUT_DEFAULT 5
67769ce76dSAlexander Graf 
68769ce76dSAlexander Graf #define FIND_RET_NONE   0
69769ce76dSAlexander Graf #define FIND_RET_OK     1
70769ce76dSAlexander Graf #define FIND_RET_WAIT   2
71769ce76dSAlexander Graf 
72e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL       "url"
73e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead"
7497a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
75212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout"
76*a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE    "cookie"
77e3542c67SMatthew Booth 
78769ce76dSAlexander Graf struct BDRVCURLState;
79769ce76dSAlexander Graf 
80769ce76dSAlexander Graf typedef struct CURLAIOCB {
81769ce76dSAlexander Graf     BlockDriverAIOCB common;
82363c3c85SNick Thomas     QEMUBH *bh;
83769ce76dSAlexander Graf     QEMUIOVector *qiov;
84363c3c85SNick Thomas 
85363c3c85SNick Thomas     int64_t sector_num;
86363c3c85SNick Thomas     int nb_sectors;
87363c3c85SNick Thomas 
88769ce76dSAlexander Graf     size_t start;
89769ce76dSAlexander Graf     size_t end;
90769ce76dSAlexander Graf } CURLAIOCB;
91769ce76dSAlexander Graf 
92769ce76dSAlexander Graf typedef struct CURLState
93769ce76dSAlexander Graf {
94769ce76dSAlexander Graf     struct BDRVCURLState *s;
95769ce76dSAlexander Graf     CURLAIOCB *acb[CURL_NUM_ACB];
96769ce76dSAlexander Graf     CURL *curl;
97838ef602SMatthew Booth     curl_socket_t sock_fd;
98769ce76dSAlexander Graf     char *orig_buf;
99769ce76dSAlexander Graf     size_t buf_start;
100769ce76dSAlexander Graf     size_t buf_off;
101769ce76dSAlexander Graf     size_t buf_len;
102769ce76dSAlexander Graf     char range[128];
103769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
104769ce76dSAlexander Graf     char in_use;
105769ce76dSAlexander Graf } CURLState;
106769ce76dSAlexander Graf 
107769ce76dSAlexander Graf typedef struct BDRVCURLState {
108769ce76dSAlexander Graf     CURLM *multi;
109031fd1beSPeter Maydell     QEMUTimer timer;
110769ce76dSAlexander Graf     size_t len;
111769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
112769ce76dSAlexander Graf     char *url;
113c76f4952SNolan     size_t readahead_size;
11497a3ea57SMatthew Booth     bool sslverify;
115212aefaaSDaniel Henrique Barboza     int timeout;
116*a94f83d9SRichard W.M. Jones     char *cookie;
1173494d650SFam Zheng     bool accept_range;
11863f0f45fSStefan Hajnoczi     AioContext *aio_context;
119769ce76dSAlexander Graf } BDRVCURLState;
120769ce76dSAlexander Graf 
121769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
122769ce76dSAlexander Graf static void curl_multi_do(void *arg);
123838ef602SMatthew Booth static void curl_multi_read(void *arg);
124769ce76dSAlexander Graf 
125031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
126031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
127031fd1beSPeter Maydell {
128031fd1beSPeter Maydell     BDRVCURLState *s = opaque;
129031fd1beSPeter Maydell 
130031fd1beSPeter Maydell     DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
131031fd1beSPeter Maydell     if (timeout_ms == -1) {
132031fd1beSPeter Maydell         timer_del(&s->timer);
133031fd1beSPeter Maydell     } else {
134031fd1beSPeter Maydell         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
135031fd1beSPeter Maydell         timer_mod(&s->timer,
136031fd1beSPeter Maydell                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
137031fd1beSPeter Maydell     }
138031fd1beSPeter Maydell     return 0;
139031fd1beSPeter Maydell }
140031fd1beSPeter Maydell #endif
141031fd1beSPeter Maydell 
142769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
14363f0f45fSStefan Hajnoczi                         void *userp, void *sp)
144769ce76dSAlexander Graf {
14563f0f45fSStefan Hajnoczi     BDRVCURLState *s;
146838ef602SMatthew Booth     CURLState *state = NULL;
147838ef602SMatthew Booth     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
148838ef602SMatthew Booth     state->sock_fd = fd;
14963f0f45fSStefan Hajnoczi     s = state->s;
150838ef602SMatthew Booth 
151d0f2c4c6Smalc     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
152769ce76dSAlexander Graf     switch (action) {
153769ce76dSAlexander Graf         case CURL_POLL_IN:
15463f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
15563f0f45fSStefan Hajnoczi                                NULL, state);
156769ce76dSAlexander Graf             break;
157769ce76dSAlexander Graf         case CURL_POLL_OUT:
15863f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, NULL, curl_multi_do, state);
159769ce76dSAlexander Graf             break;
160769ce76dSAlexander Graf         case CURL_POLL_INOUT:
16163f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
16263f0f45fSStefan Hajnoczi                                curl_multi_do, state);
163769ce76dSAlexander Graf             break;
164769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
16563f0f45fSStefan Hajnoczi             aio_set_fd_handler(s->aio_context, fd, NULL, NULL, NULL);
166769ce76dSAlexander Graf             break;
167769ce76dSAlexander Graf     }
168769ce76dSAlexander Graf 
169769ce76dSAlexander Graf     return 0;
170769ce76dSAlexander Graf }
171769ce76dSAlexander Graf 
1723494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
173769ce76dSAlexander Graf {
1743494d650SFam Zheng     BDRVCURLState *s = opaque;
175769ce76dSAlexander Graf     size_t realsize = size * nmemb;
1763494d650SFam Zheng     const char *accept_line = "Accept-Ranges: bytes";
177769ce76dSAlexander Graf 
1783494d650SFam Zheng     if (realsize >= strlen(accept_line)
1793494d650SFam Zheng         && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
1803494d650SFam Zheng         s->accept_range = true;
1810bfcd599SBlue Swirl     }
182769ce76dSAlexander Graf 
183769ce76dSAlexander Graf     return realsize;
184769ce76dSAlexander Graf }
185769ce76dSAlexander Graf 
186769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
187769ce76dSAlexander Graf {
188769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
189769ce76dSAlexander Graf     size_t realsize = size * nmemb;
190769ce76dSAlexander Graf     int i;
191769ce76dSAlexander Graf 
1920bfcd599SBlue Swirl     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
193769ce76dSAlexander Graf 
194769ce76dSAlexander Graf     if (!s || !s->orig_buf)
19538bbc0a5SMatthew Booth         return 0;
196769ce76dSAlexander Graf 
1976d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
1986d4b9e55SFam Zheng         /* buffer full, read nothing */
1996d4b9e55SFam Zheng         return 0;
2006d4b9e55SFam Zheng     }
2016d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
202769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
203769ce76dSAlexander Graf     s->buf_off += realsize;
204769ce76dSAlexander Graf 
205769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
206769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
207769ce76dSAlexander Graf 
208769ce76dSAlexander Graf         if (!acb)
209769ce76dSAlexander Graf             continue;
210769ce76dSAlexander Graf 
211769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
21203396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
213769ce76dSAlexander Graf                                 acb->end - acb->start);
214769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
215769ce76dSAlexander Graf             qemu_aio_release(acb);
216769ce76dSAlexander Graf             s->acb[i] = NULL;
217769ce76dSAlexander Graf         }
218769ce76dSAlexander Graf     }
219769ce76dSAlexander Graf 
220769ce76dSAlexander Graf     return realsize;
221769ce76dSAlexander Graf }
222769ce76dSAlexander Graf 
223769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
224769ce76dSAlexander Graf                          CURLAIOCB *acb)
225769ce76dSAlexander Graf {
226769ce76dSAlexander Graf     int i;
227769ce76dSAlexander Graf     size_t end = start + len;
228769ce76dSAlexander Graf 
229769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
230769ce76dSAlexander Graf         CURLState *state = &s->states[i];
231769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
232769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
233769ce76dSAlexander Graf 
234769ce76dSAlexander Graf         if (!state->orig_buf)
235769ce76dSAlexander Graf             continue;
236769ce76dSAlexander Graf         if (!state->buf_off)
237769ce76dSAlexander Graf             continue;
238769ce76dSAlexander Graf 
239769ce76dSAlexander Graf         // Does the existing buffer cover our section?
240769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
241769ce76dSAlexander Graf             (start <= buf_end) &&
242769ce76dSAlexander Graf             (end >= state->buf_start) &&
243769ce76dSAlexander Graf             (end <= buf_end))
244769ce76dSAlexander Graf         {
245769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
246769ce76dSAlexander Graf 
24703396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
248769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
249769ce76dSAlexander Graf 
250769ce76dSAlexander Graf             return FIND_RET_OK;
251769ce76dSAlexander Graf         }
252769ce76dSAlexander Graf 
253769ce76dSAlexander Graf         // Wait for unfinished chunks
254b7079df4SMatthew Booth         if (state->in_use &&
255b7079df4SMatthew Booth             (start >= state->buf_start) &&
256769ce76dSAlexander Graf             (start <= buf_fend) &&
257769ce76dSAlexander Graf             (end >= state->buf_start) &&
258769ce76dSAlexander Graf             (end <= buf_fend))
259769ce76dSAlexander Graf         {
260769ce76dSAlexander Graf             int j;
261769ce76dSAlexander Graf 
262769ce76dSAlexander Graf             acb->start = start - state->buf_start;
263769ce76dSAlexander Graf             acb->end = acb->start + len;
264769ce76dSAlexander Graf 
265769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
266769ce76dSAlexander Graf                 if (!state->acb[j]) {
267769ce76dSAlexander Graf                     state->acb[j] = acb;
268769ce76dSAlexander Graf                     return FIND_RET_WAIT;
269769ce76dSAlexander Graf                 }
270769ce76dSAlexander Graf             }
271769ce76dSAlexander Graf         }
272769ce76dSAlexander Graf     }
273769ce76dSAlexander Graf 
274769ce76dSAlexander Graf     return FIND_RET_NONE;
275769ce76dSAlexander Graf }
276769ce76dSAlexander Graf 
277838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s)
278769ce76dSAlexander Graf {
279769ce76dSAlexander Graf     int msgs_in_queue;
280769ce76dSAlexander Graf 
281769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
282769ce76dSAlexander Graf      * handle again. */
2831f2cead3SMatthew Booth     for (;;) {
284769ce76dSAlexander Graf         CURLMsg *msg;
285769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
286769ce76dSAlexander Graf 
2871f2cead3SMatthew Booth         /* Quit when there are no more completions */
288769ce76dSAlexander Graf         if (!msg)
289769ce76dSAlexander Graf             break;
290769ce76dSAlexander Graf 
2911f2cead3SMatthew Booth         if (msg->msg == CURLMSG_DONE) {
292769ce76dSAlexander Graf             CURLState *state = NULL;
293f6246509SMatthew Booth             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
294f6246509SMatthew Booth                               (char **)&state);
295f785a5aeSNicholas Thomas 
296f785a5aeSNicholas Thomas             /* ACBs for successful messages get completed in curl_read_cb */
297f785a5aeSNicholas Thomas             if (msg->data.result != CURLE_OK) {
298f785a5aeSNicholas Thomas                 int i;
299f785a5aeSNicholas Thomas                 for (i = 0; i < CURL_NUM_ACB; i++) {
300f785a5aeSNicholas Thomas                     CURLAIOCB *acb = state->acb[i];
301f785a5aeSNicholas Thomas 
302f785a5aeSNicholas Thomas                     if (acb == NULL) {
303f785a5aeSNicholas Thomas                         continue;
304f785a5aeSNicholas Thomas                     }
305f785a5aeSNicholas Thomas 
306f785a5aeSNicholas Thomas                     acb->common.cb(acb->common.opaque, -EIO);
307f785a5aeSNicholas Thomas                     qemu_aio_release(acb);
308f785a5aeSNicholas Thomas                     state->acb[i] = NULL;
309f785a5aeSNicholas Thomas                 }
310f785a5aeSNicholas Thomas             }
311f785a5aeSNicholas Thomas 
312769ce76dSAlexander Graf             curl_clean_state(state);
313769ce76dSAlexander Graf             break;
314769ce76dSAlexander Graf         }
315769ce76dSAlexander Graf     }
316769ce76dSAlexander Graf }
317769ce76dSAlexander Graf 
318031fd1beSPeter Maydell static void curl_multi_do(void *arg)
319031fd1beSPeter Maydell {
320838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
321031fd1beSPeter Maydell     int running;
322031fd1beSPeter Maydell     int r;
323031fd1beSPeter Maydell 
324838ef602SMatthew Booth     if (!s->s->multi) {
325031fd1beSPeter Maydell         return;
326031fd1beSPeter Maydell     }
327031fd1beSPeter Maydell 
328031fd1beSPeter Maydell     do {
329838ef602SMatthew Booth         r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
330031fd1beSPeter Maydell     } while(r == CURLM_CALL_MULTI_PERFORM);
331031fd1beSPeter Maydell 
332838ef602SMatthew Booth }
333838ef602SMatthew Booth 
334838ef602SMatthew Booth static void curl_multi_read(void *arg)
335838ef602SMatthew Booth {
336838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
337838ef602SMatthew Booth 
338838ef602SMatthew Booth     curl_multi_do(arg);
339838ef602SMatthew Booth     curl_multi_check_completion(s->s);
340031fd1beSPeter Maydell }
341031fd1beSPeter Maydell 
342031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
343031fd1beSPeter Maydell {
344031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
345031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
346031fd1beSPeter Maydell     int running;
347031fd1beSPeter Maydell 
348031fd1beSPeter Maydell     if (!s->multi) {
349031fd1beSPeter Maydell         return;
350031fd1beSPeter Maydell     }
351031fd1beSPeter Maydell 
352031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
353031fd1beSPeter Maydell 
354838ef602SMatthew Booth     curl_multi_check_completion(s);
355031fd1beSPeter Maydell #else
356031fd1beSPeter Maydell     abort();
357031fd1beSPeter Maydell #endif
358031fd1beSPeter Maydell }
359031fd1beSPeter Maydell 
360769ce76dSAlexander Graf static CURLState *curl_init_state(BDRVCURLState *s)
361769ce76dSAlexander Graf {
362769ce76dSAlexander Graf     CURLState *state = NULL;
363769ce76dSAlexander Graf     int i, j;
364769ce76dSAlexander Graf 
365769ce76dSAlexander Graf     do {
366769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
367769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
368769ce76dSAlexander Graf                 if (s->states[i].acb[j])
369769ce76dSAlexander Graf                     continue;
370769ce76dSAlexander Graf             if (s->states[i].in_use)
371769ce76dSAlexander Graf                 continue;
372769ce76dSAlexander Graf 
373769ce76dSAlexander Graf             state = &s->states[i];
374769ce76dSAlexander Graf             state->in_use = 1;
375769ce76dSAlexander Graf             break;
376769ce76dSAlexander Graf         }
377769ce76dSAlexander Graf         if (!state) {
37863f0f45fSStefan Hajnoczi             aio_poll(state->s->aio_context, true);
379769ce76dSAlexander Graf         }
380769ce76dSAlexander Graf     } while(!state);
381769ce76dSAlexander Graf 
3829e550b32SMatthew Booth     if (!state->curl) {
383769ce76dSAlexander Graf         state->curl = curl_easy_init();
3849e550b32SMatthew Booth         if (!state->curl) {
385769ce76dSAlexander Graf             return NULL;
3869e550b32SMatthew Booth         }
387769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
38897a3ea57SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
38997a3ea57SMatthew Booth                          (long) s->sslverify);
390*a94f83d9SRichard W.M. Jones         if (s->cookie) {
391*a94f83d9SRichard W.M. Jones             curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
392*a94f83d9SRichard W.M. Jones         }
393212aefaaSDaniel Henrique Barboza         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, s->timeout);
3949e550b32SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
3959e550b32SMatthew Booth                          (void *)curl_read_cb);
396769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
397769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
398769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
399769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
400769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
401769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
402f785a5aeSNicholas Thomas         curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
403769ce76dSAlexander Graf 
404fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
405fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
406fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
4078a8f5840SStefan Hajnoczi          *
4088a8f5840SStefan Hajnoczi          * Restricting protocols is only supported from 7.19.4 upwards.
409fb6d1bbdSStefan Hajnoczi          */
4108a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
411fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
412fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
4138a8f5840SStefan Hajnoczi #endif
414fb6d1bbdSStefan Hajnoczi 
415769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
416769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
417769ce76dSAlexander Graf #endif
4189e550b32SMatthew Booth     }
419769ce76dSAlexander Graf 
420769ce76dSAlexander Graf     state->s = s;
421769ce76dSAlexander Graf 
422769ce76dSAlexander Graf     return state;
423769ce76dSAlexander Graf }
424769ce76dSAlexander Graf 
425769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
426769ce76dSAlexander Graf {
427769ce76dSAlexander Graf     if (s->s->multi)
428769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
429769ce76dSAlexander Graf     s->in_use = 0;
430769ce76dSAlexander Graf }
431769ce76dSAlexander Graf 
4328e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
4338e6d58cdSKevin Wolf                                 Error **errp)
434769ce76dSAlexander Graf {
435e3542c67SMatthew Booth     qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
4368e6d58cdSKevin Wolf }
4378e6d58cdSKevin Wolf 
43863f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs)
43963f0f45fSStefan Hajnoczi {
44063f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
44163f0f45fSStefan Hajnoczi     int i;
44263f0f45fSStefan Hajnoczi 
44363f0f45fSStefan Hajnoczi     for (i = 0; i < CURL_NUM_STATES; i++) {
44463f0f45fSStefan Hajnoczi         if (s->states[i].in_use) {
44563f0f45fSStefan Hajnoczi             curl_clean_state(&s->states[i]);
44663f0f45fSStefan Hajnoczi         }
44763f0f45fSStefan Hajnoczi         if (s->states[i].curl) {
44863f0f45fSStefan Hajnoczi             curl_easy_cleanup(s->states[i].curl);
44963f0f45fSStefan Hajnoczi             s->states[i].curl = NULL;
45063f0f45fSStefan Hajnoczi         }
45163f0f45fSStefan Hajnoczi         g_free(s->states[i].orig_buf);
45263f0f45fSStefan Hajnoczi         s->states[i].orig_buf = NULL;
45363f0f45fSStefan Hajnoczi     }
45463f0f45fSStefan Hajnoczi     if (s->multi) {
45563f0f45fSStefan Hajnoczi         curl_multi_cleanup(s->multi);
45663f0f45fSStefan Hajnoczi         s->multi = NULL;
45763f0f45fSStefan Hajnoczi     }
45863f0f45fSStefan Hajnoczi 
45963f0f45fSStefan Hajnoczi     timer_del(&s->timer);
46063f0f45fSStefan Hajnoczi }
46163f0f45fSStefan Hajnoczi 
46263f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs,
46363f0f45fSStefan Hajnoczi                                     AioContext *new_context)
46463f0f45fSStefan Hajnoczi {
46563f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
46663f0f45fSStefan Hajnoczi 
46763f0f45fSStefan Hajnoczi     aio_timer_init(new_context, &s->timer,
46863f0f45fSStefan Hajnoczi                    QEMU_CLOCK_REALTIME, SCALE_NS,
46963f0f45fSStefan Hajnoczi                    curl_multi_timeout_do, s);
47063f0f45fSStefan Hajnoczi 
47163f0f45fSStefan Hajnoczi     assert(!s->multi);
47263f0f45fSStefan Hajnoczi     s->multi = curl_multi_init();
47363f0f45fSStefan Hajnoczi     s->aio_context = new_context;
47463f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
47563f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK
47663f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
47763f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
47863f0f45fSStefan Hajnoczi #endif
47963f0f45fSStefan Hajnoczi }
48063f0f45fSStefan Hajnoczi 
4818e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
4828e6d58cdSKevin Wolf     .name = "curl",
4838e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
4848e6d58cdSKevin Wolf     .desc = {
4858e6d58cdSKevin Wolf         {
486e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_URL,
4878e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
4888e6d58cdSKevin Wolf             .help = "URL to open",
4898e6d58cdSKevin Wolf         },
4908e6d58cdSKevin Wolf         {
491e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_READAHEAD,
4928e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
4938e6d58cdSKevin Wolf             .help = "Readahead size",
4948e6d58cdSKevin Wolf         },
49597a3ea57SMatthew Booth         {
49697a3ea57SMatthew Booth             .name = CURL_BLOCK_OPT_SSLVERIFY,
49797a3ea57SMatthew Booth             .type = QEMU_OPT_BOOL,
49897a3ea57SMatthew Booth             .help = "Verify SSL certificate"
49997a3ea57SMatthew Booth         },
500212aefaaSDaniel Henrique Barboza         {
501212aefaaSDaniel Henrique Barboza             .name = CURL_BLOCK_OPT_TIMEOUT,
502212aefaaSDaniel Henrique Barboza             .type = QEMU_OPT_NUMBER,
503212aefaaSDaniel Henrique Barboza             .help = "Curl timeout"
504212aefaaSDaniel Henrique Barboza         },
505*a94f83d9SRichard W.M. Jones         {
506*a94f83d9SRichard W.M. Jones             .name = CURL_BLOCK_OPT_COOKIE,
507*a94f83d9SRichard W.M. Jones             .type = QEMU_OPT_STRING,
508*a94f83d9SRichard W.M. Jones             .help = "Pass the cookie or list of cookies with each request"
509*a94f83d9SRichard W.M. Jones         },
5108e6d58cdSKevin Wolf         { /* end of list */ }
5118e6d58cdSKevin Wolf     },
5128e6d58cdSKevin Wolf };
5138e6d58cdSKevin Wolf 
514015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
515015a1036SMax Reitz                      Error **errp)
5168e6d58cdSKevin Wolf {
5178e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
5188e6d58cdSKevin Wolf     CURLState *state = NULL;
5198e6d58cdSKevin Wolf     QemuOpts *opts;
5208e6d58cdSKevin Wolf     Error *local_err = NULL;
5218e6d58cdSKevin Wolf     const char *file;
522*a94f83d9SRichard W.M. Jones     const char *cookie;
5238e6d58cdSKevin Wolf     double d;
5248e6d58cdSKevin Wolf 
5258e6d58cdSKevin Wolf     static int inited = 0;
5268e6d58cdSKevin Wolf 
527a7cea2baSRichard W.M. Jones     if (flags & BDRV_O_RDWR) {
5282a94fee3SPaolo Bonzini         error_setg(errp, "curl block device does not support writes");
529a7cea2baSRichard W.M. Jones         return -EROFS;
530a7cea2baSRichard W.M. Jones     }
531a7cea2baSRichard W.M. Jones 
53287ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
5338e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
53484d18f06SMarkus Armbruster     if (local_err) {
5352a94fee3SPaolo Bonzini         error_propagate(errp, local_err);
5368e6d58cdSKevin Wolf         goto out_noclean;
5378e6d58cdSKevin Wolf     }
5388e6d58cdSKevin Wolf 
539e3542c67SMatthew Booth     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
540e3542c67SMatthew Booth                                           READ_AHEAD_DEFAULT);
541c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
5422a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
543c76f4952SNolan                    s->readahead_size);
544c76f4952SNolan         goto out_noclean;
545c76f4952SNolan     }
546c76f4952SNolan 
547212aefaaSDaniel Henrique Barboza     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
548212aefaaSDaniel Henrique Barboza                                      CURL_TIMEOUT_DEFAULT);
549212aefaaSDaniel Henrique Barboza 
55097a3ea57SMatthew Booth     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
55197a3ea57SMatthew Booth 
552*a94f83d9SRichard W.M. Jones     cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
553*a94f83d9SRichard W.M. Jones     s->cookie = g_strdup(cookie);
554*a94f83d9SRichard W.M. Jones 
555e3542c67SMatthew Booth     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
5568e6d58cdSKevin Wolf     if (file == NULL) {
5572a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires an 'url' option");
5588e6d58cdSKevin Wolf         goto out_noclean;
5598e6d58cdSKevin Wolf     }
5608e6d58cdSKevin Wolf 
561769ce76dSAlexander Graf     if (!inited) {
562769ce76dSAlexander Graf         curl_global_init(CURL_GLOBAL_ALL);
563769ce76dSAlexander Graf         inited = 1;
564769ce76dSAlexander Graf     }
565769ce76dSAlexander Graf 
566d0f2c4c6Smalc     DPRINTF("CURL: Opening %s\n", file);
56763f0f45fSStefan Hajnoczi     s->aio_context = bdrv_get_aio_context(bs);
5688e6d58cdSKevin Wolf     s->url = g_strdup(file);
569769ce76dSAlexander Graf     state = curl_init_state(s);
570769ce76dSAlexander Graf     if (!state)
571769ce76dSAlexander Graf         goto out_noclean;
572769ce76dSAlexander Graf 
573769ce76dSAlexander Graf     // Get file size
574769ce76dSAlexander Graf 
5753494d650SFam Zheng     s->accept_range = false;
576769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
5773494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
5783494d650SFam Zheng                      curl_header_cb);
5793494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
580769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
581769ce76dSAlexander Graf         goto out;
582769ce76dSAlexander Graf     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
583769ce76dSAlexander Graf     if (d)
584769ce76dSAlexander Graf         s->len = (size_t)d;
585769ce76dSAlexander Graf     else if(!s->len)
586769ce76dSAlexander Graf         goto out;
5873494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
5883494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
5893494d650SFam Zheng         && !s->accept_range) {
5903494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
5913494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
5923494d650SFam Zheng         goto out;
5933494d650SFam Zheng     }
5940bfcd599SBlue Swirl     DPRINTF("CURL: Size = %zd\n", s->len);
595769ce76dSAlexander Graf 
596769ce76dSAlexander Graf     curl_clean_state(state);
597769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
598769ce76dSAlexander Graf     state->curl = NULL;
599769ce76dSAlexander Graf 
60063f0f45fSStefan Hajnoczi     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
601769ce76dSAlexander Graf 
6028e6d58cdSKevin Wolf     qemu_opts_del(opts);
603769ce76dSAlexander Graf     return 0;
604769ce76dSAlexander Graf 
605769ce76dSAlexander Graf out:
606acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
607769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
608769ce76dSAlexander Graf     state->curl = NULL;
609769ce76dSAlexander Graf out_noclean:
610*a94f83d9SRichard W.M. Jones     g_free(s->cookie);
6118e6d58cdSKevin Wolf     g_free(s->url);
6128e6d58cdSKevin Wolf     qemu_opts_del(opts);
613769ce76dSAlexander Graf     return -EINVAL;
614769ce76dSAlexander Graf }
615769ce76dSAlexander Graf 
616c16b5a2cSChristoph Hellwig static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
617c16b5a2cSChristoph Hellwig {
618c16b5a2cSChristoph Hellwig     // Do we have to implement canceling? Seems to work without...
619c16b5a2cSChristoph Hellwig }
620c16b5a2cSChristoph Hellwig 
621d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = {
622c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(CURLAIOCB),
623c16b5a2cSChristoph Hellwig     .cancel             = curl_aio_cancel,
624c16b5a2cSChristoph Hellwig };
625c16b5a2cSChristoph Hellwig 
626363c3c85SNick Thomas 
627363c3c85SNick Thomas static void curl_readv_bh_cb(void *p)
628769ce76dSAlexander Graf {
629769ce76dSAlexander Graf     CURLState *state;
630b69cdef8SMatthew Booth     int running;
631769ce76dSAlexander Graf 
632363c3c85SNick Thomas     CURLAIOCB *acb = p;
633363c3c85SNick Thomas     BDRVCURLState *s = acb->common.bs->opaque;
634769ce76dSAlexander Graf 
635363c3c85SNick Thomas     qemu_bh_delete(acb->bh);
636363c3c85SNick Thomas     acb->bh = NULL;
637363c3c85SNick Thomas 
638363c3c85SNick Thomas     size_t start = acb->sector_num * SECTOR_SIZE;
639363c3c85SNick Thomas     size_t end;
640769ce76dSAlexander Graf 
641769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
642769ce76dSAlexander Graf     // we can just call the callback and be done.
643363c3c85SNick Thomas     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
644769ce76dSAlexander Graf         case FIND_RET_OK:
645769ce76dSAlexander Graf             qemu_aio_release(acb);
646769ce76dSAlexander Graf             // fall through
647769ce76dSAlexander Graf         case FIND_RET_WAIT:
648363c3c85SNick Thomas             return;
649769ce76dSAlexander Graf         default:
650769ce76dSAlexander Graf             break;
651769ce76dSAlexander Graf     }
652769ce76dSAlexander Graf 
653769ce76dSAlexander Graf     // No cache found, so let's start a new request
654769ce76dSAlexander Graf     state = curl_init_state(s);
655363c3c85SNick Thomas     if (!state) {
656363c3c85SNick Thomas         acb->common.cb(acb->common.opaque, -EIO);
657363c3c85SNick Thomas         qemu_aio_release(acb);
658363c3c85SNick Thomas         return;
659363c3c85SNick Thomas     }
660769ce76dSAlexander Graf 
661769ce76dSAlexander Graf     acb->start = 0;
662363c3c85SNick Thomas     acb->end = (acb->nb_sectors * SECTOR_SIZE);
663769ce76dSAlexander Graf 
664769ce76dSAlexander Graf     state->buf_off = 0;
6657267c094SAnthony Liguori     g_free(state->orig_buf);
666769ce76dSAlexander Graf     state->buf_start = start;
667c76f4952SNolan     state->buf_len = acb->end + s->readahead_size;
668769ce76dSAlexander Graf     end = MIN(start + state->buf_len, s->len) - 1;
6698dc7a772SKevin Wolf     state->orig_buf = g_try_malloc(state->buf_len);
6708dc7a772SKevin Wolf     if (state->buf_len && state->orig_buf == NULL) {
6718dc7a772SKevin Wolf         curl_clean_state(state);
6728dc7a772SKevin Wolf         acb->common.cb(acb->common.opaque, -ENOMEM);
6738dc7a772SKevin Wolf         qemu_aio_release(acb);
6748dc7a772SKevin Wolf         return;
6758dc7a772SKevin Wolf     }
676769ce76dSAlexander Graf     state->acb[0] = acb;
677769ce76dSAlexander Graf 
6780bfcd599SBlue Swirl     snprintf(state->range, 127, "%zd-%zd", start, end);
6790bfcd599SBlue Swirl     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
680363c3c85SNick Thomas             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
681769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
682769ce76dSAlexander Graf 
683769ce76dSAlexander Graf     curl_multi_add_handle(s->multi, state->curl);
684769ce76dSAlexander Graf 
685b69cdef8SMatthew Booth     /* Tell curl it needs to kick things off */
686b69cdef8SMatthew Booth     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
687363c3c85SNick Thomas }
688363c3c85SNick Thomas 
689363c3c85SNick Thomas static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
690363c3c85SNick Thomas         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
691363c3c85SNick Thomas         BlockDriverCompletionFunc *cb, void *opaque)
692363c3c85SNick Thomas {
693363c3c85SNick Thomas     CURLAIOCB *acb;
694363c3c85SNick Thomas 
695d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
696363c3c85SNick Thomas 
697363c3c85SNick Thomas     acb->qiov = qiov;
698363c3c85SNick Thomas     acb->sector_num = sector_num;
699363c3c85SNick Thomas     acb->nb_sectors = nb_sectors;
700363c3c85SNick Thomas 
70163f0f45fSStefan Hajnoczi     acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
702363c3c85SNick Thomas     qemu_bh_schedule(acb->bh);
703769ce76dSAlexander Graf     return &acb->common;
704769ce76dSAlexander Graf }
705769ce76dSAlexander Graf 
706769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
707769ce76dSAlexander Graf {
708769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
709769ce76dSAlexander Graf 
710d0f2c4c6Smalc     DPRINTF("CURL: Close\n");
71163f0f45fSStefan Hajnoczi     curl_detach_aio_context(bs);
712031fd1beSPeter Maydell 
713*a94f83d9SRichard W.M. Jones     g_free(s->cookie);
71445724d6dSStefan Weil     g_free(s->url);
715769ce76dSAlexander Graf }
716769ce76dSAlexander Graf 
717769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs)
718769ce76dSAlexander Graf {
719769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
720769ce76dSAlexander Graf     return s->len;
721769ce76dSAlexander Graf }
722769ce76dSAlexander Graf 
723769ce76dSAlexander Graf static BlockDriver bdrv_http = {
724769ce76dSAlexander Graf     .format_name                = "http",
725769ce76dSAlexander Graf     .protocol_name              = "http",
726769ce76dSAlexander Graf 
727769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7288e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
72966f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
730769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
731769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
732769ce76dSAlexander Graf 
733769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
73463f0f45fSStefan Hajnoczi 
73563f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
73663f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
737769ce76dSAlexander Graf };
738769ce76dSAlexander Graf 
739769ce76dSAlexander Graf static BlockDriver bdrv_https = {
740769ce76dSAlexander Graf     .format_name                = "https",
741769ce76dSAlexander Graf     .protocol_name              = "https",
742769ce76dSAlexander Graf 
743769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7448e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
74566f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
746769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
747769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
748769ce76dSAlexander Graf 
749769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
75063f0f45fSStefan Hajnoczi 
75163f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
75263f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
753769ce76dSAlexander Graf };
754769ce76dSAlexander Graf 
755769ce76dSAlexander Graf static BlockDriver bdrv_ftp = {
756769ce76dSAlexander Graf     .format_name                = "ftp",
757769ce76dSAlexander Graf     .protocol_name              = "ftp",
758769ce76dSAlexander Graf 
759769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7608e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
76166f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
762769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
763769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
764769ce76dSAlexander Graf 
765769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
76663f0f45fSStefan Hajnoczi 
76763f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
76863f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
769769ce76dSAlexander Graf };
770769ce76dSAlexander Graf 
771769ce76dSAlexander Graf static BlockDriver bdrv_ftps = {
772769ce76dSAlexander Graf     .format_name                = "ftps",
773769ce76dSAlexander Graf     .protocol_name              = "ftps",
774769ce76dSAlexander Graf 
775769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7768e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
77766f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
778769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
779769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
780769ce76dSAlexander Graf 
781769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
78263f0f45fSStefan Hajnoczi 
78363f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
78463f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
785769ce76dSAlexander Graf };
786769ce76dSAlexander Graf 
787769ce76dSAlexander Graf static BlockDriver bdrv_tftp = {
788769ce76dSAlexander Graf     .format_name                = "tftp",
789769ce76dSAlexander Graf     .protocol_name              = "tftp",
790769ce76dSAlexander Graf 
791769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
7928e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
79366f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
794769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
795769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
796769ce76dSAlexander Graf 
797769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
79863f0f45fSStefan Hajnoczi 
79963f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
80063f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
801769ce76dSAlexander Graf };
802769ce76dSAlexander Graf 
803769ce76dSAlexander Graf static void curl_block_init(void)
804769ce76dSAlexander Graf {
805769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
806769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
807769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
808769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
809769ce76dSAlexander Graf     bdrv_register(&bdrv_tftp);
810769ce76dSAlexander Graf }
811769ce76dSAlexander Graf 
812769ce76dSAlexander Graf block_init(curl_block_init);
813