xref: /openbmc/qemu/block/curl.c (revision 80c71a241ae3cd3b89527865ba730b2fa1f9e46f)
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  */
24*80c71a24SPeter Maydell #include "qemu/osdep.h"
25769ce76dSAlexander Graf #include "qemu-common.h"
26796a060bSRichard W.M. Jones #include "qemu/error-report.h"
27737e150eSPaolo Bonzini #include "block/block_int.h"
2897a3ea57SMatthew Booth #include "qapi/qmp/qbool.h"
29d49b6836SMarkus Armbruster #include "qapi/qmp/qstring.h"
30769ce76dSAlexander Graf #include <curl/curl.h>
31769ce76dSAlexander Graf 
3241c23467SRichard W.M. Jones // #define DEBUG_CURL
33769ce76dSAlexander Graf // #define DEBUG_VERBOSE
34769ce76dSAlexander Graf 
35769ce76dSAlexander Graf #ifdef DEBUG_CURL
36d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
37769ce76dSAlexander Graf #else
38d0f2c4c6Smalc #define DPRINTF(fmt, ...) do { } while (0)
39769ce76dSAlexander Graf #endif
40769ce76dSAlexander Graf 
41031fd1beSPeter Maydell #if LIBCURL_VERSION_NUM >= 0x071000
42031fd1beSPeter Maydell /* The multi interface timer callback was introduced in 7.16.0 */
43031fd1beSPeter Maydell #define NEED_CURL_TIMER_CALLBACK
449aedd5a5SMatthew Booth #define HAVE_SOCKET_ACTION
459aedd5a5SMatthew Booth #endif
469aedd5a5SMatthew Booth 
479aedd5a5SMatthew Booth #ifndef HAVE_SOCKET_ACTION
489aedd5a5SMatthew Booth /* If curl_multi_socket_action isn't available, define it statically here in
499aedd5a5SMatthew Booth  * terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
509aedd5a5SMatthew Booth  * less efficient but still safe. */
519aedd5a5SMatthew Booth static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
529aedd5a5SMatthew Booth                                             curl_socket_t sockfd,
539aedd5a5SMatthew Booth                                             int ev_bitmask,
549aedd5a5SMatthew Booth                                             int *running_handles)
559aedd5a5SMatthew Booth {
569aedd5a5SMatthew Booth     return curl_multi_socket(multi_handle, sockfd, running_handles);
579aedd5a5SMatthew Booth }
589aedd5a5SMatthew Booth #define curl_multi_socket_action __curl_multi_socket_action
59031fd1beSPeter Maydell #endif
60031fd1beSPeter Maydell 
61fb6d1bbdSStefan Hajnoczi #define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
62fb6d1bbdSStefan Hajnoczi                    CURLPROTO_FTP | CURLPROTO_FTPS | \
63fb6d1bbdSStefan Hajnoczi                    CURLPROTO_TFTP)
64fb6d1bbdSStefan Hajnoczi 
65769ce76dSAlexander Graf #define CURL_NUM_STATES 8
66769ce76dSAlexander Graf #define CURL_NUM_ACB    8
67769ce76dSAlexander Graf #define SECTOR_SIZE     512
68e3542c67SMatthew Booth #define READ_AHEAD_DEFAULT (256 * 1024)
69212aefaaSDaniel Henrique Barboza #define CURL_TIMEOUT_DEFAULT 5
70f76faedaSRichard W.M. Jones #define CURL_TIMEOUT_MAX 10000
71769ce76dSAlexander Graf 
72769ce76dSAlexander Graf #define FIND_RET_NONE   0
73769ce76dSAlexander Graf #define FIND_RET_OK     1
74769ce76dSAlexander Graf #define FIND_RET_WAIT   2
75769ce76dSAlexander Graf 
76e3542c67SMatthew Booth #define CURL_BLOCK_OPT_URL       "url"
77e3542c67SMatthew Booth #define CURL_BLOCK_OPT_READAHEAD "readahead"
7897a3ea57SMatthew Booth #define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
79212aefaaSDaniel Henrique Barboza #define CURL_BLOCK_OPT_TIMEOUT "timeout"
80a94f83d9SRichard W.M. Jones #define CURL_BLOCK_OPT_COOKIE    "cookie"
81e3542c67SMatthew Booth 
82769ce76dSAlexander Graf struct BDRVCURLState;
83769ce76dSAlexander Graf 
84769ce76dSAlexander Graf typedef struct CURLAIOCB {
857c84b1b8SMarkus Armbruster     BlockAIOCB common;
86363c3c85SNick Thomas     QEMUBH *bh;
87769ce76dSAlexander Graf     QEMUIOVector *qiov;
88363c3c85SNick Thomas 
89363c3c85SNick Thomas     int64_t sector_num;
90363c3c85SNick Thomas     int nb_sectors;
91363c3c85SNick Thomas 
92769ce76dSAlexander Graf     size_t start;
93769ce76dSAlexander Graf     size_t end;
94769ce76dSAlexander Graf } CURLAIOCB;
95769ce76dSAlexander Graf 
96769ce76dSAlexander Graf typedef struct CURLState
97769ce76dSAlexander Graf {
98769ce76dSAlexander Graf     struct BDRVCURLState *s;
99769ce76dSAlexander Graf     CURLAIOCB *acb[CURL_NUM_ACB];
100769ce76dSAlexander Graf     CURL *curl;
101838ef602SMatthew Booth     curl_socket_t sock_fd;
102769ce76dSAlexander Graf     char *orig_buf;
103769ce76dSAlexander Graf     size_t buf_start;
104769ce76dSAlexander Graf     size_t buf_off;
105769ce76dSAlexander Graf     size_t buf_len;
106769ce76dSAlexander Graf     char range[128];
107769ce76dSAlexander Graf     char errmsg[CURL_ERROR_SIZE];
108769ce76dSAlexander Graf     char in_use;
109769ce76dSAlexander Graf } CURLState;
110769ce76dSAlexander Graf 
111769ce76dSAlexander Graf typedef struct BDRVCURLState {
112769ce76dSAlexander Graf     CURLM *multi;
113031fd1beSPeter Maydell     QEMUTimer timer;
114769ce76dSAlexander Graf     size_t len;
115769ce76dSAlexander Graf     CURLState states[CURL_NUM_STATES];
116769ce76dSAlexander Graf     char *url;
117c76f4952SNolan     size_t readahead_size;
11897a3ea57SMatthew Booth     bool sslverify;
119f76faedaSRichard W.M. Jones     uint64_t timeout;
120a94f83d9SRichard W.M. Jones     char *cookie;
1213494d650SFam Zheng     bool accept_range;
12263f0f45fSStefan Hajnoczi     AioContext *aio_context;
123769ce76dSAlexander Graf } BDRVCURLState;
124769ce76dSAlexander Graf 
125769ce76dSAlexander Graf static void curl_clean_state(CURLState *s);
126769ce76dSAlexander Graf static void curl_multi_do(void *arg);
127838ef602SMatthew Booth static void curl_multi_read(void *arg);
128769ce76dSAlexander Graf 
129031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
130031fd1beSPeter Maydell static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
131031fd1beSPeter Maydell {
132031fd1beSPeter Maydell     BDRVCURLState *s = opaque;
133031fd1beSPeter Maydell 
134031fd1beSPeter Maydell     DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
135031fd1beSPeter Maydell     if (timeout_ms == -1) {
136031fd1beSPeter Maydell         timer_del(&s->timer);
137031fd1beSPeter Maydell     } else {
138031fd1beSPeter Maydell         int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
139031fd1beSPeter Maydell         timer_mod(&s->timer,
140031fd1beSPeter Maydell                   qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
141031fd1beSPeter Maydell     }
142031fd1beSPeter Maydell     return 0;
143031fd1beSPeter Maydell }
144031fd1beSPeter Maydell #endif
145031fd1beSPeter Maydell 
146769ce76dSAlexander Graf static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
14763f0f45fSStefan Hajnoczi                         void *userp, void *sp)
148769ce76dSAlexander Graf {
14963f0f45fSStefan Hajnoczi     BDRVCURLState *s;
150838ef602SMatthew Booth     CURLState *state = NULL;
151838ef602SMatthew Booth     curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
152838ef602SMatthew Booth     state->sock_fd = fd;
15363f0f45fSStefan Hajnoczi     s = state->s;
154838ef602SMatthew Booth 
155d0f2c4c6Smalc     DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
156769ce76dSAlexander Graf     switch (action) {
157769ce76dSAlexander Graf         case CURL_POLL_IN:
158dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
159dca21ef2SFam Zheng                                curl_multi_read, NULL, state);
160769ce76dSAlexander Graf             break;
161769ce76dSAlexander Graf         case CURL_POLL_OUT:
162dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
163dca21ef2SFam Zheng                                NULL, curl_multi_do, state);
164769ce76dSAlexander Graf             break;
165769ce76dSAlexander Graf         case CURL_POLL_INOUT:
166dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
167dca21ef2SFam Zheng                                curl_multi_read, curl_multi_do, state);
168769ce76dSAlexander Graf             break;
169769ce76dSAlexander Graf         case CURL_POLL_REMOVE:
170dca21ef2SFam Zheng             aio_set_fd_handler(s->aio_context, fd, false,
171dca21ef2SFam Zheng                                NULL, NULL, NULL);
172769ce76dSAlexander Graf             break;
173769ce76dSAlexander Graf     }
174769ce76dSAlexander Graf 
175769ce76dSAlexander Graf     return 0;
176769ce76dSAlexander Graf }
177769ce76dSAlexander Graf 
1783494d650SFam Zheng static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
179769ce76dSAlexander Graf {
1803494d650SFam Zheng     BDRVCURLState *s = opaque;
181769ce76dSAlexander Graf     size_t realsize = size * nmemb;
1823494d650SFam Zheng     const char *accept_line = "Accept-Ranges: bytes";
183769ce76dSAlexander Graf 
1843494d650SFam Zheng     if (realsize >= strlen(accept_line)
1853494d650SFam Zheng         && strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
1863494d650SFam Zheng         s->accept_range = true;
1870bfcd599SBlue Swirl     }
188769ce76dSAlexander Graf 
189769ce76dSAlexander Graf     return realsize;
190769ce76dSAlexander Graf }
191769ce76dSAlexander Graf 
192769ce76dSAlexander Graf static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
193769ce76dSAlexander Graf {
194769ce76dSAlexander Graf     CURLState *s = ((CURLState*)opaque);
195769ce76dSAlexander Graf     size_t realsize = size * nmemb;
196769ce76dSAlexander Graf     int i;
197769ce76dSAlexander Graf 
1980bfcd599SBlue Swirl     DPRINTF("CURL: Just reading %zd bytes\n", realsize);
199769ce76dSAlexander Graf 
200769ce76dSAlexander Graf     if (!s || !s->orig_buf)
20138bbc0a5SMatthew Booth         return 0;
202769ce76dSAlexander Graf 
2036d4b9e55SFam Zheng     if (s->buf_off >= s->buf_len) {
2046d4b9e55SFam Zheng         /* buffer full, read nothing */
2056d4b9e55SFam Zheng         return 0;
2066d4b9e55SFam Zheng     }
2076d4b9e55SFam Zheng     realsize = MIN(realsize, s->buf_len - s->buf_off);
208769ce76dSAlexander Graf     memcpy(s->orig_buf + s->buf_off, ptr, realsize);
209769ce76dSAlexander Graf     s->buf_off += realsize;
210769ce76dSAlexander Graf 
211769ce76dSAlexander Graf     for(i=0; i<CURL_NUM_ACB; i++) {
212769ce76dSAlexander Graf         CURLAIOCB *acb = s->acb[i];
213769ce76dSAlexander Graf 
214769ce76dSAlexander Graf         if (!acb)
215769ce76dSAlexander Graf             continue;
216769ce76dSAlexander Graf 
217769ce76dSAlexander Graf         if ((s->buf_off >= acb->end)) {
21803396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
219769ce76dSAlexander Graf                                 acb->end - acb->start);
220769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
2218007429aSFam Zheng             qemu_aio_unref(acb);
222769ce76dSAlexander Graf             s->acb[i] = NULL;
223769ce76dSAlexander Graf         }
224769ce76dSAlexander Graf     }
225769ce76dSAlexander Graf 
226769ce76dSAlexander Graf     return realsize;
227769ce76dSAlexander Graf }
228769ce76dSAlexander Graf 
229769ce76dSAlexander Graf static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
230769ce76dSAlexander Graf                          CURLAIOCB *acb)
231769ce76dSAlexander Graf {
232769ce76dSAlexander Graf     int i;
233769ce76dSAlexander Graf     size_t end = start + len;
234769ce76dSAlexander Graf 
235769ce76dSAlexander Graf     for (i=0; i<CURL_NUM_STATES; i++) {
236769ce76dSAlexander Graf         CURLState *state = &s->states[i];
237769ce76dSAlexander Graf         size_t buf_end = (state->buf_start + state->buf_off);
238769ce76dSAlexander Graf         size_t buf_fend = (state->buf_start + state->buf_len);
239769ce76dSAlexander Graf 
240769ce76dSAlexander Graf         if (!state->orig_buf)
241769ce76dSAlexander Graf             continue;
242769ce76dSAlexander Graf         if (!state->buf_off)
243769ce76dSAlexander Graf             continue;
244769ce76dSAlexander Graf 
245769ce76dSAlexander Graf         // Does the existing buffer cover our section?
246769ce76dSAlexander Graf         if ((start >= state->buf_start) &&
247769ce76dSAlexander Graf             (start <= buf_end) &&
248769ce76dSAlexander Graf             (end >= state->buf_start) &&
249769ce76dSAlexander Graf             (end <= buf_end))
250769ce76dSAlexander Graf         {
251769ce76dSAlexander Graf             char *buf = state->orig_buf + (start - state->buf_start);
252769ce76dSAlexander Graf 
25303396148SMichael Tokarev             qemu_iovec_from_buf(acb->qiov, 0, buf, len);
254769ce76dSAlexander Graf             acb->common.cb(acb->common.opaque, 0);
255769ce76dSAlexander Graf 
256769ce76dSAlexander Graf             return FIND_RET_OK;
257769ce76dSAlexander Graf         }
258769ce76dSAlexander Graf 
259769ce76dSAlexander Graf         // Wait for unfinished chunks
260b7079df4SMatthew Booth         if (state->in_use &&
261b7079df4SMatthew Booth             (start >= state->buf_start) &&
262769ce76dSAlexander Graf             (start <= buf_fend) &&
263769ce76dSAlexander Graf             (end >= state->buf_start) &&
264769ce76dSAlexander Graf             (end <= buf_fend))
265769ce76dSAlexander Graf         {
266769ce76dSAlexander Graf             int j;
267769ce76dSAlexander Graf 
268769ce76dSAlexander Graf             acb->start = start - state->buf_start;
269769ce76dSAlexander Graf             acb->end = acb->start + len;
270769ce76dSAlexander Graf 
271769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++) {
272769ce76dSAlexander Graf                 if (!state->acb[j]) {
273769ce76dSAlexander Graf                     state->acb[j] = acb;
274769ce76dSAlexander Graf                     return FIND_RET_WAIT;
275769ce76dSAlexander Graf                 }
276769ce76dSAlexander Graf             }
277769ce76dSAlexander Graf         }
278769ce76dSAlexander Graf     }
279769ce76dSAlexander Graf 
280769ce76dSAlexander Graf     return FIND_RET_NONE;
281769ce76dSAlexander Graf }
282769ce76dSAlexander Graf 
283838ef602SMatthew Booth static void curl_multi_check_completion(BDRVCURLState *s)
284769ce76dSAlexander Graf {
285769ce76dSAlexander Graf     int msgs_in_queue;
286769ce76dSAlexander Graf 
287769ce76dSAlexander Graf     /* Try to find done transfers, so we can free the easy
288769ce76dSAlexander Graf      * handle again. */
2891f2cead3SMatthew Booth     for (;;) {
290769ce76dSAlexander Graf         CURLMsg *msg;
291769ce76dSAlexander Graf         msg = curl_multi_info_read(s->multi, &msgs_in_queue);
292769ce76dSAlexander Graf 
2931f2cead3SMatthew Booth         /* Quit when there are no more completions */
294769ce76dSAlexander Graf         if (!msg)
295769ce76dSAlexander Graf             break;
296769ce76dSAlexander Graf 
2971f2cead3SMatthew Booth         if (msg->msg == CURLMSG_DONE) {
298769ce76dSAlexander Graf             CURLState *state = NULL;
299f6246509SMatthew Booth             curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
300f6246509SMatthew Booth                               (char **)&state);
301f785a5aeSNicholas Thomas 
302f785a5aeSNicholas Thomas             /* ACBs for successful messages get completed in curl_read_cb */
303f785a5aeSNicholas Thomas             if (msg->data.result != CURLE_OK) {
304f785a5aeSNicholas Thomas                 int i;
305796a060bSRichard W.M. Jones                 static int errcount = 100;
306796a060bSRichard W.M. Jones 
307796a060bSRichard W.M. Jones                 /* Don't lose the original error message from curl, since
308796a060bSRichard W.M. Jones                  * it contains extra data.
309796a060bSRichard W.M. Jones                  */
310796a060bSRichard W.M. Jones                 if (errcount > 0) {
311796a060bSRichard W.M. Jones                     error_report("curl: %s", state->errmsg);
312796a060bSRichard W.M. Jones                     if (--errcount == 0) {
313796a060bSRichard W.M. Jones                         error_report("curl: further errors suppressed");
314796a060bSRichard W.M. Jones                     }
315796a060bSRichard W.M. Jones                 }
316796a060bSRichard W.M. Jones 
317f785a5aeSNicholas Thomas                 for (i = 0; i < CURL_NUM_ACB; i++) {
318f785a5aeSNicholas Thomas                     CURLAIOCB *acb = state->acb[i];
319f785a5aeSNicholas Thomas 
320f785a5aeSNicholas Thomas                     if (acb == NULL) {
321f785a5aeSNicholas Thomas                         continue;
322f785a5aeSNicholas Thomas                     }
323f785a5aeSNicholas Thomas 
324796a060bSRichard W.M. Jones                     acb->common.cb(acb->common.opaque, -EPROTO);
3258007429aSFam Zheng                     qemu_aio_unref(acb);
326f785a5aeSNicholas Thomas                     state->acb[i] = NULL;
327f785a5aeSNicholas Thomas                 }
328f785a5aeSNicholas Thomas             }
329f785a5aeSNicholas Thomas 
330769ce76dSAlexander Graf             curl_clean_state(state);
331769ce76dSAlexander Graf             break;
332769ce76dSAlexander Graf         }
333769ce76dSAlexander Graf     }
334769ce76dSAlexander Graf }
335769ce76dSAlexander Graf 
336031fd1beSPeter Maydell static void curl_multi_do(void *arg)
337031fd1beSPeter Maydell {
338838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
339031fd1beSPeter Maydell     int running;
340031fd1beSPeter Maydell     int r;
341031fd1beSPeter Maydell 
342838ef602SMatthew Booth     if (!s->s->multi) {
343031fd1beSPeter Maydell         return;
344031fd1beSPeter Maydell     }
345031fd1beSPeter Maydell 
346031fd1beSPeter Maydell     do {
347838ef602SMatthew Booth         r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
348031fd1beSPeter Maydell     } while(r == CURLM_CALL_MULTI_PERFORM);
349031fd1beSPeter Maydell 
350838ef602SMatthew Booth }
351838ef602SMatthew Booth 
352838ef602SMatthew Booth static void curl_multi_read(void *arg)
353838ef602SMatthew Booth {
354838ef602SMatthew Booth     CURLState *s = (CURLState *)arg;
355838ef602SMatthew Booth 
356838ef602SMatthew Booth     curl_multi_do(arg);
357838ef602SMatthew Booth     curl_multi_check_completion(s->s);
358031fd1beSPeter Maydell }
359031fd1beSPeter Maydell 
360031fd1beSPeter Maydell static void curl_multi_timeout_do(void *arg)
361031fd1beSPeter Maydell {
362031fd1beSPeter Maydell #ifdef NEED_CURL_TIMER_CALLBACK
363031fd1beSPeter Maydell     BDRVCURLState *s = (BDRVCURLState *)arg;
364031fd1beSPeter Maydell     int running;
365031fd1beSPeter Maydell 
366031fd1beSPeter Maydell     if (!s->multi) {
367031fd1beSPeter Maydell         return;
368031fd1beSPeter Maydell     }
369031fd1beSPeter Maydell 
370031fd1beSPeter Maydell     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
371031fd1beSPeter Maydell 
372838ef602SMatthew Booth     curl_multi_check_completion(s);
373031fd1beSPeter Maydell #else
374031fd1beSPeter Maydell     abort();
375031fd1beSPeter Maydell #endif
376031fd1beSPeter Maydell }
377031fd1beSPeter Maydell 
378a2f468e4SRichard W.M. Jones static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
379769ce76dSAlexander Graf {
380769ce76dSAlexander Graf     CURLState *state = NULL;
381769ce76dSAlexander Graf     int i, j;
382769ce76dSAlexander Graf 
383769ce76dSAlexander Graf     do {
384769ce76dSAlexander Graf         for (i=0; i<CURL_NUM_STATES; i++) {
385769ce76dSAlexander Graf             for (j=0; j<CURL_NUM_ACB; j++)
386769ce76dSAlexander Graf                 if (s->states[i].acb[j])
387769ce76dSAlexander Graf                     continue;
388769ce76dSAlexander Graf             if (s->states[i].in_use)
389769ce76dSAlexander Graf                 continue;
390769ce76dSAlexander Graf 
391769ce76dSAlexander Graf             state = &s->states[i];
392769ce76dSAlexander Graf             state->in_use = 1;
393769ce76dSAlexander Graf             break;
394769ce76dSAlexander Graf         }
395769ce76dSAlexander Graf         if (!state) {
396a2f468e4SRichard W.M. Jones             aio_poll(bdrv_get_aio_context(bs), true);
397769ce76dSAlexander Graf         }
398769ce76dSAlexander Graf     } while(!state);
399769ce76dSAlexander Graf 
4009e550b32SMatthew Booth     if (!state->curl) {
401769ce76dSAlexander Graf         state->curl = curl_easy_init();
4029e550b32SMatthew Booth         if (!state->curl) {
403769ce76dSAlexander Graf             return NULL;
4049e550b32SMatthew Booth         }
405769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
40697a3ea57SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
40797a3ea57SMatthew Booth                          (long) s->sslverify);
408a94f83d9SRichard W.M. Jones         if (s->cookie) {
409a94f83d9SRichard W.M. Jones             curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
410a94f83d9SRichard W.M. Jones         }
411f76faedaSRichard W.M. Jones         curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
4129e550b32SMatthew Booth         curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
4139e550b32SMatthew Booth                          (void *)curl_read_cb);
414769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
415769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
416769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
417769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
418769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
419769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
420f785a5aeSNicholas Thomas         curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
421769ce76dSAlexander Graf 
422fb6d1bbdSStefan Hajnoczi         /* Restrict supported protocols to avoid security issues in the more
423fb6d1bbdSStefan Hajnoczi          * obscure protocols.  For example, do not allow POP3/SMTP/IMAP see
424fb6d1bbdSStefan Hajnoczi          * CVE-2013-0249.
4258a8f5840SStefan Hajnoczi          *
4268a8f5840SStefan Hajnoczi          * Restricting protocols is only supported from 7.19.4 upwards.
427fb6d1bbdSStefan Hajnoczi          */
4288a8f5840SStefan Hajnoczi #if LIBCURL_VERSION_NUM >= 0x071304
429fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
430fb6d1bbdSStefan Hajnoczi         curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
4318a8f5840SStefan Hajnoczi #endif
432fb6d1bbdSStefan Hajnoczi 
433769ce76dSAlexander Graf #ifdef DEBUG_VERBOSE
434769ce76dSAlexander Graf         curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
435769ce76dSAlexander Graf #endif
4369e550b32SMatthew Booth     }
437769ce76dSAlexander Graf 
438769ce76dSAlexander Graf     state->s = s;
439769ce76dSAlexander Graf 
440769ce76dSAlexander Graf     return state;
441769ce76dSAlexander Graf }
442769ce76dSAlexander Graf 
443769ce76dSAlexander Graf static void curl_clean_state(CURLState *s)
444769ce76dSAlexander Graf {
445769ce76dSAlexander Graf     if (s->s->multi)
446769ce76dSAlexander Graf         curl_multi_remove_handle(s->s->multi, s->curl);
447769ce76dSAlexander Graf     s->in_use = 0;
448769ce76dSAlexander Graf }
449769ce76dSAlexander Graf 
4508e6d58cdSKevin Wolf static void curl_parse_filename(const char *filename, QDict *options,
4518e6d58cdSKevin Wolf                                 Error **errp)
452769ce76dSAlexander Graf {
453e3542c67SMatthew Booth     qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
4548e6d58cdSKevin Wolf }
4558e6d58cdSKevin Wolf 
45663f0f45fSStefan Hajnoczi static void curl_detach_aio_context(BlockDriverState *bs)
45763f0f45fSStefan Hajnoczi {
45863f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
45963f0f45fSStefan Hajnoczi     int i;
46063f0f45fSStefan Hajnoczi 
46163f0f45fSStefan Hajnoczi     for (i = 0; i < CURL_NUM_STATES; i++) {
46263f0f45fSStefan Hajnoczi         if (s->states[i].in_use) {
46363f0f45fSStefan Hajnoczi             curl_clean_state(&s->states[i]);
46463f0f45fSStefan Hajnoczi         }
46563f0f45fSStefan Hajnoczi         if (s->states[i].curl) {
46663f0f45fSStefan Hajnoczi             curl_easy_cleanup(s->states[i].curl);
46763f0f45fSStefan Hajnoczi             s->states[i].curl = NULL;
46863f0f45fSStefan Hajnoczi         }
46963f0f45fSStefan Hajnoczi         g_free(s->states[i].orig_buf);
47063f0f45fSStefan Hajnoczi         s->states[i].orig_buf = NULL;
47163f0f45fSStefan Hajnoczi     }
47263f0f45fSStefan Hajnoczi     if (s->multi) {
47363f0f45fSStefan Hajnoczi         curl_multi_cleanup(s->multi);
47463f0f45fSStefan Hajnoczi         s->multi = NULL;
47563f0f45fSStefan Hajnoczi     }
47663f0f45fSStefan Hajnoczi 
47763f0f45fSStefan Hajnoczi     timer_del(&s->timer);
47863f0f45fSStefan Hajnoczi }
47963f0f45fSStefan Hajnoczi 
48063f0f45fSStefan Hajnoczi static void curl_attach_aio_context(BlockDriverState *bs,
48163f0f45fSStefan Hajnoczi                                     AioContext *new_context)
48263f0f45fSStefan Hajnoczi {
48363f0f45fSStefan Hajnoczi     BDRVCURLState *s = bs->opaque;
48463f0f45fSStefan Hajnoczi 
48563f0f45fSStefan Hajnoczi     aio_timer_init(new_context, &s->timer,
48663f0f45fSStefan Hajnoczi                    QEMU_CLOCK_REALTIME, SCALE_NS,
48763f0f45fSStefan Hajnoczi                    curl_multi_timeout_do, s);
48863f0f45fSStefan Hajnoczi 
48963f0f45fSStefan Hajnoczi     assert(!s->multi);
49063f0f45fSStefan Hajnoczi     s->multi = curl_multi_init();
49163f0f45fSStefan Hajnoczi     s->aio_context = new_context;
49263f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
49363f0f45fSStefan Hajnoczi #ifdef NEED_CURL_TIMER_CALLBACK
49463f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
49563f0f45fSStefan Hajnoczi     curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
49663f0f45fSStefan Hajnoczi #endif
49763f0f45fSStefan Hajnoczi }
49863f0f45fSStefan Hajnoczi 
4998e6d58cdSKevin Wolf static QemuOptsList runtime_opts = {
5008e6d58cdSKevin Wolf     .name = "curl",
5018e6d58cdSKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
5028e6d58cdSKevin Wolf     .desc = {
5038e6d58cdSKevin Wolf         {
504e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_URL,
5058e6d58cdSKevin Wolf             .type = QEMU_OPT_STRING,
5068e6d58cdSKevin Wolf             .help = "URL to open",
5078e6d58cdSKevin Wolf         },
5088e6d58cdSKevin Wolf         {
509e3542c67SMatthew Booth             .name = CURL_BLOCK_OPT_READAHEAD,
5108e6d58cdSKevin Wolf             .type = QEMU_OPT_SIZE,
5118e6d58cdSKevin Wolf             .help = "Readahead size",
5128e6d58cdSKevin Wolf         },
51397a3ea57SMatthew Booth         {
51497a3ea57SMatthew Booth             .name = CURL_BLOCK_OPT_SSLVERIFY,
51597a3ea57SMatthew Booth             .type = QEMU_OPT_BOOL,
51697a3ea57SMatthew Booth             .help = "Verify SSL certificate"
51797a3ea57SMatthew Booth         },
518212aefaaSDaniel Henrique Barboza         {
519212aefaaSDaniel Henrique Barboza             .name = CURL_BLOCK_OPT_TIMEOUT,
520212aefaaSDaniel Henrique Barboza             .type = QEMU_OPT_NUMBER,
521212aefaaSDaniel Henrique Barboza             .help = "Curl timeout"
522212aefaaSDaniel Henrique Barboza         },
523a94f83d9SRichard W.M. Jones         {
524a94f83d9SRichard W.M. Jones             .name = CURL_BLOCK_OPT_COOKIE,
525a94f83d9SRichard W.M. Jones             .type = QEMU_OPT_STRING,
526a94f83d9SRichard W.M. Jones             .help = "Pass the cookie or list of cookies with each request"
527a94f83d9SRichard W.M. Jones         },
5288e6d58cdSKevin Wolf         { /* end of list */ }
5298e6d58cdSKevin Wolf     },
5308e6d58cdSKevin Wolf };
5318e6d58cdSKevin Wolf 
532015a1036SMax Reitz static int curl_open(BlockDriverState *bs, QDict *options, int flags,
533015a1036SMax Reitz                      Error **errp)
5348e6d58cdSKevin Wolf {
5358e6d58cdSKevin Wolf     BDRVCURLState *s = bs->opaque;
5368e6d58cdSKevin Wolf     CURLState *state = NULL;
5378e6d58cdSKevin Wolf     QemuOpts *opts;
5388e6d58cdSKevin Wolf     Error *local_err = NULL;
5398e6d58cdSKevin Wolf     const char *file;
540a94f83d9SRichard W.M. Jones     const char *cookie;
5418e6d58cdSKevin Wolf     double d;
5428e6d58cdSKevin Wolf 
5438e6d58cdSKevin Wolf     static int inited = 0;
5448e6d58cdSKevin Wolf 
545a7cea2baSRichard W.M. Jones     if (flags & BDRV_O_RDWR) {
5462a94fee3SPaolo Bonzini         error_setg(errp, "curl block device does not support writes");
547a7cea2baSRichard W.M. Jones         return -EROFS;
548a7cea2baSRichard W.M. Jones     }
549a7cea2baSRichard W.M. Jones 
55087ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
5518e6d58cdSKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
55284d18f06SMarkus Armbruster     if (local_err) {
5532a94fee3SPaolo Bonzini         error_propagate(errp, local_err);
5548e6d58cdSKevin Wolf         goto out_noclean;
5558e6d58cdSKevin Wolf     }
5568e6d58cdSKevin Wolf 
557e3542c67SMatthew Booth     s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
558e3542c67SMatthew Booth                                           READ_AHEAD_DEFAULT);
559c76f4952SNolan     if ((s->readahead_size & 0x1ff) != 0) {
5602a94fee3SPaolo Bonzini         error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
561c76f4952SNolan                    s->readahead_size);
562c76f4952SNolan         goto out_noclean;
563c76f4952SNolan     }
564c76f4952SNolan 
565212aefaaSDaniel Henrique Barboza     s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
566212aefaaSDaniel Henrique Barboza                                      CURL_TIMEOUT_DEFAULT);
567f76faedaSRichard W.M. Jones     if (s->timeout > CURL_TIMEOUT_MAX) {
568f76faedaSRichard W.M. Jones         error_setg(errp, "timeout parameter is too large or negative");
569f76faedaSRichard W.M. Jones         goto out_noclean;
570f76faedaSRichard W.M. Jones     }
571212aefaaSDaniel Henrique Barboza 
57297a3ea57SMatthew Booth     s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
57397a3ea57SMatthew Booth 
574a94f83d9SRichard W.M. Jones     cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
575a94f83d9SRichard W.M. Jones     s->cookie = g_strdup(cookie);
576a94f83d9SRichard W.M. Jones 
577e3542c67SMatthew Booth     file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
5788e6d58cdSKevin Wolf     if (file == NULL) {
5792a94fee3SPaolo Bonzini         error_setg(errp, "curl block driver requires an 'url' option");
5808e6d58cdSKevin Wolf         goto out_noclean;
5818e6d58cdSKevin Wolf     }
5828e6d58cdSKevin Wolf 
583769ce76dSAlexander Graf     if (!inited) {
584769ce76dSAlexander Graf         curl_global_init(CURL_GLOBAL_ALL);
585769ce76dSAlexander Graf         inited = 1;
586769ce76dSAlexander Graf     }
587769ce76dSAlexander Graf 
588d0f2c4c6Smalc     DPRINTF("CURL: Opening %s\n", file);
58963f0f45fSStefan Hajnoczi     s->aio_context = bdrv_get_aio_context(bs);
5908e6d58cdSKevin Wolf     s->url = g_strdup(file);
591a2f468e4SRichard W.M. Jones     state = curl_init_state(bs, s);
592769ce76dSAlexander Graf     if (!state)
593769ce76dSAlexander Graf         goto out_noclean;
594769ce76dSAlexander Graf 
595769ce76dSAlexander Graf     // Get file size
596769ce76dSAlexander Graf 
5973494d650SFam Zheng     s->accept_range = false;
598769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
5993494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
6003494d650SFam Zheng                      curl_header_cb);
6013494d650SFam Zheng     curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
602769ce76dSAlexander Graf     if (curl_easy_perform(state->curl))
603769ce76dSAlexander Graf         goto out;
604769ce76dSAlexander Graf     curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
605769ce76dSAlexander Graf     if (d)
606769ce76dSAlexander Graf         s->len = (size_t)d;
607769ce76dSAlexander Graf     else if(!s->len)
608769ce76dSAlexander Graf         goto out;
6093494d650SFam Zheng     if ((!strncasecmp(s->url, "http://", strlen("http://"))
6103494d650SFam Zheng         || !strncasecmp(s->url, "https://", strlen("https://")))
6113494d650SFam Zheng         && !s->accept_range) {
6123494d650SFam Zheng         pstrcpy(state->errmsg, CURL_ERROR_SIZE,
6133494d650SFam Zheng                 "Server does not support 'range' (byte ranges).");
6143494d650SFam Zheng         goto out;
6153494d650SFam Zheng     }
6160bfcd599SBlue Swirl     DPRINTF("CURL: Size = %zd\n", s->len);
617769ce76dSAlexander Graf 
618769ce76dSAlexander Graf     curl_clean_state(state);
619769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
620769ce76dSAlexander Graf     state->curl = NULL;
621769ce76dSAlexander Graf 
62263f0f45fSStefan Hajnoczi     curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
623769ce76dSAlexander Graf 
6248e6d58cdSKevin Wolf     qemu_opts_del(opts);
625769ce76dSAlexander Graf     return 0;
626769ce76dSAlexander Graf 
627769ce76dSAlexander Graf out:
628acd7fdc6SMaria Kustova     error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
629769ce76dSAlexander Graf     curl_easy_cleanup(state->curl);
630769ce76dSAlexander Graf     state->curl = NULL;
631769ce76dSAlexander Graf out_noclean:
632a94f83d9SRichard W.M. Jones     g_free(s->cookie);
6338e6d58cdSKevin Wolf     g_free(s->url);
6348e6d58cdSKevin Wolf     qemu_opts_del(opts);
635769ce76dSAlexander Graf     return -EINVAL;
636769ce76dSAlexander Graf }
637769ce76dSAlexander Graf 
638d7331bedSStefan Hajnoczi static const AIOCBInfo curl_aiocb_info = {
639c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(CURLAIOCB),
640c16b5a2cSChristoph Hellwig };
641c16b5a2cSChristoph Hellwig 
642363c3c85SNick Thomas 
643363c3c85SNick Thomas static void curl_readv_bh_cb(void *p)
644769ce76dSAlexander Graf {
645769ce76dSAlexander Graf     CURLState *state;
646b69cdef8SMatthew Booth     int running;
647769ce76dSAlexander Graf 
648363c3c85SNick Thomas     CURLAIOCB *acb = p;
649363c3c85SNick Thomas     BDRVCURLState *s = acb->common.bs->opaque;
650769ce76dSAlexander Graf 
651363c3c85SNick Thomas     qemu_bh_delete(acb->bh);
652363c3c85SNick Thomas     acb->bh = NULL;
653363c3c85SNick Thomas 
654363c3c85SNick Thomas     size_t start = acb->sector_num * SECTOR_SIZE;
655363c3c85SNick Thomas     size_t end;
656769ce76dSAlexander Graf 
657769ce76dSAlexander Graf     // In case we have the requested data already (e.g. read-ahead),
658769ce76dSAlexander Graf     // we can just call the callback and be done.
659363c3c85SNick Thomas     switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
660769ce76dSAlexander Graf         case FIND_RET_OK:
6618007429aSFam Zheng             qemu_aio_unref(acb);
662769ce76dSAlexander Graf             // fall through
663769ce76dSAlexander Graf         case FIND_RET_WAIT:
664363c3c85SNick Thomas             return;
665769ce76dSAlexander Graf         default:
666769ce76dSAlexander Graf             break;
667769ce76dSAlexander Graf     }
668769ce76dSAlexander Graf 
669769ce76dSAlexander Graf     // No cache found, so let's start a new request
670a2f468e4SRichard W.M. Jones     state = curl_init_state(acb->common.bs, s);
671363c3c85SNick Thomas     if (!state) {
672363c3c85SNick Thomas         acb->common.cb(acb->common.opaque, -EIO);
6738007429aSFam Zheng         qemu_aio_unref(acb);
674363c3c85SNick Thomas         return;
675363c3c85SNick Thomas     }
676769ce76dSAlexander Graf 
677769ce76dSAlexander Graf     acb->start = 0;
678363c3c85SNick Thomas     acb->end = (acb->nb_sectors * SECTOR_SIZE);
679769ce76dSAlexander Graf 
680769ce76dSAlexander Graf     state->buf_off = 0;
6817267c094SAnthony Liguori     g_free(state->orig_buf);
682769ce76dSAlexander Graf     state->buf_start = start;
683c76f4952SNolan     state->buf_len = acb->end + s->readahead_size;
684769ce76dSAlexander Graf     end = MIN(start + state->buf_len, s->len) - 1;
6858dc7a772SKevin Wolf     state->orig_buf = g_try_malloc(state->buf_len);
6868dc7a772SKevin Wolf     if (state->buf_len && state->orig_buf == NULL) {
6878dc7a772SKevin Wolf         curl_clean_state(state);
6888dc7a772SKevin Wolf         acb->common.cb(acb->common.opaque, -ENOMEM);
6898007429aSFam Zheng         qemu_aio_unref(acb);
6908dc7a772SKevin Wolf         return;
6918dc7a772SKevin Wolf     }
692769ce76dSAlexander Graf     state->acb[0] = acb;
693769ce76dSAlexander Graf 
6940bfcd599SBlue Swirl     snprintf(state->range, 127, "%zd-%zd", start, end);
6950bfcd599SBlue Swirl     DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
696363c3c85SNick Thomas             (acb->nb_sectors * SECTOR_SIZE), start, state->range);
697769ce76dSAlexander Graf     curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
698769ce76dSAlexander Graf 
699769ce76dSAlexander Graf     curl_multi_add_handle(s->multi, state->curl);
700769ce76dSAlexander Graf 
701b69cdef8SMatthew Booth     /* Tell curl it needs to kick things off */
702b69cdef8SMatthew Booth     curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
703363c3c85SNick Thomas }
704363c3c85SNick Thomas 
7057c84b1b8SMarkus Armbruster static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
706363c3c85SNick Thomas         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
707097310b5SMarkus Armbruster         BlockCompletionFunc *cb, void *opaque)
708363c3c85SNick Thomas {
709363c3c85SNick Thomas     CURLAIOCB *acb;
710363c3c85SNick Thomas 
711d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
712363c3c85SNick Thomas 
713363c3c85SNick Thomas     acb->qiov = qiov;
714363c3c85SNick Thomas     acb->sector_num = sector_num;
715363c3c85SNick Thomas     acb->nb_sectors = nb_sectors;
716363c3c85SNick Thomas 
71763f0f45fSStefan Hajnoczi     acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
718363c3c85SNick Thomas     qemu_bh_schedule(acb->bh);
719769ce76dSAlexander Graf     return &acb->common;
720769ce76dSAlexander Graf }
721769ce76dSAlexander Graf 
722769ce76dSAlexander Graf static void curl_close(BlockDriverState *bs)
723769ce76dSAlexander Graf {
724769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
725769ce76dSAlexander Graf 
726d0f2c4c6Smalc     DPRINTF("CURL: Close\n");
72763f0f45fSStefan Hajnoczi     curl_detach_aio_context(bs);
728031fd1beSPeter Maydell 
729a94f83d9SRichard W.M. Jones     g_free(s->cookie);
73045724d6dSStefan Weil     g_free(s->url);
731769ce76dSAlexander Graf }
732769ce76dSAlexander Graf 
733769ce76dSAlexander Graf static int64_t curl_getlength(BlockDriverState *bs)
734769ce76dSAlexander Graf {
735769ce76dSAlexander Graf     BDRVCURLState *s = bs->opaque;
736769ce76dSAlexander Graf     return s->len;
737769ce76dSAlexander Graf }
738769ce76dSAlexander Graf 
739769ce76dSAlexander Graf static BlockDriver bdrv_http = {
740769ce76dSAlexander Graf     .format_name                = "http",
741769ce76dSAlexander Graf     .protocol_name              = "http",
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_https = {
756769ce76dSAlexander Graf     .format_name                = "https",
757769ce76dSAlexander Graf     .protocol_name              = "https",
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_ftp = {
772769ce76dSAlexander Graf     .format_name                = "ftp",
773769ce76dSAlexander Graf     .protocol_name              = "ftp",
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_ftps = {
788769ce76dSAlexander Graf     .format_name                = "ftps",
789769ce76dSAlexander Graf     .protocol_name              = "ftps",
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 BlockDriver bdrv_tftp = {
804769ce76dSAlexander Graf     .format_name                = "tftp",
805769ce76dSAlexander Graf     .protocol_name              = "tftp",
806769ce76dSAlexander Graf 
807769ce76dSAlexander Graf     .instance_size              = sizeof(BDRVCURLState),
8088e6d58cdSKevin Wolf     .bdrv_parse_filename        = curl_parse_filename,
80966f82ceeSKevin Wolf     .bdrv_file_open             = curl_open,
810769ce76dSAlexander Graf     .bdrv_close                 = curl_close,
811769ce76dSAlexander Graf     .bdrv_getlength             = curl_getlength,
812769ce76dSAlexander Graf 
813769ce76dSAlexander Graf     .bdrv_aio_readv             = curl_aio_readv,
81463f0f45fSStefan Hajnoczi 
81563f0f45fSStefan Hajnoczi     .bdrv_detach_aio_context    = curl_detach_aio_context,
81663f0f45fSStefan Hajnoczi     .bdrv_attach_aio_context    = curl_attach_aio_context,
817769ce76dSAlexander Graf };
818769ce76dSAlexander Graf 
819769ce76dSAlexander Graf static void curl_block_init(void)
820769ce76dSAlexander Graf {
821769ce76dSAlexander Graf     bdrv_register(&bdrv_http);
822769ce76dSAlexander Graf     bdrv_register(&bdrv_https);
823769ce76dSAlexander Graf     bdrv_register(&bdrv_ftp);
824769ce76dSAlexander Graf     bdrv_register(&bdrv_ftps);
825769ce76dSAlexander Graf     bdrv_register(&bdrv_tftp);
826769ce76dSAlexander Graf }
827769ce76dSAlexander Graf 
828769ce76dSAlexander Graf block_init(curl_block_init);
829